LLVM  3.7.0
MipsTargetObjectFile.cpp
Go to the documentation of this file.
1 //===-- MipsTargetObjectFile.cpp - Mips Object Files ----------------------===//
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 #include "MipsTargetObjectFile.h"
11 #include "MipsSubtarget.h"
12 #include "MipsTargetMachine.h"
13 #include "llvm/IR/DataLayout.h"
14 #include "llvm/IR/DerivedTypes.h"
15 #include "llvm/IR/GlobalVariable.h"
16 #include "llvm/MC/MCContext.h"
17 #include "llvm/MC/MCSectionELF.h"
19 #include "llvm/Support/ELF.h"
21 using namespace llvm;
22 
23 static cl::opt<unsigned>
24 SSThreshold("mips-ssection-threshold", cl::Hidden,
25  cl::desc("Small data and bss section threshold size (default=8)"),
26  cl::init(8));
27 
28 static cl::opt<bool>
29 LocalSData("mlocal-sdata", cl::Hidden,
30  cl::desc("MIPS: Use gp_rel for object-local data."),
31  cl::init(true));
32 
33 static cl::opt<bool>
34 ExternSData("mextern-sdata", cl::Hidden,
35  cl::desc("MIPS: Use gp_rel for data that is not defined by the "
36  "current object."),
37  cl::init(true));
38 
42 
43  SmallDataSection = getContext().getELFSection(
45 
46  SmallBSSSection = getContext().getELFSection(".sbss", ELF::SHT_NOBITS,
48  this->TM = &static_cast<const MipsTargetMachine &>(TM);
49 }
50 
51 // A address must be loaded from a small section if its size is less than the
52 // small section size threshold. Data in this section must be addressed using
53 // gp_rel operator.
54 static bool IsInSmallSection(uint64_t Size) {
55  // gcc has traditionally not treated zero-sized objects as small data, so this
56  // is effectively part of the ABI.
57  return Size > 0 && Size <= SSThreshold;
58 }
59 
60 /// Return true if this global address should be placed into small data/bss
61 /// section.
64  // We first check the case where global is a declaration, because finding
65  // section kind using getKindForGlobal() is only allowed for global
66  // definitions.
68  return IsGlobalInSmallSectionImpl(GV, TM);
69 
70  return IsGlobalInSmallSection(GV, TM, getKindForGlobal(GV, TM));
71 }
72 
73 /// Return true if this global address should be placed into small data/bss
74 /// section.
77  SectionKind Kind) const {
78  return (IsGlobalInSmallSectionImpl(GV, TM) &&
79  (Kind.isDataRel() || Kind.isBSS() || Kind.isCommon()));
80 }
81 
82 /// Return true if this global address should be placed into small data/bss
83 /// section. This method does all the work, except for checking the section
84 /// kind.
87  const TargetMachine &TM) const {
88  const MipsSubtarget &Subtarget =
89  *static_cast<const MipsTargetMachine &>(TM).getSubtargetImpl();
90 
91  // Return if small section is not available.
92  if (!Subtarget.useSmallSection())
93  return false;
94 
95  // Only global variables, not functions.
96  const GlobalVariable *GVA = dyn_cast<GlobalVariable>(GV);
97  if (!GVA)
98  return false;
99 
100  // Enforce -mlocal-sdata.
101  if (!LocalSData && GV->hasLocalLinkage())
102  return false;
103 
104  // Enforce -mextern-sdata.
105  if (!ExternSData && ((GV->hasExternalLinkage() && GV->isDeclaration()) ||
106  GV->hasCommonLinkage()))
107  return false;
108 
109  Type *Ty = GV->getType()->getElementType();
111 }
112 
113 MCSection *
115  SectionKind Kind, Mangler &Mang,
116  const TargetMachine &TM) const {
117  // TODO: Could also support "weak" symbols as well with ".gnu.linkonce.s.*"
118  // sections?
119 
120  // Handle Small Section classification here.
121  if (Kind.isBSS() && IsGlobalInSmallSection(GV, TM, Kind))
122  return SmallBSSSection;
123  if (Kind.isDataRel() && IsGlobalInSmallSection(GV, TM, Kind))
124  return SmallDataSection;
125 
126  // Otherwise, we work the same as ELF.
127  return TargetLoweringObjectFileELF::SelectSectionForGlobal(GV, Kind, Mang,TM);
128 }
129 
130 /// Return true if this constant should be placed into small data section.
133  return (static_cast<const MipsTargetMachine &>(TM)
134  .getSubtargetImpl()
135  ->useSmallSection() &&
137  CN->getType())));
138 }
139 
140 MCSection *
142  const Constant *C) const {
143  if (IsConstantInSmallSection(C, *TM))
144  return SmallDataSection;
145 
146  // Otherwise, we work the same as ELF.
148 }
Instances of this class represent a uniqued identifier for a section in the current translation unit...
Definition: MCSection.h:48
static SectionKind getKindForGlobal(const GlobalValue *GV, const TargetMachine &TM)
Classify the specified global variable into a set of target independent categories embodied in Sectio...
MCSection * getSectionForConstant(SectionKind Kind, const Constant *C) const override
Given a constant with the SectionKind, return a section that it should be placed in.
MCSectionELF * getELFSection(StringRef Section, unsigned Type, unsigned Flags)
Definition: MCContext.h:311
bool isBSS() const
Definition: SectionKind.h:175
static bool IsInSmallSection(uint64_t Size)
bool hasAvailableExternallyLinkage() const
Definition: GlobalValue.h:261
MCSection * getSectionForConstant(SectionKind Kind, const Constant *C) const override
Given a constant with the SectionKind, return a section that it should be placed in.
MCSection * SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind, Mangler &Mang, const TargetMachine &TM) const override
static cl::opt< bool > ExternSData("mextern-sdata", cl::Hidden, cl::desc("MIPS: Use gp_rel for data that is not defined by the ""current object."), cl::init(true))
bool hasCommonLinkage() const
Definition: GlobalValue.h:282
virtual void Initialize(MCContext &ctx, const TargetMachine &TM)
This method must be called before any actual lowering is done.
Context object for machine code objects.
Definition: MCContext.h:48
bool IsConstantInSmallSection(const Constant *CN, const TargetMachine &TM) const
Return true if this constant should be placed into small data section.
static cl::opt< bool > LocalSData("mlocal-sdata", cl::Hidden, cl::desc("MIPS: Use gp_rel for object-local data."), cl::init(true))
Type * getElementType() const
Definition: DerivedTypes.h:323
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:325
MCSection * SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind, Mangler &Mang, const TargetMachine &TM) const override
void Initialize(MCContext &Ctx, const TargetMachine &TM) override
This method must be called before any actual lowering is done.
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:45
This is an important base class in LLVM.
Definition: Constant.h:41
bool IsGlobalInSmallSectionImpl(const GlobalValue *GV, const TargetMachine &TM) const
Return true if this global address should be placed into small data/bss section.
SectionKind - This is a simple POD value that classifies the properties of a section.
Definition: SectionKind.h:28
bool hasExternalLinkage() const
Definition: GlobalValue.h:260
unsigned UseInitArray
UseInitArray - Use .init_array instead of .ctors for static constructors.
uint64_t getTypeAllocSize(Type *Ty) const
Returns the offset in bytes between successive objects of the specified type, including alignment pad...
Definition: DataLayout.h:388
const DataLayout * getDataLayout() const
Deprecated in 3.7, will be removed in 3.8.
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:222
LLVM_ATTRIBUTE_UNUSED_RESULT std::enable_if< !is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:285
PointerType * getType() const
Global values are always pointers.
Definition: GlobalValue.h:185
bool isDeclaration() const
Return true if the primary definition of this global value is outside of the current translation unit...
Definition: Globals.cpp:128
static cl::opt< unsigned > SSThreshold("mips-ssection-threshold", cl::Hidden, cl::desc("Small data and bss section threshold size (default=8)"), cl::init(8))
bool IsGlobalInSmallSection(const GlobalValue *GV, const TargetMachine &TM, SectionKind Kind) const
Return true if this global address should be placed into small data/bss section.
bool isCommon() const
Definition: SectionKind.h:179
bool hasLocalLinkage() const
Definition: GlobalValue.h:280
const ARM::ArchExtKind Kind
bool useSmallSection() const
Primary interface to the complete machine description for the target machine.
bool isDataRel() const
Definition: SectionKind.h:181