Use alias linkage and visibility to decide tls access mode.
[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/MCTargetOptions.h"
25 #include "llvm/MC/SectionKind.h"
26 #include "llvm/Support/CommandLine.h"
27 #include "llvm/Target/TargetLowering.h"
28 #include "llvm/Target/TargetLoweringObjectFile.h"
29 using namespace llvm;
30
31 //---------------------------------------------------------------------------
32 // TargetMachine Class
33 //
34
35 TargetMachine::TargetMachine(const Target &T,
36                              StringRef TT, StringRef CPU, StringRef FS,
37                              const TargetOptions &Options)
38   : TheTarget(T), TargetTriple(TT), TargetCPU(CPU), TargetFS(FS),
39     CodeGenInfo(nullptr), AsmInfo(nullptr),
40     RequireStructuredCFG(false),
41     Options(Options) {
42 }
43
44 TargetMachine::~TargetMachine() {
45   delete CodeGenInfo;
46   delete AsmInfo;
47 }
48
49 /// \brief Reset the target options based on the function's attributes.
50 void TargetMachine::resetTargetOptions(const MachineFunction *MF) const {
51   const Function *F = MF->getFunction();
52   TargetOptions &TO = MF->getTarget().Options;
53
54 #define RESET_OPTION(X, Y)                                              \
55   do {                                                                  \
56     if (F->hasFnAttribute(Y))                                           \
57       TO.X =                                                            \
58         (F->getAttributes().                                            \
59            getAttribute(AttributeSet::FunctionIndex,                    \
60                         Y).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   TO.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 GlobalVariable *Var) {
92   switch (Var->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   const GlobalVariable *Var = dyn_cast<GlobalVariable>(GV);
131   if (Var) {
132     // If the user specified a more specific model, use that.
133     TLSModel::Model SelectedModel = getSelectedTLSModel(Var);
134     if (SelectedModel > Model)
135       return SelectedModel;
136   }
137
138   return Model;
139 }
140
141 /// getOptLevel - Returns the optimization level: None, Less,
142 /// Default, or Aggressive.
143 CodeGenOpt::Level TargetMachine::getOptLevel() const {
144   if (!CodeGenInfo)
145     return CodeGenOpt::Default;
146   return CodeGenInfo->getOptLevel();
147 }
148
149 void TargetMachine::setOptLevel(CodeGenOpt::Level Level) const {
150   if (CodeGenInfo)
151     CodeGenInfo->setOptLevel(Level);
152 }
153
154 bool TargetMachine::getAsmVerbosityDefault() const {
155   return Options.MCOptions.AsmVerbose;
156 }
157
158 void TargetMachine::setAsmVerbosityDefault(bool V) {
159   Options.MCOptions.AsmVerbose = V;
160 }
161
162 bool TargetMachine::getFunctionSections() const {
163   return Options.FunctionSections;
164 }
165
166 bool TargetMachine::getDataSections() const {
167   return Options.DataSections;
168 }
169
170 void TargetMachine::setFunctionSections(bool V) {
171   Options.FunctionSections = V;
172 }
173
174 void TargetMachine::setDataSections(bool V) {
175   Options.DataSections = V;
176 }
177
178 void TargetMachine::getNameWithPrefix(SmallVectorImpl<char> &Name,
179                                       const GlobalValue *GV, Mangler &Mang,
180                                       bool MayAlwaysUsePrivate) const {
181   if (MayAlwaysUsePrivate || !GV->hasPrivateLinkage()) {
182     // Simple case: If GV is not private, it is not important to find out if
183     // private labels are legal in this case or not.
184     Mang.getNameWithPrefix(Name, GV, false);
185     return;
186   }
187   SectionKind GVKind = TargetLoweringObjectFile::getKindForGlobal(GV, *this);
188   const TargetLoweringObjectFile &TLOF =
189       getTargetLowering()->getObjFileLowering();
190   const MCSection *TheSection = TLOF.SectionForGlobal(GV, GVKind, Mang, *this);
191   bool CannotUsePrivateLabel = TLOF.isSectionAtomizableBySymbols(*TheSection);
192   Mang.getNameWithPrefix(Name, GV, CannotUsePrivateLabel);
193 }
194
195 MCSymbol *TargetMachine::getSymbol(const GlobalValue *GV, Mangler &Mang) const {
196   SmallString<60> NameStr;
197   getNameWithPrefix(NameStr, GV, Mang);
198   const TargetLoweringObjectFile &TLOF =
199       getTargetLowering()->getObjFileLowering();
200   return TLOF.getContext().GetOrCreateSymbol(NameStr.str());
201 }