LLVM  3.7.0
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 
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/MCInstrInfo.h"
26 #include "llvm/MC/MCSectionMachO.h"
28 #include "llvm/MC/SectionKind.h"
34 using namespace llvm;
35 
36 //---------------------------------------------------------------------------
37 // TargetMachine Class
38 //
39 
40 TargetMachine::TargetMachine(const Target &T, StringRef DataLayoutString,
41  const Triple &TT, StringRef CPU, StringRef FS,
42  const TargetOptions &Options)
43  : TheTarget(T), DL(DataLayoutString), TargetTriple(TT), TargetCPU(CPU),
44  TargetFS(FS), CodeGenInfo(nullptr), AsmInfo(nullptr), MRI(nullptr),
45  MII(nullptr), STI(nullptr), RequireStructuredCFG(false),
46  Options(Options) {}
47 
49  delete CodeGenInfo;
50  delete AsmInfo;
51  delete MRI;
52  delete MII;
53  delete STI;
54 }
55 
56 /// \brief Reset the target options based on the function's attributes.
57 // FIXME: This function needs to go away for a number of reasons:
58 // a) global state on the TargetMachine is terrible in general,
59 // b) there's no default state here to keep,
60 // c) these target options should be passed only on the function
61 // and not on the TargetMachine (via TargetOptions) at all.
63 #define RESET_OPTION(X, Y) \
64  do { \
65  if (F.hasFnAttribute(Y)) \
66  Options.X = (F.getFnAttribute(Y).getValueAsString() == "true"); \
67  } while (0)
68 
69  RESET_OPTION(LessPreciseFPMADOption, "less-precise-fpmad");
70  RESET_OPTION(UnsafeFPMath, "unsafe-fp-math");
71  RESET_OPTION(NoInfsFPMath, "no-infs-fp-math");
72  RESET_OPTION(NoNaNsFPMath, "no-nans-fp-math");
73 }
74 
75 /// getRelocationModel - Returns the code generation relocation model. The
76 /// choices are static, PIC, and dynamic-no-pic, and target default.
78  if (!CodeGenInfo)
79  return Reloc::Default;
81 }
82 
83 /// getCodeModel - Returns the code model. The choices are small, kernel,
84 /// medium, large, and target default.
86  if (!CodeGenInfo)
87  return CodeModel::Default;
88  return CodeGenInfo->getCodeModel();
89 }
90 
91 /// Get the IR-specified TLS model for Var.
93  switch (GV->getThreadLocalMode()) {
95  llvm_unreachable("getSelectedTLSModel for non-TLS variable");
96  break;
100  return TLSModel::LocalDynamic;
102  return TLSModel::InitialExec;
104  return TLSModel::LocalExec;
105  }
106  llvm_unreachable("invalid TLS model");
107 }
108 
110  bool isLocal = GV->hasLocalLinkage();
111  bool isDeclaration = GV->isDeclaration();
112  bool isPIC = getRelocationModel() == Reloc::PIC_;
114  // FIXME: what should we do for protected and internal visibility?
115  // For variables, is internal different from hidden?
116  bool isHidden = GV->hasHiddenVisibility();
117 
119  if (isPIC && !isPIE) {
120  if (isLocal || isHidden)
121  Model = TLSModel::LocalDynamic;
122  else
123  Model = TLSModel::GeneralDynamic;
124  } else {
125  if (!isDeclaration || isHidden)
126  Model = TLSModel::LocalExec;
127  else
128  Model = TLSModel::InitialExec;
129  }
130 
131  // If the user specified a more specific model, use that.
132  TLSModel::Model SelectedModel = getSelectedTLSModel(GV);
133  if (SelectedModel > Model)
134  return SelectedModel;
135 
136  return Model;
137 }
138 
139 /// getOptLevel - Returns the optimization level: None, Less,
140 /// Default, or Aggressive.
142  if (!CodeGenInfo)
143  return CodeGenOpt::Default;
144  return CodeGenInfo->getOptLevel();
145 }
146 
148  if (CodeGenInfo)
149  CodeGenInfo->setOptLevel(Level);
150 }
151 
153  return TargetIRAnalysis([this](Function &F) {
155  });
156 }
157 
158 static bool canUsePrivateLabel(const MCAsmInfo &AsmInfo,
159  const MCSection &Section) {
160  if (!AsmInfo.isSectionAtomizableBySymbols(Section))
161  return true;
162 
163  // If it is not dead stripped, it is safe to use private labels.
164  const MCSectionMachO &SMO = cast<MCSectionMachO>(Section);
166  return true;
167 
168  return false;
169 }
170 
172  const GlobalValue *GV, Mangler &Mang,
173  bool MayAlwaysUsePrivate) const {
174  if (MayAlwaysUsePrivate || !GV->hasPrivateLinkage()) {
175  // Simple case: If GV is not private, it is not important to find out if
176  // private labels are legal in this case or not.
177  Mang.getNameWithPrefix(Name, GV, false);
178  return;
179  }
182  const MCSection *TheSection = TLOF->SectionForGlobal(GV, GVKind, Mang, *this);
183  bool CannotUsePrivateLabel = !canUsePrivateLabel(*AsmInfo, *TheSection);
184  TLOF->getNameWithPrefix(Name, GV, CannotUsePrivateLabel, Mang, *this);
185 }
186 
188  SmallString<128> NameStr;
189  getNameWithPrefix(NameStr, GV, Mang);
191  return TLOF->getContext().getOrCreateSymbol(NameStr);
192 }
Instances of this class represent a uniqued identifier for a section in the current translation unit...
Definition: MCSection.h:48
static SectionKind getKindForGlobal(const GlobalValue *GV, const TargetMachine &TM)
Classify the specified global variable into a set of target independent categories embodied in Sectio...
MCSectionMachO - This represents a section on a Mach-O system (used by Mac OS X). ...
CodeModel::Model getCodeModel() const
Definition: MCCodeGenInfo.h:42
void getNameWithPrefix(SmallVectorImpl< char > &Name, const GlobalValue *GV, Mangler &Mang, bool MayAlwaysUsePrivate=false) const
Reloc::Model getRelocationModel() const
Returns the code generation relocation model.
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:39
static TLSModel::Model getSelectedTLSModel(const GlobalValue *GV)
Get the IR-specified TLS model for Var.
Analysis pass providing the TargetTransformInfo.
virtual void getNameWithPrefix(SmallVectorImpl< char > &OutName, const GlobalValue *GV, bool CannotUsePrivateLabel, Mangler &Mang, const TargetMachine &TM) const
virtual bool isSectionAtomizableBySymbols(const MCSection &Section) const
True if the section is atomized using the symbols in it.
Definition: MCAsmInfo.cpp:116
F(f)
void setOptLevel(CodeGenOpt::Level Level) const
Overrides the optimization level.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:98
virtual TargetLoweringObjectFile * getObjFileLowering() const
#define RESET_OPTION(X, Y)
#define false
Definition: ConvertUTF.c:65
bool hasPrivateLinkage() const
Definition: GlobalValue.h:279
const MCAsmInfo * AsmInfo
Contains target specific asm information.
const MCSubtargetInfo * STI
static bool canUsePrivateLabel(const MCAsmInfo &AsmInfo, const MCSection &Section)
Reloc::Model getRelocationModel() const
Definition: MCCodeGenInfo.h:40
CodeGenOpt::Level getOptLevel() const
Returns the optimization level: None, Less, Default, or Aggressive.
This class is intended to be used as a base class for asm properties and features specific to the tar...
Definition: MCAsmInfo.h:58
CodeModel::Model getCodeModel() const
Returns the code model.
MCSection * SectionForGlobal(const GlobalValue *GV, SectionKind Kind, Mangler &Mang, const TargetMachine &TM) const
This method computes the appropriate section to emit the specified global variable or function defini...
bool hasHiddenVisibility() const
Definition: GlobalValue.h:141
bool hasAttribute(unsigned Value) const
virtual TargetIRAnalysis getTargetIRAnalysis()
Get a TargetIRAnalysis appropriate for the target.
SectionKind - This is a simple POD value that classifies the properties of a section.
Definition: SectionKind.h:28
const MCInstrInfo * MII
MCSymbol * getSymbol(const GlobalValue *GV, Mangler &Mang) const
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
CodeGenOpt::Level getOptLevel() const
Definition: MCCodeGenInfo.h:44
S_ATTR_NO_DEAD_STRIP - No dead stripping.
This pass provides access to the codegen interfaces that are needed for IR-level transformations.
MCCodeGenInfo * CodeGenInfo
Low level target information such as relocation model.
Target - Wrapper for Target specific information.
const MCRegisterInfo * MRI
ThreadLocalMode getThreadLocalMode() const
Definition: GlobalValue.h:160
const DataLayout & getDataLayout() const
Get the data layout for the module's target platform.
Definition: Module.cpp:372
void setOptLevel(CodeGenOpt::Level Level)
Definition: MCCodeGenInfo.h:47
MCSymbol * getOrCreateSymbol(const Twine &Name)
Lookup the symbol inside with the specified Name.
Definition: MCContext.cpp:111
bool isDeclaration() const
Return true if the primary definition of this global value is outside of the current translation unit...
Definition: Globals.cpp:128
void getNameWithPrefix(raw_ostream &OS, const GlobalValue *GV, bool CannotUsePrivateLabel) const
Print the appropriate prefix and the specified global variable's name.
Definition: Mangler.cpp:108
unsigned PositionIndependentExecutable
PositionIndependentExecutable - This flag indicates whether the code will eventually be linked into a...
void resetTargetOptions(const Function &F) const
Reset the target options based on the function's attributes.
bool hasLocalLinkage() const
Definition: GlobalValue.h:280
TLSModel::Model getTLSModel(const GlobalValue *GV) const
Returns the TLS model which should be used for the given global variable.
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:365
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:40
This pass exposes codegen information to IR-level passes.
This file describes how to lower LLVM code to machine code.