1 //===-- TargetMachine.cpp - General Target Information ---------------------==//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file describes the general parts of a Target machine.
12 //===----------------------------------------------------------------------===//
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"
29 #include "llvm/Target/TargetSubtargetInfo.h"
32 //---------------------------------------------------------------------------
33 // TargetMachine Class
36 TargetMachine::TargetMachine(const Target &T,
37 StringRef TT, StringRef CPU, StringRef FS,
38 const TargetOptions &Options)
39 : TheTarget(T), TargetTriple(TT), TargetCPU(CPU), TargetFS(FS),
40 CodeGenInfo(nullptr), AsmInfo(nullptr),
41 RequireStructuredCFG(false),
45 TargetMachine::~TargetMachine() {
50 /// \brief Reset the target options based on the function's attributes.
51 void TargetMachine::resetTargetOptions(const Function &F) const {
52 #define RESET_OPTION(X, Y) \
54 if (F.hasFnAttribute(Y)) \
55 Options.X = (F.getAttributes() \
56 .getAttribute(AttributeSet::FunctionIndex, Y) \
57 .getValueAsString() == "true"); \
60 RESET_OPTION(NoFramePointerElim, "no-frame-pointer-elim");
61 RESET_OPTION(LessPreciseFPMADOption, "less-precise-fpmad");
62 RESET_OPTION(UnsafeFPMath, "unsafe-fp-math");
63 RESET_OPTION(NoInfsFPMath, "no-infs-fp-math");
64 RESET_OPTION(NoNaNsFPMath, "no-nans-fp-math");
65 RESET_OPTION(UseSoftFloat, "use-soft-float");
66 RESET_OPTION(DisableTailCalls, "disable-tail-calls");
68 Options.MCOptions.SanitizeAddress = F.hasFnAttribute(Attribute::SanitizeAddress);
71 /// getRelocationModel - Returns the code generation relocation model. The
72 /// choices are static, PIC, and dynamic-no-pic, and target default.
73 Reloc::Model TargetMachine::getRelocationModel() const {
75 return Reloc::Default;
76 return CodeGenInfo->getRelocationModel();
79 /// getCodeModel - Returns the code model. The choices are small, kernel,
80 /// medium, large, and target default.
81 CodeModel::Model TargetMachine::getCodeModel() const {
83 return CodeModel::Default;
84 return CodeGenInfo->getCodeModel();
87 /// Get the IR-specified TLS model for Var.
88 static TLSModel::Model getSelectedTLSModel(const GlobalValue *GV) {
89 switch (GV->getThreadLocalMode()) {
90 case GlobalVariable::NotThreadLocal:
91 llvm_unreachable("getSelectedTLSModel for non-TLS variable");
93 case GlobalVariable::GeneralDynamicTLSModel:
94 return TLSModel::GeneralDynamic;
95 case GlobalVariable::LocalDynamicTLSModel:
96 return TLSModel::LocalDynamic;
97 case GlobalVariable::InitialExecTLSModel:
98 return TLSModel::InitialExec;
99 case GlobalVariable::LocalExecTLSModel:
100 return TLSModel::LocalExec;
102 llvm_unreachable("invalid TLS model");
105 TLSModel::Model TargetMachine::getTLSModel(const GlobalValue *GV) const {
106 bool isLocal = GV->hasLocalLinkage();
107 bool isDeclaration = GV->isDeclaration();
108 bool isPIC = getRelocationModel() == Reloc::PIC_;
109 bool isPIE = Options.PositionIndependentExecutable;
110 // FIXME: what should we do for protected and internal visibility?
111 // For variables, is internal different from hidden?
112 bool isHidden = GV->hasHiddenVisibility();
114 TLSModel::Model Model;
115 if (isPIC && !isPIE) {
116 if (isLocal || isHidden)
117 Model = TLSModel::LocalDynamic;
119 Model = TLSModel::GeneralDynamic;
121 if (!isDeclaration || isHidden)
122 Model = TLSModel::LocalExec;
124 Model = TLSModel::InitialExec;
127 // If the user specified a more specific model, use that.
128 TLSModel::Model SelectedModel = getSelectedTLSModel(GV);
129 if (SelectedModel > Model)
130 return SelectedModel;
135 /// getOptLevel - Returns the optimization level: None, Less,
136 /// Default, or Aggressive.
137 CodeGenOpt::Level TargetMachine::getOptLevel() const {
139 return CodeGenOpt::Default;
140 return CodeGenInfo->getOptLevel();
143 void TargetMachine::setOptLevel(CodeGenOpt::Level Level) const {
145 CodeGenInfo->setOptLevel(Level);
148 bool TargetMachine::getAsmVerbosityDefault() const {
149 return Options.MCOptions.AsmVerbose;
152 void TargetMachine::setAsmVerbosityDefault(bool V) {
153 Options.MCOptions.AsmVerbose = V;
156 bool TargetMachine::getFunctionSections() const {
157 return Options.FunctionSections;
160 bool TargetMachine::getDataSections() const {
161 return Options.DataSections;
164 void TargetMachine::setFunctionSections(bool V) {
165 Options.FunctionSections = V;
168 void TargetMachine::setDataSections(bool V) {
169 Options.DataSections = V;
172 void TargetMachine::getNameWithPrefix(SmallVectorImpl<char> &Name,
173 const GlobalValue *GV, Mangler &Mang,
174 bool MayAlwaysUsePrivate) const {
175 if (MayAlwaysUsePrivate || !GV->hasPrivateLinkage()) {
176 // Simple case: If GV is not private, it is not important to find out if
177 // private labels are legal in this case or not.
178 Mang.getNameWithPrefix(Name, GV, false);
181 SectionKind GVKind = TargetLoweringObjectFile::getKindForGlobal(GV, *this);
182 const TargetLoweringObjectFile &TLOF =
183 getSubtargetImpl()->getTargetLowering()->getObjFileLowering();
184 const MCSection *TheSection = TLOF.SectionForGlobal(GV, GVKind, Mang, *this);
185 bool CannotUsePrivateLabel = TLOF.isSectionAtomizableBySymbols(*TheSection);
186 Mang.getNameWithPrefix(Name, GV, CannotUsePrivateLabel);
189 MCSymbol *TargetMachine::getSymbol(const GlobalValue *GV, Mangler &Mang) const {
190 SmallString<60> NameStr;
191 getNameWithPrefix(NameStr, GV, Mang);
192 const TargetLoweringObjectFile &TLOF =
193 getSubtargetImpl()->getTargetLowering()->getObjFileLowering();
194 return TLOF.getContext().GetOrCreateSymbol(NameStr.str());