Coverage Report

Created: 2022-07-16 07:03

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/CodeGen/CodeGenModule.h
Line
Count
Source (jump to first uncovered line)
1
//===--- CodeGenModule.h - Per-Module state for LLVM CodeGen ----*- 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
// This is the internal per-translation-unit state used for llvm translation.
10
//
11
//===----------------------------------------------------------------------===//
12
13
#ifndef LLVM_CLANG_LIB_CODEGEN_CODEGENMODULE_H
14
#define LLVM_CLANG_LIB_CODEGEN_CODEGENMODULE_H
15
16
#include "CGVTables.h"
17
#include "CodeGenTypeCache.h"
18
#include "CodeGenTypes.h"
19
#include "SanitizerMetadata.h"
20
#include "clang/AST/DeclCXX.h"
21
#include "clang/AST/DeclObjC.h"
22
#include "clang/AST/DeclOpenMP.h"
23
#include "clang/AST/GlobalDecl.h"
24
#include "clang/AST/Mangle.h"
25
#include "clang/Basic/ABI.h"
26
#include "clang/Basic/LangOptions.h"
27
#include "clang/Basic/Module.h"
28
#include "clang/Basic/NoSanitizeList.h"
29
#include "clang/Basic/TargetInfo.h"
30
#include "clang/Basic/XRayLists.h"
31
#include "clang/Lex/PreprocessorOptions.h"
32
#include "llvm/ADT/DenseMap.h"
33
#include "llvm/ADT/SetVector.h"
34
#include "llvm/ADT/SmallPtrSet.h"
35
#include "llvm/ADT/StringMap.h"
36
#include "llvm/IR/Module.h"
37
#include "llvm/IR/ValueHandle.h"
38
#include "llvm/Transforms/Utils/SanitizerStats.h"
39
40
namespace llvm {
41
class Module;
42
class Constant;
43
class ConstantInt;
44
class Function;
45
class GlobalValue;
46
class DataLayout;
47
class FunctionType;
48
class LLVMContext;
49
class IndexedInstrProfReader;
50
}
51
52
namespace clang {
53
class ASTContext;
54
class AtomicType;
55
class FunctionDecl;
56
class IdentifierInfo;
57
class ObjCImplementationDecl;
58
class ObjCEncodeExpr;
59
class BlockExpr;
60
class CharUnits;
61
class Decl;
62
class Expr;
63
class Stmt;
64
class StringLiteral;
65
class NamedDecl;
66
class ValueDecl;
67
class VarDecl;
68
class LangOptions;
69
class CodeGenOptions;
70
class HeaderSearchOptions;
71
class DiagnosticsEngine;
72
class AnnotateAttr;
73
class CXXDestructorDecl;
74
class Module;
75
class CoverageSourceInfo;
76
class InitSegAttr;
77
78
namespace CodeGen {
79
80
class CodeGenFunction;
81
class CodeGenTBAA;
82
class CGCXXABI;
83
class CGDebugInfo;
84
class CGObjCRuntime;
85
class CGOpenCLRuntime;
86
class CGOpenMPRuntime;
87
class CGCUDARuntime;
88
class CGHLSLRuntime;
89
class CoverageMappingModuleGen;
90
class TargetCodeGenInfo;
91
92
enum ForDefinition_t : bool {
93
  NotForDefinition = false,
94
  ForDefinition = true
95
};
96
97
struct OrderGlobalInitsOrStermFinalizers {
98
  unsigned int priority;
99
  unsigned int lex_order;
100
  OrderGlobalInitsOrStermFinalizers(unsigned int p, unsigned int l)
101
937
      : priority(p), lex_order(l) {}
102
103
0
  bool operator==(const OrderGlobalInitsOrStermFinalizers &RHS) const {
104
0
    return priority == RHS.priority && lex_order == RHS.lex_order;
105
0
  }
106
107
663
  bool operator<(const OrderGlobalInitsOrStermFinalizers &RHS) const {
108
663
    return std::tie(priority, lex_order) <
109
663
           std::tie(RHS.priority, RHS.lex_order);
110
663
  }
111
};
112
113
struct ObjCEntrypoints {
114
16.4k
  ObjCEntrypoints() { memset(this, 0, sizeof(*this)); }
115
116
  /// void objc_alloc(id);
117
  llvm::FunctionCallee objc_alloc;
118
119
  /// void objc_allocWithZone(id);
120
  llvm::FunctionCallee objc_allocWithZone;
121
122
  /// void objc_alloc_init(id);
123
  llvm::FunctionCallee objc_alloc_init;
124
125
  /// void objc_autoreleasePoolPop(void*);
126
  llvm::FunctionCallee objc_autoreleasePoolPop;
127
128
  /// void objc_autoreleasePoolPop(void*);
129
  /// Note this method is used when we are using exception handling
130
  llvm::FunctionCallee objc_autoreleasePoolPopInvoke;
131
132
  /// void *objc_autoreleasePoolPush(void);
133
  llvm::Function *objc_autoreleasePoolPush;
134
135
  /// id objc_autorelease(id);
136
  llvm::Function *objc_autorelease;
137
138
  /// id objc_autorelease(id);
139
  /// Note this is the runtime method not the intrinsic.
140
  llvm::FunctionCallee objc_autoreleaseRuntimeFunction;
141
142
  /// id objc_autoreleaseReturnValue(id);
143
  llvm::Function *objc_autoreleaseReturnValue;
144
145
  /// void objc_copyWeak(id *dest, id *src);
146
  llvm::Function *objc_copyWeak;
147
148
  /// void objc_destroyWeak(id*);
149
  llvm::Function *objc_destroyWeak;
150
151
  /// id objc_initWeak(id*, id);
152
  llvm::Function *objc_initWeak;
153
154
  /// id objc_loadWeak(id*);
155
  llvm::Function *objc_loadWeak;
156
157
  /// id objc_loadWeakRetained(id*);
158
  llvm::Function *objc_loadWeakRetained;
159
160
  /// void objc_moveWeak(id *dest, id *src);
161
  llvm::Function *objc_moveWeak;
162
163
  /// id objc_retain(id);
164
  llvm::Function *objc_retain;
165
166
  /// id objc_retain(id);
167
  /// Note this is the runtime method not the intrinsic.
168
  llvm::FunctionCallee objc_retainRuntimeFunction;
169
170
  /// id objc_retainAutorelease(id);
171
  llvm::Function *objc_retainAutorelease;
172
173
  /// id objc_retainAutoreleaseReturnValue(id);
174
  llvm::Function *objc_retainAutoreleaseReturnValue;
175
176
  /// id objc_retainAutoreleasedReturnValue(id);
177
  llvm::Function *objc_retainAutoreleasedReturnValue;
178
179
  /// id objc_retainBlock(id);
180
  llvm::Function *objc_retainBlock;
181
182
  /// void objc_release(id);
183
  llvm::Function *objc_release;
184
185
  /// void objc_release(id);
186
  /// Note this is the runtime method not the intrinsic.
187
  llvm::FunctionCallee objc_releaseRuntimeFunction;
188
189
  /// void objc_storeStrong(id*, id);
190
  llvm::Function *objc_storeStrong;
191
192
  /// id objc_storeWeak(id*, id);
193
  llvm::Function *objc_storeWeak;
194
195
  /// id objc_unsafeClaimAutoreleasedReturnValue(id);
196
  llvm::Function *objc_unsafeClaimAutoreleasedReturnValue;
197
198
  /// A void(void) inline asm to use to mark that the return value of
199
  /// a call will be immediately retain.
200
  llvm::InlineAsm *retainAutoreleasedReturnValueMarker;
201
202
  /// void clang.arc.use(...);
203
  llvm::Function *clang_arc_use;
204
205
  /// void clang.arc.noop.use(...);
206
  llvm::Function *clang_arc_noop_use;
207
};
208
209
/// This class records statistics on instrumentation based profiling.
210
class InstrProfStats {
211
  uint32_t VisitedInMainFile;
212
  uint32_t MissingInMainFile;
213
  uint32_t Visited;
214
  uint32_t Missing;
215
  uint32_t Mismatched;
216
217
public:
218
  InstrProfStats()
219
      : VisitedInMainFile(0), MissingInMainFile(0), Visited(0), Missing(0),
220
36.4k
        Mismatched(0) {}
221
  /// Record that we've visited a function and whether or not that function was
222
  /// in the main source file.
223
187
  void addVisited(bool MainFile) {
224
187
    if (MainFile)
225
184
      ++VisitedInMainFile;
226
187
    ++Visited;
227
187
  }
228
  /// Record that a function we've visited has no profile data.
229
6
  void addMissing(bool MainFile) {
230
6
    if (MainFile)
231
6
      ++MissingInMainFile;
232
6
    ++Missing;
233
6
  }
234
  /// Record that a function we've visited has mismatched profile data.
235
9
  void addMismatched(bool MainFile) { ++Mismatched; }
236
  /// Whether or not the stats we've gathered indicate any potential problems.
237
53
  bool hasDiagnostics() { return Missing || 
Mismatched43
; }
238
  /// Report potential problems we've found to \c Diags.
239
  void reportDiagnostics(DiagnosticsEngine &Diags, StringRef MainFile);
240
};
241
242
/// A pair of helper functions for a __block variable.
243
class BlockByrefHelpers : public llvm::FoldingSetNode {
244
  // MSVC requires this type to be complete in order to process this
245
  // header.
246
public:
247
  llvm::Constant *CopyHelper;
248
  llvm::Constant *DisposeHelper;
249
250
  /// The alignment of the field.  This is important because
251
  /// different offsets to the field within the byref struct need to
252
  /// have different helper functions.
253
  CharUnits Alignment;
254
255
  BlockByrefHelpers(CharUnits alignment)
256
87
      : CopyHelper(nullptr), DisposeHelper(nullptr), Alignment(alignment) {}
257
67
  BlockByrefHelpers(const BlockByrefHelpers &) = default;
258
  virtual ~BlockByrefHelpers();
259
260
107
  void Profile(llvm::FoldingSetNodeID &id) const {
261
107
    id.AddInteger(Alignment.getQuantity());
262
107
    profileImpl(id);
263
107
  }
264
  virtual void profileImpl(llvm::FoldingSetNodeID &id) const = 0;
265
266
54
  virtual bool needsCopy() const { return true; }
267
  virtual void emitCopy(CodeGenFunction &CGF, Address dest, Address src) = 0;
268
269
57
  virtual bool needsDispose() const { return true; }
270
  virtual void emitDispose(CodeGenFunction &CGF, Address field) = 0;
271
};
272
273
/// This class organizes the cross-function state that is used while generating
274
/// LLVM code.
275
class CodeGenModule : public CodeGenTypeCache {
276
  CodeGenModule(const CodeGenModule &) = delete;
277
  void operator=(const CodeGenModule &) = delete;
278
279
public:
280
  struct Structor {
281
0
    Structor() : Priority(0), Initializer(nullptr), AssociatedData(nullptr) {}
282
    Structor(int Priority, llvm::Constant *Initializer,
283
             llvm::Constant *AssociatedData)
284
        : Priority(Priority), Initializer(Initializer),
285
4.30k
          AssociatedData(AssociatedData) {}
286
    int Priority;
287
    llvm::Constant *Initializer;
288
    llvm::Constant *AssociatedData;
289
  };
290
291
  typedef std::vector<Structor> CtorList;
292
293
private:
294
  ASTContext &Context;
295
  const LangOptions &LangOpts;
296
  const HeaderSearchOptions &HeaderSearchOpts; // Only used for debug info.
297
  const PreprocessorOptions &PreprocessorOpts; // Only used for debug info.
298
  const CodeGenOptions &CodeGenOpts;
299
  unsigned NumAutoVarInit = 0;
300
  llvm::Module &TheModule;
301
  DiagnosticsEngine &Diags;
302
  const TargetInfo &Target;
303
  std::unique_ptr<CGCXXABI> ABI;
304
  llvm::LLVMContext &VMContext;
305
  std::string ModuleNameHash;
306
307
  std::unique_ptr<CodeGenTBAA> TBAA;
308
309
  mutable std::unique_ptr<TargetCodeGenInfo> TheTargetCodeGenInfo;
310
311
  // This should not be moved earlier, since its initialization depends on some
312
  // of the previous reference members being already initialized and also checks
313
  // if TheTargetCodeGenInfo is NULL
314
  CodeGenTypes Types;
315
316
  /// Holds information about C++ vtables.
317
  CodeGenVTables VTables;
318
319
  std::unique_ptr<CGObjCRuntime> ObjCRuntime;
320
  std::unique_ptr<CGOpenCLRuntime> OpenCLRuntime;
321
  std::unique_ptr<CGOpenMPRuntime> OpenMPRuntime;
322
  std::unique_ptr<CGCUDARuntime> CUDARuntime;
323
  std::unique_ptr<CGHLSLRuntime> HLSLRuntime;
324
  std::unique_ptr<CGDebugInfo> DebugInfo;
325
  std::unique_ptr<ObjCEntrypoints> ObjCData;
326
  llvm::MDNode *NoObjCARCExceptionsMetadata = nullptr;
327
  std::unique_ptr<llvm::IndexedInstrProfReader> PGOReader;
328
  InstrProfStats PGOStats;
329
  std::unique_ptr<llvm::SanitizerStatReport> SanStats;
330
331
  // A set of references that have only been seen via a weakref so far. This is
332
  // used to remove the weak of the reference if we ever see a direct reference
333
  // or a definition.
334
  llvm::SmallPtrSet<llvm::GlobalValue*, 10> WeakRefReferences;
335
336
  /// This contains all the decls which have definitions but/ which are deferred
337
  /// for emission and therefore should only be output if they are actually
338
  /// used. If a decl is in this, then it is known to have not been referenced
339
  /// yet.
340
  llvm::DenseMap<StringRef, GlobalDecl> DeferredDecls;
341
342
  /// This is a list of deferred decls which we have seen that *are* actually
343
  /// referenced. These get code generated when the module is done.
344
  std::vector<GlobalDecl> DeferredDeclsToEmit;
345
149k
  void addDeferredDeclToEmit(GlobalDecl GD) {
346
149k
    DeferredDeclsToEmit.emplace_back(GD);
347
149k
    addEmittedDeferredDecl(GD);
348
149k
  }
349
350
  /// Decls that were DeferredDecls and have now been emitted.
351
  llvm::DenseMap<llvm::StringRef, GlobalDecl> EmittedDeferredDecls;
352
353
149k
  void addEmittedDeferredDecl(GlobalDecl GD) {
354
149k
    if (!llvm::isa<FunctionDecl>(GD.getDecl()))
355
4.50k
      return;
356
145k
    llvm::GlobalVariable::LinkageTypes L = getFunctionLinkage(GD);
357
145k
    if (llvm::GlobalValue::isLinkOnceLinkage(L) ||
358
145k
        
llvm::GlobalValue::isWeakLinkage(L)26.8k
) {
359
118k
      EmittedDeferredDecls[getMangledName(GD)] = GD;
360
118k
    }
361
145k
  }
362
363
  /// List of alias we have emitted. Used to make sure that what they point to
364
  /// is defined once we get to the end of the of the translation unit.
365
  std::vector<GlobalDecl> Aliases;
366
367
  /// List of multiversion functions to be emitted. This list is processed in
368
  /// conjunction with other deferred symbols and is used to ensure that
369
  /// multiversion function resolvers and ifuncs are defined and emitted.
370
  std::vector<GlobalDecl> MultiVersionFuncs;
371
372
  typedef llvm::StringMap<llvm::TrackingVH<llvm::Constant> > ReplacementsTy;
373
  ReplacementsTy Replacements;
374
375
  /// List of global values to be replaced with something else. Used when we
376
  /// want to replace a GlobalValue but can't identify it by its mangled name
377
  /// anymore (because the name is already taken).
378
  llvm::SmallVector<std::pair<llvm::GlobalValue *, llvm::Constant *>, 8>
379
    GlobalValReplacements;
380
381
  /// Variables for which we've emitted globals containing their constant
382
  /// values along with the corresponding globals, for opportunistic reuse.
383
  llvm::DenseMap<const VarDecl*, llvm::GlobalVariable*> InitializerConstants;
384
385
  /// Set of global decls for which we already diagnosed mangled name conflict.
386
  /// Required to not issue a warning (on a mangling conflict) multiple times
387
  /// for the same decl.
388
  llvm::DenseSet<GlobalDecl> DiagnosedConflictingDefinitions;
389
390
  /// A queue of (optional) vtables to consider emitting.
391
  std::vector<const CXXRecordDecl*> DeferredVTables;
392
393
  /// A queue of (optional) vtables that may be emitted opportunistically.
394
  std::vector<const CXXRecordDecl *> OpportunisticVTables;
395
396
  /// List of global values which are required to be present in the object file;
397
  /// bitcast to i8*. This is used for forcing visibility of symbols which may
398
  /// otherwise be optimized out.
399
  std::vector<llvm::WeakTrackingVH> LLVMUsed;
400
  std::vector<llvm::WeakTrackingVH> LLVMCompilerUsed;
401
402
  /// Store the list of global constructors and their respective priorities to
403
  /// be emitted when the translation unit is complete.
404
  CtorList GlobalCtors;
405
406
  /// Store the list of global destructors and their respective priorities to be
407
  /// emitted when the translation unit is complete.
408
  CtorList GlobalDtors;
409
410
  /// An ordered map of canonical GlobalDecls to their mangled names.
411
  llvm::MapVector<GlobalDecl, StringRef> MangledDeclNames;
412
  llvm::StringMap<GlobalDecl, llvm::BumpPtrAllocator> Manglings;
413
414
  /// Global annotations.
415
  std::vector<llvm::Constant*> Annotations;
416
417
  /// Map used to get unique annotation strings.
418
  llvm::StringMap<llvm::Constant*> AnnotationStrings;
419
420
  /// Used for uniquing of annotation arguments.
421
  llvm::DenseMap<unsigned, llvm::Constant *> AnnotationArgs;
422
423
  llvm::StringMap<llvm::GlobalVariable *> CFConstantStringMap;
424
425
  llvm::DenseMap<llvm::Constant *, llvm::GlobalVariable *> ConstantStringMap;
426
  llvm::DenseMap<const UnnamedGlobalConstantDecl *, llvm::GlobalVariable *>
427
      UnnamedGlobalConstantDeclMap;
428
  llvm::DenseMap<const Decl*, llvm::Constant *> StaticLocalDeclMap;
429
  llvm::DenseMap<const Decl*, llvm::GlobalVariable*> StaticLocalDeclGuardMap;
430
  llvm::DenseMap<const Expr*, llvm::Constant *> MaterializedGlobalTemporaryMap;
431
432
  llvm::DenseMap<QualType, llvm::Constant *> AtomicSetterHelperFnMap;
433
  llvm::DenseMap<QualType, llvm::Constant *> AtomicGetterHelperFnMap;
434
435
  /// Map used to get unique type descriptor constants for sanitizers.
436
  llvm::DenseMap<QualType, llvm::Constant *> TypeDescriptorMap;
437
438
  /// Map used to track internal linkage functions declared within
439
  /// extern "C" regions.
440
  typedef llvm::MapVector<IdentifierInfo *,
441
                          llvm::GlobalValue *> StaticExternCMap;
442
  StaticExternCMap StaticExternCValues;
443
444
  /// thread_local variables defined or used in this TU.
445
  std::vector<const VarDecl *> CXXThreadLocals;
446
447
  /// thread_local variables with initializers that need to run
448
  /// before any thread_local variable in this TU is odr-used.
449
  std::vector<llvm::Function *> CXXThreadLocalInits;
450
  std::vector<const VarDecl *> CXXThreadLocalInitVars;
451
452
  /// Global variables with initializers that need to run before main.
453
  std::vector<llvm::Function *> CXXGlobalInits;
454
455
  /// When a C++ decl with an initializer is deferred, null is
456
  /// appended to CXXGlobalInits, and the index of that null is placed
457
  /// here so that the initializer will be performed in the correct
458
  /// order. Once the decl is emitted, the index is replaced with ~0U to ensure
459
  /// that we don't re-emit the initializer.
460
  llvm::DenseMap<const Decl*, unsigned> DelayedCXXInitPosition;
461
462
  typedef std::pair<OrderGlobalInitsOrStermFinalizers, llvm::Function *>
463
      GlobalInitData;
464
465
  struct GlobalInitPriorityCmp {
466
    bool operator()(const GlobalInitData &LHS,
467
623
                    const GlobalInitData &RHS) const {
468
623
      return LHS.first.priority < RHS.first.priority;
469
623
    }
470
  };
471
472
  /// Global variables with initializers whose order of initialization is set by
473
  /// init_priority attribute.
474
  SmallVector<GlobalInitData, 8> PrioritizedCXXGlobalInits;
475
476
  /// Global destructor functions and arguments that need to run on termination.
477
  /// When UseSinitAndSterm is set, it instead contains sterm finalizer
478
  /// functions, which also run on unloading a shared library.
479
  typedef std::tuple<llvm::FunctionType *, llvm::WeakTrackingVH,
480
                     llvm::Constant *>
481
      CXXGlobalDtorsOrStermFinalizer_t;
482
  SmallVector<CXXGlobalDtorsOrStermFinalizer_t, 8>
483
      CXXGlobalDtorsOrStermFinalizers;
484
485
  typedef std::pair<OrderGlobalInitsOrStermFinalizers, llvm::Function *>
486
      StermFinalizerData;
487
488
  struct StermFinalizerPriorityCmp {
489
    bool operator()(const StermFinalizerData &LHS,
490
8
                    const StermFinalizerData &RHS) const {
491
8
      return LHS.first.priority < RHS.first.priority;
492
8
    }
493
  };
494
495
  /// Global variables with sterm finalizers whose order of initialization is
496
  /// set by init_priority attribute.
497
  SmallVector<StermFinalizerData, 8> PrioritizedCXXStermFinalizers;
498
499
  /// The complete set of modules that has been imported.
500
  llvm::SetVector<clang::Module *> ImportedModules;
501
502
  /// The set of modules for which the module initializers
503
  /// have been emitted.
504
  llvm::SmallPtrSet<clang::Module *, 16> EmittedModuleInitializers;
505
506
  /// A vector of metadata strings for linker options.
507
  SmallVector<llvm::MDNode *, 16> LinkerOptionsMetadata;
508
509
  /// A vector of metadata strings for dependent libraries for ELF.
510
  SmallVector<llvm::MDNode *, 16> ELFDependentLibraries;
511
512
  /// @name Cache for Objective-C runtime types
513
  /// @{
514
515
  /// Cached reference to the class for constant strings. This value has type
516
  /// int * but is actually an Obj-C class pointer.
517
  llvm::WeakTrackingVH CFConstantStringClassRef;
518
519
  /// The type used to describe the state of a fast enumeration in
520
  /// Objective-C's for..in loop.
521
  QualType ObjCFastEnumerationStateType;
522
523
  /// @}
524
525
  /// Lazily create the Objective-C runtime
526
  void createObjCRuntime();
527
528
  void createOpenCLRuntime();
529
  void createOpenMPRuntime();
530
  void createCUDARuntime();
531
  void createHLSLRuntime();
532
533
  bool isTriviallyRecursive(const FunctionDecl *F);
534
  bool shouldEmitFunction(GlobalDecl GD);
535
  bool shouldOpportunisticallyEmitVTables();
536
  /// Map used to be sure we don't emit the same CompoundLiteral twice.
537
  llvm::DenseMap<const CompoundLiteralExpr *, llvm::GlobalVariable *>
538
      EmittedCompoundLiterals;
539
540
  /// Map of the global blocks we've emitted, so that we don't have to re-emit
541
  /// them if the constexpr evaluator gets aggressive.
542
  llvm::DenseMap<const BlockExpr *, llvm::Constant *> EmittedGlobalBlocks;
543
544
  /// @name Cache for Blocks Runtime Globals
545
  /// @{
546
547
  llvm::Constant *NSConcreteGlobalBlock = nullptr;
548
  llvm::Constant *NSConcreteStackBlock = nullptr;
549
550
  llvm::FunctionCallee BlockObjectAssign = nullptr;
551
  llvm::FunctionCallee BlockObjectDispose = nullptr;
552
553
  llvm::Type *BlockDescriptorType = nullptr;
554
  llvm::Type *GenericBlockLiteralType = nullptr;
555
556
  struct {
557
    int GlobalUniqueCount;
558
  } Block;
559
560
  GlobalDecl initializedGlobalDecl;
561
562
  /// @}
563
564
  /// void @llvm.lifetime.start(i64 %size, i8* nocapture <ptr>)
565
  llvm::Function *LifetimeStartFn = nullptr;
566
567
  /// void @llvm.lifetime.end(i64 %size, i8* nocapture <ptr>)
568
  llvm::Function *LifetimeEndFn = nullptr;
569
570
  std::unique_ptr<SanitizerMetadata> SanitizerMD;
571
572
  llvm::MapVector<const Decl *, bool> DeferredEmptyCoverageMappingDecls;
573
574
  std::unique_ptr<CoverageMappingModuleGen> CoverageMapping;
575
576
  /// Mapping from canonical types to their metadata identifiers. We need to
577
  /// maintain this mapping because identifiers may be formed from distinct
578
  /// MDNodes.
579
  typedef llvm::DenseMap<QualType, llvm::Metadata *> MetadataTypeMap;
580
  MetadataTypeMap MetadataIdMap;
581
  MetadataTypeMap VirtualMetadataIdMap;
582
  MetadataTypeMap GeneralizedMetadataIdMap;
583
584
  llvm::DenseMap<const llvm::Constant *, llvm::GlobalVariable *> RTTIProxyMap;
585
586
public:
587
  CodeGenModule(ASTContext &C, const HeaderSearchOptions &headersearchopts,
588
                const PreprocessorOptions &ppopts,
589
                const CodeGenOptions &CodeGenOpts, llvm::Module &M,
590
                DiagnosticsEngine &Diags,
591
                CoverageSourceInfo *CoverageInfo = nullptr);
592
593
  ~CodeGenModule();
594
595
  void clear();
596
597
  /// Finalize LLVM code generation.
598
  void Release();
599
600
  /// Return true if we should emit location information for expressions.
601
  bool getExpressionLocationsEnabled() const;
602
603
  /// Return a reference to the configured Objective-C runtime.
604
25.5k
  CGObjCRuntime &getObjCRuntime() {
605
25.5k
    if (!ObjCRuntime) 
createObjCRuntime()0
;
606
25.5k
    return *ObjCRuntime;
607
25.5k
  }
608
609
  /// Return true iff an Objective-C runtime has been configured.
610
0
  bool hasObjCRuntime() { return !!ObjCRuntime; }
611
612
4.54M
  const std::string &getModuleNameHash() const { return ModuleNameHash; }
613
614
  /// Return a reference to the configured OpenCL runtime.
615
1.17k
  CGOpenCLRuntime &getOpenCLRuntime() {
616
1.17k
    assert(OpenCLRuntime != nullptr);
617
0
    return *OpenCLRuntime;
618
1.17k
  }
619
620
  /// Return a reference to the configured OpenMP runtime.
621
920k
  CGOpenMPRuntime &getOpenMPRuntime() {
622
920k
    assert(OpenMPRuntime != nullptr);
623
0
    return *OpenMPRuntime;
624
920k
  }
625
626
  /// Return a reference to the configured CUDA runtime.
627
1.06k
  CGCUDARuntime &getCUDARuntime() {
628
1.06k
    assert(CUDARuntime != nullptr);
629
0
    return *CUDARuntime;
630
1.06k
  }
631
632
  /// Return a reference to the configured HLSL runtime.
633
5
  CGHLSLRuntime &getHLSLRuntime() {
634
5
    assert(HLSLRuntime != nullptr);
635
0
    return *HLSLRuntime;
636
5
  }
637
638
7.32k
  ObjCEntrypoints &getObjCEntrypoints() const {
639
7.32k
    assert(ObjCData != nullptr);
640
0
    return *ObjCData;
641
7.32k
  }
642
643
  // Version checking functions, used to implement ObjC's @available:
644
  // i32 @__isOSVersionAtLeast(i32, i32, i32)
645
  llvm::FunctionCallee IsOSVersionAtLeastFn = nullptr;
646
  // i32 @__isPlatformVersionAtLeast(i32, i32, i32, i32)
647
  llvm::FunctionCallee IsPlatformVersionAtLeastFn = nullptr;
648
649
202
  InstrProfStats &getPGOStats() { return PGOStats; }
650
298k
  llvm::IndexedInstrProfReader *getPGOReader() const { return PGOReader.get(); }
651
652
503
  CoverageMappingModuleGen *getCoverageMapping() const {
653
503
    return CoverageMapping.get();
654
503
  }
655
656
22
  llvm::Constant *getStaticLocalDeclAddress(const VarDecl *D) {
657
22
    return StaticLocalDeclMap[D];
658
22
  }
659
  void setStaticLocalDeclAddress(const VarDecl *D,
660
24.9k
                                 llvm::Constant *C) {
661
24.9k
    StaticLocalDeclMap[D] = C;
662
24.9k
  }
663
664
  llvm::Constant *
665
  getOrCreateStaticVarDecl(const VarDecl &D,
666
                           llvm::GlobalValue::LinkageTypes Linkage);
667
668
9.08k
  llvm::GlobalVariable *getStaticLocalDeclGuardAddress(const VarDecl *D) {
669
9.08k
    return StaticLocalDeclGuardMap[D];
670
9.08k
  }
671
  void setStaticLocalDeclGuardAddress(const VarDecl *D,
672
9.08k
                                      llvm::GlobalVariable *C) {
673
9.08k
    StaticLocalDeclGuardMap[D] = C;
674
9.08k
  }
675
676
  Address createUnnamedGlobalFrom(const VarDecl &D, llvm::Constant *Constant,
677
                                  CharUnits Align);
678
679
  bool lookupRepresentativeDecl(StringRef MangledName,
680
                                GlobalDecl &Result) const;
681
682
7
  llvm::Constant *getAtomicSetterHelperFnMap(QualType Ty) {
683
7
    return AtomicSetterHelperFnMap[Ty];
684
7
  }
685
  void setAtomicSetterHelperFnMap(QualType Ty,
686
5
                            llvm::Constant *Fn) {
687
5
    AtomicSetterHelperFnMap[Ty] = Fn;
688
5
  }
689
690
6
  llvm::Constant *getAtomicGetterHelperFnMap(QualType Ty) {
691
6
    return AtomicGetterHelperFnMap[Ty];
692
6
  }
693
  void setAtomicGetterHelperFnMap(QualType Ty,
694
4
                            llvm::Constant *Fn) {
695
4
    AtomicGetterHelperFnMap[Ty] = Fn;
696
4
  }
697
698
2.68k
  llvm::Constant *getTypeDescriptorFromMap(QualType Ty) {
699
2.68k
    return TypeDescriptorMap[Ty];
700
2.68k
  }
701
545
  void setTypeDescriptorInMap(QualType Ty, llvm::Constant *C) {
702
545
    TypeDescriptorMap[Ty] = C;
703
545
  }
704
705
7.33M
  CGDebugInfo *getModuleDebugInfo() { return DebugInfo.get(); }
706
707
378
  llvm::MDNode *getNoObjCARCExceptionsMetadata() {
708
378
    if (!NoObjCARCExceptionsMetadata)
709
26
      NoObjCARCExceptionsMetadata = llvm::MDNode::get(getLLVMContext(), None);
710
378
    return NoObjCARCExceptionsMetadata;
711
378
  }
712
713
84.0M
  ASTContext &getContext() const { return Context; }
714
32.2M
  const LangOptions &getLangOpts() const { return LangOpts; }
715
  const HeaderSearchOptions &getHeaderSearchOpts()
716
3.76k
    const { return HeaderSearchOpts; }
717
  const PreprocessorOptions &getPreprocessorOpts()
718
31.0k
    const { return PreprocessorOpts; }
719
28.2M
  const CodeGenOptions &getCodeGenOpts() const { return CodeGenOpts; }
720
5.40M
  llvm::Module &getModule() const { return TheModule; }
721
87.7k
  DiagnosticsEngine &getDiags() const { return Diags; }
722
2.33M
  const llvm::DataLayout &getDataLayout() const {
723
2.33M
    return TheModule.getDataLayout();
724
2.33M
  }
725
6.35M
  const TargetInfo &getTarget() const { return Target; }
726
1.63M
  const llvm::Triple &getTriple() const { return Target.getTriple(); }
727
  bool supportsCOMDAT() const;
728
  void maybeSetTrivialComdat(const Decl &D, llvm::GlobalObject &GO);
729
730
5.13M
  CGCXXABI &getCXXABI() const { return *ABI; }
731
17.2M
  llvm::LLVMContext &getLLVMContext() { return VMContext; }
732
733
0
  bool shouldUseTBAA() const { return TBAA != nullptr; }
734
735
  const TargetCodeGenInfo &getTargetCodeGenInfo();
736
737
9.57M
  CodeGenTypes &getTypes() { return Types; }
738
739
13.9k
  CodeGenVTables &getVTables() { return VTables; }
740
741
49.1k
  ItaniumVTableContext &getItaniumVTableContext() {
742
49.1k
    return VTables.getItaniumVTableContext();
743
49.1k
  }
744
745
8.12k
  MicrosoftVTableContext &getMicrosoftVTableContext() {
746
8.12k
    return VTables.getMicrosoftVTableContext();
747
8.12k
  }
748
749
0
  CtorList &getGlobalCtors() { return GlobalCtors; }
750
0
  CtorList &getGlobalDtors() { return GlobalDtors; }
751
752
  /// getTBAATypeInfo - Get metadata used to describe accesses to objects of
753
  /// the given type.
754
  llvm::MDNode *getTBAATypeInfo(QualType QTy);
755
756
  /// getTBAAAccessInfo - Get TBAA information that describes an access to
757
  /// an object of the given type.
758
  TBAAAccessInfo getTBAAAccessInfo(QualType AccessType);
759
760
  /// getTBAAVTablePtrAccessInfo - Get the TBAA information that describes an
761
  /// access to a virtual table pointer.
762
  TBAAAccessInfo getTBAAVTablePtrAccessInfo(llvm::Type *VTablePtrType);
763
764
  llvm::MDNode *getTBAAStructInfo(QualType QTy);
765
766
  /// getTBAABaseTypeInfo - Get metadata that describes the given base access
767
  /// type. Return null if the type is not suitable for use in TBAA access tags.
768
  llvm::MDNode *getTBAABaseTypeInfo(QualType QTy);
769
770
  /// getTBAAAccessTagInfo - Get TBAA tag for a given memory access.
771
  llvm::MDNode *getTBAAAccessTagInfo(TBAAAccessInfo Info);
772
773
  /// mergeTBAAInfoForCast - Get merged TBAA information for the purposes of
774
  /// type casts.
775
  TBAAAccessInfo mergeTBAAInfoForCast(TBAAAccessInfo SourceInfo,
776
                                      TBAAAccessInfo TargetInfo);
777
778
  /// mergeTBAAInfoForConditionalOperator - Get merged TBAA information for the
779
  /// purposes of conditional operator.
780
  TBAAAccessInfo mergeTBAAInfoForConditionalOperator(TBAAAccessInfo InfoA,
781
                                                     TBAAAccessInfo InfoB);
782
783
  /// mergeTBAAInfoForMemoryTransfer - Get merged TBAA information for the
784
  /// purposes of memory transfer calls.
785
  TBAAAccessInfo mergeTBAAInfoForMemoryTransfer(TBAAAccessInfo DestInfo,
786
                                                TBAAAccessInfo SrcInfo);
787
788
  /// getTBAAInfoForSubobject - Get TBAA information for an access with a given
789
  /// base lvalue.
790
34.7k
  TBAAAccessInfo getTBAAInfoForSubobject(LValue Base, QualType AccessType) {
791
34.7k
    if (Base.getTBAAInfo().isMayAlias())
792
303
      return TBAAAccessInfo::getMayAliasInfo();
793
34.4k
    return getTBAAAccessInfo(AccessType);
794
34.7k
  }
795
796
  bool isTypeConstant(QualType QTy, bool ExcludeCtorDtor);
797
798
  bool isPaddedAtomicType(QualType type);
799
  bool isPaddedAtomicType(const AtomicType *type);
800
801
  /// DecorateInstructionWithTBAA - Decorate the instruction with a TBAA tag.
802
  void DecorateInstructionWithTBAA(llvm::Instruction *Inst,
803
                                   TBAAAccessInfo TBAAInfo);
804
805
  /// Adds !invariant.barrier !tag to instruction
806
  void DecorateInstructionWithInvariantGroup(llvm::Instruction *I,
807
                                             const CXXRecordDecl *RD);
808
809
  /// Emit the given number of characters as a value of type size_t.
810
  llvm::ConstantInt *getSize(CharUnits numChars);
811
812
  /// Set the visibility for the given LLVM GlobalValue.
813
  void setGlobalVisibility(llvm::GlobalValue *GV, const NamedDecl *D) const;
814
815
  void setDSOLocal(llvm::GlobalValue *GV) const;
816
817
1.21M
  bool shouldMapVisibilityToDLLExport(const NamedDecl *D) const {
818
1.21M
    return getLangOpts().hasDefaultVisibilityExportMapping() && 
D345
&&
819
1.21M
           (D->getLinkageAndVisibility().getVisibility() ==
820
345
            DefaultVisibility) &&
821
1.21M
           
(264
getLangOpts().isAllDefaultVisibilityExportMapping()264
||
822
264
            
(131
getLangOpts().isExplicitDefaultVisibilityExportMapping()131
&&
823
131
             D->getLinkageAndVisibility().isVisibilityExplicit()));
824
1.21M
  }
825
  void setDLLImportDLLExport(llvm::GlobalValue *GV, GlobalDecl D) const;
826
  void setDLLImportDLLExport(llvm::GlobalValue *GV, const NamedDecl *D) const;
827
  /// Set visibility, dllimport/dllexport and dso_local.
828
  /// This must be called after dllimport/dllexport is set.
829
  void setGVProperties(llvm::GlobalValue *GV, GlobalDecl GD) const;
830
  void setGVProperties(llvm::GlobalValue *GV, const NamedDecl *D) const;
831
832
  void setGVPropertiesAux(llvm::GlobalValue *GV, const NamedDecl *D) const;
833
834
  /// Set the TLS mode for the given LLVM GlobalValue for the thread-local
835
  /// variable declaration D.
836
  void setTLSMode(llvm::GlobalValue *GV, const VarDecl &D) const;
837
838
  /// Get LLVM TLS mode from CodeGenOptions.
839
  llvm::GlobalVariable::ThreadLocalMode GetDefaultLLVMTLSModel() const;
840
841
415k
  static llvm::GlobalValue::VisibilityTypes GetLLVMVisibility(Visibility V) {
842
415k
    switch (V) {
843
253k
    case DefaultVisibility:   return llvm::GlobalValue::DefaultVisibility;
844
161k
    case HiddenVisibility:    return llvm::GlobalValue::HiddenVisibility;
845
527
    case ProtectedVisibility: return llvm::GlobalValue::ProtectedVisibility;
846
415k
    }
847
0
    llvm_unreachable("unknown visibility!");
848
0
  }
849
850
  llvm::Constant *GetAddrOfGlobal(GlobalDecl GD,
851
                                  ForDefinition_t IsForDefinition
852
                                    = NotForDefinition);
853
854
  /// Will return a global variable of the given type. If a variable with a
855
  /// different type already exists then a new  variable with the right type
856
  /// will be created and all uses of the old variable will be replaced with a
857
  /// bitcast to the new variable.
858
  llvm::GlobalVariable *
859
  CreateOrReplaceCXXRuntimeVariable(StringRef Name, llvm::Type *Ty,
860
                                    llvm::GlobalValue::LinkageTypes Linkage,
861
                                    unsigned Alignment);
862
863
  llvm::Function *CreateGlobalInitOrCleanUpFunction(
864
      llvm::FunctionType *ty, const Twine &name, const CGFunctionInfo &FI,
865
      SourceLocation Loc = SourceLocation(), bool TLS = false,
866
      llvm::GlobalVariable::LinkageTypes Linkage =
867
          llvm::GlobalVariable::InternalLinkage);
868
869
  /// Return the AST address space of the underlying global variable for D, as
870
  /// determined by its declaration. Normally this is the same as the address
871
  /// space of D's type, but in CUDA, address spaces are associated with
872
  /// declarations, not types. If D is nullptr, return the default address
873
  /// space for global variable.
874
  ///
875
  /// For languages without explicit address spaces, if D has default address
876
  /// space, target-specific global or constant address space may be returned.
877
  LangAS GetGlobalVarAddressSpace(const VarDecl *D);
878
879
  /// Return the AST address space of constant literal, which is used to emit
880
  /// the constant literal as global variable in LLVM IR.
881
  /// Note: This is not necessarily the address space of the constant literal
882
  /// in AST. For address space agnostic language, e.g. C++, constant literal
883
  /// in AST is always in default address space.
884
  LangAS GetGlobalConstantAddressSpace() const;
885
886
  /// Return the llvm::Constant for the address of the given global variable.
887
  /// If Ty is non-null and if the global doesn't exist, then it will be created
888
  /// with the specified type instead of whatever the normal requested type
889
  /// would be. If IsForDefinition is true, it is guaranteed that an actual
890
  /// global with type Ty will be returned, not conversion of a variable with
891
  /// the same mangled name but some other type.
892
  llvm::Constant *GetAddrOfGlobalVar(const VarDecl *D,
893
                                     llvm::Type *Ty = nullptr,
894
                                     ForDefinition_t IsForDefinition
895
                                       = NotForDefinition);
896
897
  /// Return the address of the given function. If Ty is non-null, then this
898
  /// function will use the specified type if it has to create it.
899
  llvm::Constant *GetAddrOfFunction(GlobalDecl GD, llvm::Type *Ty = nullptr,
900
                                    bool ForVTable = false,
901
                                    bool DontDefer = false,
902
                                    ForDefinition_t IsForDefinition
903
                                      = NotForDefinition);
904
905
  // Return the function body address of the given function.
906
  llvm::Constant *GetFunctionStart(const ValueDecl *Decl);
907
908
  /// Get the address of the RTTI descriptor for the given type.
909
  llvm::Constant *GetAddrOfRTTIDescriptor(QualType Ty, bool ForEH = false);
910
911
  /// Get the address of a GUID.
912
  ConstantAddress GetAddrOfMSGuidDecl(const MSGuidDecl *GD);
913
914
  /// Get the address of a UnnamedGlobalConstant
915
  ConstantAddress
916
  GetAddrOfUnnamedGlobalConstantDecl(const UnnamedGlobalConstantDecl *GCD);
917
918
  /// Get the address of a template parameter object.
919
  ConstantAddress
920
  GetAddrOfTemplateParamObject(const TemplateParamObjectDecl *TPO);
921
922
  /// Get the address of the thunk for the given global decl.
923
  llvm::Constant *GetAddrOfThunk(StringRef Name, llvm::Type *FnTy,
924
                                 GlobalDecl GD);
925
926
  /// Get a reference to the target of VD.
927
  ConstantAddress GetWeakRefReference(const ValueDecl *VD);
928
929
  /// Returns the assumed alignment of an opaque pointer to the given class.
930
  CharUnits getClassPointerAlignment(const CXXRecordDecl *CD);
931
932
  /// Returns the minimum object size for an object of the given class type
933
  /// (or a class derived from it).
934
  CharUnits getMinimumClassObjectSize(const CXXRecordDecl *CD);
935
936
  /// Returns the minimum object size for an object of the given type.
937
445k
  CharUnits getMinimumObjectSize(QualType Ty) {
938
445k
    if (CXXRecordDecl *RD = Ty->getAsCXXRecordDecl())
939
366k
      return getMinimumClassObjectSize(RD);
940
78.9k
    return getContext().getTypeSizeInChars(Ty);
941
445k
  }
942
943
  /// Returns the assumed alignment of a virtual base of a class.
944
  CharUnits getVBaseAlignment(CharUnits DerivedAlign,
945
                              const CXXRecordDecl *Derived,
946
                              const CXXRecordDecl *VBase);
947
948
  /// Given a class pointer with an actual known alignment, and the
949
  /// expected alignment of an object at a dynamic offset w.r.t that
950
  /// pointer, return the alignment to assume at the offset.
951
  CharUnits getDynamicOffsetAlignment(CharUnits ActualAlign,
952
                                      const CXXRecordDecl *Class,
953
                                      CharUnits ExpectedTargetAlign);
954
955
  CharUnits
956
  computeNonVirtualBaseClassOffset(const CXXRecordDecl *DerivedClass,
957
                                   CastExpr::path_const_iterator Start,
958
                                   CastExpr::path_const_iterator End);
959
960
  /// Returns the offset from a derived class to  a class. Returns null if the
961
  /// offset is 0.
962
  llvm::Constant *
963
  GetNonVirtualBaseClassOffset(const CXXRecordDecl *ClassDecl,
964
                               CastExpr::path_const_iterator PathBegin,
965
                               CastExpr::path_const_iterator PathEnd);
966
967
  llvm::FoldingSet<BlockByrefHelpers> ByrefHelpersCache;
968
969
  /// Fetches the global unique block count.
970
175
  int getUniqueBlockCount() { return ++Block.GlobalUniqueCount; }
971
972
  /// Fetches the type of a generic block descriptor.
973
  llvm::Type *getBlockDescriptorType();
974
975
  /// The type of a generic block literal.
976
  llvm::Type *getGenericBlockLiteralType();
977
978
  /// Gets the address of a block which requires no captures.
979
  llvm::Constant *GetAddrOfGlobalBlock(const BlockExpr *BE, StringRef Name);
980
981
  /// Returns the address of a block which requires no caputres, or null if
982
  /// we've yet to emit the block for BE.
983
1.30k
  llvm::Constant *getAddrOfGlobalBlockIfEmitted(const BlockExpr *BE) {
984
1.30k
    return EmittedGlobalBlocks.lookup(BE);
985
1.30k
  }
986
987
  /// Notes that BE's global block is available via Addr. Asserts that BE
988
  /// isn't already emitted.
989
  void setAddrOfGlobalBlock(const BlockExpr *BE, llvm::Constant *Addr);
990
991
  /// Return a pointer to a constant CFString object for the given string.
992
  ConstantAddress GetAddrOfConstantCFString(const StringLiteral *Literal);
993
994
  /// Return a pointer to a constant NSString object for the given string. Or a
995
  /// user defined String object as defined via
996
  /// -fconstant-string-class=class_name option.
997
  ConstantAddress GetAddrOfConstantString(const StringLiteral *Literal);
998
999
  /// Return a constant array for the given string.
1000
  llvm::Constant *GetConstantArrayFromStringLiteral(const StringLiteral *E);
1001
1002
  /// Return a pointer to a constant array for the given string literal.
1003
  ConstantAddress
1004
  GetAddrOfConstantStringFromLiteral(const StringLiteral *S,
1005
                                     StringRef Name = ".str");
1006
1007
  /// Return a pointer to a constant array for the given ObjCEncodeExpr node.
1008
  ConstantAddress
1009
  GetAddrOfConstantStringFromObjCEncode(const ObjCEncodeExpr *);
1010
1011
  /// Returns a pointer to a character array containing the literal and a
1012
  /// terminating '\0' character. The result has pointer to array type.
1013
  ///
1014
  /// \param GlobalName If provided, the name to use for the global (if one is
1015
  /// created).
1016
  ConstantAddress
1017
  GetAddrOfConstantCString(const std::string &Str,
1018
                           const char *GlobalName = nullptr);
1019
1020
  /// Returns a pointer to a constant global variable for the given file-scope
1021
  /// compound literal expression.
1022
  ConstantAddress GetAddrOfConstantCompoundLiteral(const CompoundLiteralExpr*E);
1023
1024
  /// If it's been emitted already, returns the GlobalVariable corresponding to
1025
  /// a compound literal. Otherwise, returns null.
1026
  llvm::GlobalVariable *
1027
  getAddrOfConstantCompoundLiteralIfEmitted(const CompoundLiteralExpr *E);
1028
1029
  /// Notes that CLE's GlobalVariable is GV. Asserts that CLE isn't already
1030
  /// emitted.
1031
  void setAddrOfConstantCompoundLiteral(const CompoundLiteralExpr *CLE,
1032
                                        llvm::GlobalVariable *GV);
1033
1034
  /// Returns a pointer to a global variable representing a temporary
1035
  /// with static or thread storage duration.
1036
  ConstantAddress GetAddrOfGlobalTemporary(const MaterializeTemporaryExpr *E,
1037
                                           const Expr *Inner);
1038
1039
  /// Retrieve the record type that describes the state of an
1040
  /// Objective-C fast enumeration loop (for..in).
1041
  QualType getObjCFastEnumerationStateType();
1042
1043
  // Produce code for this constructor/destructor. This method doesn't try
1044
  // to apply any ABI rules about which other constructors/destructors
1045
  // are needed or if they are alias to each other.
1046
  llvm::Function *codegenCXXStructor(GlobalDecl GD);
1047
1048
  /// Return the address of the constructor/destructor of the given type.
1049
  llvm::Constant *
1050
  getAddrOfCXXStructor(GlobalDecl GD, const CGFunctionInfo *FnInfo = nullptr,
1051
                       llvm::FunctionType *FnType = nullptr,
1052
                       bool DontDefer = false,
1053
198k
                       ForDefinition_t IsForDefinition = NotForDefinition) {
1054
198k
    return cast<llvm::Constant>(getAddrAndTypeOfCXXStructor(GD, FnInfo, FnType,
1055
198k
                                                            DontDefer,
1056
198k
                                                            IsForDefinition)
1057
198k
                                    .getCallee());
1058
198k
  }
1059
1060
  llvm::FunctionCallee getAddrAndTypeOfCXXStructor(
1061
      GlobalDecl GD, const CGFunctionInfo *FnInfo = nullptr,
1062
      llvm::FunctionType *FnType = nullptr, bool DontDefer = false,
1063
      ForDefinition_t IsForDefinition = NotForDefinition);
1064
1065
  /// Given a builtin id for a function like "__builtin_fabsf", return a
1066
  /// Function* for "fabsf".
1067
  llvm::Constant *getBuiltinLibFunction(const FunctionDecl *FD,
1068
                                        unsigned BuiltinID);
1069
1070
  llvm::Function *getIntrinsic(unsigned IID, ArrayRef<llvm::Type*> Tys = None);
1071
1072
  /// Emit code for a single top level declaration.
1073
  void EmitTopLevelDecl(Decl *D);
1074
1075
  /// Stored a deferred empty coverage mapping for an unused
1076
  /// and thus uninstrumented top level declaration.
1077
  void AddDeferredUnusedCoverageMapping(Decl *D);
1078
1079
  /// Remove the deferred empty coverage mapping as this
1080
  /// declaration is actually instrumented.
1081
  void ClearUnusedCoverageMapping(const Decl *D);
1082
1083
  /// Emit all the deferred coverage mappings
1084
  /// for the uninstrumented functions.
1085
  void EmitDeferredUnusedCoverageMappings();
1086
1087
  /// Emit an alias for "main" if it has no arguments (needed for wasm).
1088
  void EmitMainVoidAlias();
1089
1090
  /// Tell the consumer that this variable has been instantiated.
1091
  void HandleCXXStaticMemberVarInstantiation(VarDecl *VD);
1092
1093
  /// If the declaration has internal linkage but is inside an
1094
  /// extern "C" linkage specification, prepare to emit an alias for it
1095
  /// to the expected name.
1096
  template<typename SomeDecl>
1097
  void MaybeHandleStaticInExternC(const SomeDecl *D, llvm::GlobalValue *GV);
1098
1099
  /// Add a global to a list to be added to the llvm.used metadata.
1100
  void addUsedGlobal(llvm::GlobalValue *GV);
1101
1102
  /// Add a global to a list to be added to the llvm.compiler.used metadata.
1103
  void addCompilerUsedGlobal(llvm::GlobalValue *GV);
1104
1105
  /// Add a global to a list to be added to the llvm.compiler.used metadata.
1106
  void addUsedOrCompilerUsedGlobal(llvm::GlobalValue *GV);
1107
1108
  /// Add a destructor and object to add to the C++ global destructor function.
1109
6
  void AddCXXDtorEntry(llvm::FunctionCallee DtorFn, llvm::Constant *Object) {
1110
6
    CXXGlobalDtorsOrStermFinalizers.emplace_back(DtorFn.getFunctionType(),
1111
6
                                                 DtorFn.getCallee(), Object);
1112
6
  }
1113
1114
  /// Add an sterm finalizer to the C++ global cleanup function.
1115
22
  void AddCXXStermFinalizerEntry(llvm::FunctionCallee DtorFn) {
1116
22
    CXXGlobalDtorsOrStermFinalizers.emplace_back(DtorFn.getFunctionType(),
1117
22
                                                 DtorFn.getCallee(), nullptr);
1118
22
  }
1119
1120
  /// Add an sterm finalizer to its own llvm.global_dtors entry.
1121
  void AddCXXStermFinalizerToGlobalDtor(llvm::Function *StermFinalizer,
1122
6
                                        int Priority) {
1123
6
    AddGlobalDtor(StermFinalizer, Priority);
1124
6
  }
1125
1126
  void AddCXXPrioritizedStermFinalizerEntry(llvm::Function *StermFinalizer,
1127
8
                                            int Priority) {
1128
8
    OrderGlobalInitsOrStermFinalizers Key(Priority,
1129
8
                                          PrioritizedCXXStermFinalizers.size());
1130
8
    PrioritizedCXXStermFinalizers.push_back(
1131
8
        std::make_pair(Key, StermFinalizer));
1132
8
  }
1133
1134
  /// Create or return a runtime function declaration with the specified type
1135
  /// and name. If \p AssumeConvergent is true, the call will have the
1136
  /// convergent attribute added.
1137
  llvm::FunctionCallee
1138
  CreateRuntimeFunction(llvm::FunctionType *Ty, StringRef Name,
1139
                        llvm::AttributeList ExtraAttrs = llvm::AttributeList(),
1140
                        bool Local = false, bool AssumeConvergent = false);
1141
1142
  /// Create a new runtime global variable with the specified type and name.
1143
  llvm::Constant *CreateRuntimeVariable(llvm::Type *Ty,
1144
                                        StringRef Name);
1145
1146
  ///@name Custom Blocks Runtime Interfaces
1147
  ///@{
1148
1149
  llvm::Constant *getNSConcreteGlobalBlock();
1150
  llvm::Constant *getNSConcreteStackBlock();
1151
  llvm::FunctionCallee getBlockObjectAssign();
1152
  llvm::FunctionCallee getBlockObjectDispose();
1153
1154
  ///@}
1155
1156
  llvm::Function *getLLVMLifetimeStartFn();
1157
  llvm::Function *getLLVMLifetimeEndFn();
1158
1159
  // Make sure that this type is translated.
1160
  void UpdateCompletedType(const TagDecl *TD);
1161
1162
  llvm::Constant *getMemberPointerConstant(const UnaryOperator *e);
1163
1164
  /// Emit type info if type of an expression is a variably modified
1165
  /// type. Also emit proper debug info for cast types.
1166
  void EmitExplicitCastExprType(const ExplicitCastExpr *E,
1167
                                CodeGenFunction *CGF = nullptr);
1168
1169
  /// Return the result of value-initializing the given type, i.e. a null
1170
  /// expression of the given type.  This is usually, but not always, an LLVM
1171
  /// null constant.
1172
  llvm::Constant *EmitNullConstant(QualType T);
1173
1174
  /// Return a null constant appropriate for zero-initializing a base class with
1175
  /// the given type. This is usually, but not always, an LLVM null constant.
1176
  llvm::Constant *EmitNullConstantForBase(const CXXRecordDecl *Record);
1177
1178
  /// Emit a general error that something can't be done.
1179
  void Error(SourceLocation loc, StringRef error);
1180
1181
  /// Print out an error that codegen doesn't support the specified stmt yet.
1182
  void ErrorUnsupported(const Stmt *S, const char *Type);
1183
1184
  /// Print out an error that codegen doesn't support the specified decl yet.
1185
  void ErrorUnsupported(const Decl *D, const char *Type);
1186
1187
  /// Set the attributes on the LLVM function for the given decl and function
1188
  /// info. This applies attributes necessary for handling the ABI as well as
1189
  /// user specified attributes like section.
1190
  void SetInternalFunctionAttributes(GlobalDecl GD, llvm::Function *F,
1191
                                     const CGFunctionInfo &FI);
1192
1193
  /// Set the LLVM function attributes (sext, zext, etc).
1194
  void SetLLVMFunctionAttributes(GlobalDecl GD, const CGFunctionInfo &Info,
1195
                                 llvm::Function *F, bool IsThunk);
1196
1197
  /// Set the LLVM function attributes which only apply to a function
1198
  /// definition.
1199
  void SetLLVMFunctionAttributesForDefinition(const Decl *D, llvm::Function *F);
1200
1201
  /// Set the LLVM function attributes that represent floating point
1202
  /// environment.
1203
  void setLLVMFunctionFEnvAttributes(const FunctionDecl *D, llvm::Function *F);
1204
1205
  /// Return true iff the given type uses 'sret' when used as a return type.
1206
  bool ReturnTypeUsesSRet(const CGFunctionInfo &FI);
1207
1208
  /// Return true iff the given type uses an argument slot when 'sret' is used
1209
  /// as a return type.
1210
  bool ReturnSlotInterferesWithArgs(const CGFunctionInfo &FI);
1211
1212
  /// Return true iff the given type uses 'fpret' when used as a return type.
1213
  bool ReturnTypeUsesFPRet(QualType ResultType);
1214
1215
  /// Return true iff the given type uses 'fp2ret' when used as a return type.
1216
  bool ReturnTypeUsesFP2Ret(QualType ResultType);
1217
1218
  /// Get the LLVM attributes and calling convention to use for a particular
1219
  /// function type.
1220
  ///
1221
  /// \param Name - The function name.
1222
  /// \param Info - The function type information.
1223
  /// \param CalleeInfo - The callee information these attributes are being
1224
  /// constructed for. If valid, the attributes applied to this decl may
1225
  /// contribute to the function attributes and calling convention.
1226
  /// \param Attrs [out] - On return, the attribute list to use.
1227
  /// \param CallingConv [out] - On return, the LLVM calling convention to use.
1228
  void ConstructAttributeList(StringRef Name, const CGFunctionInfo &Info,
1229
                              CGCalleeInfo CalleeInfo,
1230
                              llvm::AttributeList &Attrs, unsigned &CallingConv,
1231
                              bool AttrOnCallSite, bool IsThunk);
1232
1233
  /// Adds attributes to F according to our CodeGenOptions and LangOptions, as
1234
  /// though we had emitted it ourselves.  We remove any attributes on F that
1235
  /// conflict with the attributes we add here.
1236
  ///
1237
  /// This is useful for adding attrs to bitcode modules that you want to link
1238
  /// with but don't control, such as CUDA's libdevice.  When linking with such
1239
  /// a bitcode library, you might want to set e.g. its functions'
1240
  /// "unsafe-fp-math" attribute to match the attr of the functions you're
1241
  /// codegen'ing.  Otherwise, LLVM will interpret the bitcode module's lack of
1242
  /// unsafe-fp-math attrs as tantamount to unsafe-fp-math=false, and then LLVM
1243
  /// will propagate unsafe-fp-math=false up to every transitive caller of a
1244
  /// function in the bitcode library!
1245
  ///
1246
  /// With the exception of fast-math attrs, this will only make the attributes
1247
  /// on the function more conservative.  But it's unsafe to call this on a
1248
  /// function which relies on particular fast-math attributes for correctness.
1249
  /// It's up to you to ensure that this is safe.
1250
  void addDefaultFunctionDefinitionAttributes(llvm::Function &F);
1251
1252
  /// Like the overload taking a `Function &`, but intended specifically
1253
  /// for frontends that want to build on Clang's target-configuration logic.
1254
  void addDefaultFunctionDefinitionAttributes(llvm::AttrBuilder &attrs);
1255
1256
  StringRef getMangledName(GlobalDecl GD);
1257
  StringRef getBlockMangledName(GlobalDecl GD, const BlockDecl *BD);
1258
  const GlobalDecl getMangledNameDecl(StringRef);
1259
1260
  void EmitTentativeDefinition(const VarDecl *D);
1261
1262
  void EmitExternalDeclaration(const VarDecl *D);
1263
1264
  void EmitVTable(CXXRecordDecl *Class);
1265
1266
  void RefreshTypeCacheForClass(const CXXRecordDecl *Class);
1267
1268
  /// Appends Opts to the "llvm.linker.options" metadata value.
1269
  void AppendLinkerOptions(StringRef Opts);
1270
1271
  /// Appends a detect mismatch command to the linker options.
1272
  void AddDetectMismatch(StringRef Name, StringRef Value);
1273
1274
  /// Appends a dependent lib to the appropriate metadata value.
1275
  void AddDependentLib(StringRef Lib);
1276
1277
1278
  llvm::GlobalVariable::LinkageTypes getFunctionLinkage(GlobalDecl GD);
1279
1280
270k
  void setFunctionLinkage(GlobalDecl GD, llvm::Function *F) {
1281
270k
    F->setLinkage(getFunctionLinkage(GD));
1282
270k
  }
1283
1284
  /// Return the appropriate linkage for the vtable, VTT, and type information
1285
  /// of the given class.
1286
  llvm::GlobalVariable::LinkageTypes getVTableLinkage(const CXXRecordDecl *RD);
1287
1288
  /// Return the store size, in character units, of the given LLVM type.
1289
  CharUnits GetTargetTypeStoreSize(llvm::Type *Ty) const;
1290
1291
  /// Returns LLVM linkage for a declarator.
1292
  llvm::GlobalValue::LinkageTypes
1293
  getLLVMLinkageForDeclarator(const DeclaratorDecl *D, GVALinkage Linkage,
1294
                              bool IsConstantVariable);
1295
1296
  /// Returns LLVM linkage for a declarator.
1297
  llvm::GlobalValue::LinkageTypes
1298
  getLLVMLinkageVarDefinition(const VarDecl *VD, bool IsConstant);
1299
1300
  /// Emit all the global annotations.
1301
  void EmitGlobalAnnotations();
1302
1303
  /// Emit an annotation string.
1304
  llvm::Constant *EmitAnnotationString(StringRef Str);
1305
1306
  /// Emit the annotation's translation unit.
1307
  llvm::Constant *EmitAnnotationUnit(SourceLocation Loc);
1308
1309
  /// Emit the annotation line number.
1310
  llvm::Constant *EmitAnnotationLineNo(SourceLocation L);
1311
1312
  /// Emit additional args of the annotation.
1313
  llvm::Constant *EmitAnnotationArgs(const AnnotateAttr *Attr);
1314
1315
  /// Generate the llvm::ConstantStruct which contains the annotation
1316
  /// information for a given GlobalValue. The annotation struct is
1317
  /// {i8 *, i8 *, i8 *, i32}. The first field is a constant expression, the
1318
  /// GlobalValue being annotated. The second field is the constant string
1319
  /// created from the AnnotateAttr's annotation. The third field is a constant
1320
  /// string containing the name of the translation unit. The fourth field is
1321
  /// the line number in the file of the annotated value declaration.
1322
  llvm::Constant *EmitAnnotateAttr(llvm::GlobalValue *GV,
1323
                                   const AnnotateAttr *AA,
1324
                                   SourceLocation L);
1325
1326
  /// Add global annotations that are set on D, for the global GV. Those
1327
  /// annotations are emitted during finalization of the LLVM code.
1328
  void AddGlobalAnnotations(const ValueDecl *D, llvm::GlobalValue *GV);
1329
1330
  bool isInNoSanitizeList(SanitizerMask Kind, llvm::Function *Fn,
1331
                          SourceLocation Loc) const;
1332
1333
  bool isInNoSanitizeList(SanitizerMask Kind, llvm::GlobalVariable *GV,
1334
                          SourceLocation Loc, QualType Ty,
1335
                          StringRef Category = StringRef()) const;
1336
1337
  /// Imbue XRay attributes to a function, applying the always/never attribute
1338
  /// lists in the process. Returns true if we did imbue attributes this way,
1339
  /// false otherwise.
1340
  bool imbueXRayAttrs(llvm::Function *Fn, SourceLocation Loc,
1341
                      StringRef Category = StringRef()) const;
1342
1343
  /// \returns true if \p Fn at \p Loc should be excluded from profile
1344
  /// instrumentation by the SCL passed by \p -fprofile-list.
1345
  bool isFunctionBlockedByProfileList(llvm::Function *Fn,
1346
                                      SourceLocation Loc) const;
1347
1348
  /// \returns true if \p Fn at \p Loc should be excluded from profile
1349
  /// instrumentation.
1350
  bool isFunctionBlockedFromProfileInstr(llvm::Function *Fn,
1351
                                         SourceLocation Loc) const;
1352
1353
32.1k
  SanitizerMetadata *getSanitizerMetadata() {
1354
32.1k
    return SanitizerMD.get();
1355
32.1k
  }
1356
1357
2.96k
  void addDeferredVTable(const CXXRecordDecl *RD) {
1358
2.96k
    DeferredVTables.push_back(RD);
1359
2.96k
  }
1360
1361
  /// Emit code for a single global function or var decl. Forward declarations
1362
  /// are emitted lazily.
1363
  void EmitGlobal(GlobalDecl D);
1364
1365
  bool TryEmitBaseDestructorAsAlias(const CXXDestructorDecl *D);
1366
1367
  llvm::GlobalValue *GetGlobalValue(StringRef Ref);
1368
1369
  /// Set attributes which are common to any form of a global definition (alias,
1370
  /// Objective-C method, function, global variable).
1371
  ///
1372
  /// NOTE: This should only be called for definitions.
1373
  void SetCommonAttributes(GlobalDecl GD, llvm::GlobalValue *GV);
1374
1375
  void addReplacement(StringRef Name, llvm::Constant *C);
1376
1377
  void addGlobalValReplacement(llvm::GlobalValue *GV, llvm::Constant *C);
1378
1379
  /// Emit a code for threadprivate directive.
1380
  /// \param D Threadprivate declaration.
1381
  void EmitOMPThreadPrivateDecl(const OMPThreadPrivateDecl *D);
1382
1383
  /// Emit a code for declare reduction construct.
1384
  void EmitOMPDeclareReduction(const OMPDeclareReductionDecl *D,
1385
                               CodeGenFunction *CGF = nullptr);
1386
1387
  /// Emit a code for declare mapper construct.
1388
  void EmitOMPDeclareMapper(const OMPDeclareMapperDecl *D,
1389
                            CodeGenFunction *CGF = nullptr);
1390
1391
  /// Emit a code for requires directive.
1392
  /// \param D Requires declaration
1393
  void EmitOMPRequiresDecl(const OMPRequiresDecl *D);
1394
1395
  /// Emit a code for the allocate directive.
1396
  /// \param D The allocate declaration
1397
  void EmitOMPAllocateDecl(const OMPAllocateDecl *D);
1398
1399
  /// Return the alignment specified in an allocate directive, if present.
1400
  llvm::Optional<CharUnits> getOMPAllocateAlignment(const VarDecl *VD);
1401
1402
  /// Returns whether the given record has hidden LTO visibility and therefore
1403
  /// may participate in (single-module) CFI and whole-program vtable
1404
  /// optimization.
1405
  bool HasHiddenLTOVisibility(const CXXRecordDecl *RD);
1406
1407
  /// Returns whether the given record has public LTO visibility (regardless of
1408
  /// -lto-whole-program-visibility) and therefore may not participate in
1409
  /// (single-module) CFI and whole-program vtable optimization.
1410
  bool AlwaysHasLTOVisibilityPublic(const CXXRecordDecl *RD);
1411
1412
  /// Returns the vcall visibility of the given type. This is the scope in which
1413
  /// a virtual function call could be made which ends up being dispatched to a
1414
  /// member function of this class. This scope can be wider than the visibility
1415
  /// of the class itself when the class has a more-visible dynamic base class.
1416
  /// The client should pass in an empty Visited set, which is used to prevent
1417
  /// redundant recursive processing.
1418
  llvm::GlobalObject::VCallVisibility
1419
  GetVCallVisibilityLevel(const CXXRecordDecl *RD,
1420
                          llvm::DenseSet<const CXXRecordDecl *> &Visited);
1421
1422
  /// Emit type metadata for the given vtable using the given layout.
1423
  void EmitVTableTypeMetadata(const CXXRecordDecl *RD,
1424
                              llvm::GlobalVariable *VTable,
1425
                              const VTableLayout &VTLayout);
1426
1427
  /// Generate a cross-DSO type identifier for MD.
1428
  llvm::ConstantInt *CreateCrossDsoCfiTypeId(llvm::Metadata *MD);
1429
1430
  /// Create a metadata identifier for the given type. This may either be an
1431
  /// MDString (for external identifiers) or a distinct unnamed MDNode (for
1432
  /// internal identifiers).
1433
  llvm::Metadata *CreateMetadataIdentifierForType(QualType T);
1434
1435
  /// Create a metadata identifier that is intended to be used to check virtual
1436
  /// calls via a member function pointer.
1437
  llvm::Metadata *CreateMetadataIdentifierForVirtualMemPtrType(QualType T);
1438
1439
  /// Create a metadata identifier for the generalization of the given type.
1440
  /// This may either be an MDString (for external identifiers) or a distinct
1441
  /// unnamed MDNode (for internal identifiers).
1442
  llvm::Metadata *CreateMetadataIdentifierGeneralized(QualType T);
1443
1444
  /// Create and attach type metadata to the given function.
1445
  void CreateFunctionTypeMetadataForIcall(const FunctionDecl *FD,
1446
                                          llvm::Function *F);
1447
1448
  /// Whether this function's return type has no side effects, and thus may
1449
  /// be trivially discarded if it is unused.
1450
  bool MayDropFunctionReturn(const ASTContext &Context, QualType ReturnType);
1451
1452
  /// Returns whether this module needs the "all-vtables" type identifier.
1453
  bool NeedAllVtablesTypeId() const;
1454
1455
  /// Create and attach type metadata for the given vtable.
1456
  void AddVTableTypeMetadata(llvm::GlobalVariable *VTable, CharUnits Offset,
1457
                             const CXXRecordDecl *RD);
1458
1459
  /// Return a vector of most-base classes for RD. This is used to implement
1460
  /// control flow integrity checks for member function pointers.
1461
  ///
1462
  /// A most-base class of a class C is defined as a recursive base class of C,
1463
  /// including C itself, that does not have any bases.
1464
  std::vector<const CXXRecordDecl *>
1465
  getMostBaseClasses(const CXXRecordDecl *RD);
1466
1467
  llvm::GlobalVariable *
1468
  GetOrCreateRTTIProxyGlobalVariable(llvm::Constant *Addr);
1469
1470
  /// Get the declaration of std::terminate for the platform.
1471
  llvm::FunctionCallee getTerminateFn();
1472
1473
  llvm::SanitizerStatReport &getSanStats();
1474
1475
  llvm::Value *
1476
  createOpenCLIntToSamplerConversion(const Expr *E, CodeGenFunction &CGF);
1477
1478
  /// OpenCL v1.2 s5.6.4.6 allows the compiler to store kernel argument
1479
  /// information in the program executable. The argument information stored
1480
  /// includes the argument name, its type, the address and access qualifiers
1481
  /// used. This helper can be used to generate metadata for source code kernel
1482
  /// function as well as generated implicitly kernels. If a kernel is generated
1483
  /// implicitly null value has to be passed to the last two parameters,
1484
  /// otherwise all parameters must have valid non-null values.
1485
  /// \param FN is a pointer to IR function being generated.
1486
  /// \param FD is a pointer to function declaration if any.
1487
  /// \param CGF is a pointer to CodeGenFunction that generates this function.
1488
  void GenKernelArgMetadata(llvm::Function *FN,
1489
                            const FunctionDecl *FD = nullptr,
1490
                            CodeGenFunction *CGF = nullptr);
1491
1492
  /// Get target specific null pointer.
1493
  /// \param T is the LLVM type of the null pointer.
1494
  /// \param QT is the clang QualType of the null pointer.
1495
  llvm::Constant *getNullPointer(llvm::PointerType *T, QualType QT);
1496
1497
  CharUnits getNaturalTypeAlignment(QualType T,
1498
                                    LValueBaseInfo *BaseInfo = nullptr,
1499
                                    TBAAAccessInfo *TBAAInfo = nullptr,
1500
                                    bool forPointeeType = false);
1501
  CharUnits getNaturalPointeeTypeAlignment(QualType T,
1502
                                           LValueBaseInfo *BaseInfo = nullptr,
1503
                                           TBAAAccessInfo *TBAAInfo = nullptr);
1504
  bool stopAutoInit();
1505
1506
  /// Print the postfix for externalized static variable or kernels for single
1507
  /// source offloading languages CUDA and HIP. The unique postfix is created
1508
  /// using either the CUID argument, or the file's UniqueID and active macros.
1509
  /// The fallback method without a CUID requires that the offloading toolchain
1510
  /// does not define separate macros via the -cc1 options.
1511
  void printPostfixForExternalizedDecl(llvm::raw_ostream &OS,
1512
                                       const Decl *D) const;
1513
1514
  /// Move some lazily-emitted states to the NewBuilder. This is especially
1515
  /// essential for the incremental parsing environment like Clang Interpreter,
1516
  /// because we'll lose all important information after each repl.
1517
90
  void moveLazyEmissionStates(CodeGenModule *NewBuilder) {
1518
90
    assert(DeferredDeclsToEmit.empty() &&
1519
90
           "Should have emitted all decls deferred to emit.");
1520
0
    assert(NewBuilder->DeferredDecls.empty() &&
1521
90
           "Newly created module should not have deferred decls");
1522
0
    NewBuilder->DeferredDecls = std::move(DeferredDecls);
1523
1524
90
    assert(NewBuilder->DeferredVTables.empty() &&
1525
90
           "Newly created module should not have deferred vtables");
1526
0
    NewBuilder->DeferredVTables = std::move(DeferredVTables);
1527
1528
90
    assert(NewBuilder->MangledDeclNames.empty() &&
1529
90
           "Newly created module should not have mangled decl names");
1530
0
    assert(NewBuilder->Manglings.empty() &&
1531
90
           "Newly created module should not have manglings");
1532
0
    NewBuilder->Manglings = std::move(Manglings);
1533
1534
90
    assert(WeakRefReferences.empty() &&
1535
90
           "Not all WeakRefRefs have been applied");
1536
0
    NewBuilder->WeakRefReferences = std::move(WeakRefReferences);
1537
1538
90
    NewBuilder->TBAA = std::move(TBAA);
1539
1540
90
    assert(NewBuilder->EmittedDeferredDecls.empty() &&
1541
90
           "Still have (unmerged) EmittedDeferredDecls deferred decls");
1542
1543
0
    NewBuilder->EmittedDeferredDecls = std::move(EmittedDeferredDecls);
1544
90
  }
1545
1546
private:
1547
  llvm::Constant *GetOrCreateLLVMFunction(
1548
      StringRef MangledName, llvm::Type *Ty, GlobalDecl D, bool ForVTable,
1549
      bool DontDefer = false, bool IsThunk = false,
1550
      llvm::AttributeList ExtraAttrs = llvm::AttributeList(),
1551
      ForDefinition_t IsForDefinition = NotForDefinition);
1552
1553
  // References to multiversion functions are resolved through an implicitly
1554
  // defined resolver function. This function is responsible for creating
1555
  // the resolver symbol for the provided declaration. The value returned
1556
  // will be for an ifunc (llvm::GlobalIFunc) if the current target supports
1557
  // that feature and for a regular function (llvm::GlobalValue) otherwise.
1558
  llvm::Constant *GetOrCreateMultiVersionResolver(GlobalDecl GD);
1559
1560
  // In scenarios where a function is not known to be a multiversion function
1561
  // until a later declaration, it is sometimes necessary to change the
1562
  // previously created mangled name to align with requirements of whatever
1563
  // multiversion function kind the function is now known to be. This function
1564
  // is responsible for performing such mangled name updates.
1565
  void UpdateMultiVersionNames(GlobalDecl GD, const FunctionDecl *FD,
1566
                               StringRef &CurName);
1567
1568
  llvm::Constant *
1569
  GetOrCreateLLVMGlobal(StringRef MangledName, llvm::Type *Ty, LangAS AddrSpace,
1570
                        const VarDecl *D,
1571
                        ForDefinition_t IsForDefinition = NotForDefinition);
1572
1573
  bool GetCPUAndFeaturesAttributes(GlobalDecl GD,
1574
                                   llvm::AttrBuilder &AttrBuilder);
1575
  void setNonAliasAttributes(GlobalDecl GD, llvm::GlobalObject *GO);
1576
1577
  /// Set function attributes for a function declaration.
1578
  void SetFunctionAttributes(GlobalDecl GD, llvm::Function *F,
1579
                             bool IsIncompleteFunction, bool IsThunk);
1580
1581
  void EmitGlobalDefinition(GlobalDecl D, llvm::GlobalValue *GV = nullptr);
1582
1583
  void EmitGlobalFunctionDefinition(GlobalDecl GD, llvm::GlobalValue *GV);
1584
  void EmitMultiVersionFunctionDefinition(GlobalDecl GD, llvm::GlobalValue *GV);
1585
1586
  void EmitGlobalVarDefinition(const VarDecl *D, bool IsTentative = false);
1587
  void EmitExternalVarDeclaration(const VarDecl *D);
1588
  void EmitAliasDefinition(GlobalDecl GD);
1589
  void emitIFuncDefinition(GlobalDecl GD);
1590
  void emitCPUDispatchDefinition(GlobalDecl GD);
1591
  void EmitObjCPropertyImplementations(const ObjCImplementationDecl *D);
1592
  void EmitObjCIvarInitializations(ObjCImplementationDecl *D);
1593
1594
  // C++ related functions.
1595
1596
  void EmitDeclContext(const DeclContext *DC);
1597
  void EmitLinkageSpec(const LinkageSpecDecl *D);
1598
1599
  /// Emit the function that initializes C++ thread_local variables.
1600
  void EmitCXXThreadLocalInitFunc();
1601
1602
  /// Emit the function that initializes C++ globals.
1603
  void EmitCXXGlobalInitFunc();
1604
1605
  /// Emit the function that performs cleanup associated with C++ globals.
1606
  void EmitCXXGlobalCleanUpFunc();
1607
1608
  /// Emit the function that initializes the specified global (if PerformInit is
1609
  /// true) and registers its destructor.
1610
  void EmitCXXGlobalVarDeclInitFunc(const VarDecl *D,
1611
                                    llvm::GlobalVariable *Addr,
1612
                                    bool PerformInit);
1613
1614
  void EmitPointerToInitFunc(const VarDecl *VD, llvm::GlobalVariable *Addr,
1615
                             llvm::Function *InitFunc, InitSegAttr *ISA);
1616
1617
  // FIXME: Hardcoding priority here is gross.
1618
  void AddGlobalCtor(llvm::Function *Ctor, int Priority = 65535,
1619
                     llvm::Constant *AssociatedData = nullptr);
1620
  void AddGlobalDtor(llvm::Function *Dtor, int Priority = 65535,
1621
                     bool IsDtorAttrFunc = false);
1622
1623
  /// EmitCtorList - Generates a global array of functions and priorities using
1624
  /// the given list and name. This array will have appending linkage and is
1625
  /// suitable for use as a LLVM constructor or destructor array. Clears Fns.
1626
  void EmitCtorList(CtorList &Fns, const char *GlobalName);
1627
1628
  /// Emit any needed decls for which code generation was deferred.
1629
  void EmitDeferred();
1630
1631
  /// Try to emit external vtables as available_externally if they have emitted
1632
  /// all inlined virtual functions.  It runs after EmitDeferred() and therefore
1633
  /// is not allowed to create new references to things that need to be emitted
1634
  /// lazily.
1635
  void EmitVTablesOpportunistically();
1636
1637
  /// Call replaceAllUsesWith on all pairs in Replacements.
1638
  void applyReplacements();
1639
1640
  /// Call replaceAllUsesWith on all pairs in GlobalValReplacements.
1641
  void applyGlobalValReplacements();
1642
1643
  void checkAliases();
1644
1645
  std::map<int, llvm::TinyPtrVector<llvm::Function *>> DtorsUsingAtExit;
1646
1647
  /// Register functions annotated with __attribute__((destructor)) using
1648
  /// __cxa_atexit, if it is available, or atexit otherwise.
1649
  void registerGlobalDtorsWithAtExit();
1650
1651
  // When using sinit and sterm functions, unregister
1652
  // __attribute__((destructor)) annotated functions which were previously
1653
  // registered by the atexit subroutine using unatexit.
1654
  void unregisterGlobalDtorsWithUnAtExit();
1655
1656
  /// Emit deferred multiversion function resolvers and associated variants.
1657
  void emitMultiVersionFunctions();
1658
1659
  /// Emit any vtables which we deferred and still have a use for.
1660
  void EmitDeferredVTables();
1661
1662
  /// Emit a dummy function that reference a CoreFoundation symbol when
1663
  /// @available is used on Darwin.
1664
  void emitAtAvailableLinkGuard();
1665
1666
  /// Emit the llvm.used and llvm.compiler.used metadata.
1667
  void emitLLVMUsed();
1668
1669
  /// Emit the link options introduced by imported modules.
1670
  void EmitModuleLinkOptions();
1671
1672
  /// Helper function for EmitStaticExternCAliases() to redirect ifuncs that
1673
  /// have a resolver name that matches 'Elem' to instead resolve to the name of
1674
  /// 'CppFunc'. This redirection is necessary in cases where 'Elem' has a name
1675
  /// that will be emitted as an alias of the name bound to 'CppFunc'; ifuncs
1676
  /// may not reference aliases. Redirection is only performed if 'Elem' is only
1677
  /// used by ifuncs in which case, 'Elem' is destroyed. 'true' is returned if
1678
  /// redirection is successful, and 'false' is returned otherwise.
1679
  bool CheckAndReplaceExternCIFuncs(llvm::GlobalValue *Elem,
1680
                                    llvm::GlobalValue *CppFunc);
1681
1682
  /// Emit aliases for internal-linkage declarations inside "C" language
1683
  /// linkage specifications, giving them the "expected" name where possible.
1684
  void EmitStaticExternCAliases();
1685
1686
  void EmitDeclMetadata();
1687
1688
  /// Emit the Clang version as llvm.ident metadata.
1689
  void EmitVersionIdentMetadata();
1690
1691
  /// Emit the Clang commandline as llvm.commandline metadata.
1692
  void EmitCommandLineMetadata();
1693
1694
  /// Emit the module flag metadata used to pass options controlling the
1695
  /// the backend to LLVM.
1696
  void EmitBackendOptionsMetadata(const CodeGenOptions CodeGenOpts);
1697
1698
  /// Emits OpenCL specific Metadata e.g. OpenCL version.
1699
  void EmitOpenCLMetadata();
1700
1701
  /// Emit the llvm.gcov metadata used to tell LLVM where to emit the .gcno and
1702
  /// .gcda files in a way that persists in .bc files.
1703
  void EmitCoverageFile();
1704
1705
  /// Determine whether the definition must be emitted; if this returns \c
1706
  /// false, the definition can be emitted lazily if it's used.
1707
  bool MustBeEmitted(const ValueDecl *D);
1708
1709
  /// Determine whether the definition can be emitted eagerly, or should be
1710
  /// delayed until the end of the translation unit. This is relevant for
1711
  /// definitions whose linkage can change, e.g. implicit function instantions
1712
  /// which may later be explicitly instantiated.
1713
  bool MayBeEmittedEagerly(const ValueDecl *D);
1714
1715
  /// Check whether we can use a "simpler", more core exceptions personality
1716
  /// function.
1717
  void SimplifyPersonality();
1718
1719
  /// Helper function for ConstructAttributeList and
1720
  /// addDefaultFunctionDefinitionAttributes.  Builds a set of function
1721
  /// attributes to add to a function with the given properties.
1722
  void getDefaultFunctionAttributes(StringRef Name, bool HasOptnone,
1723
                                    bool AttrOnCallSite,
1724
                                    llvm::AttrBuilder &FuncAttrs);
1725
1726
  llvm::Metadata *CreateMetadataIdentifierImpl(QualType T, MetadataTypeMap &Map,
1727
                                               StringRef Suffix);
1728
};
1729
1730
}  // end namespace CodeGen
1731
}  // end namespace clang
1732
1733
#endif // LLVM_CLANG_LIB_CODEGEN_CODEGENMODULE_H