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(CallSite CS, Value *Op) {
77   unsigned ArgNo = 0;
78   for (auto &I : CS.args()) {
79     if (&*I == Op)
80       CS.addParamAttr(ArgNo, Attribute::NonNull);
81     ++ArgNo;
82   }
83 }
84 
85 static void setConstantInArgument(CallSite CS, Value *Op,
86                                   Constant *ConstValue) {
87   unsigned ArgNo = 0;
88   for (auto &I : CS.args()) {
89     if (&*I == Op)
90       CS.setArgument(ArgNo, ConstValue);
91     ++ArgNo;
92   }
93 }
94 
95 static 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 
111 typedef std::pair<ICmpInst *, unsigned> ConditionTy;
112 typedef SmallVector<ConditionTy, 2> ConditionsTy;
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 recordCondition(CallSite CS, BasicBlock *From, BasicBlock *To,
117                             ConditionsTy &Conditions) {
118   auto *BI = dyn_cast<BranchInst>(From->getTerminator());
119   if (!BI || !BI->isConditional())
120     return;
121 
122   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()});
133 }
134 
135 /// 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.
138 static void recordConditions(CallSite CS, BasicBlock *Pred,
139                              ConditionsTy &Conditions) {
140   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 }
150 
151 static void addConditions(CallSite CS, const ConditionsTy &Conditions) {
152   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)
156       setConstantInArgument(CS, Arg, ConstVal);
157     else if (ConstVal->getType()->isPointerTy() && ConstVal->isNullValue()) {
158       assert(Cond.second == ICmpInst::ICMP_NE);
159       addNonNullAttribute(CS, Arg);
160     }
161   }
162 }
163 
164 static 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;
168 }
169 
170 static 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();
182   if (Instr != CallSiteBB->getFirstNonPHIOrDbg())
183     return false;
184 
185   // 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()) ||
188       isa<IndirectBrInst>(Preds[1]->getTerminator()))
189     return false;
190 
191   return CallSiteBB->canSplitPredecessors();
192 }
193 
194 /// 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.
200 /// For example, in the IR below with an OR condition, the call-site can
201 /// 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.
205 ///
206 /// From :
207 ///
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 ///
234 /// 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().
237 static void splitCallSite(
238     CallSite CS,
239     const SmallVectorImpl<std::pair<BasicBlock *, ConditionsTy>> &Preds) {
240   Instruction *Instr = CS.getInstruction();
241   BasicBlock *TailBB = Instr->getParent();
242 
243   PHINode *CallPN = nullptr;
244   if (Instr->getNumUses())
245     CallPN = PHINode::Create(Instr->getType(), Preds.size(), "phi.call");
246 
247   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.");
253 
254     Instruction *NewCI = Instr->clone();
255     CallSite NewCS(NewCI);
256     addConditions(NewCS, P.second);
257     NewCI->insertBefore(&*SplitBlock->getFirstInsertionPt());
258 
259     // 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;
267       }
268     }
269     DEBUG(dbgs() << "    " << *NewCI << " in " << SplitBlock->getName()
270                  << "\n");
271     if (CallPN)
272       CallPN->addIncoming(NewCI, SplitBlock);
273   }
274 
275   // Replace users of the original call with a PHI mering call-sites split.
276   if (CallPN) {
277     CallPN->insertBefore(TailBB->getFirstNonPHI());
278     Instr->replaceAllUsesWith(CallPN);
279   }
280 
281   Instr->eraseFromParent();
282   NumCallSiteSplit++;
283 }
284 
285 // Return true if the call-site has an argument which is a PHI with only
286 // constant incoming values.
287 static bool isPredicatedOnPHI(CallSite CS) {
288   Instruction *Instr = CS.getInstruction();
289   BasicBlock *Parent = Instr->getParent();
290   if (Instr != Parent->getFirstNonPHIOrDbg())
291     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 
313 static bool tryToSplitOnPHIPredicatedArgument(CallSite CS) {
314   if (!isPredicatedOnPHI(CS))
315     return false;
316 
317   auto Preds = getTwoPredecessors(CS.getInstruction()->getParent());
318   SmallVector<std::pair<BasicBlock *, ConditionsTy>, 2> PredsCS = {
319       {Preds[0], {}}, {Preds[1], {}}};
320   splitCallSite(CS, PredsCS);
321   return true;
322 }
323 
324 static bool tryToSplitOnPredicatedArgument(CallSite CS) {
325   auto Preds = getTwoPredecessors(CS.getInstruction()->getParent());
326   if (Preds[0] == Preds[1])
327     return false;
328 
329   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   }
335 
336   if (std::all_of(PredsCS.begin(), PredsCS.end(),
337                   [](const std::pair<BasicBlock *, ConditionsTy> &P) {
338                     return P.second.empty();
339                   }))
340     return false;
341 
342   splitCallSite(CS, PredsCS);
343   return true;
344 }
345 
346 static bool tryToSplitCallSite(CallSite CS) {
347   if (!CS.arg_size() || !canSplitCallSite(CS))
348     return false;
349   return tryToSplitOnPredicatedArgument(CS) ||
350          tryToSplitOnPHIPredicatedArgument(CS);
351 }
352 
353 static 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 
372 namespace {
373 struct 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 
394 char CallSiteSplittingLegacyPass::ID = 0;
395 INITIALIZE_PASS_BEGIN(CallSiteSplittingLegacyPass, "callsite-splitting",
396                       "Call-site splitting", false, false)
397 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
398 INITIALIZE_PASS_END(CallSiteSplittingLegacyPass, "callsite-splitting",
399                     "Call-site splitting", false, false)
400 FunctionPass *llvm::createCallSiteSplittingPass() {
401   return new CallSiteSplittingLegacyPass();
402 }
403 
404 PreservedAnalyses 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 }
413