70 #define DEBUG_TYPE "safepoint-placement" 72 STATISTIC(NumEntrySafepoints,
"Number of entry safepoints inserted");
73 STATISTIC(NumBackedgeSafepoints,
"Number of backedge safepoints inserted");
76 "Number of loops without safepoints due to calls in loop");
78 "Number of loops without safepoints finite execution");
103 struct PlaceBackedgeSafepointsImpl :
public FunctionPass {
108 std::vector<Instruction *> PollLocations;
112 bool CallSafepointsEnabled;
119 PlaceBackedgeSafepointsImpl(
bool CallSafepoints =
false)
120 :
FunctionPass(ID), CallSafepointsEnabled(CallSafepoints) {
124 bool runOnLoop(
Loop *);
125 void runOnLoopAndSubLoops(
Loop *L) {
128 runOnLoopAndSubLoops(
I);
133 SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE();
134 DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
135 LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
136 TLI = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
137 for (
Loop *
I : *LI) {
138 runOnLoopAndSubLoops(
I);
182 std::vector<CallSite> &ParsePointsNeeded ,
215 assert(DT.
dominates(Header, Pred) &&
"loop latch not dominated by header?");
231 if (Current == Header)
269 std::vector<CallInst *> &Calls,
271 std::vector<BasicBlock *> &Worklist) {
274 BBI != BBE0 && BBI != BBE1; BBI++) {
275 if (
CallInst *CI = dyn_cast<CallInst>(&*BBI))
279 assert(!isa<InvokeInst>(&*BBI) &&
280 "support for invokes in poll code needed");
284 if (BBI->isTerminator()) {
287 if (Seen.
insert(Succ).second) {
288 Worklist.push_back(Succ);
296 std::vector<CallInst *> &Calls,
299 std::vector<BasicBlock *> Worklist;
301 scanOneBB(Start, End, Calls, Seen, Worklist);
302 while (!Worklist.empty()) {
309 bool PlaceBackedgeSafepointsImpl::runOnLoop(
Loop *L) {
326 LLVM_DEBUG(
dbgs() <<
"skipping safepoint placement in finite loop\n");
330 if (CallSafepointsEnabled &&
337 <<
"skipping safepoint placement due to unconditional call\n");
355 PollLocations.push_back(Term);
366 switch (II->getIntrinsicID()) {
400 if (!
I->isTerminator())
403 BasicBlock *nextBB =
I->getParent()->getUniqueSuccessor();
408 assert(HasNextInstruction(
I) &&
409 "first check if there is a next instruction!");
411 if (
I->isTerminator())
412 return &
I->getParent()->getUniqueSuccessor()->front();
413 return &*++
I->getIterator();
418 Cursor = NextInstruction(Cursor)) {
435 "either we stopped because of a call, or because of terminator");
452 const auto &FunctionGCName = F.
getGC();
453 const StringRef StatepointExampleName(
"statepoint-example");
455 return (StatepointExampleName == FunctionGCName) ||
456 (CoreCLRName == FunctionGCName);
485 getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
503 std::vector<CallSite> ParsePointNeeded;
512 auto *PBS =
new PlaceBackedgeSafepointsImpl(CanAssumeCallSafepoints);
520 auto &PollLocations = PBS->PollLocations;
532 PollLocations.erase(std::unique(PollLocations.begin(),
533 PollLocations.end()),
534 PollLocations.end());
554 for (
unsigned i = 0; i < Term->getNumSuccessors(); i++) {
560 assert(!Headers.
empty() &&
"poll location is not a loop latch?");
569 NumBackedgeSafepoints++;
574 NumBackedgeSafepoints++;
583 NumEntrySafepoints++;
592 std::vector<CallSite> RuntimeCalls;
594 ParsePointNeeded.insert(ParsePointNeeded.end(), RuntimeCalls.begin(),
605 return new PlaceSafepoints();
609 "place-backedge-safepoints-impl",
610 "Place Backedge Safepoints",
false,
false)
615 "place-backedge-safepoints-impl",
616 "Place Backedge Safepoints",
false,
false)
628 Module *M = InsertBefore->getModule();
629 assert(M &&
"must be part of a module");
636 assert(F &&
"gc.safepoint_poll function is missing");
639 "gc.safepoint_poll declared with wrong type");
640 assert(!F->
empty() &&
"gc.safepoint_poll must be a non-empty function");
645 bool IsBegin =
false;
646 if (Before == OrigBB->
begin())
652 assert(After != OrigBB->
end() &&
"must have successor");
657 assert(InlineStatus &&
"inline must succeed");
661 assert(IFI.StaticAllocas.empty() &&
"can't have allocs");
663 std::vector<CallInst *> Calls;
673 "malformed poll function");
676 assert(!Calls.empty() &&
"slow path not found for safepoint poll");
682 assert(ParsePointsNeeded.empty());
683 for (
auto *CI : Calls) {
690 ParsePointsNeeded.push_back(
CallSite(CI));
692 assert(ParsePointsNeeded.size() <= Calls.size());
static bool mustBeFiniteCountedLoop(Loop *L, ScalarEvolution *SE, BasicBlock *Pred)
Returns true if this loop is known to terminate in a finite number of iterations. ...
STATISTIC(NumEntrySafepoints, "Number of entry safepoints inserted")
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
static bool shouldRewriteFunction(Function &F)
Returns true if this function should be rewritten to include safepoint polls and parseable call sites...
This class represents lattice values for constants.
A Module instance is used to store all the information related to an LLVM module. ...
static CallInst * Create(FunctionType *Ty, Value *F, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
Implements a dense probed hash-table based set.
void push_back(const T &Elt)
void recalculate(ParentType &Func)
recalculate - compute a dominator tree for the given function
The main scalar evolution driver.
bool removeUnreachableBlocks(Function &F, LazyValueInfo *LVI=nullptr, DomTreeUpdater *DTU=nullptr, MemorySSAUpdater *MSSAU=nullptr)
Remove all blocks that can not be reached from the function's entry.
This class represents a function call, abstracting a target machine's calling convention.
bool isTerminator() const
void initializePlaceSafepointsPass(PassRegistry &)
InlineResult InlineFunction(CallInst *C, InlineFunctionInfo &IFI, AAResults *CalleeAAR=nullptr, bool InsertLifetime=true)
This function inlines the called function into the basic block of the caller.
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
This class captures the data input to the InlineFunction call, and records the auxiliary results prod...
bool isGCRelocate(ImmutableCallSite CS)
iterator begin()
Instruction iterator methods.
BasicBlock * SplitEdge(BasicBlock *From, BasicBlock *To, DominatorTree *DT=nullptr, LoopInfo *LI=nullptr, MemorySSAUpdater *MSSAU=nullptr)
Split the edge connecting specified block.
static void scanOneBB(Instruction *Start, Instruction *End, std::vector< CallInst *> &Calls, DenseSet< BasicBlock *> &Seen, std::vector< BasicBlock *> &Worklist)
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
LLVMContext & getContext() const
Get the global data context.
void getLoopLatches(SmallVectorImpl< BlockT *> &LoopLatches) const
Return all loop latch blocks of this loop.
InstrTy * getInstruction() const
static cl::opt< bool > NoBackedge("spp-no-backedge", cl::Hidden, cl::init(false))
BlockT * getHeader() const
static bool doesNotRequireEntrySafepointBefore(const CallSite &CS)
Returns true if an entry safepoint is not required before this callsite in the caller function...
static bool enableCallSafepoints(Function &F)
INITIALIZE_PASS_BEGIN(PlaceBackedgeSafepointsImpl, "place-backedge-safepoints-impl", "Place Backedge Safepoints", false, false) INITIALIZE_PASS_END(PlaceBackedgeSafepointsImpl
bool insert(const value_type &X)
Insert a new element into the SetVector.
const BasicBlock * getUniquePredecessor() const
Return the predecessor of this block if it has a unique predecessor block.
const std::string & getGC() const
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree...
bool isCall() const
Return true if a CallInst is enclosed.
void initializePlaceBackedgeSafepointsImplPass(PassRegistry &)
static void scanInlinedCode(Instruction *Start, Instruction *End, std::vector< CallInst *> &Calls, DenseSet< BasicBlock *> &Seen)
static const char *const GCSafepointPollName
static cl::opt< bool > SplitBackedge("spp-split-backedge", cl::Hidden, cl::init(false))
const BasicBlock & getEntryBlock() const
static Instruction * findLocationForEntrySafepoint(Function &F, DominatorTree &DT)
const SCEV * getCouldNotCompute()
static bool runOnFunction(Function &F, bool PostInlining)
initializer< Ty > init(const Ty &Val)
static bool enableEntrySafepoints(Function &F)
FunctionPass * createPlaceSafepointsPass()
LLVM Basic Block Representation.
bool isLoopExiting(const BlockT *BB) const
True if terminator in the block can branch to another block that is outside of the current loop...
DomTreeNodeBase * getIDom() const
const Instruction & front() const
std::pair< iterator, bool > insert(const ValueT &V)
static cl::opt< bool > NoCall("spp-no-call", cl::Hidden, cl::init(false))
APInt getUnsignedMax() const
Return the largest unsigned value contained in the ConstantRange.
Represent the analysis usage information of a pass.
static Type * getVoidTy(LLVMContext &C)
static cl::opt< bool > AllBackedges("spp-all-backedges", cl::Hidden, cl::init(false))
const Instruction & back() const
const SCEV * getMaxBackedgeTakenCount(const Loop *L)
When successful, this returns a SCEVConstant that is greater than or equal to (i.e.
bool isGCResult(ImmutableCallSite CS)
FunctionPass class - This class is used to implement most global optimizations.
static FunctionType * get(Type *Result, ArrayRef< Type *> Params, bool isVarArg)
This static method is the primary way of constructing a FunctionType.
bool isPotentiallyReachable(const Instruction *From, const Instruction *To, const DominatorTree *DT=nullptr, const LoopInfo *LI=nullptr)
Determine whether instruction 'To' is reachable from 'From', returning true if uncertain.
bool callsGCLeafFunction(ImmutableCallSite CS, const TargetLibraryInfo &TLI)
Return true if the CallSite CS calls a gc leaf function.
static void InsertSafepointPoll(Instruction *InsertBefore, std::vector< CallSite > &ParsePointsNeeded, const TargetLibraryInfo &TLI)
bool isIntN(unsigned N) const
Check if this APInt has an N-bits unsigned integer value.
place backedge safepoints Place Backedge Safepoints
FunctionPassManager manages FunctionPasses and BasicBlockPassManagers.
DomTreeNodeBase< NodeT > * getNode(const NodeT *BB) const
getNode - return the (Post)DominatorTree node for the specified basic block.
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE, "Assign register bank of generic virtual registers", false, false) RegBankSelect
static bool isGCSafepointPoll(Function &F)
void sort(IteratorTy Start, IteratorTy End)
bool contains(const LoopT *L) const
Return true if the specified loop is contained within in this loop.
Iterator for intrusive lists based on ilist_node.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
bool dominates(const Instruction *Def, const Use &U) const
Return true if Def dominates a use in User.
Provides information about what library functions are available for the current target.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Function * getFunction(StringRef Name) const
Look up the specified function in the module symbol table.
void setPreservesAll()
Set by analyses that do not transform their input at all.
static cl::opt< bool > NoEntry("spp-no-entry", cl::Hidden, cl::init(false))
InstListType::iterator iterator
Instruction iterators...
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool equals(StringRef RHS) const
equals - Check for string equality, this is more efficient than compare() when the relative ordering ...
static cl::opt< int > CountedLoopTripWidth("spp-counted-loop-trip-width", cl::Hidden, cl::init(32))
How narrow does the trip count of a loop have to be to have to be considered "counted"? Counted loops do not get safepoints at backedges.
bool hasGC() const
hasGC/getGC/setGC/clearGC - The name of the garbage collection algorithm to use during code generatio...
bool isInlineAsm() const
Check if this call is an inline asm statement.
This class represents an analyzed expression in the program.
Represents a single loop in the control flow graph.
StringRef getName() const
Return a constant reference to the value's name.
const Function * getParent() const
Return the enclosing method, or null if none.
bool empty() const
Determine if the SetVector is empty or not.
Type * getValueType() const
place backedge safepoints Place Backedge false place safepoints
bool isDeclaration() const
Return true if the primary definition of this global value is outside of the current translation unit...
bool isStatepoint(ImmutableCallSite CS)
ConstantRange getUnsignedRange(const SCEV *S)
Determine the unsigned range for a particular SCEV.
static bool enableBackedgeSafepoints(Function &F)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Module * getParent()
Get the module that this global value is contained inside of...
A vector that has set insertion semantics.
succ_range successors(Instruction *I)
static bool containsUnconditionalCallSafepoint(Loop *L, BasicBlock *Header, BasicBlock *Pred, DominatorTree &DT, const TargetLibraryInfo &TLI)
Returns true if this loop is known to contain a call safepoint which must unconditionally execute on ...
The legacy pass manager's analysis pass to compute loop information.
StringRef - Represent a constant reference to a string, i.e.
const SCEV * getExitCount(const Loop *L, BasicBlock *ExitingBlock)
Get the expression for the number of loop iterations for which this loop is guaranteed not to exit vi...
Legacy analysis pass which computes a DominatorTree.
static bool needsStatepoint(const CallSite &CS, const TargetLibraryInfo &TLI)
A wrapper class for inspecting calls to intrinsic functions.
const BasicBlock * getParent() const