diff options
| author | Sam Parker <sam.parker@arm.com> | 2018-04-18 13:50:28 +0000 |
|---|---|---|
| committer | Sam Parker <sam.parker@arm.com> | 2018-04-18 13:50:28 +0000 |
| commit | 3c19051bf0c6e650acce03abb2eeb5feee3ce986 (patch) | |
| tree | aa37a2b3dedd645da4580000d111f1e53e0028a1 | |
| parent | 715596df1fd668941fb6c6321cc2e047d3685375 (diff) | |
| download | bcm5719-llvm-3c19051bf0c6e650acce03abb2eeb5feee3ce986.tar.gz bcm5719-llvm-3c19051bf0c6e650acce03abb2eeb5feee3ce986.zip | |
[IRCE] Only check for NSW on equality predicates
After investigation discussed in D45439, it would seem that the nsw
flag restriction is unnecessary in most cases. So the IsInductionVar
lambda has been removed, the functionality extracted, and now only
require nsw when using eq/ne predicates.
Differential Revision: https://reviews.llvm.org/D45617
llvm-svn: 330256
| -rw-r--r-- | llvm/lib/Transforms/Scalar/InductiveRangeCheckElimination.cpp | 43 | ||||
| -rw-r--r-- | llvm/test/Transforms/IRCE/stride_more_than_1.ll | 24 |
2 files changed, 32 insertions, 35 deletions
diff --git a/llvm/lib/Transforms/Scalar/InductiveRangeCheckElimination.cpp b/llvm/lib/Transforms/Scalar/InductiveRangeCheckElimination.cpp index 8d9e9581ac7..6d85245c33a 100644 --- a/llvm/lib/Transforms/Scalar/InductiveRangeCheckElimination.cpp +++ b/llvm/lib/Transforms/Scalar/InductiveRangeCheckElimination.cpp @@ -916,43 +916,28 @@ LoopStructure::parseLoopStructure(ScalarEvolution &SE, return AR->getNoWrapFlags(SCEV::FlagNSW) != SCEV::FlagAnyWrap; }; - // Here we check whether the suggested AddRec is an induction variable that - // can be handled (i.e. with known constant step), and if yes, calculate its - // step and identify whether it is increasing or decreasing. - auto IsInductionVar = [&](const SCEVAddRecExpr *AR, bool &IsIncreasing, - ConstantInt *&StepCI) { - if (!AR->isAffine()) - return false; - - // Currently we only work with induction variables that have been proved to - // not wrap. This restriction can potentially be lifted in the future. - - if (!HasNoSignedWrap(AR)) - return false; - - if (const SCEVConstant *StepExpr = - dyn_cast<SCEVConstant>(AR->getStepRecurrence(SE))) { - StepCI = StepExpr->getValue(); - assert(!StepCI->isZero() && "Zero step?"); - IsIncreasing = !StepCI->isNegative(); - return true; - } - - return false; - }; - // `ICI` is interpreted as taking the backedge if the *next* value of the // induction variable satisfies some constraint. const SCEVAddRecExpr *IndVarBase = cast<SCEVAddRecExpr>(LeftSCEV); - bool IsIncreasing = false; - bool IsSignedPredicate = true; - ConstantInt *StepCI; - if (!IsInductionVar(IndVarBase, IsIncreasing, StepCI)) { + if (!IndVarBase->isAffine()) { + FailureReason = "LHS in icmp not induction variable"; + return None; + } + const SCEV* StepRec = IndVarBase->getStepRecurrence(SE); + ConstantInt *StepCI = dyn_cast<SCEVConstant>(StepRec)->getValue(); + if (!StepCI) { FailureReason = "LHS in icmp not induction variable"; return None; } + if (ICI->isEquality() && !HasNoSignedWrap(IndVarBase)) { + FailureReason = "LHS in icmp needs nsw for equality predicates"; + return None; + } + assert(!StepCI->isZero() && "Zero step?"); + bool IsIncreasing = !StepCI->isNegative(); + bool IsSignedPredicate = ICmpInst::isSigned(Pred); const SCEV *StartNext = IndVarBase->getStart(); const SCEV *Addend = SE.getNegativeSCEV(IndVarBase->getStepRecurrence(SE)); const SCEV *IndVarStart = SE.getAddExpr(StartNext, Addend); diff --git a/llvm/test/Transforms/IRCE/stride_more_than_1.ll b/llvm/test/Transforms/IRCE/stride_more_than_1.ll index aed163680d4..8aaecac7d30 100644 --- a/llvm/test/Transforms/IRCE/stride_more_than_1.ll +++ b/llvm/test/Transforms/IRCE/stride_more_than_1.ll @@ -4,7 +4,7 @@ ; CHECK: irce: in function test_01: constrained Loop at depth 1 containing: %loop<header><exiting>,%in.bounds<latch><exiting> ; CHECK: irce: in function test_02: constrained Loop at depth 1 containing: %loop<header><exiting>,%in.bounds<latch><exiting> ; CHECK: irce: in function test_03: constrained Loop at depth 1 containing: %loop<header><exiting>,%in.bounds<latch><exiting> -; CHECK-NOT: irce: in function test_04: constrained Loop at depth 1 containing: %loop<header><exiting>,%in.bounds<latch><exiting> +; CHECK: irce: in function test_04: constrained Loop at depth 1 containing: %loop<header><exiting>,%in.bounds<latch><exiting> ; CHECK: irce: in function test_05: constrained Loop at depth 1 containing: %loop<header><exiting>,%in.bounds<latch><exiting> ; CHECK: irce: in function test_06: constrained Loop at depth 1 containing: %loop<header><exiting>,%in.bounds<latch><exiting> ; CHECK-NOT: irce: in function test_07: constrained Loop at depth 1 containing: %loop<header><exiting>,%in.bounds<latch><exiting> @@ -206,12 +206,24 @@ exit: ret void } -; IV = 0; IV <s MAX_INT; IV += 7; 0 <= Len <= MAX_INT - 6. IRCE is not allowed, -; because we cannot guarantee that IV + 7 will not exceed MAX_INT. -; Negative test. +; IV = 0; IV <s MAX_INT; IV += 7; 0 <= Len <= MAX_INT - 6. IRCE is allowed +; because the branch would fail once idx.next == MAX_INT - 1 keeping the +; access in bounds. define void @test_04(i32* %arr, i32* %a_len_ptr) { - -; CHECK: @test_04( + ; CHECK: @test_04( + ; CHECK: loop: + ; CHECK: [[IV:%[^ ]+]] = phi i32 + ; CHECK: [[IDX_NEXT:%[^ ]+]] = add i32 [[IV]], 7 + + ; CHECK: main.exit.selector: + ; CHECK: [[PSEUDO_PHI:%[^ ]+]] = phi i32 [ [[IDX_NEXT]], %in.bounds ] + ; CHECK: [[COND:%[^ ]+]] = icmp slt i32 [[PSEUDO_PHI]], 2147483647 + ; CHECK: br i1 [[COND]], label %main.pseudo.exit, label %exit + + ; CHECK: loop.postloop: + ; CHECK: [[IDX_POST:%[^ ]+]] = phi i32 + ; CHECK: [[COND_POST:%[^ ]+]] = icmp slt i32 [[IDX_POST]], %exit.mainloop.at + ; CHECK: br i1 [[COND_POST]], label %in.bounds.postloop, label %out.of.bounds.loopexit entry: %len = load i32, i32* %a_len_ptr, !range !2 |

