LLVM 20.0.0git
RISCVTargetObjectFile.cpp
Go to the documentation of this file.
1//===-- RISCVTargetObjectFile.cpp - RISC-V Object Info --------------------===//
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
11#include "RISCVTargetMachine.h"
13#include "llvm/IR/Module.h"
14#include "llvm/MC/MCContext.h"
16#include "llvm/MC/MCValue.h"
17
18using namespace llvm;
19
22 *getContext().getSubtargetInfo());
23}
24
26 const TargetMachine &TM) {
28
31
32 SmallDataSection = getContext().getELFSection(
34 SmallBSSSection = getContext().getELFSection(".sbss", ELF::SHT_NOBITS,
36 SmallRODataSection =
38 SmallROData4Section = getContext().getELFSection(
39 ".srodata.cst4", ELF::SHT_PROGBITS, ELF::SHF_ALLOC | ELF::SHF_MERGE, 4);
40 SmallROData8Section = getContext().getELFSection(
41 ".srodata.cst8", ELF::SHT_PROGBITS, ELF::SHF_ALLOC | ELF::SHF_MERGE, 8);
42 SmallROData16Section = getContext().getELFSection(
43 ".srodata.cst16", ELF::SHT_PROGBITS, ELF::SHF_ALLOC | ELF::SHF_MERGE, 16);
44 SmallROData32Section = getContext().getELFSection(
45 ".srodata.cst32", ELF::SHT_PROGBITS, ELF::SHF_ALLOC | ELF::SHF_MERGE, 32);
46}
47
49 const GlobalValue *GV, const MCSymbol *Sym, const MCValue &MV,
50 int64_t Offset, MachineModuleInfo *MMI, MCStreamer &Streamer) const {
51 int64_t FinalOffset = Offset + MV.getConstant();
52 const MCExpr *Res =
54 const MCExpr *Off = MCConstantExpr::create(FinalOffset, getContext());
55 return MCBinaryExpr::createAdd(Res, Off, getContext());
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 could be addressed by
60// using 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.
70 const GlobalObject *GO, const TargetMachine &TM) const {
71 // Only global variables, not functions.
72 const GlobalVariable *GVA = dyn_cast<GlobalVariable>(GO);
73 if (!GVA)
74 return false;
75
76 // If the variable has an explicit section, it is placed in that section.
77 if (GVA->hasSection()) {
78 StringRef Section = GVA->getSection();
79
80 // Explicitly placing any variable in the small data section overrides
81 // the global -G value.
82 if (Section == ".sdata" || Section == ".sbss")
83 return true;
84
85 // Otherwise reject putting the variable to small section if it has an
86 // explicit section name.
87 return false;
88 }
89
90 if (((GVA->hasExternalLinkage() && GVA->isDeclaration()) ||
91 GVA->hasCommonLinkage()))
92 return false;
93
94 Type *Ty = GVA->getValueType();
95 // It is possible that the type of the global is unsized, i.e. a declaration
96 // of a extern struct. In this case don't presume it is in the small data
97 // section. This happens e.g. when building the FreeBSD kernel.
98 if (!Ty->isSized())
99 return false;
100
101 return isInSmallSection(
103}
104
106 const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
107 // Handle Small Section classification here.
108 if (isGlobalInSmallSection(GO, TM)) {
109 // Emit to an unique sdata/sbss section when -fdata-section is set.
110 // However, if a symbol has an explicit sdata/sbss section, place it in that
111 // section.
112 bool EmitUniquedSection = TM.getDataSections() && !GO->hasSection();
113
114 if (Kind.isBSS()) {
115 if (EmitUniquedSection) {
116 SmallString<128> Name(".sbss.");
117 Name.append(GO->getName());
120 }
121
122 return SmallBSSSection;
123 }
124
125 if (Kind.isData()) {
126 if (EmitUniquedSection) {
127 SmallString<128> Name(".sdata.");
128 Name.append(GO->getName());
131 }
132
133 return SmallDataSection;
134 }
135 }
136
137 // Otherwise, we work the same as ELF.
139}
140
144 M.getModuleFlagsMetadata(ModuleFlags);
145
146 for (const auto &MFE : ModuleFlags) {
147 StringRef Key = MFE.Key->getString();
148 if (Key == "SmallDataLimit") {
149 SSThreshold = mdconst::extract<ConstantInt>(MFE.Val)->getZExtValue();
150 break;
151 }
152 }
153}
154
155/// Return true if this constant should be placed into small data section.
157 const DataLayout &DL, const Constant *CN) const {
158 return isInSmallSection(DL.getTypeAllocSize(CN->getType()));
159}
160
162 const DataLayout &DL, SectionKind Kind, const Constant *C,
163 Align &Alignment) const {
164 if (C && isConstantInSmallSection(DL, C)) {
165 if (Kind.isMergeableConst4())
166 return SmallROData4Section;
167 if (Kind.isMergeableConst8())
168 return SmallROData8Section;
169 if (Kind.isMergeableConst16())
170 return SmallROData16Section;
171 if (Kind.isMergeableConst32())
172 return SmallROData32Section;
173 // LLVM only generate up to .rodata.cst32, and use .rodata section if more
174 // than 32 bytes, so just use .srodata here.
175 return SmallRODataSection;
176 }
177
178 // Otherwise, we work the same as ELF.
180 Alignment);
181}
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
std::string Name
uint64_t Size
Symbol * Sym
Definition: ELF_riscv.cpp:479
Module.h This file contains the declarations for the Module class.
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:457
StringRef getSection() const
Get the custom section of this global if it has one.
Definition: GlobalObject.h:117
bool hasSection() const
Check if this global has a custom object file section.
Definition: GlobalObject.h:109
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:296
const DataLayout & getDataLayout() const
Get the data layout of the module this global belongs to.
Definition: Globals.cpp:130
bool hasCommonLinkage() const
Definition: GlobalValue.h:532
Type * getValueType() const
Definition: GlobalValue.h:296
static const MCBinaryExpr * createAdd(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx)
Definition: MCExpr.h:537
static const MCConstantExpr * create(int64_t Value, MCContext &Ctx, bool PrintInHex=false, unsigned SizeInBytes=0)
Definition: MCExpr.cpp:222
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
Streaming machine code generation interface.
Definition: MCStreamer.h:213
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx)
Definition: MCExpr.h:398
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:41
This represents an "assembler immediate".
Definition: MCValue.h:36
int64_t getConstant() const
Definition: MCValue.h:43
This class contains meta information specific to a module.
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
bool isGlobalInSmallSection(const GlobalObject *GO, const TargetMachine &TM) const
Return true if this global address should be placed into small data/bss section.
MCSection * SelectSectionForGlobal(const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const override
void getModuleMetadata(Module &M) override
Get the module-level metadata that the platform cares about.
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.
bool isInSmallSection(uint64_t Size) const
void Initialize(MCContext &Ctx, const TargetMachine &TM) override
This method must be called before any actual lowering is done.
unsigned getTextSectionAlignment() const override
bool isConstantInSmallSection(const DataLayout &DL, const Constant *CN) const
Return true if this constant should be placed into small data section.
const MCExpr * getIndirectSymViaGOTPCRel(const GlobalValue *GV, const MCSymbol *Sym, const MCValue &MV, int64_t Offset, MachineModuleInfo *MMI, MCStreamer &Streamer) const override
Get the target specific PC relative GOT entry relocation.
unsigned getTextSectionAlignment() const override
SectionKind - This is a simple POD value that classifies the properties of a section.
Definition: SectionKind.h:22
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition: SmallString.h:26
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1196
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
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.
void getModuleMetadata(Module &M) override
Get the module-level metadata that the platform cares about.
MCSection * SelectSectionForGlobal(const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const override
MCSymbolRefExpr::VariantKind PLTRelativeVariantKind
Primary interface to the complete machine description for the target machine.
Definition: TargetMachine.h:77
bool getDataSections() const
Return true if data objects should be emitted into their own section, corresponds to -fdata-sections.
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:310
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:255
StringRef getName() const
Return a constant reference to the value's name.
Definition: Value.cpp:309
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
@ SHT_PROGBITS
Definition: ELF.h:1090
@ SHT_NOBITS
Definition: ELF.h:1097
@ SHF_MERGE
Definition: ELF.h:1194
@ SHF_ALLOC
Definition: ELF.h:1188
@ SHF_WRITE
Definition: ELF.h:1185
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:480
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39