1 //===- LowerSwitch.cpp - Eliminate Switch instructions --------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
14 //===----------------------------------------------------------------------===//
16 #include "llvm/Transforms/Scalar.h"
17 #include "llvm/ADT/STLExtras.h"
18 #include "llvm/IR/CFG.h"
19 #include "llvm/IR/Constants.h"
20 #include "llvm/IR/Function.h"
21 #include "llvm/IR/Instructions.h"
22 #include "llvm/IR/LLVMContext.h"
23 #include "llvm/Pass.h"
24 #include "llvm/Support/Compiler.h"
25 #include "llvm/Support/Debug.h"
26 #include "llvm/Support/raw_ostream.h"
27 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
28 #include "llvm/Transforms/Utils/UnifyFunctionExitNodes.h"
32 #define DEBUG_TYPE "lower-switch"
38 // Return true iff R is covered by Ranges.
39 static bool IsInRanges(const IntRange &R,
40 const std::vector<IntRange> &Ranges) {
41 // Note: Ranges must be sorted, non-overlapping and non-adjacent.
43 // Find the first range whose High field is >= R.High,
44 // then check if the Low field is <= R.Low. If so, we
45 // have a Range that covers R.
46 auto I = std::lower_bound(
47 Ranges.begin(), Ranges.end(), R,
48 [](const IntRange &A, const IntRange &B) { return A.High < B.High; });
49 return I != Ranges.end() && I->Low <= R.Low;
52 /// LowerSwitch Pass - Replace all SwitchInst instructions with chained branch
54 class LowerSwitch : public FunctionPass {
56 static char ID; // Pass identification, replacement for typeid
57 LowerSwitch() : FunctionPass(ID) {
58 initializeLowerSwitchPass(*PassRegistry::getPassRegistry());
61 bool runOnFunction(Function &F) override;
63 void getAnalysisUsage(AnalysisUsage &AU) const override {
64 // This is a cluster of orthogonal Transforms
65 AU.addPreserved<UnifyFunctionExitNodes>();
66 AU.addPreservedID(LowerInvokePassID);
74 CaseRange(ConstantInt *low, ConstantInt *high, BasicBlock *bb)
75 : Low(low), High(high), BB(bb) {}
78 typedef std::vector<CaseRange> CaseVector;
79 typedef std::vector<CaseRange>::iterator CaseItr;
81 void processSwitchInst(SwitchInst *SI);
83 BasicBlock *switchConvert(CaseItr Begin, CaseItr End,
84 ConstantInt *LowerBound, ConstantInt *UpperBound,
85 Value *Val, BasicBlock *Predecessor,
86 BasicBlock *OrigBlock, BasicBlock *Default,
87 const std::vector<IntRange> &UnreachableRanges);
88 BasicBlock *newLeafBlock(CaseRange &Leaf, Value *Val, BasicBlock *OrigBlock,
90 unsigned Clusterify(CaseVector &Cases, SwitchInst *SI);
93 /// The comparison function for sorting the switch case values in the vector.
94 /// WARNING: Case ranges should be disjoint!
96 bool operator () (const LowerSwitch::CaseRange& C1,
97 const LowerSwitch::CaseRange& C2) {
99 const ConstantInt* CI1 = cast<const ConstantInt>(C1.Low);
100 const ConstantInt* CI2 = cast<const ConstantInt>(C2.High);
101 return CI1->getValue().slt(CI2->getValue());
106 char LowerSwitch::ID = 0;
107 INITIALIZE_PASS(LowerSwitch, "lowerswitch",
108 "Lower SwitchInst's to branches", false, false)
110 // Publicly exposed interface to pass...
111 char &llvm::LowerSwitchID = LowerSwitch::ID;
112 // createLowerSwitchPass - Interface to this file...
113 FunctionPass *llvm::createLowerSwitchPass() {
114 return new LowerSwitch();
117 bool LowerSwitch::runOnFunction(Function &F) {
118 bool Changed = false;
120 for (Function::iterator I = F.begin(), E = F.end(); I != E; ) {
121 BasicBlock *Cur = I++; // Advance over block so we don't traverse new blocks
123 if (SwitchInst *SI = dyn_cast<SwitchInst>(Cur->getTerminator())) {
125 processSwitchInst(SI);
132 // operator<< - Used for debugging purposes.
134 static raw_ostream& operator<<(raw_ostream &O,
135 const LowerSwitch::CaseVector &C)
137 static raw_ostream& operator<<(raw_ostream &O,
138 const LowerSwitch::CaseVector &C) {
141 for (LowerSwitch::CaseVector::const_iterator B = C.begin(),
142 E = C.end(); B != E; ) {
143 O << *B->Low << " -" << *B->High;
144 if (++B != E) O << ", ";
150 // \brief Update the first occurrence of the "switch statement" BB in the PHI
151 // node with the "new" BB. The other occurrences will:
153 // 1) Be updated by subsequent calls to this function. Switch statements may
154 // have more than one outcoming edge into the same BB if they all have the same
155 // value. When the switch statement is converted these incoming edges are now
156 // coming from multiple BBs.
157 // 2) Removed if subsequent incoming values now share the same case, i.e.,
158 // multiple outcome edges are condensed into one. This is necessary to keep the
159 // number of phi values equal to the number of branches to SuccBB.
160 static void fixPhis(BasicBlock *SuccBB, BasicBlock *OrigBB, BasicBlock *NewBB,
161 unsigned NumMergedCases) {
162 for (BasicBlock::iterator I = SuccBB->begin(), IE = SuccBB->getFirstNonPHI();
164 PHINode *PN = cast<PHINode>(I);
166 // Only update the first occurence.
167 unsigned Idx = 0, E = PN->getNumIncomingValues();
168 unsigned LocalNumMergedCases = NumMergedCases;
169 for (; Idx != E; ++Idx) {
170 if (PN->getIncomingBlock(Idx) == OrigBB) {
171 PN->setIncomingBlock(Idx, NewBB);
176 // Remove additional occurences coming from condensed cases and keep the
177 // number of incoming values equal to the number of branches to SuccBB.
178 SmallVector<unsigned, 8> Indices;
179 for (++Idx; LocalNumMergedCases > 0 && Idx < E; ++Idx)
180 if (PN->getIncomingBlock(Idx) == OrigBB) {
181 Indices.push_back(Idx);
182 LocalNumMergedCases--;
184 // Remove incoming values in the reverse order to prevent invalidating
185 // *successive* index.
186 for (auto III = Indices.rbegin(), IIE = Indices.rend(); III != IIE; ++III)
187 PN->removeIncomingValue(*III);
191 // switchConvert - Convert the switch statement into a binary lookup of
192 // the case values. The function recursively builds this tree.
193 // LowerBound and UpperBound are used to keep track of the bounds for Val
194 // that have already been checked by a block emitted by one of the previous
195 // calls to switchConvert in the call stack.
197 LowerSwitch::switchConvert(CaseItr Begin, CaseItr End, ConstantInt *LowerBound,
198 ConstantInt *UpperBound, Value *Val,
199 BasicBlock *Predecessor, BasicBlock *OrigBlock,
201 const std::vector<IntRange> &UnreachableRanges) {
202 unsigned Size = End - Begin;
205 // Check if the Case Range is perfectly squeezed in between
206 // already checked Upper and Lower bounds. If it is then we can avoid
207 // emitting the code that checks if the value actually falls in the range
208 // because the bounds already tell us so.
209 if (Begin->Low == LowerBound && Begin->High == UpperBound) {
210 unsigned NumMergedCases = 0;
211 if (LowerBound && UpperBound)
213 UpperBound->getSExtValue() - LowerBound->getSExtValue();
214 fixPhis(Begin->BB, OrigBlock, Predecessor, NumMergedCases);
217 return newLeafBlock(*Begin, Val, OrigBlock, Default);
220 unsigned Mid = Size / 2;
221 std::vector<CaseRange> LHS(Begin, Begin + Mid);
222 DEBUG(dbgs() << "LHS: " << LHS << "\n");
223 std::vector<CaseRange> RHS(Begin + Mid, End);
224 DEBUG(dbgs() << "RHS: " << RHS << "\n");
226 CaseRange &Pivot = *(Begin + Mid);
227 DEBUG(dbgs() << "Pivot ==> "
228 << Pivot.Low->getValue()
229 << " -" << Pivot.High->getValue() << "\n");
231 // NewLowerBound here should never be the integer minimal value.
232 // This is because it is computed from a case range that is never
233 // the smallest, so there is always a case range that has at least
235 ConstantInt *NewLowerBound = Pivot.Low;
237 // Because NewLowerBound is never the smallest representable integer
238 // it is safe here to subtract one.
239 ConstantInt *NewUpperBound = ConstantInt::get(NewLowerBound->getContext(),
240 NewLowerBound->getValue() - 1);
242 if (!UnreachableRanges.empty()) {
243 // Check if the gap between LHS's highest and NewLowerBound is unreachable.
244 int64_t GapLow = LHS.back().High->getSExtValue() + 1;
245 int64_t GapHigh = NewLowerBound->getSExtValue() - 1;
246 IntRange Gap = { GapLow, GapHigh };
247 if (GapHigh >= GapLow && IsInRanges(Gap, UnreachableRanges))
248 NewUpperBound = LHS.back().High;
251 DEBUG(dbgs() << "LHS Bounds ==> ";
253 dbgs() << LowerBound->getSExtValue();
257 dbgs() << " - " << NewUpperBound->getSExtValue() << "\n";
258 dbgs() << "RHS Bounds ==> ";
259 dbgs() << NewLowerBound->getSExtValue() << " - ";
261 dbgs() << UpperBound->getSExtValue() << "\n";
266 // Create a new node that checks if the value is < pivot. Go to the
267 // left branch if it is and right branch if not.
268 Function* F = OrigBlock->getParent();
269 BasicBlock* NewNode = BasicBlock::Create(Val->getContext(), "NodeBlock");
271 ICmpInst* Comp = new ICmpInst(ICmpInst::ICMP_SLT,
272 Val, Pivot.Low, "Pivot");
274 BasicBlock *LBranch = switchConvert(LHS.begin(), LHS.end(), LowerBound,
275 NewUpperBound, Val, NewNode, OrigBlock,
276 Default, UnreachableRanges);
277 BasicBlock *RBranch = switchConvert(RHS.begin(), RHS.end(), NewLowerBound,
278 UpperBound, Val, NewNode, OrigBlock,
279 Default, UnreachableRanges);
281 Function::iterator FI = OrigBlock;
282 F->getBasicBlockList().insert(++FI, NewNode);
283 NewNode->getInstList().push_back(Comp);
285 BranchInst::Create(LBranch, RBranch, Comp, NewNode);
289 // newLeafBlock - Create a new leaf block for the binary lookup tree. It
290 // checks if the switch's value == the case's value. If not, then it
291 // jumps to the default branch. At this point in the tree, the value
292 // can't be another valid case value, so the jump to the "default" branch
295 BasicBlock* LowerSwitch::newLeafBlock(CaseRange& Leaf, Value* Val,
296 BasicBlock* OrigBlock,
299 Function* F = OrigBlock->getParent();
300 BasicBlock* NewLeaf = BasicBlock::Create(Val->getContext(), "LeafBlock");
301 Function::iterator FI = OrigBlock;
302 F->getBasicBlockList().insert(++FI, NewLeaf);
305 ICmpInst* Comp = nullptr;
306 if (Leaf.Low == Leaf.High) {
307 // Make the seteq instruction...
308 Comp = new ICmpInst(*NewLeaf, ICmpInst::ICMP_EQ, Val,
309 Leaf.Low, "SwitchLeaf");
311 // Make range comparison
312 if (Leaf.Low->isMinValue(true /*isSigned*/)) {
313 // Val >= Min && Val <= Hi --> Val <= Hi
314 Comp = new ICmpInst(*NewLeaf, ICmpInst::ICMP_SLE, Val, Leaf.High,
316 } else if (Leaf.Low->isZero()) {
317 // Val >= 0 && Val <= Hi --> Val <=u Hi
318 Comp = new ICmpInst(*NewLeaf, ICmpInst::ICMP_ULE, Val, Leaf.High,
321 // Emit V-Lo <=u Hi-Lo
322 Constant* NegLo = ConstantExpr::getNeg(Leaf.Low);
323 Instruction* Add = BinaryOperator::CreateAdd(Val, NegLo,
324 Val->getName()+".off",
326 Constant *UpperBound = ConstantExpr::getAdd(NegLo, Leaf.High);
327 Comp = new ICmpInst(*NewLeaf, ICmpInst::ICMP_ULE, Add, UpperBound,
332 // Make the conditional branch...
333 BasicBlock* Succ = Leaf.BB;
334 BranchInst::Create(Succ, Default, Comp, NewLeaf);
336 // If there were any PHI nodes in this successor, rewrite one entry
337 // from OrigBlock to come from NewLeaf.
338 for (BasicBlock::iterator I = Succ->begin(); isa<PHINode>(I); ++I) {
339 PHINode* PN = cast<PHINode>(I);
340 // Remove all but one incoming entries from the cluster
341 uint64_t Range = Leaf.High->getSExtValue() -
342 Leaf.Low->getSExtValue();
343 for (uint64_t j = 0; j < Range; ++j) {
344 PN->removeIncomingValue(OrigBlock);
347 int BlockIdx = PN->getBasicBlockIndex(OrigBlock);
348 assert(BlockIdx != -1 && "Switch didn't go to this successor??");
349 PN->setIncomingBlock((unsigned)BlockIdx, NewLeaf);
355 // Clusterify - Transform simple list of Cases into list of CaseRange's
356 unsigned LowerSwitch::Clusterify(CaseVector& Cases, SwitchInst *SI) {
357 unsigned numCmps = 0;
359 // Start with "simple" cases
360 for (SwitchInst::CaseIt i = SI->case_begin(), e = SI->case_end(); i != e; ++i)
361 Cases.push_back(CaseRange(i.getCaseValue(), i.getCaseValue(),
362 i.getCaseSuccessor()));
364 std::sort(Cases.begin(), Cases.end(), CaseCmp());
366 // Merge case into clusters
368 for (CaseItr I = Cases.begin(), J = std::next(Cases.begin());
370 int64_t nextValue = J->Low->getSExtValue();
371 int64_t currentValue = I->High->getSExtValue();
372 BasicBlock* nextBB = J->BB;
373 BasicBlock* currentBB = I->BB;
375 // If the two neighboring cases go to the same destination, merge them
376 // into a single case.
377 if ((nextValue-currentValue==1) && (currentBB == nextBB)) {
385 for (CaseItr I=Cases.begin(), E=Cases.end(); I!=E; ++I, ++numCmps) {
386 if (I->Low != I->High)
387 // A range counts double, since it requires two compares.
394 // processSwitchInst - Replace the specified switch instruction with a sequence
395 // of chained if-then insts in a balanced binary search.
397 void LowerSwitch::processSwitchInst(SwitchInst *SI) {
398 BasicBlock *CurBlock = SI->getParent();
399 BasicBlock *OrigBlock = CurBlock;
400 Function *F = CurBlock->getParent();
401 Value *Val = SI->getCondition(); // The value we are switching on...
402 BasicBlock* Default = SI->getDefaultDest();
404 // If there is only the default destination, just branch.
405 if (!SI->getNumCases()) {
406 BranchInst::Create(Default, CurBlock);
407 SI->eraseFromParent();
411 // Prepare cases vector.
413 unsigned numCmps = Clusterify(Cases, SI);
414 DEBUG(dbgs() << "Clusterify finished. Total clusters: " << Cases.size()
415 << ". Total compares: " << numCmps << "\n");
416 DEBUG(dbgs() << "Cases: " << Cases << "\n");
419 ConstantInt *LowerBound = nullptr;
420 ConstantInt *UpperBound = nullptr;
421 std::vector<IntRange> UnreachableRanges;
423 if (isa<UnreachableInst>(Default->getFirstNonPHIOrDbg())) {
424 // Make the bounds tightly fitted around the case value range, becase we
425 // know that the value passed to the switch must be exactly one of the case
427 assert(!Cases.empty());
428 LowerBound = Cases.front().Low;
429 UpperBound = Cases.back().High;
431 DenseMap<BasicBlock *, unsigned> Popularity;
433 BasicBlock *PopSucc = nullptr;
435 IntRange R = { INT64_MIN, INT64_MAX };
436 UnreachableRanges.push_back(R);
437 for (const auto &I : Cases) {
438 int64_t Low = I.Low->getSExtValue();
439 int64_t High = I.High->getSExtValue();
441 IntRange &LastRange = UnreachableRanges.back();
442 if (LastRange.Low == Low) {
443 // There is nothing left of the previous range.
444 UnreachableRanges.pop_back();
446 // Terminate the previous range.
447 assert(Low > LastRange.Low);
448 LastRange.High = Low - 1;
450 if (High != INT64_MAX) {
451 IntRange R = { High + 1, INT64_MAX };
452 UnreachableRanges.push_back(R);
456 int64_t N = High - Low + 1;
457 unsigned &Pop = Popularity[I.BB];
458 if ((Pop += N) > MaxPop) {
464 /* UnreachableRanges should be sorted and the ranges non-adjacent. */
465 for (auto I = UnreachableRanges.begin(), E = UnreachableRanges.end();
467 assert(I->Low <= I->High);
470 assert(Next->Low > I->High);
475 // Use the most popular block as the new default, reducing the number of
477 assert(MaxPop > 0 && PopSucc);
479 for (CaseItr I = Cases.begin(); I != Cases.end();) {
480 if (I->BB == PopSucc)
486 // If there are no cases left, just branch.
488 BranchInst::Create(Default, CurBlock);
489 SI->eraseFromParent();
494 // Create a new, empty default block so that the new hierarchy of
495 // if-then statements go to this and the PHI nodes are happy.
496 BasicBlock *NewDefault = BasicBlock::Create(SI->getContext(), "NewDefault");
497 F->getBasicBlockList().insert(Default, NewDefault);
498 BranchInst::Create(Default, NewDefault);
500 // If there is an entry in any PHI nodes for the default edge, make sure
501 // to update them as well.
502 for (BasicBlock::iterator I = Default->begin(); isa<PHINode>(I); ++I) {
503 PHINode *PN = cast<PHINode>(I);
504 int BlockIdx = PN->getBasicBlockIndex(OrigBlock);
505 assert(BlockIdx != -1 && "Switch didn't go to this successor??");
506 PN->setIncomingBlock((unsigned)BlockIdx, NewDefault);
509 BasicBlock *SwitchBlock =
510 switchConvert(Cases.begin(), Cases.end(), LowerBound, UpperBound, Val,
511 OrigBlock, OrigBlock, NewDefault, UnreachableRanges);
513 // Branch to our shiny new if-then stuff...
514 BranchInst::Create(SwitchBlock, OrigBlock);
516 // We are now done with the switch instruction, delete it.
517 BasicBlock *OldDefault = SI->getDefaultDest();
518 CurBlock->getInstList().erase(SI);
520 // If the Default block has no more predecessors just remove it.
521 if (pred_begin(OldDefault) == pred_end(OldDefault))
522 DeleteDeadBlock(OldDefault);