Skip to content

[InstCombine] Combine interleaved PHI reduction chains. #143878

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jul 1, 2025
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
14 changes: 14 additions & 0 deletions llvm/lib/Transforms/InstCombine/InstCombineInternal.h
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,20 @@ class LLVM_LIBRARY_VISIBILITY InstCombinerImpl final
Instruction *foldOpIntoPhi(Instruction &I, PHINode *PN,
bool AllowMultipleUses = false);

/// Try to fold binary operators whose operands are simple interleaved
/// recurrences to a single recurrence. This is a common pattern in reduction
/// operations.
/// Example:
/// %phi1 = phi [init1, %BB1], [%op1, %BB2]
/// %phi2 = phi [init2, %BB1], [%op2, %BB2]
/// %op1 = binop %phi1, constant1
/// %op2 = binop %phi2, constant2
/// %rdx = binop %op1, %op2
/// -->
/// %phi_combined = phi [init_combined, %BB1], [%op_combined, %BB2]
/// %rdx_combined = binop %phi_combined, constant_combined
Instruction *foldBinopWithRecurrence(BinaryOperator &BO);

/// For a binary operator with 2 phi operands, try to hoist the binary
/// operation before the phi. This can result in fewer instructions in
/// patterns where at least one set of phi operands simplifies.
Expand Down
107 changes: 107 additions & 0 deletions llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1989,7 +1989,114 @@ Instruction *InstCombinerImpl::foldOpIntoPhi(Instruction &I, PHINode *PN,
return replaceInstUsesWith(I, NewPN);
}

Instruction *InstCombinerImpl::foldBinopWithRecurrence(BinaryOperator &BO) {
if (!BO.isAssociative())
return nullptr;

// Find the interleaved binary ops.
auto Opc = BO.getOpcode();
auto *BO0 = dyn_cast<BinaryOperator>(BO.getOperand(0));
auto *BO1 = dyn_cast<BinaryOperator>(BO.getOperand(1));
if (!BO0 || !BO1 || !BO0->hasNUses(2) || !BO1->hasNUses(2) ||
BO0->getOpcode() != Opc || BO1->getOpcode() != Opc ||
!BO0->isAssociative() || !BO1->isAssociative() ||
BO0->getParent() != BO1->getParent())
return nullptr;

assert(BO.isCommutative() && BO0->isCommutative() && BO1->isCommutative() &&
"Expected commutative instructions!");

// Find the matching phis, forming the recurrences.
PHINode *PN0, *PN1;
Value *Start0, *Step0, *Start1, *Step1;
if (!matchSimpleRecurrence(BO0, PN0, Start0, Step0) || !PN0->hasOneUse() ||
!matchSimpleRecurrence(BO1, PN1, Start1, Step1) || !PN1->hasOneUse() ||
PN0->getParent() != PN1->getParent())
return nullptr;

assert(PN0->getNumIncomingValues() == 2 && PN1->getNumIncomingValues() == 2 &&
"Expected PHIs with two incoming values!");

// Convert the start and step values to constants.
auto *Init0 = dyn_cast<Constant>(Start0);
auto *Init1 = dyn_cast<Constant>(Start1);
auto *C0 = dyn_cast<Constant>(Step0);
auto *C1 = dyn_cast<Constant>(Step1);
if (!Init0 || !Init1 || !C0 || !C1)
return nullptr;

// Fold the recurrence constants.
auto *Init = ConstantFoldBinaryInstruction(Opc, Init0, Init1);
auto *C = ConstantFoldBinaryInstruction(Opc, C0, C1);
if (!Init || !C)
return nullptr;

// Create the reduced PHI.
auto *NewPN = PHINode::Create(PN0->getType(), PN0->getNumIncomingValues(),
"reduced.phi");

// Create the new binary op.
auto *NewBO = BinaryOperator::Create(Opc, NewPN, C);
if (Opc == Instruction::FAdd || Opc == Instruction::FMul) {
// Intersect FMF flags for FADD and FMUL.
FastMathFlags Intersect = BO0->getFastMathFlags() &
BO1->getFastMathFlags() & BO.getFastMathFlags();
NewBO->setFastMathFlags(Intersect);
} else {
OverflowTracking Flags;
Flags.AllKnownNonNegative = false;
Flags.AllKnownNonZero = false;
Flags.mergeFlags(*BO0);
Flags.mergeFlags(*BO1);
Flags.mergeFlags(BO);
Flags.applyFlags(*NewBO);
}
NewBO->takeName(&BO);

for (unsigned I = 0, E = PN0->getNumIncomingValues(); I != E; ++I) {
auto *V = PN0->getIncomingValue(I);
auto *BB = PN0->getIncomingBlock(I);
if (V == Init0) {
assert(((PN1->getIncomingValue(0) == Init1 &&
PN1->getIncomingBlock(0) == BB) ||
(PN1->getIncomingValue(1) == Init1 &&
PN1->getIncomingBlock(1) == BB)) &&
"Invalid incoming block!");
NewPN->addIncoming(Init, BB);
} else if (V == BO0) {
assert(((PN1->getIncomingValue(0) == BO1 &&
PN1->getIncomingBlock(0) == BB) ||
(PN1->getIncomingValue(1) == BO1 &&
PN1->getIncomingBlock(1) == BB)) &&
"Invalid incoming block!");
NewPN->addIncoming(NewBO, BB);
} else
llvm_unreachable("Unexpected incoming value!");
}

LLVM_DEBUG(dbgs() << " Combined " << *PN0 << "\n " << *BO0
<< "\n with " << *PN1 << "\n " << *BO1
<< '\n');

// Insert the new recurrence and remove the old (dead) ones.
InsertNewInstWith(NewPN, PN0->getIterator());
InsertNewInstWith(NewBO, BO0->getIterator());

eraseInstFromFunction(
*replaceInstUsesWith(*BO0, PoisonValue::get(BO0->getType())));
eraseInstFromFunction(
*replaceInstUsesWith(*BO1, PoisonValue::get(BO1->getType())));
eraseInstFromFunction(*PN0);
eraseInstFromFunction(*PN1);

return replaceInstUsesWith(BO, NewBO);
}

Instruction *InstCombinerImpl::foldBinopWithPhiOperands(BinaryOperator &BO) {
// Attempt to fold binary operators whose operands are simple recurrences.
if (auto *NewBO = foldBinopWithRecurrence(BO))
return NewBO;

// TODO: This should be similar to the incoming values check in foldOpIntoPhi:
// we are guarding against replicating the binop in >1 predecessor.
// This could miss matching a phi with 2 constant incoming values.
Expand Down
Loading
Loading