[llvm] [AMDGPU][GISel] Fold 'min(min(x,y),z)' and 'max(max(x,y),z)' into min3 and max3 (PR #200410)

via llvm-commits llvm-commits at lists.llvm.org
Sat Jun 6 07:07:19 PDT 2026


================
@@ -481,6 +491,101 @@ bool AMDGPURegBankCombinerImpl::combineD16Load(MachineInstr &MI) const {
   return false;
 }
 
+void AMDGPURegBankCombinerImpl::applyMinMaxToMinMax3(
+    MachineInstr &MI, MinMaxToMinMax3MatchInfo &MatchInfo) const {
+  B.buildInstr(MatchInfo.Opc, {MI.getOperand(0)},
+               {MatchInfo.Val0, MatchInfo.Val1, MatchInfo.Val2}, MI.getFlags());
+  MI.eraseFromParent();
+  return;
+}
+
+// min(min(a, b), c) == min(a, min(b, c)) == min3(a, b, c)
+// supported scalar type: S32 S16 U32 U16 F32 F16
+// supported vector type: V2F32
----------------
xiongzile wrote:

```C
; RUN: llc -mtriple=amdgcn-amd-mesa3d -mcpu=gfx1010 -verify-machineinstrs < %s | FileCheck -check-
prefix=DAGGFX10 %s
define <2 x float> @test_fmin3_v2f32(<2 x float> %a, <2 x float> %b, <2 x float> %c) {
; DAGGFX10-LABEL: test_fmin3_v2f32:
; DAGGFX10:       ; %bb.0:
; DAGGFX10-NEXT:    s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
; DAGGFX10-NEXT:    v_min3_f32 v0, v0, v2, v4
; DAGGFX10-NEXT:    v_min3_f32 v1, v1, v3, v5
; DAGGFX10-NEXT:    s_setpc_b64 s[30:31]
  %min1 = call <2 x float> @llvm.minnum.v2f32(<2 x float> %a, <2 x float> %b)
  %min2 = call <2 x float> @llvm.minnum.v2f32(<2 x float> %min1, <2 x float> %c)
  ret <2 x float> %min2
}
```
I use the output of SelectionDAG for testing, and it generates v_min_f32?
The v_min3_f32 instruction doesn't seem to distinct the vector or scalar.
I can only support scalar if you'd like me to and consider vector later.

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


More information about the llvm-commits mailing list