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 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // 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 | // |
Gordon Henriksen | c86b677 | 2007-11-04 16:15:04 +0000 | [diff] [blame] | 10 | // The LowerSwitch transformation rewrites switch instructions with a sequence |
| 11 | // of branches, which allows targets to get away with not implementing the |
| 12 | // switch instruction until it is convenient. |
Chris Lattner | 1438348 | 2003-04-23 16:23:59 +0000 | [diff] [blame] | 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" |
Owen Anderson | 0a205a4 | 2009-07-05 22:41:43 +0000 | [diff] [blame] | 21 | #include "llvm/LLVMContext.h" |
Chris Lattner | 1438348 | 2003-04-23 16:23:59 +0000 | [diff] [blame] | 22 | #include "llvm/Pass.h" |
Dan Gohman | b61f2f0 | 2007-11-02 22:22:02 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/STLExtras.h" |
Chris Lattner | a4f0b3a | 2006-08-27 12:54:02 +0000 | [diff] [blame] | 24 | #include "llvm/Support/Compiler.h" |
Nick Lewycky | f5a86f4 | 2009-10-25 06:57:41 +0000 | [diff] [blame] | 25 | #include "llvm/Support/Debug.h" |
Chris Lattner | 944fac7 | 2008-08-23 22:23:09 +0000 | [diff] [blame] | 26 | #include "llvm/Support/raw_ostream.h" |
Alkis Evlogimenos | 20aa474 | 2004-09-03 18:19:51 +0000 | [diff] [blame] | 27 | #include <algorithm> |
Chris Lattner | d745602 | 2004-01-09 06:02:20 +0000 | [diff] [blame] | 28 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 29 | |
Chris Lattner | 1438348 | 2003-04-23 16:23:59 +0000 | [diff] [blame] | 30 | namespace { |
Chris Lattner | 1438348 | 2003-04-23 16:23:59 +0000 | [diff] [blame] | 31 | /// LowerSwitch Pass - Replace all SwitchInst instructions with chained branch |
Chris Lattner | 473fc96 | 2010-08-18 02:41:56 +0000 | [diff] [blame] | 32 | /// instructions. |
Nick Lewycky | 6726b6d | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 33 | class LowerSwitch : public FunctionPass { |
Chris Lattner | ebbc1a5 | 2003-10-07 18:46:23 +0000 | [diff] [blame] | 34 | public: |
Nick Lewycky | ecd94c8 | 2007-05-06 13:37:16 +0000 | [diff] [blame] | 35 | static char ID; // Pass identification, replacement for typeid |
Owen Anderson | 081c34b | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 36 | LowerSwitch() : FunctionPass(ID) { |
| 37 | initializeLowerSwitchPass(*PassRegistry::getPassRegistry()); |
| 38 | } |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 39 | |
Chris Lattner | 8d89e7b | 2006-05-09 04:13:41 +0000 | [diff] [blame] | 40 | virtual bool runOnFunction(Function &F); |
Chris Lattner | ed96fe8 | 2006-05-17 21:05:27 +0000 | [diff] [blame] | 41 | |
Chris Lattner | 8d89e7b | 2006-05-09 04:13:41 +0000 | [diff] [blame] | 42 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
Anton Korobeynikov | bed2946 | 2007-04-16 18:10:23 +0000 | [diff] [blame] | 43 | // This is a cluster of orthogonal Transforms |
Chris Lattner | 8d89e7b | 2006-05-09 04:13:41 +0000 | [diff] [blame] | 44 | AU.addPreserved<UnifyFunctionExitNodes>(); |
Dan Gohman | 60493c3 | 2010-08-06 21:48:06 +0000 | [diff] [blame] | 45 | AU.addPreserved("mem2reg"); |
Chris Lattner | ed96fe8 | 2006-05-17 21:05:27 +0000 | [diff] [blame] | 46 | AU.addPreservedID(LowerInvokePassID); |
Chris Lattner | 8d89e7b | 2006-05-09 04:13:41 +0000 | [diff] [blame] | 47 | } |
Anton Korobeynikov | e2ff29c | 2007-03-10 16:46:28 +0000 | [diff] [blame] | 48 | |
| 49 | struct CaseRange { |
| 50 | Constant* Low; |
| 51 | Constant* High; |
| 52 | BasicBlock* BB; |
| 53 | |
Chris Lattner | 473fc96 | 2010-08-18 02:41:56 +0000 | [diff] [blame] | 54 | CaseRange(Constant *low = 0, Constant *high = 0, BasicBlock *bb = 0) : |
Jeff Cohen | c4558fd | 2007-03-12 17:56:27 +0000 | [diff] [blame] | 55 | Low(low), High(high), BB(bb) { } |
Anton Korobeynikov | e2ff29c | 2007-03-10 16:46:28 +0000 | [diff] [blame] | 56 | }; |
| 57 | |
| 58 | typedef std::vector<CaseRange> CaseVector; |
| 59 | typedef std::vector<CaseRange>::iterator CaseItr; |
Chris Lattner | ebbc1a5 | 2003-10-07 18:46:23 +0000 | [diff] [blame] | 60 | private: |
Chris Lattner | 1438348 | 2003-04-23 16:23:59 +0000 | [diff] [blame] | 61 | void processSwitchInst(SwitchInst *SI); |
Chris Lattner | ebbc1a5 | 2003-10-07 18:46:23 +0000 | [diff] [blame] | 62 | |
| 63 | BasicBlock* switchConvert(CaseItr Begin, CaseItr End, Value* Val, |
| 64 | BasicBlock* OrigBlock, BasicBlock* Default); |
Anton Korobeynikov | e2ff29c | 2007-03-10 16:46:28 +0000 | [diff] [blame] | 65 | BasicBlock* newLeafBlock(CaseRange& Leaf, Value* Val, |
Chris Lattner | ebbc1a5 | 2003-10-07 18:46:23 +0000 | [diff] [blame] | 66 | BasicBlock* OrigBlock, BasicBlock* Default); |
Anton Korobeynikov | e2ff29c | 2007-03-10 16:46:28 +0000 | [diff] [blame] | 67 | unsigned Clusterify(CaseVector& Cases, SwitchInst *SI); |
Chris Lattner | ebbc1a5 | 2003-10-07 18:46:23 +0000 | [diff] [blame] | 68 | }; |
Chris Lattner | 1438348 | 2003-04-23 16:23:59 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 71 | char LowerSwitch::ID = 0; |
Owen Anderson | 02dd53e | 2010-08-23 17:52:01 +0000 | [diff] [blame] | 72 | INITIALIZE_PASS(LowerSwitch, "lowerswitch", |
Owen Anderson | ce665bd | 2010-10-07 22:25:06 +0000 | [diff] [blame] | 73 | "Lower SwitchInst's to branches", false, false) |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 74 | |
Chris Lattner | 7a2bdde | 2011-04-15 05:18:47 +0000 | [diff] [blame] | 75 | // Publicly exposed interface to pass... |
Owen Anderson | 90c579d | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 76 | char &llvm::LowerSwitchID = LowerSwitch::ID; |
Chris Lattner | 1438348 | 2003-04-23 16:23:59 +0000 | [diff] [blame] | 77 | // createLowerSwitchPass - Interface to this file... |
Chris Lattner | d745602 | 2004-01-09 06:02:20 +0000 | [diff] [blame] | 78 | FunctionPass *llvm::createLowerSwitchPass() { |
Chris Lattner | 1438348 | 2003-04-23 16:23:59 +0000 | [diff] [blame] | 79 | return new LowerSwitch(); |
| 80 | } |
| 81 | |
| 82 | bool LowerSwitch::runOnFunction(Function &F) { |
| 83 | bool Changed = false; |
| 84 | |
| 85 | for (Function::iterator I = F.begin(), E = F.end(); I != E; ) { |
| 86 | BasicBlock *Cur = I++; // Advance over block so we don't traverse new blocks |
| 87 | |
| 88 | if (SwitchInst *SI = dyn_cast<SwitchInst>(Cur->getTerminator())) { |
| 89 | Changed = true; |
| 90 | processSwitchInst(SI); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | return Changed; |
| 95 | } |
| 96 | |
Chris Lattner | ebbc1a5 | 2003-10-07 18:46:23 +0000 | [diff] [blame] | 97 | // operator<< - Used for debugging purposes. |
| 98 | // |
Daniel Dunbar | 1cd1d98 | 2009-07-24 10:36:58 +0000 | [diff] [blame] | 99 | static raw_ostream& operator<<(raw_ostream &O, |
Chandler Carruth | 100c267 | 2010-10-23 08:10:43 +0000 | [diff] [blame] | 100 | const LowerSwitch::CaseVector &C) |
| 101 | LLVM_ATTRIBUTE_USED; |
Mike Stump | 66ad89c | 2009-07-27 23:33:34 +0000 | [diff] [blame] | 102 | static raw_ostream& operator<<(raw_ostream &O, |
Daniel Dunbar | 1cd1d98 | 2009-07-24 10:36:58 +0000 | [diff] [blame] | 103 | const LowerSwitch::CaseVector &C) { |
Chris Lattner | ebbc1a5 | 2003-10-07 18:46:23 +0000 | [diff] [blame] | 104 | O << "["; |
| 105 | |
Anton Korobeynikov | e2ff29c | 2007-03-10 16:46:28 +0000 | [diff] [blame] | 106 | for (LowerSwitch::CaseVector::const_iterator B = C.begin(), |
Chris Lattner | d745602 | 2004-01-09 06:02:20 +0000 | [diff] [blame] | 107 | E = C.end(); B != E; ) { |
Anton Korobeynikov | e2ff29c | 2007-03-10 16:46:28 +0000 | [diff] [blame] | 108 | O << *B->Low << " -" << *B->High; |
Chris Lattner | ebbc1a5 | 2003-10-07 18:46:23 +0000 | [diff] [blame] | 109 | if (++B != E) O << ", "; |
| 110 | } |
| 111 | |
| 112 | return O << "]"; |
| 113 | } |
Anton Korobeynikov | e2ff29c | 2007-03-10 16:46:28 +0000 | [diff] [blame] | 114 | |
Chris Lattner | ebbc1a5 | 2003-10-07 18:46:23 +0000 | [diff] [blame] | 115 | // switchConvert - Convert the switch statement into a binary lookup of |
| 116 | // the case values. The function recursively builds this tree. |
| 117 | // |
| 118 | BasicBlock* LowerSwitch::switchConvert(CaseItr Begin, CaseItr End, |
| 119 | Value* Val, BasicBlock* OrigBlock, |
| 120 | BasicBlock* Default) |
| 121 | { |
| 122 | unsigned Size = End - Begin; |
| 123 | |
| 124 | if (Size == 1) |
| 125 | return newLeafBlock(*Begin, Val, OrigBlock, Default); |
| 126 | |
| 127 | unsigned Mid = Size / 2; |
Anton Korobeynikov | e2ff29c | 2007-03-10 16:46:28 +0000 | [diff] [blame] | 128 | std::vector<CaseRange> LHS(Begin, Begin + Mid); |
David Greene | 0f4c988 | 2010-01-05 01:26:45 +0000 | [diff] [blame] | 129 | DEBUG(dbgs() << "LHS: " << LHS << "\n"); |
Anton Korobeynikov | e2ff29c | 2007-03-10 16:46:28 +0000 | [diff] [blame] | 130 | std::vector<CaseRange> RHS(Begin + Mid, End); |
David Greene | 0f4c988 | 2010-01-05 01:26:45 +0000 | [diff] [blame] | 131 | DEBUG(dbgs() << "RHS: " << RHS << "\n"); |
Chris Lattner | ebbc1a5 | 2003-10-07 18:46:23 +0000 | [diff] [blame] | 132 | |
Anton Korobeynikov | e2ff29c | 2007-03-10 16:46:28 +0000 | [diff] [blame] | 133 | CaseRange& Pivot = *(Begin + Mid); |
David Greene | 0f4c988 | 2010-01-05 01:26:45 +0000 | [diff] [blame] | 134 | DEBUG(dbgs() << "Pivot ==> " |
Chris Lattner | 944fac7 | 2008-08-23 22:23:09 +0000 | [diff] [blame] | 135 | << cast<ConstantInt>(Pivot.Low)->getValue() << " -" |
Dan Gohman | f871ccb | 2009-03-23 15:57:19 +0000 | [diff] [blame] | 136 | << cast<ConstantInt>(Pivot.High)->getValue() << "\n"); |
Chris Lattner | ebbc1a5 | 2003-10-07 18:46:23 +0000 | [diff] [blame] | 137 | |
| 138 | BasicBlock* LBranch = switchConvert(LHS.begin(), LHS.end(), Val, |
| 139 | OrigBlock, Default); |
| 140 | BasicBlock* RBranch = switchConvert(RHS.begin(), RHS.end(), Val, |
| 141 | OrigBlock, Default); |
| 142 | |
| 143 | // Create a new node that checks if the value is < pivot. Go to the |
| 144 | // left branch if it is and right branch if not. |
| 145 | Function* F = OrigBlock->getParent(); |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 146 | BasicBlock* NewNode = BasicBlock::Create(Val->getContext(), "NodeBlock"); |
Chris Lattner | 261cdfb | 2007-04-17 18:09:47 +0000 | [diff] [blame] | 147 | Function::iterator FI = OrigBlock; |
| 148 | F->getBasicBlockList().insert(++FI, NewNode); |
Chris Lattner | ebbc1a5 | 2003-10-07 18:46:23 +0000 | [diff] [blame] | 149 | |
Stepan Dyatkovskiy | e98a889 | 2012-05-24 09:33:20 +0000 | [diff] [blame] | 150 | ICmpInst* Comp = new ICmpInst(ICmpInst::ICMP_ULT, |
Owen Anderson | 333c400 | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 151 | Val, Pivot.Low, "Pivot"); |
Chris Lattner | ebbc1a5 | 2003-10-07 18:46:23 +0000 | [diff] [blame] | 152 | NewNode->getInstList().push_back(Comp); |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 153 | BranchInst::Create(LBranch, RBranch, Comp, NewNode); |
Chris Lattner | ebbc1a5 | 2003-10-07 18:46:23 +0000 | [diff] [blame] | 154 | return NewNode; |
| 155 | } |
| 156 | |
| 157 | // newLeafBlock - Create a new leaf block for the binary lookup tree. It |
| 158 | // checks if the switch's value == the case's value. If not, then it |
| 159 | // jumps to the default branch. At this point in the tree, the value |
| 160 | // can't be another valid case value, so the jump to the "default" branch |
| 161 | // is warranted. |
| 162 | // |
Anton Korobeynikov | e2ff29c | 2007-03-10 16:46:28 +0000 | [diff] [blame] | 163 | BasicBlock* LowerSwitch::newLeafBlock(CaseRange& Leaf, Value* Val, |
Chris Lattner | ebbc1a5 | 2003-10-07 18:46:23 +0000 | [diff] [blame] | 164 | BasicBlock* OrigBlock, |
| 165 | BasicBlock* Default) |
| 166 | { |
| 167 | Function* F = OrigBlock->getParent(); |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 168 | BasicBlock* NewLeaf = BasicBlock::Create(Val->getContext(), "LeafBlock"); |
Chris Lattner | 261cdfb | 2007-04-17 18:09:47 +0000 | [diff] [blame] | 169 | Function::iterator FI = OrigBlock; |
| 170 | F->getBasicBlockList().insert(++FI, NewLeaf); |
Chris Lattner | ebbc1a5 | 2003-10-07 18:46:23 +0000 | [diff] [blame] | 171 | |
Anton Korobeynikov | e2ff29c | 2007-03-10 16:46:28 +0000 | [diff] [blame] | 172 | // Emit comparison |
| 173 | ICmpInst* Comp = NULL; |
| 174 | if (Leaf.Low == Leaf.High) { |
| 175 | // Make the seteq instruction... |
Owen Anderson | 333c400 | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 176 | Comp = new ICmpInst(*NewLeaf, ICmpInst::ICMP_EQ, Val, |
| 177 | Leaf.Low, "SwitchLeaf"); |
Anton Korobeynikov | e2ff29c | 2007-03-10 16:46:28 +0000 | [diff] [blame] | 178 | } else { |
| 179 | // Make range comparison |
| 180 | if (cast<ConstantInt>(Leaf.Low)->isMinValue(true /*isSigned*/)) { |
| 181 | // Val >= Min && Val <= Hi --> Val <= Hi |
Owen Anderson | 333c400 | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 182 | Comp = new ICmpInst(*NewLeaf, ICmpInst::ICMP_SLE, Val, Leaf.High, |
| 183 | "SwitchLeaf"); |
Anton Korobeynikov | e2ff29c | 2007-03-10 16:46:28 +0000 | [diff] [blame] | 184 | } else if (cast<ConstantInt>(Leaf.Low)->isZero()) { |
| 185 | // Val >= 0 && Val <= Hi --> Val <=u Hi |
Owen Anderson | 333c400 | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 186 | Comp = new ICmpInst(*NewLeaf, ICmpInst::ICMP_ULE, Val, Leaf.High, |
| 187 | "SwitchLeaf"); |
Anton Korobeynikov | e2ff29c | 2007-03-10 16:46:28 +0000 | [diff] [blame] | 188 | } else { |
| 189 | // Emit V-Lo <=u Hi-Lo |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 190 | Constant* NegLo = ConstantExpr::getNeg(Leaf.Low); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 191 | Instruction* Add = BinaryOperator::CreateAdd(Val, NegLo, |
Anton Korobeynikov | e2ff29c | 2007-03-10 16:46:28 +0000 | [diff] [blame] | 192 | Val->getName()+".off", |
| 193 | NewLeaf); |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 194 | Constant *UpperBound = ConstantExpr::getAdd(NegLo, Leaf.High); |
Owen Anderson | 333c400 | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 195 | Comp = new ICmpInst(*NewLeaf, ICmpInst::ICMP_ULE, Add, UpperBound, |
| 196 | "SwitchLeaf"); |
Anton Korobeynikov | e2ff29c | 2007-03-10 16:46:28 +0000 | [diff] [blame] | 197 | } |
| 198 | } |
Chris Lattner | ebbc1a5 | 2003-10-07 18:46:23 +0000 | [diff] [blame] | 199 | |
| 200 | // Make the conditional branch... |
Anton Korobeynikov | e2ff29c | 2007-03-10 16:46:28 +0000 | [diff] [blame] | 201 | BasicBlock* Succ = Leaf.BB; |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 202 | BranchInst::Create(Succ, Default, Comp, NewLeaf); |
Chris Lattner | ebbc1a5 | 2003-10-07 18:46:23 +0000 | [diff] [blame] | 203 | |
| 204 | // If there were any PHI nodes in this successor, rewrite one entry |
| 205 | // from OrigBlock to come from NewLeaf. |
Reid Spencer | 2da5c3d | 2004-09-15 17:06:42 +0000 | [diff] [blame] | 206 | for (BasicBlock::iterator I = Succ->begin(); isa<PHINode>(I); ++I) { |
| 207 | PHINode* PN = cast<PHINode>(I); |
Anton Korobeynikov | e2ff29c | 2007-03-10 16:46:28 +0000 | [diff] [blame] | 208 | // Remove all but one incoming entries from the cluster |
| 209 | uint64_t Range = cast<ConstantInt>(Leaf.High)->getSExtValue() - |
| 210 | cast<ConstantInt>(Leaf.Low)->getSExtValue(); |
| 211 | for (uint64_t j = 0; j < Range; ++j) { |
| 212 | PN->removeIncomingValue(OrigBlock); |
| 213 | } |
| 214 | |
Chris Lattner | ebbc1a5 | 2003-10-07 18:46:23 +0000 | [diff] [blame] | 215 | int BlockIdx = PN->getBasicBlockIndex(OrigBlock); |
| 216 | assert(BlockIdx != -1 && "Switch didn't go to this successor??"); |
| 217 | PN->setIncomingBlock((unsigned)BlockIdx, NewLeaf); |
| 218 | } |
| 219 | |
| 220 | return NewLeaf; |
| 221 | } |
| 222 | |
Anton Korobeynikov | e2ff29c | 2007-03-10 16:46:28 +0000 | [diff] [blame] | 223 | // Clusterify - Transform simple list of Cases into list of CaseRange's |
| 224 | unsigned LowerSwitch::Clusterify(CaseVector& Cases, SwitchInst *SI) { |
Stepan Dyatkovskiy | e98a889 | 2012-05-24 09:33:20 +0000 | [diff] [blame] | 225 | |
Stepan Dyatkovskiy | 0aa32d5 | 2012-05-29 12:26:47 +0000 | [diff] [blame] | 226 | IntegersSubsetToBB TheClusterifier; |
Anton Korobeynikov | e2ff29c | 2007-03-10 16:46:28 +0000 | [diff] [blame] | 227 | |
| 228 | // Start with "simple" cases |
Stepan Dyatkovskiy | e98a889 | 2012-05-24 09:33:20 +0000 | [diff] [blame] | 229 | for (SwitchInst::CaseIt i = SI->case_begin(), e = SI->case_end(); |
| 230 | i != e; ++i) { |
| 231 | BasicBlock *SuccBB = i.getCaseSuccessor(); |
Stepan Dyatkovskiy | 0aa32d5 | 2012-05-29 12:26:47 +0000 | [diff] [blame] | 232 | IntegersSubset CaseRanges = i.getCaseValueEx(); |
| 233 | TheClusterifier.add(CaseRanges, SuccBB); |
Stepan Dyatkovskiy | e98a889 | 2012-05-24 09:33:20 +0000 | [diff] [blame] | 234 | } |
Stepan Dyatkovskiy | c10fa6c | 2012-03-08 07:06:20 +0000 | [diff] [blame] | 235 | |
Stepan Dyatkovskiy | e98a889 | 2012-05-24 09:33:20 +0000 | [diff] [blame] | 236 | TheClusterifier.optimize(); |
| 237 | |
| 238 | size_t numCmps = 0; |
Stepan Dyatkovskiy | 0aa32d5 | 2012-05-29 12:26:47 +0000 | [diff] [blame] | 239 | for (IntegersSubsetToBB::RangeIterator i = TheClusterifier.begin(), |
Stepan Dyatkovskiy | e98a889 | 2012-05-24 09:33:20 +0000 | [diff] [blame] | 240 | e = TheClusterifier.end(); i != e; ++i, ++numCmps) { |
Stepan Dyatkovskiy | 6a59073 | 2012-07-03 13:46:45 +0000 | [diff] [blame] | 241 | const IntegersSubsetToBB::RangeTy &R = i->first; |
| 242 | BasicBlock *S = i->second; |
Stepan Dyatkovskiy | 484fc93 | 2012-05-28 12:39:09 +0000 | [diff] [blame] | 243 | |
| 244 | // FIXME: Currently work with ConstantInt based numbers. |
| 245 | // Changing it to APInt based is a pretty heavy for this commit. |
Stepan Dyatkovskiy | 6a59073 | 2012-07-03 13:46:45 +0000 | [diff] [blame] | 246 | Cases.push_back(CaseRange(R.getLow().toConstantInt(), |
| 247 | R.getHigh().toConstantInt(), S)); |
| 248 | if (R.isSingleNumber()) |
Anton Korobeynikov | e2ff29c | 2007-03-10 16:46:28 +0000 | [diff] [blame] | 249 | // A range counts double, since it requires two compares. |
| 250 | ++numCmps; |
| 251 | } |
| 252 | |
Stepan Dyatkovskiy | e98a889 | 2012-05-24 09:33:20 +0000 | [diff] [blame] | 253 | return numCmps; |
Anton Korobeynikov | e2ff29c | 2007-03-10 16:46:28 +0000 | [diff] [blame] | 254 | } |
| 255 | |
Chris Lattner | 1438348 | 2003-04-23 16:23:59 +0000 | [diff] [blame] | 256 | // processSwitchInst - Replace the specified switch instruction with a sequence |
Chris Lattner | ebbc1a5 | 2003-10-07 18:46:23 +0000 | [diff] [blame] | 257 | // of chained if-then insts in a balanced binary search. |
Chris Lattner | 1438348 | 2003-04-23 16:23:59 +0000 | [diff] [blame] | 258 | // |
| 259 | void LowerSwitch::processSwitchInst(SwitchInst *SI) { |
| 260 | BasicBlock *CurBlock = SI->getParent(); |
| 261 | BasicBlock *OrigBlock = CurBlock; |
| 262 | Function *F = CurBlock->getParent(); |
Eli Friedman | bb5a744 | 2011-09-29 20:21:17 +0000 | [diff] [blame] | 263 | Value *Val = SI->getCondition(); // The value we are switching on... |
Chris Lattner | ebbc1a5 | 2003-10-07 18:46:23 +0000 | [diff] [blame] | 264 | BasicBlock* Default = SI->getDefaultDest(); |
Chris Lattner | 1438348 | 2003-04-23 16:23:59 +0000 | [diff] [blame] | 265 | |
Chris Lattner | 44bb541 | 2003-08-23 22:54:34 +0000 | [diff] [blame] | 266 | // If there is only the default destination, don't bother with the code below. |
Stepan Dyatkovskiy | 2447312 | 2012-02-01 07:49:51 +0000 | [diff] [blame] | 267 | if (!SI->getNumCases()) { |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 268 | BranchInst::Create(SI->getDefaultDest(), CurBlock); |
Chris Lattner | bf9eadd | 2004-03-14 04:14:31 +0000 | [diff] [blame] | 269 | CurBlock->getInstList().erase(SI); |
Chris Lattner | 44bb541 | 2003-08-23 22:54:34 +0000 | [diff] [blame] | 270 | return; |
| 271 | } |
| 272 | |
Chris Lattner | ebbc1a5 | 2003-10-07 18:46:23 +0000 | [diff] [blame] | 273 | // Create a new, empty default block so that the new hierarchy of |
| 274 | // if-then statements go to this and the PHI nodes are happy. |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 275 | BasicBlock* NewDefault = BasicBlock::Create(SI->getContext(), "NewDefault"); |
Chris Lattner | ebbc1a5 | 2003-10-07 18:46:23 +0000 | [diff] [blame] | 276 | F->getBasicBlockList().insert(Default, NewDefault); |
Chris Lattner | 1438348 | 2003-04-23 16:23:59 +0000 | [diff] [blame] | 277 | |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 278 | BranchInst::Create(Default, NewDefault); |
Chris Lattner | 1438348 | 2003-04-23 16:23:59 +0000 | [diff] [blame] | 279 | |
Chris Lattner | ebbc1a5 | 2003-10-07 18:46:23 +0000 | [diff] [blame] | 280 | // If there is an entry in any PHI nodes for the default edge, make sure |
| 281 | // to update them as well. |
Reid Spencer | 2da5c3d | 2004-09-15 17:06:42 +0000 | [diff] [blame] | 282 | for (BasicBlock::iterator I = Default->begin(); isa<PHINode>(I); ++I) { |
| 283 | PHINode *PN = cast<PHINode>(I); |
Chris Lattner | ebbc1a5 | 2003-10-07 18:46:23 +0000 | [diff] [blame] | 284 | int BlockIdx = PN->getBasicBlockIndex(OrigBlock); |
| 285 | assert(BlockIdx != -1 && "Switch didn't go to this successor??"); |
| 286 | PN->setIncomingBlock((unsigned)BlockIdx, NewDefault); |
Chris Lattner | 1438348 | 2003-04-23 16:23:59 +0000 | [diff] [blame] | 287 | } |
| 288 | |
Anton Korobeynikov | e2ff29c | 2007-03-10 16:46:28 +0000 | [diff] [blame] | 289 | // Prepare cases vector. |
| 290 | CaseVector Cases; |
| 291 | unsigned numCmps = Clusterify(Cases, SI); |
Chris Lattner | ebbc1a5 | 2003-10-07 18:46:23 +0000 | [diff] [blame] | 292 | |
David Greene | 0f4c988 | 2010-01-05 01:26:45 +0000 | [diff] [blame] | 293 | DEBUG(dbgs() << "Clusterify finished. Total clusters: " << Cases.size() |
Dan Gohman | 6c1980b | 2009-07-25 01:13:51 +0000 | [diff] [blame] | 294 | << ". Total compares: " << numCmps << "\n"); |
David Greene | 0f4c988 | 2010-01-05 01:26:45 +0000 | [diff] [blame] | 295 | DEBUG(dbgs() << "Cases: " << Cases << "\n"); |
Mike Stump | 02efa78 | 2009-07-27 23:14:11 +0000 | [diff] [blame] | 296 | (void)numCmps; |
Anton Korobeynikov | e2ff29c | 2007-03-10 16:46:28 +0000 | [diff] [blame] | 297 | |
Chris Lattner | ebbc1a5 | 2003-10-07 18:46:23 +0000 | [diff] [blame] | 298 | BasicBlock* SwitchBlock = switchConvert(Cases.begin(), Cases.end(), Val, |
| 299 | OrigBlock, NewDefault); |
| 300 | |
| 301 | // Branch to our shiny new if-then stuff... |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 302 | BranchInst::Create(SwitchBlock, OrigBlock); |
Chris Lattner | ebbc1a5 | 2003-10-07 18:46:23 +0000 | [diff] [blame] | 303 | |
Chris Lattner | 1438348 | 2003-04-23 16:23:59 +0000 | [diff] [blame] | 304 | // We are now done with the switch instruction, delete it. |
Chris Lattner | bf9eadd | 2004-03-14 04:14:31 +0000 | [diff] [blame] | 305 | CurBlock->getInstList().erase(SI); |
Chris Lattner | 1438348 | 2003-04-23 16:23:59 +0000 | [diff] [blame] | 306 | } |