Simplify the logic of getting hold of a PHI predecessor block.
authorGabor Greif <ggreif@gmail.com>
Fri, 23 Jan 2009 19:40:15 +0000 (19:40 +0000)
committerGabor Greif <ggreif@gmail.com>
Fri, 23 Jan 2009 19:40:15 +0000 (19:40 +0000)
There is now a direct way from value-use-iterator to incoming block in PHINode's API.
This way we avoid the iterator->index->iterator trip, and especially the costly
getOperandNo() invocation. Additionally there is now an assertion that the iterator
really refers to one of the PHI's Uses.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@62869 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/Analysis/LoopInfo.h
include/llvm/Instructions.h
lib/Transforms/IPO/GlobalOpt.cpp
lib/Transforms/Scalar/CodeGenPrepare.cpp
lib/Transforms/Utils/LCSSA.cpp
lib/VMCore/Instruction.cpp

index 7aa325a9d1d8bbe9754aebf59059ff42f5adab6f..a1bac66390777bb07f1c6d2e809a210851982649 100644 (file)
@@ -482,8 +482,7 @@ public:
              ++UI) {
           BlockT *UserBB = cast<Instruction>(*UI)->getParent();
           if (PHINode *P = dyn_cast<PHINode>(*UI)) {
-            unsigned OperandNo = UI.getOperandNo();
-            UserBB = P->getIncomingBlock(OperandNo/2);
+            UserBB = P->getIncomingBlock(UI);
           }
 
           // Check the current block, as a fast-path.  Most values are used in
index 841cf9cff27ff4e674615dd54eb4e02343f46917..fbf376afd5d4db0b9207b9d992edc3650923c09d 100644 (file)
@@ -1943,6 +1943,14 @@ public:
     return i*2;
   }
 
+  /// getIncomingBlock - Return incoming basic block corresponding
+  /// to value use iterator
+  ///
+  template <typename U>
+  BasicBlock *getIncomingBlock(value_use_iterator<U> I) const {
+    assert(this == *I && "Iterator doesn't point to PHI's Uses?");
+    return static_cast<BasicBlock*>((&I.getUse() + 1)->get());
+  }
   /// getIncomingBlock - Return incoming basic block number x
   ///
   BasicBlock *getIncomingBlock(unsigned i) const {
index 99fc7317b1fab1b54649a692d96dab0008cb4979..7f636c9270154cde574539b220d39e2092722479 100644 (file)
@@ -988,8 +988,7 @@ static void ReplaceUsesOfMallocWithGlobal(Instruction *Alloc,
     } else if (PHINode *PN = dyn_cast<PHINode>(U)) {
       // Insert the load in the corresponding predecessor, not right before the
       // PHI.
-      unsigned PredNo = Alloc->use_begin().getOperandNo()/2;
-      InsertPt = PN->getIncomingBlock(PredNo)->getTerminator();
+      InsertPt = PN->getIncomingBlock(Alloc->use_begin())->getTerminator();
     } else if (isa<BitCastInst>(U)) {
       // Must be bitcast between the malloc and store to initialize the global.
       ReplaceUsesOfMallocWithGlobal(U, GV);
index 59c6586532c08c12c0d5320cd97e304fd21721aa..0a1c641e2bf4ed2c97eaf3864c66d011419a3488 100644 (file)
@@ -459,8 +459,7 @@ static bool OptimizeNoopCopyExpression(CastInst *CI, const TargetLowering &TLI){
     // appropriate predecessor block.
     BasicBlock *UserBB = User->getParent();
     if (PHINode *PN = dyn_cast<PHINode>(User)) {
-      unsigned OpVal = UI.getOperandNo()/2;
-      UserBB = PN->getIncomingBlock(OpVal);
+      UserBB = PN->getIncomingBlock(UI);
     }
 
     // Preincrement use iterator so we don't invalidate it.
index 385588861154b2ac3aeb0cbfcdb00b3b3f71bcee..9cd7e69557ad41a7b92c31560ae1e8d25ee9831a 100644 (file)
@@ -175,8 +175,7 @@ void LCSSA::ProcessInstruction(Instruction *Instr,
        UI != E;) {
     BasicBlock *UserBB = cast<Instruction>(*UI)->getParent();
     if (PHINode *P = dyn_cast<PHINode>(*UI)) {
-      unsigned OperandNo = UI.getOperandNo();
-      UserBB = P->getIncomingBlock(OperandNo/2);
+      UserBB = P->getIncomingBlock(UI);
     }
     
     // If the user is in the loop, don't rewrite it!
@@ -212,8 +211,7 @@ void LCSSA::getLoopValuesUsedOutsideLoop(Loop *L,
            ++UI) {
         BasicBlock *UserBB = cast<Instruction>(*UI)->getParent();
         if (PHINode* p = dyn_cast<PHINode>(*UI)) {
-          unsigned OperandNo = UI.getOperandNo();
-          UserBB = p->getIncomingBlock(OperandNo/2);
+          UserBB = p->getIncomingBlock(UI);
         }
         
         if (*BB != UserBB && !inLoop(UserBB)) {
index b09ab93aa11d7885292a16d71486df9bf5ca1bfc..f33c1a23f2389c2c9e97844b00c6da88717114b2 100644 (file)
@@ -278,8 +278,7 @@ bool Instruction::isUsedOutsideOfBlock(const BasicBlock *BB) const {
       continue;
     }
     
-    unsigned UseOperand = UI.getOperandNo();
-    if (PN->getIncomingBlock(UseOperand/2) != BB)
+    if (PN->getIncomingBlock(UI) != BB)
       return true;
   }
   return false;