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"
31 //---------------------------------------------------------------------------
32 // Command-line options that tend to be useful on more than one back-end.
36 bool HasDivModLibcall;
37 bool AsmVerbosityDefault(false);
41 DataSections("fdata-sections",
42 cl::desc("Emit data into separate sections"),
45 FunctionSections("ffunction-sections",
46 cl::desc("Emit functions into separate sections"),
49 //---------------------------------------------------------------------------
50 // TargetMachine Class
53 TargetMachine::TargetMachine(const Target &T,
54 StringRef TT, StringRef CPU, StringRef FS,
55 const TargetOptions &Options)
56 : TheTarget(T), TargetTriple(TT), TargetCPU(CPU), TargetFS(FS),
57 CodeGenInfo(nullptr), AsmInfo(nullptr),
60 MCSaveTempLabels(false),
61 MCUseDwarfDirectory(false),
62 RequireStructuredCFG(false),
66 TargetMachine::~TargetMachine() {
71 /// \brief Reset the target options based on the function's attributes.
72 void TargetMachine::resetTargetOptions(const MachineFunction *MF) const {
73 const Function *F = MF->getFunction();
74 TargetOptions &TO = MF->getTarget().Options;
76 #define RESET_OPTION(X, Y) \
78 if (F->hasFnAttribute(Y)) \
80 (F->getAttributes(). \
81 getAttribute(AttributeSet::FunctionIndex, \
82 Y).getValueAsString() == "true"); \
85 RESET_OPTION(NoFramePointerElim, "no-frame-pointer-elim");
86 RESET_OPTION(LessPreciseFPMADOption, "less-precise-fpmad");
87 RESET_OPTION(UnsafeFPMath, "unsafe-fp-math");
88 RESET_OPTION(NoInfsFPMath, "no-infs-fp-math");
89 RESET_OPTION(NoNaNsFPMath, "no-nans-fp-math");
90 RESET_OPTION(UseSoftFloat, "use-soft-float");
91 RESET_OPTION(DisableTailCalls, "disable-tail-calls");
93 TO.MCOptions.SanitizeAddress = F->hasFnAttribute(Attribute::SanitizeAddress);
96 /// getRelocationModel - Returns the code generation relocation model. The
97 /// choices are static, PIC, and dynamic-no-pic, and target default.
98 Reloc::Model TargetMachine::getRelocationModel() const {
100 return Reloc::Default;
101 return CodeGenInfo->getRelocationModel();
104 /// getCodeModel - Returns the code model. The choices are small, kernel,
105 /// medium, large, and target default.
106 CodeModel::Model TargetMachine::getCodeModel() const {
108 return CodeModel::Default;
109 return CodeGenInfo->getCodeModel();
112 /// Get the IR-specified TLS model for Var.
113 static TLSModel::Model getSelectedTLSModel(const GlobalVariable *Var) {
114 switch (Var->getThreadLocalMode()) {
115 case GlobalVariable::NotThreadLocal:
116 llvm_unreachable("getSelectedTLSModel for non-TLS variable");
118 case GlobalVariable::GeneralDynamicTLSModel:
119 return TLSModel::GeneralDynamic;
120 case GlobalVariable::LocalDynamicTLSModel:
121 return TLSModel::LocalDynamic;
122 case GlobalVariable::InitialExecTLSModel:
123 return TLSModel::InitialExec;
124 case GlobalVariable::LocalExecTLSModel:
125 return TLSModel::LocalExec;
127 llvm_unreachable("invalid TLS model");
130 TLSModel::Model TargetMachine::getTLSModel(const GlobalValue *GV) const {
131 // If GV is an alias then use the aliasee for determining
133 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
134 GV = GA->getAliasedGlobal();
135 const GlobalVariable *Var = cast<GlobalVariable>(GV);
137 bool isLocal = Var->hasLocalLinkage();
138 bool isDeclaration = Var->isDeclaration();
139 bool isPIC = getRelocationModel() == Reloc::PIC_;
140 bool isPIE = Options.PositionIndependentExecutable;
141 // FIXME: what should we do for protected and internal visibility?
142 // For variables, is internal different from hidden?
143 bool isHidden = Var->hasHiddenVisibility();
145 TLSModel::Model Model;
146 if (isPIC && !isPIE) {
147 if (isLocal || isHidden)
148 Model = TLSModel::LocalDynamic;
150 Model = TLSModel::GeneralDynamic;
152 if (!isDeclaration || isHidden)
153 Model = TLSModel::LocalExec;
155 Model = TLSModel::InitialExec;
158 // If the user specified a more specific model, use that.
159 TLSModel::Model SelectedModel = getSelectedTLSModel(Var);
160 if (SelectedModel > Model)
161 return SelectedModel;
166 /// getOptLevel - Returns the optimization level: None, Less,
167 /// Default, or Aggressive.
168 CodeGenOpt::Level TargetMachine::getOptLevel() const {
170 return CodeGenOpt::Default;
171 return CodeGenInfo->getOptLevel();
174 void TargetMachine::setOptLevel(CodeGenOpt::Level Level) const {
176 CodeGenInfo->setOptLevel(Level);
179 bool TargetMachine::getAsmVerbosityDefault() {
180 return AsmVerbosityDefault;
183 void TargetMachine::setAsmVerbosityDefault(bool V) {
184 AsmVerbosityDefault = V;
187 bool TargetMachine::getFunctionSections() {
188 return FunctionSections;
191 bool TargetMachine::getDataSections() {
195 void TargetMachine::setFunctionSections(bool V) {
196 FunctionSections = V;
199 void TargetMachine::setDataSections(bool V) {
203 void TargetMachine::getNameWithPrefix(SmallVectorImpl<char> &Name,
204 const GlobalValue *GV, Mangler &Mang,
205 bool MayAlwaysUsePrivate) const {
206 if (MayAlwaysUsePrivate || !GV->hasPrivateLinkage()) {
207 // Simple case: If GV is not private, it is not important to find out if
208 // private labels are legal in this case or not.
209 Mang.getNameWithPrefix(Name, GV, false);
212 SectionKind GVKind = TargetLoweringObjectFile::getKindForGlobal(GV, *this);
213 const TargetLoweringObjectFile &TLOF =
214 getTargetLowering()->getObjFileLowering();
215 const MCSection *TheSection = TLOF.SectionForGlobal(GV, GVKind, Mang, *this);
216 bool CannotUsePrivateLabel = TLOF.isSectionAtomizableBySymbols(*TheSection);
217 Mang.getNameWithPrefix(Name, GV, CannotUsePrivateLabel);
220 MCSymbol *TargetMachine::getSymbol(const GlobalValue *GV, Mangler &Mang) const {
221 SmallString<60> NameStr;
222 getNameWithPrefix(NameStr, GV, Mang);
223 const TargetLoweringObjectFile &TLOF =
224 getTargetLowering()->getObjFileLowering();
225 return TLOF.getContext().GetOrCreateSymbol(NameStr.str());