Refactor to share code to find the position of a basic block successor in the
authorBob Wilson <bob.wilson@apple.com>
Tue, 16 Feb 2010 19:49:17 +0000 (19:49 +0000)
committerBob Wilson <bob.wilson@apple.com>
Tue, 16 Feb 2010 19:49:17 +0000 (19:49 +0000)
terminator's list of successors.

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

include/llvm/Transforms/Utils/BasicBlockUtils.h
lib/Transforms/Scalar/GVN.cpp
lib/Transforms/Utils/BasicBlockUtils.cpp

index 3f4571ebdd703f7f8fcb17cc6e95e7572df2194e..281437150f2a9a8629284e07367212f2ecede853 100644 (file)
@@ -102,6 +102,11 @@ void FindFunctionBackedges(const Function &F,
 //
 void RemoveSuccessor(TerminatorInst *TI, unsigned SuccNum);
 
+/// SuccessorNumber - Search for the specified successor of basic block BB and
+/// return its position in the terminator instruction's list of successors.
+/// It is an error to call this with a block that is not a successor.
+unsigned SuccessorNumber(BasicBlock *BB, BasicBlock *Succ);
+
 /// isCriticalEdge - Return true if the specified edge is a critical edge.
 /// Critical edges are edges from a block with multiple successors to a block
 /// with multiple predecessors.
index bf4689274b82ad7419a9e1d9760de82c5f86f1a8..b3a8687fc0448b120bca1820359007ef2fa72994 100644 (file)
@@ -2141,14 +2141,7 @@ bool GVN::performPRE(Function &F) {
       // We can't do PRE safely on a critical edge, so instead we schedule
       // the edge to be split and perform the PRE the next time we iterate
       // on the function.
-      unsigned SuccNum = 0;
-      for (unsigned i = 0, e = PREPred->getTerminator()->getNumSuccessors();
-           i != e; ++i)
-        if (PREPred->getTerminator()->getSuccessor(i) == CurrentBlock) {
-          SuccNum = i;
-          break;
-        }
-
+      unsigned SuccNum = SuccessorNumber(PREPred, CurrentBlock);
       if (isCriticalEdge(PREPred->getTerminator(), SuccNum)) {
         toSplit.push_back(std::make_pair(PREPred->getTerminator(), SuccNum));
         continue;
index 7bc4fcdf368b72ce66201824f632c575e31688d6..60185d2687a5b5b6530627ab2c8dce43d112720c 100644 (file)
@@ -274,24 +274,30 @@ void llvm::RemoveSuccessor(TerminatorInst *TI, unsigned SuccNum) {
     ReplaceInstWithInst(TI, NewTI);
 }
 
-/// SplitEdge -  Split the edge connecting specified block. Pass P must 
-/// not be NULL. 
-BasicBlock *llvm::SplitEdge(BasicBlock *BB, BasicBlock *Succ, Pass *P) {
-  TerminatorInst *LatchTerm = BB->getTerminator();
-  unsigned SuccNum = 0;
+/// SuccessorNumber - Search for the specified successor of basic block BB and
+/// return its position in the terminator instruction's list of successors.
+/// It is an error to call this with a block that is not a successor.
+unsigned llvm::SuccessorNumber(BasicBlock *BB, BasicBlock *Succ) {
+  TerminatorInst *Term = BB->getTerminator();
 #ifndef NDEBUG
-  unsigned e = LatchTerm->getNumSuccessors();
+  unsigned e = Term->getNumSuccessors();
 #endif
   for (unsigned i = 0; ; ++i) {
     assert(i != e && "Didn't find edge?");
-    if (LatchTerm->getSuccessor(i) == Succ) {
-      SuccNum = i;
-      break;
-    }
+    if (Term->getSuccessor(i) == Succ)
+      return i;
   }
+  return 0;
+}
+
+/// SplitEdge -  Split the edge connecting specified block. Pass P must 
+/// not be NULL. 
+BasicBlock *llvm::SplitEdge(BasicBlock *BB, BasicBlock *Succ, Pass *P) {
+  unsigned SuccNum = SuccessorNumber(BB, Succ);
   
   // If this is a critical edge, let SplitCriticalEdge do it.
-  if (SplitCriticalEdge(BB->getTerminator(), SuccNum, P))
+  TerminatorInst *LatchTerm = BB->getTerminator();
+  if (SplitCriticalEdge(LatchTerm, SuccNum, P))
     return LatchTerm->getSuccessor(SuccNum);
 
   // If the edge isn't critical, then BB has a single successor or Succ has a