blob: c9c4f95dbaaa5818d8da25296659ce0cca7ceac1 [file] [log] [blame]
Krzysztof Parzyszek5de59102016-04-21 18:56:45 +00001//===-- HexagonTargetObjectFile.cpp ---------------------------------------===//
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002//
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 Zelenkob2ca1b32017-01-04 02:02:05 +000013
Krzysztof Parzyszek5de59102016-04-21 18:56:45 +000014#define DEBUG_TYPE "hexagon-sdata"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000015
Krzysztof Parzyszek5de59102016-04-21 18:56:45 +000016#include "HexagonTargetObjectFile.h"
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000017#include "llvm/ADT/SmallString.h"
18#include "llvm/ADT/StringRef.h"
19#include "llvm/ADT/Twine.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000020#include "llvm/IR/DataLayout.h"
21#include "llvm/IR/DerivedTypes.h"
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000022#include "llvm/IR/GlobalObject.h"
23#include "llvm/IR/GlobalValue.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000024#include "llvm/IR/GlobalVariable.h"
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000025#include "llvm/IR/Type.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000026#include "llvm/MC/MCContext.h"
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000027#include "llvm/MC/SectionKind.h"
28#include "llvm/Support/Casting.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000029#include "llvm/Support/CommandLine.h"
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000030#include "llvm/Support/Debug.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000031#include "llvm/Support/ELF.h"
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000032#include "llvm/Support/raw_ostream.h"
33#include "llvm/Target/TargetMachine.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000034
35using namespace llvm;
36
Krzysztof Parzyszek5de59102016-04-21 18:56:45 +000037static 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
41static cl::opt<bool> NoSmallDataSorting("mno-sort-sda", cl::init(false),
42 cl::Hidden, cl::desc("Disable small data sections sorting"));
43
44static 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
48static cl::opt<bool> TraceGVPlacement("trace-gv-placement",
49 cl::Hidden, cl::init(false),
50 cl::desc("Trace global value placement"));
51
52// TraceGVPlacement controls messages for all builds. For builds with assertions
53// (debug or release), messages are also controlled by the usual debug flags
54// (e.g. -debug and -debug-only=globallayout)
55#define TRACE_TO(s, X) s << X
56#ifdef NDEBUG
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000057#define TRACE(X) \
58 do { \
59 if (TraceGVPlacement) { \
60 TRACE_TO(errs(), X); \
61 } \
62 } while (false)
Krzysztof Parzyszek5de59102016-04-21 18:56:45 +000063#else
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000064#define TRACE(X) \
65 do { \
66 if (TraceGVPlacement) { \
67 TRACE_TO(errs(), X); \
68 } else { \
69 DEBUG(TRACE_TO(dbgs(), X)); \
70 } \
71 } while (false)
Krzysztof Parzyszek5de59102016-04-21 18:56:45 +000072#endif
73
74// Returns true if the section name is such that the symbol will be put
75// in a small data section.
76// For instance, global variables with section attributes such as ".sdata"
77// ".sdata.*", ".sbss", and ".sbss.*" will go into small data.
78static bool isSmallDataSection(StringRef Sec) {
79 // sectionName is either ".sdata" or ".sbss". Looking for an exact match
80 // obviates the need for checks for section names such as ".sdatafoo".
81 if (Sec.equals(".sdata") || Sec.equals(".sbss") || Sec.equals(".scommon"))
82 return true;
83 // If either ".sdata." or ".sbss." is a substring of the section name
84 // then put the symbol in small data.
85 return Sec.find(".sdata.") != StringRef::npos ||
86 Sec.find(".sbss.") != StringRef::npos ||
87 Sec.find(".scommon.") != StringRef::npos;
88}
89
Krzysztof Parzyszek5de59102016-04-21 18:56:45 +000090static const char *getSectionSuffixForSize(unsigned Size) {
91 switch (Size) {
92 default:
93 return "";
94 case 1:
95 return ".1";
96 case 2:
97 return ".2";
98 case 4:
99 return ".4";
100 case 8:
101 return ".8";
102 }
103}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000104
105void HexagonTargetObjectFile::Initialize(MCContext &Ctx,
Krzysztof Parzyszek5de59102016-04-21 18:56:45 +0000106 const TargetMachine &TM) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000107 TargetLoweringObjectFileELF::Initialize(Ctx, TM);
Sid Manning326f8af2014-11-03 14:56:05 +0000108 InitializeELF(TM.Options.UseInitArray);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000109
Krzysztof Parzyszek5de59102016-04-21 18:56:45 +0000110 SmallDataSection =
111 getContext().getELFSection(".sdata", ELF::SHT_PROGBITS,
112 ELF::SHF_WRITE | ELF::SHF_ALLOC |
113 ELF::SHF_HEX_GPREL);
114 SmallBSSSection =
115 getContext().getELFSection(".sbss", ELF::SHT_NOBITS,
116 ELF::SHF_WRITE | ELF::SHF_ALLOC |
117 ELF::SHF_HEX_GPREL);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000118}
119
Krzysztof Parzyszek5de59102016-04-21 18:56:45 +0000120MCSection *HexagonTargetObjectFile::SelectSectionForGlobal(
Peter Collingbourne67335642016-10-24 19:23:39 +0000121 const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
122 TRACE("[SelectSectionForGlobal] GO(" << GO->getName() << ") ");
123 TRACE("input section(" << GO->getSection() << ") ");
Krzysztof Parzyszek5de59102016-04-21 18:56:45 +0000124
Peter Collingbourne67335642016-10-24 19:23:39 +0000125 TRACE((GO->hasPrivateLinkage() ? "private_linkage " : "")
126 << (GO->hasLocalLinkage() ? "local_linkage " : "")
127 << (GO->hasInternalLinkage() ? "internal " : "")
128 << (GO->hasExternalLinkage() ? "external " : "")
129 << (GO->hasCommonLinkage() ? "common_linkage " : "")
130 << (GO->hasCommonLinkage() ? "common " : "" )
Krzysztof Parzyszek5de59102016-04-21 18:56:45 +0000131 << (Kind.isCommon() ? "kind_common " : "" )
132 << (Kind.isBSS() ? "kind_bss " : "" )
133 << (Kind.isBSSLocal() ? "kind_bss_local " : "" ));
134
Peter Collingbourne67335642016-10-24 19:23:39 +0000135 if (isGlobalInSmallSection(GO, TM))
136 return selectSmallSectionForGlobal(GO, Kind, TM);
Krzysztof Parzyszek5de59102016-04-21 18:56:45 +0000137
138 if (Kind.isCommon()) {
139 // This is purely for LTO+Linker Script because commons don't really have a
140 // section. However, the BitcodeSectionWriter pass will query for the
141 // sections of commons (and the linker expects us to know their section) so
142 // we'll return one here.
143 return BSSSection;
144 }
145
146 TRACE("default_ELF_section\n");
147 // Otherwise, we work the same as ELF.
Peter Collingbourne67335642016-10-24 19:23:39 +0000148 return TargetLoweringObjectFileELF::SelectSectionForGlobal(GO, Kind, TM);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000149}
Jyotsna Verma5eb59802013-05-07 19:53:00 +0000150
Krzysztof Parzyszek5de59102016-04-21 18:56:45 +0000151MCSection *HexagonTargetObjectFile::getExplicitSectionGlobal(
Peter Collingbourne67335642016-10-24 19:23:39 +0000152 const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
153 TRACE("[getExplicitSectionGlobal] GO(" << GO->getName() << ") from("
154 << GO->getSection() << ") ");
155 TRACE((GO->hasPrivateLinkage() ? "private_linkage " : "")
156 << (GO->hasLocalLinkage() ? "local_linkage " : "")
157 << (GO->hasInternalLinkage() ? "internal " : "")
158 << (GO->hasExternalLinkage() ? "external " : "")
159 << (GO->hasCommonLinkage() ? "common_linkage " : "")
160 << (GO->hasCommonLinkage() ? "common " : "" )
Krzysztof Parzyszek5de59102016-04-21 18:56:45 +0000161 << (Kind.isCommon() ? "kind_common " : "" )
162 << (Kind.isBSS() ? "kind_bss " : "" )
163 << (Kind.isBSSLocal() ? "kind_bss_local " : "" ));
164
Peter Collingbourne67335642016-10-24 19:23:39 +0000165 if (GO->hasSection()) {
166 StringRef Section = GO->getSection();
Krzysztof Parzyszek5de59102016-04-21 18:56:45 +0000167 if (Section.find(".access.text.group") != StringRef::npos)
Peter Collingbourne67335642016-10-24 19:23:39 +0000168 return getContext().getELFSection(GO->getSection(), ELF::SHT_PROGBITS,
Krzysztof Parzyszek5de59102016-04-21 18:56:45 +0000169 ELF::SHF_ALLOC | ELF::SHF_EXECINSTR);
170 if (Section.find(".access.data.group") != StringRef::npos)
Peter Collingbourne67335642016-10-24 19:23:39 +0000171 return getContext().getELFSection(GO->getSection(), ELF::SHT_PROGBITS,
Krzysztof Parzyszek5de59102016-04-21 18:56:45 +0000172 ELF::SHF_WRITE | ELF::SHF_ALLOC);
173 }
174
Peter Collingbourne67335642016-10-24 19:23:39 +0000175 if (isGlobalInSmallSection(GO, TM))
176 return selectSmallSectionForGlobal(GO, Kind, TM);
Krzysztof Parzyszek5de59102016-04-21 18:56:45 +0000177
178 // Otherwise, we work the same as ELF.
179 TRACE("default_ELF_section\n");
Peter Collingbourne67335642016-10-24 19:23:39 +0000180 return TargetLoweringObjectFileELF::getExplicitSectionGlobal(GO, Kind, TM);
Krzysztof Parzyszek5de59102016-04-21 18:56:45 +0000181}
182
Krzysztof Parzyszek5de59102016-04-21 18:56:45 +0000183/// Return true if this global value should be placed into small data/bss
184/// section.
Peter Collingbourne67335642016-10-24 19:23:39 +0000185bool HexagonTargetObjectFile::isGlobalInSmallSection(const GlobalObject *GO,
Krzysztof Parzyszek5de59102016-04-21 18:56:45 +0000186 const TargetMachine &TM) const {
187 // Only global variables, not functions.
188 DEBUG(dbgs() << "Checking if value is in small-data, -G"
Peter Collingbourne67335642016-10-24 19:23:39 +0000189 << SmallDataThreshold << ": \"" << GO->getName() << "\": ");
190 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GO);
Krzysztof Parzyszek5de59102016-04-21 18:56:45 +0000191 if (!GVar) {
192 DEBUG(dbgs() << "no, not a global variable\n");
193 return false;
194 }
195
196 // Globals with external linkage that have an original section set must be
197 // emitted to that section, regardless of whether we would put them into
198 // small data or not. This is how we can support mixing -G0/-G8 in LTO.
199 if (GVar->hasSection()) {
200 bool IsSmall = isSmallDataSection(GVar->getSection());
201 DEBUG(dbgs() << (IsSmall ? "yes" : "no") << ", has section: "
202 << GVar->getSection() << '\n');
203 return IsSmall;
204 }
205
206 if (GVar->isConstant()) {
207 DEBUG(dbgs() << "no, is a constant\n");
208 return false;
209 }
210
211 bool IsLocal = GVar->hasLocalLinkage();
212 if (!StaticsInSData && IsLocal) {
213 DEBUG(dbgs() << "no, is static\n");
214 return false;
215 }
216
217 Type *GType = GVar->getType();
218 if (PointerType *PT = dyn_cast<PointerType>(GType))
219 GType = PT->getElementType();
220
221 if (isa<ArrayType>(GType)) {
222 DEBUG(dbgs() << "no, is an array\n");
223 return false;
224 }
225
226 // If the type is a struct with no body provided, treat is conservatively.
227 // There cannot be actual definitions of object of such a type in this CU
228 // (only references), so assuming that they are not in sdata is safe. If
229 // these objects end up in the sdata, the references will still be valid.
230 if (StructType *ST = dyn_cast<StructType>(GType)) {
231 if (ST->isOpaque()) {
232 DEBUG(dbgs() << "no, has opaque type\n");
233 return false;
234 }
235 }
236
237 unsigned Size = GVar->getParent()->getDataLayout().getTypeAllocSize(GType);
238 if (Size == 0) {
239 DEBUG(dbgs() << "no, has size 0\n");
240 return false;
241 }
242 if (Size > SmallDataThreshold) {
243 DEBUG(dbgs() << "no, size exceeds sdata threshold: " << Size << '\n');
244 return false;
245 }
246
247 DEBUG(dbgs() << "yes\n");
248 return true;
249}
250
Krzysztof Parzyszek5de59102016-04-21 18:56:45 +0000251bool HexagonTargetObjectFile::isSmallDataEnabled() const {
Jyotsna Verma5eb59802013-05-07 19:53:00 +0000252 return SmallDataThreshold > 0;
253}
254
Krzysztof Parzyszek5de59102016-04-21 18:56:45 +0000255unsigned HexagonTargetObjectFile::getSmallDataSize() const {
256 return SmallDataThreshold;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000257}
258
Krzysztof Parzyszek5de59102016-04-21 18:56:45 +0000259/// Descends any type down to "elementary" components,
260/// discovering the smallest addressable one.
261/// If zero is returned, declaration will not be modified.
262unsigned HexagonTargetObjectFile::getSmallestAddressableSize(const Type *Ty,
263 const GlobalValue *GV, const TargetMachine &TM) const {
264 // Assign the smallest element access size to the highest
265 // value which assembler can handle.
266 unsigned SmallestElement = 8;
267
268 if (!Ty)
269 return 0;
270 switch (Ty->getTypeID()) {
271 case Type::StructTyID: {
272 const StructType *STy = cast<const StructType>(Ty);
273 for (auto &E : STy->elements()) {
274 unsigned AtomicSize = getSmallestAddressableSize(E, GV, TM);
275 if (AtomicSize < SmallestElement)
276 SmallestElement = AtomicSize;
277 }
278 return (STy->getNumElements() == 0) ? 0 : SmallestElement;
279 }
280 case Type::ArrayTyID: {
281 const ArrayType *ATy = cast<const ArrayType>(Ty);
282 return getSmallestAddressableSize(ATy->getElementType(), GV, TM);
283 }
284 case Type::VectorTyID: {
285 const VectorType *PTy = cast<const VectorType>(Ty);
286 return getSmallestAddressableSize(PTy->getElementType(), GV, TM);
287 }
288 case Type::PointerTyID:
289 case Type::HalfTyID:
290 case Type::FloatTyID:
291 case Type::DoubleTyID:
292 case Type::IntegerTyID: {
293 const DataLayout &DL = GV->getParent()->getDataLayout();
294 // It is unfortunate that DL's function take non-const Type*.
295 return DL.getTypeAllocSize(const_cast<Type*>(Ty));
296 }
297 case Type::FunctionTyID:
298 case Type::VoidTyID:
299 case Type::X86_FP80TyID:
300 case Type::FP128TyID:
301 case Type::PPC_FP128TyID:
302 case Type::LabelTyID:
303 case Type::MetadataTyID:
304 case Type::X86_MMXTyID:
305 case Type::TokenTyID:
306 return 0;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000307 }
308
Krzysztof Parzyszek5de59102016-04-21 18:56:45 +0000309 return 0;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000310}
311
Krzysztof Parzyszek5de59102016-04-21 18:56:45 +0000312MCSection *HexagonTargetObjectFile::selectSmallSectionForGlobal(
Peter Collingbourne67335642016-10-24 19:23:39 +0000313 const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
314 const Type *GTy = GO->getType()->getElementType();
315 unsigned Size = getSmallestAddressableSize(GTy, GO, TM);
Krzysztof Parzyszek5de59102016-04-21 18:56:45 +0000316
317 // If we have -ffunction-section or -fdata-section then we should emit the
318 // global value to a unique section specifically for it... even for sdata.
319 bool EmitUniquedSection = TM.getDataSections();
320
321 TRACE("Small data. Size(" << Size << ")");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000322 // Handle Small Section classification here.
Krzysztof Parzyszek5de59102016-04-21 18:56:45 +0000323 if (Kind.isBSS() || Kind.isBSSLocal()) {
324 // If -mno-sort-sda is not set, find out smallest accessible entity in
325 // declaration and add it to the section name string.
326 // Note. It does not track the actual usage of the value, only its de-
327 // claration. Also, compiler adds explicit pad fields to some struct
328 // declarations - they are currently counted towards smallest addres-
329 // sable entity.
330 if (NoSmallDataSorting) {
331 TRACE(" default sbss\n");
332 return SmallBSSSection;
333 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000334
Krzysztof Parzyszek5de59102016-04-21 18:56:45 +0000335 StringRef Prefix(".sbss");
336 SmallString<128> Name(Prefix);
337 Name.append(getSectionSuffixForSize(Size));
338
339 if (EmitUniquedSection) {
340 Name.append(".");
Peter Collingbourne67335642016-10-24 19:23:39 +0000341 Name.append(GO->getName());
Krzysztof Parzyszek5de59102016-04-21 18:56:45 +0000342 }
343 TRACE(" unique sbss(" << Name << ")\n");
344 return getContext().getELFSection(Name.str(), ELF::SHT_NOBITS,
345 ELF::SHF_WRITE | ELF::SHF_ALLOC | ELF::SHF_HEX_GPREL);
346 }
347
348 if (Kind.isCommon()) {
349 // This is purely for LTO+Linker Script because commons don't really have a
350 // section. However, the BitcodeSectionWriter pass will query for the
351 // sections of commons (and the linker expects us to know their section) so
352 // we'll return one here.
353 if (NoSmallDataSorting)
354 return BSSSection;
355
356 Twine Name = Twine(".scommon") + getSectionSuffixForSize(Size);
357 TRACE(" small COMMON (" << Name << ")\n");
358
359 return getContext().getELFSection(Name.str(), ELF::SHT_NOBITS,
360 ELF::SHF_WRITE | ELF::SHF_ALLOC |
361 ELF::SHF_HEX_GPREL);
362 }
363
364 // We could have changed sdata object to a constant... in this
365 // case the Kind could be wrong for it.
366 if (Kind.isMergeableConst()) {
367 TRACE(" const_object_as_data ");
Peter Collingbourne67335642016-10-24 19:23:39 +0000368 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GO);
Krzysztof Parzyszek5de59102016-04-21 18:56:45 +0000369 if (GVar->hasSection() && isSmallDataSection(GVar->getSection()))
370 Kind = SectionKind::getData();
371 }
372
373 if (Kind.isData()) {
374 if (NoSmallDataSorting) {
375 TRACE(" default sdata\n");
376 return SmallDataSection;
377 }
378
379 StringRef Prefix(".sdata");
380 SmallString<128> Name(Prefix);
381 Name.append(getSectionSuffixForSize(Size));
382
383 if (EmitUniquedSection) {
384 Name.append(".");
Peter Collingbourne67335642016-10-24 19:23:39 +0000385 Name.append(GO->getName());
Krzysztof Parzyszek5de59102016-04-21 18:56:45 +0000386 }
387 TRACE(" unique sdata(" << Name << ")\n");
388 return getContext().getELFSection(Name.str(), ELF::SHT_PROGBITS,
389 ELF::SHF_WRITE | ELF::SHF_ALLOC | ELF::SHF_HEX_GPREL);
390 }
391
392 TRACE("default ELF section\n");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000393 // Otherwise, we work the same as ELF.
Peter Collingbourne67335642016-10-24 19:23:39 +0000394 return TargetLoweringObjectFileELF::SelectSectionForGlobal(GO, Kind, TM);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000395}