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