[llvm] [SeparateConstOffsetFromGEP] Stop distributing sext/zext over lossy trunc (PR #221381)

Fujun Han via llvm-commits llvm-commits at lists.llvm.org
Fri Sep 4 19:27:40 PDT 2026


https://github.com/Peter9606 updated https://github.com/llvm/llvm-project/pull/221381

>From a593e4767704406e272de89706faadc29213f84f Mon Sep 17 00:00:00 2001
From: Fujun Han <fujun.han at iluvatar.com>
Date: Sat, 5 Sep 2026 10:06:57 +0800
Subject: [PATCH 1/2] [SeparateConstOffsetFromGEP] Precommit tests for
 sext/zext of trunc (NFC)

The nuw/nsw flags on an add hold at the width of the add, which says
nothing about wrapping at the width of a truncation applied to its
result. These tests capture the current behavior of distributing a
sext/zext above a truncation into the operands of the add; the lossy
cases are miscompiled today.

Assisted-by: Cursor (kimi-k3)
Signed-off-by: Fujun Han <fujun.han at iluvatar.com>
---
 .../ext-of-trunc-add-wrap.ll                  | 113 ++++++++++++++++++
 1 file changed, 113 insertions(+)
 create mode 100644 llvm/test/Transforms/SeparateConstOffsetFromGEP/ext-of-trunc-add-wrap.ll

diff --git a/llvm/test/Transforms/SeparateConstOffsetFromGEP/ext-of-trunc-add-wrap.ll b/llvm/test/Transforms/SeparateConstOffsetFromGEP/ext-of-trunc-add-wrap.ll
new file mode 100644
index 0000000000000..0642ed26d033c
--- /dev/null
+++ b/llvm/test/Transforms/SeparateConstOffsetFromGEP/ext-of-trunc-add-wrap.ll
@@ -0,0 +1,113 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6
+; RUN: opt -S -mtriple=x86_64-unknown-linux-gnu -passes=separate-const-offset-from-gep < %s | FileCheck %s
+
+; The nuw/nsw flags on an add hold at the width of the add, which says nothing
+; about wrapping at the width of a truncation applied to its result.  A
+; sext/zext above such a truncation must therefore not be distributed into the
+; operands of the add.
+
+; zext i64 (trunc i8 (add nuw i32 A, B)) wraps modulo 256, so the constant 1
+; must stay inside the truncation.  For A = 251 and B = 5 the index is 0, while
+; distributing the casts would give 251 + 5 = 256.
+define ptr addrspace(1) @zext_of_lossy_trunc(ptr addrspace(1) %p, i8 %a, i64 %iv) {
+; CHECK-LABEL: define ptr addrspace(1) @zext_of_lossy_trunc(
+; CHECK-SAME: ptr addrspace(1) [[P:%.*]], i8 [[A:%.*]], i64 [[IV:%.*]]) {
+; CHECK-NEXT:    [[AZ:%.*]] = zext i8 [[A]] to i32
+; CHECK-NEXT:    [[TMP1:%.*]] = trunc i32 [[AZ]] to i8
+; CHECK-NEXT:    [[TMP2:%.*]] = zext nneg i8 [[TMP1]] to i64
+; CHECK-NEXT:    [[T:%.*]] = trunc i64 [[IV]] to i8
+; CHECK-NEXT:    [[TZ:%.*]] = zext i8 [[T]] to i32
+; CHECK-NEXT:    [[SUM8:%.*]] = trunc i32 [[TZ]] to i8
+; CHECK-NEXT:    [[IDX:%.*]] = zext nneg i8 [[SUM8]] to i64
+; CHECK-NEXT:    [[SUM2:%.*]] = add i64 [[TMP2]], [[IDX]]
+; CHECK-NEXT:    [[TMP7:%.*]] = getelementptr inbounds i32, ptr addrspace(1) [[P]], i64 [[SUM2]]
+; CHECK-NEXT:    [[Q:%.*]] = getelementptr inbounds i8, ptr addrspace(1) [[TMP7]], i64 4
+; CHECK-NEXT:    ret ptr addrspace(1) [[Q]]
+;
+  %iv.next = add nuw nsw i64 %iv, 1
+  %t = trunc i64 %iv.next to i8
+  %az = zext i8 %a to i32
+  %tz = zext i8 %t to i32
+  %sum = add nuw nsw i32 %az, %tz
+  %sum8 = trunc i32 %sum to i8
+  %idx = zext nneg i8 %sum8 to i64
+  %q = getelementptr inbounds i32, ptr addrspace(1) %p, i64 %idx
+  ret ptr addrspace(1) %q
+}
+
+; Same shape, but masking keeps the sum below 256, so the truncation loses
+; nothing and the constant can still be hoisted.
+define ptr addrspace(1) @zext_of_lossless_trunc(ptr addrspace(1) %p, i32 %x) {
+; CHECK-LABEL: define ptr addrspace(1) @zext_of_lossless_trunc(
+; CHECK-SAME: ptr addrspace(1) [[P:%.*]], i32 [[X:%.*]]) {
+; CHECK-NEXT:    [[A:%.*]] = and i32 [[X]], 15
+; CHECK-NEXT:    [[TMP1:%.*]] = trunc i32 [[A]] to i8
+; CHECK-NEXT:    [[TMP2:%.*]] = zext nneg i8 [[TMP1]] to i64
+; CHECK-NEXT:    [[TMP3:%.*]] = getelementptr inbounds i32, ptr addrspace(1) [[P]], i64 [[TMP2]]
+; CHECK-NEXT:    [[Q2:%.*]] = getelementptr inbounds i8, ptr addrspace(1) [[TMP3]], i64 12
+; CHECK-NEXT:    ret ptr addrspace(1) [[Q2]]
+;
+  %a = and i32 %x, 15
+  %sum = add nuw nsw i32 %a, 3
+  %sum8 = trunc i32 %sum to i8
+  %idx = zext nneg i8 %sum8 to i64
+  %q = getelementptr inbounds i32, ptr addrspace(1) %p, i64 %idx
+  ret ptr addrspace(1) %q
+}
+
+; sext additionally needs the sign bit of the narrow value to be clear.  Here
+; the sum reaches 130, so sext i8 of the truncation is negative and the
+; constant must stay inside the truncation.
+define ptr addrspace(1) @sext_of_lossy_trunc(ptr addrspace(1) %p, i32 %x) {
+; CHECK-LABEL: define ptr addrspace(1) @sext_of_lossy_trunc(
+; CHECK-SAME: ptr addrspace(1) [[P:%.*]], i32 [[X:%.*]]) {
+; CHECK-NEXT:    [[A:%.*]] = and i32 [[X]], 127
+; CHECK-NEXT:    [[SUM8:%.*]] = trunc i32 [[A]] to i8
+; CHECK-NEXT:    [[IDX:%.*]] = sext i8 [[SUM8]] to i64
+; CHECK-NEXT:    [[Q:%.*]] = getelementptr i32, ptr addrspace(1) [[P]], i64 [[IDX]]
+; CHECK-NEXT:    [[Q2:%.*]] = getelementptr i8, ptr addrspace(1) [[Q]], i64 12
+; CHECK-NEXT:    ret ptr addrspace(1) [[Q2]]
+;
+  %a = and i32 %x, 127
+  %sum = add nuw nsw i32 %a, 3
+  %sum8 = trunc i32 %sum to i8
+  %idx = sext i8 %sum8 to i64
+  %q = getelementptr i32, ptr addrspace(1) %p, i64 %idx
+  ret ptr addrspace(1) %q
+}
+
+; With the sign bit clear both sext and zext reproduce the wider value, so the
+; constant can be hoisted out of a sext'ed truncation as well.
+define ptr addrspace(1) @sext_of_lossless_trunc(ptr addrspace(1) %p, i32 %x) {
+; CHECK-LABEL: define ptr addrspace(1) @sext_of_lossless_trunc(
+; CHECK-SAME: ptr addrspace(1) [[P:%.*]], i32 [[X:%.*]]) {
+; CHECK-NEXT:    [[A:%.*]] = and i32 [[X]], 63
+; CHECK-NEXT:    [[TMP1:%.*]] = trunc i32 [[A]] to i8
+; CHECK-NEXT:    [[TMP2:%.*]] = sext i8 [[TMP1]] to i64
+; CHECK-NEXT:    [[TMP3:%.*]] = getelementptr i32, ptr addrspace(1) [[P]], i64 [[TMP2]]
+; CHECK-NEXT:    [[Q2:%.*]] = getelementptr i8, ptr addrspace(1) [[TMP3]], i64 12
+; CHECK-NEXT:    ret ptr addrspace(1) [[Q2]]
+;
+  %a = and i32 %x, 63
+  %sum = add nuw nsw i32 %a, 3
+  %sum8 = trunc i32 %sum to i8
+  %idx = sext i8 %sum8 to i64
+  %q = getelementptr i32, ptr addrspace(1) %p, i64 %idx
+  ret ptr addrspace(1) %q
+}
+
+; A truncation with no extension above it still distributes over the add,
+; because truncation is exact in modular arithmetic.
+define ptr addrspace(1) @bare_trunc(ptr addrspace(1) %p, i128 %i) {
+; CHECK-LABEL: define ptr addrspace(1) @bare_trunc(
+; CHECK-SAME: ptr addrspace(1) [[P:%.*]], i128 [[I:%.*]]) {
+; CHECK-NEXT:    [[TMP1:%.*]] = trunc i128 [[I]] to i64
+; CHECK-NEXT:    [[TMP2:%.*]] = getelementptr i32, ptr addrspace(1) [[P]], i64 [[TMP1]]
+; CHECK-NEXT:    [[Q2:%.*]] = getelementptr i8, ptr addrspace(1) [[TMP2]], i64 4
+; CHECK-NEXT:    ret ptr addrspace(1) [[Q2]]
+;
+  %idx = add i128 %i, 1
+  %idx.conv = trunc i128 %idx to i64
+  %q = getelementptr i32, ptr addrspace(1) %p, i64 %idx.conv
+  ret ptr addrspace(1) %q
+}

>From 6b180c5c4a52e157994e91f5340c466f8ec2887a Mon Sep 17 00:00:00 2001
From: Fujun Han <fujun.han at iluvatar.com>
Date: Sat, 5 Sep 2026 10:06:57 +0800
Subject: [PATCH 2/2] [SeparateConstOffsetFromGEP] Stop distributing sext/zext
 over lossy trunc

find() traces through a trunc while keeping a pending sext/zext, and
canTraceInto() then justifies distributing the extension into the
operands of an add/sub by checking its nuw/nsw flags. Those flags
hold at the width of the add but say nothing about wrapping at the
truncation width, so

  zext i64 (trunc i8 (add nuw i32 (zext i8 251), (zext i8 5)))

which is 0 was rebuilt as 251 + 5 = 256.

Keep tracing through a trunc with a pending sext/zext only when
known-bits proves the truncation loses no bits. For sext the sign
bit of the truncated value must be clear as well, so that sext and
zext agree on the extended value.

Assisted-by: Cursor (kimi-k3)
Signed-off-by: Fujun Han <fujun.han at iluvatar.com>
---
 .../Scalar/SeparateConstOffsetFromGEP.cpp     | 33 +++++++++++++++++--
 .../ext-of-trunc-add-wrap.ll                  | 18 +++++-----
 2 files changed, 38 insertions(+), 13 deletions(-)

diff --git a/llvm/lib/Transforms/Scalar/SeparateConstOffsetFromGEP.cpp b/llvm/lib/Transforms/Scalar/SeparateConstOffsetFromGEP.cpp
index 4870b8c888279..3eddbfb0b38d6 100644
--- a/llvm/lib/Transforms/Scalar/SeparateConstOffsetFromGEP.cpp
+++ b/llvm/lib/Transforms/Scalar/SeparateConstOffsetFromGEP.cpp
@@ -298,6 +298,13 @@ class ConstantOffsetExtractor {
   bool canTraceInto(bool SignExtended, bool ZeroExtended, BinaryOperator *BO,
                     GetElementPtrInst *GEP, Value *Idx);
 
+  /// A helper function that returns whether truncating \p V to \p BitWidth and
+  /// extending it back reproduces \p V, so that a sext/zext applied after the
+  /// truncation can still be reasoned about at the width of \p V.
+  ///
+  /// \p Signed Whether the value is extended back with sext rather than zext.
+  bool truncPreservesValue(Value *V, unsigned BitWidth, bool Signed);
+
   /// Analyze a xor expression, and identify the bits in the constant operand
   /// that are disjoint from the base operand's known set bits. For these
   /// disjoint bits, a xor is equivalent to an addition, which allows us to
@@ -684,6 +691,18 @@ bool ConstantOffsetExtractor::canTraceInto(bool SignExtended, bool ZeroExtended,
   return true;
 }
 
+bool ConstantOffsetExtractor::truncPreservesValue(Value *V, unsigned BitWidth,
+                                                  bool Signed) {
+  unsigned SrcBits = V->getType()->getScalarSizeInBits();
+  if (SrcBits <= BitWidth)
+    return true;
+
+  // Extending back with sext additionally requires the sign bit of the
+  // truncated value to be clear, so that sext and zext agree.
+  unsigned KeepBits = Signed ? BitWidth - 1 : BitWidth;
+  return computeKnownBits(V, DL).countMinLeadingZeros() >= SrcBits - KeepBits;
+}
+
 APInt ConstantOffsetExtractor::findInEitherOperand(BinaryOperator *BO,
                                                    bool SignExtended,
                                                    bool ZeroExtended) {
@@ -741,9 +760,17 @@ APInt ConstantOffsetExtractor::find(Value *V, GetElementPtrInst *GEP,
     else if (BO->getOpcode() == Instruction::Xor)
       ConstantOffset = extractDisjointBitsFromXor(BO);
   } else if (isa<TruncInst>(V)) {
-    ConstantOffset =
-        find(U->getOperand(0), GEP, Idx, SignExtended, ZeroExtended)
-            .trunc(BitWidth);
+    // A pending sext/zext applies to the truncated value, but canTraceInto
+    // checks nuw/nsw at the width of the operand it traces into.  Those flags
+    // say nothing about wrapping at BitWidth, so keep searching only when the
+    // truncation loses no bits.  Otherwise
+    //   zext i64 (trunc i8 (add nuw i32 (zext i8 251), (zext i8 5)))
+    // which is 0 would be rebuilt as 251 + 5 = 256.
+    if ((!SignExtended && !ZeroExtended) ||
+        truncPreservesValue(U->getOperand(0), BitWidth, SignExtended))
+      ConstantOffset =
+          find(U->getOperand(0), GEP, Idx, SignExtended, ZeroExtended)
+              .trunc(BitWidth);
   } else if (isa<SExtInst>(V)) {
     ConstantOffset =
         find(U->getOperand(0), GEP, Idx, /* SignExtended */ true, ZeroExtended)
diff --git a/llvm/test/Transforms/SeparateConstOffsetFromGEP/ext-of-trunc-add-wrap.ll b/llvm/test/Transforms/SeparateConstOffsetFromGEP/ext-of-trunc-add-wrap.ll
index 0642ed26d033c..3ba6286a99b0e 100644
--- a/llvm/test/Transforms/SeparateConstOffsetFromGEP/ext-of-trunc-add-wrap.ll
+++ b/llvm/test/Transforms/SeparateConstOffsetFromGEP/ext-of-trunc-add-wrap.ll
@@ -12,16 +12,14 @@
 define ptr addrspace(1) @zext_of_lossy_trunc(ptr addrspace(1) %p, i8 %a, i64 %iv) {
 ; CHECK-LABEL: define ptr addrspace(1) @zext_of_lossy_trunc(
 ; CHECK-SAME: ptr addrspace(1) [[P:%.*]], i8 [[A:%.*]], i64 [[IV:%.*]]) {
+; CHECK-NEXT:    [[IV_NEXT:%.*]] = add nuw nsw i64 [[IV]], 1
+; CHECK-NEXT:    [[T:%.*]] = trunc i64 [[IV_NEXT]] to i8
 ; CHECK-NEXT:    [[AZ:%.*]] = zext i8 [[A]] to i32
-; CHECK-NEXT:    [[TMP1:%.*]] = trunc i32 [[AZ]] to i8
-; CHECK-NEXT:    [[TMP2:%.*]] = zext nneg i8 [[TMP1]] to i64
-; CHECK-NEXT:    [[T:%.*]] = trunc i64 [[IV]] to i8
 ; CHECK-NEXT:    [[TZ:%.*]] = zext i8 [[T]] to i32
-; CHECK-NEXT:    [[SUM8:%.*]] = trunc i32 [[TZ]] to i8
+; CHECK-NEXT:    [[SUM:%.*]] = add nuw nsw i32 [[AZ]], [[TZ]]
+; CHECK-NEXT:    [[SUM8:%.*]] = trunc i32 [[SUM]] to i8
 ; CHECK-NEXT:    [[IDX:%.*]] = zext nneg i8 [[SUM8]] to i64
-; CHECK-NEXT:    [[SUM2:%.*]] = add i64 [[TMP2]], [[IDX]]
-; CHECK-NEXT:    [[TMP7:%.*]] = getelementptr inbounds i32, ptr addrspace(1) [[P]], i64 [[SUM2]]
-; CHECK-NEXT:    [[Q:%.*]] = getelementptr inbounds i8, ptr addrspace(1) [[TMP7]], i64 4
+; CHECK-NEXT:    [[Q:%.*]] = getelementptr inbounds i32, ptr addrspace(1) [[P]], i64 [[IDX]]
 ; CHECK-NEXT:    ret ptr addrspace(1) [[Q]]
 ;
   %iv.next = add nuw nsw i64 %iv, 1
@@ -62,11 +60,11 @@ define ptr addrspace(1) @sext_of_lossy_trunc(ptr addrspace(1) %p, i32 %x) {
 ; CHECK-LABEL: define ptr addrspace(1) @sext_of_lossy_trunc(
 ; CHECK-SAME: ptr addrspace(1) [[P:%.*]], i32 [[X:%.*]]) {
 ; CHECK-NEXT:    [[A:%.*]] = and i32 [[X]], 127
-; CHECK-NEXT:    [[SUM8:%.*]] = trunc i32 [[A]] to i8
+; CHECK-NEXT:    [[SUM:%.*]] = add nuw nsw i32 [[A]], 3
+; CHECK-NEXT:    [[SUM8:%.*]] = trunc i32 [[SUM]] to i8
 ; CHECK-NEXT:    [[IDX:%.*]] = sext i8 [[SUM8]] to i64
 ; CHECK-NEXT:    [[Q:%.*]] = getelementptr i32, ptr addrspace(1) [[P]], i64 [[IDX]]
-; CHECK-NEXT:    [[Q2:%.*]] = getelementptr i8, ptr addrspace(1) [[Q]], i64 12
-; CHECK-NEXT:    ret ptr addrspace(1) [[Q2]]
+; CHECK-NEXT:    ret ptr addrspace(1) [[Q]]
 ;
   %a = and i32 %x, 127
   %sum = add nuw nsw i32 %a, 3



More information about the llvm-commits mailing list