[llvm-commits] [llvm] r96080 - /llvm/trunk/lib/Transforms/Utils/BreakCriticalEdges.cpp

Chris Lattner sabre at nondot.org
Fri Feb 12 20:24:20 PST 2010


Author: lattner
Date: Fri Feb 12 22:24:19 2010
New Revision: 96080

URL: http://llvm.org/viewvc/llvm-project?rev=96080&view=rev
Log:
PHINode::getBasicBlockIndex is O(n) in the number of inputs
to a PHI, avoid it in the common case where the BB occurs
in the same index for multiple phis.  This speeds up CGP on
an insane testcase from 8.35 to 3.58s.

Modified:
    llvm/trunk/lib/Transforms/Utils/BreakCriticalEdges.cpp

Modified: llvm/trunk/lib/Transforms/Utils/BreakCriticalEdges.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Utils/BreakCriticalEdges.cpp?rev=96080&r1=96079&r2=96080&view=diff

==============================================================================
--- llvm/trunk/lib/Transforms/Utils/BreakCriticalEdges.cpp (original)
+++ llvm/trunk/lib/Transforms/Utils/BreakCriticalEdges.cpp Fri Feb 12 22:24:19 2010
@@ -179,7 +179,7 @@
   // Create a new basic block, linking it into the CFG.
   BasicBlock *NewBB = BasicBlock::Create(TI->getContext(),
                       TIBB->getName() + "." + DestBB->getName() + "_crit_edge");
-  // Create our unconditional branch...
+  // Create our unconditional branch.
   BranchInst::Create(DestBB, NewBB);
 
   // Branch to the new block, breaking the edge.
@@ -193,12 +193,19 @@
   // If there are any PHI nodes in DestBB, we need to update them so that they
   // merge incoming values from NewBB instead of from TIBB.
   //
+  unsigned BBIdx = 0;
   for (BasicBlock::iterator I = DestBB->begin(); isa<PHINode>(I); ++I) {
-    PHINode *PN = cast<PHINode>(I);
     // We no longer enter through TIBB, now we come in through NewBB.  Revector
     // exactly one entry in the PHI node that used to come from TIBB to come
     // from NewBB.
-    int BBIdx = PN->getBasicBlockIndex(TIBB);
+    PHINode *PN = cast<PHINode>(I);
+    
+    // Reuse the previous value of BBIdx if it lines up.  In cases where we have
+    // multiple phi nodes with *lots* of predecessors, this is a speed win
+    // because we don't have to scan the PHI looking for TIBB.  This happens
+    // because the BB list of PHI nodes are usually in the same order.
+    if (PN->getIncomingBlock(BBIdx) != TIBB)
+      BBIdx = PN->getBasicBlockIndex(TIBB);
     PN->setIncomingBlock(BBIdx, NewBB);
   }
   





More information about the llvm-commits mailing list