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