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/CodeGen/MachineFunction.h" 16 #include "llvm/IR/Function.h" 17 #include "llvm/IR/GlobalAlias.h" 18 #include "llvm/IR/GlobalValue.h" 19 #include "llvm/IR/GlobalVariable.h" 20 #include "llvm/IR/Mangler.h" 21 #include "llvm/MC/MCAsmInfo.h" 22 #include "llvm/MC/MCCodeGenInfo.h" 23 #include "llvm/MC/MCContext.h" 24 #include "llvm/MC/MCTargetOptions.h" 25 #include "llvm/MC/SectionKind.h" 26 #include "llvm/Support/CommandLine.h" 27 #include "llvm/Target/TargetLowering.h" 28 #include "llvm/Target/TargetLoweringObjectFile.h" 29 using namespace llvm; 30 31 //--------------------------------------------------------------------------- 32 // Command-line options that tend to be useful on more than one back-end. 33 // 34 35 namespace llvm { 36 bool HasDivModLibcall; 37 bool AsmVerbosityDefault(false); 38 } 39 40 static cl::opt<bool> 41 DataSections("fdata-sections", 42 cl::desc("Emit data into separate sections"), 43 cl::init(false)); 44 static cl::opt<bool> 45 FunctionSections("ffunction-sections", 46 cl::desc("Emit functions into separate sections"), 47 cl::init(false)); 48 49 //--------------------------------------------------------------------------- 50 // TargetMachine Class 51 // 52 53 TargetMachine::TargetMachine(const Target &T, 54 StringRef TT, StringRef CPU, StringRef FS, 55 const TargetOptions &Options) 56 : TheTarget(T), TargetTriple(TT), TargetCPU(CPU), TargetFS(FS), 57 CodeGenInfo(nullptr), AsmInfo(nullptr), 58 MCRelaxAll(false), 59 MCNoExecStack(false), 60 MCSaveTempLabels(false), 61 MCUseCFI(true), 62 MCUseDwarfDirectory(false), 63 RequireStructuredCFG(false), 64 Options(Options) { 65 } 66 67 TargetMachine::~TargetMachine() { 68 delete CodeGenInfo; 69 delete AsmInfo; 70 } 71 72 /// \brief Reset the target options based on the function's attributes. 73 void TargetMachine::resetTargetOptions(const MachineFunction *MF) const { 74 const Function *F = MF->getFunction(); 75 TargetOptions &TO = MF->getTarget().Options; 76 77 #define RESET_OPTION(X, Y) \ 78 do { \ 79 if (F->hasFnAttribute(Y)) \ 80 TO.X = \ 81 (F->getAttributes(). \ 82 getAttribute(AttributeSet::FunctionIndex, \ 83 Y).getValueAsString() == "true"); \ 84 } while (0) 85 86 RESET_OPTION(NoFramePointerElim, "no-frame-pointer-elim"); 87 RESET_OPTION(LessPreciseFPMADOption, "less-precise-fpmad"); 88 RESET_OPTION(UnsafeFPMath, "unsafe-fp-math"); 89 RESET_OPTION(NoInfsFPMath, "no-infs-fp-math"); 90 RESET_OPTION(NoNaNsFPMath, "no-nans-fp-math"); 91 RESET_OPTION(UseSoftFloat, "use-soft-float"); 92 RESET_OPTION(DisableTailCalls, "disable-tail-calls"); 93 94 TO.MCOptions.SanitizeAddress = F->hasFnAttribute(Attribute::SanitizeAddress); 95 } 96 97 /// getRelocationModel - Returns the code generation relocation model. The 98 /// choices are static, PIC, and dynamic-no-pic, and target default. 99 Reloc::Model TargetMachine::getRelocationModel() const { 100 if (!CodeGenInfo) 101 return Reloc::Default; 102 return CodeGenInfo->getRelocationModel(); 103 } 104 105 /// getCodeModel - Returns the code model. The choices are small, kernel, 106 /// medium, large, and target default. 107 CodeModel::Model TargetMachine::getCodeModel() const { 108 if (!CodeGenInfo) 109 return CodeModel::Default; 110 return CodeGenInfo->getCodeModel(); 111 } 112 113 /// Get the IR-specified TLS model for Var. 114 static TLSModel::Model getSelectedTLSModel(const GlobalVariable *Var) { 115 switch (Var->getThreadLocalMode()) { 116 case GlobalVariable::NotThreadLocal: 117 llvm_unreachable("getSelectedTLSModel for non-TLS variable"); 118 break; 119 case GlobalVariable::GeneralDynamicTLSModel: 120 return TLSModel::GeneralDynamic; 121 case GlobalVariable::LocalDynamicTLSModel: 122 return TLSModel::LocalDynamic; 123 case GlobalVariable::InitialExecTLSModel: 124 return TLSModel::InitialExec; 125 case GlobalVariable::LocalExecTLSModel: 126 return TLSModel::LocalExec; 127 } 128 llvm_unreachable("invalid TLS model"); 129 } 130 131 TLSModel::Model TargetMachine::getTLSModel(const GlobalValue *GV) const { 132 // If GV is an alias then use the aliasee for determining 133 // thread-localness. 134 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV)) 135 GV = GA->getAliasedGlobal(); 136 const GlobalVariable *Var = cast<GlobalVariable>(GV); 137 138 bool isLocal = Var->hasLocalLinkage(); 139 bool isDeclaration = Var->isDeclaration(); 140 bool isPIC = getRelocationModel() == Reloc::PIC_; 141 bool isPIE = Options.PositionIndependentExecutable; 142 // FIXME: what should we do for protected and internal visibility? 143 // For variables, is internal different from hidden? 144 bool isHidden = Var->hasHiddenVisibility(); 145 146 TLSModel::Model Model; 147 if (isPIC && !isPIE) { 148 if (isLocal || isHidden) 149 Model = TLSModel::LocalDynamic; 150 else 151 Model = TLSModel::GeneralDynamic; 152 } else { 153 if (!isDeclaration || isHidden) 154 Model = TLSModel::LocalExec; 155 else 156 Model = TLSModel::InitialExec; 157 } 158 159 // If the user specified a more specific model, use that. 160 TLSModel::Model SelectedModel = getSelectedTLSModel(Var); 161 if (SelectedModel > Model) 162 return SelectedModel; 163 164 return Model; 165 } 166 167 /// getOptLevel - Returns the optimization level: None, Less, 168 /// Default, or Aggressive. 169 CodeGenOpt::Level TargetMachine::getOptLevel() const { 170 if (!CodeGenInfo) 171 return CodeGenOpt::Default; 172 return CodeGenInfo->getOptLevel(); 173 } 174 175 void TargetMachine::setOptLevel(CodeGenOpt::Level Level) const { 176 if (CodeGenInfo) 177 CodeGenInfo->setOptLevel(Level); 178 } 179 180 bool TargetMachine::getAsmVerbosityDefault() { 181 return AsmVerbosityDefault; 182 } 183 184 void TargetMachine::setAsmVerbosityDefault(bool V) { 185 AsmVerbosityDefault = V; 186 } 187 188 bool TargetMachine::getFunctionSections() { 189 return FunctionSections; 190 } 191 192 bool TargetMachine::getDataSections() { 193 return DataSections; 194 } 195 196 void TargetMachine::setFunctionSections(bool V) { 197 FunctionSections = V; 198 } 199 200 void TargetMachine::setDataSections(bool V) { 201 DataSections = V; 202 } 203 204 void TargetMachine::getNameWithPrefix(SmallVectorImpl<char> &Name, 205 const GlobalValue *GV, Mangler &Mang, 206 bool MayAlwaysUsePrivate) const { 207 if (MayAlwaysUsePrivate || !GV->hasPrivateLinkage()) { 208 // Simple case: If GV is not private, it is not important to find out if 209 // private labels are legal in this case or not. 210 Mang.getNameWithPrefix(Name, GV, false); 211 return; 212 } 213 SectionKind GVKind = TargetLoweringObjectFile::getKindForGlobal(GV, *this); 214 const TargetLoweringObjectFile &TLOF = 215 getTargetLowering()->getObjFileLowering(); 216 const MCSection *TheSection = TLOF.SectionForGlobal(GV, GVKind, Mang, *this); 217 bool CannotUsePrivateLabel = TLOF.isSectionAtomizableBySymbols(*TheSection); 218 Mang.getNameWithPrefix(Name, GV, CannotUsePrivateLabel); 219 } 220 221 MCSymbol *TargetMachine::getSymbol(const GlobalValue *GV, Mangler &Mang) const { 222 SmallString<60> NameStr; 223 getNameWithPrefix(NameStr, GV, Mang); 224 const TargetLoweringObjectFile &TLOF = 225 getTargetLowering()->getObjFileLowering(); 226 return TLOF.getContext().GetOrCreateSymbol(NameStr.str()); 227 } 228