title: Why can the compiler not optimize floating point addition with 0?
body: <p>I have four identity functions which do essentially nothing. Only multiplication with <code>1</code> could be optimized by clang to a single <code>ret</code> statement.</p>
  
  <pre class="lang-cpp prettyprint-override"><code>float id0(float x) {
      return x + 1 - 1;
  }
  
  float id1(float x) {
      return x + 0;
  }
  
  float id2(float x) {
      return x * 2 / 2;
  }
  
  float id3(float x) {
      return x * 1;
  }
  </code></pre>
  
  <p>And the following compiler output is: (clang 10, at -O3)</p>
  
  <pre><code>.LCPI0_0:
          .long   1065353216              # float 1
  .LCPI0_1:
          .long   3212836864              # float -1
  id0(float):                                # @id0(float)
          addss   xmm0, dword ptr [rip + .LCPI0_0]
          addss   xmm0, dword ptr [rip + .LCPI0_1]
          ret
  id1(float):                                # @id1(float)
          xorps   xmm1, xmm1
          addss   xmm0, xmm1
          ret
  .LCPI2_0:
          .long   1056964608              # float 0.5
  id2(float):                                # @id2(float)
          addss   xmm0, xmm0
          mulss   xmm0, dword ptr [rip + .LCPI2_0]
          ret
  id3(float):                                # @id3(float)
          ret
  </code></pre>
  
  <p>I can understand why <code>id0</code> and <code>id2</code> can't be optimized. They increase the value which could then turn into positive infinity and the second operation would not change it back.</p>
  
  <p>But why can't <code>id1</code> be optimized? Additon with infinity would yield infinity, addition with any regular number would yield that number and addition with <code>NaN</code> would yield <code>NaN</code>. So why is it not a "true" identity operation like <code>* 1</code>.</p>
  
  <p><a href="https://godbolt.org/z/TsfVWY" rel="noreferrer">Example with Compiler Explorer</a></p>