Coverage Report

Created: 2022-01-25 06:29

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