Guard against huge loop trip counts in an APInt safe way.
authorReid Spencer <rspencer@reidspencer.com>
Fri, 2 Mar 2007 23:31:34 +0000 (23:31 +0000)
committerReid Spencer <rspencer@reidspencer.com>
Fri, 2 Mar 2007 23:31:34 +0000 (23:31 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@34858 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Transforms/Scalar/LoopUnroll.cpp

index d1770da8fce8534851540944cdcc84a48025f9f0..b0db806a495786e72733814b67912cccac900b78 100644 (file)
@@ -190,10 +190,15 @@ bool LoopUnroll::visitLoop(Loop *L) {
   ConstantInt *TripCountC = dyn_cast_or_null<ConstantInt>(L->getTripCount());
   if (!TripCountC) return Changed;  // Must have constant trip count!
 
-  uint64_t TripCountFull = TripCountC->getZExtValue();
-  if (TripCountFull != TripCountC->getZExtValue() || TripCountFull == 0)
+  // Guard against huge trip counts. This also guards against assertions in
+  // APInt from the use of getZExtValue, below.
+  if (TripCountC->getValue().getActiveBits() > 32)
     return Changed; // More than 2^32 iterations???
 
+  uint64_t TripCountFull = TripCountC->getZExtValue();
+  if (TripCountFull == 0)
+    return Changed; // Zero iteraitons?
+
   unsigned LoopSize = ApproximateLoopSize(L);
   DOUT << "Loop Unroll: F[" << Header->getParent()->getName()
        << "] Loop %" << Header->getName() << " Loop Size = "