Skip to content

Commit 47372fa

Browse files
committed
[GlobalISel] Handle div-by-pow2
This patch adds similar handling of div-by-pow2 as in `SelectionDAG`.
1 parent 001e18c commit 47372fa

File tree

4 files changed

+76
-7
lines changed

4 files changed

+76
-7
lines changed

llvm/include/llvm/CodeGen/GlobalISel/CombinerHelper.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -673,6 +673,9 @@ class CombinerHelper {
673673
bool matchSDivByConst(MachineInstr &MI);
674674
void applySDivByConst(MachineInstr &MI);
675675

676+
bool matchSDivByPow2(MachineInstr &MI);
677+
void applySDivByPow2(MachineInstr &MI);
678+
676679
// G_UMULH x, (1 << c)) -> x >> (bitwidth - c)
677680
bool matchUMulHToLShr(MachineInstr &MI);
678681
void applyUMulHToLShr(MachineInstr &MI);

llvm/include/llvm/Target/GlobalISel/Combine.td

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ def combine_extracted_vector_load : GICombineRule<
264264
(match (wip_match_opcode G_EXTRACT_VECTOR_ELT):$root,
265265
[{ return Helper.matchCombineExtractedVectorLoad(*${root}, ${matchinfo}); }]),
266266
(apply [{ Helper.applyBuildFn(*${root}, ${matchinfo}); }])>;
267-
267+
268268
def combine_indexed_load_store : GICombineRule<
269269
(defs root:$root, indexed_load_store_matchdata:$matchinfo),
270270
(match (wip_match_opcode G_LOAD, G_SEXTLOAD, G_ZEXTLOAD, G_STORE):$root,
@@ -1005,7 +1005,13 @@ def sdiv_by_const : GICombineRule<
10051005
[{ return Helper.matchSDivByConst(*${root}); }]),
10061006
(apply [{ Helper.applySDivByConst(*${root}); }])>;
10071007

1008-
def intdiv_combines : GICombineGroup<[udiv_by_const, sdiv_by_const]>;
1008+
def sdiv_by_pow2 : GICombineRule<
1009+
(defs root:$root),
1010+
(match (wip_match_opcode G_SDIV):$root,
1011+
[{ return Helper.matchSDivByPow2(*${root}); }]),
1012+
(apply [{ Helper.applySDivByPow2(*${root}); }])>;
1013+
1014+
def intdiv_combines : GICombineGroup<[udiv_by_const, sdiv_by_const, sdiv_by_pow2]>;
10091015

10101016
def reassoc_ptradd : GICombineRule<
10111017
(defs root:$root, build_fn_matchinfo:$matchinfo),
@@ -1325,7 +1331,7 @@ def constant_fold_binops : GICombineGroup<[constant_fold_binop,
13251331

13261332
def all_combines : GICombineGroup<[trivial_combines, insert_vec_elt_combines,
13271333
extract_vec_elt_combines, combines_for_extload, combine_extracted_vector_load,
1328-
undef_combines, identity_combines, phi_combines,
1334+
undef_combines, identity_combines, phi_combines,
13291335
simplify_add_to_sub, hoist_logic_op_with_same_opcode_hands, shifts_too_big,
13301336
reassocs, ptr_add_immed_chain,
13311337
shl_ashr_to_sext_inreg, sext_inreg_of_load,
@@ -1342,7 +1348,7 @@ def all_combines : GICombineGroup<[trivial_combines, insert_vec_elt_combines,
13421348
intdiv_combines, mulh_combines, redundant_neg_operands,
13431349
and_or_disjoint_mask, fma_combines, fold_binop_into_select,
13441350
sub_add_reg, select_to_minmax, redundant_binop_in_equality,
1345-
fsub_to_fneg, commute_constant_to_rhs, match_ands, match_ors,
1351+
fsub_to_fneg, commute_constant_to_rhs, match_ands, match_ors,
13461352
combine_concat_vector]>;
13471353

13481354
// A combine group used to for prelegalizer combiners at -O0. The combines in

llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1490,7 +1490,7 @@ void CombinerHelper::applyOptBrCondByInvertingCond(MachineInstr &MI,
14901490
Observer.changedInstr(*BrCond);
14911491
}
14921492

1493-
1493+
14941494
bool CombinerHelper::tryEmitMemcpyInline(MachineInstr &MI) {
14951495
MachineIRBuilder HelperBuilder(MI);
14961496
GISelObserverWrapper DummyObserver;
@@ -5286,6 +5286,66 @@ MachineInstr *CombinerHelper::buildSDivUsingMul(MachineInstr &MI) {
52865286
return MIB.buildMul(Ty, Res, Factor);
52875287
}
52885288

5289+
bool CombinerHelper::matchSDivByPow2(MachineInstr &MI) {
5290+
assert(MI.getOpcode() == TargetOpcode::G_SDIV && "Expected SDIV");
5291+
if (MI.getFlag(MachineInstr::MIFlag::IsExact))
5292+
return false;
5293+
auto &SDiv = cast<GenericMachineInstr>(MI);
5294+
Register RHS = SDiv.getReg(2);
5295+
auto MatchPow2 = [&](const Constant *C) {
5296+
if (auto *CI = dyn_cast<ConstantInt>(C))
5297+
return CI->getValue().isPowerOf2() || CI->getValue().isNegatedPowerOf2();
5298+
return false;
5299+
};
5300+
return matchUnaryPredicate(MRI, RHS, MatchPow2, /* AllowUndefs */ false);
5301+
}
5302+
5303+
void CombinerHelper::applySDivByPow2(MachineInstr &MI) {
5304+
assert(MI.getOpcode() == TargetOpcode::G_SDIV && "Expected SDIV");
5305+
auto &SDiv = cast<GenericMachineInstr>(MI);
5306+
Register Dst = SDiv.getReg(0);
5307+
Register LHS = SDiv.getReg(1);
5308+
Register RHS = SDiv.getReg(2);
5309+
LLT Ty = MRI.getType(Dst);
5310+
LLT ShiftAmtTy = getTargetLowering().getPreferredShiftAmountTy(Ty);
5311+
5312+
Builder.setInstrAndDebugLoc(MI);
5313+
5314+
auto RHSC = getIConstantVRegValWithLookThrough(RHS, MRI);
5315+
assert(RHSC.has_value() && "RHS must be a constant");
5316+
auto RHSCV = RHSC->Value;
5317+
auto Zero = Builder.buildConstant(Ty, 0);
5318+
5319+
// Special case: (sdiv X, 1) -> X
5320+
if (RHSCV.isOne()) {
5321+
replaceSingleDefInstWithReg(MI, LHS);
5322+
return;
5323+
}
5324+
// Special Case: (sdiv X, -1) -> 0-X
5325+
if (RHSCV.isAllOnes()) {
5326+
auto Sub = Builder.buildSub(Ty, Zero, LHS);
5327+
replaceSingleDefInstWithReg(MI, Sub->getOperand(0).getReg());
5328+
return;
5329+
}
5330+
5331+
unsigned Bitwidth = Ty.getScalarSizeInBits();
5332+
auto Bits = Builder.buildConstant(ShiftAmtTy, Bitwidth);
5333+
auto C1 = Builder.buildCTTZ(Ty, RHS);
5334+
C1 = Builder.buildZExtOrTrunc(ShiftAmtTy, C1);
5335+
auto Inexact = Builder.buildSub(ShiftAmtTy, Bits, C1);
5336+
auto Sign = Builder.buildAShr(
5337+
Ty, LHS, Builder.buildConstant(ShiftAmtTy, Bitwidth - 1));
5338+
// Add (LHS < 0) ? abs2 - 1 : 0;
5339+
auto Srl = Builder.buildShl(Ty, Sign, Inexact);
5340+
auto Add = Builder.buildAdd(Ty, LHS, Srl);
5341+
auto Sra = Builder.buildAShr(Ty, Add, C1);
5342+
5343+
// If dividing by a positive value, we're done. Otherwise, the result must
5344+
// be negated.
5345+
auto Res = RHSCV.isNegative() ? Builder.buildSub(Ty, Zero, Sra) : Sra;
5346+
replaceSingleDefInstWithReg(MI, Res->getOperand(0).getReg());
5347+
}
5348+
52895349
bool CombinerHelper::matchUMulHToLShr(MachineInstr &MI) {
52905350
assert(MI.getOpcode() == TargetOpcode::G_UMULH);
52915351
Register RHS = MI.getOperand(2).getReg();

llvm/test/CodeGen/AMDGPU/div_i128.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
; RUN: llc -O0 -global-isel=0 -mtriple=amdgcn-amd-amdhsa -mcpu=gfx900 -o - %s | FileCheck -check-prefixes=GFX9-O0,GFX9-SDAG-O0 %s
44

55
; FIXME: GlobalISel missing the power-of-2 cases in legalization. https://github.com/llvm/llvm-project/issues/80671
6-
; xUN: llc -global-isel=1 -mtriple=amdgcn-amd-amdhsa -mcpu=gfx900 -o - %s | FileCheck -check-prefixes=GFX9,GFX9 %s
7-
; xUN: llc -O0 -global-isel=1 -mtriple=amdgcn-amd-amdhsa -mcpu=gfx900 -o - %s | FileCheck -check-prefixes=GFX9-O0,GFX9-O0 %s
6+
; RUN: llc -global-isel=1 -mtriple=amdgcn-amd-amdhsa -mcpu=gfx900 -o - %s | FileCheck -check-prefixes=GFX9,GFX9 %s
7+
; RUN: llc -O0 -global-isel=1 -mtriple=amdgcn-amd-amdhsa -mcpu=gfx900 -o - %s | FileCheck -check-prefixes=GFX9-O0,GFX9-O0 %s
88

99
define i128 @v_sdiv_i128_vv(i128 %lhs, i128 %rhs) {
1010
; GFX9-LABEL: v_sdiv_i128_vv:

0 commit comments

Comments
 (0)