Jlm
alloca.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_OPERATORS_ALLOCA_HPP
7 #define JLM_LLVM_IR_OPERATORS_ALLOCA_HPP
8 
9 #include <jlm/llvm/ir/tac.hpp>
10 #include <jlm/llvm/ir/types.hpp>
13 
14 namespace jlm::llvm
15 {
16 
18 {
19 public:
20  ~AllocaOperation() noexcept override;
21 
23  std::shared_ptr<const rvsdg::Type> allocatedType,
24  std::shared_ptr<const rvsdg::BitType> btype,
25  size_t alignment)
26  : SimpleOperation({ btype }, { { PointerType::Create() }, { MemoryStateType::Create() } }),
28  AllocatedType_(std::move(allocatedType))
29  {}
30 
31  AllocaOperation(const AllocaOperation & other) = default;
32 
33  AllocaOperation(AllocaOperation && other) noexcept = default;
34 
35  bool
36  operator==(const Operation & other) const noexcept override;
37 
38  [[nodiscard]] std::string
39  debug_string() const override;
40 
41  [[nodiscard]] std::unique_ptr<Operation>
42  copy() const override;
43 
44  inline const rvsdg::BitType &
45  size_type() const noexcept
46  {
47  return *std::static_pointer_cast<const rvsdg::BitType>(argument(0));
48  }
49 
50  [[nodiscard]] const rvsdg::Type &
51  value_type() const noexcept
52  {
53  return *AllocatedType_;
54  }
55 
56  [[nodiscard]] const std::shared_ptr<const rvsdg::Type> &
57  ValueType() const noexcept
58  {
59  return AllocatedType_;
60  }
61 
62  inline size_t
63  alignment() const noexcept
64  {
65  return alignment_;
66  }
67 
68  static rvsdg::Input &
70  {
71  JLM_ASSERT(is<AllocaOperation>(&node));
72  return *node.input(0);
73  }
74 
75  static rvsdg::Output &
77  {
78  JLM_ASSERT(is<AllocaOperation>(&node));
79  return *node.output(0);
80  }
81 
82  static rvsdg::Output &
84  {
85  JLM_ASSERT(is<AllocaOperation>(&node));
86  return *node.output(1);
87  }
88 
89  static std::unique_ptr<llvm::ThreeAddressCode>
90  create(std::shared_ptr<const rvsdg::Type> allocatedType, const Variable * size, size_t alignment)
91  {
92  auto bt = std::dynamic_pointer_cast<const rvsdg::BitType>(size->Type());
93  if (!bt)
94  throw util::Error("expected bits type.");
95 
96  auto op = std::make_unique<AllocaOperation>(std::move(allocatedType), std::move(bt), alignment);
97  return ThreeAddressCode::create(std::move(op), { size });
98  }
99 
107  static std::vector<rvsdg::Output *>
108  create(std::shared_ptr<const rvsdg::Type> allocatedType, rvsdg::Output * size, size_t alignment)
109  {
110  auto bt = std::dynamic_pointer_cast<const rvsdg::BitType>(size->Type());
111  if (!bt)
112  throw util::Error("expected bits type.");
113 
114  return outputs(&rvsdg::CreateOpNode<AllocaOperation>(
115  { size },
116  std::move(allocatedType),
117  std::move(bt),
118  alignment));
119  }
120 
121 private:
122  size_t alignment_;
123  std::shared_ptr<const rvsdg::Type> AllocatedType_;
124 };
125 
126 }
127 
128 #endif
static rvsdg::Input & getCountInput(rvsdg::Node &node)
Definition: alloca.hpp:69
AllocaOperation(AllocaOperation &&other) noexcept=default
bool operator==(const Operation &other) const noexcept override
Definition: alloca.cpp:14
std::string debug_string() const override
Definition: alloca.cpp:21
std::shared_ptr< const rvsdg::Type > AllocatedType_
Definition: alloca.hpp:123
~AllocaOperation() noexcept override
static std::unique_ptr< llvm::ThreeAddressCode > create(std::shared_ptr< const rvsdg::Type > allocatedType, const Variable *size, size_t alignment)
Definition: alloca.hpp:90
static std::vector< rvsdg::Output * > create(std::shared_ptr< const rvsdg::Type > allocatedType, rvsdg::Output *size, size_t alignment)
Definition: alloca.hpp:108
const rvsdg::BitType & size_type() const noexcept
Definition: alloca.hpp:45
static rvsdg::Output & getMemoryStateOutput(rvsdg::Node &node)
Definition: alloca.hpp:83
const std::shared_ptr< const rvsdg::Type > & ValueType() const noexcept
Definition: alloca.hpp:57
size_t alignment() const noexcept
Definition: alloca.hpp:63
static rvsdg::Output & getPointerOutput(rvsdg::Node &node)
Definition: alloca.hpp:76
AllocaOperation(const AllocaOperation &other)=default
const rvsdg::Type & value_type() const noexcept
Definition: alloca.hpp:51
std::unique_ptr< Operation > copy() const override
Definition: alloca.cpp:27
static std::shared_ptr< const MemoryStateType > Create()
Definition: types.cpp:379
static std::shared_ptr< const PointerType > Create()
Definition: types.cpp:45
static std::unique_ptr< llvm::ThreeAddressCode > create(std::unique_ptr< rvsdg::SimpleOperation > operation, const std::vector< const Variable * > &operands)
Definition: tac.hpp:135
const std::shared_ptr< const jlm::rvsdg::Type > Type() const noexcept
Definition: variable.hpp:62
NodeInput * input(size_t index) const noexcept
Definition: node.hpp:615
NodeOutput * output(size_t index) const noexcept
Definition: node.hpp:650
const std::shared_ptr< const rvsdg::Type > & Type() const noexcept
Definition: node.hpp:366
const std::shared_ptr< const rvsdg::Type > & argument(size_t index) const noexcept
Definition: operation.cpp:23
SimpleOperation(std::vector< std::shared_ptr< const jlm::rvsdg::Type >> operands, std::vector< std::shared_ptr< const jlm::rvsdg::Type >> results)
Definition: operation.hpp:61
#define JLM_ASSERT(x)
Definition: common.hpp:16
Global memory state passed between functions.
static std::vector< jlm::rvsdg::Output * > outputs(const Node *node)
Definition: node.hpp:1058