[llvm] [InstCombine] Fold `(x == y) ? 0 : (x > y ? 1 : -1)` into `ucmp/scmp(x,y)` (PR #107314)

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 5 23:11:25 PDT 2024


dtcxzyw wrote:

> LGTM
> 
> @dtcxzyw Would be great if you could test this, in case it surfaces more optimization regressions...

In most cases, regressions occur when the result of `[u|s]cmp` is passes to a phi node, then the phi node is compared with a constant:
```
bb1:
%cmp = call i32 @llvm.ucmp(...)
br label %exit

exit:
%phi = i32 [%cmp, %bb1], [-1, %bb2]
%cmp2 = icmp slt i32 %phi, 0
```

Another missed optimization: if `ssat(X)` is only used to compare with zero, we can use `X` instead.
Alive2: https://alive2.llvm.org/ce/z/RZhWXF


https://github.com/llvm/llvm-project/pull/107314


More information about the llvm-commits mailing list