comparison lib/Target/TargetMachine.cpp @ 148:63bd29f05246

merged
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Wed, 14 Aug 2019 19:46:37 +0900
parents c2174574ed3a
children
comparison
equal deleted inserted replaced
146:3fc4d5c3e21e 148:63bd29f05246
1 //===-- TargetMachine.cpp - General Target Information ---------------------==// 1 //===-- TargetMachine.cpp - General Target Information ---------------------==//
2 // 2 //
3 // The LLVM Compiler Infrastructure 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // 4 // See https://llvm.org/LICENSE.txt for license information.
5 // This file is distributed under the University of Illinois Open Source 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 // License. See LICENSE.TXT for details.
7 // 6 //
8 //===----------------------------------------------------------------------===// 7 //===----------------------------------------------------------------------===//
9 // 8 //
10 // This file describes the general parts of a Target machine. 9 // This file describes the general parts of a Target machine.
11 // 10 //
12 //===----------------------------------------------------------------------===// 11 //===----------------------------------------------------------------------===//
13 12
14 #include "llvm/Target/TargetMachine.h" 13 #include "llvm/Target/TargetMachine.h"
15 #include "llvm/Analysis/TargetTransformInfo.h" 14 #include "llvm/Analysis/TargetTransformInfo.h"
16 #include "llvm/CodeGen/TargetLoweringObjectFile.h"
17 #include "llvm/CodeGen/TargetSubtargetInfo.h"
18 #include "llvm/IR/Function.h" 15 #include "llvm/IR/Function.h"
19 #include "llvm/IR/GlobalAlias.h" 16 #include "llvm/IR/GlobalAlias.h"
20 #include "llvm/IR/GlobalValue.h" 17 #include "llvm/IR/GlobalValue.h"
21 #include "llvm/IR/GlobalVariable.h" 18 #include "llvm/IR/GlobalVariable.h"
22 #include "llvm/IR/LegacyPassManager.h" 19 #include "llvm/IR/LegacyPassManager.h"
25 #include "llvm/MC/MCContext.h" 22 #include "llvm/MC/MCContext.h"
26 #include "llvm/MC/MCInstrInfo.h" 23 #include "llvm/MC/MCInstrInfo.h"
27 #include "llvm/MC/MCSectionMachO.h" 24 #include "llvm/MC/MCSectionMachO.h"
28 #include "llvm/MC/MCTargetOptions.h" 25 #include "llvm/MC/MCTargetOptions.h"
29 #include "llvm/MC/SectionKind.h" 26 #include "llvm/MC/SectionKind.h"
27 #include "llvm/Target/TargetLoweringObjectFile.h"
30 using namespace llvm; 28 using namespace llvm;
31 29
32 //--------------------------------------------------------------------------- 30 //---------------------------------------------------------------------------
33 // TargetMachine Class 31 // TargetMachine Class
34 // 32 //
39 : TheTarget(T), DL(DataLayoutString), TargetTriple(TT), TargetCPU(CPU), 37 : TheTarget(T), DL(DataLayoutString), TargetTriple(TT), TargetCPU(CPU),
40 TargetFS(FS), AsmInfo(nullptr), MRI(nullptr), MII(nullptr), STI(nullptr), 38 TargetFS(FS), AsmInfo(nullptr), MRI(nullptr), MII(nullptr), STI(nullptr),
41 RequireStructuredCFG(false), DefaultOptions(Options), Options(Options) { 39 RequireStructuredCFG(false), DefaultOptions(Options), Options(Options) {
42 } 40 }
43 41
44 TargetMachine::~TargetMachine() { 42 TargetMachine::~TargetMachine() = default;
45 delete AsmInfo;
46 delete MRI;
47 delete MII;
48 delete STI;
49 }
50 43
51 bool TargetMachine::isPositionIndependent() const { 44 bool TargetMachine::isPositionIndependent() const {
52 return getRelocationModel() == Reloc::PIC_; 45 return getRelocationModel() == Reloc::PIC_;
53 } 46 }
54 47
55 /// \brief Reset the target options based on the function's attributes. 48 /// Reset the target options based on the function's attributes.
56 // FIXME: This function needs to go away for a number of reasons: 49 // FIXME: This function needs to go away for a number of reasons:
57 // a) global state on the TargetMachine is terrible in general, 50 // a) global state on the TargetMachine is terrible in general,
58 // b) these target options should be passed only on the function 51 // b) these target options should be passed only on the function
59 // and not on the TargetMachine (via TargetOptions) at all. 52 // and not on the TargetMachine (via TargetOptions) at all.
60 void TargetMachine::resetTargetOptions(const Function &F) const { 53 void TargetMachine::resetTargetOptions(const Function &F) const {
68 61
69 RESET_OPTION(UnsafeFPMath, "unsafe-fp-math"); 62 RESET_OPTION(UnsafeFPMath, "unsafe-fp-math");
70 RESET_OPTION(NoInfsFPMath, "no-infs-fp-math"); 63 RESET_OPTION(NoInfsFPMath, "no-infs-fp-math");
71 RESET_OPTION(NoNaNsFPMath, "no-nans-fp-math"); 64 RESET_OPTION(NoNaNsFPMath, "no-nans-fp-math");
72 RESET_OPTION(NoSignedZerosFPMath, "no-signed-zeros-fp-math"); 65 RESET_OPTION(NoSignedZerosFPMath, "no-signed-zeros-fp-math");
73 RESET_OPTION(NoTrappingFPMath, "no-trapping-math");
74
75 StringRef Denormal =
76 F.getFnAttribute("denormal-fp-math").getValueAsString();
77 if (Denormal == "ieee")
78 Options.FPDenormalMode = FPDenormal::IEEE;
79 else if (Denormal == "preserve-sign")
80 Options.FPDenormalMode = FPDenormal::PreserveSign;
81 else if (Denormal == "positive-zero")
82 Options.FPDenormalMode = FPDenormal::PositiveZero;
83 else
84 Options.FPDenormalMode = DefaultOptions.FPDenormalMode;
85 } 66 }
86 67
87 /// Returns the code generation relocation model. The choices are static, PIC, 68 /// Returns the code generation relocation model. The choices are static, PIC,
88 /// and dynamic-no-pic. 69 /// and dynamic-no-pic.
89 Reloc::Model TargetMachine::getRelocationModel() const { return RM; } 70 Reloc::Model TargetMachine::getRelocationModel() const { return RM; }
114 const GlobalValue *GV) const { 95 const GlobalValue *GV) const {
115 // If the IR producer requested that this GV be treated as dso local, obey. 96 // If the IR producer requested that this GV be treated as dso local, obey.
116 if (GV && GV->isDSOLocal()) 97 if (GV && GV->isDSOLocal())
117 return true; 98 return true;
118 99
119 // According to the llvm language reference, we should be able to just return 100 // If we are not supossed to use a PLT, we cannot assume that intrinsics are
120 // false in here if we have a GV, as we know it is dso_preemptable. 101 // local since the linker can convert some direct access to access via plt.
121 // At this point in time, the various IR producers have not been transitioned 102 if (M.getRtLibUseGOT() && !GV)
122 // to always produce a dso_local when it is possible to do so. As a result we 103 return false;
123 // still have some pre-dso_local logic in here to improve the quality of the 104
124 // generated code: 105 // According to the llvm language reference, we should be able to
106 // just return false in here if we have a GV, as we know it is
107 // dso_preemptable. At this point in time, the various IR producers
108 // have not been transitioned to always produce a dso_local when it
109 // is possible to do so.
110 // In the case of intrinsics, GV is null and there is nowhere to put
111 // dso_local. Returning false for those will produce worse code in some
112 // architectures. For example, on x86 the caller has to set ebx before calling
113 // a plt.
114 // As a result we still have some logic in here to improve the quality of the
115 // generated code.
116 // FIXME: Add a module level metadata for whether intrinsics should be assumed
117 // local.
125 118
126 Reloc::Model RM = getRelocationModel(); 119 Reloc::Model RM = getRelocationModel();
127 const Triple &TT = getTargetTriple(); 120 const Triple &TT = getTargetTriple();
128 121
129 // DLLImport explicitly marks the GV as external. 122 // DLLImport explicitly marks the GV as external.
130 if (GV && GV->hasDLLImportStorageClass()) 123 if (GV && GV->hasDLLImportStorageClass())
131 return false; 124 return false;
132 125
126 // On MinGW, variables that haven't been declared with DLLImport may still
127 // end up automatically imported by the linker. To make this feasible,
128 // don't assume the variables to be DSO local unless we actually know
129 // that for sure. This only has to be done for variables; for functions
130 // the linker can insert thunks for calling functions from another DLL.
131 if (TT.isWindowsGNUEnvironment() && GV && GV->isDeclarationForLinker() &&
132 isa<GlobalVariable>(GV))
133 return false;
134
135 // On COFF, don't mark 'extern_weak' symbols as DSO local. If these symbols
136 // remain unresolved in the link, they can be resolved to zero, which is
137 // outside the current DSO.
138 if (TT.isOSBinFormatCOFF() && GV && GV->hasExternalWeakLinkage())
139 return false;
140
133 // Every other GV is local on COFF. 141 // Every other GV is local on COFF.
134 // Make an exception for windows OS in the triple: Some firmwares builds use 142 // Make an exception for windows OS in the triple: Some firmware builds use
135 // *-win32-macho triples. This (accidentally?) produced windows relocations 143 // *-win32-macho triples. This (accidentally?) produced windows relocations
136 // without GOT tables in older clang versions; Keep this behaviour. 144 // without GOT tables in older clang versions; Keep this behaviour.
137 if (TT.isOSBinFormatCOFF() || (TT.isOSWindows() && TT.isOSBinFormatMachO())) 145 if (TT.isOSBinFormatCOFF() || (TT.isOSWindows() && TT.isOSBinFormatMachO()))
138 return true; 146 return true;
139 147
151 if (RM == Reloc::Static) 159 if (RM == Reloc::Static)
152 return true; 160 return true;
153 return GV && GV->isStrongDefinitionForLinker(); 161 return GV && GV->isStrongDefinitionForLinker();
154 } 162 }
155 163
156 assert(TT.isOSBinFormatELF()); 164 // Due to the AIX linkage model, any global with default visibility is
165 // considered non-local.
166 if (TT.isOSBinFormatXCOFF())
167 return false;
168
169 assert(TT.isOSBinFormatELF() || TT.isOSBinFormatWasm());
157 assert(RM != Reloc::DynamicNoPIC); 170 assert(RM != Reloc::DynamicNoPIC);
158 171
159 bool IsExecutable = 172 bool IsExecutable =
160 RM == Reloc::Static || M.getPIELevel() != PIELevel::Default; 173 RM == Reloc::Static || M.getPIELevel() != PIELevel::Default;
161 if (IsExecutable) { 174 if (IsExecutable) {
170 if (F && F->hasFnAttribute(Attribute::NonLazyBind)) 183 if (F && F->hasFnAttribute(Attribute::NonLazyBind))
171 return false; 184 return false;
172 185
173 bool IsTLS = GV && GV->isThreadLocal(); 186 bool IsTLS = GV && GV->isThreadLocal();
174 bool IsAccessViaCopyRelocs = 187 bool IsAccessViaCopyRelocs =
175 Options.MCOptions.MCPIECopyRelocations && GV && isa<GlobalVariable>(GV); 188 GV && Options.MCOptions.MCPIECopyRelocations && isa<GlobalVariable>(GV);
176 Triple::ArchType Arch = TT.getArch(); 189 Triple::ArchType Arch = TT.getArch();
177 bool IsPPC = 190 bool IsPPC =
178 Arch == Triple::ppc || Arch == Triple::ppc64 || Arch == Triple::ppc64le; 191 Arch == Triple::ppc || Arch == Triple::ppc64 || Arch == Triple::ppc64le;
179 // Check if we can use copy relocations. PowerPC has no copy relocations. 192 // Check if we can use copy relocations. PowerPC has no copy relocations.
180 if (!IsTLS && !IsPPC && (RM == Reloc::Static || IsAccessViaCopyRelocs)) 193 if (!IsTLS && !IsPPC && (RM == Reloc::Static || IsAccessViaCopyRelocs))
181 return true; 194 return true;
182 } 195 }
183 196
184 // ELF supports preemption of other symbols. 197 // ELF & wasm support preemption of other symbols.
185 return false; 198 return false;
199 }
200
201 bool TargetMachine::useEmulatedTLS() const {
202 // Returns Options.EmulatedTLS if the -emulated-tls or -no-emulated-tls
203 // was specified explicitly; otherwise uses target triple to decide default.
204 if (Options.ExplicitEmulatedTLS)
205 return Options.EmulatedTLS;
206 return getTargetTriple().hasDefaultEmulatedTLS();
186 } 207 }
187 208
188 TLSModel::Model TargetMachine::getTLSModel(const GlobalValue *GV) const { 209 TLSModel::Model TargetMachine::getTLSModel(const GlobalValue *GV) const {
189 bool IsPIE = GV->getParent()->getPIELevel() != PIELevel::Default; 210 bool IsPIE = GV->getParent()->getPIELevel() != PIELevel::Default;
190 Reloc::Model RM = getRelocationModel(); 211 Reloc::Model RM = getRelocationModel();