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 Acked-by: Uros Bizjak <[email protected]> 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. Changes since v2: - function.cc: no comma operators, as requested in the v2 review. offset is set to cfa_offset once, before the #ifdef, and the DRAP arm subtracts in_arg_offset from it; the FRAME_POINTER_CFA_OFFSET arm is now unchanged from trunk. No change in generated code. - Added Uros's Acked-by from the v2 review. gcc/function.cc | 15 +++++++- .../gcc.dg/torture/stackalign/dwarf-cfa-1.c | 38 +++++++++++++++++++ 2 files changed, 51 insertions(+), 2 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..c770b2e1549 100644 --- a/gcc/function.cc +++ b/gcc/function.cc @@ -1538,12 +1538,23 @@ instantiate_new_reg (rtx x, poly_int64 *poffset) new_rtx = stack_pointer_rtx, offset = out_arg_offset; else if (x == virtual_cfa_rtx) { + offset = cfa_offset; #ifdef FRAME_POINTER_CFA_OFFSET new_rtx = frame_pointer_rtx; #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 -= in_arg_offset; + } + else + new_rtx = arg_pointer_rtx; #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
