DDG.h 20 KB

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