[llvm] [WebAssembly] Mark SIMD min and max as commutable (PR #219812)

Anutosh Bhat via llvm-commits llvm-commits at lists.llvm.org
Sun Aug 30 08:22:57 PDT 2026


https://github.com/anutosh491 created https://github.com/llvm/llvm-project/pull/219812

Vector add/mul and scalar floating-point min/max are already marked as commutable. This extends the same property to floating-point vector min/max, allowing better WebAssembly register stackification.

Should avoid any locals as per what's happening now
```
.local v128

call      red
local.set 0

call      green
local.get 0

f32x4.min
```

>From cae67a0d4df66b806a331d60c5ef4ba099e39bc8 Mon Sep 17 00:00:00 2001
From: anutosh491 <andersonbhat491 at gmail.com>
Date: Sun, 30 Aug 2026 20:50:07 +0530
Subject: [PATCH] [WebAssembly] Mark SIMD min and max as commutable

---
 .../WebAssembly/WebAssemblyInstrSIMD.td       |  2 +
 llvm/test/CodeGen/WebAssembly/reg-stackify.ll | 48 ++++++++++++++++++-
 2 files changed, 48 insertions(+), 2 deletions(-)

diff --git a/llvm/lib/Target/WebAssembly/WebAssemblyInstrSIMD.td b/llvm/lib/Target/WebAssembly/WebAssemblyInstrSIMD.td
index 0a3ee8f8eedc0..5c5a5f2946135 100644
--- a/llvm/lib/Target/WebAssembly/WebAssemblyInstrSIMD.td
+++ b/llvm/lib/Target/WebAssembly/WebAssemblyInstrSIMD.td
@@ -1345,9 +1345,11 @@ defm MUL : SIMDBinaryFP<fmul, "mul", 230>;
 defm DIV : SIMDBinaryFP<fdiv, "div", 231>;
 
 // NaN-propagating minimum: min
+let isCommutable = 1 in
 defm MIN : SIMDBinaryFP<fminimum, "min", 232>;
 
 // NaN-propagating maximum: max
+let isCommutable = 1 in
 defm MAX : SIMDBinaryFP<fmaximum, "max", 233>;
 
 // Pseudo-minimum: pmin
diff --git a/llvm/test/CodeGen/WebAssembly/reg-stackify.ll b/llvm/test/CodeGen/WebAssembly/reg-stackify.ll
index 6c97f41d858b3..046603194c501 100644
--- a/llvm/test/CodeGen/WebAssembly/reg-stackify.ll
+++ b/llvm/test/CodeGen/WebAssembly/reg-stackify.ll
@@ -1,5 +1,5 @@
-; RUN: llc < %s -asm-verbose=false -disable-wasm-fallthrough-return-opt -wasm-disable-explicit-locals -wasm-keep-registers -verify-machineinstrs | FileCheck %s
-; RUN: llc < %s -asm-verbose=false -disable-wasm-fallthrough-return-opt -verify-machineinstrs | FileCheck %s --check-prefix=NOREGS
+; RUN: llc < %s -mattr=+simd128 -asm-verbose=false -disable-wasm-fallthrough-return-opt -wasm-disable-explicit-locals -wasm-keep-registers -verify-machineinstrs | FileCheck %s
+; RUN: llc < %s -mattr=+simd128 -asm-verbose=false -disable-wasm-fallthrough-return-opt -verify-machineinstrs | FileCheck %s --check-prefix=NOREGS
 
 ; Test the register stackifier pass.
 
@@ -426,6 +426,50 @@ define i32 @commute() {
   ret i32 %add3
 }
 
+; CHECK-LABEL: commute_simd_min:
+; CHECK:  .functype commute_simd_min () -> (v128){{$}}
+; CHECK-NEXT:  call        $push0=, red_v4f32{{$}}
+; CHECK-NEXT:  call        $push1=, green_v4f32{{$}}
+; CHECK-NEXT:  f32x4.min   $push2=, $pop0, $pop1{{$}}
+; CHECK-NEXT:  return      $pop2{{$}}
+; NOREGS-LABEL: commute_simd_min:
+; NOREGS:  .functype commute_simd_min () -> (v128){{$}}
+; NOREGS-NEXT:  call        red_v4f32{{$}}
+; NOREGS-NEXT:  call        green_v4f32{{$}}
+; NOREGS-NEXT:  f32x4.min{{$}}
+; NOREGS-NEXT:  return{{$}}
+declare <4 x float> @red_v4f32()
+declare <4 x float> @green_v4f32()
+declare <4 x float> @llvm.minimum.v4f32(<4 x float>, <4 x float>)
+define <4 x float> @commute_simd_min() {
+  %red = call <4 x float> @red_v4f32()
+  %green = call <4 x float> @green_v4f32()
+  %min = call <4 x float> @llvm.minimum.v4f32(<4 x float> %green,
+                                              <4 x float> %red)
+  ret <4 x float> %min
+}
+
+; CHECK-LABEL: commute_simd_max:
+; CHECK:  .functype commute_simd_max () -> (v128){{$}}
+; CHECK-NEXT:  call        $push0=, red_v4f32{{$}}
+; CHECK-NEXT:  call        $push1=, green_v4f32{{$}}
+; CHECK-NEXT:  f32x4.max   $push2=, $pop0, $pop1{{$}}
+; CHECK-NEXT:  return      $pop2{{$}}
+; NOREGS-LABEL: commute_simd_max:
+; NOREGS:  .functype commute_simd_max () -> (v128){{$}}
+; NOREGS-NEXT:  call        red_v4f32{{$}}
+; NOREGS-NEXT:  call        green_v4f32{{$}}
+; NOREGS-NEXT:  f32x4.max{{$}}
+; NOREGS-NEXT:  return{{$}}
+declare <4 x float> @llvm.maximum.v4f32(<4 x float>, <4 x float>)
+define <4 x float> @commute_simd_max() {
+  %red = call <4 x float> @red_v4f32()
+  %green = call <4 x float> @green_v4f32()
+  %max = call <4 x float> @llvm.maximum.v4f32(<4 x float> %green,
+                                              <4 x float> %red)
+  ret <4 x float> %max
+}
+
 ; Don't stackify a register when it would move a the def of the register past
 ; an implicit local.get for the register.
 



More information about the llvm-commits mailing list