LowerSwitch.cpp 24 KB

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