1bb8507e6SMatthias Braun //===-- TargetMachine.cpp - General Target Information ---------------------==//
2bb8507e6SMatthias Braun //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6bb8507e6SMatthias Braun //
7bb8507e6SMatthias Braun //===----------------------------------------------------------------------===//
8bb8507e6SMatthias Braun //
9bb8507e6SMatthias Braun // This file describes the general parts of a Target machine.
10bb8507e6SMatthias Braun //
11bb8507e6SMatthias Braun //===----------------------------------------------------------------------===//
12bb8507e6SMatthias Braun
13bb8507e6SMatthias Braun #include "llvm/Target/TargetMachine.h"
14bb8507e6SMatthias Braun #include "llvm/Analysis/TargetTransformInfo.h"
15bb8507e6SMatthias Braun #include "llvm/IR/Function.h"
16bb8507e6SMatthias Braun #include "llvm/IR/GlobalValue.h"
17bb8507e6SMatthias Braun #include "llvm/IR/GlobalVariable.h"
18bb8507e6SMatthias Braun #include "llvm/IR/Mangler.h"
19bb8507e6SMatthias Braun #include "llvm/MC/MCAsmInfo.h"
20bb8507e6SMatthias Braun #include "llvm/MC/MCContext.h"
21bb8507e6SMatthias Braun #include "llvm/MC/MCInstrInfo.h"
22ef736a1cSserge-sans-paille #include "llvm/MC/MCRegisterInfo.h"
23ef736a1cSserge-sans-paille #include "llvm/MC/MCSubtargetInfo.h"
246054e650SDavid Blaikie #include "llvm/Target/TargetLoweringObjectFile.h"
25bb8507e6SMatthias Braun using namespace llvm;
26bb8507e6SMatthias Braun
27bb8507e6SMatthias Braun //---------------------------------------------------------------------------
28bb8507e6SMatthias Braun // TargetMachine Class
29bb8507e6SMatthias Braun //
30bb8507e6SMatthias Braun
TargetMachine(const Target & T,StringRef DataLayoutString,const Triple & TT,StringRef CPU,StringRef FS,const TargetOptions & Options)31bb8507e6SMatthias Braun TargetMachine::TargetMachine(const Target &T, StringRef DataLayoutString,
32bb8507e6SMatthias Braun const Triple &TT, StringRef CPU, StringRef FS,
33bb8507e6SMatthias Braun const TargetOptions &Options)
34adcd0268SBenjamin Kramer : TheTarget(T), DL(DataLayoutString), TargetTriple(TT),
35adcd0268SBenjamin Kramer TargetCPU(std::string(CPU)), TargetFS(std::string(FS)), AsmInfo(nullptr),
36adcd0268SBenjamin Kramer MRI(nullptr), MII(nullptr), STI(nullptr), RequireStructuredCFG(false),
37adcd0268SBenjamin Kramer O0WantsFastISel(false), DefaultOptions(Options), Options(Options) {}
38bb8507e6SMatthias Braun
3910a21625SFangrui Song TargetMachine::~TargetMachine() = default;
40bb8507e6SMatthias Braun
isPositionIndependent() const41bb8507e6SMatthias Braun bool TargetMachine::isPositionIndependent() const {
42bb8507e6SMatthias Braun return getRelocationModel() == Reloc::PIC_;
43bb8507e6SMatthias Braun }
44bb8507e6SMatthias Braun
455f8f34e4SAdrian Prantl /// Reset the target options based on the function's attributes.
46c378e52cSMatt Arsenault /// setFunctionAttributes should have made the raw attribute value consistent
47c378e52cSMatt Arsenault /// with the command line flag if used.
48c378e52cSMatt Arsenault //
49bb8507e6SMatthias Braun // FIXME: This function needs to go away for a number of reasons:
50bb8507e6SMatthias Braun // a) global state on the TargetMachine is terrible in general,
51bb8507e6SMatthias Braun // b) these target options should be passed only on the function
52bb8507e6SMatthias Braun // and not on the TargetMachine (via TargetOptions) at all.
resetTargetOptions(const Function & F) const53bb8507e6SMatthias Braun void TargetMachine::resetTargetOptions(const Function &F) const {
54bb8507e6SMatthias Braun #define RESET_OPTION(X, Y) \
55bb8507e6SMatthias Braun do { \
56d6de1e1aSSerge Guelton Options.X = F.getFnAttribute(Y).getValueAsBool(); \
57bb8507e6SMatthias Braun } while (0)
58bb8507e6SMatthias Braun
59bb8507e6SMatthias Braun RESET_OPTION(UnsafeFPMath, "unsafe-fp-math");
60bb8507e6SMatthias Braun RESET_OPTION(NoInfsFPMath, "no-infs-fp-math");
61bb8507e6SMatthias Braun RESET_OPTION(NoNaNsFPMath, "no-nans-fp-math");
62bb8507e6SMatthias Braun RESET_OPTION(NoSignedZerosFPMath, "no-signed-zeros-fp-math");
63256d2533SMasoud Ataei RESET_OPTION(ApproxFuncFPMath, "approx-func-fp-math");
64bb8507e6SMatthias Braun }
65bb8507e6SMatthias Braun
66bb8507e6SMatthias Braun /// Returns the code generation relocation model. The choices are static, PIC,
67bb8507e6SMatthias Braun /// and dynamic-no-pic.
getRelocationModel() const68bb8507e6SMatthias Braun Reloc::Model TargetMachine::getRelocationModel() const { return RM; }
69bb8507e6SMatthias Braun
70bb8507e6SMatthias Braun /// Get the IR-specified TLS model for Var.
getSelectedTLSModel(const GlobalValue * GV)71bb8507e6SMatthias Braun static TLSModel::Model getSelectedTLSModel(const GlobalValue *GV) {
72bb8507e6SMatthias Braun switch (GV->getThreadLocalMode()) {
73bb8507e6SMatthias Braun case GlobalVariable::NotThreadLocal:
74bb8507e6SMatthias Braun llvm_unreachable("getSelectedTLSModel for non-TLS variable");
75bb8507e6SMatthias Braun break;
76bb8507e6SMatthias Braun case GlobalVariable::GeneralDynamicTLSModel:
77bb8507e6SMatthias Braun return TLSModel::GeneralDynamic;
78bb8507e6SMatthias Braun case GlobalVariable::LocalDynamicTLSModel:
79bb8507e6SMatthias Braun return TLSModel::LocalDynamic;
80bb8507e6SMatthias Braun case GlobalVariable::InitialExecTLSModel:
81bb8507e6SMatthias Braun return TLSModel::InitialExec;
82bb8507e6SMatthias Braun case GlobalVariable::LocalExecTLSModel:
83bb8507e6SMatthias Braun return TLSModel::LocalExec;
84bb8507e6SMatthias Braun }
85bb8507e6SMatthias Braun llvm_unreachable("invalid TLS model");
86bb8507e6SMatthias Braun }
87bb8507e6SMatthias Braun
shouldAssumeDSOLocal(const Module & M,const GlobalValue * GV) const88bb8507e6SMatthias Braun bool TargetMachine::shouldAssumeDSOLocal(const Module &M,
89bb8507e6SMatthias Braun const GlobalValue *GV) const {
9068edf39eSFangrui Song const Triple &TT = getTargetTriple();
9168edf39eSFangrui Song Reloc::Model RM = getRelocationModel();
9263c378d3SRafael Espindola
9363c378d3SRafael Espindola // According to the llvm language reference, we should be able to
9463c378d3SRafael Espindola // just return false in here if we have a GV, as we know it is
9563c378d3SRafael Espindola // dso_preemptable. At this point in time, the various IR producers
9663c378d3SRafael Espindola // have not been transitioned to always produce a dso_local when it
9763c378d3SRafael Espindola // is possible to do so.
98a084c038SFangrui Song //
9963c378d3SRafael Espindola // As a result we still have some logic in here to improve the quality of the
10063c378d3SRafael Espindola // generated code.
101db13a138SFangrui Song if (!GV)
102ba6e15d8SFangrui Song return false;
10368edf39eSFangrui Song
10468edf39eSFangrui Song // If the IR producer requested that this GV be treated as dso local, obey.
10568edf39eSFangrui Song if (GV->isDSOLocal())
10668edf39eSFangrui Song return true;
107bb8507e6SMatthias Braun
108c5ee3123SFangrui Song if (TT.isOSBinFormatCOFF()) {
109bb8507e6SMatthias Braun // DLLImport explicitly marks the GV as external.
11068edf39eSFangrui Song if (GV->hasDLLImportStorageClass())
111bb8507e6SMatthias Braun return false;
112bb8507e6SMatthias Braun
11368df812cSMartin Storsjo // On MinGW, variables that haven't been declared with DLLImport may still
11468df812cSMartin Storsjo // end up automatically imported by the linker. To make this feasible,
11568df812cSMartin Storsjo // don't assume the variables to be DSO local unless we actually know
11668df812cSMartin Storsjo // that for sure. This only has to be done for variables; for functions
11768df812cSMartin Storsjo // the linker can insert thunks for calling functions from another DLL.
118c5ee3123SFangrui Song if (TT.isWindowsGNUEnvironment() && GV->isDeclarationForLinker() &&
119c5ee3123SFangrui Song isa<GlobalVariable>(GV))
12068df812cSMartin Storsjo return false;
12168df812cSMartin Storsjo
122c5ee3123SFangrui Song // Don't mark 'extern_weak' symbols as DSO local. If these symbols remain
123c5ee3123SFangrui Song // unresolved in the link, they can be resolved to zero, which is outside
124c5ee3123SFangrui Song // the current DSO.
125c5ee3123SFangrui Song if (GV->hasExternalWeakLinkage())
1266bf108d7SReid Kleckner return false;
1276bf108d7SReid Kleckner
128bb8507e6SMatthias Braun // Every other GV is local on COFF.
129c5ee3123SFangrui Song return true;
130c5ee3123SFangrui Song }
131c5ee3123SFangrui Song
132aa3519f1SAnirudh Prasad if (TT.isOSBinFormatGOFF())
133aa3519f1SAnirudh Prasad return true;
134aa3519f1SAnirudh Prasad
135c5ee3123SFangrui Song if (TT.isOSBinFormatMachO()) {
136bb8507e6SMatthias Braun if (RM == Reloc::Static)
137bb8507e6SMatthias Braun return true;
13868edf39eSFangrui Song return GV->isStrongDefinitionForLinker();
139bb8507e6SMatthias Braun }
140bb8507e6SMatthias Braun
141c5ee3123SFangrui Song assert(TT.isOSBinFormatELF() || TT.isOSBinFormatWasm() ||
142c5ee3123SFangrui Song TT.isOSBinFormatXCOFF());
143bb8507e6SMatthias Braun return false;
144bb8507e6SMatthias Braun }
145bb8507e6SMatthias Braun
useEmulatedTLS() const1469f9e4681SChih-Hung Hsieh bool TargetMachine::useEmulatedTLS() const {
1479f9e4681SChih-Hung Hsieh // Returns Options.EmulatedTLS if the -emulated-tls or -no-emulated-tls
1489f9e4681SChih-Hung Hsieh // was specified explicitly; otherwise uses target triple to decide default.
1499f9e4681SChih-Hung Hsieh if (Options.ExplicitEmulatedTLS)
1509f9e4681SChih-Hung Hsieh return Options.EmulatedTLS;
1519f9e4681SChih-Hung Hsieh return getTargetTriple().hasDefaultEmulatedTLS();
1529f9e4681SChih-Hung Hsieh }
1539f9e4681SChih-Hung Hsieh
getTLSModel(const GlobalValue * GV) const154bb8507e6SMatthias Braun TLSModel::Model TargetMachine::getTLSModel(const GlobalValue *GV) const {
155bb8507e6SMatthias Braun bool IsPIE = GV->getParent()->getPIELevel() != PIELevel::Default;
156bb8507e6SMatthias Braun Reloc::Model RM = getRelocationModel();
157bb8507e6SMatthias Braun bool IsSharedLibrary = RM == Reloc::PIC_ && !IsPIE;
158bb8507e6SMatthias Braun bool IsLocal = shouldAssumeDSOLocal(*GV->getParent(), GV);
159bb8507e6SMatthias Braun
160bb8507e6SMatthias Braun TLSModel::Model Model;
161bb8507e6SMatthias Braun if (IsSharedLibrary) {
162bb8507e6SMatthias Braun if (IsLocal)
163bb8507e6SMatthias Braun Model = TLSModel::LocalDynamic;
164bb8507e6SMatthias Braun else
165bb8507e6SMatthias Braun Model = TLSModel::GeneralDynamic;
166bb8507e6SMatthias Braun } else {
167bb8507e6SMatthias Braun if (IsLocal)
168bb8507e6SMatthias Braun Model = TLSModel::LocalExec;
169bb8507e6SMatthias Braun else
170bb8507e6SMatthias Braun Model = TLSModel::InitialExec;
171bb8507e6SMatthias Braun }
172bb8507e6SMatthias Braun
173bb8507e6SMatthias Braun // If the user specified a more specific model, use that.
174bb8507e6SMatthias Braun TLSModel::Model SelectedModel = getSelectedTLSModel(GV);
175bb8507e6SMatthias Braun if (SelectedModel > Model)
176bb8507e6SMatthias Braun return SelectedModel;
177bb8507e6SMatthias Braun
178bb8507e6SMatthias Braun return Model;
179bb8507e6SMatthias Braun }
180bb8507e6SMatthias Braun
181bb8507e6SMatthias Braun /// Returns the optimization level: None, Less, Default, or Aggressive.
getOptLevel() const182bb8507e6SMatthias Braun CodeGenOpt::Level TargetMachine::getOptLevel() const { return OptLevel; }
183bb8507e6SMatthias Braun
setOptLevel(CodeGenOpt::Level Level)184bb8507e6SMatthias Braun void TargetMachine::setOptLevel(CodeGenOpt::Level Level) { OptLevel = Level; }
185bb8507e6SMatthias Braun
186*c4b1a63aSJameson Nash TargetTransformInfo
getTargetTransformInfo(const Function & F) const187*c4b1a63aSJameson Nash TargetMachine::getTargetTransformInfo(const Function &F) const {
188bb8507e6SMatthias Braun return TargetTransformInfo(F.getParent()->getDataLayout());
189bb8507e6SMatthias Braun }
190bb8507e6SMatthias Braun
getNameWithPrefix(SmallVectorImpl<char> & Name,const GlobalValue * GV,Mangler & Mang,bool MayAlwaysUsePrivate) const191bb8507e6SMatthias Braun void TargetMachine::getNameWithPrefix(SmallVectorImpl<char> &Name,
192bb8507e6SMatthias Braun const GlobalValue *GV, Mangler &Mang,
193bb8507e6SMatthias Braun bool MayAlwaysUsePrivate) const {
194bb8507e6SMatthias Braun if (MayAlwaysUsePrivate || !GV->hasPrivateLinkage()) {
195bb8507e6SMatthias Braun // Simple case: If GV is not private, it is not important to find out if
196bb8507e6SMatthias Braun // private labels are legal in this case or not.
197bb8507e6SMatthias Braun Mang.getNameWithPrefix(Name, GV, false);
198bb8507e6SMatthias Braun return;
199bb8507e6SMatthias Braun }
200bb8507e6SMatthias Braun const TargetLoweringObjectFile *TLOF = getObjFileLowering();
201bb8507e6SMatthias Braun TLOF->getNameWithPrefix(Name, GV, *this);
202bb8507e6SMatthias Braun }
203bb8507e6SMatthias Braun
getSymbol(const GlobalValue * GV) const204bb8507e6SMatthias Braun MCSymbol *TargetMachine::getSymbol(const GlobalValue *GV) const {
205bb8507e6SMatthias Braun const TargetLoweringObjectFile *TLOF = getObjFileLowering();
20677618cc2Sjasonliu // XCOFF symbols could have special naming convention.
20777618cc2Sjasonliu if (MCSymbol *TargetSymbol = TLOF->getTargetSymbol(GV, *this))
20877618cc2Sjasonliu return TargetSymbol;
20977618cc2Sjasonliu
210bb8507e6SMatthias Braun SmallString<128> NameStr;
211bb8507e6SMatthias Braun getNameWithPrefix(NameStr, GV, TLOF->getMangler());
212bb8507e6SMatthias Braun return TLOF->getContext().getOrCreateSymbol(NameStr);
213bb8507e6SMatthias Braun }
21426d11ca4SSanjoy Das
getTargetIRAnalysis() const215*c4b1a63aSJameson Nash TargetIRAnalysis TargetMachine::getTargetIRAnalysis() const {
21626d11ca4SSanjoy Das // Since Analysis can't depend on Target, use a std::function to invert the
21726d11ca4SSanjoy Das // dependency.
21826d11ca4SSanjoy Das return TargetIRAnalysis(
21926d11ca4SSanjoy Das [this](const Function &F) { return this->getTargetTransformInfo(F); });
22026d11ca4SSanjoy Das }
22134b60d8aSFangrui Song
parseBinutilsVersion(StringRef Version)22234b60d8aSFangrui Song std::pair<int, int> TargetMachine::parseBinutilsVersion(StringRef Version) {
22334b60d8aSFangrui Song if (Version == "none")
22434b60d8aSFangrui Song return {INT_MAX, INT_MAX}; // Make binutilsIsAtLeast() return true.
22534b60d8aSFangrui Song std::pair<int, int> Ret;
22634b60d8aSFangrui Song if (!Version.consumeInteger(10, Ret.first) && Version.consume_front("."))
22734b60d8aSFangrui Song Version.consumeInteger(10, Ret.second);
22834b60d8aSFangrui Song return Ret;
22934b60d8aSFangrui Song }
230