Jlm
Loading...
Searching...
No Matches
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
12namespace jlm::rvsdg
13{
14
16
17bool
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
24std::string
26{
27 return jlm::util::strfmt("SLICE[", low(), ":", high(), ")");
28}
29
30std::optional<std::vector<Output *>>
32 const BitSliceOperation & operation,
33 const std::vector<Output *> & operands)
34{
35 JLM_ASSERT(operands.size() == 1);
36 const auto operand = operands[0];
37
38 const auto bitType = util::assertedCast<const BitType>(operand->Type().get());
39 if (operation.low() == 0 && operation.high() == bitType->nbits())
40 {
41 return std::vector{ operand };
42 }
43
44 return std::nullopt;
45}
46
47std::optional<std::vector<Output *>>
49 const BitSliceOperation & operation,
50 const std::vector<Output *> & operands)
51{
52 JLM_ASSERT(operands.size() == 1);
53 const auto operand = operands[0];
54
55 auto [_, constantOperation] =
58 {
59 const auto slicedValue = constantOperation->value().slice(operation.low(), operation.high());
60 return std::vector{ &BitConstantOperation::create(*operand->region(), slicedValue) };
61 }
62
63 return std::nullopt;
64}
65
66std::optional<std::vector<Output *>>
68 const BitSliceOperation & operation,
69 const std::vector<Output *> & operands)
70{
71 JLM_ASSERT(operands.size() == 1);
72 const auto operand = operands[0];
73
77 {
78 const auto newLow = operation.low() + sliceOperation->low();
79 const auto newHigh = operation.high() + sliceOperation->low();
80 return std::vector{ bitslice(sliceNode->input(0)->origin(), newLow, newHigh) };
81 }
82
83 return std::nullopt;
84}
85
86std::optional<std::vector<Output *>>
88 const BitSliceOperation & operation,
89 const std::vector<Output *> & operands)
90{
91 JLM_ASSERT(operands.size() == 1);
92 const auto operand = operands[0];
93 const auto low = operation.low();
94 const auto high = operation.high();
95
99 {
100 size_t pos = 0, n = 0;
101 std::vector<Output *> newOperands;
102 for (n = 0; n < bitConcatNode->ninputs(); n++)
103 {
104 auto argument = bitConcatNode->input(n)->origin();
105 const auto base = pos;
106 const auto numBits = std::static_pointer_cast<const BitType>(argument->Type())->nbits();
107 pos = pos + numBits;
109 {
110 const auto slice_low = (low > base) ? (low - base) : 0;
111 const auto slice_high = (high < pos) ? (high - base) : (pos - base);
113 newOperands.push_back(argument);
114 }
115 }
116
117 return std::vector{ bitconcat(newOperands) };
118 }
119
120 return std::nullopt;
121}
122
123std::unique_ptr<Operation>
125{
126 return std::make_unique<BitSliceOperation>(*this);
127}
128
130bitslice(jlm::rvsdg::Output * argument, size_t low, size_t high)
131{
133 { argument },
134 std::dynamic_pointer_cast<const jlm::rvsdg::BitType>(argument->Type()),
135 low,
136 high)
137 .output(0);
138}
139
140}
static Output & create(Region &region, BitValueRepresentation value)
Definition constant.hpp:44
static std::optional< std::vector< Output * > > normalizeIdempotent(const BitSliceOperation &operation, const std::vector< Output * > &operands)
Definition slice.cpp:31
static std::optional< std::vector< Output * > > narrowSlice(const BitSliceOperation &operation, const std::vector< Output * > &operands)
Definition slice.cpp:67
static std::optional< std::vector< Output * > > foldConstant(const BitSliceOperation &operation, const std::vector< Output * > &operands)
Definition slice.cpp:48
std::unique_ptr< Operation > copy() const override
Definition slice.cpp:124
static std::optional< std::vector< Output * > > distributeSlice(const BitSliceOperation &operation, const std::vector< Output * > &operands)
Definition slice.cpp:87
~BitSliceOperation() noexcept override
std::string debug_string() const override
Definition slice.cpp:25
size_t high() const noexcept
Definition slice.hpp:43
size_t low() const noexcept
Definition slice.hpp:37
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
#define JLM_ASSERT(x)
Definition common.hpp:16
static std::vector< jlm::rvsdg::Output * > operands(const Node *node)
Definition node.hpp:1049
jlm::rvsdg::Output * bitconcat(const std::vector< jlm::rvsdg::Output * > &operands)
Definition concat.cpp:16
NodeType * TryGetOwnerNode(const rvsdg::Input &input) noexcept
Checks if this is an input to a node of specified type.
Definition node.hpp:872
jlm::rvsdg::Output * bitslice(jlm::rvsdg::Output *argument, size_t low, size_t high)
Create bitslice.
Definition slice.cpp:130
static std::string strfmt(Args... args)
Definition strfmt.hpp:35