25 std::vector<jlm::rvsdg::Output *>
26 reduce_operands(
const BinaryOperation & op, std::vector<jlm::rvsdg::Output *> args)
29 if (op.is_commutative())
63 std::optional<std::vector<rvsdg::Output *>>
66 const std::vector<rvsdg::Output *> &
operands)
80 auto node = TryGetOwnerNode<Node>(*operand);
84 auto simpleNode =
dynamic_cast<const SimpleNode *
>(node);
87 auto flattenedBinaryOperation =
89 return simpleNode->GetOperation() == operation
90 || (flattenedBinaryOperation
91 && flattenedBinaryOperation->bin_operation() == operation);
106 auto flattenedBinaryOperation =
107 std::make_unique<FlattenedBinaryOperation>(operation, newOperands.size());
111 std::optional<std::vector<rvsdg::Output *>>
114 const std::vector<rvsdg::Output *> &
operands)
117 auto region =
operands[0]->region();
119 auto newOperands = reduce_operands(operation,
operands);
121 if (newOperands.size() == 1)
152 std::unique_ptr<Operation>
155 std::unique_ptr<BinaryOperation> copied_op(
static_cast<BinaryOperation *
>(
op_->copy().release()));
156 return std::make_unique<FlattenedBinaryOperation>(std::move(copied_op),
narguments());
168 auto region =
operands.front()->region();
170 std::deque<jlm::rvsdg::Output *> worklist(
operands.begin(),
operands.end());
171 while (worklist.size() > 1)
173 auto op1 = worklist.front();
174 worklist.pop_front();
175 auto op2 = worklist.front();
176 worklist.pop_front();
179 worklist.push_back(output);
183 return worklist.front();
190 auto region =
operands.front()->region();
192 std::deque<jlm::rvsdg::Output *> worklist(
operands.begin(),
operands.end());
193 while (worklist.size() > 1)
195 auto op1 = worklist.front();
196 worklist.pop_front();
197 auto op2 = worklist.front();
198 worklist.pop_front();
201 worklist.push_front(output);
205 return worklist.front();
211 const std::vector<jlm::rvsdg::Output *> &
operands)
const
215 static std::unordered_map<
232 if (
auto simpleNode =
dynamic_cast<const SimpleNode *
>(node))
242 else if (
auto structnode =
dynamic_cast<const StructuralNode *
>(node))
244 for (
size_t n = 0; n < structnode->nsubregions(); n++)
249 JLM_ASSERT(!Region::ContainsOperation<FlattenedBinaryOperation>(*region,
true));
252 std::optional<std::vector<rvsdg::Output *>>
255 const std::vector<rvsdg::Output *> &
operands)
~BinaryOperation() noexcept override
bool is_associative() const noexcept
~FlattenedBinaryOperation() noexcept override
const BinaryOperation & bin_operation() const noexcept
std::string debug_string() const override
jlm::rvsdg::Output * reduce(const FlattenedBinaryOperation::reduction &reduction, const std::vector< jlm::rvsdg::Output * > &operands) const
std::unique_ptr< BinaryOperation > op_
std::unique_ptr< Operation > copy() const override
virtual std::unique_ptr< Operation > copy() const =0
void divert_users(jlm::rvsdg::Output *new_origin)
Represent acyclic RVSDG subgraphs.
NodeOutput * output(size_t index) const noexcept
static SimpleNode & Create(Region ®ion, std::unique_ptr< Operation > operation, const std::vector< rvsdg::Output * > &operands)
size_t narguments() const noexcept
Container pairwise_reduce(Container args, const Reductor &reductor)
std::vector< jlm::rvsdg::Output * > associative_flatten(std::vector< jlm::rvsdg::Output * > args, const FlattenTester &flatten_tester)
Container commutative_pairwise_reduce(Container args, const Reductor &reductor)
std::optional< std::vector< rvsdg::Output * > > NormalizeBinaryOperation(const BinaryOperation &operation, const std::vector< rvsdg::Output * > &operands)
Applies the reductions implemented in the binary operations reduction functions.
static void remove(Node *node)
static jlm::rvsdg::Output * reduce_linear(const BinaryOperation &op, const std::vector< jlm::rvsdg::Output * > &operands)
size_t binop_reduction_path_t
static jlm::rvsdg::Output * reduce_parallel(const BinaryOperation &op, const std::vector< jlm::rvsdg::Output * > &operands)
static std::vector< jlm::rvsdg::Output * > operands(const Node *node)
std::optional< std::vector< rvsdg::Output * > > NormalizeFlattenedBinaryOperation(const FlattenedBinaryOperation &operation, const std::vector< rvsdg::Output * > &operands)
Applies the reductions of the binary operation represented by the flattened binary operation.
static std::vector< jlm::rvsdg::Output * > outputs(const Node *node)
std::optional< std::vector< rvsdg::Output * > > FlattenAssociativeBinaryOperation(const BinaryOperation &operation, const std::vector< rvsdg::Output * > &operands)
Flattens a cascade of the same binary operations into a single flattened binary operation.
static const binop_reduction_path_t binop_reduction_none
detail::TopDownTraverserGeneric< false > TopDownTraverser
Traverser for visiting every node in a region in a top down order.
static std::string strfmt(Args... args)