Skip to content

[GlobalISel] Allow expansion of srem by constant in prelegalizer #148845

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 1 commit into from
Jul 17, 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
23 changes: 12 additions & 11 deletions llvm/include/llvm/CodeGen/GlobalISel/CombinerHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -700,18 +700,19 @@ class CombinerHelper {
/// Given an G_UDIV \p MI or G_UREM \p MI expressing a divide by constant,
/// return an expression that implements it by multiplying by a magic number.
/// Ref: "Hacker's Delight" or "The PowerPC Compiler Writer's Guide".
MachineInstr *buildUDivorURemUsingMul(MachineInstr &MI) const;
MachineInstr *buildUDivOrURemUsingMul(MachineInstr &MI) const;
/// Combine G_UDIV or G_UREM by constant into a multiply by magic constant.
bool matchUDivorURemByConst(MachineInstr &MI) const;
void applyUDivorURemByConst(MachineInstr &MI) const;

/// Given an G_SDIV \p MI expressing a signed divide by constant, return an
/// expression that implements it by multiplying by a magic number.
/// Ref: "Hacker's Delight" or "The PowerPC Compiler Writer's Guide".
MachineInstr *buildSDivUsingMul(MachineInstr &MI) const;
/// Combine G_SDIV by constant into a multiply by magic constant.
bool matchSDivByConst(MachineInstr &MI) const;
void applySDivByConst(MachineInstr &MI) const;
bool matchUDivOrURemByConst(MachineInstr &MI) const;
void applyUDivOrURemByConst(MachineInstr &MI) const;

/// Given an G_SDIV \p MI or G_SREM \p MI expressing a signed divide by
/// constant, return an expression that implements it by multiplying by a
/// magic number. Ref: "Hacker's Delight" or "The PowerPC Compiler Writer's
/// Guide".
MachineInstr *buildSDivOrSRemUsingMul(MachineInstr &MI) const;
/// Combine G_SDIV or G_SREM by constant into a multiply by magic constant.
bool matchSDivOrSRemByConst(MachineInstr &MI) const;
void applySDivOrSRemByConst(MachineInstr &MI) const;

/// Given an G_SDIV \p MI expressing a signed divided by a pow2 constant,
/// return expressions that implements it by shifting.
Expand Down
20 changes: 13 additions & 7 deletions llvm/include/llvm/Target/GlobalISel/Combine.td
Original file line number Diff line number Diff line change
Expand Up @@ -1132,14 +1132,14 @@ def form_bitfield_extract : GICombineGroup<[bitfield_extract_from_sext_inreg,
def udiv_by_const : GICombineRule<
(defs root:$root),
(match (G_UDIV $dst, $x, $y):$root,
[{ return Helper.matchUDivorURemByConst(*${root}); }]),
(apply [{ Helper.applyUDivorURemByConst(*${root}); }])>;
[{ return Helper.matchUDivOrURemByConst(*${root}); }]),
(apply [{ Helper.applyUDivOrURemByConst(*${root}); }])>;

def sdiv_by_const : GICombineRule<
(defs root:$root),
(match (G_SDIV $dst, $x, $y):$root,
[{ return Helper.matchSDivByConst(*${root}); }]),
(apply [{ Helper.applySDivByConst(*${root}); }])>;
[{ return Helper.matchSDivOrSRemByConst(*${root}); }]),
(apply [{ Helper.applySDivOrSRemByConst(*${root}); }])>;

def sdiv_by_pow2 : GICombineRule<
(defs root:$root),
Expand All @@ -1159,10 +1159,16 @@ def intdiv_combines : GICombineGroup<[udiv_by_pow2, sdiv_by_pow2,
def urem_by_const : GICombineRule<
(defs root:$root),
(match (G_UREM $dst, $x, $y):$root,
[{ return Helper.matchUDivorURemByConst(*${root}); }]),
(apply [{ Helper.applyUDivorURemByConst(*${root}); }])>;
[{ return Helper.matchUDivOrURemByConst(*${root}); }]),
(apply [{ Helper.applyUDivOrURemByConst(*${root}); }])>;

def intrem_combines : GICombineGroup<[urem_by_const]>;
def srem_by_const : GICombineRule<
(defs root:$root),
(match (G_SREM $dst, $x, $y):$root,
[{ return Helper.matchSDivOrSRemByConst(*${root}); }]),
(apply [{ Helper.applySDivOrSRemByConst(*${root}); }])>;

def intrem_combines : GICombineGroup<[urem_by_const, srem_by_const]>;

def reassoc_ptradd : GICombineRule<
(defs root:$root, build_fn_matchinfo:$matchinfo),
Expand Down
45 changes: 29 additions & 16 deletions llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5300,7 +5300,7 @@ bool CombinerHelper::matchSubAddSameReg(MachineInstr &MI,
return false;
}

MachineInstr *CombinerHelper::buildUDivorURemUsingMul(MachineInstr &MI) const {
MachineInstr *CombinerHelper::buildUDivOrURemUsingMul(MachineInstr &MI) const {
unsigned Opcode = MI.getOpcode();
assert(Opcode == TargetOpcode::G_UDIV || Opcode == TargetOpcode::G_UREM);
auto &UDivorRem = cast<GenericMachineInstr>(MI);
Expand Down Expand Up @@ -5468,7 +5468,7 @@ MachineInstr *CombinerHelper::buildUDivorURemUsingMul(MachineInstr &MI) const {
return ret;
}

bool CombinerHelper::matchUDivorURemByConst(MachineInstr &MI) const {
bool CombinerHelper::matchUDivOrURemByConst(MachineInstr &MI) const {
unsigned Opcode = MI.getOpcode();
assert(Opcode == TargetOpcode::G_UDIV || Opcode == TargetOpcode::G_UREM);
Register Dst = MI.getOperand(0).getReg();
Expand Down Expand Up @@ -5517,13 +5517,14 @@ bool CombinerHelper::matchUDivorURemByConst(MachineInstr &MI) const {
MRI, RHS, [](const Constant *C) { return C && !C->isNullValue(); });
}

void CombinerHelper::applyUDivorURemByConst(MachineInstr &MI) const {
auto *NewMI = buildUDivorURemUsingMul(MI);
void CombinerHelper::applyUDivOrURemByConst(MachineInstr &MI) const {
auto *NewMI = buildUDivOrURemUsingMul(MI);
replaceSingleDefInstWithReg(MI, NewMI->getOperand(0).getReg());
}

bool CombinerHelper::matchSDivByConst(MachineInstr &MI) const {
assert(MI.getOpcode() == TargetOpcode::G_SDIV && "Expected SDIV");
bool CombinerHelper::matchSDivOrSRemByConst(MachineInstr &MI) const {
unsigned Opcode = MI.getOpcode();
assert(Opcode == TargetOpcode::G_SDIV || Opcode == TargetOpcode::G_SREM);
Register Dst = MI.getOperand(0).getReg();
Register RHS = MI.getOperand(2).getReg();
LLT DstTy = MRI.getType(Dst);
Expand All @@ -5543,7 +5544,8 @@ bool CombinerHelper::matchSDivByConst(MachineInstr &MI) const {
return false;

// If the sdiv has an 'exact' flag we can use a simpler lowering.
if (MI.getFlag(MachineInstr::MIFlag::IsExact)) {
if (Opcode == TargetOpcode::G_SDIV &&
MI.getFlag(MachineInstr::MIFlag::IsExact)) {
return matchUnaryPredicate(
MRI, RHS, [](const Constant *C) { return C && !C->isNullValue(); });
}
Expand All @@ -5559,23 +5561,28 @@ bool CombinerHelper::matchSDivByConst(MachineInstr &MI) const {
if (!isLegal({TargetOpcode::G_SMULH, {DstTy}}) &&
!isLegalOrHasWidenScalar({TargetOpcode::G_MUL, {WideTy, WideTy}}))
return false;
if (Opcode == TargetOpcode::G_SREM &&
!isLegalOrBeforeLegalizer({TargetOpcode::G_SUB, {DstTy, DstTy}}))
return false;
}

return matchUnaryPredicate(
MRI, RHS, [](const Constant *C) { return C && !C->isNullValue(); });
}

void CombinerHelper::applySDivByConst(MachineInstr &MI) const {
auto *NewMI = buildSDivUsingMul(MI);
void CombinerHelper::applySDivOrSRemByConst(MachineInstr &MI) const {
auto *NewMI = buildSDivOrSRemUsingMul(MI);
replaceSingleDefInstWithReg(MI, NewMI->getOperand(0).getReg());
}

MachineInstr *CombinerHelper::buildSDivUsingMul(MachineInstr &MI) const {
assert(MI.getOpcode() == TargetOpcode::G_SDIV && "Expected SDIV");
auto &SDiv = cast<GenericMachineInstr>(MI);
Register Dst = SDiv.getReg(0);
Register LHS = SDiv.getReg(1);
Register RHS = SDiv.getReg(2);
MachineInstr *CombinerHelper::buildSDivOrSRemUsingMul(MachineInstr &MI) const {
unsigned Opcode = MI.getOpcode();
assert(MI.getOpcode() == TargetOpcode::G_SDIV ||
Opcode == TargetOpcode::G_SREM);
auto &SDivorRem = cast<GenericMachineInstr>(MI);
Register Dst = SDivorRem.getReg(0);
Register LHS = SDivorRem.getReg(1);
Register RHS = SDivorRem.getReg(2);
LLT Ty = MRI.getType(Dst);
LLT ScalarTy = Ty.getScalarType();
const unsigned EltBits = ScalarTy.getScalarSizeInBits();
Expand Down Expand Up @@ -5705,7 +5712,13 @@ MachineInstr *CombinerHelper::buildSDivUsingMul(MachineInstr &MI) const {
auto SignShift = MIB.buildConstant(ShiftAmtTy, EltBits - 1);
auto T = MIB.buildLShr(Ty, Q, SignShift);
T = MIB.buildAnd(Ty, T, ShiftMask);
return MIB.buildAdd(Ty, Q, T);
auto ret = MIB.buildAdd(Ty, Q, T);

if (Opcode == TargetOpcode::G_SREM) {
auto Prod = MIB.buildMul(Ty, ret, RHS);
return MIB.buildSub(Ty, LHS, Prod);
}
return ret;
}

bool CombinerHelper::matchDivByPow2(MachineInstr &MI, bool IsSigned) const {
Expand Down
Loading
Loading