title: Auto-vectorizing: Convincing the compiler that alias check is not necessary
body: <p>I am doing some image processing, for which I benefit from vectorization.
  I have a function that vectorizes ok, but for which I am not able to convince the compiler that the input and output buffer have no overlap, and so no alias checking is necessary.
  I should be able to do so using <code>__restrict__</code>, but if the buffers are not defined as <code>__restrict__</code> when arriving as function argument, there is no way to convince the compiler that I am absolutely sure that 2 buffers will never overlap.</p>
  
  <p>This is the function:</p>
  
  <pre><code>__attribute__((optimize("tree-vectorize","tree-vectorizer-verbose=6")))
  void threshold(const cv::Mat&amp; inputRoi, cv::Mat&amp; outputRoi, const unsigned char th) {
  
      const int height = inputRoi.rows;
      const int width = inputRoi.cols;
  
      for (int j = 0; j &lt; height; j++) {
          const uint8_t* __restrict in = (const uint8_t* __restrict) inputRoi.ptr(j);
          uint8_t* __restrict out = (uint8_t* __restrict) outputRoi.ptr(j);
          for (int i = 0; i &lt; width; i++) {
             out[i] = (in[i] &lt; valueTh) ? 255 : 0;
          }
      }
  }
  </code></pre>
  
  <p>The only way I can convince the compiler to not perform the alias checking is if I put the inner loop in a separate function, in which the pointers are defined as <code>__restrict__</code> arguments. If I declare this inner function as inlined, again the alias checking is activated.</p>
  
  <p>You can see the effect also with this example, which I think is consistent: <a href="http://goo.gl/7HK5p7" rel="nofollow noreferrer">http://goo.gl/7HK5p7</a></p>
  
  <p>(Note: I know there might be better ways of writing the same function, but in this case I am just trying to understand how to avoid alias check)</p>
  
  <p><strong>Edit:</strong><br/>
  Problem is solved!! (See <a href="https://stackoverflow.com/a/27996733/2436175">answer below</a>)<br/>
  Using gcc 4.9.2, <a href="http://goo.gl/nBjzPC" rel="nofollow noreferrer">here is the complete example</a>. Note the use of the compiler flag <a href="https://gcc.gnu.org/onlinedocs/gcc/Debugging-Options.html#index-fopt-info-818" rel="nofollow noreferrer"><code>-fopt-info-vec-optimized</code></a> in place of the superseded <code>-ftree-vectorizer-verbose=N</code>.<br/> 
  So, for gcc, use <code>#pragma GCC ivdep</code> and enjoy! :)</p>