Chris Lattner | 1438348 | 2003-04-23 16:23:59 +0000 | [diff] [blame] | 1 | //===- LowerSwitch.cpp - Eliminate Switch instructions --------------------===// |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 1438348 | 2003-04-23 16:23:59 +0000 | [diff] [blame] | 9 | // |
| 10 | // The LowerSwitch transformation rewrites switch statements with a sequence of |
| 11 | // branches, which allows targets to get away with not implementing the switch |
| 12 | // statement until it is convenient. |
| 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
| 16 | #include "llvm/Transforms/Scalar.h" |
Chris Lattner | 8d89e7b | 2006-05-09 04:13:41 +0000 | [diff] [blame] | 17 | #include "llvm/Transforms/Utils/UnifyFunctionExitNodes.h" |
Chris Lattner | ebbc1a5 | 2003-10-07 18:46:23 +0000 | [diff] [blame] | 18 | #include "llvm/Constants.h" |
Chris Lattner | 1438348 | 2003-04-23 16:23:59 +0000 | [diff] [blame] | 19 | #include "llvm/Function.h" |
Misha Brukman | d8e1eea | 2004-07-29 17:05:13 +0000 | [diff] [blame] | 20 | #include "llvm/Instructions.h" |
Chris Lattner | 1438348 | 2003-04-23 16:23:59 +0000 | [diff] [blame] | 21 | #include "llvm/Pass.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 22 | #include "llvm/Support/Debug.h" |
Chris Lattner | a4f0b3a | 2006-08-27 12:54:02 +0000 | [diff] [blame] | 23 | #include "llvm/Support/Compiler.h" |
Alkis Evlogimenos | 20aa474 | 2004-09-03 18:19:51 +0000 | [diff] [blame] | 24 | #include <algorithm> |
Chris Lattner | d745602 | 2004-01-09 06:02:20 +0000 | [diff] [blame] | 25 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 26 | |
Chris Lattner | 1438348 | 2003-04-23 16:23:59 +0000 | [diff] [blame] | 27 | namespace { |
Chris Lattner | 1438348 | 2003-04-23 16:23:59 +0000 | [diff] [blame] | 28 | /// LowerSwitch Pass - Replace all SwitchInst instructions with chained branch |
| 29 | /// instructions. Note that this cannot be a BasicBlock pass because it |
| 30 | /// modifies the CFG! |
Chris Lattner | f4b5461 | 2006-06-28 22:08:15 +0000 | [diff] [blame] | 31 | class VISIBILITY_HIDDEN LowerSwitch : public FunctionPass { |
Chris Lattner | ebbc1a5 | 2003-10-07 18:46:23 +0000 | [diff] [blame] | 32 | public: |
Chris Lattner | 8d89e7b | 2006-05-09 04:13:41 +0000 | [diff] [blame] | 33 | virtual bool runOnFunction(Function &F); |
Chris Lattner | ed96fe8 | 2006-05-17 21:05:27 +0000 | [diff] [blame] | 34 | |
Chris Lattner | 8d89e7b | 2006-05-09 04:13:41 +0000 | [diff] [blame] | 35 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
Chris Lattner | ed96fe8 | 2006-05-17 21:05:27 +0000 | [diff] [blame] | 36 | // This is a cluster of orthogonal Transforms |
Chris Lattner | 8d89e7b | 2006-05-09 04:13:41 +0000 | [diff] [blame] | 37 | AU.addPreserved<UnifyFunctionExitNodes>(); |
| 38 | AU.addPreservedID(PromoteMemoryToRegisterID); |
| 39 | AU.addPreservedID(LowerSelectID); |
Chris Lattner | ed96fe8 | 2006-05-17 21:05:27 +0000 | [diff] [blame] | 40 | AU.addPreservedID(LowerInvokePassID); |
| 41 | AU.addPreservedID(LowerAllocationsID); |
Chris Lattner | 8d89e7b | 2006-05-09 04:13:41 +0000 | [diff] [blame] | 42 | } |
Chris Lattner | ed96fe8 | 2006-05-17 21:05:27 +0000 | [diff] [blame] | 43 | |
Chris Lattner | ebbc1a5 | 2003-10-07 18:46:23 +0000 | [diff] [blame] | 44 | typedef std::pair<Constant*, BasicBlock*> Case; |
| 45 | typedef std::vector<Case>::iterator CaseItr; |
| 46 | private: |
Chris Lattner | 1438348 | 2003-04-23 16:23:59 +0000 | [diff] [blame] | 47 | void processSwitchInst(SwitchInst *SI); |
Chris Lattner | ebbc1a5 | 2003-10-07 18:46:23 +0000 | [diff] [blame] | 48 | |
| 49 | BasicBlock* switchConvert(CaseItr Begin, CaseItr End, Value* Val, |
| 50 | BasicBlock* OrigBlock, BasicBlock* Default); |
| 51 | BasicBlock* newLeafBlock(Case& Leaf, Value* Val, |
| 52 | BasicBlock* OrigBlock, BasicBlock* Default); |
| 53 | }; |
| 54 | |
| 55 | /// The comparison function for sorting the switch case values in the vector. |
| 56 | struct CaseCmp { |
| 57 | bool operator () (const LowerSwitch::Case& C1, |
| 58 | const LowerSwitch::Case& C2) { |
Chris Lattner | ebbc1a5 | 2003-10-07 18:46:23 +0000 | [diff] [blame] | 59 | |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 60 | const ConstantInt* CI1 = cast<const ConstantInt>(C1.first); |
| 61 | const ConstantInt* CI2 = cast<const ConstantInt>(C2.first); |
| 62 | if (CI1->getType()->isUnsigned()) |
| 63 | return CI1->getZExtValue() < CI2->getZExtValue(); |
| 64 | return CI1->getSExtValue() < CI2->getSExtValue(); |
Chris Lattner | ebbc1a5 | 2003-10-07 18:46:23 +0000 | [diff] [blame] | 65 | } |
Chris Lattner | 1438348 | 2003-04-23 16:23:59 +0000 | [diff] [blame] | 66 | }; |
| 67 | |
Chris Lattner | 7f8897f | 2006-08-27 22:42:52 +0000 | [diff] [blame] | 68 | RegisterPass<LowerSwitch> |
Chris Lattner | 1438348 | 2003-04-23 16:23:59 +0000 | [diff] [blame] | 69 | X("lowerswitch", "Lower SwitchInst's to branches"); |
| 70 | } |
| 71 | |
Chris Lattner | b3674e4 | 2006-05-02 04:24:36 +0000 | [diff] [blame] | 72 | // Publically exposed interface to pass... |
| 73 | const PassInfo *llvm::LowerSwitchID = X.getPassInfo(); |
Chris Lattner | 1438348 | 2003-04-23 16:23:59 +0000 | [diff] [blame] | 74 | // createLowerSwitchPass - Interface to this file... |
Chris Lattner | d745602 | 2004-01-09 06:02:20 +0000 | [diff] [blame] | 75 | FunctionPass *llvm::createLowerSwitchPass() { |
Chris Lattner | 1438348 | 2003-04-23 16:23:59 +0000 | [diff] [blame] | 76 | return new LowerSwitch(); |
| 77 | } |
| 78 | |
| 79 | bool LowerSwitch::runOnFunction(Function &F) { |
| 80 | bool Changed = false; |
| 81 | |
| 82 | for (Function::iterator I = F.begin(), E = F.end(); I != E; ) { |
| 83 | BasicBlock *Cur = I++; // Advance over block so we don't traverse new blocks |
| 84 | |
| 85 | if (SwitchInst *SI = dyn_cast<SwitchInst>(Cur->getTerminator())) { |
| 86 | Changed = true; |
| 87 | processSwitchInst(SI); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | return Changed; |
| 92 | } |
| 93 | |
Chris Lattner | ebbc1a5 | 2003-10-07 18:46:23 +0000 | [diff] [blame] | 94 | // operator<< - Used for debugging purposes. |
| 95 | // |
Bill Wendling | 5c7e326 | 2006-12-17 05:15:13 +0000 | [diff] [blame] | 96 | std::ostream& operator<<(std::ostream &O, |
| 97 | const std::vector<LowerSwitch::Case> &C) { |
Chris Lattner | ebbc1a5 | 2003-10-07 18:46:23 +0000 | [diff] [blame] | 98 | O << "["; |
| 99 | |
Chris Lattner | d745602 | 2004-01-09 06:02:20 +0000 | [diff] [blame] | 100 | for (std::vector<LowerSwitch::Case>::const_iterator B = C.begin(), |
| 101 | E = C.end(); B != E; ) { |
Chris Lattner | ebbc1a5 | 2003-10-07 18:46:23 +0000 | [diff] [blame] | 102 | O << *B->first; |
| 103 | if (++B != E) O << ", "; |
| 104 | } |
| 105 | |
| 106 | return O << "]"; |
| 107 | } |
Bill Wendling | 5c7e326 | 2006-12-17 05:15:13 +0000 | [diff] [blame] | 108 | OStream& operator<<(OStream &O, const std::vector<LowerSwitch::Case> &C) { |
| 109 | if (O.stream()) *O.stream() << C; |
| 110 | return O; |
| 111 | } |
Chris Lattner | ebbc1a5 | 2003-10-07 18:46:23 +0000 | [diff] [blame] | 112 | |
| 113 | // switchConvert - Convert the switch statement into a binary lookup of |
| 114 | // the case values. The function recursively builds this tree. |
| 115 | // |
| 116 | BasicBlock* LowerSwitch::switchConvert(CaseItr Begin, CaseItr End, |
| 117 | Value* Val, BasicBlock* OrigBlock, |
| 118 | BasicBlock* Default) |
| 119 | { |
| 120 | unsigned Size = End - Begin; |
| 121 | |
| 122 | if (Size == 1) |
| 123 | return newLeafBlock(*Begin, Val, OrigBlock, Default); |
| 124 | |
| 125 | unsigned Mid = Size / 2; |
| 126 | std::vector<Case> LHS(Begin, Begin + Mid); |
Bill Wendling | 0d45a09 | 2006-11-26 10:17:54 +0000 | [diff] [blame] | 127 | DOUT << "LHS: " << LHS << "\n"; |
Chris Lattner | ebbc1a5 | 2003-10-07 18:46:23 +0000 | [diff] [blame] | 128 | std::vector<Case> RHS(Begin + Mid, End); |
Bill Wendling | 0d45a09 | 2006-11-26 10:17:54 +0000 | [diff] [blame] | 129 | DOUT << "RHS: " << RHS << "\n"; |
Chris Lattner | ebbc1a5 | 2003-10-07 18:46:23 +0000 | [diff] [blame] | 130 | |
| 131 | Case& Pivot = *(Begin + Mid); |
Bill Wendling | 0d45a09 | 2006-11-26 10:17:54 +0000 | [diff] [blame] | 132 | DOUT << "Pivot ==> " |
| 133 | << cast<ConstantInt>(Pivot.first)->getSExtValue() << "\n"; |
Chris Lattner | ebbc1a5 | 2003-10-07 18:46:23 +0000 | [diff] [blame] | 134 | |
| 135 | BasicBlock* LBranch = switchConvert(LHS.begin(), LHS.end(), Val, |
| 136 | OrigBlock, Default); |
| 137 | BasicBlock* RBranch = switchConvert(RHS.begin(), RHS.end(), Val, |
| 138 | OrigBlock, Default); |
| 139 | |
| 140 | // Create a new node that checks if the value is < pivot. Go to the |
| 141 | // left branch if it is and right branch if not. |
| 142 | Function* F = OrigBlock->getParent(); |
| 143 | BasicBlock* NewNode = new BasicBlock("NodeBlock"); |
| 144 | F->getBasicBlockList().insert(OrigBlock->getNext(), NewNode); |
| 145 | |
| 146 | SetCondInst* Comp = new SetCondInst(Instruction::SetLT, Val, Pivot.first, |
| 147 | "Pivot"); |
| 148 | NewNode->getInstList().push_back(Comp); |
Chris Lattner | f8485c6 | 2003-11-20 18:25:24 +0000 | [diff] [blame] | 149 | new BranchInst(LBranch, RBranch, Comp, NewNode); |
Chris Lattner | ebbc1a5 | 2003-10-07 18:46:23 +0000 | [diff] [blame] | 150 | return NewNode; |
| 151 | } |
| 152 | |
| 153 | // newLeafBlock - Create a new leaf block for the binary lookup tree. It |
| 154 | // checks if the switch's value == the case's value. If not, then it |
| 155 | // jumps to the default branch. At this point in the tree, the value |
| 156 | // can't be another valid case value, so the jump to the "default" branch |
| 157 | // is warranted. |
| 158 | // |
| 159 | BasicBlock* LowerSwitch::newLeafBlock(Case& Leaf, Value* Val, |
| 160 | BasicBlock* OrigBlock, |
| 161 | BasicBlock* Default) |
| 162 | { |
| 163 | Function* F = OrigBlock->getParent(); |
| 164 | BasicBlock* NewLeaf = new BasicBlock("LeafBlock"); |
| 165 | F->getBasicBlockList().insert(OrigBlock->getNext(), NewLeaf); |
| 166 | |
| 167 | // Make the seteq instruction... |
| 168 | SetCondInst* Comp = new SetCondInst(Instruction::SetEQ, Val, |
| 169 | Leaf.first, "SwitchLeaf"); |
| 170 | NewLeaf->getInstList().push_back(Comp); |
| 171 | |
| 172 | // Make the conditional branch... |
| 173 | BasicBlock* Succ = Leaf.second; |
Chris Lattner | f8485c6 | 2003-11-20 18:25:24 +0000 | [diff] [blame] | 174 | new BranchInst(Succ, Default, Comp, NewLeaf); |
Chris Lattner | ebbc1a5 | 2003-10-07 18:46:23 +0000 | [diff] [blame] | 175 | |
| 176 | // If there were any PHI nodes in this successor, rewrite one entry |
| 177 | // from OrigBlock to come from NewLeaf. |
Reid Spencer | 2da5c3d | 2004-09-15 17:06:42 +0000 | [diff] [blame] | 178 | for (BasicBlock::iterator I = Succ->begin(); isa<PHINode>(I); ++I) { |
| 179 | PHINode* PN = cast<PHINode>(I); |
Chris Lattner | ebbc1a5 | 2003-10-07 18:46:23 +0000 | [diff] [blame] | 180 | int BlockIdx = PN->getBasicBlockIndex(OrigBlock); |
| 181 | assert(BlockIdx != -1 && "Switch didn't go to this successor??"); |
| 182 | PN->setIncomingBlock((unsigned)BlockIdx, NewLeaf); |
| 183 | } |
| 184 | |
| 185 | return NewLeaf; |
| 186 | } |
| 187 | |
Chris Lattner | 1438348 | 2003-04-23 16:23:59 +0000 | [diff] [blame] | 188 | // processSwitchInst - Replace the specified switch instruction with a sequence |
Chris Lattner | ebbc1a5 | 2003-10-07 18:46:23 +0000 | [diff] [blame] | 189 | // of chained if-then insts in a balanced binary search. |
Chris Lattner | 1438348 | 2003-04-23 16:23:59 +0000 | [diff] [blame] | 190 | // |
| 191 | void LowerSwitch::processSwitchInst(SwitchInst *SI) { |
| 192 | BasicBlock *CurBlock = SI->getParent(); |
| 193 | BasicBlock *OrigBlock = CurBlock; |
| 194 | Function *F = CurBlock->getParent(); |
| 195 | Value *Val = SI->getOperand(0); // The value we are switching on... |
Chris Lattner | ebbc1a5 | 2003-10-07 18:46:23 +0000 | [diff] [blame] | 196 | BasicBlock* Default = SI->getDefaultDest(); |
Chris Lattner | 1438348 | 2003-04-23 16:23:59 +0000 | [diff] [blame] | 197 | |
Chris Lattner | 44bb541 | 2003-08-23 22:54:34 +0000 | [diff] [blame] | 198 | // If there is only the default destination, don't bother with the code below. |
| 199 | if (SI->getNumOperands() == 2) { |
Chris Lattner | 108e4ab | 2003-11-21 16:52:05 +0000 | [diff] [blame] | 200 | new BranchInst(SI->getDefaultDest(), CurBlock); |
Chris Lattner | bf9eadd | 2004-03-14 04:14:31 +0000 | [diff] [blame] | 201 | CurBlock->getInstList().erase(SI); |
Chris Lattner | 44bb541 | 2003-08-23 22:54:34 +0000 | [diff] [blame] | 202 | return; |
| 203 | } |
| 204 | |
Chris Lattner | ebbc1a5 | 2003-10-07 18:46:23 +0000 | [diff] [blame] | 205 | // Create a new, empty default block so that the new hierarchy of |
| 206 | // if-then statements go to this and the PHI nodes are happy. |
| 207 | BasicBlock* NewDefault = new BasicBlock("NewDefault"); |
| 208 | F->getBasicBlockList().insert(Default, NewDefault); |
Chris Lattner | 1438348 | 2003-04-23 16:23:59 +0000 | [diff] [blame] | 209 | |
Chris Lattner | 108e4ab | 2003-11-21 16:52:05 +0000 | [diff] [blame] | 210 | new BranchInst(Default, NewDefault); |
Chris Lattner | 1438348 | 2003-04-23 16:23:59 +0000 | [diff] [blame] | 211 | |
Chris Lattner | ebbc1a5 | 2003-10-07 18:46:23 +0000 | [diff] [blame] | 212 | // If there is an entry in any PHI nodes for the default edge, make sure |
| 213 | // to update them as well. |
Reid Spencer | 2da5c3d | 2004-09-15 17:06:42 +0000 | [diff] [blame] | 214 | for (BasicBlock::iterator I = Default->begin(); isa<PHINode>(I); ++I) { |
| 215 | PHINode *PN = cast<PHINode>(I); |
Chris Lattner | ebbc1a5 | 2003-10-07 18:46:23 +0000 | [diff] [blame] | 216 | int BlockIdx = PN->getBasicBlockIndex(OrigBlock); |
| 217 | assert(BlockIdx != -1 && "Switch didn't go to this successor??"); |
| 218 | PN->setIncomingBlock((unsigned)BlockIdx, NewDefault); |
Chris Lattner | 1438348 | 2003-04-23 16:23:59 +0000 | [diff] [blame] | 219 | } |
| 220 | |
Chris Lattner | ebbc1a5 | 2003-10-07 18:46:23 +0000 | [diff] [blame] | 221 | std::vector<Case> Cases; |
| 222 | |
| 223 | // Expand comparisons for all of the non-default cases... |
| 224 | for (unsigned i = 1; i < SI->getNumSuccessors(); ++i) |
| 225 | Cases.push_back(Case(SI->getSuccessorValue(i), SI->getSuccessor(i))); |
| 226 | |
| 227 | std::sort(Cases.begin(), Cases.end(), CaseCmp()); |
Bill Wendling | 0d45a09 | 2006-11-26 10:17:54 +0000 | [diff] [blame] | 228 | DOUT << "Cases: " << Cases << "\n"; |
Chris Lattner | ebbc1a5 | 2003-10-07 18:46:23 +0000 | [diff] [blame] | 229 | BasicBlock* SwitchBlock = switchConvert(Cases.begin(), Cases.end(), Val, |
| 230 | OrigBlock, NewDefault); |
| 231 | |
| 232 | // Branch to our shiny new if-then stuff... |
Chris Lattner | 108e4ab | 2003-11-21 16:52:05 +0000 | [diff] [blame] | 233 | new BranchInst(SwitchBlock, OrigBlock); |
Chris Lattner | ebbc1a5 | 2003-10-07 18:46:23 +0000 | [diff] [blame] | 234 | |
Chris Lattner | 1438348 | 2003-04-23 16:23:59 +0000 | [diff] [blame] | 235 | // We are now done with the switch instruction, delete it. |
Chris Lattner | bf9eadd | 2004-03-14 04:14:31 +0000 | [diff] [blame] | 236 | CurBlock->getInstList().erase(SI); |
Chris Lattner | 1438348 | 2003-04-23 16:23:59 +0000 | [diff] [blame] | 237 | } |