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 DebugUseUniqueSections(false),
59 RequireStructuredCFG(false),
63 TargetMachine::~TargetMachine() {
68 /// \brief Reset the target options based on the function's attributes.
69 void TargetMachine::resetTargetOptions(const MachineFunction *MF) const {
70 const Function *F = MF->getFunction();
71 TargetOptions &TO = MF->getTarget().Options;
73 #define RESET_OPTION(X, Y) \
75 if (F->hasFnAttribute(Y)) \
77 (F->getAttributes(). \
78 getAttribute(AttributeSet::FunctionIndex, \
79 Y).getValueAsString() == "true"); \
82 RESET_OPTION(NoFramePointerElim, "no-frame-pointer-elim");
83 RESET_OPTION(LessPreciseFPMADOption, "less-precise-fpmad");
84 RESET_OPTION(UnsafeFPMath, "unsafe-fp-math");
85 RESET_OPTION(NoInfsFPMath, "no-infs-fp-math");
86 RESET_OPTION(NoNaNsFPMath, "no-nans-fp-math");
87 RESET_OPTION(UseSoftFloat, "use-soft-float");
88 RESET_OPTION(DisableTailCalls, "disable-tail-calls");
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 {
95 return Reloc::Default;
96 return CodeGenInfo->getRelocationModel();
99 /// getCodeModel - Returns the code model. The choices are small, kernel,
100 /// medium, large, and target default.
101 CodeModel::Model TargetMachine::getCodeModel() const {
103 return CodeModel::Default;
104 return CodeGenInfo->getCodeModel();
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");
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;
122 llvm_unreachable("invalid TLS model");
125 TLSModel::Model TargetMachine::getTLSModel(const GlobalValue *GV) const {
126 // If GV is an alias then use the aliasee for determining
128 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
129 GV = GA->resolveAliasedGlobal(false);
130 const GlobalVariable *Var = cast<GlobalVariable>(GV);
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();
140 TLSModel::Model Model;
141 if (isPIC && !isPIE) {
142 if (isLocal || isHidden)
143 Model = TLSModel::LocalDynamic;
145 Model = TLSModel::GeneralDynamic;
147 if (!isDeclaration || isHidden)
148 Model = TLSModel::LocalExec;
150 Model = TLSModel::InitialExec;
153 // If the user specified a more specific model, use that.
154 TLSModel::Model SelectedModel = getSelectedTLSModel(Var);
155 if (SelectedModel > Model)
156 return SelectedModel;
161 /// getOptLevel - Returns the optimization level: None, Less,
162 /// Default, or Aggressive.
163 CodeGenOpt::Level TargetMachine::getOptLevel() const {
165 return CodeGenOpt::Default;
166 return CodeGenInfo->getOptLevel();
169 void TargetMachine::setOptLevel(CodeGenOpt::Level Level) const {
171 CodeGenInfo->setOptLevel(Level);
174 bool TargetMachine::getAsmVerbosityDefault() {
175 return AsmVerbosityDefault;
178 void TargetMachine::setAsmVerbosityDefault(bool V) {
179 AsmVerbosityDefault = V;
182 bool TargetMachine::getFunctionSections() {
183 return FunctionSections;
186 bool TargetMachine::getDataSections() {
190 void TargetMachine::setFunctionSections(bool V) {
191 FunctionSections = V;
194 void TargetMachine::setDataSections(bool V) {