[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:14:03 PST 2026


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-backend-amdgpu

Author: None (zGoldthorpe)

<details>
<summary>Changes</summary>

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`.

---
Full diff: https://github.com/llvm/llvm-project/pull/177662.diff


2 Files Affected:

- (modified) llvm/lib/Target/AMDGPU/SIISelLowering.cpp (+7) 
- (modified) llvm/test/CodeGen/AMDGPU/setcc64.ll (+11) 


``````````diff
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 }

``````````

</details>


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


More information about the llvm-commits mailing list