[PATCH] D77248: [IR] Update Constant::needsRelocation() to strip constant inbound GEPs and PLT relocations

Leonard Chan via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Jul 13 16:17:53 PDT 2020


leonardchan updated this revision to Diff 277604.
leonardchan retitled this revision from "[IR] Strip constant inbound GEPs when checking Constant::needsRelocation()" to "[IR] Update Constant::needsRelocation() to strip constant inbound GEPs and PLT relocations".
leonardchan edited the summary of this revision.
leonardchan added reviewers: phosek, mcgrathr.

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D77248/new/

https://reviews.llvm.org/D77248

Files:
  llvm/lib/IR/Constants.cpp
  llvm/test/CodeGen/X86/relptr-rodata.ll


Index: llvm/test/CodeGen/X86/relptr-rodata.ll
===================================================================
--- llvm/test/CodeGen/X86/relptr-rodata.ll
+++ llvm/test/CodeGen/X86/relptr-rodata.ll
@@ -19,3 +19,27 @@
 ; CHECK: relro2:
 ; CHECK: .long hidden-relro2
 @relro2 = constant i32 trunc (i64 sub (i64 ptrtoint (i8* @hidden to i64), i64 ptrtoint (i32* @relro2 to i64)) to i32)
+
+; CHECK:      .section .rodata.cst8
+; CHECK-NEXT: .globl obj
+; CHECK:      obj:
+; CHECK:      .long 0
+; CHECK:      .long (hidden_func-obj)-4
+
+declare hidden void @hidden_func()
+
+; Ensure that inbound GEPs with constant offsets are also resolved.
+ at obj = dso_local unnamed_addr constant { { i32, i32 } } {
+  { i32, i32 } {
+    i32 0,
+    i32 trunc (i64 sub (i64 ptrtoint (void ()* @hidden_func to i64), i64 ptrtoint (i32* getelementptr inbounds ({ { i32, i32 } }, { { i32, i32 } }* @obj, i32 0, i32 0, i32 1) to i64)) to i32)
+  } }, align 4
+
+; CHECK:      .section .rodata.rodata2
+; CHECK-NEXT: .globl rodata2
+; CHECK:      rodata2:
+; CHECK:      .long extern_func at PLT-rodata2
+
+declare void @extern_func() unnamed_addr
+
+ at rodata2 = hidden constant i32 trunc (i64 sub (i64 ptrtoint (void ()* @extern_func to i64), i64 ptrtoint (i32* @rodata2 to i64)) to i32)
Index: llvm/lib/IR/Constants.cpp
===================================================================
--- llvm/lib/IR/Constants.cpp
+++ llvm/lib/IR/Constants.cpp
@@ -646,10 +646,25 @@
           return false;
 
         // Relative pointers do not need to be dynamically relocated.
-        if (auto *LHSGV = dyn_cast<GlobalValue>(LHSOp0->stripPointerCasts()))
-          if (auto *RHSGV = dyn_cast<GlobalValue>(RHSOp0->stripPointerCasts()))
+        if (auto *LHSGV =
+                dyn_cast<GlobalValue>(LHSOp0->stripInBoundsConstantOffsets())) {
+          if (auto *RHSGV = dyn_cast<GlobalValue>(
+                  RHSOp0->stripInBoundsConstantOffsets())) {
             if (LHSGV->isDSOLocal() && RHSGV->isDSOLocal())
               return false;
+
+            // On AArch64 and x86_64, if the first operand is a function, we can
+            // guarantee that a PC relative relocation will be emitted between
+            // the PLT entry for that function and this global even if the the
+            // function is not dso_local.
+            const auto *M = LHSGV->getParent();
+            llvm::Triple TargetTriple(M->getTargetTriple());
+            auto Arch = TargetTriple.getArch();
+            if (isa<Function>(LHSGV) &&
+                (Arch == llvm::Triple::aarch64 || Arch == llvm::Triple::x86_64))
+              return false;
+          }
+        }
       }
     }
   }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D77248.277604.patch
Type: text/x-patch
Size: 2665 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200713/fd35227e/attachment.bin>


More information about the llvm-commits mailing list