For loop ignored (optimized?)

I use for / while to delay in my code. The length of the delay is not important here, although it is long enough to be noticeable. Here is a snippet of code.

uint32_t i; // Do something useful for (i = 0; i < 50000000U; ++i) {} // Do something useful 

The problem that I am observing is that this loop cycle will not execute. It is probably ignored / optimized by the compiler. However, if I qualify the i loop counter to volatile, the for loop seems to be running, and I notice the desired delay in execution.

This behavior seems a bit inconsistent for my understanding of compiler optimizations using / without the volatile keyword.

Even if the loop counter receives optimization and is stored in the processor register, should the counter not work, possibly with less delay? (Because the memory extraction overhead has been eliminated.)

The platform I create for is the Xtensa processor (from Tensilica), and the C compiler is the one provided by Tensilica, Xtensa C / C ++, with a high level of optimization.

I tried the same with gcc 4.4.7 with -o3 and the highest levels of optimization. The delay seems to work in this case.

+6
source share
1 answer

It is all about observable behavior. The only observable behavior of your cycle is that i is 50000000U after the cycle. The compiler is allowed to optimize it and replace it with i = 50000000U; . This assignment i will also be optimized because the value of i has no observable consequences.

The volatile keyword tells the compiler that writing and reading from i have observable behavior, which prevents it from being optimized.

The compiler will also not optimize function calls where it does not have access to code. Theoretically, if the compiler had access to the entire OS code, it could optimize everything except volatile variables, which are often superimposed on hardware I / O operations.

These optimization rules all correspond to what is written in the C standard (see comments for links).

In addition, if you want to get a delay, use a specialized function (for example, the OS API), they are reliable and do not consume a processor, as opposed to a delay on a delay like yours.

+18
source

All Articles