LLVM  8.0.1
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"
14 #include "llvm/BinaryFormat/ELF.h"
15 #include "llvm/IR/DataLayout.h"
16 #include "llvm/IR/DerivedTypes.h"
17 #include "llvm/IR/GlobalVariable.h"
18 #include "llvm/MC/MCContext.h"
19 #include "llvm/MC/MCSectionELF.h"
22 using namespace llvm;
23 
24 static cl::opt<unsigned>
25 SSThreshold("mips-ssection-threshold", cl::Hidden,
26  cl::desc("Small data and bss section threshold size (default=8)"),
27  cl::init(8));
28 
29 static cl::opt<bool>
30 LocalSData("mlocal-sdata", cl::Hidden,
31  cl::desc("MIPS: Use gp_rel for object-local data."),
32  cl::init(true));
33 
34 static cl::opt<bool>
35 ExternSData("mextern-sdata", cl::Hidden,
36  cl::desc("MIPS: Use gp_rel for data that is not defined by the "
37  "current object."),
38  cl::init(true));
39 
40 static cl::opt<bool>
41 EmbeddedData("membedded-data", cl::Hidden,
42  cl::desc("MIPS: Try to allocate variables in the following"
43  " sections if possible: .rodata, .sdata, .data ."),
44  cl::init(false));
45 
49 
50  SmallDataSection = getContext().getELFSection(
51  ".sdata", ELF::SHT_PROGBITS,
53 
54  SmallBSSSection = getContext().getELFSection(".sbss", ELF::SHT_NOBITS,
57  this->TM = &static_cast<const MipsTargetMachine &>(TM);
58 }
59 
60 // A address must be loaded from a small section if its size is less than the
61 // small section size threshold. Data in this section must be addressed using
62 // gp_rel operator.
63 static bool IsInSmallSection(uint64_t Size) {
64  // gcc has traditionally not treated zero-sized objects as small data, so this
65  // is effectively part of the ABI.
66  return Size > 0 && Size <= SSThreshold;
67 }
68 
69 /// Return true if this global address should be placed into small data/bss
70 /// section.
71 bool MipsTargetObjectFile::IsGlobalInSmallSection(
72  const GlobalObject *GO, const TargetMachine &TM) const {
73  // We first check the case where global is a declaration, because finding
74  // section kind using getKindForGlobal() is only allowed for global
75  // definitions.
77  return IsGlobalInSmallSectionImpl(GO, TM);
78 
79  return IsGlobalInSmallSection(GO, TM, getKindForGlobal(GO, TM));
80 }
81 
82 /// Return true if this global address should be placed into small data/bss
83 /// section.
84 bool MipsTargetObjectFile::
85 IsGlobalInSmallSection(const GlobalObject *GO, const TargetMachine &TM,
86  SectionKind Kind) const {
87  return IsGlobalInSmallSectionImpl(GO, TM) &&
88  (Kind.isData() || Kind.isBSS() || Kind.isCommon() ||
89  Kind.isReadOnly());
90 }
91 
92 /// Return true if this global address should be placed into small data/bss
93 /// section. This method does all the work, except for checking the section
94 /// kind.
95 bool MipsTargetObjectFile::
96 IsGlobalInSmallSectionImpl(const GlobalObject *GO,
97  const TargetMachine &TM) const {
98  const MipsSubtarget &Subtarget =
99  *static_cast<const MipsTargetMachine &>(TM).getSubtargetImpl();
100 
101  // Return if small section is not available.
102  if (!Subtarget.useSmallSection())
103  return false;
104 
105  // Only global variables, not functions.
106  const GlobalVariable *GVA = dyn_cast<GlobalVariable>(GO);
107  if (!GVA)
108  return false;
109 
110  // If the variable has an explicit section, it is placed in that section but
111  // it's addressing mode may change.
112  if (GVA->hasSection()) {
113  StringRef Section = GVA->getSection();
114 
115  // Explicitly placing any variable in the small data section overrides
116  // the global -G value.
117  if (Section == ".sdata" || Section == ".sbss")
118  return true;
119 
120  // Otherwise reject accessing it through the gp pointer. There are some
121  // historic cases which GCC doesn't appear to respect any more. These
122  // are .lit4, .lit8 and .srdata. For the moment reject these as well.
123  return false;
124  }
125 
126  // Enforce -mlocal-sdata.
127  if (!LocalSData && GVA->hasLocalLinkage())
128  return false;
129 
130  // Enforce -mextern-sdata.
131  if (!ExternSData && ((GVA->hasExternalLinkage() && GVA->isDeclaration()) ||
132  GVA->hasCommonLinkage()))
133  return false;
134 
135  // Enforce -membedded-data.
136  if (EmbeddedData && GVA->isConstant())
137  return false;
138 
139  Type *Ty = GVA->getValueType();
140 
141  // It is possible that the type of the global is unsized, i.e. a declaration
142  // of a extern struct. In this case don't presume it is in the small data
143  // section. This happens e.g. when building the FreeBSD kernel.
144  if (!Ty->isSized())
145  return false;
146 
147  return IsInSmallSection(
149 }
150 
152  const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
153  // TODO: Could also support "weak" symbols as well with ".gnu.linkonce.s.*"
154  // sections?
155 
156  // Handle Small Section classification here.
157  if (Kind.isBSS() && IsGlobalInSmallSection(GO, TM, Kind))
158  return SmallBSSSection;
159  if (Kind.isData() && IsGlobalInSmallSection(GO, TM, Kind))
160  return SmallDataSection;
161  if (Kind.isReadOnly() && IsGlobalInSmallSection(GO, TM, Kind))
162  return SmallDataSection;
163 
164  // Otherwise, we work the same as ELF.
166 }
167 
168 /// Return true if this constant should be placed into small data section.
170  const DataLayout &DL, const Constant *CN, const TargetMachine &TM) const {
171  return (static_cast<const MipsTargetMachine &>(TM)
172  .getSubtargetImpl()
173  ->useSmallSection() &&
175 }
176 
177 /// Return true if this constant should be placed into small data section.
179  SectionKind Kind,
180  const Constant *C,
181  unsigned &Align) const {
182  if (IsConstantInSmallSection(DL, C, *TM))
183  return SmallDataSection;
184 
185  // Otherwise, we work the same as ELF.
186  return TargetLoweringObjectFileELF::getSectionForConstant(DL, Kind, C, Align);
187 }
188 
189 const MCExpr *
191  const MCExpr *Expr =
194  Expr, MCConstantExpr::create(0x8000, getContext()), getContext());
196 }
uint64_t CallInst * C
StringRef getSection() const
Get the custom section of this global if it has one.
Definition: GlobalObject.h:90
Instances of this class represent a uniqued identifier for a section in the current translation unit...
Definition: MCSection.h:39
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:111
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
bool hasLocalLinkage() const
Definition: GlobalValue.h:436
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx)
Definition: MCExpr.h:323
This class represents lattice values for constants.
Definition: AllocatorList.h:24
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))
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:42
bool isSized(SmallPtrSetImpl< Type *> *Visited=nullptr) const
Return true if it makes sense to take the size of this type.
Definition: Type.h:265
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.
bool hasAvailableExternallyLinkage() const
Definition: GlobalValue.h:423
static bool IsInSmallSection(uint64_t Size)
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.
const DataLayout & getDataLayout() const
Get the data layout for the module&#39;s target platform.
Definition: Module.cpp:371
bool isConstant() const
If the value is a global constant, its value is immutable throughout the runtime execution of the pro...
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:36
bool hasCommonLinkage() const
Definition: GlobalValue.h:440
MCSection * SelectSectionForGlobal(const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const override
Context object for machine code objects.
Definition: MCContext.h:63
bool hasExternalLinkage() const
Definition: GlobalValue.h:422
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:245
bool useSmallSection() const
static const MCBinaryExpr * createAdd(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx)
Definition: MCExpr.h:461
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:423
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:46
This is an important base class in LLVM.
Definition: Constant.h:42
bool IsConstantInSmallSection(const DataLayout &DL, const Constant *CN, const TargetMachine &TM) const
Return true if this constant should be placed into small data section.
SectionKind - This is a simple POD value that classifies the properties of a section.
Definition: SectionKind.h:23
static cl::opt< bool > EmbeddedData("membedded-data", cl::Hidden, cl::desc("MIPS: Try to allocate variables in the following" " sections if possible: .rodata, .sdata, .data ."), cl::init(false))
bool isBSS() const
Definition: SectionKind.h:160
unsigned UseInitArray
UseInitArray - Use .init_array instead of .ctors for static constructors.
MCSection * SelectSectionForGlobal(const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const override
bool hasSection() const
Check if this global has a custom object file section.
Definition: GlobalObject.h:82
bool isCommon() const
Definition: SectionKind.h:164
static const MipsMCExpr * create(MipsExprKind Kind, const MCExpr *Expr, MCContext &Ctx)
Definition: MipsMCExpr.cpp:28
bool isReadOnly() const
Definition: SectionKind.h:123
uint64_t getTypeAllocSize(Type *Ty) const
Returns the offset in bytes between successive objects of the specified type, including alignment pad...
Definition: DataLayout.h:436
static cl::opt< unsigned > SSThreshold("mips-ssection-threshold", cl::Hidden, cl::desc("Small data and bss section threshold size (default=8)"), cl::init(8))
TargetOptions Options
Definition: TargetMachine.h:97
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:323
Type * getValueType() const
Definition: GlobalValue.h:276
uint32_t Size
Definition: Profile.cpp:47
bool isDeclaration() const
Return true if the primary definition of this global value is outside of the current translation unit...
Definition: Globals.cpp:206
const unsigned Kind
MCSectionELF * getELFSection(const Twine &Section, unsigned Type, unsigned Flags)
Definition: MCContext.h:389
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:566
bool isData() const
Definition: SectionKind.h:166
Primary interface to the complete machine description for the target machine.
Definition: TargetMachine.h:59
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
static SectionKind getKindForGlobal(const GlobalObject *GO, const TargetMachine &TM)
Classify the specified global variable into a set of target independent categories embodied in Sectio...
void Initialize(MCContext &Ctx, const TargetMachine &TM) override
This method must be called before any actual lowering is done.
static const MCConstantExpr * create(int64_t Value, MCContext &Ctx)
Definition: MCExpr.cpp:164