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 { |
Anton Korobeynikov | bed2946 | 2007-04-16 18:10:23 +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 | } |
Anton Korobeynikov | e2ff29c | 2007-03-10 16:46:28 +0000 | [diff] [blame] | 43 | |
| 44 | struct CaseRange { |
| 45 | Constant* Low; |
| 46 | Constant* High; |
| 47 | BasicBlock* BB; |
| 48 | |
Jeff Cohen | c4558fd | 2007-03-12 17:56:27 +0000 | [diff] [blame] | 49 | CaseRange() : Low(0), High(0), BB(0) { } |
| 50 | CaseRange(Constant* low, Constant* high, BasicBlock* bb) : |
| 51 | Low(low), High(high), BB(bb) { } |
Anton Korobeynikov | e2ff29c | 2007-03-10 16:46:28 +0000 | [diff] [blame] | 52 | }; |
| 53 | |
| 54 | typedef std::vector<CaseRange> CaseVector; |
| 55 | typedef std::vector<CaseRange>::iterator CaseItr; |
Chris Lattner | ebbc1a5 | 2003-10-07 18:46:23 +0000 | [diff] [blame] | 56 | private: |
Chris Lattner | 1438348 | 2003-04-23 16:23:59 +0000 | [diff] [blame] | 57 | void processSwitchInst(SwitchInst *SI); |
Chris Lattner | ebbc1a5 | 2003-10-07 18:46:23 +0000 | [diff] [blame] | 58 | |
| 59 | BasicBlock* switchConvert(CaseItr Begin, CaseItr End, Value* Val, |
| 60 | BasicBlock* OrigBlock, BasicBlock* Default); |
Anton Korobeynikov | e2ff29c | 2007-03-10 16:46:28 +0000 | [diff] [blame] | 61 | BasicBlock* newLeafBlock(CaseRange& Leaf, Value* Val, |
Chris Lattner | ebbc1a5 | 2003-10-07 18:46:23 +0000 | [diff] [blame] | 62 | BasicBlock* OrigBlock, BasicBlock* Default); |
Anton Korobeynikov | e2ff29c | 2007-03-10 16:46:28 +0000 | [diff] [blame] | 63 | unsigned Clusterify(CaseVector& Cases, SwitchInst *SI); |
Chris Lattner | ebbc1a5 | 2003-10-07 18:46:23 +0000 | [diff] [blame] | 64 | }; |
| 65 | |
| 66 | /// The comparison function for sorting the switch case values in the vector. |
Anton Korobeynikov | e2ff29c | 2007-03-10 16:46:28 +0000 | [diff] [blame] | 67 | /// WARNING: Case ranges should be disjoint! |
Chris Lattner | ebbc1a5 | 2003-10-07 18:46:23 +0000 | [diff] [blame] | 68 | struct CaseCmp { |
Anton Korobeynikov | e2ff29c | 2007-03-10 16:46:28 +0000 | [diff] [blame] | 69 | bool operator () (const LowerSwitch::CaseRange& C1, |
| 70 | const LowerSwitch::CaseRange& C2) { |
Chris Lattner | ebbc1a5 | 2003-10-07 18:46:23 +0000 | [diff] [blame] | 71 | |
Anton Korobeynikov | e2ff29c | 2007-03-10 16:46:28 +0000 | [diff] [blame] | 72 | const ConstantInt* CI1 = cast<const ConstantInt>(C1.Low); |
| 73 | const ConstantInt* CI2 = cast<const ConstantInt>(C2.High); |
| 74 | return CI1->getValue().slt(CI2->getValue()); |
Chris Lattner | ebbc1a5 | 2003-10-07 18:46:23 +0000 | [diff] [blame] | 75 | } |
Chris Lattner | 1438348 | 2003-04-23 16:23:59 +0000 | [diff] [blame] | 76 | }; |
| 77 | |
Chris Lattner | 7f8897f | 2006-08-27 22:42:52 +0000 | [diff] [blame] | 78 | RegisterPass<LowerSwitch> |
Chris Lattner | 1438348 | 2003-04-23 16:23:59 +0000 | [diff] [blame] | 79 | X("lowerswitch", "Lower SwitchInst's to branches"); |
| 80 | } |
| 81 | |
Chris Lattner | b3674e4 | 2006-05-02 04:24:36 +0000 | [diff] [blame] | 82 | // Publically exposed interface to pass... |
| 83 | const PassInfo *llvm::LowerSwitchID = X.getPassInfo(); |
Chris Lattner | 1438348 | 2003-04-23 16:23:59 +0000 | [diff] [blame] | 84 | // createLowerSwitchPass - Interface to this file... |
Chris Lattner | d745602 | 2004-01-09 06:02:20 +0000 | [diff] [blame] | 85 | FunctionPass *llvm::createLowerSwitchPass() { |
Chris Lattner | 1438348 | 2003-04-23 16:23:59 +0000 | [diff] [blame] | 86 | return new LowerSwitch(); |
| 87 | } |
| 88 | |
| 89 | bool LowerSwitch::runOnFunction(Function &F) { |
| 90 | bool Changed = false; |
| 91 | |
| 92 | for (Function::iterator I = F.begin(), E = F.end(); I != E; ) { |
| 93 | BasicBlock *Cur = I++; // Advance over block so we don't traverse new blocks |
| 94 | |
| 95 | if (SwitchInst *SI = dyn_cast<SwitchInst>(Cur->getTerminator())) { |
| 96 | Changed = true; |
| 97 | processSwitchInst(SI); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | return Changed; |
| 102 | } |
| 103 | |
Chris Lattner | ebbc1a5 | 2003-10-07 18:46:23 +0000 | [diff] [blame] | 104 | // operator<< - Used for debugging purposes. |
| 105 | // |
Anton Korobeynikov | e2ff29c | 2007-03-10 16:46:28 +0000 | [diff] [blame] | 106 | static std::ostream& operator<<(std::ostream &O, |
| 107 | const LowerSwitch::CaseVector &C) { |
Chris Lattner | ebbc1a5 | 2003-10-07 18:46:23 +0000 | [diff] [blame] | 108 | O << "["; |
| 109 | |
Anton Korobeynikov | e2ff29c | 2007-03-10 16:46:28 +0000 | [diff] [blame] | 110 | for (LowerSwitch::CaseVector::const_iterator B = C.begin(), |
Chris Lattner | d745602 | 2004-01-09 06:02:20 +0000 | [diff] [blame] | 111 | E = C.end(); B != E; ) { |
Anton Korobeynikov | e2ff29c | 2007-03-10 16:46:28 +0000 | [diff] [blame] | 112 | O << *B->Low << " -" << *B->High; |
Chris Lattner | ebbc1a5 | 2003-10-07 18:46:23 +0000 | [diff] [blame] | 113 | if (++B != E) O << ", "; |
| 114 | } |
| 115 | |
| 116 | return O << "]"; |
| 117 | } |
Anton Korobeynikov | e2ff29c | 2007-03-10 16:46:28 +0000 | [diff] [blame] | 118 | |
| 119 | static OStream& operator<<(OStream &O, const LowerSwitch::CaseVector &C) { |
Bill Wendling | 5c7e326 | 2006-12-17 05:15:13 +0000 | [diff] [blame] | 120 | if (O.stream()) *O.stream() << C; |
| 121 | return O; |
| 122 | } |
Chris Lattner | ebbc1a5 | 2003-10-07 18:46:23 +0000 | [diff] [blame] | 123 | |
| 124 | // switchConvert - Convert the switch statement into a binary lookup of |
| 125 | // the case values. The function recursively builds this tree. |
| 126 | // |
| 127 | BasicBlock* LowerSwitch::switchConvert(CaseItr Begin, CaseItr End, |
| 128 | Value* Val, BasicBlock* OrigBlock, |
| 129 | BasicBlock* Default) |
| 130 | { |
| 131 | unsigned Size = End - Begin; |
| 132 | |
| 133 | if (Size == 1) |
| 134 | return newLeafBlock(*Begin, Val, OrigBlock, Default); |
| 135 | |
| 136 | unsigned Mid = Size / 2; |
Anton Korobeynikov | e2ff29c | 2007-03-10 16:46:28 +0000 | [diff] [blame] | 137 | std::vector<CaseRange> LHS(Begin, Begin + Mid); |
Bill Wendling | 0d45a09 | 2006-11-26 10:17:54 +0000 | [diff] [blame] | 138 | DOUT << "LHS: " << LHS << "\n"; |
Anton Korobeynikov | e2ff29c | 2007-03-10 16:46:28 +0000 | [diff] [blame] | 139 | std::vector<CaseRange> RHS(Begin + Mid, End); |
Bill Wendling | 0d45a09 | 2006-11-26 10:17:54 +0000 | [diff] [blame] | 140 | DOUT << "RHS: " << RHS << "\n"; |
Chris Lattner | ebbc1a5 | 2003-10-07 18:46:23 +0000 | [diff] [blame] | 141 | |
Anton Korobeynikov | e2ff29c | 2007-03-10 16:46:28 +0000 | [diff] [blame] | 142 | CaseRange& Pivot = *(Begin + Mid); |
Reid Spencer | e8391e0 | 2007-03-02 23:15:21 +0000 | [diff] [blame] | 143 | DEBUG( DOUT << "Pivot ==> " |
Anton Korobeynikov | e2ff29c | 2007-03-10 16:46:28 +0000 | [diff] [blame] | 144 | << cast<ConstantInt>(Pivot.Low)->getValue().toStringSigned(10) |
| 145 | << " -" |
| 146 | << cast<ConstantInt>(Pivot.High)->getValue().toStringSigned(10) |
Reid Spencer | e8391e0 | 2007-03-02 23:15:21 +0000 | [diff] [blame] | 147 | << "\n"); |
Chris Lattner | ebbc1a5 | 2003-10-07 18:46:23 +0000 | [diff] [blame] | 148 | |
| 149 | BasicBlock* LBranch = switchConvert(LHS.begin(), LHS.end(), Val, |
| 150 | OrigBlock, Default); |
| 151 | BasicBlock* RBranch = switchConvert(RHS.begin(), RHS.end(), Val, |
| 152 | OrigBlock, Default); |
| 153 | |
| 154 | // Create a new node that checks if the value is < pivot. Go to the |
| 155 | // left branch if it is and right branch if not. |
| 156 | Function* F = OrigBlock->getParent(); |
| 157 | BasicBlock* NewNode = new BasicBlock("NodeBlock"); |
Chris Lattner | 261cdfb | 2007-04-17 18:09:47 +0000 | [diff] [blame] | 158 | Function::iterator FI = OrigBlock; |
| 159 | F->getBasicBlockList().insert(++FI, NewNode); |
Chris Lattner | ebbc1a5 | 2003-10-07 18:46:23 +0000 | [diff] [blame] | 160 | |
Anton Korobeynikov | e2ff29c | 2007-03-10 16:46:28 +0000 | [diff] [blame] | 161 | ICmpInst* Comp = new ICmpInst(ICmpInst::ICMP_SLT, Val, Pivot.Low, "Pivot"); |
Chris Lattner | ebbc1a5 | 2003-10-07 18:46:23 +0000 | [diff] [blame] | 162 | NewNode->getInstList().push_back(Comp); |
Chris Lattner | f8485c6 | 2003-11-20 18:25:24 +0000 | [diff] [blame] | 163 | new BranchInst(LBranch, RBranch, Comp, NewNode); |
Chris Lattner | ebbc1a5 | 2003-10-07 18:46:23 +0000 | [diff] [blame] | 164 | return NewNode; |
| 165 | } |
| 166 | |
| 167 | // newLeafBlock - Create a new leaf block for the binary lookup tree. It |
| 168 | // checks if the switch's value == the case's value. If not, then it |
| 169 | // jumps to the default branch. At this point in the tree, the value |
| 170 | // can't be another valid case value, so the jump to the "default" branch |
| 171 | // is warranted. |
| 172 | // |
Anton Korobeynikov | e2ff29c | 2007-03-10 16:46:28 +0000 | [diff] [blame] | 173 | BasicBlock* LowerSwitch::newLeafBlock(CaseRange& Leaf, Value* Val, |
Chris Lattner | ebbc1a5 | 2003-10-07 18:46:23 +0000 | [diff] [blame] | 174 | BasicBlock* OrigBlock, |
| 175 | BasicBlock* Default) |
| 176 | { |
| 177 | Function* F = OrigBlock->getParent(); |
| 178 | BasicBlock* NewLeaf = new BasicBlock("LeafBlock"); |
Chris Lattner | 261cdfb | 2007-04-17 18:09:47 +0000 | [diff] [blame] | 179 | Function::iterator FI = OrigBlock; |
| 180 | F->getBasicBlockList().insert(++FI, NewLeaf); |
Chris Lattner | ebbc1a5 | 2003-10-07 18:46:23 +0000 | [diff] [blame] | 181 | |
Anton Korobeynikov | e2ff29c | 2007-03-10 16:46:28 +0000 | [diff] [blame] | 182 | // Emit comparison |
| 183 | ICmpInst* Comp = NULL; |
| 184 | if (Leaf.Low == Leaf.High) { |
| 185 | // Make the seteq instruction... |
| 186 | Comp = new ICmpInst(ICmpInst::ICMP_EQ, Val, Leaf.Low, |
| 187 | "SwitchLeaf", NewLeaf); |
| 188 | } else { |
| 189 | // Make range comparison |
| 190 | if (cast<ConstantInt>(Leaf.Low)->isMinValue(true /*isSigned*/)) { |
| 191 | // Val >= Min && Val <= Hi --> Val <= Hi |
| 192 | Comp = new ICmpInst(ICmpInst::ICMP_SLE, Val, Leaf.High, |
| 193 | "SwitchLeaf", NewLeaf); |
| 194 | } else if (cast<ConstantInt>(Leaf.Low)->isZero()) { |
| 195 | // Val >= 0 && Val <= Hi --> Val <=u Hi |
| 196 | Comp = new ICmpInst(ICmpInst::ICMP_ULE, Val, Leaf.High, |
| 197 | "SwitchLeaf", NewLeaf); |
| 198 | } else { |
| 199 | // Emit V-Lo <=u Hi-Lo |
| 200 | Constant* NegLo = ConstantExpr::getNeg(Leaf.Low); |
| 201 | Instruction* Add = BinaryOperator::createAdd(Val, NegLo, |
| 202 | Val->getName()+".off", |
| 203 | NewLeaf); |
| 204 | Constant *UpperBound = ConstantExpr::getAdd(NegLo, Leaf.High); |
| 205 | Comp = new ICmpInst(ICmpInst::ICMP_ULE, Add, UpperBound, |
| 206 | "SwitchLeaf", NewLeaf); |
| 207 | } |
| 208 | } |
Chris Lattner | ebbc1a5 | 2003-10-07 18:46:23 +0000 | [diff] [blame] | 209 | |
| 210 | // Make the conditional branch... |
Anton Korobeynikov | e2ff29c | 2007-03-10 16:46:28 +0000 | [diff] [blame] | 211 | BasicBlock* Succ = Leaf.BB; |
Chris Lattner | f8485c6 | 2003-11-20 18:25:24 +0000 | [diff] [blame] | 212 | new BranchInst(Succ, Default, Comp, NewLeaf); |
Chris Lattner | ebbc1a5 | 2003-10-07 18:46:23 +0000 | [diff] [blame] | 213 | |
| 214 | // If there were any PHI nodes in this successor, rewrite one entry |
| 215 | // from OrigBlock to come from NewLeaf. |
Reid Spencer | 2da5c3d | 2004-09-15 17:06:42 +0000 | [diff] [blame] | 216 | for (BasicBlock::iterator I = Succ->begin(); isa<PHINode>(I); ++I) { |
| 217 | PHINode* PN = cast<PHINode>(I); |
Anton Korobeynikov | e2ff29c | 2007-03-10 16:46:28 +0000 | [diff] [blame] | 218 | // Remove all but one incoming entries from the cluster |
| 219 | uint64_t Range = cast<ConstantInt>(Leaf.High)->getSExtValue() - |
| 220 | cast<ConstantInt>(Leaf.Low)->getSExtValue(); |
| 221 | for (uint64_t j = 0; j < Range; ++j) { |
| 222 | PN->removeIncomingValue(OrigBlock); |
| 223 | } |
| 224 | |
Chris Lattner | ebbc1a5 | 2003-10-07 18:46:23 +0000 | [diff] [blame] | 225 | int BlockIdx = PN->getBasicBlockIndex(OrigBlock); |
| 226 | assert(BlockIdx != -1 && "Switch didn't go to this successor??"); |
| 227 | PN->setIncomingBlock((unsigned)BlockIdx, NewLeaf); |
| 228 | } |
| 229 | |
| 230 | return NewLeaf; |
| 231 | } |
| 232 | |
Anton Korobeynikov | e2ff29c | 2007-03-10 16:46:28 +0000 | [diff] [blame] | 233 | // Clusterify - Transform simple list of Cases into list of CaseRange's |
| 234 | unsigned LowerSwitch::Clusterify(CaseVector& Cases, SwitchInst *SI) { |
| 235 | unsigned numCmps = 0; |
| 236 | |
| 237 | // Start with "simple" cases |
| 238 | for (unsigned i = 1; i < SI->getNumSuccessors(); ++i) |
| 239 | Cases.push_back(CaseRange(SI->getSuccessorValue(i), |
| 240 | SI->getSuccessorValue(i), |
| 241 | SI->getSuccessor(i))); |
| 242 | sort(Cases.begin(), Cases.end(), CaseCmp()); |
| 243 | |
| 244 | // Merge case into clusters |
| 245 | if (Cases.size()>=2) |
| 246 | for (CaseItr I=Cases.begin(), J=++(Cases.begin()), E=Cases.end(); J!=E; ) { |
| 247 | int64_t nextValue = cast<ConstantInt>(J->Low)->getSExtValue(); |
| 248 | int64_t currentValue = cast<ConstantInt>(I->High)->getSExtValue(); |
| 249 | BasicBlock* nextBB = J->BB; |
| 250 | BasicBlock* currentBB = I->BB; |
| 251 | |
| 252 | // If the two neighboring cases go to the same destination, merge them |
| 253 | // into a single case. |
| 254 | if ((nextValue-currentValue==1) && (currentBB == nextBB)) { |
| 255 | I->High = J->High; |
| 256 | J = Cases.erase(J); |
| 257 | } else { |
| 258 | I = J++; |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | for (CaseItr I=Cases.begin(), E=Cases.end(); I!=E; ++I, ++numCmps) { |
| 263 | if (I->Low != I->High) |
| 264 | // A range counts double, since it requires two compares. |
| 265 | ++numCmps; |
| 266 | } |
| 267 | |
| 268 | return numCmps; |
| 269 | } |
| 270 | |
Chris Lattner | 1438348 | 2003-04-23 16:23:59 +0000 | [diff] [blame] | 271 | // processSwitchInst - Replace the specified switch instruction with a sequence |
Chris Lattner | ebbc1a5 | 2003-10-07 18:46:23 +0000 | [diff] [blame] | 272 | // of chained if-then insts in a balanced binary search. |
Chris Lattner | 1438348 | 2003-04-23 16:23:59 +0000 | [diff] [blame] | 273 | // |
| 274 | void LowerSwitch::processSwitchInst(SwitchInst *SI) { |
| 275 | BasicBlock *CurBlock = SI->getParent(); |
| 276 | BasicBlock *OrigBlock = CurBlock; |
| 277 | Function *F = CurBlock->getParent(); |
| 278 | Value *Val = SI->getOperand(0); // The value we are switching on... |
Chris Lattner | ebbc1a5 | 2003-10-07 18:46:23 +0000 | [diff] [blame] | 279 | BasicBlock* Default = SI->getDefaultDest(); |
Chris Lattner | 1438348 | 2003-04-23 16:23:59 +0000 | [diff] [blame] | 280 | |
Chris Lattner | 44bb541 | 2003-08-23 22:54:34 +0000 | [diff] [blame] | 281 | // If there is only the default destination, don't bother with the code below. |
| 282 | if (SI->getNumOperands() == 2) { |
Chris Lattner | 108e4ab | 2003-11-21 16:52:05 +0000 | [diff] [blame] | 283 | new BranchInst(SI->getDefaultDest(), CurBlock); |
Chris Lattner | bf9eadd | 2004-03-14 04:14:31 +0000 | [diff] [blame] | 284 | CurBlock->getInstList().erase(SI); |
Chris Lattner | 44bb541 | 2003-08-23 22:54:34 +0000 | [diff] [blame] | 285 | return; |
| 286 | } |
| 287 | |
Chris Lattner | ebbc1a5 | 2003-10-07 18:46:23 +0000 | [diff] [blame] | 288 | // Create a new, empty default block so that the new hierarchy of |
| 289 | // if-then statements go to this and the PHI nodes are happy. |
| 290 | BasicBlock* NewDefault = new BasicBlock("NewDefault"); |
| 291 | F->getBasicBlockList().insert(Default, NewDefault); |
Chris Lattner | 1438348 | 2003-04-23 16:23:59 +0000 | [diff] [blame] | 292 | |
Chris Lattner | 108e4ab | 2003-11-21 16:52:05 +0000 | [diff] [blame] | 293 | new BranchInst(Default, NewDefault); |
Chris Lattner | 1438348 | 2003-04-23 16:23:59 +0000 | [diff] [blame] | 294 | |
Chris Lattner | ebbc1a5 | 2003-10-07 18:46:23 +0000 | [diff] [blame] | 295 | // If there is an entry in any PHI nodes for the default edge, make sure |
| 296 | // to update them as well. |
Reid Spencer | 2da5c3d | 2004-09-15 17:06:42 +0000 | [diff] [blame] | 297 | for (BasicBlock::iterator I = Default->begin(); isa<PHINode>(I); ++I) { |
| 298 | PHINode *PN = cast<PHINode>(I); |
Chris Lattner | ebbc1a5 | 2003-10-07 18:46:23 +0000 | [diff] [blame] | 299 | int BlockIdx = PN->getBasicBlockIndex(OrigBlock); |
| 300 | assert(BlockIdx != -1 && "Switch didn't go to this successor??"); |
| 301 | PN->setIncomingBlock((unsigned)BlockIdx, NewDefault); |
Chris Lattner | 1438348 | 2003-04-23 16:23:59 +0000 | [diff] [blame] | 302 | } |
| 303 | |
Anton Korobeynikov | e2ff29c | 2007-03-10 16:46:28 +0000 | [diff] [blame] | 304 | // Prepare cases vector. |
| 305 | CaseVector Cases; |
| 306 | unsigned numCmps = Clusterify(Cases, SI); |
Chris Lattner | ebbc1a5 | 2003-10-07 18:46:23 +0000 | [diff] [blame] | 307 | |
Anton Korobeynikov | e2ff29c | 2007-03-10 16:46:28 +0000 | [diff] [blame] | 308 | DOUT << "Clusterify finished. Total clusters: " << Cases.size() |
| 309 | << ". Total compares: " << numCmps << "\n"; |
Bill Wendling | 0d45a09 | 2006-11-26 10:17:54 +0000 | [diff] [blame] | 310 | DOUT << "Cases: " << Cases << "\n"; |
Anton Korobeynikov | e2ff29c | 2007-03-10 16:46:28 +0000 | [diff] [blame] | 311 | |
Chris Lattner | ebbc1a5 | 2003-10-07 18:46:23 +0000 | [diff] [blame] | 312 | BasicBlock* SwitchBlock = switchConvert(Cases.begin(), Cases.end(), Val, |
| 313 | OrigBlock, NewDefault); |
| 314 | |
| 315 | // Branch to our shiny new if-then stuff... |
Chris Lattner | 108e4ab | 2003-11-21 16:52:05 +0000 | [diff] [blame] | 316 | new BranchInst(SwitchBlock, OrigBlock); |
Chris Lattner | ebbc1a5 | 2003-10-07 18:46:23 +0000 | [diff] [blame] | 317 | |
Chris Lattner | 1438348 | 2003-04-23 16:23:59 +0000 | [diff] [blame] | 318 | // We are now done with the switch instruction, delete it. |
Chris Lattner | bf9eadd | 2004-03-14 04:14:31 +0000 | [diff] [blame] | 319 | CurBlock->getInstList().erase(SI); |
Chris Lattner | 1438348 | 2003-04-23 16:23:59 +0000 | [diff] [blame] | 320 | } |