instantiate_new_reg replaced virtual_cfa_rtx with the argument pointer
plus cfa_offset.  In a frame realigned with a dynamic realign argument
pointer (DRAP) the argument pointer is at no fixed offset from SP or the
hard frame pointer.  On x86 it is eliminated to a fake location, the copy
of the return address pushed after realignment, so __builtin_dwarf_cfa
returns the wrong address.  On a target that doesn't push such a copy it
can't be eliminated at all.

virtual_incoming_args_rtx already uses crtl->args.internal_arg_pointer,
the DRAP pseudo, in this case.  Do the same for virtual_cfa_rtx,
adjusting the offset by in_arg_offset.

dwarf-cfa-1.c compares the CFA seen by a realigned and a plain function
called from the same place.  Without this change it aborts on
x86_64-linux and i686-linux with -mforce-drap.

gcc/ChangeLog:

        PR middle-end/128611
        * function.cc (instantiate_new_reg): Instantiate virtual_cfa_rtx
        from the DRAP pseudo in a realigned frame.

gcc/testsuite/ChangeLog:

        PR middle-end/128611
        * gcc.dg/torture/stackalign/dwarf-cfa-1.c: New test.

Signed-off-by: Igor Shevlyakov <[email protected]>
---
Bootstrapped and regtested on x86_64-pc-linux-gnu (-m32/-m64), no new 
regressions.

 gcc/function.cc                               | 12 +++++--
 .../gcc.dg/torture/stackalign/dwarf-cfa-1.c   | 34 +++++++++++++++++++
 2 files changed, 43 insertions(+), 3 deletions(-)
 create mode 100644 gcc/testsuite/gcc.dg/torture/stackalign/dwarf-cfa-1.c

diff --git a/gcc/function.cc b/gcc/function.cc
index 0ff9f676b81..a8b801562aa 100644
--- a/gcc/function.cc
+++ b/gcc/function.cc
@@ -1539,11 +1539,17 @@ instantiate_new_reg (rtx x, poly_int64 *poffset)
   else if (x == virtual_cfa_rtx)
     {
 #ifdef FRAME_POINTER_CFA_OFFSET
-      new_rtx = frame_pointer_rtx;
+      new_rtx = frame_pointer_rtx, offset = cfa_offset;
 #else
-      new_rtx = arg_pointer_rtx;
+      /* As for virtual_incoming_args_rtx: in a realigned frame the
+        argument pointer is at no fixed offset from SP or the hard frame
+        pointer, but the DRAP pseudo holds the incoming arguments address.  */
+      if (stack_realign_drap)
+       new_rtx = crtl->args.internal_arg_pointer,
+       offset = cfa_offset - in_arg_offset;
+      else
+       new_rtx = arg_pointer_rtx, offset = cfa_offset;
 #endif
-      offset = cfa_offset;
     }
   else if (x == virtual_preferred_stack_boundary_rtx)
     {
diff --git a/gcc/testsuite/gcc.dg/torture/stackalign/dwarf-cfa-1.c 
b/gcc/testsuite/gcc.dg/torture/stackalign/dwarf-cfa-1.c
new file mode 100644
index 00000000000..fa2e10fcf7d
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/torture/stackalign/dwarf-cfa-1.c
@@ -0,0 +1,34 @@
+/* __builtin_dwarf_cfa must give the CFA even in a function that realigns
+   its frame.  */
+
+/* { dg-do run } */
+
+extern void abort (void);
+
+__attribute__ ((noipa)) void
+sink (volatile char *p)
+{
+  p[0] = 0;
+}
+
+__attribute__ ((noipa)) void *
+plain (void)
+{
+  return __builtin_dwarf_cfa ();
+}
+
+__attribute__ ((noipa)) void *
+realigned (void)
+{
+  volatile char buf[64] __attribute__ ((aligned (64)));
+  sink (buf);
+  return __builtin_dwarf_cfa ();
+}
+
+int
+main (void)
+{
+  if (plain () != realigned ())
+    abort ();
+  return 0;
+}
-- 
2.55.0

Reply via email to