Move all of the header files which are involved in modelling the LLVM IR
[oota-llvm.git] / lib / Target / TargetMachine.cpp
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/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"
21 using namespace llvm;
22
23 //---------------------------------------------------------------------------
24 // Command-line options that tend to be useful on more than one back-end.
25 //
26
27 namespace llvm {
28   bool HasDivModLibcall;
29   bool AsmVerbosityDefault(false);
30 }
31
32 static cl::opt<bool>
33 DataSections("fdata-sections",
34   cl::desc("Emit data into separate sections"),
35   cl::init(false));
36 static cl::opt<bool>
37 FunctionSections("ffunction-sections",
38   cl::desc("Emit functions into separate sections"),
39   cl::init(false));
40
41 //---------------------------------------------------------------------------
42 // TargetMachine Class
43 //
44
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),
50     MCRelaxAll(false),
51     MCNoExecStack(false),
52     MCSaveTempLabels(false),
53     MCUseLoc(true),
54     MCUseCFI(true),
55     MCUseDwarfDirectory(false),
56     Options(Options) {
57 }
58
59 TargetMachine::~TargetMachine() {
60   delete CodeGenInfo;
61   delete AsmInfo;
62 }
63
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 {
67   if (!CodeGenInfo)
68     return Reloc::Default;
69   return CodeGenInfo->getRelocationModel();
70 }
71
72 /// getCodeModel - Returns the code model. The choices are small, kernel,
73 /// medium, large, and target default.
74 CodeModel::Model TargetMachine::getCodeModel() const {
75   if (!CodeGenInfo)
76     return CodeModel::Default;
77   return CodeGenInfo->getCodeModel();
78 }
79
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");
85     break;
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;
94   }
95   llvm_unreachable("invalid TLS model");
96 }
97
98 TLSModel::Model TargetMachine::getTLSModel(const GlobalValue *GV) const {
99   // If GV is an alias then use the aliasee for determining
100   // thread-localness.
101   if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
102     GV = GA->resolveAliasedGlobal(false);
103   const GlobalVariable *Var = cast<GlobalVariable>(GV);
104
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();
112
113   TLSModel::Model Model;
114   if (isPIC && !isPIE) {
115     if (isLocal || isHidden)
116       Model = TLSModel::LocalDynamic;
117     else
118       Model = TLSModel::GeneralDynamic;
119   } else {
120     if (!isDeclaration || isHidden)
121       Model = TLSModel::LocalExec;
122     else
123       Model = TLSModel::InitialExec;
124   }
125
126   // If the user specified a more specific model, use that.
127   TLSModel::Model SelectedModel = getSelectedTLSModel(Var);
128   if (SelectedModel > Model)
129     return SelectedModel;
130
131   return Model;
132 }
133
134 /// getOptLevel - Returns the optimization level: None, Less,
135 /// Default, or Aggressive.
136 CodeGenOpt::Level TargetMachine::getOptLevel() const {
137   if (!CodeGenInfo)
138     return CodeGenOpt::Default;
139   return CodeGenInfo->getOptLevel();
140 }
141
142 bool TargetMachine::getAsmVerbosityDefault() {
143   return AsmVerbosityDefault;
144 }
145
146 void TargetMachine::setAsmVerbosityDefault(bool V) {
147   AsmVerbosityDefault = V;
148 }
149
150 bool TargetMachine::getFunctionSections() {
151   return FunctionSections;
152 }
153
154 bool TargetMachine::getDataSections() {
155   return DataSections;
156 }
157
158 void TargetMachine::setFunctionSections(bool V) {
159   FunctionSections = V;
160 }
161
162 void TargetMachine::setDataSections(bool V) {
163   DataSections = V;
164 }