Jlm
FunctionPointer.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2024 Helge Bahmann <hcb@chaoticmind.net>
3  * See COPYING for terms of redistribution.
4  */
5 
8 
9 namespace jlm::llvm
10 {
11 
13 {}
14 
16  std::shared_ptr<const rvsdg::FunctionType> fn)
17  : UnaryOperation(fn, PointerType::Create()),
18  FunctionType_(std::move(fn))
19 {}
20 
21 bool
22 FunctionToPointerOperation::operator==(const Operation & other) const noexcept
23 {
24  if (auto o = dynamic_cast<const FunctionToPointerOperation *>(&other))
25  {
26  return *FunctionType() == *o->FunctionType();
27  }
28  else
29  {
30  return false;
31  }
32 }
33 
34 [[nodiscard]] std::string
36 {
37  return "FunPtr(" + FunctionType()->debug_string() + ")";
38 }
39 
40 [[nodiscard]] std::unique_ptr<rvsdg::Operation>
42 {
43  return Create(FunctionType());
44 }
45 
48 {
49  if (auto node = rvsdg::TryGetOwnerNode<rvsdg::SimpleNode>(*arg))
50  {
51  if (auto op = dynamic_cast<const PointerToFunctionOperation *>(&node->GetOperation()))
52  {
53  if (*op->FunctionType() == *FunctionType())
54  {
56  }
57  }
58  }
60 }
61 
65  jlm::rvsdg::Output * arg) const
66 {
67  if (auto node = rvsdg::TryGetOwnerNode<rvsdg::SimpleNode>(*arg))
68  {
69  if (auto op = dynamic_cast<const PointerToFunctionOperation *>(&node->GetOperation()))
70  {
71  if (*op->FunctionType() == *FunctionType() && path == rvsdg::unop_reduction_inverse)
72  {
73  return node->input(0)->origin();
74  }
75  }
76  }
77  return arg;
78 }
79 
80 std::unique_ptr<FunctionToPointerOperation>
81 FunctionToPointerOperation::Create(std::shared_ptr<const rvsdg::FunctionType> fn)
82 {
83  return std::make_unique<FunctionToPointerOperation>(std::move(fn));
84 }
85 
87 {}
88 
90  std::shared_ptr<const rvsdg::FunctionType> fn)
91  : UnaryOperation(PointerType::Create(), fn),
92  FunctionType_(std::move(fn))
93 {}
94 
95 bool
96 PointerToFunctionOperation::operator==(const Operation & other) const noexcept
97 {
98  if (auto o = dynamic_cast<const PointerToFunctionOperation *>(&other))
99  {
100  return *FunctionType() == *o->FunctionType();
101  }
102  else
103  {
104  return false;
105  }
106 }
107 
108 [[nodiscard]] std::string
110 {
111  return "PtrFun(" + FunctionType()->debug_string() + ")";
112 }
113 
114 [[nodiscard]] std::unique_ptr<rvsdg::Operation>
116 {
117  return Create(FunctionType());
118 }
119 
122 {
123  if (auto node = rvsdg::TryGetOwnerNode<rvsdg::SimpleNode>(*arg))
124  {
125  if (auto op = dynamic_cast<const FunctionToPointerOperation *>(&node->GetOperation()))
126  {
127  if (*op->FunctionType() == *FunctionType())
128  {
130  }
131  }
132  }
134 }
135 
139  jlm::rvsdg::Output * arg) const
140 {
141  if (auto node = rvsdg::TryGetOwnerNode<rvsdg::SimpleNode>(*arg))
142  {
143  if (auto op = dynamic_cast<const FunctionToPointerOperation *>(&node->GetOperation()))
144  {
145  if (*op->FunctionType() == *FunctionType() && path == rvsdg::unop_reduction_inverse)
146  {
147  return node->input(0)->origin();
148  }
149  }
150  }
151  return arg;
152 }
153 
154 std::unique_ptr<PointerToFunctionOperation>
155 PointerToFunctionOperation::Create(std::shared_ptr<const rvsdg::FunctionType> fn)
156 {
157  return std::make_unique<PointerToFunctionOperation>(std::move(fn));
158 }
159 
160 }
Get address of compiled function object.
std::unique_ptr< Operation > copy() const override
std::string debug_string() const override
bool operator==(const Operation &other) const noexcept override
static std::unique_ptr< FunctionToPointerOperation > Create(std::shared_ptr< const rvsdg::FunctionType > fn)
FunctionToPointerOperation(std::shared_ptr< const rvsdg::FunctionType > fn)
~FunctionToPointerOperation() noexcept override
rvsdg::unop_reduction_path_t can_reduce_operand(const jlm::rvsdg::Output *arg) const noexcept override
jlm::rvsdg::Output * reduce_operand(rvsdg::unop_reduction_path_t path, jlm::rvsdg::Output *arg) const override
const std::shared_ptr< const jlm::rvsdg::FunctionType > & FunctionType() const noexcept
Interpret pointer as callable function.
std::string debug_string() const override
PointerToFunctionOperation(std::shared_ptr< const rvsdg::FunctionType > fn)
std::unique_ptr< rvsdg::Operation > copy() const override
const std::shared_ptr< const rvsdg::FunctionType > & FunctionType() const noexcept
bool operator==(const Operation &other) const noexcept override
jlm::rvsdg::Output * reduce_operand(rvsdg::unop_reduction_path_t path, jlm::rvsdg::Output *arg) const override
rvsdg::unop_reduction_path_t can_reduce_operand(const jlm::rvsdg::Output *arg) const noexcept override
~PointerToFunctionOperation() noexcept override
static std::unique_ptr< PointerToFunctionOperation > Create(std::shared_ptr< const rvsdg::FunctionType > fn)
PointerType class.
Definition: types.hpp:25
Global memory state passed between functions.
size_t unop_reduction_path_t
Definition: unary.hpp:18
static const unop_reduction_path_t unop_reduction_inverse
Definition: unary.hpp:49
static const unop_reduction_path_t unop_reduction_none
Definition: unary.hpp:43