LLVM API Documentation

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
TargetMachine.cpp
Go to the documentation of this file.
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 
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"
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 {
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 
47 TargetMachine::TargetMachine(const Target &T,
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 
62  delete CodeGenInfo;
63  delete AsmInfo;
64 }
65 
66 /// \brief Reset the target options based on the function's attributes.
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(LessPreciseFPMADOption, "less-precise-fpmad");
82  RESET_OPTION(UnsafeFPMath, "unsafe-fp-math");
83  RESET_OPTION(NoInfsFPMath, "no-infs-fp-math");
84  RESET_OPTION(NoNaNsFPMath, "no-nans-fp-math");
85  RESET_OPTION(UseSoftFloat, "use-soft-float");
86  RESET_OPTION(DisableTailCalls, "disable-tail-calls");
87 }
88 
89 /// getRelocationModel - Returns the code generation relocation model. The
90 /// choices are static, PIC, and dynamic-no-pic, and target default.
92  if (!CodeGenInfo)
93  return Reloc::Default;
95 }
96 
97 /// getCodeModel - Returns the code model. The choices are small, kernel,
98 /// medium, large, and target default.
100  if (!CodeGenInfo)
101  return CodeModel::Default;
102  return CodeGenInfo->getCodeModel();
103 }
104 
105 /// Get the IR-specified TLS model for Var.
107  switch (Var->getThreadLocalMode()) {
109  llvm_unreachable("getSelectedTLSModel for non-TLS variable");
110  break;
114  return TLSModel::LocalDynamic;
116  return TLSModel::InitialExec;
118  return TLSModel::LocalExec;
119  }
120  llvm_unreachable("invalid TLS model");
121 }
122 
124  // If GV is an alias then use the aliasee for determining
125  // thread-localness.
126  if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
127  GV = GA->resolveAliasedGlobal(false);
128  const GlobalVariable *Var = cast<GlobalVariable>(GV);
129 
130  bool isLocal = Var->hasLocalLinkage();
131  bool isDeclaration = Var->isDeclaration();
132  bool isPIC = getRelocationModel() == Reloc::PIC_;
134  // FIXME: what should we do for protected and internal visibility?
135  // For variables, is internal different from hidden?
136  bool isHidden = Var->hasHiddenVisibility();
137 
139  if (isPIC && !isPIE) {
140  if (isLocal || isHidden)
141  Model = TLSModel::LocalDynamic;
142  else
143  Model = TLSModel::GeneralDynamic;
144  } else {
145  if (!isDeclaration || isHidden)
146  Model = TLSModel::LocalExec;
147  else
148  Model = TLSModel::InitialExec;
149  }
150 
151  // If the user specified a more specific model, use that.
152  TLSModel::Model SelectedModel = getSelectedTLSModel(Var);
153  if (SelectedModel > Model)
154  return SelectedModel;
155 
156  return Model;
157 }
158 
159 /// getOptLevel - Returns the optimization level: None, Less,
160 /// Default, or Aggressive.
162  if (!CodeGenInfo)
163  return CodeGenOpt::Default;
164  return CodeGenInfo->getOptLevel();
165 }
166 
168  if (CodeGenInfo)
169  CodeGenInfo->setOptLevel(Level);
170 }
171 
173  return AsmVerbosityDefault;
174 }
175 
178 }
179 
181  return FunctionSections;
182 }
183 
185  return DataSections;
186 }
187 
189  FunctionSections = V;
190 }
191 
193  DataSections = V;
194 }
CodeModel::Model getCodeModel() const
Definition: MCCodeGenInfo.h:42
ThreadLocalMode getThreadLocalMode() const
Reloc::Model getRelocationModel() const
static cl::opt< bool > DataSections("fdata-sections", cl::desc("Emit data into separate sections"), cl::init(false))
F(f)
const Function * getFunction() const
void resetTargetOptions(const MachineFunction *MF) const
Reset the target options based on the function's attributes.
void setOptLevel(CodeGenOpt::Level Level) const
Overrides the optimization level.
#define llvm_unreachable(msg)
static bool isDeclaration(const GlobalValue &V)
isDeclaration - Return 'true' if the global value is a declaration.
Definition: LTOModule.cpp:739
#define RESET_OPTION(X, Y)
bool AsmVerbosityDefault(false)
#define false
Definition: ConvertUTF.c:64
const MCAsmInfo * AsmInfo
static TLSModel::Model getSelectedTLSModel(const GlobalVariable *Var)
Get the IR-specified TLS model for Var.
Reloc::Model getRelocationModel() const
Definition: MCCodeGenInfo.h:40
CodeGenOpt::Level getOptLevel() const
#define true
Definition: ConvertUTF.c:65
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:314
bool HasDivModLibcall
CodeModel::Model getCodeModel() const
bool hasHiddenVisibility() const
Definition: GlobalValue.h:89
static bool getAsmVerbosityDefault()
static bool getDataSections()
static bool getFunctionSections()
static void setAsmVerbosityDefault(bool)
CodeGenOpt::Level getOptLevel() const
Definition: MCCodeGenInfo.h:44
static cl::opt< bool > FunctionSections("ffunction-sections", cl::desc("Emit functions into separate sections"), cl::init(false))
MCCodeGenInfo * CodeGenInfo
cl::opt< bool > DisableTailCalls("disable-tail-calls", cl::desc("Never emit tail calls"), cl::init(false))
void setOptLevel(CodeGenOpt::Level Level)
Definition: MCCodeGenInfo.h:47
bool isDeclaration() const
Definition: Globals.cpp:66
unsigned PositionIndependentExecutable
const TargetMachine & getTarget() const
bool hasLocalLinkage() const
Definition: GlobalValue.h:211
TLSModel::Model getTLSModel(const GlobalValue *GV) const
static void setDataSections(bool)
setDataSections - Set if the data are emit into separate sections.
static void setFunctionSections(bool)