Skip to content

Commit d6c5329

Browse files
committed
[GlobalISel] Handle div-by-pow2
This patch adds similar handling of div-by-pow2 as in `SelectionDAG`.
1 parent 6a39a71 commit d6c5329

File tree

7 files changed

+2501
-734
lines changed

7 files changed

+2501
-734
lines changed

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

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

676+
/// Given an G_SDIV \p MI expressing a signed divided by a pow2 constant,
677+
/// return expressions that implements it by shifting.
678+
bool matchDivByPow2(MachineInstr &MI, bool IsSigned);
679+
void applySDivByPow2(MachineInstr &MI);
680+
/// Given an G_UDIV \p MI expressing an unsigned divided by a pow2 constant,
681+
/// return expressions that implements it by shifting.
682+
void applyUDivByPow2(MachineInstr &MI);
683+
676684
// G_UMULH x, (1 << c)) -> x >> (bitwidth - c)
677685
bool matchUMulHToLShr(MachineInstr &MI);
678686
void applyUMulHToLShr(MachineInstr &MI);

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

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ def FmArcp : MIFlagEnum<"FmArcp">;
179179
def FmContract : MIFlagEnum<"FmContract">;
180180
def FmAfn : MIFlagEnum<"FmAfn">;
181181
def FmReassoc : MIFlagEnum<"FmReassoc">;
182+
def IsExact : MIFlagEnum<"IsExact">;
182183

183184
def MIFlags;
184185
// def not; -> Already defined as a SDNode
@@ -264,7 +265,7 @@ def combine_extracted_vector_load : GICombineRule<
264265
(match (wip_match_opcode G_EXTRACT_VECTOR_ELT):$root,
265266
[{ return Helper.matchCombineExtractedVectorLoad(*${root}, ${matchinfo}); }]),
266267
(apply [{ Helper.applyBuildFn(*${root}, ${matchinfo}); }])>;
267-
268+
268269
def combine_indexed_load_store : GICombineRule<
269270
(defs root:$root, indexed_load_store_matchdata:$matchinfo),
270271
(match (wip_match_opcode G_LOAD, G_SEXTLOAD, G_ZEXTLOAD, G_STORE):$root,
@@ -1036,7 +1037,20 @@ def sdiv_by_const : GICombineRule<
10361037
[{ return Helper.matchSDivByConst(*${root}); }]),
10371038
(apply [{ Helper.applySDivByConst(*${root}); }])>;
10381039

1039-
def intdiv_combines : GICombineGroup<[udiv_by_const, sdiv_by_const]>;
1040+
def sdiv_by_pow2 : GICombineRule<
1041+
(defs root:$root),
1042+
(match (G_SDIV $dst, $x, $y, (MIFlags (not IsExact))):$root,
1043+
[{ return Helper.matchDivByPow2(*${root}, /*IsSigned=*/true); }]),
1044+
(apply [{ Helper.applySDivByPow2(*${root}); }])>;
1045+
1046+
def udiv_by_pow2 : GICombineRule<
1047+
(defs root:$root),
1048+
(match (G_UDIV $dst, $x, $y, (MIFlags (not IsExact))):$root,
1049+
[{ return Helper.matchDivByPow2(*${root}, /*IsSigned=*/false); }]),
1050+
(apply [{ Helper.applyUDivByPow2(*${root}); }])>;
1051+
1052+
def intdiv_combines : GICombineGroup<[udiv_by_const, sdiv_by_const,
1053+
sdiv_by_pow2, udiv_by_pow2]>;
10401054

10411055
def reassoc_ptradd : GICombineRule<
10421056
(defs root:$root, build_fn_matchinfo:$matchinfo),
@@ -1356,7 +1370,7 @@ def constant_fold_binops : GICombineGroup<[constant_fold_binop,
13561370

13571371
def all_combines : GICombineGroup<[trivial_combines, insert_vec_elt_combines,
13581372
extract_vec_elt_combines, combines_for_extload, combine_extracted_vector_load,
1359-
undef_combines, identity_combines, phi_combines,
1373+
undef_combines, identity_combines, phi_combines,
13601374
simplify_add_to_sub, hoist_logic_op_with_same_opcode_hands, shifts_too_big,
13611375
reassocs, ptr_add_immed_chain,
13621376
shl_ashr_to_sext_inreg, sext_inreg_of_load,
@@ -1373,7 +1387,7 @@ def all_combines : GICombineGroup<[trivial_combines, insert_vec_elt_combines,
13731387
intdiv_combines, mulh_combines, redundant_neg_operands,
13741388
and_or_disjoint_mask, fma_combines, fold_binop_into_select,
13751389
sub_add_reg, select_to_minmax, redundant_binop_in_equality,
1376-
fsub_to_fneg, commute_constant_to_rhs, match_ands, match_ors,
1390+
fsub_to_fneg, commute_constant_to_rhs, match_ands, match_ors,
13771391
combine_concat_vector, double_icmp_zero_and_or_combine]>;
13781392

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

llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp

Lines changed: 151 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,156 @@ MachineInstr *CombinerHelper::buildSDivUsingMul(MachineInstr &MI) {
52865286
return MIB.buildMul(Ty, Res, Factor);
52875287
}
52885288

5289+
bool CombinerHelper::matchDivByPow2(MachineInstr &MI, bool IsSigned) {
5290+
assert((MI.getOpcode() == TargetOpcode::G_SDIV ||
5291+
MI.getOpcode() == TargetOpcode::G_UDIV) &&
5292+
"Expected SDIV or UDIV");
5293+
auto &Div = cast<GenericMachineInstr>(MI);
5294+
Register RHS = Div.getReg(2);
5295+
auto MatchPow2 = [&](const Constant *C) {
5296+
if (auto *CI = dyn_cast<ConstantInt>(C)) {
5297+
if (CI->getValue().isPowerOf2())
5298+
return true;
5299+
if (IsSigned && CI->getValue().isNegatedPowerOf2())
5300+
return true;
5301+
}
5302+
return false;
5303+
};
5304+
return matchUnaryPredicate(MRI, RHS, MatchPow2, /*AllowUndefs=*/false);
5305+
}
5306+
5307+
void CombinerHelper::applySDivByPow2(MachineInstr &MI) {
5308+
assert(MI.getOpcode() == TargetOpcode::G_SDIV && "Expected SDIV");
5309+
auto &SDiv = cast<GenericMachineInstr>(MI);
5310+
Register Dst = SDiv.getReg(0);
5311+
Register LHS = SDiv.getReg(1);
5312+
Register RHS = SDiv.getReg(2);
5313+
LLT Ty = MRI.getType(Dst);
5314+
LLT ShiftAmtTy = getTargetLowering().getPreferredShiftAmountTy(Ty);
5315+
5316+
Builder.setInstrAndDebugLoc(MI);
5317+
5318+
// Effectively we want to lower G_SDIV %lhs, %rhs, where %rhs is a power of 2,
5319+
// to the following version:
5320+
//
5321+
// %bits = G_CONSTANT $bitwidth
5322+
// %c1 = G_CTTZ %rhs
5323+
// %c1 = G_ZEXT %c1
5324+
// %inexact = G_SUB $bits, %c1
5325+
// %tmp = G_CONSTANT ($bitwidth - 1)
5326+
// %sign = %G_ASHR %lhs, %tmp
5327+
// %srl = G_SHL %sign, %inexact
5328+
// %add = G_ADD $lhs, $srl
5329+
// $sra = G_ASHR %add, %c1
5330+
// %one = G_CONSTANT $1
5331+
// %allones = G_CONSTANT %111..1
5332+
// %isone = G_ICMP EQ %rhs, $one
5333+
// %isallones = G_ICMP EQ %rhs, $allones
5334+
// %isoneorallones = G_OR %isone, $isallones
5335+
// %sra = G_SELECT, %isoneorallones, %lhs, %sra
5336+
// %zero = G_CONSTANT $0
5337+
// %sub = G_SUB %zero, %sra
5338+
// %isneg = G_ICMP SLT $lhs, %zero
5339+
// %res = G_SELECT %isneg, %sub, %sra
5340+
//
5341+
// When $rhs is a constant integer, or a splat vector, we can check its value
5342+
// at compile time such that the first two G_ICMP conditional statements, as
5343+
// well as the corresponding non-taken branches, can be eliminated. This can
5344+
// generate compact code even w/o any constant folding afterwards. When $rhs
5345+
// is not a splat vector, we have to generate those checks via instructions.
5346+
5347+
unsigned Bitwidth = Ty.getScalarSizeInBits();
5348+
auto Zero = Builder.buildConstant(Ty, 0);
5349+
5350+
if (auto RHSC = getConstantOrConstantSplatVector(RHS)) {
5351+
// Special case: (sdiv X, 1) -> X
5352+
if (RHSC->isOne()) {
5353+
replaceSingleDefInstWithReg(MI, LHS);
5354+
return;
5355+
}
5356+
// Special Case: (sdiv X, -1) -> 0-X
5357+
if (RHSC->isAllOnes()) {
5358+
auto Sub = Builder.buildSub(Ty, Zero, LHS);
5359+
replaceSingleDefInstWithReg(MI, Sub->getOperand(0).getReg());
5360+
return;
5361+
}
5362+
5363+
unsigned TrailingZeros = RHSC->countTrailingZeros();
5364+
auto C1 = Builder.buildConstant(ShiftAmtTy, TrailingZeros);
5365+
auto Inexact = Builder.buildConstant(ShiftAmtTy, Bitwidth - TrailingZeros);
5366+
auto Sign = Builder.buildAShr(
5367+
Ty, LHS, Builder.buildConstant(ShiftAmtTy, Bitwidth - 1));
5368+
// Add (LHS < 0) ? abs2 - 1 : 0;
5369+
auto Srl = Builder.buildShl(Ty, Sign, Inexact);
5370+
auto Add = Builder.buildAdd(Ty, LHS, Srl);
5371+
auto Sra = Builder.buildAShr(Ty, Add, C1);
5372+
5373+
// If dividing by a positive value, we're done. Otherwise, the result must
5374+
// be negated.
5375+
auto Res = RHSC->isNegative() ? Builder.buildSub(Ty, Zero, Sra) : Sra;
5376+
replaceSingleDefInstWithReg(MI, Res->getOperand(0).getReg());
5377+
return;
5378+
}
5379+
5380+
// RHS is not a splat vector. Build the above version with instructions.
5381+
auto Bits = Builder.buildConstant(ShiftAmtTy, Bitwidth);
5382+
auto C1 = Builder.buildCTTZ(Ty, RHS);
5383+
C1 = Builder.buildZExtOrTrunc(ShiftAmtTy, C1);
5384+
auto Inexact = Builder.buildSub(ShiftAmtTy, Bits, C1);
5385+
auto Sign = Builder.buildAShr(
5386+
Ty, LHS, Builder.buildConstant(ShiftAmtTy, Bitwidth - 1));
5387+
5388+
// Add (LHS < 0) ? abs2 - 1 : 0;
5389+
auto Srl = Builder.buildShl(Ty, Sign, Inexact);
5390+
auto Add = Builder.buildAdd(Ty, LHS, Srl);
5391+
auto Sra = Builder.buildAShr(Ty, Add, C1);
5392+
5393+
LLT CCVT = LLT::vector(Ty.getElementCount(), 1);
5394+
5395+
auto One = Builder.buildConstant(Ty, 1);
5396+
auto AllOnes =
5397+
Builder.buildConstant(Ty, APInt::getAllOnes(Ty.getScalarSizeInBits()));
5398+
auto IsOne = Builder.buildICmp(CmpInst::Predicate::ICMP_EQ, CCVT, RHS, One);
5399+
auto IsAllOnes =
5400+
Builder.buildICmp(CmpInst::Predicate::ICMP_EQ, CCVT, RHS, AllOnes);
5401+
auto IsOneOrAllOnes = Builder.buildOr(CCVT, IsOne, IsAllOnes);
5402+
Sra = Builder.buildSelect(Ty, IsOneOrAllOnes, LHS, Sra);
5403+
5404+
// If dividing by a positive value, we're done. Otherwise, the result must
5405+
// be negated.
5406+
auto Sub = Builder.buildSub(Ty, Zero, Sra);
5407+
auto IsNeg = Builder.buildICmp(CmpInst::Predicate::ICMP_SLT, CCVT, LHS, Zero);
5408+
auto Res = Builder.buildSelect(Ty, IsNeg, Sub, Sra);
5409+
replaceSingleDefInstWithReg(MI, Res->getOperand(0).getReg());
5410+
}
5411+
5412+
void CombinerHelper::applyUDivByPow2(MachineInstr &MI) {
5413+
assert(MI.getOpcode() == TargetOpcode::G_UDIV && "Expected SDIV");
5414+
auto &UDiv = cast<GenericMachineInstr>(MI);
5415+
Register Dst = UDiv.getReg(0);
5416+
Register LHS = UDiv.getReg(1);
5417+
Register RHS = UDiv.getReg(2);
5418+
LLT Ty = MRI.getType(Dst);
5419+
LLT ShiftAmtTy = getTargetLowering().getPreferredShiftAmountTy(Ty);
5420+
5421+
Builder.setInstrAndDebugLoc(MI);
5422+
5423+
auto RHSC = getIConstantVRegValWithLookThrough(RHS, MRI);
5424+
assert(RHSC.has_value() && "RHS must be a constant");
5425+
auto RHSCV = RHSC->Value;
5426+
5427+
// Special case: (udiv X, 1) -> X
5428+
if (RHSCV.isOne()) {
5429+
replaceSingleDefInstWithReg(MI, LHS);
5430+
return;
5431+
}
5432+
5433+
unsigned TrailingZeros = RHSCV.countTrailingZeros();
5434+
auto C1 = Builder.buildConstant(ShiftAmtTy, TrailingZeros);
5435+
auto Res = Builder.buildLShr(Ty, LHS, C1);
5436+
replaceSingleDefInstWithReg(MI, Res->getOperand(0).getReg());
5437+
}
5438+
52895439
bool CombinerHelper::matchUMulHToLShr(MachineInstr &MI) {
52905440
assert(MI.getOpcode() == TargetOpcode::G_UMULH);
52915441
Register RHS = MI.getOperand(2).getReg();

llvm/test/CodeGen/AMDGPU/GlobalISel/llvm.amdgcn.sbfe.ll

Lines changed: 12 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -670,36 +670,19 @@ define amdgpu_kernel void @bfe_sext_in_reg_i24(ptr addrspace(1) %out, ptr addrsp
670670
define amdgpu_kernel void @simplify_demanded_bfe_sdiv(ptr addrspace(1) %out, ptr addrspace(1) %in) #0 {
671671
; GFX6-LABEL: simplify_demanded_bfe_sdiv:
672672
; GFX6: ; %bb.0:
673-
; GFX6-NEXT: v_rcp_iflag_f32_e32 v0, 2.0
674-
; GFX6-NEXT: s_load_dwordx4 s[4:7], s[0:1], 0x0
675-
; GFX6-NEXT: v_mul_f32_e32 v0, 0x4f7ffffe, v0
676-
; GFX6-NEXT: v_cvt_u32_f32_e32 v0, v0
673+
; GFX6-NEXT: s_load_dwordx4 s[0:3], s[0:1], 0x0
677674
; GFX6-NEXT: s_waitcnt lgkmcnt(0)
678-
; GFX6-NEXT: s_load_dword s0, s[6:7], 0x0
679-
; GFX6-NEXT: s_mov_b32 s6, -1
680-
; GFX6-NEXT: s_mov_b32 s7, 0xf000
681-
; GFX6-NEXT: v_mul_lo_u32 v1, v0, -2
682-
; GFX6-NEXT: s_waitcnt lgkmcnt(0)
683-
; GFX6-NEXT: s_bfe_i32 s0, s0, 0x100001
684-
; GFX6-NEXT: s_ashr_i32 s2, s0, 31
685-
; GFX6-NEXT: v_mul_hi_u32 v1, v0, v1
686-
; GFX6-NEXT: s_add_i32 s0, s0, s2
687-
; GFX6-NEXT: s_xor_b32 s0, s0, s2
688-
; GFX6-NEXT: v_add_i32_e32 v0, vcc, v0, v1
689-
; GFX6-NEXT: v_mul_hi_u32 v0, s0, v0
690-
; GFX6-NEXT: v_lshlrev_b32_e32 v1, 1, v0
691-
; GFX6-NEXT: v_add_i32_e32 v2, vcc, 1, v0
692-
; GFX6-NEXT: v_sub_i32_e32 v1, vcc, s0, v1
693-
; GFX6-NEXT: v_cmp_le_u32_e32 vcc, 2, v1
694-
; GFX6-NEXT: v_cndmask_b32_e32 v0, v0, v2, vcc
695-
; GFX6-NEXT: v_subrev_i32_e64 v2, s[0:1], 2, v1
696-
; GFX6-NEXT: v_cndmask_b32_e32 v1, v1, v2, vcc
697-
; GFX6-NEXT: v_add_i32_e32 v2, vcc, 1, v0
698-
; GFX6-NEXT: v_cmp_le_u32_e32 vcc, 2, v1
699-
; GFX6-NEXT: v_cndmask_b32_e32 v0, v0, v2, vcc
700-
; GFX6-NEXT: v_xor_b32_e32 v0, s2, v0
701-
; GFX6-NEXT: v_subrev_i32_e32 v0, vcc, s2, v0
702-
; GFX6-NEXT: buffer_store_dword v0, off, s[4:7], 0
675+
; GFX6-NEXT: s_load_dword s3, s[2:3], 0x0
676+
; GFX6-NEXT: s_mov_b32 s2, -1
677+
; GFX6-NEXT: s_waitcnt lgkmcnt(0)
678+
; GFX6-NEXT: s_bfe_i32 s3, s3, 0x100001
679+
; GFX6-NEXT: s_ashr_i32 s4, s3, 31
680+
; GFX6-NEXT: s_lshl_b32 s4, s4, 31
681+
; GFX6-NEXT: s_add_i32 s3, s3, s4
682+
; GFX6-NEXT: s_ashr_i32 s3, s3, 1
683+
; GFX6-NEXT: v_mov_b32_e32 v0, s3
684+
; GFX6-NEXT: s_mov_b32 s3, 0xf000
685+
; GFX6-NEXT: buffer_store_dword v0, off, s[0:3], 0
703686
; GFX6-NEXT: s_endpgm
704687
%src = load i32, ptr addrspace(1) %in, align 4
705688
%bfe = call i32 @llvm.amdgcn.sbfe.i32(i32 %src, i32 1, i32 16)

0 commit comments

Comments
 (0)