blob: 3243731f07db03f8b0f114e61c06fc66d8b936db [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;
143 SmallPtrSet<BasicBlock *, 4> Visited = {From};
144 while (!Visited.count(From->getSinglePredecessor()) &&
145 (From = From->getSinglePredecessor())) {
146 recordCondition(CS, From, To, Conditions);
147 To = From;
148 }
149}
Jun Bum Lim0c990072017-11-03 20:41:16 +0000150
Florian Hahnc6c89bf2018-01-16 22:13:15 +0000151static void addConditions(CallSite CS, const ConditionsTy &Conditions) {
Florian Hahnbeda7d52017-12-13 03:05:20 +0000152 for (auto &Cond : Conditions) {
153 Value *Arg = Cond.first->getOperand(0);
154 Constant *ConstVal = cast<Constant>(Cond.first->getOperand(1));
155 if (Cond.second == ICmpInst::ICMP_EQ)
Florian Hahnc6c89bf2018-01-16 22:13:15 +0000156 setConstantInArgument(CS, Arg, ConstVal);
Florian Hahnbeda7d52017-12-13 03:05:20 +0000157 else if (ConstVal->getType()->isPointerTy() && ConstVal->isNullValue()) {
158 assert(Cond.second == ICmpInst::ICMP_NE);
Florian Hahnc6c89bf2018-01-16 22:13:15 +0000159 addNonNullAttribute(CS, Arg);
Jun Bum Lim0c990072017-11-03 20:41:16 +0000160 }
161 }
Florian Hahnbeda7d52017-12-13 03:05:20 +0000162}
163
164static SmallVector<BasicBlock *, 2> getTwoPredecessors(BasicBlock *BB) {
165 SmallVector<BasicBlock *, 2> Preds(predecessors((BB)));
166 assert(Preds.size() == 2 && "Expected exactly 2 predecessors!");
167 return Preds;
Jun Bum Lim0c990072017-11-03 20:41:16 +0000168}
169
170static bool canSplitCallSite(CallSite CS) {
171 // FIXME: As of now we handle only CallInst. InvokeInst could be handled
172 // without too much effort.
173 Instruction *Instr = CS.getInstruction();
174 if (!isa<CallInst>(Instr))
175 return false;
176
177 // Allow splitting a call-site only when there is no instruction before the
178 // call-site in the basic block. Based on this constraint, we only clone the
179 // call instruction, and we do not move a call-site across any other
180 // instruction.
181 BasicBlock *CallSiteBB = Instr->getParent();
Mikael Holmen66cf3832017-12-12 07:29:57 +0000182 if (Instr != CallSiteBB->getFirstNonPHIOrDbg())
Jun Bum Lim0c990072017-11-03 20:41:16 +0000183 return false;
184
Florian Hahn2a266a32017-11-18 18:14:13 +0000185 // Need 2 predecessors and cannot split an edge from an IndirectBrInst.
186 SmallVector<BasicBlock *, 2> Preds(predecessors(CallSiteBB));
187 if (Preds.size() != 2 || isa<IndirectBrInst>(Preds[0]->getTerminator()) ||
Jun Bum Lim0c990072017-11-03 20:41:16 +0000188 isa<IndirectBrInst>(Preds[1]->getTerminator()))
189 return false;
190
191 return CallSiteBB->canSplitPredecessors();
192}
193
Florian Hahnc6c89bf2018-01-16 22:13:15 +0000194/// Return true if the CS is split into its new predecessors.
195///
196/// For each (predecessor, conditions from predecessors) pair, it will split the
197/// basic block containing the call site, hook it up to the predecessor and
198/// replace the call instruction with new call instructions, which contain
199/// constraints based on the conditions from their predecessors.
Florian Hahn7e932892017-12-23 20:02:26 +0000200/// For example, in the IR below with an OR condition, the call-site can
Florian Hahnc6c89bf2018-01-16 22:13:15 +0000201/// be split. In this case, Preds for Tail is [(Header, a == null),
202/// (TBB, a != null, b == null)]. Tail is replaced by 2 split blocks, containing
203/// CallInst1, which has constraints based on the conditions from Head and
204/// CallInst2, which has constraints based on the conditions coming from TBB.
Jun Bum Lim0c990072017-11-03 20:41:16 +0000205///
Florian Hahn7e932892017-12-23 20:02:26 +0000206/// From :
Jun Bum Lim0c990072017-11-03 20:41:16 +0000207///
208/// Header:
209/// %c = icmp eq i32* %a, null
210/// br i1 %c %Tail, %TBB
211/// TBB:
212/// %c2 = icmp eq i32* %b, null
213/// br i1 %c %Tail, %End
214/// Tail:
215/// %ca = call i1 @callee (i32* %a, i32* %b)
216///
217/// to :
218///
219/// Header: // PredBB1 is Header
220/// %c = icmp eq i32* %a, null
221/// br i1 %c %Tail-split1, %TBB
222/// TBB: // PredBB2 is TBB
223/// %c2 = icmp eq i32* %b, null
224/// br i1 %c %Tail-split2, %End
225/// Tail-split1:
226/// %ca1 = call @callee (i32* null, i32* %b) // CallInst1
227/// br %Tail
228/// Tail-split2:
229/// %ca2 = call @callee (i32* nonnull %a, i32* null) // CallInst2
230/// br %Tail
231/// Tail:
232/// %p = phi i1 [%ca1, %Tail-split1],[%ca2, %Tail-split2]
233///
Florian Hahn7e932892017-12-23 20:02:26 +0000234/// Note that in case any arguments at the call-site are constrained by its
235/// predecessors, new call-sites with more constrained arguments will be
236/// created in createCallSitesOnPredicatedArgument().
Florian Hahnc6c89bf2018-01-16 22:13:15 +0000237static void splitCallSite(
238 CallSite CS,
239 const SmallVectorImpl<std::pair<BasicBlock *, ConditionsTy>> &Preds) {
Jun Bum Lim0c990072017-11-03 20:41:16 +0000240 Instruction *Instr = CS.getInstruction();
241 BasicBlock *TailBB = Instr->getParent();
Jun Bum Lim0c990072017-11-03 20:41:16 +0000242
Florian Hahnc6c89bf2018-01-16 22:13:15 +0000243 PHINode *CallPN = nullptr;
244 if (Instr->getNumUses())
245 CallPN = PHINode::Create(Instr->getType(), Preds.size(), "phi.call");
Jun Bum Lim0c990072017-11-03 20:41:16 +0000246
Florian Hahnc6c89bf2018-01-16 22:13:15 +0000247 DEBUG(dbgs() << "split call-site : " << *Instr << " into \n");
248 for (const auto &P : Preds) {
249 BasicBlock *PredBB = P.first;
250 BasicBlock *SplitBlock =
251 SplitBlockPredecessors(TailBB, PredBB, ".predBB.split");
252 assert(SplitBlock && "Unexpected new basic block split.");
Jun Bum Lim0c990072017-11-03 20:41:16 +0000253
Florian Hahnc6c89bf2018-01-16 22:13:15 +0000254 Instruction *NewCI = Instr->clone();
255 CallSite NewCS(NewCI);
256 addConditions(NewCS, P.second);
257 NewCI->insertBefore(&*SplitBlock->getFirstInsertionPt());
Jun Bum Lim0c990072017-11-03 20:41:16 +0000258
Florian Hahnc6c89bf2018-01-16 22:13:15 +0000259 // Handle PHIs used as arguments in the call-site.
260 for (PHINode &PN : TailBB->phis()) {
261 unsigned ArgNo = 0;
262 for (auto &CI : CS.args()) {
263 if (&*CI == &PN) {
264 NewCS.setArgument(ArgNo, PN.getIncomingValueForBlock(SplitBlock));
265 }
266 ++ArgNo;
Jun Bum Lim0c990072017-11-03 20:41:16 +0000267 }
Jun Bum Lim0c990072017-11-03 20:41:16 +0000268 }
Florian Hahnc6c89bf2018-01-16 22:13:15 +0000269 DEBUG(dbgs() << " " << *NewCI << " in " << SplitBlock->getName()
270 << "\n");
271 if (CallPN)
272 CallPN->addIncoming(NewCI, SplitBlock);
Jun Bum Lim0c990072017-11-03 20:41:16 +0000273 }
274
275 // Replace users of the original call with a PHI mering call-sites split.
Florian Hahnc6c89bf2018-01-16 22:13:15 +0000276 if (CallPN) {
277 CallPN->insertBefore(TailBB->getFirstNonPHI());
278 Instr->replaceAllUsesWith(CallPN);
Jun Bum Lim0c990072017-11-03 20:41:16 +0000279 }
Florian Hahnc6c89bf2018-01-16 22:13:15 +0000280
Jun Bum Lim0c990072017-11-03 20:41:16 +0000281 Instr->eraseFromParent();
282 NumCallSiteSplit++;
283}
284
Jun Bum Lim0c990072017-11-03 20:41:16 +0000285// Return true if the call-site has an argument which is a PHI with only
286// constant incoming values.
287static bool isPredicatedOnPHI(CallSite CS) {
288 Instruction *Instr = CS.getInstruction();
289 BasicBlock *Parent = Instr->getParent();
Mikael Holmen66cf3832017-12-12 07:29:57 +0000290 if (Instr != Parent->getFirstNonPHIOrDbg())
Jun Bum Lim0c990072017-11-03 20:41:16 +0000291 return false;
292
293 for (auto &BI : *Parent) {
294 if (PHINode *PN = dyn_cast<PHINode>(&BI)) {
295 for (auto &I : CS.args())
296 if (&*I == PN) {
297 assert(PN->getNumIncomingValues() == 2 &&
298 "Unexpected number of incoming values");
299 if (PN->getIncomingBlock(0) == PN->getIncomingBlock(1))
300 return false;
301 if (PN->getIncomingValue(0) == PN->getIncomingValue(1))
302 continue;
303 if (isa<Constant>(PN->getIncomingValue(0)) &&
304 isa<Constant>(PN->getIncomingValue(1)))
305 return true;
306 }
307 }
308 break;
309 }
310 return false;
311}
312
Florian Hahn2a266a32017-11-18 18:14:13 +0000313static bool tryToSplitOnPHIPredicatedArgument(CallSite CS) {
314 if (!isPredicatedOnPHI(CS))
Jun Bum Lim0c990072017-11-03 20:41:16 +0000315 return false;
316
Florian Hahn2a266a32017-11-18 18:14:13 +0000317 auto Preds = getTwoPredecessors(CS.getInstruction()->getParent());
Florian Hahnc6c89bf2018-01-16 22:13:15 +0000318 SmallVector<std::pair<BasicBlock *, ConditionsTy>, 2> PredsCS = {
319 {Preds[0], {}}, {Preds[1], {}}};
320 splitCallSite(CS, PredsCS);
Florian Hahn2a266a32017-11-18 18:14:13 +0000321 return true;
322}
Jun Bum Lim0c990072017-11-03 20:41:16 +0000323
Florian Hahn7e932892017-12-23 20:02:26 +0000324static bool tryToSplitOnPredicatedArgument(CallSite CS) {
Florian Hahn2a266a32017-11-18 18:14:13 +0000325 auto Preds = getTwoPredecessors(CS.getInstruction()->getParent());
Florian Hahn7e932892017-12-23 20:02:26 +0000326 if (Preds[0] == Preds[1])
Florian Hahn2a266a32017-11-18 18:14:13 +0000327 return false;
328
Florian Hahnc6c89bf2018-01-16 22:13:15 +0000329 SmallVector<std::pair<BasicBlock *, ConditionsTy>, 2> PredsCS;
330 for (auto *Pred : make_range(Preds.rbegin(), Preds.rend())) {
331 ConditionsTy Conditions;
332 recordConditions(CS, Pred, Conditions);
333 PredsCS.push_back({Pred, Conditions});
334 }
Florian Hahn2a266a32017-11-18 18:14:13 +0000335
Florian Hahnc6c89bf2018-01-16 22:13:15 +0000336 if (std::all_of(PredsCS.begin(), PredsCS.end(),
337 [](const std::pair<BasicBlock *, ConditionsTy> &P) {
338 return P.second.empty();
339 }))
Florian Hahnbeda7d52017-12-13 03:05:20 +0000340 return false;
341
Florian Hahnc6c89bf2018-01-16 22:13:15 +0000342 splitCallSite(CS, PredsCS);
Jun Bum Lim0c990072017-11-03 20:41:16 +0000343 return true;
344}
345
346static bool tryToSplitCallSite(CallSite CS) {
Florian Hahn2a266a32017-11-18 18:14:13 +0000347 if (!CS.arg_size() || !canSplitCallSite(CS))
Jun Bum Lim0c990072017-11-03 20:41:16 +0000348 return false;
Florian Hahn7e932892017-12-23 20:02:26 +0000349 return tryToSplitOnPredicatedArgument(CS) ||
Florian Hahn2a266a32017-11-18 18:14:13 +0000350 tryToSplitOnPHIPredicatedArgument(CS);
Jun Bum Lim0c990072017-11-03 20:41:16 +0000351}
352
353static bool doCallSiteSplitting(Function &F, TargetLibraryInfo &TLI) {
354 bool Changed = false;
355 for (Function::iterator BI = F.begin(), BE = F.end(); BI != BE;) {
356 BasicBlock &BB = *BI++;
357 for (BasicBlock::iterator II = BB.begin(), IE = BB.end(); II != IE;) {
358 Instruction *I = &*II++;
359 CallSite CS(cast<Value>(I));
360 if (!CS || isa<IntrinsicInst>(I) || isInstructionTriviallyDead(I, &TLI))
361 continue;
362
363 Function *Callee = CS.getCalledFunction();
364 if (!Callee || Callee->isDeclaration())
365 continue;
366 Changed |= tryToSplitCallSite(CS);
367 }
368 }
369 return Changed;
370}
371
372namespace {
373struct CallSiteSplittingLegacyPass : public FunctionPass {
374 static char ID;
375 CallSiteSplittingLegacyPass() : FunctionPass(ID) {
376 initializeCallSiteSplittingLegacyPassPass(*PassRegistry::getPassRegistry());
377 }
378
379 void getAnalysisUsage(AnalysisUsage &AU) const override {
380 AU.addRequired<TargetLibraryInfoWrapperPass>();
381 FunctionPass::getAnalysisUsage(AU);
382 }
383
384 bool runOnFunction(Function &F) override {
385 if (skipFunction(F))
386 return false;
387
388 auto &TLI = getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
389 return doCallSiteSplitting(F, TLI);
390 }
391};
392} // namespace
393
394char CallSiteSplittingLegacyPass::ID = 0;
395INITIALIZE_PASS_BEGIN(CallSiteSplittingLegacyPass, "callsite-splitting",
396 "Call-site splitting", false, false)
397INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
398INITIALIZE_PASS_END(CallSiteSplittingLegacyPass, "callsite-splitting",
399 "Call-site splitting", false, false)
400FunctionPass *llvm::createCallSiteSplittingPass() {
401 return new CallSiteSplittingLegacyPass();
402}
403
404PreservedAnalyses CallSiteSplittingPass::run(Function &F,
405 FunctionAnalysisManager &AM) {
406 auto &TLI = AM.getResult<TargetLibraryAnalysis>(F);
407
408 if (!doCallSiteSplitting(F, TLI))
409 return PreservedAnalyses::all();
410 PreservedAnalyses PA;
411 return PA;
412}