Krzysztof Parzyszek | 5de5910 | 2016-04-21 18:56:45 +0000 | [diff] [blame] | 1 | //===-- HexagonTargetObjectFile.cpp ---------------------------------------===// |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file contains the declarations of the HexagonTargetAsmInfo properties. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
Eugene Zelenko | b2ca1b3 | 2017-01-04 02:02:05 +0000 | [diff] [blame] | 13 | |
Krzysztof Parzyszek | 5de5910 | 2016-04-21 18:56:45 +0000 | [diff] [blame] | 14 | #define DEBUG_TYPE "hexagon-sdata" |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 15 | |
Krzysztof Parzyszek | 5de5910 | 2016-04-21 18:56:45 +0000 | [diff] [blame] | 16 | #include "HexagonTargetObjectFile.h" |
Eugene Zelenko | b2ca1b3 | 2017-01-04 02:02:05 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/SmallString.h" |
| 18 | #include "llvm/ADT/StringRef.h" |
| 19 | #include "llvm/ADT/Twine.h" |
Zachary Turner | 264b5d9 | 2017-06-07 03:48:56 +0000 | [diff] [blame] | 20 | #include "llvm/BinaryFormat/ELF.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 21 | #include "llvm/IR/DataLayout.h" |
| 22 | #include "llvm/IR/DerivedTypes.h" |
Eugene Zelenko | b2ca1b3 | 2017-01-04 02:02:05 +0000 | [diff] [blame] | 23 | #include "llvm/IR/GlobalObject.h" |
| 24 | #include "llvm/IR/GlobalValue.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 25 | #include "llvm/IR/GlobalVariable.h" |
Eugene Zelenko | b2ca1b3 | 2017-01-04 02:02:05 +0000 | [diff] [blame] | 26 | #include "llvm/IR/Type.h" |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 27 | #include "llvm/MC/MCContext.h" |
Eugene Zelenko | b2ca1b3 | 2017-01-04 02:02:05 +0000 | [diff] [blame] | 28 | #include "llvm/MC/SectionKind.h" |
| 29 | #include "llvm/Support/Casting.h" |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 30 | #include "llvm/Support/CommandLine.h" |
Eugene Zelenko | b2ca1b3 | 2017-01-04 02:02:05 +0000 | [diff] [blame] | 31 | #include "llvm/Support/Debug.h" |
Eugene Zelenko | b2ca1b3 | 2017-01-04 02:02:05 +0000 | [diff] [blame] | 32 | #include "llvm/Support/raw_ostream.h" |
| 33 | #include "llvm/Target/TargetMachine.h" |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 34 | |
| 35 | using namespace llvm; |
| 36 | |
Krzysztof Parzyszek | 5de5910 | 2016-04-21 18:56:45 +0000 | [diff] [blame] | 37 | static cl::opt<unsigned> SmallDataThreshold("hexagon-small-data-threshold", |
| 38 | cl::init(8), cl::Hidden, |
| 39 | cl::desc("The maximum size of an object in the sdata section")); |
| 40 | |
| 41 | static cl::opt<bool> NoSmallDataSorting("mno-sort-sda", cl::init(false), |
| 42 | cl::Hidden, cl::desc("Disable small data sections sorting")); |
| 43 | |
| 44 | static cl::opt<bool> StaticsInSData("hexagon-statics-in-small-data", |
| 45 | cl::init(false), cl::Hidden, cl::ZeroOrMore, |
| 46 | cl::desc("Allow static variables in .sdata")); |
| 47 | |
| 48 | static cl::opt<bool> TraceGVPlacement("trace-gv-placement", |
| 49 | cl::Hidden, cl::init(false), |
| 50 | cl::desc("Trace global value placement")); |
| 51 | |
Sumanth Gundapaneni | 8c5d595 | 2017-06-30 20:21:48 +0000 | [diff] [blame] | 52 | static cl::opt<bool> |
| 53 | EmitJtInText("hexagon-emit-jt-text", cl::Hidden, cl::init(false), |
| 54 | cl::desc("Emit hexagon jump tables in function section")); |
| 55 | |
Sumanth Gundapaneni | d5aa0f3 | 2017-07-18 15:31:37 +0000 | [diff] [blame^] | 56 | static cl::opt<bool> |
| 57 | EmitLutInText("hexagon-emit-lut-text", cl::Hidden, cl::init(false), |
| 58 | cl::desc("Emit hexagon lookup tables in function section")); |
| 59 | |
Krzysztof Parzyszek | 5de5910 | 2016-04-21 18:56:45 +0000 | [diff] [blame] | 60 | // TraceGVPlacement controls messages for all builds. For builds with assertions |
| 61 | // (debug or release), messages are also controlled by the usual debug flags |
| 62 | // (e.g. -debug and -debug-only=globallayout) |
| 63 | #define TRACE_TO(s, X) s << X |
| 64 | #ifdef NDEBUG |
Eugene Zelenko | b2ca1b3 | 2017-01-04 02:02:05 +0000 | [diff] [blame] | 65 | #define TRACE(X) \ |
| 66 | do { \ |
| 67 | if (TraceGVPlacement) { \ |
| 68 | TRACE_TO(errs(), X); \ |
| 69 | } \ |
| 70 | } while (false) |
Krzysztof Parzyszek | 5de5910 | 2016-04-21 18:56:45 +0000 | [diff] [blame] | 71 | #else |
Eugene Zelenko | b2ca1b3 | 2017-01-04 02:02:05 +0000 | [diff] [blame] | 72 | #define TRACE(X) \ |
| 73 | do { \ |
| 74 | if (TraceGVPlacement) { \ |
| 75 | TRACE_TO(errs(), X); \ |
| 76 | } else { \ |
| 77 | DEBUG(TRACE_TO(dbgs(), X)); \ |
| 78 | } \ |
| 79 | } while (false) |
Krzysztof Parzyszek | 5de5910 | 2016-04-21 18:56:45 +0000 | [diff] [blame] | 80 | #endif |
| 81 | |
| 82 | // Returns true if the section name is such that the symbol will be put |
| 83 | // in a small data section. |
| 84 | // For instance, global variables with section attributes such as ".sdata" |
| 85 | // ".sdata.*", ".sbss", and ".sbss.*" will go into small data. |
| 86 | static bool isSmallDataSection(StringRef Sec) { |
| 87 | // sectionName is either ".sdata" or ".sbss". Looking for an exact match |
| 88 | // obviates the need for checks for section names such as ".sdatafoo". |
| 89 | if (Sec.equals(".sdata") || Sec.equals(".sbss") || Sec.equals(".scommon")) |
| 90 | return true; |
| 91 | // If either ".sdata." or ".sbss." is a substring of the section name |
| 92 | // then put the symbol in small data. |
| 93 | return Sec.find(".sdata.") != StringRef::npos || |
| 94 | Sec.find(".sbss.") != StringRef::npos || |
| 95 | Sec.find(".scommon.") != StringRef::npos; |
| 96 | } |
| 97 | |
Krzysztof Parzyszek | 5de5910 | 2016-04-21 18:56:45 +0000 | [diff] [blame] | 98 | static const char *getSectionSuffixForSize(unsigned Size) { |
| 99 | switch (Size) { |
| 100 | default: |
| 101 | return ""; |
| 102 | case 1: |
| 103 | return ".1"; |
| 104 | case 2: |
| 105 | return ".2"; |
| 106 | case 4: |
| 107 | return ".4"; |
| 108 | case 8: |
| 109 | return ".8"; |
| 110 | } |
| 111 | } |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 112 | |
| 113 | void HexagonTargetObjectFile::Initialize(MCContext &Ctx, |
Krzysztof Parzyszek | 5de5910 | 2016-04-21 18:56:45 +0000 | [diff] [blame] | 114 | const TargetMachine &TM) { |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 115 | TargetLoweringObjectFileELF::Initialize(Ctx, TM); |
Sid Manning | 326f8af | 2014-11-03 14:56:05 +0000 | [diff] [blame] | 116 | InitializeELF(TM.Options.UseInitArray); |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 117 | |
Krzysztof Parzyszek | 5de5910 | 2016-04-21 18:56:45 +0000 | [diff] [blame] | 118 | SmallDataSection = |
| 119 | getContext().getELFSection(".sdata", ELF::SHT_PROGBITS, |
| 120 | ELF::SHF_WRITE | ELF::SHF_ALLOC | |
| 121 | ELF::SHF_HEX_GPREL); |
| 122 | SmallBSSSection = |
| 123 | getContext().getELFSection(".sbss", ELF::SHT_NOBITS, |
| 124 | ELF::SHF_WRITE | ELF::SHF_ALLOC | |
| 125 | ELF::SHF_HEX_GPREL); |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 126 | } |
| 127 | |
Krzysztof Parzyszek | 5de5910 | 2016-04-21 18:56:45 +0000 | [diff] [blame] | 128 | MCSection *HexagonTargetObjectFile::SelectSectionForGlobal( |
Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 129 | const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const { |
| 130 | TRACE("[SelectSectionForGlobal] GO(" << GO->getName() << ") "); |
| 131 | TRACE("input section(" << GO->getSection() << ") "); |
Krzysztof Parzyszek | 5de5910 | 2016-04-21 18:56:45 +0000 | [diff] [blame] | 132 | |
Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 133 | TRACE((GO->hasPrivateLinkage() ? "private_linkage " : "") |
| 134 | << (GO->hasLocalLinkage() ? "local_linkage " : "") |
| 135 | << (GO->hasInternalLinkage() ? "internal " : "") |
| 136 | << (GO->hasExternalLinkage() ? "external " : "") |
| 137 | << (GO->hasCommonLinkage() ? "common_linkage " : "") |
| 138 | << (GO->hasCommonLinkage() ? "common " : "" ) |
Krzysztof Parzyszek | 5de5910 | 2016-04-21 18:56:45 +0000 | [diff] [blame] | 139 | << (Kind.isCommon() ? "kind_common " : "" ) |
| 140 | << (Kind.isBSS() ? "kind_bss " : "" ) |
| 141 | << (Kind.isBSSLocal() ? "kind_bss_local " : "" )); |
| 142 | |
Sumanth Gundapaneni | d5aa0f3 | 2017-07-18 15:31:37 +0000 | [diff] [blame^] | 143 | // If the lookup table is used by more than one function, do not place |
| 144 | // it in text section. |
| 145 | if (EmitLutInText && GO->getName().startswith("switch.table")) { |
| 146 | if (const Function *Fn = getLutUsedFunction(GO)) |
| 147 | return selectSectionForLookupTable(GO, TM, Fn); |
| 148 | } |
| 149 | |
Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 150 | if (isGlobalInSmallSection(GO, TM)) |
| 151 | return selectSmallSectionForGlobal(GO, Kind, TM); |
Krzysztof Parzyszek | 5de5910 | 2016-04-21 18:56:45 +0000 | [diff] [blame] | 152 | |
| 153 | if (Kind.isCommon()) { |
| 154 | // This is purely for LTO+Linker Script because commons don't really have a |
| 155 | // section. However, the BitcodeSectionWriter pass will query for the |
| 156 | // sections of commons (and the linker expects us to know their section) so |
| 157 | // we'll return one here. |
| 158 | return BSSSection; |
| 159 | } |
| 160 | |
| 161 | TRACE("default_ELF_section\n"); |
| 162 | // Otherwise, we work the same as ELF. |
Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 163 | return TargetLoweringObjectFileELF::SelectSectionForGlobal(GO, Kind, TM); |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 164 | } |
Jyotsna Verma | 5eb5980 | 2013-05-07 19:53:00 +0000 | [diff] [blame] | 165 | |
Krzysztof Parzyszek | 5de5910 | 2016-04-21 18:56:45 +0000 | [diff] [blame] | 166 | MCSection *HexagonTargetObjectFile::getExplicitSectionGlobal( |
Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 167 | const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const { |
| 168 | TRACE("[getExplicitSectionGlobal] GO(" << GO->getName() << ") from(" |
| 169 | << GO->getSection() << ") "); |
| 170 | TRACE((GO->hasPrivateLinkage() ? "private_linkage " : "") |
| 171 | << (GO->hasLocalLinkage() ? "local_linkage " : "") |
| 172 | << (GO->hasInternalLinkage() ? "internal " : "") |
| 173 | << (GO->hasExternalLinkage() ? "external " : "") |
| 174 | << (GO->hasCommonLinkage() ? "common_linkage " : "") |
| 175 | << (GO->hasCommonLinkage() ? "common " : "" ) |
Krzysztof Parzyszek | 5de5910 | 2016-04-21 18:56:45 +0000 | [diff] [blame] | 176 | << (Kind.isCommon() ? "kind_common " : "" ) |
| 177 | << (Kind.isBSS() ? "kind_bss " : "" ) |
| 178 | << (Kind.isBSSLocal() ? "kind_bss_local " : "" )); |
| 179 | |
Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 180 | if (GO->hasSection()) { |
| 181 | StringRef Section = GO->getSection(); |
Krzysztof Parzyszek | 5de5910 | 2016-04-21 18:56:45 +0000 | [diff] [blame] | 182 | if (Section.find(".access.text.group") != StringRef::npos) |
Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 183 | return getContext().getELFSection(GO->getSection(), ELF::SHT_PROGBITS, |
Krzysztof Parzyszek | 5de5910 | 2016-04-21 18:56:45 +0000 | [diff] [blame] | 184 | ELF::SHF_ALLOC | ELF::SHF_EXECINSTR); |
| 185 | if (Section.find(".access.data.group") != StringRef::npos) |
Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 186 | return getContext().getELFSection(GO->getSection(), ELF::SHT_PROGBITS, |
Krzysztof Parzyszek | 5de5910 | 2016-04-21 18:56:45 +0000 | [diff] [blame] | 187 | ELF::SHF_WRITE | ELF::SHF_ALLOC); |
| 188 | } |
| 189 | |
Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 190 | if (isGlobalInSmallSection(GO, TM)) |
| 191 | return selectSmallSectionForGlobal(GO, Kind, TM); |
Krzysztof Parzyszek | 5de5910 | 2016-04-21 18:56:45 +0000 | [diff] [blame] | 192 | |
| 193 | // Otherwise, we work the same as ELF. |
| 194 | TRACE("default_ELF_section\n"); |
Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 195 | return TargetLoweringObjectFileELF::getExplicitSectionGlobal(GO, Kind, TM); |
Krzysztof Parzyszek | 5de5910 | 2016-04-21 18:56:45 +0000 | [diff] [blame] | 196 | } |
| 197 | |
Krzysztof Parzyszek | 5de5910 | 2016-04-21 18:56:45 +0000 | [diff] [blame] | 198 | /// Return true if this global value should be placed into small data/bss |
| 199 | /// section. |
Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 200 | bool HexagonTargetObjectFile::isGlobalInSmallSection(const GlobalObject *GO, |
Krzysztof Parzyszek | 5de5910 | 2016-04-21 18:56:45 +0000 | [diff] [blame] | 201 | const TargetMachine &TM) const { |
| 202 | // Only global variables, not functions. |
| 203 | DEBUG(dbgs() << "Checking if value is in small-data, -G" |
Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 204 | << SmallDataThreshold << ": \"" << GO->getName() << "\": "); |
| 205 | const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GO); |
Krzysztof Parzyszek | 5de5910 | 2016-04-21 18:56:45 +0000 | [diff] [blame] | 206 | if (!GVar) { |
| 207 | DEBUG(dbgs() << "no, not a global variable\n"); |
| 208 | return false; |
| 209 | } |
| 210 | |
| 211 | // Globals with external linkage that have an original section set must be |
| 212 | // emitted to that section, regardless of whether we would put them into |
| 213 | // small data or not. This is how we can support mixing -G0/-G8 in LTO. |
| 214 | if (GVar->hasSection()) { |
| 215 | bool IsSmall = isSmallDataSection(GVar->getSection()); |
| 216 | DEBUG(dbgs() << (IsSmall ? "yes" : "no") << ", has section: " |
| 217 | << GVar->getSection() << '\n'); |
| 218 | return IsSmall; |
| 219 | } |
| 220 | |
| 221 | if (GVar->isConstant()) { |
| 222 | DEBUG(dbgs() << "no, is a constant\n"); |
| 223 | return false; |
| 224 | } |
| 225 | |
| 226 | bool IsLocal = GVar->hasLocalLinkage(); |
| 227 | if (!StaticsInSData && IsLocal) { |
| 228 | DEBUG(dbgs() << "no, is static\n"); |
| 229 | return false; |
| 230 | } |
| 231 | |
| 232 | Type *GType = GVar->getType(); |
| 233 | if (PointerType *PT = dyn_cast<PointerType>(GType)) |
| 234 | GType = PT->getElementType(); |
| 235 | |
| 236 | if (isa<ArrayType>(GType)) { |
| 237 | DEBUG(dbgs() << "no, is an array\n"); |
| 238 | return false; |
| 239 | } |
| 240 | |
| 241 | // If the type is a struct with no body provided, treat is conservatively. |
| 242 | // There cannot be actual definitions of object of such a type in this CU |
| 243 | // (only references), so assuming that they are not in sdata is safe. If |
| 244 | // these objects end up in the sdata, the references will still be valid. |
| 245 | if (StructType *ST = dyn_cast<StructType>(GType)) { |
| 246 | if (ST->isOpaque()) { |
| 247 | DEBUG(dbgs() << "no, has opaque type\n"); |
| 248 | return false; |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | unsigned Size = GVar->getParent()->getDataLayout().getTypeAllocSize(GType); |
| 253 | if (Size == 0) { |
| 254 | DEBUG(dbgs() << "no, has size 0\n"); |
| 255 | return false; |
| 256 | } |
| 257 | if (Size > SmallDataThreshold) { |
| 258 | DEBUG(dbgs() << "no, size exceeds sdata threshold: " << Size << '\n'); |
| 259 | return false; |
| 260 | } |
| 261 | |
| 262 | DEBUG(dbgs() << "yes\n"); |
| 263 | return true; |
| 264 | } |
| 265 | |
Krzysztof Parzyszek | 5de5910 | 2016-04-21 18:56:45 +0000 | [diff] [blame] | 266 | bool HexagonTargetObjectFile::isSmallDataEnabled() const { |
Jyotsna Verma | 5eb5980 | 2013-05-07 19:53:00 +0000 | [diff] [blame] | 267 | return SmallDataThreshold > 0; |
| 268 | } |
| 269 | |
Krzysztof Parzyszek | 5de5910 | 2016-04-21 18:56:45 +0000 | [diff] [blame] | 270 | unsigned HexagonTargetObjectFile::getSmallDataSize() const { |
| 271 | return SmallDataThreshold; |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 272 | } |
| 273 | |
Sumanth Gundapaneni | 8c5d595 | 2017-06-30 20:21:48 +0000 | [diff] [blame] | 274 | bool HexagonTargetObjectFile::shouldPutJumpTableInFunctionSection( |
| 275 | bool UsesLabelDifference, const Function &F) const { |
| 276 | return EmitJtInText; |
| 277 | } |
| 278 | |
Krzysztof Parzyszek | 5de5910 | 2016-04-21 18:56:45 +0000 | [diff] [blame] | 279 | /// Descends any type down to "elementary" components, |
| 280 | /// discovering the smallest addressable one. |
| 281 | /// If zero is returned, declaration will not be modified. |
| 282 | unsigned HexagonTargetObjectFile::getSmallestAddressableSize(const Type *Ty, |
| 283 | const GlobalValue *GV, const TargetMachine &TM) const { |
| 284 | // Assign the smallest element access size to the highest |
| 285 | // value which assembler can handle. |
| 286 | unsigned SmallestElement = 8; |
| 287 | |
| 288 | if (!Ty) |
| 289 | return 0; |
| 290 | switch (Ty->getTypeID()) { |
| 291 | case Type::StructTyID: { |
| 292 | const StructType *STy = cast<const StructType>(Ty); |
| 293 | for (auto &E : STy->elements()) { |
| 294 | unsigned AtomicSize = getSmallestAddressableSize(E, GV, TM); |
| 295 | if (AtomicSize < SmallestElement) |
| 296 | SmallestElement = AtomicSize; |
| 297 | } |
| 298 | return (STy->getNumElements() == 0) ? 0 : SmallestElement; |
| 299 | } |
| 300 | case Type::ArrayTyID: { |
| 301 | const ArrayType *ATy = cast<const ArrayType>(Ty); |
| 302 | return getSmallestAddressableSize(ATy->getElementType(), GV, TM); |
| 303 | } |
| 304 | case Type::VectorTyID: { |
| 305 | const VectorType *PTy = cast<const VectorType>(Ty); |
| 306 | return getSmallestAddressableSize(PTy->getElementType(), GV, TM); |
| 307 | } |
| 308 | case Type::PointerTyID: |
| 309 | case Type::HalfTyID: |
| 310 | case Type::FloatTyID: |
| 311 | case Type::DoubleTyID: |
| 312 | case Type::IntegerTyID: { |
| 313 | const DataLayout &DL = GV->getParent()->getDataLayout(); |
| 314 | // It is unfortunate that DL's function take non-const Type*. |
| 315 | return DL.getTypeAllocSize(const_cast<Type*>(Ty)); |
| 316 | } |
| 317 | case Type::FunctionTyID: |
| 318 | case Type::VoidTyID: |
| 319 | case Type::X86_FP80TyID: |
| 320 | case Type::FP128TyID: |
| 321 | case Type::PPC_FP128TyID: |
| 322 | case Type::LabelTyID: |
| 323 | case Type::MetadataTyID: |
| 324 | case Type::X86_MMXTyID: |
| 325 | case Type::TokenTyID: |
| 326 | return 0; |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 327 | } |
| 328 | |
Krzysztof Parzyszek | 5de5910 | 2016-04-21 18:56:45 +0000 | [diff] [blame] | 329 | return 0; |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 330 | } |
| 331 | |
Krzysztof Parzyszek | 5de5910 | 2016-04-21 18:56:45 +0000 | [diff] [blame] | 332 | MCSection *HexagonTargetObjectFile::selectSmallSectionForGlobal( |
Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 333 | const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const { |
| 334 | const Type *GTy = GO->getType()->getElementType(); |
| 335 | unsigned Size = getSmallestAddressableSize(GTy, GO, TM); |
Krzysztof Parzyszek | 5de5910 | 2016-04-21 18:56:45 +0000 | [diff] [blame] | 336 | |
| 337 | // If we have -ffunction-section or -fdata-section then we should emit the |
| 338 | // global value to a unique section specifically for it... even for sdata. |
| 339 | bool EmitUniquedSection = TM.getDataSections(); |
| 340 | |
| 341 | TRACE("Small data. Size(" << Size << ")"); |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 342 | // Handle Small Section classification here. |
Krzysztof Parzyszek | 5de5910 | 2016-04-21 18:56:45 +0000 | [diff] [blame] | 343 | if (Kind.isBSS() || Kind.isBSSLocal()) { |
| 344 | // If -mno-sort-sda is not set, find out smallest accessible entity in |
| 345 | // declaration and add it to the section name string. |
| 346 | // Note. It does not track the actual usage of the value, only its de- |
| 347 | // claration. Also, compiler adds explicit pad fields to some struct |
| 348 | // declarations - they are currently counted towards smallest addres- |
| 349 | // sable entity. |
| 350 | if (NoSmallDataSorting) { |
| 351 | TRACE(" default sbss\n"); |
| 352 | return SmallBSSSection; |
| 353 | } |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 354 | |
Krzysztof Parzyszek | 5de5910 | 2016-04-21 18:56:45 +0000 | [diff] [blame] | 355 | StringRef Prefix(".sbss"); |
| 356 | SmallString<128> Name(Prefix); |
| 357 | Name.append(getSectionSuffixForSize(Size)); |
| 358 | |
| 359 | if (EmitUniquedSection) { |
| 360 | Name.append("."); |
Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 361 | Name.append(GO->getName()); |
Krzysztof Parzyszek | 5de5910 | 2016-04-21 18:56:45 +0000 | [diff] [blame] | 362 | } |
| 363 | TRACE(" unique sbss(" << Name << ")\n"); |
| 364 | return getContext().getELFSection(Name.str(), ELF::SHT_NOBITS, |
| 365 | ELF::SHF_WRITE | ELF::SHF_ALLOC | ELF::SHF_HEX_GPREL); |
| 366 | } |
| 367 | |
| 368 | if (Kind.isCommon()) { |
| 369 | // This is purely for LTO+Linker Script because commons don't really have a |
| 370 | // section. However, the BitcodeSectionWriter pass will query for the |
| 371 | // sections of commons (and the linker expects us to know their section) so |
| 372 | // we'll return one here. |
| 373 | if (NoSmallDataSorting) |
| 374 | return BSSSection; |
| 375 | |
| 376 | Twine Name = Twine(".scommon") + getSectionSuffixForSize(Size); |
| 377 | TRACE(" small COMMON (" << Name << ")\n"); |
| 378 | |
| 379 | return getContext().getELFSection(Name.str(), ELF::SHT_NOBITS, |
| 380 | ELF::SHF_WRITE | ELF::SHF_ALLOC | |
| 381 | ELF::SHF_HEX_GPREL); |
| 382 | } |
| 383 | |
| 384 | // We could have changed sdata object to a constant... in this |
| 385 | // case the Kind could be wrong for it. |
| 386 | if (Kind.isMergeableConst()) { |
| 387 | TRACE(" const_object_as_data "); |
Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 388 | const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GO); |
Krzysztof Parzyszek | 5de5910 | 2016-04-21 18:56:45 +0000 | [diff] [blame] | 389 | if (GVar->hasSection() && isSmallDataSection(GVar->getSection())) |
| 390 | Kind = SectionKind::getData(); |
| 391 | } |
| 392 | |
| 393 | if (Kind.isData()) { |
| 394 | if (NoSmallDataSorting) { |
| 395 | TRACE(" default sdata\n"); |
| 396 | return SmallDataSection; |
| 397 | } |
| 398 | |
| 399 | StringRef Prefix(".sdata"); |
| 400 | SmallString<128> Name(Prefix); |
| 401 | Name.append(getSectionSuffixForSize(Size)); |
| 402 | |
| 403 | if (EmitUniquedSection) { |
| 404 | Name.append("."); |
Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 405 | Name.append(GO->getName()); |
Krzysztof Parzyszek | 5de5910 | 2016-04-21 18:56:45 +0000 | [diff] [blame] | 406 | } |
| 407 | TRACE(" unique sdata(" << Name << ")\n"); |
| 408 | return getContext().getELFSection(Name.str(), ELF::SHT_PROGBITS, |
| 409 | ELF::SHF_WRITE | ELF::SHF_ALLOC | ELF::SHF_HEX_GPREL); |
| 410 | } |
| 411 | |
| 412 | TRACE("default ELF section\n"); |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 413 | // Otherwise, we work the same as ELF. |
Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 414 | return TargetLoweringObjectFileELF::SelectSectionForGlobal(GO, Kind, TM); |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 415 | } |
Sumanth Gundapaneni | d5aa0f3 | 2017-07-18 15:31:37 +0000 | [diff] [blame^] | 416 | |
| 417 | // Return the function that uses the lookup table. If there are more |
| 418 | // than one live function that uses this look table, bail out and place |
| 419 | // the lookup table in default section. |
| 420 | const Function * |
| 421 | HexagonTargetObjectFile::getLutUsedFunction(const GlobalObject *GO) const { |
| 422 | const Function *ReturnFn = nullptr; |
| 423 | for (auto U : GO->users()) { |
| 424 | // validate each instance of user to be a live function. |
| 425 | auto *I = dyn_cast<Instruction>(U); |
| 426 | if (!I) |
| 427 | continue; |
| 428 | auto *Bb = I->getParent(); |
| 429 | if (!Bb) |
| 430 | continue; |
| 431 | auto *UserFn = Bb->getParent(); |
| 432 | if (!ReturnFn) |
| 433 | ReturnFn = UserFn; |
| 434 | else if (ReturnFn != UserFn) |
| 435 | return nullptr; |
| 436 | } |
| 437 | return ReturnFn; |
| 438 | } |
| 439 | |
| 440 | MCSection *HexagonTargetObjectFile::selectSectionForLookupTable( |
| 441 | const GlobalObject *GO, const TargetMachine &TM, const Function *Fn) const { |
| 442 | |
| 443 | SectionKind Kind = SectionKind::getText(); |
| 444 | // If the function has explicit section, place the lookup table in this |
| 445 | // explicit section. |
| 446 | if (Fn->hasSection()) |
| 447 | return getExplicitSectionGlobal(Fn, Kind, TM); |
| 448 | |
| 449 | const auto *FuncObj = dyn_cast<GlobalObject>(Fn); |
| 450 | return SelectSectionForGlobal(FuncObj, Kind, TM); |
| 451 | } |