llir-opt  0.0.1
Low-Level Post-Link Optimiser for OCaml and C
reference_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 <memory>
8 #include <set>
9 #include <unordered_set>
10 #include <unordered_map>
11 #include <vector>
12 #include <llvm/Support/raw_ostream.h>
13 
14 class Block;
15 class CallGraph;
16 class Func;
17 class Global;
18 class Prog;
19 class MovInst;
20 class Object;
21 
22 
23 
27 using OffsetSet = std::set<std::pair<int64_t, int64_t>>;
28 
33 public:
35  struct Node {
37  bool HasIndirectCalls = false;
39  bool HasRaise = false;
41  bool HasBarrier = false;
43  std::unordered_set<Object *> ReadRanges;
45  std::unordered_map<Object *, OffsetSet> ReadOffsets;
47  std::unordered_set<Object *> WrittenRanges;
49  std::unordered_map<Object *, OffsetSet> WrittenOffsets;
51  std::unordered_set<Global *> Escapes;
53  std::unordered_set<Func *> Called;
55  std::unordered_set<Block *> Blocks;
56 
58  void Merge(const Node &that);
60  void AddRead(Object *object);
62  void AddWrite(Object *object);
63 
65  void dump(llvm::raw_ostream &os) const;
66  };
67 
69  ReferenceGraph(Prog &prog, CallGraph &graph);
70 
72  const Node &operator[](Func &func);
73 
74 protected:
76  virtual bool Skip(Func &func) { return false; }
77 
78 private:
80  void ExtractReferences(Func &func, Node &node);
82  void Build();
84  void Classify(Object *o, const MovInst &inst, Node &node);
86  void Classify(Object *o, const MovInst &inst, Node &node, int64_t offset);
87 
88 private:
90  CallGraph &graph_;
92  std::unordered_map<Func *, Node *> funcToNode_;
94  std::vector<std::unique_ptr<Node>> nodes_;
96  bool built_;
97 };
98 
102 inline llvm::raw_ostream &operator<<(
103  llvm::raw_ostream &os,
104  const ReferenceGraph::Node &node)
105 {
106  node.dump(os);
107  return os;
108 }
ReferenceGraph
Definition: reference_graph.h:32
Func
Definition: func.h:30
ReferenceGraph::Node::WrittenRanges
std::unordered_set< Object * > WrittenRanges
Set of written symbols.
Definition: reference_graph.h:47
ReferenceGraph::Node
Information about this node.
Definition: reference_graph.h:35
ReferenceGraph::Node::AddRead
void AddRead(Object *object)
Add an inaccurate read.
Definition: reference_graph.cpp:76
ReferenceGraph::Skip
virtual bool Skip(Func &func)
Callback which decides whether to follow or skip a function.
Definition: reference_graph.h:76
CallGraph
Definition: call_graph.h:20
ReferenceGraph::Node::WrittenOffsets
std::unordered_map< Object *, OffsetSet > WrittenOffsets
Set of written offsets in symbols.
Definition: reference_graph.h:49
ReferenceGraph::Node::HasRaise
bool HasRaise
Flag to indicate whether any reachable node raises.
Definition: reference_graph.h:39
Object
Definition: object.h:43
ReferenceGraph::Node::ReadRanges
std::unordered_set< Object * > ReadRanges
Set of referenced symbols.
Definition: reference_graph.h:43
MovInst
Definition: mov.h:17
ReferenceGraph::Node::HasIndirectCalls
bool HasIndirectCalls
Flag to indicate whether any reachable node has indirect calls.
Definition: reference_graph.h:37
ReferenceGraph::Node::AddWrite
void AddWrite(Object *object)
Add an inaccurate write.
Definition: reference_graph.cpp:89
ReferenceGraph::ReferenceGraph
ReferenceGraph(Prog &prog, CallGraph &graph)
Build reference information.
Definition: reference_graph.cpp:166
ReferenceGraph::Node::Blocks
std::unordered_set< Block * > Blocks
Set of addressed blocks.
Definition: reference_graph.h:55
ReferenceGraph::Node::HasBarrier
bool HasBarrier
Check whether there are barriers.
Definition: reference_graph.h:41
ReferenceGraph::Node::Merge
void Merge(const Node &that)
Merge another node into this one.
Definition: reference_graph.cpp:18
ReferenceGraph::Node::Escapes
std::unordered_set< Global * > Escapes
Set of symbols which escape.
Definition: reference_graph.h:51
ReferenceGraph::Node::Called
std::unordered_set< Func * > Called
Set of called functions.
Definition: reference_graph.h:53
Prog
Definition: prog.h:33
ReferenceGraph::Node::ReadOffsets
std::unordered_map< Object *, OffsetSet > ReadOffsets
Set of referenced offsets in objects.
Definition: reference_graph.h:45
Node
Definition: node.h:43
ReferenceGraph::operator[]
const Node & operator[](Func &func)
Return the set of globals referenced by a function.
Definition: reference_graph.cpp:214
ReferenceGraph::Node::dump
void dump(llvm::raw_ostream &os) const
Dump a representation of the node.
Definition: reference_graph.cpp:102
Block
Definition: block.h:29
Global
Definition: global.h:23