Skip to content

Commit 699518f

Browse files
committed
[GlobalIsel] Speedup select to integer min/max
llvm#92309
1 parent 4a5dffc commit 699518f

File tree

4 files changed

+78
-65
lines changed

4 files changed

+78
-65
lines changed

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -822,6 +822,9 @@ class CombinerHelper {
822822
// Given a binop \p MI, commute operands 1 and 2.
823823
void applyCommuteBinOpOperands(MachineInstr &MI);
824824

825+
/// Combine select to integer min/max.
826+
bool matchSelectIMinMax(const MachineOperand &MO, BuildFnTy &MatchInfo);
827+
825828
/// Combine selects.
826829
bool matchSelect(MachineInstr &MI, BuildFnTy &MatchInfo);
827830

@@ -962,9 +965,6 @@ class CombinerHelper {
962965

963966
bool tryFoldSelectOfConstants(GSelect *Select, BuildFnTy &MatchInfo);
964967

965-
/// Try to fold (icmp X, Y) ? X : Y -> integer minmax.
966-
bool tryFoldSelectToIntMinMax(GSelect *Select, BuildFnTy &MatchInfo);
967-
968968
bool isOneOrOneSplat(Register Src, bool AllowUndefs);
969969
bool isZeroOrZeroSplat(Register Src, bool AllowUndefs);
970970
bool isConstantSplatVector(Register Src, int64_t SplatValue,

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

+8-1
Original file line numberDiff line numberDiff line change
@@ -1307,6 +1307,13 @@ def select_to_minmax: GICombineRule<
13071307
[{ return Helper.matchSimplifySelectToMinMax(*${root}, ${info}); }]),
13081308
(apply [{ Helper.applyBuildFn(*${root}, ${info}); }])>;
13091309

1310+
def select_to_iminmax: GICombineRule<
1311+
(defs root:$root, build_fn_matchinfo:$info),
1312+
(match (G_ICMP $tst, $tst1, $x, $y),
1313+
(G_SELECT $root, $tst, $x, $y),
1314+
[{ return Helper.matchSelectIMinMax(${root}, ${info}); }]),
1315+
(apply [{ Helper.applyBuildFnMO(${root}, ${info}); }])>;
1316+
13101317
def match_selects : GICombineRule<
13111318
(defs root:$root, build_fn_matchinfo:$matchinfo),
13121319
(match (wip_match_opcode G_SELECT):$root,
@@ -1670,7 +1677,7 @@ def phi_combines : GICombineGroup<[extend_through_phis]>;
16701677
def bitreverse_shift : GICombineGroup<[bitreverse_shl, bitreverse_lshr]>;
16711678

16721679
def select_combines : GICombineGroup<[select_undef_cmp, select_constant_cmp,
1673-
match_selects]>;
1680+
select_to_iminmax, match_selects]>;
16741681

16751682
def trivial_combines : GICombineGroup<[copy_prop, mul_to_shl, add_p2i_to_ptradd,
16761683
mul_by_neg_one, idempotent_prop]>;

llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp

+39-61
Original file line numberDiff line numberDiff line change
@@ -6749,22 +6749,19 @@ bool CombinerHelper::tryFoldBoolSelectToLogic(GSelect *Select,
67496749
return false;
67506750
}
67516751

6752-
bool CombinerHelper::tryFoldSelectToIntMinMax(GSelect *Select,
6753-
BuildFnTy &MatchInfo) {
6752+
bool CombinerHelper::matchSelectIMinMax(const MachineOperand &MO,
6753+
BuildFnTy &MatchInfo) {
6754+
GSelect *Select = cast<GSelect>(MRI.getVRegDef(MO.getReg()));
6755+
GICmp *Cmp = cast<GICmp>(MRI.getVRegDef(Select->getCondReg()));
6756+
67546757
Register DstReg = Select->getReg(0);
6755-
Register Cond = Select->getCondReg();
67566758
Register True = Select->getTrueReg();
67576759
Register False = Select->getFalseReg();
67586760
LLT DstTy = MRI.getType(DstReg);
67596761

67606762
if (DstTy.isPointer())
67616763
return false;
67626764

6763-
// We need an G_ICMP on the condition register.
6764-
GICmp *Cmp = getOpcodeDef<GICmp>(Cond, MRI);
6765-
if (!Cmp)
6766-
return false;
6767-
67686765
// We want to fold the icmp and replace the select.
67696766
if (!MRI.hasOneNonDBGUse(Cmp->getReg(0)))
67706767
return false;
@@ -6775,62 +6772,46 @@ bool CombinerHelper::tryFoldSelectToIntMinMax(GSelect *Select,
67756772
if (CmpInst::isEquality(Pred))
67766773
return false;
67776774

6778-
Register CmpLHS = Cmp->getLHSReg();
6779-
Register CmpRHS = Cmp->getRHSReg();
6780-
6781-
// We can swap CmpLHS and CmpRHS for higher hitrate.
6782-
if (True == CmpRHS && False == CmpLHS) {
6783-
std::swap(CmpLHS, CmpRHS);
6784-
Pred = CmpInst::getSwappedPredicate(Pred);
6785-
}
6775+
[[maybe_unused]] Register CmpLHS = Cmp->getLHSReg();
6776+
[[maybe_unused]] Register CmpRHS = Cmp->getRHSReg();
67866777

67876778
// (icmp X, Y) ? X : Y -> integer minmax.
67886779
// see matchSelectPattern in ValueTracking.
67896780
// Legality between G_SELECT and integer minmax can differ.
6790-
if (True == CmpLHS && False == CmpRHS) {
6791-
switch (Pred) {
6792-
case ICmpInst::ICMP_UGT:
6793-
case ICmpInst::ICMP_UGE: {
6794-
if (!isLegalOrBeforeLegalizer({TargetOpcode::G_UMAX, DstTy}))
6795-
return false;
6796-
MatchInfo = [=](MachineIRBuilder &B) {
6797-
B.buildUMax(DstReg, True, False);
6798-
};
6799-
return true;
6800-
}
6801-
case ICmpInst::ICMP_SGT:
6802-
case ICmpInst::ICMP_SGE: {
6803-
if (!isLegalOrBeforeLegalizer({TargetOpcode::G_SMAX, DstTy}))
6804-
return false;
6805-
MatchInfo = [=](MachineIRBuilder &B) {
6806-
B.buildSMax(DstReg, True, False);
6807-
};
6808-
return true;
6809-
}
6810-
case ICmpInst::ICMP_ULT:
6811-
case ICmpInst::ICMP_ULE: {
6812-
if (!isLegalOrBeforeLegalizer({TargetOpcode::G_UMIN, DstTy}))
6813-
return false;
6814-
MatchInfo = [=](MachineIRBuilder &B) {
6815-
B.buildUMin(DstReg, True, False);
6816-
};
6817-
return true;
6818-
}
6819-
case ICmpInst::ICMP_SLT:
6820-
case ICmpInst::ICMP_SLE: {
6821-
if (!isLegalOrBeforeLegalizer({TargetOpcode::G_SMIN, DstTy}))
6822-
return false;
6823-
MatchInfo = [=](MachineIRBuilder &B) {
6824-
B.buildSMin(DstReg, True, False);
6825-
};
6826-
return true;
6827-
}
6828-
default:
6781+
assert(True == CmpLHS && False == CmpRHS && "unexpected MIR pattern");
6782+
6783+
switch (Pred) {
6784+
case ICmpInst::ICMP_UGT:
6785+
case ICmpInst::ICMP_UGE: {
6786+
if (!isLegalOrBeforeLegalizer({TargetOpcode::G_UMAX, DstTy}))
68296787
return false;
6830-
}
6788+
MatchInfo = [=](MachineIRBuilder &B) { B.buildUMax(DstReg, True, False); };
6789+
return true;
6790+
}
6791+
case ICmpInst::ICMP_SGT:
6792+
case ICmpInst::ICMP_SGE: {
6793+
if (!isLegalOrBeforeLegalizer({TargetOpcode::G_SMAX, DstTy}))
6794+
return false;
6795+
MatchInfo = [=](MachineIRBuilder &B) { B.buildSMax(DstReg, True, False); };
6796+
return true;
6797+
}
6798+
case ICmpInst::ICMP_ULT:
6799+
case ICmpInst::ICMP_ULE: {
6800+
if (!isLegalOrBeforeLegalizer({TargetOpcode::G_UMIN, DstTy}))
6801+
return false;
6802+
MatchInfo = [=](MachineIRBuilder &B) { B.buildUMin(DstReg, True, False); };
6803+
return true;
6804+
}
6805+
case ICmpInst::ICMP_SLT:
6806+
case ICmpInst::ICMP_SLE: {
6807+
if (!isLegalOrBeforeLegalizer({TargetOpcode::G_SMIN, DstTy}))
6808+
return false;
6809+
MatchInfo = [=](MachineIRBuilder &B) { B.buildSMin(DstReg, True, False); };
6810+
return true;
6811+
}
6812+
default:
6813+
return false;
68316814
}
6832-
6833-
return false;
68346815
}
68356816

68366817
bool CombinerHelper::matchSelect(MachineInstr &MI, BuildFnTy &MatchInfo) {
@@ -6842,9 +6823,6 @@ bool CombinerHelper::matchSelect(MachineInstr &MI, BuildFnTy &MatchInfo) {
68426823
if (tryFoldBoolSelectToLogic(Select, MatchInfo))
68436824
return true;
68446825

6845-
if (tryFoldSelectToIntMinMax(Select, MatchInfo))
6846-
return true;
6847-
68486826
return false;
68496827
}
68506828

llvm/test/CodeGen/AArch64/GlobalISel/combine-select.mir

+28
Original file line numberDiff line numberDiff line change
@@ -896,3 +896,31 @@ body: |
896896
RET_ReallyLR
897897
898898
...
899+
---
900+
# test failed_select icmp_sle f,t_t_f --> smin(t,f)
901+
name: select_icmp_sle_f_t_t_f_smin_t_f
902+
body: |
903+
bb.1:
904+
liveins: $x0, $x1, $x2
905+
; CHECK-LABEL: name: select_icmp_sle_f_t_t_f_smin_t_f
906+
; CHECK: liveins: $x0, $x1, $x2
907+
; CHECK-NEXT: {{ $}}
908+
; CHECK-NEXT: [[COPY:%[0-9]+]]:_(s64) = COPY $x0
909+
; CHECK-NEXT: [[COPY1:%[0-9]+]]:_(s64) = COPY $x1
910+
; CHECK-NEXT: %t1:_(s32) = G_TRUNC [[COPY]](s64)
911+
; CHECK-NEXT: %f1:_(s32) = G_TRUNC [[COPY1]](s64)
912+
; CHECK-NEXT: %t:_(<4 x s32>) = G_BUILD_VECTOR %t1(s32), %t1(s32), %t1(s32), %t1(s32)
913+
; CHECK-NEXT: %f:_(<4 x s32>) = G_BUILD_VECTOR %f1(s32), %f1(s32), %f1(s32), %f1(s32)
914+
; CHECK-NEXT: %c:_(<4 x s32>) = G_ICMP intpred(sle), %f(<4 x s32>), %t
915+
; CHECK-NEXT: %sel:_(<4 x s32>) = exact G_SELECT %c(<4 x s32>), %t, %f
916+
; CHECK-NEXT: $q0 = COPY %sel(<4 x s32>)
917+
%0:_(s64) = COPY $x0
918+
%1:_(s64) = COPY $x1
919+
%t1:_(s32) = G_TRUNC %0
920+
%f1:_(s32) = G_TRUNC %1
921+
%t:_(<4 x s32>) = G_BUILD_VECTOR %t1, %t1, %t1, %t1
922+
%f:_(<4 x s32>) = G_BUILD_VECTOR %f1, %f1, %f1, %f1
923+
%c:_(<4 x s32>) = G_ICMP intpred(sle), %f(<4 x s32>), %t(<4 x s32>)
924+
%sel:_(<4 x s32>) = exact G_SELECT %c, %t, %f
925+
$q0 = COPY %sel(<4 x s32>)
926+
...

0 commit comments

Comments
 (0)