LLVM  4.0.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(
44  ".sdata", ELF::SHT_PROGBITS,
46 
47  SmallBSSSection = getContext().getELFSection(".sbss", ELF::SHT_NOBITS,
50  this->TM = &static_cast<const MipsTargetMachine &>(TM);
51 }
52 
53 // A address must be loaded from a small section if its size is less than the
54 // small section size threshold. Data in this section must be addressed using
55 // gp_rel operator.
56 static bool IsInSmallSection(uint64_t Size) {
57  // gcc has traditionally not treated zero-sized objects as small data, so this
58  // is effectively part of the ABI.
59  return Size > 0 && Size <= SSThreshold;
60 }
61 
62 /// Return true if this global address should be placed into small data/bss
63 /// section.
64 bool MipsTargetObjectFile::IsGlobalInSmallSection(
65  const GlobalObject *GO, const TargetMachine &TM) const {
66  // We first check the case where global is a declaration, because finding
67  // section kind using getKindForGlobal() is only allowed for global
68  // definitions.
70  return IsGlobalInSmallSectionImpl(GO, TM);
71 
72  return IsGlobalInSmallSection(GO, TM, getKindForGlobal(GO, TM));
73 }
74 
75 /// Return true if this global address should be placed into small data/bss
76 /// section.
77 bool MipsTargetObjectFile::
78 IsGlobalInSmallSection(const GlobalObject *GO, const TargetMachine &TM,
79  SectionKind Kind) const {
80  return (IsGlobalInSmallSectionImpl(GO, TM) &&
81  (Kind.isData() || Kind.isBSS() || Kind.isCommon()));
82 }
83 
84 /// Return true if this global address should be placed into small data/bss
85 /// section. This method does all the work, except for checking the section
86 /// kind.
87 bool MipsTargetObjectFile::
88 IsGlobalInSmallSectionImpl(const GlobalObject *GO,
89  const TargetMachine &TM) const {
90  const MipsSubtarget &Subtarget =
91  *static_cast<const MipsTargetMachine &>(TM).getSubtargetImpl();
92 
93  // Return if small section is not available.
94  if (!Subtarget.useSmallSection())
95  return false;
96 
97  // Only global variables, not functions.
98  const GlobalVariable *GVA = dyn_cast<GlobalVariable>(GO);
99  if (!GVA)
100  return false;
101 
102  // Enforce -mlocal-sdata.
103  if (!LocalSData && GVA->hasLocalLinkage())
104  return false;
105 
106  // Enforce -mextern-sdata.
107  if (!ExternSData && ((GVA->hasExternalLinkage() && GVA->isDeclaration()) ||
108  GVA->hasCommonLinkage()))
109  return false;
110 
111  Type *Ty = GVA->getValueType();
112  return IsInSmallSection(
114 }
115 
117  const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
118  // TODO: Could also support "weak" symbols as well with ".gnu.linkonce.s.*"
119  // sections?
120 
121  // Handle Small Section classification here.
122  if (Kind.isBSS() && IsGlobalInSmallSection(GO, TM, Kind))
123  return SmallBSSSection;
124  if (Kind.isData() && IsGlobalInSmallSection(GO, TM, Kind))
125  return SmallDataSection;
126 
127  // Otherwise, we work the same as ELF.
129 }
130 
131 /// Return true if this constant should be placed into small data section.
133  const DataLayout &DL, const Constant *CN, const TargetMachine &TM) const {
134  return (static_cast<const MipsTargetMachine &>(TM)
135  .getSubtargetImpl()
136  ->useSmallSection() &&
138 }
139 
140 /// Return true if this constant should be placed into small data section.
142  SectionKind Kind,
143  const Constant *C,
144  unsigned &Align) const {
145  if (IsConstantInSmallSection(DL, C, *TM))
146  return SmallDataSection;
147 
148  // Otherwise, we work the same as ELF.
149  return TargetLoweringObjectFileELF::getSectionForConstant(DL, Kind, C, Align);
150 }
151 
152 const MCExpr *
154  const MCExpr *Expr =
157  Expr, MCConstantExpr::create(0x8000, getContext()), getContext());
158 }
Instances of this class represent a uniqued identifier for a section in the current translation unit...
Definition: MCSection.h:40
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:102
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx)
Definition: MCExpr.h:298
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:39
MCSection * getSectionForConstant(const DataLayout &DL, SectionKind Kind, const Constant *C, unsigned &Align) const override
Return true if this constant should be placed into small data section.
const MCExpr * getDebugThreadLocalSymbol(const MCSymbol *Sym) const override
Describe a TLS variable address within debug info.
Type * getValueType() const
Definition: GlobalValue.h:261
bool isBSS() const
Definition: SectionKind.h:160
static bool IsInSmallSection(uint64_t Size)
bool hasAvailableExternallyLinkage() const
Definition: GlobalValue.h:402
MCSection * getSectionForConstant(const DataLayout &DL, SectionKind Kind, const Constant *C, unsigned &Align) const override
Given a constant with the SectionKind, return a section that it should be placed in.
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:419
virtual void Initialize(MCContext &ctx, const TargetMachine &TM)
This method must be called before any actual lowering is done.
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:34
MCSection * SelectSectionForGlobal(const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const override
Context object for machine code objects.
Definition: MCContext.h:51
static const MCBinaryExpr * createAdd(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx)
Definition: MCExpr.h:429
static cl::opt< bool > LocalSData("mlocal-sdata", cl::Hidden, cl::desc("MIPS: Use gp_rel for object-local data."), cl::init(true))
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:395
void Initialize(MCContext &Ctx, const TargetMachine &TM) override
This method must be called before any actual lowering is done.
bool IsConstantInSmallSection(const DataLayout &DL, const Constant *CN, const TargetMachine &TM) const
Return true if this constant should be placed into small data section.
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:42
SectionKind - This is a simple POD value that classifies the properties of a section.
Definition: SectionKind.h:23
bool hasExternalLinkage() const
Definition: GlobalValue.h:401
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:408
MCSection * SelectSectionForGlobal(const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const override
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:230
static GCRegistry::Add< ShadowStackGC > C("shadow-stack","Very portable GC for uncooperative code generators")
bool isData() const
Definition: SectionKind.h:166
const DataLayout & getDataLayout() const
Get the data layout for the module's target platform.
Definition: Module.cpp:384
bool isDeclaration() const
Return true if the primary definition of this global value is outside of the current translation unit...
Definition: Globals.cpp:188
static cl::opt< unsigned > SSThreshold("mips-ssection-threshold", cl::Hidden, cl::desc("Small data and bss section threshold size (default=8)"), cl::init(8))
LLVM_NODISCARD 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:287
bool isCommon() const
Definition: SectionKind.h:164
bool hasLocalLinkage() const
Definition: GlobalValue.h:415
const unsigned Kind
MCSectionELF * getELFSection(const Twine &Section, unsigned Type, unsigned Flags)
Definition: MCContext.h:341
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:537
bool useSmallSection() const
Primary interface to the complete machine description for the target machine.
static SectionKind getKindForGlobal(const GlobalObject *GO, const TargetMachine &TM)
Classify the specified global variable into a set of target independent categories embodied in Sectio...
static const MCConstantExpr * create(int64_t Value, MCContext &Ctx)
Definition: MCExpr.cpp:149