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/MCCodeGenInfo.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 #include "llvm/Target/TargetLowering.h" 31 #include "llvm/Target/TargetLoweringObjectFile.h" 32 #include "llvm/Target/TargetSubtargetInfo.h" 33 using namespace llvm; 34 35 //--------------------------------------------------------------------------- 36 // TargetMachine Class 37 // 38 39 TargetMachine::TargetMachine(const Target &T, StringRef DataLayoutString, 40 const Triple &TT, StringRef CPU, StringRef FS, 41 const TargetOptions &Options) 42 : TheTarget(T), DL(DataLayoutString), TargetTriple(TT), TargetCPU(CPU), 43 TargetFS(FS), CodeGenInfo(nullptr), AsmInfo(nullptr), MRI(nullptr), 44 MII(nullptr), STI(nullptr), RequireStructuredCFG(false), 45 Options(Options) {} 46 47 TargetMachine::~TargetMachine() { 48 delete CodeGenInfo; 49 delete AsmInfo; 50 delete MRI; 51 delete MII; 52 delete STI; 53 } 54 55 bool TargetMachine::isPositionIndependent() const { 56 return getRelocationModel() == Reloc::PIC_; 57 } 58 59 /// \brief Reset the target options based on the function's attributes. 60 // FIXME: This function needs to go away for a number of reasons: 61 // a) global state on the TargetMachine is terrible in general, 62 // b) there's no default state here to keep, 63 // c) these target options should be passed only on the function 64 // and not on the TargetMachine (via TargetOptions) at all. 65 void TargetMachine::resetTargetOptions(const Function &F) const { 66 #define RESET_OPTION(X, Y) \ 67 do { \ 68 if (F.hasFnAttribute(Y)) \ 69 Options.X = (F.getFnAttribute(Y).getValueAsString() == "true"); \ 70 } while (0) 71 72 RESET_OPTION(LessPreciseFPMADOption, "less-precise-fpmad"); 73 RESET_OPTION(UnsafeFPMath, "unsafe-fp-math"); 74 RESET_OPTION(NoInfsFPMath, "no-infs-fp-math"); 75 RESET_OPTION(NoNaNsFPMath, "no-nans-fp-math"); 76 } 77 78 /// Returns the code generation relocation model. The choices are static, PIC, 79 /// and dynamic-no-pic. 80 Reloc::Model TargetMachine::getRelocationModel() const { 81 if (!CodeGenInfo) 82 return Reloc::Static; // FIXME 83 return CodeGenInfo->getRelocationModel(); 84 } 85 86 /// Returns the code model. The choices are small, kernel, medium, large, and 87 /// target default. 88 CodeModel::Model TargetMachine::getCodeModel() const { 89 if (!CodeGenInfo) 90 return CodeModel::Default; 91 return CodeGenInfo->getCodeModel(); 92 } 93 94 /// Get the IR-specified TLS model for Var. 95 static TLSModel::Model getSelectedTLSModel(const GlobalValue *GV) { 96 switch (GV->getThreadLocalMode()) { 97 case GlobalVariable::NotThreadLocal: 98 llvm_unreachable("getSelectedTLSModel for non-TLS variable"); 99 break; 100 case GlobalVariable::GeneralDynamicTLSModel: 101 return TLSModel::GeneralDynamic; 102 case GlobalVariable::LocalDynamicTLSModel: 103 return TLSModel::LocalDynamic; 104 case GlobalVariable::InitialExecTLSModel: 105 return TLSModel::InitialExec; 106 case GlobalVariable::LocalExecTLSModel: 107 return TLSModel::LocalExec; 108 } 109 llvm_unreachable("invalid TLS model"); 110 } 111 112 // FIXME: make this a proper option 113 static bool CanUseCopyRelocWithPIE = false; 114 115 bool TargetMachine::shouldAssumeDSOLocal(const Module &M, 116 const GlobalValue *GV) const { 117 Reloc::Model RM = getRelocationModel(); 118 const Triple &TT = getTargetTriple(); 119 120 // DLLImport explicitly marks the GV as external. 121 if (GV && GV->hasDLLImportStorageClass()) 122 return false; 123 124 // Every other GV is local on COFF 125 if (TT.isOSBinFormatCOFF()) 126 return true; 127 128 if (GV && (GV->hasLocalLinkage() || !GV->hasDefaultVisibility())) 129 return true; 130 131 if (TT.isOSBinFormatMachO()) { 132 if (RM == Reloc::Static) 133 return true; 134 return GV && GV->isStrongDefinitionForLinker(); 135 } 136 137 assert(TT.isOSBinFormatELF()); 138 assert(RM != Reloc::DynamicNoPIC); 139 140 bool IsExecutable = 141 RM == Reloc::Static || M.getPIELevel() != PIELevel::Default; 142 if (IsExecutable) { 143 // If the symbol is defined, it cannot be preempted. 144 if (GV && !GV->isDeclarationForLinker()) 145 return true; 146 147 bool IsTLS = GV && GV->isThreadLocal(); 148 // Check if we can use copy relocations. 149 if (!IsTLS && (RM == Reloc::Static || CanUseCopyRelocWithPIE)) 150 return true; 151 } 152 153 // ELF supports preemption of other symbols. 154 return false; 155 } 156 157 TLSModel::Model TargetMachine::getTLSModel(const GlobalValue *GV) const { 158 bool IsPIE = GV->getParent()->getPIELevel() != PIELevel::Default; 159 Reloc::Model RM = getRelocationModel(); 160 bool IsSharedLibrary = RM == Reloc::PIC_ && !IsPIE; 161 bool IsLocal = shouldAssumeDSOLocal(*GV->getParent(), GV); 162 163 TLSModel::Model Model; 164 if (IsSharedLibrary) { 165 if (IsLocal) 166 Model = TLSModel::LocalDynamic; 167 else 168 Model = TLSModel::GeneralDynamic; 169 } else { 170 if (IsLocal) 171 Model = TLSModel::LocalExec; 172 else 173 Model = TLSModel::InitialExec; 174 } 175 176 // If the user specified a more specific model, use that. 177 TLSModel::Model SelectedModel = getSelectedTLSModel(GV); 178 if (SelectedModel > Model) 179 return SelectedModel; 180 181 return Model; 182 } 183 184 /// Returns the optimization level: None, Less, Default, or Aggressive. 185 CodeGenOpt::Level TargetMachine::getOptLevel() const { 186 if (!CodeGenInfo) 187 return CodeGenOpt::Default; 188 return CodeGenInfo->getOptLevel(); 189 } 190 191 void TargetMachine::setOptLevel(CodeGenOpt::Level Level) const { 192 if (CodeGenInfo) 193 CodeGenInfo->setOptLevel(Level); 194 } 195 196 TargetIRAnalysis TargetMachine::getTargetIRAnalysis() { 197 return TargetIRAnalysis([this](const Function &F) { 198 return TargetTransformInfo(F.getParent()->getDataLayout()); 199 }); 200 } 201 202 void TargetMachine::getNameWithPrefix(SmallVectorImpl<char> &Name, 203 const GlobalValue *GV, Mangler &Mang, 204 bool MayAlwaysUsePrivate) const { 205 if (MayAlwaysUsePrivate || !GV->hasPrivateLinkage()) { 206 // Simple case: If GV is not private, it is not important to find out if 207 // private labels are legal in this case or not. 208 Mang.getNameWithPrefix(Name, GV, false); 209 return; 210 } 211 const TargetLoweringObjectFile *TLOF = getObjFileLowering(); 212 TLOF->getNameWithPrefix(Name, GV, Mang, *this); 213 } 214 215 MCSymbol *TargetMachine::getSymbol(const GlobalValue *GV, Mangler &Mang) const { 216 SmallString<128> NameStr; 217 getNameWithPrefix(NameStr, GV, Mang); 218 const TargetLoweringObjectFile *TLOF = getObjFileLowering(); 219 return TLOF->getContext().getOrCreateSymbol(NameStr); 220 } 221