Issue 234696
Summary [InstCombine] Miscompilation while simplifying copysign's sign operand
Labels new issue
Assignees
Reporter sairam2661
    **Fuzzer Generated Test**
**Reproducer**
https://alive2.llvm.org/ce/z/Uzkq2J

**Test Commit**
[e633c7da8ff144a11b3d875956fbaea06b969070](https://github.com/llvm/llvm-project/commit/e633c7da8ff144a11b3d875956fbaea06b969070)

**Description**
Since `copysign` reads only the sign bit of its second operand, InstCombine (`visitCallInst`, `SimplifyDemandedBits` on a bitcast sign operand, from #177942) removes the `and` that clears the other bits.

However, it keeps the call's `nnan` (and `ninf`), which also apply to that operand, so for a NaN `%sign` the target is poison where the source's sign operand was a zero and the result a value.

**Steps to reproduce**
- Minimized test case (src), `input.ll`

```llvm
define float @f(float %mag, float %sign) {
 %i = bitcast float %sign to i32
  %a = and i32 %i, -2147483648
  %s = bitcast i32 %a to float
  %r = call nnan float @llvm.copysign.f32(float %mag, float %s)
  ret float %r
}
```

- InstCombine output (tgt), `output.ll`

```llvm
define float @f(float %mag, float %sign) {
  %r = call nnan float @llvm.copysign.f32(float %mag, float %sign)
  ret float %r
}

declare float @llvm.copysign.f32(float, float)
```

**Output**

```txt
define float @f(float %mag, float %sign) {
#0:
  %i = bitcast float %sign to i32
  %a = and i32 %i, 2147483648
 %s = bitcast i32 %a to float
  %r = copysign nnan float %mag, %s
  ret float %r
}
=>
define float @f(float %mag, float %sign) {
#0:
  %r = copysign nnan float %mag, %sign
  ret float %r
}
Transformation doesn't verify!

ERROR: Target is more poisonous than source

Example:
float %mag = #x00000000 (+0.0)
float %sign = #x7f802000 (SNaN)

Source:
i32 %i = #x7f802000 (2139103232)
i32 %a = #x00000000 (0)
float %s = #x00000000 (+0.0)
float %r = #x00000000 (+0.0)

Target:
float %r = poison
Source value: #x00000000 (+0.0)
Target value: poison

Summary:
  0 correct transformations
  1 incorrect transformations
  0 failed-to-prove transformations
  0 Alive2 errors
```

**CC**: @regehr
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to