1 //===- LowerSwitch.cpp - Eliminate Switch instructions --------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // The LowerSwitch transformation rewrites switch instructions with a sequence
10 // of branches, which allows targets to get away with not implementing the
11 // switch instruction until it is convenient.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "llvm/ADT/DenseMap.h"
16 #include "llvm/ADT/STLExtras.h"
17 #include "llvm/ADT/SmallPtrSet.h"
18 #include "llvm/ADT/SmallVector.h"
19 #include "llvm/Analysis/AssumptionCache.h"
20 #include "llvm/Analysis/LazyValueInfo.h"
21 #include "llvm/Analysis/ValueTracking.h"
22 #include "llvm/IR/BasicBlock.h"
23 #include "llvm/IR/CFG.h"
24 #include "llvm/IR/ConstantRange.h"
25 #include "llvm/IR/Constants.h"
26 #include "llvm/IR/Function.h"
27 #include "llvm/IR/InstrTypes.h"
28 #include "llvm/IR/Instructions.h"
29 #include "llvm/IR/Value.h"
30 #include "llvm/InitializePasses.h"
31 #include "llvm/Pass.h"
32 #include "llvm/Support/Casting.h"
33 #include "llvm/Support/Compiler.h"
34 #include "llvm/Support/Debug.h"
35 #include "llvm/Support/KnownBits.h"
36 #include "llvm/Support/raw_ostream.h"
37 #include "llvm/Transforms/Utils.h"
38 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
39 #include <algorithm>
40 #include <cassert>
41 #include <cstdint>
42 #include <iterator>
43 #include <limits>
44 #include <vector>
45 
46 using namespace llvm;
47 
48 #define DEBUG_TYPE "lower-switch"
49 
50 namespace {
51 
52   struct IntRange {
53     int64_t Low, High;
54   };
55 
56 } // end anonymous namespace
57 
58 // Return true iff R is covered by Ranges.
59 static bool IsInRanges(const IntRange &R,
60                        const std::vector<IntRange> &Ranges) {
61   // Note: Ranges must be sorted, non-overlapping and non-adjacent.
62 
63   // Find the first range whose High field is >= R.High,
64   // then check if the Low field is <= R.Low. If so, we
65   // have a Range that covers R.
66   auto I = llvm::lower_bound(
67       Ranges, R, [](IntRange A, IntRange B) { return A.High < B.High; });
68   return I != Ranges.end() && I->Low <= R.Low;
69 }
70 
71 namespace {
72 
73   /// Replace all SwitchInst instructions with chained branch instructions.
74   class LowerSwitch : public FunctionPass {
75   public:
76     // Pass identification, replacement for typeid
77     static char ID;
78 
79     LowerSwitch() : FunctionPass(ID) {
80       initializeLowerSwitchPass(*PassRegistry::getPassRegistry());
81     }
82 
83     bool runOnFunction(Function &F) override;
84 
85     void getAnalysisUsage(AnalysisUsage &AU) const override {
86       AU.addRequired<LazyValueInfoWrapperPass>();
87     }
88 
89     struct CaseRange {
90       ConstantInt* Low;
91       ConstantInt* High;
92       BasicBlock* BB;
93 
94       CaseRange(ConstantInt *low, ConstantInt *high, BasicBlock *bb)
95           : Low(low), High(high), BB(bb) {}
96     };
97 
98     using CaseVector = std::vector<CaseRange>;
99     using CaseItr = std::vector<CaseRange>::iterator;
100 
101   private:
102     void processSwitchInst(SwitchInst *SI,
103                            SmallPtrSetImpl<BasicBlock *> &DeleteList,
104                            AssumptionCache *AC, LazyValueInfo *LVI);
105 
106     BasicBlock *switchConvert(CaseItr Begin, CaseItr End,
107                               ConstantInt *LowerBound, ConstantInt *UpperBound,
108                               Value *Val, BasicBlock *Predecessor,
109                               BasicBlock *OrigBlock, BasicBlock *Default,
110                               const std::vector<IntRange> &UnreachableRanges);
111     BasicBlock *newLeafBlock(CaseRange &Leaf, Value *Val,
112                              ConstantInt *LowerBound, ConstantInt *UpperBound,
113                              BasicBlock *OrigBlock, BasicBlock *Default);
114     unsigned Clusterify(CaseVector &Cases, SwitchInst *SI);
115   };
116 
117   /// The comparison function for sorting the switch case values in the vector.
118   /// WARNING: Case ranges should be disjoint!
119   struct CaseCmp {
120     bool operator()(const LowerSwitch::CaseRange& C1,
121                     const LowerSwitch::CaseRange& C2) {
122       const ConstantInt* CI1 = cast<const ConstantInt>(C1.Low);
123       const ConstantInt* CI2 = cast<const ConstantInt>(C2.High);
124       return CI1->getValue().slt(CI2->getValue());
125     }
126   };
127 
128 } // end anonymous namespace
129 
130 char LowerSwitch::ID = 0;
131 
132 // Publicly exposed interface to pass...
133 char &llvm::LowerSwitchID = LowerSwitch::ID;
134 
135 INITIALIZE_PASS_BEGIN(LowerSwitch, "lowerswitch",
136                       "Lower SwitchInst's to branches", false, false)
137 INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
138 INITIALIZE_PASS_DEPENDENCY(LazyValueInfoWrapperPass)
139 INITIALIZE_PASS_END(LowerSwitch, "lowerswitch",
140                     "Lower SwitchInst's to branches", false, false)
141 
142 // createLowerSwitchPass - Interface to this file...
143 FunctionPass *llvm::createLowerSwitchPass() {
144   return new LowerSwitch();
145 }
146 
147 bool LowerSwitch::runOnFunction(Function &F) {
148   LazyValueInfo *LVI = &getAnalysis<LazyValueInfoWrapperPass>().getLVI();
149   auto *ACT = getAnalysisIfAvailable<AssumptionCacheTracker>();
150   AssumptionCache *AC = ACT ? &ACT->getAssumptionCache(F) : nullptr;
151   // Prevent LazyValueInfo from using the DominatorTree as LowerSwitch does not
152   // preserve it and it becomes stale (when available) pretty much immediately.
153   // Currently the DominatorTree is only used by LowerSwitch indirectly via LVI
154   // and computeKnownBits to refine isValidAssumeForContext's results. Given
155   // that the latter can handle some of the simple cases w/o a DominatorTree,
156   // it's easier to refrain from using the tree than to keep it up to date.
157   LVI->disableDT();
158 
159   bool Changed = false;
160   SmallPtrSet<BasicBlock*, 8> DeleteList;
161 
162   for (Function::iterator I = F.begin(), E = F.end(); I != E; ) {
163     BasicBlock *Cur = &*I++; // Advance over block so we don't traverse new blocks
164 
165     // If the block is a dead Default block that will be deleted later, don't
166     // waste time processing it.
167     if (DeleteList.count(Cur))
168       continue;
169 
170     if (SwitchInst *SI = dyn_cast<SwitchInst>(Cur->getTerminator())) {
171       Changed = true;
172       processSwitchInst(SI, DeleteList, AC, LVI);
173     }
174   }
175 
176   for (BasicBlock* BB: DeleteList) {
177     LVI->eraseBlock(BB);
178     DeleteDeadBlock(BB);
179   }
180 
181   return Changed;
182 }
183 
184 /// Used for debugging purposes.
185 LLVM_ATTRIBUTE_USED
186 static raw_ostream &operator<<(raw_ostream &O,
187                                const LowerSwitch::CaseVector &C) {
188   O << "[";
189 
190   for (LowerSwitch::CaseVector::const_iterator B = C.begin(), E = C.end();
191        B != E;) {
192     O << "[" << B->Low->getValue() << ", " << B->High->getValue() << "]";
193     if (++B != E)
194       O << ", ";
195   }
196 
197   return O << "]";
198 }
199 
200 /// Update the first occurrence of the "switch statement" BB in the PHI
201 /// node with the "new" BB. The other occurrences will:
202 ///
203 /// 1) Be updated by subsequent calls to this function.  Switch statements may
204 /// have more than one outcoming edge into the same BB if they all have the same
205 /// value. When the switch statement is converted these incoming edges are now
206 /// coming from multiple BBs.
207 /// 2) Removed if subsequent incoming values now share the same case, i.e.,
208 /// multiple outcome edges are condensed into one. This is necessary to keep the
209 /// number of phi values equal to the number of branches to SuccBB.
210 static void
211 fixPhis(BasicBlock *SuccBB, BasicBlock *OrigBB, BasicBlock *NewBB,
212         const unsigned NumMergedCases = std::numeric_limits<unsigned>::max()) {
213   for (BasicBlock::iterator I = SuccBB->begin(),
214                             IE = SuccBB->getFirstNonPHI()->getIterator();
215        I != IE; ++I) {
216     PHINode *PN = cast<PHINode>(I);
217 
218     // Only update the first occurrence.
219     unsigned Idx = 0, E = PN->getNumIncomingValues();
220     unsigned LocalNumMergedCases = NumMergedCases;
221     for (; Idx != E; ++Idx) {
222       if (PN->getIncomingBlock(Idx) == OrigBB) {
223         PN->setIncomingBlock(Idx, NewBB);
224         break;
225       }
226     }
227 
228     // Remove additional occurrences coming from condensed cases and keep the
229     // number of incoming values equal to the number of branches to SuccBB.
230     SmallVector<unsigned, 8> Indices;
231     for (++Idx; LocalNumMergedCases > 0 && Idx < E; ++Idx)
232       if (PN->getIncomingBlock(Idx) == OrigBB) {
233         Indices.push_back(Idx);
234         LocalNumMergedCases--;
235       }
236     // Remove incoming values in the reverse order to prevent invalidating
237     // *successive* index.
238     for (unsigned III : llvm::reverse(Indices))
239       PN->removeIncomingValue(III);
240   }
241 }
242 
243 /// Convert the switch statement into a binary lookup of the case values.
244 /// The function recursively builds this tree. LowerBound and UpperBound are
245 /// used to keep track of the bounds for Val that have already been checked by
246 /// a block emitted by one of the previous calls to switchConvert in the call
247 /// stack.
248 BasicBlock *
249 LowerSwitch::switchConvert(CaseItr Begin, CaseItr End, ConstantInt *LowerBound,
250                            ConstantInt *UpperBound, Value *Val,
251                            BasicBlock *Predecessor, BasicBlock *OrigBlock,
252                            BasicBlock *Default,
253                            const std::vector<IntRange> &UnreachableRanges) {
254   assert(LowerBound && UpperBound && "Bounds must be initialized");
255   unsigned Size = End - Begin;
256 
257   if (Size == 1) {
258     // Check if the Case Range is perfectly squeezed in between
259     // already checked Upper and Lower bounds. If it is then we can avoid
260     // emitting the code that checks if the value actually falls in the range
261     // because the bounds already tell us so.
262     if (Begin->Low == LowerBound && Begin->High == UpperBound) {
263       unsigned NumMergedCases = 0;
264       NumMergedCases = UpperBound->getSExtValue() - LowerBound->getSExtValue();
265       fixPhis(Begin->BB, OrigBlock, Predecessor, NumMergedCases);
266       return Begin->BB;
267     }
268     return newLeafBlock(*Begin, Val, LowerBound, UpperBound, OrigBlock,
269                         Default);
270   }
271 
272   unsigned Mid = Size / 2;
273   std::vector<CaseRange> LHS(Begin, Begin + Mid);
274   LLVM_DEBUG(dbgs() << "LHS: " << LHS << "\n");
275   std::vector<CaseRange> RHS(Begin + Mid, End);
276   LLVM_DEBUG(dbgs() << "RHS: " << RHS << "\n");
277 
278   CaseRange &Pivot = *(Begin + Mid);
279   LLVM_DEBUG(dbgs() << "Pivot ==> [" << Pivot.Low->getValue() << ", "
280                     << Pivot.High->getValue() << "]\n");
281 
282   // NewLowerBound here should never be the integer minimal value.
283   // This is because it is computed from a case range that is never
284   // the smallest, so there is always a case range that has at least
285   // a smaller value.
286   ConstantInt *NewLowerBound = Pivot.Low;
287 
288   // Because NewLowerBound is never the smallest representable integer
289   // it is safe here to subtract one.
290   ConstantInt *NewUpperBound = ConstantInt::get(NewLowerBound->getContext(),
291                                                 NewLowerBound->getValue() - 1);
292 
293   if (!UnreachableRanges.empty()) {
294     // Check if the gap between LHS's highest and NewLowerBound is unreachable.
295     int64_t GapLow = LHS.back().High->getSExtValue() + 1;
296     int64_t GapHigh = NewLowerBound->getSExtValue() - 1;
297     IntRange Gap = { GapLow, GapHigh };
298     if (GapHigh >= GapLow && IsInRanges(Gap, UnreachableRanges))
299       NewUpperBound = LHS.back().High;
300   }
301 
302   LLVM_DEBUG(dbgs() << "LHS Bounds ==> [" << LowerBound->getSExtValue() << ", "
303                     << NewUpperBound->getSExtValue() << "]\n"
304                     << "RHS Bounds ==> [" << NewLowerBound->getSExtValue()
305                     << ", " << UpperBound->getSExtValue() << "]\n");
306 
307   // Create a new node that checks if the value is < pivot. Go to the
308   // left branch if it is and right branch if not.
309   Function* F = OrigBlock->getParent();
310   BasicBlock* NewNode = BasicBlock::Create(Val->getContext(), "NodeBlock");
311 
312   ICmpInst* Comp = new ICmpInst(ICmpInst::ICMP_SLT,
313                                 Val, Pivot.Low, "Pivot");
314 
315   BasicBlock *LBranch = switchConvert(LHS.begin(), LHS.end(), LowerBound,
316                                       NewUpperBound, Val, NewNode, OrigBlock,
317                                       Default, UnreachableRanges);
318   BasicBlock *RBranch = switchConvert(RHS.begin(), RHS.end(), NewLowerBound,
319                                       UpperBound, Val, NewNode, OrigBlock,
320                                       Default, UnreachableRanges);
321 
322   F->getBasicBlockList().insert(++OrigBlock->getIterator(), NewNode);
323   NewNode->getInstList().push_back(Comp);
324 
325   BranchInst::Create(LBranch, RBranch, Comp, NewNode);
326   return NewNode;
327 }
328 
329 /// Create a new leaf block for the binary lookup tree. It checks if the
330 /// switch's value == the case's value. If not, then it jumps to the default
331 /// branch. At this point in the tree, the value can't be another valid case
332 /// value, so the jump to the "default" branch is warranted.
333 BasicBlock *LowerSwitch::newLeafBlock(CaseRange &Leaf, Value *Val,
334                                       ConstantInt *LowerBound,
335                                       ConstantInt *UpperBound,
336                                       BasicBlock *OrigBlock,
337                                       BasicBlock *Default) {
338   Function* F = OrigBlock->getParent();
339   BasicBlock* NewLeaf = BasicBlock::Create(Val->getContext(), "LeafBlock");
340   F->getBasicBlockList().insert(++OrigBlock->getIterator(), NewLeaf);
341 
342   // Emit comparison
343   ICmpInst* Comp = nullptr;
344   if (Leaf.Low == Leaf.High) {
345     // Make the seteq instruction...
346     Comp = new ICmpInst(*NewLeaf, ICmpInst::ICMP_EQ, Val,
347                         Leaf.Low, "SwitchLeaf");
348   } else {
349     // Make range comparison
350     if (Leaf.Low == LowerBound) {
351       // Val >= Min && Val <= Hi --> Val <= Hi
352       Comp = new ICmpInst(*NewLeaf, ICmpInst::ICMP_SLE, Val, Leaf.High,
353                           "SwitchLeaf");
354     } else if (Leaf.High == UpperBound) {
355       // Val <= Max && Val >= Lo --> Val >= Lo
356       Comp = new ICmpInst(*NewLeaf, ICmpInst::ICMP_SGE, Val, Leaf.Low,
357                           "SwitchLeaf");
358     } else if (Leaf.Low->isZero()) {
359       // Val >= 0 && Val <= Hi --> Val <=u Hi
360       Comp = new ICmpInst(*NewLeaf, ICmpInst::ICMP_ULE, Val, Leaf.High,
361                           "SwitchLeaf");
362     } else {
363       // Emit V-Lo <=u Hi-Lo
364       Constant* NegLo = ConstantExpr::getNeg(Leaf.Low);
365       Instruction* Add = BinaryOperator::CreateAdd(Val, NegLo,
366                                                    Val->getName()+".off",
367                                                    NewLeaf);
368       Constant *UpperBound = ConstantExpr::getAdd(NegLo, Leaf.High);
369       Comp = new ICmpInst(*NewLeaf, ICmpInst::ICMP_ULE, Add, UpperBound,
370                           "SwitchLeaf");
371     }
372   }
373 
374   // Make the conditional branch...
375   BasicBlock* Succ = Leaf.BB;
376   BranchInst::Create(Succ, Default, Comp, NewLeaf);
377 
378   // If there were any PHI nodes in this successor, rewrite one entry
379   // from OrigBlock to come from NewLeaf.
380   for (BasicBlock::iterator I = Succ->begin(); isa<PHINode>(I); ++I) {
381     PHINode* PN = cast<PHINode>(I);
382     // Remove all but one incoming entries from the cluster
383     uint64_t Range = Leaf.High->getSExtValue() -
384                      Leaf.Low->getSExtValue();
385     for (uint64_t j = 0; j < Range; ++j) {
386       PN->removeIncomingValue(OrigBlock);
387     }
388 
389     int BlockIdx = PN->getBasicBlockIndex(OrigBlock);
390     assert(BlockIdx != -1 && "Switch didn't go to this successor??");
391     PN->setIncomingBlock((unsigned)BlockIdx, NewLeaf);
392   }
393 
394   return NewLeaf;
395 }
396 
397 /// Transform simple list of \p SI's cases into list of CaseRange's \p Cases.
398 /// \post \p Cases wouldn't contain references to \p SI's default BB.
399 /// \returns Number of \p SI's cases that do not reference \p SI's default BB.
400 unsigned LowerSwitch::Clusterify(CaseVector& Cases, SwitchInst *SI) {
401   unsigned NumSimpleCases = 0;
402 
403   // Start with "simple" cases
404   for (auto Case : SI->cases()) {
405     if (Case.getCaseSuccessor() == SI->getDefaultDest())
406       continue;
407     Cases.push_back(CaseRange(Case.getCaseValue(), Case.getCaseValue(),
408                               Case.getCaseSuccessor()));
409     ++NumSimpleCases;
410   }
411 
412   llvm::sort(Cases, CaseCmp());
413 
414   // Merge case into clusters
415   if (Cases.size() >= 2) {
416     CaseItr I = Cases.begin();
417     for (CaseItr J = std::next(I), E = Cases.end(); J != E; ++J) {
418       int64_t nextValue = J->Low->getSExtValue();
419       int64_t currentValue = I->High->getSExtValue();
420       BasicBlock* nextBB = J->BB;
421       BasicBlock* currentBB = I->BB;
422 
423       // If the two neighboring cases go to the same destination, merge them
424       // into a single case.
425       assert(nextValue > currentValue && "Cases should be strictly ascending");
426       if ((nextValue == currentValue + 1) && (currentBB == nextBB)) {
427         I->High = J->High;
428         // FIXME: Combine branch weights.
429       } else if (++I != J) {
430         *I = *J;
431       }
432     }
433     Cases.erase(std::next(I), Cases.end());
434   }
435 
436   return NumSimpleCases;
437 }
438 
439 /// Replace the specified switch instruction with a sequence of chained if-then
440 /// insts in a balanced binary search.
441 void LowerSwitch::processSwitchInst(SwitchInst *SI,
442                                     SmallPtrSetImpl<BasicBlock *> &DeleteList,
443                                     AssumptionCache *AC, LazyValueInfo *LVI) {
444   BasicBlock *OrigBlock = SI->getParent();
445   Function *F = OrigBlock->getParent();
446   Value *Val = SI->getCondition();  // The value we are switching on...
447   BasicBlock* Default = SI->getDefaultDest();
448 
449   // Don't handle unreachable blocks. If there are successors with phis, this
450   // would leave them behind with missing predecessors.
451   if ((OrigBlock != &F->getEntryBlock() && pred_empty(OrigBlock)) ||
452       OrigBlock->getSinglePredecessor() == OrigBlock) {
453     DeleteList.insert(OrigBlock);
454     return;
455   }
456 
457   // Prepare cases vector.
458   CaseVector Cases;
459   const unsigned NumSimpleCases = Clusterify(Cases, SI);
460   LLVM_DEBUG(dbgs() << "Clusterify finished. Total clusters: " << Cases.size()
461                     << ". Total non-default cases: " << NumSimpleCases
462                     << "\nCase clusters: " << Cases << "\n");
463 
464   // If there is only the default destination, just branch.
465   if (Cases.empty()) {
466     BranchInst::Create(Default, OrigBlock);
467     // Remove all the references from Default's PHIs to OrigBlock, but one.
468     fixPhis(Default, OrigBlock, OrigBlock);
469     SI->eraseFromParent();
470     return;
471   }
472 
473   ConstantInt *LowerBound = nullptr;
474   ConstantInt *UpperBound = nullptr;
475   bool DefaultIsUnreachableFromSwitch = false;
476 
477   if (isa<UnreachableInst>(Default->getFirstNonPHIOrDbg())) {
478     // Make the bounds tightly fitted around the case value range, because we
479     // know that the value passed to the switch must be exactly one of the case
480     // values.
481     LowerBound = Cases.front().Low;
482     UpperBound = Cases.back().High;
483     DefaultIsUnreachableFromSwitch = true;
484   } else {
485     // Constraining the range of the value being switched over helps eliminating
486     // unreachable BBs and minimizing the number of `add` instructions
487     // newLeafBlock ends up emitting. Running CorrelatedValuePropagation after
488     // LowerSwitch isn't as good, and also much more expensive in terms of
489     // compile time for the following reasons:
490     // 1. it processes many kinds of instructions, not just switches;
491     // 2. even if limited to icmp instructions only, it will have to process
492     //    roughly C icmp's per switch, where C is the number of cases in the
493     //    switch, while LowerSwitch only needs to call LVI once per switch.
494     const DataLayout &DL = F->getParent()->getDataLayout();
495     KnownBits Known = computeKnownBits(Val, DL, /*Depth=*/0, AC, SI);
496     // TODO Shouldn't this create a signed range?
497     ConstantRange KnownBitsRange =
498         ConstantRange::fromKnownBits(Known, /*IsSigned=*/false);
499     const ConstantRange LVIRange = LVI->getConstantRange(Val, OrigBlock, SI);
500     ConstantRange ValRange = KnownBitsRange.intersectWith(LVIRange);
501     // We delegate removal of unreachable non-default cases to other passes. In
502     // the unlikely event that some of them survived, we just conservatively
503     // maintain the invariant that all the cases lie between the bounds. This
504     // may, however, still render the default case effectively unreachable.
505     APInt Low = Cases.front().Low->getValue();
506     APInt High = Cases.back().High->getValue();
507     APInt Min = APIntOps::smin(ValRange.getSignedMin(), Low);
508     APInt Max = APIntOps::smax(ValRange.getSignedMax(), High);
509 
510     LowerBound = ConstantInt::get(SI->getContext(), Min);
511     UpperBound = ConstantInt::get(SI->getContext(), Max);
512     DefaultIsUnreachableFromSwitch = (Min + (NumSimpleCases - 1) == Max);
513   }
514 
515   std::vector<IntRange> UnreachableRanges;
516 
517   if (DefaultIsUnreachableFromSwitch) {
518     DenseMap<BasicBlock *, unsigned> Popularity;
519     unsigned MaxPop = 0;
520     BasicBlock *PopSucc = nullptr;
521 
522     IntRange R = {std::numeric_limits<int64_t>::min(),
523                   std::numeric_limits<int64_t>::max()};
524     UnreachableRanges.push_back(R);
525     for (const auto &I : Cases) {
526       int64_t Low = I.Low->getSExtValue();
527       int64_t High = I.High->getSExtValue();
528 
529       IntRange &LastRange = UnreachableRanges.back();
530       if (LastRange.Low == Low) {
531         // There is nothing left of the previous range.
532         UnreachableRanges.pop_back();
533       } else {
534         // Terminate the previous range.
535         assert(Low > LastRange.Low);
536         LastRange.High = Low - 1;
537       }
538       if (High != std::numeric_limits<int64_t>::max()) {
539         IntRange R = { High + 1, std::numeric_limits<int64_t>::max() };
540         UnreachableRanges.push_back(R);
541       }
542 
543       // Count popularity.
544       int64_t N = High - Low + 1;
545       unsigned &Pop = Popularity[I.BB];
546       if ((Pop += N) > MaxPop) {
547         MaxPop = Pop;
548         PopSucc = I.BB;
549       }
550     }
551 #ifndef NDEBUG
552     /* UnreachableRanges should be sorted and the ranges non-adjacent. */
553     for (auto I = UnreachableRanges.begin(), E = UnreachableRanges.end();
554          I != E; ++I) {
555       assert(I->Low <= I->High);
556       auto Next = I + 1;
557       if (Next != E) {
558         assert(Next->Low > I->High);
559       }
560     }
561 #endif
562 
563     // As the default block in the switch is unreachable, update the PHI nodes
564     // (remove all of the references to the default block) to reflect this.
565     const unsigned NumDefaultEdges = SI->getNumCases() + 1 - NumSimpleCases;
566     for (unsigned I = 0; I < NumDefaultEdges; ++I)
567       Default->removePredecessor(OrigBlock);
568 
569     // Use the most popular block as the new default, reducing the number of
570     // cases.
571     assert(MaxPop > 0 && PopSucc);
572     Default = PopSucc;
573     Cases.erase(
574         llvm::remove_if(
575             Cases, [PopSucc](const CaseRange &R) { return R.BB == PopSucc; }),
576         Cases.end());
577 
578     // If there are no cases left, just branch.
579     if (Cases.empty()) {
580       BranchInst::Create(Default, OrigBlock);
581       SI->eraseFromParent();
582       // As all the cases have been replaced with a single branch, only keep
583       // one entry in the PHI nodes.
584       for (unsigned I = 0 ; I < (MaxPop - 1) ; ++I)
585         PopSucc->removePredecessor(OrigBlock);
586       return;
587     }
588 
589     // If the condition was a PHI node with the switch block as a predecessor
590     // removing predecessors may have caused the condition to be erased.
591     // Getting the condition value again here protects against that.
592     Val = SI->getCondition();
593   }
594 
595   // Create a new, empty default block so that the new hierarchy of
596   // if-then statements go to this and the PHI nodes are happy.
597   BasicBlock *NewDefault = BasicBlock::Create(SI->getContext(), "NewDefault");
598   F->getBasicBlockList().insert(Default->getIterator(), NewDefault);
599   BranchInst::Create(Default, NewDefault);
600 
601   BasicBlock *SwitchBlock =
602       switchConvert(Cases.begin(), Cases.end(), LowerBound, UpperBound, Val,
603                     OrigBlock, OrigBlock, NewDefault, UnreachableRanges);
604 
605   // If there are entries in any PHI nodes for the default edge, make sure
606   // to update them as well.
607   fixPhis(Default, OrigBlock, NewDefault);
608 
609   // Branch to our shiny new if-then stuff...
610   BranchInst::Create(SwitchBlock, OrigBlock);
611 
612   // We are now done with the switch instruction, delete it.
613   BasicBlock *OldDefault = SI->getDefaultDest();
614   OrigBlock->getInstList().erase(SI);
615 
616   // If the Default block has no more predecessors just add it to DeleteList.
617   if (pred_begin(OldDefault) == pred_end(OldDefault))
618     DeleteList.insert(OldDefault);
619 }
620