Chris Lattner | 1b094a0 | 2003-04-23 16:23:59 +0000 | [diff] [blame] | 1 | //===- LowerSwitch.cpp - Eliminate Switch instructions --------------------===// |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | f3ebc3f | 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 | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 1b094a0 | 2003-04-23 16:23:59 +0000 | [diff] [blame] | 9 | // |
Gordon Henriksen | d568767 | 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 | 1b094a0 | 2003-04-23 16:23:59 +0000 | [diff] [blame] | 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
| 16 | #include "llvm/Transforms/Scalar.h" |
Jim Grosbach | fff5663 | 2014-06-16 16:55:20 +0000 | [diff] [blame] | 17 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/STLExtras.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 19 | #include "llvm/IR/Constants.h" |
| 20 | #include "llvm/IR/Function.h" |
| 21 | #include "llvm/IR/Instructions.h" |
| 22 | #include "llvm/IR/LLVMContext.h" |
Jim Grosbach | fff5663 | 2014-06-16 16:55:20 +0000 | [diff] [blame] | 23 | #include "llvm/IR/CFG.h" |
Chris Lattner | 1b094a0 | 2003-04-23 16:23:59 +0000 | [diff] [blame] | 24 | #include "llvm/Pass.h" |
Chris Lattner | 3d27be1 | 2006-08-27 12:54:02 +0000 | [diff] [blame] | 25 | #include "llvm/Support/Compiler.h" |
Nick Lewycky | 974e12b | 2009-10-25 06:57:41 +0000 | [diff] [blame] | 26 | #include "llvm/Support/Debug.h" |
Chris Lattner | 0c19df4 | 2008-08-23 22:23:09 +0000 | [diff] [blame] | 27 | #include "llvm/Support/raw_ostream.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 28 | #include "llvm/Transforms/Utils/UnifyFunctionExitNodes.h" |
Alkis Evlogimenos | a5c04ee | 2004-09-03 18:19:51 +0000 | [diff] [blame] | 29 | #include <algorithm> |
Chris Lattner | 49525f8 | 2004-01-09 06:02:20 +0000 | [diff] [blame] | 30 | using namespace llvm; |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 31 | |
Chandler Carruth | e96dd89 | 2014-04-21 22:55:11 +0000 | [diff] [blame] | 32 | #define DEBUG_TYPE "lower-switch" |
| 33 | |
Chris Lattner | 1b094a0 | 2003-04-23 16:23:59 +0000 | [diff] [blame] | 34 | namespace { |
Chris Lattner | 1b094a0 | 2003-04-23 16:23:59 +0000 | [diff] [blame] | 35 | /// LowerSwitch Pass - Replace all SwitchInst instructions with chained branch |
Chris Lattner | b45de95 | 2010-08-18 02:41:56 +0000 | [diff] [blame] | 36 | /// instructions. |
Nick Lewycky | 02d5f77 | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 37 | class LowerSwitch : public FunctionPass { |
Chris Lattner | ed92216 | 2003-10-07 18:46:23 +0000 | [diff] [blame] | 38 | public: |
Nick Lewycky | e7da2d6 | 2007-05-06 13:37:16 +0000 | [diff] [blame] | 39 | static char ID; // Pass identification, replacement for typeid |
Owen Anderson | 6c18d1a | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 40 | LowerSwitch() : FunctionPass(ID) { |
| 41 | initializeLowerSwitchPass(*PassRegistry::getPassRegistry()); |
| 42 | } |
Devang Patel | 09f162c | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 43 | |
Craig Topper | 3e4c697 | 2014-03-05 09:10:37 +0000 | [diff] [blame] | 44 | bool runOnFunction(Function &F) override; |
| 45 | |
| 46 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Anton Korobeynikov | fb80151 | 2007-04-16 18:10:23 +0000 | [diff] [blame] | 47 | // This is a cluster of orthogonal Transforms |
Chris Lattner | 4fe87d6 | 2006-05-09 04:13:41 +0000 | [diff] [blame] | 48 | AU.addPreserved<UnifyFunctionExitNodes>(); |
Dan Gohman | 0f7892b | 2010-08-06 21:48:06 +0000 | [diff] [blame] | 49 | AU.addPreserved("mem2reg"); |
Chris Lattner | e4cb476 | 2006-05-17 21:05:27 +0000 | [diff] [blame] | 50 | AU.addPreservedID(LowerInvokePassID); |
Chris Lattner | 4fe87d6 | 2006-05-09 04:13:41 +0000 | [diff] [blame] | 51 | } |
Anton Korobeynikov | 8a6dc10 | 2007-03-10 16:46:28 +0000 | [diff] [blame] | 52 | |
| 53 | struct CaseRange { |
| 54 | Constant* Low; |
| 55 | Constant* High; |
| 56 | BasicBlock* BB; |
| 57 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 58 | CaseRange(Constant *low = nullptr, Constant *high = nullptr, |
| 59 | BasicBlock *bb = nullptr) : |
Jeff Cohen | 0022741 | 2007-03-12 17:56:27 +0000 | [diff] [blame] | 60 | Low(low), High(high), BB(bb) { } |
Anton Korobeynikov | 8a6dc10 | 2007-03-10 16:46:28 +0000 | [diff] [blame] | 61 | }; |
| 62 | |
Jim Grosbach | fff5663 | 2014-06-16 16:55:20 +0000 | [diff] [blame] | 63 | typedef std::vector<CaseRange> CaseVector; |
Anton Korobeynikov | 8a6dc10 | 2007-03-10 16:46:28 +0000 | [diff] [blame] | 64 | typedef std::vector<CaseRange>::iterator CaseItr; |
Chris Lattner | ed92216 | 2003-10-07 18:46:23 +0000 | [diff] [blame] | 65 | private: |
Chris Lattner | 1b094a0 | 2003-04-23 16:23:59 +0000 | [diff] [blame] | 66 | void processSwitchInst(SwitchInst *SI); |
Chris Lattner | ed92216 | 2003-10-07 18:46:23 +0000 | [diff] [blame] | 67 | |
Jim Grosbach | fff5663 | 2014-06-16 16:55:20 +0000 | [diff] [blame] | 68 | BasicBlock *switchConvert(CaseItr Begin, CaseItr End, |
| 69 | ConstantInt *LowerBound, ConstantInt *UpperBound, |
Marcello Maggioni | 78035b1 | 2014-07-11 10:34:36 +0000 | [diff] [blame] | 70 | Value *Val, BasicBlock *Predecessor, |
| 71 | BasicBlock *OrigBlock, BasicBlock *Default); |
Jim Grosbach | fff5663 | 2014-06-16 16:55:20 +0000 | [diff] [blame] | 72 | BasicBlock *newLeafBlock(CaseRange &Leaf, Value *Val, BasicBlock *OrigBlock, |
| 73 | BasicBlock *Default); |
| 74 | unsigned Clusterify(CaseVector &Cases, SwitchInst *SI); |
Chris Lattner | ed92216 | 2003-10-07 18:46:23 +0000 | [diff] [blame] | 75 | }; |
Bob Wilson | e407736 | 2013-09-09 19:14:35 +0000 | [diff] [blame] | 76 | |
| 77 | /// The comparison function for sorting the switch case values in the vector. |
| 78 | /// WARNING: Case ranges should be disjoint! |
| 79 | struct CaseCmp { |
| 80 | bool operator () (const LowerSwitch::CaseRange& C1, |
| 81 | const LowerSwitch::CaseRange& C2) { |
| 82 | |
| 83 | const ConstantInt* CI1 = cast<const ConstantInt>(C1.Low); |
| 84 | const ConstantInt* CI2 = cast<const ConstantInt>(C2.High); |
| 85 | return CI1->getValue().slt(CI2->getValue()); |
| 86 | } |
| 87 | }; |
Chris Lattner | 1b094a0 | 2003-04-23 16:23:59 +0000 | [diff] [blame] | 88 | } |
| 89 | |
Dan Gohman | d78c400 | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 90 | char LowerSwitch::ID = 0; |
Owen Anderson | d31d82d | 2010-08-23 17:52:01 +0000 | [diff] [blame] | 91 | INITIALIZE_PASS(LowerSwitch, "lowerswitch", |
Owen Anderson | df7a4f2 | 2010-10-07 22:25:06 +0000 | [diff] [blame] | 92 | "Lower SwitchInst's to branches", false, false) |
Dan Gohman | d78c400 | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 93 | |
Chris Lattner | 0ab5e2c | 2011-04-15 05:18:47 +0000 | [diff] [blame] | 94 | // Publicly exposed interface to pass... |
Owen Anderson | a7aed18 | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 95 | char &llvm::LowerSwitchID = LowerSwitch::ID; |
Chris Lattner | 1b094a0 | 2003-04-23 16:23:59 +0000 | [diff] [blame] | 96 | // createLowerSwitchPass - Interface to this file... |
Chris Lattner | 49525f8 | 2004-01-09 06:02:20 +0000 | [diff] [blame] | 97 | FunctionPass *llvm::createLowerSwitchPass() { |
Chris Lattner | 1b094a0 | 2003-04-23 16:23:59 +0000 | [diff] [blame] | 98 | return new LowerSwitch(); |
| 99 | } |
| 100 | |
| 101 | bool LowerSwitch::runOnFunction(Function &F) { |
| 102 | bool Changed = false; |
| 103 | |
| 104 | for (Function::iterator I = F.begin(), E = F.end(); I != E; ) { |
| 105 | BasicBlock *Cur = I++; // Advance over block so we don't traverse new blocks |
| 106 | |
| 107 | if (SwitchInst *SI = dyn_cast<SwitchInst>(Cur->getTerminator())) { |
| 108 | Changed = true; |
| 109 | processSwitchInst(SI); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | return Changed; |
| 114 | } |
| 115 | |
Chris Lattner | ed92216 | 2003-10-07 18:46:23 +0000 | [diff] [blame] | 116 | // operator<< - Used for debugging purposes. |
| 117 | // |
Daniel Dunbar | 796e43e | 2009-07-24 10:36:58 +0000 | [diff] [blame] | 118 | static raw_ostream& operator<<(raw_ostream &O, |
Chandler Carruth | 88c54b8 | 2010-10-23 08:10:43 +0000 | [diff] [blame] | 119 | const LowerSwitch::CaseVector &C) |
| 120 | LLVM_ATTRIBUTE_USED; |
Mike Stump | 4798763 | 2009-07-27 23:33:34 +0000 | [diff] [blame] | 121 | static raw_ostream& operator<<(raw_ostream &O, |
Daniel Dunbar | 796e43e | 2009-07-24 10:36:58 +0000 | [diff] [blame] | 122 | const LowerSwitch::CaseVector &C) { |
Chris Lattner | ed92216 | 2003-10-07 18:46:23 +0000 | [diff] [blame] | 123 | O << "["; |
| 124 | |
Anton Korobeynikov | 8a6dc10 | 2007-03-10 16:46:28 +0000 | [diff] [blame] | 125 | for (LowerSwitch::CaseVector::const_iterator B = C.begin(), |
Chris Lattner | 49525f8 | 2004-01-09 06:02:20 +0000 | [diff] [blame] | 126 | E = C.end(); B != E; ) { |
Anton Korobeynikov | 8a6dc10 | 2007-03-10 16:46:28 +0000 | [diff] [blame] | 127 | O << *B->Low << " -" << *B->High; |
Chris Lattner | ed92216 | 2003-10-07 18:46:23 +0000 | [diff] [blame] | 128 | if (++B != E) O << ", "; |
| 129 | } |
| 130 | |
| 131 | return O << "]"; |
| 132 | } |
Anton Korobeynikov | 8a6dc10 | 2007-03-10 16:46:28 +0000 | [diff] [blame] | 133 | |
Bruno Cardoso Lopes | bc7ba2c | 2014-11-28 19:47:33 +0000 | [diff] [blame^] | 134 | // \brief Update the first occurrence of the "switch statement" BB in the PHI |
| 135 | // node with the "new" BB. The other occurrences will: |
| 136 | // |
| 137 | // 1) Be updated by subsequent calls to this function. Switch statements may |
| 138 | // have more than one outcoming edge into the same BB if they all have the same |
| 139 | // value. When the switch statement is converted these incoming edges are now |
| 140 | // coming from multiple BBs. |
| 141 | // 2) Removed if subsequent incoming values now share the same case, i.e., |
| 142 | // multiple outcome edges are condensed into one. This is necessary to keep the |
| 143 | // number of phi values equal to the number of branches to SuccBB. |
| 144 | static void fixPhis(BasicBlock *SuccBB, BasicBlock *OrigBB, BasicBlock *NewBB, |
| 145 | unsigned NumMergedCases) { |
| 146 | for (BasicBlock::iterator I = SuccBB->begin(), IE = SuccBB->getFirstNonPHI(); |
| 147 | I != IE; ++I) { |
Marcello Maggioni | 78035b1 | 2014-07-11 10:34:36 +0000 | [diff] [blame] | 148 | PHINode *PN = cast<PHINode>(I); |
| 149 | |
Juergen Ributzka | d441725 | 2014-11-10 21:05:27 +0000 | [diff] [blame] | 150 | // Only update the first occurence. |
Bruno Cardoso Lopes | bc7ba2c | 2014-11-28 19:47:33 +0000 | [diff] [blame^] | 151 | unsigned Idx = 0, E = PN->getNumIncomingValues(); |
| 152 | for (; Idx != E; ++Idx) { |
Juergen Ributzka | d441725 | 2014-11-10 21:05:27 +0000 | [diff] [blame] | 153 | if (PN->getIncomingBlock(Idx) == OrigBB) { |
| 154 | PN->setIncomingBlock(Idx, NewBB); |
| 155 | break; |
| 156 | } |
Marcello Maggioni | 78035b1 | 2014-07-11 10:34:36 +0000 | [diff] [blame] | 157 | } |
Bruno Cardoso Lopes | bc7ba2c | 2014-11-28 19:47:33 +0000 | [diff] [blame^] | 158 | |
| 159 | // Remove additional occurences coming from condensed cases and keep the |
| 160 | // number of incoming values equal to the number of branches to SuccBB. |
| 161 | for (++Idx; NumMergedCases > 0 && Idx != E; ++Idx) |
| 162 | if (PN->getIncomingBlock(Idx) == OrigBB) { |
| 163 | PN->removeIncomingValue(Idx); |
| 164 | NumMergedCases--; |
| 165 | } |
Marcello Maggioni | 78035b1 | 2014-07-11 10:34:36 +0000 | [diff] [blame] | 166 | } |
| 167 | } |
| 168 | |
Chris Lattner | ed92216 | 2003-10-07 18:46:23 +0000 | [diff] [blame] | 169 | // switchConvert - Convert the switch statement into a binary lookup of |
| 170 | // the case values. The function recursively builds this tree. |
Jim Grosbach | fff5663 | 2014-06-16 16:55:20 +0000 | [diff] [blame] | 171 | // LowerBound and UpperBound are used to keep track of the bounds for Val |
| 172 | // that have already been checked by a block emitted by one of the previous |
| 173 | // calls to switchConvert in the call stack. |
| 174 | BasicBlock *LowerSwitch::switchConvert(CaseItr Begin, CaseItr End, |
| 175 | ConstantInt *LowerBound, |
| 176 | ConstantInt *UpperBound, Value *Val, |
Marcello Maggioni | 78035b1 | 2014-07-11 10:34:36 +0000 | [diff] [blame] | 177 | BasicBlock *Predecessor, |
Jim Grosbach | fff5663 | 2014-06-16 16:55:20 +0000 | [diff] [blame] | 178 | BasicBlock *OrigBlock, |
| 179 | BasicBlock *Default) { |
Chris Lattner | ed92216 | 2003-10-07 18:46:23 +0000 | [diff] [blame] | 180 | unsigned Size = End - Begin; |
| 181 | |
Jim Grosbach | fff5663 | 2014-06-16 16:55:20 +0000 | [diff] [blame] | 182 | if (Size == 1) { |
| 183 | // Check if the Case Range is perfectly squeezed in between |
| 184 | // already checked Upper and Lower bounds. If it is then we can avoid |
| 185 | // emitting the code that checks if the value actually falls in the range |
| 186 | // because the bounds already tell us so. |
| 187 | if (Begin->Low == LowerBound && Begin->High == UpperBound) { |
Bruno Cardoso Lopes | bc7ba2c | 2014-11-28 19:47:33 +0000 | [diff] [blame^] | 188 | unsigned NumMergedCases = 0; |
| 189 | if (LowerBound && UpperBound) |
| 190 | NumMergedCases = |
| 191 | UpperBound->getSExtValue() - LowerBound->getSExtValue(); |
| 192 | fixPhis(Begin->BB, OrigBlock, Predecessor, NumMergedCases); |
Jim Grosbach | fff5663 | 2014-06-16 16:55:20 +0000 | [diff] [blame] | 193 | return Begin->BB; |
| 194 | } |
Chris Lattner | ed92216 | 2003-10-07 18:46:23 +0000 | [diff] [blame] | 195 | return newLeafBlock(*Begin, Val, OrigBlock, Default); |
Jim Grosbach | fff5663 | 2014-06-16 16:55:20 +0000 | [diff] [blame] | 196 | } |
Chris Lattner | ed92216 | 2003-10-07 18:46:23 +0000 | [diff] [blame] | 197 | |
| 198 | unsigned Mid = Size / 2; |
Anton Korobeynikov | 8a6dc10 | 2007-03-10 16:46:28 +0000 | [diff] [blame] | 199 | std::vector<CaseRange> LHS(Begin, Begin + Mid); |
David Greene | 50c5423 | 2010-01-05 01:26:45 +0000 | [diff] [blame] | 200 | DEBUG(dbgs() << "LHS: " << LHS << "\n"); |
Anton Korobeynikov | 8a6dc10 | 2007-03-10 16:46:28 +0000 | [diff] [blame] | 201 | std::vector<CaseRange> RHS(Begin + Mid, End); |
David Greene | 50c5423 | 2010-01-05 01:26:45 +0000 | [diff] [blame] | 202 | DEBUG(dbgs() << "RHS: " << RHS << "\n"); |
Chris Lattner | ed92216 | 2003-10-07 18:46:23 +0000 | [diff] [blame] | 203 | |
Jim Grosbach | fff5663 | 2014-06-16 16:55:20 +0000 | [diff] [blame] | 204 | CaseRange &Pivot = *(Begin + Mid); |
| 205 | DEBUG(dbgs() << "Pivot ==> " |
| 206 | << cast<ConstantInt>(Pivot.Low)->getValue() |
| 207 | << " -" << cast<ConstantInt>(Pivot.High)->getValue() << "\n"); |
Chris Lattner | ed92216 | 2003-10-07 18:46:23 +0000 | [diff] [blame] | 208 | |
Jim Grosbach | fff5663 | 2014-06-16 16:55:20 +0000 | [diff] [blame] | 209 | // NewLowerBound here should never be the integer minimal value. |
| 210 | // This is because it is computed from a case range that is never |
| 211 | // the smallest, so there is always a case range that has at least |
| 212 | // a smaller value. |
| 213 | ConstantInt *NewLowerBound = cast<ConstantInt>(Pivot.Low); |
| 214 | ConstantInt *NewUpperBound; |
| 215 | |
| 216 | // If we don't have a Default block then it means that we can never |
| 217 | // have a value outside of a case range, so set the UpperBound to the highest |
| 218 | // value in the LHS part of the case ranges. |
| 219 | if (Default != nullptr) { |
| 220 | // Because NewLowerBound is never the smallest representable integer |
| 221 | // it is safe here to subtract one. |
| 222 | NewUpperBound = ConstantInt::get(NewLowerBound->getContext(), |
| 223 | NewLowerBound->getValue() - 1); |
| 224 | } else { |
| 225 | CaseItr LastLHS = LHS.begin() + LHS.size() - 1; |
| 226 | NewUpperBound = cast<ConstantInt>(LastLHS->High); |
| 227 | } |
| 228 | |
| 229 | DEBUG(dbgs() << "LHS Bounds ==> "; |
| 230 | if (LowerBound) { |
| 231 | dbgs() << cast<ConstantInt>(LowerBound)->getSExtValue(); |
| 232 | } else { |
| 233 | dbgs() << "NONE"; |
| 234 | } |
| 235 | dbgs() << " - " << NewUpperBound->getSExtValue() << "\n"; |
| 236 | dbgs() << "RHS Bounds ==> "; |
| 237 | dbgs() << NewLowerBound->getSExtValue() << " - "; |
| 238 | if (UpperBound) { |
| 239 | dbgs() << cast<ConstantInt>(UpperBound)->getSExtValue() << "\n"; |
| 240 | } else { |
| 241 | dbgs() << "NONE\n"; |
| 242 | }); |
| 243 | |
Chris Lattner | ed92216 | 2003-10-07 18:46:23 +0000 | [diff] [blame] | 244 | // Create a new node that checks if the value is < pivot. Go to the |
| 245 | // left branch if it is and right branch if not. |
| 246 | Function* F = OrigBlock->getParent(); |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 247 | BasicBlock* NewNode = BasicBlock::Create(Val->getContext(), "NodeBlock"); |
Chris Lattner | ed92216 | 2003-10-07 18:46:23 +0000 | [diff] [blame] | 248 | |
Bob Wilson | e407736 | 2013-09-09 19:14:35 +0000 | [diff] [blame] | 249 | ICmpInst* Comp = new ICmpInst(ICmpInst::ICMP_SLT, |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 250 | Val, Pivot.Low, "Pivot"); |
Marcello Maggioni | 78035b1 | 2014-07-11 10:34:36 +0000 | [diff] [blame] | 251 | |
| 252 | BasicBlock *LBranch = switchConvert(LHS.begin(), LHS.end(), LowerBound, |
| 253 | NewUpperBound, Val, NewNode, OrigBlock, |
| 254 | Default); |
| 255 | BasicBlock *RBranch = switchConvert(RHS.begin(), RHS.end(), NewLowerBound, |
| 256 | UpperBound, Val, NewNode, OrigBlock, |
| 257 | Default); |
| 258 | |
| 259 | Function::iterator FI = OrigBlock; |
| 260 | F->getBasicBlockList().insert(++FI, NewNode); |
Chris Lattner | ed92216 | 2003-10-07 18:46:23 +0000 | [diff] [blame] | 261 | NewNode->getInstList().push_back(Comp); |
Marcello Maggioni | 78035b1 | 2014-07-11 10:34:36 +0000 | [diff] [blame] | 262 | |
Gabor Greif | e9ecc68 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 263 | BranchInst::Create(LBranch, RBranch, Comp, NewNode); |
Chris Lattner | ed92216 | 2003-10-07 18:46:23 +0000 | [diff] [blame] | 264 | return NewNode; |
| 265 | } |
| 266 | |
| 267 | // newLeafBlock - Create a new leaf block for the binary lookup tree. It |
| 268 | // checks if the switch's value == the case's value. If not, then it |
| 269 | // jumps to the default branch. At this point in the tree, the value |
| 270 | // can't be another valid case value, so the jump to the "default" branch |
| 271 | // is warranted. |
| 272 | // |
Anton Korobeynikov | 8a6dc10 | 2007-03-10 16:46:28 +0000 | [diff] [blame] | 273 | BasicBlock* LowerSwitch::newLeafBlock(CaseRange& Leaf, Value* Val, |
Chris Lattner | ed92216 | 2003-10-07 18:46:23 +0000 | [diff] [blame] | 274 | BasicBlock* OrigBlock, |
| 275 | BasicBlock* Default) |
| 276 | { |
| 277 | Function* F = OrigBlock->getParent(); |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 278 | BasicBlock* NewLeaf = BasicBlock::Create(Val->getContext(), "LeafBlock"); |
Chris Lattner | 233f97a | 2007-04-17 18:09:47 +0000 | [diff] [blame] | 279 | Function::iterator FI = OrigBlock; |
| 280 | F->getBasicBlockList().insert(++FI, NewLeaf); |
Chris Lattner | ed92216 | 2003-10-07 18:46:23 +0000 | [diff] [blame] | 281 | |
Anton Korobeynikov | 8a6dc10 | 2007-03-10 16:46:28 +0000 | [diff] [blame] | 282 | // Emit comparison |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 283 | ICmpInst* Comp = nullptr; |
Anton Korobeynikov | 8a6dc10 | 2007-03-10 16:46:28 +0000 | [diff] [blame] | 284 | if (Leaf.Low == Leaf.High) { |
| 285 | // Make the seteq instruction... |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 286 | Comp = new ICmpInst(*NewLeaf, ICmpInst::ICMP_EQ, Val, |
| 287 | Leaf.Low, "SwitchLeaf"); |
Anton Korobeynikov | 8a6dc10 | 2007-03-10 16:46:28 +0000 | [diff] [blame] | 288 | } else { |
| 289 | // Make range comparison |
| 290 | if (cast<ConstantInt>(Leaf.Low)->isMinValue(true /*isSigned*/)) { |
| 291 | // Val >= Min && Val <= Hi --> Val <= Hi |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 292 | Comp = new ICmpInst(*NewLeaf, ICmpInst::ICMP_SLE, Val, Leaf.High, |
| 293 | "SwitchLeaf"); |
Anton Korobeynikov | 8a6dc10 | 2007-03-10 16:46:28 +0000 | [diff] [blame] | 294 | } else if (cast<ConstantInt>(Leaf.Low)->isZero()) { |
| 295 | // Val >= 0 && Val <= Hi --> Val <=u Hi |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 296 | Comp = new ICmpInst(*NewLeaf, ICmpInst::ICMP_ULE, Val, Leaf.High, |
| 297 | "SwitchLeaf"); |
Anton Korobeynikov | 8a6dc10 | 2007-03-10 16:46:28 +0000 | [diff] [blame] | 298 | } else { |
| 299 | // Emit V-Lo <=u Hi-Lo |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 300 | Constant* NegLo = ConstantExpr::getNeg(Leaf.Low); |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 301 | Instruction* Add = BinaryOperator::CreateAdd(Val, NegLo, |
Anton Korobeynikov | 8a6dc10 | 2007-03-10 16:46:28 +0000 | [diff] [blame] | 302 | Val->getName()+".off", |
| 303 | NewLeaf); |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 304 | Constant *UpperBound = ConstantExpr::getAdd(NegLo, Leaf.High); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 305 | Comp = new ICmpInst(*NewLeaf, ICmpInst::ICMP_ULE, Add, UpperBound, |
| 306 | "SwitchLeaf"); |
Anton Korobeynikov | 8a6dc10 | 2007-03-10 16:46:28 +0000 | [diff] [blame] | 307 | } |
| 308 | } |
Chris Lattner | ed92216 | 2003-10-07 18:46:23 +0000 | [diff] [blame] | 309 | |
| 310 | // Make the conditional branch... |
Anton Korobeynikov | 8a6dc10 | 2007-03-10 16:46:28 +0000 | [diff] [blame] | 311 | BasicBlock* Succ = Leaf.BB; |
Gabor Greif | e9ecc68 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 312 | BranchInst::Create(Succ, Default, Comp, NewLeaf); |
Chris Lattner | ed92216 | 2003-10-07 18:46:23 +0000 | [diff] [blame] | 313 | |
| 314 | // If there were any PHI nodes in this successor, rewrite one entry |
| 315 | // from OrigBlock to come from NewLeaf. |
Reid Spencer | 6614946 | 2004-09-15 17:06:42 +0000 | [diff] [blame] | 316 | for (BasicBlock::iterator I = Succ->begin(); isa<PHINode>(I); ++I) { |
| 317 | PHINode* PN = cast<PHINode>(I); |
Anton Korobeynikov | 8a6dc10 | 2007-03-10 16:46:28 +0000 | [diff] [blame] | 318 | // Remove all but one incoming entries from the cluster |
| 319 | uint64_t Range = cast<ConstantInt>(Leaf.High)->getSExtValue() - |
| 320 | cast<ConstantInt>(Leaf.Low)->getSExtValue(); |
| 321 | for (uint64_t j = 0; j < Range; ++j) { |
| 322 | PN->removeIncomingValue(OrigBlock); |
| 323 | } |
| 324 | |
Chris Lattner | ed92216 | 2003-10-07 18:46:23 +0000 | [diff] [blame] | 325 | int BlockIdx = PN->getBasicBlockIndex(OrigBlock); |
| 326 | assert(BlockIdx != -1 && "Switch didn't go to this successor??"); |
| 327 | PN->setIncomingBlock((unsigned)BlockIdx, NewLeaf); |
| 328 | } |
| 329 | |
| 330 | return NewLeaf; |
| 331 | } |
| 332 | |
Anton Korobeynikov | 8a6dc10 | 2007-03-10 16:46:28 +0000 | [diff] [blame] | 333 | // Clusterify - Transform simple list of Cases into list of CaseRange's |
| 334 | unsigned LowerSwitch::Clusterify(CaseVector& Cases, SwitchInst *SI) { |
Bob Wilson | e407736 | 2013-09-09 19:14:35 +0000 | [diff] [blame] | 335 | unsigned numCmps = 0; |
Anton Korobeynikov | 8a6dc10 | 2007-03-10 16:46:28 +0000 | [diff] [blame] | 336 | |
| 337 | // Start with "simple" cases |
Bob Wilson | e407736 | 2013-09-09 19:14:35 +0000 | [diff] [blame] | 338 | for (SwitchInst::CaseIt i = SI->case_begin(), e = SI->case_end(); i != e; ++i) |
| 339 | Cases.push_back(CaseRange(i.getCaseValue(), i.getCaseValue(), |
| 340 | i.getCaseSuccessor())); |
Stepan Dyatkovskiy | 5b648af | 2012-03-08 07:06:20 +0000 | [diff] [blame] | 341 | |
Bob Wilson | e407736 | 2013-09-09 19:14:35 +0000 | [diff] [blame] | 342 | std::sort(Cases.begin(), Cases.end(), CaseCmp()); |
| 343 | |
| 344 | // Merge case into clusters |
| 345 | if (Cases.size()>=2) |
Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 346 | for (CaseItr I = Cases.begin(), J = std::next(Cases.begin()); |
| 347 | J != Cases.end();) { |
Bob Wilson | e407736 | 2013-09-09 19:14:35 +0000 | [diff] [blame] | 348 | int64_t nextValue = cast<ConstantInt>(J->Low)->getSExtValue(); |
| 349 | int64_t currentValue = cast<ConstantInt>(I->High)->getSExtValue(); |
| 350 | BasicBlock* nextBB = J->BB; |
| 351 | BasicBlock* currentBB = I->BB; |
| 352 | |
| 353 | // If the two neighboring cases go to the same destination, merge them |
| 354 | // into a single case. |
| 355 | if ((nextValue-currentValue==1) && (currentBB == nextBB)) { |
| 356 | I->High = J->High; |
| 357 | J = Cases.erase(J); |
| 358 | } else { |
| 359 | I = J++; |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | for (CaseItr I=Cases.begin(), E=Cases.end(); I!=E; ++I, ++numCmps) { |
| 364 | if (I->Low != I->High) |
Anton Korobeynikov | 8a6dc10 | 2007-03-10 16:46:28 +0000 | [diff] [blame] | 365 | // A range counts double, since it requires two compares. |
| 366 | ++numCmps; |
| 367 | } |
| 368 | |
Bob Wilson | e407736 | 2013-09-09 19:14:35 +0000 | [diff] [blame] | 369 | return numCmps; |
Anton Korobeynikov | 8a6dc10 | 2007-03-10 16:46:28 +0000 | [diff] [blame] | 370 | } |
| 371 | |
Chris Lattner | 1b094a0 | 2003-04-23 16:23:59 +0000 | [diff] [blame] | 372 | // processSwitchInst - Replace the specified switch instruction with a sequence |
Chris Lattner | ed92216 | 2003-10-07 18:46:23 +0000 | [diff] [blame] | 373 | // of chained if-then insts in a balanced binary search. |
Chris Lattner | 1b094a0 | 2003-04-23 16:23:59 +0000 | [diff] [blame] | 374 | // |
| 375 | void LowerSwitch::processSwitchInst(SwitchInst *SI) { |
| 376 | BasicBlock *CurBlock = SI->getParent(); |
| 377 | BasicBlock *OrigBlock = CurBlock; |
| 378 | Function *F = CurBlock->getParent(); |
Eli Friedman | 95031ed | 2011-09-29 20:21:17 +0000 | [diff] [blame] | 379 | Value *Val = SI->getCondition(); // The value we are switching on... |
Chris Lattner | ed92216 | 2003-10-07 18:46:23 +0000 | [diff] [blame] | 380 | BasicBlock* Default = SI->getDefaultDest(); |
Chris Lattner | 1b094a0 | 2003-04-23 16:23:59 +0000 | [diff] [blame] | 381 | |
Chris Lattner | f1b1c5e | 2003-08-23 22:54:34 +0000 | [diff] [blame] | 382 | // If there is only the default destination, don't bother with the code below. |
Stepan Dyatkovskiy | 513aaa5 | 2012-02-01 07:49:51 +0000 | [diff] [blame] | 383 | if (!SI->getNumCases()) { |
Gabor Greif | e9ecc68 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 384 | BranchInst::Create(SI->getDefaultDest(), CurBlock); |
Chris Lattner | b686595 | 2004-03-14 04:14:31 +0000 | [diff] [blame] | 385 | CurBlock->getInstList().erase(SI); |
Chris Lattner | f1b1c5e | 2003-08-23 22:54:34 +0000 | [diff] [blame] | 386 | return; |
| 387 | } |
| 388 | |
Jim Grosbach | fff5663 | 2014-06-16 16:55:20 +0000 | [diff] [blame] | 389 | const bool DefaultIsUnreachable = |
| 390 | Default->size() == 1 && isa<UnreachableInst>(Default->getTerminator()); |
Chris Lattner | ed92216 | 2003-10-07 18:46:23 +0000 | [diff] [blame] | 391 | // Create a new, empty default block so that the new hierarchy of |
| 392 | // if-then statements go to this and the PHI nodes are happy. |
Jim Grosbach | fff5663 | 2014-06-16 16:55:20 +0000 | [diff] [blame] | 393 | // if the default block is set as an unreachable we avoid creating one |
| 394 | // because will never be a valid target. |
| 395 | BasicBlock *NewDefault = nullptr; |
| 396 | if (!DefaultIsUnreachable) { |
| 397 | NewDefault = BasicBlock::Create(SI->getContext(), "NewDefault"); |
| 398 | F->getBasicBlockList().insert(Default, NewDefault); |
Chris Lattner | 1b094a0 | 2003-04-23 16:23:59 +0000 | [diff] [blame] | 399 | |
Jim Grosbach | fff5663 | 2014-06-16 16:55:20 +0000 | [diff] [blame] | 400 | BranchInst::Create(Default, NewDefault); |
| 401 | } |
Chris Lattner | ed92216 | 2003-10-07 18:46:23 +0000 | [diff] [blame] | 402 | // If there is an entry in any PHI nodes for the default edge, make sure |
| 403 | // to update them as well. |
Reid Spencer | 6614946 | 2004-09-15 17:06:42 +0000 | [diff] [blame] | 404 | for (BasicBlock::iterator I = Default->begin(); isa<PHINode>(I); ++I) { |
| 405 | PHINode *PN = cast<PHINode>(I); |
Chris Lattner | ed92216 | 2003-10-07 18:46:23 +0000 | [diff] [blame] | 406 | int BlockIdx = PN->getBasicBlockIndex(OrigBlock); |
| 407 | assert(BlockIdx != -1 && "Switch didn't go to this successor??"); |
| 408 | PN->setIncomingBlock((unsigned)BlockIdx, NewDefault); |
Chris Lattner | 1b094a0 | 2003-04-23 16:23:59 +0000 | [diff] [blame] | 409 | } |
| 410 | |
Anton Korobeynikov | 8a6dc10 | 2007-03-10 16:46:28 +0000 | [diff] [blame] | 411 | // Prepare cases vector. |
| 412 | CaseVector Cases; |
| 413 | unsigned numCmps = Clusterify(Cases, SI); |
Chris Lattner | ed92216 | 2003-10-07 18:46:23 +0000 | [diff] [blame] | 414 | |
David Greene | 50c5423 | 2010-01-05 01:26:45 +0000 | [diff] [blame] | 415 | DEBUG(dbgs() << "Clusterify finished. Total clusters: " << Cases.size() |
Dan Gohman | 29f2baf | 2009-07-25 01:13:51 +0000 | [diff] [blame] | 416 | << ". Total compares: " << numCmps << "\n"); |
David Greene | 50c5423 | 2010-01-05 01:26:45 +0000 | [diff] [blame] | 417 | DEBUG(dbgs() << "Cases: " << Cases << "\n"); |
Mike Stump | d934cc0 | 2009-07-27 23:14:11 +0000 | [diff] [blame] | 418 | (void)numCmps; |
Anton Korobeynikov | 8a6dc10 | 2007-03-10 16:46:28 +0000 | [diff] [blame] | 419 | |
Jim Grosbach | fff5663 | 2014-06-16 16:55:20 +0000 | [diff] [blame] | 420 | ConstantInt *UpperBound = nullptr; |
| 421 | ConstantInt *LowerBound = nullptr; |
| 422 | |
| 423 | // Optimize the condition where Default is an unreachable block. In this case |
| 424 | // we can make the bounds tightly fitted around the case value ranges, |
| 425 | // because we know that the value passed to the switch should always be |
| 426 | // exactly one of the case values. |
| 427 | if (DefaultIsUnreachable) { |
| 428 | CaseItr LastCase = Cases.begin() + Cases.size() - 1; |
| 429 | UpperBound = cast<ConstantInt>(LastCase->High); |
| 430 | LowerBound = cast<ConstantInt>(Cases.begin()->Low); |
| 431 | } |
| 432 | BasicBlock *SwitchBlock = |
| 433 | switchConvert(Cases.begin(), Cases.end(), LowerBound, UpperBound, Val, |
Marcello Maggioni | 78035b1 | 2014-07-11 10:34:36 +0000 | [diff] [blame] | 434 | OrigBlock, OrigBlock, NewDefault); |
Chris Lattner | ed92216 | 2003-10-07 18:46:23 +0000 | [diff] [blame] | 435 | |
| 436 | // Branch to our shiny new if-then stuff... |
Gabor Greif | e9ecc68 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 437 | BranchInst::Create(SwitchBlock, OrigBlock); |
Chris Lattner | ed92216 | 2003-10-07 18:46:23 +0000 | [diff] [blame] | 438 | |
Chris Lattner | 1b094a0 | 2003-04-23 16:23:59 +0000 | [diff] [blame] | 439 | // We are now done with the switch instruction, delete it. |
Chris Lattner | b686595 | 2004-03-14 04:14:31 +0000 | [diff] [blame] | 440 | CurBlock->getInstList().erase(SI); |
Jim Grosbach | fff5663 | 2014-06-16 16:55:20 +0000 | [diff] [blame] | 441 | |
| 442 | pred_iterator PI = pred_begin(Default), E = pred_end(Default); |
| 443 | // If the Default block has no more predecessors just remove it |
| 444 | if (PI == E) { |
| 445 | DeleteDeadBlock(Default); |
| 446 | } |
Chris Lattner | 1b094a0 | 2003-04-23 16:23:59 +0000 | [diff] [blame] | 447 | } |