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