title: for loop being ignored (optimized?) out
body: <p>I am using for/while loops for implementing a delay in my code. The duration of the delay is unimportant here though it is sufficiently large to be noticeable. Here is the code snippet.</p>
  
  <pre><code>uint32_t i;
  
  // Do something useful
  
  for (i = 0; i &lt; 50000000U; ++i)
  {}
  
  // Do something useful
  </code></pre>
  
  <p>The issue I am observing is that this for loop won't get executed. It probably gets ignored/optimized by the compiler. However, if I qualify the loop counter <code>i</code> by volatile, the for loop seems to execute and I do notice the desired delay in the execution.</p>
  
  <p>This behavior seems a bit counter-intuitive to my understanding of the compiler optimizations with/without the volatile keyword.</p>
  
  <p>Even if the loop counter is getting optimized and being stored in the processor register, shouldn't the counter still work, perhaps with a lesser delay? (Since the memory fetch overhead is done away with.)  </p>
  
  <p>The platform I am building for is Xtensa processor (by Tensilica), and the C compiler is the one provided by Tensilica, Xtensa C/C++ compiler running with highest level of optimizations.</p>
  
  <p>I tried the same with <code>gcc 4.4.7</code> with <code>-o3</code> and ofast optimization levels. The delay seems to work in that case.</p>