Skip to content

Commit 4b91949

Browse files
author
Thorsten Schütt
authored
[GlobalIsel] Combine selects with constants (#76089)
A first small step at combining selects.
1 parent 2292fd0 commit 4b91949

File tree

16 files changed

+1406
-484
lines changed

16 files changed

+1406
-484
lines changed

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

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -769,9 +769,6 @@ class CombinerHelper {
769769
bool matchCombineFSubFpExtFNegFMulToFMadOrFMA(MachineInstr &MI,
770770
BuildFnTy &MatchInfo);
771771

772-
/// Fold boolean selects to logical operations.
773-
bool matchSelectToLogical(MachineInstr &MI, BuildFnTy &MatchInfo);
774-
775772
bool matchCombineFMinMaxNaN(MachineInstr &MI, unsigned &Info);
776773

777774
/// Transform G_ADD(x, G_SUB(y, x)) to y.
@@ -814,6 +811,9 @@ class CombinerHelper {
814811
// Given a binop \p MI, commute operands 1 and 2.
815812
void applyCommuteBinOpOperands(MachineInstr &MI);
816813

814+
/// Combine selects.
815+
bool matchSelect(MachineInstr &MI, BuildFnTy &MatchInfo);
816+
817817
private:
818818
/// Checks for legality of an indexed variant of \p LdSt.
819819
bool isIndexedLoadStoreLegal(GLoadStore &LdSt) const;
@@ -904,6 +904,18 @@ class CombinerHelper {
904904
/// select (fcmp uge x, 1.0) 1.0, x -> fminnm x, 1.0
905905
bool matchFPSelectToMinMax(Register Dst, Register Cond, Register TrueVal,
906906
Register FalseVal, BuildFnTy &MatchInfo);
907+
908+
/// Try to fold selects to logical operations.
909+
bool tryFoldBoolSelectToLogic(GSelect *Select, BuildFnTy &MatchInfo);
910+
911+
bool tryFoldSelectOfConstants(GSelect *Select, BuildFnTy &MatchInfo);
912+
913+
bool isOneOrOneSplat(Register Src, bool AllowUndefs);
914+
bool isZeroOrZeroSplat(Register Src, bool AllowUndefs);
915+
bool isConstantSplatVector(Register Src, int64_t SplatValue,
916+
bool AllowUndefs);
917+
918+
std::optional<APInt> getConstantOrConstantSplatVector(Register Src);
907919
};
908920
} // namespace llvm
909921

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

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -437,13 +437,6 @@ def select_constant_cmp: GICombineRule<
437437
(apply [{ Helper.replaceSingleDefInstWithOperand(*${root}, ${matchinfo}); }])
438438
>;
439439

440-
def select_to_logical : GICombineRule<
441-
(defs root:$root, build_fn_matchinfo:$matchinfo),
442-
(match (wip_match_opcode G_SELECT):$root,
443-
[{ return Helper.matchSelectToLogical(*${root}, ${matchinfo}); }]),
444-
(apply [{ Helper.applyBuildFn(*${root}, ${matchinfo}); }])
445-
>;
446-
447440
// Fold (C op x) -> (x op C)
448441
// TODO: handle more isCommutable opcodes
449442
// TODO: handle compares (currently not marked as isCommutable)
@@ -1242,6 +1235,12 @@ def select_to_minmax: GICombineRule<
12421235
[{ return Helper.matchSimplifySelectToMinMax(*${root}, ${info}); }]),
12431236
(apply [{ Helper.applyBuildFn(*${root}, ${info}); }])>;
12441237

1238+
def match_selects : GICombineRule<
1239+
(defs root:$root, build_fn_matchinfo:$matchinfo),
1240+
(match (wip_match_opcode G_SELECT):$root,
1241+
[{ return Helper.matchSelect(*${root}, ${matchinfo}); }]),
1242+
(apply [{ Helper.applyBuildFn(*${root}, ${matchinfo}); }])>;
1243+
12451244
// FIXME: These should use the custom predicate feature once it lands.
12461245
def undef_combines : GICombineGroup<[undef_to_fp_zero, undef_to_int_zero,
12471246
undef_to_negative_one,
@@ -1282,7 +1281,7 @@ def width_reduction_combines : GICombineGroup<[reduce_shl_of_extend,
12821281
def phi_combines : GICombineGroup<[extend_through_phis]>;
12831282

12841283
def select_combines : GICombineGroup<[select_undef_cmp, select_constant_cmp,
1285-
select_to_logical]>;
1284+
match_selects]>;
12861285

12871286
def trivial_combines : GICombineGroup<[copy_prop, mul_to_shl, add_p2i_to_ptradd,
12881287
mul_by_neg_one, idempotent_prop]>;

0 commit comments

Comments
 (0)