Fix lines that have incorrect indentation or exceed 80 columns. There is no change...
[oota-llvm.git] / lib / Target / Mips / MipsTargetObjectFile.cpp
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/DerivedTypes.h"
13 #include "llvm/GlobalVariable.h"
14 #include "llvm/MC/MCContext.h"
15 #include "llvm/MC/MCSectionELF.h"
16 #include "llvm/Target/TargetData.h"
17 #include "llvm/Target/TargetMachine.h"
18 #include "llvm/Support/CommandLine.h"
19 #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
27 void MipsTargetObjectFile::Initialize(MCContext &Ctx, const TargetMachine &TM){
28   TargetLoweringObjectFileELF::Initialize(Ctx, TM);
29
30   SmallDataSection =
31     getContext().getELFSection(".sdata", ELF::SHT_PROGBITS,
32                                ELF::SHF_WRITE |ELF::SHF_ALLOC,
33                                SectionKind::getDataRel());
34
35   SmallBSSSection =
36     getContext().getELFSection(".sbss", ELF::SHT_NOBITS,
37                                ELF::SHF_WRITE |ELF::SHF_ALLOC,
38                                SectionKind::getBSS());
39
40 }
41
42 // A address must be loaded from a small section if its size is less than the
43 // small section size threshold. Data in this section must be addressed using
44 // gp_rel operator.
45 static bool IsInSmallSection(uint64_t Size) {
46   return Size > 0 && Size <= SSThreshold;
47 }
48
49 bool MipsTargetObjectFile::IsGlobalInSmallSection(const GlobalValue *GV,
50                                                   const TargetMachine &TM)
51   const {
52   if (GV->isDeclaration() || GV->hasAvailableExternallyLinkage())
53     return false;
54
55   return IsGlobalInSmallSection(GV, TM, getKindForGlobal(GV, TM));
56 }
57
58 /// IsGlobalInSmallSection - Return true if this global address should be
59 /// placed into small data/bss section.
60 bool MipsTargetObjectFile::
61 IsGlobalInSmallSection(const GlobalValue *GV, const TargetMachine &TM,
62                        SectionKind Kind) const {
63
64   // Only use small section for non linux targets.
65   const MipsSubtarget &Subtarget = TM.getSubtarget<MipsSubtarget>();
66   if (Subtarget.isLinux())
67     return false;
68
69   // Only global variables, not functions.
70   const GlobalVariable *GVA = dyn_cast<GlobalVariable>(GV);
71   if (!GVA)
72     return false;
73
74   // We can only do this for datarel or BSS objects for now.
75   if (!Kind.isBSS() && !Kind.isDataRel())
76     return false;
77
78   // If this is a internal constant string, there is a special
79   // section for it, but not in small data/bss.
80   if (Kind.isMergeable1ByteCString())
81     return false;
82
83   const Type *Ty = GV->getType()->getElementType();
84   return IsInSmallSection(TM.getTargetData()->getTypeAllocSize(Ty));
85 }
86
87
88
89 const MCSection *MipsTargetObjectFile::
90 SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
91                        Mangler *Mang, const TargetMachine &TM) const {
92   // TODO: Could also support "weak" symbols as well with ".gnu.linkonce.s.*"
93   // sections?
94
95   // Handle Small Section classification here.
96   if (Kind.isBSS() && IsGlobalInSmallSection(GV, TM, Kind))
97     return SmallBSSSection;
98   if (Kind.isDataNoRel() && IsGlobalInSmallSection(GV, TM, Kind))
99     return SmallDataSection;
100
101   // Otherwise, we work the same as ELF.
102   return TargetLoweringObjectFileELF::SelectSectionForGlobal(GV, Kind, Mang,
103                                                              TM);
104 }