LLVM 24.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
10#include "LanaiTargetMachine.h"
12#include "llvm/IR/DataLayout.h"
14#include "llvm/MC/MCContext.h"
17
18using namespace llvm;
19
29
30// A address must be loaded from a small section if its size is less than the
31// small section size threshold. Data in this section must be addressed using
32// gp_rel operator.
33static bool isInSmallSection(const LanaiOptions &CLOpts, uint64_t Size) {
34 // gcc has traditionally not treated zero-sized objects as small data, so this
35 // is effectively part of the ABI.
36 return Size > 0 && Size <= CLOpts.ssection_threshold;
37}
38
39// Return true if this global address should be placed into small data/bss
40// section.
41bool LanaiTargetObjectFile::isGlobalInSmallSection(
42 const GlobalObject *GO, const TargetMachine &TM) const {
43 if (GO == nullptr) return TM.getCodeModel() == CodeModel::Small;
44
45 // We first check the case where global is a declaration, because finding
46 // section kind using getKindForGlobal() is only allowed for global
47 // definitions.
49 return isGlobalInSmallSectionImpl(GO, TM);
50
51 return isGlobalInSmallSection(GO, TM, getKindForGlobal(GO, TM));
52}
53
54// Return true if this global address should be placed into small data/bss
55// section.
56bool LanaiTargetObjectFile::isGlobalInSmallSection(const GlobalObject *GO,
57 const TargetMachine &TM,
58 SectionKind Kind) const {
59 return isGlobalInSmallSectionImpl(GO, TM);
60}
61
62// Return true if this global address should be placed into small data/bss
63// section. This method does all the work, except for checking the section
64// kind.
65bool LanaiTargetObjectFile::isGlobalInSmallSectionImpl(
66 const GlobalObject *GO, const TargetMachine &TM) const {
67 const auto *GVA = dyn_cast<GlobalVariable>(GO);
68
69 // If not a GlobalVariable, only consider the code model.
70 if (!GVA) return TM.getCodeModel() == CodeModel::Small;
71
72 // Global values placed in sections starting with .ldata do not fit in
73 // 21-bits, so always use large memory access for them. FIXME: This is a
74 // workaround for a tool limitation.
75 if (GVA->getSection().starts_with(".ldata"))
76 return false;
77
79 return true;
80
81 if (GVA->hasLocalLinkage())
82 return false;
83
84 if (((GVA->hasExternalLinkage() && GVA->isDeclaration()) ||
85 GVA->hasCommonLinkage()))
86 return false;
87
88 Type *Ty = GVA->getValueType();
89 return isInSmallSection(
90 static_cast<const LanaiTargetMachine &>(TM).getCLOpts(),
91 GVA->getDataLayout().getTypeAllocSize(Ty));
92}
93
95 const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
96 // Handle Small Section classification here.
97 if (Kind.isBSS() && isGlobalInSmallSection(GO, TM, Kind))
98 return SmallBSSSection;
99 if (Kind.isData() && isGlobalInSmallSection(GO, TM, Kind))
100 return SmallDataSection;
101
102 // Otherwise, we work the same as ELF.
104}
105
106/// Return true if this constant should be placed into small data section.
108 const Constant *CN) const {
109 return isInSmallSection(
110 static_cast<const LanaiTargetMachine *>(TM)->getCLOpts(),
111 DL.getTypeAllocSize(CN->getType()));
112}
113
115 const DataLayout &DL, SectionKind Kind, const Constant *C, Align &Alignment,
116 const Function *F) const {
118 return SmallDataSection;
119
120 // Otherwise, we work the same as ELF.
122 Alignment, F);
123}
uint64_t uint64_t
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
static bool isInSmallSection(const LanaiOptions &CLOpts, uint64_t Size)
#define F(x, y, z)
Definition MD5.cpp:54
This is an important base class in LLVM.
Definition Constant.h:43
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:64
LLVM_ABI bool isDeclaration() const
Return true if the primary definition of this global value is outside of the current translation unit...
Definition Globals.cpp:408
bool hasAvailableExternallyLinkage() const
MCSection * getSectionForConstant(const DataLayout &DL, SectionKind Kind, const Constant *C, Align &Alignment, const Function *F) const override
Given a constant with the SectionKind, return a section that it should be placed in.
void initialize(MCContext &Ctx, const TargetMachine &TM, const Module &M) 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:550
MCContext & getContext() const
Instances of this class represent a uniqued identifier for a section in the current translation unit.
Definition MCSection.h:580
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:68
SectionKind - This is a simple POD value that classifies the properties of a section.
Definition SectionKind.h:22
MCSection * getSectionForConstant(const DataLayout &DL, SectionKind Kind, const Constant *C, Align &Alignment, const Function *F) 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
void initialize(MCContext &Ctx, const TargetMachine &TM, const Module &M) override
This method must be called before any actual lowering is done.
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.
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:46
Type * getType() const
All values are typed, get the type of this value.
Definition Value.h:257
@ SHF_ALLOC
Definition ELF.h:1259
@ SHF_WRITE
Definition ELF.h:1256
@ SHT_PROGBITS
Definition ELF.h:1157
@ SHT_NOBITS
Definition ELF.h:1164
This is an optimization pass for GlobalISel generic memory operations.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39