Missing optimization with predecrement in while-loop
Bug #1538629 reported by
demiurg_spb
This bug affects 1 person
Affects | Status | Importance | Assigned to | Milestone | |
---|---|---|---|---|---|
GNU Arm Embedded Toolchain |
New
|
Undecided
|
Unassigned |
Bug Description
foo.c:
void delay_cy(volatile unsigned int cy)
{
do
{
;
} while (--cy);
}
arm-none-eabi-gcc -S -mcpu=cortex-m1 -mthumb -msoft-float -Os foo.c
.L2:
ldr r3, [sp, #4]
subs r3, r3, #1
str r3, [sp, #4]
cmp r3, #0 - this operation is dummy because of SUBS allready sets status regiser correctly
bne .L2
.ident "GCC: (GNU Tools for ARM Embedded Processors) 5.2.1 20151202 (release) [ARM/embedded-
To post a comment you must log in.
I think this is an effect of the counter being "volatile". The line "while (--cy)" is being handled as two operations - first "cy" is decremented, then it is compared to 0. Since cy is marked volatile, these operations are not combined.
The C standards are not very clear on exactly how such code should be interpreted - I believe it is right for the compiler to be a bit conservative here.