xref: /minix3/external/bsd/llvm/dist/llvm/lib/Target/Hexagon/HexagonTargetObjectFile.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1f4a2713aSLionel Sambuc //===-- HexagonTargetObjectFile.cpp - Hexagon asm properties --------------===//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc //                     The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc //
10f4a2713aSLionel Sambuc // This file contains the declarations of the HexagonTargetAsmInfo properties.
11f4a2713aSLionel Sambuc //
12f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
13f4a2713aSLionel Sambuc 
14f4a2713aSLionel Sambuc #include "HexagonTargetObjectFile.h"
15f4a2713aSLionel Sambuc #include "HexagonSubtarget.h"
16f4a2713aSLionel Sambuc #include "HexagonTargetMachine.h"
17f4a2713aSLionel Sambuc #include "llvm/IR/DataLayout.h"
18f4a2713aSLionel Sambuc #include "llvm/IR/DerivedTypes.h"
19f4a2713aSLionel Sambuc #include "llvm/IR/Function.h"
20f4a2713aSLionel Sambuc #include "llvm/IR/GlobalVariable.h"
21f4a2713aSLionel Sambuc #include "llvm/MC/MCContext.h"
22f4a2713aSLionel Sambuc #include "llvm/Support/CommandLine.h"
23f4a2713aSLionel Sambuc #include "llvm/Support/ELF.h"
24f4a2713aSLionel Sambuc 
25f4a2713aSLionel Sambuc using namespace llvm;
26f4a2713aSLionel Sambuc 
27f4a2713aSLionel Sambuc static cl::opt<int> SmallDataThreshold("hexagon-small-data-threshold",
28f4a2713aSLionel Sambuc                                 cl::init(8), cl::Hidden,
29f4a2713aSLionel Sambuc                 cl::desc("The maximum size of an object in the sdata section"));
30f4a2713aSLionel Sambuc 
Initialize(MCContext & Ctx,const TargetMachine & TM)31f4a2713aSLionel Sambuc void HexagonTargetObjectFile::Initialize(MCContext &Ctx,
32f4a2713aSLionel Sambuc                                          const TargetMachine &TM) {
33f4a2713aSLionel Sambuc   TargetLoweringObjectFileELF::Initialize(Ctx, TM);
34*0a6a1f1dSLionel Sambuc   InitializeELF(TM.Options.UseInitArray);
35f4a2713aSLionel Sambuc 
36f4a2713aSLionel Sambuc   SmallDataSection =
37f4a2713aSLionel Sambuc     getContext().getELFSection(".sdata", ELF::SHT_PROGBITS,
38f4a2713aSLionel Sambuc                                ELF::SHF_WRITE | ELF::SHF_ALLOC,
39f4a2713aSLionel Sambuc                                SectionKind::getDataRel());
40f4a2713aSLionel Sambuc   SmallBSSSection =
41f4a2713aSLionel Sambuc     getContext().getELFSection(".sbss", ELF::SHT_NOBITS,
42f4a2713aSLionel Sambuc                                ELF::SHF_WRITE | ELF::SHF_ALLOC,
43f4a2713aSLionel Sambuc                                SectionKind::getBSS());
44f4a2713aSLionel Sambuc }
45f4a2713aSLionel Sambuc 
46f4a2713aSLionel Sambuc // sdata/sbss support taken largely from the MIPS Backend.
IsInSmallSection(uint64_t Size)47f4a2713aSLionel Sambuc static bool IsInSmallSection(uint64_t Size) {
48f4a2713aSLionel Sambuc   return Size > 0 && Size <= (uint64_t)SmallDataThreshold;
49f4a2713aSLionel Sambuc }
50f4a2713aSLionel Sambuc 
IsSmallDataEnabled() const51f4a2713aSLionel Sambuc bool HexagonTargetObjectFile::IsSmallDataEnabled () const {
52f4a2713aSLionel Sambuc   return SmallDataThreshold > 0;
53f4a2713aSLionel Sambuc }
54f4a2713aSLionel Sambuc 
55f4a2713aSLionel Sambuc /// IsGlobalInSmallSection - Return true if this global value should be
56f4a2713aSLionel Sambuc /// placed into small data/bss section.
IsGlobalInSmallSection(const GlobalValue * GV,const TargetMachine & TM) const57f4a2713aSLionel Sambuc bool HexagonTargetObjectFile::IsGlobalInSmallSection(const GlobalValue *GV,
58f4a2713aSLionel Sambuc                                                 const TargetMachine &TM) const {
59f4a2713aSLionel Sambuc   // If the primary definition of this global value is outside the current
60f4a2713aSLionel Sambuc   // translation unit or the global value is available for inspection but not
61f4a2713aSLionel Sambuc   // emission, then do nothing.
62f4a2713aSLionel Sambuc   if (GV->isDeclaration() || GV->hasAvailableExternallyLinkage())
63f4a2713aSLionel Sambuc     return false;
64f4a2713aSLionel Sambuc 
65f4a2713aSLionel Sambuc   // Otherwise, Check if GV should be in sdata/sbss, when normally it would end
66f4a2713aSLionel Sambuc   // up in getKindForGlobal(GV, TM).
67f4a2713aSLionel Sambuc   return IsGlobalInSmallSection(GV, TM, getKindForGlobal(GV, TM));
68f4a2713aSLionel Sambuc }
69f4a2713aSLionel Sambuc 
70f4a2713aSLionel Sambuc /// IsGlobalInSmallSection - Return true if this global value should be
71f4a2713aSLionel Sambuc /// placed into small data/bss section.
72f4a2713aSLionel Sambuc bool HexagonTargetObjectFile::
IsGlobalInSmallSection(const GlobalValue * GV,const TargetMachine & TM,SectionKind Kind) const73f4a2713aSLionel Sambuc IsGlobalInSmallSection(const GlobalValue *GV, const TargetMachine &TM,
74f4a2713aSLionel Sambuc                        SectionKind Kind) const {
75f4a2713aSLionel Sambuc   // Only global variables, not functions.
76f4a2713aSLionel Sambuc   const GlobalVariable *GVA = dyn_cast<GlobalVariable>(GV);
77f4a2713aSLionel Sambuc   if (!GVA)
78f4a2713aSLionel Sambuc     return false;
79f4a2713aSLionel Sambuc 
80f4a2713aSLionel Sambuc   if (Kind.isBSS() || Kind.isDataNoRel() || Kind.isCommon()) {
81f4a2713aSLionel Sambuc     Type *Ty = GV->getType()->getElementType();
82*0a6a1f1dSLionel Sambuc     return IsInSmallSection(
83*0a6a1f1dSLionel Sambuc         TM.getSubtargetImpl()->getDataLayout()->getTypeAllocSize(Ty));
84f4a2713aSLionel Sambuc   }
85f4a2713aSLionel Sambuc 
86f4a2713aSLionel Sambuc   return false;
87f4a2713aSLionel Sambuc }
88f4a2713aSLionel Sambuc 
89*0a6a1f1dSLionel Sambuc const MCSection *
SelectSectionForGlobal(const GlobalValue * GV,SectionKind Kind,Mangler & Mang,const TargetMachine & TM) const90*0a6a1f1dSLionel Sambuc HexagonTargetObjectFile::SelectSectionForGlobal(const GlobalValue *GV,
91*0a6a1f1dSLionel Sambuc                                                 SectionKind Kind, Mangler &Mang,
92*0a6a1f1dSLionel Sambuc                                                 const TargetMachine &TM) const {
93f4a2713aSLionel Sambuc 
94f4a2713aSLionel Sambuc   // Handle Small Section classification here.
95f4a2713aSLionel Sambuc   if (Kind.isBSS() && IsGlobalInSmallSection(GV, TM, Kind))
96f4a2713aSLionel Sambuc     return SmallBSSSection;
97f4a2713aSLionel Sambuc   if (Kind.isDataNoRel() && IsGlobalInSmallSection(GV, TM, Kind))
98f4a2713aSLionel Sambuc     return SmallDataSection;
99f4a2713aSLionel Sambuc 
100f4a2713aSLionel Sambuc   // Otherwise, we work the same as ELF.
101f4a2713aSLionel Sambuc   return TargetLoweringObjectFileELF::SelectSectionForGlobal(GV, Kind, Mang,TM);
102f4a2713aSLionel Sambuc }
103