Skip to content

[SelectionDAG] Drop unnecessary lower bound check in lowerRangeToAssertZExt - #196785

Merged
IrisUntainted merged 5 commits into
mainfrom
users/el-ev/_selectiondag_drop_unnecessary_lower_bound_check_in_lowerrangetoassertzext
May 11, 2026
Merged

IrisUntainted merged 5 commits into
mainfrom
users/el-ev/_selectiondag_drop_unnecessary_lower_bound_check_in_lowerrangetoassertzext

Conversation

@IrisUntainted

@IrisUntainted IrisUntainted commented May 10, 2026 •

Copy link
Copy Markdown
Member

Drop the Lo.isMinValue() check in lowerRangeToAssertZExt.

The check was introduced in 2bba779 when the bit width was computed via logBase2(Hi), which required Lo == 0 for correctness. It is no longer needed since 9e04bef when we switched to getUnsignedMax().getActiveBits() for bit width.

The change in DAGCombiner.cpp is to prevent a regression in llvm/test/CodeGen/AMDGPU/llvm.amdgcn.wavefrontsize.ll. I wasn't able to construct an individual test for it.

IrisUntainted commented May 10, 2026 •

Copy link
Copy Markdown
Member Author

@llvmorg-github-actions

llvmorg-github-actions Bot commented May 10, 2026 •

Copy link
Copy Markdown

@llvm/pr-subscribers-backend-x86
@llvm/pr-subscribers-backend-amdgpu

@llvm/pr-subscribers-llvm-selectiondag

Author: Iris Shi (el-ev)

Changes

Drop the Lo.isMinValue() check in lowerRangeToAssertZExt.

The check was introduced in 2bba779 when the bit width was computed via logBase2(Hi), which required Lo == 0 for correctness. It is no longer needed since 9e04bef when we switched to getUnsignedMax().getActiveBits() for bit width.

The change in DAGCombiner.cpp is to prevent a regression in llvm/test/CodeGen/AMDGPU/llvm.amdgcn.wavefrontsize.ll. I wasn't able to construct an individual test for it.


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

3 Files Affected:

  • (modified) llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp (+4)
  • (modified) llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp (-4)
  • (modified) llvm/test/CodeGen/AMDGPU/bit-op-reduce-width-known-bits.ll (+3-3)
diff --git a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
index 5dd025f1b42be..87dfa2f5a1c50 100644
--- a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
@@ -16213,6 +16213,10 @@ SDValue DAGCombiner::visitAssertExt(SDNode *N) {
       AssertVT == cast<VTSDNode>(N0.getOperand(1))->getVT())
     return N0;
 
+  // fold (assert?ext c, vt) -> c
+  if (isa<ConstantSDNode>(N0))
+    return N0;
+
   if (N0.getOpcode() == ISD::TRUNCATE && N0.hasOneUse() &&
       N0.getOperand(0).getOpcode() == Opcode) {
     // We have an assert, truncate, assert sandwich. Make one stronger assert
diff --git a/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp b/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp
index 894f04bb4668d..c001f0e0bd450 100644
--- a/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp
@@ -10853,10 +10853,6 @@ SDValue SelectionDAGBuilder::lowerRangeToAssertZExt(SelectionDAG &DAG,
   if (!CR || CR->isFullSet() || CR->isEmptySet() || CR->isUpperWrapped())
     return Op;
 
-  APInt Lo = CR->getUnsignedMin();
-  if (!Lo.isMinValue())
-    return Op;
-
   APInt Hi = CR->getUnsignedMax();
   unsigned Bits = std::max(Hi.getActiveBits(),
                            static_cast<unsigned>(IntegerType::MIN_INT_BITS));
diff --git a/llvm/test/CodeGen/AMDGPU/bit-op-reduce-width-known-bits.ll b/llvm/test/CodeGen/AMDGPU/bit-op-reduce-width-known-bits.ll
index ad26dfa7f93e8..a8be7e5827af0 100644
--- a/llvm/test/CodeGen/AMDGPU/bit-op-reduce-width-known-bits.ll
+++ b/llvm/test/CodeGen/AMDGPU/bit-op-reduce-width-known-bits.ll
@@ -11,7 +11,7 @@ define i64 @v_xor_i64_known_hi_i32_from_arg_range(i64 range(i64 0, 4294967296) %
 ; CHECK-LABEL: v_xor_i64_known_hi_i32_from_arg_range:
 ; CHECK:       ; %bb.0:
 ; CHECK-NEXT:    s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
-; CHECK-NEXT:    v_xor_b32_e32 v1, v1, v3
+; CHECK-NEXT:    v_mov_b32_e32 v1, v3
 ; CHECK-NEXT:    v_xor_b32_e32 v0, v0, v2
 ; CHECK-NEXT:    s_setpc_b64 s[30:31]
   %xor = xor i64 %arg0, %arg1
@@ -24,7 +24,7 @@ define i64 @v_or_i64_known_hi_i32_from_arg_range(i64 range(i64 0, 4294967296) %a
 ; CHECK-LABEL: v_or_i64_known_hi_i32_from_arg_range:
 ; CHECK:       ; %bb.0:
 ; CHECK-NEXT:    s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
-; CHECK-NEXT:    v_or_b32_e32 v1, v1, v3
+; CHECK-NEXT:    v_mov_b32_e32 v1, v3
 ; CHECK-NEXT:    v_or_b32_e32 v0, v0, v2
 ; CHECK-NEXT:    s_setpc_b64 s[30:31]
   %or = or i64 %arg0, %arg1
@@ -50,7 +50,7 @@ define i64 @s_xor_i64_known_i32_from_arg_range(i64 range(i64 0, 65) inreg %arg)
 ; CHECK-NEXT:    s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
 ; CHECK-NEXT:    s_not_b64 s[4:5], s[16:17]
 ; CHECK-NEXT:    v_mov_b32_e32 v0, s4
-; CHECK-NEXT:    v_mov_b32_e32 v1, s5
+; CHECK-NEXT:    v_mov_b32_e32 v1, -1
 ; CHECK-NEXT:    s_setpc_b64 s[30:31]
   %xor = xor i64 %arg, -1
   ret i64 %xor

@github-actions

github-actions Bot commented May 10, 2026 •

Copy link
Copy Markdown

🐧 Linux x64 Test Results

  • 195127 tests passed
  • 5196 tests skipped

✅ The build succeeded and all tests passed.

@github-actions

github-actions Bot commented May 10, 2026 •

Copy link
Copy Markdown

🪟 Windows x64 Test Results

  • 134520 tests passed
  • 3259 tests skipped

✅ The build succeeded and all tests passed.

@IrisUntainted
IrisUntainted force-pushed the users/el-ev/_selectiondag_drop_unnecessary_lower_bound_check_in_lowerrangetoassertzext branch from 777dbd3 to 4cb20fe Compare May 10, 2026 07:41

// fold (assert?ext c, vt) -> c
if (isa<ConstantSDNode>(N0))
return N0;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be better to handle this in FoldConstantArithmetic?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would still need a call to DAG.FoldConstantArithmetic here, neither cleaner nor cheaper.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FoldConstantArithmetic() is intended to be called as part of getNode().

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It turns out to be not enough. In the failing case llvm.amdgcn.wavefrontsize() is legalized to a constant after creation, thereby escaping getNode.

Legalizing: t15: i32 = llvm.amdgcn.wavefrontsize TargetConstant:i64<3656>                                                                                                    
 ... replacing: t15: i32 = llvm.amdgcn.wavefrontsize TargetConstant:i64<3656>
     with:      t24: i32 = Constant<32> 
...
Combining: t17: i32 = AssertZext Constant:i32<32>, ValueType:ch:i7
 ... into: t18: i32 = Constant<32>

@IrisUntainted
IrisUntainted force-pushed the users/el-ev/_selectiondag_drop_unnecessary_lower_bound_check_in_lowerrangetoassertzext branch from 4cb20fe to d66420c Compare May 10, 2026 09:57
@IrisUntainted
IrisUntainted force-pushed the users/el-ev/fix_abdu_fold branch from 2235483 to e535d2f Compare May 10, 2026 09:57
@IrisUntainted
IrisUntainted force-pushed the users/el-ev/_selectiondag_drop_unnecessary_lower_bound_check_in_lowerrangetoassertzext branch from d66420c to a79dea5 Compare May 11, 2026 10:36
@IrisUntainted
IrisUntainted force-pushed the users/el-ev/fix_abdu_fold branch from e535d2f to 013f722 Compare May 11, 2026 10:36

@nikic nikic left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@IrisUntainted
IrisUntainted force-pushed the users/el-ev/fix_abdu_fold branch from 013f722 to 74028a2 Compare May 11, 2026 11:43
@IrisUntainted
IrisUntainted force-pushed the users/el-ev/_selectiondag_drop_unnecessary_lower_bound_check_in_lowerrangetoassertzext branch from a79dea5 to 310084a Compare May 11, 2026 11:43

@RKSimon RKSimon left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Base automatically changed from users/el-ev/fix_abdu_fold to main May 11, 2026 14:51
@IrisUntainted
IrisUntainted merged commit b49288f into main May 11, 2026
11 of 15 checks passed
@IrisUntainted
IrisUntainted deleted the users/el-ev/_selectiondag_drop_unnecessary_lower_bound_check_in_lowerrangetoassertzext branch May 11, 2026 14:52
EuphoricThinking pushed a commit to EuphoricThinking/llvm-project that referenced this pull request May 14, 2026
…ertZExt` (llvm#196785)

Drop the `Lo.isMinValue()` check in `lowerRangeToAssertZExt`.
                                                                  
The check was introduced in 2bba779 when the bit width was computed
via `logBase2(Hi)`, which required `Lo == 0` for correctness. It is no
longer needed since 9e04bef when we switched to
`getUnsignedMax().getActiveBits()` for bit width.


The change in `DAGCombiner.cpp` is to prevent a regression in
`llvm/test/CodeGen/AMDGPU/llvm.amdgcn.wavefrontsize.ll`. I wasn't able
to construct an individual test for it.

---------

Co-authored-by: nikic <[email protected]>
pedroMVicente pushed a commit to pedroMVicente/llvm-project that referenced this pull request May 19, 2026
…ertZExt` (llvm#196785)

Drop the `Lo.isMinValue()` check in `lowerRangeToAssertZExt`.
                                                                  
The check was introduced in 2bba779 when the bit width was computed
via `logBase2(Hi)`, which required `Lo == 0` for correctness. It is no
longer needed since 9e04bef when we switched to
`getUnsignedMax().getActiveBits()` for bit width.


The change in `DAGCombiner.cpp` is to prevent a regression in
`llvm/test/CodeGen/AMDGPU/llvm.amdgcn.wavefrontsize.ll`. I wasn't able
to construct an individual test for it.

---------

Co-authored-by: nikic <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants