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