LLVM API Documentation
00001 //===-- TargetMachine.cpp - General Target Information ---------------------==// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file describes the general parts of a Target machine. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #include "llvm/Target/TargetMachine.h" 00015 #include "llvm/CodeGen/MachineFunction.h" 00016 #include "llvm/IR/Function.h" 00017 #include "llvm/IR/GlobalAlias.h" 00018 #include "llvm/IR/GlobalValue.h" 00019 #include "llvm/IR/GlobalVariable.h" 00020 #include "llvm/IR/Mangler.h" 00021 #include "llvm/MC/MCAsmInfo.h" 00022 #include "llvm/MC/MCCodeGenInfo.h" 00023 #include "llvm/MC/MCContext.h" 00024 #include "llvm/MC/MCTargetOptions.h" 00025 #include "llvm/MC/SectionKind.h" 00026 #include "llvm/Support/CommandLine.h" 00027 #include "llvm/Target/TargetLowering.h" 00028 #include "llvm/Target/TargetLoweringObjectFile.h" 00029 #include "llvm/Target/TargetSubtargetInfo.h" 00030 using namespace llvm; 00031 00032 //--------------------------------------------------------------------------- 00033 // TargetMachine Class 00034 // 00035 00036 TargetMachine::TargetMachine(const Target &T, 00037 StringRef TT, StringRef CPU, StringRef FS, 00038 const TargetOptions &Options) 00039 : TheTarget(T), TargetTriple(TT), TargetCPU(CPU), TargetFS(FS), 00040 CodeGenInfo(nullptr), AsmInfo(nullptr), 00041 RequireStructuredCFG(false), 00042 Options(Options) { 00043 } 00044 00045 TargetMachine::~TargetMachine() { 00046 delete CodeGenInfo; 00047 delete AsmInfo; 00048 } 00049 00050 /// \brief Reset the target options based on the function's attributes. 00051 void TargetMachine::resetTargetOptions(const MachineFunction *MF) const { 00052 const Function *F = MF->getFunction(); 00053 TargetOptions &TO = MF->getTarget().Options; 00054 00055 #define RESET_OPTION(X, Y) \ 00056 do { \ 00057 if (F->hasFnAttribute(Y)) \ 00058 TO.X = \ 00059 (F->getAttributes(). \ 00060 getAttribute(AttributeSet::FunctionIndex, \ 00061 Y).getValueAsString() == "true"); \ 00062 } while (0) 00063 00064 RESET_OPTION(NoFramePointerElim, "no-frame-pointer-elim"); 00065 RESET_OPTION(LessPreciseFPMADOption, "less-precise-fpmad"); 00066 RESET_OPTION(UnsafeFPMath, "unsafe-fp-math"); 00067 RESET_OPTION(NoInfsFPMath, "no-infs-fp-math"); 00068 RESET_OPTION(NoNaNsFPMath, "no-nans-fp-math"); 00069 RESET_OPTION(UseSoftFloat, "use-soft-float"); 00070 RESET_OPTION(DisableTailCalls, "disable-tail-calls"); 00071 00072 TO.MCOptions.SanitizeAddress = F->hasFnAttribute(Attribute::SanitizeAddress); 00073 } 00074 00075 /// getRelocationModel - Returns the code generation relocation model. The 00076 /// choices are static, PIC, and dynamic-no-pic, and target default. 00077 Reloc::Model TargetMachine::getRelocationModel() const { 00078 if (!CodeGenInfo) 00079 return Reloc::Default; 00080 return CodeGenInfo->getRelocationModel(); 00081 } 00082 00083 /// getCodeModel - Returns the code model. The choices are small, kernel, 00084 /// medium, large, and target default. 00085 CodeModel::Model TargetMachine::getCodeModel() const { 00086 if (!CodeGenInfo) 00087 return CodeModel::Default; 00088 return CodeGenInfo->getCodeModel(); 00089 } 00090 00091 /// Get the IR-specified TLS model for Var. 00092 static TLSModel::Model getSelectedTLSModel(const GlobalValue *GV) { 00093 switch (GV->getThreadLocalMode()) { 00094 case GlobalVariable::NotThreadLocal: 00095 llvm_unreachable("getSelectedTLSModel for non-TLS variable"); 00096 break; 00097 case GlobalVariable::GeneralDynamicTLSModel: 00098 return TLSModel::GeneralDynamic; 00099 case GlobalVariable::LocalDynamicTLSModel: 00100 return TLSModel::LocalDynamic; 00101 case GlobalVariable::InitialExecTLSModel: 00102 return TLSModel::InitialExec; 00103 case GlobalVariable::LocalExecTLSModel: 00104 return TLSModel::LocalExec; 00105 } 00106 llvm_unreachable("invalid TLS model"); 00107 } 00108 00109 TLSModel::Model TargetMachine::getTLSModel(const GlobalValue *GV) const { 00110 bool isLocal = GV->hasLocalLinkage(); 00111 bool isDeclaration = GV->isDeclaration(); 00112 bool isPIC = getRelocationModel() == Reloc::PIC_; 00113 bool isPIE = Options.PositionIndependentExecutable; 00114 // FIXME: what should we do for protected and internal visibility? 00115 // For variables, is internal different from hidden? 00116 bool isHidden = GV->hasHiddenVisibility(); 00117 00118 TLSModel::Model Model; 00119 if (isPIC && !isPIE) { 00120 if (isLocal || isHidden) 00121 Model = TLSModel::LocalDynamic; 00122 else 00123 Model = TLSModel::GeneralDynamic; 00124 } else { 00125 if (!isDeclaration || isHidden) 00126 Model = TLSModel::LocalExec; 00127 else 00128 Model = TLSModel::InitialExec; 00129 } 00130 00131 // If the user specified a more specific model, use that. 00132 TLSModel::Model SelectedModel = getSelectedTLSModel(GV); 00133 if (SelectedModel > Model) 00134 return SelectedModel; 00135 00136 return Model; 00137 } 00138 00139 /// getOptLevel - Returns the optimization level: None, Less, 00140 /// Default, or Aggressive. 00141 CodeGenOpt::Level TargetMachine::getOptLevel() const { 00142 if (!CodeGenInfo) 00143 return CodeGenOpt::Default; 00144 return CodeGenInfo->getOptLevel(); 00145 } 00146 00147 void TargetMachine::setOptLevel(CodeGenOpt::Level Level) const { 00148 if (CodeGenInfo) 00149 CodeGenInfo->setOptLevel(Level); 00150 } 00151 00152 bool TargetMachine::getAsmVerbosityDefault() const { 00153 return Options.MCOptions.AsmVerbose; 00154 } 00155 00156 void TargetMachine::setAsmVerbosityDefault(bool V) { 00157 Options.MCOptions.AsmVerbose = V; 00158 } 00159 00160 bool TargetMachine::getFunctionSections() const { 00161 return Options.FunctionSections; 00162 } 00163 00164 bool TargetMachine::getDataSections() const { 00165 return Options.DataSections; 00166 } 00167 00168 void TargetMachine::setFunctionSections(bool V) { 00169 Options.FunctionSections = V; 00170 } 00171 00172 void TargetMachine::setDataSections(bool V) { 00173 Options.DataSections = V; 00174 } 00175 00176 void TargetMachine::getNameWithPrefix(SmallVectorImpl<char> &Name, 00177 const GlobalValue *GV, Mangler &Mang, 00178 bool MayAlwaysUsePrivate) const { 00179 if (MayAlwaysUsePrivate || !GV->hasPrivateLinkage()) { 00180 // Simple case: If GV is not private, it is not important to find out if 00181 // private labels are legal in this case or not. 00182 Mang.getNameWithPrefix(Name, GV, false); 00183 return; 00184 } 00185 SectionKind GVKind = TargetLoweringObjectFile::getKindForGlobal(GV, *this); 00186 const TargetLoweringObjectFile &TLOF = 00187 getSubtargetImpl()->getTargetLowering()->getObjFileLowering(); 00188 const MCSection *TheSection = TLOF.SectionForGlobal(GV, GVKind, Mang, *this); 00189 bool CannotUsePrivateLabel = TLOF.isSectionAtomizableBySymbols(*TheSection); 00190 Mang.getNameWithPrefix(Name, GV, CannotUsePrivateLabel); 00191 } 00192 00193 MCSymbol *TargetMachine::getSymbol(const GlobalValue *GV, Mangler &Mang) const { 00194 SmallString<60> NameStr; 00195 getNameWithPrefix(NameStr, GV, Mang); 00196 const TargetLoweringObjectFile &TLOF = 00197 getSubtargetImpl()->getTargetLowering()->getObjFileLowering(); 00198 return TLOF.getContext().GetOrCreateSymbol(NameStr.str()); 00199 }