Jlm
slice.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2010 2011 2012 2014 Helge Bahmann <hcb@chaoticmind.net>
3  * Copyright 2011 2012 2013 2014 2015 Nico Reißmann <nico.reissmann@gmail.com>
4  * See COPYING for terms of redistribution.
5  */
6 
11 
12 namespace jlm::rvsdg
13 {
14 
15 BitSliceOperation::~BitSliceOperation() noexcept = default;
16 
17 bool
18 BitSliceOperation::operator==(const Operation & other) const noexcept
19 {
20  auto op = dynamic_cast<const BitSliceOperation *>(&other);
21  return op && op->low() == low() && op->high() == high() && op->argument(0) == argument(0);
22 }
23 
24 std::string
26 {
27  return jlm::util::strfmt("SLICE[", low(), ":", high(), ")");
28 }
29 
32 {
33  auto node = TryGetOwnerNode<SimpleNode>(*arg);
34  auto & arg_type = *std::dynamic_pointer_cast<const BitType>(arg->Type());
35 
36  if ((low() == 0) && (high() == arg_type.nbits()))
38 
39  if (is<BitSliceOperation>(node->GetOperation()))
40  return unop_reduction_narrow;
41 
42  if (is<BitConstantOperation>(node->GetOperation()))
44 
45  if (is<BitConcatOperation>(node->GetOperation()))
47 
48  return unop_reduction_none;
49 }
50 
53 {
54  if (path == unop_reduction_idempotent)
55  {
56  return arg;
57  }
58 
59  auto & node = AssertGetOwnerNode<SimpleNode>(*arg);
60 
61  if (path == unop_reduction_narrow)
62  {
63  auto op = static_cast<const BitSliceOperation &>(node.GetOperation());
64  return jlm::rvsdg::bitslice(node.input(0)->origin(), low() + op.low(), high() + op.low());
65  }
66 
67  if (path == unop_reduction_constant)
68  {
69  auto op = static_cast<const BitConstantOperation &>(node.GetOperation());
70  std::string s(&op.value()[0] + low(), high() - low());
71  return &BitConstantOperation::create(*arg->region(), BitValueRepresentation(s.c_str()));
72  }
73 
74  if (path == unop_reduction_distribute)
75  {
76  size_t pos = 0, n = 0;
77  std::vector<jlm::rvsdg::Output *> arguments;
78  for (n = 0; n < node.ninputs(); n++)
79  {
80  auto argument = node.input(n)->origin();
81  size_t base = pos;
82  size_t nbits = std::static_pointer_cast<const BitType>(argument->Type())->nbits();
83  pos = pos + nbits;
84  if (base < high() && pos > low())
85  {
86  size_t slice_low = (low() > base) ? (low() - base) : 0;
87  size_t slice_high = (high() < pos) ? (high() - base) : (pos - base);
88  argument = jlm::rvsdg::bitslice(argument, slice_low, slice_high);
89  arguments.push_back(argument);
90  }
91  }
92 
93  return jlm::rvsdg::bitconcat(arguments);
94  }
95 
96  return nullptr;
97 }
98 
99 std::unique_ptr<Operation>
101 {
102  return std::make_unique<BitSliceOperation>(*this);
103 }
104 
106 bitslice(jlm::rvsdg::Output * argument, size_t low, size_t high)
107 {
108  return CreateOpNode<BitSliceOperation>(
109  { argument },
110  std::dynamic_pointer_cast<const jlm::rvsdg::BitType>(argument->Type()),
111  low,
112  high)
113  .output(0);
114 }
115 
116 }
static Output & create(Region &region, BitValueRepresentation value)
Definition: constant.hpp:44
unop_reduction_path_t can_reduce_operand(const jlm::rvsdg::Output *arg) const noexcept override
Definition: slice.cpp:31
std::unique_ptr< Operation > copy() const override
Definition: slice.cpp:100
~BitSliceOperation() noexcept override
jlm::rvsdg::Output * reduce_operand(unop_reduction_path_t path, jlm::rvsdg::Output *arg) const override
Definition: slice.cpp:52
std::string debug_string() const override
Definition: slice.cpp:25
size_t high() const noexcept
Definition: slice.hpp:49
size_t low() const noexcept
Definition: slice.hpp:43
rvsdg::Region * region() const noexcept
Definition: node.cpp:151
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
size_t unop_reduction_path_t
Definition: unary.hpp:18
jlm::rvsdg::Output * bitconcat(const std::vector< jlm::rvsdg::Output * > &operands)
Definition: concat.cpp:16
static const unop_reduction_path_t unop_reduction_idempotent
Definition: unary.hpp:47
static const unop_reduction_path_t unop_reduction_constant
Definition: unary.hpp:45
static const unop_reduction_path_t unop_reduction_narrow
Definition: unary.hpp:51
static const unop_reduction_path_t unop_reduction_none
Definition: unary.hpp:43
static const unop_reduction_path_t unop_reduction_distribute
Definition: unary.hpp:53
jlm::rvsdg::Output * bitslice(jlm::rvsdg::Output *argument, size_t low, size_t high)
Create bitslice.
Definition: slice.cpp:106
static std::string strfmt(Args... args)
Definition: strfmt.hpp:35