[DFAJumpThreading] Remove incoming StartBlock from all phis when unfolding select...
[llvm-project.git] / clang / lib / CodeGen / CodeGenModule.cpp
blob35f651b39f6748a0b8b233bedc7a30c6a69d2dcf
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.
11 //===----------------------------------------------------------------------===//
13 #include "CodeGenModule.h"
14 #include "ABIInfo.h"
15 #include "CGBlocks.h"
16 #include "CGCUDARuntime.h"
17 #include "CGCXXABI.h"
18 #include "CGCall.h"
19 #include "CGDebugInfo.h"
20 #include "CGHLSLRuntime.h"
21 #include "CGObjCRuntime.h"
22 #include "CGOpenCLRuntime.h"
23 #include "CGOpenMPRuntime.h"
24 #include "CGOpenMPRuntimeGPU.h"
25 #include "CodeGenFunction.h"
26 #include "CodeGenPGO.h"
27 #include "ConstantEmitter.h"
28 #include "CoverageMappingGen.h"
29 #include "TargetInfo.h"
30 #include "clang/AST/ASTContext.h"
31 #include "clang/AST/CharUnits.h"
32 #include "clang/AST/DeclCXX.h"
33 #include "clang/AST/DeclObjC.h"
34 #include "clang/AST/DeclTemplate.h"
35 #include "clang/AST/Mangle.h"
36 #include "clang/AST/RecursiveASTVisitor.h"
37 #include "clang/AST/StmtVisitor.h"
38 #include "clang/Basic/Builtins.h"
39 #include "clang/Basic/CharInfo.h"
40 #include "clang/Basic/CodeGenOptions.h"
41 #include "clang/Basic/Diagnostic.h"
42 #include "clang/Basic/FileManager.h"
43 #include "clang/Basic/Module.h"
44 #include "clang/Basic/SourceManager.h"
45 #include "clang/Basic/TargetInfo.h"
46 #include "clang/Basic/Version.h"
47 #include "clang/CodeGen/BackendUtil.h"
48 #include "clang/CodeGen/ConstantInitBuilder.h"
49 #include "clang/Frontend/FrontendDiagnostic.h"
50 #include "llvm/ADT/STLExtras.h"
51 #include "llvm/ADT/StringExtras.h"
52 #include "llvm/ADT/StringSwitch.h"
53 #include "llvm/Analysis/TargetLibraryInfo.h"
54 #include "llvm/Frontend/OpenMP/OMPIRBuilder.h"
55 #include "llvm/IR/AttributeMask.h"
56 #include "llvm/IR/CallingConv.h"
57 #include "llvm/IR/DataLayout.h"
58 #include "llvm/IR/Intrinsics.h"
59 #include "llvm/IR/LLVMContext.h"
60 #include "llvm/IR/Module.h"
61 #include "llvm/IR/ProfileSummary.h"
62 #include "llvm/ProfileData/InstrProfReader.h"
63 #include "llvm/ProfileData/SampleProf.h"
64 #include "llvm/Support/CRC.h"
65 #include "llvm/Support/CodeGen.h"
66 #include "llvm/Support/CommandLine.h"
67 #include "llvm/Support/ConvertUTF.h"
68 #include "llvm/Support/ErrorHandling.h"
69 #include "llvm/Support/TimeProfiler.h"
70 #include "llvm/Support/xxhash.h"
71 #include "llvm/TargetParser/Triple.h"
72 #include "llvm/TargetParser/X86TargetParser.h"
73 #include <optional>
75 using namespace clang;
76 using namespace CodeGen;
78 static llvm::cl::opt<bool> LimitedCoverage(
79 "limited-coverage-experimental", llvm::cl::Hidden,
80 llvm::cl::desc("Emit limited coverage mapping information (experimental)"));
82 static const char AnnotationSection[] = "llvm.metadata";
84 static CGCXXABI *createCXXABI(CodeGenModule &CGM) {
85 switch (CGM.getContext().getCXXABIKind()) {
86 case TargetCXXABI::AppleARM64:
87 case TargetCXXABI::Fuchsia:
88 case TargetCXXABI::GenericAArch64:
89 case TargetCXXABI::GenericARM:
90 case TargetCXXABI::iOS:
91 case TargetCXXABI::WatchOS:
92 case TargetCXXABI::GenericMIPS:
93 case TargetCXXABI::GenericItanium:
94 case TargetCXXABI::WebAssembly:
95 case TargetCXXABI::XL:
96 return CreateItaniumCXXABI(CGM);
97 case TargetCXXABI::Microsoft:
98 return CreateMicrosoftCXXABI(CGM);
101 llvm_unreachable("invalid C++ ABI kind");
104 static std::unique_ptr<TargetCodeGenInfo>
105 createTargetCodeGenInfo(CodeGenModule &CGM) {
106 const TargetInfo &Target = CGM.getTarget();
107 const llvm::Triple &Triple = Target.getTriple();
108 const CodeGenOptions &CodeGenOpts = CGM.getCodeGenOpts();
110 switch (Triple.getArch()) {
111 default:
112 return createDefaultTargetCodeGenInfo(CGM);
114 case llvm::Triple::le32:
115 return createPNaClTargetCodeGenInfo(CGM);
116 case llvm::Triple::m68k:
117 return createM68kTargetCodeGenInfo(CGM);
118 case llvm::Triple::mips:
119 case llvm::Triple::mipsel:
120 if (Triple.getOS() == llvm::Triple::NaCl)
121 return createPNaClTargetCodeGenInfo(CGM);
122 return createMIPSTargetCodeGenInfo(CGM, /*IsOS32=*/true);
124 case llvm::Triple::mips64:
125 case llvm::Triple::mips64el:
126 return createMIPSTargetCodeGenInfo(CGM, /*IsOS32=*/false);
128 case llvm::Triple::avr: {
129 // For passing parameters, R8~R25 are used on avr, and R18~R25 are used
130 // on avrtiny. For passing return value, R18~R25 are used on avr, and
131 // R22~R25 are used on avrtiny.
132 unsigned NPR = Target.getABI() == "avrtiny" ? 6 : 18;
133 unsigned NRR = Target.getABI() == "avrtiny" ? 4 : 8;
134 return createAVRTargetCodeGenInfo(CGM, NPR, NRR);
137 case llvm::Triple::aarch64:
138 case llvm::Triple::aarch64_32:
139 case llvm::Triple::aarch64_be: {
140 AArch64ABIKind Kind = AArch64ABIKind::AAPCS;
141 if (Target.getABI() == "darwinpcs")
142 Kind = AArch64ABIKind::DarwinPCS;
143 else if (Triple.isOSWindows())
144 return createWindowsAArch64TargetCodeGenInfo(CGM, AArch64ABIKind::Win64);
146 return createAArch64TargetCodeGenInfo(CGM, Kind);
149 case llvm::Triple::wasm32:
150 case llvm::Triple::wasm64: {
151 WebAssemblyABIKind Kind = WebAssemblyABIKind::MVP;
152 if (Target.getABI() == "experimental-mv")
153 Kind = WebAssemblyABIKind::ExperimentalMV;
154 return createWebAssemblyTargetCodeGenInfo(CGM, Kind);
157 case llvm::Triple::arm:
158 case llvm::Triple::armeb:
159 case llvm::Triple::thumb:
160 case llvm::Triple::thumbeb: {
161 if (Triple.getOS() == llvm::Triple::Win32)
162 return createWindowsARMTargetCodeGenInfo(CGM, ARMABIKind::AAPCS_VFP);
164 ARMABIKind Kind = ARMABIKind::AAPCS;
165 StringRef ABIStr = Target.getABI();
166 if (ABIStr == "apcs-gnu")
167 Kind = ARMABIKind::APCS;
168 else if (ABIStr == "aapcs16")
169 Kind = ARMABIKind::AAPCS16_VFP;
170 else if (CodeGenOpts.FloatABI == "hard" ||
171 (CodeGenOpts.FloatABI != "soft" &&
172 (Triple.getEnvironment() == llvm::Triple::GNUEABIHF ||
173 Triple.getEnvironment() == llvm::Triple::MuslEABIHF ||
174 Triple.getEnvironment() == llvm::Triple::EABIHF)))
175 Kind = ARMABIKind::AAPCS_VFP;
177 return createARMTargetCodeGenInfo(CGM, Kind);
180 case llvm::Triple::ppc: {
181 if (Triple.isOSAIX())
182 return createAIXTargetCodeGenInfo(CGM, /*Is64Bit=*/false);
184 bool IsSoftFloat =
185 CodeGenOpts.FloatABI == "soft" || Target.hasFeature("spe");
186 return createPPC32TargetCodeGenInfo(CGM, IsSoftFloat);
188 case llvm::Triple::ppcle: {
189 bool IsSoftFloat = CodeGenOpts.FloatABI == "soft";
190 return createPPC32TargetCodeGenInfo(CGM, IsSoftFloat);
192 case llvm::Triple::ppc64:
193 if (Triple.isOSAIX())
194 return createAIXTargetCodeGenInfo(CGM, /*Is64Bit=*/true);
196 if (Triple.isOSBinFormatELF()) {
197 PPC64_SVR4_ABIKind Kind = PPC64_SVR4_ABIKind::ELFv1;
198 if (Target.getABI() == "elfv2")
199 Kind = PPC64_SVR4_ABIKind::ELFv2;
200 bool IsSoftFloat = CodeGenOpts.FloatABI == "soft";
202 return createPPC64_SVR4_TargetCodeGenInfo(CGM, Kind, IsSoftFloat);
204 return createPPC64TargetCodeGenInfo(CGM);
205 case llvm::Triple::ppc64le: {
206 assert(Triple.isOSBinFormatELF() && "PPC64 LE non-ELF not supported!");
207 PPC64_SVR4_ABIKind Kind = PPC64_SVR4_ABIKind::ELFv2;
208 if (Target.getABI() == "elfv1")
209 Kind = PPC64_SVR4_ABIKind::ELFv1;
210 bool IsSoftFloat = CodeGenOpts.FloatABI == "soft";
212 return createPPC64_SVR4_TargetCodeGenInfo(CGM, Kind, IsSoftFloat);
215 case llvm::Triple::nvptx:
216 case llvm::Triple::nvptx64:
217 return createNVPTXTargetCodeGenInfo(CGM);
219 case llvm::Triple::msp430:
220 return createMSP430TargetCodeGenInfo(CGM);
222 case llvm::Triple::riscv32:
223 case llvm::Triple::riscv64: {
224 StringRef ABIStr = Target.getABI();
225 unsigned XLen = Target.getPointerWidth(LangAS::Default);
226 unsigned ABIFLen = 0;
227 if (ABIStr.endswith("f"))
228 ABIFLen = 32;
229 else if (ABIStr.endswith("d"))
230 ABIFLen = 64;
231 return createRISCVTargetCodeGenInfo(CGM, XLen, ABIFLen);
234 case llvm::Triple::systemz: {
235 bool SoftFloat = CodeGenOpts.FloatABI == "soft";
236 bool HasVector = !SoftFloat && Target.getABI() == "vector";
237 return createSystemZTargetCodeGenInfo(CGM, HasVector, SoftFloat);
240 case llvm::Triple::tce:
241 case llvm::Triple::tcele:
242 return createTCETargetCodeGenInfo(CGM);
244 case llvm::Triple::x86: {
245 bool IsDarwinVectorABI = Triple.isOSDarwin();
246 bool IsWin32FloatStructABI = Triple.isOSWindows() && !Triple.isOSCygMing();
248 if (Triple.getOS() == llvm::Triple::Win32) {
249 return createWinX86_32TargetCodeGenInfo(
250 CGM, IsDarwinVectorABI, IsWin32FloatStructABI,
251 CodeGenOpts.NumRegisterParameters);
253 return createX86_32TargetCodeGenInfo(
254 CGM, IsDarwinVectorABI, IsWin32FloatStructABI,
255 CodeGenOpts.NumRegisterParameters, CodeGenOpts.FloatABI == "soft");
258 case llvm::Triple::x86_64: {
259 StringRef ABI = Target.getABI();
260 X86AVXABILevel AVXLevel = (ABI == "avx512" ? X86AVXABILevel::AVX512
261 : ABI == "avx" ? X86AVXABILevel::AVX
262 : X86AVXABILevel::None);
264 switch (Triple.getOS()) {
265 case llvm::Triple::Win32:
266 return createWinX86_64TargetCodeGenInfo(CGM, AVXLevel);
267 default:
268 return createX86_64TargetCodeGenInfo(CGM, AVXLevel);
271 case llvm::Triple::hexagon:
272 return createHexagonTargetCodeGenInfo(CGM);
273 case llvm::Triple::lanai:
274 return createLanaiTargetCodeGenInfo(CGM);
275 case llvm::Triple::r600:
276 return createAMDGPUTargetCodeGenInfo(CGM);
277 case llvm::Triple::amdgcn:
278 return createAMDGPUTargetCodeGenInfo(CGM);
279 case llvm::Triple::sparc:
280 return createSparcV8TargetCodeGenInfo(CGM);
281 case llvm::Triple::sparcv9:
282 return createSparcV9TargetCodeGenInfo(CGM);
283 case llvm::Triple::xcore:
284 return createXCoreTargetCodeGenInfo(CGM);
285 case llvm::Triple::arc:
286 return createARCTargetCodeGenInfo(CGM);
287 case llvm::Triple::spir:
288 case llvm::Triple::spir64:
289 return createCommonSPIRTargetCodeGenInfo(CGM);
290 case llvm::Triple::spirv32:
291 case llvm::Triple::spirv64:
292 return createSPIRVTargetCodeGenInfo(CGM);
293 case llvm::Triple::ve:
294 return createVETargetCodeGenInfo(CGM);
295 case llvm::Triple::csky: {
296 bool IsSoftFloat = !Target.hasFeature("hard-float-abi");
297 bool hasFP64 =
298 Target.hasFeature("fpuv2_df") || Target.hasFeature("fpuv3_df");
299 return createCSKYTargetCodeGenInfo(CGM, IsSoftFloat ? 0
300 : hasFP64 ? 64
301 : 32);
303 case llvm::Triple::bpfeb:
304 case llvm::Triple::bpfel:
305 return createBPFTargetCodeGenInfo(CGM);
306 case llvm::Triple::loongarch32:
307 case llvm::Triple::loongarch64: {
308 StringRef ABIStr = Target.getABI();
309 unsigned ABIFRLen = 0;
310 if (ABIStr.endswith("f"))
311 ABIFRLen = 32;
312 else if (ABIStr.endswith("d"))
313 ABIFRLen = 64;
314 return createLoongArchTargetCodeGenInfo(
315 CGM, Target.getPointerWidth(LangAS::Default), ABIFRLen);
320 const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() {
321 if (!TheTargetCodeGenInfo)
322 TheTargetCodeGenInfo = createTargetCodeGenInfo(*this);
323 return *TheTargetCodeGenInfo;
326 CodeGenModule::CodeGenModule(ASTContext &C,
327 IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS,
328 const HeaderSearchOptions &HSO,
329 const PreprocessorOptions &PPO,
330 const CodeGenOptions &CGO, llvm::Module &M,
331 DiagnosticsEngine &diags,
332 CoverageSourceInfo *CoverageInfo)
333 : Context(C), LangOpts(C.getLangOpts()), FS(FS), HeaderSearchOpts(HSO),
334 PreprocessorOpts(PPO), CodeGenOpts(CGO), TheModule(M), Diags(diags),
335 Target(C.getTargetInfo()), ABI(createCXXABI(*this)),
336 VMContext(M.getContext()), Types(*this), VTables(*this),
337 SanitizerMD(new SanitizerMetadata(*this)) {
339 // Initialize the type cache.
340 llvm::LLVMContext &LLVMContext = M.getContext();
341 VoidTy = llvm::Type::getVoidTy(LLVMContext);
342 Int8Ty = llvm::Type::getInt8Ty(LLVMContext);
343 Int16Ty = llvm::Type::getInt16Ty(LLVMContext);
344 Int32Ty = llvm::Type::getInt32Ty(LLVMContext);
345 Int64Ty = llvm::Type::getInt64Ty(LLVMContext);
346 HalfTy = llvm::Type::getHalfTy(LLVMContext);
347 BFloatTy = llvm::Type::getBFloatTy(LLVMContext);
348 FloatTy = llvm::Type::getFloatTy(LLVMContext);
349 DoubleTy = llvm::Type::getDoubleTy(LLVMContext);
350 PointerWidthInBits = C.getTargetInfo().getPointerWidth(LangAS::Default);
351 PointerAlignInBytes =
352 C.toCharUnitsFromBits(C.getTargetInfo().getPointerAlign(LangAS::Default))
353 .getQuantity();
354 SizeSizeInBytes =
355 C.toCharUnitsFromBits(C.getTargetInfo().getMaxPointerWidth()).getQuantity();
356 IntAlignInBytes =
357 C.toCharUnitsFromBits(C.getTargetInfo().getIntAlign()).getQuantity();
358 CharTy =
359 llvm::IntegerType::get(LLVMContext, C.getTargetInfo().getCharWidth());
360 IntTy = llvm::IntegerType::get(LLVMContext, C.getTargetInfo().getIntWidth());
361 IntPtrTy = llvm::IntegerType::get(LLVMContext,
362 C.getTargetInfo().getMaxPointerWidth());
363 Int8PtrTy = llvm::PointerType::get(LLVMContext, 0);
364 const llvm::DataLayout &DL = M.getDataLayout();
365 AllocaInt8PtrTy =
366 llvm::PointerType::get(LLVMContext, DL.getAllocaAddrSpace());
367 GlobalsInt8PtrTy =
368 llvm::PointerType::get(LLVMContext, DL.getDefaultGlobalsAddressSpace());
369 ConstGlobalsPtrTy = llvm::PointerType::get(
370 LLVMContext, C.getTargetAddressSpace(GetGlobalConstantAddressSpace()));
371 ASTAllocaAddressSpace = getTargetCodeGenInfo().getASTAllocaAddressSpace();
373 // Build C++20 Module initializers.
374 // TODO: Add Microsoft here once we know the mangling required for the
375 // initializers.
376 CXX20ModuleInits =
377 LangOpts.CPlusPlusModules && getCXXABI().getMangleContext().getKind() ==
378 ItaniumMangleContext::MK_Itanium;
380 RuntimeCC = getTargetCodeGenInfo().getABIInfo().getRuntimeCC();
382 if (LangOpts.ObjC)
383 createObjCRuntime();
384 if (LangOpts.OpenCL)
385 createOpenCLRuntime();
386 if (LangOpts.OpenMP)
387 createOpenMPRuntime();
388 if (LangOpts.CUDA)
389 createCUDARuntime();
390 if (LangOpts.HLSL)
391 createHLSLRuntime();
393 // Enable TBAA unless it's suppressed. ThreadSanitizer needs TBAA even at O0.
394 if (LangOpts.Sanitize.has(SanitizerKind::Thread) ||
395 (!CodeGenOpts.RelaxedAliasing && CodeGenOpts.OptimizationLevel > 0))
396 TBAA.reset(new CodeGenTBAA(Context, TheModule, CodeGenOpts, getLangOpts(),
397 getCXXABI().getMangleContext()));
399 // If debug info or coverage generation is enabled, create the CGDebugInfo
400 // object.
401 if (CodeGenOpts.getDebugInfo() != llvm::codegenoptions::NoDebugInfo ||
402 CodeGenOpts.CoverageNotesFile.size() ||
403 CodeGenOpts.CoverageDataFile.size())
404 DebugInfo.reset(new CGDebugInfo(*this));
406 Block.GlobalUniqueCount = 0;
408 if (C.getLangOpts().ObjC)
409 ObjCData.reset(new ObjCEntrypoints());
411 if (CodeGenOpts.hasProfileClangUse()) {
412 auto ReaderOrErr = llvm::IndexedInstrProfReader::create(
413 CodeGenOpts.ProfileInstrumentUsePath, *FS,
414 CodeGenOpts.ProfileRemappingFile);
415 // We're checking for profile read errors in CompilerInvocation, so if
416 // there was an error it should've already been caught. If it hasn't been
417 // somehow, trip an assertion.
418 assert(ReaderOrErr);
419 PGOReader = std::move(ReaderOrErr.get());
422 // If coverage mapping generation is enabled, create the
423 // CoverageMappingModuleGen object.
424 if (CodeGenOpts.CoverageMapping)
425 CoverageMapping.reset(new CoverageMappingModuleGen(*this, *CoverageInfo));
427 // Generate the module name hash here if needed.
428 if (CodeGenOpts.UniqueInternalLinkageNames &&
429 !getModule().getSourceFileName().empty()) {
430 std::string Path = getModule().getSourceFileName();
431 // Check if a path substitution is needed from the MacroPrefixMap.
432 for (const auto &Entry : LangOpts.MacroPrefixMap)
433 if (Path.rfind(Entry.first, 0) != std::string::npos) {
434 Path = Entry.second + Path.substr(Entry.first.size());
435 break;
437 ModuleNameHash = llvm::getUniqueInternalLinkagePostfix(Path);
441 CodeGenModule::~CodeGenModule() {}
443 void CodeGenModule::createObjCRuntime() {
444 // This is just isGNUFamily(), but we want to force implementors of
445 // new ABIs to decide how best to do this.
446 switch (LangOpts.ObjCRuntime.getKind()) {
447 case ObjCRuntime::GNUstep:
448 case ObjCRuntime::GCC:
449 case ObjCRuntime::ObjFW:
450 ObjCRuntime.reset(CreateGNUObjCRuntime(*this));
451 return;
453 case ObjCRuntime::FragileMacOSX:
454 case ObjCRuntime::MacOSX:
455 case ObjCRuntime::iOS:
456 case ObjCRuntime::WatchOS:
457 ObjCRuntime.reset(CreateMacObjCRuntime(*this));
458 return;
460 llvm_unreachable("bad runtime kind");
463 void CodeGenModule::createOpenCLRuntime() {
464 OpenCLRuntime.reset(new CGOpenCLRuntime(*this));
467 void CodeGenModule::createOpenMPRuntime() {
468 // Select a specialized code generation class based on the target, if any.
469 // If it does not exist use the default implementation.
470 switch (getTriple().getArch()) {
471 case llvm::Triple::nvptx:
472 case llvm::Triple::nvptx64:
473 case llvm::Triple::amdgcn:
474 assert(getLangOpts().OpenMPIsTargetDevice &&
475 "OpenMP AMDGPU/NVPTX is only prepared to deal with device code.");
476 OpenMPRuntime.reset(new CGOpenMPRuntimeGPU(*this));
477 break;
478 default:
479 if (LangOpts.OpenMPSimd)
480 OpenMPRuntime.reset(new CGOpenMPSIMDRuntime(*this));
481 else
482 OpenMPRuntime.reset(new CGOpenMPRuntime(*this));
483 break;
487 void CodeGenModule::createCUDARuntime() {
488 CUDARuntime.reset(CreateNVCUDARuntime(*this));
491 void CodeGenModule::createHLSLRuntime() {
492 HLSLRuntime.reset(new CGHLSLRuntime(*this));
495 void CodeGenModule::addReplacement(StringRef Name, llvm::Constant *C) {
496 Replacements[Name] = C;
499 void CodeGenModule::applyReplacements() {
500 for (auto &I : Replacements) {
501 StringRef MangledName = I.first;
502 llvm::Constant *Replacement = I.second;
503 llvm::GlobalValue *Entry = GetGlobalValue(MangledName);
504 if (!Entry)
505 continue;
506 auto *OldF = cast<llvm::Function>(Entry);
507 auto *NewF = dyn_cast<llvm::Function>(Replacement);
508 if (!NewF) {
509 if (auto *Alias = dyn_cast<llvm::GlobalAlias>(Replacement)) {
510 NewF = dyn_cast<llvm::Function>(Alias->getAliasee());
511 } else {
512 auto *CE = cast<llvm::ConstantExpr>(Replacement);
513 assert(CE->getOpcode() == llvm::Instruction::BitCast ||
514 CE->getOpcode() == llvm::Instruction::GetElementPtr);
515 NewF = dyn_cast<llvm::Function>(CE->getOperand(0));
519 // Replace old with new, but keep the old order.
520 OldF->replaceAllUsesWith(Replacement);
521 if (NewF) {
522 NewF->removeFromParent();
523 OldF->getParent()->getFunctionList().insertAfter(OldF->getIterator(),
524 NewF);
526 OldF->eraseFromParent();
530 void CodeGenModule::addGlobalValReplacement(llvm::GlobalValue *GV, llvm::Constant *C) {
531 GlobalValReplacements.push_back(std::make_pair(GV, C));
534 void CodeGenModule::applyGlobalValReplacements() {
535 for (auto &I : GlobalValReplacements) {
536 llvm::GlobalValue *GV = I.first;
537 llvm::Constant *C = I.second;
539 GV->replaceAllUsesWith(C);
540 GV->eraseFromParent();
544 // This is only used in aliases that we created and we know they have a
545 // linear structure.
546 static const llvm::GlobalValue *getAliasedGlobal(const llvm::GlobalValue *GV) {
547 const llvm::Constant *C;
548 if (auto *GA = dyn_cast<llvm::GlobalAlias>(GV))
549 C = GA->getAliasee();
550 else if (auto *GI = dyn_cast<llvm::GlobalIFunc>(GV))
551 C = GI->getResolver();
552 else
553 return GV;
555 const auto *AliaseeGV = dyn_cast<llvm::GlobalValue>(C->stripPointerCasts());
556 if (!AliaseeGV)
557 return nullptr;
559 const llvm::GlobalValue *FinalGV = AliaseeGV->getAliaseeObject();
560 if (FinalGV == GV)
561 return nullptr;
563 return FinalGV;
566 static bool checkAliasedGlobal(
567 const ASTContext &Context, DiagnosticsEngine &Diags, SourceLocation Location,
568 bool IsIFunc, const llvm::GlobalValue *Alias, const llvm::GlobalValue *&GV,
569 const llvm::MapVector<GlobalDecl, StringRef> &MangledDeclNames,
570 SourceRange AliasRange) {
571 GV = getAliasedGlobal(Alias);
572 if (!GV) {
573 Diags.Report(Location, diag::err_cyclic_alias) << IsIFunc;
574 return false;
577 if (GV->hasCommonLinkage()) {
578 const llvm::Triple &Triple = Context.getTargetInfo().getTriple();
579 if (Triple.getObjectFormat() == llvm::Triple::XCOFF) {
580 Diags.Report(Location, diag::err_alias_to_common);
581 return false;
585 if (GV->isDeclaration()) {
586 Diags.Report(Location, diag::err_alias_to_undefined) << IsIFunc << IsIFunc;
587 Diags.Report(Location, diag::note_alias_requires_mangled_name)
588 << IsIFunc << IsIFunc;
589 // Provide a note if the given function is not found and exists as a
590 // mangled name.
591 for (const auto &[Decl, Name] : MangledDeclNames) {
592 if (const auto *ND = dyn_cast<NamedDecl>(Decl.getDecl())) {
593 if (ND->getName() == GV->getName()) {
594 Diags.Report(Location, diag::note_alias_mangled_name_alternative)
595 << Name
596 << FixItHint::CreateReplacement(
597 AliasRange,
598 (Twine(IsIFunc ? "ifunc" : "alias") + "(\"" + Name + "\")")
599 .str());
603 return false;
606 if (IsIFunc) {
607 // Check resolver function type.
608 const auto *F = dyn_cast<llvm::Function>(GV);
609 if (!F) {
610 Diags.Report(Location, diag::err_alias_to_undefined)
611 << IsIFunc << IsIFunc;
612 return false;
615 llvm::FunctionType *FTy = F->getFunctionType();
616 if (!FTy->getReturnType()->isPointerTy()) {
617 Diags.Report(Location, diag::err_ifunc_resolver_return);
618 return false;
622 return true;
625 void CodeGenModule::checkAliases() {
626 // Check if the constructed aliases are well formed. It is really unfortunate
627 // that we have to do this in CodeGen, but we only construct mangled names
628 // and aliases during codegen.
629 bool Error = false;
630 DiagnosticsEngine &Diags = getDiags();
631 for (const GlobalDecl &GD : Aliases) {
632 const auto *D = cast<ValueDecl>(GD.getDecl());
633 SourceLocation Location;
634 SourceRange Range;
635 bool IsIFunc = D->hasAttr<IFuncAttr>();
636 if (const Attr *A = D->getDefiningAttr()) {
637 Location = A->getLocation();
638 Range = A->getRange();
639 } else
640 llvm_unreachable("Not an alias or ifunc?");
642 StringRef MangledName = getMangledName(GD);
643 llvm::GlobalValue *Alias = GetGlobalValue(MangledName);
644 const llvm::GlobalValue *GV = nullptr;
645 if (!checkAliasedGlobal(getContext(), Diags, Location, IsIFunc, Alias, GV,
646 MangledDeclNames, Range)) {
647 Error = true;
648 continue;
651 llvm::Constant *Aliasee =
652 IsIFunc ? cast<llvm::GlobalIFunc>(Alias)->getResolver()
653 : cast<llvm::GlobalAlias>(Alias)->getAliasee();
655 llvm::GlobalValue *AliaseeGV;
656 if (auto CE = dyn_cast<llvm::ConstantExpr>(Aliasee))
657 AliaseeGV = cast<llvm::GlobalValue>(CE->getOperand(0));
658 else
659 AliaseeGV = cast<llvm::GlobalValue>(Aliasee);
661 if (const SectionAttr *SA = D->getAttr<SectionAttr>()) {
662 StringRef AliasSection = SA->getName();
663 if (AliasSection != AliaseeGV->getSection())
664 Diags.Report(SA->getLocation(), diag::warn_alias_with_section)
665 << AliasSection << IsIFunc << IsIFunc;
668 // We have to handle alias to weak aliases in here. LLVM itself disallows
669 // this since the object semantics would not match the IL one. For
670 // compatibility with gcc we implement it by just pointing the alias
671 // to its aliasee's aliasee. We also warn, since the user is probably
672 // expecting the link to be weak.
673 if (auto *GA = dyn_cast<llvm::GlobalAlias>(AliaseeGV)) {
674 if (GA->isInterposable()) {
675 Diags.Report(Location, diag::warn_alias_to_weak_alias)
676 << GV->getName() << GA->getName() << IsIFunc;
677 Aliasee = llvm::ConstantExpr::getPointerBitCastOrAddrSpaceCast(
678 GA->getAliasee(), Alias->getType());
680 if (IsIFunc)
681 cast<llvm::GlobalIFunc>(Alias)->setResolver(Aliasee);
682 else
683 cast<llvm::GlobalAlias>(Alias)->setAliasee(Aliasee);
687 if (!Error)
688 return;
690 for (const GlobalDecl &GD : Aliases) {
691 StringRef MangledName = getMangledName(GD);
692 llvm::GlobalValue *Alias = GetGlobalValue(MangledName);
693 Alias->replaceAllUsesWith(llvm::UndefValue::get(Alias->getType()));
694 Alias->eraseFromParent();
698 void CodeGenModule::clear() {
699 DeferredDeclsToEmit.clear();
700 EmittedDeferredDecls.clear();
701 if (OpenMPRuntime)
702 OpenMPRuntime->clear();
705 void InstrProfStats::reportDiagnostics(DiagnosticsEngine &Diags,
706 StringRef MainFile) {
707 if (!hasDiagnostics())
708 return;
709 if (VisitedInMainFile > 0 && VisitedInMainFile == MissingInMainFile) {
710 if (MainFile.empty())
711 MainFile = "<stdin>";
712 Diags.Report(diag::warn_profile_data_unprofiled) << MainFile;
713 } else {
714 if (Mismatched > 0)
715 Diags.Report(diag::warn_profile_data_out_of_date) << Visited << Mismatched;
717 if (Missing > 0)
718 Diags.Report(diag::warn_profile_data_missing) << Visited << Missing;
722 static void setVisibilityFromDLLStorageClass(const clang::LangOptions &LO,
723 llvm::Module &M) {
724 if (!LO.VisibilityFromDLLStorageClass)
725 return;
727 llvm::GlobalValue::VisibilityTypes DLLExportVisibility =
728 CodeGenModule::GetLLVMVisibility(LO.getDLLExportVisibility());
729 llvm::GlobalValue::VisibilityTypes NoDLLStorageClassVisibility =
730 CodeGenModule::GetLLVMVisibility(LO.getNoDLLStorageClassVisibility());
731 llvm::GlobalValue::VisibilityTypes ExternDeclDLLImportVisibility =
732 CodeGenModule::GetLLVMVisibility(LO.getExternDeclDLLImportVisibility());
733 llvm::GlobalValue::VisibilityTypes ExternDeclNoDLLStorageClassVisibility =
734 CodeGenModule::GetLLVMVisibility(
735 LO.getExternDeclNoDLLStorageClassVisibility());
737 for (llvm::GlobalValue &GV : M.global_values()) {
738 if (GV.hasAppendingLinkage() || GV.hasLocalLinkage())
739 continue;
741 // Reset DSO locality before setting the visibility. This removes
742 // any effects that visibility options and annotations may have
743 // had on the DSO locality. Setting the visibility will implicitly set
744 // appropriate globals to DSO Local; however, this will be pessimistic
745 // w.r.t. to the normal compiler IRGen.
746 GV.setDSOLocal(false);
748 if (GV.isDeclarationForLinker()) {
749 GV.setVisibility(GV.getDLLStorageClass() ==
750 llvm::GlobalValue::DLLImportStorageClass
751 ? ExternDeclDLLImportVisibility
752 : ExternDeclNoDLLStorageClassVisibility);
753 } else {
754 GV.setVisibility(GV.getDLLStorageClass() ==
755 llvm::GlobalValue::DLLExportStorageClass
756 ? DLLExportVisibility
757 : NoDLLStorageClassVisibility);
760 GV.setDLLStorageClass(llvm::GlobalValue::DefaultStorageClass);
764 static bool isStackProtectorOn(const LangOptions &LangOpts,
765 const llvm::Triple &Triple,
766 clang::LangOptions::StackProtectorMode Mode) {
767 if (Triple.isAMDGPU() || Triple.isNVPTX())
768 return false;
769 return LangOpts.getStackProtector() == Mode;
772 void CodeGenModule::Release() {
773 Module *Primary = getContext().getCurrentNamedModule();
774 if (CXX20ModuleInits && Primary && !Primary->isHeaderLikeModule())
775 EmitModuleInitializers(Primary);
776 EmitDeferred();
777 DeferredDecls.insert(EmittedDeferredDecls.begin(),
778 EmittedDeferredDecls.end());
779 EmittedDeferredDecls.clear();
780 EmitVTablesOpportunistically();
781 applyGlobalValReplacements();
782 applyReplacements();
783 emitMultiVersionFunctions();
785 if (Context.getLangOpts().IncrementalExtensions &&
786 GlobalTopLevelStmtBlockInFlight.first) {
787 const TopLevelStmtDecl *TLSD = GlobalTopLevelStmtBlockInFlight.second;
788 GlobalTopLevelStmtBlockInFlight.first->FinishFunction(TLSD->getEndLoc());
789 GlobalTopLevelStmtBlockInFlight = {nullptr, nullptr};
792 // Module implementations are initialized the same way as a regular TU that
793 // imports one or more modules.
794 if (CXX20ModuleInits && Primary && Primary->isInterfaceOrPartition())
795 EmitCXXModuleInitFunc(Primary);
796 else
797 EmitCXXGlobalInitFunc();
798 EmitCXXGlobalCleanUpFunc();
799 registerGlobalDtorsWithAtExit();
800 EmitCXXThreadLocalInitFunc();
801 if (ObjCRuntime)
802 if (llvm::Function *ObjCInitFunction = ObjCRuntime->ModuleInitFunction())
803 AddGlobalCtor(ObjCInitFunction);
804 if (Context.getLangOpts().CUDA && CUDARuntime) {
805 if (llvm::Function *CudaCtorFunction = CUDARuntime->finalizeModule())
806 AddGlobalCtor(CudaCtorFunction);
808 if (OpenMPRuntime) {
809 if (llvm::Function *OpenMPRequiresDirectiveRegFun =
810 OpenMPRuntime->emitRequiresDirectiveRegFun()) {
811 AddGlobalCtor(OpenMPRequiresDirectiveRegFun, 0);
813 OpenMPRuntime->createOffloadEntriesAndInfoMetadata();
814 OpenMPRuntime->clear();
816 if (PGOReader) {
817 getModule().setProfileSummary(
818 PGOReader->getSummary(/* UseCS */ false).getMD(VMContext),
819 llvm::ProfileSummary::PSK_Instr);
820 if (PGOStats.hasDiagnostics())
821 PGOStats.reportDiagnostics(getDiags(), getCodeGenOpts().MainFileName);
823 llvm::stable_sort(GlobalCtors, [](const Structor &L, const Structor &R) {
824 return L.LexOrder < R.LexOrder;
826 EmitCtorList(GlobalCtors, "llvm.global_ctors");
827 EmitCtorList(GlobalDtors, "llvm.global_dtors");
828 EmitGlobalAnnotations();
829 EmitStaticExternCAliases();
830 checkAliases();
831 EmitDeferredUnusedCoverageMappings();
832 CodeGenPGO(*this).setValueProfilingFlag(getModule());
833 if (CoverageMapping)
834 CoverageMapping->emit();
835 if (CodeGenOpts.SanitizeCfiCrossDso) {
836 CodeGenFunction(*this).EmitCfiCheckFail();
837 CodeGenFunction(*this).EmitCfiCheckStub();
839 if (LangOpts.Sanitize.has(SanitizerKind::KCFI))
840 finalizeKCFITypes();
841 emitAtAvailableLinkGuard();
842 if (Context.getTargetInfo().getTriple().isWasm())
843 EmitMainVoidAlias();
845 if (getTriple().isAMDGPU()) {
846 // Emit amdgpu_code_object_version module flag, which is code object version
847 // times 100.
848 if (getTarget().getTargetOpts().CodeObjectVersion !=
849 TargetOptions::COV_None) {
850 getModule().addModuleFlag(llvm::Module::Error,
851 "amdgpu_code_object_version",
852 getTarget().getTargetOpts().CodeObjectVersion);
855 // Currently, "-mprintf-kind" option is only supported for HIP
856 if (LangOpts.HIP) {
857 auto *MDStr = llvm::MDString::get(
858 getLLVMContext(), (getTarget().getTargetOpts().AMDGPUPrintfKindVal ==
859 TargetOptions::AMDGPUPrintfKind::Hostcall)
860 ? "hostcall"
861 : "buffered");
862 getModule().addModuleFlag(llvm::Module::Error, "amdgpu_printf_kind",
863 MDStr);
867 // Emit a global array containing all external kernels or device variables
868 // used by host functions and mark it as used for CUDA/HIP. This is necessary
869 // to get kernels or device variables in archives linked in even if these
870 // kernels or device variables are only used in host functions.
871 if (!Context.CUDAExternalDeviceDeclODRUsedByHost.empty()) {
872 SmallVector<llvm::Constant *, 8> UsedArray;
873 for (auto D : Context.CUDAExternalDeviceDeclODRUsedByHost) {
874 GlobalDecl GD;
875 if (auto *FD = dyn_cast<FunctionDecl>(D))
876 GD = GlobalDecl(FD, KernelReferenceKind::Kernel);
877 else
878 GD = GlobalDecl(D);
879 UsedArray.push_back(llvm::ConstantExpr::getPointerBitCastOrAddrSpaceCast(
880 GetAddrOfGlobal(GD), Int8PtrTy));
883 llvm::ArrayType *ATy = llvm::ArrayType::get(Int8PtrTy, UsedArray.size());
885 auto *GV = new llvm::GlobalVariable(
886 getModule(), ATy, false, llvm::GlobalValue::InternalLinkage,
887 llvm::ConstantArray::get(ATy, UsedArray), "__clang_gpu_used_external");
888 addCompilerUsedGlobal(GV);
891 emitLLVMUsed();
892 if (SanStats)
893 SanStats->finish();
895 if (CodeGenOpts.Autolink &&
896 (Context.getLangOpts().Modules || !LinkerOptionsMetadata.empty())) {
897 EmitModuleLinkOptions();
900 // On ELF we pass the dependent library specifiers directly to the linker
901 // without manipulating them. This is in contrast to other platforms where
902 // they are mapped to a specific linker option by the compiler. This
903 // difference is a result of the greater variety of ELF linkers and the fact
904 // that ELF linkers tend to handle libraries in a more complicated fashion
905 // than on other platforms. This forces us to defer handling the dependent
906 // libs to the linker.
908 // CUDA/HIP device and host libraries are different. Currently there is no
909 // way to differentiate dependent libraries for host or device. Existing
910 // usage of #pragma comment(lib, *) is intended for host libraries on
911 // Windows. Therefore emit llvm.dependent-libraries only for host.
912 if (!ELFDependentLibraries.empty() && !Context.getLangOpts().CUDAIsDevice) {
913 auto *NMD = getModule().getOrInsertNamedMetadata("llvm.dependent-libraries");
914 for (auto *MD : ELFDependentLibraries)
915 NMD->addOperand(MD);
918 // Record mregparm value now so it is visible through rest of codegen.
919 if (Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86)
920 getModule().addModuleFlag(llvm::Module::Error, "NumRegisterParameters",
921 CodeGenOpts.NumRegisterParameters);
923 if (CodeGenOpts.DwarfVersion) {
924 getModule().addModuleFlag(llvm::Module::Max, "Dwarf Version",
925 CodeGenOpts.DwarfVersion);
928 if (CodeGenOpts.Dwarf64)
929 getModule().addModuleFlag(llvm::Module::Max, "DWARF64", 1);
931 if (Context.getLangOpts().SemanticInterposition)
932 // Require various optimization to respect semantic interposition.
933 getModule().setSemanticInterposition(true);
935 if (CodeGenOpts.EmitCodeView) {
936 // Indicate that we want CodeView in the metadata.
937 getModule().addModuleFlag(llvm::Module::Warning, "CodeView", 1);
939 if (CodeGenOpts.CodeViewGHash) {
940 getModule().addModuleFlag(llvm::Module::Warning, "CodeViewGHash", 1);
942 if (CodeGenOpts.ControlFlowGuard) {
943 // Function ID tables and checks for Control Flow Guard (cfguard=2).
944 getModule().addModuleFlag(llvm::Module::Warning, "cfguard", 2);
945 } else if (CodeGenOpts.ControlFlowGuardNoChecks) {
946 // Function ID tables for Control Flow Guard (cfguard=1).
947 getModule().addModuleFlag(llvm::Module::Warning, "cfguard", 1);
949 if (CodeGenOpts.EHContGuard) {
950 // Function ID tables for EH Continuation Guard.
951 getModule().addModuleFlag(llvm::Module::Warning, "ehcontguard", 1);
953 if (Context.getLangOpts().Kernel) {
954 // Note if we are compiling with /kernel.
955 getModule().addModuleFlag(llvm::Module::Warning, "ms-kernel", 1);
957 if (CodeGenOpts.OptimizationLevel > 0 && CodeGenOpts.StrictVTablePointers) {
958 // We don't support LTO with 2 with different StrictVTablePointers
959 // FIXME: we could support it by stripping all the information introduced
960 // by StrictVTablePointers.
962 getModule().addModuleFlag(llvm::Module::Error, "StrictVTablePointers",1);
964 llvm::Metadata *Ops[2] = {
965 llvm::MDString::get(VMContext, "StrictVTablePointers"),
966 llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(
967 llvm::Type::getInt32Ty(VMContext), 1))};
969 getModule().addModuleFlag(llvm::Module::Require,
970 "StrictVTablePointersRequirement",
971 llvm::MDNode::get(VMContext, Ops));
973 if (getModuleDebugInfo())
974 // We support a single version in the linked module. The LLVM
975 // parser will drop debug info with a different version number
976 // (and warn about it, too).
977 getModule().addModuleFlag(llvm::Module::Warning, "Debug Info Version",
978 llvm::DEBUG_METADATA_VERSION);
980 // We need to record the widths of enums and wchar_t, so that we can generate
981 // the correct build attributes in the ARM backend. wchar_size is also used by
982 // TargetLibraryInfo.
983 uint64_t WCharWidth =
984 Context.getTypeSizeInChars(Context.getWideCharType()).getQuantity();
985 getModule().addModuleFlag(llvm::Module::Error, "wchar_size", WCharWidth);
987 llvm::Triple::ArchType Arch = Context.getTargetInfo().getTriple().getArch();
988 if ( Arch == llvm::Triple::arm
989 || Arch == llvm::Triple::armeb
990 || Arch == llvm::Triple::thumb
991 || Arch == llvm::Triple::thumbeb) {
992 // The minimum width of an enum in bytes
993 uint64_t EnumWidth = Context.getLangOpts().ShortEnums ? 1 : 4;
994 getModule().addModuleFlag(llvm::Module::Error, "min_enum_size", EnumWidth);
997 if (Arch == llvm::Triple::riscv32 || Arch == llvm::Triple::riscv64) {
998 StringRef ABIStr = Target.getABI();
999 llvm::LLVMContext &Ctx = TheModule.getContext();
1000 getModule().addModuleFlag(llvm::Module::Error, "target-abi",
1001 llvm::MDString::get(Ctx, ABIStr));
1004 if (CodeGenOpts.SanitizeCfiCrossDso) {
1005 // Indicate that we want cross-DSO control flow integrity checks.
1006 getModule().addModuleFlag(llvm::Module::Override, "Cross-DSO CFI", 1);
1009 if (CodeGenOpts.WholeProgramVTables) {
1010 // Indicate whether VFE was enabled for this module, so that the
1011 // vcall_visibility metadata added under whole program vtables is handled
1012 // appropriately in the optimizer.
1013 getModule().addModuleFlag(llvm::Module::Error, "Virtual Function Elim",
1014 CodeGenOpts.VirtualFunctionElimination);
1017 if (LangOpts.Sanitize.has(SanitizerKind::CFIICall)) {
1018 getModule().addModuleFlag(llvm::Module::Override,
1019 "CFI Canonical Jump Tables",
1020 CodeGenOpts.SanitizeCfiCanonicalJumpTables);
1023 if (LangOpts.Sanitize.has(SanitizerKind::KCFI)) {
1024 getModule().addModuleFlag(llvm::Module::Override, "kcfi", 1);
1025 // KCFI assumes patchable-function-prefix is the same for all indirectly
1026 // called functions. Store the expected offset for code generation.
1027 if (CodeGenOpts.PatchableFunctionEntryOffset)
1028 getModule().addModuleFlag(llvm::Module::Override, "kcfi-offset",
1029 CodeGenOpts.PatchableFunctionEntryOffset);
1032 if (CodeGenOpts.CFProtectionReturn &&
1033 Target.checkCFProtectionReturnSupported(getDiags())) {
1034 // Indicate that we want to instrument return control flow protection.
1035 getModule().addModuleFlag(llvm::Module::Min, "cf-protection-return",
1039 if (CodeGenOpts.CFProtectionBranch &&
1040 Target.checkCFProtectionBranchSupported(getDiags())) {
1041 // Indicate that we want to instrument branch control flow protection.
1042 getModule().addModuleFlag(llvm::Module::Min, "cf-protection-branch",
1046 if (CodeGenOpts.FunctionReturnThunks)
1047 getModule().addModuleFlag(llvm::Module::Override, "function_return_thunk_extern", 1);
1049 if (CodeGenOpts.IndirectBranchCSPrefix)
1050 getModule().addModuleFlag(llvm::Module::Override, "indirect_branch_cs_prefix", 1);
1052 // Add module metadata for return address signing (ignoring
1053 // non-leaf/all) and stack tagging. These are actually turned on by function
1054 // attributes, but we use module metadata to emit build attributes. This is
1055 // needed for LTO, where the function attributes are inside bitcode
1056 // serialised into a global variable by the time build attributes are
1057 // emitted, so we can't access them. LTO objects could be compiled with
1058 // different flags therefore module flags are set to "Min" behavior to achieve
1059 // the same end result of the normal build where e.g BTI is off if any object
1060 // doesn't support it.
1061 if (Context.getTargetInfo().hasFeature("ptrauth") &&
1062 LangOpts.getSignReturnAddressScope() !=
1063 LangOptions::SignReturnAddressScopeKind::None)
1064 getModule().addModuleFlag(llvm::Module::Override,
1065 "sign-return-address-buildattr", 1);
1066 if (LangOpts.Sanitize.has(SanitizerKind::MemtagStack))
1067 getModule().addModuleFlag(llvm::Module::Override,
1068 "tag-stack-memory-buildattr", 1);
1070 if (Arch == llvm::Triple::thumb || Arch == llvm::Triple::thumbeb ||
1071 Arch == llvm::Triple::arm || Arch == llvm::Triple::armeb ||
1072 Arch == llvm::Triple::aarch64 || Arch == llvm::Triple::aarch64_32 ||
1073 Arch == llvm::Triple::aarch64_be) {
1074 if (LangOpts.BranchTargetEnforcement)
1075 getModule().addModuleFlag(llvm::Module::Min, "branch-target-enforcement",
1077 if (LangOpts.hasSignReturnAddress())
1078 getModule().addModuleFlag(llvm::Module::Min, "sign-return-address", 1);
1079 if (LangOpts.isSignReturnAddressScopeAll())
1080 getModule().addModuleFlag(llvm::Module::Min, "sign-return-address-all",
1082 if (!LangOpts.isSignReturnAddressWithAKey())
1083 getModule().addModuleFlag(llvm::Module::Min,
1084 "sign-return-address-with-bkey", 1);
1087 if (!CodeGenOpts.MemoryProfileOutput.empty()) {
1088 llvm::LLVMContext &Ctx = TheModule.getContext();
1089 getModule().addModuleFlag(
1090 llvm::Module::Error, "MemProfProfileFilename",
1091 llvm::MDString::get(Ctx, CodeGenOpts.MemoryProfileOutput));
1094 if (LangOpts.CUDAIsDevice && getTriple().isNVPTX()) {
1095 // Indicate whether __nvvm_reflect should be configured to flush denormal
1096 // floating point values to 0. (This corresponds to its "__CUDA_FTZ"
1097 // property.)
1098 getModule().addModuleFlag(llvm::Module::Override, "nvvm-reflect-ftz",
1099 CodeGenOpts.FP32DenormalMode.Output !=
1100 llvm::DenormalMode::IEEE);
1103 if (LangOpts.EHAsynch)
1104 getModule().addModuleFlag(llvm::Module::Warning, "eh-asynch", 1);
1106 // Indicate whether this Module was compiled with -fopenmp
1107 if (getLangOpts().OpenMP && !getLangOpts().OpenMPSimd)
1108 getModule().addModuleFlag(llvm::Module::Max, "openmp", LangOpts.OpenMP);
1109 if (getLangOpts().OpenMPIsTargetDevice)
1110 getModule().addModuleFlag(llvm::Module::Max, "openmp-device",
1111 LangOpts.OpenMP);
1113 // Emit OpenCL specific module metadata: OpenCL/SPIR version.
1114 if (LangOpts.OpenCL || (LangOpts.CUDAIsDevice && getTriple().isSPIRV())) {
1115 EmitOpenCLMetadata();
1116 // Emit SPIR version.
1117 if (getTriple().isSPIR()) {
1118 // SPIR v2.0 s2.12 - The SPIR version used by the module is stored in the
1119 // opencl.spir.version named metadata.
1120 // C++ for OpenCL has a distinct mapping for version compatibility with
1121 // OpenCL.
1122 auto Version = LangOpts.getOpenCLCompatibleVersion();
1123 llvm::Metadata *SPIRVerElts[] = {
1124 llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(
1125 Int32Ty, Version / 100)),
1126 llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(
1127 Int32Ty, (Version / 100 > 1) ? 0 : 2))};
1128 llvm::NamedMDNode *SPIRVerMD =
1129 TheModule.getOrInsertNamedMetadata("opencl.spir.version");
1130 llvm::LLVMContext &Ctx = TheModule.getContext();
1131 SPIRVerMD->addOperand(llvm::MDNode::get(Ctx, SPIRVerElts));
1135 // HLSL related end of code gen work items.
1136 if (LangOpts.HLSL)
1137 getHLSLRuntime().finishCodeGen();
1139 if (uint32_t PLevel = Context.getLangOpts().PICLevel) {
1140 assert(PLevel < 3 && "Invalid PIC Level");
1141 getModule().setPICLevel(static_cast<llvm::PICLevel::Level>(PLevel));
1142 if (Context.getLangOpts().PIE)
1143 getModule().setPIELevel(static_cast<llvm::PIELevel::Level>(PLevel));
1146 if (getCodeGenOpts().CodeModel.size() > 0) {
1147 unsigned CM = llvm::StringSwitch<unsigned>(getCodeGenOpts().CodeModel)
1148 .Case("tiny", llvm::CodeModel::Tiny)
1149 .Case("small", llvm::CodeModel::Small)
1150 .Case("kernel", llvm::CodeModel::Kernel)
1151 .Case("medium", llvm::CodeModel::Medium)
1152 .Case("large", llvm::CodeModel::Large)
1153 .Default(~0u);
1154 if (CM != ~0u) {
1155 llvm::CodeModel::Model codeModel = static_cast<llvm::CodeModel::Model>(CM);
1156 getModule().setCodeModel(codeModel);
1158 if (CM == llvm::CodeModel::Medium &&
1159 Context.getTargetInfo().getTriple().getArch() ==
1160 llvm::Triple::x86_64) {
1161 getModule().setLargeDataThreshold(getCodeGenOpts().LargeDataThreshold);
1166 if (CodeGenOpts.NoPLT)
1167 getModule().setRtLibUseGOT();
1168 if (getTriple().isOSBinFormatELF() &&
1169 CodeGenOpts.DirectAccessExternalData !=
1170 getModule().getDirectAccessExternalData()) {
1171 getModule().setDirectAccessExternalData(
1172 CodeGenOpts.DirectAccessExternalData);
1174 if (CodeGenOpts.UnwindTables)
1175 getModule().setUwtable(llvm::UWTableKind(CodeGenOpts.UnwindTables));
1177 switch (CodeGenOpts.getFramePointer()) {
1178 case CodeGenOptions::FramePointerKind::None:
1179 // 0 ("none") is the default.
1180 break;
1181 case CodeGenOptions::FramePointerKind::NonLeaf:
1182 getModule().setFramePointer(llvm::FramePointerKind::NonLeaf);
1183 break;
1184 case CodeGenOptions::FramePointerKind::All:
1185 getModule().setFramePointer(llvm::FramePointerKind::All);
1186 break;
1189 SimplifyPersonality();
1191 if (getCodeGenOpts().EmitDeclMetadata)
1192 EmitDeclMetadata();
1194 if (getCodeGenOpts().CoverageNotesFile.size() ||
1195 getCodeGenOpts().CoverageDataFile.size())
1196 EmitCoverageFile();
1198 if (CGDebugInfo *DI = getModuleDebugInfo())
1199 DI->finalize();
1201 if (getCodeGenOpts().EmitVersionIdentMetadata)
1202 EmitVersionIdentMetadata();
1204 if (!getCodeGenOpts().RecordCommandLine.empty())
1205 EmitCommandLineMetadata();
1207 if (!getCodeGenOpts().StackProtectorGuard.empty())
1208 getModule().setStackProtectorGuard(getCodeGenOpts().StackProtectorGuard);
1209 if (!getCodeGenOpts().StackProtectorGuardReg.empty())
1210 getModule().setStackProtectorGuardReg(
1211 getCodeGenOpts().StackProtectorGuardReg);
1212 if (!getCodeGenOpts().StackProtectorGuardSymbol.empty())
1213 getModule().setStackProtectorGuardSymbol(
1214 getCodeGenOpts().StackProtectorGuardSymbol);
1215 if (getCodeGenOpts().StackProtectorGuardOffset != INT_MAX)
1216 getModule().setStackProtectorGuardOffset(
1217 getCodeGenOpts().StackProtectorGuardOffset);
1218 if (getCodeGenOpts().StackAlignment)
1219 getModule().setOverrideStackAlignment(getCodeGenOpts().StackAlignment);
1220 if (getCodeGenOpts().SkipRaxSetup)
1221 getModule().addModuleFlag(llvm::Module::Override, "SkipRaxSetup", 1);
1222 if (getLangOpts().RegCall4)
1223 getModule().addModuleFlag(llvm::Module::Override, "RegCallv4", 1);
1225 if (getContext().getTargetInfo().getMaxTLSAlign())
1226 getModule().addModuleFlag(llvm::Module::Error, "MaxTLSAlign",
1227 getContext().getTargetInfo().getMaxTLSAlign());
1229 getTargetCodeGenInfo().emitTargetGlobals(*this);
1231 getTargetCodeGenInfo().emitTargetMetadata(*this, MangledDeclNames);
1233 EmitBackendOptionsMetadata(getCodeGenOpts());
1235 // If there is device offloading code embed it in the host now.
1236 EmbedObject(&getModule(), CodeGenOpts, getDiags());
1238 // Set visibility from DLL storage class
1239 // We do this at the end of LLVM IR generation; after any operation
1240 // that might affect the DLL storage class or the visibility, and
1241 // before anything that might act on these.
1242 setVisibilityFromDLLStorageClass(LangOpts, getModule());
1245 void CodeGenModule::EmitOpenCLMetadata() {
1246 // SPIR v2.0 s2.13 - The OpenCL version used by the module is stored in the
1247 // opencl.ocl.version named metadata node.
1248 // C++ for OpenCL has a distinct mapping for versions compatibile with OpenCL.
1249 auto Version = LangOpts.getOpenCLCompatibleVersion();
1250 llvm::Metadata *OCLVerElts[] = {
1251 llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(
1252 Int32Ty, Version / 100)),
1253 llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(
1254 Int32Ty, (Version % 100) / 10))};
1255 llvm::NamedMDNode *OCLVerMD =
1256 TheModule.getOrInsertNamedMetadata("opencl.ocl.version");
1257 llvm::LLVMContext &Ctx = TheModule.getContext();
1258 OCLVerMD->addOperand(llvm::MDNode::get(Ctx, OCLVerElts));
1261 void CodeGenModule::EmitBackendOptionsMetadata(
1262 const CodeGenOptions &CodeGenOpts) {
1263 if (getTriple().isRISCV()) {
1264 getModule().addModuleFlag(llvm::Module::Min, "SmallDataLimit",
1265 CodeGenOpts.SmallDataLimit);
1269 void CodeGenModule::UpdateCompletedType(const TagDecl *TD) {
1270 // Make sure that this type is translated.
1271 Types.UpdateCompletedType(TD);
1274 void CodeGenModule::RefreshTypeCacheForClass(const CXXRecordDecl *RD) {
1275 // Make sure that this type is translated.
1276 Types.RefreshTypeCacheForClass(RD);
1279 llvm::MDNode *CodeGenModule::getTBAATypeInfo(QualType QTy) {
1280 if (!TBAA)
1281 return nullptr;
1282 return TBAA->getTypeInfo(QTy);
1285 TBAAAccessInfo CodeGenModule::getTBAAAccessInfo(QualType AccessType) {
1286 if (!TBAA)
1287 return TBAAAccessInfo();
1288 if (getLangOpts().CUDAIsDevice) {
1289 // As CUDA builtin surface/texture types are replaced, skip generating TBAA
1290 // access info.
1291 if (AccessType->isCUDADeviceBuiltinSurfaceType()) {
1292 if (getTargetCodeGenInfo().getCUDADeviceBuiltinSurfaceDeviceType() !=
1293 nullptr)
1294 return TBAAAccessInfo();
1295 } else if (AccessType->isCUDADeviceBuiltinTextureType()) {
1296 if (getTargetCodeGenInfo().getCUDADeviceBuiltinTextureDeviceType() !=
1297 nullptr)
1298 return TBAAAccessInfo();
1301 return TBAA->getAccessInfo(AccessType);
1304 TBAAAccessInfo
1305 CodeGenModule::getTBAAVTablePtrAccessInfo(llvm::Type *VTablePtrType) {
1306 if (!TBAA)
1307 return TBAAAccessInfo();
1308 return TBAA->getVTablePtrAccessInfo(VTablePtrType);
1311 llvm::MDNode *CodeGenModule::getTBAAStructInfo(QualType QTy) {
1312 if (!TBAA)
1313 return nullptr;
1314 return TBAA->getTBAAStructInfo(QTy);
1317 llvm::MDNode *CodeGenModule::getTBAABaseTypeInfo(QualType QTy) {
1318 if (!TBAA)
1319 return nullptr;
1320 return TBAA->getBaseTypeInfo(QTy);
1323 llvm::MDNode *CodeGenModule::getTBAAAccessTagInfo(TBAAAccessInfo Info) {
1324 if (!TBAA)
1325 return nullptr;
1326 return TBAA->getAccessTagInfo(Info);
1329 TBAAAccessInfo CodeGenModule::mergeTBAAInfoForCast(TBAAAccessInfo SourceInfo,
1330 TBAAAccessInfo TargetInfo) {
1331 if (!TBAA)
1332 return TBAAAccessInfo();
1333 return TBAA->mergeTBAAInfoForCast(SourceInfo, TargetInfo);
1336 TBAAAccessInfo
1337 CodeGenModule::mergeTBAAInfoForConditionalOperator(TBAAAccessInfo InfoA,
1338 TBAAAccessInfo InfoB) {
1339 if (!TBAA)
1340 return TBAAAccessInfo();
1341 return TBAA->mergeTBAAInfoForConditionalOperator(InfoA, InfoB);
1344 TBAAAccessInfo
1345 CodeGenModule::mergeTBAAInfoForMemoryTransfer(TBAAAccessInfo DestInfo,
1346 TBAAAccessInfo SrcInfo) {
1347 if (!TBAA)
1348 return TBAAAccessInfo();
1349 return TBAA->mergeTBAAInfoForConditionalOperator(DestInfo, SrcInfo);
1352 void CodeGenModule::DecorateInstructionWithTBAA(llvm::Instruction *Inst,
1353 TBAAAccessInfo TBAAInfo) {
1354 if (llvm::MDNode *Tag = getTBAAAccessTagInfo(TBAAInfo))
1355 Inst->setMetadata(llvm::LLVMContext::MD_tbaa, Tag);
1358 void CodeGenModule::DecorateInstructionWithInvariantGroup(
1359 llvm::Instruction *I, const CXXRecordDecl *RD) {
1360 I->setMetadata(llvm::LLVMContext::MD_invariant_group,
1361 llvm::MDNode::get(getLLVMContext(), {}));
1364 void CodeGenModule::Error(SourceLocation loc, StringRef message) {
1365 unsigned diagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error, "%0");
1366 getDiags().Report(Context.getFullLoc(loc), diagID) << message;
1369 /// ErrorUnsupported - Print out an error that codegen doesn't support the
1370 /// specified stmt yet.
1371 void CodeGenModule::ErrorUnsupported(const Stmt *S, const char *Type) {
1372 unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error,
1373 "cannot compile this %0 yet");
1374 std::string Msg = Type;
1375 getDiags().Report(Context.getFullLoc(S->getBeginLoc()), DiagID)
1376 << Msg << S->getSourceRange();
1379 /// ErrorUnsupported - Print out an error that codegen doesn't support the
1380 /// specified decl yet.
1381 void CodeGenModule::ErrorUnsupported(const Decl *D, const char *Type) {
1382 unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error,
1383 "cannot compile this %0 yet");
1384 std::string Msg = Type;
1385 getDiags().Report(Context.getFullLoc(D->getLocation()), DiagID) << Msg;
1388 llvm::ConstantInt *CodeGenModule::getSize(CharUnits size) {
1389 return llvm::ConstantInt::get(SizeTy, size.getQuantity());
1392 void CodeGenModule::setGlobalVisibility(llvm::GlobalValue *GV,
1393 const NamedDecl *D) const {
1394 // Internal definitions always have default visibility.
1395 if (GV->hasLocalLinkage()) {
1396 GV->setVisibility(llvm::GlobalValue::DefaultVisibility);
1397 return;
1399 if (!D)
1400 return;
1402 // Set visibility for definitions, and for declarations if requested globally
1403 // or set explicitly.
1404 LinkageInfo LV = D->getLinkageAndVisibility();
1406 // OpenMP declare target variables must be visible to the host so they can
1407 // be registered. We require protected visibility unless the variable has
1408 // the DT_nohost modifier and does not need to be registered.
1409 if (Context.getLangOpts().OpenMP &&
1410 Context.getLangOpts().OpenMPIsTargetDevice && isa<VarDecl>(D) &&
1411 D->hasAttr<OMPDeclareTargetDeclAttr>() &&
1412 D->getAttr<OMPDeclareTargetDeclAttr>()->getDevType() !=
1413 OMPDeclareTargetDeclAttr::DT_NoHost &&
1414 LV.getVisibility() == HiddenVisibility) {
1415 GV->setVisibility(llvm::GlobalValue::ProtectedVisibility);
1416 return;
1419 if (GV->hasDLLExportStorageClass() || GV->hasDLLImportStorageClass()) {
1420 // Reject incompatible dlllstorage and visibility annotations.
1421 if (!LV.isVisibilityExplicit())
1422 return;
1423 if (GV->hasDLLExportStorageClass()) {
1424 if (LV.getVisibility() == HiddenVisibility)
1425 getDiags().Report(D->getLocation(),
1426 diag::err_hidden_visibility_dllexport);
1427 } else if (LV.getVisibility() != DefaultVisibility) {
1428 getDiags().Report(D->getLocation(),
1429 diag::err_non_default_visibility_dllimport);
1431 return;
1434 if (LV.isVisibilityExplicit() || getLangOpts().SetVisibilityForExternDecls ||
1435 !GV->isDeclarationForLinker())
1436 GV->setVisibility(GetLLVMVisibility(LV.getVisibility()));
1439 static bool shouldAssumeDSOLocal(const CodeGenModule &CGM,
1440 llvm::GlobalValue *GV) {
1441 if (GV->hasLocalLinkage())
1442 return true;
1444 if (!GV->hasDefaultVisibility() && !GV->hasExternalWeakLinkage())
1445 return true;
1447 // DLLImport explicitly marks the GV as external.
1448 if (GV->hasDLLImportStorageClass())
1449 return false;
1451 const llvm::Triple &TT = CGM.getTriple();
1452 const auto &CGOpts = CGM.getCodeGenOpts();
1453 if (TT.isWindowsGNUEnvironment()) {
1454 // In MinGW, variables without DLLImport can still be automatically
1455 // imported from a DLL by the linker; don't mark variables that
1456 // potentially could come from another DLL as DSO local.
1458 // With EmulatedTLS, TLS variables can be autoimported from other DLLs
1459 // (and this actually happens in the public interface of libstdc++), so
1460 // such variables can't be marked as DSO local. (Native TLS variables
1461 // can't be dllimported at all, though.)
1462 if (GV->isDeclarationForLinker() && isa<llvm::GlobalVariable>(GV) &&
1463 (!GV->isThreadLocal() || CGM.getCodeGenOpts().EmulatedTLS) &&
1464 CGOpts.AutoImport)
1465 return false;
1468 // On COFF, don't mark 'extern_weak' symbols as DSO local. If these symbols
1469 // remain unresolved in the link, they can be resolved to zero, which is
1470 // outside the current DSO.
1471 if (TT.isOSBinFormatCOFF() && GV->hasExternalWeakLinkage())
1472 return false;
1474 // Every other GV is local on COFF.
1475 // Make an exception for windows OS in the triple: Some firmware builds use
1476 // *-win32-macho triples. This (accidentally?) produced windows relocations
1477 // without GOT tables in older clang versions; Keep this behaviour.
1478 // FIXME: even thread local variables?
1479 if (TT.isOSBinFormatCOFF() || (TT.isOSWindows() && TT.isOSBinFormatMachO()))
1480 return true;
1482 // Only handle COFF and ELF for now.
1483 if (!TT.isOSBinFormatELF())
1484 return false;
1486 // If this is not an executable, don't assume anything is local.
1487 llvm::Reloc::Model RM = CGOpts.RelocationModel;
1488 const auto &LOpts = CGM.getLangOpts();
1489 if (RM != llvm::Reloc::Static && !LOpts.PIE) {
1490 // On ELF, if -fno-semantic-interposition is specified and the target
1491 // supports local aliases, there will be neither CC1
1492 // -fsemantic-interposition nor -fhalf-no-semantic-interposition. Set
1493 // dso_local on the function if using a local alias is preferable (can avoid
1494 // PLT indirection).
1495 if (!(isa<llvm::Function>(GV) && GV->canBenefitFromLocalAlias()))
1496 return false;
1497 return !(CGM.getLangOpts().SemanticInterposition ||
1498 CGM.getLangOpts().HalfNoSemanticInterposition);
1501 // A definition cannot be preempted from an executable.
1502 if (!GV->isDeclarationForLinker())
1503 return true;
1505 // Most PIC code sequences that assume that a symbol is local cannot produce a
1506 // 0 if it turns out the symbol is undefined. While this is ABI and relocation
1507 // depended, it seems worth it to handle it here.
1508 if (RM == llvm::Reloc::PIC_ && GV->hasExternalWeakLinkage())
1509 return false;
1511 // PowerPC64 prefers TOC indirection to avoid copy relocations.
1512 if (TT.isPPC64())
1513 return false;
1515 if (CGOpts.DirectAccessExternalData) {
1516 // If -fdirect-access-external-data (default for -fno-pic), set dso_local
1517 // for non-thread-local variables. If the symbol is not defined in the
1518 // executable, a copy relocation will be needed at link time. dso_local is
1519 // excluded for thread-local variables because they generally don't support
1520 // copy relocations.
1521 if (auto *Var = dyn_cast<llvm::GlobalVariable>(GV))
1522 if (!Var->isThreadLocal())
1523 return true;
1525 // -fno-pic sets dso_local on a function declaration to allow direct
1526 // accesses when taking its address (similar to a data symbol). If the
1527 // function is not defined in the executable, a canonical PLT entry will be
1528 // needed at link time. -fno-direct-access-external-data can avoid the
1529 // canonical PLT entry. We don't generalize this condition to -fpie/-fpic as
1530 // it could just cause trouble without providing perceptible benefits.
1531 if (isa<llvm::Function>(GV) && !CGOpts.NoPLT && RM == llvm::Reloc::Static)
1532 return true;
1535 // If we can use copy relocations we can assume it is local.
1537 // Otherwise don't assume it is local.
1538 return false;
1541 void CodeGenModule::setDSOLocal(llvm::GlobalValue *GV) const {
1542 GV->setDSOLocal(shouldAssumeDSOLocal(*this, GV));
1545 void CodeGenModule::setDLLImportDLLExport(llvm::GlobalValue *GV,
1546 GlobalDecl GD) const {
1547 const auto *D = dyn_cast<NamedDecl>(GD.getDecl());
1548 // C++ destructors have a few C++ ABI specific special cases.
1549 if (const auto *Dtor = dyn_cast_or_null<CXXDestructorDecl>(D)) {
1550 getCXXABI().setCXXDestructorDLLStorage(GV, Dtor, GD.getDtorType());
1551 return;
1553 setDLLImportDLLExport(GV, D);
1556 void CodeGenModule::setDLLImportDLLExport(llvm::GlobalValue *GV,
1557 const NamedDecl *D) const {
1558 if (D && D->isExternallyVisible()) {
1559 if (D->hasAttr<DLLImportAttr>())
1560 GV->setDLLStorageClass(llvm::GlobalVariable::DLLImportStorageClass);
1561 else if ((D->hasAttr<DLLExportAttr>() ||
1562 shouldMapVisibilityToDLLExport(D)) &&
1563 !GV->isDeclarationForLinker())
1564 GV->setDLLStorageClass(llvm::GlobalVariable::DLLExportStorageClass);
1568 void CodeGenModule::setGVProperties(llvm::GlobalValue *GV,
1569 GlobalDecl GD) const {
1570 setDLLImportDLLExport(GV, GD);
1571 setGVPropertiesAux(GV, dyn_cast<NamedDecl>(GD.getDecl()));
1574 void CodeGenModule::setGVProperties(llvm::GlobalValue *GV,
1575 const NamedDecl *D) const {
1576 setDLLImportDLLExport(GV, D);
1577 setGVPropertiesAux(GV, D);
1580 void CodeGenModule::setGVPropertiesAux(llvm::GlobalValue *GV,
1581 const NamedDecl *D) const {
1582 setGlobalVisibility(GV, D);
1583 setDSOLocal(GV);
1584 GV->setPartition(CodeGenOpts.SymbolPartition);
1587 static llvm::GlobalVariable::ThreadLocalMode GetLLVMTLSModel(StringRef S) {
1588 return llvm::StringSwitch<llvm::GlobalVariable::ThreadLocalMode>(S)
1589 .Case("global-dynamic", llvm::GlobalVariable::GeneralDynamicTLSModel)
1590 .Case("local-dynamic", llvm::GlobalVariable::LocalDynamicTLSModel)
1591 .Case("initial-exec", llvm::GlobalVariable::InitialExecTLSModel)
1592 .Case("local-exec", llvm::GlobalVariable::LocalExecTLSModel);
1595 llvm::GlobalVariable::ThreadLocalMode
1596 CodeGenModule::GetDefaultLLVMTLSModel() const {
1597 switch (CodeGenOpts.getDefaultTLSModel()) {
1598 case CodeGenOptions::GeneralDynamicTLSModel:
1599 return llvm::GlobalVariable::GeneralDynamicTLSModel;
1600 case CodeGenOptions::LocalDynamicTLSModel:
1601 return llvm::GlobalVariable::LocalDynamicTLSModel;
1602 case CodeGenOptions::InitialExecTLSModel:
1603 return llvm::GlobalVariable::InitialExecTLSModel;
1604 case CodeGenOptions::LocalExecTLSModel:
1605 return llvm::GlobalVariable::LocalExecTLSModel;
1607 llvm_unreachable("Invalid TLS model!");
1610 void CodeGenModule::setTLSMode(llvm::GlobalValue *GV, const VarDecl &D) const {
1611 assert(D.getTLSKind() && "setting TLS mode on non-TLS var!");
1613 llvm::GlobalValue::ThreadLocalMode TLM;
1614 TLM = GetDefaultLLVMTLSModel();
1616 // Override the TLS model if it is explicitly specified.
1617 if (const TLSModelAttr *Attr = D.getAttr<TLSModelAttr>()) {
1618 TLM = GetLLVMTLSModel(Attr->getModel());
1621 GV->setThreadLocalMode(TLM);
1624 static std::string getCPUSpecificMangling(const CodeGenModule &CGM,
1625 StringRef Name) {
1626 const TargetInfo &Target = CGM.getTarget();
1627 return (Twine('.') + Twine(Target.CPUSpecificManglingCharacter(Name))).str();
1630 static void AppendCPUSpecificCPUDispatchMangling(const CodeGenModule &CGM,
1631 const CPUSpecificAttr *Attr,
1632 unsigned CPUIndex,
1633 raw_ostream &Out) {
1634 // cpu_specific gets the current name, dispatch gets the resolver if IFunc is
1635 // supported.
1636 if (Attr)
1637 Out << getCPUSpecificMangling(CGM, Attr->getCPUName(CPUIndex)->getName());
1638 else if (CGM.getTarget().supportsIFunc())
1639 Out << ".resolver";
1642 static void AppendTargetVersionMangling(const CodeGenModule &CGM,
1643 const TargetVersionAttr *Attr,
1644 raw_ostream &Out) {
1645 if (Attr->isDefaultVersion())
1646 return;
1647 Out << "._";
1648 const TargetInfo &TI = CGM.getTarget();
1649 llvm::SmallVector<StringRef, 8> Feats;
1650 Attr->getFeatures(Feats);
1651 llvm::stable_sort(Feats, [&TI](const StringRef FeatL, const StringRef FeatR) {
1652 return TI.multiVersionSortPriority(FeatL) <
1653 TI.multiVersionSortPriority(FeatR);
1655 for (const auto &Feat : Feats) {
1656 Out << 'M';
1657 Out << Feat;
1661 static void AppendTargetMangling(const CodeGenModule &CGM,
1662 const TargetAttr *Attr, raw_ostream &Out) {
1663 if (Attr->isDefaultVersion())
1664 return;
1666 Out << '.';
1667 const TargetInfo &Target = CGM.getTarget();
1668 ParsedTargetAttr Info = Target.parseTargetAttr(Attr->getFeaturesStr());
1669 llvm::sort(Info.Features, [&Target](StringRef LHS, StringRef RHS) {
1670 // Multiversioning doesn't allow "no-${feature}", so we can
1671 // only have "+" prefixes here.
1672 assert(LHS.startswith("+") && RHS.startswith("+") &&
1673 "Features should always have a prefix.");
1674 return Target.multiVersionSortPriority(LHS.substr(1)) >
1675 Target.multiVersionSortPriority(RHS.substr(1));
1678 bool IsFirst = true;
1680 if (!Info.CPU.empty()) {
1681 IsFirst = false;
1682 Out << "arch_" << Info.CPU;
1685 for (StringRef Feat : Info.Features) {
1686 if (!IsFirst)
1687 Out << '_';
1688 IsFirst = false;
1689 Out << Feat.substr(1);
1693 // Returns true if GD is a function decl with internal linkage and
1694 // needs a unique suffix after the mangled name.
1695 static bool isUniqueInternalLinkageDecl(GlobalDecl GD,
1696 CodeGenModule &CGM) {
1697 const Decl *D = GD.getDecl();
1698 return !CGM.getModuleNameHash().empty() && isa<FunctionDecl>(D) &&
1699 (CGM.getFunctionLinkage(GD) == llvm::GlobalValue::InternalLinkage);
1702 static void AppendTargetClonesMangling(const CodeGenModule &CGM,
1703 const TargetClonesAttr *Attr,
1704 unsigned VersionIndex,
1705 raw_ostream &Out) {
1706 const TargetInfo &TI = CGM.getTarget();
1707 if (TI.getTriple().isAArch64()) {
1708 StringRef FeatureStr = Attr->getFeatureStr(VersionIndex);
1709 if (FeatureStr == "default")
1710 return;
1711 Out << "._";
1712 SmallVector<StringRef, 8> Features;
1713 FeatureStr.split(Features, "+");
1714 llvm::stable_sort(Features,
1715 [&TI](const StringRef FeatL, const StringRef FeatR) {
1716 return TI.multiVersionSortPriority(FeatL) <
1717 TI.multiVersionSortPriority(FeatR);
1719 for (auto &Feat : Features) {
1720 Out << 'M';
1721 Out << Feat;
1723 } else {
1724 Out << '.';
1725 StringRef FeatureStr = Attr->getFeatureStr(VersionIndex);
1726 if (FeatureStr.startswith("arch="))
1727 Out << "arch_" << FeatureStr.substr(sizeof("arch=") - 1);
1728 else
1729 Out << FeatureStr;
1731 Out << '.' << Attr->getMangledIndex(VersionIndex);
1735 static std::string getMangledNameImpl(CodeGenModule &CGM, GlobalDecl GD,
1736 const NamedDecl *ND,
1737 bool OmitMultiVersionMangling = false) {
1738 SmallString<256> Buffer;
1739 llvm::raw_svector_ostream Out(Buffer);
1740 MangleContext &MC = CGM.getCXXABI().getMangleContext();
1741 if (!CGM.getModuleNameHash().empty())
1742 MC.needsUniqueInternalLinkageNames();
1743 bool ShouldMangle = MC.shouldMangleDeclName(ND);
1744 if (ShouldMangle)
1745 MC.mangleName(GD.getWithDecl(ND), Out);
1746 else {
1747 IdentifierInfo *II = ND->getIdentifier();
1748 assert(II && "Attempt to mangle unnamed decl.");
1749 const auto *FD = dyn_cast<FunctionDecl>(ND);
1751 if (FD &&
1752 FD->getType()->castAs<FunctionType>()->getCallConv() == CC_X86RegCall) {
1753 if (CGM.getLangOpts().RegCall4)
1754 Out << "__regcall4__" << II->getName();
1755 else
1756 Out << "__regcall3__" << II->getName();
1757 } else if (FD && FD->hasAttr<CUDAGlobalAttr>() &&
1758 GD.getKernelReferenceKind() == KernelReferenceKind::Stub) {
1759 Out << "__device_stub__" << II->getName();
1760 } else {
1761 Out << II->getName();
1765 // Check if the module name hash should be appended for internal linkage
1766 // symbols. This should come before multi-version target suffixes are
1767 // appended. This is to keep the name and module hash suffix of the
1768 // internal linkage function together. The unique suffix should only be
1769 // added when name mangling is done to make sure that the final name can
1770 // be properly demangled. For example, for C functions without prototypes,
1771 // name mangling is not done and the unique suffix should not be appeneded
1772 // then.
1773 if (ShouldMangle && isUniqueInternalLinkageDecl(GD, CGM)) {
1774 assert(CGM.getCodeGenOpts().UniqueInternalLinkageNames &&
1775 "Hash computed when not explicitly requested");
1776 Out << CGM.getModuleNameHash();
1779 if (const auto *FD = dyn_cast<FunctionDecl>(ND))
1780 if (FD->isMultiVersion() && !OmitMultiVersionMangling) {
1781 switch (FD->getMultiVersionKind()) {
1782 case MultiVersionKind::CPUDispatch:
1783 case MultiVersionKind::CPUSpecific:
1784 AppendCPUSpecificCPUDispatchMangling(CGM,
1785 FD->getAttr<CPUSpecificAttr>(),
1786 GD.getMultiVersionIndex(), Out);
1787 break;
1788 case MultiVersionKind::Target:
1789 AppendTargetMangling(CGM, FD->getAttr<TargetAttr>(), Out);
1790 break;
1791 case MultiVersionKind::TargetVersion:
1792 AppendTargetVersionMangling(CGM, FD->getAttr<TargetVersionAttr>(), Out);
1793 break;
1794 case MultiVersionKind::TargetClones:
1795 AppendTargetClonesMangling(CGM, FD->getAttr<TargetClonesAttr>(),
1796 GD.getMultiVersionIndex(), Out);
1797 break;
1798 case MultiVersionKind::None:
1799 llvm_unreachable("None multiversion type isn't valid here");
1803 // Make unique name for device side static file-scope variable for HIP.
1804 if (CGM.getContext().shouldExternalize(ND) &&
1805 CGM.getLangOpts().GPURelocatableDeviceCode &&
1806 CGM.getLangOpts().CUDAIsDevice)
1807 CGM.printPostfixForExternalizedDecl(Out, ND);
1809 return std::string(Out.str());
1812 void CodeGenModule::UpdateMultiVersionNames(GlobalDecl GD,
1813 const FunctionDecl *FD,
1814 StringRef &CurName) {
1815 if (!FD->isMultiVersion())
1816 return;
1818 // Get the name of what this would be without the 'target' attribute. This
1819 // allows us to lookup the version that was emitted when this wasn't a
1820 // multiversion function.
1821 std::string NonTargetName =
1822 getMangledNameImpl(*this, GD, FD, /*OmitMultiVersionMangling=*/true);
1823 GlobalDecl OtherGD;
1824 if (lookupRepresentativeDecl(NonTargetName, OtherGD)) {
1825 assert(OtherGD.getCanonicalDecl()
1826 .getDecl()
1827 ->getAsFunction()
1828 ->isMultiVersion() &&
1829 "Other GD should now be a multiversioned function");
1830 // OtherFD is the version of this function that was mangled BEFORE
1831 // becoming a MultiVersion function. It potentially needs to be updated.
1832 const FunctionDecl *OtherFD = OtherGD.getCanonicalDecl()
1833 .getDecl()
1834 ->getAsFunction()
1835 ->getMostRecentDecl();
1836 std::string OtherName = getMangledNameImpl(*this, OtherGD, OtherFD);
1837 // This is so that if the initial version was already the 'default'
1838 // version, we don't try to update it.
1839 if (OtherName != NonTargetName) {
1840 // Remove instead of erase, since others may have stored the StringRef
1841 // to this.
1842 const auto ExistingRecord = Manglings.find(NonTargetName);
1843 if (ExistingRecord != std::end(Manglings))
1844 Manglings.remove(&(*ExistingRecord));
1845 auto Result = Manglings.insert(std::make_pair(OtherName, OtherGD));
1846 StringRef OtherNameRef = MangledDeclNames[OtherGD.getCanonicalDecl()] =
1847 Result.first->first();
1848 // If this is the current decl is being created, make sure we update the name.
1849 if (GD.getCanonicalDecl() == OtherGD.getCanonicalDecl())
1850 CurName = OtherNameRef;
1851 if (llvm::GlobalValue *Entry = GetGlobalValue(NonTargetName))
1852 Entry->setName(OtherName);
1857 StringRef CodeGenModule::getMangledName(GlobalDecl GD) {
1858 GlobalDecl CanonicalGD = GD.getCanonicalDecl();
1860 // Some ABIs don't have constructor variants. Make sure that base and
1861 // complete constructors get mangled the same.
1862 if (const auto *CD = dyn_cast<CXXConstructorDecl>(CanonicalGD.getDecl())) {
1863 if (!getTarget().getCXXABI().hasConstructorVariants()) {
1864 CXXCtorType OrigCtorType = GD.getCtorType();
1865 assert(OrigCtorType == Ctor_Base || OrigCtorType == Ctor_Complete);
1866 if (OrigCtorType == Ctor_Base)
1867 CanonicalGD = GlobalDecl(CD, Ctor_Complete);
1871 // In CUDA/HIP device compilation with -fgpu-rdc, the mangled name of a
1872 // static device variable depends on whether the variable is referenced by
1873 // a host or device host function. Therefore the mangled name cannot be
1874 // cached.
1875 if (!LangOpts.CUDAIsDevice || !getContext().mayExternalize(GD.getDecl())) {
1876 auto FoundName = MangledDeclNames.find(CanonicalGD);
1877 if (FoundName != MangledDeclNames.end())
1878 return FoundName->second;
1881 // Keep the first result in the case of a mangling collision.
1882 const auto *ND = cast<NamedDecl>(GD.getDecl());
1883 std::string MangledName = getMangledNameImpl(*this, GD, ND);
1885 // Ensure either we have different ABIs between host and device compilations,
1886 // says host compilation following MSVC ABI but device compilation follows
1887 // Itanium C++ ABI or, if they follow the same ABI, kernel names after
1888 // mangling should be the same after name stubbing. The later checking is
1889 // very important as the device kernel name being mangled in host-compilation
1890 // is used to resolve the device binaries to be executed. Inconsistent naming
1891 // result in undefined behavior. Even though we cannot check that naming
1892 // directly between host- and device-compilations, the host- and
1893 // device-mangling in host compilation could help catching certain ones.
1894 assert(!isa<FunctionDecl>(ND) || !ND->hasAttr<CUDAGlobalAttr>() ||
1895 getContext().shouldExternalize(ND) || getLangOpts().CUDAIsDevice ||
1896 (getContext().getAuxTargetInfo() &&
1897 (getContext().getAuxTargetInfo()->getCXXABI() !=
1898 getContext().getTargetInfo().getCXXABI())) ||
1899 getCUDARuntime().getDeviceSideName(ND) ==
1900 getMangledNameImpl(
1901 *this,
1902 GD.getWithKernelReferenceKind(KernelReferenceKind::Kernel),
1903 ND));
1905 auto Result = Manglings.insert(std::make_pair(MangledName, GD));
1906 return MangledDeclNames[CanonicalGD] = Result.first->first();
1909 StringRef CodeGenModule::getBlockMangledName(GlobalDecl GD,
1910 const BlockDecl *BD) {
1911 MangleContext &MangleCtx = getCXXABI().getMangleContext();
1912 const Decl *D = GD.getDecl();
1914 SmallString<256> Buffer;
1915 llvm::raw_svector_ostream Out(Buffer);
1916 if (!D)
1917 MangleCtx.mangleGlobalBlock(BD,
1918 dyn_cast_or_null<VarDecl>(initializedGlobalDecl.getDecl()), Out);
1919 else if (const auto *CD = dyn_cast<CXXConstructorDecl>(D))
1920 MangleCtx.mangleCtorBlock(CD, GD.getCtorType(), BD, Out);
1921 else if (const auto *DD = dyn_cast<CXXDestructorDecl>(D))
1922 MangleCtx.mangleDtorBlock(DD, GD.getDtorType(), BD, Out);
1923 else
1924 MangleCtx.mangleBlock(cast<DeclContext>(D), BD, Out);
1926 auto Result = Manglings.insert(std::make_pair(Out.str(), BD));
1927 return Result.first->first();
1930 const GlobalDecl CodeGenModule::getMangledNameDecl(StringRef Name) {
1931 auto it = MangledDeclNames.begin();
1932 while (it != MangledDeclNames.end()) {
1933 if (it->second == Name)
1934 return it->first;
1935 it++;
1937 return GlobalDecl();
1940 llvm::GlobalValue *CodeGenModule::GetGlobalValue(StringRef Name) {
1941 return getModule().getNamedValue(Name);
1944 /// AddGlobalCtor - Add a function to the list that will be called before
1945 /// main() runs.
1946 void CodeGenModule::AddGlobalCtor(llvm::Function *Ctor, int Priority,
1947 unsigned LexOrder,
1948 llvm::Constant *AssociatedData) {
1949 // FIXME: Type coercion of void()* types.
1950 GlobalCtors.push_back(Structor(Priority, LexOrder, Ctor, AssociatedData));
1953 /// AddGlobalDtor - Add a function to the list that will be called
1954 /// when the module is unloaded.
1955 void CodeGenModule::AddGlobalDtor(llvm::Function *Dtor, int Priority,
1956 bool IsDtorAttrFunc) {
1957 if (CodeGenOpts.RegisterGlobalDtorsWithAtExit &&
1958 (!getContext().getTargetInfo().getTriple().isOSAIX() || IsDtorAttrFunc)) {
1959 DtorsUsingAtExit[Priority].push_back(Dtor);
1960 return;
1963 // FIXME: Type coercion of void()* types.
1964 GlobalDtors.push_back(Structor(Priority, ~0U, Dtor, nullptr));
1967 void CodeGenModule::EmitCtorList(CtorList &Fns, const char *GlobalName) {
1968 if (Fns.empty()) return;
1970 // Ctor function type is void()*.
1971 llvm::FunctionType* CtorFTy = llvm::FunctionType::get(VoidTy, false);
1972 llvm::Type *CtorPFTy = llvm::PointerType::get(CtorFTy,
1973 TheModule.getDataLayout().getProgramAddressSpace());
1975 // Get the type of a ctor entry, { i32, void ()*, i8* }.
1976 llvm::StructType *CtorStructTy = llvm::StructType::get(
1977 Int32Ty, CtorPFTy, VoidPtrTy);
1979 // Construct the constructor and destructor arrays.
1980 ConstantInitBuilder builder(*this);
1981 auto ctors = builder.beginArray(CtorStructTy);
1982 for (const auto &I : Fns) {
1983 auto ctor = ctors.beginStruct(CtorStructTy);
1984 ctor.addInt(Int32Ty, I.Priority);
1985 ctor.add(llvm::ConstantExpr::getBitCast(I.Initializer, CtorPFTy));
1986 if (I.AssociatedData)
1987 ctor.add(llvm::ConstantExpr::getBitCast(I.AssociatedData, VoidPtrTy));
1988 else
1989 ctor.addNullPointer(VoidPtrTy);
1990 ctor.finishAndAddTo(ctors);
1993 auto list =
1994 ctors.finishAndCreateGlobal(GlobalName, getPointerAlign(),
1995 /*constant*/ false,
1996 llvm::GlobalValue::AppendingLinkage);
1998 // The LTO linker doesn't seem to like it when we set an alignment
1999 // on appending variables. Take it off as a workaround.
2000 list->setAlignment(std::nullopt);
2002 Fns.clear();
2005 llvm::GlobalValue::LinkageTypes
2006 CodeGenModule::getFunctionLinkage(GlobalDecl GD) {
2007 const auto *D = cast<FunctionDecl>(GD.getDecl());
2009 GVALinkage Linkage = getContext().GetGVALinkageForFunction(D);
2011 if (const auto *Dtor = dyn_cast<CXXDestructorDecl>(D))
2012 return getCXXABI().getCXXDestructorLinkage(Linkage, Dtor, GD.getDtorType());
2014 return getLLVMLinkageForDeclarator(D, Linkage);
2017 llvm::ConstantInt *CodeGenModule::CreateCrossDsoCfiTypeId(llvm::Metadata *MD) {
2018 llvm::MDString *MDS = dyn_cast<llvm::MDString>(MD);
2019 if (!MDS) return nullptr;
2021 return llvm::ConstantInt::get(Int64Ty, llvm::MD5Hash(MDS->getString()));
2024 llvm::ConstantInt *CodeGenModule::CreateKCFITypeId(QualType T) {
2025 if (auto *FnType = T->getAs<FunctionProtoType>())
2026 T = getContext().getFunctionType(
2027 FnType->getReturnType(), FnType->getParamTypes(),
2028 FnType->getExtProtoInfo().withExceptionSpec(EST_None));
2030 std::string OutName;
2031 llvm::raw_string_ostream Out(OutName);
2032 getCXXABI().getMangleContext().mangleCanonicalTypeName(
2033 T, Out, getCodeGenOpts().SanitizeCfiICallNormalizeIntegers);
2035 if (getCodeGenOpts().SanitizeCfiICallNormalizeIntegers)
2036 Out << ".normalized";
2038 return llvm::ConstantInt::get(Int32Ty,
2039 static_cast<uint32_t>(llvm::xxHash64(OutName)));
2042 void CodeGenModule::SetLLVMFunctionAttributes(GlobalDecl GD,
2043 const CGFunctionInfo &Info,
2044 llvm::Function *F, bool IsThunk) {
2045 unsigned CallingConv;
2046 llvm::AttributeList PAL;
2047 ConstructAttributeList(F->getName(), Info, GD, PAL, CallingConv,
2048 /*AttrOnCallSite=*/false, IsThunk);
2049 F->setAttributes(PAL);
2050 F->setCallingConv(static_cast<llvm::CallingConv::ID>(CallingConv));
2053 static void removeImageAccessQualifier(std::string& TyName) {
2054 std::string ReadOnlyQual("__read_only");
2055 std::string::size_type ReadOnlyPos = TyName.find(ReadOnlyQual);
2056 if (ReadOnlyPos != std::string::npos)
2057 // "+ 1" for the space after access qualifier.
2058 TyName.erase(ReadOnlyPos, ReadOnlyQual.size() + 1);
2059 else {
2060 std::string WriteOnlyQual("__write_only");
2061 std::string::size_type WriteOnlyPos = TyName.find(WriteOnlyQual);
2062 if (WriteOnlyPos != std::string::npos)
2063 TyName.erase(WriteOnlyPos, WriteOnlyQual.size() + 1);
2064 else {
2065 std::string ReadWriteQual("__read_write");
2066 std::string::size_type ReadWritePos = TyName.find(ReadWriteQual);
2067 if (ReadWritePos != std::string::npos)
2068 TyName.erase(ReadWritePos, ReadWriteQual.size() + 1);
2073 // Returns the address space id that should be produced to the
2074 // kernel_arg_addr_space metadata. This is always fixed to the ids
2075 // as specified in the SPIR 2.0 specification in order to differentiate
2076 // for example in clGetKernelArgInfo() implementation between the address
2077 // spaces with targets without unique mapping to the OpenCL address spaces
2078 // (basically all single AS CPUs).
2079 static unsigned ArgInfoAddressSpace(LangAS AS) {
2080 switch (AS) {
2081 case LangAS::opencl_global:
2082 return 1;
2083 case LangAS::opencl_constant:
2084 return 2;
2085 case LangAS::opencl_local:
2086 return 3;
2087 case LangAS::opencl_generic:
2088 return 4; // Not in SPIR 2.0 specs.
2089 case LangAS::opencl_global_device:
2090 return 5;
2091 case LangAS::opencl_global_host:
2092 return 6;
2093 default:
2094 return 0; // Assume private.
2098 void CodeGenModule::GenKernelArgMetadata(llvm::Function *Fn,
2099 const FunctionDecl *FD,
2100 CodeGenFunction *CGF) {
2101 assert(((FD && CGF) || (!FD && !CGF)) &&
2102 "Incorrect use - FD and CGF should either be both null or not!");
2103 // Create MDNodes that represent the kernel arg metadata.
2104 // Each MDNode is a list in the form of "key", N number of values which is
2105 // the same number of values as their are kernel arguments.
2107 const PrintingPolicy &Policy = Context.getPrintingPolicy();
2109 // MDNode for the kernel argument address space qualifiers.
2110 SmallVector<llvm::Metadata *, 8> addressQuals;
2112 // MDNode for the kernel argument access qualifiers (images only).
2113 SmallVector<llvm::Metadata *, 8> accessQuals;
2115 // MDNode for the kernel argument type names.
2116 SmallVector<llvm::Metadata *, 8> argTypeNames;
2118 // MDNode for the kernel argument base type names.
2119 SmallVector<llvm::Metadata *, 8> argBaseTypeNames;
2121 // MDNode for the kernel argument type qualifiers.
2122 SmallVector<llvm::Metadata *, 8> argTypeQuals;
2124 // MDNode for the kernel argument names.
2125 SmallVector<llvm::Metadata *, 8> argNames;
2127 if (FD && CGF)
2128 for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) {
2129 const ParmVarDecl *parm = FD->getParamDecl(i);
2130 // Get argument name.
2131 argNames.push_back(llvm::MDString::get(VMContext, parm->getName()));
2133 if (!getLangOpts().OpenCL)
2134 continue;
2135 QualType ty = parm->getType();
2136 std::string typeQuals;
2138 // Get image and pipe access qualifier:
2139 if (ty->isImageType() || ty->isPipeType()) {
2140 const Decl *PDecl = parm;
2141 if (const auto *TD = ty->getAs<TypedefType>())
2142 PDecl = TD->getDecl();
2143 const OpenCLAccessAttr *A = PDecl->getAttr<OpenCLAccessAttr>();
2144 if (A && A->isWriteOnly())
2145 accessQuals.push_back(llvm::MDString::get(VMContext, "write_only"));
2146 else if (A && A->isReadWrite())
2147 accessQuals.push_back(llvm::MDString::get(VMContext, "read_write"));
2148 else
2149 accessQuals.push_back(llvm::MDString::get(VMContext, "read_only"));
2150 } else
2151 accessQuals.push_back(llvm::MDString::get(VMContext, "none"));
2153 auto getTypeSpelling = [&](QualType Ty) {
2154 auto typeName = Ty.getUnqualifiedType().getAsString(Policy);
2156 if (Ty.isCanonical()) {
2157 StringRef typeNameRef = typeName;
2158 // Turn "unsigned type" to "utype"
2159 if (typeNameRef.consume_front("unsigned "))
2160 return std::string("u") + typeNameRef.str();
2161 if (typeNameRef.consume_front("signed "))
2162 return typeNameRef.str();
2165 return typeName;
2168 if (ty->isPointerType()) {
2169 QualType pointeeTy = ty->getPointeeType();
2171 // Get address qualifier.
2172 addressQuals.push_back(
2173 llvm::ConstantAsMetadata::get(CGF->Builder.getInt32(
2174 ArgInfoAddressSpace(pointeeTy.getAddressSpace()))));
2176 // Get argument type name.
2177 std::string typeName = getTypeSpelling(pointeeTy) + "*";
2178 std::string baseTypeName =
2179 getTypeSpelling(pointeeTy.getCanonicalType()) + "*";
2180 argTypeNames.push_back(llvm::MDString::get(VMContext, typeName));
2181 argBaseTypeNames.push_back(
2182 llvm::MDString::get(VMContext, baseTypeName));
2184 // Get argument type qualifiers:
2185 if (ty.isRestrictQualified())
2186 typeQuals = "restrict";
2187 if (pointeeTy.isConstQualified() ||
2188 (pointeeTy.getAddressSpace() == LangAS::opencl_constant))
2189 typeQuals += typeQuals.empty() ? "const" : " const";
2190 if (pointeeTy.isVolatileQualified())
2191 typeQuals += typeQuals.empty() ? "volatile" : " volatile";
2192 } else {
2193 uint32_t AddrSpc = 0;
2194 bool isPipe = ty->isPipeType();
2195 if (ty->isImageType() || isPipe)
2196 AddrSpc = ArgInfoAddressSpace(LangAS::opencl_global);
2198 addressQuals.push_back(
2199 llvm::ConstantAsMetadata::get(CGF->Builder.getInt32(AddrSpc)));
2201 // Get argument type name.
2202 ty = isPipe ? ty->castAs<PipeType>()->getElementType() : ty;
2203 std::string typeName = getTypeSpelling(ty);
2204 std::string baseTypeName = getTypeSpelling(ty.getCanonicalType());
2206 // Remove access qualifiers on images
2207 // (as they are inseparable from type in clang implementation,
2208 // but OpenCL spec provides a special query to get access qualifier
2209 // via clGetKernelArgInfo with CL_KERNEL_ARG_ACCESS_QUALIFIER):
2210 if (ty->isImageType()) {
2211 removeImageAccessQualifier(typeName);
2212 removeImageAccessQualifier(baseTypeName);
2215 argTypeNames.push_back(llvm::MDString::get(VMContext, typeName));
2216 argBaseTypeNames.push_back(
2217 llvm::MDString::get(VMContext, baseTypeName));
2219 if (isPipe)
2220 typeQuals = "pipe";
2222 argTypeQuals.push_back(llvm::MDString::get(VMContext, typeQuals));
2225 if (getLangOpts().OpenCL) {
2226 Fn->setMetadata("kernel_arg_addr_space",
2227 llvm::MDNode::get(VMContext, addressQuals));
2228 Fn->setMetadata("kernel_arg_access_qual",
2229 llvm::MDNode::get(VMContext, accessQuals));
2230 Fn->setMetadata("kernel_arg_type",
2231 llvm::MDNode::get(VMContext, argTypeNames));
2232 Fn->setMetadata("kernel_arg_base_type",
2233 llvm::MDNode::get(VMContext, argBaseTypeNames));
2234 Fn->setMetadata("kernel_arg_type_qual",
2235 llvm::MDNode::get(VMContext, argTypeQuals));
2237 if (getCodeGenOpts().EmitOpenCLArgMetadata ||
2238 getCodeGenOpts().HIPSaveKernelArgName)
2239 Fn->setMetadata("kernel_arg_name",
2240 llvm::MDNode::get(VMContext, argNames));
2243 /// Determines whether the language options require us to model
2244 /// unwind exceptions. We treat -fexceptions as mandating this
2245 /// except under the fragile ObjC ABI with only ObjC exceptions
2246 /// enabled. This means, for example, that C with -fexceptions
2247 /// enables this.
2248 static bool hasUnwindExceptions(const LangOptions &LangOpts) {
2249 // If exceptions are completely disabled, obviously this is false.
2250 if (!LangOpts.Exceptions) return false;
2252 // If C++ exceptions are enabled, this is true.
2253 if (LangOpts.CXXExceptions) return true;
2255 // If ObjC exceptions are enabled, this depends on the ABI.
2256 if (LangOpts.ObjCExceptions) {
2257 return LangOpts.ObjCRuntime.hasUnwindExceptions();
2260 return true;
2263 static bool requiresMemberFunctionPointerTypeMetadata(CodeGenModule &CGM,
2264 const CXXMethodDecl *MD) {
2265 // Check that the type metadata can ever actually be used by a call.
2266 if (!CGM.getCodeGenOpts().LTOUnit ||
2267 !CGM.HasHiddenLTOVisibility(MD->getParent()))
2268 return false;
2270 // Only functions whose address can be taken with a member function pointer
2271 // need this sort of type metadata.
2272 return MD->isImplicitObjectMemberFunction() && !MD->isVirtual() &&
2273 !isa<CXXConstructorDecl, CXXDestructorDecl>(MD);
2276 SmallVector<const CXXRecordDecl *, 0>
2277 CodeGenModule::getMostBaseClasses(const CXXRecordDecl *RD) {
2278 llvm::SetVector<const CXXRecordDecl *> MostBases;
2280 std::function<void (const CXXRecordDecl *)> CollectMostBases;
2281 CollectMostBases = [&](const CXXRecordDecl *RD) {
2282 if (RD->getNumBases() == 0)
2283 MostBases.insert(RD);
2284 for (const CXXBaseSpecifier &B : RD->bases())
2285 CollectMostBases(B.getType()->getAsCXXRecordDecl());
2287 CollectMostBases(RD);
2288 return MostBases.takeVector();
2291 void CodeGenModule::SetLLVMFunctionAttributesForDefinition(const Decl *D,
2292 llvm::Function *F) {
2293 llvm::AttrBuilder B(F->getContext());
2295 if ((!D || !D->hasAttr<NoUwtableAttr>()) && CodeGenOpts.UnwindTables)
2296 B.addUWTableAttr(llvm::UWTableKind(CodeGenOpts.UnwindTables));
2298 if (CodeGenOpts.StackClashProtector)
2299 B.addAttribute("probe-stack", "inline-asm");
2301 if (!hasUnwindExceptions(LangOpts))
2302 B.addAttribute(llvm::Attribute::NoUnwind);
2304 if (D && D->hasAttr<NoStackProtectorAttr>())
2305 ; // Do nothing.
2306 else if (D && D->hasAttr<StrictGuardStackCheckAttr>() &&
2307 isStackProtectorOn(LangOpts, getTriple(), LangOptions::SSPOn))
2308 B.addAttribute(llvm::Attribute::StackProtectStrong);
2309 else if (isStackProtectorOn(LangOpts, getTriple(), LangOptions::SSPOn))
2310 B.addAttribute(llvm::Attribute::StackProtect);
2311 else if (isStackProtectorOn(LangOpts, getTriple(), LangOptions::SSPStrong))
2312 B.addAttribute(llvm::Attribute::StackProtectStrong);
2313 else if (isStackProtectorOn(LangOpts, getTriple(), LangOptions::SSPReq))
2314 B.addAttribute(llvm::Attribute::StackProtectReq);
2316 if (!D) {
2317 // If we don't have a declaration to control inlining, the function isn't
2318 // explicitly marked as alwaysinline for semantic reasons, and inlining is
2319 // disabled, mark the function as noinline.
2320 if (!F->hasFnAttribute(llvm::Attribute::AlwaysInline) &&
2321 CodeGenOpts.getInlining() == CodeGenOptions::OnlyAlwaysInlining)
2322 B.addAttribute(llvm::Attribute::NoInline);
2324 F->addFnAttrs(B);
2325 return;
2328 // Handle SME attributes that apply to function definitions,
2329 // rather than to function prototypes.
2330 if (D->hasAttr<ArmLocallyStreamingAttr>())
2331 B.addAttribute("aarch64_pstate_sm_body");
2333 if (D->hasAttr<ArmNewZAAttr>())
2334 B.addAttribute("aarch64_pstate_za_new");
2336 // Track whether we need to add the optnone LLVM attribute,
2337 // starting with the default for this optimization level.
2338 bool ShouldAddOptNone =
2339 !CodeGenOpts.DisableO0ImplyOptNone && CodeGenOpts.OptimizationLevel == 0;
2340 // We can't add optnone in the following cases, it won't pass the verifier.
2341 ShouldAddOptNone &= !D->hasAttr<MinSizeAttr>();
2342 ShouldAddOptNone &= !D->hasAttr<AlwaysInlineAttr>();
2344 // Add optnone, but do so only if the function isn't always_inline.
2345 if ((ShouldAddOptNone || D->hasAttr<OptimizeNoneAttr>()) &&
2346 !F->hasFnAttribute(llvm::Attribute::AlwaysInline)) {
2347 B.addAttribute(llvm::Attribute::OptimizeNone);
2349 // OptimizeNone implies noinline; we should not be inlining such functions.
2350 B.addAttribute(llvm::Attribute::NoInline);
2352 // We still need to handle naked functions even though optnone subsumes
2353 // much of their semantics.
2354 if (D->hasAttr<NakedAttr>())
2355 B.addAttribute(llvm::Attribute::Naked);
2357 // OptimizeNone wins over OptimizeForSize and MinSize.
2358 F->removeFnAttr(llvm::Attribute::OptimizeForSize);
2359 F->removeFnAttr(llvm::Attribute::MinSize);
2360 } else if (D->hasAttr<NakedAttr>()) {
2361 // Naked implies noinline: we should not be inlining such functions.
2362 B.addAttribute(llvm::Attribute::Naked);
2363 B.addAttribute(llvm::Attribute::NoInline);
2364 } else if (D->hasAttr<NoDuplicateAttr>()) {
2365 B.addAttribute(llvm::Attribute::NoDuplicate);
2366 } else if (D->hasAttr<NoInlineAttr>() && !F->hasFnAttribute(llvm::Attribute::AlwaysInline)) {
2367 // Add noinline if the function isn't always_inline.
2368 B.addAttribute(llvm::Attribute::NoInline);
2369 } else if (D->hasAttr<AlwaysInlineAttr>() &&
2370 !F->hasFnAttribute(llvm::Attribute::NoInline)) {
2371 // (noinline wins over always_inline, and we can't specify both in IR)
2372 B.addAttribute(llvm::Attribute::AlwaysInline);
2373 } else if (CodeGenOpts.getInlining() == CodeGenOptions::OnlyAlwaysInlining) {
2374 // If we're not inlining, then force everything that isn't always_inline to
2375 // carry an explicit noinline attribute.
2376 if (!F->hasFnAttribute(llvm::Attribute::AlwaysInline))
2377 B.addAttribute(llvm::Attribute::NoInline);
2378 } else {
2379 // Otherwise, propagate the inline hint attribute and potentially use its
2380 // absence to mark things as noinline.
2381 if (auto *FD = dyn_cast<FunctionDecl>(D)) {
2382 // Search function and template pattern redeclarations for inline.
2383 auto CheckForInline = [](const FunctionDecl *FD) {
2384 auto CheckRedeclForInline = [](const FunctionDecl *Redecl) {
2385 return Redecl->isInlineSpecified();
2387 if (any_of(FD->redecls(), CheckRedeclForInline))
2388 return true;
2389 const FunctionDecl *Pattern = FD->getTemplateInstantiationPattern();
2390 if (!Pattern)
2391 return false;
2392 return any_of(Pattern->redecls(), CheckRedeclForInline);
2394 if (CheckForInline(FD)) {
2395 B.addAttribute(llvm::Attribute::InlineHint);
2396 } else if (CodeGenOpts.getInlining() ==
2397 CodeGenOptions::OnlyHintInlining &&
2398 !FD->isInlined() &&
2399 !F->hasFnAttribute(llvm::Attribute::AlwaysInline)) {
2400 B.addAttribute(llvm::Attribute::NoInline);
2405 // Add other optimization related attributes if we are optimizing this
2406 // function.
2407 if (!D->hasAttr<OptimizeNoneAttr>()) {
2408 if (D->hasAttr<ColdAttr>()) {
2409 if (!ShouldAddOptNone)
2410 B.addAttribute(llvm::Attribute::OptimizeForSize);
2411 B.addAttribute(llvm::Attribute::Cold);
2413 if (D->hasAttr<HotAttr>())
2414 B.addAttribute(llvm::Attribute::Hot);
2415 if (D->hasAttr<MinSizeAttr>())
2416 B.addAttribute(llvm::Attribute::MinSize);
2419 F->addFnAttrs(B);
2421 unsigned alignment = D->getMaxAlignment() / Context.getCharWidth();
2422 if (alignment)
2423 F->setAlignment(llvm::Align(alignment));
2425 if (!D->hasAttr<AlignedAttr>())
2426 if (LangOpts.FunctionAlignment)
2427 F->setAlignment(llvm::Align(1ull << LangOpts.FunctionAlignment));
2429 // Some C++ ABIs require 2-byte alignment for member functions, in order to
2430 // reserve a bit for differentiating between virtual and non-virtual member
2431 // functions. If the current target's C++ ABI requires this and this is a
2432 // member function, set its alignment accordingly.
2433 if (getTarget().getCXXABI().areMemberFunctionsAligned()) {
2434 if (isa<CXXMethodDecl>(D) && F->getPointerAlignment(getDataLayout()) < 2)
2435 F->setAlignment(std::max(llvm::Align(2), F->getAlign().valueOrOne()));
2438 // In the cross-dso CFI mode with canonical jump tables, we want !type
2439 // attributes on definitions only.
2440 if (CodeGenOpts.SanitizeCfiCrossDso &&
2441 CodeGenOpts.SanitizeCfiCanonicalJumpTables) {
2442 if (auto *FD = dyn_cast<FunctionDecl>(D)) {
2443 // Skip available_externally functions. They won't be codegen'ed in the
2444 // current module anyway.
2445 if (getContext().GetGVALinkageForFunction(FD) != GVA_AvailableExternally)
2446 CreateFunctionTypeMetadataForIcall(FD, F);
2450 // Emit type metadata on member functions for member function pointer checks.
2451 // These are only ever necessary on definitions; we're guaranteed that the
2452 // definition will be present in the LTO unit as a result of LTO visibility.
2453 auto *MD = dyn_cast<CXXMethodDecl>(D);
2454 if (MD && requiresMemberFunctionPointerTypeMetadata(*this, MD)) {
2455 for (const CXXRecordDecl *Base : getMostBaseClasses(MD->getParent())) {
2456 llvm::Metadata *Id =
2457 CreateMetadataIdentifierForType(Context.getMemberPointerType(
2458 MD->getType(), Context.getRecordType(Base).getTypePtr()));
2459 F->addTypeMetadata(0, Id);
2464 void CodeGenModule::SetCommonAttributes(GlobalDecl GD, llvm::GlobalValue *GV) {
2465 const Decl *D = GD.getDecl();
2466 if (isa_and_nonnull<NamedDecl>(D))
2467 setGVProperties(GV, GD);
2468 else
2469 GV->setVisibility(llvm::GlobalValue::DefaultVisibility);
2471 if (D && D->hasAttr<UsedAttr>())
2472 addUsedOrCompilerUsedGlobal(GV);
2474 if (const auto *VD = dyn_cast_if_present<VarDecl>(D);
2475 VD &&
2476 ((CodeGenOpts.KeepPersistentStorageVariables &&
2477 (VD->getStorageDuration() == SD_Static ||
2478 VD->getStorageDuration() == SD_Thread)) ||
2479 (CodeGenOpts.KeepStaticConsts && VD->getStorageDuration() == SD_Static &&
2480 VD->getType().isConstQualified())))
2481 addUsedOrCompilerUsedGlobal(GV);
2484 bool CodeGenModule::GetCPUAndFeaturesAttributes(GlobalDecl GD,
2485 llvm::AttrBuilder &Attrs,
2486 bool SetTargetFeatures) {
2487 // Add target-cpu and target-features attributes to functions. If
2488 // we have a decl for the function and it has a target attribute then
2489 // parse that and add it to the feature set.
2490 StringRef TargetCPU = getTarget().getTargetOpts().CPU;
2491 StringRef TuneCPU = getTarget().getTargetOpts().TuneCPU;
2492 std::vector<std::string> Features;
2493 const auto *FD = dyn_cast_or_null<FunctionDecl>(GD.getDecl());
2494 FD = FD ? FD->getMostRecentDecl() : FD;
2495 const auto *TD = FD ? FD->getAttr<TargetAttr>() : nullptr;
2496 const auto *TV = FD ? FD->getAttr<TargetVersionAttr>() : nullptr;
2497 assert((!TD || !TV) && "both target_version and target specified");
2498 const auto *SD = FD ? FD->getAttr<CPUSpecificAttr>() : nullptr;
2499 const auto *TC = FD ? FD->getAttr<TargetClonesAttr>() : nullptr;
2500 bool AddedAttr = false;
2501 if (TD || TV || SD || TC) {
2502 llvm::StringMap<bool> FeatureMap;
2503 getContext().getFunctionFeatureMap(FeatureMap, GD);
2505 // Produce the canonical string for this set of features.
2506 for (const llvm::StringMap<bool>::value_type &Entry : FeatureMap)
2507 Features.push_back((Entry.getValue() ? "+" : "-") + Entry.getKey().str());
2509 // Now add the target-cpu and target-features to the function.
2510 // While we populated the feature map above, we still need to
2511 // get and parse the target attribute so we can get the cpu for
2512 // the function.
2513 if (TD) {
2514 ParsedTargetAttr ParsedAttr =
2515 Target.parseTargetAttr(TD->getFeaturesStr());
2516 if (!ParsedAttr.CPU.empty() &&
2517 getTarget().isValidCPUName(ParsedAttr.CPU)) {
2518 TargetCPU = ParsedAttr.CPU;
2519 TuneCPU = ""; // Clear the tune CPU.
2521 if (!ParsedAttr.Tune.empty() &&
2522 getTarget().isValidCPUName(ParsedAttr.Tune))
2523 TuneCPU = ParsedAttr.Tune;
2526 if (SD) {
2527 // Apply the given CPU name as the 'tune-cpu' so that the optimizer can
2528 // favor this processor.
2529 TuneCPU = SD->getCPUName(GD.getMultiVersionIndex())->getName();
2531 } else {
2532 // Otherwise just add the existing target cpu and target features to the
2533 // function.
2534 Features = getTarget().getTargetOpts().Features;
2537 if (!TargetCPU.empty()) {
2538 Attrs.addAttribute("target-cpu", TargetCPU);
2539 AddedAttr = true;
2541 if (!TuneCPU.empty()) {
2542 Attrs.addAttribute("tune-cpu", TuneCPU);
2543 AddedAttr = true;
2545 if (!Features.empty() && SetTargetFeatures) {
2546 llvm::erase_if(Features, [&](const std::string& F) {
2547 return getTarget().isReadOnlyFeature(F.substr(1));
2549 llvm::sort(Features);
2550 Attrs.addAttribute("target-features", llvm::join(Features, ","));
2551 AddedAttr = true;
2554 return AddedAttr;
2557 void CodeGenModule::setNonAliasAttributes(GlobalDecl GD,
2558 llvm::GlobalObject *GO) {
2559 const Decl *D = GD.getDecl();
2560 SetCommonAttributes(GD, GO);
2562 if (D) {
2563 if (auto *GV = dyn_cast<llvm::GlobalVariable>(GO)) {
2564 if (D->hasAttr<RetainAttr>())
2565 addUsedGlobal(GV);
2566 if (auto *SA = D->getAttr<PragmaClangBSSSectionAttr>())
2567 GV->addAttribute("bss-section", SA->getName());
2568 if (auto *SA = D->getAttr<PragmaClangDataSectionAttr>())
2569 GV->addAttribute("data-section", SA->getName());
2570 if (auto *SA = D->getAttr<PragmaClangRodataSectionAttr>())
2571 GV->addAttribute("rodata-section", SA->getName());
2572 if (auto *SA = D->getAttr<PragmaClangRelroSectionAttr>())
2573 GV->addAttribute("relro-section", SA->getName());
2576 if (auto *F = dyn_cast<llvm::Function>(GO)) {
2577 if (D->hasAttr<RetainAttr>())
2578 addUsedGlobal(F);
2579 if (auto *SA = D->getAttr<PragmaClangTextSectionAttr>())
2580 if (!D->getAttr<SectionAttr>())
2581 F->addFnAttr("implicit-section-name", SA->getName());
2583 llvm::AttrBuilder Attrs(F->getContext());
2584 if (GetCPUAndFeaturesAttributes(GD, Attrs)) {
2585 // We know that GetCPUAndFeaturesAttributes will always have the
2586 // newest set, since it has the newest possible FunctionDecl, so the
2587 // new ones should replace the old.
2588 llvm::AttributeMask RemoveAttrs;
2589 RemoveAttrs.addAttribute("target-cpu");
2590 RemoveAttrs.addAttribute("target-features");
2591 RemoveAttrs.addAttribute("tune-cpu");
2592 F->removeFnAttrs(RemoveAttrs);
2593 F->addFnAttrs(Attrs);
2597 if (const auto *CSA = D->getAttr<CodeSegAttr>())
2598 GO->setSection(CSA->getName());
2599 else if (const auto *SA = D->getAttr<SectionAttr>())
2600 GO->setSection(SA->getName());
2603 getTargetCodeGenInfo().setTargetAttributes(D, GO, *this);
2606 void CodeGenModule::SetInternalFunctionAttributes(GlobalDecl GD,
2607 llvm::Function *F,
2608 const CGFunctionInfo &FI) {
2609 const Decl *D = GD.getDecl();
2610 SetLLVMFunctionAttributes(GD, FI, F, /*IsThunk=*/false);
2611 SetLLVMFunctionAttributesForDefinition(D, F);
2613 F->setLinkage(llvm::Function::InternalLinkage);
2615 setNonAliasAttributes(GD, F);
2618 static void setLinkageForGV(llvm::GlobalValue *GV, const NamedDecl *ND) {
2619 // Set linkage and visibility in case we never see a definition.
2620 LinkageInfo LV = ND->getLinkageAndVisibility();
2621 // Don't set internal linkage on declarations.
2622 // "extern_weak" is overloaded in LLVM; we probably should have
2623 // separate linkage types for this.
2624 if (isExternallyVisible(LV.getLinkage()) &&
2625 (ND->hasAttr<WeakAttr>() || ND->isWeakImported()))
2626 GV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
2629 void CodeGenModule::CreateFunctionTypeMetadataForIcall(const FunctionDecl *FD,
2630 llvm::Function *F) {
2631 // Only if we are checking indirect calls.
2632 if (!LangOpts.Sanitize.has(SanitizerKind::CFIICall))
2633 return;
2635 // Non-static class methods are handled via vtable or member function pointer
2636 // checks elsewhere.
2637 if (isa<CXXMethodDecl>(FD) && !cast<CXXMethodDecl>(FD)->isStatic())
2638 return;
2640 llvm::Metadata *MD = CreateMetadataIdentifierForType(FD->getType());
2641 F->addTypeMetadata(0, MD);
2642 F->addTypeMetadata(0, CreateMetadataIdentifierGeneralized(FD->getType()));
2644 // Emit a hash-based bit set entry for cross-DSO calls.
2645 if (CodeGenOpts.SanitizeCfiCrossDso)
2646 if (auto CrossDsoTypeId = CreateCrossDsoCfiTypeId(MD))
2647 F->addTypeMetadata(0, llvm::ConstantAsMetadata::get(CrossDsoTypeId));
2650 void CodeGenModule::setKCFIType(const FunctionDecl *FD, llvm::Function *F) {
2651 llvm::LLVMContext &Ctx = F->getContext();
2652 llvm::MDBuilder MDB(Ctx);
2653 F->setMetadata(llvm::LLVMContext::MD_kcfi_type,
2654 llvm::MDNode::get(
2655 Ctx, MDB.createConstant(CreateKCFITypeId(FD->getType()))));
2658 static bool allowKCFIIdentifier(StringRef Name) {
2659 // KCFI type identifier constants are only necessary for external assembly
2660 // functions, which means it's safe to skip unusual names. Subset of
2661 // MCAsmInfo::isAcceptableChar() and MCAsmInfoXCOFF::isAcceptableChar().
2662 return llvm::all_of(Name, [](const char &C) {
2663 return llvm::isAlnum(C) || C == '_' || C == '.';
2667 void CodeGenModule::finalizeKCFITypes() {
2668 llvm::Module &M = getModule();
2669 for (auto &F : M.functions()) {
2670 // Remove KCFI type metadata from non-address-taken local functions.
2671 bool AddressTaken = F.hasAddressTaken();
2672 if (!AddressTaken && F.hasLocalLinkage())
2673 F.eraseMetadata(llvm::LLVMContext::MD_kcfi_type);
2675 // Generate a constant with the expected KCFI type identifier for all
2676 // address-taken function declarations to support annotating indirectly
2677 // called assembly functions.
2678 if (!AddressTaken || !F.isDeclaration())
2679 continue;
2681 const llvm::ConstantInt *Type;
2682 if (const llvm::MDNode *MD = F.getMetadata(llvm::LLVMContext::MD_kcfi_type))
2683 Type = llvm::mdconst::extract<llvm::ConstantInt>(MD->getOperand(0));
2684 else
2685 continue;
2687 StringRef Name = F.getName();
2688 if (!allowKCFIIdentifier(Name))
2689 continue;
2691 std::string Asm = (".weak __kcfi_typeid_" + Name + "\n.set __kcfi_typeid_" +
2692 Name + ", " + Twine(Type->getZExtValue()) + "\n")
2693 .str();
2694 M.appendModuleInlineAsm(Asm);
2698 void CodeGenModule::SetFunctionAttributes(GlobalDecl GD, llvm::Function *F,
2699 bool IsIncompleteFunction,
2700 bool IsThunk) {
2702 if (llvm::Intrinsic::ID IID = F->getIntrinsicID()) {
2703 // If this is an intrinsic function, set the function's attributes
2704 // to the intrinsic's attributes.
2705 F->setAttributes(llvm::Intrinsic::getAttributes(getLLVMContext(), IID));
2706 return;
2709 const auto *FD = cast<FunctionDecl>(GD.getDecl());
2711 if (!IsIncompleteFunction)
2712 SetLLVMFunctionAttributes(GD, getTypes().arrangeGlobalDeclaration(GD), F,
2713 IsThunk);
2715 // Add the Returned attribute for "this", except for iOS 5 and earlier
2716 // where substantial code, including the libstdc++ dylib, was compiled with
2717 // GCC and does not actually return "this".
2718 if (!IsThunk && getCXXABI().HasThisReturn(GD) &&
2719 !(getTriple().isiOS() && getTriple().isOSVersionLT(6))) {
2720 assert(!F->arg_empty() &&
2721 F->arg_begin()->getType()
2722 ->canLosslesslyBitCastTo(F->getReturnType()) &&
2723 "unexpected this return");
2724 F->addParamAttr(0, llvm::Attribute::Returned);
2727 // Only a few attributes are set on declarations; these may later be
2728 // overridden by a definition.
2730 setLinkageForGV(F, FD);
2731 setGVProperties(F, FD);
2733 // Setup target-specific attributes.
2734 if (!IsIncompleteFunction && F->isDeclaration())
2735 getTargetCodeGenInfo().setTargetAttributes(FD, F, *this);
2737 if (const auto *CSA = FD->getAttr<CodeSegAttr>())
2738 F->setSection(CSA->getName());
2739 else if (const auto *SA = FD->getAttr<SectionAttr>())
2740 F->setSection(SA->getName());
2742 if (const auto *EA = FD->getAttr<ErrorAttr>()) {
2743 if (EA->isError())
2744 F->addFnAttr("dontcall-error", EA->getUserDiagnostic());
2745 else if (EA->isWarning())
2746 F->addFnAttr("dontcall-warn", EA->getUserDiagnostic());
2749 // If we plan on emitting this inline builtin, we can't treat it as a builtin.
2750 if (FD->isInlineBuiltinDeclaration()) {
2751 const FunctionDecl *FDBody;
2752 bool HasBody = FD->hasBody(FDBody);
2753 (void)HasBody;
2754 assert(HasBody && "Inline builtin declarations should always have an "
2755 "available body!");
2756 if (shouldEmitFunction(FDBody))
2757 F->addFnAttr(llvm::Attribute::NoBuiltin);
2760 if (FD->isReplaceableGlobalAllocationFunction()) {
2761 // A replaceable global allocation function does not act like a builtin by
2762 // default, only if it is invoked by a new-expression or delete-expression.
2763 F->addFnAttr(llvm::Attribute::NoBuiltin);
2766 if (isa<CXXConstructorDecl>(FD) || isa<CXXDestructorDecl>(FD))
2767 F->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
2768 else if (const auto *MD = dyn_cast<CXXMethodDecl>(FD))
2769 if (MD->isVirtual())
2770 F->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
2772 // Don't emit entries for function declarations in the cross-DSO mode. This
2773 // is handled with better precision by the receiving DSO. But if jump tables
2774 // are non-canonical then we need type metadata in order to produce the local
2775 // jump table.
2776 if (!CodeGenOpts.SanitizeCfiCrossDso ||
2777 !CodeGenOpts.SanitizeCfiCanonicalJumpTables)
2778 CreateFunctionTypeMetadataForIcall(FD, F);
2780 if (LangOpts.Sanitize.has(SanitizerKind::KCFI))
2781 setKCFIType(FD, F);
2783 if (getLangOpts().OpenMP && FD->hasAttr<OMPDeclareSimdDeclAttr>())
2784 getOpenMPRuntime().emitDeclareSimdFunction(FD, F);
2786 if (CodeGenOpts.InlineMaxStackSize != UINT_MAX)
2787 F->addFnAttr("inline-max-stacksize", llvm::utostr(CodeGenOpts.InlineMaxStackSize));
2789 if (const auto *CB = FD->getAttr<CallbackAttr>()) {
2790 // Annotate the callback behavior as metadata:
2791 // - The callback callee (as argument number).
2792 // - The callback payloads (as argument numbers).
2793 llvm::LLVMContext &Ctx = F->getContext();
2794 llvm::MDBuilder MDB(Ctx);
2796 // The payload indices are all but the first one in the encoding. The first
2797 // identifies the callback callee.
2798 int CalleeIdx = *CB->encoding_begin();
2799 ArrayRef<int> PayloadIndices(CB->encoding_begin() + 1, CB->encoding_end());
2800 F->addMetadata(llvm::LLVMContext::MD_callback,
2801 *llvm::MDNode::get(Ctx, {MDB.createCallbackEncoding(
2802 CalleeIdx, PayloadIndices,
2803 /* VarArgsArePassed */ false)}));
2807 void CodeGenModule::addUsedGlobal(llvm::GlobalValue *GV) {
2808 assert((isa<llvm::Function>(GV) || !GV->isDeclaration()) &&
2809 "Only globals with definition can force usage.");
2810 LLVMUsed.emplace_back(GV);
2813 void CodeGenModule::addCompilerUsedGlobal(llvm::GlobalValue *GV) {
2814 assert(!GV->isDeclaration() &&
2815 "Only globals with definition can force usage.");
2816 LLVMCompilerUsed.emplace_back(GV);
2819 void CodeGenModule::addUsedOrCompilerUsedGlobal(llvm::GlobalValue *GV) {
2820 assert((isa<llvm::Function>(GV) || !GV->isDeclaration()) &&
2821 "Only globals with definition can force usage.");
2822 if (getTriple().isOSBinFormatELF())
2823 LLVMCompilerUsed.emplace_back(GV);
2824 else
2825 LLVMUsed.emplace_back(GV);
2828 static void emitUsed(CodeGenModule &CGM, StringRef Name,
2829 std::vector<llvm::WeakTrackingVH> &List) {
2830 // Don't create llvm.used if there is no need.
2831 if (List.empty())
2832 return;
2834 // Convert List to what ConstantArray needs.
2835 SmallVector<llvm::Constant*, 8> UsedArray;
2836 UsedArray.resize(List.size());
2837 for (unsigned i = 0, e = List.size(); i != e; ++i) {
2838 UsedArray[i] =
2839 llvm::ConstantExpr::getPointerBitCastOrAddrSpaceCast(
2840 cast<llvm::Constant>(&*List[i]), CGM.Int8PtrTy);
2843 if (UsedArray.empty())
2844 return;
2845 llvm::ArrayType *ATy = llvm::ArrayType::get(CGM.Int8PtrTy, UsedArray.size());
2847 auto *GV = new llvm::GlobalVariable(
2848 CGM.getModule(), ATy, false, llvm::GlobalValue::AppendingLinkage,
2849 llvm::ConstantArray::get(ATy, UsedArray), Name);
2851 GV->setSection("llvm.metadata");
2854 void CodeGenModule::emitLLVMUsed() {
2855 emitUsed(*this, "llvm.used", LLVMUsed);
2856 emitUsed(*this, "llvm.compiler.used", LLVMCompilerUsed);
2859 void CodeGenModule::AppendLinkerOptions(StringRef Opts) {
2860 auto *MDOpts = llvm::MDString::get(getLLVMContext(), Opts);
2861 LinkerOptionsMetadata.push_back(llvm::MDNode::get(getLLVMContext(), MDOpts));
2864 void CodeGenModule::AddDetectMismatch(StringRef Name, StringRef Value) {
2865 llvm::SmallString<32> Opt;
2866 getTargetCodeGenInfo().getDetectMismatchOption(Name, Value, Opt);
2867 if (Opt.empty())
2868 return;
2869 auto *MDOpts = llvm::MDString::get(getLLVMContext(), Opt);
2870 LinkerOptionsMetadata.push_back(llvm::MDNode::get(getLLVMContext(), MDOpts));
2873 void CodeGenModule::AddDependentLib(StringRef Lib) {
2874 auto &C = getLLVMContext();
2875 if (getTarget().getTriple().isOSBinFormatELF()) {
2876 ELFDependentLibraries.push_back(
2877 llvm::MDNode::get(C, llvm::MDString::get(C, Lib)));
2878 return;
2881 llvm::SmallString<24> Opt;
2882 getTargetCodeGenInfo().getDependentLibraryOption(Lib, Opt);
2883 auto *MDOpts = llvm::MDString::get(getLLVMContext(), Opt);
2884 LinkerOptionsMetadata.push_back(llvm::MDNode::get(C, MDOpts));
2887 /// Add link options implied by the given module, including modules
2888 /// it depends on, using a postorder walk.
2889 static void addLinkOptionsPostorder(CodeGenModule &CGM, Module *Mod,
2890 SmallVectorImpl<llvm::MDNode *> &Metadata,
2891 llvm::SmallPtrSet<Module *, 16> &Visited) {
2892 // Import this module's parent.
2893 if (Mod->Parent && Visited.insert(Mod->Parent).second) {
2894 addLinkOptionsPostorder(CGM, Mod->Parent, Metadata, Visited);
2897 // Import this module's dependencies.
2898 for (Module *Import : llvm::reverse(Mod->Imports)) {
2899 if (Visited.insert(Import).second)
2900 addLinkOptionsPostorder(CGM, Import, Metadata, Visited);
2903 // Add linker options to link against the libraries/frameworks
2904 // described by this module.
2905 llvm::LLVMContext &Context = CGM.getLLVMContext();
2906 bool IsELF = CGM.getTarget().getTriple().isOSBinFormatELF();
2908 // For modules that use export_as for linking, use that module
2909 // name instead.
2910 if (Mod->UseExportAsModuleLinkName)
2911 return;
2913 for (const Module::LinkLibrary &LL : llvm::reverse(Mod->LinkLibraries)) {
2914 // Link against a framework. Frameworks are currently Darwin only, so we
2915 // don't to ask TargetCodeGenInfo for the spelling of the linker option.
2916 if (LL.IsFramework) {
2917 llvm::Metadata *Args[2] = {llvm::MDString::get(Context, "-framework"),
2918 llvm::MDString::get(Context, LL.Library)};
2920 Metadata.push_back(llvm::MDNode::get(Context, Args));
2921 continue;
2924 // Link against a library.
2925 if (IsELF) {
2926 llvm::Metadata *Args[2] = {
2927 llvm::MDString::get(Context, "lib"),
2928 llvm::MDString::get(Context, LL.Library),
2930 Metadata.push_back(llvm::MDNode::get(Context, Args));
2931 } else {
2932 llvm::SmallString<24> Opt;
2933 CGM.getTargetCodeGenInfo().getDependentLibraryOption(LL.Library, Opt);
2934 auto *OptString = llvm::MDString::get(Context, Opt);
2935 Metadata.push_back(llvm::MDNode::get(Context, OptString));
2940 void CodeGenModule::EmitModuleInitializers(clang::Module *Primary) {
2941 assert(Primary->isNamedModuleUnit() &&
2942 "We should only emit module initializers for named modules.");
2944 // Emit the initializers in the order that sub-modules appear in the
2945 // source, first Global Module Fragments, if present.
2946 if (auto GMF = Primary->getGlobalModuleFragment()) {
2947 for (Decl *D : getContext().getModuleInitializers(GMF)) {
2948 if (isa<ImportDecl>(D))
2949 continue;
2950 assert(isa<VarDecl>(D) && "GMF initializer decl is not a var?");
2951 EmitTopLevelDecl(D);
2954 // Second any associated with the module, itself.
2955 for (Decl *D : getContext().getModuleInitializers(Primary)) {
2956 // Skip import decls, the inits for those are called explicitly.
2957 if (isa<ImportDecl>(D))
2958 continue;
2959 EmitTopLevelDecl(D);
2961 // Third any associated with the Privat eMOdule Fragment, if present.
2962 if (auto PMF = Primary->getPrivateModuleFragment()) {
2963 for (Decl *D : getContext().getModuleInitializers(PMF)) {
2964 // Skip import decls, the inits for those are called explicitly.
2965 if (isa<ImportDecl>(D))
2966 continue;
2967 assert(isa<VarDecl>(D) && "PMF initializer decl is not a var?");
2968 EmitTopLevelDecl(D);
2973 void CodeGenModule::EmitModuleLinkOptions() {
2974 // Collect the set of all of the modules we want to visit to emit link
2975 // options, which is essentially the imported modules and all of their
2976 // non-explicit child modules.
2977 llvm::SetVector<clang::Module *> LinkModules;
2978 llvm::SmallPtrSet<clang::Module *, 16> Visited;
2979 SmallVector<clang::Module *, 16> Stack;
2981 // Seed the stack with imported modules.
2982 for (Module *M : ImportedModules) {
2983 // Do not add any link flags when an implementation TU of a module imports
2984 // a header of that same module.
2985 if (M->getTopLevelModuleName() == getLangOpts().CurrentModule &&
2986 !getLangOpts().isCompilingModule())
2987 continue;
2988 if (Visited.insert(M).second)
2989 Stack.push_back(M);
2992 // Find all of the modules to import, making a little effort to prune
2993 // non-leaf modules.
2994 while (!Stack.empty()) {
2995 clang::Module *Mod = Stack.pop_back_val();
2997 bool AnyChildren = false;
2999 // Visit the submodules of this module.
3000 for (const auto &SM : Mod->submodules()) {
3001 // Skip explicit children; they need to be explicitly imported to be
3002 // linked against.
3003 if (SM->IsExplicit)
3004 continue;
3006 if (Visited.insert(SM).second) {
3007 Stack.push_back(SM);
3008 AnyChildren = true;
3012 // We didn't find any children, so add this module to the list of
3013 // modules to link against.
3014 if (!AnyChildren) {
3015 LinkModules.insert(Mod);
3019 // Add link options for all of the imported modules in reverse topological
3020 // order. We don't do anything to try to order import link flags with respect
3021 // to linker options inserted by things like #pragma comment().
3022 SmallVector<llvm::MDNode *, 16> MetadataArgs;
3023 Visited.clear();
3024 for (Module *M : LinkModules)
3025 if (Visited.insert(M).second)
3026 addLinkOptionsPostorder(*this, M, MetadataArgs, Visited);
3027 std::reverse(MetadataArgs.begin(), MetadataArgs.end());
3028 LinkerOptionsMetadata.append(MetadataArgs.begin(), MetadataArgs.end());
3030 // Add the linker options metadata flag.
3031 auto *NMD = getModule().getOrInsertNamedMetadata("llvm.linker.options");
3032 for (auto *MD : LinkerOptionsMetadata)
3033 NMD->addOperand(MD);
3036 void CodeGenModule::EmitDeferred() {
3037 // Emit deferred declare target declarations.
3038 if (getLangOpts().OpenMP && !getLangOpts().OpenMPSimd)
3039 getOpenMPRuntime().emitDeferredTargetDecls();
3041 // Emit code for any potentially referenced deferred decls. Since a
3042 // previously unused static decl may become used during the generation of code
3043 // for a static function, iterate until no changes are made.
3045 if (!DeferredVTables.empty()) {
3046 EmitDeferredVTables();
3048 // Emitting a vtable doesn't directly cause more vtables to
3049 // become deferred, although it can cause functions to be
3050 // emitted that then need those vtables.
3051 assert(DeferredVTables.empty());
3054 // Emit CUDA/HIP static device variables referenced by host code only.
3055 // Note we should not clear CUDADeviceVarODRUsedByHost since it is still
3056 // needed for further handling.
3057 if (getLangOpts().CUDA && getLangOpts().CUDAIsDevice)
3058 llvm::append_range(DeferredDeclsToEmit,
3059 getContext().CUDADeviceVarODRUsedByHost);
3061 // Stop if we're out of both deferred vtables and deferred declarations.
3062 if (DeferredDeclsToEmit.empty())
3063 return;
3065 // Grab the list of decls to emit. If EmitGlobalDefinition schedules more
3066 // work, it will not interfere with this.
3067 std::vector<GlobalDecl> CurDeclsToEmit;
3068 CurDeclsToEmit.swap(DeferredDeclsToEmit);
3070 for (GlobalDecl &D : CurDeclsToEmit) {
3071 // We should call GetAddrOfGlobal with IsForDefinition set to true in order
3072 // to get GlobalValue with exactly the type we need, not something that
3073 // might had been created for another decl with the same mangled name but
3074 // different type.
3075 llvm::GlobalValue *GV = dyn_cast<llvm::GlobalValue>(
3076 GetAddrOfGlobal(D, ForDefinition));
3078 // In case of different address spaces, we may still get a cast, even with
3079 // IsForDefinition equal to true. Query mangled names table to get
3080 // GlobalValue.
3081 if (!GV)
3082 GV = GetGlobalValue(getMangledName(D));
3084 // Make sure GetGlobalValue returned non-null.
3085 assert(GV);
3087 // Check to see if we've already emitted this. This is necessary
3088 // for a couple of reasons: first, decls can end up in the
3089 // deferred-decls queue multiple times, and second, decls can end
3090 // up with definitions in unusual ways (e.g. by an extern inline
3091 // function acquiring a strong function redefinition). Just
3092 // ignore these cases.
3093 if (!GV->isDeclaration())
3094 continue;
3096 // If this is OpenMP, check if it is legal to emit this global normally.
3097 if (LangOpts.OpenMP && OpenMPRuntime && OpenMPRuntime->emitTargetGlobal(D))
3098 continue;
3100 // Otherwise, emit the definition and move on to the next one.
3101 EmitGlobalDefinition(D, GV);
3103 // If we found out that we need to emit more decls, do that recursively.
3104 // This has the advantage that the decls are emitted in a DFS and related
3105 // ones are close together, which is convenient for testing.
3106 if (!DeferredVTables.empty() || !DeferredDeclsToEmit.empty()) {
3107 EmitDeferred();
3108 assert(DeferredVTables.empty() && DeferredDeclsToEmit.empty());
3113 void CodeGenModule::EmitVTablesOpportunistically() {
3114 // Try to emit external vtables as available_externally if they have emitted
3115 // all inlined virtual functions. It runs after EmitDeferred() and therefore
3116 // is not allowed to create new references to things that need to be emitted
3117 // lazily. Note that it also uses fact that we eagerly emitting RTTI.
3119 assert((OpportunisticVTables.empty() || shouldOpportunisticallyEmitVTables())
3120 && "Only emit opportunistic vtables with optimizations");
3122 for (const CXXRecordDecl *RD : OpportunisticVTables) {
3123 assert(getVTables().isVTableExternal(RD) &&
3124 "This queue should only contain external vtables");
3125 if (getCXXABI().canSpeculativelyEmitVTable(RD))
3126 VTables.GenerateClassData(RD);
3128 OpportunisticVTables.clear();
3131 void CodeGenModule::EmitGlobalAnnotations() {
3132 if (Annotations.empty())
3133 return;
3135 // Create a new global variable for the ConstantStruct in the Module.
3136 llvm::Constant *Array = llvm::ConstantArray::get(llvm::ArrayType::get(
3137 Annotations[0]->getType(), Annotations.size()), Annotations);
3138 auto *gv = new llvm::GlobalVariable(getModule(), Array->getType(), false,
3139 llvm::GlobalValue::AppendingLinkage,
3140 Array, "llvm.global.annotations");
3141 gv->setSection(AnnotationSection);
3144 llvm::Constant *CodeGenModule::EmitAnnotationString(StringRef Str) {
3145 llvm::Constant *&AStr = AnnotationStrings[Str];
3146 if (AStr)
3147 return AStr;
3149 // Not found yet, create a new global.
3150 llvm::Constant *s = llvm::ConstantDataArray::getString(getLLVMContext(), Str);
3151 auto *gv = new llvm::GlobalVariable(
3152 getModule(), s->getType(), true, llvm::GlobalValue::PrivateLinkage, s,
3153 ".str", nullptr, llvm::GlobalValue::NotThreadLocal,
3154 ConstGlobalsPtrTy->getAddressSpace());
3155 gv->setSection(AnnotationSection);
3156 gv->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
3157 AStr = gv;
3158 return gv;
3161 llvm::Constant *CodeGenModule::EmitAnnotationUnit(SourceLocation Loc) {
3162 SourceManager &SM = getContext().getSourceManager();
3163 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
3164 if (PLoc.isValid())
3165 return EmitAnnotationString(PLoc.getFilename());
3166 return EmitAnnotationString(SM.getBufferName(Loc));
3169 llvm::Constant *CodeGenModule::EmitAnnotationLineNo(SourceLocation L) {
3170 SourceManager &SM = getContext().getSourceManager();
3171 PresumedLoc PLoc = SM.getPresumedLoc(L);
3172 unsigned LineNo = PLoc.isValid() ? PLoc.getLine() :
3173 SM.getExpansionLineNumber(L);
3174 return llvm::ConstantInt::get(Int32Ty, LineNo);
3177 llvm::Constant *CodeGenModule::EmitAnnotationArgs(const AnnotateAttr *Attr) {
3178 ArrayRef<Expr *> Exprs = {Attr->args_begin(), Attr->args_size()};
3179 if (Exprs.empty())
3180 return llvm::ConstantPointerNull::get(ConstGlobalsPtrTy);
3182 llvm::FoldingSetNodeID ID;
3183 for (Expr *E : Exprs) {
3184 ID.Add(cast<clang::ConstantExpr>(E)->getAPValueResult());
3186 llvm::Constant *&Lookup = AnnotationArgs[ID.ComputeHash()];
3187 if (Lookup)
3188 return Lookup;
3190 llvm::SmallVector<llvm::Constant *, 4> LLVMArgs;
3191 LLVMArgs.reserve(Exprs.size());
3192 ConstantEmitter ConstEmiter(*this);
3193 llvm::transform(Exprs, std::back_inserter(LLVMArgs), [&](const Expr *E) {
3194 const auto *CE = cast<clang::ConstantExpr>(E);
3195 return ConstEmiter.emitAbstract(CE->getBeginLoc(), CE->getAPValueResult(),
3196 CE->getType());
3198 auto *Struct = llvm::ConstantStruct::getAnon(LLVMArgs);
3199 auto *GV = new llvm::GlobalVariable(getModule(), Struct->getType(), true,
3200 llvm::GlobalValue::PrivateLinkage, Struct,
3201 ".args");
3202 GV->setSection(AnnotationSection);
3203 GV->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
3204 auto *Bitcasted = llvm::ConstantExpr::getBitCast(GV, GlobalsInt8PtrTy);
3206 Lookup = Bitcasted;
3207 return Bitcasted;
3210 llvm::Constant *CodeGenModule::EmitAnnotateAttr(llvm::GlobalValue *GV,
3211 const AnnotateAttr *AA,
3212 SourceLocation L) {
3213 // Get the globals for file name, annotation, and the line number.
3214 llvm::Constant *AnnoGV = EmitAnnotationString(AA->getAnnotation()),
3215 *UnitGV = EmitAnnotationUnit(L),
3216 *LineNoCst = EmitAnnotationLineNo(L),
3217 *Args = EmitAnnotationArgs(AA);
3219 llvm::Constant *GVInGlobalsAS = GV;
3220 if (GV->getAddressSpace() !=
3221 getDataLayout().getDefaultGlobalsAddressSpace()) {
3222 GVInGlobalsAS = llvm::ConstantExpr::getAddrSpaceCast(
3224 llvm::PointerType::get(
3225 GV->getContext(), getDataLayout().getDefaultGlobalsAddressSpace()));
3228 // Create the ConstantStruct for the global annotation.
3229 llvm::Constant *Fields[] = {
3230 llvm::ConstantExpr::getBitCast(GVInGlobalsAS, GlobalsInt8PtrTy),
3231 llvm::ConstantExpr::getBitCast(AnnoGV, ConstGlobalsPtrTy),
3232 llvm::ConstantExpr::getBitCast(UnitGV, ConstGlobalsPtrTy),
3233 LineNoCst,
3234 Args,
3236 return llvm::ConstantStruct::getAnon(Fields);
3239 void CodeGenModule::AddGlobalAnnotations(const ValueDecl *D,
3240 llvm::GlobalValue *GV) {
3241 assert(D->hasAttr<AnnotateAttr>() && "no annotate attribute");
3242 // Get the struct elements for these annotations.
3243 for (const auto *I : D->specific_attrs<AnnotateAttr>())
3244 Annotations.push_back(EmitAnnotateAttr(GV, I, D->getLocation()));
3247 bool CodeGenModule::isInNoSanitizeList(SanitizerMask Kind, llvm::Function *Fn,
3248 SourceLocation Loc) const {
3249 const auto &NoSanitizeL = getContext().getNoSanitizeList();
3250 // NoSanitize by function name.
3251 if (NoSanitizeL.containsFunction(Kind, Fn->getName()))
3252 return true;
3253 // NoSanitize by location. Check "mainfile" prefix.
3254 auto &SM = Context.getSourceManager();
3255 FileEntryRef MainFile = *SM.getFileEntryRefForID(SM.getMainFileID());
3256 if (NoSanitizeL.containsMainFile(Kind, MainFile.getName()))
3257 return true;
3259 // Check "src" prefix.
3260 if (Loc.isValid())
3261 return NoSanitizeL.containsLocation(Kind, Loc);
3262 // If location is unknown, this may be a compiler-generated function. Assume
3263 // it's located in the main file.
3264 return NoSanitizeL.containsFile(Kind, MainFile.getName());
3267 bool CodeGenModule::isInNoSanitizeList(SanitizerMask Kind,
3268 llvm::GlobalVariable *GV,
3269 SourceLocation Loc, QualType Ty,
3270 StringRef Category) const {
3271 const auto &NoSanitizeL = getContext().getNoSanitizeList();
3272 if (NoSanitizeL.containsGlobal(Kind, GV->getName(), Category))
3273 return true;
3274 auto &SM = Context.getSourceManager();
3275 if (NoSanitizeL.containsMainFile(
3276 Kind, SM.getFileEntryRefForID(SM.getMainFileID())->getName(),
3277 Category))
3278 return true;
3279 if (NoSanitizeL.containsLocation(Kind, Loc, Category))
3280 return true;
3282 // Check global type.
3283 if (!Ty.isNull()) {
3284 // Drill down the array types: if global variable of a fixed type is
3285 // not sanitized, we also don't instrument arrays of them.
3286 while (auto AT = dyn_cast<ArrayType>(Ty.getTypePtr()))
3287 Ty = AT->getElementType();
3288 Ty = Ty.getCanonicalType().getUnqualifiedType();
3289 // Only record types (classes, structs etc.) are ignored.
3290 if (Ty->isRecordType()) {
3291 std::string TypeStr = Ty.getAsString(getContext().getPrintingPolicy());
3292 if (NoSanitizeL.containsType(Kind, TypeStr, Category))
3293 return true;
3296 return false;
3299 bool CodeGenModule::imbueXRayAttrs(llvm::Function *Fn, SourceLocation Loc,
3300 StringRef Category) const {
3301 const auto &XRayFilter = getContext().getXRayFilter();
3302 using ImbueAttr = XRayFunctionFilter::ImbueAttribute;
3303 auto Attr = ImbueAttr::NONE;
3304 if (Loc.isValid())
3305 Attr = XRayFilter.shouldImbueLocation(Loc, Category);
3306 if (Attr == ImbueAttr::NONE)
3307 Attr = XRayFilter.shouldImbueFunction(Fn->getName());
3308 switch (Attr) {
3309 case ImbueAttr::NONE:
3310 return false;
3311 case ImbueAttr::ALWAYS:
3312 Fn->addFnAttr("function-instrument", "xray-always");
3313 break;
3314 case ImbueAttr::ALWAYS_ARG1:
3315 Fn->addFnAttr("function-instrument", "xray-always");
3316 Fn->addFnAttr("xray-log-args", "1");
3317 break;
3318 case ImbueAttr::NEVER:
3319 Fn->addFnAttr("function-instrument", "xray-never");
3320 break;
3322 return true;
3325 ProfileList::ExclusionType
3326 CodeGenModule::isFunctionBlockedByProfileList(llvm::Function *Fn,
3327 SourceLocation Loc) const {
3328 const auto &ProfileList = getContext().getProfileList();
3329 // If the profile list is empty, then instrument everything.
3330 if (ProfileList.isEmpty())
3331 return ProfileList::Allow;
3332 CodeGenOptions::ProfileInstrKind Kind = getCodeGenOpts().getProfileInstr();
3333 // First, check the function name.
3334 if (auto V = ProfileList.isFunctionExcluded(Fn->getName(), Kind))
3335 return *V;
3336 // Next, check the source location.
3337 if (Loc.isValid())
3338 if (auto V = ProfileList.isLocationExcluded(Loc, Kind))
3339 return *V;
3340 // If location is unknown, this may be a compiler-generated function. Assume
3341 // it's located in the main file.
3342 auto &SM = Context.getSourceManager();
3343 if (auto MainFile = SM.getFileEntryRefForID(SM.getMainFileID()))
3344 if (auto V = ProfileList.isFileExcluded(MainFile->getName(), Kind))
3345 return *V;
3346 return ProfileList.getDefault(Kind);
3349 ProfileList::ExclusionType
3350 CodeGenModule::isFunctionBlockedFromProfileInstr(llvm::Function *Fn,
3351 SourceLocation Loc) const {
3352 auto V = isFunctionBlockedByProfileList(Fn, Loc);
3353 if (V != ProfileList::Allow)
3354 return V;
3356 auto NumGroups = getCodeGenOpts().ProfileTotalFunctionGroups;
3357 if (NumGroups > 1) {
3358 auto Group = llvm::crc32(arrayRefFromStringRef(Fn->getName())) % NumGroups;
3359 if (Group != getCodeGenOpts().ProfileSelectedFunctionGroup)
3360 return ProfileList::Skip;
3362 return ProfileList::Allow;
3365 bool CodeGenModule::MustBeEmitted(const ValueDecl *Global) {
3366 // Never defer when EmitAllDecls is specified.
3367 if (LangOpts.EmitAllDecls)
3368 return true;
3370 const auto *VD = dyn_cast<VarDecl>(Global);
3371 if (VD &&
3372 ((CodeGenOpts.KeepPersistentStorageVariables &&
3373 (VD->getStorageDuration() == SD_Static ||
3374 VD->getStorageDuration() == SD_Thread)) ||
3375 (CodeGenOpts.KeepStaticConsts && VD->getStorageDuration() == SD_Static &&
3376 VD->getType().isConstQualified())))
3377 return true;
3379 return getContext().DeclMustBeEmitted(Global);
3382 bool CodeGenModule::MayBeEmittedEagerly(const ValueDecl *Global) {
3383 // In OpenMP 5.0 variables and function may be marked as
3384 // device_type(host/nohost) and we should not emit them eagerly unless we sure
3385 // that they must be emitted on the host/device. To be sure we need to have
3386 // seen a declare target with an explicit mentioning of the function, we know
3387 // we have if the level of the declare target attribute is -1. Note that we
3388 // check somewhere else if we should emit this at all.
3389 if (LangOpts.OpenMP >= 50 && !LangOpts.OpenMPSimd) {
3390 std::optional<OMPDeclareTargetDeclAttr *> ActiveAttr =
3391 OMPDeclareTargetDeclAttr::getActiveAttr(Global);
3392 if (!ActiveAttr || (*ActiveAttr)->getLevel() != (unsigned)-1)
3393 return false;
3396 if (const auto *FD = dyn_cast<FunctionDecl>(Global)) {
3397 if (FD->getTemplateSpecializationKind() == TSK_ImplicitInstantiation)
3398 // Implicit template instantiations may change linkage if they are later
3399 // explicitly instantiated, so they should not be emitted eagerly.
3400 return false;
3402 if (const auto *VD = dyn_cast<VarDecl>(Global)) {
3403 if (Context.getInlineVariableDefinitionKind(VD) ==
3404 ASTContext::InlineVariableDefinitionKind::WeakUnknown)
3405 // A definition of an inline constexpr static data member may change
3406 // linkage later if it's redeclared outside the class.
3407 return false;
3408 if (CXX20ModuleInits && VD->getOwningModule() &&
3409 !VD->getOwningModule()->isModuleMapModule()) {
3410 // For CXX20, module-owned initializers need to be deferred, since it is
3411 // not known at this point if they will be run for the current module or
3412 // as part of the initializer for an imported one.
3413 return false;
3416 // If OpenMP is enabled and threadprivates must be generated like TLS, delay
3417 // codegen for global variables, because they may be marked as threadprivate.
3418 if (LangOpts.OpenMP && LangOpts.OpenMPUseTLS &&
3419 getContext().getTargetInfo().isTLSSupported() && isa<VarDecl>(Global) &&
3420 !Global->getType().isConstantStorage(getContext(), false, false) &&
3421 !OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(Global))
3422 return false;
3424 return true;
3427 ConstantAddress CodeGenModule::GetAddrOfMSGuidDecl(const MSGuidDecl *GD) {
3428 StringRef Name = getMangledName(GD);
3430 // The UUID descriptor should be pointer aligned.
3431 CharUnits Alignment = CharUnits::fromQuantity(PointerAlignInBytes);
3433 // Look for an existing global.
3434 if (llvm::GlobalVariable *GV = getModule().getNamedGlobal(Name))
3435 return ConstantAddress(GV, GV->getValueType(), Alignment);
3437 ConstantEmitter Emitter(*this);
3438 llvm::Constant *Init;
3440 APValue &V = GD->getAsAPValue();
3441 if (!V.isAbsent()) {
3442 // If possible, emit the APValue version of the initializer. In particular,
3443 // this gets the type of the constant right.
3444 Init = Emitter.emitForInitializer(
3445 GD->getAsAPValue(), GD->getType().getAddressSpace(), GD->getType());
3446 } else {
3447 // As a fallback, directly construct the constant.
3448 // FIXME: This may get padding wrong under esoteric struct layout rules.
3449 // MSVC appears to create a complete type 'struct __s_GUID' that it
3450 // presumably uses to represent these constants.
3451 MSGuidDecl::Parts Parts = GD->getParts();
3452 llvm::Constant *Fields[4] = {
3453 llvm::ConstantInt::get(Int32Ty, Parts.Part1),
3454 llvm::ConstantInt::get(Int16Ty, Parts.Part2),
3455 llvm::ConstantInt::get(Int16Ty, Parts.Part3),
3456 llvm::ConstantDataArray::getRaw(
3457 StringRef(reinterpret_cast<char *>(Parts.Part4And5), 8), 8,
3458 Int8Ty)};
3459 Init = llvm::ConstantStruct::getAnon(Fields);
3462 auto *GV = new llvm::GlobalVariable(
3463 getModule(), Init->getType(),
3464 /*isConstant=*/true, llvm::GlobalValue::LinkOnceODRLinkage, Init, Name);
3465 if (supportsCOMDAT())
3466 GV->setComdat(TheModule.getOrInsertComdat(GV->getName()));
3467 setDSOLocal(GV);
3469 if (!V.isAbsent()) {
3470 Emitter.finalize(GV);
3471 return ConstantAddress(GV, GV->getValueType(), Alignment);
3474 llvm::Type *Ty = getTypes().ConvertTypeForMem(GD->getType());
3475 return ConstantAddress(GV, Ty, Alignment);
3478 ConstantAddress CodeGenModule::GetAddrOfUnnamedGlobalConstantDecl(
3479 const UnnamedGlobalConstantDecl *GCD) {
3480 CharUnits Alignment = getContext().getTypeAlignInChars(GCD->getType());
3482 llvm::GlobalVariable **Entry = nullptr;
3483 Entry = &UnnamedGlobalConstantDeclMap[GCD];
3484 if (*Entry)
3485 return ConstantAddress(*Entry, (*Entry)->getValueType(), Alignment);
3487 ConstantEmitter Emitter(*this);
3488 llvm::Constant *Init;
3490 const APValue &V = GCD->getValue();
3492 assert(!V.isAbsent());
3493 Init = Emitter.emitForInitializer(V, GCD->getType().getAddressSpace(),
3494 GCD->getType());
3496 auto *GV = new llvm::GlobalVariable(getModule(), Init->getType(),
3497 /*isConstant=*/true,
3498 llvm::GlobalValue::PrivateLinkage, Init,
3499 ".constant");
3500 GV->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
3501 GV->setAlignment(Alignment.getAsAlign());
3503 Emitter.finalize(GV);
3505 *Entry = GV;
3506 return ConstantAddress(GV, GV->getValueType(), Alignment);
3509 ConstantAddress CodeGenModule::GetAddrOfTemplateParamObject(
3510 const TemplateParamObjectDecl *TPO) {
3511 StringRef Name = getMangledName(TPO);
3512 CharUnits Alignment = getNaturalTypeAlignment(TPO->getType());
3514 if (llvm::GlobalVariable *GV = getModule().getNamedGlobal(Name))
3515 return ConstantAddress(GV, GV->getValueType(), Alignment);
3517 ConstantEmitter Emitter(*this);
3518 llvm::Constant *Init = Emitter.emitForInitializer(
3519 TPO->getValue(), TPO->getType().getAddressSpace(), TPO->getType());
3521 if (!Init) {
3522 ErrorUnsupported(TPO, "template parameter object");
3523 return ConstantAddress::invalid();
3526 llvm::GlobalValue::LinkageTypes Linkage =
3527 isExternallyVisible(TPO->getLinkageAndVisibility().getLinkage())
3528 ? llvm::GlobalValue::LinkOnceODRLinkage
3529 : llvm::GlobalValue::InternalLinkage;
3530 auto *GV = new llvm::GlobalVariable(getModule(), Init->getType(),
3531 /*isConstant=*/true, Linkage, Init, Name);
3532 setGVProperties(GV, TPO);
3533 if (supportsCOMDAT())
3534 GV->setComdat(TheModule.getOrInsertComdat(GV->getName()));
3535 Emitter.finalize(GV);
3537 return ConstantAddress(GV, GV->getValueType(), Alignment);
3540 ConstantAddress CodeGenModule::GetWeakRefReference(const ValueDecl *VD) {
3541 const AliasAttr *AA = VD->getAttr<AliasAttr>();
3542 assert(AA && "No alias?");
3544 CharUnits Alignment = getContext().getDeclAlign(VD);
3545 llvm::Type *DeclTy = getTypes().ConvertTypeForMem(VD->getType());
3547 // See if there is already something with the target's name in the module.
3548 llvm::GlobalValue *Entry = GetGlobalValue(AA->getAliasee());
3549 if (Entry)
3550 return ConstantAddress(Entry, DeclTy, Alignment);
3552 llvm::Constant *Aliasee;
3553 if (isa<llvm::FunctionType>(DeclTy))
3554 Aliasee = GetOrCreateLLVMFunction(AA->getAliasee(), DeclTy,
3555 GlobalDecl(cast<FunctionDecl>(VD)),
3556 /*ForVTable=*/false);
3557 else
3558 Aliasee = GetOrCreateLLVMGlobal(AA->getAliasee(), DeclTy, LangAS::Default,
3559 nullptr);
3561 auto *F = cast<llvm::GlobalValue>(Aliasee);
3562 F->setLinkage(llvm::Function::ExternalWeakLinkage);
3563 WeakRefReferences.insert(F);
3565 return ConstantAddress(Aliasee, DeclTy, Alignment);
3568 void CodeGenModule::EmitGlobal(GlobalDecl GD) {
3569 const auto *Global = cast<ValueDecl>(GD.getDecl());
3571 // Weak references don't produce any output by themselves.
3572 if (Global->hasAttr<WeakRefAttr>())
3573 return;
3575 // If this is an alias definition (which otherwise looks like a declaration)
3576 // emit it now.
3577 if (Global->hasAttr<AliasAttr>())
3578 return EmitAliasDefinition(GD);
3580 // IFunc like an alias whose value is resolved at runtime by calling resolver.
3581 if (Global->hasAttr<IFuncAttr>())
3582 return emitIFuncDefinition(GD);
3584 // If this is a cpu_dispatch multiversion function, emit the resolver.
3585 if (Global->hasAttr<CPUDispatchAttr>())
3586 return emitCPUDispatchDefinition(GD);
3588 // If this is CUDA, be selective about which declarations we emit.
3589 if (LangOpts.CUDA) {
3590 if (LangOpts.CUDAIsDevice) {
3591 if (!Global->hasAttr<CUDADeviceAttr>() &&
3592 !Global->hasAttr<CUDAGlobalAttr>() &&
3593 !Global->hasAttr<CUDAConstantAttr>() &&
3594 !Global->hasAttr<CUDASharedAttr>() &&
3595 !Global->getType()->isCUDADeviceBuiltinSurfaceType() &&
3596 !Global->getType()->isCUDADeviceBuiltinTextureType() &&
3597 !(LangOpts.HIPStdPar &&
3598 isa<FunctionDecl>(Global) &&
3599 !Global->hasAttr<CUDAHostAttr>()))
3600 return;
3601 } else {
3602 // We need to emit host-side 'shadows' for all global
3603 // device-side variables because the CUDA runtime needs their
3604 // size and host-side address in order to provide access to
3605 // their device-side incarnations.
3607 // So device-only functions are the only things we skip.
3608 if (isa<FunctionDecl>(Global) && !Global->hasAttr<CUDAHostAttr>() &&
3609 Global->hasAttr<CUDADeviceAttr>())
3610 return;
3612 assert((isa<FunctionDecl>(Global) || isa<VarDecl>(Global)) &&
3613 "Expected Variable or Function");
3617 if (LangOpts.OpenMP) {
3618 // If this is OpenMP, check if it is legal to emit this global normally.
3619 if (OpenMPRuntime && OpenMPRuntime->emitTargetGlobal(GD))
3620 return;
3621 if (auto *DRD = dyn_cast<OMPDeclareReductionDecl>(Global)) {
3622 if (MustBeEmitted(Global))
3623 EmitOMPDeclareReduction(DRD);
3624 return;
3626 if (auto *DMD = dyn_cast<OMPDeclareMapperDecl>(Global)) {
3627 if (MustBeEmitted(Global))
3628 EmitOMPDeclareMapper(DMD);
3629 return;
3633 // Ignore declarations, they will be emitted on their first use.
3634 if (const auto *FD = dyn_cast<FunctionDecl>(Global)) {
3635 // Forward declarations are emitted lazily on first use.
3636 if (!FD->doesThisDeclarationHaveABody()) {
3637 if (!FD->doesDeclarationForceExternallyVisibleDefinition())
3638 return;
3640 StringRef MangledName = getMangledName(GD);
3642 // Compute the function info and LLVM type.
3643 const CGFunctionInfo &FI = getTypes().arrangeGlobalDeclaration(GD);
3644 llvm::Type *Ty = getTypes().GetFunctionType(FI);
3646 GetOrCreateLLVMFunction(MangledName, Ty, GD, /*ForVTable=*/false,
3647 /*DontDefer=*/false);
3648 return;
3650 } else {
3651 const auto *VD = cast<VarDecl>(Global);
3652 assert(VD->isFileVarDecl() && "Cannot emit local var decl as global.");
3653 if (VD->isThisDeclarationADefinition() != VarDecl::Definition &&
3654 !Context.isMSStaticDataMemberInlineDefinition(VD)) {
3655 if (LangOpts.OpenMP) {
3656 // Emit declaration of the must-be-emitted declare target variable.
3657 if (std::optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res =
3658 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD)) {
3660 // If this variable has external storage and doesn't require special
3661 // link handling we defer to its canonical definition.
3662 if (VD->hasExternalStorage() &&
3663 Res != OMPDeclareTargetDeclAttr::MT_Link)
3664 return;
3666 bool UnifiedMemoryEnabled =
3667 getOpenMPRuntime().hasRequiresUnifiedSharedMemory();
3668 if ((*Res == OMPDeclareTargetDeclAttr::MT_To ||
3669 *Res == OMPDeclareTargetDeclAttr::MT_Enter) &&
3670 !UnifiedMemoryEnabled) {
3671 (void)GetAddrOfGlobalVar(VD);
3672 } else {
3673 assert(((*Res == OMPDeclareTargetDeclAttr::MT_Link) ||
3674 ((*Res == OMPDeclareTargetDeclAttr::MT_To ||
3675 *Res == OMPDeclareTargetDeclAttr::MT_Enter) &&
3676 UnifiedMemoryEnabled)) &&
3677 "Link clause or to clause with unified memory expected.");
3678 (void)getOpenMPRuntime().getAddrOfDeclareTargetVar(VD);
3681 return;
3684 // If this declaration may have caused an inline variable definition to
3685 // change linkage, make sure that it's emitted.
3686 if (Context.getInlineVariableDefinitionKind(VD) ==
3687 ASTContext::InlineVariableDefinitionKind::Strong)
3688 GetAddrOfGlobalVar(VD);
3689 return;
3693 // Defer code generation to first use when possible, e.g. if this is an inline
3694 // function. If the global must always be emitted, do it eagerly if possible
3695 // to benefit from cache locality.
3696 if (MustBeEmitted(Global) && MayBeEmittedEagerly(Global)) {
3697 // Emit the definition if it can't be deferred.
3698 EmitGlobalDefinition(GD);
3699 addEmittedDeferredDecl(GD);
3700 return;
3703 // If we're deferring emission of a C++ variable with an
3704 // initializer, remember the order in which it appeared in the file.
3705 if (getLangOpts().CPlusPlus && isa<VarDecl>(Global) &&
3706 cast<VarDecl>(Global)->hasInit()) {
3707 DelayedCXXInitPosition[Global] = CXXGlobalInits.size();
3708 CXXGlobalInits.push_back(nullptr);
3711 StringRef MangledName = getMangledName(GD);
3712 if (GetGlobalValue(MangledName) != nullptr) {
3713 // The value has already been used and should therefore be emitted.
3714 addDeferredDeclToEmit(GD);
3715 } else if (MustBeEmitted(Global)) {
3716 // The value must be emitted, but cannot be emitted eagerly.
3717 assert(!MayBeEmittedEagerly(Global));
3718 addDeferredDeclToEmit(GD);
3719 } else {
3720 // Otherwise, remember that we saw a deferred decl with this name. The
3721 // first use of the mangled name will cause it to move into
3722 // DeferredDeclsToEmit.
3723 DeferredDecls[MangledName] = GD;
3727 // Check if T is a class type with a destructor that's not dllimport.
3728 static bool HasNonDllImportDtor(QualType T) {
3729 if (const auto *RT = T->getBaseElementTypeUnsafe()->getAs<RecordType>())
3730 if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl()))
3731 if (RD->getDestructor() && !RD->getDestructor()->hasAttr<DLLImportAttr>())
3732 return true;
3734 return false;
3737 namespace {
3738 struct FunctionIsDirectlyRecursive
3739 : public ConstStmtVisitor<FunctionIsDirectlyRecursive, bool> {
3740 const StringRef Name;
3741 const Builtin::Context &BI;
3742 FunctionIsDirectlyRecursive(StringRef N, const Builtin::Context &C)
3743 : Name(N), BI(C) {}
3745 bool VisitCallExpr(const CallExpr *E) {
3746 const FunctionDecl *FD = E->getDirectCallee();
3747 if (!FD)
3748 return false;
3749 AsmLabelAttr *Attr = FD->getAttr<AsmLabelAttr>();
3750 if (Attr && Name == Attr->getLabel())
3751 return true;
3752 unsigned BuiltinID = FD->getBuiltinID();
3753 if (!BuiltinID || !BI.isLibFunction(BuiltinID))
3754 return false;
3755 StringRef BuiltinName = BI.getName(BuiltinID);
3756 if (BuiltinName.startswith("__builtin_") &&
3757 Name == BuiltinName.slice(strlen("__builtin_"), StringRef::npos)) {
3758 return true;
3760 return false;
3763 bool VisitStmt(const Stmt *S) {
3764 for (const Stmt *Child : S->children())
3765 if (Child && this->Visit(Child))
3766 return true;
3767 return false;
3771 // Make sure we're not referencing non-imported vars or functions.
3772 struct DLLImportFunctionVisitor
3773 : public RecursiveASTVisitor<DLLImportFunctionVisitor> {
3774 bool SafeToInline = true;
3776 bool shouldVisitImplicitCode() const { return true; }
3778 bool VisitVarDecl(VarDecl *VD) {
3779 if (VD->getTLSKind()) {
3780 // A thread-local variable cannot be imported.
3781 SafeToInline = false;
3782 return SafeToInline;
3785 // A variable definition might imply a destructor call.
3786 if (VD->isThisDeclarationADefinition())
3787 SafeToInline = !HasNonDllImportDtor(VD->getType());
3789 return SafeToInline;
3792 bool VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) {
3793 if (const auto *D = E->getTemporary()->getDestructor())
3794 SafeToInline = D->hasAttr<DLLImportAttr>();
3795 return SafeToInline;
3798 bool VisitDeclRefExpr(DeclRefExpr *E) {
3799 ValueDecl *VD = E->getDecl();
3800 if (isa<FunctionDecl>(VD))
3801 SafeToInline = VD->hasAttr<DLLImportAttr>();
3802 else if (VarDecl *V = dyn_cast<VarDecl>(VD))
3803 SafeToInline = !V->hasGlobalStorage() || V->hasAttr<DLLImportAttr>();
3804 return SafeToInline;
3807 bool VisitCXXConstructExpr(CXXConstructExpr *E) {
3808 SafeToInline = E->getConstructor()->hasAttr<DLLImportAttr>();
3809 return SafeToInline;
3812 bool VisitCXXMemberCallExpr(CXXMemberCallExpr *E) {
3813 CXXMethodDecl *M = E->getMethodDecl();
3814 if (!M) {
3815 // Call through a pointer to member function. This is safe to inline.
3816 SafeToInline = true;
3817 } else {
3818 SafeToInline = M->hasAttr<DLLImportAttr>();
3820 return SafeToInline;
3823 bool VisitCXXDeleteExpr(CXXDeleteExpr *E) {
3824 SafeToInline = E->getOperatorDelete()->hasAttr<DLLImportAttr>();
3825 return SafeToInline;
3828 bool VisitCXXNewExpr(CXXNewExpr *E) {
3829 SafeToInline = E->getOperatorNew()->hasAttr<DLLImportAttr>();
3830 return SafeToInline;
3835 // isTriviallyRecursive - Check if this function calls another
3836 // decl that, because of the asm attribute or the other decl being a builtin,
3837 // ends up pointing to itself.
3838 bool
3839 CodeGenModule::isTriviallyRecursive(const FunctionDecl *FD) {
3840 StringRef Name;
3841 if (getCXXABI().getMangleContext().shouldMangleDeclName(FD)) {
3842 // asm labels are a special kind of mangling we have to support.
3843 AsmLabelAttr *Attr = FD->getAttr<AsmLabelAttr>();
3844 if (!Attr)
3845 return false;
3846 Name = Attr->getLabel();
3847 } else {
3848 Name = FD->getName();
3851 FunctionIsDirectlyRecursive Walker(Name, Context.BuiltinInfo);
3852 const Stmt *Body = FD->getBody();
3853 return Body ? Walker.Visit(Body) : false;
3856 bool CodeGenModule::shouldEmitFunction(GlobalDecl GD) {
3857 if (getFunctionLinkage(GD) != llvm::Function::AvailableExternallyLinkage)
3858 return true;
3859 const auto *F = cast<FunctionDecl>(GD.getDecl());
3860 if (CodeGenOpts.OptimizationLevel == 0 && !F->hasAttr<AlwaysInlineAttr>())
3861 return false;
3863 if (F->hasAttr<NoInlineAttr>())
3864 return false;
3866 if (F->hasAttr<DLLImportAttr>() && !F->hasAttr<AlwaysInlineAttr>()) {
3867 // Check whether it would be safe to inline this dllimport function.
3868 DLLImportFunctionVisitor Visitor;
3869 Visitor.TraverseFunctionDecl(const_cast<FunctionDecl*>(F));
3870 if (!Visitor.SafeToInline)
3871 return false;
3873 if (const CXXDestructorDecl *Dtor = dyn_cast<CXXDestructorDecl>(F)) {
3874 // Implicit destructor invocations aren't captured in the AST, so the
3875 // check above can't see them. Check for them manually here.
3876 for (const Decl *Member : Dtor->getParent()->decls())
3877 if (isa<FieldDecl>(Member))
3878 if (HasNonDllImportDtor(cast<FieldDecl>(Member)->getType()))
3879 return false;
3880 for (const CXXBaseSpecifier &B : Dtor->getParent()->bases())
3881 if (HasNonDllImportDtor(B.getType()))
3882 return false;
3886 // Inline builtins declaration must be emitted. They often are fortified
3887 // functions.
3888 if (F->isInlineBuiltinDeclaration())
3889 return true;
3891 // PR9614. Avoid cases where the source code is lying to us. An available
3892 // externally function should have an equivalent function somewhere else,
3893 // but a function that calls itself through asm label/`__builtin_` trickery is
3894 // clearly not equivalent to the real implementation.
3895 // This happens in glibc's btowc and in some configure checks.
3896 return !isTriviallyRecursive(F);
3899 bool CodeGenModule::shouldOpportunisticallyEmitVTables() {
3900 return CodeGenOpts.OptimizationLevel > 0;
3903 void CodeGenModule::EmitMultiVersionFunctionDefinition(GlobalDecl GD,
3904 llvm::GlobalValue *GV) {
3905 const auto *FD = cast<FunctionDecl>(GD.getDecl());
3907 if (FD->isCPUSpecificMultiVersion()) {
3908 auto *Spec = FD->getAttr<CPUSpecificAttr>();
3909 for (unsigned I = 0; I < Spec->cpus_size(); ++I)
3910 EmitGlobalFunctionDefinition(GD.getWithMultiVersionIndex(I), nullptr);
3911 } else if (FD->isTargetClonesMultiVersion()) {
3912 auto *Clone = FD->getAttr<TargetClonesAttr>();
3913 for (unsigned I = 0; I < Clone->featuresStrs_size(); ++I)
3914 if (Clone->isFirstOfVersion(I))
3915 EmitGlobalFunctionDefinition(GD.getWithMultiVersionIndex(I), nullptr);
3916 // Ensure that the resolver function is also emitted.
3917 GetOrCreateMultiVersionResolver(GD);
3918 } else
3919 EmitGlobalFunctionDefinition(GD, GV);
3922 void CodeGenModule::EmitGlobalDefinition(GlobalDecl GD, llvm::GlobalValue *GV) {
3923 const auto *D = cast<ValueDecl>(GD.getDecl());
3925 PrettyStackTraceDecl CrashInfo(const_cast<ValueDecl *>(D), D->getLocation(),
3926 Context.getSourceManager(),
3927 "Generating code for declaration");
3929 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
3930 // At -O0, don't generate IR for functions with available_externally
3931 // linkage.
3932 if (!shouldEmitFunction(GD))
3933 return;
3935 llvm::TimeTraceScope TimeScope("CodeGen Function", [&]() {
3936 std::string Name;
3937 llvm::raw_string_ostream OS(Name);
3938 FD->getNameForDiagnostic(OS, getContext().getPrintingPolicy(),
3939 /*Qualified=*/true);
3940 return Name;
3943 if (const auto *Method = dyn_cast<CXXMethodDecl>(D)) {
3944 // Make sure to emit the definition(s) before we emit the thunks.
3945 // This is necessary for the generation of certain thunks.
3946 if (isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method))
3947 ABI->emitCXXStructor(GD);
3948 else if (FD->isMultiVersion())
3949 EmitMultiVersionFunctionDefinition(GD, GV);
3950 else
3951 EmitGlobalFunctionDefinition(GD, GV);
3953 if (Method->isVirtual())
3954 getVTables().EmitThunks(GD);
3956 return;
3959 if (FD->isMultiVersion())
3960 return EmitMultiVersionFunctionDefinition(GD, GV);
3961 return EmitGlobalFunctionDefinition(GD, GV);
3964 if (const auto *VD = dyn_cast<VarDecl>(D))
3965 return EmitGlobalVarDefinition(VD, !VD->hasDefinition());
3967 llvm_unreachable("Invalid argument to EmitGlobalDefinition()");
3970 static void ReplaceUsesOfNonProtoTypeWithRealFunction(llvm::GlobalValue *Old,
3971 llvm::Function *NewFn);
3973 static unsigned
3974 TargetMVPriority(const TargetInfo &TI,
3975 const CodeGenFunction::MultiVersionResolverOption &RO) {
3976 unsigned Priority = 0;
3977 unsigned NumFeatures = 0;
3978 for (StringRef Feat : RO.Conditions.Features) {
3979 Priority = std::max(Priority, TI.multiVersionSortPriority(Feat));
3980 NumFeatures++;
3983 if (!RO.Conditions.Architecture.empty())
3984 Priority = std::max(
3985 Priority, TI.multiVersionSortPriority(RO.Conditions.Architecture));
3987 Priority += TI.multiVersionFeatureCost() * NumFeatures;
3989 return Priority;
3992 // Multiversion functions should be at most 'WeakODRLinkage' so that a different
3993 // TU can forward declare the function without causing problems. Particularly
3994 // in the cases of CPUDispatch, this causes issues. This also makes sure we
3995 // work with internal linkage functions, so that the same function name can be
3996 // used with internal linkage in multiple TUs.
3997 llvm::GlobalValue::LinkageTypes getMultiversionLinkage(CodeGenModule &CGM,
3998 GlobalDecl GD) {
3999 const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
4000 if (FD->getFormalLinkage() == Linkage::Internal)
4001 return llvm::GlobalValue::InternalLinkage;
4002 return llvm::GlobalValue::WeakODRLinkage;
4005 void CodeGenModule::emitMultiVersionFunctions() {
4006 std::vector<GlobalDecl> MVFuncsToEmit;
4007 MultiVersionFuncs.swap(MVFuncsToEmit);
4008 for (GlobalDecl GD : MVFuncsToEmit) {
4009 const auto *FD = cast<FunctionDecl>(GD.getDecl());
4010 assert(FD && "Expected a FunctionDecl");
4012 SmallVector<CodeGenFunction::MultiVersionResolverOption, 10> Options;
4013 if (FD->isTargetMultiVersion()) {
4014 getContext().forEachMultiversionedFunctionVersion(
4015 FD, [this, &GD, &Options](const FunctionDecl *CurFD) {
4016 GlobalDecl CurGD{
4017 (CurFD->isDefined() ? CurFD->getDefinition() : CurFD)};
4018 StringRef MangledName = getMangledName(CurGD);
4019 llvm::Constant *Func = GetGlobalValue(MangledName);
4020 if (!Func) {
4021 if (CurFD->isDefined()) {
4022 EmitGlobalFunctionDefinition(CurGD, nullptr);
4023 Func = GetGlobalValue(MangledName);
4024 } else {
4025 const CGFunctionInfo &FI =
4026 getTypes().arrangeGlobalDeclaration(GD);
4027 llvm::FunctionType *Ty = getTypes().GetFunctionType(FI);
4028 Func = GetAddrOfFunction(CurGD, Ty, /*ForVTable=*/false,
4029 /*DontDefer=*/false, ForDefinition);
4031 assert(Func && "This should have just been created");
4033 if (CurFD->getMultiVersionKind() == MultiVersionKind::Target) {
4034 const auto *TA = CurFD->getAttr<TargetAttr>();
4035 llvm::SmallVector<StringRef, 8> Feats;
4036 TA->getAddedFeatures(Feats);
4037 Options.emplace_back(cast<llvm::Function>(Func),
4038 TA->getArchitecture(), Feats);
4039 } else {
4040 const auto *TVA = CurFD->getAttr<TargetVersionAttr>();
4041 llvm::SmallVector<StringRef, 8> Feats;
4042 TVA->getFeatures(Feats);
4043 Options.emplace_back(cast<llvm::Function>(Func),
4044 /*Architecture*/ "", Feats);
4047 } else if (FD->isTargetClonesMultiVersion()) {
4048 const auto *TC = FD->getAttr<TargetClonesAttr>();
4049 for (unsigned VersionIndex = 0; VersionIndex < TC->featuresStrs_size();
4050 ++VersionIndex) {
4051 if (!TC->isFirstOfVersion(VersionIndex))
4052 continue;
4053 GlobalDecl CurGD{(FD->isDefined() ? FD->getDefinition() : FD),
4054 VersionIndex};
4055 StringRef Version = TC->getFeatureStr(VersionIndex);
4056 StringRef MangledName = getMangledName(CurGD);
4057 llvm::Constant *Func = GetGlobalValue(MangledName);
4058 if (!Func) {
4059 if (FD->isDefined()) {
4060 EmitGlobalFunctionDefinition(CurGD, nullptr);
4061 Func = GetGlobalValue(MangledName);
4062 } else {
4063 const CGFunctionInfo &FI =
4064 getTypes().arrangeGlobalDeclaration(CurGD);
4065 llvm::FunctionType *Ty = getTypes().GetFunctionType(FI);
4066 Func = GetAddrOfFunction(CurGD, Ty, /*ForVTable=*/false,
4067 /*DontDefer=*/false, ForDefinition);
4069 assert(Func && "This should have just been created");
4072 StringRef Architecture;
4073 llvm::SmallVector<StringRef, 1> Feature;
4075 if (getTarget().getTriple().isAArch64()) {
4076 if (Version != "default") {
4077 llvm::SmallVector<StringRef, 8> VerFeats;
4078 Version.split(VerFeats, "+");
4079 for (auto &CurFeat : VerFeats)
4080 Feature.push_back(CurFeat.trim());
4082 } else {
4083 if (Version.startswith("arch="))
4084 Architecture = Version.drop_front(sizeof("arch=") - 1);
4085 else if (Version != "default")
4086 Feature.push_back(Version);
4089 Options.emplace_back(cast<llvm::Function>(Func), Architecture, Feature);
4091 } else {
4092 assert(0 && "Expected a target or target_clones multiversion function");
4093 continue;
4096 llvm::Constant *ResolverConstant = GetOrCreateMultiVersionResolver(GD);
4097 if (auto *IFunc = dyn_cast<llvm::GlobalIFunc>(ResolverConstant))
4098 ResolverConstant = IFunc->getResolver();
4099 llvm::Function *ResolverFunc = cast<llvm::Function>(ResolverConstant);
4101 ResolverFunc->setLinkage(getMultiversionLinkage(*this, GD));
4103 if (!ResolverFunc->hasLocalLinkage() && supportsCOMDAT())
4104 ResolverFunc->setComdat(
4105 getModule().getOrInsertComdat(ResolverFunc->getName()));
4107 const TargetInfo &TI = getTarget();
4108 llvm::stable_sort(
4109 Options, [&TI](const CodeGenFunction::MultiVersionResolverOption &LHS,
4110 const CodeGenFunction::MultiVersionResolverOption &RHS) {
4111 return TargetMVPriority(TI, LHS) > TargetMVPriority(TI, RHS);
4113 CodeGenFunction CGF(*this);
4114 CGF.EmitMultiVersionResolver(ResolverFunc, Options);
4117 // Ensure that any additions to the deferred decls list caused by emitting a
4118 // variant are emitted. This can happen when the variant itself is inline and
4119 // calls a function without linkage.
4120 if (!MVFuncsToEmit.empty())
4121 EmitDeferred();
4123 // Ensure that any additions to the multiversion funcs list from either the
4124 // deferred decls or the multiversion functions themselves are emitted.
4125 if (!MultiVersionFuncs.empty())
4126 emitMultiVersionFunctions();
4129 void CodeGenModule::emitCPUDispatchDefinition(GlobalDecl GD) {
4130 const auto *FD = cast<FunctionDecl>(GD.getDecl());
4131 assert(FD && "Not a FunctionDecl?");
4132 assert(FD->isCPUDispatchMultiVersion() && "Not a multiversion function?");
4133 const auto *DD = FD->getAttr<CPUDispatchAttr>();
4134 assert(DD && "Not a cpu_dispatch Function?");
4136 const CGFunctionInfo &FI = getTypes().arrangeGlobalDeclaration(GD);
4137 llvm::FunctionType *DeclTy = getTypes().GetFunctionType(FI);
4139 StringRef ResolverName = getMangledName(GD);
4140 UpdateMultiVersionNames(GD, FD, ResolverName);
4142 llvm::Type *ResolverType;
4143 GlobalDecl ResolverGD;
4144 if (getTarget().supportsIFunc()) {
4145 ResolverType = llvm::FunctionType::get(
4146 llvm::PointerType::get(DeclTy,
4147 getTypes().getTargetAddressSpace(FD->getType())),
4148 false);
4150 else {
4151 ResolverType = DeclTy;
4152 ResolverGD = GD;
4155 auto *ResolverFunc = cast<llvm::Function>(GetOrCreateLLVMFunction(
4156 ResolverName, ResolverType, ResolverGD, /*ForVTable=*/false));
4157 ResolverFunc->setLinkage(getMultiversionLinkage(*this, GD));
4158 if (supportsCOMDAT())
4159 ResolverFunc->setComdat(
4160 getModule().getOrInsertComdat(ResolverFunc->getName()));
4162 SmallVector<CodeGenFunction::MultiVersionResolverOption, 10> Options;
4163 const TargetInfo &Target = getTarget();
4164 unsigned Index = 0;
4165 for (const IdentifierInfo *II : DD->cpus()) {
4166 // Get the name of the target function so we can look it up/create it.
4167 std::string MangledName = getMangledNameImpl(*this, GD, FD, true) +
4168 getCPUSpecificMangling(*this, II->getName());
4170 llvm::Constant *Func = GetGlobalValue(MangledName);
4172 if (!Func) {
4173 GlobalDecl ExistingDecl = Manglings.lookup(MangledName);
4174 if (ExistingDecl.getDecl() &&
4175 ExistingDecl.getDecl()->getAsFunction()->isDefined()) {
4176 EmitGlobalFunctionDefinition(ExistingDecl, nullptr);
4177 Func = GetGlobalValue(MangledName);
4178 } else {
4179 if (!ExistingDecl.getDecl())
4180 ExistingDecl = GD.getWithMultiVersionIndex(Index);
4182 Func = GetOrCreateLLVMFunction(
4183 MangledName, DeclTy, ExistingDecl,
4184 /*ForVTable=*/false, /*DontDefer=*/true,
4185 /*IsThunk=*/false, llvm::AttributeList(), ForDefinition);
4189 llvm::SmallVector<StringRef, 32> Features;
4190 Target.getCPUSpecificCPUDispatchFeatures(II->getName(), Features);
4191 llvm::transform(Features, Features.begin(),
4192 [](StringRef Str) { return Str.substr(1); });
4193 llvm::erase_if(Features, [&Target](StringRef Feat) {
4194 return !Target.validateCpuSupports(Feat);
4196 Options.emplace_back(cast<llvm::Function>(Func), StringRef{}, Features);
4197 ++Index;
4200 llvm::stable_sort(
4201 Options, [](const CodeGenFunction::MultiVersionResolverOption &LHS,
4202 const CodeGenFunction::MultiVersionResolverOption &RHS) {
4203 return llvm::X86::getCpuSupportsMask(LHS.Conditions.Features) >
4204 llvm::X86::getCpuSupportsMask(RHS.Conditions.Features);
4207 // If the list contains multiple 'default' versions, such as when it contains
4208 // 'pentium' and 'generic', don't emit the call to the generic one (since we
4209 // always run on at least a 'pentium'). We do this by deleting the 'least
4210 // advanced' (read, lowest mangling letter).
4211 while (Options.size() > 1 &&
4212 llvm::all_of(llvm::X86::getCpuSupportsMask(
4213 (Options.end() - 2)->Conditions.Features),
4214 [](auto X) { return X == 0; })) {
4215 StringRef LHSName = (Options.end() - 2)->Function->getName();
4216 StringRef RHSName = (Options.end() - 1)->Function->getName();
4217 if (LHSName.compare(RHSName) < 0)
4218 Options.erase(Options.end() - 2);
4219 else
4220 Options.erase(Options.end() - 1);
4223 CodeGenFunction CGF(*this);
4224 CGF.EmitMultiVersionResolver(ResolverFunc, Options);
4226 if (getTarget().supportsIFunc()) {
4227 llvm::GlobalValue::LinkageTypes Linkage = getMultiversionLinkage(*this, GD);
4228 auto *IFunc = cast<llvm::GlobalValue>(GetOrCreateMultiVersionResolver(GD));
4230 // Fix up function declarations that were created for cpu_specific before
4231 // cpu_dispatch was known
4232 if (!isa<llvm::GlobalIFunc>(IFunc)) {
4233 assert(cast<llvm::Function>(IFunc)->isDeclaration());
4234 auto *GI = llvm::GlobalIFunc::create(DeclTy, 0, Linkage, "", ResolverFunc,
4235 &getModule());
4236 GI->takeName(IFunc);
4237 IFunc->replaceAllUsesWith(GI);
4238 IFunc->eraseFromParent();
4239 IFunc = GI;
4242 std::string AliasName = getMangledNameImpl(
4243 *this, GD, FD, /*OmitMultiVersionMangling=*/true);
4244 llvm::Constant *AliasFunc = GetGlobalValue(AliasName);
4245 if (!AliasFunc) {
4246 auto *GA = llvm::GlobalAlias::create(DeclTy, 0, Linkage, AliasName, IFunc,
4247 &getModule());
4248 SetCommonAttributes(GD, GA);
4253 /// If a dispatcher for the specified mangled name is not in the module, create
4254 /// and return an llvm Function with the specified type.
4255 llvm::Constant *CodeGenModule::GetOrCreateMultiVersionResolver(GlobalDecl GD) {
4256 const auto *FD = cast<FunctionDecl>(GD.getDecl());
4257 assert(FD && "Not a FunctionDecl?");
4259 std::string MangledName =
4260 getMangledNameImpl(*this, GD, FD, /*OmitMultiVersionMangling=*/true);
4262 // Holds the name of the resolver, in ifunc mode this is the ifunc (which has
4263 // a separate resolver).
4264 std::string ResolverName = MangledName;
4265 if (getTarget().supportsIFunc())
4266 ResolverName += ".ifunc";
4267 else if (FD->isTargetMultiVersion())
4268 ResolverName += ".resolver";
4270 // If the resolver has already been created, just return it.
4271 if (llvm::GlobalValue *ResolverGV = GetGlobalValue(ResolverName))
4272 return ResolverGV;
4274 const CGFunctionInfo &FI = getTypes().arrangeGlobalDeclaration(GD);
4275 llvm::FunctionType *DeclTy = getTypes().GetFunctionType(FI);
4277 // The resolver needs to be created. For target and target_clones, defer
4278 // creation until the end of the TU.
4279 if (FD->isTargetMultiVersion() || FD->isTargetClonesMultiVersion())
4280 MultiVersionFuncs.push_back(GD);
4282 // For cpu_specific, don't create an ifunc yet because we don't know if the
4283 // cpu_dispatch will be emitted in this translation unit.
4284 if (getTarget().supportsIFunc() && !FD->isCPUSpecificMultiVersion()) {
4285 llvm::Type *ResolverType = llvm::FunctionType::get(
4286 llvm::PointerType::get(DeclTy,
4287 getTypes().getTargetAddressSpace(FD->getType())),
4288 false);
4289 llvm::Constant *Resolver = GetOrCreateLLVMFunction(
4290 MangledName + ".resolver", ResolverType, GlobalDecl{},
4291 /*ForVTable=*/false);
4292 llvm::GlobalIFunc *GIF =
4293 llvm::GlobalIFunc::create(DeclTy, 0, getMultiversionLinkage(*this, GD),
4294 "", Resolver, &getModule());
4295 GIF->setName(ResolverName);
4296 SetCommonAttributes(FD, GIF);
4298 return GIF;
4301 llvm::Constant *Resolver = GetOrCreateLLVMFunction(
4302 ResolverName, DeclTy, GlobalDecl{}, /*ForVTable=*/false);
4303 assert(isa<llvm::GlobalValue>(Resolver) &&
4304 "Resolver should be created for the first time");
4305 SetCommonAttributes(FD, cast<llvm::GlobalValue>(Resolver));
4306 return Resolver;
4309 /// GetOrCreateLLVMFunction - If the specified mangled name is not in the
4310 /// module, create and return an llvm Function with the specified type. If there
4311 /// is something in the module with the specified name, return it potentially
4312 /// bitcasted to the right type.
4314 /// If D is non-null, it specifies a decl that correspond to this. This is used
4315 /// to set the attributes on the function when it is first created.
4316 llvm::Constant *CodeGenModule::GetOrCreateLLVMFunction(
4317 StringRef MangledName, llvm::Type *Ty, GlobalDecl GD, bool ForVTable,
4318 bool DontDefer, bool IsThunk, llvm::AttributeList ExtraAttrs,
4319 ForDefinition_t IsForDefinition) {
4320 const Decl *D = GD.getDecl();
4322 // Any attempts to use a MultiVersion function should result in retrieving
4323 // the iFunc instead. Name Mangling will handle the rest of the changes.
4324 if (const FunctionDecl *FD = cast_or_null<FunctionDecl>(D)) {
4325 // For the device mark the function as one that should be emitted.
4326 if (getLangOpts().OpenMPIsTargetDevice && OpenMPRuntime &&
4327 !OpenMPRuntime->markAsGlobalTarget(GD) && FD->isDefined() &&
4328 !DontDefer && !IsForDefinition) {
4329 if (const FunctionDecl *FDDef = FD->getDefinition()) {
4330 GlobalDecl GDDef;
4331 if (const auto *CD = dyn_cast<CXXConstructorDecl>(FDDef))
4332 GDDef = GlobalDecl(CD, GD.getCtorType());
4333 else if (const auto *DD = dyn_cast<CXXDestructorDecl>(FDDef))
4334 GDDef = GlobalDecl(DD, GD.getDtorType());
4335 else
4336 GDDef = GlobalDecl(FDDef);
4337 EmitGlobal(GDDef);
4341 if (FD->isMultiVersion()) {
4342 UpdateMultiVersionNames(GD, FD, MangledName);
4343 if (!IsForDefinition)
4344 return GetOrCreateMultiVersionResolver(GD);
4348 // Lookup the entry, lazily creating it if necessary.
4349 llvm::GlobalValue *Entry = GetGlobalValue(MangledName);
4350 if (Entry) {
4351 if (WeakRefReferences.erase(Entry)) {
4352 const FunctionDecl *FD = cast_or_null<FunctionDecl>(D);
4353 if (FD && !FD->hasAttr<WeakAttr>())
4354 Entry->setLinkage(llvm::Function::ExternalLinkage);
4357 // Handle dropped DLL attributes.
4358 if (D && !D->hasAttr<DLLImportAttr>() && !D->hasAttr<DLLExportAttr>() &&
4359 !shouldMapVisibilityToDLLExport(cast_or_null<NamedDecl>(D))) {
4360 Entry->setDLLStorageClass(llvm::GlobalValue::DefaultStorageClass);
4361 setDSOLocal(Entry);
4364 // If there are two attempts to define the same mangled name, issue an
4365 // error.
4366 if (IsForDefinition && !Entry->isDeclaration()) {
4367 GlobalDecl OtherGD;
4368 // Check that GD is not yet in DiagnosedConflictingDefinitions is required
4369 // to make sure that we issue an error only once.
4370 if (lookupRepresentativeDecl(MangledName, OtherGD) &&
4371 (GD.getCanonicalDecl().getDecl() !=
4372 OtherGD.getCanonicalDecl().getDecl()) &&
4373 DiagnosedConflictingDefinitions.insert(GD).second) {
4374 getDiags().Report(D->getLocation(), diag::err_duplicate_mangled_name)
4375 << MangledName;
4376 getDiags().Report(OtherGD.getDecl()->getLocation(),
4377 diag::note_previous_definition);
4381 if ((isa<llvm::Function>(Entry) || isa<llvm::GlobalAlias>(Entry)) &&
4382 (Entry->getValueType() == Ty)) {
4383 return Entry;
4386 // Make sure the result is of the correct type.
4387 // (If function is requested for a definition, we always need to create a new
4388 // function, not just return a bitcast.)
4389 if (!IsForDefinition)
4390 return Entry;
4393 // This function doesn't have a complete type (for example, the return
4394 // type is an incomplete struct). Use a fake type instead, and make
4395 // sure not to try to set attributes.
4396 bool IsIncompleteFunction = false;
4398 llvm::FunctionType *FTy;
4399 if (isa<llvm::FunctionType>(Ty)) {
4400 FTy = cast<llvm::FunctionType>(Ty);
4401 } else {
4402 FTy = llvm::FunctionType::get(VoidTy, false);
4403 IsIncompleteFunction = true;
4406 llvm::Function *F =
4407 llvm::Function::Create(FTy, llvm::Function::ExternalLinkage,
4408 Entry ? StringRef() : MangledName, &getModule());
4410 // If we already created a function with the same mangled name (but different
4411 // type) before, take its name and add it to the list of functions to be
4412 // replaced with F at the end of CodeGen.
4414 // This happens if there is a prototype for a function (e.g. "int f()") and
4415 // then a definition of a different type (e.g. "int f(int x)").
4416 if (Entry) {
4417 F->takeName(Entry);
4419 // This might be an implementation of a function without a prototype, in
4420 // which case, try to do special replacement of calls which match the new
4421 // prototype. The really key thing here is that we also potentially drop
4422 // arguments from the call site so as to make a direct call, which makes the
4423 // inliner happier and suppresses a number of optimizer warnings (!) about
4424 // dropping arguments.
4425 if (!Entry->use_empty()) {
4426 ReplaceUsesOfNonProtoTypeWithRealFunction(Entry, F);
4427 Entry->removeDeadConstantUsers();
4430 addGlobalValReplacement(Entry, F);
4433 assert(F->getName() == MangledName && "name was uniqued!");
4434 if (D)
4435 SetFunctionAttributes(GD, F, IsIncompleteFunction, IsThunk);
4436 if (ExtraAttrs.hasFnAttrs()) {
4437 llvm::AttrBuilder B(F->getContext(), ExtraAttrs.getFnAttrs());
4438 F->addFnAttrs(B);
4441 if (!DontDefer) {
4442 // All MSVC dtors other than the base dtor are linkonce_odr and delegate to
4443 // each other bottoming out with the base dtor. Therefore we emit non-base
4444 // dtors on usage, even if there is no dtor definition in the TU.
4445 if (isa_and_nonnull<CXXDestructorDecl>(D) &&
4446 getCXXABI().useThunkForDtorVariant(cast<CXXDestructorDecl>(D),
4447 GD.getDtorType()))
4448 addDeferredDeclToEmit(GD);
4450 // This is the first use or definition of a mangled name. If there is a
4451 // deferred decl with this name, remember that we need to emit it at the end
4452 // of the file.
4453 auto DDI = DeferredDecls.find(MangledName);
4454 if (DDI != DeferredDecls.end()) {
4455 // Move the potentially referenced deferred decl to the
4456 // DeferredDeclsToEmit list, and remove it from DeferredDecls (since we
4457 // don't need it anymore).
4458 addDeferredDeclToEmit(DDI->second);
4459 DeferredDecls.erase(DDI);
4461 // Otherwise, there are cases we have to worry about where we're
4462 // using a declaration for which we must emit a definition but where
4463 // we might not find a top-level definition:
4464 // - member functions defined inline in their classes
4465 // - friend functions defined inline in some class
4466 // - special member functions with implicit definitions
4467 // If we ever change our AST traversal to walk into class methods,
4468 // this will be unnecessary.
4470 // We also don't emit a definition for a function if it's going to be an
4471 // entry in a vtable, unless it's already marked as used.
4472 } else if (getLangOpts().CPlusPlus && D) {
4473 // Look for a declaration that's lexically in a record.
4474 for (const auto *FD = cast<FunctionDecl>(D)->getMostRecentDecl(); FD;
4475 FD = FD->getPreviousDecl()) {
4476 if (isa<CXXRecordDecl>(FD->getLexicalDeclContext())) {
4477 if (FD->doesThisDeclarationHaveABody()) {
4478 addDeferredDeclToEmit(GD.getWithDecl(FD));
4479 break;
4486 // Make sure the result is of the requested type.
4487 if (!IsIncompleteFunction) {
4488 assert(F->getFunctionType() == Ty);
4489 return F;
4492 return F;
4495 /// GetAddrOfFunction - Return the address of the given function. If Ty is
4496 /// non-null, then this function will use the specified type if it has to
4497 /// create it (this occurs when we see a definition of the function).
4498 llvm::Constant *
4499 CodeGenModule::GetAddrOfFunction(GlobalDecl GD, llvm::Type *Ty, bool ForVTable,
4500 bool DontDefer,
4501 ForDefinition_t IsForDefinition) {
4502 // If there was no specific requested type, just convert it now.
4503 if (!Ty) {
4504 const auto *FD = cast<FunctionDecl>(GD.getDecl());
4505 Ty = getTypes().ConvertType(FD->getType());
4508 // Devirtualized destructor calls may come through here instead of via
4509 // getAddrOfCXXStructor. Make sure we use the MS ABI base destructor instead
4510 // of the complete destructor when necessary.
4511 if (const auto *DD = dyn_cast<CXXDestructorDecl>(GD.getDecl())) {
4512 if (getTarget().getCXXABI().isMicrosoft() &&
4513 GD.getDtorType() == Dtor_Complete &&
4514 DD->getParent()->getNumVBases() == 0)
4515 GD = GlobalDecl(DD, Dtor_Base);
4518 StringRef MangledName = getMangledName(GD);
4519 auto *F = GetOrCreateLLVMFunction(MangledName, Ty, GD, ForVTable, DontDefer,
4520 /*IsThunk=*/false, llvm::AttributeList(),
4521 IsForDefinition);
4522 // Returns kernel handle for HIP kernel stub function.
4523 if (LangOpts.CUDA && !LangOpts.CUDAIsDevice &&
4524 cast<FunctionDecl>(GD.getDecl())->hasAttr<CUDAGlobalAttr>()) {
4525 auto *Handle = getCUDARuntime().getKernelHandle(
4526 cast<llvm::Function>(F->stripPointerCasts()), GD);
4527 if (IsForDefinition)
4528 return F;
4529 return Handle;
4531 return F;
4534 llvm::Constant *CodeGenModule::GetFunctionStart(const ValueDecl *Decl) {
4535 llvm::GlobalValue *F =
4536 cast<llvm::GlobalValue>(GetAddrOfFunction(Decl)->stripPointerCasts());
4538 return llvm::ConstantExpr::getBitCast(
4539 llvm::NoCFIValue::get(F),
4540 llvm::PointerType::get(VMContext, F->getAddressSpace()));
4543 static const FunctionDecl *
4544 GetRuntimeFunctionDecl(ASTContext &C, StringRef Name) {
4545 TranslationUnitDecl *TUDecl = C.getTranslationUnitDecl();
4546 DeclContext *DC = TranslationUnitDecl::castToDeclContext(TUDecl);
4548 IdentifierInfo &CII = C.Idents.get(Name);
4549 for (const auto *Result : DC->lookup(&CII))
4550 if (const auto *FD = dyn_cast<FunctionDecl>(Result))
4551 return FD;
4553 if (!C.getLangOpts().CPlusPlus)
4554 return nullptr;
4556 // Demangle the premangled name from getTerminateFn()
4557 IdentifierInfo &CXXII =
4558 (Name == "_ZSt9terminatev" || Name == "?terminate@@YAXXZ")
4559 ? C.Idents.get("terminate")
4560 : C.Idents.get(Name);
4562 for (const auto &N : {"__cxxabiv1", "std"}) {
4563 IdentifierInfo &NS = C.Idents.get(N);
4564 for (const auto *Result : DC->lookup(&NS)) {
4565 const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(Result);
4566 if (auto *LSD = dyn_cast<LinkageSpecDecl>(Result))
4567 for (const auto *Result : LSD->lookup(&NS))
4568 if ((ND = dyn_cast<NamespaceDecl>(Result)))
4569 break;
4571 if (ND)
4572 for (const auto *Result : ND->lookup(&CXXII))
4573 if (const auto *FD = dyn_cast<FunctionDecl>(Result))
4574 return FD;
4578 return nullptr;
4581 /// CreateRuntimeFunction - Create a new runtime function with the specified
4582 /// type and name.
4583 llvm::FunctionCallee
4584 CodeGenModule::CreateRuntimeFunction(llvm::FunctionType *FTy, StringRef Name,
4585 llvm::AttributeList ExtraAttrs, bool Local,
4586 bool AssumeConvergent) {
4587 if (AssumeConvergent) {
4588 ExtraAttrs =
4589 ExtraAttrs.addFnAttribute(VMContext, llvm::Attribute::Convergent);
4592 llvm::Constant *C =
4593 GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(), /*ForVTable=*/false,
4594 /*DontDefer=*/false, /*IsThunk=*/false,
4595 ExtraAttrs);
4597 if (auto *F = dyn_cast<llvm::Function>(C)) {
4598 if (F->empty()) {
4599 F->setCallingConv(getRuntimeCC());
4601 // In Windows Itanium environments, try to mark runtime functions
4602 // dllimport. For Mingw and MSVC, don't. We don't really know if the user
4603 // will link their standard library statically or dynamically. Marking
4604 // functions imported when they are not imported can cause linker errors
4605 // and warnings.
4606 if (!Local && getTriple().isWindowsItaniumEnvironment() &&
4607 !getCodeGenOpts().LTOVisibilityPublicStd) {
4608 const FunctionDecl *FD = GetRuntimeFunctionDecl(Context, Name);
4609 if (!FD || FD->hasAttr<DLLImportAttr>()) {
4610 F->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass);
4611 F->setLinkage(llvm::GlobalValue::ExternalLinkage);
4614 setDSOLocal(F);
4618 return {FTy, C};
4621 /// GetOrCreateLLVMGlobal - If the specified mangled name is not in the module,
4622 /// create and return an llvm GlobalVariable with the specified type and address
4623 /// space. If there is something in the module with the specified name, return
4624 /// it potentially bitcasted to the right type.
4626 /// If D is non-null, it specifies a decl that correspond to this. This is used
4627 /// to set the attributes on the global when it is first created.
4629 /// If IsForDefinition is true, it is guaranteed that an actual global with
4630 /// type Ty will be returned, not conversion of a variable with the same
4631 /// mangled name but some other type.
4632 llvm::Constant *
4633 CodeGenModule::GetOrCreateLLVMGlobal(StringRef MangledName, llvm::Type *Ty,
4634 LangAS AddrSpace, const VarDecl *D,
4635 ForDefinition_t IsForDefinition) {
4636 // Lookup the entry, lazily creating it if necessary.
4637 llvm::GlobalValue *Entry = GetGlobalValue(MangledName);
4638 unsigned TargetAS = getContext().getTargetAddressSpace(AddrSpace);
4639 if (Entry) {
4640 if (WeakRefReferences.erase(Entry)) {
4641 if (D && !D->hasAttr<WeakAttr>())
4642 Entry->setLinkage(llvm::Function::ExternalLinkage);
4645 // Handle dropped DLL attributes.
4646 if (D && !D->hasAttr<DLLImportAttr>() && !D->hasAttr<DLLExportAttr>() &&
4647 !shouldMapVisibilityToDLLExport(D))
4648 Entry->setDLLStorageClass(llvm::GlobalValue::DefaultStorageClass);
4650 if (LangOpts.OpenMP && !LangOpts.OpenMPSimd && D)
4651 getOpenMPRuntime().registerTargetGlobalVariable(D, Entry);
4653 if (Entry->getValueType() == Ty && Entry->getAddressSpace() == TargetAS)
4654 return Entry;
4656 // If there are two attempts to define the same mangled name, issue an
4657 // error.
4658 if (IsForDefinition && !Entry->isDeclaration()) {
4659 GlobalDecl OtherGD;
4660 const VarDecl *OtherD;
4662 // Check that D is not yet in DiagnosedConflictingDefinitions is required
4663 // to make sure that we issue an error only once.
4664 if (D && lookupRepresentativeDecl(MangledName, OtherGD) &&
4665 (D->getCanonicalDecl() != OtherGD.getCanonicalDecl().getDecl()) &&
4666 (OtherD = dyn_cast<VarDecl>(OtherGD.getDecl())) &&
4667 OtherD->hasInit() &&
4668 DiagnosedConflictingDefinitions.insert(D).second) {
4669 getDiags().Report(D->getLocation(), diag::err_duplicate_mangled_name)
4670 << MangledName;
4671 getDiags().Report(OtherGD.getDecl()->getLocation(),
4672 diag::note_previous_definition);
4676 // Make sure the result is of the correct type.
4677 if (Entry->getType()->getAddressSpace() != TargetAS)
4678 return llvm::ConstantExpr::getAddrSpaceCast(
4679 Entry, llvm::PointerType::get(Ty->getContext(), TargetAS));
4681 // (If global is requested for a definition, we always need to create a new
4682 // global, not just return a bitcast.)
4683 if (!IsForDefinition)
4684 return Entry;
4687 auto DAddrSpace = GetGlobalVarAddressSpace(D);
4689 auto *GV = new llvm::GlobalVariable(
4690 getModule(), Ty, false, llvm::GlobalValue::ExternalLinkage, nullptr,
4691 MangledName, nullptr, llvm::GlobalVariable::NotThreadLocal,
4692 getContext().getTargetAddressSpace(DAddrSpace));
4694 // If we already created a global with the same mangled name (but different
4695 // type) before, take its name and remove it from its parent.
4696 if (Entry) {
4697 GV->takeName(Entry);
4699 if (!Entry->use_empty()) {
4700 llvm::Constant *NewPtrForOldDecl =
4701 llvm::ConstantExpr::getBitCast(GV, Entry->getType());
4702 Entry->replaceAllUsesWith(NewPtrForOldDecl);
4705 Entry->eraseFromParent();
4708 // This is the first use or definition of a mangled name. If there is a
4709 // deferred decl with this name, remember that we need to emit it at the end
4710 // of the file.
4711 auto DDI = DeferredDecls.find(MangledName);
4712 if (DDI != DeferredDecls.end()) {
4713 // Move the potentially referenced deferred decl to the DeferredDeclsToEmit
4714 // list, and remove it from DeferredDecls (since we don't need it anymore).
4715 addDeferredDeclToEmit(DDI->second);
4716 DeferredDecls.erase(DDI);
4719 // Handle things which are present even on external declarations.
4720 if (D) {
4721 if (LangOpts.OpenMP && !LangOpts.OpenMPSimd)
4722 getOpenMPRuntime().registerTargetGlobalVariable(D, GV);
4724 // FIXME: This code is overly simple and should be merged with other global
4725 // handling.
4726 GV->setConstant(D->getType().isConstantStorage(getContext(), false, false));
4728 GV->setAlignment(getContext().getDeclAlign(D).getAsAlign());
4730 setLinkageForGV(GV, D);
4732 if (D->getTLSKind()) {
4733 if (D->getTLSKind() == VarDecl::TLS_Dynamic)
4734 CXXThreadLocals.push_back(D);
4735 setTLSMode(GV, *D);
4738 setGVProperties(GV, D);
4740 // If required by the ABI, treat declarations of static data members with
4741 // inline initializers as definitions.
4742 if (getContext().isMSStaticDataMemberInlineDefinition(D)) {
4743 EmitGlobalVarDefinition(D);
4746 // Emit section information for extern variables.
4747 if (D->hasExternalStorage()) {
4748 if (const SectionAttr *SA = D->getAttr<SectionAttr>())
4749 GV->setSection(SA->getName());
4752 // Handle XCore specific ABI requirements.
4753 if (getTriple().getArch() == llvm::Triple::xcore &&
4754 D->getLanguageLinkage() == CLanguageLinkage &&
4755 D->getType().isConstant(Context) &&
4756 isExternallyVisible(D->getLinkageAndVisibility().getLinkage()))
4757 GV->setSection(".cp.rodata");
4759 // Check if we a have a const declaration with an initializer, we may be
4760 // able to emit it as available_externally to expose it's value to the
4761 // optimizer.
4762 if (Context.getLangOpts().CPlusPlus && GV->hasExternalLinkage() &&
4763 D->getType().isConstQualified() && !GV->hasInitializer() &&
4764 !D->hasDefinition() && D->hasInit() && !D->hasAttr<DLLImportAttr>()) {
4765 const auto *Record =
4766 Context.getBaseElementType(D->getType())->getAsCXXRecordDecl();
4767 bool HasMutableFields = Record && Record->hasMutableFields();
4768 if (!HasMutableFields) {
4769 const VarDecl *InitDecl;
4770 const Expr *InitExpr = D->getAnyInitializer(InitDecl);
4771 if (InitExpr) {
4772 ConstantEmitter emitter(*this);
4773 llvm::Constant *Init = emitter.tryEmitForInitializer(*InitDecl);
4774 if (Init) {
4775 auto *InitType = Init->getType();
4776 if (GV->getValueType() != InitType) {
4777 // The type of the initializer does not match the definition.
4778 // This happens when an initializer has a different type from
4779 // the type of the global (because of padding at the end of a
4780 // structure for instance).
4781 GV->setName(StringRef());
4782 // Make a new global with the correct type, this is now guaranteed
4783 // to work.
4784 auto *NewGV = cast<llvm::GlobalVariable>(
4785 GetAddrOfGlobalVar(D, InitType, IsForDefinition)
4786 ->stripPointerCasts());
4788 // Erase the old global, since it is no longer used.
4789 GV->eraseFromParent();
4790 GV = NewGV;
4791 } else {
4792 GV->setInitializer(Init);
4793 GV->setConstant(true);
4794 GV->setLinkage(llvm::GlobalValue::AvailableExternallyLinkage);
4796 emitter.finalize(GV);
4803 if (D &&
4804 D->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly) {
4805 getTargetCodeGenInfo().setTargetAttributes(D, GV, *this);
4806 // External HIP managed variables needed to be recorded for transformation
4807 // in both device and host compilations.
4808 if (getLangOpts().CUDA && D && D->hasAttr<HIPManagedAttr>() &&
4809 D->hasExternalStorage())
4810 getCUDARuntime().handleVarRegistration(D, *GV);
4813 if (D)
4814 SanitizerMD->reportGlobal(GV, *D);
4816 LangAS ExpectedAS =
4817 D ? D->getType().getAddressSpace()
4818 : (LangOpts.OpenCL ? LangAS::opencl_global : LangAS::Default);
4819 assert(getContext().getTargetAddressSpace(ExpectedAS) == TargetAS);
4820 if (DAddrSpace != ExpectedAS) {
4821 return getTargetCodeGenInfo().performAddrSpaceCast(
4822 *this, GV, DAddrSpace, ExpectedAS,
4823 llvm::PointerType::get(getLLVMContext(), TargetAS));
4826 return GV;
4829 llvm::Constant *
4830 CodeGenModule::GetAddrOfGlobal(GlobalDecl GD, ForDefinition_t IsForDefinition) {
4831 const Decl *D = GD.getDecl();
4833 if (isa<CXXConstructorDecl>(D) || isa<CXXDestructorDecl>(D))
4834 return getAddrOfCXXStructor(GD, /*FnInfo=*/nullptr, /*FnType=*/nullptr,
4835 /*DontDefer=*/false, IsForDefinition);
4837 if (isa<CXXMethodDecl>(D)) {
4838 auto FInfo =
4839 &getTypes().arrangeCXXMethodDeclaration(cast<CXXMethodDecl>(D));
4840 auto Ty = getTypes().GetFunctionType(*FInfo);
4841 return GetAddrOfFunction(GD, Ty, /*ForVTable=*/false, /*DontDefer=*/false,
4842 IsForDefinition);
4845 if (isa<FunctionDecl>(D)) {
4846 const CGFunctionInfo &FI = getTypes().arrangeGlobalDeclaration(GD);
4847 llvm::FunctionType *Ty = getTypes().GetFunctionType(FI);
4848 return GetAddrOfFunction(GD, Ty, /*ForVTable=*/false, /*DontDefer=*/false,
4849 IsForDefinition);
4852 return GetAddrOfGlobalVar(cast<VarDecl>(D), /*Ty=*/nullptr, IsForDefinition);
4855 llvm::GlobalVariable *CodeGenModule::CreateOrReplaceCXXRuntimeVariable(
4856 StringRef Name, llvm::Type *Ty, llvm::GlobalValue::LinkageTypes Linkage,
4857 llvm::Align Alignment) {
4858 llvm::GlobalVariable *GV = getModule().getNamedGlobal(Name);
4859 llvm::GlobalVariable *OldGV = nullptr;
4861 if (GV) {
4862 // Check if the variable has the right type.
4863 if (GV->getValueType() == Ty)
4864 return GV;
4866 // Because C++ name mangling, the only way we can end up with an already
4867 // existing global with the same name is if it has been declared extern "C".
4868 assert(GV->isDeclaration() && "Declaration has wrong type!");
4869 OldGV = GV;
4872 // Create a new variable.
4873 GV = new llvm::GlobalVariable(getModule(), Ty, /*isConstant=*/true,
4874 Linkage, nullptr, Name);
4876 if (OldGV) {
4877 // Replace occurrences of the old variable if needed.
4878 GV->takeName(OldGV);
4880 if (!OldGV->use_empty()) {
4881 llvm::Constant *NewPtrForOldDecl =
4882 llvm::ConstantExpr::getBitCast(GV, OldGV->getType());
4883 OldGV->replaceAllUsesWith(NewPtrForOldDecl);
4886 OldGV->eraseFromParent();
4889 if (supportsCOMDAT() && GV->isWeakForLinker() &&
4890 !GV->hasAvailableExternallyLinkage())
4891 GV->setComdat(TheModule.getOrInsertComdat(GV->getName()));
4893 GV->setAlignment(Alignment);
4895 return GV;
4898 /// GetAddrOfGlobalVar - Return the llvm::Constant for the address of the
4899 /// given global variable. If Ty is non-null and if the global doesn't exist,
4900 /// then it will be created with the specified type instead of whatever the
4901 /// normal requested type would be. If IsForDefinition is true, it is guaranteed
4902 /// that an actual global with type Ty will be returned, not conversion of a
4903 /// variable with the same mangled name but some other type.
4904 llvm::Constant *CodeGenModule::GetAddrOfGlobalVar(const VarDecl *D,
4905 llvm::Type *Ty,
4906 ForDefinition_t IsForDefinition) {
4907 assert(D->hasGlobalStorage() && "Not a global variable");
4908 QualType ASTTy = D->getType();
4909 if (!Ty)
4910 Ty = getTypes().ConvertTypeForMem(ASTTy);
4912 StringRef MangledName = getMangledName(D);
4913 return GetOrCreateLLVMGlobal(MangledName, Ty, ASTTy.getAddressSpace(), D,
4914 IsForDefinition);
4917 /// CreateRuntimeVariable - Create a new runtime global variable with the
4918 /// specified type and name.
4919 llvm::Constant *
4920 CodeGenModule::CreateRuntimeVariable(llvm::Type *Ty,
4921 StringRef Name) {
4922 LangAS AddrSpace = getContext().getLangOpts().OpenCL ? LangAS::opencl_global
4923 : LangAS::Default;
4924 auto *Ret = GetOrCreateLLVMGlobal(Name, Ty, AddrSpace, nullptr);
4925 setDSOLocal(cast<llvm::GlobalValue>(Ret->stripPointerCasts()));
4926 return Ret;
4929 void CodeGenModule::EmitTentativeDefinition(const VarDecl *D) {
4930 assert(!D->getInit() && "Cannot emit definite definitions here!");
4932 StringRef MangledName = getMangledName(D);
4933 llvm::GlobalValue *GV = GetGlobalValue(MangledName);
4935 // We already have a definition, not declaration, with the same mangled name.
4936 // Emitting of declaration is not required (and actually overwrites emitted
4937 // definition).
4938 if (GV && !GV->isDeclaration())
4939 return;
4941 // If we have not seen a reference to this variable yet, place it into the
4942 // deferred declarations table to be emitted if needed later.
4943 if (!MustBeEmitted(D) && !GV) {
4944 DeferredDecls[MangledName] = D;
4945 return;
4948 // The tentative definition is the only definition.
4949 EmitGlobalVarDefinition(D);
4952 void CodeGenModule::EmitExternalDeclaration(const VarDecl *D) {
4953 EmitExternalVarDeclaration(D);
4956 CharUnits CodeGenModule::GetTargetTypeStoreSize(llvm::Type *Ty) const {
4957 return Context.toCharUnitsFromBits(
4958 getDataLayout().getTypeStoreSizeInBits(Ty));
4961 LangAS CodeGenModule::GetGlobalVarAddressSpace(const VarDecl *D) {
4962 if (LangOpts.OpenCL) {
4963 LangAS AS = D ? D->getType().getAddressSpace() : LangAS::opencl_global;
4964 assert(AS == LangAS::opencl_global ||
4965 AS == LangAS::opencl_global_device ||
4966 AS == LangAS::opencl_global_host ||
4967 AS == LangAS::opencl_constant ||
4968 AS == LangAS::opencl_local ||
4969 AS >= LangAS::FirstTargetAddressSpace);
4970 return AS;
4973 if (LangOpts.SYCLIsDevice &&
4974 (!D || D->getType().getAddressSpace() == LangAS::Default))
4975 return LangAS::sycl_global;
4977 if (LangOpts.CUDA && LangOpts.CUDAIsDevice) {
4978 if (D) {
4979 if (D->hasAttr<CUDAConstantAttr>())
4980 return LangAS::cuda_constant;
4981 if (D->hasAttr<CUDASharedAttr>())
4982 return LangAS::cuda_shared;
4983 if (D->hasAttr<CUDADeviceAttr>())
4984 return LangAS::cuda_device;
4985 if (D->getType().isConstQualified())
4986 return LangAS::cuda_constant;
4988 return LangAS::cuda_device;
4991 if (LangOpts.OpenMP) {
4992 LangAS AS;
4993 if (OpenMPRuntime->hasAllocateAttributeForGlobalVar(D, AS))
4994 return AS;
4996 return getTargetCodeGenInfo().getGlobalVarAddressSpace(*this, D);
4999 LangAS CodeGenModule::GetGlobalConstantAddressSpace() const {
5000 // OpenCL v1.2 s6.5.3: a string literal is in the constant address space.
5001 if (LangOpts.OpenCL)
5002 return LangAS::opencl_constant;
5003 if (LangOpts.SYCLIsDevice)
5004 return LangAS::sycl_global;
5005 if (LangOpts.HIP && LangOpts.CUDAIsDevice && getTriple().isSPIRV())
5006 // For HIPSPV map literals to cuda_device (maps to CrossWorkGroup in SPIR-V)
5007 // instead of default AS (maps to Generic in SPIR-V). Otherwise, we end up
5008 // with OpVariable instructions with Generic storage class which is not
5009 // allowed (SPIR-V V1.6 s3.42.8). Also, mapping literals to SPIR-V
5010 // UniformConstant storage class is not viable as pointers to it may not be
5011 // casted to Generic pointers which are used to model HIP's "flat" pointers.
5012 return LangAS::cuda_device;
5013 if (auto AS = getTarget().getConstantAddressSpace())
5014 return *AS;
5015 return LangAS::Default;
5018 // In address space agnostic languages, string literals are in default address
5019 // space in AST. However, certain targets (e.g. amdgcn) request them to be
5020 // emitted in constant address space in LLVM IR. To be consistent with other
5021 // parts of AST, string literal global variables in constant address space
5022 // need to be casted to default address space before being put into address
5023 // map and referenced by other part of CodeGen.
5024 // In OpenCL, string literals are in constant address space in AST, therefore
5025 // they should not be casted to default address space.
5026 static llvm::Constant *
5027 castStringLiteralToDefaultAddressSpace(CodeGenModule &CGM,
5028 llvm::GlobalVariable *GV) {
5029 llvm::Constant *Cast = GV;
5030 if (!CGM.getLangOpts().OpenCL) {
5031 auto AS = CGM.GetGlobalConstantAddressSpace();
5032 if (AS != LangAS::Default)
5033 Cast = CGM.getTargetCodeGenInfo().performAddrSpaceCast(
5034 CGM, GV, AS, LangAS::Default,
5035 llvm::PointerType::get(
5036 CGM.getLLVMContext(),
5037 CGM.getContext().getTargetAddressSpace(LangAS::Default)));
5039 return Cast;
5042 template<typename SomeDecl>
5043 void CodeGenModule::MaybeHandleStaticInExternC(const SomeDecl *D,
5044 llvm::GlobalValue *GV) {
5045 if (!getLangOpts().CPlusPlus)
5046 return;
5048 // Must have 'used' attribute, or else inline assembly can't rely on
5049 // the name existing.
5050 if (!D->template hasAttr<UsedAttr>())
5051 return;
5053 // Must have internal linkage and an ordinary name.
5054 if (!D->getIdentifier() || D->getFormalLinkage() != Linkage::Internal)
5055 return;
5057 // Must be in an extern "C" context. Entities declared directly within
5058 // a record are not extern "C" even if the record is in such a context.
5059 const SomeDecl *First = D->getFirstDecl();
5060 if (First->getDeclContext()->isRecord() || !First->isInExternCContext())
5061 return;
5063 // OK, this is an internal linkage entity inside an extern "C" linkage
5064 // specification. Make a note of that so we can give it the "expected"
5065 // mangled name if nothing else is using that name.
5066 std::pair<StaticExternCMap::iterator, bool> R =
5067 StaticExternCValues.insert(std::make_pair(D->getIdentifier(), GV));
5069 // If we have multiple internal linkage entities with the same name
5070 // in extern "C" regions, none of them gets that name.
5071 if (!R.second)
5072 R.first->second = nullptr;
5075 static bool shouldBeInCOMDAT(CodeGenModule &CGM, const Decl &D) {
5076 if (!CGM.supportsCOMDAT())
5077 return false;
5079 if (D.hasAttr<SelectAnyAttr>())
5080 return true;
5082 GVALinkage Linkage;
5083 if (auto *VD = dyn_cast<VarDecl>(&D))
5084 Linkage = CGM.getContext().GetGVALinkageForVariable(VD);
5085 else
5086 Linkage = CGM.getContext().GetGVALinkageForFunction(cast<FunctionDecl>(&D));
5088 switch (Linkage) {
5089 case GVA_Internal:
5090 case GVA_AvailableExternally:
5091 case GVA_StrongExternal:
5092 return false;
5093 case GVA_DiscardableODR:
5094 case GVA_StrongODR:
5095 return true;
5097 llvm_unreachable("No such linkage");
5100 bool CodeGenModule::supportsCOMDAT() const {
5101 return getTriple().supportsCOMDAT();
5104 void CodeGenModule::maybeSetTrivialComdat(const Decl &D,
5105 llvm::GlobalObject &GO) {
5106 if (!shouldBeInCOMDAT(*this, D))
5107 return;
5108 GO.setComdat(TheModule.getOrInsertComdat(GO.getName()));
5111 /// Pass IsTentative as true if you want to create a tentative definition.
5112 void CodeGenModule::EmitGlobalVarDefinition(const VarDecl *D,
5113 bool IsTentative) {
5114 // OpenCL global variables of sampler type are translated to function calls,
5115 // therefore no need to be translated.
5116 QualType ASTTy = D->getType();
5117 if (getLangOpts().OpenCL && ASTTy->isSamplerT())
5118 return;
5120 // If this is OpenMP device, check if it is legal to emit this global
5121 // normally.
5122 if (LangOpts.OpenMPIsTargetDevice && OpenMPRuntime &&
5123 OpenMPRuntime->emitTargetGlobalVariable(D))
5124 return;
5126 llvm::TrackingVH<llvm::Constant> Init;
5127 bool NeedsGlobalCtor = false;
5128 // Whether the definition of the variable is available externally.
5129 // If yes, we shouldn't emit the GloablCtor and GlobalDtor for the variable
5130 // since this is the job for its original source.
5131 bool IsDefinitionAvailableExternally =
5132 getContext().GetGVALinkageForVariable(D) == GVA_AvailableExternally;
5133 bool NeedsGlobalDtor =
5134 !IsDefinitionAvailableExternally &&
5135 D->needsDestruction(getContext()) == QualType::DK_cxx_destructor;
5137 const VarDecl *InitDecl;
5138 const Expr *InitExpr = D->getAnyInitializer(InitDecl);
5140 std::optional<ConstantEmitter> emitter;
5142 // CUDA E.2.4.1 "__shared__ variables cannot have an initialization
5143 // as part of their declaration." Sema has already checked for
5144 // error cases, so we just need to set Init to UndefValue.
5145 bool IsCUDASharedVar =
5146 getLangOpts().CUDAIsDevice && D->hasAttr<CUDASharedAttr>();
5147 // Shadows of initialized device-side global variables are also left
5148 // undefined.
5149 // Managed Variables should be initialized on both host side and device side.
5150 bool IsCUDAShadowVar =
5151 !getLangOpts().CUDAIsDevice && !D->hasAttr<HIPManagedAttr>() &&
5152 (D->hasAttr<CUDAConstantAttr>() || D->hasAttr<CUDADeviceAttr>() ||
5153 D->hasAttr<CUDASharedAttr>());
5154 bool IsCUDADeviceShadowVar =
5155 getLangOpts().CUDAIsDevice && !D->hasAttr<HIPManagedAttr>() &&
5156 (D->getType()->isCUDADeviceBuiltinSurfaceType() ||
5157 D->getType()->isCUDADeviceBuiltinTextureType());
5158 if (getLangOpts().CUDA &&
5159 (IsCUDASharedVar || IsCUDAShadowVar || IsCUDADeviceShadowVar))
5160 Init = llvm::UndefValue::get(getTypes().ConvertTypeForMem(ASTTy));
5161 else if (D->hasAttr<LoaderUninitializedAttr>())
5162 Init = llvm::UndefValue::get(getTypes().ConvertTypeForMem(ASTTy));
5163 else if (!InitExpr) {
5164 // This is a tentative definition; tentative definitions are
5165 // implicitly initialized with { 0 }.
5167 // Note that tentative definitions are only emitted at the end of
5168 // a translation unit, so they should never have incomplete
5169 // type. In addition, EmitTentativeDefinition makes sure that we
5170 // never attempt to emit a tentative definition if a real one
5171 // exists. A use may still exists, however, so we still may need
5172 // to do a RAUW.
5173 assert(!ASTTy->isIncompleteType() && "Unexpected incomplete type");
5174 Init = EmitNullConstant(D->getType());
5175 } else {
5176 initializedGlobalDecl = GlobalDecl(D);
5177 emitter.emplace(*this);
5178 llvm::Constant *Initializer = emitter->tryEmitForInitializer(*InitDecl);
5179 if (!Initializer) {
5180 QualType T = InitExpr->getType();
5181 if (D->getType()->isReferenceType())
5182 T = D->getType();
5184 if (getLangOpts().CPlusPlus) {
5185 if (InitDecl->hasFlexibleArrayInit(getContext()))
5186 ErrorUnsupported(D, "flexible array initializer");
5187 Init = EmitNullConstant(T);
5189 if (!IsDefinitionAvailableExternally)
5190 NeedsGlobalCtor = true;
5191 } else {
5192 ErrorUnsupported(D, "static initializer");
5193 Init = llvm::UndefValue::get(getTypes().ConvertType(T));
5195 } else {
5196 Init = Initializer;
5197 // We don't need an initializer, so remove the entry for the delayed
5198 // initializer position (just in case this entry was delayed) if we
5199 // also don't need to register a destructor.
5200 if (getLangOpts().CPlusPlus && !NeedsGlobalDtor)
5201 DelayedCXXInitPosition.erase(D);
5203 #ifndef NDEBUG
5204 CharUnits VarSize = getContext().getTypeSizeInChars(ASTTy) +
5205 InitDecl->getFlexibleArrayInitChars(getContext());
5206 CharUnits CstSize = CharUnits::fromQuantity(
5207 getDataLayout().getTypeAllocSize(Init->getType()));
5208 assert(VarSize == CstSize && "Emitted constant has unexpected size");
5209 #endif
5213 llvm::Type* InitType = Init->getType();
5214 llvm::Constant *Entry =
5215 GetAddrOfGlobalVar(D, InitType, ForDefinition_t(!IsTentative));
5217 // Strip off pointer casts if we got them.
5218 Entry = Entry->stripPointerCasts();
5220 // Entry is now either a Function or GlobalVariable.
5221 auto *GV = dyn_cast<llvm::GlobalVariable>(Entry);
5223 // We have a definition after a declaration with the wrong type.
5224 // We must make a new GlobalVariable* and update everything that used OldGV
5225 // (a declaration or tentative definition) with the new GlobalVariable*
5226 // (which will be a definition).
5228 // This happens if there is a prototype for a global (e.g.
5229 // "extern int x[];") and then a definition of a different type (e.g.
5230 // "int x[10];"). This also happens when an initializer has a different type
5231 // from the type of the global (this happens with unions).
5232 if (!GV || GV->getValueType() != InitType ||
5233 GV->getType()->getAddressSpace() !=
5234 getContext().getTargetAddressSpace(GetGlobalVarAddressSpace(D))) {
5236 // Move the old entry aside so that we'll create a new one.
5237 Entry->setName(StringRef());
5239 // Make a new global with the correct type, this is now guaranteed to work.
5240 GV = cast<llvm::GlobalVariable>(
5241 GetAddrOfGlobalVar(D, InitType, ForDefinition_t(!IsTentative))
5242 ->stripPointerCasts());
5244 // Replace all uses of the old global with the new global
5245 llvm::Constant *NewPtrForOldDecl =
5246 llvm::ConstantExpr::getPointerBitCastOrAddrSpaceCast(GV,
5247 Entry->getType());
5248 Entry->replaceAllUsesWith(NewPtrForOldDecl);
5250 // Erase the old global, since it is no longer used.
5251 cast<llvm::GlobalValue>(Entry)->eraseFromParent();
5254 MaybeHandleStaticInExternC(D, GV);
5256 if (D->hasAttr<AnnotateAttr>())
5257 AddGlobalAnnotations(D, GV);
5259 // Set the llvm linkage type as appropriate.
5260 llvm::GlobalValue::LinkageTypes Linkage = getLLVMLinkageVarDefinition(D);
5262 // CUDA B.2.1 "The __device__ qualifier declares a variable that resides on
5263 // the device. [...]"
5264 // CUDA B.2.2 "The __constant__ qualifier, optionally used together with
5265 // __device__, declares a variable that: [...]
5266 // Is accessible from all the threads within the grid and from the host
5267 // through the runtime library (cudaGetSymbolAddress() / cudaGetSymbolSize()
5268 // / cudaMemcpyToSymbol() / cudaMemcpyFromSymbol())."
5269 if (LangOpts.CUDA) {
5270 if (LangOpts.CUDAIsDevice) {
5271 if (Linkage != llvm::GlobalValue::InternalLinkage &&
5272 (D->hasAttr<CUDADeviceAttr>() || D->hasAttr<CUDAConstantAttr>() ||
5273 D->getType()->isCUDADeviceBuiltinSurfaceType() ||
5274 D->getType()->isCUDADeviceBuiltinTextureType()))
5275 GV->setExternallyInitialized(true);
5276 } else {
5277 getCUDARuntime().internalizeDeviceSideVar(D, Linkage);
5279 getCUDARuntime().handleVarRegistration(D, *GV);
5282 GV->setInitializer(Init);
5283 if (emitter)
5284 emitter->finalize(GV);
5286 // If it is safe to mark the global 'constant', do so now.
5287 GV->setConstant(!NeedsGlobalCtor && !NeedsGlobalDtor &&
5288 D->getType().isConstantStorage(getContext(), true, true));
5290 // If it is in a read-only section, mark it 'constant'.
5291 if (const SectionAttr *SA = D->getAttr<SectionAttr>()) {
5292 const ASTContext::SectionInfo &SI = Context.SectionInfos[SA->getName()];
5293 if ((SI.SectionFlags & ASTContext::PSF_Write) == 0)
5294 GV->setConstant(true);
5297 CharUnits AlignVal = getContext().getDeclAlign(D);
5298 // Check for alignment specifed in an 'omp allocate' directive.
5299 if (std::optional<CharUnits> AlignValFromAllocate =
5300 getOMPAllocateAlignment(D))
5301 AlignVal = *AlignValFromAllocate;
5302 GV->setAlignment(AlignVal.getAsAlign());
5304 // On Darwin, unlike other Itanium C++ ABI platforms, the thread-wrapper
5305 // function is only defined alongside the variable, not also alongside
5306 // callers. Normally, all accesses to a thread_local go through the
5307 // thread-wrapper in order to ensure initialization has occurred, underlying
5308 // variable will never be used other than the thread-wrapper, so it can be
5309 // converted to internal linkage.
5311 // However, if the variable has the 'constinit' attribute, it _can_ be
5312 // referenced directly, without calling the thread-wrapper, so the linkage
5313 // must not be changed.
5315 // Additionally, if the variable isn't plain external linkage, e.g. if it's
5316 // weak or linkonce, the de-duplication semantics are important to preserve,
5317 // so we don't change the linkage.
5318 if (D->getTLSKind() == VarDecl::TLS_Dynamic &&
5319 Linkage == llvm::GlobalValue::ExternalLinkage &&
5320 Context.getTargetInfo().getTriple().isOSDarwin() &&
5321 !D->hasAttr<ConstInitAttr>())
5322 Linkage = llvm::GlobalValue::InternalLinkage;
5324 GV->setLinkage(Linkage);
5325 if (D->hasAttr<DLLImportAttr>())
5326 GV->setDLLStorageClass(llvm::GlobalVariable::DLLImportStorageClass);
5327 else if (D->hasAttr<DLLExportAttr>())
5328 GV->setDLLStorageClass(llvm::GlobalVariable::DLLExportStorageClass);
5329 else
5330 GV->setDLLStorageClass(llvm::GlobalVariable::DefaultStorageClass);
5332 if (Linkage == llvm::GlobalVariable::CommonLinkage) {
5333 // common vars aren't constant even if declared const.
5334 GV->setConstant(false);
5335 // Tentative definition of global variables may be initialized with
5336 // non-zero null pointers. In this case they should have weak linkage
5337 // since common linkage must have zero initializer and must not have
5338 // explicit section therefore cannot have non-zero initial value.
5339 if (!GV->getInitializer()->isNullValue())
5340 GV->setLinkage(llvm::GlobalVariable::WeakAnyLinkage);
5343 setNonAliasAttributes(D, GV);
5345 if (D->getTLSKind() && !GV->isThreadLocal()) {
5346 if (D->getTLSKind() == VarDecl::TLS_Dynamic)
5347 CXXThreadLocals.push_back(D);
5348 setTLSMode(GV, *D);
5351 maybeSetTrivialComdat(*D, *GV);
5353 // Emit the initializer function if necessary.
5354 if (NeedsGlobalCtor || NeedsGlobalDtor)
5355 EmitCXXGlobalVarDeclInitFunc(D, GV, NeedsGlobalCtor);
5357 SanitizerMD->reportGlobal(GV, *D, NeedsGlobalCtor);
5359 // Emit global variable debug information.
5360 if (CGDebugInfo *DI = getModuleDebugInfo())
5361 if (getCodeGenOpts().hasReducedDebugInfo())
5362 DI->EmitGlobalVariable(GV, D);
5365 void CodeGenModule::EmitExternalVarDeclaration(const VarDecl *D) {
5366 if (CGDebugInfo *DI = getModuleDebugInfo())
5367 if (getCodeGenOpts().hasReducedDebugInfo()) {
5368 QualType ASTTy = D->getType();
5369 llvm::Type *Ty = getTypes().ConvertTypeForMem(D->getType());
5370 llvm::Constant *GV =
5371 GetOrCreateLLVMGlobal(D->getName(), Ty, ASTTy.getAddressSpace(), D);
5372 DI->EmitExternalVariable(
5373 cast<llvm::GlobalVariable>(GV->stripPointerCasts()), D);
5377 static bool isVarDeclStrongDefinition(const ASTContext &Context,
5378 CodeGenModule &CGM, const VarDecl *D,
5379 bool NoCommon) {
5380 // Don't give variables common linkage if -fno-common was specified unless it
5381 // was overridden by a NoCommon attribute.
5382 if ((NoCommon || D->hasAttr<NoCommonAttr>()) && !D->hasAttr<CommonAttr>())
5383 return true;
5385 // C11 6.9.2/2:
5386 // A declaration of an identifier for an object that has file scope without
5387 // an initializer, and without a storage-class specifier or with the
5388 // storage-class specifier static, constitutes a tentative definition.
5389 if (D->getInit() || D->hasExternalStorage())
5390 return true;
5392 // A variable cannot be both common and exist in a section.
5393 if (D->hasAttr<SectionAttr>())
5394 return true;
5396 // A variable cannot be both common and exist in a section.
5397 // We don't try to determine which is the right section in the front-end.
5398 // If no specialized section name is applicable, it will resort to default.
5399 if (D->hasAttr<PragmaClangBSSSectionAttr>() ||
5400 D->hasAttr<PragmaClangDataSectionAttr>() ||
5401 D->hasAttr<PragmaClangRelroSectionAttr>() ||
5402 D->hasAttr<PragmaClangRodataSectionAttr>())
5403 return true;
5405 // Thread local vars aren't considered common linkage.
5406 if (D->getTLSKind())
5407 return true;
5409 // Tentative definitions marked with WeakImportAttr are true definitions.
5410 if (D->hasAttr<WeakImportAttr>())
5411 return true;
5413 // A variable cannot be both common and exist in a comdat.
5414 if (shouldBeInCOMDAT(CGM, *D))
5415 return true;
5417 // Declarations with a required alignment do not have common linkage in MSVC
5418 // mode.
5419 if (Context.getTargetInfo().getCXXABI().isMicrosoft()) {
5420 if (D->hasAttr<AlignedAttr>())
5421 return true;
5422 QualType VarType = D->getType();
5423 if (Context.isAlignmentRequired(VarType))
5424 return true;
5426 if (const auto *RT = VarType->getAs<RecordType>()) {
5427 const RecordDecl *RD = RT->getDecl();
5428 for (const FieldDecl *FD : RD->fields()) {
5429 if (FD->isBitField())
5430 continue;
5431 if (FD->hasAttr<AlignedAttr>())
5432 return true;
5433 if (Context.isAlignmentRequired(FD->getType()))
5434 return true;
5439 // Microsoft's link.exe doesn't support alignments greater than 32 bytes for
5440 // common symbols, so symbols with greater alignment requirements cannot be
5441 // common.
5442 // Other COFF linkers (ld.bfd and LLD) support arbitrary power-of-two
5443 // alignments for common symbols via the aligncomm directive, so this
5444 // restriction only applies to MSVC environments.
5445 if (Context.getTargetInfo().getTriple().isKnownWindowsMSVCEnvironment() &&
5446 Context.getTypeAlignIfKnown(D->getType()) >
5447 Context.toBits(CharUnits::fromQuantity(32)))
5448 return true;
5450 return false;
5453 llvm::GlobalValue::LinkageTypes
5454 CodeGenModule::getLLVMLinkageForDeclarator(const DeclaratorDecl *D,
5455 GVALinkage Linkage) {
5456 if (Linkage == GVA_Internal)
5457 return llvm::Function::InternalLinkage;
5459 if (D->hasAttr<WeakAttr>())
5460 return llvm::GlobalVariable::WeakAnyLinkage;
5462 if (const auto *FD = D->getAsFunction())
5463 if (FD->isMultiVersion() && Linkage == GVA_AvailableExternally)
5464 return llvm::GlobalVariable::LinkOnceAnyLinkage;
5466 // We are guaranteed to have a strong definition somewhere else,
5467 // so we can use available_externally linkage.
5468 if (Linkage == GVA_AvailableExternally)
5469 return llvm::GlobalValue::AvailableExternallyLinkage;
5471 // Note that Apple's kernel linker doesn't support symbol
5472 // coalescing, so we need to avoid linkonce and weak linkages there.
5473 // Normally, this means we just map to internal, but for explicit
5474 // instantiations we'll map to external.
5476 // In C++, the compiler has to emit a definition in every translation unit
5477 // that references the function. We should use linkonce_odr because
5478 // a) if all references in this translation unit are optimized away, we
5479 // don't need to codegen it. b) if the function persists, it needs to be
5480 // merged with other definitions. c) C++ has the ODR, so we know the
5481 // definition is dependable.
5482 if (Linkage == GVA_DiscardableODR)
5483 return !Context.getLangOpts().AppleKext ? llvm::Function::LinkOnceODRLinkage
5484 : llvm::Function::InternalLinkage;
5486 // An explicit instantiation of a template has weak linkage, since
5487 // explicit instantiations can occur in multiple translation units
5488 // and must all be equivalent. However, we are not allowed to
5489 // throw away these explicit instantiations.
5491 // CUDA/HIP: For -fno-gpu-rdc case, device code is limited to one TU,
5492 // so say that CUDA templates are either external (for kernels) or internal.
5493 // This lets llvm perform aggressive inter-procedural optimizations. For
5494 // -fgpu-rdc case, device function calls across multiple TU's are allowed,
5495 // therefore we need to follow the normal linkage paradigm.
5496 if (Linkage == GVA_StrongODR) {
5497 if (getLangOpts().AppleKext)
5498 return llvm::Function::ExternalLinkage;
5499 if (getLangOpts().CUDA && getLangOpts().CUDAIsDevice &&
5500 !getLangOpts().GPURelocatableDeviceCode)
5501 return D->hasAttr<CUDAGlobalAttr>() ? llvm::Function::ExternalLinkage
5502 : llvm::Function::InternalLinkage;
5503 return llvm::Function::WeakODRLinkage;
5506 // C++ doesn't have tentative definitions and thus cannot have common
5507 // linkage.
5508 if (!getLangOpts().CPlusPlus && isa<VarDecl>(D) &&
5509 !isVarDeclStrongDefinition(Context, *this, cast<VarDecl>(D),
5510 CodeGenOpts.NoCommon))
5511 return llvm::GlobalVariable::CommonLinkage;
5513 // selectany symbols are externally visible, so use weak instead of
5514 // linkonce. MSVC optimizes away references to const selectany globals, so
5515 // all definitions should be the same and ODR linkage should be used.
5516 // http://msdn.microsoft.com/en-us/library/5tkz6s71.aspx
5517 if (D->hasAttr<SelectAnyAttr>())
5518 return llvm::GlobalVariable::WeakODRLinkage;
5520 // Otherwise, we have strong external linkage.
5521 assert(Linkage == GVA_StrongExternal);
5522 return llvm::GlobalVariable::ExternalLinkage;
5525 llvm::GlobalValue::LinkageTypes
5526 CodeGenModule::getLLVMLinkageVarDefinition(const VarDecl *VD) {
5527 GVALinkage Linkage = getContext().GetGVALinkageForVariable(VD);
5528 return getLLVMLinkageForDeclarator(VD, Linkage);
5531 /// Replace the uses of a function that was declared with a non-proto type.
5532 /// We want to silently drop extra arguments from call sites
5533 static void replaceUsesOfNonProtoConstant(llvm::Constant *old,
5534 llvm::Function *newFn) {
5535 // Fast path.
5536 if (old->use_empty()) return;
5538 llvm::Type *newRetTy = newFn->getReturnType();
5539 SmallVector<llvm::Value*, 4> newArgs;
5541 for (llvm::Value::use_iterator ui = old->use_begin(), ue = old->use_end();
5542 ui != ue; ) {
5543 llvm::Value::use_iterator use = ui++; // Increment before the use is erased.
5544 llvm::User *user = use->getUser();
5546 // Recognize and replace uses of bitcasts. Most calls to
5547 // unprototyped functions will use bitcasts.
5548 if (auto *bitcast = dyn_cast<llvm::ConstantExpr>(user)) {
5549 if (bitcast->getOpcode() == llvm::Instruction::BitCast)
5550 replaceUsesOfNonProtoConstant(bitcast, newFn);
5551 continue;
5554 // Recognize calls to the function.
5555 llvm::CallBase *callSite = dyn_cast<llvm::CallBase>(user);
5556 if (!callSite) continue;
5557 if (!callSite->isCallee(&*use))
5558 continue;
5560 // If the return types don't match exactly, then we can't
5561 // transform this call unless it's dead.
5562 if (callSite->getType() != newRetTy && !callSite->use_empty())
5563 continue;
5565 // Get the call site's attribute list.
5566 SmallVector<llvm::AttributeSet, 8> newArgAttrs;
5567 llvm::AttributeList oldAttrs = callSite->getAttributes();
5569 // If the function was passed too few arguments, don't transform.
5570 unsigned newNumArgs = newFn->arg_size();
5571 if (callSite->arg_size() < newNumArgs)
5572 continue;
5574 // If extra arguments were passed, we silently drop them.
5575 // If any of the types mismatch, we don't transform.
5576 unsigned argNo = 0;
5577 bool dontTransform = false;
5578 for (llvm::Argument &A : newFn->args()) {
5579 if (callSite->getArgOperand(argNo)->getType() != A.getType()) {
5580 dontTransform = true;
5581 break;
5584 // Add any parameter attributes.
5585 newArgAttrs.push_back(oldAttrs.getParamAttrs(argNo));
5586 argNo++;
5588 if (dontTransform)
5589 continue;
5591 // Okay, we can transform this. Create the new call instruction and copy
5592 // over the required information.
5593 newArgs.append(callSite->arg_begin(), callSite->arg_begin() + argNo);
5595 // Copy over any operand bundles.
5596 SmallVector<llvm::OperandBundleDef, 1> newBundles;
5597 callSite->getOperandBundlesAsDefs(newBundles);
5599 llvm::CallBase *newCall;
5600 if (isa<llvm::CallInst>(callSite)) {
5601 newCall =
5602 llvm::CallInst::Create(newFn, newArgs, newBundles, "", callSite);
5603 } else {
5604 auto *oldInvoke = cast<llvm::InvokeInst>(callSite);
5605 newCall = llvm::InvokeInst::Create(newFn, oldInvoke->getNormalDest(),
5606 oldInvoke->getUnwindDest(), newArgs,
5607 newBundles, "", callSite);
5609 newArgs.clear(); // for the next iteration
5611 if (!newCall->getType()->isVoidTy())
5612 newCall->takeName(callSite);
5613 newCall->setAttributes(
5614 llvm::AttributeList::get(newFn->getContext(), oldAttrs.getFnAttrs(),
5615 oldAttrs.getRetAttrs(), newArgAttrs));
5616 newCall->setCallingConv(callSite->getCallingConv());
5618 // Finally, remove the old call, replacing any uses with the new one.
5619 if (!callSite->use_empty())
5620 callSite->replaceAllUsesWith(newCall);
5622 // Copy debug location attached to CI.
5623 if (callSite->getDebugLoc())
5624 newCall->setDebugLoc(callSite->getDebugLoc());
5626 callSite->eraseFromParent();
5630 /// ReplaceUsesOfNonProtoTypeWithRealFunction - This function is called when we
5631 /// implement a function with no prototype, e.g. "int foo() {}". If there are
5632 /// existing call uses of the old function in the module, this adjusts them to
5633 /// call the new function directly.
5635 /// This is not just a cleanup: the always_inline pass requires direct calls to
5636 /// functions to be able to inline them. If there is a bitcast in the way, it
5637 /// won't inline them. Instcombine normally deletes these calls, but it isn't
5638 /// run at -O0.
5639 static void ReplaceUsesOfNonProtoTypeWithRealFunction(llvm::GlobalValue *Old,
5640 llvm::Function *NewFn) {
5641 // If we're redefining a global as a function, don't transform it.
5642 if (!isa<llvm::Function>(Old)) return;
5644 replaceUsesOfNonProtoConstant(Old, NewFn);
5647 void CodeGenModule::HandleCXXStaticMemberVarInstantiation(VarDecl *VD) {
5648 auto DK = VD->isThisDeclarationADefinition();
5649 if (DK == VarDecl::Definition && VD->hasAttr<DLLImportAttr>())
5650 return;
5652 TemplateSpecializationKind TSK = VD->getTemplateSpecializationKind();
5653 // If we have a definition, this might be a deferred decl. If the
5654 // instantiation is explicit, make sure we emit it at the end.
5655 if (VD->getDefinition() && TSK == TSK_ExplicitInstantiationDefinition)
5656 GetAddrOfGlobalVar(VD);
5658 EmitTopLevelDecl(VD);
5661 void CodeGenModule::EmitGlobalFunctionDefinition(GlobalDecl GD,
5662 llvm::GlobalValue *GV) {
5663 const auto *D = cast<FunctionDecl>(GD.getDecl());
5665 // Compute the function info and LLVM type.
5666 const CGFunctionInfo &FI = getTypes().arrangeGlobalDeclaration(GD);
5667 llvm::FunctionType *Ty = getTypes().GetFunctionType(FI);
5669 // Get or create the prototype for the function.
5670 if (!GV || (GV->getValueType() != Ty))
5671 GV = cast<llvm::GlobalValue>(GetAddrOfFunction(GD, Ty, /*ForVTable=*/false,
5672 /*DontDefer=*/true,
5673 ForDefinition));
5675 // Already emitted.
5676 if (!GV->isDeclaration())
5677 return;
5679 // We need to set linkage and visibility on the function before
5680 // generating code for it because various parts of IR generation
5681 // want to propagate this information down (e.g. to local static
5682 // declarations).
5683 auto *Fn = cast<llvm::Function>(GV);
5684 setFunctionLinkage(GD, Fn);
5686 // FIXME: this is redundant with part of setFunctionDefinitionAttributes
5687 setGVProperties(Fn, GD);
5689 MaybeHandleStaticInExternC(D, Fn);
5691 maybeSetTrivialComdat(*D, *Fn);
5693 CodeGenFunction(*this).GenerateCode(GD, Fn, FI);
5695 setNonAliasAttributes(GD, Fn);
5696 SetLLVMFunctionAttributesForDefinition(D, Fn);
5698 if (const ConstructorAttr *CA = D->getAttr<ConstructorAttr>())
5699 AddGlobalCtor(Fn, CA->getPriority());
5700 if (const DestructorAttr *DA = D->getAttr<DestructorAttr>())
5701 AddGlobalDtor(Fn, DA->getPriority(), true);
5702 if (D->hasAttr<AnnotateAttr>())
5703 AddGlobalAnnotations(D, Fn);
5704 if (getLangOpts().OpenMP && D->hasAttr<OMPDeclareTargetDeclAttr>())
5705 getOpenMPRuntime().emitDeclareTargetFunction(D, GV);
5708 void CodeGenModule::EmitAliasDefinition(GlobalDecl GD) {
5709 const auto *D = cast<ValueDecl>(GD.getDecl());
5710 const AliasAttr *AA = D->getAttr<AliasAttr>();
5711 assert(AA && "Not an alias?");
5713 StringRef MangledName = getMangledName(GD);
5715 if (AA->getAliasee() == MangledName) {
5716 Diags.Report(AA->getLocation(), diag::err_cyclic_alias) << 0;
5717 return;
5720 // If there is a definition in the module, then it wins over the alias.
5721 // This is dubious, but allow it to be safe. Just ignore the alias.
5722 llvm::GlobalValue *Entry = GetGlobalValue(MangledName);
5723 if (Entry && !Entry->isDeclaration())
5724 return;
5726 Aliases.push_back(GD);
5728 llvm::Type *DeclTy = getTypes().ConvertTypeForMem(D->getType());
5730 // Create a reference to the named value. This ensures that it is emitted
5731 // if a deferred decl.
5732 llvm::Constant *Aliasee;
5733 llvm::GlobalValue::LinkageTypes LT;
5734 if (isa<llvm::FunctionType>(DeclTy)) {
5735 Aliasee = GetOrCreateLLVMFunction(AA->getAliasee(), DeclTy, GD,
5736 /*ForVTable=*/false);
5737 LT = getFunctionLinkage(GD);
5738 } else {
5739 Aliasee = GetOrCreateLLVMGlobal(AA->getAliasee(), DeclTy, LangAS::Default,
5740 /*D=*/nullptr);
5741 if (const auto *VD = dyn_cast<VarDecl>(GD.getDecl()))
5742 LT = getLLVMLinkageVarDefinition(VD);
5743 else
5744 LT = getFunctionLinkage(GD);
5747 // Create the new alias itself, but don't set a name yet.
5748 unsigned AS = Aliasee->getType()->getPointerAddressSpace();
5749 auto *GA =
5750 llvm::GlobalAlias::create(DeclTy, AS, LT, "", Aliasee, &getModule());
5752 if (Entry) {
5753 if (GA->getAliasee() == Entry) {
5754 Diags.Report(AA->getLocation(), diag::err_cyclic_alias) << 0;
5755 return;
5758 assert(Entry->isDeclaration());
5760 // If there is a declaration in the module, then we had an extern followed
5761 // by the alias, as in:
5762 // extern int test6();
5763 // ...
5764 // int test6() __attribute__((alias("test7")));
5766 // Remove it and replace uses of it with the alias.
5767 GA->takeName(Entry);
5769 Entry->replaceAllUsesWith(llvm::ConstantExpr::getBitCast(GA,
5770 Entry->getType()));
5771 Entry->eraseFromParent();
5772 } else {
5773 GA->setName(MangledName);
5776 // Set attributes which are particular to an alias; this is a
5777 // specialization of the attributes which may be set on a global
5778 // variable/function.
5779 if (D->hasAttr<WeakAttr>() || D->hasAttr<WeakRefAttr>() ||
5780 D->isWeakImported()) {
5781 GA->setLinkage(llvm::Function::WeakAnyLinkage);
5784 if (const auto *VD = dyn_cast<VarDecl>(D))
5785 if (VD->getTLSKind())
5786 setTLSMode(GA, *VD);
5788 SetCommonAttributes(GD, GA);
5790 // Emit global alias debug information.
5791 if (isa<VarDecl>(D))
5792 if (CGDebugInfo *DI = getModuleDebugInfo())
5793 DI->EmitGlobalAlias(cast<llvm::GlobalValue>(GA->getAliasee()->stripPointerCasts()), GD);
5796 void CodeGenModule::emitIFuncDefinition(GlobalDecl GD) {
5797 const auto *D = cast<ValueDecl>(GD.getDecl());
5798 const IFuncAttr *IFA = D->getAttr<IFuncAttr>();
5799 assert(IFA && "Not an ifunc?");
5801 StringRef MangledName = getMangledName(GD);
5803 if (IFA->getResolver() == MangledName) {
5804 Diags.Report(IFA->getLocation(), diag::err_cyclic_alias) << 1;
5805 return;
5808 // Report an error if some definition overrides ifunc.
5809 llvm::GlobalValue *Entry = GetGlobalValue(MangledName);
5810 if (Entry && !Entry->isDeclaration()) {
5811 GlobalDecl OtherGD;
5812 if (lookupRepresentativeDecl(MangledName, OtherGD) &&
5813 DiagnosedConflictingDefinitions.insert(GD).second) {
5814 Diags.Report(D->getLocation(), diag::err_duplicate_mangled_name)
5815 << MangledName;
5816 Diags.Report(OtherGD.getDecl()->getLocation(),
5817 diag::note_previous_definition);
5819 return;
5822 Aliases.push_back(GD);
5824 llvm::Type *DeclTy = getTypes().ConvertTypeForMem(D->getType());
5825 llvm::Type *ResolverTy = llvm::GlobalIFunc::getResolverFunctionType(DeclTy);
5826 llvm::Constant *Resolver =
5827 GetOrCreateLLVMFunction(IFA->getResolver(), ResolverTy, {},
5828 /*ForVTable=*/false);
5829 llvm::GlobalIFunc *GIF =
5830 llvm::GlobalIFunc::create(DeclTy, 0, llvm::Function::ExternalLinkage,
5831 "", Resolver, &getModule());
5832 if (Entry) {
5833 if (GIF->getResolver() == Entry) {
5834 Diags.Report(IFA->getLocation(), diag::err_cyclic_alias) << 1;
5835 return;
5837 assert(Entry->isDeclaration());
5839 // If there is a declaration in the module, then we had an extern followed
5840 // by the ifunc, as in:
5841 // extern int test();
5842 // ...
5843 // int test() __attribute__((ifunc("resolver")));
5845 // Remove it and replace uses of it with the ifunc.
5846 GIF->takeName(Entry);
5848 Entry->replaceAllUsesWith(llvm::ConstantExpr::getBitCast(GIF,
5849 Entry->getType()));
5850 Entry->eraseFromParent();
5851 } else
5852 GIF->setName(MangledName);
5853 if (auto *F = dyn_cast<llvm::Function>(Resolver)) {
5854 F->addFnAttr(llvm::Attribute::DisableSanitizerInstrumentation);
5856 SetCommonAttributes(GD, GIF);
5859 llvm::Function *CodeGenModule::getIntrinsic(unsigned IID,
5860 ArrayRef<llvm::Type*> Tys) {
5861 return llvm::Intrinsic::getDeclaration(&getModule(), (llvm::Intrinsic::ID)IID,
5862 Tys);
5865 static llvm::StringMapEntry<llvm::GlobalVariable *> &
5866 GetConstantCFStringEntry(llvm::StringMap<llvm::GlobalVariable *> &Map,
5867 const StringLiteral *Literal, bool TargetIsLSB,
5868 bool &IsUTF16, unsigned &StringLength) {
5869 StringRef String = Literal->getString();
5870 unsigned NumBytes = String.size();
5872 // Check for simple case.
5873 if (!Literal->containsNonAsciiOrNull()) {
5874 StringLength = NumBytes;
5875 return *Map.insert(std::make_pair(String, nullptr)).first;
5878 // Otherwise, convert the UTF8 literals into a string of shorts.
5879 IsUTF16 = true;
5881 SmallVector<llvm::UTF16, 128> ToBuf(NumBytes + 1); // +1 for ending nulls.
5882 const llvm::UTF8 *FromPtr = (const llvm::UTF8 *)String.data();
5883 llvm::UTF16 *ToPtr = &ToBuf[0];
5885 (void)llvm::ConvertUTF8toUTF16(&FromPtr, FromPtr + NumBytes, &ToPtr,
5886 ToPtr + NumBytes, llvm::strictConversion);
5888 // ConvertUTF8toUTF16 returns the length in ToPtr.
5889 StringLength = ToPtr - &ToBuf[0];
5891 // Add an explicit null.
5892 *ToPtr = 0;
5893 return *Map.insert(std::make_pair(
5894 StringRef(reinterpret_cast<const char *>(ToBuf.data()),
5895 (StringLength + 1) * 2),
5896 nullptr)).first;
5899 ConstantAddress
5900 CodeGenModule::GetAddrOfConstantCFString(const StringLiteral *Literal) {
5901 unsigned StringLength = 0;
5902 bool isUTF16 = false;
5903 llvm::StringMapEntry<llvm::GlobalVariable *> &Entry =
5904 GetConstantCFStringEntry(CFConstantStringMap, Literal,
5905 getDataLayout().isLittleEndian(), isUTF16,
5906 StringLength);
5908 if (auto *C = Entry.second)
5909 return ConstantAddress(
5910 C, C->getValueType(), CharUnits::fromQuantity(C->getAlignment()));
5912 llvm::Constant *Zero = llvm::Constant::getNullValue(Int32Ty);
5913 llvm::Constant *Zeros[] = { Zero, Zero };
5915 const ASTContext &Context = getContext();
5916 const llvm::Triple &Triple = getTriple();
5918 const auto CFRuntime = getLangOpts().CFRuntime;
5919 const bool IsSwiftABI =
5920 static_cast<unsigned>(CFRuntime) >=
5921 static_cast<unsigned>(LangOptions::CoreFoundationABI::Swift);
5922 const bool IsSwift4_1 = CFRuntime == LangOptions::CoreFoundationABI::Swift4_1;
5924 // If we don't already have it, get __CFConstantStringClassReference.
5925 if (!CFConstantStringClassRef) {
5926 const char *CFConstantStringClassName = "__CFConstantStringClassReference";
5927 llvm::Type *Ty = getTypes().ConvertType(getContext().IntTy);
5928 Ty = llvm::ArrayType::get(Ty, 0);
5930 switch (CFRuntime) {
5931 default: break;
5932 case LangOptions::CoreFoundationABI::Swift: [[fallthrough]];
5933 case LangOptions::CoreFoundationABI::Swift5_0:
5934 CFConstantStringClassName =
5935 Triple.isOSDarwin() ? "$s15SwiftFoundation19_NSCFConstantStringCN"
5936 : "$s10Foundation19_NSCFConstantStringCN";
5937 Ty = IntPtrTy;
5938 break;
5939 case LangOptions::CoreFoundationABI::Swift4_2:
5940 CFConstantStringClassName =
5941 Triple.isOSDarwin() ? "$S15SwiftFoundation19_NSCFConstantStringCN"
5942 : "$S10Foundation19_NSCFConstantStringCN";
5943 Ty = IntPtrTy;
5944 break;
5945 case LangOptions::CoreFoundationABI::Swift4_1:
5946 CFConstantStringClassName =
5947 Triple.isOSDarwin() ? "__T015SwiftFoundation19_NSCFConstantStringCN"
5948 : "__T010Foundation19_NSCFConstantStringCN";
5949 Ty = IntPtrTy;
5950 break;
5953 llvm::Constant *C = CreateRuntimeVariable(Ty, CFConstantStringClassName);
5955 if (Triple.isOSBinFormatELF() || Triple.isOSBinFormatCOFF()) {
5956 llvm::GlobalValue *GV = nullptr;
5958 if ((GV = dyn_cast<llvm::GlobalValue>(C))) {
5959 IdentifierInfo &II = Context.Idents.get(GV->getName());
5960 TranslationUnitDecl *TUDecl = Context.getTranslationUnitDecl();
5961 DeclContext *DC = TranslationUnitDecl::castToDeclContext(TUDecl);
5963 const VarDecl *VD = nullptr;
5964 for (const auto *Result : DC->lookup(&II))
5965 if ((VD = dyn_cast<VarDecl>(Result)))
5966 break;
5968 if (Triple.isOSBinFormatELF()) {
5969 if (!VD)
5970 GV->setLinkage(llvm::GlobalValue::ExternalLinkage);
5971 } else {
5972 GV->setLinkage(llvm::GlobalValue::ExternalLinkage);
5973 if (!VD || !VD->hasAttr<DLLExportAttr>())
5974 GV->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass);
5975 else
5976 GV->setDLLStorageClass(llvm::GlobalValue::DLLExportStorageClass);
5979 setDSOLocal(GV);
5983 // Decay array -> ptr
5984 CFConstantStringClassRef =
5985 IsSwiftABI ? llvm::ConstantExpr::getPtrToInt(C, Ty)
5986 : llvm::ConstantExpr::getGetElementPtr(Ty, C, Zeros);
5989 QualType CFTy = Context.getCFConstantStringType();
5991 auto *STy = cast<llvm::StructType>(getTypes().ConvertType(CFTy));
5993 ConstantInitBuilder Builder(*this);
5994 auto Fields = Builder.beginStruct(STy);
5996 // Class pointer.
5997 Fields.add(cast<llvm::Constant>(CFConstantStringClassRef));
5999 // Flags.
6000 if (IsSwiftABI) {
6001 Fields.addInt(IntPtrTy, IsSwift4_1 ? 0x05 : 0x01);
6002 Fields.addInt(Int64Ty, isUTF16 ? 0x07d0 : 0x07c8);
6003 } else {
6004 Fields.addInt(IntTy, isUTF16 ? 0x07d0 : 0x07C8);
6007 // String pointer.
6008 llvm::Constant *C = nullptr;
6009 if (isUTF16) {
6010 auto Arr = llvm::ArrayRef(
6011 reinterpret_cast<uint16_t *>(const_cast<char *>(Entry.first().data())),
6012 Entry.first().size() / 2);
6013 C = llvm::ConstantDataArray::get(VMContext, Arr);
6014 } else {
6015 C = llvm::ConstantDataArray::getString(VMContext, Entry.first());
6018 // Note: -fwritable-strings doesn't make the backing store strings of
6019 // CFStrings writable.
6020 auto *GV =
6021 new llvm::GlobalVariable(getModule(), C->getType(), /*isConstant=*/true,
6022 llvm::GlobalValue::PrivateLinkage, C, ".str");
6023 GV->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
6024 // Don't enforce the target's minimum global alignment, since the only use
6025 // of the string is via this class initializer.
6026 CharUnits Align = isUTF16 ? Context.getTypeAlignInChars(Context.ShortTy)
6027 : Context.getTypeAlignInChars(Context.CharTy);
6028 GV->setAlignment(Align.getAsAlign());
6030 // FIXME: We set the section explicitly to avoid a bug in ld64 224.1.
6031 // Without it LLVM can merge the string with a non unnamed_addr one during
6032 // LTO. Doing that changes the section it ends in, which surprises ld64.
6033 if (Triple.isOSBinFormatMachO())
6034 GV->setSection(isUTF16 ? "__TEXT,__ustring"
6035 : "__TEXT,__cstring,cstring_literals");
6036 // Make sure the literal ends up in .rodata to allow for safe ICF and for
6037 // the static linker to adjust permissions to read-only later on.
6038 else if (Triple.isOSBinFormatELF())
6039 GV->setSection(".rodata");
6041 // String.
6042 llvm::Constant *Str =
6043 llvm::ConstantExpr::getGetElementPtr(GV->getValueType(), GV, Zeros);
6045 if (isUTF16)
6046 // Cast the UTF16 string to the correct type.
6047 Str = llvm::ConstantExpr::getBitCast(Str, Int8PtrTy);
6048 Fields.add(Str);
6050 // String length.
6051 llvm::IntegerType *LengthTy =
6052 llvm::IntegerType::get(getModule().getContext(),
6053 Context.getTargetInfo().getLongWidth());
6054 if (IsSwiftABI) {
6055 if (CFRuntime == LangOptions::CoreFoundationABI::Swift4_1 ||
6056 CFRuntime == LangOptions::CoreFoundationABI::Swift4_2)
6057 LengthTy = Int32Ty;
6058 else
6059 LengthTy = IntPtrTy;
6061 Fields.addInt(LengthTy, StringLength);
6063 // Swift ABI requires 8-byte alignment to ensure that the _Atomic(uint64_t) is
6064 // properly aligned on 32-bit platforms.
6065 CharUnits Alignment =
6066 IsSwiftABI ? Context.toCharUnitsFromBits(64) : getPointerAlign();
6068 // The struct.
6069 GV = Fields.finishAndCreateGlobal("_unnamed_cfstring_", Alignment,
6070 /*isConstant=*/false,
6071 llvm::GlobalVariable::PrivateLinkage);
6072 GV->addAttribute("objc_arc_inert");
6073 switch (Triple.getObjectFormat()) {
6074 case llvm::Triple::UnknownObjectFormat:
6075 llvm_unreachable("unknown file format");
6076 case llvm::Triple::DXContainer:
6077 case llvm::Triple::GOFF:
6078 case llvm::Triple::SPIRV:
6079 case llvm::Triple::XCOFF:
6080 llvm_unreachable("unimplemented");
6081 case llvm::Triple::COFF:
6082 case llvm::Triple::ELF:
6083 case llvm::Triple::Wasm:
6084 GV->setSection("cfstring");
6085 break;
6086 case llvm::Triple::MachO:
6087 GV->setSection("__DATA,__cfstring");
6088 break;
6090 Entry.second = GV;
6092 return ConstantAddress(GV, GV->getValueType(), Alignment);
6095 bool CodeGenModule::getExpressionLocationsEnabled() const {
6096 return !CodeGenOpts.EmitCodeView || CodeGenOpts.DebugColumnInfo;
6099 QualType CodeGenModule::getObjCFastEnumerationStateType() {
6100 if (ObjCFastEnumerationStateType.isNull()) {
6101 RecordDecl *D = Context.buildImplicitRecord("__objcFastEnumerationState");
6102 D->startDefinition();
6104 QualType FieldTypes[] = {
6105 Context.UnsignedLongTy, Context.getPointerType(Context.getObjCIdType()),
6106 Context.getPointerType(Context.UnsignedLongTy),
6107 Context.getConstantArrayType(Context.UnsignedLongTy, llvm::APInt(32, 5),
6108 nullptr, ArraySizeModifier::Normal, 0)};
6110 for (size_t i = 0; i < 4; ++i) {
6111 FieldDecl *Field = FieldDecl::Create(Context,
6113 SourceLocation(),
6114 SourceLocation(), nullptr,
6115 FieldTypes[i], /*TInfo=*/nullptr,
6116 /*BitWidth=*/nullptr,
6117 /*Mutable=*/false,
6118 ICIS_NoInit);
6119 Field->setAccess(AS_public);
6120 D->addDecl(Field);
6123 D->completeDefinition();
6124 ObjCFastEnumerationStateType = Context.getTagDeclType(D);
6127 return ObjCFastEnumerationStateType;
6130 llvm::Constant *
6131 CodeGenModule::GetConstantArrayFromStringLiteral(const StringLiteral *E) {
6132 assert(!E->getType()->isPointerType() && "Strings are always arrays");
6134 // Don't emit it as the address of the string, emit the string data itself
6135 // as an inline array.
6136 if (E->getCharByteWidth() == 1) {
6137 SmallString<64> Str(E->getString());
6139 // Resize the string to the right size, which is indicated by its type.
6140 const ConstantArrayType *CAT = Context.getAsConstantArrayType(E->getType());
6141 assert(CAT && "String literal not of constant array type!");
6142 Str.resize(CAT->getSize().getZExtValue());
6143 return llvm::ConstantDataArray::getString(VMContext, Str, false);
6146 auto *AType = cast<llvm::ArrayType>(getTypes().ConvertType(E->getType()));
6147 llvm::Type *ElemTy = AType->getElementType();
6148 unsigned NumElements = AType->getNumElements();
6150 // Wide strings have either 2-byte or 4-byte elements.
6151 if (ElemTy->getPrimitiveSizeInBits() == 16) {
6152 SmallVector<uint16_t, 32> Elements;
6153 Elements.reserve(NumElements);
6155 for(unsigned i = 0, e = E->getLength(); i != e; ++i)
6156 Elements.push_back(E->getCodeUnit(i));
6157 Elements.resize(NumElements);
6158 return llvm::ConstantDataArray::get(VMContext, Elements);
6161 assert(ElemTy->getPrimitiveSizeInBits() == 32);
6162 SmallVector<uint32_t, 32> Elements;
6163 Elements.reserve(NumElements);
6165 for(unsigned i = 0, e = E->getLength(); i != e; ++i)
6166 Elements.push_back(E->getCodeUnit(i));
6167 Elements.resize(NumElements);
6168 return llvm::ConstantDataArray::get(VMContext, Elements);
6171 static llvm::GlobalVariable *
6172 GenerateStringLiteral(llvm::Constant *C, llvm::GlobalValue::LinkageTypes LT,
6173 CodeGenModule &CGM, StringRef GlobalName,
6174 CharUnits Alignment) {
6175 unsigned AddrSpace = CGM.getContext().getTargetAddressSpace(
6176 CGM.GetGlobalConstantAddressSpace());
6178 llvm::Module &M = CGM.getModule();
6179 // Create a global variable for this string
6180 auto *GV = new llvm::GlobalVariable(
6181 M, C->getType(), !CGM.getLangOpts().WritableStrings, LT, C, GlobalName,
6182 nullptr, llvm::GlobalVariable::NotThreadLocal, AddrSpace);
6183 GV->setAlignment(Alignment.getAsAlign());
6184 GV->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
6185 if (GV->isWeakForLinker()) {
6186 assert(CGM.supportsCOMDAT() && "Only COFF uses weak string literals");
6187 GV->setComdat(M.getOrInsertComdat(GV->getName()));
6189 CGM.setDSOLocal(GV);
6191 return GV;
6194 /// GetAddrOfConstantStringFromLiteral - Return a pointer to a
6195 /// constant array for the given string literal.
6196 ConstantAddress
6197 CodeGenModule::GetAddrOfConstantStringFromLiteral(const StringLiteral *S,
6198 StringRef Name) {
6199 CharUnits Alignment = getContext().getAlignOfGlobalVarInChars(S->getType());
6201 llvm::Constant *C = GetConstantArrayFromStringLiteral(S);
6202 llvm::GlobalVariable **Entry = nullptr;
6203 if (!LangOpts.WritableStrings) {
6204 Entry = &ConstantStringMap[C];
6205 if (auto GV = *Entry) {
6206 if (uint64_t(Alignment.getQuantity()) > GV->getAlignment())
6207 GV->setAlignment(Alignment.getAsAlign());
6208 return ConstantAddress(castStringLiteralToDefaultAddressSpace(*this, GV),
6209 GV->getValueType(), Alignment);
6213 SmallString<256> MangledNameBuffer;
6214 StringRef GlobalVariableName;
6215 llvm::GlobalValue::LinkageTypes LT;
6217 // Mangle the string literal if that's how the ABI merges duplicate strings.
6218 // Don't do it if they are writable, since we don't want writes in one TU to
6219 // affect strings in another.
6220 if (getCXXABI().getMangleContext().shouldMangleStringLiteral(S) &&
6221 !LangOpts.WritableStrings) {
6222 llvm::raw_svector_ostream Out(MangledNameBuffer);
6223 getCXXABI().getMangleContext().mangleStringLiteral(S, Out);
6224 LT = llvm::GlobalValue::LinkOnceODRLinkage;
6225 GlobalVariableName = MangledNameBuffer;
6226 } else {
6227 LT = llvm::GlobalValue::PrivateLinkage;
6228 GlobalVariableName = Name;
6231 auto GV = GenerateStringLiteral(C, LT, *this, GlobalVariableName, Alignment);
6233 CGDebugInfo *DI = getModuleDebugInfo();
6234 if (DI && getCodeGenOpts().hasReducedDebugInfo())
6235 DI->AddStringLiteralDebugInfo(GV, S);
6237 if (Entry)
6238 *Entry = GV;
6240 SanitizerMD->reportGlobal(GV, S->getStrTokenLoc(0), "<string literal>");
6242 return ConstantAddress(castStringLiteralToDefaultAddressSpace(*this, GV),
6243 GV->getValueType(), Alignment);
6246 /// GetAddrOfConstantStringFromObjCEncode - Return a pointer to a constant
6247 /// array for the given ObjCEncodeExpr node.
6248 ConstantAddress
6249 CodeGenModule::GetAddrOfConstantStringFromObjCEncode(const ObjCEncodeExpr *E) {
6250 std::string Str;
6251 getContext().getObjCEncodingForType(E->getEncodedType(), Str);
6253 return GetAddrOfConstantCString(Str);
6256 /// GetAddrOfConstantCString - Returns a pointer to a character array containing
6257 /// the literal and a terminating '\0' character.
6258 /// The result has pointer to array type.
6259 ConstantAddress CodeGenModule::GetAddrOfConstantCString(
6260 const std::string &Str, const char *GlobalName) {
6261 StringRef StrWithNull(Str.c_str(), Str.size() + 1);
6262 CharUnits Alignment =
6263 getContext().getAlignOfGlobalVarInChars(getContext().CharTy);
6265 llvm::Constant *C =
6266 llvm::ConstantDataArray::getString(getLLVMContext(), StrWithNull, false);
6268 // Don't share any string literals if strings aren't constant.
6269 llvm::GlobalVariable **Entry = nullptr;
6270 if (!LangOpts.WritableStrings) {
6271 Entry = &ConstantStringMap[C];
6272 if (auto GV = *Entry) {
6273 if (uint64_t(Alignment.getQuantity()) > GV->getAlignment())
6274 GV->setAlignment(Alignment.getAsAlign());
6275 return ConstantAddress(castStringLiteralToDefaultAddressSpace(*this, GV),
6276 GV->getValueType(), Alignment);
6280 // Get the default prefix if a name wasn't specified.
6281 if (!GlobalName)
6282 GlobalName = ".str";
6283 // Create a global variable for this.
6284 auto GV = GenerateStringLiteral(C, llvm::GlobalValue::PrivateLinkage, *this,
6285 GlobalName, Alignment);
6286 if (Entry)
6287 *Entry = GV;
6289 return ConstantAddress(castStringLiteralToDefaultAddressSpace(*this, GV),
6290 GV->getValueType(), Alignment);
6293 ConstantAddress CodeGenModule::GetAddrOfGlobalTemporary(
6294 const MaterializeTemporaryExpr *E, const Expr *Init) {
6295 assert((E->getStorageDuration() == SD_Static ||
6296 E->getStorageDuration() == SD_Thread) && "not a global temporary");
6297 const auto *VD = cast<VarDecl>(E->getExtendingDecl());
6299 // If we're not materializing a subobject of the temporary, keep the
6300 // cv-qualifiers from the type of the MaterializeTemporaryExpr.
6301 QualType MaterializedType = Init->getType();
6302 if (Init == E->getSubExpr())
6303 MaterializedType = E->getType();
6305 CharUnits Align = getContext().getTypeAlignInChars(MaterializedType);
6307 auto InsertResult = MaterializedGlobalTemporaryMap.insert({E, nullptr});
6308 if (!InsertResult.second) {
6309 // We've seen this before: either we already created it or we're in the
6310 // process of doing so.
6311 if (!InsertResult.first->second) {
6312 // We recursively re-entered this function, probably during emission of
6313 // the initializer. Create a placeholder. We'll clean this up in the
6314 // outer call, at the end of this function.
6315 llvm::Type *Type = getTypes().ConvertTypeForMem(MaterializedType);
6316 InsertResult.first->second = new llvm::GlobalVariable(
6317 getModule(), Type, false, llvm::GlobalVariable::InternalLinkage,
6318 nullptr);
6320 return ConstantAddress(InsertResult.first->second,
6321 llvm::cast<llvm::GlobalVariable>(
6322 InsertResult.first->second->stripPointerCasts())
6323 ->getValueType(),
6324 Align);
6327 // FIXME: If an externally-visible declaration extends multiple temporaries,
6328 // we need to give each temporary the same name in every translation unit (and
6329 // we also need to make the temporaries externally-visible).
6330 SmallString<256> Name;
6331 llvm::raw_svector_ostream Out(Name);
6332 getCXXABI().getMangleContext().mangleReferenceTemporary(
6333 VD, E->getManglingNumber(), Out);
6335 APValue *Value = nullptr;
6336 if (E->getStorageDuration() == SD_Static && VD && VD->evaluateValue()) {
6337 // If the initializer of the extending declaration is a constant
6338 // initializer, we should have a cached constant initializer for this
6339 // temporary. Note that this might have a different value from the value
6340 // computed by evaluating the initializer if the surrounding constant
6341 // expression modifies the temporary.
6342 Value = E->getOrCreateValue(false);
6345 // Try evaluating it now, it might have a constant initializer.
6346 Expr::EvalResult EvalResult;
6347 if (!Value && Init->EvaluateAsRValue(EvalResult, getContext()) &&
6348 !EvalResult.hasSideEffects())
6349 Value = &EvalResult.Val;
6351 LangAS AddrSpace =
6352 VD ? GetGlobalVarAddressSpace(VD) : MaterializedType.getAddressSpace();
6354 std::optional<ConstantEmitter> emitter;
6355 llvm::Constant *InitialValue = nullptr;
6356 bool Constant = false;
6357 llvm::Type *Type;
6358 if (Value) {
6359 // The temporary has a constant initializer, use it.
6360 emitter.emplace(*this);
6361 InitialValue = emitter->emitForInitializer(*Value, AddrSpace,
6362 MaterializedType);
6363 Constant =
6364 MaterializedType.isConstantStorage(getContext(), /*ExcludeCtor*/ Value,
6365 /*ExcludeDtor*/ false);
6366 Type = InitialValue->getType();
6367 } else {
6368 // No initializer, the initialization will be provided when we
6369 // initialize the declaration which performed lifetime extension.
6370 Type = getTypes().ConvertTypeForMem(MaterializedType);
6373 // Create a global variable for this lifetime-extended temporary.
6374 llvm::GlobalValue::LinkageTypes Linkage = getLLVMLinkageVarDefinition(VD);
6375 if (Linkage == llvm::GlobalVariable::ExternalLinkage) {
6376 const VarDecl *InitVD;
6377 if (VD->isStaticDataMember() && VD->getAnyInitializer(InitVD) &&
6378 isa<CXXRecordDecl>(InitVD->getLexicalDeclContext())) {
6379 // Temporaries defined inside a class get linkonce_odr linkage because the
6380 // class can be defined in multiple translation units.
6381 Linkage = llvm::GlobalVariable::LinkOnceODRLinkage;
6382 } else {
6383 // There is no need for this temporary to have external linkage if the
6384 // VarDecl has external linkage.
6385 Linkage = llvm::GlobalVariable::InternalLinkage;
6388 auto TargetAS = getContext().getTargetAddressSpace(AddrSpace);
6389 auto *GV = new llvm::GlobalVariable(
6390 getModule(), Type, Constant, Linkage, InitialValue, Name.c_str(),
6391 /*InsertBefore=*/nullptr, llvm::GlobalVariable::NotThreadLocal, TargetAS);
6392 if (emitter) emitter->finalize(GV);
6393 // Don't assign dllimport or dllexport to local linkage globals.
6394 if (!llvm::GlobalValue::isLocalLinkage(Linkage)) {
6395 setGVProperties(GV, VD);
6396 if (GV->getDLLStorageClass() == llvm::GlobalVariable::DLLExportStorageClass)
6397 // The reference temporary should never be dllexport.
6398 GV->setDLLStorageClass(llvm::GlobalVariable::DefaultStorageClass);
6400 GV->setAlignment(Align.getAsAlign());
6401 if (supportsCOMDAT() && GV->isWeakForLinker())
6402 GV->setComdat(TheModule.getOrInsertComdat(GV->getName()));
6403 if (VD->getTLSKind())
6404 setTLSMode(GV, *VD);
6405 llvm::Constant *CV = GV;
6406 if (AddrSpace != LangAS::Default)
6407 CV = getTargetCodeGenInfo().performAddrSpaceCast(
6408 *this, GV, AddrSpace, LangAS::Default,
6409 llvm::PointerType::get(
6410 getLLVMContext(),
6411 getContext().getTargetAddressSpace(LangAS::Default)));
6413 // Update the map with the new temporary. If we created a placeholder above,
6414 // replace it with the new global now.
6415 llvm::Constant *&Entry = MaterializedGlobalTemporaryMap[E];
6416 if (Entry) {
6417 Entry->replaceAllUsesWith(
6418 llvm::ConstantExpr::getBitCast(CV, Entry->getType()));
6419 llvm::cast<llvm::GlobalVariable>(Entry)->eraseFromParent();
6421 Entry = CV;
6423 return ConstantAddress(CV, Type, Align);
6426 /// EmitObjCPropertyImplementations - Emit information for synthesized
6427 /// properties for an implementation.
6428 void CodeGenModule::EmitObjCPropertyImplementations(const
6429 ObjCImplementationDecl *D) {
6430 for (const auto *PID : D->property_impls()) {
6431 // Dynamic is just for type-checking.
6432 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
6433 ObjCPropertyDecl *PD = PID->getPropertyDecl();
6435 // Determine which methods need to be implemented, some may have
6436 // been overridden. Note that ::isPropertyAccessor is not the method
6437 // we want, that just indicates if the decl came from a
6438 // property. What we want to know is if the method is defined in
6439 // this implementation.
6440 auto *Getter = PID->getGetterMethodDecl();
6441 if (!Getter || Getter->isSynthesizedAccessorStub())
6442 CodeGenFunction(*this).GenerateObjCGetter(
6443 const_cast<ObjCImplementationDecl *>(D), PID);
6444 auto *Setter = PID->getSetterMethodDecl();
6445 if (!PD->isReadOnly() && (!Setter || Setter->isSynthesizedAccessorStub()))
6446 CodeGenFunction(*this).GenerateObjCSetter(
6447 const_cast<ObjCImplementationDecl *>(D), PID);
6452 static bool needsDestructMethod(ObjCImplementationDecl *impl) {
6453 const ObjCInterfaceDecl *iface = impl->getClassInterface();
6454 for (const ObjCIvarDecl *ivar = iface->all_declared_ivar_begin();
6455 ivar; ivar = ivar->getNextIvar())
6456 if (ivar->getType().isDestructedType())
6457 return true;
6459 return false;
6462 static bool AllTrivialInitializers(CodeGenModule &CGM,
6463 ObjCImplementationDecl *D) {
6464 CodeGenFunction CGF(CGM);
6465 for (ObjCImplementationDecl::init_iterator B = D->init_begin(),
6466 E = D->init_end(); B != E; ++B) {
6467 CXXCtorInitializer *CtorInitExp = *B;
6468 Expr *Init = CtorInitExp->getInit();
6469 if (!CGF.isTrivialInitializer(Init))
6470 return false;
6472 return true;
6475 /// EmitObjCIvarInitializations - Emit information for ivar initialization
6476 /// for an implementation.
6477 void CodeGenModule::EmitObjCIvarInitializations(ObjCImplementationDecl *D) {
6478 // We might need a .cxx_destruct even if we don't have any ivar initializers.
6479 if (needsDestructMethod(D)) {
6480 IdentifierInfo *II = &getContext().Idents.get(".cxx_destruct");
6481 Selector cxxSelector = getContext().Selectors.getSelector(0, &II);
6482 ObjCMethodDecl *DTORMethod = ObjCMethodDecl::Create(
6483 getContext(), D->getLocation(), D->getLocation(), cxxSelector,
6484 getContext().VoidTy, nullptr, D,
6485 /*isInstance=*/true, /*isVariadic=*/false,
6486 /*isPropertyAccessor=*/true, /*isSynthesizedAccessorStub=*/false,
6487 /*isImplicitlyDeclared=*/true,
6488 /*isDefined=*/false, ObjCImplementationControl::Required);
6489 D->addInstanceMethod(DTORMethod);
6490 CodeGenFunction(*this).GenerateObjCCtorDtorMethod(D, DTORMethod, false);
6491 D->setHasDestructors(true);
6494 // If the implementation doesn't have any ivar initializers, we don't need
6495 // a .cxx_construct.
6496 if (D->getNumIvarInitializers() == 0 ||
6497 AllTrivialInitializers(*this, D))
6498 return;
6500 IdentifierInfo *II = &getContext().Idents.get(".cxx_construct");
6501 Selector cxxSelector = getContext().Selectors.getSelector(0, &II);
6502 // The constructor returns 'self'.
6503 ObjCMethodDecl *CTORMethod = ObjCMethodDecl::Create(
6504 getContext(), D->getLocation(), D->getLocation(), cxxSelector,
6505 getContext().getObjCIdType(), nullptr, D, /*isInstance=*/true,
6506 /*isVariadic=*/false,
6507 /*isPropertyAccessor=*/true, /*isSynthesizedAccessorStub=*/false,
6508 /*isImplicitlyDeclared=*/true,
6509 /*isDefined=*/false, ObjCImplementationControl::Required);
6510 D->addInstanceMethod(CTORMethod);
6511 CodeGenFunction(*this).GenerateObjCCtorDtorMethod(D, CTORMethod, true);
6512 D->setHasNonZeroConstructors(true);
6515 // EmitLinkageSpec - Emit all declarations in a linkage spec.
6516 void CodeGenModule::EmitLinkageSpec(const LinkageSpecDecl *LSD) {
6517 if (LSD->getLanguage() != LinkageSpecLanguageIDs::C &&
6518 LSD->getLanguage() != LinkageSpecLanguageIDs::CXX) {
6519 ErrorUnsupported(LSD, "linkage spec");
6520 return;
6523 EmitDeclContext(LSD);
6526 void CodeGenModule::EmitTopLevelStmt(const TopLevelStmtDecl *D) {
6527 // Device code should not be at top level.
6528 if (LangOpts.CUDA && LangOpts.CUDAIsDevice)
6529 return;
6531 std::unique_ptr<CodeGenFunction> &CurCGF =
6532 GlobalTopLevelStmtBlockInFlight.first;
6534 // We emitted a top-level stmt but after it there is initialization.
6535 // Stop squashing the top-level stmts into a single function.
6536 if (CurCGF && CXXGlobalInits.back() != CurCGF->CurFn) {
6537 CurCGF->FinishFunction(D->getEndLoc());
6538 CurCGF = nullptr;
6541 if (!CurCGF) {
6542 // void __stmts__N(void)
6543 // FIXME: Ask the ABI name mangler to pick a name.
6544 std::string Name = "__stmts__" + llvm::utostr(CXXGlobalInits.size());
6545 FunctionArgList Args;
6546 QualType RetTy = getContext().VoidTy;
6547 const CGFunctionInfo &FnInfo =
6548 getTypes().arrangeBuiltinFunctionDeclaration(RetTy, Args);
6549 llvm::FunctionType *FnTy = getTypes().GetFunctionType(FnInfo);
6550 llvm::Function *Fn = llvm::Function::Create(
6551 FnTy, llvm::GlobalValue::InternalLinkage, Name, &getModule());
6553 CurCGF.reset(new CodeGenFunction(*this));
6554 GlobalTopLevelStmtBlockInFlight.second = D;
6555 CurCGF->StartFunction(GlobalDecl(), RetTy, Fn, FnInfo, Args,
6556 D->getBeginLoc(), D->getBeginLoc());
6557 CXXGlobalInits.push_back(Fn);
6560 CurCGF->EmitStmt(D->getStmt());
6563 void CodeGenModule::EmitDeclContext(const DeclContext *DC) {
6564 for (auto *I : DC->decls()) {
6565 // Unlike other DeclContexts, the contents of an ObjCImplDecl at TU scope
6566 // are themselves considered "top-level", so EmitTopLevelDecl on an
6567 // ObjCImplDecl does not recursively visit them. We need to do that in
6568 // case they're nested inside another construct (LinkageSpecDecl /
6569 // ExportDecl) that does stop them from being considered "top-level".
6570 if (auto *OID = dyn_cast<ObjCImplDecl>(I)) {
6571 for (auto *M : OID->methods())
6572 EmitTopLevelDecl(M);
6575 EmitTopLevelDecl(I);
6579 /// EmitTopLevelDecl - Emit code for a single top level declaration.
6580 void CodeGenModule::EmitTopLevelDecl(Decl *D) {
6581 // Ignore dependent declarations.
6582 if (D->isTemplated())
6583 return;
6585 // Consteval function shouldn't be emitted.
6586 if (auto *FD = dyn_cast<FunctionDecl>(D); FD && FD->isImmediateFunction())
6587 return;
6589 switch (D->getKind()) {
6590 case Decl::CXXConversion:
6591 case Decl::CXXMethod:
6592 case Decl::Function:
6593 EmitGlobal(cast<FunctionDecl>(D));
6594 // Always provide some coverage mapping
6595 // even for the functions that aren't emitted.
6596 AddDeferredUnusedCoverageMapping(D);
6597 break;
6599 case Decl::CXXDeductionGuide:
6600 // Function-like, but does not result in code emission.
6601 break;
6603 case Decl::Var:
6604 case Decl::Decomposition:
6605 case Decl::VarTemplateSpecialization:
6606 EmitGlobal(cast<VarDecl>(D));
6607 if (auto *DD = dyn_cast<DecompositionDecl>(D))
6608 for (auto *B : DD->bindings())
6609 if (auto *HD = B->getHoldingVar())
6610 EmitGlobal(HD);
6611 break;
6613 // Indirect fields from global anonymous structs and unions can be
6614 // ignored; only the actual variable requires IR gen support.
6615 case Decl::IndirectField:
6616 break;
6618 // C++ Decls
6619 case Decl::Namespace:
6620 EmitDeclContext(cast<NamespaceDecl>(D));
6621 break;
6622 case Decl::ClassTemplateSpecialization: {
6623 const auto *Spec = cast<ClassTemplateSpecializationDecl>(D);
6624 if (CGDebugInfo *DI = getModuleDebugInfo())
6625 if (Spec->getSpecializationKind() ==
6626 TSK_ExplicitInstantiationDefinition &&
6627 Spec->hasDefinition())
6628 DI->completeTemplateDefinition(*Spec);
6629 } [[fallthrough]];
6630 case Decl::CXXRecord: {
6631 CXXRecordDecl *CRD = cast<CXXRecordDecl>(D);
6632 if (CGDebugInfo *DI = getModuleDebugInfo()) {
6633 if (CRD->hasDefinition())
6634 DI->EmitAndRetainType(getContext().getRecordType(cast<RecordDecl>(D)));
6635 if (auto *ES = D->getASTContext().getExternalSource())
6636 if (ES->hasExternalDefinitions(D) == ExternalASTSource::EK_Never)
6637 DI->completeUnusedClass(*CRD);
6639 // Emit any static data members, they may be definitions.
6640 for (auto *I : CRD->decls())
6641 if (isa<VarDecl>(I) || isa<CXXRecordDecl>(I))
6642 EmitTopLevelDecl(I);
6643 break;
6645 // No code generation needed.
6646 case Decl::UsingShadow:
6647 case Decl::ClassTemplate:
6648 case Decl::VarTemplate:
6649 case Decl::Concept:
6650 case Decl::VarTemplatePartialSpecialization:
6651 case Decl::FunctionTemplate:
6652 case Decl::TypeAliasTemplate:
6653 case Decl::Block:
6654 case Decl::Empty:
6655 case Decl::Binding:
6656 break;
6657 case Decl::Using: // using X; [C++]
6658 if (CGDebugInfo *DI = getModuleDebugInfo())
6659 DI->EmitUsingDecl(cast<UsingDecl>(*D));
6660 break;
6661 case Decl::UsingEnum: // using enum X; [C++]
6662 if (CGDebugInfo *DI = getModuleDebugInfo())
6663 DI->EmitUsingEnumDecl(cast<UsingEnumDecl>(*D));
6664 break;
6665 case Decl::NamespaceAlias:
6666 if (CGDebugInfo *DI = getModuleDebugInfo())
6667 DI->EmitNamespaceAlias(cast<NamespaceAliasDecl>(*D));
6668 break;
6669 case Decl::UsingDirective: // using namespace X; [C++]
6670 if (CGDebugInfo *DI = getModuleDebugInfo())
6671 DI->EmitUsingDirective(cast<UsingDirectiveDecl>(*D));
6672 break;
6673 case Decl::CXXConstructor:
6674 getCXXABI().EmitCXXConstructors(cast<CXXConstructorDecl>(D));
6675 break;
6676 case Decl::CXXDestructor:
6677 getCXXABI().EmitCXXDestructors(cast<CXXDestructorDecl>(D));
6678 break;
6680 case Decl::StaticAssert:
6681 // Nothing to do.
6682 break;
6684 // Objective-C Decls
6686 // Forward declarations, no (immediate) code generation.
6687 case Decl::ObjCInterface:
6688 case Decl::ObjCCategory:
6689 break;
6691 case Decl::ObjCProtocol: {
6692 auto *Proto = cast<ObjCProtocolDecl>(D);
6693 if (Proto->isThisDeclarationADefinition())
6694 ObjCRuntime->GenerateProtocol(Proto);
6695 break;
6698 case Decl::ObjCCategoryImpl:
6699 // Categories have properties but don't support synthesize so we
6700 // can ignore them here.
6701 ObjCRuntime->GenerateCategory(cast<ObjCCategoryImplDecl>(D));
6702 break;
6704 case Decl::ObjCImplementation: {
6705 auto *OMD = cast<ObjCImplementationDecl>(D);
6706 EmitObjCPropertyImplementations(OMD);
6707 EmitObjCIvarInitializations(OMD);
6708 ObjCRuntime->GenerateClass(OMD);
6709 // Emit global variable debug information.
6710 if (CGDebugInfo *DI = getModuleDebugInfo())
6711 if (getCodeGenOpts().hasReducedDebugInfo())
6712 DI->getOrCreateInterfaceType(getContext().getObjCInterfaceType(
6713 OMD->getClassInterface()), OMD->getLocation());
6714 break;
6716 case Decl::ObjCMethod: {
6717 auto *OMD = cast<ObjCMethodDecl>(D);
6718 // If this is not a prototype, emit the body.
6719 if (OMD->getBody())
6720 CodeGenFunction(*this).GenerateObjCMethod(OMD);
6721 break;
6723 case Decl::ObjCCompatibleAlias:
6724 ObjCRuntime->RegisterAlias(cast<ObjCCompatibleAliasDecl>(D));
6725 break;
6727 case Decl::PragmaComment: {
6728 const auto *PCD = cast<PragmaCommentDecl>(D);
6729 switch (PCD->getCommentKind()) {
6730 case PCK_Unknown:
6731 llvm_unreachable("unexpected pragma comment kind");
6732 case PCK_Linker:
6733 AppendLinkerOptions(PCD->getArg());
6734 break;
6735 case PCK_Lib:
6736 AddDependentLib(PCD->getArg());
6737 break;
6738 case PCK_Compiler:
6739 case PCK_ExeStr:
6740 case PCK_User:
6741 break; // We ignore all of these.
6743 break;
6746 case Decl::PragmaDetectMismatch: {
6747 const auto *PDMD = cast<PragmaDetectMismatchDecl>(D);
6748 AddDetectMismatch(PDMD->getName(), PDMD->getValue());
6749 break;
6752 case Decl::LinkageSpec:
6753 EmitLinkageSpec(cast<LinkageSpecDecl>(D));
6754 break;
6756 case Decl::FileScopeAsm: {
6757 // File-scope asm is ignored during device-side CUDA compilation.
6758 if (LangOpts.CUDA && LangOpts.CUDAIsDevice)
6759 break;
6760 // File-scope asm is ignored during device-side OpenMP compilation.
6761 if (LangOpts.OpenMPIsTargetDevice)
6762 break;
6763 // File-scope asm is ignored during device-side SYCL compilation.
6764 if (LangOpts.SYCLIsDevice)
6765 break;
6766 auto *AD = cast<FileScopeAsmDecl>(D);
6767 getModule().appendModuleInlineAsm(AD->getAsmString()->getString());
6768 break;
6771 case Decl::TopLevelStmt:
6772 EmitTopLevelStmt(cast<TopLevelStmtDecl>(D));
6773 break;
6775 case Decl::Import: {
6776 auto *Import = cast<ImportDecl>(D);
6778 // If we've already imported this module, we're done.
6779 if (!ImportedModules.insert(Import->getImportedModule()))
6780 break;
6782 // Emit debug information for direct imports.
6783 if (!Import->getImportedOwningModule()) {
6784 if (CGDebugInfo *DI = getModuleDebugInfo())
6785 DI->EmitImportDecl(*Import);
6788 // For C++ standard modules we are done - we will call the module
6789 // initializer for imported modules, and that will likewise call those for
6790 // any imports it has.
6791 if (CXX20ModuleInits && Import->getImportedOwningModule() &&
6792 !Import->getImportedOwningModule()->isModuleMapModule())
6793 break;
6795 // For clang C++ module map modules the initializers for sub-modules are
6796 // emitted here.
6798 // Find all of the submodules and emit the module initializers.
6799 llvm::SmallPtrSet<clang::Module *, 16> Visited;
6800 SmallVector<clang::Module *, 16> Stack;
6801 Visited.insert(Import->getImportedModule());
6802 Stack.push_back(Import->getImportedModule());
6804 while (!Stack.empty()) {
6805 clang::Module *Mod = Stack.pop_back_val();
6806 if (!EmittedModuleInitializers.insert(Mod).second)
6807 continue;
6809 for (auto *D : Context.getModuleInitializers(Mod))
6810 EmitTopLevelDecl(D);
6812 // Visit the submodules of this module.
6813 for (auto *Submodule : Mod->submodules()) {
6814 // Skip explicit children; they need to be explicitly imported to emit
6815 // the initializers.
6816 if (Submodule->IsExplicit)
6817 continue;
6819 if (Visited.insert(Submodule).second)
6820 Stack.push_back(Submodule);
6823 break;
6826 case Decl::Export:
6827 EmitDeclContext(cast<ExportDecl>(D));
6828 break;
6830 case Decl::OMPThreadPrivate:
6831 EmitOMPThreadPrivateDecl(cast<OMPThreadPrivateDecl>(D));
6832 break;
6834 case Decl::OMPAllocate:
6835 EmitOMPAllocateDecl(cast<OMPAllocateDecl>(D));
6836 break;
6838 case Decl::OMPDeclareReduction:
6839 EmitOMPDeclareReduction(cast<OMPDeclareReductionDecl>(D));
6840 break;
6842 case Decl::OMPDeclareMapper:
6843 EmitOMPDeclareMapper(cast<OMPDeclareMapperDecl>(D));
6844 break;
6846 case Decl::OMPRequires:
6847 EmitOMPRequiresDecl(cast<OMPRequiresDecl>(D));
6848 break;
6850 case Decl::Typedef:
6851 case Decl::TypeAlias: // using foo = bar; [C++11]
6852 if (CGDebugInfo *DI = getModuleDebugInfo())
6853 DI->EmitAndRetainType(
6854 getContext().getTypedefType(cast<TypedefNameDecl>(D)));
6855 break;
6857 case Decl::Record:
6858 if (CGDebugInfo *DI = getModuleDebugInfo())
6859 if (cast<RecordDecl>(D)->getDefinition())
6860 DI->EmitAndRetainType(getContext().getRecordType(cast<RecordDecl>(D)));
6861 break;
6863 case Decl::Enum:
6864 if (CGDebugInfo *DI = getModuleDebugInfo())
6865 if (cast<EnumDecl>(D)->getDefinition())
6866 DI->EmitAndRetainType(getContext().getEnumType(cast<EnumDecl>(D)));
6867 break;
6869 case Decl::HLSLBuffer:
6870 getHLSLRuntime().addBuffer(cast<HLSLBufferDecl>(D));
6871 break;
6873 default:
6874 // Make sure we handled everything we should, every other kind is a
6875 // non-top-level decl. FIXME: Would be nice to have an isTopLevelDeclKind
6876 // function. Need to recode Decl::Kind to do that easily.
6877 assert(isa<TypeDecl>(D) && "Unsupported decl kind");
6878 break;
6882 void CodeGenModule::AddDeferredUnusedCoverageMapping(Decl *D) {
6883 // Do we need to generate coverage mapping?
6884 if (!CodeGenOpts.CoverageMapping)
6885 return;
6886 switch (D->getKind()) {
6887 case Decl::CXXConversion:
6888 case Decl::CXXMethod:
6889 case Decl::Function:
6890 case Decl::ObjCMethod:
6891 case Decl::CXXConstructor:
6892 case Decl::CXXDestructor: {
6893 if (!cast<FunctionDecl>(D)->doesThisDeclarationHaveABody())
6894 break;
6895 SourceManager &SM = getContext().getSourceManager();
6896 if (LimitedCoverage && SM.getMainFileID() != SM.getFileID(D->getBeginLoc()))
6897 break;
6898 auto I = DeferredEmptyCoverageMappingDecls.find(D);
6899 if (I == DeferredEmptyCoverageMappingDecls.end())
6900 DeferredEmptyCoverageMappingDecls[D] = true;
6901 break;
6903 default:
6904 break;
6908 void CodeGenModule::ClearUnusedCoverageMapping(const Decl *D) {
6909 // Do we need to generate coverage mapping?
6910 if (!CodeGenOpts.CoverageMapping)
6911 return;
6912 if (const auto *Fn = dyn_cast<FunctionDecl>(D)) {
6913 if (Fn->isTemplateInstantiation())
6914 ClearUnusedCoverageMapping(Fn->getTemplateInstantiationPattern());
6916 auto I = DeferredEmptyCoverageMappingDecls.find(D);
6917 if (I == DeferredEmptyCoverageMappingDecls.end())
6918 DeferredEmptyCoverageMappingDecls[D] = false;
6919 else
6920 I->second = false;
6923 void CodeGenModule::EmitDeferredUnusedCoverageMappings() {
6924 // We call takeVector() here to avoid use-after-free.
6925 // FIXME: DeferredEmptyCoverageMappingDecls is getting mutated because
6926 // we deserialize function bodies to emit coverage info for them, and that
6927 // deserializes more declarations. How should we handle that case?
6928 for (const auto &Entry : DeferredEmptyCoverageMappingDecls.takeVector()) {
6929 if (!Entry.second)
6930 continue;
6931 const Decl *D = Entry.first;
6932 switch (D->getKind()) {
6933 case Decl::CXXConversion:
6934 case Decl::CXXMethod:
6935 case Decl::Function:
6936 case Decl::ObjCMethod: {
6937 CodeGenPGO PGO(*this);
6938 GlobalDecl GD(cast<FunctionDecl>(D));
6939 PGO.emitEmptyCounterMapping(D, getMangledName(GD),
6940 getFunctionLinkage(GD));
6941 break;
6943 case Decl::CXXConstructor: {
6944 CodeGenPGO PGO(*this);
6945 GlobalDecl GD(cast<CXXConstructorDecl>(D), Ctor_Base);
6946 PGO.emitEmptyCounterMapping(D, getMangledName(GD),
6947 getFunctionLinkage(GD));
6948 break;
6950 case Decl::CXXDestructor: {
6951 CodeGenPGO PGO(*this);
6952 GlobalDecl GD(cast<CXXDestructorDecl>(D), Dtor_Base);
6953 PGO.emitEmptyCounterMapping(D, getMangledName(GD),
6954 getFunctionLinkage(GD));
6955 break;
6957 default:
6958 break;
6963 void CodeGenModule::EmitMainVoidAlias() {
6964 // In order to transition away from "__original_main" gracefully, emit an
6965 // alias for "main" in the no-argument case so that libc can detect when
6966 // new-style no-argument main is in used.
6967 if (llvm::Function *F = getModule().getFunction("main")) {
6968 if (!F->isDeclaration() && F->arg_size() == 0 && !F->isVarArg() &&
6969 F->getReturnType()->isIntegerTy(Context.getTargetInfo().getIntWidth())) {
6970 auto *GA = llvm::GlobalAlias::create("__main_void", F);
6971 GA->setVisibility(llvm::GlobalValue::HiddenVisibility);
6976 /// Turns the given pointer into a constant.
6977 static llvm::Constant *GetPointerConstant(llvm::LLVMContext &Context,
6978 const void *Ptr) {
6979 uintptr_t PtrInt = reinterpret_cast<uintptr_t>(Ptr);
6980 llvm::Type *i64 = llvm::Type::getInt64Ty(Context);
6981 return llvm::ConstantInt::get(i64, PtrInt);
6984 static void EmitGlobalDeclMetadata(CodeGenModule &CGM,
6985 llvm::NamedMDNode *&GlobalMetadata,
6986 GlobalDecl D,
6987 llvm::GlobalValue *Addr) {
6988 if (!GlobalMetadata)
6989 GlobalMetadata =
6990 CGM.getModule().getOrInsertNamedMetadata("clang.global.decl.ptrs");
6992 // TODO: should we report variant information for ctors/dtors?
6993 llvm::Metadata *Ops[] = {llvm::ConstantAsMetadata::get(Addr),
6994 llvm::ConstantAsMetadata::get(GetPointerConstant(
6995 CGM.getLLVMContext(), D.getDecl()))};
6996 GlobalMetadata->addOperand(llvm::MDNode::get(CGM.getLLVMContext(), Ops));
6999 bool CodeGenModule::CheckAndReplaceExternCIFuncs(llvm::GlobalValue *Elem,
7000 llvm::GlobalValue *CppFunc) {
7001 // Store the list of ifuncs we need to replace uses in.
7002 llvm::SmallVector<llvm::GlobalIFunc *> IFuncs;
7003 // List of ConstantExprs that we should be able to delete when we're done
7004 // here.
7005 llvm::SmallVector<llvm::ConstantExpr *> CEs;
7007 // It isn't valid to replace the extern-C ifuncs if all we find is itself!
7008 if (Elem == CppFunc)
7009 return false;
7011 // First make sure that all users of this are ifuncs (or ifuncs via a
7012 // bitcast), and collect the list of ifuncs and CEs so we can work on them
7013 // later.
7014 for (llvm::User *User : Elem->users()) {
7015 // Users can either be a bitcast ConstExpr that is used by the ifuncs, OR an
7016 // ifunc directly. In any other case, just give up, as we don't know what we
7017 // could break by changing those.
7018 if (auto *ConstExpr = dyn_cast<llvm::ConstantExpr>(User)) {
7019 if (ConstExpr->getOpcode() != llvm::Instruction::BitCast)
7020 return false;
7022 for (llvm::User *CEUser : ConstExpr->users()) {
7023 if (auto *IFunc = dyn_cast<llvm::GlobalIFunc>(CEUser)) {
7024 IFuncs.push_back(IFunc);
7025 } else {
7026 return false;
7029 CEs.push_back(ConstExpr);
7030 } else if (auto *IFunc = dyn_cast<llvm::GlobalIFunc>(User)) {
7031 IFuncs.push_back(IFunc);
7032 } else {
7033 // This user is one we don't know how to handle, so fail redirection. This
7034 // will result in an ifunc retaining a resolver name that will ultimately
7035 // fail to be resolved to a defined function.
7036 return false;
7040 // Now we know this is a valid case where we can do this alias replacement, we
7041 // need to remove all of the references to Elem (and the bitcasts!) so we can
7042 // delete it.
7043 for (llvm::GlobalIFunc *IFunc : IFuncs)
7044 IFunc->setResolver(nullptr);
7045 for (llvm::ConstantExpr *ConstExpr : CEs)
7046 ConstExpr->destroyConstant();
7048 // We should now be out of uses for the 'old' version of this function, so we
7049 // can erase it as well.
7050 Elem->eraseFromParent();
7052 for (llvm::GlobalIFunc *IFunc : IFuncs) {
7053 // The type of the resolver is always just a function-type that returns the
7054 // type of the IFunc, so create that here. If the type of the actual
7055 // resolver doesn't match, it just gets bitcast to the right thing.
7056 auto *ResolverTy =
7057 llvm::FunctionType::get(IFunc->getType(), /*isVarArg*/ false);
7058 llvm::Constant *Resolver = GetOrCreateLLVMFunction(
7059 CppFunc->getName(), ResolverTy, {}, /*ForVTable*/ false);
7060 IFunc->setResolver(Resolver);
7062 return true;
7065 /// For each function which is declared within an extern "C" region and marked
7066 /// as 'used', but has internal linkage, create an alias from the unmangled
7067 /// name to the mangled name if possible. People expect to be able to refer
7068 /// to such functions with an unmangled name from inline assembly within the
7069 /// same translation unit.
7070 void CodeGenModule::EmitStaticExternCAliases() {
7071 if (!getTargetCodeGenInfo().shouldEmitStaticExternCAliases())
7072 return;
7073 for (auto &I : StaticExternCValues) {
7074 IdentifierInfo *Name = I.first;
7075 llvm::GlobalValue *Val = I.second;
7077 // If Val is null, that implies there were multiple declarations that each
7078 // had a claim to the unmangled name. In this case, generation of the alias
7079 // is suppressed. See CodeGenModule::MaybeHandleStaticInExternC.
7080 if (!Val)
7081 break;
7083 llvm::GlobalValue *ExistingElem =
7084 getModule().getNamedValue(Name->getName());
7086 // If there is either not something already by this name, or we were able to
7087 // replace all uses from IFuncs, create the alias.
7088 if (!ExistingElem || CheckAndReplaceExternCIFuncs(ExistingElem, Val))
7089 addCompilerUsedGlobal(llvm::GlobalAlias::create(Name->getName(), Val));
7093 bool CodeGenModule::lookupRepresentativeDecl(StringRef MangledName,
7094 GlobalDecl &Result) const {
7095 auto Res = Manglings.find(MangledName);
7096 if (Res == Manglings.end())
7097 return false;
7098 Result = Res->getValue();
7099 return true;
7102 /// Emits metadata nodes associating all the global values in the
7103 /// current module with the Decls they came from. This is useful for
7104 /// projects using IR gen as a subroutine.
7106 /// Since there's currently no way to associate an MDNode directly
7107 /// with an llvm::GlobalValue, we create a global named metadata
7108 /// with the name 'clang.global.decl.ptrs'.
7109 void CodeGenModule::EmitDeclMetadata() {
7110 llvm::NamedMDNode *GlobalMetadata = nullptr;
7112 for (auto &I : MangledDeclNames) {
7113 llvm::GlobalValue *Addr = getModule().getNamedValue(I.second);
7114 // Some mangled names don't necessarily have an associated GlobalValue
7115 // in this module, e.g. if we mangled it for DebugInfo.
7116 if (Addr)
7117 EmitGlobalDeclMetadata(*this, GlobalMetadata, I.first, Addr);
7121 /// Emits metadata nodes for all the local variables in the current
7122 /// function.
7123 void CodeGenFunction::EmitDeclMetadata() {
7124 if (LocalDeclMap.empty()) return;
7126 llvm::LLVMContext &Context = getLLVMContext();
7128 // Find the unique metadata ID for this name.
7129 unsigned DeclPtrKind = Context.getMDKindID("clang.decl.ptr");
7131 llvm::NamedMDNode *GlobalMetadata = nullptr;
7133 for (auto &I : LocalDeclMap) {
7134 const Decl *D = I.first;
7135 llvm::Value *Addr = I.second.getPointer();
7136 if (auto *Alloca = dyn_cast<llvm::AllocaInst>(Addr)) {
7137 llvm::Value *DAddr = GetPointerConstant(getLLVMContext(), D);
7138 Alloca->setMetadata(
7139 DeclPtrKind, llvm::MDNode::get(
7140 Context, llvm::ValueAsMetadata::getConstant(DAddr)));
7141 } else if (auto *GV = dyn_cast<llvm::GlobalValue>(Addr)) {
7142 GlobalDecl GD = GlobalDecl(cast<VarDecl>(D));
7143 EmitGlobalDeclMetadata(CGM, GlobalMetadata, GD, GV);
7148 void CodeGenModule::EmitVersionIdentMetadata() {
7149 llvm::NamedMDNode *IdentMetadata =
7150 TheModule.getOrInsertNamedMetadata("llvm.ident");
7151 std::string Version = getClangFullVersion();
7152 llvm::LLVMContext &Ctx = TheModule.getContext();
7154 llvm::Metadata *IdentNode[] = {llvm::MDString::get(Ctx, Version)};
7155 IdentMetadata->addOperand(llvm::MDNode::get(Ctx, IdentNode));
7158 void CodeGenModule::EmitCommandLineMetadata() {
7159 llvm::NamedMDNode *CommandLineMetadata =
7160 TheModule.getOrInsertNamedMetadata("llvm.commandline");
7161 std::string CommandLine = getCodeGenOpts().RecordCommandLine;
7162 llvm::LLVMContext &Ctx = TheModule.getContext();
7164 llvm::Metadata *CommandLineNode[] = {llvm::MDString::get(Ctx, CommandLine)};
7165 CommandLineMetadata->addOperand(llvm::MDNode::get(Ctx, CommandLineNode));
7168 void CodeGenModule::EmitCoverageFile() {
7169 llvm::NamedMDNode *CUNode = TheModule.getNamedMetadata("llvm.dbg.cu");
7170 if (!CUNode)
7171 return;
7173 llvm::NamedMDNode *GCov = TheModule.getOrInsertNamedMetadata("llvm.gcov");
7174 llvm::LLVMContext &Ctx = TheModule.getContext();
7175 auto *CoverageDataFile =
7176 llvm::MDString::get(Ctx, getCodeGenOpts().CoverageDataFile);
7177 auto *CoverageNotesFile =
7178 llvm::MDString::get(Ctx, getCodeGenOpts().CoverageNotesFile);
7179 for (int i = 0, e = CUNode->getNumOperands(); i != e; ++i) {
7180 llvm::MDNode *CU = CUNode->getOperand(i);
7181 llvm::Metadata *Elts[] = {CoverageNotesFile, CoverageDataFile, CU};
7182 GCov->addOperand(llvm::MDNode::get(Ctx, Elts));
7186 llvm::Constant *CodeGenModule::GetAddrOfRTTIDescriptor(QualType Ty,
7187 bool ForEH) {
7188 // Return a bogus pointer if RTTI is disabled, unless it's for EH.
7189 // FIXME: should we even be calling this method if RTTI is disabled
7190 // and it's not for EH?
7191 if (!shouldEmitRTTI(ForEH))
7192 return llvm::Constant::getNullValue(GlobalsInt8PtrTy);
7194 if (ForEH && Ty->isObjCObjectPointerType() &&
7195 LangOpts.ObjCRuntime.isGNUFamily())
7196 return ObjCRuntime->GetEHType(Ty);
7198 return getCXXABI().getAddrOfRTTIDescriptor(Ty);
7201 void CodeGenModule::EmitOMPThreadPrivateDecl(const OMPThreadPrivateDecl *D) {
7202 // Do not emit threadprivates in simd-only mode.
7203 if (LangOpts.OpenMP && LangOpts.OpenMPSimd)
7204 return;
7205 for (auto RefExpr : D->varlists()) {
7206 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(RefExpr)->getDecl());
7207 bool PerformInit =
7208 VD->getAnyInitializer() &&
7209 !VD->getAnyInitializer()->isConstantInitializer(getContext(),
7210 /*ForRef=*/false);
7212 Address Addr(GetAddrOfGlobalVar(VD),
7213 getTypes().ConvertTypeForMem(VD->getType()),
7214 getContext().getDeclAlign(VD));
7215 if (auto InitFunction = getOpenMPRuntime().emitThreadPrivateVarDefinition(
7216 VD, Addr, RefExpr->getBeginLoc(), PerformInit))
7217 CXXGlobalInits.push_back(InitFunction);
7221 llvm::Metadata *
7222 CodeGenModule::CreateMetadataIdentifierImpl(QualType T, MetadataTypeMap &Map,
7223 StringRef Suffix) {
7224 if (auto *FnType = T->getAs<FunctionProtoType>())
7225 T = getContext().getFunctionType(
7226 FnType->getReturnType(), FnType->getParamTypes(),
7227 FnType->getExtProtoInfo().withExceptionSpec(EST_None));
7229 llvm::Metadata *&InternalId = Map[T.getCanonicalType()];
7230 if (InternalId)
7231 return InternalId;
7233 if (isExternallyVisible(T->getLinkage())) {
7234 std::string OutName;
7235 llvm::raw_string_ostream Out(OutName);
7236 getCXXABI().getMangleContext().mangleCanonicalTypeName(
7237 T, Out, getCodeGenOpts().SanitizeCfiICallNormalizeIntegers);
7239 if (getCodeGenOpts().SanitizeCfiICallNormalizeIntegers)
7240 Out << ".normalized";
7242 Out << Suffix;
7244 InternalId = llvm::MDString::get(getLLVMContext(), Out.str());
7245 } else {
7246 InternalId = llvm::MDNode::getDistinct(getLLVMContext(),
7247 llvm::ArrayRef<llvm::Metadata *>());
7250 return InternalId;
7253 llvm::Metadata *CodeGenModule::CreateMetadataIdentifierForType(QualType T) {
7254 return CreateMetadataIdentifierImpl(T, MetadataIdMap, "");
7257 llvm::Metadata *
7258 CodeGenModule::CreateMetadataIdentifierForVirtualMemPtrType(QualType T) {
7259 return CreateMetadataIdentifierImpl(T, VirtualMetadataIdMap, ".virtual");
7262 // Generalize pointer types to a void pointer with the qualifiers of the
7263 // originally pointed-to type, e.g. 'const char *' and 'char * const *'
7264 // generalize to 'const void *' while 'char *' and 'const char **' generalize to
7265 // 'void *'.
7266 static QualType GeneralizeType(ASTContext &Ctx, QualType Ty) {
7267 if (!Ty->isPointerType())
7268 return Ty;
7270 return Ctx.getPointerType(
7271 QualType(Ctx.VoidTy).withCVRQualifiers(
7272 Ty->getPointeeType().getCVRQualifiers()));
7275 // Apply type generalization to a FunctionType's return and argument types
7276 static QualType GeneralizeFunctionType(ASTContext &Ctx, QualType Ty) {
7277 if (auto *FnType = Ty->getAs<FunctionProtoType>()) {
7278 SmallVector<QualType, 8> GeneralizedParams;
7279 for (auto &Param : FnType->param_types())
7280 GeneralizedParams.push_back(GeneralizeType(Ctx, Param));
7282 return Ctx.getFunctionType(
7283 GeneralizeType(Ctx, FnType->getReturnType()),
7284 GeneralizedParams, FnType->getExtProtoInfo());
7287 if (auto *FnType = Ty->getAs<FunctionNoProtoType>())
7288 return Ctx.getFunctionNoProtoType(
7289 GeneralizeType(Ctx, FnType->getReturnType()));
7291 llvm_unreachable("Encountered unknown FunctionType");
7294 llvm::Metadata *CodeGenModule::CreateMetadataIdentifierGeneralized(QualType T) {
7295 return CreateMetadataIdentifierImpl(GeneralizeFunctionType(getContext(), T),
7296 GeneralizedMetadataIdMap, ".generalized");
7299 /// Returns whether this module needs the "all-vtables" type identifier.
7300 bool CodeGenModule::NeedAllVtablesTypeId() const {
7301 // Returns true if at least one of vtable-based CFI checkers is enabled and
7302 // is not in the trapping mode.
7303 return ((LangOpts.Sanitize.has(SanitizerKind::CFIVCall) &&
7304 !CodeGenOpts.SanitizeTrap.has(SanitizerKind::CFIVCall)) ||
7305 (LangOpts.Sanitize.has(SanitizerKind::CFINVCall) &&
7306 !CodeGenOpts.SanitizeTrap.has(SanitizerKind::CFINVCall)) ||
7307 (LangOpts.Sanitize.has(SanitizerKind::CFIDerivedCast) &&
7308 !CodeGenOpts.SanitizeTrap.has(SanitizerKind::CFIDerivedCast)) ||
7309 (LangOpts.Sanitize.has(SanitizerKind::CFIUnrelatedCast) &&
7310 !CodeGenOpts.SanitizeTrap.has(SanitizerKind::CFIUnrelatedCast)));
7313 void CodeGenModule::AddVTableTypeMetadata(llvm::GlobalVariable *VTable,
7314 CharUnits Offset,
7315 const CXXRecordDecl *RD) {
7316 llvm::Metadata *MD =
7317 CreateMetadataIdentifierForType(QualType(RD->getTypeForDecl(), 0));
7318 VTable->addTypeMetadata(Offset.getQuantity(), MD);
7320 if (CodeGenOpts.SanitizeCfiCrossDso)
7321 if (auto CrossDsoTypeId = CreateCrossDsoCfiTypeId(MD))
7322 VTable->addTypeMetadata(Offset.getQuantity(),
7323 llvm::ConstantAsMetadata::get(CrossDsoTypeId));
7325 if (NeedAllVtablesTypeId()) {
7326 llvm::Metadata *MD = llvm::MDString::get(getLLVMContext(), "all-vtables");
7327 VTable->addTypeMetadata(Offset.getQuantity(), MD);
7331 llvm::SanitizerStatReport &CodeGenModule::getSanStats() {
7332 if (!SanStats)
7333 SanStats = std::make_unique<llvm::SanitizerStatReport>(&getModule());
7335 return *SanStats;
7338 llvm::Value *
7339 CodeGenModule::createOpenCLIntToSamplerConversion(const Expr *E,
7340 CodeGenFunction &CGF) {
7341 llvm::Constant *C = ConstantEmitter(CGF).emitAbstract(E, E->getType());
7342 auto *SamplerT = getOpenCLRuntime().getSamplerType(E->getType().getTypePtr());
7343 auto *FTy = llvm::FunctionType::get(SamplerT, {C->getType()}, false);
7344 auto *Call = CGF.EmitRuntimeCall(
7345 CreateRuntimeFunction(FTy, "__translate_sampler_initializer"), {C});
7346 return Call;
7349 CharUnits CodeGenModule::getNaturalPointeeTypeAlignment(
7350 QualType T, LValueBaseInfo *BaseInfo, TBAAAccessInfo *TBAAInfo) {
7351 return getNaturalTypeAlignment(T->getPointeeType(), BaseInfo, TBAAInfo,
7352 /* forPointeeType= */ true);
7355 CharUnits CodeGenModule::getNaturalTypeAlignment(QualType T,
7356 LValueBaseInfo *BaseInfo,
7357 TBAAAccessInfo *TBAAInfo,
7358 bool forPointeeType) {
7359 if (TBAAInfo)
7360 *TBAAInfo = getTBAAAccessInfo(T);
7362 // FIXME: This duplicates logic in ASTContext::getTypeAlignIfKnown. But
7363 // that doesn't return the information we need to compute BaseInfo.
7365 // Honor alignment typedef attributes even on incomplete types.
7366 // We also honor them straight for C++ class types, even as pointees;
7367 // there's an expressivity gap here.
7368 if (auto TT = T->getAs<TypedefType>()) {
7369 if (auto Align = TT->getDecl()->getMaxAlignment()) {
7370 if (BaseInfo)
7371 *BaseInfo = LValueBaseInfo(AlignmentSource::AttributedType);
7372 return getContext().toCharUnitsFromBits(Align);
7376 bool AlignForArray = T->isArrayType();
7378 // Analyze the base element type, so we don't get confused by incomplete
7379 // array types.
7380 T = getContext().getBaseElementType(T);
7382 if (T->isIncompleteType()) {
7383 // We could try to replicate the logic from
7384 // ASTContext::getTypeAlignIfKnown, but nothing uses the alignment if the
7385 // type is incomplete, so it's impossible to test. We could try to reuse
7386 // getTypeAlignIfKnown, but that doesn't return the information we need
7387 // to set BaseInfo. So just ignore the possibility that the alignment is
7388 // greater than one.
7389 if (BaseInfo)
7390 *BaseInfo = LValueBaseInfo(AlignmentSource::Type);
7391 return CharUnits::One();
7394 if (BaseInfo)
7395 *BaseInfo = LValueBaseInfo(AlignmentSource::Type);
7397 CharUnits Alignment;
7398 const CXXRecordDecl *RD;
7399 if (T.getQualifiers().hasUnaligned()) {
7400 Alignment = CharUnits::One();
7401 } else if (forPointeeType && !AlignForArray &&
7402 (RD = T->getAsCXXRecordDecl())) {
7403 // For C++ class pointees, we don't know whether we're pointing at a
7404 // base or a complete object, so we generally need to use the
7405 // non-virtual alignment.
7406 Alignment = getClassPointerAlignment(RD);
7407 } else {
7408 Alignment = getContext().getTypeAlignInChars(T);
7411 // Cap to the global maximum type alignment unless the alignment
7412 // was somehow explicit on the type.
7413 if (unsigned MaxAlign = getLangOpts().MaxTypeAlign) {
7414 if (Alignment.getQuantity() > MaxAlign &&
7415 !getContext().isAlignmentRequired(T))
7416 Alignment = CharUnits::fromQuantity(MaxAlign);
7418 return Alignment;
7421 bool CodeGenModule::stopAutoInit() {
7422 unsigned StopAfter = getContext().getLangOpts().TrivialAutoVarInitStopAfter;
7423 if (StopAfter) {
7424 // This number is positive only when -ftrivial-auto-var-init-stop-after=* is
7425 // used
7426 if (NumAutoVarInit >= StopAfter) {
7427 return true;
7429 if (!NumAutoVarInit) {
7430 unsigned DiagID = getDiags().getCustomDiagID(
7431 DiagnosticsEngine::Warning,
7432 "-ftrivial-auto-var-init-stop-after=%0 has been enabled to limit the "
7433 "number of times ftrivial-auto-var-init=%1 gets applied.");
7434 getDiags().Report(DiagID)
7435 << StopAfter
7436 << (getContext().getLangOpts().getTrivialAutoVarInit() ==
7437 LangOptions::TrivialAutoVarInitKind::Zero
7438 ? "zero"
7439 : "pattern");
7441 ++NumAutoVarInit;
7443 return false;
7446 void CodeGenModule::printPostfixForExternalizedDecl(llvm::raw_ostream &OS,
7447 const Decl *D) const {
7448 // ptxas does not allow '.' in symbol names. On the other hand, HIP prefers
7449 // postfix beginning with '.' since the symbol name can be demangled.
7450 if (LangOpts.HIP)
7451 OS << (isa<VarDecl>(D) ? ".static." : ".intern.");
7452 else
7453 OS << (isa<VarDecl>(D) ? "__static__" : "__intern__");
7455 // If the CUID is not specified we try to generate a unique postfix.
7456 if (getLangOpts().CUID.empty()) {
7457 SourceManager &SM = getContext().getSourceManager();
7458 PresumedLoc PLoc = SM.getPresumedLoc(D->getLocation());
7459 assert(PLoc.isValid() && "Source location is expected to be valid.");
7461 // Get the hash of the user defined macros.
7462 llvm::MD5 Hash;
7463 llvm::MD5::MD5Result Result;
7464 for (const auto &Arg : PreprocessorOpts.Macros)
7465 Hash.update(Arg.first);
7466 Hash.final(Result);
7468 // Get the UniqueID for the file containing the decl.
7469 llvm::sys::fs::UniqueID ID;
7470 if (llvm::sys::fs::getUniqueID(PLoc.getFilename(), ID)) {
7471 PLoc = SM.getPresumedLoc(D->getLocation(), /*UseLineDirectives=*/false);
7472 assert(PLoc.isValid() && "Source location is expected to be valid.");
7473 if (auto EC = llvm::sys::fs::getUniqueID(PLoc.getFilename(), ID))
7474 SM.getDiagnostics().Report(diag::err_cannot_open_file)
7475 << PLoc.getFilename() << EC.message();
7477 OS << llvm::format("%x", ID.getFile()) << llvm::format("%x", ID.getDevice())
7478 << "_" << llvm::utohexstr(Result.low(), /*LowerCase=*/true, /*Width=*/8);
7479 } else {
7480 OS << getContext().getCUIDHash();
7484 void CodeGenModule::moveLazyEmissionStates(CodeGenModule *NewBuilder) {
7485 assert(DeferredDeclsToEmit.empty() &&
7486 "Should have emitted all decls deferred to emit.");
7487 assert(NewBuilder->DeferredDecls.empty() &&
7488 "Newly created module should not have deferred decls");
7489 NewBuilder->DeferredDecls = std::move(DeferredDecls);
7490 assert(EmittedDeferredDecls.empty() &&
7491 "Still have (unmerged) EmittedDeferredDecls deferred decls");
7493 assert(NewBuilder->DeferredVTables.empty() &&
7494 "Newly created module should not have deferred vtables");
7495 NewBuilder->DeferredVTables = std::move(DeferredVTables);
7497 assert(NewBuilder->MangledDeclNames.empty() &&
7498 "Newly created module should not have mangled decl names");
7499 assert(NewBuilder->Manglings.empty() &&
7500 "Newly created module should not have manglings");
7501 NewBuilder->Manglings = std::move(Manglings);
7503 NewBuilder->WeakRefReferences = std::move(WeakRefReferences);
7505 NewBuilder->TBAA = std::move(TBAA);
7507 NewBuilder->ABI->MangleCtx = std::move(ABI->MangleCtx);