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