No empty "for" loop optimization?

Dear all.

I'm currently evaluating uVision 5.13.0.0. I wrote a simple code for STM32F429I-DISCO board that is supposed to blink LEDs. However I ran into a disturbing effect that I did not expect. It seems that no matter what optimization level I use (O0..O3) the empty for loops are never optimized away. The code looks like this:

int main(void)
{
  int i;

  HAL_Init();
  LED_Initialize();

  while (1) {
    HAL_GPIO_WritePin(GPIOG, GPIO_PIN_13, GPIO_PIN_SET);
    for (i=0; i < 1000000; i++) {
      ;
    }
    HAL_GPIO_WritePin(GPIOG, GPIO_PIN_13, GPIO_PIN_RESET);
    for (i=0; i < 1000000; i++) {
        ;
    }
  }
}


And I expect it NOT to blink visibly, because the i declaration lacks the volatile specifier. However the loops are always there. I looked at assembly output and no matter what I do (tried different for loop statements) the loops are always there.

My question is: does Keil compiler actually perform such loop removal ?

Parents
  • Remember that Keil is specifically an embedded compiler: so maybe their thinking is that the only reason you'd have an empty for loop is because you want a delay - so they leave it in...?

    Posts from people complaining that their loops get optimised away appear with monotonous regularity - but this is the first one I've ever seen complaining that empty loops don't get optimised!

    Anyhow, if you are performing a bona fide evaluation with a genuine view to purchase - you should contact Keil (or your distributor) directly about this.

Reply
  • Remember that Keil is specifically an embedded compiler: so maybe their thinking is that the only reason you'd have an empty for loop is because you want a delay - so they leave it in...?

    Posts from people complaining that their loops get optimised away appear with monotonous regularity - but this is the first one I've ever seen complaining that empty loops don't get optimised!

    Anyhow, if you are performing a bona fide evaluation with a genuine view to purchase - you should contact Keil (or your distributor) directly about this.

Children
More questions in this forum