Jacques Pienaar | fcef3e4 | 2016-03-28 13:09:54 +0000 | [diff] [blame] | 1 | // |
| 2 | // The LLVM Compiler Infrastructure |
| 3 | // |
| 4 | // This file is distributed under the University of Illinois Open Source |
| 5 | // License. See LICENSE.TXT for details. |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "LanaiTargetObjectFile.h" |
| 10 | |
| 11 | #include "LanaiSubtarget.h" |
| 12 | #include "LanaiTargetMachine.h" |
| 13 | #include "llvm/IR/DataLayout.h" |
| 14 | #include "llvm/IR/DerivedTypes.h" |
| 15 | #include "llvm/IR/GlobalVariable.h" |
| 16 | #include "llvm/MC/MCContext.h" |
| 17 | #include "llvm/MC/MCSectionELF.h" |
| 18 | #include "llvm/Support/CommandLine.h" |
| 19 | #include "llvm/Support/ELF.h" |
| 20 | #include "llvm/Target/TargetMachine.h" |
| 21 | |
| 22 | using namespace llvm; |
| 23 | |
| 24 | static cl::opt<unsigned> SSThreshold( |
| 25 | "lanai-ssection-threshold", cl::Hidden, |
| 26 | cl::desc("Small data and bss section threshold size (default=0)"), |
| 27 | cl::init(0)); |
| 28 | |
| 29 | void LanaiTargetObjectFile::Initialize(MCContext &Ctx, |
| 30 | const TargetMachine &TM) { |
| 31 | TargetLoweringObjectFileELF::Initialize(Ctx, TM); |
| 32 | InitializeELF(TM.Options.UseInitArray); |
| 33 | |
| 34 | SmallDataSection = getContext().getELFSection( |
| 35 | ".sdata", ELF::SHT_PROGBITS, ELF::SHF_WRITE | ELF::SHF_ALLOC); |
| 36 | SmallBSSSection = getContext().getELFSection(".sbss", ELF::SHT_NOBITS, |
| 37 | ELF::SHF_WRITE | ELF::SHF_ALLOC); |
| 38 | } |
| 39 | |
| 40 | // A address must be loaded from a small section if its size is less than the |
| 41 | // small section size threshold. Data in this section must be addressed using |
| 42 | // gp_rel operator. |
| 43 | static bool isInSmallSection(uint64_t Size) { |
| 44 | // gcc has traditionally not treated zero-sized objects as small data, so this |
| 45 | // is effectively part of the ABI. |
| 46 | return Size > 0 && Size <= SSThreshold; |
| 47 | } |
| 48 | |
| 49 | // Return true if this global address should be placed into small data/bss |
| 50 | // section. |
| 51 | bool LanaiTargetObjectFile::isGlobalInSmallSection( |
Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 52 | const GlobalObject *GO, const TargetMachine &TM) const { |
Jacques Pienaar | ccffe38 | 2016-12-15 16:56:16 +0000 | [diff] [blame^] | 53 | if (GO == nullptr) |
| 54 | return false; |
| 55 | |
Jacques Pienaar | fcef3e4 | 2016-03-28 13:09:54 +0000 | [diff] [blame] | 56 | // We first check the case where global is a declaration, because finding |
| 57 | // section kind using getKindForGlobal() is only allowed for global |
| 58 | // definitions. |
Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 59 | if (GO->isDeclaration() || GO->hasAvailableExternallyLinkage()) |
| 60 | return isGlobalInSmallSectionImpl(GO, TM); |
Jacques Pienaar | fcef3e4 | 2016-03-28 13:09:54 +0000 | [diff] [blame] | 61 | |
Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 62 | return isGlobalInSmallSection(GO, TM, getKindForGlobal(GO, TM)); |
Jacques Pienaar | fcef3e4 | 2016-03-28 13:09:54 +0000 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | // Return true if this global address should be placed into small data/bss |
| 66 | // section. |
Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 67 | bool LanaiTargetObjectFile::isGlobalInSmallSection(const GlobalObject *GO, |
Jacques Pienaar | fcef3e4 | 2016-03-28 13:09:54 +0000 | [diff] [blame] | 68 | const TargetMachine &TM, |
| 69 | SectionKind Kind) const { |
Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 70 | return (isGlobalInSmallSectionImpl(GO, TM) && |
Jacques Pienaar | fcef3e4 | 2016-03-28 13:09:54 +0000 | [diff] [blame] | 71 | (Kind.isData() || Kind.isBSS() || Kind.isCommon())); |
| 72 | } |
| 73 | |
| 74 | // Return true if this global address should be placed into small data/bss |
| 75 | // section. This method does all the work, except for checking the section |
| 76 | // kind. |
| 77 | bool LanaiTargetObjectFile::isGlobalInSmallSectionImpl( |
Jacques Pienaar | ccffe38 | 2016-12-15 16:56:16 +0000 | [diff] [blame^] | 78 | const GlobalObject *GO, const TargetMachine &TM) const { |
Jacques Pienaar | fcef3e4 | 2016-03-28 13:09:54 +0000 | [diff] [blame] | 79 | // Only global variables, not functions. |
Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 80 | const auto *GVA = dyn_cast<GlobalVariable>(GO); |
Jacques Pienaar | fcef3e4 | 2016-03-28 13:09:54 +0000 | [diff] [blame] | 81 | if (!GVA) |
| 82 | return false; |
| 83 | |
Jacques Pienaar | ccffe38 | 2016-12-15 16:56:16 +0000 | [diff] [blame^] | 84 | // Global values placed in sections starting with .ldata do not fit in |
| 85 | // 21-bits, so always use large memory access for them. FIXME: This is a |
| 86 | // workaround for a tool limitation. |
| 87 | if (GVA->getSection().startswith(".ldata")) |
| 88 | return false; |
| 89 | |
| 90 | if (TM.getCodeModel() == CodeModel::Small) |
| 91 | return true; |
| 92 | |
Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 93 | if (GVA->hasLocalLinkage()) |
Jacques Pienaar | fcef3e4 | 2016-03-28 13:09:54 +0000 | [diff] [blame] | 94 | return false; |
| 95 | |
Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 96 | if (((GVA->hasExternalLinkage() && GVA->isDeclaration()) || |
| 97 | GVA->hasCommonLinkage())) |
Jacques Pienaar | fcef3e4 | 2016-03-28 13:09:54 +0000 | [diff] [blame] | 98 | return false; |
| 99 | |
Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 100 | Type *Ty = GVA->getValueType(); |
Jacques Pienaar | fcef3e4 | 2016-03-28 13:09:54 +0000 | [diff] [blame] | 101 | return isInSmallSection( |
Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 102 | GVA->getParent()->getDataLayout().getTypeAllocSize(Ty)); |
Jacques Pienaar | fcef3e4 | 2016-03-28 13:09:54 +0000 | [diff] [blame] | 103 | } |
| 104 | |
Eric Christopher | 4367c7f | 2016-09-16 07:33:15 +0000 | [diff] [blame] | 105 | MCSection *LanaiTargetObjectFile::SelectSectionForGlobal( |
Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 106 | const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const { |
Jacques Pienaar | fcef3e4 | 2016-03-28 13:09:54 +0000 | [diff] [blame] | 107 | // Handle Small Section classification here. |
Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 108 | if (Kind.isBSS() && isGlobalInSmallSection(GO, TM, Kind)) |
Jacques Pienaar | fcef3e4 | 2016-03-28 13:09:54 +0000 | [diff] [blame] | 109 | return SmallBSSSection; |
Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 110 | if (Kind.isData() && isGlobalInSmallSection(GO, TM, Kind)) |
Jacques Pienaar | fcef3e4 | 2016-03-28 13:09:54 +0000 | [diff] [blame] | 111 | return SmallDataSection; |
| 112 | |
| 113 | // Otherwise, we work the same as ELF. |
Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 114 | return TargetLoweringObjectFileELF::SelectSectionForGlobal(GO, Kind, TM); |
Jacques Pienaar | fcef3e4 | 2016-03-28 13:09:54 +0000 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | /// Return true if this constant should be placed into small data section. |
Jacques Pienaar | 5ffdef5 | 2016-05-20 21:41:53 +0000 | [diff] [blame] | 118 | bool LanaiTargetObjectFile::isConstantInSmallSection(const DataLayout &DL, |
| 119 | const Constant *CN) const { |
Jacques Pienaar | fcef3e4 | 2016-03-28 13:09:54 +0000 | [diff] [blame] | 120 | return isInSmallSection(DL.getTypeAllocSize(CN->getType())); |
| 121 | } |
| 122 | |
| 123 | MCSection *LanaiTargetObjectFile::getSectionForConstant(const DataLayout &DL, |
| 124 | SectionKind Kind, |
| 125 | const Constant *C, |
| 126 | unsigned &Align) const { |
| 127 | if (isConstantInSmallSection(DL, C)) |
| 128 | return SmallDataSection; |
| 129 | |
| 130 | // Otherwise, we work the same as ELF. |
| 131 | return TargetLoweringObjectFileELF::getSectionForConstant(DL, Kind, C, Align); |
| 132 | } |