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