Index: lib/Transforms/Scalar/IndVarSimplify.cpp =================================================================== --- lib/Transforms/Scalar/IndVarSimplify.cpp +++ lib/Transforms/Scalar/IndVarSimplify.cpp @@ -1689,7 +1689,8 @@ /// loop to be a canonical != comparison against the incremented loop induction /// variable. This pass is able to rewrite the exit tests of any loop where the /// SCEV analysis can determine a loop-invariant trip count of the loop, which -/// is actually a much broader range than just linear tests. +/// is actually a much broader range than just linear tests. Returns nullptr on +/// failure. Value *IndVarSimplify:: LinearFunctionTestReplace(Loop *L, const SCEV *BackedgeTakenCount, @@ -1740,16 +1741,17 @@ SE->getAddExpr(SE->getZeroExtendExpr(IVInit, WideTy), SE->getZeroExtendExpr(BackedgeTakenCount, WideTy))) WrappingFlags = ScalarEvolution::clearFlags(WrappingFlags, SCEV::FlagNUW); - if (!ScalarEvolution::maskFlags(IncrementedIndvarSCEV->getNoWrapFlags(), - WrappingFlags)) { - // Add one to the "backedge-taken" count to get the trip count. - // This addition may overflow, which is valid as long as the comparison is - // truncated to BackedgeTakenCount->getType(). - IVCount = - SE->getAddExpr(BackedgeTakenCount, - SE->getConstant(BackedgeTakenCount->getType(), 1)); - CmpIndVar = IncrementedIndvar; - } + if (ScalarEvolution::maskFlags(IncrementedIndvarSCEV->getNoWrapFlags(), + WrappingFlags)) + return nullptr; + + // Add one to the "backedge-taken" count to get the trip count. This + // addition may overflow, which is valid as long as the comparison is + // truncated to BackedgeTakenCount->getType(). + IVCount = + SE->getAddExpr(BackedgeTakenCount, + SE->getConstant(BackedgeTakenCount->getType(), 1)); + CmpIndVar = IncrementedIndvar; } Value *ExitCnt = genLoopLimit(IndVar, IVCount, L, Rewriter, SE); Index: test/Transforms/IndVarSimplify/lftr-bad-ivcount.ll =================================================================== --- /dev/null +++ test/Transforms/IndVarSimplify/lftr-bad-ivcount.ll @@ -0,0 +1,21 @@ +; RUN: opt -S -indvars < %s | FileCheck %s + +define void @test1(i32 %data_len, i32 %sample) { +; CHECK-LABEL: @test1( +entry: +; CHECK: [[IVCount:[^ ]+]] = sub i32 %data_len, %sample + %sub = sub i32 %data_len, %sample + %cmp4 = icmp eq i32 %data_len, %sample + br i1 %cmp4, label %for.end, label %for.body + +for.body: ; preds = %entry, %for.body + %indvars.iv = phi i64 [ %indvars.iv.next, %for.body ], [ 0, %entry ] + %indvars.iv.next = add nuw i64 %indvars.iv, 1 + %tr = trunc i64 %indvars.iv.next to i32 + %cmp = icmp ult i32 %tr, %sub +; CHECK: %cmp = icmp ult i32 %tr, [[IVCount]] + br i1 %cmp, label %for.body, label %for.end + +for.end: ; preds = %for.body, %entry + ret void +}