• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/MC/MCAsmInfo.h"
21 #include "llvm/MC/MCCodeGenInfo.h"
22 #include "llvm/Support/CommandLine.h"
23 using namespace llvm;
24 
25 //---------------------------------------------------------------------------
26 // Command-line options that tend to be useful on more than one back-end.
27 //
28 
29 namespace llvm {
30   bool HasDivModLibcall;
31   bool AsmVerbosityDefault(false);
32 }
33 
34 static cl::opt<bool>
35 DataSections("fdata-sections",
36   cl::desc("Emit data into separate sections"),
37   cl::init(false));
38 static cl::opt<bool>
39 FunctionSections("ffunction-sections",
40   cl::desc("Emit functions into separate sections"),
41   cl::init(false));
42 
43 //---------------------------------------------------------------------------
44 // TargetMachine Class
45 //
46 
TargetMachine(const Target & T,StringRef TT,StringRef CPU,StringRef FS,const TargetOptions & Options)47 TargetMachine::TargetMachine(const Target &T,
48                              StringRef TT, StringRef CPU, StringRef FS,
49                              const TargetOptions &Options)
50   : TheTarget(T), TargetTriple(TT), TargetCPU(CPU), TargetFS(FS),
51     CodeGenInfo(0), AsmInfo(0),
52     MCRelaxAll(false),
53     MCNoExecStack(false),
54     MCSaveTempLabels(false),
55     MCUseLoc(true),
56     MCUseCFI(true),
57     MCUseDwarfDirectory(false),
58     Options(Options) {
59 }
60 
~TargetMachine()61 TargetMachine::~TargetMachine() {
62   delete CodeGenInfo;
63   delete AsmInfo;
64 }
65 
66 /// \brief Reset the target options based on the function's attributes.
resetTargetOptions(const MachineFunction * MF) const67 void TargetMachine::resetTargetOptions(const MachineFunction *MF) const {
68   const Function *F = MF->getFunction();
69   TargetOptions &TO = MF->getTarget().Options;
70 
71 #define RESET_OPTION(X, Y)                                              \
72   do {                                                                  \
73     if (F->hasFnAttribute(Y))                                           \
74       TO.X =                                                            \
75         (F->getAttributes().                                            \
76            getAttribute(AttributeSet::FunctionIndex,                    \
77                         Y).getValueAsString() == "true");               \
78   } while (0)
79 
80   RESET_OPTION(NoFramePointerElim, "no-frame-pointer-elim");
81   RESET_OPTION(NoFramePointerElimNonLeaf, "no-frame-pointer-elim-non-leaf");
82   RESET_OPTION(LessPreciseFPMADOption, "less-precise-fpmad");
83   RESET_OPTION(UnsafeFPMath, "unsafe-fp-math");
84   RESET_OPTION(NoInfsFPMath, "no-infs-fp-math");
85   RESET_OPTION(NoNaNsFPMath, "no-nans-fp-math");
86   RESET_OPTION(UseSoftFloat, "use-soft-float");
87   RESET_OPTION(DisableTailCalls, "disable-tail-calls");
88 }
89 
90 /// getRelocationModel - Returns the code generation relocation model. The
91 /// choices are static, PIC, and dynamic-no-pic, and target default.
getRelocationModel() const92 Reloc::Model TargetMachine::getRelocationModel() const {
93   if (!CodeGenInfo)
94     return Reloc::Default;
95   return CodeGenInfo->getRelocationModel();
96 }
97 
98 /// getCodeModel - Returns the code model. The choices are small, kernel,
99 /// medium, large, and target default.
getCodeModel() const100 CodeModel::Model TargetMachine::getCodeModel() const {
101   if (!CodeGenInfo)
102     return CodeModel::Default;
103   return CodeGenInfo->getCodeModel();
104 }
105 
106 /// Get the IR-specified TLS model for Var.
getSelectedTLSModel(const GlobalVariable * Var)107 static TLSModel::Model getSelectedTLSModel(const GlobalVariable *Var) {
108   switch (Var->getThreadLocalMode()) {
109   case GlobalVariable::NotThreadLocal:
110     llvm_unreachable("getSelectedTLSModel for non-TLS variable");
111     break;
112   case GlobalVariable::GeneralDynamicTLSModel:
113     return TLSModel::GeneralDynamic;
114   case GlobalVariable::LocalDynamicTLSModel:
115     return TLSModel::LocalDynamic;
116   case GlobalVariable::InitialExecTLSModel:
117     return TLSModel::InitialExec;
118   case GlobalVariable::LocalExecTLSModel:
119     return TLSModel::LocalExec;
120   }
121   llvm_unreachable("invalid TLS model");
122 }
123 
getTLSModel(const GlobalValue * GV) const124 TLSModel::Model TargetMachine::getTLSModel(const GlobalValue *GV) const {
125   // If GV is an alias then use the aliasee for determining
126   // thread-localness.
127   if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
128     GV = GA->resolveAliasedGlobal(false);
129   const GlobalVariable *Var = cast<GlobalVariable>(GV);
130 
131   bool isLocal = Var->hasLocalLinkage();
132   bool isDeclaration = Var->isDeclaration();
133   bool isPIC = getRelocationModel() == Reloc::PIC_;
134   bool isPIE = Options.PositionIndependentExecutable;
135   // FIXME: what should we do for protected and internal visibility?
136   // For variables, is internal different from hidden?
137   bool isHidden = Var->hasHiddenVisibility();
138 
139   TLSModel::Model Model;
140   if (isPIC && !isPIE) {
141     if (isLocal || isHidden)
142       Model = TLSModel::LocalDynamic;
143     else
144       Model = TLSModel::GeneralDynamic;
145   } else {
146     if (!isDeclaration || isHidden)
147       Model = TLSModel::LocalExec;
148     else
149       Model = TLSModel::InitialExec;
150   }
151 
152   // If the user specified a more specific model, use that.
153   TLSModel::Model SelectedModel = getSelectedTLSModel(Var);
154   if (SelectedModel > Model)
155     return SelectedModel;
156 
157   return Model;
158 }
159 
160 /// getOptLevel - Returns the optimization level: None, Less,
161 /// Default, or Aggressive.
getOptLevel() const162 CodeGenOpt::Level TargetMachine::getOptLevel() const {
163   if (!CodeGenInfo)
164     return CodeGenOpt::Default;
165   return CodeGenInfo->getOptLevel();
166 }
167 
getAsmVerbosityDefault()168 bool TargetMachine::getAsmVerbosityDefault() {
169   return AsmVerbosityDefault;
170 }
171 
setAsmVerbosityDefault(bool V)172 void TargetMachine::setAsmVerbosityDefault(bool V) {
173   AsmVerbosityDefault = V;
174 }
175 
getFunctionSections()176 bool TargetMachine::getFunctionSections() {
177   return FunctionSections;
178 }
179 
getDataSections()180 bool TargetMachine::getDataSections() {
181   return DataSections;
182 }
183 
setFunctionSections(bool V)184 void TargetMachine::setFunctionSections(bool V) {
185   FunctionSections = V;
186 }
187 
setDataSections(bool V)188 void TargetMachine::setDataSections(bool V) {
189   DataSections = V;
190 }
191