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