84 #define DEBUG_TYPE "safepoint-placement"
85 STATISTIC(NumEntrySafepoints,
"Number of entry safepoints inserted");
86 STATISTIC(NumCallSafepoints,
"Number of call safepoints inserted");
87 STATISTIC(NumBackedgeSafepoints,
"Number of backedge safepoints inserted");
89 STATISTIC(CallInLoop,
"Number of loops w/o safepoints due to calls in loop");
90 STATISTIC(FiniteExecution,
"Number of loops w/o safepoints finite execution");
116 struct PlaceBackedgeSafepointsImpl :
public FunctionPass {
121 std::vector<TerminatorInst *> PollLocations;
125 bool CallSafepointsEnabled;
131 PlaceBackedgeSafepointsImpl(
bool CallSafepoints =
false)
136 bool runOnLoop(
Loop *);
137 void runOnLoopAndSubLoops(
Loop *L) {
139 for (
auto I = L->
begin(), E = L->
end();
I != E;
I++)
140 runOnLoopAndSubLoops(*
I);
144 bool runOnFunction(
Function &
F)
override {
145 SE = &getAnalysis<ScalarEvolution>();
146 DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
147 LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
148 for (
auto I = LI->begin(), E = LI->end();
I != E;
I++) {
149 runOnLoopAndSubLoops(*
I);
176 bool runOnFunction(
Function &F)
override;
191 std::vector<CallSite> &ParsePointsNeeded );
228 assert(DT.
dominates(Header, Pred) &&
"loop latch not dominated by header?");
244 if (Current == Header)
259 const unsigned upperTripBound = 8192;
291 std::vector<CallInst *> &calls,
292 std::set<BasicBlock *> &seen,
293 std::vector<BasicBlock *> &worklist) {
297 if (
CallInst *CI = dyn_cast<CallInst>(&*itr)) {
301 assert(!dyn_cast<InvokeInst>(&*itr) &&
302 "support for invokes in poll code needed");
305 if (itr->isTerminator()) {
308 if (seen.count(Succ) == 0) {
309 worklist.push_back(Succ);
317 std::vector<CallInst *> &calls,
318 std::set<BasicBlock *> &seen) {
320 std::vector<BasicBlock *> worklist;
322 scanOneBB(start, end, calls, seen, worklist);
323 while (!worklist.empty()) {
330 bool PlaceBackedgeSafepointsImpl::runOnLoop(
Loop *L) {
348 errs() <<
"skipping safepoint placement in finite loop\n";
352 if (CallSafepointsEnabled &&
358 errs() <<
"skipping safepoint placement due to unconditional call\n";
375 errs() <<
"[LSP] terminator instruction: ";
379 PollLocations.push_back(term);
390 switch (II->getIntrinsicID()) {
391 case Intrinsic::experimental_gc_statepoint:
392 case Intrinsic::experimental_patchpoint_void:
393 case Intrinsic::experimental_patchpoint_i64:
424 if (!
I->isTerminator()) {
427 BasicBlock *nextBB =
I->getParent()->getUniqueSuccessor();
431 auto nextInstruction = [&hasNextInstruction](
Instruction *
I) {
432 assert(hasNextInstruction(
I) &&
433 "first check if there is a next instruction!");
434 if (
I->isTerminator()) {
435 return I->getParent()->getUniqueSuccessor()->begin();
443 cursor = nextInstruction(cursor)) {
459 assert((hasNextInstruction(cursor) || cursor->
isTerminator()) &&
460 "either we stopped because of a call, or because of terminator");
467 std::vector<CallSite> &
Found ) {
468 assert(Found.empty() &&
"must be empty!");
471 if (isa<CallInst>(inst) || isa<InvokeInst>(inst)) {
490 vec.reserve(vec.size());
493 if (seen.insert(V).second) {
511 const char *FunctionGCName = F.
getGC();
512 const StringRef StatepointExampleName(
"statepoint-example");
514 return (StatepointExampleName == FunctionGCName) ||
515 (CoreCLRName == FunctionGCName);
539 assert(!isa<PHINode>(ret->
begin()));
544 bool PlaceSafepoints::runOnFunction(
Function &F) {
561 bool modified =
false;
577 std::vector<CallSite> ParsePointNeeded;
586 PlaceBackedgeSafepointsImpl *PBS =
587 new PlaceBackedgeSafepointsImpl(CanAssumeCallSafepoints);
595 auto &PollLocations = PBS->PollLocations;
602 std::sort(PollLocations.begin(), PollLocations.end(), OrderByBBName);
607 PollLocations.erase(std::unique(PollLocations.begin(),
608 PollLocations.end()),
609 PollLocations.end());
629 for (
unsigned i = 0; i < Term->getNumSuccessors(); i++) {
635 assert(!Headers.
empty() &&
"poll location is not a loop latch?");
644 NumBackedgeSafepoints++;
649 NumBackedgeSafepoints++;
661 NumEntrySafepoints++;
668 std::vector<CallSite> RuntimeCalls;
670 ParsePointNeeded.insert(ParsePointNeeded.end(), RuntimeCalls.begin(),
679 std::vector<CallSite> Calls;
681 NumCallSafepoints += Calls.size();
682 ParsePointNeeded.insert(ParsePointNeeded.end(), Calls.begin(), Calls.end());
695 if (!ParsePointNeeded.empty())
701 std::vector<Value *> Results;
702 Results.reserve(ParsePointNeeded.size());
703 for (
size_t i = 0; i < ParsePointNeeded.size(); i++) {
713 Invoke->getParent());
717 Results.push_back(GCResult);
719 assert(Results.size() == ParsePointNeeded.size());
722 for (
size_t i = 0; i < ParsePointNeeded.size(); i++) {
724 Value *GCResult = Results[i];
744 return new PlaceSafepoints();
748 "place-backedge-safepoints-impl",
749 "Place Backedge Safepoints",
false,
false)
764 if (isa<IntrinsicInst>(inst)) {
777 const Function *F = CS.getCalledFunction();
789 std::vector<CallSite> &ParsePointsNeeded ) {
792 assert(M &&
"must be part of a module");
801 "gc.safepoint_poll declared with wrong type");
802 assert(!F->
empty() &&
"gc.safepoint_poll must be a non-empty function");
808 if (before == OrigBB->
begin()) {
814 assert(after != OrigBB->
end() &&
"must have successor");
819 assert(InlineStatus &&
"inline must succeed");
823 assert(IFI.StaticAllocas.empty() &&
"can't have allocs");
825 std::vector<CallInst *> calls;
826 std::set<BasicBlock *> BBs;
831 start = OrigBB->
begin();
840 "malformed poll function");
843 assert(!calls.empty() &&
"slow path not found for safepoint poll");
849 assert(ParsePointsNeeded.empty());
850 for (
size_t i = 0; i < calls.size(); i++) {
859 ParsePointsNeeded.push_back(
CallSite(calls[i]));
861 assert(ParsePointsNeeded.size() <= calls.size());
890 uint32_t NumPatchBytes;
907 bool HasNumPatchBytes =
911 if (HasNumPatchBytes)
912 AttrsToRemove.
addAttribute(
"statepoint-num-patch-bytes");
920 Value *StatepointTarget = NumPatchBytes == 0
928 ID, NumPatchBytes, StatepointTarget,
943 assert(ToReplace->
getNextNode() &&
"not a terminator, must have next");
952 Builder.SetInsertPoint(ToReplace->
getParent());
953 InvokeInst *Invoke = Builder.CreateGCStatepointInvoke(
954 ID, NumPatchBytes, StatepointTarget, ToReplace->
getNormalDest(),
971 assert(!isa<PHINode>(NormalDest->
begin()));
973 Builder.SetInsertPoint(IP);
984 std::string TakenName =
986 CallInst *GCResult = Builder.CreateGCResult(Token, CS.
getType(), TakenName);
Pass interface - Implemented by all 'passes'.
std::enable_if< std::numeric_limits< T >::is_signed, bool >::type getAsInteger(unsigned Radix, T &Result) const
Parse the current string as an integer of the specified radix.
static bool mustBeFiniteCountedLoop(Loop *L, ScalarEvolution *SE, BasicBlock *Pred)
Returns true if this loop is known to terminate in a finite number of iterations. ...
iplist< Instruction >::iterator eraseFromParent()
eraseFromParent - This method unlinks 'this' from the containing basic block and deletes it...
void setAttributes(const AttributeSet &Attrs)
setAttributes - Set the parameter attributes for this invoke.
void push_back(const T &Elt)
const_iterator end(StringRef path)
Get end iterator over path.
BasicBlock * getUniquePredecessor()
Return the predecessor of this block if it has a unique predecessor block.
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
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...
const Instruction & back() const
const SCEV * getExitCount(Loop *L, BasicBlock *ExitingBlock)
void getLoopLatches(SmallVectorImpl< BlockT * > &LoopLatches) const
getLoopLatches - Return all loop latch blocks of this loop.
A Module instance is used to store all the information related to an LLVM module. ...
bool InlineFunction(CallInst *C, InlineFunctionInfo &IFI, bool InsertLifetime=true)
InlineFunction - This function inlines the called function into the basic block of the caller...
InstrTy * getInstruction() const
ScalarEvolution - This class is the main scalar evolution driver.
const char * getGC() const
CallInst - This class represents a function call, abstracting a target machine's calling convention...
static Value * ReplaceWithStatepoint(const CallSite &CS, Pass *P)
Replaces the given call site (Call or Invoke) with a gc.statepoint intrinsic with an empty deoptimiza...
static void scanOneBB(Instruction *start, Instruction *end, std::vector< CallInst * > &calls, std::set< BasicBlock * > &seen, std::vector< BasicBlock * > &worklist)
bool isLoopExiting(const BlockT *BB) const
isLoopExiting - True if terminator in the block can branch to another block that is outside of the cu...
void initializePlaceSafepointsPass(PassRegistry &)
const Function * getParent() const
Return the enclosing method, or null if none.
Attribute getFnAttribute(Attribute::AttrKind Kind) const
Return the attribute for the given attribute kind.
AttrBuilder & addAttribute(Attribute::AttrKind Val)
Add an attribute to the builder.
InlineFunctionInfo - This class captures the data input to the InlineFunction call, and records the auxiliary results produced by it.
BlockT * getHeader() const
STATISTIC(NumEntrySafepoints,"Number of entry safepoints inserted")
AttributeSet getRetAttributes() const
The attributes for the ret value are returned.
StringRef getName() const
Return a constant reference to the value's name.
iterator begin()
Instruction iterator methods.
static void scanInlinedCode(Instruction *start, Instruction *end, std::vector< CallInst * > &calls, std::set< BasicBlock * > &seen)
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
static CallInst * Create(Value *Func, ArrayRef< Value * > Args, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
NodeTy * getNextNode()
Get the next node, or 0 for the list tail.
ArrayRef< T > makeArrayRef(const T &OneElt)
Construct an ArrayRef from a single element.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
static bool isGCLeafFunction(const CallSite &CS)
bool isIntN(unsigned N) const
Check if this APInt has an N-bits unsigned integer value.
static cl::opt< bool > NoBackedge("spp-no-backedge", cl::Hidden, cl::init(false))
bool isStatepoint(const ImmutableCallSite &CS)
place backedge safepoints impl
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)
static bool containsUnconditionalCallSafepoint(Loop *L, BasicBlock *Header, BasicBlock *Pred, DominatorTree &DT)
Returns true if this loop is known to contain a call safepoint which must unconditionally execute on ...
bool insert(const value_type &X)
Insert a new element into the SetVector.
static bool needsStatepoint(const CallSite &CS)
ValTy * getCalledValue() const
getCalledValue - Return the pointer to function that is being called.
bool empty() const
Determine if the SetVector is empty or not.
static FunctionType * get(Type *Result, ArrayRef< Type * > Params, bool isVarArg)
FunctionType::get - This static method is the primary way of constructing a FunctionType.
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree...
Type * getElementType() const
BasicBlock * getNormalDest() const
bool ult(const APInt &RHS) const
Unsigned less than comparison.
void FoldSingleEntryPHINodes(BasicBlock *BB, AliasAnalysis *AA=nullptr, MemoryDependenceAnalysis *MemDep=nullptr)
FoldSingleEntryPHINodes - We know that BB has one predecessor.
Function * getFunction(StringRef Name) const
Look up the specified function in the module symbol table.
void initializePlaceBackedgeSafepointsImplPass(PassRegistry &)
static const char *const GCSafepointPollName
static cl::opt< bool > SplitBackedge("spp-split-backedge", cl::Hidden, cl::init(false))
static Instruction * findLocationForEntrySafepoint(Function &F, DominatorTree &DT)
const SCEV * getCouldNotCompute()
initializer< Ty > init(const Ty &Val)
static bool enableEntrySafepoints(Function &F)
Subclasses of this class are all able to terminate a basic block.
static ConstantPointerNull * get(PointerType *T)
get() - Static factory methods - Return objects of the specified value
bool isLeaf(ID id)
Returns true if the intrinsic is a leaf, i.e.
static void unique_unsorted(std::vector< T > &vec)
Implement a unique function which doesn't require we sort the input vector.
FunctionPass * createPlaceSafepointsPass()
LLVM Basic Block Representation.
bool isGCRelocate(const Value *V)
static cl::opt< bool > NoCall("spp-no-call", cl::Hidden, cl::init(false))
const DebugLoc & getDebugLoc() const
getDebugLoc - Return the debug location for this node as a DebugLoc.
Represent the analysis usage information of a pass.
static Type * getVoidTy(LLVMContext &C)
bool contains(const LoopT *L) const
contains - Return true if the specified loop is contained within in this loop.
static cl::opt< bool > AllBackedges("spp-all-backedges", cl::Hidden, cl::init(false))
const SCEV * getMaxBackedgeTakenCount(const Loop *L)
getMaxBackedgeTakenCount - Similar to getBackedgeTakenCount, except return the least SCEV value that ...
FunctionPass class - This class is used to implement most global optimizations.
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.
static BasicBlock * normalizeForInvokeSafepoint(BasicBlock *BB, BasicBlock *InvokeParent)
place backedge safepoints Place Backedge Safepoints
FunctionPassManager manages FunctionPasses and BasicBlockPassManagers.
LLVMContext & getContext() const
All values hold a context through their type.
CallingConv::ID getCallingConv() const
getCallingConv/setCallingConv - Get or set the calling convention of this function call...
static bool isGCSafepointPoll(Function &F)
void dump() const
Support for debugging, callable in GDB: V->dump()
bool isTerminator() const
bool dominates(const Instruction *Def, const Use &U) const
Return true if Def dominates a use in User.
BasicBlock * getUnwindDest() const
DomTreeNodeBase< NodeT > * getIDom() const
const Module * getModule() const
Return the module owning the function this instruction belongs to or nullptr it the function does not...
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Module.h This file contains the declarations for the Module class.
Type * getType() const
All values are typed, get the type of this value.
static void findCallSafepoints(Function &F, std::vector< CallSite > &Found)
Identify the list of call sites which need to be have parseable state.
const BasicBlock & getEntryBlock() const
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
AttributeSet removeAttributes(LLVMContext &C, unsigned Index, AttributeSet Attrs) const
Remove the specified attributes at the specified index from this attribute list.
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))
INITIALIZE_PASS_BEGIN(PlaceBackedgeSafepointsImpl,"place-backedge-safepoints-impl","Place Backedge Safepoints", false, false) INITIALIZE_PASS_END(PlaceBackedgeSafepointsImpl
bool hasGC() const
hasGC/getGC/setGC/clearGC - The name of the garbage collection algorithm to use during code generatio...
bool equals(StringRef RHS) const
equals - Check for string equality, this is more efficient than compare() when the relative ordering ...
bool isInlineAsm() const
isInlineAsm - Check if this call is an inline asm statement.
PointerType * getType() const
Global values are always pointers.
SCEV - This class represents an analyzed expression in the program.
iterator_range< inst_iterator > inst_range(Function *F)
bool isDeclaration() const
Return true if the primary definition of this global value is outside of the current translation unit...
TerminatorInst * getTerminator()
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
const AttributeSet & getAttributes() const
getAttributes/setAttributes - get or set the parameter attributes of the call.
bool isStringAttribute() const
Return true if the attribute is a string (target-dependent) attribute.
place backedge safepoints Place Backedge false place safepoints
Attribute getAttribute(unsigned Index, Attribute::AttrKind Kind) const
Return the attribute object that exists at the given index.
Type * getType() const
getType - Return the type of the instruction that generated this call site
ConstantRange getUnsignedRange(const SCEV *S)
getUnsignedRange - Determine the unsigned range for a particular SCEV.
StringRef getValueAsString() const
Return the attribute's value as a string.
static bool enableBackedgeSafepoints(Function &F)
void setAttributes(const AttributeSet &Attrs)
setAttributes - Set the parameter attributes for this call.
Module * getParent()
Get the module that this global value is contained inside of...
LLVM Value Representation.
succ_range successors(BasicBlock *BB)
A vector that has set insertion semantics.
place backedge safepoints Place Backedge false
static const Function * getParent(const Value *V)
static cl::opt< bool > TraceLSP("spp-trace", cl::Hidden, cl::init(false))
void setCallingConv(CallingConv::ID CC)
InvokeInst - Invoke instruction.
BasicBlock * SplitEdge(BasicBlock *From, BasicBlock *To, DominatorTree *DT=nullptr, LoopInfo *LI=nullptr)
SplitEdge - Split the edge connecting specified block.
IterTy arg_begin() const
arg_begin/arg_end - Return iterators corresponding to the actual argument list for a call site...
The legacy pass manager's analysis pass to compute loop information.
void recalculate(FT &F)
recalculate - compute a dominator tree for the given function
StringRef - Represent a constant reference to a string, i.e.
BasicBlock * SplitBlockPredecessors(BasicBlock *BB, ArrayRef< BasicBlock * > Preds, const char *Suffix, AliasAnalysis *AA=nullptr, DominatorTree *DT=nullptr, LoopInfo *LI=nullptr, bool PreserveLCSSA=false)
SplitBlockPredecessors - This method introduces at least one new basic block into the function and mo...
bool removeUnreachableBlocks(Function &F)
Remove all blocks that can not be reached from the function's entry.
CallingConv::ID getCallingConv() const
getCallingConv/setCallingConv - Get or set the calling convention of this function call...
Legacy analysis pass which computes a DominatorTree.
iterator getFirstInsertionPt()
Returns an iterator to the first instruction in this block that is suitable for inserting a non-PHI i...
DomTreeNodeBase< NodeT > * getNode(NodeT *BB) const
getNode - return the (Post)DominatorTree node for the specified basic block.
bool isGCResult(const Value *V)
APInt getUnsignedMax() const
Return the largest unsigned value contained in the ConstantRange.
bool isInvoke() const
isInvoke - true if a InvokeInst is enclosed.
static cl::opt< bool > SkipCounted("spp-counted", cl::Hidden, cl::init(true))
If true, do not place backedge safepoints in counted loops.
bool isCall() const
isCall - true if a CallInst is enclosed.
const BasicBlock * getParent() const
InstListType::iterator iterator
Instruction iterators...
static void InsertSafepointPoll(Instruction *InsertBefore, std::vector< CallSite > &ParsePointsNeeded)
IntrinsicInst - A useful wrapper class for inspecting calls to intrinsic functions.
LLVMContext & getContext() const
Get the global data context.
bool isVoidTy() const
isVoidTy - Return true if this is 'void'.
AttributeSet getFnAttributes() const
The function attributes are returned.