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), STI(nullptr), RequireStructuredCFG(false),
48 TargetMachine::~TargetMachine() {
56 /// \brief Reset the target options based on the function's attributes.
57 // FIXME: This function needs to go away for a number of reasons:
58 // a) global state on the TargetMachine is terrible in general,
59 // b) there's no default state here to keep,
60 // c) these target options should be passed only on the function
61 // and not on the TargetMachine (via TargetOptions) at all.
62 void TargetMachine::resetTargetOptions(const Function &F) const {
63 #define RESET_OPTION(X, Y) \
65 if (F.hasFnAttribute(Y)) \
66 Options.X = (F.getFnAttribute(Y).getValueAsString() == "true"); \
69 RESET_OPTION(NoFramePointerElim, "no-frame-pointer-elim");
70 RESET_OPTION(LessPreciseFPMADOption, "less-precise-fpmad");
71 RESET_OPTION(UnsafeFPMath, "unsafe-fp-math");
72 RESET_OPTION(NoInfsFPMath, "no-infs-fp-math");
73 RESET_OPTION(NoNaNsFPMath, "no-nans-fp-math");
74 RESET_OPTION(UseSoftFloat, "use-soft-float");
75 RESET_OPTION(DisableTailCalls, "disable-tail-calls");
77 Options.MCOptions.SanitizeAddress = F.hasFnAttribute(Attribute::SanitizeAddress);
80 /// getRelocationModel - Returns the code generation relocation model. The
81 /// choices are static, PIC, and dynamic-no-pic, and target default.
82 Reloc::Model TargetMachine::getRelocationModel() const {
84 return Reloc::Default;
85 return CodeGenInfo->getRelocationModel();
88 /// getCodeModel - Returns the code model. The choices are small, kernel,
89 /// medium, large, and target default.
90 CodeModel::Model TargetMachine::getCodeModel() const {
92 return CodeModel::Default;
93 return CodeGenInfo->getCodeModel();
96 /// Get the IR-specified TLS model for Var.
97 static TLSModel::Model getSelectedTLSModel(const GlobalValue *GV) {
98 switch (GV->getThreadLocalMode()) {
99 case GlobalVariable::NotThreadLocal:
100 llvm_unreachable("getSelectedTLSModel for non-TLS variable");
102 case GlobalVariable::GeneralDynamicTLSModel:
103 return TLSModel::GeneralDynamic;
104 case GlobalVariable::LocalDynamicTLSModel:
105 return TLSModel::LocalDynamic;
106 case GlobalVariable::InitialExecTLSModel:
107 return TLSModel::InitialExec;
108 case GlobalVariable::LocalExecTLSModel:
109 return TLSModel::LocalExec;
111 llvm_unreachable("invalid TLS model");
114 TLSModel::Model TargetMachine::getTLSModel(const GlobalValue *GV) const {
115 bool isLocal = GV->hasLocalLinkage();
116 bool isDeclaration = GV->isDeclaration();
117 bool isPIC = getRelocationModel() == Reloc::PIC_;
118 bool isPIE = Options.PositionIndependentExecutable;
119 // FIXME: what should we do for protected and internal visibility?
120 // For variables, is internal different from hidden?
121 bool isHidden = GV->hasHiddenVisibility();
123 TLSModel::Model Model;
124 if (isPIC && !isPIE) {
125 if (isLocal || isHidden)
126 Model = TLSModel::LocalDynamic;
128 Model = TLSModel::GeneralDynamic;
130 if (!isDeclaration || isHidden)
131 Model = TLSModel::LocalExec;
133 Model = TLSModel::InitialExec;
136 // If the user specified a more specific model, use that.
137 TLSModel::Model SelectedModel = getSelectedTLSModel(GV);
138 if (SelectedModel > Model)
139 return SelectedModel;
144 /// getOptLevel - Returns the optimization level: None, Less,
145 /// Default, or Aggressive.
146 CodeGenOpt::Level TargetMachine::getOptLevel() const {
148 return CodeGenOpt::Default;
149 return CodeGenInfo->getOptLevel();
152 void TargetMachine::setOptLevel(CodeGenOpt::Level Level) const {
154 CodeGenInfo->setOptLevel(Level);
157 TargetIRAnalysis TargetMachine::getTargetIRAnalysis() {
158 return TargetIRAnalysis(
159 [this](Function &) { return TargetTransformInfo(getDataLayout()); });
162 static bool canUsePrivateLabel(const MCAsmInfo &AsmInfo,
163 const MCSection &Section) {
164 if (!AsmInfo.isSectionAtomizableBySymbols(Section))
167 // If it is not dead stripped, it is safe to use private labels.
168 const MCSectionMachO &SMO = cast<MCSectionMachO>(Section);
169 if (SMO.hasAttribute(MachO::S_ATTR_NO_DEAD_STRIP))
175 void TargetMachine::getNameWithPrefix(SmallVectorImpl<char> &Name,
176 const GlobalValue *GV, Mangler &Mang,
177 bool MayAlwaysUsePrivate) const {
178 if (MayAlwaysUsePrivate || !GV->hasPrivateLinkage()) {
179 // Simple case: If GV is not private, it is not important to find out if
180 // private labels are legal in this case or not.
181 Mang.getNameWithPrefix(Name, GV, false);
184 SectionKind GVKind = TargetLoweringObjectFile::getKindForGlobal(GV, *this);
185 const TargetLoweringObjectFile *TLOF = getObjFileLowering();
186 const MCSection *TheSection = TLOF->SectionForGlobal(GV, GVKind, Mang, *this);
187 bool CannotUsePrivateLabel = !canUsePrivateLabel(*AsmInfo, *TheSection);
188 TLOF->getNameWithPrefix(Name, GV, CannotUsePrivateLabel, Mang, *this);
191 MCSymbol *TargetMachine::getSymbol(const GlobalValue *GV, Mangler &Mang) const {
192 SmallString<60> NameStr;
193 getNameWithPrefix(NameStr, GV, Mang);
194 const TargetLoweringObjectFile *TLOF = getObjFileLowering();
195 return TLOF->getContext().GetOrCreateSymbol(NameStr);