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