[all-commits] [llvm/llvm-project] 1a4734: [InstCombine] fold (sext(ucmp/scmp(X, Y)) -> ucmp/...

Takashi Idobe via All-commits all-commits at lists.llvm.org
Thu Apr 9 09:54:06 PDT 2026


  Branch: refs/heads/main
  Home:   https://github.com/llvm/llvm-project
  Commit: 1a4734789d2dcc42bb2d183ffb94b0cb9b41292f
      https://github.com/llvm/llvm-project/commit/1a4734789d2dcc42bb2d183ffb94b0cb9b41292f
  Author: Takashi Idobe <idobetakashi at gmail.com>
  Date:   2026-04-10 (Fri, 10 Apr 2026)

  Changed paths:
    M llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
    M llvm/test/Transforms/InstCombine/scmp.ll
    M llvm/test/Transforms/InstCombine/ucmp.ll

  Log Message:
  -----------
  [InstCombine] fold (sext(ucmp/scmp(X, Y)) -> ucmp/scmp(X, Y) (#191182)

relates: https://github.com/llvm/llvm-project/issues/190538
related PR: https://github.com/llvm/llvm-project/pull/190787

This PR folds (sext(ucmp/scmp(X, Y)) -> ucmp/scmp(X, Y). Since ucmp/scmp
return -1, 0, 1, if you see the sequence:

```llvm
%_3 = tail call i8 @llvm.ucmp.i8.i64(i64 %a, i64 %b)
%_0 = sext i8 %_3 to i64
```

It's fine to turn this into:

```llvm
%_0 = tail call i64 @llvm.ucmp.i64.i64(i64 %a, i64 %b)
```

[Alive Proof](https://alive2.llvm.org/ce/z/VmeKM2)

Likewise for scmp:

```llvm
%_3 = tail call i8 @llvm.scmp.i8.i64(i64 %a, i64 %b)
%_0 = sext i8 %_3 to i64
```

```llvm
%_0 = tail call i64 @llvm.scmp.i64.i64(i64 %a, i64 %b)
```

[Alive Proof](https://alive2.llvm.org/ce/z/WSm7YS)

For ucmp, a simple way to get this is to cmp two rust unsigned integers:

```rust
pub fn compare(a: u64, b: u64) -> i64 {
    a.cmp(&b) as i64
}
```

Which gets this:

```llvm
define noundef range(i64 -1, 2) i64 @compare(i64 noundef %a, i64 noundef %b) unnamed_addr {
start:
  %_3 = tail call i8 @llvm.ucmp.i8.i64(i64 %a, i64 %b)
  %_0 = sext i8 %_3 to i64
  ret i64 %_0
}

declare range(i8 -1, 2) i8 @llvm.ucmp.i8.i64(i64, i64)
```

For scmp, swapping to i64s will suffice:

```rust
pub fn compare(a: i64, b: i64) -> i64 {
    a.cmp(&b) as i64
}
```

```llvm
define noundef range(i64 -1, 2) i64 @compare(i64 noundef %a, i64 noundef %b) unnamed_addr {
start:
  %_3 = tail call i8 @llvm.scmp.i8.i64(i64 %a, i64 %b)
  %_0 = sext i8 %_3 to i64
  ret i64 %_0
}

declare range(i8 -1, 2) i8 @llvm.scmp.i8.i64(i64, i64)
```



To unsubscribe from these emails, change your notification settings at https://github.com/llvm/llvm-project/settings/notifications


More information about the All-commits mailing list