Add a note about a potential PIC optimization.
[oota-llvm.git] / include / llvm / Analysis / LoopInfo.h
index 4cb03fbd8e71c6dfb7d461243c85b635e3cda908..dff1624a5a0a4bb1b3d16bdaf62e038ff1ad40d5 100644 (file)
@@ -43,6 +43,8 @@
 #include <algorithm>
 #include <ostream>
 
+namespace llvm {
+
 template<typename T>
 static void RemoveFromVector(std::vector<T*> &V, T *N) {
   typename std::vector<T*>::iterator I = std::find(V.begin(), V.end(), N);
@@ -50,8 +52,6 @@ static void RemoveFromVector(std::vector<T*> &V, T *N) {
   V.erase(I);
 }
 
-namespace llvm {
-
 class DominatorTree;
 class LoopInfo;
 class PHINode;
@@ -80,7 +80,7 @@ public:
   /// Loop ctor - This creates an empty loop.
   LoopBase() : ParentLoop(0) {}
   ~LoopBase() {
-    for (unsigned i = 0, e = SubLoops.size(); i != e; ++i)
+    for (size_t i = 0, e = SubLoops.size(); i != e; ++i)
       delete SubLoops[i];
   }
 
@@ -404,19 +404,73 @@ public:
     if (BranchInst *BI = dyn_cast<BranchInst>(BackedgeBlock->getTerminator()))
       if (BI->isConditional()) {
         if (ICmpInst *ICI = dyn_cast<ICmpInst>(BI->getCondition())) {
-          if (ICI->getOperand(0) == Inc)
+          if (ICI->getOperand(0) == Inc) {
             if (BI->getSuccessor(0) == getHeader()) {
               if (ICI->getPredicate() == ICmpInst::ICMP_NE)
                 return ICI->getOperand(1);
             } else if (ICI->getPredicate() == ICmpInst::ICMP_EQ) {
               return ICI->getOperand(1);
             }
+          }
         }
       }
 
     return 0;
   }
   
+  /// getSmallConstantTripCount - Returns the trip count of this loop as a
+  /// normal unsigned value, if possible. Returns 0 if the trip count is unknown
+  /// of not constant. Will also return 0 if the trip count is very large 
+  /// (>= 2^32)
+  inline unsigned getSmallConstantTripCount() const {
+    Value* TripCount = this->getTripCount();
+    if (TripCount) {
+      if (ConstantInt *TripCountC = dyn_cast<ConstantInt>(TripCount)) {
+        // Guard against huge trip counts.
+        if (TripCountC->getValue().getActiveBits() <= 32) {
+          return (unsigned)TripCountC->getZExtValue();
+        }
+      }
+    }
+    return 0;
+  }
+
+  /// getSmallConstantTripMultiple - Returns the largest constant divisor of the
+  /// trip count of this loop as a normal unsigned value, if possible. This
+  /// means that the actual trip count is always a multiple of the returned
+  /// value (don't forget the trip count could very well be zero as well!).
+  ///
+  /// Returns 1 if the trip count is unknown or not guaranteed to be the
+  /// multiple of a constant (which is also the case if the trip count is simply
+  /// constant, use getSmallConstantTripCount for that case), Will also return 1
+  /// if the trip count is very large (>= 2^32).
+  inline unsigned getSmallConstantTripMultiple() const {
+    Value* TripCount = this->getTripCount();
+    // This will hold the ConstantInt result, if any
+    ConstantInt *Result = NULL;
+    if (TripCount) {
+      // See if the trip count is constant itself
+      Result = dyn_cast<ConstantInt>(TripCount);
+      // if not, see if it is a multiplication
+      if (!Result)
+        if (BinaryOperator *BO = dyn_cast<BinaryOperator>(TripCount)) {
+          switch (BO->getOpcode()) {
+          case BinaryOperator::Mul:
+            Result = dyn_cast<ConstantInt>(BO->getOperand(1));
+            break;
+          default: 
+            break;
+          }
+        }
+    }
+    // Guard against huge trip counts.
+    if (Result && Result->getValue().getActiveBits() <= 32) {
+      return (unsigned)Result->getZExtValue();
+    } else {
+      return 1;
+    }
+  }
+  
   /// isLCSSAForm - Return true if the Loop is in LCSSA form
   inline bool isLCSSAForm() const {
     // Sort the blocks vector so that we can use binary search to do quick
@@ -846,7 +900,8 @@ public:
            "This loop should not be inserted here!");
 
     // Check to see if it belongs in a child loop...
-    for (unsigned i = 0, e = Parent->SubLoops.size(); i != e; ++i)
+    for (unsigned i = 0, e = static_cast<unsigned>(Parent->SubLoops.size());
+         i != e; ++i)
       if (Parent->SubLoops[i]->contains(LHeader)) {
         InsertLoopInto(L, Parent->SubLoops[i]);
         return;
@@ -1011,7 +1066,4 @@ void LoopBase<BlockT>::addBasicBlockToLoop(BlockT *NewBB,
 
 } // End llvm namespace
 
-// Make sure that any clients of this file link in LoopInfo.cpp
-FORCE_DEFINING_FILE_TO_BE_LINKED(LoopInfo)
-
 #endif