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/IR/GlobalAlias.h"
16 #include "llvm/IR/GlobalValue.h"
17 #include "llvm/IR/GlobalVariable.h"
18 #include "llvm/MC/MCAsmInfo.h"
19 #include "llvm/MC/MCCodeGenInfo.h"
20 #include "llvm/Support/CommandLine.h"
23 //---------------------------------------------------------------------------
24 // Command-line options that tend to be useful on more than one back-end.
28 bool HasDivModLibcall;
29 bool AsmVerbosityDefault(false);
33 DataSections("fdata-sections",
34 cl::desc("Emit data into separate sections"),
37 FunctionSections("ffunction-sections",
38 cl::desc("Emit functions into separate sections"),
41 //---------------------------------------------------------------------------
42 // TargetMachine Class
45 TargetMachine::TargetMachine(const Target &T,
46 StringRef TT, StringRef CPU, StringRef FS,
47 const TargetOptions &Options)
48 : TheTarget(T), TargetTriple(TT), TargetCPU(CPU), TargetFS(FS),
49 CodeGenInfo(0), AsmInfo(0),
52 MCSaveTempLabels(false),
55 MCUseDwarfDirectory(false),
59 TargetMachine::~TargetMachine() {
64 /// getRelocationModel - Returns the code generation relocation model. The
65 /// choices are static, PIC, and dynamic-no-pic, and target default.
66 Reloc::Model TargetMachine::getRelocationModel() const {
68 return Reloc::Default;
69 return CodeGenInfo->getRelocationModel();
72 /// getCodeModel - Returns the code model. The choices are small, kernel,
73 /// medium, large, and target default.
74 CodeModel::Model TargetMachine::getCodeModel() const {
76 return CodeModel::Default;
77 return CodeGenInfo->getCodeModel();
80 /// Get the IR-specified TLS model for Var.
81 static TLSModel::Model getSelectedTLSModel(const GlobalVariable *Var) {
82 switch (Var->getThreadLocalMode()) {
83 case GlobalVariable::NotThreadLocal:
84 llvm_unreachable("getSelectedTLSModel for non-TLS variable");
86 case GlobalVariable::GeneralDynamicTLSModel:
87 return TLSModel::GeneralDynamic;
88 case GlobalVariable::LocalDynamicTLSModel:
89 return TLSModel::LocalDynamic;
90 case GlobalVariable::InitialExecTLSModel:
91 return TLSModel::InitialExec;
92 case GlobalVariable::LocalExecTLSModel:
93 return TLSModel::LocalExec;
95 llvm_unreachable("invalid TLS model");
98 TLSModel::Model TargetMachine::getTLSModel(const GlobalValue *GV) const {
99 // If GV is an alias then use the aliasee for determining
101 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
102 GV = GA->resolveAliasedGlobal(false);
103 const GlobalVariable *Var = cast<GlobalVariable>(GV);
105 bool isLocal = Var->hasLocalLinkage();
106 bool isDeclaration = Var->isDeclaration();
107 bool isPIC = getRelocationModel() == Reloc::PIC_;
108 bool isPIE = Options.PositionIndependentExecutable;
109 // FIXME: what should we do for protected and internal visibility?
110 // For variables, is internal different from hidden?
111 bool isHidden = Var->hasHiddenVisibility();
113 TLSModel::Model Model;
114 if (isPIC && !isPIE) {
115 if (isLocal || isHidden)
116 Model = TLSModel::LocalDynamic;
118 Model = TLSModel::GeneralDynamic;
120 if (!isDeclaration || isHidden)
121 Model = TLSModel::LocalExec;
123 Model = TLSModel::InitialExec;
126 // If the user specified a more specific model, use that.
127 TLSModel::Model SelectedModel = getSelectedTLSModel(Var);
128 if (SelectedModel > Model)
129 return SelectedModel;
134 /// getOptLevel - Returns the optimization level: None, Less,
135 /// Default, or Aggressive.
136 CodeGenOpt::Level TargetMachine::getOptLevel() const {
138 return CodeGenOpt::Default;
139 return CodeGenInfo->getOptLevel();
142 bool TargetMachine::getAsmVerbosityDefault() {
143 return AsmVerbosityDefault;
146 void TargetMachine::setAsmVerbosityDefault(bool V) {
147 AsmVerbosityDefault = V;
150 bool TargetMachine::getFunctionSections() {
151 return FunctionSections;
154 bool TargetMachine::getDataSections() {
158 void TargetMachine::setFunctionSections(bool V) {
159 FunctionSections = V;
162 void TargetMachine::setDataSections(bool V) {