title: GCC: vectorization difference between two similar loops
body: <p>When compiling with <code>gcc -O3</code>, why does the following loop not vectorize (automatically):</p>
  
  <pre><code>#define SIZE (65536)
  
  int a[SIZE], b[SIZE], c[SIZE];
  
  int foo () {
    int i, j;
  
    for (i=0; i&lt;SIZE; i++){
      for (j=i; j&lt;SIZE; j++) {
        a[i] = b[i] &gt; c[j] ? b[i] : c[j];
      }
    }
    return a[0];
  }
  </code></pre>
  
  <p>when the following one does?</p>
  
  <pre><code>#define SIZE (65536)
  
  int a[SIZE], b[SIZE], c[SIZE];
  
  int foov () {
    int i, j;
  
    for (i=0; i&lt;SIZE; i++){
      for (j=i; j&lt;SIZE; j++) {
        a[i] += b[i] &gt; c[j] ? b[i] : c[j];
      }
    }
    return a[0];
  }
  </code></pre>
  
  <p>The only difference is whether the result of the expression in the inner loop is <strong>assigned to a[i], or added to a[i]</strong>.</p>
  
  <p>For reference <code>-ftree-vectorizer-verbose=6</code> gives the following output for the first (non-vectorizing) loop.</p>
  
  <pre><code>v.c:8: note: not vectorized: inner-loop count not invariant.
  v.c:9: note: Unknown alignment for access: c
  v.c:9: note: Alignment of access forced using peeling.
  v.c:9: note: not vectorized: live stmt not supported: D.2700_5 = c[j_20];
  
  v.c:5: note: vectorized 0 loops in function.
  </code></pre>
  
  <p>And the same output for the loop that vectorizes is:</p>
  
  <pre><code>v.c:8: note: not vectorized: inner-loop count not invariant.
  v.c:9: note: Unknown alignment for access: c
  v.c:9: note: Alignment of access forced using peeling.
  v.c:9: note: vect_model_load_cost: aligned.
  v.c:9: note: vect_model_load_cost: inside_cost = 1, outside_cost = 0 .
  v.c:9: note: vect_model_simple_cost: inside_cost = 1, outside_cost = 1 .
  v.c:9: note: vect_model_reduction_cost: inside_cost = 1, outside_cost = 6 .
  v.c:9: note: cost model: prologue peel iters set to vf/2.
  v.c:9: note: cost model: epilogue peel iters set to vf/2 because peeling for alignment is unknown .
  v.c:9: note: Cost model analysis:
    Vector inside of loop cost: 3
    Vector outside of loop cost: 27
    Scalar iteration cost: 3
    Scalar outside cost: 7
    prologue iterations: 2
    epilogue iterations: 2
    Calculated minimum iters for profitability: 8
  
  v.c:9: note:   Profitability threshold = 7
  
  v.c:9: note: Profitability threshold is 7 loop iterations.
  v.c:9: note: LOOP VECTORIZED.
  v.c:5: note: vectorized 1 loops in function.
  </code></pre>