35 #ifndef LLVM_ANALYSIS_LAZYCALLGRAPH_H
36 #define LLVM_ANALYSIS_LAZYCALLGRAPH_H
58 class PreservedAnalyses;
114 class call_edge_iterator;
142 explicit operator bool()
const;
168 void setKind(
Kind K) {
Value.setInt(K); }
193 std::forward_iterator_tag> {
202 while (
I != E && !*
I)
209 using iterator_adaptor_base::operator++;
213 }
while (
I != E && !*
I);
224 std::forward_iterator_tag> {
231 void advanceToNextEdge() {
232 while (
I !=
E && (!*
I || !
I->isCall()))
245 using iterator_adaptor_base::operator++;
258 assert(EdgeIndexMap.find(&N) != EdgeIndexMap.end() &&
"No such edge!");
259 return Edges[EdgeIndexMap.find(&N)->second];
262 auto EI = EdgeIndexMap.find(&N);
263 return EI != EdgeIndexMap.end() ? &Edges[EI->second] :
nullptr;
276 for (
auto &
E : Edges)
296 bool removeEdgeInternal(
Node &ChildN);
332 operator bool()
const {
return Edges.hasValue(); }
358 return populateSlow();
376 : G(&G), F(&F), DFSNumber(0), LowLink(0) {}
379 EdgeSequence &populateSlow();
386 void replaceFunction(
Function &NewF);
388 void clear() { Edges.reset(); }
417 template <
typename NodeRangeT>
418 SCC(
RefSCC &OuterRefSCC, NodeRangeT &&Nodes)
419 : OuterRefSCC(&OuterRefSCC), Nodes(std::forward<NodeRangeT>(Nodes)) {}
422 OuterRefSCC =
nullptr;
441 OS <<
"..., " << *C.Nodes.back();
470 int size()
const {
return Nodes.size(); }
564 OS <<
"..., " << *RC.SCCs.back();
592 void handleTrivialEdgeInsertion(
Node &SourceN,
Node &TargetN);
603 ssize_t
size()
const {
return SCCs.size(); }
608 return SCCs.begin() + SCCIndices.find(&C)->second;
626 return Parents.count(const_cast<RefSCC *>(&C));
865 std::forward_iterator_tag, RefSCC> {
880 : G(&G), RC(nullptr) {}
885 if (Index == (
int)G.PostOrderRefSCCs.size())
889 return G.PostOrderRefSCCs[Index];
894 return G == Arg.G && RC == Arg.RC;
899 using iterator_facade_base::operator++;
901 assert(RC &&
"Cannot increment the end iterator!");
902 RC = getRC(*G, G->RefSCCIndices.find(RC)->second + 1);
923 if (!EntryEdges.
empty())
924 assert(!PostOrderRefSCCs.empty() &&
925 "Must form RefSCCs before iterating them!");
929 if (!EntryEdges.
empty())
930 assert(!PostOrderRefSCCs.empty() &&
931 "Must form RefSCCs before iterating them!");
933 postorder_ref_scc_iterator::IsAtEndT());
955 return &
C->getOuterRefSCC();
967 return insertInto(
F, N);
975 return LibFunctions.getArrayRef();
1002 return insertEdge(
get(Source),
get(Target), EK);
1006 void removeEdge(Node &SourceN, Node &TargetN);
1051 template <
typename CallbackT>
1054 CallbackT Callback) {
1055 while (!Worklist.
empty()) {
1058 if (
Function *
F = dyn_cast<Function>(C)) {
1059 if (!
F->isDeclaration())
1074 if (Visited.
insert(cast<Constant>(
Op)).second)
1095 EdgeSequence EntryEdges;
1127 Node &insertInto(
Function &
F, Node *&MappedN);
1130 void updateGraphPtrs();
1135 template <
typename... Ts> SCC *createSCC(Ts &&...
Args) {
1136 return new (SCCBPA.Allocate()) SCC(std::forward<Ts>(
Args)...);
1142 template <
typename... Ts> RefSCC *createRefSCC(Ts &&...
Args) {
1143 return new (RefSCCBPA.Allocate()) RefSCC(std::forward<Ts>(
Args)...);
1157 template <
typename RootsT,
typename GetBeginT,
typename GetEndT,
1158 typename GetNodeT,
typename FormSCCCallbackT>
1159 static void buildGenericSCCs(RootsT &&Roots, GetBeginT &&GetBegin,
1160 GetEndT &&GetEnd, GetNodeT &&GetNode,
1161 FormSCCCallbackT &&FormSCC);
1164 void buildSCCs(RefSCC &RC, node_stack_range Nodes);
1170 void connectRefSCC(RefSCC &RC);
1176 int getRefSCCIndex(RefSCC &RC) {
1177 auto IndexIt = RefSCCIndices.find(&RC);
1178 assert(IndexIt != RefSCCIndices.end() &&
"RefSCC doesn't have an index!");
1179 assert(PostOrderRefSCCs[IndexIt->second] == &RC &&
1180 "Index does not point back at RC!");
1181 return IndexIt->second;
1188 inline LazyCallGraph::Edge::operator
bool()
const {
return Value.getPointer(); }
1191 assert(*
this &&
"Queried a null edge!");
1192 return Value.getInt();
1196 assert(*
this &&
"Queried a null edge!");
1197 return getKind() == Call;
1201 assert(*
this &&
"Queried a null edge!");
1202 return *
Value.getPointer();
1206 assert(*
this &&
"Queried a null edge!");
1207 return getNode().getFunction();
1225 static ChildIteratorType
child_end(NodeRef
N) {
return (*N)->end(); }
SCC * lookupSCC(Node &N) const
Lookup a function's SCC in the graph.
void push_back(const T &Elt)
pointee_iterator< SmallPtrSetImpl< RefSCC * >::const_iterator > parent_iterator
bool isChildOf(const RefSCC &C) const
Test if this RefSCC is a child of C.
EdgeSequence & operator*() const
EdgeSequence::iterator end()
void insertEdge(Node &SourceN, Node &TargetN, Edge::Kind EK)
Update the call graph after inserting a new edge.
iterator_range< call_iterator > calls()
call_iterator & operator++()
Kind getKind() const
Returnss the Kind of the edge.
void removeOutgoingEdge(Node &SourceN, Node &TargetN)
Remove an edge whose source is in this RefSCC and target is not.
bool isDescendantOf(const SCC &C) const
Test if this SCC is a descendant of C.
void removeEdge(Node &SourceN, Node &TargetN)
Update the call graph after deleting an edge.
EdgeSequence & populate()
Populate the edges of this node if necessary.
A Module instance is used to store all the information related to an LLVM module. ...
friend raw_ostream & operator<<(raw_ostream &OS, const RefSCC &RC)
Print a short description useful for debugging or logging.
Kind
The kind of edge in the graph.
Function & getFunction() const
Get the function referenced by this edge.
pointee_iterator< SmallVectorImpl< Node * >::const_iterator > iterator
static NodeRef getEntryNode(NodeRef N)
The edge sequence object.
This provides a very simple, boring adaptor for a begin and end iterator into a range type...
Node & getNode() const
Get the call graph node referenced by this edge.
An efficient, type-erasing, non-owning reference to a callable.
A pass which prints the call graph as a DOT file to a raw_ostream.
iterator_range< iterator > range
static void dump(StringRef Title, SpillInfo const &Spills)
std::string getName() const
Provide a short name by printing this RefSCC to a std::string.
parent_iterator parent_end() const
pointee_iterator< SmallVectorImpl< SCC * >::const_iterator > iterator
This file defines the MallocAllocator and BumpPtrAllocator interfaces.
StringRef getName() const
Return a constant reference to the value's name.
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
friend raw_ostream & operator<<(raw_ostream &OS, const Node &N)
Print the name of this node's function.
The address of a basic block.
LazyCallGraph::EdgeSequence::iterator ChildIteratorType
LazyCallGraph(Module &M, TargetLibraryInfo &TLI)
Construct a graph for the given module.
call_iterator call_begin()
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs...ExtraArgs)
Get the result of an analysis pass for a given IR unit.
bool isAncestorOf(const SCC &C) const
Test if this SCC is an ancestor of C.
EdgeSequence::iterator begin()
postorder_ref_scc_iterator postorder_ref_scc_begin()
bool isLibFunction(Function &F) const
Test whether a function is a known and defined library function tracked by the call graph...
bool isParentOf(const RefSCC &C) const
Test if this RefSCC is a parent of C.
bool isDescendantOf(const RefSCC &C) const
Test if this RefSCC is a descendant of C.
LLVM_NODISCARD bool empty() const
static ChildIteratorType child_begin(NodeRef N)
A RefSCC of the call graph.
bool isCall() const
Test whether the edge represents a direct call to a function.
void insertTrivialCallEdge(Node &SourceN, Node &TargetN)
A convenience wrapper around the above to handle trivial cases of inserting a new call edge...
A CRTP mix-in to automatically provide informational APIs needed for passes.
A lazily constructed view of the call graph of a module.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
iterator_range< parent_iterator > parents() const
LazyCallGraph & operator=(LazyCallGraph &&RHS)
void replaceNodeFunction(Node &N, Function &NewF)
Directly replace a node's function with a new function.
CRTP base class which implements the entire standard iterator facade in terms of a minimal subset of ...
void switchOutgoingEdgeToCall(Node &SourceN, Node &TargetN)
Make an existing outgoing ref edge into a call edge.
std::string getName() const
Provide a short name by printing this SCC to a std::string.
friend raw_ostream & operator<<(raw_ostream &OS, const SCC &C)
Print a short descrtiption useful for debugging or logging.
A set of analyses that are preserved following a run of a transformation pass.
parent_iterator parent_begin() const
An iterator used for the edges to both entry nodes and child nodes.
PointerIntPair - This class implements a pair of a pointer and small integer.
CRTP base class for adapting an iterator to a different type.
This is an important base class in LLVM.
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator begin()
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
void switchOutgoingEdgeToRef(Node &SourceN, Node &TargetN)
Make an existing outgoing call edge into a ref edge.
This file contains the declarations for the subclasses of Constant, which represent the different fla...
reference operator*() const
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
std::reverse_iterator< iterator > reverse_iterator
A CRTP mix-in that provides informational APIs needed for analysis passes.
void insertInternalRefEdge(Node &SourceN, Node &TargetN)
Insert a ref edge from one node in this RefSCC to another in this RefSCC.
SmallVector< RefSCC *, 1 > removeInternalRefEdge(Node &SourceN, Node &TargetN)
Remove a ref edge which is entirely within this RefSCC.
SmallVector< RefSCC *, 1 > insertIncomingRefEdge(Node &SourceN, Node &TargetN)
Insert an edge whose source is in a descendant RefSCC and target is in this RefSCC.
RefSCC & getOuterRefSCC() const
A node in the call graph.
SCC & operator[](int Idx)
A class used to represent edges in the call graph.
Edge & operator[](Node &N)
bool operator==(const Node &N) const
Equality is defined as address equality.
LazyCallGraph Result
Inform generic clients of the result type.
EdgeSequence * operator->() const
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
Function & getFunction() const
A SetVector that performs no allocations if smaller than a certain size.
postorder_ref_scc_iterator & operator++()
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
void removeDeadFunction(Function &F)
Remove a dead function from the call graph (typically to delete it).
LazyCallGraph run(Module &M, ModuleAnalysisManager &AM)
Compute the LazyCallGraph for the module M.
iterator find(SCC &C) const
Module.h This file contains the declarations for the Module class.
Provides information about what library functions are available for the current target.
LazyCallGraph::EdgeSequence::iterator ChildIteratorType
An iterator type that allows iterating over the pointees via some other iterator. ...
LLVM_NODISCARD T pop_back_val()
LazyCallGraph & getGraph() const
ArrayRef< Function * > getLibFunctions() const
Get the sequence of known and defined library functions.
bool isAncestorOf(const RefSCC &C) const
Test if this RefSCC is an ancestor of C.
A BumpPtrAllocator that allows only elements of a specific type to be allocated.
LazyCallGraph::Node * NodeRef
A range adaptor for a pair of iterators.
Target - Wrapper for Target specific information.
An iterator over specifically call edges.
typename SuperClass::iterator iterator
void insertOutgoingEdge(Node &SourceN, Node &TargetN, Edge::Kind EK)
Insert an edge whose parent is in this RefSCC and child is in some child RefSCC.
static void visitReferences(SmallVectorImpl< Constant * > &Worklist, SmallPtrSetImpl< Constant * > &Visited, CallbackT Callback)
Recursively visits the defined functions whose address is reachable from every constant in the Workli...
static void clear(coro::Shape &Shape)
void switchTrivialInternalEdgeToRef(Node &SourceN, Node &TargetN)
Make an existing internal call edge between separate SCCs into a ref edge.
iterator_range< iterator > switchInternalEdgeToRef(Node &SourceN, Node &TargetN)
Make an existing internal call edge within a single SCC into a ref edge.
bool switchInternalEdgeToCall(Node &SourceN, Node &TargetN, function_ref< void(ArrayRef< SCC * > MergedSCCs)> MergeCB={})
Make an existing internal ref edge into a call edge.
A post-order depth-first RefSCC iterator over the call graph.
Node * lookup(const Function &F) const
Lookup a function in the graph which has already been scanned and added.
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator end()
static NodeRef getEntryNode(NodeRef N)
A pass which prints the call graph to a raw_ostream.
bool operator!=(const Node &N) const
iterator_range< value_op_iterator > operand_values()
static ChildIteratorType child_end(NodeRef N)
StringRef getName() const
An analysis pass which computes the call graph for a module.
bool isChildOf(const SCC &C) const
Test if this SCC is a child of C.
Analysis pass providing the TargetLibraryInfo.
void insertTrivialRefEdge(Node &SourceN, Node &TargetN)
A convenience wrapper around the above to handle trivial cases of inserting a new ref edge...
void removeEdge(Function &Source, Function &Target)
Update the call graph after deleting an edge.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
A raw_ostream that writes to an std::string.
LLVM Value Representation.
LazyCallGraph::Node * NodeRef
An SCC of the call graph.
friend class LazyCallGraph
This class implements an extremely fast bulk output stream that can only output to a stream...
StringRef - Represent a constant reference to a string, i.e.
A container for analyses that lazily runs them and caches their results.
bool isParentOf(const SCC &C) const
Test if this SCC is a parent of C.
RefSCC * lookupRefSCC(Node &N) const
Lookup a function's RefSCC in the graph.
This header defines various interfaces for pass management in LLVM.
void insertEdge(Function &Source, Function &Target, Edge::Kind EK)
Update the call graph after inserting a new edge.
postorder_ref_scc_iterator postorder_ref_scc_end()
bool operator==(const postorder_ref_scc_iterator &Arg) const
static ChildIteratorType child_begin(NodeRef N)
iterator_range< postorder_ref_scc_iterator > postorder_ref_sccs()
A special type used by analysis passes to provide an address that identifies that particular analysis...
static ChildIteratorType child_end(NodeRef N)