title: What optimizations does __builtin_unreachable facilitate?
body: <p>Judging from gcc's documentation</p>
  
  <blockquote>
    <p>If control flow reaches the point of the <code>__builtin_unreachable</code>, the program is undefined.</p>
  </blockquote>
  
  <p>I thought <code>__builtin_unreachable</code> may be used as a hint to the optimizer in all sorts of creative ways. So I did a little experiment</p>
  
  <pre><code>void stdswap(int&amp; x, int&amp; y)
  {
      std::swap(x, y);
  }
  
  void brswap(int&amp; x, int&amp; y)
  {
      if(&amp;x == &amp;y)
          __builtin_unreachable();
      x ^= y;
      y ^= x;
      x ^= y;
  }
  
  void rswap(int&amp; __restrict x, int&amp; __restrict y)
  {
      x ^= y;
      y ^= x;
      x ^= y;
  }
  </code></pre>
  
  <p><a href="https://godbolt.org/z/cphR1Q" rel="noreferrer">gets compiled to</a> (g++ -O2)</p>
  
  <pre><code>stdswap(int&amp;, int&amp;):
          mov     eax, DWORD PTR [rdi]
          mov     edx, DWORD PTR [rsi]
          mov     DWORD PTR [rdi], edx
          mov     DWORD PTR [rsi], eax
          ret
  brswap(int&amp;, int&amp;):
          mov     eax, DWORD PTR [rdi]
          xor     eax, DWORD PTR [rsi]
          mov     DWORD PTR [rdi], eax
          xor     eax, DWORD PTR [rsi]
          mov     DWORD PTR [rsi], eax
          xor     DWORD PTR [rdi], eax
          ret
  rswap(int&amp;, int&amp;):
          mov     eax, DWORD PTR [rsi]
          mov     edx, DWORD PTR [rdi]
          mov     DWORD PTR [rdi], eax
          mov     DWORD PTR [rsi], edx
          ret
  </code></pre>
  
  <p>I assume that <code>stdswap</code> and <code>rswap</code> is optimal from the optimizer's perspective. Why doesn't <code>brswap</code> get compiled to the same thing? Can I get it to compile to the same thing with <code>__builtin_unreachable</code>?</p>