mark "addr" as having type "iPTR", eliminating some type comparisons
[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/MCSectionELF.h"
15 #include "llvm/Target/TargetData.h"
16 #include "llvm/Target/TargetMachine.h"
17 #include "llvm/Support/CommandLine.h"
18 using namespace llvm;
19
20 static cl::opt<unsigned>
21 SSThreshold("mips-ssection-threshold", cl::Hidden,
22             cl::desc("Small data and bss section threshold size (default=8)"),
23             cl::init(8));
24
25 void MipsTargetObjectFile::Initialize(MCContext &Ctx, const TargetMachine &TM){
26   TargetLoweringObjectFileELF::Initialize(Ctx, TM);
27  
28   SmallDataSection =
29     getELFSection(".sdata", MCSectionELF::SHT_PROGBITS,
30                   MCSectionELF::SHF_WRITE | MCSectionELF::SHF_ALLOC,
31                   SectionKind::getDataRel());
32   
33   SmallBSSSection =
34     getELFSection(".sbss", MCSectionELF::SHT_NOBITS,
35                   MCSectionELF::SHF_WRITE | MCSectionELF::SHF_ALLOC,
36                   SectionKind::getBSS());
37   
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   return Size > 0 && Size <= SSThreshold;
45 }
46
47 bool MipsTargetObjectFile::IsGlobalInSmallSection(const GlobalValue *GV,
48                                                 const TargetMachine &TM) const {
49   if (GV->isDeclaration() || GV->hasAvailableExternallyLinkage())
50     return false;
51   
52   return IsGlobalInSmallSection(GV, TM, getKindForGlobal(GV, TM));
53 }
54
55 /// IsGlobalInSmallSection - Return true if this global address should be
56 /// placed into small data/bss section.
57 bool MipsTargetObjectFile::
58 IsGlobalInSmallSection(const GlobalValue *GV, const TargetMachine &TM,
59                        SectionKind Kind) const {
60
61   // Only use small section for non linux targets.
62   const MipsSubtarget &Subtarget = TM.getSubtarget<MipsSubtarget>();
63   if (Subtarget.isLinux())
64     return false;
65
66   // Only global variables, not functions.
67   const GlobalVariable *GVA = dyn_cast<GlobalVariable>(GV);
68   if (!GVA)
69     return false;
70   
71   // We can only do this for datarel or BSS objects for now.
72   if (!Kind.isBSS() && !Kind.isDataRel())
73     return false;
74   
75   // If this is a internal constant string, there is a special
76   // section for it, but not in small data/bss.
77   if (Kind.isMergeable1ByteCString())
78     return false;
79
80   const Type *Ty = GV->getType()->getElementType();
81   return IsInSmallSection(TM.getTargetData()->getTypeAllocSize(Ty));
82 }
83
84
85
86 const MCSection *MipsTargetObjectFile::
87 SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
88                        Mangler *Mang, const TargetMachine &TM) const {
89   // TODO: Could also support "weak" symbols as well with ".gnu.linkonce.s.*"
90   // sections?
91   
92   // Handle Small Section classification here.
93   if (Kind.isBSS() && IsGlobalInSmallSection(GV, TM, Kind))
94     return SmallBSSSection;
95   if (Kind.isDataNoRel() && IsGlobalInSmallSection(GV, TM, Kind))
96     return SmallDataSection;
97   
98   // Otherwise, we work the same as ELF.
99   return TargetLoweringObjectFileELF::SelectSectionForGlobal(GV, Kind, Mang,TM);
100 }