Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/Target/TargetMachine.cpp
Line
Count
Source (jump to first uncovered line)
1
//===-- TargetMachine.cpp - General Target Information ---------------------==//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
//
9
// This file describes the general parts of a Target machine.
10
//
11
//===----------------------------------------------------------------------===//
12
13
#include "llvm/Target/TargetMachine.h"
14
#include "llvm/Analysis/TargetTransformInfo.h"
15
#include "llvm/IR/Function.h"
16
#include "llvm/IR/GlobalAlias.h"
17
#include "llvm/IR/GlobalValue.h"
18
#include "llvm/IR/GlobalVariable.h"
19
#include "llvm/IR/LegacyPassManager.h"
20
#include "llvm/IR/Mangler.h"
21
#include "llvm/MC/MCAsmInfo.h"
22
#include "llvm/MC/MCContext.h"
23
#include "llvm/MC/MCInstrInfo.h"
24
#include "llvm/MC/MCSectionMachO.h"
25
#include "llvm/MC/MCTargetOptions.h"
26
#include "llvm/MC/SectionKind.h"
27
#include "llvm/Target/TargetLoweringObjectFile.h"
28
using namespace llvm;
29
30
//---------------------------------------------------------------------------
31
// TargetMachine Class
32
//
33
34
TargetMachine::TargetMachine(const Target &T, StringRef DataLayoutString,
35
                             const Triple &TT, StringRef CPU, StringRef FS,
36
                             const TargetOptions &Options)
37
    : TheTarget(T), DL(DataLayoutString), TargetTriple(TT), TargetCPU(CPU),
38
      TargetFS(FS), AsmInfo(nullptr), MRI(nullptr), MII(nullptr), STI(nullptr),
39
52.4k
      RequireStructuredCFG(false), DefaultOptions(Options), Options(Options) {
40
52.4k
}
41
42
39.6k
TargetMachine::~TargetMachine() = default;
43
44
1.24M
bool TargetMachine::isPositionIndependent() const {
45
1.24M
  return getRelocationModel() == Reloc::PIC_;
46
1.24M
}
47
48
/// Reset the target options based on the function's attributes.
49
// FIXME: This function needs to go away for a number of reasons:
50
// a) global state on the TargetMachine is terrible in general,
51
// b) these target options should be passed only on the function
52
//    and not on the TargetMachine (via TargetOptions) at all.
53
405k
void TargetMachine::resetTargetOptions(const Function &F) const {
54
405k
#define RESET_OPTION(X, Y)                                                     \
55
1.62M
  do {                                                                         \
56
1.62M
    if (F.hasFnAttribute(Y))                                                   \
57
1.62M
      
Options.X = (F.getFnAttribute(Y).getValueAsString() == "true")271k
; \
58
1.62M
    else                                                                       \
59
1.62M
      
Options.X = DefaultOptions.X1.34M
; \
60
1.62M
  } while (0)
61
405k
62
405k
  RESET_OPTION(UnsafeFPMath, "unsafe-fp-math");
63
405k
  RESET_OPTION(NoInfsFPMath, "no-infs-fp-math");
64
405k
  RESET_OPTION(NoNaNsFPMath, "no-nans-fp-math");
65
405k
  RESET_OPTION(NoSignedZerosFPMath, "no-signed-zeros-fp-math");
66
405k
}
67
68
/// Returns the code generation relocation model. The choices are static, PIC,
69
/// and dynamic-no-pic.
70
2.22M
Reloc::Model TargetMachine::getRelocationModel() const { return RM; }
71
72
/// Returns the code model. The choices are small, kernel, medium, large, and
73
/// target default.
74
11.5M
CodeModel::Model TargetMachine::getCodeModel() const { return CMModel; }
75
76
/// Get the IR-specified TLS model for Var.
77
632
static TLSModel::Model getSelectedTLSModel(const GlobalValue *GV) {
78
632
  switch (GV->getThreadLocalMode()) {
79
632
  case GlobalVariable::NotThreadLocal:
80
0
    llvm_unreachable("getSelectedTLSModel for non-TLS variable");
81
632
    
break0
;
82
632
  case GlobalVariable::GeneralDynamicTLSModel:
83
426
    return TLSModel::GeneralDynamic;
84
632
  case GlobalVariable::LocalDynamicTLSModel:
85
91
    return TLSModel::LocalDynamic;
86
632
  case GlobalVariable::InitialExecTLSModel:
87
67
    return TLSModel::InitialExec;
88
632
  case GlobalVariable::LocalExecTLSModel:
89
48
    return TLSModel::LocalExec;
90
0
  }
91
0
  llvm_unreachable("invalid TLS model");
92
0
}
93
94
bool TargetMachine::shouldAssumeDSOLocal(const Module &M,
95
1.82M
                                         const GlobalValue *GV) const {
96
1.82M
  // If the IR producer requested that this GV be treated as dso local, obey.
97
1.82M
  if (GV && 
GV->isDSOLocal()1.80M
)
98
1.18M
    return true;
99
639k
100
639k
  // If we are not supossed to use a PLT, we cannot assume that intrinsics are
101
639k
  // local since the linker can convert some direct access to access via plt.
102
639k
  if (M.getRtLibUseGOT() && 
!GV14
)
103
4
    return false;
104
639k
105
639k
  // According to the llvm language reference, we should be able to
106
639k
  // just return false in here if we have a GV, as we know it is
107
639k
  // dso_preemptable.  At this point in time, the various IR producers
108
639k
  // have not been transitioned to always produce a dso_local when it
109
639k
  // is possible to do so.
110
639k
  // In the case of intrinsics, GV is null and there is nowhere to put
111
639k
  // dso_local. Returning false for those will produce worse code in some
112
639k
  // architectures. For example, on x86 the caller has to set ebx before calling
113
639k
  // a plt.
114
639k
  // As a result we still have some logic in here to improve the quality of the
115
639k
  // generated code.
116
639k
  // FIXME: Add a module level metadata for whether intrinsics should be assumed
117
639k
  // local.
118
639k
119
639k
  Reloc::Model RM = getRelocationModel();
120
639k
  const Triple &TT = getTargetTriple();
121
639k
122
639k
  // DLLImport explicitly marks the GV as external.
123
639k
  if (GV && 
GV->hasDLLImportStorageClass()627k
)
124
97
    return false;
125
638k
126
638k
  // On MinGW, variables that haven't been declared with DLLImport may still
127
638k
  // end up automatically imported by the linker. To make this feasible,
128
638k
  // don't assume the variables to be DSO local unless we actually know
129
638k
  // that for sure. This only has to be done for variables; for functions
130
638k
  // the linker can insert thunks for calling functions from another DLL.
131
638k
  if (TT.isWindowsGNUEnvironment() && 
GV578
&&
GV->isDeclarationForLinker()463
&&
132
638k
      
isa<GlobalVariable>(GV)418
)
133
154
    return false;
134
638k
135
638k
  // On COFF, don't mark 'extern_weak' symbols as DSO local. If these symbols
136
638k
  // remain unresolved in the link, they can be resolved to zero, which is
137
638k
  // outside the current DSO.
138
638k
  if (TT.isOSBinFormatCOFF() && 
GV3.12k
&&
GV->hasExternalWeakLinkage()2.79k
)
139
5
    return false;
140
638k
141
638k
  // Every other GV is local on COFF.
142
638k
  // Make an exception for windows OS in the triple: Some firmware builds use
143
638k
  // *-win32-macho triples. This (accidentally?) produced windows relocations
144
638k
  // without GOT tables in older clang versions; Keep this behaviour.
145
638k
  if (TT.isOSBinFormatCOFF() || 
(635k
TT.isOSWindows()635k
&&
TT.isOSBinFormatMachO()10
))
146
3.13k
    return true;
147
635k
148
635k
  // Most PIC code sequences that assume that a symbol is local cannot
149
635k
  // produce a 0 if it turns out the symbol is undefined. While this
150
635k
  // is ABI and relocation depended, it seems worth it to handle it
151
635k
  // here.
152
635k
  if (GV && 
isPositionIndependent()624k
&&
GV->hasExternalWeakLinkage()582k
)
153
766
    return false;
154
634k
155
634k
  if (GV && 
!GV->hasDefaultVisibility()623k
)
156
8
    return true;
157
634k
158
634k
  if (TT.isOSBinFormatMachO()) {
159
585k
    if (RM == Reloc::Static)
160
2.61k
      return true;
161
583k
    return GV && 
GV->isStrongDefinitionForLinker()579k
;
162
583k
  }
163
49.1k
164
49.1k
  // Due to the AIX linkage model, any global with default visibility is
165
49.1k
  // considered non-local.
166
49.1k
  if (TT.isOSBinFormatXCOFF())
167
30
    return false;
168
49.1k
169
49.1k
  assert(TT.isOSBinFormatELF() || TT.isOSBinFormatWasm());
170
49.1k
  assert(RM != Reloc::DynamicNoPIC);
171
49.1k
172
49.1k
  bool IsExecutable =
173
49.1k
      RM == Reloc::Static || 
M.getPIELevel() != PIELevel::Default6.91k
;
174
49.1k
  if (IsExecutable) {
175
42.4k
    // If the symbol is defined, it cannot be preempted.
176
42.4k
    if (GV && 
!GV->isDeclarationForLinker()36.4k
)
177
21.6k
      return true;
178
20.7k
179
20.7k
    // A symbol marked nonlazybind should not be accessed with a plt. If the
180
20.7k
    // symbol turns out to be external, the linker will convert a direct
181
20.7k
    // access to an access via the plt, so don't assume it is local.
182
20.7k
    const Function *F = dyn_cast_or_null<Function>(GV);
183
20.7k
    if (F && 
F->hasFnAttribute(Attribute::NonLazyBind)8.56k
)
184
11
      return false;
185
20.7k
186
20.7k
    bool IsTLS = GV && 
GV->isThreadLocal()14.8k
;
187
20.7k
    bool IsAccessViaCopyRelocs =
188
20.7k
        GV && 
Options.MCOptions.MCPIECopyRelocations14.8k
&&
isa<GlobalVariable>(GV)28
;
189
20.7k
    Triple::ArchType Arch = TT.getArch();
190
20.7k
    bool IsPPC =
191
20.7k
        Arch == Triple::ppc || 
Arch == Triple::ppc6420.4k
||
Arch == Triple::ppc64le20.3k
;
192
20.7k
    // Check if we can use copy relocations. PowerPC has no copy relocations.
193
20.7k
    if (!IsTLS && 
!IsPPC20.6k
&&
(18.5k
RM == Reloc::Static18.5k
||
IsAccessViaCopyRelocs131
))
194
18.4k
      return true;
195
8.97k
  }
196
8.97k
197
8.97k
  // ELF & wasm support preemption of other symbols.
198
8.97k
  return false;
199
8.97k
}
200
201
488k
bool TargetMachine::useEmulatedTLS() const {
202
488k
  // Returns Options.EmulatedTLS if the -emulated-tls or -no-emulated-tls
203
488k
  // was specified explicitly; otherwise uses target triple to decide default.
204
488k
  if (Options.ExplicitEmulatedTLS)
205
1.54k
    return Options.EmulatedTLS;
206
486k
  return getTargetTriple().hasDefaultEmulatedTLS();
207
486k
}
208
209
632
TLSModel::Model TargetMachine::getTLSModel(const GlobalValue *GV) const {
210
632
  bool IsPIE = GV->getParent()->getPIELevel() != PIELevel::Default;
211
632
  Reloc::Model RM = getRelocationModel();
212
632
  bool IsSharedLibrary = RM == Reloc::PIC_ && 
!IsPIE330
;
213
632
  bool IsLocal = shouldAssumeDSOLocal(*GV->getParent(), GV);
214
632
215
632
  TLSModel::Model Model;
216
632
  if (IsSharedLibrary) {
217
307
    if (IsLocal)
218
66
      Model = TLSModel::LocalDynamic;
219
241
    else
220
241
      Model = TLSModel::GeneralDynamic;
221
325
  } else {
222
325
    if (IsLocal)
223
192
      Model = TLSModel::LocalExec;
224
133
    else
225
133
      Model = TLSModel::InitialExec;
226
325
  }
227
632
228
632
  // If the user specified a more specific model, use that.
229
632
  TLSModel::Model SelectedModel = getSelectedTLSModel(GV);
230
632
  if (SelectedModel > Model)
231
109
    return SelectedModel;
232
523
233
523
  return Model;
234
523
}
235
236
/// Returns the optimization level: None, Less, Default, or Aggressive.
237
11.7M
CodeGenOpt::Level TargetMachine::getOptLevel() const { return OptLevel; }
238
239
540
void TargetMachine::setOptLevel(CodeGenOpt::Level Level) { OptLevel = Level; }
240
241
0
TargetTransformInfo TargetMachine::getTargetTransformInfo(const Function &F) {
242
0
  return TargetTransformInfo(F.getParent()->getDataLayout());
243
0
}
244
245
void TargetMachine::getNameWithPrefix(SmallVectorImpl<char> &Name,
246
                                      const GlobalValue *GV, Mangler &Mang,
247
4.26M
                                      bool MayAlwaysUsePrivate) const {
248
4.26M
  if (MayAlwaysUsePrivate || 
!GV->hasPrivateLinkage()4.26M
) {
249
2.63M
    // Simple case: If GV is not private, it is not important to find out if
250
2.63M
    // private labels are legal in this case or not.
251
2.63M
    Mang.getNameWithPrefix(Name, GV, false);
252
2.63M
    return;
253
2.63M
  }
254
1.63M
  const TargetLoweringObjectFile *TLOF = getObjFileLowering();
255
1.63M
  TLOF->getNameWithPrefix(Name, GV, *this);
256
1.63M
}
257
258
4.01M
MCSymbol *TargetMachine::getSymbol(const GlobalValue *GV) const {
259
4.01M
  const TargetLoweringObjectFile *TLOF = getObjFileLowering();
260
4.01M
  SmallString<128> NameStr;
261
4.01M
  getNameWithPrefix(NameStr, GV, TLOF->getMangler());
262
4.01M
  return TLOF->getContext().getOrCreateSymbol(NameStr);
263
4.01M
}
264
265
105k
TargetIRAnalysis TargetMachine::getTargetIRAnalysis() {
266
105k
  // Since Analysis can't depend on Target, use a std::function to invert the
267
105k
  // dependency.
268
105k
  return TargetIRAnalysis(
269
15.7M
      [this](const Function &F) { return this->getTargetTransformInfo(F); });
270
105k
}