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