Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/include/llvm/Analysis/TargetLibraryInfo.h
Line
Count
Source (jump to first uncovered line)
1
//===-- TargetLibraryInfo.h - Library information ---------------*- C++ -*-===//
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
#ifndef LLVM_ANALYSIS_TARGETLIBRARYINFO_H
10
#define LLVM_ANALYSIS_TARGETLIBRARYINFO_H
11
12
#include "llvm/ADT/DenseMap.h"
13
#include "llvm/ADT/Optional.h"
14
#include "llvm/ADT/Triple.h"
15
#include "llvm/IR/CallSite.h"
16
#include "llvm/IR/Function.h"
17
#include "llvm/IR/Module.h"
18
#include "llvm/IR/PassManager.h"
19
#include "llvm/Pass.h"
20
21
namespace llvm {
22
template <typename T> class ArrayRef;
23
24
/// Describes a possible vectorization of a function.
25
/// Function 'VectorFnName' is equivalent to 'ScalarFnName' vectorized
26
/// by a factor 'VectorizationFactor'.
27
struct VecDesc {
28
  StringRef ScalarFnName;
29
  StringRef VectorFnName;
30
  unsigned VectorizationFactor;
31
};
32
33
  enum LibFunc {
34
#define TLI_DEFINE_ENUM
35
#include "llvm/Analysis/TargetLibraryInfo.def"
36
37
    NumLibFuncs
38
  };
39
40
/// Implementation of the target library information.
41
///
42
/// This class constructs tables that hold the target library information and
43
/// make it available. However, it is somewhat expensive to compute and only
44
/// depends on the triple. So users typically interact with the \c
45
/// TargetLibraryInfo wrapper below.
46
class TargetLibraryInfoImpl {
47
  friend class TargetLibraryInfo;
48
49
  unsigned char AvailableArray[(NumLibFuncs+3)/4];
50
  llvm::DenseMap<unsigned, std::string> CustomNames;
51
  static StringRef const StandardNames[NumLibFuncs];
52
  bool ShouldExtI32Param, ShouldExtI32Return, ShouldSignExtI32Param;
53
54
  enum AvailabilityState {
55
    StandardName = 3, // (memset to all ones)
56
    CustomName = 1,
57
    Unavailable = 0  // (memset to all zeros)
58
  };
59
8.46M
  void setState(LibFunc F, AvailabilityState State) {
60
8.46M
    AvailableArray[F/4] &= ~(3 << 2*(F&3));
61
8.46M
    AvailableArray[F/4] |= State << 2*(F&3);
62
8.46M
  }
63
27.5M
  AvailabilityState getState(LibFunc F) const {
64
27.5M
    return static_cast<AvailabilityState>((AvailableArray[F/4] >> 2*(F&3)) & 3);
65
27.5M
  }
66
67
  /// Vectorization descriptors - sorted by ScalarFnName.
68
  std::vector<VecDesc> VectorDescs;
69
  /// Scalarization descriptors - same content as VectorDescs but sorted based
70
  /// on VectorFnName rather than ScalarFnName.
71
  std::vector<VecDesc> ScalarDescs;
72
73
  /// Return true if the function type FTy is valid for the library function
74
  /// F, regardless of whether the function is available.
75
  bool isValidProtoForLibFunc(const FunctionType &FTy, LibFunc F,
76
                              const DataLayout *DL) const;
77
78
public:
79
  /// List of known vector-functions libraries.
80
  ///
81
  /// The vector-functions library defines, which functions are vectorizable
82
  /// and with which factor. The library can be specified by either frontend,
83
  /// or a commandline option, and then used by
84
  /// addVectorizableFunctionsFromVecLib for filling up the tables of
85
  /// vectorizable functions.
86
  enum VectorLibrary {
87
    NoLibrary,  // Don't use any vector library.
88
    Accelerate, // Use Accelerate framework.
89
    MASSV,      // IBM MASS vector library.
90
    SVML        // Intel short vector math library.
91
  };
92
93
  TargetLibraryInfoImpl();
94
  explicit TargetLibraryInfoImpl(const Triple &T);
95
96
  // Provide value semantics.
97
  TargetLibraryInfoImpl(const TargetLibraryInfoImpl &TLI);
98
  TargetLibraryInfoImpl(TargetLibraryInfoImpl &&TLI);
99
  TargetLibraryInfoImpl &operator=(const TargetLibraryInfoImpl &TLI);
100
  TargetLibraryInfoImpl &operator=(TargetLibraryInfoImpl &&TLI);
101
102
  /// Searches for a particular function name.
103
  ///
104
  /// If it is one of the known library functions, return true and set F to the
105
  /// corresponding value.
106
  bool getLibFunc(StringRef funcName, LibFunc &F) const;
107
108
  /// Searches for a particular function name, also checking that its type is
109
  /// valid for the library function matching that name.
110
  ///
111
  /// If it is one of the known library functions, return true and set F to the
112
  /// corresponding value.
113
  bool getLibFunc(const Function &FDecl, LibFunc &F) const;
114
115
  /// Forces a function to be marked as unavailable.
116
8.28M
  void setUnavailable(LibFunc F) {
117
8.28M
    setState(F, Unavailable);
118
8.28M
  }
119
120
  /// Forces a function to be marked as available.
121
135k
  void setAvailable(LibFunc F) {
122
135k
    setState(F, StandardName);
123
135k
  }
124
125
  /// Forces a function to be marked as available and provide an alternate name
126
  /// that must be used.
127
39.3k
  void setAvailableWithName(LibFunc F, StringRef Name) {
128
39.3k
    if (
StandardNames[F] != Name39.3k
) {
129
39.3k
      setState(F, CustomName);
130
39.3k
      CustomNames[F] = Name;
131
39.3k
      assert(CustomNames.find(F) != CustomNames.end());
132
18.4E
    } else {
133
18.4E
      setState(F, StandardName);
134
18.4E
    }
135
39.3k
  }
136
137
  /// Disables all builtins.
138
  ///
139
  /// This can be used for options like -fno-builtin.
140
  void disableAllFunctions();
141
142
  /// Add a set of scalar -> vector mappings, queryable via
143
  /// getVectorizedFunction and getScalarizedFunction.
144
  void addVectorizableFunctions(ArrayRef<VecDesc> Fns);
145
146
  /// Calls addVectorizableFunctions with a known preset of functions for the
147
  /// given vector library.
148
  void addVectorizableFunctionsFromVecLib(enum VectorLibrary VecLib);
149
150
  /// Return true if the function F has a vector equivalent with vectorization
151
  /// factor VF.
152
1.57k
  bool isFunctionVectorizable(StringRef F, unsigned VF) const {
153
1.57k
    return !getVectorizedFunction(F, VF).empty();
154
1.57k
  }
155
156
  /// Return true if the function F has a vector equivalent with any
157
  /// vectorization factor.
158
  bool isFunctionVectorizable(StringRef F) const;
159
160
  /// Return the name of the equivalent of F, vectorized with factor VF. If no
161
  /// such mapping exists, return the empty string.
162
  StringRef getVectorizedFunction(StringRef F, unsigned VF) const;
163
164
  /// Return true if the function F has a scalar equivalent, and set VF to be
165
  /// the vectorization factor.
166
0
  bool isFunctionScalarizable(StringRef F, unsigned &VF) const {
167
0
    return !getScalarizedFunction(F, VF).empty();
168
0
  }
169
170
  /// Return the name of the equivalent of F, scalarized. If no such mapping
171
  /// exists, return the empty string.
172
  ///
173
  /// Set VF to the vectorization factor.
174
  StringRef getScalarizedFunction(StringRef F, unsigned &VF) const;
175
176
  /// Set to true iff i32 parameters to library functions should have signext
177
  /// or zeroext attributes if they correspond to C-level int or unsigned int,
178
  /// respectively.
179
99.8k
  void setShouldExtI32Param(bool Val) {
180
99.8k
    ShouldExtI32Param = Val;
181
99.8k
  }
182
183
  /// Set to true iff i32 results from library functions should have signext
184
  /// or zeroext attributes if they correspond to C-level int or unsigned int,
185
  /// respectively.
186
99.8k
  void setShouldExtI32Return(bool Val) {
187
99.8k
    ShouldExtI32Return = Val;
188
99.8k
  }
189
190
  /// Set to true iff i32 parameters to library functions should have signext
191
  /// attribute if they correspond to C-level int or unsigned int.
192
99.8k
  void setShouldSignExtI32Param(bool Val) {
193
99.8k
    ShouldSignExtI32Param = Val;
194
99.8k
  }
195
196
  /// Returns the size of the wchar_t type in bytes or 0 if the size is unknown.
197
  /// This queries the 'wchar_size' metadata.
198
  unsigned getWCharSize(const Module &M) const;
199
};
200
201
/// Provides information about what library functions are available for
202
/// the current target.
203
///
204
/// This both allows optimizations to handle them specially and frontends to
205
/// disable such optimizations through -fno-builtin etc.
206
class TargetLibraryInfo {
207
  friend class TargetLibraryAnalysis;
208
  friend class TargetLibraryInfoWrapperPass;
209
210
  const TargetLibraryInfoImpl *Impl;
211
212
public:
213
128k
  explicit TargetLibraryInfo(const TargetLibraryInfoImpl &Impl) : Impl(&Impl) {}
214
215
  // Provide value semantics.
216
0
  TargetLibraryInfo(const TargetLibraryInfo &TLI) : Impl(TLI.Impl) {}
217
16.2k
  TargetLibraryInfo(TargetLibraryInfo &&TLI) : Impl(TLI.Impl) {}
218
0
  TargetLibraryInfo &operator=(const TargetLibraryInfo &TLI) {
219
0
    Impl = TLI.Impl;
220
0
    return *this;
221
0
  }
222
0
  TargetLibraryInfo &operator=(TargetLibraryInfo &&TLI) {
223
0
    Impl = TLI.Impl;
224
0
    return *this;
225
0
  }
226
227
  /// Searches for a particular function name.
228
  ///
229
  /// If it is one of the known library functions, return true and set F to the
230
  /// corresponding value.
231
26.6M
  bool getLibFunc(StringRef funcName, LibFunc &F) const {
232
26.6M
    return Impl->getLibFunc(funcName, F);
233
26.6M
  }
234
235
24.0M
  bool getLibFunc(const Function &FDecl, LibFunc &F) const {
236
24.0M
    return Impl->getLibFunc(FDecl, F);
237
24.0M
  }
238
239
  /// If a callsite does not have the 'nobuiltin' attribute, return if the
240
  /// called function is a known library function and set F to that function.
241
1.83M
  bool getLibFunc(ImmutableCallSite CS, LibFunc &F) const {
242
1.83M
    return !CS.isNoBuiltin() && 
CS.getCalledFunction()767k
&&
243
1.83M
           
getLibFunc(*(CS.getCalledFunction()), F)727k
;
244
1.83M
  }
245
246
  /// Tests whether a library function is available.
247
21.6M
  bool has(LibFunc F) const {
248
21.6M
    return Impl->getState(F) != TargetLibraryInfoImpl::Unavailable;
249
21.6M
  }
250
1.57k
  bool isFunctionVectorizable(StringRef F, unsigned VF) const {
251
1.57k
    return Impl->isFunctionVectorizable(F, VF);
252
1.57k
  }
253
31.8k
  bool isFunctionVectorizable(StringRef F) const {
254
31.8k
    return Impl->isFunctionVectorizable(F);
255
31.8k
  }
256
134
  StringRef getVectorizedFunction(StringRef F, unsigned VF) const {
257
134
    return Impl->getVectorizedFunction(F, VF);
258
134
  }
259
260
  /// Tests if the function is both available and a candidate for optimized code
261
  /// generation.
262
46.9k
  bool hasOptimizedCodeGen(LibFunc F) const {
263
46.9k
    if (Impl->getState(F) == TargetLibraryInfoImpl::Unavailable)
264
4.59k
      return false;
265
42.3k
    switch (F) {
266
42.3k
    
default: break35.2k
;
267
42.3k
    
case LibFunc_copysign: 7.10k
case LibFunc_copysignf: 7.10k
case LibFunc_copysignl:
268
7.10k
    case LibFunc_fabs:         case LibFunc_fabsf:      case LibFunc_fabsl:
269
7.10k
    case LibFunc_sin:          case LibFunc_sinf:       case LibFunc_sinl:
270
7.10k
    case LibFunc_cos:          case LibFunc_cosf:       case LibFunc_cosl:
271
7.10k
    case LibFunc_sqrt:         case LibFunc_sqrtf:      case LibFunc_sqrtl:
272
7.10k
    case LibFunc_sqrt_finite:  case LibFunc_sqrtf_finite:
273
7.10k
                                                   case LibFunc_sqrtl_finite:
274
7.10k
    case LibFunc_fmax:         case LibFunc_fmaxf:      case LibFunc_fmaxl:
275
7.10k
    case LibFunc_fmin:         case LibFunc_fminf:      case LibFunc_fminl:
276
7.10k
    case LibFunc_floor:        case LibFunc_floorf:     case LibFunc_floorl:
277
7.10k
    case LibFunc_nearbyint:    case LibFunc_nearbyintf: case LibFunc_nearbyintl:
278
7.10k
    case LibFunc_ceil:         case LibFunc_ceilf:      case LibFunc_ceill:
279
7.10k
    case LibFunc_rint:         case LibFunc_rintf:      case LibFunc_rintl:
280
7.10k
    case LibFunc_round:        case LibFunc_roundf:     case LibFunc_roundl:
281
7.10k
    case LibFunc_trunc:        case LibFunc_truncf:     case LibFunc_truncl:
282
7.10k
    case LibFunc_log2:         case LibFunc_log2f:      case LibFunc_log2l:
283
7.10k
    case LibFunc_exp2:         case LibFunc_exp2f:      case LibFunc_exp2l:
284
7.10k
    case LibFunc_memcmp:       case LibFunc_bcmp:       case LibFunc_strcmp:
285
7.10k
    case LibFunc_strcpy:       case LibFunc_stpcpy:     case LibFunc_strlen:
286
7.10k
    case LibFunc_strnlen:      case LibFunc_memchr:     case LibFunc_mempcpy:
287
7.10k
      return true;
288
35.2k
    }
289
35.2k
    return false;
290
35.2k
  }
291
292
5.89M
  StringRef getName(LibFunc F) const {
293
5.89M
    auto State = Impl->getState(F);
294
5.89M
    if (State == TargetLibraryInfoImpl::Unavailable)
295
182
      return StringRef();
296
5.89M
    if (State == TargetLibraryInfoImpl::StandardName)
297
5.89M
      return Impl->StandardNames[F];
298
46
    assert(State == TargetLibraryInfoImpl::CustomName);
299
46
    return Impl->CustomNames.find(F)->second;
300
46
  }
301
302
  /// Returns extension attribute kind to be used for i32 parameters
303
  /// corresponding to C-level int or unsigned int.  May be zeroext, signext,
304
  /// or none.
305
228
  Attribute::AttrKind getExtAttrForI32Param(bool Signed = true) const {
306
228
    if (Impl->ShouldExtI32Param)
307
8
      return Signed ? 
Attribute::SExt0
: Attribute::ZExt;
308
220
    if (Impl->ShouldSignExtI32Param)
309
4
      return Attribute::SExt;
310
216
    return Attribute::None;
311
216
  }
312
313
  /// Returns extension attribute kind to be used for i32 return values
314
  /// corresponding to C-level int or unsigned int.  May be zeroext, signext,
315
  /// or none.
316
0
  Attribute::AttrKind getExtAttrForI32Return(bool Signed = true) const {
317
0
    if (Impl->ShouldExtI32Return)
318
0
      return Signed ? Attribute::SExt : Attribute::ZExt;
319
0
    return Attribute::None;
320
0
  }
321
322
  /// \copydoc TargetLibraryInfoImpl::getWCharSize()
323
73
  unsigned getWCharSize(const Module &M) const {
324
73
    return Impl->getWCharSize(M);
325
73
  }
326
327
  /// Handle invalidation from the pass manager.
328
  ///
329
  /// If we try to invalidate this info, just return false. It cannot become
330
  /// invalid even if the module or function changes.
331
  bool invalidate(Module &, const PreservedAnalyses &,
332
722
                  ModuleAnalysisManager::Invalidator &) {
333
722
    return false;
334
722
  }
335
  bool invalidate(Function &, const PreservedAnalyses &,
336
6.73k
                  FunctionAnalysisManager::Invalidator &) {
337
6.73k
    return false;
338
6.73k
  }
339
};
340
341
/// Analysis pass providing the \c TargetLibraryInfo.
342
///
343
/// Note that this pass's result cannot be invalidated, it is immutable for the
344
/// life of the module.
345
class TargetLibraryAnalysis : public AnalysisInfoMixin<TargetLibraryAnalysis> {
346
public:
347
  typedef TargetLibraryInfo Result;
348
349
  /// Default construct the library analysis.
350
  ///
351
  /// This will use the module's triple to construct the library info for that
352
  /// module.
353
2.64k
  TargetLibraryAnalysis() {}
354
355
  /// Construct a library analysis with preset info.
356
  ///
357
  /// This will directly copy the preset info into the result without
358
  /// consulting the module's triple.
359
  TargetLibraryAnalysis(TargetLibraryInfoImpl PresetInfoImpl)
360
248
      : PresetInfoImpl(std::move(PresetInfoImpl)) {}
361
362
  TargetLibraryInfo run(Module &M, ModuleAnalysisManager &);
363
  TargetLibraryInfo run(Function &F, FunctionAnalysisManager &);
364
365
private:
366
  friend AnalysisInfoMixin<TargetLibraryAnalysis>;
367
  static AnalysisKey Key;
368
369
  Optional<TargetLibraryInfoImpl> PresetInfoImpl;
370
371
  StringMap<std::unique_ptr<TargetLibraryInfoImpl>> Impls;
372
373
  TargetLibraryInfoImpl &lookupInfoImpl(const Triple &T);
374
};
375
376
class TargetLibraryInfoWrapperPass : public ImmutablePass {
377
  TargetLibraryInfoImpl TLIImpl;
378
  TargetLibraryInfo TLI;
379
380
  virtual void anchor();
381
382
public:
383
  static char ID;
384
  TargetLibraryInfoWrapperPass();
385
  explicit TargetLibraryInfoWrapperPass(const Triple &T);
386
  explicit TargetLibraryInfoWrapperPass(const TargetLibraryInfoImpl &TLI);
387
388
52.2M
  TargetLibraryInfo &getTLI() { return TLI; }
389
0
  const TargetLibraryInfo &getTLI() const { return TLI; }
390
};
391
392
} // end namespace llvm
393
394
#endif