10b57cec5SDimitry Andric //===-- TargetMachine.cpp - General Target Information ---------------------==// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric // 90b57cec5SDimitry Andric // This file describes the general parts of a Target machine. 100b57cec5SDimitry Andric // 110b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 120b57cec5SDimitry Andric 130b57cec5SDimitry Andric #include "llvm/Target/TargetMachine.h" 140b57cec5SDimitry Andric #include "llvm/Analysis/TargetTransformInfo.h" 150b57cec5SDimitry Andric #include "llvm/IR/Function.h" 160b57cec5SDimitry Andric #include "llvm/IR/GlobalValue.h" 170b57cec5SDimitry Andric #include "llvm/IR/GlobalVariable.h" 180b57cec5SDimitry Andric #include "llvm/IR/Mangler.h" 19*0fca6ea1SDimitry Andric #include "llvm/IR/Module.h" 200b57cec5SDimitry Andric #include "llvm/MC/MCAsmInfo.h" 210b57cec5SDimitry Andric #include "llvm/MC/MCContext.h" 220b57cec5SDimitry Andric #include "llvm/MC/MCInstrInfo.h" 2381ad6265SDimitry Andric #include "llvm/MC/MCRegisterInfo.h" 2481ad6265SDimitry Andric #include "llvm/MC/MCSubtargetInfo.h" 2506c3fb27SDimitry Andric #include "llvm/Support/CodeGen.h" 260b57cec5SDimitry Andric #include "llvm/Target/TargetLoweringObjectFile.h" 270b57cec5SDimitry Andric using namespace llvm; 280b57cec5SDimitry Andric 290b57cec5SDimitry Andric //--------------------------------------------------------------------------- 300b57cec5SDimitry Andric // TargetMachine Class 310b57cec5SDimitry Andric // 320b57cec5SDimitry Andric 330b57cec5SDimitry Andric TargetMachine::TargetMachine(const Target &T, StringRef DataLayoutString, 340b57cec5SDimitry Andric const Triple &TT, StringRef CPU, StringRef FS, 350b57cec5SDimitry Andric const TargetOptions &Options) 365ffd83dbSDimitry Andric : TheTarget(T), DL(DataLayoutString), TargetTriple(TT), 375ffd83dbSDimitry Andric TargetCPU(std::string(CPU)), TargetFS(std::string(FS)), AsmInfo(nullptr), 385ffd83dbSDimitry Andric MRI(nullptr), MII(nullptr), STI(nullptr), RequireStructuredCFG(false), 395f757f3fSDimitry Andric O0WantsFastISel(false), Options(Options) {} 400b57cec5SDimitry Andric 410b57cec5SDimitry Andric TargetMachine::~TargetMachine() = default; 420b57cec5SDimitry Andric 435f757f3fSDimitry Andric bool TargetMachine::isLargeGlobalValue(const GlobalValue *GVal) const { 4406c3fb27SDimitry Andric if (getTargetTriple().getArch() != Triple::x86_64) 4506c3fb27SDimitry Andric return false; 465f757f3fSDimitry Andric 47*0fca6ea1SDimitry Andric // Remaining logic below is ELF-specific. For other object file formats where 48*0fca6ea1SDimitry Andric // the large code model is mostly used for JIT compilation, just look at the 49*0fca6ea1SDimitry Andric // code model. 50*0fca6ea1SDimitry Andric if (!getTargetTriple().isOSBinFormatELF()) 51*0fca6ea1SDimitry Andric return getCodeModel() == CodeModel::Large; 52*0fca6ea1SDimitry Andric 535f757f3fSDimitry Andric auto *GO = GVal->getAliaseeObject(); 545f757f3fSDimitry Andric 555f757f3fSDimitry Andric // Be conservative if we can't find an underlying GlobalObject. 565f757f3fSDimitry Andric if (!GO) 5706c3fb27SDimitry Andric return true; 585f757f3fSDimitry Andric 595f757f3fSDimitry Andric auto *GV = dyn_cast<GlobalVariable>(GO); 605f757f3fSDimitry Andric 61*0fca6ea1SDimitry Andric auto IsPrefix = [](StringRef Name, StringRef Prefix) { 62*0fca6ea1SDimitry Andric return Name.consume_front(Prefix) && (Name.empty() || Name[0] == '.'); 63*0fca6ea1SDimitry Andric }; 64*0fca6ea1SDimitry Andric 655f757f3fSDimitry Andric // Functions/GlobalIFuncs are only large under the large code model. 66*0fca6ea1SDimitry Andric if (!GV) { 67*0fca6ea1SDimitry Andric // Handle explicit sections as we do for GlobalVariables with an explicit 68*0fca6ea1SDimitry Andric // section, see comments below. 69*0fca6ea1SDimitry Andric if (GO->hasSection()) { 70*0fca6ea1SDimitry Andric StringRef Name = GO->getSection(); 71*0fca6ea1SDimitry Andric return IsPrefix(Name, ".ltext"); 72*0fca6ea1SDimitry Andric } 735f757f3fSDimitry Andric return getCodeModel() == CodeModel::Large; 74*0fca6ea1SDimitry Andric } 755f757f3fSDimitry Andric 765f757f3fSDimitry Andric if (GV->isThreadLocal()) 775f757f3fSDimitry Andric return false; 785f757f3fSDimitry Andric 795f757f3fSDimitry Andric // For x86-64, we treat an explicit GlobalVariable small code model to mean 805f757f3fSDimitry Andric // that the global should be placed in a small section, and ditto for large. 815f757f3fSDimitry Andric if (auto CM = GV->getCodeModel()) { 825f757f3fSDimitry Andric if (*CM == CodeModel::Small) 835f757f3fSDimitry Andric return false; 845f757f3fSDimitry Andric if (*CM == CodeModel::Large) 855f757f3fSDimitry Andric return true; 865f757f3fSDimitry Andric } 875f757f3fSDimitry Andric 887a6dacacSDimitry Andric // Treat all globals in explicit sections as small, except for the standard 897a6dacacSDimitry Andric // large sections of .lbss, .ldata, .lrodata. This reduces the risk of linking 907a6dacacSDimitry Andric // together small and large sections, resulting in small references to large 917a6dacacSDimitry Andric // data sections. The code model attribute overrides this above. 927a6dacacSDimitry Andric if (GV->hasSection()) { 937a6dacacSDimitry Andric StringRef Name = GV->getSection(); 94*0fca6ea1SDimitry Andric return IsPrefix(Name, ".lbss") || IsPrefix(Name, ".ldata") || 95*0fca6ea1SDimitry Andric IsPrefix(Name, ".lrodata"); 967a6dacacSDimitry Andric } 977a6dacacSDimitry Andric 987a6dacacSDimitry Andric // Respect large data threshold for medium and large code models. 995f757f3fSDimitry Andric if (getCodeModel() == CodeModel::Medium || 1005f757f3fSDimitry Andric getCodeModel() == CodeModel::Large) { 1015f757f3fSDimitry Andric if (!GV->getValueType()->isSized()) 1025f757f3fSDimitry Andric return true; 103*0fca6ea1SDimitry Andric // Linker defined start/stop symbols can point to arbitrary points in the 104*0fca6ea1SDimitry Andric // binary, so treat them as large. 105*0fca6ea1SDimitry Andric if (GV->isDeclaration() && (GV->getName() == "__ehdr_start" || 106*0fca6ea1SDimitry Andric GV->getName().starts_with("__start_") || 107*0fca6ea1SDimitry Andric GV->getName().starts_with("__stop_"))) 108*0fca6ea1SDimitry Andric return true; 109*0fca6ea1SDimitry Andric const DataLayout &DL = GV->getDataLayout(); 110*0fca6ea1SDimitry Andric uint64_t Size = DL.getTypeAllocSize(GV->getValueType()); 1115f757f3fSDimitry Andric return Size == 0 || Size > LargeDataThreshold; 1125f757f3fSDimitry Andric } 1135f757f3fSDimitry Andric 1145f757f3fSDimitry Andric return false; 11506c3fb27SDimitry Andric } 11606c3fb27SDimitry Andric 1170b57cec5SDimitry Andric bool TargetMachine::isPositionIndependent() const { 1180b57cec5SDimitry Andric return getRelocationModel() == Reloc::PIC_; 1190b57cec5SDimitry Andric } 1200b57cec5SDimitry Andric 1210b57cec5SDimitry Andric /// Reset the target options based on the function's attributes. 1225ffd83dbSDimitry Andric /// setFunctionAttributes should have made the raw attribute value consistent 1235ffd83dbSDimitry Andric /// with the command line flag if used. 1245ffd83dbSDimitry Andric // 1250b57cec5SDimitry Andric // FIXME: This function needs to go away for a number of reasons: 1260b57cec5SDimitry Andric // a) global state on the TargetMachine is terrible in general, 1270b57cec5SDimitry Andric // b) these target options should be passed only on the function 1280b57cec5SDimitry Andric // and not on the TargetMachine (via TargetOptions) at all. 1290b57cec5SDimitry Andric void TargetMachine::resetTargetOptions(const Function &F) const { 1300b57cec5SDimitry Andric #define RESET_OPTION(X, Y) \ 1310b57cec5SDimitry Andric do { \ 132fe6060f1SDimitry Andric Options.X = F.getFnAttribute(Y).getValueAsBool(); \ 1330b57cec5SDimitry Andric } while (0) 1340b57cec5SDimitry Andric 1350b57cec5SDimitry Andric RESET_OPTION(UnsafeFPMath, "unsafe-fp-math"); 1360b57cec5SDimitry Andric RESET_OPTION(NoInfsFPMath, "no-infs-fp-math"); 1370b57cec5SDimitry Andric RESET_OPTION(NoNaNsFPMath, "no-nans-fp-math"); 1380b57cec5SDimitry Andric RESET_OPTION(NoSignedZerosFPMath, "no-signed-zeros-fp-math"); 13981ad6265SDimitry Andric RESET_OPTION(ApproxFuncFPMath, "approx-func-fp-math"); 1400b57cec5SDimitry Andric } 1410b57cec5SDimitry Andric 1420b57cec5SDimitry Andric /// Returns the code generation relocation model. The choices are static, PIC, 1430b57cec5SDimitry Andric /// and dynamic-no-pic. 1440b57cec5SDimitry Andric Reloc::Model TargetMachine::getRelocationModel() const { return RM; } 1450b57cec5SDimitry Andric 1465f757f3fSDimitry Andric uint64_t TargetMachine::getMaxCodeSize() const { 1475f757f3fSDimitry Andric switch (getCodeModel()) { 1485f757f3fSDimitry Andric case CodeModel::Tiny: 1495f757f3fSDimitry Andric return llvm::maxUIntN(10); 1505f757f3fSDimitry Andric case CodeModel::Small: 1515f757f3fSDimitry Andric case CodeModel::Kernel: 1525f757f3fSDimitry Andric case CodeModel::Medium: 1535f757f3fSDimitry Andric return llvm::maxUIntN(31); 1545f757f3fSDimitry Andric case CodeModel::Large: 1555f757f3fSDimitry Andric return llvm::maxUIntN(64); 1565f757f3fSDimitry Andric } 1575f757f3fSDimitry Andric llvm_unreachable("Unhandled CodeModel enum"); 1585f757f3fSDimitry Andric } 1595f757f3fSDimitry Andric 1600b57cec5SDimitry Andric /// Get the IR-specified TLS model for Var. 1610b57cec5SDimitry Andric static TLSModel::Model getSelectedTLSModel(const GlobalValue *GV) { 1620b57cec5SDimitry Andric switch (GV->getThreadLocalMode()) { 1630b57cec5SDimitry Andric case GlobalVariable::NotThreadLocal: 1640b57cec5SDimitry Andric llvm_unreachable("getSelectedTLSModel for non-TLS variable"); 1650b57cec5SDimitry Andric break; 1660b57cec5SDimitry Andric case GlobalVariable::GeneralDynamicTLSModel: 1670b57cec5SDimitry Andric return TLSModel::GeneralDynamic; 1680b57cec5SDimitry Andric case GlobalVariable::LocalDynamicTLSModel: 1690b57cec5SDimitry Andric return TLSModel::LocalDynamic; 1700b57cec5SDimitry Andric case GlobalVariable::InitialExecTLSModel: 1710b57cec5SDimitry Andric return TLSModel::InitialExec; 1720b57cec5SDimitry Andric case GlobalVariable::LocalExecTLSModel: 1730b57cec5SDimitry Andric return TLSModel::LocalExec; 1740b57cec5SDimitry Andric } 1750b57cec5SDimitry Andric llvm_unreachable("invalid TLS model"); 1760b57cec5SDimitry Andric } 1770b57cec5SDimitry Andric 178*0fca6ea1SDimitry Andric bool TargetMachine::shouldAssumeDSOLocal(const GlobalValue *GV) const { 179e8d8bef9SDimitry Andric const Triple &TT = getTargetTriple(); 180e8d8bef9SDimitry Andric Reloc::Model RM = getRelocationModel(); 1810b57cec5SDimitry Andric 1820b57cec5SDimitry Andric // According to the llvm language reference, we should be able to 1830b57cec5SDimitry Andric // just return false in here if we have a GV, as we know it is 1840b57cec5SDimitry Andric // dso_preemptable. At this point in time, the various IR producers 1850b57cec5SDimitry Andric // have not been transitioned to always produce a dso_local when it 1860b57cec5SDimitry Andric // is possible to do so. 187e8d8bef9SDimitry Andric // 1880b57cec5SDimitry Andric // As a result we still have some logic in here to improve the quality of the 1890b57cec5SDimitry Andric // generated code. 190e8d8bef9SDimitry Andric if (!GV) 191349cc55cSDimitry Andric return false; 1920b57cec5SDimitry Andric 193e8d8bef9SDimitry Andric // If the IR producer requested that this GV be treated as dso local, obey. 194e8d8bef9SDimitry Andric if (GV->isDSOLocal()) 195e8d8bef9SDimitry Andric return true; 1960b57cec5SDimitry Andric 197349cc55cSDimitry Andric if (TT.isOSBinFormatCOFF()) { 1980b57cec5SDimitry Andric // DLLImport explicitly marks the GV as external. 199e8d8bef9SDimitry Andric if (GV->hasDLLImportStorageClass()) 2000b57cec5SDimitry Andric return false; 2010b57cec5SDimitry Andric 2020b57cec5SDimitry Andric // On MinGW, variables that haven't been declared with DLLImport may still 2030b57cec5SDimitry Andric // end up automatically imported by the linker. To make this feasible, 2040b57cec5SDimitry Andric // don't assume the variables to be DSO local unless we actually know 2050b57cec5SDimitry Andric // that for sure. This only has to be done for variables; for functions 2060b57cec5SDimitry Andric // the linker can insert thunks for calling functions from another DLL. 207349cc55cSDimitry Andric if (TT.isWindowsGNUEnvironment() && GV->isDeclarationForLinker() && 208349cc55cSDimitry Andric isa<GlobalVariable>(GV)) 2090b57cec5SDimitry Andric return false; 2100b57cec5SDimitry Andric 211349cc55cSDimitry Andric // Don't mark 'extern_weak' symbols as DSO local. If these symbols remain 212349cc55cSDimitry Andric // unresolved in the link, they can be resolved to zero, which is outside 213349cc55cSDimitry Andric // the current DSO. 214349cc55cSDimitry Andric if (GV->hasExternalWeakLinkage()) 2150b57cec5SDimitry Andric return false; 2160b57cec5SDimitry Andric 2170b57cec5SDimitry Andric // Every other GV is local on COFF. 218349cc55cSDimitry Andric return true; 219349cc55cSDimitry Andric } 220349cc55cSDimitry Andric 221349cc55cSDimitry Andric if (TT.isOSBinFormatGOFF()) 2220b57cec5SDimitry Andric return true; 2230b57cec5SDimitry Andric 2240b57cec5SDimitry Andric if (TT.isOSBinFormatMachO()) { 2250b57cec5SDimitry Andric if (RM == Reloc::Static) 2260b57cec5SDimitry Andric return true; 227e8d8bef9SDimitry Andric return GV->isStrongDefinitionForLinker(); 2280b57cec5SDimitry Andric } 2290b57cec5SDimitry Andric 230349cc55cSDimitry Andric assert(TT.isOSBinFormatELF() || TT.isOSBinFormatWasm() || 231349cc55cSDimitry Andric TT.isOSBinFormatXCOFF()); 2320b57cec5SDimitry Andric return false; 2330b57cec5SDimitry Andric } 2340b57cec5SDimitry Andric 23506c3fb27SDimitry Andric bool TargetMachine::useEmulatedTLS() const { return Options.EmulatedTLS; } 2367a6dacacSDimitry Andric bool TargetMachine::useTLSDESC() const { return Options.EnableTLSDESC; } 2370b57cec5SDimitry Andric 2380b57cec5SDimitry Andric TLSModel::Model TargetMachine::getTLSModel(const GlobalValue *GV) const { 2390b57cec5SDimitry Andric bool IsPIE = GV->getParent()->getPIELevel() != PIELevel::Default; 2400b57cec5SDimitry Andric Reloc::Model RM = getRelocationModel(); 2410b57cec5SDimitry Andric bool IsSharedLibrary = RM == Reloc::PIC_ && !IsPIE; 242*0fca6ea1SDimitry Andric bool IsLocal = shouldAssumeDSOLocal(GV); 2430b57cec5SDimitry Andric 2440b57cec5SDimitry Andric TLSModel::Model Model; 2450b57cec5SDimitry Andric if (IsSharedLibrary) { 2460b57cec5SDimitry Andric if (IsLocal) 2470b57cec5SDimitry Andric Model = TLSModel::LocalDynamic; 2480b57cec5SDimitry Andric else 2490b57cec5SDimitry Andric Model = TLSModel::GeneralDynamic; 2500b57cec5SDimitry Andric } else { 2510b57cec5SDimitry Andric if (IsLocal) 2520b57cec5SDimitry Andric Model = TLSModel::LocalExec; 2530b57cec5SDimitry Andric else 2540b57cec5SDimitry Andric Model = TLSModel::InitialExec; 2550b57cec5SDimitry Andric } 2560b57cec5SDimitry Andric 2570b57cec5SDimitry Andric // If the user specified a more specific model, use that. 2580b57cec5SDimitry Andric TLSModel::Model SelectedModel = getSelectedTLSModel(GV); 2590b57cec5SDimitry Andric if (SelectedModel > Model) 2600b57cec5SDimitry Andric return SelectedModel; 2610b57cec5SDimitry Andric 2620b57cec5SDimitry Andric return Model; 2630b57cec5SDimitry Andric } 2640b57cec5SDimitry Andric 2650b57cec5SDimitry Andric /// Returns the optimization level: None, Less, Default, or Aggressive. 2665f757f3fSDimitry Andric CodeGenOptLevel TargetMachine::getOptLevel() const { return OptLevel; } 2670b57cec5SDimitry Andric 2685f757f3fSDimitry Andric void TargetMachine::setOptLevel(CodeGenOptLevel Level) { OptLevel = Level; } 2690b57cec5SDimitry Andric 27081ad6265SDimitry Andric TargetTransformInfo 27181ad6265SDimitry Andric TargetMachine::getTargetTransformInfo(const Function &F) const { 272*0fca6ea1SDimitry Andric return TargetTransformInfo(F.getDataLayout()); 2730b57cec5SDimitry Andric } 2740b57cec5SDimitry Andric 2750b57cec5SDimitry Andric void TargetMachine::getNameWithPrefix(SmallVectorImpl<char> &Name, 2760b57cec5SDimitry Andric const GlobalValue *GV, Mangler &Mang, 2770b57cec5SDimitry Andric bool MayAlwaysUsePrivate) const { 2780b57cec5SDimitry Andric if (MayAlwaysUsePrivate || !GV->hasPrivateLinkage()) { 2790b57cec5SDimitry Andric // Simple case: If GV is not private, it is not important to find out if 2800b57cec5SDimitry Andric // private labels are legal in this case or not. 2810b57cec5SDimitry Andric Mang.getNameWithPrefix(Name, GV, false); 2820b57cec5SDimitry Andric return; 2830b57cec5SDimitry Andric } 2840b57cec5SDimitry Andric const TargetLoweringObjectFile *TLOF = getObjFileLowering(); 2850b57cec5SDimitry Andric TLOF->getNameWithPrefix(Name, GV, *this); 2860b57cec5SDimitry Andric } 2870b57cec5SDimitry Andric 2880b57cec5SDimitry Andric MCSymbol *TargetMachine::getSymbol(const GlobalValue *GV) const { 2890b57cec5SDimitry Andric const TargetLoweringObjectFile *TLOF = getObjFileLowering(); 2905ffd83dbSDimitry Andric // XCOFF symbols could have special naming convention. 2915ffd83dbSDimitry Andric if (MCSymbol *TargetSymbol = TLOF->getTargetSymbol(GV, *this)) 2925ffd83dbSDimitry Andric return TargetSymbol; 2935ffd83dbSDimitry Andric 2940b57cec5SDimitry Andric SmallString<128> NameStr; 2950b57cec5SDimitry Andric getNameWithPrefix(NameStr, GV, TLOF->getMangler()); 2960b57cec5SDimitry Andric return TLOF->getContext().getOrCreateSymbol(NameStr); 2970b57cec5SDimitry Andric } 2980b57cec5SDimitry Andric 29981ad6265SDimitry Andric TargetIRAnalysis TargetMachine::getTargetIRAnalysis() const { 3000b57cec5SDimitry Andric // Since Analysis can't depend on Target, use a std::function to invert the 3010b57cec5SDimitry Andric // dependency. 3020b57cec5SDimitry Andric return TargetIRAnalysis( 3030b57cec5SDimitry Andric [this](const Function &F) { return this->getTargetTransformInfo(F); }); 3040b57cec5SDimitry Andric } 305e8d8bef9SDimitry Andric 306e8d8bef9SDimitry Andric std::pair<int, int> TargetMachine::parseBinutilsVersion(StringRef Version) { 307e8d8bef9SDimitry Andric if (Version == "none") 308e8d8bef9SDimitry Andric return {INT_MAX, INT_MAX}; // Make binutilsIsAtLeast() return true. 309e8d8bef9SDimitry Andric std::pair<int, int> Ret; 310e8d8bef9SDimitry Andric if (!Version.consumeInteger(10, Ret.first) && Version.consume_front(".")) 311e8d8bef9SDimitry Andric Version.consumeInteger(10, Ret.second); 312e8d8bef9SDimitry Andric return Ret; 313e8d8bef9SDimitry Andric } 314