TargetMachine.cpp revision cd81d94322a39503e4a3e87b6ee03d4fcb3465fb
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"
29using namespace llvm;
30
31//---------------------------------------------------------------------------
32// TargetMachine Class
33//
34
35TargetMachine::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
44TargetMachine::~TargetMachine() {
45  delete CodeGenInfo;
46  delete AsmInfo;
47}
48
49/// \brief Reset the target options based on the function's attributes.
50void 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.
76Reloc::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.
84CodeModel::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.
91static TLSModel::Model getSelectedTLSModel(const GlobalValue *GV) {
92  switch (GV->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
108TLSModel::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  // If the user specified a more specific model, use that.
131  TLSModel::Model SelectedModel = getSelectedTLSModel(GV);
132  if (SelectedModel > Model)
133    return SelectedModel;
134
135  return Model;
136}
137
138/// getOptLevel - Returns the optimization level: None, Less,
139/// Default, or Aggressive.
140CodeGenOpt::Level TargetMachine::getOptLevel() const {
141  if (!CodeGenInfo)
142    return CodeGenOpt::Default;
143  return CodeGenInfo->getOptLevel();
144}
145
146void TargetMachine::setOptLevel(CodeGenOpt::Level Level) const {
147  if (CodeGenInfo)
148    CodeGenInfo->setOptLevel(Level);
149}
150
151bool TargetMachine::getAsmVerbosityDefault() const {
152  return Options.MCOptions.AsmVerbose;
153}
154
155void TargetMachine::setAsmVerbosityDefault(bool V) {
156  Options.MCOptions.AsmVerbose = V;
157}
158
159bool TargetMachine::getFunctionSections() const {
160  return Options.FunctionSections;
161}
162
163bool TargetMachine::getDataSections() const {
164  return Options.DataSections;
165}
166
167void TargetMachine::setFunctionSections(bool V) {
168  Options.FunctionSections = V;
169}
170
171void TargetMachine::setDataSections(bool V) {
172  Options.DataSections = V;
173}
174
175void TargetMachine::getNameWithPrefix(SmallVectorImpl<char> &Name,
176                                      const GlobalValue *GV, Mangler &Mang,
177                                      bool MayAlwaysUsePrivate) const {
178  if (MayAlwaysUsePrivate || !GV->hasPrivateLinkage()) {
179    // Simple case: If GV is not private, it is not important to find out if
180    // private labels are legal in this case or not.
181    Mang.getNameWithPrefix(Name, GV, false);
182    return;
183  }
184  SectionKind GVKind = TargetLoweringObjectFile::getKindForGlobal(GV, *this);
185  const TargetLoweringObjectFile &TLOF =
186      getTargetLowering()->getObjFileLowering();
187  const MCSection *TheSection = TLOF.SectionForGlobal(GV, GVKind, Mang, *this);
188  bool CannotUsePrivateLabel = TLOF.isSectionAtomizableBySymbols(*TheSection);
189  Mang.getNameWithPrefix(Name, GV, CannotUsePrivateLabel);
190}
191
192MCSymbol *TargetMachine::getSymbol(const GlobalValue *GV, Mangler &Mang) const {
193  SmallString<60> NameStr;
194  getNameWithPrefix(NameStr, GV, Mang);
195  const TargetLoweringObjectFile &TLOF =
196      getTargetLowering()->getObjFileLowering();
197  return TLOF.getContext().GetOrCreateSymbol(NameStr.str());
198}
199