title: Why does the Rust compiler not optimize code assuming that two mutable references cannot alias?
body: <p>As far as I know, reference/pointer aliasing can hinder the compiler's ability to generate optimized code, since they must ensure the generated binary behaves correctly in the case where the two references/pointers indeed alias. For instance, in the following C code,</p>
  <pre class="lang-c prettyprint-override"><code>void adds(int *a, int *b) {
      *a += *b;
      *a += *b;
  }
  </code></pre>
  <p>when compiled by <code>clang version 6.0.0-1ubuntu2 (tags/RELEASE_600/final)</code> with the <code>-O3</code> flag, it emits</p>
  <pre class="lang-none prettyprint-override"><code>0000000000000000 &lt;adds&gt;:
     0:    8b 07                    mov    (%rdi),%eax  # load a into EAX
     2:    03 06                    add    (%rsi),%eax  # load-and-add b
     4:    89 07                    mov    %eax,(%rdi)  # store into a
     6:    03 06                    add    (%rsi),%eax  # load-and-add b again
     8:    89 07                    mov    %eax,(%rdi)  # store into a again
     a:    c3                       retq
  </code></pre>
  <p>Here the code stores back to <code>(%rdi)</code> twice in case <code>int *a</code> and <code>int *b</code> alias.</p>
  <p>When we explicitly tell the compiler that these two pointers cannot alias with the <code>restrict</code> keyword:</p>
  <pre class="lang-c prettyprint-override"><code>void adds(int *restrict a, int *restrict b) {
      *a += *b;
      *a += *b;
  }
  </code></pre>
  <p>Then Clang will emit a more optimized version that effectively does <code>*a += 2 * (*b)</code>, which is equivalent if (as promised by <code>restrict</code>) <code>*b</code> isn't modified by assigning to <code>*a</code>:</p>
  <pre class="lang-none prettyprint-override"><code>0000000000000000 &lt;adds&gt;:
     0:    8b 06                    mov    (%rsi),%eax   # load b once
     2:    01 c0                    add    %eax,%eax     # double it
     4:    01 07                    add    %eax,(%rdi)   # *a += 2 * (*b)
     6:    c3                       retq
  </code></pre>
  <p>Since Rust makes sure (except in unsafe code) that two mutable references cannot alias, I would think that the compiler should be able to emit the more optimized version of the code.</p>
  <p>When I test with the code below and compile it with <code>rustc 1.35.0</code> with <code>-C opt-level=3 --emit obj</code>,</p>
  <pre class="lang-rust prettyprint-override"><code>#![crate_type = &quot;staticlib&quot;]
  #[no_mangle]
  fn adds(a: &amp;mut i32, b: &amp;mut i32) {
      *a += *b;
      *a += *b;
  }
  </code></pre>
  <p>it generates:</p>
  <pre class="lang-none prettyprint-override"><code>0000000000000000 &lt;adds&gt;:
     0:    8b 07                    mov    (%rdi),%eax
     2:    03 06                    add    (%rsi),%eax
     4:    89 07                    mov    %eax,(%rdi)
     6:    03 06                    add    (%rsi),%eax
     8:    89 07                    mov    %eax,(%rdi)
     a:    c3                       retq
  </code></pre>
  <p>This does not take advantage of the guarantee that <code>a</code> and <code>b</code> cannot alias.</p>
  <p>Is this because the current Rust compiler is still in development and has not yet incorporated alias analysis to do the optimization?</p>
  <p>Is this because there is still a chance that <code>a</code> and <code>b</code> could alias, even in safe Rust?</p>