Skip to content

Commit 14c2cf8

Browse files
committed
[AArch64][GlobalISel] Don't bail out of the select(cmp(a, b)) -> csel optimization with multiple users.
It can still be beneficial to do the optimization if the result of the compare is used by *another* select. Differential Revision: https://reviews.llvm.org/D73511
1 parent 5547919 commit 14c2cf8

File tree

2 files changed

+47
-3
lines changed

2 files changed

+47
-3
lines changed

llvm/lib/Target/AArch64/AArch64InstructionSelector.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3508,8 +3508,17 @@ bool AArch64InstructionSelector::tryOptSelect(MachineInstr &I) const {
35083508
MachineInstr *CondDef = MRI.getVRegDef(I.getOperand(1).getReg());
35093509
while (CondDef) {
35103510
// We can only fold if all of the defs have one use.
3511-
if (!MRI.hasOneUse(CondDef->getOperand(0).getReg()))
3512-
return false;
3511+
Register CondDefReg = CondDef->getOperand(0).getReg();
3512+
if (!MRI.hasOneUse(CondDefReg)) {
3513+
// Unless it's another select.
3514+
for (auto UI = MRI.use_instr_begin(CondDefReg), UE = MRI.use_instr_end();
3515+
UI != UE; ++UI) {
3516+
if (CondDef == &*UI)
3517+
continue;
3518+
if (UI->getOpcode() != TargetOpcode::G_SELECT)
3519+
return false;
3520+
}
3521+
}
35133522

35143523
// We can skip over G_TRUNC since the condition is 1-bit.
35153524
// Truncating/extending can have no impact on the value.

llvm/test/CodeGen/AArch64/GlobalISel/fold-fp-select.mir

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# Verify the following:
55
#
66
# - We can fold compares into selects.
7-
# - This only happens when the result of the compare is only used by the select.
7+
# - This only happens when the result of the compare is only used by selects.
88
#
99
# Also verify that, for now:
1010
#
@@ -47,6 +47,41 @@ body: |
4747
$s0 = COPY %4(s32)
4848
RET_ReallyLR implicit $s0
4949
50+
...
51+
---
52+
name: fcmp_more_than_one_select
53+
alignment: 4
54+
legalized: true
55+
regBankSelected: true
56+
tracksRegLiveness: true
57+
body: |
58+
bb.0:
59+
liveins: $s0, $s1, $w1
60+
61+
; CHECK-LABEL: name: fcmp_more_than_one_select
62+
; CHECK: liveins: $s0, $s1, $w1
63+
; CHECK: [[COPY:%[0-9]+]]:fpr32 = COPY $s0
64+
; CHECK: [[COPY1:%[0-9]+]]:fpr32 = COPY $s1
65+
; CHECK: [[FMOVS0_:%[0-9]+]]:fpr32 = FMOVS0
66+
; CHECK: FCMPSri [[COPY]], implicit-def $nzcv
67+
; CHECK: [[FCSELSrrr:%[0-9]+]]:fpr32 = FCSELSrrr [[FMOVS0_]], [[COPY1]], 0, implicit $nzcv
68+
; CHECK: FCMPSri [[COPY]], implicit-def $nzcv
69+
; CHECK: [[FCSELSrrr1:%[0-9]+]]:fpr32 = FCSELSrrr [[COPY1]], [[FMOVS0_]], 0, implicit $nzcv
70+
; CHECK: $s0 = COPY [[FCSELSrrr]]
71+
; CHECK: $s1 = COPY [[FCSELSrrr1]]
72+
; CHECK: RET_ReallyLR implicit $s0
73+
%0:fpr(s32) = COPY $s0
74+
%1:fpr(s32) = COPY $s1
75+
%2:fpr(s32) = G_FCONSTANT float 0.000000e+00
76+
%5:gpr(s32) = G_FCMP floatpred(oeq), %0(s32), %2
77+
%3:gpr(s1) = G_TRUNC %5(s32)
78+
%6:fpr(s1) = COPY %3(s1)
79+
%4:fpr(s32) = G_SELECT %6(s1), %2, %1
80+
%7:fpr(s32) = G_SELECT %6(s1), %1, %2
81+
$s0 = COPY %4(s32)
82+
$s1 = COPY %7(s32)
83+
RET_ReallyLR implicit $s0
84+
5085
...
5186
---
5287
name: using_icmp

0 commit comments

Comments
 (0)