Recommit r224935 with a fix for the ObjC++/AArch64 bug that that revision
[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/MCSectionMachO.h"
25 #include "llvm/MC/MCTargetOptions.h"
26 #include "llvm/MC/SectionKind.h"
27 #include "llvm/Support/CommandLine.h"
28 #include "llvm/Target/TargetLowering.h"
29 #include "llvm/Target/TargetLoweringObjectFile.h"
30 #include "llvm/Target/TargetSubtargetInfo.h"
31 using namespace llvm;
32
33 //---------------------------------------------------------------------------
34 // TargetMachine Class
35 //
36
37 TargetMachine::TargetMachine(const Target &T,
38                              StringRef TT, StringRef CPU, StringRef FS,
39                              const TargetOptions &Options)
40   : TheTarget(T), TargetTriple(TT), TargetCPU(CPU), TargetFS(FS),
41     CodeGenInfo(nullptr), AsmInfo(nullptr),
42     RequireStructuredCFG(false),
43     Options(Options) {
44 }
45
46 TargetMachine::~TargetMachine() {
47   delete CodeGenInfo;
48   delete AsmInfo;
49 }
50
51 /// \brief Reset the target options based on the function's attributes.
52 void TargetMachine::resetTargetOptions(const Function &F) const {
53 #define RESET_OPTION(X, Y)                                                     \
54   do {                                                                         \
55     if (F.hasFnAttribute(Y))                                                  \
56       Options.X = (F.getAttributes()                                          \
57                        .getAttribute(AttributeSet::FunctionIndex, Y)           \
58                        .getValueAsString() == "true");                         \
59   } while (0)
60
61   RESET_OPTION(NoFramePointerElim, "no-frame-pointer-elim");
62   RESET_OPTION(LessPreciseFPMADOption, "less-precise-fpmad");
63   RESET_OPTION(UnsafeFPMath, "unsafe-fp-math");
64   RESET_OPTION(NoInfsFPMath, "no-infs-fp-math");
65   RESET_OPTION(NoNaNsFPMath, "no-nans-fp-math");
66   RESET_OPTION(UseSoftFloat, "use-soft-float");
67   RESET_OPTION(DisableTailCalls, "disable-tail-calls");
68
69   Options.MCOptions.SanitizeAddress = F.hasFnAttribute(Attribute::SanitizeAddress);
70 }
71
72 /// getRelocationModel - Returns the code generation relocation model. The
73 /// choices are static, PIC, and dynamic-no-pic, and target default.
74 Reloc::Model TargetMachine::getRelocationModel() const {
75   if (!CodeGenInfo)
76     return Reloc::Default;
77   return CodeGenInfo->getRelocationModel();
78 }
79
80 /// getCodeModel - Returns the code model. The choices are small, kernel,
81 /// medium, large, and target default.
82 CodeModel::Model TargetMachine::getCodeModel() const {
83   if (!CodeGenInfo)
84     return CodeModel::Default;
85   return CodeGenInfo->getCodeModel();
86 }
87
88 /// Get the IR-specified TLS model for Var.
89 static TLSModel::Model getSelectedTLSModel(const GlobalValue *GV) {
90   switch (GV->getThreadLocalMode()) {
91   case GlobalVariable::NotThreadLocal:
92     llvm_unreachable("getSelectedTLSModel for non-TLS variable");
93     break;
94   case GlobalVariable::GeneralDynamicTLSModel:
95     return TLSModel::GeneralDynamic;
96   case GlobalVariable::LocalDynamicTLSModel:
97     return TLSModel::LocalDynamic;
98   case GlobalVariable::InitialExecTLSModel:
99     return TLSModel::InitialExec;
100   case GlobalVariable::LocalExecTLSModel:
101     return TLSModel::LocalExec;
102   }
103   llvm_unreachable("invalid TLS model");
104 }
105
106 TLSModel::Model TargetMachine::getTLSModel(const GlobalValue *GV) const {
107   bool isLocal = GV->hasLocalLinkage();
108   bool isDeclaration = GV->isDeclaration();
109   bool isPIC = getRelocationModel() == Reloc::PIC_;
110   bool isPIE = Options.PositionIndependentExecutable;
111   // FIXME: what should we do for protected and internal visibility?
112   // For variables, is internal different from hidden?
113   bool isHidden = GV->hasHiddenVisibility();
114
115   TLSModel::Model Model;
116   if (isPIC && !isPIE) {
117     if (isLocal || isHidden)
118       Model = TLSModel::LocalDynamic;
119     else
120       Model = TLSModel::GeneralDynamic;
121   } else {
122     if (!isDeclaration || isHidden)
123       Model = TLSModel::LocalExec;
124     else
125       Model = TLSModel::InitialExec;
126   }
127
128   // If the user specified a more specific model, use that.
129   TLSModel::Model SelectedModel = getSelectedTLSModel(GV);
130   if (SelectedModel > Model)
131     return SelectedModel;
132
133   return Model;
134 }
135
136 /// getOptLevel - Returns the optimization level: None, Less,
137 /// Default, or Aggressive.
138 CodeGenOpt::Level TargetMachine::getOptLevel() const {
139   if (!CodeGenInfo)
140     return CodeGenOpt::Default;
141   return CodeGenInfo->getOptLevel();
142 }
143
144 void TargetMachine::setOptLevel(CodeGenOpt::Level Level) const {
145   if (CodeGenInfo)
146     CodeGenInfo->setOptLevel(Level);
147 }
148
149 bool TargetMachine::getAsmVerbosityDefault() const {
150   return Options.MCOptions.AsmVerbose;
151 }
152
153 void TargetMachine::setAsmVerbosityDefault(bool V) {
154   Options.MCOptions.AsmVerbose = V;
155 }
156
157 bool TargetMachine::getFunctionSections() const {
158   return Options.FunctionSections;
159 }
160
161 bool TargetMachine::getDataSections() const {
162   return Options.DataSections;
163 }
164
165 void TargetMachine::setFunctionSections(bool V) {
166   Options.FunctionSections = V;
167 }
168
169 void TargetMachine::setDataSections(bool V) {
170   Options.DataSections = V;
171 }
172
173 static bool canUsePrivateLabel(const MCAsmInfo &AsmInfo,
174                                const MCSection &Section) {
175   if (!AsmInfo.isSectionAtomizableBySymbols(Section))
176     return true;
177
178   // If it is not dead stripped, it is safe to use private labels.
179   const MCSectionMachO &SMO = cast<MCSectionMachO>(Section);
180   if (SMO.hasAttribute(MachO::S_ATTR_NO_DEAD_STRIP))
181     return true;
182
183   return false;
184 }
185
186 void TargetMachine::getNameWithPrefix(SmallVectorImpl<char> &Name,
187                                       const GlobalValue *GV, Mangler &Mang,
188                                       bool MayAlwaysUsePrivate) const {
189   if (MayAlwaysUsePrivate || !GV->hasPrivateLinkage()) {
190     // Simple case: If GV is not private, it is not important to find out if
191     // private labels are legal in this case or not.
192     Mang.getNameWithPrefix(Name, GV, false);
193     return;
194   }
195   SectionKind GVKind = TargetLoweringObjectFile::getKindForGlobal(GV, *this);
196   const TargetLoweringObjectFile &TLOF =
197       getSubtargetImpl()->getTargetLowering()->getObjFileLowering();
198   const MCSection *TheSection = TLOF.SectionForGlobal(GV, GVKind, Mang, *this);
199   bool CannotUsePrivateLabel = !canUsePrivateLabel(*AsmInfo, *TheSection);
200   Mang.getNameWithPrefix(Name, GV, CannotUsePrivateLabel);
201 }
202
203 MCSymbol *TargetMachine::getSymbol(const GlobalValue *GV, Mangler &Mang) const {
204   SmallString<60> NameStr;
205   getNameWithPrefix(NameStr, GV, Mang);
206   const TargetLoweringObjectFile &TLOF =
207       getSubtargetImpl()->getTargetLowering()->getObjFileLowering();
208   return TLOF.getContext().GetOrCreateSymbol(NameStr.str());
209 }