#include "llvm/Pass.h"
#include "Support/Debug.h"
#include "Support/Statistic.h"
+using namespace llvm;
namespace {
Statistic<> NumLowered("lowerswitch", "Number of SwitchInst's replaced");
}
// createLowerSwitchPass - Interface to this file...
-FunctionPass *createLowerSwitchPass() {
+FunctionPass *llvm::createLowerSwitchPass() {
return new LowerSwitch();
}
// 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 << ", ";
}
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);
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;
}
// 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.
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;
}
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.
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);
}