10b57cec5SDimitry Andric //===- PlaceSafepoints.cpp - Place GC Safepoints --------------------------===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric // 90b57cec5SDimitry Andric // Place garbage collection safepoints at appropriate locations in the IR. This 100b57cec5SDimitry Andric // does not make relocation semantics or variable liveness explicit. That's 110b57cec5SDimitry Andric // done by RewriteStatepointsForGC. 120b57cec5SDimitry Andric // 130b57cec5SDimitry Andric // Terminology: 140b57cec5SDimitry Andric // - A call is said to be "parseable" if there is a stack map generated for the 150b57cec5SDimitry Andric // return PC of the call. A runtime can determine where values listed in the 160b57cec5SDimitry Andric // deopt arguments and (after RewriteStatepointsForGC) gc arguments are located 170b57cec5SDimitry Andric // on the stack when the code is suspended inside such a call. Every parse 180b57cec5SDimitry Andric // point is represented by a call wrapped in an gc.statepoint intrinsic. 190b57cec5SDimitry Andric // - A "poll" is an explicit check in the generated code to determine if the 200b57cec5SDimitry Andric // runtime needs the generated code to cooperate by calling a helper routine 210b57cec5SDimitry Andric // and thus suspending its execution at a known state. The call to the helper 220b57cec5SDimitry Andric // routine will be parseable. The (gc & runtime specific) logic of a poll is 230b57cec5SDimitry Andric // assumed to be provided in a function of the name "gc.safepoint_poll". 240b57cec5SDimitry Andric // 250b57cec5SDimitry Andric // We aim to insert polls such that running code can quickly be brought to a 260b57cec5SDimitry Andric // well defined state for inspection by the collector. In the current 270b57cec5SDimitry Andric // implementation, this is done via the insertion of poll sites at method entry 280b57cec5SDimitry Andric // and the backedge of most loops. We try to avoid inserting more polls than 290b57cec5SDimitry Andric // are necessary to ensure a finite period between poll sites. This is not 300b57cec5SDimitry Andric // because the poll itself is expensive in the generated code; it's not. Polls 310b57cec5SDimitry Andric // do tend to impact the optimizer itself in negative ways; we'd like to avoid 320b57cec5SDimitry Andric // perturbing the optimization of the method as much as we can. 330b57cec5SDimitry Andric // 340b57cec5SDimitry Andric // We also need to make most call sites parseable. The callee might execute a 350b57cec5SDimitry Andric // poll (or otherwise be inspected by the GC). If so, the entire stack 360b57cec5SDimitry Andric // (including the suspended frame of the current method) must be parseable. 370b57cec5SDimitry Andric // 380b57cec5SDimitry Andric // This pass will insert: 390b57cec5SDimitry Andric // - Call parse points ("call safepoints") for any call which may need to 400b57cec5SDimitry Andric // reach a safepoint during the execution of the callee function. 410b57cec5SDimitry Andric // - Backedge safepoint polls and entry safepoint polls to ensure that 420b57cec5SDimitry Andric // executing code reaches a safepoint poll in a finite amount of time. 430b57cec5SDimitry Andric // 440b57cec5SDimitry Andric // We do not currently support return statepoints, but adding them would not 450b57cec5SDimitry Andric // be hard. They are not required for correctness - entry safepoints are an 460b57cec5SDimitry Andric // alternative - but some GCs may prefer them. Patches welcome. 470b57cec5SDimitry Andric // 480b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 490b57cec5SDimitry Andric 5006c3fb27SDimitry Andric #include "llvm/Transforms/Scalar/PlaceSafepoints.h" 51480093f4SDimitry Andric #include "llvm/InitializePasses.h" 520b57cec5SDimitry Andric #include "llvm/Pass.h" 530b57cec5SDimitry Andric 540b57cec5SDimitry Andric #include "llvm/ADT/SetVector.h" 550b57cec5SDimitry Andric #include "llvm/ADT/Statistic.h" 560b57cec5SDimitry Andric #include "llvm/Analysis/CFG.h" 5781ad6265SDimitry Andric #include "llvm/Analysis/LoopInfo.h" 580b57cec5SDimitry Andric #include "llvm/Analysis/ScalarEvolution.h" 590b57cec5SDimitry Andric #include "llvm/Analysis/TargetLibraryInfo.h" 600b57cec5SDimitry Andric #include "llvm/IR/Dominators.h" 610b57cec5SDimitry Andric #include "llvm/IR/IntrinsicInst.h" 620b57cec5SDimitry Andric #include "llvm/IR/LegacyPassManager.h" 63*0fca6ea1SDimitry Andric #include "llvm/IR/Module.h" 640b57cec5SDimitry Andric #include "llvm/IR/Statepoint.h" 650b57cec5SDimitry Andric #include "llvm/Support/CommandLine.h" 660b57cec5SDimitry Andric #include "llvm/Support/Debug.h" 670b57cec5SDimitry Andric #include "llvm/Transforms/Scalar.h" 680b57cec5SDimitry Andric #include "llvm/Transforms/Utils/BasicBlockUtils.h" 690b57cec5SDimitry Andric #include "llvm/Transforms/Utils/Cloning.h" 7081ad6265SDimitry Andric #include "llvm/Transforms/Utils/Local.h" 710b57cec5SDimitry Andric 7206c3fb27SDimitry Andric using namespace llvm; 7306c3fb27SDimitry Andric 7406c3fb27SDimitry Andric #define DEBUG_TYPE "place-safepoints" 750b57cec5SDimitry Andric 760b57cec5SDimitry Andric STATISTIC(NumEntrySafepoints, "Number of entry safepoints inserted"); 770b57cec5SDimitry Andric STATISTIC(NumBackedgeSafepoints, "Number of backedge safepoints inserted"); 780b57cec5SDimitry Andric 790b57cec5SDimitry Andric STATISTIC(CallInLoop, 800b57cec5SDimitry Andric "Number of loops without safepoints due to calls in loop"); 810b57cec5SDimitry Andric STATISTIC(FiniteExecution, 820b57cec5SDimitry Andric "Number of loops without safepoints finite execution"); 830b57cec5SDimitry Andric 840b57cec5SDimitry Andric // Ignore opportunities to avoid placing safepoints on backedges, useful for 850b57cec5SDimitry Andric // validation 860b57cec5SDimitry Andric static cl::opt<bool> AllBackedges("spp-all-backedges", cl::Hidden, 870b57cec5SDimitry Andric cl::init(false)); 880b57cec5SDimitry Andric 890b57cec5SDimitry Andric /// How narrow does the trip count of a loop have to be to have to be considered 900b57cec5SDimitry Andric /// "counted"? Counted loops do not get safepoints at backedges. 910b57cec5SDimitry Andric static cl::opt<int> CountedLoopTripWidth("spp-counted-loop-trip-width", 920b57cec5SDimitry Andric cl::Hidden, cl::init(32)); 930b57cec5SDimitry Andric 940b57cec5SDimitry Andric // If true, split the backedge of a loop when placing the safepoint, otherwise 950b57cec5SDimitry Andric // split the latch block itself. Both are useful to support for 960b57cec5SDimitry Andric // experimentation, but in practice, it looks like splitting the backedge 970b57cec5SDimitry Andric // optimizes better. 980b57cec5SDimitry Andric static cl::opt<bool> SplitBackedge("spp-split-backedge", cl::Hidden, 990b57cec5SDimitry Andric cl::init(false)); 1000b57cec5SDimitry Andric 1010b57cec5SDimitry Andric namespace { 1020b57cec5SDimitry Andric /// An analysis pass whose purpose is to identify each of the backedges in 1030b57cec5SDimitry Andric /// the function which require a safepoint poll to be inserted. 10406c3fb27SDimitry Andric class PlaceBackedgeSafepointsLegacyPass : public FunctionPass { 10506c3fb27SDimitry Andric public: 1060b57cec5SDimitry Andric static char ID; 1070b57cec5SDimitry Andric 1080b57cec5SDimitry Andric /// The output of the pass - gives a list of each backedge (described by 1090b57cec5SDimitry Andric /// pointing at the branch) which need a poll inserted. 1100b57cec5SDimitry Andric std::vector<Instruction *> PollLocations; 1110b57cec5SDimitry Andric 1120b57cec5SDimitry Andric /// True unless we're running spp-no-calls in which case we need to disable 1130b57cec5SDimitry Andric /// the call-dependent placement opts. 1140b57cec5SDimitry Andric bool CallSafepointsEnabled; 1150b57cec5SDimitry Andric 11606c3fb27SDimitry Andric PlaceBackedgeSafepointsLegacyPass(bool CallSafepoints = false) 1170b57cec5SDimitry Andric : FunctionPass(ID), CallSafepointsEnabled(CallSafepoints) { 11806c3fb27SDimitry Andric initializePlaceBackedgeSafepointsLegacyPassPass( 11906c3fb27SDimitry Andric *PassRegistry::getPassRegistry()); 1200b57cec5SDimitry Andric } 1210b57cec5SDimitry Andric 1220b57cec5SDimitry Andric bool runOnLoop(Loop *); 12306c3fb27SDimitry Andric 1240b57cec5SDimitry Andric void runOnLoopAndSubLoops(Loop *L) { 1250b57cec5SDimitry Andric // Visit all the subloops 1260b57cec5SDimitry Andric for (Loop *I : *L) 1270b57cec5SDimitry Andric runOnLoopAndSubLoops(I); 1280b57cec5SDimitry Andric runOnLoop(L); 1290b57cec5SDimitry Andric } 1300b57cec5SDimitry Andric 1310b57cec5SDimitry Andric bool runOnFunction(Function &F) override { 1320b57cec5SDimitry Andric SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE(); 1330b57cec5SDimitry Andric DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree(); 1340b57cec5SDimitry Andric LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo(); 1358bcb0991SDimitry Andric TLI = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F); 1360b57cec5SDimitry Andric for (Loop *I : *LI) { 1370b57cec5SDimitry Andric runOnLoopAndSubLoops(I); 1380b57cec5SDimitry Andric } 1390b57cec5SDimitry Andric return false; 1400b57cec5SDimitry Andric } 1410b57cec5SDimitry Andric 1420b57cec5SDimitry Andric void getAnalysisUsage(AnalysisUsage &AU) const override { 1430b57cec5SDimitry Andric AU.addRequired<DominatorTreeWrapperPass>(); 1440b57cec5SDimitry Andric AU.addRequired<ScalarEvolutionWrapperPass>(); 1450b57cec5SDimitry Andric AU.addRequired<LoopInfoWrapperPass>(); 1460b57cec5SDimitry Andric AU.addRequired<TargetLibraryInfoWrapperPass>(); 1470b57cec5SDimitry Andric // We no longer modify the IR at all in this pass. Thus all 1480b57cec5SDimitry Andric // analysis are preserved. 1490b57cec5SDimitry Andric AU.setPreservesAll(); 1500b57cec5SDimitry Andric } 15106c3fb27SDimitry Andric 15206c3fb27SDimitry Andric private: 15306c3fb27SDimitry Andric ScalarEvolution *SE = nullptr; 15406c3fb27SDimitry Andric DominatorTree *DT = nullptr; 15506c3fb27SDimitry Andric LoopInfo *LI = nullptr; 15606c3fb27SDimitry Andric TargetLibraryInfo *TLI = nullptr; 1570b57cec5SDimitry Andric }; 15806c3fb27SDimitry Andric } // namespace 1590b57cec5SDimitry Andric 1600b57cec5SDimitry Andric static cl::opt<bool> NoEntry("spp-no-entry", cl::Hidden, cl::init(false)); 1610b57cec5SDimitry Andric static cl::opt<bool> NoCall("spp-no-call", cl::Hidden, cl::init(false)); 1620b57cec5SDimitry Andric static cl::opt<bool> NoBackedge("spp-no-backedge", cl::Hidden, cl::init(false)); 1630b57cec5SDimitry Andric 16406c3fb27SDimitry Andric char PlaceBackedgeSafepointsLegacyPass::ID = 0; 1650b57cec5SDimitry Andric 16606c3fb27SDimitry Andric INITIALIZE_PASS_BEGIN(PlaceBackedgeSafepointsLegacyPass, 16706c3fb27SDimitry Andric "place-backedge-safepoints-impl", 16806c3fb27SDimitry Andric "Place Backedge Safepoints", false, false) 16906c3fb27SDimitry Andric INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass) 17006c3fb27SDimitry Andric INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) 17106c3fb27SDimitry Andric INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass) 17206c3fb27SDimitry Andric INITIALIZE_PASS_END(PlaceBackedgeSafepointsLegacyPass, 17306c3fb27SDimitry Andric "place-backedge-safepoints-impl", 17406c3fb27SDimitry Andric "Place Backedge Safepoints", false, false) 1750b57cec5SDimitry Andric 17606c3fb27SDimitry Andric static bool containsUnconditionalCallSafepoint(Loop *L, BasicBlock *Header, 17706c3fb27SDimitry Andric BasicBlock *Pred, 17806c3fb27SDimitry Andric DominatorTree &DT, 17906c3fb27SDimitry Andric const TargetLibraryInfo &TLI); 1800b57cec5SDimitry Andric 18106c3fb27SDimitry Andric static bool mustBeFiniteCountedLoop(Loop *L, ScalarEvolution *SE, 18206c3fb27SDimitry Andric BasicBlock *Pred); 18306c3fb27SDimitry Andric 18406c3fb27SDimitry Andric static Instruction *findLocationForEntrySafepoint(Function &F, 18506c3fb27SDimitry Andric DominatorTree &DT); 18606c3fb27SDimitry Andric 18706c3fb27SDimitry Andric static bool isGCSafepointPoll(Function &F); 18806c3fb27SDimitry Andric static bool shouldRewriteFunction(Function &F); 18906c3fb27SDimitry Andric static bool enableEntrySafepoints(Function &F); 19006c3fb27SDimitry Andric static bool enableBackedgeSafepoints(Function &F); 19106c3fb27SDimitry Andric static bool enableCallSafepoints(Function &F); 19206c3fb27SDimitry Andric 1930b57cec5SDimitry Andric static void 194*0fca6ea1SDimitry Andric InsertSafepointPoll(BasicBlock::iterator InsertBefore, 1950b57cec5SDimitry Andric std::vector<CallBase *> &ParsePointsNeeded /*rval*/, 1960b57cec5SDimitry Andric const TargetLibraryInfo &TLI); 1970b57cec5SDimitry Andric 19806c3fb27SDimitry Andric bool PlaceBackedgeSafepointsLegacyPass::runOnLoop(Loop *L) { 19906c3fb27SDimitry Andric // Loop through all loop latches (branches controlling backedges). We need 20006c3fb27SDimitry Andric // to place a safepoint on every backedge (potentially). 20106c3fb27SDimitry Andric // Note: In common usage, there will be only one edge due to LoopSimplify 20206c3fb27SDimitry Andric // having run sometime earlier in the pipeline, but this code must be correct 20306c3fb27SDimitry Andric // w.r.t. loops with multiple backedges. 20406c3fb27SDimitry Andric BasicBlock *Header = L->getHeader(); 20506c3fb27SDimitry Andric SmallVector<BasicBlock *, 16> LoopLatches; 20606c3fb27SDimitry Andric L->getLoopLatches(LoopLatches); 20706c3fb27SDimitry Andric for (BasicBlock *Pred : LoopLatches) { 20806c3fb27SDimitry Andric assert(L->contains(Pred)); 20906c3fb27SDimitry Andric 21006c3fb27SDimitry Andric // Make a policy decision about whether this loop needs a safepoint or 21106c3fb27SDimitry Andric // not. Note that this is about unburdening the optimizer in loops, not 21206c3fb27SDimitry Andric // avoiding the runtime cost of the actual safepoint. 21306c3fb27SDimitry Andric if (!AllBackedges) { 21406c3fb27SDimitry Andric if (mustBeFiniteCountedLoop(L, SE, Pred)) { 21506c3fb27SDimitry Andric LLVM_DEBUG(dbgs() << "skipping safepoint placement in finite loop\n"); 21606c3fb27SDimitry Andric FiniteExecution++; 21706c3fb27SDimitry Andric continue; 21806c3fb27SDimitry Andric } 21906c3fb27SDimitry Andric if (CallSafepointsEnabled && 22006c3fb27SDimitry Andric containsUnconditionalCallSafepoint(L, Header, Pred, *DT, *TLI)) { 22106c3fb27SDimitry Andric // Note: This is only semantically legal since we won't do any further 22206c3fb27SDimitry Andric // IPO or inlining before the actual call insertion.. If we hadn't, we 22306c3fb27SDimitry Andric // might latter loose this call safepoint. 22406c3fb27SDimitry Andric LLVM_DEBUG( 22506c3fb27SDimitry Andric dbgs() 22606c3fb27SDimitry Andric << "skipping safepoint placement due to unconditional call\n"); 22706c3fb27SDimitry Andric CallInLoop++; 22806c3fb27SDimitry Andric continue; 22906c3fb27SDimitry Andric } 23006c3fb27SDimitry Andric } 23106c3fb27SDimitry Andric 23206c3fb27SDimitry Andric // TODO: We can create an inner loop which runs a finite number of 23306c3fb27SDimitry Andric // iterations with an outer loop which contains a safepoint. This would 23406c3fb27SDimitry Andric // not help runtime performance that much, but it might help our ability to 23506c3fb27SDimitry Andric // optimize the inner loop. 23606c3fb27SDimitry Andric 23706c3fb27SDimitry Andric // Safepoint insertion would involve creating a new basic block (as the 23806c3fb27SDimitry Andric // target of the current backedge) which does the safepoint (of all live 23906c3fb27SDimitry Andric // variables) and branches to the true header 24006c3fb27SDimitry Andric Instruction *Term = Pred->getTerminator(); 24106c3fb27SDimitry Andric 24206c3fb27SDimitry Andric LLVM_DEBUG(dbgs() << "[LSP] terminator instruction: " << *Term); 24306c3fb27SDimitry Andric 24406c3fb27SDimitry Andric PollLocations.push_back(Term); 24506c3fb27SDimitry Andric } 24606c3fb27SDimitry Andric 24706c3fb27SDimitry Andric return false; 24806c3fb27SDimitry Andric } 24906c3fb27SDimitry Andric 25006c3fb27SDimitry Andric bool PlaceSafepointsPass::runImpl(Function &F, const TargetLibraryInfo &TLI) { 25106c3fb27SDimitry Andric if (F.isDeclaration() || F.empty()) { 25206c3fb27SDimitry Andric // This is a declaration, nothing to do. Must exit early to avoid crash in 25306c3fb27SDimitry Andric // dom tree calculation 25406c3fb27SDimitry Andric return false; 25506c3fb27SDimitry Andric } 25606c3fb27SDimitry Andric 25706c3fb27SDimitry Andric if (isGCSafepointPoll(F)) { 25806c3fb27SDimitry Andric // Given we're inlining this inside of safepoint poll insertion, this 25906c3fb27SDimitry Andric // doesn't make any sense. Note that we do make any contained calls 26006c3fb27SDimitry Andric // parseable after we inline a poll. 26106c3fb27SDimitry Andric return false; 26206c3fb27SDimitry Andric } 26306c3fb27SDimitry Andric 26406c3fb27SDimitry Andric if (!shouldRewriteFunction(F)) 26506c3fb27SDimitry Andric return false; 26606c3fb27SDimitry Andric 26706c3fb27SDimitry Andric bool Modified = false; 26806c3fb27SDimitry Andric 26906c3fb27SDimitry Andric // In various bits below, we rely on the fact that uses are reachable from 27006c3fb27SDimitry Andric // defs. When there are basic blocks unreachable from the entry, dominance 27106c3fb27SDimitry Andric // and reachablity queries return non-sensical results. Thus, we preprocess 27206c3fb27SDimitry Andric // the function to ensure these properties hold. 27306c3fb27SDimitry Andric Modified |= removeUnreachableBlocks(F); 27406c3fb27SDimitry Andric 27506c3fb27SDimitry Andric // STEP 1 - Insert the safepoint polling locations. We do not need to 27606c3fb27SDimitry Andric // actually insert parse points yet. That will be done for all polls and 27706c3fb27SDimitry Andric // calls in a single pass. 27806c3fb27SDimitry Andric 27906c3fb27SDimitry Andric DominatorTree DT; 28006c3fb27SDimitry Andric DT.recalculate(F); 28106c3fb27SDimitry Andric 28206c3fb27SDimitry Andric SmallVector<Instruction *, 16> PollsNeeded; 28306c3fb27SDimitry Andric std::vector<CallBase *> ParsePointNeeded; 28406c3fb27SDimitry Andric 28506c3fb27SDimitry Andric if (enableBackedgeSafepoints(F)) { 28606c3fb27SDimitry Andric // Construct a pass manager to run the LoopPass backedge logic. We 28706c3fb27SDimitry Andric // need the pass manager to handle scheduling all the loop passes 28806c3fb27SDimitry Andric // appropriately. Doing this by hand is painful and just not worth messing 28906c3fb27SDimitry Andric // with for the moment. 29006c3fb27SDimitry Andric legacy::FunctionPassManager FPM(F.getParent()); 29106c3fb27SDimitry Andric bool CanAssumeCallSafepoints = enableCallSafepoints(F); 292*0fca6ea1SDimitry Andric 293*0fca6ea1SDimitry Andric FPM.add(new TargetLibraryInfoWrapperPass(TLI)); 29406c3fb27SDimitry Andric auto *PBS = new PlaceBackedgeSafepointsLegacyPass(CanAssumeCallSafepoints); 29506c3fb27SDimitry Andric FPM.add(PBS); 29606c3fb27SDimitry Andric FPM.run(F); 29706c3fb27SDimitry Andric 29806c3fb27SDimitry Andric // We preserve dominance information when inserting the poll, otherwise 29906c3fb27SDimitry Andric // we'd have to recalculate this on every insert 30006c3fb27SDimitry Andric DT.recalculate(F); 30106c3fb27SDimitry Andric 30206c3fb27SDimitry Andric auto &PollLocations = PBS->PollLocations; 30306c3fb27SDimitry Andric 30406c3fb27SDimitry Andric auto OrderByBBName = [](Instruction *a, Instruction *b) { 30506c3fb27SDimitry Andric return a->getParent()->getName() < b->getParent()->getName(); 30606c3fb27SDimitry Andric }; 30706c3fb27SDimitry Andric // We need the order of list to be stable so that naming ends up stable 30806c3fb27SDimitry Andric // when we split edges. This makes test cases much easier to write. 30906c3fb27SDimitry Andric llvm::sort(PollLocations, OrderByBBName); 31006c3fb27SDimitry Andric 31106c3fb27SDimitry Andric // We can sometimes end up with duplicate poll locations. This happens if 31206c3fb27SDimitry Andric // a single loop is visited more than once. The fact this happens seems 31306c3fb27SDimitry Andric // wrong, but it does happen for the split-backedge.ll test case. 314*0fca6ea1SDimitry Andric PollLocations.erase(llvm::unique(PollLocations), PollLocations.end()); 31506c3fb27SDimitry Andric 31606c3fb27SDimitry Andric // Insert a poll at each point the analysis pass identified 31706c3fb27SDimitry Andric // The poll location must be the terminator of a loop latch block. 31806c3fb27SDimitry Andric for (Instruction *Term : PollLocations) { 31906c3fb27SDimitry Andric // We are inserting a poll, the function is modified 32006c3fb27SDimitry Andric Modified = true; 32106c3fb27SDimitry Andric 32206c3fb27SDimitry Andric if (SplitBackedge) { 32306c3fb27SDimitry Andric // Split the backedge of the loop and insert the poll within that new 32406c3fb27SDimitry Andric // basic block. This creates a loop with two latches per original 32506c3fb27SDimitry Andric // latch (which is non-ideal), but this appears to be easier to 32606c3fb27SDimitry Andric // optimize in practice than inserting the poll immediately before the 32706c3fb27SDimitry Andric // latch test. 32806c3fb27SDimitry Andric 32906c3fb27SDimitry Andric // Since this is a latch, at least one of the successors must dominate 33006c3fb27SDimitry Andric // it. Its possible that we have a) duplicate edges to the same header 33106c3fb27SDimitry Andric // and b) edges to distinct loop headers. We need to insert pools on 33206c3fb27SDimitry Andric // each. 33306c3fb27SDimitry Andric SetVector<BasicBlock *> Headers; 33406c3fb27SDimitry Andric for (unsigned i = 0; i < Term->getNumSuccessors(); i++) { 33506c3fb27SDimitry Andric BasicBlock *Succ = Term->getSuccessor(i); 33606c3fb27SDimitry Andric if (DT.dominates(Succ, Term->getParent())) { 33706c3fb27SDimitry Andric Headers.insert(Succ); 33806c3fb27SDimitry Andric } 33906c3fb27SDimitry Andric } 34006c3fb27SDimitry Andric assert(!Headers.empty() && "poll location is not a loop latch?"); 34106c3fb27SDimitry Andric 34206c3fb27SDimitry Andric // The split loop structure here is so that we only need to recalculate 34306c3fb27SDimitry Andric // the dominator tree once. Alternatively, we could just keep it up to 34406c3fb27SDimitry Andric // date and use a more natural merged loop. 34506c3fb27SDimitry Andric SetVector<BasicBlock *> SplitBackedges; 34606c3fb27SDimitry Andric for (BasicBlock *Header : Headers) { 34706c3fb27SDimitry Andric BasicBlock *NewBB = SplitEdge(Term->getParent(), Header, &DT); 34806c3fb27SDimitry Andric PollsNeeded.push_back(NewBB->getTerminator()); 34906c3fb27SDimitry Andric NumBackedgeSafepoints++; 35006c3fb27SDimitry Andric } 35106c3fb27SDimitry Andric } else { 35206c3fb27SDimitry Andric // Split the latch block itself, right before the terminator. 35306c3fb27SDimitry Andric PollsNeeded.push_back(Term); 35406c3fb27SDimitry Andric NumBackedgeSafepoints++; 35506c3fb27SDimitry Andric } 35606c3fb27SDimitry Andric } 35706c3fb27SDimitry Andric } 35806c3fb27SDimitry Andric 35906c3fb27SDimitry Andric if (enableEntrySafepoints(F)) { 36006c3fb27SDimitry Andric if (Instruction *Location = findLocationForEntrySafepoint(F, DT)) { 36106c3fb27SDimitry Andric PollsNeeded.push_back(Location); 36206c3fb27SDimitry Andric Modified = true; 36306c3fb27SDimitry Andric NumEntrySafepoints++; 36406c3fb27SDimitry Andric } 36506c3fb27SDimitry Andric // TODO: else we should assert that there was, in fact, a policy choice to 36606c3fb27SDimitry Andric // not insert a entry safepoint poll. 36706c3fb27SDimitry Andric } 36806c3fb27SDimitry Andric 36906c3fb27SDimitry Andric // Now that we've identified all the needed safepoint poll locations, insert 37006c3fb27SDimitry Andric // safepoint polls themselves. 37106c3fb27SDimitry Andric for (Instruction *PollLocation : PollsNeeded) { 37206c3fb27SDimitry Andric std::vector<CallBase *> RuntimeCalls; 373*0fca6ea1SDimitry Andric InsertSafepointPoll(PollLocation->getIterator(), RuntimeCalls, TLI); 37406c3fb27SDimitry Andric llvm::append_range(ParsePointNeeded, RuntimeCalls); 37506c3fb27SDimitry Andric } 37606c3fb27SDimitry Andric 37706c3fb27SDimitry Andric return Modified; 37806c3fb27SDimitry Andric } 37906c3fb27SDimitry Andric 38006c3fb27SDimitry Andric PreservedAnalyses PlaceSafepointsPass::run(Function &F, 38106c3fb27SDimitry Andric FunctionAnalysisManager &AM) { 38206c3fb27SDimitry Andric auto &TLI = AM.getResult<TargetLibraryAnalysis>(F); 38306c3fb27SDimitry Andric 38406c3fb27SDimitry Andric if (!runImpl(F, TLI)) 38506c3fb27SDimitry Andric return PreservedAnalyses::all(); 38606c3fb27SDimitry Andric 38706c3fb27SDimitry Andric // TODO: can we preserve more? 38806c3fb27SDimitry Andric return PreservedAnalyses::none(); 38906c3fb27SDimitry Andric } 39006c3fb27SDimitry Andric 3910b57cec5SDimitry Andric static bool needsStatepoint(CallBase *Call, const TargetLibraryInfo &TLI) { 3920b57cec5SDimitry Andric if (callsGCLeafFunction(Call, TLI)) 3930b57cec5SDimitry Andric return false; 3940b57cec5SDimitry Andric if (auto *CI = dyn_cast<CallInst>(Call)) { 3950b57cec5SDimitry Andric if (CI->isInlineAsm()) 3960b57cec5SDimitry Andric return false; 3970b57cec5SDimitry Andric } 3980b57cec5SDimitry Andric 3995ffd83dbSDimitry Andric return !(isa<GCStatepointInst>(Call) || isa<GCRelocateInst>(Call) || 4005ffd83dbSDimitry Andric isa<GCResultInst>(Call)); 4010b57cec5SDimitry Andric } 4020b57cec5SDimitry Andric 4030b57cec5SDimitry Andric /// Returns true if this loop is known to contain a call safepoint which 4040b57cec5SDimitry Andric /// must unconditionally execute on any iteration of the loop which returns 4050b57cec5SDimitry Andric /// to the loop header via an edge from Pred. Returns a conservative correct 4060b57cec5SDimitry Andric /// answer; i.e. false is always valid. 4070b57cec5SDimitry Andric static bool containsUnconditionalCallSafepoint(Loop *L, BasicBlock *Header, 4080b57cec5SDimitry Andric BasicBlock *Pred, 4090b57cec5SDimitry Andric DominatorTree &DT, 4100b57cec5SDimitry Andric const TargetLibraryInfo &TLI) { 4110b57cec5SDimitry Andric // In general, we're looking for any cut of the graph which ensures 4120b57cec5SDimitry Andric // there's a call safepoint along every edge between Header and Pred. 4130b57cec5SDimitry Andric // For the moment, we look only for the 'cuts' that consist of a single call 4140b57cec5SDimitry Andric // instruction in a block which is dominated by the Header and dominates the 4150b57cec5SDimitry Andric // loop latch (Pred) block. Somewhat surprisingly, walking the entire chain 4160b57cec5SDimitry Andric // of such dominating blocks gets substantially more occurrences than just 4170b57cec5SDimitry Andric // checking the Pred and Header blocks themselves. This may be due to the 4180b57cec5SDimitry Andric // density of loop exit conditions caused by range and null checks. 4190b57cec5SDimitry Andric // TODO: structure this as an analysis pass, cache the result for subloops, 4200b57cec5SDimitry Andric // avoid dom tree recalculations 4210b57cec5SDimitry Andric assert(DT.dominates(Header, Pred) && "loop latch not dominated by header?"); 4220b57cec5SDimitry Andric 4230b57cec5SDimitry Andric BasicBlock *Current = Pred; 4240b57cec5SDimitry Andric while (true) { 4250b57cec5SDimitry Andric for (Instruction &I : *Current) { 4260b57cec5SDimitry Andric if (auto *Call = dyn_cast<CallBase>(&I)) 4270b57cec5SDimitry Andric // Note: Technically, needing a safepoint isn't quite the right 4280b57cec5SDimitry Andric // condition here. We should instead be checking if the target method 4290b57cec5SDimitry Andric // has an 4300b57cec5SDimitry Andric // unconditional poll. In practice, this is only a theoretical concern 4310b57cec5SDimitry Andric // since we don't have any methods with conditional-only safepoint 4320b57cec5SDimitry Andric // polls. 4330b57cec5SDimitry Andric if (needsStatepoint(Call, TLI)) 4340b57cec5SDimitry Andric return true; 4350b57cec5SDimitry Andric } 4360b57cec5SDimitry Andric 4370b57cec5SDimitry Andric if (Current == Header) 4380b57cec5SDimitry Andric break; 4390b57cec5SDimitry Andric Current = DT.getNode(Current)->getIDom()->getBlock(); 4400b57cec5SDimitry Andric } 4410b57cec5SDimitry Andric 4420b57cec5SDimitry Andric return false; 4430b57cec5SDimitry Andric } 4440b57cec5SDimitry Andric 4450b57cec5SDimitry Andric /// Returns true if this loop is known to terminate in a finite number of 4460b57cec5SDimitry Andric /// iterations. Note that this function may return false for a loop which 4470b57cec5SDimitry Andric /// does actual terminate in a finite constant number of iterations due to 4480b57cec5SDimitry Andric /// conservatism in the analysis. 4490b57cec5SDimitry Andric static bool mustBeFiniteCountedLoop(Loop *L, ScalarEvolution *SE, 4500b57cec5SDimitry Andric BasicBlock *Pred) { 4510b57cec5SDimitry Andric // A conservative bound on the loop as a whole. 4528bcb0991SDimitry Andric const SCEV *MaxTrips = SE->getConstantMaxBackedgeTakenCount(L); 453e8d8bef9SDimitry Andric if (!isa<SCEVCouldNotCompute>(MaxTrips) && 4540b57cec5SDimitry Andric SE->getUnsignedRange(MaxTrips).getUnsignedMax().isIntN( 4550b57cec5SDimitry Andric CountedLoopTripWidth)) 4560b57cec5SDimitry Andric return true; 4570b57cec5SDimitry Andric 4580b57cec5SDimitry Andric // If this is a conditional branch to the header with the alternate path 4590b57cec5SDimitry Andric // being outside the loop, we can ask questions about the execution frequency 4600b57cec5SDimitry Andric // of the exit block. 4610b57cec5SDimitry Andric if (L->isLoopExiting(Pred)) { 4620b57cec5SDimitry Andric // This returns an exact expression only. TODO: We really only need an 4630b57cec5SDimitry Andric // upper bound here, but SE doesn't expose that. 4640b57cec5SDimitry Andric const SCEV *MaxExec = SE->getExitCount(L, Pred); 465e8d8bef9SDimitry Andric if (!isa<SCEVCouldNotCompute>(MaxExec) && 4660b57cec5SDimitry Andric SE->getUnsignedRange(MaxExec).getUnsignedMax().isIntN( 4670b57cec5SDimitry Andric CountedLoopTripWidth)) 4680b57cec5SDimitry Andric return true; 4690b57cec5SDimitry Andric } 4700b57cec5SDimitry Andric 4710b57cec5SDimitry Andric return /* not finite */ false; 4720b57cec5SDimitry Andric } 4730b57cec5SDimitry Andric 4740b57cec5SDimitry Andric static void scanOneBB(Instruction *Start, Instruction *End, 4750b57cec5SDimitry Andric std::vector<CallInst *> &Calls, 4760b57cec5SDimitry Andric DenseSet<BasicBlock *> &Seen, 4770b57cec5SDimitry Andric std::vector<BasicBlock *> &Worklist) { 4780b57cec5SDimitry Andric for (BasicBlock::iterator BBI(Start), BBE0 = Start->getParent()->end(), 4790b57cec5SDimitry Andric BBE1 = BasicBlock::iterator(End); 4800b57cec5SDimitry Andric BBI != BBE0 && BBI != BBE1; BBI++) { 4810b57cec5SDimitry Andric if (CallInst *CI = dyn_cast<CallInst>(&*BBI)) 4820b57cec5SDimitry Andric Calls.push_back(CI); 4830b57cec5SDimitry Andric 4840b57cec5SDimitry Andric // FIXME: This code does not handle invokes 4850b57cec5SDimitry Andric assert(!isa<InvokeInst>(&*BBI) && 4860b57cec5SDimitry Andric "support for invokes in poll code needed"); 4870b57cec5SDimitry Andric 4880b57cec5SDimitry Andric // Only add the successor blocks if we reach the terminator instruction 4890b57cec5SDimitry Andric // without encountering end first 4900b57cec5SDimitry Andric if (BBI->isTerminator()) { 4910b57cec5SDimitry Andric BasicBlock *BB = BBI->getParent(); 4920b57cec5SDimitry Andric for (BasicBlock *Succ : successors(BB)) { 4930b57cec5SDimitry Andric if (Seen.insert(Succ).second) { 4940b57cec5SDimitry Andric Worklist.push_back(Succ); 4950b57cec5SDimitry Andric } 4960b57cec5SDimitry Andric } 4970b57cec5SDimitry Andric } 4980b57cec5SDimitry Andric } 4990b57cec5SDimitry Andric } 5000b57cec5SDimitry Andric 5010b57cec5SDimitry Andric static void scanInlinedCode(Instruction *Start, Instruction *End, 5020b57cec5SDimitry Andric std::vector<CallInst *> &Calls, 5030b57cec5SDimitry Andric DenseSet<BasicBlock *> &Seen) { 5040b57cec5SDimitry Andric Calls.clear(); 5050b57cec5SDimitry Andric std::vector<BasicBlock *> Worklist; 5060b57cec5SDimitry Andric Seen.insert(Start->getParent()); 5070b57cec5SDimitry Andric scanOneBB(Start, End, Calls, Seen, Worklist); 5080b57cec5SDimitry Andric while (!Worklist.empty()) { 5090b57cec5SDimitry Andric BasicBlock *BB = Worklist.back(); 5100b57cec5SDimitry Andric Worklist.pop_back(); 5110b57cec5SDimitry Andric scanOneBB(&*BB->begin(), End, Calls, Seen, Worklist); 5120b57cec5SDimitry Andric } 5130b57cec5SDimitry Andric } 5140b57cec5SDimitry Andric 5150b57cec5SDimitry Andric /// Returns true if an entry safepoint is not required before this callsite in 5160b57cec5SDimitry Andric /// the caller function. 5170b57cec5SDimitry Andric static bool doesNotRequireEntrySafepointBefore(CallBase *Call) { 5180b57cec5SDimitry Andric if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(Call)) { 5190b57cec5SDimitry Andric switch (II->getIntrinsicID()) { 5200b57cec5SDimitry Andric case Intrinsic::experimental_gc_statepoint: 5210b57cec5SDimitry Andric case Intrinsic::experimental_patchpoint_void: 522*0fca6ea1SDimitry Andric case Intrinsic::experimental_patchpoint: 5230b57cec5SDimitry Andric // The can wrap an actual call which may grow the stack by an unbounded 5240b57cec5SDimitry Andric // amount or run forever. 5250b57cec5SDimitry Andric return false; 5260b57cec5SDimitry Andric default: 5270b57cec5SDimitry Andric // Most LLVM intrinsics are things which do not expand to actual calls, or 5280b57cec5SDimitry Andric // at least if they do, are leaf functions that cause only finite stack 5290b57cec5SDimitry Andric // growth. In particular, the optimizer likes to form things like memsets 5300b57cec5SDimitry Andric // out of stores in the original IR. Another important example is 5310b57cec5SDimitry Andric // llvm.localescape which must occur in the entry block. Inserting a 5320b57cec5SDimitry Andric // safepoint before it is not legal since it could push the localescape 5330b57cec5SDimitry Andric // out of the entry block. 5340b57cec5SDimitry Andric return true; 5350b57cec5SDimitry Andric } 5360b57cec5SDimitry Andric } 5370b57cec5SDimitry Andric return false; 5380b57cec5SDimitry Andric } 5390b57cec5SDimitry Andric 5400b57cec5SDimitry Andric static Instruction *findLocationForEntrySafepoint(Function &F, 5410b57cec5SDimitry Andric DominatorTree &DT) { 5420b57cec5SDimitry Andric 5430b57cec5SDimitry Andric // Conceptually, this poll needs to be on method entry, but in 5440b57cec5SDimitry Andric // practice, we place it as late in the entry block as possible. We 5450b57cec5SDimitry Andric // can place it as late as we want as long as it dominates all calls 5460b57cec5SDimitry Andric // that can grow the stack. This, combined with backedge polls, 5470b57cec5SDimitry Andric // give us all the progress guarantees we need. 5480b57cec5SDimitry Andric 5490b57cec5SDimitry Andric // hasNextInstruction and nextInstruction are used to iterate 5500b57cec5SDimitry Andric // through a "straight line" execution sequence. 5510b57cec5SDimitry Andric 5520b57cec5SDimitry Andric auto HasNextInstruction = [](Instruction *I) { 5530b57cec5SDimitry Andric if (!I->isTerminator()) 5540b57cec5SDimitry Andric return true; 5550b57cec5SDimitry Andric 5560b57cec5SDimitry Andric BasicBlock *nextBB = I->getParent()->getUniqueSuccessor(); 5570b57cec5SDimitry Andric return nextBB && (nextBB->getUniquePredecessor() != nullptr); 5580b57cec5SDimitry Andric }; 5590b57cec5SDimitry Andric 5600b57cec5SDimitry Andric auto NextInstruction = [&](Instruction *I) { 5610b57cec5SDimitry Andric assert(HasNextInstruction(I) && 5620b57cec5SDimitry Andric "first check if there is a next instruction!"); 5630b57cec5SDimitry Andric 5640b57cec5SDimitry Andric if (I->isTerminator()) 5650b57cec5SDimitry Andric return &I->getParent()->getUniqueSuccessor()->front(); 5660b57cec5SDimitry Andric return &*++I->getIterator(); 5670b57cec5SDimitry Andric }; 5680b57cec5SDimitry Andric 5690b57cec5SDimitry Andric Instruction *Cursor = nullptr; 5700b57cec5SDimitry Andric for (Cursor = &F.getEntryBlock().front(); HasNextInstruction(Cursor); 5710b57cec5SDimitry Andric Cursor = NextInstruction(Cursor)) { 5720b57cec5SDimitry Andric 5730b57cec5SDimitry Andric // We need to ensure a safepoint poll occurs before any 'real' call. The 5740b57cec5SDimitry Andric // easiest way to ensure finite execution between safepoints in the face of 5750b57cec5SDimitry Andric // recursive and mutually recursive functions is to enforce that each take 5760b57cec5SDimitry Andric // a safepoint. Additionally, we need to ensure a poll before any call 5770b57cec5SDimitry Andric // which can grow the stack by an unbounded amount. This isn't required 5780b57cec5SDimitry Andric // for GC semantics per se, but is a common requirement for languages 5790b57cec5SDimitry Andric // which detect stack overflow via guard pages and then throw exceptions. 5800b57cec5SDimitry Andric if (auto *Call = dyn_cast<CallBase>(Cursor)) { 5810b57cec5SDimitry Andric if (doesNotRequireEntrySafepointBefore(Call)) 5820b57cec5SDimitry Andric continue; 5830b57cec5SDimitry Andric break; 5840b57cec5SDimitry Andric } 5850b57cec5SDimitry Andric } 5860b57cec5SDimitry Andric 5870b57cec5SDimitry Andric assert((HasNextInstruction(Cursor) || Cursor->isTerminator()) && 5880b57cec5SDimitry Andric "either we stopped because of a call, or because of terminator"); 5890b57cec5SDimitry Andric 5900b57cec5SDimitry Andric return Cursor; 5910b57cec5SDimitry Andric } 5920b57cec5SDimitry Andric 593e8d8bef9SDimitry Andric const char GCSafepointPollName[] = "gc.safepoint_poll"; 5940b57cec5SDimitry Andric 5950b57cec5SDimitry Andric static bool isGCSafepointPoll(Function &F) { 596*0fca6ea1SDimitry Andric return F.getName() == GCSafepointPollName; 5970b57cec5SDimitry Andric } 5980b57cec5SDimitry Andric 5990b57cec5SDimitry Andric /// Returns true if this function should be rewritten to include safepoint 6000b57cec5SDimitry Andric /// polls and parseable call sites. The main point of this function is to be 6010b57cec5SDimitry Andric /// an extension point for custom logic. 6020b57cec5SDimitry Andric static bool shouldRewriteFunction(Function &F) { 6030b57cec5SDimitry Andric // TODO: This should check the GCStrategy 6040b57cec5SDimitry Andric if (F.hasGC()) { 6050b57cec5SDimitry Andric const auto &FunctionGCName = F.getGC(); 6060b57cec5SDimitry Andric const StringRef StatepointExampleName("statepoint-example"); 6070b57cec5SDimitry Andric const StringRef CoreCLRName("coreclr"); 6080b57cec5SDimitry Andric return (StatepointExampleName == FunctionGCName) || 6090b57cec5SDimitry Andric (CoreCLRName == FunctionGCName); 6100b57cec5SDimitry Andric } else 6110b57cec5SDimitry Andric return false; 6120b57cec5SDimitry Andric } 6130b57cec5SDimitry Andric 6140b57cec5SDimitry Andric // TODO: These should become properties of the GCStrategy, possibly with 6150b57cec5SDimitry Andric // command line overrides. 6160b57cec5SDimitry Andric static bool enableEntrySafepoints(Function &F) { return !NoEntry; } 6170b57cec5SDimitry Andric static bool enableBackedgeSafepoints(Function &F) { return !NoBackedge; } 6180b57cec5SDimitry Andric static bool enableCallSafepoints(Function &F) { return !NoCall; } 6190b57cec5SDimitry Andric 62006c3fb27SDimitry Andric // Insert a safepoint poll immediately before the given instruction. Does 62106c3fb27SDimitry Andric // not handle the parsability of state at the runtime call, that's the 62206c3fb27SDimitry Andric // callers job. 6230b57cec5SDimitry Andric static void 624*0fca6ea1SDimitry Andric InsertSafepointPoll(BasicBlock::iterator InsertBefore, 6250b57cec5SDimitry Andric std::vector<CallBase *> &ParsePointsNeeded /*rval*/, 6260b57cec5SDimitry Andric const TargetLibraryInfo &TLI) { 6270b57cec5SDimitry Andric BasicBlock *OrigBB = InsertBefore->getParent(); 6280b57cec5SDimitry Andric Module *M = InsertBefore->getModule(); 6290b57cec5SDimitry Andric assert(M && "must be part of a module"); 6300b57cec5SDimitry Andric 6310b57cec5SDimitry Andric // Inline the safepoint poll implementation - this will get all the branch, 6320b57cec5SDimitry Andric // control flow, etc.. Most importantly, it will introduce the actual slow 6330b57cec5SDimitry Andric // path call - where we need to insert a safepoint (parsepoint). 6340b57cec5SDimitry Andric 6350b57cec5SDimitry Andric auto *F = M->getFunction(GCSafepointPollName); 6360b57cec5SDimitry Andric assert(F && "gc.safepoint_poll function is missing"); 6370b57cec5SDimitry Andric assert(F->getValueType() == 6380b57cec5SDimitry Andric FunctionType::get(Type::getVoidTy(M->getContext()), false) && 6390b57cec5SDimitry Andric "gc.safepoint_poll declared with wrong type"); 6400b57cec5SDimitry Andric assert(!F->empty() && "gc.safepoint_poll must be a non-empty function"); 6410b57cec5SDimitry Andric CallInst *PollCall = CallInst::Create(F, "", InsertBefore); 6420b57cec5SDimitry Andric 6430b57cec5SDimitry Andric // Record some information about the call site we're replacing 6440b57cec5SDimitry Andric BasicBlock::iterator Before(PollCall), After(PollCall); 6450b57cec5SDimitry Andric bool IsBegin = false; 6460b57cec5SDimitry Andric if (Before == OrigBB->begin()) 6470b57cec5SDimitry Andric IsBegin = true; 6480b57cec5SDimitry Andric else 6490b57cec5SDimitry Andric Before--; 6500b57cec5SDimitry Andric 6510b57cec5SDimitry Andric After++; 6520b57cec5SDimitry Andric assert(After != OrigBB->end() && "must have successor"); 6530b57cec5SDimitry Andric 6540b57cec5SDimitry Andric // Do the actual inlining 6550b57cec5SDimitry Andric InlineFunctionInfo IFI; 6565ffd83dbSDimitry Andric bool InlineStatus = InlineFunction(*PollCall, IFI).isSuccess(); 6570b57cec5SDimitry Andric assert(InlineStatus && "inline must succeed"); 6580b57cec5SDimitry Andric (void)InlineStatus; // suppress warning in release-asserts 6590b57cec5SDimitry Andric 6600b57cec5SDimitry Andric // Check post-conditions 6610b57cec5SDimitry Andric assert(IFI.StaticAllocas.empty() && "can't have allocs"); 6620b57cec5SDimitry Andric 6630b57cec5SDimitry Andric std::vector<CallInst *> Calls; // new calls 6640b57cec5SDimitry Andric DenseSet<BasicBlock *> BBs; // new BBs + insertee 6650b57cec5SDimitry Andric 6660b57cec5SDimitry Andric // Include only the newly inserted instructions, Note: begin may not be valid 6670b57cec5SDimitry Andric // if we inserted to the beginning of the basic block 6680b57cec5SDimitry Andric BasicBlock::iterator Start = IsBegin ? OrigBB->begin() : std::next(Before); 6690b57cec5SDimitry Andric 6700b57cec5SDimitry Andric // If your poll function includes an unreachable at the end, that's not 6710b57cec5SDimitry Andric // valid. Bugpoint likes to create this, so check for it. 6720b57cec5SDimitry Andric assert(isPotentiallyReachable(&*Start, &*After) && 6730b57cec5SDimitry Andric "malformed poll function"); 6740b57cec5SDimitry Andric 6750b57cec5SDimitry Andric scanInlinedCode(&*Start, &*After, Calls, BBs); 6760b57cec5SDimitry Andric assert(!Calls.empty() && "slow path not found for safepoint poll"); 6770b57cec5SDimitry Andric 6780b57cec5SDimitry Andric // Record the fact we need a parsable state at the runtime call contained in 6790b57cec5SDimitry Andric // the poll function. This is required so that the runtime knows how to 6800b57cec5SDimitry Andric // parse the last frame when we actually take the safepoint (i.e. execute 6810b57cec5SDimitry Andric // the slow path) 6820b57cec5SDimitry Andric assert(ParsePointsNeeded.empty()); 6830b57cec5SDimitry Andric for (auto *CI : Calls) { 6840b57cec5SDimitry Andric // No safepoint needed or wanted 6850b57cec5SDimitry Andric if (!needsStatepoint(CI, TLI)) 6860b57cec5SDimitry Andric continue; 6870b57cec5SDimitry Andric 6880b57cec5SDimitry Andric // These are likely runtime calls. Should we assert that via calling 6890b57cec5SDimitry Andric // convention or something? 6900b57cec5SDimitry Andric ParsePointsNeeded.push_back(CI); 6910b57cec5SDimitry Andric } 6920b57cec5SDimitry Andric assert(ParsePointsNeeded.size() <= Calls.size()); 6930b57cec5SDimitry Andric } 694