Removed #include <iostream> and replaced with llvm_* streams.
[oota-llvm.git] / lib / Transforms / Scalar / LoopUnswitch.cpp
index 9c3655205b576aaabf8481a7d6df977cb1b2e57b..8b2f6cfc5eb8be1a580c3eff67ec18cb90714002 100644 (file)
@@ -40,7 +40,6 @@
 #include "llvm/Support/Debug.h"
 #include "llvm/Support/CommandLine.h"
 #include <algorithm>
-#include <iostream>
 #include <set>
 using namespace llvm;
 
@@ -173,7 +172,8 @@ bool LoopUnswitch::visitLoop(Loop *L) {
         // See if this, or some part of it, is loop invariant.  If so, we can
         // unswitch on it if we desire.
         Value *LoopCond = FindLIVLoopCondition(BI->getCondition(), L, Changed);
-        if (LoopCond && UnswitchIfProfitable(LoopCond, ConstantBool::True, L)) {
+        if (LoopCond && UnswitchIfProfitable(LoopCond, ConstantBool::getTrue(),
+                                             L)) {
           ++NumBranches;
           return true;
         }
@@ -196,7 +196,8 @@ bool LoopUnswitch::visitLoop(Loop *L) {
          BBI != E; ++BBI)
       if (SelectInst *SI = dyn_cast<SelectInst>(BBI)) {
         Value *LoopCond = FindLIVLoopCondition(SI->getCondition(), L, Changed);
-        if (LoopCond && UnswitchIfProfitable(LoopCond, ConstantBool::True, L)) {
+        if (LoopCond && UnswitchIfProfitable(LoopCond, ConstantBool::getTrue(),
+                                             L)) {
           ++NumSelects;
           return true;
         }
@@ -286,9 +287,9 @@ static bool IsTrivialUnswitchCondition(Loop *L, Value *Cond, Constant **Val = 0,
     // side-effects.  If so, determine the value of Cond that causes it to do
     // this.
     if ((LoopExitBB = isTrivialLoopExitBlock(L, BI->getSuccessor(0)))) {
-      if (Val) *Val = ConstantBool::True;
+      if (Val) *Val = ConstantBool::getTrue();
     } else if ((LoopExitBB = isTrivialLoopExitBlock(L, BI->getSuccessor(1)))) {
-      if (Val) *Val = ConstantBool::False;
+      if (Val) *Val = ConstantBool::getFalse();
     }
   } else if (SwitchInst *SI = dyn_cast<SwitchInst>(HeaderTerm)) {
     // If this isn't a switch on Cond, we can't handle it.
@@ -367,9 +368,9 @@ bool LoopUnswitch::UnswitchIfProfitable(Value *LoopCond, Constant *Val,Loop *L){
     // FIXME: this should estimate growth by the amount of code shared by the
     // resultant unswitched loops.
     //
-    DEBUG(std::cerr << "NOT unswitching loop %"
-                    << L->getHeader()->getName() << ", cost too high: "
-                    << L->getBlocks().size() << "\n");
+    DOUT << "NOT unswitching loop %"
+         << L->getHeader()->getName() << ", cost too high: "
+         << L->getBlocks().size() << "\n";
     return false;
   }
   
@@ -488,7 +489,7 @@ static void EmitPreheaderBranchOnCondition(Value *LIC, Constant *Val,
   Value *BranchVal = LIC;
   if (!isa<ConstantBool>(Val)) {
     BranchVal = BinaryOperator::createSetEQ(LIC, Val, "tmp", InsertPt);
-  } else if (Val != ConstantBool::True) {
+  } else if (Val != ConstantBool::getTrue()) {
     // We want to enter the new loop when the condition is true.
     std::swap(TrueDest, FalseDest);
   }
@@ -506,10 +507,10 @@ static void EmitPreheaderBranchOnCondition(Value *LIC, Constant *Val,
 void LoopUnswitch::UnswitchTrivialCondition(Loop *L, Value *Cond, 
                                             Constant *Val, 
                                             BasicBlock *ExitBlock) {
-  DEBUG(std::cerr << "loop-unswitch: Trivial-Unswitch loop %"
-        << L->getHeader()->getName() << " [" << L->getBlocks().size()
-        << " blocks] in Function " << L->getHeader()->getParent()->getName()
-        << " on cond: " << *Val << " == " << *Cond << "\n");
+  DOUT << "loop-unswitch: Trivial-Unswitch loop %"
+       << L->getHeader()->getName() << " [" << L->getBlocks().size()
+       << " blocks] in Function " << L->getHeader()->getParent()->getName()
+       << " on cond: " << *Val << " == " << *Cond << "\n";
   
   // First step, split the preheader, so that we know that there is a safe place
   // to insert the conditional branch.  We will change 'OrigPH' to have a
@@ -551,10 +552,10 @@ void LoopUnswitch::UnswitchTrivialCondition(Loop *L, Value *Cond,
 void LoopUnswitch::UnswitchNontrivialCondition(Value *LIC, Constant *Val, 
                                                Loop *L) {
   Function *F = L->getHeader()->getParent();
-  DEBUG(std::cerr << "loop-unswitch: Unswitching loop %"
-                  << L->getHeader()->getName() << " [" << L->getBlocks().size()
-                  << " blocks] in Function " << F->getName()
-                  << " when '" << *Val << "' == " << *LIC << "\n");
+  DOUT << "loop-unswitch: Unswitching loop %"
+       << L->getHeader()->getName() << " [" << L->getBlocks().size()
+       << " blocks] in Function " << F->getName()
+       << " when '" << *Val << "' == " << *LIC << "\n";
 
   // LoopBlocks contains all of the basic blocks of the loop, including the
   // preheader of the loop, the body of the loop, and the exit blocks of the 
@@ -570,15 +571,10 @@ void LoopUnswitch::UnswitchNontrivialCondition(Value *LIC, Constant *Val,
   LoopBlocks.insert(LoopBlocks.end(), L->block_begin(), L->block_end());
 
   std::vector<BasicBlock*> ExitBlocks;
-  L->getExitBlocks(ExitBlocks);
-  std::sort(ExitBlocks.begin(), ExitBlocks.end());
-  ExitBlocks.erase(std::unique(ExitBlocks.begin(), ExitBlocks.end()),
-                   ExitBlocks.end());
-  
+  L->getUniqueExitBlocks(ExitBlocks);
+
   // Split all of the edges from inside the loop to their exit blocks.  Update
   // the appropriate Phi nodes as we do so.
-  unsigned NumBlocks = L->getBlocks().size();
-  
   for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i) {
     BasicBlock *ExitBlock = ExitBlocks[i];
     std::vector<BasicBlock*> Preds(pred_begin(ExitBlock), pred_end(ExitBlock));
@@ -626,11 +622,8 @@ void LoopUnswitch::UnswitchNontrivialCondition(Value *LIC, Constant *Val,
   
   // The exit blocks may have been changed due to edge splitting, recompute.
   ExitBlocks.clear();
-  L->getExitBlocks(ExitBlocks);
-  std::sort(ExitBlocks.begin(), ExitBlocks.end());
-  ExitBlocks.erase(std::unique(ExitBlocks.begin(), ExitBlocks.end()),
-                   ExitBlocks.end());
-  
+  L->getUniqueExitBlocks(ExitBlocks);
+
   // Add exit blocks to the loop blocks.
   LoopBlocks.insert(LoopBlocks.end(), ExitBlocks.begin(), ExitBlocks.end());
 
@@ -727,7 +720,7 @@ static void RemoveFromWorklist(Instruction *I,
 /// program, replacing all uses with V and update the worklist.
 static void ReplaceUsesOfWith(Instruction *I, Value *V, 
                               std::vector<Instruction*> &Worklist) {
-  DEBUG(std::cerr << "Replace with '" << *V << "': " << *I);
+  DOUT << "Replace with '" << *V << "': " << *I;
 
   // Add uses to the worklist, which may be dead now.
   for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
@@ -786,7 +779,7 @@ void LoopUnswitch::RemoveBlockIfDead(BasicBlock *BB,
     return;
   }
 
-  DEBUG(std::cerr << "Nuking dead block: " << *BB);
+  DOUT << "Nuking dead block: " << *BB;
   
   // Remove the instructions in the basic block from the worklist.
   for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) {
@@ -970,10 +963,8 @@ void LoopUnswitch::RewriteLoopBodyWithConditionConstant(Loop *L, Value *LIC,
               BasicBlock* Split = SplitBlock(Old, SI);
               
               Instruction* OldTerm = Old->getTerminator();
-              BranchInst* Branch = new BranchInst(Split,
-                                        SI->getSuccessor(i),
-                                        ConstantBool::True,
-                                        OldTerm);
+              new BranchInst(Split, SI->getSuccessor(i),
+                             ConstantBool::getTrue(), OldTerm);
               
               Old->getTerminator()->eraseFromParent();
               
@@ -1021,7 +1012,7 @@ void LoopUnswitch::SimplifyCode(std::vector<Instruction*> &Worklist) {
     
     // Simple DCE.
     if (isInstructionTriviallyDead(I)) {
-      DEBUG(std::cerr << "Remove dead instruction '" << *I);
+      DOUT << "Remove dead instruction '" << *I;
       
       // Add uses to the worklist, which may be dead now.
       for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
@@ -1074,8 +1065,8 @@ void LoopUnswitch::SimplifyCode(std::vector<Instruction*> &Worklist) {
         if (!SinglePred) continue;  // Nothing to do.
         assert(SinglePred == Pred && "CFG broken");
 
-        DEBUG(std::cerr << "Merging blocks: " << Pred->getName() << " <- " 
-                        << Succ->getName() << "\n");
+        DOUT << "Merging blocks: " << Pred->getName() << " <- " 
+             << Succ->getName() << "\n";
         
         // Resolve any single entry PHI nodes in Succ.
         while (PHINode *PN = dyn_cast<PHINode>(Succ->begin()))
@@ -1100,7 +1091,7 @@ void LoopUnswitch::SimplifyCode(std::vector<Instruction*> &Worklist) {
         // remove dead blocks.
         break;  // FIXME: Enable.
 
-        DEBUG(std::cerr << "Folded branch: " << *BI);
+        DOUT << "Folded branch: " << *BI;
         BasicBlock *DeadSucc = BI->getSuccessor(CB->getValue());
         BasicBlock *LiveSucc = BI->getSuccessor(!CB->getValue());
         DeadSucc->removePredecessor(BI->getParent(), true);