1 //===-- HexagonTargetObjectFile.cpp - Hexagon asm properties --------------===//
2 //
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 //===----------------------------------------------------------------------===//
13 
14 #include "HexagonTargetObjectFile.h"
15 #include "HexagonSubtarget.h"
16 #include "HexagonTargetMachine.h"
17 #include "llvm/IR/DataLayout.h"
18 #include "llvm/IR/DerivedTypes.h"
19 #include "llvm/IR/Function.h"
20 #include "llvm/IR/GlobalVariable.h"
21 #include "llvm/MC/MCContext.h"
22 #include "llvm/Support/CommandLine.h"
23 #include "llvm/Support/ELF.h"
24 
25 using namespace llvm;
26 
27 static cl::opt<int> SmallDataThreshold("hexagon-small-data-threshold",
28                                 cl::init(8), cl::Hidden,
29                 cl::desc("The maximum size of an object in the sdata section"));
30 
Initialize(MCContext & Ctx,const TargetMachine & TM)31 void HexagonTargetObjectFile::Initialize(MCContext &Ctx,
32                                          const TargetMachine &TM) {
33   TargetLoweringObjectFileELF::Initialize(Ctx, TM);
34   InitializeELF(TM.Options.UseInitArray);
35 
36   SmallDataSection = getContext().getELFSection(
37       ".sdata", ELF::SHT_PROGBITS, ELF::SHF_WRITE | ELF::SHF_ALLOC);
38   SmallBSSSection = getContext().getELFSection(".sbss", ELF::SHT_NOBITS,
39                                                ELF::SHF_WRITE | ELF::SHF_ALLOC);
40 }
41 
42 // sdata/sbss support taken largely from the MIPS Backend.
IsInSmallSection(uint64_t Size)43 static bool IsInSmallSection(uint64_t Size) {
44   return Size > 0 && Size <= (uint64_t)SmallDataThreshold;
45 }
46 
IsSmallDataEnabled() const47 bool HexagonTargetObjectFile::IsSmallDataEnabled () const {
48   return SmallDataThreshold > 0;
49 }
50 
51 /// IsGlobalInSmallSection - Return true if this global value should be
52 /// placed into small data/bss section.
IsGlobalInSmallSection(const GlobalValue * GV,const TargetMachine & TM) const53 bool HexagonTargetObjectFile::IsGlobalInSmallSection(const GlobalValue *GV,
54                                                 const TargetMachine &TM) const {
55   // If the primary definition of this global value is outside the current
56   // translation unit or the global value is available for inspection but not
57   // emission, then do nothing.
58   if (GV->isDeclaration() || GV->hasAvailableExternallyLinkage())
59     return false;
60 
61   // Otherwise, Check if GV should be in sdata/sbss, when normally it would end
62   // up in getKindForGlobal(GV, TM).
63   return IsGlobalInSmallSection(GV, TM, getKindForGlobal(GV, TM));
64 }
65 
66 /// IsGlobalInSmallSection - Return true if this global value should be
67 /// placed into small data/bss section.
68 bool HexagonTargetObjectFile::
IsGlobalInSmallSection(const GlobalValue * GV,const TargetMachine & TM,SectionKind Kind) const69 IsGlobalInSmallSection(const GlobalValue *GV, const TargetMachine &TM,
70                        SectionKind Kind) const {
71   // Only global variables, not functions.
72   const GlobalVariable *GVA = dyn_cast<GlobalVariable>(GV);
73   if (!GVA)
74     return false;
75 
76   if (Kind.isBSS() || Kind.isDataNoRel() || Kind.isCommon()) {
77     Type *Ty = GV->getType()->getElementType();
78     return IsInSmallSection(TM.getDataLayout()->getTypeAllocSize(Ty));
79   }
80 
81   return false;
82 }
83 
84 const MCSection *
SelectSectionForGlobal(const GlobalValue * GV,SectionKind Kind,Mangler & Mang,const TargetMachine & TM) const85 HexagonTargetObjectFile::SelectSectionForGlobal(const GlobalValue *GV,
86                                                 SectionKind Kind, Mangler &Mang,
87                                                 const TargetMachine &TM) const {
88 
89   // Handle Small Section classification here.
90   if (Kind.isBSS() && IsGlobalInSmallSection(GV, TM, Kind))
91     return SmallBSSSection;
92   if (Kind.isDataNoRel() && IsGlobalInSmallSection(GV, TM, Kind))
93     return SmallDataSection;
94 
95   // Otherwise, we work the same as ELF.
96   return TargetLoweringObjectFileELF::SelectSectionForGlobal(GV, Kind, Mang,TM);
97 }
98