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