1 //===- CallSiteSplitting.cpp ----------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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 :
16 // 1) If a call site is dominated by an OR condition and if any of its arguments
17 // are predicated on this OR condition, try to split the condition with more
18 // constrained arguments. For example, in the code below, we try to split the
19 // call site since we can predicate the argument(ptr) based on the OR condition.
26 // callee(null) // set the known constant value
28 // callee(nonnull ptr) // set non-null attribute in the argument
30 // 2) We can also split a call-site based on constant incoming values of a PHI
34 // %c = icmp eq i32 %i1, %i2
35 // br i1 %c, label %Tail, label %TBB
39 // %p = phi i32 [ 0, %Header], [ 1, %TBB]
40 // call void @bar(i32 %p)
43 // %c = icmp eq i32 %i1, %i2
44 // br i1 %c, label %Tail-split0, label %TBB
46 // br label %Tail-split1
48 // call void @bar(i32 0)
51 // call void @bar(i32 1)
54 // %p = phi i32 [ 0, %Tail-split0 ], [ 1, %Tail-split1 ]
56 //===----------------------------------------------------------------------===//
58 #include "llvm/Transforms/Scalar/CallSiteSplitting.h"
59 #include "llvm/ADT/Statistic.h"
60 #include "llvm/Analysis/TargetLibraryInfo.h"
61 #include "llvm/IR/IntrinsicInst.h"
62 #include "llvm/IR/PatternMatch.h"
63 #include "llvm/Support/Debug.h"
64 #include "llvm/Transforms/Scalar.h"
65 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
66 #include "llvm/Transforms/Utils/Local.h"
69 using namespace PatternMatch;
71 #define DEBUG_TYPE "callsite-splitting"
73 STATISTIC(NumCallSiteSplit, "Number of call-site split");
75 static void addNonNullAttribute(Instruction *CallI, Instruction *&NewCallI,
78 NewCallI = CallI->clone();
79 CallSite CS(NewCallI);
81 for (auto &I : CS.args()) {
83 CS.addParamAttr(ArgNo, Attribute::NonNull);
88 static void setConstantInArgument(Instruction *CallI, Instruction *&NewCallI,
89 Value *Op, Constant *ConstValue) {
91 NewCallI = CallI->clone();
92 CallSite CS(NewCallI);
94 for (auto &I : CS.args()) {
96 CS.setArgument(ArgNo, ConstValue);
101 static bool createCallSitesOnOrPredicatedArgument(
102 CallSite CS, Instruction *&NewCSTakenFromHeader,
103 Instruction *&NewCSTakenFromNextCond,
104 SmallVectorImpl<BranchInst *> &BranchInsts, BasicBlock *HeaderBB) {
105 assert(BranchInsts.size() <= 2 &&
106 "Unexpected number of blocks in the OR predicated condition");
107 Instruction *Instr = CS.getInstruction();
108 BasicBlock *CallSiteBB = Instr->getParent();
109 TerminatorInst *HeaderTI = HeaderBB->getTerminator();
110 bool IsCSInTakenPath = CallSiteBB == HeaderTI->getSuccessor(0);
112 for (unsigned I = 0, E = BranchInsts.size(); I != E; ++I) {
113 BranchInst *PBI = BranchInsts[I];
114 assert(isa<ICmpInst>(PBI->getCondition()) &&
115 "Unexpected condition in a conditional branch.");
116 ICmpInst *Cmp = cast<ICmpInst>(PBI->getCondition());
117 Value *Arg = Cmp->getOperand(0);
118 assert(isa<Constant>(Cmp->getOperand(1)) &&
119 "Expected op1 to be a constant.");
120 Constant *ConstVal = cast<Constant>(Cmp->getOperand(1));
121 CmpInst::Predicate Pred = Cmp->getPredicate();
123 if (PBI->getParent() == HeaderBB) {
124 Instruction *&CallTakenFromHeader =
125 IsCSInTakenPath ? NewCSTakenFromHeader : NewCSTakenFromNextCond;
126 Instruction *&CallUntakenFromHeader =
127 IsCSInTakenPath ? NewCSTakenFromNextCond : NewCSTakenFromHeader;
129 assert((Pred == ICmpInst::ICMP_EQ || Pred == ICmpInst::ICMP_NE) &&
130 "Unexpected predicate in an OR condition");
132 // Set the constant value for agruments in the call predicated based on
134 Instruction *&CallToSetConst = Pred == ICmpInst::ICMP_EQ
135 ? CallTakenFromHeader
136 : CallUntakenFromHeader;
137 setConstantInArgument(Instr, CallToSetConst, Arg, ConstVal);
139 // Add the NonNull attribute if compared with the null pointer.
140 if (ConstVal->getType()->isPointerTy() && ConstVal->isNullValue()) {
141 Instruction *&CallToSetAttr = Pred == ICmpInst::ICMP_EQ
142 ? CallUntakenFromHeader
143 : CallTakenFromHeader;
144 addNonNullAttribute(Instr, CallToSetAttr, Arg);
149 if (Pred == ICmpInst::ICMP_EQ) {
150 if (PBI->getSuccessor(0) == Instr->getParent()) {
151 // Set the constant value for the call taken from the second block in
153 setConstantInArgument(Instr, NewCSTakenFromNextCond, Arg, ConstVal);
155 // Add the NonNull attribute if compared with the null pointer for the
156 // call taken from the second block in the OR condition.
157 if (ConstVal->getType()->isPointerTy() && ConstVal->isNullValue())
158 addNonNullAttribute(Instr, NewCSTakenFromNextCond, Arg);
161 if (PBI->getSuccessor(0) == Instr->getParent()) {
162 // Add the NonNull attribute if compared with the null pointer for the
163 // call taken from the second block in the OR condition.
164 if (ConstVal->getType()->isPointerTy() && ConstVal->isNullValue())
165 addNonNullAttribute(Instr, NewCSTakenFromNextCond, Arg);
166 } else if (Pred == ICmpInst::ICMP_NE) {
167 // Set the constant value for the call in the untaken path from the
169 setConstantInArgument(Instr, NewCSTakenFromNextCond, Arg, ConstVal);
171 llvm_unreachable("Unexpected condition");
174 return NewCSTakenFromHeader || NewCSTakenFromNextCond;
177 static bool canSplitCallSite(CallSite CS) {
178 // FIXME: As of now we handle only CallInst. InvokeInst could be handled
179 // without too much effort.
180 Instruction *Instr = CS.getInstruction();
181 if (!isa<CallInst>(Instr))
184 // Allow splitting a call-site only when there is no instruction before the
185 // call-site in the basic block. Based on this constraint, we only clone the
186 // call instruction, and we do not move a call-site across any other
188 BasicBlock *CallSiteBB = Instr->getParent();
189 if (Instr != CallSiteBB->getFirstNonPHI())
192 pred_iterator PII = pred_begin(CallSiteBB);
193 pred_iterator PIE = pred_end(CallSiteBB);
194 unsigned NumPreds = std::distance(PII, PIE);
196 // Allow only one extra call-site. No more than two from one call-site.
200 // Cannot split an edge from an IndirectBrInst.
201 BasicBlock *Preds[2] = {*PII++, *PII};
202 if (isa<IndirectBrInst>(Preds[0]->getTerminator()) ||
203 isa<IndirectBrInst>(Preds[1]->getTerminator()))
206 return CallSiteBB->canSplitPredecessors();
209 /// Return true if the CS is split into its new predecessors which are directly
210 /// hooked to each of its orignial predecessors pointed by PredBB1 and PredBB2.
211 /// Note that PredBB1 and PredBB2 are decided in findPredicatedArgument(),
212 /// especially for the OR predicated case where PredBB1 will point the header,
213 /// and PredBB2 will point the the second compare block. CallInst1 and CallInst2
214 /// will be the new call-sites placed in the new predecessors split for PredBB1
215 /// and PredBB2, repectively. Therefore, CallInst1 will be the call-site placed
216 /// between Header and Tail, and CallInst2 will be the call-site between TBB and
217 /// Tail. For example, in the IR below with an OR condition, the call-site can
223 /// %c = icmp eq i32* %a, null
224 /// br i1 %c %Tail, %TBB
226 /// %c2 = icmp eq i32* %b, null
227 /// br i1 %c %Tail, %End
229 /// %ca = call i1 @callee (i32* %a, i32* %b)
233 /// Header: // PredBB1 is Header
234 /// %c = icmp eq i32* %a, null
235 /// br i1 %c %Tail-split1, %TBB
236 /// TBB: // PredBB2 is TBB
237 /// %c2 = icmp eq i32* %b, null
238 /// br i1 %c %Tail-split2, %End
240 /// %ca1 = call @callee (i32* null, i32* %b) // CallInst1
243 /// %ca2 = call @callee (i32* nonnull %a, i32* null) // CallInst2
246 /// %p = phi i1 [%ca1, %Tail-split1],[%ca2, %Tail-split2]
248 /// Note that for an OR predicated case, CallInst1 and CallInst2 should be
249 /// created with more constrained arguments in
250 /// createCallSitesOnOrPredicatedArgument().
251 static void splitCallSite(CallSite CS, BasicBlock *PredBB1, BasicBlock *PredBB2,
252 Instruction *CallInst1, Instruction *CallInst2) {
253 Instruction *Instr = CS.getInstruction();
254 BasicBlock *TailBB = Instr->getParent();
255 assert(Instr == (TailBB->getFirstNonPHI()) && "Unexpected call-site");
257 BasicBlock *SplitBlock1 =
258 SplitBlockPredecessors(TailBB, PredBB1, ".predBB1.split");
259 BasicBlock *SplitBlock2 =
260 SplitBlockPredecessors(TailBB, PredBB2, ".predBB2.split");
262 assert((SplitBlock1 && SplitBlock2) && "Unexpected new basic block split.");
265 CallInst1 = Instr->clone();
267 CallInst2 = Instr->clone();
269 CallInst1->insertBefore(&*SplitBlock1->getFirstInsertionPt());
270 CallInst2->insertBefore(&*SplitBlock2->getFirstInsertionPt());
272 CallSite CS1(CallInst1);
273 CallSite CS2(CallInst2);
275 // Handle PHIs used as arguments in the call-site.
276 for (auto &PI : *TailBB) {
277 PHINode *PN = dyn_cast<PHINode>(&PI);
281 for (auto &CI : CS.args()) {
283 CS1.setArgument(ArgNo, PN->getIncomingValueForBlock(SplitBlock1));
284 CS2.setArgument(ArgNo, PN->getIncomingValueForBlock(SplitBlock2));
290 // Replace users of the original call with a PHI mering call-sites split.
291 if (Instr->getNumUses()) {
292 PHINode *PN = PHINode::Create(Instr->getType(), 2, "phi.call", Instr);
293 PN->addIncoming(CallInst1, SplitBlock1);
294 PN->addIncoming(CallInst2, SplitBlock2);
295 Instr->replaceAllUsesWith(PN);
297 DEBUG(dbgs() << "split call-site : " << *Instr << " into \n");
298 DEBUG(dbgs() << " " << *CallInst1 << " in " << SplitBlock1->getName()
300 DEBUG(dbgs() << " " << *CallInst2 << " in " << SplitBlock2->getName()
302 Instr->eraseFromParent();
306 static bool isCondRelevantToAnyCallArgument(ICmpInst *Cmp, CallSite CS) {
307 assert(isa<Constant>(Cmp->getOperand(1)) && "Expected a constant operand.");
308 Value *Op0 = Cmp->getOperand(0);
310 for (CallSite::arg_iterator I = CS.arg_begin(), E = CS.arg_end(); I != E;
312 // Don't consider constant or arguments that are already known non-null.
313 if (isa<Constant>(*I) || CS.paramHasAttr(ArgNo, Attribute::NonNull))
322 static void findOrCondRelevantToCallArgument(
323 CallSite CS, BasicBlock *PredBB, BasicBlock *OtherPredBB,
324 SmallVectorImpl<BranchInst *> &BranchInsts, BasicBlock *&HeaderBB) {
325 auto *PBI = dyn_cast<BranchInst>(PredBB->getTerminator());
326 if (!PBI || !PBI->isConditional())
329 if (PBI->getSuccessor(0) == OtherPredBB ||
330 PBI->getSuccessor(1) == OtherPredBB)
331 if (PredBB == OtherPredBB->getSinglePredecessor()) {
332 assert(!HeaderBB && "Expect to find only a single header block");
336 CmpInst::Predicate Pred;
337 Value *Cond = PBI->getCondition();
338 if (!match(Cond, m_ICmp(Pred, m_Value(), m_Constant())))
340 ICmpInst *Cmp = cast<ICmpInst>(Cond);
341 if (Pred == ICmpInst::ICMP_EQ || Pred == ICmpInst::ICMP_NE)
342 if (isCondRelevantToAnyCallArgument(Cmp, CS))
343 BranchInsts.push_back(PBI);
346 // Return true if the call-site has an argument which is a PHI with only
347 // constant incoming values.
348 static bool isPredicatedOnPHI(CallSite CS) {
349 Instruction *Instr = CS.getInstruction();
350 BasicBlock *Parent = Instr->getParent();
351 if (Instr != Parent->getFirstNonPHI())
354 for (auto &BI : *Parent) {
355 if (PHINode *PN = dyn_cast<PHINode>(&BI)) {
356 for (auto &I : CS.args())
358 assert(PN->getNumIncomingValues() == 2 &&
359 "Unexpected number of incoming values");
360 if (PN->getIncomingBlock(0) == PN->getIncomingBlock(1))
362 if (PN->getIncomingValue(0) == PN->getIncomingValue(1))
364 if (isa<Constant>(PN->getIncomingValue(0)) &&
365 isa<Constant>(PN->getIncomingValue(1)))
374 // Return true if an agument in CS is predicated on an 'or' condition.
375 // Create new call-site with arguments constrained based on the OR condition.
376 static bool findPredicatedOnOrCondition(CallSite CS, BasicBlock *PredBB1,
378 Instruction *&NewCallTakenFromHeader,
379 Instruction *&NewCallTakenFromNextCond,
380 BasicBlock *&HeaderBB) {
381 SmallVector<BranchInst *, 4> BranchInsts;
382 findOrCondRelevantToCallArgument(CS, PredBB1, PredBB2, BranchInsts, HeaderBB);
383 findOrCondRelevantToCallArgument(CS, PredBB2, PredBB1, BranchInsts, HeaderBB);
384 if (BranchInsts.empty() || !HeaderBB)
387 // If an OR condition is detected, try to create call sites with constrained
388 // arguments (e.g., NonNull attribute or constant value).
389 return createCallSitesOnOrPredicatedArgument(CS, NewCallTakenFromHeader,
390 NewCallTakenFromNextCond,
391 BranchInsts, HeaderBB);
394 static bool findPredicatedArgument(CallSite CS, Instruction *&CallInst1,
395 Instruction *&CallInst2,
396 BasicBlock *&PredBB1, BasicBlock *&PredBB2) {
397 BasicBlock *CallSiteBB = CS.getInstruction()->getParent();
398 pred_iterator PII = pred_begin(CallSiteBB);
399 pred_iterator PIE = pred_end(CallSiteBB);
400 assert(std::distance(PII, PIE) == 2 && "Expect only two predecessors.");
402 BasicBlock *Preds[2] = {*PII++, *PII};
403 BasicBlock *&HeaderBB = PredBB1;
404 if (!findPredicatedOnOrCondition(CS, Preds[0], Preds[1], CallInst1, CallInst2,
406 !isPredicatedOnPHI(CS))
412 PredBB2 = PredBB1 == Preds[0] ? Preds[1] : Preds[0];
416 static bool tryToSplitCallSite(CallSite CS) {
420 BasicBlock *PredBB1 = nullptr;
421 BasicBlock *PredBB2 = nullptr;
422 Instruction *CallInst1 = nullptr;
423 Instruction *CallInst2 = nullptr;
424 if (!canSplitCallSite(CS) ||
425 !findPredicatedArgument(CS, CallInst1, CallInst2, PredBB1, PredBB2)) {
426 assert(!CallInst1 && !CallInst2 && "Unexpected new call-sites cloned.");
429 splitCallSite(CS, PredBB1, PredBB2, CallInst1, CallInst2);
433 static bool doCallSiteSplitting(Function &F, TargetLibraryInfo &TLI) {
434 bool Changed = false;
435 for (Function::iterator BI = F.begin(), BE = F.end(); BI != BE;) {
436 BasicBlock &BB = *BI++;
437 for (BasicBlock::iterator II = BB.begin(), IE = BB.end(); II != IE;) {
438 Instruction *I = &*II++;
439 CallSite CS(cast<Value>(I));
440 if (!CS || isa<IntrinsicInst>(I) || isInstructionTriviallyDead(I, &TLI))
443 Function *Callee = CS.getCalledFunction();
444 if (!Callee || Callee->isDeclaration())
446 Changed |= tryToSplitCallSite(CS);
453 struct CallSiteSplittingLegacyPass : public FunctionPass {
455 CallSiteSplittingLegacyPass() : FunctionPass(ID) {
456 initializeCallSiteSplittingLegacyPassPass(*PassRegistry::getPassRegistry());
459 void getAnalysisUsage(AnalysisUsage &AU) const override {
460 AU.addRequired<TargetLibraryInfoWrapperPass>();
461 FunctionPass::getAnalysisUsage(AU);
464 bool runOnFunction(Function &F) override {
468 auto &TLI = getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
469 return doCallSiteSplitting(F, TLI);
474 char CallSiteSplittingLegacyPass::ID = 0;
475 INITIALIZE_PASS_BEGIN(CallSiteSplittingLegacyPass, "callsite-splitting",
476 "Call-site splitting", false, false)
477 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
478 INITIALIZE_PASS_END(CallSiteSplittingLegacyPass, "callsite-splitting",
479 "Call-site splitting", false, false)
480 FunctionPass *llvm::createCallSiteSplittingPass() {
481 return new CallSiteSplittingLegacyPass();
484 PreservedAnalyses CallSiteSplittingPass::run(Function &F,
485 FunctionAnalysisManager &AM) {
486 auto &TLI = AM.getResult<TargetLibraryAnalysis>(F);
488 if (!doCallSiteSplitting(F, TLI))
489 return PreservedAnalyses::all();
490 PreservedAnalyses PA;