LLVM  14.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 "LanaiSubtarget.h"
11 #include "LanaiTargetMachine.h"
12 #include "llvm/BinaryFormat/ELF.h"
13 #include "llvm/IR/DataLayout.h"
14 #include "llvm/IR/DerivedTypes.h"
15 #include "llvm/IR/GlobalVariable.h"
16 #include "llvm/MC/MCContext.h"
17 #include "llvm/MC/MCSectionELF.h"
20 
21 using namespace llvm;
22 
24  "lanai-ssection-threshold", cl::Hidden,
25  cl::desc("Small data and bss section threshold size (default=0)"),
26  cl::init(0));
27 
29  const TargetMachine &TM) {
31 
32  SmallDataSection = getContext().getELFSection(
34  SmallBSSSection = getContext().getELFSection(".sbss", ELF::SHT_NOBITS,
36 }
37 
38 // A address must be loaded from a small section if its size is less than the
39 // small section size threshold. Data in this section must be addressed using
40 // gp_rel operator.
42  // gcc has traditionally not treated zero-sized objects as small data, so this
43  // is effectively part of the ABI.
44  return Size > 0 && Size <= SSThreshold;
45 }
46 
47 // Return true if this global address should be placed into small data/bss
48 // section.
49 bool LanaiTargetObjectFile::isGlobalInSmallSection(
50  const GlobalObject *GO, const TargetMachine &TM) const {
51  if (GO == nullptr) return TM.getCodeModel() == CodeModel::Small;
52 
53  // We first check the case where global is a declaration, because finding
54  // section kind using getKindForGlobal() is only allowed for global
55  // definitions.
57  return isGlobalInSmallSectionImpl(GO, TM);
58 
59  return isGlobalInSmallSection(GO, TM, getKindForGlobal(GO, TM));
60 }
61 
62 // Return true if this global address should be placed into small data/bss
63 // section.
64 bool LanaiTargetObjectFile::isGlobalInSmallSection(const GlobalObject *GO,
65  const TargetMachine &TM,
66  SectionKind Kind) const {
67  return isGlobalInSmallSectionImpl(GO, TM);
68 }
69 
70 // Return true if this global address should be placed into small data/bss
71 // section. This method does all the work, except for checking the section
72 // kind.
73 bool LanaiTargetObjectFile::isGlobalInSmallSectionImpl(
74  const GlobalObject *GO, const TargetMachine &TM) const {
75  const auto *GVA = dyn_cast<GlobalVariable>(GO);
76 
77  // If not a GlobalVariable, only consider the code model.
78  if (!GVA) return TM.getCodeModel() == CodeModel::Small;
79 
80  // Global values placed in sections starting with .ldata do not fit in
81  // 21-bits, so always use large memory access for them. FIXME: This is a
82  // workaround for a tool limitation.
83  if (GVA->getSection().startswith(".ldata"))
84  return false;
85 
87  return true;
88 
89  if (GVA->hasLocalLinkage())
90  return false;
91 
92  if (((GVA->hasExternalLinkage() && GVA->isDeclaration()) ||
93  GVA->hasCommonLinkage()))
94  return false;
95 
96  Type *Ty = GVA->getValueType();
97  return isInSmallSection(
98  GVA->getParent()->getDataLayout().getTypeAllocSize(Ty));
99 }
100 
102  const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
103  // Handle Small Section classification here.
104  if (Kind.isBSS() && isGlobalInSmallSection(GO, TM, Kind))
105  return SmallBSSSection;
106  if (Kind.isData() && isGlobalInSmallSection(GO, TM, Kind))
107  return SmallDataSection;
108 
109  // Otherwise, we work the same as ELF.
111 }
112 
113 /// Return true if this constant should be placed into small data section.
115  const Constant *CN) const {
116  return isInSmallSection(DL.getTypeAllocSize(CN->getType()));
117 }
118 
120  const DataLayout &DL, SectionKind Kind, const Constant *C,
121  Align &Alignment) const {
123  return SmallDataSection;
124 
125  // Otherwise, we work the same as ELF.
127  Alignment);
128 }
llvm::Check::Size
@ Size
Definition: FileCheck.h:73
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AllocatorList.h:23
llvm::DataLayout
A parsed version of the target data layout string in and methods for querying it.
Definition: DataLayout.h:113
llvm::TargetLoweringObjectFileELF::SelectSectionForGlobal
MCSection * SelectSectionForGlobal(const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const override
Definition: TargetLoweringObjectFileImpl.cpp:874
llvm::MCContext
Context object for machine code objects.
Definition: MCContext.h:72
MCSectionELF.h
llvm::cl::Hidden
@ Hidden
Definition: CommandLine.h:143
llvm::Type
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
llvm::MCObjectFileInfo::getContext
MCContext & getContext() const
Definition: MCObjectFileInfo.h:235
CommandLine.h
ELF.h
TargetMachine.h
llvm::GlobalValue::isDeclaration
bool isDeclaration() const
Return true if the primary definition of this global value is outside of the current translation unit...
Definition: Globals.cpp:228
C
(vector float) vec_cmpeq(*A, *B) C
Definition: README_ALTIVEC.txt:86
llvm::GlobalObject
Definition: GlobalObject.h:28
MCContext.h
llvm::LanaiTargetObjectFile::getSectionForConstant
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.
Definition: LanaiTargetObjectFile.cpp:119
llvm::MCContext::getELFSection
MCSectionELF * getELFSection(const Twine &Section, unsigned Type, unsigned Flags)
Definition: MCContext.h:540
llvm::CodeModel::Small
@ Small
Definition: CodeGen.h:28
isInSmallSection
static bool isInSmallSection(uint64_t Size)
Definition: LanaiTargetObjectFile.cpp:41
llvm::Align
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39
llvm::lltok::Kind
Kind
Definition: LLToken.h:18
llvm::TargetLoweringObjectFile::getKindForGlobal
static SectionKind getKindForGlobal(const GlobalObject *GO, const TargetMachine &TM)
Classify the specified global variable into a set of target independent categories embodied in Sectio...
Definition: TargetLoweringObjectFile.cpp:202
llvm::LanaiTargetObjectFile::SelectSectionForGlobal
MCSection * SelectSectionForGlobal(const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const override
Definition: LanaiTargetObjectFile.cpp:101
llvm::TargetLoweringObjectFile::TM
const TargetMachine * TM
Definition: TargetLoweringObjectFile.h:67
llvm::cl::opt
Definition: CommandLine.h:1432
llvm::GlobalValue::hasAvailableExternallyLinkage
bool hasAvailableExternallyLinkage() const
Definition: GlobalValue.h:435
llvm::Constant
This is an important base class in LLVM.
Definition: Constant.h:41
uint64_t
llvm::ELF::SHT_PROGBITS
@ SHT_PROGBITS
Definition: ELF.h:916
llvm::ELF::SHF_ALLOC
@ SHF_ALLOC
Definition: ELF.h:994
llvm::cl::init
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:441
llvm::TargetMachine
Primary interface to the complete machine description for the target machine.
Definition: TargetMachine.h:80
llvm::TargetLoweringObjectFileELF::getSectionForConstant
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.
Definition: TargetLoweringObjectFileImpl.cpp:968
llvm::LanaiTargetObjectFile::Initialize
void Initialize(MCContext &Ctx, const TargetMachine &TM) override
This method must be called before any actual lowering is done.
Definition: LanaiTargetObjectFile.cpp:28
DataLayout.h
llvm::Value::getType
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:255
DL
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Definition: AArch64SLSHardening.cpp:76
llvm::LanaiTargetObjectFile::isConstantInSmallSection
bool isConstantInSmallSection(const DataLayout &DL, const Constant *CN) const
Return true if this constant should be placed into small data section.
Definition: LanaiTargetObjectFile.cpp:114
llvm::MCSection
Instances of this class represent a uniqued identifier for a section in the current translation unit.
Definition: MCSection.h:39
LanaiTargetObjectFile.h
llvm::SectionKind
SectionKind - This is a simple POD value that classifies the properties of a section.
Definition: SectionKind.h:22
GlobalVariable.h
llvm::TargetLoweringObjectFileELF::Initialize
void Initialize(MCContext &Ctx, const TargetMachine &TM) override
This method must be called before any actual lowering is done.
Definition: TargetLoweringObjectFileImpl.cpp:116
llvm::TargetMachine::getCodeModel
CodeModel::Model getCodeModel() const
Returns the code model.
Definition: TargetMachine.cpp:74
DerivedTypes.h
TM
const char LLVMTargetMachineRef TM
Definition: PassBuilderBindings.cpp:47
llvm::ELF::SHF_WRITE
@ SHF_WRITE
Definition: ELF.h:991
LanaiTargetMachine.h
llvm::cl::desc
Definition: CommandLine.h:412
SSThreshold
static cl::opt< unsigned > SSThreshold("lanai-ssection-threshold", cl::Hidden, cl::desc("Small data and bss section threshold size (default=0)"), cl::init(0))
llvm::ELF::SHT_NOBITS
@ SHT_NOBITS
Definition: ELF.h:923
LanaiSubtarget.h