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