[llvm] [DAGCombiner] Fix APInt truncation assertion (PR #209914)

Arthur Eubanks via llvm-commits llvm-commits at lists.llvm.org
Fri Jul 17 11:00:46 PDT 2026


https://github.com/aeubanks updated https://github.com/llvm/llvm-project/pull/209914

>From 636a86252bef25426165536db56fc5114bb8a49f Mon Sep 17 00:00:00 2001
From: Arthur Eubanks <aeubanks at google.com>
Date: Wed, 15 Jul 2026 22:37:50 +0000
Subject: [PATCH] [DAGCombiner] Fix APInt truncation assertion

getConstant() would create an APInt with ImplicitTrunc = false, but the constant here can really be anything.

Fixes #209884.
---
 llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp |  6 ++-
 .../X86/dagcombine-sub-globaladdress.ll       | 37 +++++++++++++++++++
 2 files changed, 41 insertions(+), 2 deletions(-)
 create mode 100644 llvm/test/CodeGen/X86/dagcombine-sub-globaladdress.ll

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