LLVM  mainline
HexagonTargetObjectFile.cpp
Go to the documentation of this file.
00001 //===-- HexagonTargetObjectFile.cpp - Hexagon asm properties --------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file contains the declarations of the HexagonTargetAsmInfo properties.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "HexagonTargetObjectFile.h"
00015 #include "HexagonSubtarget.h"
00016 #include "HexagonTargetMachine.h"
00017 #include "llvm/IR/DataLayout.h"
00018 #include "llvm/IR/DerivedTypes.h"
00019 #include "llvm/IR/Function.h"
00020 #include "llvm/IR/GlobalVariable.h"
00021 #include "llvm/MC/MCContext.h"
00022 #include "llvm/Support/CommandLine.h"
00023 #include "llvm/Support/ELF.h"
00024 
00025 using namespace llvm;
00026 
00027 static cl::opt<int> SmallDataThreshold("hexagon-small-data-threshold",
00028                                 cl::init(8), cl::Hidden,
00029                 cl::desc("The maximum size of an object in the sdata section"));
00030 
00031 void HexagonTargetObjectFile::Initialize(MCContext &Ctx,
00032                                          const TargetMachine &TM) {
00033   TargetLoweringObjectFileELF::Initialize(Ctx, TM);
00034   InitializeELF(TM.Options.UseInitArray);
00035 
00036   SmallDataSection = getContext().getELFSection(
00037       ".sdata", ELF::SHT_PROGBITS, ELF::SHF_WRITE | ELF::SHF_ALLOC);
00038   SmallBSSSection = getContext().getELFSection(".sbss", ELF::SHT_NOBITS,
00039                                                ELF::SHF_WRITE | ELF::SHF_ALLOC);
00040 }
00041 
00042 // sdata/sbss support taken largely from the MIPS Backend.
00043 static bool IsInSmallSection(uint64_t Size) {
00044   return Size > 0 && Size <= (uint64_t)SmallDataThreshold;
00045 }
00046 
00047 bool HexagonTargetObjectFile::IsSmallDataEnabled () const {
00048   return SmallDataThreshold > 0;
00049 }
00050 
00051 /// IsGlobalInSmallSection - Return true if this global value should be
00052 /// placed into small data/bss section.
00053 bool HexagonTargetObjectFile::IsGlobalInSmallSection(const GlobalValue *GV,
00054                                                 const TargetMachine &TM) const {
00055   // If the primary definition of this global value is outside the current
00056   // translation unit or the global value is available for inspection but not
00057   // emission, then do nothing.
00058   if (GV->isDeclaration() || GV->hasAvailableExternallyLinkage())
00059     return false;
00060 
00061   // Otherwise, Check if GV should be in sdata/sbss, when normally it would end
00062   // up in getKindForGlobal(GV, TM).
00063   return IsGlobalInSmallSection(GV, TM, getKindForGlobal(GV, TM));
00064 }
00065 
00066 /// IsGlobalInSmallSection - Return true if this global value should be
00067 /// placed into small data/bss section.
00068 bool HexagonTargetObjectFile::
00069 IsGlobalInSmallSection(const GlobalValue *GV, const TargetMachine &TM,
00070                        SectionKind Kind) const {
00071   // Only global variables, not functions.
00072   const GlobalVariable *GVA = dyn_cast<GlobalVariable>(GV);
00073   if (!GVA)
00074     return false;
00075 
00076   if (Kind.isBSS() || Kind.isDataNoRel() || Kind.isCommon()) {
00077     Type *Ty = GV->getType()->getElementType();
00078     return IsInSmallSection(TM.getDataLayout()->getTypeAllocSize(Ty));
00079   }
00080 
00081   return false;
00082 }
00083 
00084 MCSection *
00085 HexagonTargetObjectFile::SelectSectionForGlobal(const GlobalValue *GV,
00086                                                 SectionKind Kind, Mangler &Mang,
00087                                                 const TargetMachine &TM) const {
00088 
00089   // Handle Small Section classification here.
00090   if (Kind.isBSS() && IsGlobalInSmallSection(GV, TM, Kind))
00091     return SmallBSSSection;
00092   if (Kind.isDataNoRel() && IsGlobalInSmallSection(GV, TM, Kind))
00093     return SmallDataSection;
00094 
00095   // Otherwise, we work the same as ELF.
00096   return TargetLoweringObjectFileELF::SelectSectionForGlobal(GV, Kind, Mang,TM);
00097 }