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 ?

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
  • Thank you for your input.

    Just to clarify my intentions. I'm an academic teacher and I'm preparing a course on embedded systems. This year we plan to switch from GCC to Keil compiler. Actually I'm porting some of the older examples I had for GCC and this one was for the purpose of showing the volatile keyword.

    The example above is just to show the principle. Here's a more useful example:

    void logStatus(void)
    {
    #if defined LOG_ENABLED
      printf("This is log");
    #endif
    }
    
    int main(void)
    {
      int j;
    
      for (j = 0; j < 3; j++)    {
        logStatus();
      }
      return 0;
    }
    


    I tried this and assuming that LOG_ENABLED is not defined the output in Keil is that logStatus call in the for loop is actually optimized out (that's good) but the loop is still there. The GCC (4.8.2) in this case reduces the whole main function to just:

    movs    r0, #0
    bx      lr
    


    I just wanted to confirm if this is the expected behavior in Keil?