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 MachineFunction *MF) const {
52 const Function *F = MF->getFunction();
53 TargetOptions &TO = MF->getTarget().Options;
55 #define RESET_OPTION(X, Y) \
57 if (F->hasFnAttribute(Y)) \
59 (F->getAttributes(). \
60 getAttribute(AttributeSet::FunctionIndex, \
61 Y).getValueAsString() == "true"); \
64 RESET_OPTION(NoFramePointerElim, "no-frame-pointer-elim");
65 RESET_OPTION(LessPreciseFPMADOption, "less-precise-fpmad");
66 RESET_OPTION(UnsafeFPMath, "unsafe-fp-math");
67 RESET_OPTION(NoInfsFPMath, "no-infs-fp-math");
68 RESET_OPTION(NoNaNsFPMath, "no-nans-fp-math");
69 RESET_OPTION(UseSoftFloat, "use-soft-float");
70 RESET_OPTION(DisableTailCalls, "disable-tail-calls");
72 TO.MCOptions.SanitizeAddress = F->hasFnAttribute(Attribute::SanitizeAddress);
75 /// getRelocationModel - Returns the code generation relocation model. The
76 /// choices are static, PIC, and dynamic-no-pic, and target default.
77 Reloc::Model TargetMachine::getRelocationModel() const {
79 return Reloc::Default;
80 return CodeGenInfo->getRelocationModel();
83 /// getCodeModel - Returns the code model. The choices are small, kernel,
84 /// medium, large, and target default.
85 CodeModel::Model TargetMachine::getCodeModel() const {
87 return CodeModel::Default;
88 return CodeGenInfo->getCodeModel();
91 /// Get the IR-specified TLS model for Var.
92 static TLSModel::Model getSelectedTLSModel(const GlobalValue *GV) {
93 switch (GV->getThreadLocalMode()) {
94 case GlobalVariable::NotThreadLocal:
95 llvm_unreachable("getSelectedTLSModel for non-TLS variable");
97 case GlobalVariable::GeneralDynamicTLSModel:
98 return TLSModel::GeneralDynamic;
99 case GlobalVariable::LocalDynamicTLSModel:
100 return TLSModel::LocalDynamic;
101 case GlobalVariable::InitialExecTLSModel:
102 return TLSModel::InitialExec;
103 case GlobalVariable::LocalExecTLSModel:
104 return TLSModel::LocalExec;
106 llvm_unreachable("invalid TLS model");
109 TLSModel::Model TargetMachine::getTLSModel(const GlobalValue *GV) const {
110 bool isLocal = GV->hasLocalLinkage();
111 bool isDeclaration = GV->isDeclaration();
112 bool isPIC = getRelocationModel() == Reloc::PIC_;
113 bool isPIE = Options.PositionIndependentExecutable;
114 // FIXME: what should we do for protected and internal visibility?
115 // For variables, is internal different from hidden?
116 bool isHidden = GV->hasHiddenVisibility();
118 TLSModel::Model Model;
119 if (isPIC && !isPIE) {
120 if (isLocal || isHidden)
121 Model = TLSModel::LocalDynamic;
123 Model = TLSModel::GeneralDynamic;
125 if (!isDeclaration || isHidden)
126 Model = TLSModel::LocalExec;
128 Model = TLSModel::InitialExec;
131 // If the user specified a more specific model, use that.
132 TLSModel::Model SelectedModel = getSelectedTLSModel(GV);
133 if (SelectedModel > Model)
134 return SelectedModel;
139 /// getOptLevel - Returns the optimization level: None, Less,
140 /// Default, or Aggressive.
141 CodeGenOpt::Level TargetMachine::getOptLevel() const {
143 return CodeGenOpt::Default;
144 return CodeGenInfo->getOptLevel();
147 void TargetMachine::setOptLevel(CodeGenOpt::Level Level) const {
149 CodeGenInfo->setOptLevel(Level);
152 bool TargetMachine::getAsmVerbosityDefault() const {
153 return Options.MCOptions.AsmVerbose;
156 void TargetMachine::setAsmVerbosityDefault(bool V) {
157 Options.MCOptions.AsmVerbose = V;
160 bool TargetMachine::getFunctionSections() const {
161 return Options.FunctionSections;
164 bool TargetMachine::getDataSections() const {
165 return Options.DataSections;
168 void TargetMachine::setFunctionSections(bool V) {
169 Options.FunctionSections = V;
172 void TargetMachine::setDataSections(bool V) {
173 Options.DataSections = V;
176 void TargetMachine::getNameWithPrefix(SmallVectorImpl<char> &Name,
177 const GlobalValue *GV, Mangler &Mang,
178 bool MayAlwaysUsePrivate) const {
179 if (MayAlwaysUsePrivate || !GV->hasPrivateLinkage()) {
180 // Simple case: If GV is not private, it is not important to find out if
181 // private labels are legal in this case or not.
182 Mang.getNameWithPrefix(Name, GV, false);
185 SectionKind GVKind = TargetLoweringObjectFile::getKindForGlobal(GV, *this);
186 const TargetLoweringObjectFile &TLOF =
187 getSubtargetImpl()->getTargetLowering()->getObjFileLowering();
188 const MCSection *TheSection = TLOF.SectionForGlobal(GV, GVKind, Mang, *this);
189 bool CannotUsePrivateLabel = TLOF.isSectionAtomizableBySymbols(*TheSection);
190 Mang.getNameWithPrefix(Name, GV, CannotUsePrivateLabel);
193 MCSymbol *TargetMachine::getSymbol(const GlobalValue *GV, Mangler &Mang) const {
194 SmallString<60> NameStr;
195 getNameWithPrefix(NameStr, GV, Mang);
196 const TargetLoweringObjectFile &TLOF =
197 getSubtargetImpl()->getTargetLowering()->getObjFileLowering();
198 return TLOF.getContext().GetOrCreateSymbol(NameStr.str());