Remove the default clause from a fully-covering switch
[llvm-core.git] / lib / Transforms / Utils / LowerSwitch.cpp
blob890afbc46e636b714545958ea4b3317553ed21d2
1 //===- LowerSwitch.cpp - Eliminate Switch instructions --------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
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/ADT/STLExtras.h"
17 #include "llvm/IR/CFG.h"
18 #include "llvm/IR/Constants.h"
19 #include "llvm/IR/Function.h"
20 #include "llvm/IR/Instructions.h"
21 #include "llvm/IR/LLVMContext.h"
22 #include "llvm/Pass.h"
23 #include "llvm/Support/Compiler.h"
24 #include "llvm/Support/Debug.h"
25 #include "llvm/Support/raw_ostream.h"
26 #include "llvm/Transforms/Scalar.h"
27 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
28 #include "llvm/Transforms/Utils/UnifyFunctionExitNodes.h"
29 #include <algorithm>
30 using namespace llvm;
32 #define DEBUG_TYPE "lower-switch"
34 namespace {
35 struct IntRange {
36 int64_t Low, High;
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 /// Replace all SwitchInst instructions with chained branch instructions.
53 class LowerSwitch : public FunctionPass {
54 public:
55 static char ID; // Pass identification, replacement for typeid
56 LowerSwitch() : FunctionPass(ID) {
57 initializeLowerSwitchPass(*PassRegistry::getPassRegistry());
60 bool runOnFunction(Function &F) override;
62 struct CaseRange {
63 ConstantInt* Low;
64 ConstantInt* High;
65 BasicBlock* BB;
67 CaseRange(ConstantInt *low, ConstantInt *high, BasicBlock *bb)
68 : Low(low), High(high), BB(bb) {}
71 typedef std::vector<CaseRange> CaseVector;
72 typedef std::vector<CaseRange>::iterator CaseItr;
73 private:
74 void processSwitchInst(SwitchInst *SI, SmallPtrSetImpl<BasicBlock*> &DeleteList);
76 BasicBlock *switchConvert(CaseItr Begin, CaseItr End,
77 ConstantInt *LowerBound, ConstantInt *UpperBound,
78 Value *Val, BasicBlock *Predecessor,
79 BasicBlock *OrigBlock, BasicBlock *Default,
80 const std::vector<IntRange> &UnreachableRanges);
81 BasicBlock *newLeafBlock(CaseRange &Leaf, Value *Val, BasicBlock *OrigBlock,
82 BasicBlock *Default);
83 unsigned Clusterify(CaseVector &Cases, SwitchInst *SI);
86 /// The comparison function for sorting the switch case values in the vector.
87 /// WARNING: Case ranges should be disjoint!
88 struct CaseCmp {
89 bool operator () (const LowerSwitch::CaseRange& C1,
90 const LowerSwitch::CaseRange& C2) {
92 const ConstantInt* CI1 = cast<const ConstantInt>(C1.Low);
93 const ConstantInt* CI2 = cast<const ConstantInt>(C2.High);
94 return CI1->getValue().slt(CI2->getValue());
99 char LowerSwitch::ID = 0;
100 INITIALIZE_PASS(LowerSwitch, "lowerswitch",
101 "Lower SwitchInst's to branches", false, false)
103 // Publicly exposed interface to pass...
104 char &llvm::LowerSwitchID = LowerSwitch::ID;
105 // createLowerSwitchPass - Interface to this file...
106 FunctionPass *llvm::createLowerSwitchPass() {
107 return new LowerSwitch();
110 bool LowerSwitch::runOnFunction(Function &F) {
111 bool Changed = false;
112 SmallPtrSet<BasicBlock*, 8> DeleteList;
114 for (Function::iterator I = F.begin(), E = F.end(); I != E; ) {
115 BasicBlock *Cur = &*I++; // Advance over block so we don't traverse new blocks
117 // If the block is a dead Default block that will be deleted later, don't
118 // waste time processing it.
119 if (DeleteList.count(Cur))
120 continue;
122 if (SwitchInst *SI = dyn_cast<SwitchInst>(Cur->getTerminator())) {
123 Changed = true;
124 processSwitchInst(SI, DeleteList);
128 for (BasicBlock* BB: DeleteList) {
129 DeleteDeadBlock(BB);
132 return Changed;
135 /// Used for debugging purposes.
136 static raw_ostream& operator<<(raw_ostream &O,
137 const LowerSwitch::CaseVector &C)
138 LLVM_ATTRIBUTE_USED;
139 static raw_ostream& operator<<(raw_ostream &O,
140 const LowerSwitch::CaseVector &C) {
141 O << "[";
143 for (LowerSwitch::CaseVector::const_iterator B = C.begin(),
144 E = C.end(); B != E; ) {
145 O << *B->Low << " -" << *B->High;
146 if (++B != E) O << ", ";
149 return O << "]";
152 /// \brief Update the first occurrence of the "switch statement" BB in the PHI
153 /// node with the "new" BB. The other occurrences will:
155 /// 1) Be updated by subsequent calls to this function. Switch statements may
156 /// have more than one outcoming edge into the same BB if they all have the same
157 /// value. When the switch statement is converted these incoming edges are now
158 /// coming from multiple BBs.
159 /// 2) Removed if subsequent incoming values now share the same case, i.e.,
160 /// multiple outcome edges are condensed into one. This is necessary to keep the
161 /// number of phi values equal to the number of branches to SuccBB.
162 static void fixPhis(BasicBlock *SuccBB, BasicBlock *OrigBB, BasicBlock *NewBB,
163 unsigned NumMergedCases) {
164 for (BasicBlock::iterator I = SuccBB->begin(),
165 IE = SuccBB->getFirstNonPHI()->getIterator();
166 I != IE; ++I) {
167 PHINode *PN = cast<PHINode>(I);
169 // Only update the first occurrence.
170 unsigned Idx = 0, E = PN->getNumIncomingValues();
171 unsigned LocalNumMergedCases = NumMergedCases;
172 for (; Idx != E; ++Idx) {
173 if (PN->getIncomingBlock(Idx) == OrigBB) {
174 PN->setIncomingBlock(Idx, NewBB);
175 break;
179 // Remove additional occurrences coming from condensed cases and keep the
180 // number of incoming values equal to the number of branches to SuccBB.
181 SmallVector<unsigned, 8> Indices;
182 for (++Idx; LocalNumMergedCases > 0 && Idx < E; ++Idx)
183 if (PN->getIncomingBlock(Idx) == OrigBB) {
184 Indices.push_back(Idx);
185 LocalNumMergedCases--;
187 // Remove incoming values in the reverse order to prevent invalidating
188 // *successive* index.
189 for (unsigned III : reverse(Indices))
190 PN->removeIncomingValue(III);
194 /// Convert the switch statement into a binary lookup of the case values.
195 /// The function recursively builds this tree. LowerBound and UpperBound are
196 /// used to keep track of the bounds for Val that have already been checked by
197 /// a block emitted by one of the previous calls to switchConvert in the call
198 /// stack.
199 BasicBlock *
200 LowerSwitch::switchConvert(CaseItr Begin, CaseItr End, ConstantInt *LowerBound,
201 ConstantInt *UpperBound, Value *Val,
202 BasicBlock *Predecessor, BasicBlock *OrigBlock,
203 BasicBlock *Default,
204 const std::vector<IntRange> &UnreachableRanges) {
205 unsigned Size = End - Begin;
207 if (Size == 1) {
208 // Check if the Case Range is perfectly squeezed in between
209 // already checked Upper and Lower bounds. If it is then we can avoid
210 // emitting the code that checks if the value actually falls in the range
211 // because the bounds already tell us so.
212 if (Begin->Low == LowerBound && Begin->High == UpperBound) {
213 unsigned NumMergedCases = 0;
214 if (LowerBound && UpperBound)
215 NumMergedCases =
216 UpperBound->getSExtValue() - LowerBound->getSExtValue();
217 fixPhis(Begin->BB, OrigBlock, Predecessor, NumMergedCases);
218 return Begin->BB;
220 return newLeafBlock(*Begin, Val, OrigBlock, Default);
223 unsigned Mid = Size / 2;
224 std::vector<CaseRange> LHS(Begin, Begin + Mid);
225 DEBUG(dbgs() << "LHS: " << LHS << "\n");
226 std::vector<CaseRange> RHS(Begin + Mid, End);
227 DEBUG(dbgs() << "RHS: " << RHS << "\n");
229 CaseRange &Pivot = *(Begin + Mid);
230 DEBUG(dbgs() << "Pivot ==> "
231 << Pivot.Low->getValue()
232 << " -" << Pivot.High->getValue() << "\n");
234 // NewLowerBound here should never be the integer minimal value.
235 // This is because it is computed from a case range that is never
236 // the smallest, so there is always a case range that has at least
237 // a smaller value.
238 ConstantInt *NewLowerBound = Pivot.Low;
240 // Because NewLowerBound is never the smallest representable integer
241 // it is safe here to subtract one.
242 ConstantInt *NewUpperBound = ConstantInt::get(NewLowerBound->getContext(),
243 NewLowerBound->getValue() - 1);
245 if (!UnreachableRanges.empty()) {
246 // Check if the gap between LHS's highest and NewLowerBound is unreachable.
247 int64_t GapLow = LHS.back().High->getSExtValue() + 1;
248 int64_t GapHigh = NewLowerBound->getSExtValue() - 1;
249 IntRange Gap = { GapLow, GapHigh };
250 if (GapHigh >= GapLow && IsInRanges(Gap, UnreachableRanges))
251 NewUpperBound = LHS.back().High;
254 DEBUG(dbgs() << "LHS Bounds ==> ";
255 if (LowerBound) {
256 dbgs() << LowerBound->getSExtValue();
257 } else {
258 dbgs() << "NONE";
260 dbgs() << " - " << NewUpperBound->getSExtValue() << "\n";
261 dbgs() << "RHS Bounds ==> ";
262 dbgs() << NewLowerBound->getSExtValue() << " - ";
263 if (UpperBound) {
264 dbgs() << UpperBound->getSExtValue() << "\n";
265 } else {
266 dbgs() << "NONE\n";
269 // Create a new node that checks if the value is < pivot. Go to the
270 // left branch if it is and right branch if not.
271 Function* F = OrigBlock->getParent();
272 BasicBlock* NewNode = BasicBlock::Create(Val->getContext(), "NodeBlock");
274 ICmpInst* Comp = new ICmpInst(ICmpInst::ICMP_SLT,
275 Val, Pivot.Low, "Pivot");
277 BasicBlock *LBranch = switchConvert(LHS.begin(), LHS.end(), LowerBound,
278 NewUpperBound, Val, NewNode, OrigBlock,
279 Default, UnreachableRanges);
280 BasicBlock *RBranch = switchConvert(RHS.begin(), RHS.end(), NewLowerBound,
281 UpperBound, Val, NewNode, OrigBlock,
282 Default, UnreachableRanges);
284 F->getBasicBlockList().insert(++OrigBlock->getIterator(), NewNode);
285 NewNode->getInstList().push_back(Comp);
287 BranchInst::Create(LBranch, RBranch, Comp, NewNode);
288 return NewNode;
291 /// Create a new leaf block for the binary lookup tree. It checks if the
292 /// switch's value == the case's value. If not, then it jumps to the default
293 /// branch. At this point in the tree, the value can't be another valid case
294 /// value, so the jump to the "default" branch is warranted.
295 BasicBlock* LowerSwitch::newLeafBlock(CaseRange& Leaf, Value* Val,
296 BasicBlock* OrigBlock,
297 BasicBlock* Default)
299 Function* F = OrigBlock->getParent();
300 BasicBlock* NewLeaf = BasicBlock::Create(Val->getContext(), "LeafBlock");
301 F->getBasicBlockList().insert(++OrigBlock->getIterator(), NewLeaf);
303 // Emit comparison
304 ICmpInst* Comp = nullptr;
305 if (Leaf.Low == Leaf.High) {
306 // Make the seteq instruction...
307 Comp = new ICmpInst(*NewLeaf, ICmpInst::ICMP_EQ, Val,
308 Leaf.Low, "SwitchLeaf");
309 } else {
310 // Make range comparison
311 if (Leaf.Low->isMinValue(true /*isSigned*/)) {
312 // Val >= Min && Val <= Hi --> Val <= Hi
313 Comp = new ICmpInst(*NewLeaf, ICmpInst::ICMP_SLE, Val, Leaf.High,
314 "SwitchLeaf");
315 } else if (Leaf.Low->isZero()) {
316 // Val >= 0 && Val <= Hi --> Val <=u Hi
317 Comp = new ICmpInst(*NewLeaf, ICmpInst::ICMP_ULE, Val, Leaf.High,
318 "SwitchLeaf");
319 } else {
320 // Emit V-Lo <=u Hi-Lo
321 Constant* NegLo = ConstantExpr::getNeg(Leaf.Low);
322 Instruction* Add = BinaryOperator::CreateAdd(Val, NegLo,
323 Val->getName()+".off",
324 NewLeaf);
325 Constant *UpperBound = ConstantExpr::getAdd(NegLo, Leaf.High);
326 Comp = new ICmpInst(*NewLeaf, ICmpInst::ICMP_ULE, Add, UpperBound,
327 "SwitchLeaf");
331 // Make the conditional branch...
332 BasicBlock* Succ = Leaf.BB;
333 BranchInst::Create(Succ, Default, Comp, NewLeaf);
335 // If there were any PHI nodes in this successor, rewrite one entry
336 // from OrigBlock to come from NewLeaf.
337 for (BasicBlock::iterator I = Succ->begin(); isa<PHINode>(I); ++I) {
338 PHINode* PN = cast<PHINode>(I);
339 // Remove all but one incoming entries from the cluster
340 uint64_t Range = Leaf.High->getSExtValue() -
341 Leaf.Low->getSExtValue();
342 for (uint64_t j = 0; j < Range; ++j) {
343 PN->removeIncomingValue(OrigBlock);
346 int BlockIdx = PN->getBasicBlockIndex(OrigBlock);
347 assert(BlockIdx != -1 && "Switch didn't go to this successor??");
348 PN->setIncomingBlock((unsigned)BlockIdx, NewLeaf);
351 return NewLeaf;
354 /// Transform simple list of Cases into list of CaseRange's.
355 unsigned LowerSwitch::Clusterify(CaseVector& Cases, SwitchInst *SI) {
356 unsigned numCmps = 0;
358 // Start with "simple" cases
359 for (auto Case : SI->cases())
360 Cases.push_back(CaseRange(Case.getCaseValue(), Case.getCaseValue(),
361 Case.getCaseSuccessor()));
363 std::sort(Cases.begin(), Cases.end(), CaseCmp());
365 // Merge case into clusters
366 if (Cases.size() >= 2) {
367 CaseItr I = Cases.begin();
368 for (CaseItr J = std::next(I), E = Cases.end(); J != E; ++J) {
369 int64_t nextValue = J->Low->getSExtValue();
370 int64_t currentValue = I->High->getSExtValue();
371 BasicBlock* nextBB = J->BB;
372 BasicBlock* currentBB = I->BB;
374 // If the two neighboring cases go to the same destination, merge them
375 // into a single case.
376 assert(nextValue > currentValue && "Cases should be strictly ascending");
377 if ((nextValue == currentValue + 1) && (currentBB == nextBB)) {
378 I->High = J->High;
379 // FIXME: Combine branch weights.
380 } else if (++I != J) {
381 *I = *J;
384 Cases.erase(std::next(I), Cases.end());
387 for (CaseItr I=Cases.begin(), E=Cases.end(); I!=E; ++I, ++numCmps) {
388 if (I->Low != I->High)
389 // A range counts double, since it requires two compares.
390 ++numCmps;
393 return numCmps;
396 /// Replace the specified switch instruction with a sequence of chained if-then
397 /// insts in a balanced binary search.
398 void LowerSwitch::processSwitchInst(SwitchInst *SI,
399 SmallPtrSetImpl<BasicBlock*> &DeleteList) {
400 BasicBlock *CurBlock = SI->getParent();
401 BasicBlock *OrigBlock = CurBlock;
402 Function *F = CurBlock->getParent();
403 Value *Val = SI->getCondition(); // The value we are switching on...
404 BasicBlock* Default = SI->getDefaultDest();
406 // Don't handle unreachable blocks. If there are successors with phis, this
407 // would leave them behind with missing predecessors.
408 if ((CurBlock != &F->getEntryBlock() && pred_empty(CurBlock)) ||
409 CurBlock->getSinglePredecessor() == CurBlock) {
410 DeleteList.insert(CurBlock);
411 return;
414 // If there is only the default destination, just branch.
415 if (!SI->getNumCases()) {
416 BranchInst::Create(Default, CurBlock);
417 SI->eraseFromParent();
418 return;
421 // Prepare cases vector.
422 CaseVector Cases;
423 unsigned numCmps = Clusterify(Cases, SI);
424 DEBUG(dbgs() << "Clusterify finished. Total clusters: " << Cases.size()
425 << ". Total compares: " << numCmps << "\n");
426 DEBUG(dbgs() << "Cases: " << Cases << "\n");
427 (void)numCmps;
429 ConstantInt *LowerBound = nullptr;
430 ConstantInt *UpperBound = nullptr;
431 std::vector<IntRange> UnreachableRanges;
433 if (isa<UnreachableInst>(Default->getFirstNonPHIOrDbg())) {
434 // Make the bounds tightly fitted around the case value range, because we
435 // know that the value passed to the switch must be exactly one of the case
436 // values.
437 assert(!Cases.empty());
438 LowerBound = Cases.front().Low;
439 UpperBound = Cases.back().High;
441 DenseMap<BasicBlock *, unsigned> Popularity;
442 unsigned MaxPop = 0;
443 BasicBlock *PopSucc = nullptr;
445 IntRange R = { INT64_MIN, INT64_MAX };
446 UnreachableRanges.push_back(R);
447 for (const auto &I : Cases) {
448 int64_t Low = I.Low->getSExtValue();
449 int64_t High = I.High->getSExtValue();
451 IntRange &LastRange = UnreachableRanges.back();
452 if (LastRange.Low == Low) {
453 // There is nothing left of the previous range.
454 UnreachableRanges.pop_back();
455 } else {
456 // Terminate the previous range.
457 assert(Low > LastRange.Low);
458 LastRange.High = Low - 1;
460 if (High != INT64_MAX) {
461 IntRange R = { High + 1, INT64_MAX };
462 UnreachableRanges.push_back(R);
465 // Count popularity.
466 int64_t N = High - Low + 1;
467 unsigned &Pop = Popularity[I.BB];
468 if ((Pop += N) > MaxPop) {
469 MaxPop = Pop;
470 PopSucc = I.BB;
473 #ifndef NDEBUG
474 /* UnreachableRanges should be sorted and the ranges non-adjacent. */
475 for (auto I = UnreachableRanges.begin(), E = UnreachableRanges.end();
476 I != E; ++I) {
477 assert(I->Low <= I->High);
478 auto Next = I + 1;
479 if (Next != E) {
480 assert(Next->Low > I->High);
483 #endif
485 // Use the most popular block as the new default, reducing the number of
486 // cases.
487 assert(MaxPop > 0 && PopSucc);
488 Default = PopSucc;
489 Cases.erase(
490 remove_if(Cases,
491 [PopSucc](const CaseRange &R) { return R.BB == PopSucc; }),
492 Cases.end());
494 // If there are no cases left, just branch.
495 if (Cases.empty()) {
496 BranchInst::Create(Default, CurBlock);
497 SI->eraseFromParent();
498 return;
502 // Create a new, empty default block so that the new hierarchy of
503 // if-then statements go to this and the PHI nodes are happy.
504 BasicBlock *NewDefault = BasicBlock::Create(SI->getContext(), "NewDefault");
505 F->getBasicBlockList().insert(Default->getIterator(), NewDefault);
506 BranchInst::Create(Default, NewDefault);
508 // If there is an entry in any PHI nodes for the default edge, make sure
509 // to update them as well.
510 for (BasicBlock::iterator I = Default->begin(); isa<PHINode>(I); ++I) {
511 PHINode *PN = cast<PHINode>(I);
512 int BlockIdx = PN->getBasicBlockIndex(OrigBlock);
513 assert(BlockIdx != -1 && "Switch didn't go to this successor??");
514 PN->setIncomingBlock((unsigned)BlockIdx, NewDefault);
517 BasicBlock *SwitchBlock =
518 switchConvert(Cases.begin(), Cases.end(), LowerBound, UpperBound, Val,
519 OrigBlock, OrigBlock, NewDefault, UnreachableRanges);
521 // Branch to our shiny new if-then stuff...
522 BranchInst::Create(SwitchBlock, OrigBlock);
524 // We are now done with the switch instruction, delete it.
525 BasicBlock *OldDefault = SI->getDefaultDest();
526 CurBlock->getInstList().erase(SI);
528 // If the Default block has no more predecessors just add it to DeleteList.
529 if (pred_begin(OldDefault) == pred_end(OldDefault))
530 DeleteList.insert(OldDefault);