LLVM  9.0.0svn
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) {
32 
33  SmallDataSection = getContext().getELFSection(
35  SmallBSSSection = getContext().getELFSection(".sbss", ELF::SHT_NOBITS,
37 }
38 
39 // A address must be loaded from a small section if its size is less than the
40 // small section size threshold. Data in this section must be addressed using
41 // gp_rel operator.
42 static bool isInSmallSection(uint64_t Size) {
43  // gcc has traditionally not treated zero-sized objects as small data, so this
44  // is effectively part of the ABI.
45  return Size > 0 && Size <= SSThreshold;
46 }
47 
48 // Return true if this global address should be placed into small data/bss
49 // section.
50 bool LanaiTargetObjectFile::isGlobalInSmallSection(
51  const GlobalObject *GO, const TargetMachine &TM) const {
52  if (GO == nullptr) return TM.getCodeModel() == CodeModel::Small;
53 
54  // We first check the case where global is a declaration, because finding
55  // section kind using getKindForGlobal() is only allowed for global
56  // definitions.
58  return isGlobalInSmallSectionImpl(GO, TM);
59 
60  return isGlobalInSmallSection(GO, TM, getKindForGlobal(GO, TM));
61 }
62 
63 // Return true if this global address should be placed into small data/bss
64 // section.
65 bool LanaiTargetObjectFile::isGlobalInSmallSection(const GlobalObject *GO,
66  const TargetMachine &TM,
67  SectionKind Kind) const {
68  return isGlobalInSmallSectionImpl(GO, TM);
69 }
70 
71 // Return true if this global address should be placed into small data/bss
72 // section. This method does all the work, except for checking the section
73 // kind.
74 bool LanaiTargetObjectFile::isGlobalInSmallSectionImpl(
75  const GlobalObject *GO, const TargetMachine &TM) const {
76  const auto *GVA = dyn_cast<GlobalVariable>(GO);
77 
78  // If not a GlobalVariable, only consider the code model.
79  if (!GVA) return TM.getCodeModel() == CodeModel::Small;
80 
81  // Global values placed in sections starting with .ldata do not fit in
82  // 21-bits, so always use large memory access for them. FIXME: This is a
83  // workaround for a tool limitation.
84  if (GVA->getSection().startswith(".ldata"))
85  return false;
86 
87  if (TM.getCodeModel() == CodeModel::Small)
88  return true;
89 
90  if (GVA->hasLocalLinkage())
91  return false;
92 
93  if (((GVA->hasExternalLinkage() && GVA->isDeclaration()) ||
94  GVA->hasCommonLinkage()))
95  return false;
96 
97  Type *Ty = GVA->getValueType();
98  return isInSmallSection(
99  GVA->getParent()->getDataLayout().getTypeAllocSize(Ty));
100 }
101 
103  const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
104  // Handle Small Section classification here.
105  if (Kind.isBSS() && isGlobalInSmallSection(GO, TM, Kind))
106  return SmallBSSSection;
107  if (Kind.isData() && isGlobalInSmallSection(GO, TM, Kind))
108  return SmallDataSection;
109 
110  // Otherwise, we work the same as ELF.
112 }
113 
114 /// Return true if this constant should be placed into small data section.
116  const Constant *CN) const {
117  return isInSmallSection(DL.getTypeAllocSize(CN->getType()));
118 }
119 
121  SectionKind Kind,
122  const Constant *C,
123  unsigned &Align) const {
124  if (isConstantInSmallSection(DL, C))
125  return SmallDataSection;
126 
127  // Otherwise, we work the same as ELF.
128  return TargetLoweringObjectFileELF::getSectionForConstant(DL, Kind, C, Align);
129 }
uint64_t CallInst * C
Instances of this class represent a uniqued identifier for a section in the current translation unit...
Definition: MCSection.h:38
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:110
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
This class represents lattice values for constants.
Definition: AllocatorList.h:23
bool hasAvailableExternallyLinkage() const
Definition: GlobalValue.h:422
static cl::opt< unsigned > SSThreshold("lanai-ssection-threshold", cl::Hidden, cl::desc("Small data and bss section threshold size (default=0)"), cl::init(0))
MCSection * getSectionForConstant(const DataLayout &DL, SectionKind Kind, const Constant *C, unsigned &Align) 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
Context object for machine code objects.
Definition: MCContext.h:62
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:244
MCSection * SelectSectionForGlobal(const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const override
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:422
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:45
This is an important base class in LLVM.
Definition: Constant.h:41
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
bool isBSS() const
Definition: SectionKind.h:159
unsigned UseInitArray
UseInitArray - Use .init_array instead of .ctors for static constructors.
static bool isInSmallSection(uint64_t Size)
CodeModel::Model getCodeModel() const
Returns the code model.
uint64_t getTypeAllocSize(Type *Ty) const
Returns the offset in bytes between successive objects of the specified type, including alignment pad...
Definition: DataLayout.h:438
TargetOptions Options
Definition: TargetMachine.h:96
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:322
uint32_t Size
Definition: Profile.cpp:46
bool isDeclaration() const
Return true if the primary definition of this global value is outside of the current translation unit...
Definition: Globals.cpp:205
MCSection * getSectionForConstant(const DataLayout &DL, SectionKind Kind, const Constant *C, unsigned &Align) const override
Given a constant with the SectionKind, return a section that it should be placed in.
const unsigned Kind
MCSectionELF * getELFSection(const Twine &Section, unsigned Type, unsigned Flags)
Definition: MCContext.h:388
bool isData() const
Definition: SectionKind.h:165
void Initialize(MCContext &Ctx, const TargetMachine &TM) override
This method must be called before any actual lowering is done.
Primary interface to the complete machine description for the target machine.
Definition: TargetMachine.h:58
static SectionKind getKindForGlobal(const GlobalObject *GO, const TargetMachine &TM)
Classify the specified global variable into a set of target independent categories embodied in Sectio...
void Initialize(MCContext &Ctx, const TargetMachine &TM) override
This method must be called before any actual lowering is done.