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