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: the DRAP holds arg_pointer_rtx +
in_arg_offset. Do the same for virtual_cfa_rtx, at offset
cfa_offset - in_arg_offset. On x86, the only target that currently
reaches this path, in_arg_offset is 0; subtracting it keeps the
instantiation correct for a target where FIRST_PARM_OFFSET is not 0.
The FRAME_POINTER_CFA_OFFSET branch is left alone. Of the targets that
define it, only nvptx also provides a DRAP (TARGET_GET_DRAP_RTX), and
there is no CFA-based unwinding on PTX to check it against.
dwarf-cfa-1.c compares the CFA seen by a realigned and a plain function
called from the same frame. Without this change it aborts on x86_64
and i686 whenever the frame is realigned with a DRAP: in the
-mforce-drap variants of stackalign.exp, and with -m32 unless
-maccumulate-outgoing-args is given.
Assisted-by: Claude Opus 5.5
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]>
---
Not fully regtested: verdict for 06a7392e236e is stale.
Changes since v1:
v2 addresses review comments on v1 received off-list.
- No change in generated code; v1 and v2 give identical assembly for
the testcase across -m32/-m64, -O0/-O2/-Os, with and without
-mforce-drap.
- function.cc: the DRAP arm now uses braces and separate statements, as
the virtual_incoming_args_rtx arm above it does. The comment says
that the DRAP pseudo holds arg_pointer_rtx + in_arg_offset, hence the
cfa_offset - in_arg_offset.
- Commit message: in_arg_offset is 0 on x86, the only target that
currently reaches this path, so the subtraction is for targets where
FIRST_PARM_OFFSET is not 0. The FRAME_POINTER_CFA_OFFSET branch is
left alone on purpose: of the targets that define it only nvptx also
has a DRAP, and PTX has no CFA-based unwinding to check against.
- dwarf-cfa-1.c: comment explaining why the two CFAs must be equal and
which variants use a DRAP. stackalign.exp already adds -mforce-drap
(and -mforce-drap -fpic) variants on targets with automatic stack
alignment: in the v1 regtest, 14 of the 28 runs of the test on each
board (unix and unix/-m32) used -mforce-drap (-O0, -O1, -O2, -O3 -g,
-Os and both -flto variants, each with and without -fpic), and all of
these abort without the patch. No dg-additional-options
"-mforce-drap" there, so the torture runs keep covering frames
without a DRAP as well.
- Commit message: Assisted-by: Claude Opus 5.5.
Testcase before (gcc 16.2) and after (this patch) on x86_64-linux;
every configuration that realigns with a DRAP aborts before and passes
after, the others pass both times:
-m64 / -m32, -mforce-drap abort -> pass
-m32, -mno-accumulate-outgoing-args (DRAP) abort -> pass
-m32, -maccumulate-outgoing-args (no DRAP) pass -> pass
-m64, no -mforce-drap (no DRAP) pass -> pass
(each at -O0 and -O2).
realigned () on x86_64 at -O2 -mforce-drap with the patch; the CFA now
comes from the DRAP (%r10), where before it was 16(%rbp):
leaq 8(%rsp), %r10
andq $-64, %rsp
pushq -8(%r10)
pushq %rbp
movq %rsp, %rbp
pushq %r10
pushq %rbx
leaq -112(%rbp), %rdi
movq %r10, %rbx
subq $96, %rsp
call sink
addq $96, %rsp
movq %rbx, %rax
popq %rbx
popq %r10
popq %rbp
leaq -8(%r10), %rsp
ret
v1 was bootstrapped and regtested on
x86_64-pc-linux-gnu (-m32/-m64) with no new regressions; for v2
stackalign.exp was rerun on both boards with no failures.
gcc/function.cc | 16 ++++++--
.../gcc.dg/torture/stackalign/dwarf-cfa-1.c | 38 +++++++++++++++++++
2 files changed, 51 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..21753d56484 100644
--- a/gcc/function.cc
+++ b/gcc/function.cc
@@ -1539,11 +1539,21 @@ 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;
+ if (stack_realign_drap)
+ {
+ /* As for virtual_incoming_args_rtx: if DRAP is used to realign
+ the stack, the argument pointer is at no fixed offset from SP
+ or the hard frame pointer. The DRAP pseudo holds the address
+ of the incoming arguments (arg_pointer_rtx + in_arg_offset),
+ so the CFA is at cfa_offset - in_arg_offset from it. */
+ 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..78c95021522
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/torture/stackalign/dwarf-cfa-1.c
@@ -0,0 +1,38 @@
+/* __builtin_dwarf_cfa must give the CFA even in a function that realigns
+ its frame. PLAIN and REALIGNED take no arguments and are called from
+ the same frame of MAIN, so both must see the same CFA: the stack
+ pointer before the call. On x86 REALIGNED is realigned with a DRAP
+ in the -mforce-drap variants that stackalign.exp adds, and also by
+ default with -m32. */
+
+/* { 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