AVR: Compiler produces inefficient code when compare 16 bit variables
Original Reporter info from Mantis: Timm Thaler
-
Reporter name: Timm Thaler
Original Reporter info from Mantis: Timm Thaler
- Reporter name: Timm Thaler
Description:
Since some weeks the compiler startet with producing inefficient code when comparing 16 bit variable with possible carry. The variables are expanded to 32 bit and the compare is done with 32 bit.
Adding a constant in the compare is important to reproduce. It does not happen when comparing without adding or adding a variable.
Steps to reproduce:
var
a, b : uint16;
begin
repeat
if a > b + 1 then begin
end;
until false;
end.
compiles to
# [17] if a > b + 1 then begin
mov r2,r19
mov r3,r20
mov r23,r1
mov r24,r1
ldi r21,1
add r2,r21
adc r3,r1
adc r23,r1
adc r24,r1
cp r2,r25
cpc r3,r18
cpc r23,r1
cpc r24,r1
brge .Lj3
Additional information:
Compiler options is -O3, but -O4 or -Os doesn't change behavior.
It may seem to be a good idea to prevent overflow problems, but it puffs the code and doubles execution time - and in most cases it is not necessary.
Mantis conversion info:
- Mantis ID: 35872
- OS: embedded
- OS Build: avr5
- Build: r42480
- Platform: avr
- Version: 3.3.1