blob: 177c348d7c84af6982a86ce8d518e2185628503d [file] [log] [blame]
Jia Liub22310f2012-02-18 12:03:15 +00001//===-- HexagonTargetObjectFile.cpp - Hexagon asm properties --------------===//
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//===----------------------------------------------------------------------===//
13
Craig Topperb25fda92012-03-17 18:46:09 +000014#include "HexagonTargetObjectFile.h"
15#include "HexagonSubtarget.h"
16#include "HexagonTargetMachine.h"
Chandler Carruth9fb823b2013-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 Linthicum1213a7a2011-12-12 21:14:40 +000021#include "llvm/MC/MCContext.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000022#include "llvm/Support/CommandLine.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000023#include "llvm/Support/ELF.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000024
25using namespace llvm;
26
27static cl::opt<int> SmallDataThreshold("hexagon-small-data-threshold",
Jyotsna Verma5eb59802013-05-07 19:53:00 +000028 cl::init(8), cl::Hidden,
29 cl::desc("The maximum size of an object in the sdata section"));
Tony Linthicum1213a7a2011-12-12 21:14:40 +000030
31void HexagonTargetObjectFile::Initialize(MCContext &Ctx,
32 const TargetMachine &TM) {
33 TargetLoweringObjectFileELF::Initialize(Ctx, TM);
Sid Manning326f8af2014-11-03 14:56:05 +000034 InitializeELF(TM.Options.UseInitArray);
Tony Linthicum1213a7a2011-12-12 21:14:40 +000035
Rafael Espindolaba31e272015-01-29 17:33:21 +000036 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);
Tony Linthicum1213a7a2011-12-12 21:14:40 +000040}
41
42// sdata/sbss support taken largely from the MIPS Backend.
43static bool IsInSmallSection(uint64_t Size) {
44 return Size > 0 && Size <= (uint64_t)SmallDataThreshold;
45}
Jyotsna Verma5eb59802013-05-07 19:53:00 +000046
47bool HexagonTargetObjectFile::IsSmallDataEnabled () const {
48 return SmallDataThreshold > 0;
49}
50
Tony Linthicum1213a7a2011-12-12 21:14:40 +000051/// IsGlobalInSmallSection - Return true if this global value should be
52/// placed into small data/bss section.
53bool 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.
68bool HexagonTargetObjectFile::
69IsGlobalInSmallSection(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
Rafael Espindola449711c2015-11-18 06:02:15 +000076 if (Kind.isBSS() || Kind.isData() || Kind.isCommon()) {
Manuel Jacob5f6eaac2016-01-16 20:30:46 +000077 Type *Ty = GV->getValueType();
Mehdi Aminibd7287e2015-07-16 06:11:10 +000078 return IsInSmallSection(
79 GV->getParent()->getDataLayout().getTypeAllocSize(Ty));
Tony Linthicum1213a7a2011-12-12 21:14:40 +000080 }
81
82 return false;
83}
84
Rafael Espindola0709a7b2015-05-21 19:20:38 +000085MCSection *
Rafael Espindolafa0f7282014-02-08 14:53:28 +000086HexagonTargetObjectFile::SelectSectionForGlobal(const GlobalValue *GV,
87 SectionKind Kind, Mangler &Mang,
88 const TargetMachine &TM) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +000089
90 // Handle Small Section classification here.
91 if (Kind.isBSS() && IsGlobalInSmallSection(GV, TM, Kind))
92 return SmallBSSSection;
Rafael Espindola449711c2015-11-18 06:02:15 +000093 if (Kind.isData() && IsGlobalInSmallSection(GV, TM, Kind))
Tony Linthicum1213a7a2011-12-12 21:14:40 +000094 return SmallDataSection;
95
96 // Otherwise, we work the same as ELF.
97 return TargetLoweringObjectFileELF::SelectSectionForGlobal(GV, Kind, Mang,TM);
98}