Skip to content

Commit 08ae945

Browse files
committed
GlobalISel: Copy correct flags to select
This was looking for a compare condition, and copying the compare flags. I don't think this was ever correct outside of certain min/max patterns which aren't checked, but this probably predates select instructions having fast math flags.
1 parent 66224d3 commit 08ae945

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

llvm/lib/CodeGen/GlobalISel/IRTranslator.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1008,10 +1008,8 @@ bool IRTranslator::translateSelect(const User &U,
10081008
ArrayRef<Register> Op1Regs = getOrCreateVRegs(*U.getOperand(2));
10091009

10101010
uint16_t Flags = 0;
1011-
if (const SelectInst *SI = dyn_cast<SelectInst>(&U)) {
1012-
if (const CmpInst *Cmp = dyn_cast<CmpInst>(SI->getCondition()))
1013-
Flags = MachineInstr::copyFlagsFromInstruction(*Cmp);
1014-
}
1011+
if (const SelectInst *SI = dyn_cast<SelectInst>(&U))
1012+
Flags = MachineInstr::copyFlagsFromInstruction(*SI);
10151013

10161014
for (unsigned i = 0; i < ResRegs.size(); ++i) {
10171015
MIRBuilder.buildSelect(ResRegs[i], Tst, Op0Regs[i], Op1Regs[i], Flags);

llvm/test/CodeGen/AArch64/GlobalISel/arm64-irtranslator.ll

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -962,6 +962,31 @@ define i32 @test_select(i1 %tst, i32 %lhs, i32 %rhs) {
962962
ret i32 %res
963963
}
964964

965+
; CHECK-LABEL: name: test_select_flags
966+
; CHECK: [[COPY:%[0-9]+]]:_(s32) = COPY $w0
967+
; CHECK: [[TRUNC:%[0-9]+]]:_(s1) = G_TRUNC [[COPY]](s32)
968+
; CHECK: [[COPY1:%[0-9]+]]:_(s32) = COPY $s0
969+
; CHECK: [[COPY2:%[0-9]+]]:_(s32) = COPY $s1
970+
; CHECK: [[SELECT:%[0-9]+]]:_(s32) = nnan G_SELECT [[TRUNC]](s1), [[COPY1]], [[COPY2]]
971+
define float @test_select_flags(i1 %tst, float %lhs, float %rhs) {
972+
%res = select nnan i1 %tst, float %lhs, float %rhs
973+
ret float %res
974+
}
975+
976+
; Don't take the flags from the compare condition
977+
; CHECK-LABEL: name: test_select_cmp_flags
978+
; CHECK: [[COPY0:%[0-9]+]]:_(s32) = COPY $s0
979+
; CHECK: [[COPY1:%[0-9]+]]:_(s32) = COPY $s1
980+
; CHECK: [[COPY2:%[0-9]+]]:_(s32) = COPY $s2
981+
; CHECK: [[COPY3:%[0-9]+]]:_(s32) = COPY $s3
982+
; CHECK: [[CMP:%[0-9]+]]:_(s1) = nsz G_FCMP floatpred(oeq), [[COPY0]](s32), [[COPY1]]
983+
; CHECK: [[SELECT:%[0-9]+]]:_(s32) = G_SELECT [[CMP]](s1), [[COPY2]], [[COPY3]]
984+
define float @test_select_cmp_flags(float %cmp0, float %cmp1, float %lhs, float %rhs) {
985+
%tst = fcmp nsz oeq float %cmp0, %cmp1
986+
%res = select i1 %tst, float %lhs, float %rhs
987+
ret float %res
988+
}
989+
965990
; CHECK-LABEL: name: test_select_ptr
966991
; CHECK: [[TST_C:%[0-9]+]]:_(s32) = COPY $w0
967992
; CHECK: [[TST:%[0-9]+]]:_(s1) = G_TRUNC [[TST_C]]

0 commit comments

Comments
 (0)