Skip to content

[GlobalISel] fdiv to fmul transform #144305

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
4 changes: 4 additions & 0 deletions llvm/include/llvm/CodeGen/GlobalISel/CombinerHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -810,6 +810,10 @@ class CombinerHelper {

bool matchCombineFMinMaxNaN(MachineInstr &MI, unsigned &Info) const;

bool matchRepeatedFPDivisor(MachineInstr &MI,
SmallVector<MachineInstr *> &MatchInfo) const;
void applyRepeatedFPDivisor(SmallVector<MachineInstr *> &MatchInfo) const;

/// Transform G_ADD(x, G_SUB(y, x)) to y.
/// Transform G_ADD(G_SUB(y, x), x) to y.
bool matchAddSubSameReg(MachineInstr &MI, Register &Src) const;
Expand Down
11 changes: 10 additions & 1 deletion llvm/include/llvm/Target/GlobalISel/Combine.td
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ def build_fn_matchinfo :
GIDefMatchData<"std::function<void(MachineIRBuilder &)>">;
def unsigned_matchinfo: GIDefMatchData<"unsigned">;
def register_vector_matchinfo : GIDefMatchData<"SmallVector<Register>">;
def mi_vector_matchinfo : GIDefMatchData<"SmallVector<MachineInstr *>">;

def copy_prop : GICombineRule<
(defs root:$d),
Expand Down Expand Up @@ -1333,6 +1334,14 @@ def combine_minmax_nan: GICombineRule<
[{ return Helper.matchCombineFMinMaxNaN(*${root}, ${info}); }]),
(apply [{ Helper.replaceSingleDefInstWithOperand(*${root}, ${info}); }])>;

// Combine multiple FDIVs with the same divisor into multiple FMULs by the
// reciprocal.
def fdiv_repeated_divison: GICombineRule<
(defs root:$root, mi_vector_matchinfo:$matchinfo),
(match (G_FDIV $dst, $src1, $src2):$root,
[{ return Helper.matchRepeatedFPDivisor(*${root}, ${matchinfo}); }]),
(apply [{ Helper.applyRepeatedFPDivisor(${matchinfo}); }])>;

// Transform (add x, (sub y, x)) -> y
// Transform (add (sub y, x), x) -> y
def add_sub_reg_frags : GICombinePatFrag<
Expand Down Expand Up @@ -2056,7 +2065,7 @@ def all_combines : GICombineGroup<[integer_reassoc_combines, trivial_combines,
constant_fold_cast_op, fabs_fneg_fold,
intdiv_combines, mulh_combines, redundant_neg_operands,
and_or_disjoint_mask, fma_combines, fold_binop_into_select,
intrem_combines, sub_add_reg, select_to_minmax,
intrem_combines, sub_add_reg, select_to_minmax, fdiv_repeated_divison,
fsub_to_fneg, commute_constant_to_rhs, match_ands, match_ors,
simplify_neg_minmax, combine_concat_vector,
sext_trunc, zext_trunc, prefer_sign_combines, shuffle_combines,
Expand Down
67 changes: 67 additions & 0 deletions llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6408,6 +6408,73 @@ bool CombinerHelper::matchCombineFMinMaxNaN(MachineInstr &MI,
return MatchNaN(1) || MatchNaN(2);
}

// Combine multiple FDIVs with the same divisor into multiple FMULs by the
// reciprocal.
// E.g., (a / Y; b / Y;) -> (recip = 1.0 / Y; a * recip; b * recip)
bool CombinerHelper::matchRepeatedFPDivisor(
MachineInstr &MI, SmallVector<MachineInstr *> &MatchInfo) const {
assert(MI.getOpcode() == TargetOpcode::G_FDIV);

Register X = MI.getOperand(1).getReg();
Register Y = MI.getOperand(2).getReg();

if (!MI.getFlag(MachineInstr::MIFlag::FmArcp))
return false;

// Skip if current node is a reciprocal/fneg-reciprocal.
auto N0CFP = isConstantOrConstantSplatVectorFP(*MRI.getVRegDef(X), MRI);
if (N0CFP && (N0CFP->isExactlyValue(1.0) || N0CFP->isExactlyValue(-1.0)))
return false;

// Exit early if the target does not want this transform or if there can't
// possibly be enough uses of the divisor to make the transform worthwhile.
unsigned MinUses = getTargetLowering().combineRepeatedFPDivisors();
if (!MinUses)
return false;

// Find all FDIV users of the same divisor. For the moment we limit all
// instructions to a single BB and use the first Instr in MatchInfo as the
// dominating position.
MatchInfo.push_back(&MI);
for (auto &U : MRI.use_nodbg_instructions(Y)) {
if (&U == &MI || U.getParent() != MI.getParent())
continue;
if (U.getOpcode() == TargetOpcode::G_FDIV &&
U.getOperand(2).getReg() == Y && U.getOperand(1).getReg() != Y) {
// This division is eligible for optimization only if global unsafe math
// is enabled or if this division allows reciprocal formation.
if (U.getFlag(MachineInstr::MIFlag::FmArcp)) {
MatchInfo.push_back(&U);
if (dominates(U, *MatchInfo[0]))
std::swap(MatchInfo[0], MatchInfo.back());
Comment on lines +6448 to +6449
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think you're handling the case where neither FDIV dominates the other. In that case, where would you insert the reciprocal instruction?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

At the moment we need to check that they are in the same BB to prevent that case, yeah. U.getParent() != MI.getParent().
I wasn't sure how to handle this more generally. This is already technically O(n^2) as the dominates calls are currently O(n).

}
}
}

// Now that we have the actual number of divisor uses, make sure it meets
// the minimum threshold specified by the target.
return MatchInfo.size() >= MinUses;
}

void CombinerHelper::applyRepeatedFPDivisor(
SmallVector<MachineInstr *> &MatchInfo) const {
// Generate the new div at the position of the first instruction, that we have
// ensured will dominate all other instructions.
Builder.setInsertPt(*MatchInfo[0]->getParent(), MatchInfo[0]);
LLT Ty = MRI.getType(MatchInfo[0]->getOperand(0).getReg());
auto Div = Builder.buildFDiv(Ty, Builder.buildFConstant(Ty, 1.0),
MatchInfo[0]->getOperand(2).getReg(),
MatchInfo[0]->getFlags());

// Replace all found div's with fmul instructions.
for (MachineInstr *MI : MatchInfo) {
Builder.setInsertPt(*MI->getParent(), MI);
Builder.buildFMul(MI->getOperand(0).getReg(), MI->getOperand(1).getReg(),
Div->getOperand(0).getReg(), MI->getFlags());
MI->eraseFromParent();
}
}

bool CombinerHelper::matchAddSubSameReg(MachineInstr &MI, Register &Src) const {
assert(MI.getOpcode() == TargetOpcode::G_ADD && "Expected a G_ADD");
Register LHS = MI.getOperand(1).getReg();
Expand Down
Loading