Skip to content

Commit d6fc7ac

Browse files
committed
[ARM] Use TargetMachine over Subtarget in ARMAsmPrinter
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
1 parent f3b407f commit d6fc7ac

File tree

4 files changed

+28
-13
lines changed

4 files changed

+28
-13
lines changed

llvm/lib/Target/ARM/ARMAsmPrinter.cpp

Lines changed: 12 additions & 11 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()) {
888-
bool IsIndirect =
889-
(TargetFlags & ARMII::MO_NONLAZY) && Subtarget->isGVIndirectSymbol(GV);
887+
const Triple &TT = TM.getTargetTriple();
888+
if (TT.isOSBinFormatMachO()) {
889+
bool IsIndirect = (TargetFlags & ARMII::MO_NONLAZY) &&
890+
ARMSubtarget::isGVIndirectSymbol(TM, 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: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -317,20 +317,25 @@ bool ARMSubtarget::isRWPI() const {
317317
TM.getRelocationModel() == Reloc::ROPI_RWPI;
318318
}
319319

320-
bool ARMSubtarget::isGVIndirectSymbol(const GlobalValue *GV) const {
320+
bool ARMSubtarget::isGVIndirectSymbol(const TargetMachine &TM,
321+
const GlobalValue *GV) {
321322
if (!TM.shouldAssumeDSOLocal(GV))
322323
return true;
323324

324325
// 32 bit macho has no relocation for a-b if a is undefined, even if b is in
325326
// the section that is being relocated. This means we have to use o load even
326327
// for GVs that are known to be local to the dso.
327-
if (isTargetMachO() && TM.isPositionIndependent() &&
328+
if (TM.getTargetTriple().isOSBinFormatMachO() && TM.isPositionIndependent() &&
328329
(GV->isDeclarationForLinker() || GV->hasCommonLinkage()))
329330
return true;
330331

331332
return false;
332333
}
333334

335+
bool ARMSubtarget::isGVIndirectSymbol(const GlobalValue *GV) const {
336+
return isGVIndirectSymbol(TM, GV);
337+
}
338+
334339
bool ARMSubtarget::isGVInGOT(const GlobalValue *GV) const {
335340
return isTargetELF() && TM.isPositionIndependent() && !GV->isDSOLocal();
336341
}

llvm/lib/Target/ARM/ARMSubtarget.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,8 @@ class ARMSubtarget : public ARMGenSubtargetInfo {
464464
}
465465

466466
/// True if the GV will be accessed via an indirect symbol.
467+
static bool isGVIndirectSymbol(const TargetMachine &TM,
468+
const GlobalValue *GV);
467469
bool isGVIndirectSymbol(const GlobalValue *GV) const;
468470

469471
/// Returns the constant pool modifier needed to access the GV.
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)