annotate llvm/lib/Target/Hexagon/HexagonTargetObjectFile.cpp @ 152:e8a9b4f4d755

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