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...

enter image description here enter image description here

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

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

1 Answer 1


The cpi instruction can only compare against register r16 or greater...

enter image description here

..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.

  • \$\begingroup\$ With the number and complexity of compiler optimisations, I imagine it's probably often quite difficult to say exactly why a compiler compiled code a certain way. \$\endgroup\$
    – BeB00
    Mar 20, 2018 at 15:23
  • \$\begingroup\$ The compiler only can do so much on such optimisations and especially register allocation is an art on its own. Also it has been a long time since gcc5 and maybe more modern Ives perform better. Have a look at the GCC explorer (you may have to install your own to get more recent avr versions) \$\endgroup\$
    – PlasmaHH
    Mar 20, 2018 at 17:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.