1 //===-- HexagonTargetObjectFile.cpp ---------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file contains the declarations of the HexagonTargetAsmInfo properties.
11 //===----------------------------------------------------------------------===//
13 #include "HexagonTargetObjectFile.h"
14 #include "llvm/ADT/SmallString.h"
15 #include "llvm/ADT/StringRef.h"
16 #include "llvm/ADT/Twine.h"
17 #include "llvm/BinaryFormat/ELF.h"
18 #include "llvm/IR/DataLayout.h"
19 #include "llvm/IR/DerivedTypes.h"
20 #include "llvm/IR/GlobalObject.h"
21 #include "llvm/IR/GlobalValue.h"
22 #include "llvm/IR/GlobalVariable.h"
23 #include "llvm/IR/Module.h"
24 #include "llvm/IR/Type.h"
25 #include "llvm/MC/MCContext.h"
26 #include "llvm/MC/SectionKind.h"
27 #include "llvm/Support/Casting.h"
28 #include "llvm/Support/CommandLine.h"
29 #include "llvm/Support/Debug.h"
30 #include "llvm/Support/raw_ostream.h"
31 #include "llvm/Target/TargetMachine.h"
33 #define DEBUG_TYPE "hexagon-sdata"
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"));
41 static cl::opt
<bool> NoSmallDataSorting("mno-sort-sda", cl::init(false),
42 cl::Hidden
, cl::desc("Disable small data sections sorting"));
45 StaticsInSData("hexagon-statics-in-small-data", cl::Hidden
,
46 cl::desc("Allow static variables in .sdata"));
48 static cl::opt
<bool> TraceGVPlacement("trace-gv-placement",
49 cl::Hidden
, cl::init(false),
50 cl::desc("Trace global value placement"));
53 EmitJtInText("hexagon-emit-jt-text", cl::Hidden
, cl::init(false),
54 cl::desc("Emit hexagon jump tables in function section"));
57 EmitLutInText("hexagon-emit-lut-text", cl::Hidden
, cl::init(false),
58 cl::desc("Emit hexagon lookup tables in function section"));
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
67 if (TraceGVPlacement) { \
68 TRACE_TO(errs(), X); \
74 if (TraceGVPlacement) { \
75 TRACE_TO(errs(), X); \
77 LLVM_DEBUG(TRACE_TO(dbgs(), X)); \
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
== ".sdata" || Sec
== ".sbss" || Sec
== ".scommon")
91 // If either ".sdata." or ".sbss." is a substring of the section name
92 // then put the symbol in small data.
93 return Sec
.contains(".sdata.") || Sec
.contains(".sbss.") ||
94 Sec
.contains(".scommon.");
97 static const char *getSectionSuffixForSize(unsigned Size
) {
112 void HexagonTargetObjectFile::Initialize(MCContext
&Ctx
,
113 const TargetMachine
&TM
) {
114 TargetLoweringObjectFileELF::Initialize(Ctx
, TM
);
117 getContext().getELFSection(".sdata", ELF::SHT_PROGBITS
,
118 ELF::SHF_WRITE
| ELF::SHF_ALLOC
|
121 getContext().getELFSection(".sbss", ELF::SHT_NOBITS
,
122 ELF::SHF_WRITE
| ELF::SHF_ALLOC
|
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() << ") ");
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 " : "" ));
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().starts_with("switch.table")) {
144 if (const Function
*Fn
= getLutUsedFunction(GO
))
145 return selectSectionForLookupTable(GO
, TM
, Fn
);
148 if (isGlobalInSmallSection(GO
, TM
))
149 return selectSmallSectionForGlobal(GO
, Kind
, TM
);
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.
159 TRACE("default_ELF_section\n");
160 // Otherwise, we work the same as ELF.
161 return TargetLoweringObjectFileELF::SelectSectionForGlobal(GO
, Kind
, TM
);
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 " : "" ));
178 if (GO
->hasSection()) {
179 StringRef Section
= GO
->getSection();
180 if (Section
.contains(".access.text.group"))
181 return getContext().getELFSection(GO
->getSection(), ELF::SHT_PROGBITS
,
182 ELF::SHF_ALLOC
| ELF::SHF_EXECINSTR
);
183 if (Section
.contains(".access.data.group"))
184 return getContext().getELFSection(GO
->getSection(), ELF::SHT_PROGBITS
,
185 ELF::SHF_WRITE
| ELF::SHF_ALLOC
);
188 if (isGlobalInSmallSection(GO
, TM
))
189 return selectSmallSectionForGlobal(GO
, Kind
, TM
);
191 // Otherwise, we work the same as ELF.
192 TRACE("default_ELF_section\n");
193 return TargetLoweringObjectFileELF::getExplicitSectionGlobal(GO
, Kind
, TM
);
196 /// Return true if this global value should be placed into small data/bss
198 bool HexagonTargetObjectFile::isGlobalInSmallSection(const GlobalObject
*GO
,
199 const TargetMachine
&TM
) const {
200 bool HaveSData
= isSmallDataEnabled(TM
);
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
);
209 LLVM_DEBUG(dbgs() << "no, not a global variable\n");
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');
223 // If sdata is disabled, stop the checks here.
225 LLVM_DEBUG(dbgs() << "no, small-data allocation is disabled\n");
229 if (GVar
->isConstant()) {
230 LLVM_DEBUG(dbgs() << "no, is a constant\n");
234 bool IsLocal
= GVar
->hasLocalLinkage();
235 if (!StaticsInSData
&& IsLocal
) {
236 LLVM_DEBUG(dbgs() << "no, is static\n");
240 Type
*GType
= GVar
->getValueType();
241 if (isa
<ArrayType
>(GType
)) {
242 LLVM_DEBUG(dbgs() << "no, is an array\n");
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");
257 unsigned Size
= GVar
->getDataLayout().getTypeAllocSize(GType
);
259 LLVM_DEBUG(dbgs() << "no, has size 0\n");
262 if (Size
> SmallDataThreshold
) {
263 LLVM_DEBUG(dbgs() << "no, size exceeds sdata threshold: " << Size
<< '\n');
267 LLVM_DEBUG(dbgs() << "yes\n");
271 bool HexagonTargetObjectFile::isSmallDataEnabled(const TargetMachine
&TM
)
273 return SmallDataThreshold
> 0 && !TM
.isPositionIndependent();
276 unsigned HexagonTargetObjectFile::getSmallDataSize() const {
277 return SmallDataThreshold
;
280 bool HexagonTargetObjectFile::shouldPutJumpTableInFunctionSection(
281 bool UsesLabelDifference
, const Function
&F
) const {
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;
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
;
304 return (STy
->getNumElements() == 0) ? 0 : SmallestElement
;
306 case Type::ArrayTyID
: {
307 const ArrayType
*ATy
= cast
<const ArrayType
>(Ty
);
308 return getSmallestAddressableSize(ATy
->getElementType(), GV
, TM
);
310 case Type::FixedVectorTyID
:
311 case Type::ScalableVectorTyID
: {
312 const VectorType
*PTy
= cast
<const VectorType
>(Ty
);
313 return getSmallestAddressableSize(PTy
->getElementType(), GV
, TM
);
315 case Type::PointerTyID
:
317 case Type::FloatTyID
:
318 case Type::DoubleTyID
:
319 case Type::IntegerTyID
: {
320 const DataLayout
&DL
= GV
->getDataLayout();
321 // It is unfortunate that DL's function take non-const Type*.
322 return DL
.getTypeAllocSize(const_cast<Type
*>(Ty
));
324 case Type::FunctionTyID
:
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::X86_AMXTyID
:
334 case Type::TokenTyID
:
335 case Type::TypedPointerTyID
:
336 case Type::TargetExtTyID
:
343 MCSection
*HexagonTargetObjectFile::selectSmallSectionForGlobal(
344 const GlobalObject
*GO
, SectionKind Kind
, const TargetMachine
&TM
) const {
345 const Type
*GTy
= GO
->getValueType();
346 unsigned Size
= getSmallestAddressableSize(GTy
, GO
, TM
);
348 // If we have -ffunction-section or -fdata-section then we should emit the
349 // global value to a unique section specifically for it... even for sdata.
350 bool EmitUniquedSection
= TM
.getDataSections();
352 TRACE("Small data. Size(" << Size
<< ")");
353 // Handle Small Section classification here.
354 if (Kind
.isBSS() || Kind
.isBSSLocal()) {
355 // If -mno-sort-sda is not set, find out smallest accessible entity in
356 // declaration and add it to the section name string.
357 // Note. It does not track the actual usage of the value, only its de-
358 // claration. Also, compiler adds explicit pad fields to some struct
359 // declarations - they are currently counted towards smallest addres-
361 if (NoSmallDataSorting
) {
362 TRACE(" default sbss\n");
363 return SmallBSSSection
;
366 StringRef
Prefix(".sbss");
367 SmallString
<128> Name(Prefix
);
368 Name
.append(getSectionSuffixForSize(Size
));
370 if (EmitUniquedSection
) {
372 Name
.append(GO
->getName());
374 TRACE(" unique sbss(" << Name
<< ")\n");
375 return getContext().getELFSection(Name
.str(), ELF::SHT_NOBITS
,
376 ELF::SHF_WRITE
| ELF::SHF_ALLOC
| ELF::SHF_HEX_GPREL
);
379 if (Kind
.isCommon()) {
380 // This is purely for LTO+Linker Script because commons don't really have a
381 // section. However, the BitcodeSectionWriter pass will query for the
382 // sections of commons (and the linker expects us to know their section) so
383 // we'll return one here.
384 if (NoSmallDataSorting
)
387 Twine Name
= Twine(".scommon") + getSectionSuffixForSize(Size
);
388 TRACE(" small COMMON (" << Name
<< ")\n");
390 return getContext().getELFSection(Name
.str(), ELF::SHT_NOBITS
,
391 ELF::SHF_WRITE
| ELF::SHF_ALLOC
|
395 // We could have changed sdata object to a constant... in this
396 // case the Kind could be wrong for it.
397 if (Kind
.isMergeableConst()) {
398 TRACE(" const_object_as_data ");
399 const GlobalVariable
*GVar
= dyn_cast
<GlobalVariable
>(GO
);
400 if (GVar
->hasSection() && isSmallDataSection(GVar
->getSection()))
401 Kind
= SectionKind::getData();
405 if (NoSmallDataSorting
) {
406 TRACE(" default sdata\n");
407 return SmallDataSection
;
410 StringRef
Prefix(".sdata");
411 SmallString
<128> Name(Prefix
);
412 Name
.append(getSectionSuffixForSize(Size
));
414 if (EmitUniquedSection
) {
416 Name
.append(GO
->getName());
418 TRACE(" unique sdata(" << Name
<< ")\n");
419 return getContext().getELFSection(Name
.str(), ELF::SHT_PROGBITS
,
420 ELF::SHF_WRITE
| ELF::SHF_ALLOC
| ELF::SHF_HEX_GPREL
);
423 TRACE("default ELF section\n");
424 // Otherwise, we work the same as ELF.
425 return TargetLoweringObjectFileELF::SelectSectionForGlobal(GO
, Kind
, TM
);
428 // Return the function that uses the lookup table. If there are more
429 // than one live function that uses this look table, bail out and place
430 // the lookup table in default section.
432 HexagonTargetObjectFile::getLutUsedFunction(const GlobalObject
*GO
) const {
433 const Function
*ReturnFn
= nullptr;
434 for (const auto *U
: GO
->users()) {
435 // validate each instance of user to be a live function.
436 auto *I
= dyn_cast
<Instruction
>(U
);
439 auto *Bb
= I
->getParent();
442 auto *UserFn
= Bb
->getParent();
445 else if (ReturnFn
!= UserFn
)
451 MCSection
*HexagonTargetObjectFile::selectSectionForLookupTable(
452 const GlobalObject
*GO
, const TargetMachine
&TM
, const Function
*Fn
) const {
454 SectionKind Kind
= SectionKind::getText();
455 // If the function has explicit section, place the lookup table in this
457 if (Fn
->hasSection())
458 return getExplicitSectionGlobal(Fn
, Kind
, TM
);
460 const auto *FuncObj
= dyn_cast
<GlobalObject
>(Fn
);
461 return SelectSectionForGlobal(FuncObj
, Kind
, TM
);