Jia Liu | b22310f | 2012-02-18 12:03:15 +0000 | [diff] [blame] | 1 | //===-- HexagonTargetObjectFile.cpp - Hexagon asm properties --------------===// |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 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 | // This file contains the declarations of the HexagonTargetAsmInfo properties. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Craig Topper | b25fda9 | 2012-03-17 18:46:09 +0000 | [diff] [blame] | 14 | #include "HexagonTargetObjectFile.h" |
| 15 | #include "HexagonSubtarget.h" |
| 16 | #include "HexagonTargetMachine.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 17 | #include "llvm/IR/DataLayout.h" |
| 18 | #include "llvm/IR/DerivedTypes.h" |
| 19 | #include "llvm/IR/Function.h" |
| 20 | #include "llvm/IR/GlobalVariable.h" |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 21 | #include "llvm/MC/MCContext.h" |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 22 | #include "llvm/Support/CommandLine.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 23 | #include "llvm/Support/ELF.h" |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 24 | |
| 25 | using namespace llvm; |
| 26 | |
| 27 | static cl::opt<int> SmallDataThreshold("hexagon-small-data-threshold", |
Jyotsna Verma | 5eb5980 | 2013-05-07 19:53:00 +0000 | [diff] [blame] | 28 | cl::init(8), cl::Hidden, |
| 29 | cl::desc("The maximum size of an object in the sdata section")); |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 30 | |
| 31 | void HexagonTargetObjectFile::Initialize(MCContext &Ctx, |
| 32 | const TargetMachine &TM) { |
| 33 | TargetLoweringObjectFileELF::Initialize(Ctx, TM); |
Sid Manning | 326f8af | 2014-11-03 14:56:05 +0000 | [diff] [blame] | 34 | InitializeELF(TM.Options.UseInitArray); |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 35 | |
Rafael Espindola | ba31e27 | 2015-01-29 17:33:21 +0000 | [diff] [blame^] | 36 | SmallDataSection = getContext().getELFSection( |
| 37 | ".sdata", ELF::SHT_PROGBITS, ELF::SHF_WRITE | ELF::SHF_ALLOC); |
| 38 | SmallBSSSection = getContext().getELFSection(".sbss", ELF::SHT_NOBITS, |
| 39 | ELF::SHF_WRITE | ELF::SHF_ALLOC); |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | // sdata/sbss support taken largely from the MIPS Backend. |
| 43 | static bool IsInSmallSection(uint64_t Size) { |
| 44 | return Size > 0 && Size <= (uint64_t)SmallDataThreshold; |
| 45 | } |
Jyotsna Verma | 5eb5980 | 2013-05-07 19:53:00 +0000 | [diff] [blame] | 46 | |
| 47 | bool HexagonTargetObjectFile::IsSmallDataEnabled () const { |
| 48 | return SmallDataThreshold > 0; |
| 49 | } |
| 50 | |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 51 | /// IsGlobalInSmallSection - Return true if this global value should be |
| 52 | /// placed into small data/bss section. |
| 53 | bool HexagonTargetObjectFile::IsGlobalInSmallSection(const GlobalValue *GV, |
| 54 | const TargetMachine &TM) const { |
| 55 | // If the primary definition of this global value is outside the current |
| 56 | // translation unit or the global value is available for inspection but not |
| 57 | // emission, then do nothing. |
| 58 | if (GV->isDeclaration() || GV->hasAvailableExternallyLinkage()) |
| 59 | return false; |
| 60 | |
| 61 | // Otherwise, Check if GV should be in sdata/sbss, when normally it would end |
| 62 | // up in getKindForGlobal(GV, TM). |
| 63 | return IsGlobalInSmallSection(GV, TM, getKindForGlobal(GV, TM)); |
| 64 | } |
| 65 | |
| 66 | /// IsGlobalInSmallSection - Return true if this global value should be |
| 67 | /// placed into small data/bss section. |
| 68 | bool HexagonTargetObjectFile:: |
| 69 | IsGlobalInSmallSection(const GlobalValue *GV, const TargetMachine &TM, |
| 70 | SectionKind Kind) const { |
| 71 | // Only global variables, not functions. |
| 72 | const GlobalVariable *GVA = dyn_cast<GlobalVariable>(GV); |
| 73 | if (!GVA) |
| 74 | return false; |
| 75 | |
| 76 | if (Kind.isBSS() || Kind.isDataNoRel() || Kind.isCommon()) { |
| 77 | Type *Ty = GV->getType()->getElementType(); |
Eric Christopher | 8b77065 | 2015-01-26 19:03:15 +0000 | [diff] [blame] | 78 | return IsInSmallSection(TM.getDataLayout()->getTypeAllocSize(Ty)); |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | return false; |
| 82 | } |
| 83 | |
Rafael Espindola | fa0f728 | 2014-02-08 14:53:28 +0000 | [diff] [blame] | 84 | const MCSection * |
| 85 | HexagonTargetObjectFile::SelectSectionForGlobal(const GlobalValue *GV, |
| 86 | SectionKind Kind, Mangler &Mang, |
| 87 | const TargetMachine &TM) const { |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 88 | |
| 89 | // Handle Small Section classification here. |
| 90 | if (Kind.isBSS() && IsGlobalInSmallSection(GV, TM, Kind)) |
| 91 | return SmallBSSSection; |
| 92 | if (Kind.isDataNoRel() && IsGlobalInSmallSection(GV, TM, Kind)) |
| 93 | return SmallDataSection; |
| 94 | |
| 95 | // Otherwise, we work the same as ELF. |
| 96 | return TargetLoweringObjectFileELF::SelectSectionForGlobal(GV, Kind, Mang,TM); |
| 97 | } |