| 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" |
| Jun Bum Lim | 0c99007 | 2017-11-03 20:41:16 +0000 | [diff] [blame] | 63 | #include "llvm/IR/IntrinsicInst.h" |
| 64 | #include "llvm/IR/PatternMatch.h" |
| 65 | #include "llvm/Support/Debug.h" |
| 66 | #include "llvm/Transforms/Scalar.h" |
| 67 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
| Florian Hahn | b4e3bad | 2018-02-14 13:59:12 +0000 | [diff] [blame] | 68 | #include "llvm/Transforms/Utils/Cloning.h" |
| Jun Bum Lim | 0c99007 | 2017-11-03 20:41:16 +0000 | [diff] [blame] | 69 | #include "llvm/Transforms/Utils/Local.h" |
| 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()) { |
| 100 | if (&*I == Op) |
| 101 | CS.setArgument(ArgNo, ConstValue); |
| 102 | ++ArgNo; |
| 103 | } |
| 104 | } |
| 105 | |
| Florian Hahn | 2a266a3 | 2017-11-18 18:14:13 +0000 | [diff] [blame] | 106 | static bool isCondRelevantToAnyCallArgument(ICmpInst *Cmp, CallSite CS) { |
| 107 | assert(isa<Constant>(Cmp->getOperand(1)) && "Expected a constant operand."); |
| 108 | Value *Op0 = Cmp->getOperand(0); |
| 109 | unsigned ArgNo = 0; |
| 110 | for (CallSite::arg_iterator I = CS.arg_begin(), E = CS.arg_end(); I != E; |
| 111 | ++I, ++ArgNo) { |
| 112 | // Don't consider constant or arguments that are already known non-null. |
| 113 | if (isa<Constant>(*I) || CS.paramHasAttr(ArgNo, Attribute::NonNull)) |
| 114 | continue; |
| 115 | |
| 116 | if (*I == Op0) |
| 117 | return true; |
| 118 | } |
| 119 | return false; |
| 120 | } |
| 121 | |
| Florian Hahn | c6c89bf | 2018-01-16 22:13:15 +0000 | [diff] [blame] | 122 | typedef std::pair<ICmpInst *, unsigned> ConditionTy; |
| 123 | typedef SmallVector<ConditionTy, 2> ConditionsTy; |
| 124 | |
| Florian Hahn | beda7d5 | 2017-12-13 03:05:20 +0000 | [diff] [blame] | 125 | /// If From has a conditional jump to To, add the condition to Conditions, |
| 126 | /// if it is relevant to any argument at CS. |
| Florian Hahn | c6c89bf | 2018-01-16 22:13:15 +0000 | [diff] [blame] | 127 | static void recordCondition(CallSite CS, BasicBlock *From, BasicBlock *To, |
| 128 | ConditionsTy &Conditions) { |
| Florian Hahn | beda7d5 | 2017-12-13 03:05:20 +0000 | [diff] [blame] | 129 | auto *BI = dyn_cast<BranchInst>(From->getTerminator()); |
| 130 | if (!BI || !BI->isConditional()) |
| 131 | return; |
| Florian Hahn | 2a266a3 | 2017-11-18 18:14:13 +0000 | [diff] [blame] | 132 | |
| Florian Hahn | beda7d5 | 2017-12-13 03:05:20 +0000 | [diff] [blame] | 133 | CmpInst::Predicate Pred; |
| 134 | Value *Cond = BI->getCondition(); |
| 135 | if (!match(Cond, m_ICmp(Pred, m_Value(), m_Constant()))) |
| 136 | return; |
| 137 | |
| 138 | ICmpInst *Cmp = cast<ICmpInst>(Cond); |
| 139 | if (Pred == ICmpInst::ICMP_EQ || Pred == ICmpInst::ICMP_NE) |
| 140 | if (isCondRelevantToAnyCallArgument(Cmp, CS)) |
| 141 | Conditions.push_back({Cmp, From->getTerminator()->getSuccessor(0) == To |
| 142 | ? Pred |
| 143 | : Cmp->getInversePredicate()}); |
| Florian Hahn | 2a266a3 | 2017-11-18 18:14:13 +0000 | [diff] [blame] | 144 | } |
| 145 | |
| Florian Hahn | beda7d5 | 2017-12-13 03:05:20 +0000 | [diff] [blame] | 146 | /// Record ICmp conditions relevant to any argument in CS following Pred's |
| 147 | /// single successors. If there are conflicting conditions along a path, like |
| 148 | /// x == 1 and x == 0, the first condition will be used. |
| Florian Hahn | c6c89bf | 2018-01-16 22:13:15 +0000 | [diff] [blame] | 149 | static void recordConditions(CallSite CS, BasicBlock *Pred, |
| 150 | ConditionsTy &Conditions) { |
| Florian Hahn | beda7d5 | 2017-12-13 03:05:20 +0000 | [diff] [blame] | 151 | recordCondition(CS, Pred, CS.getInstruction()->getParent(), Conditions); |
| 152 | BasicBlock *From = Pred; |
| 153 | BasicBlock *To = Pred; |
| Florian Hahn | 212afb9 | 2018-01-26 10:36:50 +0000 | [diff] [blame] | 154 | SmallPtrSet<BasicBlock *, 4> Visited; |
| Florian Hahn | beda7d5 | 2017-12-13 03:05:20 +0000 | [diff] [blame] | 155 | while (!Visited.count(From->getSinglePredecessor()) && |
| 156 | (From = From->getSinglePredecessor())) { |
| 157 | recordCondition(CS, From, To, Conditions); |
| Florian Hahn | 212afb9 | 2018-01-26 10:36:50 +0000 | [diff] [blame] | 158 | Visited.insert(From); |
| Florian Hahn | beda7d5 | 2017-12-13 03:05:20 +0000 | [diff] [blame] | 159 | To = From; |
| 160 | } |
| 161 | } |
| Jun Bum Lim | 0c99007 | 2017-11-03 20:41:16 +0000 | [diff] [blame] | 162 | |
| Florian Hahn | c6c89bf | 2018-01-16 22:13:15 +0000 | [diff] [blame] | 163 | static void addConditions(CallSite CS, const ConditionsTy &Conditions) { |
| Florian Hahn | beda7d5 | 2017-12-13 03:05:20 +0000 | [diff] [blame] | 164 | for (auto &Cond : Conditions) { |
| 165 | Value *Arg = Cond.first->getOperand(0); |
| 166 | Constant *ConstVal = cast<Constant>(Cond.first->getOperand(1)); |
| 167 | if (Cond.second == ICmpInst::ICMP_EQ) |
| Florian Hahn | c6c89bf | 2018-01-16 22:13:15 +0000 | [diff] [blame] | 168 | setConstantInArgument(CS, Arg, ConstVal); |
| Florian Hahn | beda7d5 | 2017-12-13 03:05:20 +0000 | [diff] [blame] | 169 | else if (ConstVal->getType()->isPointerTy() && ConstVal->isNullValue()) { |
| 170 | assert(Cond.second == ICmpInst::ICMP_NE); |
| Florian Hahn | c6c89bf | 2018-01-16 22:13:15 +0000 | [diff] [blame] | 171 | addNonNullAttribute(CS, Arg); |
| Jun Bum Lim | 0c99007 | 2017-11-03 20:41:16 +0000 | [diff] [blame] | 172 | } |
| 173 | } |
| Florian Hahn | beda7d5 | 2017-12-13 03:05:20 +0000 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | static SmallVector<BasicBlock *, 2> getTwoPredecessors(BasicBlock *BB) { |
| 177 | SmallVector<BasicBlock *, 2> Preds(predecessors((BB))); |
| 178 | assert(Preds.size() == 2 && "Expected exactly 2 predecessors!"); |
| 179 | return Preds; |
| Jun Bum Lim | 0c99007 | 2017-11-03 20:41:16 +0000 | [diff] [blame] | 180 | } |
| 181 | |
| Florian Hahn | b4e3bad | 2018-02-14 13:59:12 +0000 | [diff] [blame] | 182 | static bool canSplitCallSite(CallSite CS, TargetTransformInfo &TTI) { |
| Jun Bum Lim | 0c99007 | 2017-11-03 20:41:16 +0000 | [diff] [blame] | 183 | // FIXME: As of now we handle only CallInst. InvokeInst could be handled |
| 184 | // without too much effort. |
| 185 | Instruction *Instr = CS.getInstruction(); |
| 186 | if (!isa<CallInst>(Instr)) |
| 187 | return false; |
| 188 | |
| Jun Bum Lim | 0c99007 | 2017-11-03 20:41:16 +0000 | [diff] [blame] | 189 | BasicBlock *CallSiteBB = Instr->getParent(); |
| Florian Hahn | b4e3bad | 2018-02-14 13:59:12 +0000 | [diff] [blame] | 190 | // Allow splitting a call-site only when the CodeSize cost of the |
| 191 | // instructions before the call is less then DuplicationThreshold. The |
| 192 | // instructions before the call will be duplicated in the split blocks and |
| 193 | // corresponding uses will be updated. |
| 194 | unsigned Cost = 0; |
| 195 | for (auto &InstBeforeCall : |
| 196 | llvm::make_range(CallSiteBB->begin(), Instr->getIterator())) { |
| 197 | Cost += TTI.getInstructionCost(&InstBeforeCall, |
| 198 | TargetTransformInfo::TCK_CodeSize); |
| 199 | if (Cost >= DuplicationThreshold) |
| 200 | return false; |
| 201 | } |
| Jun Bum Lim | 0c99007 | 2017-11-03 20:41:16 +0000 | [diff] [blame] | 202 | |
| Florian Hahn | 2a266a3 | 2017-11-18 18:14:13 +0000 | [diff] [blame] | 203 | // Need 2 predecessors and cannot split an edge from an IndirectBrInst. |
| 204 | SmallVector<BasicBlock *, 2> Preds(predecessors(CallSiteBB)); |
| 205 | if (Preds.size() != 2 || isa<IndirectBrInst>(Preds[0]->getTerminator()) || |
| Jun Bum Lim | 0c99007 | 2017-11-03 20:41:16 +0000 | [diff] [blame] | 206 | isa<IndirectBrInst>(Preds[1]->getTerminator())) |
| 207 | return false; |
| 208 | |
| 209 | return CallSiteBB->canSplitPredecessors(); |
| 210 | } |
| 211 | |
| Florian Hahn | c6c89bf | 2018-01-16 22:13:15 +0000 | [diff] [blame] | 212 | /// Return true if the CS is split into its new predecessors. |
| 213 | /// |
| 214 | /// For each (predecessor, conditions from predecessors) pair, it will split the |
| 215 | /// basic block containing the call site, hook it up to the predecessor and |
| 216 | /// replace the call instruction with new call instructions, which contain |
| 217 | /// constraints based on the conditions from their predecessors. |
| Florian Hahn | 7e93289 | 2017-12-23 20:02:26 +0000 | [diff] [blame] | 218 | /// 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] | 219 | /// be split. In this case, Preds for Tail is [(Header, a == null), |
| 220 | /// (TBB, a != null, b == null)]. Tail is replaced by 2 split blocks, containing |
| 221 | /// CallInst1, which has constraints based on the conditions from Head and |
| 222 | /// CallInst2, which has constraints based on the conditions coming from TBB. |
| Jun Bum Lim | 0c99007 | 2017-11-03 20:41:16 +0000 | [diff] [blame] | 223 | /// |
| Florian Hahn | 7e93289 | 2017-12-23 20:02:26 +0000 | [diff] [blame] | 224 | /// From : |
| Jun Bum Lim | 0c99007 | 2017-11-03 20:41:16 +0000 | [diff] [blame] | 225 | /// |
| 226 | /// Header: |
| 227 | /// %c = icmp eq i32* %a, null |
| 228 | /// br i1 %c %Tail, %TBB |
| 229 | /// TBB: |
| 230 | /// %c2 = icmp eq i32* %b, null |
| 231 | /// br i1 %c %Tail, %End |
| 232 | /// Tail: |
| 233 | /// %ca = call i1 @callee (i32* %a, i32* %b) |
| 234 | /// |
| 235 | /// to : |
| 236 | /// |
| 237 | /// Header: // PredBB1 is Header |
| 238 | /// %c = icmp eq i32* %a, null |
| 239 | /// br i1 %c %Tail-split1, %TBB |
| 240 | /// TBB: // PredBB2 is TBB |
| 241 | /// %c2 = icmp eq i32* %b, null |
| 242 | /// br i1 %c %Tail-split2, %End |
| 243 | /// Tail-split1: |
| 244 | /// %ca1 = call @callee (i32* null, i32* %b) // CallInst1 |
| 245 | /// br %Tail |
| 246 | /// Tail-split2: |
| 247 | /// %ca2 = call @callee (i32* nonnull %a, i32* null) // CallInst2 |
| 248 | /// br %Tail |
| 249 | /// Tail: |
| 250 | /// %p = phi i1 [%ca1, %Tail-split1],[%ca2, %Tail-split2] |
| 251 | /// |
| Florian Hahn | 7e93289 | 2017-12-23 20:02:26 +0000 | [diff] [blame] | 252 | /// Note that in case any arguments at the call-site are constrained by its |
| 253 | /// predecessors, new call-sites with more constrained arguments will be |
| 254 | /// created in createCallSitesOnPredicatedArgument(). |
| Florian Hahn | c6c89bf | 2018-01-16 22:13:15 +0000 | [diff] [blame] | 255 | static void splitCallSite( |
| 256 | CallSite CS, |
| 257 | const SmallVectorImpl<std::pair<BasicBlock *, ConditionsTy>> &Preds) { |
| Jun Bum Lim | 0c99007 | 2017-11-03 20:41:16 +0000 | [diff] [blame] | 258 | Instruction *Instr = CS.getInstruction(); |
| 259 | BasicBlock *TailBB = Instr->getParent(); |
| Jun Bum Lim | 0c99007 | 2017-11-03 20:41:16 +0000 | [diff] [blame] | 260 | |
| Florian Hahn | c6c89bf | 2018-01-16 22:13:15 +0000 | [diff] [blame] | 261 | PHINode *CallPN = nullptr; |
| 262 | if (Instr->getNumUses()) |
| 263 | CallPN = PHINode::Create(Instr->getType(), Preds.size(), "phi.call"); |
| Jun Bum Lim | 0c99007 | 2017-11-03 20:41:16 +0000 | [diff] [blame] | 264 | |
| Florian Hahn | c6c89bf | 2018-01-16 22:13:15 +0000 | [diff] [blame] | 265 | DEBUG(dbgs() << "split call-site : " << *Instr << " into \n"); |
| Florian Hahn | b4e3bad | 2018-02-14 13:59:12 +0000 | [diff] [blame] | 266 | |
| 267 | assert(Preds.size() == 2 && "The ValueToValueMaps array has size 2."); |
| 268 | // ValueToValueMapTy is neither copy nor moveable, so we use a simple array |
| 269 | // here. |
| 270 | ValueToValueMapTy ValueToValueMaps[2]; |
| 271 | for (unsigned i = 0; i < Preds.size(); i++) { |
| 272 | BasicBlock *PredBB = Preds[i].first; |
| 273 | BasicBlock *SplitBlock = DuplicateInstructionsInSplitBetween( |
| 274 | TailBB, PredBB, &*std::next(Instr->getIterator()), ValueToValueMaps[i]); |
| Florian Hahn | c6c89bf | 2018-01-16 22:13:15 +0000 | [diff] [blame] | 275 | assert(SplitBlock && "Unexpected new basic block split."); |
| Jun Bum Lim | 0c99007 | 2017-11-03 20:41:16 +0000 | [diff] [blame] | 276 | |
| Florian Hahn | b4e3bad | 2018-02-14 13:59:12 +0000 | [diff] [blame] | 277 | Instruction *NewCI = |
| 278 | &*std::prev(SplitBlock->getTerminator()->getIterator()); |
| Florian Hahn | c6c89bf | 2018-01-16 22:13:15 +0000 | [diff] [blame] | 279 | CallSite NewCS(NewCI); |
| Florian Hahn | b4e3bad | 2018-02-14 13:59:12 +0000 | [diff] [blame] | 280 | addConditions(NewCS, Preds[i].second); |
| Jun Bum Lim | 0c99007 | 2017-11-03 20:41:16 +0000 | [diff] [blame] | 281 | |
| Florian Hahn | c6c89bf | 2018-01-16 22:13:15 +0000 | [diff] [blame] | 282 | // Handle PHIs used as arguments in the call-site. |
| 283 | for (PHINode &PN : TailBB->phis()) { |
| 284 | unsigned ArgNo = 0; |
| 285 | for (auto &CI : CS.args()) { |
| 286 | if (&*CI == &PN) { |
| 287 | NewCS.setArgument(ArgNo, PN.getIncomingValueForBlock(SplitBlock)); |
| 288 | } |
| 289 | ++ArgNo; |
| Jun Bum Lim | 0c99007 | 2017-11-03 20:41:16 +0000 | [diff] [blame] | 290 | } |
| Jun Bum Lim | 0c99007 | 2017-11-03 20:41:16 +0000 | [diff] [blame] | 291 | } |
| Florian Hahn | c6c89bf | 2018-01-16 22:13:15 +0000 | [diff] [blame] | 292 | DEBUG(dbgs() << " " << *NewCI << " in " << SplitBlock->getName() |
| 293 | << "\n"); |
| 294 | if (CallPN) |
| 295 | CallPN->addIncoming(NewCI, SplitBlock); |
| Jun Bum Lim | 0c99007 | 2017-11-03 20:41:16 +0000 | [diff] [blame] | 296 | } |
| 297 | |
| Florian Hahn | b4e3bad | 2018-02-14 13:59:12 +0000 | [diff] [blame] | 298 | auto *OriginalBegin = &*TailBB->begin(); |
| Jun Bum Lim | 0c99007 | 2017-11-03 20:41:16 +0000 | [diff] [blame] | 299 | // 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] | 300 | if (CallPN) { |
| Florian Hahn | b4e3bad | 2018-02-14 13:59:12 +0000 | [diff] [blame] | 301 | CallPN->insertBefore(OriginalBegin); |
| Florian Hahn | c6c89bf | 2018-01-16 22:13:15 +0000 | [diff] [blame] | 302 | Instr->replaceAllUsesWith(CallPN); |
| Jun Bum Lim | 0c99007 | 2017-11-03 20:41:16 +0000 | [diff] [blame] | 303 | } |
| Florian Hahn | c6c89bf | 2018-01-16 22:13:15 +0000 | [diff] [blame] | 304 | |
| Florian Hahn | b4e3bad | 2018-02-14 13:59:12 +0000 | [diff] [blame] | 305 | // Remove instructions moved to split blocks from TailBB, from the duplicated |
| 306 | // call instruction to the beginning of the basic block. If an instruction |
| 307 | // has any uses, add a new PHI node to combine the values coming from the |
| 308 | // split blocks. The new PHI nodes are placed before the first original |
| 309 | // instruction, so we do not end up deleting them. By using reverse-order, we |
| 310 | // do not introduce unnecessary PHI nodes for def-use chains from the call |
| 311 | // instruction to the beginning of the block. |
| 312 | auto I = Instr->getReverseIterator(); |
| 313 | while (I != TailBB->rend()) { |
| 314 | Instruction *CurrentI = &*I++; |
| 315 | if (!CurrentI->use_empty()) { |
| 316 | // If an existing PHI has users after the call, there is no need to create |
| 317 | // a new one. |
| 318 | if (isa<PHINode>(CurrentI)) |
| 319 | continue; |
| 320 | PHINode *NewPN = PHINode::Create(CurrentI->getType(), Preds.size()); |
| 321 | for (auto &Mapping : ValueToValueMaps) |
| 322 | NewPN->addIncoming(Mapping[CurrentI], |
| 323 | cast<Instruction>(Mapping[CurrentI])->getParent()); |
| 324 | NewPN->insertBefore(&*TailBB->begin()); |
| 325 | CurrentI->replaceAllUsesWith(NewPN); |
| 326 | } |
| 327 | CurrentI->eraseFromParent(); |
| 328 | // We are done once we handled the first original instruction in TailBB. |
| 329 | if (CurrentI == OriginalBegin) |
| 330 | break; |
| 331 | } |
| 332 | |
| Jun Bum Lim | 0c99007 | 2017-11-03 20:41:16 +0000 | [diff] [blame] | 333 | NumCallSiteSplit++; |
| 334 | } |
| 335 | |
| Jun Bum Lim | 0c99007 | 2017-11-03 20:41:16 +0000 | [diff] [blame] | 336 | // Return true if the call-site has an argument which is a PHI with only |
| 337 | // constant incoming values. |
| 338 | static bool isPredicatedOnPHI(CallSite CS) { |
| 339 | Instruction *Instr = CS.getInstruction(); |
| 340 | BasicBlock *Parent = Instr->getParent(); |
| Mikael Holmen | 66cf383 | 2017-12-12 07:29:57 +0000 | [diff] [blame] | 341 | if (Instr != Parent->getFirstNonPHIOrDbg()) |
| Jun Bum Lim | 0c99007 | 2017-11-03 20:41:16 +0000 | [diff] [blame] | 342 | return false; |
| 343 | |
| 344 | for (auto &BI : *Parent) { |
| 345 | if (PHINode *PN = dyn_cast<PHINode>(&BI)) { |
| 346 | for (auto &I : CS.args()) |
| 347 | if (&*I == PN) { |
| 348 | assert(PN->getNumIncomingValues() == 2 && |
| 349 | "Unexpected number of incoming values"); |
| 350 | if (PN->getIncomingBlock(0) == PN->getIncomingBlock(1)) |
| 351 | return false; |
| 352 | if (PN->getIncomingValue(0) == PN->getIncomingValue(1)) |
| 353 | continue; |
| 354 | if (isa<Constant>(PN->getIncomingValue(0)) && |
| 355 | isa<Constant>(PN->getIncomingValue(1))) |
| 356 | return true; |
| 357 | } |
| 358 | } |
| 359 | break; |
| 360 | } |
| 361 | return false; |
| 362 | } |
| 363 | |
| Florian Hahn | 2a266a3 | 2017-11-18 18:14:13 +0000 | [diff] [blame] | 364 | static bool tryToSplitOnPHIPredicatedArgument(CallSite CS) { |
| 365 | if (!isPredicatedOnPHI(CS)) |
| Jun Bum Lim | 0c99007 | 2017-11-03 20:41:16 +0000 | [diff] [blame] | 366 | return false; |
| 367 | |
| Florian Hahn | 2a266a3 | 2017-11-18 18:14:13 +0000 | [diff] [blame] | 368 | auto Preds = getTwoPredecessors(CS.getInstruction()->getParent()); |
| Florian Hahn | c6c89bf | 2018-01-16 22:13:15 +0000 | [diff] [blame] | 369 | SmallVector<std::pair<BasicBlock *, ConditionsTy>, 2> PredsCS = { |
| 370 | {Preds[0], {}}, {Preds[1], {}}}; |
| 371 | splitCallSite(CS, PredsCS); |
| Florian Hahn | 2a266a3 | 2017-11-18 18:14:13 +0000 | [diff] [blame] | 372 | return true; |
| 373 | } |
| Jun Bum Lim | 0c99007 | 2017-11-03 20:41:16 +0000 | [diff] [blame] | 374 | |
| Florian Hahn | 7e93289 | 2017-12-23 20:02:26 +0000 | [diff] [blame] | 375 | static bool tryToSplitOnPredicatedArgument(CallSite CS) { |
| Florian Hahn | 2a266a3 | 2017-11-18 18:14:13 +0000 | [diff] [blame] | 376 | auto Preds = getTwoPredecessors(CS.getInstruction()->getParent()); |
| Florian Hahn | 7e93289 | 2017-12-23 20:02:26 +0000 | [diff] [blame] | 377 | if (Preds[0] == Preds[1]) |
| Florian Hahn | 2a266a3 | 2017-11-18 18:14:13 +0000 | [diff] [blame] | 378 | return false; |
| 379 | |
| Florian Hahn | c6c89bf | 2018-01-16 22:13:15 +0000 | [diff] [blame] | 380 | SmallVector<std::pair<BasicBlock *, ConditionsTy>, 2> PredsCS; |
| 381 | for (auto *Pred : make_range(Preds.rbegin(), Preds.rend())) { |
| 382 | ConditionsTy Conditions; |
| 383 | recordConditions(CS, Pred, Conditions); |
| 384 | PredsCS.push_back({Pred, Conditions}); |
| 385 | } |
| Florian Hahn | 2a266a3 | 2017-11-18 18:14:13 +0000 | [diff] [blame] | 386 | |
| Florian Hahn | c6c89bf | 2018-01-16 22:13:15 +0000 | [diff] [blame] | 387 | if (std::all_of(PredsCS.begin(), PredsCS.end(), |
| 388 | [](const std::pair<BasicBlock *, ConditionsTy> &P) { |
| 389 | return P.second.empty(); |
| 390 | })) |
| Florian Hahn | beda7d5 | 2017-12-13 03:05:20 +0000 | [diff] [blame] | 391 | return false; |
| 392 | |
| Florian Hahn | c6c89bf | 2018-01-16 22:13:15 +0000 | [diff] [blame] | 393 | splitCallSite(CS, PredsCS); |
| Jun Bum Lim | 0c99007 | 2017-11-03 20:41:16 +0000 | [diff] [blame] | 394 | return true; |
| 395 | } |
| 396 | |
| Florian Hahn | b4e3bad | 2018-02-14 13:59:12 +0000 | [diff] [blame] | 397 | static bool tryToSplitCallSite(CallSite CS, TargetTransformInfo &TTI) { |
| 398 | if (!CS.arg_size() || !canSplitCallSite(CS, TTI)) |
| Jun Bum Lim | 0c99007 | 2017-11-03 20:41:16 +0000 | [diff] [blame] | 399 | return false; |
| Florian Hahn | 7e93289 | 2017-12-23 20:02:26 +0000 | [diff] [blame] | 400 | return tryToSplitOnPredicatedArgument(CS) || |
| Florian Hahn | 2a266a3 | 2017-11-18 18:14:13 +0000 | [diff] [blame] | 401 | tryToSplitOnPHIPredicatedArgument(CS); |
| Jun Bum Lim | 0c99007 | 2017-11-03 20:41:16 +0000 | [diff] [blame] | 402 | } |
| 403 | |
| Florian Hahn | b4e3bad | 2018-02-14 13:59:12 +0000 | [diff] [blame] | 404 | static bool doCallSiteSplitting(Function &F, TargetLibraryInfo &TLI, |
| 405 | TargetTransformInfo &TTI) { |
| Jun Bum Lim | 0c99007 | 2017-11-03 20:41:16 +0000 | [diff] [blame] | 406 | bool Changed = false; |
| 407 | for (Function::iterator BI = F.begin(), BE = F.end(); BI != BE;) { |
| 408 | BasicBlock &BB = *BI++; |
| 409 | for (BasicBlock::iterator II = BB.begin(), IE = BB.end(); II != IE;) { |
| 410 | Instruction *I = &*II++; |
| 411 | CallSite CS(cast<Value>(I)); |
| 412 | if (!CS || isa<IntrinsicInst>(I) || isInstructionTriviallyDead(I, &TLI)) |
| 413 | continue; |
| 414 | |
| 415 | Function *Callee = CS.getCalledFunction(); |
| 416 | if (!Callee || Callee->isDeclaration()) |
| 417 | continue; |
| Florian Hahn | b4e3bad | 2018-02-14 13:59:12 +0000 | [diff] [blame] | 418 | Changed |= tryToSplitCallSite(CS, TTI); |
| Jun Bum Lim | 0c99007 | 2017-11-03 20:41:16 +0000 | [diff] [blame] | 419 | } |
| 420 | } |
| 421 | return Changed; |
| 422 | } |
| 423 | |
| 424 | namespace { |
| 425 | struct CallSiteSplittingLegacyPass : public FunctionPass { |
| 426 | static char ID; |
| 427 | CallSiteSplittingLegacyPass() : FunctionPass(ID) { |
| 428 | initializeCallSiteSplittingLegacyPassPass(*PassRegistry::getPassRegistry()); |
| 429 | } |
| 430 | |
| 431 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 432 | AU.addRequired<TargetLibraryInfoWrapperPass>(); |
| Florian Hahn | b4e3bad | 2018-02-14 13:59:12 +0000 | [diff] [blame] | 433 | AU.addRequired<TargetTransformInfoWrapperPass>(); |
| Jun Bum Lim | 0c99007 | 2017-11-03 20:41:16 +0000 | [diff] [blame] | 434 | FunctionPass::getAnalysisUsage(AU); |
| 435 | } |
| 436 | |
| 437 | bool runOnFunction(Function &F) override { |
| 438 | if (skipFunction(F)) |
| 439 | return false; |
| 440 | |
| 441 | auto &TLI = getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(); |
| Florian Hahn | b4e3bad | 2018-02-14 13:59:12 +0000 | [diff] [blame] | 442 | auto &TTI = getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F); |
| 443 | return doCallSiteSplitting(F, TLI, TTI); |
| Jun Bum Lim | 0c99007 | 2017-11-03 20:41:16 +0000 | [diff] [blame] | 444 | } |
| 445 | }; |
| 446 | } // namespace |
| 447 | |
| 448 | char CallSiteSplittingLegacyPass::ID = 0; |
| 449 | INITIALIZE_PASS_BEGIN(CallSiteSplittingLegacyPass, "callsite-splitting", |
| 450 | "Call-site splitting", false, false) |
| 451 | INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass) |
| Florian Hahn | b4e3bad | 2018-02-14 13:59:12 +0000 | [diff] [blame] | 452 | INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass) |
| Jun Bum Lim | 0c99007 | 2017-11-03 20:41:16 +0000 | [diff] [blame] | 453 | INITIALIZE_PASS_END(CallSiteSplittingLegacyPass, "callsite-splitting", |
| 454 | "Call-site splitting", false, false) |
| 455 | FunctionPass *llvm::createCallSiteSplittingPass() { |
| 456 | return new CallSiteSplittingLegacyPass(); |
| 457 | } |
| 458 | |
| 459 | PreservedAnalyses CallSiteSplittingPass::run(Function &F, |
| 460 | FunctionAnalysisManager &AM) { |
| 461 | auto &TLI = AM.getResult<TargetLibraryAnalysis>(F); |
| Florian Hahn | b4e3bad | 2018-02-14 13:59:12 +0000 | [diff] [blame] | 462 | auto &TTI = AM.getResult<TargetIRAnalysis>(F); |
| Jun Bum Lim | 0c99007 | 2017-11-03 20:41:16 +0000 | [diff] [blame] | 463 | |
| Florian Hahn | b4e3bad | 2018-02-14 13:59:12 +0000 | [diff] [blame] | 464 | if (!doCallSiteSplitting(F, TLI, TTI)) |
| Jun Bum Lim | 0c99007 | 2017-11-03 20:41:16 +0000 | [diff] [blame] | 465 | return PreservedAnalyses::all(); |
| 466 | PreservedAnalyses PA; |
| 467 | return PA; |
| 468 | } |