Jlm
cfg-structure.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2017 Nico Reißmann <nico.reissmann@gmail.com>
3  * See COPYING for terms of redistribution.
4  */
5 
6 #ifndef JLM_LLVM_IR_CFG_STRUCTURE_HPP
7 #define JLM_LLVM_IR_CFG_STRUCTURE_HPP
8 
9 #include <jlm/util/HashSet.hpp>
12 
13 #include <memory>
14 #include <unordered_set>
15 #include <vector>
16 
17 namespace jlm::llvm
18 {
19 
20 class ControlFlowGraph;
21 class ControlFlowGraphEdge;
22 class ControlFlowGraphNode;
23 
27 {
28  using constiterator = util::
29  PtrIterator<ControlFlowGraphNode, std::unordered_set<ControlFlowGraphNode *>::const_iterator>;
30 
31 public:
32  explicit StronglyConnectedComponent(const std::unordered_set<ControlFlowGraphNode *> & nodes)
33  : nodes_(nodes)
34  {}
35 
37  begin() const;
38 
40  end() const;
41 
42  bool
44  {
45  return nodes_.find(node) != nodes_.end();
46  }
47 
48  size_t
49  nnodes() const noexcept
50  {
51  return nodes_.size();
52  }
53 
54 private:
55  std::unordered_set<ControlFlowGraphNode *> nodes_{};
56 };
57 
70 {
73 
76 
77 public:
78  size_t
79  NumEntryNodes() const noexcept
80  {
81  return EntryNodes_.Size();
82  }
83 
84  size_t
85  NumExitNodes() const noexcept
86  {
87  return ExitNodes_.Size();
88  }
89 
90  size_t
91  NumEntryEdges() const noexcept
92  {
93  return EntryEdges_.Size();
94  }
95 
96  size_t
97  NumRepetitionEdges() const noexcept
98  {
99  return RepetitionEdges_.Size();
100  }
101 
102  size_t
103  NumExitEdges() const noexcept
104  {
105  return ExitEdges_.Size();
106  }
107 
109  EntryNodes() const
110  {
111  return { EntryNodes_.Items().begin(), EntryNodes_.Items().end() };
112  }
113 
115  ExitNodes() const
116  {
117  return { ExitNodes_.Items().begin(), ExitNodes_.Items().end() };
118  }
119 
121  EntryEdges() const
122  {
123  return { EntryEdges_.Items().begin(), EntryEdges_.Items().end() };
124  }
125 
128  {
129  return { RepetitionEdges_.Items().begin(), RepetitionEdges_.Items().end() };
130  }
131 
133  ExitEdges() const
134  {
135  return { ExitEdges_.Items().begin(), ExitEdges_.Items().end() };
136  }
137 
141  static std::unique_ptr<StronglyConnectedComponentStructure>
142  Create(const StronglyConnectedComponent & scc);
143 
149  bool
150  IsTailControlledLoop() const noexcept;
151 
152 private:
153  util::HashSet<ControlFlowGraphNode *> EntryNodes_{};
158 };
159 
160 bool
161 is_valid(const ControlFlowGraph & cfg);
162 
163 bool
164 is_closed(const ControlFlowGraph & cfg);
165 
166 bool
167 is_linear(const ControlFlowGraph & cfg);
168 
172 std::vector<StronglyConnectedComponent>
173 find_sccs(const ControlFlowGraph & cfg);
174 
179 std::vector<StronglyConnectedComponent>
180 find_sccs(ControlFlowGraphNode * entry, ControlFlowGraphNode * exit);
181 
182 static inline bool
184 {
185  auto sccs = find_sccs(cfg);
186  return sccs.size() == 0;
187 }
188 
189 bool
190 is_structured(const ControlFlowGraph & cfg);
191 
192 bool
193 is_proper_structured(const ControlFlowGraph & cfg);
194 
195 bool
196 is_reducible(const ControlFlowGraph & cfg);
197 
209 void
210 straighten(ControlFlowGraph & cfg);
211 
214 void
215 purge(ControlFlowGraph & cfg);
216 
221 void
222 prune(ControlFlowGraph & cfg);
223 
224 }
225 
226 #endif
Strongly Connected Component Structure.
util::IteratorRange< CfgEdgeConstIterator > EdgeIteratorRange
util::HashSet< ControlFlowGraphEdge * > EntryEdges_
util::HashSet< ControlFlowGraphEdge * > ExitEdges_
util::HashSet< ControlFlowGraphNode * > EntryNodes_
static std::unique_ptr< StronglyConnectedComponentStructure > Create(const StronglyConnectedComponent &scc)
util::IteratorRange< CfgNodeConstIterator > NodeIteratorRange
util::HashSet< ControlFlowGraphEdge * > RepetitionEdges_
util::HashSet< ControlFlowGraphNode * > ExitNodes_
Strongly Connected Component.
std::unordered_set< ControlFlowGraphNode * > nodes_
StronglyConnectedComponent(const std::unordered_set< ControlFlowGraphNode * > &nodes)
bool contains(ControlFlowGraphNode *node) const
util::PtrIterator< ControlFlowGraphNode, std::unordered_set< ControlFlowGraphNode * >::const_iterator > constiterator
Global memory state passed between functions.
std::vector< StronglyConnectedComponent > find_sccs(const ControlFlowGraph &cfg)
bool is_structured(const ControlFlowGraph &cfg)
static bool is_linear(const ControlFlowGraphNode *node) noexcept
bool is_reducible(const ControlFlowGraph &cfg)
bool is_valid(const ControlFlowGraph &cfg)
void straighten(ControlFlowGraph &cfg)
bool is_proper_structured(const ControlFlowGraph &cfg)
void purge(ControlFlowGraph &cfg)
Remove all basic blocks without instructions.
bool is_closed(const ControlFlowGraph &cfg)
static bool is_acyclic(const ControlFlowGraph &cfg)
void prune(ControlFlowGraph &cfg)