Clarify the logic: the flag is renamed to `deleteFn' to signify it will delete
[oota-llvm.git] / lib / Transforms / Utils / LowerSwitch.cpp
index a45192bb0cf52d67aaaca340a0d46646749df7ad..653d9ffa47ce998dc9afd71247ccda6ec21626e9 100644 (file)
@@ -22,6 +22,7 @@
 #include "llvm/Pass.h"
 #include "Support/Debug.h"
 #include "Support/Statistic.h"
+using namespace llvm;
 
 namespace {
   Statistic<> NumLowered("lowerswitch", "Number of SwitchInst's replaced");
@@ -60,7 +61,7 @@ namespace {
 }
 
 // createLowerSwitchPass - Interface to this file...
-FunctionPass *createLowerSwitchPass() {
+FunctionPass *llvm::createLowerSwitchPass() {
   return new LowerSwitch();
 }
 
@@ -81,12 +82,12 @@ bool LowerSwitch::runOnFunction(Function &F) {
 
 // operator<< - Used for debugging purposes.
 //
-std::ostream& operator << (std::ostream& O, std::vector<LowerSwitch::Case>& C)
-{
+std::ostream& operator<<(std::ostream &O,
+                         const std::vector<LowerSwitch::Case> &C) {
   O << "[";
 
-  for (std::vector<LowerSwitch::Case>::iterator B = C.begin(), E = C.end();
-      B != E; ) {
+  for (std::vector<LowerSwitch::Case>::const_iterator B = C.begin(),
+         E = C.end(); B != E; ) {
     O << *B->first;
     if (++B != E) O << ", ";
   }
@@ -114,7 +115,8 @@ BasicBlock* LowerSwitch::switchConvert(CaseItr Begin, CaseItr End,
 
   Case& Pivot = *(Begin + Mid);
   DEBUG(std::cerr << "Pivot ==> "
-                  << cast<ConstantUInt>(Pivot.first)->getValue() << "\n");
+                  << (int64_t)cast<ConstantInt>(Pivot.first)->getRawValue()
+                  << "\n");
 
   BasicBlock* LBranch = switchConvert(LHS.begin(), LHS.end(), Val,
                                       OrigBlock, Default);
@@ -130,8 +132,7 @@ BasicBlock* LowerSwitch::switchConvert(CaseItr Begin, CaseItr End,
   SetCondInst* Comp = new SetCondInst(Instruction::SetLT, Val, Pivot.first,
                                       "Pivot");
   NewNode->getInstList().push_back(Comp);
-  BranchInst* Br = new BranchInst(LBranch, RBranch, Comp);
-  NewNode->getInstList().push_back(Br);
+  new BranchInst(LBranch, RBranch, Comp, NewNode);
   return NewNode;
 }
 
@@ -156,8 +157,7 @@ BasicBlock* LowerSwitch::newLeafBlock(Case& Leaf, Value* Val,
 
   // Make the conditional branch...
   BasicBlock* Succ = Leaf.second;
-  Instruction* Br = new BranchInst(Succ, Default, Comp);
-  NewLeaf->getInstList().push_back(Br);
+  new BranchInst(Succ, Default, Comp, NewLeaf);
 
   // If there were any PHI nodes in this successor, rewrite one entry
   // from OrigBlock to come from NewLeaf.
@@ -181,13 +181,10 @@ void LowerSwitch::processSwitchInst(SwitchInst *SI) {
   Value *Val = SI->getOperand(0);  // The value we are switching on...
   BasicBlock* Default = SI->getDefaultDest();
 
-  // Unlink the switch instruction from it's block.
-  CurBlock->getInstList().remove(SI);
-
   // If there is only the default destination, don't bother with the code below.
   if (SI->getNumOperands() == 2) {
-    CurBlock->getInstList().push_back(new BranchInst(SI->getDefaultDest()));
-    delete SI;
+    new BranchInst(SI->getDefaultDest(), CurBlock);
+    CurBlock->getInstList().erase(SI);
     return;
   }
 
@@ -196,7 +193,7 @@ void LowerSwitch::processSwitchInst(SwitchInst *SI) {
   BasicBlock* NewDefault = new BasicBlock("NewDefault");
   F->getBasicBlockList().insert(Default, NewDefault);
 
-  NewDefault->getInstList().push_back(new BranchInst(Default));
+  new BranchInst(Default, NewDefault);
 
   // If there is an entry in any PHI nodes for the default edge, make sure
   // to update them as well.
@@ -219,8 +216,8 @@ void LowerSwitch::processSwitchInst(SwitchInst *SI) {
                                           OrigBlock, NewDefault);
 
   // Branch to our shiny new if-then stuff...
-  OrigBlock->getInstList().push_back(new BranchInst(SwitchBlock));
+  new BranchInst(SwitchBlock, OrigBlock);
 
   // We are now done with the switch instruction, delete it.
-  delete SI;
+  CurBlock->getInstList().erase(SI);
 }