[llvm] [llvm] Allow GOTPCREL replacements for negative offsets (PR #72308)

via llvm-commits llvm-commits at lists.llvm.org
Tue Nov 14 12:29:03 PST 2023


https://github.com/PiJoules created https://github.com/llvm/llvm-project/pull/72308

None

>From aeb82c32af49e30869416a9d927ba45d5da840a8 Mon Sep 17 00:00:00 2001
From: Leonard Chan <leonardchan at google.com>
Date: Tue, 14 Nov 2023 20:27:24 +0000
Subject: [PATCH] [llvm] Allow GOTPCREL replacements for negative offsets

---
 llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp |  5 -----
 llvm/test/MC/MachO/cstexpr-gotpcrel-64.ll  | 13 +++++++++++++
 2 files changed, 13 insertions(+), 5 deletions(-)

diff --git a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
index 8637d0658c2c905..15ff39883680369 100644
--- a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
+++ b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
@@ -3513,12 +3513,7 @@ static void handleIndirectSymViaGOTPCRel(AsmPrinter &AP, const MCExpr **ME,
   //
   //    gotpcrelcst := <offset from @foo base> + <cst>
   //
-  // If gotpcrelcst is positive it means that we can safely fold the pc rel
-  // displacement into the GOTPCREL. We can also can have an extra offset <cst>
-  // if the target knows how to encode it.
   int64_t GOTPCRelCst = Offset + MV.getConstant();
-  if (GOTPCRelCst < 0)
-    return;
   if (!AP.getObjFileLowering().supportGOTPCRelWithOffset() && GOTPCRelCst != 0)
     return;
 
diff --git a/llvm/test/MC/MachO/cstexpr-gotpcrel-64.ll b/llvm/test/MC/MachO/cstexpr-gotpcrel-64.ll
index 5fd1a962af467fe..18f9d97c4a876d9 100644
--- a/llvm/test/MC/MachO/cstexpr-gotpcrel-64.ll
+++ b/llvm/test/MC/MachO/cstexpr-gotpcrel-64.ll
@@ -99,3 +99,16 @@ define void @foo() {
 ; X86-NOGOT-EQUIV-LABEL: _c:
 ; X86-NOGOT-EQUIV:   .quad _b
 @c = global ptr @b
+
+; X86-LABEL: table_with_negative_offset:
+; X86-NEXT:    .long   _a at GOTPCREL+4294967292
+ at table_with_negative_offset = dso_local unnamed_addr constant [3 x i32] [
+  i32 trunc (
+    i64 sub (
+      i64 ptrtoint (ptr @b to i64),
+      i64 ptrtoint (ptr getelementptr inbounds ([3 x i32], ptr @table_with_negative_offset, i32 0, i32 2) to i64)
+    )
+    to i32),
+  i32 0,
+  i32 0
+], align 4



More information about the llvm-commits mailing list