Skip to content
Closed
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
9 changes: 8 additions & 1 deletion llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -795,7 +795,8 @@ Instruction *InstCombinerImpl::foldFMulReassoc(BinaryOperator &I) {
return BinaryOperator::CreateFDivFMF(CC1, X, FMF);
}
if (match(Op0, m_FDiv(m_Value(X), m_Constant(C1)))) {
// FIXME: This seems like it should also be checking for arcp
// Constant folding is not affected by allowReciprocal,
// since constants are calculated at compile time.
Comment on lines +798 to +799
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't exactly how semantics works

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, despite the original comment, this really needs to be matching against the reassoc flag, not the arcp flag (since arcp explicitly doesn't allow reassociation of the division operation, only the inversion of the division).

// (X / C1) * C --> X * (C / C1)
Constant *CDivC1 =
ConstantFoldBinaryOpOperands(Instruction::FDiv, C, C1, DL);
Expand Down Expand Up @@ -837,6 +838,12 @@ Instruction *InstCombinerImpl::foldFMulReassoc(BinaryOperator &I) {
m_Value(Z)))) {
BinaryOperator *DivOp = cast<BinaryOperator>(((Z == Op0) ? Op1 : Op0));
FastMathFlags FMF = I.getFastMathFlags() & DivOp->getFastMathFlags();
// Preserve (X / Y) * Z when reciprocal math is enabled to allow backend
// rcp instruction emission. Sinking to (X * Z) / Y would prevent this
// optimization, alter computation order and potentially affecting
// precision.
if (FMF.allowReciprocal())
return nullptr;
if (FMF.allowReassoc()) {
// Sink division: (X / Y) * Z --> (X * Z) / Y
auto *NewFMul = Builder.CreateFMulFMF(X, Z, FMF);
Expand Down