[PATCH] D57644: [X86] Add ST0 as an implicit def/use of x87 load/store instructions during FP stackifying.

Craig Topper via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sat Feb 2 19:19:18 PST 2019


craig.topper created this revision.
craig.topper added reviewers: RKSimon, spatel, rnk.
Herald added a project: LLVM.

These instructions implicitly operate on ST0, but we don't currently add that information to the MachineInstr. We also don't add it the tablegen definitions either.

For the most part this doesn't cause any problems because the stackifying occurs after register allocation. All the instructions are marked as having side effects so the postRA scheduler won't reorder them amongst themselves.

But nothing stops inline assembly using X87 instructions from being reordered around other x87 instructions if that inline assembly wasn't marked volatile.

The two test cases I've identified so far in PR40539 involve loads and stores used to set up the inline assembly or capture the results of the inline assembly ending up in the wrong order.

This patch adds implicit ST0 uses/defs to the load/store instructions to prevent this from happening.

I plan to fix all of the FP instructions, but the binops are bit trickier to get right. So I've chosen fixing the known test cases as a good first step.

I think we also need to update the tablegen descriptions so MS inline assembly infers the right clobbers, but I haven't checked that yet.


Repository:
  rL LLVM

https://reviews.llvm.org/D57644

Files:
  lib/Target/X86/X86FloatingPoint.cpp
  test/CodeGen/X86/pr40539.ll


Index: test/CodeGen/X86/pr40539.ll
===================================================================
--- test/CodeGen/X86/pr40539.ll
+++ test/CodeGen/X86/pr40539.ll
@@ -1,9 +1,6 @@
 ; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
 ; RUN: llc < %s -mtriple=i686-unknown-unknown -mcpu=pentium4 | FileCheck %s
 
-; FIXME: The fstps in the following test case should be between the inline
-; assembly expansion and the cmpeqss. The postRA scheduler has rearranged them.
-
 @f1 = global float 1.000000e+00, align 4
 
 define zeroext i1 @_Z9test_log2v() {
@@ -12,13 +9,13 @@
 ; CHECK-NEXT:    pushl %eax
 ; CHECK-NEXT:    .cfi_def_cfa_offset 8
 ; CHECK-NEXT:    flds f1
-; CHECK-NEXT:    fstps (%esp)
-; CHECK-NEXT:    xorps %xmm0, %xmm0
 ; CHECK-NEXT:    #APP
 ; CHECK-NEXT:    fld1
 ; CHECK-NEXT:    fxch %st(1)
 ; CHECK-NEXT:    fyl2x
 ; CHECK-NEXT:    #NO_APP
+; CHECK-NEXT:    fstps (%esp)
+; CHECK-NEXT:    xorps %xmm0, %xmm0
 ; CHECK-NEXT:    cmpeqss (%esp), %xmm0
 ; CHECK-NEXT:    movd %xmm0, %eax
 ; CHECK-NEXT:    andl $1, %eax
@@ -44,12 +41,12 @@
 ; CHECK-NEXT:    .cfi_def_cfa_offset 12
 ; CHECK-NEXT:    movss {{.*#+}} xmm0 = mem[0],zero,zero,zero
 ; CHECK-NEXT:    movss {{.*#+}} xmm1 = mem[0],zero,zero,zero
-; CHECK-NEXT:    #APP
-; CHECK-NEXT:    fcos
-; CHECK-NEXT:    #NO_APP
 ; CHECK-NEXT:    divss {{\.LCPI.*}}, %xmm0
 ; CHECK-NEXT:    movss %xmm0, {{[0-9]+}}(%esp)
 ; CHECK-NEXT:    flds {{[0-9]+}}(%esp)
+; CHECK-NEXT:    #APP
+; CHECK-NEXT:    fcos
+; CHECK-NEXT:    #NO_APP
 ; CHECK-NEXT:    fstps (%esp)
 ; CHECK-NEXT:    movss {{.*#+}} xmm0 = mem[0],zero,zero,zero
 ; CHECK-NEXT:    ucomiss %xmm0, %xmm1
Index: lib/Target/X86/X86FloatingPoint.cpp
===================================================================
--- lib/Target/X86/X86FloatingPoint.cpp
+++ lib/Target/X86/X86FloatingPoint.cpp
@@ -1095,6 +1095,8 @@
   // Change from the pseudo instruction to the concrete instruction.
   MI.RemoveOperand(0); // Remove the explicit ST(0) operand
   MI.setDesc(TII->get(getConcreteOpcode(MI.getOpcode())));
+  MI.addOperand(
+      MachineOperand::CreateReg(X86::ST0, /*isDef*/ true, /*isImp*/ true));
 
   // Result gets pushed on the stack.
   pushReg(DestReg);
@@ -1139,6 +1141,8 @@
   // Convert from the pseudo instruction to the concrete instruction.
   MI.RemoveOperand(NumOps - 1); // Remove explicit ST(0) operand
   MI.setDesc(TII->get(getConcreteOpcode(MI.getOpcode())));
+  MI.addOperand(
+      MachineOperand::CreateReg(X86::ST0, /*isDef*/ false, /*isImp*/ true));
 
   if (MI.getOpcode() == X86::IST_FP64m || MI.getOpcode() == X86::ISTT_FP16m ||
       MI.getOpcode() == X86::ISTT_FP32m || MI.getOpcode() == X86::ISTT_FP64m ||


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D57644.184925.patch
Type: text/x-patch
Size: 2697 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190203/e466e16a/attachment.bin>


More information about the llvm-commits mailing list