]> granicus.if.org Git - llvm/commitdiff
[CVP] Adjust iteration order to reduce the amount of work required
authorPhilip Reames <listmail@philipreames.com>
Fri, 30 Dec 2016 18:00:55 +0000 (18:00 +0000)
committerPhilip Reames <listmail@philipreames.com>
Fri, 30 Dec 2016 18:00:55 +0000 (18:00 +0000)
CVP doesn't care about the order of blocks visited, but by using a pre-order traversal over the graph we can a) not visit unreachable blocks and b) optimize as we go so that analysis of later blocks produce slightly more precise results.

I noticed this via inspection and don't have a concrete example which points to the issue.

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

lib/Transforms/Scalar/CorrelatedValuePropagation.cpp

index de62d9de7a8a5da8a465fd79b7036f6dd745306e..141e99b92cdc1a1dbabe4908127429129d035cf6 100644 (file)
@@ -481,9 +481,14 @@ static Constant *getConstantAt(Value *V, Instruction *At, LazyValueInfo *LVI) {
 static bool runImpl(Function &F, LazyValueInfo *LVI) {
   bool FnChanged = false;
 
-  for (BasicBlock &BB : F) {
+  // Visiting in a pre-order depth-first traversal causes us to simplify early
+  // blocks before querying later blocks (which require us to analyze early
+  // blocks).  Eagerly simplifying shallow blocks means there is strictly less
+  // work to do for deep blocks.  This also means we don't visit unreachable
+  // blocks. 
+  for (BasicBlock *BB : depth_first(&F.getEntryBlock())) {
     bool BBChanged = false;
-    for (BasicBlock::iterator BI = BB.begin(), BE = BB.end(); BI != BE;) {
+    for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI != BE;) {
       Instruction *II = &*BI++;
       switch (II->getOpcode()) {
       case Instruction::Select:
@@ -519,7 +524,7 @@ static bool runImpl(Function &F, LazyValueInfo *LVI) {
       }
     }
 
-    Instruction *Term = BB.getTerminator();
+    Instruction *Term = BB->getTerminator();
     switch (Term->getOpcode()) {
     case Instruction::Switch:
       BBChanged |= processSwitch(cast<SwitchInst>(Term), LVI);