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;
144   while (!Visited.count(From->getSinglePredecessor()) &&
145          (From = From->getSinglePredecessor())) {
146     recordCondition(CS, From, To, Conditions);
147     Visited.insert(From);
148     To = From;
149   }
150 }
151 
152 static void addConditions(CallSite CS, const ConditionsTy &Conditions) {
153   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)
157       setConstantInArgument(CS, Arg, ConstVal);
158     else if (ConstVal->getType()->isPointerTy() && ConstVal->isNullValue()) {
159       assert(Cond.second == ICmpInst::ICMP_NE);
160       addNonNullAttribute(CS, Arg);
161     }
162   }
163 }
164 
165 static 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;
169 }
170 
171 static 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();
183   if (Instr != CallSiteBB->getFirstNonPHIOrDbg())
184     return false;
185 
186   // 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()) ||
189       isa<IndirectBrInst>(Preds[1]->getTerminator()))
190     return false;
191 
192   return CallSiteBB->canSplitPredecessors();
193 }
194 
195 /// 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.
201 /// For example, in the IR below with an OR condition, the call-site can
202 /// 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.
206 ///
207 /// From :
208 ///
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 ///
235 /// 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().
238 static void splitCallSite(
239     CallSite CS,
240     const SmallVectorImpl<std::pair<BasicBlock *, ConditionsTy>> &Preds) {
241   Instruction *Instr = CS.getInstruction();
242   BasicBlock *TailBB = Instr->getParent();
243 
244   PHINode *CallPN = nullptr;
245   if (Instr->getNumUses())
246     CallPN = PHINode::Create(Instr->getType(), Preds.size(), "phi.call");
247 
248   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.");
254 
255     Instruction *NewCI = Instr->clone();
256     CallSite NewCS(NewCI);
257     addConditions(NewCS, P.second);
258     NewCI->insertBefore(&*SplitBlock->getFirstInsertionPt());
259 
260     // 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;
268       }
269     }
270     DEBUG(dbgs() << "    " << *NewCI << " in " << SplitBlock->getName()
271                  << "\n");
272     if (CallPN)
273       CallPN->addIncoming(NewCI, SplitBlock);
274   }
275 
276   // Replace users of the original call with a PHI mering call-sites split.
277   if (CallPN) {
278     CallPN->insertBefore(TailBB->getFirstNonPHI());
279     Instr->replaceAllUsesWith(CallPN);
280   }
281 
282   Instr->eraseFromParent();
283   NumCallSiteSplit++;
284 }
285 
286 // Return true if the call-site has an argument which is a PHI with only
287 // constant incoming values.
288 static bool isPredicatedOnPHI(CallSite CS) {
289   Instruction *Instr = CS.getInstruction();
290   BasicBlock *Parent = Instr->getParent();
291   if (Instr != Parent->getFirstNonPHIOrDbg())
292     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 
314 static bool tryToSplitOnPHIPredicatedArgument(CallSite CS) {
315   if (!isPredicatedOnPHI(CS))
316     return false;
317 
318   auto Preds = getTwoPredecessors(CS.getInstruction()->getParent());
319   SmallVector<std::pair<BasicBlock *, ConditionsTy>, 2> PredsCS = {
320       {Preds[0], {}}, {Preds[1], {}}};
321   splitCallSite(CS, PredsCS);
322   return true;
323 }
324 
325 static bool tryToSplitOnPredicatedArgument(CallSite CS) {
326   auto Preds = getTwoPredecessors(CS.getInstruction()->getParent());
327   if (Preds[0] == Preds[1])
328     return false;
329 
330   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   }
336 
337   if (std::all_of(PredsCS.begin(), PredsCS.end(),
338                   [](const std::pair<BasicBlock *, ConditionsTy> &P) {
339                     return P.second.empty();
340                   }))
341     return false;
342 
343   splitCallSite(CS, PredsCS);
344   return true;
345 }
346 
347 static bool tryToSplitCallSite(CallSite CS) {
348   if (!CS.arg_size() || !canSplitCallSite(CS))
349     return false;
350   return tryToSplitOnPredicatedArgument(CS) ||
351          tryToSplitOnPHIPredicatedArgument(CS);
352 }
353 
354 static 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 
373 namespace {
374 struct 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 
395 char CallSiteSplittingLegacyPass::ID = 0;
396 INITIALIZE_PASS_BEGIN(CallSiteSplittingLegacyPass, "callsite-splitting",
397                       "Call-site splitting", false, false)
398 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
399 INITIALIZE_PASS_END(CallSiteSplittingLegacyPass, "callsite-splitting",
400                     "Call-site splitting", false, false)
401 FunctionPass *llvm::createCallSiteSplittingPass() {
402   return new CallSiteSplittingLegacyPass();
403 }
404 
405 PreservedAnalyses 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 }
414