blob: 76358a040d9bd708b37339629cb75b3bb33d3021 [file] [log] [blame]
Jun Bum Lim0c990072017-11-03 20:41:16 +00001//===- CallSiteSplitting.cpp ----------------------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements a transformation that tries to split a call-site to pass
11// more constrained arguments if its argument is predicated in the control flow
12// so that we can expose better context to the later passes (e.g, inliner, jump
13// threading, or IPA-CP based function cloning, etc.).
14// As of now we support two cases :
15//
Florian Hahn7e932892017-12-23 20:02:26 +000016// 1) Try to a split call-site with constrained arguments, if any constraints
17// on any argument can be found by following the single predecessors of the
18// all site's predecessors. Currently this pass only handles call-sites with 2
19// predecessors. For example, in the code below, we try to split the call-site
20// since we can predicate the argument(ptr) based on the OR condition.
Jun Bum Lim0c990072017-11-03 20:41:16 +000021//
22// Split from :
23// if (!ptr || c)
24// callee(ptr);
25// to :
26// if (!ptr)
27// callee(null) // set the known constant value
28// else if (c)
29// callee(nonnull ptr) // set non-null attribute in the argument
30//
31// 2) We can also split a call-site based on constant incoming values of a PHI
32// For example,
33// from :
34// Header:
35// %c = icmp eq i32 %i1, %i2
36// br i1 %c, label %Tail, label %TBB
37// TBB:
38// br label Tail%
39// Tail:
40// %p = phi i32 [ 0, %Header], [ 1, %TBB]
41// call void @bar(i32 %p)
42// to
43// Header:
44// %c = icmp eq i32 %i1, %i2
45// br i1 %c, label %Tail-split0, label %TBB
46// TBB:
47// br label %Tail-split1
48// Tail-split0:
49// call void @bar(i32 0)
50// br label %Tail
51// Tail-split1:
52// call void @bar(i32 1)
53// br label %Tail
54// Tail:
55// %p = phi i32 [ 0, %Tail-split0 ], [ 1, %Tail-split1 ]
56//
57//===----------------------------------------------------------------------===//
58
59#include "llvm/Transforms/Scalar/CallSiteSplitting.h"
60#include "llvm/ADT/Statistic.h"
61#include "llvm/Analysis/TargetLibraryInfo.h"
62#include "llvm/IR/IntrinsicInst.h"
63#include "llvm/IR/PatternMatch.h"
64#include "llvm/Support/Debug.h"
65#include "llvm/Transforms/Scalar.h"
66#include "llvm/Transforms/Utils/BasicBlockUtils.h"
67#include "llvm/Transforms/Utils/Local.h"
68
69using namespace llvm;
70using namespace PatternMatch;
71
72#define DEBUG_TYPE "callsite-splitting"
73
74STATISTIC(NumCallSiteSplit, "Number of call-site split");
75
Florian Hahnc6c89bf2018-01-16 22:13:15 +000076static void addNonNullAttribute(CallSite CS, Value *Op) {
Jun Bum Lim0c990072017-11-03 20:41:16 +000077 unsigned ArgNo = 0;
78 for (auto &I : CS.args()) {
79 if (&*I == Op)
80 CS.addParamAttr(ArgNo, Attribute::NonNull);
81 ++ArgNo;
82 }
83}
84
Florian Hahnc6c89bf2018-01-16 22:13:15 +000085static void setConstantInArgument(CallSite CS, Value *Op,
86 Constant *ConstValue) {
Jun Bum Lim0c990072017-11-03 20:41:16 +000087 unsigned ArgNo = 0;
88 for (auto &I : CS.args()) {
89 if (&*I == Op)
90 CS.setArgument(ArgNo, ConstValue);
91 ++ArgNo;
92 }
93}
94
Florian Hahn2a266a32017-11-18 18:14:13 +000095static bool isCondRelevantToAnyCallArgument(ICmpInst *Cmp, CallSite CS) {
96 assert(isa<Constant>(Cmp->getOperand(1)) && "Expected a constant operand.");
97 Value *Op0 = Cmp->getOperand(0);
98 unsigned ArgNo = 0;
99 for (CallSite::arg_iterator I = CS.arg_begin(), E = CS.arg_end(); I != E;
100 ++I, ++ArgNo) {
101 // Don't consider constant or arguments that are already known non-null.
102 if (isa<Constant>(*I) || CS.paramHasAttr(ArgNo, Attribute::NonNull))
103 continue;
104
105 if (*I == Op0)
106 return true;
107 }
108 return false;
109}
110
Florian Hahnc6c89bf2018-01-16 22:13:15 +0000111typedef std::pair<ICmpInst *, unsigned> ConditionTy;
112typedef SmallVector<ConditionTy, 2> ConditionsTy;
113
Florian Hahnbeda7d52017-12-13 03:05:20 +0000114/// If From has a conditional jump to To, add the condition to Conditions,
115/// if it is relevant to any argument at CS.
Florian Hahnc6c89bf2018-01-16 22:13:15 +0000116static void recordCondition(CallSite CS, BasicBlock *From, BasicBlock *To,
117 ConditionsTy &Conditions) {
Florian Hahnbeda7d52017-12-13 03:05:20 +0000118 auto *BI = dyn_cast<BranchInst>(From->getTerminator());
119 if (!BI || !BI->isConditional())
120 return;
Florian Hahn2a266a32017-11-18 18:14:13 +0000121
Florian Hahnbeda7d52017-12-13 03:05:20 +0000122 CmpInst::Predicate Pred;
123 Value *Cond = BI->getCondition();
124 if (!match(Cond, m_ICmp(Pred, m_Value(), m_Constant())))
125 return;
126
127 ICmpInst *Cmp = cast<ICmpInst>(Cond);
128 if (Pred == ICmpInst::ICMP_EQ || Pred == ICmpInst::ICMP_NE)
129 if (isCondRelevantToAnyCallArgument(Cmp, CS))
130 Conditions.push_back({Cmp, From->getTerminator()->getSuccessor(0) == To
131 ? Pred
132 : Cmp->getInversePredicate()});
Florian Hahn2a266a32017-11-18 18:14:13 +0000133}
134
Florian Hahnbeda7d52017-12-13 03:05:20 +0000135/// Record ICmp conditions relevant to any argument in CS following Pred's
136/// single successors. If there are conflicting conditions along a path, like
137/// x == 1 and x == 0, the first condition will be used.
Florian Hahnc6c89bf2018-01-16 22:13:15 +0000138static void recordConditions(CallSite CS, BasicBlock *Pred,
139 ConditionsTy &Conditions) {
Florian Hahnbeda7d52017-12-13 03:05:20 +0000140 recordCondition(CS, Pred, CS.getInstruction()->getParent(), Conditions);
141 BasicBlock *From = Pred;
142 BasicBlock *To = Pred;
Florian Hahn212afb92018-01-26 10:36:50 +0000143 SmallPtrSet<BasicBlock *, 4> Visited;
Florian Hahnbeda7d52017-12-13 03:05:20 +0000144 while (!Visited.count(From->getSinglePredecessor()) &&
145 (From = From->getSinglePredecessor())) {
146 recordCondition(CS, From, To, Conditions);
Florian Hahn212afb92018-01-26 10:36:50 +0000147 Visited.insert(From);
Florian Hahnbeda7d52017-12-13 03:05:20 +0000148 To = From;
149 }
150}
Jun Bum Lim0c990072017-11-03 20:41:16 +0000151
Florian Hahnc6c89bf2018-01-16 22:13:15 +0000152static void addConditions(CallSite CS, const ConditionsTy &Conditions) {
Florian Hahnbeda7d52017-12-13 03:05:20 +0000153 for (auto &Cond : Conditions) {
154 Value *Arg = Cond.first->getOperand(0);
155 Constant *ConstVal = cast<Constant>(Cond.first->getOperand(1));
156 if (Cond.second == ICmpInst::ICMP_EQ)
Florian Hahnc6c89bf2018-01-16 22:13:15 +0000157 setConstantInArgument(CS, Arg, ConstVal);
Florian Hahnbeda7d52017-12-13 03:05:20 +0000158 else if (ConstVal->getType()->isPointerTy() && ConstVal->isNullValue()) {
159 assert(Cond.second == ICmpInst::ICMP_NE);
Florian Hahnc6c89bf2018-01-16 22:13:15 +0000160 addNonNullAttribute(CS, Arg);
Jun Bum Lim0c990072017-11-03 20:41:16 +0000161 }
162 }
Florian Hahnbeda7d52017-12-13 03:05:20 +0000163}
164
165static SmallVector<BasicBlock *, 2> getTwoPredecessors(BasicBlock *BB) {
166 SmallVector<BasicBlock *, 2> Preds(predecessors((BB)));
167 assert(Preds.size() == 2 && "Expected exactly 2 predecessors!");
168 return Preds;
Jun Bum Lim0c990072017-11-03 20:41:16 +0000169}
170
171static bool canSplitCallSite(CallSite CS) {
172 // FIXME: As of now we handle only CallInst. InvokeInst could be handled
173 // without too much effort.
174 Instruction *Instr = CS.getInstruction();
175 if (!isa<CallInst>(Instr))
176 return false;
177
178 // Allow splitting a call-site only when there is no instruction before the
179 // call-site in the basic block. Based on this constraint, we only clone the
180 // call instruction, and we do not move a call-site across any other
181 // instruction.
182 BasicBlock *CallSiteBB = Instr->getParent();
Mikael Holmen66cf3832017-12-12 07:29:57 +0000183 if (Instr != CallSiteBB->getFirstNonPHIOrDbg())
Jun Bum Lim0c990072017-11-03 20:41:16 +0000184 return false;
185
Florian Hahn2a266a32017-11-18 18:14:13 +0000186 // Need 2 predecessors and cannot split an edge from an IndirectBrInst.
187 SmallVector<BasicBlock *, 2> Preds(predecessors(CallSiteBB));
188 if (Preds.size() != 2 || isa<IndirectBrInst>(Preds[0]->getTerminator()) ||
Jun Bum Lim0c990072017-11-03 20:41:16 +0000189 isa<IndirectBrInst>(Preds[1]->getTerminator()))
190 return false;
191
192 return CallSiteBB->canSplitPredecessors();
193}
194
Florian Hahnc6c89bf2018-01-16 22:13:15 +0000195/// Return true if the CS is split into its new predecessors.
196///
197/// For each (predecessor, conditions from predecessors) pair, it will split the
198/// basic block containing the call site, hook it up to the predecessor and
199/// replace the call instruction with new call instructions, which contain
200/// constraints based on the conditions from their predecessors.
Florian Hahn7e932892017-12-23 20:02:26 +0000201/// For example, in the IR below with an OR condition, the call-site can
Florian Hahnc6c89bf2018-01-16 22:13:15 +0000202/// be split. In this case, Preds for Tail is [(Header, a == null),
203/// (TBB, a != null, b == null)]. Tail is replaced by 2 split blocks, containing
204/// CallInst1, which has constraints based on the conditions from Head and
205/// CallInst2, which has constraints based on the conditions coming from TBB.
Jun Bum Lim0c990072017-11-03 20:41:16 +0000206///
Florian Hahn7e932892017-12-23 20:02:26 +0000207/// From :
Jun Bum Lim0c990072017-11-03 20:41:16 +0000208///
209/// Header:
210/// %c = icmp eq i32* %a, null
211/// br i1 %c %Tail, %TBB
212/// TBB:
213/// %c2 = icmp eq i32* %b, null
214/// br i1 %c %Tail, %End
215/// Tail:
216/// %ca = call i1 @callee (i32* %a, i32* %b)
217///
218/// to :
219///
220/// Header: // PredBB1 is Header
221/// %c = icmp eq i32* %a, null
222/// br i1 %c %Tail-split1, %TBB
223/// TBB: // PredBB2 is TBB
224/// %c2 = icmp eq i32* %b, null
225/// br i1 %c %Tail-split2, %End
226/// Tail-split1:
227/// %ca1 = call @callee (i32* null, i32* %b) // CallInst1
228/// br %Tail
229/// Tail-split2:
230/// %ca2 = call @callee (i32* nonnull %a, i32* null) // CallInst2
231/// br %Tail
232/// Tail:
233/// %p = phi i1 [%ca1, %Tail-split1],[%ca2, %Tail-split2]
234///
Florian Hahn7e932892017-12-23 20:02:26 +0000235/// Note that in case any arguments at the call-site are constrained by its
236/// predecessors, new call-sites with more constrained arguments will be
237/// created in createCallSitesOnPredicatedArgument().
Florian Hahnc6c89bf2018-01-16 22:13:15 +0000238static void splitCallSite(
239 CallSite CS,
240 const SmallVectorImpl<std::pair<BasicBlock *, ConditionsTy>> &Preds) {
Jun Bum Lim0c990072017-11-03 20:41:16 +0000241 Instruction *Instr = CS.getInstruction();
242 BasicBlock *TailBB = Instr->getParent();
Jun Bum Lim0c990072017-11-03 20:41:16 +0000243
Florian Hahnc6c89bf2018-01-16 22:13:15 +0000244 PHINode *CallPN = nullptr;
245 if (Instr->getNumUses())
246 CallPN = PHINode::Create(Instr->getType(), Preds.size(), "phi.call");
Jun Bum Lim0c990072017-11-03 20:41:16 +0000247
Florian Hahnc6c89bf2018-01-16 22:13:15 +0000248 DEBUG(dbgs() << "split call-site : " << *Instr << " into \n");
249 for (const auto &P : Preds) {
250 BasicBlock *PredBB = P.first;
251 BasicBlock *SplitBlock =
252 SplitBlockPredecessors(TailBB, PredBB, ".predBB.split");
253 assert(SplitBlock && "Unexpected new basic block split.");
Jun Bum Lim0c990072017-11-03 20:41:16 +0000254
Florian Hahnc6c89bf2018-01-16 22:13:15 +0000255 Instruction *NewCI = Instr->clone();
256 CallSite NewCS(NewCI);
257 addConditions(NewCS, P.second);
258 NewCI->insertBefore(&*SplitBlock->getFirstInsertionPt());
Jun Bum Lim0c990072017-11-03 20:41:16 +0000259
Florian Hahnc6c89bf2018-01-16 22:13:15 +0000260 // Handle PHIs used as arguments in the call-site.
261 for (PHINode &PN : TailBB->phis()) {
262 unsigned ArgNo = 0;
263 for (auto &CI : CS.args()) {
264 if (&*CI == &PN) {
265 NewCS.setArgument(ArgNo, PN.getIncomingValueForBlock(SplitBlock));
266 }
267 ++ArgNo;
Jun Bum Lim0c990072017-11-03 20:41:16 +0000268 }
Jun Bum Lim0c990072017-11-03 20:41:16 +0000269 }
Florian Hahnc6c89bf2018-01-16 22:13:15 +0000270 DEBUG(dbgs() << " " << *NewCI << " in " << SplitBlock->getName()
271 << "\n");
272 if (CallPN)
273 CallPN->addIncoming(NewCI, SplitBlock);
Jun Bum Lim0c990072017-11-03 20:41:16 +0000274 }
275
276 // Replace users of the original call with a PHI mering call-sites split.
Florian Hahnc6c89bf2018-01-16 22:13:15 +0000277 if (CallPN) {
278 CallPN->insertBefore(TailBB->getFirstNonPHI());
279 Instr->replaceAllUsesWith(CallPN);
Jun Bum Lim0c990072017-11-03 20:41:16 +0000280 }
Florian Hahnc6c89bf2018-01-16 22:13:15 +0000281
Jun Bum Lim0c990072017-11-03 20:41:16 +0000282 Instr->eraseFromParent();
283 NumCallSiteSplit++;
284}
285
Jun Bum Lim0c990072017-11-03 20:41:16 +0000286// Return true if the call-site has an argument which is a PHI with only
287// constant incoming values.
288static bool isPredicatedOnPHI(CallSite CS) {
289 Instruction *Instr = CS.getInstruction();
290 BasicBlock *Parent = Instr->getParent();
Mikael Holmen66cf3832017-12-12 07:29:57 +0000291 if (Instr != Parent->getFirstNonPHIOrDbg())
Jun Bum Lim0c990072017-11-03 20:41:16 +0000292 return false;
293
294 for (auto &BI : *Parent) {
295 if (PHINode *PN = dyn_cast<PHINode>(&BI)) {
296 for (auto &I : CS.args())
297 if (&*I == PN) {
298 assert(PN->getNumIncomingValues() == 2 &&
299 "Unexpected number of incoming values");
300 if (PN->getIncomingBlock(0) == PN->getIncomingBlock(1))
301 return false;
302 if (PN->getIncomingValue(0) == PN->getIncomingValue(1))
303 continue;
304 if (isa<Constant>(PN->getIncomingValue(0)) &&
305 isa<Constant>(PN->getIncomingValue(1)))
306 return true;
307 }
308 }
309 break;
310 }
311 return false;
312}
313
Florian Hahn2a266a32017-11-18 18:14:13 +0000314static bool tryToSplitOnPHIPredicatedArgument(CallSite CS) {
315 if (!isPredicatedOnPHI(CS))
Jun Bum Lim0c990072017-11-03 20:41:16 +0000316 return false;
317
Florian Hahn2a266a32017-11-18 18:14:13 +0000318 auto Preds = getTwoPredecessors(CS.getInstruction()->getParent());
Florian Hahnc6c89bf2018-01-16 22:13:15 +0000319 SmallVector<std::pair<BasicBlock *, ConditionsTy>, 2> PredsCS = {
320 {Preds[0], {}}, {Preds[1], {}}};
321 splitCallSite(CS, PredsCS);
Florian Hahn2a266a32017-11-18 18:14:13 +0000322 return true;
323}
Jun Bum Lim0c990072017-11-03 20:41:16 +0000324
Florian Hahn7e932892017-12-23 20:02:26 +0000325static bool tryToSplitOnPredicatedArgument(CallSite CS) {
Florian Hahn2a266a32017-11-18 18:14:13 +0000326 auto Preds = getTwoPredecessors(CS.getInstruction()->getParent());
Florian Hahn7e932892017-12-23 20:02:26 +0000327 if (Preds[0] == Preds[1])
Florian Hahn2a266a32017-11-18 18:14:13 +0000328 return false;
329
Florian Hahnc6c89bf2018-01-16 22:13:15 +0000330 SmallVector<std::pair<BasicBlock *, ConditionsTy>, 2> PredsCS;
331 for (auto *Pred : make_range(Preds.rbegin(), Preds.rend())) {
332 ConditionsTy Conditions;
333 recordConditions(CS, Pred, Conditions);
334 PredsCS.push_back({Pred, Conditions});
335 }
Florian Hahn2a266a32017-11-18 18:14:13 +0000336
Florian Hahnc6c89bf2018-01-16 22:13:15 +0000337 if (std::all_of(PredsCS.begin(), PredsCS.end(),
338 [](const std::pair<BasicBlock *, ConditionsTy> &P) {
339 return P.second.empty();
340 }))
Florian Hahnbeda7d52017-12-13 03:05:20 +0000341 return false;
342
Florian Hahnc6c89bf2018-01-16 22:13:15 +0000343 splitCallSite(CS, PredsCS);
Jun Bum Lim0c990072017-11-03 20:41:16 +0000344 return true;
345}
346
347static bool tryToSplitCallSite(CallSite CS) {
Florian Hahn2a266a32017-11-18 18:14:13 +0000348 if (!CS.arg_size() || !canSplitCallSite(CS))
Jun Bum Lim0c990072017-11-03 20:41:16 +0000349 return false;
Florian Hahn7e932892017-12-23 20:02:26 +0000350 return tryToSplitOnPredicatedArgument(CS) ||
Florian Hahn2a266a32017-11-18 18:14:13 +0000351 tryToSplitOnPHIPredicatedArgument(CS);
Jun Bum Lim0c990072017-11-03 20:41:16 +0000352}
353
354static bool doCallSiteSplitting(Function &F, TargetLibraryInfo &TLI) {
355 bool Changed = false;
356 for (Function::iterator BI = F.begin(), BE = F.end(); BI != BE;) {
357 BasicBlock &BB = *BI++;
358 for (BasicBlock::iterator II = BB.begin(), IE = BB.end(); II != IE;) {
359 Instruction *I = &*II++;
360 CallSite CS(cast<Value>(I));
361 if (!CS || isa<IntrinsicInst>(I) || isInstructionTriviallyDead(I, &TLI))
362 continue;
363
364 Function *Callee = CS.getCalledFunction();
365 if (!Callee || Callee->isDeclaration())
366 continue;
367 Changed |= tryToSplitCallSite(CS);
368 }
369 }
370 return Changed;
371}
372
373namespace {
374struct CallSiteSplittingLegacyPass : public FunctionPass {
375 static char ID;
376 CallSiteSplittingLegacyPass() : FunctionPass(ID) {
377 initializeCallSiteSplittingLegacyPassPass(*PassRegistry::getPassRegistry());
378 }
379
380 void getAnalysisUsage(AnalysisUsage &AU) const override {
381 AU.addRequired<TargetLibraryInfoWrapperPass>();
382 FunctionPass::getAnalysisUsage(AU);
383 }
384
385 bool runOnFunction(Function &F) override {
386 if (skipFunction(F))
387 return false;
388
389 auto &TLI = getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
390 return doCallSiteSplitting(F, TLI);
391 }
392};
393} // namespace
394
395char CallSiteSplittingLegacyPass::ID = 0;
396INITIALIZE_PASS_BEGIN(CallSiteSplittingLegacyPass, "callsite-splitting",
397 "Call-site splitting", false, false)
398INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
399INITIALIZE_PASS_END(CallSiteSplittingLegacyPass, "callsite-splitting",
400 "Call-site splitting", false, false)
401FunctionPass *llvm::createCallSiteSplittingPass() {
402 return new CallSiteSplittingLegacyPass();
403}
404
405PreservedAnalyses CallSiteSplittingPass::run(Function &F,
406 FunctionAnalysisManager &AM) {
407 auto &TLI = AM.getResult<TargetLibraryAnalysis>(F);
408
409 if (!doCallSiteSplitting(F, TLI))
410 return PreservedAnalyses::all();
411 PreservedAnalyses PA;
412 return PA;
413}