[llvm] [DAG] Fold (umin (sub a b) a) -> (usubo a b); (select usubo.1 a usubo.0) (PR #161651)

Chaitanya Koparkar via llvm-commits llvm-commits at lists.llvm.org
Fri Oct 31 05:48:10 PDT 2025


ckoparkar wrote:

1. Test failure

<details>

Previously the code was:

```
if (N0.getOpcode() == ISD::SUB) {
  sd_match(N, m_UMin(m_Sub(m_Value(A), m_Value(B)), m_Deferred(A)))
  ...
```
Once I removed the `if (N0.getOpcode() == ISD::SUB)`, this pattern match grabbed this:

```
  SelectionDAG has 17 nodes:
  t2: i32,ch = CopyFromReg t0, Register:i32 %0
                t8: i32 = sub t2, Constant:i32<4>
              t9: i32 = umin t2, t8


N: t9: i32 = umin t2, t8
A: t2: i32,ch = CopyFromReg t0, Register:i32 %0
B: t7: i32 = Constant<4>
```

`t8: i32 = sub..` is the second argument of `umin` so I'm kind of confused as to why this pattern match succeeded. I'm now pattern matching on individual arguments of `umin`.

</details>

2. More tests

<details>

I've added some tests with vector types, incorrect patterns, etc. @arsenm also mentioned a test for multiple use.

I didn't guard against multiple uses here since I thought the results of `sub` and `umin` are still available after the fold. Am I misunderstanding something here and should I be guarding the pattern with `m_OneUse`?

</details>

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


More information about the llvm-commits mailing list