blob: f9d5d2d4d36b513b597a917937cefccf721e2dff [file] [log] [blame]
Chris Lattner466a0492002-05-21 20:50:24 +00001//===- SimplifyCFG.cpp - Code to perform CFG simplification ---------------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanb1c93172005-04-21 23:48:37 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner466a0492002-05-21 20:50:24 +00009//
Chris Lattnera704ac82002-10-08 21:36:33 +000010// Peephole optimize the CFG.
Chris Lattner466a0492002-05-21 20:50:24 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/Transforms/Utils/Local.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000015#include "llvm/ADT/DenseMap.h"
16#include "llvm/ADT/STLExtras.h"
17#include "llvm/ADT/SetVector.h"
18#include "llvm/ADT/SmallPtrSet.h"
19#include "llvm/ADT/SmallVector.h"
20#include "llvm/ADT/Statistic.h"
Benjamin Kramer7c302602013-11-12 12:24:36 +000021#include "llvm/Analysis/ConstantFolding.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000022#include "llvm/Analysis/InstructionSimplify.h"
Chandler Carruthd3e73552013-01-07 03:08:10 +000023#include "llvm/Analysis/TargetTransformInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000024#include "llvm/Analysis/ValueTracking.h"
Chandler Carruth1305dc32014-03-04 11:45:46 +000025#include "llvm/IR/CFG.h"
Chandler Carruth8cd041e2014-03-04 12:24:34 +000026#include "llvm/IR/ConstantRange.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000027#include "llvm/IR/Constants.h"
28#include "llvm/IR/DataLayout.h"
29#include "llvm/IR/DerivedTypes.h"
30#include "llvm/IR/GlobalVariable.h"
31#include "llvm/IR/IRBuilder.h"
32#include "llvm/IR/Instructions.h"
33#include "llvm/IR/IntrinsicInst.h"
34#include "llvm/IR/LLVMContext.h"
35#include "llvm/IR/MDBuilder.h"
36#include "llvm/IR/Metadata.h"
37#include "llvm/IR/Module.h"
Chandler Carruth64396b02014-03-04 12:05:47 +000038#include "llvm/IR/NoFolder.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000039#include "llvm/IR/Operator.h"
Chandler Carruth820a9082014-03-04 11:08:18 +000040#include "llvm/IR/PatternMatch.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000041#include "llvm/IR/Type.h"
Evan Chengd983eba2011-01-29 04:46:23 +000042#include "llvm/Support/CommandLine.h"
Chris Lattnerd7beca32010-12-14 06:17:25 +000043#include "llvm/Support/Debug.h"
44#include "llvm/Support/raw_ostream.h"
Chandler Carruthaafe0912012-06-29 12:38:19 +000045#include "llvm/Transforms/Utils/BasicBlockUtils.h"
Rafael Espindolaea46c322014-08-15 15:46:38 +000046#include "llvm/Transforms/Utils/Local.h"
Jingyue Wufc029672014-09-30 22:23:38 +000047#include "llvm/Transforms/Utils/ValueMapper.h"
Chris Lattner466a0492002-05-21 20:50:24 +000048#include <algorithm>
Chris Lattner5edb2f32004-10-18 04:07:22 +000049#include <map>
Chandler Carruthed0881b2012-12-03 16:50:05 +000050#include <set>
Chris Lattnerdf3c3422004-01-09 06:12:26 +000051using namespace llvm;
Benjamin Kramer37172222013-07-04 14:22:02 +000052using namespace PatternMatch;
Brian Gaeke960707c2003-11-11 22:41:34 +000053
Chandler Carruth964daaa2014-04-22 02:55:47 +000054#define DEBUG_TYPE "simplifycfg"
55
James Molloy1b6207e2015-02-13 10:48:30 +000056// Chosen as 2 so as to be cheap, but still to have enough power to fold
57// a select, so the "clamp" idiom (of a min followed by a max) will be caught.
58// To catch this, we need to fold a compare and a select, hence '2' being the
59// minimum reasonable default.
Peter Collingbourne616044a2011-04-29 18:47:38 +000060static cl::opt<unsigned>
James Molloy1b6207e2015-02-13 10:48:30 +000061PHINodeFoldingThreshold("phi-node-folding-threshold", cl::Hidden, cl::init(2),
62 cl::desc("Control the amount of phi node folding to perform (default = 2)"));
Peter Collingbourne616044a2011-04-29 18:47:38 +000063
Evan Chengd983eba2011-01-29 04:46:23 +000064static cl::opt<bool>
65DupRet("simplifycfg-dup-ret", cl::Hidden, cl::init(false),
66 cl::desc("Duplicate return instructions into unconditional branches"));
67
Manman Ren93ab6492012-09-20 22:37:36 +000068static cl::opt<bool>
69SinkCommon("simplifycfg-sink-common", cl::Hidden, cl::init(true),
70 cl::desc("Sink common instructions down to the end block"));
71
Alp Tokercb402912014-01-24 17:20:08 +000072static cl::opt<bool> HoistCondStores(
73 "simplifycfg-hoist-cond-stores", cl::Hidden, cl::init(true),
74 cl::desc("Hoist conditional stores if an unconditional store precedes"));
Arnold Schwaighofer474df6d2013-04-29 21:28:24 +000075
Hans Wennborg39583b82012-09-26 09:44:49 +000076STATISTIC(NumBitMaps, "Number of switch instructions turned into bitmaps");
Erik Eckstein105374f2014-11-17 09:13:57 +000077STATISTIC(NumLinearMaps, "Number of switch instructions turned into linear mapping");
Hans Wennborgcd3a11f2012-09-26 14:01:53 +000078STATISTIC(NumLookupTables, "Number of switch instructions turned into lookup tables");
Hans Wennborgb73c0b02014-03-12 18:35:40 +000079STATISTIC(NumLookupTablesHoles, "Number of switch instructions turned into lookup tables (holes checked)");
Erik Eckstein0d86c762014-11-27 15:13:14 +000080STATISTIC(NumTableCmpReuses, "Number of reused switch table lookup compares");
Manman Ren93ab6492012-09-20 22:37:36 +000081STATISTIC(NumSinkCommons, "Number of common instructions sunk down to the end block");
Hans Wennborgcd3a11f2012-09-26 14:01:53 +000082STATISTIC(NumSpeculations, "Number of speculative executed instructions");
Evan Cheng89553cc2008-06-12 21:15:59 +000083
Jakob Stoklund Olesen916f48a2010-02-05 22:03:18 +000084namespace {
Marcello Maggioni5bbe3df2014-10-14 01:58:26 +000085 // The first field contains the value that the switch produces when a certain
Sanjay Patel9361d352015-09-10 16:31:19 +000086 // case group is selected, and the second field is a vector containing the
87 // cases composing the case group.
Marcello Maggioni5bbe3df2014-10-14 01:58:26 +000088 typedef SmallVector<std::pair<Constant *, SmallVector<ConstantInt *, 4>>, 2>
89 SwitchCaseResultVectorTy;
90 // The first field contains the phi node that generates a result of the switch
Sanjay Patel9361d352015-09-10 16:31:19 +000091 // and the second field contains the value generated for a certain case in the
92 // switch for that PHI.
Marcello Maggioni5bbe3df2014-10-14 01:58:26 +000093 typedef SmallVector<std::pair<PHINode *, Constant *>, 4> SwitchCaseResultsTy;
94
Eric Christopherb65acc62012-07-02 23:22:21 +000095 /// ValueEqualityComparisonCase - Represents a case of a switch.
96 struct ValueEqualityComparisonCase {
97 ConstantInt *Value;
98 BasicBlock *Dest;
99
100 ValueEqualityComparisonCase(ConstantInt *Value, BasicBlock *Dest)
101 : Value(Value), Dest(Dest) {}
102
103 bool operator<(ValueEqualityComparisonCase RHS) const {
104 // Comparing pointers is ok as we only rely on the order for uniquing.
105 return Value < RHS.Value;
106 }
Benjamin Kramerc5b06782012-10-14 11:15:42 +0000107
108 bool operator==(BasicBlock *RHSDest) const { return Dest == RHSDest; }
Eric Christopherb65acc62012-07-02 23:22:21 +0000109 };
110
Jakob Stoklund Olesen916f48a2010-02-05 22:03:18 +0000111class SimplifyCFGOpt {
Chandler Carruth0b4ef9c2013-01-07 03:53:25 +0000112 const TargetTransformInfo &TTI;
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000113 const DataLayout &DL;
Jingyue Wufc029672014-09-30 22:23:38 +0000114 unsigned BonusInstThreshold;
Chandler Carruth66b31302015-01-04 12:03:27 +0000115 AssumptionCache *AC;
Jakob Stoklund Olesen916f48a2010-02-05 22:03:18 +0000116 Value *isValueEqualityComparison(TerminatorInst *TI);
117 BasicBlock *GetValueEqualityComparisonCases(TerminatorInst *TI,
Eric Christopherb65acc62012-07-02 23:22:21 +0000118 std::vector<ValueEqualityComparisonCase> &Cases);
Jakob Stoklund Olesen916f48a2010-02-05 22:03:18 +0000119 bool SimplifyEqualityComparisonWithOnlyPredecessor(TerminatorInst *TI,
Devang Patela7ec47d2011-05-18 20:35:38 +0000120 BasicBlock *Pred,
121 IRBuilder<> &Builder);
Devang Patel58380552011-05-18 20:53:17 +0000122 bool FoldValueComparisonIntoPredecessors(TerminatorInst *TI,
123 IRBuilder<> &Builder);
Jakob Stoklund Olesen916f48a2010-02-05 22:03:18 +0000124
Devang Pateldd14e0f2011-05-18 21:33:11 +0000125 bool SimplifyReturn(ReturnInst *RI, IRBuilder<> &Builder);
Bill Wendlingd5d95b02012-02-06 21:16:41 +0000126 bool SimplifyResume(ResumeInst *RI, IRBuilder<> &Builder);
Andrew Kaylor50e4e862015-09-04 23:39:40 +0000127 bool SimplifyCleanupReturn(CleanupReturnInst *RI);
Chris Lattner25c3af32010-12-13 06:25:44 +0000128 bool SimplifyUnreachable(UnreachableInst *UI);
Devang Patela7ec47d2011-05-18 20:35:38 +0000129 bool SimplifySwitch(SwitchInst *SI, IRBuilder<> &Builder);
Chris Lattner25c3af32010-12-13 06:25:44 +0000130 bool SimplifyIndirectBr(IndirectBrInst *IBI);
Devang Patel767f6932011-05-18 18:28:48 +0000131 bool SimplifyUncondBranch(BranchInst *BI, IRBuilder <> &Builder);
Devang Patela7ec47d2011-05-18 20:35:38 +0000132 bool SimplifyCondBranch(BranchInst *BI, IRBuilder <>&Builder);
Chris Lattner25c3af32010-12-13 06:25:44 +0000133
Jakob Stoklund Olesen916f48a2010-02-05 22:03:18 +0000134public:
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000135 SimplifyCFGOpt(const TargetTransformInfo &TTI, const DataLayout &DL,
136 unsigned BonusInstThreshold, AssumptionCache *AC)
137 : TTI(TTI), DL(DL), BonusInstThreshold(BonusInstThreshold), AC(AC) {}
Jakob Stoklund Olesen916f48a2010-02-05 22:03:18 +0000138 bool run(BasicBlock *BB);
139};
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000140}
Jakob Stoklund Olesen916f48a2010-02-05 22:03:18 +0000141
Sanjay Patel09159b8f2015-06-24 20:40:57 +0000142/// Return true if it is safe to merge these two
Chris Lattner76dc2042005-08-03 00:19:45 +0000143/// terminator instructions together.
Chris Lattner76dc2042005-08-03 00:19:45 +0000144static bool SafeToMergeTerminators(TerminatorInst *SI1, TerminatorInst *SI2) {
145 if (SI1 == SI2) return false; // Can't merge with self!
Andrew Trickf3cf1932012-08-29 21:46:36 +0000146
Chris Lattner76dc2042005-08-03 00:19:45 +0000147 // It is not safe to merge these two switch instructions if they have a common
148 // successor, and if that successor has a PHI node, and if *that* PHI node has
149 // conflicting incoming values from the two switch blocks.
150 BasicBlock *SI1BB = SI1->getParent();
151 BasicBlock *SI2BB = SI2->getParent();
Chris Lattnerb7b75142007-04-02 01:44:59 +0000152 SmallPtrSet<BasicBlock*, 16> SI1Succs(succ_begin(SI1BB), succ_end(SI1BB));
Andrew Trickf3cf1932012-08-29 21:46:36 +0000153
Duncan P. N. Exon Smith6c990152014-07-21 17:06:51 +0000154 for (succ_iterator I = succ_begin(SI2BB), E = succ_end(SI2BB); I != E; ++I)
155 if (SI1Succs.count(*I))
156 for (BasicBlock::iterator BBI = (*I)->begin();
Chris Lattner76dc2042005-08-03 00:19:45 +0000157 isa<PHINode>(BBI); ++BBI) {
158 PHINode *PN = cast<PHINode>(BBI);
159 if (PN->getIncomingValueForBlock(SI1BB) !=
160 PN->getIncomingValueForBlock(SI2BB))
161 return false;
162 }
Andrew Trickf3cf1932012-08-29 21:46:36 +0000163
Chris Lattner76dc2042005-08-03 00:19:45 +0000164 return true;
165}
166
Sanjay Patel09159b8f2015-06-24 20:40:57 +0000167/// Return true if it is safe and profitable to merge these two terminator
168/// instructions together, where SI1 is an unconditional branch. PhiNodes will
169/// store all PHI nodes in common successors.
Manman Rend33f4ef2012-06-13 05:43:29 +0000170static bool isProfitableToFoldUnconditional(BranchInst *SI1,
171 BranchInst *SI2,
Nick Lewycky0a045bb2012-06-24 10:15:42 +0000172 Instruction *Cond,
Manman Rend33f4ef2012-06-13 05:43:29 +0000173 SmallVectorImpl<PHINode*> &PhiNodes) {
174 if (SI1 == SI2) return false; // Can't merge with self!
175 assert(SI1->isUnconditional() && SI2->isConditional());
176
177 // We fold the unconditional branch if we can easily update all PHI nodes in
Andrew Trickf3cf1932012-08-29 21:46:36 +0000178 // common successors:
Manman Rend33f4ef2012-06-13 05:43:29 +0000179 // 1> We have a constant incoming value for the conditional branch;
180 // 2> We have "Cond" as the incoming value for the unconditional branch;
181 // 3> SI2->getCondition() and Cond have same operands.
182 CmpInst *Ci2 = dyn_cast<CmpInst>(SI2->getCondition());
183 if (!Ci2) return false;
184 if (!(Cond->getOperand(0) == Ci2->getOperand(0) &&
185 Cond->getOperand(1) == Ci2->getOperand(1)) &&
186 !(Cond->getOperand(0) == Ci2->getOperand(1) &&
187 Cond->getOperand(1) == Ci2->getOperand(0)))
188 return false;
189
190 BasicBlock *SI1BB = SI1->getParent();
191 BasicBlock *SI2BB = SI2->getParent();
192 SmallPtrSet<BasicBlock*, 16> SI1Succs(succ_begin(SI1BB), succ_end(SI1BB));
Duncan P. N. Exon Smith6c990152014-07-21 17:06:51 +0000193 for (succ_iterator I = succ_begin(SI2BB), E = succ_end(SI2BB); I != E; ++I)
194 if (SI1Succs.count(*I))
195 for (BasicBlock::iterator BBI = (*I)->begin();
Manman Rend33f4ef2012-06-13 05:43:29 +0000196 isa<PHINode>(BBI); ++BBI) {
197 PHINode *PN = cast<PHINode>(BBI);
198 if (PN->getIncomingValueForBlock(SI1BB) != Cond ||
Nick Lewycky0a045bb2012-06-24 10:15:42 +0000199 !isa<ConstantInt>(PN->getIncomingValueForBlock(SI2BB)))
Manman Rend33f4ef2012-06-13 05:43:29 +0000200 return false;
201 PhiNodes.push_back(PN);
202 }
203 return true;
204}
205
Sanjay Patel09159b8f2015-06-24 20:40:57 +0000206/// Update PHI nodes in Succ to indicate that there will now be entries in it
207/// from the 'NewPred' block. The values that will be flowing into the PHI nodes
208/// will be the same as those coming in from ExistPred, an existing predecessor
209/// of Succ.
Chris Lattner76dc2042005-08-03 00:19:45 +0000210static void AddPredecessorToBlock(BasicBlock *Succ, BasicBlock *NewPred,
211 BasicBlock *ExistPred) {
Chris Lattner76dc2042005-08-03 00:19:45 +0000212 if (!isa<PHINode>(Succ->begin())) return; // Quick exit if nothing to do
Andrew Trickf3cf1932012-08-29 21:46:36 +0000213
Chris Lattner80b03a12008-07-13 22:23:11 +0000214 PHINode *PN;
215 for (BasicBlock::iterator I = Succ->begin();
216 (PN = dyn_cast<PHINode>(I)); ++I)
217 PN->addIncoming(PN->getIncomingValueForBlock(ExistPred), NewPred);
Chris Lattner76dc2042005-08-03 00:19:45 +0000218}
219
Sanjay Patel09159b8f2015-06-24 20:40:57 +0000220/// Compute an abstract "cost" of speculating the given instruction,
221/// which is assumed to be safe to speculate. TCC_Free means cheap,
222/// TCC_Basic means less cheap, and TCC_Expensive means prohibitively
James Molloy7c336572015-02-11 12:15:41 +0000223/// expensive.
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000224static unsigned ComputeSpeculationCost(const User *I,
James Molloy7c336572015-02-11 12:15:41 +0000225 const TargetTransformInfo &TTI) {
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000226 assert(isSafeToSpeculativelyExecute(I) &&
Dan Gohman5ab9c0a2012-01-05 23:58:56 +0000227 "Instruction is not safe to speculatively execute!");
James Molloy7c336572015-02-11 12:15:41 +0000228 return TTI.getUserCost(I);
Dan Gohman5ab9c0a2012-01-05 23:58:56 +0000229}
Sanjay Patelf9b77632015-09-15 15:24:42 +0000230
Sanjay Patel09159b8f2015-06-24 20:40:57 +0000231/// If we have a merge point of an "if condition" as accepted above,
232/// return true if the specified value dominates the block. We
Bill Wendlingcaf1d222009-01-19 23:43:56 +0000233/// don't handle the true generality of domination here, just a special case
234/// which works well enough for us.
235///
236/// If AggressiveInsts is non-null, and if V does not dominate BB, we check to
Peter Collingbournee3511e12011-04-29 18:47:31 +0000237/// see if V (which must be an instruction) and its recursive operands
238/// that do not dominate BB have a combined cost lower than CostRemaining and
239/// are non-trapping. If both are true, the instruction is inserted into the
240/// set and true is returned.
241///
242/// The cost for most non-trapping instructions is defined as 1 except for
243/// Select whose cost is 2.
244///
245/// After this function returns, CostRemaining is decreased by the cost of
246/// V plus its non-dominating operands. If that cost is greater than
247/// CostRemaining, false is returned and CostRemaining is undefined.
Chris Lattner45c35b12004-10-14 05:13:36 +0000248static bool DominatesMergePoint(Value *V, BasicBlock *BB,
Craig Topper71b7b682014-08-21 05:55:13 +0000249 SmallPtrSetImpl<Instruction*> *AggressiveInsts,
Hal Finkela995f922014-07-10 14:41:31 +0000250 unsigned &CostRemaining,
James Molloy7c336572015-02-11 12:15:41 +0000251 const TargetTransformInfo &TTI) {
Chris Lattner0aa56562004-04-09 22:50:22 +0000252 Instruction *I = dyn_cast<Instruction>(V);
Chris Lattnerb8b11592006-10-20 00:42:07 +0000253 if (!I) {
254 // Non-instructions all dominate instructions, but not all constantexprs
255 // can be executed unconditionally.
256 if (ConstantExpr *C = dyn_cast<ConstantExpr>(V))
257 if (C->canTrap())
258 return false;
259 return true;
260 }
Chris Lattner0aa56562004-04-09 22:50:22 +0000261 BasicBlock *PBB = I->getParent();
Chris Lattner18d1f192004-02-11 03:36:04 +0000262
Chris Lattner0ce80cd2005-02-27 06:18:25 +0000263 // We don't want to allow weird loops that might have the "if condition" in
Chris Lattner0aa56562004-04-09 22:50:22 +0000264 // the bottom of this block.
265 if (PBB == BB) return false;
Chris Lattner18d1f192004-02-11 03:36:04 +0000266
Chris Lattner0aa56562004-04-09 22:50:22 +0000267 // If this instruction is defined in a block that contains an unconditional
268 // branch to BB, then it must be in the 'conditional' part of the "if
Chris Lattner9ac168d2010-12-14 07:41:39 +0000269 // statement". If not, it definitely dominates the region.
270 BranchInst *BI = dyn_cast<BranchInst>(PBB->getTerminator());
Craig Topperf40110f2014-04-25 05:29:35 +0000271 if (!BI || BI->isConditional() || BI->getSuccessor(0) != BB)
Chris Lattner9ac168d2010-12-14 07:41:39 +0000272 return true;
Eli Friedmanb8f6a4f2009-07-17 04:28:42 +0000273
Chris Lattner9ac168d2010-12-14 07:41:39 +0000274 // If we aren't allowing aggressive promotion anymore, then don't consider
275 // instructions in the 'if region'.
Craig Topperf40110f2014-04-25 05:29:35 +0000276 if (!AggressiveInsts) return false;
Andrew Trickf3cf1932012-08-29 21:46:36 +0000277
Peter Collingbournee3511e12011-04-29 18:47:31 +0000278 // If we have seen this instruction before, don't count it again.
279 if (AggressiveInsts->count(I)) return true;
280
Chris Lattner9ac168d2010-12-14 07:41:39 +0000281 // Okay, it looks like the instruction IS in the "condition". Check to
282 // see if it's a cheap instruction to unconditionally compute, and if it
283 // only uses stuff defined outside of the condition. If so, hoist it out.
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000284 if (!isSafeToSpeculativelyExecute(I))
Chris Lattner9ac168d2010-12-14 07:41:39 +0000285 return false;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000286
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000287 unsigned Cost = ComputeSpeculationCost(I, TTI);
Chris Lattner0aa56562004-04-09 22:50:22 +0000288
Peter Collingbournee3511e12011-04-29 18:47:31 +0000289 if (Cost > CostRemaining)
290 return false;
291
292 CostRemaining -= Cost;
293
294 // Okay, we can only really hoist these out if their operands do
295 // not take us over the cost threshold.
Chris Lattner9ac168d2010-12-14 07:41:39 +0000296 for (User::op_iterator i = I->op_begin(), e = I->op_end(); i != e; ++i)
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000297 if (!DominatesMergePoint(*i, BB, AggressiveInsts, CostRemaining, TTI))
Chris Lattner9ac168d2010-12-14 07:41:39 +0000298 return false;
299 // Okay, it's safe to do this! Remember this instruction.
300 AggressiveInsts->insert(I);
Chris Lattner18d1f192004-02-11 03:36:04 +0000301 return true;
302}
Chris Lattner466a0492002-05-21 20:50:24 +0000303
Sanjay Patel09159b8f2015-06-24 20:40:57 +0000304/// Extract ConstantInt from value, looking through IntToPtr
Jakob Stoklund Olesen916f48a2010-02-05 22:03:18 +0000305/// and PointerNullValue. Return NULL if value is not a constant int.
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000306static ConstantInt *GetConstantInt(Value *V, const DataLayout &DL) {
Jakob Stoklund Olesen916f48a2010-02-05 22:03:18 +0000307 // Normal constant int.
308 ConstantInt *CI = dyn_cast<ConstantInt>(V);
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000309 if (CI || !isa<Constant>(V) || !V->getType()->isPointerTy())
Jakob Stoklund Olesen916f48a2010-02-05 22:03:18 +0000310 return CI;
311
312 // This is some kind of pointer constant. Turn it into a pointer-sized
313 // ConstantInt if possible.
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000314 IntegerType *PtrTy = cast<IntegerType>(DL.getIntPtrType(V->getType()));
Jakob Stoklund Olesen916f48a2010-02-05 22:03:18 +0000315
316 // Null pointer means 0, see SelectionDAGBuilder::getValue(const Value*).
317 if (isa<ConstantPointerNull>(V))
318 return ConstantInt::get(PtrTy, 0);
319
320 // IntToPtr const int.
321 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
322 if (CE->getOpcode() == Instruction::IntToPtr)
323 if (ConstantInt *CI = dyn_cast<ConstantInt>(CE->getOperand(0))) {
324 // The constant is very likely to have the right type already.
325 if (CI->getType() == PtrTy)
326 return CI;
327 else
328 return cast<ConstantInt>
329 (ConstantExpr::getIntegerCast(CI, PtrTy, /*isSigned=*/false));
330 }
Craig Topperf40110f2014-04-25 05:29:35 +0000331 return nullptr;
Jakob Stoklund Olesen916f48a2010-02-05 22:03:18 +0000332}
333
Mehdi Aminiffd01002014-11-20 22:40:25 +0000334namespace {
Mehdi Amini9a25cb82014-11-19 20:09:11 +0000335
Mehdi Aminiffd01002014-11-20 22:40:25 +0000336/// Given a chain of or (||) or and (&&) comparison of a value against a
337/// constant, this will try to recover the information required for a switch
338/// structure.
339/// It will depth-first traverse the chain of comparison, seeking for patterns
340/// like %a == 12 or %a < 4 and combine them to produce a set of integer
341/// representing the different cases for the switch.
342/// Note that if the chain is composed of '||' it will build the set of elements
343/// that matches the comparisons (i.e. any of this value validate the chain)
344/// while for a chain of '&&' it will build the set elements that make the test
345/// fail.
346struct ConstantComparesGatherer {
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000347 const DataLayout &DL;
Mehdi Aminiffd01002014-11-20 22:40:25 +0000348 Value *CompValue; /// Value found for the switch comparison
349 Value *Extra; /// Extra clause to be checked before the switch
350 SmallVector<ConstantInt *, 8> Vals; /// Set of integers to match in switch
351 unsigned UsedICmps; /// Number of comparisons matched in the and/or chain
Timur Iskhodzhanov71526a32014-11-20 12:36:43 +0000352
Mehdi Aminiffd01002014-11-20 22:40:25 +0000353 /// Construct and compute the result for the comparison instruction Cond
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000354 ConstantComparesGatherer(Instruction *Cond, const DataLayout &DL)
355 : DL(DL), CompValue(nullptr), Extra(nullptr), UsedICmps(0) {
356 gather(Cond);
Mehdi Amini9a25cb82014-11-19 20:09:11 +0000357 }
358
Mehdi Aminiffd01002014-11-20 22:40:25 +0000359 /// Prevent copy
Aaron Ballmanf9a18972015-02-15 22:54:22 +0000360 ConstantComparesGatherer(const ConstantComparesGatherer &) = delete;
Mehdi Aminiffd01002014-11-20 22:40:25 +0000361 ConstantComparesGatherer &
Aaron Ballmanf9a18972015-02-15 22:54:22 +0000362 operator=(const ConstantComparesGatherer &) = delete;
Mehdi Amini9a25cb82014-11-19 20:09:11 +0000363
Mehdi Aminiffd01002014-11-20 22:40:25 +0000364private:
Mehdi Amini9a25cb82014-11-19 20:09:11 +0000365
Mehdi Aminiffd01002014-11-20 22:40:25 +0000366 /// Try to set the current value used for the comparison, it succeeds only if
367 /// it wasn't set before or if the new value is the same as the old one
368 bool setValueOnce(Value *NewVal) {
369 if(CompValue && CompValue != NewVal) return false;
370 CompValue = NewVal;
371 return (CompValue != nullptr);
372 }
373
374 /// Try to match Instruction "I" as a comparison against a constant and
375 /// populates the array Vals with the set of values that match (or do not
376 /// match depending on isEQ).
377 /// Return false on failure. On success, the Value the comparison matched
378 /// against is placed in CompValue.
379 /// If CompValue is already set, the function is expected to fail if a match
380 /// is found but the value compared to is different.
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000381 bool matchInstruction(Instruction *I, bool isEQ) {
Mehdi Aminiffd01002014-11-20 22:40:25 +0000382 // If this is an icmp against a constant, handle this as one of the cases.
383 ICmpInst *ICI;
384 ConstantInt *C;
385 if (!((ICI = dyn_cast<ICmpInst>(I)) &&
386 (C = GetConstantInt(I->getOperand(1), DL)))) {
387 return false;
388 }
389
390 Value *RHSVal;
391 ConstantInt *RHSC;
392
393 // Pattern match a special case
394 // (x & ~2^x) == y --> x == y || x == y|2^x
395 // This undoes a transformation done by instcombine to fuse 2 compares.
396 if (ICI->getPredicate() == (isEQ ? ICmpInst::ICMP_EQ:ICmpInst::ICMP_NE)) {
397 if (match(ICI->getOperand(0),
398 m_And(m_Value(RHSVal), m_ConstantInt(RHSC)))) {
399 APInt Not = ~RHSC->getValue();
400 if (Not.isPowerOf2()) {
401 // If we already have a value for the switch, it has to match!
402 if(!setValueOnce(RHSVal))
403 return false;
404
405 Vals.push_back(C);
406 Vals.push_back(ConstantInt::get(C->getContext(),
407 C->getValue() | Not));
408 UsedICmps++;
409 return true;
410 }
Mehdi Amini9a25cb82014-11-19 20:09:11 +0000411 }
Mehdi Aminiffd01002014-11-20 22:40:25 +0000412
413 // If we already have a value for the switch, it has to match!
414 if(!setValueOnce(ICI->getOperand(0)))
415 return false;
416
417 UsedICmps++;
418 Vals.push_back(C);
419 return ICI->getOperand(0);
420 }
421
422 // If we have "x ult 3", for example, then we can add 0,1,2 to the set.
Sanjoy Das7182d362015-03-18 00:41:24 +0000423 ConstantRange Span = ConstantRange::makeAllowedICmpRegion(
424 ICI->getPredicate(), C->getValue());
Mehdi Aminiffd01002014-11-20 22:40:25 +0000425
426 // Shift the range if the compare is fed by an add. This is the range
427 // compare idiom as emitted by instcombine.
428 Value *CandidateVal = I->getOperand(0);
429 if(match(I->getOperand(0), m_Add(m_Value(RHSVal), m_ConstantInt(RHSC)))) {
430 Span = Span.subtract(RHSC->getValue());
431 CandidateVal = RHSVal;
432 }
433
434 // If this is an and/!= check, then we are looking to build the set of
435 // value that *don't* pass the and chain. I.e. to turn "x ugt 2" into
436 // x != 0 && x != 1.
437 if (!isEQ)
438 Span = Span.inverse();
439
440 // If there are a ton of values, we don't want to make a ginormous switch.
441 if (Span.getSetSize().ugt(8) || Span.isEmptySet()) {
442 return false;
Mehdi Amini9a25cb82014-11-19 20:09:11 +0000443 }
444
445 // If we already have a value for the switch, it has to match!
Mehdi Aminiffd01002014-11-20 22:40:25 +0000446 if(!setValueOnce(CandidateVal))
447 return false;
448
449 // Add all values from the range to the set
450 for (APInt Tmp = Span.getLower(); Tmp != Span.getUpper(); ++Tmp)
451 Vals.push_back(ConstantInt::get(I->getContext(), Tmp));
Mehdi Amini9a25cb82014-11-19 20:09:11 +0000452
453 UsedICmps++;
Mehdi Aminiffd01002014-11-20 22:40:25 +0000454 return true;
455
Mehdi Amini9a25cb82014-11-19 20:09:11 +0000456 }
457
Sanjay Patel09159b8f2015-06-24 20:40:57 +0000458 /// Given a potentially 'or'd or 'and'd together collection of icmp
Mehdi Aminiffd01002014-11-20 22:40:25 +0000459 /// eq/ne/lt/gt instructions that compare a value against a constant, extract
460 /// the value being compared, and stick the list constants into the Vals
461 /// vector.
462 /// One "Extra" case is allowed to differ from the other.
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000463 void gather(Value *V) {
Mehdi Aminiffd01002014-11-20 22:40:25 +0000464 Instruction *I = dyn_cast<Instruction>(V);
465 bool isEQ = (I->getOpcode() == Instruction::Or);
Mehdi Amini9a25cb82014-11-19 20:09:11 +0000466
Mehdi Aminiffd01002014-11-20 22:40:25 +0000467 // Keep a stack (SmallVector for efficiency) for depth-first traversal
468 SmallVector<Value *, 8> DFT;
Mehdi Amini9a25cb82014-11-19 20:09:11 +0000469
Mehdi Aminiffd01002014-11-20 22:40:25 +0000470 // Initialize
471 DFT.push_back(V);
Mehdi Amini9a25cb82014-11-19 20:09:11 +0000472
Mehdi Aminiffd01002014-11-20 22:40:25 +0000473 while(!DFT.empty()) {
474 V = DFT.pop_back_val();
Mehdi Amini9a25cb82014-11-19 20:09:11 +0000475
Mehdi Aminiffd01002014-11-20 22:40:25 +0000476 if (Instruction *I = dyn_cast<Instruction>(V)) {
477 // If it is a || (or && depending on isEQ), process the operands.
478 if (I->getOpcode() == (isEQ ? Instruction::Or : Instruction::And)) {
479 DFT.push_back(I->getOperand(1));
480 DFT.push_back(I->getOperand(0));
481 continue;
482 }
Mehdi Amini9a25cb82014-11-19 20:09:11 +0000483
Mehdi Aminiffd01002014-11-20 22:40:25 +0000484 // Try to match the current instruction
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000485 if (matchInstruction(I, isEQ))
Mehdi Aminiffd01002014-11-20 22:40:25 +0000486 // Match succeed, continue the loop
487 continue;
Mehdi Amini9a25cb82014-11-19 20:09:11 +0000488 }
Timur Iskhodzhanov71526a32014-11-20 12:36:43 +0000489
Mehdi Aminiffd01002014-11-20 22:40:25 +0000490 // One element of the sequence of || (or &&) could not be match as a
491 // comparison against the same value as the others.
492 // We allow only one "Extra" case to be checked before the switch
493 if (!Extra) {
494 Extra = V;
Timur Iskhodzhanov71526a32014-11-20 12:36:43 +0000495 continue;
496 }
Mehdi Aminiffd01002014-11-20 22:40:25 +0000497 // Failed to parse a proper sequence, abort now
498 CompValue = nullptr;
499 break;
Chris Lattner5a177e62010-12-13 04:26:26 +0000500 }
Anton Korobeynikov1bfd1212008-02-20 11:26:25 +0000501 }
Mehdi Aminiffd01002014-11-20 22:40:25 +0000502};
Timur Iskhodzhanov71526a32014-11-20 12:36:43 +0000503
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000504}
Nick Lewyckye87d54c2011-12-26 20:37:40 +0000505
Eli Friedmancb61afb2008-12-16 20:54:32 +0000506static void EraseTerminatorInstAndDCECond(TerminatorInst *TI) {
Craig Topperf40110f2014-04-25 05:29:35 +0000507 Instruction *Cond = nullptr;
Eli Friedmancb61afb2008-12-16 20:54:32 +0000508 if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) {
509 Cond = dyn_cast<Instruction>(SI->getCondition());
510 } else if (BranchInst *BI = dyn_cast<BranchInst>(TI)) {
511 if (BI->isConditional())
512 Cond = dyn_cast<Instruction>(BI->getCondition());
Frits van Bommel8fb69ee2010-12-05 18:29:03 +0000513 } else if (IndirectBrInst *IBI = dyn_cast<IndirectBrInst>(TI)) {
514 Cond = dyn_cast<Instruction>(IBI->getAddress());
Eli Friedmancb61afb2008-12-16 20:54:32 +0000515 }
516
517 TI->eraseFromParent();
518 if (Cond) RecursivelyDeleteTriviallyDeadInstructions(Cond);
519}
520
Sanjay Patel09159b8f2015-06-24 20:40:57 +0000521/// Return true if the specified terminator checks
Chris Lattner8e84c122008-11-27 23:25:44 +0000522/// to see if a value is equal to constant integer value.
Jakob Stoklund Olesen916f48a2010-02-05 22:03:18 +0000523Value *SimplifyCFGOpt::isValueEqualityComparison(TerminatorInst *TI) {
Craig Topperf40110f2014-04-25 05:29:35 +0000524 Value *CV = nullptr;
Chris Lattnera64923a2004-03-16 19:45:22 +0000525 if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) {
526 // Do not permit merging of large switch instructions into their
527 // predecessors unless there is only one predecessor.
Jakob Stoklund Olesen916f48a2010-02-05 22:03:18 +0000528 if (SI->getNumSuccessors()*std::distance(pred_begin(SI->getParent()),
529 pred_end(SI->getParent())) <= 128)
530 CV = SI->getCondition();
531 } else if (BranchInst *BI = dyn_cast<BranchInst>(TI))
Chris Lattnerd3e6ae22004-02-28 21:28:10 +0000532 if (BI->isConditional() && BI->getCondition()->hasOneUse())
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000533 if (ICmpInst *ICI = dyn_cast<ICmpInst>(BI->getCondition())) {
Rafael Espindola37dc9e12014-02-21 00:06:31 +0000534 if (ICI->isEquality() && GetConstantInt(ICI->getOperand(1), DL))
Jakob Stoklund Olesen916f48a2010-02-05 22:03:18 +0000535 CV = ICI->getOperand(0);
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000536 }
Jakob Stoklund Olesen916f48a2010-02-05 22:03:18 +0000537
538 // Unwrap any lossless ptrtoint cast.
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000539 if (CV) {
Matt Arsenaultfa646592013-10-21 18:55:08 +0000540 if (PtrToIntInst *PTII = dyn_cast<PtrToIntInst>(CV)) {
541 Value *Ptr = PTII->getPointerOperand();
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000542 if (PTII->getType() == DL.getIntPtrType(Ptr->getType()))
Matt Arsenaultfa646592013-10-21 18:55:08 +0000543 CV = Ptr;
544 }
545 }
Jakob Stoklund Olesen916f48a2010-02-05 22:03:18 +0000546 return CV;
Chris Lattnerd3e6ae22004-02-28 21:28:10 +0000547}
548
Sanjay Patel09159b8f2015-06-24 20:40:57 +0000549/// Given a value comparison instruction,
Bill Wendlingcaf1d222009-01-19 23:43:56 +0000550/// decode all of the 'cases' that it represents and return the 'default' block.
Jakob Stoklund Olesen916f48a2010-02-05 22:03:18 +0000551BasicBlock *SimplifyCFGOpt::
Misha Brukmanb1c93172005-04-21 23:48:37 +0000552GetValueEqualityComparisonCases(TerminatorInst *TI,
Eric Christopherb65acc62012-07-02 23:22:21 +0000553 std::vector<ValueEqualityComparisonCase>
554 &Cases) {
Chris Lattnerd3e6ae22004-02-28 21:28:10 +0000555 if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) {
Eric Christopherb65acc62012-07-02 23:22:21 +0000556 Cases.reserve(SI->getNumCases());
557 for (SwitchInst::CaseIt i = SI->case_begin(), e = SI->case_end(); i != e; ++i)
558 Cases.push_back(ValueEqualityComparisonCase(i.getCaseValue(),
559 i.getCaseSuccessor()));
Chris Lattnerd3e6ae22004-02-28 21:28:10 +0000560 return SI->getDefaultDest();
561 }
Eric Christopherb65acc62012-07-02 23:22:21 +0000562
Chris Lattnerd3e6ae22004-02-28 21:28:10 +0000563 BranchInst *BI = cast<BranchInst>(TI);
Reid Spencer266e42b2006-12-23 06:05:41 +0000564 ICmpInst *ICI = cast<ICmpInst>(BI->getCondition());
Eric Christopherb65acc62012-07-02 23:22:21 +0000565 BasicBlock *Succ = BI->getSuccessor(ICI->getPredicate() == ICmpInst::ICMP_NE);
566 Cases.push_back(ValueEqualityComparisonCase(GetConstantInt(ICI->getOperand(1),
Rafael Espindola37dc9e12014-02-21 00:06:31 +0000567 DL),
Eric Christopherb65acc62012-07-02 23:22:21 +0000568 Succ));
Reid Spencer266e42b2006-12-23 06:05:41 +0000569 return BI->getSuccessor(ICI->getPredicate() == ICmpInst::ICMP_EQ);
Chris Lattnerd3e6ae22004-02-28 21:28:10 +0000570}
571
Eric Christopherb65acc62012-07-02 23:22:21 +0000572
Sanjay Patel09159b8f2015-06-24 20:40:57 +0000573/// Given a vector of bb/value pairs, remove any entries
Eric Christopherb65acc62012-07-02 23:22:21 +0000574/// in the list that match the specified block.
575static void EliminateBlockCases(BasicBlock *BB,
576 std::vector<ValueEqualityComparisonCase> &Cases) {
Benjamin Kramerc5b06782012-10-14 11:15:42 +0000577 Cases.erase(std::remove(Cases.begin(), Cases.end(), BB), Cases.end());
Eric Christopherb65acc62012-07-02 23:22:21 +0000578}
579
Sanjay Patel09159b8f2015-06-24 20:40:57 +0000580/// Return true if there are any keys in C1 that exist in C2 as well.
Eric Christopherb65acc62012-07-02 23:22:21 +0000581static bool
582ValuesOverlap(std::vector<ValueEqualityComparisonCase> &C1,
583 std::vector<ValueEqualityComparisonCase > &C2) {
584 std::vector<ValueEqualityComparisonCase> *V1 = &C1, *V2 = &C2;
585
586 // Make V1 be smaller than V2.
587 if (V1->size() > V2->size())
588 std::swap(V1, V2);
589
590 if (V1->size() == 0) return false;
591 if (V1->size() == 1) {
592 // Just scan V2.
593 ConstantInt *TheVal = (*V1)[0].Value;
594 for (unsigned i = 0, e = V2->size(); i != e; ++i)
595 if (TheVal == (*V2)[i].Value)
596 return true;
597 }
598
599 // Otherwise, just sort both lists and compare element by element.
600 array_pod_sort(V1->begin(), V1->end());
601 array_pod_sort(V2->begin(), V2->end());
602 unsigned i1 = 0, i2 = 0, e1 = V1->size(), e2 = V2->size();
603 while (i1 != e1 && i2 != e2) {
604 if ((*V1)[i1].Value == (*V2)[i2].Value)
605 return true;
606 if ((*V1)[i1].Value < (*V2)[i2].Value)
607 ++i1;
608 else
609 ++i2;
610 }
611 return false;
612}
613
Sanjay Patel09159b8f2015-06-24 20:40:57 +0000614/// If TI is known to be a terminator instruction and its block is known to
615/// only have a single predecessor block, check to see if that predecessor is
616/// also a value comparison with the same value, and if that comparison
617/// determines the outcome of this comparison. If so, simplify TI. This does a
618/// very limited form of jump threading.
Jakob Stoklund Olesen916f48a2010-02-05 22:03:18 +0000619bool SimplifyCFGOpt::
620SimplifyEqualityComparisonWithOnlyPredecessor(TerminatorInst *TI,
Devang Patela7ec47d2011-05-18 20:35:38 +0000621 BasicBlock *Pred,
622 IRBuilder<> &Builder) {
Chris Lattner1cca9592005-02-24 06:17:52 +0000623 Value *PredVal = isValueEqualityComparison(Pred->getTerminator());
624 if (!PredVal) return false; // Not a value comparison in predecessor.
625
626 Value *ThisVal = isValueEqualityComparison(TI);
627 assert(ThisVal && "This isn't a value comparison!!");
628 if (ThisVal != PredVal) return false; // Different predicates.
629
Andrew Trick3051aa12012-08-29 21:46:38 +0000630 // TODO: Preserve branch weight metadata, similarly to how
631 // FoldValueComparisonIntoPredecessors preserves it.
632
Chris Lattner1cca9592005-02-24 06:17:52 +0000633 // Find out information about when control will move from Pred to TI's block.
Eric Christopherb65acc62012-07-02 23:22:21 +0000634 std::vector<ValueEqualityComparisonCase> PredCases;
Chris Lattner1cca9592005-02-24 06:17:52 +0000635 BasicBlock *PredDef = GetValueEqualityComparisonCases(Pred->getTerminator(),
636 PredCases);
Eric Christopherb65acc62012-07-02 23:22:21 +0000637 EliminateBlockCases(PredDef, PredCases); // Remove default from cases.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000638
Chris Lattner1cca9592005-02-24 06:17:52 +0000639 // Find information about how control leaves this block.
Eric Christopherb65acc62012-07-02 23:22:21 +0000640 std::vector<ValueEqualityComparisonCase> ThisCases;
Chris Lattner1cca9592005-02-24 06:17:52 +0000641 BasicBlock *ThisDef = GetValueEqualityComparisonCases(TI, ThisCases);
Eric Christopherb65acc62012-07-02 23:22:21 +0000642 EliminateBlockCases(ThisDef, ThisCases); // Remove default from cases.
Chris Lattner1cca9592005-02-24 06:17:52 +0000643
644 // If TI's block is the default block from Pred's comparison, potentially
645 // simplify TI based on this knowledge.
646 if (PredDef == TI->getParent()) {
647 // If we are here, we know that the value is none of those cases listed in
648 // PredCases. If there are any cases in ThisCases that are in PredCases, we
649 // can simplify TI.
Eric Christopherb65acc62012-07-02 23:22:21 +0000650 if (!ValuesOverlap(PredCases, ThisCases))
Chris Lattner4088e2b2010-12-13 01:47:07 +0000651 return false;
Andrew Trickf3cf1932012-08-29 21:46:36 +0000652
Chris Lattner4088e2b2010-12-13 01:47:07 +0000653 if (isa<BranchInst>(TI)) {
654 // Okay, one of the successors of this condbr is dead. Convert it to a
655 // uncond br.
656 assert(ThisCases.size() == 1 && "Branch can only have one case!");
657 // Insert the new branch.
Devang Patela7ec47d2011-05-18 20:35:38 +0000658 Instruction *NI = Builder.CreateBr(ThisDef);
Chris Lattner4088e2b2010-12-13 01:47:07 +0000659 (void) NI;
Chris Lattner1cca9592005-02-24 06:17:52 +0000660
Chris Lattner4088e2b2010-12-13 01:47:07 +0000661 // Remove PHI node entries for the dead edge.
Eric Christopherb65acc62012-07-02 23:22:21 +0000662 ThisCases[0].Dest->removePredecessor(TI->getParent());
Chris Lattner1cca9592005-02-24 06:17:52 +0000663
Chris Lattner4088e2b2010-12-13 01:47:07 +0000664 DEBUG(dbgs() << "Threading pred instr: " << *Pred->getTerminator()
665 << "Through successor TI: " << *TI << "Leaving: " << *NI << "\n");
Chris Lattner1cca9592005-02-24 06:17:52 +0000666
Chris Lattner4088e2b2010-12-13 01:47:07 +0000667 EraseTerminatorInstAndDCECond(TI);
668 return true;
Chris Lattner1cca9592005-02-24 06:17:52 +0000669 }
Andrew Trickf3cf1932012-08-29 21:46:36 +0000670
Chris Lattner4088e2b2010-12-13 01:47:07 +0000671 SwitchInst *SI = cast<SwitchInst>(TI);
672 // Okay, TI has cases that are statically dead, prune them away.
Eric Christopherb65acc62012-07-02 23:22:21 +0000673 SmallPtrSet<Constant*, 16> DeadCases;
674 for (unsigned i = 0, e = PredCases.size(); i != e; ++i)
675 DeadCases.insert(PredCases[i].Value);
Chris Lattner1cca9592005-02-24 06:17:52 +0000676
David Greene725c7c32010-01-05 01:26:52 +0000677 DEBUG(dbgs() << "Threading pred instr: " << *Pred->getTerminator()
Chris Lattner4088e2b2010-12-13 01:47:07 +0000678 << "Through successor TI: " << *TI);
Chris Lattner1cca9592005-02-24 06:17:52 +0000679
Manman Ren8691e522012-09-14 21:53:06 +0000680 // Collect branch weights into a vector.
681 SmallVector<uint32_t, 8> Weights;
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +0000682 MDNode *MD = SI->getMetadata(LLVMContext::MD_prof);
Manman Ren8691e522012-09-14 21:53:06 +0000683 bool HasWeight = MD && (MD->getNumOperands() == 2 + SI->getNumCases());
684 if (HasWeight)
685 for (unsigned MD_i = 1, MD_e = MD->getNumOperands(); MD_i < MD_e;
686 ++MD_i) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000687 ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(MD_i));
Manman Ren8691e522012-09-14 21:53:06 +0000688 Weights.push_back(CI->getValue().getZExtValue());
689 }
Eric Christopherb65acc62012-07-02 23:22:21 +0000690 for (SwitchInst::CaseIt i = SI->case_end(), e = SI->case_begin(); i != e;) {
691 --i;
692 if (DeadCases.count(i.getCaseValue())) {
Manman Ren8691e522012-09-14 21:53:06 +0000693 if (HasWeight) {
694 std::swap(Weights[i.getCaseIndex()+1], Weights.back());
695 Weights.pop_back();
696 }
Eric Christopherb65acc62012-07-02 23:22:21 +0000697 i.getCaseSuccessor()->removePredecessor(TI->getParent());
698 SI->removeCase(i);
699 }
700 }
Manman Ren97c18762012-10-11 22:28:34 +0000701 if (HasWeight && Weights.size() >= 2)
Manman Ren8691e522012-09-14 21:53:06 +0000702 SI->setMetadata(LLVMContext::MD_prof,
703 MDBuilder(SI->getParent()->getContext()).
704 createBranchWeights(Weights));
Eric Christopherb65acc62012-07-02 23:22:21 +0000705
706 DEBUG(dbgs() << "Leaving: " << *TI << "\n");
Chris Lattner1cca9592005-02-24 06:17:52 +0000707 return true;
708 }
Andrew Trickf3cf1932012-08-29 21:46:36 +0000709
Chris Lattner4088e2b2010-12-13 01:47:07 +0000710 // Otherwise, TI's block must correspond to some matched value. Find out
711 // which value (or set of values) this is.
Craig Topperf40110f2014-04-25 05:29:35 +0000712 ConstantInt *TIV = nullptr;
Chris Lattner4088e2b2010-12-13 01:47:07 +0000713 BasicBlock *TIBB = TI->getParent();
Eric Christopherb65acc62012-07-02 23:22:21 +0000714 for (unsigned i = 0, e = PredCases.size(); i != e; ++i)
715 if (PredCases[i].Dest == TIBB) {
Craig Topperf40110f2014-04-25 05:29:35 +0000716 if (TIV)
Eric Christopherb65acc62012-07-02 23:22:21 +0000717 return false; // Cannot handle multiple values coming to this block.
718 TIV = PredCases[i].Value;
719 }
720 assert(TIV && "No edge from pred to succ?");
Chris Lattner4088e2b2010-12-13 01:47:07 +0000721
722 // Okay, we found the one constant that our value can be if we get into TI's
723 // BB. Find out which successor will unconditionally be branched to.
Craig Topperf40110f2014-04-25 05:29:35 +0000724 BasicBlock *TheRealDest = nullptr;
Eric Christopherb65acc62012-07-02 23:22:21 +0000725 for (unsigned i = 0, e = ThisCases.size(); i != e; ++i)
726 if (ThisCases[i].Value == TIV) {
727 TheRealDest = ThisCases[i].Dest;
728 break;
729 }
Chris Lattner4088e2b2010-12-13 01:47:07 +0000730
731 // If not handled by any explicit cases, it is handled by the default case.
Craig Topperf40110f2014-04-25 05:29:35 +0000732 if (!TheRealDest) TheRealDest = ThisDef;
Chris Lattner4088e2b2010-12-13 01:47:07 +0000733
734 // Remove PHI node entries for dead edges.
735 BasicBlock *CheckEdge = TheRealDest;
Duncan P. N. Exon Smith6c990152014-07-21 17:06:51 +0000736 for (succ_iterator SI = succ_begin(TIBB), e = succ_end(TIBB); SI != e; ++SI)
737 if (*SI != CheckEdge)
738 (*SI)->removePredecessor(TIBB);
Chris Lattner4088e2b2010-12-13 01:47:07 +0000739 else
Craig Topperf40110f2014-04-25 05:29:35 +0000740 CheckEdge = nullptr;
Chris Lattner4088e2b2010-12-13 01:47:07 +0000741
742 // Insert the new branch.
Devang Patela7ec47d2011-05-18 20:35:38 +0000743 Instruction *NI = Builder.CreateBr(TheRealDest);
Chris Lattner4088e2b2010-12-13 01:47:07 +0000744 (void) NI;
745
746 DEBUG(dbgs() << "Threading pred instr: " << *Pred->getTerminator()
747 << "Through successor TI: " << *TI << "Leaving: " << *NI << "\n");
748
749 EraseTerminatorInstAndDCECond(TI);
750 return true;
Chris Lattner1cca9592005-02-24 06:17:52 +0000751}
752
Dale Johannesen7f99d222009-03-12 21:01:11 +0000753namespace {
Sanjay Patel09159b8f2015-06-24 20:40:57 +0000754 /// This class implements a stable ordering of constant
Dale Johannesen7f99d222009-03-12 21:01:11 +0000755 /// integers that does not depend on their address. This is important for
756 /// applications that sort ConstantInt's to ensure uniqueness.
757 struct ConstantIntOrdering {
758 bool operator()(const ConstantInt *LHS, const ConstantInt *RHS) const {
759 return LHS->getValue().ult(RHS->getValue());
760 }
761 };
762}
Dale Johannesen5a41b2d2009-03-12 01:00:26 +0000763
Benjamin Kramer8817cca2013-09-22 14:09:50 +0000764static int ConstantIntSortPredicate(ConstantInt *const *P1,
765 ConstantInt *const *P2) {
766 const ConstantInt *LHS = *P1;
767 const ConstantInt *RHS = *P2;
Chris Lattnere893e262010-12-15 04:52:41 +0000768 if (LHS->getValue().ult(RHS->getValue()))
769 return 1;
770 if (LHS->getValue() == RHS->getValue())
771 return 0;
772 return -1;
Chris Lattner7c8e6042010-12-13 02:00:58 +0000773}
774
Andrew Trick3051aa12012-08-29 21:46:38 +0000775static inline bool HasBranchWeights(const Instruction* I) {
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +0000776 MDNode *ProfMD = I->getMetadata(LLVMContext::MD_prof);
Andrew Trick3051aa12012-08-29 21:46:38 +0000777 if (ProfMD && ProfMD->getOperand(0))
778 if (MDString* MDS = dyn_cast<MDString>(ProfMD->getOperand(0)))
779 return MDS->getString().equals("branch_weights");
780
781 return false;
782}
783
Manman Ren571d9e42012-09-11 17:43:35 +0000784/// Get Weights of a given TerminatorInst, the default weight is at the front
785/// of the vector. If TI is a conditional eq, we need to swap the branch-weight
786/// metadata.
787static void GetBranchWeights(TerminatorInst *TI,
788 SmallVectorImpl<uint64_t> &Weights) {
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +0000789 MDNode *MD = TI->getMetadata(LLVMContext::MD_prof);
Manman Ren571d9e42012-09-11 17:43:35 +0000790 assert(MD);
791 for (unsigned i = 1, e = MD->getNumOperands(); i < e; ++i) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000792 ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(i));
Manman Ren571d9e42012-09-11 17:43:35 +0000793 Weights.push_back(CI->getValue().getZExtValue());
Andrew Trick3051aa12012-08-29 21:46:38 +0000794 }
795
Manman Ren571d9e42012-09-11 17:43:35 +0000796 // If TI is a conditional eq, the default case is the false case,
797 // and the corresponding branch-weight data is at index 2. We swap the
798 // default weight to be the first entry.
799 if (BranchInst* BI = dyn_cast<BranchInst>(TI)) {
800 assert(Weights.size() == 2);
801 ICmpInst *ICI = cast<ICmpInst>(BI->getCondition());
802 if (ICI->getPredicate() == ICmpInst::ICMP_EQ)
803 std::swap(Weights.front(), Weights.back());
Andrew Trick3051aa12012-08-29 21:46:38 +0000804 }
805}
806
Manman Renf1cb16e2014-01-27 23:39:03 +0000807/// Keep halving the weights until all can fit in uint32_t.
Andrew Trick3051aa12012-08-29 21:46:38 +0000808static void FitWeights(MutableArrayRef<uint64_t> Weights) {
Benjamin Kramer79da9412014-03-09 14:42:55 +0000809 uint64_t Max = *std::max_element(Weights.begin(), Weights.end());
810 if (Max > UINT_MAX) {
811 unsigned Offset = 32 - countLeadingZeros(Max);
812 for (uint64_t &I : Weights)
813 I >>= Offset;
Manman Renf1cb16e2014-01-27 23:39:03 +0000814 }
Andrew Trick3051aa12012-08-29 21:46:38 +0000815}
816
Sanjay Patel09159b8f2015-06-24 20:40:57 +0000817/// The specified terminator is a value equality comparison instruction
818/// (either a switch or a branch on "X == c").
Bill Wendlingcaf1d222009-01-19 23:43:56 +0000819/// See if any of the predecessors of the terminator block are value comparisons
820/// on the same value. If so, and if safe to do so, fold them together.
Devang Patel58380552011-05-18 20:53:17 +0000821bool SimplifyCFGOpt::FoldValueComparisonIntoPredecessors(TerminatorInst *TI,
822 IRBuilder<> &Builder) {
Chris Lattnerd3e6ae22004-02-28 21:28:10 +0000823 BasicBlock *BB = TI->getParent();
824 Value *CV = isValueEqualityComparison(TI); // CondVal
825 assert(CV && "Not a comparison?");
826 bool Changed = false;
827
Chris Lattner6b39cb92008-02-18 07:42:56 +0000828 SmallVector<BasicBlock*, 16> Preds(pred_begin(BB), pred_end(BB));
Chris Lattnerd3e6ae22004-02-28 21:28:10 +0000829 while (!Preds.empty()) {
Dan Gohman9a6fef02009-05-06 17:22:41 +0000830 BasicBlock *Pred = Preds.pop_back_val();
Misha Brukmanb1c93172005-04-21 23:48:37 +0000831
Chris Lattnerd3e6ae22004-02-28 21:28:10 +0000832 // See if the predecessor is a comparison with the same value.
833 TerminatorInst *PTI = Pred->getTerminator();
834 Value *PCV = isValueEqualityComparison(PTI); // PredCondVal
835
836 if (PCV == CV && SafeToMergeTerminators(TI, PTI)) {
837 // Figure out which 'cases' to copy from SI to PSI.
Eric Christopherb65acc62012-07-02 23:22:21 +0000838 std::vector<ValueEqualityComparisonCase> BBCases;
Chris Lattnerd3e6ae22004-02-28 21:28:10 +0000839 BasicBlock *BBDefault = GetValueEqualityComparisonCases(TI, BBCases);
840
Eric Christopherb65acc62012-07-02 23:22:21 +0000841 std::vector<ValueEqualityComparisonCase> PredCases;
Chris Lattnerd3e6ae22004-02-28 21:28:10 +0000842 BasicBlock *PredDefault = GetValueEqualityComparisonCases(PTI, PredCases);
843
844 // Based on whether the default edge from PTI goes to BB or not, fill in
845 // PredCases and PredDefault with the new switch cases we would like to
846 // build.
Chris Lattner6b39cb92008-02-18 07:42:56 +0000847 SmallVector<BasicBlock*, 8> NewSuccessors;
Chris Lattnerd3e6ae22004-02-28 21:28:10 +0000848
Andrew Trick3051aa12012-08-29 21:46:38 +0000849 // Update the branch weight metadata along the way
850 SmallVector<uint64_t, 8> Weights;
Andrew Trick3051aa12012-08-29 21:46:38 +0000851 bool PredHasWeights = HasBranchWeights(PTI);
852 bool SuccHasWeights = HasBranchWeights(TI);
853
Manman Ren5e5049d2012-09-14 19:05:19 +0000854 if (PredHasWeights) {
Manman Ren571d9e42012-09-11 17:43:35 +0000855 GetBranchWeights(PTI, Weights);
Andrew Trick7656f6d2012-11-15 18:40:31 +0000856 // branch-weight metadata is inconsistent here.
Manman Ren5e5049d2012-09-14 19:05:19 +0000857 if (Weights.size() != 1 + PredCases.size())
858 PredHasWeights = SuccHasWeights = false;
859 } else if (SuccHasWeights)
Andrew Trick3051aa12012-08-29 21:46:38 +0000860 // If there are no predecessor weights but there are successor weights,
861 // populate Weights with 1, which will later be scaled to the sum of
862 // successor's weights
863 Weights.assign(1 + PredCases.size(), 1);
Andrew Trick3051aa12012-08-29 21:46:38 +0000864
Manman Ren571d9e42012-09-11 17:43:35 +0000865 SmallVector<uint64_t, 8> SuccWeights;
Manman Ren5e5049d2012-09-14 19:05:19 +0000866 if (SuccHasWeights) {
Manman Ren571d9e42012-09-11 17:43:35 +0000867 GetBranchWeights(TI, SuccWeights);
Andrew Trick7656f6d2012-11-15 18:40:31 +0000868 // branch-weight metadata is inconsistent here.
Manman Ren5e5049d2012-09-14 19:05:19 +0000869 if (SuccWeights.size() != 1 + BBCases.size())
870 PredHasWeights = SuccHasWeights = false;
871 } else if (PredHasWeights)
Manman Ren571d9e42012-09-11 17:43:35 +0000872 SuccWeights.assign(1 + BBCases.size(), 1);
Andrew Trick3051aa12012-08-29 21:46:38 +0000873
Chris Lattnerd3e6ae22004-02-28 21:28:10 +0000874 if (PredDefault == BB) {
875 // If this is the default destination from PTI, only the edges in TI
876 // that don't occur in PTI, or that branch to BB will be activated.
Eric Christopherb65acc62012-07-02 23:22:21 +0000877 std::set<ConstantInt*, ConstantIntOrdering> PTIHandled;
878 for (unsigned i = 0, e = PredCases.size(); i != e; ++i)
879 if (PredCases[i].Dest != BB)
880 PTIHandled.insert(PredCases[i].Value);
881 else {
882 // The default destination is BB, we don't need explicit targets.
883 std::swap(PredCases[i], PredCases.back());
Andrew Trick3051aa12012-08-29 21:46:38 +0000884
Manman Ren571d9e42012-09-11 17:43:35 +0000885 if (PredHasWeights || SuccHasWeights) {
886 // Increase weight for the default case.
887 Weights[0] += Weights[i+1];
Andrew Trick3051aa12012-08-29 21:46:38 +0000888 std::swap(Weights[i+1], Weights.back());
889 Weights.pop_back();
890 }
891
Eric Christopherb65acc62012-07-02 23:22:21 +0000892 PredCases.pop_back();
893 --i; --e;
894 }
Chris Lattnerd3e6ae22004-02-28 21:28:10 +0000895
Eric Christopherb65acc62012-07-02 23:22:21 +0000896 // Reconstruct the new switch statement we will be building.
Chris Lattnerd3e6ae22004-02-28 21:28:10 +0000897 if (PredDefault != BBDefault) {
898 PredDefault->removePredecessor(Pred);
899 PredDefault = BBDefault;
900 NewSuccessors.push_back(BBDefault);
901 }
Andrew Trick3051aa12012-08-29 21:46:38 +0000902
Manman Ren571d9e42012-09-11 17:43:35 +0000903 unsigned CasesFromPred = Weights.size();
904 uint64_t ValidTotalSuccWeight = 0;
Eric Christopherb65acc62012-07-02 23:22:21 +0000905 for (unsigned i = 0, e = BBCases.size(); i != e; ++i)
906 if (!PTIHandled.count(BBCases[i].Value) &&
907 BBCases[i].Dest != BBDefault) {
908 PredCases.push_back(BBCases[i]);
909 NewSuccessors.push_back(BBCases[i].Dest);
Manman Ren571d9e42012-09-11 17:43:35 +0000910 if (SuccHasWeights || PredHasWeights) {
911 // The default weight is at index 0, so weight for the ith case
912 // should be at index i+1. Scale the cases from successor by
913 // PredDefaultWeight (Weights[0]).
914 Weights.push_back(Weights[0] * SuccWeights[i+1]);
915 ValidTotalSuccWeight += SuccWeights[i+1];
Andrew Trick3051aa12012-08-29 21:46:38 +0000916 }
Eric Christopherb65acc62012-07-02 23:22:21 +0000917 }
Chris Lattnerd3e6ae22004-02-28 21:28:10 +0000918
Manman Ren571d9e42012-09-11 17:43:35 +0000919 if (SuccHasWeights || PredHasWeights) {
920 ValidTotalSuccWeight += SuccWeights[0];
921 // Scale the cases from predecessor by ValidTotalSuccWeight.
922 for (unsigned i = 1; i < CasesFromPred; ++i)
923 Weights[i] *= ValidTotalSuccWeight;
924 // Scale the default weight by SuccDefaultWeight (SuccWeights[0]).
925 Weights[0] *= SuccWeights[0];
926 }
Chris Lattnerd3e6ae22004-02-28 21:28:10 +0000927 } else {
928 // If this is not the default destination from PSI, only the edges
929 // in SI that occur in PSI with a destination of BB will be
930 // activated.
Eric Christopherb65acc62012-07-02 23:22:21 +0000931 std::set<ConstantInt*, ConstantIntOrdering> PTIHandled;
Manman Rend81b8e82012-09-14 17:29:56 +0000932 std::map<ConstantInt*, uint64_t> WeightsForHandled;
Eric Christopherb65acc62012-07-02 23:22:21 +0000933 for (unsigned i = 0, e = PredCases.size(); i != e; ++i)
934 if (PredCases[i].Dest == BB) {
935 PTIHandled.insert(PredCases[i].Value);
Manman Rend81b8e82012-09-14 17:29:56 +0000936
937 if (PredHasWeights || SuccHasWeights) {
938 WeightsForHandled[PredCases[i].Value] = Weights[i+1];
939 std::swap(Weights[i+1], Weights.back());
940 Weights.pop_back();
941 }
942
Eric Christopherb65acc62012-07-02 23:22:21 +0000943 std::swap(PredCases[i], PredCases.back());
944 PredCases.pop_back();
945 --i; --e;
946 }
Chris Lattnerd3e6ae22004-02-28 21:28:10 +0000947
948 // Okay, now we know which constants were sent to BB from the
949 // predecessor. Figure out where they will all go now.
Eric Christopherb65acc62012-07-02 23:22:21 +0000950 for (unsigned i = 0, e = BBCases.size(); i != e; ++i)
951 if (PTIHandled.count(BBCases[i].Value)) {
952 // If this is one we are capable of getting...
Manman Rend81b8e82012-09-14 17:29:56 +0000953 if (PredHasWeights || SuccHasWeights)
954 Weights.push_back(WeightsForHandled[BBCases[i].Value]);
Eric Christopherb65acc62012-07-02 23:22:21 +0000955 PredCases.push_back(BBCases[i]);
956 NewSuccessors.push_back(BBCases[i].Dest);
957 PTIHandled.erase(BBCases[i].Value);// This constant is taken care of
958 }
959
Chris Lattnerd3e6ae22004-02-28 21:28:10 +0000960 // If there are any constants vectored to BB that TI doesn't handle,
961 // they must go to the default destination of TI.
Andrew Trickf3cf1932012-08-29 21:46:36 +0000962 for (std::set<ConstantInt*, ConstantIntOrdering>::iterator I =
Eric Christopherb65acc62012-07-02 23:22:21 +0000963 PTIHandled.begin(),
964 E = PTIHandled.end(); I != E; ++I) {
Andrew Trick90f50292012-11-15 18:40:29 +0000965 if (PredHasWeights || SuccHasWeights)
966 Weights.push_back(WeightsForHandled[*I]);
Eric Christopherb65acc62012-07-02 23:22:21 +0000967 PredCases.push_back(ValueEqualityComparisonCase(*I, BBDefault));
Chris Lattnerd3e6ae22004-02-28 21:28:10 +0000968 NewSuccessors.push_back(BBDefault);
Eric Christopherb65acc62012-07-02 23:22:21 +0000969 }
Chris Lattnerd3e6ae22004-02-28 21:28:10 +0000970 }
971
972 // Okay, at this point, we know which new successor Pred will get. Make
973 // sure we update the number of entries in the PHI nodes for these
974 // successors.
Sanjay Patelf4b34b72015-09-10 16:25:38 +0000975 for (BasicBlock *NewSuccessor : NewSuccessors)
976 AddPredecessorToBlock(NewSuccessor, Pred, BB);
Chris Lattnerd3e6ae22004-02-28 21:28:10 +0000977
Devang Patel58380552011-05-18 20:53:17 +0000978 Builder.SetInsertPoint(PTI);
Jakob Stoklund Olesen916f48a2010-02-05 22:03:18 +0000979 // Convert pointer to int before we switch.
Duncan Sands19d0b472010-02-16 11:11:14 +0000980 if (CV->getType()->isPointerTy()) {
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000981 CV = Builder.CreatePtrToInt(CV, DL.getIntPtrType(CV->getType()),
Devang Patel58380552011-05-18 20:53:17 +0000982 "magicptr");
Jakob Stoklund Olesen916f48a2010-02-05 22:03:18 +0000983 }
984
Chris Lattnerd3e6ae22004-02-28 21:28:10 +0000985 // Now that the successors are updated, create the new Switch instruction.
Devang Patel58380552011-05-18 20:53:17 +0000986 SwitchInst *NewSI = Builder.CreateSwitch(CV, PredDefault,
987 PredCases.size());
Devang Patelb849cd52011-05-17 23:29:05 +0000988 NewSI->setDebugLoc(PTI->getDebugLoc());
Sanjay Patel5e7bd912015-09-10 16:15:21 +0000989 for (ValueEqualityComparisonCase &V : PredCases)
990 NewSI->addCase(V.Value, V.Dest);
Chris Lattner3215bb62005-01-01 16:02:12 +0000991
Andrew Trick3051aa12012-08-29 21:46:38 +0000992 if (PredHasWeights || SuccHasWeights) {
993 // Halve the weights if any of them cannot fit in an uint32_t
994 FitWeights(Weights);
995
996 SmallVector<uint32_t, 8> MDWeights(Weights.begin(), Weights.end());
997
998 NewSI->setMetadata(LLVMContext::MD_prof,
999 MDBuilder(BB->getContext()).
1000 createBranchWeights(MDWeights));
1001 }
1002
Eli Friedmancb61afb2008-12-16 20:54:32 +00001003 EraseTerminatorInstAndDCECond(PTI);
Chris Lattner3215bb62005-01-01 16:02:12 +00001004
Chris Lattnerd3e6ae22004-02-28 21:28:10 +00001005 // Okay, last check. If BB is still a successor of PSI, then we must
1006 // have an infinite loop case. If so, add an infinitely looping block
1007 // to handle the case to preserve the behavior of the code.
Craig Topperf40110f2014-04-25 05:29:35 +00001008 BasicBlock *InfLoopBlock = nullptr;
Chris Lattnerd3e6ae22004-02-28 21:28:10 +00001009 for (unsigned i = 0, e = NewSI->getNumSuccessors(); i != e; ++i)
1010 if (NewSI->getSuccessor(i) == BB) {
Craig Topperf40110f2014-04-25 05:29:35 +00001011 if (!InfLoopBlock) {
Chris Lattner80b03a12008-07-13 22:23:11 +00001012 // Insert it at the end of the function, because it's either code,
Chris Lattnerd3e6ae22004-02-28 21:28:10 +00001013 // or it won't matter if it's hot. :)
Owen Anderson55f1c092009-08-13 21:58:54 +00001014 InfLoopBlock = BasicBlock::Create(BB->getContext(),
1015 "infloop", BB->getParent());
Gabor Greife9ecc682008-04-06 20:25:17 +00001016 BranchInst::Create(InfLoopBlock, InfLoopBlock);
Chris Lattnerd3e6ae22004-02-28 21:28:10 +00001017 }
1018 NewSI->setSuccessor(i, InfLoopBlock);
1019 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001020
Chris Lattnerd3e6ae22004-02-28 21:28:10 +00001021 Changed = true;
1022 }
1023 }
1024 return Changed;
1025}
1026
Sanjay Patel09159b8f2015-06-24 20:40:57 +00001027// If we would need to insert a select that uses the value of this invoke
1028// (comments in HoistThenElseCodeToIf explain why we would need to do this), we
1029// can't hoist the invoke, as there is nowhere to put the select in this case.
Dale Johannesen9df78ee2009-06-15 20:59:27 +00001030static bool isSafeToHoistInvoke(BasicBlock *BB1, BasicBlock *BB2,
1031 Instruction *I1, Instruction *I2) {
Duncan P. N. Exon Smith6c990152014-07-21 17:06:51 +00001032 for (succ_iterator SI = succ_begin(BB1), E = succ_end(BB1); SI != E; ++SI) {
Dale Johannesen9df78ee2009-06-15 20:59:27 +00001033 PHINode *PN;
Duncan P. N. Exon Smith6c990152014-07-21 17:06:51 +00001034 for (BasicBlock::iterator BBI = SI->begin();
Dale Johannesen9df78ee2009-06-15 20:59:27 +00001035 (PN = dyn_cast<PHINode>(BBI)); ++BBI) {
1036 Value *BB1V = PN->getIncomingValueForBlock(BB1);
1037 Value *BB2V = PN->getIncomingValueForBlock(BB2);
1038 if (BB1V != BB2V && (BB1V==I1 || BB2V==I2)) {
1039 return false;
1040 }
1041 }
1042 }
1043 return true;
1044}
1045
Arnold Schwaighoferd7d010e2014-10-10 01:27:02 +00001046static bool passingValueIsAlwaysUndefined(Value *V, Instruction *I);
1047
Sanjay Patel09159b8f2015-06-24 20:40:57 +00001048/// Given a conditional branch that goes to BB1 and BB2, hoist any common code
1049/// in the two blocks up into the branch block. The caller of this function
1050/// guarantees that BI's block dominates BB1 and BB2.
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001051static bool HoistThenElseCodeToIf(BranchInst *BI,
Chad Rosier54390052015-02-23 19:15:16 +00001052 const TargetTransformInfo &TTI) {
Chris Lattner389cfac2004-11-30 00:29:14 +00001053 // This does very trivial matching, with limited scanning, to find identical
1054 // instructions in the two blocks. In particular, we don't want to get into
1055 // O(M*N) situations here where M and N are the sizes of BB1 and BB2. As
1056 // such, we currently just scan for obviously identical instructions in an
1057 // identical order.
1058 BasicBlock *BB1 = BI->getSuccessor(0); // The true destination.
1059 BasicBlock *BB2 = BI->getSuccessor(1); // The false destination
1060
Devang Patelf10e2872009-02-04 00:03:08 +00001061 BasicBlock::iterator BB1_Itr = BB1->begin();
1062 BasicBlock::iterator BB2_Itr = BB2->begin();
1063
Duncan P. N. Exon Smith5b4c8372015-10-13 02:39:05 +00001064 Instruction *I1 = &*BB1_Itr++, *I2 = &*BB2_Itr++;
Devang Patel197c3522011-04-07 17:27:36 +00001065 // Skip debug info if it is not identical.
1066 DbgInfoIntrinsic *DBI1 = dyn_cast<DbgInfoIntrinsic>(I1);
1067 DbgInfoIntrinsic *DBI2 = dyn_cast<DbgInfoIntrinsic>(I2);
1068 if (!DBI1 || !DBI2 || !DBI1->isIdenticalToWhenDefined(DBI2)) {
1069 while (isa<DbgInfoIntrinsic>(I1))
Duncan P. N. Exon Smith5b4c8372015-10-13 02:39:05 +00001070 I1 = &*BB1_Itr++;
Devang Patel197c3522011-04-07 17:27:36 +00001071 while (isa<DbgInfoIntrinsic>(I2))
Duncan P. N. Exon Smith5b4c8372015-10-13 02:39:05 +00001072 I2 = &*BB2_Itr++;
Devang Patel197c3522011-04-07 17:27:36 +00001073 }
Devang Patele48ddf82011-04-07 00:30:15 +00001074 if (isa<PHINode>(I1) || !I1->isIdenticalToWhenDefined(I2) ||
Dale Johannesen9df78ee2009-06-15 20:59:27 +00001075 (isa<InvokeInst>(I1) && !isSafeToHoistInvoke(BB1, BB2, I1, I2)))
Chris Lattner389cfac2004-11-30 00:29:14 +00001076 return false;
1077
Chris Lattner389cfac2004-11-30 00:29:14 +00001078 BasicBlock *BIParent = BI->getParent();
Chris Lattner389cfac2004-11-30 00:29:14 +00001079
David Majnemerc82f27a2013-06-03 20:43:12 +00001080 bool Changed = false;
Chris Lattner389cfac2004-11-30 00:29:14 +00001081 do {
1082 // If we are hoisting the terminator instruction, don't move one (making a
1083 // broken BB), instead clone it, and remove BI.
1084 if (isa<TerminatorInst>(I1))
1085 goto HoistTerminator;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001086
Chad Rosier54390052015-02-23 19:15:16 +00001087 if (!TTI.isProfitableToHoist(I1) || !TTI.isProfitableToHoist(I2))
1088 return Changed;
1089
Chris Lattner389cfac2004-11-30 00:29:14 +00001090 // For a normal instruction, we just move one to right before the branch,
1091 // then replace all uses of the other with the first. Finally, we remove
1092 // the now redundant second instruction.
Duncan P. N. Exon Smith5b4c8372015-10-13 02:39:05 +00001093 BIParent->getInstList().splice(BI->getIterator(), BB1->getInstList(), I1);
Chris Lattner389cfac2004-11-30 00:29:14 +00001094 if (!I2->use_empty())
1095 I2->replaceAllUsesWith(I1);
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001096 I1->intersectOptionalDataWith(I2);
Rafael Espindolaea46c322014-08-15 15:46:38 +00001097 unsigned KnownIDs[] = {
Piotr Padlewskidc9b2cf2015-10-02 22:12:22 +00001098 LLVMContext::MD_tbaa, LLVMContext::MD_range,
1099 LLVMContext::MD_fpmath, LLVMContext::MD_invariant_load,
Artur Pilipenko5c5011d2015-11-02 17:53:51 +00001100 LLVMContext::MD_nonnull, LLVMContext::MD_invariant_group,
1101 LLVMContext::MD_align, LLVMContext::MD_dereferenceable,
1102 LLVMContext::MD_dereferenceable_or_null};
Rafael Espindolaea46c322014-08-15 15:46:38 +00001103 combineMetadata(I1, I2, KnownIDs);
Chris Lattnerd7beca32010-12-14 06:17:25 +00001104 I2->eraseFromParent();
David Majnemerc82f27a2013-06-03 20:43:12 +00001105 Changed = true;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001106
Duncan P. N. Exon Smith5b4c8372015-10-13 02:39:05 +00001107 I1 = &*BB1_Itr++;
1108 I2 = &*BB2_Itr++;
Devang Patel197c3522011-04-07 17:27:36 +00001109 // Skip debug info if it is not identical.
1110 DbgInfoIntrinsic *DBI1 = dyn_cast<DbgInfoIntrinsic>(I1);
1111 DbgInfoIntrinsic *DBI2 = dyn_cast<DbgInfoIntrinsic>(I2);
1112 if (!DBI1 || !DBI2 || !DBI1->isIdenticalToWhenDefined(DBI2)) {
1113 while (isa<DbgInfoIntrinsic>(I1))
Duncan P. N. Exon Smith5b4c8372015-10-13 02:39:05 +00001114 I1 = &*BB1_Itr++;
Devang Patel197c3522011-04-07 17:27:36 +00001115 while (isa<DbgInfoIntrinsic>(I2))
Duncan P. N. Exon Smith5b4c8372015-10-13 02:39:05 +00001116 I2 = &*BB2_Itr++;
Devang Patel197c3522011-04-07 17:27:36 +00001117 }
Devang Patele48ddf82011-04-07 00:30:15 +00001118 } while (I1->isIdenticalToWhenDefined(I2));
Chris Lattner389cfac2004-11-30 00:29:14 +00001119
1120 return true;
1121
1122HoistTerminator:
Dale Johannesen9df78ee2009-06-15 20:59:27 +00001123 // It may not be possible to hoist an invoke.
1124 if (isa<InvokeInst>(I1) && !isSafeToHoistInvoke(BB1, BB2, I1, I2))
David Majnemerc82f27a2013-06-03 20:43:12 +00001125 return Changed;
1126
Duncan P. N. Exon Smith6c990152014-07-21 17:06:51 +00001127 for (succ_iterator SI = succ_begin(BB1), E = succ_end(BB1); SI != E; ++SI) {
David Majnemerc82f27a2013-06-03 20:43:12 +00001128 PHINode *PN;
Duncan P. N. Exon Smith6c990152014-07-21 17:06:51 +00001129 for (BasicBlock::iterator BBI = SI->begin();
David Majnemerc82f27a2013-06-03 20:43:12 +00001130 (PN = dyn_cast<PHINode>(BBI)); ++BBI) {
1131 Value *BB1V = PN->getIncomingValueForBlock(BB1);
1132 Value *BB2V = PN->getIncomingValueForBlock(BB2);
1133 if (BB1V == BB2V)
1134 continue;
1135
Arnold Schwaighoferd7d010e2014-10-10 01:27:02 +00001136 // Check for passingValueIsAlwaysUndefined here because we would rather
1137 // eliminate undefined control flow then converting it to a select.
1138 if (passingValueIsAlwaysUndefined(BB1V, PN) ||
1139 passingValueIsAlwaysUndefined(BB2V, PN))
1140 return Changed;
1141
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001142 if (isa<ConstantExpr>(BB1V) && !isSafeToSpeculativelyExecute(BB1V))
David Majnemerc82f27a2013-06-03 20:43:12 +00001143 return Changed;
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001144 if (isa<ConstantExpr>(BB2V) && !isSafeToSpeculativelyExecute(BB2V))
David Majnemerc82f27a2013-06-03 20:43:12 +00001145 return Changed;
1146 }
1147 }
Dale Johannesen9df78ee2009-06-15 20:59:27 +00001148
Chris Lattner389cfac2004-11-30 00:29:14 +00001149 // Okay, it is safe to hoist the terminator.
Nick Lewycky42fb7452009-09-27 07:38:41 +00001150 Instruction *NT = I1->clone();
Duncan P. N. Exon Smith5b4c8372015-10-13 02:39:05 +00001151 BIParent->getInstList().insert(BI->getIterator(), NT);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +00001152 if (!NT->getType()->isVoidTy()) {
Chris Lattner389cfac2004-11-30 00:29:14 +00001153 I1->replaceAllUsesWith(NT);
1154 I2->replaceAllUsesWith(NT);
Chris Lattner8dd4cae2007-02-11 01:37:51 +00001155 NT->takeName(I1);
Chris Lattner389cfac2004-11-30 00:29:14 +00001156 }
1157
Devang Patel1407fb42011-05-19 20:52:46 +00001158 IRBuilder<true, NoFolder> Builder(NT);
Chris Lattner389cfac2004-11-30 00:29:14 +00001159 // Hoisting one of the terminators from our successor is a great thing.
1160 // Unfortunately, the successors of the if/else blocks may have PHI nodes in
1161 // them. If they do, all PHI entries for BB1/BB2 must agree for all PHI
1162 // nodes, so we insert select instruction to compute the final result.
1163 std::map<std::pair<Value*,Value*>, SelectInst*> InsertedSelects;
Duncan P. N. Exon Smith6c990152014-07-21 17:06:51 +00001164 for (succ_iterator SI = succ_begin(BB1), E = succ_end(BB1); SI != E; ++SI) {
Chris Lattner389cfac2004-11-30 00:29:14 +00001165 PHINode *PN;
Duncan P. N. Exon Smith6c990152014-07-21 17:06:51 +00001166 for (BasicBlock::iterator BBI = SI->begin();
Chris Lattner01944572004-11-30 07:47:34 +00001167 (PN = dyn_cast<PHINode>(BBI)); ++BBI) {
Chris Lattner389cfac2004-11-30 00:29:14 +00001168 Value *BB1V = PN->getIncomingValueForBlock(BB1);
1169 Value *BB2V = PN->getIncomingValueForBlock(BB2);
Chris Lattner4088e2b2010-12-13 01:47:07 +00001170 if (BB1V == BB2V) continue;
Andrew Trickf3cf1932012-08-29 21:46:36 +00001171
Chris Lattner4088e2b2010-12-13 01:47:07 +00001172 // These values do not agree. Insert a select instruction before NT
1173 // that determines the right value.
1174 SelectInst *&SI = InsertedSelects[std::make_pair(BB1V, BB2V)];
Craig Topperf40110f2014-04-25 05:29:35 +00001175 if (!SI)
Devang Patel1407fb42011-05-19 20:52:46 +00001176 SI = cast<SelectInst>
1177 (Builder.CreateSelect(BI->getCondition(), BB1V, BB2V,
1178 BB1V->getName()+"."+BB2V->getName()));
1179
Chris Lattner4088e2b2010-12-13 01:47:07 +00001180 // Make the PHI node use the select for all incoming values for BB1/BB2
1181 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
1182 if (PN->getIncomingBlock(i) == BB1 || PN->getIncomingBlock(i) == BB2)
1183 PN->setIncomingValue(i, SI);
Chris Lattner389cfac2004-11-30 00:29:14 +00001184 }
1185 }
1186
1187 // Update any PHI nodes in our new successors.
Duncan P. N. Exon Smith6c990152014-07-21 17:06:51 +00001188 for (succ_iterator SI = succ_begin(BB1), E = succ_end(BB1); SI != E; ++SI)
1189 AddPredecessorToBlock(*SI, BIParent, BB1);
Misha Brukmanb1c93172005-04-21 23:48:37 +00001190
Eli Friedmancb61afb2008-12-16 20:54:32 +00001191 EraseTerminatorInstAndDCECond(BI);
Chris Lattner389cfac2004-11-30 00:29:14 +00001192 return true;
1193}
1194
Sanjay Patel09159b8f2015-06-24 20:40:57 +00001195/// Given an unconditional branch that goes to BBEnd,
Manman Ren93ab6492012-09-20 22:37:36 +00001196/// check whether BBEnd has only two predecessors and the other predecessor
1197/// ends with an unconditional branch. If it is true, sink any common code
1198/// in the two predecessors to BBEnd.
1199static bool SinkThenElseCodeToEnd(BranchInst *BI1) {
1200 assert(BI1->isUnconditional());
1201 BasicBlock *BB1 = BI1->getParent();
1202 BasicBlock *BBEnd = BI1->getSuccessor(0);
1203
1204 // Check that BBEnd has two predecessors and the other predecessor ends with
1205 // an unconditional branch.
Benjamin Kramerf064b652012-09-30 21:03:56 +00001206 pred_iterator PI = pred_begin(BBEnd), PE = pred_end(BBEnd);
1207 BasicBlock *Pred0 = *PI++;
1208 if (PI == PE) // Only one predecessor.
Manman Ren93ab6492012-09-20 22:37:36 +00001209 return false;
Benjamin Kramerf064b652012-09-30 21:03:56 +00001210 BasicBlock *Pred1 = *PI++;
1211 if (PI != PE) // More than two predecessors.
1212 return false;
1213 BasicBlock *BB2 = (Pred0 == BB1) ? Pred1 : Pred0;
Manman Ren93ab6492012-09-20 22:37:36 +00001214 BranchInst *BI2 = dyn_cast<BranchInst>(BB2->getTerminator());
1215 if (!BI2 || !BI2->isUnconditional())
1216 return false;
1217
1218 // Gather the PHI nodes in BBEnd.
Michael Liao5313da32014-12-23 08:26:55 +00001219 SmallDenseMap<std::pair<Value *, Value *>, PHINode *> JointValueMap;
Craig Topperf40110f2014-04-25 05:29:35 +00001220 Instruction *FirstNonPhiInBBEnd = nullptr;
Michael Liao5313da32014-12-23 08:26:55 +00001221 for (BasicBlock::iterator I = BBEnd->begin(), E = BBEnd->end(); I != E; ++I) {
Manman Ren93ab6492012-09-20 22:37:36 +00001222 if (PHINode *PN = dyn_cast<PHINode>(I)) {
1223 Value *BB1V = PN->getIncomingValueForBlock(BB1);
Andrew Trick90f50292012-11-15 18:40:29 +00001224 Value *BB2V = PN->getIncomingValueForBlock(BB2);
Michael Liao5313da32014-12-23 08:26:55 +00001225 JointValueMap[std::make_pair(BB1V, BB2V)] = PN;
Manman Ren93ab6492012-09-20 22:37:36 +00001226 } else {
1227 FirstNonPhiInBBEnd = &*I;
1228 break;
1229 }
1230 }
1231 if (!FirstNonPhiInBBEnd)
1232 return false;
Andrew Trick90f50292012-11-15 18:40:29 +00001233
Manman Ren93ab6492012-09-20 22:37:36 +00001234 // This does very trivial matching, with limited scanning, to find identical
1235 // instructions in the two blocks. We scan backward for obviously identical
1236 // instructions in an identical order.
1237 BasicBlock::InstListType::reverse_iterator RI1 = BB1->getInstList().rbegin(),
Michael Liao5313da32014-12-23 08:26:55 +00001238 RE1 = BB1->getInstList().rend(),
1239 RI2 = BB2->getInstList().rbegin(),
1240 RE2 = BB2->getInstList().rend();
Manman Ren93ab6492012-09-20 22:37:36 +00001241 // Skip debug info.
1242 while (RI1 != RE1 && isa<DbgInfoIntrinsic>(&*RI1)) ++RI1;
1243 if (RI1 == RE1)
1244 return false;
1245 while (RI2 != RE2 && isa<DbgInfoIntrinsic>(&*RI2)) ++RI2;
1246 if (RI2 == RE2)
1247 return false;
1248 // Skip the unconditional branches.
1249 ++RI1;
1250 ++RI2;
1251
1252 bool Changed = false;
1253 while (RI1 != RE1 && RI2 != RE2) {
1254 // Skip debug info.
1255 while (RI1 != RE1 && isa<DbgInfoIntrinsic>(&*RI1)) ++RI1;
1256 if (RI1 == RE1)
1257 return Changed;
1258 while (RI2 != RE2 && isa<DbgInfoIntrinsic>(&*RI2)) ++RI2;
1259 if (RI2 == RE2)
1260 return Changed;
1261
1262 Instruction *I1 = &*RI1, *I2 = &*RI2;
Michael Liao5313da32014-12-23 08:26:55 +00001263 auto InstPair = std::make_pair(I1, I2);
Manman Ren93ab6492012-09-20 22:37:36 +00001264 // I1 and I2 should have a single use in the same PHI node, and they
1265 // perform the same operation.
1266 // Cannot move control-flow-involving, volatile loads, vaarg, etc.
1267 if (isa<PHINode>(I1) || isa<PHINode>(I2) ||
1268 isa<TerminatorInst>(I1) || isa<TerminatorInst>(I2) ||
David Majnemerba275f92015-08-19 19:54:02 +00001269 I1->isEHPad() || I2->isEHPad() ||
Manman Ren93ab6492012-09-20 22:37:36 +00001270 isa<AllocaInst>(I1) || isa<AllocaInst>(I2) ||
1271 I1->mayHaveSideEffects() || I2->mayHaveSideEffects() ||
1272 I1->mayReadOrWriteMemory() || I2->mayReadOrWriteMemory() ||
1273 !I1->hasOneUse() || !I2->hasOneUse() ||
Michael Liao5313da32014-12-23 08:26:55 +00001274 !JointValueMap.count(InstPair))
Manman Ren93ab6492012-09-20 22:37:36 +00001275 return Changed;
1276
1277 // Check whether we should swap the operands of ICmpInst.
Michael Liao5313da32014-12-23 08:26:55 +00001278 // TODO: Add support of communativity.
Manman Ren93ab6492012-09-20 22:37:36 +00001279 ICmpInst *ICmp1 = dyn_cast<ICmpInst>(I1), *ICmp2 = dyn_cast<ICmpInst>(I2);
1280 bool SwapOpnds = false;
1281 if (ICmp1 && ICmp2 &&
1282 ICmp1->getOperand(0) != ICmp2->getOperand(0) &&
1283 ICmp1->getOperand(1) != ICmp2->getOperand(1) &&
1284 (ICmp1->getOperand(0) == ICmp2->getOperand(1) ||
1285 ICmp1->getOperand(1) == ICmp2->getOperand(0))) {
1286 ICmp2->swapOperands();
1287 SwapOpnds = true;
1288 }
1289 if (!I1->isSameOperationAs(I2)) {
1290 if (SwapOpnds)
1291 ICmp2->swapOperands();
1292 return Changed;
1293 }
1294
1295 // The operands should be either the same or they need to be generated
1296 // with a PHI node after sinking. We only handle the case where there is
1297 // a single pair of different operands.
Craig Topperf40110f2014-04-25 05:29:35 +00001298 Value *DifferentOp1 = nullptr, *DifferentOp2 = nullptr;
Michael Liao5313da32014-12-23 08:26:55 +00001299 unsigned Op1Idx = ~0U;
Manman Ren93ab6492012-09-20 22:37:36 +00001300 for (unsigned I = 0, E = I1->getNumOperands(); I != E; ++I) {
1301 if (I1->getOperand(I) == I2->getOperand(I))
1302 continue;
Michael Liao5313da32014-12-23 08:26:55 +00001303 // Early exit if we have more-than one pair of different operands or if
1304 // we need a PHI node to replace a constant.
1305 if (Op1Idx != ~0U ||
Manman Ren93ab6492012-09-20 22:37:36 +00001306 isa<Constant>(I1->getOperand(I)) ||
1307 isa<Constant>(I2->getOperand(I))) {
1308 // If we can't sink the instructions, undo the swapping.
1309 if (SwapOpnds)
1310 ICmp2->swapOperands();
1311 return Changed;
1312 }
1313 DifferentOp1 = I1->getOperand(I);
1314 Op1Idx = I;
1315 DifferentOp2 = I2->getOperand(I);
1316 }
1317
Michael Liao5313da32014-12-23 08:26:55 +00001318 DEBUG(dbgs() << "SINK common instructions " << *I1 << "\n");
1319 DEBUG(dbgs() << " " << *I2 << "\n");
1320
1321 // We insert the pair of different operands to JointValueMap and
1322 // remove (I1, I2) from JointValueMap.
1323 if (Op1Idx != ~0U) {
1324 auto &NewPN = JointValueMap[std::make_pair(DifferentOp1, DifferentOp2)];
1325 if (!NewPN) {
1326 NewPN =
1327 PHINode::Create(DifferentOp1->getType(), 2,
Duncan P. N. Exon Smith5b4c8372015-10-13 02:39:05 +00001328 DifferentOp1->getName() + ".sink", &BBEnd->front());
Michael Liao5313da32014-12-23 08:26:55 +00001329 NewPN->addIncoming(DifferentOp1, BB1);
1330 NewPN->addIncoming(DifferentOp2, BB2);
1331 DEBUG(dbgs() << "Create PHI node " << *NewPN << "\n";);
1332 }
Manman Ren93ab6492012-09-20 22:37:36 +00001333 // I1 should use NewPN instead of DifferentOp1.
1334 I1->setOperand(Op1Idx, NewPN);
Manman Ren93ab6492012-09-20 22:37:36 +00001335 }
Michael Liao5313da32014-12-23 08:26:55 +00001336 PHINode *OldPN = JointValueMap[InstPair];
1337 JointValueMap.erase(InstPair);
Manman Ren93ab6492012-09-20 22:37:36 +00001338
Manman Ren93ab6492012-09-20 22:37:36 +00001339 // We need to update RE1 and RE2 if we are going to sink the first
1340 // instruction in the basic block down.
1341 bool UpdateRE1 = (I1 == BB1->begin()), UpdateRE2 = (I2 == BB2->begin());
1342 // Sink the instruction.
Duncan P. N. Exon Smith5b4c8372015-10-13 02:39:05 +00001343 BBEnd->getInstList().splice(FirstNonPhiInBBEnd->getIterator(),
1344 BB1->getInstList(), I1);
Manman Ren93ab6492012-09-20 22:37:36 +00001345 if (!OldPN->use_empty())
1346 OldPN->replaceAllUsesWith(I1);
1347 OldPN->eraseFromParent();
1348
1349 if (!I2->use_empty())
1350 I2->replaceAllUsesWith(I1);
1351 I1->intersectOptionalDataWith(I2);
Philip Reamesd92c2a72014-10-22 16:37:13 +00001352 // TODO: Use combineMetadata here to preserve what metadata we can
1353 // (analogous to the hoisting case above).
Manman Ren93ab6492012-09-20 22:37:36 +00001354 I2->eraseFromParent();
1355
1356 if (UpdateRE1)
1357 RE1 = BB1->getInstList().rend();
1358 if (UpdateRE2)
1359 RE2 = BB2->getInstList().rend();
Duncan P. N. Exon Smith5b4c8372015-10-13 02:39:05 +00001360 FirstNonPhiInBBEnd = &*I1;
Manman Ren93ab6492012-09-20 22:37:36 +00001361 NumSinkCommons++;
1362 Changed = true;
1363 }
1364 return Changed;
1365}
1366
Arnold Schwaighofer474df6d2013-04-29 21:28:24 +00001367/// \brief Determine if we can hoist sink a sole store instruction out of a
1368/// conditional block.
1369///
1370/// We are looking for code like the following:
1371/// BrBB:
1372/// store i32 %add, i32* %arrayidx2
1373/// ... // No other stores or function calls (we could be calling a memory
1374/// ... // function).
1375/// %cmp = icmp ult %x, %y
1376/// br i1 %cmp, label %EndBB, label %ThenBB
1377/// ThenBB:
1378/// store i32 %add5, i32* %arrayidx2
1379/// br label EndBB
1380/// EndBB:
1381/// ...
1382/// We are going to transform this into:
1383/// BrBB:
1384/// store i32 %add, i32* %arrayidx2
1385/// ... //
1386/// %cmp = icmp ult %x, %y
1387/// %add.add5 = select i1 %cmp, i32 %add, %add5
1388/// store i32 %add.add5, i32* %arrayidx2
1389/// ...
1390///
1391/// \return The pointer to the value of the previous store if the store can be
1392/// hoisted into the predecessor block. 0 otherwise.
Benjamin Kramerad5c24f2013-05-23 16:09:15 +00001393static Value *isSafeToSpeculateStore(Instruction *I, BasicBlock *BrBB,
1394 BasicBlock *StoreBB, BasicBlock *EndBB) {
Arnold Schwaighofer474df6d2013-04-29 21:28:24 +00001395 StoreInst *StoreToHoist = dyn_cast<StoreInst>(I);
1396 if (!StoreToHoist)
Craig Topperf40110f2014-04-25 05:29:35 +00001397 return nullptr;
Arnold Schwaighofer474df6d2013-04-29 21:28:24 +00001398
1399 // Volatile or atomic.
1400 if (!StoreToHoist->isSimple())
Craig Topperf40110f2014-04-25 05:29:35 +00001401 return nullptr;
Arnold Schwaighofer474df6d2013-04-29 21:28:24 +00001402
1403 Value *StorePtr = StoreToHoist->getPointerOperand();
1404
1405 // Look for a store to the same pointer in BrBB.
1406 unsigned MaxNumInstToLookAt = 10;
1407 for (BasicBlock::reverse_iterator RI = BrBB->rbegin(),
1408 RE = BrBB->rend(); RI != RE && (--MaxNumInstToLookAt); ++RI) {
1409 Instruction *CurI = &*RI;
1410
1411 // Could be calling an instruction that effects memory like free().
1412 if (CurI->mayHaveSideEffects() && !isa<StoreInst>(CurI))
Craig Topperf40110f2014-04-25 05:29:35 +00001413 return nullptr;
Arnold Schwaighofer474df6d2013-04-29 21:28:24 +00001414
1415 StoreInst *SI = dyn_cast<StoreInst>(CurI);
1416 // Found the previous store make sure it stores to the same location.
1417 if (SI && SI->getPointerOperand() == StorePtr)
1418 // Found the previous store, return its value operand.
1419 return SI->getValueOperand();
1420 else if (SI)
Craig Topperf40110f2014-04-25 05:29:35 +00001421 return nullptr; // Unknown store.
Arnold Schwaighofer474df6d2013-04-29 21:28:24 +00001422 }
1423
Craig Topperf40110f2014-04-25 05:29:35 +00001424 return nullptr;
Arnold Schwaighofer474df6d2013-04-29 21:28:24 +00001425}
1426
Chandler Carruth8a4a1662013-01-24 08:05:06 +00001427/// \brief Speculate a conditional basic block flattening the CFG.
Dan Gohman5ab9c0a2012-01-05 23:58:56 +00001428///
Chandler Carruth8a4a1662013-01-24 08:05:06 +00001429/// Note that this is a very risky transform currently. Speculating
1430/// instructions like this is most often not desirable. Instead, there is an MI
1431/// pass which can do it with full awareness of the resource constraints.
1432/// However, some cases are "obvious" and we should do directly. An example of
1433/// this is speculating a single, reasonably cheap instruction.
1434///
1435/// There is only one distinct advantage to flattening the CFG at the IR level:
1436/// it makes very common but simplistic optimizations such as are common in
1437/// instcombine and the DAG combiner more powerful by removing CFG edges and
1438/// modeling their effects with easier to reason about SSA value graphs.
1439///
1440///
1441/// An illustration of this transform is turning this IR:
1442/// \code
1443/// BB:
1444/// %cmp = icmp ult %x, %y
1445/// br i1 %cmp, label %EndBB, label %ThenBB
1446/// ThenBB:
1447/// %sub = sub %x, %y
Dan Gohman5ab9c0a2012-01-05 23:58:56 +00001448/// br label BB2
Chandler Carruth8a4a1662013-01-24 08:05:06 +00001449/// EndBB:
1450/// %phi = phi [ %sub, %ThenBB ], [ 0, %EndBB ]
1451/// ...
1452/// \endcode
1453///
1454/// Into this IR:
1455/// \code
1456/// BB:
1457/// %cmp = icmp ult %x, %y
1458/// %sub = sub %x, %y
1459/// %cond = select i1 %cmp, 0, %sub
1460/// ...
1461/// \endcode
1462///
1463/// \returns true if the conditional block is removed.
Hal Finkela995f922014-07-10 14:41:31 +00001464static bool SpeculativelyExecuteBB(BranchInst *BI, BasicBlock *ThenBB,
James Molloy7c336572015-02-11 12:15:41 +00001465 const TargetTransformInfo &TTI) {
Chandler Carruth1d20c022013-01-24 08:22:40 +00001466 // Be conservative for now. FP select instruction can often be expensive.
1467 Value *BrCond = BI->getCondition();
1468 if (isa<FCmpInst>(BrCond))
1469 return false;
1470
Chandler Carruthe2a779f2013-01-24 09:59:39 +00001471 BasicBlock *BB = BI->getParent();
1472 BasicBlock *EndBB = ThenBB->getTerminator()->getSuccessor(0);
1473
1474 // If ThenBB is actually on the false edge of the conditional branch, remember
1475 // to swap the select operands later.
1476 bool Invert = false;
1477 if (ThenBB != BI->getSuccessor(0)) {
1478 assert(ThenBB == BI->getSuccessor(1) && "No edge from 'if' block?");
1479 Invert = true;
1480 }
1481 assert(EndBB == BI->getSuccessor(!Invert) && "No edge from to end block");
1482
Chandler Carruthceff2222013-01-25 05:40:09 +00001483 // Keep a count of how many times instructions are used within CondBB when
1484 // they are candidates for sinking into CondBB. Specifically:
1485 // - They are defined in BB, and
1486 // - They have no side effects, and
1487 // - All of their uses are in CondBB.
1488 SmallDenseMap<Instruction *, unsigned, 4> SinkCandidateUseCounts;
1489
Chandler Carruth7481ca82013-01-24 11:52:58 +00001490 unsigned SpeculationCost = 0;
Craig Topperf40110f2014-04-25 05:29:35 +00001491 Value *SpeculatedStoreValue = nullptr;
1492 StoreInst *SpeculatedStore = nullptr;
Chandler Carruth7481ca82013-01-24 11:52:58 +00001493 for (BasicBlock::iterator BBI = ThenBB->begin(),
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001494 BBE = std::prev(ThenBB->end());
Devang Patel5aed7762009-03-06 06:00:17 +00001495 BBI != BBE; ++BBI) {
Duncan P. N. Exon Smith5b4c8372015-10-13 02:39:05 +00001496 Instruction *I = &*BBI;
Devang Patel5aed7762009-03-06 06:00:17 +00001497 // Skip debug info.
Chandler Carruth7481ca82013-01-24 11:52:58 +00001498 if (isa<DbgInfoIntrinsic>(I))
1499 continue;
Devang Patel5aed7762009-03-06 06:00:17 +00001500
Mark Lacey274f48b2015-04-12 18:18:51 +00001501 // Only speculatively execute a single instruction (not counting the
Chandler Carruth7481ca82013-01-24 11:52:58 +00001502 // terminator) for now.
Chandler Carruth329b5902013-01-27 06:42:03 +00001503 ++SpeculationCost;
1504 if (SpeculationCost > 1)
Devang Patel5aed7762009-03-06 06:00:17 +00001505 return false;
Dan Gohman5ab9c0a2012-01-05 23:58:56 +00001506
Dan Gohman5ab9c0a2012-01-05 23:58:56 +00001507 // Don't hoist the instruction if it's unsafe or expensive.
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001508 if (!isSafeToSpeculativelyExecute(I) &&
1509 !(HoistCondStores && (SpeculatedStoreValue = isSafeToSpeculateStore(
1510 I, BB, ThenBB, EndBB))))
Dan Gohman5ab9c0a2012-01-05 23:58:56 +00001511 return false;
Arnold Schwaighofer474df6d2013-04-29 21:28:24 +00001512 if (!SpeculatedStoreValue &&
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001513 ComputeSpeculationCost(I, TTI) >
1514 PHINodeFoldingThreshold * TargetTransformInfo::TCC_Basic)
Dan Gohman5ab9c0a2012-01-05 23:58:56 +00001515 return false;
1516
Arnold Schwaighofer474df6d2013-04-29 21:28:24 +00001517 // Store the store speculation candidate.
1518 if (SpeculatedStoreValue)
1519 SpeculatedStore = cast<StoreInst>(I);
1520
Dan Gohman5ab9c0a2012-01-05 23:58:56 +00001521 // Do not hoist the instruction if any of its operands are defined but not
Arnold Schwaighofer474df6d2013-04-29 21:28:24 +00001522 // used in BB. The transformation will prevent the operand from
Dan Gohman5ab9c0a2012-01-05 23:58:56 +00001523 // being sunk into the use block.
Chandler Carruth7481ca82013-01-24 11:52:58 +00001524 for (User::op_iterator i = I->op_begin(), e = I->op_end();
Dan Gohman5ab9c0a2012-01-05 23:58:56 +00001525 i != e; ++i) {
1526 Instruction *OpI = dyn_cast<Instruction>(*i);
Chandler Carruthceff2222013-01-25 05:40:09 +00001527 if (!OpI || OpI->getParent() != BB ||
1528 OpI->mayHaveSideEffects())
1529 continue; // Not a candidate for sinking.
1530
1531 ++SinkCandidateUseCounts[OpI];
Dan Gohman5ab9c0a2012-01-05 23:58:56 +00001532 }
1533 }
Evan Cheng89200c92008-06-07 08:52:29 +00001534
Chandler Carruthceff2222013-01-25 05:40:09 +00001535 // Consider any sink candidates which are only used in CondBB as costs for
1536 // speculation. Note, while we iterate over a DenseMap here, we are summing
1537 // and so iteration order isn't significant.
1538 for (SmallDenseMap<Instruction *, unsigned, 4>::iterator I =
1539 SinkCandidateUseCounts.begin(), E = SinkCandidateUseCounts.end();
1540 I != E; ++I)
1541 if (I->first->getNumUses() == I->second) {
Chandler Carruth329b5902013-01-27 06:42:03 +00001542 ++SpeculationCost;
1543 if (SpeculationCost > 1)
Chandler Carruthceff2222013-01-25 05:40:09 +00001544 return false;
1545 }
1546
Chandler Carruth76aacbd2013-01-24 10:40:51 +00001547 // Check that the PHI nodes can be converted to selects.
1548 bool HaveRewritablePHIs = false;
Chandler Carruthe2a779f2013-01-24 09:59:39 +00001549 for (BasicBlock::iterator I = EndBB->begin();
Dan Gohman5ab9c0a2012-01-05 23:58:56 +00001550 PHINode *PN = dyn_cast<PHINode>(I); ++I) {
Chandler Carruthe2a779f2013-01-24 09:59:39 +00001551 Value *OrigV = PN->getIncomingValueForBlock(BB);
1552 Value *ThenV = PN->getIncomingValueForBlock(ThenBB);
Dan Gohman5ab9c0a2012-01-05 23:58:56 +00001553
Rafael Espindolaa5e536a2013-06-04 14:11:59 +00001554 // FIXME: Try to remove some of the duplication with HoistThenElseCodeToIf.
Dan Gohman5ab9c0a2012-01-05 23:58:56 +00001555 // Skip PHIs which are trivial.
Chandler Carruthe2a779f2013-01-24 09:59:39 +00001556 if (ThenV == OrigV)
Dan Gohman5ab9c0a2012-01-05 23:58:56 +00001557 continue;
1558
Arnold Schwaighoferd7d010e2014-10-10 01:27:02 +00001559 // Don't convert to selects if we could remove undefined behavior instead.
1560 if (passingValueIsAlwaysUndefined(OrigV, PN) ||
1561 passingValueIsAlwaysUndefined(ThenV, PN))
1562 return false;
1563
Chandler Carruth76aacbd2013-01-24 10:40:51 +00001564 HaveRewritablePHIs = true;
Rafael Espindolaa5e536a2013-06-04 14:11:59 +00001565 ConstantExpr *OrigCE = dyn_cast<ConstantExpr>(OrigV);
1566 ConstantExpr *ThenCE = dyn_cast<ConstantExpr>(ThenV);
1567 if (!OrigCE && !ThenCE)
Chandler Carruth8a210052013-01-24 11:53:01 +00001568 continue; // Known safe and cheap.
1569
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001570 if ((ThenCE && !isSafeToSpeculativelyExecute(ThenCE)) ||
1571 (OrigCE && !isSafeToSpeculativelyExecute(OrigCE)))
Chandler Carruth8a210052013-01-24 11:53:01 +00001572 return false;
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001573 unsigned OrigCost = OrigCE ? ComputeSpeculationCost(OrigCE, TTI) : 0;
1574 unsigned ThenCost = ThenCE ? ComputeSpeculationCost(ThenCE, TTI) : 0;
James Molloy7c336572015-02-11 12:15:41 +00001575 unsigned MaxCost = 2 * PHINodeFoldingThreshold *
1576 TargetTransformInfo::TCC_Basic;
1577 if (OrigCost + ThenCost > MaxCost)
Chandler Carruth8a210052013-01-24 11:53:01 +00001578 return false;
Chandler Carruth76aacbd2013-01-24 10:40:51 +00001579
Chandler Carruth01bffaa2013-01-24 12:05:17 +00001580 // Account for the cost of an unfolded ConstantExpr which could end up
1581 // getting expanded into Instructions.
1582 // FIXME: This doesn't account for how many operations are combined in the
Chandler Carruth329b5902013-01-27 06:42:03 +00001583 // constant expression.
1584 ++SpeculationCost;
1585 if (SpeculationCost > 1)
Chandler Carruth76aacbd2013-01-24 10:40:51 +00001586 return false;
Evan Cheng89200c92008-06-07 08:52:29 +00001587 }
Dan Gohman5ab9c0a2012-01-05 23:58:56 +00001588
1589 // If there are no PHIs to process, bail early. This helps ensure idempotence
1590 // as well.
Arnold Schwaighofer474df6d2013-04-29 21:28:24 +00001591 if (!HaveRewritablePHIs && !(HoistCondStores && SpeculatedStoreValue))
Dan Gohman5ab9c0a2012-01-05 23:58:56 +00001592 return false;
Andrew Trickf3cf1932012-08-29 21:46:36 +00001593
Dan Gohman5ab9c0a2012-01-05 23:58:56 +00001594 // If we get here, we can hoist the instruction and if-convert.
Chandler Carruthe2a779f2013-01-24 09:59:39 +00001595 DEBUG(dbgs() << "SPECULATIVELY EXECUTING BB" << *ThenBB << "\n";);
Evan Cheng89200c92008-06-07 08:52:29 +00001596
Arnold Schwaighofer474df6d2013-04-29 21:28:24 +00001597 // Insert a select of the value of the speculated store.
1598 if (SpeculatedStoreValue) {
1599 IRBuilder<true, NoFolder> Builder(BI);
1600 Value *TrueV = SpeculatedStore->getValueOperand();
1601 Value *FalseV = SpeculatedStoreValue;
1602 if (Invert)
1603 std::swap(TrueV, FalseV);
1604 Value *S = Builder.CreateSelect(BrCond, TrueV, FalseV, TrueV->getName() +
1605 "." + FalseV->getName());
1606 SpeculatedStore->setOperand(0, S);
1607 }
1608
Chandler Carruth7481ca82013-01-24 11:52:58 +00001609 // Hoist the instructions.
Duncan P. N. Exon Smith5b4c8372015-10-13 02:39:05 +00001610 BB->getInstList().splice(BI->getIterator(), ThenBB->getInstList(),
1611 ThenBB->begin(), std::prev(ThenBB->end()));
Evan Cheng89553cc2008-06-12 21:15:59 +00001612
Dan Gohman5ab9c0a2012-01-05 23:58:56 +00001613 // Insert selects and rewrite the PHI operands.
Devang Patel1407fb42011-05-19 20:52:46 +00001614 IRBuilder<true, NoFolder> Builder(BI);
Chandler Carruth76aacbd2013-01-24 10:40:51 +00001615 for (BasicBlock::iterator I = EndBB->begin();
1616 PHINode *PN = dyn_cast<PHINode>(I); ++I) {
1617 unsigned OrigI = PN->getBasicBlockIndex(BB);
1618 unsigned ThenI = PN->getBasicBlockIndex(ThenBB);
1619 Value *OrigV = PN->getIncomingValue(OrigI);
1620 Value *ThenV = PN->getIncomingValue(ThenI);
1621
1622 // Skip PHIs which are trivial.
1623 if (OrigV == ThenV)
1624 continue;
Evan Cheng89200c92008-06-07 08:52:29 +00001625
Dan Gohman5ab9c0a2012-01-05 23:58:56 +00001626 // Create a select whose true value is the speculatively executed value and
Chandler Carruth76aacbd2013-01-24 10:40:51 +00001627 // false value is the preexisting value. Swap them if the branch
1628 // destinations were inverted.
1629 Value *TrueV = ThenV, *FalseV = OrigV;
Dan Gohman5ab9c0a2012-01-05 23:58:56 +00001630 if (Invert)
Chandler Carruth76aacbd2013-01-24 10:40:51 +00001631 std::swap(TrueV, FalseV);
1632 Value *V = Builder.CreateSelect(BrCond, TrueV, FalseV,
1633 TrueV->getName() + "." + FalseV->getName());
1634 PN->setIncomingValue(OrigI, V);
1635 PN->setIncomingValue(ThenI, V);
Evan Cheng89200c92008-06-07 08:52:29 +00001636 }
1637
Evan Cheng89553cc2008-06-12 21:15:59 +00001638 ++NumSpeculations;
Evan Cheng89200c92008-06-07 08:52:29 +00001639 return true;
1640}
1641
Tom Stellarde1631dd2013-10-21 20:07:30 +00001642/// \returns True if this block contains a CallInst with the NoDuplicate
1643/// attribute.
1644static bool HasNoDuplicateCall(const BasicBlock *BB) {
1645 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I) {
1646 const CallInst *CI = dyn_cast<CallInst>(I);
1647 if (!CI)
1648 continue;
1649 if (CI->cannotDuplicate())
1650 return true;
1651 }
1652 return false;
1653}
1654
Sanjay Patel09159b8f2015-06-24 20:40:57 +00001655/// Return true if we can thread a branch across this block.
Chris Lattnerf0bd8d02005-09-20 00:43:16 +00001656static bool BlockIsSimpleEnoughToThreadThrough(BasicBlock *BB) {
1657 BranchInst *BI = cast<BranchInst>(BB->getTerminator());
Chris Lattner6c701062005-09-20 01:48:40 +00001658 unsigned Size = 0;
Andrew Trickf3cf1932012-08-29 21:46:36 +00001659
Devang Patel84fceff2009-03-10 18:00:05 +00001660 for (BasicBlock::iterator BBI = BB->begin(); &*BBI != BI; ++BBI) {
Dale Johannesened6f5a82009-03-12 23:18:09 +00001661 if (isa<DbgInfoIntrinsic>(BBI))
1662 continue;
Chris Lattner6c701062005-09-20 01:48:40 +00001663 if (Size > 10) return false; // Don't clone large BB's.
Dale Johannesened6f5a82009-03-12 23:18:09 +00001664 ++Size;
Andrew Trickf3cf1932012-08-29 21:46:36 +00001665
Dale Johannesened6f5a82009-03-12 23:18:09 +00001666 // We can only support instructions that do not define values that are
Chris Lattner6c701062005-09-20 01:48:40 +00001667 // live outside of the current basic block.
Chandler Carruthcdf47882014-03-09 03:16:01 +00001668 for (User *U : BBI->users()) {
1669 Instruction *UI = cast<Instruction>(U);
1670 if (UI->getParent() != BB || isa<PHINode>(UI)) return false;
Chris Lattner6c701062005-09-20 01:48:40 +00001671 }
Andrew Trickf3cf1932012-08-29 21:46:36 +00001672
Chris Lattnerf0bd8d02005-09-20 00:43:16 +00001673 // Looks ok, continue checking.
1674 }
Chris Lattner6c701062005-09-20 01:48:40 +00001675
Chris Lattnerf0bd8d02005-09-20 00:43:16 +00001676 return true;
1677}
1678
Sanjay Patel09159b8f2015-06-24 20:40:57 +00001679/// If we have a conditional branch on a PHI node value that is defined in the
1680/// same block as the branch and if any PHI entries are constants, thread edges
1681/// corresponding to that entry to be branches to their ultimate destination.
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001682static bool FoldCondBranchOnPHI(BranchInst *BI, const DataLayout &DL) {
Chris Lattner748f9032005-09-19 23:49:37 +00001683 BasicBlock *BB = BI->getParent();
1684 PHINode *PN = dyn_cast<PHINode>(BI->getCondition());
Chris Lattner049cb442005-09-19 23:57:04 +00001685 // NOTE: we currently cannot transform this case if the PHI node is used
1686 // outside of the block.
Chris Lattnerf0bd8d02005-09-20 00:43:16 +00001687 if (!PN || PN->getParent() != BB || !PN->hasOneUse())
1688 return false;
Andrew Trickf3cf1932012-08-29 21:46:36 +00001689
Chris Lattner748f9032005-09-19 23:49:37 +00001690 // Degenerate case of a single entry PHI.
1691 if (PN->getNumIncomingValues() == 1) {
Chris Lattnerdc3f6f22008-12-03 19:44:02 +00001692 FoldSingleEntryPHINodes(PN->getParent());
Andrew Trickf3cf1932012-08-29 21:46:36 +00001693 return true;
Chris Lattner748f9032005-09-19 23:49:37 +00001694 }
1695
1696 // Now we know that this block has multiple preds and two succs.
Chris Lattnerf0bd8d02005-09-20 00:43:16 +00001697 if (!BlockIsSimpleEnoughToThreadThrough(BB)) return false;
Andrew Trickf3cf1932012-08-29 21:46:36 +00001698
Tom Stellarde1631dd2013-10-21 20:07:30 +00001699 if (HasNoDuplicateCall(BB)) return false;
1700
Chris Lattner748f9032005-09-19 23:49:37 +00001701 // Okay, this is a simple enough basic block. See if any phi values are
1702 // constants.
Zhou Sheng75b871f2007-01-11 12:24:14 +00001703 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
Chris Lattner4088e2b2010-12-13 01:47:07 +00001704 ConstantInt *CB = dyn_cast<ConstantInt>(PN->getIncomingValue(i));
Craig Topperf40110f2014-04-25 05:29:35 +00001705 if (!CB || !CB->getType()->isIntegerTy(1)) continue;
Andrew Trickf3cf1932012-08-29 21:46:36 +00001706
Chris Lattner4088e2b2010-12-13 01:47:07 +00001707 // Okay, we now know that all edges from PredBB should be revectored to
1708 // branch to RealDest.
1709 BasicBlock *PredBB = PN->getIncomingBlock(i);
1710 BasicBlock *RealDest = BI->getSuccessor(!CB->getZExtValue());
Andrew Trickf3cf1932012-08-29 21:46:36 +00001711
Chris Lattner4088e2b2010-12-13 01:47:07 +00001712 if (RealDest == BB) continue; // Skip self loops.
Bill Wendling4f163df2011-06-04 09:42:04 +00001713 // Skip if the predecessor's terminator is an indirect branch.
1714 if (isa<IndirectBrInst>(PredBB->getTerminator())) continue;
Andrew Trickf3cf1932012-08-29 21:46:36 +00001715
Chris Lattner4088e2b2010-12-13 01:47:07 +00001716 // The dest block might have PHI nodes, other predecessors and other
1717 // difficult cases. Instead of being smart about this, just insert a new
1718 // block that jumps to the destination block, effectively splitting
1719 // the edge we are about to create.
1720 BasicBlock *EdgeBB = BasicBlock::Create(BB->getContext(),
1721 RealDest->getName()+".critedge",
1722 RealDest->getParent(), RealDest);
1723 BranchInst::Create(RealDest, EdgeBB);
Andrew Trickf3cf1932012-08-29 21:46:36 +00001724
Chris Lattner0f4d67b2010-12-14 07:09:42 +00001725 // Update PHI nodes.
1726 AddPredecessorToBlock(RealDest, EdgeBB, BB);
Chris Lattner4088e2b2010-12-13 01:47:07 +00001727
1728 // BB may have instructions that are being threaded over. Clone these
1729 // instructions into EdgeBB. We know that there will be no uses of the
1730 // cloned instructions outside of EdgeBB.
1731 BasicBlock::iterator InsertPt = EdgeBB->begin();
1732 DenseMap<Value*, Value*> TranslateMap; // Track translated values.
1733 for (BasicBlock::iterator BBI = BB->begin(); &*BBI != BI; ++BBI) {
1734 if (PHINode *PN = dyn_cast<PHINode>(BBI)) {
1735 TranslateMap[PN] = PN->getIncomingValueForBlock(PredBB);
1736 continue;
1737 }
1738 // Clone the instruction.
1739 Instruction *N = BBI->clone();
1740 if (BBI->hasName()) N->setName(BBI->getName()+".c");
Andrew Trickf3cf1932012-08-29 21:46:36 +00001741
Chris Lattner4088e2b2010-12-13 01:47:07 +00001742 // Update operands due to translation.
1743 for (User::op_iterator i = N->op_begin(), e = N->op_end();
1744 i != e; ++i) {
1745 DenseMap<Value*, Value*>::iterator PI = TranslateMap.find(*i);
1746 if (PI != TranslateMap.end())
1747 *i = PI->second;
1748 }
Andrew Trickf3cf1932012-08-29 21:46:36 +00001749
Chris Lattner4088e2b2010-12-13 01:47:07 +00001750 // Check for trivial simplification.
Rafael Espindola37dc9e12014-02-21 00:06:31 +00001751 if (Value *V = SimplifyInstruction(N, DL)) {
Duncan P. N. Exon Smith5b4c8372015-10-13 02:39:05 +00001752 TranslateMap[&*BBI] = V;
Chris Lattnerd7beca32010-12-14 06:17:25 +00001753 delete N; // Instruction folded away, don't need actual inst
Chris Lattner4088e2b2010-12-13 01:47:07 +00001754 } else {
1755 // Insert the new instruction into its new home.
1756 EdgeBB->getInstList().insert(InsertPt, N);
1757 if (!BBI->use_empty())
Duncan P. N. Exon Smith5b4c8372015-10-13 02:39:05 +00001758 TranslateMap[&*BBI] = N;
Chris Lattner4088e2b2010-12-13 01:47:07 +00001759 }
1760 }
1761
1762 // Loop over all of the edges from PredBB to BB, changing them to branch
1763 // to EdgeBB instead.
1764 TerminatorInst *PredBBTI = PredBB->getTerminator();
1765 for (unsigned i = 0, e = PredBBTI->getNumSuccessors(); i != e; ++i)
1766 if (PredBBTI->getSuccessor(i) == BB) {
1767 BB->removePredecessor(PredBB);
1768 PredBBTI->setSuccessor(i, EdgeBB);
1769 }
Bill Wendling4f163df2011-06-04 09:42:04 +00001770
Chris Lattner4088e2b2010-12-13 01:47:07 +00001771 // Recurse, simplifying any other constants.
Rafael Espindola37dc9e12014-02-21 00:06:31 +00001772 return FoldCondBranchOnPHI(BI, DL) | true;
Zhou Sheng75b871f2007-01-11 12:24:14 +00001773 }
Chris Lattner748f9032005-09-19 23:49:37 +00001774
1775 return false;
1776}
1777
Sanjay Patel09159b8f2015-06-24 20:40:57 +00001778/// Given a BB that starts with the specified two-entry PHI node,
1779/// see if we can eliminate it.
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001780static bool FoldTwoEntryPHINode(PHINode *PN, const TargetTransformInfo &TTI,
1781 const DataLayout &DL) {
Chris Lattnercc14ebc2005-09-23 06:39:30 +00001782 // Ok, this is a two entry PHI node. Check to see if this is a simple "if
1783 // statement", which has a very simple dominance structure. Basically, we
1784 // are trying to find the condition that is being branched on, which
1785 // subsequently causes this merge to happen. We really want control
1786 // dependence information for this check, but simplifycfg can't keep it up
1787 // to date, and this catches most of the cases we care about anyway.
Chris Lattnercc14ebc2005-09-23 06:39:30 +00001788 BasicBlock *BB = PN->getParent();
1789 BasicBlock *IfTrue, *IfFalse;
1790 Value *IfCond = GetIfCondition(BB, IfTrue, IfFalse);
Chris Lattner335f0e42010-12-14 08:01:53 +00001791 if (!IfCond ||
1792 // Don't bother if the branch will be constant folded trivially.
1793 isa<ConstantInt>(IfCond))
1794 return false;
Andrew Trickf3cf1932012-08-29 21:46:36 +00001795
Chris Lattner95adf8f12006-11-18 19:19:36 +00001796 // Okay, we found that we can merge this two-entry phi node into a select.
1797 // Doing so would require us to fold *all* two entry phi nodes in this block.
1798 // At some point this becomes non-profitable (particularly if the target
1799 // doesn't support cmov's). Only do this transformation if there are two or
1800 // fewer PHI nodes in this block.
1801 unsigned NumPhis = 0;
1802 for (BasicBlock::iterator I = BB->begin(); isa<PHINode>(I); ++NumPhis, ++I)
1803 if (NumPhis > 2)
1804 return false;
Andrew Trickf3cf1932012-08-29 21:46:36 +00001805
Chris Lattnercc14ebc2005-09-23 06:39:30 +00001806 // Loop over the PHI's seeing if we can promote them all to select
1807 // instructions. While we are at it, keep track of the instructions
1808 // that need to be moved to the dominating block.
Chris Lattner9ac168d2010-12-14 07:41:39 +00001809 SmallPtrSet<Instruction*, 4> AggressiveInsts;
Peter Collingbourne616044a2011-04-29 18:47:38 +00001810 unsigned MaxCostVal0 = PHINodeFoldingThreshold,
1811 MaxCostVal1 = PHINodeFoldingThreshold;
James Molloy7c336572015-02-11 12:15:41 +00001812 MaxCostVal0 *= TargetTransformInfo::TCC_Basic;
1813 MaxCostVal1 *= TargetTransformInfo::TCC_Basic;
Andrew Trickf3cf1932012-08-29 21:46:36 +00001814
Chris Lattner7499b452010-12-14 08:46:09 +00001815 for (BasicBlock::iterator II = BB->begin(); isa<PHINode>(II);) {
1816 PHINode *PN = cast<PHINode>(II++);
Rafael Espindola37dc9e12014-02-21 00:06:31 +00001817 if (Value *V = SimplifyInstruction(PN, DL)) {
Chris Lattnerb42d2932010-12-14 07:20:29 +00001818 PN->replaceAllUsesWith(V);
Chris Lattner7499b452010-12-14 08:46:09 +00001819 PN->eraseFromParent();
Chris Lattnerb42d2932010-12-14 07:20:29 +00001820 continue;
Chris Lattnercc14ebc2005-09-23 06:39:30 +00001821 }
Andrew Trickf3cf1932012-08-29 21:46:36 +00001822
Peter Collingbournee3511e12011-04-29 18:47:31 +00001823 if (!DominatesMergePoint(PN->getIncomingValue(0), BB, &AggressiveInsts,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001824 MaxCostVal0, TTI) ||
Peter Collingbournee3511e12011-04-29 18:47:31 +00001825 !DominatesMergePoint(PN->getIncomingValue(1), BB, &AggressiveInsts,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001826 MaxCostVal1, TTI))
Chris Lattnerb42d2932010-12-14 07:20:29 +00001827 return false;
Chris Lattnercc14ebc2005-09-23 06:39:30 +00001828 }
Andrew Trickf3cf1932012-08-29 21:46:36 +00001829
Sylvestre Ledru35521e22012-07-23 08:51:15 +00001830 // If we folded the first phi, PN dangles at this point. Refresh it. If
Chris Lattner9ac168d2010-12-14 07:41:39 +00001831 // we ran out of PHIs then we simplified them all.
1832 PN = dyn_cast<PHINode>(BB->begin());
Craig Topperf40110f2014-04-25 05:29:35 +00001833 if (!PN) return true;
Andrew Trickf3cf1932012-08-29 21:46:36 +00001834
Chris Lattner7499b452010-12-14 08:46:09 +00001835 // Don't fold i1 branches on PHIs which contain binary operators. These can
1836 // often be turned into switches and other things.
1837 if (PN->getType()->isIntegerTy(1) &&
1838 (isa<BinaryOperator>(PN->getIncomingValue(0)) ||
1839 isa<BinaryOperator>(PN->getIncomingValue(1)) ||
1840 isa<BinaryOperator>(IfCond)))
1841 return false;
Andrew Trickf3cf1932012-08-29 21:46:36 +00001842
Chris Lattnercc14ebc2005-09-23 06:39:30 +00001843 // If we all PHI nodes are promotable, check to make sure that all
1844 // instructions in the predecessor blocks can be promoted as well. If
1845 // not, we won't be able to get rid of the control flow, so it's not
1846 // worth promoting to select instructions.
Craig Topperf40110f2014-04-25 05:29:35 +00001847 BasicBlock *DomBlock = nullptr;
Chris Lattner9ac168d2010-12-14 07:41:39 +00001848 BasicBlock *IfBlock1 = PN->getIncomingBlock(0);
1849 BasicBlock *IfBlock2 = PN->getIncomingBlock(1);
1850 if (cast<BranchInst>(IfBlock1->getTerminator())->isConditional()) {
Craig Topperf40110f2014-04-25 05:29:35 +00001851 IfBlock1 = nullptr;
Chris Lattner9ac168d2010-12-14 07:41:39 +00001852 } else {
1853 DomBlock = *pred_begin(IfBlock1);
1854 for (BasicBlock::iterator I = IfBlock1->begin();!isa<TerminatorInst>(I);++I)
Duncan P. N. Exon Smith5b4c8372015-10-13 02:39:05 +00001855 if (!AggressiveInsts.count(&*I) && !isa<DbgInfoIntrinsic>(I)) {
Chris Lattnercc14ebc2005-09-23 06:39:30 +00001856 // This is not an aggressive instruction that we can promote.
1857 // Because of this, we won't be able to get rid of the control
1858 // flow, so the xform is not worth it.
1859 return false;
1860 }
1861 }
Andrew Trickf3cf1932012-08-29 21:46:36 +00001862
Chris Lattner9ac168d2010-12-14 07:41:39 +00001863 if (cast<BranchInst>(IfBlock2->getTerminator())->isConditional()) {
Craig Topperf40110f2014-04-25 05:29:35 +00001864 IfBlock2 = nullptr;
Chris Lattner9ac168d2010-12-14 07:41:39 +00001865 } else {
1866 DomBlock = *pred_begin(IfBlock2);
1867 for (BasicBlock::iterator I = IfBlock2->begin();!isa<TerminatorInst>(I);++I)
Duncan P. N. Exon Smith5b4c8372015-10-13 02:39:05 +00001868 if (!AggressiveInsts.count(&*I) && !isa<DbgInfoIntrinsic>(I)) {
Chris Lattnercc14ebc2005-09-23 06:39:30 +00001869 // This is not an aggressive instruction that we can promote.
1870 // Because of this, we won't be able to get rid of the control
1871 // flow, so the xform is not worth it.
1872 return false;
1873 }
1874 }
Andrew Trickf3cf1932012-08-29 21:46:36 +00001875
Chris Lattner9fd838d2010-12-14 07:23:10 +00001876 DEBUG(dbgs() << "FOUND IF CONDITION! " << *IfCond << " T: "
Chris Lattner9ac168d2010-12-14 07:41:39 +00001877 << IfTrue->getName() << " F: " << IfFalse->getName() << "\n");
Andrew Trickf3cf1932012-08-29 21:46:36 +00001878
Chris Lattnercc14ebc2005-09-23 06:39:30 +00001879 // If we can still promote the PHI nodes after this gauntlet of tests,
1880 // do all of the PHI's now.
Chris Lattner7499b452010-12-14 08:46:09 +00001881 Instruction *InsertPt = DomBlock->getTerminator();
Devang Patel1407fb42011-05-19 20:52:46 +00001882 IRBuilder<true, NoFolder> Builder(InsertPt);
Andrew Trickf3cf1932012-08-29 21:46:36 +00001883
Chris Lattnercc14ebc2005-09-23 06:39:30 +00001884 // Move all 'aggressive' instructions, which are defined in the
1885 // conditional parts of the if's up to the dominating block.
Chris Lattner4088e2b2010-12-13 01:47:07 +00001886 if (IfBlock1)
Duncan P. N. Exon Smith5b4c8372015-10-13 02:39:05 +00001887 DomBlock->getInstList().splice(InsertPt->getIterator(),
Chris Lattner4088e2b2010-12-13 01:47:07 +00001888 IfBlock1->getInstList(), IfBlock1->begin(),
Duncan P. N. Exon Smith5b4c8372015-10-13 02:39:05 +00001889 IfBlock1->getTerminator()->getIterator());
Chris Lattner4088e2b2010-12-13 01:47:07 +00001890 if (IfBlock2)
Duncan P. N. Exon Smith5b4c8372015-10-13 02:39:05 +00001891 DomBlock->getInstList().splice(InsertPt->getIterator(),
Chris Lattner4088e2b2010-12-13 01:47:07 +00001892 IfBlock2->getInstList(), IfBlock2->begin(),
Duncan P. N. Exon Smith5b4c8372015-10-13 02:39:05 +00001893 IfBlock2->getTerminator()->getIterator());
Andrew Trickf3cf1932012-08-29 21:46:36 +00001894
Chris Lattnercc14ebc2005-09-23 06:39:30 +00001895 while (PHINode *PN = dyn_cast<PHINode>(BB->begin())) {
1896 // Change the PHI node into a select instruction.
Chris Lattner4088e2b2010-12-13 01:47:07 +00001897 Value *TrueVal = PN->getIncomingValue(PN->getIncomingBlock(0) == IfFalse);
1898 Value *FalseVal = PN->getIncomingValue(PN->getIncomingBlock(0) == IfTrue);
Andrew Trickf3cf1932012-08-29 21:46:36 +00001899
1900 SelectInst *NV =
Devang Patel5c810ce2011-05-18 18:16:44 +00001901 cast<SelectInst>(Builder.CreateSelect(IfCond, TrueVal, FalseVal, ""));
Chris Lattner8dd4cae2007-02-11 01:37:51 +00001902 PN->replaceAllUsesWith(NV);
1903 NV->takeName(PN);
Chris Lattnerd7beca32010-12-14 06:17:25 +00001904 PN->eraseFromParent();
Chris Lattnercc14ebc2005-09-23 06:39:30 +00001905 }
Andrew Trickf3cf1932012-08-29 21:46:36 +00001906
Chris Lattner335f0e42010-12-14 08:01:53 +00001907 // At this point, IfBlock1 and IfBlock2 are both empty, so our if statement
1908 // has been flattened. Change DomBlock to jump directly to our new block to
1909 // avoid other simplifycfg's kicking in on the diamond.
1910 TerminatorInst *OldTI = DomBlock->getTerminator();
Devang Patel5c810ce2011-05-18 18:16:44 +00001911 Builder.SetInsertPoint(OldTI);
1912 Builder.CreateBr(BB);
Chris Lattner335f0e42010-12-14 08:01:53 +00001913 OldTI->eraseFromParent();
Chris Lattnercc14ebc2005-09-23 06:39:30 +00001914 return true;
1915}
Chris Lattner748f9032005-09-19 23:49:37 +00001916
Sanjay Patel09159b8f2015-06-24 20:40:57 +00001917/// If we found a conditional branch that goes to two returning blocks,
1918/// try to merge them together into one return,
Chris Lattner86bbf332008-04-24 00:01:19 +00001919/// introducing a select if the return values disagree.
Andrew Trickf3cf1932012-08-29 21:46:36 +00001920static bool SimplifyCondBranchToTwoReturns(BranchInst *BI,
Devang Pateldd14e0f2011-05-18 21:33:11 +00001921 IRBuilder<> &Builder) {
Chris Lattner86bbf332008-04-24 00:01:19 +00001922 assert(BI->isConditional() && "Must be a conditional branch");
1923 BasicBlock *TrueSucc = BI->getSuccessor(0);
1924 BasicBlock *FalseSucc = BI->getSuccessor(1);
1925 ReturnInst *TrueRet = cast<ReturnInst>(TrueSucc->getTerminator());
1926 ReturnInst *FalseRet = cast<ReturnInst>(FalseSucc->getTerminator());
Andrew Trickf3cf1932012-08-29 21:46:36 +00001927
Chris Lattner86bbf332008-04-24 00:01:19 +00001928 // Check to ensure both blocks are empty (just a return) or optionally empty
1929 // with PHI nodes. If there are other instructions, merging would cause extra
1930 // computation on one path or the other.
Chris Lattner4088e2b2010-12-13 01:47:07 +00001931 if (!TrueSucc->getFirstNonPHIOrDbg()->isTerminator())
Devang Patel086b2122009-02-05 00:30:42 +00001932 return false;
Chris Lattner4088e2b2010-12-13 01:47:07 +00001933 if (!FalseSucc->getFirstNonPHIOrDbg()->isTerminator())
Devang Patel086b2122009-02-05 00:30:42 +00001934 return false;
Chris Lattner86bbf332008-04-24 00:01:19 +00001935
Devang Pateldd14e0f2011-05-18 21:33:11 +00001936 Builder.SetInsertPoint(BI);
Chris Lattner86bbf332008-04-24 00:01:19 +00001937 // Okay, we found a branch that is going to two return nodes. If
1938 // there is no return value for this function, just change the
1939 // branch into a return.
1940 if (FalseRet->getNumOperands() == 0) {
1941 TrueSucc->removePredecessor(BI->getParent());
1942 FalseSucc->removePredecessor(BI->getParent());
Devang Pateldd14e0f2011-05-18 21:33:11 +00001943 Builder.CreateRetVoid();
Eli Friedmancb61afb2008-12-16 20:54:32 +00001944 EraseTerminatorInstAndDCECond(BI);
Chris Lattner86bbf332008-04-24 00:01:19 +00001945 return true;
1946 }
Andrew Trickf3cf1932012-08-29 21:46:36 +00001947
Dan Gohmanfa1211f2008-07-23 00:34:11 +00001948 // Otherwise, figure out what the true and false return values are
1949 // so we can insert a new select instruction.
1950 Value *TrueValue = TrueRet->getReturnValue();
1951 Value *FalseValue = FalseRet->getReturnValue();
Andrew Trickf3cf1932012-08-29 21:46:36 +00001952
Dan Gohmanfa1211f2008-07-23 00:34:11 +00001953 // Unwrap any PHI nodes in the return blocks.
1954 if (PHINode *TVPN = dyn_cast_or_null<PHINode>(TrueValue))
1955 if (TVPN->getParent() == TrueSucc)
1956 TrueValue = TVPN->getIncomingValueForBlock(BI->getParent());
1957 if (PHINode *FVPN = dyn_cast_or_null<PHINode>(FalseValue))
1958 if (FVPN->getParent() == FalseSucc)
1959 FalseValue = FVPN->getIncomingValueForBlock(BI->getParent());
Andrew Trickf3cf1932012-08-29 21:46:36 +00001960
Dan Gohmanfa1211f2008-07-23 00:34:11 +00001961 // In order for this transformation to be safe, we must be able to
1962 // unconditionally execute both operands to the return. This is
1963 // normally the case, but we could have a potentially-trapping
1964 // constant expression that prevents this transformation from being
1965 // safe.
1966 if (ConstantExpr *TCV = dyn_cast_or_null<ConstantExpr>(TrueValue))
1967 if (TCV->canTrap())
1968 return false;
1969 if (ConstantExpr *FCV = dyn_cast_or_null<ConstantExpr>(FalseValue))
1970 if (FCV->canTrap())
1971 return false;
Andrew Trickf3cf1932012-08-29 21:46:36 +00001972
Chris Lattner86bbf332008-04-24 00:01:19 +00001973 // Okay, we collected all the mapped values and checked them for sanity, and
1974 // defined to really do this transformation. First, update the CFG.
1975 TrueSucc->removePredecessor(BI->getParent());
1976 FalseSucc->removePredecessor(BI->getParent());
Andrew Trickf3cf1932012-08-29 21:46:36 +00001977
Chris Lattner86bbf332008-04-24 00:01:19 +00001978 // Insert select instructions where needed.
1979 Value *BrCond = BI->getCondition();
Dan Gohmanfa1211f2008-07-23 00:34:11 +00001980 if (TrueValue) {
Chris Lattner86bbf332008-04-24 00:01:19 +00001981 // Insert a select if the results differ.
Dan Gohmanfa1211f2008-07-23 00:34:11 +00001982 if (TrueValue == FalseValue || isa<UndefValue>(FalseValue)) {
1983 } else if (isa<UndefValue>(TrueValue)) {
1984 TrueValue = FalseValue;
1985 } else {
Devang Pateldd14e0f2011-05-18 21:33:11 +00001986 TrueValue = Builder.CreateSelect(BrCond, TrueValue,
1987 FalseValue, "retval");
Chris Lattner86bbf332008-04-24 00:01:19 +00001988 }
Chris Lattner86bbf332008-04-24 00:01:19 +00001989 }
1990
Andrew Trickf3cf1932012-08-29 21:46:36 +00001991 Value *RI = !TrueValue ?
Devang Pateldd14e0f2011-05-18 21:33:11 +00001992 Builder.CreateRetVoid() : Builder.CreateRet(TrueValue);
1993
Daniel Dunbar5e0a58b2009-08-23 10:29:55 +00001994 (void) RI;
Andrew Trickf3cf1932012-08-29 21:46:36 +00001995
David Greene725c7c32010-01-05 01:26:52 +00001996 DEBUG(dbgs() << "\nCHANGING BRANCH TO TWO RETURNS INTO SELECT:"
Chris Lattnerb25de3f2009-08-23 04:37:46 +00001997 << "\n " << *BI << "NewRet = " << *RI
1998 << "TRUEBLOCK: " << *TrueSucc << "FALSEBLOCK: "<< *FalseSucc);
Andrew Trickf3cf1932012-08-29 21:46:36 +00001999
Eli Friedmancb61afb2008-12-16 20:54:32 +00002000 EraseTerminatorInstAndDCECond(BI);
2001
Chris Lattner86bbf332008-04-24 00:01:19 +00002002 return true;
2003}
2004
Sanjay Patel09159b8f2015-06-24 20:40:57 +00002005/// Given a conditional BranchInstruction, retrieve the probabilities of the
2006/// branch taking each edge. Fills in the two APInt parameters and returns true,
2007/// or returns false if no or invalid metadata was found.
Juergen Ributzka194350a2014-12-09 17:32:12 +00002008static bool ExtractBranchMetadata(BranchInst *BI,
2009 uint64_t &ProbTrue, uint64_t &ProbFalse) {
2010 assert(BI->isConditional() &&
2011 "Looking for probabilities on unconditional branch?");
2012 MDNode *ProfileData = BI->getMetadata(LLVMContext::MD_prof);
2013 if (!ProfileData || ProfileData->getNumOperands() != 3) return false;
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002014 ConstantInt *CITrue =
2015 mdconst::dyn_extract<ConstantInt>(ProfileData->getOperand(1));
2016 ConstantInt *CIFalse =
2017 mdconst::dyn_extract<ConstantInt>(ProfileData->getOperand(2));
Juergen Ributzka194350a2014-12-09 17:32:12 +00002018 if (!CITrue || !CIFalse) return false;
2019 ProbTrue = CITrue->getValue().getZExtValue();
2020 ProbFalse = CIFalse->getValue().getZExtValue();
2021 return true;
2022}
2023
Sanjay Patel09159b8f2015-06-24 20:40:57 +00002024/// Return true if the given instruction is available
Manman Rend33f4ef2012-06-13 05:43:29 +00002025/// in its predecessor block. If yes, the instruction will be removed.
Benjamin Kramerabbfe692012-07-13 13:25:15 +00002026static bool checkCSEInPredecessor(Instruction *Inst, BasicBlock *PB) {
Manman Rend33f4ef2012-06-13 05:43:29 +00002027 if (!isa<BinaryOperator>(Inst) && !isa<CmpInst>(Inst))
2028 return false;
2029 for (BasicBlock::iterator I = PB->begin(), E = PB->end(); I != E; I++) {
2030 Instruction *PBI = &*I;
2031 // Check whether Inst and PBI generate the same value.
2032 if (Inst->isIdenticalTo(PBI)) {
2033 Inst->replaceAllUsesWith(PBI);
2034 Inst->eraseFromParent();
2035 return true;
2036 }
2037 }
2038 return false;
2039}
Nick Lewycky3c3feaf2012-01-25 09:43:14 +00002040
Sanjay Patel09159b8f2015-06-24 20:40:57 +00002041/// If this basic block is simple enough, and if a predecessor branches to us
2042/// and one of our successors, fold the block into the predecessor and use
2043/// logical operations to pick the right destination.
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002044bool llvm::FoldBranchToCommonDest(BranchInst *BI, unsigned BonusInstThreshold) {
Chris Lattner80b03a12008-07-13 22:23:11 +00002045 BasicBlock *BB = BI->getParent();
Devang Patel1407fb42011-05-19 20:52:46 +00002046
Craig Topperf40110f2014-04-25 05:29:35 +00002047 Instruction *Cond = nullptr;
Manman Rend33f4ef2012-06-13 05:43:29 +00002048 if (BI->isConditional())
2049 Cond = dyn_cast<Instruction>(BI->getCondition());
2050 else {
2051 // For unconditional branch, check for a simple CFG pattern, where
2052 // BB has a single predecessor and BB's successor is also its predecessor's
2053 // successor. If such pattern exisits, check for CSE between BB and its
2054 // predecessor.
2055 if (BasicBlock *PB = BB->getSinglePredecessor())
2056 if (BranchInst *PBI = dyn_cast<BranchInst>(PB->getTerminator()))
2057 if (PBI->isConditional() &&
2058 (BI->getSuccessor(0) == PBI->getSuccessor(0) ||
2059 BI->getSuccessor(0) == PBI->getSuccessor(1))) {
2060 for (BasicBlock::iterator I = BB->begin(), E = BB->end();
2061 I != E; ) {
Duncan P. N. Exon Smith5b4c8372015-10-13 02:39:05 +00002062 Instruction *Curr = &*I++;
Manman Rend33f4ef2012-06-13 05:43:29 +00002063 if (isa<CmpInst>(Curr)) {
2064 Cond = Curr;
2065 break;
2066 }
2067 // Quit if we can't remove this instruction.
2068 if (!checkCSEInPredecessor(Curr, PB))
2069 return false;
2070 }
2071 }
2072
Craig Topperf40110f2014-04-25 05:29:35 +00002073 if (!Cond)
Manman Rend33f4ef2012-06-13 05:43:29 +00002074 return false;
2075 }
Andrew Trickf3cf1932012-08-29 21:46:36 +00002076
Craig Topperf40110f2014-04-25 05:29:35 +00002077 if (!Cond || (!isa<CmpInst>(Cond) && !isa<BinaryOperator>(Cond)) ||
2078 Cond->getParent() != BB || !Cond->hasOneUse())
Owen Anderson2cfe9132010-07-14 19:52:16 +00002079 return false;
Devang Pateld715ec82011-04-06 22:37:20 +00002080
Chris Lattner2e25b8f2008-07-13 21:12:01 +00002081 // Make sure the instruction after the condition is the cond branch.
Duncan P. N. Exon Smith5b4c8372015-10-13 02:39:05 +00002082 BasicBlock::iterator CondIt = ++Cond->getIterator();
Chris Lattnerfba5cdf2011-04-14 02:44:53 +00002083
Sanjay Patel0a2ada72014-07-06 23:10:24 +00002084 // Ignore dbg intrinsics.
Chris Lattnerfba5cdf2011-04-14 02:44:53 +00002085 while (isa<DbgInfoIntrinsic>(CondIt)) ++CondIt;
Andrew Trickf3cf1932012-08-29 21:46:36 +00002086
Chris Lattnerfba5cdf2011-04-14 02:44:53 +00002087 if (&*CondIt != BI)
Chris Lattner2e25b8f2008-07-13 21:12:01 +00002088 return false;
Chris Lattnerea9f1d32009-01-19 23:03:13 +00002089
Jingyue Wufc029672014-09-30 22:23:38 +00002090 // Only allow this transformation if computing the condition doesn't involve
2091 // too many instructions and these involved instructions can be executed
2092 // unconditionally. We denote all involved instructions except the condition
2093 // as "bonus instructions", and only allow this transformation when the
2094 // number of the bonus instructions does not exceed a certain threshold.
2095 unsigned NumBonusInsts = 0;
2096 for (auto I = BB->begin(); Cond != I; ++I) {
2097 // Ignore dbg intrinsics.
2098 if (isa<DbgInfoIntrinsic>(I))
2099 continue;
Duncan P. N. Exon Smith5b4c8372015-10-13 02:39:05 +00002100 if (!I->hasOneUse() || !isSafeToSpeculativelyExecute(&*I))
Jingyue Wufc029672014-09-30 22:23:38 +00002101 return false;
2102 // I has only one use and can be executed unconditionally.
2103 Instruction *User = dyn_cast<Instruction>(I->user_back());
2104 if (User == nullptr || User->getParent() != BB)
2105 return false;
2106 // I is used in the same BB. Since BI uses Cond and doesn't have more slots
2107 // to use any other instruction, User must be an instruction between next(I)
2108 // and Cond.
2109 ++NumBonusInsts;
2110 // Early exits once we reach the limit.
2111 if (NumBonusInsts > BonusInstThreshold)
2112 return false;
2113 }
2114
Chris Lattnerea9f1d32009-01-19 23:03:13 +00002115 // Cond is known to be a compare or binary operator. Check to make sure that
2116 // neither operand is a potentially-trapping constant expression.
2117 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Cond->getOperand(0)))
2118 if (CE->canTrap())
2119 return false;
2120 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Cond->getOperand(1)))
2121 if (CE->canTrap())
2122 return false;
Andrew Trickf3cf1932012-08-29 21:46:36 +00002123
Chris Lattner2e25b8f2008-07-13 21:12:01 +00002124 // Finally, don't infinitely unroll conditional loops.
2125 BasicBlock *TrueDest = BI->getSuccessor(0);
Craig Topperf40110f2014-04-25 05:29:35 +00002126 BasicBlock *FalseDest = (BI->isConditional()) ? BI->getSuccessor(1) : nullptr;
Chris Lattner2e25b8f2008-07-13 21:12:01 +00002127 if (TrueDest == BB || FalseDest == BB)
2128 return false;
Devang Pateld715ec82011-04-06 22:37:20 +00002129
Duncan P. N. Exon Smith6c990152014-07-21 17:06:51 +00002130 for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) {
2131 BasicBlock *PredBlock = *PI;
Chris Lattner2e25b8f2008-07-13 21:12:01 +00002132 BranchInst *PBI = dyn_cast<BranchInst>(PredBlock->getTerminator());
Andrew Trickf3cf1932012-08-29 21:46:36 +00002133
Chris Lattner80b03a12008-07-13 22:23:11 +00002134 // Check that we have two conditional branches. If there is a PHI node in
2135 // the common successor, verify that the same value flows in from both
2136 // blocks.
Manman Rend33f4ef2012-06-13 05:43:29 +00002137 SmallVector<PHINode*, 4> PHIs;
Craig Topperf40110f2014-04-25 05:29:35 +00002138 if (!PBI || PBI->isUnconditional() ||
Andrew Trickf3cf1932012-08-29 21:46:36 +00002139 (BI->isConditional() &&
Manman Rend33f4ef2012-06-13 05:43:29 +00002140 !SafeToMergeTerminators(BI, PBI)) ||
2141 (!BI->isConditional() &&
2142 !isProfitableToFoldUnconditional(BI, PBI, Cond, PHIs)))
Chris Lattner2e25b8f2008-07-13 21:12:01 +00002143 continue;
Andrew Trickf3cf1932012-08-29 21:46:36 +00002144
Chris Lattnerfba5cdf2011-04-14 02:44:53 +00002145 // Determine if the two branches share a common destination.
Axel Naumann4a127062012-09-17 14:20:57 +00002146 Instruction::BinaryOps Opc = Instruction::BinaryOpsEnd;
Chris Lattnerfba5cdf2011-04-14 02:44:53 +00002147 bool InvertPredCond = false;
Andrew Trickf3cf1932012-08-29 21:46:36 +00002148
Manman Rend33f4ef2012-06-13 05:43:29 +00002149 if (BI->isConditional()) {
2150 if (PBI->getSuccessor(0) == TrueDest)
2151 Opc = Instruction::Or;
2152 else if (PBI->getSuccessor(1) == FalseDest)
2153 Opc = Instruction::And;
2154 else if (PBI->getSuccessor(0) == FalseDest)
2155 Opc = Instruction::And, InvertPredCond = true;
2156 else if (PBI->getSuccessor(1) == TrueDest)
2157 Opc = Instruction::Or, InvertPredCond = true;
2158 else
2159 continue;
2160 } else {
2161 if (PBI->getSuccessor(0) != TrueDest && PBI->getSuccessor(1) != TrueDest)
2162 continue;
2163 }
Chris Lattnerfba5cdf2011-04-14 02:44:53 +00002164
David Greene725c7c32010-01-05 01:26:52 +00002165 DEBUG(dbgs() << "FOLDING BRANCH TO COMMON DEST:\n" << *PBI << *BB);
Andrew Trickf3cf1932012-08-29 21:46:36 +00002166 IRBuilder<> Builder(PBI);
Devang Patel1407fb42011-05-19 20:52:46 +00002167
Chris Lattner55eaae12008-07-13 21:20:19 +00002168 // If we need to invert the condition in the pred block to match, do so now.
2169 if (InvertPredCond) {
Chris Lattnerfbeb5582010-12-13 07:00:06 +00002170 Value *NewCond = PBI->getCondition();
Andrew Trickf3cf1932012-08-29 21:46:36 +00002171
Chris Lattnerfbeb5582010-12-13 07:00:06 +00002172 if (NewCond->hasOneUse() && isa<CmpInst>(NewCond)) {
2173 CmpInst *CI = cast<CmpInst>(NewCond);
2174 CI->setPredicate(CI->getInversePredicate());
2175 } else {
Andrew Trickf3cf1932012-08-29 21:46:36 +00002176 NewCond = Builder.CreateNot(NewCond,
Devang Patel1407fb42011-05-19 20:52:46 +00002177 PBI->getCondition()->getName()+".not");
Chris Lattnerfbeb5582010-12-13 07:00:06 +00002178 }
Andrew Trickf3cf1932012-08-29 21:46:36 +00002179
Chris Lattner2e25b8f2008-07-13 21:12:01 +00002180 PBI->setCondition(NewCond);
Nick Lewycky8d302df2011-12-26 20:54:14 +00002181 PBI->swapSuccessors();
Chris Lattner2e25b8f2008-07-13 21:12:01 +00002182 }
Andrew Trickf3cf1932012-08-29 21:46:36 +00002183
Jingyue Wufc029672014-09-30 22:23:38 +00002184 // If we have bonus instructions, clone them into the predecessor block.
Sanjay Pateladb110c2015-06-24 20:07:50 +00002185 // Note that there may be multiple predecessor blocks, so we cannot move
Jingyue Wufc029672014-09-30 22:23:38 +00002186 // bonus instructions to a predecessor block.
2187 ValueToValueMapTy VMap; // maps original values to cloned values
2188 // We already make sure Cond is the last instruction before BI. Therefore,
Sanjay Pateladb110c2015-06-24 20:07:50 +00002189 // all instructions before Cond other than DbgInfoIntrinsic are bonus
Jingyue Wufc029672014-09-30 22:23:38 +00002190 // instructions.
2191 for (auto BonusInst = BB->begin(); Cond != BonusInst; ++BonusInst) {
2192 if (isa<DbgInfoIntrinsic>(BonusInst))
2193 continue;
2194 Instruction *NewBonusInst = BonusInst->clone();
2195 RemapInstruction(NewBonusInst, VMap,
2196 RF_NoModuleLevelChanges | RF_IgnoreMissingEntries);
Duncan P. N. Exon Smith5b4c8372015-10-13 02:39:05 +00002197 VMap[&*BonusInst] = NewBonusInst;
Rafael Espindolaab73c492014-01-28 16:56:46 +00002198
2199 // If we moved a load, we cannot any longer claim any knowledge about
2200 // its potential value. The previous information might have been valid
2201 // only given the branch precondition.
2202 // For an analogous reason, we must also drop all the metadata whose
2203 // semantics we don't understand.
Adrian Prantlcbdfdb72015-08-20 22:00:30 +00002204 NewBonusInst->dropUnknownNonDebugMetadata();
Rafael Espindolaab73c492014-01-28 16:56:46 +00002205
Duncan P. N. Exon Smith5b4c8372015-10-13 02:39:05 +00002206 PredBlock->getInstList().insert(PBI->getIterator(), NewBonusInst);
2207 NewBonusInst->takeName(&*BonusInst);
Jingyue Wufc029672014-09-30 22:23:38 +00002208 BonusInst->setName(BonusInst->getName() + ".old");
Owen Anderson2cfe9132010-07-14 19:52:16 +00002209 }
Andrew Trickf3cf1932012-08-29 21:46:36 +00002210
Chris Lattner55eaae12008-07-13 21:20:19 +00002211 // Clone Cond into the predecessor basic block, and or/and the
2212 // two conditions together.
Nick Lewycky42fb7452009-09-27 07:38:41 +00002213 Instruction *New = Cond->clone();
Jingyue Wufc029672014-09-30 22:23:38 +00002214 RemapInstruction(New, VMap,
2215 RF_NoModuleLevelChanges | RF_IgnoreMissingEntries);
Duncan P. N. Exon Smith5b4c8372015-10-13 02:39:05 +00002216 PredBlock->getInstList().insert(PBI->getIterator(), New);
Chris Lattner55eaae12008-07-13 21:20:19 +00002217 New->takeName(Cond);
Jingyue Wufc029672014-09-30 22:23:38 +00002218 Cond->setName(New->getName() + ".old");
Andrew Trickf3cf1932012-08-29 21:46:36 +00002219
Manman Rend33f4ef2012-06-13 05:43:29 +00002220 if (BI->isConditional()) {
Andrew Trickf3cf1932012-08-29 21:46:36 +00002221 Instruction *NewCond =
Manman Rend33f4ef2012-06-13 05:43:29 +00002222 cast<Instruction>(Builder.CreateBinOp(Opc, PBI->getCondition(),
Devang Patel1407fb42011-05-19 20:52:46 +00002223 New, "or.cond"));
Manman Rend33f4ef2012-06-13 05:43:29 +00002224 PBI->setCondition(NewCond);
2225
Manman Renbfb9d432012-09-15 00:39:57 +00002226 uint64_t PredTrueWeight, PredFalseWeight, SuccTrueWeight, SuccFalseWeight;
Juergen Ributzka194350a2014-12-09 17:32:12 +00002227 bool PredHasWeights = ExtractBranchMetadata(PBI, PredTrueWeight,
2228 PredFalseWeight);
2229 bool SuccHasWeights = ExtractBranchMetadata(BI, SuccTrueWeight,
2230 SuccFalseWeight);
Manman Renbfb9d432012-09-15 00:39:57 +00002231 SmallVector<uint64_t, 8> NewWeights;
2232
Manman Rend33f4ef2012-06-13 05:43:29 +00002233 if (PBI->getSuccessor(0) == BB) {
Manman Renbfb9d432012-09-15 00:39:57 +00002234 if (PredHasWeights && SuccHasWeights) {
2235 // PBI: br i1 %x, BB, FalseDest
2236 // BI: br i1 %y, TrueDest, FalseDest
2237 //TrueWeight is TrueWeight for PBI * TrueWeight for BI.
2238 NewWeights.push_back(PredTrueWeight * SuccTrueWeight);
2239 //FalseWeight is FalseWeight for PBI * TotalWeight for BI +
2240 // TrueWeight for PBI * FalseWeight for BI.
2241 // We assume that total weights of a BranchInst can fit into 32 bits.
2242 // Therefore, we will not have overflow using 64-bit arithmetic.
2243 NewWeights.push_back(PredFalseWeight * (SuccFalseWeight +
2244 SuccTrueWeight) + PredTrueWeight * SuccFalseWeight);
2245 }
Manman Rend33f4ef2012-06-13 05:43:29 +00002246 AddPredecessorToBlock(TrueDest, PredBlock, BB);
2247 PBI->setSuccessor(0, TrueDest);
2248 }
2249 if (PBI->getSuccessor(1) == BB) {
Manman Renbfb9d432012-09-15 00:39:57 +00002250 if (PredHasWeights && SuccHasWeights) {
2251 // PBI: br i1 %x, TrueDest, BB
2252 // BI: br i1 %y, TrueDest, FalseDest
2253 //TrueWeight is TrueWeight for PBI * TotalWeight for BI +
2254 // FalseWeight for PBI * TrueWeight for BI.
2255 NewWeights.push_back(PredTrueWeight * (SuccFalseWeight +
2256 SuccTrueWeight) + PredFalseWeight * SuccTrueWeight);
2257 //FalseWeight is FalseWeight for PBI * FalseWeight for BI.
2258 NewWeights.push_back(PredFalseWeight * SuccFalseWeight);
2259 }
Manman Rend33f4ef2012-06-13 05:43:29 +00002260 AddPredecessorToBlock(FalseDest, PredBlock, BB);
2261 PBI->setSuccessor(1, FalseDest);
2262 }
Manman Renbfb9d432012-09-15 00:39:57 +00002263 if (NewWeights.size() == 2) {
2264 // Halve the weights if any of them cannot fit in an uint32_t
2265 FitWeights(NewWeights);
2266
2267 SmallVector<uint32_t, 8> MDWeights(NewWeights.begin(),NewWeights.end());
2268 PBI->setMetadata(LLVMContext::MD_prof,
2269 MDBuilder(BI->getContext()).
2270 createBranchWeights(MDWeights));
2271 } else
Craig Topperf40110f2014-04-25 05:29:35 +00002272 PBI->setMetadata(LLVMContext::MD_prof, nullptr);
Manman Rend33f4ef2012-06-13 05:43:29 +00002273 } else {
2274 // Update PHI nodes in the common successors.
2275 for (unsigned i = 0, e = PHIs.size(); i != e; ++i) {
Nick Lewycky0a045bb2012-06-24 10:15:42 +00002276 ConstantInt *PBI_C = cast<ConstantInt>(
Manman Rend33f4ef2012-06-13 05:43:29 +00002277 PHIs[i]->getIncomingValueForBlock(PBI->getParent()));
2278 assert(PBI_C->getType()->isIntegerTy(1));
Craig Topperf40110f2014-04-25 05:29:35 +00002279 Instruction *MergedCond = nullptr;
Manman Rend33f4ef2012-06-13 05:43:29 +00002280 if (PBI->getSuccessor(0) == TrueDest) {
2281 // Create (PBI_Cond and PBI_C) or (!PBI_Cond and BI_Value)
2282 // PBI_C is true: PBI_Cond or (!PBI_Cond and BI_Value)
2283 // is false: !PBI_Cond and BI_Value
2284 Instruction *NotCond =
2285 cast<Instruction>(Builder.CreateNot(PBI->getCondition(),
2286 "not.cond"));
2287 MergedCond =
2288 cast<Instruction>(Builder.CreateBinOp(Instruction::And,
2289 NotCond, New,
2290 "and.cond"));
2291 if (PBI_C->isOne())
2292 MergedCond =
2293 cast<Instruction>(Builder.CreateBinOp(Instruction::Or,
2294 PBI->getCondition(), MergedCond,
2295 "or.cond"));
2296 } else {
2297 // Create (PBI_Cond and BI_Value) or (!PBI_Cond and PBI_C)
2298 // PBI_C is true: (PBI_Cond and BI_Value) or (!PBI_Cond)
2299 // is false: PBI_Cond and BI_Value
Andrew Trickf3cf1932012-08-29 21:46:36 +00002300 MergedCond =
Manman Rend33f4ef2012-06-13 05:43:29 +00002301 cast<Instruction>(Builder.CreateBinOp(Instruction::And,
2302 PBI->getCondition(), New,
2303 "and.cond"));
2304 if (PBI_C->isOne()) {
2305 Instruction *NotCond =
2306 cast<Instruction>(Builder.CreateNot(PBI->getCondition(),
2307 "not.cond"));
Andrew Trickf3cf1932012-08-29 21:46:36 +00002308 MergedCond =
Manman Rend33f4ef2012-06-13 05:43:29 +00002309 cast<Instruction>(Builder.CreateBinOp(Instruction::Or,
2310 NotCond, MergedCond,
2311 "or.cond"));
2312 }
2313 }
2314 // Update PHI Node.
2315 PHIs[i]->setIncomingValue(PHIs[i]->getBasicBlockIndex(PBI->getParent()),
2316 MergedCond);
2317 }
2318 // Change PBI from Conditional to Unconditional.
2319 BranchInst *New_PBI = BranchInst::Create(TrueDest, PBI);
2320 EraseTerminatorInstAndDCECond(PBI);
2321 PBI = New_PBI;
Chris Lattner55eaae12008-07-13 21:20:19 +00002322 }
Devang Pateld715ec82011-04-06 22:37:20 +00002323
Nick Lewyckyc554a9b2011-12-27 04:31:52 +00002324 // TODO: If BB is reachable from all paths through PredBlock, then we
2325 // could replace PBI's branch probabilities with BI's.
2326
Chris Lattnerfba5cdf2011-04-14 02:44:53 +00002327 // Copy any debug value intrinsics into the end of PredBlock.
2328 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
2329 if (isa<DbgInfoIntrinsic>(*I))
2330 I->clone()->insertBefore(PBI);
Andrew Trickf3cf1932012-08-29 21:46:36 +00002331
Chris Lattner5a9d59d2010-12-14 05:57:30 +00002332 return true;
Chris Lattner2e25b8f2008-07-13 21:12:01 +00002333 }
2334 return false;
2335}
2336
Sanjay Patel09159b8f2015-06-24 20:40:57 +00002337/// If we have a conditional branch as a predecessor of another block,
2338/// this function tries to simplify it. We know
Chris Lattner9aada1d2008-07-13 21:53:26 +00002339/// that PBI and BI are both conditional branches, and BI is in one of the
2340/// successor blocks of PBI - PBI branches to BI.
Philip Reamesb42db212015-10-14 22:46:19 +00002341static bool SimplifyCondBranchToCondBranch(BranchInst *PBI, BranchInst *BI,
2342 const DataLayout &DL) {
Chris Lattner9aada1d2008-07-13 21:53:26 +00002343 assert(PBI->isConditional() && BI->isConditional());
2344 BasicBlock *BB = BI->getParent();
Dan Gohman5476cfd2009-08-12 16:23:25 +00002345
Chris Lattner9aada1d2008-07-13 21:53:26 +00002346 // If this block ends with a branch instruction, and if there is a
Andrew Trickf3cf1932012-08-29 21:46:36 +00002347 // predecessor that ends on a branch of the same condition, make
Chris Lattner9aada1d2008-07-13 21:53:26 +00002348 // this conditional branch redundant.
2349 if (PBI->getCondition() == BI->getCondition() &&
2350 PBI->getSuccessor(0) != PBI->getSuccessor(1)) {
2351 // Okay, the outcome of this conditional branch is statically
2352 // knowable. If this block had a single pred, handle specially.
2353 if (BB->getSinglePredecessor()) {
2354 // Turn this into a branch on constant.
2355 bool CondIsTrue = PBI->getSuccessor(0) == BB;
Andrew Trickf3cf1932012-08-29 21:46:36 +00002356 BI->setCondition(ConstantInt::get(Type::getInt1Ty(BB->getContext()),
Owen Anderson55f1c092009-08-13 21:58:54 +00002357 CondIsTrue));
Chris Lattner9aada1d2008-07-13 21:53:26 +00002358 return true; // Nuke the branch on constant.
2359 }
Andrew Trickf3cf1932012-08-29 21:46:36 +00002360
Chris Lattner9aada1d2008-07-13 21:53:26 +00002361 // Otherwise, if there are multiple predecessors, insert a PHI that merges
2362 // in the constant and simplify the block result. Subsequent passes of
2363 // simplifycfg will thread the block.
2364 if (BlockIsSimpleEnoughToThreadThrough(BB)) {
Jay Foade0938d82011-03-30 11:19:20 +00002365 pred_iterator PB = pred_begin(BB), PE = pred_end(BB);
Duncan P. N. Exon Smith5b4c8372015-10-13 02:39:05 +00002366 PHINode *NewPN = PHINode::Create(
2367 Type::getInt1Ty(BB->getContext()), std::distance(PB, PE),
2368 BI->getCondition()->getName() + ".pr", &BB->front());
Chris Lattner5eed3722008-07-13 21:55:46 +00002369 // Okay, we're going to insert the PHI node. Since PBI is not the only
2370 // predecessor, compute the PHI'd conditional value for all of the preds.
2371 // Any predecessor where the condition is not computable we keep symbolic.
Jay Foade0938d82011-03-30 11:19:20 +00002372 for (pred_iterator PI = PB; PI != PE; ++PI) {
Gabor Greif8629f122010-07-12 10:59:23 +00002373 BasicBlock *P = *PI;
2374 if ((PBI = dyn_cast<BranchInst>(P->getTerminator())) &&
Chris Lattner9aada1d2008-07-13 21:53:26 +00002375 PBI != BI && PBI->isConditional() &&
2376 PBI->getCondition() == BI->getCondition() &&
2377 PBI->getSuccessor(0) != PBI->getSuccessor(1)) {
2378 bool CondIsTrue = PBI->getSuccessor(0) == BB;
Andrew Trickf3cf1932012-08-29 21:46:36 +00002379 NewPN->addIncoming(ConstantInt::get(Type::getInt1Ty(BB->getContext()),
Gabor Greif8629f122010-07-12 10:59:23 +00002380 CondIsTrue), P);
Chris Lattner9aada1d2008-07-13 21:53:26 +00002381 } else {
Gabor Greif8629f122010-07-12 10:59:23 +00002382 NewPN->addIncoming(BI->getCondition(), P);
Chris Lattner9aada1d2008-07-13 21:53:26 +00002383 }
Gabor Greif8629f122010-07-12 10:59:23 +00002384 }
Andrew Trickf3cf1932012-08-29 21:46:36 +00002385
Chris Lattner9aada1d2008-07-13 21:53:26 +00002386 BI->setCondition(NewPN);
Chris Lattner9aada1d2008-07-13 21:53:26 +00002387 return true;
2388 }
2389 }
Andrew Trickf3cf1932012-08-29 21:46:36 +00002390
Philip Reamesb42db212015-10-14 22:46:19 +00002391 if (auto *CE = dyn_cast<ConstantExpr>(BI->getCondition()))
2392 if (CE->canTrap())
2393 return false;
2394
Philip Reames846e3e42015-10-29 03:11:49 +00002395 // If BI is reached from the true path of PBI and PBI's condition implies
2396 // BI's condition, we know the direction of the BI branch.
2397 if (PBI->getSuccessor(0) == BI->getParent() &&
2398 isImpliedCondition(PBI->getCondition(), BI->getCondition()) &&
2399 PBI->getSuccessor(0) != PBI->getSuccessor(1) &&
2400 BB->getSinglePredecessor()) {
2401 // Turn this into a branch on constant.
2402 auto *OldCond = BI->getCondition();
2403 BI->setCondition(ConstantInt::getTrue(BB->getContext()));
2404 RecursivelyDeleteTriviallyDeadInstructions(OldCond);
2405 return true; // Nuke the branch on constant.
2406 }
2407
Chris Lattner9aada1d2008-07-13 21:53:26 +00002408 // If this is a conditional branch in an empty block, and if any
Sanjay Patel0a2ada72014-07-06 23:10:24 +00002409 // predecessors are a conditional branch to one of our destinations,
Chris Lattner9aada1d2008-07-13 21:53:26 +00002410 // fold the conditions into logical ops and one cond br.
Zhou Sheng264e46e2009-02-26 06:56:37 +00002411 BasicBlock::iterator BBI = BB->begin();
2412 // Ignore dbg intrinsics.
2413 while (isa<DbgInfoIntrinsic>(BBI))
2414 ++BBI;
2415 if (&*BBI != BI)
Chris Lattner834ab4e2008-07-13 22:04:41 +00002416 return false;
Chris Lattnerc59945b2009-01-20 01:15:41 +00002417
Chris Lattner834ab4e2008-07-13 22:04:41 +00002418 int PBIOp, BIOp;
2419 if (PBI->getSuccessor(0) == BI->getSuccessor(0))
2420 PBIOp = BIOp = 0;
2421 else if (PBI->getSuccessor(0) == BI->getSuccessor(1))
2422 PBIOp = 0, BIOp = 1;
2423 else if (PBI->getSuccessor(1) == BI->getSuccessor(0))
2424 PBIOp = 1, BIOp = 0;
2425 else if (PBI->getSuccessor(1) == BI->getSuccessor(1))
2426 PBIOp = BIOp = 1;
2427 else
2428 return false;
Andrew Trickf3cf1932012-08-29 21:46:36 +00002429
Chris Lattner834ab4e2008-07-13 22:04:41 +00002430 // Check to make sure that the other destination of this branch
2431 // isn't BB itself. If so, this is an infinite loop that will
2432 // keep getting unwound.
2433 if (PBI->getSuccessor(PBIOp) == BB)
2434 return false;
Andrew Trickf3cf1932012-08-29 21:46:36 +00002435
2436 // Do not perform this transformation if it would require
Chris Lattner834ab4e2008-07-13 22:04:41 +00002437 // insertion of a large number of select instructions. For targets
2438 // without predication/cmovs, this is a big pessimization.
Andrew Trickf3cf1932012-08-29 21:46:36 +00002439
Sanjay Patela932da82014-07-07 21:19:00 +00002440 // Also do not perform this transformation if any phi node in the common
2441 // destination block can trap when reached by BB or PBB (PR17073). In that
2442 // case, it would be unsafe to hoist the operation into a select instruction.
2443
2444 BasicBlock *CommonDest = PBI->getSuccessor(PBIOp);
Chris Lattner834ab4e2008-07-13 22:04:41 +00002445 unsigned NumPhis = 0;
2446 for (BasicBlock::iterator II = CommonDest->begin();
Sanjay Patela932da82014-07-07 21:19:00 +00002447 isa<PHINode>(II); ++II, ++NumPhis) {
Chris Lattner834ab4e2008-07-13 22:04:41 +00002448 if (NumPhis > 2) // Disable this xform.
2449 return false;
Andrew Trickf3cf1932012-08-29 21:46:36 +00002450
Sanjay Patela932da82014-07-07 21:19:00 +00002451 PHINode *PN = cast<PHINode>(II);
2452 Value *BIV = PN->getIncomingValueForBlock(BB);
2453 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(BIV))
2454 if (CE->canTrap())
2455 return false;
2456
2457 unsigned PBBIdx = PN->getBasicBlockIndex(PBI->getParent());
2458 Value *PBIV = PN->getIncomingValue(PBBIdx);
2459 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(PBIV))
2460 if (CE->canTrap())
2461 return false;
2462 }
2463
Chris Lattner834ab4e2008-07-13 22:04:41 +00002464 // Finally, if everything is ok, fold the branches to logical ops.
Sanjay Patela932da82014-07-07 21:19:00 +00002465 BasicBlock *OtherDest = BI->getSuccessor(BIOp ^ 1);
Andrew Trickf3cf1932012-08-29 21:46:36 +00002466
David Greene725c7c32010-01-05 01:26:52 +00002467 DEBUG(dbgs() << "FOLDING BRs:" << *PBI->getParent()
Chris Lattnerb25de3f2009-08-23 04:37:46 +00002468 << "AND: " << *BI->getParent());
Andrew Trickf3cf1932012-08-29 21:46:36 +00002469
2470
Chris Lattner80b03a12008-07-13 22:23:11 +00002471 // If OtherDest *is* BB, then BB is a basic block with a single conditional
2472 // branch in it, where one edge (OtherDest) goes back to itself but the other
2473 // exits. We don't *know* that the program avoids the infinite loop
2474 // (even though that seems likely). If we do this xform naively, we'll end up
2475 // recursively unpeeling the loop. Since we know that (after the xform is
2476 // done) that the block *is* infinite if reached, we just make it an obviously
2477 // infinite loop with no cond branch.
2478 if (OtherDest == BB) {
2479 // Insert it at the end of the function, because it's either code,
2480 // or it won't matter if it's hot. :)
Owen Anderson55f1c092009-08-13 21:58:54 +00002481 BasicBlock *InfLoopBlock = BasicBlock::Create(BB->getContext(),
2482 "infloop", BB->getParent());
Chris Lattner80b03a12008-07-13 22:23:11 +00002483 BranchInst::Create(InfLoopBlock, InfLoopBlock);
2484 OtherDest = InfLoopBlock;
Andrew Trickf3cf1932012-08-29 21:46:36 +00002485 }
2486
David Greene725c7c32010-01-05 01:26:52 +00002487 DEBUG(dbgs() << *PBI->getParent()->getParent());
Devang Patel1407fb42011-05-19 20:52:46 +00002488
Chris Lattner834ab4e2008-07-13 22:04:41 +00002489 // BI may have other predecessors. Because of this, we leave
2490 // it alone, but modify PBI.
Andrew Trickf3cf1932012-08-29 21:46:36 +00002491
Chris Lattner834ab4e2008-07-13 22:04:41 +00002492 // Make sure we get to CommonDest on True&True directions.
2493 Value *PBICond = PBI->getCondition();
Devang Patel1407fb42011-05-19 20:52:46 +00002494 IRBuilder<true, NoFolder> Builder(PBI);
Chris Lattner834ab4e2008-07-13 22:04:41 +00002495 if (PBIOp)
Devang Patel1407fb42011-05-19 20:52:46 +00002496 PBICond = Builder.CreateNot(PBICond, PBICond->getName()+".not");
2497
Chris Lattner834ab4e2008-07-13 22:04:41 +00002498 Value *BICond = BI->getCondition();
2499 if (BIOp)
Devang Patel1407fb42011-05-19 20:52:46 +00002500 BICond = Builder.CreateNot(BICond, BICond->getName()+".not");
2501
Chris Lattner834ab4e2008-07-13 22:04:41 +00002502 // Merge the conditions.
Devang Patel1407fb42011-05-19 20:52:46 +00002503 Value *Cond = Builder.CreateOr(PBICond, BICond, "brmerge");
Andrew Trickf3cf1932012-08-29 21:46:36 +00002504
Chris Lattner834ab4e2008-07-13 22:04:41 +00002505 // Modify PBI to branch on the new condition to the new dests.
2506 PBI->setCondition(Cond);
2507 PBI->setSuccessor(0, CommonDest);
2508 PBI->setSuccessor(1, OtherDest);
Andrew Trickf3cf1932012-08-29 21:46:36 +00002509
Manman Ren2d4c10f2012-09-17 21:30:40 +00002510 // Update branch weight for PBI.
2511 uint64_t PredTrueWeight, PredFalseWeight, SuccTrueWeight, SuccFalseWeight;
Juergen Ributzka194350a2014-12-09 17:32:12 +00002512 bool PredHasWeights = ExtractBranchMetadata(PBI, PredTrueWeight,
2513 PredFalseWeight);
2514 bool SuccHasWeights = ExtractBranchMetadata(BI, SuccTrueWeight,
2515 SuccFalseWeight);
Manman Ren2d4c10f2012-09-17 21:30:40 +00002516 if (PredHasWeights && SuccHasWeights) {
2517 uint64_t PredCommon = PBIOp ? PredFalseWeight : PredTrueWeight;
2518 uint64_t PredOther = PBIOp ?PredTrueWeight : PredFalseWeight;
2519 uint64_t SuccCommon = BIOp ? SuccFalseWeight : SuccTrueWeight;
2520 uint64_t SuccOther = BIOp ? SuccTrueWeight : SuccFalseWeight;
2521 // The weight to CommonDest should be PredCommon * SuccTotal +
2522 // PredOther * SuccCommon.
2523 // The weight to OtherDest should be PredOther * SuccOther.
Benjamin Kramerea68a942015-02-19 15:26:17 +00002524 uint64_t NewWeights[2] = {PredCommon * (SuccCommon + SuccOther) +
2525 PredOther * SuccCommon,
2526 PredOther * SuccOther};
Manman Ren2d4c10f2012-09-17 21:30:40 +00002527 // Halve the weights if any of them cannot fit in an uint32_t
2528 FitWeights(NewWeights);
2529
Manman Ren2d4c10f2012-09-17 21:30:40 +00002530 PBI->setMetadata(LLVMContext::MD_prof,
Benjamin Kramerea68a942015-02-19 15:26:17 +00002531 MDBuilder(BI->getContext())
2532 .createBranchWeights(NewWeights[0], NewWeights[1]));
Manman Ren2d4c10f2012-09-17 21:30:40 +00002533 }
2534
Chris Lattner834ab4e2008-07-13 22:04:41 +00002535 // OtherDest may have phi nodes. If so, add an entry from PBI's
2536 // block that are identical to the entries for BI's block.
Chris Lattner0f4d67b2010-12-14 07:09:42 +00002537 AddPredecessorToBlock(OtherDest, PBI->getParent(), BB);
Andrew Trickf3cf1932012-08-29 21:46:36 +00002538
Chris Lattner834ab4e2008-07-13 22:04:41 +00002539 // We know that the CommonDest already had an edge from PBI to
2540 // it. If it has PHIs though, the PHIs may have different
2541 // entries for BB and PBI's BB. If so, insert a select to make
2542 // them agree.
Chris Lattner0f4d67b2010-12-14 07:09:42 +00002543 PHINode *PN;
Chris Lattner834ab4e2008-07-13 22:04:41 +00002544 for (BasicBlock::iterator II = CommonDest->begin();
2545 (PN = dyn_cast<PHINode>(II)); ++II) {
2546 Value *BIV = PN->getIncomingValueForBlock(BB);
2547 unsigned PBBIdx = PN->getBasicBlockIndex(PBI->getParent());
2548 Value *PBIV = PN->getIncomingValue(PBBIdx);
2549 if (BIV != PBIV) {
2550 // Insert a select in PBI to pick the right value.
Devang Patel1407fb42011-05-19 20:52:46 +00002551 Value *NV = cast<SelectInst>
2552 (Builder.CreateSelect(PBICond, PBIV, BIV, PBIV->getName()+".mux"));
Chris Lattner834ab4e2008-07-13 22:04:41 +00002553 PN->setIncomingValue(PBBIdx, NV);
Chris Lattner9aada1d2008-07-13 21:53:26 +00002554 }
2555 }
Andrew Trickf3cf1932012-08-29 21:46:36 +00002556
David Greene725c7c32010-01-05 01:26:52 +00002557 DEBUG(dbgs() << "INTO: " << *PBI->getParent());
2558 DEBUG(dbgs() << *PBI->getParent()->getParent());
Andrew Trickf3cf1932012-08-29 21:46:36 +00002559
Chris Lattner834ab4e2008-07-13 22:04:41 +00002560 // This basic block is probably dead. We know it has at least
2561 // one fewer predecessor.
2562 return true;
Chris Lattner9aada1d2008-07-13 21:53:26 +00002563}
2564
Sanjay Patel09159b8f2015-06-24 20:40:57 +00002565// Simplifies a terminator by replacing it with a branch to TrueBB if Cond is
2566// true or to FalseBB if Cond is false.
Frits van Bommel8e158492011-01-11 12:52:11 +00002567// Takes care of updating the successors and removing the old terminator.
2568// Also makes sure not to introduce new successors by assuming that edges to
2569// non-successor TrueBBs and FalseBBs aren't reachable.
2570static bool SimplifyTerminatorOnSelect(TerminatorInst *OldTerm, Value *Cond,
Manman Ren774246a2012-09-17 22:28:55 +00002571 BasicBlock *TrueBB, BasicBlock *FalseBB,
2572 uint32_t TrueWeight,
2573 uint32_t FalseWeight){
Frits van Bommel8e158492011-01-11 12:52:11 +00002574 // Remove any superfluous successor edges from the CFG.
2575 // First, figure out which successors to preserve.
2576 // If TrueBB and FalseBB are equal, only try to preserve one copy of that
2577 // successor.
2578 BasicBlock *KeepEdge1 = TrueBB;
Craig Topperf40110f2014-04-25 05:29:35 +00002579 BasicBlock *KeepEdge2 = TrueBB != FalseBB ? FalseBB : nullptr;
Frits van Bommel8e158492011-01-11 12:52:11 +00002580
2581 // Then remove the rest.
Pete Cooperebcd7482015-08-06 20:22:46 +00002582 for (BasicBlock *Succ : OldTerm->successors()) {
Frits van Bommel8e158492011-01-11 12:52:11 +00002583 // Make sure only to keep exactly one copy of each edge.
2584 if (Succ == KeepEdge1)
Craig Topperf40110f2014-04-25 05:29:35 +00002585 KeepEdge1 = nullptr;
Frits van Bommel8e158492011-01-11 12:52:11 +00002586 else if (Succ == KeepEdge2)
Craig Topperf40110f2014-04-25 05:29:35 +00002587 KeepEdge2 = nullptr;
Frits van Bommel8e158492011-01-11 12:52:11 +00002588 else
David Majnemerdc3b67b2015-10-21 18:22:24 +00002589 Succ->removePredecessor(OldTerm->getParent(),
2590 /*DontDeleteUselessPHIs=*/true);
Frits van Bommel8e158492011-01-11 12:52:11 +00002591 }
2592
Devang Patel2c2ea222011-05-18 18:43:31 +00002593 IRBuilder<> Builder(OldTerm);
2594 Builder.SetCurrentDebugLocation(OldTerm->getDebugLoc());
2595
Frits van Bommel8e158492011-01-11 12:52:11 +00002596 // Insert an appropriate new terminator.
Craig Topperf40110f2014-04-25 05:29:35 +00002597 if (!KeepEdge1 && !KeepEdge2) {
Frits van Bommel8e158492011-01-11 12:52:11 +00002598 if (TrueBB == FalseBB)
2599 // We were only looking for one successor, and it was present.
2600 // Create an unconditional branch to it.
Devang Patel2c2ea222011-05-18 18:43:31 +00002601 Builder.CreateBr(TrueBB);
Manman Ren774246a2012-09-17 22:28:55 +00002602 else {
Frits van Bommel8e158492011-01-11 12:52:11 +00002603 // We found both of the successors we were looking for.
2604 // Create a conditional branch sharing the condition of the select.
Manman Ren774246a2012-09-17 22:28:55 +00002605 BranchInst *NewBI = Builder.CreateCondBr(Cond, TrueBB, FalseBB);
2606 if (TrueWeight != FalseWeight)
2607 NewBI->setMetadata(LLVMContext::MD_prof,
2608 MDBuilder(OldTerm->getContext()).
2609 createBranchWeights(TrueWeight, FalseWeight));
2610 }
Frits van Bommel8e158492011-01-11 12:52:11 +00002611 } else if (KeepEdge1 && (KeepEdge2 || TrueBB == FalseBB)) {
2612 // Neither of the selected blocks were successors, so this
2613 // terminator must be unreachable.
2614 new UnreachableInst(OldTerm->getContext(), OldTerm);
2615 } else {
2616 // One of the selected values was a successor, but the other wasn't.
2617 // Insert an unconditional branch to the one that was found;
2618 // the edge to the one that wasn't must be unreachable.
Craig Topperf40110f2014-04-25 05:29:35 +00002619 if (!KeepEdge1)
Frits van Bommel8e158492011-01-11 12:52:11 +00002620 // Only TrueBB was found.
Devang Patel2c2ea222011-05-18 18:43:31 +00002621 Builder.CreateBr(TrueBB);
Frits van Bommel8e158492011-01-11 12:52:11 +00002622 else
2623 // Only FalseBB was found.
Devang Patel2c2ea222011-05-18 18:43:31 +00002624 Builder.CreateBr(FalseBB);
Frits van Bommel8e158492011-01-11 12:52:11 +00002625 }
2626
2627 EraseTerminatorInstAndDCECond(OldTerm);
2628 return true;
2629}
2630
Sanjay Patel09159b8f2015-06-24 20:40:57 +00002631// Replaces
Frits van Bommel8ae07992011-02-28 09:44:07 +00002632// (switch (select cond, X, Y)) on constant X, Y
2633// with a branch - conditional if X and Y lead to distinct BBs,
2634// unconditional otherwise.
2635static bool SimplifySwitchOnSelect(SwitchInst *SI, SelectInst *Select) {
2636 // Check for constant integer values in the select.
2637 ConstantInt *TrueVal = dyn_cast<ConstantInt>(Select->getTrueValue());
2638 ConstantInt *FalseVal = dyn_cast<ConstantInt>(Select->getFalseValue());
2639 if (!TrueVal || !FalseVal)
2640 return false;
2641
2642 // Find the relevant condition and destinations.
2643 Value *Condition = Select->getCondition();
Stepan Dyatkovskiy5b648af2012-03-08 07:06:20 +00002644 BasicBlock *TrueBB = SI->findCaseValue(TrueVal).getCaseSuccessor();
2645 BasicBlock *FalseBB = SI->findCaseValue(FalseVal).getCaseSuccessor();
Frits van Bommel8ae07992011-02-28 09:44:07 +00002646
Manman Ren774246a2012-09-17 22:28:55 +00002647 // Get weight for TrueBB and FalseBB.
2648 uint32_t TrueWeight = 0, FalseWeight = 0;
2649 SmallVector<uint64_t, 8> Weights;
2650 bool HasWeights = HasBranchWeights(SI);
2651 if (HasWeights) {
2652 GetBranchWeights(SI, Weights);
2653 if (Weights.size() == 1 + SI->getNumCases()) {
2654 TrueWeight = (uint32_t)Weights[SI->findCaseValue(TrueVal).
2655 getSuccessorIndex()];
2656 FalseWeight = (uint32_t)Weights[SI->findCaseValue(FalseVal).
2657 getSuccessorIndex()];
2658 }
2659 }
2660
Frits van Bommel8ae07992011-02-28 09:44:07 +00002661 // Perform the actual simplification.
Manman Ren774246a2012-09-17 22:28:55 +00002662 return SimplifyTerminatorOnSelect(SI, Condition, TrueBB, FalseBB,
2663 TrueWeight, FalseWeight);
Frits van Bommel8ae07992011-02-28 09:44:07 +00002664}
2665
Sanjay Patel09159b8f2015-06-24 20:40:57 +00002666// Replaces
Frits van Bommel8fb69ee2010-12-05 18:29:03 +00002667// (indirectbr (select cond, blockaddress(@fn, BlockA),
2668// blockaddress(@fn, BlockB)))
2669// with
2670// (br cond, BlockA, BlockB).
2671static bool SimplifyIndirectBrOnSelect(IndirectBrInst *IBI, SelectInst *SI) {
2672 // Check that both operands of the select are block addresses.
2673 BlockAddress *TBA = dyn_cast<BlockAddress>(SI->getTrueValue());
2674 BlockAddress *FBA = dyn_cast<BlockAddress>(SI->getFalseValue());
2675 if (!TBA || !FBA)
2676 return false;
2677
2678 // Extract the actual blocks.
2679 BasicBlock *TrueBB = TBA->getBasicBlock();
2680 BasicBlock *FalseBB = FBA->getBasicBlock();
2681
Frits van Bommel8e158492011-01-11 12:52:11 +00002682 // Perform the actual simplification.
Manman Ren774246a2012-09-17 22:28:55 +00002683 return SimplifyTerminatorOnSelect(IBI, SI->getCondition(), TrueBB, FalseBB,
2684 0, 0);
Frits van Bommel8fb69ee2010-12-05 18:29:03 +00002685}
2686
Sanjay Patel09159b8f2015-06-24 20:40:57 +00002687/// This is called when we find an icmp instruction
2688/// (a seteq/setne with a constant) as the only instruction in a
Chris Lattnerd9bacc02010-12-13 03:18:54 +00002689/// block that ends with an uncond branch. We are looking for a very specific
2690/// pattern that occurs when "A == 1 || A == 2 || A == 3" gets simplified. In
2691/// this case, we merge the first two "or's of icmp" into a switch, but then the
2692/// default value goes to an uncond block with a seteq in it, we get something
2693/// like:
2694///
2695/// switch i8 %A, label %DEFAULT [ i8 1, label %end i8 2, label %end ]
2696/// DEFAULT:
2697/// %tmp = icmp eq i8 %A, 92
2698/// br label %end
2699/// end:
2700/// ... = phi i1 [ true, %entry ], [ %tmp, %DEFAULT ], [ true, %entry ]
Andrew Trickf3cf1932012-08-29 21:46:36 +00002701///
Chris Lattnerd9bacc02010-12-13 03:18:54 +00002702/// We prefer to split the edge to 'end' so that there is a true/false entry to
2703/// the PHI, merging the third icmp into the switch.
Chandler Carruth0b4ef9c2013-01-07 03:53:25 +00002704static bool TryToSimplifyUncondBranchWithICmpInIt(
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002705 ICmpInst *ICI, IRBuilder<> &Builder, const DataLayout &DL,
2706 const TargetTransformInfo &TTI, unsigned BonusInstThreshold,
2707 AssumptionCache *AC) {
Chris Lattnerd9bacc02010-12-13 03:18:54 +00002708 BasicBlock *BB = ICI->getParent();
Devang Patel767f6932011-05-18 18:28:48 +00002709
Chris Lattnerd9bacc02010-12-13 03:18:54 +00002710 // If the block has any PHIs in it or the icmp has multiple uses, it is too
2711 // complex.
2712 if (isa<PHINode>(BB->begin()) || !ICI->hasOneUse()) return false;
2713
2714 Value *V = ICI->getOperand(0);
2715 ConstantInt *Cst = cast<ConstantInt>(ICI->getOperand(1));
Andrew Trickf3cf1932012-08-29 21:46:36 +00002716
Chris Lattnerd9bacc02010-12-13 03:18:54 +00002717 // The pattern we're looking for is where our only predecessor is a switch on
2718 // 'V' and this block is the default case for the switch. In this case we can
2719 // fold the compared value into the switch to simplify things.
2720 BasicBlock *Pred = BB->getSinglePredecessor();
Craig Topperf40110f2014-04-25 05:29:35 +00002721 if (!Pred || !isa<SwitchInst>(Pred->getTerminator())) return false;
Andrew Trickf3cf1932012-08-29 21:46:36 +00002722
Chris Lattnerd9bacc02010-12-13 03:18:54 +00002723 SwitchInst *SI = cast<SwitchInst>(Pred->getTerminator());
2724 if (SI->getCondition() != V)
2725 return false;
Andrew Trickf3cf1932012-08-29 21:46:36 +00002726
Chris Lattnerd9bacc02010-12-13 03:18:54 +00002727 // If BB is reachable on a non-default case, then we simply know the value of
2728 // V in this block. Substitute it and constant fold the icmp instruction
2729 // away.
2730 if (SI->getDefaultDest() != BB) {
2731 ConstantInt *VVal = SI->findCaseDest(BB);
2732 assert(VVal && "Should have a unique destination value");
2733 ICI->setOperand(0, VVal);
Andrew Trickf3cf1932012-08-29 21:46:36 +00002734
Rafael Espindola37dc9e12014-02-21 00:06:31 +00002735 if (Value *V = SimplifyInstruction(ICI, DL)) {
Chris Lattnerd7beca32010-12-14 06:17:25 +00002736 ICI->replaceAllUsesWith(V);
Chris Lattnerd9bacc02010-12-13 03:18:54 +00002737 ICI->eraseFromParent();
2738 }
2739 // BB is now empty, so it is likely to simplify away.
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002740 return SimplifyCFG(BB, TTI, BonusInstThreshold, AC) | true;
Chris Lattnerd9bacc02010-12-13 03:18:54 +00002741 }
Andrew Trickf3cf1932012-08-29 21:46:36 +00002742
Chris Lattner62cc76e2010-12-13 03:43:57 +00002743 // Ok, the block is reachable from the default dest. If the constant we're
2744 // comparing exists in one of the other edges, then we can constant fold ICI
2745 // and zap it.
Stepan Dyatkovskiy97b02fc2012-03-11 06:09:17 +00002746 if (SI->findCaseValue(Cst) != SI->case_default()) {
Chris Lattner62cc76e2010-12-13 03:43:57 +00002747 Value *V;
2748 if (ICI->getPredicate() == ICmpInst::ICMP_EQ)
2749 V = ConstantInt::getFalse(BB->getContext());
2750 else
2751 V = ConstantInt::getTrue(BB->getContext());
Andrew Trickf3cf1932012-08-29 21:46:36 +00002752
Chris Lattner62cc76e2010-12-13 03:43:57 +00002753 ICI->replaceAllUsesWith(V);
2754 ICI->eraseFromParent();
2755 // BB is now empty, so it is likely to simplify away.
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002756 return SimplifyCFG(BB, TTI, BonusInstThreshold, AC) | true;
Chris Lattner62cc76e2010-12-13 03:43:57 +00002757 }
Andrew Trickf3cf1932012-08-29 21:46:36 +00002758
Chris Lattnerd9bacc02010-12-13 03:18:54 +00002759 // The use of the icmp has to be in the 'end' block, by the only PHI node in
2760 // the block.
2761 BasicBlock *SuccBlock = BB->getTerminator()->getSuccessor(0);
Chandler Carruthcdf47882014-03-09 03:16:01 +00002762 PHINode *PHIUse = dyn_cast<PHINode>(ICI->user_back());
Craig Topperf40110f2014-04-25 05:29:35 +00002763 if (PHIUse == nullptr || PHIUse != &SuccBlock->front() ||
Chris Lattnerd9bacc02010-12-13 03:18:54 +00002764 isa<PHINode>(++BasicBlock::iterator(PHIUse)))
2765 return false;
2766
2767 // If the icmp is a SETEQ, then the default dest gets false, the new edge gets
2768 // true in the PHI.
2769 Constant *DefaultCst = ConstantInt::getTrue(BB->getContext());
2770 Constant *NewCst = ConstantInt::getFalse(BB->getContext());
2771
2772 if (ICI->getPredicate() == ICmpInst::ICMP_EQ)
2773 std::swap(DefaultCst, NewCst);
2774
2775 // Replace ICI (which is used by the PHI for the default value) with true or
2776 // false depending on if it is EQ or NE.
2777 ICI->replaceAllUsesWith(DefaultCst);
2778 ICI->eraseFromParent();
2779
2780 // Okay, the switch goes to this block on a default value. Add an edge from
2781 // the switch to the merge point on the compared value.
2782 BasicBlock *NewBB = BasicBlock::Create(BB->getContext(), "switch.edge",
2783 BB->getParent(), BB);
Manman Rence48ea72012-09-17 23:07:43 +00002784 SmallVector<uint64_t, 8> Weights;
2785 bool HasWeights = HasBranchWeights(SI);
2786 if (HasWeights) {
2787 GetBranchWeights(SI, Weights);
2788 if (Weights.size() == 1 + SI->getNumCases()) {
2789 // Split weight for default case to case for "Cst".
2790 Weights[0] = (Weights[0]+1) >> 1;
2791 Weights.push_back(Weights[0]);
2792
2793 SmallVector<uint32_t, 8> MDWeights(Weights.begin(), Weights.end());
2794 SI->setMetadata(LLVMContext::MD_prof,
2795 MDBuilder(SI->getContext()).
2796 createBranchWeights(MDWeights));
2797 }
2798 }
Chris Lattnerd9bacc02010-12-13 03:18:54 +00002799 SI->addCase(Cst, NewBB);
Andrew Trickf3cf1932012-08-29 21:46:36 +00002800
Chris Lattnerd9bacc02010-12-13 03:18:54 +00002801 // NewBB branches to the phi block, add the uncond branch and the phi entry.
Devang Patel767f6932011-05-18 18:28:48 +00002802 Builder.SetInsertPoint(NewBB);
2803 Builder.SetCurrentDebugLocation(SI->getDebugLoc());
2804 Builder.CreateBr(SuccBlock);
Chris Lattnerd9bacc02010-12-13 03:18:54 +00002805 PHIUse->addIncoming(NewCst, NewBB);
2806 return true;
2807}
2808
Sanjay Patel09159b8f2015-06-24 20:40:57 +00002809/// The specified branch is a conditional branch.
Chris Lattnera69c4432010-12-13 05:03:41 +00002810/// Check to see if it is branching on an or/and chain of icmp instructions, and
2811/// fold it into a switch instruction if so.
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002812static bool SimplifyBranchOnICmpChain(BranchInst *BI, IRBuilder<> &Builder,
2813 const DataLayout &DL) {
Chris Lattnera69c4432010-12-13 05:03:41 +00002814 Instruction *Cond = dyn_cast<Instruction>(BI->getCondition());
Craig Topperf40110f2014-04-25 05:29:35 +00002815 if (!Cond) return false;
Andrew Trickf3cf1932012-08-29 21:46:36 +00002816
Chris Lattnera69c4432010-12-13 05:03:41 +00002817 // Change br (X == 0 | X == 1), T, F into a switch instruction.
2818 // If this is a bunch of seteq's or'd together, or if it's a bunch of
2819 // 'setne's and'ed together, collect them.
Andrew Trickf3cf1932012-08-29 21:46:36 +00002820
Mehdi Amini9a25cb82014-11-19 20:09:11 +00002821 // Try to gather values from a chain of and/or to be turned into a switch
Mehdi Aminiffd01002014-11-20 22:40:25 +00002822 ConstantComparesGatherer ConstantCompare(Cond, DL);
2823 // Unpack the result
2824 SmallVectorImpl<ConstantInt*> &Values = ConstantCompare.Vals;
2825 Value *CompVal = ConstantCompare.CompValue;
2826 unsigned UsedICmps = ConstantCompare.UsedICmps;
2827 Value *ExtraCase = ConstantCompare.Extra;
Andrew Trickf3cf1932012-08-29 21:46:36 +00002828
Chris Lattnera69c4432010-12-13 05:03:41 +00002829 // If we didn't have a multiply compared value, fail.
Craig Topperf40110f2014-04-25 05:29:35 +00002830 if (!CompVal) return false;
Chris Lattnera69c4432010-12-13 05:03:41 +00002831
Benjamin Kramer8d6a8c12011-02-07 22:37:28 +00002832 // Avoid turning single icmps into a switch.
2833 if (UsedICmps <= 1)
2834 return false;
2835
Mehdi Aminiffd01002014-11-20 22:40:25 +00002836 bool TrueWhenEqual = (Cond->getOpcode() == Instruction::Or);
2837
Chris Lattnera69c4432010-12-13 05:03:41 +00002838 // There might be duplicate constants in the list, which the switch
2839 // instruction can't handle, remove them now.
2840 array_pod_sort(Values.begin(), Values.end(), ConstantIntSortPredicate);
2841 Values.erase(std::unique(Values.begin(), Values.end()), Values.end());
Andrew Trickf3cf1932012-08-29 21:46:36 +00002842
Chris Lattnera69c4432010-12-13 05:03:41 +00002843 // If Extra was used, we require at least two switch values to do the
Sanjay Patel59661452015-09-10 15:14:34 +00002844 // transformation. A switch with one value is just a conditional branch.
Chris Lattnera69c4432010-12-13 05:03:41 +00002845 if (ExtraCase && Values.size() < 2) return false;
Andrew Trickf3cf1932012-08-29 21:46:36 +00002846
Andrew Trick3051aa12012-08-29 21:46:38 +00002847 // TODO: Preserve branch weight metadata, similarly to how
2848 // FoldValueComparisonIntoPredecessors preserves it.
2849
Chris Lattnera69c4432010-12-13 05:03:41 +00002850 // Figure out which block is which destination.
2851 BasicBlock *DefaultBB = BI->getSuccessor(1);
2852 BasicBlock *EdgeBB = BI->getSuccessor(0);
2853 if (!TrueWhenEqual) std::swap(DefaultBB, EdgeBB);
Andrew Trickf3cf1932012-08-29 21:46:36 +00002854
Chris Lattnera69c4432010-12-13 05:03:41 +00002855 BasicBlock *BB = BI->getParent();
Andrew Trickf3cf1932012-08-29 21:46:36 +00002856
Chris Lattnerd7beca32010-12-14 06:17:25 +00002857 DEBUG(dbgs() << "Converting 'icmp' chain with " << Values.size()
Chris Lattner5a9d59d2010-12-14 05:57:30 +00002858 << " cases into SWITCH. BB is:\n" << *BB);
Andrew Trickf3cf1932012-08-29 21:46:36 +00002859
Chris Lattnera69c4432010-12-13 05:03:41 +00002860 // If there are any extra values that couldn't be folded into the switch
2861 // then we evaluate them with an explicit branch first. Split the block
2862 // right before the condbr to handle it.
2863 if (ExtraCase) {
Duncan P. N. Exon Smith5b4c8372015-10-13 02:39:05 +00002864 BasicBlock *NewBB =
2865 BB->splitBasicBlock(BI->getIterator(), "switch.early.test");
Chris Lattnera69c4432010-12-13 05:03:41 +00002866 // Remove the uncond branch added to the old block.
2867 TerminatorInst *OldTI = BB->getTerminator();
Devang Patel7de6c4b2011-05-18 23:18:47 +00002868 Builder.SetInsertPoint(OldTI);
2869
Chris Lattner5a9d59d2010-12-14 05:57:30 +00002870 if (TrueWhenEqual)
Devang Patel7de6c4b2011-05-18 23:18:47 +00002871 Builder.CreateCondBr(ExtraCase, EdgeBB, NewBB);
Chris Lattner5a9d59d2010-12-14 05:57:30 +00002872 else
Devang Patel7de6c4b2011-05-18 23:18:47 +00002873 Builder.CreateCondBr(ExtraCase, NewBB, EdgeBB);
Andrew Trickf3cf1932012-08-29 21:46:36 +00002874
Chris Lattnera69c4432010-12-13 05:03:41 +00002875 OldTI->eraseFromParent();
Andrew Trickf3cf1932012-08-29 21:46:36 +00002876
Chris Lattnercb570f82010-12-13 05:34:18 +00002877 // If there are PHI nodes in EdgeBB, then we need to add a new entry to them
2878 // for the edge we just added.
Chris Lattner0f4d67b2010-12-14 07:09:42 +00002879 AddPredecessorToBlock(EdgeBB, BB, NewBB);
Andrew Trickf3cf1932012-08-29 21:46:36 +00002880
Chris Lattnerd7beca32010-12-14 06:17:25 +00002881 DEBUG(dbgs() << " ** 'icmp' chain unhandled condition: " << *ExtraCase
2882 << "\nEXTRABB = " << *BB);
Chris Lattnera69c4432010-12-13 05:03:41 +00002883 BB = NewBB;
2884 }
Devang Patel7de6c4b2011-05-18 23:18:47 +00002885
2886 Builder.SetInsertPoint(BI);
Chris Lattnera69c4432010-12-13 05:03:41 +00002887 // Convert pointer to int before we switch.
2888 if (CompVal->getType()->isPointerTy()) {
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002889 CompVal = Builder.CreatePtrToInt(
2890 CompVal, DL.getIntPtrType(CompVal->getType()), "magicptr");
Chris Lattnera69c4432010-12-13 05:03:41 +00002891 }
Andrew Trickf3cf1932012-08-29 21:46:36 +00002892
Chris Lattnera69c4432010-12-13 05:03:41 +00002893 // Create the new switch instruction now.
Devang Patel7de6c4b2011-05-18 23:18:47 +00002894 SwitchInst *New = Builder.CreateSwitch(CompVal, DefaultBB, Values.size());
Devang Patelb849cd52011-05-17 23:29:05 +00002895
Chris Lattnera69c4432010-12-13 05:03:41 +00002896 // Add all of the 'cases' to the switch instruction.
2897 for (unsigned i = 0, e = Values.size(); i != e; ++i)
2898 New->addCase(Values[i], EdgeBB);
Andrew Trickf3cf1932012-08-29 21:46:36 +00002899
Chris Lattnera69c4432010-12-13 05:03:41 +00002900 // We added edges from PI to the EdgeBB. As such, if there were any
2901 // PHI nodes in EdgeBB, they need entries to be added corresponding to
2902 // the number of edges added.
2903 for (BasicBlock::iterator BBI = EdgeBB->begin();
2904 isa<PHINode>(BBI); ++BBI) {
2905 PHINode *PN = cast<PHINode>(BBI);
2906 Value *InVal = PN->getIncomingValueForBlock(BB);
2907 for (unsigned i = 0, e = Values.size()-1; i != e; ++i)
2908 PN->addIncoming(InVal, BB);
2909 }
Andrew Trickf3cf1932012-08-29 21:46:36 +00002910
Chris Lattnera69c4432010-12-13 05:03:41 +00002911 // Erase the old branch instruction.
2912 EraseTerminatorInstAndDCECond(BI);
Andrew Trickf3cf1932012-08-29 21:46:36 +00002913
Chris Lattnerd7beca32010-12-14 06:17:25 +00002914 DEBUG(dbgs() << " ** 'icmp' chain result is:\n" << *BB << '\n');
Chris Lattnera69c4432010-12-13 05:03:41 +00002915 return true;
2916}
2917
Duncan Sands29192d02011-09-05 12:57:57 +00002918bool SimplifyCFGOpt::SimplifyResume(ResumeInst *RI, IRBuilder<> &Builder) {
2919 // If this is a trivial landing pad that just continues unwinding the caught
2920 // exception then zap the landing pad, turning its invokes into calls.
2921 BasicBlock *BB = RI->getParent();
2922 LandingPadInst *LPInst = dyn_cast<LandingPadInst>(BB->getFirstNonPHI());
Chen Li7009cd32015-10-23 21:13:01 +00002923 if (RI->getValue() != LPInst)
2924 // Not a landing pad, or the resume is not unwinding the exception that
2925 // caused control to branch here.
Duncan Sands29192d02011-09-05 12:57:57 +00002926 return false;
2927
Chen Li7009cd32015-10-23 21:13:01 +00002928 // Check that there are no other instructions except for debug intrinsics.
2929 BasicBlock::iterator I = LPInst->getIterator(), E = RI->getIterator();
Duncan Sands29192d02011-09-05 12:57:57 +00002930 while (++I != E)
2931 if (!isa<DbgInfoIntrinsic>(I))
2932 return false;
2933
Chen Lic6e28782015-10-22 20:48:38 +00002934 // Turn all invokes that unwind here into calls and delete the basic block.
Chen Li7009cd32015-10-23 21:13:01 +00002935 for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB); PI != PE;) {
2936 BasicBlock *Pred = *PI++;
2937 removeUnwindEdge(Pred);
Chen Lic6e28782015-10-22 20:48:38 +00002938 }
2939
Chen Li7009cd32015-10-23 21:13:01 +00002940 // The landingpad is now unreachable. Zap it.
2941 BB->eraseFromParent();
2942 return true;
Duncan Sands29192d02011-09-05 12:57:57 +00002943}
2944
Andrew Kaylor50e4e862015-09-04 23:39:40 +00002945bool SimplifyCFGOpt::SimplifyCleanupReturn(CleanupReturnInst *RI) {
2946 // If this is a trivial cleanup pad that executes no instructions, it can be
2947 // eliminated. If the cleanup pad continues to the caller, any predecessor
2948 // that is an EH pad will be updated to continue to the caller and any
2949 // predecessor that terminates with an invoke instruction will have its invoke
2950 // instruction converted to a call instruction. If the cleanup pad being
2951 // simplified does not continue to the caller, each predecessor will be
2952 // updated to continue to the unwind destination of the cleanup pad being
2953 // simplified.
2954 BasicBlock *BB = RI->getParent();
2955 Instruction *CPInst = dyn_cast<CleanupPadInst>(BB->getFirstNonPHI());
2956 if (!CPInst)
2957 // This isn't an empty cleanup.
2958 return false;
2959
2960 // Check that there are no other instructions except for debug intrinsics.
Duncan P. N. Exon Smith5b4c8372015-10-13 02:39:05 +00002961 BasicBlock::iterator I = CPInst->getIterator(), E = RI->getIterator();
Andrew Kaylor50e4e862015-09-04 23:39:40 +00002962 while (++I != E)
2963 if (!isa<DbgInfoIntrinsic>(I))
2964 return false;
2965
2966 // If the cleanup return we are simplifying unwinds to the caller, this
2967 // will set UnwindDest to nullptr.
2968 BasicBlock *UnwindDest = RI->getUnwindDest();
2969
2970 // We're about to remove BB from the control flow. Before we do, sink any
2971 // PHINodes into the unwind destination. Doing this before changing the
2972 // control flow avoids some potentially slow checks, since we can currently
2973 // be certain that UnwindDest and BB have no common predecessors (since they
2974 // are both EH pads).
2975 if (UnwindDest) {
2976 // First, go through the PHI nodes in UnwindDest and update any nodes that
2977 // reference the block we are removing
Duncan P. N. Exon Smith5b4c8372015-10-13 02:39:05 +00002978 for (BasicBlock::iterator I = UnwindDest->begin(),
2979 IE = UnwindDest->getFirstNonPHI()->getIterator();
Andrew Kaylor50e4e862015-09-04 23:39:40 +00002980 I != IE; ++I) {
2981 PHINode *DestPN = cast<PHINode>(I);
2982
Andrew Kaylor2a9a6d82015-09-05 01:00:51 +00002983 int Idx = DestPN->getBasicBlockIndex(BB);
Andrew Kaylor50e4e862015-09-04 23:39:40 +00002984 // Since BB unwinds to UnwindDest, it has to be in the PHI node.
Craig Topper02a55d72015-09-05 04:49:44 +00002985 assert(Idx != -1);
Andrew Kaylor50e4e862015-09-04 23:39:40 +00002986 // This PHI node has an incoming value that corresponds to a control
2987 // path through the cleanup pad we are removing. If the incoming
2988 // value is in the cleanup pad, it must be a PHINode (because we
2989 // verified above that the block is otherwise empty). Otherwise, the
2990 // value is either a constant or a value that dominates the cleanup
2991 // pad being removed.
2992 //
2993 // Because BB and UnwindDest are both EH pads, all of their
2994 // predecessors must unwind to these blocks, and since no instruction
2995 // can have multiple unwind destinations, there will be no overlap in
2996 // incoming blocks between SrcPN and DestPN.
2997 Value *SrcVal = DestPN->getIncomingValue(Idx);
2998 PHINode *SrcPN = dyn_cast<PHINode>(SrcVal);
2999
3000 // Remove the entry for the block we are deleting.
3001 DestPN->removeIncomingValue(Idx, false);
3002
3003 if (SrcPN && SrcPN->getParent() == BB) {
3004 // If the incoming value was a PHI node in the cleanup pad we are
3005 // removing, we need to merge that PHI node's incoming values into
3006 // DestPN.
3007 for (unsigned SrcIdx = 0, SrcE = SrcPN->getNumIncomingValues();
3008 SrcIdx != SrcE; ++SrcIdx) {
3009 DestPN->addIncoming(SrcPN->getIncomingValue(SrcIdx),
3010 SrcPN->getIncomingBlock(SrcIdx));
3011 }
3012 } else {
3013 // Otherwise, the incoming value came from above BB and
3014 // so we can just reuse it. We must associate all of BB's
3015 // predecessors with this value.
3016 for (auto *pred : predecessors(BB)) {
3017 DestPN->addIncoming(SrcVal, pred);
3018 }
3019 }
3020 }
3021
3022 // Sink any remaining PHI nodes directly into UnwindDest.
3023 Instruction *InsertPt = UnwindDest->getFirstNonPHI();
Duncan P. N. Exon Smith5b4c8372015-10-13 02:39:05 +00003024 for (BasicBlock::iterator I = BB->begin(),
3025 IE = BB->getFirstNonPHI()->getIterator();
Andrew Kaylor50e4e862015-09-04 23:39:40 +00003026 I != IE;) {
3027 // The iterator must be incremented here because the instructions are
3028 // being moved to another block.
3029 PHINode *PN = cast<PHINode>(I++);
3030 if (PN->use_empty())
3031 // If the PHI node has no uses, just leave it. It will be erased
3032 // when we erase BB below.
3033 continue;
3034
3035 // Otherwise, sink this PHI node into UnwindDest.
3036 // Any predecessors to UnwindDest which are not already represented
3037 // must be back edges which inherit the value from the path through
3038 // BB. In this case, the PHI value must reference itself.
3039 for (auto *pred : predecessors(UnwindDest))
3040 if (pred != BB)
3041 PN->addIncoming(PN, pred);
3042 PN->moveBefore(InsertPt);
3043 }
3044 }
3045
3046 for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB); PI != PE;) {
3047 // The iterator must be updated here because we are removing this pred.
3048 BasicBlock *PredBB = *PI++;
Andrew Kaylor50e4e862015-09-04 23:39:40 +00003049 if (UnwindDest == nullptr) {
Joseph Tremoulet09af67a2015-09-27 01:47:46 +00003050 removeUnwindEdge(PredBB);
Andrew Kaylor50e4e862015-09-04 23:39:40 +00003051 } else {
Andrew Kaylor50e4e862015-09-04 23:39:40 +00003052 TerminatorInst *TI = PredBB->getTerminator();
Joseph Tremoulet09af67a2015-09-27 01:47:46 +00003053 TI->replaceUsesOfWith(BB, UnwindDest);
Andrew Kaylor50e4e862015-09-04 23:39:40 +00003054 }
3055 }
3056
3057 // The cleanup pad is now unreachable. Zap it.
3058 BB->eraseFromParent();
3059 return true;
3060}
3061
Devang Pateldd14e0f2011-05-18 21:33:11 +00003062bool SimplifyCFGOpt::SimplifyReturn(ReturnInst *RI, IRBuilder<> &Builder) {
Chris Lattner25c3af32010-12-13 06:25:44 +00003063 BasicBlock *BB = RI->getParent();
3064 if (!BB->getFirstNonPHIOrDbg()->isTerminator()) return false;
Andrew Trickf3cf1932012-08-29 21:46:36 +00003065
Chris Lattner25c3af32010-12-13 06:25:44 +00003066 // Find predecessors that end with branches.
3067 SmallVector<BasicBlock*, 8> UncondBranchPreds;
3068 SmallVector<BranchInst*, 8> CondBranchPreds;
Duncan P. N. Exon Smith6c990152014-07-21 17:06:51 +00003069 for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) {
3070 BasicBlock *P = *PI;
Chris Lattner25c3af32010-12-13 06:25:44 +00003071 TerminatorInst *PTI = P->getTerminator();
3072 if (BranchInst *BI = dyn_cast<BranchInst>(PTI)) {
3073 if (BI->isUnconditional())
3074 UncondBranchPreds.push_back(P);
3075 else
3076 CondBranchPreds.push_back(BI);
3077 }
3078 }
Andrew Trickf3cf1932012-08-29 21:46:36 +00003079
Chris Lattner25c3af32010-12-13 06:25:44 +00003080 // If we found some, do the transformation!
Evan Chengd983eba2011-01-29 04:46:23 +00003081 if (!UncondBranchPreds.empty() && DupRet) {
Chris Lattner25c3af32010-12-13 06:25:44 +00003082 while (!UncondBranchPreds.empty()) {
3083 BasicBlock *Pred = UncondBranchPreds.pop_back_val();
3084 DEBUG(dbgs() << "FOLDING: " << *BB
3085 << "INTO UNCOND BRANCH PRED: " << *Pred);
Evan Chengd983eba2011-01-29 04:46:23 +00003086 (void)FoldReturnIntoUncondBranch(RI, BB, Pred);
Chris Lattner25c3af32010-12-13 06:25:44 +00003087 }
Andrew Trickf3cf1932012-08-29 21:46:36 +00003088
Chris Lattner25c3af32010-12-13 06:25:44 +00003089 // If we eliminated all predecessors of the block, delete the block now.
Ramkumar Ramachandra40c3e032015-01-13 03:46:47 +00003090 if (pred_empty(BB))
Chris Lattner25c3af32010-12-13 06:25:44 +00003091 // We know there are no successors, so just nuke the block.
3092 BB->eraseFromParent();
Andrew Trickf3cf1932012-08-29 21:46:36 +00003093
Chris Lattner25c3af32010-12-13 06:25:44 +00003094 return true;
3095 }
Andrew Trickf3cf1932012-08-29 21:46:36 +00003096
Chris Lattner25c3af32010-12-13 06:25:44 +00003097 // Check out all of the conditional branches going to this return
3098 // instruction. If any of them just select between returns, change the
3099 // branch itself into a select/return pair.
3100 while (!CondBranchPreds.empty()) {
3101 BranchInst *BI = CondBranchPreds.pop_back_val();
Andrew Trickf3cf1932012-08-29 21:46:36 +00003102
Chris Lattner25c3af32010-12-13 06:25:44 +00003103 // Check to see if the non-BB successor is also a return block.
3104 if (isa<ReturnInst>(BI->getSuccessor(0)->getTerminator()) &&
3105 isa<ReturnInst>(BI->getSuccessor(1)->getTerminator()) &&
Devang Pateldd14e0f2011-05-18 21:33:11 +00003106 SimplifyCondBranchToTwoReturns(BI, Builder))
Chris Lattner25c3af32010-12-13 06:25:44 +00003107 return true;
3108 }
3109 return false;
3110}
3111
Chris Lattner25c3af32010-12-13 06:25:44 +00003112bool SimplifyCFGOpt::SimplifyUnreachable(UnreachableInst *UI) {
3113 BasicBlock *BB = UI->getParent();
Andrew Trickf3cf1932012-08-29 21:46:36 +00003114
Chris Lattner25c3af32010-12-13 06:25:44 +00003115 bool Changed = false;
Andrew Trickf3cf1932012-08-29 21:46:36 +00003116
Chris Lattner25c3af32010-12-13 06:25:44 +00003117 // If there are any instructions immediately before the unreachable that can
3118 // be removed, do so.
Duncan P. N. Exon Smith5b4c8372015-10-13 02:39:05 +00003119 while (UI->getIterator() != BB->begin()) {
3120 BasicBlock::iterator BBI = UI->getIterator();
Chris Lattner25c3af32010-12-13 06:25:44 +00003121 --BBI;
Eli Friedman0ffdf2e2011-08-15 23:59:28 +00003122 // Do not delete instructions that can have side effects which might cause
3123 // the unreachable to not be reachable; specifically, calls and volatile
3124 // operations may have this effect.
Chris Lattner25c3af32010-12-13 06:25:44 +00003125 if (isa<CallInst>(BBI) && !isa<DbgInfoIntrinsic>(BBI)) break;
Eli Friedman0ffdf2e2011-08-15 23:59:28 +00003126
3127 if (BBI->mayHaveSideEffects()) {
3128 if (StoreInst *SI = dyn_cast<StoreInst>(BBI)) {
3129 if (SI->isVolatile())
3130 break;
3131 } else if (LoadInst *LI = dyn_cast<LoadInst>(BBI)) {
3132 if (LI->isVolatile())
3133 break;
3134 } else if (AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(BBI)) {
3135 if (RMWI->isVolatile())
3136 break;
3137 } else if (AtomicCmpXchgInst *CXI = dyn_cast<AtomicCmpXchgInst>(BBI)) {
3138 if (CXI->isVolatile())
3139 break;
3140 } else if (!isa<FenceInst>(BBI) && !isa<VAArgInst>(BBI) &&
3141 !isa<LandingPadInst>(BBI)) {
Chris Lattner25c3af32010-12-13 06:25:44 +00003142 break;
Eli Friedman0ffdf2e2011-08-15 23:59:28 +00003143 }
Bill Wendling55d875f2011-08-16 20:41:17 +00003144 // Note that deleting LandingPad's here is in fact okay, although it
3145 // involves a bit of subtle reasoning. If this inst is a LandingPad,
3146 // all the predecessors of this block will be the unwind edges of Invokes,
3147 // and we can therefore guarantee this block will be erased.
Eli Friedman0ffdf2e2011-08-15 23:59:28 +00003148 }
3149
Eli Friedmanaac35b32011-03-09 00:48:33 +00003150 // Delete this instruction (any uses are guaranteed to be dead)
3151 if (!BBI->use_empty())
3152 BBI->replaceAllUsesWith(UndefValue::get(BBI->getType()));
Chris Lattnerd7beca32010-12-14 06:17:25 +00003153 BBI->eraseFromParent();
Chris Lattner25c3af32010-12-13 06:25:44 +00003154 Changed = true;
3155 }
Andrew Trickf3cf1932012-08-29 21:46:36 +00003156
Chris Lattner25c3af32010-12-13 06:25:44 +00003157 // If the unreachable instruction is the first in the block, take a gander
3158 // at all of the predecessors of this instruction, and simplify them.
3159 if (&BB->front() != UI) return Changed;
Andrew Trickf3cf1932012-08-29 21:46:36 +00003160
Chris Lattner25c3af32010-12-13 06:25:44 +00003161 SmallVector<BasicBlock*, 8> Preds(pred_begin(BB), pred_end(BB));
3162 for (unsigned i = 0, e = Preds.size(); i != e; ++i) {
3163 TerminatorInst *TI = Preds[i]->getTerminator();
Devang Patel31458a02011-05-19 00:09:21 +00003164 IRBuilder<> Builder(TI);
Chris Lattner25c3af32010-12-13 06:25:44 +00003165 if (BranchInst *BI = dyn_cast<BranchInst>(TI)) {
3166 if (BI->isUnconditional()) {
3167 if (BI->getSuccessor(0) == BB) {
3168 new UnreachableInst(TI->getContext(), TI);
3169 TI->eraseFromParent();
3170 Changed = true;
3171 }
3172 } else {
3173 if (BI->getSuccessor(0) == BB) {
Devang Patel31458a02011-05-19 00:09:21 +00003174 Builder.CreateBr(BI->getSuccessor(1));
Chris Lattner25c3af32010-12-13 06:25:44 +00003175 EraseTerminatorInstAndDCECond(BI);
3176 } else if (BI->getSuccessor(1) == BB) {
Devang Patel31458a02011-05-19 00:09:21 +00003177 Builder.CreateBr(BI->getSuccessor(0));
Chris Lattner25c3af32010-12-13 06:25:44 +00003178 EraseTerminatorInstAndDCECond(BI);
3179 Changed = true;
3180 }
3181 }
3182 } else if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) {
Stepan Dyatkovskiy97b02fc2012-03-11 06:09:17 +00003183 for (SwitchInst::CaseIt i = SI->case_begin(), e = SI->case_end();
Stepan Dyatkovskiy5b648af2012-03-08 07:06:20 +00003184 i != e; ++i)
3185 if (i.getCaseSuccessor() == BB) {
Chris Lattner25c3af32010-12-13 06:25:44 +00003186 BB->removePredecessor(SI->getParent());
3187 SI->removeCase(i);
3188 --i; --e;
3189 Changed = true;
3190 }
Joseph Tremoulet09af67a2015-09-27 01:47:46 +00003191 } else if ((isa<InvokeInst>(TI) &&
3192 cast<InvokeInst>(TI)->getUnwindDest() == BB) ||
3193 isa<CatchEndPadInst>(TI) || isa<TerminatePadInst>(TI)) {
3194 removeUnwindEdge(TI->getParent());
3195 Changed = true;
David Majnemer49293702015-10-27 22:43:56 +00003196 } else if (isa<CleanupReturnInst>(TI) || isa<CleanupEndPadInst>(TI)) {
Joseph Tremoulet09af67a2015-09-27 01:47:46 +00003197 new UnreachableInst(TI->getContext(), TI);
3198 TI->eraseFromParent();
3199 Changed = true;
Chris Lattner25c3af32010-12-13 06:25:44 +00003200 }
Joseph Tremoulet09af67a2015-09-27 01:47:46 +00003201 // TODO: If TI is a CatchPadInst, then (BB must be its normal dest and)
3202 // we can eliminate it, redirecting its preds to its unwind successor,
3203 // or to the next outer handler if the removed catch is the last for its
3204 // catchendpad.
Chris Lattner25c3af32010-12-13 06:25:44 +00003205 }
Andrew Trickf3cf1932012-08-29 21:46:36 +00003206
Chris Lattner25c3af32010-12-13 06:25:44 +00003207 // If this block is now dead, remove it.
Ramkumar Ramachandra40c3e032015-01-13 03:46:47 +00003208 if (pred_empty(BB) &&
Chris Lattner25c3af32010-12-13 06:25:44 +00003209 BB != &BB->getParent()->getEntryBlock()) {
3210 // We know there are no successors, so just nuke the block.
3211 BB->eraseFromParent();
3212 return true;
3213 }
3214
3215 return Changed;
3216}
3217
Hans Wennborg68000082015-01-26 19:52:32 +00003218static bool CasesAreContiguous(SmallVectorImpl<ConstantInt *> &Cases) {
3219 assert(Cases.size() >= 1);
3220
3221 array_pod_sort(Cases.begin(), Cases.end(), ConstantIntSortPredicate);
3222 for (size_t I = 1, E = Cases.size(); I != E; ++I) {
3223 if (Cases[I - 1]->getValue() != Cases[I]->getValue() + 1)
3224 return false;
3225 }
3226 return true;
3227}
3228
3229/// Turn a switch with two reachable destinations into an integer range
3230/// comparison and branch.
Devang Patela7ec47d2011-05-18 20:35:38 +00003231static bool TurnSwitchRangeIntoICmp(SwitchInst *SI, IRBuilder<> &Builder) {
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003232 assert(SI->getNumCases() > 1 && "Degenerate switch?");
Benjamin Kramerf4ea1d52011-02-02 15:56:22 +00003233
Hans Wennborg68000082015-01-26 19:52:32 +00003234 bool HasDefault =
3235 !isa<UnreachableInst>(SI->getDefaultDest()->getFirstNonPHIOrDbg());
Benjamin Kramer62aa46b2011-02-03 22:51:41 +00003236
Hans Wennborg68000082015-01-26 19:52:32 +00003237 // Partition the cases into two sets with different destinations.
3238 BasicBlock *DestA = HasDefault ? SI->getDefaultDest() : nullptr;
3239 BasicBlock *DestB = nullptr;
3240 SmallVector <ConstantInt *, 16> CasesA;
3241 SmallVector <ConstantInt *, 16> CasesB;
3242
3243 for (SwitchInst::CaseIt I : SI->cases()) {
3244 BasicBlock *Dest = I.getCaseSuccessor();
3245 if (!DestA) DestA = Dest;
3246 if (Dest == DestA) {
3247 CasesA.push_back(I.getCaseValue());
3248 continue;
3249 }
3250 if (!DestB) DestB = Dest;
3251 if (Dest == DestB) {
3252 CasesB.push_back(I.getCaseValue());
3253 continue;
3254 }
3255 return false; // More than two destinations.
Benjamin Kramer62aa46b2011-02-03 22:51:41 +00003256 }
3257
Hans Wennborg68000082015-01-26 19:52:32 +00003258 assert(DestA && DestB && "Single-destination switch should have been folded.");
3259 assert(DestA != DestB);
3260 assert(DestB != SI->getDefaultDest());
3261 assert(!CasesB.empty() && "There must be non-default cases.");
3262 assert(!CasesA.empty() || HasDefault);
3263
3264 // Figure out if one of the sets of cases form a contiguous range.
3265 SmallVectorImpl<ConstantInt *> *ContiguousCases = nullptr;
3266 BasicBlock *ContiguousDest = nullptr;
3267 BasicBlock *OtherDest = nullptr;
3268 if (!CasesA.empty() && CasesAreContiguous(CasesA)) {
3269 ContiguousCases = &CasesA;
3270 ContiguousDest = DestA;
3271 OtherDest = DestB;
3272 } else if (CasesAreContiguous(CasesB)) {
3273 ContiguousCases = &CasesB;
3274 ContiguousDest = DestB;
3275 OtherDest = DestA;
3276 } else
3277 return false;
3278
3279 // Start building the compare and branch.
3280
3281 Constant *Offset = ConstantExpr::getNeg(ContiguousCases->back());
3282 Constant *NumCases = ConstantInt::get(Offset->getType(), ContiguousCases->size());
Benjamin Kramerf4ea1d52011-02-02 15:56:22 +00003283
Benjamin Kramer8d6a8c12011-02-07 22:37:28 +00003284 Value *Sub = SI->getCondition();
3285 if (!Offset->isNullValue())
Hans Wennborg68000082015-01-26 19:52:32 +00003286 Sub = Builder.CreateAdd(Sub, Offset, Sub->getName() + ".off");
3287
Hans Wennborgc9e1d992013-04-16 08:35:36 +00003288 Value *Cmp;
3289 // If NumCases overflowed, then all possible values jump to the successor.
Hans Wennborg68000082015-01-26 19:52:32 +00003290 if (NumCases->isNullValue() && !ContiguousCases->empty())
Hans Wennborgc9e1d992013-04-16 08:35:36 +00003291 Cmp = ConstantInt::getTrue(SI->getContext());
3292 else
3293 Cmp = Builder.CreateICmpULT(Sub, NumCases, "switch");
Hans Wennborg68000082015-01-26 19:52:32 +00003294 BranchInst *NewBI = Builder.CreateCondBr(Cmp, ContiguousDest, OtherDest);
Benjamin Kramerf4ea1d52011-02-02 15:56:22 +00003295
Manman Ren56575552012-09-18 00:47:33 +00003296 // Update weight for the newly-created conditional branch.
Hans Wennborg68000082015-01-26 19:52:32 +00003297 if (HasBranchWeights(SI)) {
3298 SmallVector<uint64_t, 8> Weights;
Manman Ren56575552012-09-18 00:47:33 +00003299 GetBranchWeights(SI, Weights);
3300 if (Weights.size() == 1 + SI->getNumCases()) {
Hans Wennborg68000082015-01-26 19:52:32 +00003301 uint64_t TrueWeight = 0;
3302 uint64_t FalseWeight = 0;
3303 for (size_t I = 0, E = Weights.size(); I != E; ++I) {
3304 if (SI->getSuccessor(I) == ContiguousDest)
3305 TrueWeight += Weights[I];
3306 else
3307 FalseWeight += Weights[I];
3308 }
3309 while (TrueWeight > UINT32_MAX || FalseWeight > UINT32_MAX) {
3310 TrueWeight /= 2;
3311 FalseWeight /= 2;
3312 }
Manman Ren56575552012-09-18 00:47:33 +00003313 NewBI->setMetadata(LLVMContext::MD_prof,
Hans Wennborg68000082015-01-26 19:52:32 +00003314 MDBuilder(SI->getContext()).createBranchWeights(
3315 (uint32_t)TrueWeight, (uint32_t)FalseWeight));
Manman Ren56575552012-09-18 00:47:33 +00003316 }
3317 }
3318
Hans Wennborg68000082015-01-26 19:52:32 +00003319 // Prune obsolete incoming values off the successors' PHI nodes.
3320 for (auto BBI = ContiguousDest->begin(); isa<PHINode>(BBI); ++BBI) {
3321 unsigned PreviousEdges = ContiguousCases->size();
3322 if (ContiguousDest == SI->getDefaultDest()) ++PreviousEdges;
3323 for (unsigned I = 0, E = PreviousEdges - 1; I != E; ++I)
Benjamin Kramerf4ea1d52011-02-02 15:56:22 +00003324 cast<PHINode>(BBI)->removeIncomingValue(SI->getParent());
3325 }
Hans Wennborg68000082015-01-26 19:52:32 +00003326 for (auto BBI = OtherDest->begin(); isa<PHINode>(BBI); ++BBI) {
3327 unsigned PreviousEdges = SI->getNumCases() - ContiguousCases->size();
3328 if (OtherDest == SI->getDefaultDest()) ++PreviousEdges;
3329 for (unsigned I = 0, E = PreviousEdges - 1; I != E; ++I)
3330 cast<PHINode>(BBI)->removeIncomingValue(SI->getParent());
3331 }
3332
3333 // Drop the switch.
Benjamin Kramerf4ea1d52011-02-02 15:56:22 +00003334 SI->eraseFromParent();
3335
3336 return true;
3337}
Chris Lattner25c3af32010-12-13 06:25:44 +00003338
Sanjay Patel09159b8f2015-06-24 20:40:57 +00003339/// Compute masked bits for the condition of a switch
Benjamin Kramerd96205c2011-05-14 15:57:25 +00003340/// and use it to remove dead cases.
Mehdi Aminia28d91d2015-03-10 02:37:25 +00003341static bool EliminateDeadSwitchCases(SwitchInst *SI, AssumptionCache *AC,
3342 const DataLayout &DL) {
Benjamin Kramerd96205c2011-05-14 15:57:25 +00003343 Value *Cond = SI->getCondition();
Matt Arsenault8227b9f2013-09-06 00:37:24 +00003344 unsigned Bits = Cond->getType()->getIntegerBitWidth();
Benjamin Kramerd96205c2011-05-14 15:57:25 +00003345 APInt KnownZero(Bits, 0), KnownOne(Bits, 0);
Chandler Carruth66b31302015-01-04 12:03:27 +00003346 computeKnownBits(Cond, KnownZero, KnownOne, DL, 0, AC, SI);
Benjamin Kramerd96205c2011-05-14 15:57:25 +00003347
3348 // Gather dead cases.
3349 SmallVector<ConstantInt*, 8> DeadCases;
Stepan Dyatkovskiy97b02fc2012-03-11 06:09:17 +00003350 for (SwitchInst::CaseIt I = SI->case_begin(), E = SI->case_end(); I != E; ++I) {
Stepan Dyatkovskiy5b648af2012-03-08 07:06:20 +00003351 if ((I.getCaseValue()->getValue() & KnownZero) != 0 ||
3352 (I.getCaseValue()->getValue() & KnownOne) != KnownOne) {
3353 DeadCases.push_back(I.getCaseValue());
Benjamin Kramerd96205c2011-05-14 15:57:25 +00003354 DEBUG(dbgs() << "SimplifyCFG: switch case '"
Stepan Dyatkovskiy5b648af2012-03-08 07:06:20 +00003355 << I.getCaseValue() << "' is dead.\n");
Benjamin Kramerd96205c2011-05-14 15:57:25 +00003356 }
3357 }
3358
Philip Reames98a2dab2015-08-26 23:56:46 +00003359 // If we can prove that the cases must cover all possible values, the
Philip Reames05370132015-09-10 17:44:47 +00003360 // default destination becomes dead and we can remove it. If we know some
3361 // of the bits in the value, we can use that to more precisely compute the
3362 // number of possible unique case values.
Philip Reames98a2dab2015-08-26 23:56:46 +00003363 bool HasDefault =
3364 !isa<UnreachableInst>(SI->getDefaultDest()->getFirstNonPHIOrDbg());
Philip Reames05370132015-09-10 17:44:47 +00003365 const unsigned NumUnknownBits = Bits -
3366 (KnownZero.Or(KnownOne)).countPopulation();
Filipe Cabecinhas48b090a2015-09-10 22:34:39 +00003367 assert(NumUnknownBits <= Bits);
Philip Reames05370132015-09-10 17:44:47 +00003368 if (HasDefault && DeadCases.empty() &&
3369 NumUnknownBits < 64 /* avoid overflow */ &&
3370 SI->getNumCases() == (1ULL << NumUnknownBits)) {
Philip Reames98a2dab2015-08-26 23:56:46 +00003371 DEBUG(dbgs() << "SimplifyCFG: switch default is dead.\n");
3372 BasicBlock *NewDefault = SplitBlockPredecessors(SI->getDefaultDest(),
3373 SI->getParent(), "");
Duncan P. N. Exon Smith5b4c8372015-10-13 02:39:05 +00003374 SI->setDefaultDest(&*NewDefault);
3375 SplitBlock(&*NewDefault, &NewDefault->front());
Philip Reames98a2dab2015-08-26 23:56:46 +00003376 auto *OldTI = NewDefault->getTerminator();
3377 new UnreachableInst(SI->getContext(), OldTI);
3378 EraseTerminatorInstAndDCECond(OldTI);
3379 return true;
3380 }
3381
Manman Ren56575552012-09-18 00:47:33 +00003382 SmallVector<uint64_t, 8> Weights;
3383 bool HasWeight = HasBranchWeights(SI);
3384 if (HasWeight) {
3385 GetBranchWeights(SI, Weights);
3386 HasWeight = (Weights.size() == 1 + SI->getNumCases());
3387 }
3388
Benjamin Kramerd96205c2011-05-14 15:57:25 +00003389 // Remove dead cases from the switch.
3390 for (unsigned I = 0, E = DeadCases.size(); I != E; ++I) {
Stepan Dyatkovskiy5b648af2012-03-08 07:06:20 +00003391 SwitchInst::CaseIt Case = SI->findCaseValue(DeadCases[I]);
Stepan Dyatkovskiy97b02fc2012-03-11 06:09:17 +00003392 assert(Case != SI->case_default() &&
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003393 "Case was not found. Probably mistake in DeadCases forming.");
Manman Ren56575552012-09-18 00:47:33 +00003394 if (HasWeight) {
3395 std::swap(Weights[Case.getCaseIndex()+1], Weights.back());
3396 Weights.pop_back();
3397 }
3398
Benjamin Kramerd96205c2011-05-14 15:57:25 +00003399 // Prune unused values from PHI nodes.
Stepan Dyatkovskiy5b648af2012-03-08 07:06:20 +00003400 Case.getCaseSuccessor()->removePredecessor(SI->getParent());
Benjamin Kramerd96205c2011-05-14 15:57:25 +00003401 SI->removeCase(Case);
3402 }
Justin Bogner0ba3f212013-12-20 08:21:30 +00003403 if (HasWeight && Weights.size() >= 2) {
Manman Ren56575552012-09-18 00:47:33 +00003404 SmallVector<uint32_t, 8> MDWeights(Weights.begin(), Weights.end());
3405 SI->setMetadata(LLVMContext::MD_prof,
3406 MDBuilder(SI->getParent()->getContext()).
3407 createBranchWeights(MDWeights));
3408 }
Benjamin Kramerd96205c2011-05-14 15:57:25 +00003409
3410 return !DeadCases.empty();
3411}
3412
Sanjay Patel09159b8f2015-06-24 20:40:57 +00003413/// If BB would be eligible for simplification by
3414/// TryToSimplifyUncondBranchFromEmptyBlock (i.e. it is empty and terminated
Hans Wennborg4ab4a8e2011-06-18 10:28:47 +00003415/// by an unconditional branch), look at the phi node for BB in the successor
3416/// block and see if the incoming value is equal to CaseValue. If so, return
3417/// the phi node, and set PhiIndex to BB's index in the phi node.
3418static PHINode *FindPHIForConditionForwarding(ConstantInt *CaseValue,
3419 BasicBlock *BB,
3420 int *PhiIndex) {
3421 if (BB->getFirstNonPHIOrDbg() != BB->getTerminator())
Craig Topperf40110f2014-04-25 05:29:35 +00003422 return nullptr; // BB must be empty to be a candidate for simplification.
Hans Wennborg4ab4a8e2011-06-18 10:28:47 +00003423 if (!BB->getSinglePredecessor())
Craig Topperf40110f2014-04-25 05:29:35 +00003424 return nullptr; // BB must be dominated by the switch.
Hans Wennborg4ab4a8e2011-06-18 10:28:47 +00003425
3426 BranchInst *Branch = dyn_cast<BranchInst>(BB->getTerminator());
3427 if (!Branch || !Branch->isUnconditional())
Craig Topperf40110f2014-04-25 05:29:35 +00003428 return nullptr; // Terminator must be unconditional branch.
Hans Wennborg4ab4a8e2011-06-18 10:28:47 +00003429
3430 BasicBlock *Succ = Branch->getSuccessor(0);
3431
3432 BasicBlock::iterator I = Succ->begin();
3433 while (PHINode *PHI = dyn_cast<PHINode>(I++)) {
3434 int Idx = PHI->getBasicBlockIndex(BB);
3435 assert(Idx >= 0 && "PHI has no entry for predecessor?");
3436
3437 Value *InValue = PHI->getIncomingValue(Idx);
3438 if (InValue != CaseValue) continue;
3439
3440 *PhiIndex = Idx;
3441 return PHI;
3442 }
3443
Craig Topperf40110f2014-04-25 05:29:35 +00003444 return nullptr;
Hans Wennborg4ab4a8e2011-06-18 10:28:47 +00003445}
3446
Sanjay Patel09159b8f2015-06-24 20:40:57 +00003447/// Try to forward the condition of a switch instruction to a phi node
3448/// dominated by the switch, if that would mean that some of the destination
3449/// blocks of the switch can be folded away.
Hans Wennborg4ab4a8e2011-06-18 10:28:47 +00003450/// Returns true if a change is made.
3451static bool ForwardSwitchConditionToPHI(SwitchInst *SI) {
3452 typedef DenseMap<PHINode*, SmallVector<int,4> > ForwardingNodesMap;
3453 ForwardingNodesMap ForwardingNodes;
3454
Stepan Dyatkovskiy97b02fc2012-03-11 06:09:17 +00003455 for (SwitchInst::CaseIt I = SI->case_begin(), E = SI->case_end(); I != E; ++I) {
Stepan Dyatkovskiy5b648af2012-03-08 07:06:20 +00003456 ConstantInt *CaseValue = I.getCaseValue();
3457 BasicBlock *CaseDest = I.getCaseSuccessor();
Hans Wennborg4ab4a8e2011-06-18 10:28:47 +00003458
3459 int PhiIndex;
3460 PHINode *PHI = FindPHIForConditionForwarding(CaseValue, CaseDest,
3461 &PhiIndex);
3462 if (!PHI) continue;
3463
3464 ForwardingNodes[PHI].push_back(PhiIndex);
3465 }
3466
3467 bool Changed = false;
3468
3469 for (ForwardingNodesMap::iterator I = ForwardingNodes.begin(),
3470 E = ForwardingNodes.end(); I != E; ++I) {
3471 PHINode *Phi = I->first;
Craig Topperb94011f2013-07-14 04:42:23 +00003472 SmallVectorImpl<int> &Indexes = I->second;
Hans Wennborg4ab4a8e2011-06-18 10:28:47 +00003473
3474 if (Indexes.size() < 2) continue;
3475
3476 for (size_t I = 0, E = Indexes.size(); I != E; ++I)
3477 Phi->setIncomingValue(Indexes[I], SI->getCondition());
3478 Changed = true;
3479 }
3480
3481 return Changed;
3482}
3483
Sanjay Patel09159b8f2015-06-24 20:40:57 +00003484/// Return true if the backend will be able to handle
Hans Wennborg8a62fc52012-09-06 09:43:28 +00003485/// initializing an array of constants like C.
Hans Wennborg08238ad2012-09-07 08:22:57 +00003486static bool ValidLookupTableConstant(Constant *C) {
Hans Wennborg4dc89512014-06-20 00:38:12 +00003487 if (C->isThreadDependent())
3488 return false;
3489 if (C->isDLLImportDependent())
3490 return false;
Hans Wennborg8a62fc52012-09-06 09:43:28 +00003491
Hans Wennborgb03ebfb2014-06-26 00:30:52 +00003492 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C))
3493 return CE->isGEPWithNoNotionalOverIndexing();
3494
Hans Wennborg8a62fc52012-09-06 09:43:28 +00003495 return isa<ConstantFP>(C) ||
3496 isa<ConstantInt>(C) ||
3497 isa<ConstantPointerNull>(C) ||
3498 isa<GlobalValue>(C) ||
3499 isa<UndefValue>(C);
3500}
3501
Sanjay Patel09159b8f2015-06-24 20:40:57 +00003502/// If V is a Constant, return it. Otherwise, try to look up
Hans Wennborg4fef2fe2012-10-31 15:31:09 +00003503/// its constant value in ConstantPool, returning 0 if it's not there.
Hans Wennborg09acdb92012-10-31 15:14:39 +00003504static Constant *LookupConstant(Value *V,
3505 const SmallDenseMap<Value*, Constant*>& ConstantPool) {
3506 if (Constant *C = dyn_cast<Constant>(V))
3507 return C;
3508 return ConstantPool.lookup(V);
3509}
3510
Sanjay Patel09159b8f2015-06-24 20:40:57 +00003511/// Try to fold instruction I into a constant. This works for
Hans Wennborg9e74dd92012-10-31 13:42:45 +00003512/// simple instructions such as binary operations where both operands are
3513/// constant or can be replaced by constants from the ConstantPool. Returns the
Hans Wennborg4fef2fe2012-10-31 15:31:09 +00003514/// resulting constant on success, 0 otherwise.
Benjamin Kramer7c302602013-11-12 12:24:36 +00003515static Constant *
Mehdi Aminia28d91d2015-03-10 02:37:25 +00003516ConstantFold(Instruction *I, const DataLayout &DL,
3517 const SmallDenseMap<Value *, Constant *> &ConstantPool) {
Hans Wennborg9e74dd92012-10-31 13:42:45 +00003518 if (SelectInst *Select = dyn_cast<SelectInst>(I)) {
Hans Wennborg09acdb92012-10-31 15:14:39 +00003519 Constant *A = LookupConstant(Select->getCondition(), ConstantPool);
3520 if (!A)
Craig Topperf40110f2014-04-25 05:29:35 +00003521 return nullptr;
Hans Wennborg09acdb92012-10-31 15:14:39 +00003522 if (A->isAllOnesValue())
3523 return LookupConstant(Select->getTrueValue(), ConstantPool);
3524 if (A->isNullValue())
3525 return LookupConstant(Select->getFalseValue(), ConstantPool);
Craig Topperf40110f2014-04-25 05:29:35 +00003526 return nullptr;
Hans Wennborg9e74dd92012-10-31 13:42:45 +00003527 }
3528
Benjamin Kramer7c302602013-11-12 12:24:36 +00003529 SmallVector<Constant *, 4> COps;
3530 for (unsigned N = 0, E = I->getNumOperands(); N != E; ++N) {
3531 if (Constant *A = LookupConstant(I->getOperand(N), ConstantPool))
3532 COps.push_back(A);
3533 else
Craig Topperf40110f2014-04-25 05:29:35 +00003534 return nullptr;
Hans Wennborg9e74dd92012-10-31 13:42:45 +00003535 }
3536
Mehdi Aminia28d91d2015-03-10 02:37:25 +00003537 if (CmpInst *Cmp = dyn_cast<CmpInst>(I)) {
Benjamin Kramer7c302602013-11-12 12:24:36 +00003538 return ConstantFoldCompareInstOperands(Cmp->getPredicate(), COps[0],
3539 COps[1], DL);
Mehdi Aminia28d91d2015-03-10 02:37:25 +00003540 }
Benjamin Kramer7c302602013-11-12 12:24:36 +00003541
3542 return ConstantFoldInstOperands(I->getOpcode(), I->getType(), COps, DL);
Hans Wennborg9e74dd92012-10-31 13:42:45 +00003543}
3544
Sanjay Patel09159b8f2015-06-24 20:40:57 +00003545/// Try to determine the resulting constant values in phi nodes
Hans Wennborg9e74dd92012-10-31 13:42:45 +00003546/// at the common destination basic block, *CommonDest, for one of the case
Hans Wennborg4fef2fe2012-10-31 15:31:09 +00003547/// destionations CaseDest corresponding to value CaseVal (0 for the default
Hans Wennborg9e74dd92012-10-31 13:42:45 +00003548/// case), of a switch instruction SI.
Craig Topperb94011f2013-07-14 04:42:23 +00003549static bool
Mehdi Aminia28d91d2015-03-10 02:37:25 +00003550GetCaseResults(SwitchInst *SI, ConstantInt *CaseVal, BasicBlock *CaseDest,
Craig Topperb94011f2013-07-14 04:42:23 +00003551 BasicBlock **CommonDest,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00003552 SmallVectorImpl<std::pair<PHINode *, Constant *>> &Res,
3553 const DataLayout &DL) {
Hans Wennborg8a62fc52012-09-06 09:43:28 +00003554 // The block from which we enter the common destination.
3555 BasicBlock *Pred = SI->getParent();
3556
Hans Wennborg9e74dd92012-10-31 13:42:45 +00003557 // If CaseDest is empty except for some side-effect free instructions through
3558 // which we can constant-propagate the CaseVal, continue to its successor.
3559 SmallDenseMap<Value*, Constant*> ConstantPool;
3560 ConstantPool.insert(std::make_pair(SI->getCondition(), CaseVal));
3561 for (BasicBlock::iterator I = CaseDest->begin(), E = CaseDest->end(); I != E;
3562 ++I) {
3563 if (TerminatorInst *T = dyn_cast<TerminatorInst>(I)) {
3564 // If the terminator is a simple branch, continue to the next block.
3565 if (T->getNumSuccessors() != 1)
3566 return false;
3567 Pred = CaseDest;
3568 CaseDest = T->getSuccessor(0);
3569 } else if (isa<DbgInfoIntrinsic>(I)) {
3570 // Skip debug intrinsic.
3571 continue;
Duncan P. N. Exon Smith5b4c8372015-10-13 02:39:05 +00003572 } else if (Constant *C = ConstantFold(&*I, DL, ConstantPool)) {
Hans Wennborg9e74dd92012-10-31 13:42:45 +00003573 // Instruction is side-effect free and constant.
Hans Wennborgdcc6e5b2015-01-09 22:13:31 +00003574
3575 // If the instruction has uses outside this block or a phi node slot for
3576 // the block, it is not safe to bypass the instruction since it would then
3577 // no longer dominate all its uses.
3578 for (auto &Use : I->uses()) {
3579 User *User = Use.getUser();
3580 if (Instruction *I = dyn_cast<Instruction>(User))
3581 if (I->getParent() == CaseDest)
3582 continue;
3583 if (PHINode *Phi = dyn_cast<PHINode>(User))
3584 if (Phi->getIncomingBlock(Use) == CaseDest)
3585 continue;
3586 return false;
3587 }
3588
Duncan P. N. Exon Smith5b4c8372015-10-13 02:39:05 +00003589 ConstantPool.insert(std::make_pair(&*I, C));
Hans Wennborg9e74dd92012-10-31 13:42:45 +00003590 } else {
3591 break;
3592 }
Hans Wennborg8a62fc52012-09-06 09:43:28 +00003593 }
3594
3595 // If we did not have a CommonDest before, use the current one.
3596 if (!*CommonDest)
3597 *CommonDest = CaseDest;
3598 // If the destination isn't the common one, abort.
3599 if (CaseDest != *CommonDest)
3600 return false;
3601
3602 // Get the values for this case from phi nodes in the destination block.
3603 BasicBlock::iterator I = (*CommonDest)->begin();
3604 while (PHINode *PHI = dyn_cast<PHINode>(I++)) {
3605 int Idx = PHI->getBasicBlockIndex(Pred);
3606 if (Idx == -1)
3607 continue;
3608
Hans Wennborg09acdb92012-10-31 15:14:39 +00003609 Constant *ConstVal = LookupConstant(PHI->getIncomingValue(Idx),
3610 ConstantPool);
Hans Wennborg9e74dd92012-10-31 13:42:45 +00003611 if (!ConstVal)
Hans Wennborg8a62fc52012-09-06 09:43:28 +00003612 return false;
3613
3614 // Be conservative about which kinds of constants we support.
3615 if (!ValidLookupTableConstant(ConstVal))
3616 return false;
3617
3618 Res.push_back(std::make_pair(PHI, ConstVal));
3619 }
3620
Hans Wennborgac114a32014-01-12 00:44:41 +00003621 return Res.size() > 0;
Hans Wennborg8a62fc52012-09-06 09:43:28 +00003622}
3623
Sanjay Patel09159b8f2015-06-24 20:40:57 +00003624// Helper function used to add CaseVal to the list of cases that generate
3625// Result.
Marcello Maggioni5bbe3df2014-10-14 01:58:26 +00003626static void MapCaseToResult(ConstantInt *CaseVal,
3627 SwitchCaseResultVectorTy &UniqueResults,
3628 Constant *Result) {
3629 for (auto &I : UniqueResults) {
3630 if (I.first == Result) {
3631 I.second.push_back(CaseVal);
3632 return;
3633 }
3634 }
3635 UniqueResults.push_back(std::make_pair(Result,
3636 SmallVector<ConstantInt*, 4>(1, CaseVal)));
3637}
3638
Sanjay Patel09159b8f2015-06-24 20:40:57 +00003639// Helper function that initializes a map containing
Marcello Maggioni5bbe3df2014-10-14 01:58:26 +00003640// results for the PHI node of the common destination block for a switch
3641// instruction. Returns false if multiple PHI nodes have been found or if
3642// there is not a common destination block for the switch.
Mehdi Aminia28d91d2015-03-10 02:37:25 +00003643static bool InitializeUniqueCases(SwitchInst *SI, PHINode *&PHI,
3644 BasicBlock *&CommonDest,
3645 SwitchCaseResultVectorTy &UniqueResults,
3646 Constant *&DefaultResult,
3647 const DataLayout &DL) {
Marcello Maggioni5bbe3df2014-10-14 01:58:26 +00003648 for (auto &I : SI->cases()) {
3649 ConstantInt *CaseVal = I.getCaseValue();
3650
3651 // Resulting value at phi nodes for this case value.
3652 SwitchCaseResultsTy Results;
3653 if (!GetCaseResults(SI, CaseVal, I.getCaseSuccessor(), &CommonDest, Results,
3654 DL))
3655 return false;
3656
3657 // Only one value per case is permitted
3658 if (Results.size() > 1)
3659 return false;
3660 MapCaseToResult(CaseVal, UniqueResults, Results.begin()->second);
3661
3662 // Check the PHI consistency.
3663 if (!PHI)
3664 PHI = Results[0].first;
3665 else if (PHI != Results[0].first)
3666 return false;
3667 }
3668 // Find the default result value.
3669 SmallVector<std::pair<PHINode *, Constant *>, 1> DefaultResults;
3670 BasicBlock *DefaultDest = SI->getDefaultDest();
3671 GetCaseResults(SI, nullptr, SI->getDefaultDest(), &CommonDest, DefaultResults,
3672 DL);
3673 // If the default value is not found abort unless the default destination
3674 // is unreachable.
3675 DefaultResult =
3676 DefaultResults.size() == 1 ? DefaultResults.begin()->second : nullptr;
3677 if ((!DefaultResult &&
3678 !isa<UnreachableInst>(DefaultDest->getFirstNonPHIOrDbg())))
3679 return false;
3680
3681 return true;
3682}
3683
Sanjay Patel09159b8f2015-06-24 20:40:57 +00003684// Helper function that checks if it is possible to transform a switch with only
3685// two cases (or two cases + default) that produces a result into a select.
Marcello Maggioni5bbe3df2014-10-14 01:58:26 +00003686// Example:
3687// switch (a) {
3688// case 10: %0 = icmp eq i32 %a, 10
3689// return 10; %1 = select i1 %0, i32 10, i32 4
3690// case 20: ----> %2 = icmp eq i32 %a, 20
3691// return 2; %3 = select i1 %2, i32 2, i32 %1
3692// default:
3693// return 4;
3694// }
3695static Value *
3696ConvertTwoCaseSwitch(const SwitchCaseResultVectorTy &ResultVector,
3697 Constant *DefaultResult, Value *Condition,
3698 IRBuilder<> &Builder) {
3699 assert(ResultVector.size() == 2 &&
3700 "We should have exactly two unique results at this point");
3701 // If we are selecting between only two cases transform into a simple
3702 // select or a two-way select if default is possible.
3703 if (ResultVector[0].second.size() == 1 &&
3704 ResultVector[1].second.size() == 1) {
3705 ConstantInt *const FirstCase = ResultVector[0].second[0];
3706 ConstantInt *const SecondCase = ResultVector[1].second[0];
3707
3708 bool DefaultCanTrigger = DefaultResult;
3709 Value *SelectValue = ResultVector[1].first;
3710 if (DefaultCanTrigger) {
3711 Value *const ValueCompare =
3712 Builder.CreateICmpEQ(Condition, SecondCase, "switch.selectcmp");
3713 SelectValue = Builder.CreateSelect(ValueCompare, ResultVector[1].first,
3714 DefaultResult, "switch.select");
3715 }
3716 Value *const ValueCompare =
3717 Builder.CreateICmpEQ(Condition, FirstCase, "switch.selectcmp");
3718 return Builder.CreateSelect(ValueCompare, ResultVector[0].first, SelectValue,
3719 "switch.select");
3720 }
3721
3722 return nullptr;
3723}
3724
Sanjay Patel09159b8f2015-06-24 20:40:57 +00003725// Helper function to cleanup a switch instruction that has been converted into
3726// a select, fixing up PHI nodes and basic blocks.
Marcello Maggioni5bbe3df2014-10-14 01:58:26 +00003727static void RemoveSwitchAfterSelectConversion(SwitchInst *SI, PHINode *PHI,
3728 Value *SelectValue,
3729 IRBuilder<> &Builder) {
3730 BasicBlock *SelectBB = SI->getParent();
3731 while (PHI->getBasicBlockIndex(SelectBB) >= 0)
3732 PHI->removeIncomingValue(SelectBB);
3733 PHI->addIncoming(SelectValue, SelectBB);
3734
3735 Builder.CreateBr(PHI->getParent());
3736
3737 // Remove the switch.
3738 for (unsigned i = 0, e = SI->getNumSuccessors(); i < e; ++i) {
3739 BasicBlock *Succ = SI->getSuccessor(i);
3740
3741 if (Succ == PHI->getParent())
3742 continue;
3743 Succ->removePredecessor(SelectBB);
3744 }
3745 SI->eraseFromParent();
3746}
3747
Sanjay Patel09159b8f2015-06-24 20:40:57 +00003748/// If the switch is only used to initialize one or more
Marcello Maggioni5bbe3df2014-10-14 01:58:26 +00003749/// phi nodes in a common successor block with only two different
3750/// constant values, replace the switch with select.
3751static bool SwitchToSelect(SwitchInst *SI, IRBuilder<> &Builder,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00003752 AssumptionCache *AC, const DataLayout &DL) {
Marcello Maggioni5bbe3df2014-10-14 01:58:26 +00003753 Value *const Cond = SI->getCondition();
3754 PHINode *PHI = nullptr;
3755 BasicBlock *CommonDest = nullptr;
3756 Constant *DefaultResult;
3757 SwitchCaseResultVectorTy UniqueResults;
3758 // Collect all the cases that will deliver the same value from the switch.
Mehdi Aminia28d91d2015-03-10 02:37:25 +00003759 if (!InitializeUniqueCases(SI, PHI, CommonDest, UniqueResults, DefaultResult,
3760 DL))
Marcello Maggioni5bbe3df2014-10-14 01:58:26 +00003761 return false;
3762 // Selects choose between maximum two values.
3763 if (UniqueResults.size() != 2)
3764 return false;
3765 assert(PHI != nullptr && "PHI for value select not found");
3766
3767 Builder.SetInsertPoint(SI);
3768 Value *SelectValue = ConvertTwoCaseSwitch(
3769 UniqueResults,
3770 DefaultResult, Cond, Builder);
3771 if (SelectValue) {
3772 RemoveSwitchAfterSelectConversion(SI, PHI, SelectValue, Builder);
3773 return true;
3774 }
3775 // The switch couldn't be converted into a select.
3776 return false;
3777}
3778
Hans Wennborg776d7122012-09-26 09:34:53 +00003779namespace {
Sanjay Patel09159b8f2015-06-24 20:40:57 +00003780 /// This class represents a lookup table that can be used to replace a switch.
Hans Wennborg776d7122012-09-26 09:34:53 +00003781 class SwitchLookupTable {
3782 public:
Sanjay Patel09159b8f2015-06-24 20:40:57 +00003783 /// Create a lookup table to use as a switch replacement with the contents
3784 /// of Values, using DefaultValue to fill any holes in the table.
Mehdi Aminia28d91d2015-03-10 02:37:25 +00003785 SwitchLookupTable(
3786 Module &M, uint64_t TableSize, ConstantInt *Offset,
3787 const SmallVectorImpl<std::pair<ConstantInt *, Constant *>> &Values,
3788 Constant *DefaultValue, const DataLayout &DL);
Hans Wennborg776d7122012-09-26 09:34:53 +00003789
Sanjay Patel09159b8f2015-06-24 20:40:57 +00003790 /// Build instructions with Builder to retrieve the value at
Hans Wennborg776d7122012-09-26 09:34:53 +00003791 /// the position given by Index in the lookup table.
Manman Ren4d189fb2014-07-24 21:13:20 +00003792 Value *BuildLookup(Value *Index, IRBuilder<> &Builder);
Hans Wennborg776d7122012-09-26 09:34:53 +00003793
Sanjay Patel09159b8f2015-06-24 20:40:57 +00003794 /// Return true if a table with TableSize elements of
Hans Wennborg39583b82012-09-26 09:44:49 +00003795 /// type ElementType would fit in a target-legal register.
Mehdi Aminia28d91d2015-03-10 02:37:25 +00003796 static bool WouldFitInRegister(const DataLayout &DL, uint64_t TableSize,
Craig Toppere3dcce92015-08-01 22:20:21 +00003797 Type *ElementType);
Hans Wennborg39583b82012-09-26 09:44:49 +00003798
Hans Wennborg776d7122012-09-26 09:34:53 +00003799 private:
3800 // Depending on the contents of the table, it can be represented in
3801 // different ways.
3802 enum {
3803 // For tables where each element contains the same value, we just have to
3804 // store that single value and return it for each lookup.
3805 SingleValueKind,
3806
Erik Eckstein105374f2014-11-17 09:13:57 +00003807 // For tables where there is a linear relationship between table index
3808 // and values. We calculate the result with a simple multiplication
3809 // and addition instead of a table lookup.
3810 LinearMapKind,
3811
Hans Wennborg39583b82012-09-26 09:44:49 +00003812 // For small tables with integer elements, we can pack them into a bitmap
3813 // that fits into a target-legal register. Values are retrieved by
3814 // shift and mask operations.
3815 BitMapKind,
3816
Hans Wennborg776d7122012-09-26 09:34:53 +00003817 // The table is stored as an array of values. Values are retrieved by load
3818 // instructions from the table.
3819 ArrayKind
3820 } Kind;
3821
3822 // For SingleValueKind, this is the single value.
3823 Constant *SingleValue;
3824
Hans Wennborg39583b82012-09-26 09:44:49 +00003825 // For BitMapKind, this is the bitmap.
3826 ConstantInt *BitMap;
3827 IntegerType *BitMapElementTy;
3828
Erik Eckstein105374f2014-11-17 09:13:57 +00003829 // For LinearMapKind, these are the constants used to derive the value.
3830 ConstantInt *LinearOffset;
3831 ConstantInt *LinearMultiplier;
3832
Hans Wennborg776d7122012-09-26 09:34:53 +00003833 // For ArrayKind, this is the array.
3834 GlobalVariable *Array;
3835 };
Alexander Kornienkof00654e2015-06-23 09:49:53 +00003836}
Hans Wennborg776d7122012-09-26 09:34:53 +00003837
Mehdi Aminia28d91d2015-03-10 02:37:25 +00003838SwitchLookupTable::SwitchLookupTable(
3839 Module &M, uint64_t TableSize, ConstantInt *Offset,
3840 const SmallVectorImpl<std::pair<ConstantInt *, Constant *>> &Values,
3841 Constant *DefaultValue, const DataLayout &DL)
Craig Topperf40110f2014-04-25 05:29:35 +00003842 : SingleValue(nullptr), BitMap(nullptr), BitMapElementTy(nullptr),
Erik Eckstein105374f2014-11-17 09:13:57 +00003843 LinearOffset(nullptr), LinearMultiplier(nullptr), Array(nullptr) {
Hans Wennborgf2e2c102012-09-26 11:07:37 +00003844 assert(Values.size() && "Can't build lookup table without values!");
3845 assert(TableSize >= Values.size() && "Can't fit values in table!");
Hans Wennborg8a62fc52012-09-06 09:43:28 +00003846
3847 // If all values in the table are equal, this is that value.
Hans Wennborg776d7122012-09-26 09:34:53 +00003848 SingleValue = Values.begin()->second;
Hans Wennborg8a62fc52012-09-06 09:43:28 +00003849
Hans Wennborgac114a32014-01-12 00:44:41 +00003850 Type *ValueType = Values.begin()->second->getType();
3851
Hans Wennborg8a62fc52012-09-06 09:43:28 +00003852 // Build up the table contents.
Hans Wennborg776d7122012-09-26 09:34:53 +00003853 SmallVector<Constant*, 64> TableContents(TableSize);
3854 for (size_t I = 0, E = Values.size(); I != E; ++I) {
3855 ConstantInt *CaseVal = Values[I].first;
3856 Constant *CaseRes = Values[I].second;
Hans Wennborgac114a32014-01-12 00:44:41 +00003857 assert(CaseRes->getType() == ValueType);
Hans Wennborg8a62fc52012-09-06 09:43:28 +00003858
Hans Wennborg776d7122012-09-26 09:34:53 +00003859 uint64_t Idx = (CaseVal->getValue() - Offset->getValue())
3860 .getLimitedValue();
Hans Wennborg8a62fc52012-09-06 09:43:28 +00003861 TableContents[Idx] = CaseRes;
3862
Hans Wennborg776d7122012-09-26 09:34:53 +00003863 if (CaseRes != SingleValue)
Craig Topperf40110f2014-04-25 05:29:35 +00003864 SingleValue = nullptr;
Hans Wennborg8a62fc52012-09-06 09:43:28 +00003865 }
3866
3867 // Fill in any holes in the table with the default result.
Hans Wennborg776d7122012-09-26 09:34:53 +00003868 if (Values.size() < TableSize) {
Marcello Maggioni89c05ad2014-07-03 08:29:06 +00003869 assert(DefaultValue &&
3870 "Need a default value to fill the lookup table holes.");
Hans Wennborgac114a32014-01-12 00:44:41 +00003871 assert(DefaultValue->getType() == ValueType);
Hans Wennborg776d7122012-09-26 09:34:53 +00003872 for (uint64_t I = 0; I < TableSize; ++I) {
3873 if (!TableContents[I])
3874 TableContents[I] = DefaultValue;
Hans Wennborg8a62fc52012-09-06 09:43:28 +00003875 }
3876
Hans Wennborg776d7122012-09-26 09:34:53 +00003877 if (DefaultValue != SingleValue)
Craig Topperf40110f2014-04-25 05:29:35 +00003878 SingleValue = nullptr;
Hans Wennborg8a62fc52012-09-06 09:43:28 +00003879 }
3880
Hans Wennborg776d7122012-09-26 09:34:53 +00003881 // If each element in the table contains the same value, we only need to store
3882 // that single value.
3883 if (SingleValue) {
3884 Kind = SingleValueKind;
3885 return;
Hans Wennborg8a62fc52012-09-06 09:43:28 +00003886 }
3887
Erik Eckstein105374f2014-11-17 09:13:57 +00003888 // Check if we can derive the value with a linear transformation from the
3889 // table index.
3890 if (isa<IntegerType>(ValueType)) {
3891 bool LinearMappingPossible = true;
3892 APInt PrevVal;
3893 APInt DistToPrev;
3894 assert(TableSize >= 2 && "Should be a SingleValue table.");
3895 // Check if there is the same distance between two consecutive values.
3896 for (uint64_t I = 0; I < TableSize; ++I) {
3897 ConstantInt *ConstVal = dyn_cast<ConstantInt>(TableContents[I]);
3898 if (!ConstVal) {
3899 // This is an undef. We could deal with it, but undefs in lookup tables
3900 // are very seldom. It's probably not worth the additional complexity.
3901 LinearMappingPossible = false;
3902 break;
3903 }
3904 APInt Val = ConstVal->getValue();
3905 if (I != 0) {
3906 APInt Dist = Val - PrevVal;
3907 if (I == 1) {
3908 DistToPrev = Dist;
3909 } else if (Dist != DistToPrev) {
3910 LinearMappingPossible = false;
3911 break;
3912 }
3913 }
3914 PrevVal = Val;
3915 }
3916 if (LinearMappingPossible) {
3917 LinearOffset = cast<ConstantInt>(TableContents[0]);
3918 LinearMultiplier = ConstantInt::get(M.getContext(), DistToPrev);
3919 Kind = LinearMapKind;
3920 ++NumLinearMaps;
3921 return;
3922 }
3923 }
3924
Hans Wennborg39583b82012-09-26 09:44:49 +00003925 // If the type is integer and the table fits in a register, build a bitmap.
Rafael Espindola37dc9e12014-02-21 00:06:31 +00003926 if (WouldFitInRegister(DL, TableSize, ValueType)) {
Hans Wennborgac114a32014-01-12 00:44:41 +00003927 IntegerType *IT = cast<IntegerType>(ValueType);
Hans Wennborg39583b82012-09-26 09:44:49 +00003928 APInt TableInt(TableSize * IT->getBitWidth(), 0);
3929 for (uint64_t I = TableSize; I > 0; --I) {
3930 TableInt <<= IT->getBitWidth();
Benjamin Kramer9fc3dc72012-10-01 11:31:48 +00003931 // Insert values into the bitmap. Undef values are set to zero.
3932 if (!isa<UndefValue>(TableContents[I - 1])) {
3933 ConstantInt *Val = cast<ConstantInt>(TableContents[I - 1]);
3934 TableInt |= Val->getValue().zext(TableInt.getBitWidth());
3935 }
Hans Wennborg39583b82012-09-26 09:44:49 +00003936 }
3937 BitMap = ConstantInt::get(M.getContext(), TableInt);
3938 BitMapElementTy = IT;
3939 Kind = BitMapKind;
3940 ++NumBitMaps;
3941 return;
3942 }
3943
Hans Wennborg776d7122012-09-26 09:34:53 +00003944 // Store the table in an array.
Hans Wennborgac114a32014-01-12 00:44:41 +00003945 ArrayType *ArrayTy = ArrayType::get(ValueType, TableSize);
Hans Wennborg8a62fc52012-09-06 09:43:28 +00003946 Constant *Initializer = ConstantArray::get(ArrayTy, TableContents);
3947
Hans Wennborg776d7122012-09-26 09:34:53 +00003948 Array = new GlobalVariable(M, ArrayTy, /*constant=*/ true,
3949 GlobalVariable::PrivateLinkage,
3950 Initializer,
3951 "switch.table");
3952 Array->setUnnamedAddr(true);
3953 Kind = ArrayKind;
3954}
3955
Manman Ren4d189fb2014-07-24 21:13:20 +00003956Value *SwitchLookupTable::BuildLookup(Value *Index, IRBuilder<> &Builder) {
Hans Wennborg776d7122012-09-26 09:34:53 +00003957 switch (Kind) {
3958 case SingleValueKind:
3959 return SingleValue;
Erik Eckstein105374f2014-11-17 09:13:57 +00003960 case LinearMapKind: {
3961 // Derive the result value from the input value.
3962 Value *Result = Builder.CreateIntCast(Index, LinearMultiplier->getType(),
3963 false, "switch.idx.cast");
3964 if (!LinearMultiplier->isOne())
3965 Result = Builder.CreateMul(Result, LinearMultiplier, "switch.idx.mult");
3966 if (!LinearOffset->isZero())
3967 Result = Builder.CreateAdd(Result, LinearOffset, "switch.offset");
3968 return Result;
3969 }
Hans Wennborg39583b82012-09-26 09:44:49 +00003970 case BitMapKind: {
3971 // Type of the bitmap (e.g. i59).
3972 IntegerType *MapTy = BitMap->getType();
3973
3974 // Cast Index to the same type as the bitmap.
3975 // Note: The Index is <= the number of elements in the table, so
3976 // truncating it to the width of the bitmask is safe.
Hans Wennborgcd3a11f2012-09-26 14:01:53 +00003977 Value *ShiftAmt = Builder.CreateZExtOrTrunc(Index, MapTy, "switch.cast");
Hans Wennborg39583b82012-09-26 09:44:49 +00003978
3979 // Multiply the shift amount by the element width.
3980 ShiftAmt = Builder.CreateMul(ShiftAmt,
3981 ConstantInt::get(MapTy, BitMapElementTy->getBitWidth()),
3982 "switch.shiftamt");
3983
3984 // Shift down.
3985 Value *DownShifted = Builder.CreateLShr(BitMap, ShiftAmt,
3986 "switch.downshift");
3987 // Mask off.
3988 return Builder.CreateTrunc(DownShifted, BitMapElementTy,
3989 "switch.masked");
3990 }
Hans Wennborg776d7122012-09-26 09:34:53 +00003991 case ArrayKind: {
Manman Renedc60372014-07-23 23:13:23 +00003992 // Make sure the table index will not overflow when treated as signed.
Manman Ren4d189fb2014-07-24 21:13:20 +00003993 IntegerType *IT = cast<IntegerType>(Index->getType());
3994 uint64_t TableSize = Array->getInitializer()->getType()
3995 ->getArrayNumElements();
3996 if (TableSize > (1ULL << (IT->getBitWidth() - 1)))
3997 Index = Builder.CreateZExt(Index,
3998 IntegerType::get(IT->getContext(),
3999 IT->getBitWidth() + 1),
4000 "switch.tableidx.zext");
Manman Renedc60372014-07-23 23:13:23 +00004001
Hans Wennborg776d7122012-09-26 09:34:53 +00004002 Value *GEPIndices[] = { Builder.getInt32(0), Index };
David Blaikieaa41cd52015-04-03 21:33:42 +00004003 Value *GEP = Builder.CreateInBoundsGEP(Array->getValueType(), Array,
4004 GEPIndices, "switch.gep");
Hans Wennborg776d7122012-09-26 09:34:53 +00004005 return Builder.CreateLoad(GEP, "switch.load");
4006 }
4007 }
4008 llvm_unreachable("Unknown lookup table kind!");
4009}
4010
Mehdi Aminia28d91d2015-03-10 02:37:25 +00004011bool SwitchLookupTable::WouldFitInRegister(const DataLayout &DL,
Hans Wennborg39583b82012-09-26 09:44:49 +00004012 uint64_t TableSize,
Craig Toppere3dcce92015-08-01 22:20:21 +00004013 Type *ElementType) {
4014 auto *IT = dyn_cast<IntegerType>(ElementType);
Hans Wennborg39583b82012-09-26 09:44:49 +00004015 if (!IT)
4016 return false;
4017 // FIXME: If the type is wider than it needs to be, e.g. i8 but all values
4018 // are <= 15, we could try to narrow the type.
Benjamin Kramerc2081d12012-09-27 18:29:58 +00004019
4020 // Avoid overflow, fitsInLegalInteger uses unsigned int for the width.
4021 if (TableSize >= UINT_MAX/IT->getBitWidth())
4022 return false;
Mehdi Aminia28d91d2015-03-10 02:37:25 +00004023 return DL.fitsInLegalInteger(TableSize * IT->getBitWidth());
Hans Wennborg39583b82012-09-26 09:44:49 +00004024}
4025
Sanjay Patel09159b8f2015-06-24 20:40:57 +00004026/// Determine whether a lookup table should be built for this switch, based on
4027/// the number of cases, size of the table, and the types of the results.
Mehdi Aminia28d91d2015-03-10 02:37:25 +00004028static bool
4029ShouldBuildLookupTable(SwitchInst *SI, uint64_t TableSize,
4030 const TargetTransformInfo &TTI, const DataLayout &DL,
4031 const SmallDenseMap<PHINode *, Type *> &ResultTypes) {
Hans Wennborgf2e2c102012-09-26 11:07:37 +00004032 if (SI->getNumCases() > TableSize || TableSize >= UINT64_MAX / 10)
4033 return false; // TableSize overflowed, or mul below might overflow.
Hans Wennborg776d7122012-09-26 09:34:53 +00004034
Chandler Carruth77d433d2012-11-30 09:26:25 +00004035 bool AllTablesFitInRegister = true;
Evan Cheng65df8082012-11-30 02:02:42 +00004036 bool HasIllegalType = false;
Hans Wennborga6a11a92014-11-18 02:37:11 +00004037 for (const auto &I : ResultTypes) {
4038 Type *Ty = I.second;
Chandler Carruthd9ef81e2012-11-30 09:34:29 +00004039
4040 // Saturate this flag to true.
Chandler Carruth0b4ef9c2013-01-07 03:53:25 +00004041 HasIllegalType = HasIllegalType || !TTI.isTypeLegal(Ty);
Chandler Carruthd9ef81e2012-11-30 09:34:29 +00004042
4043 // Saturate this flag to false.
4044 AllTablesFitInRegister = AllTablesFitInRegister &&
Rafael Espindola37dc9e12014-02-21 00:06:31 +00004045 SwitchLookupTable::WouldFitInRegister(DL, TableSize, Ty);
Chandler Carruthd9ef81e2012-11-30 09:34:29 +00004046
4047 // If both flags saturate, we're done. NOTE: This *only* works with
4048 // saturating flags, and all flags have to saturate first due to the
4049 // non-deterministic behavior of iterating over a dense map.
4050 if (HasIllegalType && !AllTablesFitInRegister)
Evan Cheng65df8082012-11-30 02:02:42 +00004051 break;
Hans Wennborg39583b82012-09-26 09:44:49 +00004052 }
Evan Cheng65df8082012-11-30 02:02:42 +00004053
Chandler Carruth77d433d2012-11-30 09:26:25 +00004054 // If each table would fit in a register, we should build it anyway.
4055 if (AllTablesFitInRegister)
4056 return true;
4057
4058 // Don't build a table that doesn't fit in-register if it has illegal types.
4059 if (HasIllegalType)
4060 return false;
4061
4062 // The table density should be at least 40%. This is the same criterion as for
4063 // jump tables, see SelectionDAGBuilder::handleJTSwitchCase.
4064 // FIXME: Find the best cut-off.
4065 return SI->getNumCases() * 10 >= TableSize * 4;
Hans Wennborg8a62fc52012-09-06 09:43:28 +00004066}
4067
Erik Eckstein0d86c762014-11-27 15:13:14 +00004068/// Try to reuse the switch table index compare. Following pattern:
4069/// \code
4070/// if (idx < tablesize)
4071/// r = table[idx]; // table does not contain default_value
4072/// else
4073/// r = default_value;
4074/// if (r != default_value)
4075/// ...
4076/// \endcode
4077/// Is optimized to:
4078/// \code
4079/// cond = idx < tablesize;
4080/// if (cond)
4081/// r = table[idx];
4082/// else
4083/// r = default_value;
4084/// if (cond)
4085/// ...
4086/// \endcode
4087/// Jump threading will then eliminate the second if(cond).
4088static void reuseTableCompare(User *PhiUser, BasicBlock *PhiBlock,
4089 BranchInst *RangeCheckBranch, Constant *DefaultValue,
4090 const SmallVectorImpl<std::pair<ConstantInt*, Constant*> >& Values) {
4091
4092 ICmpInst *CmpInst = dyn_cast<ICmpInst>(PhiUser);
4093 if (!CmpInst)
4094 return;
4095
4096 // We require that the compare is in the same block as the phi so that jump
4097 // threading can do its work afterwards.
4098 if (CmpInst->getParent() != PhiBlock)
4099 return;
4100
4101 Constant *CmpOp1 = dyn_cast<Constant>(CmpInst->getOperand(1));
4102 if (!CmpOp1)
4103 return;
4104
4105 Value *RangeCmp = RangeCheckBranch->getCondition();
4106 Constant *TrueConst = ConstantInt::getTrue(RangeCmp->getType());
4107 Constant *FalseConst = ConstantInt::getFalse(RangeCmp->getType());
4108
4109 // Check if the compare with the default value is constant true or false.
4110 Constant *DefaultConst = ConstantExpr::getICmp(CmpInst->getPredicate(),
4111 DefaultValue, CmpOp1, true);
4112 if (DefaultConst != TrueConst && DefaultConst != FalseConst)
4113 return;
4114
4115 // Check if the compare with the case values is distinct from the default
4116 // compare result.
4117 for (auto ValuePair : Values) {
4118 Constant *CaseConst = ConstantExpr::getICmp(CmpInst->getPredicate(),
4119 ValuePair.second, CmpOp1, true);
4120 if (!CaseConst || CaseConst == DefaultConst)
4121 return;
4122 assert((CaseConst == TrueConst || CaseConst == FalseConst) &&
4123 "Expect true or false as compare result.");
4124 }
4125
4126 // Check if the branch instruction dominates the phi node. It's a simple
4127 // dominance check, but sufficient for our needs.
4128 // Although this check is invariant in the calling loops, it's better to do it
4129 // at this late stage. Practically we do it at most once for a switch.
4130 BasicBlock *BranchBlock = RangeCheckBranch->getParent();
4131 for (auto PI = pred_begin(PhiBlock), E = pred_end(PhiBlock); PI != E; ++PI) {
4132 BasicBlock *Pred = *PI;
4133 if (Pred != BranchBlock && Pred->getUniquePredecessor() != BranchBlock)
4134 return;
4135 }
4136
4137 if (DefaultConst == FalseConst) {
4138 // The compare yields the same result. We can replace it.
4139 CmpInst->replaceAllUsesWith(RangeCmp);
4140 ++NumTableCmpReuses;
4141 } else {
4142 // The compare yields the same result, just inverted. We can replace it.
4143 Value *InvertedTableCmp = BinaryOperator::CreateXor(RangeCmp,
4144 ConstantInt::get(RangeCmp->getType(), 1), "inverted.cmp",
4145 RangeCheckBranch);
4146 CmpInst->replaceAllUsesWith(InvertedTableCmp);
4147 ++NumTableCmpReuses;
4148 }
4149}
4150
Sanjay Patel09159b8f2015-06-24 20:40:57 +00004151/// If the switch is only used to initialize one or more phi nodes in a common
4152/// successor block with different constant values, replace the switch with
4153/// lookup tables.
Mehdi Aminia28d91d2015-03-10 02:37:25 +00004154static bool SwitchToLookupTable(SwitchInst *SI, IRBuilder<> &Builder,
4155 const DataLayout &DL,
4156 const TargetTransformInfo &TTI) {
Hans Wennborg8a62fc52012-09-06 09:43:28 +00004157 assert(SI->getNumCases() > 1 && "Degenerate switch?");
Hans Wennborgf3254832012-10-30 11:23:25 +00004158
Hans Wennborgc3c8d952012-11-07 21:35:12 +00004159 // Only build lookup table when we have a target that supports it.
Chandler Carruth0b4ef9c2013-01-07 03:53:25 +00004160 if (!TTI.shouldBuildLookupTables())
Hans Wennborgf3254832012-10-30 11:23:25 +00004161 return false;
4162
Hans Wennborg8a62fc52012-09-06 09:43:28 +00004163 // FIXME: If the switch is too sparse for a lookup table, perhaps we could
4164 // split off a dense part and build a lookup table for that.
4165
Hans Wennborg8a62fc52012-09-06 09:43:28 +00004166 // FIXME: This creates arrays of GEPs to constant strings, which means each
4167 // GEP needs a runtime relocation in PIC code. We should just build one big
4168 // string and lookup indices into that.
4169
Hans Wennborg4744ac12014-01-15 05:00:27 +00004170 // Ignore switches with less than three cases. Lookup tables will not make them
4171 // faster, so we don't analyze them.
4172 if (SI->getNumCases() < 3)
Hans Wennborg8a62fc52012-09-06 09:43:28 +00004173 return false;
4174
4175 // Figure out the corresponding result for each case value and phi node in the
Eric Christopher572e03a2015-06-19 01:53:21 +00004176 // common destination, as well as the min and max case values.
Hans Wennborg8a62fc52012-09-06 09:43:28 +00004177 assert(SI->case_begin() != SI->case_end());
4178 SwitchInst::CaseIt CI = SI->case_begin();
4179 ConstantInt *MinCaseVal = CI.getCaseValue();
4180 ConstantInt *MaxCaseVal = CI.getCaseValue();
4181
Craig Topperf40110f2014-04-25 05:29:35 +00004182 BasicBlock *CommonDest = nullptr;
Hans Wennborg7fd5c8442012-09-10 07:44:22 +00004183 typedef SmallVector<std::pair<ConstantInt*, Constant*>, 4> ResultListTy;
Hans Wennborg8a62fc52012-09-06 09:43:28 +00004184 SmallDenseMap<PHINode*, ResultListTy> ResultLists;
4185 SmallDenseMap<PHINode*, Constant*> DefaultResults;
4186 SmallDenseMap<PHINode*, Type*> ResultTypes;
4187 SmallVector<PHINode*, 4> PHIs;
4188
4189 for (SwitchInst::CaseIt E = SI->case_end(); CI != E; ++CI) {
4190 ConstantInt *CaseVal = CI.getCaseValue();
4191 if (CaseVal->getValue().slt(MinCaseVal->getValue()))
4192 MinCaseVal = CaseVal;
4193 if (CaseVal->getValue().sgt(MaxCaseVal->getValue()))
4194 MaxCaseVal = CaseVal;
4195
4196 // Resulting value at phi nodes for this case value.
4197 typedef SmallVector<std::pair<PHINode*, Constant*>, 4> ResultsTy;
4198 ResultsTy Results;
Hans Wennborg9e74dd92012-10-31 13:42:45 +00004199 if (!GetCaseResults(SI, CaseVal, CI.getCaseSuccessor(), &CommonDest,
Rafael Espindola37dc9e12014-02-21 00:06:31 +00004200 Results, DL))
Hans Wennborg8a62fc52012-09-06 09:43:28 +00004201 return false;
4202
4203 // Append the result from this case to the list for each phi.
Hans Wennborga6a11a92014-11-18 02:37:11 +00004204 for (const auto &I : Results) {
4205 PHINode *PHI = I.first;
4206 Constant *Value = I.second;
4207 if (!ResultLists.count(PHI))
4208 PHIs.push_back(PHI);
4209 ResultLists[PHI].push_back(std::make_pair(CaseVal, Value));
Hans Wennborg8a62fc52012-09-06 09:43:28 +00004210 }
4211 }
4212
Hans Wennborgac114a32014-01-12 00:44:41 +00004213 // Keep track of the result types.
Hans Wennborga6a11a92014-11-18 02:37:11 +00004214 for (PHINode *PHI : PHIs) {
Hans Wennborgac114a32014-01-12 00:44:41 +00004215 ResultTypes[PHI] = ResultLists[PHI][0].second->getType();
4216 }
4217
4218 uint64_t NumResults = ResultLists[PHIs[0]].size();
4219 APInt RangeSpread = MaxCaseVal->getValue() - MinCaseVal->getValue();
4220 uint64_t TableSize = RangeSpread.getLimitedValue() + 1;
4221 bool TableHasHoles = (NumResults < TableSize);
4222
Hans Wennborgb73c0b02014-03-12 18:35:40 +00004223 // If the table has holes, we need a constant result for the default case
4224 // or a bitmask that fits in a register.
Hans Wennborg7fd5c8442012-09-10 07:44:22 +00004225 SmallVector<std::pair<PHINode*, Constant*>, 4> DefaultResultsList;
Erik Eckstein0d86c762014-11-27 15:13:14 +00004226 bool HasDefaultResults = GetCaseResults(SI, nullptr, SI->getDefaultDest(),
Mehdi Aminia28d91d2015-03-10 02:37:25 +00004227 &CommonDest, DefaultResultsList, DL);
Hans Wennborga6a11a92014-11-18 02:37:11 +00004228
Hans Wennborgb73c0b02014-03-12 18:35:40 +00004229 bool NeedMask = (TableHasHoles && !HasDefaultResults);
4230 if (NeedMask) {
4231 // As an extra penalty for the validity test we require more cases.
4232 if (SI->getNumCases() < 4) // FIXME: Find best threshold value (benchmark).
4233 return false;
Mehdi Aminia28d91d2015-03-10 02:37:25 +00004234 if (!DL.fitsInLegalInteger(TableSize))
Hans Wennborgb73c0b02014-03-12 18:35:40 +00004235 return false;
4236 }
Hans Wennborgac114a32014-01-12 00:44:41 +00004237
Hans Wennborga6a11a92014-11-18 02:37:11 +00004238 for (const auto &I : DefaultResultsList) {
4239 PHINode *PHI = I.first;
4240 Constant *Result = I.second;
Hans Wennborg7fd5c8442012-09-10 07:44:22 +00004241 DefaultResults[PHI] = Result;
Hans Wennborg8a62fc52012-09-06 09:43:28 +00004242 }
4243
Rafael Espindola37dc9e12014-02-21 00:06:31 +00004244 if (!ShouldBuildLookupTable(SI, TableSize, TTI, DL, ResultTypes))
Hans Wennborg8a62fc52012-09-06 09:43:28 +00004245 return false;
4246
Hans Wennborg8a62fc52012-09-06 09:43:28 +00004247 // Create the BB that does the lookups.
Hans Wennborg776d7122012-09-26 09:34:53 +00004248 Module &Mod = *CommonDest->getParent()->getParent();
Hans Wennborg8a62fc52012-09-06 09:43:28 +00004249 BasicBlock *LookupBB = BasicBlock::Create(Mod.getContext(),
4250 "switch.lookup",
4251 CommonDest->getParent(),
4252 CommonDest);
4253
Michael Gottesmanc024f322013-10-20 07:04:37 +00004254 // Compute the table index value.
Hans Wennborg8a62fc52012-09-06 09:43:28 +00004255 Builder.SetInsertPoint(SI);
4256 Value *TableIndex = Builder.CreateSub(SI->getCondition(), MinCaseVal,
4257 "switch.tableidx");
Michael Gottesmanc024f322013-10-20 07:04:37 +00004258
4259 // Compute the maximum table size representable by the integer type we are
4260 // switching upon.
Michael Gottesman63c63ac2013-10-21 05:20:11 +00004261 unsigned CaseSize = MinCaseVal->getType()->getPrimitiveSizeInBits();
Hans Wennborgac114a32014-01-12 00:44:41 +00004262 uint64_t MaxTableSize = CaseSize > 63 ? UINT64_MAX : 1ULL << CaseSize;
Michael Gottesmanc024f322013-10-20 07:04:37 +00004263 assert(MaxTableSize >= TableSize &&
4264 "It is impossible for a switch to have more entries than the max "
4265 "representable value of its input integer type's size.");
4266
Hans Wennborgb64cb272015-01-26 19:52:34 +00004267 // If the default destination is unreachable, or if the lookup table covers
4268 // all values of the conditional variable, branch directly to the lookup table
4269 // BB. Otherwise, check that the condition is within the case range.
4270 const bool DefaultIsReachable =
4271 !isa<UnreachableInst>(SI->getDefaultDest()->getFirstNonPHIOrDbg());
4272 const bool GeneratingCoveredLookupTable = (MaxTableSize == TableSize);
Erik Eckstein0d86c762014-11-27 15:13:14 +00004273 BranchInst *RangeCheckBranch = nullptr;
4274
Hans Wennborgb64cb272015-01-26 19:52:34 +00004275 if (!DefaultIsReachable || GeneratingCoveredLookupTable) {
Michael Gottesmanc024f322013-10-20 07:04:37 +00004276 Builder.CreateBr(LookupBB);
Hans Wennborg86ac6302015-04-24 20:57:56 +00004277 // Note: We call removeProdecessor later since we need to be able to get the
4278 // PHI value for the default case in case we're using a bit mask.
Michael Gottesmanc024f322013-10-20 07:04:37 +00004279 } else {
4280 Value *Cmp = Builder.CreateICmpULT(TableIndex, ConstantInt::get(
Manman Renedc60372014-07-23 23:13:23 +00004281 MinCaseVal->getType(), TableSize));
Erik Eckstein0d86c762014-11-27 15:13:14 +00004282 RangeCheckBranch = Builder.CreateCondBr(Cmp, LookupBB, SI->getDefaultDest());
Michael Gottesmanc024f322013-10-20 07:04:37 +00004283 }
Hans Wennborg8a62fc52012-09-06 09:43:28 +00004284
4285 // Populate the BB that does the lookups.
4286 Builder.SetInsertPoint(LookupBB);
Hans Wennborgb73c0b02014-03-12 18:35:40 +00004287
4288 if (NeedMask) {
4289 // Before doing the lookup we do the hole check.
4290 // The LookupBB is therefore re-purposed to do the hole check
4291 // and we create a new LookupBB.
4292 BasicBlock *MaskBB = LookupBB;
4293 MaskBB->setName("switch.hole_check");
4294 LookupBB = BasicBlock::Create(Mod.getContext(),
4295 "switch.lookup",
4296 CommonDest->getParent(),
4297 CommonDest);
4298
Juergen Ributzkac9591e92014-11-17 19:39:56 +00004299 // Make the mask's bitwidth at least 8bit and a power-of-2 to avoid
4300 // unnecessary illegal types.
4301 uint64_t TableSizePowOf2 = NextPowerOf2(std::max(7ULL, TableSize - 1ULL));
4302 APInt MaskInt(TableSizePowOf2, 0);
4303 APInt One(TableSizePowOf2, 1);
Hans Wennborgb73c0b02014-03-12 18:35:40 +00004304 // Build bitmask; fill in a 1 bit for every case.
Hans Wennborgb73c0b02014-03-12 18:35:40 +00004305 const ResultListTy &ResultList = ResultLists[PHIs[0]];
4306 for (size_t I = 0, E = ResultList.size(); I != E; ++I) {
4307 uint64_t Idx = (ResultList[I].first->getValue() -
4308 MinCaseVal->getValue()).getLimitedValue();
4309 MaskInt |= One << Idx;
4310 }
4311 ConstantInt *TableMask = ConstantInt::get(Mod.getContext(), MaskInt);
4312
4313 // Get the TableIndex'th bit of the bitmask.
4314 // If this bit is 0 (meaning hole) jump to the default destination,
4315 // else continue with table lookup.
4316 IntegerType *MapTy = TableMask->getType();
4317 Value *MaskIndex = Builder.CreateZExtOrTrunc(TableIndex, MapTy,
4318 "switch.maskindex");
4319 Value *Shifted = Builder.CreateLShr(TableMask, MaskIndex,
4320 "switch.shifted");
4321 Value *LoBit = Builder.CreateTrunc(Shifted,
4322 Type::getInt1Ty(Mod.getContext()),
4323 "switch.lobit");
4324 Builder.CreateCondBr(LoBit, LookupBB, SI->getDefaultDest());
4325
4326 Builder.SetInsertPoint(LookupBB);
4327 AddPredecessorToBlock(SI->getDefaultDest(), MaskBB, SI->getParent());
4328 }
4329
Hans Wennborg86ac6302015-04-24 20:57:56 +00004330 if (!DefaultIsReachable || GeneratingCoveredLookupTable) {
4331 // We cached PHINodes in PHIs, to avoid accessing deleted PHINodes later,
4332 // do not delete PHINodes here.
4333 SI->getDefaultDest()->removePredecessor(SI->getParent(),
4334 /*DontDeleteUselessPHIs=*/true);
4335 }
4336
Hans Wennborg8a62fc52012-09-06 09:43:28 +00004337 bool ReturnedEarly = false;
Hans Wennborg776d7122012-09-26 09:34:53 +00004338 for (size_t I = 0, E = PHIs.size(); I != E; ++I) {
4339 PHINode *PHI = PHIs[I];
Erik Eckstein0d86c762014-11-27 15:13:14 +00004340 const ResultListTy &ResultList = ResultLists[PHI];
Hans Wennborg8a62fc52012-09-06 09:43:28 +00004341
Hans Wennborgb73c0b02014-03-12 18:35:40 +00004342 // If using a bitmask, use any value to fill the lookup table holes.
4343 Constant *DV = NeedMask ? ResultLists[PHI][0].second : DefaultResults[PHI];
Erik Eckstein0d86c762014-11-27 15:13:14 +00004344 SwitchLookupTable Table(Mod, TableSize, MinCaseVal, ResultList, DV, DL);
Hans Wennborg776d7122012-09-26 09:34:53 +00004345
Manman Ren4d189fb2014-07-24 21:13:20 +00004346 Value *Result = Table.BuildLookup(TableIndex, Builder);
Hans Wennborg8a62fc52012-09-06 09:43:28 +00004347
Hans Wennborgf744fa92012-09-19 14:24:21 +00004348 // If the result is used to return immediately from the function, we want to
4349 // do that right here.
Chandler Carruthcdf47882014-03-09 03:16:01 +00004350 if (PHI->hasOneUse() && isa<ReturnInst>(*PHI->user_begin()) &&
4351 PHI->user_back() == CommonDest->getFirstNonPHIOrDbg()) {
Hans Wennborgf744fa92012-09-19 14:24:21 +00004352 Builder.CreateRet(Result);
4353 ReturnedEarly = true;
4354 break;
Hans Wennborg8a62fc52012-09-06 09:43:28 +00004355 }
4356
Erik Eckstein0d86c762014-11-27 15:13:14 +00004357 // Do a small peephole optimization: re-use the switch table compare if
4358 // possible.
4359 if (!TableHasHoles && HasDefaultResults && RangeCheckBranch) {
4360 BasicBlock *PhiBlock = PHI->getParent();
4361 // Search for compare instructions which use the phi.
4362 for (auto *User : PHI->users()) {
4363 reuseTableCompare(User, PhiBlock, RangeCheckBranch, DV, ResultList);
4364 }
4365 }
4366
Hans Wennborgf744fa92012-09-19 14:24:21 +00004367 PHI->addIncoming(Result, LookupBB);
Hans Wennborg8a62fc52012-09-06 09:43:28 +00004368 }
4369
4370 if (!ReturnedEarly)
4371 Builder.CreateBr(CommonDest);
4372
4373 // Remove the switch.
Michael Gottesman63c63ac2013-10-21 05:20:11 +00004374 for (unsigned i = 0, e = SI->getNumSuccessors(); i < e; ++i) {
Hans Wennborg8a62fc52012-09-06 09:43:28 +00004375 BasicBlock *Succ = SI->getSuccessor(i);
Michael Gottesmanc024f322013-10-20 07:04:37 +00004376
Michael Gottesman63c63ac2013-10-21 05:20:11 +00004377 if (Succ == SI->getDefaultDest())
Michael Gottesmanc024f322013-10-20 07:04:37 +00004378 continue;
Hans Wennborg8a62fc52012-09-06 09:43:28 +00004379 Succ->removePredecessor(SI->getParent());
4380 }
4381 SI->eraseFromParent();
4382
4383 ++NumLookupTables;
Hans Wennborgb73c0b02014-03-12 18:35:40 +00004384 if (NeedMask)
4385 ++NumLookupTablesHoles;
Hans Wennborg8a62fc52012-09-06 09:43:28 +00004386 return true;
4387}
4388
Devang Patela7ec47d2011-05-18 20:35:38 +00004389bool SimplifyCFGOpt::SimplifySwitch(SwitchInst *SI, IRBuilder<> &Builder) {
Chris Lattner25c3af32010-12-13 06:25:44 +00004390 BasicBlock *BB = SI->getParent();
4391
Jakob Stoklund Olesen977f41a2012-10-25 18:51:15 +00004392 if (isValueEqualityComparison(SI)) {
4393 // If we only have one predecessor, and if it is a branch on this value,
4394 // see if that predecessor totally determines the outcome of this switch.
4395 if (BasicBlock *OnlyPred = BB->getSinglePredecessor())
4396 if (SimplifyEqualityComparisonWithOnlyPredecessor(SI, OnlyPred, Builder))
Mehdi Aminia28d91d2015-03-10 02:37:25 +00004397 return SimplifyCFG(BB, TTI, BonusInstThreshold, AC) | true;
Frits van Bommel8ae07992011-02-28 09:44:07 +00004398
Jakob Stoklund Olesen977f41a2012-10-25 18:51:15 +00004399 Value *Cond = SI->getCondition();
4400 if (SelectInst *Select = dyn_cast<SelectInst>(Cond))
4401 if (SimplifySwitchOnSelect(SI, Select))
Mehdi Aminia28d91d2015-03-10 02:37:25 +00004402 return SimplifyCFG(BB, TTI, BonusInstThreshold, AC) | true;
Frits van Bommel8ae07992011-02-28 09:44:07 +00004403
Jakob Stoklund Olesen977f41a2012-10-25 18:51:15 +00004404 // If the block only contains the switch, see if we can fold the block
4405 // away into any preds.
4406 BasicBlock::iterator BBI = BB->begin();
4407 // Ignore dbg intrinsics.
4408 while (isa<DbgInfoIntrinsic>(BBI))
4409 ++BBI;
4410 if (SI == &*BBI)
4411 if (FoldValueComparisonIntoPredecessors(SI, Builder))
Mehdi Aminia28d91d2015-03-10 02:37:25 +00004412 return SimplifyCFG(BB, TTI, BonusInstThreshold, AC) | true;
Jakob Stoklund Olesen977f41a2012-10-25 18:51:15 +00004413 }
Benjamin Kramerf4ea1d52011-02-02 15:56:22 +00004414
4415 // Try to transform the switch into an icmp and a branch.
Devang Patela7ec47d2011-05-18 20:35:38 +00004416 if (TurnSwitchRangeIntoICmp(SI, Builder))
Mehdi Aminia28d91d2015-03-10 02:37:25 +00004417 return SimplifyCFG(BB, TTI, BonusInstThreshold, AC) | true;
Benjamin Kramerd96205c2011-05-14 15:57:25 +00004418
4419 // Remove unreachable cases.
Mehdi Aminia28d91d2015-03-10 02:37:25 +00004420 if (EliminateDeadSwitchCases(SI, AC, DL))
4421 return SimplifyCFG(BB, TTI, BonusInstThreshold, AC) | true;
Benjamin Kramerd96205c2011-05-14 15:57:25 +00004422
Mehdi Aminia28d91d2015-03-10 02:37:25 +00004423 if (SwitchToSelect(SI, Builder, AC, DL))
4424 return SimplifyCFG(BB, TTI, BonusInstThreshold, AC) | true;
Marcello Maggioni5bbe3df2014-10-14 01:58:26 +00004425
Hans Wennborg4ab4a8e2011-06-18 10:28:47 +00004426 if (ForwardSwitchConditionToPHI(SI))
Mehdi Aminia28d91d2015-03-10 02:37:25 +00004427 return SimplifyCFG(BB, TTI, BonusInstThreshold, AC) | true;
Hans Wennborg4ab4a8e2011-06-18 10:28:47 +00004428
Mehdi Aminia28d91d2015-03-10 02:37:25 +00004429 if (SwitchToLookupTable(SI, Builder, DL, TTI))
4430 return SimplifyCFG(BB, TTI, BonusInstThreshold, AC) | true;
Hans Wennborg8a62fc52012-09-06 09:43:28 +00004431
Chris Lattner25c3af32010-12-13 06:25:44 +00004432 return false;
4433}
4434
4435bool SimplifyCFGOpt::SimplifyIndirectBr(IndirectBrInst *IBI) {
4436 BasicBlock *BB = IBI->getParent();
4437 bool Changed = false;
Andrew Trickf3cf1932012-08-29 21:46:36 +00004438
Chris Lattner25c3af32010-12-13 06:25:44 +00004439 // Eliminate redundant destinations.
4440 SmallPtrSet<Value *, 8> Succs;
4441 for (unsigned i = 0, e = IBI->getNumDestinations(); i != e; ++i) {
4442 BasicBlock *Dest = IBI->getDestination(i);
David Blaikie70573dc2014-11-19 07:49:26 +00004443 if (!Dest->hasAddressTaken() || !Succs.insert(Dest).second) {
Chris Lattner25c3af32010-12-13 06:25:44 +00004444 Dest->removePredecessor(BB);
4445 IBI->removeDestination(i);
4446 --i; --e;
4447 Changed = true;
4448 }
Andrew Trickf3cf1932012-08-29 21:46:36 +00004449 }
Chris Lattner25c3af32010-12-13 06:25:44 +00004450
4451 if (IBI->getNumDestinations() == 0) {
4452 // If the indirectbr has no successors, change it to unreachable.
4453 new UnreachableInst(IBI->getContext(), IBI);
4454 EraseTerminatorInstAndDCECond(IBI);
4455 return true;
4456 }
Andrew Trickf3cf1932012-08-29 21:46:36 +00004457
Chris Lattner25c3af32010-12-13 06:25:44 +00004458 if (IBI->getNumDestinations() == 1) {
4459 // If the indirectbr has one successor, change it to a direct branch.
4460 BranchInst::Create(IBI->getDestination(0), IBI);
4461 EraseTerminatorInstAndDCECond(IBI);
4462 return true;
4463 }
Andrew Trickf3cf1932012-08-29 21:46:36 +00004464
Chris Lattner25c3af32010-12-13 06:25:44 +00004465 if (SelectInst *SI = dyn_cast<SelectInst>(IBI->getAddress())) {
4466 if (SimplifyIndirectBrOnSelect(IBI, SI))
Mehdi Aminia28d91d2015-03-10 02:37:25 +00004467 return SimplifyCFG(BB, TTI, BonusInstThreshold, AC) | true;
Chris Lattner25c3af32010-12-13 06:25:44 +00004468 }
4469 return Changed;
4470}
4471
Philip Reames2b969d72015-03-24 22:28:45 +00004472/// Given an block with only a single landing pad and a unconditional branch
4473/// try to find another basic block which this one can be merged with. This
4474/// handles cases where we have multiple invokes with unique landing pads, but
4475/// a shared handler.
4476///
4477/// We specifically choose to not worry about merging non-empty blocks
4478/// here. That is a PRE/scheduling problem and is best solved elsewhere. In
4479/// practice, the optimizer produces empty landing pad blocks quite frequently
4480/// when dealing with exception dense code. (see: instcombine, gvn, if-else
4481/// sinking in this file)
4482///
4483/// This is primarily a code size optimization. We need to avoid performing
4484/// any transform which might inhibit optimization (such as our ability to
4485/// specialize a particular handler via tail commoning). We do this by not
4486/// merging any blocks which require us to introduce a phi. Since the same
4487/// values are flowing through both blocks, we don't loose any ability to
4488/// specialize. If anything, we make such specialization more likely.
4489///
4490/// TODO - This transformation could remove entries from a phi in the target
4491/// block when the inputs in the phi are the same for the two blocks being
4492/// merged. In some cases, this could result in removal of the PHI entirely.
4493static bool TryToMergeLandingPad(LandingPadInst *LPad, BranchInst *BI,
4494 BasicBlock *BB) {
4495 auto Succ = BB->getUniqueSuccessor();
4496 assert(Succ);
4497 // If there's a phi in the successor block, we'd likely have to introduce
4498 // a phi into the merged landing pad block.
4499 if (isa<PHINode>(*Succ->begin()))
4500 return false;
4501
4502 for (BasicBlock *OtherPred : predecessors(Succ)) {
4503 if (BB == OtherPred)
4504 continue;
4505 BasicBlock::iterator I = OtherPred->begin();
4506 LandingPadInst *LPad2 = dyn_cast<LandingPadInst>(I);
4507 if (!LPad2 || !LPad2->isIdenticalTo(LPad))
4508 continue;
4509 for (++I; isa<DbgInfoIntrinsic>(I); ++I) {}
4510 BranchInst *BI2 = dyn_cast<BranchInst>(I);
4511 if (!BI2 || !BI2->isIdenticalTo(BI))
4512 continue;
4513
4514 // We've found an identical block. Update our predeccessors to take that
4515 // path instead and make ourselves dead.
4516 SmallSet<BasicBlock *, 16> Preds;
4517 Preds.insert(pred_begin(BB), pred_end(BB));
4518 for (BasicBlock *Pred : Preds) {
4519 InvokeInst *II = cast<InvokeInst>(Pred->getTerminator());
4520 assert(II->getNormalDest() != BB &&
4521 II->getUnwindDest() == BB && "unexpected successor");
4522 II->setUnwindDest(OtherPred);
4523 }
4524
4525 // The debug info in OtherPred doesn't cover the merged control flow that
4526 // used to go through BB. We need to delete it or update it.
4527 for (auto I = OtherPred->begin(), E = OtherPred->end();
4528 I != E;) {
4529 Instruction &Inst = *I; I++;
4530 if (isa<DbgInfoIntrinsic>(Inst))
4531 Inst.eraseFromParent();
4532 }
4533
4534 SmallSet<BasicBlock *, 16> Succs;
4535 Succs.insert(succ_begin(BB), succ_end(BB));
4536 for (BasicBlock *Succ : Succs) {
4537 Succ->removePredecessor(BB);
4538 }
4539
4540 IRBuilder<> Builder(BI);
4541 Builder.CreateUnreachable();
4542 BI->eraseFromParent();
4543 return true;
4544 }
4545 return false;
4546}
4547
Devang Patel767f6932011-05-18 18:28:48 +00004548bool SimplifyCFGOpt::SimplifyUncondBranch(BranchInst *BI, IRBuilder<> &Builder){
Chris Lattner25c3af32010-12-13 06:25:44 +00004549 BasicBlock *BB = BI->getParent();
Andrew Trickf3cf1932012-08-29 21:46:36 +00004550
Manman Ren93ab6492012-09-20 22:37:36 +00004551 if (SinkCommon && SinkThenElseCodeToEnd(BI))
4552 return true;
4553
Chris Lattner25c3af32010-12-13 06:25:44 +00004554 // If the Terminator is the only non-phi instruction, simplify the block.
Duncan P. N. Exon Smith5b4c8372015-10-13 02:39:05 +00004555 BasicBlock::iterator I = BB->getFirstNonPHIOrDbg()->getIterator();
Chris Lattner25c3af32010-12-13 06:25:44 +00004556 if (I->isTerminator() && BB != &BB->getParent()->getEntryBlock() &&
4557 TryToSimplifyUncondBranchFromEmptyBlock(BB))
4558 return true;
Andrew Trickf3cf1932012-08-29 21:46:36 +00004559
Chris Lattner25c3af32010-12-13 06:25:44 +00004560 // If the only instruction in the block is a seteq/setne comparison
4561 // against a constant, try to simplify the block.
4562 if (ICmpInst *ICI = dyn_cast<ICmpInst>(I))
4563 if (ICI->isEquality() && isa<ConstantInt>(ICI->getOperand(1))) {
4564 for (++I; isa<DbgInfoIntrinsic>(I); ++I)
4565 ;
Nick Lewyckye87d54c2011-12-26 20:37:40 +00004566 if (I->isTerminator() &&
Mehdi Aminia28d91d2015-03-10 02:37:25 +00004567 TryToSimplifyUncondBranchWithICmpInIt(ICI, Builder, DL, TTI,
4568 BonusInstThreshold, AC))
Chris Lattner25c3af32010-12-13 06:25:44 +00004569 return true;
4570 }
Andrew Trickf3cf1932012-08-29 21:46:36 +00004571
Philip Reames2b969d72015-03-24 22:28:45 +00004572 // See if we can merge an empty landing pad block with another which is
4573 // equivalent.
4574 if (LandingPadInst *LPad = dyn_cast<LandingPadInst>(I)) {
4575 for (++I; isa<DbgInfoIntrinsic>(I); ++I) {}
4576 if (I->isTerminator() &&
4577 TryToMergeLandingPad(LPad, BI, BB))
4578 return true;
4579 }
4580
Manman Rend33f4ef2012-06-13 05:43:29 +00004581 // If this basic block is ONLY a compare and a branch, and if a predecessor
4582 // branches to us and our successor, fold the comparison into the
4583 // predecessor and use logical operations to update the incoming value
4584 // for PHI nodes in common successor.
Mehdi Aminia28d91d2015-03-10 02:37:25 +00004585 if (FoldBranchToCommonDest(BI, BonusInstThreshold))
4586 return SimplifyCFG(BB, TTI, BonusInstThreshold, AC) | true;
Chris Lattner25c3af32010-12-13 06:25:44 +00004587 return false;
4588}
4589
4590
Devang Patela7ec47d2011-05-18 20:35:38 +00004591bool SimplifyCFGOpt::SimplifyCondBranch(BranchInst *BI, IRBuilder<> &Builder) {
Chris Lattner25c3af32010-12-13 06:25:44 +00004592 BasicBlock *BB = BI->getParent();
Andrew Trickf3cf1932012-08-29 21:46:36 +00004593
Chris Lattner25c3af32010-12-13 06:25:44 +00004594 // Conditional branch
4595 if (isValueEqualityComparison(BI)) {
4596 // If we only have one predecessor, and if it is a branch on this value,
4597 // see if that predecessor totally determines the outcome of this
4598 // switch.
4599 if (BasicBlock *OnlyPred = BB->getSinglePredecessor())
Devang Patela7ec47d2011-05-18 20:35:38 +00004600 if (SimplifyEqualityComparisonWithOnlyPredecessor(BI, OnlyPred, Builder))
Mehdi Aminia28d91d2015-03-10 02:37:25 +00004601 return SimplifyCFG(BB, TTI, BonusInstThreshold, AC) | true;
Andrew Trickf3cf1932012-08-29 21:46:36 +00004602
Chris Lattner25c3af32010-12-13 06:25:44 +00004603 // This block must be empty, except for the setcond inst, if it exists.
4604 // Ignore dbg intrinsics.
4605 BasicBlock::iterator I = BB->begin();
4606 // Ignore dbg intrinsics.
4607 while (isa<DbgInfoIntrinsic>(I))
4608 ++I;
4609 if (&*I == BI) {
Devang Patel58380552011-05-18 20:53:17 +00004610 if (FoldValueComparisonIntoPredecessors(BI, Builder))
Mehdi Aminia28d91d2015-03-10 02:37:25 +00004611 return SimplifyCFG(BB, TTI, BonusInstThreshold, AC) | true;
Chris Lattner25c3af32010-12-13 06:25:44 +00004612 } else if (&*I == cast<Instruction>(BI->getCondition())){
4613 ++I;
4614 // Ignore dbg intrinsics.
4615 while (isa<DbgInfoIntrinsic>(I))
4616 ++I;
Devang Patel58380552011-05-18 20:53:17 +00004617 if (&*I == BI && FoldValueComparisonIntoPredecessors(BI, Builder))
Mehdi Aminia28d91d2015-03-10 02:37:25 +00004618 return SimplifyCFG(BB, TTI, BonusInstThreshold, AC) | true;
Chris Lattner25c3af32010-12-13 06:25:44 +00004619 }
4620 }
Andrew Trickf3cf1932012-08-29 21:46:36 +00004621
Chris Lattner25c3af32010-12-13 06:25:44 +00004622 // Try to turn "br (X == 0 | X == 1), T, F" into a switch instruction.
Mehdi Aminia28d91d2015-03-10 02:37:25 +00004623 if (SimplifyBranchOnICmpChain(BI, Builder, DL))
Chris Lattner25c3af32010-12-13 06:25:44 +00004624 return true;
Andrew Trickf3cf1932012-08-29 21:46:36 +00004625
Dan Gohman5ab9c0a2012-01-05 23:58:56 +00004626 // If this basic block is ONLY a compare and a branch, and if a predecessor
4627 // branches to us and one of our successors, fold the comparison into the
4628 // predecessor and use logical operations to pick the right destination.
Mehdi Aminia28d91d2015-03-10 02:37:25 +00004629 if (FoldBranchToCommonDest(BI, BonusInstThreshold))
4630 return SimplifyCFG(BB, TTI, BonusInstThreshold, AC) | true;
Andrew Trickf3cf1932012-08-29 21:46:36 +00004631
Chris Lattner25c3af32010-12-13 06:25:44 +00004632 // We have a conditional branch to two blocks that are only reachable
4633 // from BI. We know that the condbr dominates the two blocks, so see if
4634 // there is any identical code in the "then" and "else" blocks. If so, we
4635 // can hoist it up to the branching block.
Craig Topperf40110f2014-04-25 05:29:35 +00004636 if (BI->getSuccessor(0)->getSinglePredecessor()) {
4637 if (BI->getSuccessor(1)->getSinglePredecessor()) {
Mehdi Aminia28d91d2015-03-10 02:37:25 +00004638 if (HoistThenElseCodeToIf(BI, TTI))
4639 return SimplifyCFG(BB, TTI, BonusInstThreshold, AC) | true;
Chris Lattner25c3af32010-12-13 06:25:44 +00004640 } else {
4641 // If Successor #1 has multiple preds, we may be able to conditionally
Sanjay Patel0a2ada72014-07-06 23:10:24 +00004642 // execute Successor #0 if it branches to Successor #1.
Chris Lattner25c3af32010-12-13 06:25:44 +00004643 TerminatorInst *Succ0TI = BI->getSuccessor(0)->getTerminator();
4644 if (Succ0TI->getNumSuccessors() == 1 &&
4645 Succ0TI->getSuccessor(0) == BI->getSuccessor(1))
Mehdi Aminia28d91d2015-03-10 02:37:25 +00004646 if (SpeculativelyExecuteBB(BI, BI->getSuccessor(0), TTI))
4647 return SimplifyCFG(BB, TTI, BonusInstThreshold, AC) | true;
Chris Lattner25c3af32010-12-13 06:25:44 +00004648 }
Craig Topperf40110f2014-04-25 05:29:35 +00004649 } else if (BI->getSuccessor(1)->getSinglePredecessor()) {
Chris Lattner25c3af32010-12-13 06:25:44 +00004650 // If Successor #0 has multiple preds, we may be able to conditionally
Sanjay Patel0a2ada72014-07-06 23:10:24 +00004651 // execute Successor #1 if it branches to Successor #0.
Chris Lattner25c3af32010-12-13 06:25:44 +00004652 TerminatorInst *Succ1TI = BI->getSuccessor(1)->getTerminator();
4653 if (Succ1TI->getNumSuccessors() == 1 &&
4654 Succ1TI->getSuccessor(0) == BI->getSuccessor(0))
Mehdi Aminia28d91d2015-03-10 02:37:25 +00004655 if (SpeculativelyExecuteBB(BI, BI->getSuccessor(1), TTI))
4656 return SimplifyCFG(BB, TTI, BonusInstThreshold, AC) | true;
Chris Lattner25c3af32010-12-13 06:25:44 +00004657 }
Andrew Trickf3cf1932012-08-29 21:46:36 +00004658
Chris Lattner25c3af32010-12-13 06:25:44 +00004659 // If this is a branch on a phi node in the current block, thread control
4660 // through this block if any PHI node entries are constants.
4661 if (PHINode *PN = dyn_cast<PHINode>(BI->getCondition()))
4662 if (PN->getParent() == BI->getParent())
Rafael Espindola37dc9e12014-02-21 00:06:31 +00004663 if (FoldCondBranchOnPHI(BI, DL))
Mehdi Aminia28d91d2015-03-10 02:37:25 +00004664 return SimplifyCFG(BB, TTI, BonusInstThreshold, AC) | true;
Andrew Trickf3cf1932012-08-29 21:46:36 +00004665
Chris Lattner25c3af32010-12-13 06:25:44 +00004666 // Scan predecessor blocks for conditional branches.
Duncan P. N. Exon Smith6c990152014-07-21 17:06:51 +00004667 for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI)
4668 if (BranchInst *PBI = dyn_cast<BranchInst>((*PI)->getTerminator()))
Chris Lattner25c3af32010-12-13 06:25:44 +00004669 if (PBI != BI && PBI->isConditional())
Philip Reamesb42db212015-10-14 22:46:19 +00004670 if (SimplifyCondBranchToCondBranch(PBI, BI, DL))
Mehdi Aminia28d91d2015-03-10 02:37:25 +00004671 return SimplifyCFG(BB, TTI, BonusInstThreshold, AC) | true;
Chris Lattner25c3af32010-12-13 06:25:44 +00004672
4673 return false;
4674}
4675
Benjamin Kramerfb212a62011-08-26 01:22:29 +00004676/// Check if passing a value to an instruction will cause undefined behavior.
4677static bool passingValueIsAlwaysUndefined(Value *V, Instruction *I) {
4678 Constant *C = dyn_cast<Constant>(V);
4679 if (!C)
4680 return false;
4681
Benjamin Kramerd12e82e2012-10-04 16:11:49 +00004682 if (I->use_empty())
Benjamin Kramerfb212a62011-08-26 01:22:29 +00004683 return false;
4684
4685 if (C->isNullValue()) {
Benjamin Kramerd12e82e2012-10-04 16:11:49 +00004686 // Only look at the first use, avoid hurting compile time with long uselists
Chandler Carruthcdf47882014-03-09 03:16:01 +00004687 User *Use = *I->user_begin();
Benjamin Kramerfb212a62011-08-26 01:22:29 +00004688
4689 // Now make sure that there are no instructions in between that can alter
4690 // control flow (eg. calls)
4691 for (BasicBlock::iterator i = ++BasicBlock::iterator(I); &*i != Use; ++i)
Benjamin Kramer0655b782011-08-26 02:25:55 +00004692 if (i == I->getParent()->end() || i->mayHaveSideEffects())
Benjamin Kramerfb212a62011-08-26 01:22:29 +00004693 return false;
4694
4695 // Look through GEPs. A load from a GEP derived from NULL is still undefined
4696 if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Use))
4697 if (GEP->getPointerOperand() == I)
4698 return passingValueIsAlwaysUndefined(V, GEP);
4699
4700 // Look through bitcasts.
4701 if (BitCastInst *BC = dyn_cast<BitCastInst>(Use))
4702 return passingValueIsAlwaysUndefined(V, BC);
4703
Benjamin Kramer0655b782011-08-26 02:25:55 +00004704 // Load from null is undefined.
4705 if (LoadInst *LI = dyn_cast<LoadInst>(Use))
Andrew Tricka0a5ca02013-03-07 01:03:35 +00004706 if (!LI->isVolatile())
4707 return LI->getPointerAddressSpace() == 0;
Benjamin Kramerfb212a62011-08-26 01:22:29 +00004708
Benjamin Kramer0655b782011-08-26 02:25:55 +00004709 // Store to null is undefined.
4710 if (StoreInst *SI = dyn_cast<StoreInst>(Use))
Andrew Tricka0a5ca02013-03-07 01:03:35 +00004711 if (!SI->isVolatile())
4712 return SI->getPointerAddressSpace() == 0 && SI->getPointerOperand() == I;
Benjamin Kramerfb212a62011-08-26 01:22:29 +00004713 }
4714 return false;
4715}
4716
4717/// If BB has an incoming value that will always trigger undefined behavior
Nick Lewyckye87d54c2011-12-26 20:37:40 +00004718/// (eg. null pointer dereference), remove the branch leading here.
Benjamin Kramerfb212a62011-08-26 01:22:29 +00004719static bool removeUndefIntroducingPredecessor(BasicBlock *BB) {
4720 for (BasicBlock::iterator i = BB->begin();
4721 PHINode *PHI = dyn_cast<PHINode>(i); ++i)
4722 for (unsigned i = 0, e = PHI->getNumIncomingValues(); i != e; ++i)
4723 if (passingValueIsAlwaysUndefined(PHI->getIncomingValue(i), PHI)) {
4724 TerminatorInst *T = PHI->getIncomingBlock(i)->getTerminator();
4725 IRBuilder<> Builder(T);
4726 if (BranchInst *BI = dyn_cast<BranchInst>(T)) {
4727 BB->removePredecessor(PHI->getIncomingBlock(i));
4728 // Turn uncoditional branches into unreachables and remove the dead
4729 // destination from conditional branches.
4730 if (BI->isUnconditional())
4731 Builder.CreateUnreachable();
4732 else
4733 Builder.CreateBr(BI->getSuccessor(0) == BB ? BI->getSuccessor(1) :
4734 BI->getSuccessor(0));
4735 BI->eraseFromParent();
4736 return true;
4737 }
4738 // TODO: SwitchInst.
4739 }
4740
4741 return false;
4742}
4743
Jakob Stoklund Olesen916f48a2010-02-05 22:03:18 +00004744bool SimplifyCFGOpt::run(BasicBlock *BB) {
Chris Lattner3f5823f2003-08-24 18:36:16 +00004745 bool Changed = false;
Chris Lattner466a0492002-05-21 20:50:24 +00004746
Chris Lattnerd7beca32010-12-14 06:17:25 +00004747 assert(BB && BB->getParent() && "Block not embedded in function!");
Chris Lattner466a0492002-05-21 20:50:24 +00004748 assert(BB->getTerminator() && "Degenerate basic block encountered!");
Chris Lattner466a0492002-05-21 20:50:24 +00004749
Dan Gohman4a63fad2010-08-14 00:29:42 +00004750 // Remove basic blocks that have no predecessors (except the entry block)...
4751 // or that just have themself as a predecessor. These are unreachable.
Ramkumar Ramachandra181233b2015-01-13 04:17:47 +00004752 if ((pred_empty(BB) &&
Chris Lattnerd7beca32010-12-14 06:17:25 +00004753 BB != &BB->getParent()->getEntryBlock()) ||
Dan Gohman4a63fad2010-08-14 00:29:42 +00004754 BB->getSinglePredecessor() == BB) {
David Greene725c7c32010-01-05 01:26:52 +00004755 DEBUG(dbgs() << "Removing BB: \n" << *BB);
Chris Lattner7eb270e2008-12-03 06:40:52 +00004756 DeleteDeadBlock(BB);
Chris Lattner466a0492002-05-21 20:50:24 +00004757 return true;
4758 }
4759
Chris Lattner031340a2003-08-17 19:41:53 +00004760 // Check to see if we can constant propagate this terminator instruction
4761 // away...
Frits van Bommelad964552011-05-22 16:24:18 +00004762 Changed |= ConstantFoldTerminator(BB, true);
Chris Lattner031340a2003-08-17 19:41:53 +00004763
Dan Gohman1a951062009-10-30 22:39:04 +00004764 // Check for and eliminate duplicate PHI nodes in this block.
4765 Changed |= EliminateDuplicatePHINodes(BB);
4766
Benjamin Kramerfb212a62011-08-26 01:22:29 +00004767 // Check for and remove branches that will always cause undefined behavior.
4768 Changed |= removeUndefIntroducingPredecessor(BB);
4769
Chris Lattner2e3832d2010-12-13 05:10:48 +00004770 // Merge basic blocks into their predecessor if there is only one distinct
4771 // pred, and if there is only one distinct successor of the predecessor, and
4772 // if there are no PHI nodes.
4773 //
4774 if (MergeBlockIntoPredecessor(BB))
4775 return true;
Andrew Trickf3cf1932012-08-29 21:46:36 +00004776
Devang Patel15ad6762011-05-18 18:01:27 +00004777 IRBuilder<> Builder(BB);
4778
Dan Gohman20af5a02008-03-11 21:53:06 +00004779 // If there is a trivial two-entry PHI node in this basic block, and we can
4780 // eliminate it, do so now.
4781 if (PHINode *PN = dyn_cast<PHINode>(BB->begin()))
4782 if (PN->getNumIncomingValues() == 2)
Mehdi Aminia28d91d2015-03-10 02:37:25 +00004783 Changed |= FoldTwoEntryPHINode(PN, TTI, DL);
Dan Gohman20af5a02008-03-11 21:53:06 +00004784
Devang Patela7ec47d2011-05-18 20:35:38 +00004785 Builder.SetInsertPoint(BB->getTerminator());
Chris Lattner25c3af32010-12-13 06:25:44 +00004786 if (BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator())) {
Chris Lattner1d057612010-12-13 06:36:51 +00004787 if (BI->isUnconditional()) {
Devang Patel767f6932011-05-18 18:28:48 +00004788 if (SimplifyUncondBranch(BI, Builder)) return true;
Chris Lattner1d057612010-12-13 06:36:51 +00004789 } else {
Devang Patela7ec47d2011-05-18 20:35:38 +00004790 if (SimplifyCondBranch(BI, Builder)) return true;
Chris Lattner1d057612010-12-13 06:36:51 +00004791 }
4792 } else if (ReturnInst *RI = dyn_cast<ReturnInst>(BB->getTerminator())) {
Devang Pateldd14e0f2011-05-18 21:33:11 +00004793 if (SimplifyReturn(RI, Builder)) return true;
Bill Wendlingd5d95b02012-02-06 21:16:41 +00004794 } else if (ResumeInst *RI = dyn_cast<ResumeInst>(BB->getTerminator())) {
4795 if (SimplifyResume(RI, Builder)) return true;
Andrew Kaylor50e4e862015-09-04 23:39:40 +00004796 } else if (CleanupReturnInst *RI =
4797 dyn_cast<CleanupReturnInst>(BB->getTerminator())) {
4798 if (SimplifyCleanupReturn(RI)) return true;
Chris Lattner1d057612010-12-13 06:36:51 +00004799 } else if (SwitchInst *SI = dyn_cast<SwitchInst>(BB->getTerminator())) {
Devang Patela7ec47d2011-05-18 20:35:38 +00004800 if (SimplifySwitch(SI, Builder)) return true;
Chris Lattner1d057612010-12-13 06:36:51 +00004801 } else if (UnreachableInst *UI =
4802 dyn_cast<UnreachableInst>(BB->getTerminator())) {
4803 if (SimplifyUnreachable(UI)) return true;
Chris Lattner1d057612010-12-13 06:36:51 +00004804 } else if (IndirectBrInst *IBI =
4805 dyn_cast<IndirectBrInst>(BB->getTerminator())) {
4806 if (SimplifyIndirectBr(IBI)) return true;
Chris Lattnere42732e2004-02-16 06:35:48 +00004807 }
4808
Chris Lattner031340a2003-08-17 19:41:53 +00004809 return Changed;
Chris Lattner466a0492002-05-21 20:50:24 +00004810}
Jakob Stoklund Olesen916f48a2010-02-05 22:03:18 +00004811
Sanjay Patel09159b8f2015-06-24 20:40:57 +00004812/// This function is used to do simplification of a CFG.
4813/// For example, it adjusts branches to branches to eliminate the extra hop,
Jakob Stoklund Olesen916f48a2010-02-05 22:03:18 +00004814/// eliminates unreachable basic blocks, and does other "peephole" optimization
4815/// of the CFG. It returns true if a modification was made.
4816///
Chandler Carruth0b4ef9c2013-01-07 03:53:25 +00004817bool llvm::SimplifyCFG(BasicBlock *BB, const TargetTransformInfo &TTI,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00004818 unsigned BonusInstThreshold, AssumptionCache *AC) {
4819 return SimplifyCFGOpt(TTI, BB->getModule()->getDataLayout(),
4820 BonusInstThreshold, AC).run(BB);
Jakob Stoklund Olesen916f48a2010-02-05 22:03:18 +00004821}