Jlm
Phi.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2012 2013 2014 2015 Nico Reißmann <nico.reissmann@gmail.com>
3  * Copyright 2012 2013 2014 2025 Helge Bahmann <hcb@chaoticmind.net>
4  * See COPYING for terms of redistribution.
5  */
6 
7 #ifndef JLM_RVSDG_PHI_HPP
8 #define JLM_RVSDG_PHI_HPP
9 
10 #include <jlm/rvsdg/graph.hpp>
11 #include <jlm/rvsdg/lambda.hpp>
12 #include <jlm/rvsdg/node.hpp>
13 #include <jlm/rvsdg/region.hpp>
15 #include <jlm/util/common.hpp>
16 
17 namespace jlm::rvsdg
18 {
19 
21 {
22 public:
23  ~PhiOperation() override;
24 
25  [[nodiscard]] std::string
26  debug_string() const override;
27 
28  [[nodiscard]] std::unique_ptr<Operation>
29  copy() const override;
30 };
31 
32 class PhiBuilder;
33 
45 class PhiNode final : public rvsdg::StructuralNode
46 {
47  friend class PhiBuilder;
48 
49 public:
50  ~PhiNode() override;
51 
52 private:
53  explicit PhiNode(rvsdg::Region * parent)
54  : StructuralNode(parent, 1)
55  {}
56 
57  static PhiNode *
59  {
60  return new PhiNode(parent);
61  }
62 
63 public:
72  struct ContextVar
73  {
81 
90  };
91 
99  struct FixVar
100  {
111 
119 
127  };
128 
129  [[nodiscard]] const PhiOperation &
130  GetOperation() const noexcept override;
131 
146  ContextVar
147  AddContextVar(jlm::rvsdg::Output & origin);
148 
161  void
162  RemoveContextVars(std::vector<ContextVar> vars);
163 
172  [[nodiscard]] std::vector<ContextVar>
173  GetContextVars() const noexcept;
174 
193  [[nodiscard]] ContextVar
194  MapInputContextVar(const rvsdg::Input & input) const noexcept;
195 
214  [[nodiscard]] std::optional<ContextVar>
215  MapArgumentContextVar(const rvsdg::Output & argument) const noexcept;
216 
229  void
230  RemoveFixVars(std::vector<FixVar> vars);
231 
240  [[nodiscard]] std::vector<FixVar>
241  GetFixVars() const noexcept;
242 
262  [[nodiscard]] std::optional<FixVar>
263  MapArgumentFixVar(const rvsdg::Output & argument) const noexcept;
264 
280  [[nodiscard]] FixVar
281  MapResultFixVar(const rvsdg::Input & result) const noexcept;
282 
297  [[nodiscard]] FixVar
298  MapOutputFixVar(const rvsdg::Output & output) const noexcept;
299 
316  [[nodiscard]] std::variant<FixVar, ContextVar>
317  MapArgument(const rvsdg::Output & argument) const noexcept;
318 
319  rvsdg::Region *
320  subregion() const noexcept
321  {
322  return StructuralNode::subregion(0);
323  }
324 
325  PhiNode *
326  copy(rvsdg::Region * region, rvsdg::SubstitutionMap & smap) const override;
327 
336  static std::vector<rvsdg::LambdaNode *>
337  ExtractLambdaNodes(const PhiNode & phiNode);
338 };
339 
341 class PhiBuilder final
342 {
343 public:
344  constexpr PhiBuilder() noexcept
345  : node_(nullptr)
346  {}
347 
348  rvsdg::Region *
349  subregion() const noexcept
350  {
351  return node_ ? node_->subregion() : nullptr;
352  }
353 
354  void
356  {
357  if (node_)
358  return;
359 
360  node_ = PhiNode::create(parent);
361  }
362 
365 
367  AddFixVar(std::shared_ptr<const jlm::rvsdg::Type> type);
368 
369  PhiNode *
370  end();
371 
372 private:
374 };
375 
376 }
377 
378 #endif
rvsdg::Region * region() const noexcept
Definition: node.hpp:761
constexpr PhiBuilder() noexcept
Definition: Phi.hpp:344
rvsdg::Region * subregion() const noexcept
Definition: Phi.hpp:349
PhiNode * end()
Definition: Phi.cpp:270
PhiNode::ContextVar AddContextVar(jlm::rvsdg::Output &origin)
Definition: Phi.cpp:251
PhiNode::FixVar AddFixVar(std::shared_ptr< const jlm::rvsdg::Type > type)
Definition: Phi.cpp:257
void begin(rvsdg::Region *parent)
Definition: Phi.hpp:355
PhiNode * node_
Definition: Phi.hpp:373
A phi node represents the fixpoint of mutually recursive definitions.
Definition: Phi.hpp:46
PhiNode(rvsdg::Region *parent)
Definition: Phi.hpp:53
rvsdg::Region * subregion() const noexcept
Definition: Phi.hpp:320
FixVar MapResultFixVar(const rvsdg::Input &result) const noexcept
Maps region result to fixpoint variable.
Definition: Phi.cpp:91
PhiNode * copy(rvsdg::Region *region, rvsdg::SubstitutionMap &smap) const override
Copy a node with substitutions.
Definition: Phi.cpp:191
ContextVar MapInputContextVar(const rvsdg::Input &input) const noexcept
Maps input to context variable.
Definition: Phi.cpp:109
std::optional< ContextVar > MapArgumentContextVar(const rvsdg::Output &argument) const noexcept
Attempts to map bound variable reference to context variable.
Definition: Phi.cpp:117
static std::vector< rvsdg::LambdaNode * > ExtractLambdaNodes(const PhiNode &phiNode)
Definition: Phi.cpp:226
std::vector< FixVar > GetFixVars() const noexcept
Gets all fixpoint variables.
Definition: Phi.cpp:63
void RemoveContextVars(std::vector< ContextVar > vars)
Removes context variables from phi node.
Definition: Phi.cpp:150
static PhiNode * create(rvsdg::Region *parent)
Definition: Phi.hpp:58
FixVar MapOutputFixVar(const rvsdg::Output &output) const noexcept
Maps output to fixpoint variable.
Definition: Phi.cpp:100
std::vector< ContextVar > GetContextVars() const noexcept
Gets all bound context variables.
Definition: Phi.cpp:50
std::optional< FixVar > MapArgumentFixVar(const rvsdg::Output &argument) const noexcept
Tries to map region argument to fixpoint variable.
Definition: Phi.cpp:76
std::variant< FixVar, ContextVar > MapArgument(const rvsdg::Output &argument) const noexcept
Maps region argument to its function.
Definition: Phi.cpp:132
ContextVar AddContextVar(jlm::rvsdg::Output &origin)
Adds a context variable to the phi node.
Definition: Phi.cpp:40
void RemoveFixVars(std::vector< FixVar > vars)
Removes fixpoint variables from the phi node.
Definition: Phi.cpp:168
const PhiOperation & GetOperation() const noexcept override
Definition: Phi.cpp:32
std::string debug_string() const override
Definition: Phi.cpp:18
std::unique_ptr< Operation > copy() const override
Definition: Phi.cpp:24
Represent acyclic RVSDG subgraphs.
Definition: region.hpp:213
StructuralOutput * output(size_t index) const noexcept
StructuralInput * input(size_t index) const noexcept
rvsdg::Region * subregion(size_t index) const noexcept
static std::string type(const Node *n)
Definition: view.cpp:255
Bound context variable.
Definition: Phi.hpp:73
rvsdg::Input * input
Input variable bound into the phi construct.
Definition: Phi.hpp:80
rvsdg::Output * inner
Access to bound object in subregion.
Definition: Phi.hpp:89
Description of a recursively defined variable.
Definition: Phi.hpp:100
rvsdg::Output * recref
Reference to mutual-recursively defined object in phi.
Definition: Phi.hpp:110
rvsdg::Input * result
Definition result of a variable within the phi region.
Definition: Phi.hpp:118
rvsdg::Output * output
Output of phi region representing externally available definition.
Definition: Phi.hpp:126