Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1 +1 @@
13805
13758
19 changes: 19 additions & 0 deletions jlm/llvm/ir/operators/IntegerOperations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -864,6 +864,25 @@ IntegerEqOperation::foldConstants(
return foldBinaryOperationConstants<IntegerEqOperation>(operands);
}

std::optional<std::vector<rvsdg::Output *>>
IntegerEqOperation::normalizeIdenticalOperands(
const IntegerEqOperation &,
const std::vector<rvsdg::Output *> & operands)
{
JLM_ASSERT(operands.size() == 2);
auto & operand1 = *operands[0];
auto & operand2 = *operands[1];

const auto & tracedOperand1 = llvm::traceOutput(operand1, true);
const auto & tracedOperand2 = llvm::traceOutput(operand2, true);
if (&tracedOperand1 == &tracedOperand2)
{
return outputs(&IntegerConstantOperation::Create(*operand1.region(), 1, 1));
}

return std::nullopt;
}

IntegerNeOperation::~IntegerNeOperation() noexcept = default;

bool
Expand Down
17 changes: 17 additions & 0 deletions jlm/llvm/ir/operators/IntegerOperations.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -884,6 +884,23 @@ class IntegerEqOperation final : public IntegerBinaryOperation
foldConstants(
const IntegerEqOperation & operation,
const std::vector<rvsdg::Output *> & operands);

/**
* Performs the following normalization:
* y = IntegerEqOperation x x
* =>
* y = 1
*
* @param operation The \ref IntegerEqOperation on which the transformation is performed.
* @param operands The operands of the \ref IntegerEqOperation node.
*
* @return If the normalization could be applied, then the result of the \ref IntegerEqOperation
* after the transformation. Otherwise, std::nullopt.
*/
static std::optional<std::vector<rvsdg::Output *>>
normalizeIdenticalOperands(
const IntegerEqOperation & operation,
const std::vector<rvsdg::Output *> & operands);
};

/**
Expand Down
44 changes: 44 additions & 0 deletions jlm/llvm/ir/operators/IntegerOperationsTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -245,4 +245,48 @@ TEST(IntegerSubOperationTests, normalizeAdditiveInverse)
}
}

TEST(IntegerEqOperationTests, normalizeIdenticalOperands)
{
using namespace jlm::rvsdg;

// Arrange
auto i32Type = BitType::Create(32);

Graph graph;

auto & i0 = GraphImport::Create(graph, i32Type, "i0");
auto & i1 = GraphImport::Create(graph, i32Type, "i1");

auto & eqNode1 = IntegerEqOperation::createNode(32, i0, i0);
auto & eqNode2 = IntegerEqOperation::createNode(32, i0, i1);

auto & x1 = GraphExport::Create(*eqNode1.output(0), "x1");
auto & x2 = GraphExport::Create(*eqNode2.output(0), "x2");

// Act
ReduceNode<IntegerEqOperation>(
IntegerEqOperation::normalizeIdenticalOperands,
dynamic_cast<SimpleNode &>(eqNode1));

ReduceNode<IntegerEqOperation>(
IntegerEqOperation::normalizeIdenticalOperands,
dynamic_cast<SimpleNode &>(eqNode2));

graph.PruneNodes();

// Assert
{
auto [_, op] = TryGetSimpleNodeAndOptionalOp<IntegerConstantOperation>(*x1.origin());
EXPECT_TRUE(op);
EXPECT_EQ(op->Representation().nbits(), 1u);
EXPECT_EQ(op->Representation().to_uint(), 1u);
}

{
auto [node, op] = TryGetSimpleNodeAndOptionalOp<IntegerEqOperation>(*x2.origin());
EXPECT_TRUE(op);
EXPECT_EQ(node, &eqNode2);
}
}

}
4 changes: 2 additions & 2 deletions jlm/llvm/opt/NodeReduction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,8 @@ static std::vector<rvsdg::NodeNormalization<FPExtOperation>>
static std::vector<rvsdg::NodeNormalization<FPTruncOperation>>
fpTruncOperationNormalizations({ FPTruncOperation::foldConstant });

static std::vector<rvsdg::NodeNormalization<IntegerEqOperation>>
integerEqNormalizations({ IntegerEqOperation::foldConstants });
static std::vector<rvsdg::NodeNormalization<IntegerEqOperation>> integerEqNormalizations(
{ IntegerEqOperation::foldConstants, IntegerEqOperation::normalizeIdenticalOperands });

static std::vector<rvsdg::NodeNormalization<IntegerNeOperation>>
integerNeNormalizations({ IntegerNeOperation::foldConstants });
Expand Down
Loading