https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81720
Bug ID: 81720 Summary: [arm] Invalid code generation Product: gcc Version: 7.1.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: target Assignee: unassigned at gcc dot gnu.org Reporter: oarias at knights dot ucf.edu Target Milestone: --- Created attachment 41928 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=41928&action=edit Testcase to generate the bug. Greetings, Yesterday, I ran into a problem with the compiler issuing invalid instructions when compiling code with optimization levels of -O2 or higher. I narrowed it down to the following testcase [attached]: int main(void) { volatile unsigned int read_value = *((volatile unsigned int*)0x00000000); return 0; } When compiled with $ arm-none-eabi-gcc -S -c testcase.c -O1 -mthumb -mno-thumb-interwork -mcpu=cortex-m4 The following assembly is generated [trimmed, see full attachment]: main: @ Volatile: function does not return. @ args = 0, pretend = 0, frame = 0 @ frame_needed = 0, uses_anonymous_args = 0 @ link register save eliminated. movs r3, #0 ldr r3, [r3] .inst 0xdeff .size main, .-main The .inst 0xdeff should definitely not be there. Any code that happens after the dereference takes place is never emitted by the compiler. The dereference in question is actually valid for the target I am working with [bare-metal ARM system]. Of course, the assembler happily picks this up and creates object files with the erroneous code. I am using the following gcc: $ arm-none-eabi-gcc --version arm-none-eabi-gcc (Arch Repository) 7.1.0 Copyright (C) 2017 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. Which has been compiled as: $ arm-none-eabi-gcc -v Using built-in specs. COLLECT_GCC=arm-none-eabi-gcc COLLECT_LTO_WRAPPER=/usr/lib/gcc/arm-none-eabi/7.1.0/lto-wrapper Target: arm-none-eabi Configured with: /build/arm-none-eabi-gcc/src/gcc-7-20170504/configure --target=arm-none-eabi --prefix=/usr --with-sysroot=/usr/arm-none-eabi --with-native-system-header-dir=/include --libexecdir=/usr/lib --enable-languages=c,c++ --enable-plugins --disable-decimal-float --disable-libffi --disable-libgomp --disable-libmudflap --disable-libquadmath --disable-libssp --disable-libstdcxx-pch --disable-nls --disable-shared --disable-threads --disable-tls --with-gnu-as --with-gnu-ld --with-system-zlib --with-newlib --with-headers=/usr/arm-none-eabi/include --with-python-dir=share/gcc-arm-none-eabi --with-gmp --with-mpfr --with-mpc --with-isl --with-libelf --enable-gnu-indirect-function --with-host-libstdcxx='-static-libgcc -Wl,-Bstatic,-lstdc++,-Bdynamic -lm' --with-pkgversion='Arch Repository' --with-bugurl=https://bugs.archlinux.org/ --with-multilib-list=armv6-m,armv7-m,armv7e-m,armv7-r Thread model: single gcc version 7.1.0 (Arch Repository) Any advice? Thank you. Cheers, Orlando.