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