131d157ae1ac2cd9c787dc3c1d28e64c682803844Jia Liu//===-- HexagonTargetObjectFile.cpp - Hexagon asm properties --------------===//
2b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum//
3b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum//                     The LLVM Compiler Infrastructure
4b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum//
5b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum// This file is distributed under the University of Illinois Open Source
6b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum// License. See LICENSE.TXT for details.
7b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum//
8b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum//===----------------------------------------------------------------------===//
9b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum//
10b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum// This file contains the declarations of the HexagonTargetAsmInfo properties.
11b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum//
12b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum//===----------------------------------------------------------------------===//
13b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
1479aa3417eb6f58d668aadfedf075240a41d35a26Craig Topper#include "HexagonTargetObjectFile.h"
1579aa3417eb6f58d668aadfedf075240a41d35a26Craig Topper#include "HexagonSubtarget.h"
1679aa3417eb6f58d668aadfedf075240a41d35a26Craig Topper#include "HexagonTargetMachine.h"
170b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/DataLayout.h"
180b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/DerivedTypes.h"
190b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/Function.h"
200b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/GlobalVariable.h"
21b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum#include "llvm/MC/MCContext.h"
22b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum#include "llvm/Support/CommandLine.h"
23d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "llvm/Support/ELF.h"
24b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
25b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicumusing namespace llvm;
26b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
27b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicumstatic cl::opt<int> SmallDataThreshold("hexagon-small-data-threshold",
28f931f691ee23d431135481fcf23a58658824ca67Jyotsna Verma                                cl::init(8), cl::Hidden,
29f931f691ee23d431135481fcf23a58658824ca67Jyotsna Verma                cl::desc("The maximum size of an object in the sdata section"));
30b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
31b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicumvoid HexagonTargetObjectFile::Initialize(MCContext &Ctx,
32b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum                                         const TargetMachine &TM) {
33b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  TargetLoweringObjectFileELF::Initialize(Ctx, TM);
34b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
35b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
36b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  SmallDataSection =
37b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    getContext().getELFSection(".sdata", ELF::SHT_PROGBITS,
38b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum                               ELF::SHF_WRITE | ELF::SHF_ALLOC,
39b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum                               SectionKind::getDataRel());
40b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  SmallBSSSection =
41b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    getContext().getELFSection(".sbss", ELF::SHT_NOBITS,
42b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum                               ELF::SHF_WRITE | ELF::SHF_ALLOC,
43b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum                               SectionKind::getBSS());
44b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum}
45b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
46b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum// sdata/sbss support taken largely from the MIPS Backend.
47b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicumstatic bool IsInSmallSection(uint64_t Size) {
48b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  return Size > 0 && Size <= (uint64_t)SmallDataThreshold;
49b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum}
50f931f691ee23d431135481fcf23a58658824ca67Jyotsna Verma
51f931f691ee23d431135481fcf23a58658824ca67Jyotsna Vermabool HexagonTargetObjectFile::IsSmallDataEnabled () const {
52f931f691ee23d431135481fcf23a58658824ca67Jyotsna Verma  return SmallDataThreshold > 0;
53f931f691ee23d431135481fcf23a58658824ca67Jyotsna Verma}
54f931f691ee23d431135481fcf23a58658824ca67Jyotsna Verma
55b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum/// IsGlobalInSmallSection - Return true if this global value should be
56b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum/// placed into small data/bss section.
57b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicumbool HexagonTargetObjectFile::IsGlobalInSmallSection(const GlobalValue *GV,
58b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum                                                const TargetMachine &TM) const {
59b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  // If the primary definition of this global value is outside the current
60b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  // translation unit or the global value is available for inspection but not
61b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  // emission, then do nothing.
62b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  if (GV->isDeclaration() || GV->hasAvailableExternallyLinkage())
63b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    return false;
64b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
65b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  // Otherwise, Check if GV should be in sdata/sbss, when normally it would end
66b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  // up in getKindForGlobal(GV, TM).
67b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  return IsGlobalInSmallSection(GV, TM, getKindForGlobal(GV, TM));
68b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum}
69b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
70b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum/// IsGlobalInSmallSection - Return true if this global value should be
71b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum/// placed into small data/bss section.
72b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicumbool HexagonTargetObjectFile::
73b4b54153ad760c69a00a08531abef4ed434a5092Tony LinthicumIsGlobalInSmallSection(const GlobalValue *GV, const TargetMachine &TM,
74b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum                       SectionKind Kind) const {
75b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  // Only global variables, not functions.
76b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  const GlobalVariable *GVA = dyn_cast<GlobalVariable>(GV);
77b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  if (!GVA)
78b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    return false;
79b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
80b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  if (Kind.isBSS() || Kind.isDataNoRel() || Kind.isCommon()) {
81b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    Type *Ty = GV->getType()->getElementType();
823574eca1b02600bac4e625297f4ecf745f4c4f32Micah Villmow    return IsInSmallSection(TM.getDataLayout()->getTypeAllocSize(Ty));
83b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  }
84b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
85b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  return false;
86b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum}
87b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
8836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hinesconst MCSection *
8936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen HinesHexagonTargetObjectFile::SelectSectionForGlobal(const GlobalValue *GV,
9036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines                                                SectionKind Kind, Mangler &Mang,
9136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines                                                const TargetMachine &TM) const {
92b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
93b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  // Handle Small Section classification here.
94b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  if (Kind.isBSS() && IsGlobalInSmallSection(GV, TM, Kind))
95b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    return SmallBSSSection;
96b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  if (Kind.isDataNoRel() && IsGlobalInSmallSection(GV, TM, Kind))
97b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    return SmallDataSection;
98b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
99b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  // Otherwise, we work the same as ELF.
100b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  return TargetLoweringObjectFileELF::SelectSectionForGlobal(GV, Kind, Mang,TM);
101b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum}
102