llir-opt  0.0.1
Low-Level Post-Link Optimiser for OCaml and C
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 <vector>
8 #include <memory>
9 
10 #include "core/adt/id.h"
11 
12 class Node;
13 class DerefNode;
14 class GraphNode;
15 class RootNode;
16 class SetNode;
17 
18 
19 
23 class Graph final {
24 public:
28  class SetIterator : public std::iterator<std::forward_iterator_tag, SetNode *> {
29  public:
30  bool operator==(const SetIterator &x) const { return it_ == x.it_; }
31  bool operator!=(const SetIterator &x) const { return !operator==(x); }
32 
33  SetIterator &operator++() {
34  ++it_;
35  Skip();
36  return *this;
37  }
38 
39  SetIterator operator++(int) {
40  auto tmp = *this;
41  ++*this;
42  return tmp;
43  }
44 
45  SetNode *operator*() const { return *it_; }
46 
47  private:
48  friend class Graph;
49 
51  Graph *graph,
52  std::vector<SetNode *>::iterator it)
53  : graph_(graph)
54  , it_(it)
55  {
56  Skip();
57  }
58 
59  void Skip()
60  {
61  while (it_ != graph_->sets_.end() && !*it_) {
62  ++it_;
63  }
64  }
65 
66  private:
68  Graph *graph_;
70  std::vector<SetNode *>::iterator it_;
71  };
72 
73 public:
75  SetNode *Set();
77  DerefNode *Deref(SetNode *set);
79  RootNode *Root(SetNode *set);
80 
82  SetNode *Get(ID<SetNode *> id) const { return sets_[id]; }
84  DerefNode *Get(ID<DerefNode *> id) const { return derefs_[id]; }
85 
89  DerefNode *Find(ID<DerefNode *> id) const { return derefs_[id]; }
90 
92  SetNode *Union(SetNode *a, SetNode *b);
93 
95  SetIterator begin() { return SetIterator(this, sets_.begin()); }
97  SetIterator end() { return SetIterator(this, sets_.end()); }
98 
99 private:
101  void Replace(SetNode *a, SetNode *b);
103  void Replace(DerefNode *a, DerefNode *b);
104 
105 private:
106  friend class SCCSolver;
107 
109  std::vector<SetNode *> sets_;
111  std::vector<DerefNode *> derefs_;
113  std::vector<RootNode *> roots_;
114 
116  std::vector<std::unique_ptr<Node>> nodes_;
117 
119  struct Entry {
121  uint32_t Parent;
123  uint32_t Rank;
124 
126  Entry(uint32_t parent, uint32_t rank) : Parent(parent), Rank(rank) {}
127  };
128 
130  std::vector<Entry> unions_;
131 };
RootNode
Definition: node.h:256
Graph::Deref
DerefNode * Deref(SetNode *set)
Creates a deref node.
Definition: graph.cpp:22
Graph::Find
DerefNode * Find(ID< DerefNode * > id) const
Finds a deref node, given its ID.
Definition: graph.h:89
Graph::Find
SetNode * Find(ID< SetNode * > id)
Finds a set node, given its ID.
Definition: graph.cpp:44
SCCSolver
Definition: scc.h:19
Graph::SetIterator
Definition: graph.h:28
ID
Definition: id.h:19
Graph::Get
DerefNode * Get(ID< DerefNode * > id) const
Returns a deref mapped to an id.
Definition: graph.h:84
Graph::Root
RootNode * Root(SetNode *set)
Creates a root node.
Definition: graph.cpp:34
Graph::end
SetIterator end()
Iterator over sets - end.
Definition: graph.h:97
Graph::Union
SetNode * Union(SetNode *a, SetNode *b)
Unifies two nodes.
Definition: graph.cpp:59
Graph::Get
SetNode * Get(ID< SetNode * > id) const
Returns a set mapped to an id.
Definition: graph.h:82
Graph::Set
SetNode * Set()
Creates a set node.
Definition: graph.cpp:11
SetNode
Definition: node.h:116
Node
Definition: node.h:43
DerefNode
Definition: node.h:205
GraphNode
Definition: node.h:73
Graph
Definition: graph.h:23
Graph::begin
SetIterator begin()
Iterator over sets - begin.
Definition: graph.h:95