Jun Bum Lim | 0c99007 | 2017-11-03 20:41:16 +0000 | [diff] [blame] | 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 | // |
Florian Hahn | 7e93289 | 2017-12-23 20:02:26 +0000 | [diff] [blame] | 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. |
Jun Bum Lim | 0c99007 | 2017-11-03 20:41:16 +0000 | [diff] [blame] | 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" |
Florian Hahn | b4e3bad | 2018-02-14 13:59:12 +0000 | [diff] [blame] | 62 | #include "llvm/Analysis/TargetTransformInfo.h" |
David Blaikie | 2be3922 | 2018-03-21 22:34:23 +0000 | [diff] [blame] | 63 | #include "llvm/Analysis/Utils/Local.h" |
Jun Bum Lim | 0c99007 | 2017-11-03 20:41:16 +0000 | [diff] [blame] | 64 | #include "llvm/IR/IntrinsicInst.h" |
| 65 | #include "llvm/IR/PatternMatch.h" |
| 66 | #include "llvm/Support/Debug.h" |
| 67 | #include "llvm/Transforms/Scalar.h" |
| 68 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
Florian Hahn | b4e3bad | 2018-02-14 13:59:12 +0000 | [diff] [blame] | 69 | #include "llvm/Transforms/Utils/Cloning.h" |
Jun Bum Lim | 0c99007 | 2017-11-03 20:41:16 +0000 | [diff] [blame] | 70 | |
| 71 | using namespace llvm; |
| 72 | using namespace PatternMatch; |
| 73 | |
| 74 | #define DEBUG_TYPE "callsite-splitting" |
| 75 | |
| 76 | STATISTIC(NumCallSiteSplit, "Number of call-site split"); |
| 77 | |
Florian Hahn | b4e3bad | 2018-02-14 13:59:12 +0000 | [diff] [blame] | 78 | /// Only allow instructions before a call, if their CodeSize cost is below |
| 79 | /// DuplicationThreshold. Those instructions need to be duplicated in all |
| 80 | /// split blocks. |
| 81 | static cl::opt<unsigned> |
| 82 | DuplicationThreshold("callsite-splitting-duplication-threshold", cl::Hidden, |
| 83 | cl::desc("Only allow instructions before a call, if " |
| 84 | "their cost is below DuplicationThreshold"), |
| 85 | cl::init(5)); |
| 86 | |
Florian Hahn | c6c89bf | 2018-01-16 22:13:15 +0000 | [diff] [blame] | 87 | static void addNonNullAttribute(CallSite CS, Value *Op) { |
Jun Bum Lim | 0c99007 | 2017-11-03 20:41:16 +0000 | [diff] [blame] | 88 | unsigned ArgNo = 0; |
| 89 | for (auto &I : CS.args()) { |
| 90 | if (&*I == Op) |
| 91 | CS.addParamAttr(ArgNo, Attribute::NonNull); |
| 92 | ++ArgNo; |
| 93 | } |
| 94 | } |
| 95 | |
Florian Hahn | c6c89bf | 2018-01-16 22:13:15 +0000 | [diff] [blame] | 96 | static void setConstantInArgument(CallSite CS, Value *Op, |
| 97 | Constant *ConstValue) { |
Jun Bum Lim | 0c99007 | 2017-11-03 20:41:16 +0000 | [diff] [blame] | 98 | unsigned ArgNo = 0; |
| 99 | for (auto &I : CS.args()) { |
Xin Tong | 8edff27 | 2018-04-23 20:09:08 +0000 | [diff] [blame] | 100 | if (&*I == Op) { |
| 101 | // It is possible we have already added the non-null attribute to the |
| 102 | // parameter by using an earlier constraining condition. |
| 103 | CS.removeParamAttr(ArgNo, Attribute::NonNull); |
Jun Bum Lim | 0c99007 | 2017-11-03 20:41:16 +0000 | [diff] [blame] | 104 | CS.setArgument(ArgNo, ConstValue); |
Xin Tong | 8edff27 | 2018-04-23 20:09:08 +0000 | [diff] [blame] | 105 | } |
Jun Bum Lim | 0c99007 | 2017-11-03 20:41:16 +0000 | [diff] [blame] | 106 | ++ArgNo; |
| 107 | } |
| 108 | } |
| 109 | |
Florian Hahn | 2a266a3 | 2017-11-18 18:14:13 +0000 | [diff] [blame] | 110 | static bool isCondRelevantToAnyCallArgument(ICmpInst *Cmp, CallSite CS) { |
| 111 | assert(isa<Constant>(Cmp->getOperand(1)) && "Expected a constant operand."); |
| 112 | Value *Op0 = Cmp->getOperand(0); |
| 113 | unsigned ArgNo = 0; |
| 114 | for (CallSite::arg_iterator I = CS.arg_begin(), E = CS.arg_end(); I != E; |
| 115 | ++I, ++ArgNo) { |
| 116 | // Don't consider constant or arguments that are already known non-null. |
| 117 | if (isa<Constant>(*I) || CS.paramHasAttr(ArgNo, Attribute::NonNull)) |
| 118 | continue; |
| 119 | |
| 120 | if (*I == Op0) |
| 121 | return true; |
| 122 | } |
| 123 | return false; |
| 124 | } |
| 125 | |
Florian Hahn | c6c89bf | 2018-01-16 22:13:15 +0000 | [diff] [blame] | 126 | typedef std::pair<ICmpInst *, unsigned> ConditionTy; |
| 127 | typedef SmallVector<ConditionTy, 2> ConditionsTy; |
| 128 | |
Florian Hahn | beda7d5 | 2017-12-13 03:05:20 +0000 | [diff] [blame] | 129 | /// If From has a conditional jump to To, add the condition to Conditions, |
| 130 | /// if it is relevant to any argument at CS. |
Florian Hahn | c6c89bf | 2018-01-16 22:13:15 +0000 | [diff] [blame] | 131 | static void recordCondition(CallSite CS, BasicBlock *From, BasicBlock *To, |
| 132 | ConditionsTy &Conditions) { |
Florian Hahn | beda7d5 | 2017-12-13 03:05:20 +0000 | [diff] [blame] | 133 | auto *BI = dyn_cast<BranchInst>(From->getTerminator()); |
| 134 | if (!BI || !BI->isConditional()) |
| 135 | return; |
Florian Hahn | 2a266a3 | 2017-11-18 18:14:13 +0000 | [diff] [blame] | 136 | |
Florian Hahn | beda7d5 | 2017-12-13 03:05:20 +0000 | [diff] [blame] | 137 | CmpInst::Predicate Pred; |
| 138 | Value *Cond = BI->getCondition(); |
| 139 | if (!match(Cond, m_ICmp(Pred, m_Value(), m_Constant()))) |
| 140 | return; |
| 141 | |
| 142 | ICmpInst *Cmp = cast<ICmpInst>(Cond); |
| 143 | if (Pred == ICmpInst::ICMP_EQ || Pred == ICmpInst::ICMP_NE) |
| 144 | if (isCondRelevantToAnyCallArgument(Cmp, CS)) |
| 145 | Conditions.push_back({Cmp, From->getTerminator()->getSuccessor(0) == To |
| 146 | ? Pred |
| 147 | : Cmp->getInversePredicate()}); |
Florian Hahn | 2a266a3 | 2017-11-18 18:14:13 +0000 | [diff] [blame] | 148 | } |
| 149 | |
Florian Hahn | beda7d5 | 2017-12-13 03:05:20 +0000 | [diff] [blame] | 150 | /// Record ICmp conditions relevant to any argument in CS following Pred's |
Xin Tong | d83c883 | 2018-04-13 04:35:38 +0000 | [diff] [blame] | 151 | /// single predecessors. If there are conflicting conditions along a path, like |
Florian Hahn | beda7d5 | 2017-12-13 03:05:20 +0000 | [diff] [blame] | 152 | /// x == 1 and x == 0, the first condition will be used. |
Florian Hahn | c6c89bf | 2018-01-16 22:13:15 +0000 | [diff] [blame] | 153 | static void recordConditions(CallSite CS, BasicBlock *Pred, |
| 154 | ConditionsTy &Conditions) { |
Florian Hahn | beda7d5 | 2017-12-13 03:05:20 +0000 | [diff] [blame] | 155 | recordCondition(CS, Pred, CS.getInstruction()->getParent(), Conditions); |
| 156 | BasicBlock *From = Pred; |
| 157 | BasicBlock *To = Pred; |
Florian Hahn | 212afb9 | 2018-01-26 10:36:50 +0000 | [diff] [blame] | 158 | SmallPtrSet<BasicBlock *, 4> Visited; |
Florian Hahn | beda7d5 | 2017-12-13 03:05:20 +0000 | [diff] [blame] | 159 | while (!Visited.count(From->getSinglePredecessor()) && |
| 160 | (From = From->getSinglePredecessor())) { |
| 161 | recordCondition(CS, From, To, Conditions); |
Florian Hahn | 212afb9 | 2018-01-26 10:36:50 +0000 | [diff] [blame] | 162 | Visited.insert(From); |
Florian Hahn | beda7d5 | 2017-12-13 03:05:20 +0000 | [diff] [blame] | 163 | To = From; |
| 164 | } |
| 165 | } |
Jun Bum Lim | 0c99007 | 2017-11-03 20:41:16 +0000 | [diff] [blame] | 166 | |
Florian Hahn | c6c89bf | 2018-01-16 22:13:15 +0000 | [diff] [blame] | 167 | static void addConditions(CallSite CS, const ConditionsTy &Conditions) { |
Florian Hahn | beda7d5 | 2017-12-13 03:05:20 +0000 | [diff] [blame] | 168 | for (auto &Cond : Conditions) { |
| 169 | Value *Arg = Cond.first->getOperand(0); |
| 170 | Constant *ConstVal = cast<Constant>(Cond.first->getOperand(1)); |
| 171 | if (Cond.second == ICmpInst::ICMP_EQ) |
Florian Hahn | c6c89bf | 2018-01-16 22:13:15 +0000 | [diff] [blame] | 172 | setConstantInArgument(CS, Arg, ConstVal); |
Florian Hahn | beda7d5 | 2017-12-13 03:05:20 +0000 | [diff] [blame] | 173 | else if (ConstVal->getType()->isPointerTy() && ConstVal->isNullValue()) { |
| 174 | assert(Cond.second == ICmpInst::ICMP_NE); |
Florian Hahn | c6c89bf | 2018-01-16 22:13:15 +0000 | [diff] [blame] | 175 | addNonNullAttribute(CS, Arg); |
Jun Bum Lim | 0c99007 | 2017-11-03 20:41:16 +0000 | [diff] [blame] | 176 | } |
| 177 | } |
Florian Hahn | beda7d5 | 2017-12-13 03:05:20 +0000 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | static SmallVector<BasicBlock *, 2> getTwoPredecessors(BasicBlock *BB) { |
| 181 | SmallVector<BasicBlock *, 2> Preds(predecessors((BB))); |
| 182 | assert(Preds.size() == 2 && "Expected exactly 2 predecessors!"); |
| 183 | return Preds; |
Jun Bum Lim | 0c99007 | 2017-11-03 20:41:16 +0000 | [diff] [blame] | 184 | } |
| 185 | |
Florian Hahn | b4e3bad | 2018-02-14 13:59:12 +0000 | [diff] [blame] | 186 | static bool canSplitCallSite(CallSite CS, TargetTransformInfo &TTI) { |
Jun Bum Lim | 0c99007 | 2017-11-03 20:41:16 +0000 | [diff] [blame] | 187 | // FIXME: As of now we handle only CallInst. InvokeInst could be handled |
| 188 | // without too much effort. |
| 189 | Instruction *Instr = CS.getInstruction(); |
| 190 | if (!isa<CallInst>(Instr)) |
| 191 | return false; |
| 192 | |
Jun Bum Lim | 0c99007 | 2017-11-03 20:41:16 +0000 | [diff] [blame] | 193 | BasicBlock *CallSiteBB = Instr->getParent(); |
Florian Hahn | b4e3bad | 2018-02-14 13:59:12 +0000 | [diff] [blame] | 194 | // Allow splitting a call-site only when the CodeSize cost of the |
| 195 | // instructions before the call is less then DuplicationThreshold. The |
| 196 | // instructions before the call will be duplicated in the split blocks and |
| 197 | // corresponding uses will be updated. |
| 198 | unsigned Cost = 0; |
| 199 | for (auto &InstBeforeCall : |
| 200 | llvm::make_range(CallSiteBB->begin(), Instr->getIterator())) { |
| 201 | Cost += TTI.getInstructionCost(&InstBeforeCall, |
| 202 | TargetTransformInfo::TCK_CodeSize); |
| 203 | if (Cost >= DuplicationThreshold) |
| 204 | return false; |
| 205 | } |
Jun Bum Lim | 0c99007 | 2017-11-03 20:41:16 +0000 | [diff] [blame] | 206 | |
Florian Hahn | 2a266a3 | 2017-11-18 18:14:13 +0000 | [diff] [blame] | 207 | // Need 2 predecessors and cannot split an edge from an IndirectBrInst. |
| 208 | SmallVector<BasicBlock *, 2> Preds(predecessors(CallSiteBB)); |
| 209 | if (Preds.size() != 2 || isa<IndirectBrInst>(Preds[0]->getTerminator()) || |
Jun Bum Lim | 0c99007 | 2017-11-03 20:41:16 +0000 | [diff] [blame] | 210 | isa<IndirectBrInst>(Preds[1]->getTerminator())) |
| 211 | return false; |
| 212 | |
Taewook Oh | e0db533 | 2018-04-05 04:16:23 +0000 | [diff] [blame] | 213 | // Do not split a call-site in an exception handling block. This check |
| 214 | // prevents triggering an assertion in SplitEdge used via |
| 215 | // DuplicateInstructionsInSplitBetween. |
| 216 | if (CallSiteBB->isEHPad()) |
| 217 | return false; |
| 218 | |
Jun Bum Lim | 0c99007 | 2017-11-03 20:41:16 +0000 | [diff] [blame] | 219 | return CallSiteBB->canSplitPredecessors(); |
| 220 | } |
| 221 | |
Fedor Indutny | f9e09c1 | 2018-03-03 21:40:14 +0000 | [diff] [blame] | 222 | static Instruction *cloneInstForMustTail(Instruction *I, Instruction *Before, |
| 223 | Value *V) { |
| 224 | Instruction *Copy = I->clone(); |
| 225 | Copy->setName(I->getName()); |
| 226 | Copy->insertBefore(Before); |
| 227 | if (V) |
| 228 | Copy->setOperand(0, V); |
| 229 | return Copy; |
| 230 | } |
| 231 | |
| 232 | /// Copy mandatory `musttail` return sequence that follows original `CI`, and |
| 233 | /// link it up to `NewCI` value instead: |
Florian Hahn | c6c89bf | 2018-01-16 22:13:15 +0000 | [diff] [blame] | 234 | /// |
Fedor Indutny | f9e09c1 | 2018-03-03 21:40:14 +0000 | [diff] [blame] | 235 | /// * (optional) `bitcast NewCI to ...` |
| 236 | /// * `ret bitcast or NewCI` |
| 237 | /// |
| 238 | /// Insert this sequence right before `SplitBB`'s terminator, which will be |
| 239 | /// cleaned up later in `splitCallSite` below. |
| 240 | static void copyMustTailReturn(BasicBlock *SplitBB, Instruction *CI, |
| 241 | Instruction *NewCI) { |
| 242 | bool IsVoid = SplitBB->getParent()->getReturnType()->isVoidTy(); |
| 243 | auto II = std::next(CI->getIterator()); |
| 244 | |
| 245 | BitCastInst* BCI = dyn_cast<BitCastInst>(&*II); |
| 246 | if (BCI) |
| 247 | ++II; |
| 248 | |
| 249 | ReturnInst* RI = dyn_cast<ReturnInst>(&*II); |
| 250 | assert(RI && "`musttail` call must be followed by `ret` instruction"); |
| 251 | |
| 252 | TerminatorInst *TI = SplitBB->getTerminator(); |
| 253 | Value *V = NewCI; |
| 254 | if (BCI) |
| 255 | V = cloneInstForMustTail(BCI, TI, V); |
| 256 | cloneInstForMustTail(RI, TI, IsVoid ? nullptr : V); |
| 257 | |
| 258 | // FIXME: remove TI here, `DuplicateInstructionsInSplitBetween` has a bug |
| 259 | // that prevents doing this now. |
| 260 | } |
| 261 | |
Florian Hahn | c6c89bf | 2018-01-16 22:13:15 +0000 | [diff] [blame] | 262 | /// For each (predecessor, conditions from predecessors) pair, it will split the |
| 263 | /// basic block containing the call site, hook it up to the predecessor and |
| 264 | /// replace the call instruction with new call instructions, which contain |
| 265 | /// constraints based on the conditions from their predecessors. |
Florian Hahn | 7e93289 | 2017-12-23 20:02:26 +0000 | [diff] [blame] | 266 | /// For example, in the IR below with an OR condition, the call-site can |
Florian Hahn | c6c89bf | 2018-01-16 22:13:15 +0000 | [diff] [blame] | 267 | /// be split. In this case, Preds for Tail is [(Header, a == null), |
| 268 | /// (TBB, a != null, b == null)]. Tail is replaced by 2 split blocks, containing |
| 269 | /// CallInst1, which has constraints based on the conditions from Head and |
| 270 | /// CallInst2, which has constraints based on the conditions coming from TBB. |
Jun Bum Lim | 0c99007 | 2017-11-03 20:41:16 +0000 | [diff] [blame] | 271 | /// |
Florian Hahn | 7e93289 | 2017-12-23 20:02:26 +0000 | [diff] [blame] | 272 | /// From : |
Jun Bum Lim | 0c99007 | 2017-11-03 20:41:16 +0000 | [diff] [blame] | 273 | /// |
| 274 | /// Header: |
| 275 | /// %c = icmp eq i32* %a, null |
| 276 | /// br i1 %c %Tail, %TBB |
| 277 | /// TBB: |
| 278 | /// %c2 = icmp eq i32* %b, null |
| 279 | /// br i1 %c %Tail, %End |
| 280 | /// Tail: |
| 281 | /// %ca = call i1 @callee (i32* %a, i32* %b) |
| 282 | /// |
| 283 | /// to : |
| 284 | /// |
| 285 | /// Header: // PredBB1 is Header |
| 286 | /// %c = icmp eq i32* %a, null |
| 287 | /// br i1 %c %Tail-split1, %TBB |
| 288 | /// TBB: // PredBB2 is TBB |
| 289 | /// %c2 = icmp eq i32* %b, null |
| 290 | /// br i1 %c %Tail-split2, %End |
| 291 | /// Tail-split1: |
| 292 | /// %ca1 = call @callee (i32* null, i32* %b) // CallInst1 |
| 293 | /// br %Tail |
| 294 | /// Tail-split2: |
| 295 | /// %ca2 = call @callee (i32* nonnull %a, i32* null) // CallInst2 |
| 296 | /// br %Tail |
| 297 | /// Tail: |
| 298 | /// %p = phi i1 [%ca1, %Tail-split1],[%ca2, %Tail-split2] |
| 299 | /// |
Florian Hahn | 7e93289 | 2017-12-23 20:02:26 +0000 | [diff] [blame] | 300 | /// Note that in case any arguments at the call-site are constrained by its |
| 301 | /// predecessors, new call-sites with more constrained arguments will be |
| 302 | /// created in createCallSitesOnPredicatedArgument(). |
Florian Hahn | c6c89bf | 2018-01-16 22:13:15 +0000 | [diff] [blame] | 303 | static void splitCallSite( |
| 304 | CallSite CS, |
Florian Hahn | 9bc0bc4 | 2018-03-22 15:23:33 +0000 | [diff] [blame] | 305 | const SmallVectorImpl<std::pair<BasicBlock *, ConditionsTy>> &Preds, |
| 306 | DominatorTree *DT) { |
Jun Bum Lim | 0c99007 | 2017-11-03 20:41:16 +0000 | [diff] [blame] | 307 | Instruction *Instr = CS.getInstruction(); |
| 308 | BasicBlock *TailBB = Instr->getParent(); |
Fedor Indutny | f9e09c1 | 2018-03-03 21:40:14 +0000 | [diff] [blame] | 309 | bool IsMustTailCall = CS.isMustTailCall(); |
Jun Bum Lim | 0c99007 | 2017-11-03 20:41:16 +0000 | [diff] [blame] | 310 | |
Florian Hahn | c6c89bf | 2018-01-16 22:13:15 +0000 | [diff] [blame] | 311 | PHINode *CallPN = nullptr; |
Fedor Indutny | f9e09c1 | 2018-03-03 21:40:14 +0000 | [diff] [blame] | 312 | |
| 313 | // `musttail` calls must be followed by optional `bitcast`, and `ret`. The |
| 314 | // split blocks will be terminated right after that so there're no users for |
| 315 | // this phi in a `TailBB`. |
Craig Topper | 3b4ad9c | 2018-03-12 18:40:59 +0000 | [diff] [blame] | 316 | if (!IsMustTailCall && !Instr->use_empty()) |
Florian Hahn | c6c89bf | 2018-01-16 22:13:15 +0000 | [diff] [blame] | 317 | CallPN = PHINode::Create(Instr->getType(), Preds.size(), "phi.call"); |
Jun Bum Lim | 0c99007 | 2017-11-03 20:41:16 +0000 | [diff] [blame] | 318 | |
Florian Hahn | c6c89bf | 2018-01-16 22:13:15 +0000 | [diff] [blame] | 319 | DEBUG(dbgs() << "split call-site : " << *Instr << " into \n"); |
Florian Hahn | b4e3bad | 2018-02-14 13:59:12 +0000 | [diff] [blame] | 320 | |
| 321 | assert(Preds.size() == 2 && "The ValueToValueMaps array has size 2."); |
| 322 | // ValueToValueMapTy is neither copy nor moveable, so we use a simple array |
| 323 | // here. |
| 324 | ValueToValueMapTy ValueToValueMaps[2]; |
| 325 | for (unsigned i = 0; i < Preds.size(); i++) { |
| 326 | BasicBlock *PredBB = Preds[i].first; |
| 327 | BasicBlock *SplitBlock = DuplicateInstructionsInSplitBetween( |
Florian Hahn | 9bc0bc4 | 2018-03-22 15:23:33 +0000 | [diff] [blame] | 328 | TailBB, PredBB, &*std::next(Instr->getIterator()), ValueToValueMaps[i], |
| 329 | DT); |
Florian Hahn | c6c89bf | 2018-01-16 22:13:15 +0000 | [diff] [blame] | 330 | assert(SplitBlock && "Unexpected new basic block split."); |
Jun Bum Lim | 0c99007 | 2017-11-03 20:41:16 +0000 | [diff] [blame] | 331 | |
Florian Hahn | b4e3bad | 2018-02-14 13:59:12 +0000 | [diff] [blame] | 332 | Instruction *NewCI = |
| 333 | &*std::prev(SplitBlock->getTerminator()->getIterator()); |
Florian Hahn | c6c89bf | 2018-01-16 22:13:15 +0000 | [diff] [blame] | 334 | CallSite NewCS(NewCI); |
Florian Hahn | b4e3bad | 2018-02-14 13:59:12 +0000 | [diff] [blame] | 335 | addConditions(NewCS, Preds[i].second); |
Jun Bum Lim | 0c99007 | 2017-11-03 20:41:16 +0000 | [diff] [blame] | 336 | |
Florian Hahn | c6c89bf | 2018-01-16 22:13:15 +0000 | [diff] [blame] | 337 | // Handle PHIs used as arguments in the call-site. |
| 338 | for (PHINode &PN : TailBB->phis()) { |
| 339 | unsigned ArgNo = 0; |
| 340 | for (auto &CI : CS.args()) { |
| 341 | if (&*CI == &PN) { |
| 342 | NewCS.setArgument(ArgNo, PN.getIncomingValueForBlock(SplitBlock)); |
| 343 | } |
| 344 | ++ArgNo; |
Jun Bum Lim | 0c99007 | 2017-11-03 20:41:16 +0000 | [diff] [blame] | 345 | } |
Jun Bum Lim | 0c99007 | 2017-11-03 20:41:16 +0000 | [diff] [blame] | 346 | } |
Florian Hahn | c6c89bf | 2018-01-16 22:13:15 +0000 | [diff] [blame] | 347 | DEBUG(dbgs() << " " << *NewCI << " in " << SplitBlock->getName() |
| 348 | << "\n"); |
| 349 | if (CallPN) |
| 350 | CallPN->addIncoming(NewCI, SplitBlock); |
Fedor Indutny | f9e09c1 | 2018-03-03 21:40:14 +0000 | [diff] [blame] | 351 | |
| 352 | // Clone and place bitcast and return instructions before `TI` |
| 353 | if (IsMustTailCall) |
| 354 | copyMustTailReturn(SplitBlock, Instr, NewCI); |
| 355 | } |
| 356 | |
| 357 | NumCallSiteSplit++; |
| 358 | |
| 359 | // FIXME: remove TI in `copyMustTailReturn` |
| 360 | if (IsMustTailCall) { |
| 361 | // Remove superfluous `br` terminators from the end of the Split blocks |
Fedor Indutny | 364b9c2 | 2018-03-03 22:34:38 +0000 | [diff] [blame] | 362 | // NOTE: Removing terminator removes the SplitBlock from the TailBB's |
| 363 | // predecessors. Therefore we must get complete list of Splits before |
| 364 | // attempting removal. |
| 365 | SmallVector<BasicBlock *, 2> Splits(predecessors((TailBB))); |
| 366 | assert(Splits.size() == 2 && "Expected exactly 2 splits!"); |
| 367 | for (unsigned i = 0; i < Splits.size(); i++) |
| 368 | Splits[i]->getTerminator()->eraseFromParent(); |
Fedor Indutny | f9e09c1 | 2018-03-03 21:40:14 +0000 | [diff] [blame] | 369 | |
| 370 | // Erase the tail block once done with musttail patching |
| 371 | TailBB->eraseFromParent(); |
| 372 | return; |
Jun Bum Lim | 0c99007 | 2017-11-03 20:41:16 +0000 | [diff] [blame] | 373 | } |
| 374 | |
Florian Hahn | b4e3bad | 2018-02-14 13:59:12 +0000 | [diff] [blame] | 375 | auto *OriginalBegin = &*TailBB->begin(); |
Jun Bum Lim | 0c99007 | 2017-11-03 20:41:16 +0000 | [diff] [blame] | 376 | // Replace users of the original call with a PHI mering call-sites split. |
Florian Hahn | c6c89bf | 2018-01-16 22:13:15 +0000 | [diff] [blame] | 377 | if (CallPN) { |
Florian Hahn | b4e3bad | 2018-02-14 13:59:12 +0000 | [diff] [blame] | 378 | CallPN->insertBefore(OriginalBegin); |
Florian Hahn | c6c89bf | 2018-01-16 22:13:15 +0000 | [diff] [blame] | 379 | Instr->replaceAllUsesWith(CallPN); |
Jun Bum Lim | 0c99007 | 2017-11-03 20:41:16 +0000 | [diff] [blame] | 380 | } |
Florian Hahn | c6c89bf | 2018-01-16 22:13:15 +0000 | [diff] [blame] | 381 | |
Florian Hahn | b4e3bad | 2018-02-14 13:59:12 +0000 | [diff] [blame] | 382 | // Remove instructions moved to split blocks from TailBB, from the duplicated |
| 383 | // call instruction to the beginning of the basic block. If an instruction |
| 384 | // has any uses, add a new PHI node to combine the values coming from the |
| 385 | // split blocks. The new PHI nodes are placed before the first original |
| 386 | // instruction, so we do not end up deleting them. By using reverse-order, we |
| 387 | // do not introduce unnecessary PHI nodes for def-use chains from the call |
| 388 | // instruction to the beginning of the block. |
| 389 | auto I = Instr->getReverseIterator(); |
| 390 | while (I != TailBB->rend()) { |
| 391 | Instruction *CurrentI = &*I++; |
| 392 | if (!CurrentI->use_empty()) { |
| 393 | // If an existing PHI has users after the call, there is no need to create |
| 394 | // a new one. |
| 395 | if (isa<PHINode>(CurrentI)) |
| 396 | continue; |
| 397 | PHINode *NewPN = PHINode::Create(CurrentI->getType(), Preds.size()); |
| 398 | for (auto &Mapping : ValueToValueMaps) |
| 399 | NewPN->addIncoming(Mapping[CurrentI], |
| 400 | cast<Instruction>(Mapping[CurrentI])->getParent()); |
| 401 | NewPN->insertBefore(&*TailBB->begin()); |
| 402 | CurrentI->replaceAllUsesWith(NewPN); |
| 403 | } |
| 404 | CurrentI->eraseFromParent(); |
| 405 | // We are done once we handled the first original instruction in TailBB. |
| 406 | if (CurrentI == OriginalBegin) |
| 407 | break; |
| 408 | } |
Jun Bum Lim | 0c99007 | 2017-11-03 20:41:16 +0000 | [diff] [blame] | 409 | } |
| 410 | |
Jun Bum Lim | 0c99007 | 2017-11-03 20:41:16 +0000 | [diff] [blame] | 411 | // Return true if the call-site has an argument which is a PHI with only |
| 412 | // constant incoming values. |
| 413 | static bool isPredicatedOnPHI(CallSite CS) { |
| 414 | Instruction *Instr = CS.getInstruction(); |
| 415 | BasicBlock *Parent = Instr->getParent(); |
Mikael Holmen | 66cf383 | 2017-12-12 07:29:57 +0000 | [diff] [blame] | 416 | if (Instr != Parent->getFirstNonPHIOrDbg()) |
Jun Bum Lim | 0c99007 | 2017-11-03 20:41:16 +0000 | [diff] [blame] | 417 | return false; |
| 418 | |
| 419 | for (auto &BI : *Parent) { |
| 420 | if (PHINode *PN = dyn_cast<PHINode>(&BI)) { |
| 421 | for (auto &I : CS.args()) |
| 422 | if (&*I == PN) { |
| 423 | assert(PN->getNumIncomingValues() == 2 && |
| 424 | "Unexpected number of incoming values"); |
| 425 | if (PN->getIncomingBlock(0) == PN->getIncomingBlock(1)) |
| 426 | return false; |
| 427 | if (PN->getIncomingValue(0) == PN->getIncomingValue(1)) |
| 428 | continue; |
| 429 | if (isa<Constant>(PN->getIncomingValue(0)) && |
| 430 | isa<Constant>(PN->getIncomingValue(1))) |
| 431 | return true; |
| 432 | } |
| 433 | } |
| 434 | break; |
| 435 | } |
| 436 | return false; |
| 437 | } |
| 438 | |
Florian Hahn | 9bc0bc4 | 2018-03-22 15:23:33 +0000 | [diff] [blame] | 439 | static bool tryToSplitOnPHIPredicatedArgument(CallSite CS, DominatorTree *DT) { |
Florian Hahn | 2a266a3 | 2017-11-18 18:14:13 +0000 | [diff] [blame] | 440 | if (!isPredicatedOnPHI(CS)) |
Jun Bum Lim | 0c99007 | 2017-11-03 20:41:16 +0000 | [diff] [blame] | 441 | return false; |
| 442 | |
Florian Hahn | 2a266a3 | 2017-11-18 18:14:13 +0000 | [diff] [blame] | 443 | auto Preds = getTwoPredecessors(CS.getInstruction()->getParent()); |
Florian Hahn | c6c89bf | 2018-01-16 22:13:15 +0000 | [diff] [blame] | 444 | SmallVector<std::pair<BasicBlock *, ConditionsTy>, 2> PredsCS = { |
| 445 | {Preds[0], {}}, {Preds[1], {}}}; |
Florian Hahn | 9bc0bc4 | 2018-03-22 15:23:33 +0000 | [diff] [blame] | 446 | splitCallSite(CS, PredsCS, DT); |
Florian Hahn | 2a266a3 | 2017-11-18 18:14:13 +0000 | [diff] [blame] | 447 | return true; |
| 448 | } |
Jun Bum Lim | 0c99007 | 2017-11-03 20:41:16 +0000 | [diff] [blame] | 449 | |
Florian Hahn | 9bc0bc4 | 2018-03-22 15:23:33 +0000 | [diff] [blame] | 450 | static bool tryToSplitOnPredicatedArgument(CallSite CS, DominatorTree *DT) { |
Florian Hahn | 2a266a3 | 2017-11-18 18:14:13 +0000 | [diff] [blame] | 451 | auto Preds = getTwoPredecessors(CS.getInstruction()->getParent()); |
Florian Hahn | 7e93289 | 2017-12-23 20:02:26 +0000 | [diff] [blame] | 452 | if (Preds[0] == Preds[1]) |
Florian Hahn | 2a266a3 | 2017-11-18 18:14:13 +0000 | [diff] [blame] | 453 | return false; |
| 454 | |
Florian Hahn | c6c89bf | 2018-01-16 22:13:15 +0000 | [diff] [blame] | 455 | SmallVector<std::pair<BasicBlock *, ConditionsTy>, 2> PredsCS; |
| 456 | for (auto *Pred : make_range(Preds.rbegin(), Preds.rend())) { |
| 457 | ConditionsTy Conditions; |
| 458 | recordConditions(CS, Pred, Conditions); |
| 459 | PredsCS.push_back({Pred, Conditions}); |
| 460 | } |
Florian Hahn | 2a266a3 | 2017-11-18 18:14:13 +0000 | [diff] [blame] | 461 | |
Florian Hahn | c6c89bf | 2018-01-16 22:13:15 +0000 | [diff] [blame] | 462 | if (std::all_of(PredsCS.begin(), PredsCS.end(), |
| 463 | [](const std::pair<BasicBlock *, ConditionsTy> &P) { |
| 464 | return P.second.empty(); |
| 465 | })) |
Florian Hahn | beda7d5 | 2017-12-13 03:05:20 +0000 | [diff] [blame] | 466 | return false; |
| 467 | |
Florian Hahn | 9bc0bc4 | 2018-03-22 15:23:33 +0000 | [diff] [blame] | 468 | splitCallSite(CS, PredsCS, DT); |
Jun Bum Lim | 0c99007 | 2017-11-03 20:41:16 +0000 | [diff] [blame] | 469 | return true; |
| 470 | } |
| 471 | |
Florian Hahn | 9bc0bc4 | 2018-03-22 15:23:33 +0000 | [diff] [blame] | 472 | static bool tryToSplitCallSite(CallSite CS, TargetTransformInfo &TTI, |
| 473 | DominatorTree *DT) { |
Florian Hahn | b4e3bad | 2018-02-14 13:59:12 +0000 | [diff] [blame] | 474 | if (!CS.arg_size() || !canSplitCallSite(CS, TTI)) |
Jun Bum Lim | 0c99007 | 2017-11-03 20:41:16 +0000 | [diff] [blame] | 475 | return false; |
Florian Hahn | 9bc0bc4 | 2018-03-22 15:23:33 +0000 | [diff] [blame] | 476 | return tryToSplitOnPredicatedArgument(CS, DT) || |
| 477 | tryToSplitOnPHIPredicatedArgument(CS, DT); |
Jun Bum Lim | 0c99007 | 2017-11-03 20:41:16 +0000 | [diff] [blame] | 478 | } |
| 479 | |
Florian Hahn | b4e3bad | 2018-02-14 13:59:12 +0000 | [diff] [blame] | 480 | static bool doCallSiteSplitting(Function &F, TargetLibraryInfo &TLI, |
Florian Hahn | 9bc0bc4 | 2018-03-22 15:23:33 +0000 | [diff] [blame] | 481 | TargetTransformInfo &TTI, DominatorTree *DT) { |
Jun Bum Lim | 0c99007 | 2017-11-03 20:41:16 +0000 | [diff] [blame] | 482 | bool Changed = false; |
| 483 | for (Function::iterator BI = F.begin(), BE = F.end(); BI != BE;) { |
| 484 | BasicBlock &BB = *BI++; |
Florian Hahn | 517dc51 | 2018-03-06 14:00:58 +0000 | [diff] [blame] | 485 | auto II = BB.getFirstNonPHIOrDbg()->getIterator(); |
| 486 | auto IE = BB.getTerminator()->getIterator(); |
| 487 | // Iterate until we reach the terminator instruction. tryToSplitCallSite |
| 488 | // can replace BB's terminator in case BB is a successor of itself. In that |
| 489 | // case, IE will be invalidated and we also have to check the current |
| 490 | // terminator. |
| 491 | while (II != IE && &*II != BB.getTerminator()) { |
Jun Bum Lim | 0c99007 | 2017-11-03 20:41:16 +0000 | [diff] [blame] | 492 | Instruction *I = &*II++; |
| 493 | CallSite CS(cast<Value>(I)); |
| 494 | if (!CS || isa<IntrinsicInst>(I) || isInstructionTriviallyDead(I, &TLI)) |
| 495 | continue; |
| 496 | |
| 497 | Function *Callee = CS.getCalledFunction(); |
| 498 | if (!Callee || Callee->isDeclaration()) |
| 499 | continue; |
Fedor Indutny | f9e09c1 | 2018-03-03 21:40:14 +0000 | [diff] [blame] | 500 | |
| 501 | // Successful musttail call-site splits result in erased CI and erased BB. |
| 502 | // Check if such path is possible before attempting the splitting. |
| 503 | bool IsMustTail = CS.isMustTailCall(); |
| 504 | |
Florian Hahn | 9bc0bc4 | 2018-03-22 15:23:33 +0000 | [diff] [blame] | 505 | Changed |= tryToSplitCallSite(CS, TTI, DT); |
Fedor Indutny | f9e09c1 | 2018-03-03 21:40:14 +0000 | [diff] [blame] | 506 | |
| 507 | // There're no interesting instructions after this. The call site |
| 508 | // itself might have been erased on splitting. |
| 509 | if (IsMustTail) |
| 510 | break; |
Jun Bum Lim | 0c99007 | 2017-11-03 20:41:16 +0000 | [diff] [blame] | 511 | } |
| 512 | } |
| 513 | return Changed; |
| 514 | } |
| 515 | |
| 516 | namespace { |
| 517 | struct CallSiteSplittingLegacyPass : public FunctionPass { |
| 518 | static char ID; |
| 519 | CallSiteSplittingLegacyPass() : FunctionPass(ID) { |
| 520 | initializeCallSiteSplittingLegacyPassPass(*PassRegistry::getPassRegistry()); |
| 521 | } |
| 522 | |
| 523 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 524 | AU.addRequired<TargetLibraryInfoWrapperPass>(); |
Florian Hahn | b4e3bad | 2018-02-14 13:59:12 +0000 | [diff] [blame] | 525 | AU.addRequired<TargetTransformInfoWrapperPass>(); |
Florian Hahn | 9bc0bc4 | 2018-03-22 15:23:33 +0000 | [diff] [blame] | 526 | AU.addPreserved<DominatorTreeWrapperPass>(); |
Jun Bum Lim | 0c99007 | 2017-11-03 20:41:16 +0000 | [diff] [blame] | 527 | FunctionPass::getAnalysisUsage(AU); |
| 528 | } |
| 529 | |
| 530 | bool runOnFunction(Function &F) override { |
| 531 | if (skipFunction(F)) |
| 532 | return false; |
| 533 | |
| 534 | auto &TLI = getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(); |
Florian Hahn | b4e3bad | 2018-02-14 13:59:12 +0000 | [diff] [blame] | 535 | auto &TTI = getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F); |
Florian Hahn | 9bc0bc4 | 2018-03-22 15:23:33 +0000 | [diff] [blame] | 536 | auto *DTWP = getAnalysisIfAvailable<DominatorTreeWrapperPass>(); |
| 537 | return doCallSiteSplitting(F, TLI, TTI, |
| 538 | DTWP ? &DTWP->getDomTree() : nullptr); |
Jun Bum Lim | 0c99007 | 2017-11-03 20:41:16 +0000 | [diff] [blame] | 539 | } |
| 540 | }; |
| 541 | } // namespace |
| 542 | |
| 543 | char CallSiteSplittingLegacyPass::ID = 0; |
| 544 | INITIALIZE_PASS_BEGIN(CallSiteSplittingLegacyPass, "callsite-splitting", |
| 545 | "Call-site splitting", false, false) |
| 546 | INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass) |
Florian Hahn | b4e3bad | 2018-02-14 13:59:12 +0000 | [diff] [blame] | 547 | INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass) |
Jun Bum Lim | 0c99007 | 2017-11-03 20:41:16 +0000 | [diff] [blame] | 548 | INITIALIZE_PASS_END(CallSiteSplittingLegacyPass, "callsite-splitting", |
| 549 | "Call-site splitting", false, false) |
| 550 | FunctionPass *llvm::createCallSiteSplittingPass() { |
| 551 | return new CallSiteSplittingLegacyPass(); |
| 552 | } |
| 553 | |
| 554 | PreservedAnalyses CallSiteSplittingPass::run(Function &F, |
| 555 | FunctionAnalysisManager &AM) { |
| 556 | auto &TLI = AM.getResult<TargetLibraryAnalysis>(F); |
Florian Hahn | b4e3bad | 2018-02-14 13:59:12 +0000 | [diff] [blame] | 557 | auto &TTI = AM.getResult<TargetIRAnalysis>(F); |
Florian Hahn | 9bc0bc4 | 2018-03-22 15:23:33 +0000 | [diff] [blame] | 558 | auto *DT = AM.getCachedResult<DominatorTreeAnalysis>(F); |
Jun Bum Lim | 0c99007 | 2017-11-03 20:41:16 +0000 | [diff] [blame] | 559 | |
Florian Hahn | 9bc0bc4 | 2018-03-22 15:23:33 +0000 | [diff] [blame] | 560 | if (!doCallSiteSplitting(F, TLI, TTI, DT)) |
Jun Bum Lim | 0c99007 | 2017-11-03 20:41:16 +0000 | [diff] [blame] | 561 | return PreservedAnalyses::all(); |
| 562 | PreservedAnalyses PA; |
Florian Hahn | 9bc0bc4 | 2018-03-22 15:23:33 +0000 | [diff] [blame] | 563 | PA.preserve<DominatorTreeAnalysis>(); |
Jun Bum Lim | 0c99007 | 2017-11-03 20:41:16 +0000 | [diff] [blame] | 564 | return PA; |
| 565 | } |