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