blob: 4b9d0dadfc1733df4662948265ae4dd642d376a6 [file] [log] [blame]
Chris Lattner1b094a02003-04-23 16:23:59 +00001//===- LowerSwitch.cpp - Eliminate Switch instructions --------------------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Misha Brukmanb1c93172005-04-21 23:48:37 +00006//
John Criswell482202a2003-10-20 19:43:21 +00007//===----------------------------------------------------------------------===//
Chris Lattner1b094a02003-04-23 16:23:59 +00008//
Gordon Henriksend5687672007-11-04 16:15:04 +00009// The LowerSwitch transformation rewrites switch instructions with a sequence
10// of branches, which allows targets to get away with not implementing the
11// switch instruction until it is convenient.
Chris Lattner1b094a02003-04-23 16:23:59 +000012//
13//===----------------------------------------------------------------------===//
14
Eugene Zelenkofce43572017-10-21 00:57:46 +000015#include "llvm/ADT/DenseMap.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000016#include "llvm/ADT/STLExtras.h"
Eugene Zelenkofce43572017-10-21 00:57:46 +000017#include "llvm/ADT/SmallPtrSet.h"
18#include "llvm/ADT/SmallVector.h"
Roman Tereshin99a66722019-02-22 14:33:46 +000019#include "llvm/Analysis/AssumptionCache.h"
20#include "llvm/Analysis/LazyValueInfo.h"
21#include "llvm/Analysis/ValueTracking.h"
Eugene Zelenkofce43572017-10-21 00:57:46 +000022#include "llvm/IR/BasicBlock.h"
Benjamin Kramer799003b2015-03-23 19:32:43 +000023#include "llvm/IR/CFG.h"
Roman Tereshin99a66722019-02-22 14:33:46 +000024#include "llvm/IR/ConstantRange.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000025#include "llvm/IR/Constants.h"
26#include "llvm/IR/Function.h"
Eugene Zelenkofce43572017-10-21 00:57:46 +000027#include "llvm/IR/InstrTypes.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000028#include "llvm/IR/Instructions.h"
Eugene Zelenkofce43572017-10-21 00:57:46 +000029#include "llvm/IR/Value.h"
Reid Kleckner05da2fe2019-11-13 13:15:01 -080030#include "llvm/InitializePasses.h"
Chris Lattner1b094a02003-04-23 16:23:59 +000031#include "llvm/Pass.h"
Eugene Zelenkofce43572017-10-21 00:57:46 +000032#include "llvm/Support/Casting.h"
Chris Lattner3d27be12006-08-27 12:54:02 +000033#include "llvm/Support/Compiler.h"
Nick Lewycky974e12b2009-10-25 06:57:41 +000034#include "llvm/Support/Debug.h"
Roman Tereshin99a66722019-02-22 14:33:46 +000035#include "llvm/Support/KnownBits.h"
Chris Lattner0c19df42008-08-23 22:23:09 +000036#include "llvm/Support/raw_ostream.h"
David Blaikiea373d182018-03-28 17:44:36 +000037#include "llvm/Transforms/Utils.h"
Benjamin Kramer799003b2015-03-23 19:32:43 +000038#include "llvm/Transforms/Utils/BasicBlockUtils.h"
Alkis Evlogimenosa5c04ee2004-09-03 18:19:51 +000039#include <algorithm>
Eugene Zelenkofce43572017-10-21 00:57:46 +000040#include <cassert>
41#include <cstdint>
42#include <iterator>
43#include <limits>
44#include <vector>
45
Chris Lattner49525f82004-01-09 06:02:20 +000046using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000047
Chandler Carruthe96dd892014-04-21 22:55:11 +000048#define DEBUG_TYPE "lower-switch"
49
Chris Lattner1b094a02003-04-23 16:23:59 +000050namespace {
Eugene Zelenkofce43572017-10-21 00:57:46 +000051
Hans Wennborgae9c9712015-01-23 20:43:51 +000052 struct IntRange {
53 int64_t Low, High;
54 };
Hans Wennborgae9c9712015-01-23 20:43:51 +000055
Eugene Zelenkofce43572017-10-21 00:57:46 +000056} // end anonymous namespace
57
58// Return true iff R is covered by Ranges.
59static bool IsInRanges(const IntRange &R,
60 const std::vector<IntRange> &Ranges) {
61 // Note: Ranges must be sorted, non-overlapping and non-adjacent.
62
63 // Find the first range whose High field is >= R.High,
64 // then check if the Low field is <= R.Low. If so, we
65 // have a Range that covers R.
Fangrui Songcecc4352019-04-12 02:02:06 +000066 auto I = llvm::lower_bound(
67 Ranges, R, [](IntRange A, IntRange B) { return A.High < B.High; });
Eugene Zelenkofce43572017-10-21 00:57:46 +000068 return I != Ranges.end() && I->Low <= R.Low;
69}
70
71namespace {
Hans Wennborgae9c9712015-01-23 20:43:51 +000072
Sanjay Patel815adac2015-09-16 16:21:08 +000073 /// Replace all SwitchInst instructions with chained branch instructions.
Nick Lewycky02d5f772009-10-25 06:33:48 +000074 class LowerSwitch : public FunctionPass {
Chris Lattnered922162003-10-07 18:46:23 +000075 public:
Eugene Zelenkofce43572017-10-21 00:57:46 +000076 // Pass identification, replacement for typeid
77 static char ID;
78
Owen Anderson6c18d1a2010-10-19 17:21:58 +000079 LowerSwitch() : FunctionPass(ID) {
80 initializeLowerSwitchPass(*PassRegistry::getPassRegistry());
Karl-Johan Karlsson1ffeb5d2018-07-10 12:06:16 +000081 }
Devang Patel09f162c2007-05-01 21:15:47 +000082
Craig Topper3e4c6972014-03-05 09:10:37 +000083 bool runOnFunction(Function &F) override;
84
Roman Tereshin99a66722019-02-22 14:33:46 +000085 void getAnalysisUsage(AnalysisUsage &AU) const override {
86 AU.addRequired<LazyValueInfoWrapperPass>();
87 }
88
Anton Korobeynikov8a6dc102007-03-10 16:46:28 +000089 struct CaseRange {
Hans Wennborg8b4dbdf2015-02-05 16:58:10 +000090 ConstantInt* Low;
91 ConstantInt* High;
Anton Korobeynikov8a6dc102007-03-10 16:46:28 +000092 BasicBlock* BB;
93
Hans Wennborg8b4dbdf2015-02-05 16:58:10 +000094 CaseRange(ConstantInt *low, ConstantInt *high, BasicBlock *bb)
Hans Wennborg8c82fbc2015-02-05 16:50:27 +000095 : Low(low), High(high), BB(bb) {}
Anton Korobeynikov8a6dc102007-03-10 16:46:28 +000096 };
97
Eugene Zelenkofce43572017-10-21 00:57:46 +000098 using CaseVector = std::vector<CaseRange>;
99 using CaseItr = std::vector<CaseRange>::iterator;
100
Chris Lattnered922162003-10-07 18:46:23 +0000101 private:
Roman Tereshin99a66722019-02-22 14:33:46 +0000102 void processSwitchInst(SwitchInst *SI,
103 SmallPtrSetImpl<BasicBlock *> &DeleteList,
104 AssumptionCache *AC, LazyValueInfo *LVI);
Chris Lattnered922162003-10-07 18:46:23 +0000105
Jim Grosbachfff56632014-06-16 16:55:20 +0000106 BasicBlock *switchConvert(CaseItr Begin, CaseItr End,
107 ConstantInt *LowerBound, ConstantInt *UpperBound,
Marcello Maggioni78035b12014-07-11 10:34:36 +0000108 Value *Val, BasicBlock *Predecessor,
Hans Wennborgae9c9712015-01-23 20:43:51 +0000109 BasicBlock *OrigBlock, BasicBlock *Default,
110 const std::vector<IntRange> &UnreachableRanges);
Roman Tereshin99a66722019-02-22 14:33:46 +0000111 BasicBlock *newLeafBlock(CaseRange &Leaf, Value *Val,
112 ConstantInt *LowerBound, ConstantInt *UpperBound,
113 BasicBlock *OrigBlock, BasicBlock *Default);
Jim Grosbachfff56632014-06-16 16:55:20 +0000114 unsigned Clusterify(CaseVector &Cases, SwitchInst *SI);
Chris Lattnered922162003-10-07 18:46:23 +0000115 };
Bob Wilsone4077362013-09-09 19:14:35 +0000116
117 /// The comparison function for sorting the switch case values in the vector.
118 /// WARNING: Case ranges should be disjoint!
119 struct CaseCmp {
Eugene Zelenkofce43572017-10-21 00:57:46 +0000120 bool operator()(const LowerSwitch::CaseRange& C1,
121 const LowerSwitch::CaseRange& C2) {
Bob Wilsone4077362013-09-09 19:14:35 +0000122 const ConstantInt* CI1 = cast<const ConstantInt>(C1.Low);
123 const ConstantInt* CI2 = cast<const ConstantInt>(C2.High);
124 return CI1->getValue().slt(CI2->getValue());
125 }
126 };
Eugene Zelenkofce43572017-10-21 00:57:46 +0000127
128} // end anonymous namespace
Chris Lattner1b094a02003-04-23 16:23:59 +0000129
Dan Gohmand78c4002008-05-13 00:00:25 +0000130char LowerSwitch::ID = 0;
Dan Gohmand78c4002008-05-13 00:00:25 +0000131
Chris Lattner0ab5e2c2011-04-15 05:18:47 +0000132// Publicly exposed interface to pass...
Owen Andersona7aed182010-08-06 18:33:48 +0000133char &llvm::LowerSwitchID = LowerSwitch::ID;
Eugene Zelenkofce43572017-10-21 00:57:46 +0000134
Roman Tereshin99a66722019-02-22 14:33:46 +0000135INITIALIZE_PASS_BEGIN(LowerSwitch, "lowerswitch",
136 "Lower SwitchInst's to branches", false, false)
137INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
138INITIALIZE_PASS_DEPENDENCY(LazyValueInfoWrapperPass)
139INITIALIZE_PASS_END(LowerSwitch, "lowerswitch",
140 "Lower SwitchInst's to branches", false, false)
Eugene Zelenkofce43572017-10-21 00:57:46 +0000141
Chris Lattner1b094a02003-04-23 16:23:59 +0000142// createLowerSwitchPass - Interface to this file...
Chris Lattner49525f82004-01-09 06:02:20 +0000143FunctionPass *llvm::createLowerSwitchPass() {
Chris Lattner1b094a02003-04-23 16:23:59 +0000144 return new LowerSwitch();
145}
146
147bool LowerSwitch::runOnFunction(Function &F) {
Roman Tereshin99a66722019-02-22 14:33:46 +0000148 LazyValueInfo *LVI = &getAnalysis<LazyValueInfoWrapperPass>().getLVI();
149 auto *ACT = getAnalysisIfAvailable<AssumptionCacheTracker>();
150 AssumptionCache *AC = ACT ? &ACT->getAssumptionCache(F) : nullptr;
151 // Prevent LazyValueInfo from using the DominatorTree as LowerSwitch does not
152 // preserve it and it becomes stale (when available) pretty much immediately.
153 // Currently the DominatorTree is only used by LowerSwitch indirectly via LVI
154 // and computeKnownBits to refine isValidAssumeForContext's results. Given
155 // that the latter can handle some of the simple cases w/o a DominatorTree,
156 // it's easier to refrain from using the tree than to keep it up to date.
157 LVI->disableDT();
158
Chris Lattner1b094a02003-04-23 16:23:59 +0000159 bool Changed = false;
Chen Li0786bc92015-08-11 20:16:17 +0000160 SmallPtrSet<BasicBlock*, 8> DeleteList;
Chris Lattner1b094a02003-04-23 16:23:59 +0000161
162 for (Function::iterator I = F.begin(), E = F.end(); I != E; ) {
Duncan P. N. Exon Smith5b4c8372015-10-13 02:39:05 +0000163 BasicBlock *Cur = &*I++; // Advance over block so we don't traverse new blocks
Chris Lattner1b094a02003-04-23 16:23:59 +0000164
Chen Li0786bc92015-08-11 20:16:17 +0000165 // If the block is a dead Default block that will be deleted later, don't
166 // waste time processing it.
167 if (DeleteList.count(Cur))
168 continue;
169
Chris Lattner1b094a02003-04-23 16:23:59 +0000170 if (SwitchInst *SI = dyn_cast<SwitchInst>(Cur->getTerminator())) {
171 Changed = true;
Roman Tereshin99a66722019-02-22 14:33:46 +0000172 processSwitchInst(SI, DeleteList, AC, LVI);
Chris Lattner1b094a02003-04-23 16:23:59 +0000173 }
174 }
175
Chen Li10f01bd2015-08-11 18:12:26 +0000176 for (BasicBlock* BB: DeleteList) {
Roman Tereshin99a66722019-02-22 14:33:46 +0000177 LVI->eraseBlock(BB);
Chen Li10f01bd2015-08-11 18:12:26 +0000178 DeleteDeadBlock(BB);
179 }
180
Chris Lattner1b094a02003-04-23 16:23:59 +0000181 return Changed;
182}
183
Sanjay Patel815adac2015-09-16 16:21:08 +0000184/// Used for debugging purposes.
Fangrui Song862eebb2018-05-05 20:14:38 +0000185LLVM_ATTRIBUTE_USED
186static raw_ostream &operator<<(raw_ostream &O,
Daniel Dunbar796e43e2009-07-24 10:36:58 +0000187 const LowerSwitch::CaseVector &C) {
Chris Lattnered922162003-10-07 18:46:23 +0000188 O << "[";
189
Roman Tereshin99a66722019-02-22 14:33:46 +0000190 for (LowerSwitch::CaseVector::const_iterator B = C.begin(), E = C.end();
191 B != E;) {
192 O << "[" << B->Low->getValue() << ", " << B->High->getValue() << "]";
193 if (++B != E)
194 O << ", ";
Chris Lattnered922162003-10-07 18:46:23 +0000195 }
196
197 return O << "]";
198}
Anton Korobeynikov8a6dc102007-03-10 16:46:28 +0000199
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000200/// Update the first occurrence of the "switch statement" BB in the PHI
Sanjay Patel815adac2015-09-16 16:21:08 +0000201/// node with the "new" BB. The other occurrences will:
202///
203/// 1) Be updated by subsequent calls to this function. Switch statements may
204/// have more than one outcoming edge into the same BB if they all have the same
205/// value. When the switch statement is converted these incoming edges are now
206/// coming from multiple BBs.
207/// 2) Removed if subsequent incoming values now share the same case, i.e.,
208/// multiple outcome edges are condensed into one. This is necessary to keep the
209/// number of phi values equal to the number of branches to SuccBB.
Roman Tereshin99a66722019-02-22 14:33:46 +0000210static void
211fixPhis(BasicBlock *SuccBB, BasicBlock *OrigBB, BasicBlock *NewBB,
212 const unsigned NumMergedCases = std::numeric_limits<unsigned>::max()) {
Duncan P. N. Exon Smith5b4c8372015-10-13 02:39:05 +0000213 for (BasicBlock::iterator I = SuccBB->begin(),
214 IE = SuccBB->getFirstNonPHI()->getIterator();
Bruno Cardoso Lopesbc7ba2c2014-11-28 19:47:33 +0000215 I != IE; ++I) {
Marcello Maggioni78035b12014-07-11 10:34:36 +0000216 PHINode *PN = cast<PHINode>(I);
217
Benjamin Kramerdf005cb2015-08-08 18:27:36 +0000218 // Only update the first occurrence.
Bruno Cardoso Lopesbc7ba2c2014-11-28 19:47:33 +0000219 unsigned Idx = 0, E = PN->getNumIncomingValues();
Bruno Cardoso Lopes15520db2014-12-02 18:31:53 +0000220 unsigned LocalNumMergedCases = NumMergedCases;
Bruno Cardoso Lopesbc7ba2c2014-11-28 19:47:33 +0000221 for (; Idx != E; ++Idx) {
Juergen Ributzkad4417252014-11-10 21:05:27 +0000222 if (PN->getIncomingBlock(Idx) == OrigBB) {
223 PN->setIncomingBlock(Idx, NewBB);
224 break;
225 }
Marcello Maggioni78035b12014-07-11 10:34:36 +0000226 }
Bruno Cardoso Lopesbc7ba2c2014-11-28 19:47:33 +0000227
Benjamin Kramerdf005cb2015-08-08 18:27:36 +0000228 // Remove additional occurrences coming from condensed cases and keep the
Bruno Cardoso Lopesbc7ba2c2014-11-28 19:47:33 +0000229 // number of incoming values equal to the number of branches to SuccBB.
Michael Liao24fcae82015-03-17 18:03:10 +0000230 SmallVector<unsigned, 8> Indices;
Bruno Cardoso Lopes15520db2014-12-02 18:31:53 +0000231 for (++Idx; LocalNumMergedCases > 0 && Idx < E; ++Idx)
Bruno Cardoso Lopesbc7ba2c2014-11-28 19:47:33 +0000232 if (PN->getIncomingBlock(Idx) == OrigBB) {
Michael Liao24fcae82015-03-17 18:03:10 +0000233 Indices.push_back(Idx);
Bruno Cardoso Lopes15520db2014-12-02 18:31:53 +0000234 LocalNumMergedCases--;
Bruno Cardoso Lopesbc7ba2c2014-11-28 19:47:33 +0000235 }
Michael Liao24fcae82015-03-17 18:03:10 +0000236 // Remove incoming values in the reverse order to prevent invalidating
237 // *successive* index.
Eugene Zelenkofce43572017-10-21 00:57:46 +0000238 for (unsigned III : llvm::reverse(Indices))
David Majnemerd7708772016-06-24 04:05:21 +0000239 PN->removeIncomingValue(III);
Marcello Maggioni78035b12014-07-11 10:34:36 +0000240 }
241}
242
Sanjay Patel815adac2015-09-16 16:21:08 +0000243/// Convert the switch statement into a binary lookup of the case values.
244/// The function recursively builds this tree. LowerBound and UpperBound are
245/// used to keep track of the bounds for Val that have already been checked by
246/// a block emitted by one of the previous calls to switchConvert in the call
247/// stack.
Hans Wennborgae9c9712015-01-23 20:43:51 +0000248BasicBlock *
249LowerSwitch::switchConvert(CaseItr Begin, CaseItr End, ConstantInt *LowerBound,
250 ConstantInt *UpperBound, Value *Val,
251 BasicBlock *Predecessor, BasicBlock *OrigBlock,
252 BasicBlock *Default,
253 const std::vector<IntRange> &UnreachableRanges) {
Roman Tereshin99a66722019-02-22 14:33:46 +0000254 assert(LowerBound && UpperBound && "Bounds must be initialized");
Chris Lattnered922162003-10-07 18:46:23 +0000255 unsigned Size = End - Begin;
256
Jim Grosbachfff56632014-06-16 16:55:20 +0000257 if (Size == 1) {
258 // Check if the Case Range is perfectly squeezed in between
259 // already checked Upper and Lower bounds. If it is then we can avoid
260 // emitting the code that checks if the value actually falls in the range
261 // because the bounds already tell us so.
262 if (Begin->Low == LowerBound && Begin->High == UpperBound) {
Bruno Cardoso Lopesbc7ba2c2014-11-28 19:47:33 +0000263 unsigned NumMergedCases = 0;
Roman Tereshin99a66722019-02-22 14:33:46 +0000264 NumMergedCases = UpperBound->getSExtValue() - LowerBound->getSExtValue();
Bruno Cardoso Lopesbc7ba2c2014-11-28 19:47:33 +0000265 fixPhis(Begin->BB, OrigBlock, Predecessor, NumMergedCases);
Jim Grosbachfff56632014-06-16 16:55:20 +0000266 return Begin->BB;
267 }
Roman Tereshin99a66722019-02-22 14:33:46 +0000268 return newLeafBlock(*Begin, Val, LowerBound, UpperBound, OrigBlock,
269 Default);
Jim Grosbachfff56632014-06-16 16:55:20 +0000270 }
Chris Lattnered922162003-10-07 18:46:23 +0000271
272 unsigned Mid = Size / 2;
Anton Korobeynikov8a6dc102007-03-10 16:46:28 +0000273 std::vector<CaseRange> LHS(Begin, Begin + Mid);
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000274 LLVM_DEBUG(dbgs() << "LHS: " << LHS << "\n");
Anton Korobeynikov8a6dc102007-03-10 16:46:28 +0000275 std::vector<CaseRange> RHS(Begin + Mid, End);
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000276 LLVM_DEBUG(dbgs() << "RHS: " << RHS << "\n");
Chris Lattnered922162003-10-07 18:46:23 +0000277
Jim Grosbachfff56632014-06-16 16:55:20 +0000278 CaseRange &Pivot = *(Begin + Mid);
Roman Tereshin99a66722019-02-22 14:33:46 +0000279 LLVM_DEBUG(dbgs() << "Pivot ==> [" << Pivot.Low->getValue() << ", "
280 << Pivot.High->getValue() << "]\n");
Chris Lattnered922162003-10-07 18:46:23 +0000281
Jim Grosbachfff56632014-06-16 16:55:20 +0000282 // NewLowerBound here should never be the integer minimal value.
283 // This is because it is computed from a case range that is never
284 // the smallest, so there is always a case range that has at least
285 // a smaller value.
Hans Wennborg8b4dbdf2015-02-05 16:58:10 +0000286 ConstantInt *NewLowerBound = Pivot.Low;
Jim Grosbachfff56632014-06-16 16:55:20 +0000287
Hans Wennborgae9c9712015-01-23 20:43:51 +0000288 // Because NewLowerBound is never the smallest representable integer
289 // it is safe here to subtract one.
290 ConstantInt *NewUpperBound = ConstantInt::get(NewLowerBound->getContext(),
291 NewLowerBound->getValue() - 1);
292
293 if (!UnreachableRanges.empty()) {
294 // Check if the gap between LHS's highest and NewLowerBound is unreachable.
Hans Wennborg8b4dbdf2015-02-05 16:58:10 +0000295 int64_t GapLow = LHS.back().High->getSExtValue() + 1;
Hans Wennborgae9c9712015-01-23 20:43:51 +0000296 int64_t GapHigh = NewLowerBound->getSExtValue() - 1;
297 IntRange Gap = { GapLow, GapHigh };
298 if (GapHigh >= GapLow && IsInRanges(Gap, UnreachableRanges))
Hans Wennborg8b4dbdf2015-02-05 16:58:10 +0000299 NewUpperBound = LHS.back().High;
Jim Grosbachfff56632014-06-16 16:55:20 +0000300 }
301
Roman Tereshin99a66722019-02-22 14:33:46 +0000302 LLVM_DEBUG(dbgs() << "LHS Bounds ==> [" << LowerBound->getSExtValue() << ", "
303 << NewUpperBound->getSExtValue() << "]\n"
304 << "RHS Bounds ==> [" << NewLowerBound->getSExtValue()
305 << ", " << UpperBound->getSExtValue() << "]\n");
Jim Grosbachfff56632014-06-16 16:55:20 +0000306
Chris Lattnered922162003-10-07 18:46:23 +0000307 // Create a new node that checks if the value is < pivot. Go to the
308 // left branch if it is and right branch if not.
309 Function* F = OrigBlock->getParent();
Owen Anderson55f1c092009-08-13 21:58:54 +0000310 BasicBlock* NewNode = BasicBlock::Create(Val->getContext(), "NodeBlock");
Chris Lattnered922162003-10-07 18:46:23 +0000311
Bob Wilsone4077362013-09-09 19:14:35 +0000312 ICmpInst* Comp = new ICmpInst(ICmpInst::ICMP_SLT,
Owen Anderson1e5f00e2009-07-09 23:48:35 +0000313 Val, Pivot.Low, "Pivot");
Marcello Maggioni78035b12014-07-11 10:34:36 +0000314
315 BasicBlock *LBranch = switchConvert(LHS.begin(), LHS.end(), LowerBound,
316 NewUpperBound, Val, NewNode, OrigBlock,
Hans Wennborgae9c9712015-01-23 20:43:51 +0000317 Default, UnreachableRanges);
Marcello Maggioni78035b12014-07-11 10:34:36 +0000318 BasicBlock *RBranch = switchConvert(RHS.begin(), RHS.end(), NewLowerBound,
319 UpperBound, Val, NewNode, OrigBlock,
Hans Wennborgae9c9712015-01-23 20:43:51 +0000320 Default, UnreachableRanges);
Marcello Maggioni78035b12014-07-11 10:34:36 +0000321
Duncan P. N. Exon Smith5b4c8372015-10-13 02:39:05 +0000322 F->getBasicBlockList().insert(++OrigBlock->getIterator(), NewNode);
Chris Lattnered922162003-10-07 18:46:23 +0000323 NewNode->getInstList().push_back(Comp);
Marcello Maggioni78035b12014-07-11 10:34:36 +0000324
Gabor Greife9ecc682008-04-06 20:25:17 +0000325 BranchInst::Create(LBranch, RBranch, Comp, NewNode);
Chris Lattnered922162003-10-07 18:46:23 +0000326 return NewNode;
327}
328
Sanjay Patel815adac2015-09-16 16:21:08 +0000329/// Create a new leaf block for the binary lookup tree. It checks if the
330/// switch's value == the case's value. If not, then it jumps to the default
331/// branch. At this point in the tree, the value can't be another valid case
332/// value, so the jump to the "default" branch is warranted.
Roman Tereshin99a66722019-02-22 14:33:46 +0000333BasicBlock *LowerSwitch::newLeafBlock(CaseRange &Leaf, Value *Val,
334 ConstantInt *LowerBound,
335 ConstantInt *UpperBound,
336 BasicBlock *OrigBlock,
337 BasicBlock *Default) {
Chris Lattnered922162003-10-07 18:46:23 +0000338 Function* F = OrigBlock->getParent();
Owen Anderson55f1c092009-08-13 21:58:54 +0000339 BasicBlock* NewLeaf = BasicBlock::Create(Val->getContext(), "LeafBlock");
Duncan P. N. Exon Smith5b4c8372015-10-13 02:39:05 +0000340 F->getBasicBlockList().insert(++OrigBlock->getIterator(), NewLeaf);
Chris Lattnered922162003-10-07 18:46:23 +0000341
Anton Korobeynikov8a6dc102007-03-10 16:46:28 +0000342 // Emit comparison
Craig Topperf40110f2014-04-25 05:29:35 +0000343 ICmpInst* Comp = nullptr;
Anton Korobeynikov8a6dc102007-03-10 16:46:28 +0000344 if (Leaf.Low == Leaf.High) {
345 // Make the seteq instruction...
Owen Anderson1e5f00e2009-07-09 23:48:35 +0000346 Comp = new ICmpInst(*NewLeaf, ICmpInst::ICMP_EQ, Val,
347 Leaf.Low, "SwitchLeaf");
Anton Korobeynikov8a6dc102007-03-10 16:46:28 +0000348 } else {
349 // Make range comparison
Roman Tereshin99a66722019-02-22 14:33:46 +0000350 if (Leaf.Low == LowerBound) {
Anton Korobeynikov8a6dc102007-03-10 16:46:28 +0000351 // Val >= Min && Val <= Hi --> Val <= Hi
Owen Anderson1e5f00e2009-07-09 23:48:35 +0000352 Comp = new ICmpInst(*NewLeaf, ICmpInst::ICMP_SLE, Val, Leaf.High,
353 "SwitchLeaf");
Roman Tereshin99a66722019-02-22 14:33:46 +0000354 } else if (Leaf.High == UpperBound) {
355 // Val <= Max && Val >= Lo --> Val >= Lo
356 Comp = new ICmpInst(*NewLeaf, ICmpInst::ICMP_SGE, Val, Leaf.Low,
357 "SwitchLeaf");
Hans Wennborg8b4dbdf2015-02-05 16:58:10 +0000358 } else if (Leaf.Low->isZero()) {
Anton Korobeynikov8a6dc102007-03-10 16:46:28 +0000359 // Val >= 0 && Val <= Hi --> Val <=u Hi
Owen Anderson1e5f00e2009-07-09 23:48:35 +0000360 Comp = new ICmpInst(*NewLeaf, ICmpInst::ICMP_ULE, Val, Leaf.High,
Karl-Johan Karlsson1ffeb5d2018-07-10 12:06:16 +0000361 "SwitchLeaf");
Anton Korobeynikov8a6dc102007-03-10 16:46:28 +0000362 } else {
363 // Emit V-Lo <=u Hi-Lo
Owen Anderson487375e2009-07-29 18:55:55 +0000364 Constant* NegLo = ConstantExpr::getNeg(Leaf.Low);
Gabor Greife1f6e4b2008-05-16 19:29:10 +0000365 Instruction* Add = BinaryOperator::CreateAdd(Val, NegLo,
Anton Korobeynikov8a6dc102007-03-10 16:46:28 +0000366 Val->getName()+".off",
367 NewLeaf);
Owen Anderson487375e2009-07-29 18:55:55 +0000368 Constant *UpperBound = ConstantExpr::getAdd(NegLo, Leaf.High);
Owen Anderson1e5f00e2009-07-09 23:48:35 +0000369 Comp = new ICmpInst(*NewLeaf, ICmpInst::ICMP_ULE, Add, UpperBound,
370 "SwitchLeaf");
Anton Korobeynikov8a6dc102007-03-10 16:46:28 +0000371 }
372 }
Chris Lattnered922162003-10-07 18:46:23 +0000373
374 // Make the conditional branch...
Anton Korobeynikov8a6dc102007-03-10 16:46:28 +0000375 BasicBlock* Succ = Leaf.BB;
Gabor Greife9ecc682008-04-06 20:25:17 +0000376 BranchInst::Create(Succ, Default, Comp, NewLeaf);
Chris Lattnered922162003-10-07 18:46:23 +0000377
378 // If there were any PHI nodes in this successor, rewrite one entry
379 // from OrigBlock to come from NewLeaf.
Reid Spencer66149462004-09-15 17:06:42 +0000380 for (BasicBlock::iterator I = Succ->begin(); isa<PHINode>(I); ++I) {
381 PHINode* PN = cast<PHINode>(I);
Anton Korobeynikov8a6dc102007-03-10 16:46:28 +0000382 // Remove all but one incoming entries from the cluster
Hans Wennborg8b4dbdf2015-02-05 16:58:10 +0000383 uint64_t Range = Leaf.High->getSExtValue() -
384 Leaf.Low->getSExtValue();
Anton Korobeynikov8a6dc102007-03-10 16:46:28 +0000385 for (uint64_t j = 0; j < Range; ++j) {
386 PN->removeIncomingValue(OrigBlock);
387 }
Karl-Johan Karlsson1ffeb5d2018-07-10 12:06:16 +0000388
Chris Lattnered922162003-10-07 18:46:23 +0000389 int BlockIdx = PN->getBasicBlockIndex(OrigBlock);
390 assert(BlockIdx != -1 && "Switch didn't go to this successor??");
391 PN->setIncomingBlock((unsigned)BlockIdx, NewLeaf);
392 }
393
394 return NewLeaf;
395}
396
Roman Tereshin99a66722019-02-22 14:33:46 +0000397/// Transform simple list of \p SI's cases into list of CaseRange's \p Cases.
398/// \post \p Cases wouldn't contain references to \p SI's default BB.
399/// \returns Number of \p SI's cases that do not reference \p SI's default BB.
Anton Korobeynikov8a6dc102007-03-10 16:46:28 +0000400unsigned LowerSwitch::Clusterify(CaseVector& Cases, SwitchInst *SI) {
Roman Tereshin99a66722019-02-22 14:33:46 +0000401 unsigned NumSimpleCases = 0;
Anton Korobeynikov8a6dc102007-03-10 16:46:28 +0000402
403 // Start with "simple" cases
Roman Tereshin99a66722019-02-22 14:33:46 +0000404 for (auto Case : SI->cases()) {
405 if (Case.getCaseSuccessor() == SI->getDefaultDest())
406 continue;
Chandler Carruth927d8e62017-04-12 07:27:28 +0000407 Cases.push_back(CaseRange(Case.getCaseValue(), Case.getCaseValue(),
408 Case.getCaseSuccessor()));
Roman Tereshin99a66722019-02-22 14:33:46 +0000409 ++NumSimpleCases;
410 }
Chandler Carruth927d8e62017-04-12 07:27:28 +0000411
Fangrui Song0cac7262018-09-27 02:13:45 +0000412 llvm::sort(Cases, CaseCmp());
Bob Wilsone4077362013-09-09 19:14:35 +0000413
414 // Merge case into clusters
Benjamin Kramer00a477f2015-06-20 15:59:34 +0000415 if (Cases.size() >= 2) {
416 CaseItr I = Cases.begin();
417 for (CaseItr J = std::next(I), E = Cases.end(); J != E; ++J) {
Hans Wennborg8b4dbdf2015-02-05 16:58:10 +0000418 int64_t nextValue = J->Low->getSExtValue();
419 int64_t currentValue = I->High->getSExtValue();
Bob Wilsone4077362013-09-09 19:14:35 +0000420 BasicBlock* nextBB = J->BB;
421 BasicBlock* currentBB = I->BB;
422
423 // If the two neighboring cases go to the same destination, merge them
424 // into a single case.
Justin Bognere46d3792015-06-20 00:28:25 +0000425 assert(nextValue > currentValue && "Cases should be strictly ascending");
426 if ((nextValue == currentValue + 1) && (currentBB == nextBB)) {
Bob Wilsone4077362013-09-09 19:14:35 +0000427 I->High = J->High;
Benjamin Kramer00a477f2015-06-20 15:59:34 +0000428 // FIXME: Combine branch weights.
429 } else if (++I != J) {
430 *I = *J;
Bob Wilsone4077362013-09-09 19:14:35 +0000431 }
432 }
Benjamin Kramer00a477f2015-06-20 15:59:34 +0000433 Cases.erase(std::next(I), Cases.end());
434 }
Bob Wilsone4077362013-09-09 19:14:35 +0000435
Roman Tereshin99a66722019-02-22 14:33:46 +0000436 return NumSimpleCases;
437}
Anton Korobeynikov8a6dc102007-03-10 16:46:28 +0000438
Sanjay Patel815adac2015-09-16 16:21:08 +0000439/// Replace the specified switch instruction with a sequence of chained if-then
440/// insts in a balanced binary search.
Chen Li0786bc92015-08-11 20:16:17 +0000441void LowerSwitch::processSwitchInst(SwitchInst *SI,
Roman Tereshin99a66722019-02-22 14:33:46 +0000442 SmallPtrSetImpl<BasicBlock *> &DeleteList,
443 AssumptionCache *AC, LazyValueInfo *LVI) {
444 BasicBlock *OrigBlock = SI->getParent();
445 Function *F = OrigBlock->getParent();
Eli Friedman95031ed2011-09-29 20:21:17 +0000446 Value *Val = SI->getCondition(); // The value we are switching on...
Chris Lattnered922162003-10-07 18:46:23 +0000447 BasicBlock* Default = SI->getDefaultDest();
Chris Lattner1b094a02003-04-23 16:23:59 +0000448
Matt Arsenault01d17e72017-04-21 23:54:12 +0000449 // Don't handle unreachable blocks. If there are successors with phis, this
450 // would leave them behind with missing predecessors.
Roman Tereshin99a66722019-02-22 14:33:46 +0000451 if ((OrigBlock != &F->getEntryBlock() && pred_empty(OrigBlock)) ||
452 OrigBlock->getSinglePredecessor() == OrigBlock) {
453 DeleteList.insert(OrigBlock);
Chris Lattnerf1b1c5e2003-08-23 22:54:34 +0000454 return;
455 }
456
Hans Wennborgae9c9712015-01-23 20:43:51 +0000457 // Prepare cases vector.
458 CaseVector Cases;
Roman Tereshin99a66722019-02-22 14:33:46 +0000459 const unsigned NumSimpleCases = Clusterify(Cases, SI);
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000460 LLVM_DEBUG(dbgs() << "Clusterify finished. Total clusters: " << Cases.size()
Roman Tereshin99a66722019-02-22 14:33:46 +0000461 << ". Total non-default cases: " << NumSimpleCases
462 << "\nCase clusters: " << Cases << "\n");
463
464 // If there is only the default destination, just branch.
465 if (Cases.empty()) {
466 BranchInst::Create(Default, OrigBlock);
467 // Remove all the references from Default's PHIs to OrigBlock, but one.
468 fixPhis(Default, OrigBlock, OrigBlock);
469 SI->eraseFromParent();
470 return;
471 }
Hans Wennborgae9c9712015-01-23 20:43:51 +0000472
473 ConstantInt *LowerBound = nullptr;
474 ConstantInt *UpperBound = nullptr;
Roman Tereshin99a66722019-02-22 14:33:46 +0000475 bool DefaultIsUnreachableFromSwitch = false;
Hans Wennborgae9c9712015-01-23 20:43:51 +0000476
477 if (isa<UnreachableInst>(Default->getFirstNonPHIOrDbg())) {
Benjamin Kramerdf005cb2015-08-08 18:27:36 +0000478 // Make the bounds tightly fitted around the case value range, because we
Hans Wennborgae9c9712015-01-23 20:43:51 +0000479 // know that the value passed to the switch must be exactly one of the case
480 // values.
Hans Wennborg8b4dbdf2015-02-05 16:58:10 +0000481 LowerBound = Cases.front().Low;
482 UpperBound = Cases.back().High;
Roman Tereshin99a66722019-02-22 14:33:46 +0000483 DefaultIsUnreachableFromSwitch = true;
484 } else {
485 // Constraining the range of the value being switched over helps eliminating
486 // unreachable BBs and minimizing the number of `add` instructions
487 // newLeafBlock ends up emitting. Running CorrelatedValuePropagation after
488 // LowerSwitch isn't as good, and also much more expensive in terms of
489 // compile time for the following reasons:
490 // 1. it processes many kinds of instructions, not just switches;
491 // 2. even if limited to icmp instructions only, it will have to process
492 // roughly C icmp's per switch, where C is the number of cases in the
493 // switch, while LowerSwitch only needs to call LVI once per switch.
494 const DataLayout &DL = F->getParent()->getDataLayout();
495 KnownBits Known = computeKnownBits(Val, DL, /*Depth=*/0, AC, SI);
Nikita Popov0125e442019-03-23 12:48:54 +0000496 // TODO Shouldn't this create a signed range?
497 ConstantRange KnownBitsRange =
Rui Ueyama49a3ad22019-07-16 04:46:31 +0000498 ConstantRange::fromKnownBits(Known, /*IsSigned=*/false);
Roman Tereshin99a66722019-02-22 14:33:46 +0000499 const ConstantRange LVIRange = LVI->getConstantRange(Val, OrigBlock, SI);
500 ConstantRange ValRange = KnownBitsRange.intersectWith(LVIRange);
501 // We delegate removal of unreachable non-default cases to other passes. In
502 // the unlikely event that some of them survived, we just conservatively
503 // maintain the invariant that all the cases lie between the bounds. This
504 // may, however, still render the default case effectively unreachable.
505 APInt Low = Cases.front().Low->getValue();
506 APInt High = Cases.back().High->getValue();
507 APInt Min = APIntOps::smin(ValRange.getSignedMin(), Low);
508 APInt Max = APIntOps::smax(ValRange.getSignedMax(), High);
Hans Wennborgae9c9712015-01-23 20:43:51 +0000509
Roman Tereshin99a66722019-02-22 14:33:46 +0000510 LowerBound = ConstantInt::get(SI->getContext(), Min);
511 UpperBound = ConstantInt::get(SI->getContext(), Max);
512 DefaultIsUnreachableFromSwitch = (Min + (NumSimpleCases - 1) == Max);
513 }
514
515 std::vector<IntRange> UnreachableRanges;
516
517 if (DefaultIsUnreachableFromSwitch) {
Hans Wennborgae9c9712015-01-23 20:43:51 +0000518 DenseMap<BasicBlock *, unsigned> Popularity;
519 unsigned MaxPop = 0;
520 BasicBlock *PopSucc = nullptr;
521
Eugene Zelenkofce43572017-10-21 00:57:46 +0000522 IntRange R = {std::numeric_limits<int64_t>::min(),
523 std::numeric_limits<int64_t>::max()};
Hans Wennborgae9c9712015-01-23 20:43:51 +0000524 UnreachableRanges.push_back(R);
525 for (const auto &I : Cases) {
Hans Wennborg8b4dbdf2015-02-05 16:58:10 +0000526 int64_t Low = I.Low->getSExtValue();
527 int64_t High = I.High->getSExtValue();
Hans Wennborgae9c9712015-01-23 20:43:51 +0000528
529 IntRange &LastRange = UnreachableRanges.back();
530 if (LastRange.Low == Low) {
531 // There is nothing left of the previous range.
532 UnreachableRanges.pop_back();
533 } else {
534 // Terminate the previous range.
535 assert(Low > LastRange.Low);
536 LastRange.High = Low - 1;
537 }
Eugene Zelenkofce43572017-10-21 00:57:46 +0000538 if (High != std::numeric_limits<int64_t>::max()) {
539 IntRange R = { High + 1, std::numeric_limits<int64_t>::max() };
Hans Wennborgae9c9712015-01-23 20:43:51 +0000540 UnreachableRanges.push_back(R);
541 }
542
543 // Count popularity.
544 int64_t N = High - Low + 1;
545 unsigned &Pop = Popularity[I.BB];
546 if ((Pop += N) > MaxPop) {
547 MaxPop = Pop;
548 PopSucc = I.BB;
549 }
550 }
551#ifndef NDEBUG
552 /* UnreachableRanges should be sorted and the ranges non-adjacent. */
553 for (auto I = UnreachableRanges.begin(), E = UnreachableRanges.end();
554 I != E; ++I) {
555 assert(I->Low <= I->High);
556 auto Next = I + 1;
557 if (Next != E) {
558 assert(Next->Low > I->High);
559 }
560 }
561#endif
562
Karl-Johan Karlsson1ffeb5d2018-07-10 12:06:16 +0000563 // As the default block in the switch is unreachable, update the PHI nodes
Roman Tereshin99a66722019-02-22 14:33:46 +0000564 // (remove all of the references to the default block) to reflect this.
565 const unsigned NumDefaultEdges = SI->getNumCases() + 1 - NumSimpleCases;
566 for (unsigned I = 0; I < NumDefaultEdges; ++I)
567 Default->removePredecessor(OrigBlock);
Karl-Johan Karlsson1ffeb5d2018-07-10 12:06:16 +0000568
Hans Wennborgae9c9712015-01-23 20:43:51 +0000569 // Use the most popular block as the new default, reducing the number of
570 // cases.
571 assert(MaxPop > 0 && PopSucc);
572 Default = PopSucc;
David Majnemerc7004902016-08-12 04:32:37 +0000573 Cases.erase(
Eugene Zelenkofce43572017-10-21 00:57:46 +0000574 llvm::remove_if(
575 Cases, [PopSucc](const CaseRange &R) { return R.BB == PopSucc; }),
David Majnemerc7004902016-08-12 04:32:37 +0000576 Cases.end());
Hans Wennborgae9c9712015-01-23 20:43:51 +0000577
578 // If there are no cases left, just branch.
579 if (Cases.empty()) {
Roman Tereshin99a66722019-02-22 14:33:46 +0000580 BranchInst::Create(Default, OrigBlock);
Hans Wennborgae9c9712015-01-23 20:43:51 +0000581 SI->eraseFromParent();
Karl-Johan Karlsson1ffeb5d2018-07-10 12:06:16 +0000582 // As all the cases have been replaced with a single branch, only keep
583 // one entry in the PHI nodes.
584 for (unsigned I = 0 ; I < (MaxPop - 1) ; ++I)
585 PopSucc->removePredecessor(OrigBlock);
Hans Wennborgae9c9712015-01-23 20:43:51 +0000586 return;
587 }
Andrew Kaylor4172dba2019-06-03 17:54:15 +0000588
589 // If the condition was a PHI node with the switch block as a predecessor
590 // removing predecessors may have caused the condition to be erased.
591 // Getting the condition value again here protects against that.
592 Val = SI->getCondition();
Hans Wennborgae9c9712015-01-23 20:43:51 +0000593 }
594
Chris Lattnered922162003-10-07 18:46:23 +0000595 // Create a new, empty default block so that the new hierarchy of
596 // if-then statements go to this and the PHI nodes are happy.
Hans Wennborgae9c9712015-01-23 20:43:51 +0000597 BasicBlock *NewDefault = BasicBlock::Create(SI->getContext(), "NewDefault");
Duncan P. N. Exon Smith5b4c8372015-10-13 02:39:05 +0000598 F->getBasicBlockList().insert(Default->getIterator(), NewDefault);
Hans Wennborgae9c9712015-01-23 20:43:51 +0000599 BranchInst::Create(Default, NewDefault);
Chris Lattner1b094a02003-04-23 16:23:59 +0000600
Jim Grosbachfff56632014-06-16 16:55:20 +0000601 BasicBlock *SwitchBlock =
602 switchConvert(Cases.begin(), Cases.end(), LowerBound, UpperBound, Val,
Hans Wennborgae9c9712015-01-23 20:43:51 +0000603 OrigBlock, OrigBlock, NewDefault, UnreachableRanges);
Chris Lattnered922162003-10-07 18:46:23 +0000604
Karl-Johan Karlsson11d68a62018-05-22 08:46:48 +0000605 // If there are entries in any PHI nodes for the default edge, make sure
606 // to update them as well.
Roman Tereshin99a66722019-02-22 14:33:46 +0000607 fixPhis(Default, OrigBlock, NewDefault);
Karl-Johan Karlsson11d68a62018-05-22 08:46:48 +0000608
Chris Lattnered922162003-10-07 18:46:23 +0000609 // Branch to our shiny new if-then stuff...
Gabor Greife9ecc682008-04-06 20:25:17 +0000610 BranchInst::Create(SwitchBlock, OrigBlock);
Chris Lattnered922162003-10-07 18:46:23 +0000611
Chris Lattner1b094a02003-04-23 16:23:59 +0000612 // We are now done with the switch instruction, delete it.
Hans Wennborgae9c9712015-01-23 20:43:51 +0000613 BasicBlock *OldDefault = SI->getDefaultDest();
Roman Tereshin99a66722019-02-22 14:33:46 +0000614 OrigBlock->getInstList().erase(SI);
Jim Grosbachfff56632014-06-16 16:55:20 +0000615
Chen Li10f01bd2015-08-11 18:12:26 +0000616 // If the Default block has no more predecessors just add it to DeleteList.
Hans Wennborgae9c9712015-01-23 20:43:51 +0000617 if (pred_begin(OldDefault) == pred_end(OldDefault))
Chen Li0786bc92015-08-11 20:16:17 +0000618 DeleteList.insert(OldDefault);
Chris Lattner1b094a02003-04-23 16:23:59 +0000619}