Coverage Report

Created: 2022-07-16 07:03

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/CodeGen/CodeGenModule.cpp
Line
Count
Source (jump to first uncovered line)
1
//===--- CodeGenModule.cpp - Emit LLVM Code from ASTs for a Module --------===//
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 coordinates the per-module state used while generating code.
10
//
11
//===----------------------------------------------------------------------===//
12
13
#include "CodeGenModule.h"
14
#include "CGBlocks.h"
15
#include "CGCUDARuntime.h"
16
#include "CGCXXABI.h"
17
#include "CGCall.h"
18
#include "CGDebugInfo.h"
19
#include "CGHLSLRuntime.h"
20
#include "CGObjCRuntime.h"
21
#include "CGOpenCLRuntime.h"
22
#include "CGOpenMPRuntime.h"
23
#include "CGOpenMPRuntimeGPU.h"
24
#include "CodeGenFunction.h"
25
#include "CodeGenPGO.h"
26
#include "ConstantEmitter.h"
27
#include "CoverageMappingGen.h"
28
#include "TargetInfo.h"
29
#include "clang/AST/ASTContext.h"
30
#include "clang/AST/CharUnits.h"
31
#include "clang/AST/DeclCXX.h"
32
#include "clang/AST/DeclObjC.h"
33
#include "clang/AST/DeclTemplate.h"
34
#include "clang/AST/Mangle.h"
35
#include "clang/AST/RecordLayout.h"
36
#include "clang/AST/RecursiveASTVisitor.h"
37
#include "clang/AST/StmtVisitor.h"
38
#include "clang/Basic/Builtins.h"
39
#include "clang/Basic/CharInfo.h"
40
#include "clang/Basic/CodeGenOptions.h"
41
#include "clang/Basic/Diagnostic.h"
42
#include "clang/Basic/FileManager.h"
43
#include "clang/Basic/Module.h"
44
#include "clang/Basic/SourceManager.h"
45
#include "clang/Basic/TargetInfo.h"
46
#include "clang/Basic/Version.h"
47
#include "clang/CodeGen/BackendUtil.h"
48
#include "clang/CodeGen/ConstantInitBuilder.h"
49
#include "clang/Frontend/FrontendDiagnostic.h"
50
#include "llvm/ADT/StringSwitch.h"
51
#include "llvm/ADT/Triple.h"
52
#include "llvm/Analysis/TargetLibraryInfo.h"
53
#include "llvm/Frontend/OpenMP/OMPIRBuilder.h"
54
#include "llvm/IR/CallingConv.h"
55
#include "llvm/IR/DataLayout.h"
56
#include "llvm/IR/Intrinsics.h"
57
#include "llvm/IR/LLVMContext.h"
58
#include "llvm/IR/Module.h"
59
#include "llvm/IR/ProfileSummary.h"
60
#include "llvm/ProfileData/InstrProfReader.h"
61
#include "llvm/Support/CRC.h"
62
#include "llvm/Support/CodeGen.h"
63
#include "llvm/Support/CommandLine.h"
64
#include "llvm/Support/ConvertUTF.h"
65
#include "llvm/Support/ErrorHandling.h"
66
#include "llvm/Support/MD5.h"
67
#include "llvm/Support/TimeProfiler.h"
68
#include "llvm/Support/X86TargetParser.h"
69
70
using namespace clang;
71
using namespace CodeGen;
72
73
static llvm::cl::opt<bool> LimitedCoverage(
74
    "limited-coverage-experimental", llvm::cl::Hidden,
75
    llvm::cl::desc("Emit limited coverage mapping information (experimental)"));
76
77
static const char AnnotationSection[] = "llvm.metadata";
78
79
36.4k
static CGCXXABI *createCXXABI(CodeGenModule &CGM) {
80
36.4k
  switch (CGM.getContext().getCXXABIKind()) {
81
106
  case TargetCXXABI::AppleARM64:
82
136
  case TargetCXXABI::Fuchsia:
83
2.44k
  case TargetCXXABI::GenericAArch64:
84
2.93k
  case TargetCXXABI::GenericARM:
85
3.02k
  case TargetCXXABI::iOS:
86
3.05k
  case TargetCXXABI::WatchOS:
87
3.20k
  case TargetCXXABI::GenericMIPS:
88
35.4k
  case TargetCXXABI::GenericItanium:
89
35.5k
  case TargetCXXABI::WebAssembly:
90
35.6k
  case TargetCXXABI::XL:
91
35.6k
    return CreateItaniumCXXABI(CGM);
92
810
  case TargetCXXABI::Microsoft:
93
810
    return CreateMicrosoftCXXABI(CGM);
94
36.4k
  }
95
96
0
  llvm_unreachable("invalid C++ ABI kind");
97
0
}
98
99
CodeGenModule::CodeGenModule(ASTContext &C, const HeaderSearchOptions &HSO,
100
                             const PreprocessorOptions &PPO,
101
                             const CodeGenOptions &CGO, llvm::Module &M,
102
                             DiagnosticsEngine &diags,
103
                             CoverageSourceInfo *CoverageInfo)
104
    : Context(C), LangOpts(C.getLangOpts()), HeaderSearchOpts(HSO),
105
      PreprocessorOpts(PPO), CodeGenOpts(CGO), TheModule(M), Diags(diags),
106
      Target(C.getTargetInfo()), ABI(createCXXABI(*this)),
107
      VMContext(M.getContext()), Types(*this), VTables(*this),
108
36.4k
      SanitizerMD(new SanitizerMetadata(*this)) {
109
110
  // Initialize the type cache.
111
36.4k
  llvm::LLVMContext &LLVMContext = M.getContext();
112
36.4k
  VoidTy = llvm::Type::getVoidTy(LLVMContext);
113
36.4k
  Int8Ty = llvm::Type::getInt8Ty(LLVMContext);
114
36.4k
  Int16Ty = llvm::Type::getInt16Ty(LLVMContext);
115
36.4k
  Int32Ty = llvm::Type::getInt32Ty(LLVMContext);
116
36.4k
  Int64Ty = llvm::Type::getInt64Ty(LLVMContext);
117
36.4k
  HalfTy = llvm::Type::getHalfTy(LLVMContext);
118
36.4k
  BFloatTy = llvm::Type::getBFloatTy(LLVMContext);
119
36.4k
  FloatTy = llvm::Type::getFloatTy(LLVMContext);
120
36.4k
  DoubleTy = llvm::Type::getDoubleTy(LLVMContext);
121
36.4k
  PointerWidthInBits = C.getTargetInfo().getPointerWidth(0);
122
36.4k
  PointerAlignInBytes =
123
36.4k
    C.toCharUnitsFromBits(C.getTargetInfo().getPointerAlign(0)).getQuantity();
124
36.4k
  SizeSizeInBytes =
125
36.4k
    C.toCharUnitsFromBits(C.getTargetInfo().getMaxPointerWidth()).getQuantity();
126
36.4k
  IntAlignInBytes =
127
36.4k
    C.toCharUnitsFromBits(C.getTargetInfo().getIntAlign()).getQuantity();
128
36.4k
  CharTy =
129
36.4k
    llvm::IntegerType::get(LLVMContext, C.getTargetInfo().getCharWidth());
130
36.4k
  IntTy = llvm::IntegerType::get(LLVMContext, C.getTargetInfo().getIntWidth());
131
36.4k
  IntPtrTy = llvm::IntegerType::get(LLVMContext,
132
36.4k
    C.getTargetInfo().getMaxPointerWidth());
133
36.4k
  Int8PtrTy = Int8Ty->getPointerTo(0);
134
36.4k
  Int8PtrPtrTy = Int8PtrTy->getPointerTo(0);
135
36.4k
  const llvm::DataLayout &DL = M.getDataLayout();
136
36.4k
  AllocaInt8PtrTy = Int8Ty->getPointerTo(DL.getAllocaAddrSpace());
137
36.4k
  GlobalsInt8PtrTy = Int8Ty->getPointerTo(DL.getDefaultGlobalsAddressSpace());
138
36.4k
  ASTAllocaAddressSpace = getTargetCodeGenInfo().getASTAllocaAddressSpace();
139
140
36.4k
  RuntimeCC = getTargetCodeGenInfo().getABIInfo().getRuntimeCC();
141
142
36.4k
  if (LangOpts.ObjC)
143
16.4k
    createObjCRuntime();
144
36.4k
  if (LangOpts.OpenCL)
145
412
    createOpenCLRuntime();
146
36.4k
  if (LangOpts.OpenMP)
147
5.88k
    createOpenMPRuntime();
148
36.4k
  if (LangOpts.CUDA)
149
216
    createCUDARuntime();
150
36.4k
  if (LangOpts.HLSL)
151
5
    createHLSLRuntime();
152
153
  // Enable TBAA unless it's suppressed. ThreadSanitizer needs TBAA even at O0.
154
36.4k
  if (LangOpts.Sanitize.has(SanitizerKind::Thread) ||
155
36.4k
      
(36.4k
!CodeGenOpts.RelaxedAliasing36.4k
&&
CodeGenOpts.OptimizationLevel > 036.3k
))
156
3.21k
    TBAA.reset(new CodeGenTBAA(Context, TheModule, CodeGenOpts, getLangOpts(),
157
3.21k
                               getCXXABI().getMangleContext()));
158
159
  // If debug info or coverage generation is enabled, create the CGDebugInfo
160
  // object.
161
36.4k
  if (CodeGenOpts.getDebugInfo() != codegenoptions::NoDebugInfo ||
162
36.4k
      
CodeGenOpts.EmitGcovArcs25.6k
||
CodeGenOpts.EmitGcovNotes25.6k
)
163
10.7k
    DebugInfo.reset(new CGDebugInfo(*this));
164
165
36.4k
  Block.GlobalUniqueCount = 0;
166
167
36.4k
  if (C.getLangOpts().ObjC)
168
16.4k
    ObjCData.reset(new ObjCEntrypoints());
169
170
36.4k
  if (CodeGenOpts.hasProfileClangUse()) {
171
47
    auto ReaderOrErr = llvm::IndexedInstrProfReader::create(
172
47
        CodeGenOpts.ProfileInstrumentUsePath, CodeGenOpts.ProfileRemappingFile);
173
47
    if (auto E = ReaderOrErr.takeError()) {
174
1
      unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
175
1
                                              "Could not read profile %0: %1");
176
1
      llvm::handleAllErrors(std::move(E), [&](const llvm::ErrorInfoBase &EI) {
177
1
        getDiags().Report(DiagID) << CodeGenOpts.ProfileInstrumentUsePath
178
1
                                  << EI.message();
179
1
      });
180
1
    } else
181
46
      PGOReader = std::move(ReaderOrErr.get());
182
47
  }
183
184
  // If coverage mapping generation is enabled, create the
185
  // CoverageMappingModuleGen object.
186
36.4k
  if (CodeGenOpts.CoverageMapping)
187
81
    CoverageMapping.reset(new CoverageMappingModuleGen(*this, *CoverageInfo));
188
189
  // Generate the module name hash here if needed.
190
36.4k
  if (CodeGenOpts.UniqueInternalLinkageNames &&
191
36.4k
      
!getModule().getSourceFileName().empty()5
) {
192
5
    std::string Path = getModule().getSourceFileName();
193
    // Check if a path substitution is needed from the MacroPrefixMap.
194
5
    for (const auto &Entry : LangOpts.MacroPrefixMap)
195
0
      if (Path.rfind(Entry.first, 0) != std::string::npos) {
196
0
        Path = Entry.second + Path.substr(Entry.first.size());
197
0
        break;
198
0
      }
199
5
    llvm::MD5 Md5;
200
5
    Md5.update(Path);
201
5
    llvm::MD5::MD5Result R;
202
5
    Md5.final(R);
203
5
    SmallString<32> Str;
204
5
    llvm::MD5::stringifyResult(R, Str);
205
    // Convert MD5hash to Decimal. Demangler suffixes can either contain
206
    // numbers or characters but not both.
207
5
    llvm::APInt IntHash(128, Str.str(), 16);
208
    // Prepend "__uniq" before the hash for tools like profilers to understand
209
    // that this symbol is of internal linkage type.  The "__uniq" is the
210
    // pre-determined prefix that is used to tell tools that this symbol was
211
    // created with -funique-internal-linakge-symbols and the tools can strip or
212
    // keep the prefix as needed.
213
5
    ModuleNameHash = (Twine(".__uniq.") +
214
5
        Twine(toString(IntHash, /* Radix = */ 10, /* Signed = */false))).str();
215
5
  }
216
36.4k
}
217
218
36.3k
CodeGenModule::~CodeGenModule() {}
219
220
16.4k
void CodeGenModule::createObjCRuntime() {
221
  // This is just isGNUFamily(), but we want to force implementors of
222
  // new ABIs to decide how best to do this.
223
16.4k
  switch (LangOpts.ObjCRuntime.getKind()) {
224
40
  case ObjCRuntime::GNUstep:
225
69
  case ObjCRuntime::GCC:
226
90
  case ObjCRuntime::ObjFW:
227
90
    ObjCRuntime.reset(CreateGNUObjCRuntime(*this));
228
90
    return;
229
230
168
  case ObjCRuntime::FragileMacOSX:
231
16.3k
  case ObjCRuntime::MacOSX:
232
16.3k
  case ObjCRuntime::iOS:
233
16.3k
  case ObjCRuntime::WatchOS:
234
16.3k
    ObjCRuntime.reset(CreateMacObjCRuntime(*this));
235
16.3k
    return;
236
16.4k
  }
237
0
  llvm_unreachable("bad runtime kind");
238
0
}
239
240
412
void CodeGenModule::createOpenCLRuntime() {
241
412
  OpenCLRuntime.reset(new CGOpenCLRuntime(*this));
242
412
}
243
244
5.88k
void CodeGenModule::createOpenMPRuntime() {
245
  // Select a specialized code generation class based on the target, if any.
246
  // If it does not exist use the default implementation.
247
5.88k
  switch (getTriple().getArch()) {
248
56
  case llvm::Triple::nvptx:
249
161
  case llvm::Triple::nvptx64:
250
178
  case llvm::Triple::amdgcn:
251
178
    assert(getLangOpts().OpenMPIsDevice &&
252
178
           "OpenMP AMDGPU/NVPTX is only prepared to deal with device code.");
253
0
    OpenMPRuntime.reset(new CGOpenMPRuntimeGPU(*this));
254
178
    break;
255
5.70k
  default:
256
5.70k
    if (LangOpts.OpenMPSimd)
257
2.46k
      OpenMPRuntime.reset(new CGOpenMPSIMDRuntime(*this));
258
3.23k
    else
259
3.23k
      OpenMPRuntime.reset(new CGOpenMPRuntime(*this));
260
5.70k
    break;
261
5.88k
  }
262
5.88k
}
263
264
216
void CodeGenModule::createCUDARuntime() {
265
216
  CUDARuntime.reset(CreateNVCUDARuntime(*this));
266
216
}
267
268
5
void CodeGenModule::createHLSLRuntime() {
269
5
  HLSLRuntime.reset(new CGHLSLRuntime(*this));
270
5
}
271
272
147
void CodeGenModule::addReplacement(StringRef Name, llvm::Constant *C) {
273
147
  Replacements[Name] = C;
274
147
}
275
276
35.9k
void CodeGenModule::applyReplacements() {
277
35.9k
  for (auto &I : Replacements) {
278
129
    StringRef MangledName = I.first();
279
129
    llvm::Constant *Replacement = I.second;
280
129
    llvm::GlobalValue *Entry = GetGlobalValue(MangledName);
281
129
    if (!Entry)
282
0
      continue;
283
129
    auto *OldF = cast<llvm::Function>(Entry);
284
129
    auto *NewF = dyn_cast<llvm::Function>(Replacement);
285
129
    if (!NewF) {
286
21
      if (auto *Alias = dyn_cast<llvm::GlobalAlias>(Replacement)) {
287
0
        NewF = dyn_cast<llvm::Function>(Alias->getAliasee());
288
21
      } else {
289
21
        auto *CE = cast<llvm::ConstantExpr>(Replacement);
290
21
        assert(CE->getOpcode() == llvm::Instruction::BitCast ||
291
21
               CE->getOpcode() == llvm::Instruction::GetElementPtr);
292
0
        NewF = dyn_cast<llvm::Function>(CE->getOperand(0));
293
21
      }
294
21
    }
295
296
    // Replace old with new, but keep the old order.
297
0
    OldF->replaceAllUsesWith(Replacement);
298
129
    if (NewF) {
299
127
      NewF->removeFromParent();
300
127
      OldF->getParent()->getFunctionList().insertAfter(OldF->getIterator(),
301
127
                                                       NewF);
302
127
    }
303
129
    OldF->eraseFromParent();
304
129
  }
305
35.9k
}
306
307
53
void CodeGenModule::addGlobalValReplacement(llvm::GlobalValue *GV, llvm::Constant *C) {
308
53
  GlobalValReplacements.push_back(std::make_pair(GV, C));
309
53
}
310
311
35.9k
void CodeGenModule::applyGlobalValReplacements() {
312
35.9k
  for (auto &I : GlobalValReplacements) {
313
53
    llvm::GlobalValue *GV = I.first;
314
53
    llvm::Constant *C = I.second;
315
316
53
    GV->replaceAllUsesWith(C);
317
53
    GV->eraseFromParent();
318
53
  }
319
35.9k
}
320
321
// This is only used in aliases that we created and we know they have a
322
// linear structure.
323
166
static const llvm::GlobalValue *getAliasedGlobal(const llvm::GlobalValue *GV) {
324
166
  const llvm::Constant *C;
325
166
  if (auto *GA = dyn_cast<llvm::GlobalAlias>(GV))
326
146
    C = GA->getAliasee();
327
20
  else if (auto *GI = dyn_cast<llvm::GlobalIFunc>(GV))
328
20
    C = GI->getResolver();
329
0
  else
330
0
    return GV;
331
332
166
  const auto *AliaseeGV = dyn_cast<llvm::GlobalValue>(C->stripPointerCasts());
333
166
  if (!AliaseeGV)
334
0
    return nullptr;
335
336
166
  const llvm::GlobalValue *FinalGV = AliaseeGV->getAliaseeObject();
337
166
  if (FinalGV == GV)
338
1
    return nullptr;
339
340
165
  return FinalGV;
341
166
}
342
343
static bool checkAliasedGlobal(DiagnosticsEngine &Diags,
344
                               SourceLocation Location, bool IsIFunc,
345
                               const llvm::GlobalValue *Alias,
346
166
                               const llvm::GlobalValue *&GV) {
347
166
  GV = getAliasedGlobal(Alias);
348
166
  if (!GV) {
349
4
    Diags.Report(Location, diag::err_cyclic_alias) << IsIFunc;
350
4
    return false;
351
4
  }
352
353
162
  if (GV->isDeclaration()) {
354
7
    Diags.Report(Location, diag::err_alias_to_undefined) << IsIFunc << IsIFunc;
355
7
    return false;
356
7
  }
357
358
155
  if (IsIFunc) {
359
    // Check resolver function type.
360
17
    const auto *F = dyn_cast<llvm::Function>(GV);
361
17
    if (!F) {
362
2
      Diags.Report(Location, diag::err_alias_to_undefined)
363
2
          << IsIFunc << IsIFunc;
364
2
      return false;
365
2
    }
366
367
15
    llvm::FunctionType *FTy = F->getFunctionType();
368
15
    if (!FTy->getReturnType()->isPointerTy()) {
369
1
      Diags.Report(Location, diag::err_ifunc_resolver_return);
370
1
      return false;
371
1
    }
372
15
  }
373
374
152
  return true;
375
155
}
376
377
35.9k
void CodeGenModule::checkAliases() {
378
  // Check if the constructed aliases are well formed. It is really unfortunate
379
  // that we have to do this in CodeGen, but we only construct mangled names
380
  // and aliases during codegen.
381
35.9k
  bool Error = false;
382
35.9k
  DiagnosticsEngine &Diags = getDiags();
383
35.9k
  for (const GlobalDecl &GD : Aliases) {
384
166
    const auto *D = cast<ValueDecl>(GD.getDecl());
385
166
    SourceLocation Location;
386
166
    bool IsIFunc = D->hasAttr<IFuncAttr>();
387
166
    if (const Attr *A = D->getDefiningAttr())
388
166
      Location = A->getLocation();
389
0
    else
390
0
      llvm_unreachable("Not an alias or ifunc?");
391
392
166
    StringRef MangledName = getMangledName(GD);
393
166
    llvm::GlobalValue *Alias = GetGlobalValue(MangledName);
394
166
    const llvm::GlobalValue *GV = nullptr;
395
166
    if (!checkAliasedGlobal(Diags, Location, IsIFunc, Alias, GV)) {
396
14
      Error = true;
397
14
      continue;
398
14
    }
399
400
152
    llvm::Constant *Aliasee =
401
152
        IsIFunc ? 
cast<llvm::GlobalIFunc>(Alias)->getResolver()14
402
152
                : 
cast<llvm::GlobalAlias>(Alias)->getAliasee()138
;
403
404
152
    llvm::GlobalValue *AliaseeGV;
405
152
    if (auto CE = dyn_cast<llvm::ConstantExpr>(Aliasee))
406
34
      AliaseeGV = cast<llvm::GlobalValue>(CE->getOperand(0));
407
118
    else
408
118
      AliaseeGV = cast<llvm::GlobalValue>(Aliasee);
409
410
152
    if (const SectionAttr *SA = D->getAttr<SectionAttr>()) {
411
6
      StringRef AliasSection = SA->getName();
412
6
      if (AliasSection != AliaseeGV->getSection())
413
5
        Diags.Report(SA->getLocation(), diag::warn_alias_with_section)
414
5
            << AliasSection << IsIFunc << IsIFunc;
415
6
    }
416
417
    // We have to handle alias to weak aliases in here. LLVM itself disallows
418
    // this since the object semantics would not match the IL one. For
419
    // compatibility with gcc we implement it by just pointing the alias
420
    // to its aliasee's aliasee. We also warn, since the user is probably
421
    // expecting the link to be weak.
422
152
    if (auto *GA = dyn_cast<llvm::GlobalAlias>(AliaseeGV)) {
423
23
      if (GA->isInterposable()) {
424
12
        Diags.Report(Location, diag::warn_alias_to_weak_alias)
425
12
            << GV->getName() << GA->getName() << IsIFunc;
426
12
        Aliasee = llvm::ConstantExpr::getPointerBitCastOrAddrSpaceCast(
427
12
            GA->getAliasee(), Alias->getType());
428
429
12
        if (IsIFunc)
430
1
          cast<llvm::GlobalIFunc>(Alias)->setResolver(Aliasee);
431
11
        else
432
11
          cast<llvm::GlobalAlias>(Alias)->setAliasee(Aliasee);
433
12
      }
434
23
    }
435
152
  }
436
35.9k
  if (!Error)
437
35.9k
    return;
438
439
37
  
for (const GlobalDecl &GD : Aliases)3
{
440
37
    StringRef MangledName = getMangledName(GD);
441
37
    llvm::GlobalValue *Alias = GetGlobalValue(MangledName);
442
37
    Alias->replaceAllUsesWith(llvm::UndefValue::get(Alias->getType()));
443
37
    Alias->eraseFromParent();
444
37
  }
445
3
}
446
447
532
void CodeGenModule::clear() {
448
532
  DeferredDeclsToEmit.clear();
449
532
  EmittedDeferredDecls.clear();
450
532
  if (OpenMPRuntime)
451
23
    OpenMPRuntime->clear();
452
532
}
453
454
void InstrProfStats::reportDiagnostics(DiagnosticsEngine &Diags,
455
7
                                       StringRef MainFile) {
456
7
  if (!hasDiagnostics())
457
0
    return;
458
7
  if (VisitedInMainFile > 0 && VisitedInMainFile == MissingInMainFile) {
459
3
    if (MainFile.empty())
460
2
      MainFile = "<stdin>";
461
3
    Diags.Report(diag::warn_profile_data_unprofiled) << MainFile;
462
4
  } else {
463
4
    if (Mismatched > 0)
464
4
      Diags.Report(diag::warn_profile_data_out_of_date) << Visited << Mismatched;
465
466
4
    if (Missing > 0)
467
2
      Diags.Report(diag::warn_profile_data_missing) << Visited << Missing;
468
4
  }
469
7
}
470
471
static void setVisibilityFromDLLStorageClass(const clang::LangOptions &LO,
472
35.9k
                                             llvm::Module &M) {
473
35.9k
  if (!LO.VisibilityFromDLLStorageClass)
474
35.8k
    return;
475
476
5
  llvm::GlobalValue::VisibilityTypes DLLExportVisibility =
477
5
      CodeGenModule::GetLLVMVisibility(LO.getDLLExportVisibility());
478
5
  llvm::GlobalValue::VisibilityTypes NoDLLStorageClassVisibility =
479
5
      CodeGenModule::GetLLVMVisibility(LO.getNoDLLStorageClassVisibility());
480
5
  llvm::GlobalValue::VisibilityTypes ExternDeclDLLImportVisibility =
481
5
      CodeGenModule::GetLLVMVisibility(LO.getExternDeclDLLImportVisibility());
482
5
  llvm::GlobalValue::VisibilityTypes ExternDeclNoDLLStorageClassVisibility =
483
5
      CodeGenModule::GetLLVMVisibility(
484
5
          LO.getExternDeclNoDLLStorageClassVisibility());
485
486
207
  for (llvm::GlobalValue &GV : M.global_values()) {
487
207
    if (GV.hasAppendingLinkage() || GV.hasLocalLinkage())
488
3
      continue;
489
490
    // Reset DSO locality before setting the visibility. This removes
491
    // any effects that visibility options and annotations may have
492
    // had on the DSO locality. Setting the visibility will implicitly set
493
    // appropriate globals to DSO Local; however, this will be pessimistic
494
    // w.r.t. to the normal compiler IRGen.
495
204
    GV.setDSOLocal(false);
496
497
204
    if (GV.isDeclarationForLinker()) {
498
15
      GV.setVisibility(GV.getDLLStorageClass() ==
499
15
                               llvm::GlobalValue::DLLImportStorageClass
500
15
                           ? 
ExternDeclDLLImportVisibility6
501
15
                           : 
ExternDeclNoDLLStorageClassVisibility9
);
502
189
    } else {
503
189
      GV.setVisibility(GV.getDLLStorageClass() ==
504
189
                               llvm::GlobalValue::DLLExportStorageClass
505
189
                           ? 
DLLExportVisibility9
506
189
                           : 
NoDLLStorageClassVisibility180
);
507
189
    }
508
509
204
    GV.setDLLStorageClass(llvm::GlobalValue::DefaultStorageClass);
510
204
  }
511
5
}
512
513
35.9k
void CodeGenModule::Release() {
514
35.9k
  EmitDeferred();
515
35.9k
  DeferredDecls.insert(EmittedDeferredDecls.begin(),
516
35.9k
                       EmittedDeferredDecls.end());
517
35.9k
  EmittedDeferredDecls.clear();
518
35.9k
  EmitVTablesOpportunistically();
519
35.9k
  applyGlobalValReplacements();
520
35.9k
  applyReplacements();
521
35.9k
  emitMultiVersionFunctions();
522
35.9k
  EmitCXXGlobalInitFunc();
523
35.9k
  EmitCXXGlobalCleanUpFunc();
524
35.9k
  registerGlobalDtorsWithAtExit();
525
35.9k
  EmitCXXThreadLocalInitFunc();
526
35.9k
  if (ObjCRuntime)
527
16.2k
    if (llvm::Function *ObjCInitFunction = ObjCRuntime->ModuleInitFunction())
528
50
      AddGlobalCtor(ObjCInitFunction);
529
35.9k
  if (Context.getLangOpts().CUDA && 
CUDARuntime201
) {
530
201
    if (llvm::Function *CudaCtorFunction = CUDARuntime->finalizeModule())
531
51
      AddGlobalCtor(CudaCtorFunction);
532
201
  }
533
35.9k
  if (OpenMPRuntime) {
534
5.86k
    if (llvm::Function *OpenMPRequiresDirectiveRegFun =
535
5.86k
            OpenMPRuntime->emitRequiresDirectiveRegFun()) {
536
1.95k
      AddGlobalCtor(OpenMPRequiresDirectiveRegFun, 0);
537
1.95k
    }
538
5.86k
    OpenMPRuntime->createOffloadEntriesAndInfoMetadata();
539
5.86k
    OpenMPRuntime->clear();
540
5.86k
  }
541
35.9k
  if (PGOReader) {
542
46
    getModule().setProfileSummary(
543
46
        PGOReader->getSummary(/* UseCS */ false).getMD(VMContext),
544
46
        llvm::ProfileSummary::PSK_Instr);
545
46
    if (PGOStats.hasDiagnostics())
546
7
      PGOStats.reportDiagnostics(getDiags(), getCodeGenOpts().MainFileName);
547
46
  }
548
35.9k
  EmitCtorList(GlobalCtors, "llvm.global_ctors");
549
35.9k
  EmitCtorList(GlobalDtors, "llvm.global_dtors");
550
35.9k
  EmitGlobalAnnotations();
551
35.9k
  EmitStaticExternCAliases();
552
35.9k
  checkAliases();
553
35.9k
  EmitDeferredUnusedCoverageMappings();
554
35.9k
  CodeGenPGO(*this).setValueProfilingFlag(getModule());
555
35.9k
  if (CoverageMapping)
556
81
    CoverageMapping->emit();
557
35.9k
  if (CodeGenOpts.SanitizeCfiCrossDso) {
558
14
    CodeGenFunction(*this).EmitCfiCheckFail();
559
14
    CodeGenFunction(*this).EmitCfiCheckStub();
560
14
  }
561
35.9k
  emitAtAvailableLinkGuard();
562
35.9k
  if (Context.getTargetInfo().getTriple().isWasm())
563
40
    EmitMainVoidAlias();
564
565
35.9k
  if (getTriple().isAMDGPU()) {
566
    // Emit reference of __amdgpu_device_library_preserve_asan_functions to
567
    // preserve ASAN functions in bitcode libraries.
568
299
    if (LangOpts.Sanitize.has(SanitizerKind::Address)) {
569
2
      auto *FT = llvm::FunctionType::get(VoidTy, {});
570
2
      auto *F = llvm::Function::Create(
571
2
          FT, llvm::GlobalValue::ExternalLinkage,
572
2
          "__amdgpu_device_library_preserve_asan_functions", &getModule());
573
2
      auto *Var = new llvm::GlobalVariable(
574
2
          getModule(), FT->getPointerTo(),
575
2
          /*isConstant=*/true, llvm::GlobalValue::WeakAnyLinkage, F,
576
2
          "__amdgpu_device_library_preserve_asan_functions_ptr", nullptr,
577
2
          llvm::GlobalVariable::NotThreadLocal);
578
2
      addCompilerUsedGlobal(Var);
579
2
    }
580
    // Emit amdgpu_code_object_version module flag, which is code object version
581
    // times 100.
582
    // ToDo: Enable module flag for all code object version when ROCm device
583
    // library is ready.
584
299
    if (getTarget().getTargetOpts().CodeObjectVersion == TargetOptions::COV_5) {
585
2
      getModule().addModuleFlag(llvm::Module::Error,
586
2
                                "amdgpu_code_object_version",
587
2
                                getTarget().getTargetOpts().CodeObjectVersion);
588
2
    }
589
299
  }
590
591
  // Emit a global array containing all external kernels or device variables
592
  // used by host functions and mark it as used for CUDA/HIP. This is necessary
593
  // to get kernels or device variables in archives linked in even if these
594
  // kernels or device variables are only used in host functions.
595
35.9k
  if (!Context.CUDAExternalDeviceDeclODRUsedByHost.empty()) {
596
11
    SmallVector<llvm::Constant *, 8> UsedArray;
597
24
    for (auto D : Context.CUDAExternalDeviceDeclODRUsedByHost) {
598
24
      GlobalDecl GD;
599
24
      if (auto *FD = dyn_cast<FunctionDecl>(D))
600
8
        GD = GlobalDecl(FD, KernelReferenceKind::Kernel);
601
16
      else
602
16
        GD = GlobalDecl(D);
603
24
      UsedArray.push_back(llvm::ConstantExpr::getPointerBitCastOrAddrSpaceCast(
604
24
          GetAddrOfGlobal(GD), Int8PtrTy));
605
24
    }
606
607
11
    llvm::ArrayType *ATy = llvm::ArrayType::get(Int8PtrTy, UsedArray.size());
608
609
11
    auto *GV = new llvm::GlobalVariable(
610
11
        getModule(), ATy, false, llvm::GlobalValue::InternalLinkage,
611
11
        llvm::ConstantArray::get(ATy, UsedArray), "__clang_gpu_used_external");
612
11
    addCompilerUsedGlobal(GV);
613
11
  }
614
615
35.9k
  emitLLVMUsed();
616
35.9k
  if (SanStats)
617
2
    SanStats->finish();
618
619
35.9k
  if (CodeGenOpts.Autolink &&
620
35.9k
      
(35.8k
Context.getLangOpts().Modules35.8k
||
!LinkerOptionsMetadata.empty()33.6k
)) {
621
2.30k
    EmitModuleLinkOptions();
622
2.30k
  }
623
624
  // On ELF we pass the dependent library specifiers directly to the linker
625
  // without manipulating them. This is in contrast to other platforms where
626
  // they are mapped to a specific linker option by the compiler. This
627
  // difference is a result of the greater variety of ELF linkers and the fact
628
  // that ELF linkers tend to handle libraries in a more complicated fashion
629
  // than on other platforms. This forces us to defer handling the dependent
630
  // libs to the linker.
631
  //
632
  // CUDA/HIP device and host libraries are different. Currently there is no
633
  // way to differentiate dependent libraries for host or device. Existing
634
  // usage of #pragma comment(lib, *) is intended for host libraries on
635
  // Windows. Therefore emit llvm.dependent-libraries only for host.
636
35.9k
  if (!ELFDependentLibraries.empty() && 
!Context.getLangOpts().CUDAIsDevice9
) {
637
6
    auto *NMD = getModule().getOrInsertNamedMetadata("llvm.dependent-libraries");
638
6
    for (auto *MD : ELFDependentLibraries)
639
18
      NMD->addOperand(MD);
640
6
  }
641
642
  // Record mregparm value now so it is visible through rest of codegen.
643
35.9k
  if (Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86)
644
3.12k
    getModule().addModuleFlag(llvm::Module::Error, "NumRegisterParameters",
645
3.12k
                              CodeGenOpts.NumRegisterParameters);
646
647
35.9k
  if (CodeGenOpts.DwarfVersion) {
648
3.58k
    getModule().addModuleFlag(llvm::Module::Max, "Dwarf Version",
649
3.58k
                              CodeGenOpts.DwarfVersion);
650
3.58k
  }
651
652
35.9k
  if (CodeGenOpts.Dwarf64)
653
1
    getModule().addModuleFlag(llvm::Module::Max, "DWARF64", 1);
654
655
35.9k
  if (Context.getLangOpts().SemanticInterposition)
656
    // Require various optimization to respect semantic interposition.
657
1
    getModule().setSemanticInterposition(true);
658
659
35.9k
  if (CodeGenOpts.EmitCodeView) {
660
    // Indicate that we want CodeView in the metadata.
661
55
    getModule().addModuleFlag(llvm::Module::Warning, "CodeView", 1);
662
55
  }
663
35.9k
  if (CodeGenOpts.CodeViewGHash) {
664
1
    getModule().addModuleFlag(llvm::Module::Warning, "CodeViewGHash", 1);
665
1
  }
666
35.9k
  if (CodeGenOpts.ControlFlowGuard) {
667
    // Function ID tables and checks for Control Flow Guard (cfguard=2).
668
1
    getModule().addModuleFlag(llvm::Module::Warning, "cfguard", 2);
669
35.9k
  } else if (CodeGenOpts.ControlFlowGuardNoChecks) {
670
    // Function ID tables for Control Flow Guard (cfguard=1).
671
1
    getModule().addModuleFlag(llvm::Module::Warning, "cfguard", 1);
672
1
  }
673
35.9k
  if (CodeGenOpts.EHContGuard) {
674
    // Function ID tables for EH Continuation Guard.
675
1
    getModule().addModuleFlag(llvm::Module::Warning, "ehcontguard", 1);
676
1
  }
677
35.9k
  if (CodeGenOpts.OptimizationLevel > 0 && 
CodeGenOpts.StrictVTablePointers3.16k
) {
678
    // We don't support LTO with 2 with different StrictVTablePointers
679
    // FIXME: we could support it by stripping all the information introduced
680
    // by StrictVTablePointers.
681
682
6
    getModule().addModuleFlag(llvm::Module::Error, "StrictVTablePointers",1);
683
684
6
    llvm::Metadata *Ops[2] = {
685
6
              llvm::MDString::get(VMContext, "StrictVTablePointers"),
686
6
              llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(
687
6
                  llvm::Type::getInt32Ty(VMContext), 1))};
688
689
6
    getModule().addModuleFlag(llvm::Module::Require,
690
6
                              "StrictVTablePointersRequirement",
691
6
                              llvm::MDNode::get(VMContext, Ops));
692
6
  }
693
35.9k
  if (getModuleDebugInfo())
694
    // We support a single version in the linked module. The LLVM
695
    // parser will drop debug info with a different version number
696
    // (and warn about it, too).
697
10.7k
    getModule().addModuleFlag(llvm::Module::Warning, "Debug Info Version",
698
10.7k
                              llvm::DEBUG_METADATA_VERSION);
699
700
  // We need to record the widths of enums and wchar_t, so that we can generate
701
  // the correct build attributes in the ARM backend. wchar_size is also used by
702
  // TargetLibraryInfo.
703
35.9k
  uint64_t WCharWidth =
704
35.9k
      Context.getTypeSizeInChars(Context.getWideCharType()).getQuantity();
705
35.9k
  getModule().addModuleFlag(llvm::Module::Error, "wchar_size", WCharWidth);
706
707
35.9k
  llvm::Triple::ArchType Arch = Context.getTargetInfo().getTriple().getArch();
708
35.9k
  if (   Arch == llvm::Triple::arm
709
35.9k
      || 
Arch == llvm::Triple::armeb35.6k
710
35.9k
      || 
Arch == llvm::Triple::thumb35.6k
711
35.9k
      || 
Arch == llvm::Triple::thumbeb35.2k
) {
712
    // The minimum width of an enum in bytes
713
642
    uint64_t EnumWidth = Context.getLangOpts().ShortEnums ? 
11
:
4641
;
714
642
    getModule().addModuleFlag(llvm::Module::Error, "min_enum_size", EnumWidth);
715
642
  }
716
717
35.9k
  if (Arch == llvm::Triple::riscv32 || 
Arch == llvm::Triple::riscv6435.8k
) {
718
116
    StringRef ABIStr = Target.getABI();
719
116
    llvm::LLVMContext &Ctx = TheModule.getContext();
720
116
    getModule().addModuleFlag(llvm::Module::Error, "target-abi",
721
116
                              llvm::MDString::get(Ctx, ABIStr));
722
116
  }
723
724
35.9k
  if (CodeGenOpts.SanitizeCfiCrossDso) {
725
    // Indicate that we want cross-DSO control flow integrity checks.
726
14
    getModule().addModuleFlag(llvm::Module::Override, "Cross-DSO CFI", 1);
727
14
  }
728
729
35.9k
  if (CodeGenOpts.WholeProgramVTables) {
730
    // Indicate whether VFE was enabled for this module, so that the
731
    // vcall_visibility metadata added under whole program vtables is handled
732
    // appropriately in the optimizer.
733
20
    getModule().addModuleFlag(llvm::Module::Error, "Virtual Function Elim",
734
20
                              CodeGenOpts.VirtualFunctionElimination);
735
20
  }
736
737
35.9k
  if (LangOpts.Sanitize.has(SanitizerKind::CFIICall)) {
738
25
    getModule().addModuleFlag(llvm::Module::Override,
739
25
                              "CFI Canonical Jump Tables",
740
25
                              CodeGenOpts.SanitizeCfiCanonicalJumpTables);
741
25
  }
742
743
35.9k
  if (CodeGenOpts.CFProtectionReturn &&
744
35.9k
      
Target.checkCFProtectionReturnSupported(getDiags())0
) {
745
    // Indicate that we want to instrument return control flow protection.
746
0
    getModule().addModuleFlag(llvm::Module::Override, "cf-protection-return",
747
0
                              1);
748
0
  }
749
750
35.9k
  if (CodeGenOpts.CFProtectionBranch &&
751
35.9k
      
Target.checkCFProtectionBranchSupported(getDiags())4
) {
752
    // Indicate that we want to instrument branch control flow protection.
753
4
    getModule().addModuleFlag(llvm::Module::Override, "cf-protection-branch",
754
4
                              1);
755
4
  }
756
757
35.9k
  if (CodeGenOpts.IBTSeal)
758
1
    getModule().addModuleFlag(llvm::Module::Override, "ibt-seal", 1);
759
760
35.9k
  if (CodeGenOpts.FunctionReturnThunks)
761
5
    getModule().addModuleFlag(llvm::Module::Override, "function_return_thunk_extern", 1);
762
763
  // Add module metadata for return address signing (ignoring
764
  // non-leaf/all) and stack tagging. These are actually turned on by function
765
  // attributes, but we use module metadata to emit build attributes. This is
766
  // needed for LTO, where the function attributes are inside bitcode
767
  // serialised into a global variable by the time build attributes are
768
  // emitted, so we can't access them. LTO objects could be compiled with
769
  // different flags therefore module flags are set to "Min" behavior to achieve
770
  // the same end result of the normal build where e.g BTI is off if any object
771
  // doesn't support it.
772
35.9k
  if (Context.getTargetInfo().hasFeature("ptrauth") &&
773
35.9k
      LangOpts.getSignReturnAddressScope() !=
774
0
          LangOptions::SignReturnAddressScopeKind::None)
775
0
    getModule().addModuleFlag(llvm::Module::Override,
776
0
                              "sign-return-address-buildattr", 1);
777
35.9k
  if (LangOpts.Sanitize.has(SanitizerKind::MemtagStack))
778
12
    getModule().addModuleFlag(llvm::Module::Override,
779
12
                              "tag-stack-memory-buildattr", 1);
780
781
35.9k
  if (Arch == llvm::Triple::thumb || 
Arch == llvm::Triple::thumbeb35.5k
||
782
35.9k
      
Arch == llvm::Triple::arm35.5k
||
Arch == llvm::Triple::armeb35.2k
||
783
35.9k
      
Arch == llvm::Triple::aarch6435.2k
||
Arch == llvm::Triple::aarch64_3232.8k
||
784
35.9k
      
Arch == llvm::Triple::aarch64_be32.8k
) {
785
3.11k
    getModule().addModuleFlag(llvm::Module::Min, "branch-target-enforcement",
786
3.11k
                              LangOpts.BranchTargetEnforcement);
787
788
3.11k
    getModule().addModuleFlag(llvm::Module::Min, "sign-return-address",
789
3.11k
                              LangOpts.hasSignReturnAddress());
790
791
3.11k
    getModule().addModuleFlag(llvm::Module::Min, "sign-return-address-all",
792
3.11k
                              LangOpts.isSignReturnAddressScopeAll());
793
794
3.11k
    getModule().addModuleFlag(llvm::Module::Min,
795
3.11k
                              "sign-return-address-with-bkey",
796
3.11k
                              !LangOpts.isSignReturnAddressWithAKey());
797
3.11k
  }
798
799
35.9k
  if (!CodeGenOpts.MemoryProfileOutput.empty()) {
800
4
    llvm::LLVMContext &Ctx = TheModule.getContext();
801
4
    getModule().addModuleFlag(
802
4
        llvm::Module::Error, "MemProfProfileFilename",
803
4
        llvm::MDString::get(Ctx, CodeGenOpts.MemoryProfileOutput));
804
4
  }
805
806
35.9k
  if (LangOpts.CUDAIsDevice && 
getTriple().isNVPTX()128
) {
807
    // Indicate whether __nvvm_reflect should be configured to flush denormal
808
    // floating point values to 0.  (This corresponds to its "__CUDA_FTZ"
809
    // property.)
810
38
    getModule().addModuleFlag(llvm::Module::Override, "nvvm-reflect-ftz",
811
38
                              CodeGenOpts.FP32DenormalMode.Output !=
812
38
                                  llvm::DenormalMode::IEEE);
813
38
  }
814
815
35.9k
  if (LangOpts.EHAsynch)
816
4
    getModule().addModuleFlag(llvm::Module::Warning, "eh-asynch", 1);
817
818
  // Indicate whether this Module was compiled with -fopenmp
819
35.9k
  if (getLangOpts().OpenMP && 
!getLangOpts().OpenMPSimd5.86k
)
820
3.39k
    getModule().addModuleFlag(llvm::Module::Max, "openmp", LangOpts.OpenMP);
821
35.9k
  if (getLangOpts().OpenMPIsDevice)
822
509
    getModule().addModuleFlag(llvm::Module::Max, "openmp-device",
823
509
                              LangOpts.OpenMP);
824
825
  // Emit OpenCL specific module metadata: OpenCL/SPIR version.
826
35.9k
  if (LangOpts.OpenCL || 
(35.5k
LangOpts.CUDAIsDevice35.5k
&&
getTriple().isSPIRV()128
)) {
827
394
    EmitOpenCLMetadata();
828
    // Emit SPIR version.
829
394
    if (getTriple().isSPIR()) {
830
      // SPIR v2.0 s2.12 - The SPIR version used by the module is stored in the
831
      // opencl.spir.version named metadata.
832
      // C++ for OpenCL has a distinct mapping for version compatibility with
833
      // OpenCL.
834
116
      auto Version = LangOpts.getOpenCLCompatibleVersion();
835
116
      llvm::Metadata *SPIRVerElts[] = {
836
116
          llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(
837
116
              Int32Ty, Version / 100)),
838
116
          llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(
839
116
              Int32Ty, (Version / 100 > 1) ? 
080
:
236
))};
840
116
      llvm::NamedMDNode *SPIRVerMD =
841
116
          TheModule.getOrInsertNamedMetadata("opencl.spir.version");
842
116
      llvm::LLVMContext &Ctx = TheModule.getContext();
843
116
      SPIRVerMD->addOperand(llvm::MDNode::get(Ctx, SPIRVerElts));
844
116
    }
845
394
  }
846
847
  // HLSL related end of code gen work items.
848
35.9k
  if (LangOpts.HLSL)
849
5
    getHLSLRuntime().finishCodeGen();
850
851
35.9k
  if (uint32_t PLevel = Context.getLangOpts().PICLevel) {
852
4.49k
    assert(PLevel < 3 && "Invalid PIC Level");
853
0
    getModule().setPICLevel(static_cast<llvm::PICLevel::Level>(PLevel));
854
4.49k
    if (Context.getLangOpts().PIE)
855
294
      getModule().setPIELevel(static_cast<llvm::PIELevel::Level>(PLevel));
856
4.49k
  }
857
858
35.9k
  if (getCodeGenOpts().CodeModel.size() > 0) {
859
20.4k
    unsigned CM = llvm::StringSwitch<unsigned>(getCodeGenOpts().CodeModel)
860
20.4k
                  .Case("tiny", llvm::CodeModel::Tiny)
861
20.4k
                  .Case("small", llvm::CodeModel::Small)
862
20.4k
                  .Case("kernel", llvm::CodeModel::Kernel)
863
20.4k
                  .Case("medium", llvm::CodeModel::Medium)
864
20.4k
                  .Case("large", llvm::CodeModel::Large)
865
20.4k
                  .Default(~0u);
866
20.4k
    if (CM != ~0u) {
867
10
      llvm::CodeModel::Model codeModel = static_cast<llvm::CodeModel::Model>(CM);
868
10
      getModule().setCodeModel(codeModel);
869
10
    }
870
20.4k
  }
871
872
35.9k
  if (CodeGenOpts.NoPLT)
873
5
    getModule().setRtLibUseGOT();
874
35.9k
  if (CodeGenOpts.UnwindTables)
875
4.24k
    getModule().setUwtable(llvm::UWTableKind(CodeGenOpts.UnwindTables));
876
877
35.9k
  switch (CodeGenOpts.getFramePointer()) {
878
16.1k
  case CodeGenOptions::FramePointerKind::None:
879
    // 0 ("none") is the default.
880
16.1k
    break;
881
85
  case CodeGenOptions::FramePointerKind::NonLeaf:
882
85
    getModule().setFramePointer(llvm::FramePointerKind::NonLeaf);
883
85
    break;
884
19.6k
  case CodeGenOptions::FramePointerKind::All:
885
19.6k
    getModule().setFramePointer(llvm::FramePointerKind::All);
886
19.6k
    break;
887
35.9k
  }
888
889
35.9k
  SimplifyPersonality();
890
891
35.9k
  if (getCodeGenOpts().EmitDeclMetadata)
892
15.4k
    EmitDeclMetadata();
893
894
35.9k
  if (getCodeGenOpts().EmitGcovArcs || 
getCodeGenOpts().EmitGcovNotes35.8k
)
895
17
    EmitCoverageFile();
896
897
35.9k
  if (CGDebugInfo *DI = getModuleDebugInfo())
898
10.7k
    DI->finalize();
899
900
35.9k
  if (getCodeGenOpts().EmitVersionIdentMetadata)
901
35.9k
    EmitVersionIdentMetadata();
902
903
35.9k
  if (!getCodeGenOpts().RecordCommandLine.empty())
904
0
    EmitCommandLineMetadata();
905
906
35.9k
  if (!getCodeGenOpts().StackProtectorGuard.empty())
907
5
    getModule().setStackProtectorGuard(getCodeGenOpts().StackProtectorGuard);
908
35.9k
  if (!getCodeGenOpts().StackProtectorGuardReg.empty())
909
1
    getModule().setStackProtectorGuardReg(
910
1
        getCodeGenOpts().StackProtectorGuardReg);
911
35.9k
  if (!getCodeGenOpts().StackProtectorGuardSymbol.empty())
912
0
    getModule().setStackProtectorGuardSymbol(
913
0
        getCodeGenOpts().StackProtectorGuardSymbol);
914
35.9k
  if (getCodeGenOpts().StackProtectorGuardOffset != INT_MAX)
915
5
    getModule().setStackProtectorGuardOffset(
916
5
        getCodeGenOpts().StackProtectorGuardOffset);
917
35.9k
  if (getCodeGenOpts().StackAlignment)
918
1
    getModule().setOverrideStackAlignment(getCodeGenOpts().StackAlignment);
919
35.9k
  if (getCodeGenOpts().SkipRaxSetup)
920
1
    getModule().addModuleFlag(llvm::Module::Override, "SkipRaxSetup", 1);
921
922
35.9k
  getTargetCodeGenInfo().emitTargetMetadata(*this, MangledDeclNames);
923
924
35.9k
  EmitBackendOptionsMetadata(getCodeGenOpts());
925
926
  // If there is device offloading code embed it in the host now.
927
35.9k
  EmbedObject(&getModule(), CodeGenOpts, getDiags());
928
929
  // Set visibility from DLL storage class
930
  // We do this at the end of LLVM IR generation; after any operation
931
  // that might affect the DLL storage class or the visibility, and
932
  // before anything that might act on these.
933
35.9k
  setVisibilityFromDLLStorageClass(LangOpts, getModule());
934
35.9k
}
935
936
394
void CodeGenModule::EmitOpenCLMetadata() {
937
  // SPIR v2.0 s2.13 - The OpenCL version used by the module is stored in the
938
  // opencl.ocl.version named metadata node.
939
  // C++ for OpenCL has a distinct mapping for versions compatibile with OpenCL.
940
394
  auto Version = LangOpts.getOpenCLCompatibleVersion();
941
394
  llvm::Metadata *OCLVerElts[] = {
942
394
      llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(
943
394
          Int32Ty, Version / 100)),
944
394
      llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(
945
394
          Int32Ty, (Version % 100) / 10))};
946
394
  llvm::NamedMDNode *OCLVerMD =
947
394
      TheModule.getOrInsertNamedMetadata("opencl.ocl.version");
948
394
  llvm::LLVMContext &Ctx = TheModule.getContext();
949
394
  OCLVerMD->addOperand(llvm::MDNode::get(Ctx, OCLVerElts));
950
394
}
951
952
void CodeGenModule::EmitBackendOptionsMetadata(
953
35.9k
    const CodeGenOptions CodeGenOpts) {
954
35.9k
  switch (getTriple().getArch()) {
955
35.7k
  default:
956
35.7k
    break;
957
35.7k
  case llvm::Triple::riscv32:
958
116
  case llvm::Triple::riscv64:
959
116
    getModule().addModuleFlag(llvm::Module::Error, "SmallDataLimit",
960
116
                              CodeGenOpts.SmallDataLimit);
961
116
    break;
962
35.9k
  }
963
35.9k
}
964
965
2.38M
void CodeGenModule::UpdateCompletedType(const TagDecl *TD) {
966
  // Make sure that this type is translated.
967
2.38M
  Types.UpdateCompletedType(TD);
968
2.38M
}
969
970
476
void CodeGenModule::RefreshTypeCacheForClass(const CXXRecordDecl *RD) {
971
  // Make sure that this type is translated.
972
476
  Types.RefreshTypeCacheForClass(RD);
973
476
}
974
975
218k
llvm::MDNode *CodeGenModule::getTBAATypeInfo(QualType QTy) {
976
218k
  if (!TBAA)
977
216k
    return nullptr;
978
1.51k
  return TBAA->getTypeInfo(QTy);
979
218k
}
980
981
2.87M
TBAAAccessInfo CodeGenModule::getTBAAAccessInfo(QualType AccessType) {
982
2.87M
  if (!TBAA)
983
2.67M
    return TBAAAccessInfo();
984
199k
  if (getLangOpts().CUDAIsDevice) {
985
    // As CUDA builtin surface/texture types are replaced, skip generating TBAA
986
    // access info.
987
42
    if (AccessType->isCUDADeviceBuiltinSurfaceType()) {
988
0
      if (getTargetCodeGenInfo().getCUDADeviceBuiltinSurfaceDeviceType() !=
989
0
          nullptr)
990
0
        return TBAAAccessInfo();
991
42
    } else if (AccessType->isCUDADeviceBuiltinTextureType()) {
992
0
      if (getTargetCodeGenInfo().getCUDADeviceBuiltinTextureDeviceType() !=
993
0
          nullptr)
994
0
        return TBAAAccessInfo();
995
0
    }
996
42
  }
997
199k
  return TBAA->getAccessInfo(AccessType);
998
199k
}
999
1000
TBAAAccessInfo
1001
4.96k
CodeGenModule::getTBAAVTablePtrAccessInfo(llvm::Type *VTablePtrType) {
1002
4.96k
  if (!TBAA)
1003
4.34k
    return TBAAAccessInfo();
1004
622
  return TBAA->getVTablePtrAccessInfo(VTablePtrType);
1005
4.96k
}
1006
1007
11.1k
llvm::MDNode *CodeGenModule::getTBAAStructInfo(QualType QTy) {
1008
11.1k
  if (!TBAA)
1009
10.7k
    return nullptr;
1010
335
  return TBAA->getTBAAStructInfo(QTy);
1011
11.1k
}
1012
1013
218k
llvm::MDNode *CodeGenModule::getTBAABaseTypeInfo(QualType QTy) {
1014
218k
  if (!TBAA)
1015
216k
    return nullptr;
1016
1.43k
  return TBAA->getBaseTypeInfo(QTy);
1017
218k
}
1018
1019
2.29M
llvm::MDNode *CodeGenModule::getTBAAAccessTagInfo(TBAAAccessInfo Info) {
1020
2.29M
  if (!TBAA)
1021
2.09M
    return nullptr;
1022
192k
  return TBAA->getAccessTagInfo(Info);
1023
2.29M
}
1024
1025
TBAAAccessInfo CodeGenModule::mergeTBAAInfoForCast(TBAAAccessInfo SourceInfo,
1026
2.55k
                                                   TBAAAccessInfo TargetInfo) {
1027
2.55k
  if (!TBAA)
1028
2.09k
    return TBAAAccessInfo();
1029
468
  return TBAA->mergeTBAAInfoForCast(SourceInfo, TargetInfo);
1030
2.55k
}
1031
1032
TBAAAccessInfo
1033
CodeGenModule::mergeTBAAInfoForConditionalOperator(TBAAAccessInfo InfoA,
1034
516
                                                   TBAAAccessInfo InfoB) {
1035
516
  if (!TBAA)
1036
513
    return TBAAAccessInfo();
1037
3
  return TBAA->mergeTBAAInfoForConditionalOperator(InfoA, InfoB);
1038
516
}
1039
1040
TBAAAccessInfo
1041
CodeGenModule::mergeTBAAInfoForMemoryTransfer(TBAAAccessInfo DestInfo,
1042
7
                                              TBAAAccessInfo SrcInfo) {
1043
7
  if (!TBAA)
1044
0
    return TBAAAccessInfo();
1045
7
  return TBAA->mergeTBAAInfoForConditionalOperator(DestInfo, SrcInfo);
1046
7
}
1047
1048
void CodeGenModule::DecorateInstructionWithTBAA(llvm::Instruction *Inst,
1049
2.29M
                                                TBAAAccessInfo TBAAInfo) {
1050
2.29M
  if (llvm::MDNode *Tag = getTBAAAccessTagInfo(TBAAInfo))
1051
191k
    Inst->setMetadata(llvm::LLVMContext::MD_tbaa, Tag);
1052
2.29M
}
1053
1054
void CodeGenModule::DecorateInstructionWithInvariantGroup(
1055
172
    llvm::Instruction *I, const CXXRecordDecl *RD) {
1056
172
  I->setMetadata(llvm::LLVMContext::MD_invariant_group,
1057
172
                 llvm::MDNode::get(getLLVMContext(), {}));
1058
172
}
1059
1060
15
void CodeGenModule::Error(SourceLocation loc, StringRef message) {
1061
15
  unsigned diagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error, "%0");
1062
15
  getDiags().Report(Context.getFullLoc(loc), diagID) << message;
1063
15
}
1064
1065
/// ErrorUnsupported - Print out an error that codegen doesn't support the
1066
/// specified stmt yet.
1067
3
void CodeGenModule::ErrorUnsupported(const Stmt *S, const char *Type) {
1068
3
  unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error,
1069
3
                                               "cannot compile this %0 yet");
1070
3
  std::string Msg = Type;
1071
3
  getDiags().Report(Context.getFullLoc(S->getBeginLoc()), DiagID)
1072
3
      << Msg << S->getSourceRange();
1073
3
}
1074
1075
/// ErrorUnsupported - Print out an error that codegen doesn't support the
1076
/// specified decl yet.
1077
8
void CodeGenModule::ErrorUnsupported(const Decl *D, const char *Type) {
1078
8
  unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error,
1079
8
                                               "cannot compile this %0 yet");
1080
8
  std::string Msg = Type;
1081
8
  getDiags().Report(Context.getFullLoc(D->getLocation()), DiagID) << Msg;
1082
8
}
1083
1084
42.8k
llvm::ConstantInt *CodeGenModule::getSize(CharUnits size) {
1085
42.8k
  return llvm::ConstantInt::get(SizeTy, size.getQuantity());
1086
42.8k
}
1087
1088
void CodeGenModule::setGlobalVisibility(llvm::GlobalValue *GV,
1089
872k
                                        const NamedDecl *D) const {
1090
872k
  if (GV->hasDLLImportStorageClass())
1091
1.94k
    return;
1092
  // Internal definitions always have default visibility.
1093
870k
  if (GV->hasLocalLinkage()) {
1094
57.1k
    GV->setVisibility(llvm::GlobalValue::DefaultVisibility);
1095
57.1k
    return;
1096
57.1k
  }
1097
813k
  if (!D)
1098
93
    return;
1099
  // Set visibility for definitions, and for declarations if requested globally
1100
  // or set explicitly.
1101
813k
  LinkageInfo LV = D->getLinkageAndVisibility();
1102
813k
  if (LV.isVisibilityExplicit() || 
getLangOpts().SetVisibilityForExternDecls597k
||
1103
813k
      
!GV->isDeclarationForLinker()597k
)
1104
413k
    GV->setVisibility(GetLLVMVisibility(LV.getVisibility()));
1105
813k
}
1106
1107
static bool shouldAssumeDSOLocal(const CodeGenModule &CGM,
1108
1.37M
                                 llvm::GlobalValue *GV) {
1109
1.37M
  if (GV->hasLocalLinkage())
1110
119k
    return true;
1111
1112
1.25M
  if (!GV->hasDefaultVisibility() && 
!GV->hasExternalWeakLinkage()272k
)
1113
272k
    return true;
1114
1115
  // DLLImport explicitly marks the GV as external.
1116
984k
  if (GV->hasDLLImportStorageClass())
1117
2.20k
    return false;
1118
1119
982k
  const llvm::Triple &TT = CGM.getTriple();
1120
982k
  if (TT.isWindowsGNUEnvironment()) {
1121
    // In MinGW, variables without DLLImport can still be automatically
1122
    // imported from a DLL by the linker; don't mark variables that
1123
    // potentially could come from another DLL as DSO local.
1124
1125
    // With EmulatedTLS, TLS variables can be autoimported from other DLLs
1126
    // (and this actually happens in the public interface of libstdc++), so
1127
    // such variables can't be marked as DSO local. (Native TLS variables
1128
    // can't be dllimported at all, though.)
1129
9.73k
    if (GV->isDeclarationForLinker() && 
isa<llvm::GlobalVariable>(GV)6.55k
&&
1130
9.73k
        
(408
!GV->isThreadLocal()408
||
CGM.getCodeGenOpts().EmulatedTLS13
))
1131
397
      return false;
1132
9.73k
  }
1133
1134
  // On COFF, don't mark 'extern_weak' symbols as DSO local. If these symbols
1135
  // remain unresolved in the link, they can be resolved to zero, which is
1136
  // outside the current DSO.
1137
981k
  if (TT.isOSBinFormatCOFF() && 
GV->hasExternalWeakLinkage()59.6k
)
1138
4
    return false;
1139
1140
  // Every other GV is local on COFF.
1141
  // Make an exception for windows OS in the triple: Some firmware builds use
1142
  // *-win32-macho triples. This (accidentally?) produced windows relocations
1143
  // without GOT tables in older clang versions; Keep this behaviour.
1144
  // FIXME: even thread local variables?
1145
981k
  if (TT.isOSBinFormatCOFF() || 
(922k
TT.isOSWindows()922k
&&
TT.isOSBinFormatMachO()107
))
1146
59.6k
    return true;
1147
1148
  // Only handle COFF and ELF for now.
1149
922k
  if (!TT.isOSBinFormatELF())
1150
519k
    return false;
1151
1152
  // If this is not an executable, don't assume anything is local.
1153
403k
  const auto &CGOpts = CGM.getCodeGenOpts();
1154
403k
  llvm::Reloc::Model RM = CGOpts.RelocationModel;
1155
403k
  const auto &LOpts = CGM.getLangOpts();
1156
403k
  if (RM != llvm::Reloc::Static && 
!LOpts.PIE401k
) {
1157
    // On ELF, if -fno-semantic-interposition is specified and the target
1158
    // supports local aliases, there will be neither CC1
1159
    // -fsemantic-interposition nor -fhalf-no-semantic-interposition. Set
1160
    // dso_local on the function if using a local alias is preferable (can avoid
1161
    // PLT indirection).
1162
399k
    if (!(isa<llvm::Function>(GV) && 
GV->canBenefitFromLocalAlias()371k
))
1163
323k
      return false;
1164
75.5k
    return !(CGM.getLangOpts().SemanticInterposition ||
1165
75.5k
             
CGM.getLangOpts().HalfNoSemanticInterposition75.5k
);
1166
399k
  }
1167
1168
  // A definition cannot be preempted from an executable.
1169
3.76k
  if (!GV->isDeclarationForLinker())
1170
1.32k
    return true;
1171
1172
  // Most PIC code sequences that assume that a symbol is local cannot produce a
1173
  // 0 if it turns out the symbol is undefined. While this is ABI and relocation
1174
  // depended, it seems worth it to handle it here.
1175
2.43k
  if (RM == llvm::Reloc::PIC_ && 
GV->hasExternalWeakLinkage()1.29k
)
1176
7
    return false;
1177
1178
  // PowerPC64 prefers TOC indirection to avoid copy relocations.
1179
2.43k
  if (TT.isPPC64())
1180
164
    return false;
1181
1182
2.26k
  if (CGOpts.DirectAccessExternalData) {
1183
    // If -fdirect-access-external-data (default for -fno-pic), set dso_local
1184
    // for non-thread-local variables. If the symbol is not defined in the
1185
    // executable, a copy relocation will be needed at link time. dso_local is
1186
    // excluded for thread-local variables because they generally don't support
1187
    // copy relocations.
1188
1.06k
    if (auto *Var = dyn_cast<llvm::GlobalVariable>(GV))
1189
241
      if (!Var->isThreadLocal())
1190
231
        return true;
1191
1192
    // -fno-pic sets dso_local on a function declaration to allow direct
1193
    // accesses when taking its address (similar to a data symbol). If the
1194
    // function is not defined in the executable, a canonical PLT entry will be
1195
    // needed at link time. -fno-direct-access-external-data can avoid the
1196
    // canonical PLT entry. We don't generalize this condition to -fpie/-fpic as
1197
    // it could just cause trouble without providing perceptible benefits.
1198
832
    if (isa<llvm::Function>(GV) && 
!CGOpts.NoPLT822
&&
RM == llvm::Reloc::Static775
)
1199
767
      return true;
1200
832
  }
1201
1202
  // If we can use copy relocations we can assume it is local.
1203
1204
  // Otherwise don't assume it is local.
1205
1.26k
  return false;
1206
2.26k
}
1207
1208
1.37M
void CodeGenModule::setDSOLocal(llvm::GlobalValue *GV) const {
1209
1.37M
  GV->setDSOLocal(shouldAssumeDSOLocal(*this, GV));
1210
1.37M
}
1211
1212
void CodeGenModule::setDLLImportDLLExport(llvm::GlobalValue *GV,
1213
510k
                                          GlobalDecl GD) const {
1214
510k
  const auto *D = dyn_cast<NamedDecl>(GD.getDecl());
1215
  // C++ destructors have a few C++ ABI specific special cases.
1216
510k
  if (const auto *Dtor = dyn_cast_or_null<CXXDestructorDecl>(D)) {
1217
16.8k
    getCXXABI().setCXXDestructorDLLStorage(GV, Dtor, GD.getDtorType());
1218
16.8k
    return;
1219
16.8k
  }
1220
493k
  setDLLImportDLLExport(GV, D);
1221
493k
}
1222
1223
void CodeGenModule::setDLLImportDLLExport(llvm::GlobalValue *GV,
1224
871k
                                          const NamedDecl *D) const {
1225
871k
  if (D && 
D->isExternallyVisible()871k
) {
1226
786k
    if (D->hasAttr<DLLImportAttr>())
1227
1.93k
      GV->setDLLStorageClass(llvm::GlobalVariable::DLLImportStorageClass);
1228
784k
    else if ((D->hasAttr<DLLExportAttr>() ||
1229
784k
              
shouldMapVisibilityToDLLExport(D)777k
) &&
1230
784k
             
!GV->isDeclarationForLinker()7.09k
)
1231
2.79k
      GV->setDLLStorageClass(llvm::GlobalVariable::DLLExportStorageClass);
1232
786k
  }
1233
871k
}
1234
1235
void CodeGenModule::setGVProperties(llvm::GlobalValue *GV,
1236
510k
                                    GlobalDecl GD) const {
1237
510k
  setDLLImportDLLExport(GV, GD);
1238
510k
  setGVPropertiesAux(GV, dyn_cast<NamedDecl>(GD.getDecl()));
1239
510k
}
1240
1241
void CodeGenModule::setGVProperties(llvm::GlobalValue *GV,
1242
361k
                                    const NamedDecl *D) const {
1243
361k
  setDLLImportDLLExport(GV, D);
1244
361k
  setGVPropertiesAux(GV, D);
1245
361k
}
1246
1247
void CodeGenModule::setGVPropertiesAux(llvm::GlobalValue *GV,
1248
872k
                                       const NamedDecl *D) const {
1249
872k
  setGlobalVisibility(GV, D);
1250
872k
  setDSOLocal(GV);
1251
872k
  GV->setPartition(CodeGenOpts.SymbolPartition);
1252
872k
}
1253
1254
21
static llvm::GlobalVariable::ThreadLocalMode GetLLVMTLSModel(StringRef S) {
1255
21
  return llvm::StringSwitch<llvm::GlobalVariable::ThreadLocalMode>(S)
1256
21
      .Case("global-dynamic", llvm::GlobalVariable::GeneralDynamicTLSModel)
1257
21
      .Case("local-dynamic", llvm::GlobalVariable::LocalDynamicTLSModel)
1258
21
      .Case("initial-exec", llvm::GlobalVariable::InitialExecTLSModel)
1259
21
      .Case("local-exec", llvm::GlobalVariable::LocalExecTLSModel);
1260
21
}
1261
1262
llvm::GlobalVariable::ThreadLocalMode
1263
632
CodeGenModule::GetDefaultLLVMTLSModel() const {
1264
632
  switch (CodeGenOpts.getDefaultTLSModel()) {
1265
609
  case CodeGenOptions::GeneralDynamicTLSModel:
1266
609
    return llvm::GlobalVariable::GeneralDynamicTLSModel;
1267
11
  case CodeGenOptions::LocalDynamicTLSModel:
1268
11
    return llvm::GlobalVariable::LocalDynamicTLSModel;
1269
6
  case CodeGenOptions::InitialExecTLSModel:
1270
6
    return llvm::GlobalVariable::InitialExecTLSModel;
1271
6
  case CodeGenOptions::LocalExecTLSModel:
1272
6
    return llvm::GlobalVariable::LocalExecTLSModel;
1273
632
  }
1274
0
  llvm_unreachable("Invalid TLS model!");
1275
0
}
1276
1277
581
void CodeGenModule::setTLSMode(llvm::GlobalValue *GV, const VarDecl &D) const {
1278
581
  assert(D.getTLSKind() && "setting TLS mode on non-TLS var!");
1279
1280
0
  llvm::GlobalValue::ThreadLocalMode TLM;
1281
581
  TLM = GetDefaultLLVMTLSModel();
1282
1283
  // Override the TLS model if it is explicitly specified.
1284
581
  if (const TLSModelAttr *Attr = D.getAttr<TLSModelAttr>()) {
1285
21
    TLM = GetLLVMTLSModel(Attr->getModel());
1286
21
  }
1287
1288
581
  GV->setThreadLocalMode(TLM);
1289
581
}
1290
1291
static std::string getCPUSpecificMangling(const CodeGenModule &CGM,
1292
152
                                          StringRef Name) {
1293
152
  const TargetInfo &Target = CGM.getTarget();
1294
152
  return (Twine('.') + Twine(Target.CPUSpecificManglingCharacter(Name))).str();
1295
152
}
1296
1297
static void AppendCPUSpecificCPUDispatchMangling(const CodeGenModule &CGM,
1298
                                                 const CPUSpecificAttr *Attr,
1299
                                                 unsigned CPUIndex,
1300
182
                                                 raw_ostream &Out) {
1301
  // cpu_specific gets the current name, dispatch gets the resolver if IFunc is
1302
  // supported.
1303
182
  if (Attr)
1304
54
    Out << getCPUSpecificMangling(CGM, Attr->getCPUName(CPUIndex)->getName());
1305
128
  else if (CGM.getTarget().supportsIFunc())
1306
21
    Out << ".resolver";
1307
182
}
1308
1309
static void AppendTargetMangling(const CodeGenModule &CGM,
1310
409
                                 const TargetAttr *Attr, raw_ostream &Out) {
1311
409
  if (Attr->isDefaultVersion())
1312
263
    return;
1313
1314
146
  Out << '.';
1315
146
  const TargetInfo &Target = CGM.getTarget();
1316
146
  ParsedTargetAttr Info =
1317
146
      Attr->parse([&Target](StringRef LHS, StringRef RHS) {
1318
        // Multiversioning doesn't allow "no-${feature}", so we can
1319
        // only have "+" prefixes here.
1320
10
        assert(LHS.startswith("+") && RHS.startswith("+") &&
1321
10
               "Features should always have a prefix.");
1322
0
        return Target.multiVersionSortPriority(LHS.substr(1)) >
1323
10
               Target.multiVersionSortPriority(RHS.substr(1));
1324
10
      });
1325
1326
146
  bool IsFirst = true;
1327
1328
146
  if (!Info.Architecture.empty()) {
1329
94
    IsFirst = false;
1330
94
    Out << "arch_" << Info.Architecture;
1331
94
  }
1332
1333
146
  for (StringRef Feat : Info.Features) {
1334
64
    if (!IsFirst)
1335
12
      Out << '_';
1336
64
    IsFirst = false;
1337
64
    Out << Feat.substr(1);
1338
64
  }
1339
146
}
1340
1341
// Returns true if GD is a function decl with internal linkage and
1342
// needs a unique suffix after the mangled name.
1343
static bool isUniqueInternalLinkageDecl(GlobalDecl GD,
1344
1.40M
                                        CodeGenModule &CGM) {
1345
1.40M
  const Decl *D = GD.getDecl();
1346
1.40M
  return !CGM.getModuleNameHash().empty() && 
isa<FunctionDecl>(D)42
&&
1347
1.40M
         
(CGM.getFunctionLinkage(GD) == llvm::GlobalValue::InternalLinkage)33
;
1348
1.40M
}
1349
1350
static void AppendTargetClonesMangling(const CodeGenModule &CGM,
1351
                                       const TargetClonesAttr *Attr,
1352
                                       unsigned VersionIndex,
1353
48
                                       raw_ostream &Out) {
1354
48
  Out << '.';
1355
48
  StringRef FeatureStr = Attr->getFeatureStr(VersionIndex);
1356
48
  if (FeatureStr.startswith("arch="))
1357
8
    Out << "arch_" << FeatureStr.substr(sizeof("arch=") - 1);
1358
40
  else
1359
40
    Out << FeatureStr;
1360
1361
48
  Out << '.' << Attr->getMangledIndex(VersionIndex);
1362
48
}
1363
1364
static std::string getMangledNameImpl(CodeGenModule &CGM, GlobalDecl GD,
1365
                                      const NamedDecl *ND,
1366
3.14M
                                      bool OmitMultiVersionMangling = false) {
1367
3.14M
  SmallString<256> Buffer;
1368
3.14M
  llvm::raw_svector_ostream Out(Buffer);
1369
3.14M
  MangleContext &MC = CGM.getCXXABI().getMangleContext();
1370
3.14M
  if (!CGM.getModuleNameHash().empty())
1371
46
    MC.needsUniqueInternalLinkageNames();
1372
3.14M
  bool ShouldMangle = MC.shouldMangleDeclName(ND);
1373
3.14M
  if (ShouldMangle)
1374
1.40M
    MC.mangleName(GD.getWithDecl(ND), Out);
1375
1.73M
  else {
1376
1.73M
    IdentifierInfo *II = ND->getIdentifier();
1377
1.73M
    assert(II && "Attempt to mangle unnamed decl.");
1378
0
    const auto *FD = dyn_cast<FunctionDecl>(ND);
1379
1380
1.73M
    if (FD &&
1381
1.73M
        
FD->getType()->castAs<FunctionType>()->getCallConv() == CC_X86RegCall1.71M
) {
1382
67
      Out << "__regcall3__" << II->getName();
1383
1.73M
    } else if (FD && 
FD->hasAttr<CUDAGlobalAttr>()1.71M
&&
1384
1.73M
               
GD.getKernelReferenceKind() == KernelReferenceKind::Stub32
) {
1385
7
      Out << "__device_stub__" << II->getName();
1386
1.73M
    } else {
1387
1.73M
      Out << II->getName();
1388
1.73M
    }
1389
1.73M
  }
1390
1391
  // Check if the module name hash should be appended for internal linkage
1392
  // symbols.   This should come before multi-version target suffixes are
1393
  // appended. This is to keep the name and module hash suffix of the
1394
  // internal linkage function together.  The unique suffix should only be
1395
  // added when name mangling is done to make sure that the final name can
1396
  // be properly demangled.  For example, for C functions without prototypes,
1397
  // name mangling is not done and the unique suffix should not be appeneded
1398
  // then.
1399
3.14M
  if (ShouldMangle && 
isUniqueInternalLinkageDecl(GD, CGM)1.40M
) {
1400
26
    assert(CGM.getCodeGenOpts().UniqueInternalLinkageNames &&
1401
26
           "Hash computed when not explicitly requested");
1402
0
    Out << CGM.getModuleNameHash();
1403
26
  }
1404
1405
3.14M
  if (const auto *FD = dyn_cast<FunctionDecl>(ND))
1406
3.06M
    if (FD->isMultiVersion() && 
!OmitMultiVersionMangling1.52k
) {
1407
639
      switch (FD->getMultiVersionKind()) {
1408
128
      case MultiVersionKind::CPUDispatch:
1409
182
      case MultiVersionKind::CPUSpecific:
1410
182
        AppendCPUSpecificCPUDispatchMangling(CGM,
1411
182
                                             FD->getAttr<CPUSpecificAttr>(),
1412
182
                                             GD.getMultiVersionIndex(), Out);
1413
182
        break;
1414
409
      case MultiVersionKind::Target:
1415
409
        AppendTargetMangling(CGM, FD->getAttr<TargetAttr>(), Out);
1416
409
        break;
1417
48
      case MultiVersionKind::TargetClones:
1418
48
        AppendTargetClonesMangling(CGM, FD->getAttr<TargetClonesAttr>(),
1419
48
                                   GD.getMultiVersionIndex(), Out);
1420
48
        break;
1421
0
      case MultiVersionKind::None:
1422
0
        llvm_unreachable("None multiversion type isn't valid here");
1423
639
      }
1424
639
    }
1425
1426
  // Make unique name for device side static file-scope variable for HIP.
1427
3.14M
  if (CGM.getContext().shouldExternalize(ND) &&
1428
3.14M
      
CGM.getLangOpts().GPURelocatableDeviceCode345
&&
1429
3.14M
      
CGM.getLangOpts().CUDAIsDevice101
)
1430
90
    CGM.printPostfixForExternalizedDecl(Out, ND);
1431
1432
3.14M
  return std::string(Out.str());
1433
3.14M
}
1434
1435
void CodeGenModule::UpdateMultiVersionNames(GlobalDecl GD,
1436
                                            const FunctionDecl *FD,
1437
525
                                            StringRef &CurName) {
1438
525
  if (!FD->isMultiVersion())
1439
0
    return;
1440
1441
  // Get the name of what this would be without the 'target' attribute.  This
1442
  // allows us to lookup the version that was emitted when this wasn't a
1443
  // multiversion function.
1444
525
  std::string NonTargetName =
1445
525
      getMangledNameImpl(*this, GD, FD, /*OmitMultiVersionMangling=*/true);
1446
525
  GlobalDecl OtherGD;
1447
525
  if (lookupRepresentativeDecl(NonTargetName, OtherGD)) {
1448
313
    assert(OtherGD.getCanonicalDecl()
1449
313
               .getDecl()
1450
313
               ->getAsFunction()
1451
313
               ->isMultiVersion() &&
1452
313
           "Other GD should now be a multiversioned function");
1453
    // OtherFD is the version of this function that was mangled BEFORE
1454
    // becoming a MultiVersion function.  It potentially needs to be updated.
1455
0
    const FunctionDecl *OtherFD = OtherGD.getCanonicalDecl()
1456
313
                                      .getDecl()
1457
313
                                      ->getAsFunction()
1458
313
                                      ->getMostRecentDecl();
1459
313
    std::string OtherName = getMangledNameImpl(*this, OtherGD, OtherFD);
1460
    // This is so that if the initial version was already the 'default'
1461
    // version, we don't try to update it.
1462
313
    if (OtherName != NonTargetName) {
1463
      // Remove instead of erase, since others may have stored the StringRef
1464
      // to this.
1465
24
      const auto ExistingRecord = Manglings.find(NonTargetName);
1466
24
      if (ExistingRecord != std::end(Manglings))
1467
24
        Manglings.remove(&(*ExistingRecord));
1468
24
      auto Result = Manglings.insert(std::make_pair(OtherName, OtherGD));
1469
24
      StringRef OtherNameRef = MangledDeclNames[OtherGD.getCanonicalDecl()] =
1470
24
          Result.first->first();
1471
      // If this is the current decl is being created, make sure we update the name.
1472
24
      if (GD.getCanonicalDecl() == OtherGD.getCanonicalDecl())
1473
3
        CurName = OtherNameRef;
1474
24
      if (llvm::GlobalValue *Entry = GetGlobalValue(NonTargetName))
1475
19
        Entry->setName(OtherName);
1476
24
    }
1477
313
  }
1478
525
}
1479
1480
4.74M
StringRef CodeGenModule::getMangledName(GlobalDecl GD) {
1481
4.74M
  GlobalDecl CanonicalGD = GD.getCanonicalDecl();
1482
1483
  // Some ABIs don't have constructor variants.  Make sure that base and
1484
  // complete constructors get mangled the same.
1485
4.74M
  if (const auto *CD = dyn_cast<CXXConstructorDecl>(CanonicalGD.getDecl())) {
1486
334k
    if (!getTarget().getCXXABI().hasConstructorVariants()) {
1487
5.45k
      CXXCtorType OrigCtorType = GD.getCtorType();
1488
5.45k
      assert(OrigCtorType == Ctor_Base || OrigCtorType == Ctor_Complete);
1489
5.45k
      if (OrigCtorType == Ctor_Base)
1490
1.47k
        CanonicalGD = GlobalDecl(CD, Ctor_Complete);
1491
5.45k
    }
1492
334k
  }
1493
1494
  // In CUDA/HIP device compilation with -fgpu-rdc, the mangled name of a
1495
  // static device variable depends on whether the variable is referenced by
1496
  // a host or device host function. Therefore the mangled name cannot be
1497
  // cached.
1498
4.74M
  if (!LangOpts.CUDAIsDevice || 
!getContext().mayExternalize(GD.getDecl())3.84k
) {
1499
4.74M
    auto FoundName = MangledDeclNames.find(CanonicalGD);
1500
4.74M
    if (FoundName != MangledDeclNames.end())
1501
1.60M
      return FoundName->second;
1502
4.74M
  }
1503
1504
  // Keep the first result in the case of a mangling collision.
1505
3.13M
  const auto *ND = cast<NamedDecl>(GD.getDecl());
1506
3.13M
  std::string MangledName = getMangledNameImpl(*this, GD, ND);
1507
1508
  // Ensure either we have different ABIs between host and device compilations,
1509
  // says host compilation following MSVC ABI but device compilation follows
1510
  // Itanium C++ ABI or, if they follow the same ABI, kernel names after
1511
  // mangling should be the same after name stubbing. The later checking is
1512
  // very important as the device kernel name being mangled in host-compilation
1513
  // is used to resolve the device binaries to be executed. Inconsistent naming
1514
  // result in undefined behavior. Even though we cannot check that naming
1515
  // directly between host- and device-compilations, the host- and
1516
  // device-mangling in host compilation could help catching certain ones.
1517
3.13M
  assert(!isa<FunctionDecl>(ND) || !ND->hasAttr<CUDAGlobalAttr>() ||
1518
3.13M
         getContext().shouldExternalize(ND) || getLangOpts().CUDAIsDevice ||
1519
3.13M
         (getContext().getAuxTargetInfo() &&
1520
3.13M
          (getContext().getAuxTargetInfo()->getCXXABI() !=
1521
3.13M
           getContext().getTargetInfo().getCXXABI())) ||
1522
3.13M
         getCUDARuntime().getDeviceSideName(ND) ==
1523
3.13M
             getMangledNameImpl(
1524
3.13M
                 *this,
1525
3.13M
                 GD.getWithKernelReferenceKind(KernelReferenceKind::Kernel),
1526
3.13M
                 ND));
1527
1528
0
  auto Result = Manglings.insert(std::make_pair(MangledName, GD));
1529
3.13M
  return MangledDeclNames[CanonicalGD] = Result.first->first();
1530
4.74M
}
1531
1532
StringRef CodeGenModule::getBlockMangledName(GlobalDecl GD,
1533
1.18k
                                             const BlockDecl *BD) {
1534
1.18k
  MangleContext &MangleCtx = getCXXABI().getMangleContext();
1535
1.18k
  const Decl *D = GD.getDecl();
1536
1537
1.18k
  SmallString<256> Buffer;
1538
1.18k
  llvm::raw_svector_ostream Out(Buffer);
1539
1.18k
  if (!D)
1540
114
    MangleCtx.mangleGlobalBlock(BD,
1541
114
      dyn_cast_or_null<VarDecl>(initializedGlobalDecl.getDecl()), Out);
1542
1.06k
  else if (const auto *CD = dyn_cast<CXXConstructorDecl>(D))
1543
32
    MangleCtx.mangleCtorBlock(CD, GD.getCtorType(), BD, Out);
1544
1.03k
  else if (const auto *DD = dyn_cast<CXXDestructorDecl>(D))
1545
10
    MangleCtx.mangleDtorBlock(DD, GD.getDtorType(), BD, Out);
1546
1.02k
  else
1547
1.02k
    MangleCtx.mangleBlock(cast<DeclContext>(D), BD, Out);
1548
1549
1.18k
  auto Result = Manglings.insert(std::make_pair(Out.str(), BD));
1550
1.18k
  return Result.first->first();
1551
1.18k
}
1552
1553
3
const GlobalDecl CodeGenModule::getMangledNameDecl(StringRef Name) {
1554
3
  auto it = MangledDeclNames.begin();
1555
8
  while (it != MangledDeclNames.end()) {
1556
8
    if (it->second == Name)
1557
3
      return it->first;
1558
5
    it++;
1559
5
  }
1560
0
  return GlobalDecl();
1561
3
}
1562
1563
3.96M
llvm::GlobalValue *CodeGenModule::GetGlobalValue(StringRef Name) {
1564
3.96M
  return getModule().getNamedValue(Name);
1565
3.96M
}
1566
1567
/// AddGlobalCtor - Add a function to the list that will be called before
1568
/// main() runs.
1569
void CodeGenModule::AddGlobalCtor(llvm::Function *Ctor, int Priority,
1570
4.25k
                                  llvm::Constant *AssociatedData) {
1571
  // FIXME: Type coercion of void()* types.
1572
4.25k
  GlobalCtors.push_back(Structor(Priority, Ctor, AssociatedData));
1573
4.25k
}
1574
1575
/// AddGlobalDtor - Add a function to the list that will be called
1576
/// when the module is unloaded.
1577
void CodeGenModule::AddGlobalDtor(llvm::Function *Dtor, int Priority,
1578
76
                                  bool IsDtorAttrFunc) {
1579
76
  if (CodeGenOpts.RegisterGlobalDtorsWithAtExit &&
1580
76
      
(32
!getContext().getTargetInfo().getTriple().isOSAIX()32
||
IsDtorAttrFunc22
)) {
1581
22
    DtorsUsingAtExit[Priority].push_back(Dtor);
1582
22
    return;
1583
22
  }
1584
1585
  // FIXME: Type coercion of void()* types.
1586
54
  GlobalDtors.push_back(Structor(Priority, Dtor, nullptr));
1587
54
}
1588
1589
71.8k
void CodeGenModule::EmitCtorList(CtorList &Fns, const char *GlobalName) {
1590
71.8k
  if (Fns.empty()) 
return68.5k
;
1591
1592
  // Ctor function type is void()*.
1593
3.26k
  llvm::FunctionType* CtorFTy = llvm::FunctionType::get(VoidTy, false);
1594
3.26k
  llvm::Type *CtorPFTy = llvm::PointerType::get(CtorFTy,
1595
3.26k
      TheModule.getDataLayout().getProgramAddressSpace());
1596
1597
  // Get the type of a ctor entry, { i32, void ()*, i8* }.
1598
3.26k
  llvm::StructType *CtorStructTy = llvm::StructType::get(
1599
3.26k
      Int32Ty, CtorPFTy, VoidPtrTy);
1600
1601
  // Construct the constructor and destructor arrays.
1602
3.26k
  ConstantInitBuilder builder(*this);
1603
3.26k
  auto ctors = builder.beginArray(CtorStructTy);
1604
4.30k
  for (const auto &I : Fns) {
1605
4.30k
    auto ctor = ctors.beginStruct(CtorStructTy);
1606
4.30k
    ctor.addInt(Int32Ty, I.Priority);
1607
4.30k
    ctor.add(llvm::ConstantExpr::getBitCast(I.Initializer, CtorPFTy));
1608
4.30k
    if (I.AssociatedData)
1609
106
      ctor.add(llvm::ConstantExpr::getBitCast(I.AssociatedData, VoidPtrTy));
1610
4.20k
    else
1611
4.20k
      ctor.addNullPointer(VoidPtrTy);
1612
4.30k
    ctor.finishAndAddTo(ctors);
1613
4.30k
  }
1614
1615
3.26k
  auto list =
1616
3.26k
    ctors.finishAndCreateGlobal(GlobalName, getPointerAlign(),
1617
3.26k
                                /*constant*/ false,
1618
3.26k
                                llvm::GlobalValue::AppendingLinkage);
1619
1620
  // The LTO linker doesn't seem to like it when we set an alignment
1621
  // on appending variables.  Take it off as a workaround.
1622
3.26k
  list->setAlignment(llvm::None);
1623
1624
3.26k
  Fns.clear();
1625
3.26k
}
1626
1627
llvm::GlobalValue::LinkageTypes
1628
687k
CodeGenModule::getFunctionLinkage(GlobalDecl GD) {
1629
687k
  const auto *D = cast<FunctionDecl>(GD.getDecl());
1630
1631
687k
  GVALinkage Linkage = getContext().GetGVALinkageForFunction(D);
1632
1633
687k
  if (const auto *Dtor = dyn_cast<CXXDestructorDecl>(D))
1634
50.2k
    return getCXXABI().getCXXDestructorLinkage(Linkage, Dtor, GD.getDtorType());
1635
1636
637k
  if (isa<CXXConstructorDecl>(D) &&
1637
637k
      
cast<CXXConstructorDecl>(D)->isInheritingConstructor()120k
&&
1638
637k
      
Context.getTargetInfo().getCXXABI().isMicrosoft()537
) {
1639
    // Our approach to inheriting constructors is fundamentally different from
1640
    // that used by the MS ABI, so keep our inheriting constructor thunks
1641
    // internal rather than trying to pick an unambiguous mangling for them.
1642
36
    return llvm::GlobalValue::InternalLinkage;
1643
36
  }
1644
1645
637k
  return getLLVMLinkageForDeclarator(D, Linkage, /*IsConstantVariable=*/false);
1646
637k
}
1647
1648
103
llvm::ConstantInt *CodeGenModule::CreateCrossDsoCfiTypeId(llvm::Metadata *MD) {
1649
103
  llvm::MDString *MDS = dyn_cast<llvm::MDString>(MD);
1650
103
  if (!MDS) 
return nullptr9
;
1651
1652
94
  return llvm::ConstantInt::get(Int64Ty, llvm::MD5Hash(MDS->getString()));
1653
103
}
1654
1655
void CodeGenModule::SetLLVMFunctionAttributes(GlobalDecl GD,
1656
                                              const CGFunctionInfo &Info,
1657
350k
                                              llvm::Function *F, bool IsThunk) {
1658
350k
  unsigned CallingConv;
1659
350k
  llvm::AttributeList PAL;
1660
350k
  ConstructAttributeList(F->getName(), Info, GD, PAL, CallingConv,
1661
350k
                         /*AttrOnCallSite=*/false, IsThunk);
1662
350k
  F->setAttributes(PAL);
1663
350k
  F->setCallingConv(static_cast<llvm::CallingConv::ID>(CallingConv));
1664
350k
}
1665
1666
56
static void removeImageAccessQualifier(std::string& TyName) {
1667
56
  std::string ReadOnlyQual("__read_only");
1668
56
  std::string::size_type ReadOnlyPos = TyName.find(ReadOnlyQual);
1669
56
  if (ReadOnlyPos != std::string::npos)
1670
    // "+ 1" for the space after access qualifier.
1671
28
    TyName.erase(ReadOnlyPos, ReadOnlyQual.size() + 1);
1672
28
  else {
1673
28
    std::string WriteOnlyQual("__write_only");
1674
28
    std::string::size_type WriteOnlyPos = TyName.find(WriteOnlyQual);
1675
28
    if (WriteOnlyPos != std::string::npos)
1676
14
      TyName.erase(WriteOnlyPos, WriteOnlyQual.size() + 1);
1677
14
    else {
1678
14
      std::string ReadWriteQual("__read_write");
1679
14
      std::string::size_type ReadWritePos = TyName.find(ReadWriteQual);
1680
14
      if (ReadWritePos != std::string::npos)
1681
6
        TyName.erase(ReadWritePos, ReadWriteQual.size() + 1);
1682
14
    }
1683
28
  }
1684
56
}
1685
1686
// Returns the address space id that should be produced to the
1687
// kernel_arg_addr_space metadata. This is always fixed to the ids
1688
// as specified in the SPIR 2.0 specification in order to differentiate
1689
// for example in clGetKernelArgInfo() implementation between the address
1690
// spaces with targets without unique mapping to the OpenCL address spaces
1691
// (basically all single AS CPUs).
1692
385
static unsigned ArgInfoAddressSpace(LangAS AS) {
1693
385
  switch (AS) {
1694
264
  case LangAS::opencl_global:
1695
264
    return 1;
1696
10
  case LangAS::opencl_constant:
1697
10
    return 2;
1698
111
  case LangAS::opencl_local:
1699
111
    return 3;
1700
0
  case LangAS::opencl_generic:
1701
0
    return 4; // Not in SPIR 2.0 specs.
1702
0
  case LangAS::opencl_global_device:
1703
0
    return 5;
1704
0
  case LangAS::opencl_global_host:
1705
0
    return 6;
1706
0
  default:
1707
0
    return 0; // Assume private.
1708
385
  }
1709
385
}
1710
1711
void CodeGenModule::GenKernelArgMetadata(llvm::Function *Fn,
1712
                                         const FunctionDecl *FD,
1713
613
                                         CodeGenFunction *CGF) {
1714
613
  assert(((FD && CGF) || (!FD && !CGF)) &&
1715
613
         "Incorrect use - FD and CGF should either be both null or not!");
1716
  // Create MDNodes that represent the kernel arg metadata.
1717
  // Each MDNode is a list in the form of "key", N number of values which is
1718
  // the same number of values as their are kernel arguments.
1719
1720
0
  const PrintingPolicy &Policy = Context.getPrintingPolicy();
1721
1722
  // MDNode for the kernel argument address space qualifiers.
1723
613
  SmallVector<llvm::Metadata *, 8> addressQuals;
1724
1725
  // MDNode for the kernel argument access qualifiers (images only).
1726
613
  SmallVector<llvm::Metadata *, 8> accessQuals;
1727
1728
  // MDNode for the kernel argument type names.
1729
613
  SmallVector<llvm::Metadata *, 8> argTypeNames;
1730
1731
  // MDNode for the kernel argument base type names.
1732
613
  SmallVector<llvm::Metadata *, 8> argBaseTypeNames;
1733
1734
  // MDNode for the kernel argument type qualifiers.
1735
613
  SmallVector<llvm::Metadata *, 8> argTypeQuals;
1736
1737
  // MDNode for the kernel argument names.
1738
613
  SmallVector<llvm::Metadata *, 8> argNames;
1739
1740
613
  if (FD && 
CGF604
)
1741
1.35k
    
for (unsigned i = 0, e = FD->getNumParams(); 604
i != e;
++i754
) {
1742
754
      const ParmVarDecl *parm = FD->getParamDecl(i);
1743
      // Get argument name.
1744
754
      argNames.push_back(llvm::MDString::get(VMContext, parm->getName()));
1745
1746
754
      if (!getLangOpts().OpenCL)
1747
96
        continue;
1748
658
      QualType ty = parm->getType();
1749
658
      std::string typeQuals;
1750
1751
      // Get image and pipe access qualifier:
1752
658
      if (ty->isImageType() || 
ty->isPipeType()630
) {
1753
61
        const Decl *PDecl = parm;
1754
61
        if (auto *TD = dyn_cast<TypedefType>(ty))
1755
23
          PDecl = TD->getDecl();
1756
61
        const OpenCLAccessAttr *A = PDecl->getAttr<OpenCLAccessAttr>();
1757
61
        if (A && 
A->isWriteOnly()43
)
1758
12
          accessQuals.push_back(llvm::MDString::get(VMContext, "write_only"));
1759
49
        else if (A && 
A->isReadWrite()31
)
1760
4
          accessQuals.push_back(llvm::MDString::get(VMContext, "read_write"));
1761
45
        else
1762
45
          accessQuals.push_back(llvm::MDString::get(VMContext, "read_only"));
1763
61
      } else
1764
597
        accessQuals.push_back(llvm::MDString::get(VMContext, "none"));
1765
1766
1.31k
      auto getTypeSpelling = [&](QualType Ty) {
1767
1.31k
        auto typeName = Ty.getUnqualifiedType().getAsString(Policy);
1768
1769
1.31k
        if (Ty.isCanonical()) {
1770
968
          StringRef typeNameRef = typeName;
1771
          // Turn "unsigned type" to "utype"
1772
968
          if (typeNameRef.consume_front("unsigned "))
1773
122
            return std::string("u") + typeNameRef.str();
1774
846
          if (typeNameRef.consume_front("signed "))
1775
8
            return typeNameRef.str();
1776
846
        }
1777
1778
1.18k
        return typeName;
1779
1.31k
      };
1780
1781
658
      if (ty->isPointerType()) {
1782
324
        QualType pointeeTy = ty->getPointeeType();
1783
1784
        // Get address qualifier.
1785
324
        addressQuals.push_back(
1786
324
            llvm::ConstantAsMetadata::get(CGF->Builder.getInt32(
1787
324
                ArgInfoAddressSpace(pointeeTy.getAddressSpace()))));
1788
1789
        // Get argument type name.
1790
324
        std::string typeName = getTypeSpelling(pointeeTy) + "*";
1791
324
        std::string baseTypeName =
1792
324
            getTypeSpelling(pointeeTy.getCanonicalType()) + "*";
1793
324
        argTypeNames.push_back(llvm::MDString::get(VMContext, typeName));
1794
324
        argBaseTypeNames.push_back(
1795
324
            llvm::MDString::get(VMContext, baseTypeName));
1796
1797
        // Get argument type qualifiers:
1798
324
        if (ty.isRestrictQualified())
1799
18
          typeQuals = "restrict";
1800
324
        if (pointeeTy.isConstQualified() ||
1801
324
            
(pointeeTy.getAddressSpace() == LangAS::opencl_constant)306
)
1802
28
          typeQuals += typeQuals.empty() ? 
"const"18
:
" const"10
;
1803
324
        if (pointeeTy.isVolatileQualified())
1804
100
          typeQuals += typeQuals.empty() ? 
"volatile"88
:
" volatile"12
;
1805
334
      } else {
1806
334
        uint32_t AddrSpc = 0;
1807
334
        bool isPipe = ty->isPipeType();
1808
334
        if (ty->isImageType() || 
isPipe306
)
1809
61
          AddrSpc = ArgInfoAddressSpace(LangAS::opencl_global);
1810
1811
334
        addressQuals.push_back(
1812
334
            llvm::ConstantAsMetadata::get(CGF->Builder.getInt32(AddrSpc)));
1813
1814
        // Get argument type name.
1815
334
        ty = isPipe ? 
ty->castAs<PipeType>()->getElementType()33
:
ty301
;
1816
334
        std::string typeName = getTypeSpelling(ty);
1817
334
        std::string baseTypeName = getTypeSpelling(ty.getCanonicalType());
1818
1819
        // Remove access qualifiers on images
1820
        // (as they are inseparable from type in clang implementation,
1821
        // but OpenCL spec provides a special query to get access qualifier
1822
        // via clGetKernelArgInfo with CL_KERNEL_ARG_ACCESS_QUALIFIER):
1823
334
        if (ty->isImageType()) {
1824
28
          removeImageAccessQualifier(typeName);
1825
28
          removeImageAccessQualifier(baseTypeName);
1826
28
        }
1827
1828
334
        argTypeNames.push_back(llvm::MDString::get(VMContext, typeName));
1829
334
        argBaseTypeNames.push_back(
1830
334
            llvm::MDString::get(VMContext, baseTypeName));
1831
1832
334
        if (isPipe)
1833
33
          typeQuals = "pipe";
1834
334
      }
1835
658
      argTypeQuals.push_back(llvm::MDString::get(VMContext, typeQuals));
1836
658
    }
1837
1838
613
  if (getLangOpts().OpenCL) {
1839
526
    Fn->setMetadata("kernel_arg_addr_space",
1840
526
                    llvm::MDNode::get(VMContext, addressQuals));
1841
526
    Fn->setMetadata("kernel_arg_access_qual",
1842
526
                    llvm::MDNode::get(VMContext, accessQuals));
1843
526
    Fn->setMetadata("kernel_arg_type",
1844
526
                    llvm::MDNode::get(VMContext, argTypeNames));
1845
526
    Fn->setMetadata("kernel_arg_base_type",
1846
526
                    llvm::MDNode::get(VMContext, argBaseTypeNames));
1847
526
    Fn->setMetadata("kernel_arg_type_qual",
1848
526
                    llvm::MDNode::get(VMContext, argTypeQuals));
1849
526
  }
1850
613
  if (getCodeGenOpts().EmitOpenCLArgMetadata ||
1851
613
      
getCodeGenOpts().HIPSaveKernelArgName603
)
1852
11
    Fn->setMetadata("kernel_arg_name",
1853
11
                    llvm::MDNode::get(VMContext, argNames));
1854
613
}
1855
1856
/// Determines whether the language options require us to model
1857
/// unwind exceptions.  We treat -fexceptions as mandating this
1858
/// except under the fragile ObjC ABI with only ObjC exceptions
1859
/// enabled.  This means, for example, that C with -fexceptions
1860
/// enables this.
1861
314k
static bool hasUnwindExceptions(const LangOptions &LangOpts) {
1862
  // If exceptions are completely disabled, obviously this is false.
1863
314k
  if (!LangOpts.Exceptions) 
return false220k
;
1864
1865
  // If C++ exceptions are enabled, this is true.
1866
94.0k
  if (LangOpts.CXXExceptions) 
return true90.9k
;
1867
1868
  // If ObjC exceptions are enabled, this depends on the ABI.
1869
3.11k
  if (LangOpts.ObjCExceptions) {
1870
2.06k
    return LangOpts.ObjCRuntime.hasUnwindExceptions();
1871
2.06k
  }
1872
1873
1.04k
  return true;
1874
3.11k
}
1875
1876
static bool requiresMemberFunctionPointerTypeMetadata(CodeGenModule &CGM,
1877
110k
                                                      const CXXMethodDecl *MD) {
1878
  // Check that the type metadata can ever actually be used by a call.
1879
110k
  if (!CGM.getCodeGenOpts().LTOUnit ||
1880
110k
      
!CGM.HasHiddenLTOVisibility(MD->getParent())270
)
1881
110k
    return false;
1882
1883
  // Only functions whose address can be taken with a member function pointer
1884
  // need this sort of type metadata.
1885
218
  return !MD->isStatic() && !MD->isVirtual() && 
!isa<CXXConstructorDecl>(MD)128
&&
1886
218
         
!isa<CXXDestructorDecl>(MD)4
;
1887
110k
}
1888
1889
std::vector<const CXXRecordDecl *>
1890
3
CodeGenModule::getMostBaseClasses(const CXXRecordDecl *RD) {
1891
3
  llvm::SetVector<const CXXRecordDecl *> MostBases;
1892
1893
3
  std::function<void (const CXXRecordDecl *)> CollectMostBases;
1894
8
  CollectMostBases = [&](const CXXRecordDecl *RD) {
1895
8
    if (RD->getNumBases() == 0)
1896
4
      MostBases.insert(RD);
1897
8
    for (const CXXBaseSpecifier &B : RD->bases())
1898
5
      CollectMostBases(B.getType()->getAsCXXRecordDecl());
1899
8
  };
1900
3
  CollectMostBases(RD);
1901
3
  return MostBases.takeVector();
1902
3
}
1903
1904
llvm::GlobalVariable *
1905
111
CodeGenModule::GetOrCreateRTTIProxyGlobalVariable(llvm::Constant *Addr) {
1906
111
  auto It = RTTIProxyMap.find(Addr);
1907
111
  if (It != RTTIProxyMap.end())
1908
48
    return It->second;
1909
1910
63
  auto *FTRTTIProxy = new llvm::GlobalVariable(
1911
63
      TheModule, Addr->getType(),
1912
63
      /*isConstant=*/true, llvm::GlobalValue::PrivateLinkage, Addr,
1913
63
      "__llvm_rtti_proxy");
1914
63
  FTRTTIProxy->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
1915
1916
63
  RTTIProxyMap[Addr] = FTRTTIProxy;
1917
63
  return FTRTTIProxy;
1918
111
}
1919
1920
void CodeGenModule::SetLLVMFunctionAttributesForDefinition(const Decl *D,
1921
314k
                                                           llvm::Function *F) {
1922
314k
  llvm::AttrBuilder B(F->getContext());
1923
1924
314k
  if (CodeGenOpts.UnwindTables)
1925
89.4k
    B.addUWTableAttr(llvm::UWTableKind(CodeGenOpts.UnwindTables));
1926
1927
314k
  if (CodeGenOpts.StackClashProtector)
1928
15
    B.addAttribute("probe-stack", "inline-asm");
1929
1930
314k
  if (!hasUnwindExceptions(LangOpts))
1931
220k
    B.addAttribute(llvm::Attribute::NoUnwind);
1932
1933
314k
  if (!D || 
!D->hasAttr<NoStackProtectorAttr>()299k
) {
1934
314k
    if (LangOpts.getStackProtector() == LangOptions::SSPOn)
1935
88.4k
      B.addAttribute(llvm::Attribute::StackProtect);
1936
225k
    else if (LangOpts.getStackProtector() == LangOptions::SSPStrong)
1937
70
      B.addAttribute(llvm::Attribute::StackProtectStrong);
1938
225k
    else if (LangOpts.getStackProtector() == LangOptions::SSPReq)
1939
2
      B.addAttribute(llvm::Attribute::StackProtectReq);
1940
314k
  }
1941
1942
314k
  if (!D) {
1943
    // If we don't have a declaration to control inlining, the function isn't
1944
    // explicitly marked as alwaysinline for semantic reasons, and inlining is
1945
    // disabled, mark the function as noinline.
1946
14.9k
    if (!F->hasFnAttribute(llvm::Attribute::AlwaysInline) &&
1947
14.9k
        CodeGenOpts.getInlining() == CodeGenOptions::OnlyAlwaysInlining)
1948
14.4k
      B.addAttribute(llvm::Attribute::NoInline);
1949
1950
14.9k
    F->addFnAttrs(B);
1951
14.9k
    return;
1952
14.9k
  }
1953
1954
  // Track whether we need to add the optnone LLVM attribute,
1955
  // starting with the default for this optimization level.
1956
299k
  bool ShouldAddOptNone =
1957
299k
      !CodeGenOpts.DisableO0ImplyOptNone && 
CodeGenOpts.OptimizationLevel == 0281k
;
1958
  // We can't add optnone in the following cases, it won't pass the verifier.
1959
299k
  ShouldAddOptNone &= !D->hasAttr<MinSizeAttr>();
1960
299k
  ShouldAddOptNone &= !D->hasAttr<AlwaysInlineAttr>();
1961
1962
  // Add optnone, but do so only if the function isn't always_inline.
1963
299k
  if ((ShouldAddOptNone || 
D->hasAttr<OptimizeNoneAttr>()70.0k
) &&
1964
299k
      
!F->hasFnAttribute(llvm::Attribute::AlwaysInline)229k
) {
1965
229k
    B.addAttribute(llvm::Attribute::OptimizeNone);
1966
1967
    // OptimizeNone implies noinline; we should not be inlining such functions.
1968
229k
    B.addAttribute(llvm::Attribute::NoInline);
1969
1970
    // We still need to handle naked functions even though optnone subsumes
1971
    // much of their semantics.
1972
229k
    if (D->hasAttr<NakedAttr>())
1973
8
      B.addAttribute(llvm::Attribute::Naked);
1974
1975
    // OptimizeNone wins over OptimizeForSize and MinSize.
1976
229k
    F->removeFnAttr(llvm::Attribute::OptimizeForSize);
1977
229k
    F->removeFnAttr(llvm::Attribute::MinSize);
1978
229k
  } else 
if (69.9k
D->hasAttr<NakedAttr>()69.9k
) {
1979
    // Naked implies noinline: we should not be inlining such functions.
1980
2
    B.addAttribute(llvm::Attribute::Naked);
1981
2
    B.addAttribute(llvm::Attribute::NoInline);
1982
69.9k
  } else if (D->hasAttr<NoDuplicateAttr>()) {
1983
0
    B.addAttribute(llvm::Attribute::NoDuplicate);
1984
69.9k
  } else if (D->hasAttr<NoInlineAttr>() && 
!F->hasFnAttribute(llvm::Attribute::AlwaysInline)323
) {
1985
    // Add noinline if the function isn't always_inline.
1986
322
    B.addAttribute(llvm::Attribute::NoInline);
1987
69.5k
  } else if (D->hasAttr<AlwaysInlineAttr>() &&
1988
69.5k
             
!F->hasFnAttribute(llvm::Attribute::NoInline)16.6k
) {
1989
    // (noinline wins over always_inline, and we can't specify both in IR)
1990
16.6k
    B.addAttribute(llvm::Attribute::AlwaysInline);
1991
52.9k
  } else if (CodeGenOpts.getInlining() == CodeGenOptions::OnlyAlwaysInlining) {
1992
    // If we're not inlining, then force everything that isn't always_inline to
1993
    // carry an explicit noinline attribute.
1994
13.6k
    if (!F->hasFnAttribute(llvm::Attribute::AlwaysInline))
1995
13.6k
      B.addAttribute(llvm::Attribute::NoInline);
1996
39.2k
  } else {
1997
    // Otherwise, propagate the inline hint attribute and potentially use its
1998
    // absence to mark things as noinline.
1999
39.2k
    if (auto *FD = dyn_cast<FunctionDecl>(D)) {
2000
      // Search function and template pattern redeclarations for inline.
2001
38.9k
      auto CheckForInline = [](const FunctionDecl *FD) {
2002
40.3k
        auto CheckRedeclForInline = [](const FunctionDecl *Redecl) {
2003
40.3k
          return Redecl->isInlineSpecified();
2004
40.3k
        };
2005
38.9k
        if (any_of(FD->redecls(), CheckRedeclForInline))
2006
1.28k
          return true;
2007
37.7k
        const FunctionDecl *Pattern = FD->getTemplateInstantiationPattern();
2008
37.7k
        if (!Pattern)
2009
37.0k
          return false;
2010
704
        return any_of(Pattern->redecls(), CheckRedeclForInline);
2011
37.7k
      };
2012
38.9k
      if (CheckForInline(FD)) {
2013
1.29k
        B.addAttribute(llvm::Attribute::InlineHint);
2014
37.6k
      } else if (CodeGenOpts.getInlining() ==
2015
37.6k
                     CodeGenOptions::OnlyHintInlining &&
2016
37.6k
                 
!FD->isInlined()19
&&
2017
37.6k
                 
!F->hasFnAttribute(llvm::Attribute::AlwaysInline)9
) {
2018
9
        B.addAttribute(llvm::Attribute::NoInline);
2019
9
      }
2020
38.9k
    }
2021
39.2k
  }
2022
2023
  // Add other optimization related attributes if we are optimizing this
2024
  // function.
2025
299k
  if (!D->hasAttr<OptimizeNoneAttr>()) {
2026
299k
    if (D->hasAttr<ColdAttr>()) {
2027
15
      if (!ShouldAddOptNone)
2028
1
        B.addAttribute(llvm::Attribute::OptimizeForSize);
2029
15
      B.addAttribute(llvm::Attribute::Cold);
2030
15
    }
2031
299k
    if (D->hasAttr<HotAttr>())
2032
1
      B.addAttribute(llvm::Attribute::Hot);
2033
299k
    if (D->hasAttr<MinSizeAttr>())
2034
22
      B.addAttribute(llvm::Attribute::MinSize);
2035
299k
  }
2036
2037
299k
  F->addFnAttrs(B);
2038
2039
299k
  unsigned alignment = D->getMaxAlignment() / Context.getCharWidth();
2040
299k
  if (alignment)
2041
9
    F->setAlignment(llvm::Align(alignment));
2042
2043
299k
  if (!D->hasAttr<AlignedAttr>())
2044
299k
    if (LangOpts.FunctionAlignment)
2045
2
      F->setAlignment(llvm::Align(1ull << LangOpts.FunctionAlignment));
2046
2047
  // Some C++ ABIs require 2-byte alignment for member functions, in order to
2048
  // reserve a bit for differentiating between virtual and non-virtual member
2049
  // functions. If the current target's C++ ABI requires this and this is a
2050
  // member function, set its alignment accordingly.
2051
299k
  if (getTarget().getCXXABI().areMemberFunctionsAligned()) {
2052
298k
    if (F->getAlignment() < 2 && 
isa<CXXMethodDecl>(D)298k
)
2053
110k
      F->setAlignment(llvm::Align(2));
2054
298k
  }
2055
2056
  // In the cross-dso CFI mode with canonical jump tables, we want !type
2057
  // attributes on definitions only.
2058
299k
  if (CodeGenOpts.SanitizeCfiCrossDso &&
2059
299k
      
CodeGenOpts.SanitizeCfiCanonicalJumpTables47
) {
2060
26
    if (auto *FD = dyn_cast<FunctionDecl>(D)) {
2061
      // Skip available_externally functions. They won't be codegen'ed in the
2062
      // current module anyway.
2063
26
      if (getContext().GetGVALinkageForFunction(FD) != GVA_AvailableExternally)
2064
24
        CreateFunctionTypeMetadataForIcall(FD, F);
2065
26
    }
2066
26
  }
2067
2068
  // Emit type metadata on member functions for member function pointer checks.
2069
  // These are only ever necessary on definitions; we're guaranteed that the
2070
  // definition will be present in the LTO unit as a result of LTO visibility.
2071
299k
  auto *MD = dyn_cast<CXXMethodDecl>(D);
2072
299k
  if (MD && 
requiresMemberFunctionPointerTypeMetadata(*this, MD)110k
) {
2073
2
    for (const CXXRecordDecl *Base : getMostBaseClasses(MD->getParent())) {
2074
2
      llvm::Metadata *Id =
2075
2
          CreateMetadataIdentifierForType(Context.getMemberPointerType(
2076
2
              MD->getType(), Context.getRecordType(Base).getTypePtr()));
2077
2
      F->addTypeMetadata(0, Id);
2078
2
    }
2079
2
  }
2080
299k
}
2081
2082
void CodeGenModule::setLLVMFunctionFEnvAttributes(const FunctionDecl *D,
2083
213k
                                                  llvm::Function *F) {
2084
213k
  if (D->hasAttr<StrictFPAttr>()) {
2085
1.73k
    llvm::AttrBuilder FuncAttrs(F->getContext());
2086
1.73k
    FuncAttrs.addAttribute("strictfp");
2087
1.73k
    F->addFnAttrs(FuncAttrs);
2088
1.73k
  }
2089
213k
}
2090
2091
336k
void CodeGenModule::SetCommonAttributes(GlobalDecl GD, llvm::GlobalValue *GV) {
2092
336k
  const Decl *D = GD.getDecl();
2093
336k
  if (isa_and_nonnull<NamedDecl>(D))
2094
296k
    setGVProperties(GV, GD);
2095
40.0k
  else
2096
40.0k
    GV->setVisibility(llvm::GlobalValue::DefaultVisibility);
2097
2098
336k
  if (D && 
D->hasAttr<UsedAttr>()322k
)
2099
267
    addUsedOrCompilerUsedGlobal(GV);
2100
2101
336k
  if (CodeGenOpts.KeepStaticConsts && 
D4
&&
isa<VarDecl>(D)4
) {
2102
4
    const auto *VD = cast<VarDecl>(D);
2103
4
    if (VD->getType().isConstQualified() &&
2104
4
        VD->getStorageDuration() == SD_Static)
2105
4
      addUsedOrCompilerUsedGlobal(GV);
2106
4
  }
2107
336k
}
2108
2109
bool CodeGenModule::GetCPUAndFeaturesAttributes(GlobalDecl GD,
2110
649k
                                                llvm::AttrBuilder &Attrs) {
2111
  // Add target-cpu and target-features attributes to functions. If
2112
  // we have a decl for the function and it has a target attribute then
2113
  // parse that and add it to the feature set.
2114
649k
  StringRef TargetCPU = getTarget().getTargetOpts().CPU;
2115
649k
  StringRef TuneCPU = getTarget().getTargetOpts().TuneCPU;
2116
649k
  std::vector<std::string> Features;
2117
649k
  const auto *FD = dyn_cast_or_null<FunctionDecl>(GD.getDecl());
2118
649k
  FD = FD ? 
FD->getMostRecentDecl()576k
:
FD72.6k
;
2119
649k
  const auto *TD = FD ? 
FD->getAttr<TargetAttr>()576k
:
nullptr72.6k
;
2120
649k
  const auto *SD = FD ? 
FD->getAttr<CPUSpecificAttr>()576k
:
nullptr72.6k
;
2121
649k
  const auto *TC = FD ? 
FD->getAttr<TargetClonesAttr>()576k
:
nullptr72.6k
;
2122
649k
  bool AddedAttr = false;
2123
649k
  if (TD || 
SD628k
||
TC628k
) {
2124
20.7k
    llvm::StringMap<bool> FeatureMap;
2125
20.7k
    getContext().getFunctionFeatureMap(FeatureMap, GD);
2126
2127
    // Produce the canonical string for this set of features.
2128
20.7k
    for (const llvm::StringMap<bool>::value_type &Entry : FeatureMap)
2129
308k
      Features.push_back((Entry.getValue() ? 
"+"306k
:
"-"1.35k
) + Entry.getKey().str());
2130
2131
    // Now add the target-cpu and target-features to the function.
2132
    // While we populated the feature map above, we still need to
2133
    // get and parse the target attribute so we can get the cpu for
2134
    // the function.
2135
20.7k
    if (TD) {
2136
20.5k
      ParsedTargetAttr ParsedAttr = TD->parse();
2137
20.5k
      if (!ParsedAttr.Architecture.empty() &&
2138
20.5k
          
getTarget().isValidCPUName(ParsedAttr.Architecture)176
) {
2139
176
        TargetCPU = ParsedAttr.Architecture;
2140
176
        TuneCPU = ""; // Clear the tune CPU.
2141
176
      }
2142
20.5k
      if (!ParsedAttr.Tune.empty() &&
2143
20.5k
          
getTarget().isValidCPUName(ParsedAttr.Tune)2
)
2144
2
        TuneCPU = ParsedAttr.Tune;
2145
20.5k
    }
2146
2147
20.7k
    if (SD) {
2148
      // Apply the given CPU name as the 'tune-cpu' so that the optimizer can
2149
      // favor this processor.
2150
92
      TuneCPU = getTarget().getCPUSpecificTuneName(
2151
92
          SD->getCPUName(GD.getMultiVersionIndex())->getName());
2152
92
    }
2153
628k
  } else {
2154
    // Otherwise just add the existing target cpu and target features to the
2155
    // function.
2156
628k
    Features = getTarget().getTargetOpts().Features;
2157
628k
  }
2158
2159
649k
  if (!TargetCPU.empty()) {
2160
204k
    Attrs.addAttribute("target-cpu", TargetCPU);
2161
204k
    AddedAttr = true;
2162
204k
  }
2163
649k
  if (!TuneCPU.empty()) {
2164
189k
    Attrs.addAttribute("tune-cpu", TuneCPU);
2165
189k
    AddedAttr = true;
2166
189k
  }
2167
649k
  if (!Features.empty()) {
2168
634k
    llvm::sort(Features);
2169
634k
    Attrs.addAttribute("target-features", llvm::join(Features, ","));
2170
634k
    AddedAttr = true;
2171
634k
  }
2172
2173
649k
  return AddedAttr;
2174
649k
}
2175
2176
void CodeGenModule::setNonAliasAttributes(GlobalDecl GD,
2177
336k
                                          llvm::GlobalObject *GO) {
2178
336k
  const Decl *D = GD.getDecl();
2179
336k
  SetCommonAttributes(GD, GO);
2180
2181
336k
  if (D) {
2182
322k
    if (auto *GV = dyn_cast<llvm::GlobalVariable>(GO)) {
2183
24.0k
      if (D->hasAttr<RetainAttr>())
2184
5
        addUsedGlobal(GV);
2185
24.0k
      if (auto *SA = D->getAttr<PragmaClangBSSSectionAttr>())
2186
31
        GV->addAttribute("bss-section", SA->getName());
2187
24.0k
      if (auto *SA = D->getAttr<PragmaClangDataSectionAttr>())
2188
22
        GV->addAttribute("data-section", SA->getName());
2189
24.0k
      if (auto *SA = D->getAttr<PragmaClangRodataSectionAttr>())
2190
30
        GV->addAttribute("rodata-section", SA->getName());
2191
24.0k
      if (auto *SA = D->getAttr<PragmaClangRelroSectionAttr>())
2192
12
        GV->addAttribute("relro-section", SA->getName());
2193
24.0k
    }
2194
2195
322k
    if (auto *F = dyn_cast<llvm::Function>(GO)) {
2196
298k
      if (D->hasAttr<RetainAttr>())
2197
9
        addUsedGlobal(F);
2198
298k
      if (auto *SA = D->getAttr<PragmaClangTextSectionAttr>())
2199
8
        if (!D->getAttr<SectionAttr>())
2200
4
          F->addFnAttr("implicit-section-name", SA->getName());
2201
2202
298k
      llvm::AttrBuilder Attrs(F->getContext());
2203
298k
      if (GetCPUAndFeaturesAttributes(GD, Attrs)) {
2204
        // We know that GetCPUAndFeaturesAttributes will always have the
2205
        // newest set, since it has the newest possible FunctionDecl, so the
2206
        // new ones should replace the old.
2207
292k
        llvm::AttributeMask RemoveAttrs;
2208
292k
        RemoveAttrs.addAttribute("target-cpu");
2209
292k
        RemoveAttrs.addAttribute("target-features");
2210
292k
        RemoveAttrs.addAttribute("tune-cpu");
2211
292k
        F->removeFnAttrs(RemoveAttrs);
2212
292k
        F->addFnAttrs(Attrs);
2213
292k
      }
2214
298k
    }
2215
2216
322k
    if (const auto *CSA = D->getAttr<CodeSegAttr>())
2217
28
      GO->setSection(CSA->getName());
2218
322k
    else if (const auto *SA = D->getAttr<SectionAttr>())
2219
96
      GO->setSection(SA->getName());
2220
322k
  }
2221
2222
336k
  getTargetCodeGenInfo().setTargetAttributes(D, GO, *this);
2223
336k
}
2224
2225
void CodeGenModule::SetInternalFunctionAttributes(GlobalDecl GD,
2226
                                                  llvm::Function *F,
2227
42.6k
                                                  const CGFunctionInfo &FI) {
2228
42.6k
  const Decl *D = GD.getDecl();
2229
42.6k
  SetLLVMFunctionAttributes(GD, FI, F, /*IsThunk=*/false);
2230
42.6k
  SetLLVMFunctionAttributesForDefinition(D, F);
2231
2232
42.6k
  F->setLinkage(llvm::Function::InternalLinkage);
2233
2234
42.6k
  setNonAliasAttributes(GD, F);
2235
42.6k
}
2236
2237
340k
static void setLinkageForGV(llvm::GlobalValue *GV, const NamedDecl *ND) {
2238
  // Set linkage and visibility in case we never see a definition.
2239
340k
  LinkageInfo LV = ND->getLinkageAndVisibility();
2240
  // Don't set internal linkage on declarations.
2241
  // "extern_weak" is overloaded in LLVM; we probably should have
2242
  // separate linkage types for this.
2243
340k
  if (isExternallyVisible(LV.getLinkage()) &&
2244
340k
      
(309k
ND->hasAttr<WeakAttr>()309k
||
ND->isWeakImported()309k
))
2245
120
    GV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
2246
340k
}
2247
2248
void CodeGenModule::CreateFunctionTypeMetadataForIcall(const FunctionDecl *FD,
2249
306k
                                                       llvm::Function *F) {
2250
  // Only if we are checking indirect calls.
2251
306k
  if (!LangOpts.Sanitize.has(SanitizerKind::CFIICall))
2252
306k
    return;
2253
2254
  // Non-static class methods are handled via vtable or member function pointer
2255
  // checks elsewhere.
2256
76
  if (isa<CXXMethodDecl>(FD) && 
!cast<CXXMethodDecl>(FD)->isStatic()7
)
2257
6
    return;
2258
2259
70
  llvm::Metadata *MD = CreateMetadataIdentifierForType(FD->getType());
2260
70
  F->addTypeMetadata(0, MD);
2261
70
  F->addTypeMetadata(0, CreateMetadataIdentifierGeneralized(FD->getType()));
2262
2263
  // Emit a hash-based bit set entry for cross-DSO calls.
2264
70
  if (CodeGenOpts.SanitizeCfiCrossDso)
2265
29
    if (auto CrossDsoTypeId = CreateCrossDsoCfiTypeId(MD))
2266
29
      F->addTypeMetadata(0, llvm::ConstantAsMetadata::get(CrossDsoTypeId));
2267
70
}
2268
2269
void CodeGenModule::SetFunctionAttributes(GlobalDecl GD, llvm::Function *F,
2270
                                          bool IsIncompleteFunction,
2271
306k
                                          bool IsThunk) {
2272
2273
306k
  if (llvm::Intrinsic::ID IID = F->getIntrinsicID()) {
2274
    // If this is an intrinsic function, set the function's attributes
2275
    // to the intrinsic's attributes.
2276
0
    F->setAttributes(llvm::Intrinsic::getAttributes(getLLVMContext(), IID));
2277
0
    return;
2278
0
  }
2279
2280
306k
  const auto *FD = cast<FunctionDecl>(GD.getDecl());
2281
2282
306k
  if (!IsIncompleteFunction)
2283
306k
    SetLLVMFunctionAttributes(GD, getTypes().arrangeGlobalDeclaration(GD), F,
2284
306k
                              IsThunk);
2285
2286
  // Add the Returned attribute for "this", except for iOS 5 and earlier
2287
  // where substantial code, including the libstdc++ dylib, was compiled with
2288
  // GCC and does not actually return "this".
2289
306k
  if (!IsThunk && 
getCXXABI().HasThisReturn(GD)305k
&&
2290
306k
      
!(2.02k
getTriple().isiOS()2.02k
&&
getTriple().isOSVersionLT(6)212
)) {
2291
1.99k
    assert(!F->arg_empty() &&
2292
1.99k
           F->arg_begin()->getType()
2293
1.99k
             ->canLosslesslyBitCastTo(F->getReturnType()) &&
2294
1.99k
           "unexpected this return");
2295
0
    F->addParamAttr(0, llvm::Attribute::Returned);
2296
1.99k
  }
2297
2298
  // Only a few attributes are set on declarations; these may later be
2299
  // overridden by a definition.
2300
2301
0
  setLinkageForGV(F, FD);
2302
306k
  setGVProperties(F, FD);
2303
2304
  // Setup target-specific attributes.
2305
306k
  if (!IsIncompleteFunction && 
F->isDeclaration()306k
)
2306
306k
    getTargetCodeGenInfo().setTargetAttributes(FD, F, *this);
2307
2308
306k
  if (const auto *CSA = FD->getAttr<CodeSegAttr>())
2309
28
    F->setSection(CSA->getName());
2310
306k
  else if (const auto *SA = FD->getAttr<SectionAttr>())
2311
40
     F->setSection(SA->getName());
2312
2313
306k
  if (const auto *EA = FD->getAttr<ErrorAttr>()) {
2314
32
    if (EA->isError())
2315
21
      F->addFnAttr("dontcall-error", EA->getUserDiagnostic());
2316
11
    else if (EA->isWarning())
2317
11
      F->addFnAttr("dontcall-warn", EA->getUserDiagnostic());
2318
32
  }
2319
2320
  // If we plan on emitting this inline builtin, we can't treat it as a builtin.
2321
306k
  if (FD->isInlineBuiltinDeclaration()) {
2322
16
    const FunctionDecl *FDBody;
2323
16
    bool HasBody = FD->hasBody(FDBody);
2324
16
    (void)HasBody;
2325
16
    assert(HasBody && "Inline builtin declarations should always have an "
2326
16
                      "available body!");
2327
16
    if (shouldEmitFunction(FDBody))
2328
16
      F->addFnAttr(llvm::Attribute::NoBuiltin);
2329
16
  }
2330
2331
306k
  if (FD->isReplaceableGlobalAllocationFunction()) {
2332
    // A replaceable global allocation function does not act like a builtin by
2333
    // default, only if it is invoked by a new-expression or delete-expression.
2334
1.94k
    F->addFnAttr(llvm::Attribute::NoBuiltin);
2335
1.94k
  }
2336
2337
306k
  if (isa<CXXConstructorDecl>(FD) || 
isa<CXXDestructorDecl>(FD)263k
)
2338
62.6k
    F->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
2339
243k
  else if (const auto *MD = dyn_cast<CXXMethodDecl>(FD))
2340
58.0k
    if (MD->isVirtual())
2341
2.88k
      F->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
2342
2343
  // Don't emit entries for function declarations in the cross-DSO mode. This
2344
  // is handled with better precision by the receiving DSO. But if jump tables
2345
  // are non-canonical then we need type metadata in order to produce the local
2346
  // jump table.
2347
306k
  if (!CodeGenOpts.SanitizeCfiCrossDso ||
2348
306k
      
!CodeGenOpts.SanitizeCfiCanonicalJumpTables58
)
2349
306k
    CreateFunctionTypeMetadataForIcall(FD, F);
2350
2351
306k
  if (getLangOpts().OpenMP && 
FD->hasAttr<OMPDeclareSimdDeclAttr>()36.6k
)
2352
204
    getOpenMPRuntime().emitDeclareSimdFunction(FD, F);
2353
2354
306k
  if (const auto *CB = FD->getAttr<CallbackAttr>()) {
2355
    // Annotate the callback behavior as metadata:
2356
    //  - The callback callee (as argument number).
2357
    //  - The callback payloads (as argument numbers).
2358
17
    llvm::LLVMContext &Ctx = F->getContext();
2359
17
    llvm::MDBuilder MDB(Ctx);
2360
2361
    // The payload indices are all but the first one in the encoding. The first
2362
    // identifies the callback callee.
2363
17
    int CalleeIdx = *CB->encoding_begin();
2364
17
    ArrayRef<int> PayloadIndices(CB->encoding_begin() + 1, CB->encoding_end());
2365
17
    F->addMetadata(llvm::LLVMContext::MD_callback,
2366
17
                   *llvm::MDNode::get(Ctx, {MDB.createCallbackEncoding(
2367
17
                                               CalleeIdx, PayloadIndices,
2368
17
                                               /* VarArgsArePassed */ false)}));
2369
17
  }
2370
306k
}
2371
2372
754
void CodeGenModule::addUsedGlobal(llvm::GlobalValue *GV) {
2373
754
  assert((isa<llvm::Function>(GV) || !GV->isDeclaration()) &&
2374
754
         "Only globals with definition can force usage.");
2375
0
  LLVMUsed.emplace_back(GV);
2376
754
}
2377
2378
25.5k
void CodeGenModule::addCompilerUsedGlobal(llvm::GlobalValue *GV) {
2379
25.5k
  assert(!GV->isDeclaration() &&
2380
25.5k
         "Only globals with definition can force usage.");
2381
0
  LLVMCompilerUsed.emplace_back(GV);
2382
25.5k
}
2383
2384
273
void CodeGenModule::addUsedOrCompilerUsedGlobal(llvm::GlobalValue *GV) {
2385
273
  assert((isa<llvm::Function>(GV) || !GV->isDeclaration()) &&
2386
273
         "Only globals with definition can force usage.");
2387
273
  if (getTriple().isOSBinFormatELF())
2388
56
    LLVMCompilerUsed.emplace_back(GV);
2389
217
  else
2390
217
    LLVMUsed.emplace_back(GV);
2391
273
}
2392
2393
static void emitUsed(CodeGenModule &CGM, StringRef Name,
2394
71.8k
                     std::vector<llvm::WeakTrackingVH> &List) {
2395
  // Don't create llvm.used if there is no need.
2396
71.8k
  if (List.empty())
2397
69.3k
    return;
2398
2399
  // Convert List to what ConstantArray needs.
2400
2.47k
  SmallVector<llvm::Constant*, 8> UsedArray;
2401
2.47k
  UsedArray.resize(List.size());
2402
29.0k
  for (unsigned i = 0, e = List.size(); i != e; 
++i26.5k
) {
2403
26.5k
    UsedArray[i] =
2404
26.5k
        llvm::ConstantExpr::getPointerBitCastOrAddrSpaceCast(
2405
26.5k
            cast<llvm::Constant>(&*List[i]), CGM.Int8PtrTy);
2406
26.5k
  }
2407
2408
2.47k
  if (UsedArray.empty())
2409
0
    return;
2410
2.47k
  llvm::ArrayType *ATy = llvm::ArrayType::get(CGM.Int8PtrTy, UsedArray.size());
2411
2412
2.47k
  auto *GV = new llvm::GlobalVariable(
2413
2.47k
      CGM.getModule(), ATy, false, llvm::GlobalValue::AppendingLinkage,
2414
2.47k
      llvm::ConstantArray::get(ATy, UsedArray), Name);
2415
2416
2.47k
  GV->setSection("llvm.metadata");
2417
2.47k
}
2418
2419
35.9k
void CodeGenModule::emitLLVMUsed() {
2420
35.9k
  emitUsed(*this, "llvm.used", LLVMUsed);
2421
35.9k
  emitUsed(*this, "llvm.compiler.used", LLVMCompilerUsed);
2422
35.9k
}
2423
2424
14
void CodeGenModule::AppendLinkerOptions(StringRef Opts) {
2425
14
  auto *MDOpts = llvm::MDString::get(getLLVMContext(), Opts);
2426
14
  LinkerOptionsMetadata.push_back(llvm::MDNode::get(getLLVMContext(), MDOpts));
2427
14
}
2428
2429
14
void CodeGenModule::AddDetectMismatch(StringRef Name, StringRef Value) {
2430
14
  llvm::SmallString<32> Opt;
2431
14
  getTargetCodeGenInfo().getDetectMismatchOption(Name, Value, Opt);
2432
14
  if (Opt.empty())
2433
4
    return;
2434
10
  auto *MDOpts = llvm::MDString::get(getLLVMContext(), Opt);
2435
10
  LinkerOptionsMetadata.push_back(llvm::MDNode::get(getLLVMContext(), MDOpts));
2436
10
}
2437
2438
128
void CodeGenModule::AddDependentLib(StringRef Lib) {
2439
128
  auto &C = getLLVMContext();
2440
128
  if (getTarget().getTriple().isOSBinFormatELF()) {
2441
21
      ELFDependentLibraries.push_back(
2442
21
        llvm::MDNode::get(C, llvm::MDString::get(C, Lib)));
2443
21
    return;
2444
21
  }
2445
2446
107
  llvm::SmallString<24> Opt;
2447
107
  getTargetCodeGenInfo().getDependentLibraryOption(Lib, Opt);
2448
107
  auto *MDOpts = llvm::MDString::get(getLLVMContext(), Opt);
2449
107
  LinkerOptionsMetadata.push_back(llvm::MDNode::get(C, MDOpts));
2450
107
}
2451
2452
/// Add link options implied by the given module, including modules
2453
/// it depends on, using a postorder walk.
2454
static void addLinkOptionsPostorder(CodeGenModule &CGM, Module *Mod,
2455
                                    SmallVectorImpl<llvm::MDNode *> &Metadata,
2456
989k
                                    llvm::SmallPtrSet<Module *, 16> &Visited) {
2457
  // Import this module's parent.
2458
989k
  if (Mod->Parent && 
Visited.insert(Mod->Parent).second982k
) {
2459
53.7k
    addLinkOptionsPostorder(CGM, Mod->Parent, Metadata, Visited);
2460
53.7k
  }
2461
2462
  // Import this module's dependencies.
2463
5.73M
  for (Module *Import : llvm::reverse(Mod->Imports)) {
2464
5.73M
    if (Visited.insert(Import).second)
2465
871k
      addLinkOptionsPostorder(CGM, Import, Metadata, Visited);
2466
5.73M
  }
2467
2468
  // Add linker options to link against the libraries/frameworks
2469
  // described by this module.
2470
989k
  llvm::LLVMContext &Context = CGM.getLLVMContext();
2471
989k
  bool IsELF = CGM.getTarget().getTriple().isOSBinFormatELF();
2472
2473
  // For modules that use export_as for linking, use that module
2474
  // name instead.
2475
989k
  if (Mod->UseExportAsModuleLinkName)
2476
4
    return;
2477
2478
989k
  for (const Module::LinkLibrary &LL : llvm::reverse(Mod->LinkLibraries)) {
2479
    // Link against a framework.  Frameworks are currently Darwin only, so we
2480
    // don't to ask TargetCodeGenInfo for the spelling of the linker option.
2481
1.27k
    if (LL.IsFramework) {
2482
1.27k
      llvm::Metadata *Args[2] = {llvm::MDString::get(Context, "-framework"),
2483
1.27k
                                 llvm::MDString::get(Context, LL.Library)};
2484
2485
1.27k
      Metadata.push_back(llvm::MDNode::get(Context, Args));
2486
1.27k
      continue;
2487
1.27k
    }
2488
2489
    // Link against a library.
2490
2
    if (IsELF) {
2491
0
      llvm::Metadata *Args[2] = {
2492
0
          llvm::MDString::get(Context, "lib"),
2493
0
          llvm::MDString::get(Context, LL.Library),
2494
0
      };
2495
0
      Metadata.push_back(llvm::MDNode::get(Context, Args));
2496
2
    } else {
2497
2
      llvm::SmallString<24> Opt;
2498
2
      CGM.getTargetCodeGenInfo().getDependentLibraryOption(LL.Library, Opt);
2499
2
      auto *OptString = llvm::MDString::get(Context, Opt);
2500
2
      Metadata.push_back(llvm::MDNode::get(Context, OptString));
2501
2
    }
2502
2
  }
2503
989k
}
2504
2505
2.30k
void CodeGenModule::EmitModuleLinkOptions() {
2506
  // Collect the set of all of the modules we want to visit to emit link
2507
  // options, which is essentially the imported modules and all of their
2508
  // non-explicit child modules.
2509
2.30k
  llvm::SetVector<clang::Module *> LinkModules;
2510
2.30k
  llvm::SmallPtrSet<clang::Module *, 16> Visited;
2511
2.30k
  SmallVector<clang::Module *, 16> Stack;
2512
2513
  // Seed the stack with imported modules.
2514
2.30k
  for (Module *M : ImportedModules) {
2515
    // Do not add any link flags when an implementation TU of a module imports
2516
    // a header of that same module.
2517
2.05k
    if (M->getTopLevelModuleName() == getLangOpts().CurrentModule &&
2518
2.05k
        
!getLangOpts().isCompilingModule()6
)
2519
6
      continue;
2520
2.04k
    if (Visited.insert(M).second)
2521
2.04k
      Stack.push_back(M);
2522
2.04k
  }
2523
2524
  // Find all of the modules to import, making a little effort to prune
2525
  // non-leaf modules.
2526
601k
  while (!Stack.empty()) {
2527
599k
    clang::Module *Mod = Stack.pop_back_val();
2528
2529
599k
    bool AnyChildren = false;
2530
2531
    // Visit the submodules of this module.
2532
599k
    for (const auto &SM : Mod->submodules()) {
2533
      // Skip explicit children; they need to be explicitly imported to be
2534
      // linked against.
2535
597k
      if (SM->IsExplicit)
2536
130
        continue;
2537
2538
597k
      if (Visited.insert(SM).second) {
2539
597k
        Stack.push_back(SM);
2540
597k
        AnyChildren = true;
2541
597k
      }
2542
597k
    }
2543
2544
    // We didn't find any children, so add this module to the list of
2545
    // modules to link against.
2546
599k
    if (!AnyChildren) {
2547
562k
      LinkModules.insert(Mod);
2548
562k
    }
2549
599k
  }
2550
2551
  // Add link options for all of the imported modules in reverse topological
2552
  // order.  We don't do anything to try to order import link flags with respect
2553
  // to linker options inserted by things like #pragma comment().
2554
2.30k
  SmallVector<llvm::MDNode *, 16> MetadataArgs;
2555
2.30k
  Visited.clear();
2556
2.30k
  for (Module *M : LinkModules)
2557
562k
    if (Visited.insert(M).second)
2558
64.1k
      addLinkOptionsPostorder(*this, M, MetadataArgs, Visited);
2559
2.30k
  std::reverse(MetadataArgs.begin(), MetadataArgs.end());
2560
2.30k
  LinkerOptionsMetadata.append(MetadataArgs.begin(), MetadataArgs.end());
2561
2562
  // Add the linker options metadata flag.
2563
2.30k
  auto *NMD = getModule().getOrInsertNamedMetadata("llvm.linker.options");
2564
2.30k
  for (auto *MD : LinkerOptionsMetadata)
2565
1.41k
    NMD->addOperand(MD);
2566
2.30k
}
2567
2568
106k
void CodeGenModule::EmitDeferred() {
2569
  // Emit deferred declare target declarations.
2570
106k
  if (getLangOpts().OpenMP && 
!getLangOpts().OpenMPSimd18.4k
)
2571
10.8k
    getOpenMPRuntime().emitDeferredTargetDecls();
2572
2573
  // Emit code for any potentially referenced deferred decls.  Since a
2574
  // previously unused static decl may become used during the generation of code
2575
  // for a static function, iterate until no changes are made.
2576
2577
106k
  if (!DeferredVTables.empty()) {
2578
2.24k
    EmitDeferredVTables();
2579
2580
    // Emitting a vtable doesn't directly cause more vtables to
2581
    // become deferred, although it can cause functions to be
2582
    // emitted that then need those vtables.
2583
2.24k
    assert(DeferredVTables.empty());
2584
2.24k
  }
2585
2586
  // Emit CUDA/HIP static device variables referenced by host code only.
2587
  // Note we should not clear CUDADeviceVarODRUsedByHost since it is still
2588
  // needed for further handling.
2589
106k
  if (getLangOpts().CUDA && 
getLangOpts().CUDAIsDevice346
)
2590
212
    llvm::append_range(DeferredDeclsToEmit,
2591
212
                       getContext().CUDADeviceVarODRUsedByHost);
2592
2593
  // Stop if we're out of both deferred vtables and deferred declarations.
2594
106k
  if (DeferredDeclsToEmit.empty())
2595
28.1k
    return;
2596
2597
  // Grab the list of decls to emit. If EmitGlobalDefinition schedules more
2598
  // work, it will not interfere with this.
2599
78.6k
  std::vector<GlobalDecl> CurDeclsToEmit;
2600
78.6k
  CurDeclsToEmit.swap(DeferredDeclsToEmit);
2601
2602
149k
  for (GlobalDecl &D : CurDeclsToEmit) {
2603
    // We should call GetAddrOfGlobal with IsForDefinition set to true in order
2604
    // to get GlobalValue with exactly the type we need, not something that
2605
    // might had been created for another decl with the same mangled name but
2606
    // different type.
2607
149k
    llvm::GlobalValue *GV = dyn_cast<llvm::GlobalValue>(
2608
149k
        GetAddrOfGlobal(D, ForDefinition));
2609
2610
    // In case of different address spaces, we may still get a cast, even with
2611
    // IsForDefinition equal to true. Query mangled names table to get
2612
    // GlobalValue.
2613
149k
    if (!GV)
2614
255
      GV = GetGlobalValue(getMangledName(D));
2615
2616
    // Make sure GetGlobalValue returned non-null.
2617
149k
    assert(GV);
2618
2619
    // Check to see if we've already emitted this.  This is necessary
2620
    // for a couple of reasons: first, decls can end up in the
2621
    // deferred-decls queue multiple times, and second, decls can end
2622
    // up with definitions in unusual ways (e.g. by an extern inline
2623
    // function acquiring a strong function redefinition).  Just
2624
    // ignore these cases.
2625
149k
    if (!GV->isDeclaration())
2626
2.13k
      continue;
2627
2628
    // If this is OpenMP, check if it is legal to emit this global normally.
2629
147k
    if (LangOpts.OpenMP && 
OpenMPRuntime32.6k
&&
OpenMPRuntime->emitTargetGlobal(D)32.6k
)
2630
22
      continue;
2631
2632
    // Otherwise, emit the definition and move on to the next one.
2633
147k
    EmitGlobalDefinition(D, GV);
2634
2635
    // If we found out that we need to emit more decls, do that recursively.
2636
    // This has the advantage that the decls are emitted in a DFS and related
2637
    // ones are close together, which is convenient for testing.
2638
147k
    if (!DeferredVTables.empty() || 
!DeferredDeclsToEmit.empty()145k
) {
2639
70.8k
      EmitDeferred();
2640
70.8k
      assert(DeferredVTables.empty() && DeferredDeclsToEmit.empty());
2641
70.8k
    }
2642
147k
  }
2643
78.6k
}
2644
2645
35.9k
void CodeGenModule::EmitVTablesOpportunistically() {
2646
  // Try to emit external vtables as available_externally if they have emitted
2647
  // all inlined virtual functions.  It runs after EmitDeferred() and therefore
2648
  // is not allowed to create new references to things that need to be emitted
2649
  // lazily. Note that it also uses fact that we eagerly emitting RTTI.
2650
2651
35.9k
  assert((OpportunisticVTables.empty() || shouldOpportunisticallyEmitVTables())
2652
35.9k
         && "Only emit opportunistic vtables with optimizations");
2653
2654
25
  for (const CXXRecordDecl *RD : OpportunisticVTables) {
2655
25
    assert(getVTables().isVTableExternal(RD) &&
2656
25
           "This queue should only contain external vtables");
2657
25
    if (getCXXABI().canSpeculativelyEmitVTable(RD))
2658
4
      VTables.GenerateClassData(RD);
2659
25
  }
2660
35.9k
  OpportunisticVTables.clear();
2661
35.9k
}
2662
2663
35.9k
void CodeGenModule::EmitGlobalAnnotations() {
2664
35.9k
  if (Annotations.empty())
2665
35.8k
    return;
2666
2667
  // Create a new global variable for the ConstantStruct in the Module.
2668
6
  llvm::Constant *Array = llvm::ConstantArray::get(llvm::ArrayType::get(
2669
6
    Annotations[0]->getType(), Annotations.size()), Annotations);
2670
6
  auto *gv = new llvm::GlobalVariable(getModule(), Array->getType(), false,
2671
6
                                      llvm::GlobalValue::AppendingLinkage,
2672
6
                                      Array, "llvm.global.annotations");
2673
6
  gv->setSection(AnnotationSection);
2674
6
}
2675
2676
118
llvm::Constant *CodeGenModule::EmitAnnotationString(StringRef Str) {
2677
118
  llvm::Constant *&AStr = AnnotationStrings[Str];
2678
118
  if (AStr)
2679
65
    return AStr;
2680
2681
  // Not found yet, create a new global.
2682
53
  llvm::Constant *s = llvm::ConstantDataArray::getString(getLLVMContext(), Str);
2683
53
  auto *gv =
2684
53
      new llvm::GlobalVariable(getModule(), s->getType(), true,
2685
53
                               llvm::GlobalValue::PrivateLinkage, s, ".str");
2686
53
  gv->setSection(AnnotationSection);
2687
53
  gv->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
2688
53
  AStr = gv;
2689
53
  return gv;
2690
118
}
2691
2692
59
llvm::Constant *CodeGenModule::EmitAnnotationUnit(SourceLocation Loc) {
2693
59
  SourceManager &SM = getContext().getSourceManager();
2694
59
  PresumedLoc PLoc = SM.getPresumedLoc(Loc);
2695
59
  if (PLoc.isValid())
2696
59
    return EmitAnnotationString(PLoc.getFilename());
2697
0
  return EmitAnnotationString(SM.getBufferName(Loc));
2698
59
}
2699
2700
59
llvm::Constant *CodeGenModule::EmitAnnotationLineNo(SourceLocation L) {
2701
59
  SourceManager &SM = getContext().getSourceManager();
2702
59
  PresumedLoc PLoc = SM.getPresumedLoc(L);
2703
59
  unsigned LineNo = PLoc.isValid() ? PLoc.getLine() :
2704
59
    
SM.getExpansionLineNumber(L)0
;
2705
59
  return llvm::ConstantInt::get(Int32Ty, LineNo);
2706
59
}
2707
2708
52
llvm::Constant *CodeGenModule::EmitAnnotationArgs(const AnnotateAttr *Attr) {
2709
52
  ArrayRef<Expr *> Exprs = {Attr->args_begin(), Attr->args_size()};
2710
52
  if (Exprs.empty())
2711
45
    return llvm::ConstantPointerNull::get(GlobalsInt8PtrTy);
2712
2713
7
  llvm::FoldingSetNodeID ID;
2714
13
  for (Expr *E : Exprs) {
2715
13
    ID.Add(cast<clang::ConstantExpr>(E)->getAPValueResult());
2716
13
  }
2717
7
  llvm::Constant *&Lookup = AnnotationArgs[ID.ComputeHash()];
2718
7
  if (Lookup)
2719
1
    return Lookup;
2720
2721
6
  llvm::SmallVector<llvm::Constant *, 4> LLVMArgs;
2722
6
  LLVMArgs.reserve(Exprs.size());
2723
6
  ConstantEmitter ConstEmiter(*this);
2724
12
  llvm::transform(Exprs, std::back_inserter(LLVMArgs), [&](const Expr *E) {
2725
12
    const auto *CE = cast<clang::ConstantExpr>(E);
2726
12
    return ConstEmiter.emitAbstract(CE->getBeginLoc(), CE->getAPValueResult(),
2727
12
                                    CE->getType());
2728
12
  });
2729
6
  auto *Struct = llvm::ConstantStruct::getAnon(LLVMArgs);
2730
6
  auto *GV = new llvm::GlobalVariable(getModule(), Struct->getType(), true,
2731
6
                                      llvm::GlobalValue::PrivateLinkage, Struct,
2732
6
                                      ".args");
2733
6
  GV->setSection(AnnotationSection);
2734
6
  GV->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
2735
6
  auto *Bitcasted = llvm::ConstantExpr::getBitCast(GV, GlobalsInt8PtrTy);
2736
2737
6
  Lookup = Bitcasted;
2738
6
  return Bitcasted;
2739
7
}
2740
2741
llvm::Constant *CodeGenModule::EmitAnnotateAttr(llvm::GlobalValue *GV,
2742
                                                const AnnotateAttr *AA,
2743
31
                                                SourceLocation L) {
2744
  // Get the globals for file name, annotation, and the line number.
2745
31
  llvm::Constant *AnnoGV = EmitAnnotationString(AA->getAnnotation()),
2746
31
                 *UnitGV = EmitAnnotationUnit(L),
2747
31
                 *LineNoCst = EmitAnnotationLineNo(L),
2748
31
                 *Args = EmitAnnotationArgs(AA);
2749
2750
31
  llvm::Constant *GVInGlobalsAS = GV;
2751
31
  if (GV->getAddressSpace() !=
2752
31
      getDataLayout().getDefaultGlobalsAddressSpace()) {
2753
5
    GVInGlobalsAS = llvm::ConstantExpr::getAddrSpaceCast(
2754
5
        GV, GV->getValueType()->getPointerTo(
2755
5
                getDataLayout().getDefaultGlobalsAddressSpace()));
2756
5
  }
2757
2758
  // Create the ConstantStruct for the global annotation.
2759
31
  llvm::Constant *Fields[] = {
2760
31
      llvm::ConstantExpr::getBitCast(GVInGlobalsAS, GlobalsInt8PtrTy),
2761
31
      llvm::ConstantExpr::getBitCast(AnnoGV, GlobalsInt8PtrTy),
2762
31
      llvm::ConstantExpr::getBitCast(UnitGV, GlobalsInt8PtrTy),
2763
31
      LineNoCst,
2764
31
      Args,
2765
31
  };
2766
31
  return llvm::ConstantStruct::getAnon(Fields);
2767
31
}
2768
2769
void CodeGenModule::AddGlobalAnnotations(const ValueDecl *D,
2770
19
                                         llvm::GlobalValue *GV) {
2771
19
  assert(D->hasAttr<AnnotateAttr>() && "no annotate attribute");
2772
  // Get the struct elements for these annotations.
2773
0
  for (const auto *I : D->specific_attrs<AnnotateAttr>())
2774
31
    Annotations.push_back(EmitAnnotateAttr(GV, I, D->getLocation()));
2775
19
}
2776
2777
bool CodeGenModule::isInNoSanitizeList(SanitizerMask Kind, llvm::Function *Fn,
2778
7.54k
                                       SourceLocation Loc) const {
2779
7.54k
  const auto &NoSanitizeL = getContext().getNoSanitizeList();
2780
  // NoSanitize by function name.
2781
7.54k
  if (NoSanitizeL.containsFunction(Kind, Fn->getName()))
2782
18
    return true;
2783
  // NoSanitize by location. Check "mainfile" prefix.
2784
7.52k
  auto &SM = Context.getSourceManager();
2785
7.52k
  const FileEntry &MainFile = *SM.getFileEntryForID(SM.getMainFileID());
2786
7.52k
  if (NoSanitizeL.containsMainFile(Kind, MainFile.getName()))
2787
8
    return true;
2788
2789
  // Check "src" prefix.
2790
7.51k
  if (Loc.isValid())
2791
7.23k
    return NoSanitizeL.containsLocation(Kind, Loc);
2792
  // If location is unknown, this may be a compiler-generated function. Assume
2793
  // it's located in the main file.
2794
283
  return NoSanitizeL.containsFile(Kind, MainFile.getName());
2795
7.51k
}
2796
2797
bool CodeGenModule::isInNoSanitizeList(SanitizerMask Kind,
2798
                                       llvm::GlobalVariable *GV,
2799
                                       SourceLocation Loc, QualType Ty,
2800
1.70k
                                       StringRef Category) const {
2801
1.70k
  const auto &NoSanitizeL = getContext().getNoSanitizeList();
2802
1.70k
  if (NoSanitizeL.containsGlobal(Kind, GV->getName(), Category))
2803
28
    return true;
2804
1.67k
  auto &SM = Context.getSourceManager();
2805
1.67k
  if (NoSanitizeL.containsMainFile(
2806
1.67k
          Kind, SM.getFileEntryForID(SM.getMainFileID())->getName(), Category))
2807
8
    return true;
2808
1.66k
  if (NoSanitizeL.containsLocation(Kind, Loc, Category))
2809
64
    return true;
2810
2811
  // Check global type.
2812
1.60k
  if (!Ty.isNull()) {
2813
    // Drill down the array types: if global variable of a fixed type is
2814
    // not sanitized, we also don't instrument arrays of them.
2815
1.58k
    while (auto AT = dyn_cast<ArrayType>(Ty.getTypePtr()))
2816
112
      Ty = AT->getElementType();
2817
1.47k
    Ty = Ty.getCanonicalType().getUnqualifiedType();
2818
    // Only record types (classes, structs etc.) are ignored.
2819
1.47k
    if (Ty->isRecordType()) {
2820
566
      std::string TypeStr = Ty.getAsString(getContext().getPrintingPolicy());
2821
566
      if (NoSanitizeL.containsType(Kind, TypeStr, Category))
2822
2
        return true;
2823
566
    }
2824
1.47k
  }
2825
1.60k
  return false;
2826
1.60k
}
2827
2828
bool CodeGenModule::imbueXRayAttrs(llvm::Function *Fn, SourceLocation Loc,
2829
31
                                   StringRef Category) const {
2830
31
  const auto &XRayFilter = getContext().getXRayFilter();
2831
31
  using ImbueAttr = XRayFunctionFilter::ImbueAttribute;
2832
31
  auto Attr = ImbueAttr::NONE;
2833
31
  if (Loc.isValid())
2834
29
    Attr = XRayFilter.shouldImbueLocation(Loc, Category);
2835
31
  if (Attr == ImbueAttr::NONE)
2836
23
    Attr = XRayFilter.shouldImbueFunction(Fn->getName());
2837
31
  switch (Attr) {
2838
19
  case ImbueAttr::NONE:
2839
19
    return false;
2840
5
  case ImbueAttr::ALWAYS:
2841
5
    Fn->addFnAttr("function-instrument", "xray-always");
2842
5
    break;
2843
2
  case ImbueAttr::ALWAYS_ARG1:
2844
2
    Fn->addFnAttr("function-instrument", "xray-always");
2845
2
    Fn->addFnAttr("xray-log-args", "1");
2846
2
    break;
2847
5
  case ImbueAttr::NEVER:
2848
5
    Fn->addFnAttr("function-instrument", "xray-never");
2849
5
    break;
2850
31
  }
2851
12
  return true;
2852
31
}
2853
2854
bool CodeGenModule::isFunctionBlockedByProfileList(llvm::Function *Fn,
2855
486
                                                   SourceLocation Loc) const {
2856
486
  const auto &ProfileList = getContext().getProfileList();
2857
  // If the profile list is empty, then instrument everything.
2858
486
  if (ProfileList.isEmpty())
2859
476
    return false;
2860
10
  CodeGenOptions::ProfileInstrKind Kind = getCodeGenOpts().getProfileInstr();
2861
  // First, check the function name.
2862
10
  Optional<bool> V = ProfileList.isFunctionExcluded(Fn->getName(), Kind);
2863
10
  if (V)
2864
5
    return *V;
2865
  // Next, check the source location.
2866
5
  if (Loc.isValid()) {
2867
5
    Optional<bool> V = ProfileList.isLocationExcluded(Loc, Kind);
2868
5
    if (V)
2869
2
      return *V;
2870
5
  }
2871
  // If location is unknown, this may be a compiler-generated function. Assume
2872
  // it's located in the main file.
2873
3
  auto &SM = Context.getSourceManager();
2874
3
  if (const auto *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
2875
3
    Optional<bool> V = ProfileList.isFileExcluded(MainFile->getName(), Kind);
2876
3
    if (V)
2877
0
      return *V;
2878
3
  }
2879
3
  return ProfileList.getDefault();
2880
3
}
2881
2882
bool CodeGenModule::isFunctionBlockedFromProfileInstr(
2883
486
    llvm::Function *Fn, SourceLocation Loc) const {
2884
486
  if (isFunctionBlockedByProfileList(Fn, Loc))
2885
4
    return true;
2886
2887
482
  auto NumGroups = getCodeGenOpts().ProfileTotalFunctionGroups;
2888
482
  if (NumGroups > 1) {
2889
9
    auto Group = llvm::crc32(arrayRefFromStringRef(Fn->getName())) % NumGroups;
2890
9
    if (Group != getCodeGenOpts().ProfileSelectedFunctionGroup)
2891
6
      return true;
2892
9
  }
2893
476
  return false;
2894
482
}
2895
2896
6.36M
bool CodeGenModule::MustBeEmitted(const ValueDecl *Global) {
2897
  // Never defer when EmitAllDecls is specified.
2898
6.36M
  if (LangOpts.EmitAllDecls)
2899
587
    return true;
2900
2901
6.36M
  if (CodeGenOpts.KeepStaticConsts) {
2902
4
    const auto *VD = dyn_cast<VarDecl>(Global);
2903
4
    if (VD && VD->getType().isConstQualified() &&
2904
4
        VD->getStorageDuration() == SD_Static)
2905
4
      return true;
2906
4
  }
2907
2908
6.36M
  return getContext().DeclMustBeEmitted(Global);
2909
6.36M
}
2910
2911
158k
bool CodeGenModule::MayBeEmittedEagerly(const ValueDecl *Global) {
2912
  // In OpenMP 5.0 variables and function may be marked as
2913
  // device_type(host/nohost) and we should not emit them eagerly unless we sure
2914
  // that they must be emitted on the host/device. To be sure we need to have
2915
  // seen a declare target with an explicit mentioning of the function, we know
2916
  // we have if the level of the declare target attribute is -1. Note that we
2917
  // check somewhere else if we should emit this at all.
2918
158k
  if (LangOpts.OpenMP >= 50 && 
!LangOpts.OpenMPSimd19.8k
) {
2919
14.1k
    llvm::Optional<OMPDeclareTargetDeclAttr *> ActiveAttr =
2920
14.1k
        OMPDeclareTargetDeclAttr::getActiveAttr(Global);
2921
14.1k
    if (!ActiveAttr || 
(*ActiveAttr)->getLevel() != (unsigned)-12.24k
)
2922
14.1k
      return false;
2923
14.1k
  }
2924
2925
144k
  if (const auto *FD = dyn_cast<FunctionDecl>(Global)) {
2926
127k
    if (FD->getTemplateSpecializationKind() == TSK_ImplicitInstantiation)
2927
      // Implicit template instantiations may change linkage if they are later
2928
      // explicitly instantiated, so they should not be emitted eagerly.
2929
220
      return false;
2930
127k
  }
2931
144k
  if (const auto *VD = dyn_cast<VarDecl>(Global))
2932
16.8k
    if (Context.getInlineVariableDefinitionKind(VD) ==
2933
16.8k
        ASTContext::InlineVariableDefinitionKind::WeakUnknown)
2934
      // A definition of an inline constexpr static data member may change
2935
      // linkage later if it's redeclared outside the class.
2936
68
      return false;
2937
  // If OpenMP is enabled and threadprivates must be generated like TLS, delay
2938
  // codegen for global variables, because they may be marked as threadprivate.
2939
144k
  if (LangOpts.OpenMP && 
LangOpts.OpenMPUseTLS10.3k
&&
2940
144k
      
getContext().getTargetInfo().isTLSSupported()3.05k
&&
isa<VarDecl>(Global)2.85k
&&
2941
144k
      
!isTypeConstant(Global->getType(), false)1.52k
&&
2942
144k
      
!OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(Global)1.52k
)
2943
1.36k
    return false;
2944
2945
143k
  return true;
2946
144k
}
2947
2948
68
ConstantAddress CodeGenModule::GetAddrOfMSGuidDecl(const MSGuidDecl *GD) {
2949
68
  StringRef Name = getMangledName(GD);
2950
2951
  // The UUID descriptor should be pointer aligned.
2952
68
  CharUnits Alignment = CharUnits::fromQuantity(PointerAlignInBytes);
2953
2954
  // Look for an existing global.
2955
68
  if (llvm::GlobalVariable *GV = getModule().getNamedGlobal(Name))
2956
36
    return ConstantAddress(GV, GV->getValueType(), Alignment);
2957
2958
32
  ConstantEmitter Emitter(*this);
2959
32
  llvm::Constant *Init;
2960
2961
32
  APValue &V = GD->getAsAPValue();
2962
32
  if (!V.isAbsent()) {
2963
    // If possible, emit the APValue version of the initializer. In particular,
2964
    // this gets the type of the constant right.
2965
16
    Init = Emitter.emitForInitializer(
2966
16
        GD->getAsAPValue(), GD->getType().getAddressSpace(), GD->getType());
2967
16
  } else {
2968
    // As a fallback, directly construct the constant.
2969
    // FIXME: This may get padding wrong under esoteric struct layout rules.
2970
    // MSVC appears to create a complete type 'struct __s_GUID' that it
2971
    // presumably uses to represent these constants.
2972
16
    MSGuidDecl::Parts Parts = GD->getParts();
2973
16
    llvm::Constant *Fields[4] = {
2974
16
        llvm::ConstantInt::get(Int32Ty, Parts.Part1),
2975
16
        llvm::ConstantInt::get(Int16Ty, Parts.Part2),
2976
16
        llvm::ConstantInt::get(Int16Ty, Parts.Part3),
2977
16
        llvm::ConstantDataArray::getRaw(
2978
16
            StringRef(reinterpret_cast<char *>(Parts.Part4And5), 8), 8,
2979
16
            Int8Ty)};
2980
16
    Init = llvm::ConstantStruct::getAnon(Fields);
2981
16
  }
2982
2983
32
  auto *GV = new llvm::GlobalVariable(
2984
32
      getModule(), Init->getType(),
2985
32
      /*isConstant=*/true, llvm::GlobalValue::LinkOnceODRLinkage, Init, Name);
2986
32
  if (supportsCOMDAT())
2987
31
    GV->setComdat(TheModule.getOrInsertComdat(GV->getName()));
2988
32
  setDSOLocal(GV);
2989
2990
32
  if (!V.isAbsent()) {
2991
16
    Emitter.finalize(GV);
2992
16
    return ConstantAddress(GV, GV->getValueType(), Alignment);
2993
16
  }
2994
2995
16
  llvm::Type *Ty = getTypes().ConvertTypeForMem(GD->getType());
2996
16
  llvm::Constant *Addr = llvm::ConstantExpr::getBitCast(
2997
16
      GV, Ty->getPointerTo(GV->getAddressSpace()));
2998
16
  return ConstantAddress(Addr, Ty, Alignment);
2999
32
}
3000
3001
ConstantAddress CodeGenModule::GetAddrOfUnnamedGlobalConstantDecl(
3002
38
    const UnnamedGlobalConstantDecl *GCD) {
3003
38
  CharUnits Alignment = getContext().getTypeAlignInChars(GCD->getType());
3004
3005
38
  llvm::GlobalVariable **Entry = nullptr;
3006
38
  Entry = &UnnamedGlobalConstantDeclMap[GCD];
3007
38
  if (*Entry)
3008
2
    return ConstantAddress(*Entry, (*Entry)->getValueType(), Alignment);
3009
3010
36
  ConstantEmitter Emitter(*this);
3011
36
  llvm::Constant *Init;
3012
3013
36
  const APValue &V = GCD->getValue();
3014
3015
36
  assert(!V.isAbsent());
3016
0
  Init = Emitter.emitForInitializer(V, GCD->getType().getAddressSpace(),
3017
36
                                    GCD->getType());
3018
3019
36
  auto *GV = new llvm::GlobalVariable(getModule(), Init->getType(),
3020
36
                                      /*isConstant=*/true,
3021
36
                                      llvm::GlobalValue::PrivateLinkage, Init,
3022
36
                                      ".constant");
3023
36
  GV->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
3024
36
  GV->setAlignment(Alignment.getAsAlign());
3025
3026
36
  Emitter.finalize(GV);
3027
3028
36
  *Entry = GV;
3029
36
  return ConstantAddress(GV, GV->getValueType(), Alignment);
3030
38
}
3031
3032
ConstantAddress CodeGenModule::GetAddrOfTemplateParamObject(
3033
7
    const TemplateParamObjectDecl *TPO) {
3034
7
  StringRef Name = getMangledName(TPO);
3035
7
  CharUnits Alignment = getNaturalTypeAlignment(TPO->getType());
3036
3037
7
  if (llvm::GlobalVariable *GV = getModule().getNamedGlobal(Name))
3038
2
    return ConstantAddress(GV, GV->getValueType(), Alignment);
3039
3040
5
  ConstantEmitter Emitter(*this);
3041
5
  llvm::Constant *Init = Emitter.emitForInitializer(
3042
5
        TPO->getValue(), TPO->getType().getAddressSpace(), TPO->getType());
3043
3044
5
  if (!Init) {
3045
0
    ErrorUnsupported(TPO, "template parameter object");
3046
0
    return ConstantAddress::invalid();
3047
0
  }
3048
3049
5
  auto *GV = new llvm::GlobalVariable(
3050
5
      getModule(), Init->getType(),
3051
5
      /*isConstant=*/true, llvm::GlobalValue::LinkOnceODRLinkage, Init, Name);
3052
5
  if (supportsCOMDAT())
3053
5
    GV->setComdat(TheModule.getOrInsertComdat(GV->getName()));
3054
5
  Emitter.finalize(GV);
3055
3056
5
  return ConstantAddress(GV, GV->getValueType(), Alignment);
3057
5
}
3058
3059
36
ConstantAddress CodeGenModule::GetWeakRefReference(const ValueDecl *VD) {
3060
36
  const AliasAttr *AA = VD->getAttr<AliasAttr>();
3061
36
  assert(AA && "No alias?");
3062
3063
0
  CharUnits Alignment = getContext().getDeclAlign(VD);
3064
36
  llvm::Type *DeclTy = getTypes().ConvertTypeForMem(VD->getType());
3065
3066
  // See if there is already something with the target's name in the module.
3067
36
  llvm::GlobalValue *Entry = GetGlobalValue(AA->getAliasee());
3068
36
  if (Entry) {
3069
6
    unsigned AS = getContext().getTargetAddressSpace(VD->getType());
3070
6
    auto Ptr = llvm::ConstantExpr::getBitCast(Entry, DeclTy->getPointerTo(AS));
3071
6
    return ConstantAddress(Ptr, DeclTy, Alignment);
3072
6
  }
3073
3074
30
  llvm::Constant *Aliasee;
3075
30
  if (isa<llvm::FunctionType>(DeclTy))
3076
25
    Aliasee = GetOrCreateLLVMFunction(AA->getAliasee(), DeclTy,
3077
25
                                      GlobalDecl(cast<FunctionDecl>(VD)),
3078
25
                                      /*ForVTable=*/false);
3079
5
  else
3080
5
    Aliasee = GetOrCreateLLVMGlobal(AA->getAliasee(), DeclTy, LangAS::Default,
3081
5
                                    nullptr);
3082
3083
30
  auto *F = cast<llvm::GlobalValue>(Aliasee);
3084
30
  F->setLinkage(llvm::Function::ExternalWeakLinkage);
3085
30
  WeakRefReferences.insert(F);
3086
3087
30
  return ConstantAddress(Aliasee, DeclTy, Alignment);
3088
36
}
3089
3090
24.1M
void CodeGenModule::EmitGlobal(GlobalDecl GD) {
3091
24.1M
  const auto *Global = cast<ValueDecl>(GD.getDecl());
3092
3093
  // Weak references don't produce any output by themselves.
3094
24.1M
  if (Global->hasAttr<WeakRefAttr>())
3095
34
    return;
3096
3097
  // If this is an alias definition (which otherwise looks like a declaration)
3098
  // emit it now.
3099
24.1M
  if (Global->hasAttr<AliasAttr>())
3100
150
    return EmitAliasDefinition(GD);
3101
3102
  // IFunc like an alias whose value is resolved at runtime by calling resolver.
3103
24.1M
  if (Global->hasAttr<IFuncAttr>())
3104
21
    return emitIFuncDefinition(GD);
3105
3106
  // If this is a cpu_dispatch multiversion function, emit the resolver.
3107
24.1M
  if (Global->hasAttr<CPUDispatchAttr>())
3108
41
    return emitCPUDispatchDefinition(GD);
3109
3110
  // If this is CUDA, be selective about which declarations we emit.
3111
24.1M
  if (LangOpts.CUDA) {
3112
6.21k
    if (LangOpts.CUDAIsDevice) {
3113
5.03k
      if (!Global->hasAttr<CUDADeviceAttr>() &&
3114
5.03k
          
!Global->hasAttr<CUDAGlobalAttr>()1.43k
&&
3115
5.03k
          
!Global->hasAttr<CUDAConstantAttr>()1.29k
&&
3116
5.03k
          
!Global->hasAttr<CUDASharedAttr>()1.17k
&&
3117
5.03k
          
!Global->getType()->isCUDADeviceBuiltinSurfaceType()1.16k
&&
3118
5.03k
          
!Global->getType()->isCUDADeviceBuiltinTextureType()1.16k
)
3119
1.16k
        return;
3120
5.03k
    } else {
3121
      // We need to emit host-side 'shadows' for all global
3122
      // device-side variables because the CUDA runtime needs their
3123
      // size and host-side address in order to provide access to
3124
      // their device-side incarnations.
3125
3126
      // So device-only functions are the only things we skip.
3127
1.17k
      if (isa<FunctionDecl>(Global) && 
!Global->hasAttr<CUDAHostAttr>()897
&&
3128
1.17k
          
Global->hasAttr<CUDADeviceAttr>()632
)
3129
177
        return;
3130
3131
1.00k
      assert((isa<FunctionDecl>(Global) || isa<VarDecl>(Global)) &&
3132
1.00k
             "Expected Variable or Function");
3133
1.00k
    }
3134
6.21k
  }
3135
3136
24.1M
  if (LangOpts.OpenMP) {
3137
    // If this is OpenMP, check if it is legal to emit this global normally.
3138
81.5k
    if (OpenMPRuntime && OpenMPRuntime->emitTargetGlobal(GD))
3139
27.6k
      return;
3140
53.9k
    if (auto *DRD = dyn_cast<OMPDeclareReductionDecl>(Global)) {
3141
10
      if (MustBeEmitted(Global))
3142
10
        EmitOMPDeclareReduction(DRD);
3143
10
      return;
3144
53.9k
    } else if (auto *DMD = dyn_cast<OMPDeclareMapperDecl>(Global)) {
3145
0
      if (MustBeEmitted(Global))
3146
0
        EmitOMPDeclareMapper(DMD);
3147
0
      return;
3148
0
    }
3149
53.9k
  }
3150
3151
  // Ignore declarations, they will be emitted on their first use.
3152
24.0M
  if (const auto *FD = dyn_cast<FunctionDecl>(Global)) {
3153
    // Forward declarations are emitted lazily on first use.
3154
22.3M
    if (!FD->doesThisDeclarationHaveABody()) {
3155
19.8M
      if (!FD->doesDeclarationForceExternallyVisibleDefinition())
3156
19.8M
        return;
3157
3158
23
      StringRef MangledName = getMangledName(GD);
3159
3160
      // Compute the function info and LLVM type.
3161
23
      const CGFunctionInfo &FI = getTypes().arrangeGlobalDeclaration(GD);
3162
23
      llvm::Type *Ty = getTypes().GetFunctionType(FI);
3163
3164
23
      GetOrCreateLLVMFunction(MangledName, Ty, GD, /*ForVTable=*/false,
3165
23
                              /*DontDefer=*/false);
3166
23
      return;
3167
19.8M
    }
3168
22.3M
  } else {
3169
1.68M
    const auto *VD = cast<VarDecl>(Global);
3170
1.68M
    assert(VD->isFileVarDecl() && "Cannot emit local var decl as global.");
3171
1.68M
    if (VD->isThisDeclarationADefinition() != VarDecl::Definition &&
3172
1.68M
        
!Context.isMSStaticDataMemberInlineDefinition(VD)931k
) {
3173
931k
      if (LangOpts.OpenMP) {
3174
        // Emit declaration of the must-be-emitted declare target variable.
3175
1.13k
        if (llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res =
3176
1.13k
                OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD)) {
3177
140
          bool UnifiedMemoryEnabled =
3178
140
              getOpenMPRuntime().hasRequiresUnifiedSharedMemory();
3179
140
          if (*Res == OMPDeclareTargetDeclAttr::MT_To &&
3180
140
              
!UnifiedMemoryEnabled106
) {
3181
106
            (void)GetAddrOfGlobalVar(VD);
3182
106
          } else {
3183
34
            assert(((*Res == OMPDeclareTargetDeclAttr::MT_Link) ||
3184
34
                    (*Res == OMPDeclareTargetDeclAttr::MT_To &&
3185
34
                     UnifiedMemoryEnabled)) &&
3186
34
                   "Link clause or to clause with unified memory expected.");
3187
0
            (void)getOpenMPRuntime().getAddrOfDeclareTargetVar(VD);
3188
34
          }
3189
3190
0
          return;
3191
140
        }
3192
1.13k
      }
3193
      // If this declaration may have caused an inline variable definition to
3194
      // change linkage, make sure that it's emitted.
3195
931k
      if (Context.getInlineVariableDefinitionKind(VD) ==
3196
931k
          ASTContext::InlineVariableDefinitionKind::Strong)
3197
7
        GetAddrOfGlobalVar(VD);
3198
931k
      return;
3199
931k
    }
3200
1.68M
  }
3201
3202
  // Defer code generation to first use when possible, e.g. if this is an inline
3203
  // function. If the global must always be emitted, do it eagerly if possible
3204
  // to benefit from cache locality.
3205
3.25M
  if (MustBeEmitted(Global) && 
MayBeEmittedEagerly(Global)151k
) {
3206
    // Emit the definition if it can't be deferred.
3207
143k
    EmitGlobalDefinition(GD);
3208
143k
    return;
3209
143k
  }
3210
3211
  // If we're deferring emission of a C++ variable with an
3212
  // initializer, remember the order in which it appeared in the file.
3213
3.11M
  if (getLangOpts().CPlusPlus && 
isa<VarDecl>(Global)1.59M
&&
3214
3.11M
      
cast<VarDecl>(Global)->hasInit()733k
) {
3215
36.3k
    DelayedCXXInitPosition[Global] = CXXGlobalInits.size();
3216
36.3k
    CXXGlobalInits.push_back(nullptr);
3217
36.3k
  }
3218
3219
3.11M
  StringRef MangledName = getMangledName(GD);
3220
3.11M
  if (GetGlobalValue(MangledName) != nullptr) {
3221
    // The value has already been used and should therefore be emitted.
3222
12.3k
    addDeferredDeclToEmit(GD);
3223
3.10M
  } else if (MustBeEmitted(Global)) {
3224
    // The value must be emitted, but cannot be emitted eagerly.
3225
7.80k
    assert(!MayBeEmittedEagerly(Global));
3226
0
    addDeferredDeclToEmit(GD);
3227
3.09M
  } else {
3228
    // Otherwise, remember that we saw a deferred decl with this name.  The
3229
    // first use of the mangled name will cause it to move into
3230
    // DeferredDeclsToEmit.
3231
3.09M
    DeferredDecls[MangledName] = GD;
3232
3.09M
  }
3233
3.11M
}
3234
3235
// Check if T is a class type with a destructor that's not dllimport.
3236
48
static bool HasNonDllImportDtor(QualType T) {
3237
48
  if (const auto *RT = T->getBaseElementTypeUnsafe()->getAs<RecordType>())
3238
9
    if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl()))
3239
9
      if (RD->getDestructor() && !RD->getDestructor()->hasAttr<DLLImportAttr>())
3240
6
        return true;
3241
3242
42
  return false;
3243
48
}
3244
3245
namespace {
3246
  struct FunctionIsDirectlyRecursive
3247
      : public ConstStmtVisitor<FunctionIsDirectlyRecursive, bool> {
3248
    const StringRef Name;
3249
    const Builtin::Context &BI;
3250
    FunctionIsDirectlyRecursive(StringRef N, const Builtin::Context &C)
3251
50
        : Name(N), BI(C) {}
3252
3253
13
    bool VisitCallExpr(const CallExpr *E) {
3254
13
      const FunctionDecl *FD = E->getDirectCallee();
3255
13
      if (!FD)
3256
0
        return false;
3257
13
      AsmLabelAttr *Attr = FD->getAttr<AsmLabelAttr>();
3258
13
      if (Attr && 
Name == Attr->getLabel()1
)
3259
1
        return true;
3260
12
      unsigned BuiltinID = FD->getBuiltinID();
3261
12
      if (!BuiltinID || 
!BI.isLibFunction(BuiltinID)3
)
3262
10
        return false;
3263
2
      StringRef BuiltinName = BI.getName(BuiltinID);
3264
2
      if (BuiltinName.startswith("__builtin_") &&
3265
2
          Name == BuiltinName.slice(strlen("__builtin_"), StringRef::npos)) {
3266
2
        return true;
3267
2
      }
3268
0
      return false;
3269
2
    }
3270
3271
331
    bool VisitStmt(const Stmt *S) {
3272
331
      for (const Stmt *Child : S->children())
3273
297
        if (Child && 
this->Visit(Child)294
)
3274
6
          return true;
3275
325
      return false;
3276
331
    }
3277
  };
3278
3279
  // Make sure we're not referencing non-imported vars or functions.
3280
  struct DLLImportFunctionVisitor
3281
      : public RecursiveASTVisitor<DLLImportFunctionVisitor> {
3282
    bool SafeToInline = true;
3283
3284
128
    bool shouldVisitImplicitCode() const { return true; }
3285
3286
43
    bool VisitVarDecl(VarDecl *VD) {
3287
43
      if (VD->getTLSKind()) {
3288
        // A thread-local variable cannot be imported.
3289
2
        SafeToInline = false;
3290
2
        return SafeToInline;
3291
2
      }
3292
3293
      // A variable definition might imply a destructor call.
3294
41
      if (VD->isThisDeclarationADefinition())
3295
41
        SafeToInline = !HasNonDllImportDtor(VD->getType());
3296
3297
41
      return SafeToInline;
3298
43
    }
3299
3300
2
    bool VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) {
3301
2
      if (const auto *D = E->getTemporary()->getDestructor())
3302
2
        SafeToInline = D->hasAttr<DLLImportAttr>();
3303
2
      return SafeToInline;
3304
2
    }
3305
3306
33
    bool VisitDeclRefExpr(DeclRefExpr *E) {
3307
33
      ValueDecl *VD = E->getDecl();
3308
33
      if (isa<FunctionDecl>(VD))
3309
6
        SafeToInline = VD->hasAttr<DLLImportAttr>();
3310
27
      else if (VarDecl *V = dyn_cast<VarDecl>(VD))
3311
27
        SafeToInline = !V->hasGlobalStorage() || 
V->hasAttr<DLLImportAttr>()17
;
3312
33
      return SafeToInline;
3313
33
    }
3314
3315
11
    bool VisitCXXConstructExpr(CXXConstructExpr *E) {
3316
11
      SafeToInline = E->getConstructor()->hasAttr<DLLImportAttr>();
3317
11
      return SafeToInline;
3318
11
    }
3319
3320
7
    bool VisitCXXMemberCallExpr(CXXMemberCallExpr *E) {
3321
7
      CXXMethodDecl *M = E->getMethodDecl();
3322
7
      if (!M) {
3323
        // Call through a pointer to member function. This is safe to inline.
3324
2
        SafeToInline = true;
3325
5
      } else {
3326
5
        SafeToInline = M->hasAttr<DLLImportAttr>();
3327
5
      }
3328
7
      return SafeToInline;
3329
7
    }
3330
3331
4
    bool VisitCXXDeleteExpr(CXXDeleteExpr *E) {
3332
4
      SafeToInline = E->getOperatorDelete()->hasAttr<DLLImportAttr>();
3333
4
      return SafeToInline;
3334
4
    }
3335
3336
4
    bool VisitCXXNewExpr(CXXNewExpr *E) {
3337
4
      SafeToInline = E->getOperatorNew()->hasAttr<DLLImportAttr>();
3338
4
      return SafeToInline;
3339
4
    }
3340
  };
3341
}
3342
3343
// isTriviallyRecursive - Check if this function calls another
3344
// decl that, because of the asm attribute or the other decl being a builtin,
3345
// ends up pointing to itself.
3346
bool
3347
313
CodeGenModule::isTriviallyRecursive(const FunctionDecl *FD) {
3348
313
  StringRef Name;
3349
313
  if (getCXXABI().getMangleContext().shouldMangleDeclName(FD)) {
3350
    // asm labels are a special kind of mangling we have to support.
3351
264
    AsmLabelAttr *Attr = FD->getAttr<AsmLabelAttr>();
3352
264
    if (!Attr)
3353
263
      return false;
3354
1
    Name = Attr->getLabel();
3355
49
  } else {
3356
49
    Name = FD->getName();
3357
49
  }
3358
3359
50
  FunctionIsDirectlyRecursive Walker(Name, Context.BuiltinInfo);
3360
50
  const Stmt *Body = FD->getBody();
3361
50
  return Body ? Walker.Visit(Body) : 
false0
;
3362
313
}
3363
3364
270k
bool CodeGenModule::shouldEmitFunction(GlobalDecl GD) {
3365
270k
  if (getFunctionLinkage(GD) != llvm::Function::AvailableExternallyLinkage)
3366
269k
    return true;
3367
1.36k
  const auto *F = cast<FunctionDecl>(GD.getDecl());
3368
1.36k
  if (CodeGenOpts.OptimizationLevel == 0 && 
!F->hasAttr<AlwaysInlineAttr>()1.06k
)
3369
1.00k
    return false;
3370
3371
360
  if (F->hasAttr<DLLImportAttr>() && 
!F->hasAttr<AlwaysInlineAttr>()223
) {
3372
    // Check whether it would be safe to inline this dllimport function.
3373
205
    DLLImportFunctionVisitor Visitor;
3374
205
    Visitor.TraverseFunctionDecl(const_cast<FunctionDecl*>(F));
3375
205
    if (!Visitor.SafeToInline)
3376
18
      return false;
3377
3378
187
    if (const CXXDestructorDecl *Dtor = dyn_cast<CXXDestructorDecl>(F)) {
3379
      // Implicit destructor invocations aren't captured in the AST, so the
3380
      // check above can't see them. Check for them manually here.
3381
18
      for (const Decl *Member : Dtor->getParent()->decls())
3382
97
        if (isa<FieldDecl>(Member))
3383
3
          if (HasNonDllImportDtor(cast<FieldDecl>(Member)->getType()))
3384
2
            return false;
3385
16
      for (const CXXBaseSpecifier &B : Dtor->getParent()->bases())
3386
4
        if (HasNonDllImportDtor(B.getType()))
3387
2
          return false;
3388
16
    }
3389
187
  }
3390
3391
  // Inline builtins declaration must be emitted. They often are fortified
3392
  // functions.
3393
338
  if (F->isInlineBuiltinDeclaration())
3394
25
    return true;
3395
3396
  // PR9614. Avoid cases where the source code is lying to us. An available
3397
  // externally function should have an equivalent function somewhere else,
3398
  // but a function that calls itself through asm label/`__builtin_` trickery is
3399
  // clearly not equivalent to the real implementation.
3400
  // This happens in glibc's btowc and in some configure checks.
3401
313
  return !isTriviallyRecursive(F);
3402
338
}
3403
3404
537
bool CodeGenModule::shouldOpportunisticallyEmitVTables() {
3405
537
  return CodeGenOpts.OptimizationLevel > 0;
3406
537
}
3407
3408
void CodeGenModule::EmitMultiVersionFunctionDefinition(GlobalDecl GD,
3409
138
                                                       llvm::GlobalValue *GV) {
3410
138
  const auto *FD = cast<FunctionDecl>(GD.getDecl());
3411
3412
138
  if (FD->isCPUSpecificMultiVersion()) {
3413
44
    auto *Spec = FD->getAttr<CPUSpecificAttr>();
3414
94
    for (unsigned I = 0; I < Spec->cpus_size(); 
++I50
)
3415
50
      EmitGlobalFunctionDefinition(GD.getWithMultiVersionIndex(I), nullptr);
3416
94
  } else if (FD->isTargetClonesMultiVersion()) {
3417
10
    auto *Clone = FD->getAttr<TargetClonesAttr>();
3418
32
    for (unsigned I = 0; I < Clone->featuresStrs_size(); 
++I22
)
3419
22
      if (Clone->isFirstOfVersion(I))
3420
20
        EmitGlobalFunctionDefinition(GD.getWithMultiVersionIndex(I), nullptr);
3421
    // Ensure that the resolver function is also emitted.
3422
10
    GetOrCreateMultiVersionResolver(GD);
3423
10
  } else
3424
84
    EmitGlobalFunctionDefinition(GD, GV);
3425
138
}
3426
3427
290k
void CodeGenModule::EmitGlobalDefinition(GlobalDecl GD, llvm::GlobalValue *GV) {
3428
290k
  const auto *D = cast<ValueDecl>(GD.getDecl());
3429
3430
290k
  PrettyStackTraceDecl CrashInfo(const_cast<ValueDecl *>(D), D->getLocation(),
3431
290k
                                 Context.getSourceManager(),
3432
290k
                                 "Generating code for declaration");
3433
3434
290k
  if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
3435
    // At -O0, don't generate IR for functions with available_externally
3436
    // linkage.
3437
270k
    if (!shouldEmitFunction(GD))
3438
1.02k
      return;
3439
3440
269k
    llvm::TimeTraceScope TimeScope("CodeGen Function", [&]() {
3441
6
      std::string Name;
3442
6
      llvm::raw_string_ostream OS(Name);
3443
6
      FD->getNameForDiagnostic(OS, getContext().getPrintingPolicy(),
3444
6
                               /*Qualified=*/true);
3445
6
      return Name;
3446
6
    });
3447
3448
269k
    if (const auto *Method = dyn_cast<CXXMethodDecl>(D)) {
3449
      // Make sure to emit the definition(s) before we emit the thunks.
3450
      // This is necessary for the generation of certain thunks.
3451
110k
      if (isa<CXXConstructorDecl>(Method) || 
isa<CXXDestructorDecl>(Method)69.7k
)
3452
57.0k
        ABI->emitCXXStructor(GD);
3453
53.3k
      else if (FD->isMultiVersion())
3454
32
        EmitMultiVersionFunctionDefinition(GD, GV);
3455
53.2k
      else
3456
53.2k
        EmitGlobalFunctionDefinition(GD, GV);
3457
3458
110k
      if (Method->isVirtual())
3459
4.18k
        getVTables().EmitThunks(GD);
3460
3461
110k
      return;
3462
110k
    }
3463
3464
159k
    if (FD->isMultiVersion())
3465
106
      return EmitMultiVersionFunctionDefinition(GD, GV);
3466
159k
    return EmitGlobalFunctionDefinition(GD, GV);
3467
159k
  }
3468
3469
19.7k
  if (const auto *VD = dyn_cast<VarDecl>(D))
3470
19.7k
    return EmitGlobalVarDefinition(VD, !VD->hasDefinition());
3471
3472
0
  llvm_unreachable("Invalid argument to EmitGlobalDefinition()");
3473
0
}
3474
3475
static void ReplaceUsesOfNonProtoTypeWithRealFunction(llvm::GlobalValue *Old,
3476
                                                      llvm::Function *NewFn);
3477
3478
static unsigned
3479
TargetMVPriority(const TargetInfo &TI,
3480
552
                 const CodeGenFunction::MultiVersionResolverOption &RO) {
3481
552
  unsigned Priority = 0;
3482
552
  for (StringRef Feat : RO.Conditions.Features)
3483
162
    Priority = std::max(Priority, TI.multiVersionSortPriority(Feat));
3484
3485
552
  if (!RO.Conditions.Architecture.empty())
3486
293
    Priority = std::max(
3487
293
        Priority, TI.multiVersionSortPriority(RO.Conditions.Architecture));
3488
552
  return Priority;
3489
552
}
3490
3491
// Multiversion functions should be at most 'WeakODRLinkage' so that a different
3492
// TU can forward declare the function without causing problems.  Particularly
3493
// in the cases of CPUDispatch, this causes issues. This also makes sure we
3494
// work with internal linkage functions, so that the same function name can be
3495
// used with internal linkage in multiple TUs.
3496
llvm::GlobalValue::LinkageTypes getMultiversionLinkage(CodeGenModule &CGM,
3497
200
                                                       GlobalDecl GD) {
3498
200
  const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
3499
200
  if (FD->getFormalLinkage() == InternalLinkage)
3500
18
    return llvm::GlobalValue::InternalLinkage;
3501
182
  return llvm::GlobalValue::WeakODRLinkage;
3502
200
}
3503
3504
35.9k
void CodeGenModule::emitMultiVersionFunctions() {
3505
35.9k
  std::vector<GlobalDecl> MVFuncsToEmit;
3506
35.9k
  MultiVersionFuncs.swap(MVFuncsToEmit);
3507
35.9k
  for (GlobalDecl GD : MVFuncsToEmit) {
3508
80
    const auto *FD = cast<FunctionDecl>(GD.getDecl());
3509
80
    assert(FD && "Expected a FunctionDecl");
3510
3511
0
    SmallVector<CodeGenFunction::MultiVersionResolverOption, 10> Options;
3512
80
    if (FD->isTargetMultiVersion()) {
3513
58
      getContext().forEachMultiversionedFunctionVersion(
3514
196
          FD, [this, &GD, &Options](const FunctionDecl *CurFD) {
3515
196
            GlobalDecl CurGD{
3516
196
                (CurFD->isDefined() ? 
CurFD->getDefinition()163
:
CurFD33
)};
3517
196
            StringRef MangledName = getMangledName(CurGD);
3518
196
            llvm::Constant *Func = GetGlobalValue(MangledName);
3519
196
            if (!Func) {
3520
101
              if (CurFD->isDefined()) {
3521
68
                EmitGlobalFunctionDefinition(CurGD, nullptr);
3522
68
                Func = GetGlobalValue(MangledName);
3523
68
              } else {
3524
33
                const CGFunctionInfo &FI =
3525
33
                    getTypes().arrangeGlobalDeclaration(GD);
3526
33
                llvm::FunctionType *Ty = getTypes().GetFunctionType(FI);
3527
33
                Func = GetAddrOfFunction(CurGD, Ty, /*ForVTable=*/false,
3528
33
                                         /*DontDefer=*/false, ForDefinition);
3529
33
              }
3530
101
              assert(Func && "This should have just been created");
3531
101
            }
3532
3533
0
            const auto *TA = CurFD->getAttr<TargetAttr>();
3534
196
            llvm::SmallVector<StringRef, 8> Feats;
3535
196
            TA->getAddedFeatures(Feats);
3536
3537
196
            Options.emplace_back(cast<llvm::Function>(Func),
3538
196
                                 TA->getArchitecture(), Feats);
3539
196
          });
3540
58
    } else 
if (22
FD->isTargetClonesMultiVersion()22
) {
3541
22
      const auto *TC = FD->getAttr<TargetClonesAttr>();
3542
72
      for (unsigned VersionIndex = 0; VersionIndex < TC->featuresStrs_size();
3543
50
           ++VersionIndex) {
3544
50
        if (!TC->isFirstOfVersion(VersionIndex))
3545
2
          continue;
3546
48
        GlobalDecl CurGD{(FD->isDefined() ? 
FD->getDefinition()44
:
FD4
),
3547
48
                         VersionIndex};
3548
48
        StringRef Version = TC->getFeatureStr(VersionIndex);
3549
48
        StringRef MangledName = getMangledName(CurGD);
3550
48
        llvm::Constant *Func = GetGlobalValue(MangledName);
3551
48
        if (!Func) {
3552
28
          if (FD->isDefined()) {
3553
24
            EmitGlobalFunctionDefinition(CurGD, nullptr);
3554
24
            Func = GetGlobalValue(MangledName);
3555
24
          } else {
3556
4
            const CGFunctionInfo &FI =
3557
4
                getTypes().arrangeGlobalDeclaration(CurGD);
3558
4
            llvm::FunctionType *Ty = getTypes().GetFunctionType(FI);
3559
4
            Func = GetAddrOfFunction(CurGD, Ty, /*ForVTable=*/false,
3560
4
                                     /*DontDefer=*/false, ForDefinition);
3561
4
          }
3562
28
          assert(Func && "This should have just been created");
3563
28
        }
3564
3565
0
        StringRef Architecture;
3566
48
        llvm::SmallVector<StringRef, 1> Feature;
3567
3568
48
        if (Version.startswith("arch="))
3569
8
          Architecture = Version.drop_front(sizeof("arch=") - 1);
3570
40
        else if (Version != "default")
3571
18
          Feature.push_back(Version);
3572
3573
48
        Options.emplace_back(cast<llvm::Function>(Func), Architecture, Feature);
3574
48
      }
3575
22
    } else {
3576
0
      assert(0 && "Expected a target or target_clones multiversion function");
3577
0
      continue;
3578
0
    }
3579
3580
80
    llvm::Constant *ResolverConstant = GetOrCreateMultiVersionResolver(GD);
3581
80
    if (auto *IFunc = dyn_cast<llvm::GlobalIFunc>(ResolverConstant))
3582
40
      ResolverConstant = IFunc->getResolver();
3583
80
    llvm::Function *ResolverFunc = cast<llvm::Function>(ResolverConstant);
3584
3585
80
    ResolverFunc->setLinkage(getMultiversionLinkage(*this, GD));
3586
3587
80
    if (supportsCOMDAT())
3588
80
      ResolverFunc->setComdat(
3589
80
          getModule().getOrInsertComdat(ResolverFunc->getName()));
3590
3591
80
    const TargetInfo &TI = getTarget();
3592
80
    llvm::stable_sort(
3593
80
        Options, [&TI](const CodeGenFunction::MultiVersionResolverOption &LHS,
3594
276
                       const CodeGenFunction::MultiVersionResolverOption &RHS) {
3595
276
          return TargetMVPriority(TI, LHS) > TargetMVPriority(TI, RHS);
3596
276
        });
3597
80
    CodeGenFunction CGF(*this);
3598
80
    CGF.EmitMultiVersionResolver(ResolverFunc, Options);
3599
80
  }
3600
3601
  // Ensure that any additions to the deferred decls list caused by emitting a
3602
  // variant are emitted.  This can happen when the variant itself is inline and
3603
  // calls a function without linkage.
3604
35.9k
  if (!MVFuncsToEmit.empty())
3605
30
    EmitDeferred();
3606
3607
  // Ensure that any additions to the multiversion funcs list from either the
3608
  // deferred decls or the multiversion functions themselves are emitted.
3609
35.9k
  if (!MultiVersionFuncs.empty())
3610
2
    emitMultiVersionFunctions();
3611
35.9k
}
3612
3613
41
void CodeGenModule::emitCPUDispatchDefinition(GlobalDecl GD) {
3614
41
  const auto *FD = cast<FunctionDecl>(GD.getDecl());
3615
41
  assert(FD && "Not a FunctionDecl?");
3616
0
  assert(FD->isCPUDispatchMultiVersion() && "Not a multiversion function?");
3617
0
  const auto *DD = FD->getAttr<CPUDispatchAttr>();
3618
41
  assert(DD && "Not a cpu_dispatch Function?");
3619
3620
0
  const CGFunctionInfo &FI = getTypes().arrangeGlobalDeclaration(GD);
3621
41
  llvm::FunctionType *DeclTy = getTypes().GetFunctionType(FI);
3622
3623
41
  StringRef ResolverName = getMangledName(GD);
3624
41
  UpdateMultiVersionNames(GD, FD, ResolverName);
3625
3626
41
  llvm::Type *ResolverType;
3627
41
  GlobalDecl ResolverGD;
3628
41
  if (getTarget().supportsIFunc()) {
3629
21
    ResolverType = llvm::FunctionType::get(
3630
21
        llvm::PointerType::get(DeclTy,
3631
21
                               Context.getTargetAddressSpace(FD->getType())),
3632
21
        false);
3633
21
  }
3634
20
  else {
3635
20
    ResolverType = DeclTy;
3636
20
    ResolverGD = GD;
3637
20
  }
3638
3639
41
  auto *ResolverFunc = cast<llvm::Function>(GetOrCreateLLVMFunction(
3640
41
      ResolverName, ResolverType, ResolverGD, /*ForVTable=*/false));
3641
41
  ResolverFunc->setLinkage(getMultiversionLinkage(*this, GD));
3642
41
  if (supportsCOMDAT())
3643
41
    ResolverFunc->setComdat(
3644
41
        getModule().getOrInsertComdat(ResolverFunc->getName()));
3645
3646
41
  SmallVector<CodeGenFunction::MultiVersionResolverOption, 10> Options;
3647
41
  const TargetInfo &Target = getTarget();
3648
41
  unsigned Index = 0;
3649
98
  for (const IdentifierInfo *II : DD->cpus()) {
3650
    // Get the name of the target function so we can look it up/create it.
3651
98
    std::string MangledName = getMangledNameImpl(*this, GD, FD, true) +
3652
98
                              getCPUSpecificMangling(*this, II->getName());
3653
3654
98
    llvm::Constant *Func = GetGlobalValue(MangledName);
3655
3656
98
    if (!Func) {
3657
70
      GlobalDecl ExistingDecl = Manglings.lookup(MangledName);
3658
70
      if (ExistingDecl.getDecl() &&
3659
70
          
ExistingDecl.getDecl()->getAsFunction()->isDefined()4
) {
3660
4
        EmitGlobalFunctionDefinition(ExistingDecl, nullptr);
3661
4
        Func = GetGlobalValue(MangledName);
3662
66
      } else {
3663
66
        if (!ExistingDecl.getDecl())
3664
66
          ExistingDecl = GD.getWithMultiVersionIndex(Index);
3665
3666
66
      Func = GetOrCreateLLVMFunction(
3667
66
          MangledName, DeclTy, ExistingDecl,
3668
66
          /*ForVTable=*/false, /*DontDefer=*/true,
3669
66
          /*IsThunk=*/false, llvm::AttributeList(), ForDefinition);
3670
66
      }
3671
70
    }
3672
3673
98
    llvm::SmallVector<StringRef, 32> Features;
3674
98
    Target.getCPUSpecificCPUDispatchFeatures(II->getName(), Features);
3675
98
    llvm::transform(Features, Features.begin(),
3676
918
                    [](StringRef Str) { return Str.substr(1); });
3677
918
    llvm::erase_if(Features, [&Target](StringRef Feat) {
3678
918
      return !Target.validateCpuSupports(Feat);
3679
918
    });
3680
98
    Options.emplace_back(cast<llvm::Function>(Func), StringRef{}, Features);
3681
98
    ++Index;
3682
98
  }
3683
3684
41
  llvm::stable_sort(
3685
41
      Options, [](const CodeGenFunction::MultiVersionResolverOption &LHS,
3686
69
                  const CodeGenFunction::MultiVersionResolverOption &RHS) {
3687
69
        return llvm::X86::getCpuSupportsMask(LHS.Conditions.Features) >
3688
69
               llvm::X86::getCpuSupportsMask(RHS.Conditions.Features);
3689
69
      });
3690
3691
  // If the list contains multiple 'default' versions, such as when it contains
3692
  // 'pentium' and 'generic', don't emit the call to the generic one (since we
3693
  // always run on at least a 'pentium'). We do this by deleting the 'least
3694
  // advanced' (read, lowest mangling letter).
3695
43
  while (Options.size() > 1 &&
3696
43
         llvm::X86::getCpuSupportsMask(
3697
37
             (Options.end() - 2)->Conditions.Features) == 0) {
3698
2
    StringRef LHSName = (Options.end() - 2)->Function->getName();
3699
2
    StringRef RHSName = (Options.end() - 1)->Function->getName();
3700
2
    if (LHSName.compare(RHSName) < 0)
3701
2
      Options.erase(Options.end() - 2);
3702
0
    else
3703
0
      Options.erase(Options.end() - 1);
3704
2
  }
3705
3706
41
  CodeGenFunction CGF(*this);
3707
41
  CGF.EmitMultiVersionResolver(ResolverFunc, Options);
3708
3709
41
  if (getTarget().supportsIFunc()) {
3710
21
    llvm::GlobalValue::LinkageTypes Linkage = getMultiversionLinkage(*this, GD);
3711
21
    auto *IFunc = cast<llvm::GlobalValue>(GetOrCreateMultiVersionResolver(GD));
3712
3713
    // Fix up function declarations that were created for cpu_specific before
3714
    // cpu_dispatch was known
3715
21
    if (!isa<llvm::GlobalIFunc>(IFunc)) {
3716
3
      assert(cast<llvm::Function>(IFunc)->isDeclaration());
3717
0
      auto *GI = llvm::GlobalIFunc::create(DeclTy, 0, Linkage, "", ResolverFunc,
3718
3
                                           &getModule());
3719
3
      GI->takeName(IFunc);
3720
3
      IFunc->replaceAllUsesWith(GI);
3721
3
      IFunc->eraseFromParent();
3722
3
      IFunc = GI;
3723
3
    }
3724
3725
0
    std::string AliasName = getMangledNameImpl(
3726
21
        *this, GD, FD, /*OmitMultiVersionMangling=*/true);
3727
21
    llvm::Constant *AliasFunc = GetGlobalValue(AliasName);
3728
21
    if (!AliasFunc) {
3729
21
      auto *GA = llvm::GlobalAlias::create(DeclTy, 0, Linkage, AliasName, IFunc,
3730
21
                                           &getModule());
3731
21
      SetCommonAttributes(GD, GA);
3732
21
    }
3733
21
  }
3734
41
}
3735
3736
/// If a dispatcher for the specified mangled name is not in the module, create
3737
/// and return an llvm Function with the specified type.
3738
242
llvm::Constant *CodeGenModule::GetOrCreateMultiVersionResolver(GlobalDecl GD) {
3739
242
  const auto *FD = cast<FunctionDecl>(GD.getDecl());
3740
242
  assert(FD && "Not a FunctionDecl?");
3741
3742
0
  std::string MangledName =
3743
242
      getMangledNameImpl(*this, GD, FD, /*OmitMultiVersionMangling=*/true);
3744
3745
  // Holds the name of the resolver, in ifunc mode this is the ifunc (which has
3746
  // a separate resolver).
3747
242
  std::string ResolverName = MangledName;
3748
242
  if (getTarget().supportsIFunc())
3749
121
    ResolverName += ".ifunc";
3750
121
  else if (FD->isTargetMultiVersion())
3751
65
    ResolverName += ".resolver";
3752
3753
  // If the resolver has already been created, just return it.
3754
242
  if (llvm::GlobalValue *ResolverGV = GetGlobalValue(ResolverName))
3755
119
    return ResolverGV;
3756
3757
123
  const CGFunctionInfo &FI = getTypes().arrangeGlobalDeclaration(GD);
3758
123
  llvm::FunctionType *DeclTy = getTypes().GetFunctionType(FI);
3759
3760
  // The resolver needs to be created. For target and target_clones, defer
3761
  // creation until the end of the TU.
3762
123
  if (FD->isTargetMultiVersion() || 
FD->isTargetClonesMultiVersion()65
)
3763
80
    MultiVersionFuncs.push_back(GD);
3764
3765
  // For cpu_specific, don't create an ifunc yet because we don't know if the
3766
  // cpu_dispatch will be emitted in this translation unit.
3767
123
  if (getTarget().supportsIFunc() && 
!FD->isCPUSpecificMultiVersion()62
) {
3768
58
    llvm::Type *ResolverType = llvm::FunctionType::get(
3769
58
        llvm::PointerType::get(
3770
58
            DeclTy, getContext().getTargetAddressSpace(FD->getType())),
3771
58
        false);
3772
58
    llvm::Constant *Resolver = GetOrCreateLLVMFunction(
3773
58
        MangledName + ".resolver", ResolverType, GlobalDecl{},
3774
58
        /*ForVTable=*/false);
3775
58
    llvm::GlobalIFunc *GIF =
3776
58
        llvm::GlobalIFunc::create(DeclTy, 0, getMultiversionLinkage(*this, GD),
3777
58
                                  "", Resolver, &getModule());
3778
58
    GIF->setName(ResolverName);
3779
58
    SetCommonAttributes(FD, GIF);
3780
3781
58
    return GIF;
3782
58
  }
3783
3784
65
  llvm::Constant *Resolver = GetOrCreateLLVMFunction(
3785
65
      ResolverName, DeclTy, GlobalDecl{}, /*ForVTable=*/false);
3786
65
  assert(isa<llvm::GlobalValue>(Resolver) &&
3787
65
         "Resolver should be created for the first time");
3788
0
  SetCommonAttributes(FD, cast<llvm::GlobalValue>(Resolver));
3789
65
  return Resolver;
3790
123
}
3791
3792
/// GetOrCreateLLVMFunction - If the specified mangled name is not in the
3793
/// module, create and return an llvm Function with the specified type. If there
3794
/// is something in the module with the specified name, return it potentially
3795
/// bitcasted to the right type.
3796
///
3797
/// If D is non-null, it specifies a decl that correspond to this.  This is used
3798
/// to set the attributes on the function when it is first created.
3799
llvm::Constant *CodeGenModule::GetOrCreateLLVMFunction(
3800
    StringRef MangledName, llvm::Type *Ty, GlobalDecl GD, bool ForVTable,
3801
    bool DontDefer, bool IsThunk, llvm::AttributeList ExtraAttrs,
3802
742k
    ForDefinition_t IsForDefinition) {
3803
742k
  const Decl *D = GD.getDecl();
3804
3805
  // Any attempts to use a MultiVersion function should result in retrieving
3806
  // the iFunc instead. Name Mangling will handle the rest of the changes.
3807
742k
  if (const FunctionDecl *FD = cast_or_null<FunctionDecl>(D)) {
3808
    // For the device mark the function as one that should be emitted.
3809
691k
    if (getLangOpts().OpenMPIsDevice && 
OpenMPRuntime3.66k
&&
3810
691k
        
!OpenMPRuntime->markAsGlobalTarget(GD)3.66k
&&
FD->isDefined()1.21k
&&
3811
691k
        
!DontDefer991
&&
!IsForDefinition740
) {
3812
398
      if (const FunctionDecl *FDDef = FD->getDefinition()) {
3813
398
        GlobalDecl GDDef;
3814
398
        if (const auto *CD = dyn_cast<CXXConstructorDecl>(FDDef))
3815
41
          GDDef = GlobalDecl(CD, GD.getCtorType());
3816
357
        else if (const auto *DD = dyn_cast<CXXDestructorDecl>(FDDef))
3817
17
          GDDef = GlobalDecl(DD, GD.getDtorType());
3818
340
        else
3819
340
          GDDef = GlobalDecl(FDDef);
3820
398
        EmitGlobal(GDDef);
3821
398
      }
3822
398
    }
3823
3824
691k
    if (FD->isMultiVersion()) {
3825
484
      UpdateMultiVersionNames(GD, FD, MangledName);
3826
484
      if (!IsForDefinition)
3827
131
        return GetOrCreateMultiVersionResolver(GD);
3828
484
    }
3829
691k
  }
3830
3831
  // Lookup the entry, lazily creating it if necessary.
3832
742k
  llvm::GlobalValue *Entry = GetGlobalValue(MangledName);
3833
742k
  if (Entry) {
3834
422k
    if (WeakRefReferences.erase(Entry)) {
3835
7
      const FunctionDecl *FD = cast_or_null<FunctionDecl>(D);
3836
7
      if (FD && !FD->hasAttr<WeakAttr>())
3837
6
        Entry->setLinkage(llvm::Function::ExternalLinkage);
3838
7
    }
3839
3840
    // Handle dropped DLL attributes.
3841
422k
    if (D && 
!D->hasAttr<DLLImportAttr>()384k
&&
!D->hasAttr<DLLExportAttr>()383k
&&
3842
422k
        
!shouldMapVisibilityToDLLExport(cast_or_null<NamedDecl>(D))383k
) {
3843
383k
      Entry->setDLLStorageClass(llvm::GlobalValue::DefaultStorageClass);
3844
383k
      setDSOLocal(Entry);
3845
383k
    }
3846
3847
    // If there are two attempts to define the same mangled name, issue an
3848
    // error.
3849
422k
    if (IsForDefinition && 
!Entry->isDeclaration()195k
) {
3850
1.87k
      GlobalDecl OtherGD;
3851
      // Check that GD is not yet in DiagnosedConflictingDefinitions is required
3852
      // to make sure that we issue an error only once.
3853
1.87k
      if (lookupRepresentativeDecl(MangledName, OtherGD) &&
3854
1.87k
          (GD.getCanonicalDecl().getDecl() !=
3855
1.87k
           OtherGD.getCanonicalDecl().getDecl()) &&
3856
1.87k
          
DiagnosedConflictingDefinitions.insert(GD).second3
) {
3857
2
        getDiags().Report(D->getLocation(), diag::err_duplicate_mangled_name)
3858
2
            << MangledName;
3859
2
        getDiags().Report(OtherGD.getDecl()->getLocation(),
3860
2
                          diag::note_previous_definition);
3861
2
      }
3862
1.87k
    }
3863
3864
422k
    if ((isa<llvm::Function>(Entry) || 
isa<llvm::GlobalAlias>(Entry)65
) &&
3865
422k
        
(Entry->getValueType() == Ty)422k
) {
3866
422k
      return Entry;
3867
422k
    }
3868
3869
    // Make sure the result is of the correct type.
3870
    // (If function is requested for a definition, we always need to create a new
3871
    // function, not just return a bitcast.)
3872
403
    if (!IsForDefinition)
3873
350
      return llvm::ConstantExpr::getBitCast(Entry, Ty->getPointerTo());
3874
403
  }
3875
3876
  // This function doesn't have a complete type (for example, the return
3877
  // type is an incomplete struct). Use a fake type instead, and make
3878
  // sure not to try to set attributes.
3879
319k
  bool IsIncompleteFunction = false;
3880
3881
319k
  llvm::FunctionType *FTy;
3882
319k
  if (isa<llvm::FunctionType>(Ty)) {
3883
319k
    FTy = cast<llvm::FunctionType>(Ty);
3884
319k
  } else {
3885
37
    FTy = llvm::FunctionType::get(VoidTy, false);
3886
37
    IsIncompleteFunction = true;
3887
37
  }
3888
3889
319k
  llvm::Function *F =
3890
319k
      llvm::Function::Create(FTy, llvm::Function::ExternalLinkage,
3891
319k
                             Entry ? 
StringRef()53
:
MangledName319k
, &getModule());
3892
3893
  // If we already created a function with the same mangled name (but different
3894
  // type) before, take its name and add it to the list of functions to be
3895
  // replaced with F at the end of CodeGen.
3896
  //
3897
  // This happens if there is a prototype for a function (e.g. "int f()") and
3898
  // then a definition of a different type (e.g. "int f(int x)").
3899
319k
  if (Entry) {
3900
53
    F->takeName(Entry);
3901
3902
    // This might be an implementation of a function without a prototype, in
3903
    // which case, try to do special replacement of calls which match the new
3904
    // prototype.  The really key thing here is that we also potentially drop
3905
    // arguments from the call site so as to make a direct call, which makes the
3906
    // inliner happier and suppresses a number of optimizer warnings (!) about
3907
    // dropping arguments.
3908
53
    if (!Entry->use_empty()) {
3909
53
      ReplaceUsesOfNonProtoTypeWithRealFunction(Entry, F);
3910
53
      Entry->removeDeadConstantUsers();
3911
53
    }
3912
3913
53
    llvm::Constant *BC = llvm::ConstantExpr::getBitCast(
3914
53
        F, Entry->getValueType()->getPointerTo());
3915
53
    addGlobalValReplacement(Entry, BC);
3916
53
  }
3917
3918
319k
  assert(F->getName() == MangledName && "name was uniqued!");
3919
319k
  if (D)
3920
306k
    SetFunctionAttributes(GD, F, IsIncompleteFunction, IsThunk);
3921
319k
  if (ExtraAttrs.hasFnAttrs()) {
3922
2.14k
    llvm::AttrBuilder B(F->getContext(), ExtraAttrs.getFnAttrs());
3923
2.14k
    F->addFnAttrs(B);
3924
2.14k
  }
3925
3926
319k
  if (!DontDefer) {
3927
    // All MSVC dtors other than the base dtor are linkonce_odr and delegate to
3928
    // each other bottoming out with the base dtor.  Therefore we emit non-base
3929
    // dtors on usage, even if there is no dtor definition in the TU.
3930
191k
    if (D && 
isa<CXXDestructorDecl>(D)178k
&&
3931
191k
        getCXXABI().useThunkForDtorVariant(cast<CXXDestructorDecl>(D),
3932
18.4k
                                           GD.getDtorType()))
3933
310
      addDeferredDeclToEmit(GD);
3934
3935
    // This is the first use or definition of a mangled name.  If there is a
3936
    // deferred decl with this name, remember that we need to emit it at the end
3937
    // of the file.
3938
191k
    auto DDI = DeferredDecls.find(MangledName);
3939
191k
    if (DDI != DeferredDecls.end()) {
3940
      // Move the potentially referenced deferred decl to the
3941
      // DeferredDeclsToEmit list, and remove it from DeferredDecls (since we
3942
      // don't need it anymore).
3943
118k
      addDeferredDeclToEmit(DDI->second);
3944
118k
      DeferredDecls.erase(DDI);
3945
3946
      // Otherwise, there are cases we have to worry about where we're
3947
      // using a declaration for which we must emit a definition but where
3948
      // we might not find a top-level definition:
3949
      //   - member functions defined inline in their classes
3950
      //   - friend functions defined inline in some class
3951
      //   - special member functions with implicit definitions
3952
      // If we ever change our AST traversal to walk into class methods,
3953
      // this will be unnecessary.
3954
      //
3955
      // We also don't emit a definition for a function if it's going to be an
3956
      // entry in a vtable, unless it's already marked as used.
3957
118k
    } else 
if (73.8k
getLangOpts().CPlusPlus73.8k
&&
D64.1k
) {
3958
      // Look for a declaration that's lexically in a record.
3959
101k
      for (const auto *FD = cast<FunctionDecl>(D)->getMostRecentDecl(); FD;
3960
57.9k
           
FD = FD->getPreviousDecl()48.0k
) {
3961
57.9k
        if (isa<CXXRecordDecl>(FD->getLexicalDeclContext())) {
3962
26.9k
          if (FD->doesThisDeclarationHaveABody()) {
3963
9.90k
            addDeferredDeclToEmit(GD.getWithDecl(FD));
3964
9.90k
            break;
3965
9.90k
          }
3966
26.9k
        }
3967
57.9k
      }
3968
53.5k
    }
3969
191k
  }
3970
3971
  // Make sure the result is of the requested type.
3972
319k
  if (!IsIncompleteFunction) {
3973
319k
    assert(F->getFunctionType() == Ty);
3974
0
    return F;
3975
319k
  }
3976
3977
37
  llvm::Type *PTy = llvm::PointerType::getUnqual(Ty);
3978
37
  return llvm::ConstantExpr::getBitCast(F, PTy);
3979
319k
}
3980
3981
/// GetAddrOfFunction - Return the address of the given function.  If Ty is
3982
/// non-null, then this function will use the specified type if it has to
3983
/// create it (this occurs when we see a definition of the function).
3984
llvm::Constant *CodeGenModule::GetAddrOfFunction(GlobalDecl GD,
3985
                                                 llvm::Type *Ty,
3986
                                                 bool ForVTable,
3987
                                                 bool DontDefer,
3988
485k
                                              ForDefinition_t IsForDefinition) {
3989
485k
  assert(!cast<FunctionDecl>(GD.getDecl())->isConsteval() &&
3990
485k
         "consteval function should never be emitted");
3991
  // If there was no specific requested type, just convert it now.
3992
485k
  if (!Ty) {
3993
177k
    const auto *FD = cast<FunctionDecl>(GD.getDecl());
3994
177k
    Ty = getTypes().ConvertType(FD->getType());
3995
177k
  }
3996
3997
  // Devirtualized destructor calls may come through here instead of via
3998
  // getAddrOfCXXStructor. Make sure we use the MS ABI base destructor instead
3999
  // of the complete destructor when necessary.
4000
485k
  if (const auto *DD = dyn_cast<CXXDestructorDecl>(GD.getDecl())) {
4001
1.86k
    if (getTarget().getCXXABI().isMicrosoft() &&
4002
1.86k
        
GD.getDtorType() == Dtor_Complete261
&&
4003
1.86k
        
DD->getParent()->getNumVBases() == 01
)
4004
1
      GD = GlobalDecl(DD, Dtor_Base);
4005
1.86k
  }
4006
4007
485k
  StringRef MangledName = getMangledName(GD);
4008
485k
  auto *F = GetOrCreateLLVMFunction(MangledName, Ty, GD, ForVTable, DontDefer,
4009
485k
                                    /*IsThunk=*/false, llvm::AttributeList(),
4010
485k
                                    IsForDefinition);
4011
  // Returns kernel handle for HIP kernel stub function.
4012
485k
  if (LangOpts.CUDA && 
!LangOpts.CUDAIsDevice1.41k
&&
4013
485k
      
cast<FunctionDecl>(GD.getDecl())->hasAttr<CUDAGlobalAttr>()603
) {
4014
186
    auto *Handle = getCUDARuntime().getKernelHandle(
4015
186
        cast<llvm::Function>(F->stripPointerCasts()), GD);
4016
186
    if (IsForDefinition)
4017
87
      return F;
4018
99
    return llvm::ConstantExpr::getBitCast(Handle, Ty->getPointerTo());
4019
186
  }
4020
485k
  return F;
4021
485k
}
4022
4023
10
llvm::Constant *CodeGenModule::GetFunctionStart(const ValueDecl *Decl) {
4024
10
  llvm::GlobalValue *F =
4025
10
      cast<llvm::GlobalValue>(GetAddrOfFunction(Decl)->stripPointerCasts());
4026
4027
10
  return llvm::ConstantExpr::getBitCast(llvm::NoCFIValue::get(F),
4028
10
                                        llvm::Type::getInt8PtrTy(VMContext));
4029
10
}
4030
4031
static const FunctionDecl *
4032
102
GetRuntimeFunctionDecl(ASTContext &C, StringRef Name) {
4033
102
  TranslationUnitDecl *TUDecl = C.getTranslationUnitDecl();
4034
102
  DeclContext *DC = TranslationUnitDecl::castToDeclContext(TUDecl);
4035
4036
102
  IdentifierInfo &CII = C.Idents.get(Name);
4037
102
  for (const auto *Result : DC->lookup(&CII))
4038
0
    if (const auto *FD = dyn_cast<FunctionDecl>(Result))
4039
0
      return FD;
4040
4041
102
  if (!C.getLangOpts().CPlusPlus)
4042
17
    return nullptr;
4043
4044
  // Demangle the premangled name from getTerminateFn()
4045
85
  IdentifierInfo &CXXII =
4046
85
      (Name == "_ZSt9terminatev" || 
Name == "?terminate@@YAXXZ"80
)
4047
85
          ? 
C.Idents.get("terminate")5
4048
85
          : 
C.Idents.get(Name)80
;
4049
4050
160
  for (const auto &N : {"__cxxabiv1", "std"}) {
4051
160
    IdentifierInfo &NS = C.Idents.get(N);
4052
160
    for (const auto *Result : DC->lookup(&NS)) {
4053
113
      const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(Result);
4054
113
      if (auto *LSD = dyn_cast<LinkageSpecDecl>(Result))
4055
0
        for (const auto *Result : LSD->lookup(&NS))
4056
0
          if ((ND = dyn_cast<NamespaceDecl>(Result)))
4057
0
            break;
4058
4059
113
      if (ND)
4060
113
        for (const auto *Result : ND->lookup(&CXXII))
4061
14
          if (const auto *FD = dyn_cast<FunctionDecl>(Result))
4062
14
            return FD;
4063
113
    }
4064
160
  }
4065
4066
71
  return nullptr;
4067
85
}
4068
4069
/// CreateRuntimeFunction - Create a new runtime function with the specified
4070
/// type and name.
4071
llvm::FunctionCallee
4072
CodeGenModule::CreateRuntimeFunction(llvm::FunctionType *FTy, StringRef Name,
4073
                                     llvm::AttributeList ExtraAttrs, bool Local,
4074
50.7k
                                     bool AssumeConvergent) {
4075
50.7k
  if (AssumeConvergent) {
4076
0
    ExtraAttrs =
4077
0
        ExtraAttrs.addFnAttribute(VMContext, llvm::Attribute::Convergent);
4078
0
  }
4079
4080
50.7k
  llvm::Constant *C =
4081
50.7k
      GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(), /*ForVTable=*/false,
4082
50.7k
                              /*DontDefer=*/false, /*IsThunk=*/false,
4083
50.7k
                              ExtraAttrs);
4084
4085
50.7k
  if (auto *F = dyn_cast<llvm::Function>(C)) {
4086
50.7k
    if (F->empty()) {
4087
46.4k
      F->setCallingConv(getRuntimeCC());
4088
4089
      // In Windows Itanium environments, try to mark runtime functions
4090
      // dllimport. For Mingw and MSVC, don't. We don't really know if the user
4091
      // will link their standard library statically or dynamically. Marking
4092
      // functions imported when they are not imported can cause linker errors
4093
      // and warnings.
4094
46.4k
      if (!Local && 
getTriple().isWindowsItaniumEnvironment()35.0k
&&
4095
46.4k
          
!getCodeGenOpts().LTOVisibilityPublicStd170
) {
4096
102
        const FunctionDecl *FD = GetRuntimeFunctionDecl(Context, Name);
4097
102
        if (!FD || 
FD->hasAttr<DLLImportAttr>()14
) {
4098
93
          F->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass);
4099
93
          F->setLinkage(llvm::GlobalValue::ExternalLinkage);
4100
93
        }
4101
102
      }
4102
46.4k
      setDSOLocal(F);
4103
46.4k
    }
4104
50.7k
  }
4105
4106
50.7k
  return {FTy, C};
4107
50.7k
}
4108
4109
/// isTypeConstant - Determine whether an object of this type can be emitted
4110
/// as a constant.
4111
///
4112
/// If ExcludeCtor is true, the duration when the object's constructor runs
4113
/// will not be considered. The caller will need to verify that the object is
4114
/// not written to during its construction.
4115
70.9k
bool CodeGenModule::isTypeConstant(QualType Ty, bool ExcludeCtor) {
4116
70.9k
  if (!Ty.isConstant(Context) && 
!Ty->isReferenceType()68.0k
)
4117
58.6k
    return false;
4118
4119
12.3k
  if (Context.getLangOpts().CPlusPlus) {
4120
11.6k
    if (const CXXRecordDecl *Record
4121
11.6k
          = Context.getBaseElementType(Ty)->getAsCXXRecordDecl())
4122
469
      return ExcludeCtor && 
!Record->hasMutableFields()258
&&
4123
469
             
Record->hasTrivialDestructor()247
;
4124
11.6k
  }
4125
4126
11.8k
  return true;
4127
12.3k
}
4128
4129
/// GetOrCreateLLVMGlobal - If the specified mangled name is not in the module,
4130
/// create and return an llvm GlobalVariable with the specified type and address
4131
/// space. If there is something in the module with the specified name, return
4132
/// it potentially bitcasted to the right type.
4133
///
4134
/// If D is non-null, it specifies a decl that correspond to this.  This is used
4135
/// to set the attributes on the global when it is first created.
4136
///
4137
/// If IsForDefinition is true, it is guaranteed that an actual global with
4138
/// type Ty will be returned, not conversion of a variable with the same
4139
/// mangled name but some other type.
4140
llvm::Constant *
4141
CodeGenModule::GetOrCreateLLVMGlobal(StringRef MangledName, llvm::Type *Ty,
4142
                                     LangAS AddrSpace, const VarDecl *D,
4143
97.3k
                                     ForDefinition_t IsForDefinition) {
4144
  // Lookup the entry, lazily creating it if necessary.
4145
97.3k
  llvm::GlobalValue *Entry = GetGlobalValue(MangledName);
4146
97.3k
  unsigned TargetAS = getContext().getTargetAddressSpace(AddrSpace);
4147
97.3k
  if (Entry) {
4148
61.0k
    if (WeakRefReferences.erase(Entry)) {
4149
4
      if (D && !D->hasAttr<WeakAttr>())
4150
3
        Entry->setLinkage(llvm::Function::ExternalLinkage);
4151
4
    }
4152
4153
    // Handle dropped DLL attributes.
4154
61.0k
    if (D && 
!D->hasAttr<DLLImportAttr>()57.0k
&&
!D->hasAttr<DLLExportAttr>()56.9k
&&
4155
61.0k
        
!shouldMapVisibilityToDLLExport(D)56.6k
)
4156
56.6k
      Entry->setDLLStorageClass(llvm::GlobalValue::DefaultStorageClass);
4157
4158
61.0k
    if (LangOpts.OpenMP && 
!LangOpts.OpenMPSimd33.2k
&&
D16.9k
)
4159
15.6k
      getOpenMPRuntime().registerTargetGlobalVariable(D, Entry);
4160
4161
61.0k
    if (Entry->getValueType() == Ty && 
Entry->getAddressSpace() == TargetAS60.9k
)
4162
60.4k
      return Entry;
4163
4164
    // If there are two attempts to define the same mangled name, issue an
4165
    // error.
4166
527
    if (IsForDefinition && 
!Entry->isDeclaration()378
) {
4167
162
      GlobalDecl OtherGD;
4168
162
      const VarDecl *OtherD;
4169
4170
      // Check that D is not yet in DiagnosedConflictingDefinitions is required
4171
      // to make sure that we issue an error only once.
4172
162
      if (D && lookupRepresentativeDecl(MangledName, OtherGD) &&
4173
162
          (D->getCanonicalDecl() != OtherGD.getCanonicalDecl().getDecl()) &&
4174
162
          
(OtherD = dyn_cast<VarDecl>(OtherGD.getDecl()))2
&&
4175
162
          
OtherD->hasInit()2
&&
4176
162
          
DiagnosedConflictingDefinitions.insert(D).second1
) {
4177
1
        getDiags().Report(D->getLocation(), diag::err_duplicate_mangled_name)
4178
1
            << MangledName;
4179
1
        getDiags().Report(OtherGD.getDecl()->getLocation(),
4180
1
                          diag::note_previous_definition);
4181
1
      }
4182
162
    }
4183
4184
    // Make sure the result is of the correct type.
4185
527
    if (Entry->getType()->getAddressSpace() != TargetAS) {
4186
441
      return llvm::ConstantExpr::getAddrSpaceCast(Entry,
4187
441
                                                  Ty->getPointerTo(TargetAS));
4188
441
    }
4189
4190
    // (If global is requested for a definition, we always need to create a new
4191
    // global, not just return a bitcast.)
4192
86
    if (!IsForDefinition)
4193
40
      return llvm::ConstantExpr::getBitCast(Entry, Ty->getPointerTo(TargetAS));
4194
86
  }
4195
4196
36.3k
  auto DAddrSpace = GetGlobalVarAddressSpace(D);
4197
4198
36.3k
  auto *GV = new llvm::GlobalVariable(
4199
36.3k
      getModule(), Ty, false, llvm::GlobalValue::ExternalLinkage, nullptr,
4200
36.3k
      MangledName, nullptr, llvm::GlobalVariable::NotThreadLocal,
4201
36.3k
      getContext().getTargetAddressSpace(DAddrSpace));
4202
4203
  // If we already created a global with the same mangled name (but different
4204
  // type) before, take its name and remove it from its parent.
4205
36.3k
  if (Entry) {
4206
46
    GV->takeName(Entry);
4207
4208
46
    if (!Entry->use_empty()) {
4209
44
      llvm::Constant *NewPtrForOldDecl =
4210
44
          llvm::ConstantExpr::getBitCast(GV, Entry->getType());
4211
44
      Entry->replaceAllUsesWith(NewPtrForOldDecl);
4212
44
    }
4213
4214
46
    Entry->eraseFromParent();
4215
46
  }
4216
4217
  // This is the first use or definition of a mangled name.  If there is a
4218
  // deferred decl with this name, remember that we need to emit it at the end
4219
  // of the file.
4220
36.3k
  auto DDI = DeferredDecls.find(MangledName);
4221
36.3k
  if (DDI != DeferredDecls.end()) {
4222
    // Move the potentially referenced deferred decl to the DeferredDeclsToEmit
4223
    // list, and remove it from DeferredDecls (since we don't need it anymore).
4224
1.26k
    addDeferredDeclToEmit(DDI->second);
4225
1.26k
    DeferredDecls.erase(DDI);
4226
1.26k
  }
4227
4228
  // Handle things which are present even on external declarations.
4229
36.3k
  if (D) {
4230
34.3k
    if (LangOpts.OpenMP && 
!LangOpts.OpenMPSimd8.37k
)
4231
4.03k
      getOpenMPRuntime().registerTargetGlobalVariable(D, GV);
4232
4233
    // FIXME: This code is overly simple and should be merged with other global
4234
    // handling.
4235
34.3k
    GV->setConstant(isTypeConstant(D->getType(), false));
4236
4237
34.3k
    GV->setAlignment(getContext().getDeclAlign(D).getAsAlign());
4238
4239
34.3k
    setLinkageForGV(GV, D);
4240
4241
34.3k
    if (D->getTLSKind()) {
4242
432
      if (D->getTLSKind() == VarDecl::TLS_Dynamic)
4243
347
        CXXThreadLocals.push_back(D);
4244
432
      setTLSMode(GV, *D);
4245
432
    }
4246
4247
34.3k
    setGVProperties(GV, D);
4248
4249
    // If required by the ABI, treat declarations of static data members with
4250
    // inline initializers as definitions.
4251
34.3k
    if (getContext().isMSStaticDataMemberInlineDefinition(D)) {
4252
98
      EmitGlobalVarDefinition(D);
4253
98
    }
4254
4255
    // Emit section information for extern variables.
4256
34.3k
    if (D->hasExternalStorage()) {
4257
2.12k
      if (const SectionAttr *SA = D->getAttr<SectionAttr>())
4258
3
        GV->setSection(SA->getName());
4259
2.12k
    }
4260
4261
    // Handle XCore specific ABI requirements.
4262
34.3k
    if (getTriple().getArch() == llvm::Triple::xcore &&
4263
34.3k
        
D->getLanguageLinkage() == CLanguageLinkage0
&&
4264
34.3k
        
D->getType().isConstant(Context)0
&&
4265
34.3k
        
isExternallyVisible(D->getLinkageAndVisibility().getLinkage())0
)
4266
0
      GV->setSection(".cp.rodata");
4267
4268
    // Check if we a have a const declaration with an initializer, we may be
4269
    // able to emit it as available_externally to expose it's value to the
4270
    // optimizer.
4271
34.3k
    if (Context.getLangOpts().CPlusPlus && 
GV->hasExternalLinkage()27.1k
&&
4272
34.3k
        
D->getType().isConstQualified()27.0k
&&
!GV->hasInitializer()764
&&
4273
34.3k
        
!D->hasDefinition()764
&&
D->hasInit()173
&&
!D->hasAttr<DLLImportAttr>()67
) {
4274
40
      const auto *Record =
4275
40
          Context.getBaseElementType(D->getType())->getAsCXXRecordDecl();
4276
40
      bool HasMutableFields = Record && 
Record->hasMutableFields()8
;
4277
40
      if (!HasMutableFields) {
4278
38
        const VarDecl *InitDecl;
4279
38
        const Expr *InitExpr = D->getAnyInitializer(InitDecl);
4280
38
        if (InitExpr) {
4281
38
          ConstantEmitter emitter(*this);
4282
38
          llvm::Constant *Init = emitter.tryEmitForInitializer(*InitDecl);
4283
38
          if (Init) {
4284
38
            auto *InitType = Init->getType();
4285
38
            if (GV->getValueType() != InitType) {
4286
              // The type of the initializer does not match the definition.
4287
              // This happens when an initializer has a different type from
4288
              // the type of the global (because of padding at the end of a
4289
              // structure for instance).
4290
2
              GV->setName(StringRef());
4291
              // Make a new global with the correct type, this is now guaranteed
4292
              // to work.
4293
2
              auto *NewGV = cast<llvm::GlobalVariable>(
4294
2
                  GetAddrOfGlobalVar(D, InitType, IsForDefinition)
4295
2
                      ->stripPointerCasts());
4296
4297
              // Erase the old global, since it is no longer used.
4298
2
              GV->eraseFromParent();
4299
2
              GV = NewGV;
4300
36
            } else {
4301
36
              GV->setInitializer(Init);
4302
36
              GV->setConstant(true);
4303
36
              GV->setLinkage(llvm::GlobalValue::AvailableExternallyLinkage);
4304
36
            }
4305
38
            emitter.finalize(GV);
4306
38
          }
4307
38
        }
4308
38
      }
4309
40
    }
4310
34.3k
  }
4311
4312
36.3k
  if (GV->isDeclaration()) {
4313
36.2k
    getTargetCodeGenInfo().setTargetAttributes(D, GV, *this);
4314
    // External HIP managed variables needed to be recorded for transformation
4315
    // in both device and host compilations.
4316
36.2k
    if (getLangOpts().CUDA && 
D453
&&
D->hasAttr<HIPManagedAttr>()449
&&
4317
36.2k
        
D->hasExternalStorage()34
)
4318
8
      getCUDARuntime().handleVarRegistration(D, *GV);
4319
36.2k
  }
4320
4321
36.3k
  if (D)
4322
34.3k
    SanitizerMD->reportGlobal(GV, *D);
4323
4324
36.3k
  LangAS ExpectedAS =
4325
36.3k
      D ? 
D->getType().getAddressSpace()34.3k
4326
36.3k
        : 
(1.97k
LangOpts.OpenCL1.97k
?
LangAS::opencl_global3
:
LangAS::Default1.97k
);
4327
36.3k
  assert(getContext().getTargetAddressSpace(ExpectedAS) == TargetAS);
4328
36.3k
  if (DAddrSpace != ExpectedAS) {
4329
267
    return getTargetCodeGenInfo().performAddrSpaceCast(
4330
267
        *this, GV, DAddrSpace, ExpectedAS, Ty->getPointerTo(TargetAS));
4331
267
  }
4332
4333
36.0k
  return GV;
4334
36.3k
}
4335
4336
llvm::Constant *
4337
162k
CodeGenModule::GetAddrOfGlobal(GlobalDecl GD, ForDefinition_t IsForDefinition) {
4338
162k
  const Decl *D = GD.getDecl();
4339
4340
162k
  if (isa<CXXConstructorDecl>(D) || 
isa<CXXDestructorDecl>(D)122k
)
4341
56.1k
    return getAddrOfCXXStructor(GD, /*FnInfo=*/nullptr, /*FnType=*/nullptr,
4342
56.1k
                                /*DontDefer=*/false, IsForDefinition);
4343
4344
106k
  if (isa<CXXMethodDecl>(D)) {
4345
52.5k
    auto FInfo =
4346
52.5k
        &getTypes().arrangeCXXMethodDeclaration(cast<CXXMethodDecl>(D));
4347
52.5k
    auto Ty = getTypes().GetFunctionType(*FInfo);
4348
52.5k
    return GetAddrOfFunction(GD, Ty, /*ForVTable=*/false, /*DontDefer=*/false,
4349
52.5k
                             IsForDefinition);
4350
52.5k
  }
4351
4352
53.6k
  if (isa<FunctionDecl>(D)) {
4353
48.9k
    const CGFunctionInfo &FI = getTypes().arrangeGlobalDeclaration(GD);
4354
48.9k
    llvm::FunctionType *Ty = getTypes().GetFunctionType(FI);
4355
48.9k
    return GetAddrOfFunction(GD, Ty, /*ForVTable=*/false, /*DontDefer=*/false,
4356
48.9k
                             IsForDefinition);
4357
48.9k
  }
4358
4359
4.71k
  return GetAddrOfGlobalVar(cast<VarDecl>(D), /*Ty=*/nullptr, IsForDefinition);
4360
53.6k
}
4361
4362
llvm::GlobalVariable *CodeGenModule::CreateOrReplaceCXXRuntimeVariable(
4363
    StringRef Name, llvm::Type *Ty, llvm::GlobalValue::LinkageTypes Linkage,
4364
6.71k
    unsigned Alignment) {
4365
6.71k
  llvm::GlobalVariable *GV = getModule().getNamedGlobal(Name);
4366
6.71k
  llvm::GlobalVariable *OldGV = nullptr;
4367
4368
6.71k
  if (GV) {
4369
    // Check if the variable has the right type.
4370
434
    if (GV->getValueType() == Ty)
4371
434
      return GV;
4372
4373
    // Because C++ name mangling, the only way we can end up with an already
4374
    // existing global with the same name is if it has been declared extern "C".
4375
0
    assert(GV->isDeclaration() && "Declaration has wrong type!");
4376
0
    OldGV = GV;
4377
0
  }
4378
4379
  // Create a new variable.
4380
6.27k
  GV = new llvm::GlobalVariable(getModule(), Ty, /*isConstant=*/true,
4381
6.27k
                                Linkage, nullptr, Name);
4382
4383
6.27k
  if (OldGV) {
4384
    // Replace occurrences of the old variable if needed.
4385
0
    GV->takeName(OldGV);
4386
4387
0
    if (!OldGV->use_empty()) {
4388
0
      llvm::Constant *NewPtrForOldDecl =
4389
0
      llvm::ConstantExpr::getBitCast(GV, OldGV->getType());
4390
0
      OldGV->replaceAllUsesWith(NewPtrForOldDecl);
4391
0
    }
4392
4393
0
    OldGV->eraseFromParent();
4394
0
  }
4395
4396
6.27k
  if (supportsCOMDAT() && 
GV->isWeakForLinker()2.54k
&&
4397
6.27k
      
!GV->hasAvailableExternallyLinkage()1.26k
)
4398
1.26k
    GV->setComdat(TheModule.getOrInsertComdat(GV->getName()));
4399
4400
6.27k
  GV->setAlignment(llvm::MaybeAlign(Alignment));
4401
4402
6.27k
  return GV;
4403
6.71k
}
4404
4405
/// GetAddrOfGlobalVar - Return the llvm::Constant for the address of the
4406
/// given global variable.  If Ty is non-null and if the global doesn't exist,
4407
/// then it will be created with the specified type instead of whatever the
4408
/// normal requested type would be. If IsForDefinition is true, it is guaranteed
4409
/// that an actual global with type Ty will be returned, not conversion of a
4410
/// variable with the same mangled name but some other type.
4411
llvm::Constant *CodeGenModule::GetAddrOfGlobalVar(const VarDecl *D,
4412
                                                  llvm::Type *Ty,
4413
91.3k
                                           ForDefinition_t IsForDefinition) {
4414
91.3k
  assert(D->hasGlobalStorage() && "Not a global variable");
4415
0
  QualType ASTTy = D->getType();
4416
91.3k
  if (!Ty)
4417
67.2k
    Ty = getTypes().ConvertTypeForMem(ASTTy);
4418
4419
91.3k
  StringRef MangledName = getMangledName(D);
4420
91.3k
  return GetOrCreateLLVMGlobal(MangledName, Ty, ASTTy.getAddressSpace(), D,
4421
91.3k
                               IsForDefinition);
4422
91.3k
}
4423
4424
/// CreateRuntimeVariable - Create a new runtime global variable with the
4425
/// specified type and name.
4426
llvm::Constant *
4427
CodeGenModule::CreateRuntimeVariable(llvm::Type *Ty,
4428
5.43k
                                     StringRef Name) {
4429
5.43k
  LangAS AddrSpace = getContext().getLangOpts().OpenCL ? 
LangAS::opencl_global7
4430
5.43k
                                                       : 
LangAS::Default5.42k
;
4431
5.43k
  auto *Ret = GetOrCreateLLVMGlobal(Name, Ty, AddrSpace, nullptr);
4432
5.43k
  setDSOLocal(cast<llvm::GlobalValue>(Ret->stripPointerCasts()));
4433
5.43k
  return Ret;
4434
5.43k
}
4435
4436
4.22k
void CodeGenModule::EmitTentativeDefinition(const VarDecl *D) {
4437
4.22k
  assert(!D->getInit() && "Cannot emit definite definitions here!");
4438
4439
0
  StringRef MangledName = getMangledName(D);
4440
4.22k
  llvm::GlobalValue *GV = GetGlobalValue(MangledName);
4441
4442
  // We already have a definition, not declaration, with the same mangled name.
4443
  // Emitting of declaration is not required (and actually overwrites emitted
4444
  // definition).
4445
4.22k
  if (GV && 
!GV->isDeclaration()2.41k
)
4446
1
    return;
4447
4448
  // If we have not seen a reference to this variable yet, place it into the
4449
  // deferred declarations table to be emitted if needed later.
4450
4.22k
  if (!MustBeEmitted(D) && 
!GV120
) {
4451
72
      DeferredDecls[MangledName] = D;
4452
72
      return;
4453
72
  }
4454
4455
  // The tentative definition is the only definition.
4456
4.14k
  EmitGlobalVarDefinition(D);
4457
4.14k
}
4458
4459
7
void CodeGenModule::EmitExternalDeclaration(const VarDecl *D) {
4460
7
  EmitExternalVarDeclaration(D);
4461
7
}
4462
4463
353
CharUnits CodeGenModule::GetTargetTypeStoreSize(llvm::Type *Ty) const {
4464
353
  return Context.toCharUnitsFromBits(
4465
353
      getDataLayout().getTypeStoreSizeInBits(Ty));
4466
353
}
4467
4468
73.0k
LangAS CodeGenModule::GetGlobalVarAddressSpace(const VarDecl *D) {
4469
73.0k
  if (LangOpts.OpenCL) {
4470
865
    LangAS AS = D ? 
D->getType().getAddressSpace()862
:
LangAS::opencl_global3
;
4471
865
    assert(AS == LangAS::opencl_global ||
4472
865
           AS == LangAS::opencl_global_device ||
4473
865
           AS == LangAS::opencl_global_host ||
4474
865
           AS == LangAS::opencl_constant ||
4475
865
           AS == LangAS::opencl_local ||
4476
865
           AS >= LangAS::FirstTargetAddressSpace);
4477
0
    return AS;
4478
865
  }
4479
4480
72.2k
  if (LangOpts.SYCLIsDevice &&
4481
72.2k
      
(1
!D1
||
D->getType().getAddressSpace() == LangAS::Default1
))
4482
1
    return LangAS::sycl_global;
4483
4484
72.2k
  if (LangOpts.CUDA && 
LangOpts.CUDAIsDevice870
) {
4485
431
    if (D && D->hasAttr<CUDAConstantAttr>())
4486
177
      return LangAS::cuda_constant;
4487
254
    else if (D && D->hasAttr<CUDASharedAttr>())
4488
37
      return LangAS::cuda_shared;
4489
217
    else if (D && D->hasAttr<CUDADeviceAttr>())
4490
201
      return LangAS::cuda_device;
4491
16
    else if (D && D->getType().isConstQualified())
4492
14
      return LangAS::cuda_constant;
4493
2
    else
4494
2
      return LangAS::cuda_device;
4495
431
  }
4496
4497
71.7k
  if (LangOpts.OpenMP) {
4498
18.9k
    LangAS AS;
4499
18.9k
    if (OpenMPRuntime->hasAllocateAttributeForGlobalVar(D, AS))
4500
215
      return AS;
4501
18.9k
  }
4502
71.5k
  return getTargetCodeGenInfo().getGlobalVarAddressSpace(*this, D);
4503
71.7k
}
4504
4505
150k
LangAS CodeGenModule::GetGlobalConstantAddressSpace() const {
4506
  // OpenCL v1.2 s6.5.3: a string literal is in the constant address space.
4507
150k
  if (LangOpts.OpenCL)
4508
40
    return LangAS::opencl_constant;
4509
150k
  if (LangOpts.SYCLIsDevice)
4510
8
    return LangAS::sycl_global;
4511
150k
  if (LangOpts.HIP && 
LangOpts.CUDAIsDevice386
&&
getTriple().isSPIRV()60
)
4512
    // For HIPSPV map literals to cuda_device (maps to CrossWorkGroup in SPIR-V)
4513
    // instead of default AS (maps to Generic in SPIR-V). Otherwise, we end up
4514
    // with OpVariable instructions with Generic storage class which is not
4515
    // allowed (SPIR-V V1.6 s3.42.8). Also, mapping literals to SPIR-V
4516
    // UniformConstant storage class is not viable as pointers to it may not be
4517
    // casted to Generic pointers which are used to model HIP's "flat" pointers.
4518
2
    return LangAS::cuda_device;
4519
150k
  if (auto AS = getTarget().getConstantAddressSpace())
4520
150k
    return *AS;
4521
0
  return LangAS::Default;
4522
150k
}
4523
4524
// In address space agnostic languages, string literals are in default address
4525
// space in AST. However, certain targets (e.g. amdgcn) request them to be
4526
// emitted in constant address space in LLVM IR. To be consistent with other
4527
// parts of AST, string literal global variables in constant address space
4528
// need to be casted to default address space before being put into address
4529
// map and referenced by other part of CodeGen.
4530
// In OpenCL, string literals are in constant address space in AST, therefore
4531
// they should not be casted to default address space.
4532
static llvm::Constant *
4533
castStringLiteralToDefaultAddressSpace(CodeGenModule &CGM,
4534
89.1k
                                       llvm::GlobalVariable *GV) {
4535
89.1k
  llvm::Constant *Cast = GV;
4536
89.1k
  if (!CGM.getLangOpts().OpenCL) {
4537
89.1k
    auto AS = CGM.GetGlobalConstantAddressSpace();
4538
89.1k
    if (AS != LangAS::Default)
4539
61
      Cast = CGM.getTargetCodeGenInfo().performAddrSpaceCast(
4540
61
          CGM, GV, AS, LangAS::Default,
4541
61
          GV->getValueType()->getPointerTo(
4542
61
              CGM.getContext().getTargetAddressSpace(LangAS::Default)));
4543
89.1k
  }
4544
89.1k
  return Cast;
4545
89.1k
}
4546
4547
template<typename SomeDecl>
4548
void CodeGenModule::MaybeHandleStaticInExternC(const SomeDecl *D,
4549
237k
                                               llvm::GlobalValue *GV) {
4550
237k
  if (!getLangOpts().CPlusPlus)
4551
92.4k
    return;
4552
4553
  // Must have 'used' attribute, or else inline assembly can't rely on
4554
  // the name existing.
4555
144k
  if (!D->template hasAttr<UsedAttr>())
4556
144k
    return;
4557
4558
  // Must have internal linkage and an ordinary name.
4559
206
  if (!D->getIdentifier() || 
D->getFormalLinkage() != InternalLinkage205
)
4560
176
    return;
4561
4562
  // Must be in an extern "C" context. Entities declared directly within
4563
  // a record are not extern "C" even if the record is in such a context.
4564
30
  const SomeDecl *First = D->getFirstDecl();
4565
30
  if (First->getDeclContext()->isRecord() || !First->isInExternCContext())
4566
20
    return;
4567
4568
  // OK, this is an internal linkage entity inside an extern "C" linkage
4569
  // specification. Make a note of that so we can give it the "expected"
4570
  // mangled name if nothing else is using that name.
4571
10
  std::pair<StaticExternCMap::iterator, bool> R =
4572
10
      StaticExternCValues.insert(std::make_pair(D->getIdentifier(), GV));
4573
4574
  // If we have multiple internal linkage entities with the same name
4575
  // in extern "C" regions, none of them gets that name.
4576
10
  if (!R.second)
4577
3
    R.first->second = nullptr;
4578
10
}
void clang::CodeGen::CodeGenModule::MaybeHandleStaticInExternC<clang::VarDecl>(clang::VarDecl const*, llvm::GlobalValue*)
Line
Count
Source
4549
24.0k
                                               llvm::GlobalValue *GV) {
4550
24.0k
  if (!getLangOpts().CPlusPlus)
4551
6.74k
    return;
4552
4553
  // Must have 'used' attribute, or else inline assembly can't rely on
4554
  // the name existing.
4555
17.2k
  if (!D->template hasAttr<UsedAttr>())
4556
17.2k
    return;
4557
4558
  // Must have internal linkage and an ordinary name.
4559
25
  if (!D->getIdentifier() || 
D->getFormalLinkage() != InternalLinkage24
)
4560
6
    return;
4561
4562
  // Must be in an extern "C" context. Entities declared directly within
4563
  // a record are not extern "C" even if the record is in such a context.
4564
19
  const SomeDecl *First = D->getFirstDecl();
4565
19
  if (First->getDeclContext()->isRecord() || !First->isInExternCContext())
4566
15
    return;
4567
4568
  // OK, this is an internal linkage entity inside an extern "C" linkage
4569
  // specification. Make a note of that so we can give it the "expected"
4570
  // mangled name if nothing else is using that name.
4571
4
  std::pair<StaticExternCMap::iterator, bool> R =
4572
4
      StaticExternCValues.insert(std::make_pair(D->getIdentifier(), GV));
4573
4574
  // If we have multiple internal linkage entities with the same name
4575
  // in extern "C" regions, none of them gets that name.
4576
4
  if (!R.second)
4577
1
    R.first->second = nullptr;
4578
4
}
void clang::CodeGen::CodeGenModule::MaybeHandleStaticInExternC<clang::FunctionDecl>(clang::FunctionDecl const*, llvm::GlobalValue*)
Line
Count
Source
4549
213k
                                               llvm::GlobalValue *GV) {
4550
213k
  if (!getLangOpts().CPlusPlus)
4551
85.7k
    return;
4552
4553
  // Must have 'used' attribute, or else inline assembly can't rely on
4554
  // the name existing.
4555
127k
  if (!D->template hasAttr<UsedAttr>())
4556
127k
    return;
4557
4558
  // Must have internal linkage and an ordinary name.
4559
181
  if (!D->getIdentifier() || D->getFormalLinkage() != InternalLinkage)
4560
170
    return;
4561
4562
  // Must be in an extern "C" context. Entities declared directly within
4563
  // a record are not extern "C" even if the record is in such a context.
4564
11
  const SomeDecl *First = D->getFirstDecl();
4565
11
  if (First->getDeclContext()->isRecord() || !First->isInExternCContext())
4566
5
    return;
4567
4568
  // OK, this is an internal linkage entity inside an extern "C" linkage
4569
  // specification. Make a note of that so we can give it the "expected"
4570
  // mangled name if nothing else is using that name.
4571
6
  std::pair<StaticExternCMap::iterator, bool> R =
4572
6
      StaticExternCValues.insert(std::make_pair(D->getIdentifier(), GV));
4573
4574
  // If we have multiple internal linkage entities with the same name
4575
  // in extern "C" regions, none of them gets that name.
4576
6
  if (!R.second)
4577
2
    R.first->second = nullptr;
4578
6
}
4579
4580
293k
static bool shouldBeInCOMDAT(CodeGenModule &CGM, const Decl &D) {
4581
293k
  if (!CGM.supportsCOMDAT())
4582
160k
    return false;
4583
4584
133k
  if (D.hasAttr<SelectAnyAttr>())
4585
22
    return true;
4586
4587
133k
  GVALinkage Linkage;
4588
133k
  if (auto *VD = dyn_cast<VarDecl>(&D))
4589
14.8k
    Linkage = CGM.getContext().GetGVALinkageForVariable(VD);
4590
118k
  else
4591
118k
    Linkage = CGM.getContext().GetGVALinkageForFunction(cast<FunctionDecl>(&D));
4592
4593
133k
  switch (Linkage) {
4594
9.98k
  case GVA_Internal:
4595
10.3k
  case GVA_AvailableExternally:
4596
104k
  case GVA_StrongExternal:
4597
104k
    return false;
4598
25.9k
  case GVA_DiscardableODR:
4599
28.5k
  case GVA_StrongODR:
4600
28.5k
    return true;
4601
133k
  }
4602
0
  llvm_unreachable("No such linkage");
4603
0
}
4604
4605
void CodeGenModule::maybeSetTrivialComdat(const Decl &D,
4606
293k
                                          llvm::GlobalObject &GO) {
4607
293k
  if (!shouldBeInCOMDAT(*this, D))
4608
264k
    return;
4609
28.6k
  GO.setComdat(TheModule.getOrInsertComdat(GO.getName()));
4610
28.6k
}
4611
4612
/// Pass IsTentative as true if you want to create a tentative definition.
4613
void CodeGenModule::EmitGlobalVarDefinition(const VarDecl *D,
4614
24.0k
                                            bool IsTentative) {
4615
  // OpenCL global variables of sampler type are translated to function calls,
4616
  // therefore no need to be translated.
4617
24.0k
  QualType ASTTy = D->getType();
4618
24.0k
  if (getLangOpts().OpenCL && 
ASTTy->isSamplerT()320
)
4619
7
    return;
4620
4621
  // If this is OpenMP device, check if it is legal to emit this global
4622
  // normally.
4623
24.0k
  if (LangOpts.OpenMPIsDevice && 
OpenMPRuntime144
&&
4624
24.0k
      
OpenMPRuntime->emitTargetGlobalVariable(D)144
)
4625
2
    return;
4626
4627
24.0k
  llvm::TrackingVH<llvm::Constant> Init;
4628
24.0k
  bool NeedsGlobalCtor = false;
4629
24.0k
  bool NeedsGlobalDtor =
4630
24.0k
      D->needsDestruction(getContext()) == QualType::DK_cxx_destructor;
4631
4632
24.0k
  const VarDecl *InitDecl;
4633
24.0k
  const Expr *InitExpr = D->getAnyInitializer(InitDecl);
4634
4635
24.0k
  Optional<ConstantEmitter> emitter;
4636
4637
  // CUDA E.2.4.1 "__shared__ variables cannot have an initialization
4638
  // as part of their declaration."  Sema has already checked for
4639
  // error cases, so we just need to set Init to UndefValue.
4640
24.0k
  bool IsCUDASharedVar =
4641
24.0k
      getLangOpts().CUDAIsDevice && 
D->hasAttr<CUDASharedAttr>()177
;
4642
  // Shadows of initialized device-side global variables are also left
4643
  // undefined.
4644
  // Managed Variables should be initialized on both host side and device side.
4645
24.0k
  bool IsCUDAShadowVar =
4646
24.0k
      !getLangOpts().CUDAIsDevice && 
!D->hasAttr<HIPManagedAttr>()23.8k
&&
4647
24.0k
      
(23.8k
D->hasAttr<CUDAConstantAttr>()23.8k
||
D->hasAttr<CUDADeviceAttr>()23.7k
||
4648
23.8k
       
D->hasAttr<CUDASharedAttr>()23.7k
);
4649
24.0k
  bool IsCUDADeviceShadowVar =
4650
24.0k
      getLangOpts().CUDAIsDevice && 
!D->hasAttr<HIPManagedAttr>()177
&&
4651
24.0k
      
(163
D->getType()->isCUDADeviceBuiltinSurfaceType()163
||
4652
163
       D->getType()->isCUDADeviceBuiltinTextureType());
4653
24.0k
  if (getLangOpts().CUDA &&
4654
24.0k
      
(363
IsCUDASharedVar363
||
IsCUDAShadowVar356
||
IsCUDADeviceShadowVar227
))
4655
136
    Init = llvm::UndefValue::get(getTypes().ConvertTypeForMem(ASTTy));
4656
23.8k
  else if (D->hasAttr<LoaderUninitializedAttr>())
4657
45
    Init = llvm::UndefValue::get(getTypes().ConvertTypeForMem(ASTTy));
4658
23.8k
  else if (!InitExpr) {
4659
    // This is a tentative definition; tentative definitions are
4660
    // implicitly initialized with { 0 }.
4661
    //
4662
    // Note that tentative definitions are only emitted at the end of
4663
    // a translation unit, so they should never have incomplete
4664
    // type. In addition, EmitTentativeDefinition makes sure that we
4665
    // never attempt to emit a tentative definition if a real one
4666
    // exists. A use may still exists, however, so we still may need
4667
    // to do a RAUW.
4668
7.22k
    assert(!ASTTy->isIncompleteType() && "Unexpected incomplete type");
4669
0
    Init = EmitNullConstant(D->getType());
4670
16.6k
  } else {
4671
16.6k
    initializedGlobalDecl = GlobalDecl(D);
4672
16.6k
    emitter.emplace(*this);
4673
16.6k
    llvm::Constant *Initializer = emitter->tryEmitForInitializer(*InitDecl);
4674
16.6k
    if (!Initializer) {
4675
5.48k
      QualType T = InitExpr->getType();
4676
5.48k
      if (D->getType()->isReferenceType())
4677
87
        T = D->getType();
4678
4679
5.48k
      if (getLangOpts().CPlusPlus) {
4680
5.48k
        if (InitDecl->hasFlexibleArrayInit(getContext()))
4681
2
          ErrorUnsupported(D, "flexible array initializer");
4682
5.48k
        Init = EmitNullConstant(T);
4683
5.48k
        NeedsGlobalCtor = true;
4684
5.48k
      } else {
4685
0
        ErrorUnsupported(D, "static initializer");
4686
0
        Init = llvm::UndefValue::get(getTypes().ConvertType(T));
4687
0
      }
4688
11.1k
    } else {
4689
11.1k
      Init = Initializer;
4690
      // We don't need an initializer, so remove the entry for the delayed
4691
      // initializer position (just in case this entry was delayed) if we
4692
      // also don't need to register a destructor.
4693
11.1k
      if (getLangOpts().CPlusPlus && 
!NeedsGlobalDtor8.56k
)
4694
8.09k
        DelayedCXXInitPosition.erase(D);
4695
4696
11.1k
#ifndef NDEBUG
4697
11.1k
      CharUnits VarSize = getContext().getTypeSizeInChars(ASTTy) +
4698
11.1k
                          InitDecl->getFlexibleArrayInitChars(getContext());
4699
11.1k
      CharUnits CstSize = CharUnits::fromQuantity(
4700
11.1k
          getDataLayout().getTypeAllocSize(Init->getType()));
4701
11.1k
      assert(VarSize == CstSize && "Emitted constant has unexpected size");
4702
11.1k
#endif
4703
11.1k
    }
4704
16.6k
  }
4705
4706
0
  llvm::Type* InitType = Init->getType();
4707
24.0k
  llvm::Constant *Entry =
4708
24.0k
      GetAddrOfGlobalVar(D, InitType, ForDefinition_t(!IsTentative));
4709
4710
  // Strip off pointer casts if we got them.
4711
24.0k
  Entry = Entry->stripPointerCasts();
4712
4713
  // Entry is now either a Function or GlobalVariable.
4714
24.0k
  auto *GV = dyn_cast<llvm::GlobalVariable>(Entry);
4715
4716
  // We have a definition after a declaration with the wrong type.
4717
  // We must make a new GlobalVariable* and update everything that used OldGV
4718
  // (a declaration or tentative definition) with the new GlobalVariable*
4719
  // (which will be a definition).
4720
  //
4721
  // This happens if there is a prototype for a global (e.g.
4722
  // "extern int x[];") and then a definition of a different type (e.g.
4723
  // "int x[10];"). This also happens when an initializer has a different type
4724
  // from the type of the global (this happens with unions).
4725
24.0k
  if (!GV || GV->getValueType() != InitType ||
4726
24.0k
      GV->getType()->getAddressSpace() !=
4727
24.0k
          getContext().getTargetAddressSpace(GetGlobalVarAddressSpace(D))) {
4728
4729
    // Move the old entry aside so that we'll create a new one.
4730
3
    Entry->setName(StringRef());
4731
4732
    // Make a new global with the correct type, this is now guaranteed to work.
4733
3
    GV = cast<llvm::GlobalVariable>(
4734
3
        GetAddrOfGlobalVar(D, InitType, ForDefinition_t(!IsTentative))
4735
3
            ->stripPointerCasts());
4736
4737
    // Replace all uses of the old global with the new global
4738
3
    llvm::Constant *NewPtrForOldDecl =
4739
3
        llvm::ConstantExpr::getPointerBitCastOrAddrSpaceCast(GV,
4740
3
                                                             Entry->getType());
4741
3
    Entry->replaceAllUsesWith(NewPtrForOldDecl);
4742
4743
    // Erase the old global, since it is no longer used.
4744
3
    cast<llvm::GlobalValue>(Entry)->eraseFromParent();
4745
3
  }
4746
4747
24.0k
  MaybeHandleStaticInExternC(D, GV);
4748
4749
24.0k
  if (D->hasAttr<AnnotateAttr>())
4750
8
    AddGlobalAnnotations(D, GV);
4751
4752
  // Set the llvm linkage type as appropriate.
4753
24.0k
  llvm::GlobalValue::LinkageTypes Linkage =
4754
24.0k
      getLLVMLinkageVarDefinition(D, GV->isConstant());
4755
4756
  // CUDA B.2.1 "The __device__ qualifier declares a variable that resides on
4757
  // the device. [...]"
4758
  // CUDA B.2.2 "The __constant__ qualifier, optionally used together with
4759
  // __device__, declares a variable that: [...]
4760
  // Is accessible from all the threads within the grid and from the host
4761
  // through the runtime library (cudaGetSymbolAddress() / cudaGetSymbolSize()
4762
  // / cudaMemcpyToSymbol() / cudaMemcpyFromSymbol())."
4763
24.0k
  if (GV && LangOpts.CUDA) {
4764
363
    if (LangOpts.CUDAIsDevice) {
4765
177
      if (Linkage != llvm::GlobalValue::InternalLinkage &&
4766
177
          
(141
D->hasAttr<CUDADeviceAttr>()141
||
D->hasAttr<CUDAConstantAttr>()58
||
4767
141
           
D->getType()->isCUDADeviceBuiltinSurfaceType()6
||
4768
141
           
D->getType()->isCUDADeviceBuiltinTextureType()6
))
4769
135
        GV->setExternallyInitialized(true);
4770
186
    } else {
4771
186
      getCUDARuntime().internalizeDeviceSideVar(D, Linkage);
4772
186
    }
4773
363
    getCUDARuntime().handleVarRegistration(D, *GV);
4774
363
  }
4775
4776
24.0k
  GV->setInitializer(Init);
4777
24.0k
  if (emitter)
4778
16.6k
    emitter->finalize(GV);
4779
4780
  // If it is safe to mark the global 'constant', do so now.
4781
24.0k
  GV->setConstant(!NeedsGlobalCtor && 
!NeedsGlobalDtor18.5k
&&
4782
24.0k
                  
isTypeConstant(D->getType(), true)18.0k
);
4783
4784
  // If it is in a read-only section, mark it 'constant'.
4785
24.0k
  if (const SectionAttr *SA = D->getAttr<SectionAttr>()) {
4786
54
    const ASTContext::SectionInfo &SI = Context.SectionInfos[SA->getName()];
4787
54
    if ((SI.SectionFlags & ASTContext::PSF_Write) == 0)
4788
8
      GV->setConstant(true);
4789
54
  }
4790
4791
24.0k
  CharUnits AlignVal = getContext().getDeclAlign(D);
4792
  // Check for alignment specifed in an 'omp allocate' directive.
4793
24.0k
  if (llvm::Optional<CharUnits> AlignValFromAllocate =
4794
24.0k
          getOMPAllocateAlignment(D))
4795
16
    AlignVal = *AlignValFromAllocate;
4796
24.0k
  GV->setAlignment(AlignVal.getAsAlign());
4797
4798
  // On Darwin, unlike other Itanium C++ ABI platforms, the thread-wrapper
4799
  // function is only defined alongside the variable, not also alongside
4800
  // callers. Normally, all accesses to a thread_local go through the
4801
  // thread-wrapper in order to ensure initialization has occurred, underlying
4802
  // variable will never be used other than the thread-wrapper, so it can be
4803
  // converted to internal linkage.
4804
  //
4805
  // However, if the variable has the 'constinit' attribute, it _can_ be
4806
  // referenced directly, without calling the thread-wrapper, so the linkage
4807
  // must not be changed.
4808
  //
4809
  // Additionally, if the variable isn't plain external linkage, e.g. if it's
4810
  // weak or linkonce, the de-duplication semantics are important to preserve,
4811
  // so we don't change the linkage.
4812
24.0k
  if (D->getTLSKind() == VarDecl::TLS_Dynamic &&
4813
24.0k
      
Linkage == llvm::GlobalValue::ExternalLinkage273
&&
4814
24.0k
      
Context.getTargetInfo().getTriple().isOSDarwin()122
&&
4815
24.0k
      
!D->hasAttr<ConstInitAttr>()32
)
4816
29
    Linkage = llvm::GlobalValue::InternalLinkage;
4817
4818
24.0k
  GV->setLinkage(Linkage);
4819
24.0k
  if (D->hasAttr<DLLImportAttr>())
4820
50
    GV->setDLLStorageClass(llvm::GlobalVariable::DLLImportStorageClass);
4821
23.9k
  else if (D->hasAttr<DLLExportAttr>())
4822
511
    GV->setDLLStorageClass(llvm::GlobalVariable::DLLExportStorageClass);
4823
23.4k
  else
4824
23.4k
    GV->setDLLStorageClass(llvm::GlobalVariable::DefaultStorageClass);
4825
4826
24.0k
  if (Linkage == llvm::GlobalVariable::CommonLinkage) {
4827
    // common vars aren't constant even if declared const.
4828
17
    GV->setConstant(false);
4829
    // Tentative definition of global variables may be initialized with
4830
    // non-zero null pointers. In this case they should have weak linkage
4831
    // since common linkage must have zero initializer and must not have
4832
    // explicit section therefore cannot have non-zero initial value.
4833
17
    if (!GV->getInitializer()->isNullValue())
4834
4
      GV->setLinkage(llvm::GlobalVariable::WeakAnyLinkage);
4835
17
  }
4836
4837
24.0k
  setNonAliasAttributes(D, GV);
4838
4839
24.0k
  if (D->getTLSKind() && 
!GV->isThreadLocal()345
) {
4840
4
    if (D->getTLSKind() == VarDecl::TLS_Dynamic)
4841
0
      CXXThreadLocals.push_back(D);
4842
4
    setTLSMode(GV, *D);
4843
4
  }
4844
4845
24.0k
  maybeSetTrivialComdat(*D, *GV);
4846
4847
  // Emit the initializer function if necessary.
4848
24.0k
  if (NeedsGlobalCtor || 
NeedsGlobalDtor18.5k
)
4849
5.94k
    EmitCXXGlobalVarDeclInitFunc(D, GV, NeedsGlobalCtor);
4850
4851
24.0k
  SanitizerMD->reportGlobal(GV, *D, NeedsGlobalCtor);
4852
4853
  // Emit global variable debug information.
4854
24.0k
  if (CGDebugInfo *DI = getModuleDebugInfo())
4855
4.89k
    if (getCodeGenOpts().hasReducedDebugInfo())
4856
4.80k
      DI->EmitGlobalVariable(GV, D);
4857
24.0k
}
4858
4859
7
void CodeGenModule::EmitExternalVarDeclaration(const VarDecl *D) {
4860
7
  if (CGDebugInfo *DI = getModuleDebugInfo())
4861
7
    if (getCodeGenOpts().hasReducedDebugInfo()) {
4862
7
      QualType ASTTy = D->getType();
4863
7
      llvm::Type *Ty = getTypes().ConvertTypeForMem(D->getType());
4864
7
      llvm::Constant *GV =
4865
7
          GetOrCreateLLVMGlobal(D->getName(), Ty, ASTTy.getAddressSpace(), D);
4866
7
      DI->EmitExternalVariable(
4867
7
          cast<llvm::GlobalVariable>(GV->stripPointerCasts()), D);
4868
7
    }
4869
7
}
4870
4871
static bool isVarDeclStrongDefinition(const ASTContext &Context,
4872
                                      CodeGenModule &CGM, const VarDecl *D,
4873
6.55k
                                      bool NoCommon) {
4874
  // Don't give variables common linkage if -fno-common was specified unless it
4875
  // was overridden by a NoCommon attribute.
4876
6.55k
  if ((NoCommon || 
D->hasAttr<NoCommonAttr>()41
) &&
!D->hasAttr<CommonAttr>()6.51k
)
4877
6.51k
    return true;
4878
4879
  // C11 6.9.2/2:
4880
  //   A declaration of an identifier for an object that has file scope without
4881
  //   an initializer, and without a storage-class specifier or with the
4882
  //   storage-class specifier static, constitutes a tentative definition.
4883
42
  if (D->getInit() || 
D->hasExternalStorage()18
)
4884
24
    return true;
4885
4886
  // A variable cannot be both common and exist in a section.
4887
18
  if (D->hasAttr<SectionAttr>())
4888
0
    return true;
4889
4890
  // A variable cannot be both common and exist in a section.
4891
  // We don't try to determine which is the right section in the front-end.
4892
  // If no specialized section name is applicable, it will resort to default.
4893
18
  if (D->hasAttr<PragmaClangBSSSectionAttr>() ||
4894
18
      D->hasAttr<PragmaClangDataSectionAttr>() ||
4895
18
      D->hasAttr<PragmaClangRelroSectionAttr>() ||
4896
18
      D->hasAttr<PragmaClangRodataSectionAttr>())
4897
0
    return true;
4898
4899
  // Thread local vars aren't considered common linkage.
4900
18
  if (D->getTLSKind())
4901
0
    return true;
4902
4903
  // Tentative definitions marked with WeakImportAttr are true definitions.
4904
18
  if (D->hasAttr<WeakImportAttr>())
4905
0
    return true;
4906
4907
  // A variable cannot be both common and exist in a comdat.
4908
18
  if (shouldBeInCOMDAT(CGM, *D))
4909
0
    return true;
4910
4911
  // Declarations with a required alignment do not have common linkage in MSVC
4912
  // mode.
4913
18
  if (Context.getTargetInfo().getCXXABI().isMicrosoft()) {
4914
3
    if (D->hasAttr<AlignedAttr>())
4915
0
      return true;
4916
3
    QualType VarType = D->getType();
4917
3
    if (Context.isAlignmentRequired(VarType))
4918
0
      return true;
4919
4920
3
    if (const auto *RT = VarType->getAs<RecordType>()) {
4921
0
      const RecordDecl *RD = RT->getDecl();
4922
0
      for (const FieldDecl *FD : RD->fields()) {
4923
0
        if (FD->isBitField())
4924
0
          continue;
4925
0
        if (FD->hasAttr<AlignedAttr>())
4926
0
          return true;
4927
0
        if (Context.isAlignmentRequired(FD->getType()))
4928
0
          return true;
4929
0
      }
4930
0
    }
4931
3
  }
4932
4933
  // Microsoft's link.exe doesn't support alignments greater than 32 bytes for
4934
  // common symbols, so symbols with greater alignment requirements cannot be
4935
  // common.
4936
  // Other COFF linkers (ld.bfd and LLD) support arbitrary power-of-two
4937
  // alignments for common symbols via the aligncomm directive, so this
4938
  // restriction only applies to MSVC environments.
4939
18
  if (Context.getTargetInfo().getTriple().isKnownWindowsMSVCEnvironment() &&
4940
18
      Context.getTypeAlignIfKnown(D->getType()) >
4941
3
          Context.toBits(CharUnits::fromQuantity(32)))
4942
1
    return true;
4943
4944
17
  return false;
4945
18
}
4946
4947
llvm::GlobalValue::LinkageTypes CodeGenModule::getLLVMLinkageForDeclarator(
4948
724k
    const DeclaratorDecl *D, GVALinkage Linkage, bool IsConstantVariable) {
4949
724k
  if (Linkage == GVA_Internal)
4950
76.4k
    return llvm::Function::InternalLinkage;
4951
4952
647k
  if (D->hasAttr<WeakAttr>())
4953
149
    return llvm::GlobalVariable::WeakAnyLinkage;
4954
4955
647k
  if (const auto *FD = D->getAsFunction())
4956
624k
    if (FD->isMultiVersion() && 
Linkage == GVA_AvailableExternally376
)
4957
22
      return llvm::GlobalVariable::LinkOnceAnyLinkage;
4958
4959
  // We are guaranteed to have a strong definition somewhere else,
4960
  // so we can use available_externally linkage.
4961
647k
  if (Linkage == GVA_AvailableExternally)
4962
3.15k
    return llvm::GlobalValue::AvailableExternallyLinkage;
4963
4964
  // Note that Apple's kernel linker doesn't support symbol
4965
  // coalescing, so we need to avoid linkonce and weak linkages there.
4966
  // Normally, this means we just map to internal, but for explicit
4967
  // instantiations we'll map to external.
4968
4969
  // In C++, the compiler has to emit a definition in every translation unit
4970
  // that references the function.  We should use linkonce_odr because
4971
  // a) if all references in this translation unit are optimized away, we
4972
  // don't need to codegen it.  b) if the function persists, it needs to be
4973
  // merged with other definitions. c) C++ has the ODR, so we know the
4974
  // definition is dependable.
4975
644k
  if (Linkage == GVA_DiscardableODR)
4976
352k
    return !Context.getLangOpts().AppleKext ? 
llvm::Function::LinkOnceODRLinkage352k
4977
352k
                                            : 
llvm::Function::InternalLinkage30
;
4978
4979
  // An explicit instantiation of a template has weak linkage, since
4980
  // explicit instantiations can occur in multiple translation units
4981
  // and must all be equivalent. However, we are not allowed to
4982
  // throw away these explicit instantiations.
4983
  //
4984
  // CUDA/HIP: For -fno-gpu-rdc case, device code is limited to one TU,
4985
  // so say that CUDA templates are either external (for kernels) or internal.
4986
  // This lets llvm perform aggressive inter-procedural optimizations. For
4987
  // -fgpu-rdc case, device function calls across multiple TU's are allowed,
4988
  // therefore we need to follow the normal linkage paradigm.
4989
292k
  if (Linkage == GVA_StrongODR) {
4990
6.18k
    if (getLangOpts().AppleKext)
4991
3
      return llvm::Function::ExternalLinkage;
4992
6.18k
    if (getLangOpts().CUDA && 
getLangOpts().CUDAIsDevice97
&&
4993
6.18k
        
!getLangOpts().GPURelocatableDeviceCode97
)
4994
69
      return D->hasAttr<CUDAGlobalAttr>() ? 
llvm::Function::ExternalLinkage64
4995
69
                                          : 
llvm::Function::InternalLinkage5
;
4996
6.11k
    return llvm::Function::WeakODRLinkage;
4997
6.18k
  }
4998
4999
  // C++ doesn't have tentative definitions and thus cannot have common
5000
  // linkage.
5001
286k
  if (!getLangOpts().CPlusPlus && 
isa<VarDecl>(D)147k
&&
5002
286k
      !isVarDeclStrongDefinition(Context, *this, cast<VarDecl>(D),
5003
6.55k
                                 CodeGenOpts.NoCommon))
5004
17
    return llvm::GlobalVariable::CommonLinkage;
5005
5006
  // selectany symbols are externally visible, so use weak instead of
5007
  // linkonce.  MSVC optimizes away references to const selectany globals, so
5008
  // all definitions should be the same and ODR linkage should be used.
5009
  // http://msdn.microsoft.com/en-us/library/5tkz6s71.aspx
5010
286k
  if (D->hasAttr<SelectAnyAttr>())
5011
19
    return llvm::GlobalVariable::WeakODRLinkage;
5012
5013
  // Otherwise, we have strong external linkage.
5014
286k
  assert(Linkage == GVA_StrongExternal);
5015
0
  return llvm::GlobalVariable::ExternalLinkage;
5016
286k
}
5017
5018
llvm::GlobalValue::LinkageTypes CodeGenModule::getLLVMLinkageVarDefinition(
5019
38.2k
    const VarDecl *VD, bool IsConstant) {
5020
38.2k
  GVALinkage Linkage = getContext().GetGVALinkageForVariable(VD);
5021
38.2k
  return getLLVMLinkageForDeclarator(VD, Linkage, IsConstant);
5022
38.2k
}
5023
5024
/// Replace the uses of a function that was declared with a non-proto type.
5025
/// We want to silently drop extra arguments from call sites
5026
static void replaceUsesOfNonProtoConstant(llvm::Constant *old,
5027
64
                                          llvm::Function *newFn) {
5028
  // Fast path.
5029
64
  if (old->use_empty()) 
return2
;
5030
5031
62
  llvm::Type *newRetTy = newFn->getReturnType();
5032
62
  SmallVector<llvm::Value*, 4> newArgs;
5033
5034
62
  for (llvm::Value::use_iterator ui = old->use_begin(), ue = old->use_end();
5035
128
         ui != ue; ) {
5036
66
    llvm::Value::use_iterator use = ui++; // Increment before the use is erased.
5037
66
    llvm::User *user = use->getUser();
5038
5039
    // Recognize and replace uses of bitcasts.  Most calls to
5040
    // unprototyped functions will use bitcasts.
5041
66
    if (auto *bitcast = dyn_cast<llvm::ConstantExpr>(user)) {
5042
12
      if (bitcast->getOpcode() == llvm::Instruction::BitCast)
5043
12
        replaceUsesOfNonProtoConstant(bitcast, newFn);
5044
12
      continue;
5045
12
    }
5046
5047
    // Recognize calls to the function.
5048
54
    llvm::CallBase *callSite = dyn_cast<llvm::CallBase>(user);
5049
<