Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 1 | //===- PlaceSafepoints.cpp - Place GC Safepoints --------------------------===// |
| 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 | // Place garbage collection safepoints at appropriate locations in the IR. This |
| 11 | // does not make relocation semantics or variable liveness explicit. That's |
| 12 | // done by RewriteStatepointsForGC. |
| 13 | // |
Philip Reames | d4a912f | 2015-02-09 22:44:03 +0000 | [diff] [blame] | 14 | // Terminology: |
| 15 | // - A call is said to be "parseable" if there is a stack map generated for the |
| 16 | // return PC of the call. A runtime can determine where values listed in the |
| 17 | // deopt arguments and (after RewriteStatepointsForGC) gc arguments are located |
| 18 | // on the stack when the code is suspended inside such a call. Every parse |
NAKAMURA Takumi | fb3bd71 | 2015-05-25 01:43:23 +0000 | [diff] [blame] | 19 | // point is represented by a call wrapped in an gc.statepoint intrinsic. |
Philip Reames | d4a912f | 2015-02-09 22:44:03 +0000 | [diff] [blame] | 20 | // - A "poll" is an explicit check in the generated code to determine if the |
| 21 | // runtime needs the generated code to cooperate by calling a helper routine |
| 22 | // and thus suspending its execution at a known state. The call to the helper |
| 23 | // routine will be parseable. The (gc & runtime specific) logic of a poll is |
| 24 | // assumed to be provided in a function of the name "gc.safepoint_poll". |
| 25 | // |
| 26 | // We aim to insert polls such that running code can quickly be brought to a |
| 27 | // well defined state for inspection by the collector. In the current |
| 28 | // implementation, this is done via the insertion of poll sites at method entry |
| 29 | // and the backedge of most loops. We try to avoid inserting more polls than |
Benjamin Kramer | df005cb | 2015-08-08 18:27:36 +0000 | [diff] [blame] | 30 | // are necessary to ensure a finite period between poll sites. This is not |
Philip Reames | d4a912f | 2015-02-09 22:44:03 +0000 | [diff] [blame] | 31 | // because the poll itself is expensive in the generated code; it's not. Polls |
| 32 | // do tend to impact the optimizer itself in negative ways; we'd like to avoid |
| 33 | // perturbing the optimization of the method as much as we can. |
| 34 | // |
| 35 | // We also need to make most call sites parseable. The callee might execute a |
| 36 | // poll (or otherwise be inspected by the GC). If so, the entire stack |
| 37 | // (including the suspended frame of the current method) must be parseable. |
| 38 | // |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 39 | // This pass will insert: |
Philip Reames | d4a912f | 2015-02-09 22:44:03 +0000 | [diff] [blame] | 40 | // - Call parse points ("call safepoints") for any call which may need to |
| 41 | // reach a safepoint during the execution of the callee function. |
| 42 | // - Backedge safepoint polls and entry safepoint polls to ensure that |
| 43 | // executing code reaches a safepoint poll in a finite amount of time. |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 44 | // |
Philip Reames | d4a912f | 2015-02-09 22:44:03 +0000 | [diff] [blame] | 45 | // We do not currently support return statepoints, but adding them would not |
| 46 | // be hard. They are not required for correctness - entry safepoints are an |
| 47 | // alternative - but some GCs may prefer them. Patches welcome. |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 48 | // |
| 49 | //===----------------------------------------------------------------------===// |
| 50 | |
| 51 | #include "llvm/Pass.h" |
Chandler Carruth | 30d69c2 | 2015-02-13 10:01:29 +0000 | [diff] [blame] | 52 | #include "llvm/IR/LegacyPassManager.h" |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 53 | #include "llvm/ADT/SetOperations.h" |
Philip Reames | 5708cca | 2015-05-12 20:43:48 +0000 | [diff] [blame] | 54 | #include "llvm/ADT/SetVector.h" |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 55 | #include "llvm/ADT/Statistic.h" |
Swaroop Sridhar | 665bc9c | 2015-05-20 01:07:23 +0000 | [diff] [blame] | 56 | #include "llvm/ADT/StringRef.h" |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 57 | #include "llvm/Analysis/LoopPass.h" |
| 58 | #include "llvm/Analysis/LoopInfo.h" |
| 59 | #include "llvm/Analysis/ScalarEvolution.h" |
| 60 | #include "llvm/Analysis/ScalarEvolutionExpressions.h" |
| 61 | #include "llvm/Analysis/CFG.h" |
| 62 | #include "llvm/Analysis/InstructionSimplify.h" |
| 63 | #include "llvm/IR/BasicBlock.h" |
| 64 | #include "llvm/IR/CallSite.h" |
| 65 | #include "llvm/IR/Dominators.h" |
| 66 | #include "llvm/IR/Function.h" |
| 67 | #include "llvm/IR/IRBuilder.h" |
| 68 | #include "llvm/IR/InstIterator.h" |
| 69 | #include "llvm/IR/Instructions.h" |
| 70 | #include "llvm/IR/Intrinsics.h" |
| 71 | #include "llvm/IR/IntrinsicInst.h" |
| 72 | #include "llvm/IR/Module.h" |
| 73 | #include "llvm/IR/Statepoint.h" |
| 74 | #include "llvm/IR/Value.h" |
| 75 | #include "llvm/IR/Verifier.h" |
| 76 | #include "llvm/Support/Debug.h" |
| 77 | #include "llvm/Support/CommandLine.h" |
| 78 | #include "llvm/Support/raw_ostream.h" |
| 79 | #include "llvm/Transforms/Scalar.h" |
| 80 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
| 81 | #include "llvm/Transforms/Utils/Cloning.h" |
| 82 | #include "llvm/Transforms/Utils/Local.h" |
| 83 | |
| 84 | #define DEBUG_TYPE "safepoint-placement" |
| 85 | STATISTIC(NumEntrySafepoints, "Number of entry safepoints inserted"); |
| 86 | STATISTIC(NumCallSafepoints, "Number of call safepoints inserted"); |
| 87 | STATISTIC(NumBackedgeSafepoints, "Number of backedge safepoints inserted"); |
| 88 | |
| 89 | STATISTIC(CallInLoop, "Number of loops w/o safepoints due to calls in loop"); |
| 90 | STATISTIC(FiniteExecution, "Number of loops w/o safepoints finite execution"); |
| 91 | |
| 92 | using namespace llvm; |
| 93 | |
Benjamin Kramer | df005cb | 2015-08-08 18:27:36 +0000 | [diff] [blame] | 94 | // Ignore opportunities to avoid placing safepoints on backedges, useful for |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 95 | // validation |
Philip Reames | 1f3e5c1 | 2015-02-20 23:32:03 +0000 | [diff] [blame] | 96 | static cl::opt<bool> AllBackedges("spp-all-backedges", cl::Hidden, |
| 97 | cl::init(false)); |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 98 | |
Sanjoy Das | f75e15e | 2015-09-15 01:42:48 +0000 | [diff] [blame] | 99 | /// How narrow does the trip count of a loop have to be to have to be considered |
| 100 | /// "counted"? Counted loops do not get safepoints at backedges. |
| 101 | static cl::opt<int> CountedLoopTripWidth("spp-counted-loop-trip-width", |
| 102 | cl::Hidden, cl::init(32)); |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 103 | |
| 104 | // If true, split the backedge of a loop when placing the safepoint, otherwise |
| 105 | // split the latch block itself. Both are useful to support for |
| 106 | // experimentation, but in practice, it looks like splitting the backedge |
| 107 | // optimizes better. |
Philip Reames | 1f3e5c1 | 2015-02-20 23:32:03 +0000 | [diff] [blame] | 108 | static cl::opt<bool> SplitBackedge("spp-split-backedge", cl::Hidden, |
| 109 | cl::init(false)); |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 110 | |
| 111 | // Print tracing output |
Philip Reames | 1f3e5c1 | 2015-02-20 23:32:03 +0000 | [diff] [blame] | 112 | static cl::opt<bool> TraceLSP("spp-trace", cl::Hidden, cl::init(false)); |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 113 | |
| 114 | namespace { |
| 115 | |
Philip Reames | 9f12904 | 2015-05-12 21:09:36 +0000 | [diff] [blame] | 116 | /// An analysis pass whose purpose is to identify each of the backedges in |
| 117 | /// the function which require a safepoint poll to be inserted. |
| 118 | struct PlaceBackedgeSafepointsImpl : public FunctionPass { |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 119 | static char ID; |
| 120 | |
| 121 | /// The output of the pass - gives a list of each backedge (described by |
| 122 | /// pointing at the branch) which need a poll inserted. |
| 123 | std::vector<TerminatorInst *> PollLocations; |
| 124 | |
| 125 | /// True unless we're running spp-no-calls in which case we need to disable |
Benjamin Kramer | df005cb | 2015-08-08 18:27:36 +0000 | [diff] [blame] | 126 | /// the call-dependent placement opts. |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 127 | bool CallSafepointsEnabled; |
Philip Reames | 9f12904 | 2015-05-12 21:09:36 +0000 | [diff] [blame] | 128 | |
| 129 | ScalarEvolution *SE = nullptr; |
| 130 | DominatorTree *DT = nullptr; |
| 131 | LoopInfo *LI = nullptr; |
NAKAMURA Takumi | fb3bd71 | 2015-05-25 01:43:23 +0000 | [diff] [blame] | 132 | |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 133 | PlaceBackedgeSafepointsImpl(bool CallSafepoints = false) |
Philip Reames | 9f12904 | 2015-05-12 21:09:36 +0000 | [diff] [blame] | 134 | : FunctionPass(ID), CallSafepointsEnabled(CallSafepoints) { |
Philip Reames | 5a9685d | 2015-02-04 00:39:57 +0000 | [diff] [blame] | 135 | initializePlaceBackedgeSafepointsImplPass(*PassRegistry::getPassRegistry()); |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 136 | } |
| 137 | |
Philip Reames | 9f12904 | 2015-05-12 21:09:36 +0000 | [diff] [blame] | 138 | bool runOnLoop(Loop *); |
| 139 | void runOnLoopAndSubLoops(Loop *L) { |
| 140 | // Visit all the subloops |
| 141 | for (auto I = L->begin(), E = L->end(); I != E; I++) |
| 142 | runOnLoopAndSubLoops(*I); |
| 143 | runOnLoop(L); |
| 144 | } |
Justin Bogner | 383749a | 2015-05-12 21:49:47 +0000 | [diff] [blame] | 145 | |
| 146 | bool runOnFunction(Function &F) override { |
Chandler Carruth | 2f1fd16 | 2015-08-17 02:08:17 +0000 | [diff] [blame] | 147 | SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE(); |
Philip Reames | 9f12904 | 2015-05-12 21:09:36 +0000 | [diff] [blame] | 148 | DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree(); |
| 149 | LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo(); |
| 150 | for (auto I = LI->begin(), E = LI->end(); I != E; I++) { |
| 151 | runOnLoopAndSubLoops(*I); |
| 152 | } |
| 153 | return false; |
| 154 | } |
NAKAMURA Takumi | fb3bd71 | 2015-05-25 01:43:23 +0000 | [diff] [blame] | 155 | |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 156 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Philip Reames | 57bdac9 | 2015-05-12 20:56:33 +0000 | [diff] [blame] | 157 | AU.addRequired<DominatorTreeWrapperPass>(); |
Chandler Carruth | 2f1fd16 | 2015-08-17 02:08:17 +0000 | [diff] [blame] | 158 | AU.addRequired<ScalarEvolutionWrapperPass>(); |
Philip Reames | 9f12904 | 2015-05-12 21:09:36 +0000 | [diff] [blame] | 159 | AU.addRequired<LoopInfoWrapperPass>(); |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 160 | // We no longer modify the IR at all in this pass. Thus all |
| 161 | // analysis are preserved. |
| 162 | AU.setPreservesAll(); |
| 163 | } |
| 164 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 165 | } |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 166 | |
Philip Reames | 1f3e5c1 | 2015-02-20 23:32:03 +0000 | [diff] [blame] | 167 | static cl::opt<bool> NoEntry("spp-no-entry", cl::Hidden, cl::init(false)); |
| 168 | static cl::opt<bool> NoCall("spp-no-call", cl::Hidden, cl::init(false)); |
| 169 | static cl::opt<bool> NoBackedge("spp-no-backedge", cl::Hidden, cl::init(false)); |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 170 | |
| 171 | namespace { |
Philip Reames | 7b98179 | 2015-05-12 21:21:18 +0000 | [diff] [blame] | 172 | struct PlaceSafepoints : public FunctionPass { |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 173 | static char ID; // Pass identification, replacement for typeid |
| 174 | |
Philip Reames | 7b98179 | 2015-05-12 21:21:18 +0000 | [diff] [blame] | 175 | PlaceSafepoints() : FunctionPass(ID) { |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 176 | initializePlaceSafepointsPass(*PassRegistry::getPassRegistry()); |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 177 | } |
Philip Reames | 7b98179 | 2015-05-12 21:21:18 +0000 | [diff] [blame] | 178 | bool runOnFunction(Function &F) override; |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 179 | |
| 180 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 181 | // We modify the graph wholesale (inlining, block insertion, etc). We |
| 182 | // preserve nothing at the moment. We could potentially preserve dom tree |
| 183 | // if that was worth doing |
| 184 | } |
| 185 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 186 | } |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 187 | |
| 188 | // Insert a safepoint poll immediately before the given instruction. Does |
| 189 | // not handle the parsability of state at the runtime call, that's the |
| 190 | // callers job. |
NAKAMURA Takumi | fb3bd71 | 2015-05-25 01:43:23 +0000 | [diff] [blame] | 191 | static void |
Philip Reames | 38840245 | 2015-05-26 21:03:23 +0000 | [diff] [blame] | 192 | InsertSafepointPoll(Instruction *InsertBefore, |
Philip Reames | 5a9685d | 2015-02-04 00:39:57 +0000 | [diff] [blame] | 193 | std::vector<CallSite> &ParsePointsNeeded /*rval*/); |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 194 | |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 195 | static bool needsStatepoint(const CallSite &CS) { |
Sanjoy Das | c21a05a | 2015-10-08 23:18:30 +0000 | [diff] [blame] | 196 | if (callsGCLeafFunction(CS)) |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 197 | return false; |
| 198 | if (CS.isCall()) { |
| 199 | CallInst *call = cast<CallInst>(CS.getInstruction()); |
| 200 | if (call->isInlineAsm()) |
| 201 | return false; |
| 202 | } |
| 203 | if (isStatepoint(CS) || isGCRelocate(CS) || isGCResult(CS)) { |
| 204 | return false; |
| 205 | } |
| 206 | return true; |
| 207 | } |
| 208 | |
Philip Reames | 5a9685d | 2015-02-04 00:39:57 +0000 | [diff] [blame] | 209 | static Value *ReplaceWithStatepoint(const CallSite &CS, Pass *P); |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 210 | |
| 211 | /// Returns true if this loop is known to contain a call safepoint which |
| 212 | /// must unconditionally execute on any iteration of the loop which returns |
| 213 | /// to the loop header via an edge from Pred. Returns a conservative correct |
| 214 | /// answer; i.e. false is always valid. |
| 215 | static bool containsUnconditionalCallSafepoint(Loop *L, BasicBlock *Header, |
| 216 | BasicBlock *Pred, |
| 217 | DominatorTree &DT) { |
| 218 | // In general, we're looking for any cut of the graph which ensures |
| 219 | // there's a call safepoint along every edge between Header and Pred. |
| 220 | // For the moment, we look only for the 'cuts' that consist of a single call |
| 221 | // instruction in a block which is dominated by the Header and dominates the |
| 222 | // loop latch (Pred) block. Somewhat surprisingly, walking the entire chain |
Benjamin Kramer | df005cb | 2015-08-08 18:27:36 +0000 | [diff] [blame] | 223 | // of such dominating blocks gets substantially more occurrences than just |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 224 | // checking the Pred and Header blocks themselves. This may be due to the |
| 225 | // density of loop exit conditions caused by range and null checks. |
| 226 | // TODO: structure this as an analysis pass, cache the result for subloops, |
| 227 | // avoid dom tree recalculations |
| 228 | assert(DT.dominates(Header, Pred) && "loop latch not dominated by header?"); |
| 229 | |
| 230 | BasicBlock *Current = Pred; |
| 231 | while (true) { |
| 232 | for (Instruction &I : *Current) { |
Benjamin Kramer | 3a09ef6 | 2015-04-10 14:50:08 +0000 | [diff] [blame] | 233 | if (auto CS = CallSite(&I)) |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 234 | // Note: Technically, needing a safepoint isn't quite the right |
| 235 | // condition here. We should instead be checking if the target method |
| 236 | // has an |
| 237 | // unconditional poll. In practice, this is only a theoretical concern |
| 238 | // since we don't have any methods with conditional-only safepoint |
| 239 | // polls. |
| 240 | if (needsStatepoint(CS)) |
| 241 | return true; |
| 242 | } |
| 243 | |
| 244 | if (Current == Header) |
| 245 | break; |
| 246 | Current = DT.getNode(Current)->getIDom()->getBlock(); |
| 247 | } |
| 248 | |
| 249 | return false; |
| 250 | } |
| 251 | |
| 252 | /// Returns true if this loop is known to terminate in a finite number of |
| 253 | /// iterations. Note that this function may return false for a loop which |
| 254 | /// does actual terminate in a finite constant number of iterations due to |
| 255 | /// conservatism in the analysis. |
| 256 | static bool mustBeFiniteCountedLoop(Loop *L, ScalarEvolution *SE, |
Philip Reames | 5a9685d | 2015-02-04 00:39:57 +0000 | [diff] [blame] | 257 | BasicBlock *Pred) { |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 258 | // A conservative bound on the loop as a whole. |
| 259 | const SCEV *MaxTrips = SE->getMaxBackedgeTakenCount(L); |
Sanjoy Das | f75e15e | 2015-09-15 01:42:48 +0000 | [diff] [blame] | 260 | if (MaxTrips != SE->getCouldNotCompute() && |
| 261 | SE->getUnsignedRange(MaxTrips).getUnsignedMax().isIntN( |
| 262 | CountedLoopTripWidth)) |
| 263 | return true; |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 264 | |
| 265 | // If this is a conditional branch to the header with the alternate path |
| 266 | // being outside the loop, we can ask questions about the execution frequency |
| 267 | // of the exit block. |
| 268 | if (L->isLoopExiting(Pred)) { |
| 269 | // This returns an exact expression only. TODO: We really only need an |
| 270 | // upper bound here, but SE doesn't expose that. |
| 271 | const SCEV *MaxExec = SE->getExitCount(L, Pred); |
Sanjoy Das | f75e15e | 2015-09-15 01:42:48 +0000 | [diff] [blame] | 272 | if (MaxExec != SE->getCouldNotCompute() && |
| 273 | SE->getUnsignedRange(MaxExec).getUnsignedMax().isIntN( |
| 274 | CountedLoopTripWidth)) |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 275 | return true; |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | return /* not finite */ false; |
| 279 | } |
| 280 | |
| 281 | static void scanOneBB(Instruction *start, Instruction *end, |
Philip Reames | 5a9685d | 2015-02-04 00:39:57 +0000 | [diff] [blame] | 282 | std::vector<CallInst *> &calls, |
| 283 | std::set<BasicBlock *> &seen, |
| 284 | std::vector<BasicBlock *> &worklist) { |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 285 | for (BasicBlock::iterator itr(start); |
| 286 | itr != start->getParent()->end() && itr != BasicBlock::iterator(end); |
| 287 | itr++) { |
| 288 | if (CallInst *CI = dyn_cast<CallInst>(&*itr)) { |
| 289 | calls.push_back(CI); |
| 290 | } |
| 291 | // FIXME: This code does not handle invokes |
| 292 | assert(!dyn_cast<InvokeInst>(&*itr) && |
| 293 | "support for invokes in poll code needed"); |
| 294 | // Only add the successor blocks if we reach the terminator instruction |
| 295 | // without encountering end first |
| 296 | if (itr->isTerminator()) { |
| 297 | BasicBlock *BB = itr->getParent(); |
Philip Reames | a29de87 | 2015-02-09 22:26:11 +0000 | [diff] [blame] | 298 | for (BasicBlock *Succ : successors(BB)) { |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 299 | if (seen.count(Succ) == 0) { |
| 300 | worklist.push_back(Succ); |
| 301 | seen.insert(Succ); |
| 302 | } |
| 303 | } |
| 304 | } |
| 305 | } |
| 306 | } |
| 307 | static void scanInlinedCode(Instruction *start, Instruction *end, |
Philip Reames | 5a9685d | 2015-02-04 00:39:57 +0000 | [diff] [blame] | 308 | std::vector<CallInst *> &calls, |
| 309 | std::set<BasicBlock *> &seen) { |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 310 | calls.clear(); |
| 311 | std::vector<BasicBlock *> worklist; |
| 312 | seen.insert(start->getParent()); |
| 313 | scanOneBB(start, end, calls, seen, worklist); |
| 314 | while (!worklist.empty()) { |
| 315 | BasicBlock *BB = worklist.back(); |
| 316 | worklist.pop_back(); |
| 317 | scanOneBB(&*BB->begin(), end, calls, seen, worklist); |
| 318 | } |
| 319 | } |
| 320 | |
Philip Reames | 9f12904 | 2015-05-12 21:09:36 +0000 | [diff] [blame] | 321 | bool PlaceBackedgeSafepointsImpl::runOnLoop(Loop *L) { |
Philip Reames | 5708cca | 2015-05-12 20:43:48 +0000 | [diff] [blame] | 322 | // Loop through all loop latches (branches controlling backedges). We need |
NAKAMURA Takumi | fb3bd71 | 2015-05-25 01:43:23 +0000 | [diff] [blame] | 323 | // to place a safepoint on every backedge (potentially). |
Philip Reames | 5708cca | 2015-05-12 20:43:48 +0000 | [diff] [blame] | 324 | // Note: In common usage, there will be only one edge due to LoopSimplify |
| 325 | // having run sometime earlier in the pipeline, but this code must be correct |
| 326 | // w.r.t. loops with multiple backedges. |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 327 | BasicBlock *header = L->getHeader(); |
Philip Reames | 5708cca | 2015-05-12 20:43:48 +0000 | [diff] [blame] | 328 | SmallVector<BasicBlock*, 16> LoopLatches; |
| 329 | L->getLoopLatches(LoopLatches); |
| 330 | for (BasicBlock *pred : LoopLatches) { |
| 331 | assert(L->contains(pred)); |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 332 | |
| 333 | // Make a policy decision about whether this loop needs a safepoint or |
| 334 | // not. Note that this is about unburdening the optimizer in loops, not |
| 335 | // avoiding the runtime cost of the actual safepoint. |
| 336 | if (!AllBackedges) { |
| 337 | if (mustBeFiniteCountedLoop(L, SE, pred)) { |
| 338 | if (TraceLSP) |
| 339 | errs() << "skipping safepoint placement in finite loop\n"; |
| 340 | FiniteExecution++; |
| 341 | continue; |
| 342 | } |
| 343 | if (CallSafepointsEnabled && |
Philip Reames | 57bdac9 | 2015-05-12 20:56:33 +0000 | [diff] [blame] | 344 | containsUnconditionalCallSafepoint(L, header, pred, *DT)) { |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 345 | // Note: This is only semantically legal since we won't do any further |
| 346 | // IPO or inlining before the actual call insertion.. If we hadn't, we |
| 347 | // might latter loose this call safepoint. |
| 348 | if (TraceLSP) |
| 349 | errs() << "skipping safepoint placement due to unconditional call\n"; |
| 350 | CallInLoop++; |
| 351 | continue; |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | // TODO: We can create an inner loop which runs a finite number of |
| 356 | // iterations with an outer loop which contains a safepoint. This would |
| 357 | // not help runtime performance that much, but it might help our ability to |
| 358 | // optimize the inner loop. |
| 359 | |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 360 | // Safepoint insertion would involve creating a new basic block (as the |
| 361 | // target of the current backedge) which does the safepoint (of all live |
| 362 | // variables) and branches to the true header |
| 363 | TerminatorInst *term = pred->getTerminator(); |
| 364 | |
| 365 | if (TraceLSP) { |
| 366 | errs() << "[LSP] terminator instruction: "; |
| 367 | term->dump(); |
| 368 | } |
| 369 | |
| 370 | PollLocations.push_back(term); |
| 371 | } |
| 372 | |
Philip Reames | 5708cca | 2015-05-12 20:43:48 +0000 | [diff] [blame] | 373 | return false; |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 374 | } |
| 375 | |
Philip Reames | d97cdf2 | 2015-05-19 23:40:11 +0000 | [diff] [blame] | 376 | /// Returns true if an entry safepoint is not required before this callsite in |
NAKAMURA Takumi | fb3bd71 | 2015-05-25 01:43:23 +0000 | [diff] [blame] | 377 | /// the caller function. |
Philip Reames | d97cdf2 | 2015-05-19 23:40:11 +0000 | [diff] [blame] | 378 | static bool doesNotRequireEntrySafepointBefore(const CallSite &CS) { |
| 379 | Instruction *Inst = CS.getInstruction(); |
| 380 | if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(Inst)) { |
| 381 | switch (II->getIntrinsicID()) { |
| 382 | case Intrinsic::experimental_gc_statepoint: |
| 383 | case Intrinsic::experimental_patchpoint_void: |
| 384 | case Intrinsic::experimental_patchpoint_i64: |
| 385 | // The can wrap an actual call which may grow the stack by an unbounded |
| 386 | // amount or run forever. |
| 387 | return false; |
| 388 | default: |
| 389 | // Most LLVM intrinsics are things which do not expand to actual calls, or |
| 390 | // at least if they do, are leaf functions that cause only finite stack |
| 391 | // growth. In particular, the optimizer likes to form things like memsets |
| 392 | // out of stores in the original IR. Another important example is |
Reid Kleckner | 6038179 | 2015-07-07 22:25:32 +0000 | [diff] [blame] | 393 | // llvm.localescape which must occur in the entry block. Inserting a |
| 394 | // safepoint before it is not legal since it could push the localescape |
Philip Reames | d97cdf2 | 2015-05-19 23:40:11 +0000 | [diff] [blame] | 395 | // out of the entry block. |
| 396 | return true; |
| 397 | } |
| 398 | } |
| 399 | return false; |
| 400 | } |
| 401 | |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 402 | static Instruction *findLocationForEntrySafepoint(Function &F, |
| 403 | DominatorTree &DT) { |
| 404 | |
| 405 | // Conceptually, this poll needs to be on method entry, but in |
| 406 | // practice, we place it as late in the entry block as possible. We |
| 407 | // can place it as late as we want as long as it dominates all calls |
| 408 | // that can grow the stack. This, combined with backedge polls, |
| 409 | // give us all the progress guarantees we need. |
| 410 | |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 411 | // hasNextInstruction and nextInstruction are used to iterate |
| 412 | // through a "straight line" execution sequence. |
| 413 | |
| 414 | auto hasNextInstruction = [](Instruction *I) { |
| 415 | if (!I->isTerminator()) { |
| 416 | return true; |
| 417 | } |
| 418 | BasicBlock *nextBB = I->getParent()->getUniqueSuccessor(); |
| 419 | return nextBB && (nextBB->getUniquePredecessor() != nullptr); |
| 420 | }; |
| 421 | |
| 422 | auto nextInstruction = [&hasNextInstruction](Instruction *I) { |
| 423 | assert(hasNextInstruction(I) && |
| 424 | "first check if there is a next instruction!"); |
| 425 | if (I->isTerminator()) { |
Duncan P. N. Exon Smith | be4d8cb | 2015-10-13 19:26:58 +0000 | [diff] [blame] | 426 | return &I->getParent()->getUniqueSuccessor()->front(); |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 427 | } else { |
Duncan P. N. Exon Smith | be4d8cb | 2015-10-13 19:26:58 +0000 | [diff] [blame] | 428 | return &*++I->getIterator(); |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 429 | } |
| 430 | }; |
| 431 | |
| 432 | Instruction *cursor = nullptr; |
Duncan P. N. Exon Smith | be4d8cb | 2015-10-13 19:26:58 +0000 | [diff] [blame] | 433 | for (cursor = &F.getEntryBlock().front(); hasNextInstruction(cursor); |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 434 | cursor = nextInstruction(cursor)) { |
| 435 | |
Philip Reames | d97cdf2 | 2015-05-19 23:40:11 +0000 | [diff] [blame] | 436 | // We need to ensure a safepoint poll occurs before any 'real' call. The |
| 437 | // easiest way to ensure finite execution between safepoints in the face of |
| 438 | // recursive and mutually recursive functions is to enforce that each take |
| 439 | // a safepoint. Additionally, we need to ensure a poll before any call |
| 440 | // which can grow the stack by an unbounded amount. This isn't required |
| 441 | // for GC semantics per se, but is a common requirement for languages |
| 442 | // which detect stack overflow via guard pages and then throw exceptions. |
| 443 | if (auto CS = CallSite(cursor)) { |
| 444 | if (doesNotRequireEntrySafepointBefore(CS)) |
| 445 | continue; |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 446 | break; |
| 447 | } |
| 448 | } |
| 449 | |
Philip Reames | 5a9685d | 2015-02-04 00:39:57 +0000 | [diff] [blame] | 450 | assert((hasNextInstruction(cursor) || cursor->isTerminator()) && |
| 451 | "either we stopped because of a call, or because of terminator"); |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 452 | |
Philip Reames | 52e7a59 | 2015-05-26 21:16:42 +0000 | [diff] [blame] | 453 | return cursor; |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | /// Identify the list of call sites which need to be have parseable state |
| 457 | static void findCallSafepoints(Function &F, |
| 458 | std::vector<CallSite> &Found /*rval*/) { |
| 459 | assert(Found.empty() && "must be empty!"); |
Nico Rieck | 7819951 | 2015-08-06 19:10:45 +0000 | [diff] [blame] | 460 | for (Instruction &I : instructions(F)) { |
Philip Reames | a29de87 | 2015-02-09 22:26:11 +0000 | [diff] [blame] | 461 | Instruction *inst = &I; |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 462 | if (isa<CallInst>(inst) || isa<InvokeInst>(inst)) { |
| 463 | CallSite CS(inst); |
| 464 | |
| 465 | // No safepoint needed or wanted |
| 466 | if (!needsStatepoint(CS)) { |
| 467 | continue; |
| 468 | } |
| 469 | |
| 470 | Found.push_back(CS); |
| 471 | } |
| 472 | } |
| 473 | } |
| 474 | |
| 475 | /// Implement a unique function which doesn't require we sort the input |
| 476 | /// vector. Doing so has the effect of changing the output of a couple of |
| 477 | /// tests in ways which make them less useful in testing fused safepoints. |
| 478 | template <typename T> static void unique_unsorted(std::vector<T> &vec) { |
| 479 | std::set<T> seen; |
| 480 | std::vector<T> tmp; |
| 481 | vec.reserve(vec.size()); |
| 482 | std::swap(tmp, vec); |
| 483 | for (auto V : tmp) { |
| 484 | if (seen.insert(V).second) { |
| 485 | vec.push_back(V); |
| 486 | } |
| 487 | } |
| 488 | } |
| 489 | |
Benjamin Kramer | 82f8652 | 2015-06-07 16:36:28 +0000 | [diff] [blame] | 490 | static const char *const GCSafepointPollName = "gc.safepoint_poll"; |
Philip Reames | b1ed02f | 2015-02-09 21:48:05 +0000 | [diff] [blame] | 491 | |
| 492 | static bool isGCSafepointPoll(Function &F) { |
| 493 | return F.getName().equals(GCSafepointPollName); |
| 494 | } |
| 495 | |
Philip Reames | 0b1b387 | 2015-02-21 00:09:09 +0000 | [diff] [blame] | 496 | /// Returns true if this function should be rewritten to include safepoint |
| 497 | /// polls and parseable call sites. The main point of this function is to be |
NAKAMURA Takumi | fb3bd71 | 2015-05-25 01:43:23 +0000 | [diff] [blame] | 498 | /// an extension point for custom logic. |
Philip Reames | 0b1b387 | 2015-02-21 00:09:09 +0000 | [diff] [blame] | 499 | static bool shouldRewriteFunction(Function &F) { |
| 500 | // TODO: This should check the GCStrategy |
| 501 | if (F.hasGC()) { |
NAKAMURA Takumi | fb3bd71 | 2015-05-25 01:43:23 +0000 | [diff] [blame] | 502 | const char *FunctionGCName = F.getGC(); |
| 503 | const StringRef StatepointExampleName("statepoint-example"); |
| 504 | const StringRef CoreCLRName("coreclr"); |
| 505 | return (StatepointExampleName == FunctionGCName) || |
NAKAMURA Takumi | 5582a6a | 2015-05-25 01:43:34 +0000 | [diff] [blame] | 506 | (CoreCLRName == FunctionGCName); |
Philip Reames | 0b1b387 | 2015-02-21 00:09:09 +0000 | [diff] [blame] | 507 | } else |
| 508 | return false; |
| 509 | } |
| 510 | |
| 511 | // TODO: These should become properties of the GCStrategy, possibly with |
| 512 | // command line overrides. |
| 513 | static bool enableEntrySafepoints(Function &F) { return !NoEntry; } |
| 514 | static bool enableBackedgeSafepoints(Function &F) { return !NoBackedge; } |
| 515 | static bool enableCallSafepoints(Function &F) { return !NoCall; } |
| 516 | |
Igor Laevsky | 5e23e16 | 2015-05-08 11:59:09 +0000 | [diff] [blame] | 517 | // Normalize basic block to make it ready to be target of invoke statepoint. |
| 518 | // Ensure that 'BB' does not have phi nodes. It may require spliting it. |
| 519 | static BasicBlock *normalizeForInvokeSafepoint(BasicBlock *BB, |
| 520 | BasicBlock *InvokeParent) { |
| 521 | BasicBlock *ret = BB; |
| 522 | |
| 523 | if (!BB->getUniquePredecessor()) { |
| 524 | ret = SplitBlockPredecessors(BB, InvokeParent, ""); |
| 525 | } |
| 526 | |
| 527 | // Now that 'ret' has unique predecessor we can safely remove all phi nodes |
| 528 | // from it |
| 529 | FoldSingleEntryPHINodes(ret); |
| 530 | assert(!isa<PHINode>(ret->begin())); |
| 531 | |
| 532 | return ret; |
| 533 | } |
Philip Reames | 0b1b387 | 2015-02-21 00:09:09 +0000 | [diff] [blame] | 534 | |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 535 | bool PlaceSafepoints::runOnFunction(Function &F) { |
| 536 | if (F.isDeclaration() || F.empty()) { |
| 537 | // This is a declaration, nothing to do. Must exit early to avoid crash in |
| 538 | // dom tree calculation |
| 539 | return false; |
| 540 | } |
| 541 | |
Philip Reames | 7e7dc3e | 2015-02-10 00:04:53 +0000 | [diff] [blame] | 542 | if (isGCSafepointPoll(F)) { |
| 543 | // Given we're inlining this inside of safepoint poll insertion, this |
| 544 | // doesn't make any sense. Note that we do make any contained calls |
NAKAMURA Takumi | fb3bd71 | 2015-05-25 01:43:23 +0000 | [diff] [blame] | 545 | // parseable after we inline a poll. |
Philip Reames | 7e7dc3e | 2015-02-10 00:04:53 +0000 | [diff] [blame] | 546 | return false; |
| 547 | } |
| 548 | |
Philip Reames | 0b1b387 | 2015-02-21 00:09:09 +0000 | [diff] [blame] | 549 | if (!shouldRewriteFunction(F)) |
| 550 | return false; |
| 551 | |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 552 | bool modified = false; |
| 553 | |
| 554 | // In various bits below, we rely on the fact that uses are reachable from |
| 555 | // defs. When there are basic blocks unreachable from the entry, dominance |
| 556 | // and reachablity queries return non-sensical results. Thus, we preprocess |
| 557 | // the function to ensure these properties hold. |
| 558 | modified |= removeUnreachableBlocks(F); |
| 559 | |
| 560 | // STEP 1 - Insert the safepoint polling locations. We do not need to |
| 561 | // actually insert parse points yet. That will be done for all polls and |
| 562 | // calls in a single pass. |
| 563 | |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 564 | DominatorTree DT; |
Philip Reames | 4d1a3ef | 2015-05-13 00:32:23 +0000 | [diff] [blame] | 565 | DT.recalculate(F); |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 566 | |
Philip Reames | 4d1a3ef | 2015-05-13 00:32:23 +0000 | [diff] [blame] | 567 | SmallVector<Instruction *, 16> PollsNeeded; |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 568 | std::vector<CallSite> ParsePointNeeded; |
| 569 | |
Philip Reames | 0b1b387 | 2015-02-21 00:09:09 +0000 | [diff] [blame] | 570 | if (enableBackedgeSafepoints(F)) { |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 571 | // Construct a pass manager to run the LoopPass backedge logic. We |
| 572 | // need the pass manager to handle scheduling all the loop passes |
| 573 | // appropriately. Doing this by hand is painful and just not worth messing |
| 574 | // with for the moment. |
Chandler Carruth | 30d69c2 | 2015-02-13 10:01:29 +0000 | [diff] [blame] | 575 | legacy::FunctionPassManager FPM(F.getParent()); |
Philip Reames | 0b1b387 | 2015-02-21 00:09:09 +0000 | [diff] [blame] | 576 | bool CanAssumeCallSafepoints = enableCallSafepoints(F); |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 577 | PlaceBackedgeSafepointsImpl *PBS = |
Philip Reames | b1ed02f | 2015-02-09 21:48:05 +0000 | [diff] [blame] | 578 | new PlaceBackedgeSafepointsImpl(CanAssumeCallSafepoints); |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 579 | FPM.add(PBS); |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 580 | FPM.run(F); |
| 581 | |
| 582 | // We preserve dominance information when inserting the poll, otherwise |
| 583 | // we'd have to recalculate this on every insert |
| 584 | DT.recalculate(F); |
| 585 | |
Philip Reames | 5708cca | 2015-05-12 20:43:48 +0000 | [diff] [blame] | 586 | auto &PollLocations = PBS->PollLocations; |
| 587 | |
| 588 | auto OrderByBBName = [](Instruction *a, Instruction *b) { |
| 589 | return a->getParent()->getName() < b->getParent()->getName(); |
| 590 | }; |
| 591 | // We need the order of list to be stable so that naming ends up stable |
| 592 | // when we split edges. This makes test cases much easier to write. |
| 593 | std::sort(PollLocations.begin(), PollLocations.end(), OrderByBBName); |
| 594 | |
| 595 | // We can sometimes end up with duplicate poll locations. This happens if |
| 596 | // a single loop is visited more than once. The fact this happens seems |
| 597 | // wrong, but it does happen for the split-backedge.ll test case. |
| 598 | PollLocations.erase(std::unique(PollLocations.begin(), |
| 599 | PollLocations.end()), |
| 600 | PollLocations.end()); |
| 601 | |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 602 | // Insert a poll at each point the analysis pass identified |
Philip Reames | 89fe570 | 2015-05-12 23:39:23 +0000 | [diff] [blame] | 603 | // The poll location must be the terminator of a loop latch block. |
| 604 | for (TerminatorInst *Term : PollLocations) { |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 605 | // We are inserting a poll, the function is modified |
| 606 | modified = true; |
NAKAMURA Takumi | fb3bd71 | 2015-05-25 01:43:23 +0000 | [diff] [blame] | 607 | |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 608 | if (SplitBackedge) { |
| 609 | // Split the backedge of the loop and insert the poll within that new |
| 610 | // basic block. This creates a loop with two latches per original |
| 611 | // latch (which is non-ideal), but this appears to be easier to |
| 612 | // optimize in practice than inserting the poll immediately before the |
| 613 | // latch test. |
| 614 | |
| 615 | // Since this is a latch, at least one of the successors must dominate |
| 616 | // it. Its possible that we have a) duplicate edges to the same header |
| 617 | // and b) edges to distinct loop headers. We need to insert pools on |
Philip Reames | 5708cca | 2015-05-12 20:43:48 +0000 | [diff] [blame] | 618 | // each. |
| 619 | SetVector<BasicBlock *> Headers; |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 620 | for (unsigned i = 0; i < Term->getNumSuccessors(); i++) { |
| 621 | BasicBlock *Succ = Term->getSuccessor(i); |
| 622 | if (DT.dominates(Succ, Term->getParent())) { |
| 623 | Headers.insert(Succ); |
| 624 | } |
| 625 | } |
| 626 | assert(!Headers.empty() && "poll location is not a loop latch?"); |
| 627 | |
| 628 | // The split loop structure here is so that we only need to recalculate |
| 629 | // the dominator tree once. Alternatively, we could just keep it up to |
| 630 | // date and use a more natural merged loop. |
Philip Reames | 5708cca | 2015-05-12 20:43:48 +0000 | [diff] [blame] | 631 | SetVector<BasicBlock *> SplitBackedges; |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 632 | for (BasicBlock *Header : Headers) { |
Philip Reames | 89fe570 | 2015-05-12 23:39:23 +0000 | [diff] [blame] | 633 | BasicBlock *NewBB = SplitEdge(Term->getParent(), Header, &DT); |
Philip Reames | 4d1a3ef | 2015-05-13 00:32:23 +0000 | [diff] [blame] | 634 | PollsNeeded.push_back(NewBB->getTerminator()); |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 635 | NumBackedgeSafepoints++; |
| 636 | } |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 637 | } else { |
| 638 | // Split the latch block itself, right before the terminator. |
Philip Reames | 4d1a3ef | 2015-05-13 00:32:23 +0000 | [diff] [blame] | 639 | PollsNeeded.push_back(Term); |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 640 | NumBackedgeSafepoints++; |
| 641 | } |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 642 | } |
| 643 | } |
| 644 | |
Philip Reames | 0b1b387 | 2015-02-21 00:09:09 +0000 | [diff] [blame] | 645 | if (enableEntrySafepoints(F)) { |
Philip Reames | 4d1a3ef | 2015-05-13 00:32:23 +0000 | [diff] [blame] | 646 | Instruction *Location = findLocationForEntrySafepoint(F, DT); |
| 647 | if (!Location) { |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 648 | // policy choice not to insert? |
| 649 | } else { |
Philip Reames | 4d1a3ef | 2015-05-13 00:32:23 +0000 | [diff] [blame] | 650 | PollsNeeded.push_back(Location); |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 651 | modified = true; |
| 652 | NumEntrySafepoints++; |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 653 | } |
| 654 | } |
| 655 | |
Philip Reames | 4d1a3ef | 2015-05-13 00:32:23 +0000 | [diff] [blame] | 656 | // Now that we've identified all the needed safepoint poll locations, insert |
| 657 | // safepoint polls themselves. |
| 658 | for (Instruction *PollLocation : PollsNeeded) { |
| 659 | std::vector<CallSite> RuntimeCalls; |
| 660 | InsertSafepointPoll(PollLocation, RuntimeCalls); |
| 661 | ParsePointNeeded.insert(ParsePointNeeded.end(), RuntimeCalls.begin(), |
| 662 | RuntimeCalls.end()); |
| 663 | } |
| 664 | PollsNeeded.clear(); // make sure we don't accidentally use |
| 665 | // The dominator tree has been invalidated by the inlining performed in the |
| 666 | // above loop. TODO: Teach the inliner how to update the dom tree? |
| 667 | DT.recalculate(F); |
NAKAMURA Takumi | fb3bd71 | 2015-05-25 01:43:23 +0000 | [diff] [blame] | 668 | |
Philip Reames | 0b1b387 | 2015-02-21 00:09:09 +0000 | [diff] [blame] | 669 | if (enableCallSafepoints(F)) { |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 670 | std::vector<CallSite> Calls; |
| 671 | findCallSafepoints(F, Calls); |
| 672 | NumCallSafepoints += Calls.size(); |
Philip Reames | 5a9685d | 2015-02-04 00:39:57 +0000 | [diff] [blame] | 673 | ParsePointNeeded.insert(ParsePointNeeded.end(), Calls.begin(), Calls.end()); |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 674 | } |
| 675 | |
| 676 | // Unique the vectors since we can end up with duplicates if we scan the call |
| 677 | // site for call safepoints after we add it for entry or backedge. The |
| 678 | // only reason we need tracking at all is that some functions might have |
| 679 | // polls but not call safepoints and thus we might miss marking the runtime |
| 680 | // calls for the polls. (This is useful in test cases!) |
| 681 | unique_unsorted(ParsePointNeeded); |
| 682 | |
| 683 | // Any parse point (no matter what source) will be handled here |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 684 | |
| 685 | // We're about to start modifying the function |
| 686 | if (!ParsePointNeeded.empty()) |
| 687 | modified = true; |
| 688 | |
| 689 | // Now run through and insert the safepoints, but do _NOT_ update or remove |
| 690 | // any existing uses. We have references to live variables that need to |
| 691 | // survive to the last iteration of this loop. |
| 692 | std::vector<Value *> Results; |
| 693 | Results.reserve(ParsePointNeeded.size()); |
| 694 | for (size_t i = 0; i < ParsePointNeeded.size(); i++) { |
| 695 | CallSite &CS = ParsePointNeeded[i]; |
Igor Laevsky | 5e23e16 | 2015-05-08 11:59:09 +0000 | [diff] [blame] | 696 | |
| 697 | // For invoke statepoints we need to remove all phi nodes at the normal |
| 698 | // destination block. |
| 699 | // Reason for this is that we can place gc_result only after last phi node |
| 700 | // in basic block. We will get malformed code after RAUW for the |
| 701 | // gc_result if one of this phi nodes uses result from the invoke. |
| 702 | if (InvokeInst *Invoke = dyn_cast<InvokeInst>(CS.getInstruction())) { |
| 703 | normalizeForInvokeSafepoint(Invoke->getNormalDest(), |
| 704 | Invoke->getParent()); |
| 705 | } |
| 706 | |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 707 | Value *GCResult = ReplaceWithStatepoint(CS, nullptr); |
| 708 | Results.push_back(GCResult); |
| 709 | } |
| 710 | assert(Results.size() == ParsePointNeeded.size()); |
| 711 | |
| 712 | // Adjust all users of the old call sites to use the new ones instead |
| 713 | for (size_t i = 0; i < ParsePointNeeded.size(); i++) { |
| 714 | CallSite &CS = ParsePointNeeded[i]; |
| 715 | Value *GCResult = Results[i]; |
| 716 | if (GCResult) { |
Chen Li | 74ca2a8 | 2015-05-18 19:02:25 +0000 | [diff] [blame] | 717 | // Can not RAUW for the invoke gc result in case of phi nodes preset. |
| 718 | assert(CS.isCall() || !isa<PHINode>(cast<Instruction>(GCResult)->getParent()->begin())); |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 719 | |
| 720 | // Replace all uses with the new call |
| 721 | CS.getInstruction()->replaceAllUsesWith(GCResult); |
| 722 | } |
| 723 | |
| 724 | // Now that we've handled all uses, remove the original call itself |
| 725 | // Note: The insert point can't be the deleted instruction! |
| 726 | CS.getInstruction()->eraseFromParent(); |
| 727 | } |
| 728 | return modified; |
| 729 | } |
| 730 | |
| 731 | char PlaceBackedgeSafepointsImpl::ID = 0; |
| 732 | char PlaceSafepoints::ID = 0; |
| 733 | |
Philip Reames | 7b98179 | 2015-05-12 21:21:18 +0000 | [diff] [blame] | 734 | FunctionPass *llvm::createPlaceSafepointsPass() { |
| 735 | return new PlaceSafepoints(); |
| 736 | } |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 737 | |
| 738 | INITIALIZE_PASS_BEGIN(PlaceBackedgeSafepointsImpl, |
| 739 | "place-backedge-safepoints-impl", |
| 740 | "Place Backedge Safepoints", false, false) |
Chandler Carruth | 2f1fd16 | 2015-08-17 02:08:17 +0000 | [diff] [blame] | 741 | INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass) |
Philip Reames | 57bdac9 | 2015-05-12 20:56:33 +0000 | [diff] [blame] | 742 | INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) |
Philip Reames | 9f12904 | 2015-05-12 21:09:36 +0000 | [diff] [blame] | 743 | INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass) |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 744 | INITIALIZE_PASS_END(PlaceBackedgeSafepointsImpl, |
| 745 | "place-backedge-safepoints-impl", |
| 746 | "Place Backedge Safepoints", false, false) |
| 747 | |
| 748 | INITIALIZE_PASS_BEGIN(PlaceSafepoints, "place-safepoints", "Place Safepoints", |
| 749 | false, false) |
| 750 | INITIALIZE_PASS_END(PlaceSafepoints, "place-safepoints", "Place Safepoints", |
| 751 | false, false) |
| 752 | |
Philip Reames | 5a9685d | 2015-02-04 00:39:57 +0000 | [diff] [blame] | 753 | static void |
Philip Reames | 38840245 | 2015-05-26 21:03:23 +0000 | [diff] [blame] | 754 | InsertSafepointPoll(Instruction *InsertBefore, |
Philip Reames | 5a9685d | 2015-02-04 00:39:57 +0000 | [diff] [blame] | 755 | std::vector<CallSite> &ParsePointsNeeded /*rval*/) { |
Philip Reames | 38840245 | 2015-05-26 21:03:23 +0000 | [diff] [blame] | 756 | BasicBlock *OrigBB = InsertBefore->getParent(); |
| 757 | Module *M = InsertBefore->getModule(); |
| 758 | assert(M && "must be part of a module"); |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 759 | |
| 760 | // Inline the safepoint poll implementation - this will get all the branch, |
| 761 | // control flow, etc.. Most importantly, it will introduce the actual slow |
| 762 | // path call - where we need to insert a safepoint (parsepoint). |
Philip Reames | 38840245 | 2015-05-26 21:03:23 +0000 | [diff] [blame] | 763 | |
| 764 | auto *F = M->getFunction(GCSafepointPollName); |
| 765 | assert(F->getType()->getElementType() == |
| 766 | FunctionType::get(Type::getVoidTy(M->getContext()), false) && |
| 767 | "gc.safepoint_poll declared with wrong type"); |
Ramkumar Ramachandra | 3edf74f | 2015-02-09 23:02:10 +0000 | [diff] [blame] | 768 | assert(!F->empty() && "gc.safepoint_poll must be a non-empty function"); |
Philip Reames | 38840245 | 2015-05-26 21:03:23 +0000 | [diff] [blame] | 769 | CallInst *PollCall = CallInst::Create(F, "", InsertBefore); |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 770 | |
| 771 | // Record some information about the call site we're replacing |
Philip Reames | 38840245 | 2015-05-26 21:03:23 +0000 | [diff] [blame] | 772 | BasicBlock::iterator before(PollCall), after(PollCall); |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 773 | bool isBegin(false); |
Philip Reames | 38840245 | 2015-05-26 21:03:23 +0000 | [diff] [blame] | 774 | if (before == OrigBB->begin()) { |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 775 | isBegin = true; |
| 776 | } else { |
| 777 | before--; |
| 778 | } |
| 779 | after++; |
Philip Reames | 38840245 | 2015-05-26 21:03:23 +0000 | [diff] [blame] | 780 | assert(after != OrigBB->end() && "must have successor"); |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 781 | |
| 782 | // do the actual inlining |
| 783 | InlineFunctionInfo IFI; |
Philip Reames | 38840245 | 2015-05-26 21:03:23 +0000 | [diff] [blame] | 784 | bool InlineStatus = InlineFunction(PollCall, IFI); |
| 785 | assert(InlineStatus && "inline must succeed"); |
| 786 | (void)InlineStatus; // suppress warning in release-asserts |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 787 | |
| 788 | // Check post conditions |
| 789 | assert(IFI.StaticAllocas.empty() && "can't have allocs"); |
| 790 | |
| 791 | std::vector<CallInst *> calls; // new calls |
| 792 | std::set<BasicBlock *> BBs; // new BBs + insertee |
| 793 | // Include only the newly inserted instructions, Note: begin may not be valid |
| 794 | // if we inserted to the beginning of the basic block |
| 795 | BasicBlock::iterator start; |
| 796 | if (isBegin) { |
| 797 | start = OrigBB->begin(); |
| 798 | } else { |
| 799 | start = before; |
| 800 | start++; |
| 801 | } |
| 802 | |
| 803 | // If your poll function includes an unreachable at the end, that's not |
| 804 | // valid. Bugpoint likes to create this, so check for it. |
| 805 | assert(isPotentiallyReachable(&*start, &*after, nullptr, nullptr) && |
| 806 | "malformed poll function"); |
| 807 | |
| 808 | scanInlinedCode(&*(start), &*(after), calls, BBs); |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 809 | assert(!calls.empty() && "slow path not found for safepoint poll"); |
| 810 | |
| 811 | // Record the fact we need a parsable state at the runtime call contained in |
| 812 | // the poll function. This is required so that the runtime knows how to |
| 813 | // parse the last frame when we actually take the safepoint (i.e. execute |
| 814 | // the slow path) |
| 815 | assert(ParsePointsNeeded.empty()); |
| 816 | for (size_t i = 0; i < calls.size(); i++) { |
| 817 | |
| 818 | // No safepoint needed or wanted |
| 819 | if (!needsStatepoint(calls[i])) { |
| 820 | continue; |
| 821 | } |
| 822 | |
| 823 | // These are likely runtime calls. Should we assert that via calling |
| 824 | // convention or something? |
| 825 | ParsePointsNeeded.push_back(CallSite(calls[i])); |
| 826 | } |
| 827 | assert(ParsePointsNeeded.size() <= calls.size()); |
| 828 | } |
| 829 | |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 830 | /// Replaces the given call site (Call or Invoke) with a gc.statepoint |
| 831 | /// intrinsic with an empty deoptimization arguments list. This does |
| 832 | /// NOT do explicit relocation for GC support. |
| 833 | static Value *ReplaceWithStatepoint(const CallSite &CS, /* to replace */ |
| 834 | Pass *P) { |
NAKAMURA Takumi | 2a5bd54 | 2015-05-07 10:18:46 +0000 | [diff] [blame] | 835 | assert(CS.getInstruction()->getParent()->getParent()->getParent() && |
| 836 | "must be set"); |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 837 | |
| 838 | // TODO: technically, a pass is not allowed to get functions from within a |
| 839 | // function pass since it might trigger a new function addition. Refactor |
| 840 | // this logic out to the initialization of the pass. Doesn't appear to |
| 841 | // matter in practice. |
| 842 | |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 843 | // Then go ahead and use the builder do actually do the inserts. We insert |
| 844 | // immediately before the previous instruction under the assumption that all |
| 845 | // arguments will be available here. We can't insert afterwards since we may |
| 846 | // be replacing a terminator. |
Sanjoy Das | 93abd81 | 2015-05-06 23:53:19 +0000 | [diff] [blame] | 847 | IRBuilder<> Builder(CS.getInstruction()); |
Philip Reames | b1ed02f | 2015-02-09 21:48:05 +0000 | [diff] [blame] | 848 | |
| 849 | // Note: The gc args are not filled in at this time, that's handled by |
| 850 | // RewriteStatepointsForGC (which is currently under review). |
| 851 | |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 852 | // Create the statepoint given all the arguments |
Sanjoy Das | 93abd81 | 2015-05-06 23:53:19 +0000 | [diff] [blame] | 853 | Instruction *Token = nullptr; |
Sanjoy Das | ba74e64 | 2015-05-13 20:11:31 +0000 | [diff] [blame] | 854 | |
| 855 | uint64_t ID; |
| 856 | uint32_t NumPatchBytes; |
| 857 | |
| 858 | AttributeSet OriginalAttrs = CS.getAttributes(); |
| 859 | Attribute AttrID = |
| 860 | OriginalAttrs.getAttribute(AttributeSet::FunctionIndex, "statepoint-id"); |
| 861 | Attribute AttrNumPatchBytes = OriginalAttrs.getAttribute( |
| 862 | AttributeSet::FunctionIndex, "statepoint-num-patch-bytes"); |
| 863 | |
| 864 | AttrBuilder AttrsToRemove; |
| 865 | bool HasID = AttrID.isStringAttribute() && |
| 866 | !AttrID.getValueAsString().getAsInteger(10, ID); |
| 867 | |
| 868 | if (HasID) |
| 869 | AttrsToRemove.addAttribute("statepoint-id"); |
| 870 | else |
| 871 | ID = 0xABCDEF00; |
| 872 | |
| 873 | bool HasNumPatchBytes = |
| 874 | AttrNumPatchBytes.isStringAttribute() && |
| 875 | !AttrNumPatchBytes.getValueAsString().getAsInteger(10, NumPatchBytes); |
| 876 | |
| 877 | if (HasNumPatchBytes) |
| 878 | AttrsToRemove.addAttribute("statepoint-num-patch-bytes"); |
| 879 | else |
| 880 | NumPatchBytes = 0; |
| 881 | |
| 882 | OriginalAttrs = OriginalAttrs.removeAttributes( |
| 883 | CS.getInstruction()->getContext(), AttributeSet::FunctionIndex, |
| 884 | AttrsToRemove); |
| 885 | |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 886 | if (CS.isCall()) { |
Sanjoy Das | 93abd81 | 2015-05-06 23:53:19 +0000 | [diff] [blame] | 887 | CallInst *ToReplace = cast<CallInst>(CS.getInstruction()); |
Sanjoy Das | abe1c68 | 2015-05-06 23:53:09 +0000 | [diff] [blame] | 888 | CallInst *Call = Builder.CreateGCStatepointCall( |
Sanjoy Das | cfe41f0 | 2015-07-28 23:50:30 +0000 | [diff] [blame] | 889 | ID, NumPatchBytes, CS.getCalledValue(), |
Sanjoy Das | ba74e64 | 2015-05-13 20:11:31 +0000 | [diff] [blame] | 890 | makeArrayRef(CS.arg_begin(), CS.arg_end()), None, None, |
| 891 | "safepoint_token"); |
Sanjoy Das | 93abd81 | 2015-05-06 23:53:19 +0000 | [diff] [blame] | 892 | Call->setTailCall(ToReplace->isTailCall()); |
| 893 | Call->setCallingConv(ToReplace->getCallingConv()); |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 894 | |
Sanjoy Das | abf1560 | 2015-05-06 23:53:21 +0000 | [diff] [blame] | 895 | // In case if we can handle this set of attributes - set up function |
| 896 | // attributes directly on statepoint and return attributes later for |
| 897 | // gc_result intrinsic. |
| 898 | Call->setAttributes(OriginalAttrs.getFnAttributes()); |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 899 | |
Sanjoy Das | 93abd81 | 2015-05-06 23:53:19 +0000 | [diff] [blame] | 900 | Token = Call; |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 901 | |
Benjamin Kramer | df005cb | 2015-08-08 18:27:36 +0000 | [diff] [blame] | 902 | // Put the following gc_result and gc_relocate calls immediately after |
Sanjoy Das | abf1560 | 2015-05-06 23:53:21 +0000 | [diff] [blame] | 903 | // the old call (which we're about to delete). |
| 904 | assert(ToReplace->getNextNode() && "not a terminator, must have next"); |
| 905 | Builder.SetInsertPoint(ToReplace->getNextNode()); |
| 906 | Builder.SetCurrentDebugLocation(ToReplace->getNextNode()->getDebugLoc()); |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 907 | } else if (CS.isInvoke()) { |
Sanjoy Das | 93abd81 | 2015-05-06 23:53:19 +0000 | [diff] [blame] | 908 | InvokeInst *ToReplace = cast<InvokeInst>(CS.getInstruction()); |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 909 | |
| 910 | // Insert the new invoke into the old block. We'll remove the old one in a |
| 911 | // moment at which point this will become the new terminator for the |
| 912 | // original block. |
Sanjoy Das | 93abd81 | 2015-05-06 23:53:19 +0000 | [diff] [blame] | 913 | Builder.SetInsertPoint(ToReplace->getParent()); |
| 914 | InvokeInst *Invoke = Builder.CreateGCStatepointInvoke( |
Sanjoy Das | cfe41f0 | 2015-07-28 23:50:30 +0000 | [diff] [blame] | 915 | ID, NumPatchBytes, CS.getCalledValue(), ToReplace->getNormalDest(), |
Sanjoy Das | 93abd81 | 2015-05-06 23:53:19 +0000 | [diff] [blame] | 916 | ToReplace->getUnwindDest(), makeArrayRef(CS.arg_begin(), CS.arg_end()), |
Sanjoy Das | 8045810 | 2015-05-15 00:26:15 +0000 | [diff] [blame] | 917 | None, None, "safepoint_token"); |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 918 | |
Sanjoy Das | 2c26614 | 2015-05-15 00:26:21 +0000 | [diff] [blame] | 919 | Invoke->setCallingConv(ToReplace->getCallingConv()); |
| 920 | |
Sanjoy Das | abf1560 | 2015-05-06 23:53:21 +0000 | [diff] [blame] | 921 | // In case if we can handle this set of attributes - set up function |
| 922 | // attributes directly on statepoint and return attributes later for |
| 923 | // gc_result intrinsic. |
Sanjoy Das | 93abd81 | 2015-05-06 23:53:19 +0000 | [diff] [blame] | 924 | Invoke->setAttributes(OriginalAttrs.getFnAttributes()); |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 925 | |
Sanjoy Das | 93abd81 | 2015-05-06 23:53:19 +0000 | [diff] [blame] | 926 | Token = Invoke; |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 927 | |
| 928 | // We'll insert the gc.result into the normal block |
Igor Laevsky | 5e23e16 | 2015-05-08 11:59:09 +0000 | [diff] [blame] | 929 | BasicBlock *NormalDest = ToReplace->getNormalDest(); |
| 930 | // Can not insert gc.result in case of phi nodes preset. |
Benjamin Kramer | df005cb | 2015-08-08 18:27:36 +0000 | [diff] [blame] | 931 | // Should have removed this cases prior to running this function |
Igor Laevsky | 5e23e16 | 2015-05-08 11:59:09 +0000 | [diff] [blame] | 932 | assert(!isa<PHINode>(NormalDest->begin())); |
| 933 | Instruction *IP = &*(NormalDest->getFirstInsertionPt()); |
| 934 | Builder.SetInsertPoint(IP); |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 935 | } else { |
| 936 | llvm_unreachable("unexpect type of CallSite"); |
| 937 | } |
Sanjoy Das | 93abd81 | 2015-05-06 23:53:19 +0000 | [diff] [blame] | 938 | assert(Token); |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 939 | |
| 940 | // Handle the return value of the original call - update all uses to use a |
| 941 | // gc_result hanging off the statepoint node we just inserted |
| 942 | |
| 943 | // Only add the gc_result iff there is actually a used result |
| 944 | if (!CS.getType()->isVoidTy() && !CS.getInstruction()->use_empty()) { |
Sanjoy Das | 93abd81 | 2015-05-06 23:53:19 +0000 | [diff] [blame] | 945 | std::string TakenName = |
| 946 | CS.getInstruction()->hasName() ? CS.getInstruction()->getName() : ""; |
| 947 | CallInst *GCResult = Builder.CreateGCResult(Token, CS.getType(), TakenName); |
Sanjoy Das | abf1560 | 2015-05-06 23:53:21 +0000 | [diff] [blame] | 948 | GCResult->setAttributes(OriginalAttrs.getRetAttributes()); |
Sanjoy Das | 93abd81 | 2015-05-06 23:53:19 +0000 | [diff] [blame] | 949 | return GCResult; |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 950 | } else { |
| 951 | // No return value for the call. |
| 952 | return nullptr; |
| 953 | } |
| 954 | } |