AsmPrinter: Remove ELF's special lowerRelativeReference for unnamed_addr function

https://reviews.llvm.org/D17938 introduced lowerRelativeReference to
give ConstantExpr sub (A-B) special semantics in ELF: when `A` is an
`unnamed_addr` function, create a PLT-generating relocation. This was
intended for C++ relative vtables, but C++ relative vtable ended up
using DSOLocalEquivalent (lowerDSOLocalEquivalent).

This special treatment of `unnamed_addr` seems unusual.
Let's remove it. Only COFF needs an overload to generate a @IMGREL32
relocation specifier (llvm/test/MC/COFF/cross-section-relative.ll).

Pull Request: https://github.com/llvm/llvm-project/pull/132684
diff --git a/llvm/include/llvm/CodeGen/TargetLoweringObjectFileImpl.h b/llvm/include/llvm/CodeGen/TargetLoweringObjectFileImpl.h
index 8b0e579..f035d81 100644
--- a/llvm/include/llvm/CodeGen/TargetLoweringObjectFileImpl.h
+++ b/llvm/include/llvm/CodeGen/TargetLoweringObjectFileImpl.h
@@ -125,10 +125,6 @@
   lowerSymbolDifference(const MCSymbol *LHS, const MCSymbol *RHS,
                         int64_t Addend,
                         std::optional<int64_t> PCRelativeOffset) const;
-  const MCExpr *lowerRelativeReference(const GlobalValue *LHS,
-                                       const GlobalValue *RHS, int64_t Addend,
-                                       std::optional<int64_t> PCRelativeOffset,
-                                       const TargetMachine &TM) const override;
 
   const MCExpr *lowerDSOLocalEquivalent(const MCSymbol *LHS,
                                         const MCSymbol *RHS, int64_t Addend,
diff --git a/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp b/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
index 4c20c5d..c941529 100644
--- a/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
+++ b/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
@@ -1233,24 +1233,6 @@
   return Res;
 }
 
-const MCExpr *TargetLoweringObjectFileELF::lowerRelativeReference(
-    const GlobalValue *LHS, const GlobalValue *RHS, int64_t Addend,
-    std::optional<int64_t> PCRelativeOffset, const TargetMachine &TM) const {
-  // We may only use a PLT-relative relocation to refer to unnamed_addr
-  // functions.
-  if (!LHS->hasGlobalUnnamedAddr() || !LHS->getValueType()->isFunctionTy())
-    return nullptr;
-
-  // Basic correctness checks.
-  if (LHS->getType()->getPointerAddressSpace() != 0 ||
-      RHS->getType()->getPointerAddressSpace() != 0 || LHS->isThreadLocal() ||
-      RHS->isThreadLocal())
-    return nullptr;
-
-  return lowerSymbolDifference(TM.getSymbol(LHS), TM.getSymbol(RHS), Addend,
-                               PCRelativeOffset);
-}
-
 // Reference the PLT entry of a function, optionally with a subtrahend (`RHS`).
 const MCExpr *TargetLoweringObjectFileELF::lowerDSOLocalEquivalent(
     const MCSymbol *LHS, const MCSymbol *RHS, int64_t Addend,
diff --git a/llvm/test/CodeGen/ARM/plt-relative-reloc.ll b/llvm/test/CodeGen/ARM/relative-reloc.ll
similarity index 78%
rename from llvm/test/CodeGen/ARM/plt-relative-reloc.ll
rename to llvm/test/CodeGen/ARM/relative-reloc.ll
index ede8919..6505372 100644
--- a/llvm/test/CodeGen/ARM/plt-relative-reloc.ll
+++ b/llvm/test/CodeGen/ARM/relative-reloc.ll
@@ -10,7 +10,8 @@
 declare void @fn2() unnamed_addr
 declare void @fn3()
 
+;; Create a PC-relative relocation that the linker might decline if the addend symbol is preemptible.
 ; CHECK: .long 0
-; CHECK-NEXT: .long fn1(prel31)-vtable-4
-; CHECK-NEXT: .long fn2(prel31)-vtable-4
+; CHECK-NEXT: .long fn1-vtable-4
+; CHECK-NEXT: .long fn2-vtable-4
 ; CHECK-NEXT: .long fn3-vtable-4
diff --git a/llvm/test/CodeGen/RISCV/plt-relative-reloc.ll b/llvm/test/CodeGen/RISCV/relative-reloc.ll
similarity index 84%
rename from llvm/test/CodeGen/RISCV/plt-relative-reloc.ll
rename to llvm/test/CodeGen/RISCV/relative-reloc.ll
index d2dceb7..6c94b9f 100644
--- a/llvm/test/CodeGen/RISCV/plt-relative-reloc.ll
+++ b/llvm/test/CodeGen/RISCV/relative-reloc.ll
@@ -12,10 +12,11 @@
 declare void @fn3()
 @global4 = external unnamed_addr global i8
 
+;; Create a PC-relative relocation that the linker might decline if the addend symbol is preemptible.
 ; CHECK:      vtable:
 ; CHECK-NEXT:         .word   0                               # 0x0
-; CHECK-NEXT:         .word   %pltpcrel(fn1)
-; CHECK-NEXT:         .word   %pltpcrel(fn2+4)
+; CHECK-NEXT:         .word   fn1-vtable-4
+; CHECK-NEXT:         .word   fn2-vtable-4
 ; CHECK-NEXT:         .word   fn3-vtable-4
 ; CHECK-NEXT:         .word   global4-vtable-4
 ; CHECK-NEXT:         .size   vtable, 20
diff --git a/llvm/test/CodeGen/X86/x86-plt-relative-reloc.ll b/llvm/test/CodeGen/X86/relative-reloc-32.ll
similarity index 88%
rename from llvm/test/CodeGen/X86/x86-plt-relative-reloc.ll
rename to llvm/test/CodeGen/X86/relative-reloc-32.ll
index d5e8028..7d0b1fd 100644
--- a/llvm/test/CodeGen/X86/x86-plt-relative-reloc.ll
+++ b/llvm/test/CodeGen/X86/relative-reloc-32.ll
@@ -11,6 +11,6 @@
 declare void @fn3()
 
 ; CHECK: .long 0
-; CHECK-NEXT: .long fn1@PLT-vtable-4
-; CHECK-NEXT: .long fn2@PLT-vtable-4
+; CHECK-NEXT: .long fn1-vtable-4
+; CHECK-NEXT: .long fn2-vtable-4
 ; CHECK-NEXT: .long fn3-vtable-4
diff --git a/llvm/test/CodeGen/X86/x86-64-plt-relative-reloc.ll b/llvm/test/CodeGen/X86/relative-reloc-64.ll
similarity index 84%
rename from llvm/test/CodeGen/X86/x86-64-plt-relative-reloc.ll
rename to llvm/test/CodeGen/X86/relative-reloc-64.ll
index 54736c9..6f88edf 100644
--- a/llvm/test/CodeGen/X86/x86-64-plt-relative-reloc.ll
+++ b/llvm/test/CodeGen/X86/relative-reloc-64.ll
@@ -12,8 +12,9 @@
 declare void @fn3()
 @global4 = external unnamed_addr global i8
 
+;; Create a PC-relative relocation that the linker might decline if the addend symbol is preemptible.
 ; CHECK: .long 0
-; CHECK-NEXT: .long fn1@PLT-vtable-4
-; CHECK-NEXT: .long fn2@PLT-vtable-4
+; CHECK-NEXT: .long fn1-vtable-4
+; CHECK-NEXT: .long fn2-vtable-4
 ; CHECK-NEXT: .long fn3-vtable-4
 ; CHECK-NEXT: .long global4-vtable-4