LLVM 19.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/MC/MCContext.h"
15#include "llvm/MC/MCValue.h"
16
17using namespace llvm;
18
21 *getContext().getSubtargetInfo());
22}
23
25 const TargetMachine &TM) {
27
30
31 SmallDataSection = getContext().getELFSection(
33 SmallBSSSection = getContext().getELFSection(".sbss", ELF::SHT_NOBITS,
35 SmallRODataSection =
37 SmallROData4Section = getContext().getELFSection(
38 ".srodata.cst4", ELF::SHT_PROGBITS, ELF::SHF_ALLOC | ELF::SHF_MERGE, 4);
39 SmallROData8Section = getContext().getELFSection(
40 ".srodata.cst8", ELF::SHT_PROGBITS, ELF::SHF_ALLOC | ELF::SHF_MERGE, 8);
41 SmallROData16Section = getContext().getELFSection(
42 ".srodata.cst16", ELF::SHT_PROGBITS, ELF::SHF_ALLOC | ELF::SHF_MERGE, 16);
43 SmallROData32Section = getContext().getELFSection(
44 ".srodata.cst32", ELF::SHT_PROGBITS, ELF::SHF_ALLOC | ELF::SHF_MERGE, 32);
45}
46
48 const GlobalValue *GV, const MCSymbol *Sym, const MCValue &MV,
49 int64_t Offset, MachineModuleInfo *MMI, MCStreamer &Streamer) const {
50 int64_t FinalOffset = Offset + MV.getConstant();
51 const MCExpr *Res =
53 const MCExpr *Off = MCConstantExpr::create(FinalOffset, getContext());
54 return MCBinaryExpr::createAdd(Res, Off, getContext());
55}
56
57// A address must be loaded from a small section if its size is less than the
58// small section size threshold. Data in this section could be addressed by
59// using gp_rel operator.
61 // gcc has traditionally not treated zero-sized objects as small data, so this
62 // is effectively part of the ABI.
63 return Size > 0 && Size <= SSThreshold;
64}
65
66// Return true if this global address should be placed into small data/bss
67// section.
69 const GlobalObject *GO, const TargetMachine &TM) const {
70 // Only global variables, not functions.
71 const GlobalVariable *GVA = dyn_cast<GlobalVariable>(GO);
72 if (!GVA)
73 return false;
74
75 // If the variable has an explicit section, it is placed in that section.
76 if (GVA->hasSection()) {
77 StringRef Section = GVA->getSection();
78
79 // Explicitly placing any variable in the small data section overrides
80 // the global -G value.
81 if (Section == ".sdata" || Section == ".sbss")
82 return true;
83
84 // Otherwise reject putting the variable to small section if it has an
85 // explicit section name.
86 return false;
87 }
88
89 if (((GVA->hasExternalLinkage() && GVA->isDeclaration()) ||
90 GVA->hasCommonLinkage()))
91 return false;
92
93 Type *Ty = GVA->getValueType();
94 // It is possible that the type of the global is unsized, i.e. a declaration
95 // of a extern struct. In this case don't presume it is in the small data
96 // section. This happens e.g. when building the FreeBSD kernel.
97 if (!Ty->isSized())
98 return false;
99
100 return isInSmallSection(
102}
103
105 const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
106 // Handle Small Section classification here.
107 if (Kind.isBSS() && isGlobalInSmallSection(GO, TM))
108 return SmallBSSSection;
109 if (Kind.isData() && isGlobalInSmallSection(GO, TM))
110 return SmallDataSection;
111
112 // Otherwise, we work the same as ELF.
114}
115
119 M.getModuleFlagsMetadata(ModuleFlags);
120
121 for (const auto &MFE : ModuleFlags) {
122 StringRef Key = MFE.Key->getString();
123 if (Key == "SmallDataLimit") {
124 SSThreshold = mdconst::extract<ConstantInt>(MFE.Val)->getZExtValue();
125 break;
126 }
127 }
128}
129
130/// Return true if this constant should be placed into small data section.
132 const DataLayout &DL, const Constant *CN) const {
133 return isInSmallSection(DL.getTypeAllocSize(CN->getType()));
134}
135
137 const DataLayout &DL, SectionKind Kind, const Constant *C,
138 Align &Alignment) const {
140 if (Kind.isMergeableConst4())
141 return SmallROData4Section;
142 if (Kind.isMergeableConst8())
143 return SmallROData8Section;
144 if (Kind.isMergeableConst16())
145 return SmallROData16Section;
146 if (Kind.isMergeableConst32())
147 return SmallROData32Section;
148 // LLVM only generate up to .rodata.cst32, and use .rodata section if more
149 // than 32 bytes, so just use .srodata here.
150 return SmallRODataSection;
151 }
152
153 // Otherwise, we work the same as ELF.
155 Alignment);
156}
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
uint64_t Size
Symbol * Sym
Definition: ELF_riscv.cpp:479
const char LLVMTargetMachineRef TM
This is an important base class in LLVM.
Definition: Constant.h:41
A parsed version of the target data layout string in and methods for querying it.
Definition: DataLayout.h:110
TypeSize getTypeAllocSize(Type *Ty) const
Returns the offset in bytes between successive objects of the specified type, including alignment pad...
Definition: DataLayout.h:504
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:510
bool isDeclaration() const
Return true if the primary definition of this global value is outside of the current translation unit...
Definition: Globals.cpp:274
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:655
bool hasCommonLinkage() const
Definition: GlobalValue.h:531
Type * getValueType() const
Definition: GlobalValue.h:296
static const MCBinaryExpr * createAdd(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx)
Definition: MCExpr.h:536
static const MCConstantExpr * create(int64_t Value, MCContext &Ctx, bool PrintInHex=false, unsigned SizeInBytes=0)
Definition: MCExpr.cpp:194
Context object for machine code objects.
Definition: MCContext.h:76
MCSectionELF * getELFSection(const Twine &Section, unsigned Type, unsigned Flags)
Definition: MCContext.h:567
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:35
MCContext & getContext() const
Instances of this class represent a uniqued identifier for a section in the current translation unit.
Definition: MCSection.h:39
Streaming machine code generation interface.
Definition: MCStreamer.h:212
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx)
Definition: MCExpr.h:397
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:40
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
const DataLayout & getDataLayout() const
Get the data layout for the module's target platform.
Definition: Module.h:287
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
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1209
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.
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:76
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:302
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
@ SHF_MERGE
Definition: ELF.h:1163
@ SHF_ALLOC
Definition: ELF.h:1157
@ SHF_WRITE
Definition: ELF.h:1154
@ SHT_PROGBITS
Definition: ELF.h:1063
@ SHT_NOBITS
Definition: ELF.h:1070
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:456
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39