llir-opt  0.0.1
Low-Level Post-Link Optimiser for OCaml and C
dag.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 
9 #include <llvm/ADT/DenseSet.h>
10 #include <llvm/Support/raw_ostream.h>
11 
12 class Block;
13 class Func;
14 
15 
16 
20 struct DAGBlock {
22  const unsigned Index;
23 
25  llvm::DenseSet<Block *> Blocks;
27  llvm::SmallVector<DAGBlock *, 4> Succs;
29  llvm::SmallVector<DAGBlock *, 4> Preds;
31  int64_t Length;
33  bool IsLoop;
35  bool Lands;
37  bool Returns;
39  bool IsReturn;
41  bool Raises;
43  bool IsRaise;
45  bool Traps;
47  bool IsTrap;
48 
50  bool Exits() const
51  {
52  return Returns || Raises || Traps;
53  }
54 
56  bool IsExit() const
57  {
58  return IsReturn || IsRaise || IsTrap;
59  }
60 
62  DAGBlock(unsigned index) : Index(index) {}
63 };
64 
68 llvm::raw_ostream &operator<<(llvm::raw_ostream &os, DAGBlock &node);
69 
70 
71 
75 class DAGFunc {
76 public:
77  using NodeList = std::vector<std::unique_ptr<DAGBlock>>;
78 
79  template <typename T>
80  struct iterator : llvm::iterator_adaptor_base
81  < iterator<T>
82  , T
83  , std::random_access_iterator_tag
84  , DAGBlock *
85  >
86  {
87  iterator<T>(T it)
88  : llvm::iterator_adaptor_base
89  < iterator<T>
90  , T
91  , std::random_access_iterator_tag
92  , DAGBlock *
93  >(it)
94  {
95  }
96 
97  DAGBlock *operator*() const { return this->I->get(); }
98  DAGBlock *operator->() const { return operator*(); }
99  };
100 
103 
104 public:
105  DAGFunc(Func &func);
106 
107  DAGBlock *operator[] (Block *block) { return blocks_[block]; }
108  DAGBlock *operator[] (unsigned idx) { return &*nodes_[idx]; }
109 
110  Func &GetFunc() { return func_; }
111 
112  // Iterator over nodes.
113  size_t size() const { return nodes_.size(); }
114  node_iterator begin() { return node_iterator(nodes_.begin()); }
115  node_iterator end() { return node_iterator(nodes_.end()); }
116  reverse_node_iterator rbegin() { return reverse_node_iterator(nodes_.rbegin()); }
117  reverse_node_iterator rend() { return reverse_node_iterator(nodes_.rend()); }
118 
119 private:
121  Func &func_;
123  NodeList nodes_;
125  std::unordered_map<Block *, DAGBlock *> blocks_;
126 };
Func
Definition: func.h:30
DAGBlock::IsExit
bool IsExit() const
Checks whether the node is an exit.
Definition: dag.h:56
DAGBlock::Raises
bool Raises
Flag to indicate whether the node raises.
Definition: dag.h:41
DAGBlock::IsLoop
bool IsLoop
Flag indicating whether this is a loop to be over-approximated.
Definition: dag.h:33
DAGBlock::Blocks
llvm::DenseSet< Block * > Blocks
Blocks which are part of the collapsed node.
Definition: dag.h:25
DAGBlock::IsRaise
bool IsRaise
Flag to indicate whether the node is a raise.
Definition: dag.h:43
DAGBlock::Traps
bool Traps
Node leads to a trap.
Definition: dag.h:45
DAGFunc::iterator
Definition: dag.h:80
DAGFunc
Definition: dag.h:75
DAGBlock::Length
int64_t Length
Length of the longest path to an exit.
Definition: dag.h:31
DAGBlock::Succs
llvm::SmallVector< DAGBlock *, 4 > Succs
Set of successor nodes.
Definition: dag.h:27
DAGBlock::Returns
bool Returns
Flag to indicate whether the node is on a path to return.
Definition: dag.h:37
DAGBlock::Preds
llvm::SmallVector< DAGBlock *, 4 > Preds
Set of predecessor nodes.
Definition: dag.h:29
DAGBlock::Exits
bool Exits() const
Checks whether the node exits.
Definition: dag.h:50
DAGBlock::IsTrap
bool IsTrap
Node traps.
Definition: dag.h:47
DAGBlock::Index
const unsigned Index
Index of the DAG block (higher is closer to entry).
Definition: dag.h:22
DAGBlock::IsReturn
bool IsReturn
Flag to indicate whether the node is a return.
Definition: dag.h:39
Block
Definition: block.h:29
DAGBlock
Definition: dag.h:20
DAGBlock::Lands
bool Lands
Flag to indicate whether the node has landing pads.
Definition: dag.h:35
DAGBlock::DAGBlock
DAGBlock(unsigned index)
Creates a DAG block.
Definition: dag.h:62