LLVM 20.0.0git
MipsTargetObjectFile.cpp
Go to the documentation of this file.
1//===-- MipsTargetObjectFile.cpp - Mips Object Files ----------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
10#include "MipsSubtarget.h"
11#include "MipsTargetMachine.h"
14#include "llvm/IR/DataLayout.h"
17#include "llvm/MC/MCContext.h"
21using namespace llvm;
22
24SSThreshold("mips-ssection-threshold", cl::Hidden,
25 cl::desc("Small data and bss section threshold size (default=8)"),
26 cl::init(8));
27
28static cl::opt<bool>
29LocalSData("mlocal-sdata", cl::Hidden,
30 cl::desc("MIPS: Use gp_rel for object-local data."),
31 cl::init(true));
32
33static cl::opt<bool>
34ExternSData("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
39static cl::opt<bool>
40EmbeddedData("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
47
48 SmallDataSection = getContext().getELFSection(
49 ".sdata", ELF::SHT_PROGBITS,
51
52 SmallBSSSection = getContext().getELFSection(".sbss", ELF::SHT_NOBITS,
55 this->TM = &static_cast<const MipsTargetMachine &>(TM);
56}
57
58// A address must be loaded from a small section if its size is less than the
59// small section size threshold. Data in this section must be addressed using
60// gp_rel operator.
62 // gcc has traditionally not treated zero-sized objects as small data, so this
63 // is effectively part of the ABI.
64 return Size > 0 && Size <= SSThreshold;
65}
66
67/// Return true if this global address should be placed into small data/bss
68/// section.
69bool MipsTargetObjectFile::IsGlobalInSmallSection(
70 const GlobalObject *GO, const TargetMachine &TM) const {
71 // We first check the case where global is a declaration, because finding
72 // section kind using getKindForGlobal() is only allowed for global
73 // definitions.
75 return IsGlobalInSmallSectionImpl(GO, TM);
76
77 return IsGlobalInSmallSection(GO, TM, getKindForGlobal(GO, TM));
78}
79
80/// Return true if this global address should be placed into small data/bss
81/// section.
82bool MipsTargetObjectFile::
83IsGlobalInSmallSection(const GlobalObject *GO, const TargetMachine &TM,
84 SectionKind Kind) const {
85 return IsGlobalInSmallSectionImpl(GO, TM) &&
86 (Kind.isData() || Kind.isBSS() || Kind.isCommon() ||
87 Kind.isReadOnly());
88}
89
90/// Return true if this global address should be placed into small data/bss
91/// section. This method does all the work, except for checking the section
92/// kind.
93bool MipsTargetObjectFile::
94IsGlobalInSmallSectionImpl(const GlobalObject *GO,
95 const TargetMachine &TM) const {
96 const MipsSubtarget &Subtarget =
97 *static_cast<const MipsTargetMachine &>(TM).getSubtargetImpl();
98
99 // Return if small section is not available.
100 if (!Subtarget.useSmallSection())
101 return false;
102
103 // Only global variables, not functions.
104 const GlobalVariable *GVA = dyn_cast<GlobalVariable>(GO);
105 if (!GVA)
106 return false;
107
108 // If the variable has an explicit section, it is placed in that section but
109 // it's addressing mode may change.
110 if (GVA->hasSection()) {
111 StringRef Section = GVA->getSection();
112
113 // Explicitly placing any variable in the small data section overrides
114 // the global -G value.
115 if (Section == ".sdata" || Section == ".sbss")
116 return true;
117
118 // Otherwise reject accessing it through the gp pointer. There are some
119 // historic cases which GCC doesn't appear to respect any more. These
120 // are .lit4, .lit8 and .srdata. For the moment reject these as well.
121 return false;
122 }
123
124 // Enforce -mlocal-sdata.
125 if (!LocalSData && GVA->hasLocalLinkage())
126 return false;
127
128 // Enforce -mextern-sdata.
129 if (!ExternSData && ((GVA->hasExternalLinkage() && GVA->isDeclaration()) ||
130 GVA->hasCommonLinkage()))
131 return false;
132
133 // Enforce -membedded-data.
134 if (EmbeddedData && GVA->isConstant())
135 return false;
136
137 Type *Ty = GVA->getValueType();
138
139 // It is possible that the type of the global is unsized, i.e. a declaration
140 // of a extern struct. In this case don't presume it is in the small data
141 // section. This happens e.g. when building the FreeBSD kernel.
142 if (!Ty->isSized())
143 return false;
144
145 return IsInSmallSection(
147}
148
150 const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
151 // TODO: Could also support "weak" symbols as well with ".gnu.linkonce.s.*"
152 // sections?
153
154 // Handle Small Section classification here.
155 if (Kind.isBSS() && IsGlobalInSmallSection(GO, TM, Kind))
156 return SmallBSSSection;
157 if (Kind.isData() && IsGlobalInSmallSection(GO, TM, Kind))
158 return SmallDataSection;
159 if (Kind.isReadOnly() && IsGlobalInSmallSection(GO, TM, Kind))
160 return SmallDataSection;
161
162 // Otherwise, we work the same as ELF.
164}
165
166/// Return true if this constant should be placed into small data section.
168 const DataLayout &DL, const Constant *CN, const TargetMachine &TM) const {
169 return (static_cast<const MipsTargetMachine &>(TM)
170 .getSubtargetImpl()
171 ->useSmallSection() &&
172 LocalSData && IsInSmallSection(DL.getTypeAllocSize(CN->getType())));
173}
174
175/// Return true if this constant should be placed into small data section.
177 SectionKind Kind,
178 const Constant *C,
179 Align &Alignment) const {
181 return SmallDataSection;
182
183 // Otherwise, we work the same as ELF.
185 Alignment);
186}
187
188const MCExpr *
190 const MCExpr *Expr =
193 Expr, MCConstantExpr::create(0x8000, getContext()), getContext());
195}
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
uint64_t Size
Symbol * Sym
Definition: ELF_riscv.cpp:479
static cl::opt< unsigned > SSThreshold("lanai-ssection-threshold", cl::Hidden, cl::desc("Small data and bss section threshold size (default=0)"), cl::init(0))
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))
static cl::opt< unsigned > SSThreshold("mips-ssection-threshold", cl::Hidden, cl::desc("Small data and bss section threshold size (default=8)"), cl::init(8))
static cl::opt< bool > LocalSData("mlocal-sdata", cl::Hidden, cl::desc("MIPS: Use gp_rel for object-local data."), cl::init(true))
static bool IsInSmallSection(uint64_t Size)
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))
const char LLVMTargetMachineRef TM
This is an important base class in LLVM.
Definition: Constant.h:42
A parsed version of the target data layout string in and methods for querying it.
Definition: DataLayout.h:63
TypeSize getTypeAllocSize(Type *Ty) const
Returns the offset in bytes between successive objects of the specified type, including alignment pad...
Definition: DataLayout.h:462
StringRef getSection() const
Get the custom section of this global if it has one.
Definition: GlobalObject.h:118
bool hasSection() const
Check if this global has a custom object file section.
Definition: GlobalObject.h:110
bool hasExternalLinkage() const
Definition: GlobalValue.h:511
bool isDeclaration() const
Return true if the primary definition of this global value is outside of the current translation unit...
Definition: Globals.cpp:290
bool hasLocalLinkage() const
Definition: GlobalValue.h:528
const DataLayout & getDataLayout() const
Get the data layout of the module this global belongs to.
Definition: Globals.cpp:124
bool hasCommonLinkage() const
Definition: GlobalValue.h:532
bool hasAvailableExternallyLinkage() const
Definition: GlobalValue.h:512
Type * getValueType() const
Definition: GlobalValue.h:296
bool isConstant() const
If the value is a global constant, its value is immutable throughout the runtime execution of the pro...
static const MCBinaryExpr * createAdd(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx)
Definition: MCExpr.h:532
static const MCConstantExpr * create(int64_t Value, MCContext &Ctx, bool PrintInHex=false, unsigned SizeInBytes=0)
Definition: MCExpr.cpp:193
Context object for machine code objects.
Definition: MCContext.h:83
MCSectionELF * getELFSection(const Twine &Section, unsigned Type, unsigned Flags)
Definition: MCContext.h:551
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:34
MCContext & getContext() const
Instances of this class represent a uniqued identifier for a section in the current translation unit.
Definition: MCSection.h:36
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx)
Definition: MCExpr.h:393
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:41
static const MipsMCExpr * create(MipsExprKind Kind, const MCExpr *Expr, MCContext &Ctx)
Definition: MipsMCExpr.cpp:27
bool useSmallSection() const
MCSection * getSectionForConstant(const DataLayout &DL, SectionKind Kind, const Constant *C, Align &Alignment) const override
Return true if this constant should be placed into small data section.
MCSection * SelectSectionForGlobal(const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const override
const MCExpr * getDebugThreadLocalSymbol(const MCSymbol *Sym) const override
Describe a TLS variable address within debug info.
bool IsConstantInSmallSection(const DataLayout &DL, const Constant *CN, const TargetMachine &TM) const
Return true if this constant should be placed into small data section.
void Initialize(MCContext &Ctx, const TargetMachine &TM) override
This method must be called before any actual lowering is done.
SectionKind - This is a simple POD value that classifies the properties of a section.
Definition: SectionKind.h:22
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
void Initialize(MCContext &Ctx, const TargetMachine &TM) override
This method must be called before any actual lowering is done.
MCSection * getSectionForConstant(const DataLayout &DL, SectionKind Kind, const Constant *C, Align &Alignment) const override
Given a constant with the SectionKind, return a section that it should be placed in.
MCSection * SelectSectionForGlobal(const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const override
static SectionKind getKindForGlobal(const GlobalObject *GO, const TargetMachine &TM)
Classify the specified global variable into a set of target independent categories embodied in Sectio...
Primary interface to the complete machine description for the target machine.
Definition: TargetMachine.h:77
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
bool isSized(SmallPtrSetImpl< Type * > *Visited=nullptr) const
Return true if it makes sense to take the size of this type.
Definition: Type.h:298
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:255
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
@ SHT_PROGBITS
Definition: ELF.h:1089
@ SHT_NOBITS
Definition: ELF.h:1096
@ SHF_ALLOC
Definition: ELF.h:1186
@ SHF_MIPS_GPREL
Definition: ELF.h:1269
@ SHF_WRITE
Definition: ELF.h:1183
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:443
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39