1 //===-- TargetMachine.cpp - General Target Information ---------------------==// 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 describes the general parts of a Target machine. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "llvm/Target/TargetMachine.h" 15 #include "llvm/Analysis/TargetTransformInfo.h" 16 #include "llvm/CodeGen/MachineFunction.h" 17 #include "llvm/IR/Function.h" 18 #include "llvm/IR/GlobalAlias.h" 19 #include "llvm/IR/GlobalValue.h" 20 #include "llvm/IR/GlobalVariable.h" 21 #include "llvm/IR/LegacyPassManager.h" 22 #include "llvm/IR/Mangler.h" 23 #include "llvm/MC/MCAsmInfo.h" 24 #include "llvm/MC/MCContext.h" 25 #include "llvm/MC/MCInstrInfo.h" 26 #include "llvm/MC/MCSectionMachO.h" 27 #include "llvm/MC/MCTargetOptions.h" 28 #include "llvm/MC/SectionKind.h" 29 #include "llvm/Target/TargetLowering.h" 30 #include "llvm/Target/TargetLoweringObjectFile.h" 31 #include "llvm/Target/TargetSubtargetInfo.h" 32 using namespace llvm; 33 34 cl::opt<bool> EnableIPRA("enable-ipra", cl::init(false), cl::Hidden, 35 cl::desc("Enable interprocedural register allocation " 36 "to reduce load/store at procedure calls.")); 37 38 cl::opt<bool> DebugInfoForProfiling( 39 "debug-info-for-profiling", cl::init(false), cl::Hidden, 40 cl::desc("Emit extra debug info to make sample profile more accurate.")); 41 42 //--------------------------------------------------------------------------- 43 // TargetMachine Class 44 // 45 46 TargetMachine::TargetMachine(const Target &T, StringRef DataLayoutString, 47 const Triple &TT, StringRef CPU, StringRef FS, 48 const TargetOptions &Options) 49 : TheTarget(T), DL(DataLayoutString), TargetTriple(TT), TargetCPU(CPU), 50 TargetFS(FS), AsmInfo(nullptr), MRI(nullptr), MII(nullptr), STI(nullptr), 51 RequireStructuredCFG(false), DefaultOptions(Options), Options(Options) { 52 if (EnableIPRA.getNumOccurrences()) 53 this->Options.EnableIPRA = EnableIPRA; 54 if (DebugInfoForProfiling.getNumOccurrences()) 55 this->Options.DebugInfoForProfiling = DebugInfoForProfiling; 56 } 57 58 TargetMachine::~TargetMachine() { 59 delete AsmInfo; 60 delete MRI; 61 delete MII; 62 delete STI; 63 } 64 65 bool TargetMachine::isPositionIndependent() const { 66 return getRelocationModel() == Reloc::PIC_; 67 } 68 69 /// \brief Reset the target options based on the function's attributes. 70 // FIXME: This function needs to go away for a number of reasons: 71 // a) global state on the TargetMachine is terrible in general, 72 // b) these target options should be passed only on the function 73 // and not on the TargetMachine (via TargetOptions) at all. 74 void TargetMachine::resetTargetOptions(const Function &F) const { 75 #define RESET_OPTION(X, Y) \ 76 do { \ 77 if (F.hasFnAttribute(Y)) \ 78 Options.X = (F.getFnAttribute(Y).getValueAsString() == "true"); \ 79 else \ 80 Options.X = DefaultOptions.X; \ 81 } while (0) 82 83 RESET_OPTION(LessPreciseFPMADOption, "less-precise-fpmad"); 84 RESET_OPTION(UnsafeFPMath, "unsafe-fp-math"); 85 RESET_OPTION(NoInfsFPMath, "no-infs-fp-math"); 86 RESET_OPTION(NoNaNsFPMath, "no-nans-fp-math"); 87 RESET_OPTION(NoSignedZerosFPMath, "no-signed-zeros-fp-math"); 88 RESET_OPTION(NoTrappingFPMath, "no-trapping-math"); 89 90 StringRef Denormal = 91 F.getFnAttribute("denormal-fp-math").getValueAsString(); 92 if (Denormal == "ieee") 93 Options.FPDenormalMode = FPDenormal::IEEE; 94 else if (Denormal == "preserve-sign") 95 Options.FPDenormalMode = FPDenormal::PreserveSign; 96 else if (Denormal == "positive-zero") 97 Options.FPDenormalMode = FPDenormal::PositiveZero; 98 else 99 Options.FPDenormalMode = DefaultOptions.FPDenormalMode; 100 } 101 102 /// Returns the code generation relocation model. The choices are static, PIC, 103 /// and dynamic-no-pic. 104 Reloc::Model TargetMachine::getRelocationModel() const { return RM; } 105 106 /// Returns the code model. The choices are small, kernel, medium, large, and 107 /// target default. 108 CodeModel::Model TargetMachine::getCodeModel() const { return CMModel; } 109 110 /// Get the IR-specified TLS model for Var. 111 static TLSModel::Model getSelectedTLSModel(const GlobalValue *GV) { 112 switch (GV->getThreadLocalMode()) { 113 case GlobalVariable::NotThreadLocal: 114 llvm_unreachable("getSelectedTLSModel for non-TLS variable"); 115 break; 116 case GlobalVariable::GeneralDynamicTLSModel: 117 return TLSModel::GeneralDynamic; 118 case GlobalVariable::LocalDynamicTLSModel: 119 return TLSModel::LocalDynamic; 120 case GlobalVariable::InitialExecTLSModel: 121 return TLSModel::InitialExec; 122 case GlobalVariable::LocalExecTLSModel: 123 return TLSModel::LocalExec; 124 } 125 llvm_unreachable("invalid TLS model"); 126 } 127 128 bool TargetMachine::shouldAssumeDSOLocal(const Module &M, 129 const GlobalValue *GV) const { 130 Reloc::Model RM = getRelocationModel(); 131 const Triple &TT = getTargetTriple(); 132 133 // DLLImport explicitly marks the GV as external. 134 if (GV && GV->hasDLLImportStorageClass()) 135 return false; 136 137 // Every other GV is local on COFF. 138 // Make an exception for windows OS in the triple: Some firmwares builds use 139 // *-win32-macho triples. This (accidentally?) produced windows relocations 140 // without GOT tables in older clang versions; Keep this behaviour. 141 if (TT.isOSBinFormatCOFF() || (TT.isOSWindows() && TT.isOSBinFormatMachO())) 142 return true; 143 144 if (GV && (GV->hasLocalLinkage() || !GV->hasDefaultVisibility())) 145 return true; 146 147 if (TT.isOSBinFormatMachO()) { 148 if (RM == Reloc::Static) 149 return true; 150 return GV && GV->isStrongDefinitionForLinker(); 151 } 152 153 assert(TT.isOSBinFormatELF()); 154 assert(RM != Reloc::DynamicNoPIC); 155 156 bool IsExecutable = 157 RM == Reloc::Static || M.getPIELevel() != PIELevel::Default; 158 if (IsExecutable) { 159 // If the symbol is defined, it cannot be preempted. 160 if (GV && !GV->isDeclarationForLinker()) 161 return true; 162 163 bool IsTLS = GV && GV->isThreadLocal(); 164 bool IsAccessViaCopyRelocs = 165 Options.MCOptions.MCPIECopyRelocations && GV && isa<GlobalVariable>(GV); 166 Triple::ArchType Arch = TT.getArch(); 167 bool IsPPC = 168 Arch == Triple::ppc || Arch == Triple::ppc64 || Arch == Triple::ppc64le; 169 // Check if we can use copy relocations. PowerPC has no copy relocations. 170 if (!IsTLS && !IsPPC && (RM == Reloc::Static || IsAccessViaCopyRelocs)) 171 return true; 172 } 173 174 // ELF supports preemption of other symbols. 175 return false; 176 } 177 178 TLSModel::Model TargetMachine::getTLSModel(const GlobalValue *GV) const { 179 bool IsPIE = GV->getParent()->getPIELevel() != PIELevel::Default; 180 Reloc::Model RM = getRelocationModel(); 181 bool IsSharedLibrary = RM == Reloc::PIC_ && !IsPIE; 182 bool IsLocal = shouldAssumeDSOLocal(*GV->getParent(), GV); 183 184 TLSModel::Model Model; 185 if (IsSharedLibrary) { 186 if (IsLocal) 187 Model = TLSModel::LocalDynamic; 188 else 189 Model = TLSModel::GeneralDynamic; 190 } else { 191 if (IsLocal) 192 Model = TLSModel::LocalExec; 193 else 194 Model = TLSModel::InitialExec; 195 } 196 197 // If the user specified a more specific model, use that. 198 TLSModel::Model SelectedModel = getSelectedTLSModel(GV); 199 if (SelectedModel > Model) 200 return SelectedModel; 201 202 return Model; 203 } 204 205 /// Returns the optimization level: None, Less, Default, or Aggressive. 206 CodeGenOpt::Level TargetMachine::getOptLevel() const { return OptLevel; } 207 208 void TargetMachine::setOptLevel(CodeGenOpt::Level Level) { OptLevel = Level; } 209 210 TargetIRAnalysis TargetMachine::getTargetIRAnalysis() { 211 return TargetIRAnalysis([](const Function &F) { 212 return TargetTransformInfo(F.getParent()->getDataLayout()); 213 }); 214 } 215 216 void TargetMachine::getNameWithPrefix(SmallVectorImpl<char> &Name, 217 const GlobalValue *GV, Mangler &Mang, 218 bool MayAlwaysUsePrivate) const { 219 if (MayAlwaysUsePrivate || !GV->hasPrivateLinkage()) { 220 // Simple case: If GV is not private, it is not important to find out if 221 // private labels are legal in this case or not. 222 Mang.getNameWithPrefix(Name, GV, false); 223 return; 224 } 225 const TargetLoweringObjectFile *TLOF = getObjFileLowering(); 226 TLOF->getNameWithPrefix(Name, GV, *this); 227 } 228 229 MCSymbol *TargetMachine::getSymbol(const GlobalValue *GV) const { 230 const TargetLoweringObjectFile *TLOF = getObjFileLowering(); 231 SmallString<128> NameStr; 232 getNameWithPrefix(NameStr, GV, TLOF->getMangler()); 233 return TLOF->getContext().getOrCreateSymbol(NameStr); 234 } 235