22#define DEBUG_TYPE "dgb"
24STATISTIC(TotalGraphs,
"Number of dependence graphs created.");
25STATISTIC(TotalDefUseEdges,
"Number of def-use edges created.");
26STATISTIC(TotalMemoryEdges,
"Number of memory dependence edges created.");
27STATISTIC(TotalFineGrainedNodes,
"Number of fine-grained nodes created.");
28STATISTIC(TotalPiBlockNodes,
"Number of pi-block nodes created.");
30 "Number of confused memory dependencies between two nodes.");
32 "Number of times the source and sink of dependence was reversed to "
33 "expose cycles in the graph.");
44 size_t NextOrdinal = 1;
53 assert(
IMap.empty() &&
"Expected empty instruction map at start");
57 IMap.insert(std::make_pair(&
I, &NewNode));
59 ++TotalFineGrainedNodes;
118 for (NodeListType &NL : ListOfSCCs) {
120 <<
" nodes in it.\n");
126 return getOrdinal(*LHS) < getOrdinal(*RHS);
129 NodeType &PiNode = createPiBlock(NL);
141 if (*
N == PiNode || NodesInSCC.count(
N))
158 using EdgeKind =
typename EdgeType::EdgeKind;
165 case EdgeKind::RegisterDefUse:
166 createDefUseEdge(Src, Dst);
168 case EdgeKind::MemoryDependence:
169 createMemoryEdge(Src, Dst);
171 case EdgeKind::Rooted:
172 createRootedEdge(Src, Dst);
181 if (!Src->hasEdgeTo(*Dst))
184 dbgs() <<
"reconnecting("
185 << (Dir == Direction::Incoming ?
"incoming)" :
"outgoing)")
186 <<
":\nSrc:" << *Src <<
"\nDst:" << *Dst <<
"\nNew:" << *New
188 assert((Dir == Direction::Incoming || Dir == Direction::Outgoing) &&
189 "Invalid direction.");
192 Src->findEdgesTo(*Dst, EL);
193 for (EdgeType *OldEdge : EL) {
194 EdgeKind Kind = OldEdge->getKind();
195 if (!EdgeAlreadyCreated[Dir][Kind]) {
196 if (Dir == Direction::Incoming) {
197 createEdgeOfKind(*Src, *New, Kind);
199 }
else if (Dir == Direction::Outgoing) {
200 createEdgeOfKind(*New, *Dst, Kind);
203 EdgeAlreadyCreated[Dir][Kind] =
true;
205 Src->removeEdge(*OldEdge);
206 destroyEdge(*OldEdge);
207 LLVM_DEBUG(
dbgs() <<
"removed old edge between Src and Dst.\n\n");
213 reconnectEdges(
N, SCCNode, &PiNode, Direction::Incoming);
216 reconnectEdges(SCCNode,
N, &PiNode, Direction::Outgoing);
222 InstOrdinalMap.
clear();
223 NodeOrdinalMap.clear();
229 for (NodeType *
N :
Graph) {
231 N->collectInstructions([](
const Instruction *
I) {
return true; }, SrcIList);
240 NodeType *DstNode =
IMap.lookup(UI);
249 <<
"skipped def-use edge since the sink" << *UI
250 <<
" is outside the range of instructions being considered.\n");
258 <<
"skipped def-use edge since the sink and the source ("
259 <<
N <<
") are the same.\n");
263 if (VisitedTargets.
insert(DstNode).second) {
274 using DGIterator =
typename G::iterator;
276 return I->mayReadOrWriteMemory();
278 for (DGIterator SrcIt =
Graph.begin(), E =
Graph.end(); SrcIt != E; ++SrcIt) {
280 (*SrcIt)->collectInstructions(isMemoryAccess, SrcIList);
281 if (SrcIList.
empty())
284 for (DGIterator DstIt = SrcIt; DstIt != E; ++DstIt) {
285 if (**SrcIt == **DstIt)
288 (*DstIt)->collectInstructions(isMemoryAccess, DstIList);
289 if (DstIList.
empty())
291 bool ForwardEdgeCreated =
false;
292 bool BackwardEdgeCreated =
false;
295 auto D =
DI.depends(ISrc, IDst);
305 auto createConfusedEdges = [&](NodeType &Src, NodeType &Dst) {
306 if (!ForwardEdgeCreated) {
310 if (!BackwardEdgeCreated) {
314 ForwardEdgeCreated = BackwardEdgeCreated =
true;
315 ++TotalConfusedEdges;
318 auto createForwardEdge = [&](NodeType &Src, NodeType &Dst) {
319 if (!ForwardEdgeCreated) {
323 ForwardEdgeCreated =
true;
326 auto createBackwardEdge = [&](NodeType &Src, NodeType &Dst) {
327 if (!BackwardEdgeCreated) {
331 BackwardEdgeCreated =
true;
335 createConfusedEdges(**SrcIt, **DstIt);
336 else if (
D->isOrdered() && !
D->isLoopIndependent()) {
337 bool ReversedEdge =
false;
338 for (
unsigned Level = 1; Level <=
D->getLevels(); ++Level) {
342 createBackwardEdge(**SrcIt, **DstIt);
344 ++TotalEdgeReversals;
349 createConfusedEdges(**SrcIt, **DstIt);
354 createForwardEdge(**SrcIt, **DstIt);
356 createForwardEdge(**SrcIt, **DstIt);
359 if (ForwardEdgeCreated && BackwardEdgeCreated)
366 if (ForwardEdgeCreated && BackwardEdgeCreated)
389 for (NodeType *
N :
Graph) {
390 if (
N->getEdges().size() != 1)
392 EdgeType &Edge =
N->back();
393 if (!Edge.isDefUse())
395 CandidateSourceNodes.
insert(
N);
399 TargetInDegreeMap.
insert({&Edge.getTargetNode(), 0});
403 dbgs() <<
"Size of candidate src node list:" << CandidateSourceNodes.
size()
404 <<
"\nNode with single outgoing def-use edge:\n";
405 for (NodeType *
N : CandidateSourceNodes) {
410 for (NodeType *
N :
Graph) {
411 for (EdgeType *E : *
N) {
412 NodeType *Tgt = &E->getTargetNode();
413 auto TgtIT = TargetInDegreeMap.
find(Tgt);
414 if (TgtIT != TargetInDegreeMap.
end())
420 dbgs() <<
"Size of target in-degree map:" << TargetInDegreeMap.
size()
421 <<
"\nContent of in-degree map:\n";
422 for (
auto &
I : TargetInDegreeMap) {
423 dbgs() <<
I.first <<
" --> " <<
I.second <<
"\n";
428 CandidateSourceNodes.
end());
429 while (!Worklist.empty()) {
430 NodeType &Src = *Worklist.pop_back_val();
433 if (!CandidateSourceNodes.
erase(&Src))
436 assert(Src.getEdges().size() == 1 &&
437 "Expected a single edge from the candidate src node.");
438 NodeType &Tgt = Src.back().getTargetNode();
439 assert(TargetInDegreeMap.
find(&Tgt) != TargetInDegreeMap.
end() &&
440 "Expected target to be in the in-degree map.");
442 if (TargetInDegreeMap[&Tgt] != 1)
450 if (Tgt.hasEdgeTo(Src))
453 LLVM_DEBUG(
dbgs() <<
"Merging:" << Src <<
"\nWith:" << Tgt <<
"\n");
467 if (CandidateSourceNodes.
erase(&Tgt)) {
468 Worklist.push_back(&Src);
469 CandidateSourceNodes.
insert(&Src);
470 LLVM_DEBUG(
dbgs() <<
"Putting " << &Src <<
" back in the worklist.\n");
484 using NodeKind =
typename NodeType::NodeKind;
486 if (
N->getKind() == NodeKind::PiBlock) {
495 size_t OldSize =
Graph.Nodes.size();
498 if (
Graph.Nodes.size() != OldSize)
500 "Expected the number of nodes to stay the same after the sort");
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
SmallVector< Instruction *, 2 > InstructionListType
This file builds on the ADT/GraphTraits.h file to build generic depth first graph iterator.
This file defines an array type that can be indexed using scoped enum values.
Loop::LoopBounds::Direction Direction
uint64_t IntrinsicInst * II
This file builds on the ADT/GraphTraits.h file to build a generic graph post order iterator.
This builds on the llvm/ADT/GraphTraits.h file to find the strongly connected components (SCCs) of a ...
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
This abstract builder class defines a set of high-level steps for creating DDG-like graphs.
void createAndConnectRootNode()
Create a root node and add edges such that each node in the graph is reachable from the root.
void computeInstructionOrdinals()
Compute ordinal numbers for each instruction and store them in a map for future look up.
virtual const NodeListType & getNodesInPiBlock(const NodeType &N)=0
Given a pi-block node, return a vector of all the nodes contained within it.
virtual EdgeType & createMemoryEdge(NodeType &Src, NodeType &Tgt)=0
Create a memory dependence edge going from Src to Tgt.
virtual bool shouldCreatePiBlocks() const
virtual bool shouldSimplify() const
Return true if graph simplification step is requested, and false otherwise.
void sortNodesTopologically()
Topologically sort the graph nodes.
virtual NodeType & createFineGrainedNode(Instruction &I)=0
Create an atomic node in the graph given a single instruction.
virtual EdgeType & createRootedEdge(NodeType &Src, NodeType &Tgt)=0
void createFineGrainedNodes()
Create fine grained nodes.
virtual NodeType & createRootNode()=0
InstToOrdinalMap InstOrdinalMap
A mapping from each instruction to an ordinal number.
virtual EdgeType & createDefUseEdge(NodeType &Src, NodeType &Tgt)=0
Create a def-use edge going from Src to Tgt.
void createMemoryDependencyEdges()
Analyze data dependencies that exist between memory loads or stores, in the graph nodes and create ed...
virtual void mergeNodes(NodeType &A, NodeType &B)=0
Append the content of node B into node A and remove B and the edge between A and B from the graph.
DependenceInfo & DI
Dependence information used to create memory dependence edges in the graph.
void simplify()
Go through all the nodes in the graph and collapse any two nodes 'a' and 'b' if all of the following ...
const BasicBlockListType & BBList
The list of basic blocks to consider when building the graph.
InstToNodeMap IMap
A mapping from instructions to the corresponding nodes in the graph.
size_t getOrdinal(Instruction &I)
Given an instruction I return its associated ordinal number.
SmallVector< NodeType *, 4 > NodeListType
void createDefUseEdges()
Analyze the def-use chains and create edges from the nodes containing definitions to the nodes contai...
NodeToOrdinalMap NodeOrdinalMap
A mapping from nodes to an ordinal number.
DataDependenceGraph & Graph
virtual bool areNodesMergeable(const NodeType &A, const NodeType &B) const =0
Return true if it's safe to merge the two nodes.
void createPiBlocks()
Apply graph abstraction to groups of nodes that belong to a strongly connected component of the graph...
LLVM Basic Block Representation.
iterator find(const_arg_type_t< KeyT > Val)
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Encapsulate some common data and functionality needed for different variations of data dependence gra...
bool erase(PtrType Ptr)
Remove pointer from the set.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
reference emplace_back(ArgTypes &&... Args)
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
This is an optimization pass for GlobalISel generic memory operations.
iterator_range< df_ext_iterator< T, SetTy > > depth_first_ext(const T &G, SetTy &S)
constexpr from_range_t from_range
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
scc_iterator< T > scc_begin(const T &G)
Construct the begin iterator for a deduced graph type T.
auto reverse(ContainerTy &&C)
void sort(IteratorTy Start, IteratorTy End)
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
auto post_order(const T &G)
Post-order traversal of a graph.
scc_iterator< T > scc_end(const T &G)
Construct the end iterator for a deduced graph type T.
Determine the kind of a node from its type.