[llvm] ea863cb - [DAGCombiner] Fix APInt truncation assertion (#209914)
via llvm-commits
llvm-commits at lists.llvm.org
Fri Jul 17 13:54:18 PDT 2026
Author: Arthur Eubanks
Date: 2026-07-17T20:54:13Z
New Revision: ea863cb2dec8e0f040dfe22a52d3b537e4a25c29
URL: https://github.com/llvm/llvm-project/commit/ea863cb2dec8e0f040dfe22a52d3b537e4a25c29
DIFF: https://github.com/llvm/llvm-project/commit/ea863cb2dec8e0f040dfe22a52d3b537e4a25c29.diff
LOG: [DAGCombiner] Fix APInt truncation assertion (#209914)
getConstant() would create an APInt with ImplicitTrunc = false, but the
constant here can really be anything.
Fixes #209884.
Added:
llvm/test/CodeGen/X86/dagcombine-sub-globaladdress.ll
Modified:
llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
Removed:
################################################################################
diff --git a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
index aba6d8c5ac310..32e6d65159b5e 100644
--- a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
@@ -4560,8 +4560,10 @@ SDValue DAGCombiner::visitSUB(SDNode *N) {
// fold (sub Sym+c1, Sym+c2) -> c1-c2
if (GlobalAddressSDNode *GB = dyn_cast<GlobalAddressSDNode>(N1))
if (GA->getGlobal() == GB->getGlobal())
- return DAG.getConstant((uint64_t)GA->getOffset() - GB->getOffset(),
- DL, VT);
+ return DAG.getConstant(
+ APInt(VT.getScalarSizeInBits(), GA->getOffset() - GB->getOffset(),
+ /*isSigned=*/false, /*implicitTrunc=*/true),
+ DL, VT);
}
// sub X, (sextinreg Y i1) -> add X, (and Y 1)
diff --git a/llvm/test/CodeGen/X86/dagcombine-sub-globaladdress.ll b/llvm/test/CodeGen/X86/dagcombine-sub-globaladdress.ll
new file mode 100644
index 0000000000000..433ed8603859d
--- /dev/null
+++ b/llvm/test/CodeGen/X86/dagcombine-sub-globaladdress.ll
@@ -0,0 +1,37 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
+; RUN: llc < %s -mtriple=i386-pc-windows-msvc | FileCheck %s
+
+ at global = external constant i8
+
+define i32 @sub_global_offset() {
+; CHECK-LABEL: sub_global_offset:
+; CHECK: # %bb.0:
+; CHECK-NEXT: movl $-2, %eax
+; CHECK-NEXT: retl
+ %a = ptrtoint ptr @global to i32
+ %b = ptrtoint ptr getelementptr (i8, ptr @global, i32 2) to i32
+ %sub = sub i32 %a, %b
+ ret i32 %sub
+}
+
+define i32 @sub_global_offset_large_unsigned() {
+; CHECK-LABEL: sub_global_offset_large_unsigned:
+; CHECK: # %bb.0:
+; CHECK-NEXT: movl $-2147483648, %eax # imm = 0x80000000
+; CHECK-NEXT: retl
+ %a = ptrtoint ptr getelementptr (i8, ptr @global, i32 2147483648) to i32
+ %b = ptrtoint ptr @global to i32
+ %sub = sub i32 %a, %b
+ ret i32 %sub
+}
+
+define i32 @sub_global_offset_negative_large() {
+; CHECK-LABEL: sub_global_offset_negative_large:
+; CHECK: # %bb.0:
+; CHECK-NEXT: movl $-2147483648, %eax # imm = 0x80000000
+; CHECK-NEXT: retl
+ %a = ptrtoint ptr @global to i32
+ %b = ptrtoint ptr getelementptr (i8, ptr @global, i32 2147483648) to i32
+ %sub = sub i32 %a, %b
+ ret i32 %sub
+}
More information about the llvm-commits
mailing list