xref: /llvm-project/llvm/lib/Target/Hexagon/HexagonTargetObjectFile.cpp (revision 8c24f33158d81d5f4b0c5d27c2f07396f0f1484b)
1 //===-- HexagonTargetObjectFile.cpp ---------------------------------------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file contains the declarations of the HexagonTargetAsmInfo properties.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #define DEBUG_TYPE "hexagon-sdata"
14 
15 #include "HexagonTargetObjectFile.h"
16 #include "llvm/ADT/SmallString.h"
17 #include "llvm/ADT/StringRef.h"
18 #include "llvm/ADT/Twine.h"
19 #include "llvm/BinaryFormat/ELF.h"
20 #include "llvm/IR/DataLayout.h"
21 #include "llvm/IR/DerivedTypes.h"
22 #include "llvm/IR/GlobalObject.h"
23 #include "llvm/IR/GlobalValue.h"
24 #include "llvm/IR/GlobalVariable.h"
25 #include "llvm/IR/Type.h"
26 #include "llvm/MC/MCContext.h"
27 #include "llvm/MC/SectionKind.h"
28 #include "llvm/Support/Casting.h"
29 #include "llvm/Support/CommandLine.h"
30 #include "llvm/Support/Debug.h"
31 #include "llvm/Support/raw_ostream.h"
32 #include "llvm/Target/TargetMachine.h"
33 
34 using namespace llvm;
35 
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 
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 
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 
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
64 #define TRACE(X)                                                               \
65   do {                                                                         \
66     if (TraceGVPlacement) {                                                    \
67       TRACE_TO(errs(), X);                                                     \
68     }                                                                          \
69   } while (false)
70 #else
71 #define TRACE(X)                                                               \
72   do {                                                                         \
73     if (TraceGVPlacement) {                                                    \
74       TRACE_TO(errs(), X);                                                     \
75     } else {                                                                   \
76       LLVM_DEBUG(TRACE_TO(dbgs(), X));                                         \
77     }                                                                          \
78   } while (false)
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 
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 }
111 
112 void HexagonTargetObjectFile::Initialize(MCContext &Ctx,
113       const TargetMachine &TM) {
114   TargetLoweringObjectFileELF::Initialize(Ctx, TM);
115 
116   SmallDataSection =
117     getContext().getELFSection(".sdata", ELF::SHT_PROGBITS,
118                                ELF::SHF_WRITE | ELF::SHF_ALLOC |
119                                ELF::SHF_HEX_GPREL);
120   SmallBSSSection =
121     getContext().getELFSection(".sbss", ELF::SHT_NOBITS,
122                                ELF::SHF_WRITE | ELF::SHF_ALLOC |
123                                ELF::SHF_HEX_GPREL);
124 }
125 
126 MCSection *HexagonTargetObjectFile::SelectSectionForGlobal(
127     const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
128   TRACE("[SelectSectionForGlobal] GO(" << GO->getName() << ") ");
129   TRACE("input section(" << GO->getSection() << ") ");
130 
131   TRACE((GO->hasPrivateLinkage() ? "private_linkage " : "")
132          << (GO->hasLocalLinkage() ? "local_linkage " : "")
133          << (GO->hasInternalLinkage() ? "internal " : "")
134          << (GO->hasExternalLinkage() ? "external " : "")
135          << (GO->hasCommonLinkage() ? "common_linkage " : "")
136          << (GO->hasCommonLinkage() ? "common " : "" )
137          << (Kind.isCommon() ? "kind_common " : "" )
138          << (Kind.isBSS() ? "kind_bss " : "" )
139          << (Kind.isBSSLocal() ? "kind_bss_local " : "" ));
140 
141   // If the lookup table is used by more than one function, do not place
142   // it in text section.
143   if (EmitLutInText && GO->getName().startswith("switch.table")) {
144     if (const Function *Fn = getLutUsedFunction(GO))
145       return selectSectionForLookupTable(GO, TM, Fn);
146   }
147 
148   if (isGlobalInSmallSection(GO, TM))
149     return selectSmallSectionForGlobal(GO, Kind, TM);
150 
151   if (Kind.isCommon()) {
152     // This is purely for LTO+Linker Script because commons don't really have a
153     // section. However, the BitcodeSectionWriter pass will query for the
154     // sections of commons (and the linker expects us to know their section) so
155     // we'll return one here.
156     return BSSSection;
157   }
158 
159   TRACE("default_ELF_section\n");
160   // Otherwise, we work the same as ELF.
161   return TargetLoweringObjectFileELF::SelectSectionForGlobal(GO, Kind, TM);
162 }
163 
164 MCSection *HexagonTargetObjectFile::getExplicitSectionGlobal(
165     const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
166   TRACE("[getExplicitSectionGlobal] GO(" << GO->getName() << ") from("
167         << GO->getSection() << ") ");
168   TRACE((GO->hasPrivateLinkage() ? "private_linkage " : "")
169          << (GO->hasLocalLinkage() ? "local_linkage " : "")
170          << (GO->hasInternalLinkage() ? "internal " : "")
171          << (GO->hasExternalLinkage() ? "external " : "")
172          << (GO->hasCommonLinkage() ? "common_linkage " : "")
173          << (GO->hasCommonLinkage() ? "common " : "" )
174          << (Kind.isCommon() ? "kind_common " : "" )
175          << (Kind.isBSS() ? "kind_bss " : "" )
176          << (Kind.isBSSLocal() ? "kind_bss_local " : "" ));
177 
178   if (GO->hasSection()) {
179     StringRef Section = GO->getSection();
180     if (Section.find(".access.text.group") != StringRef::npos)
181       return getContext().getELFSection(GO->getSection(), ELF::SHT_PROGBITS,
182                                         ELF::SHF_ALLOC | ELF::SHF_EXECINSTR);
183     if (Section.find(".access.data.group") != StringRef::npos)
184       return getContext().getELFSection(GO->getSection(), ELF::SHT_PROGBITS,
185                                         ELF::SHF_WRITE | ELF::SHF_ALLOC);
186   }
187 
188   if (isGlobalInSmallSection(GO, TM))
189     return selectSmallSectionForGlobal(GO, Kind, TM);
190 
191   // Otherwise, we work the same as ELF.
192   TRACE("default_ELF_section\n");
193   return TargetLoweringObjectFileELF::getExplicitSectionGlobal(GO, Kind, TM);
194 }
195 
196 /// Return true if this global value should be placed into small data/bss
197 /// section.
198 bool HexagonTargetObjectFile::isGlobalInSmallSection(const GlobalObject *GO,
199       const TargetMachine &TM) const {
200   bool HaveSData = isSmallDataEnabled(TM);
201   if (!HaveSData)
202     LLVM_DEBUG(dbgs() << "Small-data allocation is disabled, but symbols "
203                          "may have explicit section assignments...\n");
204   // Only global variables, not functions.
205   LLVM_DEBUG(dbgs() << "Checking if value is in small-data, -G"
206                     << SmallDataThreshold << ": \"" << GO->getName() << "\": ");
207   const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GO);
208   if (!GVar) {
209     LLVM_DEBUG(dbgs() << "no, not a global variable\n");
210     return false;
211   }
212 
213   // Globals with external linkage that have an original section set must be
214   // emitted to that section, regardless of whether we would put them into
215   // small data or not. This is how we can support mixing -G0/-G8 in LTO.
216   if (GVar->hasSection()) {
217     bool IsSmall = isSmallDataSection(GVar->getSection());
218     LLVM_DEBUG(dbgs() << (IsSmall ? "yes" : "no")
219                       << ", has section: " << GVar->getSection() << '\n');
220     return IsSmall;
221   }
222 
223   // If sdata is disabled, stop the checks here.
224   if (!HaveSData) {
225     LLVM_DEBUG(dbgs() << "no, small-data allocation is disabled\n");
226     return false;
227   }
228 
229   if (GVar->isConstant()) {
230     LLVM_DEBUG(dbgs() << "no, is a constant\n");
231     return false;
232   }
233 
234   bool IsLocal = GVar->hasLocalLinkage();
235   if (!StaticsInSData && IsLocal) {
236     LLVM_DEBUG(dbgs() << "no, is static\n");
237     return false;
238   }
239 
240   Type *GType = GVar->getValueType();
241   if (isa<ArrayType>(GType)) {
242     LLVM_DEBUG(dbgs() << "no, is an array\n");
243     return false;
244   }
245 
246   // If the type is a struct with no body provided, treat is conservatively.
247   // There cannot be actual definitions of object of such a type in this CU
248   // (only references), so assuming that they are not in sdata is safe. If
249   // these objects end up in the sdata, the references will still be valid.
250   if (StructType *ST = dyn_cast<StructType>(GType)) {
251     if (ST->isOpaque()) {
252       LLVM_DEBUG(dbgs() << "no, has opaque type\n");
253       return false;
254     }
255   }
256 
257   unsigned Size = GVar->getParent()->getDataLayout().getTypeAllocSize(GType);
258   if (Size == 0) {
259     LLVM_DEBUG(dbgs() << "no, has size 0\n");
260     return false;
261   }
262   if (Size > SmallDataThreshold) {
263     LLVM_DEBUG(dbgs() << "no, size exceeds sdata threshold: " << Size << '\n');
264     return false;
265   }
266 
267   LLVM_DEBUG(dbgs() << "yes\n");
268   return true;
269 }
270 
271 bool HexagonTargetObjectFile::isSmallDataEnabled(const TargetMachine &TM)
272     const {
273   return SmallDataThreshold > 0 && !TM.isPositionIndependent();
274 }
275 
276 unsigned HexagonTargetObjectFile::getSmallDataSize() const {
277   return SmallDataThreshold;
278 }
279 
280 bool HexagonTargetObjectFile::shouldPutJumpTableInFunctionSection(
281     bool UsesLabelDifference, const Function &F) const {
282   return EmitJtInText;
283 }
284 
285 /// Descends any type down to "elementary" components,
286 /// discovering the smallest addressable one.
287 /// If zero is returned, declaration will not be modified.
288 unsigned HexagonTargetObjectFile::getSmallestAddressableSize(const Type *Ty,
289       const GlobalValue *GV, const TargetMachine &TM) const {
290   // Assign the smallest element access size to the highest
291   // value which assembler can handle.
292   unsigned SmallestElement = 8;
293 
294   if (!Ty)
295     return 0;
296   switch (Ty->getTypeID()) {
297   case Type::StructTyID: {
298     const StructType *STy = cast<const StructType>(Ty);
299     for (auto &E : STy->elements()) {
300       unsigned AtomicSize = getSmallestAddressableSize(E, GV, TM);
301       if (AtomicSize < SmallestElement)
302         SmallestElement = AtomicSize;
303     }
304     return (STy->getNumElements() == 0) ? 0 : SmallestElement;
305   }
306   case Type::ArrayTyID: {
307     const ArrayType *ATy = cast<const ArrayType>(Ty);
308     return getSmallestAddressableSize(ATy->getElementType(), GV, TM);
309   }
310   case Type::FixedVectorTyID:
311   case Type::ScalableVectorTyID: {
312     const VectorType *PTy = cast<const VectorType>(Ty);
313     return getSmallestAddressableSize(PTy->getElementType(), GV, TM);
314   }
315   case Type::PointerTyID:
316   case Type::HalfTyID:
317   case Type::FloatTyID:
318   case Type::DoubleTyID:
319   case Type::IntegerTyID: {
320     const DataLayout &DL = GV->getParent()->getDataLayout();
321     // It is unfortunate that DL's function take non-const Type*.
322     return DL.getTypeAllocSize(const_cast<Type*>(Ty));
323   }
324   case Type::FunctionTyID:
325   case Type::VoidTyID:
326   case Type::BFloatTyID:
327   case Type::X86_FP80TyID:
328   case Type::FP128TyID:
329   case Type::PPC_FP128TyID:
330   case Type::LabelTyID:
331   case Type::MetadataTyID:
332   case Type::X86_MMXTyID:
333   case Type::TokenTyID:
334     return 0;
335   }
336 
337   return 0;
338 }
339 
340 MCSection *HexagonTargetObjectFile::selectSmallSectionForGlobal(
341     const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
342   const Type *GTy = GO->getValueType();
343   unsigned Size = getSmallestAddressableSize(GTy, GO, TM);
344 
345   // If we have -ffunction-section or -fdata-section then we should emit the
346   // global value to a unique section specifically for it... even for sdata.
347   bool EmitUniquedSection = TM.getDataSections();
348 
349   TRACE("Small data. Size(" << Size << ")");
350   // Handle Small Section classification here.
351   if (Kind.isBSS() || Kind.isBSSLocal()) {
352     // If -mno-sort-sda is not set, find out smallest accessible entity in
353     // declaration and add it to the section name string.
354     // Note. It does not track the actual usage of the value, only its de-
355     // claration. Also, compiler adds explicit pad fields to some struct
356     // declarations - they are currently counted towards smallest addres-
357     // sable entity.
358     if (NoSmallDataSorting) {
359       TRACE(" default sbss\n");
360       return SmallBSSSection;
361     }
362 
363     StringRef Prefix(".sbss");
364     SmallString<128> Name(Prefix);
365     Name.append(getSectionSuffixForSize(Size));
366 
367     if (EmitUniquedSection) {
368       Name.append(".");
369       Name.append(GO->getName());
370     }
371     TRACE(" unique sbss(" << Name << ")\n");
372     return getContext().getELFSection(Name.str(), ELF::SHT_NOBITS,
373                 ELF::SHF_WRITE | ELF::SHF_ALLOC | ELF::SHF_HEX_GPREL);
374   }
375 
376   if (Kind.isCommon()) {
377     // This is purely for LTO+Linker Script because commons don't really have a
378     // section. However, the BitcodeSectionWriter pass will query for the
379     // sections of commons (and the linker expects us to know their section) so
380     // we'll return one here.
381     if (NoSmallDataSorting)
382       return BSSSection;
383 
384     Twine Name = Twine(".scommon") + getSectionSuffixForSize(Size);
385     TRACE(" small COMMON (" << Name << ")\n");
386 
387     return getContext().getELFSection(Name.str(), ELF::SHT_NOBITS,
388                                       ELF::SHF_WRITE | ELF::SHF_ALLOC |
389                                       ELF::SHF_HEX_GPREL);
390   }
391 
392   // We could have changed sdata object to a constant... in this
393   // case the Kind could be wrong for it.
394   if (Kind.isMergeableConst()) {
395     TRACE(" const_object_as_data ");
396     const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GO);
397     if (GVar->hasSection() && isSmallDataSection(GVar->getSection()))
398       Kind = SectionKind::getData();
399   }
400 
401   if (Kind.isData()) {
402     if (NoSmallDataSorting) {
403       TRACE(" default sdata\n");
404       return SmallDataSection;
405     }
406 
407     StringRef Prefix(".sdata");
408     SmallString<128> Name(Prefix);
409     Name.append(getSectionSuffixForSize(Size));
410 
411     if (EmitUniquedSection) {
412       Name.append(".");
413       Name.append(GO->getName());
414     }
415     TRACE(" unique sdata(" << Name << ")\n");
416     return getContext().getELFSection(Name.str(), ELF::SHT_PROGBITS,
417                 ELF::SHF_WRITE | ELF::SHF_ALLOC | ELF::SHF_HEX_GPREL);
418   }
419 
420   TRACE("default ELF section\n");
421   // Otherwise, we work the same as ELF.
422   return TargetLoweringObjectFileELF::SelectSectionForGlobal(GO, Kind, TM);
423 }
424 
425 // Return the function that uses the lookup table. If there are more
426 // than one live function that uses this look table, bail out and place
427 // the lookup table in default section.
428 const Function *
429 HexagonTargetObjectFile::getLutUsedFunction(const GlobalObject *GO) const {
430   const Function *ReturnFn = nullptr;
431   for (auto U : GO->users()) {
432     // validate each instance of user to be a live function.
433     auto *I = dyn_cast<Instruction>(U);
434     if (!I)
435       continue;
436     auto *Bb = I->getParent();
437     if (!Bb)
438       continue;
439     auto *UserFn = Bb->getParent();
440     if (!ReturnFn)
441       ReturnFn = UserFn;
442     else if (ReturnFn != UserFn)
443       return nullptr;
444   }
445   return ReturnFn;
446 }
447 
448 MCSection *HexagonTargetObjectFile::selectSectionForLookupTable(
449     const GlobalObject *GO, const TargetMachine &TM, const Function *Fn) const {
450 
451   SectionKind Kind = SectionKind::getText();
452   // If the function has explicit section, place the lookup table in this
453   // explicit section.
454   if (Fn->hasSection())
455     return getExplicitSectionGlobal(Fn, Kind, TM);
456 
457   const auto *FuncObj = dyn_cast<GlobalObject>(Fn);
458   return SelectSectionForGlobal(FuncObj, Kind, TM);
459 }
460