LLVM API Documentation

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 
00035 
00036   SmallDataSection =
00037     getContext().getELFSection(".sdata", ELF::SHT_PROGBITS,
00038                                ELF::SHF_WRITE | ELF::SHF_ALLOC,
00039                                SectionKind::getDataRel());
00040   SmallBSSSection =
00041     getContext().getELFSection(".sbss", ELF::SHT_NOBITS,
00042                                ELF::SHF_WRITE | ELF::SHF_ALLOC,
00043                                SectionKind::getBSS());
00044 }
00045 
00046 // sdata/sbss support taken largely from the MIPS Backend.
00047 static bool IsInSmallSection(uint64_t Size) {
00048   return Size > 0 && Size <= (uint64_t)SmallDataThreshold;
00049 }
00050 
00051 bool HexagonTargetObjectFile::IsSmallDataEnabled () const {
00052   return SmallDataThreshold > 0;
00053 }
00054 
00055 /// IsGlobalInSmallSection - Return true if this global value should be
00056 /// placed into small data/bss section.
00057 bool HexagonTargetObjectFile::IsGlobalInSmallSection(const GlobalValue *GV,
00058                                                 const TargetMachine &TM) const {
00059   // If the primary definition of this global value is outside the current
00060   // translation unit or the global value is available for inspection but not
00061   // emission, then do nothing.
00062   if (GV->isDeclaration() || GV->hasAvailableExternallyLinkage())
00063     return false;
00064 
00065   // Otherwise, Check if GV should be in sdata/sbss, when normally it would end
00066   // up in getKindForGlobal(GV, TM).
00067   return IsGlobalInSmallSection(GV, TM, getKindForGlobal(GV, TM));
00068 }
00069 
00070 /// IsGlobalInSmallSection - Return true if this global value should be
00071 /// placed into small data/bss section.
00072 bool HexagonTargetObjectFile::
00073 IsGlobalInSmallSection(const GlobalValue *GV, const TargetMachine &TM,
00074                        SectionKind Kind) const {
00075   // Only global variables, not functions.
00076   const GlobalVariable *GVA = dyn_cast<GlobalVariable>(GV);
00077   if (!GVA)
00078     return false;
00079 
00080   if (Kind.isBSS() || Kind.isDataNoRel() || Kind.isCommon()) {
00081     Type *Ty = GV->getType()->getElementType();
00082     return IsInSmallSection(
00083         TM.getSubtargetImpl()->getDataLayout()->getTypeAllocSize(Ty));
00084   }
00085 
00086   return false;
00087 }
00088 
00089 const MCSection *
00090 HexagonTargetObjectFile::SelectSectionForGlobal(const GlobalValue *GV,
00091                                                 SectionKind Kind, Mangler &Mang,
00092                                                 const TargetMachine &TM) const {
00093 
00094   // Handle Small Section classification here.
00095   if (Kind.isBSS() && IsGlobalInSmallSection(GV, TM, Kind))
00096     return SmallBSSSection;
00097   if (Kind.isDataNoRel() && IsGlobalInSmallSection(GV, TM, Kind))
00098     return SmallDataSection;
00099 
00100   // Otherwise, we work the same as ELF.
00101   return TargetLoweringObjectFileELF::SelectSectionForGlobal(GV, Kind, Mang,TM);
00102 }