Issue 234549
Summary [CodeGenPrepare] Dominating-compare fold swaps the arms of a select that also uses the compare as a value
Labels llvm:codegen, miscompilation, generated by fuzzer
Assignees
Reporter ChoKyuWon
    ### reproducer
godbolt: https://godbolt.org/z/TTGKMMbq4
alive: https://alive2.llvm.org/ce/z/PZrANd

```llvm
target triple = "powerpc64le-unknown-linux-gnu"

define i1 @f(i32 %a, i32 %b) {
entry:
 %lt = icmp slt i32 %a, %b
  br i1 %lt, label %t, label %cmpbb

t:
  ret i1 false

cmpbb:
  %eq = icmp eq i32 %a, %b
  %s = select i1 %eq, i1 %eq, i1 false
  ret i1 %s
}

define i32 @main() {
  %r = call i1 @f(i32 0, i32 0)
  %z = zext i1 %r to i32
  ret i32 %z
}
```

```console
$ opt -S -passes='require<profile-summary>,function(codegenprepare)' repro.ll
...
cmpbb:
  %eq = icmp sgt i32 %a, %b
  %s = select i1 %eq, i1 false, i1 %eq
  ret i1 %s
}
...

$ llubi repro.ll; echo $?
1
$ opt -S -passes='require<profile-summary>,function(codegenprepare)' repro.ll | llubi -; echo $?
0

$ alive-tv --disable-undef-input --disable-poison-input '-passes=require<profile-summary>,function(codegenprepare)' repro.ll
...
Transformation doesn't verify!

ERROR: Value mismatch

Example:
i32 %a = #x00000000 (0)
i32 %b = #x00000000 (0)
...
Source value: #x1 (1)
Target value: #x0 (0)
```

### suggested fix
`foldICmpWithDominatingICmp` accepts any `SelectInst` user whose condition is `Cmp`, swaps its arms, and changes `Cmp` from `eq` to `sgt`/`slt`. The swap only compensates for the condition use, so an arm that is `Cmp` itself now yields the new predicate's value.
In the user-check loop, also bail out when the select uses `Cmp` as a value (`SI->getTrueValue() == Cmp || SI->getFalseValue() == Cmp`).

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

Reply via email to