DDG.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  1. //===- llvm/Analysis/DDG.h --------------------------------------*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file defines the Data-Dependence Graph (DDG).
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #ifndef LLVM_ANALYSIS_DDG_H
  13. #define LLVM_ANALYSIS_DDG_H
  14. #include "llvm/ADT/DenseMap.h"
  15. #include "llvm/ADT/DirectedGraph.h"
  16. #include "llvm/Analysis/DependenceAnalysis.h"
  17. #include "llvm/Analysis/DependenceGraphBuilder.h"
  18. #include "llvm/Analysis/LoopAnalysisManager.h"
  19. #include "llvm/IR/Instructions.h"
  20. namespace llvm {
  21. class DDGNode;
  22. class DDGEdge;
  23. using DDGNodeBase = DGNode<DDGNode, DDGEdge>;
  24. using DDGEdgeBase = DGEdge<DDGNode, DDGEdge>;
  25. using DDGBase = DirectedGraph<DDGNode, DDGEdge>;
  26. class LPMUpdater;
  27. /// Data Dependence Graph Node
  28. /// The graph can represent the following types of nodes:
  29. /// 1. Single instruction node containing just one instruction.
  30. /// 2. Multiple instruction node where two or more instructions from
  31. /// the same basic block are merged into one node.
  32. /// 3. Pi-block node which is a group of other DDG nodes that are part of a
  33. /// strongly-connected component of the graph.
  34. /// A pi-block node contains more than one single or multiple instruction
  35. /// nodes. The root node cannot be part of a pi-block.
  36. /// 4. Root node is a special node that connects to all components such that
  37. /// there is always a path from it to any node in the graph.
  38. class DDGNode : public DDGNodeBase {
  39. public:
  40. using InstructionListType = SmallVectorImpl<Instruction *>;
  41. enum class NodeKind {
  42. Unknown,
  43. SingleInstruction,
  44. MultiInstruction,
  45. PiBlock,
  46. Root,
  47. };
  48. DDGNode() = delete;
  49. DDGNode(const NodeKind K) : DDGNodeBase(), Kind(K) {}
  50. DDGNode(const DDGNode &N) : DDGNodeBase(N), Kind(N.Kind) {}
  51. DDGNode(DDGNode &&N) : DDGNodeBase(std::move(N)), Kind(N.Kind) {}
  52. virtual ~DDGNode() = 0;
  53. DDGNode &operator=(const DDGNode &N) {
  54. DGNode::operator=(N);
  55. Kind = N.Kind;
  56. return *this;
  57. }
  58. DDGNode &operator=(DDGNode &&N) {
  59. DGNode::operator=(std::move(N));
  60. Kind = N.Kind;
  61. return *this;
  62. }
  63. /// Getter for the kind of this node.
  64. NodeKind getKind() const { return Kind; }
  65. /// Collect a list of instructions, in \p IList, for which predicate \p Pred
  66. /// evaluates to true when iterating over instructions of this node. Return
  67. /// true if at least one instruction was collected, and false otherwise.
  68. bool collectInstructions(llvm::function_ref<bool(Instruction *)> const &Pred,
  69. InstructionListType &IList) const;
  70. protected:
  71. /// Setter for the kind of this node.
  72. void setKind(NodeKind K) { Kind = K; }
  73. private:
  74. NodeKind Kind;
  75. };
  76. /// Subclass of DDGNode representing the root node of the graph.
  77. /// There should only be one such node in a given graph.
  78. class RootDDGNode : public DDGNode {
  79. public:
  80. RootDDGNode() : DDGNode(NodeKind::Root) {}
  81. RootDDGNode(const RootDDGNode &N) = delete;
  82. RootDDGNode(RootDDGNode &&N) : DDGNode(std::move(N)) {}
  83. ~RootDDGNode() {}
  84. /// Define classof to be able to use isa<>, cast<>, dyn_cast<>, etc.
  85. static bool classof(const DDGNode *N) {
  86. return N->getKind() == NodeKind::Root;
  87. }
  88. static bool classof(const RootDDGNode *N) { return true; }
  89. };
  90. /// Subclass of DDGNode representing single or multi-instruction nodes.
  91. class SimpleDDGNode : public DDGNode {
  92. friend class DDGBuilder;
  93. public:
  94. SimpleDDGNode() = delete;
  95. SimpleDDGNode(Instruction &I);
  96. SimpleDDGNode(const SimpleDDGNode &N);
  97. SimpleDDGNode(SimpleDDGNode &&N);
  98. ~SimpleDDGNode();
  99. SimpleDDGNode &operator=(const SimpleDDGNode &N) {
  100. DDGNode::operator=(N);
  101. InstList = N.InstList;
  102. return *this;
  103. }
  104. SimpleDDGNode &operator=(SimpleDDGNode &&N) {
  105. DDGNode::operator=(std::move(N));
  106. InstList = std::move(N.InstList);
  107. return *this;
  108. }
  109. /// Get the list of instructions in this node.
  110. const InstructionListType &getInstructions() const {
  111. assert(!InstList.empty() && "Instruction List is empty.");
  112. return InstList;
  113. }
  114. InstructionListType &getInstructions() {
  115. return const_cast<InstructionListType &>(
  116. static_cast<const SimpleDDGNode *>(this)->getInstructions());
  117. }
  118. /// Get the first/last instruction in the node.
  119. Instruction *getFirstInstruction() const { return getInstructions().front(); }
  120. Instruction *getLastInstruction() const { return getInstructions().back(); }
  121. /// Define classof to be able to use isa<>, cast<>, dyn_cast<>, etc.
  122. static bool classof(const DDGNode *N) {
  123. return N->getKind() == NodeKind::SingleInstruction ||
  124. N->getKind() == NodeKind::MultiInstruction;
  125. }
  126. static bool classof(const SimpleDDGNode *N) { return true; }
  127. private:
  128. /// Append the list of instructions in \p Input to this node.
  129. void appendInstructions(const InstructionListType &Input) {
  130. setKind((InstList.size() == 0 && Input.size() == 1)
  131. ? NodeKind::SingleInstruction
  132. : NodeKind::MultiInstruction);
  133. llvm::append_range(InstList, Input);
  134. }
  135. void appendInstructions(const SimpleDDGNode &Input) {
  136. appendInstructions(Input.getInstructions());
  137. }
  138. /// List of instructions associated with a single or multi-instruction node.
  139. SmallVector<Instruction *, 2> InstList;
  140. };
  141. /// Subclass of DDGNode representing a pi-block. A pi-block represents a group
  142. /// of DDG nodes that are part of a strongly-connected component of the graph.
  143. /// Replacing all the SCCs with pi-blocks results in an acyclic representation
  144. /// of the DDG. For example if we have:
  145. /// {a -> b}, {b -> c, d}, {c -> a}
  146. /// the cycle a -> b -> c -> a is abstracted into a pi-block "p" as follows:
  147. /// {p -> d} with "p" containing: {a -> b}, {b -> c}, {c -> a}
  148. class PiBlockDDGNode : public DDGNode {
  149. public:
  150. using PiNodeList = SmallVector<DDGNode *, 4>;
  151. PiBlockDDGNode() = delete;
  152. PiBlockDDGNode(const PiNodeList &List);
  153. PiBlockDDGNode(const PiBlockDDGNode &N);
  154. PiBlockDDGNode(PiBlockDDGNode &&N);
  155. ~PiBlockDDGNode();
  156. PiBlockDDGNode &operator=(const PiBlockDDGNode &N) {
  157. DDGNode::operator=(N);
  158. NodeList = N.NodeList;
  159. return *this;
  160. }
  161. PiBlockDDGNode &operator=(PiBlockDDGNode &&N) {
  162. DDGNode::operator=(std::move(N));
  163. NodeList = std::move(N.NodeList);
  164. return *this;
  165. }
  166. /// Get the list of nodes in this pi-block.
  167. const PiNodeList &getNodes() const {
  168. assert(!NodeList.empty() && "Node list is empty.");
  169. return NodeList;
  170. }
  171. PiNodeList &getNodes() {
  172. return const_cast<PiNodeList &>(
  173. static_cast<const PiBlockDDGNode *>(this)->getNodes());
  174. }
  175. /// Define classof to be able to use isa<>, cast<>, dyn_cast<>, etc.
  176. static bool classof(const DDGNode *N) {
  177. return N->getKind() == NodeKind::PiBlock;
  178. }
  179. private:
  180. /// List of nodes in this pi-block.
  181. PiNodeList NodeList;
  182. };
  183. /// Data Dependency Graph Edge.
  184. /// An edge in the DDG can represent a def-use relationship or
  185. /// a memory dependence based on the result of DependenceAnalysis.
  186. /// A rooted edge connects the root node to one of the components
  187. /// of the graph.
  188. class DDGEdge : public DDGEdgeBase {
  189. public:
  190. /// The kind of edge in the DDG
  191. enum class EdgeKind {
  192. Unknown,
  193. RegisterDefUse,
  194. MemoryDependence,
  195. Rooted,
  196. Last = Rooted // Must be equal to the largest enum value.
  197. };
  198. explicit DDGEdge(DDGNode &N) = delete;
  199. DDGEdge(DDGNode &N, EdgeKind K) : DDGEdgeBase(N), Kind(K) {}
  200. DDGEdge(const DDGEdge &E) : DDGEdgeBase(E), Kind(E.getKind()) {}
  201. DDGEdge(DDGEdge &&E) : DDGEdgeBase(std::move(E)), Kind(E.Kind) {}
  202. DDGEdge &operator=(const DDGEdge &E) {
  203. DDGEdgeBase::operator=(E);
  204. Kind = E.Kind;
  205. return *this;
  206. }
  207. DDGEdge &operator=(DDGEdge &&E) {
  208. DDGEdgeBase::operator=(std::move(E));
  209. Kind = E.Kind;
  210. return *this;
  211. }
  212. /// Get the edge kind
  213. EdgeKind getKind() const { return Kind; };
  214. /// Return true if this is a def-use edge, and false otherwise.
  215. bool isDefUse() const { return Kind == EdgeKind::RegisterDefUse; }
  216. /// Return true if this is a memory dependence edge, and false otherwise.
  217. bool isMemoryDependence() const { return Kind == EdgeKind::MemoryDependence; }
  218. /// Return true if this is an edge stemming from the root node, and false
  219. /// otherwise.
  220. bool isRooted() const { return Kind == EdgeKind::Rooted; }
  221. private:
  222. EdgeKind Kind;
  223. };
  224. /// Encapsulate some common data and functionality needed for different
  225. /// variations of data dependence graphs.
  226. template <typename NodeType> class DependenceGraphInfo {
  227. public:
  228. using DependenceList = SmallVector<std::unique_ptr<Dependence>, 1>;
  229. DependenceGraphInfo() = delete;
  230. DependenceGraphInfo(const DependenceGraphInfo &G) = delete;
  231. DependenceGraphInfo(const std::string &N, const DependenceInfo &DepInfo)
  232. : Name(N), DI(DepInfo), Root(nullptr) {}
  233. DependenceGraphInfo(DependenceGraphInfo &&G)
  234. : Name(std::move(G.Name)), DI(std::move(G.DI)), Root(G.Root) {}
  235. virtual ~DependenceGraphInfo() {}
  236. /// Return the label that is used to name this graph.
  237. StringRef getName() const { return Name; }
  238. /// Return the root node of the graph.
  239. NodeType &getRoot() const {
  240. assert(Root && "Root node is not available yet. Graph construction may "
  241. "still be in progress\n");
  242. return *Root;
  243. }
  244. /// Collect all the data dependency infos coming from any pair of memory
  245. /// accesses from \p Src to \p Dst, and store them into \p Deps. Return true
  246. /// if a dependence exists, and false otherwise.
  247. bool getDependencies(const NodeType &Src, const NodeType &Dst,
  248. DependenceList &Deps) const;
  249. /// Return a string representing the type of dependence that the dependence
  250. /// analysis identified between the two given nodes. This function assumes
  251. /// that there is a memory dependence between the given two nodes.
  252. std::string getDependenceString(const NodeType &Src,
  253. const NodeType &Dst) const;
  254. protected:
  255. // Name of the graph.
  256. std::string Name;
  257. // Store a copy of DependenceInfo in the graph, so that individual memory
  258. // dependencies don't need to be stored. Instead when the dependence is
  259. // queried it is recomputed using @DI.
  260. const DependenceInfo DI;
  261. // A special node in the graph that has an edge to every connected component of
  262. // the graph, to ensure all nodes are reachable in a graph walk.
  263. NodeType *Root = nullptr;
  264. };
  265. using DDGInfo = DependenceGraphInfo<DDGNode>;
  266. /// Data Dependency Graph
  267. class DataDependenceGraph : public DDGBase, public DDGInfo {
  268. friend AbstractDependenceGraphBuilder<DataDependenceGraph>;
  269. friend class DDGBuilder;
  270. public:
  271. using NodeType = DDGNode;
  272. using EdgeType = DDGEdge;
  273. DataDependenceGraph() = delete;
  274. DataDependenceGraph(const DataDependenceGraph &G) = delete;
  275. DataDependenceGraph(DataDependenceGraph &&G)
  276. : DDGBase(std::move(G)), DDGInfo(std::move(G)) {}
  277. DataDependenceGraph(Function &F, DependenceInfo &DI);
  278. DataDependenceGraph(Loop &L, LoopInfo &LI, DependenceInfo &DI);
  279. ~DataDependenceGraph();
  280. /// If node \p N belongs to a pi-block return a pointer to the pi-block,
  281. /// otherwise return null.
  282. const PiBlockDDGNode *getPiBlock(const NodeType &N) const;
  283. protected:
  284. /// Add node \p N to the graph, if it's not added yet, and keep track of the
  285. /// root node as well as pi-blocks and their members. Return true if node is
  286. /// successfully added.
  287. bool addNode(NodeType &N);
  288. private:
  289. using PiBlockMapType = DenseMap<const NodeType *, const PiBlockDDGNode *>;
  290. /// Mapping from graph nodes to their containing pi-blocks. If a node is not
  291. /// part of a pi-block, it will not appear in this map.
  292. PiBlockMapType PiBlockMap;
  293. };
  294. /// Concrete implementation of a pure data dependence graph builder. This class
  295. /// provides custom implementation for the pure-virtual functions used in the
  296. /// generic dependence graph build algorithm.
  297. ///
  298. /// For information about time complexity of the build algorithm see the
  299. /// comments near the declaration of AbstractDependenceGraphBuilder.
  300. class DDGBuilder : public AbstractDependenceGraphBuilder<DataDependenceGraph> {
  301. public:
  302. DDGBuilder(DataDependenceGraph &G, DependenceInfo &D,
  303. const BasicBlockListType &BBs)
  304. : AbstractDependenceGraphBuilder(G, D, BBs) {}
  305. DDGNode &createRootNode() final override {
  306. auto *RN = new RootDDGNode();
  307. assert(RN && "Failed to allocate memory for DDG root node.");
  308. Graph.addNode(*RN);
  309. return *RN;
  310. }
  311. DDGNode &createFineGrainedNode(Instruction &I) final override {
  312. auto *SN = new SimpleDDGNode(I);
  313. assert(SN && "Failed to allocate memory for simple DDG node.");
  314. Graph.addNode(*SN);
  315. return *SN;
  316. }
  317. DDGNode &createPiBlock(const NodeListType &L) final override {
  318. auto *Pi = new PiBlockDDGNode(L);
  319. assert(Pi && "Failed to allocate memory for pi-block node.");
  320. Graph.addNode(*Pi);
  321. return *Pi;
  322. }
  323. DDGEdge &createDefUseEdge(DDGNode &Src, DDGNode &Tgt) final override {
  324. auto *E = new DDGEdge(Tgt, DDGEdge::EdgeKind::RegisterDefUse);
  325. assert(E && "Failed to allocate memory for edge");
  326. Graph.connect(Src, Tgt, *E);
  327. return *E;
  328. }
  329. DDGEdge &createMemoryEdge(DDGNode &Src, DDGNode &Tgt) final override {
  330. auto *E = new DDGEdge(Tgt, DDGEdge::EdgeKind::MemoryDependence);
  331. assert(E && "Failed to allocate memory for edge");
  332. Graph.connect(Src, Tgt, *E);
  333. return *E;
  334. }
  335. DDGEdge &createRootedEdge(DDGNode &Src, DDGNode &Tgt) final override {
  336. auto *E = new DDGEdge(Tgt, DDGEdge::EdgeKind::Rooted);
  337. assert(E && "Failed to allocate memory for edge");
  338. assert(isa<RootDDGNode>(Src) && "Expected root node");
  339. Graph.connect(Src, Tgt, *E);
  340. return *E;
  341. }
  342. const NodeListType &getNodesInPiBlock(const DDGNode &N) final override {
  343. auto *PiNode = dyn_cast<const PiBlockDDGNode>(&N);
  344. assert(PiNode && "Expected a pi-block node.");
  345. return PiNode->getNodes();
  346. }
  347. /// Return true if the two nodes \pSrc and \pTgt are both simple nodes and
  348. /// the consecutive instructions after merging belong to the same basic block.
  349. bool areNodesMergeable(const DDGNode &Src,
  350. const DDGNode &Tgt) const final override;
  351. void mergeNodes(DDGNode &Src, DDGNode &Tgt) final override;
  352. bool shouldSimplify() const final override;
  353. bool shouldCreatePiBlocks() const final override;
  354. };
  355. raw_ostream &operator<<(raw_ostream &OS, const DDGNode &N);
  356. raw_ostream &operator<<(raw_ostream &OS, const DDGNode::NodeKind K);
  357. raw_ostream &operator<<(raw_ostream &OS, const DDGEdge &E);
  358. raw_ostream &operator<<(raw_ostream &OS, const DDGEdge::EdgeKind K);
  359. raw_ostream &operator<<(raw_ostream &OS, const DataDependenceGraph &G);
  360. //===--------------------------------------------------------------------===//
  361. // DDG Analysis Passes
  362. //===--------------------------------------------------------------------===//
  363. /// Analysis pass that builds the DDG for a loop.
  364. class DDGAnalysis : public AnalysisInfoMixin<DDGAnalysis> {
  365. public:
  366. using Result = std::unique_ptr<DataDependenceGraph>;
  367. Result run(Loop &L, LoopAnalysisManager &AM, LoopStandardAnalysisResults &AR);
  368. private:
  369. friend AnalysisInfoMixin<DDGAnalysis>;
  370. static AnalysisKey Key;
  371. };
  372. /// Textual printer pass for the DDG of a loop.
  373. class DDGAnalysisPrinterPass : public PassInfoMixin<DDGAnalysisPrinterPass> {
  374. public:
  375. explicit DDGAnalysisPrinterPass(raw_ostream &OS) : OS(OS) {}
  376. PreservedAnalyses run(Loop &L, LoopAnalysisManager &AM,
  377. LoopStandardAnalysisResults &AR, LPMUpdater &U);
  378. private:
  379. raw_ostream &OS;
  380. };
  381. //===--------------------------------------------------------------------===//
  382. // DependenceGraphInfo Implementation
  383. //===--------------------------------------------------------------------===//
  384. template <typename NodeType>
  385. bool DependenceGraphInfo<NodeType>::getDependencies(
  386. const NodeType &Src, const NodeType &Dst, DependenceList &Deps) const {
  387. assert(Deps.empty() && "Expected empty output list at the start.");
  388. // List of memory access instructions from src and dst nodes.
  389. SmallVector<Instruction *, 8> SrcIList, DstIList;
  390. auto isMemoryAccess = [](const Instruction *I) {
  391. return I->mayReadOrWriteMemory();
  392. };
  393. Src.collectInstructions(isMemoryAccess, SrcIList);
  394. Dst.collectInstructions(isMemoryAccess, DstIList);
  395. for (auto *SrcI : SrcIList)
  396. for (auto *DstI : DstIList)
  397. if (auto Dep =
  398. const_cast<DependenceInfo *>(&DI)->depends(SrcI, DstI, true))
  399. Deps.push_back(std::move(Dep));
  400. return !Deps.empty();
  401. }
  402. template <typename NodeType>
  403. std::string
  404. DependenceGraphInfo<NodeType>::getDependenceString(const NodeType &Src,
  405. const NodeType &Dst) const {
  406. std::string Str;
  407. raw_string_ostream OS(Str);
  408. DependenceList Deps;
  409. if (!getDependencies(Src, Dst, Deps))
  410. return OS.str();
  411. interleaveComma(Deps, OS, [&](const std::unique_ptr<Dependence> &D) {
  412. D->dump(OS);
  413. // Remove the extra new-line character printed by the dump
  414. // method
  415. if (OS.str().back() == '\n')
  416. OS.str().pop_back();
  417. });
  418. return OS.str();
  419. }
  420. //===--------------------------------------------------------------------===//
  421. // GraphTraits specializations for the DDG
  422. //===--------------------------------------------------------------------===//
  423. /// non-const versions of the grapth trait specializations for DDG
  424. template <> struct GraphTraits<DDGNode *> {
  425. using NodeRef = DDGNode *;
  426. static DDGNode *DDGGetTargetNode(DGEdge<DDGNode, DDGEdge> *P) {
  427. return &P->getTargetNode();
  428. }
  429. // Provide a mapped iterator so that the GraphTrait-based implementations can
  430. // find the target nodes without having to explicitly go through the edges.
  431. using ChildIteratorType =
  432. mapped_iterator<DDGNode::iterator, decltype(&DDGGetTargetNode)>;
  433. using ChildEdgeIteratorType = DDGNode::iterator;
  434. static NodeRef getEntryNode(NodeRef N) { return N; }
  435. static ChildIteratorType child_begin(NodeRef N) {
  436. return ChildIteratorType(N->begin(), &DDGGetTargetNode);
  437. }
  438. static ChildIteratorType child_end(NodeRef N) {
  439. return ChildIteratorType(N->end(), &DDGGetTargetNode);
  440. }
  441. static ChildEdgeIteratorType child_edge_begin(NodeRef N) {
  442. return N->begin();
  443. }
  444. static ChildEdgeIteratorType child_edge_end(NodeRef N) { return N->end(); }
  445. };
  446. template <>
  447. struct GraphTraits<DataDependenceGraph *> : public GraphTraits<DDGNode *> {
  448. using nodes_iterator = DataDependenceGraph::iterator;
  449. static NodeRef getEntryNode(DataDependenceGraph *DG) {
  450. return &DG->getRoot();
  451. }
  452. static nodes_iterator nodes_begin(DataDependenceGraph *DG) {
  453. return DG->begin();
  454. }
  455. static nodes_iterator nodes_end(DataDependenceGraph *DG) { return DG->end(); }
  456. };
  457. /// const versions of the grapth trait specializations for DDG
  458. template <> struct GraphTraits<const DDGNode *> {
  459. using NodeRef = const DDGNode *;
  460. static const DDGNode *DDGGetTargetNode(const DGEdge<DDGNode, DDGEdge> *P) {
  461. return &P->getTargetNode();
  462. }
  463. // Provide a mapped iterator so that the GraphTrait-based implementations can
  464. // find the target nodes without having to explicitly go through the edges.
  465. using ChildIteratorType =
  466. mapped_iterator<DDGNode::const_iterator, decltype(&DDGGetTargetNode)>;
  467. using ChildEdgeIteratorType = DDGNode::const_iterator;
  468. static NodeRef getEntryNode(NodeRef N) { return N; }
  469. static ChildIteratorType child_begin(NodeRef N) {
  470. return ChildIteratorType(N->begin(), &DDGGetTargetNode);
  471. }
  472. static ChildIteratorType child_end(NodeRef N) {
  473. return ChildIteratorType(N->end(), &DDGGetTargetNode);
  474. }
  475. static ChildEdgeIteratorType child_edge_begin(NodeRef N) {
  476. return N->begin();
  477. }
  478. static ChildEdgeIteratorType child_edge_end(NodeRef N) { return N->end(); }
  479. };
  480. template <>
  481. struct GraphTraits<const DataDependenceGraph *>
  482. : public GraphTraits<const DDGNode *> {
  483. using nodes_iterator = DataDependenceGraph::const_iterator;
  484. static NodeRef getEntryNode(const DataDependenceGraph *DG) {
  485. return &DG->getRoot();
  486. }
  487. static nodes_iterator nodes_begin(const DataDependenceGraph *DG) {
  488. return DG->begin();
  489. }
  490. static nodes_iterator nodes_end(const DataDependenceGraph *DG) {
  491. return DG->end();
  492. }
  493. };
  494. } // namespace llvm
  495. #endif // LLVM_ANALYSIS_DDG_H