19#include "llvm/IR/IRBuilder.h"
20#include "llvm/IR/LLVMContext.h"
34 const ConstantInt* CI1 = cast<const ConstantInt>(C1.
value);
35 const ConstantInt* CI2 = cast<const ConstantInt>(C2.
value);
36 return CI1->getValue().slt(CI2->getValue());
43 for (Function::iterator I = F.begin(), E = F.end(); I != E; ) {
44 BasicBlock *cur = &*I;
47 if (SwitchInst *SI = dyn_cast<SwitchInst>(cur->getTerminator())) {
59 Value* value, BasicBlock* origBlock,
60 BasicBlock* defaultBlock)
62 BasicBlock *curHead = defaultBlock;
63 Function *F = origBlock->getParent();
64 llvm::IRBuilder<> Builder(defaultBlock);
67 for (
CaseItr it = begin; it < end; ++it) {
68 BasicBlock *newBlock = BasicBlock::Create(F->getContext(),
"NodeBlock");
69 Function::iterator FI = origBlock->getIterator();
70 F->getBasicBlockList().insert(++FI, newBlock);
71 Builder.SetInsertPoint(newBlock);
72 auto cmpValue = Builder.CreateICmpEQ(value, it->value,
"case.cmp");
73 Builder.CreateCondBr(cmpValue, it->block, curHead);
77 for (BasicBlock::iterator bi = it->block->begin(); isa<PHINode>(bi); ++bi) {
78 PHINode* PN = cast<PHINode>(bi);
80 int blockIndex = PN->getBasicBlockIndex(origBlock);
81 assert(blockIndex != -1 &&
"Switch didn't go to this successor??");
82 PN->setIncomingBlock((
unsigned)blockIndex, newBlock);
89 Builder.SetInsertPoint(origBlock);
90 Builder.CreateBr(curHead);
97 BasicBlock *origBlock = SI->getParent();
98 BasicBlock *defaultBlock = SI->getDefaultDest();
99 Function *F = origBlock->getParent();
100 Value *switchValue = SI->getCondition();
104 BasicBlock* newDefault = BasicBlock::Create(F->getContext(),
"newDefault");
105 llvm::IRBuilder<> Builder(newDefault);
107 F->getBasicBlockList().insert(defaultBlock->getIterator(), newDefault);
108 Builder.CreateBr(defaultBlock);
112 for (BasicBlock::iterator I = defaultBlock->begin(); isa<PHINode>(I); ++I) {
113 PHINode *PN = cast<PHINode>(I);
114 int BlockIdx = PN->getBasicBlockIndex(origBlock);
115 assert(BlockIdx != -1 &&
"Switch didn't go to this successor??");
116 PN->setIncomingBlock((
unsigned)BlockIdx, newDefault);
121 for (
auto i : SI->cases())
123 i.getCaseSuccessor()));
129 std::reverse(cases.begin(), cases.end());
131 switchConvert(cases.begin(), cases.end(), switchValue, origBlock, newDefault);
134 origBlock->getInstList().erase(SI);
void processSwitchInst(llvm::SwitchInst *SI)
std::vector< SwitchCase > CaseVector
bool runOnFunction(llvm::Function &F) override
void switchConvert(CaseItr begin, CaseItr end, llvm::Value *value, llvm::BasicBlock *origBlock, llvm::BasicBlock *defaultBlock)
std::vector< SwitchCase >::iterator CaseItr
bool operator()(const LowerSwitchPass::SwitchCase &C1, const LowerSwitchPass::SwitchCase &C2)