title: C++ Auto-vectorization of STL vector on GCC 4.6.3
body: <p><em><strong>Setup</em></strong></p>
  
  <p>I get curious behaviours when trying to vectorize a code on GCC 4.6.3.</p>
  
  <p>In the following is the snippet code using 3 different ways to perform a simple addition.</p>
  
  <ul>
  <li>loopFunc() uses pointers.</li>
  <li>loopFunc2() uses STL vector directly accessed via operator[].</li>
  <li>loopFunc3() uses STL vector iterators.</li>
  </ul>
  
  <p>The .cpp snippet code is compiled with the following line:</p>
  
  <pre><code>g++ -o snippet-tree-vectorize -fdump-tree-optimized -ftree-vectorizer-verbose=2 -O2 -ftree-vectorize snippet.cpp
  </code></pre>
  
  <p><em><strong>Question:</em></strong></p>
  
  <ul>
  <li>loopFunc() and loopFunc3() get vectorized by GCC (easily checkable with -ftree-vectorizer-verbose=2)</li>
  <li>loopFunc2() does not get vectorized.</li>
  </ul>
  
  <p><em><strong>Would someone have an explanation for that?</em></strong></p>
  
  <pre><code>typedef int data_type;
  
  void loopFunc(data_type* d1,const data_type* d2,const data_type* d3) 
  {
      for (int i = 0; i &lt; HUGE-1; i++)
              d1[i] = d2[i] + d3[i];
  }
  
  void loopFunc2(std::vector&lt;data_type&gt;&amp; d1,const std::vector&lt;data_type&gt;&amp; d2,const std::vector&lt;data_type&gt;&amp; d3) 
  {
      for (int i = 0; i &lt; HUGE-1; i++)
              d1[i] = d2[i] + d3[i];
  }
  
  void loopFunc3(std::vector&lt;data_type&gt;&amp; d1,const std::vector&lt;data_type&gt;&amp; d2,const std::vector&lt;data_type&gt;&amp; d3) 
  {
      std::vector&lt;data_type&gt;::iterator it1 = d1.begin();
      std::vector&lt;data_type&gt;::const_iterator it2 = d2.begin();
      std::vector&lt;data_type&gt;::const_iterator it3 = d3.begin();
      for(; it1 != d1.end(); it1++,it2++,it3++)
              *it1 = *it2 + *it3;
  }
  
  int main() 
  {
      std::vector&lt;data_type&gt; d1(HUGE, 0); 
      std::vector&lt;data_type&gt; d2(HUGE, 1); 
      std::vector&lt;data_type&gt; d3(HUGE, 2); 
  
      loopFunc(&amp;d1[0],&amp;d2[0],&amp;d3[0]);
      loopFunc2(d1,d2,d3);
      loopFunc3(d1,d2,d3);
  
      return 0;
  }
  </code></pre>