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