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