blob: 7773cff2d21ae1932ad6e302c78b074a070fed3c [file] [log] [blame]
Jia Liu31d157a2012-02-18 12:03:15 +00001//===-- HexagonTargetObjectFile.cpp - Hexagon asm properties --------------===//
Tony Linthicumb4b54152011-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//===----------------------------------------------------------------------===//
13
Craig Topper79aa3412012-03-17 18:46:09 +000014#include "HexagonTargetObjectFile.h"
15#include "HexagonSubtarget.h"
16#include "HexagonTargetMachine.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000017#include "llvm/IR/DataLayout.h"
18#include "llvm/IR/DerivedTypes.h"
19#include "llvm/IR/Function.h"
20#include "llvm/IR/GlobalVariable.h"
Tony Linthicumb4b54152011-12-12 21:14:40 +000021#include "llvm/MC/MCContext.h"
Tony Linthicumb4b54152011-12-12 21:14:40 +000022#include "llvm/Support/CommandLine.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000023#include "llvm/Support/ELF.h"
Tony Linthicumb4b54152011-12-12 21:14:40 +000024
25using namespace llvm;
26
27static cl::opt<int> SmallDataThreshold("hexagon-small-data-threshold",
Jyotsna Verma81fda3b2013-05-07 16:42:15 +000028 cl::init(8), cl::Hidden,
29 cl::desc("The maximum size of an object in the sdata section"));
Tony Linthicumb4b54152011-12-12 21:14:40 +000030
31void HexagonTargetObjectFile::Initialize(MCContext &Ctx,
32 const TargetMachine &TM) {
33 TargetLoweringObjectFileELF::Initialize(Ctx, TM);
34
35
36 SmallDataSection =
37 getContext().getELFSection(".sdata", ELF::SHT_PROGBITS,
38 ELF::SHF_WRITE | ELF::SHF_ALLOC,
39 SectionKind::getDataRel());
40 SmallBSSSection =
41 getContext().getELFSection(".sbss", ELF::SHT_NOBITS,
42 ELF::SHF_WRITE | ELF::SHF_ALLOC,
43 SectionKind::getBSS());
44}
45
46// sdata/sbss support taken largely from the MIPS Backend.
47static bool IsInSmallSection(uint64_t Size) {
48 return Size > 0 && Size <= (uint64_t)SmallDataThreshold;
49}
Jyotsna Verma81fda3b2013-05-07 16:42:15 +000050
51bool HexagonTargetObjectFile::IsSmallDataEnabled () const {
52 return SmallDataThreshold > 0;
53}
54
Tony Linthicumb4b54152011-12-12 21:14:40 +000055/// IsGlobalInSmallSection - Return true if this global value should be
56/// placed into small data/bss section.
57bool HexagonTargetObjectFile::IsGlobalInSmallSection(const GlobalValue *GV,
58 const TargetMachine &TM) const {
59 // If the primary definition of this global value is outside the current
60 // translation unit or the global value is available for inspection but not
61 // emission, then do nothing.
62 if (GV->isDeclaration() || GV->hasAvailableExternallyLinkage())
63 return false;
64
65 // Otherwise, Check if GV should be in sdata/sbss, when normally it would end
66 // up in getKindForGlobal(GV, TM).
67 return IsGlobalInSmallSection(GV, TM, getKindForGlobal(GV, TM));
68}
69
70/// IsGlobalInSmallSection - Return true if this global value should be
71/// placed into small data/bss section.
72bool HexagonTargetObjectFile::
73IsGlobalInSmallSection(const GlobalValue *GV, const TargetMachine &TM,
74 SectionKind Kind) const {
75 // Only global variables, not functions.
76 const GlobalVariable *GVA = dyn_cast<GlobalVariable>(GV);
77 if (!GVA)
78 return false;
79
80 if (Kind.isBSS() || Kind.isDataNoRel() || Kind.isCommon()) {
81 Type *Ty = GV->getType()->getElementType();
Micah Villmow3574eca2012-10-08 16:38:25 +000082 return IsInSmallSection(TM.getDataLayout()->getTypeAllocSize(Ty));
Tony Linthicumb4b54152011-12-12 21:14:40 +000083 }
84
85 return false;
86}
87
88const MCSection *HexagonTargetObjectFile::
89SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
90 Mangler *Mang, const TargetMachine &TM) const {
91
92 // Handle Small Section classification here.
93 if (Kind.isBSS() && IsGlobalInSmallSection(GV, TM, Kind))
94 return SmallBSSSection;
95 if (Kind.isDataNoRel() && IsGlobalInSmallSection(GV, TM, Kind))
96 return SmallDataSection;
97
98 // Otherwise, we work the same as ELF.
99 return TargetLoweringObjectFileELF::SelectSectionForGlobal(GV, Kind, Mang,TM);
100}