LLVM 17.0.0git
RISCVTargetObjectFile.cpp
Go to the documentation of this file.
1//===-- RISCVTargetObjectFile.cpp - RISCV 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
10#include "RISCVTargetMachine.h"
12#include "llvm/MC/MCContext.h"
14
15using namespace llvm;
16
18 const TargetMachine &TM) {
20
22
23 SmallDataSection = getContext().getELFSection(
25 SmallBSSSection = getContext().getELFSection(".sbss", ELF::SHT_NOBITS,
27}
28
29// A address must be loaded from a small section if its size is less than the
30// small section size threshold. Data in this section could be addressed by
31// using gp_rel operator.
33 // gcc has traditionally not treated zero-sized objects as small data, so this
34 // is effectively part of the ABI.
35 return Size > 0 && Size <= SSThreshold;
36}
37
38// Return true if this global address should be placed into small data/bss
39// section.
41 const GlobalObject *GO, const TargetMachine &TM) const {
42 // Only global variables, not functions.
43 const GlobalVariable *GVA = dyn_cast<GlobalVariable>(GO);
44 if (!GVA)
45 return false;
46
47 // If the variable has an explicit section, it is placed in that section.
48 if (GVA->hasSection()) {
49 StringRef Section = GVA->getSection();
50
51 // Explicitly placing any variable in the small data section overrides
52 // the global -G value.
53 if (Section == ".sdata" || Section == ".sbss")
54 return true;
55
56 // Otherwise reject putting the variable to small section if it has an
57 // explicit section name.
58 return false;
59 }
60
61 if (((GVA->hasExternalLinkage() && GVA->isDeclaration()) ||
62 GVA->hasCommonLinkage()))
63 return false;
64
65 Type *Ty = GVA->getValueType();
66 // It is possible that the type of the global is unsized, i.e. a declaration
67 // of a extern struct. In this case don't presume it is in the small data
68 // section. This happens e.g. when building the FreeBSD kernel.
69 if (!Ty->isSized())
70 return false;
71
72 return isInSmallSection(
74}
75
77 const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
78 // Handle Small Section classification here.
79 if (Kind.isBSS() && isGlobalInSmallSection(GO, TM))
80 return SmallBSSSection;
81 if (Kind.isData() && isGlobalInSmallSection(GO, TM))
82 return SmallDataSection;
83
84 // Otherwise, we work the same as ELF.
86}
87
91 M.getModuleFlagsMetadata(ModuleFlags);
92
93 for (const auto &MFE : ModuleFlags) {
94 StringRef Key = MFE.Key->getString();
95 if (Key == "SmallDataLimit") {
96 SSThreshold = mdconst::extract<ConstantInt>(MFE.Val)->getZExtValue();
97 break;
98 }
99 }
100}
101
102/// Return true if this constant should be placed into small data section.
104 const DataLayout &DL, const Constant *CN) const {
105 return isInSmallSection(DL.getTypeAllocSize(CN->getType()));
106}
107
109 const DataLayout &DL, SectionKind Kind, const Constant *C,
110 Align &Alignment) const {
112 return SmallDataSection;
113
114 // Otherwise, we work the same as ELF.
116 Alignment);
117}
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
uint64_t Size
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:500
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:506
bool isDeclaration() const
Return true if the primary definition of this global value is outside of the current translation unit...
Definition: Globals.cpp:275
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:652
bool hasCommonLinkage() const
Definition: GlobalValue.h:527
Type * getValueType() const
Definition: GlobalValue.h:292
Context object for machine code objects.
Definition: MCContext.h:76
MCSectionELF * getELFSection(const Twine &Section, unsigned Type, unsigned Flags)
Definition: MCContext.h:563
MCContext & getContext() const
Instances of this class represent a uniqued identifier for a section in the current translation unit.
Definition: MCSection.h:39
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.cpp:398
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.
bool isConstantInSmallSection(const DataLayout &DL, const Constant *CN) 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:22
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1200
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:78
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:304
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_ALLOC
Definition: ELF.h:1083
@ SHF_WRITE
Definition: ELF.h:1080
@ SHT_PROGBITS
Definition: ELF.h:995
@ SHT_NOBITS
Definition: ELF.h:1002
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