Implement review feedback for the ConstantBool->ConstantInt merge. Chris
[oota-llvm.git] / lib / Transforms / Scalar / CondPropagate.cpp
index 49c87fd02f069eb8b6ad64b90f629febbdf29299..253535e3a5e1c3e7398fe4d489eaaa11a0a0aa24 100644 (file)
 #include "llvm/Type.h"
 #include "llvm/ADT/STLExtras.h"
 #include "llvm/ADT/Statistic.h"
-#include <iostream>
+#include "llvm/Support/Streams.h"
 using namespace llvm;
 
-namespace {
-  Statistic<>
-  NumBrThread("condprop", "Number of CFG edges threaded through branches");
-  Statistic<>
-  NumSwThread("condprop", "Number of CFG edges threaded through switches");
+STATISTIC(NumBrThread, "Number of CFG edges threaded through branches");
+STATISTIC(NumSwThread, "Number of CFG edges threaded through switches");
 
+namespace {
   struct CondProp : public FunctionPass {
     virtual bool runOnFunction(Function &F);
 
@@ -46,7 +44,7 @@ namespace {
     void SimplifyPredecessors(SwitchInst *SI);
     void RevectorBlockTo(BasicBlock *FromBB, BasicBlock *ToBB);
   };
-  RegisterOpt<CondProp> X("condprop", "Conditional Propagation");
+  RegisterPass<CondProp> X("condprop", "Conditional Propagation");
 }
 
 FunctionPass *llvm::createCondPropagationPass() {
@@ -135,12 +133,13 @@ void CondProp::SimplifyPredecessors(BranchInst *BI) {
   // constants.  Walk from the end to remove operands from the end when
   // possible, and to avoid invalidating "i".
   for (unsigned i = PN->getNumIncomingValues(); i != 0; --i)
-    if (ConstantBool *CB = dyn_cast<ConstantBool>(PN->getIncomingValue(i-1))) {
+    if (ConstantInt *CB = dyn_cast<ConstantInt>(PN->getIncomingValue(i-1))) {
+      if (CB->getType() != Type::Int1Ty) continue;
       // If we have a constant, forward the edge from its current to its
       // ultimate destination.
       bool PHIGone = PN->getNumIncomingValues() == 2;
       RevectorBlockTo(PN->getIncomingBlock(i-1),
-                      BI->getSuccessor(CB->getValue() == 0));
+                      BI->getSuccessor(CB->getZExtValue() == 0));
       ++NumBrThread;
 
       // If there were two predecessors before this simplification, the PHI node
@@ -196,11 +195,14 @@ void CondProp::RevectorBlockTo(BasicBlock *FromBB, BasicBlock *ToBB) {
   // Get the old block we are threading through.
   BasicBlock *OldSucc = FromBr->getSuccessor(0);
 
-  // ToBB should not have any PHI nodes in it to update, because OldSucc had
-  // multiple successors.  If OldSucc had multiple successor and ToBB had
-  // multiple predecessors, the edge between them would be critical, which we
-  // already took care of.
-  assert(!isa<PHINode>(ToBB->begin()) && "Critical Edge Found!");
+  // OldSucc had multiple successors. If ToBB has multiple predecessors, then 
+  // the edge between them would be critical, which we already took care of.
+  // If ToBB has single operand PHI node then take care of it here.
+  while (PHINode *PN = dyn_cast<PHINode>(ToBB->begin())) {
+    assert(PN->getNumIncomingValues() == 1 && "Critical Edge Found!");    
+    PN->replaceAllUsesWith(PN->getIncomingValue(0));
+    PN->eraseFromParent();
+  }
 
   // Update PHI nodes in OldSucc to know that FromBB no longer branches to it.
   OldSucc->removePredecessor(FromBB);