-Poof- My Delay Was Optimized Away!

drain
In the Arduino IDE, compiler optimization is set at “–Os”, which means optimization for reduced code size (this level includes most level 2 optimizations also). It might have some surprising affects on your code. For example, Look at the below simple Blink program in which I’ve incorporated a self-brewed delay (a for loop):

void setup() {
  pinMode(13, OUTPUT);
}

void loop() {
  digitalWrite(13, HIGH);
  for (int i=0; i<1000; i++);  //home-brewed delay inserted here
  digitalWrite(13, LOW);
  delay(1000);
}

Now look at the code produced, and notice the for loop has been completely eliminated.

                          ; digitalWrite(13, HIGH);
100:  lds  r24, 0x0100
104:  ldi  r22, 0x01      ; 1 = HIGH
106:  call 0x364          ; 0x364
                          ;
                          ; for (int i=0; i<1000; i++);
                          ; code conspicuously missing
                          ;
                          ; digitalWrite(13, LOW);
10a:  lds  r24, 0x0100
10e:  ldi  r22, 0x00      ; 0 = LOW
110:  call 0x364          ; 0x364
                          ;
                          ; delay(1000);
114:  ldi  r22, 0xE8      ; 232
116:  ldi  r23, 0x03      ; 3
118:  ldi  r24, 0x00      ; 0
11a:  ldi  r25, 0x00      ; 0
11c:  call 0x1be          ; 0x1be
120:  ret

Here’s what the same code looks like when compiled inside AVR Studio with optimization turned off:

;void loop() {
  PUSH      R29            Push register on stack
  PUSH      R28            Push register on stack
  RCALL     PC+0x0001      Relative call subroutine
  IN        R28,0x3D       In from I/O location
  IN        R29,0x3E       In from I/O location

;digitalWrite(13, HIGH);
  LDI       R24,0x0D       Load immediate
  LDI       R22,0x01       Load immediate
  CALL      0x0000035D     Call subroutine

;for (i=0; i<1000; i++);  //home-brewed delay inserted here
  STD       Y+2,R1         Store indirect with displacement
  STD       Y+1,R1         Store indirect with displacement
  RJMP      PC+0x0006      Relative jump

  LDD       R24,Y+1        Load indirect with displacement
  LDD       R25,Y+2        Load indirect with displacement
  ADIW      R24,0x01       Add immediate to word
  STD       Y+2,R25        Store indirect with displacement
  STD       Y+1,R24        Store indirect with displacement

  LDD       R24,Y+1        Load indirect with displacement
  LDD       R25,Y+2        Load indirect with displacement
  LDI       R18,0x03       Load immediate
  CPI       R24,0xE8       Compare with immediate
  CPC       R25,R18        Compare with carry
  BRLT      PC-0x0A        Branch if less than, signed

;digitalWrite(13, LOW);
  LDI       R24,0x0D       Load immediate
  LDI       R22,0x00       Load immediate
  CALL      0x0000035D     Call subroutine

;delay(1000);
  LDI       R22,0xE8       Load immediate
  LDI       R23,0x03       Load immediate
  LDI       R24,0x00       Load immediate
  LDI       R25,0x00       Load immediate
  CALL      0x00000179     Call subroutine

  POP       R0             Pop register from stack
  POP       R0             Pop register from stack
  POP       R28            Pop register from stack
  POP       R29            Pop register from stack
  RET                      Subroutine return
;}

optimize

As of Arduino version 1.0.5, the default optimization level is set at –Os (for reduced size) during all compiles. For the most part, this is a good thing in the AVR environment. However, there is an open issue requesting the optimization level to be selectable from inside the IDE (#421).

For now, your only alternative is to use a programming environment other than the Arduino IDE (AVR Studio, command line, etc.).

Advertisements

About Jim Eli

µC experimenter
This entry was posted in Uncategorized and tagged , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s