Separate PassInfo into two classes: a constructor-free superclass (StaticPassInfo...
[oota-llvm.git] / lib / Analysis / LiveValues.cpp
index 2634463df2096f088a049cee469ded644291c008..23964ffc457ed5cd305efb61de02bec1f0c06862 100644 (file)
@@ -17,7 +17,9 @@
 #include "llvm/Analysis/LoopInfo.h"
 using namespace llvm;
 
-FunctionPass *llvm::createLiveValuesPass() { return new LiveValues(); }
+namespace llvm {
+  FunctionPass *createLiveValuesPass() { return new LiveValues(); }
+}
 
 char LiveValues::ID = 0;
 static RegisterPass<LiveValues>
@@ -123,7 +125,7 @@ LiveValues::Memo &LiveValues::compute(const Value *V) {
   bool LiveOutOfDefBB = false;
 
   // Examine each use of the value.
-  for (Value::use_const_iterator I = V->use_begin(), E = V->use_end();
+  for (Value::const_use_iterator I = V->use_begin(), E = V->use_end();
        I != E; ++I) {
     const User *U = *I;
     const BasicBlock *UseBB = cast<Instruction>(U)->getParent();
@@ -131,27 +133,41 @@ LiveValues::Memo &LiveValues::compute(const Value *V) {
     // Note the block in which this use occurs.
     M.Used.insert(UseBB);
 
+    // If the use block doesn't have successors, the value can be
+    // considered killed.
+    if (succ_begin(UseBB) == succ_end(UseBB))
+      M.Killed.insert(UseBB);
+
     // Observe whether the value is used outside of the loop in which
     // it is defined. Switch to an enclosing loop if necessary.
     for (; L; L = L->getParentLoop())
       if (L->contains(UseBB))
         break;
 
-    if (isa<PHINode>(U)) {
-      // The value is used by a PHI, so it is live-out of the defining block.
-      LiveOutOfDefBB = true;
-    } else if (UseBB != DefBB) {
-      // A use outside the defining block has been found.
+    // Search for live-through blocks.
+    const BasicBlock *BB;
+    if (const PHINode *PHI = dyn_cast<PHINode>(U)) {
+      // For PHI nodes, start the search at the incoming block paired with the
+      // incoming value, which must be dominated by the definition.
+      unsigned Num = PHI->getIncomingValueNumForOperand(I.getOperandNo());
+      BB = PHI->getIncomingBlock(Num);
+
+      // A PHI-node use means the value is live-out of it's defining block
+      // even if that block also contains the only use.
       LiveOutOfDefBB = true;
+    } else {
+      // Otherwise just start the search at the use.
+      BB = UseBB;
 
-      // Climb the immediate dominator tree from the use to the definition
-      // and mark all intermediate blocks as live-through. Don't do this if
-      // the user is a PHI because such users may not be dominated by the
-      // definition.
-      for (const BasicBlock *BB = getImmediateDominator(UseBB, DT);
-           BB != DefBB; BB = getImmediateDominator(BB, DT))
-        if (!M.LiveThrough.insert(BB))
-          break;
+      // Note if the use is outside the defining block.
+      LiveOutOfDefBB |= UseBB != DefBB;
+    }
+
+    // Climb the immediate dominator tree from the use to the definition
+    // and mark all intermediate blocks as live-through.
+    for (; BB != DefBB; BB = getImmediateDominator(BB, DT)) {
+      if (BB != UseBB && !M.LiveThrough.insert(BB))
+        break;
     }
   }
 
@@ -168,7 +184,7 @@ LiveValues::Memo &LiveValues::compute(const Value *V) {
     }
   }
 
-  // If the value was never used outside the the block in which it was
+  // If the value was never used outside the block in which it was
   // defined, it's killed in that block.
   if (!LiveOutOfDefBB)
     M.Killed.insert(DefBB);