OSDN Git Service

GlobalISel: Copy correct flags to select
authorMatt Arsenault <Matthew.Arsenault@amd.com>
Tue, 19 May 2020 13:27:25 +0000 (09:27 -0400)
committerMatt Arsenault <Matthew.Arsenault@amd.com>
Tue, 19 May 2020 22:31:24 +0000 (18:31 -0400)
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.

llvm/lib/CodeGen/GlobalISel/IRTranslator.cpp
llvm/test/CodeGen/AArch64/GlobalISel/arm64-irtranslator.ll

index d749c0d..eba352a 100644 (file)
@@ -1008,10 +1008,8 @@ bool IRTranslator::translateSelect(const User &U,
   ArrayRef<Register> Op1Regs = getOrCreateVRegs(*U.getOperand(2));
 
   uint16_t Flags = 0;
-  if (const SelectInst *SI = dyn_cast<SelectInst>(&U)) {
-    if (const CmpInst *Cmp = dyn_cast<CmpInst>(SI->getCondition()))
-      Flags = MachineInstr::copyFlagsFromInstruction(*Cmp);
-  }
+  if (const SelectInst *SI = dyn_cast<SelectInst>(&U))
+    Flags = MachineInstr::copyFlagsFromInstruction(*SI);
 
   for (unsigned i = 0; i < ResRegs.size(); ++i) {
     MIRBuilder.buildSelect(ResRegs[i], Tst, Op0Regs[i], Op1Regs[i], Flags);
index e1444c8..8e312d8 100644 (file)
@@ -962,6 +962,31 @@ define i32 @test_select(i1 %tst, i32 %lhs, i32 %rhs) {
   ret i32 %res
 }
 
+; CHECK-LABEL: name: test_select_flags
+; CHECK:   [[COPY:%[0-9]+]]:_(s32) = COPY $w0
+; CHECK:   [[TRUNC:%[0-9]+]]:_(s1) = G_TRUNC [[COPY]](s32)
+; CHECK:   [[COPY1:%[0-9]+]]:_(s32) = COPY $s0
+; CHECK:   [[COPY2:%[0-9]+]]:_(s32) = COPY $s1
+; CHECK:   [[SELECT:%[0-9]+]]:_(s32) = nnan G_SELECT [[TRUNC]](s1), [[COPY1]], [[COPY2]]
+define float @test_select_flags(i1 %tst, float %lhs, float %rhs) {
+  %res = select nnan i1 %tst, float %lhs, float %rhs
+  ret float %res
+}
+
+; Don't take the flags from the compare condition
+; CHECK-LABEL: name: test_select_cmp_flags
+; CHECK:   [[COPY0:%[0-9]+]]:_(s32) = COPY $s0
+; CHECK:   [[COPY1:%[0-9]+]]:_(s32) = COPY $s1
+; CHECK:   [[COPY2:%[0-9]+]]:_(s32) = COPY $s2
+; CHECK:   [[COPY3:%[0-9]+]]:_(s32) = COPY $s3
+; CHECK:   [[CMP:%[0-9]+]]:_(s1) = nsz G_FCMP floatpred(oeq), [[COPY0]](s32), [[COPY1]]
+; CHECK:   [[SELECT:%[0-9]+]]:_(s32) = G_SELECT [[CMP]](s1), [[COPY2]], [[COPY3]]
+define float @test_select_cmp_flags(float %cmp0, float %cmp1, float %lhs, float %rhs) {
+  %tst = fcmp nsz oeq float %cmp0, %cmp1
+  %res = select i1 %tst, float %lhs, float %rhs
+  ret float %res
+}
+
 ; CHECK-LABEL: name: test_select_ptr
 ; CHECK: [[TST_C:%[0-9]+]]:_(s32) = COPY $w0
 ; CHECK: [[TST:%[0-9]+]]:_(s1) = G_TRUNC [[TST_C]]