Jlm
sext.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_SEXT_HPP
7 #define JLM_LLVM_IR_OPERATORS_SEXT_HPP
8 
9 #include <jlm/llvm/ir/tac.hpp>
10 #include <jlm/rvsdg/bitstring.hpp>
11 #include <jlm/rvsdg/unary.hpp>
12 
13 namespace jlm::llvm
14 {
15 
17 {
18 public:
19  ~SExtOperation() noexcept override;
20 
22  std::shared_ptr<const rvsdg::BitType> otype,
23  std::shared_ptr<const rvsdg::BitType> rtype)
24  : UnaryOperation(otype, rtype)
25  {
26  if (otype->nbits() >= rtype->nbits())
27  throw util::Error("expected operand's #bits to be smaller than results's #bits.");
28  }
29 
30  inline SExtOperation(
31  std::shared_ptr<const rvsdg::Type> srctype,
32  std::shared_ptr<const rvsdg::Type> dsttype)
33  : UnaryOperation(srctype, dsttype)
34  {
35  auto ot = std::dynamic_pointer_cast<const rvsdg::BitType>(srctype);
36  if (!ot)
37  throw util::Error("expected bits type.");
38 
39  auto rt = std::dynamic_pointer_cast<const rvsdg::BitType>(dsttype);
40  if (!rt)
41  throw util::Error("expected bits type.");
42 
43  if (ot->nbits() >= rt->nbits())
44  throw util::Error("expected operand's #bits to be smaller than results' #bits.");
45  }
46 
47  bool
48  operator==(const Operation & other) const noexcept override;
49 
50  [[nodiscard]] std::string
51  debug_string() const override;
52 
53  [[nodiscard]] std::unique_ptr<Operation>
54  copy() const override;
55 
57  can_reduce_operand(const rvsdg::Output * operand) const noexcept override;
58 
60  reduce_operand(rvsdg::unop_reduction_path_t path, rvsdg::Output * operand) const override;
61 
62  inline size_t
63  nsrcbits() const noexcept
64  {
65  return std::static_pointer_cast<const rvsdg::BitType>(argument(0))->nbits();
66  }
67 
68  inline size_t
69  ndstbits() const noexcept
70  {
71  return std::static_pointer_cast<const rvsdg::BitType>(result(0))->nbits();
72  }
73 
74  static std::unique_ptr<llvm::ThreeAddressCode>
75  create(const Variable * operand, const std::shared_ptr<const rvsdg::Type> & type)
76  {
77  auto ot = std::dynamic_pointer_cast<const rvsdg::BitType>(operand->Type());
78  if (!ot)
79  throw util::Error("expected bits type.");
80 
81  auto rt = std::dynamic_pointer_cast<const rvsdg::BitType>(type);
82  if (!rt)
83  throw util::Error("expected bits type.");
84 
85  auto op = std::make_unique<SExtOperation>(std::move(ot), std::move(rt));
86  return ThreeAddressCode::create(std::move(op), { operand });
87  }
88 
89  static rvsdg::Output *
90  create(size_t ndstbits, rvsdg::Output * operand)
91  {
92  auto ot = std::dynamic_pointer_cast<const rvsdg::BitType>(operand->Type());
93  if (!ot)
94  throw util::Error("expected bits type.");
95 
96  return rvsdg::CreateOpNode<SExtOperation>(
97  { operand },
98  std::move(ot),
100  .output(0);
101  }
102 };
103 
104 }
105 
106 #endif
static std::unique_ptr< llvm::ThreeAddressCode > create(const Variable *operand, const std::shared_ptr< const rvsdg::Type > &type)
Definition: sext.hpp:75
~SExtOperation() noexcept override
static rvsdg::Output * create(size_t ndstbits, rvsdg::Output *operand)
Definition: sext.hpp:90
std::string debug_string() const override
Definition: sext.cpp:87
rvsdg::unop_reduction_path_t can_reduce_operand(const rvsdg::Output *operand) const noexcept override
Definition: sext.cpp:99
std::unique_ptr< Operation > copy() const override
Definition: sext.cpp:93
size_t nsrcbits() const noexcept
Definition: sext.hpp:63
rvsdg::Output * reduce_operand(rvsdg::unop_reduction_path_t path, rvsdg::Output *operand) const override
Definition: sext.cpp:118
SExtOperation(std::shared_ptr< const rvsdg::Type > srctype, std::shared_ptr< const rvsdg::Type > dsttype)
Definition: sext.hpp:30
size_t ndstbits() const noexcept
Definition: sext.hpp:69
bool operator==(const Operation &other) const noexcept override
Definition: sext.cpp:80
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
static std::shared_ptr< const BitType > Create(std::size_t nbits)
Creates bit type of specified width.
Definition: type.cpp:45
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
const std::shared_ptr< const rvsdg::Type > & result(size_t index) const noexcept
Definition: operation.cpp:36
Unary operator.
Definition: unary.hpp:26
UnaryOperation(std::shared_ptr< const jlm::rvsdg::Type > operand, std::shared_ptr< const jlm::rvsdg::Type > result)
Definition: unary.hpp:30
Global memory state passed between functions.
size_t unop_reduction_path_t
Definition: unary.hpp:18
static std::string type(const Node *n)
Definition: view.cpp:255