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