Make loop-simplify produce better loops by turning PHI nodes like X = phi [X, Y]
authorChris Lattner <sabre@nondot.org>
Wed, 10 Aug 2005 02:07:32 +0000 (02:07 +0000)
committerChris Lattner <sabre@nondot.org>
Wed, 10 Aug 2005 02:07:32 +0000 (02:07 +0000)
into just Y.  This often occurs when it seperates loops that have collapsed loop
headers.  This implements LoopSimplify/phi-node-simplify.ll

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

lib/Transforms/Utils/LoopSimplify.cpp

index da1a6550a497bb68304711ae208cd52f07d5c3b1..69b2ec444707d378020ef8898002f10ddd6f6148 100644 (file)
@@ -71,7 +71,7 @@ namespace {
       AU.addPreserved<ImmediateDominators>();
       AU.addPreserved<DominatorTree>();
       AU.addPreserved<DominanceFrontier>();
-      AU.addPreservedID(BreakCriticalEdgesID);  // No crit edges added....
+      AU.addPreservedID(BreakCriticalEdgesID);  // No critical edges added.
     }
   private:
     bool ProcessLoop(Loop *L);
@@ -190,8 +190,23 @@ bool LoopSimplify::ProcessLoop(Loop *L) {
     Changed = true;
   }
 
+  // Scan over the PHI nodes in the loop header.  Since they now have only two
+  // incoming values (the loop is canonicalized), we may have simplified the PHI
+  // down to 'X = phi [X, Y]', which should be replaced with 'Y'.
+  PHINode *PN;
+  DominatorSet &DS = getAnalysis<DominatorSet>();
+  for (BasicBlock::iterator I = L->getHeader()->begin();
+       (PN = dyn_cast<PHINode>(I++)); )
+    if (Value *V = PN->hasConstantValue(true))
+      if (!isa<Instruction>(V) ||
+          DS.dominates(cast<Instruction>(V)->getParent(), L->getHeader())) {
+        PN->replaceAllUsesWith(V);
+        PN->eraseFromParent();
+      }
+
   for (Loop::iterator I = L->begin(), E = L->end(); I != E; ++I)
     Changed |= ProcessLoop(*I);
+
   return Changed;
 }