Wrong code generation with -O2 for arm-none-eabi

Bug #1809240 reported by Christian Eggers on 2018-12-20
8
This bug affects 1 person
Affects Status Importance Assigned to Milestone
GNU Arm Embedded Toolchain
Undecided
Unassigned

Bug Description

release version (binary package):
- gcc-arm-none-eabi-7-2017-q4-major
- gcc-arm-none-eabi-7-2018-q2-update
- gcc-arm-none-eabi-8-2018-q4-major
host: openSUSE Leap 42.3 (x86_64)

testcase:

/*
 * working:
 * arm-none-eabi-gcc -mcpu=cortex-m4 -mthumb -O1 -c -o "test.o" "test.c"
 * Not working:
 * arm-none-eabi-gcc -mcpu=cortex-m4 -mthumb -O2 -c -o "test.o" "test.c"
 */
typedef struct
{
 volatile unsigned m;
} FOO;

#define BAR ((FOO *)0x1000)

static inline void bar(unsigned arg)
{
 BAR->m = (BAR->m & ~2) | (2 & (0x7FFFFU << (arg & 0x20U)));
}

void foo(void)
{
 bar(0x7FU);
}

symptoms:

compiled with -O1:
00000000 <foo>:
   0: f44f 5280 mov.w r2, #4096 ; 0x1000
   4: 6813 ldr r3, [r2, #0]
   6: f023 0302 bic.w r3, r3, #2 ; R3 = R3 & ~2 (correct)
   a: 6013 str r3, [r2, #0]
   c: 4770 bx lr

compiled with -O2:
00000000 <foo>:
   0: f44f 5280 mov.w r2, #4096 ; 0x1000
   4: 6813 ldr r3, [r2, #0]
   6: f043 0302 orr.w r3, r3, #2 ; R3 = R3 | 2 (wrong)
   a: 6013 str r3, [r2, #0]
   c: 4770 bx lr
   e: bf00 nop

Christian Eggers (ceggers-arri) wrote :

Also affected: gcc-arm-none-eabi-8-2018-q4-major

description: updated

The bug is in the code, not the compiler. This is undefined behaviour (ISO/IEC 9899:201x draft N1570; 6.5.7 Bitwise shift operators):

"If the value of the right operand is negative or is greater than or equal to the width of the promoted left operand, the behavior is undefined."

Christian Eggers (ceggers-arri) wrote :

The standard looks unambiguous for me. So this is a bug in the library I use (STM32Cube for STM32F3). I just filled a bug at st.com

Thank you very much for analysing my problem!

regards
Christian

Changed in gcc-arm-embedded:
status: New → Invalid
To post a comment you must log in.
This report contains Public information  Edit
Everyone can see this information.

Other bug subscribers