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),
58 RequireStructuredCFG(false),
62 TargetMachine::~TargetMachine() {
67 /// \brief Reset the target options based on the function's attributes.
68 void TargetMachine::resetTargetOptions(const MachineFunction *MF) const {
69 const Function *F = MF->getFunction();
70 TargetOptions &TO = MF->getTarget().Options;
72 #define RESET_OPTION(X, Y) \
74 if (F->hasFnAttribute(Y)) \
76 (F->getAttributes(). \
77 getAttribute(AttributeSet::FunctionIndex, \
78 Y).getValueAsString() == "true"); \
81 RESET_OPTION(NoFramePointerElim, "no-frame-pointer-elim");
82 RESET_OPTION(LessPreciseFPMADOption, "less-precise-fpmad");
83 RESET_OPTION(UnsafeFPMath, "unsafe-fp-math");
84 RESET_OPTION(NoInfsFPMath, "no-infs-fp-math");
85 RESET_OPTION(NoNaNsFPMath, "no-nans-fp-math");
86 RESET_OPTION(UseSoftFloat, "use-soft-float");
87 RESET_OPTION(DisableTailCalls, "disable-tail-calls");
89 TO.MCOptions.SanitizeAddress = F->hasFnAttribute(Attribute::SanitizeAddress);
92 /// getRelocationModel - Returns the code generation relocation model. The
93 /// choices are static, PIC, and dynamic-no-pic, and target default.
94 Reloc::Model TargetMachine::getRelocationModel() const {
96 return Reloc::Default;
97 return CodeGenInfo->getRelocationModel();
100 /// getCodeModel - Returns the code model. The choices are small, kernel,
101 /// medium, large, and target default.
102 CodeModel::Model TargetMachine::getCodeModel() const {
104 return CodeModel::Default;
105 return CodeGenInfo->getCodeModel();
108 /// Get the IR-specified TLS model for Var.
109 static TLSModel::Model getSelectedTLSModel(const GlobalVariable *Var) {
110 switch (Var->getThreadLocalMode()) {
111 case GlobalVariable::NotThreadLocal:
112 llvm_unreachable("getSelectedTLSModel for non-TLS variable");
114 case GlobalVariable::GeneralDynamicTLSModel:
115 return TLSModel::GeneralDynamic;
116 case GlobalVariable::LocalDynamicTLSModel:
117 return TLSModel::LocalDynamic;
118 case GlobalVariable::InitialExecTLSModel:
119 return TLSModel::InitialExec;
120 case GlobalVariable::LocalExecTLSModel:
121 return TLSModel::LocalExec;
123 llvm_unreachable("invalid TLS model");
126 TLSModel::Model TargetMachine::getTLSModel(const GlobalValue *GV) const {
127 // If GV is an alias then use the aliasee for determining
129 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
130 GV = GA->getAliasedGlobal();
131 const GlobalVariable *Var = cast<GlobalVariable>(GV);
133 bool isLocal = Var->hasLocalLinkage();
134 bool isDeclaration = Var->isDeclaration();
135 bool isPIC = getRelocationModel() == Reloc::PIC_;
136 bool isPIE = Options.PositionIndependentExecutable;
137 // FIXME: what should we do for protected and internal visibility?
138 // For variables, is internal different from hidden?
139 bool isHidden = Var->hasHiddenVisibility();
141 TLSModel::Model Model;
142 if (isPIC && !isPIE) {
143 if (isLocal || isHidden)
144 Model = TLSModel::LocalDynamic;
146 Model = TLSModel::GeneralDynamic;
148 if (!isDeclaration || isHidden)
149 Model = TLSModel::LocalExec;
151 Model = TLSModel::InitialExec;
154 // If the user specified a more specific model, use that.
155 TLSModel::Model SelectedModel = getSelectedTLSModel(Var);
156 if (SelectedModel > Model)
157 return SelectedModel;
162 /// getOptLevel - Returns the optimization level: None, Less,
163 /// Default, or Aggressive.
164 CodeGenOpt::Level TargetMachine::getOptLevel() const {
166 return CodeGenOpt::Default;
167 return CodeGenInfo->getOptLevel();
170 void TargetMachine::setOptLevel(CodeGenOpt::Level Level) const {
172 CodeGenInfo->setOptLevel(Level);
175 bool TargetMachine::getAsmVerbosityDefault() {
176 return AsmVerbosityDefault;
179 void TargetMachine::setAsmVerbosityDefault(bool V) {
180 AsmVerbosityDefault = V;
183 bool TargetMachine::getFunctionSections() {
184 return FunctionSections;
187 bool TargetMachine::getDataSections() {
191 void TargetMachine::setFunctionSections(bool V) {
192 FunctionSections = V;
195 void TargetMachine::setDataSections(bool V) {
199 void TargetMachine::getNameWithPrefix(SmallVectorImpl<char> &Name,
200 const GlobalValue *GV, Mangler &Mang,
201 bool MayAlwaysUsePrivate) const {
202 if (MayAlwaysUsePrivate || !GV->hasPrivateLinkage()) {
203 // Simple case: If GV is not private, it is not important to find out if
204 // private labels are legal in this case or not.
205 Mang.getNameWithPrefix(Name, GV, false);
208 SectionKind GVKind = TargetLoweringObjectFile::getKindForGlobal(GV, *this);
209 const TargetLoweringObjectFile &TLOF =
210 getTargetLowering()->getObjFileLowering();
211 const MCSection *TheSection = TLOF.SectionForGlobal(GV, GVKind, Mang, *this);
212 bool CannotUsePrivateLabel = TLOF.isSectionAtomizableBySymbols(*TheSection);
213 Mang.getNameWithPrefix(Name, GV, CannotUsePrivateLabel);
216 MCSymbol *TargetMachine::getSymbol(const GlobalValue *GV, Mangler &Mang) const {
217 SmallString<60> NameStr;
218 getNameWithPrefix(NameStr, GV, Mang);
219 const TargetLoweringObjectFile &TLOF =
220 getTargetLowering()->getObjFileLowering();
221 return TLOF.getContext().GetOrCreateSymbol(NameStr.str());