LLVM 20.0.0git
LanaiTargetObjectFile.cpp
Go to the documentation of this file.
1//
2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
3// See https://llvm.org/LICENSE.txt for license information.
4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5//
6//===----------------------------------------------------------------------===//
7
9
11#include "llvm/IR/DataLayout.h"
13#include "llvm/MC/MCContext.h"
17
18using namespace llvm;
19
21 "lanai-ssection-threshold", cl::Hidden,
22 cl::desc("Small data and bss section threshold size (default=0)"),
23 cl::init(0));
24
26 const TargetMachine &TM) {
28
29 SmallDataSection = getContext().getELFSection(
31 SmallBSSSection = getContext().getELFSection(".sbss", ELF::SHT_NOBITS,
33}
34
35// A address must be loaded from a small section if its size is less than the
36// small section size threshold. Data in this section must be addressed using
37// gp_rel operator.
39 // gcc has traditionally not treated zero-sized objects as small data, so this
40 // is effectively part of the ABI.
41 return Size > 0 && Size <= SSThreshold;
42}
43
44// Return true if this global address should be placed into small data/bss
45// section.
46bool LanaiTargetObjectFile::isGlobalInSmallSection(
47 const GlobalObject *GO, const TargetMachine &TM) const {
48 if (GO == nullptr) return TM.getCodeModel() == CodeModel::Small;
49
50 // We first check the case where global is a declaration, because finding
51 // section kind using getKindForGlobal() is only allowed for global
52 // definitions.
54 return isGlobalInSmallSectionImpl(GO, TM);
55
56 return isGlobalInSmallSection(GO, TM, getKindForGlobal(GO, TM));
57}
58
59// Return true if this global address should be placed into small data/bss
60// section.
61bool LanaiTargetObjectFile::isGlobalInSmallSection(const GlobalObject *GO,
62 const TargetMachine &TM,
63 SectionKind Kind) const {
64 return isGlobalInSmallSectionImpl(GO, TM);
65}
66
67// Return true if this global address should be placed into small data/bss
68// section. This method does all the work, except for checking the section
69// kind.
70bool LanaiTargetObjectFile::isGlobalInSmallSectionImpl(
71 const GlobalObject *GO, const TargetMachine &TM) const {
72 const auto *GVA = dyn_cast<GlobalVariable>(GO);
73
74 // If not a GlobalVariable, only consider the code model.
75 if (!GVA) return TM.getCodeModel() == CodeModel::Small;
76
77 // Global values placed in sections starting with .ldata do not fit in
78 // 21-bits, so always use large memory access for them. FIXME: This is a
79 // workaround for a tool limitation.
80 if (GVA->getSection().starts_with(".ldata"))
81 return false;
82
84 return true;
85
86 if (GVA->hasLocalLinkage())
87 return false;
88
89 if (((GVA->hasExternalLinkage() && GVA->isDeclaration()) ||
90 GVA->hasCommonLinkage()))
91 return false;
92
93 Type *Ty = GVA->getValueType();
94 return isInSmallSection(
95 GVA->getDataLayout().getTypeAllocSize(Ty));
96}
97
99 const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
100 // Handle Small Section classification here.
101 if (Kind.isBSS() && isGlobalInSmallSection(GO, TM, Kind))
102 return SmallBSSSection;
103 if (Kind.isData() && isGlobalInSmallSection(GO, TM, Kind))
104 return SmallDataSection;
105
106 // Otherwise, we work the same as ELF.
108}
109
110/// Return true if this constant should be placed into small data section.
112 const Constant *CN) const {
113 return isInSmallSection(DL.getTypeAllocSize(CN->getType()));
114}
115
117 const DataLayout &DL, SectionKind Kind, const Constant *C,
118 Align &Alignment) const {
120 return SmallDataSection;
121
122 // Otherwise, we work the same as ELF.
124 Alignment);
125}
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
uint64_t Size
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 bool isInSmallSection(uint64_t Size)
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
bool isDeclaration() const
Return true if the primary definition of this global value is outside of the current translation unit...
Definition: Globals.cpp:296
bool hasAvailableExternallyLinkage() const
Definition: GlobalValue.h:512
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 Initialize(MCContext &Ctx, const TargetMachine &TM) override
This method must be called before any actual lowering is done.
MCSection * SelectSectionForGlobal(const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const override
bool isConstantInSmallSection(const DataLayout &DL, const Constant *CN) const
Return true if this constant should be placed into small data section.
Context object for machine code objects.
Definition: MCContext.h:83
MCSectionELF * getELFSection(const Twine &Section, unsigned Type, unsigned Flags)
Definition: MCContext.h:551
MCContext & getContext() const
Instances of this class represent a uniqued identifier for a section in the current translation unit.
Definition: MCSection.h:36
SectionKind - This is a simple POD value that classifies the properties of a section.
Definition: SectionKind.h:22
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
CodeModel::Model getCodeModel() const
Returns the code model.
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
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:1090
@ SHT_NOBITS
Definition: ELF.h:1097
@ SHF_ALLOC
Definition: ELF.h:1188
@ SHF_WRITE
Definition: ELF.h:1185
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