llir-opt  0.0.1
Low-Level Post-Link Optimiser for OCaml and C
object_graph.h
1 // This file if part of the llir-opt project.
2 // Licensing information can be found in the LICENSE file.
3 // (C) 2018 Nandor Licker. All rights reserved.
4 
5 #pragma once
6 
7 #include <llvm/ADT/GraphTraits.h>
8 #include <llvm/ADT/PointerUnion.h>
9 
10 #include "core/data.h"
11 #include "core/object.h"
12 #include "core/atom.h"
13 
14 
15 
19 class ObjectGraph final {
20 public:
24  class Node {
25  public:
27  class iterator {
28  public:
30  iterator(const Node *node, Item *start);
32  iterator(const Node *node, Object *func);
34  iterator() : it_(static_cast<Item *>(nullptr)) {}
35 
36  bool operator!=(const iterator &that) const { return !(*this == that); }
37  bool operator==(const iterator &that) const;
38 
39  iterator &operator++();
40 
41  iterator operator++(int)
42  {
43  iterator it(*this);
44  ++*this;
45  return it;
46  }
47 
48  Node *operator*() const;
49 
50  private:
52  const Node *node_;
54  llvm::PointerUnion<Item *, Object *> it_;
55  };
56 
57  public:
58 
60  Node(const ObjectGraph *graph, Prog *prog);
62  Node(const ObjectGraph *graph, Object *object);
63 
65  iterator begin() const;
66  iterator end() const { return iterator(); }
67 
69  Object *GetObject() const;
70 
71  private:
72  friend class iterator;
74  const ObjectGraph *graph_;
76  llvm::PointerUnion<Object *, Prog *> node_;
77  };
78 
79 public:
81  ObjectGraph(Prog &p);
82 
84  ~ObjectGraph();
85 
87  const Node *Entry() const { return &entry_; }
88 
90  Node *operator[](Object *o) const;
91 
92 private:
93  friend class Node::iterator;
95  Node entry_;
97  mutable std::unordered_map<Object *, std::unique_ptr<Node>> nodes_;
98 };
99 
101 namespace llvm {
102 
103 template <>
104 struct GraphTraits<ObjectGraph::Node *> {
105  using NodeRef = const ObjectGraph::Node *;
107 
108  static ChildIteratorType child_begin(NodeRef N) { return N->begin(); }
109  static ChildIteratorType child_end(NodeRef N) { return N->end(); }
110 };
111 
113 template <>
114 struct GraphTraits<ObjectGraph *> : public GraphTraits<ObjectGraph::Node *> {
115  static NodeRef getEntryNode(const ObjectGraph *g)
116  {
117  return g->Entry();
118  }
119 };
120 
121 } // end namespace llvm
ObjectGraph::~ObjectGraph
~ObjectGraph()
Cleanup.
Definition: object_graph.cpp:174
llvm
Graph traits for call graph nodes.
Definition: call_graph.h:129
ObjectGraph
Definition: object_graph.h:19
ObjectGraph::Node
Definition: object_graph.h:24
ObjectGraph::Node::iterator
Iterator over object references.
Definition: object_graph.h:27
Object
Definition: object.h:43
ObjectGraph::Entry
const Node * Entry() const
Returns the virtual the entry node.
Definition: object_graph.h:87
ObjectGraph::operator[]
Node * operator[](Object *o) const
Returns the node for an object.
Definition: object_graph.cpp:179
ObjectGraph::Node::GetObject
Object * GetObject() const
Returns the object, null for virtual entry.
Definition: object_graph.cpp:162
Prog
Definition: prog.h:33
Node
Definition: node.h:43
ObjectGraph::Node::Node
Node(const ObjectGraph *graph, Prog *prog)
Entry node.
Definition: object_graph.cpp:131
llvm::GraphTraits< ObjectGraph::Node * >
Definition: object_graph.h:104
ObjectGraph::Node::begin
iterator begin() const
Return iterators over the referenced objects.
Definition: object_graph.cpp:143
ObjectGraph::Node::iterator::iterator
iterator()
End iterator.
Definition: object_graph.h:34
ObjectGraph::ObjectGraph
ObjectGraph(Prog &p)
Creates a object graph for a program.
Definition: object_graph.cpp:168