13ca95b02SDimitry Andric //===-- HexagonTargetObjectFile.cpp ---------------------------------------===//
2dff0c46cSDimitry Andric //
3dff0c46cSDimitry Andric // The LLVM Compiler Infrastructure
4dff0c46cSDimitry Andric //
5dff0c46cSDimitry Andric // This file is distributed under the University of Illinois Open Source
6dff0c46cSDimitry Andric // License. See LICENSE.TXT for details.
7dff0c46cSDimitry Andric //
8dff0c46cSDimitry Andric //===----------------------------------------------------------------------===//
9dff0c46cSDimitry Andric //
10dff0c46cSDimitry Andric // This file contains the declarations of the HexagonTargetAsmInfo properties.
11dff0c46cSDimitry Andric //
12dff0c46cSDimitry Andric //===----------------------------------------------------------------------===//
138e0f8b8cSDimitry Andric
143ca95b02SDimitry Andric #define DEBUG_TYPE "hexagon-sdata"
15dff0c46cSDimitry Andric
163ca95b02SDimitry Andric #include "HexagonTargetObjectFile.h"
178e0f8b8cSDimitry Andric #include "llvm/ADT/SmallString.h"
188e0f8b8cSDimitry Andric #include "llvm/ADT/StringRef.h"
198e0f8b8cSDimitry Andric #include "llvm/ADT/Twine.h"
20db17bf38SDimitry Andric #include "llvm/BinaryFormat/ELF.h"
21139f7f9bSDimitry Andric #include "llvm/IR/DataLayout.h"
22139f7f9bSDimitry Andric #include "llvm/IR/DerivedTypes.h"
238e0f8b8cSDimitry Andric #include "llvm/IR/GlobalObject.h"
248e0f8b8cSDimitry Andric #include "llvm/IR/GlobalValue.h"
25139f7f9bSDimitry Andric #include "llvm/IR/GlobalVariable.h"
268e0f8b8cSDimitry Andric #include "llvm/IR/Type.h"
27dff0c46cSDimitry Andric #include "llvm/MC/MCContext.h"
288e0f8b8cSDimitry Andric #include "llvm/MC/SectionKind.h"
298e0f8b8cSDimitry Andric #include "llvm/Support/Casting.h"
30dff0c46cSDimitry Andric #include "llvm/Support/CommandLine.h"
318e0f8b8cSDimitry Andric #include "llvm/Support/Debug.h"
328e0f8b8cSDimitry Andric #include "llvm/Support/raw_ostream.h"
338e0f8b8cSDimitry Andric #include "llvm/Target/TargetMachine.h"
34dff0c46cSDimitry Andric
35dff0c46cSDimitry Andric using namespace llvm;
36dff0c46cSDimitry Andric
373ca95b02SDimitry Andric static cl::opt<unsigned> SmallDataThreshold("hexagon-small-data-threshold",
38f785676fSDimitry Andric cl::init(8), cl::Hidden,
39f785676fSDimitry Andric cl::desc("The maximum size of an object in the sdata section"));
40dff0c46cSDimitry Andric
413ca95b02SDimitry Andric static cl::opt<bool> NoSmallDataSorting("mno-sort-sda", cl::init(false),
423ca95b02SDimitry Andric cl::Hidden, cl::desc("Disable small data sections sorting"));
433ca95b02SDimitry Andric
443ca95b02SDimitry Andric static cl::opt<bool> StaticsInSData("hexagon-statics-in-small-data",
453ca95b02SDimitry Andric cl::init(false), cl::Hidden, cl::ZeroOrMore,
463ca95b02SDimitry Andric cl::desc("Allow static variables in .sdata"));
473ca95b02SDimitry Andric
483ca95b02SDimitry Andric static cl::opt<bool> TraceGVPlacement("trace-gv-placement",
493ca95b02SDimitry Andric cl::Hidden, cl::init(false),
503ca95b02SDimitry Andric cl::desc("Trace global value placement"));
513ca95b02SDimitry Andric
52a580b014SDimitry Andric static cl::opt<bool>
53a580b014SDimitry Andric EmitJtInText("hexagon-emit-jt-text", cl::Hidden, cl::init(false),
54a580b014SDimitry Andric cl::desc("Emit hexagon jump tables in function section"));
55a580b014SDimitry Andric
56b40b48b8SDimitry Andric static cl::opt<bool>
57b40b48b8SDimitry Andric EmitLutInText("hexagon-emit-lut-text", cl::Hidden, cl::init(false),
58b40b48b8SDimitry Andric cl::desc("Emit hexagon lookup tables in function section"));
59b40b48b8SDimitry Andric
603ca95b02SDimitry Andric // TraceGVPlacement controls messages for all builds. For builds with assertions
613ca95b02SDimitry Andric // (debug or release), messages are also controlled by the usual debug flags
623ca95b02SDimitry Andric // (e.g. -debug and -debug-only=globallayout)
633ca95b02SDimitry Andric #define TRACE_TO(s, X) s << X
643ca95b02SDimitry Andric #ifdef NDEBUG
658e0f8b8cSDimitry Andric #define TRACE(X) \
668e0f8b8cSDimitry Andric do { \
678e0f8b8cSDimitry Andric if (TraceGVPlacement) { \
688e0f8b8cSDimitry Andric TRACE_TO(errs(), X); \
698e0f8b8cSDimitry Andric } \
708e0f8b8cSDimitry Andric } while (false)
713ca95b02SDimitry Andric #else
723ca95b02SDimitry Andric #define TRACE(X) \
733ca95b02SDimitry Andric do { \
748e0f8b8cSDimitry Andric if (TraceGVPlacement) { \
758e0f8b8cSDimitry Andric TRACE_TO(errs(), X); \
768e0f8b8cSDimitry Andric } else { \
774ba319b5SDimitry Andric LLVM_DEBUG(TRACE_TO(dbgs(), X)); \
788e0f8b8cSDimitry Andric } \
798e0f8b8cSDimitry Andric } while (false)
803ca95b02SDimitry Andric #endif
813ca95b02SDimitry Andric
823ca95b02SDimitry Andric // Returns true if the section name is such that the symbol will be put
833ca95b02SDimitry Andric // in a small data section.
843ca95b02SDimitry Andric // For instance, global variables with section attributes such as ".sdata"
853ca95b02SDimitry Andric // ".sdata.*", ".sbss", and ".sbss.*" will go into small data.
isSmallDataSection(StringRef Sec)863ca95b02SDimitry Andric static bool isSmallDataSection(StringRef Sec) {
873ca95b02SDimitry Andric // sectionName is either ".sdata" or ".sbss". Looking for an exact match
883ca95b02SDimitry Andric // obviates the need for checks for section names such as ".sdatafoo".
893ca95b02SDimitry Andric if (Sec.equals(".sdata") || Sec.equals(".sbss") || Sec.equals(".scommon"))
903ca95b02SDimitry Andric return true;
913ca95b02SDimitry Andric // If either ".sdata." or ".sbss." is a substring of the section name
923ca95b02SDimitry Andric // then put the symbol in small data.
933ca95b02SDimitry Andric return Sec.find(".sdata.") != StringRef::npos ||
943ca95b02SDimitry Andric Sec.find(".sbss.") != StringRef::npos ||
953ca95b02SDimitry Andric Sec.find(".scommon.") != StringRef::npos;
963ca95b02SDimitry Andric }
973ca95b02SDimitry Andric
getSectionSuffixForSize(unsigned Size)983ca95b02SDimitry Andric static const char *getSectionSuffixForSize(unsigned Size) {
993ca95b02SDimitry Andric switch (Size) {
1003ca95b02SDimitry Andric default:
1013ca95b02SDimitry Andric return "";
1023ca95b02SDimitry Andric case 1:
1033ca95b02SDimitry Andric return ".1";
1043ca95b02SDimitry Andric case 2:
1053ca95b02SDimitry Andric return ".2";
1063ca95b02SDimitry Andric case 4:
1073ca95b02SDimitry Andric return ".4";
1083ca95b02SDimitry Andric case 8:
1093ca95b02SDimitry Andric return ".8";
1103ca95b02SDimitry Andric }
1113ca95b02SDimitry Andric }
1123ca95b02SDimitry Andric
Initialize(MCContext & Ctx,const TargetMachine & TM)113dff0c46cSDimitry Andric void HexagonTargetObjectFile::Initialize(MCContext &Ctx,
114dff0c46cSDimitry Andric const TargetMachine &TM) {
115dff0c46cSDimitry Andric TargetLoweringObjectFileELF::Initialize(Ctx, TM);
11639d628a0SDimitry Andric InitializeELF(TM.Options.UseInitArray);
117dff0c46cSDimitry Andric
1183ca95b02SDimitry Andric SmallDataSection =
1193ca95b02SDimitry Andric getContext().getELFSection(".sdata", ELF::SHT_PROGBITS,
1203ca95b02SDimitry Andric ELF::SHF_WRITE | ELF::SHF_ALLOC |
1213ca95b02SDimitry Andric ELF::SHF_HEX_GPREL);
1223ca95b02SDimitry Andric SmallBSSSection =
1233ca95b02SDimitry Andric getContext().getELFSection(".sbss", ELF::SHT_NOBITS,
1243ca95b02SDimitry Andric ELF::SHF_WRITE | ELF::SHF_ALLOC |
1253ca95b02SDimitry Andric ELF::SHF_HEX_GPREL);
1263ca95b02SDimitry Andric }
1273ca95b02SDimitry Andric
SelectSectionForGlobal(const GlobalObject * GO,SectionKind Kind,const TargetMachine & TM) const1283ca95b02SDimitry Andric MCSection *HexagonTargetObjectFile::SelectSectionForGlobal(
129d88c1a5aSDimitry Andric const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
130d88c1a5aSDimitry Andric TRACE("[SelectSectionForGlobal] GO(" << GO->getName() << ") ");
131d88c1a5aSDimitry Andric TRACE("input section(" << GO->getSection() << ") ");
1323ca95b02SDimitry Andric
133d88c1a5aSDimitry Andric TRACE((GO->hasPrivateLinkage() ? "private_linkage " : "")
134d88c1a5aSDimitry Andric << (GO->hasLocalLinkage() ? "local_linkage " : "")
135d88c1a5aSDimitry Andric << (GO->hasInternalLinkage() ? "internal " : "")
136d88c1a5aSDimitry Andric << (GO->hasExternalLinkage() ? "external " : "")
137d88c1a5aSDimitry Andric << (GO->hasCommonLinkage() ? "common_linkage " : "")
138d88c1a5aSDimitry Andric << (GO->hasCommonLinkage() ? "common " : "" )
1393ca95b02SDimitry Andric << (Kind.isCommon() ? "kind_common " : "" )
1403ca95b02SDimitry Andric << (Kind.isBSS() ? "kind_bss " : "" )
1413ca95b02SDimitry Andric << (Kind.isBSSLocal() ? "kind_bss_local " : "" ));
1423ca95b02SDimitry Andric
143b40b48b8SDimitry Andric // If the lookup table is used by more than one function, do not place
144b40b48b8SDimitry Andric // it in text section.
145b40b48b8SDimitry Andric if (EmitLutInText && GO->getName().startswith("switch.table")) {
146b40b48b8SDimitry Andric if (const Function *Fn = getLutUsedFunction(GO))
147b40b48b8SDimitry Andric return selectSectionForLookupTable(GO, TM, Fn);
148b40b48b8SDimitry Andric }
149b40b48b8SDimitry Andric
150d88c1a5aSDimitry Andric if (isGlobalInSmallSection(GO, TM))
151d88c1a5aSDimitry Andric return selectSmallSectionForGlobal(GO, Kind, TM);
1523ca95b02SDimitry Andric
1533ca95b02SDimitry Andric if (Kind.isCommon()) {
1543ca95b02SDimitry Andric // This is purely for LTO+Linker Script because commons don't really have a
1553ca95b02SDimitry Andric // section. However, the BitcodeSectionWriter pass will query for the
1563ca95b02SDimitry Andric // sections of commons (and the linker expects us to know their section) so
1573ca95b02SDimitry Andric // we'll return one here.
1583ca95b02SDimitry Andric return BSSSection;
1593ca95b02SDimitry Andric }
1603ca95b02SDimitry Andric
1613ca95b02SDimitry Andric TRACE("default_ELF_section\n");
1623ca95b02SDimitry Andric // Otherwise, we work the same as ELF.
163d88c1a5aSDimitry Andric return TargetLoweringObjectFileELF::SelectSectionForGlobal(GO, Kind, TM);
1643ca95b02SDimitry Andric }
1653ca95b02SDimitry Andric
getExplicitSectionGlobal(const GlobalObject * GO,SectionKind Kind,const TargetMachine & TM) const1663ca95b02SDimitry Andric MCSection *HexagonTargetObjectFile::getExplicitSectionGlobal(
167d88c1a5aSDimitry Andric const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
168d88c1a5aSDimitry Andric TRACE("[getExplicitSectionGlobal] GO(" << GO->getName() << ") from("
169d88c1a5aSDimitry Andric << GO->getSection() << ") ");
170d88c1a5aSDimitry Andric TRACE((GO->hasPrivateLinkage() ? "private_linkage " : "")
171d88c1a5aSDimitry Andric << (GO->hasLocalLinkage() ? "local_linkage " : "")
172d88c1a5aSDimitry Andric << (GO->hasInternalLinkage() ? "internal " : "")
173d88c1a5aSDimitry Andric << (GO->hasExternalLinkage() ? "external " : "")
174d88c1a5aSDimitry Andric << (GO->hasCommonLinkage() ? "common_linkage " : "")
175d88c1a5aSDimitry Andric << (GO->hasCommonLinkage() ? "common " : "" )
1763ca95b02SDimitry Andric << (Kind.isCommon() ? "kind_common " : "" )
1773ca95b02SDimitry Andric << (Kind.isBSS() ? "kind_bss " : "" )
1783ca95b02SDimitry Andric << (Kind.isBSSLocal() ? "kind_bss_local " : "" ));
1793ca95b02SDimitry Andric
180d88c1a5aSDimitry Andric if (GO->hasSection()) {
181d88c1a5aSDimitry Andric StringRef Section = GO->getSection();
1823ca95b02SDimitry Andric if (Section.find(".access.text.group") != StringRef::npos)
183d88c1a5aSDimitry Andric return getContext().getELFSection(GO->getSection(), ELF::SHT_PROGBITS,
1843ca95b02SDimitry Andric ELF::SHF_ALLOC | ELF::SHF_EXECINSTR);
1853ca95b02SDimitry Andric if (Section.find(".access.data.group") != StringRef::npos)
186d88c1a5aSDimitry Andric return getContext().getELFSection(GO->getSection(), ELF::SHT_PROGBITS,
187ff0cc061SDimitry Andric ELF::SHF_WRITE | ELF::SHF_ALLOC);
188dff0c46cSDimitry Andric }
189dff0c46cSDimitry Andric
190d88c1a5aSDimitry Andric if (isGlobalInSmallSection(GO, TM))
191d88c1a5aSDimitry Andric return selectSmallSectionForGlobal(GO, Kind, TM);
1923ca95b02SDimitry Andric
1933ca95b02SDimitry Andric // Otherwise, we work the same as ELF.
1943ca95b02SDimitry Andric TRACE("default_ELF_section\n");
195d88c1a5aSDimitry Andric return TargetLoweringObjectFileELF::getExplicitSectionGlobal(GO, Kind, TM);
196dff0c46cSDimitry Andric }
197f785676fSDimitry Andric
1983ca95b02SDimitry Andric /// Return true if this global value should be placed into small data/bss
1993ca95b02SDimitry Andric /// section.
isGlobalInSmallSection(const GlobalObject * GO,const TargetMachine & TM) const200d88c1a5aSDimitry Andric bool HexagonTargetObjectFile::isGlobalInSmallSection(const GlobalObject *GO,
2013ca95b02SDimitry Andric const TargetMachine &TM) const {
202*b5893f02SDimitry Andric bool HaveSData = isSmallDataEnabled(TM);
203*b5893f02SDimitry Andric if (!HaveSData)
204*b5893f02SDimitry Andric LLVM_DEBUG(dbgs() << "Small-data allocation is disabled, but symbols "
205*b5893f02SDimitry Andric "may have explicit section assignments...\n");
2063ca95b02SDimitry Andric // Only global variables, not functions.
2074ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << "Checking if value is in small-data, -G"
208d88c1a5aSDimitry Andric << SmallDataThreshold << ": \"" << GO->getName() << "\": ");
209d88c1a5aSDimitry Andric const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GO);
2103ca95b02SDimitry Andric if (!GVar) {
2114ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << "no, not a global variable\n");
2123ca95b02SDimitry Andric return false;
2133ca95b02SDimitry Andric }
2143ca95b02SDimitry Andric
2153ca95b02SDimitry Andric // Globals with external linkage that have an original section set must be
2163ca95b02SDimitry Andric // emitted to that section, regardless of whether we would put them into
2173ca95b02SDimitry Andric // small data or not. This is how we can support mixing -G0/-G8 in LTO.
2183ca95b02SDimitry Andric if (GVar->hasSection()) {
2193ca95b02SDimitry Andric bool IsSmall = isSmallDataSection(GVar->getSection());
2204ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << (IsSmall ? "yes" : "no")
2214ba319b5SDimitry Andric << ", has section: " << GVar->getSection() << '\n');
2223ca95b02SDimitry Andric return IsSmall;
2233ca95b02SDimitry Andric }
2243ca95b02SDimitry Andric
225*b5893f02SDimitry Andric // If sdata is disabled, stop the checks here.
226*b5893f02SDimitry Andric if (!HaveSData) {
227*b5893f02SDimitry Andric LLVM_DEBUG(dbgs() << "no, small-data allocation is disabled\n");
228*b5893f02SDimitry Andric return false;
229*b5893f02SDimitry Andric }
230*b5893f02SDimitry Andric
2313ca95b02SDimitry Andric if (GVar->isConstant()) {
2324ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << "no, is a constant\n");
2333ca95b02SDimitry Andric return false;
2343ca95b02SDimitry Andric }
2353ca95b02SDimitry Andric
2363ca95b02SDimitry Andric bool IsLocal = GVar->hasLocalLinkage();
2373ca95b02SDimitry Andric if (!StaticsInSData && IsLocal) {
2384ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << "no, is static\n");
2393ca95b02SDimitry Andric return false;
2403ca95b02SDimitry Andric }
2413ca95b02SDimitry Andric
2423ca95b02SDimitry Andric Type *GType = GVar->getType();
2433ca95b02SDimitry Andric if (PointerType *PT = dyn_cast<PointerType>(GType))
2443ca95b02SDimitry Andric GType = PT->getElementType();
2453ca95b02SDimitry Andric
2463ca95b02SDimitry Andric if (isa<ArrayType>(GType)) {
2474ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << "no, is an array\n");
2483ca95b02SDimitry Andric return false;
2493ca95b02SDimitry Andric }
2503ca95b02SDimitry Andric
2513ca95b02SDimitry Andric // If the type is a struct with no body provided, treat is conservatively.
2523ca95b02SDimitry Andric // There cannot be actual definitions of object of such a type in this CU
2533ca95b02SDimitry Andric // (only references), so assuming that they are not in sdata is safe. If
2543ca95b02SDimitry Andric // these objects end up in the sdata, the references will still be valid.
2553ca95b02SDimitry Andric if (StructType *ST = dyn_cast<StructType>(GType)) {
2563ca95b02SDimitry Andric if (ST->isOpaque()) {
2574ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << "no, has opaque type\n");
2583ca95b02SDimitry Andric return false;
2593ca95b02SDimitry Andric }
2603ca95b02SDimitry Andric }
2613ca95b02SDimitry Andric
2623ca95b02SDimitry Andric unsigned Size = GVar->getParent()->getDataLayout().getTypeAllocSize(GType);
2633ca95b02SDimitry Andric if (Size == 0) {
2644ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << "no, has size 0\n");
2653ca95b02SDimitry Andric return false;
2663ca95b02SDimitry Andric }
2673ca95b02SDimitry Andric if (Size > SmallDataThreshold) {
2684ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << "no, size exceeds sdata threshold: " << Size << '\n');
2693ca95b02SDimitry Andric return false;
2703ca95b02SDimitry Andric }
2713ca95b02SDimitry Andric
2724ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << "yes\n");
2733ca95b02SDimitry Andric return true;
2743ca95b02SDimitry Andric }
2753ca95b02SDimitry Andric
isSmallDataEnabled(const TargetMachine & TM) const276*b5893f02SDimitry Andric bool HexagonTargetObjectFile::isSmallDataEnabled(const TargetMachine &TM)
277*b5893f02SDimitry Andric const {
278*b5893f02SDimitry Andric return SmallDataThreshold > 0 && !TM.isPositionIndependent();
279f785676fSDimitry Andric }
280f785676fSDimitry Andric
getSmallDataSize() const2813ca95b02SDimitry Andric unsigned HexagonTargetObjectFile::getSmallDataSize() const {
2823ca95b02SDimitry Andric return SmallDataThreshold;
2833ca95b02SDimitry Andric }
2843ca95b02SDimitry Andric
shouldPutJumpTableInFunctionSection(bool UsesLabelDifference,const Function & F) const285a580b014SDimitry Andric bool HexagonTargetObjectFile::shouldPutJumpTableInFunctionSection(
286a580b014SDimitry Andric bool UsesLabelDifference, const Function &F) const {
287a580b014SDimitry Andric return EmitJtInText;
288a580b014SDimitry Andric }
289a580b014SDimitry Andric
2903ca95b02SDimitry Andric /// Descends any type down to "elementary" components,
2913ca95b02SDimitry Andric /// discovering the smallest addressable one.
2923ca95b02SDimitry Andric /// If zero is returned, declaration will not be modified.
getSmallestAddressableSize(const Type * Ty,const GlobalValue * GV,const TargetMachine & TM) const2933ca95b02SDimitry Andric unsigned HexagonTargetObjectFile::getSmallestAddressableSize(const Type *Ty,
2943ca95b02SDimitry Andric const GlobalValue *GV, const TargetMachine &TM) const {
2953ca95b02SDimitry Andric // Assign the smallest element access size to the highest
2963ca95b02SDimitry Andric // value which assembler can handle.
2973ca95b02SDimitry Andric unsigned SmallestElement = 8;
2983ca95b02SDimitry Andric
2993ca95b02SDimitry Andric if (!Ty)
3003ca95b02SDimitry Andric return 0;
3013ca95b02SDimitry Andric switch (Ty->getTypeID()) {
3023ca95b02SDimitry Andric case Type::StructTyID: {
3033ca95b02SDimitry Andric const StructType *STy = cast<const StructType>(Ty);
3043ca95b02SDimitry Andric for (auto &E : STy->elements()) {
3053ca95b02SDimitry Andric unsigned AtomicSize = getSmallestAddressableSize(E, GV, TM);
3063ca95b02SDimitry Andric if (AtomicSize < SmallestElement)
3073ca95b02SDimitry Andric SmallestElement = AtomicSize;
3083ca95b02SDimitry Andric }
3093ca95b02SDimitry Andric return (STy->getNumElements() == 0) ? 0 : SmallestElement;
3103ca95b02SDimitry Andric }
3113ca95b02SDimitry Andric case Type::ArrayTyID: {
3123ca95b02SDimitry Andric const ArrayType *ATy = cast<const ArrayType>(Ty);
3133ca95b02SDimitry Andric return getSmallestAddressableSize(ATy->getElementType(), GV, TM);
3143ca95b02SDimitry Andric }
3153ca95b02SDimitry Andric case Type::VectorTyID: {
3163ca95b02SDimitry Andric const VectorType *PTy = cast<const VectorType>(Ty);
3173ca95b02SDimitry Andric return getSmallestAddressableSize(PTy->getElementType(), GV, TM);
3183ca95b02SDimitry Andric }
3193ca95b02SDimitry Andric case Type::PointerTyID:
3203ca95b02SDimitry Andric case Type::HalfTyID:
3213ca95b02SDimitry Andric case Type::FloatTyID:
3223ca95b02SDimitry Andric case Type::DoubleTyID:
3233ca95b02SDimitry Andric case Type::IntegerTyID: {
3243ca95b02SDimitry Andric const DataLayout &DL = GV->getParent()->getDataLayout();
3253ca95b02SDimitry Andric // It is unfortunate that DL's function take non-const Type*.
3263ca95b02SDimitry Andric return DL.getTypeAllocSize(const_cast<Type*>(Ty));
3273ca95b02SDimitry Andric }
3283ca95b02SDimitry Andric case Type::FunctionTyID:
3293ca95b02SDimitry Andric case Type::VoidTyID:
3303ca95b02SDimitry Andric case Type::X86_FP80TyID:
3313ca95b02SDimitry Andric case Type::FP128TyID:
3323ca95b02SDimitry Andric case Type::PPC_FP128TyID:
3333ca95b02SDimitry Andric case Type::LabelTyID:
3343ca95b02SDimitry Andric case Type::MetadataTyID:
3353ca95b02SDimitry Andric case Type::X86_MMXTyID:
3363ca95b02SDimitry Andric case Type::TokenTyID:
3373ca95b02SDimitry Andric return 0;
3383ca95b02SDimitry Andric }
3393ca95b02SDimitry Andric
3403ca95b02SDimitry Andric return 0;
3413ca95b02SDimitry Andric }
3423ca95b02SDimitry Andric
selectSmallSectionForGlobal(const GlobalObject * GO,SectionKind Kind,const TargetMachine & TM) const3433ca95b02SDimitry Andric MCSection *HexagonTargetObjectFile::selectSmallSectionForGlobal(
344d88c1a5aSDimitry Andric const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
345d88c1a5aSDimitry Andric const Type *GTy = GO->getType()->getElementType();
346d88c1a5aSDimitry Andric unsigned Size = getSmallestAddressableSize(GTy, GO, TM);
347dff0c46cSDimitry Andric
3483ca95b02SDimitry Andric // If we have -ffunction-section or -fdata-section then we should emit the
3493ca95b02SDimitry Andric // global value to a unique section specifically for it... even for sdata.
3503ca95b02SDimitry Andric bool EmitUniquedSection = TM.getDataSections();
351dff0c46cSDimitry Andric
3523ca95b02SDimitry Andric TRACE("Small data. Size(" << Size << ")");
353dff0c46cSDimitry Andric // Handle Small Section classification here.
3543ca95b02SDimitry Andric if (Kind.isBSS() || Kind.isBSSLocal()) {
3553ca95b02SDimitry Andric // If -mno-sort-sda is not set, find out smallest accessible entity in
3563ca95b02SDimitry Andric // declaration and add it to the section name string.
3573ca95b02SDimitry Andric // Note. It does not track the actual usage of the value, only its de-
3583ca95b02SDimitry Andric // claration. Also, compiler adds explicit pad fields to some struct
3593ca95b02SDimitry Andric // declarations - they are currently counted towards smallest addres-
3603ca95b02SDimitry Andric // sable entity.
3613ca95b02SDimitry Andric if (NoSmallDataSorting) {
3623ca95b02SDimitry Andric TRACE(" default sbss\n");
363dff0c46cSDimitry Andric return SmallBSSSection;
3643ca95b02SDimitry Andric }
365dff0c46cSDimitry Andric
3663ca95b02SDimitry Andric StringRef Prefix(".sbss");
3673ca95b02SDimitry Andric SmallString<128> Name(Prefix);
3683ca95b02SDimitry Andric Name.append(getSectionSuffixForSize(Size));
3693ca95b02SDimitry Andric
3703ca95b02SDimitry Andric if (EmitUniquedSection) {
3713ca95b02SDimitry Andric Name.append(".");
372d88c1a5aSDimitry Andric Name.append(GO->getName());
3733ca95b02SDimitry Andric }
3743ca95b02SDimitry Andric TRACE(" unique sbss(" << Name << ")\n");
3753ca95b02SDimitry Andric return getContext().getELFSection(Name.str(), ELF::SHT_NOBITS,
3763ca95b02SDimitry Andric ELF::SHF_WRITE | ELF::SHF_ALLOC | ELF::SHF_HEX_GPREL);
3773ca95b02SDimitry Andric }
3783ca95b02SDimitry Andric
3793ca95b02SDimitry Andric if (Kind.isCommon()) {
3803ca95b02SDimitry Andric // This is purely for LTO+Linker Script because commons don't really have a
3813ca95b02SDimitry Andric // section. However, the BitcodeSectionWriter pass will query for the
3823ca95b02SDimitry Andric // sections of commons (and the linker expects us to know their section) so
3833ca95b02SDimitry Andric // we'll return one here.
3843ca95b02SDimitry Andric if (NoSmallDataSorting)
3853ca95b02SDimitry Andric return BSSSection;
3863ca95b02SDimitry Andric
3873ca95b02SDimitry Andric Twine Name = Twine(".scommon") + getSectionSuffixForSize(Size);
3883ca95b02SDimitry Andric TRACE(" small COMMON (" << Name << ")\n");
3893ca95b02SDimitry Andric
3903ca95b02SDimitry Andric return getContext().getELFSection(Name.str(), ELF::SHT_NOBITS,
3913ca95b02SDimitry Andric ELF::SHF_WRITE | ELF::SHF_ALLOC |
3923ca95b02SDimitry Andric ELF::SHF_HEX_GPREL);
3933ca95b02SDimitry Andric }
3943ca95b02SDimitry Andric
3953ca95b02SDimitry Andric // We could have changed sdata object to a constant... in this
3963ca95b02SDimitry Andric // case the Kind could be wrong for it.
3973ca95b02SDimitry Andric if (Kind.isMergeableConst()) {
3983ca95b02SDimitry Andric TRACE(" const_object_as_data ");
399d88c1a5aSDimitry Andric const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GO);
4003ca95b02SDimitry Andric if (GVar->hasSection() && isSmallDataSection(GVar->getSection()))
4013ca95b02SDimitry Andric Kind = SectionKind::getData();
4023ca95b02SDimitry Andric }
4033ca95b02SDimitry Andric
4043ca95b02SDimitry Andric if (Kind.isData()) {
4053ca95b02SDimitry Andric if (NoSmallDataSorting) {
4063ca95b02SDimitry Andric TRACE(" default sdata\n");
4073ca95b02SDimitry Andric return SmallDataSection;
4083ca95b02SDimitry Andric }
4093ca95b02SDimitry Andric
4103ca95b02SDimitry Andric StringRef Prefix(".sdata");
4113ca95b02SDimitry Andric SmallString<128> Name(Prefix);
4123ca95b02SDimitry Andric Name.append(getSectionSuffixForSize(Size));
4133ca95b02SDimitry Andric
4143ca95b02SDimitry Andric if (EmitUniquedSection) {
4153ca95b02SDimitry Andric Name.append(".");
416d88c1a5aSDimitry Andric Name.append(GO->getName());
4173ca95b02SDimitry Andric }
4183ca95b02SDimitry Andric TRACE(" unique sdata(" << Name << ")\n");
4193ca95b02SDimitry Andric return getContext().getELFSection(Name.str(), ELF::SHT_PROGBITS,
4203ca95b02SDimitry Andric ELF::SHF_WRITE | ELF::SHF_ALLOC | ELF::SHF_HEX_GPREL);
4213ca95b02SDimitry Andric }
4223ca95b02SDimitry Andric
4233ca95b02SDimitry Andric TRACE("default ELF section\n");
424dff0c46cSDimitry Andric // Otherwise, we work the same as ELF.
425d88c1a5aSDimitry Andric return TargetLoweringObjectFileELF::SelectSectionForGlobal(GO, Kind, TM);
426dff0c46cSDimitry Andric }
427b40b48b8SDimitry Andric
428b40b48b8SDimitry Andric // Return the function that uses the lookup table. If there are more
429b40b48b8SDimitry Andric // than one live function that uses this look table, bail out and place
430b40b48b8SDimitry Andric // the lookup table in default section.
431b40b48b8SDimitry Andric const Function *
getLutUsedFunction(const GlobalObject * GO) const432b40b48b8SDimitry Andric HexagonTargetObjectFile::getLutUsedFunction(const GlobalObject *GO) const {
433b40b48b8SDimitry Andric const Function *ReturnFn = nullptr;
434b40b48b8SDimitry Andric for (auto U : GO->users()) {
435b40b48b8SDimitry Andric // validate each instance of user to be a live function.
436b40b48b8SDimitry Andric auto *I = dyn_cast<Instruction>(U);
437b40b48b8SDimitry Andric if (!I)
438b40b48b8SDimitry Andric continue;
439b40b48b8SDimitry Andric auto *Bb = I->getParent();
440b40b48b8SDimitry Andric if (!Bb)
441b40b48b8SDimitry Andric continue;
442b40b48b8SDimitry Andric auto *UserFn = Bb->getParent();
443b40b48b8SDimitry Andric if (!ReturnFn)
444b40b48b8SDimitry Andric ReturnFn = UserFn;
445b40b48b8SDimitry Andric else if (ReturnFn != UserFn)
446b40b48b8SDimitry Andric return nullptr;
447b40b48b8SDimitry Andric }
448b40b48b8SDimitry Andric return ReturnFn;
449b40b48b8SDimitry Andric }
450b40b48b8SDimitry Andric
selectSectionForLookupTable(const GlobalObject * GO,const TargetMachine & TM,const Function * Fn) const451b40b48b8SDimitry Andric MCSection *HexagonTargetObjectFile::selectSectionForLookupTable(
452b40b48b8SDimitry Andric const GlobalObject *GO, const TargetMachine &TM, const Function *Fn) const {
453b40b48b8SDimitry Andric
454b40b48b8SDimitry Andric SectionKind Kind = SectionKind::getText();
455b40b48b8SDimitry Andric // If the function has explicit section, place the lookup table in this
456b40b48b8SDimitry Andric // explicit section.
457b40b48b8SDimitry Andric if (Fn->hasSection())
458b40b48b8SDimitry Andric return getExplicitSectionGlobal(Fn, Kind, TM);
459b40b48b8SDimitry Andric
460b40b48b8SDimitry Andric const auto *FuncObj = dyn_cast<GlobalObject>(Fn);
461b40b48b8SDimitry Andric return SelectSectionForGlobal(FuncObj, Kind, TM);
462b40b48b8SDimitry Andric }
463