LLVM  6.0.0svn
LanaiTargetObjectFile.cpp
Go to the documentation of this file.
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 
10 
11 #include "LanaiSubtarget.h"
12 #include "LanaiTargetMachine.h"
13 #include "llvm/BinaryFormat/ELF.h"
14 #include "llvm/IR/DataLayout.h"
15 #include "llvm/IR/DerivedTypes.h"
16 #include "llvm/IR/GlobalVariable.h"
17 #include "llvm/MC/MCContext.h"
18 #include "llvm/MC/MCSectionELF.h"
21 
22 using namespace llvm;
23 
25  "lanai-ssection-threshold", cl::Hidden,
26  cl::desc("Small data and bss section threshold size (default=0)"),
27  cl::init(0));
28 
30  const TargetMachine &TM) {
33 
34  SmallDataSection = getContext().getELFSection(
36  SmallBSSSection = getContext().getELFSection(".sbss", ELF::SHT_NOBITS,
38 }
39 
40 // A address must be loaded from a small section if its size is less than the
41 // small section size threshold. Data in this section must be addressed using
42 // gp_rel operator.
43 static bool isInSmallSection(uint64_t Size) {
44  // gcc has traditionally not treated zero-sized objects as small data, so this
45  // is effectively part of the ABI.
46  return Size > 0 && Size <= SSThreshold;
47 }
48 
49 // Return true if this global address should be placed into small data/bss
50 // section.
51 bool LanaiTargetObjectFile::isGlobalInSmallSection(
52  const GlobalObject *GO, const TargetMachine &TM) const {
53  if (GO == nullptr)
54  return false;
55 
56  // We first check the case where global is a declaration, because finding
57  // section kind using getKindForGlobal() is only allowed for global
58  // definitions.
60  return isGlobalInSmallSectionImpl(GO, TM);
61 
62  return isGlobalInSmallSection(GO, TM, getKindForGlobal(GO, TM));
63 }
64 
65 // Return true if this global address should be placed into small data/bss
66 // section.
67 bool LanaiTargetObjectFile::isGlobalInSmallSection(const GlobalObject *GO,
68  const TargetMachine &TM,
69  SectionKind Kind) const {
70  return (isGlobalInSmallSectionImpl(GO, TM) &&
71  (Kind.isData() || Kind.isBSS() || Kind.isCommon()));
72 }
73 
74 // Return true if this global address should be placed into small data/bss
75 // section. This method does all the work, except for checking the section
76 // kind.
77 bool LanaiTargetObjectFile::isGlobalInSmallSectionImpl(
78  const GlobalObject *GO, const TargetMachine &TM) const {
79  // Only global variables, not functions.
80  const auto *GVA = dyn_cast<GlobalVariable>(GO);
81  if (!GVA)
82  return false;
83 
84  // Global values placed in sections starting with .ldata do not fit in
85  // 21-bits, so always use large memory access for them. FIXME: This is a
86  // workaround for a tool limitation.
87  if (GVA->getSection().startswith(".ldata"))
88  return false;
89 
90  if (TM.getCodeModel() == CodeModel::Small)
91  return true;
92 
93  if (GVA->hasLocalLinkage())
94  return false;
95 
96  if (((GVA->hasExternalLinkage() && GVA->isDeclaration()) ||
97  GVA->hasCommonLinkage()))
98  return false;
99 
100  Type *Ty = GVA->getValueType();
101  return isInSmallSection(
102  GVA->getParent()->getDataLayout().getTypeAllocSize(Ty));
103 }
104 
106  const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
107  // Handle Small Section classification here.
108  if (Kind.isBSS() && isGlobalInSmallSection(GO, TM, Kind))
109  return SmallBSSSection;
110  if (Kind.isData() && isGlobalInSmallSection(GO, TM, Kind))
111  return SmallDataSection;
112 
113  // Otherwise, we work the same as ELF.
115 }
116 
117 /// Return true if this constant should be placed into small data section.
119  const Constant *CN) const {
120  return isInSmallSection(DL.getTypeAllocSize(CN->getType()));
121 }
122 
124  SectionKind Kind,
125  const Constant *C,
126  unsigned &Align) const {
127  if (isConstantInSmallSection(DL, C))
128  return SmallDataSection;
129 
130  // Otherwise, we work the same as ELF.
131  return TargetLoweringObjectFileELF::getSectionForConstant(DL, Kind, C, Align);
132 }
uint64_t CallInst * C
Instances of this class represent a uniqued identifier for a section in the current translation unit...
Definition: MCSection.h:39
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:109
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
bool hasAvailableExternallyLinkage() const
Definition: GlobalValue.h:414
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.
virtual void Initialize(MCContext &ctx, const TargetMachine &TM)
This method must be called before any actual lowering is done.
MCSection * SelectSectionForGlobal(const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const override
Context object for machine code objects.
Definition: MCContext.h:59
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:245
MCSection * SelectSectionForGlobal(const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const override
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:406
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:46
This is an important base class in LLVM.
Definition: Constant.h:42
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:23
bool isBSS() const
Definition: SectionKind.h:160
unsigned UseInitArray
UseInitArray - Use .init_array instead of .ctors for static constructors.
bool isCommon() const
Definition: SectionKind.h:164
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:403
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:323
bool isDeclaration() const
Return true if the primary definition of this global value is outside of the current translation unit...
Definition: Globals.cpp:201
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:379
bool isData() const
Definition: SectionKind.h:166
constexpr char Size[]
Key for Kernel::Arg::Metadata::mSize.
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:57
static SectionKind getKindForGlobal(const GlobalObject *GO, const TargetMachine &TM)
Classify the specified global variable into a set of target independent categories embodied in Sectio...