Remove MCUseCFI from TargetMachine.
[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     MCRelaxAll(false),
59     MCNoExecStack(false),
60     MCSaveTempLabels(false),
61     MCUseDwarfDirectory(false),
62     RequireStructuredCFG(false),
63     Options(Options) {
64 }
65
66 TargetMachine::~TargetMachine() {
67   delete CodeGenInfo;
68   delete AsmInfo;
69 }
70
71 /// \brief Reset the target options based on the function's attributes.
72 void TargetMachine::resetTargetOptions(const MachineFunction *MF) const {
73   const Function *F = MF->getFunction();
74   TargetOptions &TO = MF->getTarget().Options;
75
76 #define RESET_OPTION(X, Y)                                              \
77   do {                                                                  \
78     if (F->hasFnAttribute(Y))                                           \
79       TO.X =                                                            \
80         (F->getAttributes().                                            \
81            getAttribute(AttributeSet::FunctionIndex,                    \
82                         Y).getValueAsString() == "true");               \
83   } while (0)
84
85   RESET_OPTION(NoFramePointerElim, "no-frame-pointer-elim");
86   RESET_OPTION(LessPreciseFPMADOption, "less-precise-fpmad");
87   RESET_OPTION(UnsafeFPMath, "unsafe-fp-math");
88   RESET_OPTION(NoInfsFPMath, "no-infs-fp-math");
89   RESET_OPTION(NoNaNsFPMath, "no-nans-fp-math");
90   RESET_OPTION(UseSoftFloat, "use-soft-float");
91   RESET_OPTION(DisableTailCalls, "disable-tail-calls");
92
93   TO.MCOptions.SanitizeAddress = F->hasFnAttribute(Attribute::SanitizeAddress);
94 }
95
96 /// getRelocationModel - Returns the code generation relocation model. The
97 /// choices are static, PIC, and dynamic-no-pic, and target default.
98 Reloc::Model TargetMachine::getRelocationModel() const {
99   if (!CodeGenInfo)
100     return Reloc::Default;
101   return CodeGenInfo->getRelocationModel();
102 }
103
104 /// getCodeModel - Returns the code model. The choices are small, kernel,
105 /// medium, large, and target default.
106 CodeModel::Model TargetMachine::getCodeModel() const {
107   if (!CodeGenInfo)
108     return CodeModel::Default;
109   return CodeGenInfo->getCodeModel();
110 }
111
112 /// Get the IR-specified TLS model for Var.
113 static TLSModel::Model getSelectedTLSModel(const GlobalVariable *Var) {
114   switch (Var->getThreadLocalMode()) {
115   case GlobalVariable::NotThreadLocal:
116     llvm_unreachable("getSelectedTLSModel for non-TLS variable");
117     break;
118   case GlobalVariable::GeneralDynamicTLSModel:
119     return TLSModel::GeneralDynamic;
120   case GlobalVariable::LocalDynamicTLSModel:
121     return TLSModel::LocalDynamic;
122   case GlobalVariable::InitialExecTLSModel:
123     return TLSModel::InitialExec;
124   case GlobalVariable::LocalExecTLSModel:
125     return TLSModel::LocalExec;
126   }
127   llvm_unreachable("invalid TLS model");
128 }
129
130 TLSModel::Model TargetMachine::getTLSModel(const GlobalValue *GV) const {
131   // If GV is an alias then use the aliasee for determining
132   // thread-localness.
133   if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
134     GV = GA->getAliasedGlobal();
135   const GlobalVariable *Var = cast<GlobalVariable>(GV);
136
137   bool isLocal = Var->hasLocalLinkage();
138   bool isDeclaration = Var->isDeclaration();
139   bool isPIC = getRelocationModel() == Reloc::PIC_;
140   bool isPIE = Options.PositionIndependentExecutable;
141   // FIXME: what should we do for protected and internal visibility?
142   // For variables, is internal different from hidden?
143   bool isHidden = Var->hasHiddenVisibility();
144
145   TLSModel::Model Model;
146   if (isPIC && !isPIE) {
147     if (isLocal || isHidden)
148       Model = TLSModel::LocalDynamic;
149     else
150       Model = TLSModel::GeneralDynamic;
151   } else {
152     if (!isDeclaration || isHidden)
153       Model = TLSModel::LocalExec;
154     else
155       Model = TLSModel::InitialExec;
156   }
157
158   // If the user specified a more specific model, use that.
159   TLSModel::Model SelectedModel = getSelectedTLSModel(Var);
160   if (SelectedModel > Model)
161     return SelectedModel;
162
163   return Model;
164 }
165
166 /// getOptLevel - Returns the optimization level: None, Less,
167 /// Default, or Aggressive.
168 CodeGenOpt::Level TargetMachine::getOptLevel() const {
169   if (!CodeGenInfo)
170     return CodeGenOpt::Default;
171   return CodeGenInfo->getOptLevel();
172 }
173
174 void TargetMachine::setOptLevel(CodeGenOpt::Level Level) const {
175   if (CodeGenInfo)
176     CodeGenInfo->setOptLevel(Level);
177 }
178
179 bool TargetMachine::getAsmVerbosityDefault() {
180   return AsmVerbosityDefault;
181 }
182
183 void TargetMachine::setAsmVerbosityDefault(bool V) {
184   AsmVerbosityDefault = V;
185 }
186
187 bool TargetMachine::getFunctionSections() {
188   return FunctionSections;
189 }
190
191 bool TargetMachine::getDataSections() {
192   return DataSections;
193 }
194
195 void TargetMachine::setFunctionSections(bool V) {
196   FunctionSections = V;
197 }
198
199 void TargetMachine::setDataSections(bool V) {
200   DataSections = V;
201 }
202
203 void TargetMachine::getNameWithPrefix(SmallVectorImpl<char> &Name,
204                                       const GlobalValue *GV, Mangler &Mang,
205                                       bool MayAlwaysUsePrivate) const {
206   if (MayAlwaysUsePrivate || !GV->hasPrivateLinkage()) {
207     // Simple case: If GV is not private, it is not important to find out if
208     // private labels are legal in this case or not.
209     Mang.getNameWithPrefix(Name, GV, false);
210     return;
211   }
212   SectionKind GVKind = TargetLoweringObjectFile::getKindForGlobal(GV, *this);
213   const TargetLoweringObjectFile &TLOF =
214       getTargetLowering()->getObjFileLowering();
215   const MCSection *TheSection = TLOF.SectionForGlobal(GV, GVKind, Mang, *this);
216   bool CannotUsePrivateLabel = TLOF.isSectionAtomizableBySymbols(*TheSection);
217   Mang.getNameWithPrefix(Name, GV, CannotUsePrivateLabel);
218 }
219
220 MCSymbol *TargetMachine::getSymbol(const GlobalValue *GV, Mangler &Mang) const {
221   SmallString<60> NameStr;
222   getNameWithPrefix(NameStr, GV, Mang);
223   const TargetLoweringObjectFile &TLOF =
224       getTargetLowering()->getObjFileLowering();
225   return TLOF.getContext().GetOrCreateSymbol(NameStr.str());
226 }