llvm.org GIT mirror llvm / 1438348
New pass to lower switch instructions to branch instructions git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@5865 91177308-0d34-0410-b5e6-96231b3b80d8 Chris Lattner 17 years ago
1 changed file(s) with 102 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
0 //===- LowerSwitch.cpp - Eliminate Switch instructions --------------------===//
1 //
2 // The LowerSwitch transformation rewrites switch statements with a sequence of
3 // branches, which allows targets to get away with not implementing the switch
4 // statement until it is convenient.
5 //
6 //===----------------------------------------------------------------------===//
7
8 #include "llvm/Transforms/Scalar.h"
9 #include "llvm/Function.h"
10 #include "llvm/iTerminators.h"
11 #include "llvm/iOperators.h"
12 #include "llvm/iPHINode.h"
13 #include "llvm/Pass.h"
14 #include "Support/Statistic.h"
15
16 namespace {
17 Statistic<> NumLowered("lowerswitch", "Number of SwitchInst's replaced");
18
19 /// LowerSwitch Pass - Replace all SwitchInst instructions with chained branch
20 /// instructions. Note that this cannot be a BasicBlock pass because it
21 /// modifies the CFG!
22 struct LowerSwitch : public FunctionPass {
23 bool runOnFunction(Function &F);
24 void processSwitchInst(SwitchInst *SI);
25 };
26
27 RegisterOpt
28 X("lowerswitch", "Lower SwitchInst's to branches");
29 }
30
31 // createLowerSwitchPass - Interface to this file...
32 Pass *createLowerSwitchPass() {
33 return new LowerSwitch();
34 }
35
36 bool LowerSwitch::runOnFunction(Function &F) {
37 bool Changed = false;
38
39 for (Function::iterator I = F.begin(), E = F.end(); I != E; ) {
40 BasicBlock *Cur = I++; // Advance over block so we don't traverse new blocks
41
42 if (SwitchInst *SI = dyn_cast(Cur->getTerminator())) {
43 Changed = true;
44 processSwitchInst(SI);
45 }
46 }
47
48 return Changed;
49 }
50
51 // processSwitchInst - Replace the specified switch instruction with a sequence
52 // of chained basic blocks. Right now we just insert an incredibly stupid
53 // linear sequence of branches. It would be better to do a balanced binary
54 // search eventually. FIXME
55 //
56 void LowerSwitch::processSwitchInst(SwitchInst *SI) {
57 BasicBlock *CurBlock = SI->getParent();
58 BasicBlock *OrigBlock = CurBlock;
59 Function *F = CurBlock->getParent();
60 Value *Val = SI->getOperand(0); // The value we are switching on...
61
62 // Unlink the switch instruction from it's block.
63 CurBlock->getInstList().remove(SI);
64
65 // Expand comparisons for all of the non-default cases...
66 for (unsigned i = 2, e = SI->getNumOperands(); i != e; i += 2) {
67 // Insert a new basic block after the current one...
68 BasicBlock *NextBlock;
69 if (i != e-2) {
70 NextBlock = new BasicBlock("switchblock");
71 F->getBasicBlockList().insert(CurBlock->getNext(), NextBlock);
72 } else { // Last case, if it's not the value, go to default block.
73 NextBlock = cast(SI->getDefaultDest());
74 }
75
76 // Make the seteq instruction...
77 Instruction *Comp = new SetCondInst(Instruction::SetEQ, Val,
78 SI->getOperand(i), "switchcase");
79 CurBlock->getInstList().push_back(Comp);
80
81 // Make the conditional branch...
82 BasicBlock *Succ = cast(SI->getOperand(i+1));
83 Instruction *Br = new BranchInst(Succ, NextBlock, Comp);
84 CurBlock->getInstList().push_back(Br);
85
86 // If there were any PHI nodes in this success, rewrite one entry from
87 // OrigBlock to come from CurBlock.
88 for (BasicBlock::iterator I = Succ->begin();
89 PHINode *PN = dyn_cast(I); ++I) {
90 int BlockIdx = PN->getBasicBlockIndex(OrigBlock);
91 assert(BlockIdx != -1 && "Switch didn't go to this successor??");
92 PN->setIncomingBlock((unsigned)BlockIdx, CurBlock);
93 }
94
95 CurBlock = NextBlock; // Move on to the next condition
96 }
97
98
99 // We are now done with the switch instruction, delete it.
100 delete SI;
101 }