llir-opt  0.0.1
Low-Level Post-Link Optimiser for OCaml and C
node.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 <functional>
8 #include <optional>
9 #include <unordered_set>
10 #include <utility>
11 #include <vector>
12 
13 #include <llvm/ADT/ilist.h>
14 #include <llvm/ADT/ilist_node.h>
15 #include <llvm/ADT/iterator.h>
16 #include <llvm/ADT/iterator_range.h>
17 
18 #include "core/adt/bitset.h"
19 
20 
21 
22 // Forward declaration of item types.
23 class Extern;
24 class Func;
25 
26 // Forward declaration of node types.
27 class RootNode;
28 class SetNode;
29 class DerefNode;
30 class GraphNode;
31 
32 // Forward declaration of the whole graph.
33 class Graph;
34 
35 // Solver needs access to traversal fields.
36 class SCCSolver;
37 
38 
39 
43 class Node {
44 public:
46  enum class Kind {
47  SET,
48  DEREF,
49  ROOT,
50  };
51 
53  virtual ~Node();
54 
56  GraphNode *ToGraph();
57 
59  RootNode *AsRoot();
60 
61 protected:
63  Node(Kind kind);
64 
65 protected:
68 };
69 
73 class GraphNode : public Node {
74 public:
76  GraphNode(Kind kind, uint64_t id);
77 
79  virtual ~GraphNode();
80 
82  uint64_t GetID() const { return id_; }
83 
85  bool IsDeref() const { return kind_ == Kind::DEREF; }
87  bool IsSet() const { return kind_ == Kind::SET; }
88 
90  SetNode *AsSet();
92  DerefNode *AsDeref();
93 
94 protected:
95  friend class SetNode;
96  friend class DerefNode;
98  uint64_t id_;
99 
100 private:
102  friend class SCCSolver;
104  uint32_t Epoch;
106  uint32_t Index;
108  uint32_t Link;
110  bool InComponent;
111 };
112 
116 class SetNode final : public GraphNode {
117 public:
119  SetNode(uint64_t id);
120 
122  ~SetNode();
123 
125  DerefNode *Deref();
126 
128  void AddFunc(ID<Func *> func) { funcs_.Insert(func); }
130  void AddExtern(ID<Extern *> ext) { exts_.Insert(ext); }
132  void AddNode(ID<SetNode *> node) { nodes_.Insert(node); }
133 
135  bool Propagate(SetNode *that);
137  bool Equals(SetNode *that);
138 
140  bool AddSet(SetNode *node);
142  bool AddDeref(DerefNode *node);
143 
145  llvm::iterator_range<BitSet<SetNode *>::iterator> sets()
146  {
147  return llvm::make_range(sets_.begin(), sets_.end());
148  }
149 
151  llvm::iterator_range<BitSet<DerefNode *>::iterator> derefs()
152  {
153  return llvm::make_range(derefOuts_.begin(), derefOuts_.end());
154  }
155 
157  llvm::iterator_range<BitSet<Func *>::iterator> points_to_func()
158  {
159  return llvm::make_range(funcs_.begin(), funcs_.end());
160  }
161 
163  llvm::iterator_range<BitSet<Extern *>::iterator> points_to_ext()
164  {
165  return llvm::make_range(exts_.begin(), exts_.end());
166  }
167 
169  llvm::iterator_range<BitSet<SetNode *>::iterator> points_to_node()
170  {
171  return llvm::make_range(nodes_.begin(), nodes_.end());
172  }
173 
175  void sets(std::function<ID<SetNode *>(ID<SetNode *>)> &&f);
177  void points_to_node(std::function<ID<SetNode *>(ID<SetNode *>)> &&f);
178 
179 private:
180  friend class Graph;
181  friend class RootNode;
182  friend class DerefNode;
183 
185  DerefNode *deref_;
186 
188  BitSet<SetNode *> sets_;
190  BitSet<DerefNode *> derefIns_;
192  BitSet<DerefNode *> derefOuts_;
193 
195  BitSet<Func *> funcs_;
197  BitSet<Extern *> exts_;
199  BitSet<SetNode *> nodes_;
200 };
201 
205 class DerefNode final : public GraphNode {
206 public:
208  DerefNode(SetNode *node, RootNode *contents, uint64_t id);
209 
211  ~DerefNode();
212 
214  SetNode *Node() const { return node_; }
216  SetNode *Contents();
217 
219  bool AddSet(SetNode *node);
220 
222  llvm::iterator_range<BitSet<SetNode *>::iterator> set_ins()
223  {
224  return llvm::make_range(setIns_.begin(), setIns_.end());
225  }
226 
228  llvm::iterator_range<BitSet<SetNode *>::iterator> set_outs()
229  {
230  return llvm::make_range(setOuts_.begin(), setOuts_.end());
231  }
232 
234  void set_ins(std::function<ID<SetNode *>(ID<SetNode *>)> &&f);
236  void set_outs(std::function<ID<SetNode *>(ID<SetNode *>)> &&f);
237 
238 private:
239  friend class Graph;
240  friend class SetNode;
241 
243  SetNode *node_;
245  RootNode *contents_;
246 
248  BitSet<SetNode *> setIns_;
250  BitSet<SetNode *> setOuts_;
251 };
252 
256 class RootNode : public Node {
257 public:
259  RootNode(Graph *graph, SetNode *actual);
260 
262  SetNode *Set() const;
263 
264 private:
265  friend class Node;
266  friend class SetNode;
267  friend class DerefNode;
268 
270  Graph *graph_;
272  mutable uint32_t id_;
273 };
274 
DerefNode::Node
SetNode * Node() const
Returns the dereferenced node.
Definition: node.h:214
Func
Definition: func.h:30
RootNode
Definition: node.h:256
SetNode::Equals
bool Equals(SetNode *that)
Checks if two nodes are equal.
Definition: node.cpp:113
GraphNode::id_
uint64_t id_
ID of the node.
Definition: node.h:98
DerefNode::Contents
SetNode * Contents()
Returns the set node with the contents.
Definition: node.cpp:169
SetNode::AddNode
void AddNode(ID< SetNode * > node)
Adds a node to the set.
Definition: node.h:132
BitSet::begin
iterator begin() const
Start iterator.
Definition: bitset.h:356
DerefNode::~DerefNode
~DerefNode()
Deletes the deref node.
Definition: node.cpp:164
SCCSolver
Definition: scc.h:19
DerefNode::set_ins
llvm::iterator_range< BitSet< SetNode * >::iterator > set_ins()
Iterator over the incoming edges.
Definition: node.h:222
RootNode::RootNode
RootNode(Graph *graph, SetNode *actual)
Creates a new root node.
Definition: node.cpp:222
Node::kind_
Kind kind_
Node kind.
Definition: node.h:67
BitSet::end
iterator end() const
End iterator.
Definition: bitset.h:362
ID
Definition: id.h:19
SetNode::AddDeref
bool AddDeref(DerefNode *node)
Adds an edge from this node to another set node.
Definition: node.cpp:102
SetNode::derefs
llvm::iterator_range< BitSet< DerefNode * >::iterator > derefs()
Iterator over the outgoing edges.
Definition: node.h:151
GraphNode::AsDeref
DerefNode * AsDeref()
Returns the node as a deref, if it is one.
Definition: node.cpp:62
GraphNode::~GraphNode
virtual ~GraphNode()
Deletes the node.
Definition: node.cpp:51
SetNode::points_to_node
llvm::iterator_range< BitSet< SetNode * >::iterator > points_to_node()
Nodes pointed to.
Definition: node.h:169
GraphNode::IsDeref
bool IsDeref() const
Checks if the node is a load.
Definition: node.h:85
BitSet< SetNode * >
RootNode::Set
SetNode * Set() const
Returns the set node.
Definition: node.cpp:230
Node::ToGraph
GraphNode * ToGraph()
Converts the node to a graph node.
Definition: node.cpp:23
SetNode
Definition: node.h:116
SetNode::points_to_func
llvm::iterator_range< BitSet< Func * >::iterator > points_to_func()
Functions pointed to.
Definition: node.h:157
BitSet::Insert
bool Insert(const ID< T > &item)
Inserts an item into the bitset.
Definition: bitset.h:391
SetNode::AddExtern
void AddExtern(ID< Extern * > ext)
Adds an extern to the set.
Definition: node.h:130
SetNode::~SetNode
~SetNode()
Deletes a set node.
Definition: node.cpp:75
Node
Definition: node.h:43
Node::Kind
Kind
Enumeration of enum kinds.
Definition: node.h:46
DerefNode
Definition: node.h:205
SetNode::points_to_ext
llvm::iterator_range< BitSet< Extern * >::iterator > points_to_ext()
Externs pointed to.
Definition: node.h:163
SetNode::Deref
DerefNode * Deref()
Returns a node dereferencing this one.
Definition: node.cpp:80
DerefNode::set_outs
llvm::iterator_range< BitSet< SetNode * >::iterator > set_outs()
Iterator over the outgoing edges.
Definition: node.h:228
SetNode::AddSet
bool AddSet(SetNode *node)
Adds an edge from this node to another set node.
Definition: node.cpp:96
GraphNode
Definition: node.h:73
GraphNode::AsSet
SetNode * AsSet()
Returns the node as a set, if it is one.
Definition: node.cpp:56
Graph
Definition: graph.h:23
SetNode::Propagate
bool Propagate(SetNode *that)
Propagates values to another set.
Definition: node.cpp:86
Node::AsRoot
RootNode * AsRoot()
Converts the node to a root node (if it is one).
Definition: node.cpp:34
Extern
Definition: extern.h:20
Node::Node
Node(Kind kind)
Creates a new node.
Definition: node.cpp:12
GraphNode::GetID
uint64_t GetID() const
Returns the ID of the node.
Definition: node.h:82
GraphNode::GraphNode
GraphNode(Kind kind, uint64_t id)
Constructs a graph node.
Definition: node.cpp:40
SetNode::sets
llvm::iterator_range< BitSet< SetNode * >::iterator > sets()
Iterator over the outgoing edges.
Definition: node.h:145
SetNode::AddFunc
void AddFunc(ID< Func * > func)
Adds a function to the set.
Definition: node.h:128
Node::~Node
virtual ~Node()
Virtual destructor.
Definition: node.cpp:18
GraphNode::IsSet
bool IsSet() const
Checks if the ndoe is a set.
Definition: node.h:87
DerefNode::AddSet
bool AddSet(SetNode *node)
Adds an edge from this node to another node.
Definition: node.cpp:175