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),
62 MCUseDwarfDirectory(false),
63 RequireStructuredCFG(false),
67 TargetMachine::~TargetMachine() {
72 /// \brief Reset the target options based on the function's attributes.
73 void TargetMachine::resetTargetOptions(const MachineFunction *MF) const {
74 const Function *F = MF->getFunction();
75 TargetOptions &TO = MF->getTarget().Options;
77 #define RESET_OPTION(X, Y) \
79 if (F->hasFnAttribute(Y)) \
81 (F->getAttributes(). \
82 getAttribute(AttributeSet::FunctionIndex, \
83 Y).getValueAsString() == "true"); \
86 RESET_OPTION(NoFramePointerElim, "no-frame-pointer-elim");
87 RESET_OPTION(LessPreciseFPMADOption, "less-precise-fpmad");
88 RESET_OPTION(UnsafeFPMath, "unsafe-fp-math");
89 RESET_OPTION(NoInfsFPMath, "no-infs-fp-math");
90 RESET_OPTION(NoNaNsFPMath, "no-nans-fp-math");
91 RESET_OPTION(UseSoftFloat, "use-soft-float");
92 RESET_OPTION(DisableTailCalls, "disable-tail-calls");
94 TO.MCOptions.SanitizeAddress = F->hasFnAttribute(Attribute::SanitizeAddress);
97 /// getRelocationModel - Returns the code generation relocation model. The
98 /// choices are static, PIC, and dynamic-no-pic, and target default.
99 Reloc::Model TargetMachine::getRelocationModel() const {
101 return Reloc::Default;
102 return CodeGenInfo->getRelocationModel();
105 /// getCodeModel - Returns the code model. The choices are small, kernel,
106 /// medium, large, and target default.
107 CodeModel::Model TargetMachine::getCodeModel() const {
109 return CodeModel::Default;
110 return CodeGenInfo->getCodeModel();
113 /// Get the IR-specified TLS model for Var.
114 static TLSModel::Model getSelectedTLSModel(const GlobalVariable *Var) {
115 switch (Var->getThreadLocalMode()) {
116 case GlobalVariable::NotThreadLocal:
117 llvm_unreachable("getSelectedTLSModel for non-TLS variable");
119 case GlobalVariable::GeneralDynamicTLSModel:
120 return TLSModel::GeneralDynamic;
121 case GlobalVariable::LocalDynamicTLSModel:
122 return TLSModel::LocalDynamic;
123 case GlobalVariable::InitialExecTLSModel:
124 return TLSModel::InitialExec;
125 case GlobalVariable::LocalExecTLSModel:
126 return TLSModel::LocalExec;
128 llvm_unreachable("invalid TLS model");
131 TLSModel::Model TargetMachine::getTLSModel(const GlobalValue *GV) const {
132 // If GV is an alias then use the aliasee for determining
134 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
135 GV = GA->getAliasedGlobal();
136 const GlobalVariable *Var = cast<GlobalVariable>(GV);
138 bool isLocal = Var->hasLocalLinkage();
139 bool isDeclaration = Var->isDeclaration();
140 bool isPIC = getRelocationModel() == Reloc::PIC_;
141 bool isPIE = Options.PositionIndependentExecutable;
142 // FIXME: what should we do for protected and internal visibility?
143 // For variables, is internal different from hidden?
144 bool isHidden = Var->hasHiddenVisibility();
146 TLSModel::Model Model;
147 if (isPIC && !isPIE) {
148 if (isLocal || isHidden)
149 Model = TLSModel::LocalDynamic;
151 Model = TLSModel::GeneralDynamic;
153 if (!isDeclaration || isHidden)
154 Model = TLSModel::LocalExec;
156 Model = TLSModel::InitialExec;
159 // If the user specified a more specific model, use that.
160 TLSModel::Model SelectedModel = getSelectedTLSModel(Var);
161 if (SelectedModel > Model)
162 return SelectedModel;
167 /// getOptLevel - Returns the optimization level: None, Less,
168 /// Default, or Aggressive.
169 CodeGenOpt::Level TargetMachine::getOptLevel() const {
171 return CodeGenOpt::Default;
172 return CodeGenInfo->getOptLevel();
175 void TargetMachine::setOptLevel(CodeGenOpt::Level Level) const {
177 CodeGenInfo->setOptLevel(Level);
180 bool TargetMachine::getAsmVerbosityDefault() {
181 return AsmVerbosityDefault;
184 void TargetMachine::setAsmVerbosityDefault(bool V) {
185 AsmVerbosityDefault = V;
188 bool TargetMachine::getFunctionSections() {
189 return FunctionSections;
192 bool TargetMachine::getDataSections() {
196 void TargetMachine::setFunctionSections(bool V) {
197 FunctionSections = V;
200 void TargetMachine::setDataSections(bool V) {
204 void TargetMachine::getNameWithPrefix(SmallVectorImpl<char> &Name,
205 const GlobalValue *GV, Mangler &Mang,
206 bool MayAlwaysUsePrivate) const {
207 if (MayAlwaysUsePrivate || !GV->hasPrivateLinkage()) {
208 // Simple case: If GV is not private, it is not important to find out if
209 // private labels are legal in this case or not.
210 Mang.getNameWithPrefix(Name, GV, false);
213 SectionKind GVKind = TargetLoweringObjectFile::getKindForGlobal(GV, *this);
214 const TargetLoweringObjectFile &TLOF =
215 getTargetLowering()->getObjFileLowering();
216 const MCSection *TheSection = TLOF.SectionForGlobal(GV, GVKind, Mang, *this);
217 bool CannotUsePrivateLabel = TLOF.isSectionAtomizableBySymbols(*TheSection);
218 Mang.getNameWithPrefix(Name, GV, CannotUsePrivateLabel);
221 MCSymbol *TargetMachine::getSymbol(const GlobalValue *GV, Mangler &Mang) const {
222 SmallString<60> NameStr;
223 getNameWithPrefix(NameStr, GV, Mang);
224 const TargetLoweringObjectFile &TLOF =
225 getTargetLowering()->getObjFileLowering();
226 return TLOF.getContext().GetOrCreateSymbol(NameStr.str());