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",
28b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum                                cl::init(8), cl::Hidden);
29b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
30b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicumvoid HexagonTargetObjectFile::Initialize(MCContext &Ctx,
31b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum                                         const TargetMachine &TM) {
32b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  TargetLoweringObjectFileELF::Initialize(Ctx, TM);
33b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
34b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
35b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  SmallDataSection =
36b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    getContext().getELFSection(".sdata", ELF::SHT_PROGBITS,
37b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum                               ELF::SHF_WRITE | ELF::SHF_ALLOC,
38b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum                               SectionKind::getDataRel());
39b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  SmallBSSSection =
40b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    getContext().getELFSection(".sbss", ELF::SHT_NOBITS,
41b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum                               ELF::SHF_WRITE | ELF::SHF_ALLOC,
42b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum                               SectionKind::getBSS());
43b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum}
44b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
45b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum// sdata/sbss support taken largely from the MIPS Backend.
46b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicumstatic bool IsInSmallSection(uint64_t Size) {
47b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  return Size > 0 && Size <= (uint64_t)SmallDataThreshold;
48b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum}
49b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum/// IsGlobalInSmallSection - Return true if this global value should be
50b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum/// placed into small data/bss section.
51b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicumbool HexagonTargetObjectFile::IsGlobalInSmallSection(const GlobalValue *GV,
52b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum                                                const TargetMachine &TM) const {
53b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  // If the primary definition of this global value is outside the current
54b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  // translation unit or the global value is available for inspection but not
55b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  // emission, then do nothing.
56b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  if (GV->isDeclaration() || GV->hasAvailableExternallyLinkage())
57b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    return false;
58b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
59b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  // Otherwise, Check if GV should be in sdata/sbss, when normally it would end
60b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  // up in getKindForGlobal(GV, TM).
61b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  return IsGlobalInSmallSection(GV, TM, getKindForGlobal(GV, TM));
62b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum}
63b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
64b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum/// IsGlobalInSmallSection - Return true if this global value should be
65b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum/// placed into small data/bss section.
66b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicumbool HexagonTargetObjectFile::
67b4b54153ad760c69a00a08531abef4ed434a5092Tony LinthicumIsGlobalInSmallSection(const GlobalValue *GV, const TargetMachine &TM,
68b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum                       SectionKind Kind) const {
69b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  // Only global variables, not functions.
70b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  const GlobalVariable *GVA = dyn_cast<GlobalVariable>(GV);
71b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  if (!GVA)
72b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    return false;
73b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
74b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  if (Kind.isBSS() || Kind.isDataNoRel() || Kind.isCommon()) {
75b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    Type *Ty = GV->getType()->getElementType();
763574eca1b02600bac4e625297f4ecf745f4c4f32Micah Villmow    return IsInSmallSection(TM.getDataLayout()->getTypeAllocSize(Ty));
77b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  }
78b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
79b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  return false;
80b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum}
81b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
82b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicumconst MCSection *HexagonTargetObjectFile::
83b4b54153ad760c69a00a08531abef4ed434a5092Tony LinthicumSelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
84b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum                       Mangler *Mang, const TargetMachine &TM) const {
85b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
86b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  // Handle Small Section classification here.
87b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  if (Kind.isBSS() && IsGlobalInSmallSection(GV, TM, Kind))
88b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    return SmallBSSSection;
89b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  if (Kind.isDataNoRel() && IsGlobalInSmallSection(GV, TM, Kind))
90b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    return SmallDataSection;
91b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
92b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  // Otherwise, we work the same as ELF.
93b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  return TargetLoweringObjectFileELF::SelectSectionForGlobal(GV, Kind, Mang,TM);
94b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum}
95