[PATCH] D101174: [IRCE] Relieve bound check on isSafeIncreasingBound and isSafeDecreasingBound

JinGu Kang via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Apr 23 09:04:03 PDT 2021


jaykang10 created this revision.
jaykang10 added reviewers: mkazantsev, samparker, reames.
Herald added a subscriber: hiraditya.
jaykang10 requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

I have seen IRCE pass fails to check of new loop's bound `parseLoopStructure`.

On `isSafeIncreasingBound` and `isSafeDecreasingBound`, `SE.isLoopEntryGuardedByCond` often fails to check the validity of new condition on new loop's entry. `isLoopEntryGuardedByCond` checks the condition with non-recursive way first. If it is failed, it starts to climb up the predecessor chain. At this moment, the new loop pass manager populates loops in forward program order. With the order, if there are sibling loops, the target loop's predecessor could be located in previously processed loop which has complex CFG from optimization. From this situation, it is not easy to expect `isLoopEntryGuardedByCond` returns true.

If it is possible, we need to relieve the bound checks in order to handle more cases. This patch suggests two things as below.

`isSafeIncreasingBound` checks `start < bound < limit`. In this case, we can skip the `lower bound` check because the only `upper bound` has been changed with new predicate for new loop.

`isSafeDecreasingBound` checks `limit < bound < start`. In this case, we can skip the `upper bound` check because the only `lower bound` has been changed with new predicate for new loop.


https://reviews.llvm.org/D101174

Files:
  llvm/lib/Transforms/Scalar/InductiveRangeCheckElimination.cpp
  llvm/test/Transforms/IRCE/variable-loop-bounds.ll


Index: llvm/test/Transforms/IRCE/variable-loop-bounds.ll
===================================================================
--- llvm/test/Transforms/IRCE/variable-loop-bounds.ll
+++ llvm/test/Transforms/IRCE/variable-loop-bounds.ll
@@ -9,6 +9,8 @@
 ; CHECK: irce: in function signed_var_imm_dec_sge: constrained Loop at depth 1 containing: %for.body<header>,%if.else,%for.inc<latch><exiting>
 ; CHECK: irce: in function signed_var_imm_dec_ne: constrained Loop at depth 1 containing: %for.body<header>,%if.else,%for.inc<latch><exiting>
 ; CHECK-NOT: irce: in function signed_var_imm_dec_eq: constrained Loop at depth 1 containing: %for.body<header>,%if.else,%for.inc<latch><exiting>
+; CHECK: irce: in function test_dec_bound: constrained Loop at depth 1 containing: %7<header>,%10,%11<latch><exiting>
+; CHECK: irce: in function test_inc_bound: constrained Loop at depth 1 containing: %2<header>,%5,%6<latch><exiting>
 
 ; CHECK-LABEL: test_inc_eq(
 ; CHECK: main.exit.selector:
@@ -352,3 +354,43 @@
   %cmp = icmp eq i32 %dec, %M
   br i1 %cmp, label %for.cond.cleanup, label %for.body
 }
+
+define void @test_dec_bound(i64 %0, i64 %1) {
+  br label %3
+
+3:                                                ; preds = %7, %2
+  %4 = phi i64 [ %8, %7 ], [ %1, %2 ]
+  %5 = icmp slt i64 %4, %0
+  br i1 %5, label %6, label %7
+
+6:                                                ; preds = %3
+  br label %7
+
+7:                                                ; preds = %6, %3
+  %8 = add nuw nsw i64 %4, -1
+  %9 = icmp eq i64 %8, 0
+  br i1 %9, label %10, label %3
+
+10:                                               ; preds = %7
+  ret void
+}
+
+define void @test_inc_bound(i64 %0) {
+  br label %2
+
+2:                                                ; preds = %6, %1
+  %3 = phi i64 [ %7, %6 ], [ 1, %1 ]
+  %4 = icmp slt i64 %3, %0
+  br i1 %4, label %5, label %6
+
+5:                                                ; preds = %2
+  br label %6
+
+6:                                                ; preds = %5, %2
+  %7 = add nuw nsw i64 %3, 1
+  %8 = icmp eq i64 %7, 0
+  br i1 %8, label %9, label %2
+
+9:                                                ; preds = %6
+  ret void
+}
Index: llvm/lib/Transforms/Scalar/InductiveRangeCheckElimination.cpp
===================================================================
--- llvm/lib/Transforms/Scalar/InductiveRangeCheckElimination.cpp
+++ llvm/lib/Transforms/Scalar/InductiveRangeCheckElimination.cpp
@@ -696,12 +696,7 @@
     APInt::getMinValue(BitWidth);
   const SCEV *Limit = SE.getMinusSCEV(SE.getConstant(Min), StepPlusOne);
 
-  const SCEV *MinusOne =
-    SE.getMinusSCEV(BoundSCEV, SE.getOne(BoundSCEV->getType()));
-
-  return SE.isLoopEntryGuardedByCond(L, BoundPred, Start, MinusOne) &&
-         SE.isLoopEntryGuardedByCond(L, BoundPred, BoundSCEV, Limit);
-
+  return SE.isLoopEntryGuardedByCond(L, BoundPred, BoundSCEV, Limit);
 }
 
 /// Given a loop with an increasing induction variable, is it possible to
@@ -744,9 +739,7 @@
     APInt::getMaxValue(BitWidth);
   const SCEV *Limit = SE.getMinusSCEV(SE.getConstant(Max), StepMinusOne);
 
-  return (SE.isLoopEntryGuardedByCond(L, BoundPred, Start,
-                                      SE.getAddExpr(BoundSCEV, Step)) &&
-          SE.isLoopEntryGuardedByCond(L, BoundPred, BoundSCEV, Limit));
+  return SE.isLoopEntryGuardedByCond(L, BoundPred, BoundSCEV, Limit);
 }
 
 Optional<LoopStructure>


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D101174.340047.patch
Type: text/x-patch
Size: 3421 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210423/c96394b2/attachment.bin>


More information about the llvm-commits mailing list