https://gcc.gnu.org/bugzilla/show_bug.cgi?id=67226
Bug ID: 67226 Summary: Incorrect code generated for tail call, where parameters are structs passed by value, -O2 is used, and target is ARM Product: gcc Version: 5.2.1 Status: UNCONFIRMED Severity: normal Priority: P3 Component: target Assignee: unassigned at gcc dot gnu.org Reporter: jack.d.whitham at gmail dot com Target Milestone: --- Created attachment 36188 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=36188&action=edit Test case to reproduce bug, run "show_bug.sh" on ARMv7 system with GCC 5.2.1 I compiled this code with "gcc (Debian 5.2.1-15) 5.2.1 20150808" on an ARMv7 target (RPi2). typedef struct assembly_operand_t { int type, value, symtype, symflags, marker; } assembly_operand; void t2(assembly_operand to, assembly_operand from) { if (to.value == from.value) return; if (to.value == 0) assemblez_1(32, from); else assemblez_store(to, from); } My code calls the function t2 with to.value == 0. If I compile t2 with -O0 or -O1, assemblez_1 is called as expected with the correct arguments. If I compile t2 with -O2, the "from" structure is copied incorrectly. The "type" field has the incorrect value. I have seen this bug on GCC 4.6.x and 4.7.x as well as 5.2.1 and it seems to behave the same regardless of whether the target ISA is 32-bit ARM or Thumb. However it does not occur on x86/x86_64. For ARM targets, it appears that -O2 causes the call to assemblez_1 to be generated as a tail call whereas it is an ordinary call with -O1/-O0. In the attached test case the same code is compiled with both -O0 and -O2. Both copies are then executed. The test case shows that the "from.type" field is corrupted in the -O2 case. See the "show_bug.sh" script for the command-line options used. More information: This GCC is from https://packages.debian.org/sid/gcc. # gcc --version gcc (Debian 5.2.1-15) 5.2.1 20150808 Copyright (C) 2015 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.