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