diff --git a/clang/lib/CodeGen/CGLoopInfo.h b/clang/lib/CodeGen/CGLoopInfo.h --- a/clang/lib/CodeGen/CGLoopInfo.h +++ b/clang/lib/CodeGen/CGLoopInfo.h @@ -75,6 +75,9 @@ /// Value for llvm.loop.pipeline.iicount metadata. unsigned PipelineInitiationInterval; + + /// Value to prevent Dead Loop Elimination. + bool NoProgress; }; /// Information used when generating a structured loop. @@ -205,7 +208,7 @@ void push(llvm::BasicBlock *Header, clang::ASTContext &Ctx, const clang::CodeGenOptions &CGOpts, llvm::ArrayRef Attrs, const llvm::DebugLoc &StartLoc, - const llvm::DebugLoc &EndLoc); + const llvm::DebugLoc &EndLoc, const bool NoProgress = false); /// End the current loop. void pop(); @@ -272,6 +275,9 @@ StagedAttrs.PipelineInitiationInterval = C; } + /// Set no progress for the next loop pushed. + void setNoProgress(bool P) { StagedAttrs.NoProgress = P; } + private: /// Returns true if there is LoopInfo on the stack. bool hasInfo() const { return !Active.empty(); } diff --git a/clang/lib/CodeGen/CGLoopInfo.cpp b/clang/lib/CodeGen/CGLoopInfo.cpp --- a/clang/lib/CodeGen/CGLoopInfo.cpp +++ b/clang/lib/CodeGen/CGLoopInfo.cpp @@ -418,10 +418,14 @@ LoopProperties.push_back(EndLoc.getAsMDNode()); } + LLVMContext &Ctx = Header->getContext(); + if (Attrs.NoProgress) + LoopProperties.push_back( + MDNode::get(Ctx, MDString::get(Ctx, "llvm.loop.noprogress"))); + assert(!!AccGroup == Attrs.IsParallel && "There must be an access group iff the loop is parallel"); if (Attrs.IsParallel) { - LLVMContext &Ctx = Header->getContext(); LoopProperties.push_back(MDNode::get( Ctx, {MDString::get(Ctx, "llvm.loop.parallel_accesses"), AccGroup})); } @@ -438,7 +442,7 @@ VectorizePredicateEnable(LoopAttributes::Unspecified), VectorizeWidth(0), InterleaveCount(0), UnrollCount(0), UnrollAndJamCount(0), DistributeEnable(LoopAttributes::Unspecified), PipelineDisabled(false), - PipelineInitiationInterval(0) {} + PipelineInitiationInterval(0), NoProgress(false) {} void LoopAttributes::clear() { IsParallel = false; @@ -453,6 +457,7 @@ DistributeEnable = LoopAttributes::Unspecified; PipelineDisabled = false; PipelineInitiationInterval = 0; + NoProgress = false; } LoopInfo::LoopInfo(BasicBlock *Header, const LoopAttributes &Attrs, @@ -476,7 +481,7 @@ Attrs.UnrollEnable == LoopAttributes::Unspecified && Attrs.UnrollAndJamEnable == LoopAttributes::Unspecified && Attrs.DistributeEnable == LoopAttributes::Unspecified && !StartLoc && - !EndLoc) + !EndLoc && !Attrs.NoProgress) return; TempLoopID = MDNode::getTemporary(Header->getContext(), None); @@ -577,8 +582,7 @@ const clang::CodeGenOptions &CGOpts, ArrayRef Attrs, const llvm::DebugLoc &StartLoc, - const llvm::DebugLoc &EndLoc) { - + const llvm::DebugLoc &EndLoc, const bool NoProgress) { // Identify loop hint attributes from Attrs. for (const auto *Attr : Attrs) { const LoopHintAttr *LH = dyn_cast(Attr); @@ -755,6 +759,8 @@ } } + setNoProgress(NoProgress); + if (CGOpts.OptimizationLevel > 0) // Disable unrolling for the loop, if unrolling is disabled (via // -fno-unroll-loops) and no pragmas override the decision. diff --git a/clang/lib/CodeGen/CGStmt.cpp b/clang/lib/CodeGen/CGStmt.cpp --- a/clang/lib/CodeGen/CGStmt.cpp +++ b/clang/lib/CodeGen/CGStmt.cpp @@ -736,10 +736,26 @@ JumpDest LoopHeader = getJumpDestInCurrentScope("while.cond"); EmitBlock(LoopHeader.getBlock()); + // Evaluate the conditional in the while header. C99 6.8.5.1: The + // evaluation of the controlling expression takes place before each + // execution of the loop body. + llvm::Value *BoolCondVal = EvaluateExprAsBool(S.getCond()); + + // while(1) is common, avoid extra exit blocks. Be sure + // to correctly handle break/continue though. + bool EmitBoolCondBranch = true; + bool NoProgress = false; + if (llvm::ConstantInt *C = dyn_cast(BoolCondVal)) + if (C->isOne()) { + EmitBoolCondBranch = false; + CurFn->addFnAttr(llvm::Attribute::NoProgress); + NoProgress = true; + } + const SourceRange &R = S.getSourceRange(); LoopStack.push(LoopHeader.getBlock(), CGM.getContext(), CGM.getCodeGenOpts(), WhileAttrs, SourceLocToDebugLoc(R.getBegin()), - SourceLocToDebugLoc(R.getEnd())); + SourceLocToDebugLoc(R.getEnd()), NoProgress); // Create an exit block for when the condition fails, which will // also become the break target. @@ -760,18 +776,6 @@ if (S.getConditionVariable()) EmitDecl(*S.getConditionVariable()); - // Evaluate the conditional in the while header. C99 6.8.5.1: The - // evaluation of the controlling expression takes place before each - // execution of the loop body. - llvm::Value *BoolCondVal = EvaluateExprAsBool(S.getCond()); - - // while(1) is common, avoid extra exit blocks. Be sure - // to correctly handle break/continue though. - bool EmitBoolCondBranch = true; - if (llvm::ConstantInt *C = dyn_cast(BoolCondVal)) - if (C->isOne()) - EmitBoolCondBranch = false; - // As long as the condition is true, go to the loop body. llvm::BasicBlock *LoopBody = createBasicBlock("while.body"); if (EmitBoolCondBranch) { @@ -838,27 +842,33 @@ EmitBlock(LoopCond.getBlock()); - const SourceRange &R = S.getSourceRange(); - LoopStack.push(LoopBody, CGM.getContext(), CGM.getCodeGenOpts(), DoAttrs, - SourceLocToDebugLoc(R.getBegin()), - SourceLocToDebugLoc(R.getEnd())); - - // C99 6.8.5.2: "The evaluation of the controlling expression takes place - // after each execution of the loop body." - // Evaluate the conditional in the while header. // C99 6.8.5p2/p4: The first substatement is executed if the expression // compares unequal to 0. The condition must be a scalar type. llvm::Value *BoolCondVal = EvaluateExprAsBool(S.getCond()); - BreakContinueStack.pop_back(); - // "do {} while (0)" is common in macros, avoid extra blocks. Be sure // to correctly handle break/continue though. bool EmitBoolCondBranch = true; - if (llvm::ConstantInt *C = dyn_cast(BoolCondVal)) + bool NoProgress = false; + if (llvm::ConstantInt *C = dyn_cast(BoolCondVal)) { if (C->isZero()) EmitBoolCondBranch = false; + if (C->isOne()) { + CurFn->addFnAttr(llvm::Attribute::NoProgress); + NoProgress = true; + } + } + + const SourceRange &R = S.getSourceRange(); + LoopStack.push(LoopBody, CGM.getContext(), CGM.getCodeGenOpts(), DoAttrs, + SourceLocToDebugLoc(R.getBegin()), + SourceLocToDebugLoc(R.getEnd()), NoProgress); + + // C99 6.8.5.2: "The evaluation of the controlling expression takes place + // after each execution of the loop body." + + BreakContinueStack.pop_back(); // As long as the condition is true, iterate the loop. if (EmitBoolCondBranch) { @@ -896,10 +906,20 @@ llvm::BasicBlock *CondBlock = Continue.getBlock(); EmitBlock(CondBlock); + bool NoProgress = false; + if (S.getCond()) { + llvm::Value *BoolCondVal = EvaluateExprAsBool(S.getCond()); + if (llvm::ConstantInt *C = dyn_cast(BoolCondVal)) + if (C->isOne()) { + CurFn->addFnAttr(llvm::Attribute::NoProgress); + NoProgress = true; + } + } + const SourceRange &R = S.getSourceRange(); LoopStack.push(CondBlock, CGM.getContext(), CGM.getCodeGenOpts(), ForAttrs, SourceLocToDebugLoc(R.getBegin()), - SourceLocToDebugLoc(R.getEnd())); + SourceLocToDebugLoc(R.getEnd()), NoProgress); // If the for loop doesn't have an increment we can just use the // condition as the continue block. Otherwise we'll need to create diff --git a/clang/test/CodeGen/attr-noprogress.c b/clang/test/CodeGen/attr-noprogress.c new file mode 100644 --- /dev/null +++ b/clang/test/CodeGen/attr-noprogress.c @@ -0,0 +1,52 @@ +// NOTE: Assertions have been autogenerated by utils/update_cc_test_checks.py UTC_ARGS: --check-attributes +// RUN: %clang_cc1 -S -emit-llvm %s -o - | FileCheck %s + +// CHECK: Function Attrs: noinline nounwind optnone noprogress +// CHECK-LABEL: @f( +// CHECK-NEXT: entry: +// CHECK-NEXT: br label [[FOR_COND:%.*]] +// CHECK: for.cond: +// CHECK-NEXT: br i1 true, label [[FOR_BODY:%.*]], label [[FOR_END:%.*]] +// CHECK: for.body: +// CHECK-NEXT: br label [[FOR_COND]], !llvm.loop [[LOOP2:!.]] +// CHECK: for.end: +// CHECK-NEXT: ret void +// +void f() { + for (; 1;) { + } +} + +// CHECK: Function Attrs: noinline nounwind optnone noprogress +// CHECK-LABEL: @w( +// CHECK-NEXT: entry: +// CHECK-NEXT: br label [[WHILE_BODY:%.*]] +// CHECK: while.body: +// CHECK-NEXT: br label [[WHILE_BODY]], !llvm.loop [[LOOP4:!.]] +// +void w() { + while (1) { + } +} + +// CHECK: Function Attrs: noinline nounwind optnone noprogress +// CHECK-LABEL: @d( +// CHECK-NEXT: entry: +// CHECK-NEXT: br label [[DO_BODY:%.*]] +// CHECK: do.body: +// CHECK-NEXT: br label [[DO_COND:%.*]] +// CHECK: do.cond: +// CHECK-NEXT: br i1 true, label [[DO_BODY]], label [[DO_END:%.*]], !llvm.loop [[LOOP5:!.]] +// CHECK: do.end: +// CHECK-NEXT: ret void +// +void d() { + do { + + } while (1); +} + +// CHECK: [[LOOP2]] = distinct !{[[LOOP2]], [[LOOP_MD:!.]]} +// CHECK: [[LOOP_MD]] = !{!"llvm.loop.noprogress"} +// CHECK: [[LOOP4]] = distinct !{[[LOOP4]], [[LOOP_MD]]} +// CHECK: [[LOOP5]] = distinct !{[[LOOP5]], [[LOOP_MD]]} diff --git a/clang/test/CodeGen/attr-noprogress.cpp b/clang/test/CodeGen/attr-noprogress.cpp new file mode 100644 --- /dev/null +++ b/clang/test/CodeGen/attr-noprogress.cpp @@ -0,0 +1,55 @@ +// NOTE: Assertions have been autogenerated by utils/update_cc_test_checks.py UTC_ARGS: --function-signature --check-attributes --force-update +// RUN: %clang_cc1 -S -emit-llvm %s -o - | FileCheck %s + +// CHECK: Function Attrs: noinline nounwind optnone noprogress +// CHECK-LABEL: define {{[^@]+}}@_Z1fv +// CHECK-SAME: () [[ATTR0:#.*]] { +// CHECK-NEXT: entry: +// CHECK-NEXT: br label [[FOR_COND:%.*]] +// CHECK: for.cond: +// CHECK-NEXT: br i1 true, label [[FOR_BODY:%.*]], label [[FOR_END:%.*]] +// CHECK: for.body: +// CHECK-NEXT: br label [[FOR_COND]], !llvm.loop [[LOOP2:!.]] +// CHECK: for.end: +// CHECK-NEXT: ret void +// +void f() { + for (; 1;) { + } +} + +// CHECK: Function Attrs: noinline nounwind optnone noprogress +// CHECK-LABEL: define {{[^@]+}}@_Z1wv +// CHECK-SAME: () [[ATTR0]] { +// CHECK-NEXT: entry: +// CHECK-NEXT: br label [[WHILE_BODY:%.*]] +// CHECK: while.body: +// CHECK-NEXT: br label [[WHILE_BODY]], !llvm.loop [[LOOP4:!.]] +// +void w() { + while (1) { + } +} + +// CHECK: Function Attrs: noinline nounwind optnone noprogress +// CHECK-LABEL: define {{[^@]+}}@_Z1dv +// CHECK-SAME: () [[ATTR0]] { +// CHECK-NEXT: entry: +// CHECK-NEXT: br label [[DO_BODY:%.*]] +// CHECK: do.body: +// CHECK-NEXT: br label [[DO_COND:%.*]] +// CHECK: do.cond: +// CHECK-NEXT: br i1 true, label [[DO_BODY]], label [[DO_END:%.*]], !llvm.loop [[LOOP5:!.]] +// CHECK: do.end: +// CHECK-NEXT: ret void +// +void d() { + do { + + } while (1); +} + +// CHECK: [[LOOP2]] = distinct !{[[LOOP2]], [[LOOP_MD:!.]]} +// CHECK: [[LOOP_MD]] = !{!"llvm.loop.noprogress"} +// CHECK: [[LOOP4]] = distinct !{[[LOOP4]], [[LOOP_MD]]} +// CHECK: [[LOOP5]] = distinct !{[[LOOP5]], [[LOOP_MD]]}