[PATCH] D39590: [IRCE][NFC] Make range checl's End a non-null SCEV

Max Kazantsev via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Nov 3 03:28:22 PDT 2017


mkazantsev created this revision.

Currently, IRC contains `Begin` and `Step` as SCEVs and End as value.
Aside from that, End can also be `nullptr` which can be later conditionally
converted into a non-null SCEV.

To make this logic more transparent, this patch makes End a SCEV and
calculates it early, so that it is never a null.


https://reviews.llvm.org/D39590

Files:
  lib/Transforms/Scalar/InductiveRangeCheckElimination.cpp
  test/Transforms/IRCE/only-lower-check.ll


Index: test/Transforms/IRCE/only-lower-check.ll
===================================================================
--- test/Transforms/IRCE/only-lower-check.ll
+++ test/Transforms/IRCE/only-lower-check.ll
@@ -3,7 +3,7 @@
 ; CHECK: irce: loop has 1 inductive range checks:
 ; CHECK-NEXT: InductiveRangeCheck:
 ; CHECK-NEXT:   Kind: RANGE_CHECK_LOWER
-; CHECK-NEXT:   Begin: (-1 + %n)  Step: -1  End: (null)
+; CHECK-NEXT:   Begin: (-1 + %n)  Step: -1  End: 2147483647
 ; CHECK-NEXT:   CheckUse:   br i1 %abc, label %in.bounds, label %out.of.bounds, !prof !1 Operand: 0
 ; CHECK-NEXT: irce: in function only_lower_check: constrained Loop at depth 1 containing: %loop<header><exiting>,%in.bounds<latch><exiting>
 
Index: lib/Transforms/Scalar/InductiveRangeCheckElimination.cpp
===================================================================
--- lib/Transforms/Scalar/InductiveRangeCheckElimination.cpp
+++ lib/Transforms/Scalar/InductiveRangeCheckElimination.cpp
@@ -179,10 +179,7 @@
     OS << "  Step: ";
     Step->print(OS);
     OS << "  End: ";
-    if (End)
-      End->print(OS);
-    else
-      OS << "(null)";
+    End->print(OS);
     OS << "\n  CheckUse: ";
     getCheckUse()->getUser()->print(OS);
     OS << " Operand: " << getCheckUse()->getOperandNo() << "\n";
@@ -393,8 +390,23 @@
   if (!IsAffineIndex)
     return;
 
+  // We strengthen "0 <= I" to "0 <= I < INT_SMAX" and "I < L" to "0 <= I < L".
+  // We can potentially do much better here.
+  const SCEV *End = nullptr;
+  if (Length)
+    End = SE.getSCEV(Length);
+  else {
+    assert(RCKind == InductiveRangeCheck::RANGE_CHECK_LOWER && "invariant!");
+    unsigned BitWidth = cast<IntegerType>(Index->getType())->getBitWidth();
+    // So far we can only reach this point for Signed range check. This may
+    // change in future. In this case we will need to pick Unsigned max for the
+    // unsigned range check.
+    assert(IsSigned && "RANGE_CHECK_LOWER is only used for signed conditions!");
+    End = SE.getConstant(APInt::getSignedMaxValue(BitWidth));
+  }
+
   InductiveRangeCheck IRC;
-  IRC.End = Length ? SE.getSCEV(Length) : nullptr;
+  IRC.End = End;
   IRC.Begin = IndexAddRec->getStart();
   IRC.Step = IndexAddRec->getStepRecurrence(SE);
   IRC.CheckUse = &ConditionUse;
@@ -1630,17 +1642,7 @@
 
   const SCEV *M = SE.getMinusSCEV(C, A);
   const SCEV *Begin = SE.getNegativeSCEV(M);
-  const SCEV *UpperLimit = nullptr;
-
-  // We strengthen "0 <= I" to "0 <= I < INT_SMAX" and "I < L" to "0 <= I < L".
-  // We can potentially do much better here.
-  if (const SCEV *L = getEnd())
-    UpperLimit = L;
-  else {
-    assert(Kind == InductiveRangeCheck::RANGE_CHECK_LOWER && "invariant!");
-    unsigned BitWidth = cast<IntegerType>(IndVar->getType())->getBitWidth();
-    UpperLimit = SE.getConstant(APInt::getSignedMaxValue(BitWidth));
-  }
+  const SCEV *UpperLimit = getEnd();
 
   const SCEV *End = SE.getMinusSCEV(UpperLimit, M);
   return InductiveRangeCheck::Range(Begin, End);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D39590.121452.patch
Type: text/x-patch
Size: 2987 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20171103/23bdfa7b/attachment.bin>


More information about the llvm-commits mailing list