blob: 4edb4f849dad863ec734c4c58899a6de302d1230 [file] [log] [blame]
Jacques Pienaarfcef3e42016-03-28 13:09:54 +00001//
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
9#include "LanaiTargetObjectFile.h"
10
11#include "LanaiSubtarget.h"
12#include "LanaiTargetMachine.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"
18#include "llvm/Support/CommandLine.h"
19#include "llvm/Support/ELF.h"
20#include "llvm/Target/TargetMachine.h"
21
22using namespace llvm;
23
24static cl::opt<unsigned> SSThreshold(
25 "lanai-ssection-threshold", cl::Hidden,
26 cl::desc("Small data and bss section threshold size (default=0)"),
27 cl::init(0));
28
29void LanaiTargetObjectFile::Initialize(MCContext &Ctx,
30 const TargetMachine &TM) {
31 TargetLoweringObjectFileELF::Initialize(Ctx, TM);
32 InitializeELF(TM.Options.UseInitArray);
33
34 SmallDataSection = getContext().getELFSection(
35 ".sdata", ELF::SHT_PROGBITS, ELF::SHF_WRITE | ELF::SHF_ALLOC);
36 SmallBSSSection = getContext().getELFSection(".sbss", ELF::SHT_NOBITS,
37 ELF::SHF_WRITE | ELF::SHF_ALLOC);
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.
43static 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.
51bool LanaiTargetObjectFile::isGlobalInSmallSection(
Peter Collingbourne67335642016-10-24 19:23:39 +000052 const GlobalObject *GO, const TargetMachine &TM) const {
Jacques Pienaarfcef3e42016-03-28 13:09:54 +000053 // We first check the case where global is a declaration, because finding
54 // section kind using getKindForGlobal() is only allowed for global
55 // definitions.
Peter Collingbourne67335642016-10-24 19:23:39 +000056 if (GO->isDeclaration() || GO->hasAvailableExternallyLinkage())
57 return isGlobalInSmallSectionImpl(GO, TM);
Jacques Pienaarfcef3e42016-03-28 13:09:54 +000058
Peter Collingbourne67335642016-10-24 19:23:39 +000059 return isGlobalInSmallSection(GO, TM, getKindForGlobal(GO, TM));
Jacques Pienaarfcef3e42016-03-28 13:09:54 +000060}
61
62// Return true if this global address should be placed into small data/bss
63// section.
Peter Collingbourne67335642016-10-24 19:23:39 +000064bool LanaiTargetObjectFile::isGlobalInSmallSection(const GlobalObject *GO,
Jacques Pienaarfcef3e42016-03-28 13:09:54 +000065 const TargetMachine &TM,
66 SectionKind Kind) const {
Peter Collingbourne67335642016-10-24 19:23:39 +000067 return (isGlobalInSmallSectionImpl(GO, TM) &&
Jacques Pienaarfcef3e42016-03-28 13:09:54 +000068 (Kind.isData() || Kind.isBSS() || Kind.isCommon()));
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.
74bool LanaiTargetObjectFile::isGlobalInSmallSectionImpl(
Peter Collingbourne67335642016-10-24 19:23:39 +000075 const GlobalObject *GO, const TargetMachine & /*TM*/) const {
Jacques Pienaarfcef3e42016-03-28 13:09:54 +000076 // Only global variables, not functions.
Peter Collingbourne67335642016-10-24 19:23:39 +000077 const auto *GVA = dyn_cast<GlobalVariable>(GO);
Jacques Pienaarfcef3e42016-03-28 13:09:54 +000078 if (!GVA)
79 return false;
80
Peter Collingbourne67335642016-10-24 19:23:39 +000081 if (GVA->hasLocalLinkage())
Jacques Pienaarfcef3e42016-03-28 13:09:54 +000082 return false;
83
Peter Collingbourne67335642016-10-24 19:23:39 +000084 if (((GVA->hasExternalLinkage() && GVA->isDeclaration()) ||
85 GVA->hasCommonLinkage()))
Jacques Pienaarfcef3e42016-03-28 13:09:54 +000086 return false;
87
Peter Collingbourne67335642016-10-24 19:23:39 +000088 Type *Ty = GVA->getValueType();
Jacques Pienaarfcef3e42016-03-28 13:09:54 +000089 return isInSmallSection(
Peter Collingbourne67335642016-10-24 19:23:39 +000090 GVA->getParent()->getDataLayout().getTypeAllocSize(Ty));
Jacques Pienaarfcef3e42016-03-28 13:09:54 +000091}
92
Eric Christopher4367c7f2016-09-16 07:33:15 +000093MCSection *LanaiTargetObjectFile::SelectSectionForGlobal(
Peter Collingbourne67335642016-10-24 19:23:39 +000094 const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
Jacques Pienaarfcef3e42016-03-28 13:09:54 +000095 // Handle Small Section classification here.
Peter Collingbourne67335642016-10-24 19:23:39 +000096 if (Kind.isBSS() && isGlobalInSmallSection(GO, TM, Kind))
Jacques Pienaarfcef3e42016-03-28 13:09:54 +000097 return SmallBSSSection;
Peter Collingbourne67335642016-10-24 19:23:39 +000098 if (Kind.isData() && isGlobalInSmallSection(GO, TM, Kind))
Jacques Pienaarfcef3e42016-03-28 13:09:54 +000099 return SmallDataSection;
100
101 // Otherwise, we work the same as ELF.
Peter Collingbourne67335642016-10-24 19:23:39 +0000102 return TargetLoweringObjectFileELF::SelectSectionForGlobal(GO, Kind, TM);
Jacques Pienaarfcef3e42016-03-28 13:09:54 +0000103}
104
105/// Return true if this constant should be placed into small data section.
Jacques Pienaar5ffdef52016-05-20 21:41:53 +0000106bool LanaiTargetObjectFile::isConstantInSmallSection(const DataLayout &DL,
107 const Constant *CN) const {
Jacques Pienaarfcef3e42016-03-28 13:09:54 +0000108 return isInSmallSection(DL.getTypeAllocSize(CN->getType()));
109}
110
111MCSection *LanaiTargetObjectFile::getSectionForConstant(const DataLayout &DL,
112 SectionKind Kind,
113 const Constant *C,
114 unsigned &Align) const {
115 if (isConstantInSmallSection(DL, C))
116 return SmallDataSection;
117
118 // Otherwise, we work the same as ELF.
119 return TargetLoweringObjectFileELF::getSectionForConstant(DL, Kind, C, Align);
120}