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