[llvm] [InstCombine] Handle select-like i1 sext/zexts in canonicalizeClampLike (PR #227067)

Luke Lau via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 29 09:53:09 PDT 2026


lukel97 wrote:

> what happened to this part of the description I saw in my email
> 
> > I think this also means canonicalizeClampLike now subsumes the visitTrunc combine added in #189703, we might be able to remove it in another PR. It didn't catch this case because it needs icmp_sgt(A, 0) on one of the arms, but canonicalizeClampLike can also detect icmp_sgt(A, -1)

It turns out there's some cases that visitTrunc catches that canonicalizeClampLike doesn't:

```llvm
define i8 @clamp_i32_to_i8_swapped_select_input(i1 %c, i32 %p, i32 %q) {
  %x = select i1 %c, i32 %p, i32 %q
  %cmp = icmp ugt i32 %x, 255
  %0 = icmp sgt i32 %x, 0
  %shr = sext i1 %0 to i32
  %cond = select i1 %cmp, i32 %shr, i32 %x
  %trunc = trunc i32 %cond to i8
  ret i8 %trunc
}
```

canonicalizeClampLike doesn't fold this because it sees %x as the inner select instead of %shr which is a sext select-like. visitTrunc currently catches this. 

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


More information about the llvm-commits mailing list