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/Transforms/Utils/BasicBlockUtils.h"
18#include "llvm/ADT/STLExtras.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/IR/CFG.h"
24#include "llvm/Pass.h"
25#include "llvm/Support/Compiler.h"
26#include "llvm/Support/Debug.h"
27#include "llvm/Support/raw_ostream.h"
28#include "llvm/Transforms/Utils/UnifyFunctionExitNodes.h"
29#include <algorithm>
30using namespace llvm;
31
32#define DEBUG_TYPE "lower-switch"
33
34namespace {
35  /// LowerSwitch Pass - Replace all SwitchInst instructions with chained branch
36  /// instructions.
37  class LowerSwitch : public FunctionPass {
38  public:
39    static char ID; // Pass identification, replacement for typeid
40    LowerSwitch() : FunctionPass(ID) {
41      initializeLowerSwitchPass(*PassRegistry::getPassRegistry());
42    }
43
44    bool runOnFunction(Function &F) override;
45
46    void getAnalysisUsage(AnalysisUsage &AU) const override {
47      // This is a cluster of orthogonal Transforms
48      AU.addPreserved<UnifyFunctionExitNodes>();
49      AU.addPreserved("mem2reg");
50      AU.addPreservedID(LowerInvokePassID);
51    }
52
53    struct CaseRange {
54      Constant* Low;
55      Constant* High;
56      BasicBlock* BB;
57
58      CaseRange(Constant *low = nullptr, Constant *high = nullptr,
59                BasicBlock *bb = nullptr) :
60        Low(low), High(high), BB(bb) { }
61    };
62
63    typedef std::vector<CaseRange> CaseVector;
64    typedef std::vector<CaseRange>::iterator CaseItr;
65  private:
66    void processSwitchInst(SwitchInst *SI);
67
68    BasicBlock *switchConvert(CaseItr Begin, CaseItr End,
69                              ConstantInt *LowerBound, ConstantInt *UpperBound,
70                              Value *Val, BasicBlock *OrigBlock,
71                              BasicBlock *Default);
72    BasicBlock *newLeafBlock(CaseRange &Leaf, Value *Val, BasicBlock *OrigBlock,
73                             BasicBlock *Default);
74    unsigned Clusterify(CaseVector &Cases, SwitchInst *SI);
75  };
76
77  /// The comparison function for sorting the switch case values in the vector.
78  /// WARNING: Case ranges should be disjoint!
79  struct CaseCmp {
80    bool operator () (const LowerSwitch::CaseRange& C1,
81                      const LowerSwitch::CaseRange& C2) {
82
83      const ConstantInt* CI1 = cast<const ConstantInt>(C1.Low);
84      const ConstantInt* CI2 = cast<const ConstantInt>(C2.High);
85      return CI1->getValue().slt(CI2->getValue());
86    }
87  };
88}
89
90char LowerSwitch::ID = 0;
91INITIALIZE_PASS(LowerSwitch, "lowerswitch",
92                "Lower SwitchInst's to branches", false, false)
93
94// Publicly exposed interface to pass...
95char &llvm::LowerSwitchID = LowerSwitch::ID;
96// createLowerSwitchPass - Interface to this file...
97FunctionPass *llvm::createLowerSwitchPass() {
98  return new LowerSwitch();
99}
100
101bool LowerSwitch::runOnFunction(Function &F) {
102  bool Changed = false;
103
104  for (Function::iterator I = F.begin(), E = F.end(); I != E; ) {
105    BasicBlock *Cur = I++; // Advance over block so we don't traverse new blocks
106
107    if (SwitchInst *SI = dyn_cast<SwitchInst>(Cur->getTerminator())) {
108      Changed = true;
109      processSwitchInst(SI);
110    }
111  }
112
113  return Changed;
114}
115
116// operator<< - Used for debugging purposes.
117//
118static raw_ostream& operator<<(raw_ostream &O,
119                               const LowerSwitch::CaseVector &C)
120    LLVM_ATTRIBUTE_USED;
121static raw_ostream& operator<<(raw_ostream &O,
122                               const LowerSwitch::CaseVector &C) {
123  O << "[";
124
125  for (LowerSwitch::CaseVector::const_iterator B = C.begin(),
126         E = C.end(); B != E; ) {
127    O << *B->Low << " -" << *B->High;
128    if (++B != E) O << ", ";
129  }
130
131  return O << "]";
132}
133
134// switchConvert - Convert the switch statement into a binary lookup of
135// the case values. The function recursively builds this tree.
136// LowerBound and UpperBound are used to keep track of the bounds for Val
137// that have already been checked by a block emitted by one of the previous
138// calls to switchConvert in the call stack.
139BasicBlock *LowerSwitch::switchConvert(CaseItr Begin, CaseItr End,
140                                       ConstantInt *LowerBound,
141                                       ConstantInt *UpperBound, Value *Val,
142                                       BasicBlock *OrigBlock,
143                                       BasicBlock *Default) {
144  unsigned Size = End - Begin;
145
146  if (Size == 1) {
147    // Check if the Case Range is perfectly squeezed in between
148    // already checked Upper and Lower bounds. If it is then we can avoid
149    // emitting the code that checks if the value actually falls in the range
150    // because the bounds already tell us so.
151    if (Begin->Low == LowerBound && Begin->High == UpperBound) {
152      return Begin->BB;
153    }
154    return newLeafBlock(*Begin, Val, OrigBlock, Default);
155  }
156
157  unsigned Mid = Size / 2;
158  std::vector<CaseRange> LHS(Begin, Begin + Mid);
159  DEBUG(dbgs() << "LHS: " << LHS << "\n");
160  std::vector<CaseRange> RHS(Begin + Mid, End);
161  DEBUG(dbgs() << "RHS: " << RHS << "\n");
162
163  CaseRange &Pivot = *(Begin + Mid);
164  DEBUG(dbgs() << "Pivot ==> "
165               << cast<ConstantInt>(Pivot.Low)->getValue()
166               << " -" << cast<ConstantInt>(Pivot.High)->getValue() << "\n");
167
168  // NewLowerBound here should never be the integer minimal value.
169  // This is because it is computed from a case range that is never
170  // the smallest, so there is always a case range that has at least
171  // a smaller value.
172  ConstantInt *NewLowerBound = cast<ConstantInt>(Pivot.Low);
173  ConstantInt *NewUpperBound;
174
175  // If we don't have a Default block then it means that we can never
176  // have a value outside of a case range, so set the UpperBound to the highest
177  // value in the LHS part of the case ranges.
178  if (Default != nullptr) {
179    // Because NewLowerBound is never the smallest representable integer
180    // it is safe here to subtract one.
181    NewUpperBound = ConstantInt::get(NewLowerBound->getContext(),
182                                     NewLowerBound->getValue() - 1);
183  } else {
184    CaseItr LastLHS = LHS.begin() + LHS.size() - 1;
185    NewUpperBound = cast<ConstantInt>(LastLHS->High);
186  }
187
188  DEBUG(dbgs() << "LHS Bounds ==> ";
189        if (LowerBound) {
190          dbgs() << cast<ConstantInt>(LowerBound)->getSExtValue();
191        } else {
192          dbgs() << "NONE";
193        }
194        dbgs() << " - " << NewUpperBound->getSExtValue() << "\n";
195        dbgs() << "RHS Bounds ==> ";
196        dbgs() << NewLowerBound->getSExtValue() << " - ";
197        if (UpperBound) {
198          dbgs() << cast<ConstantInt>(UpperBound)->getSExtValue() << "\n";
199        } else {
200          dbgs() << "NONE\n";
201        });
202
203  BasicBlock *LBranch = switchConvert(LHS.begin(), LHS.end(), LowerBound,
204                                      NewUpperBound, Val, OrigBlock, Default);
205  BasicBlock *RBranch = switchConvert(RHS.begin(), RHS.end(), NewLowerBound,
206                                      UpperBound, Val, OrigBlock, Default);
207
208  // Create a new node that checks if the value is < pivot. Go to the
209  // left branch if it is and right branch if not.
210  Function* F = OrigBlock->getParent();
211  BasicBlock* NewNode = BasicBlock::Create(Val->getContext(), "NodeBlock");
212  Function::iterator FI = OrigBlock;
213  F->getBasicBlockList().insert(++FI, NewNode);
214
215  ICmpInst* Comp = new ICmpInst(ICmpInst::ICMP_SLT,
216                                Val, Pivot.Low, "Pivot");
217  NewNode->getInstList().push_back(Comp);
218  BranchInst::Create(LBranch, RBranch, Comp, NewNode);
219  return NewNode;
220}
221
222// newLeafBlock - Create a new leaf block for the binary lookup tree. It
223// checks if the switch's value == the case's value. If not, then it
224// jumps to the default branch. At this point in the tree, the value
225// can't be another valid case value, so the jump to the "default" branch
226// is warranted.
227//
228BasicBlock* LowerSwitch::newLeafBlock(CaseRange& Leaf, Value* Val,
229                                      BasicBlock* OrigBlock,
230                                      BasicBlock* Default)
231{
232  Function* F = OrigBlock->getParent();
233  BasicBlock* NewLeaf = BasicBlock::Create(Val->getContext(), "LeafBlock");
234  Function::iterator FI = OrigBlock;
235  F->getBasicBlockList().insert(++FI, NewLeaf);
236
237  // Emit comparison
238  ICmpInst* Comp = nullptr;
239  if (Leaf.Low == Leaf.High) {
240    // Make the seteq instruction...
241    Comp = new ICmpInst(*NewLeaf, ICmpInst::ICMP_EQ, Val,
242                        Leaf.Low, "SwitchLeaf");
243  } else {
244    // Make range comparison
245    if (cast<ConstantInt>(Leaf.Low)->isMinValue(true /*isSigned*/)) {
246      // Val >= Min && Val <= Hi --> Val <= Hi
247      Comp = new ICmpInst(*NewLeaf, ICmpInst::ICMP_SLE, Val, Leaf.High,
248                          "SwitchLeaf");
249    } else if (cast<ConstantInt>(Leaf.Low)->isZero()) {
250      // Val >= 0 && Val <= Hi --> Val <=u Hi
251      Comp = new ICmpInst(*NewLeaf, ICmpInst::ICMP_ULE, Val, Leaf.High,
252                          "SwitchLeaf");
253    } else {
254      // Emit V-Lo <=u Hi-Lo
255      Constant* NegLo = ConstantExpr::getNeg(Leaf.Low);
256      Instruction* Add = BinaryOperator::CreateAdd(Val, NegLo,
257                                                   Val->getName()+".off",
258                                                   NewLeaf);
259      Constant *UpperBound = ConstantExpr::getAdd(NegLo, Leaf.High);
260      Comp = new ICmpInst(*NewLeaf, ICmpInst::ICMP_ULE, Add, UpperBound,
261                          "SwitchLeaf");
262    }
263  }
264
265  // Make the conditional branch...
266  BasicBlock* Succ = Leaf.BB;
267  BranchInst::Create(Succ, Default, Comp, NewLeaf);
268
269  // If there were any PHI nodes in this successor, rewrite one entry
270  // from OrigBlock to come from NewLeaf.
271  for (BasicBlock::iterator I = Succ->begin(); isa<PHINode>(I); ++I) {
272    PHINode* PN = cast<PHINode>(I);
273    // Remove all but one incoming entries from the cluster
274    uint64_t Range = cast<ConstantInt>(Leaf.High)->getSExtValue() -
275                     cast<ConstantInt>(Leaf.Low)->getSExtValue();
276    for (uint64_t j = 0; j < Range; ++j) {
277      PN->removeIncomingValue(OrigBlock);
278    }
279
280    int BlockIdx = PN->getBasicBlockIndex(OrigBlock);
281    assert(BlockIdx != -1 && "Switch didn't go to this successor??");
282    PN->setIncomingBlock((unsigned)BlockIdx, NewLeaf);
283  }
284
285  return NewLeaf;
286}
287
288// Clusterify - Transform simple list of Cases into list of CaseRange's
289unsigned LowerSwitch::Clusterify(CaseVector& Cases, SwitchInst *SI) {
290  unsigned numCmps = 0;
291
292  // Start with "simple" cases
293  for (SwitchInst::CaseIt i = SI->case_begin(), e = SI->case_end(); i != e; ++i)
294    Cases.push_back(CaseRange(i.getCaseValue(), i.getCaseValue(),
295                              i.getCaseSuccessor()));
296
297  std::sort(Cases.begin(), Cases.end(), CaseCmp());
298
299  // Merge case into clusters
300  if (Cases.size()>=2)
301    for (CaseItr I = Cases.begin(), J = std::next(Cases.begin());
302         J != Cases.end();) {
303      int64_t nextValue = cast<ConstantInt>(J->Low)->getSExtValue();
304      int64_t currentValue = cast<ConstantInt>(I->High)->getSExtValue();
305      BasicBlock* nextBB = J->BB;
306      BasicBlock* currentBB = I->BB;
307
308      // If the two neighboring cases go to the same destination, merge them
309      // into a single case.
310      if ((nextValue-currentValue==1) && (currentBB == nextBB)) {
311        I->High = J->High;
312        J = Cases.erase(J);
313      } else {
314        I = J++;
315      }
316    }
317
318  for (CaseItr I=Cases.begin(), E=Cases.end(); I!=E; ++I, ++numCmps) {
319    if (I->Low != I->High)
320      // A range counts double, since it requires two compares.
321      ++numCmps;
322  }
323
324  return numCmps;
325}
326
327// processSwitchInst - Replace the specified switch instruction with a sequence
328// of chained if-then insts in a balanced binary search.
329//
330void LowerSwitch::processSwitchInst(SwitchInst *SI) {
331  BasicBlock *CurBlock = SI->getParent();
332  BasicBlock *OrigBlock = CurBlock;
333  Function *F = CurBlock->getParent();
334  Value *Val = SI->getCondition();  // The value we are switching on...
335  BasicBlock* Default = SI->getDefaultDest();
336
337  // If there is only the default destination, don't bother with the code below.
338  if (!SI->getNumCases()) {
339    BranchInst::Create(SI->getDefaultDest(), CurBlock);
340    CurBlock->getInstList().erase(SI);
341    return;
342  }
343
344  const bool DefaultIsUnreachable =
345      Default->size() == 1 && isa<UnreachableInst>(Default->getTerminator());
346  // Create a new, empty default block so that the new hierarchy of
347  // if-then statements go to this and the PHI nodes are happy.
348  // if the default block is set as an unreachable we avoid creating one
349  // because will never be a valid target.
350  BasicBlock *NewDefault = nullptr;
351  if (!DefaultIsUnreachable) {
352    NewDefault = BasicBlock::Create(SI->getContext(), "NewDefault");
353    F->getBasicBlockList().insert(Default, NewDefault);
354
355    BranchInst::Create(Default, NewDefault);
356  }
357  // If there is an entry in any PHI nodes for the default edge, make sure
358  // to update them as well.
359  for (BasicBlock::iterator I = Default->begin(); isa<PHINode>(I); ++I) {
360    PHINode *PN = cast<PHINode>(I);
361    int BlockIdx = PN->getBasicBlockIndex(OrigBlock);
362    assert(BlockIdx != -1 && "Switch didn't go to this successor??");
363    PN->setIncomingBlock((unsigned)BlockIdx, NewDefault);
364  }
365
366  // Prepare cases vector.
367  CaseVector Cases;
368  unsigned numCmps = Clusterify(Cases, SI);
369
370  DEBUG(dbgs() << "Clusterify finished. Total clusters: " << Cases.size()
371               << ". Total compares: " << numCmps << "\n");
372  DEBUG(dbgs() << "Cases: " << Cases << "\n");
373  (void)numCmps;
374
375  ConstantInt *UpperBound = nullptr;
376  ConstantInt *LowerBound = nullptr;
377
378  // Optimize the condition where Default is an unreachable block. In this case
379  // we can make the bounds tightly fitted around the case value ranges,
380  // because we know that the value passed to the switch should always be
381  // exactly one of the case values.
382  if (DefaultIsUnreachable) {
383    CaseItr LastCase = Cases.begin() + Cases.size() - 1;
384    UpperBound = cast<ConstantInt>(LastCase->High);
385    LowerBound = cast<ConstantInt>(Cases.begin()->Low);
386  }
387  BasicBlock *SwitchBlock =
388      switchConvert(Cases.begin(), Cases.end(), LowerBound, UpperBound, Val,
389                    OrigBlock, NewDefault);
390
391  // Branch to our shiny new if-then stuff...
392  BranchInst::Create(SwitchBlock, OrigBlock);
393
394  // We are now done with the switch instruction, delete it.
395  CurBlock->getInstList().erase(SI);
396
397  pred_iterator PI = pred_begin(Default), E = pred_end(Default);
398  // If the Default block has no more predecessors just remove it
399  if (PI == E) {
400    DeleteDeadBlock(Default);
401  }
402}
403