[llvm] [AMDGPU][ISel] `setcc` peephole for null check of high 32 bits of a mask (PR #177662)

via llvm-commits llvm-commits at lists.llvm.org
Fri Jan 23 11:13:13 PST 2026


https://github.com/zGoldthorpe created https://github.com/llvm/llvm-project/pull/177662

This optimisation is motivated by [this minimal example](https://godbolt.org/z/9o83GvGsM).

Particularly, if `mask` is a 64-bit integer, a select
```cpp
auto out = ((mask >> 32) != 0) ? a : b;
```
converts the null-check on the higher 32-bits of `mask` into a `v_cmp_lt_u64` with the integer `1 << 32` stored in a pair of VGPRs (effectively wasting two VGPRs).

On the other hand, if we were instead checking the lower 32 bits of `mask`, the assembly generated performs an `s_cmp_eq_u32` between the 32-bit register holding the lower half of `mask` with the immediate `0`.

This patch modifies ISel so that the analogous codegen occurs when checking the upper half of `mask`.

>From b38aade13a0b08357fdced4c927fa4cf0a536234 Mon Sep 17 00:00:00 2001
From: Zach Goldthorpe <Zach.Goldthorpe at amd.com>
Date: Fri, 23 Jan 2026 12:54:32 -0600
Subject: [PATCH] Added peephole for `setcc`.

---
 llvm/lib/Target/AMDGPU/SIISelLowering.cpp |  7 +++++++
 llvm/test/CodeGen/AMDGPU/setcc64.ll       | 11 +++++++++++
 2 files changed, 18 insertions(+)

diff --git a/llvm/lib/Target/AMDGPU/SIISelLowering.cpp b/llvm/lib/Target/AMDGPU/SIISelLowering.cpp
index 657eec1d93076..c0179107e8ae4 100644
--- a/llvm/lib/Target/AMDGPU/SIISelLowering.cpp
+++ b/llvm/lib/Target/AMDGPU/SIISelLowering.cpp
@@ -16849,6 +16849,13 @@ SDValue SITargetLowering::performSetCCCombine(SDNode *N,
           return LHS.getOperand(0);
       }
     }
+
+    // setcc v.64, 0x1'000'000, ult => setcc v.hi32, 0, eq
+    if (VT == MVT::i64 && CRHSVal.getZExtValue() == 1ull << 32 &&
+        CC == ISD::SETULT) {
+      return DAG.getSetCC(SL, N->getValueType(0), getHiHalf64(LHS, DAG),
+                          DAG.getConstant(0, SL, MVT::i32), ISD::SETEQ);
+    }
   }
 
   // Eliminate setcc by using carryout from add/sub instruction
diff --git a/llvm/test/CodeGen/AMDGPU/setcc64.ll b/llvm/test/CodeGen/AMDGPU/setcc64.ll
index b36ed3e91c573..4660b1c1e93e2 100644
--- a/llvm/test/CodeGen/AMDGPU/setcc64.ll
+++ b/llvm/test/CodeGen/AMDGPU/setcc64.ll
@@ -284,4 +284,15 @@ entry:
   ret void
 }
 
+; GCN-LABEL: {{^}}i64_select:
+; GCN: s_cmp_eq_u32
+; GCN: s_cselect_b32
+; GCN: s_cselect_b32
+define amdgpu_kernel void @i64_select(ptr addrspace(1) %out, i64 %a, i64 %b, i64 %mask) #0 {
+  %mask.hi.z = icmp ult i64 %mask, 4294967296
+  %tmp = select i1 %mask.hi.z, i64 %a, i64 %b
+  store i64 %tmp, ptr addrspace(1) %out
+  ret void
+}
+
 attributes #0 = { nounwind }



More information about the llvm-commits mailing list