LLVM API Documentation

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MipsTargetObjectFile.cpp
Go to the documentation of this file.
1 //===-- MipsTargetObjectFile.cpp - Mips Object Files ----------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "MipsTargetObjectFile.h"
11 #include "MipsSubtarget.h"
12 #include "llvm/IR/DataLayout.h"
13 #include "llvm/IR/DerivedTypes.h"
14 #include "llvm/IR/GlobalVariable.h"
15 #include "llvm/MC/MCContext.h"
16 #include "llvm/MC/MCSectionELF.h"
18 #include "llvm/Support/ELF.h"
20 using namespace llvm;
21 
22 static cl::opt<unsigned>
23 SSThreshold("mips-ssection-threshold", cl::Hidden,
24  cl::desc("Small data and bss section threshold size (default=8)"),
25  cl::init(8));
26 
30 
31  SmallDataSection =
32  getContext().getELFSection(".sdata", ELF::SHT_PROGBITS,
33  ELF::SHF_WRITE |ELF::SHF_ALLOC,
35 
36  SmallBSSSection =
37  getContext().getELFSection(".sbss", ELF::SHT_NOBITS,
38  ELF::SHF_WRITE |ELF::SHF_ALLOC,
40 
41  // Register info information
42  const MipsSubtarget &Subtarget = TM.getSubtarget<MipsSubtarget>();
43  if (Subtarget.isABI_N64() || Subtarget.isABI_N32())
44  ReginfoSection =
45  getContext().getELFSection(".MIPS.options",
46  ELF::SHT_MIPS_OPTIONS,
47  ELF::SHF_ALLOC |ELF::SHF_MIPS_NOSTRIP,
49  else
50  ReginfoSection =
51  getContext().getELFSection(".reginfo",
52  ELF::SHT_MIPS_REGINFO,
53  ELF::SHF_ALLOC,
55 }
56 
57 // A address must be loaded from a small section if its size is less than the
58 // small section size threshold. Data in this section must be addressed using
59 // gp_rel operator.
60 static bool IsInSmallSection(uint64_t Size) {
61  return Size > 0 && Size <= SSThreshold;
62 }
63 
65  const TargetMachine &TM) const {
67  return false;
68 
69  return IsGlobalInSmallSection(GV, TM, getKindForGlobal(GV, TM));
70 }
71 
72 /// IsGlobalInSmallSection - Return true if this global address should be
73 /// placed into small data/bss section.
76  SectionKind Kind) const {
77 
78  const MipsSubtarget &Subtarget = TM.getSubtarget<MipsSubtarget>();
79 
80  // Return if small section is not available.
81  if (!Subtarget.useSmallSection())
82  return false;
83 
84  // Only global variables, not functions.
85  const GlobalVariable *GVA = dyn_cast<GlobalVariable>(GV);
86  if (!GVA)
87  return false;
88 
89  // We can only do this for datarel or BSS objects for now.
90  if (!Kind.isBSS() && !Kind.isDataRel())
91  return false;
92 
93  // If this is a internal constant string, there is a special
94  // section for it, but not in small data/bss.
95  if (Kind.isMergeable1ByteCString())
96  return false;
97 
98  Type *Ty = GV->getType()->getElementType();
100 }
101 
102 
103 
106  Mangler *Mang, const TargetMachine &TM) const {
107  // TODO: Could also support "weak" symbols as well with ".gnu.linkonce.s.*"
108  // sections?
109 
110  // Handle Small Section classification here.
111  if (Kind.isBSS() && IsGlobalInSmallSection(GV, TM, Kind))
112  return SmallBSSSection;
113  if (Kind.isDataNoRel() && IsGlobalInSmallSection(GV, TM, Kind))
114  return SmallDataSection;
115 
116  // Otherwise, we work the same as ELF.
117  return TargetLoweringObjectFileELF::SelectSectionForGlobal(GV, Kind, Mang,TM);
118 }
static SectionKind getKindForGlobal(const GlobalValue *GV, const TargetMachine &TM)
enable_if_c<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:266
const MCSectionELF * getELFSection(StringRef Section, unsigned Type, unsigned Flags, SectionKind Kind)
Definition: MCContext.cpp:244
static SectionKind getDataRel()
Definition: SectionKind.h:229
bool isBSS() const
Definition: SectionKind.h:176
static bool IsInSmallSection(uint64_t Size)
bool hasAvailableExternallyLinkage() const
Definition: GlobalValue.h:195
static SectionKind getBSS()
Definition: SectionKind.h:225
virtual void Initialize(MCContext &ctx, const TargetMachine &TM)
bool isABI_N64() const
bool isABI_N32() const
Type * getElementType() const
Definition: DerivedTypes.h:319
bool isDataNoRel() const
Definition: SectionKind.h:190
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:314
bool isMergeable1ByteCString() const
Definition: SectionKind.h:149
void Initialize(MCContext &Ctx, const TargetMachine &TM)
const STC & getSubtarget() const
uint64_t getTypeAllocSize(Type *Ty) const
Definition: DataLayout.h:326
static SectionKind getMetadata()
Definition: SectionKind.h:207
PointerType * getType() const
getType - Global values are always pointers.
Definition: GlobalValue.h:107
virtual const MCSection * SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind, Mangler *Mang, const TargetMachine &TM) const
bool isDeclaration() const
Definition: Globals.cpp:66
static cl::opt< unsigned > SSThreshold("mips-ssection-threshold", cl::Hidden, cl::desc("Small data and bss section threshold size (default=8)"), cl::init(8))
virtual const DataLayout * getDataLayout() const
bool IsGlobalInSmallSection(const GlobalValue *GV, const TargetMachine &TM, SectionKind Kind) const
bool useSmallSection() const
const MCSection * SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind, Mangler *Mang, const TargetMachine &TM) const
bool isDataRel() const
Definition: SectionKind.h:182