# Why does AVR-GCC apparently waste 6 cycles here?

This line...

            if (thisWindowsSinceLastFlash <=1 ) {


...complies to this code...

11e8:   81 e0           ldi r24, 0x01   ; 1
11ea:   8d 15           cp  r24, r13
11ec:   38 f4           brcc    .+14        ; 0x11fc


Why didn't the compiler use the cpi like this...

1204:   01 30           cpi r16, 0x01   ; 1
1206:   39 f4           brcc    .+14


and save the cycles and space and register used by the cp?

Both cp and cpi seam to update the same flags and leave everything in the same state...

What am I missing?

Note that the cp ended up being much more expensive that just the extra cycle because it forced a push/pop sequence on r24, which costs an additional 4 cycles (6 cycles in total).

I am using the AVR8/GNU C Compiler : 5.4.0 that is included with Atmel Studio 7.0

• What optimization level is the compiler set to use? (-O... flag for the CLI, no idea how that would be configured in Atmel Studio) Mar 20, 2018 at 15:31

The cpi instruction can only compare against register r16 or greater...
..so because the value to be compared was in r13 ( < r16), the cpi could not be use.
Still leaves the question as to why the compiler did not allocate to r24 instead of r13 in the first place an save the cycles and the push/pop sequence on r24, but maybe there are other places where r13 is used that being in the lower bank helped.