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/MC/MCAsmInfo.h"
21 #include "llvm/MC/MCCodeGenInfo.h"
22 #include "llvm/Support/CommandLine.h"
25 //---------------------------------------------------------------------------
26 // Command-line options that tend to be useful on more than one back-end.
30 bool HasDivModLibcall;
31 bool AsmVerbosityDefault(false);
35 DataSections("fdata-sections",
36 cl::desc("Emit data into separate sections"),
39 FunctionSections("ffunction-sections",
40 cl::desc("Emit functions into separate sections"),
43 //---------------------------------------------------------------------------
44 // TargetMachine Class
47 TargetMachine::TargetMachine(const Target &T,
48 StringRef TT, StringRef CPU, StringRef FS,
49 const TargetOptions &Options)
50 : TheTarget(T), TargetTriple(TT), TargetCPU(CPU), TargetFS(FS),
51 CodeGenInfo(0), AsmInfo(0),
54 MCSaveTempLabels(false),
57 MCUseDwarfDirectory(false),
58 RequireStructuredCFG(false),
62 TargetMachine::~TargetMachine() {
67 /// \brief Reset the target options based on the function's attributes.
68 void TargetMachine::resetTargetOptions(const MachineFunction *MF) const {
69 const Function *F = MF->getFunction();
70 TargetOptions &TO = MF->getTarget().Options;
72 #define RESET_OPTION(X, Y) \
74 if (F->hasFnAttribute(Y)) \
76 (F->getAttributes(). \
77 getAttribute(AttributeSet::FunctionIndex, \
78 Y).getValueAsString() == "true"); \
81 RESET_OPTION(NoFramePointerElim, "no-frame-pointer-elim");
82 RESET_OPTION(LessPreciseFPMADOption, "less-precise-fpmad");
83 RESET_OPTION(UnsafeFPMath, "unsafe-fp-math");
84 RESET_OPTION(NoInfsFPMath, "no-infs-fp-math");
85 RESET_OPTION(NoNaNsFPMath, "no-nans-fp-math");
86 RESET_OPTION(UseSoftFloat, "use-soft-float");
87 RESET_OPTION(DisableTailCalls, "disable-tail-calls");
90 /// getRelocationModel - Returns the code generation relocation model. The
91 /// choices are static, PIC, and dynamic-no-pic, and target default.
92 Reloc::Model TargetMachine::getRelocationModel() const {
94 return Reloc::Default;
95 return CodeGenInfo->getRelocationModel();
98 /// getCodeModel - Returns the code model. The choices are small, kernel,
99 /// medium, large, and target default.
100 CodeModel::Model TargetMachine::getCodeModel() const {
102 return CodeModel::Default;
103 return CodeGenInfo->getCodeModel();
106 /// Get the IR-specified TLS model for Var.
107 static TLSModel::Model getSelectedTLSModel(const GlobalVariable *Var) {
108 switch (Var->getThreadLocalMode()) {
109 case GlobalVariable::NotThreadLocal:
110 llvm_unreachable("getSelectedTLSModel for non-TLS variable");
112 case GlobalVariable::GeneralDynamicTLSModel:
113 return TLSModel::GeneralDynamic;
114 case GlobalVariable::LocalDynamicTLSModel:
115 return TLSModel::LocalDynamic;
116 case GlobalVariable::InitialExecTLSModel:
117 return TLSModel::InitialExec;
118 case GlobalVariable::LocalExecTLSModel:
119 return TLSModel::LocalExec;
121 llvm_unreachable("invalid TLS model");
124 TLSModel::Model TargetMachine::getTLSModel(const GlobalValue *GV) const {
125 // If GV is an alias then use the aliasee for determining
127 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
128 GV = GA->resolveAliasedGlobal(false);
129 const GlobalVariable *Var = cast<GlobalVariable>(GV);
131 bool isLocal = Var->hasLocalLinkage();
132 bool isDeclaration = Var->isDeclaration();
133 bool isPIC = getRelocationModel() == Reloc::PIC_;
134 bool isPIE = Options.PositionIndependentExecutable;
135 // FIXME: what should we do for protected and internal visibility?
136 // For variables, is internal different from hidden?
137 bool isHidden = Var->hasHiddenVisibility();
139 TLSModel::Model Model;
140 if (isPIC && !isPIE) {
141 if (isLocal || isHidden)
142 Model = TLSModel::LocalDynamic;
144 Model = TLSModel::GeneralDynamic;
146 if (!isDeclaration || isHidden)
147 Model = TLSModel::LocalExec;
149 Model = TLSModel::InitialExec;
152 // If the user specified a more specific model, use that.
153 TLSModel::Model SelectedModel = getSelectedTLSModel(Var);
154 if (SelectedModel > Model)
155 return SelectedModel;
160 /// getOptLevel - Returns the optimization level: None, Less,
161 /// Default, or Aggressive.
162 CodeGenOpt::Level TargetMachine::getOptLevel() const {
164 return CodeGenOpt::Default;
165 return CodeGenInfo->getOptLevel();
168 void TargetMachine::setOptLevel(CodeGenOpt::Level Level) const {
170 CodeGenInfo->setOptLevel(Level);
173 bool TargetMachine::getAsmVerbosityDefault() {
174 return AsmVerbosityDefault;
177 void TargetMachine::setAsmVerbosityDefault(bool V) {
178 AsmVerbosityDefault = V;
181 bool TargetMachine::getFunctionSections() {
182 return FunctionSections;
185 bool TargetMachine::getDataSections() {
189 void TargetMachine::setFunctionSections(bool V) {
190 FunctionSections = V;
193 void TargetMachine::setDataSections(bool V) {