title: Why can't (or doesn't) the compiler optimize a predictable addition loop into a multiplication?
body: <p>This is a question that came to mind while reading the brilliant answer by <a href="https://stackoverflow.com/users/922184/mysticial">Mysticial</a> to the question: <a href="https://stackoverflow.com/questions/11227809/why-is-processing-a-sorted-array-faster-than-an-unsorted-array">why is it faster to process a sorted array than an unsorted array</a>?</p>
  
  <p>Context for the types involved:</p>
  
  <pre><code>const unsigned arraySize = 32768;
  int data[arraySize];
  long long sum = 0;
  </code></pre>
  
  <p>In his answer he explains that the Intel Compiler (ICC) optimizes this:</p>
  
  <pre><code>for (int i = 0; i &lt; 100000; ++i)
      for (int c = 0; c &lt; arraySize; ++c)
          if (data[c] &gt;= 128)
              sum += data[c];
  </code></pre>
  
  <p>...into something equivalent to this:</p>
  
  <pre><code>for (int c = 0; c &lt; arraySize; ++c)
      if (data[c] &gt;= 128)
          for (int i = 0; i &lt; 100000; ++i)
              sum += data[c];
  </code></pre>
  
  <p>The optimizer is recognizing that these are equivalent and is therefore <a href="http://en.wikipedia.org/wiki/Loop_interchange" rel="noreferrer">exchanging the loops</a>, moving the branch outside the inner loop. Very clever!</p>
  
  <p>But why doesn't it do this?</p>
  
  <pre><code>for (int c = 0; c &lt; arraySize; ++c)
      if (data[c] &gt;= 128)
          sum += 100000 * data[c];
  </code></pre>
  
  <p>Hopefully Mysticial (or anyone else) can give an equally brilliant answer. I've never learned about the optimizations discussed in that other question before, so I'm really grateful for this.</p>