1bb8507e6SMatthias Braun //===-- TargetMachine.cpp - General Target Information ---------------------==// 2bb8507e6SMatthias Braun // 3bb8507e6SMatthias Braun // The LLVM Compiler Infrastructure 4bb8507e6SMatthias Braun // 5bb8507e6SMatthias Braun // This file is distributed under the University of Illinois Open Source 6bb8507e6SMatthias Braun // License. See LICENSE.TXT for details. 7bb8507e6SMatthias Braun // 8bb8507e6SMatthias Braun //===----------------------------------------------------------------------===// 9bb8507e6SMatthias Braun // 10bb8507e6SMatthias Braun // This file describes the general parts of a Target machine. 11bb8507e6SMatthias Braun // 12bb8507e6SMatthias Braun //===----------------------------------------------------------------------===// 13bb8507e6SMatthias Braun 14bb8507e6SMatthias Braun #include "llvm/Target/TargetMachine.h" 15bb8507e6SMatthias Braun #include "llvm/Analysis/TargetTransformInfo.h" 16bb8507e6SMatthias Braun #include "llvm/IR/Function.h" 17bb8507e6SMatthias Braun #include "llvm/IR/GlobalAlias.h" 18bb8507e6SMatthias Braun #include "llvm/IR/GlobalValue.h" 19bb8507e6SMatthias Braun #include "llvm/IR/GlobalVariable.h" 20bb8507e6SMatthias Braun #include "llvm/IR/LegacyPassManager.h" 21bb8507e6SMatthias Braun #include "llvm/IR/Mangler.h" 22bb8507e6SMatthias Braun #include "llvm/MC/MCAsmInfo.h" 23bb8507e6SMatthias Braun #include "llvm/MC/MCContext.h" 24bb8507e6SMatthias Braun #include "llvm/MC/MCInstrInfo.h" 25bb8507e6SMatthias Braun #include "llvm/MC/MCSectionMachO.h" 26bb8507e6SMatthias Braun #include "llvm/MC/MCTargetOptions.h" 27bb8507e6SMatthias Braun #include "llvm/MC/SectionKind.h" 286054e650SDavid Blaikie #include "llvm/Target/TargetLoweringObjectFile.h" 29bb8507e6SMatthias Braun using namespace llvm; 30bb8507e6SMatthias Braun 31bb8507e6SMatthias Braun //--------------------------------------------------------------------------- 32bb8507e6SMatthias Braun // TargetMachine Class 33bb8507e6SMatthias Braun // 34bb8507e6SMatthias Braun 35bb8507e6SMatthias Braun TargetMachine::TargetMachine(const Target &T, StringRef DataLayoutString, 36bb8507e6SMatthias Braun const Triple &TT, StringRef CPU, StringRef FS, 37bb8507e6SMatthias Braun const TargetOptions &Options) 38bb8507e6SMatthias Braun : TheTarget(T), DL(DataLayoutString), TargetTriple(TT), TargetCPU(CPU), 39bb8507e6SMatthias Braun TargetFS(FS), AsmInfo(nullptr), MRI(nullptr), MII(nullptr), STI(nullptr), 40bb8507e6SMatthias Braun RequireStructuredCFG(false), DefaultOptions(Options), Options(Options) { 41bb8507e6SMatthias Braun } 42bb8507e6SMatthias Braun 43bb8507e6SMatthias Braun TargetMachine::~TargetMachine() { 44bb8507e6SMatthias Braun delete AsmInfo; 45bb8507e6SMatthias Braun delete MRI; 46bb8507e6SMatthias Braun delete MII; 47bb8507e6SMatthias Braun delete STI; 48bb8507e6SMatthias Braun } 49bb8507e6SMatthias Braun 50bb8507e6SMatthias Braun bool TargetMachine::isPositionIndependent() const { 51bb8507e6SMatthias Braun return getRelocationModel() == Reloc::PIC_; 52bb8507e6SMatthias Braun } 53bb8507e6SMatthias Braun 54*5f8f34e4SAdrian Prantl /// Reset the target options based on the function's attributes. 55bb8507e6SMatthias Braun // FIXME: This function needs to go away for a number of reasons: 56bb8507e6SMatthias Braun // a) global state on the TargetMachine is terrible in general, 57bb8507e6SMatthias Braun // b) these target options should be passed only on the function 58bb8507e6SMatthias Braun // and not on the TargetMachine (via TargetOptions) at all. 59bb8507e6SMatthias Braun void TargetMachine::resetTargetOptions(const Function &F) const { 60bb8507e6SMatthias Braun #define RESET_OPTION(X, Y) \ 61bb8507e6SMatthias Braun do { \ 62bb8507e6SMatthias Braun if (F.hasFnAttribute(Y)) \ 63bb8507e6SMatthias Braun Options.X = (F.getFnAttribute(Y).getValueAsString() == "true"); \ 64bb8507e6SMatthias Braun else \ 65bb8507e6SMatthias Braun Options.X = DefaultOptions.X; \ 66bb8507e6SMatthias Braun } while (0) 67bb8507e6SMatthias Braun 68bb8507e6SMatthias Braun RESET_OPTION(UnsafeFPMath, "unsafe-fp-math"); 69bb8507e6SMatthias Braun RESET_OPTION(NoInfsFPMath, "no-infs-fp-math"); 70bb8507e6SMatthias Braun RESET_OPTION(NoNaNsFPMath, "no-nans-fp-math"); 71bb8507e6SMatthias Braun RESET_OPTION(NoSignedZerosFPMath, "no-signed-zeros-fp-math"); 72bb8507e6SMatthias Braun RESET_OPTION(NoTrappingFPMath, "no-trapping-math"); 73bb8507e6SMatthias Braun 74bb8507e6SMatthias Braun StringRef Denormal = 75bb8507e6SMatthias Braun F.getFnAttribute("denormal-fp-math").getValueAsString(); 76bb8507e6SMatthias Braun if (Denormal == "ieee") 77bb8507e6SMatthias Braun Options.FPDenormalMode = FPDenormal::IEEE; 78bb8507e6SMatthias Braun else if (Denormal == "preserve-sign") 79bb8507e6SMatthias Braun Options.FPDenormalMode = FPDenormal::PreserveSign; 80bb8507e6SMatthias Braun else if (Denormal == "positive-zero") 81bb8507e6SMatthias Braun Options.FPDenormalMode = FPDenormal::PositiveZero; 82bb8507e6SMatthias Braun else 83bb8507e6SMatthias Braun Options.FPDenormalMode = DefaultOptions.FPDenormalMode; 84bb8507e6SMatthias Braun } 85bb8507e6SMatthias Braun 86bb8507e6SMatthias Braun /// Returns the code generation relocation model. The choices are static, PIC, 87bb8507e6SMatthias Braun /// and dynamic-no-pic. 88bb8507e6SMatthias Braun Reloc::Model TargetMachine::getRelocationModel() const { return RM; } 89bb8507e6SMatthias Braun 90bb8507e6SMatthias Braun /// Returns the code model. The choices are small, kernel, medium, large, and 91bb8507e6SMatthias Braun /// target default. 92bb8507e6SMatthias Braun CodeModel::Model TargetMachine::getCodeModel() const { return CMModel; } 93bb8507e6SMatthias Braun 94bb8507e6SMatthias Braun /// Get the IR-specified TLS model for Var. 95bb8507e6SMatthias Braun static TLSModel::Model getSelectedTLSModel(const GlobalValue *GV) { 96bb8507e6SMatthias Braun switch (GV->getThreadLocalMode()) { 97bb8507e6SMatthias Braun case GlobalVariable::NotThreadLocal: 98bb8507e6SMatthias Braun llvm_unreachable("getSelectedTLSModel for non-TLS variable"); 99bb8507e6SMatthias Braun break; 100bb8507e6SMatthias Braun case GlobalVariable::GeneralDynamicTLSModel: 101bb8507e6SMatthias Braun return TLSModel::GeneralDynamic; 102bb8507e6SMatthias Braun case GlobalVariable::LocalDynamicTLSModel: 103bb8507e6SMatthias Braun return TLSModel::LocalDynamic; 104bb8507e6SMatthias Braun case GlobalVariable::InitialExecTLSModel: 105bb8507e6SMatthias Braun return TLSModel::InitialExec; 106bb8507e6SMatthias Braun case GlobalVariable::LocalExecTLSModel: 107bb8507e6SMatthias Braun return TLSModel::LocalExec; 108bb8507e6SMatthias Braun } 109bb8507e6SMatthias Braun llvm_unreachable("invalid TLS model"); 110bb8507e6SMatthias Braun } 111bb8507e6SMatthias Braun 112bb8507e6SMatthias Braun bool TargetMachine::shouldAssumeDSOLocal(const Module &M, 113bb8507e6SMatthias Braun const GlobalValue *GV) const { 1146f36637bSRafael Espindola // If the IR producer requested that this GV be treated as dso local, obey. 1156f36637bSRafael Espindola if (GV && GV->isDSOLocal()) 1166f36637bSRafael Espindola return true; 1176f36637bSRafael Espindola 11863c378d3SRafael Espindola // If we are not supossed to use a PLT, we cannot assume that intrinsics are 11963c378d3SRafael Espindola // local since the linker can convert some direct access to access via plt. 12063c378d3SRafael Espindola if (M.getRtLibUseGOT() && !GV) 12163c378d3SRafael Espindola return false; 12263c378d3SRafael Espindola 12363c378d3SRafael Espindola // According to the llvm language reference, we should be able to 12463c378d3SRafael Espindola // just return false in here if we have a GV, as we know it is 12563c378d3SRafael Espindola // dso_preemptable. At this point in time, the various IR producers 12663c378d3SRafael Espindola // have not been transitioned to always produce a dso_local when it 12763c378d3SRafael Espindola // is possible to do so. 12863c378d3SRafael Espindola // In the case of intrinsics, GV is null and there is nowhere to put 12963c378d3SRafael Espindola // dso_local. Returning false for those will produce worse code in some 13063c378d3SRafael Espindola // architectures. For example, on x86 the caller has to set ebx before calling 13163c378d3SRafael Espindola // a plt. 13263c378d3SRafael Espindola // As a result we still have some logic in here to improve the quality of the 13363c378d3SRafael Espindola // generated code. 13463c378d3SRafael Espindola // FIXME: Add a module level metadata for whether intrinsics should be assumed 13563c378d3SRafael Espindola // local. 1366f36637bSRafael Espindola 137bb8507e6SMatthias Braun Reloc::Model RM = getRelocationModel(); 138bb8507e6SMatthias Braun const Triple &TT = getTargetTriple(); 139bb8507e6SMatthias Braun 140bb8507e6SMatthias Braun // DLLImport explicitly marks the GV as external. 141bb8507e6SMatthias Braun if (GV && GV->hasDLLImportStorageClass()) 142bb8507e6SMatthias Braun return false; 143bb8507e6SMatthias Braun 144bb8507e6SMatthias Braun // Every other GV is local on COFF. 145ba02f3f2SRafael Espindola // Make an exception for windows OS in the triple: Some firmware builds use 146bb8507e6SMatthias Braun // *-win32-macho triples. This (accidentally?) produced windows relocations 147bb8507e6SMatthias Braun // without GOT tables in older clang versions; Keep this behaviour. 148bb8507e6SMatthias Braun if (TT.isOSBinFormatCOFF() || (TT.isOSWindows() && TT.isOSBinFormatMachO())) 149bb8507e6SMatthias Braun return true; 150bb8507e6SMatthias Braun 1512393c3b4SRafael Espindola // Most PIC code sequences that assume that a symbol is local cannot 1522393c3b4SRafael Espindola // produce a 0 if it turns out the symbol is undefined. While this 1532393c3b4SRafael Espindola // is ABI and relocation depended, it seems worth it to handle it 1542393c3b4SRafael Espindola // here. 15563c378d3SRafael Espindola if (GV && isPositionIndependent() && GV->hasExternalWeakLinkage()) 1562393c3b4SRafael Espindola return false; 1572393c3b4SRafael Espindola 15863c378d3SRafael Espindola if (GV && !GV->hasDefaultVisibility()) 159bb8507e6SMatthias Braun return true; 160bb8507e6SMatthias Braun 161bb8507e6SMatthias Braun if (TT.isOSBinFormatMachO()) { 162bb8507e6SMatthias Braun if (RM == Reloc::Static) 163bb8507e6SMatthias Braun return true; 16463c378d3SRafael Espindola return GV && GV->isStrongDefinitionForLinker(); 165bb8507e6SMatthias Braun } 166bb8507e6SMatthias Braun 167bb8507e6SMatthias Braun assert(TT.isOSBinFormatELF()); 168bb8507e6SMatthias Braun assert(RM != Reloc::DynamicNoPIC); 169bb8507e6SMatthias Braun 170bb8507e6SMatthias Braun bool IsExecutable = 171bb8507e6SMatthias Braun RM == Reloc::Static || M.getPIELevel() != PIELevel::Default; 172bb8507e6SMatthias Braun if (IsExecutable) { 173bb8507e6SMatthias Braun // If the symbol is defined, it cannot be preempted. 17463c378d3SRafael Espindola if (GV && !GV->isDeclarationForLinker()) 175bb8507e6SMatthias Braun return true; 176bb8507e6SMatthias Braun 177056b3fd6SSriraman Tallam // A symbol marked nonlazybind should not be accessed with a plt. If the 178056b3fd6SSriraman Tallam // symbol turns out to be external, the linker will convert a direct 179056b3fd6SSriraman Tallam // access to an access via the plt, so don't assume it is local. 18063c378d3SRafael Espindola const Function *F = dyn_cast_or_null<Function>(GV); 181056b3fd6SSriraman Tallam if (F && F->hasFnAttribute(Attribute::NonLazyBind)) 182056b3fd6SSriraman Tallam return false; 183056b3fd6SSriraman Tallam 18463c378d3SRafael Espindola bool IsTLS = GV && GV->isThreadLocal(); 1852393c3b4SRafael Espindola bool IsAccessViaCopyRelocs = 18663c378d3SRafael Espindola GV && Options.MCOptions.MCPIECopyRelocations && isa<GlobalVariable>(GV); 187bb8507e6SMatthias Braun Triple::ArchType Arch = TT.getArch(); 188bb8507e6SMatthias Braun bool IsPPC = 189bb8507e6SMatthias Braun Arch == Triple::ppc || Arch == Triple::ppc64 || Arch == Triple::ppc64le; 190bb8507e6SMatthias Braun // Check if we can use copy relocations. PowerPC has no copy relocations. 191bb8507e6SMatthias Braun if (!IsTLS && !IsPPC && (RM == Reloc::Static || IsAccessViaCopyRelocs)) 192bb8507e6SMatthias Braun return true; 193bb8507e6SMatthias Braun } 194bb8507e6SMatthias Braun 195bb8507e6SMatthias Braun // ELF supports preemption of other symbols. 196bb8507e6SMatthias Braun return false; 197bb8507e6SMatthias Braun } 198bb8507e6SMatthias Braun 1999f9e4681SChih-Hung Hsieh bool TargetMachine::useEmulatedTLS() const { 2009f9e4681SChih-Hung Hsieh // Returns Options.EmulatedTLS if the -emulated-tls or -no-emulated-tls 2019f9e4681SChih-Hung Hsieh // was specified explicitly; otherwise uses target triple to decide default. 2029f9e4681SChih-Hung Hsieh if (Options.ExplicitEmulatedTLS) 2039f9e4681SChih-Hung Hsieh return Options.EmulatedTLS; 2049f9e4681SChih-Hung Hsieh return getTargetTriple().hasDefaultEmulatedTLS(); 2059f9e4681SChih-Hung Hsieh } 2069f9e4681SChih-Hung Hsieh 207bb8507e6SMatthias Braun TLSModel::Model TargetMachine::getTLSModel(const GlobalValue *GV) const { 208bb8507e6SMatthias Braun bool IsPIE = GV->getParent()->getPIELevel() != PIELevel::Default; 209bb8507e6SMatthias Braun Reloc::Model RM = getRelocationModel(); 210bb8507e6SMatthias Braun bool IsSharedLibrary = RM == Reloc::PIC_ && !IsPIE; 211bb8507e6SMatthias Braun bool IsLocal = shouldAssumeDSOLocal(*GV->getParent(), GV); 212bb8507e6SMatthias Braun 213bb8507e6SMatthias Braun TLSModel::Model Model; 214bb8507e6SMatthias Braun if (IsSharedLibrary) { 215bb8507e6SMatthias Braun if (IsLocal) 216bb8507e6SMatthias Braun Model = TLSModel::LocalDynamic; 217bb8507e6SMatthias Braun else 218bb8507e6SMatthias Braun Model = TLSModel::GeneralDynamic; 219bb8507e6SMatthias Braun } else { 220bb8507e6SMatthias Braun if (IsLocal) 221bb8507e6SMatthias Braun Model = TLSModel::LocalExec; 222bb8507e6SMatthias Braun else 223bb8507e6SMatthias Braun Model = TLSModel::InitialExec; 224bb8507e6SMatthias Braun } 225bb8507e6SMatthias Braun 226bb8507e6SMatthias Braun // If the user specified a more specific model, use that. 227bb8507e6SMatthias Braun TLSModel::Model SelectedModel = getSelectedTLSModel(GV); 228bb8507e6SMatthias Braun if (SelectedModel > Model) 229bb8507e6SMatthias Braun return SelectedModel; 230bb8507e6SMatthias Braun 231bb8507e6SMatthias Braun return Model; 232bb8507e6SMatthias Braun } 233bb8507e6SMatthias Braun 234bb8507e6SMatthias Braun /// Returns the optimization level: None, Less, Default, or Aggressive. 235bb8507e6SMatthias Braun CodeGenOpt::Level TargetMachine::getOptLevel() const { return OptLevel; } 236bb8507e6SMatthias Braun 237bb8507e6SMatthias Braun void TargetMachine::setOptLevel(CodeGenOpt::Level Level) { OptLevel = Level; } 238bb8507e6SMatthias Braun 23926d11ca4SSanjoy Das TargetTransformInfo TargetMachine::getTargetTransformInfo(const Function &F) { 240bb8507e6SMatthias Braun return TargetTransformInfo(F.getParent()->getDataLayout()); 241bb8507e6SMatthias Braun } 242bb8507e6SMatthias Braun 243bb8507e6SMatthias Braun void TargetMachine::getNameWithPrefix(SmallVectorImpl<char> &Name, 244bb8507e6SMatthias Braun const GlobalValue *GV, Mangler &Mang, 245bb8507e6SMatthias Braun bool MayAlwaysUsePrivate) const { 246bb8507e6SMatthias Braun if (MayAlwaysUsePrivate || !GV->hasPrivateLinkage()) { 247bb8507e6SMatthias Braun // Simple case: If GV is not private, it is not important to find out if 248bb8507e6SMatthias Braun // private labels are legal in this case or not. 249bb8507e6SMatthias Braun Mang.getNameWithPrefix(Name, GV, false); 250bb8507e6SMatthias Braun return; 251bb8507e6SMatthias Braun } 252bb8507e6SMatthias Braun const TargetLoweringObjectFile *TLOF = getObjFileLowering(); 253bb8507e6SMatthias Braun TLOF->getNameWithPrefix(Name, GV, *this); 254bb8507e6SMatthias Braun } 255bb8507e6SMatthias Braun 256bb8507e6SMatthias Braun MCSymbol *TargetMachine::getSymbol(const GlobalValue *GV) const { 257bb8507e6SMatthias Braun const TargetLoweringObjectFile *TLOF = getObjFileLowering(); 258bb8507e6SMatthias Braun SmallString<128> NameStr; 259bb8507e6SMatthias Braun getNameWithPrefix(NameStr, GV, TLOF->getMangler()); 260bb8507e6SMatthias Braun return TLOF->getContext().getOrCreateSymbol(NameStr); 261bb8507e6SMatthias Braun } 26226d11ca4SSanjoy Das 26326d11ca4SSanjoy Das TargetIRAnalysis TargetMachine::getTargetIRAnalysis() { 26426d11ca4SSanjoy Das // Since Analysis can't depend on Target, use a std::function to invert the 26526d11ca4SSanjoy Das // dependency. 26626d11ca4SSanjoy Das return TargetIRAnalysis( 26726d11ca4SSanjoy Das [this](const Function &F) { return this->getTargetTransformInfo(F); }); 26826d11ca4SSanjoy Das } 269