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/Analysis/TargetTransformInfo.h"
16 #include "llvm/CodeGen/MachineFunction.h"
17 #include "llvm/IR/Function.h"
18 #include "llvm/IR/GlobalAlias.h"
19 #include "llvm/IR/GlobalValue.h"
20 #include "llvm/IR/GlobalVariable.h"
21 #include "llvm/IR/Mangler.h"
22 #include "llvm/MC/MCAsmInfo.h"
23 #include "llvm/MC/MCCodeGenInfo.h"
24 #include "llvm/MC/MCContext.h"
25 #include "llvm/MC/MCInstrInfo.h"
26 #include "llvm/MC/MCSectionMachO.h"
27 #include "llvm/MC/MCTargetOptions.h"
28 #include "llvm/MC/SectionKind.h"
29 #include "llvm/IR/LegacyPassManager.h"
30 #include "llvm/Support/CommandLine.h"
31 #include "llvm/Target/TargetLowering.h"
32 #include "llvm/Target/TargetLoweringObjectFile.h"
33 #include "llvm/Target/TargetSubtargetInfo.h"
36 //---------------------------------------------------------------------------
37 // TargetMachine Class
40 TargetMachine::TargetMachine(const Target &T, StringRef DataLayoutString,
41 StringRef TT, StringRef CPU, StringRef FS,
42 const TargetOptions &Options)
43 : TheTarget(T), DL(DataLayoutString), TargetTriple(TT), TargetCPU(CPU),
44 TargetFS(FS), CodeGenInfo(nullptr), AsmInfo(nullptr), MRI(nullptr),
45 MII(nullptr), RequireStructuredCFG(false), Options(Options) {}
47 TargetMachine::~TargetMachine() {
54 /// \brief Reset the target options based on the function's attributes.
55 void TargetMachine::resetTargetOptions(const Function &F) const {
56 #define RESET_OPTION(X, Y) \
58 if (F.hasFnAttribute(Y)) \
59 Options.X = (F.getFnAttribute(Y).getValueAsString() == "true"); \
62 RESET_OPTION(NoFramePointerElim, "no-frame-pointer-elim");
63 RESET_OPTION(LessPreciseFPMADOption, "less-precise-fpmad");
64 RESET_OPTION(UnsafeFPMath, "unsafe-fp-math");
65 RESET_OPTION(NoInfsFPMath, "no-infs-fp-math");
66 RESET_OPTION(NoNaNsFPMath, "no-nans-fp-math");
67 RESET_OPTION(UseSoftFloat, "use-soft-float");
68 RESET_OPTION(DisableTailCalls, "disable-tail-calls");
70 Options.MCOptions.SanitizeAddress = F.hasFnAttribute(Attribute::SanitizeAddress);
73 /// getRelocationModel - Returns the code generation relocation model. The
74 /// choices are static, PIC, and dynamic-no-pic, and target default.
75 Reloc::Model TargetMachine::getRelocationModel() const {
77 return Reloc::Default;
78 return CodeGenInfo->getRelocationModel();
81 /// getCodeModel - Returns the code model. The choices are small, kernel,
82 /// medium, large, and target default.
83 CodeModel::Model TargetMachine::getCodeModel() const {
85 return CodeModel::Default;
86 return CodeGenInfo->getCodeModel();
89 /// Get the IR-specified TLS model for Var.
90 static TLSModel::Model getSelectedTLSModel(const GlobalValue *GV) {
91 switch (GV->getThreadLocalMode()) {
92 case GlobalVariable::NotThreadLocal:
93 llvm_unreachable("getSelectedTLSModel for non-TLS variable");
95 case GlobalVariable::GeneralDynamicTLSModel:
96 return TLSModel::GeneralDynamic;
97 case GlobalVariable::LocalDynamicTLSModel:
98 return TLSModel::LocalDynamic;
99 case GlobalVariable::InitialExecTLSModel:
100 return TLSModel::InitialExec;
101 case GlobalVariable::LocalExecTLSModel:
102 return TLSModel::LocalExec;
104 llvm_unreachable("invalid TLS model");
107 TLSModel::Model TargetMachine::getTLSModel(const GlobalValue *GV) const {
108 bool isLocal = GV->hasLocalLinkage();
109 bool isDeclaration = GV->isDeclaration();
110 bool isPIC = getRelocationModel() == Reloc::PIC_;
111 bool isPIE = Options.PositionIndependentExecutable;
112 // FIXME: what should we do for protected and internal visibility?
113 // For variables, is internal different from hidden?
114 bool isHidden = GV->hasHiddenVisibility();
116 TLSModel::Model Model;
117 if (isPIC && !isPIE) {
118 if (isLocal || isHidden)
119 Model = TLSModel::LocalDynamic;
121 Model = TLSModel::GeneralDynamic;
123 if (!isDeclaration || isHidden)
124 Model = TLSModel::LocalExec;
126 Model = TLSModel::InitialExec;
129 // If the user specified a more specific model, use that.
130 TLSModel::Model SelectedModel = getSelectedTLSModel(GV);
131 if (SelectedModel > Model)
132 return SelectedModel;
137 /// getOptLevel - Returns the optimization level: None, Less,
138 /// Default, or Aggressive.
139 CodeGenOpt::Level TargetMachine::getOptLevel() const {
141 return CodeGenOpt::Default;
142 return CodeGenInfo->getOptLevel();
145 void TargetMachine::setOptLevel(CodeGenOpt::Level Level) const {
147 CodeGenInfo->setOptLevel(Level);
150 TargetIRAnalysis TargetMachine::getTargetIRAnalysis() {
151 return TargetIRAnalysis(
152 [this](Function &) { return TargetTransformInfo(getDataLayout()); });
155 static bool canUsePrivateLabel(const MCAsmInfo &AsmInfo,
156 const MCSection &Section) {
157 if (!AsmInfo.isSectionAtomizableBySymbols(Section))
160 // If it is not dead stripped, it is safe to use private labels.
161 const MCSectionMachO &SMO = cast<MCSectionMachO>(Section);
162 if (SMO.hasAttribute(MachO::S_ATTR_NO_DEAD_STRIP))
168 void TargetMachine::getNameWithPrefix(SmallVectorImpl<char> &Name,
169 const GlobalValue *GV, Mangler &Mang,
170 bool MayAlwaysUsePrivate) const {
171 if (MayAlwaysUsePrivate || !GV->hasPrivateLinkage()) {
172 // Simple case: If GV is not private, it is not important to find out if
173 // private labels are legal in this case or not.
174 Mang.getNameWithPrefix(Name, GV, false);
177 SectionKind GVKind = TargetLoweringObjectFile::getKindForGlobal(GV, *this);
178 const TargetLoweringObjectFile *TLOF = getObjFileLowering();
179 const MCSection *TheSection = TLOF->SectionForGlobal(GV, GVKind, Mang, *this);
180 bool CannotUsePrivateLabel = !canUsePrivateLabel(*AsmInfo, *TheSection);
181 TLOF->getNameWithPrefix(Name, GV, CannotUsePrivateLabel, Mang, *this);
184 MCSymbol *TargetMachine::getSymbol(const GlobalValue *GV, Mangler &Mang) const {
185 SmallString<60> NameStr;
186 getNameWithPrefix(NameStr, GV, Mang);
187 const TargetLoweringObjectFile *TLOF = getObjFileLowering();
188 return TLOF->getContext().GetOrCreateSymbol(NameStr.str());