This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

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 ?

0