LLVM  6.0.0svn
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/BinaryFormat/ELF.h"
14 #include "llvm/IR/DataLayout.h"
15 #include "llvm/IR/DerivedTypes.h"
16 #include "llvm/IR/GlobalVariable.h"
17 #include "llvm/MC/MCContext.h"
18 #include "llvm/MC/MCSectionELF.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 
39 static cl::opt<bool>
40 EmbeddedData("membedded-data", cl::Hidden,
41  cl::desc("MIPS: Try to allocate variables in the following"
42  " sections if possible: .rodata, .sdata, .data ."),
43  cl::init(false));
44 
48 
49  SmallDataSection = getContext().getELFSection(
50  ".sdata", ELF::SHT_PROGBITS,
52 
53  SmallBSSSection = getContext().getELFSection(".sbss", ELF::SHT_NOBITS,
56  this->TM = &static_cast<const MipsTargetMachine &>(TM);
57 }
58 
59 // A address must be loaded from a small section if its size is less than the
60 // small section size threshold. Data in this section must be addressed using
61 // gp_rel operator.
62 static bool IsInSmallSection(uint64_t Size) {
63  // gcc has traditionally not treated zero-sized objects as small data, so this
64  // is effectively part of the ABI.
65  return Size > 0 && Size <= SSThreshold;
66 }
67 
68 /// Return true if this global address should be placed into small data/bss
69 /// section.
70 bool MipsTargetObjectFile::IsGlobalInSmallSection(
71  const GlobalObject *GO, const TargetMachine &TM) const {
72  // We first check the case where global is a declaration, because finding
73  // section kind using getKindForGlobal() is only allowed for global
74  // definitions.
76  return IsGlobalInSmallSectionImpl(GO, TM);
77 
78  return IsGlobalInSmallSection(GO, TM, getKindForGlobal(GO, TM));
79 }
80 
81 /// Return true if this global address should be placed into small data/bss
82 /// section.
83 bool MipsTargetObjectFile::
84 IsGlobalInSmallSection(const GlobalObject *GO, const TargetMachine &TM,
85  SectionKind Kind) const {
86  return IsGlobalInSmallSectionImpl(GO, TM) &&
87  (Kind.isData() || Kind.isBSS() || Kind.isCommon() ||
88  Kind.isReadOnly());
89 }
90 
91 /// Return true if this global address should be placed into small data/bss
92 /// section. This method does all the work, except for checking the section
93 /// kind.
94 bool MipsTargetObjectFile::
95 IsGlobalInSmallSectionImpl(const GlobalObject *GO,
96  const TargetMachine &TM) const {
97  const MipsSubtarget &Subtarget =
98  *static_cast<const MipsTargetMachine &>(TM).getSubtargetImpl();
99 
100  // Return if small section is not available.
101  if (!Subtarget.useSmallSection())
102  return false;
103 
104  // Only global variables, not functions.
105  const GlobalVariable *GVA = dyn_cast<GlobalVariable>(GO);
106  if (!GVA)
107  return false;
108 
109  // If the variable has an explicit section, it is placed in that section but
110  // it's addressing mode may change.
111  if (GVA->hasSection()) {
112  StringRef Section = GVA->getSection();
113 
114  // Explicitly placing any variable in the small data section overrides
115  // the global -G value.
116  if (Section == ".sdata" || Section == ".sbss")
117  return true;
118 
119  // Otherwise reject accessing it through the gp pointer. There are some
120  // historic cases which GCC doesn't appear to respect any more. These
121  // are .lit4, .lit8 and .srdata. For the moment reject these as well.
122  return false;
123  }
124 
125  // Enforce -mlocal-sdata.
126  if (!LocalSData && GVA->hasLocalLinkage())
127  return false;
128 
129  // Enforce -mextern-sdata.
130  if (!ExternSData && ((GVA->hasExternalLinkage() && GVA->isDeclaration()) ||
131  GVA->hasCommonLinkage()))
132  return false;
133 
134  // Enforce -membedded-data.
135  if (EmbeddedData && GVA->isConstant())
136  return false;
137 
138  Type *Ty = GVA->getValueType();
139  return IsInSmallSection(
141 }
142 
144  const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
145  // TODO: Could also support "weak" symbols as well with ".gnu.linkonce.s.*"
146  // sections?
147 
148  // Handle Small Section classification here.
149  if (Kind.isBSS() && IsGlobalInSmallSection(GO, TM, Kind))
150  return SmallBSSSection;
151  if (Kind.isData() && IsGlobalInSmallSection(GO, TM, Kind))
152  return SmallDataSection;
153  if (Kind.isReadOnly() && IsGlobalInSmallSection(GO, TM, Kind))
154  return SmallDataSection;
155 
156  // Otherwise, we work the same as ELF.
158 }
159 
160 /// Return true if this constant should be placed into small data section.
162  const DataLayout &DL, const Constant *CN, const TargetMachine &TM) const {
163  return (static_cast<const MipsTargetMachine &>(TM)
164  .getSubtargetImpl()
165  ->useSmallSection() &&
167 }
168 
169 /// Return true if this constant should be placed into small data section.
171  SectionKind Kind,
172  const Constant *C,
173  unsigned &Align) const {
174  if (IsConstantInSmallSection(DL, C, *TM))
175  return SmallDataSection;
176 
177  // Otherwise, we work the same as ELF.
178  return TargetLoweringObjectFileELF::getSectionForConstant(DL, Kind, C, Align);
179 }
180 
181 const MCExpr *
183  const MCExpr *Expr =
186  Expr, MCConstantExpr::create(0x8000, getContext()), getContext());
187 }
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:109
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
bool hasLocalLinkage() const
Definition: GlobalValue.h:427
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx)
Definition: MCExpr.h:305
Compute iterated dominance frontiers using a linear time algorithm.
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
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:414
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:361
virtual void Initialize(MCContext &ctx, const TargetMachine &TM)
This method must be called before any actual lowering is done.
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:431
MCSection * SelectSectionForGlobal(const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const override
Context object for machine code objects.
Definition: MCContext.h:59
bool hasExternalLinkage() const
Definition: GlobalValue.h:413
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:443
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:406
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
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:403
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:96
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:267
bool isDeclaration() const
Return true if the primary definition of this global value is outside of the current translation unit...
Definition: Globals.cpp:201
const unsigned Kind
MCSectionELF * getELFSection(const Twine &Section, unsigned Type, unsigned Flags)
Definition: MCContext.h:379
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:556
bool isData() const
Definition: SectionKind.h:166
constexpr char Size[]
Key for Kernel::Arg::Metadata::mSize.
Primary interface to the complete machine description for the target machine.
Definition: TargetMachine.h:57
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...
static const MCConstantExpr * create(int64_t Value, MCContext &Ctx)
Definition: MCExpr.cpp:159