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.
13 //
14 //===----------------------------------------------------------------------===//
15
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"
29 #include <algorithm>
30 using namespace llvm;
31
32 #define DEBUG_TYPE "lower-switch"
33
34 namespace {
35 struct IntRange {
36 int64_t Low, High;
37 };
38 // Return true iff R is covered by Ranges.
IsInRanges(const IntRange & R,const std::vector<IntRange> & 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.
42
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;
50 }
51
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
LowerSwitch()56 LowerSwitch() : FunctionPass(ID) {
57 initializeLowerSwitchPass(*PassRegistry::getPassRegistry());
58 }
59
60 bool runOnFunction(Function &F) override;
61
62 struct CaseRange {
63 ConstantInt* Low;
64 ConstantInt* High;
65 BasicBlock* BB;
66
CaseRange__anona881aacb0111::LowerSwitch::CaseRange67 CaseRange(ConstantInt *low, ConstantInt *high, BasicBlock *bb)
68 : Low(low), High(high), BB(bb) {}
69 };
70
71 typedef std::vector<CaseRange> CaseVector;
72 typedef std::vector<CaseRange>::iterator CaseItr;
73 private:
74 void processSwitchInst(SwitchInst *SI, SmallPtrSetImpl<BasicBlock*> &DeleteList);
75
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);
84 };
85
86 /// The comparison function for sorting the switch case values in the vector.
87 /// WARNING: Case ranges should be disjoint!
88 struct CaseCmp {
operator ()__anona881aacb0111::CaseCmp89 bool operator () (const LowerSwitch::CaseRange& C1,
90 const LowerSwitch::CaseRange& C2) {
91
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());
95 }
96 };
97 }
98
99 char LowerSwitch::ID = 0;
100 INITIALIZE_PASS(LowerSwitch, "lowerswitch",
101 "Lower SwitchInst's to branches", false, false)
102
103 // Publicly exposed interface to pass...
104 char &llvm::LowerSwitchID = LowerSwitch::ID;
105 // createLowerSwitchPass - Interface to this file...
createLowerSwitchPass()106 FunctionPass *llvm::createLowerSwitchPass() {
107 return new LowerSwitch();
108 }
109
runOnFunction(Function & F)110 bool LowerSwitch::runOnFunction(Function &F) {
111 bool Changed = false;
112 SmallPtrSet<BasicBlock*, 8> DeleteList;
113
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
116
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;
121
122 if (SwitchInst *SI = dyn_cast<SwitchInst>(Cur->getTerminator())) {
123 Changed = true;
124 processSwitchInst(SI, DeleteList);
125 }
126 }
127
128 for (BasicBlock* BB: DeleteList) {
129 DeleteDeadBlock(BB);
130 }
131
132 return Changed;
133 }
134
135 /// Used for debugging purposes.
136 static raw_ostream& operator<<(raw_ostream &O,
137 const LowerSwitch::CaseVector &C)
138 LLVM_ATTRIBUTE_USED;
operator <<(raw_ostream & O,const LowerSwitch::CaseVector & C)139 static raw_ostream& operator<<(raw_ostream &O,
140 const LowerSwitch::CaseVector &C) {
141 O << "[";
142
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 << ", ";
147 }
148
149 return O << "]";
150 }
151
152 /// \brief Update the first occurrence of the "switch statement" BB in the PHI
153 /// node with the "new" BB. The other occurrences will:
154 ///
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.
fixPhis(BasicBlock * SuccBB,BasicBlock * OrigBB,BasicBlock * NewBB,unsigned NumMergedCases)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);
168
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;
176 }
177 }
178
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--;
186 }
187 // Remove incoming values in the reverse order to prevent invalidating
188 // *successive* index.
189 for (unsigned III : reverse(Indices))
190 PN->removeIncomingValue(III);
191 }
192 }
193
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 *
switchConvert(CaseItr Begin,CaseItr End,ConstantInt * LowerBound,ConstantInt * UpperBound,Value * Val,BasicBlock * Predecessor,BasicBlock * OrigBlock,BasicBlock * Default,const std::vector<IntRange> & UnreachableRanges)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;
206
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;
219 }
220 return newLeafBlock(*Begin, Val, OrigBlock, Default);
221 }
222
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");
228
229 CaseRange &Pivot = *(Begin + Mid);
230 DEBUG(dbgs() << "Pivot ==> "
231 << Pivot.Low->getValue()
232 << " -" << Pivot.High->getValue() << "\n");
233
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;
239
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);
244
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;
252 }
253
254 DEBUG(dbgs() << "LHS Bounds ==> ";
255 if (LowerBound) {
256 dbgs() << LowerBound->getSExtValue();
257 } else {
258 dbgs() << "NONE";
259 }
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";
267 });
268
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");
273
274 ICmpInst* Comp = new ICmpInst(ICmpInst::ICMP_SLT,
275 Val, Pivot.Low, "Pivot");
276
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);
283
284 F->getBasicBlockList().insert(++OrigBlock->getIterator(), NewNode);
285 NewNode->getInstList().push_back(Comp);
286
287 BranchInst::Create(LBranch, RBranch, Comp, NewNode);
288 return NewNode;
289 }
290
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.
newLeafBlock(CaseRange & Leaf,Value * Val,BasicBlock * OrigBlock,BasicBlock * Default)295 BasicBlock* LowerSwitch::newLeafBlock(CaseRange& Leaf, Value* Val,
296 BasicBlock* OrigBlock,
297 BasicBlock* Default)
298 {
299 Function* F = OrigBlock->getParent();
300 BasicBlock* NewLeaf = BasicBlock::Create(Val->getContext(), "LeafBlock");
301 F->getBasicBlockList().insert(++OrigBlock->getIterator(), NewLeaf);
302
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");
328 }
329 }
330
331 // Make the conditional branch...
332 BasicBlock* Succ = Leaf.BB;
333 BranchInst::Create(Succ, Default, Comp, NewLeaf);
334
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);
344 }
345
346 int BlockIdx = PN->getBasicBlockIndex(OrigBlock);
347 assert(BlockIdx != -1 && "Switch didn't go to this successor??");
348 PN->setIncomingBlock((unsigned)BlockIdx, NewLeaf);
349 }
350
351 return NewLeaf;
352 }
353
354 /// Transform simple list of Cases into list of CaseRange's.
Clusterify(CaseVector & Cases,SwitchInst * SI)355 unsigned LowerSwitch::Clusterify(CaseVector& Cases, SwitchInst *SI) {
356 unsigned numCmps = 0;
357
358 // Start with "simple" cases
359 for (SwitchInst::CaseIt i = SI->case_begin(), e = SI->case_end(); i != e; ++i)
360 Cases.push_back(CaseRange(i.getCaseValue(), i.getCaseValue(),
361 i.getCaseSuccessor()));
362
363 std::sort(Cases.begin(), Cases.end(), CaseCmp());
364
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;
373
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;
382 }
383 }
384 Cases.erase(std::next(I), Cases.end());
385 }
386
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;
391 }
392
393 return numCmps;
394 }
395
396 /// Replace the specified switch instruction with a sequence of chained if-then
397 /// insts in a balanced binary search.
processSwitchInst(SwitchInst * SI,SmallPtrSetImpl<BasicBlock * > & DeleteList)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();
405
406 // If there is only the default destination, just branch.
407 if (!SI->getNumCases()) {
408 BranchInst::Create(Default, CurBlock);
409 SI->eraseFromParent();
410 return;
411 }
412
413 // Prepare cases vector.
414 CaseVector Cases;
415 unsigned numCmps = Clusterify(Cases, SI);
416 DEBUG(dbgs() << "Clusterify finished. Total clusters: " << Cases.size()
417 << ". Total compares: " << numCmps << "\n");
418 DEBUG(dbgs() << "Cases: " << Cases << "\n");
419 (void)numCmps;
420
421 ConstantInt *LowerBound = nullptr;
422 ConstantInt *UpperBound = nullptr;
423 std::vector<IntRange> UnreachableRanges;
424
425 if (isa<UnreachableInst>(Default->getFirstNonPHIOrDbg())) {
426 // Make the bounds tightly fitted around the case value range, because we
427 // know that the value passed to the switch must be exactly one of the case
428 // values.
429 assert(!Cases.empty());
430 LowerBound = Cases.front().Low;
431 UpperBound = Cases.back().High;
432
433 DenseMap<BasicBlock *, unsigned> Popularity;
434 unsigned MaxPop = 0;
435 BasicBlock *PopSucc = nullptr;
436
437 IntRange R = { INT64_MIN, INT64_MAX };
438 UnreachableRanges.push_back(R);
439 for (const auto &I : Cases) {
440 int64_t Low = I.Low->getSExtValue();
441 int64_t High = I.High->getSExtValue();
442
443 IntRange &LastRange = UnreachableRanges.back();
444 if (LastRange.Low == Low) {
445 // There is nothing left of the previous range.
446 UnreachableRanges.pop_back();
447 } else {
448 // Terminate the previous range.
449 assert(Low > LastRange.Low);
450 LastRange.High = Low - 1;
451 }
452 if (High != INT64_MAX) {
453 IntRange R = { High + 1, INT64_MAX };
454 UnreachableRanges.push_back(R);
455 }
456
457 // Count popularity.
458 int64_t N = High - Low + 1;
459 unsigned &Pop = Popularity[I.BB];
460 if ((Pop += N) > MaxPop) {
461 MaxPop = Pop;
462 PopSucc = I.BB;
463 }
464 }
465 #ifndef NDEBUG
466 /* UnreachableRanges should be sorted and the ranges non-adjacent. */
467 for (auto I = UnreachableRanges.begin(), E = UnreachableRanges.end();
468 I != E; ++I) {
469 assert(I->Low <= I->High);
470 auto Next = I + 1;
471 if (Next != E) {
472 assert(Next->Low > I->High);
473 }
474 }
475 #endif
476
477 // Use the most popular block as the new default, reducing the number of
478 // cases.
479 assert(MaxPop > 0 && PopSucc);
480 Default = PopSucc;
481 Cases.erase(std::remove_if(
482 Cases.begin(), Cases.end(),
483 [PopSucc](const CaseRange &R) { return R.BB == PopSucc; }),
484 Cases.end());
485
486 // If there are no cases left, just branch.
487 if (Cases.empty()) {
488 BranchInst::Create(Default, CurBlock);
489 SI->eraseFromParent();
490 return;
491 }
492 }
493
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->getIterator(), NewDefault);
498 BranchInst::Create(Default, NewDefault);
499
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);
507 }
508
509 BasicBlock *SwitchBlock =
510 switchConvert(Cases.begin(), Cases.end(), LowerBound, UpperBound, Val,
511 OrigBlock, OrigBlock, NewDefault, UnreachableRanges);
512
513 // Branch to our shiny new if-then stuff...
514 BranchInst::Create(SwitchBlock, OrigBlock);
515
516 // We are now done with the switch instruction, delete it.
517 BasicBlock *OldDefault = SI->getDefaultDest();
518 CurBlock->getInstList().erase(SI);
519
520 // If the Default block has no more predecessors just add it to DeleteList.
521 if (pred_begin(OldDefault) == pred_end(OldDefault))
522 DeleteList.insert(OldDefault);
523 }
524