llir-opt  0.0.1
Low-Level Post-Link Optimiser for OCaml and C
solver.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 <unordered_map>
8 #include <unordered_set>
9 #include <vector>
10 
11 #include "core/adt/queue.h"
12 #include "passes/pta/graph.h"
13 #include "passes/pta/scc.h"
14 
15 class Atom;
16 class Inst;
17 class Node;
18 class Global;
19 
20 class RootNode;
21 
22 
23 
27 class ConstraintSolver final {
28 public:
33 
35  Node *Load(Node *ptr);
36 
38  void Subset(Node *from, Node *to);
39 
41  RootNode *Root();
42 
44  Node *Empty();
45 
47  void Store(Node *ptr, Node *val)
48  {
49  Subset(val, Load(ptr));
50  }
51 
53  Node *Alloc(const std::vector<Inst *> &context)
54  {
55  auto *set = Set();
56  set->AddNode(Set()->GetID());
57  return set;
58  }
59 
61  RootNode *Lookup(Global *global);
62 
64  RootNode *Root(SetNode *set);
66  RootNode *Anchor(Node *node);
68  DerefNode *Deref(SetNode *set);
70  SetNode *Set();
71 
73  ID<Func *> Map(Func *func);
75  Func *Map(const ID<Func *> &id);
77  ID<Extern *> Map(Extern *ext);
79  Extern *Map(const ID<Extern *> &id);
81  SetNode *Map(const ID<SetNode *> &id);
82 
84  void Solve();
85 
86 private:
88  friend class RootNode;
89 
91  Graph graph_;
92 
94  std::unordered_map<Func *, ID<Func *>> funcToID_;
96  std::vector<Func *> idToFunc_;
98  std::unordered_map<Extern *, ID<Extern *>> extToID_;
100  std::vector<Extern *> idToExt_;
101 
103  SCCSolver scc_;
105  Queue<SetNode *> queue_;
106 };
Inst
Definition: inst.h:53
Func
Definition: func.h:30
ConstraintSolver::Alloc
Node * Alloc(const std::vector< Inst * > &context)
Allocation site.
Definition: solver.h:53
RootNode
Definition: node.h:256
Atom
Definition: atom.h:23
ConstraintSolver::Subset
void Subset(Node *from, Node *to)
Generates a subset constraint.
Definition: solver.cpp:69
SCCSolver
Definition: scc.h:19
ConstraintSolver::~ConstraintSolver
~ConstraintSolver()
Cleans up after the solver.
Definition: solver.cpp:28
ConstraintSolver
Definition: solver.h:27
ID
Definition: id.h:19
ConstraintSolver::Set
SetNode * Set()
Creates a set node.
Definition: solver.cpp:33
ConstraintSolver::Lookup
RootNode * Lookup(Global *global)
Returns the node attached to a global.
ConstraintSolver::Empty
Node * Empty()
Constructs an empty node.
Definition: solver.cpp:168
ConstraintSolver::ConstraintSolver
ConstraintSolver()
Initialises the solver.
Definition: solver.cpp:22
ConstraintSolver::Deref
DerefNode * Deref(SetNode *set)
Creates a deref node.
Definition: solver.cpp:40
SetNode
Definition: node.h:116
ConstraintSolver::Root
RootNode * Root()
Constructs a root node.
Definition: solver.cpp:96
ConstraintSolver::Anchor
RootNode * Anchor(Node *node)
Creates a root from a node.
Definition: solver.cpp:147
Node
Definition: node.h:43
ConstraintSolver::Store
void Store(Node *ptr, Node *val)
Creates a store constraint.
Definition: solver.h:47
DerefNode
Definition: node.h:205
Queue< SetNode * >
Graph
Definition: graph.h:23
Extern
Definition: extern.h:20
ConstraintSolver::Load
Node * Load(Node *ptr)
Returns a load constraint.
Definition: solver.cpp:47
ConstraintSolver::Solve
void Solve()
Solves the constraints until a fixpoint is reached.
Definition: solver.cpp:174
Global
Definition: global.h:23
ConstraintSolver::Map
ID< Func * > Map(Func *func)
Maps a function to an ID.
Definition: solver.cpp:109