[llvm] [CGP] Fix missing sign extension for base offset in optimizeMemoryInst (PR #161377)

Vladimir Radosavljevic via llvm-commits llvm-commits at lists.llvm.org
Fri Oct 10 03:10:48 PDT 2025


https://github.com/vladimirradosavljevic updated https://github.com/llvm/llvm-project/pull/161377

>From 926ce13aec9005dc616e27d1a8bee7ac8bb6d854 Mon Sep 17 00:00:00 2001
From: Vladimir Radosavljevic <vr at matterlabs.dev>
Date: Tue, 30 Sep 2025 15:25:58 +0200
Subject: [PATCH 1/5] [CGP] NFC: Add pre-commit test

---
 .../CodeGenPrepare/X86/baseoffs-sext-bug.ll   | 52 +++++++++++++++++++
 1 file changed, 52 insertions(+)
 create mode 100644 llvm/test/Transforms/CodeGenPrepare/X86/baseoffs-sext-bug.ll

diff --git a/llvm/test/Transforms/CodeGenPrepare/X86/baseoffs-sext-bug.ll b/llvm/test/Transforms/CodeGenPrepare/X86/baseoffs-sext-bug.ll
new file mode 100644
index 0000000000000..1ceeb492bdb03
--- /dev/null
+++ b/llvm/test/Transforms/CodeGenPrepare/X86/baseoffs-sext-bug.ll
@@ -0,0 +1,52 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6
+; RUN: opt -S -passes='require<profile-summary>,function(codegenprepare)' < %s | FileCheck --check-prefix=GEP %s
+; RUN: opt -S -passes='require<profile-summary>,function(codegenprepare)' -addr-sink-using-gep=false < %s | FileCheck --check-prefix=NO-GEP %s
+
+target triple = "x86_64--linux-gnu"
+target datalayout = "e-m:e-p0:128:128-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
+; -p0:128:128 is added to ensure that transformation will be triggered.
+
+define i128 @test(i128 %arg) {
+; GEP-LABEL: define i128 @test(
+; GEP-SAME: i128 [[ARG:%.*]]) {
+; GEP-NEXT:  [[ENTRY:.*]]:
+; GEP-NEXT:    [[CMP:%.*]] = icmp ugt i128 [[ARG]], 10
+; GEP-NEXT:    br i1 [[CMP]], label %[[THEN:.*]], label %[[EXIT:.*]]
+; GEP:       [[THEN]]:
+; GEP-NEXT:    [[SUNKADDR:%.*]] = inttoptr i128 [[ARG]] to ptr
+; GEP-NEXT:    [[SUNKADDR1:%.*]] = getelementptr i8, ptr [[SUNKADDR]], i128 18446744073709551584
+; GEP-NEXT:    [[LOAD:%.*]] = load i128, ptr [[SUNKADDR1]], align 16
+; GEP-NEXT:    br label %[[EXIT]]
+; GEP:       [[EXIT]]:
+; GEP-NEXT:    [[PHI:%.*]] = phi i128 [ [[LOAD]], %[[THEN]] ], [ 0, %[[ENTRY]] ]
+; GEP-NEXT:    ret i128 [[PHI]]
+;
+; NO-GEP-LABEL: define i128 @test(
+; NO-GEP-SAME: i128 [[ARG:%.*]]) {
+; NO-GEP-NEXT:  [[ENTRY:.*]]:
+; NO-GEP-NEXT:    [[CMP:%.*]] = icmp ugt i128 [[ARG]], 10
+; NO-GEP-NEXT:    br i1 [[CMP]], label %[[THEN:.*]], label %[[EXIT:.*]]
+; NO-GEP:       [[THEN]]:
+; NO-GEP-NEXT:    [[SUNKADDR:%.*]] = add i128 [[ARG]], 18446744073709551584
+; NO-GEP-NEXT:    [[SUNKADDR1:%.*]] = inttoptr i128 [[SUNKADDR]] to ptr
+; NO-GEP-NEXT:    [[LOAD:%.*]] = load i128, ptr [[SUNKADDR1]], align 16
+; NO-GEP-NEXT:    br label %[[EXIT]]
+; NO-GEP:       [[EXIT]]:
+; NO-GEP-NEXT:    [[PHI:%.*]] = phi i128 [ [[LOAD]], %[[THEN]] ], [ 0, %[[ENTRY]] ]
+; NO-GEP-NEXT:    ret i128 [[PHI]]
+;
+entry:
+  %add = add i128 %arg, -32
+  %cmp = icmp ugt i128 %arg, 10
+  br i1 %cmp, label %then, label %exit
+
+then:
+  %inttoptr = inttoptr i128 %add to ptr
+  %load = load i128, ptr %inttoptr, align 16
+  br label %exit
+
+exit:
+  %phi = phi i128 [ %load, %then ], [ 0, %entry ]
+  ret i128 %phi
+}
+

>From a0ca124e457e18a9db3806b4a1cf0ccf68273439 Mon Sep 17 00:00:00 2001
From: Vladimir Radosavljevic <vr at matterlabs.dev>
Date: Tue, 30 Sep 2025 15:27:36 +0200
Subject: [PATCH 2/5] [CGP] Fix missing sign extension for base offset in
 optimizeMemoryInst

If we have integers larger than 64-bit we need to
explicitly sign extend them, otherwise we will get
wrong zero extended values.
---
 llvm/lib/CodeGen/CodeGenPrepare.cpp                          | 4 ++--
 llvm/test/Transforms/CodeGenPrepare/X86/baseoffs-sext-bug.ll | 4 ++--
 2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/llvm/lib/CodeGen/CodeGenPrepare.cpp b/llvm/lib/CodeGen/CodeGenPrepare.cpp
index eb73d01b3558c..1a68577652f52 100644
--- a/llvm/lib/CodeGen/CodeGenPrepare.cpp
+++ b/llvm/lib/CodeGen/CodeGenPrepare.cpp
@@ -6100,7 +6100,7 @@ bool CodeGenPrepare::optimizeMemoryInst(Instruction *MemoryInst, Value *Addr,
 
       // Add in the Base Offset if present.
       if (AddrMode.BaseOffs) {
-        Value *V = ConstantInt::get(IntPtrTy, AddrMode.BaseOffs);
+        Value *V = ConstantInt::get(IntPtrTy, AddrMode.BaseOffs, true);
         if (ResultIndex) {
           // We need to add this separately from the scale above to help with
           // SDAG consecutive load/store merging.
@@ -6226,7 +6226,7 @@ bool CodeGenPrepare::optimizeMemoryInst(Instruction *MemoryInst, Value *Addr,
 
     // Add in the Base Offset if present.
     if (AddrMode.BaseOffs) {
-      Value *V = ConstantInt::get(IntPtrTy, AddrMode.BaseOffs);
+      Value *V = ConstantInt::get(IntPtrTy, AddrMode.BaseOffs, true);
       if (Result)
         Result = Builder.CreateAdd(Result, V, "sunkaddr");
       else
diff --git a/llvm/test/Transforms/CodeGenPrepare/X86/baseoffs-sext-bug.ll b/llvm/test/Transforms/CodeGenPrepare/X86/baseoffs-sext-bug.ll
index 1ceeb492bdb03..a8ef80b389dac 100644
--- a/llvm/test/Transforms/CodeGenPrepare/X86/baseoffs-sext-bug.ll
+++ b/llvm/test/Transforms/CodeGenPrepare/X86/baseoffs-sext-bug.ll
@@ -14,7 +14,7 @@ define i128 @test(i128 %arg) {
 ; GEP-NEXT:    br i1 [[CMP]], label %[[THEN:.*]], label %[[EXIT:.*]]
 ; GEP:       [[THEN]]:
 ; GEP-NEXT:    [[SUNKADDR:%.*]] = inttoptr i128 [[ARG]] to ptr
-; GEP-NEXT:    [[SUNKADDR1:%.*]] = getelementptr i8, ptr [[SUNKADDR]], i128 18446744073709551584
+; GEP-NEXT:    [[SUNKADDR1:%.*]] = getelementptr i8, ptr [[SUNKADDR]], i128 -32
 ; GEP-NEXT:    [[LOAD:%.*]] = load i128, ptr [[SUNKADDR1]], align 16
 ; GEP-NEXT:    br label %[[EXIT]]
 ; GEP:       [[EXIT]]:
@@ -27,7 +27,7 @@ define i128 @test(i128 %arg) {
 ; NO-GEP-NEXT:    [[CMP:%.*]] = icmp ugt i128 [[ARG]], 10
 ; NO-GEP-NEXT:    br i1 [[CMP]], label %[[THEN:.*]], label %[[EXIT:.*]]
 ; NO-GEP:       [[THEN]]:
-; NO-GEP-NEXT:    [[SUNKADDR:%.*]] = add i128 [[ARG]], 18446744073709551584
+; NO-GEP-NEXT:    [[SUNKADDR:%.*]] = add i128 [[ARG]], -32
 ; NO-GEP-NEXT:    [[SUNKADDR1:%.*]] = inttoptr i128 [[SUNKADDR]] to ptr
 ; NO-GEP-NEXT:    [[LOAD:%.*]] = load i128, ptr [[SUNKADDR1]], align 16
 ; NO-GEP-NEXT:    br label %[[EXIT]]

>From 57216c7b10036f4cc1521dcc744aab0a30ec9381 Mon Sep 17 00:00:00 2001
From: Vladimir Radosavljevic <vr at matterlabs.dev>
Date: Fri, 10 Oct 2025 12:04:07 +0200
Subject: [PATCH 3/5] [CGP] Address comments

---
 llvm/lib/CodeGen/CodeGenPrepare.cpp | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/llvm/lib/CodeGen/CodeGenPrepare.cpp b/llvm/lib/CodeGen/CodeGenPrepare.cpp
index 1a68577652f52..417917656a4bb 100644
--- a/llvm/lib/CodeGen/CodeGenPrepare.cpp
+++ b/llvm/lib/CodeGen/CodeGenPrepare.cpp
@@ -6100,7 +6100,7 @@ bool CodeGenPrepare::optimizeMemoryInst(Instruction *MemoryInst, Value *Addr,
 
       // Add in the Base Offset if present.
       if (AddrMode.BaseOffs) {
-        Value *V = ConstantInt::get(IntPtrTy, AddrMode.BaseOffs, true);
+        Value *V = ConstantInt::getSigned(IntPtrTy, AddrMode.BaseOffs);
         if (ResultIndex) {
           // We need to add this separately from the scale above to help with
           // SDAG consecutive load/store merging.
@@ -6226,7 +6226,7 @@ bool CodeGenPrepare::optimizeMemoryInst(Instruction *MemoryInst, Value *Addr,
 
     // Add in the Base Offset if present.
     if (AddrMode.BaseOffs) {
-      Value *V = ConstantInt::get(IntPtrTy, AddrMode.BaseOffs, true);
+      Value *V = ConstantInt::getSigned(IntPtrTy, AddrMode.BaseOffs);
       if (Result)
         Result = Builder.CreateAdd(Result, V, "sunkaddr");
       else

>From 68cdb30604eafe981597a3b52a0df3845b004520 Mon Sep 17 00:00:00 2001
From: Vladimir Radosavljevic <vr at matterlabs.dev>
Date: Fri, 10 Oct 2025 12:07:48 +0200
Subject: [PATCH 4/5] [CGP] NFC: Add test that exposes another issue

---
 .../CodeGenPrepare/X86/baseoffs-sext-bug.ll   | 29 +++++++++++++++++++
 1 file changed, 29 insertions(+)

diff --git a/llvm/test/Transforms/CodeGenPrepare/X86/baseoffs-sext-bug.ll b/llvm/test/Transforms/CodeGenPrepare/X86/baseoffs-sext-bug.ll
index a8ef80b389dac..cf09845737f4b 100644
--- a/llvm/test/Transforms/CodeGenPrepare/X86/baseoffs-sext-bug.ll
+++ b/llvm/test/Transforms/CodeGenPrepare/X86/baseoffs-sext-bug.ll
@@ -50,3 +50,32 @@ exit:
   ret i128 %phi
 }
 
+define void @test_combine(ptr %ptr, i128 %arg) {
+; GEP-LABEL: define void @test_combine(
+; GEP-SAME: ptr [[PTR:%.*]], i128 [[ARG:%.*]]) {
+; GEP-NEXT:  [[ENTRY:.*:]]
+; GEP-NEXT:    [[CMP:%.*]] = icmp ugt i128 [[ARG]], 10
+; GEP-NEXT:    [[SELECT1:%.*]] = select i1 [[CMP]], i128 18446744073709551584, i128 0
+; GEP-NEXT:    [[SUNKADDR:%.*]] = getelementptr inbounds i8, ptr [[PTR]], i128 [[SELECT1]]
+; GEP-NEXT:    store i128 1, ptr [[SUNKADDR]], align 16
+; GEP-NEXT:    ret void
+;
+; NO-GEP-LABEL: define void @test_combine(
+; NO-GEP-SAME: ptr [[PTR:%.*]], i128 [[ARG:%.*]]) {
+; NO-GEP-NEXT:  [[ENTRY:.*:]]
+; NO-GEP-NEXT:    [[CMP:%.*]] = icmp ugt i128 [[ARG]], 10
+; NO-GEP-NEXT:    [[SELECT1:%.*]] = select i1 [[CMP]], i128 18446744073709551584, i128 0
+; NO-GEP-NEXT:    [[SUNKADDR:%.*]] = ptrtoint ptr [[PTR]] to i128
+; NO-GEP-NEXT:    [[SUNKADDR2:%.*]] = add i128 [[SUNKADDR]], [[SELECT1]]
+; NO-GEP-NEXT:    [[SUNKADDR3:%.*]] = inttoptr i128 [[SUNKADDR2]] to ptr
+; NO-GEP-NEXT:    store i128 1, ptr [[SUNKADDR3]], align 16
+; NO-GEP-NEXT:    ret void
+;
+entry:
+  %cmp = icmp ugt i128 %arg, 10
+  %gep = getelementptr inbounds i8, ptr %ptr, i128 -32
+  %select = select i1 %cmp, ptr %gep, ptr %ptr
+  store i128 1, ptr %select, align 16
+  ret void
+}
+

>From 9691142d3e1d0446a70d5859ad5b529a1ed4134f Mon Sep 17 00:00:00 2001
From: Vladimir Radosavljevic <vr at matterlabs.dev>
Date: Fri, 10 Oct 2025 12:09:47 +0200
Subject: [PATCH 5/5] [CGP] Fix issue in one more place

---
 llvm/lib/CodeGen/CodeGenPrepare.cpp                          | 2 +-
 llvm/test/Transforms/CodeGenPrepare/X86/baseoffs-sext-bug.ll | 4 ++--
 2 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/llvm/lib/CodeGen/CodeGenPrepare.cpp b/llvm/lib/CodeGen/CodeGenPrepare.cpp
index 417917656a4bb..4320b1d7b1dc6 100644
--- a/llvm/lib/CodeGen/CodeGenPrepare.cpp
+++ b/llvm/lib/CodeGen/CodeGenPrepare.cpp
@@ -3194,7 +3194,7 @@ struct ExtAddrMode : public TargetLowering::AddrMode {
     case ScaledRegField:
       return ScaledReg;
     case BaseOffsField:
-      return ConstantInt::get(IntPtrTy, BaseOffs);
+      return ConstantInt::getSigned(IntPtrTy, BaseOffs);
     }
   }
 
diff --git a/llvm/test/Transforms/CodeGenPrepare/X86/baseoffs-sext-bug.ll b/llvm/test/Transforms/CodeGenPrepare/X86/baseoffs-sext-bug.ll
index cf09845737f4b..51a461e61d886 100644
--- a/llvm/test/Transforms/CodeGenPrepare/X86/baseoffs-sext-bug.ll
+++ b/llvm/test/Transforms/CodeGenPrepare/X86/baseoffs-sext-bug.ll
@@ -55,7 +55,7 @@ define void @test_combine(ptr %ptr, i128 %arg) {
 ; GEP-SAME: ptr [[PTR:%.*]], i128 [[ARG:%.*]]) {
 ; GEP-NEXT:  [[ENTRY:.*:]]
 ; GEP-NEXT:    [[CMP:%.*]] = icmp ugt i128 [[ARG]], 10
-; GEP-NEXT:    [[SELECT1:%.*]] = select i1 [[CMP]], i128 18446744073709551584, i128 0
+; GEP-NEXT:    [[SELECT1:%.*]] = select i1 [[CMP]], i128 -32, i128 0
 ; GEP-NEXT:    [[SUNKADDR:%.*]] = getelementptr inbounds i8, ptr [[PTR]], i128 [[SELECT1]]
 ; GEP-NEXT:    store i128 1, ptr [[SUNKADDR]], align 16
 ; GEP-NEXT:    ret void
@@ -64,7 +64,7 @@ define void @test_combine(ptr %ptr, i128 %arg) {
 ; NO-GEP-SAME: ptr [[PTR:%.*]], i128 [[ARG:%.*]]) {
 ; NO-GEP-NEXT:  [[ENTRY:.*:]]
 ; NO-GEP-NEXT:    [[CMP:%.*]] = icmp ugt i128 [[ARG]], 10
-; NO-GEP-NEXT:    [[SELECT1:%.*]] = select i1 [[CMP]], i128 18446744073709551584, i128 0
+; NO-GEP-NEXT:    [[SELECT1:%.*]] = select i1 [[CMP]], i128 -32, i128 0
 ; NO-GEP-NEXT:    [[SUNKADDR:%.*]] = ptrtoint ptr [[PTR]] to i128
 ; NO-GEP-NEXT:    [[SUNKADDR2:%.*]] = add i128 [[SUNKADDR]], [[SELECT1]]
 ; NO-GEP-NEXT:    [[SUNKADDR3:%.*]] = inttoptr i128 [[SUNKADDR2]] to ptr



More information about the llvm-commits mailing list