Jlm
delta.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_RVSDG_DELTA_HPP
7 #define JLM_RVSDG_DELTA_HPP
8 
9 #include <jlm/rvsdg/region.hpp>
11 
12 namespace jlm::rvsdg
13 {
14 
18 {
19 public:
20  ~DeltaOperation() noexcept override;
21 
23  std::shared_ptr<const rvsdg::Type> type,
24  bool constant,
25  std::shared_ptr<const rvsdg::Type> reftype)
27  type_(std::move(type)),
28  reftype_(std::move(reftype))
29  {}
30 
31  DeltaOperation(const DeltaOperation & other) = default;
32 
33  DeltaOperation(DeltaOperation && other) noexcept = default;
34 
36  operator=(const DeltaOperation &) = delete;
37 
39  operator=(DeltaOperation &&) = delete;
40 
41  [[nodiscard]] std::string
42  debug_string() const override;
43 
44  [[nodiscard]] std::unique_ptr<Operation>
45  copy() const override;
46 
47  [[nodiscard]] bool
48  operator==(const Operation & other) const noexcept override;
49 
50  bool
51  constant() const noexcept
52  {
53  return constant_;
54  }
55 
56  [[nodiscard]] const rvsdg::Type &
57  type() const noexcept
58  {
59  return *type_;
60  }
61 
62  [[nodiscard]] const std::shared_ptr<const rvsdg::Type> &
63  Type() const noexcept
64  {
65  return type_;
66  }
67 
68  [[nodiscard]] const std::shared_ptr<const rvsdg::Type> &
69  ReferenceType() const noexcept
70  {
71  return reftype_;
72  }
73 
92  static inline std::unique_ptr<DeltaOperation>
94  std::shared_ptr<const rvsdg::Type> type,
95  bool constant,
96  std::shared_ptr<const rvsdg::Type> reftype)
97  {
98  return std::make_unique<DeltaOperation>(std::move(type), constant, std::move(reftype));
99  }
100 
101 private:
102  bool constant_;
103  std::shared_ptr<const rvsdg::Type> type_;
104  std::shared_ptr<const rvsdg::Type> reftype_;
105 };
106 
128 class DeltaNode final : public rvsdg::StructuralNode
129 {
130 public:
131  ~DeltaNode() noexcept override;
132 
133 private:
134  DeltaNode(rvsdg::Region * parent, std::unique_ptr<DeltaOperation> op)
135  : StructuralNode(parent, 1),
136  Operation_(std::move(op))
137  {}
138 
139 public:
148  struct ContextVar
149  {
158 
167  };
168 
180  ContextVar
182 
201  [[nodiscard]] ContextVar
202  MapInputContextVar(const rvsdg::Input & input) const noexcept;
203 
219  [[nodiscard]] ContextVar
220  MapBinderContextVar(const rvsdg::Output & output) const noexcept;
221 
230  [[nodiscard]] std::vector<ContextVar>
231  GetContextVars() const noexcept;
232 
233  rvsdg::Region *
234  subregion() const noexcept
235  {
236  return StructuralNode::subregion(0);
237  }
238 
239  [[nodiscard]] const DeltaOperation &
240  GetOperation() const noexcept override;
241 
242  [[nodiscard]] const std::shared_ptr<const rvsdg::Type> &
243  Type() const noexcept
244  {
245  return GetOperation().Type();
246  }
247 
248  bool
249  constant() const noexcept
250  {
251  return GetOperation().constant();
252  }
253 
265  template<typename F>
266  size_t
267  RemoveDeltaInputsWhere(const F & match);
268 
276  size_t
278  {
279  auto match = [](const rvsdg::Input &)
280  {
281  return true;
282  };
283 
285  }
286 
287  [[nodiscard]] rvsdg::Output &
288  output() const noexcept;
289 
290  [[nodiscard]] rvsdg::Input &
291  result() const noexcept;
292 
293  DeltaNode *
294  copy(rvsdg::Region * region, const std::vector<jlm::rvsdg::Output *> & operands) const override;
295 
296  DeltaNode *
297  copy(rvsdg::Region * region, rvsdg::SubstitutionMap & smap) const override;
298 
312  static DeltaNode *
313  Create(rvsdg::Region * parent, std::unique_ptr<DeltaOperation> op)
314  {
315  return new DeltaNode(parent, std::move(op));
316  }
317 
325  rvsdg::Output &
327 
328 private:
329  std::unique_ptr<DeltaOperation> Operation_;
330 };
331 
332 template<typename F>
333 size_t
335 {
336  util::HashSet<size_t> inputIndices;
337  util::HashSet<size_t> argumentIndices;
338  for (auto [input, argument] : GetContextVars())
339  {
340  if (argument->IsDead() && match(*input))
341  {
342  inputIndices.insert(input->index());
343  argumentIndices.insert(argument->index());
344  }
345  }
346 
347  [[maybe_unused]] const auto numRemoveArguments = subregion()->RemoveArguments(argumentIndices);
348  JLM_ASSERT(numRemoveArguments == argumentIndices.Size());
349 
350  [[maybe_unused]] const auto numRemovedInputs = RemoveInputs(inputIndices);
351  JLM_ASSERT(numRemovedInputs == inputIndices.Size());
352 
353  return numRemovedInputs;
354 }
355 
356 }
357 
358 #endif
Delta node.
Definition: delta.hpp:129
ContextVar MapInputContextVar(const rvsdg::Input &input) const noexcept
Maps input to context variable.
Definition: delta.cpp:25
rvsdg::Input & result() const noexcept
Definition: delta.cpp:116
std::vector< ContextVar > GetContextVars() const noexcept
Gets all bound context variables.
Definition: delta.cpp:39
DeltaNode(rvsdg::Region *parent, std::unique_ptr< DeltaOperation > op)
Definition: delta.hpp:134
ContextVar MapBinderContextVar(const rvsdg::Output &output) const noexcept
Maps bound variable reference to context variable.
Definition: delta.cpp:32
~DeltaNode() noexcept override
rvsdg::Region * subregion() const noexcept
Definition: delta.hpp:234
size_t RemoveDeltaInputsWhere(const F &match)
Definition: delta.hpp:334
ContextVar AddContextVar(jlm::rvsdg::Output &origin)
Adds a context/free variable to the delta node.
Definition: delta.cpp:16
const DeltaOperation & GetOperation() const noexcept override
Definition: delta.cpp:71
std::unique_ptr< DeltaOperation > Operation_
Definition: delta.hpp:329
static DeltaNode * Create(rvsdg::Region *parent, std::unique_ptr< DeltaOperation > op)
Definition: delta.hpp:313
size_t PruneDeltaInputs()
Definition: delta.hpp:277
bool constant() const noexcept
Definition: delta.hpp:249
rvsdg::Output & finalize(rvsdg::Output *result)
Definition: delta.cpp:122
rvsdg::Output & output() const noexcept
Definition: delta.cpp:110
DeltaNode * copy(rvsdg::Region *region, const std::vector< jlm::rvsdg::Output * > &operands) const override
Definition: delta.cpp:77
Delta operation.
Definition: delta.hpp:18
DeltaOperation & operator=(DeltaOperation &&)=delete
~DeltaOperation() noexcept override
std::string debug_string() const override
Definition: delta.cpp:50
DeltaOperation(const DeltaOperation &other)=default
const rvsdg::Type & type() const noexcept
Definition: delta.hpp:57
static std::unique_ptr< DeltaOperation > Create(std::shared_ptr< const rvsdg::Type > type, bool constant, std::shared_ptr< const rvsdg::Type > reftype)
Creates parameterized delta operation.
Definition: delta.hpp:93
DeltaOperation & operator=(const DeltaOperation &)=delete
std::shared_ptr< const rvsdg::Type > reftype_
Definition: delta.hpp:104
DeltaOperation(DeltaOperation &&other) noexcept=default
bool constant() const noexcept
Definition: delta.hpp:51
const std::shared_ptr< const rvsdg::Type > & Type() const noexcept
Definition: delta.hpp:63
const std::shared_ptr< const rvsdg::Type > & ReferenceType() const noexcept
Definition: delta.hpp:69
std::shared_ptr< const rvsdg::Type > type_
Definition: delta.hpp:103
bool operator==(const Operation &other) const noexcept override
Definition: delta.cpp:62
std::unique_ptr< Operation > copy() const override
Definition: delta.cpp:56
size_t index() const noexcept
Definition: node.hpp:52
rvsdg::Region * region() const noexcept
Definition: node.hpp:761
size_t RemoveInputs(const util::HashSet< size_t > &indices)
Definition: node.cpp:306
Represent acyclic RVSDG subgraphs.
Definition: region.hpp:213
size_t RemoveArguments(const util::HashSet< size_t > &indices)
Definition: region.cpp:210
StructuralInput * input(size_t index) const noexcept
rvsdg::Region * subregion(size_t index) const noexcept
bool insert(ItemType item)
Definition: HashSet.hpp:210
std::size_t Size() const noexcept
Definition: HashSet.hpp:187
#define JLM_ASSERT(x)
Definition: common.hpp:16
jlm::rvsdg::Output * match(size_t nbits, const std::unordered_map< uint64_t, uint64_t > &mapping, uint64_t default_alternative, size_t nalternatives, jlm::rvsdg::Output *operand)
Definition: control.cpp:179
static std::vector< jlm::rvsdg::Output * > operands(const Node *node)
Definition: node.hpp:1049
Bound context variable.
Definition: delta.hpp:149
rvsdg::Input * input
Input variable bound into delta node.
Definition: delta.hpp:157
rvsdg::Output * inner
Access to bound object in subregion.
Definition: delta.hpp:166