xref: /freebsd-src/contrib/llvm-project/llvm/lib/Transforms/Scalar/CallSiteSplitting.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
10b57cec5SDimitry Andric //===- CallSiteSplitting.cpp ----------------------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This file implements a transformation that tries to split a call-site to pass
100b57cec5SDimitry Andric // more constrained arguments if its argument is predicated in the control flow
110b57cec5SDimitry Andric // so that we can expose better context to the later passes (e.g, inliner, jump
120b57cec5SDimitry Andric // threading, or IPA-CP based function cloning, etc.).
130b57cec5SDimitry Andric // As of now we support two cases :
140b57cec5SDimitry Andric //
150b57cec5SDimitry Andric // 1) Try to a split call-site with constrained arguments, if any constraints
160b57cec5SDimitry Andric // on any argument can be found by following the single predecessors of the
170b57cec5SDimitry Andric // all site's predecessors. Currently this pass only handles call-sites with 2
180b57cec5SDimitry Andric // predecessors. For example, in the code below, we try to split the call-site
190b57cec5SDimitry Andric // since we can predicate the argument(ptr) based on the OR condition.
200b57cec5SDimitry Andric //
210b57cec5SDimitry Andric // Split from :
220b57cec5SDimitry Andric //   if (!ptr || c)
230b57cec5SDimitry Andric //     callee(ptr);
240b57cec5SDimitry Andric // to :
250b57cec5SDimitry Andric //   if (!ptr)
260b57cec5SDimitry Andric //     callee(null)         // set the known constant value
270b57cec5SDimitry Andric //   else if (c)
280b57cec5SDimitry Andric //     callee(nonnull ptr)  // set non-null attribute in the argument
290b57cec5SDimitry Andric //
300b57cec5SDimitry Andric // 2) We can also split a call-site based on constant incoming values of a PHI
310b57cec5SDimitry Andric // For example,
320b57cec5SDimitry Andric // from :
330b57cec5SDimitry Andric //   Header:
340b57cec5SDimitry Andric //    %c = icmp eq i32 %i1, %i2
350b57cec5SDimitry Andric //    br i1 %c, label %Tail, label %TBB
360b57cec5SDimitry Andric //   TBB:
370b57cec5SDimitry Andric //    br label Tail%
380b57cec5SDimitry Andric //   Tail:
390b57cec5SDimitry Andric //    %p = phi i32 [ 0, %Header], [ 1, %TBB]
400b57cec5SDimitry Andric //    call void @bar(i32 %p)
410b57cec5SDimitry Andric // to
420b57cec5SDimitry Andric //   Header:
430b57cec5SDimitry Andric //    %c = icmp eq i32 %i1, %i2
440b57cec5SDimitry Andric //    br i1 %c, label %Tail-split0, label %TBB
450b57cec5SDimitry Andric //   TBB:
460b57cec5SDimitry Andric //    br label %Tail-split1
470b57cec5SDimitry Andric //   Tail-split0:
480b57cec5SDimitry Andric //    call void @bar(i32 0)
490b57cec5SDimitry Andric //    br label %Tail
500b57cec5SDimitry Andric //   Tail-split1:
510b57cec5SDimitry Andric //    call void @bar(i32 1)
520b57cec5SDimitry Andric //    br label %Tail
530b57cec5SDimitry Andric //   Tail:
540b57cec5SDimitry Andric //    %p = phi i32 [ 0, %Tail-split0 ], [ 1, %Tail-split1 ]
550b57cec5SDimitry Andric //
560b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
570b57cec5SDimitry Andric 
580b57cec5SDimitry Andric #include "llvm/Transforms/Scalar/CallSiteSplitting.h"
590b57cec5SDimitry Andric #include "llvm/ADT/Statistic.h"
6081ad6265SDimitry Andric #include "llvm/Analysis/DomTreeUpdater.h"
610b57cec5SDimitry Andric #include "llvm/Analysis/TargetLibraryInfo.h"
620b57cec5SDimitry Andric #include "llvm/Analysis/TargetTransformInfo.h"
630b57cec5SDimitry Andric #include "llvm/IR/IntrinsicInst.h"
640b57cec5SDimitry Andric #include "llvm/IR/PatternMatch.h"
65480093f4SDimitry Andric #include "llvm/Support/CommandLine.h"
660b57cec5SDimitry Andric #include "llvm/Support/Debug.h"
670b57cec5SDimitry Andric #include "llvm/Transforms/Utils/Cloning.h"
68480093f4SDimitry Andric #include "llvm/Transforms/Utils/Local.h"
690b57cec5SDimitry Andric 
700b57cec5SDimitry Andric using namespace llvm;
710b57cec5SDimitry Andric using namespace PatternMatch;
720b57cec5SDimitry Andric 
730b57cec5SDimitry Andric #define DEBUG_TYPE "callsite-splitting"
740b57cec5SDimitry Andric 
750b57cec5SDimitry Andric STATISTIC(NumCallSiteSplit, "Number of call-site split");
760b57cec5SDimitry Andric 
770b57cec5SDimitry Andric /// Only allow instructions before a call, if their CodeSize cost is below
780b57cec5SDimitry Andric /// DuplicationThreshold. Those instructions need to be duplicated in all
790b57cec5SDimitry Andric /// split blocks.
800b57cec5SDimitry Andric static cl::opt<unsigned>
810b57cec5SDimitry Andric     DuplicationThreshold("callsite-splitting-duplication-threshold", cl::Hidden,
820b57cec5SDimitry Andric                          cl::desc("Only allow instructions before a call, if "
830b57cec5SDimitry Andric                                   "their cost is below DuplicationThreshold"),
840b57cec5SDimitry Andric                          cl::init(5));
850b57cec5SDimitry Andric 
865ffd83dbSDimitry Andric static void addNonNullAttribute(CallBase &CB, Value *Op) {
870b57cec5SDimitry Andric   unsigned ArgNo = 0;
885ffd83dbSDimitry Andric   for (auto &I : CB.args()) {
890b57cec5SDimitry Andric     if (&*I == Op)
905ffd83dbSDimitry Andric       CB.addParamAttr(ArgNo, Attribute::NonNull);
910b57cec5SDimitry Andric     ++ArgNo;
920b57cec5SDimitry Andric   }
930b57cec5SDimitry Andric }
940b57cec5SDimitry Andric 
955ffd83dbSDimitry Andric static void setConstantInArgument(CallBase &CB, Value *Op,
960b57cec5SDimitry Andric                                   Constant *ConstValue) {
970b57cec5SDimitry Andric   unsigned ArgNo = 0;
985ffd83dbSDimitry Andric   for (auto &I : CB.args()) {
990b57cec5SDimitry Andric     if (&*I == Op) {
1000b57cec5SDimitry Andric       // It is possible we have already added the non-null attribute to the
1010b57cec5SDimitry Andric       // parameter by using an earlier constraining condition.
1025ffd83dbSDimitry Andric       CB.removeParamAttr(ArgNo, Attribute::NonNull);
1035ffd83dbSDimitry Andric       CB.setArgOperand(ArgNo, ConstValue);
1040b57cec5SDimitry Andric     }
1050b57cec5SDimitry Andric     ++ArgNo;
1060b57cec5SDimitry Andric   }
1070b57cec5SDimitry Andric }
1080b57cec5SDimitry Andric 
1095ffd83dbSDimitry Andric static bool isCondRelevantToAnyCallArgument(ICmpInst *Cmp, CallBase &CB) {
1100b57cec5SDimitry Andric   assert(isa<Constant>(Cmp->getOperand(1)) && "Expected a constant operand.");
1110b57cec5SDimitry Andric   Value *Op0 = Cmp->getOperand(0);
1120b57cec5SDimitry Andric   unsigned ArgNo = 0;
1135ffd83dbSDimitry Andric   for (auto I = CB.arg_begin(), E = CB.arg_end(); I != E; ++I, ++ArgNo) {
1140b57cec5SDimitry Andric     // Don't consider constant or arguments that are already known non-null.
1155ffd83dbSDimitry Andric     if (isa<Constant>(*I) || CB.paramHasAttr(ArgNo, Attribute::NonNull))
1160b57cec5SDimitry Andric       continue;
1170b57cec5SDimitry Andric 
1180b57cec5SDimitry Andric     if (*I == Op0)
1190b57cec5SDimitry Andric       return true;
1200b57cec5SDimitry Andric   }
1210b57cec5SDimitry Andric   return false;
1220b57cec5SDimitry Andric }
1230b57cec5SDimitry Andric 
12481ad6265SDimitry Andric using ConditionTy = std::pair<ICmpInst *, unsigned>;
12581ad6265SDimitry Andric using ConditionsTy = SmallVector<ConditionTy, 2>;
1260b57cec5SDimitry Andric 
1270b57cec5SDimitry Andric /// If From has a conditional jump to To, add the condition to Conditions,
1285ffd83dbSDimitry Andric /// if it is relevant to any argument at CB.
1295ffd83dbSDimitry Andric static void recordCondition(CallBase &CB, BasicBlock *From, BasicBlock *To,
1300b57cec5SDimitry Andric                             ConditionsTy &Conditions) {
1310b57cec5SDimitry Andric   auto *BI = dyn_cast<BranchInst>(From->getTerminator());
1320b57cec5SDimitry Andric   if (!BI || !BI->isConditional())
1330b57cec5SDimitry Andric     return;
1340b57cec5SDimitry Andric 
1350b57cec5SDimitry Andric   CmpInst::Predicate Pred;
1360b57cec5SDimitry Andric   Value *Cond = BI->getCondition();
1370b57cec5SDimitry Andric   if (!match(Cond, m_ICmp(Pred, m_Value(), m_Constant())))
1380b57cec5SDimitry Andric     return;
1390b57cec5SDimitry Andric 
1400b57cec5SDimitry Andric   ICmpInst *Cmp = cast<ICmpInst>(Cond);
1410b57cec5SDimitry Andric   if (Pred == ICmpInst::ICMP_EQ || Pred == ICmpInst::ICMP_NE)
1425ffd83dbSDimitry Andric     if (isCondRelevantToAnyCallArgument(Cmp, CB))
1430b57cec5SDimitry Andric       Conditions.push_back({Cmp, From->getTerminator()->getSuccessor(0) == To
1440b57cec5SDimitry Andric                                      ? Pred
1450b57cec5SDimitry Andric                                      : Cmp->getInversePredicate()});
1460b57cec5SDimitry Andric }
1470b57cec5SDimitry Andric 
1485ffd83dbSDimitry Andric /// Record ICmp conditions relevant to any argument in CB following Pred's
1490b57cec5SDimitry Andric /// single predecessors. If there are conflicting conditions along a path, like
1500b57cec5SDimitry Andric /// x == 1 and x == 0, the first condition will be used. We stop once we reach
1510b57cec5SDimitry Andric /// an edge to StopAt.
1525ffd83dbSDimitry Andric static void recordConditions(CallBase &CB, BasicBlock *Pred,
1530b57cec5SDimitry Andric                              ConditionsTy &Conditions, BasicBlock *StopAt) {
1540b57cec5SDimitry Andric   BasicBlock *From = Pred;
1550b57cec5SDimitry Andric   BasicBlock *To = Pred;
1560b57cec5SDimitry Andric   SmallPtrSet<BasicBlock *, 4> Visited;
1570b57cec5SDimitry Andric   while (To != StopAt && !Visited.count(From->getSinglePredecessor()) &&
1580b57cec5SDimitry Andric          (From = From->getSinglePredecessor())) {
1595ffd83dbSDimitry Andric     recordCondition(CB, From, To, Conditions);
1600b57cec5SDimitry Andric     Visited.insert(From);
1610b57cec5SDimitry Andric     To = From;
1620b57cec5SDimitry Andric   }
1630b57cec5SDimitry Andric }
1640b57cec5SDimitry Andric 
1655ffd83dbSDimitry Andric static void addConditions(CallBase &CB, const ConditionsTy &Conditions) {
166bdd1243dSDimitry Andric   for (const auto &Cond : Conditions) {
1670b57cec5SDimitry Andric     Value *Arg = Cond.first->getOperand(0);
1680b57cec5SDimitry Andric     Constant *ConstVal = cast<Constant>(Cond.first->getOperand(1));
1690b57cec5SDimitry Andric     if (Cond.second == ICmpInst::ICMP_EQ)
1705ffd83dbSDimitry Andric       setConstantInArgument(CB, Arg, ConstVal);
1710b57cec5SDimitry Andric     else if (ConstVal->getType()->isPointerTy() && ConstVal->isNullValue()) {
1720b57cec5SDimitry Andric       assert(Cond.second == ICmpInst::ICMP_NE);
1735ffd83dbSDimitry Andric       addNonNullAttribute(CB, Arg);
1740b57cec5SDimitry Andric     }
1750b57cec5SDimitry Andric   }
1760b57cec5SDimitry Andric }
1770b57cec5SDimitry Andric 
1780b57cec5SDimitry Andric static SmallVector<BasicBlock *, 2> getTwoPredecessors(BasicBlock *BB) {
1790b57cec5SDimitry Andric   SmallVector<BasicBlock *, 2> Preds(predecessors((BB)));
1800b57cec5SDimitry Andric   assert(Preds.size() == 2 && "Expected exactly 2 predecessors!");
1810b57cec5SDimitry Andric   return Preds;
1820b57cec5SDimitry Andric }
1830b57cec5SDimitry Andric 
1845ffd83dbSDimitry Andric static bool canSplitCallSite(CallBase &CB, TargetTransformInfo &TTI) {
1855ffd83dbSDimitry Andric   if (CB.isConvergent() || CB.cannotDuplicate())
1860b57cec5SDimitry Andric     return false;
1870b57cec5SDimitry Andric 
1880b57cec5SDimitry Andric   // FIXME: As of now we handle only CallInst. InvokeInst could be handled
1890b57cec5SDimitry Andric   // without too much effort.
1905ffd83dbSDimitry Andric   if (!isa<CallInst>(CB))
1910b57cec5SDimitry Andric     return false;
1920b57cec5SDimitry Andric 
1935ffd83dbSDimitry Andric   BasicBlock *CallSiteBB = CB.getParent();
1940b57cec5SDimitry Andric   // Need 2 predecessors and cannot split an edge from an IndirectBrInst.
1950b57cec5SDimitry Andric   SmallVector<BasicBlock *, 2> Preds(predecessors(CallSiteBB));
1960b57cec5SDimitry Andric   if (Preds.size() != 2 || isa<IndirectBrInst>(Preds[0]->getTerminator()) ||
1970b57cec5SDimitry Andric       isa<IndirectBrInst>(Preds[1]->getTerminator()))
1980b57cec5SDimitry Andric     return false;
1990b57cec5SDimitry Andric 
2000b57cec5SDimitry Andric   // BasicBlock::canSplitPredecessors is more aggressive, so checking for
2010b57cec5SDimitry Andric   // BasicBlock::isEHPad as well.
2020b57cec5SDimitry Andric   if (!CallSiteBB->canSplitPredecessors() || CallSiteBB->isEHPad())
2030b57cec5SDimitry Andric     return false;
2040b57cec5SDimitry Andric 
2050b57cec5SDimitry Andric   // Allow splitting a call-site only when the CodeSize cost of the
2060b57cec5SDimitry Andric   // instructions before the call is less then DuplicationThreshold. The
2070b57cec5SDimitry Andric   // instructions before the call will be duplicated in the split blocks and
2080b57cec5SDimitry Andric   // corresponding uses will be updated.
209e8d8bef9SDimitry Andric   InstructionCost Cost = 0;
2100b57cec5SDimitry Andric   for (auto &InstBeforeCall :
2115ffd83dbSDimitry Andric        llvm::make_range(CallSiteBB->begin(), CB.getIterator())) {
2120b57cec5SDimitry Andric     Cost += TTI.getInstructionCost(&InstBeforeCall,
2130b57cec5SDimitry Andric                                    TargetTransformInfo::TCK_CodeSize);
2140b57cec5SDimitry Andric     if (Cost >= DuplicationThreshold)
2150b57cec5SDimitry Andric       return false;
2160b57cec5SDimitry Andric   }
2170b57cec5SDimitry Andric 
2180b57cec5SDimitry Andric   return true;
2190b57cec5SDimitry Andric }
2200b57cec5SDimitry Andric 
2210b57cec5SDimitry Andric static Instruction *cloneInstForMustTail(Instruction *I, Instruction *Before,
2220b57cec5SDimitry Andric                                          Value *V) {
2230b57cec5SDimitry Andric   Instruction *Copy = I->clone();
2240b57cec5SDimitry Andric   Copy->setName(I->getName());
2250b57cec5SDimitry Andric   Copy->insertBefore(Before);
2260b57cec5SDimitry Andric   if (V)
2270b57cec5SDimitry Andric     Copy->setOperand(0, V);
2280b57cec5SDimitry Andric   return Copy;
2290b57cec5SDimitry Andric }
2300b57cec5SDimitry Andric 
2310b57cec5SDimitry Andric /// Copy mandatory `musttail` return sequence that follows original `CI`, and
2320b57cec5SDimitry Andric /// link it up to `NewCI` value instead:
2330b57cec5SDimitry Andric ///
2340b57cec5SDimitry Andric ///   * (optional) `bitcast NewCI to ...`
2350b57cec5SDimitry Andric ///   * `ret bitcast or NewCI`
2360b57cec5SDimitry Andric ///
2370b57cec5SDimitry Andric /// Insert this sequence right before `SplitBB`'s terminator, which will be
2380b57cec5SDimitry Andric /// cleaned up later in `splitCallSite` below.
2390b57cec5SDimitry Andric static void copyMustTailReturn(BasicBlock *SplitBB, Instruction *CI,
2400b57cec5SDimitry Andric                                Instruction *NewCI) {
2410b57cec5SDimitry Andric   bool IsVoid = SplitBB->getParent()->getReturnType()->isVoidTy();
2420b57cec5SDimitry Andric   auto II = std::next(CI->getIterator());
2430b57cec5SDimitry Andric 
2440b57cec5SDimitry Andric   BitCastInst* BCI = dyn_cast<BitCastInst>(&*II);
2450b57cec5SDimitry Andric   if (BCI)
2460b57cec5SDimitry Andric     ++II;
2470b57cec5SDimitry Andric 
2480b57cec5SDimitry Andric   ReturnInst* RI = dyn_cast<ReturnInst>(&*II);
2490b57cec5SDimitry Andric   assert(RI && "`musttail` call must be followed by `ret` instruction");
2500b57cec5SDimitry Andric 
2510b57cec5SDimitry Andric   Instruction *TI = SplitBB->getTerminator();
2520b57cec5SDimitry Andric   Value *V = NewCI;
2530b57cec5SDimitry Andric   if (BCI)
2540b57cec5SDimitry Andric     V = cloneInstForMustTail(BCI, TI, V);
2550b57cec5SDimitry Andric   cloneInstForMustTail(RI, TI, IsVoid ? nullptr : V);
2560b57cec5SDimitry Andric 
2570b57cec5SDimitry Andric   // FIXME: remove TI here, `DuplicateInstructionsInSplitBetween` has a bug
2580b57cec5SDimitry Andric   // that prevents doing this now.
2590b57cec5SDimitry Andric }
2600b57cec5SDimitry Andric 
2610b57cec5SDimitry Andric /// For each (predecessor, conditions from predecessors) pair, it will split the
2620b57cec5SDimitry Andric /// basic block containing the call site, hook it up to the predecessor and
2630b57cec5SDimitry Andric /// replace the call instruction with new call instructions, which contain
2640b57cec5SDimitry Andric /// constraints based on the conditions from their predecessors.
2650b57cec5SDimitry Andric /// For example, in the IR below with an OR condition, the call-site can
2660b57cec5SDimitry Andric /// be split. In this case, Preds for Tail is [(Header, a == null),
2670b57cec5SDimitry Andric /// (TBB, a != null, b == null)]. Tail is replaced by 2 split blocks, containing
2680b57cec5SDimitry Andric /// CallInst1, which has constraints based on the conditions from Head and
2690b57cec5SDimitry Andric /// CallInst2, which has constraints based on the conditions coming from TBB.
2700b57cec5SDimitry Andric ///
2710b57cec5SDimitry Andric /// From :
2720b57cec5SDimitry Andric ///
2730b57cec5SDimitry Andric ///   Header:
2740b57cec5SDimitry Andric ///     %c = icmp eq i32* %a, null
2750b57cec5SDimitry Andric ///     br i1 %c %Tail, %TBB
2760b57cec5SDimitry Andric ///   TBB:
2770b57cec5SDimitry Andric ///     %c2 = icmp eq i32* %b, null
2780b57cec5SDimitry Andric ///     br i1 %c %Tail, %End
2790b57cec5SDimitry Andric ///   Tail:
2800b57cec5SDimitry Andric ///     %ca = call i1  @callee (i32* %a, i32* %b)
2810b57cec5SDimitry Andric ///
2820b57cec5SDimitry Andric ///  to :
2830b57cec5SDimitry Andric ///
2840b57cec5SDimitry Andric ///   Header:                          // PredBB1 is Header
2850b57cec5SDimitry Andric ///     %c = icmp eq i32* %a, null
2860b57cec5SDimitry Andric ///     br i1 %c %Tail-split1, %TBB
2870b57cec5SDimitry Andric ///   TBB:                             // PredBB2 is TBB
2880b57cec5SDimitry Andric ///     %c2 = icmp eq i32* %b, null
2890b57cec5SDimitry Andric ///     br i1 %c %Tail-split2, %End
2900b57cec5SDimitry Andric ///   Tail-split1:
2910b57cec5SDimitry Andric ///     %ca1 = call @callee (i32* null, i32* %b)         // CallInst1
2920b57cec5SDimitry Andric ///    br %Tail
2930b57cec5SDimitry Andric ///   Tail-split2:
2940b57cec5SDimitry Andric ///     %ca2 = call @callee (i32* nonnull %a, i32* null) // CallInst2
2950b57cec5SDimitry Andric ///    br %Tail
2960b57cec5SDimitry Andric ///   Tail:
2970b57cec5SDimitry Andric ///    %p = phi i1 [%ca1, %Tail-split1],[%ca2, %Tail-split2]
2980b57cec5SDimitry Andric ///
2990b57cec5SDimitry Andric /// Note that in case any arguments at the call-site are constrained by its
3000b57cec5SDimitry Andric /// predecessors, new call-sites with more constrained arguments will be
3010b57cec5SDimitry Andric /// created in createCallSitesOnPredicatedArgument().
30281ad6265SDimitry Andric static void splitCallSite(CallBase &CB,
30381ad6265SDimitry Andric                           ArrayRef<std::pair<BasicBlock *, ConditionsTy>> Preds,
3040b57cec5SDimitry Andric                           DomTreeUpdater &DTU) {
3055ffd83dbSDimitry Andric   BasicBlock *TailBB = CB.getParent();
3065ffd83dbSDimitry Andric   bool IsMustTailCall = CB.isMustTailCall();
3070b57cec5SDimitry Andric 
3080b57cec5SDimitry Andric   PHINode *CallPN = nullptr;
3090b57cec5SDimitry Andric 
3100b57cec5SDimitry Andric   // `musttail` calls must be followed by optional `bitcast`, and `ret`. The
3110b57cec5SDimitry Andric   // split blocks will be terminated right after that so there're no users for
3120b57cec5SDimitry Andric   // this phi in a `TailBB`.
3135ffd83dbSDimitry Andric   if (!IsMustTailCall && !CB.use_empty()) {
3145ffd83dbSDimitry Andric     CallPN = PHINode::Create(CB.getType(), Preds.size(), "phi.call");
3155ffd83dbSDimitry Andric     CallPN->setDebugLoc(CB.getDebugLoc());
3160b57cec5SDimitry Andric   }
3170b57cec5SDimitry Andric 
3185ffd83dbSDimitry Andric   LLVM_DEBUG(dbgs() << "split call-site : " << CB << " into \n");
3190b57cec5SDimitry Andric 
3200b57cec5SDimitry Andric   assert(Preds.size() == 2 && "The ValueToValueMaps array has size 2.");
3210b57cec5SDimitry Andric   // ValueToValueMapTy is neither copy nor moveable, so we use a simple array
3220b57cec5SDimitry Andric   // here.
3230b57cec5SDimitry Andric   ValueToValueMapTy ValueToValueMaps[2];
3240b57cec5SDimitry Andric   for (unsigned i = 0; i < Preds.size(); i++) {
3250b57cec5SDimitry Andric     BasicBlock *PredBB = Preds[i].first;
3260b57cec5SDimitry Andric     BasicBlock *SplitBlock = DuplicateInstructionsInSplitBetween(
3275ffd83dbSDimitry Andric         TailBB, PredBB, &*std::next(CB.getIterator()), ValueToValueMaps[i],
3280b57cec5SDimitry Andric         DTU);
3290b57cec5SDimitry Andric     assert(SplitBlock && "Unexpected new basic block split.");
3300b57cec5SDimitry Andric 
3315ffd83dbSDimitry Andric     auto *NewCI =
3325ffd83dbSDimitry Andric         cast<CallBase>(&*std::prev(SplitBlock->getTerminator()->getIterator()));
3335ffd83dbSDimitry Andric     addConditions(*NewCI, Preds[i].second);
3340b57cec5SDimitry Andric 
3350b57cec5SDimitry Andric     // Handle PHIs used as arguments in the call-site.
3360b57cec5SDimitry Andric     for (PHINode &PN : TailBB->phis()) {
3370b57cec5SDimitry Andric       unsigned ArgNo = 0;
3385ffd83dbSDimitry Andric       for (auto &CI : CB.args()) {
3390b57cec5SDimitry Andric         if (&*CI == &PN) {
3405ffd83dbSDimitry Andric           NewCI->setArgOperand(ArgNo, PN.getIncomingValueForBlock(SplitBlock));
3410b57cec5SDimitry Andric         }
3420b57cec5SDimitry Andric         ++ArgNo;
3430b57cec5SDimitry Andric       }
3440b57cec5SDimitry Andric     }
3450b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "    " << *NewCI << " in " << SplitBlock->getName()
3460b57cec5SDimitry Andric                       << "\n");
3470b57cec5SDimitry Andric     if (CallPN)
3480b57cec5SDimitry Andric       CallPN->addIncoming(NewCI, SplitBlock);
3490b57cec5SDimitry Andric 
3500b57cec5SDimitry Andric     // Clone and place bitcast and return instructions before `TI`
3510b57cec5SDimitry Andric     if (IsMustTailCall)
3525ffd83dbSDimitry Andric       copyMustTailReturn(SplitBlock, &CB, NewCI);
3530b57cec5SDimitry Andric   }
3540b57cec5SDimitry Andric 
3550b57cec5SDimitry Andric   NumCallSiteSplit++;
3560b57cec5SDimitry Andric 
3570b57cec5SDimitry Andric   // FIXME: remove TI in `copyMustTailReturn`
3580b57cec5SDimitry Andric   if (IsMustTailCall) {
3590b57cec5SDimitry Andric     // Remove superfluous `br` terminators from the end of the Split blocks
3600b57cec5SDimitry Andric     // NOTE: Removing terminator removes the SplitBlock from the TailBB's
3610b57cec5SDimitry Andric     // predecessors. Therefore we must get complete list of Splits before
3620b57cec5SDimitry Andric     // attempting removal.
3630b57cec5SDimitry Andric     SmallVector<BasicBlock *, 2> Splits(predecessors((TailBB)));
3640b57cec5SDimitry Andric     assert(Splits.size() == 2 && "Expected exactly 2 splits!");
365bdd1243dSDimitry Andric     for (BasicBlock *BB : Splits) {
366bdd1243dSDimitry Andric       BB->getTerminator()->eraseFromParent();
367bdd1243dSDimitry Andric       DTU.applyUpdatesPermissive({{DominatorTree::Delete, BB, TailBB}});
3680b57cec5SDimitry Andric     }
3690b57cec5SDimitry Andric 
3700b57cec5SDimitry Andric     // Erase the tail block once done with musttail patching
3710b57cec5SDimitry Andric     DTU.deleteBB(TailBB);
3720b57cec5SDimitry Andric     return;
3730b57cec5SDimitry Andric   }
3740b57cec5SDimitry Andric 
3755f757f3fSDimitry Andric   BasicBlock::iterator OriginalBegin = TailBB->begin();
3760b57cec5SDimitry Andric   // Replace users of the original call with a PHI mering call-sites split.
3770b57cec5SDimitry Andric   if (CallPN) {
3785f757f3fSDimitry Andric     CallPN->insertBefore(*TailBB, OriginalBegin);
3795ffd83dbSDimitry Andric     CB.replaceAllUsesWith(CallPN);
3800b57cec5SDimitry Andric   }
3810b57cec5SDimitry Andric 
3820b57cec5SDimitry Andric   // Remove instructions moved to split blocks from TailBB, from the duplicated
3830b57cec5SDimitry Andric   // call instruction to the beginning of the basic block. If an instruction
3840b57cec5SDimitry Andric   // has any uses, add a new PHI node to combine the values coming from the
3850b57cec5SDimitry Andric   // split blocks. The new PHI nodes are placed before the first original
3860b57cec5SDimitry Andric   // instruction, so we do not end up deleting them. By using reverse-order, we
3870b57cec5SDimitry Andric   // do not introduce unnecessary PHI nodes for def-use chains from the call
3880b57cec5SDimitry Andric   // instruction to the beginning of the block.
3895ffd83dbSDimitry Andric   auto I = CB.getReverseIterator();
3905f757f3fSDimitry Andric   Instruction *OriginalBeginInst = &*OriginalBegin;
3910b57cec5SDimitry Andric   while (I != TailBB->rend()) {
3920b57cec5SDimitry Andric     Instruction *CurrentI = &*I++;
3930b57cec5SDimitry Andric     if (!CurrentI->use_empty()) {
3940b57cec5SDimitry Andric       // If an existing PHI has users after the call, there is no need to create
3950b57cec5SDimitry Andric       // a new one.
3960b57cec5SDimitry Andric       if (isa<PHINode>(CurrentI))
3970b57cec5SDimitry Andric         continue;
3980b57cec5SDimitry Andric       PHINode *NewPN = PHINode::Create(CurrentI->getType(), Preds.size());
3990b57cec5SDimitry Andric       NewPN->setDebugLoc(CurrentI->getDebugLoc());
4000b57cec5SDimitry Andric       for (auto &Mapping : ValueToValueMaps)
4010b57cec5SDimitry Andric         NewPN->addIncoming(Mapping[CurrentI],
4020b57cec5SDimitry Andric                            cast<Instruction>(Mapping[CurrentI])->getParent());
4035f757f3fSDimitry Andric       NewPN->insertBefore(*TailBB, TailBB->begin());
4040b57cec5SDimitry Andric       CurrentI->replaceAllUsesWith(NewPN);
4050b57cec5SDimitry Andric     }
406*0fca6ea1SDimitry Andric     CurrentI->dropDbgRecords();
4070b57cec5SDimitry Andric     CurrentI->eraseFromParent();
4080b57cec5SDimitry Andric     // We are done once we handled the first original instruction in TailBB.
4095f757f3fSDimitry Andric     if (CurrentI == OriginalBeginInst)
4100b57cec5SDimitry Andric       break;
4110b57cec5SDimitry Andric   }
4120b57cec5SDimitry Andric }
4130b57cec5SDimitry Andric 
4140b57cec5SDimitry Andric // Return true if the call-site has an argument which is a PHI with only
4150b57cec5SDimitry Andric // constant incoming values.
4165ffd83dbSDimitry Andric static bool isPredicatedOnPHI(CallBase &CB) {
4175ffd83dbSDimitry Andric   BasicBlock *Parent = CB.getParent();
4185ffd83dbSDimitry Andric   if (&CB != Parent->getFirstNonPHIOrDbg())
4190b57cec5SDimitry Andric     return false;
4200b57cec5SDimitry Andric 
4215ffd83dbSDimitry Andric   for (auto &PN : Parent->phis()) {
4225ffd83dbSDimitry Andric     for (auto &Arg : CB.args()) {
4235ffd83dbSDimitry Andric       if (&*Arg != &PN)
4240b57cec5SDimitry Andric         continue;
4255ffd83dbSDimitry Andric       assert(PN.getNumIncomingValues() == 2 &&
4265ffd83dbSDimitry Andric              "Unexpected number of incoming values");
4275ffd83dbSDimitry Andric       if (PN.getIncomingBlock(0) == PN.getIncomingBlock(1))
4285ffd83dbSDimitry Andric         return false;
4295ffd83dbSDimitry Andric       if (PN.getIncomingValue(0) == PN.getIncomingValue(1))
4305ffd83dbSDimitry Andric         continue;
4315ffd83dbSDimitry Andric       if (isa<Constant>(PN.getIncomingValue(0)) &&
4325ffd83dbSDimitry Andric           isa<Constant>(PN.getIncomingValue(1)))
4330b57cec5SDimitry Andric         return true;
4340b57cec5SDimitry Andric     }
4350b57cec5SDimitry Andric   }
4360b57cec5SDimitry Andric   return false;
4370b57cec5SDimitry Andric }
4380b57cec5SDimitry Andric 
4390b57cec5SDimitry Andric using PredsWithCondsTy = SmallVector<std::pair<BasicBlock *, ConditionsTy>, 2>;
4400b57cec5SDimitry Andric 
4410b57cec5SDimitry Andric // Check if any of the arguments in CS are predicated on a PHI node and return
4420b57cec5SDimitry Andric // the set of predecessors we should use for splitting.
4435ffd83dbSDimitry Andric static PredsWithCondsTy shouldSplitOnPHIPredicatedArgument(CallBase &CB) {
4445ffd83dbSDimitry Andric   if (!isPredicatedOnPHI(CB))
4450b57cec5SDimitry Andric     return {};
4460b57cec5SDimitry Andric 
4475ffd83dbSDimitry Andric   auto Preds = getTwoPredecessors(CB.getParent());
4480b57cec5SDimitry Andric   return {{Preds[0], {}}, {Preds[1], {}}};
4490b57cec5SDimitry Andric }
4500b57cec5SDimitry Andric 
4510b57cec5SDimitry Andric // Checks if any of the arguments in CS are predicated in a predecessor and
4520b57cec5SDimitry Andric // returns a list of predecessors with the conditions that hold on their edges
4530b57cec5SDimitry Andric // to CS.
4545ffd83dbSDimitry Andric static PredsWithCondsTy shouldSplitOnPredicatedArgument(CallBase &CB,
4550b57cec5SDimitry Andric                                                         DomTreeUpdater &DTU) {
4565ffd83dbSDimitry Andric   auto Preds = getTwoPredecessors(CB.getParent());
4570b57cec5SDimitry Andric   if (Preds[0] == Preds[1])
4580b57cec5SDimitry Andric     return {};
4590b57cec5SDimitry Andric 
4600b57cec5SDimitry Andric   // We can stop recording conditions once we reached the immediate dominator
4610b57cec5SDimitry Andric   // for the block containing the call site. Conditions in predecessors of the
4620b57cec5SDimitry Andric   // that node will be the same for all paths to the call site and splitting
4630b57cec5SDimitry Andric   // is not beneficial.
4640b57cec5SDimitry Andric   assert(DTU.hasDomTree() && "We need a DTU with a valid DT!");
4655ffd83dbSDimitry Andric   auto *CSDTNode = DTU.getDomTree().getNode(CB.getParent());
4660b57cec5SDimitry Andric   BasicBlock *StopAt = CSDTNode ? CSDTNode->getIDom()->getBlock() : nullptr;
4670b57cec5SDimitry Andric 
4680b57cec5SDimitry Andric   SmallVector<std::pair<BasicBlock *, ConditionsTy>, 2> PredsCS;
469349cc55cSDimitry Andric   for (auto *Pred : llvm::reverse(Preds)) {
4700b57cec5SDimitry Andric     ConditionsTy Conditions;
4710b57cec5SDimitry Andric     // Record condition on edge BB(CS) <- Pred
4725ffd83dbSDimitry Andric     recordCondition(CB, Pred, CB.getParent(), Conditions);
4730b57cec5SDimitry Andric     // Record conditions following Pred's single predecessors.
4745ffd83dbSDimitry Andric     recordConditions(CB, Pred, Conditions, StopAt);
4750b57cec5SDimitry Andric     PredsCS.push_back({Pred, Conditions});
4760b57cec5SDimitry Andric   }
4770b57cec5SDimitry Andric 
4780b57cec5SDimitry Andric   if (all_of(PredsCS, [](const std::pair<BasicBlock *, ConditionsTy> &P) {
4790b57cec5SDimitry Andric         return P.second.empty();
4800b57cec5SDimitry Andric       }))
4810b57cec5SDimitry Andric     return {};
4820b57cec5SDimitry Andric 
4830b57cec5SDimitry Andric   return PredsCS;
4840b57cec5SDimitry Andric }
4850b57cec5SDimitry Andric 
4865ffd83dbSDimitry Andric static bool tryToSplitCallSite(CallBase &CB, TargetTransformInfo &TTI,
4870b57cec5SDimitry Andric                                DomTreeUpdater &DTU) {
4880b57cec5SDimitry Andric   // Check if we can split the call site.
4895ffd83dbSDimitry Andric   if (!CB.arg_size() || !canSplitCallSite(CB, TTI))
4900b57cec5SDimitry Andric     return false;
4910b57cec5SDimitry Andric 
4925ffd83dbSDimitry Andric   auto PredsWithConds = shouldSplitOnPredicatedArgument(CB, DTU);
4930b57cec5SDimitry Andric   if (PredsWithConds.empty())
4945ffd83dbSDimitry Andric     PredsWithConds = shouldSplitOnPHIPredicatedArgument(CB);
4950b57cec5SDimitry Andric   if (PredsWithConds.empty())
4960b57cec5SDimitry Andric     return false;
4970b57cec5SDimitry Andric 
4985ffd83dbSDimitry Andric   splitCallSite(CB, PredsWithConds, DTU);
4990b57cec5SDimitry Andric   return true;
5000b57cec5SDimitry Andric }
5010b57cec5SDimitry Andric 
5020b57cec5SDimitry Andric static bool doCallSiteSplitting(Function &F, TargetLibraryInfo &TLI,
5030b57cec5SDimitry Andric                                 TargetTransformInfo &TTI, DominatorTree &DT) {
5040b57cec5SDimitry Andric 
5050b57cec5SDimitry Andric   DomTreeUpdater DTU(&DT, DomTreeUpdater::UpdateStrategy::Lazy);
5060b57cec5SDimitry Andric   bool Changed = false;
507349cc55cSDimitry Andric   for (BasicBlock &BB : llvm::make_early_inc_range(F)) {
5080b57cec5SDimitry Andric     auto II = BB.getFirstNonPHIOrDbg()->getIterator();
5090b57cec5SDimitry Andric     auto IE = BB.getTerminator()->getIterator();
5100b57cec5SDimitry Andric     // Iterate until we reach the terminator instruction. tryToSplitCallSite
5110b57cec5SDimitry Andric     // can replace BB's terminator in case BB is a successor of itself. In that
5120b57cec5SDimitry Andric     // case, IE will be invalidated and we also have to check the current
5130b57cec5SDimitry Andric     // terminator.
5140b57cec5SDimitry Andric     while (II != IE && &*II != BB.getTerminator()) {
5155ffd83dbSDimitry Andric       CallBase *CB = dyn_cast<CallBase>(&*II++);
5165ffd83dbSDimitry Andric       if (!CB || isa<IntrinsicInst>(CB) || isInstructionTriviallyDead(CB, &TLI))
5170b57cec5SDimitry Andric         continue;
5180b57cec5SDimitry Andric 
5195ffd83dbSDimitry Andric       Function *Callee = CB->getCalledFunction();
5200b57cec5SDimitry Andric       if (!Callee || Callee->isDeclaration())
5210b57cec5SDimitry Andric         continue;
5220b57cec5SDimitry Andric 
5230b57cec5SDimitry Andric       // Successful musttail call-site splits result in erased CI and erased BB.
5240b57cec5SDimitry Andric       // Check if such path is possible before attempting the splitting.
5255ffd83dbSDimitry Andric       bool IsMustTail = CB->isMustTailCall();
5260b57cec5SDimitry Andric 
5275ffd83dbSDimitry Andric       Changed |= tryToSplitCallSite(*CB, TTI, DTU);
5280b57cec5SDimitry Andric 
5290b57cec5SDimitry Andric       // There're no interesting instructions after this. The call site
5300b57cec5SDimitry Andric       // itself might have been erased on splitting.
5310b57cec5SDimitry Andric       if (IsMustTail)
5320b57cec5SDimitry Andric         break;
5330b57cec5SDimitry Andric     }
5340b57cec5SDimitry Andric   }
5350b57cec5SDimitry Andric   return Changed;
5360b57cec5SDimitry Andric }
5370b57cec5SDimitry Andric 
5380b57cec5SDimitry Andric PreservedAnalyses CallSiteSplittingPass::run(Function &F,
5390b57cec5SDimitry Andric                                              FunctionAnalysisManager &AM) {
5400b57cec5SDimitry Andric   auto &TLI = AM.getResult<TargetLibraryAnalysis>(F);
5410b57cec5SDimitry Andric   auto &TTI = AM.getResult<TargetIRAnalysis>(F);
5420b57cec5SDimitry Andric   auto &DT = AM.getResult<DominatorTreeAnalysis>(F);
5430b57cec5SDimitry Andric 
5440b57cec5SDimitry Andric   if (!doCallSiteSplitting(F, TLI, TTI, DT))
5450b57cec5SDimitry Andric     return PreservedAnalyses::all();
5460b57cec5SDimitry Andric   PreservedAnalyses PA;
5470b57cec5SDimitry Andric   PA.preserve<DominatorTreeAnalysis>();
5480b57cec5SDimitry Andric   return PA;
5490b57cec5SDimitry Andric }
550