Skip to content
Open
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
28 changes: 28 additions & 0 deletions jlm/llvm/ir/operators/IntegerOperations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -762,6 +762,34 @@ IntegerOrOperation::foldConstants(
return foldBinaryOperationConstants<IntegerOrOperation>(operands);
}

std::optional<std::vector<rvsdg::Output *>>
IntegerOrOperation::normalizeIdempotent(
const IntegerOrOperation &,
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);
auto [c1Node, c1Operation] =
rvsdg::TryGetSimpleNodeAndOptionalOp<IntegerConstantOperation>(tracedOperand1);
if (c1Operation && c1Operation->Representation().to_uint() == 0)
{
return std::vector({ &operand2 });
}

const auto & tracedOperand2 = llvm::traceOutput(operand2);
auto [c2Node, c2Operation] =
rvsdg::TryGetSimpleNodeAndOptionalOp<IntegerConstantOperation>(tracedOperand2);
if (c2Operation && c2Operation->Representation().to_uint() == 0)
{
return std::vector({ &operand1 });
}

return std::nullopt;
}

IntegerXorOperation::~IntegerXorOperation() noexcept = default;

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

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

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

TEST(IntegerOrOperationTest, normalizeIdempotent)
{
using namespace jlm::rvsdg;

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

Graph graph;

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

auto & zeroNode = IntegerConstantOperation::Create(graph.GetRootRegion(), 32, 0);
auto & oneNode = IntegerConstantOperation::Create(graph.GetRootRegion(), 32, 1);

auto & subNode1 = IntegerOrOperation::createNode(32, i0, *zeroNode.output(0));
auto & subNode2 = IntegerOrOperation::createNode(32, *zeroNode.output(0), i0);
auto & subNode3 = IntegerOrOperation::createNode(32, i0, *oneNode.output(0));
auto & subNode4 = IntegerOrOperation::createNode(32, *oneNode.output(0), i0);

auto & x1 = GraphExport::Create(*subNode1.output(0), "x1");
auto & x2 = GraphExport::Create(*subNode2.output(0), "x2");
auto & x3 = GraphExport::Create(*subNode3.output(0), "x3");
auto & x4 = GraphExport::Create(*subNode4.output(0), "x4");

// Act
ReduceNode<IntegerOrOperation>(
IntegerOrOperation::normalizeIdempotent,
dynamic_cast<SimpleNode &>(subNode1));
ReduceNode<IntegerOrOperation>(
IntegerOrOperation::normalizeIdempotent,
dynamic_cast<SimpleNode &>(subNode2));
ReduceNode<IntegerOrOperation>(
IntegerOrOperation::normalizeIdempotent,
dynamic_cast<SimpleNode &>(subNode3));
ReduceNode<IntegerOrOperation>(
IntegerOrOperation::normalizeIdempotent,
dynamic_cast<SimpleNode &>(subNode4));

graph.PruneNodes();

view(graph, stdout);

// Assert
EXPECT_EQ(x1.origin(), &i0);
EXPECT_EQ(x2.origin(), &i0);
EXPECT_EQ(x3.origin(), subNode3.output(0));
EXPECT_EQ(x4.origin(), subNode4.output(0));
}

}
4 changes: 2 additions & 2 deletions jlm/llvm/opt/NodeReduction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,8 @@ static std::vector<rvsdg::NodeNormalization<IntegerLShrOperation>>
static std::vector<rvsdg::NodeNormalization<IntegerAndOperation>>
integerAndNormalizations({ IntegerAndOperation::foldConstants });

static std::vector<rvsdg::NodeNormalization<IntegerOrOperation>>
integerOrNormalizations({ IntegerOrOperation::foldConstants });
static std::vector<rvsdg::NodeNormalization<IntegerOrOperation>> integerOrNormalizations(
{ IntegerOrOperation::foldConstants, IntegerOrOperation::normalizeIdempotent });

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