<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/59245>59245</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            llvm.canonicalize() folding incorrect for denormals
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            miscompilation,
            llvm:instcombine
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          nunoplopes
      </td>
    </tr>
</table>

<pre>
    File Transforms/InstSimplify/canonicalize.ll has the following test:
```llvm
define float @canonicalize_neg_denorm_positive_zero_output_preserve_sign_input() "denormal-fp-math"="positive-zero,preserve-sign" {
; CHECK-LABEL: @canonicalize_neg_denorm_positive_zero_output_preserve_sign_input(
; CHECK-NEXT:    ret float 0.000000e+00
;
  %ret = call float @llvm.canonicalize.f32(float bitcast (i32 -2139095041 to float))
  ret float %ret
}
```

The expected result is wrong. This function should return -0.0.
If you run the equivalent multiplication by 1.0, you get -0.0:
```llvm
define float @src() "denormal-fp-math"="positive-zero,preserve-sign" {
  %ret = fmul float bitcast (i32 -2139095041 to float), 1.0
  ret float %ret
}

define float @tgt() "denormal-fp-math"="positive-zero,preserve-sign" {
  ret float 0.0
}

Transformation doesn't verify!

ERROR: Value mismatch

Example:

Source:
float %ret = #x80000000 (-0.0)

Target:
Source value: #x80000000 (-0.0)
Target value: #x00000000 (+0.0)
```

In summary, canonicalize should use just the input denormal flag and ignore the output one.

cc @arsenm
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJytVdtupDgQ_Rp4sUBgoC8PPKRz0UY72pUy0WjfWgYK8MjYrC896fn6LZvudCcbrXakIMtgu3zqdqpoVHesH7gA8qyZNL3Sk4now6M09iufZsH7Iy5bJpXkLRP8J6RCkJEZYkcgvRJC_eByIBaMjYqbKLuLsptolS1DiMO0bHXQc4kXhGKWRGV2jbiXMOw7kKh7PyvDLT_A_idotVfOzs7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