Skip to content
This repository was archived by the owner on Mar 28, 2020. It is now read-only.

Commit 22f5417

Browse files
committed
Use shouldAssumeDSOLocal on AArch64.
This reduces code duplication and now AArch64 also handles PIE. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@270844 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 10c072c commit 22f5417

File tree

5 files changed

+69
-62
lines changed

5 files changed

+69
-62
lines changed

include/llvm/CodeGen/Analysis.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@
1717
#include "llvm/ADT/ArrayRef.h"
1818
#include "llvm/ADT/DenseMap.h"
1919
#include "llvm/ADT/SmallVector.h"
20+
#include "llvm/ADT/Triple.h"
2021
#include "llvm/CodeGen/ISDOpcodes.h"
2122
#include "llvm/IR/CallSite.h"
2223
#include "llvm/IR/InlineAsm.h"
2324
#include "llvm/IR/Instructions.h"
25+
#include "llvm/Support/CodeGen.h"
2426

2527
namespace llvm {
2628
class GlobalValue;
@@ -118,6 +120,9 @@ bool returnTypeIsEligibleForTailCall(const Function *F,
118120
// or we are in LTO.
119121
bool canBeOmittedFromSymbolTable(const GlobalValue *GV);
120122

123+
bool shouldAssumeDSOLocal(Reloc::Model RM, const Triple &TT, const Module &M,
124+
const GlobalValue *GV);
125+
121126
DenseMap<const MachineBasicBlock *, int>
122127
getFuncletMembership(const MachineFunction &MF);
123128

lib/CodeGen/Analysis.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -650,6 +650,49 @@ bool llvm::canBeOmittedFromSymbolTable(const GlobalValue *GV) {
650650
return !GS.IsCompared;
651651
}
652652

653+
// FIXME: make this a proper option
654+
static bool CanUseCopyRelocWithPIE = false;
655+
656+
bool llvm::shouldAssumeDSOLocal(Reloc::Model RM, const Triple &TT,
657+
const Module &M, const GlobalValue *GV) {
658+
// DLLImport explicitly marks the GV as external.
659+
if (GV && GV->hasDLLImportStorageClass())
660+
return false;
661+
662+
// Every other GV is local on COFF
663+
if (TT.isOSBinFormatCOFF())
664+
return true;
665+
666+
if (RM == Reloc::Static)
667+
return true;
668+
669+
if (GV && (GV->hasLocalLinkage() || !GV->hasDefaultVisibility()))
670+
return true;
671+
672+
if (TT.isOSBinFormatELF()) {
673+
assert(RM != Reloc::DynamicNoPIC);
674+
// Some linkers can use copy relocations with pie executables.
675+
if (M.getPIELevel() != PIELevel::Default) {
676+
if (CanUseCopyRelocWithPIE)
677+
return true;
678+
679+
// If the symbol is defined, it cannot be preempted.
680+
if (GV && !GV->isDeclarationForLinker())
681+
return true;
682+
return false;
683+
}
684+
685+
// ELF supports preemption of other symbols.
686+
return false;
687+
}
688+
689+
assert(TT.isOSBinFormatMachO());
690+
if (GV && GV->isStrongDefinitionForLinker())
691+
return true;
692+
693+
return false;
694+
}
695+
653696
static void collectFuncletMembers(
654697
DenseMap<const MachineBasicBlock *, int> &FuncletMembership, int Funclet,
655698
const MachineBasicBlock *MBB) {

lib/Target/AArch64/AArch64Subtarget.cpp

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "AArch64Subtarget.h"
1515
#include "AArch64InstrInfo.h"
1616
#include "AArch64PBQPRegAlloc.h"
17+
#include "llvm/CodeGen/Analysis.h"
1718
#include "llvm/CodeGen/MachineScheduler.h"
1819
#include "llvm/IR/GlobalValue.h"
1920
#include "llvm/Support/TargetRegistry.h"
@@ -73,39 +74,25 @@ const RegisterBankInfo *AArch64Subtarget::getRegBankInfo() const {
7374
unsigned char
7475
AArch64Subtarget::ClassifyGlobalReference(const GlobalValue *GV,
7576
const TargetMachine &TM) const {
76-
bool isDef = GV->isStrongDefinitionForLinker();
77-
7877
// MachO large model always goes via a GOT, simply to get a single 8-byte
7978
// absolute relocation on all global addresses.
8079
if (TM.getCodeModel() == CodeModel::Large && isTargetMachO())
8180
return AArch64II::MO_GOT;
8281

82+
Reloc::Model RM = TM.getRelocationModel();
83+
if (!shouldAssumeDSOLocal(RM, TargetTriple, *GV->getParent(), GV))
84+
return AArch64II::MO_GOT;
85+
8386
// The small code mode's direct accesses use ADRP, which cannot necessarily
8487
// produce the value 0 (if the code is above 4GB).
8588
if (TM.getCodeModel() == CodeModel::Small && GV->hasExternalWeakLinkage()) {
8689
// In PIC mode use the GOT, but in absolute mode use a constant pool load.
87-
if (TM.getRelocationModel() == Reloc::Static)
90+
if (RM == Reloc::Static)
8891
return AArch64II::MO_CONSTPOOL;
8992
else
9093
return AArch64II::MO_GOT;
9194
}
9295

93-
// If symbol visibility is hidden, the extra load is not needed if
94-
// the symbol is definitely defined in the current translation unit.
95-
96-
// The handling of non-hidden symbols in PIC mode is rather target-dependent:
97-
// + On MachO, if the symbol is defined in this module the GOT can be
98-
// skipped.
99-
// + On ELF, the R_AARCH64_COPY relocation means that even symbols actually
100-
// defined could end up in unexpected places. Use a GOT.
101-
if (TM.getRelocationModel() != Reloc::Static && GV->hasDefaultVisibility()) {
102-
if (isTargetMachO())
103-
return isDef ? AArch64II::MO_NO_FLAG : AArch64II::MO_GOT;
104-
else
105-
// No need to go through the GOT for local symbols on ELF.
106-
return GV->hasLocalLinkage() ? AArch64II::MO_NO_FLAG : AArch64II::MO_GOT;
107-
}
108-
10996
return AArch64II::MO_NO_FLAG;
11097
}
11198

lib/Target/X86/X86Subtarget.cpp

Lines changed: 1 addition & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "X86Subtarget.h"
1515
#include "X86InstrInfo.h"
1616
#include "X86TargetMachine.h"
17+
#include "llvm/CodeGen/Analysis.h"
1718
#include "llvm/IR/Attributes.h"
1819
#include "llvm/IR/Function.h"
1920
#include "llvm/IR/GlobalValue.h"
@@ -50,49 +51,6 @@ unsigned char X86Subtarget::classifyBlockAddressReference() const {
5051
return classifyLocalReference(nullptr);
5152
}
5253

53-
// FIXME: make this a proper option
54-
static bool CanUseCopyRelocWithPIE = false;
55-
56-
static bool shouldAssumeDSOLocal(Reloc::Model RM, const Triple &TT,
57-
const Module &M, const GlobalValue *GV) {
58-
// DLLImport explicitly marks the GV as external.
59-
if (GV && GV->hasDLLImportStorageClass())
60-
return false;
61-
62-
// Every other GV is local on COFF
63-
if (TT.isOSBinFormatCOFF())
64-
return true;
65-
66-
if (RM == Reloc::Static)
67-
return true;
68-
69-
if (GV && (GV->hasLocalLinkage() || !GV->hasDefaultVisibility()))
70-
return true;
71-
72-
if (TT.isOSBinFormatELF()) {
73-
assert(RM != Reloc::DynamicNoPIC);
74-
// Some linkers can use copy relocations with pie executables.
75-
if (M.getPIELevel() != PIELevel::Default) {
76-
if (CanUseCopyRelocWithPIE)
77-
return true;
78-
79-
// If the symbol is defined, it cannot be preempted.
80-
if (GV && !GV->isDeclarationForLinker())
81-
return true;
82-
return false;
83-
}
84-
85-
// ELF supports preemption of other symbols.
86-
return false;
87-
}
88-
89-
assert(TT.isOSBinFormatMachO());
90-
if (GV && GV->isStrongDefinitionForLinker())
91-
return true;
92-
93-
return false;
94-
}
95-
9654
/// Classify a global variable reference for the current subtarget according to
9755
/// how we should reference it in a non-pcrel context.
9856
unsigned char

test/CodeGen/AArch64/pie.ll

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
; RUN: llc -mtriple aarch64-pc-linux -relocation-model=pic < %s | FileCheck %s
2+
3+
@g1 = global i32 42
4+
5+
define i32* @get_g1() {
6+
; CHECK: get_g1:
7+
; CHECK: adrp x0, g1
8+
; CHECK-NEXT: add x0, x0, :lo12:g1
9+
ret i32* @g1
10+
}
11+
12+
!llvm.module.flags = !{!0}
13+
14+
!0 = !{i32 1, !"PIE Level", i32 2}

0 commit comments

Comments
 (0)