[PATCH][expr.c] PR 65358 Avoid clobbering partial argument during sibcall

Kyrill Tkachov kyrylo.tkachov@arm.com
Mon Apr 20 08:25:00 GMT 2015


Hi Jeff,

On 17/04/15 18:26, Jeff Law wrote:
> On 03/19/2015 08:39 AM, Kyrill Tkachov wrote:
>> Hi all,
>>
>> This patch fixes PR 65358. For details look at the excellent write-up
>> by Honggyu in bugzilla. The problem is that we're trying to pass a struct
>> partially on the stack and partially in regs during a tail-call
>> optimisation
>> but the struct we're passing is also a partial incoming arg though the
>> split
>> between stack and regs is different from its outgoing usage.
>>
>> The emit_push_insn code ends up doing a block move for the on-stack part
>> but
>> ends up overwriting the part that needs to be loaded into regs.
>> My first thought was to just load the regs part first and then do the stack
>> part but that doesn't work as multiple comments in that function indicate
>> (the block move being expanded to movmem or other functions being one of
>> the
>> reasons).
>>
>> My proposed solution is to detect when the overlap happens, find the
>> overlapping region and load it before the stack pushing into pseudos and
>> after the stack pushing is done move the overlapping values from the
>> pseudos
>> into the hard argument regs that they're supposed to go.
>>
>> That way this new functionality should only ever be triggered when there's
>> the overlap in this PR (causing wrong-code) and shouldn't affect codegen
>> anywhere else.
>>
>> Bootstrapped and tested on arm-none-linux-gnueabihf, aarch64-none-linux-gnu
>> and x86_64-linux-gnu.
>>
>> According to the PR this appears at least as far back 4.6 so this isn't a
>> regression on the release branches, but it is a wrong-code bug.
>>
>> I'll let Honggyu upstream the testcase separately
>> (https://gcc.gnu.org/ml/gcc-patches/2015-03/msg00984.html)
>>
>> I'll be testing this on the 4.8 and 4.9 branches.
>> Thoughts on this approach?
>>
>> Thanks,
>> Kyrill
>>
>> 2015-03-19  Kyrylo Tkachov <kyrylo.tkachov@arm.com>
>>
>>       PR middle-end/65358
>>       * expr.c (memory_load_overlap): New function.
>>       (emit_push_insn): When pushing partial args to the stack would
>>       clobber the register part load the overlapping part into a pseudo
>>       and put it into the hard reg after pushing.
>>
>> expr.patch
>>
>>
>> commit 490c5f2074d76a2927afaea99e4dd0bacccb413c
>> Author: Kyrylo Tkachov<kyrylo.tkachov@arm.com>
>> Date:   Wed Mar 18 13:42:37 2015 +0000
>>
>>       [expr.c] PR 65358 Avoid clobbering partial argument during sibcall
>>
>> diff --git a/gcc/expr.c b/gcc/expr.c
>> index dc13a14..d3b9156 100644
>> --- a/gcc/expr.c
>> +++ b/gcc/expr.c
>> @@ -4121,6 +4121,25 @@ emit_single_push_insn (machine_mode mode, rtx x, tree type)
>>    }
>>    #endif
>>
>> +/* Add SIZE to X and check whether it's greater than Y.
>> +   If it is, return the constant amount by which it's greater or smaller.
>> +   If the two are not statically comparable (for example, X and Y contain
>> +   different registers) return -1.  This is used in expand_push_insn to
>> +   figure out if reading SIZE bytes from location X will end up reading from
>> +   location Y.  */
>> +
>> +static int
>> +memory_load_overlap (rtx x, rtx y, HOST_WIDE_INT size)
>> +{
>> +  rtx tmp = plus_constant (Pmode, x, size);
>> +  rtx sub = simplify_gen_binary (MINUS, Pmode, tmp, y);
>> +
>> +  if (!CONST_INT_P (sub))
>> +    return -1;
>> +
>> +  return INTVAL (sub);
>> +}
> Hmmm, so what happens if the difference is < 0?   I'd be a bit worried
> about that case for the PA (for example).
>
> So how about asserting that the INTVAL is >= 0 prior to returning so
> that we catch that case if it ever occurs?

INTVAL being >= 0 is the case that I want to catch with this function.
INTVAL <0 is the usual case on leaf call optimisation. On arm, at least,
it means that x and y use the same base register (i.e. same stack frame)
but the offsets are such that reading SIZE bytes from X will not overlap
with Y, thus not requiring the workaround in this patch.
Thus, asserting that the result is positive is not right here.

What characteristic on pa makes this problematic? Is it the STACK_GROWS_UPWARD?
Should I then extend this function to do something like:

HOST_WIDE_INT res = INTVAL (sub);
#ifndef STACK_GROWS_DOWNWARD
res = -res;
#endif

return res?



>
> OK for the trunk with the added assert.  Please commit the testcase from
> Honggyu at the same time you commit the patch.

Thanks, will do after the above is resolved.

Kyrill

>
> Let's let it simmer for a while on the trunk before considering it to be
> backported.
>
> jeff
>



More information about the Gcc-patches mailing list