From 2c966d209bd8c96cd767506d0e9f4b8445dcce38 Mon Sep 17 00:00:00 2001 From: Sam Parker Date: Thu, 12 Apr 2018 12:49:40 +0000 Subject: [PATCH] [IRCE] isKnownNonNegative helper function Created a helper function to query for non negative SCEVs. Uses the SGE predicate to catch constants that could be interpreted as negative. Differential Revision: https://reviews.llvm.org/D45481 llvm-svn: 329907 --- .../Scalar/InductiveRangeCheckElimination.cpp | 11 +++++++++-- test/Transforms/IRCE/variable-loop-bounds.ll | 2 +- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/lib/Transforms/Scalar/InductiveRangeCheckElimination.cpp b/lib/Transforms/Scalar/InductiveRangeCheckElimination.cpp index 330a2c234c3..8d9e9581ac7 100644 --- a/lib/Transforms/Scalar/InductiveRangeCheckElimination.cpp +++ b/lib/Transforms/Scalar/InductiveRangeCheckElimination.cpp @@ -804,6 +804,13 @@ static bool CannotBeMinInLoop(const SCEV *BoundSCEV, Loop *L, SE.getConstant(Min)); } +static bool isKnownNonNegativeInLoop(const SCEV *BoundSCEV, Loop *L, + ScalarEvolution &SE) { + const SCEV *Zero = SE.getZero(BoundSCEV->getType()); + return SE.isAvailableAtLoopEntry(BoundSCEV, L) && + SE.isLoopEntryGuardedByCond(L, ICmpInst::ICMP_SGE, BoundSCEV, Zero); +} + Optional LoopStructure::parseLoopStructure(ScalarEvolution &SE, BranchProbabilityInfo *BPI, Loop &L, @@ -963,8 +970,8 @@ LoopStructure::parseLoopStructure(ScalarEvolution &SE, // If both parts are known non-negative, it is profitable to use // unsigned comparison in increasing loop. This allows us to make the // comparison check against "RightSCEV + 1" more optimistic. - if (SE.isKnownNonNegative(IndVarStart) && - SE.isKnownNonNegative(RightSCEV)) + if (isKnownNonNegativeInLoop(IndVarStart, &L, SE) && + isKnownNonNegativeInLoop(RightSCEV, &L, SE)) Pred = ICmpInst::ICMP_ULT; else Pred = ICmpInst::ICMP_SLT; diff --git a/test/Transforms/IRCE/variable-loop-bounds.ll b/test/Transforms/IRCE/variable-loop-bounds.ll index d5a57093b32..5749b2c2d41 100644 --- a/test/Transforms/IRCE/variable-loop-bounds.ll +++ b/test/Transforms/IRCE/variable-loop-bounds.ll @@ -55,7 +55,7 @@ for.inc: ; CHECK-LABEL: test_inc_ne ; CHECK: main.exit.selector: ; CHECK: [[PSEUDO_PHI:%[^ ]+]] = phi i32 [ %inc, %for.inc ] -; CHECK: [[COND:%[^ ]+]] = icmp slt i32 [[PSEUDO_PHI]], %N +; CHECK: [[COND:%[^ ]+]] = icmp ult i32 [[PSEUDO_PHI]], %N ; CHECK: br i1 [[COND]], label %main.pseudo.exit, label %for.cond.cleanup.loopexit define void @test_inc_ne(i32* nocapture %a, i32* nocapture readonly %b, i32* nocapture readonly %c, i32 %N) { entry: