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