1 //===-- TargetMachine.cpp - General Target Information ---------------------==//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file describes the general parts of a Target machine.
12 //===----------------------------------------------------------------------===//
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"
30 //---------------------------------------------------------------------------
31 // Command-line options that tend to be useful on more than one back-end.
35 bool HasDivModLibcall;
36 bool AsmVerbosityDefault(false);
40 DataSections("fdata-sections",
41 cl::desc("Emit data into separate sections"),
44 FunctionSections("ffunction-sections",
45 cl::desc("Emit functions into separate sections"),
48 //---------------------------------------------------------------------------
49 // TargetMachine Class
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(0), AsmInfo(0),
59 MCSaveTempLabels(false),
61 MCUseDwarfDirectory(false),
62 RequireStructuredCFG(false),
66 TargetMachine::~TargetMachine() {
71 /// \brief Reset the target options based on the function's attributes.
72 void TargetMachine::resetTargetOptions(const MachineFunction *MF) const {
73 const Function *F = MF->getFunction();
74 TargetOptions &TO = MF->getTarget().Options;
76 #define RESET_OPTION(X, Y) \
78 if (F->hasFnAttribute(Y)) \
80 (F->getAttributes(). \
81 getAttribute(AttributeSet::FunctionIndex, \
82 Y).getValueAsString() == "true"); \
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");
94 /// getRelocationModel - Returns the code generation relocation model. The
95 /// choices are static, PIC, and dynamic-no-pic, and target default.
96 Reloc::Model TargetMachine::getRelocationModel() const {
98 return Reloc::Default;
99 return CodeGenInfo->getRelocationModel();
102 /// getCodeModel - Returns the code model. The choices are small, kernel,
103 /// medium, large, and target default.
104 CodeModel::Model TargetMachine::getCodeModel() const {
106 return CodeModel::Default;
107 return CodeGenInfo->getCodeModel();
110 /// Get the IR-specified TLS model for Var.
111 static TLSModel::Model getSelectedTLSModel(const GlobalVariable *Var) {
112 switch (Var->getThreadLocalMode()) {
113 case GlobalVariable::NotThreadLocal:
114 llvm_unreachable("getSelectedTLSModel for non-TLS variable");
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;
125 llvm_unreachable("invalid TLS model");
128 TLSModel::Model TargetMachine::getTLSModel(const GlobalValue *GV) const {
129 // If GV is an alias then use the aliasee for determining
131 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
132 GV = GA->resolveAliasedGlobal(false);
133 const GlobalVariable *Var = cast<GlobalVariable>(GV);
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();
143 TLSModel::Model Model;
144 if (isPIC && !isPIE) {
145 if (isLocal || isHidden)
146 Model = TLSModel::LocalDynamic;
148 Model = TLSModel::GeneralDynamic;
150 if (!isDeclaration || isHidden)
151 Model = TLSModel::LocalExec;
153 Model = TLSModel::InitialExec;
156 // If the user specified a more specific model, use that.
157 TLSModel::Model SelectedModel = getSelectedTLSModel(Var);
158 if (SelectedModel > Model)
159 return SelectedModel;
164 /// getOptLevel - Returns the optimization level: None, Less,
165 /// Default, or Aggressive.
166 CodeGenOpt::Level TargetMachine::getOptLevel() const {
168 return CodeGenOpt::Default;
169 return CodeGenInfo->getOptLevel();
172 void TargetMachine::setOptLevel(CodeGenOpt::Level Level) const {
174 CodeGenInfo->setOptLevel(Level);
177 bool TargetMachine::getAsmVerbosityDefault() {
178 return AsmVerbosityDefault;
181 void TargetMachine::setAsmVerbosityDefault(bool V) {
182 AsmVerbosityDefault = V;
185 bool TargetMachine::getFunctionSections() {
186 return FunctionSections;
189 bool TargetMachine::getDataSections() {
193 void TargetMachine::setFunctionSections(bool V) {
194 FunctionSections = V;
197 void TargetMachine::setDataSections(bool V) {
201 void 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);
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);
218 MCSymbol *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());