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