[llvm] r226804 - [NFC] Introduce a 'struct Range' for IRCE

Sanjoy Das sanjoy at playingwithpointers.com
Thu Jan 22 01:32:02 PST 2015


Author: sanjoy
Date: Thu Jan 22 03:32:02 2015
New Revision: 226804

URL: http://llvm.org/viewvc/llvm-project?rev=226804&view=rev
Log:
[NFC] Introduce a 'struct Range' for IRCE

Use the struct instead of a std::pair<Value *, Value *>.  This makes a
Range an obviously immutable object, and we can now assert that a
range is well-typed (Begin->getType() == End->getType()) on its
construction.


Modified:
    llvm/trunk/lib/Transforms/Scalar/InductiveRangeCheckElimination.cpp

Modified: llvm/trunk/lib/Transforms/Scalar/InductiveRangeCheckElimination.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/InductiveRangeCheckElimination.cpp?rev=226804&r1=226803&r2=226804&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/Scalar/InductiveRangeCheckElimination.cpp (original)
+++ llvm/trunk/lib/Transforms/Scalar/InductiveRangeCheckElimination.cpp Thu Jan 22 03:32:02 2015
@@ -138,9 +138,23 @@ public:
 
   BranchInst *getBranch() const { return Branch; }
 
-  /// Represents an integer range [Range.first, Range.second).  If Range.second
-  /// < Range.first, then the value denotes the empty range.
-  typedef std::pair<Value *, Value *> Range;
+  /// Represents an signed integer range [Range.getBegin(), Range.getEnd()).  If
+  /// R.getEnd() sle R.getBegin(), then R denotes the empty range.
+
+  class Range {
+    Value *Begin;
+    Value *End;
+
+  public:
+    Range(Value *Begin, Value *End) : Begin(Begin), End(End) {
+      assert(Begin->getType() == End->getType() && "ill-typed range!");
+    }
+
+    Type *getType() const { return Begin->getType(); }
+    Value *getBegin() const { return Begin; }
+    Value *getEnd() const { return End; }
+  };
+
   typedef SpecificBumpPtrAllocator<InductiveRangeCheck> AllocatorTy;
 
   /// This is the value the condition of the branch needs to evaluate to for the
@@ -710,9 +724,7 @@ Optional<LoopConstrainer::SubRanges>
 LoopConstrainer::calculateSubRanges(Value *&HeaderCountOut) const {
   IntegerType *Ty = cast<IntegerType>(LatchTakenCount->getType());
 
-  assert(Range.first->getType() == Range.second->getType() &&
-         "ill-typed range!");
-  if (Range.first->getType() != Ty)
+  if (Range.getType() != Ty)
     return None;
 
   SCEVExpander Expander(SE, "irce");
@@ -731,8 +743,8 @@ LoopConstrainer::calculateSubRanges(Valu
   ConstantInt *One = ConstantInt::get(Ty, 1);
   HeaderCountOut = MaybeSimplify(B.CreateAdd(LatchCountV, One, "header.count"));
 
-  const SCEV *RangeBegin = SE.getSCEV(Range.first);
-  const SCEV *RangeEnd = SE.getSCEV(Range.second);
+  const SCEV *RangeBegin = SE.getSCEV(Range.getBegin());
+  const SCEV *RangeEnd = SE.getSCEV(Range.getEnd());
   const SCEV *HeaderCountSCEV = SE.getSCEV(HeaderCountOut);
   const SCEV *Zero = SE.getConstant(Ty, 0);
 
@@ -741,12 +753,12 @@ LoopConstrainer::calculateSubRanges(Valu
   bool ProvablyNoPreloop =
       SE.isKnownPredicate(ICmpInst::ICMP_SLE, RangeBegin, Zero);
   if (!ProvablyNoPreloop)
-    Result.ExitPreLoopAt = ConstructSMinOf(HeaderCountOut, Range.first, B);
+    Result.ExitPreLoopAt = ConstructSMinOf(HeaderCountOut, Range.getBegin(), B);
 
   bool ProvablyNoPostLoop =
       SE.isKnownPredicate(ICmpInst::ICMP_SLE, HeaderCountSCEV, RangeEnd);
   if (!ProvablyNoPostLoop)
-    Result.ExitMainLoopAt = ConstructSMinOf(HeaderCountOut, Range.second, B);
+    Result.ExitMainLoopAt = ConstructSMinOf(HeaderCountOut, Range.getEnd(), B);
 
   return Result;
 }
@@ -1127,26 +1139,24 @@ InductiveRangeCheck::computeSafeIteratio
   Value *Begin = MaybeSimplify(B.CreateNeg(OffsetV));
   Value *End = MaybeSimplify(B.CreateSub(getLength(), OffsetV));
 
-  return std::make_pair(Begin, End);
+  return InductiveRangeCheck::Range(Begin, End);
 }
 
 static Optional<InductiveRangeCheck::Range>
 IntersectRange(const Optional<InductiveRangeCheck::Range> &R1,
                const InductiveRangeCheck::Range &R2, IRBuilder<> &B) {
-  assert(R2.first->getType() == R2.second->getType() && "ill-typed range!");
-
   if (!R1.hasValue())
     return R2;
   auto &R1Value = R1.getValue();
 
   // TODO: we could widen the smaller range and have this work; but for now we
   // bail out to keep things simple.
-  if (R1Value.first->getType() != R2.first->getType())
+  if (R1Value.getType() != R2.getType())
     return None;
 
-  Value *NewMin = ConstructSMaxOf(R1Value.first, R2.first, B);
-  Value *NewMax = ConstructSMinOf(R1Value.second, R2.second, B);
-  return std::make_pair(NewMin, NewMax);
+  Value *NewMin = ConstructSMaxOf(R1Value.getBegin(), R2.getBegin(), B);
+  Value *NewMax = ConstructSMinOf(R1Value.getEnd(), R2.getEnd(), B);
+  return InductiveRangeCheck::Range(NewMin, NewMax);
 }
 
 bool InductiveRangeCheckElimination::runOnLoop(Loop *L, LPPassManager &LPM) {





More information about the llvm-commits mailing list