1 //===- 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 //
16 // 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.
21 //
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 
69 using namespace llvm;
70 using namespace PatternMatch;
71 
72 #define DEBUG_TYPE "callsite-splitting"
73 
74 STATISTIC(NumCallSiteSplit, "Number of call-site split");
75 
76 static void addNonNullAttribute(Instruction *CallI, Instruction *NewCallI,
77                                 Value *Op) {
78   CallSite CS(NewCallI);
79   unsigned ArgNo = 0;
80   for (auto &I : CS.args()) {
81     if (&*I == Op)
82       CS.addParamAttr(ArgNo, Attribute::NonNull);
83     ++ArgNo;
84   }
85 }
86 
87 static void setConstantInArgument(Instruction *CallI, Instruction *NewCallI,
88                                   Value *Op, Constant *ConstValue) {
89   CallSite CS(NewCallI);
90   unsigned ArgNo = 0;
91   for (auto &I : CS.args()) {
92     if (&*I == Op)
93       CS.setArgument(ArgNo, ConstValue);
94     ++ArgNo;
95   }
96 }
97 
98 static bool isCondRelevantToAnyCallArgument(ICmpInst *Cmp, CallSite CS) {
99   assert(isa<Constant>(Cmp->getOperand(1)) && "Expected a constant operand.");
100   Value *Op0 = Cmp->getOperand(0);
101   unsigned ArgNo = 0;
102   for (CallSite::arg_iterator I = CS.arg_begin(), E = CS.arg_end(); I != E;
103        ++I, ++ArgNo) {
104     // Don't consider constant or arguments that are already known non-null.
105     if (isa<Constant>(*I) || CS.paramHasAttr(ArgNo, Attribute::NonNull))
106       continue;
107 
108     if (*I == Op0)
109       return true;
110   }
111   return false;
112 }
113 
114 /// If From has a conditional jump to To, add the condition to Conditions,
115 /// if it is relevant to any argument at CS.
116 static void
117 recordCondition(const CallSite &CS, BasicBlock *From, BasicBlock *To,
118                 SmallVectorImpl<std::pair<ICmpInst *, unsigned>> &Conditions) {
119   auto *BI = dyn_cast<BranchInst>(From->getTerminator());
120   if (!BI || !BI->isConditional())
121     return;
122 
123   CmpInst::Predicate Pred;
124   Value *Cond = BI->getCondition();
125   if (!match(Cond, m_ICmp(Pred, m_Value(), m_Constant())))
126     return;
127 
128   ICmpInst *Cmp = cast<ICmpInst>(Cond);
129   if (Pred == ICmpInst::ICMP_EQ || Pred == ICmpInst::ICMP_NE)
130     if (isCondRelevantToAnyCallArgument(Cmp, CS))
131       Conditions.push_back({Cmp, From->getTerminator()->getSuccessor(0) == To
132                                      ? Pred
133                                      : Cmp->getInversePredicate()});
134 }
135 
136 /// Record ICmp conditions relevant to any argument in CS following Pred's
137 /// single successors. If there are conflicting conditions along a path, like
138 /// x == 1 and x == 0, the first condition will be used.
139 static void
140 recordConditions(const CallSite &CS, BasicBlock *Pred,
141                  SmallVectorImpl<std::pair<ICmpInst *, unsigned>> &Conditions) {
142   recordCondition(CS, Pred, CS.getInstruction()->getParent(), Conditions);
143   BasicBlock *From = Pred;
144   BasicBlock *To = Pred;
145   SmallPtrSet<BasicBlock *, 4> Visited;
146   while (!Visited.count(From->getSinglePredecessor()) &&
147          (From = From->getSinglePredecessor())) {
148     recordCondition(CS, From, To, Conditions);
149     Visited.insert(From);
150     To = From;
151   }
152 }
153 
154 static Instruction *
155 addConditions(CallSite &CS,
156               SmallVectorImpl<std::pair<ICmpInst *, unsigned>> &Conditions) {
157   if (Conditions.empty())
158     return nullptr;
159 
160   Instruction *NewCI = CS.getInstruction()->clone();
161   for (auto &Cond : Conditions) {
162     Value *Arg = Cond.first->getOperand(0);
163     Constant *ConstVal = cast<Constant>(Cond.first->getOperand(1));
164     if (Cond.second == ICmpInst::ICMP_EQ)
165       setConstantInArgument(CS.getInstruction(), NewCI, Arg, ConstVal);
166     else if (ConstVal->getType()->isPointerTy() && ConstVal->isNullValue()) {
167       assert(Cond.second == ICmpInst::ICMP_NE);
168       addNonNullAttribute(CS.getInstruction(), NewCI, Arg);
169     }
170   }
171   return NewCI;
172 }
173 
174 static SmallVector<BasicBlock *, 2> getTwoPredecessors(BasicBlock *BB) {
175   SmallVector<BasicBlock *, 2> Preds(predecessors((BB)));
176   assert(Preds.size() == 2 && "Expected exactly 2 predecessors!");
177   return Preds;
178 }
179 
180 static bool canSplitCallSite(CallSite CS) {
181   // FIXME: As of now we handle only CallInst. InvokeInst could be handled
182   // without too much effort.
183   Instruction *Instr = CS.getInstruction();
184   if (!isa<CallInst>(Instr))
185     return false;
186 
187   // Allow splitting a call-site only when there is no instruction before the
188   // call-site in the basic block. Based on this constraint, we only clone the
189   // call instruction, and we do not move a call-site across any other
190   // instruction.
191   BasicBlock *CallSiteBB = Instr->getParent();
192   if (Instr != CallSiteBB->getFirstNonPHIOrDbg())
193     return false;
194 
195   // Need 2 predecessors and cannot split an edge from an IndirectBrInst.
196   SmallVector<BasicBlock *, 2> Preds(predecessors(CallSiteBB));
197   if (Preds.size() != 2 || isa<IndirectBrInst>(Preds[0]->getTerminator()) ||
198       isa<IndirectBrInst>(Preds[1]->getTerminator()))
199     return false;
200 
201   return CallSiteBB->canSplitPredecessors();
202 }
203 
204 /// Return true if the CS is split into its new predecessors which are directly
205 /// hooked to each of its original predecessors pointed by PredBB1 and PredBB2.
206 /// CallInst1 and CallInst2 will be the new call-sites placed in the new
207 /// predecessors split for PredBB1 and PredBB2, respectively.
208 /// For example, in the IR below with an OR condition, the call-site can
209 /// be split. Assuming PredBB1=Header and PredBB2=TBB, CallInst1 will be the
210 /// call-site placed between Header and Tail, and CallInst2 will be the
211 /// call-site between TBB and Tail.
212 ///
213 /// From :
214 ///
215 ///   Header:
216 ///     %c = icmp eq i32* %a, null
217 ///     br i1 %c %Tail, %TBB
218 ///   TBB:
219 ///     %c2 = icmp eq i32* %b, null
220 ///     br i1 %c %Tail, %End
221 ///   Tail:
222 ///     %ca = call i1  @callee (i32* %a, i32* %b)
223 ///
224 ///  to :
225 ///
226 ///   Header:                          // PredBB1 is Header
227 ///     %c = icmp eq i32* %a, null
228 ///     br i1 %c %Tail-split1, %TBB
229 ///   TBB:                             // PredBB2 is TBB
230 ///     %c2 = icmp eq i32* %b, null
231 ///     br i1 %c %Tail-split2, %End
232 ///   Tail-split1:
233 ///     %ca1 = call @callee (i32* null, i32* %b)         // CallInst1
234 ///    br %Tail
235 ///   Tail-split2:
236 ///     %ca2 = call @callee (i32* nonnull %a, i32* null) // CallInst2
237 ///    br %Tail
238 ///   Tail:
239 ///    %p = phi i1 [%ca1, %Tail-split1],[%ca2, %Tail-split2]
240 ///
241 /// Note that in case any arguments at the call-site are constrained by its
242 /// predecessors, new call-sites with more constrained arguments will be
243 /// created in createCallSitesOnPredicatedArgument().
244 static void splitCallSite(CallSite CS, BasicBlock *PredBB1, BasicBlock *PredBB2,
245                           Instruction *CallInst1, Instruction *CallInst2) {
246   Instruction *Instr = CS.getInstruction();
247   BasicBlock *TailBB = Instr->getParent();
248   assert(Instr == (TailBB->getFirstNonPHIOrDbg()) && "Unexpected call-site");
249 
250   BasicBlock *SplitBlock1 =
251       SplitBlockPredecessors(TailBB, PredBB1, ".predBB1.split");
252   BasicBlock *SplitBlock2 =
253       SplitBlockPredecessors(TailBB, PredBB2, ".predBB2.split");
254 
255   assert((SplitBlock1 && SplitBlock2) && "Unexpected new basic block split.");
256 
257   if (!CallInst1)
258     CallInst1 = Instr->clone();
259   if (!CallInst2)
260     CallInst2 = Instr->clone();
261 
262   CallInst1->insertBefore(&*SplitBlock1->getFirstInsertionPt());
263   CallInst2->insertBefore(&*SplitBlock2->getFirstInsertionPt());
264 
265   CallSite CS1(CallInst1);
266   CallSite CS2(CallInst2);
267 
268   // Handle PHIs used as arguments in the call-site.
269   for (PHINode &PN : TailBB->phis()) {
270     unsigned ArgNo = 0;
271     for (auto &CI : CS.args()) {
272       if (&*CI == &PN) {
273         CS1.setArgument(ArgNo, PN.getIncomingValueForBlock(SplitBlock1));
274         CS2.setArgument(ArgNo, PN.getIncomingValueForBlock(SplitBlock2));
275       }
276       ++ArgNo;
277     }
278   }
279 
280   // Replace users of the original call with a PHI mering call-sites split.
281   if (Instr->getNumUses()) {
282     PHINode *PN = PHINode::Create(Instr->getType(), 2, "phi.call",
283                                   TailBB->getFirstNonPHI());
284     PN->addIncoming(CallInst1, SplitBlock1);
285     PN->addIncoming(CallInst2, SplitBlock2);
286     Instr->replaceAllUsesWith(PN);
287   }
288   DEBUG(dbgs() << "split call-site : " << *Instr << " into \n");
289   DEBUG(dbgs() << "    " << *CallInst1 << " in " << SplitBlock1->getName()
290                << "\n");
291   DEBUG(dbgs() << "    " << *CallInst2 << " in " << SplitBlock2->getName()
292                << "\n");
293   Instr->eraseFromParent();
294   NumCallSiteSplit++;
295 }
296 
297 // Return true if the call-site has an argument which is a PHI with only
298 // constant incoming values.
299 static bool isPredicatedOnPHI(CallSite CS) {
300   Instruction *Instr = CS.getInstruction();
301   BasicBlock *Parent = Instr->getParent();
302   if (Instr != Parent->getFirstNonPHIOrDbg())
303     return false;
304 
305   for (auto &BI : *Parent) {
306     if (PHINode *PN = dyn_cast<PHINode>(&BI)) {
307       for (auto &I : CS.args())
308         if (&*I == PN) {
309           assert(PN->getNumIncomingValues() == 2 &&
310                  "Unexpected number of incoming values");
311           if (PN->getIncomingBlock(0) == PN->getIncomingBlock(1))
312             return false;
313           if (PN->getIncomingValue(0) == PN->getIncomingValue(1))
314             continue;
315           if (isa<Constant>(PN->getIncomingValue(0)) &&
316               isa<Constant>(PN->getIncomingValue(1)))
317             return true;
318         }
319     }
320     break;
321   }
322   return false;
323 }
324 
325 static bool tryToSplitOnPHIPredicatedArgument(CallSite CS) {
326   if (!isPredicatedOnPHI(CS))
327     return false;
328 
329   auto Preds = getTwoPredecessors(CS.getInstruction()->getParent());
330   splitCallSite(CS, Preds[0], Preds[1], nullptr, nullptr);
331   return true;
332 }
333 
334 static bool tryToSplitOnPredicatedArgument(CallSite CS) {
335   auto Preds = getTwoPredecessors(CS.getInstruction()->getParent());
336   if (Preds[0] == Preds[1])
337     return false;
338 
339   SmallVector<std::pair<ICmpInst *, unsigned>, 2> C1, C2;
340   recordConditions(CS, Preds[0], C1);
341   recordConditions(CS, Preds[1], C2);
342 
343   Instruction *CallInst1 = addConditions(CS, C1);
344   Instruction *CallInst2 = addConditions(CS, C2);
345   if (!CallInst1 && !CallInst2)
346     return false;
347 
348   splitCallSite(CS, Preds[1], Preds[0], CallInst2, CallInst1);
349   return true;
350 }
351 
352 static bool tryToSplitCallSite(CallSite CS) {
353   if (!CS.arg_size() || !canSplitCallSite(CS))
354     return false;
355   return tryToSplitOnPredicatedArgument(CS) ||
356          tryToSplitOnPHIPredicatedArgument(CS);
357 }
358 
359 static bool doCallSiteSplitting(Function &F, TargetLibraryInfo &TLI) {
360   bool Changed = false;
361   for (Function::iterator BI = F.begin(), BE = F.end(); BI != BE;) {
362     BasicBlock &BB = *BI++;
363     for (BasicBlock::iterator II = BB.begin(), IE = BB.end(); II != IE;) {
364       Instruction *I = &*II++;
365       CallSite CS(cast<Value>(I));
366       if (!CS || isa<IntrinsicInst>(I) || isInstructionTriviallyDead(I, &TLI))
367         continue;
368 
369       Function *Callee = CS.getCalledFunction();
370       if (!Callee || Callee->isDeclaration())
371         continue;
372       Changed |= tryToSplitCallSite(CS);
373     }
374   }
375   return Changed;
376 }
377 
378 namespace {
379 struct CallSiteSplittingLegacyPass : public FunctionPass {
380   static char ID;
381   CallSiteSplittingLegacyPass() : FunctionPass(ID) {
382     initializeCallSiteSplittingLegacyPassPass(*PassRegistry::getPassRegistry());
383   }
384 
385   void getAnalysisUsage(AnalysisUsage &AU) const override {
386     AU.addRequired<TargetLibraryInfoWrapperPass>();
387     FunctionPass::getAnalysisUsage(AU);
388   }
389 
390   bool runOnFunction(Function &F) override {
391     if (skipFunction(F))
392       return false;
393 
394     auto &TLI = getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
395     return doCallSiteSplitting(F, TLI);
396   }
397 };
398 } // namespace
399 
400 char CallSiteSplittingLegacyPass::ID = 0;
401 INITIALIZE_PASS_BEGIN(CallSiteSplittingLegacyPass, "callsite-splitting",
402                       "Call-site splitting", false, false)
403 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
404 INITIALIZE_PASS_END(CallSiteSplittingLegacyPass, "callsite-splitting",
405                     "Call-site splitting", false, false)
406 FunctionPass *llvm::createCallSiteSplittingPass() {
407   return new CallSiteSplittingLegacyPass();
408 }
409 
410 PreservedAnalyses CallSiteSplittingPass::run(Function &F,
411                                              FunctionAnalysisManager &AM) {
412   auto &TLI = AM.getResult<TargetLibraryAnalysis>(F);
413 
414   if (!doCallSiteSplitting(F, TLI))
415     return PreservedAnalyses::all();
416   PreservedAnalyses PA;
417   return PA;
418 }
419