LLVM  10.0.0svn
RISCVTargetObjectFile.cpp
Go to the documentation of this file.
1 //===-- RISCVTargetObjectFile.cpp - RISCV Object Info -----------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
10 #include "RISCVTargetMachine.h"
11 #include "llvm/BinaryFormat/ELF.h"
12 #include "llvm/MC/MCContext.h"
13 #include "llvm/MC/MCSectionELF.h"
14 
15 using namespace llvm;
16 
18  const TargetMachine &TM) {
21 
22  SmallDataSection = getContext().getELFSection(
24  SmallBSSSection = getContext().getELFSection(".sbss", ELF::SHT_NOBITS,
26 }
27 
28 // A address must be loaded from a small section if its size is less than the
29 // small section size threshold. Data in this section could be addressed by
30 // using gp_rel operator.
32  // gcc has traditionally not treated zero-sized objects as small data, so this
33  // is effectively part of the ABI.
34  return Size > 0 && Size <= SSThreshold;
35 }
36 
37 // Return true if this global address should be placed into small data/bss
38 // section.
40  const GlobalObject *GO, const TargetMachine &TM) const {
41  // Only global variables, not functions.
42  const GlobalVariable *GVA = dyn_cast<GlobalVariable>(GO);
43  if (!GVA)
44  return false;
45 
46  // If the variable has an explicit section, it is placed in that section.
47  if (GVA->hasSection()) {
48  StringRef Section = GVA->getSection();
49 
50  // Explicitly placing any variable in the small data section overrides
51  // the global -G value.
52  if (Section == ".sdata" || Section == ".sbss")
53  return true;
54 
55  // Otherwise reject putting the variable to small section if it has an
56  // explicit section name.
57  return false;
58  }
59 
60  if (((GVA->hasExternalLinkage() && GVA->isDeclaration()) ||
61  GVA->hasCommonLinkage()))
62  return false;
63 
64  Type *Ty = GVA->getValueType();
65  // It is possible that the type of the global is unsized, i.e. a declaration
66  // of a extern struct. In this case don't presume it is in the small data
67  // section. This happens e.g. when building the FreeBSD kernel.
68  if (!Ty->isSized())
69  return false;
70 
71  return isInSmallSection(
73 }
74 
76  const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
77  // Handle Small Section classification here.
78  if (Kind.isBSS() && isGlobalInSmallSection(GO, TM))
79  return SmallBSSSection;
80  if (Kind.isData() && isGlobalInSmallSection(GO, TM))
81  return SmallDataSection;
82 
83  // Otherwise, we work the same as ELF.
85 }
86 
89  M.getModuleFlagsMetadata(ModuleFlags);
90 
91  for (const auto &MFE : ModuleFlags) {
92  StringRef Key = MFE.Key->getString();
93  if (Key == "SmallDataLimit") {
94  SSThreshold = mdconst::extract<ConstantInt>(MFE.Val)->getZExtValue();
95  break;
96  }
97  }
98 }
99 
100 /// Return true if this constant should be placed into small data section.
102  const DataLayout &DL, const Constant *CN) const {
103  return isInSmallSection(DL.getTypeAllocSize(CN->getType()));
104 }
105 
107  const DataLayout &DL, SectionKind Kind, const Constant *C,
108  unsigned &Align) const {
109  if (isConstantInSmallSection(DL, C))
110  return SmallDataSection;
111 
112  // Otherwise, we work the same as ELF.
113  return TargetLoweringObjectFileELF::getSectionForConstant(DL, Kind, C, Align);
114 }
uint64_t CallInst * C
StringRef getSection() const
Get the custom section of this global if it has one.
Definition: GlobalObject.h:109
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:111
This class represents lattice values for constants.
Definition: AllocatorList.h:23
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:66
bool isSized(SmallPtrSetImpl< Type *> *Visited=nullptr) const
Return true if it makes sense to take the size of this type.
Definition: Type.h:265
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 * 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.
bool isGlobalInSmallSection(const GlobalObject *GO, const TargetMachine &TM) const
Return true if this global address should be placed into small data/bss section.
const DataLayout & getDataLayout() const
Get the data layout for the module&#39;s target platform.
Definition: Module.cpp:369
bool hasCommonLinkage() const
Definition: GlobalValue.h:449
MCSection * SelectSectionForGlobal(const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const override
Context object for machine code objects.
Definition: MCContext.h:65
bool hasExternalLinkage() const
Definition: GlobalValue.h:431
Key
PAL metadata keys.
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:246
void getModuleMetadata(Module &M) override
Get the module-level metadata that the platform cares about.
bool isConstantInSmallSection(const DataLayout &DL, const Constant *CN) const
Return true if this constant should be placed into small data section.
MCSection * SelectSectionForGlobal(const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const override
void getModuleFlagsMetadata(SmallVectorImpl< ModuleFlagEntry > &Flags) const
Returns the module flags in the provided vector.
Definition: Module.cpp:290
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:41
TypeSize getTypeAllocSize(Type *Ty) const
Returns the offset in bytes between successive objects of the specified type, including alignment pad...
Definition: DataLayout.h:486
SectionKind - This is a simple POD value that classifies the properties of a section.
Definition: SectionKind.h:22
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:40
bool isBSS() const
Definition: SectionKind.h:159
unsigned UseInitArray
UseInitArray - Use .init_array instead of .ctors for static constructors.
bool hasSection() const
Check if this global has a custom object file section.
Definition: GlobalObject.h:101
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:837
TargetOptions Options
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:332
Type * getValueType() const
Definition: GlobalValue.h:279
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:231
MCSectionELF * getELFSection(const Twine &Section, unsigned Type, unsigned Flags)
Definition: MCContext.h:424
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:575
bool isData() const
Definition: SectionKind.h:165
Primary interface to the complete machine description for the target machine.
Definition: TargetMachine.h:65
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
bool isInSmallSection(uint64_t Size) const
void Initialize(MCContext &Ctx, const TargetMachine &TM) override
This method must be called before any actual lowering is done.
void Initialize(MCContext &Ctx, const TargetMachine &TM) override
This method must be called before any actual lowering is done.