blob: 386cd14c827beaf3e6cb7e03dd8b332f5162a3cf [file] [log] [blame]
Krzysztof Parzyszek5de59102016-04-21 18:56:45 +00001//===-- HexagonTargetObjectFile.cpp ---------------------------------------===//
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains the declarations of the HexagonTargetAsmInfo properties.
11//
12//===----------------------------------------------------------------------===//
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000013
Krzysztof Parzyszek5de59102016-04-21 18:56:45 +000014#define DEBUG_TYPE "hexagon-sdata"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000015
Krzysztof Parzyszek5de59102016-04-21 18:56:45 +000016#include "HexagonTargetObjectFile.h"
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000017#include "llvm/ADT/SmallString.h"
18#include "llvm/ADT/StringRef.h"
19#include "llvm/ADT/Twine.h"
Zachary Turner264b5d92017-06-07 03:48:56 +000020#include "llvm/BinaryFormat/ELF.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000021#include "llvm/IR/DataLayout.h"
22#include "llvm/IR/DerivedTypes.h"
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000023#include "llvm/IR/GlobalObject.h"
24#include "llvm/IR/GlobalValue.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000025#include "llvm/IR/GlobalVariable.h"
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000026#include "llvm/IR/Type.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000027#include "llvm/MC/MCContext.h"
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000028#include "llvm/MC/SectionKind.h"
29#include "llvm/Support/Casting.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000030#include "llvm/Support/CommandLine.h"
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000031#include "llvm/Support/Debug.h"
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000032#include "llvm/Support/raw_ostream.h"
33#include "llvm/Target/TargetMachine.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000034
35using namespace llvm;
36
Krzysztof Parzyszek5de59102016-04-21 18:56:45 +000037static 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"));
40
41static cl::opt<bool> NoSmallDataSorting("mno-sort-sda", cl::init(false),
42 cl::Hidden, cl::desc("Disable small data sections sorting"));
43
44static cl::opt<bool> StaticsInSData("hexagon-statics-in-small-data",
45 cl::init(false), cl::Hidden, cl::ZeroOrMore,
46 cl::desc("Allow static variables in .sdata"));
47
48static cl::opt<bool> TraceGVPlacement("trace-gv-placement",
49 cl::Hidden, cl::init(false),
50 cl::desc("Trace global value placement"));
51
Sumanth Gundapaneni8c5d5952017-06-30 20:21:48 +000052static cl::opt<bool>
53 EmitJtInText("hexagon-emit-jt-text", cl::Hidden, cl::init(false),
54 cl::desc("Emit hexagon jump tables in function section"));
55
Sumanth Gundapanenid5aa0f32017-07-18 15:31:37 +000056static cl::opt<bool>
57 EmitLutInText("hexagon-emit-lut-text", cl::Hidden, cl::init(false),
58 cl::desc("Emit hexagon lookup tables in function section"));
59
Krzysztof Parzyszek5de59102016-04-21 18:56:45 +000060// 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
64#ifdef NDEBUG
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000065#define TRACE(X) \
66 do { \
67 if (TraceGVPlacement) { \
68 TRACE_TO(errs(), X); \
69 } \
70 } while (false)
Krzysztof Parzyszek5de59102016-04-21 18:56:45 +000071#else
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000072#define TRACE(X) \
73 do { \
74 if (TraceGVPlacement) { \
75 TRACE_TO(errs(), X); \
76 } else { \
Nicola Zaghend34e60c2018-05-14 12:53:11 +000077 LLVM_DEBUG(TRACE_TO(dbgs(), X)); \
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000078 } \
79 } while (false)
Krzysztof Parzyszek5de59102016-04-21 18:56:45 +000080#endif
81
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.
86static 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.equals(".sdata") || Sec.equals(".sbss") || Sec.equals(".scommon"))
90 return true;
91 // If either ".sdata." or ".sbss." is a substring of the section name
92 // then put the symbol in small data.
93 return Sec.find(".sdata.") != StringRef::npos ||
94 Sec.find(".sbss.") != StringRef::npos ||
95 Sec.find(".scommon.") != StringRef::npos;
96}
97
Krzysztof Parzyszek5de59102016-04-21 18:56:45 +000098static const char *getSectionSuffixForSize(unsigned Size) {
99 switch (Size) {
100 default:
101 return "";
102 case 1:
103 return ".1";
104 case 2:
105 return ".2";
106 case 4:
107 return ".4";
108 case 8:
109 return ".8";
110 }
111}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000112
113void HexagonTargetObjectFile::Initialize(MCContext &Ctx,
Krzysztof Parzyszek5de59102016-04-21 18:56:45 +0000114 const TargetMachine &TM) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000115 TargetLoweringObjectFileELF::Initialize(Ctx, TM);
Sid Manning326f8af2014-11-03 14:56:05 +0000116 InitializeELF(TM.Options.UseInitArray);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000117
Krzysztof Parzyszek5de59102016-04-21 18:56:45 +0000118 SmallDataSection =
119 getContext().getELFSection(".sdata", ELF::SHT_PROGBITS,
120 ELF::SHF_WRITE | ELF::SHF_ALLOC |
121 ELF::SHF_HEX_GPREL);
122 SmallBSSSection =
123 getContext().getELFSection(".sbss", ELF::SHT_NOBITS,
124 ELF::SHF_WRITE | ELF::SHF_ALLOC |
125 ELF::SHF_HEX_GPREL);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000126}
127
Krzysztof Parzyszek5de59102016-04-21 18:56:45 +0000128MCSection *HexagonTargetObjectFile::SelectSectionForGlobal(
Peter Collingbourne67335642016-10-24 19:23:39 +0000129 const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
130 TRACE("[SelectSectionForGlobal] GO(" << GO->getName() << ") ");
131 TRACE("input section(" << GO->getSection() << ") ");
Krzysztof Parzyszek5de59102016-04-21 18:56:45 +0000132
Peter Collingbourne67335642016-10-24 19:23:39 +0000133 TRACE((GO->hasPrivateLinkage() ? "private_linkage " : "")
134 << (GO->hasLocalLinkage() ? "local_linkage " : "")
135 << (GO->hasInternalLinkage() ? "internal " : "")
136 << (GO->hasExternalLinkage() ? "external " : "")
137 << (GO->hasCommonLinkage() ? "common_linkage " : "")
138 << (GO->hasCommonLinkage() ? "common " : "" )
Krzysztof Parzyszek5de59102016-04-21 18:56:45 +0000139 << (Kind.isCommon() ? "kind_common " : "" )
140 << (Kind.isBSS() ? "kind_bss " : "" )
141 << (Kind.isBSSLocal() ? "kind_bss_local " : "" ));
142
Sumanth Gundapanenid5aa0f32017-07-18 15:31:37 +0000143 // If the lookup table is used by more than one function, do not place
144 // it in text section.
145 if (EmitLutInText && GO->getName().startswith("switch.table")) {
146 if (const Function *Fn = getLutUsedFunction(GO))
147 return selectSectionForLookupTable(GO, TM, Fn);
148 }
149
Peter Collingbourne67335642016-10-24 19:23:39 +0000150 if (isGlobalInSmallSection(GO, TM))
151 return selectSmallSectionForGlobal(GO, Kind, TM);
Krzysztof Parzyszek5de59102016-04-21 18:56:45 +0000152
153 if (Kind.isCommon()) {
154 // This is purely for LTO+Linker Script because commons don't really have a
155 // section. However, the BitcodeSectionWriter pass will query for the
156 // sections of commons (and the linker expects us to know their section) so
157 // we'll return one here.
158 return BSSSection;
159 }
160
161 TRACE("default_ELF_section\n");
162 // Otherwise, we work the same as ELF.
Peter Collingbourne67335642016-10-24 19:23:39 +0000163 return TargetLoweringObjectFileELF::SelectSectionForGlobal(GO, Kind, TM);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000164}
Jyotsna Verma5eb59802013-05-07 19:53:00 +0000165
Krzysztof Parzyszek5de59102016-04-21 18:56:45 +0000166MCSection *HexagonTargetObjectFile::getExplicitSectionGlobal(
Peter Collingbourne67335642016-10-24 19:23:39 +0000167 const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
168 TRACE("[getExplicitSectionGlobal] GO(" << GO->getName() << ") from("
169 << GO->getSection() << ") ");
170 TRACE((GO->hasPrivateLinkage() ? "private_linkage " : "")
171 << (GO->hasLocalLinkage() ? "local_linkage " : "")
172 << (GO->hasInternalLinkage() ? "internal " : "")
173 << (GO->hasExternalLinkage() ? "external " : "")
174 << (GO->hasCommonLinkage() ? "common_linkage " : "")
175 << (GO->hasCommonLinkage() ? "common " : "" )
Krzysztof Parzyszek5de59102016-04-21 18:56:45 +0000176 << (Kind.isCommon() ? "kind_common " : "" )
177 << (Kind.isBSS() ? "kind_bss " : "" )
178 << (Kind.isBSSLocal() ? "kind_bss_local " : "" ));
179
Peter Collingbourne67335642016-10-24 19:23:39 +0000180 if (GO->hasSection()) {
181 StringRef Section = GO->getSection();
Krzysztof Parzyszek5de59102016-04-21 18:56:45 +0000182 if (Section.find(".access.text.group") != StringRef::npos)
Peter Collingbourne67335642016-10-24 19:23:39 +0000183 return getContext().getELFSection(GO->getSection(), ELF::SHT_PROGBITS,
Krzysztof Parzyszek5de59102016-04-21 18:56:45 +0000184 ELF::SHF_ALLOC | ELF::SHF_EXECINSTR);
185 if (Section.find(".access.data.group") != StringRef::npos)
Peter Collingbourne67335642016-10-24 19:23:39 +0000186 return getContext().getELFSection(GO->getSection(), ELF::SHT_PROGBITS,
Krzysztof Parzyszek5de59102016-04-21 18:56:45 +0000187 ELF::SHF_WRITE | ELF::SHF_ALLOC);
188 }
189
Peter Collingbourne67335642016-10-24 19:23:39 +0000190 if (isGlobalInSmallSection(GO, TM))
191 return selectSmallSectionForGlobal(GO, Kind, TM);
Krzysztof Parzyszek5de59102016-04-21 18:56:45 +0000192
193 // Otherwise, we work the same as ELF.
194 TRACE("default_ELF_section\n");
Peter Collingbourne67335642016-10-24 19:23:39 +0000195 return TargetLoweringObjectFileELF::getExplicitSectionGlobal(GO, Kind, TM);
Krzysztof Parzyszek5de59102016-04-21 18:56:45 +0000196}
197
Krzysztof Parzyszek5de59102016-04-21 18:56:45 +0000198/// Return true if this global value should be placed into small data/bss
199/// section.
Peter Collingbourne67335642016-10-24 19:23:39 +0000200bool HexagonTargetObjectFile::isGlobalInSmallSection(const GlobalObject *GO,
Krzysztof Parzyszek5de59102016-04-21 18:56:45 +0000201 const TargetMachine &TM) const {
Krzysztof Parzyszek977a1fe2018-10-31 15:54:31 +0000202 if (!isSmallDataEnabled(TM)) {
203 LLVM_DEBUG(dbgs() << "Small data is not available.\n");
204 return false;
205 }
206
Krzysztof Parzyszek5de59102016-04-21 18:56:45 +0000207 // Only global variables, not functions.
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000208 LLVM_DEBUG(dbgs() << "Checking if value is in small-data, -G"
209 << SmallDataThreshold << ": \"" << GO->getName() << "\": ");
Peter Collingbourne67335642016-10-24 19:23:39 +0000210 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GO);
Krzysztof Parzyszek5de59102016-04-21 18:56:45 +0000211 if (!GVar) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000212 LLVM_DEBUG(dbgs() << "no, not a global variable\n");
Krzysztof Parzyszek5de59102016-04-21 18:56:45 +0000213 return false;
214 }
215
216 // Globals with external linkage that have an original section set must be
217 // emitted to that section, regardless of whether we would put them into
218 // small data or not. This is how we can support mixing -G0/-G8 in LTO.
219 if (GVar->hasSection()) {
220 bool IsSmall = isSmallDataSection(GVar->getSection());
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000221 LLVM_DEBUG(dbgs() << (IsSmall ? "yes" : "no")
222 << ", has section: " << GVar->getSection() << '\n');
Krzysztof Parzyszek5de59102016-04-21 18:56:45 +0000223 return IsSmall;
224 }
225
226 if (GVar->isConstant()) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000227 LLVM_DEBUG(dbgs() << "no, is a constant\n");
Krzysztof Parzyszek5de59102016-04-21 18:56:45 +0000228 return false;
229 }
230
231 bool IsLocal = GVar->hasLocalLinkage();
232 if (!StaticsInSData && IsLocal) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000233 LLVM_DEBUG(dbgs() << "no, is static\n");
Krzysztof Parzyszek5de59102016-04-21 18:56:45 +0000234 return false;
235 }
236
237 Type *GType = GVar->getType();
238 if (PointerType *PT = dyn_cast<PointerType>(GType))
239 GType = PT->getElementType();
240
241 if (isa<ArrayType>(GType)) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000242 LLVM_DEBUG(dbgs() << "no, is an array\n");
Krzysztof Parzyszek5de59102016-04-21 18:56:45 +0000243 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()) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000252 LLVM_DEBUG(dbgs() << "no, has opaque type\n");
Krzysztof Parzyszek5de59102016-04-21 18:56:45 +0000253 return false;
254 }
255 }
256
257 unsigned Size = GVar->getParent()->getDataLayout().getTypeAllocSize(GType);
258 if (Size == 0) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000259 LLVM_DEBUG(dbgs() << "no, has size 0\n");
Krzysztof Parzyszek5de59102016-04-21 18:56:45 +0000260 return false;
261 }
262 if (Size > SmallDataThreshold) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000263 LLVM_DEBUG(dbgs() << "no, size exceeds sdata threshold: " << Size << '\n');
Krzysztof Parzyszek5de59102016-04-21 18:56:45 +0000264 return false;
265 }
266
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000267 LLVM_DEBUG(dbgs() << "yes\n");
Krzysztof Parzyszek5de59102016-04-21 18:56:45 +0000268 return true;
269}
270
Krzysztof Parzyszek977a1fe2018-10-31 15:54:31 +0000271bool HexagonTargetObjectFile::isSmallDataEnabled(const TargetMachine &TM)
272 const {
273 return SmallDataThreshold > 0 && !TM.isPositionIndependent();
Jyotsna Verma5eb59802013-05-07 19:53:00 +0000274}
275
Krzysztof Parzyszek5de59102016-04-21 18:56:45 +0000276unsigned HexagonTargetObjectFile::getSmallDataSize() const {
277 return SmallDataThreshold;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000278}
279
Sumanth Gundapaneni8c5d5952017-06-30 20:21:48 +0000280bool HexagonTargetObjectFile::shouldPutJumpTableInFunctionSection(
281 bool UsesLabelDifference, const Function &F) const {
282 return EmitJtInText;
283}
284
Krzysztof Parzyszek5de59102016-04-21 18:56:45 +0000285/// Descends any type down to "elementary" components,
286/// discovering the smallest addressable one.
287/// If zero is returned, declaration will not be modified.
288unsigned 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::VectorTyID: {
311 const VectorType *PTy = cast<const VectorType>(Ty);
312 return getSmallestAddressableSize(PTy->getElementType(), GV, TM);
313 }
314 case Type::PointerTyID:
315 case Type::HalfTyID:
316 case Type::FloatTyID:
317 case Type::DoubleTyID:
318 case Type::IntegerTyID: {
319 const DataLayout &DL = GV->getParent()->getDataLayout();
320 // It is unfortunate that DL's function take non-const Type*.
321 return DL.getTypeAllocSize(const_cast<Type*>(Ty));
322 }
323 case Type::FunctionTyID:
324 case Type::VoidTyID:
325 case Type::X86_FP80TyID:
326 case Type::FP128TyID:
327 case Type::PPC_FP128TyID:
328 case Type::LabelTyID:
329 case Type::MetadataTyID:
330 case Type::X86_MMXTyID:
331 case Type::TokenTyID:
332 return 0;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000333 }
334
Krzysztof Parzyszek5de59102016-04-21 18:56:45 +0000335 return 0;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000336}
337
Krzysztof Parzyszek5de59102016-04-21 18:56:45 +0000338MCSection *HexagonTargetObjectFile::selectSmallSectionForGlobal(
Peter Collingbourne67335642016-10-24 19:23:39 +0000339 const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
340 const Type *GTy = GO->getType()->getElementType();
341 unsigned Size = getSmallestAddressableSize(GTy, GO, TM);
Krzysztof Parzyszek5de59102016-04-21 18:56:45 +0000342
343 // If we have -ffunction-section or -fdata-section then we should emit the
344 // global value to a unique section specifically for it... even for sdata.
345 bool EmitUniquedSection = TM.getDataSections();
346
347 TRACE("Small data. Size(" << Size << ")");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000348 // Handle Small Section classification here.
Krzysztof Parzyszek5de59102016-04-21 18:56:45 +0000349 if (Kind.isBSS() || Kind.isBSSLocal()) {
350 // If -mno-sort-sda is not set, find out smallest accessible entity in
351 // declaration and add it to the section name string.
352 // Note. It does not track the actual usage of the value, only its de-
353 // claration. Also, compiler adds explicit pad fields to some struct
354 // declarations - they are currently counted towards smallest addres-
355 // sable entity.
356 if (NoSmallDataSorting) {
357 TRACE(" default sbss\n");
358 return SmallBSSSection;
359 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000360
Krzysztof Parzyszek5de59102016-04-21 18:56:45 +0000361 StringRef Prefix(".sbss");
362 SmallString<128> Name(Prefix);
363 Name.append(getSectionSuffixForSize(Size));
364
365 if (EmitUniquedSection) {
366 Name.append(".");
Peter Collingbourne67335642016-10-24 19:23:39 +0000367 Name.append(GO->getName());
Krzysztof Parzyszek5de59102016-04-21 18:56:45 +0000368 }
369 TRACE(" unique sbss(" << Name << ")\n");
370 return getContext().getELFSection(Name.str(), ELF::SHT_NOBITS,
371 ELF::SHF_WRITE | ELF::SHF_ALLOC | ELF::SHF_HEX_GPREL);
372 }
373
374 if (Kind.isCommon()) {
375 // This is purely for LTO+Linker Script because commons don't really have a
376 // section. However, the BitcodeSectionWriter pass will query for the
377 // sections of commons (and the linker expects us to know their section) so
378 // we'll return one here.
379 if (NoSmallDataSorting)
380 return BSSSection;
381
382 Twine Name = Twine(".scommon") + getSectionSuffixForSize(Size);
383 TRACE(" small COMMON (" << Name << ")\n");
384
385 return getContext().getELFSection(Name.str(), ELF::SHT_NOBITS,
386 ELF::SHF_WRITE | ELF::SHF_ALLOC |
387 ELF::SHF_HEX_GPREL);
388 }
389
390 // We could have changed sdata object to a constant... in this
391 // case the Kind could be wrong for it.
392 if (Kind.isMergeableConst()) {
393 TRACE(" const_object_as_data ");
Peter Collingbourne67335642016-10-24 19:23:39 +0000394 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GO);
Krzysztof Parzyszek5de59102016-04-21 18:56:45 +0000395 if (GVar->hasSection() && isSmallDataSection(GVar->getSection()))
396 Kind = SectionKind::getData();
397 }
398
399 if (Kind.isData()) {
400 if (NoSmallDataSorting) {
401 TRACE(" default sdata\n");
402 return SmallDataSection;
403 }
404
405 StringRef Prefix(".sdata");
406 SmallString<128> Name(Prefix);
407 Name.append(getSectionSuffixForSize(Size));
408
409 if (EmitUniquedSection) {
410 Name.append(".");
Peter Collingbourne67335642016-10-24 19:23:39 +0000411 Name.append(GO->getName());
Krzysztof Parzyszek5de59102016-04-21 18:56:45 +0000412 }
413 TRACE(" unique sdata(" << Name << ")\n");
414 return getContext().getELFSection(Name.str(), ELF::SHT_PROGBITS,
415 ELF::SHF_WRITE | ELF::SHF_ALLOC | ELF::SHF_HEX_GPREL);
416 }
417
418 TRACE("default ELF section\n");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000419 // Otherwise, we work the same as ELF.
Peter Collingbourne67335642016-10-24 19:23:39 +0000420 return TargetLoweringObjectFileELF::SelectSectionForGlobal(GO, Kind, TM);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000421}
Sumanth Gundapanenid5aa0f32017-07-18 15:31:37 +0000422
423// Return the function that uses the lookup table. If there are more
424// than one live function that uses this look table, bail out and place
425// the lookup table in default section.
426const Function *
427HexagonTargetObjectFile::getLutUsedFunction(const GlobalObject *GO) const {
428 const Function *ReturnFn = nullptr;
429 for (auto U : GO->users()) {
430 // validate each instance of user to be a live function.
431 auto *I = dyn_cast<Instruction>(U);
432 if (!I)
433 continue;
434 auto *Bb = I->getParent();
435 if (!Bb)
436 continue;
437 auto *UserFn = Bb->getParent();
438 if (!ReturnFn)
439 ReturnFn = UserFn;
440 else if (ReturnFn != UserFn)
441 return nullptr;
442 }
443 return ReturnFn;
444}
445
446MCSection *HexagonTargetObjectFile::selectSectionForLookupTable(
447 const GlobalObject *GO, const TargetMachine &TM, const Function *Fn) const {
448
449 SectionKind Kind = SectionKind::getText();
450 // If the function has explicit section, place the lookup table in this
451 // explicit section.
452 if (Fn->hasSection())
453 return getExplicitSectionGlobal(Fn, Kind, TM);
454
455 const auto *FuncObj = dyn_cast<GlobalObject>(Fn);
456 return SelectSectionForGlobal(FuncObj, Kind, TM);
457}