Coverage Report

Created: 2020-09-15 12:33

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