Handle aliases to tls variables in all architectures, not just x86.
[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/GlobalAlias.h"
15 #include "llvm/GlobalValue.h"
16 #include "llvm/GlobalVariable.h"
17 #include "llvm/MC/MCAsmInfo.h"
18 #include "llvm/MC/MCCodeGenInfo.h"
19 #include "llvm/Target/TargetMachine.h"
20 #include "llvm/Support/CommandLine.h"
21 using namespace llvm;
22
23 //---------------------------------------------------------------------------
24 // Command-line options that tend to be useful on more than one back-end.
25 //
26
27 namespace llvm {
28   bool HasDivModLibcall;
29   bool AsmVerbosityDefault(false);
30 }
31
32 static cl::opt<bool>
33 DataSections("fdata-sections",
34   cl::desc("Emit data into separate sections"),
35   cl::init(false));
36 static cl::opt<bool>
37 FunctionSections("ffunction-sections",
38   cl::desc("Emit functions into separate sections"),
39   cl::init(false));
40
41 //---------------------------------------------------------------------------
42 // TargetMachine Class
43 //
44
45 TargetMachine::TargetMachine(const Target &T,
46                              StringRef TT, StringRef CPU, StringRef FS,
47                              const TargetOptions &Options)
48   : TheTarget(T), TargetTriple(TT), TargetCPU(CPU), TargetFS(FS),
49     CodeGenInfo(0), AsmInfo(0),
50     MCRelaxAll(false),
51     MCNoExecStack(false),
52     MCSaveTempLabels(false),
53     MCUseLoc(true),
54     MCUseCFI(true),
55     MCUseDwarfDirectory(false),
56     Options(Options) {
57 }
58
59 TargetMachine::~TargetMachine() {
60   delete CodeGenInfo;
61   delete AsmInfo;
62 }
63
64 /// getRelocationModel - Returns the code generation relocation model. The
65 /// choices are static, PIC, and dynamic-no-pic, and target default.
66 Reloc::Model TargetMachine::getRelocationModel() const {
67   if (!CodeGenInfo)
68     return Reloc::Default;
69   return CodeGenInfo->getRelocationModel();
70 }
71
72 /// getCodeModel - Returns the code model. The choices are small, kernel,
73 /// medium, large, and target default.
74 CodeModel::Model TargetMachine::getCodeModel() const {
75   if (!CodeGenInfo)
76     return CodeModel::Default;
77   return CodeGenInfo->getCodeModel();
78 }
79
80 TLSModel::Model TargetMachine::getTLSModel(const GlobalValue *GV) const {
81   // If GV is an alias then use the aliasee for determining
82   // thread-localness.
83   if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
84     GV = GA->resolveAliasedGlobal(false);
85   const GlobalVariable *Var = cast<GlobalVariable>(GV);
86
87   bool isLocal = Var->hasLocalLinkage();
88   bool isDeclaration = Var->isDeclaration();
89   // FIXME: what should we do for protected and internal visibility?
90   // For variables, is internal different from hidden?
91   bool isHidden = Var->hasHiddenVisibility();
92
93   if (getRelocationModel() == Reloc::PIC_ &&
94       !Options.PositionIndependentExecutable) {
95     if (isLocal || isHidden)
96       return TLSModel::LocalDynamic;
97     else
98       return TLSModel::GeneralDynamic;
99   } else {
100     if (!isDeclaration || isHidden)
101       return TLSModel::LocalExec;
102     else
103       return TLSModel::InitialExec;
104   }
105 }
106
107 /// getOptLevel - Returns the optimization level: None, Less,
108 /// Default, or Aggressive.
109 CodeGenOpt::Level TargetMachine::getOptLevel() const {
110   if (!CodeGenInfo)
111     return CodeGenOpt::Default;
112   return CodeGenInfo->getOptLevel();
113 }
114
115 bool TargetMachine::getAsmVerbosityDefault() {
116   return AsmVerbosityDefault;
117 }
118
119 void TargetMachine::setAsmVerbosityDefault(bool V) {
120   AsmVerbosityDefault = V;
121 }
122
123 bool TargetMachine::getFunctionSections() {
124   return FunctionSections;
125 }
126
127 bool TargetMachine::getDataSections() {
128   return DataSections;
129 }
130
131 void TargetMachine::setFunctionSections(bool V) {
132   FunctionSections = V;
133 }
134
135 void TargetMachine::setDataSections(bool V) {
136   DataSections = V;
137 }
138