Skip to content

Commit 4d1f249

Browse files
authored
[ARM] Use TargetMachine over Subtarget in ARMAsmPrinter (#166329)
The subtarget may not be set if no functions are present in the module. Attempt to use the TargetMachine directly in more cases. Fixes #165422 Fixes #167577
1 parent ec085e5 commit 4d1f249

File tree

4 files changed

+33
-21
lines changed

4 files changed

+33
-21
lines changed

llvm/lib/Target/ARM/ARMAsmPrinter.cpp

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,8 @@ void ARMAsmPrinter::emitXXStructor(const DataLayout &DL, const Constant *CV) {
9797

9898
const MCExpr *E = MCSymbolRefExpr::create(
9999
GetARMGVSymbol(GV, ARMII::MO_NO_FLAG),
100-
(Subtarget->isTargetELF() ? ARM::S_TARGET1 : ARM::S_None), OutContext);
100+
(TM.getTargetTriple().isOSBinFormatELF() ? ARM::S_TARGET1 : ARM::S_None),
101+
OutContext);
101102

102103
OutStreamer->emitValue(E, Size);
103104
}
@@ -595,8 +596,7 @@ void ARMAsmPrinter::emitEndOfAsmFile(Module &M) {
595596
ARMTargetStreamer &ATS = static_cast<ARMTargetStreamer &>(TS);
596597

597598
if (OptimizationGoals > 0 &&
598-
(Subtarget->isTargetAEABI() || Subtarget->isTargetGNUAEABI() ||
599-
Subtarget->isTargetMuslAEABI()))
599+
(TT.isTargetAEABI() || TT.isTargetGNUAEABI() || TT.isTargetMuslAEABI()))
600600
ATS.emitAttribute(ARMBuildAttrs::ABI_optimization_goals, OptimizationGoals);
601601
OptimizationGoals = -1;
602602

@@ -884,9 +884,10 @@ static uint8_t getModifierSpecifier(ARMCP::ARMCPModifier Modifier) {
884884

885885
MCSymbol *ARMAsmPrinter::GetARMGVSymbol(const GlobalValue *GV,
886886
unsigned char TargetFlags) {
887-
if (Subtarget->isTargetMachO()) {
887+
const Triple &TT = TM.getTargetTriple();
888+
if (TT.isOSBinFormatMachO()) {
888889
bool IsIndirect =
889-
(TargetFlags & ARMII::MO_NONLAZY) && Subtarget->isGVIndirectSymbol(GV);
890+
(TargetFlags & ARMII::MO_NONLAZY) && getTM().isGVIndirectSymbol(GV);
890891

891892
if (!IsIndirect)
892893
return getSymbol(GV);
@@ -903,9 +904,8 @@ MCSymbol *ARMAsmPrinter::GetARMGVSymbol(const GlobalValue *GV,
903904
StubSym = MachineModuleInfoImpl::StubValueTy(getSymbol(GV),
904905
!GV->hasInternalLinkage());
905906
return MCSym;
906-
} else if (Subtarget->isTargetCOFF()) {
907-
assert(Subtarget->isTargetWindows() &&
908-
"Windows is the only supported COFF target");
907+
} else if (TT.isOSBinFormatCOFF()) {
908+
assert(TT.isOSWindows() && "Windows is the only supported COFF target");
909909

910910
bool IsIndirect =
911911
(TargetFlags & (ARMII::MO_DLLIMPORT | ARMII::MO_COFFSTUB));
@@ -932,7 +932,7 @@ MCSymbol *ARMAsmPrinter::GetARMGVSymbol(const GlobalValue *GV,
932932
}
933933

934934
return MCSym;
935-
} else if (Subtarget->isTargetELF()) {
935+
} else if (TT.isOSBinFormatELF()) {
936936
return getSymbolPreferLocal(*GV);
937937
}
938938
llvm_unreachable("unexpected target");
@@ -978,7 +978,8 @@ void ARMAsmPrinter::emitMachineConstantPoolValue(
978978

979979
// On Darwin, const-pool entries may get the "FOO$non_lazy_ptr" mangling, so
980980
// flag the global as MO_NONLAZY.
981-
unsigned char TF = Subtarget->isTargetMachO() ? ARMII::MO_NONLAZY : 0;
981+
unsigned char TF =
982+
TM.getTargetTriple().isOSBinFormatMachO() ? ARMII::MO_NONLAZY : 0;
982983
MCSym = GetARMGVSymbol(GV, TF);
983984
} else if (ACPV->isMachineBasicBlock()) {
984985
const MachineBasicBlock *MBB = cast<ARMConstantPoolMBB>(ACPV)->getMBB();

llvm/lib/Target/ARM/ARMSubtarget.cpp

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -318,17 +318,7 @@ bool ARMSubtarget::isRWPI() const {
318318
}
319319

320320
bool ARMSubtarget::isGVIndirectSymbol(const GlobalValue *GV) const {
321-
if (!TM.shouldAssumeDSOLocal(GV))
322-
return true;
323-
324-
// 32 bit macho has no relocation for a-b if a is undefined, even if b is in
325-
// the section that is being relocated. This means we have to use o load even
326-
// for GVs that are known to be local to the dso.
327-
if (isTargetMachO() && TM.isPositionIndependent() &&
328-
(GV->isDeclarationForLinker() || GV->hasCommonLinkage()))
329-
return true;
330-
331-
return false;
321+
return TM.isGVIndirectSymbol(GV);
332322
}
333323

334324
bool ARMSubtarget::isGVInGOT(const GlobalValue *GV) const {

llvm/lib/Target/ARM/ARMTargetMachine.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,20 @@ class ARMBaseTargetMachine : public CodeGenTargetMachineImpl {
9898
return true;
9999
}
100100

101+
bool isGVIndirectSymbol(const GlobalValue *GV) const {
102+
if (!shouldAssumeDSOLocal(GV))
103+
return true;
104+
105+
// 32 bit macho has no relocation for a-b if a is undefined, even if b is in
106+
// the section that is being relocated. This means we have to use o load
107+
// even for GVs that are known to be local to the dso.
108+
if (getTargetTriple().isOSBinFormatMachO() && isPositionIndependent() &&
109+
(GV->isDeclarationForLinker() || GV->hasCommonLinkage()))
110+
return true;
111+
112+
return false;
113+
}
114+
101115
yaml::MachineFunctionInfo *createDefaultFuncInfoYAML() const override;
102116
yaml::MachineFunctionInfo *
103117
convertFuncInfoToYAML(const MachineFunction &MF) const override;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
; RUN: llc -mtriple=arm-unknown-linux-gnueabihf < %s | FileCheck %s
2+
3+
; This test contains a llvm.global_ctors with no other definitions. Make sure we do not crash in that case.
4+
; CHECK: .section .init_array,"aw",%init_array
5+
6+
declare ccc void @ghczmbignum_GHCziNumziBackendziSelected_init__prof_init()
7+
@llvm.global_ctors = appending global [1 x {i32, void ()*, i8* }] [{i32, void ()*, i8* }{i32 65535, void ()* @ghczmbignum_GHCziNumziBackendziSelected_init__prof_init, i8* null } ]

0 commit comments

Comments
 (0)