[llvm] [AMDGPU] Preserve live SCC when shrinking disjoint S_OR_B32 to S_ADDK_I32 (PR #228025)
via llvm-commits
llvm-commits at lists.llvm.org
Thu Oct 1 03:30:15 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-backend-amdgpu
Author: Teja Alaghari (TejaX-Alaghari)
<details>
<summary>Changes</summary>
Do not shrink a disjoint `s_or_b32` to `s_addk_i32` when SCC is live. The instructions produce equivalent data results but incompatible SCC values.
`or` sets SCC when the result is nonzero whereas, `addk` sets SCC when there's a signed overflow.
This causes silent code after compare folding.
---
Full diff: https://github.com/llvm/llvm-project/pull/228025.diff
2 Files Affected:
- (modified) llvm/lib/Target/AMDGPU/SIShrinkInstructions.cpp (+6)
- (modified) llvm/test/CodeGen/AMDGPU/s_or_b32_transformation.ll (+29)
``````````diff
diff --git a/llvm/lib/Target/AMDGPU/SIShrinkInstructions.cpp b/llvm/lib/Target/AMDGPU/SIShrinkInstructions.cpp
index 1483fd43ad8b1..41afb13ea4307 100644
--- a/llvm/lib/Target/AMDGPU/SIShrinkInstructions.cpp
+++ b/llvm/lib/Target/AMDGPU/SIShrinkInstructions.cpp
@@ -953,6 +953,12 @@ bool SIShrinkInstructions::run(MachineFunction &MF) {
}
if (Src0->isReg() && Src0->getReg() == Dest->getReg()) {
if (Src1->isImm() && isKImmOperand(MI, *Src1)) {
+ // S_OR_B32 sets SCC to (result != 0), but S_ADDK_I32 sets it to
+ // signed overflow, so only shrink the OR when SCC is dead.
+ if (MI.getOpcode() == AMDGPU::S_OR_B32 &&
+ !MI.findRegisterDefOperand(AMDGPU::SCC, /*TRI=*/nullptr)
+ ->isDead())
+ continue;
unsigned Opc = (MI.getOpcode() == AMDGPU::S_MUL_I32)
? AMDGPU::S_MULK_I32
: AMDGPU::S_ADDK_I32;
diff --git a/llvm/test/CodeGen/AMDGPU/s_or_b32_transformation.ll b/llvm/test/CodeGen/AMDGPU/s_or_b32_transformation.ll
index 68f90e7a73e27..53a1fe89af520 100644
--- a/llvm/test/CodeGen/AMDGPU/s_or_b32_transformation.ll
+++ b/llvm/test/CodeGen/AMDGPU/s_or_b32_transformation.ll
@@ -1,6 +1,7 @@
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 6
; RUN: llc -mtriple=amdgpu9.00 < %s | FileCheck %s
; This tests if disjoint s_or_b32 gets transformed to s_addk_i32 when we can't use s_bitset1_b32
+; and SCC is dead.
define amdgpu_ps i32 @s_or_b32_i32(i32 inreg %x) {
; CHECK-LABEL: s_or_b32_i32:
@@ -29,3 +30,31 @@ define amdgpu_ps i32 @s_or_b32_to_s_bitset1_b32(i32 inreg %x) {
ret i32 %or
}
+define amdgpu_ps i32 @s_or_b32_disjoint_live_scc(i32 inreg %n) {
+; CHECK-LABEL: s_or_b32_disjoint_live_scc:
+; CHECK: ; %bb.0: ; %entry
+; CHECK-NEXT: s_lshl_b32 s0, s0, 31
+; CHECK-NEXT: s_or_b32 s0, s0, 0x800
+; CHECK-NEXT: s_cselect_b32 s0, 0, 1
+; CHECK-NEXT: ; return to shader part epilog
+entry:
+ switch i32 0, label %tail [
+ i32 0, label %case
+ ]
+
+case:
+ br label %tail
+
+tail:
+ %phi = phi i32 [ 32, %case ], [ 0, %entry ]
+ %ins0 = insertelement <2 x i32> poison, i32 %n, i32 0
+ %ins1 = insertelement <2 x i32> %ins0, i32 %phi, i32 1
+ %mul = mul <2 x i32> %ins1, <i32 -2147483648, i32 64>
+ %e0 = extractelement <2 x i32> %mul, i32 0
+ %e1 = extractelement <2 x i32> %mul, i32 1
+ %s1 = add i32 %e0, %e1
+ %cmp = icmp eq i32 %s1, 0
+ %sel = select i1 %cmp, i32 1, i32 0
+ ret i32 %sel
+}
+
``````````
</details>
https://github.com/llvm/llvm-project/pull/228025
More information about the llvm-commits
mailing list