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" |
Sanjoy Das | 360a4e4 | 2016-01-28 23:03:17 +0000 | [diff] [blame^] | 52 | |
Philip Reames | 5708cca | 2015-05-12 20:43:48 +0000 | [diff] [blame] | 53 | #include "llvm/ADT/SetVector.h" |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 54 | #include "llvm/ADT/Statistic.h" |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 55 | #include "llvm/Analysis/CFG.h" |
Sanjoy Das | 360a4e4 | 2016-01-28 23:03:17 +0000 | [diff] [blame^] | 56 | #include "llvm/Analysis/ScalarEvolution.h" |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 57 | #include "llvm/IR/CallSite.h" |
| 58 | #include "llvm/IR/Dominators.h" |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 59 | #include "llvm/IR/IntrinsicInst.h" |
Sanjoy Das | 360a4e4 | 2016-01-28 23:03:17 +0000 | [diff] [blame^] | 60 | #include "llvm/IR/LegacyPassManager.h" |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 61 | #include "llvm/IR/Statepoint.h" |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 62 | #include "llvm/Support/CommandLine.h" |
Sanjoy Das | 360a4e4 | 2016-01-28 23:03:17 +0000 | [diff] [blame^] | 63 | #include "llvm/Support/Debug.h" |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 64 | #include "llvm/Transforms/Scalar.h" |
| 65 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
| 66 | #include "llvm/Transforms/Utils/Cloning.h" |
| 67 | #include "llvm/Transforms/Utils/Local.h" |
| 68 | |
| 69 | #define DEBUG_TYPE "safepoint-placement" |
| 70 | STATISTIC(NumEntrySafepoints, "Number of entry safepoints inserted"); |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 71 | STATISTIC(NumBackedgeSafepoints, "Number of backedge safepoints inserted"); |
| 72 | |
| 73 | STATISTIC(CallInLoop, "Number of loops w/o safepoints due to calls in loop"); |
| 74 | STATISTIC(FiniteExecution, "Number of loops w/o safepoints finite execution"); |
| 75 | |
| 76 | using namespace llvm; |
| 77 | |
Benjamin Kramer | df005cb | 2015-08-08 18:27:36 +0000 | [diff] [blame] | 78 | // Ignore opportunities to avoid placing safepoints on backedges, useful for |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 79 | // validation |
Philip Reames | 1f3e5c1 | 2015-02-20 23:32:03 +0000 | [diff] [blame] | 80 | static cl::opt<bool> AllBackedges("spp-all-backedges", cl::Hidden, |
| 81 | cl::init(false)); |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 82 | |
Sanjoy Das | f75e15e | 2015-09-15 01:42:48 +0000 | [diff] [blame] | 83 | /// How narrow does the trip count of a loop have to be to have to be considered |
| 84 | /// "counted"? Counted loops do not get safepoints at backedges. |
| 85 | static cl::opt<int> CountedLoopTripWidth("spp-counted-loop-trip-width", |
| 86 | cl::Hidden, cl::init(32)); |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 87 | |
| 88 | // If true, split the backedge of a loop when placing the safepoint, otherwise |
| 89 | // split the latch block itself. Both are useful to support for |
| 90 | // experimentation, but in practice, it looks like splitting the backedge |
| 91 | // optimizes better. |
Philip Reames | 1f3e5c1 | 2015-02-20 23:32:03 +0000 | [diff] [blame] | 92 | static cl::opt<bool> SplitBackedge("spp-split-backedge", cl::Hidden, |
| 93 | cl::init(false)); |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 94 | |
| 95 | // Print tracing output |
Philip Reames | 1f3e5c1 | 2015-02-20 23:32:03 +0000 | [diff] [blame] | 96 | static cl::opt<bool> TraceLSP("spp-trace", cl::Hidden, cl::init(false)); |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 97 | |
| 98 | namespace { |
| 99 | |
Philip Reames | 9f12904 | 2015-05-12 21:09:36 +0000 | [diff] [blame] | 100 | /// An analysis pass whose purpose is to identify each of the backedges in |
| 101 | /// the function which require a safepoint poll to be inserted. |
| 102 | struct PlaceBackedgeSafepointsImpl : public FunctionPass { |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 103 | static char ID; |
| 104 | |
| 105 | /// The output of the pass - gives a list of each backedge (described by |
| 106 | /// pointing at the branch) which need a poll inserted. |
| 107 | std::vector<TerminatorInst *> PollLocations; |
| 108 | |
| 109 | /// 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] | 110 | /// the call-dependent placement opts. |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 111 | bool CallSafepointsEnabled; |
Philip Reames | 9f12904 | 2015-05-12 21:09:36 +0000 | [diff] [blame] | 112 | |
| 113 | ScalarEvolution *SE = nullptr; |
| 114 | DominatorTree *DT = nullptr; |
| 115 | LoopInfo *LI = nullptr; |
NAKAMURA Takumi | fb3bd71 | 2015-05-25 01:43:23 +0000 | [diff] [blame] | 116 | |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 117 | PlaceBackedgeSafepointsImpl(bool CallSafepoints = false) |
Philip Reames | 9f12904 | 2015-05-12 21:09:36 +0000 | [diff] [blame] | 118 | : FunctionPass(ID), CallSafepointsEnabled(CallSafepoints) { |
Philip Reames | 5a9685d | 2015-02-04 00:39:57 +0000 | [diff] [blame] | 119 | initializePlaceBackedgeSafepointsImplPass(*PassRegistry::getPassRegistry()); |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 120 | } |
| 121 | |
Philip Reames | 9f12904 | 2015-05-12 21:09:36 +0000 | [diff] [blame] | 122 | bool runOnLoop(Loop *); |
| 123 | void runOnLoopAndSubLoops(Loop *L) { |
| 124 | // Visit all the subloops |
| 125 | for (auto I = L->begin(), E = L->end(); I != E; I++) |
| 126 | runOnLoopAndSubLoops(*I); |
| 127 | runOnLoop(L); |
| 128 | } |
Justin Bogner | 383749a | 2015-05-12 21:49:47 +0000 | [diff] [blame] | 129 | |
| 130 | bool runOnFunction(Function &F) override { |
Chandler Carruth | 2f1fd16 | 2015-08-17 02:08:17 +0000 | [diff] [blame] | 131 | SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE(); |
Philip Reames | 9f12904 | 2015-05-12 21:09:36 +0000 | [diff] [blame] | 132 | DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree(); |
| 133 | LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo(); |
| 134 | for (auto I = LI->begin(), E = LI->end(); I != E; I++) { |
| 135 | runOnLoopAndSubLoops(*I); |
| 136 | } |
| 137 | return false; |
| 138 | } |
NAKAMURA Takumi | fb3bd71 | 2015-05-25 01:43:23 +0000 | [diff] [blame] | 139 | |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 140 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Philip Reames | 57bdac9 | 2015-05-12 20:56:33 +0000 | [diff] [blame] | 141 | AU.addRequired<DominatorTreeWrapperPass>(); |
Chandler Carruth | 2f1fd16 | 2015-08-17 02:08:17 +0000 | [diff] [blame] | 142 | AU.addRequired<ScalarEvolutionWrapperPass>(); |
Philip Reames | 9f12904 | 2015-05-12 21:09:36 +0000 | [diff] [blame] | 143 | AU.addRequired<LoopInfoWrapperPass>(); |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 144 | // We no longer modify the IR at all in this pass. Thus all |
| 145 | // analysis are preserved. |
| 146 | AU.setPreservesAll(); |
| 147 | } |
| 148 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 149 | } |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 150 | |
Philip Reames | 1f3e5c1 | 2015-02-20 23:32:03 +0000 | [diff] [blame] | 151 | static cl::opt<bool> NoEntry("spp-no-entry", cl::Hidden, cl::init(false)); |
| 152 | static cl::opt<bool> NoCall("spp-no-call", cl::Hidden, cl::init(false)); |
| 153 | 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] | 154 | |
| 155 | namespace { |
Philip Reames | 7b98179 | 2015-05-12 21:21:18 +0000 | [diff] [blame] | 156 | struct PlaceSafepoints : public FunctionPass { |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 157 | static char ID; // Pass identification, replacement for typeid |
| 158 | |
Philip Reames | 7b98179 | 2015-05-12 21:21:18 +0000 | [diff] [blame] | 159 | PlaceSafepoints() : FunctionPass(ID) { |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 160 | initializePlaceSafepointsPass(*PassRegistry::getPassRegistry()); |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 161 | } |
Philip Reames | 7b98179 | 2015-05-12 21:21:18 +0000 | [diff] [blame] | 162 | bool runOnFunction(Function &F) override; |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 163 | |
| 164 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 165 | // We modify the graph wholesale (inlining, block insertion, etc). We |
| 166 | // preserve nothing at the moment. We could potentially preserve dom tree |
| 167 | // if that was worth doing |
| 168 | } |
| 169 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 170 | } |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 171 | |
| 172 | // Insert a safepoint poll immediately before the given instruction. Does |
| 173 | // not handle the parsability of state at the runtime call, that's the |
| 174 | // callers job. |
NAKAMURA Takumi | fb3bd71 | 2015-05-25 01:43:23 +0000 | [diff] [blame] | 175 | static void |
Philip Reames | 38840245 | 2015-05-26 21:03:23 +0000 | [diff] [blame] | 176 | InsertSafepointPoll(Instruction *InsertBefore, |
Philip Reames | 5a9685d | 2015-02-04 00:39:57 +0000 | [diff] [blame] | 177 | std::vector<CallSite> &ParsePointsNeeded /*rval*/); |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 178 | |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 179 | static bool needsStatepoint(const CallSite &CS) { |
Sanjoy Das | c21a05a | 2015-10-08 23:18:30 +0000 | [diff] [blame] | 180 | if (callsGCLeafFunction(CS)) |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 181 | return false; |
| 182 | if (CS.isCall()) { |
| 183 | CallInst *call = cast<CallInst>(CS.getInstruction()); |
| 184 | if (call->isInlineAsm()) |
| 185 | return false; |
| 186 | } |
| 187 | if (isStatepoint(CS) || isGCRelocate(CS) || isGCResult(CS)) { |
| 188 | return false; |
| 189 | } |
| 190 | return true; |
| 191 | } |
| 192 | |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 193 | /// Returns true if this loop is known to contain a call safepoint which |
| 194 | /// must unconditionally execute on any iteration of the loop which returns |
| 195 | /// to the loop header via an edge from Pred. Returns a conservative correct |
| 196 | /// answer; i.e. false is always valid. |
| 197 | static bool containsUnconditionalCallSafepoint(Loop *L, BasicBlock *Header, |
| 198 | BasicBlock *Pred, |
| 199 | DominatorTree &DT) { |
| 200 | // In general, we're looking for any cut of the graph which ensures |
| 201 | // there's a call safepoint along every edge between Header and Pred. |
| 202 | // For the moment, we look only for the 'cuts' that consist of a single call |
| 203 | // instruction in a block which is dominated by the Header and dominates the |
| 204 | // loop latch (Pred) block. Somewhat surprisingly, walking the entire chain |
Benjamin Kramer | df005cb | 2015-08-08 18:27:36 +0000 | [diff] [blame] | 205 | // of such dominating blocks gets substantially more occurrences than just |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 206 | // checking the Pred and Header blocks themselves. This may be due to the |
| 207 | // density of loop exit conditions caused by range and null checks. |
| 208 | // TODO: structure this as an analysis pass, cache the result for subloops, |
| 209 | // avoid dom tree recalculations |
| 210 | assert(DT.dominates(Header, Pred) && "loop latch not dominated by header?"); |
| 211 | |
| 212 | BasicBlock *Current = Pred; |
| 213 | while (true) { |
| 214 | for (Instruction &I : *Current) { |
Benjamin Kramer | 3a09ef6 | 2015-04-10 14:50:08 +0000 | [diff] [blame] | 215 | if (auto CS = CallSite(&I)) |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 216 | // Note: Technically, needing a safepoint isn't quite the right |
| 217 | // condition here. We should instead be checking if the target method |
| 218 | // has an |
| 219 | // unconditional poll. In practice, this is only a theoretical concern |
| 220 | // since we don't have any methods with conditional-only safepoint |
| 221 | // polls. |
| 222 | if (needsStatepoint(CS)) |
| 223 | return true; |
| 224 | } |
| 225 | |
| 226 | if (Current == Header) |
| 227 | break; |
| 228 | Current = DT.getNode(Current)->getIDom()->getBlock(); |
| 229 | } |
| 230 | |
| 231 | return false; |
| 232 | } |
| 233 | |
| 234 | /// Returns true if this loop is known to terminate in a finite number of |
| 235 | /// iterations. Note that this function may return false for a loop which |
| 236 | /// does actual terminate in a finite constant number of iterations due to |
| 237 | /// conservatism in the analysis. |
| 238 | static bool mustBeFiniteCountedLoop(Loop *L, ScalarEvolution *SE, |
Philip Reames | 5a9685d | 2015-02-04 00:39:57 +0000 | [diff] [blame] | 239 | BasicBlock *Pred) { |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 240 | // A conservative bound on the loop as a whole. |
| 241 | const SCEV *MaxTrips = SE->getMaxBackedgeTakenCount(L); |
Sanjoy Das | f75e15e | 2015-09-15 01:42:48 +0000 | [diff] [blame] | 242 | if (MaxTrips != SE->getCouldNotCompute() && |
| 243 | SE->getUnsignedRange(MaxTrips).getUnsignedMax().isIntN( |
| 244 | CountedLoopTripWidth)) |
| 245 | return true; |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 246 | |
| 247 | // If this is a conditional branch to the header with the alternate path |
| 248 | // being outside the loop, we can ask questions about the execution frequency |
| 249 | // of the exit block. |
| 250 | if (L->isLoopExiting(Pred)) { |
| 251 | // This returns an exact expression only. TODO: We really only need an |
| 252 | // upper bound here, but SE doesn't expose that. |
| 253 | const SCEV *MaxExec = SE->getExitCount(L, Pred); |
Sanjoy Das | f75e15e | 2015-09-15 01:42:48 +0000 | [diff] [blame] | 254 | if (MaxExec != SE->getCouldNotCompute() && |
| 255 | SE->getUnsignedRange(MaxExec).getUnsignedMax().isIntN( |
| 256 | CountedLoopTripWidth)) |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 257 | return true; |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | return /* not finite */ false; |
| 261 | } |
| 262 | |
| 263 | static void scanOneBB(Instruction *start, Instruction *end, |
Philip Reames | 5a9685d | 2015-02-04 00:39:57 +0000 | [diff] [blame] | 264 | std::vector<CallInst *> &calls, |
| 265 | std::set<BasicBlock *> &seen, |
| 266 | std::vector<BasicBlock *> &worklist) { |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 267 | for (BasicBlock::iterator itr(start); |
| 268 | itr != start->getParent()->end() && itr != BasicBlock::iterator(end); |
| 269 | itr++) { |
| 270 | if (CallInst *CI = dyn_cast<CallInst>(&*itr)) { |
| 271 | calls.push_back(CI); |
| 272 | } |
| 273 | // FIXME: This code does not handle invokes |
| 274 | assert(!dyn_cast<InvokeInst>(&*itr) && |
| 275 | "support for invokes in poll code needed"); |
| 276 | // Only add the successor blocks if we reach the terminator instruction |
| 277 | // without encountering end first |
| 278 | if (itr->isTerminator()) { |
| 279 | BasicBlock *BB = itr->getParent(); |
Philip Reames | a29de87 | 2015-02-09 22:26:11 +0000 | [diff] [blame] | 280 | for (BasicBlock *Succ : successors(BB)) { |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 281 | if (seen.count(Succ) == 0) { |
| 282 | worklist.push_back(Succ); |
| 283 | seen.insert(Succ); |
| 284 | } |
| 285 | } |
| 286 | } |
| 287 | } |
| 288 | } |
| 289 | static void scanInlinedCode(Instruction *start, Instruction *end, |
Philip Reames | 5a9685d | 2015-02-04 00:39:57 +0000 | [diff] [blame] | 290 | std::vector<CallInst *> &calls, |
| 291 | std::set<BasicBlock *> &seen) { |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 292 | calls.clear(); |
| 293 | std::vector<BasicBlock *> worklist; |
| 294 | seen.insert(start->getParent()); |
| 295 | scanOneBB(start, end, calls, seen, worklist); |
| 296 | while (!worklist.empty()) { |
| 297 | BasicBlock *BB = worklist.back(); |
| 298 | worklist.pop_back(); |
| 299 | scanOneBB(&*BB->begin(), end, calls, seen, worklist); |
| 300 | } |
| 301 | } |
| 302 | |
Philip Reames | 9f12904 | 2015-05-12 21:09:36 +0000 | [diff] [blame] | 303 | bool PlaceBackedgeSafepointsImpl::runOnLoop(Loop *L) { |
Philip Reames | 5708cca | 2015-05-12 20:43:48 +0000 | [diff] [blame] | 304 | // Loop through all loop latches (branches controlling backedges). We need |
NAKAMURA Takumi | fb3bd71 | 2015-05-25 01:43:23 +0000 | [diff] [blame] | 305 | // to place a safepoint on every backedge (potentially). |
Philip Reames | 5708cca | 2015-05-12 20:43:48 +0000 | [diff] [blame] | 306 | // Note: In common usage, there will be only one edge due to LoopSimplify |
| 307 | // having run sometime earlier in the pipeline, but this code must be correct |
| 308 | // w.r.t. loops with multiple backedges. |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 309 | BasicBlock *header = L->getHeader(); |
Philip Reames | 5708cca | 2015-05-12 20:43:48 +0000 | [diff] [blame] | 310 | SmallVector<BasicBlock*, 16> LoopLatches; |
| 311 | L->getLoopLatches(LoopLatches); |
| 312 | for (BasicBlock *pred : LoopLatches) { |
| 313 | assert(L->contains(pred)); |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 314 | |
| 315 | // Make a policy decision about whether this loop needs a safepoint or |
| 316 | // not. Note that this is about unburdening the optimizer in loops, not |
| 317 | // avoiding the runtime cost of the actual safepoint. |
| 318 | if (!AllBackedges) { |
| 319 | if (mustBeFiniteCountedLoop(L, SE, pred)) { |
| 320 | if (TraceLSP) |
| 321 | errs() << "skipping safepoint placement in finite loop\n"; |
| 322 | FiniteExecution++; |
| 323 | continue; |
| 324 | } |
| 325 | if (CallSafepointsEnabled && |
Philip Reames | 57bdac9 | 2015-05-12 20:56:33 +0000 | [diff] [blame] | 326 | containsUnconditionalCallSafepoint(L, header, pred, *DT)) { |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 327 | // Note: This is only semantically legal since we won't do any further |
| 328 | // IPO or inlining before the actual call insertion.. If we hadn't, we |
| 329 | // might latter loose this call safepoint. |
| 330 | if (TraceLSP) |
| 331 | errs() << "skipping safepoint placement due to unconditional call\n"; |
| 332 | CallInLoop++; |
| 333 | continue; |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | // TODO: We can create an inner loop which runs a finite number of |
| 338 | // iterations with an outer loop which contains a safepoint. This would |
| 339 | // not help runtime performance that much, but it might help our ability to |
| 340 | // optimize the inner loop. |
| 341 | |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 342 | // Safepoint insertion would involve creating a new basic block (as the |
| 343 | // target of the current backedge) which does the safepoint (of all live |
| 344 | // variables) and branches to the true header |
| 345 | TerminatorInst *term = pred->getTerminator(); |
| 346 | |
| 347 | if (TraceLSP) { |
| 348 | errs() << "[LSP] terminator instruction: "; |
| 349 | term->dump(); |
| 350 | } |
| 351 | |
| 352 | PollLocations.push_back(term); |
| 353 | } |
| 354 | |
Philip Reames | 5708cca | 2015-05-12 20:43:48 +0000 | [diff] [blame] | 355 | return false; |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 356 | } |
| 357 | |
Philip Reames | d97cdf2 | 2015-05-19 23:40:11 +0000 | [diff] [blame] | 358 | /// 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] | 359 | /// the caller function. |
Philip Reames | d97cdf2 | 2015-05-19 23:40:11 +0000 | [diff] [blame] | 360 | static bool doesNotRequireEntrySafepointBefore(const CallSite &CS) { |
| 361 | Instruction *Inst = CS.getInstruction(); |
| 362 | if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(Inst)) { |
| 363 | switch (II->getIntrinsicID()) { |
| 364 | case Intrinsic::experimental_gc_statepoint: |
| 365 | case Intrinsic::experimental_patchpoint_void: |
| 366 | case Intrinsic::experimental_patchpoint_i64: |
| 367 | // The can wrap an actual call which may grow the stack by an unbounded |
| 368 | // amount or run forever. |
| 369 | return false; |
| 370 | default: |
| 371 | // Most LLVM intrinsics are things which do not expand to actual calls, or |
| 372 | // at least if they do, are leaf functions that cause only finite stack |
| 373 | // growth. In particular, the optimizer likes to form things like memsets |
| 374 | // out of stores in the original IR. Another important example is |
Reid Kleckner | 6038179 | 2015-07-07 22:25:32 +0000 | [diff] [blame] | 375 | // llvm.localescape which must occur in the entry block. Inserting a |
| 376 | // safepoint before it is not legal since it could push the localescape |
Philip Reames | d97cdf2 | 2015-05-19 23:40:11 +0000 | [diff] [blame] | 377 | // out of the entry block. |
| 378 | return true; |
| 379 | } |
| 380 | } |
| 381 | return false; |
| 382 | } |
| 383 | |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 384 | static Instruction *findLocationForEntrySafepoint(Function &F, |
| 385 | DominatorTree &DT) { |
| 386 | |
| 387 | // Conceptually, this poll needs to be on method entry, but in |
| 388 | // practice, we place it as late in the entry block as possible. We |
| 389 | // can place it as late as we want as long as it dominates all calls |
| 390 | // that can grow the stack. This, combined with backedge polls, |
| 391 | // give us all the progress guarantees we need. |
| 392 | |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 393 | // hasNextInstruction and nextInstruction are used to iterate |
| 394 | // through a "straight line" execution sequence. |
| 395 | |
| 396 | auto hasNextInstruction = [](Instruction *I) { |
| 397 | if (!I->isTerminator()) { |
| 398 | return true; |
| 399 | } |
| 400 | BasicBlock *nextBB = I->getParent()->getUniqueSuccessor(); |
| 401 | return nextBB && (nextBB->getUniquePredecessor() != nullptr); |
| 402 | }; |
| 403 | |
| 404 | auto nextInstruction = [&hasNextInstruction](Instruction *I) { |
| 405 | assert(hasNextInstruction(I) && |
| 406 | "first check if there is a next instruction!"); |
| 407 | if (I->isTerminator()) { |
Duncan P. N. Exon Smith | be4d8cb | 2015-10-13 19:26:58 +0000 | [diff] [blame] | 408 | return &I->getParent()->getUniqueSuccessor()->front(); |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 409 | } else { |
Duncan P. N. Exon Smith | be4d8cb | 2015-10-13 19:26:58 +0000 | [diff] [blame] | 410 | return &*++I->getIterator(); |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 411 | } |
| 412 | }; |
| 413 | |
| 414 | Instruction *cursor = nullptr; |
Duncan P. N. Exon Smith | be4d8cb | 2015-10-13 19:26:58 +0000 | [diff] [blame] | 415 | for (cursor = &F.getEntryBlock().front(); hasNextInstruction(cursor); |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 416 | cursor = nextInstruction(cursor)) { |
| 417 | |
Philip Reames | d97cdf2 | 2015-05-19 23:40:11 +0000 | [diff] [blame] | 418 | // We need to ensure a safepoint poll occurs before any 'real' call. The |
| 419 | // easiest way to ensure finite execution between safepoints in the face of |
| 420 | // recursive and mutually recursive functions is to enforce that each take |
| 421 | // a safepoint. Additionally, we need to ensure a poll before any call |
| 422 | // which can grow the stack by an unbounded amount. This isn't required |
| 423 | // for GC semantics per se, but is a common requirement for languages |
| 424 | // which detect stack overflow via guard pages and then throw exceptions. |
| 425 | if (auto CS = CallSite(cursor)) { |
| 426 | if (doesNotRequireEntrySafepointBefore(CS)) |
| 427 | continue; |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 428 | break; |
| 429 | } |
| 430 | } |
| 431 | |
Philip Reames | 5a9685d | 2015-02-04 00:39:57 +0000 | [diff] [blame] | 432 | assert((hasNextInstruction(cursor) || cursor->isTerminator()) && |
| 433 | "either we stopped because of a call, or because of terminator"); |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 434 | |
Philip Reames | 52e7a59 | 2015-05-26 21:16:42 +0000 | [diff] [blame] | 435 | return cursor; |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 436 | } |
| 437 | |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 438 | /// Implement a unique function which doesn't require we sort the input |
| 439 | /// vector. Doing so has the effect of changing the output of a couple of |
| 440 | /// tests in ways which make them less useful in testing fused safepoints. |
| 441 | template <typename T> static void unique_unsorted(std::vector<T> &vec) { |
| 442 | std::set<T> seen; |
| 443 | std::vector<T> tmp; |
| 444 | vec.reserve(vec.size()); |
| 445 | std::swap(tmp, vec); |
| 446 | for (auto V : tmp) { |
| 447 | if (seen.insert(V).second) { |
| 448 | vec.push_back(V); |
| 449 | } |
| 450 | } |
| 451 | } |
| 452 | |
Benjamin Kramer | 82f8652 | 2015-06-07 16:36:28 +0000 | [diff] [blame] | 453 | static const char *const GCSafepointPollName = "gc.safepoint_poll"; |
Philip Reames | b1ed02f | 2015-02-09 21:48:05 +0000 | [diff] [blame] | 454 | |
| 455 | static bool isGCSafepointPoll(Function &F) { |
| 456 | return F.getName().equals(GCSafepointPollName); |
| 457 | } |
| 458 | |
Philip Reames | 0b1b387 | 2015-02-21 00:09:09 +0000 | [diff] [blame] | 459 | /// Returns true if this function should be rewritten to include safepoint |
| 460 | /// 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] | 461 | /// an extension point for custom logic. |
Philip Reames | 0b1b387 | 2015-02-21 00:09:09 +0000 | [diff] [blame] | 462 | static bool shouldRewriteFunction(Function &F) { |
| 463 | // TODO: This should check the GCStrategy |
| 464 | if (F.hasGC()) { |
Mehdi Amini | 599ebf2 | 2016-01-08 02:28:20 +0000 | [diff] [blame] | 465 | const auto &FunctionGCName = F.getGC(); |
NAKAMURA Takumi | fb3bd71 | 2015-05-25 01:43:23 +0000 | [diff] [blame] | 466 | const StringRef StatepointExampleName("statepoint-example"); |
| 467 | const StringRef CoreCLRName("coreclr"); |
| 468 | return (StatepointExampleName == FunctionGCName) || |
NAKAMURA Takumi | 5582a6a | 2015-05-25 01:43:34 +0000 | [diff] [blame] | 469 | (CoreCLRName == FunctionGCName); |
Philip Reames | 0b1b387 | 2015-02-21 00:09:09 +0000 | [diff] [blame] | 470 | } else |
| 471 | return false; |
| 472 | } |
| 473 | |
| 474 | // TODO: These should become properties of the GCStrategy, possibly with |
| 475 | // command line overrides. |
| 476 | static bool enableEntrySafepoints(Function &F) { return !NoEntry; } |
| 477 | static bool enableBackedgeSafepoints(Function &F) { return !NoBackedge; } |
| 478 | static bool enableCallSafepoints(Function &F) { return !NoCall; } |
| 479 | |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 480 | bool PlaceSafepoints::runOnFunction(Function &F) { |
| 481 | if (F.isDeclaration() || F.empty()) { |
| 482 | // This is a declaration, nothing to do. Must exit early to avoid crash in |
| 483 | // dom tree calculation |
| 484 | return false; |
| 485 | } |
| 486 | |
Philip Reames | 7e7dc3e | 2015-02-10 00:04:53 +0000 | [diff] [blame] | 487 | if (isGCSafepointPoll(F)) { |
| 488 | // Given we're inlining this inside of safepoint poll insertion, this |
| 489 | // 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] | 490 | // parseable after we inline a poll. |
Philip Reames | 7e7dc3e | 2015-02-10 00:04:53 +0000 | [diff] [blame] | 491 | return false; |
| 492 | } |
| 493 | |
Philip Reames | 0b1b387 | 2015-02-21 00:09:09 +0000 | [diff] [blame] | 494 | if (!shouldRewriteFunction(F)) |
| 495 | return false; |
| 496 | |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 497 | bool modified = false; |
| 498 | |
| 499 | // In various bits below, we rely on the fact that uses are reachable from |
| 500 | // defs. When there are basic blocks unreachable from the entry, dominance |
| 501 | // and reachablity queries return non-sensical results. Thus, we preprocess |
| 502 | // the function to ensure these properties hold. |
| 503 | modified |= removeUnreachableBlocks(F); |
| 504 | |
| 505 | // STEP 1 - Insert the safepoint polling locations. We do not need to |
| 506 | // actually insert parse points yet. That will be done for all polls and |
| 507 | // calls in a single pass. |
| 508 | |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 509 | DominatorTree DT; |
Philip Reames | 4d1a3ef | 2015-05-13 00:32:23 +0000 | [diff] [blame] | 510 | DT.recalculate(F); |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 511 | |
Philip Reames | 4d1a3ef | 2015-05-13 00:32:23 +0000 | [diff] [blame] | 512 | SmallVector<Instruction *, 16> PollsNeeded; |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 513 | std::vector<CallSite> ParsePointNeeded; |
| 514 | |
Philip Reames | 0b1b387 | 2015-02-21 00:09:09 +0000 | [diff] [blame] | 515 | if (enableBackedgeSafepoints(F)) { |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 516 | // Construct a pass manager to run the LoopPass backedge logic. We |
| 517 | // need the pass manager to handle scheduling all the loop passes |
| 518 | // appropriately. Doing this by hand is painful and just not worth messing |
| 519 | // with for the moment. |
Chandler Carruth | 30d69c2 | 2015-02-13 10:01:29 +0000 | [diff] [blame] | 520 | legacy::FunctionPassManager FPM(F.getParent()); |
Philip Reames | 0b1b387 | 2015-02-21 00:09:09 +0000 | [diff] [blame] | 521 | bool CanAssumeCallSafepoints = enableCallSafepoints(F); |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 522 | PlaceBackedgeSafepointsImpl *PBS = |
Philip Reames | b1ed02f | 2015-02-09 21:48:05 +0000 | [diff] [blame] | 523 | new PlaceBackedgeSafepointsImpl(CanAssumeCallSafepoints); |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 524 | FPM.add(PBS); |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 525 | FPM.run(F); |
| 526 | |
| 527 | // We preserve dominance information when inserting the poll, otherwise |
| 528 | // we'd have to recalculate this on every insert |
| 529 | DT.recalculate(F); |
| 530 | |
Philip Reames | 5708cca | 2015-05-12 20:43:48 +0000 | [diff] [blame] | 531 | auto &PollLocations = PBS->PollLocations; |
| 532 | |
| 533 | auto OrderByBBName = [](Instruction *a, Instruction *b) { |
| 534 | return a->getParent()->getName() < b->getParent()->getName(); |
| 535 | }; |
| 536 | // We need the order of list to be stable so that naming ends up stable |
| 537 | // when we split edges. This makes test cases much easier to write. |
| 538 | std::sort(PollLocations.begin(), PollLocations.end(), OrderByBBName); |
| 539 | |
| 540 | // We can sometimes end up with duplicate poll locations. This happens if |
| 541 | // a single loop is visited more than once. The fact this happens seems |
| 542 | // wrong, but it does happen for the split-backedge.ll test case. |
| 543 | PollLocations.erase(std::unique(PollLocations.begin(), |
| 544 | PollLocations.end()), |
| 545 | PollLocations.end()); |
| 546 | |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 547 | // Insert a poll at each point the analysis pass identified |
Philip Reames | 89fe570 | 2015-05-12 23:39:23 +0000 | [diff] [blame] | 548 | // The poll location must be the terminator of a loop latch block. |
| 549 | for (TerminatorInst *Term : PollLocations) { |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 550 | // We are inserting a poll, the function is modified |
| 551 | modified = true; |
NAKAMURA Takumi | fb3bd71 | 2015-05-25 01:43:23 +0000 | [diff] [blame] | 552 | |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 553 | if (SplitBackedge) { |
| 554 | // Split the backedge of the loop and insert the poll within that new |
| 555 | // basic block. This creates a loop with two latches per original |
| 556 | // latch (which is non-ideal), but this appears to be easier to |
| 557 | // optimize in practice than inserting the poll immediately before the |
| 558 | // latch test. |
| 559 | |
| 560 | // Since this is a latch, at least one of the successors must dominate |
| 561 | // it. Its possible that we have a) duplicate edges to the same header |
| 562 | // 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] | 563 | // each. |
| 564 | SetVector<BasicBlock *> Headers; |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 565 | for (unsigned i = 0; i < Term->getNumSuccessors(); i++) { |
| 566 | BasicBlock *Succ = Term->getSuccessor(i); |
| 567 | if (DT.dominates(Succ, Term->getParent())) { |
| 568 | Headers.insert(Succ); |
| 569 | } |
| 570 | } |
| 571 | assert(!Headers.empty() && "poll location is not a loop latch?"); |
| 572 | |
| 573 | // The split loop structure here is so that we only need to recalculate |
| 574 | // the dominator tree once. Alternatively, we could just keep it up to |
| 575 | // date and use a more natural merged loop. |
Philip Reames | 5708cca | 2015-05-12 20:43:48 +0000 | [diff] [blame] | 576 | SetVector<BasicBlock *> SplitBackedges; |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 577 | for (BasicBlock *Header : Headers) { |
Philip Reames | 89fe570 | 2015-05-12 23:39:23 +0000 | [diff] [blame] | 578 | BasicBlock *NewBB = SplitEdge(Term->getParent(), Header, &DT); |
Philip Reames | 4d1a3ef | 2015-05-13 00:32:23 +0000 | [diff] [blame] | 579 | PollsNeeded.push_back(NewBB->getTerminator()); |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 580 | NumBackedgeSafepoints++; |
| 581 | } |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 582 | } else { |
| 583 | // Split the latch block itself, right before the terminator. |
Philip Reames | 4d1a3ef | 2015-05-13 00:32:23 +0000 | [diff] [blame] | 584 | PollsNeeded.push_back(Term); |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 585 | NumBackedgeSafepoints++; |
| 586 | } |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 587 | } |
| 588 | } |
| 589 | |
Philip Reames | 0b1b387 | 2015-02-21 00:09:09 +0000 | [diff] [blame] | 590 | if (enableEntrySafepoints(F)) { |
Philip Reames | 4d1a3ef | 2015-05-13 00:32:23 +0000 | [diff] [blame] | 591 | Instruction *Location = findLocationForEntrySafepoint(F, DT); |
| 592 | if (!Location) { |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 593 | // policy choice not to insert? |
| 594 | } else { |
Philip Reames | 4d1a3ef | 2015-05-13 00:32:23 +0000 | [diff] [blame] | 595 | PollsNeeded.push_back(Location); |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 596 | modified = true; |
| 597 | NumEntrySafepoints++; |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 598 | } |
| 599 | } |
| 600 | |
Philip Reames | 4d1a3ef | 2015-05-13 00:32:23 +0000 | [diff] [blame] | 601 | // Now that we've identified all the needed safepoint poll locations, insert |
| 602 | // safepoint polls themselves. |
| 603 | for (Instruction *PollLocation : PollsNeeded) { |
| 604 | std::vector<CallSite> RuntimeCalls; |
| 605 | InsertSafepointPoll(PollLocation, RuntimeCalls); |
| 606 | ParsePointNeeded.insert(ParsePointNeeded.end(), RuntimeCalls.begin(), |
| 607 | RuntimeCalls.end()); |
| 608 | } |
Sanjoy Das | 9563974 | 2016-01-22 21:02:55 +0000 | [diff] [blame] | 609 | |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 610 | return modified; |
| 611 | } |
| 612 | |
| 613 | char PlaceBackedgeSafepointsImpl::ID = 0; |
| 614 | char PlaceSafepoints::ID = 0; |
| 615 | |
Philip Reames | 7b98179 | 2015-05-12 21:21:18 +0000 | [diff] [blame] | 616 | FunctionPass *llvm::createPlaceSafepointsPass() { |
| 617 | return new PlaceSafepoints(); |
| 618 | } |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 619 | |
| 620 | INITIALIZE_PASS_BEGIN(PlaceBackedgeSafepointsImpl, |
| 621 | "place-backedge-safepoints-impl", |
| 622 | "Place Backedge Safepoints", false, false) |
Chandler Carruth | 2f1fd16 | 2015-08-17 02:08:17 +0000 | [diff] [blame] | 623 | INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass) |
Philip Reames | 57bdac9 | 2015-05-12 20:56:33 +0000 | [diff] [blame] | 624 | INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) |
Philip Reames | 9f12904 | 2015-05-12 21:09:36 +0000 | [diff] [blame] | 625 | INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass) |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 626 | INITIALIZE_PASS_END(PlaceBackedgeSafepointsImpl, |
| 627 | "place-backedge-safepoints-impl", |
| 628 | "Place Backedge Safepoints", false, false) |
| 629 | |
| 630 | INITIALIZE_PASS_BEGIN(PlaceSafepoints, "place-safepoints", "Place Safepoints", |
| 631 | false, false) |
| 632 | INITIALIZE_PASS_END(PlaceSafepoints, "place-safepoints", "Place Safepoints", |
| 633 | false, false) |
| 634 | |
Philip Reames | 5a9685d | 2015-02-04 00:39:57 +0000 | [diff] [blame] | 635 | static void |
Philip Reames | 38840245 | 2015-05-26 21:03:23 +0000 | [diff] [blame] | 636 | InsertSafepointPoll(Instruction *InsertBefore, |
Philip Reames | 5a9685d | 2015-02-04 00:39:57 +0000 | [diff] [blame] | 637 | std::vector<CallSite> &ParsePointsNeeded /*rval*/) { |
Philip Reames | 38840245 | 2015-05-26 21:03:23 +0000 | [diff] [blame] | 638 | BasicBlock *OrigBB = InsertBefore->getParent(); |
| 639 | Module *M = InsertBefore->getModule(); |
| 640 | assert(M && "must be part of a module"); |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 641 | |
| 642 | // Inline the safepoint poll implementation - this will get all the branch, |
| 643 | // control flow, etc.. Most importantly, it will introduce the actual slow |
| 644 | // path call - where we need to insert a safepoint (parsepoint). |
Philip Reames | 38840245 | 2015-05-26 21:03:23 +0000 | [diff] [blame] | 645 | |
| 646 | auto *F = M->getFunction(GCSafepointPollName); |
Manuel Jacob | e3773d6 | 2015-12-29 21:57:55 +0000 | [diff] [blame] | 647 | assert(F && "gc.safepoint_poll function is missing"); |
Manuel Jacob | 5f6eaac | 2016-01-16 20:30:46 +0000 | [diff] [blame] | 648 | assert(F->getValueType() == |
Philip Reames | 38840245 | 2015-05-26 21:03:23 +0000 | [diff] [blame] | 649 | FunctionType::get(Type::getVoidTy(M->getContext()), false) && |
| 650 | "gc.safepoint_poll declared with wrong type"); |
Ramkumar Ramachandra | 3edf74f | 2015-02-09 23:02:10 +0000 | [diff] [blame] | 651 | assert(!F->empty() && "gc.safepoint_poll must be a non-empty function"); |
Philip Reames | 38840245 | 2015-05-26 21:03:23 +0000 | [diff] [blame] | 652 | CallInst *PollCall = CallInst::Create(F, "", InsertBefore); |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 653 | |
| 654 | // Record some information about the call site we're replacing |
Philip Reames | 38840245 | 2015-05-26 21:03:23 +0000 | [diff] [blame] | 655 | BasicBlock::iterator before(PollCall), after(PollCall); |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 656 | bool isBegin(false); |
Philip Reames | 38840245 | 2015-05-26 21:03:23 +0000 | [diff] [blame] | 657 | if (before == OrigBB->begin()) { |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 658 | isBegin = true; |
| 659 | } else { |
| 660 | before--; |
| 661 | } |
| 662 | after++; |
Philip Reames | 38840245 | 2015-05-26 21:03:23 +0000 | [diff] [blame] | 663 | assert(after != OrigBB->end() && "must have successor"); |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 664 | |
| 665 | // do the actual inlining |
| 666 | InlineFunctionInfo IFI; |
Philip Reames | 38840245 | 2015-05-26 21:03:23 +0000 | [diff] [blame] | 667 | bool InlineStatus = InlineFunction(PollCall, IFI); |
| 668 | assert(InlineStatus && "inline must succeed"); |
| 669 | (void)InlineStatus; // suppress warning in release-asserts |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 670 | |
| 671 | // Check post conditions |
| 672 | assert(IFI.StaticAllocas.empty() && "can't have allocs"); |
| 673 | |
| 674 | std::vector<CallInst *> calls; // new calls |
| 675 | std::set<BasicBlock *> BBs; // new BBs + insertee |
| 676 | // Include only the newly inserted instructions, Note: begin may not be valid |
| 677 | // if we inserted to the beginning of the basic block |
| 678 | BasicBlock::iterator start; |
| 679 | if (isBegin) { |
| 680 | start = OrigBB->begin(); |
| 681 | } else { |
| 682 | start = before; |
| 683 | start++; |
| 684 | } |
| 685 | |
| 686 | // If your poll function includes an unreachable at the end, that's not |
| 687 | // valid. Bugpoint likes to create this, so check for it. |
| 688 | assert(isPotentiallyReachable(&*start, &*after, nullptr, nullptr) && |
| 689 | "malformed poll function"); |
| 690 | |
| 691 | scanInlinedCode(&*(start), &*(after), calls, BBs); |
Philip Reames | 47cc673 | 2015-02-04 00:37:33 +0000 | [diff] [blame] | 692 | assert(!calls.empty() && "slow path not found for safepoint poll"); |
| 693 | |
| 694 | // Record the fact we need a parsable state at the runtime call contained in |
| 695 | // the poll function. This is required so that the runtime knows how to |
| 696 | // parse the last frame when we actually take the safepoint (i.e. execute |
| 697 | // the slow path) |
| 698 | assert(ParsePointsNeeded.empty()); |
| 699 | for (size_t i = 0; i < calls.size(); i++) { |
| 700 | |
| 701 | // No safepoint needed or wanted |
| 702 | if (!needsStatepoint(calls[i])) { |
| 703 | continue; |
| 704 | } |
| 705 | |
| 706 | // These are likely runtime calls. Should we assert that via calling |
| 707 | // convention or something? |
| 708 | ParsePointsNeeded.push_back(CallSite(calls[i])); |
| 709 | } |
| 710 | assert(ParsePointsNeeded.size() <= calls.size()); |
| 711 | } |