d60a70846a768a9747bdfbc4c4db958962d97ad6
[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/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/MCTargetOptions.h"
25 #include "llvm/MC/SectionKind.h"
26 #include "llvm/Support/CommandLine.h"
27 #include "llvm/Target/TargetLowering.h"
28 #include "llvm/Target/TargetLoweringObjectFile.h"
29 using namespace llvm;
30
31 //---------------------------------------------------------------------------
32 // Command-line options that tend to be useful on more than one back-end.
33 //
34
35 namespace llvm {
36   bool HasDivModLibcall;
37   bool AsmVerbosityDefault(false);
38 }
39
40 static cl::opt<bool>
41 DataSections("fdata-sections",
42   cl::desc("Emit data into separate sections"),
43   cl::init(false));
44 static cl::opt<bool>
45 FunctionSections("ffunction-sections",
46   cl::desc("Emit functions into separate sections"),
47   cl::init(false));
48
49 //---------------------------------------------------------------------------
50 // TargetMachine Class
51 //
52
53 TargetMachine::TargetMachine(const Target &T,
54                              StringRef TT, StringRef CPU, StringRef FS,
55                              const TargetOptions &Options)
56   : TheTarget(T), TargetTriple(TT), TargetCPU(CPU), TargetFS(FS),
57     CodeGenInfo(nullptr), AsmInfo(nullptr),
58     RequireStructuredCFG(false),
59     Options(Options) {
60 }
61
62 TargetMachine::~TargetMachine() {
63   delete CodeGenInfo;
64   delete AsmInfo;
65 }
66
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;
71
72 #define RESET_OPTION(X, Y)                                              \
73   do {                                                                  \
74     if (F->hasFnAttribute(Y))                                           \
75       TO.X =                                                            \
76         (F->getAttributes().                                            \
77            getAttribute(AttributeSet::FunctionIndex,                    \
78                         Y).getValueAsString() == "true");               \
79   } while (0)
80
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");
88
89   TO.MCOptions.SanitizeAddress = F->hasFnAttribute(Attribute::SanitizeAddress);
90 }
91
92 /// getRelocationModel - Returns the code generation relocation model. The
93 /// choices are static, PIC, and dynamic-no-pic, and target default.
94 Reloc::Model TargetMachine::getRelocationModel() const {
95   if (!CodeGenInfo)
96     return Reloc::Default;
97   return CodeGenInfo->getRelocationModel();
98 }
99
100 /// getCodeModel - Returns the code model. The choices are small, kernel,
101 /// medium, large, and target default.
102 CodeModel::Model TargetMachine::getCodeModel() const {
103   if (!CodeGenInfo)
104     return CodeModel::Default;
105   return CodeGenInfo->getCodeModel();
106 }
107
108 /// Get the IR-specified TLS model for Var.
109 static TLSModel::Model getSelectedTLSModel(const GlobalVariable *Var) {
110   switch (Var->getThreadLocalMode()) {
111   case GlobalVariable::NotThreadLocal:
112     llvm_unreachable("getSelectedTLSModel for non-TLS variable");
113     break;
114   case GlobalVariable::GeneralDynamicTLSModel:
115     return TLSModel::GeneralDynamic;
116   case GlobalVariable::LocalDynamicTLSModel:
117     return TLSModel::LocalDynamic;
118   case GlobalVariable::InitialExecTLSModel:
119     return TLSModel::InitialExec;
120   case GlobalVariable::LocalExecTLSModel:
121     return TLSModel::LocalExec;
122   }
123   llvm_unreachable("invalid TLS model");
124 }
125
126 TLSModel::Model TargetMachine::getTLSModel(const GlobalValue *GV) const {
127   // If GV is an alias then use the aliasee for determining
128   // thread-localness.
129   if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
130     GV = GA->getAliasedGlobal();
131   const GlobalVariable *Var = cast<GlobalVariable>(GV);
132
133   bool isLocal = Var->hasLocalLinkage();
134   bool isDeclaration = Var->isDeclaration();
135   bool isPIC = getRelocationModel() == Reloc::PIC_;
136   bool isPIE = Options.PositionIndependentExecutable;
137   // FIXME: what should we do for protected and internal visibility?
138   // For variables, is internal different from hidden?
139   bool isHidden = Var->hasHiddenVisibility();
140
141   TLSModel::Model Model;
142   if (isPIC && !isPIE) {
143     if (isLocal || isHidden)
144       Model = TLSModel::LocalDynamic;
145     else
146       Model = TLSModel::GeneralDynamic;
147   } else {
148     if (!isDeclaration || isHidden)
149       Model = TLSModel::LocalExec;
150     else
151       Model = TLSModel::InitialExec;
152   }
153
154   // If the user specified a more specific model, use that.
155   TLSModel::Model SelectedModel = getSelectedTLSModel(Var);
156   if (SelectedModel > Model)
157     return SelectedModel;
158
159   return Model;
160 }
161
162 /// getOptLevel - Returns the optimization level: None, Less,
163 /// Default, or Aggressive.
164 CodeGenOpt::Level TargetMachine::getOptLevel() const {
165   if (!CodeGenInfo)
166     return CodeGenOpt::Default;
167   return CodeGenInfo->getOptLevel();
168 }
169
170 void TargetMachine::setOptLevel(CodeGenOpt::Level Level) const {
171   if (CodeGenInfo)
172     CodeGenInfo->setOptLevel(Level);
173 }
174
175 bool TargetMachine::getAsmVerbosityDefault() {
176   return AsmVerbosityDefault;
177 }
178
179 void TargetMachine::setAsmVerbosityDefault(bool V) {
180   AsmVerbosityDefault = V;
181 }
182
183 bool TargetMachine::getFunctionSections() {
184   return FunctionSections;
185 }
186
187 bool TargetMachine::getDataSections() {
188   return DataSections;
189 }
190
191 void TargetMachine::setFunctionSections(bool V) {
192   FunctionSections = V;
193 }
194
195 void TargetMachine::setDataSections(bool V) {
196   DataSections = V;
197 }
198
199 void TargetMachine::getNameWithPrefix(SmallVectorImpl<char> &Name,
200                                       const GlobalValue *GV, Mangler &Mang,
201                                       bool MayAlwaysUsePrivate) const {
202   if (MayAlwaysUsePrivate || !GV->hasPrivateLinkage()) {
203     // Simple case: If GV is not private, it is not important to find out if
204     // private labels are legal in this case or not.
205     Mang.getNameWithPrefix(Name, GV, false);
206     return;
207   }
208   SectionKind GVKind = TargetLoweringObjectFile::getKindForGlobal(GV, *this);
209   const TargetLoweringObjectFile &TLOF =
210       getTargetLowering()->getObjFileLowering();
211   const MCSection *TheSection = TLOF.SectionForGlobal(GV, GVKind, Mang, *this);
212   bool CannotUsePrivateLabel = TLOF.isSectionAtomizableBySymbols(*TheSection);
213   Mang.getNameWithPrefix(Name, GV, CannotUsePrivateLabel);
214 }
215
216 MCSymbol *TargetMachine::getSymbol(const GlobalValue *GV, Mangler &Mang) const {
217   SmallString<60> NameStr;
218   getNameWithPrefix(NameStr, GV, Mang);
219   const TargetLoweringObjectFile &TLOF =
220       getTargetLowering()->getObjFileLowering();
221   return TLOF.getContext().GetOrCreateSymbol(NameStr.str());
222 }