clang 22.0.0git
CodeGenModule.cpp
Go to the documentation of this file.
1//===--- CodeGenModule.cpp - Emit LLVM Code from ASTs for a Module --------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This coordinates the per-module state used while generating code.
10//
11//===----------------------------------------------------------------------===//
12
13#include "CodeGenModule.h"
14#include "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"
31#include "clang/AST/ASTLambda.h"
32#include "clang/AST/CharUnits.h"
33#include "clang/AST/Decl.h"
34#include "clang/AST/DeclCXX.h"
35#include "clang/AST/DeclObjC.h"
37#include "clang/AST/Mangle.h"
43#include "clang/Basic/Module.h"
46#include "clang/Basic/Version.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/BinaryFormat/ELF.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/RISCVISAInfo.h"
72#include "llvm/TargetParser/Triple.h"
73#include "llvm/TargetParser/X86TargetParser.h"
74#include "llvm/Transforms/Utils/BuildLibCalls.h"
75#include <optional>
76#include <set>
77
78using namespace clang;
79using namespace CodeGen;
80
81static llvm::cl::opt<bool> LimitedCoverage(
82 "limited-coverage-experimental", llvm::cl::Hidden,
83 llvm::cl::desc("Emit limited coverage mapping information (experimental)"));
84
85static const char AnnotationSection[] = "llvm.metadata";
86
88 switch (CGM.getContext().getCXXABIKind()) {
89 case TargetCXXABI::AppleARM64:
90 case TargetCXXABI::Fuchsia:
91 case TargetCXXABI::GenericAArch64:
92 case TargetCXXABI::GenericARM:
93 case TargetCXXABI::iOS:
94 case TargetCXXABI::WatchOS:
95 case TargetCXXABI::GenericMIPS:
96 case TargetCXXABI::GenericItanium:
97 case TargetCXXABI::WebAssembly:
98 case TargetCXXABI::XL:
99 return CreateItaniumCXXABI(CGM);
100 case TargetCXXABI::Microsoft:
101 return CreateMicrosoftCXXABI(CGM);
102 }
103
104 llvm_unreachable("invalid C++ ABI kind");
105}
106
107static std::unique_ptr<TargetCodeGenInfo>
109 const TargetInfo &Target = CGM.getTarget();
110 const llvm::Triple &Triple = Target.getTriple();
111 const CodeGenOptions &CodeGenOpts = CGM.getCodeGenOpts();
112
113 switch (Triple.getArch()) {
114 default:
116
117 case llvm::Triple::m68k:
118 return createM68kTargetCodeGenInfo(CGM);
119 case llvm::Triple::mips:
120 case llvm::Triple::mipsel:
121 if (Triple.getOS() == llvm::Triple::Win32)
122 return createWindowsMIPSTargetCodeGenInfo(CGM, /*IsOS32=*/true);
123 return createMIPSTargetCodeGenInfo(CGM, /*IsOS32=*/true);
124
125 case llvm::Triple::mips64:
126 case llvm::Triple::mips64el:
127 return createMIPSTargetCodeGenInfo(CGM, /*IsOS32=*/false);
128
129 case llvm::Triple::avr: {
130 // For passing parameters, R8~R25 are used on avr, and R18~R25 are used
131 // on avrtiny. For passing return value, R18~R25 are used on avr, and
132 // R22~R25 are used on avrtiny.
133 unsigned NPR = Target.getABI() == "avrtiny" ? 6 : 18;
134 unsigned NRR = Target.getABI() == "avrtiny" ? 4 : 8;
135 return createAVRTargetCodeGenInfo(CGM, NPR, NRR);
136 }
137
138 case llvm::Triple::aarch64:
139 case llvm::Triple::aarch64_32:
140 case llvm::Triple::aarch64_be: {
141 AArch64ABIKind Kind = AArch64ABIKind::AAPCS;
142 if (Target.getABI() == "darwinpcs")
143 Kind = AArch64ABIKind::DarwinPCS;
144 else if (Triple.isOSWindows())
145 return createWindowsAArch64TargetCodeGenInfo(CGM, AArch64ABIKind::Win64);
146 else if (Target.getABI() == "aapcs-soft")
147 Kind = AArch64ABIKind::AAPCSSoft;
148 else if (Target.getABI() == "pauthtest")
149 Kind = AArch64ABIKind::PAuthTest;
150
151 return createAArch64TargetCodeGenInfo(CGM, Kind);
152 }
153
154 case llvm::Triple::wasm32:
155 case llvm::Triple::wasm64: {
156 WebAssemblyABIKind Kind = WebAssemblyABIKind::MVP;
157 if (Target.getABI() == "experimental-mv")
158 Kind = WebAssemblyABIKind::ExperimentalMV;
159 return createWebAssemblyTargetCodeGenInfo(CGM, Kind);
160 }
161
162 case llvm::Triple::arm:
163 case llvm::Triple::armeb:
164 case llvm::Triple::thumb:
165 case llvm::Triple::thumbeb: {
166 if (Triple.getOS() == llvm::Triple::Win32)
167 return createWindowsARMTargetCodeGenInfo(CGM, ARMABIKind::AAPCS_VFP);
168
169 ARMABIKind Kind = ARMABIKind::AAPCS;
170 StringRef ABIStr = Target.getABI();
171 if (ABIStr == "apcs-gnu")
172 Kind = ARMABIKind::APCS;
173 else if (ABIStr == "aapcs16")
174 Kind = ARMABIKind::AAPCS16_VFP;
175 else if (CodeGenOpts.FloatABI == "hard" ||
176 (CodeGenOpts.FloatABI != "soft" && Triple.isHardFloatABI()))
177 Kind = ARMABIKind::AAPCS_VFP;
178
179 return createARMTargetCodeGenInfo(CGM, Kind);
180 }
181
182 case llvm::Triple::ppc: {
183 if (Triple.isOSAIX())
184 return createAIXTargetCodeGenInfo(CGM, /*Is64Bit=*/false);
185
186 bool IsSoftFloat =
187 CodeGenOpts.FloatABI == "soft" || Target.hasFeature("spe");
188 return createPPC32TargetCodeGenInfo(CGM, IsSoftFloat);
189 }
190 case llvm::Triple::ppcle: {
191 bool IsSoftFloat = CodeGenOpts.FloatABI == "soft";
192 return createPPC32TargetCodeGenInfo(CGM, IsSoftFloat);
193 }
194 case llvm::Triple::ppc64:
195 if (Triple.isOSAIX())
196 return createAIXTargetCodeGenInfo(CGM, /*Is64Bit=*/true);
197
198 if (Triple.isOSBinFormatELF()) {
199 PPC64_SVR4_ABIKind Kind = PPC64_SVR4_ABIKind::ELFv1;
200 if (Target.getABI() == "elfv2")
201 Kind = PPC64_SVR4_ABIKind::ELFv2;
202 bool IsSoftFloat = CodeGenOpts.FloatABI == "soft";
203
204 return createPPC64_SVR4_TargetCodeGenInfo(CGM, Kind, IsSoftFloat);
205 }
207 case llvm::Triple::ppc64le: {
208 assert(Triple.isOSBinFormatELF() && "PPC64 LE non-ELF not supported!");
209 PPC64_SVR4_ABIKind Kind = PPC64_SVR4_ABIKind::ELFv2;
210 if (Target.getABI() == "elfv1")
211 Kind = PPC64_SVR4_ABIKind::ELFv1;
212 bool IsSoftFloat = CodeGenOpts.FloatABI == "soft";
213
214 return createPPC64_SVR4_TargetCodeGenInfo(CGM, Kind, IsSoftFloat);
215 }
216
217 case llvm::Triple::nvptx:
218 case llvm::Triple::nvptx64:
220
221 case llvm::Triple::msp430:
223
224 case llvm::Triple::riscv32:
225 case llvm::Triple::riscv64: {
226 StringRef ABIStr = Target.getABI();
227 unsigned XLen = Target.getPointerWidth(LangAS::Default);
228 unsigned ABIFLen = 0;
229 if (ABIStr.ends_with("f"))
230 ABIFLen = 32;
231 else if (ABIStr.ends_with("d"))
232 ABIFLen = 64;
233 bool EABI = ABIStr.ends_with("e");
234 return createRISCVTargetCodeGenInfo(CGM, XLen, ABIFLen, EABI);
235 }
236
237 case llvm::Triple::systemz: {
238 bool SoftFloat = CodeGenOpts.FloatABI == "soft";
239 bool HasVector = !SoftFloat && Target.getABI() == "vector";
240 return createSystemZTargetCodeGenInfo(CGM, HasVector, SoftFloat);
241 }
242
243 case llvm::Triple::tce:
244 case llvm::Triple::tcele:
245 return createTCETargetCodeGenInfo(CGM);
246
247 case llvm::Triple::x86: {
248 bool IsDarwinVectorABI = Triple.isOSDarwin();
249 bool IsWin32FloatStructABI = Triple.isOSWindows() && !Triple.isOSCygMing();
250
251 if (Triple.getOS() == llvm::Triple::Win32) {
253 CGM, IsDarwinVectorABI, IsWin32FloatStructABI,
254 CodeGenOpts.NumRegisterParameters);
255 }
257 CGM, IsDarwinVectorABI, IsWin32FloatStructABI,
258 CodeGenOpts.NumRegisterParameters, CodeGenOpts.FloatABI == "soft");
259 }
260
261 case llvm::Triple::x86_64: {
262 StringRef ABI = Target.getABI();
263 X86AVXABILevel AVXLevel = (ABI == "avx512" ? X86AVXABILevel::AVX512
264 : ABI == "avx" ? X86AVXABILevel::AVX
265 : X86AVXABILevel::None);
266
267 switch (Triple.getOS()) {
268 case llvm::Triple::UEFI:
269 case llvm::Triple::Win32:
270 return createWinX86_64TargetCodeGenInfo(CGM, AVXLevel);
271 default:
272 return createX86_64TargetCodeGenInfo(CGM, AVXLevel);
273 }
274 }
275 case llvm::Triple::hexagon:
277 case llvm::Triple::lanai:
279 case llvm::Triple::r600:
281 case llvm::Triple::amdgcn:
283 case llvm::Triple::sparc:
285 case llvm::Triple::sparcv9:
287 case llvm::Triple::xcore:
289 case llvm::Triple::arc:
290 return createARCTargetCodeGenInfo(CGM);
291 case llvm::Triple::spir:
292 case llvm::Triple::spir64:
294 case llvm::Triple::spirv32:
295 case llvm::Triple::spirv64:
296 case llvm::Triple::spirv:
298 case llvm::Triple::dxil:
300 case llvm::Triple::ve:
301 return createVETargetCodeGenInfo(CGM);
302 case llvm::Triple::csky: {
303 bool IsSoftFloat = !Target.hasFeature("hard-float-abi");
304 bool hasFP64 =
305 Target.hasFeature("fpuv2_df") || Target.hasFeature("fpuv3_df");
306 return createCSKYTargetCodeGenInfo(CGM, IsSoftFloat ? 0
307 : hasFP64 ? 64
308 : 32);
309 }
310 case llvm::Triple::bpfeb:
311 case llvm::Triple::bpfel:
312 return createBPFTargetCodeGenInfo(CGM);
313 case llvm::Triple::loongarch32:
314 case llvm::Triple::loongarch64: {
315 StringRef ABIStr = Target.getABI();
316 unsigned ABIFRLen = 0;
317 if (ABIStr.ends_with("f"))
318 ABIFRLen = 32;
319 else if (ABIStr.ends_with("d"))
320 ABIFRLen = 64;
322 CGM, Target.getPointerWidth(LangAS::Default), ABIFRLen);
323 }
324 }
325}
326
328 if (!TheTargetCodeGenInfo)
329 TheTargetCodeGenInfo = createTargetCodeGenInfo(*this);
330 return *TheTargetCodeGenInfo;
331}
332
334 llvm::LLVMContext &Context,
335 const LangOptions &Opts) {
336#ifndef NDEBUG
337 // Don't verify non-standard ABI configurations.
338 if (Opts.AlignDouble || Opts.OpenCL || Opts.HLSL)
339 return;
340
341 llvm::Triple Triple = Target.getTriple();
342 llvm::DataLayout DL(Target.getDataLayoutString());
343 auto Check = [&](const char *Name, llvm::Type *Ty, unsigned Alignment) {
344 llvm::Align DLAlign = DL.getABITypeAlign(Ty);
345 llvm::Align ClangAlign(Alignment / 8);
346 if (DLAlign != ClangAlign) {
347 llvm::errs() << "For target " << Triple.str() << " type " << Name
348 << " mapping to " << *Ty << " has data layout alignment "
349 << DLAlign.value() << " while clang specifies "
350 << ClangAlign.value() << "\n";
351 abort();
352 }
353 };
354
355 Check("bool", llvm::Type::getIntNTy(Context, Target.BoolWidth),
356 Target.BoolAlign);
357 Check("short", llvm::Type::getIntNTy(Context, Target.ShortWidth),
358 Target.ShortAlign);
359 Check("int", llvm::Type::getIntNTy(Context, Target.IntWidth),
360 Target.IntAlign);
361 Check("long", llvm::Type::getIntNTy(Context, Target.LongWidth),
362 Target.LongAlign);
363 // FIXME: M68k specifies incorrect long long alignment in both LLVM and Clang.
364 if (Triple.getArch() != llvm::Triple::m68k)
365 Check("long long", llvm::Type::getIntNTy(Context, Target.LongLongWidth),
366 Target.LongLongAlign);
367 // FIXME: There are int128 alignment mismatches on multiple targets.
368 if (Target.hasInt128Type() && !Target.getTargetOpts().ForceEnableInt128 &&
369 !Triple.isAMDGPU() && !Triple.isSPIRV() &&
370 Triple.getArch() != llvm::Triple::ve)
371 Check("__int128", llvm::Type::getIntNTy(Context, 128), Target.Int128Align);
372
373 if (Target.hasFloat16Type())
374 Check("half", llvm::Type::getFloatingPointTy(Context, *Target.HalfFormat),
375 Target.HalfAlign);
376 if (Target.hasBFloat16Type())
377 Check("bfloat", llvm::Type::getBFloatTy(Context), Target.BFloat16Align);
378 Check("float", llvm::Type::getFloatingPointTy(Context, *Target.FloatFormat),
379 Target.FloatAlign);
380 // FIXME: AIX specifies wrong double alignment in DataLayout
381 if (!Triple.isOSAIX()) {
382 Check("double",
383 llvm::Type::getFloatingPointTy(Context, *Target.DoubleFormat),
384 Target.DoubleAlign);
385 Check("long double",
386 llvm::Type::getFloatingPointTy(Context, *Target.LongDoubleFormat),
387 Target.LongDoubleAlign);
388 }
389 if (Target.hasFloat128Type())
390 Check("__float128", llvm::Type::getFP128Ty(Context), Target.Float128Align);
391 if (Target.hasIbm128Type())
392 Check("__ibm128", llvm::Type::getPPC_FP128Ty(Context), Target.Ibm128Align);
393
394 Check("void*", llvm::PointerType::getUnqual(Context), Target.PointerAlign);
395#endif
396}
397
398CodeGenModule::CodeGenModule(ASTContext &C,
400 const HeaderSearchOptions &HSO,
401 const PreprocessorOptions &PPO,
402 const CodeGenOptions &CGO, llvm::Module &M,
403 DiagnosticsEngine &diags,
404 CoverageSourceInfo *CoverageInfo)
405 : Context(C), LangOpts(C.getLangOpts()), FS(FS), HeaderSearchOpts(HSO),
406 PreprocessorOpts(PPO), CodeGenOpts(CGO), TheModule(M), Diags(diags),
407 Target(C.getTargetInfo()), ABI(createCXXABI(*this)),
408 VMContext(M.getContext()), VTables(*this), StackHandler(diags),
409 SanitizerMD(new SanitizerMetadata(*this)),
410 AtomicOpts(Target.getAtomicOpts()) {
411
412 // Initialize the type cache.
413 Types.reset(new CodeGenTypes(*this));
414 llvm::LLVMContext &LLVMContext = M.getContext();
415 VoidTy = llvm::Type::getVoidTy(LLVMContext);
416 Int8Ty = llvm::Type::getInt8Ty(LLVMContext);
417 Int16Ty = llvm::Type::getInt16Ty(LLVMContext);
418 Int32Ty = llvm::Type::getInt32Ty(LLVMContext);
419 Int64Ty = llvm::Type::getInt64Ty(LLVMContext);
420 HalfTy = llvm::Type::getHalfTy(LLVMContext);
421 BFloatTy = llvm::Type::getBFloatTy(LLVMContext);
422 FloatTy = llvm::Type::getFloatTy(LLVMContext);
423 DoubleTy = llvm::Type::getDoubleTy(LLVMContext);
424 PointerWidthInBits = C.getTargetInfo().getPointerWidth(LangAS::Default);
426 C.toCharUnitsFromBits(C.getTargetInfo().getPointerAlign(LangAS::Default))
427 .getQuantity();
429 C.toCharUnitsFromBits(C.getTargetInfo().getMaxPointerWidth()).getQuantity();
431 C.toCharUnitsFromBits(C.getTargetInfo().getIntAlign()).getQuantity();
432 CharTy =
433 llvm::IntegerType::get(LLVMContext, C.getTargetInfo().getCharWidth());
434 IntTy = llvm::IntegerType::get(LLVMContext, C.getTargetInfo().getIntWidth());
435 IntPtrTy = llvm::IntegerType::get(LLVMContext,
436 C.getTargetInfo().getMaxPointerWidth());
437 Int8PtrTy = llvm::PointerType::get(LLVMContext,
438 C.getTargetAddressSpace(LangAS::Default));
439 const llvm::DataLayout &DL = M.getDataLayout();
441 llvm::PointerType::get(LLVMContext, DL.getAllocaAddrSpace());
443 llvm::PointerType::get(LLVMContext, DL.getDefaultGlobalsAddressSpace());
444 ConstGlobalsPtrTy = llvm::PointerType::get(
445 LLVMContext, C.getTargetAddressSpace(GetGlobalConstantAddressSpace()));
447
448 // Build C++20 Module initializers.
449 // TODO: Add Microsoft here once we know the mangling required for the
450 // initializers.
451 CXX20ModuleInits =
452 LangOpts.CPlusPlusModules && getCXXABI().getMangleContext().getKind() ==
454
455 RuntimeCC = getTargetCodeGenInfo().getABIInfo().getRuntimeCC();
456
457 if (LangOpts.ObjC)
458 createObjCRuntime();
459 if (LangOpts.OpenCL)
460 createOpenCLRuntime();
461 if (LangOpts.OpenMP)
462 createOpenMPRuntime();
463 if (LangOpts.CUDA)
464 createCUDARuntime();
465 if (LangOpts.HLSL)
466 createHLSLRuntime();
467
468 // Enable TBAA unless it's suppressed. TSan and TySan need TBAA even at O0.
469 if (LangOpts.Sanitize.hasOneOf(SanitizerKind::Thread | SanitizerKind::Type) ||
470 (!CodeGenOpts.RelaxedAliasing && CodeGenOpts.OptimizationLevel > 0))
471 TBAA.reset(new CodeGenTBAA(Context, getTypes(), TheModule, CodeGenOpts,
472 getLangOpts()));
473
474 // If debug info or coverage generation is enabled, create the CGDebugInfo
475 // object.
476 if (CodeGenOpts.getDebugInfo() != llvm::codegenoptions::NoDebugInfo ||
477 CodeGenOpts.CoverageNotesFile.size() ||
478 CodeGenOpts.CoverageDataFile.size())
479 DebugInfo.reset(new CGDebugInfo(*this));
480 else if (getTriple().isOSWindows())
481 // On Windows targets, we want to emit compiler info even if debug info is
482 // otherwise disabled. Use a temporary CGDebugInfo instance to emit only
483 // basic compiler metadata.
484 CGDebugInfo(*this);
485
486 Block.GlobalUniqueCount = 0;
487
488 if (C.getLangOpts().ObjC)
489 ObjCData.reset(new ObjCEntrypoints());
490
491 if (CodeGenOpts.hasProfileClangUse()) {
492 auto ReaderOrErr = llvm::IndexedInstrProfReader::create(
493 CodeGenOpts.ProfileInstrumentUsePath, *FS,
494 CodeGenOpts.ProfileRemappingFile);
495 // We're checking for profile read errors in CompilerInvocation, so if
496 // there was an error it should've already been caught. If it hasn't been
497 // somehow, trip an assertion.
498 assert(ReaderOrErr);
499 PGOReader = std::move(ReaderOrErr.get());
500 }
501
502 // If coverage mapping generation is enabled, create the
503 // CoverageMappingModuleGen object.
504 if (CodeGenOpts.CoverageMapping)
505 CoverageMapping.reset(new CoverageMappingModuleGen(*this, *CoverageInfo));
506
507 // Generate the module name hash here if needed.
508 if (CodeGenOpts.UniqueInternalLinkageNames &&
509 !getModule().getSourceFileName().empty()) {
510 std::string Path = getModule().getSourceFileName();
511 // Check if a path substitution is needed from the MacroPrefixMap.
512 for (const auto &Entry : LangOpts.MacroPrefixMap)
513 if (Path.rfind(Entry.first, 0) != std::string::npos) {
514 Path = Entry.second + Path.substr(Entry.first.size());
515 break;
516 }
517 ModuleNameHash = llvm::getUniqueInternalLinkagePostfix(Path);
518 }
519
520 // Record mregparm value now so it is visible through all of codegen.
521 if (Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86)
522 getModule().addModuleFlag(llvm::Module::Error, "NumRegisterParameters",
523 CodeGenOpts.NumRegisterParameters);
524
525 // If there are any functions that are marked for Windows secure hot-patching,
526 // then build the list of functions now.
527 if (!CGO.MSSecureHotPatchFunctionsFile.empty() ||
528 !CGO.MSSecureHotPatchFunctionsList.empty()) {
529 if (!CGO.MSSecureHotPatchFunctionsFile.empty()) {
530 auto BufOrErr =
531 llvm::MemoryBuffer::getFile(CGO.MSSecureHotPatchFunctionsFile);
532 if (BufOrErr) {
533 const llvm::MemoryBuffer &FileBuffer = **BufOrErr;
534 for (llvm::line_iterator I(FileBuffer.getMemBufferRef(), true), E;
535 I != E; ++I)
536 this->MSHotPatchFunctions.push_back(std::string{*I});
537 } else {
538 auto &DE = Context.getDiagnostics();
539 unsigned DiagID =
541 "failed to open hotpatch functions file "
542 "(-fms-hotpatch-functions-file): %0 : %1");
543 DE.Report(DiagID) << CGO.MSSecureHotPatchFunctionsFile
544 << BufOrErr.getError().message();
545 }
546 }
547
548 for (const auto &FuncName : CGO.MSSecureHotPatchFunctionsList)
549 this->MSHotPatchFunctions.push_back(FuncName);
550
551 llvm::sort(this->MSHotPatchFunctions);
552 }
553
554 if (!Context.getAuxTargetInfo())
555 checkDataLayoutConsistency(Context.getTargetInfo(), LLVMContext, LangOpts);
556}
557
559
560void CodeGenModule::createObjCRuntime() {
561 // This is just isGNUFamily(), but we want to force implementors of
562 // new ABIs to decide how best to do this.
563 switch (LangOpts.ObjCRuntime.getKind()) {
565 case ObjCRuntime::GCC:
567 ObjCRuntime.reset(CreateGNUObjCRuntime(*this));
568 return;
569
572 case ObjCRuntime::iOS:
574 ObjCRuntime.reset(CreateMacObjCRuntime(*this));
575 return;
576 }
577 llvm_unreachable("bad runtime kind");
578}
579
580void CodeGenModule::createOpenCLRuntime() {
581 OpenCLRuntime.reset(new CGOpenCLRuntime(*this));
582}
583
584void CodeGenModule::createOpenMPRuntime() {
585 if (!LangOpts.OMPHostIRFile.empty() &&
586 !llvm::sys::fs::exists(LangOpts.OMPHostIRFile))
587 Diags.Report(diag::err_omp_host_ir_file_not_found)
588 << LangOpts.OMPHostIRFile;
589
590 // Select a specialized code generation class based on the target, if any.
591 // If it does not exist use the default implementation.
592 switch (getTriple().getArch()) {
593 case llvm::Triple::nvptx:
594 case llvm::Triple::nvptx64:
595 case llvm::Triple::amdgcn:
596 case llvm::Triple::spirv64:
597 assert(
598 getLangOpts().OpenMPIsTargetDevice &&
599 "OpenMP AMDGPU/NVPTX/SPIRV is only prepared to deal with device code.");
600 OpenMPRuntime.reset(new CGOpenMPRuntimeGPU(*this));
601 break;
602 default:
603 if (LangOpts.OpenMPSimd)
604 OpenMPRuntime.reset(new CGOpenMPSIMDRuntime(*this));
605 else
606 OpenMPRuntime.reset(new CGOpenMPRuntime(*this));
607 break;
608 }
609}
610
611void CodeGenModule::createCUDARuntime() {
612 CUDARuntime.reset(CreateNVCUDARuntime(*this));
613}
614
615void CodeGenModule::createHLSLRuntime() {
616 HLSLRuntime.reset(new CGHLSLRuntime(*this));
617}
618
619void CodeGenModule::addReplacement(StringRef Name, llvm::Constant *C) {
620 Replacements[Name] = C;
621}
622
623void CodeGenModule::applyReplacements() {
624 for (auto &I : Replacements) {
625 StringRef MangledName = I.first;
626 llvm::Constant *Replacement = I.second;
627 llvm::GlobalValue *Entry = GetGlobalValue(MangledName);
628 if (!Entry)
629 continue;
630 auto *OldF = cast<llvm::Function>(Entry);
631 auto *NewF = dyn_cast<llvm::Function>(Replacement);
632 if (!NewF) {
633 if (auto *Alias = dyn_cast<llvm::GlobalAlias>(Replacement)) {
634 NewF = dyn_cast<llvm::Function>(Alias->getAliasee());
635 } else {
636 auto *CE = cast<llvm::ConstantExpr>(Replacement);
637 assert(CE->getOpcode() == llvm::Instruction::BitCast ||
638 CE->getOpcode() == llvm::Instruction::GetElementPtr);
639 NewF = dyn_cast<llvm::Function>(CE->getOperand(0));
640 }
641 }
642
643 // Replace old with new, but keep the old order.
644 OldF->replaceAllUsesWith(Replacement);
645 if (NewF) {
646 NewF->removeFromParent();
647 OldF->getParent()->getFunctionList().insertAfter(OldF->getIterator(),
648 NewF);
649 }
650 OldF->eraseFromParent();
651 }
652}
653
654void CodeGenModule::addGlobalValReplacement(llvm::GlobalValue *GV, llvm::Constant *C) {
655 GlobalValReplacements.push_back(std::make_pair(GV, C));
656}
657
658void CodeGenModule::applyGlobalValReplacements() {
659 for (auto &I : GlobalValReplacements) {
660 llvm::GlobalValue *GV = I.first;
661 llvm::Constant *C = I.second;
662
663 GV->replaceAllUsesWith(C);
664 GV->eraseFromParent();
665 }
666}
667
668// This is only used in aliases that we created and we know they have a
669// linear structure.
670static const llvm::GlobalValue *getAliasedGlobal(const llvm::GlobalValue *GV) {
671 const llvm::Constant *C;
672 if (auto *GA = dyn_cast<llvm::GlobalAlias>(GV))
673 C = GA->getAliasee();
674 else if (auto *GI = dyn_cast<llvm::GlobalIFunc>(GV))
675 C = GI->getResolver();
676 else
677 return GV;
678
679 const auto *AliaseeGV = dyn_cast<llvm::GlobalValue>(C->stripPointerCasts());
680 if (!AliaseeGV)
681 return nullptr;
682
683 const llvm::GlobalValue *FinalGV = AliaseeGV->getAliaseeObject();
684 if (FinalGV == GV)
685 return nullptr;
686
687 return FinalGV;
688}
689
691 const ASTContext &Context, DiagnosticsEngine &Diags, SourceLocation Location,
692 bool IsIFunc, const llvm::GlobalValue *Alias, const llvm::GlobalValue *&GV,
693 const llvm::MapVector<GlobalDecl, StringRef> &MangledDeclNames,
694 SourceRange AliasRange) {
695 GV = getAliasedGlobal(Alias);
696 if (!GV) {
697 Diags.Report(Location, diag::err_cyclic_alias) << IsIFunc;
698 return false;
699 }
700
701 if (GV->hasCommonLinkage()) {
702 const llvm::Triple &Triple = Context.getTargetInfo().getTriple();
703 if (Triple.getObjectFormat() == llvm::Triple::XCOFF) {
704 Diags.Report(Location, diag::err_alias_to_common);
705 return false;
706 }
707 }
708
709 if (GV->isDeclaration()) {
710 Diags.Report(Location, diag::err_alias_to_undefined) << IsIFunc << IsIFunc;
711 Diags.Report(Location, diag::note_alias_requires_mangled_name)
712 << IsIFunc << IsIFunc;
713 // Provide a note if the given function is not found and exists as a
714 // mangled name.
715 for (const auto &[Decl, Name] : MangledDeclNames) {
716 if (const auto *ND = dyn_cast<NamedDecl>(Decl.getDecl())) {
717 IdentifierInfo *II = ND->getIdentifier();
718 if (II && II->getName() == GV->getName()) {
719 Diags.Report(Location, diag::note_alias_mangled_name_alternative)
720 << Name
722 AliasRange,
723 (Twine(IsIFunc ? "ifunc" : "alias") + "(\"" + Name + "\")")
724 .str());
725 }
726 }
727 }
728 return false;
729 }
730
731 if (IsIFunc) {
732 // Check resolver function type.
733 const auto *F = dyn_cast<llvm::Function>(GV);
734 if (!F) {
735 Diags.Report(Location, diag::err_alias_to_undefined)
736 << IsIFunc << IsIFunc;
737 return false;
738 }
739
740 llvm::FunctionType *FTy = F->getFunctionType();
741 if (!FTy->getReturnType()->isPointerTy()) {
742 Diags.Report(Location, diag::err_ifunc_resolver_return);
743 return false;
744 }
745 }
746
747 return true;
748}
749
750// Emit a warning if toc-data attribute is requested for global variables that
751// have aliases and remove the toc-data attribute.
752static void checkAliasForTocData(llvm::GlobalVariable *GVar,
753 const CodeGenOptions &CodeGenOpts,
754 DiagnosticsEngine &Diags,
755 SourceLocation Location) {
756 if (GVar->hasAttribute("toc-data")) {
757 auto GVId = GVar->getName();
758 // Is this a global variable specified by the user as local?
759 if ((llvm::binary_search(CodeGenOpts.TocDataVarsUserSpecified, GVId))) {
760 Diags.Report(Location, diag::warn_toc_unsupported_type)
761 << GVId << "the variable has an alias";
762 }
763 llvm::AttributeSet CurrAttributes = GVar->getAttributes();
764 llvm::AttributeSet NewAttributes =
765 CurrAttributes.removeAttribute(GVar->getContext(), "toc-data");
766 GVar->setAttributes(NewAttributes);
767 }
768}
769
770void CodeGenModule::checkAliases() {
771 // Check if the constructed aliases are well formed. It is really unfortunate
772 // that we have to do this in CodeGen, but we only construct mangled names
773 // and aliases during codegen.
774 bool Error = false;
775 DiagnosticsEngine &Diags = getDiags();
776 for (const GlobalDecl &GD : Aliases) {
777 const auto *D = cast<ValueDecl>(GD.getDecl());
778 SourceLocation Location;
780 bool IsIFunc = D->hasAttr<IFuncAttr>();
781 if (const Attr *A = D->getDefiningAttr()) {
782 Location = A->getLocation();
783 Range = A->getRange();
784 } else
785 llvm_unreachable("Not an alias or ifunc?");
786
787 StringRef MangledName = getMangledName(GD);
788 llvm::GlobalValue *Alias = GetGlobalValue(MangledName);
789 const llvm::GlobalValue *GV = nullptr;
790 if (!checkAliasedGlobal(getContext(), Diags, Location, IsIFunc, Alias, GV,
791 MangledDeclNames, Range)) {
792 Error = true;
793 continue;
794 }
795
796 if (getContext().getTargetInfo().getTriple().isOSAIX())
797 if (const llvm::GlobalVariable *GVar =
798 dyn_cast<const llvm::GlobalVariable>(GV))
799 checkAliasForTocData(const_cast<llvm::GlobalVariable *>(GVar),
800 getCodeGenOpts(), Diags, Location);
801
802 llvm::Constant *Aliasee =
803 IsIFunc ? cast<llvm::GlobalIFunc>(Alias)->getResolver()
804 : cast<llvm::GlobalAlias>(Alias)->getAliasee();
805
806 llvm::GlobalValue *AliaseeGV;
807 if (auto CE = dyn_cast<llvm::ConstantExpr>(Aliasee))
808 AliaseeGV = cast<llvm::GlobalValue>(CE->getOperand(0));
809 else
810 AliaseeGV = cast<llvm::GlobalValue>(Aliasee);
811
812 if (const SectionAttr *SA = D->getAttr<SectionAttr>()) {
813 StringRef AliasSection = SA->getName();
814 if (AliasSection != AliaseeGV->getSection())
815 Diags.Report(SA->getLocation(), diag::warn_alias_with_section)
816 << AliasSection << IsIFunc << IsIFunc;
817 }
818
819 // We have to handle alias to weak aliases in here. LLVM itself disallows
820 // this since the object semantics would not match the IL one. For
821 // compatibility with gcc we implement it by just pointing the alias
822 // to its aliasee's aliasee. We also warn, since the user is probably
823 // expecting the link to be weak.
824 if (auto *GA = dyn_cast<llvm::GlobalAlias>(AliaseeGV)) {
825 if (GA->isInterposable()) {
826 Diags.Report(Location, diag::warn_alias_to_weak_alias)
827 << GV->getName() << GA->getName() << IsIFunc;
828 Aliasee = llvm::ConstantExpr::getPointerBitCastOrAddrSpaceCast(
829 GA->getAliasee(), Alias->getType());
830
831 if (IsIFunc)
832 cast<llvm::GlobalIFunc>(Alias)->setResolver(Aliasee);
833 else
834 cast<llvm::GlobalAlias>(Alias)->setAliasee(Aliasee);
835 }
836 }
837 // ifunc resolvers are usually implemented to run before sanitizer
838 // initialization. Disable instrumentation to prevent the ordering issue.
839 if (IsIFunc)
840 cast<llvm::Function>(Aliasee)->addFnAttr(
841 llvm::Attribute::DisableSanitizerInstrumentation);
842 }
843 if (!Error)
844 return;
845
846 for (const GlobalDecl &GD : Aliases) {
847 StringRef MangledName = getMangledName(GD);
848 llvm::GlobalValue *Alias = GetGlobalValue(MangledName);
849 Alias->replaceAllUsesWith(llvm::PoisonValue::get(Alias->getType()));
850 Alias->eraseFromParent();
851 }
852}
853
855 DeferredDeclsToEmit.clear();
856 EmittedDeferredDecls.clear();
857 DeferredAnnotations.clear();
858 if (OpenMPRuntime)
859 OpenMPRuntime->clear();
860}
861
863 StringRef MainFile) {
864 if (!hasDiagnostics())
865 return;
866 if (VisitedInMainFile > 0 && VisitedInMainFile == MissingInMainFile) {
867 if (MainFile.empty())
868 MainFile = "<stdin>";
869 Diags.Report(diag::warn_profile_data_unprofiled) << MainFile;
870 } else {
871 if (Mismatched > 0)
872 Diags.Report(diag::warn_profile_data_out_of_date) << Visited << Mismatched;
873
874 if (Missing > 0)
875 Diags.Report(diag::warn_profile_data_missing) << Visited << Missing;
876 }
877}
878
879static std::optional<llvm::GlobalValue::VisibilityTypes>
881 // Map to LLVM visibility.
882 switch (K) {
884 return std::nullopt;
886 return llvm::GlobalValue::DefaultVisibility;
888 return llvm::GlobalValue::HiddenVisibility;
890 return llvm::GlobalValue::ProtectedVisibility;
891 }
892 llvm_unreachable("unknown option value!");
893}
894
895static void
896setLLVMVisibility(llvm::GlobalValue &GV,
897 std::optional<llvm::GlobalValue::VisibilityTypes> V) {
898 if (!V)
899 return;
900
901 // Reset DSO locality before setting the visibility. This removes
902 // any effects that visibility options and annotations may have
903 // had on the DSO locality. Setting the visibility will implicitly set
904 // appropriate globals to DSO Local; however, this will be pessimistic
905 // w.r.t. to the normal compiler IRGen.
906 GV.setDSOLocal(false);
907 GV.setVisibility(*V);
908}
909
911 llvm::Module &M) {
912 if (!LO.VisibilityFromDLLStorageClass)
913 return;
914
915 std::optional<llvm::GlobalValue::VisibilityTypes> DLLExportVisibility =
916 getLLVMVisibility(LO.getDLLExportVisibility());
917
918 std::optional<llvm::GlobalValue::VisibilityTypes>
919 NoDLLStorageClassVisibility =
920 getLLVMVisibility(LO.getNoDLLStorageClassVisibility());
921
922 std::optional<llvm::GlobalValue::VisibilityTypes>
923 ExternDeclDLLImportVisibility =
924 getLLVMVisibility(LO.getExternDeclDLLImportVisibility());
925
926 std::optional<llvm::GlobalValue::VisibilityTypes>
927 ExternDeclNoDLLStorageClassVisibility =
928 getLLVMVisibility(LO.getExternDeclNoDLLStorageClassVisibility());
929
930 for (llvm::GlobalValue &GV : M.global_values()) {
931 if (GV.hasAppendingLinkage() || GV.hasLocalLinkage())
932 continue;
933
934 if (GV.isDeclarationForLinker())
935 setLLVMVisibility(GV, GV.getDLLStorageClass() ==
936 llvm::GlobalValue::DLLImportStorageClass
937 ? ExternDeclDLLImportVisibility
938 : ExternDeclNoDLLStorageClassVisibility);
939 else
940 setLLVMVisibility(GV, GV.getDLLStorageClass() ==
941 llvm::GlobalValue::DLLExportStorageClass
942 ? DLLExportVisibility
943 : NoDLLStorageClassVisibility);
944
945 GV.setDLLStorageClass(llvm::GlobalValue::DefaultStorageClass);
946 }
947}
948
949static bool isStackProtectorOn(const LangOptions &LangOpts,
950 const llvm::Triple &Triple,
952 if (Triple.isGPU())
953 return false;
954 return LangOpts.getStackProtector() == Mode;
955}
956
959 if (CXX20ModuleInits && Primary && !Primary->isHeaderLikeModule())
960 EmitModuleInitializers(Primary);
961 EmitDeferred();
962 DeferredDecls.insert_range(EmittedDeferredDecls);
963 EmittedDeferredDecls.clear();
964 EmitVTablesOpportunistically();
965 applyGlobalValReplacements();
966 applyReplacements();
967 emitMultiVersionFunctions();
968
969 if (Context.getLangOpts().IncrementalExtensions &&
970 GlobalTopLevelStmtBlockInFlight.first) {
971 const TopLevelStmtDecl *TLSD = GlobalTopLevelStmtBlockInFlight.second;
972 GlobalTopLevelStmtBlockInFlight.first->FinishFunction(TLSD->getEndLoc());
973 GlobalTopLevelStmtBlockInFlight = {nullptr, nullptr};
974 }
975
976 // Module implementations are initialized the same way as a regular TU that
977 // imports one or more modules.
978 if (CXX20ModuleInits && Primary && Primary->isInterfaceOrPartition())
979 EmitCXXModuleInitFunc(Primary);
980 else
981 EmitCXXGlobalInitFunc();
982 EmitCXXGlobalCleanUpFunc();
983 registerGlobalDtorsWithAtExit();
984 EmitCXXThreadLocalInitFunc();
985 if (ObjCRuntime)
986 if (llvm::Function *ObjCInitFunction = ObjCRuntime->ModuleInitFunction())
987 AddGlobalCtor(ObjCInitFunction);
988 if (Context.getLangOpts().CUDA && CUDARuntime) {
989 if (llvm::Function *CudaCtorFunction = CUDARuntime->finalizeModule())
990 AddGlobalCtor(CudaCtorFunction);
991 }
992 if (OpenMPRuntime) {
993 OpenMPRuntime->createOffloadEntriesAndInfoMetadata();
994 OpenMPRuntime->clear();
995 }
996 if (PGOReader) {
997 getModule().setProfileSummary(
998 PGOReader->getSummary(/* UseCS */ false).getMD(VMContext),
999 llvm::ProfileSummary::PSK_Instr);
1000 if (PGOStats.hasDiagnostics())
1001 PGOStats.reportDiagnostics(getDiags(), getCodeGenOpts().MainFileName);
1002 }
1003 llvm::stable_sort(GlobalCtors, [](const Structor &L, const Structor &R) {
1004 return L.LexOrder < R.LexOrder;
1005 });
1006 EmitCtorList(GlobalCtors, "llvm.global_ctors");
1007 EmitCtorList(GlobalDtors, "llvm.global_dtors");
1009 EmitStaticExternCAliases();
1010 checkAliases();
1014 if (CoverageMapping)
1015 CoverageMapping->emit();
1016 if (CodeGenOpts.SanitizeCfiCrossDso) {
1019 }
1020 if (LangOpts.Sanitize.has(SanitizerKind::KCFI))
1022 emitAtAvailableLinkGuard();
1023 if (Context.getTargetInfo().getTriple().isWasm())
1025
1026 if (getTriple().isAMDGPU() ||
1027 (getTriple().isSPIRV() && getTriple().getVendor() == llvm::Triple::AMD)) {
1028 // Emit amdhsa_code_object_version module flag, which is code object version
1029 // times 100.
1030 if (getTarget().getTargetOpts().CodeObjectVersion !=
1031 llvm::CodeObjectVersionKind::COV_None) {
1032 getModule().addModuleFlag(llvm::Module::Error,
1033 "amdhsa_code_object_version",
1034 getTarget().getTargetOpts().CodeObjectVersion);
1035 }
1036
1037 // Currently, "-mprintf-kind" option is only supported for HIP
1038 if (LangOpts.HIP) {
1039 auto *MDStr = llvm::MDString::get(
1040 getLLVMContext(), (getTarget().getTargetOpts().AMDGPUPrintfKindVal ==
1042 ? "hostcall"
1043 : "buffered");
1044 getModule().addModuleFlag(llvm::Module::Error, "amdgpu_printf_kind",
1045 MDStr);
1046 }
1047 }
1048
1049 // Emit a global array containing all external kernels or device variables
1050 // used by host functions and mark it as used for CUDA/HIP. This is necessary
1051 // to get kernels or device variables in archives linked in even if these
1052 // kernels or device variables are only used in host functions.
1053 if (!Context.CUDAExternalDeviceDeclODRUsedByHost.empty()) {
1055 for (auto D : Context.CUDAExternalDeviceDeclODRUsedByHost) {
1056 GlobalDecl GD;
1057 if (auto *FD = dyn_cast<FunctionDecl>(D))
1059 else
1060 GD = GlobalDecl(D);
1061 UsedArray.push_back(llvm::ConstantExpr::getPointerBitCastOrAddrSpaceCast(
1063 }
1064
1065 llvm::ArrayType *ATy = llvm::ArrayType::get(Int8PtrTy, UsedArray.size());
1066
1067 auto *GV = new llvm::GlobalVariable(
1068 getModule(), ATy, false, llvm::GlobalValue::InternalLinkage,
1069 llvm::ConstantArray::get(ATy, UsedArray), "__clang_gpu_used_external");
1071 }
1072 if (LangOpts.HIP) {
1073 // Emit a unique ID so that host and device binaries from the same
1074 // compilation unit can be associated.
1075 auto *GV = new llvm::GlobalVariable(
1076 getModule(), Int8Ty, false, llvm::GlobalValue::ExternalLinkage,
1077 llvm::Constant::getNullValue(Int8Ty),
1078 "__hip_cuid_" + getContext().getCUIDHash());
1081 }
1082 emitLLVMUsed();
1083 if (SanStats)
1084 SanStats->finish();
1085
1086 if (CodeGenOpts.Autolink &&
1087 (Context.getLangOpts().Modules || !LinkerOptionsMetadata.empty())) {
1088 EmitModuleLinkOptions();
1089 }
1090
1091 // On ELF we pass the dependent library specifiers directly to the linker
1092 // without manipulating them. This is in contrast to other platforms where
1093 // they are mapped to a specific linker option by the compiler. This
1094 // difference is a result of the greater variety of ELF linkers and the fact
1095 // that ELF linkers tend to handle libraries in a more complicated fashion
1096 // than on other platforms. This forces us to defer handling the dependent
1097 // libs to the linker.
1098 //
1099 // CUDA/HIP device and host libraries are different. Currently there is no
1100 // way to differentiate dependent libraries for host or device. Existing
1101 // usage of #pragma comment(lib, *) is intended for host libraries on
1102 // Windows. Therefore emit llvm.dependent-libraries only for host.
1103 if (!ELFDependentLibraries.empty() && !Context.getLangOpts().CUDAIsDevice) {
1104 auto *NMD = getModule().getOrInsertNamedMetadata("llvm.dependent-libraries");
1105 for (auto *MD : ELFDependentLibraries)
1106 NMD->addOperand(MD);
1107 }
1108
1109 if (CodeGenOpts.DwarfVersion) {
1110 getModule().addModuleFlag(llvm::Module::Max, "Dwarf Version",
1111 CodeGenOpts.DwarfVersion);
1112 }
1113
1114 if (CodeGenOpts.Dwarf64)
1115 getModule().addModuleFlag(llvm::Module::Max, "DWARF64", 1);
1116
1117 if (Context.getLangOpts().SemanticInterposition)
1118 // Require various optimization to respect semantic interposition.
1119 getModule().setSemanticInterposition(true);
1120
1121 if (CodeGenOpts.EmitCodeView) {
1122 // Indicate that we want CodeView in the metadata.
1123 getModule().addModuleFlag(llvm::Module::Warning, "CodeView", 1);
1124 }
1125 if (CodeGenOpts.CodeViewGHash) {
1126 getModule().addModuleFlag(llvm::Module::Warning, "CodeViewGHash", 1);
1127 }
1128 if (CodeGenOpts.ControlFlowGuard) {
1129 // Function ID tables and checks for Control Flow Guard (cfguard=2).
1130 getModule().addModuleFlag(llvm::Module::Warning, "cfguard", 2);
1131 } else if (CodeGenOpts.ControlFlowGuardNoChecks) {
1132 // Function ID tables for Control Flow Guard (cfguard=1).
1133 getModule().addModuleFlag(llvm::Module::Warning, "cfguard", 1);
1134 }
1135 if (CodeGenOpts.EHContGuard) {
1136 // Function ID tables for EH Continuation Guard.
1137 getModule().addModuleFlag(llvm::Module::Warning, "ehcontguard", 1);
1138 }
1139 if (Context.getLangOpts().Kernel) {
1140 // Note if we are compiling with /kernel.
1141 getModule().addModuleFlag(llvm::Module::Warning, "ms-kernel", 1);
1142 }
1143 if (CodeGenOpts.OptimizationLevel > 0 && CodeGenOpts.StrictVTablePointers) {
1144 // We don't support LTO with 2 with different StrictVTablePointers
1145 // FIXME: we could support it by stripping all the information introduced
1146 // by StrictVTablePointers.
1147
1148 getModule().addModuleFlag(llvm::Module::Error, "StrictVTablePointers",1);
1149
1150 llvm::Metadata *Ops[2] = {
1151 llvm::MDString::get(VMContext, "StrictVTablePointers"),
1152 llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(
1153 llvm::Type::getInt32Ty(VMContext), 1))};
1154
1155 getModule().addModuleFlag(llvm::Module::Require,
1156 "StrictVTablePointersRequirement",
1157 llvm::MDNode::get(VMContext, Ops));
1158 }
1159 if (getModuleDebugInfo() || getTriple().isOSWindows())
1160 // We support a single version in the linked module. The LLVM
1161 // parser will drop debug info with a different version number
1162 // (and warn about it, too).
1163 getModule().addModuleFlag(llvm::Module::Warning, "Debug Info Version",
1164 llvm::DEBUG_METADATA_VERSION);
1165
1166 // We need to record the widths of enums and wchar_t, so that we can generate
1167 // the correct build attributes in the ARM backend. wchar_size is also used by
1168 // TargetLibraryInfo.
1169 uint64_t WCharWidth =
1170 Context.getTypeSizeInChars(Context.getWideCharType()).getQuantity();
1171 getModule().addModuleFlag(llvm::Module::Error, "wchar_size", WCharWidth);
1172
1173 if (getTriple().isOSzOS()) {
1174 getModule().addModuleFlag(llvm::Module::Warning,
1175 "zos_product_major_version",
1176 uint32_t(CLANG_VERSION_MAJOR));
1177 getModule().addModuleFlag(llvm::Module::Warning,
1178 "zos_product_minor_version",
1179 uint32_t(CLANG_VERSION_MINOR));
1180 getModule().addModuleFlag(llvm::Module::Warning, "zos_product_patchlevel",
1181 uint32_t(CLANG_VERSION_PATCHLEVEL));
1182 std::string ProductId = getClangVendor() + "clang";
1183 getModule().addModuleFlag(llvm::Module::Error, "zos_product_id",
1184 llvm::MDString::get(VMContext, ProductId));
1185
1186 // Record the language because we need it for the PPA2.
1187 StringRef lang_str = languageToString(
1189 getModule().addModuleFlag(llvm::Module::Error, "zos_cu_language",
1190 llvm::MDString::get(VMContext, lang_str));
1191
1192 time_t TT = PreprocessorOpts.SourceDateEpoch
1193 ? *PreprocessorOpts.SourceDateEpoch
1194 : std::time(nullptr);
1195 getModule().addModuleFlag(llvm::Module::Max, "zos_translation_time",
1196 static_cast<uint64_t>(TT));
1197
1198 // Multiple modes will be supported here.
1199 getModule().addModuleFlag(llvm::Module::Error, "zos_le_char_mode",
1200 llvm::MDString::get(VMContext, "ascii"));
1201 }
1202
1203 llvm::Triple T = Context.getTargetInfo().getTriple();
1204 if (T.isARM() || T.isThumb()) {
1205 // The minimum width of an enum in bytes
1206 uint64_t EnumWidth = Context.getLangOpts().ShortEnums ? 1 : 4;
1207 getModule().addModuleFlag(llvm::Module::Error, "min_enum_size", EnumWidth);
1208 }
1209
1210 if (T.isRISCV()) {
1211 StringRef ABIStr = Target.getABI();
1212 llvm::LLVMContext &Ctx = TheModule.getContext();
1213 getModule().addModuleFlag(llvm::Module::Error, "target-abi",
1214 llvm::MDString::get(Ctx, ABIStr));
1215
1216 // Add the canonical ISA string as metadata so the backend can set the ELF
1217 // attributes correctly. We use AppendUnique so LTO will keep all of the
1218 // unique ISA strings that were linked together.
1219 const std::vector<std::string> &Features =
1221 auto ParseResult =
1222 llvm::RISCVISAInfo::parseFeatures(T.isRISCV64() ? 64 : 32, Features);
1223 if (!errorToBool(ParseResult.takeError()))
1224 getModule().addModuleFlag(
1225 llvm::Module::AppendUnique, "riscv-isa",
1226 llvm::MDNode::get(
1227 Ctx, llvm::MDString::get(Ctx, (*ParseResult)->toString())));
1228 }
1229
1230 if (CodeGenOpts.SanitizeCfiCrossDso) {
1231 // Indicate that we want cross-DSO control flow integrity checks.
1232 getModule().addModuleFlag(llvm::Module::Override, "Cross-DSO CFI", 1);
1233 }
1234
1235 if (CodeGenOpts.WholeProgramVTables) {
1236 // Indicate whether VFE was enabled for this module, so that the
1237 // vcall_visibility metadata added under whole program vtables is handled
1238 // appropriately in the optimizer.
1239 getModule().addModuleFlag(llvm::Module::Error, "Virtual Function Elim",
1240 CodeGenOpts.VirtualFunctionElimination);
1241 }
1242
1243 if (LangOpts.Sanitize.has(SanitizerKind::CFIICall)) {
1244 getModule().addModuleFlag(llvm::Module::Override,
1245 "CFI Canonical Jump Tables",
1246 CodeGenOpts.SanitizeCfiCanonicalJumpTables);
1247 }
1248
1249 if (CodeGenOpts.SanitizeCfiICallNormalizeIntegers) {
1250 getModule().addModuleFlag(llvm::Module::Override, "cfi-normalize-integers",
1251 1);
1252 }
1253
1254 if (!CodeGenOpts.UniqueSourceFileIdentifier.empty()) {
1255 getModule().addModuleFlag(
1256 llvm::Module::Append, "Unique Source File Identifier",
1257 llvm::MDTuple::get(
1258 TheModule.getContext(),
1259 llvm::MDString::get(TheModule.getContext(),
1260 CodeGenOpts.UniqueSourceFileIdentifier)));
1261 }
1262
1263 if (LangOpts.Sanitize.has(SanitizerKind::KCFI)) {
1264 getModule().addModuleFlag(llvm::Module::Override, "kcfi", 1);
1265 // KCFI assumes patchable-function-prefix is the same for all indirectly
1266 // called functions. Store the expected offset for code generation.
1267 if (CodeGenOpts.PatchableFunctionEntryOffset)
1268 getModule().addModuleFlag(llvm::Module::Override, "kcfi-offset",
1269 CodeGenOpts.PatchableFunctionEntryOffset);
1270 if (CodeGenOpts.SanitizeKcfiArity)
1271 getModule().addModuleFlag(llvm::Module::Override, "kcfi-arity", 1);
1272 }
1273
1274 if (CodeGenOpts.CFProtectionReturn &&
1275 Target.checkCFProtectionReturnSupported(getDiags())) {
1276 // Indicate that we want to instrument return control flow protection.
1277 getModule().addModuleFlag(llvm::Module::Min, "cf-protection-return",
1278 1);
1279 }
1280
1281 if (CodeGenOpts.CFProtectionBranch &&
1282 Target.checkCFProtectionBranchSupported(getDiags())) {
1283 // Indicate that we want to instrument branch control flow protection.
1284 getModule().addModuleFlag(llvm::Module::Min, "cf-protection-branch",
1285 1);
1286
1287 auto Scheme = CodeGenOpts.getCFBranchLabelScheme();
1288 if (Target.checkCFBranchLabelSchemeSupported(Scheme, getDiags())) {
1290 Scheme = Target.getDefaultCFBranchLabelScheme();
1291 getModule().addModuleFlag(
1292 llvm::Module::Error, "cf-branch-label-scheme",
1293 llvm::MDString::get(getLLVMContext(),
1295 }
1296 }
1297
1298 if (CodeGenOpts.FunctionReturnThunks)
1299 getModule().addModuleFlag(llvm::Module::Override, "function_return_thunk_extern", 1);
1300
1301 if (CodeGenOpts.IndirectBranchCSPrefix)
1302 getModule().addModuleFlag(llvm::Module::Override, "indirect_branch_cs_prefix", 1);
1303
1304 // Add module metadata for return address signing (ignoring
1305 // non-leaf/all) and stack tagging. These are actually turned on by function
1306 // attributes, but we use module metadata to emit build attributes. This is
1307 // needed for LTO, where the function attributes are inside bitcode
1308 // serialised into a global variable by the time build attributes are
1309 // emitted, so we can't access them. LTO objects could be compiled with
1310 // different flags therefore module flags are set to "Min" behavior to achieve
1311 // the same end result of the normal build where e.g BTI is off if any object
1312 // doesn't support it.
1313 if (Context.getTargetInfo().hasFeature("ptrauth") &&
1314 LangOpts.getSignReturnAddressScope() !=
1316 getModule().addModuleFlag(llvm::Module::Override,
1317 "sign-return-address-buildattr", 1);
1318 if (LangOpts.Sanitize.has(SanitizerKind::MemtagStack))
1319 getModule().addModuleFlag(llvm::Module::Override,
1320 "tag-stack-memory-buildattr", 1);
1321
1322 if (T.isARM() || T.isThumb() || T.isAArch64()) {
1323 if (LangOpts.BranchTargetEnforcement)
1324 getModule().addModuleFlag(llvm::Module::Min, "branch-target-enforcement",
1325 1);
1326 if (LangOpts.BranchProtectionPAuthLR)
1327 getModule().addModuleFlag(llvm::Module::Min, "branch-protection-pauth-lr",
1328 1);
1329 if (LangOpts.GuardedControlStack)
1330 getModule().addModuleFlag(llvm::Module::Min, "guarded-control-stack", 1);
1331 if (LangOpts.hasSignReturnAddress())
1332 getModule().addModuleFlag(llvm::Module::Min, "sign-return-address", 1);
1333 if (LangOpts.isSignReturnAddressScopeAll())
1334 getModule().addModuleFlag(llvm::Module::Min, "sign-return-address-all",
1335 1);
1336 if (!LangOpts.isSignReturnAddressWithAKey())
1337 getModule().addModuleFlag(llvm::Module::Min,
1338 "sign-return-address-with-bkey", 1);
1339
1340 if (LangOpts.PointerAuthELFGOT)
1341 getModule().addModuleFlag(llvm::Module::Min, "ptrauth-elf-got", 1);
1342
1343 if (getTriple().isOSLinux()) {
1344 if (LangOpts.PointerAuthCalls)
1345 getModule().addModuleFlag(llvm::Module::Min, "ptrauth-sign-personality",
1346 1);
1347 assert(getTriple().isOSBinFormatELF());
1348 using namespace llvm::ELF;
1349 uint64_t PAuthABIVersion =
1350 (LangOpts.PointerAuthIntrinsics
1351 << AARCH64_PAUTH_PLATFORM_LLVM_LINUX_VERSION_INTRINSICS) |
1352 (LangOpts.PointerAuthCalls
1353 << AARCH64_PAUTH_PLATFORM_LLVM_LINUX_VERSION_CALLS) |
1354 (LangOpts.PointerAuthReturns
1355 << AARCH64_PAUTH_PLATFORM_LLVM_LINUX_VERSION_RETURNS) |
1356 (LangOpts.PointerAuthAuthTraps
1357 << AARCH64_PAUTH_PLATFORM_LLVM_LINUX_VERSION_AUTHTRAPS) |
1358 (LangOpts.PointerAuthVTPtrAddressDiscrimination
1359 << AARCH64_PAUTH_PLATFORM_LLVM_LINUX_VERSION_VPTRADDRDISCR) |
1360 (LangOpts.PointerAuthVTPtrTypeDiscrimination
1361 << AARCH64_PAUTH_PLATFORM_LLVM_LINUX_VERSION_VPTRTYPEDISCR) |
1362 (LangOpts.PointerAuthInitFini
1363 << AARCH64_PAUTH_PLATFORM_LLVM_LINUX_VERSION_INITFINI) |
1364 (LangOpts.PointerAuthInitFiniAddressDiscrimination
1365 << AARCH64_PAUTH_PLATFORM_LLVM_LINUX_VERSION_INITFINIADDRDISC) |
1366 (LangOpts.PointerAuthELFGOT
1367 << AARCH64_PAUTH_PLATFORM_LLVM_LINUX_VERSION_GOT) |
1368 (LangOpts.PointerAuthIndirectGotos
1369 << AARCH64_PAUTH_PLATFORM_LLVM_LINUX_VERSION_GOTOS) |
1370 (LangOpts.PointerAuthTypeInfoVTPtrDiscrimination
1371 << AARCH64_PAUTH_PLATFORM_LLVM_LINUX_VERSION_TYPEINFOVPTRDISCR) |
1372 (LangOpts.PointerAuthFunctionTypeDiscrimination
1373 << AARCH64_PAUTH_PLATFORM_LLVM_LINUX_VERSION_FPTRTYPEDISCR);
1374 static_assert(AARCH64_PAUTH_PLATFORM_LLVM_LINUX_VERSION_FPTRTYPEDISCR ==
1375 AARCH64_PAUTH_PLATFORM_LLVM_LINUX_VERSION_LAST,
1376 "Update when new enum items are defined");
1377 if (PAuthABIVersion != 0) {
1378 getModule().addModuleFlag(llvm::Module::Error,
1379 "aarch64-elf-pauthabi-platform",
1380 AARCH64_PAUTH_PLATFORM_LLVM_LINUX);
1381 getModule().addModuleFlag(llvm::Module::Error,
1382 "aarch64-elf-pauthabi-version",
1383 PAuthABIVersion);
1384 }
1385 }
1386 }
1387
1388 if (CodeGenOpts.StackClashProtector)
1389 getModule().addModuleFlag(
1390 llvm::Module::Override, "probe-stack",
1391 llvm::MDString::get(TheModule.getContext(), "inline-asm"));
1392
1393 if (CodeGenOpts.StackProbeSize && CodeGenOpts.StackProbeSize != 4096)
1394 getModule().addModuleFlag(llvm::Module::Min, "stack-probe-size",
1395 CodeGenOpts.StackProbeSize);
1396
1397 if (!CodeGenOpts.MemoryProfileOutput.empty()) {
1398 llvm::LLVMContext &Ctx = TheModule.getContext();
1399 getModule().addModuleFlag(
1400 llvm::Module::Error, "MemProfProfileFilename",
1401 llvm::MDString::get(Ctx, CodeGenOpts.MemoryProfileOutput));
1402 }
1403
1404 if (LangOpts.CUDAIsDevice && getTriple().isNVPTX()) {
1405 // Indicate whether __nvvm_reflect should be configured to flush denormal
1406 // floating point values to 0. (This corresponds to its "__CUDA_FTZ"
1407 // property.)
1408 getModule().addModuleFlag(llvm::Module::Override, "nvvm-reflect-ftz",
1409 CodeGenOpts.FP32DenormalMode.Output !=
1410 llvm::DenormalMode::IEEE);
1411 }
1412
1413 if (LangOpts.EHAsynch)
1414 getModule().addModuleFlag(llvm::Module::Warning, "eh-asynch", 1);
1415
1416 // Emit Import Call section.
1417 if (CodeGenOpts.ImportCallOptimization)
1418 getModule().addModuleFlag(llvm::Module::Warning, "import-call-optimization",
1419 1);
1420
1421 // Enable unwind v2 (epilog).
1422 if (CodeGenOpts.getWinX64EHUnwindV2() != llvm::WinX64EHUnwindV2Mode::Disabled)
1423 getModule().addModuleFlag(
1424 llvm::Module::Warning, "winx64-eh-unwindv2",
1425 static_cast<unsigned>(CodeGenOpts.getWinX64EHUnwindV2()));
1426
1427 // Indicate whether this Module was compiled with -fopenmp
1428 if (getLangOpts().OpenMP && !getLangOpts().OpenMPSimd)
1429 getModule().addModuleFlag(llvm::Module::Max, "openmp", LangOpts.OpenMP);
1430 if (getLangOpts().OpenMPIsTargetDevice)
1431 getModule().addModuleFlag(llvm::Module::Max, "openmp-device",
1432 LangOpts.OpenMP);
1433
1434 // Emit OpenCL specific module metadata: OpenCL/SPIR version.
1435 if (LangOpts.OpenCL || (LangOpts.CUDAIsDevice && getTriple().isSPIRV())) {
1436 EmitOpenCLMetadata();
1437 // Emit SPIR version.
1438 if (getTriple().isSPIR()) {
1439 // SPIR v2.0 s2.12 - The SPIR version used by the module is stored in the
1440 // opencl.spir.version named metadata.
1441 // C++ for OpenCL has a distinct mapping for version compatibility with
1442 // OpenCL.
1443 auto Version = LangOpts.getOpenCLCompatibleVersion();
1444 llvm::Metadata *SPIRVerElts[] = {
1445 llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(
1446 Int32Ty, Version / 100)),
1447 llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(
1448 Int32Ty, (Version / 100 > 1) ? 0 : 2))};
1449 llvm::NamedMDNode *SPIRVerMD =
1450 TheModule.getOrInsertNamedMetadata("opencl.spir.version");
1451 llvm::LLVMContext &Ctx = TheModule.getContext();
1452 SPIRVerMD->addOperand(llvm::MDNode::get(Ctx, SPIRVerElts));
1453 }
1454 }
1455
1456 // HLSL related end of code gen work items.
1457 if (LangOpts.HLSL)
1459
1460 if (uint32_t PLevel = Context.getLangOpts().PICLevel) {
1461 assert(PLevel < 3 && "Invalid PIC Level");
1462 getModule().setPICLevel(static_cast<llvm::PICLevel::Level>(PLevel));
1463 if (Context.getLangOpts().PIE)
1464 getModule().setPIELevel(static_cast<llvm::PIELevel::Level>(PLevel));
1465 }
1466
1467 if (getCodeGenOpts().CodeModel.size() > 0) {
1468 unsigned CM = llvm::StringSwitch<unsigned>(getCodeGenOpts().CodeModel)
1469 .Case("tiny", llvm::CodeModel::Tiny)
1470 .Case("small", llvm::CodeModel::Small)
1471 .Case("kernel", llvm::CodeModel::Kernel)
1472 .Case("medium", llvm::CodeModel::Medium)
1473 .Case("large", llvm::CodeModel::Large)
1474 .Default(~0u);
1475 if (CM != ~0u) {
1476 llvm::CodeModel::Model codeModel = static_cast<llvm::CodeModel::Model>(CM);
1477 getModule().setCodeModel(codeModel);
1478
1479 if ((CM == llvm::CodeModel::Medium || CM == llvm::CodeModel::Large) &&
1480 Context.getTargetInfo().getTriple().getArch() ==
1481 llvm::Triple::x86_64) {
1482 getModule().setLargeDataThreshold(getCodeGenOpts().LargeDataThreshold);
1483 }
1484 }
1485 }
1486
1487 if (CodeGenOpts.NoPLT)
1488 getModule().setRtLibUseGOT();
1489 if (getTriple().isOSBinFormatELF() &&
1490 CodeGenOpts.DirectAccessExternalData !=
1491 getModule().getDirectAccessExternalData()) {
1492 getModule().setDirectAccessExternalData(
1493 CodeGenOpts.DirectAccessExternalData);
1494 }
1495 if (CodeGenOpts.UnwindTables)
1496 getModule().setUwtable(llvm::UWTableKind(CodeGenOpts.UnwindTables));
1497
1498 switch (CodeGenOpts.getFramePointer()) {
1500 // 0 ("none") is the default.
1501 break;
1503 getModule().setFramePointer(llvm::FramePointerKind::Reserved);
1504 break;
1506 getModule().setFramePointer(llvm::FramePointerKind::NonLeaf);
1507 break;
1509 getModule().setFramePointer(llvm::FramePointerKind::All);
1510 break;
1511 }
1512
1513 SimplifyPersonality();
1514
1515 if (getCodeGenOpts().EmitDeclMetadata)
1516 EmitDeclMetadata();
1517
1518 if (getCodeGenOpts().CoverageNotesFile.size() ||
1519 getCodeGenOpts().CoverageDataFile.size())
1520 EmitCoverageFile();
1521
1522 if (CGDebugInfo *DI = getModuleDebugInfo())
1523 DI->finalize();
1524
1525 if (getCodeGenOpts().EmitVersionIdentMetadata)
1526 EmitVersionIdentMetadata();
1527
1528 if (!getCodeGenOpts().RecordCommandLine.empty())
1529 EmitCommandLineMetadata();
1530
1531 if (!getCodeGenOpts().StackProtectorGuard.empty())
1532 getModule().setStackProtectorGuard(getCodeGenOpts().StackProtectorGuard);
1533 if (!getCodeGenOpts().StackProtectorGuardReg.empty())
1534 getModule().setStackProtectorGuardReg(
1535 getCodeGenOpts().StackProtectorGuardReg);
1536 if (!getCodeGenOpts().StackProtectorGuardSymbol.empty())
1537 getModule().setStackProtectorGuardSymbol(
1538 getCodeGenOpts().StackProtectorGuardSymbol);
1539 if (getCodeGenOpts().StackProtectorGuardOffset != INT_MAX)
1540 getModule().setStackProtectorGuardOffset(
1541 getCodeGenOpts().StackProtectorGuardOffset);
1542 if (getCodeGenOpts().StackAlignment)
1543 getModule().setOverrideStackAlignment(getCodeGenOpts().StackAlignment);
1544 if (getCodeGenOpts().SkipRaxSetup)
1545 getModule().addModuleFlag(llvm::Module::Override, "SkipRaxSetup", 1);
1546 if (getLangOpts().RegCall4)
1547 getModule().addModuleFlag(llvm::Module::Override, "RegCallv4", 1);
1548
1549 if (getContext().getTargetInfo().getMaxTLSAlign())
1550 getModule().addModuleFlag(llvm::Module::Error, "MaxTLSAlign",
1551 getContext().getTargetInfo().getMaxTLSAlign());
1552
1554
1555 getTargetCodeGenInfo().emitTargetMetadata(*this, MangledDeclNames);
1556
1557 EmitBackendOptionsMetadata(getCodeGenOpts());
1558
1559 // If there is device offloading code embed it in the host now.
1560 EmbedObject(&getModule(), CodeGenOpts, getDiags());
1561
1562 // Set visibility from DLL storage class
1563 // We do this at the end of LLVM IR generation; after any operation
1564 // that might affect the DLL storage class or the visibility, and
1565 // before anything that might act on these.
1567
1568 // Check the tail call symbols are truly undefined.
1569 if (getTriple().isPPC() && !MustTailCallUndefinedGlobals.empty()) {
1570 for (auto &I : MustTailCallUndefinedGlobals) {
1571 if (!I.first->isDefined())
1572 getDiags().Report(I.second, diag::err_ppc_impossible_musttail) << 2;
1573 else {
1574 StringRef MangledName = getMangledName(GlobalDecl(I.first));
1575 llvm::GlobalValue *Entry = GetGlobalValue(MangledName);
1576 if (!Entry || Entry->isWeakForLinker() ||
1577 Entry->isDeclarationForLinker())
1578 getDiags().Report(I.second, diag::err_ppc_impossible_musttail) << 2;
1579 }
1580 }
1581 }
1582}
1583
1584void CodeGenModule::EmitOpenCLMetadata() {
1585 // SPIR v2.0 s2.13 - The OpenCL version used by the module is stored in the
1586 // opencl.ocl.version named metadata node.
1587 // C++ for OpenCL has a distinct mapping for versions compatible with OpenCL.
1588 auto CLVersion = LangOpts.getOpenCLCompatibleVersion();
1589
1590 auto EmitVersion = [this](StringRef MDName, int Version) {
1591 llvm::Metadata *OCLVerElts[] = {
1592 llvm::ConstantAsMetadata::get(
1593 llvm::ConstantInt::get(Int32Ty, Version / 100)),
1594 llvm::ConstantAsMetadata::get(
1595 llvm::ConstantInt::get(Int32Ty, (Version % 100) / 10))};
1596 llvm::NamedMDNode *OCLVerMD = TheModule.getOrInsertNamedMetadata(MDName);
1597 llvm::LLVMContext &Ctx = TheModule.getContext();
1598 OCLVerMD->addOperand(llvm::MDNode::get(Ctx, OCLVerElts));
1599 };
1600
1601 EmitVersion("opencl.ocl.version", CLVersion);
1602 if (LangOpts.OpenCLCPlusPlus) {
1603 // In addition to the OpenCL compatible version, emit the C++ version.
1604 EmitVersion("opencl.cxx.version", LangOpts.OpenCLCPlusPlusVersion);
1605 }
1606}
1607
1608void CodeGenModule::EmitBackendOptionsMetadata(
1609 const CodeGenOptions &CodeGenOpts) {
1610 if (getTriple().isRISCV()) {
1611 getModule().addModuleFlag(llvm::Module::Min, "SmallDataLimit",
1612 CodeGenOpts.SmallDataLimit);
1613 }
1614}
1615
1617 // Make sure that this type is translated.
1619}
1620
1622 // Make sure that this type is translated.
1624}
1625
1627 if (!TBAA)
1628 return nullptr;
1629 return TBAA->getTypeInfo(QTy);
1630}
1631
1633 if (!TBAA)
1634 return TBAAAccessInfo();
1635 if (getLangOpts().CUDAIsDevice) {
1636 // As CUDA builtin surface/texture types are replaced, skip generating TBAA
1637 // access info.
1638 if (AccessType->isCUDADeviceBuiltinSurfaceType()) {
1639 if (getTargetCodeGenInfo().getCUDADeviceBuiltinSurfaceDeviceType() !=
1640 nullptr)
1641 return TBAAAccessInfo();
1642 } else if (AccessType->isCUDADeviceBuiltinTextureType()) {
1643 if (getTargetCodeGenInfo().getCUDADeviceBuiltinTextureDeviceType() !=
1644 nullptr)
1645 return TBAAAccessInfo();
1646 }
1647 }
1648 return TBAA->getAccessInfo(AccessType);
1649}
1650
1653 if (!TBAA)
1654 return TBAAAccessInfo();
1655 return TBAA->getVTablePtrAccessInfo(VTablePtrType);
1656}
1657
1659 if (!TBAA)
1660 return nullptr;
1661 return TBAA->getTBAAStructInfo(QTy);
1662}
1663
1665 if (!TBAA)
1666 return nullptr;
1667 return TBAA->getBaseTypeInfo(QTy);
1668}
1669
1671 if (!TBAA)
1672 return nullptr;
1673 return TBAA->getAccessTagInfo(Info);
1674}
1675
1678 if (!TBAA)
1679 return TBAAAccessInfo();
1680 return TBAA->mergeTBAAInfoForCast(SourceInfo, TargetInfo);
1681}
1682
1685 TBAAAccessInfo InfoB) {
1686 if (!TBAA)
1687 return TBAAAccessInfo();
1688 return TBAA->mergeTBAAInfoForConditionalOperator(InfoA, InfoB);
1689}
1690
1693 TBAAAccessInfo SrcInfo) {
1694 if (!TBAA)
1695 return TBAAAccessInfo();
1696 return TBAA->mergeTBAAInfoForConditionalOperator(DestInfo, SrcInfo);
1697}
1698
1700 TBAAAccessInfo TBAAInfo) {
1701 if (llvm::MDNode *Tag = getTBAAAccessTagInfo(TBAAInfo))
1702 Inst->setMetadata(llvm::LLVMContext::MD_tbaa, Tag);
1703}
1704
1706 llvm::Instruction *I, const CXXRecordDecl *RD) {
1707 I->setMetadata(llvm::LLVMContext::MD_invariant_group,
1708 llvm::MDNode::get(getLLVMContext(), {}));
1709}
1710
1711void CodeGenModule::Error(SourceLocation loc, StringRef message) {
1712 unsigned diagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error, "%0");
1713 getDiags().Report(Context.getFullLoc(loc), diagID) << message;
1714}
1715
1716/// ErrorUnsupported - Print out an error that codegen doesn't support the
1717/// specified stmt yet.
1718void CodeGenModule::ErrorUnsupported(const Stmt *S, const char *Type) {
1720 "cannot compile this %0 yet");
1721 std::string Msg = Type;
1722 getDiags().Report(Context.getFullLoc(S->getBeginLoc()), DiagID)
1723 << Msg << S->getSourceRange();
1724}
1725
1726/// ErrorUnsupported - Print out an error that codegen doesn't support the
1727/// specified decl yet.
1728void CodeGenModule::ErrorUnsupported(const Decl *D, const char *Type) {
1730 "cannot compile this %0 yet");
1731 std::string Msg = Type;
1732 getDiags().Report(Context.getFullLoc(D->getLocation()), DiagID) << Msg;
1733}
1734
1736 llvm::function_ref<void()> Fn) {
1737 StackHandler.runWithSufficientStackSpace(Loc, Fn);
1738}
1739
1740llvm::ConstantInt *CodeGenModule::getSize(CharUnits size) {
1741 return llvm::ConstantInt::get(SizeTy, size.getQuantity());
1742}
1743
1744void CodeGenModule::setGlobalVisibility(llvm::GlobalValue *GV,
1745 const NamedDecl *D) const {
1746 // Internal definitions always have default visibility.
1747 if (GV->hasLocalLinkage()) {
1748 GV->setVisibility(llvm::GlobalValue::DefaultVisibility);
1749 return;
1750 }
1751 if (!D)
1752 return;
1753
1754 // Set visibility for definitions, and for declarations if requested globally
1755 // or set explicitly.
1756 LinkageInfo LV = D->getLinkageAndVisibility();
1757
1758 // OpenMP declare target variables must be visible to the host so they can
1759 // be registered. We require protected visibility unless the variable has
1760 // the DT_nohost modifier and does not need to be registered.
1761 if (Context.getLangOpts().OpenMP &&
1762 Context.getLangOpts().OpenMPIsTargetDevice && isa<VarDecl>(D) &&
1763 D->hasAttr<OMPDeclareTargetDeclAttr>() &&
1764 D->getAttr<OMPDeclareTargetDeclAttr>()->getDevType() !=
1765 OMPDeclareTargetDeclAttr::DT_NoHost &&
1767 GV->setVisibility(llvm::GlobalValue::ProtectedVisibility);
1768 return;
1769 }
1770
1771 if (Context.getLangOpts().HLSL && !D->isInExportDeclContext()) {
1772 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
1773 return;
1774 }
1775
1776 if (GV->hasDLLExportStorageClass() || GV->hasDLLImportStorageClass()) {
1777 // Reject incompatible dlllstorage and visibility annotations.
1778 if (!LV.isVisibilityExplicit())
1779 return;
1780 if (GV->hasDLLExportStorageClass()) {
1781 if (LV.getVisibility() == HiddenVisibility)
1783 diag::err_hidden_visibility_dllexport);
1784 } else if (LV.getVisibility() != DefaultVisibility) {
1786 diag::err_non_default_visibility_dllimport);
1787 }
1788 return;
1789 }
1790
1791 if (LV.isVisibilityExplicit() || getLangOpts().SetVisibilityForExternDecls ||
1792 !GV->isDeclarationForLinker())
1793 GV->setVisibility(GetLLVMVisibility(LV.getVisibility()));
1794}
1795
1797 llvm::GlobalValue *GV) {
1798 if (GV->hasLocalLinkage())
1799 return true;
1800
1801 if (!GV->hasDefaultVisibility() && !GV->hasExternalWeakLinkage())
1802 return true;
1803
1804 // DLLImport explicitly marks the GV as external.
1805 if (GV->hasDLLImportStorageClass())
1806 return false;
1807
1808 const llvm::Triple &TT = CGM.getTriple();
1809 const auto &CGOpts = CGM.getCodeGenOpts();
1810 if (TT.isOSCygMing()) {
1811 // In MinGW, variables without DLLImport can still be automatically
1812 // imported from a DLL by the linker; don't mark variables that
1813 // potentially could come from another DLL as DSO local.
1814
1815 // With EmulatedTLS, TLS variables can be autoimported from other DLLs
1816 // (and this actually happens in the public interface of libstdc++), so
1817 // such variables can't be marked as DSO local. (Native TLS variables
1818 // can't be dllimported at all, though.)
1819 if (GV->isDeclarationForLinker() && isa<llvm::GlobalVariable>(GV) &&
1820 (!GV->isThreadLocal() || CGM.getCodeGenOpts().EmulatedTLS) &&
1821 CGOpts.AutoImport)
1822 return false;
1823 }
1824
1825 // On COFF, don't mark 'extern_weak' symbols as DSO local. If these symbols
1826 // remain unresolved in the link, they can be resolved to zero, which is
1827 // outside the current DSO.
1828 if (TT.isOSBinFormatCOFF() && GV->hasExternalWeakLinkage())
1829 return false;
1830
1831 // Every other GV is local on COFF.
1832 // Make an exception for windows OS in the triple: Some firmware builds use
1833 // *-win32-macho triples. This (accidentally?) produced windows relocations
1834 // without GOT tables in older clang versions; Keep this behaviour.
1835 // FIXME: even thread local variables?
1836 if (TT.isOSBinFormatCOFF() || (TT.isOSWindows() && TT.isOSBinFormatMachO()))
1837 return true;
1838
1839 // Only handle COFF and ELF for now.
1840 if (!TT.isOSBinFormatELF())
1841 return false;
1842
1843 // If this is not an executable, don't assume anything is local.
1844 llvm::Reloc::Model RM = CGOpts.RelocationModel;
1845 const auto &LOpts = CGM.getLangOpts();
1846 if (RM != llvm::Reloc::Static && !LOpts.PIE) {
1847 // On ELF, if -fno-semantic-interposition is specified and the target
1848 // supports local aliases, there will be neither CC1
1849 // -fsemantic-interposition nor -fhalf-no-semantic-interposition. Set
1850 // dso_local on the function if using a local alias is preferable (can avoid
1851 // PLT indirection).
1852 if (!(isa<llvm::Function>(GV) && GV->canBenefitFromLocalAlias()))
1853 return false;
1854 return !(CGM.getLangOpts().SemanticInterposition ||
1855 CGM.getLangOpts().HalfNoSemanticInterposition);
1856 }
1857
1858 // A definition cannot be preempted from an executable.
1859 if (!GV->isDeclarationForLinker())
1860 return true;
1861
1862 // Most PIC code sequences that assume that a symbol is local cannot produce a
1863 // 0 if it turns out the symbol is undefined. While this is ABI and relocation
1864 // depended, it seems worth it to handle it here.
1865 if (RM == llvm::Reloc::PIC_ && GV->hasExternalWeakLinkage())
1866 return false;
1867
1868 // PowerPC64 prefers TOC indirection to avoid copy relocations.
1869 if (TT.isPPC64())
1870 return false;
1871
1872 if (CGOpts.DirectAccessExternalData) {
1873 // If -fdirect-access-external-data (default for -fno-pic), set dso_local
1874 // for non-thread-local variables. If the symbol is not defined in the
1875 // executable, a copy relocation will be needed at link time. dso_local is
1876 // excluded for thread-local variables because they generally don't support
1877 // copy relocations.
1878 if (auto *Var = dyn_cast<llvm::GlobalVariable>(GV))
1879 if (!Var->isThreadLocal())
1880 return true;
1881
1882 // -fno-pic sets dso_local on a function declaration to allow direct
1883 // accesses when taking its address (similar to a data symbol). If the
1884 // function is not defined in the executable, a canonical PLT entry will be
1885 // needed at link time. -fno-direct-access-external-data can avoid the
1886 // canonical PLT entry. We don't generalize this condition to -fpie/-fpic as
1887 // it could just cause trouble without providing perceptible benefits.
1888 if (isa<llvm::Function>(GV) && !CGOpts.NoPLT && RM == llvm::Reloc::Static)
1889 return true;
1890 }
1891
1892 // If we can use copy relocations we can assume it is local.
1893
1894 // Otherwise don't assume it is local.
1895 return false;
1896}
1897
1898void CodeGenModule::setDSOLocal(llvm::GlobalValue *GV) const {
1899 GV->setDSOLocal(shouldAssumeDSOLocal(*this, GV));
1900}
1901
1902void CodeGenModule::setDLLImportDLLExport(llvm::GlobalValue *GV,
1903 GlobalDecl GD) const {
1904 const auto *D = dyn_cast<NamedDecl>(GD.getDecl());
1905 // C++ destructors have a few C++ ABI specific special cases.
1906 if (const auto *Dtor = dyn_cast_or_null<CXXDestructorDecl>(D)) {
1908 return;
1909 }
1911}
1912
1913void CodeGenModule::setDLLImportDLLExport(llvm::GlobalValue *GV,
1914 const NamedDecl *D) const {
1915 if (D && D->isExternallyVisible()) {
1916 if (D->hasAttr<DLLImportAttr>())
1917 GV->setDLLStorageClass(llvm::GlobalVariable::DLLImportStorageClass);
1918 else if ((D->hasAttr<DLLExportAttr>() ||
1920 !GV->isDeclarationForLinker())
1921 GV->setDLLStorageClass(llvm::GlobalVariable::DLLExportStorageClass);
1922 }
1923}
1924
1925void CodeGenModule::setGVProperties(llvm::GlobalValue *GV,
1926 GlobalDecl GD) const {
1927 setDLLImportDLLExport(GV, GD);
1928 setGVPropertiesAux(GV, dyn_cast<NamedDecl>(GD.getDecl()));
1929}
1930
1931void CodeGenModule::setGVProperties(llvm::GlobalValue *GV,
1932 const NamedDecl *D) const {
1934 setGVPropertiesAux(GV, D);
1935}
1936
1937void CodeGenModule::setGVPropertiesAux(llvm::GlobalValue *GV,
1938 const NamedDecl *D) const {
1940 setDSOLocal(GV);
1941 GV->setPartition(CodeGenOpts.SymbolPartition);
1942}
1943
1944static llvm::GlobalVariable::ThreadLocalMode GetLLVMTLSModel(StringRef S) {
1945 return llvm::StringSwitch<llvm::GlobalVariable::ThreadLocalMode>(S)
1946 .Case("global-dynamic", llvm::GlobalVariable::GeneralDynamicTLSModel)
1947 .Case("local-dynamic", llvm::GlobalVariable::LocalDynamicTLSModel)
1948 .Case("initial-exec", llvm::GlobalVariable::InitialExecTLSModel)
1949 .Case("local-exec", llvm::GlobalVariable::LocalExecTLSModel);
1950}
1951
1952llvm::GlobalVariable::ThreadLocalMode
1954 switch (CodeGenOpts.getDefaultTLSModel()) {
1956 return llvm::GlobalVariable::GeneralDynamicTLSModel;
1958 return llvm::GlobalVariable::LocalDynamicTLSModel;
1960 return llvm::GlobalVariable::InitialExecTLSModel;
1962 return llvm::GlobalVariable::LocalExecTLSModel;
1963 }
1964 llvm_unreachable("Invalid TLS model!");
1965}
1966
1967void CodeGenModule::setTLSMode(llvm::GlobalValue *GV, const VarDecl &D) const {
1968 assert(D.getTLSKind() && "setting TLS mode on non-TLS var!");
1969
1970 llvm::GlobalValue::ThreadLocalMode TLM;
1971 TLM = GetDefaultLLVMTLSModel();
1972
1973 // Override the TLS model if it is explicitly specified.
1974 if (const TLSModelAttr *Attr = D.getAttr<TLSModelAttr>()) {
1975 TLM = GetLLVMTLSModel(Attr->getModel());
1976 }
1977
1978 GV->setThreadLocalMode(TLM);
1979}
1980
1981static std::string getCPUSpecificMangling(const CodeGenModule &CGM,
1982 StringRef Name) {
1983 const TargetInfo &Target = CGM.getTarget();
1984 return (Twine('.') + Twine(Target.CPUSpecificManglingCharacter(Name))).str();
1985}
1986
1988 const CPUSpecificAttr *Attr,
1989 unsigned CPUIndex,
1990 raw_ostream &Out) {
1991 // cpu_specific gets the current name, dispatch gets the resolver if IFunc is
1992 // supported.
1993 if (Attr)
1994 Out << getCPUSpecificMangling(CGM, Attr->getCPUName(CPUIndex)->getName());
1995 else if (CGM.getTarget().supportsIFunc())
1996 Out << ".resolver";
1997}
1998
1999// Returns true if GD is a function decl with internal linkage and
2000// needs a unique suffix after the mangled name.
2002 CodeGenModule &CGM) {
2003 const Decl *D = GD.getDecl();
2004 return !CGM.getModuleNameHash().empty() && isa<FunctionDecl>(D) &&
2005 (CGM.getFunctionLinkage(GD) == llvm::GlobalValue::InternalLinkage);
2006}
2007
2008static std::string getMangledNameImpl(CodeGenModule &CGM, GlobalDecl GD,
2009 const NamedDecl *ND,
2010 bool OmitMultiVersionMangling = false) {
2011 SmallString<256> Buffer;
2012 llvm::raw_svector_ostream Out(Buffer);
2014 if (!CGM.getModuleNameHash().empty())
2016 bool ShouldMangle = MC.shouldMangleDeclName(ND);
2017 if (ShouldMangle)
2018 MC.mangleName(GD.getWithDecl(ND), Out);
2019 else {
2020 IdentifierInfo *II = ND->getIdentifier();
2021 assert(II && "Attempt to mangle unnamed decl.");
2022 const auto *FD = dyn_cast<FunctionDecl>(ND);
2023
2024 if (FD &&
2025 FD->getType()->castAs<FunctionType>()->getCallConv() == CC_X86RegCall) {
2026 if (CGM.getLangOpts().RegCall4)
2027 Out << "__regcall4__" << II->getName();
2028 else
2029 Out << "__regcall3__" << II->getName();
2030 } else if (FD && FD->hasAttr<CUDAGlobalAttr>() &&
2032 Out << "__device_stub__" << II->getName();
2033 } else if (FD &&
2034 DeviceKernelAttr::isOpenCLSpelling(
2035 FD->getAttr<DeviceKernelAttr>()) &&
2037 Out << "__clang_ocl_kern_imp_" << II->getName();
2038 } else {
2039 Out << II->getName();
2040 }
2041 }
2042
2043 // Check if the module name hash should be appended for internal linkage
2044 // symbols. This should come before multi-version target suffixes are
2045 // appended. This is to keep the name and module hash suffix of the
2046 // internal linkage function together. The unique suffix should only be
2047 // added when name mangling is done to make sure that the final name can
2048 // be properly demangled. For example, for C functions without prototypes,
2049 // name mangling is not done and the unique suffix should not be appeneded
2050 // then.
2051 if (ShouldMangle && isUniqueInternalLinkageDecl(GD, CGM)) {
2052 assert(CGM.getCodeGenOpts().UniqueInternalLinkageNames &&
2053 "Hash computed when not explicitly requested");
2054 Out << CGM.getModuleNameHash();
2055 }
2056
2057 if (const auto *FD = dyn_cast<FunctionDecl>(ND))
2058 if (FD->isMultiVersion() && !OmitMultiVersionMangling) {
2059 switch (FD->getMultiVersionKind()) {
2063 FD->getAttr<CPUSpecificAttr>(),
2064 GD.getMultiVersionIndex(), Out);
2065 break;
2067 auto *Attr = FD->getAttr<TargetAttr>();
2068 assert(Attr && "Expected TargetAttr to be present "
2069 "for attribute mangling");
2070 const ABIInfo &Info = CGM.getTargetCodeGenInfo().getABIInfo();
2071 Info.appendAttributeMangling(Attr, Out);
2072 break;
2073 }
2075 auto *Attr = FD->getAttr<TargetVersionAttr>();
2076 assert(Attr && "Expected TargetVersionAttr to be present "
2077 "for attribute mangling");
2078 const ABIInfo &Info = CGM.getTargetCodeGenInfo().getABIInfo();
2079 Info.appendAttributeMangling(Attr, Out);
2080 break;
2081 }
2083 auto *Attr = FD->getAttr<TargetClonesAttr>();
2084 assert(Attr && "Expected TargetClonesAttr to be present "
2085 "for attribute mangling");
2086 unsigned Index = GD.getMultiVersionIndex();
2087 const ABIInfo &Info = CGM.getTargetCodeGenInfo().getABIInfo();
2088 Info.appendAttributeMangling(Attr, Index, Out);
2089 break;
2090 }
2092 llvm_unreachable("None multiversion type isn't valid here");
2093 }
2094 }
2095
2096 // Make unique name for device side static file-scope variable for HIP.
2097 if (CGM.getContext().shouldExternalize(ND) &&
2098 CGM.getLangOpts().GPURelocatableDeviceCode &&
2099 CGM.getLangOpts().CUDAIsDevice)
2101
2102 return std::string(Out.str());
2103}
2104
2105void CodeGenModule::UpdateMultiVersionNames(GlobalDecl GD,
2106 const FunctionDecl *FD,
2107 StringRef &CurName) {
2108 if (!FD->isMultiVersion())
2109 return;
2110
2111 // Get the name of what this would be without the 'target' attribute. This
2112 // allows us to lookup the version that was emitted when this wasn't a
2113 // multiversion function.
2114 std::string NonTargetName =
2115 getMangledNameImpl(*this, GD, FD, /*OmitMultiVersionMangling=*/true);
2116 GlobalDecl OtherGD;
2117 if (lookupRepresentativeDecl(NonTargetName, OtherGD)) {
2118 assert(OtherGD.getCanonicalDecl()
2119 .getDecl()
2120 ->getAsFunction()
2121 ->isMultiVersion() &&
2122 "Other GD should now be a multiversioned function");
2123 // OtherFD is the version of this function that was mangled BEFORE
2124 // becoming a MultiVersion function. It potentially needs to be updated.
2125 const FunctionDecl *OtherFD = OtherGD.getCanonicalDecl()
2126 .getDecl()
2127 ->getAsFunction()
2129 std::string OtherName = getMangledNameImpl(*this, OtherGD, OtherFD);
2130 // This is so that if the initial version was already the 'default'
2131 // version, we don't try to update it.
2132 if (OtherName != NonTargetName) {
2133 // Remove instead of erase, since others may have stored the StringRef
2134 // to this.
2135 const auto ExistingRecord = Manglings.find(NonTargetName);
2136 if (ExistingRecord != std::end(Manglings))
2137 Manglings.remove(&(*ExistingRecord));
2138 auto Result = Manglings.insert(std::make_pair(OtherName, OtherGD));
2139 StringRef OtherNameRef = MangledDeclNames[OtherGD.getCanonicalDecl()] =
2140 Result.first->first();
2141 // If this is the current decl is being created, make sure we update the name.
2142 if (GD.getCanonicalDecl() == OtherGD.getCanonicalDecl())
2143 CurName = OtherNameRef;
2144 if (llvm::GlobalValue *Entry = GetGlobalValue(NonTargetName))
2145 Entry->setName(OtherName);
2146 }
2147 }
2148}
2149
2151 GlobalDecl CanonicalGD = GD.getCanonicalDecl();
2152
2153 // Some ABIs don't have constructor variants. Make sure that base and
2154 // complete constructors get mangled the same.
2155 if (const auto *CD = dyn_cast<CXXConstructorDecl>(CanonicalGD.getDecl())) {
2156 if (!getTarget().getCXXABI().hasConstructorVariants()) {
2157 CXXCtorType OrigCtorType = GD.getCtorType();
2158 assert(OrigCtorType == Ctor_Base || OrigCtorType == Ctor_Complete);
2159 if (OrigCtorType == Ctor_Base)
2160 CanonicalGD = GlobalDecl(CD, Ctor_Complete);
2161 }
2162 }
2163
2164 // In CUDA/HIP device compilation with -fgpu-rdc, the mangled name of a
2165 // static device variable depends on whether the variable is referenced by
2166 // a host or device host function. Therefore the mangled name cannot be
2167 // cached.
2168 if (!LangOpts.CUDAIsDevice || !getContext().mayExternalize(GD.getDecl())) {
2169 auto FoundName = MangledDeclNames.find(CanonicalGD);
2170 if (FoundName != MangledDeclNames.end())
2171 return FoundName->second;
2172 }
2173
2174 // Keep the first result in the case of a mangling collision.
2175 const auto *ND = cast<NamedDecl>(GD.getDecl());
2176 std::string MangledName = getMangledNameImpl(*this, GD, ND);
2177
2178 // Ensure either we have different ABIs between host and device compilations,
2179 // says host compilation following MSVC ABI but device compilation follows
2180 // Itanium C++ ABI or, if they follow the same ABI, kernel names after
2181 // mangling should be the same after name stubbing. The later checking is
2182 // very important as the device kernel name being mangled in host-compilation
2183 // is used to resolve the device binaries to be executed. Inconsistent naming
2184 // result in undefined behavior. Even though we cannot check that naming
2185 // directly between host- and device-compilations, the host- and
2186 // device-mangling in host compilation could help catching certain ones.
2187 assert(!isa<FunctionDecl>(ND) || !ND->hasAttr<CUDAGlobalAttr>() ||
2188 getContext().shouldExternalize(ND) || getLangOpts().CUDAIsDevice ||
2189 (getContext().getAuxTargetInfo() &&
2190 (getContext().getAuxTargetInfo()->getCXXABI() !=
2191 getContext().getTargetInfo().getCXXABI())) ||
2192 getCUDARuntime().getDeviceSideName(ND) ==
2194 *this,
2196 ND));
2197
2198 // This invariant should hold true in the future.
2199 // Prior work:
2200 // https://discourse.llvm.org/t/rfc-clang-diagnostic-for-demangling-failures/82835/8
2201 // https://github.com/llvm/llvm-project/issues/111345
2202 // assert(!((StringRef(MangledName).starts_with("_Z") ||
2203 // StringRef(MangledName).starts_with("?")) &&
2204 // !GD.getDecl()->hasAttr<AsmLabelAttr>() &&
2205 // llvm::demangle(MangledName) == MangledName) &&
2206 // "LLVM demangler must demangle clang-generated names");
2207
2208 auto Result = Manglings.insert(std::make_pair(MangledName, GD));
2209 return MangledDeclNames[CanonicalGD] = Result.first->first();
2210}
2211
2213 const BlockDecl *BD) {
2214 MangleContext &MangleCtx = getCXXABI().getMangleContext();
2215 const Decl *D = GD.getDecl();
2216
2217 SmallString<256> Buffer;
2218 llvm::raw_svector_ostream Out(Buffer);
2219 if (!D)
2220 MangleCtx.mangleGlobalBlock(BD,
2221 dyn_cast_or_null<VarDecl>(initializedGlobalDecl.getDecl()), Out);
2222 else if (const auto *CD = dyn_cast<CXXConstructorDecl>(D))
2223 MangleCtx.mangleCtorBlock(CD, GD.getCtorType(), BD, Out);
2224 else if (const auto *DD = dyn_cast<CXXDestructorDecl>(D))
2225 MangleCtx.mangleDtorBlock(DD, GD.getDtorType(), BD, Out);
2226 else
2227 MangleCtx.mangleBlock(cast<DeclContext>(D), BD, Out);
2228
2229 auto Result = Manglings.insert(std::make_pair(Out.str(), BD));
2230 return Result.first->first();
2231}
2232
2234 auto it = MangledDeclNames.begin();
2235 while (it != MangledDeclNames.end()) {
2236 if (it->second == Name)
2237 return it->first;
2238 it++;
2239 }
2240 return GlobalDecl();
2241}
2242
2243llvm::GlobalValue *CodeGenModule::GetGlobalValue(StringRef Name) {
2244 return getModule().getNamedValue(Name);
2245}
2246
2247/// AddGlobalCtor - Add a function to the list that will be called before
2248/// main() runs.
2249void CodeGenModule::AddGlobalCtor(llvm::Function *Ctor, int Priority,
2250 unsigned LexOrder,
2251 llvm::Constant *AssociatedData) {
2252 // FIXME: Type coercion of void()* types.
2253 GlobalCtors.push_back(Structor(Priority, LexOrder, Ctor, AssociatedData));
2254}
2255
2256/// AddGlobalDtor - Add a function to the list that will be called
2257/// when the module is unloaded.
2258void CodeGenModule::AddGlobalDtor(llvm::Function *Dtor, int Priority,
2259 bool IsDtorAttrFunc) {
2260 if (CodeGenOpts.RegisterGlobalDtorsWithAtExit &&
2261 (!getContext().getTargetInfo().getTriple().isOSAIX() || IsDtorAttrFunc)) {
2262 DtorsUsingAtExit[Priority].push_back(Dtor);
2263 return;
2264 }
2265
2266 // FIXME: Type coercion of void()* types.
2267 GlobalDtors.push_back(Structor(Priority, ~0U, Dtor, nullptr));
2268}
2269
2270void CodeGenModule::EmitCtorList(CtorList &Fns, const char *GlobalName) {
2271 if (Fns.empty()) return;
2272
2273 const PointerAuthSchema &InitFiniAuthSchema =
2275
2276 // Ctor function type is ptr.
2277 llvm::PointerType *PtrTy = llvm::PointerType::get(
2278 getLLVMContext(), TheModule.getDataLayout().getProgramAddressSpace());
2279
2280 // Get the type of a ctor entry, { i32, ptr, ptr }.
2281 llvm::StructType *CtorStructTy = llvm::StructType::get(Int32Ty, PtrTy, PtrTy);
2282
2283 // Construct the constructor and destructor arrays.
2284 ConstantInitBuilder Builder(*this);
2285 auto Ctors = Builder.beginArray(CtorStructTy);
2286 for (const auto &I : Fns) {
2287 auto Ctor = Ctors.beginStruct(CtorStructTy);
2288 Ctor.addInt(Int32Ty, I.Priority);
2289 if (InitFiniAuthSchema) {
2290 llvm::Constant *StorageAddress =
2291 (InitFiniAuthSchema.isAddressDiscriminated()
2292 ? llvm::ConstantExpr::getIntToPtr(
2293 llvm::ConstantInt::get(
2294 IntPtrTy,
2295 llvm::ConstantPtrAuth::AddrDiscriminator_CtorsDtors),
2296 PtrTy)
2297 : nullptr);
2298 llvm::Constant *SignedCtorPtr = getConstantSignedPointer(
2299 I.Initializer, InitFiniAuthSchema.getKey(), StorageAddress,
2300 llvm::ConstantInt::get(
2301 SizeTy, InitFiniAuthSchema.getConstantDiscrimination()));
2302 Ctor.add(SignedCtorPtr);
2303 } else {
2304 Ctor.add(I.Initializer);
2305 }
2306 if (I.AssociatedData)
2307 Ctor.add(I.AssociatedData);
2308 else
2309 Ctor.addNullPointer(PtrTy);
2310 Ctor.finishAndAddTo(Ctors);
2311 }
2312
2313 auto List = Ctors.finishAndCreateGlobal(GlobalName, getPointerAlign(),
2314 /*constant*/ false,
2315 llvm::GlobalValue::AppendingLinkage);
2316
2317 // The LTO linker doesn't seem to like it when we set an alignment
2318 // on appending variables. Take it off as a workaround.
2319 List->setAlignment(std::nullopt);
2320
2321 Fns.clear();
2322}
2323
2324llvm::GlobalValue::LinkageTypes
2326 const auto *D = cast<FunctionDecl>(GD.getDecl());
2327
2329
2330 if (const auto *Dtor = dyn_cast<CXXDestructorDecl>(D))
2332
2334}
2335
2336llvm::ConstantInt *CodeGenModule::CreateCrossDsoCfiTypeId(llvm::Metadata *MD) {
2337 llvm::MDString *MDS = dyn_cast<llvm::MDString>(MD);
2338 if (!MDS) return nullptr;
2339
2340 return llvm::ConstantInt::get(Int64Ty, llvm::MD5Hash(MDS->getString()));
2341}
2342
2343// Generalize pointer types to a void pointer with the qualifiers of the
2344// originally pointed-to type, e.g. 'const char *' and 'char * const *'
2345// generalize to 'const void *' while 'char *' and 'const char **' generalize to
2346// 'void *'.
2348 if (!Ty->isPointerType())
2349 return Ty;
2350
2351 return Ctx.getPointerType(
2352 QualType(Ctx.VoidTy)
2354}
2355
2356// Apply type generalization to a FunctionType's return and argument types
2358 if (auto *FnType = Ty->getAs<FunctionProtoType>()) {
2359 SmallVector<QualType, 8> GeneralizedParams;
2360 for (auto &Param : FnType->param_types())
2361 GeneralizedParams.push_back(GeneralizeType(Ctx, Param));
2362
2363 return Ctx.getFunctionType(GeneralizeType(Ctx, FnType->getReturnType()),
2364 GeneralizedParams, FnType->getExtProtoInfo());
2365 }
2366
2367 if (auto *FnType = Ty->getAs<FunctionNoProtoType>())
2368 return Ctx.getFunctionNoProtoType(
2369 GeneralizeType(Ctx, FnType->getReturnType()));
2370
2371 llvm_unreachable("Encountered unknown FunctionType");
2372}
2373
2374llvm::ConstantInt *CodeGenModule::CreateKCFITypeId(QualType T, StringRef Salt) {
2375 if (getCodeGenOpts().SanitizeCfiICallGeneralizePointers)
2377 if (auto *FnType = T->getAs<FunctionProtoType>())
2379 FnType->getReturnType(), FnType->getParamTypes(),
2380 FnType->getExtProtoInfo().withExceptionSpec(EST_None));
2381
2382 std::string OutName;
2383 llvm::raw_string_ostream Out(OutName);
2385 T, Out, getCodeGenOpts().SanitizeCfiICallNormalizeIntegers);
2386
2387 if (!Salt.empty())
2388 Out << "." << Salt;
2389
2390 if (getCodeGenOpts().SanitizeCfiICallNormalizeIntegers)
2391 Out << ".normalized";
2392 if (getCodeGenOpts().SanitizeCfiICallGeneralizePointers)
2393 Out << ".generalized";
2394
2395 return llvm::ConstantInt::get(Int32Ty,
2396 static_cast<uint32_t>(llvm::xxHash64(OutName)));
2397}
2398
2400 const CGFunctionInfo &Info,
2401 llvm::Function *F, bool IsThunk) {
2402 unsigned CallingConv;
2403 llvm::AttributeList PAL;
2404 ConstructAttributeList(F->getName(), Info, GD, PAL, CallingConv,
2405 /*AttrOnCallSite=*/false, IsThunk);
2406 if (CallingConv == llvm::CallingConv::X86_VectorCall &&
2407 getTarget().getTriple().isWindowsArm64EC()) {
2409 if (const Decl *D = GD.getDecl())
2410 Loc = D->getLocation();
2411
2412 Error(Loc, "__vectorcall calling convention is not currently supported");
2413 }
2414 F->setAttributes(PAL);
2415 F->setCallingConv(static_cast<llvm::CallingConv::ID>(CallingConv));
2416}
2417
2418static void removeImageAccessQualifier(std::string& TyName) {
2419 std::string ReadOnlyQual("__read_only");
2420 std::string::size_type ReadOnlyPos = TyName.find(ReadOnlyQual);
2421 if (ReadOnlyPos != std::string::npos)
2422 // "+ 1" for the space after access qualifier.
2423 TyName.erase(ReadOnlyPos, ReadOnlyQual.size() + 1);
2424 else {
2425 std::string WriteOnlyQual("__write_only");
2426 std::string::size_type WriteOnlyPos = TyName.find(WriteOnlyQual);
2427 if (WriteOnlyPos != std::string::npos)
2428 TyName.erase(WriteOnlyPos, WriteOnlyQual.size() + 1);
2429 else {
2430 std::string ReadWriteQual("__read_write");
2431 std::string::size_type ReadWritePos = TyName.find(ReadWriteQual);
2432 if (ReadWritePos != std::string::npos)
2433 TyName.erase(ReadWritePos, ReadWriteQual.size() + 1);
2434 }
2435 }
2436}
2437
2438// Returns the address space id that should be produced to the
2439// kernel_arg_addr_space metadata. This is always fixed to the ids
2440// as specified in the SPIR 2.0 specification in order to differentiate
2441// for example in clGetKernelArgInfo() implementation between the address
2442// spaces with targets without unique mapping to the OpenCL address spaces
2443// (basically all single AS CPUs).
2444static unsigned ArgInfoAddressSpace(LangAS AS) {
2445 switch (AS) {
2447 return 1;
2449 return 2;
2451 return 3;
2453 return 4; // Not in SPIR 2.0 specs.
2455 return 5;
2457 return 6;
2458 default:
2459 return 0; // Assume private.
2460 }
2461}
2462
2464 const FunctionDecl *FD,
2465 CodeGenFunction *CGF) {
2466 assert(((FD && CGF) || (!FD && !CGF)) &&
2467 "Incorrect use - FD and CGF should either be both null or not!");
2468 // Create MDNodes that represent the kernel arg metadata.
2469 // Each MDNode is a list in the form of "key", N number of values which is
2470 // the same number of values as their are kernel arguments.
2471
2472 const PrintingPolicy &Policy = Context.getPrintingPolicy();
2473
2474 // MDNode for the kernel argument address space qualifiers.
2476
2477 // MDNode for the kernel argument access qualifiers (images only).
2479
2480 // MDNode for the kernel argument type names.
2482
2483 // MDNode for the kernel argument base type names.
2484 SmallVector<llvm::Metadata *, 8> argBaseTypeNames;
2485
2486 // MDNode for the kernel argument type qualifiers.
2488
2489 // MDNode for the kernel argument names.
2491
2492 if (FD && CGF)
2493 for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) {
2494 const ParmVarDecl *parm = FD->getParamDecl(i);
2495 // Get argument name.
2496 argNames.push_back(llvm::MDString::get(VMContext, parm->getName()));
2497
2498 if (!getLangOpts().OpenCL)
2499 continue;
2500 QualType ty = parm->getType();
2501 std::string typeQuals;
2502
2503 // Get image and pipe access qualifier:
2504 if (ty->isImageType() || ty->isPipeType()) {
2505 const Decl *PDecl = parm;
2506 if (const auto *TD = ty->getAs<TypedefType>())
2507 PDecl = TD->getDecl();
2508 const OpenCLAccessAttr *A = PDecl->getAttr<OpenCLAccessAttr>();
2509 if (A && A->isWriteOnly())
2510 accessQuals.push_back(llvm::MDString::get(VMContext, "write_only"));
2511 else if (A && A->isReadWrite())
2512 accessQuals.push_back(llvm::MDString::get(VMContext, "read_write"));
2513 else
2514 accessQuals.push_back(llvm::MDString::get(VMContext, "read_only"));
2515 } else
2516 accessQuals.push_back(llvm::MDString::get(VMContext, "none"));
2517
2518 auto getTypeSpelling = [&](QualType Ty) {
2519 auto typeName = Ty.getUnqualifiedType().getAsString(Policy);
2520
2521 if (Ty.isCanonical()) {
2522 StringRef typeNameRef = typeName;
2523 // Turn "unsigned type" to "utype"
2524 if (typeNameRef.consume_front("unsigned "))
2525 return std::string("u") + typeNameRef.str();
2526 if (typeNameRef.consume_front("signed "))
2527 return typeNameRef.str();
2528 }
2529
2530 return typeName;
2531 };
2532
2533 if (ty->isPointerType()) {
2534 QualType pointeeTy = ty->getPointeeType();
2535
2536 // Get address qualifier.
2537 addressQuals.push_back(
2538 llvm::ConstantAsMetadata::get(CGF->Builder.getInt32(
2539 ArgInfoAddressSpace(pointeeTy.getAddressSpace()))));
2540
2541 // Get argument type name.
2542 std::string typeName = getTypeSpelling(pointeeTy) + "*";
2543 std::string baseTypeName =
2544 getTypeSpelling(pointeeTy.getCanonicalType()) + "*";
2545 argTypeNames.push_back(llvm::MDString::get(VMContext, typeName));
2546 argBaseTypeNames.push_back(
2547 llvm::MDString::get(VMContext, baseTypeName));
2548
2549 // Get argument type qualifiers:
2550 if (ty.isRestrictQualified())
2551 typeQuals = "restrict";
2552 if (pointeeTy.isConstQualified() ||
2554 typeQuals += typeQuals.empty() ? "const" : " const";
2555 if (pointeeTy.isVolatileQualified())
2556 typeQuals += typeQuals.empty() ? "volatile" : " volatile";
2557 } else {
2558 uint32_t AddrSpc = 0;
2559 bool isPipe = ty->isPipeType();
2560 if (ty->isImageType() || isPipe)
2562
2563 addressQuals.push_back(
2564 llvm::ConstantAsMetadata::get(CGF->Builder.getInt32(AddrSpc)));
2565
2566 // Get argument type name.
2567 ty = isPipe ? ty->castAs<PipeType>()->getElementType() : ty;
2568 std::string typeName = getTypeSpelling(ty);
2569 std::string baseTypeName = getTypeSpelling(ty.getCanonicalType());
2570
2571 // Remove access qualifiers on images
2572 // (as they are inseparable from type in clang implementation,
2573 // but OpenCL spec provides a special query to get access qualifier
2574 // via clGetKernelArgInfo with CL_KERNEL_ARG_ACCESS_QUALIFIER):
2575 if (ty->isImageType()) {
2577 removeImageAccessQualifier(baseTypeName);
2578 }
2579
2580 argTypeNames.push_back(llvm::MDString::get(VMContext, typeName));
2581 argBaseTypeNames.push_back(
2582 llvm::MDString::get(VMContext, baseTypeName));
2583
2584 if (isPipe)
2585 typeQuals = "pipe";
2586 }
2587 argTypeQuals.push_back(llvm::MDString::get(VMContext, typeQuals));
2588 }
2589
2590 if (getLangOpts().OpenCL) {
2591 Fn->setMetadata("kernel_arg_addr_space",
2592 llvm::MDNode::get(VMContext, addressQuals));
2593 Fn->setMetadata("kernel_arg_access_qual",
2594 llvm::MDNode::get(VMContext, accessQuals));
2595 Fn->setMetadata("kernel_arg_type",
2596 llvm::MDNode::get(VMContext, argTypeNames));
2597 Fn->setMetadata("kernel_arg_base_type",
2598 llvm::MDNode::get(VMContext, argBaseTypeNames));
2599 Fn->setMetadata("kernel_arg_type_qual",
2600 llvm::MDNode::get(VMContext, argTypeQuals));
2601 }
2602 if (getCodeGenOpts().EmitOpenCLArgMetadata ||
2603 getCodeGenOpts().HIPSaveKernelArgName)
2604 Fn->setMetadata("kernel_arg_name",
2605 llvm::MDNode::get(VMContext, argNames));
2606}
2607
2608/// Determines whether the language options require us to model
2609/// unwind exceptions. We treat -fexceptions as mandating this
2610/// except under the fragile ObjC ABI with only ObjC exceptions
2611/// enabled. This means, for example, that C with -fexceptions
2612/// enables this.
2613static bool hasUnwindExceptions(const LangOptions &LangOpts) {
2614 // If exceptions are completely disabled, obviously this is false.
2615 if (!LangOpts.Exceptions) return false;
2616
2617 // If C++ exceptions are enabled, this is true.
2618 if (LangOpts.CXXExceptions) return true;
2619
2620 // If ObjC exceptions are enabled, this depends on the ABI.
2621 if (LangOpts.ObjCExceptions) {
2622 return LangOpts.ObjCRuntime.hasUnwindExceptions();
2623 }
2624
2625 return true;
2626}
2627
2629 const CXXMethodDecl *MD) {
2630 // Check that the type metadata can ever actually be used by a call.
2631 if (!CGM.getCodeGenOpts().LTOUnit ||
2633 return false;
2634
2635 // Only functions whose address can be taken with a member function pointer
2636 // need this sort of type metadata.
2637 return MD->isImplicitObjectMemberFunction() && !MD->isVirtual() &&
2638 !isa<CXXConstructorDecl, CXXDestructorDecl>(MD);
2639}
2640
2643 llvm::SetVector<const CXXRecordDecl *> MostBases;
2644
2645 std::function<void (const CXXRecordDecl *)> CollectMostBases;
2646 CollectMostBases = [&](const CXXRecordDecl *RD) {
2647 if (RD->getNumBases() == 0)
2648 MostBases.insert(RD);
2649 for (const CXXBaseSpecifier &B : RD->bases())
2650 CollectMostBases(B.getType()->getAsCXXRecordDecl());
2651 };
2652 CollectMostBases(RD);
2653 return MostBases.takeVector();
2654}
2655
2657 llvm::Function *F) {
2658 llvm::AttrBuilder B(F->getContext());
2659
2660 if ((!D || !D->hasAttr<NoUwtableAttr>()) && CodeGenOpts.UnwindTables)
2661 B.addUWTableAttr(llvm::UWTableKind(CodeGenOpts.UnwindTables));
2662
2663 if (CodeGenOpts.StackClashProtector)
2664 B.addAttribute("probe-stack", "inline-asm");
2665
2666 if (CodeGenOpts.StackProbeSize && CodeGenOpts.StackProbeSize != 4096)
2667 B.addAttribute("stack-probe-size",
2668 std::to_string(CodeGenOpts.StackProbeSize));
2669
2670 if (!hasUnwindExceptions(LangOpts))
2671 B.addAttribute(llvm::Attribute::NoUnwind);
2672
2673 if (D && D->hasAttr<NoStackProtectorAttr>())
2674 ; // Do nothing.
2675 else if (D && D->hasAttr<StrictGuardStackCheckAttr>() &&
2677 B.addAttribute(llvm::Attribute::StackProtectStrong);
2678 else if (isStackProtectorOn(LangOpts, getTriple(), LangOptions::SSPOn))
2679 B.addAttribute(llvm::Attribute::StackProtect);
2681 B.addAttribute(llvm::Attribute::StackProtectStrong);
2682 else if (isStackProtectorOn(LangOpts, getTriple(), LangOptions::SSPReq))
2683 B.addAttribute(llvm::Attribute::StackProtectReq);
2684
2685 if (!D) {
2686 // Non-entry HLSL functions must always be inlined.
2687 if (getLangOpts().HLSL && !F->hasFnAttribute(llvm::Attribute::NoInline))
2688 B.addAttribute(llvm::Attribute::AlwaysInline);
2689 // If we don't have a declaration to control inlining, the function isn't
2690 // explicitly marked as alwaysinline for semantic reasons, and inlining is
2691 // disabled, mark the function as noinline.
2692 else if (!F->hasFnAttribute(llvm::Attribute::AlwaysInline) &&
2693 CodeGenOpts.getInlining() == CodeGenOptions::OnlyAlwaysInlining)
2694 B.addAttribute(llvm::Attribute::NoInline);
2695
2696 F->addFnAttrs(B);
2697 return;
2698 }
2699
2700 // Handle SME attributes that apply to function definitions,
2701 // rather than to function prototypes.
2702 if (D->hasAttr<ArmLocallyStreamingAttr>())
2703 B.addAttribute("aarch64_pstate_sm_body");
2704
2705 if (auto *Attr = D->getAttr<ArmNewAttr>()) {
2706 if (Attr->isNewZA())
2707 B.addAttribute("aarch64_new_za");
2708 if (Attr->isNewZT0())
2709 B.addAttribute("aarch64_new_zt0");
2710 }
2711
2712 // Track whether we need to add the optnone LLVM attribute,
2713 // starting with the default for this optimization level.
2714 bool ShouldAddOptNone =
2715 !CodeGenOpts.DisableO0ImplyOptNone && CodeGenOpts.OptimizationLevel == 0;
2716 // We can't add optnone in the following cases, it won't pass the verifier.
2717 ShouldAddOptNone &= !D->hasAttr<MinSizeAttr>();
2718 ShouldAddOptNone &= !D->hasAttr<AlwaysInlineAttr>();
2719
2720 // Non-entry HLSL functions must always be inlined.
2721 if (getLangOpts().HLSL && !F->hasFnAttribute(llvm::Attribute::NoInline) &&
2722 !D->hasAttr<NoInlineAttr>()) {
2723 B.addAttribute(llvm::Attribute::AlwaysInline);
2724 } else if ((ShouldAddOptNone || D->hasAttr<OptimizeNoneAttr>()) &&
2725 !F->hasFnAttribute(llvm::Attribute::AlwaysInline)) {
2726 // Add optnone, but do so only if the function isn't always_inline.
2727 B.addAttribute(llvm::Attribute::OptimizeNone);
2728
2729 // OptimizeNone implies noinline; we should not be inlining such functions.
2730 B.addAttribute(llvm::Attribute::NoInline);
2731
2732 // We still need to handle naked functions even though optnone subsumes
2733 // much of their semantics.
2734 if (D->hasAttr<NakedAttr>())
2735 B.addAttribute(llvm::Attribute::Naked);
2736
2737 // OptimizeNone wins over OptimizeForSize and MinSize.
2738 F->removeFnAttr(llvm::Attribute::OptimizeForSize);
2739 F->removeFnAttr(llvm::Attribute::MinSize);
2740 } else if (D->hasAttr<NakedAttr>()) {
2741 // Naked implies noinline: we should not be inlining such functions.
2742 B.addAttribute(llvm::Attribute::Naked);
2743 B.addAttribute(llvm::Attribute::NoInline);
2744 } else if (D->hasAttr<NoDuplicateAttr>()) {
2745 B.addAttribute(llvm::Attribute::NoDuplicate);
2746 } else if (D->hasAttr<NoInlineAttr>() &&
2747 !F->hasFnAttribute(llvm::Attribute::AlwaysInline)) {
2748 // Add noinline if the function isn't always_inline.
2749 B.addAttribute(llvm::Attribute::NoInline);
2750 } else if (D->hasAttr<AlwaysInlineAttr>() &&
2751 !F->hasFnAttribute(llvm::Attribute::NoInline)) {
2752 // (noinline wins over always_inline, and we can't specify both in IR)
2753 B.addAttribute(llvm::Attribute::AlwaysInline);
2754 } else if (CodeGenOpts.getInlining() == CodeGenOptions::OnlyAlwaysInlining) {
2755 // If we're not inlining, then force everything that isn't always_inline to
2756 // carry an explicit noinline attribute.
2757 if (!F->hasFnAttribute(llvm::Attribute::AlwaysInline))
2758 B.addAttribute(llvm::Attribute::NoInline);
2759 } else {
2760 // Otherwise, propagate the inline hint attribute and potentially use its
2761 // absence to mark things as noinline.
2762 if (auto *FD = dyn_cast<FunctionDecl>(D)) {
2763 // Search function and template pattern redeclarations for inline.
2764 auto CheckForInline = [](const FunctionDecl *FD) {
2765 auto CheckRedeclForInline = [](const FunctionDecl *Redecl) {
2766 return Redecl->isInlineSpecified();
2767 };
2768 if (any_of(FD->redecls(), CheckRedeclForInline))
2769 return true;
2770 const FunctionDecl *Pattern = FD->getTemplateInstantiationPattern();
2771 if (!Pattern)
2772 return false;
2773 return any_of(Pattern->redecls(), CheckRedeclForInline);
2774 };
2775 if (CheckForInline(FD)) {
2776 B.addAttribute(llvm::Attribute::InlineHint);
2777 } else if (CodeGenOpts.getInlining() ==
2779 !FD->isInlined() &&
2780 !F->hasFnAttribute(llvm::Attribute::AlwaysInline)) {
2781 B.addAttribute(llvm::Attribute::NoInline);
2782 }
2783 }
2784 }
2785
2786 // Add other optimization related attributes if we are optimizing this
2787 // function.
2788 if (!D->hasAttr<OptimizeNoneAttr>()) {
2789 if (D->hasAttr<ColdAttr>()) {
2790 if (!ShouldAddOptNone)
2791 B.addAttribute(llvm::Attribute::OptimizeForSize);
2792 B.addAttribute(llvm::Attribute::Cold);
2793 }
2794 if (D->hasAttr<HotAttr>())
2795 B.addAttribute(llvm::Attribute::Hot);
2796 if (D->hasAttr<MinSizeAttr>())
2797 B.addAttribute(llvm::Attribute::MinSize);
2798 }
2799
2800 F->addFnAttrs(B);
2801
2802 unsigned alignment = D->getMaxAlignment() / Context.getCharWidth();
2803 if (alignment)
2804 F->setAlignment(llvm::Align(alignment));
2805
2806 if (!D->hasAttr<AlignedAttr>())
2807 if (LangOpts.FunctionAlignment)
2808 F->setAlignment(llvm::Align(1ull << LangOpts.FunctionAlignment));
2809
2810 // Some C++ ABIs require 2-byte alignment for member functions, in order to
2811 // reserve a bit for differentiating between virtual and non-virtual member
2812 // functions. If the current target's C++ ABI requires this and this is a
2813 // member function, set its alignment accordingly.
2814 if (getTarget().getCXXABI().areMemberFunctionsAligned()) {
2815 if (isa<CXXMethodDecl>(D) && F->getPointerAlignment(getDataLayout()) < 2)
2816 F->setAlignment(std::max(llvm::Align(2), F->getAlign().valueOrOne()));
2817 }
2818
2819 // In the cross-dso CFI mode with canonical jump tables, we want !type
2820 // attributes on definitions only.
2821 if (CodeGenOpts.SanitizeCfiCrossDso &&
2822 CodeGenOpts.SanitizeCfiCanonicalJumpTables) {
2823 if (auto *FD = dyn_cast<FunctionDecl>(D)) {
2824 // Skip available_externally functions. They won't be codegen'ed in the
2825 // current module anyway.
2826 if (getContext().GetGVALinkageForFunction(FD) != GVA_AvailableExternally)
2828 }
2829 }
2830
2831 // Emit type metadata on member functions for member function pointer checks.
2832 // These are only ever necessary on definitions; we're guaranteed that the
2833 // definition will be present in the LTO unit as a result of LTO visibility.
2834 auto *MD = dyn_cast<CXXMethodDecl>(D);
2835 if (MD && requiresMemberFunctionPointerTypeMetadata(*this, MD)) {
2836 for (const CXXRecordDecl *Base : getMostBaseClasses(MD->getParent())) {
2837 llvm::Metadata *Id =
2839 MD->getType(), /*Qualifier=*/std::nullopt, Base));
2840 F->addTypeMetadata(0, Id);
2841 }
2842 }
2843}
2844
2845void CodeGenModule::SetCommonAttributes(GlobalDecl GD, llvm::GlobalValue *GV) {
2846 const Decl *D = GD.getDecl();
2847 if (isa_and_nonnull<NamedDecl>(D))
2848 setGVProperties(GV, GD);
2849 else
2850 GV->setVisibility(llvm::GlobalValue::DefaultVisibility);
2851
2852 if (D && D->hasAttr<UsedAttr>())
2854
2855 if (const auto *VD = dyn_cast_if_present<VarDecl>(D);
2856 VD &&
2857 ((CodeGenOpts.KeepPersistentStorageVariables &&
2858 (VD->getStorageDuration() == SD_Static ||
2859 VD->getStorageDuration() == SD_Thread)) ||
2860 (CodeGenOpts.KeepStaticConsts && VD->getStorageDuration() == SD_Static &&
2861 VD->getType().isConstQualified())))
2863}
2864
2865bool CodeGenModule::GetCPUAndFeaturesAttributes(GlobalDecl GD,
2866 llvm::AttrBuilder &Attrs,
2867 bool SetTargetFeatures) {
2868 // Add target-cpu and target-features attributes to functions. If
2869 // we have a decl for the function and it has a target attribute then
2870 // parse that and add it to the feature set.
2871 StringRef TargetCPU = getTarget().getTargetOpts().CPU;
2872 StringRef TuneCPU = getTarget().getTargetOpts().TuneCPU;
2873 std::vector<std::string> Features;
2874 const auto *FD = dyn_cast_or_null<FunctionDecl>(GD.getDecl());
2875 FD = FD ? FD->getMostRecentDecl() : FD;
2876 const auto *TD = FD ? FD->getAttr<TargetAttr>() : nullptr;
2877 const auto *TV = FD ? FD->getAttr<TargetVersionAttr>() : nullptr;
2878 assert((!TD || !TV) && "both target_version and target specified");
2879 const auto *SD = FD ? FD->getAttr<CPUSpecificAttr>() : nullptr;
2880 const auto *TC = FD ? FD->getAttr<TargetClonesAttr>() : nullptr;
2881 bool AddedAttr = false;
2882 if (TD || TV || SD || TC) {
2883 llvm::StringMap<bool> FeatureMap;
2884 getContext().getFunctionFeatureMap(FeatureMap, GD);
2885
2886 // Produce the canonical string for this set of features.
2887 for (const llvm::StringMap<bool>::value_type &Entry : FeatureMap)
2888 Features.push_back((Entry.getValue() ? "+" : "-") + Entry.getKey().str());
2889
2890 // Now add the target-cpu and target-features to the function.
2891 // While we populated the feature map above, we still need to
2892 // get and parse the target attribute so we can get the cpu for
2893 // the function.
2894 if (TD) {
2896 Target.parseTargetAttr(TD->getFeaturesStr());
2897 if (!ParsedAttr.CPU.empty() &&
2898 getTarget().isValidCPUName(ParsedAttr.CPU)) {
2899 TargetCPU = ParsedAttr.CPU;
2900 TuneCPU = ""; // Clear the tune CPU.
2901 }
2902 if (!ParsedAttr.Tune.empty() &&
2903 getTarget().isValidCPUName(ParsedAttr.Tune))
2904 TuneCPU = ParsedAttr.Tune;
2905 }
2906
2907 if (SD) {
2908 // Apply the given CPU name as the 'tune-cpu' so that the optimizer can
2909 // favor this processor.
2910 TuneCPU = SD->getCPUName(GD.getMultiVersionIndex())->getName();
2911 }
2912 } else {
2913 // Otherwise just add the existing target cpu and target features to the
2914 // function.
2915 Features = getTarget().getTargetOpts().Features;
2916 }
2917
2918 if (!TargetCPU.empty()) {
2919 Attrs.addAttribute("target-cpu", TargetCPU);
2920 AddedAttr = true;
2921 }
2922 if (!TuneCPU.empty()) {
2923 Attrs.addAttribute("tune-cpu", TuneCPU);
2924 AddedAttr = true;
2925 }
2926 if (!Features.empty() && SetTargetFeatures) {
2927 llvm::erase_if(Features, [&](const std::string& F) {
2928 return getTarget().isReadOnlyFeature(F.substr(1));
2929 });
2930 llvm::sort(Features);
2931 Attrs.addAttribute("target-features", llvm::join(Features, ","));
2932 AddedAttr = true;
2933 }
2934 // Add metadata for AArch64 Function Multi Versioning.
2935 if (getTarget().getTriple().isAArch64()) {
2937 bool IsDefault = false;
2938 if (TV) {
2939 IsDefault = TV->isDefaultVersion();
2940 TV->getFeatures(Feats);
2941 } else if (TC) {
2942 IsDefault = TC->isDefaultVersion(GD.getMultiVersionIndex());
2943 TC->getFeatures(Feats, GD.getMultiVersionIndex());
2944 }
2945 if (IsDefault) {
2946 Attrs.addAttribute("fmv-features");
2947 AddedAttr = true;
2948 } else if (!Feats.empty()) {
2949 // Sort features and remove duplicates.
2950 std::set<StringRef> OrderedFeats(Feats.begin(), Feats.end());
2951 std::string FMVFeatures;
2952 for (StringRef F : OrderedFeats)
2953 FMVFeatures.append("," + F.str());
2954 Attrs.addAttribute("fmv-features", FMVFeatures.substr(1));
2955 AddedAttr = true;
2956 }
2957 }
2958 return AddedAttr;
2959}
2960
2961void CodeGenModule::setNonAliasAttributes(GlobalDecl GD,
2962 llvm::GlobalObject *GO) {
2963 const Decl *D = GD.getDecl();
2964 SetCommonAttributes(GD, GO);
2965
2966 if (D) {
2967 if (auto *GV = dyn_cast<llvm::GlobalVariable>(GO)) {
2968 if (D->hasAttr<RetainAttr>())
2969 addUsedGlobal(GV);
2970 if (auto *SA = D->getAttr<PragmaClangBSSSectionAttr>())
2971 GV->addAttribute("bss-section", SA->getName());
2972 if (auto *SA = D->getAttr<PragmaClangDataSectionAttr>())
2973 GV->addAttribute("data-section", SA->getName());
2974 if (auto *SA = D->getAttr<PragmaClangRodataSectionAttr>())
2975 GV->addAttribute("rodata-section", SA->getName());
2976 if (auto *SA = D->getAttr<PragmaClangRelroSectionAttr>())
2977 GV->addAttribute("relro-section", SA->getName());
2978 }
2979
2980 if (auto *F = dyn_cast<llvm::Function>(GO)) {
2981 if (D->hasAttr<RetainAttr>())
2982 addUsedGlobal(F);
2983 if (auto *SA = D->getAttr<PragmaClangTextSectionAttr>())
2984 if (!D->getAttr<SectionAttr>())
2985 F->setSection(SA->getName());
2986
2987 llvm::AttrBuilder Attrs(F->getContext());
2988 if (GetCPUAndFeaturesAttributes(GD, Attrs)) {
2989 // We know that GetCPUAndFeaturesAttributes will always have the
2990 // newest set, since it has the newest possible FunctionDecl, so the
2991 // new ones should replace the old.
2992 llvm::AttributeMask RemoveAttrs;
2993 RemoveAttrs.addAttribute("target-cpu");
2994 RemoveAttrs.addAttribute("target-features");
2995 RemoveAttrs.addAttribute("fmv-features");
2996 RemoveAttrs.addAttribute("tune-cpu");
2997 F->removeFnAttrs(RemoveAttrs);
2998 F->addFnAttrs(Attrs);
2999 }
3000 }
3001
3002 if (const auto *CSA = D->getAttr<CodeSegAttr>())
3003 GO->setSection(CSA->getName());
3004 else if (const auto *SA = D->getAttr<SectionAttr>())
3005 GO->setSection(SA->getName());
3006 }
3007
3009}
3010
3012 llvm::Function *F,
3013 const CGFunctionInfo &FI) {
3014 const Decl *D = GD.getDecl();
3015 SetLLVMFunctionAttributes(GD, FI, F, /*IsThunk=*/false);
3017
3018 F->setLinkage(llvm::Function::InternalLinkage);
3019
3020 setNonAliasAttributes(GD, F);
3021}
3022
3023static void setLinkageForGV(llvm::GlobalValue *GV, const NamedDecl *ND) {
3024 // Set linkage and visibility in case we never see a definition.
3026 // Don't set internal linkage on declarations.
3027 // "extern_weak" is overloaded in LLVM; we probably should have
3028 // separate linkage types for this.
3029 if (isExternallyVisible(LV.getLinkage()) &&
3030 (ND->hasAttr<WeakAttr>() || ND->isWeakImported()))
3031 GV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
3032}
3033
3035 llvm::Function *F) {
3036 // Only if we are checking indirect calls.
3037 if (!LangOpts.Sanitize.has(SanitizerKind::CFIICall))
3038 return;
3039
3040 // Non-static class methods are handled via vtable or member function pointer
3041 // checks elsewhere.
3042 if (isa<CXXMethodDecl>(FD) && !cast<CXXMethodDecl>(FD)->isStatic())
3043 return;
3044
3045 llvm::Metadata *MD = CreateMetadataIdentifierForType(FD->getType());
3046 F->addTypeMetadata(0, MD);
3047 F->addTypeMetadata(0, CreateMetadataIdentifierGeneralized(FD->getType()));
3048
3049 // Emit a hash-based bit set entry for cross-DSO calls.
3050 if (CodeGenOpts.SanitizeCfiCrossDso)
3051 if (auto CrossDsoTypeId = CreateCrossDsoCfiTypeId(MD))
3052 F->addTypeMetadata(0, llvm::ConstantAsMetadata::get(CrossDsoTypeId));
3053}
3054
3055void CodeGenModule::setKCFIType(const FunctionDecl *FD, llvm::Function *F) {
3056 llvm::LLVMContext &Ctx = F->getContext();
3057 llvm::MDBuilder MDB(Ctx);
3058 llvm::StringRef Salt;
3059
3060 if (const auto *FP = FD->getType()->getAs<FunctionProtoType>())
3061 if (const auto &Info = FP->getExtraAttributeInfo())
3062 Salt = Info.CFISalt;
3063
3064 F->setMetadata(llvm::LLVMContext::MD_kcfi_type,
3065 llvm::MDNode::get(Ctx, MDB.createConstant(CreateKCFITypeId(
3066 FD->getType(), Salt))));
3067}
3068
3069static bool allowKCFIIdentifier(StringRef Name) {
3070 // KCFI type identifier constants are only necessary for external assembly
3071 // functions, which means it's safe to skip unusual names. Subset of
3072 // MCAsmInfo::isAcceptableChar() and MCAsmInfoXCOFF::isAcceptableChar().
3073 return llvm::all_of(Name, [](const char &C) {
3074 return llvm::isAlnum(C) || C == '_' || C == '.';
3075 });
3076}
3077
3079 llvm::Module &M = getModule();
3080 for (auto &F : M.functions()) {
3081 // Remove KCFI type metadata from non-address-taken local functions.
3082 bool AddressTaken = F.hasAddressTaken();
3083 if (!AddressTaken && F.hasLocalLinkage())
3084 F.eraseMetadata(llvm::LLVMContext::MD_kcfi_type);
3085
3086 // Generate a constant with the expected KCFI type identifier for all
3087 // address-taken function declarations to support annotating indirectly
3088 // called assembly functions.
3089 if (!AddressTaken || !F.isDeclaration())
3090 continue;
3091
3092 const llvm::ConstantInt *Type;
3093 if (const llvm::MDNode *MD = F.getMetadata(llvm::LLVMContext::MD_kcfi_type))
3094 Type = llvm::mdconst::extract<llvm::ConstantInt>(MD->getOperand(0));
3095 else
3096 continue;
3097
3098 StringRef Name = F.getName();
3099 if (!allowKCFIIdentifier(Name))
3100 continue;
3101
3102 std::string Asm = (".weak __kcfi_typeid_" + Name + "\n.set __kcfi_typeid_" +
3103 Name + ", " + Twine(Type->getZExtValue()) + "\n")
3104 .str();
3105 M.appendModuleInlineAsm(Asm);
3106 }
3107}
3108
3109void CodeGenModule::SetFunctionAttributes(GlobalDecl GD, llvm::Function *F,
3110 bool IsIncompleteFunction,
3111 bool IsThunk) {
3112
3113 if (F->getIntrinsicID() != llvm::Intrinsic::not_intrinsic) {
3114 // If this is an intrinsic function, the attributes will have been set
3115 // when the function was created.
3116 return;
3117 }
3118
3119 const auto *FD = cast<FunctionDecl>(GD.getDecl());
3120
3121 if (!IsIncompleteFunction)
3122 SetLLVMFunctionAttributes(GD, getTypes().arrangeGlobalDeclaration(GD), F,
3123 IsThunk);
3124
3125 // Add the Returned attribute for "this", except for iOS 5 and earlier
3126 // where substantial code, including the libstdc++ dylib, was compiled with
3127 // GCC and does not actually return "this".
3128 if (!IsThunk && getCXXABI().HasThisReturn(GD) &&
3129 !(getTriple().isiOS() && getTriple().isOSVersionLT(6))) {
3130 assert(!F->arg_empty() &&
3131 F->arg_begin()->getType()
3132 ->canLosslesslyBitCastTo(F->getReturnType()) &&
3133 "unexpected this return");
3134 F->addParamAttr(0, llvm::Attribute::Returned);
3135 }
3136
3137 // Only a few attributes are set on declarations; these may later be
3138 // overridden by a definition.
3139
3140 setLinkageForGV(F, FD);
3141 setGVProperties(F, FD);
3142
3143 // Setup target-specific attributes.
3144 if (!IsIncompleteFunction && F->isDeclaration())
3146
3147 if (const auto *CSA = FD->getAttr<CodeSegAttr>())
3148 F->setSection(CSA->getName());
3149 else if (const auto *SA = FD->getAttr<SectionAttr>())
3150 F->setSection(SA->getName());
3151
3152 if (const auto *EA = FD->getAttr<ErrorAttr>()) {
3153 if (EA->isError())
3154 F->addFnAttr("dontcall-error", EA->getUserDiagnostic());
3155 else if (EA->isWarning())
3156 F->addFnAttr("dontcall-warn", EA->getUserDiagnostic());
3157 }
3158
3159 // If we plan on emitting this inline builtin, we can't treat it as a builtin.
3160 if (FD->isInlineBuiltinDeclaration()) {
3161 const FunctionDecl *FDBody;
3162 bool HasBody = FD->hasBody(FDBody);
3163 (void)HasBody;
3164 assert(HasBody && "Inline builtin declarations should always have an "
3165 "available body!");
3166 if (shouldEmitFunction(FDBody))
3167 F->addFnAttr(llvm::Attribute::NoBuiltin);
3168 }
3169
3171 // A replaceable global allocation function does not act like a builtin by
3172 // default, only if it is invoked by a new-expression or delete-expression.
3173 F->addFnAttr(llvm::Attribute::NoBuiltin);
3174 }
3175
3176 if (isa<CXXConstructorDecl>(FD) || isa<CXXDestructorDecl>(FD))
3177 F->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
3178 else if (const auto *MD = dyn_cast<CXXMethodDecl>(FD))
3179 if (MD->isVirtual())
3180 F->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
3181
3182 // Don't emit entries for function declarations in the cross-DSO mode. This
3183 // is handled with better precision by the receiving DSO. But if jump tables
3184 // are non-canonical then we need type metadata in order to produce the local
3185 // jump table.
3186 if (!CodeGenOpts.SanitizeCfiCrossDso ||
3187 !CodeGenOpts.SanitizeCfiCanonicalJumpTables)
3189
3190 if (LangOpts.Sanitize.has(SanitizerKind::KCFI))
3191 setKCFIType(FD, F);
3192
3193 if (getLangOpts().OpenMP && FD->hasAttr<OMPDeclareSimdDeclAttr>())
3195
3196 if (CodeGenOpts.InlineMaxStackSize != UINT_MAX)
3197 F->addFnAttr("inline-max-stacksize", llvm::utostr(CodeGenOpts.InlineMaxStackSize));
3198
3199 if (const auto *CB = FD->getAttr<CallbackAttr>()) {
3200 // Annotate the callback behavior as metadata:
3201 // - The callback callee (as argument number).
3202 // - The callback payloads (as argument numbers).
3203 llvm::LLVMContext &Ctx = F->getContext();
3204 llvm::MDBuilder MDB(Ctx);
3205
3206 // The payload indices are all but the first one in the encoding. The first
3207 // identifies the callback callee.
3208 int CalleeIdx = *CB->encoding_begin();
3209 ArrayRef<int> PayloadIndices(CB->encoding_begin() + 1, CB->encoding_end());
3210 F->addMetadata(llvm::LLVMContext::MD_callback,
3211 *llvm::MDNode::get(Ctx, {MDB.createCallbackEncoding(
3212 CalleeIdx, PayloadIndices,
3213 /* VarArgsArePassed */ false)}));
3214 }
3215}
3216
3217void CodeGenModule::addUsedGlobal(llvm::GlobalValue *GV) {
3218 assert((isa<llvm::Function>(GV) || !GV->isDeclaration()) &&
3219 "Only globals with definition can force usage.");
3220 LLVMUsed.emplace_back(GV);
3221}
3222
3223void CodeGenModule::addCompilerUsedGlobal(llvm::GlobalValue *GV) {
3224 assert(!GV->isDeclaration() &&
3225 "Only globals with definition can force usage.");
3226 LLVMCompilerUsed.emplace_back(GV);
3227}
3228
3230 assert((isa<llvm::Function>(GV) || !GV->isDeclaration()) &&
3231 "Only globals with definition can force usage.");
3232 if (getTriple().isOSBinFormatELF())
3233 LLVMCompilerUsed.emplace_back(GV);
3234 else
3235 LLVMUsed.emplace_back(GV);
3236}
3237
3238static void emitUsed(CodeGenModule &CGM, StringRef Name,
3239 std::vector<llvm::WeakTrackingVH> &List) {
3240 // Don't create llvm.used if there is no need.
3241 if (List.empty())
3242 return;
3243
3244 // Convert List to what ConstantArray needs.
3246 UsedArray.resize(List.size());
3247 for (unsigned i = 0, e = List.size(); i != e; ++i) {
3248 UsedArray[i] =
3249 llvm::ConstantExpr::getPointerBitCastOrAddrSpaceCast(
3250 cast<llvm::Constant>(&*List[i]), CGM.Int8PtrTy);
3251 }
3252
3253 if (UsedArray.empty())
3254 return;
3255 llvm::ArrayType *ATy = llvm::ArrayType::get(CGM.Int8PtrTy, UsedArray.size());
3256
3257 auto *GV = new llvm::GlobalVariable(
3258 CGM.getModule(), ATy, false, llvm::GlobalValue::AppendingLinkage,
3259 llvm::ConstantArray::get(ATy, UsedArray), Name);
3260
3261 GV->setSection("llvm.metadata");
3262}
3263
3264void CodeGenModule::emitLLVMUsed() {
3265 emitUsed(*this, "llvm.used", LLVMUsed);
3266 emitUsed(*this, "llvm.compiler.used", LLVMCompilerUsed);
3267}
3268
3270 auto *MDOpts = llvm::MDString::get(getLLVMContext(), Opts);
3271 LinkerOptionsMetadata.push_back(llvm::MDNode::get(getLLVMContext(), MDOpts));
3272}
3273
3274void CodeGenModule::AddDetectMismatch(StringRef Name, StringRef Value) {
3277 if (Opt.empty())
3278 return;
3279 auto *MDOpts = llvm::MDString::get(getLLVMContext(), Opt);
3280 LinkerOptionsMetadata.push_back(llvm::MDNode::get(getLLVMContext(), MDOpts));
3281}
3282
3284 auto &C = getLLVMContext();
3285 if (getTarget().getTriple().isOSBinFormatELF()) {
3286 ELFDependentLibraries.push_back(
3287 llvm::MDNode::get(C, llvm::MDString::get(C, Lib)));
3288 return;
3289 }
3290
3293 auto *MDOpts = llvm::MDString::get(getLLVMContext(), Opt);
3294 LinkerOptionsMetadata.push_back(llvm::MDNode::get(C, MDOpts));
3295}
3296
3297/// Add link options implied by the given module, including modules
3298/// it depends on, using a postorder walk.
3302 // Import this module's parent.
3303 if (Mod->Parent && Visited.insert(Mod->Parent).second) {
3304 addLinkOptionsPostorder(CGM, Mod->Parent, Metadata, Visited);
3305 }
3306
3307 // Import this module's dependencies.
3308 for (Module *Import : llvm::reverse(Mod->Imports)) {
3309 if (Visited.insert(Import).second)
3310 addLinkOptionsPostorder(CGM, Import, Metadata, Visited);
3311 }
3312
3313 // Add linker options to link against the libraries/frameworks
3314 // described by this module.
3315 llvm::LLVMContext &Context = CGM.getLLVMContext();
3316 bool IsELF = CGM.getTarget().getTriple().isOSBinFormatELF();
3317
3318 // For modules that use export_as for linking, use that module
3319 // name instead.
3321 return;
3322
3323 for (const Module::LinkLibrary &LL : llvm::reverse(Mod->LinkLibraries)) {
3324 // Link against a framework. Frameworks are currently Darwin only, so we
3325 // don't to ask TargetCodeGenInfo for the spelling of the linker option.
3326 if (LL.IsFramework) {
3327 llvm::Metadata *Args[2] = {llvm::MDString::get(Context, "-framework"),
3328 llvm::MDString::get(Context, LL.Library)};
3329
3330 Metadata.push_back(llvm::MDNode::get(Context, Args));
3331 continue;
3332 }
3333
3334 // Link against a library.
3335 if (IsELF) {
3336 llvm::Metadata *Args[2] = {
3337 llvm::MDString::get(Context, "lib"),
3338 llvm::MDString::get(Context, LL.Library),
3339 };
3340 Metadata.push_back(llvm::MDNode::get(Context, Args));
3341 } else {
3343 CGM.getTargetCodeGenInfo().getDependentLibraryOption(LL.Library, Opt);
3344 auto *OptString = llvm::MDString::get(Context, Opt);
3345 Metadata.push_back(llvm::MDNode::get(Context, OptString));
3346 }
3347 }
3348}
3349
3350void CodeGenModule::EmitModuleInitializers(clang::Module *Primary) {
3351 assert(Primary->isNamedModuleUnit() &&
3352 "We should only emit module initializers for named modules.");
3353
3354 // Emit the initializers in the order that sub-modules appear in the
3355 // source, first Global Module Fragments, if present.
3356 if (auto GMF = Primary->getGlobalModuleFragment()) {
3357 for (Decl *D : getContext().getModuleInitializers(GMF)) {
3358 if (isa<ImportDecl>(D))
3359 continue;
3360 assert(isa<VarDecl>(D) && "GMF initializer decl is not a var?");
3362 }
3363 }
3364 // Second any associated with the module, itself.
3365 for (Decl *D : getContext().getModuleInitializers(Primary)) {
3366 // Skip import decls, the inits for those are called explicitly.
3367 if (isa<ImportDecl>(D))
3368 continue;
3370 }
3371 // Third any associated with the Privat eMOdule Fragment, if present.
3372 if (auto PMF = Primary->getPrivateModuleFragment()) {
3373 for (Decl *D : getContext().getModuleInitializers(PMF)) {
3374 // Skip import decls, the inits for those are called explicitly.
3375 if (isa<ImportDecl>(D))
3376 continue;
3377 assert(isa<VarDecl>(D) && "PMF initializer decl is not a var?");
3379 }
3380 }
3381}
3382
3383void CodeGenModule::EmitModuleLinkOptions() {
3384 // Collect the set of all of the modules we want to visit to emit link
3385 // options, which is essentially the imported modules and all of their
3386 // non-explicit child modules.
3387 llvm::SetVector<clang::Module *> LinkModules;
3390
3391 // Seed the stack with imported modules.
3392 for (Module *M : ImportedModules) {
3393 // Do not add any link flags when an implementation TU of a module imports
3394 // a header of that same module.
3395 if (M->getTopLevelModuleName() == getLangOpts().CurrentModule &&
3396 !getLangOpts().isCompilingModule())
3397 continue;
3398 if (Visited.insert(M).second)
3399 Stack.push_back(M);
3400 }
3401
3402 // Find all of the modules to import, making a little effort to prune
3403 // non-leaf modules.
3404 while (!Stack.empty()) {
3405 clang::Module *Mod = Stack.pop_back_val();
3406
3407 bool AnyChildren = false;
3408
3409 // Visit the submodules of this module.
3410 for (const auto &SM : Mod->submodules()) {
3411 // Skip explicit children; they need to be explicitly imported to be
3412 // linked against.
3413 if (SM->IsExplicit)
3414 continue;
3415
3416 if (Visited.insert(SM).second) {
3417 Stack.push_back(SM);
3418 AnyChildren = true;
3419 }
3420 }
3421
3422 // We didn't find any children, so add this module to the list of
3423 // modules to link against.
3424 if (!AnyChildren) {
3425 LinkModules.insert(Mod);
3426 }
3427 }
3428
3429 // Add link options for all of the imported modules in reverse topological
3430 // order. We don't do anything to try to order import link flags with respect
3431 // to linker options inserted by things like #pragma comment().
3433 Visited.clear();
3434 for (Module *M : LinkModules)
3435 if (Visited.insert(M).second)
3436 addLinkOptionsPostorder(*this, M, MetadataArgs, Visited);
3437 std::reverse(MetadataArgs.begin(), MetadataArgs.end());
3438 LinkerOptionsMetadata.append(MetadataArgs.begin(), MetadataArgs.end());
3439
3440 // Add the linker options metadata flag.
3441 if (!LinkerOptionsMetadata.empty()) {
3442 auto *NMD = getModule().getOrInsertNamedMetadata("llvm.linker.options");
3443 for (auto *MD : LinkerOptionsMetadata)
3444 NMD->addOperand(MD);
3445 }
3446}
3447
3448void CodeGenModule::EmitDeferred() {
3449 // Emit deferred declare target declarations.
3450 if (getLangOpts().OpenMP && !getLangOpts().OpenMPSimd)
3452
3453 // Emit code for any potentially referenced deferred decls. Since a
3454 // previously unused static decl may become used during the generation of code
3455 // for a static function, iterate until no changes are made.
3456
3457 if (!DeferredVTables.empty()) {
3458 EmitDeferredVTables();
3459
3460 // Emitting a vtable doesn't directly cause more vtables to
3461 // become deferred, although it can cause functions to be
3462 // emitted that then need those vtables.
3463 assert(DeferredVTables.empty());
3464 }
3465
3466 // Emit CUDA/HIP static device variables referenced by host code only.
3467 // Note we should not clear CUDADeviceVarODRUsedByHost since it is still
3468 // needed for further handling.
3469 if (getLangOpts().CUDA && getLangOpts().CUDAIsDevice)
3470 llvm::append_range(DeferredDeclsToEmit,
3471 getContext().CUDADeviceVarODRUsedByHost);
3472
3473 // Stop if we're out of both deferred vtables and deferred declarations.
3474 if (DeferredDeclsToEmit.empty())
3475 return;
3476
3477 // Grab the list of decls to emit. If EmitGlobalDefinition schedules more
3478 // work, it will not interfere with this.
3479 std::vector<GlobalDecl> CurDeclsToEmit;
3480 CurDeclsToEmit.swap(DeferredDeclsToEmit);
3481
3482 for (GlobalDecl &D : CurDeclsToEmit) {
3483 // Functions declared with the sycl_kernel_entry_point attribute are
3484 // emitted normally during host compilation. During device compilation,
3485 // a SYCL kernel caller offload entry point function is generated and
3486 // emitted in place of each of these functions.
3487 if (const auto *FD = D.getDecl()->getAsFunction()) {
3488 if (LangOpts.SYCLIsDevice && FD->hasAttr<SYCLKernelEntryPointAttr>() &&
3489 FD->isDefined()) {
3490 // Functions with an invalid sycl_kernel_entry_point attribute are
3491 // ignored during device compilation.
3492 if (!FD->getAttr<SYCLKernelEntryPointAttr>()->isInvalidAttr()) {
3493 // Generate and emit the SYCL kernel caller function.
3494 EmitSYCLKernelCaller(FD, getContext());
3495 // Recurse to emit any symbols directly or indirectly referenced
3496 // by the SYCL kernel caller function.
3497 EmitDeferred();
3498 }
3499 // Do not emit the sycl_kernel_entry_point attributed function.
3500 continue;
3501 }
3502 }
3503
3504 // We should call GetAddrOfGlobal with IsForDefinition set to true in order
3505 // to get GlobalValue with exactly the type we need, not something that
3506 // might had been created for another decl with the same mangled name but
3507 // different type.
3508 llvm::GlobalValue *GV = dyn_cast<llvm::GlobalValue>(
3510
3511 // In case of different address spaces, we may still get a cast, even with
3512 // IsForDefinition equal to true. Query mangled names table to get
3513 // GlobalValue.
3514 if (!GV)
3516
3517 // Make sure GetGlobalValue returned non-null.
3518 assert(GV);
3519
3520 // Check to see if we've already emitted this. This is necessary
3521 // for a couple of reasons: first, decls can end up in the
3522 // deferred-decls queue multiple times, and second, decls can end
3523 // up with definitions in unusual ways (e.g. by an extern inline
3524 // function acquiring a strong function redefinition). Just
3525 // ignore these cases.
3526 if (!GV->isDeclaration())
3527 continue;
3528
3529 // If this is OpenMP, check if it is legal to emit this global normally.
3530 if (LangOpts.OpenMP && OpenMPRuntime && OpenMPRuntime->emitTargetGlobal(D))
3531 continue;
3532
3533 // Otherwise, emit the definition and move on to the next one.
3534 EmitGlobalDefinition(D, GV);
3535
3536 // If we found out that we need to emit more decls, do that recursively.
3537 // This has the advantage that the decls are emitted in a DFS and related
3538 // ones are close together, which is convenient for testing.
3539 if (!DeferredVTables.empty() || !DeferredDeclsToEmit.empty()) {
3540 EmitDeferred();
3541 assert(DeferredVTables.empty() && DeferredDeclsToEmit.empty());
3542 }
3543 }
3544}
3545
3546void CodeGenModule::EmitVTablesOpportunistically() {
3547 // Try to emit external vtables as available_externally if they have emitted
3548 // all inlined virtual functions. It runs after EmitDeferred() and therefore
3549 // is not allowed to create new references to things that need to be emitted
3550 // lazily. Note that it also uses fact that we eagerly emitting RTTI.
3551
3552 assert((OpportunisticVTables.empty() || shouldOpportunisticallyEmitVTables())
3553 && "Only emit opportunistic vtables with optimizations");
3554
3555 for (const CXXRecordDecl *RD : OpportunisticVTables) {
3556 assert(getVTables().isVTableExternal(RD) &&
3557 "This queue should only contain external vtables");
3558 if (getCXXABI().canSpeculativelyEmitVTable(RD))
3559 VTables.GenerateClassData(RD);
3560 }
3561 OpportunisticVTables.clear();
3562}
3563
3565 for (const auto& [MangledName, VD] : DeferredAnnotations) {
3566 llvm::GlobalValue *GV = GetGlobalValue(MangledName);
3567 if (GV)
3568 AddGlobalAnnotations(VD, GV);
3569 }
3570 DeferredAnnotations.clear();
3571
3572 if (Annotations.empty())
3573 return;
3574
3575 // Create a new global variable for the ConstantStruct in the Module.
3576 llvm::Constant *Array = llvm::ConstantArray::get(llvm::ArrayType::get(
3577 Annotations[0]->getType(), Annotations.size()), Annotations);
3578 auto *gv = new llvm::GlobalVariable(getModule(), Array->getType(), false,
3579 llvm::GlobalValue::AppendingLinkage,
3580 Array, "llvm.global.annotations");
3581 gv->setSection(AnnotationSection);
3582}
3583
3584llvm::Constant *CodeGenModule::EmitAnnotationString(StringRef Str) {
3585 llvm::Constant *&AStr = AnnotationStrings[Str];
3586 if (AStr)
3587 return AStr;
3588
3589 // Not found yet, create a new global.
3590 llvm::Constant *s = llvm::ConstantDataArray::getString(getLLVMContext(), Str);
3591 auto *gv = new llvm::GlobalVariable(
3592 getModule(), s->getType(), true, llvm::GlobalValue::PrivateLinkage, s,
3593 ".str", nullptr, llvm::GlobalValue::NotThreadLocal,
3594 ConstGlobalsPtrTy->getAddressSpace());
3595 gv->setSection(AnnotationSection);
3596 gv->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
3597 AStr = gv;
3598 return gv;
3599}
3600
3603 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
3604 if (PLoc.isValid())
3605 return EmitAnnotationString(PLoc.getFilename());
3606 return EmitAnnotationString(SM.getBufferName(Loc));
3607}
3608
3611 PresumedLoc PLoc = SM.getPresumedLoc(L);
3612 unsigned LineNo = PLoc.isValid() ? PLoc.getLine() :
3613 SM.getExpansionLineNumber(L);
3614 return llvm::ConstantInt::get(Int32Ty, LineNo);
3615}
3616
3617llvm::Constant *CodeGenModule::EmitAnnotationArgs(const AnnotateAttr *Attr) {
3618 ArrayRef<Expr *> Exprs = {Attr->args_begin(), Attr->args_size()};
3619 if (Exprs.empty())
3620 return llvm::ConstantPointerNull::get(ConstGlobalsPtrTy);
3621
3622 llvm::FoldingSetNodeID ID;
3623 for (Expr *E : Exprs) {
3624 ID.Add(cast<clang::ConstantExpr>(E)->getAPValueResult());
3625 }
3626 llvm::Constant *&Lookup = AnnotationArgs[ID.ComputeHash()];
3627 if (Lookup)
3628 return Lookup;
3629
3631 LLVMArgs.reserve(Exprs.size());
3632 ConstantEmitter ConstEmiter(*this);
3633 llvm::transform(Exprs, std::back_inserter(LLVMArgs), [&](const Expr *E) {
3634 const auto *CE = cast<clang::ConstantExpr>(E);
3635 return ConstEmiter.emitAbstract(CE->getBeginLoc(), CE->getAPValueResult(),
3636 CE->getType());
3637 });
3638 auto *Struct = llvm::ConstantStruct::getAnon(LLVMArgs);
3639 auto *GV = new llvm::GlobalVariable(getModule(), Struct->getType(), true,
3640 llvm::GlobalValue::PrivateLinkage, Struct,
3641 ".args");
3642 GV->setSection(AnnotationSection);
3643 GV->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
3644
3645 Lookup = GV;
3646 return GV;
3647}
3648
3649llvm::Constant *CodeGenModule::EmitAnnotateAttr(llvm::GlobalValue *GV,
3650 const AnnotateAttr *AA,
3651 SourceLocation L) {
3652 // Get the globals for file name, annotation, and the line number.
3653 llvm::Constant *AnnoGV = EmitAnnotationString(AA->getAnnotation()),
3654 *UnitGV = EmitAnnotationUnit(L),
3655 *LineNoCst = EmitAnnotationLineNo(L),
3656 *Args = EmitAnnotationArgs(AA);
3657
3658 llvm::Constant *GVInGlobalsAS = GV;
3659 if (GV->getAddressSpace() !=
3660 getDataLayout().getDefaultGlobalsAddressSpace()) {
3661 GVInGlobalsAS = llvm::ConstantExpr::getAddrSpaceCast(
3662 GV,
3663 llvm::PointerType::get(
3664 GV->getContext(), getDataLayout().getDefaultGlobalsAddressSpace()));
3665 }
3666
3667 // Create the ConstantStruct for the global annotation.
3668 llvm::Constant *Fields[] = {
3669 GVInGlobalsAS, AnnoGV, UnitGV, LineNoCst, Args,
3670 };
3671 return llvm::ConstantStruct::getAnon(Fields);
3672}
3673
3675 llvm::GlobalValue *GV) {
3676 assert(D->hasAttr<AnnotateAttr>() && "no annotate attribute");
3677 // Get the struct elements for these annotations.
3678 for (const auto *I : D->specific_attrs<AnnotateAttr>())
3679 Annotations.push_back(EmitAnnotateAttr(GV, I, D->getLocation()));
3680}
3681
3683 SourceLocation Loc) const {
3684 const auto &NoSanitizeL = getContext().getNoSanitizeList();
3685 // NoSanitize by function name.
3686 if (NoSanitizeL.containsFunction(Kind, Fn->getName()))
3687 return true;
3688 // NoSanitize by location. Check "mainfile" prefix.
3689 auto &SM = Context.getSourceManager();
3690 FileEntryRef MainFile = *SM.getFileEntryRefForID(SM.getMainFileID());
3691 if (NoSanitizeL.containsMainFile(Kind, MainFile.getName()))
3692 return true;
3693
3694 // Check "src" prefix.
3695 if (Loc.isValid())
3696 return NoSanitizeL.containsLocation(Kind, Loc);
3697 // If location is unknown, this may be a compiler-generated function. Assume
3698 // it's located in the main file.
3699 return NoSanitizeL.containsFile(Kind, MainFile.getName());
3700}
3701
3703 llvm::GlobalVariable *GV,
3705 StringRef Category) const {
3706 const auto &NoSanitizeL = getContext().getNoSanitizeList();
3707 if (NoSanitizeL.containsGlobal(Kind, GV->getName(), Category))
3708 return true;
3709 auto &SM = Context.getSourceManager();
3710 if (NoSanitizeL.containsMainFile(
3711 Kind, SM.getFileEntryRefForID(SM.getMainFileID())->getName(),
3712 Category))
3713 return true;
3714 if (NoSanitizeL.containsLocation(Kind, Loc, Category))
3715 return true;
3716
3717 // Check global type.
3718 if (!Ty.isNull()) {
3719 // Drill down the array types: if global variable of a fixed type is
3720 // not sanitized, we also don't instrument arrays of them.
3721 while (auto AT = dyn_cast<ArrayType>(Ty.getTypePtr()))
3722 Ty = AT->getElementType();
3724 // Only record types (classes, structs etc.) are ignored.
3725 if (Ty->isRecordType()) {
3726 std::string TypeStr = Ty.getAsString(getContext().getPrintingPolicy());
3727 if (NoSanitizeL.containsType(Kind, TypeStr, Category))
3728 return true;
3729 }
3730 }
3731 return false;
3732}
3733
3735 StringRef Category) const {
3736 const auto &XRayFilter = getContext().getXRayFilter();
3737 using ImbueAttr = XRayFunctionFilter::ImbueAttribute;
3738 auto Attr = ImbueAttr::NONE;
3739 if (Loc.isValid())
3740 Attr = XRayFilter.shouldImbueLocation(Loc, Category);
3741 if (Attr == ImbueAttr::NONE)
3742 Attr = XRayFilter.shouldImbueFunction(Fn->getName());
3743 switch (Attr) {
3744 case ImbueAttr::NONE:
3745 return false;
3746 case ImbueAttr::ALWAYS:
3747 Fn->addFnAttr("function-instrument", "xray-always");
3748 break;
3749 case ImbueAttr::ALWAYS_ARG1:
3750 Fn->addFnAttr("function-instrument", "xray-always");
3751 Fn->addFnAttr("xray-log-args", "1");
3752 break;
3753 case ImbueAttr::NEVER:
3754 Fn->addFnAttr("function-instrument", "xray-never");
3755 break;
3756 }
3757 return true;
3758}
3759
3762 SourceLocation Loc) const {
3763 const auto &ProfileList = getContext().getProfileList();
3764 // If the profile list is empty, then instrument everything.
3765 if (ProfileList.isEmpty())
3766 return ProfileList::Allow;
3767 llvm::driver::ProfileInstrKind Kind = getCodeGenOpts().getProfileInstr();
3768 // First, check the function name.
3769 if (auto V = ProfileList.isFunctionExcluded(Fn->getName(), Kind))
3770 return *V;
3771 // Next, check the source location.
3772 if (Loc.isValid())
3773 if (auto V = ProfileList.isLocationExcluded(Loc, Kind))
3774 return *V;
3775 // If location is unknown, this may be a compiler-generated function. Assume
3776 // it's located in the main file.
3777 auto &SM = Context.getSourceManager();
3778 if (auto MainFile = SM.getFileEntryRefForID(SM.getMainFileID()))
3779 if (auto V = ProfileList.isFileExcluded(MainFile->getName(), Kind))
3780 return *V;
3781 return ProfileList.getDefault(Kind);
3782}
3783
3786 SourceLocation Loc) const {
3788 if (V != ProfileList::Allow)
3789 return V;
3790
3791 auto NumGroups = getCodeGenOpts().ProfileTotalFunctionGroups;
3792 if (NumGroups > 1) {
3793 auto Group = llvm::crc32(arrayRefFromStringRef(Fn->getName())) % NumGroups;
3794 if (Group != getCodeGenOpts().ProfileSelectedFunctionGroup)
3795 return ProfileList::Skip;
3796 }
3797 return ProfileList::Allow;
3798}
3799
3800bool CodeGenModule::MustBeEmitted(const ValueDecl *Global) {
3801 // Never defer when EmitAllDecls is specified.
3802 if (LangOpts.EmitAllDecls)
3803 return true;
3804
3805 const auto *VD = dyn_cast<VarDecl>(Global);
3806 if (VD &&
3807 ((CodeGenOpts.KeepPersistentStorageVariables &&
3808 (VD->getStorageDuration() == SD_Static ||
3809 VD->getStorageDuration() == SD_Thread)) ||
3810 (CodeGenOpts.KeepStaticConsts && VD->getStorageDuration() == SD_Static &&
3811 VD->getType().isConstQualified())))
3812 return true;
3813
3815}
3816
3817bool CodeGenModule::MayBeEmittedEagerly(const ValueDecl *Global) {
3818 // In OpenMP 5.0 variables and function may be marked as
3819 // device_type(host/nohost) and we should not emit them eagerly unless we sure
3820 // that they must be emitted on the host/device. To be sure we need to have
3821 // seen a declare target with an explicit mentioning of the function, we know
3822 // we have if the level of the declare target attribute is -1. Note that we
3823 // check somewhere else if we should emit this at all.
3824 if (LangOpts.OpenMP >= 50 && !LangOpts.OpenMPSimd) {
3825 std::optional<OMPDeclareTargetDeclAttr *> ActiveAttr =
3826 OMPDeclareTargetDeclAttr::getActiveAttr(Global);
3827 if (!ActiveAttr || (*ActiveAttr)->getLevel() != (unsigned)-1)
3828 return false;
3829 }
3830
3831 if (const auto *FD = dyn_cast<FunctionDecl>(Global)) {
3833 // Implicit template instantiations may change linkage if they are later
3834 // explicitly instantiated, so they should not be emitted eagerly.
3835 return false;
3836 // Defer until all versions have been semantically checked.
3837 if (FD->hasAttr<TargetVersionAttr>() && !FD->isMultiVersion())
3838 return false;
3839 // Defer emission of SYCL kernel entry point functions during device
3840 // compilation.
3841 if (LangOpts.SYCLIsDevice && FD->hasAttr<SYCLKernelEntryPointAttr>())
3842 return false;
3843 }
3844 if (const auto *VD = dyn_cast<VarDecl>(Global)) {
3845 if (Context.getInlineVariableDefinitionKind(VD) ==
3847 // A definition of an inline constexpr static data member may change
3848 // linkage later if it's redeclared outside the class.
3849 return false;
3850 if (CXX20ModuleInits && VD->getOwningModule() &&
3851 !VD->getOwningModule()->isModuleMapModule()) {
3852 // For CXX20, module-owned initializers need to be deferred, since it is
3853 // not known at this point if they will be run for the current module or
3854 // as part of the initializer for an imported one.
3855 return false;
3856 }
3857 }
3858 // If OpenMP is enabled and threadprivates must be generated like TLS, delay
3859 // codegen for global variables, because they may be marked as threadprivate.
3860 if (LangOpts.OpenMP && LangOpts.OpenMPUseTLS &&
3861 getContext().getTargetInfo().isTLSSupported() && isa<VarDecl>(Global) &&
3862 !Global->getType().isConstantStorage(getContext(), false, false) &&
3863 !OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(Global))
3864 return false;
3865
3866 return true;
3867}
3868
3870 StringRef Name = getMangledName(GD);
3871
3872 // The UUID descriptor should be pointer aligned.
3874
3875 // Look for an existing global.
3876 if (llvm::GlobalVariable *GV = getModule().getNamedGlobal(Name))
3877 return ConstantAddress(GV, GV->getValueType(), Alignment);
3878
3879 ConstantEmitter Emitter(*this);
3880 llvm::Constant *Init;
3881
3882 APValue &V = GD->getAsAPValue();
3883 if (!V.isAbsent()) {
3884 // If possible, emit the APValue version of the initializer. In particular,
3885 // this gets the type of the constant right.
3886 Init = Emitter.emitForInitializer(
3887 GD->getAsAPValue(), GD->getType().getAddressSpace(), GD->getType());
3888 } else {
3889 // As a fallback, directly construct the constant.
3890 // FIXME: This may get padding wrong under esoteric struct layout rules.
3891 // MSVC appears to create a complete type 'struct __s_GUID' that it
3892 // presumably uses to represent these constants.
3893 MSGuidDecl::Parts Parts = GD->getParts();
3894 llvm::Constant *Fields[4] = {
3895 llvm::ConstantInt::get(Int32Ty, Parts.Part1),
3896 llvm::ConstantInt::get(Int16Ty, Parts.Part2),
3897 llvm::ConstantInt::get(Int16Ty, Parts.Part3),
3898 llvm::ConstantDataArray::getRaw(
3899 StringRef(reinterpret_cast<char *>(Parts.Part4And5), 8), 8,
3900 Int8Ty)};
3901 Init = llvm::ConstantStruct::getAnon(Fields);
3902 }
3903
3904 auto *GV = new llvm::GlobalVariable(
3905 getModule(), Init->getType(),
3906 /*isConstant=*/true, llvm::GlobalValue::LinkOnceODRLinkage, Init, Name);
3907 if (supportsCOMDAT())
3908 GV->setComdat(TheModule.getOrInsertComdat(GV->getName()));
3909 setDSOLocal(GV);
3910
3911 if (!V.isAbsent()) {
3912 Emitter.finalize(GV);
3913 return ConstantAddress(GV, GV->getValueType(), Alignment);
3914 }
3915
3916 llvm::Type *Ty = getTypes().ConvertTypeForMem(GD->getType());
3917 return ConstantAddress(GV, Ty, Alignment);
3918}
3919
3921 const UnnamedGlobalConstantDecl *GCD) {
3922 CharUnits Alignment = getContext().getTypeAlignInChars(GCD->getType());
3923
3924 llvm::GlobalVariable **Entry = nullptr;
3925 Entry = &UnnamedGlobalConstantDeclMap[GCD];
3926 if (*Entry)
3927 return ConstantAddress(*Entry, (*Entry)->getValueType(), Alignment);
3928
3929 ConstantEmitter Emitter(*this);
3930 llvm::Constant *Init;
3931
3932 const APValue &V = GCD->getValue();
3933
3934 assert(!V.isAbsent());
3935 Init = Emitter.emitForInitializer(V, GCD->getType().getAddressSpace(),
3936 GCD->getType());
3937
3938 auto *GV = new llvm::GlobalVariable(getModule(), Init->getType(),
3939 /*isConstant=*/true,
3940 llvm::GlobalValue::PrivateLinkage, Init,
3941 ".constant");
3942 GV->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
3943 GV->setAlignment(Alignment.getAsAlign());
3944
3945 Emitter.finalize(GV);
3946
3947 *Entry = GV;
3948 return ConstantAddress(GV, GV->getValueType(), Alignment);
3949}
3950
3952 const TemplateParamObjectDecl *TPO) {
3953 StringRef Name = getMangledName(TPO);
3954 CharUnits Alignment = getNaturalTypeAlignment(TPO->getType());
3955
3956 if (llvm::GlobalVariable *GV = getModule().getNamedGlobal(Name))
3957 return ConstantAddress(GV, GV->getValueType(), Alignment);
3958
3959 ConstantEmitter Emitter(*this);
3960 llvm::Constant *Init = Emitter.emitForInitializer(
3961 TPO->getValue(), TPO->getType().getAddressSpace(), TPO->getType());
3962
3963 if (!Init) {
3964 ErrorUnsupported(TPO, "template parameter object");
3965 return ConstantAddress::invalid();
3966 }
3967
3968 llvm::GlobalValue::LinkageTypes Linkage =
3970 ? llvm::GlobalValue::LinkOnceODRLinkage
3971 : llvm::GlobalValue::InternalLinkage;
3972 auto *GV = new llvm::GlobalVariable(getModule(), Init->getType(),
3973 /*isConstant=*/true, Linkage, Init, Name);
3974 setGVProperties(GV, TPO);
3975 if (supportsCOMDAT() && Linkage == llvm::GlobalValue::LinkOnceODRLinkage)
3976 GV->setComdat(TheModule.getOrInsertComdat(GV->getName()));
3977 Emitter.finalize(GV);
3978
3979 return ConstantAddress(GV, GV->getValueType(), Alignment);
3980}
3981
3983 const AliasAttr *AA = VD->getAttr<AliasAttr>();
3984 assert(AA && "No alias?");
3985
3986 CharUnits Alignment = getContext().getDeclAlign(VD);
3987 llvm::Type *DeclTy = getTypes().ConvertTypeForMem(VD->getType());
3988
3989 // See if there is already something with the target's name in the module.
3990 llvm::GlobalValue *Entry = GetGlobalValue(AA->getAliasee());
3991 if (Entry)
3992 return ConstantAddress(Entry, DeclTy, Alignment);
3993
3994 llvm::Constant *Aliasee;
3995 if (isa<llvm::FunctionType>(DeclTy))
3996 Aliasee = GetOrCreateLLVMFunction(AA->getAliasee(), DeclTy,
3997 GlobalDecl(cast<FunctionDecl>(VD)),
3998 /*ForVTable=*/false);
3999 else
4000 Aliasee = GetOrCreateLLVMGlobal(AA->getAliasee(), DeclTy, LangAS::Default,
4001 nullptr);
4002
4003 auto *F = cast<llvm::GlobalValue>(Aliasee);
4004 F->setLinkage(llvm::Function::ExternalWeakLinkage);
4005 WeakRefReferences.insert(F);
4006
4007 return ConstantAddress(Aliasee, DeclTy, Alignment);
4008}
4009
4010template <typename AttrT> static bool hasImplicitAttr(const ValueDecl *D) {
4011 if (!D)
4012 return false;
4013 if (auto *A = D->getAttr<AttrT>())
4014 return A->isImplicit();
4015 return D->isImplicit();
4016}
4017
4018bool CodeGenModule::shouldEmitCUDAGlobalVar(const VarDecl *Global) const {
4019 assert(LangOpts.CUDA && "Should not be called by non-CUDA languages");
4020 // We need to emit host-side 'shadows' for all global
4021 // device-side variables because the CUDA runtime needs their
4022 // size and host-side address in order to provide access to
4023 // their device-side incarnations.
4024 return !LangOpts.CUDAIsDevice || Global->hasAttr<CUDADeviceAttr>() ||
4025 Global->hasAttr<CUDAConstantAttr>() ||
4026 Global->hasAttr<CUDASharedAttr>() ||
4027 Global->getType()->isCUDADeviceBuiltinSurfaceType() ||
4028 Global->getType()->isCUDADeviceBuiltinTextureType();
4029}
4030
4032 const auto *Global = cast<ValueDecl>(GD.getDecl());
4033
4034 // Weak references don't produce any output by themselves.
4035 if (Global->hasAttr<WeakRefAttr>())
4036 return;
4037
4038 // If this is an alias definition (which otherwise looks like a declaration)
4039 // emit it now.
4040 if (Global->hasAttr<AliasAttr>())
4041 return EmitAliasDefinition(GD);
4042
4043 // IFunc like an alias whose value is resolved at runtime by calling resolver.
4044 if (Global->hasAttr<IFuncAttr>())
4045 return emitIFuncDefinition(GD);
4046
4047 // If this is a cpu_dispatch multiversion function, emit the resolver.
4048 if (Global->hasAttr<CPUDispatchAttr>())
4049 return emitCPUDispatchDefinition(GD);
4050
4051 // If this is CUDA, be selective about which declarations we emit.
4052 // Non-constexpr non-lambda implicit host device functions are not emitted
4053 // unless they are used on device side.
4054 if (LangOpts.CUDA) {
4055 assert((isa<FunctionDecl>(Global) || isa<VarDecl>(Global)) &&
4056 "Expected Variable or Function");
4057 if (const auto *VD = dyn_cast<VarDecl>(Global)) {
4058 if (!shouldEmitCUDAGlobalVar(VD))
4059 return;
4060 } else if (LangOpts.CUDAIsDevice) {
4061 const auto *FD = dyn_cast<FunctionDecl>(Global);
4062 if ((!Global->hasAttr<CUDADeviceAttr>() ||
4063 (LangOpts.OffloadImplicitHostDeviceTemplates &&
4064 hasImplicitAttr<CUDAHostAttr>(FD) &&
4065 hasImplicitAttr<CUDADeviceAttr>(FD) && !FD->isConstexpr() &&
4066 !isLambdaCallOperator(FD) &&
4067 !getContext().CUDAImplicitHostDeviceFunUsedByDevice.count(FD))) &&
4068 !Global->hasAttr<CUDAGlobalAttr>() &&
4069 !(LangOpts.HIPStdPar && isa<FunctionDecl>(Global) &&
4070 !Global->hasAttr<CUDAHostAttr>()))
4071 return;
4072 // Device-only functions are the only things we skip.
4073 } else if (!Global->hasAttr<CUDAHostAttr>() &&
4074 Global->hasAttr<CUDADeviceAttr>())
4075 return;
4076 }
4077
4078 if (LangOpts.OpenMP) {
4079 // If this is OpenMP, check if it is legal to emit this global normally.
4080 if (OpenMPRuntime && OpenMPRuntime->emitTargetGlobal(GD))
4081 return;
4082 if (auto *DRD = dyn_cast<OMPDeclareReductionDecl>(Global)) {
4083 if (MustBeEmitted(Global))
4085 return;
4086 }
4087 if (auto *DMD = dyn_cast<OMPDeclareMapperDecl>(Global)) {
4088 if (MustBeEmitted(Global))
4090 return;
4091 }
4092 }
4093
4094 // Ignore declarations, they will be emitted on their first use.
4095 if (const auto *FD = dyn_cast<FunctionDecl>(Global)) {
4096 if (DeviceKernelAttr::isOpenCLSpelling(FD->getAttr<DeviceKernelAttr>()) &&
4098 addDeferredDeclToEmit(GlobalDecl(FD, KernelReferenceKind::Stub));
4099
4100 // Update deferred annotations with the latest declaration if the function
4101 // function was already used or defined.
4102 if (FD->hasAttr<AnnotateAttr>()) {
4103 StringRef MangledName = getMangledName(GD);
4104 if (GetGlobalValue(MangledName))
4105 DeferredAnnotations[MangledName] = FD;
4106 }
4107
4108 // Forward declarations are emitted lazily on first use.
4109 if (!FD->doesThisDeclarationHaveABody()) {
4111 (!FD->isMultiVersion() || !getTarget().getTriple().isAArch64()))
4112 return;
4113
4114 StringRef MangledName = getMangledName(GD);
4115
4116 // Compute the function info and LLVM type.
4118 llvm::Type *Ty = getTypes().GetFunctionType(FI);
4119
4120 GetOrCreateLLVMFunction(MangledName, Ty, GD, /*ForVTable=*/false,
4121 /*DontDefer=*/false);
4122 return;
4123 }
4124 } else {
4125 const auto *VD = cast<VarDecl>(Global);
4126 assert(VD->isFileVarDecl() && "Cannot emit local var decl as global.");
4127 if (VD->isThisDeclarationADefinition() != VarDecl::Definition &&
4129 if (LangOpts.OpenMP) {
4130 // Emit declaration of the must-be-emitted declare target variable.
4131 if (std::optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res =
4132 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD)) {
4133
4134 // If this variable has external storage and doesn't require special
4135 // link handling we defer to its canonical definition.
4136 if (VD->hasExternalStorage() &&
4137 Res != OMPDeclareTargetDeclAttr::MT_Link)
4138 return;
4139
4140 bool UnifiedMemoryEnabled =
4142 if ((*Res == OMPDeclareTargetDeclAttr::MT_To ||
4143 *Res == OMPDeclareTargetDeclAttr::MT_Enter) &&
4144 !UnifiedMemoryEnabled) {
4145 (void)GetAddrOfGlobalVar(VD);
4146 } else {
4147 assert(((*Res == OMPDeclareTargetDeclAttr::MT_Link) ||
4148 ((*Res == OMPDeclareTargetDeclAttr::MT_To ||
4149 *Res == OMPDeclareTargetDeclAttr::MT_Enter) &&
4150 UnifiedMemoryEnabled)) &&
4151 "Link clause or to clause with unified memory expected.");
4153 }
4154
4155 return;
4156 }
4157 }
4158 // If this declaration may have caused an inline variable definition to
4159 // change linkage, make sure that it's emitted.
4160 if (Context.getInlineVariableDefinitionKind(VD) ==
4163 return;
4164 }
4165 }
4166
4167 // Defer code generation to first use when possible, e.g. if this is an inline
4168 // function. If the global must always be emitted, do it eagerly if possible
4169 // to benefit from cache locality.
4170 if (MustBeEmitted(Global) && MayBeEmittedEagerly(Global)) {
4171 // Emit the definition if it can't be deferred.
4172 EmitGlobalDefinition(GD);
4173 addEmittedDeferredDecl(GD);
4174 return;
4175 }
4176
4177 // If we're deferring emission of a C++ variable with an
4178 // initializer, remember the order in which it appeared in the file.
4179 if (getLangOpts().CPlusPlus && isa<VarDecl>(Global) &&
4180 cast<VarDecl>(Global)->hasInit()) {
4181 DelayedCXXInitPosition[Global] = CXXGlobalInits.size();
4182 CXXGlobalInits.push_back(nullptr);
4183 }
4184
4185 StringRef MangledName = getMangledName(GD);
4186 if (GetGlobalValue(MangledName) != nullptr) {
4187 // The value has already been used and should therefore be emitted.
4188 addDeferredDeclToEmit(GD);
4189 } else if (MustBeEmitted(Global)) {
4190 // The value must be emitted, but cannot be emitted eagerly.
4191 assert(!MayBeEmittedEagerly(Global));
4192 addDeferredDeclToEmit(GD);
4193 } else {
4194 // Otherwise, remember that we saw a deferred decl with this name. The
4195 // first use of the mangled name will cause it to move into
4196 // DeferredDeclsToEmit.
4197 DeferredDecls[MangledName] = GD;
4198 }
4199}
4200
4201// Check if T is a class type with a destructor that's not dllimport.
4203 if (const auto *RT =
4205 if (auto *RD = dyn_cast<CXXRecordDecl>(RT->getOriginalDecl())) {
4206 RD = RD->getDefinitionOrSelf();
4207 if (RD->getDestructor() && !RD->getDestructor()->hasAttr<DLLImportAttr>())
4208 return true;
4209 }
4210
4211 return false;
4212}
4213
4214namespace {
4215 struct FunctionIsDirectlyRecursive
4216 : public ConstStmtVisitor<FunctionIsDirectlyRecursive, bool> {
4217 const StringRef Name;
4218 const Builtin::Context &BI;
4219 FunctionIsDirectlyRecursive(StringRef N, const Builtin::Context &C)
4220 : Name(N), BI(C) {}
4221
4222 bool VisitCallExpr(const CallExpr *E) {
4223 const FunctionDecl *FD = E->getDirectCallee();
4224 if (!FD)
4225 return false;
4226 AsmLabelAttr *Attr = FD->getAttr<AsmLabelAttr>();
4227 if (Attr && Name == Attr->getLabel())
4228 return true;
4229 unsigned BuiltinID = FD->getBuiltinID();
4230 if (!BuiltinID || !BI.isLibFunction(BuiltinID))
4231 return false;
4232 std::string BuiltinNameStr = BI.getName(BuiltinID);
4233 StringRef BuiltinName = BuiltinNameStr;
4234 return BuiltinName.consume_front("__builtin_") && Name == BuiltinName;
4235 }
4236
4237 bool VisitStmt(const Stmt *S) {
4238 for (const Stmt *Child : S->children())
4239 if (Child && this->Visit(Child))
4240 return true;
4241 return false;
4242 }
4243 };
4244
4245 // Make sure we're not referencing non-imported vars or functions.
4246 struct DLLImportFunctionVisitor
4247 : public RecursiveASTVisitor<DLLImportFunctionVisitor> {
4248 bool SafeToInline = true;
4249
4250 bool shouldVisitImplicitCode() const { return true; }
4251
4252 bool VisitVarDecl(VarDecl *VD) {
4253 if (VD->getTLSKind()) {
4254 // A thread-local variable cannot be imported.
4255 SafeToInline = false;
4256 return SafeToInline;
4257 }
4258
4259 // A variable definition might imply a destructor call.
4261 SafeToInline = !HasNonDllImportDtor(VD->getType());
4262
4263 return SafeToInline;
4264 }
4265
4266 bool VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) {
4267 if (const auto *D = E->getTemporary()->getDestructor())
4268 SafeToInline = D->hasAttr<DLLImportAttr>();
4269 return SafeToInline;
4270 }
4271
4272 bool VisitDeclRefExpr(DeclRefExpr *E) {
4273 ValueDecl *VD = E->getDecl();
4274 if (isa<FunctionDecl>(VD))
4275 SafeToInline = VD->hasAttr<DLLImportAttr>();
4276 else if (VarDecl *V = dyn_cast<VarDecl>(VD))
4277 SafeToInline = !V->hasGlobalStorage() || V->hasAttr<DLLImportAttr>();
4278 return SafeToInline;
4279 }
4280
4281 bool VisitCXXConstructExpr(CXXConstructExpr *E) {
4282 SafeToInline = E->getConstructor()->hasAttr<DLLImportAttr>();
4283 return SafeToInline;
4284 }
4285
4286 bool VisitCXXMemberCallExpr(CXXMemberCallExpr *E) {
4287 CXXMethodDecl *M = E->getMethodDecl();
4288 if (!M) {
4289 // Call through a pointer to member function. This is safe to inline.
4290 SafeToInline = true;
4291 } else {
4292 SafeToInline = M->hasAttr<DLLImportAttr>();
4293 }
4294 return SafeToInline;
4295 }
4296
4297 bool VisitCXXDeleteExpr(CXXDeleteExpr *E) {
4298 SafeToInline = E->getOperatorDelete()->hasAttr<DLLImportAttr>();
4299 return SafeToInline;
4300 }
4301
4302 bool VisitCXXNewExpr(CXXNewExpr *E) {
4303 SafeToInline = E->getOperatorNew()->hasAttr<DLLImportAttr>();
4304 return SafeToInline;
4305 }
4306 };
4307}
4308
4309// isTriviallyRecursive - Check if this function calls another
4310// decl that, because of the asm attribute or the other decl being a builtin,
4311// ends up pointing to itself.
4312bool
4313CodeGenModule::isTriviallyRecursive(const FunctionDecl *FD) {
4314 StringRef Name;
4315 if (getCXXABI().getMangleContext().shouldMangleDeclName(FD)) {
4316 // asm labels are a special kind of mangling we have to support.
4317 AsmLabelAttr *Attr = FD->getAttr<AsmLabelAttr>();
4318 if (!Attr)
4319 return false;
4320 Name = Attr->getLabel();
4321 } else {
4322 Name = FD->getName();
4323 }
4324
4325 FunctionIsDirectlyRecursive Walker(Name, Context.BuiltinInfo);
4326 const Stmt *Body = FD->getBody();
4327 return Body ? Walker.Visit(Body) : false;
4328}
4329
4330bool CodeGenModule::shouldEmitFunction(GlobalDecl GD) {
4331 if (getFunctionLinkage(GD) != llvm::Function::AvailableExternallyLinkage)
4332 return true;
4333
4334 const auto *F = cast<FunctionDecl>(GD.getDecl());
4335 // Inline builtins declaration must be emitted. They often are fortified
4336 // functions.
4337 if (F->isInlineBuiltinDeclaration())
4338 return true;
4339
4340 if (CodeGenOpts.OptimizationLevel == 0 && !F->hasAttr<AlwaysInlineAttr>())
4341 return false;
4342
4343 // We don't import function bodies from other named module units since that
4344 // behavior may break ABI compatibility of the current unit.
4345 if (const Module *M = F->getOwningModule();
4346 M && M->getTopLevelModule()->isNamedModule() &&
4347 getContext().getCurrentNamedModule() != M->getTopLevelModule()) {
4348 // There are practices to mark template member function as always-inline
4349 // and mark the template as extern explicit instantiation but not give
4350 // the definition for member function. So we have to emit the function
4351 // from explicitly instantiation with always-inline.
4352 //
4353 // See https://github.com/llvm/llvm-project/issues/86893 for details.
4354 //
4355 // TODO: Maybe it is better to give it a warning if we call a non-inline
4356 // function from other module units which is marked as always-inline.
4357 if (!F->isTemplateInstantiation() || !F->hasAttr<AlwaysInlineAttr>()) {
4358 return false;
4359 }
4360 }
4361
4362 if (F->hasAttr<NoInlineAttr>())
4363 return false;
4364
4365 if (F->hasAttr<DLLImportAttr>() && !F->hasAttr<AlwaysInlineAttr>()) {
4366 // Check whether it would be safe to inline this dllimport function.
4367 DLLImportFunctionVisitor Visitor;
4368 Visitor.TraverseFunctionDecl(const_cast<FunctionDecl*>(F));
4369 if (!Visitor.SafeToInline)
4370 return false;
4371
4372 if (const CXXDestructorDecl *Dtor = dyn_cast<CXXDestructorDecl>(F)) {
4373 // Implicit destructor invocations aren't captured in the AST, so the
4374 // check above can't see them. Check for them manually here.
4375 for (const Decl *Member : Dtor->getParent()->decls())
4376 if (isa<FieldDecl>(Member))
4377 if (HasNonDllImportDtor(cast<FieldDecl>(Member)->getType()))
4378 return false;
4379 for (const CXXBaseSpecifier &B : Dtor->getParent()->bases())
4380 if (HasNonDllImportDtor(B.getType()))
4381 return false;
4382 }
4383 }
4384
4385 // PR9614. Avoid cases where the source code is lying to us. An available
4386 // externally function should have an equivalent function somewhere else,
4387 // but a function that calls itself through asm label/`__builtin_` trickery is
4388 // clearly not equivalent to the real implementation.
4389 // This happens in glibc's btowc and in some configure checks.
4390 return !isTriviallyRecursive(F);
4391}
4392
4393bool CodeGenModule::shouldOpportunisticallyEmitVTables() {
4394 return CodeGenOpts.OptimizationLevel > 0;
4395}
4396
4397void CodeGenModule::EmitMultiVersionFunctionDefinition(GlobalDecl GD,
4398 llvm::GlobalValue *GV) {
4399 const auto *FD = cast<FunctionDecl>(GD.getDecl());
4400
4401 if (FD->isCPUSpecificMultiVersion()) {
4402 auto *Spec = FD->getAttr<CPUSpecificAttr>();
4403 for (unsigned I = 0; I < Spec->cpus_size(); ++I)
4404 EmitGlobalFunctionDefinition(GD.getWithMultiVersionIndex(I), nullptr);
4405 } else if (auto *TC = FD->getAttr<TargetClonesAttr>()) {
4406 for (unsigned I = 0; I < TC->featuresStrs_size(); ++I)
4407 if (TC->isFirstOfVersion(I))
4408 EmitGlobalFunctionDefinition(GD.getWithMultiVersionIndex(I), nullptr);
4409 } else
4410 EmitGlobalFunctionDefinition(GD, GV);
4411
4412 // Ensure that the resolver function is also emitted.
4414 // On AArch64 defer the resolver emission until the entire TU is processed.
4415 if (getTarget().getTriple().isAArch64())
4416 AddDeferredMultiVersionResolverToEmit(GD);
4417 else
4418 GetOrCreateMultiVersionResolver(GD);
4419 }
4420}
4421
4422void CodeGenModule::EmitGlobalDefinition(GlobalDecl GD, llvm::GlobalValue *GV) {
4423 const auto *D = cast<ValueDecl>(GD.getDecl());
4424
4425 PrettyStackTraceDecl CrashInfo(const_cast<ValueDecl *>(D), D->getLocation(),
4426 Context.getSourceManager(),
4427 "Generating code for declaration");
4428
4429 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
4430 // At -O0, don't generate IR for functions with available_externally
4431 // linkage.
4432 if (!shouldEmitFunction(GD))
4433 return;
4434
4435 llvm::TimeTraceScope TimeScope("CodeGen Function", [&]() {
4436 std::string Name;
4437 llvm::raw_string_ostream OS(Name);
4438 FD->getNameForDiagnostic(OS, getContext().getPrintingPolicy(),
4439 /*Qualified=*/true);
4440 return Name;
4441 });
4442
4443 if (const auto *Method = dyn_cast<CXXMethodDecl>(D)) {
4444 // Make sure to emit the definition(s) before we emit the thunks.
4445 // This is necessary for the generation of certain thunks.
4446 if (isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method))
4447 ABI->emitCXXStructor(GD);
4448 else if (FD->isMultiVersion())
4449 EmitMultiVersionFunctionDefinition(GD, GV);
4450 else
4451 EmitGlobalFunctionDefinition(GD, GV);
4452
4453 if (Method->isVirtual())
4454 getVTables().EmitThunks(GD);
4455
4456 return;
4457 }
4458
4459 if (FD->isMultiVersion())
4460 return EmitMultiVersionFunctionDefinition(GD, GV);
4461 return EmitGlobalFunctionDefinition(GD, GV);
4462 }
4463
4464 if (const auto *VD = dyn_cast<VarDecl>(D))
4465 return EmitGlobalVarDefinition(VD, !VD->hasDefinition());
4466
4467 llvm_unreachable("Invalid argument to EmitGlobalDefinition()");
4468}
4469
4470static void ReplaceUsesOfNonProtoTypeWithRealFunction(llvm::GlobalValue *Old,
4471 llvm::Function *NewFn);
4472
4473static llvm::APInt
4477 if (RO.Architecture)
4478 Features.push_back(*RO.Architecture);
4479 return TI.getFMVPriority(Features);
4480}
4481
4482// Multiversion functions should be at most 'WeakODRLinkage' so that a different
4483// TU can forward declare the function without causing problems. Particularly
4484// in the cases of CPUDispatch, this causes issues. This also makes sure we
4485// work with internal linkage functions, so that the same function name can be
4486// used with internal linkage in multiple TUs.
4487static llvm::GlobalValue::LinkageTypes
4489 const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
4491 return llvm::GlobalValue::InternalLinkage;
4492 return llvm::GlobalValue::WeakODRLinkage;
4493}
4494
4495void CodeGenModule::emitMultiVersionFunctions() {
4496 std::vector<GlobalDecl> MVFuncsToEmit;
4497 MultiVersionFuncs.swap(MVFuncsToEmit);
4498 for (GlobalDecl GD : MVFuncsToEmit) {
4499 const auto *FD = cast<FunctionDecl>(GD.getDecl());
4500 assert(FD && "Expected a FunctionDecl");
4501
4502 auto createFunction = [&](const FunctionDecl *Decl, unsigned MVIdx = 0) {
4503 GlobalDecl CurGD{Decl->isDefined() ? Decl->getDefinition() : Decl, MVIdx};
4504 StringRef MangledName = getMangledName(CurGD);
4505 llvm::Constant *Func = GetGlobalValue(MangledName);
4506 if (!Func) {
4507 if (Decl->isDefined()) {
4508 EmitGlobalFunctionDefinition(CurGD, nullptr);
4509 Func = GetGlobalValue(MangledName);
4510 } else {
4512 llvm::FunctionType *Ty = getTypes().GetFunctionType(FI);
4513 Func = GetAddrOfFunction(CurGD, Ty, /*ForVTable=*/false,
4514 /*DontDefer=*/false, ForDefinition);
4515 }
4516 assert(Func && "This should have just been created");
4517 }
4518 return cast<llvm::Function>(Func);
4519 };
4520
4521 // For AArch64, a resolver is only emitted if a function marked with
4522 // target_version("default")) or target_clones("default") is defined
4523 // in this TU. For other architectures it is always emitted.
4524 bool ShouldEmitResolver = !getTarget().getTriple().isAArch64();
4526
4528 FD, [&](const FunctionDecl *CurFD) {
4530 bool IsDefined = CurFD->getDefinition() != nullptr;
4531
4532 if (const auto *TA = CurFD->getAttr<TargetAttr>()) {
4533 assert(getTarget().getTriple().isX86() && "Unsupported target");
4534 TA->getX86AddedFeatures(Feats);
4535 llvm::Function *Func = createFunction(CurFD);
4536 Options.emplace_back(Func, Feats, TA->getX86Architecture());
4537 } else if (const auto *TVA = CurFD->getAttr<TargetVersionAttr>()) {
4538 if (TVA->isDefaultVersion() && IsDefined)
4539 ShouldEmitResolver = true;
4540 llvm::Function *Func = createFunction(CurFD);
4541 char Delim = getTarget().getTriple().isAArch64() ? '+' : ',';
4542 TVA->getFeatures(Feats, Delim);
4543 Options.emplace_back(Func, Feats);
4544 } else if (const auto *TC = CurFD->getAttr<TargetClonesAttr>()) {
4545 for (unsigned I = 0; I < TC->featuresStrs_size(); ++I) {
4546 if (!TC->isFirstOfVersion(I))
4547 continue;
4548 if (TC->isDefaultVersion(I) && IsDefined)
4549 ShouldEmitResolver = true;
4550 llvm::Function *Func = createFunction(CurFD, I);
4551 Feats.clear();
4552 if (getTarget().getTriple().isX86()) {
4553 TC->getX86Feature(Feats, I);
4554 Options.emplace_back(Func, Feats, TC->getX86Architecture(I));
4555 } else {
4556 char Delim = getTarget().getTriple().isAArch64() ? '+' : ',';
4557 TC->getFeatures(Feats, I, Delim);
4558 Options.emplace_back(Func, Feats);
4559 }
4560 }
4561 } else
4562 llvm_unreachable("unexpected MultiVersionKind");
4563 });
4564
4565 if (!ShouldEmitResolver)
4566 continue;
4567
4568 llvm::Constant *ResolverConstant = GetOrCreateMultiVersionResolver(GD);
4569 if (auto *IFunc = dyn_cast<llvm::GlobalIFunc>(ResolverConstant)) {
4570 ResolverConstant = IFunc->getResolver();
4571 if (FD->isTargetClonesMultiVersion() &&
4572 !getTarget().getTriple().isAArch64()) {
4573 std::string MangledName = getMangledNameImpl(
4574 *this, GD, FD, /*OmitMultiVersionMangling=*/true);
4575 if (!GetGlobalValue(MangledName + ".ifunc")) {
4577 llvm::FunctionType *DeclTy = getTypes().GetFunctionType(FI);
4578 // In prior versions of Clang, the mangling for ifuncs incorrectly
4579 // included an .ifunc suffix. This alias is generated for backward
4580 // compatibility. It is deprecated, and may be removed in the future.
4581 auto *Alias = llvm::GlobalAlias::create(
4582 DeclTy, 0, getMultiversionLinkage(*this, GD),
4583 MangledName + ".ifunc", IFunc, &getModule());
4584 SetCommonAttributes(FD, Alias);
4585 }
4586 }
4587 }
4588 llvm::Function *ResolverFunc = cast<llvm::Function>(ResolverConstant);
4589
4590 ResolverFunc->setLinkage(getMultiversionLinkage(*this, GD));
4591
4592 if (!ResolverFunc->hasLocalLinkage() && supportsCOMDAT())
4593 ResolverFunc->setComdat(
4594 getModule().getOrInsertComdat(ResolverFunc->getName()));
4595
4596 const TargetInfo &TI = getTarget();
4597 llvm::stable_sort(
4598 Options, [&TI](const CodeGenFunction::FMVResolverOption &LHS,
4600 return getFMVPriority(TI, LHS).ugt(getFMVPriority(TI, RHS));
4601 });
4602 CodeGenFunction CGF(*this);
4603 CGF.EmitMultiVersionResolver(ResolverFunc, Options);
4604 }
4605
4606 // Ensure that any additions to the deferred decls list caused by emitting a
4607 // variant are emitted. This can happen when the variant itself is inline and
4608 // calls a function without linkage.
4609 if (!MVFuncsToEmit.empty())
4610 EmitDeferred();
4611
4612 // Ensure that any additions to the multiversion funcs list from either the
4613 // deferred decls or the multiversion functions themselves are emitted.
4614 if (!MultiVersionFuncs.empty())
4615 emitMultiVersionFunctions();
4616}
4617
4618static void replaceDeclarationWith(llvm::GlobalValue *Old,
4619 llvm::Constant *New) {
4620 assert(cast<llvm::Function>(Old)->isDeclaration() && "Not a declaration");
4621 New->takeName(Old);
4622 Old->replaceAllUsesWith(New);
4623 Old->eraseFromParent();
4624}
4625
4626void CodeGenModule::emitCPUDispatchDefinition(GlobalDecl GD) {
4627 const auto *FD = cast<FunctionDecl>(GD.getDecl());
4628 assert(FD && "Not a FunctionDecl?");
4629 assert(FD->isCPUDispatchMultiVersion() && "Not a multiversion function?");
4630 const auto *DD = FD->getAttr<CPUDispatchAttr>();
4631 assert(DD && "Not a cpu_dispatch Function?");
4632
4634 llvm::FunctionType *DeclTy = getTypes().GetFunctionType(FI);
4635
4636 StringRef ResolverName = getMangledName(GD);
4637 UpdateMultiVersionNames(GD, FD, ResolverName);
4638
4639 llvm::Type *ResolverType;
4640 GlobalDecl ResolverGD;
4641 if (getTarget().supportsIFunc()) {
4642 ResolverType = llvm::FunctionType::get(
4643 llvm::PointerType::get(getLLVMContext(),
4644 getTypes().getTargetAddressSpace(FD->getType())),
4645 false);
4646 }
4647 else {
4648 ResolverType = DeclTy;
4649 ResolverGD = GD;
4650 }
4651
4652 auto *ResolverFunc = cast<llvm::Function>(GetOrCreateLLVMFunction(
4653 ResolverName, ResolverType, ResolverGD, /*ForVTable=*/false));
4654 ResolverFunc->setLinkage(getMultiversionLinkage(*this, GD));
4655 if (supportsCOMDAT())
4656 ResolverFunc->setComdat(
4657 getModule().getOrInsertComdat(ResolverFunc->getName()));
4658
4660 const TargetInfo &Target = getTarget();
4661 unsigned Index = 0;
4662 for (const IdentifierInfo *II : DD->cpus()) {
4663 // Get the name of the target function so we can look it up/create it.
4664 std::string MangledName = getMangledNameImpl(*this, GD, FD, true) +
4665 getCPUSpecificMangling(*this, II->getName());
4666
4667 llvm::Constant *Func = GetGlobalValue(MangledName);
4668
4669 if (!Func) {
4670 GlobalDecl ExistingDecl = Manglings.lookup(MangledName);
4671 if (ExistingDecl.getDecl() &&
4672 ExistingDecl.getDecl()->getAsFunction()->isDefined()) {
4673 EmitGlobalFunctionDefinition(ExistingDecl, nullptr);
4674 Func = GetGlobalValue(MangledName);
4675 } else {
4676 if (!ExistingDecl.getDecl())
4677 ExistingDecl = GD.getWithMultiVersionIndex(Index);
4678
4679 Func = GetOrCreateLLVMFunction(
4680 MangledName, DeclTy, ExistingDecl,
4681 /*ForVTable=*/false, /*DontDefer=*/true,
4682 /*IsThunk=*/false, llvm::AttributeList(), ForDefinition);
4683 }
4684 }
4685
4687 Target.getCPUSpecificCPUDispatchFeatures(II->getName(), Features);
4688 llvm::transform(Features, Features.begin(),
4689 [](StringRef Str) { return Str.substr(1); });
4690 llvm::erase_if(Features, [&Target](StringRef Feat) {
4691 return !Target.validateCpuSupports(Feat);
4692 });
4693 Options.emplace_back(cast<llvm::Function>(Func), Features);
4694 ++Index;
4695 }
4696
4697 llvm::stable_sort(Options, [](const CodeGenFunction::FMVResolverOption &LHS,
4699 return llvm::X86::getCpuSupportsMask(LHS.Features) >
4700 llvm::X86::getCpuSupportsMask(RHS.Features);
4701 });
4702
4703 // If the list contains multiple 'default' versions, such as when it contains
4704 // 'pentium' and 'generic', don't emit the call to the generic one (since we
4705 // always run on at least a 'pentium'). We do this by deleting the 'least
4706 // advanced' (read, lowest mangling letter).
4707 while (Options.size() > 1 && llvm::all_of(llvm::X86::getCpuSupportsMask(
4708 (Options.end() - 2)->Features),
4709 [](auto X) { return X == 0; })) {
4710 StringRef LHSName = (Options.end() - 2)->Function->getName();
4711 StringRef RHSName = (Options.end() - 1)->Function->getName();
4712 if (LHSName.compare(RHSName) < 0)
4713 Options.erase(Options.end() - 2);
4714 else
4715 Options.erase(Options.end() - 1);
4716 }
4717
4718 CodeGenFunction CGF(*this);
4719 CGF.EmitMultiVersionResolver(ResolverFunc, Options);
4720
4721 if (getTarget().supportsIFunc()) {
4722 llvm::GlobalValue::LinkageTypes Linkage = getMultiversionLinkage(*this, GD);
4723 auto *IFunc = cast<llvm::GlobalValue>(GetOrCreateMultiVersionResolver(GD));
4724 unsigned AS = IFunc->getType()->getPointerAddressSpace();
4725
4726 // Fix up function declarations that were created for cpu_specific before
4727 // cpu_dispatch was known
4728 if (!isa<llvm::GlobalIFunc>(IFunc)) {
4729 auto *GI = llvm::GlobalIFunc::create(DeclTy, AS, Linkage, "",
4730 ResolverFunc, &getModule());
4731 replaceDeclarationWith(IFunc, GI);
4732 IFunc = GI;
4733 }
4734
4735 std::string AliasName = getMangledNameImpl(
4736 *this, GD, FD, /*OmitMultiVersionMangling=*/true);
4737 llvm::Constant *AliasFunc = GetGlobalValue(AliasName);
4738 if (!AliasFunc) {
4739 auto *GA = llvm::GlobalAlias::create(DeclTy, AS, Linkage, AliasName,
4740 IFunc, &getModule());
4741 SetCommonAttributes(GD, GA);
4742 }
4743 }
4744}
4745
4746/// Adds a declaration to the list of multi version functions if not present.
4747void CodeGenModule::AddDeferredMultiVersionResolverToEmit(GlobalDecl GD) {
4748 const auto *FD = cast<FunctionDecl>(GD.getDecl());
4749 assert(FD && "Not a FunctionDecl?");
4750
4752 std::string MangledName =
4753 getMangledNameImpl(*this, GD, FD, /*OmitMultiVersionMangling=*/true);
4754 if (!DeferredResolversToEmit.insert(MangledName).second)
4755 return;
4756 }
4757 MultiVersionFuncs.push_back(GD);
4758}
4759
4760/// If a dispatcher for the specified mangled name is not in the module, create
4761/// and return it. The dispatcher is either an llvm Function with the specified
4762/// type, or a global ifunc.
4763llvm::Constant *CodeGenModule::GetOrCreateMultiVersionResolver(GlobalDecl GD) {
4764 const auto *FD = cast<FunctionDecl>(GD.getDecl());
4765 assert(FD && "Not a FunctionDecl?");
4766
4767 std::string MangledName =
4768 getMangledNameImpl(*this, GD, FD, /*OmitMultiVersionMangling=*/true);
4769
4770 // Holds the name of the resolver, in ifunc mode this is the ifunc (which has
4771 // a separate resolver).
4772 std::string ResolverName = MangledName;
4773 if (getTarget().supportsIFunc()) {
4774 switch (FD->getMultiVersionKind()) {
4776 llvm_unreachable("unexpected MultiVersionKind::None for resolver");
4780 ResolverName += ".ifunc";
4781 break;
4784 break;
4785 }
4786 } else if (FD->isTargetMultiVersion()) {
4787 ResolverName += ".resolver";
4788 }
4789
4790 bool ShouldReturnIFunc =
4792
4793 // If the resolver has already been created, just return it. This lookup may
4794 // yield a function declaration instead of a resolver on AArch64. That is
4795 // because we didn't know whether a resolver will be generated when we first
4796 // encountered a use of the symbol named after this resolver. Therefore,
4797 // targets which support ifuncs should not return here unless we actually
4798 // found an ifunc.
4799 llvm::GlobalValue *ResolverGV = GetGlobalValue(ResolverName);
4800 if (ResolverGV && (isa<llvm::GlobalIFunc>(ResolverGV) || !ShouldReturnIFunc))
4801 return ResolverGV;
4802
4804 llvm::FunctionType *DeclTy = getTypes().GetFunctionType(FI);
4805
4806 // The resolver needs to be created. For target and target_clones, defer
4807 // creation until the end of the TU.
4809 AddDeferredMultiVersionResolverToEmit(GD);
4810
4811 // For cpu_specific, don't create an ifunc yet because we don't know if the
4812 // cpu_dispatch will be emitted in this translation unit.
4813 if (ShouldReturnIFunc) {
4814 unsigned AS = getTypes().getTargetAddressSpace(FD->getType());
4815 llvm::Type *ResolverType = llvm::FunctionType::get(
4816 llvm::PointerType::get(getLLVMContext(), AS), false);
4817 llvm::Constant *Resolver = GetOrCreateLLVMFunction(
4818 MangledName + ".resolver", ResolverType, GlobalDecl{},
4819 /*ForVTable=*/false);
4820 llvm::GlobalIFunc *GIF =
4821 llvm::GlobalIFunc::create(DeclTy, AS, getMultiversionLinkage(*this, GD),
4822 "", Resolver, &getModule());
4823 GIF->setName(ResolverName);
4824 SetCommonAttributes(FD, GIF);
4825 if (ResolverGV)
4826 replaceDeclarationWith(ResolverGV, GIF);
4827 return GIF;
4828 }
4829
4830 llvm::Constant *Resolver = GetOrCreateLLVMFunction(
4831 ResolverName, DeclTy, GlobalDecl{}, /*ForVTable=*/false);
4832 assert(isa<llvm::GlobalValue>(Resolver) && !ResolverGV &&
4833 "Resolver should be created for the first time");
4834 SetCommonAttributes(FD, cast<llvm::GlobalValue>(Resolver));
4835 return Resolver;
4836}
4837
4838bool CodeGenModule::shouldDropDLLAttribute(const Decl *D,
4839 const llvm::GlobalValue *GV) const {
4840 auto SC = GV->getDLLStorageClass();
4841 if (SC == llvm::GlobalValue::DefaultStorageClass)
4842 return false;
4843 const Decl *MRD = D->getMostRecentDecl();
4844 return (((SC == llvm::GlobalValue::DLLImportStorageClass &&
4845 !MRD->hasAttr<DLLImportAttr>()) ||
4846 (SC == llvm::GlobalValue::DLLExportStorageClass &&
4847 !MRD->hasAttr<DLLExportAttr>())) &&
4848 !shouldMapVisibilityToDLLExport(cast<NamedDecl>(MRD)));
4849}
4850
4851/// GetOrCreateLLVMFunction - If the specified mangled name is not in the
4852/// module, create and return an llvm Function with the specified type. If there
4853/// is something in the module with the specified name, return it potentially
4854/// bitcasted to the right type.
4855///
4856/// If D is non-null, it specifies a decl that correspond to this. This is used
4857/// to set the attributes on the function when it is first created.
4858llvm::Constant *CodeGenModule::GetOrCreateLLVMFunction(
4859 StringRef MangledName, llvm::Type *Ty, GlobalDecl GD, bool ForVTable,
4860 bool DontDefer, bool IsThunk, llvm::AttributeList ExtraAttrs,
4861 ForDefinition_t IsForDefinition) {
4862 const Decl *D = GD.getDecl();
4863
4864 std::string NameWithoutMultiVersionMangling;
4865 if (const FunctionDecl *FD = cast_or_null<FunctionDecl>(D)) {
4866 // For the device mark the function as one that should be emitted.
4867 if (getLangOpts().OpenMPIsTargetDevice && OpenMPRuntime &&
4868 !OpenMPRuntime->markAsGlobalTarget(GD) && FD->isDefined() &&
4869 !DontDefer && !IsForDefinition) {
4870 if (const FunctionDecl *FDDef = FD->getDefinition()) {
4871 GlobalDecl GDDef;
4872 if (const auto *CD = dyn_cast<CXXConstructorDecl>(FDDef))
4873 GDDef = GlobalDecl(CD, GD.getCtorType());
4874 else if (const auto *DD = dyn_cast<CXXDestructorDecl>(FDDef))
4875 GDDef = GlobalDecl(DD, GD.getDtorType());
4876 else
4877 GDDef = GlobalDecl(FDDef);
4878 EmitGlobal(GDDef);
4879 }
4880 }
4881
4882 // Any attempts to use a MultiVersion function should result in retrieving
4883 // the iFunc instead. Name Mangling will handle the rest of the changes.
4884 if (FD->isMultiVersion()) {
4885 UpdateMultiVersionNames(GD, FD, MangledName);
4886 if (!IsForDefinition) {
4887 // On AArch64 we do not immediatelly emit an ifunc resolver when a
4888 // function is used. Instead we defer the emission until we see a
4889 // default definition. In the meantime we just reference the symbol
4890 // without FMV mangling (it may or may not be replaced later).
4891 if (getTarget().getTriple().isAArch64()) {
4892 AddDeferredMultiVersionResolverToEmit(GD);
4893 NameWithoutMultiVersionMangling = getMangledNameImpl(
4894 *this, GD, FD, /*OmitMultiVersionMangling=*/true);
4895 } else
4896 return GetOrCreateMultiVersionResolver(GD);
4897 }
4898 }
4899 }
4900
4901 if (!NameWithoutMultiVersionMangling.empty())
4902 MangledName = NameWithoutMultiVersionMangling;
4903
4904 // Lookup the entry, lazily creating it if necessary.
4905 llvm::GlobalValue *Entry = GetGlobalValue(MangledName);
4906 if (Entry) {
4907 if (WeakRefReferences.erase(Entry)) {
4908 const FunctionDecl *FD = cast_or_null<FunctionDecl>(D);
4909 if (FD && !FD->hasAttr<WeakAttr>())
4910 Entry->setLinkage(llvm::Function::ExternalLinkage);
4911 }
4912
4913 // Handle dropped DLL attributes.
4914 if (D && shouldDropDLLAttribute(D, Entry)) {
4915 Entry->setDLLStorageClass(llvm::GlobalValue::DefaultStorageClass);
4916 setDSOLocal(Entry);
4917 }
4918
4919 // If there are two attempts to define the same mangled name, issue an
4920 // error.
4921 if (IsForDefinition && !Entry->isDeclaration()) {
4922 GlobalDecl OtherGD;
4923 // Check that GD is not yet in DiagnosedConflictingDefinitions is required
4924 // to make sure that we issue an error only once.
4925 if (lookupRepresentativeDecl(MangledName, OtherGD) &&
4926 (GD.getCanonicalDecl().getDecl() !=
4927 OtherGD.getCanonicalDecl().getDecl()) &&
4928 DiagnosedConflictingDefinitions.insert(GD).second) {
4929 getDiags().Report(D->getLocation(), diag::err_duplicate_mangled_name)
4930 << MangledName;
4931 getDiags().Report(OtherGD.getDecl()->getLocation(),
4932 diag::note_previous_definition);
4933 }
4934 }
4935
4936 if ((isa<llvm::Function>(Entry) || isa<llvm::GlobalAlias>(Entry)) &&
4937 (Entry->getValueType() == Ty)) {
4938 return Entry;
4939 }
4940
4941 // Make sure the result is of the correct type.
4942 // (If function is requested for a definition, we always need to create a new
4943 // function, not just return a bitcast.)
4944 if (!IsForDefinition)
4945 return Entry;
4946 }
4947
4948 // This function doesn't have a complete type (for example, the return
4949 // type is an incomplete struct). Use a fake type instead, and make
4950 // sure not to try to set attributes.
4951 bool IsIncompleteFunction = false;
4952
4953 llvm::FunctionType *FTy;
4954 if (isa<llvm::FunctionType>(Ty)) {
4955 FTy = cast<llvm::FunctionType>(Ty);
4956 } else {
4957 FTy = llvm::FunctionType::get(VoidTy, false);
4958 IsIncompleteFunction = true;
4959 }
4960
4961 llvm::Function *F =
4962 llvm::Function::Create(FTy, llvm::Function::ExternalLinkage,
4963 Entry ? StringRef() : MangledName, &getModule());
4964
4965 // Store the declaration associated with this function so it is potentially
4966 // updated by further declarations or definitions and emitted at the end.
4967 if (D && D->hasAttr<AnnotateAttr>())
4968 DeferredAnnotations[MangledName] = cast<ValueDecl>(D);
4969
4970 // If we already created a function with the same mangled name (but different
4971 // type) before, take its name and add it to the list of functions to be
4972 // replaced with F at the end of CodeGen.
4973 //
4974 // This happens if there is a prototype for a function (e.g. "int f()") and
4975 // then a definition of a different type (e.g. "int f(int x)").
4976 if (Entry) {
4977 F->takeName(Entry);
4978
4979 // This might be an implementation of a function without a prototype, in
4980 // which case, try to do special replacement of calls which match the new
4981 // prototype. The really key thing here is that we also potentially drop
4982 // arguments from the call site so as to make a direct call, which makes the
4983 // inliner happier and suppresses a number of optimizer warnings (!) about
4984 // dropping arguments.
4985 if (!Entry->use_empty()) {
4987 Entry->removeDeadConstantUsers();
4988 }
4989
4990 addGlobalValReplacement(Entry, F);
4991 }
4992
4993 assert(F->getName() == MangledName && "name was uniqued!");
4994 if (D)
4995 SetFunctionAttributes(GD, F, IsIncompleteFunction, IsThunk);
4996 if (ExtraAttrs.hasFnAttrs()) {
4997 llvm::AttrBuilder B(F->getContext(), ExtraAttrs.getFnAttrs());
4998 F->addFnAttrs(B);
4999 }
5000
5001 if (!DontDefer) {
5002 // All MSVC dtors other than the base dtor are linkonce_odr and delegate to
5003 // each other bottoming out with the base dtor. Therefore we emit non-base
5004 // dtors on usage, even if there is no dtor definition in the TU.
5005 if (isa_and_nonnull<CXXDestructorDecl>(D) &&
5006 getCXXABI().useThunkForDtorVariant(cast<CXXDestructorDecl>(D),
5007 GD.getDtorType()))
5008 addDeferredDeclToEmit(GD);
5009
5010 // This is the first use or definition of a mangled name. If there is a
5011 // deferred decl with this name, remember that we need to emit it at the end
5012 // of the file.
5013 auto DDI = DeferredDecls.find(MangledName);
5014 if (DDI != DeferredDecls.end()) {
5015 // Move the potentially referenced deferred decl to the
5016 // DeferredDeclsToEmit list, and remove it from DeferredDecls (since we
5017 // don't need it anymore).
5018 addDeferredDeclToEmit(DDI->second);
5019 DeferredDecls.erase(DDI);
5020
5021 // Otherwise, there are cases we have to worry about where we're
5022 // using a declaration for which we must emit a definition but where
5023 // we might not find a top-level definition:
5024 // - member functions defined inline in their classes
5025 // - friend functions defined inline in some class
5026 // - special member functions with implicit definitions
5027 // If we ever change our AST traversal to walk into class methods,
5028 // this will be unnecessary.
5029 //
5030 // We also don't emit a definition for a function if it's going to be an
5031 // entry in a vtable, unless it's already marked as used.
5032 } else if (getLangOpts().CPlusPlus && D) {
5033 // Look for a declaration that's lexically in a record.
5034 for (const auto *FD = cast<FunctionDecl>(D)->getMostRecentDecl(); FD;
5035 FD = FD->getPreviousDecl()) {
5036 if (isa<CXXRecordDecl>(FD->getLexicalDeclContext())) {
5037 if (FD->doesThisDeclarationHaveABody()) {
5038 addDeferredDeclToEmit(GD.getWithDecl(FD));
5039 break;
5040 }
5041 }
5042 }
5043 }
5044 }
5045
5046 // Make sure the result is of the requested type.
5047 if (!IsIncompleteFunction) {
5048 assert(F->getFunctionType() == Ty);
5049 return F;
5050 }
5051
5052 return F;
5053}
5054
5055/// GetAddrOfFunction - Return the address of the given function. If Ty is
5056/// non-null, then this function will use the specified type if it has to
5057/// create it (this occurs when we see a definition of the function).
5058llvm::Constant *
5059CodeGenModule::GetAddrOfFunction(GlobalDecl GD, llvm::Type *Ty, bool ForVTable,
5060 bool DontDefer,
5061 ForDefinition_t IsForDefinition) {
5062 // If there was no specific requested type, just convert it now.
5063 if (!Ty) {
5064 const auto *FD = cast<FunctionDecl>(GD.getDecl());
5065 Ty = getTypes().ConvertType(FD->getType());
5066 if (DeviceKernelAttr::isOpenCLSpelling(FD->getAttr<DeviceKernelAttr>()) &&
5069 Ty = getTypes().GetFunctionType(FI);
5070 }
5071 }
5072
5073 // Devirtualized destructor calls may come through here instead of via
5074 // getAddrOfCXXStructor. Make sure we use the MS ABI base destructor instead
5075 // of the complete destructor when necessary.
5076 if (const auto *DD = dyn_cast<CXXDestructorDecl>(GD.getDecl())) {
5077 if (getTarget().getCXXABI().isMicrosoft() &&
5078 GD.getDtorType() == Dtor_Complete &&
5079 DD->getParent()->getNumVBases() == 0)
5080 GD = GlobalDecl(DD, Dtor_Base);
5081 }
5082
5083 StringRef MangledName = getMangledName(GD);
5084 auto *F = GetOrCreateLLVMFunction(MangledName, Ty, GD, ForVTable, DontDefer,
5085 /*IsThunk=*/false, llvm::AttributeList(),
5086 IsForDefinition);
5087 // Returns kernel handle for HIP kernel stub function.
5088 if (LangOpts.CUDA && !LangOpts.CUDAIsDevice &&
5089 cast<FunctionDecl>(GD.getDecl())->hasAttr<CUDAGlobalAttr>()) {
5090 auto *Handle = getCUDARuntime().getKernelHandle(
5091 cast<llvm::Function>(F->stripPointerCasts()), GD);
5092 if (IsForDefinition)
5093 return F;
5094 return Handle;
5095 }
5096 return F;
5097}
5098
5100 llvm::GlobalValue *F =
5101 cast<llvm::GlobalValue>(GetAddrOfFunction(Decl)->stripPointerCasts());
5102
5103 return llvm::NoCFIValue::get(F);
5104}
5105
5106static const FunctionDecl *
5108 TranslationUnitDecl *TUDecl = C.getTranslationUnitDecl();
5110
5111 IdentifierInfo &CII = C.Idents.get(Name);
5112 for (const auto *Result : DC->lookup(&CII))
5113 if (const auto *FD = dyn_cast<FunctionDecl>(Result))
5114 return FD;
5115
5116 if (!C.getLangOpts().CPlusPlus)
5117 return nullptr;
5118
5119 // Demangle the premangled name from getTerminateFn()
5120 IdentifierInfo &CXXII =
5121 (Name == "_ZSt9terminatev" || Name == "?terminate@@YAXXZ")
5122 ? C.Idents.get("terminate")
5123 : C.Idents.get(Name);
5124
5125 for (const auto &N : {"__cxxabiv1", "std"}) {
5126 IdentifierInfo &NS = C.Idents.get(N);
5127 for (const auto *Result : DC->lookup(&NS)) {
5128 const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(Result);
5129 if (auto *LSD = dyn_cast<LinkageSpecDecl>(Result))
5130 for (const auto *Result : LSD->lookup(&NS))
5131 if ((ND = dyn_cast<NamespaceDecl>(Result)))
5132 break;
5133
5134 if (ND)
5135 for (const auto *Result : ND->lookup(&CXXII))
5136 if (const auto *FD = dyn_cast<FunctionDecl>(Result))
5137 return FD;
5138 }
5139 }
5140
5141 return nullptr;
5142}
5143
5144static void setWindowsItaniumDLLImport(CodeGenModule &CGM, bool Local,
5145 llvm::Function *F, StringRef Name) {
5146 // In Windows Itanium environments, try to mark runtime functions
5147 // dllimport. For Mingw and MSVC, don't. We don't really know if the user
5148 // will link their standard library statically or dynamically. Marking
5149 // functions imported when they are not imported can cause linker errors
5150 // and warnings.
5151 if (!Local && CGM.getTriple().isWindowsItaniumEnvironment() &&
5152 !CGM.getCodeGenOpts().LTOVisibilityPublicStd) {
5153 const FunctionDecl *FD = GetRuntimeFunctionDecl(CGM.getContext(), Name);
5154 if (!FD || FD->hasAttr<DLLImportAttr>()) {
5155 F->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass);
5156 F->setLinkage(llvm::GlobalValue::ExternalLinkage);
5157 }
5158 }
5159}
5160
5162 QualType ReturnTy, ArrayRef<QualType> ArgTys, StringRef Name,
5163 llvm::AttributeList ExtraAttrs, bool Local, bool AssumeConvergent) {
5164 if (AssumeConvergent) {
5165 ExtraAttrs =
5166 ExtraAttrs.addFnAttribute(VMContext, llvm::Attribute::Convergent);
5167 }
5168
5169 QualType FTy = Context.getFunctionType(ReturnTy, ArgTys,
5172 Context.getCanonicalType(FTy).castAs<FunctionProtoType>());
5173 auto *ConvTy = getTypes().GetFunctionType(Info);
5174 llvm::Constant *C = GetOrCreateLLVMFunction(
5175 Name, ConvTy, GlobalDecl(), /*ForVTable=*/false,
5176 /*DontDefer=*/false, /*IsThunk=*/false, ExtraAttrs);
5177
5178 if (auto *F = dyn_cast<llvm::Function>(C)) {
5179 if (F->empty()) {
5180 SetLLVMFunctionAttributes(GlobalDecl(), Info, F, /*IsThunk*/ false);
5181 // FIXME: Set calling-conv properly in ExtProtoInfo
5182 F->setCallingConv(getRuntimeCC());
5183 setWindowsItaniumDLLImport(*this, Local, F, Name);
5184 setDSOLocal(F);
5185 }
5186 }
5187 return {ConvTy, C};
5188}
5189
5190/// CreateRuntimeFunction - Create a new runtime function with the specified
5191/// type and name.
5192llvm::FunctionCallee
5193CodeGenModule::CreateRuntimeFunction(llvm::FunctionType *FTy, StringRef Name,
5194 llvm::AttributeList ExtraAttrs, bool Local,
5195 bool AssumeConvergent) {
5196 if (AssumeConvergent) {
5197 ExtraAttrs =
5198 ExtraAttrs.addFnAttribute(VMContext, llvm::Attribute::Convergent);
5199 }
5200
5201 llvm::Constant *C =
5202 GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(), /*ForVTable=*/false,
5203 /*DontDefer=*/false, /*IsThunk=*/false,
5204 ExtraAttrs);
5205
5206 if (auto *F = dyn_cast<llvm::Function>(C)) {
5207 if (F->empty()) {
5208 F->setCallingConv(getRuntimeCC());
5209 setWindowsItaniumDLLImport(*this, Local, F, Name);
5210 setDSOLocal(F);
5211 // FIXME: We should use CodeGenModule::SetLLVMFunctionAttributes() instead
5212 // of trying to approximate the attributes using the LLVM function
5213 // signature. The other overload of CreateRuntimeFunction does this; it
5214 // should be used for new code.
5215 markRegisterParameterAttributes(F);
5216 }
5217 }
5218
5219 return {FTy, C};
5220}
5221
5222/// GetOrCreateLLVMGlobal - If the specified mangled name is not in the module,
5223/// create and return an llvm GlobalVariable with the specified type and address
5224/// space. If there is something in the module with the specified name, return
5225/// it potentially bitcasted to the right type.
5226///
5227/// If D is non-null, it specifies a decl that correspond to this. This is used
5228/// to set the attributes on the global when it is first created.
5229///
5230/// If IsForDefinition is true, it is guaranteed that an actual global with
5231/// type Ty will be returned, not conversion of a variable with the same
5232/// mangled name but some other type.
5233llvm::Constant *
5234CodeGenModule::GetOrCreateLLVMGlobal(StringRef MangledName, llvm::Type *Ty,
5235 LangAS AddrSpace, const VarDecl *D,
5236 ForDefinition_t IsForDefinition) {
5237 // Lookup the entry, lazily creating it if necessary.
5238 llvm::GlobalValue *Entry = GetGlobalValue(MangledName);
5239 unsigned TargetAS = getContext().getTargetAddressSpace(AddrSpace);
5240 if (Entry) {
5241 if (WeakRefReferences.erase(Entry)) {
5242 if (D && !D->hasAttr<WeakAttr>())
5243 Entry->setLinkage(llvm::Function::ExternalLinkage);
5244 }
5245
5246 // Handle dropped DLL attributes.
5247 if (D && shouldDropDLLAttribute(D, Entry))
5248 Entry->setDLLStorageClass(llvm::GlobalValue::DefaultStorageClass);
5249
5250 if (LangOpts.OpenMP && !LangOpts.OpenMPSimd && D)
5252
5253 if (Entry->getValueType() == Ty && Entry->getAddressSpace() == TargetAS)
5254 return Entry;
5255
5256 // If there are two attempts to define the same mangled name, issue an
5257 // error.
5258 if (IsForDefinition && !Entry->isDeclaration()) {
5259 GlobalDecl OtherGD;
5260 const VarDecl *OtherD;
5261
5262 // Check that D is not yet in DiagnosedConflictingDefinitions is required
5263 // to make sure that we issue an error only once.
5264 if (D && lookupRepresentativeDecl(MangledName, OtherGD) &&
5265 (D->getCanonicalDecl() != OtherGD.getCanonicalDecl().getDecl()) &&
5266 (OtherD = dyn_cast<VarDecl>(OtherGD.getDecl())) &&
5267 OtherD->hasInit() &&
5268 DiagnosedConflictingDefinitions.insert(D).second) {
5269 getDiags().Report(D->getLocation(), diag::err_duplicate_mangled_name)
5270 << MangledName;
5271 getDiags().Report(OtherGD.getDecl()->getLocation(),
5272 diag::note_previous_definition);
5273 }
5274 }
5275
5276 // Make sure the result is of the correct type.
5277 if (Entry->getType()->getAddressSpace() != TargetAS)
5278 return llvm::ConstantExpr::getAddrSpaceCast(
5279 Entry, llvm::PointerType::get(Ty->getContext(), TargetAS));
5280
5281 // (If global is requested for a definition, we always need to create a new
5282 // global, not just return a bitcast.)
5283 if (!IsForDefinition)
5284 return Entry;
5285 }
5286
5287 auto DAddrSpace = GetGlobalVarAddressSpace(D);
5288
5289 auto *GV = new llvm::GlobalVariable(
5290 getModule(), Ty, false, llvm::GlobalValue::ExternalLinkage, nullptr,
5291 MangledName, nullptr, llvm::GlobalVariable::NotThreadLocal,
5292 getContext().getTargetAddressSpace(DAddrSpace));
5293
5294 // If we already created a global with the same mangled name (but different
5295 // type) before, take its name and remove it from its parent.
5296 if (Entry) {
5297 GV->takeName(Entry);
5298
5299 if (!Entry->use_empty()) {
5300 Entry->replaceAllUsesWith(GV);
5301 }
5302
5303 Entry->eraseFromParent();
5304 }
5305
5306 // This is the first use or definition of a mangled name. If there is a
5307 // deferred decl with this name, remember that we need to emit it at the end
5308 // of the file.
5309 auto DDI = DeferredDecls.find(MangledName);
5310 if (DDI != DeferredDecls.end()) {
5311 // Move the potentially referenced deferred decl to the DeferredDeclsToEmit
5312 // list, and remove it from DeferredDecls (since we don't need it anymore).
5313 addDeferredDeclToEmit(DDI->second);
5314 DeferredDecls.erase(DDI);
5315 }
5316
5317 // Handle things which are present even on external declarations.
5318 if (D) {
5319 if (LangOpts.OpenMP && !LangOpts.OpenMPSimd)
5321
5322 // FIXME: This code is overly simple and should be merged with other global
5323 // handling.
5324 GV->setConstant(D->getType().isConstantStorage(getContext(), false, false));
5325
5326 GV->setAlignment(getContext().getDeclAlign(D).getAsAlign());
5327
5328 setLinkageForGV(GV, D);
5329
5330 if (D->getTLSKind()) {
5331 if (D->getTLSKind() == VarDecl::TLS_Dynamic)
5332 CXXThreadLocals.push_back(D);
5333 setTLSMode(GV, *D);
5334 }
5335
5336 setGVProperties(GV, D);
5337
5338 // If required by the ABI, treat declarations of static data members with
5339 // inline initializers as definitions.
5340 if (getContext().isMSStaticDataMemberInlineDefinition(D)) {
5341 EmitGlobalVarDefinition(D);
5342 }
5343
5344 // Emit section information for extern variables.
5345 if (D->hasExternalStorage()) {
5346 if (const SectionAttr *SA = D->getAttr<SectionAttr>())
5347 GV->setSection(SA->getName());
5348 }
5349
5350 // Handle XCore specific ABI requirements.
5351 if (getTriple().getArch() == llvm::Triple::xcore &&
5352 D->getLanguageLinkage() == CLanguageLinkage &&
5353 D->getType().isConstant(Context) &&
5354 isExternallyVisible(D->getLinkageAndVisibility().getLinkage()))
5355 GV->setSection(".cp.rodata");
5356
5357 // Handle code model attribute
5358 if (const auto *CMA = D->getAttr<CodeModelAttr>())
5359 GV->setCodeModel(CMA->getModel());
5360
5361 // Check if we a have a const declaration with an initializer, we may be
5362 // able to emit it as available_externally to expose it's value to the
5363 // optimizer.
5364 if (Context.getLangOpts().CPlusPlus && GV->hasExternalLinkage() &&
5365 D->getType().isConstQualified() && !GV->hasInitializer() &&
5366 !D->hasDefinition() && D->hasInit() && !D->hasAttr<DLLImportAttr>()) {
5367 const auto *Record =
5368 Context.getBaseElementType(D->getType())->getAsCXXRecordDecl();
5369 bool HasMutableFields = Record && Record->hasMutableFields();
5370 if (!HasMutableFields) {
5371 const VarDecl *InitDecl;
5372 const Expr *InitExpr = D->getAnyInitializer(InitDecl);
5373 if (InitExpr) {
5374 ConstantEmitter emitter(*this);
5375 llvm::Constant *Init = emitter.tryEmitForInitializer(*InitDecl);
5376 if (Init) {
5377 auto *InitType = Init->getType();
5378 if (GV->getValueType() != InitType) {
5379 // The type of the initializer does not match the definition.
5380 // This happens when an initializer has a different type from
5381 // the type of the global (because of padding at the end of a
5382 // structure for instance).
5383 GV->setName(StringRef());
5384 // Make a new global with the correct type, this is now guaranteed
5385 // to work.
5386 auto *NewGV = cast<llvm::GlobalVariable>(
5387 GetAddrOfGlobalVar(D, InitType, IsForDefinition)
5388 ->stripPointerCasts());
5389
5390 // Erase the old global, since it is no longer used.
5391 GV->eraseFromParent();
5392 GV = NewGV;
5393 } else {
5394 GV->setInitializer(Init);
5395 GV->setConstant(true);
5396 GV->setLinkage(llvm::GlobalValue::AvailableExternallyLinkage);
5397 }
5398 emitter.finalize(GV);
5399 }
5400 }
5401 }
5402 }
5403 }
5404
5405 if (D &&
5406 D->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly) {
5408 // External HIP managed variables needed to be recorded for transformation
5409 // in both device and host compilations.
5410 if (getLangOpts().CUDA && D && D->hasAttr<HIPManagedAttr>() &&
5411 D->hasExternalStorage())
5413 }
5414
5415 if (D)
5416 SanitizerMD->reportGlobal(GV, *D);
5417
5418 LangAS ExpectedAS =
5419 D ? D->getType().getAddressSpace()
5420 : (LangOpts.OpenCL ? LangAS::opencl_global : LangAS::Default);
5421 assert(getContext().getTargetAddressSpace(ExpectedAS) == TargetAS);
5422 if (DAddrSpace != ExpectedAS) {
5424 *this, GV, DAddrSpace,
5425 llvm::PointerType::get(getLLVMContext(), TargetAS));
5426 }
5427
5428 return GV;
5429}
5430
5431llvm::Constant *
5433 const Decl *D = GD.getDecl();
5434
5435 if (isa<CXXConstructorDecl>(D) || isa<CXXDestructorDecl>(D))
5436 return getAddrOfCXXStructor(GD, /*FnInfo=*/nullptr, /*FnType=*/nullptr,
5437 /*DontDefer=*/false, IsForDefinition);
5438
5439 if (isa<CXXMethodDecl>(D)) {
5440 auto FInfo =
5441 &getTypes().arrangeCXXMethodDeclaration(cast<CXXMethodDecl>(D));
5442 auto Ty = getTypes().GetFunctionType(*FInfo);
5443 return GetAddrOfFunction(GD, Ty, /*ForVTable=*/false, /*DontDefer=*/false,
5444 IsForDefinition);
5445 }
5446
5447 if (isa<FunctionDecl>(D)) {
5449 llvm::FunctionType *Ty = getTypes().GetFunctionType(FI);
5450 return GetAddrOfFunction(GD, Ty, /*ForVTable=*/false, /*DontDefer=*/false,
5451 IsForDefinition);
5452 }
5453
5454 return GetAddrOfGlobalVar(cast<VarDecl>(D), /*Ty=*/nullptr, IsForDefinition);
5455}
5456
5458 StringRef Name, llvm::Type *Ty, llvm::GlobalValue::LinkageTypes Linkage,
5459 llvm::Align Alignment) {
5460 llvm::GlobalVariable *GV = getModule().getNamedGlobal(Name);
5461 llvm::GlobalVariable *OldGV = nullptr;
5462
5463 if (GV) {
5464 // Check if the variable has the right type.
5465 if (GV->getValueType() == Ty)
5466 return GV;
5467
5468 // Because C++ name mangling, the only way we can end up with an already
5469 // existing global with the same name is if it has been declared extern "C".
5470 assert(GV->isDeclaration() && "Declaration has wrong type!");
5471 OldGV = GV;
5472 }
5473
5474 // Create a new variable.
5475 GV = new llvm::GlobalVariable(getModule(), Ty, /*isConstant=*/true,
5476 Linkage, nullptr, Name);
5477
5478 if (OldGV) {
5479 // Replace occurrences of the old variable if needed.
5480 GV->takeName(OldGV);
5481
5482 if (!OldGV->use_empty()) {
5483 OldGV->replaceAllUsesWith(GV);
5484 }
5485
5486 OldGV->eraseFromParent();
5487 }
5488
5489 if (supportsCOMDAT() && GV->isWeakForLinker() &&
5490 !GV->hasAvailableExternallyLinkage())
5491 GV->setComdat(TheModule.getOrInsertComdat(GV->getName()));
5492
5493 GV->setAlignment(Alignment);
5494
5495 return GV;
5496}
5497
5498/// GetAddrOfGlobalVar - Return the llvm::Constant for the address of the
5499/// given global variable. If Ty is non-null and if the global doesn't exist,
5500/// then it will be created with the specified type instead of whatever the
5501/// normal requested type would be. If IsForDefinition is true, it is guaranteed
5502/// that an actual global with type Ty will be returned, not conversion of a
5503/// variable with the same mangled name but some other type.
5505 llvm::Type *Ty,
5506 ForDefinition_t IsForDefinition) {
5507 assert(D->hasGlobalStorage() && "Not a global variable");
5508 QualType ASTTy = D->getType();
5509 if (!Ty)
5510 Ty = getTypes().ConvertTypeForMem(ASTTy);
5511
5512 StringRef MangledName = getMangledName(D);
5513 return GetOrCreateLLVMGlobal(MangledName, Ty, ASTTy.getAddressSpace(), D,
5514 IsForDefinition);
5515}
5516
5517/// CreateRuntimeVariable - Create a new runtime global variable with the
5518/// specified type and name.
5519llvm::Constant *
5521 StringRef Name) {
5522 LangAS AddrSpace = getContext().getLangOpts().OpenCL ? LangAS::opencl_global
5524 auto *Ret = GetOrCreateLLVMGlobal(Name, Ty, AddrSpace, nullptr);
5525 setDSOLocal(cast<llvm::GlobalValue>(Ret->stripPointerCasts()));
5526 return Ret;
5527}
5528
5530 assert(!D->getInit() && "Cannot emit definite definitions here!");
5531
5532 StringRef MangledName = getMangledName(D);
5533 llvm::GlobalValue *GV = GetGlobalValue(MangledName);
5534
5535 // We already have a definition, not declaration, with the same mangled name.
5536 // Emitting of declaration is not required (and actually overwrites emitted
5537 // definition).
5538 if (GV && !GV->isDeclaration())
5539 return;
5540
5541 // If we have not seen a reference to this variable yet, place it into the
5542 // deferred declarations table to be emitted if needed later.
5543 if (!MustBeEmitted(D) && !GV) {
5544 DeferredDecls[MangledName] = D;
5545 return;
5546 }
5547
5548 // The tentative definition is the only definition.
5549 EmitGlobalVarDefinition(D);
5550}
5551
5552// Return a GlobalDecl. Use the base variants for destructors and constructors.
5554 if (auto const *CD = dyn_cast<const CXXConstructorDecl>(D))
5556 else if (auto const *DD = dyn_cast<const CXXDestructorDecl>(D))
5558 return GlobalDecl(D);
5559}
5560
5563 if (!DI || !getCodeGenOpts().hasReducedDebugInfo())
5564 return;
5565
5567 if (!GD)
5568 return;
5569
5570 llvm::Constant *Addr = GetAddrOfGlobal(GD)->stripPointerCasts();
5571 if (const auto *VD = dyn_cast<VarDecl>(D)) {
5573 cast<llvm::GlobalVariable>(Addr->stripPointerCasts()), VD);
5574 } else if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
5575 llvm::Function *Fn = cast<llvm::Function>(Addr);
5576 if (!Fn->getSubprogram())
5577 DI->EmitFunctionDecl(GD, FD->getLocation(), FD->getType(), Fn);
5578 }
5579}
5580
5582 return Context.toCharUnitsFromBits(
5583 getDataLayout().getTypeStoreSizeInBits(Ty));
5584}
5585
5587 if (LangOpts.OpenCL) {
5588 LangAS AS = D ? D->getType().getAddressSpace() : LangAS::opencl_global;
5589 assert(AS == LangAS::opencl_global ||
5593 AS == LangAS::opencl_local ||
5595 return AS;
5596 }
5597
5598 if (LangOpts.SYCLIsDevice &&
5599 (!D || D->getType().getAddressSpace() == LangAS::Default))
5600 return LangAS::sycl_global;
5601
5602 if (LangOpts.CUDA && LangOpts.CUDAIsDevice) {
5603 if (D) {
5604 if (D->hasAttr<CUDAConstantAttr>())
5605 return LangAS::cuda_constant;
5606 if (D->hasAttr<CUDASharedAttr>())
5607 return LangAS::cuda_shared;
5608 if (D->hasAttr<CUDADeviceAttr>())
5609 return LangAS::cuda_device;
5610 if (D->getType().isConstQualified())
5611 return LangAS::cuda_constant;
5612 }
5613 return LangAS::cuda_device;
5614 }
5615
5616 if (LangOpts.OpenMP) {
5617 LangAS AS;
5618 if (OpenMPRuntime->hasAllocateAttributeForGlobalVar(D, AS))
5619 return AS;
5620 }
5622}
5623
5625 // OpenCL v1.2 s6.5.3: a string literal is in the constant address space.
5626 if (LangOpts.OpenCL)
5628 if (LangOpts.SYCLIsDevice)
5629 return LangAS::sycl_global;
5630 if (LangOpts.HIP && LangOpts.CUDAIsDevice && getTriple().isSPIRV())
5631 // For HIPSPV map literals to cuda_device (maps to CrossWorkGroup in SPIR-V)
5632 // instead of default AS (maps to Generic in SPIR-V). Otherwise, we end up
5633 // with OpVariable instructions with Generic storage class which is not
5634 // allowed (SPIR-V V1.6 s3.42.8). Also, mapping literals to SPIR-V
5635 // UniformConstant storage class is not viable as pointers to it may not be
5636 // casted to Generic pointers which are used to model HIP's "flat" pointers.
5637 return LangAS::cuda_device;
5638 if (auto AS = getTarget().getConstantAddressSpace())
5639 return *AS;
5640 return LangAS::Default;
5641}
5642
5643// In address space agnostic languages, string literals are in default address
5644// space in AST. However, certain targets (e.g. amdgcn) request them to be
5645// emitted in constant address space in LLVM IR. To be consistent with other
5646// parts of AST, string literal global variables in constant address space
5647// need to be casted to default address space before being put into address
5648// map and referenced by other part of CodeGen.
5649// In OpenCL, string literals are in constant address space in AST, therefore
5650// they should not be casted to default address space.
5651static llvm::Constant *
5653 llvm::GlobalVariable *GV) {
5654 llvm::Constant *Cast = GV;
5655 if (!CGM.getLangOpts().OpenCL) {
5656 auto AS = CGM.GetGlobalConstantAddressSpace();
5657 if (AS != LangAS::Default)
5659 CGM, GV, AS,
5660 llvm::PointerType::get(
5661 CGM.getLLVMContext(),
5663 }
5664 return Cast;
5665}
5666
5667template<typename SomeDecl>
5669 llvm::GlobalValue *GV) {
5670 if (!getLangOpts().CPlusPlus)
5671 return;
5672
5673 // Must have 'used' attribute, or else inline assembly can't rely on
5674 // the name existing.
5675 if (!D->template hasAttr<UsedAttr>())
5676 return;
5677
5678 // Must have internal linkage and an ordinary name.
5679 if (!D->getIdentifier() || D->getFormalLinkage() != Linkage::Internal)
5680 return;
5681
5682 // Must be in an extern "C" context. Entities declared directly within
5683 // a record are not extern "C" even if the record is in such a context.
5684 const SomeDecl *First = D->getFirstDecl();
5685 if (First->getDeclContext()->isRecord() || !First->isInExternCContext())
5686 return;
5687
5688 // OK, this is an internal linkage entity inside an extern "C" linkage
5689 // specification. Make a note of that so we can give it the "expected"
5690 // mangled name if nothing else is using that name.
5691 std::pair<StaticExternCMap::iterator, bool> R =
5692 StaticExternCValues.insert(std::make_pair(D->getIdentifier(), GV));
5693
5694 // If we have multiple internal linkage entities with the same name
5695 // in extern "C" regions, none of them gets that name.
5696 if (!R.second)
5697 R.first->second = nullptr;
5698}
5699
5700static bool shouldBeInCOMDAT(CodeGenModule &CGM, const Decl &D) {
5701 if (!CGM.supportsCOMDAT())
5702 return false;
5703
5704 if (D.hasAttr<SelectAnyAttr>())
5705 return true;
5706
5708 if (auto *VD = dyn_cast<VarDecl>(&D))
5710 else
5711 Linkage = CGM.getContext().GetGVALinkageForFunction(cast<FunctionDecl>(&D));
5712
5713 switch (Linkage) {
5714 case GVA_Internal:
5716 case GVA_StrongExternal:
5717 return false;
5718 case GVA_DiscardableODR:
5719 case GVA_StrongODR:
5720 return true;
5721 }
5722 llvm_unreachable("No such linkage");
5723}
5724
5726 return getTriple().supportsCOMDAT();
5727}
5728
5730 llvm::GlobalObject &GO) {
5731 if (!shouldBeInCOMDAT(*this, D))
5732 return;
5733 GO.setComdat(TheModule.getOrInsertComdat(GO.getName()));
5734}
5735
5738}
5739
5740/// Pass IsTentative as true if you want to create a tentative definition.
5741void CodeGenModule::EmitGlobalVarDefinition(const VarDecl *D,
5742 bool IsTentative) {
5743 // OpenCL global variables of sampler type are translated to function calls,
5744 // therefore no need to be translated.
5745 QualType ASTTy = D->getType();
5746 if (getLangOpts().OpenCL && ASTTy->isSamplerT())
5747 return;
5748
5749 // HLSL default buffer constants will be emitted during HLSLBufferDecl codegen
5750 if (getLangOpts().HLSL &&
5751 D->getType().getAddressSpace() == LangAS::hlsl_constant)
5752 return;
5753
5754 // If this is OpenMP device, check if it is legal to emit this global
5755 // normally.
5756 if (LangOpts.OpenMPIsTargetDevice && OpenMPRuntime &&
5757 OpenMPRuntime->emitTargetGlobalVariable(D))
5758 return;
5759
5760 llvm::TrackingVH<llvm::Constant> Init;
5761 bool NeedsGlobalCtor = false;
5762 // Whether the definition of the variable is available externally.
5763 // If yes, we shouldn't emit the GloablCtor and GlobalDtor for the variable
5764 // since this is the job for its original source.
5765 bool IsDefinitionAvailableExternally =
5767 bool NeedsGlobalDtor =
5768 !IsDefinitionAvailableExternally &&
5769 D->needsDestruction(getContext()) == QualType::DK_cxx_destructor;
5770
5771 // It is helpless to emit the definition for an available_externally variable
5772 // which can't be marked as const.
5773 // We don't need to check if it needs global ctor or dtor. See the above
5774 // comment for ideas.
5775 if (IsDefinitionAvailableExternally &&
5776 (!D->hasConstantInitialization() ||
5777 // TODO: Update this when we have interface to check constexpr
5778 // destructor.
5779 D->needsDestruction(getContext()) ||
5780 !D->getType().isConstantStorage(getContext(), true, true)))
5781 return;
5782
5783 const VarDecl *InitDecl;
5784 const Expr *InitExpr = D->getAnyInitializer(InitDecl);
5785
5786 std::optional<ConstantEmitter> emitter;
5787
5788 // CUDA E.2.4.1 "__shared__ variables cannot have an initialization
5789 // as part of their declaration." Sema has already checked for
5790 // error cases, so we just need to set Init to UndefValue.
5791 bool IsCUDASharedVar =
5792 getLangOpts().CUDAIsDevice && D->hasAttr<CUDASharedAttr>();
5793 // Shadows of initialized device-side global variables are also left
5794 // undefined.
5795 // Managed Variables should be initialized on both host side and device side.
5796 bool IsCUDAShadowVar =
5797 !getLangOpts().CUDAIsDevice && !D->hasAttr<HIPManagedAttr>() &&
5798 (D->hasAttr<CUDAConstantAttr>() || D->hasAttr<CUDADeviceAttr>() ||
5799 D->hasAttr<CUDASharedAttr>());
5800 bool IsCUDADeviceShadowVar =
5801 getLangOpts().CUDAIsDevice && !D->hasAttr<HIPManagedAttr>() &&
5802 (D->getType()->isCUDADeviceBuiltinSurfaceType() ||
5803 D->getType()->isCUDADeviceBuiltinTextureType());
5804 if (getLangOpts().CUDA &&
5805 (IsCUDASharedVar || IsCUDAShadowVar || IsCUDADeviceShadowVar)) {
5806 Init = llvm::UndefValue::get(getTypes().ConvertTypeForMem(ASTTy));
5807 } else if (getLangOpts().HLSL &&
5808 (D->getType()->isHLSLResourceRecord() ||
5809 D->getType()->isHLSLResourceRecordArray())) {
5810 Init = llvm::PoisonValue::get(getTypes().ConvertType(ASTTy));
5811 NeedsGlobalCtor = D->getType()->isHLSLResourceRecord();
5812 } else if (D->hasAttr<LoaderUninitializedAttr>()) {
5813 Init = llvm::UndefValue::get(getTypes().ConvertTypeForMem(ASTTy));
5814 } else if (!InitExpr) {
5815 // This is a tentative definition; tentative definitions are
5816 // implicitly initialized with { 0 }.
5817 //
5818 // Note that tentative definitions are only emitted at the end of
5819 // a translation unit, so they should never have incomplete
5820 // type. In addition, EmitTentativeDefinition makes sure that we
5821 // never attempt to emit a tentative definition if a real one
5822 // exists. A use may still exists, however, so we still may need
5823 // to do a RAUW.
5824 assert(!ASTTy->isIncompleteType() && "Unexpected incomplete type");
5825 Init = EmitNullConstant(D->getType());
5826 } else {
5827 initializedGlobalDecl = GlobalDecl(D);
5828 emitter.emplace(*this);
5829 llvm::Constant *Initializer = emitter->tryEmitForInitializer(*InitDecl);
5830 if (!Initializer) {
5831 QualType T = InitExpr->getType();
5832 if (D->getType()->isReferenceType())
5833 T = D->getType();
5834
5835 if (getLangOpts().CPlusPlus) {
5837 if (!IsDefinitionAvailableExternally)
5838 NeedsGlobalCtor = true;
5839 if (InitDecl->hasFlexibleArrayInit(getContext())) {
5840 ErrorUnsupported(D, "flexible array initializer");
5841 // We cannot create ctor for flexible array initializer
5842 NeedsGlobalCtor = false;
5843 }
5844 } else {
5845 ErrorUnsupported(D, "static initializer");
5846 Init = llvm::PoisonValue::get(getTypes().ConvertType(T));
5847 }
5848 } else {
5849 Init = Initializer;
5850 // We don't need an initializer, so remove the entry for the delayed
5851 // initializer position (just in case this entry was delayed) if we
5852 // also don't need to register a destructor.
5853 if (getLangOpts().CPlusPlus && !NeedsGlobalDtor)
5854 DelayedCXXInitPosition.erase(D);
5855
5856#ifndef NDEBUG
5857 CharUnits VarSize = getContext().getTypeSizeInChars(ASTTy) +
5860 getDataLayout().getTypeAllocSize(Init->getType()));
5861 assert(VarSize == CstSize && "Emitted constant has unexpected size");
5862#endif
5863 }
5864 }
5865
5866 llvm::Type* InitType = Init->getType();
5867 llvm::Constant *Entry =
5868 GetAddrOfGlobalVar(D, InitType, ForDefinition_t(!IsTentative));
5869
5870 // Strip off pointer casts if we got them.
5871 Entry = Entry->stripPointerCasts();
5872
5873 // Entry is now either a Function or GlobalVariable.
5874 auto *GV = dyn_cast<llvm::GlobalVariable>(Entry);
5875
5876 // We have a definition after a declaration with the wrong type.
5877 // We must make a new GlobalVariable* and update everything that used OldGV
5878 // (a declaration or tentative definition) with the new GlobalVariable*
5879 // (which will be a definition).
5880 //
5881 // This happens if there is a prototype for a global (e.g.
5882 // "extern int x[];") and then a definition of a different type (e.g.
5883 // "int x[10];"). This also happens when an initializer has a different type
5884 // from the type of the global (this happens with unions).
5885 if (!GV || GV->getValueType() != InitType ||
5886 GV->getType()->getAddressSpace() !=
5887 getContext().getTargetAddressSpace(GetGlobalVarAddressSpace(D))) {
5888
5889 // Move the old entry aside so that we'll create a new one.
5890 Entry->setName(StringRef());
5891
5892 // Make a new global with the correct type, this is now guaranteed to work.
5893 GV = cast<llvm::GlobalVariable>(
5894 GetAddrOfGlobalVar(D, InitType, ForDefinition_t(!IsTentative))
5895 ->stripPointerCasts());
5896
5897 // Replace all uses of the old global with the new global
5898 llvm::Constant *NewPtrForOldDecl =
5899 llvm::ConstantExpr::getPointerBitCastOrAddrSpaceCast(GV,
5900 Entry->getType());
5901 Entry->replaceAllUsesWith(NewPtrForOldDecl);
5902
5903 // Erase the old global, since it is no longer used.
5904 cast<llvm::GlobalValue>(Entry)->eraseFromParent();
5905 }
5906
5908
5909 if (D->hasAttr<AnnotateAttr>())
5911
5912 // Set the llvm linkage type as appropriate.
5913 llvm::GlobalValue::LinkageTypes Linkage = getLLVMLinkageVarDefinition(D);
5914
5915 // CUDA B.2.1 "The __device__ qualifier declares a variable that resides on
5916 // the device. [...]"
5917 // CUDA B.2.2 "The __constant__ qualifier, optionally used together with
5918 // __device__, declares a variable that: [...]
5919 // Is accessible from all the threads within the grid and from the host
5920 // through the runtime library (cudaGetSymbolAddress() / cudaGetSymbolSize()
5921 // / cudaMemcpyToSymbol() / cudaMemcpyFromSymbol())."
5922 if (LangOpts.CUDA) {
5923 if (LangOpts.CUDAIsDevice) {
5924 if (Linkage != llvm::GlobalValue::InternalLinkage &&
5925 (D->hasAttr<CUDADeviceAttr>() || D->hasAttr<CUDAConstantAttr>() ||
5926 D->getType()->isCUDADeviceBuiltinSurfaceType() ||
5927 D->getType()->isCUDADeviceBuiltinTextureType()))
5928 GV->setExternallyInitialized(true);
5929 } else {
5931 }
5933 }
5934
5935 if (LangOpts.HLSL && GetGlobalVarAddressSpace(D) == LangAS::hlsl_input) {
5936 // HLSL Input variables are considered to be set by the driver/pipeline, but
5937 // only visible to a single thread/wave.
5938 GV->setExternallyInitialized(true);
5939 } else {
5940 GV->setInitializer(Init);
5941 }
5942
5943 if (LangOpts.HLSL)
5945
5946 if (emitter)
5947 emitter->finalize(GV);
5948
5949 // If it is safe to mark the global 'constant', do so now.
5950 GV->setConstant((D->hasAttr<CUDAConstantAttr>() && LangOpts.CUDAIsDevice) ||
5951 (!NeedsGlobalCtor && !NeedsGlobalDtor &&
5952 D->getType().isConstantStorage(getContext(), true, true)));
5953
5954 // If it is in a read-only section, mark it 'constant'.
5955 if (const SectionAttr *SA = D->getAttr<SectionAttr>()) {
5956 const ASTContext::SectionInfo &SI = Context.SectionInfos[SA->getName()];
5957 if ((SI.SectionFlags & ASTContext::PSF_Write) == 0)
5958 GV->setConstant(true);
5959 }
5960
5961 CharUnits AlignVal = getContext().getDeclAlign(D);
5962 // Check for alignment specifed in an 'omp allocate' directive.
5963 if (std::optional<CharUnits> AlignValFromAllocate =
5965 AlignVal = *AlignValFromAllocate;
5966 GV->setAlignment(AlignVal.getAsAlign());
5967
5968 // On Darwin, unlike other Itanium C++ ABI platforms, the thread-wrapper
5969 // function is only defined alongside the variable, not also alongside
5970 // callers. Normally, all accesses to a thread_local go through the
5971 // thread-wrapper in order to ensure initialization has occurred, underlying
5972 // variable will never be used other than the thread-wrapper, so it can be
5973 // converted to internal linkage.
5974 //
5975 // However, if the variable has the 'constinit' attribute, it _can_ be
5976 // referenced directly, without calling the thread-wrapper, so the linkage
5977 // must not be changed.
5978 //
5979 // Additionally, if the variable isn't plain external linkage, e.g. if it's
5980 // weak or linkonce, the de-duplication semantics are important to preserve,
5981 // so we don't change the linkage.
5982 if (D->getTLSKind() == VarDecl::TLS_Dynamic &&
5983 Linkage == llvm::GlobalValue::ExternalLinkage &&
5984 Context.getTargetInfo().getTriple().isOSDarwin() &&
5985 !D->hasAttr<ConstInitAttr>())
5986 Linkage = llvm::GlobalValue::InternalLinkage;
5987
5988 // HLSL variables in the input address space maps like memory-mapped
5989 // variables. Even if they are 'static', they are externally initialized and
5990 // read/write by the hardware/driver/pipeline.
5991 if (LangOpts.HLSL && GetGlobalVarAddressSpace(D) == LangAS::hlsl_input)
5992 Linkage = llvm::GlobalValue::ExternalLinkage;
5993
5994 GV->setLinkage(Linkage);
5995 if (D->hasAttr<DLLImportAttr>())
5996 GV->setDLLStorageClass(llvm::GlobalVariable::DLLImportStorageClass);
5997 else if (D->hasAttr<DLLExportAttr>())
5998 GV->setDLLStorageClass(llvm::GlobalVariable::DLLExportStorageClass);
5999 else
6000 GV->setDLLStorageClass(llvm::GlobalVariable::DefaultStorageClass);
6001
6002 if (Linkage == llvm::GlobalVariable::CommonLinkage) {
6003 // common vars aren't constant even if declared const.
6004 GV->setConstant(false);
6005 // Tentative definition of global variables may be initialized with
6006 // non-zero null pointers. In this case they should have weak linkage
6007 // since common linkage must have zero initializer and must not have
6008 // explicit section therefore cannot have non-zero initial value.
6009 if (!GV->getInitializer()->isNullValue())
6010 GV->setLinkage(llvm::GlobalVariable::WeakAnyLinkage);
6011 }
6012
6013 setNonAliasAttributes(D, GV);
6014
6015 if (D->getTLSKind() && !GV->isThreadLocal()) {
6016 if (D->getTLSKind() == VarDecl::TLS_Dynamic)
6017 CXXThreadLocals.push_back(D);
6018 setTLSMode(GV, *D);
6019 }
6020
6021 maybeSetTrivialComdat(*D, *GV);
6022
6023 // Emit the initializer function if necessary.
6024 if (NeedsGlobalCtor || NeedsGlobalDtor)
6025 EmitCXXGlobalVarDeclInitFunc(D, GV, NeedsGlobalCtor);
6026
6027 SanitizerMD->reportGlobal(GV, *D, NeedsGlobalCtor);
6028
6029 // Emit global variable debug information.
6030 if (CGDebugInfo *DI = getModuleDebugInfo())
6031 if (getCodeGenOpts().hasReducedDebugInfo())
6032 DI->EmitGlobalVariable(GV, D);
6033}
6034
6035static bool isVarDeclStrongDefinition(const ASTContext &Context,
6036 CodeGenModule &CGM, const VarDecl *D,
6037 bool NoCommon) {
6038 // Don't give variables common linkage if -fno-common was specified unless it
6039 // was overridden by a NoCommon attribute.
6040 if ((NoCommon || D->hasAttr<NoCommonAttr>()) && !D->hasAttr<CommonAttr>())
6041 return true;
6042
6043 // C11 6.9.2/2:
6044 // A declaration of an identifier for an object that has file scope without
6045 // an initializer, and without a storage-class specifier or with the
6046 // storage-class specifier static, constitutes a tentative definition.
6047 if (D->getInit() || D->hasExternalStorage())
6048 return true;
6049
6050 // A variable cannot be both common and exist in a section.
6051 if (D->hasAttr<SectionAttr>())
6052 return true;
6053
6054 // A variable cannot be both common and exist in a section.
6055 // We don't try to determine which is the right section in the front-end.
6056 // If no specialized section name is applicable, it will resort to default.
6057 if (D->hasAttr<PragmaClangBSSSectionAttr>() ||
6058 D->hasAttr<PragmaClangDataSectionAttr>() ||
6059 D->hasAttr<PragmaClangRelroSectionAttr>() ||
6060 D->hasAttr<PragmaClangRodataSectionAttr>())
6061 return true;
6062
6063 // Thread local vars aren't considered common linkage.
6064 if (D->getTLSKind())
6065 return true;
6066
6067 // Tentative definitions marked with WeakImportAttr are true definitions.
6068 if (D->hasAttr<WeakImportAttr>())
6069 return true;
6070
6071 // A variable cannot be both common and exist in a comdat.
6072 if (shouldBeInCOMDAT(CGM, *D))
6073 return true;
6074
6075 // Declarations with a required alignment do not have common linkage in MSVC
6076 // mode.
6077 if (Context.getTargetInfo().getCXXABI().isMicrosoft()) {
6078 if (D->hasAttr<AlignedAttr>())
6079 return true;
6080 QualType VarType = D->getType();
6081 if (Context.isAlignmentRequired(VarType))
6082 return true;
6083
6084 if (const auto *RD = VarType->getAsRecordDecl()) {
6085 for (const FieldDecl *FD : RD->fields()) {
6086 if (FD->isBitField())
6087 continue;
6088 if (FD->hasAttr<AlignedAttr>())
6089 return true;
6090 if (Context.isAlignmentRequired(FD->getType()))
6091 return true;
6092 }
6093 }
6094 }
6095
6096 // Microsoft's link.exe doesn't support alignments greater than 32 bytes for
6097 // common symbols, so symbols with greater alignment requirements cannot be
6098 // common.
6099 // Other COFF linkers (ld.bfd and LLD) support arbitrary power-of-two
6100 // alignments for common symbols via the aligncomm directive, so this
6101 // restriction only applies to MSVC environments.
6102 if (Context.getTargetInfo().getTriple().isKnownWindowsMSVCEnvironment() &&
6103 Context.getTypeAlignIfKnown(D->getType()) >
6104 Context.toBits(CharUnits::fromQuantity(32)))
6105 return true;
6106
6107 return false;
6108}
6109
6110llvm::GlobalValue::LinkageTypes
6113 if (Linkage == GVA_Internal)
6114 return llvm::Function::InternalLinkage;
6115
6116 if (D->hasAttr<WeakAttr>())
6117 return llvm::GlobalVariable::WeakAnyLinkage;
6118
6119 if (const auto *FD = D->getAsFunction())
6121 return llvm::GlobalVariable::LinkOnceAnyLinkage;
6122
6123 // We are guaranteed to have a strong definition somewhere else,
6124 // so we can use available_externally linkage.
6126 return llvm::GlobalValue::AvailableExternallyLinkage;
6127
6128 // Note that Apple's kernel linker doesn't support symbol
6129 // coalescing, so we need to avoid linkonce and weak linkages there.
6130 // Normally, this means we just map to internal, but for explicit
6131 // instantiations we'll map to external.
6132
6133 // In C++, the compiler has to emit a definition in every translation unit
6134 // that references the function. We should use linkonce_odr because
6135 // a) if all references in this translation unit are optimized away, we
6136 // don't need to codegen it. b) if the function persists, it needs to be
6137 // merged with other definitions. c) C++ has the ODR, so we know the
6138 // definition is dependable.
6140 return !Context.getLangOpts().AppleKext ? llvm::Function::LinkOnceODRLinkage
6141 : llvm::Function::InternalLinkage;
6142
6143 // An explicit instantiation of a template has weak linkage, since
6144 // explicit instantiations can occur in multiple translation units
6145 // and must all be equivalent. However, we are not allowed to
6146 // throw away these explicit instantiations.
6147 //
6148 // CUDA/HIP: For -fno-gpu-rdc case, device code is limited to one TU,
6149 // so say that CUDA templates are either external (for kernels) or internal.
6150 // This lets llvm perform aggressive inter-procedural optimizations. For
6151 // -fgpu-rdc case, device function calls across multiple TU's are allowed,
6152 // therefore we need to follow the normal linkage paradigm.
6153 if (Linkage == GVA_StrongODR) {
6154 if (getLangOpts().AppleKext)
6155 return llvm::Function::ExternalLinkage;
6156 if (getLangOpts().CUDA && getLangOpts().CUDAIsDevice &&
6157 !getLangOpts().GPURelocatableDeviceCode)
6158 return D->hasAttr<CUDAGlobalAttr>() ? llvm::Function::ExternalLinkage
6159 : llvm::Function::InternalLinkage;
6160 return llvm::Function::WeakODRLinkage;
6161 }
6162
6163 // C++ doesn't have tentative definitions and thus cannot have common
6164 // linkage.
6165 if (!getLangOpts().CPlusPlus && isa<VarDecl>(D) &&
6166 !isVarDeclStrongDefinition(Context, *this, cast<VarDecl>(D),
6167 CodeGenOpts.NoCommon))
6168 return llvm::GlobalVariable::CommonLinkage;
6169
6170 // selectany symbols are externally visible, so use weak instead of
6171 // linkonce. MSVC optimizes away references to const selectany globals, so
6172 // all definitions should be the same and ODR linkage should be used.
6173 // http://msdn.microsoft.com/en-us/library/5tkz6s71.aspx
6174 if (D->hasAttr<SelectAnyAttr>())
6175 return llvm::GlobalVariable::WeakODRLinkage;
6176
6177 // Otherwise, we have strong external linkage.
6178 assert(Linkage == GVA_StrongExternal);
6179 return llvm::GlobalVariable::ExternalLinkage;
6180}
6181
6182llvm::GlobalValue::LinkageTypes
6186}
6187
6188/// Replace the uses of a function that was declared with a non-proto type.
6189/// We want to silently drop extra arguments from call sites
6190static void replaceUsesOfNonProtoConstant(llvm::Constant *old,
6191 llvm::Function *newFn) {
6192 // Fast path.
6193 if (old->use_empty())
6194 return;
6195
6196 llvm::Type *newRetTy = newFn->getReturnType();
6198
6199 SmallVector<llvm::CallBase *> callSitesToBeRemovedFromParent;
6200
6201 for (llvm::Value::use_iterator ui = old->use_begin(), ue = old->use_end();
6202 ui != ue; ui++) {
6203 llvm::User *user = ui->getUser();
6204
6205 // Recognize and replace uses of bitcasts. Most calls to
6206 // unprototyped functions will use bitcasts.
6207 if (auto *bitcast = dyn_cast<llvm::ConstantExpr>(user)) {
6208 if (bitcast->getOpcode() == llvm::Instruction::BitCast)
6209 replaceUsesOfNonProtoConstant(bitcast, newFn);
6210 continue;
6211 }
6212
6213 // Recognize calls to the function.
6214 llvm::CallBase *callSite = dyn_cast<llvm::CallBase>(user);
6215 if (!callSite)
6216 continue;
6217 if (!callSite->isCallee(&*ui))
6218 continue;
6219
6220 // If the return types don't match exactly, then we can't
6221 // transform this call unless it's dead.
6222 if (callSite->getType() != newRetTy && !callSite->use_empty())
6223 continue;
6224
6225 // Get the call site's attribute list.
6227 llvm::AttributeList oldAttrs = callSite->getAttributes();
6228
6229 // If the function was passed too few arguments, don't transform.
6230 unsigned newNumArgs = newFn->arg_size();
6231 if (callSite->arg_size() < newNumArgs)
6232 continue;
6233
6234 // If extra arguments were passed, we silently drop them.
6235 // If any of the types mismatch, we don't transform.
6236 unsigned argNo = 0;
6237 bool dontTransform = false;
6238 for (llvm::Argument &A : newFn->args()) {
6239 if (callSite->getArgOperand(argNo)->getType() != A.getType()) {
6240 dontTransform = true;
6241 break;
6242 }
6243
6244 // Add any parameter attributes.
6245 newArgAttrs.push_back(oldAttrs.getParamAttrs(argNo));
6246 argNo++;
6247 }
6248 if (dontTransform)
6249 continue;
6250
6251 // Okay, we can transform this. Create the new call instruction and copy
6252 // over the required information.
6253 newArgs.append(callSite->arg_begin(), callSite->arg_begin() + argNo);
6254
6255 // Copy over any operand bundles.
6257 callSite->getOperandBundlesAsDefs(newBundles);
6258
6259 llvm::CallBase *newCall;
6260 if (isa<llvm::CallInst>(callSite)) {
6261 newCall = llvm::CallInst::Create(newFn, newArgs, newBundles, "",
6262 callSite->getIterator());
6263 } else {
6264 auto *oldInvoke = cast<llvm::InvokeInst>(callSite);
6265 newCall = llvm::InvokeInst::Create(
6266 newFn, oldInvoke->getNormalDest(), oldInvoke->getUnwindDest(),
6267 newArgs, newBundles, "", callSite->getIterator());
6268 }
6269 newArgs.clear(); // for the next iteration
6270
6271 if (!newCall->getType()->isVoidTy())
6272 newCall->takeName(callSite);
6273 newCall->setAttributes(
6274 llvm::AttributeList::get(newFn->getContext(), oldAttrs.getFnAttrs(),
6275 oldAttrs.getRetAttrs(), newArgAttrs));
6276 newCall->setCallingConv(callSite->getCallingConv());
6277
6278 // Finally, remove the old call, replacing any uses with the new one.
6279 if (!callSite->use_empty())
6280 callSite->replaceAllUsesWith(newCall);
6281
6282 // Copy debug location attached to CI.
6283 if (callSite->getDebugLoc())
6284 newCall->setDebugLoc(callSite->getDebugLoc());
6285
6286 callSitesToBeRemovedFromParent.push_back(callSite);
6287 }
6288
6289 for (auto *callSite : callSitesToBeRemovedFromParent) {
6290 callSite->eraseFromParent();
6291 }
6292}
6293
6294/// ReplaceUsesOfNonProtoTypeWithRealFunction - This function is called when we
6295/// implement a function with no prototype, e.g. "int foo() {}". If there are
6296/// existing call uses of the old function in the module, this adjusts them to
6297/// call the new function directly.
6298///
6299/// This is not just a cleanup: the always_inline pass requires direct calls to
6300/// functions to be able to inline them. If there is a bitcast in the way, it
6301/// won't inline them. Instcombine normally deletes these calls, but it isn't
6302/// run at -O0.
6303static void ReplaceUsesOfNonProtoTypeWithRealFunction(llvm::GlobalValue *Old,
6304 llvm::Function *NewFn) {
6305 // If we're redefining a global as a function, don't transform it.
6306 if (!isa<llvm::Function>(Old)) return;
6307
6309}
6310
6312 auto DK = VD->isThisDeclarationADefinition();
6313 if ((DK == VarDecl::Definition && VD->hasAttr<DLLImportAttr>()) ||
6314 (LangOpts.CUDA && !shouldEmitCUDAGlobalVar(VD)))
6315 return;
6316
6318 // If we have a definition, this might be a deferred decl. If the
6319 // instantiation is explicit, make sure we emit it at the end.
6322
6323 EmitTopLevelDecl(VD);
6324}
6325
6326void CodeGenModule::EmitGlobalFunctionDefinition(GlobalDecl GD,
6327 llvm::GlobalValue *GV) {
6328 const auto *D = cast<FunctionDecl>(GD.getDecl());
6329
6330 // Compute the function info and LLVM type.
6332 llvm::FunctionType *Ty = getTypes().GetFunctionType(FI);
6333
6334 // Get or create the prototype for the function.
6335 if (!GV || (GV->getValueType() != Ty))
6336 GV = cast<llvm::GlobalValue>(GetAddrOfFunction(GD, Ty, /*ForVTable=*/false,
6337 /*DontDefer=*/true,
6338 ForDefinition));
6339
6340 // Already emitted.
6341 if (!GV->isDeclaration())
6342 return;
6343
6344 // We need to set linkage and visibility on the function before
6345 // generating code for it because various parts of IR generation
6346 // want to propagate this information down (e.g. to local static
6347 // declarations).
6348 auto *Fn = cast<llvm::Function>(GV);
6349 setFunctionLinkage(GD, Fn);
6350
6351 // FIXME: this is redundant with part of setFunctionDefinitionAttributes
6352 setGVProperties(Fn, GD);
6353
6355
6356 maybeSetTrivialComdat(*D, *Fn);
6357
6358 CodeGenFunction(*this).GenerateCode(GD, Fn, FI);
6359
6360 setNonAliasAttributes(GD, Fn);
6361
6362 bool ShouldAddOptNone = !CodeGenOpts.DisableO0ImplyOptNone &&
6363 (CodeGenOpts.OptimizationLevel == 0) &&
6364 !D->hasAttr<MinSizeAttr>();
6365
6366 if (DeviceKernelAttr::isOpenCLSpelling(D->getAttr<DeviceKernelAttr>())) {
6368 !D->hasAttr<NoInlineAttr>() &&
6369 !Fn->hasFnAttribute(llvm::Attribute::NoInline) &&
6370 !D->hasAttr<OptimizeNoneAttr>() &&
6371 !Fn->hasFnAttribute(llvm::Attribute::OptimizeNone) &&
6372 !ShouldAddOptNone) {
6373 Fn->addFnAttr(llvm::Attribute::AlwaysInline);
6374 }
6375 }
6376
6378
6379 if (const ConstructorAttr *CA = D->getAttr<ConstructorAttr>())
6380 AddGlobalCtor(Fn, CA->getPriority());
6381 if (const DestructorAttr *DA = D->getAttr<DestructorAttr>())
6382 AddGlobalDtor(Fn, DA->getPriority(), true);
6383 if (getLangOpts().OpenMP && D->hasAttr<OMPDeclareTargetDeclAttr>())
6385}
6386
6387void CodeGenModule::EmitAliasDefinition(GlobalDecl GD) {
6388 const auto *D = cast<ValueDecl>(GD.getDecl());
6389 const AliasAttr *AA = D->getAttr<AliasAttr>();
6390 assert(AA && "Not an alias?");
6391
6392 StringRef MangledName = getMangledName(GD);
6393
6394 if (AA->getAliasee() == MangledName) {
6395 Diags.Report(AA->getLocation(), diag::err_cyclic_alias) << 0;
6396 return;
6397 }
6398
6399 // If there is a definition in the module, then it wins over the alias.
6400 // This is dubious, but allow it to be safe. Just ignore the alias.
6401 llvm::GlobalValue *Entry = GetGlobalValue(MangledName);
6402 if (Entry && !Entry->isDeclaration())
6403 return;
6404
6405 Aliases.push_back(GD);
6406
6407 llvm::Type *DeclTy = getTypes().ConvertTypeForMem(D->getType());
6408
6409 // Create a reference to the named value. This ensures that it is emitted
6410 // if a deferred decl.
6411 llvm::Constant *Aliasee;
6412 llvm::GlobalValue::LinkageTypes LT;
6413 if (isa<llvm::FunctionType>(DeclTy)) {
6414 Aliasee = GetOrCreateLLVMFunction(AA->getAliasee(), DeclTy, GD,
6415 /*ForVTable=*/false);
6416 LT = getFunctionLinkage(GD);
6417 } else {
6418 Aliasee = GetOrCreateLLVMGlobal(AA->getAliasee(), DeclTy, LangAS::Default,
6419 /*D=*/nullptr);
6420 if (const auto *VD = dyn_cast<VarDecl>(GD.getDecl()))
6422 else
6423 LT = getFunctionLinkage(GD);
6424 }
6425
6426 // Create the new alias itself, but don't set a name yet.
6427 unsigned AS = Aliasee->getType()->getPointerAddressSpace();
6428 auto *GA =
6429 llvm::GlobalAlias::create(DeclTy, AS, LT, "", Aliasee, &getModule());
6430
6431 if (Entry) {
6432 if (GA->getAliasee() == Entry) {
6433 Diags.Report(AA->getLocation(), diag::err_cyclic_alias) << 0;
6434 return;
6435 }
6436
6437 assert(Entry->isDeclaration());
6438
6439 // If there is a declaration in the module, then we had an extern followed
6440 // by the alias, as in:
6441 // extern int test6();
6442 // ...
6443 // int test6() __attribute__((alias("test7")));
6444 //
6445 // Remove it and replace uses of it with the alias.
6446 GA->takeName(Entry);
6447
6448 Entry->replaceAllUsesWith(GA);
6449 Entry->eraseFromParent();
6450 } else {
6451 GA->setName(MangledName);
6452 }
6453
6454 // Set attributes which are particular to an alias; this is a
6455 // specialization of the attributes which may be set on a global
6456 // variable/function.
6457 if (D->hasAttr<WeakAttr>() || D->hasAttr<WeakRefAttr>() ||
6458 D->isWeakImported()) {
6459 GA->setLinkage(llvm::Function::WeakAnyLinkage);
6460 }
6461
6462 if (const auto *VD = dyn_cast<VarDecl>(D))
6463 if (VD->getTLSKind())
6464 setTLSMode(GA, *VD);
6465
6466 SetCommonAttributes(GD, GA);
6467
6468 // Emit global alias debug information.
6469 if (isa<VarDecl>(D))
6470 if (CGDebugInfo *DI = getModuleDebugInfo())
6471 DI->EmitGlobalAlias(cast<llvm::GlobalValue>(GA->getAliasee()->stripPointerCasts()), GD);
6472}
6473
6474void CodeGenModule::emitIFuncDefinition(GlobalDecl GD) {
6475 const auto *D = cast<ValueDecl>(GD.getDecl());
6476 const IFuncAttr *IFA = D->getAttr<IFuncAttr>();
6477 assert(IFA && "Not an ifunc?");
6478
6479 StringRef MangledName = getMangledName(GD);
6480
6481 if (IFA->getResolver() == MangledName) {
6482 Diags.Report(IFA->getLocation(), diag::err_cyclic_alias) << 1;
6483 return;
6484 }
6485
6486 // Report an error if some definition overrides ifunc.
6487 llvm::GlobalValue *Entry = GetGlobalValue(MangledName);
6488 if (Entry && !Entry->isDeclaration()) {
6489 GlobalDecl OtherGD;
6490 if (lookupRepresentativeDecl(MangledName, OtherGD) &&
6491 DiagnosedConflictingDefinitions.insert(GD).second) {
6492 Diags.Report(D->getLocation(), diag::err_duplicate_mangled_name)
6493 << MangledName;
6494 Diags.Report(OtherGD.getDecl()->getLocation(),
6495 diag::note_previous_definition);
6496 }
6497 return;
6498 }
6499
6500 Aliases.push_back(GD);
6501
6502 // The resolver might not be visited yet. Specify a dummy non-function type to
6503 // indicate IsIncompleteFunction. Either the type is ignored (if the resolver
6504 // was emitted) or the whole function will be replaced (if the resolver has
6505 // not been emitted).
6506 llvm::Constant *Resolver =
6507 GetOrCreateLLVMFunction(IFA->getResolver(), VoidTy, {},
6508 /*ForVTable=*/false);
6509 llvm::Type *DeclTy = getTypes().ConvertTypeForMem(D->getType());
6510 unsigned AS = getTypes().getTargetAddressSpace(D->getType());
6511 llvm::GlobalIFunc *GIF = llvm::GlobalIFunc::create(
6512 DeclTy, AS, llvm::Function::ExternalLinkage, "", Resolver, &getModule());
6513 if (Entry) {
6514 if (GIF->getResolver() == Entry) {
6515 Diags.Report(IFA->getLocation(), diag::err_cyclic_alias) << 1;
6516 return;
6517 }
6518 assert(Entry->isDeclaration());
6519
6520 // If there is a declaration in the module, then we had an extern followed
6521 // by the ifunc, as in:
6522 // extern int test();
6523 // ...
6524 // int test() __attribute__((ifunc("resolver")));
6525 //
6526 // Remove it and replace uses of it with the ifunc.
6527 GIF->takeName(Entry);
6528
6529 Entry->replaceAllUsesWith(GIF);
6530 Entry->eraseFromParent();
6531 } else
6532 GIF->setName(MangledName);
6533 SetCommonAttributes(GD, GIF);
6534}
6535
6536llvm::Function *CodeGenModule::getIntrinsic(unsigned IID,
6538 return llvm::Intrinsic::getOrInsertDeclaration(&getModule(),
6539 (llvm::Intrinsic::ID)IID, Tys);
6540}
6541
6542static llvm::StringMapEntry<llvm::GlobalVariable *> &
6543GetConstantCFStringEntry(llvm::StringMap<llvm::GlobalVariable *> &Map,
6544 const StringLiteral *Literal, bool TargetIsLSB,
6545 bool &IsUTF16, unsigned &StringLength) {
6546 StringRef String = Literal->getString();
6547 unsigned NumBytes = String.size();
6548
6549 // Check for simple case.
6550 if (!Literal->containsNonAsciiOrNull()) {
6551 StringLength = NumBytes;
6552 return *Map.insert(std::make_pair(String, nullptr)).first;
6553 }
6554
6555 // Otherwise, convert the UTF8 literals into a string of shorts.
6556 IsUTF16 = true;
6557
6558 SmallVector<llvm::UTF16, 128> ToBuf(NumBytes + 1); // +1 for ending nulls.
6559 const llvm::UTF8 *FromPtr = (const llvm::UTF8 *)String.data();
6560 llvm::UTF16 *ToPtr = &ToBuf[0];
6561
6562 (void)llvm::ConvertUTF8toUTF16(&FromPtr, FromPtr + NumBytes, &ToPtr,
6563 ToPtr + NumBytes, llvm::strictConversion);
6564
6565 // ConvertUTF8toUTF16 returns the length in ToPtr.
6566 StringLength = ToPtr - &ToBuf[0];
6567
6568 // Add an explicit null.
6569 *ToPtr = 0;
6570 return *Map.insert(std::make_pair(
6571 StringRef(reinterpret_cast<const char *>(ToBuf.data()),
6572 (StringLength + 1) * 2),
6573 nullptr)).first;
6574}
6575
6578 unsigned StringLength = 0;
6579 bool isUTF16 = false;
6580 llvm::StringMapEntry<llvm::GlobalVariable *> &Entry =
6581 GetConstantCFStringEntry(CFConstantStringMap, Literal,
6582 getDataLayout().isLittleEndian(), isUTF16,
6583 StringLength);
6584
6585 if (auto *C = Entry.second)
6586 return ConstantAddress(
6587 C, C->getValueType(), CharUnits::fromQuantity(C->getAlignment()));
6588
6589 const ASTContext &Context = getContext();
6590 const llvm::Triple &Triple = getTriple();
6591
6592 const auto CFRuntime = getLangOpts().CFRuntime;
6593 const bool IsSwiftABI =
6594 static_cast<unsigned>(CFRuntime) >=
6595 static_cast<unsigned>(LangOptions::CoreFoundationABI::Swift);
6596 const bool IsSwift4_1 = CFRuntime == LangOptions::CoreFoundationABI::Swift4_1;
6597
6598 // If we don't already have it, get __CFConstantStringClassReference.
6599 if (!CFConstantStringClassRef) {
6600 const char *CFConstantStringClassName = "__CFConstantStringClassReference";
6601 llvm::Type *Ty = getTypes().ConvertType(getContext().IntTy);
6602 Ty = llvm::ArrayType::get(Ty, 0);
6603
6604 switch (CFRuntime) {
6605 default: break;
6606 case LangOptions::CoreFoundationABI::Swift: [[fallthrough]];
6608 CFConstantStringClassName =
6609 Triple.isOSDarwin() ? "$s15SwiftFoundation19_NSCFConstantStringCN"
6610 : "$s10Foundation19_NSCFConstantStringCN";
6611 Ty = IntPtrTy;
6612 break;
6614 CFConstantStringClassName =
6615 Triple.isOSDarwin() ? "$S15SwiftFoundation19_NSCFConstantStringCN"
6616 : "$S10Foundation19_NSCFConstantStringCN";
6617 Ty = IntPtrTy;
6618 break;
6620 CFConstantStringClassName =
6621 Triple.isOSDarwin() ? "__T015SwiftFoundation19_NSCFConstantStringCN"
6622 : "__T010Foundation19_NSCFConstantStringCN";
6623 Ty = IntPtrTy;
6624 break;
6625 }
6626
6627 llvm::Constant *C = CreateRuntimeVariable(Ty, CFConstantStringClassName);
6628
6629 if (Triple.isOSBinFormatELF() || Triple.isOSBinFormatCOFF()) {
6630 llvm::GlobalValue *GV = nullptr;
6631
6632 if ((GV = dyn_cast<llvm::GlobalValue>(C))) {
6633 IdentifierInfo &II = Context.Idents.get(GV->getName());
6634 TranslationUnitDecl *TUDecl = Context.getTranslationUnitDecl();
6636
6637 const VarDecl *VD = nullptr;
6638 for (const auto *Result : DC->lookup(&II))
6639 if ((VD = dyn_cast<VarDecl>(Result)))
6640 break;
6641
6642 if (Triple.isOSBinFormatELF()) {
6643 if (!VD)
6644 GV->setLinkage(llvm::GlobalValue::ExternalLinkage);
6645 } else {
6646 GV->setLinkage(llvm::GlobalValue::ExternalLinkage);
6647 if (!VD || !VD->hasAttr<DLLExportAttr>())
6648 GV->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass);
6649 else
6650 GV->setDLLStorageClass(llvm::GlobalValue::DLLExportStorageClass);
6651 }
6652
6653 setDSOLocal(GV);
6654 }
6655 }
6656
6657 // Decay array -> ptr
6658 CFConstantStringClassRef =
6659 IsSwiftABI ? llvm::ConstantExpr::getPtrToInt(C, Ty) : C;
6660 }
6661
6662 QualType CFTy = Context.getCFConstantStringType();
6663
6664 auto *STy = cast<llvm::StructType>(getTypes().ConvertType(CFTy));
6665
6666 ConstantInitBuilder Builder(*this);
6667 auto Fields = Builder.beginStruct(STy);
6668
6669 // Class pointer.
6670 Fields.addSignedPointer(cast<llvm::Constant>(CFConstantStringClassRef),
6671 getCodeGenOpts().PointerAuth.ObjCIsaPointers,
6672 GlobalDecl(), QualType());
6673
6674 // Flags.
6675 if (IsSwiftABI) {
6676 Fields.addInt(IntPtrTy, IsSwift4_1 ? 0x05 : 0x01);
6677 Fields.addInt(Int64Ty, isUTF16 ? 0x07d0 : 0x07c8);
6678 } else {
6679 Fields.addInt(IntTy, isUTF16 ? 0x07d0 : 0x07C8);
6680 }
6681
6682 // String pointer.
6683 llvm::Constant *C = nullptr;
6684 if (isUTF16) {
6685 auto Arr = llvm::ArrayRef(
6686 reinterpret_cast<uint16_t *>(const_cast<char *>(Entry.first().data())),
6687 Entry.first().size() / 2);
6688 C = llvm::ConstantDataArray::get(VMContext, Arr);
6689 } else {
6690 C = llvm::ConstantDataArray::getString(VMContext, Entry.first());
6691 }
6692
6693 // Note: -fwritable-strings doesn't make the backing store strings of
6694 // CFStrings writable.
6695 auto *GV =
6696 new llvm::GlobalVariable(getModule(), C->getType(), /*isConstant=*/true,
6697 llvm::GlobalValue::PrivateLinkage, C, ".str");
6698 GV->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
6699 // Don't enforce the target's minimum global alignment, since the only use
6700 // of the string is via this class initializer.
6701 CharUnits Align = isUTF16 ? Context.getTypeAlignInChars(Context.ShortTy)
6702 : Context.getTypeAlignInChars(Context.CharTy);
6703 GV->setAlignment(Align.getAsAlign());
6704
6705 // FIXME: We set the section explicitly to avoid a bug in ld64 224.1.
6706 // Without it LLVM can merge the string with a non unnamed_addr one during
6707 // LTO. Doing that changes the section it ends in, which surprises ld64.
6708 if (Triple.isOSBinFormatMachO())
6709 GV->setSection(isUTF16 ? "__TEXT,__ustring"
6710 : "__TEXT,__cstring,cstring_literals");
6711 // Make sure the literal ends up in .rodata to allow for safe ICF and for
6712 // the static linker to adjust permissions to read-only later on.
6713 else if (Triple.isOSBinFormatELF())
6714 GV->setSection(".rodata");
6715
6716 // String.
6717 Fields.add(GV);
6718
6719 // String length.
6720 llvm::IntegerType *LengthTy =
6721 llvm::IntegerType::get(getModule().getContext(),
6722 Context.getTargetInfo().getLongWidth());
6723 if (IsSwiftABI) {
6726 LengthTy = Int32Ty;
6727 else
6728 LengthTy = IntPtrTy;
6729 }
6730 Fields.addInt(LengthTy, StringLength);
6731
6732 // Swift ABI requires 8-byte alignment to ensure that the _Atomic(uint64_t) is
6733 // properly aligned on 32-bit platforms.
6734 CharUnits Alignment =
6735 IsSwiftABI ? Context.toCharUnitsFromBits(64) : getPointerAlign();
6736
6737 // The struct.
6738 GV = Fields.finishAndCreateGlobal("_unnamed_cfstring_", Alignment,
6739 /*isConstant=*/false,
6740 llvm::GlobalVariable::PrivateLinkage);
6741 GV->addAttribute("objc_arc_inert");
6742 switch (Triple.getObjectFormat()) {
6743 case llvm::Triple::UnknownObjectFormat:
6744 llvm_unreachable("unknown file format");
6745 case llvm::Triple::DXContainer:
6746 case llvm::Triple::GOFF:
6747 case llvm::Triple::SPIRV:
6748 case llvm::Triple::XCOFF:
6749 llvm_unreachable("unimplemented");
6750 case llvm::Triple::COFF:
6751 case llvm::Triple::ELF:
6752 case llvm::Triple::Wasm:
6753 GV->setSection("cfstring");
6754 break;
6755 case llvm::Triple::MachO:
6756 GV->setSection("__DATA,__cfstring");
6757 break;
6758 }
6759 Entry.second = GV;
6760
6761 return ConstantAddress(GV, GV->getValueType(), Alignment);
6762}
6763
6765 return !CodeGenOpts.EmitCodeView || CodeGenOpts.DebugColumnInfo;
6766}
6767
6769 if (ObjCFastEnumerationStateType.isNull()) {
6770 RecordDecl *D = Context.buildImplicitRecord("__objcFastEnumerationState");
6771 D->startDefinition();
6772
6773 QualType FieldTypes[] = {
6774 Context.UnsignedLongTy, Context.getPointerType(Context.getObjCIdType()),
6775 Context.getPointerType(Context.UnsignedLongTy),
6776 Context.getConstantArrayType(Context.UnsignedLongTy, llvm::APInt(32, 5),
6777 nullptr, ArraySizeModifier::Normal, 0)};
6778
6779 for (size_t i = 0; i < 4; ++i) {
6780 FieldDecl *Field = FieldDecl::Create(Context,
6781 D,
6783 SourceLocation(), nullptr,
6784 FieldTypes[i], /*TInfo=*/nullptr,
6785 /*BitWidth=*/nullptr,
6786 /*Mutable=*/false,
6787 ICIS_NoInit);
6788 Field->setAccess(AS_public);
6789 D->addDecl(Field);
6790 }
6791
6792 D->completeDefinition();
6793 ObjCFastEnumerationStateType = Context.getCanonicalTagType(D);
6794 }
6795
6796 return ObjCFastEnumerationStateType;
6797}
6798
6799llvm::Constant *
6801 assert(!E->getType()->isPointerType() && "Strings are always arrays");
6802
6803 // Don't emit it as the address of the string, emit the string data itself
6804 // as an inline array.
6805 if (E->getCharByteWidth() == 1) {
6806 SmallString<64> Str(E->getString());
6807
6808 // Resize the string to the right size, which is indicated by its type.
6809 const ConstantArrayType *CAT = Context.getAsConstantArrayType(E->getType());
6810 assert(CAT && "String literal not of constant array type!");
6811 Str.resize(CAT->getZExtSize());
6812 return llvm::ConstantDataArray::getString(VMContext, Str, false);
6813 }
6814
6815 auto *AType = cast<llvm::ArrayType>(getTypes().ConvertType(E->getType()));
6816 llvm::Type *ElemTy = AType->getElementType();
6817 unsigned NumElements = AType->getNumElements();
6818
6819 // Wide strings have either 2-byte or 4-byte elements.
6820 if (ElemTy->getPrimitiveSizeInBits() == 16) {
6822 Elements.reserve(NumElements);
6823
6824 for(unsigned i = 0, e = E->getLength(); i != e; ++i)
6825 Elements.push_back(E->getCodeUnit(i));
6826 Elements.resize(NumElements);
6827 return llvm::ConstantDataArray::get(VMContext, Elements);
6828 }
6829
6830 assert(ElemTy->getPrimitiveSizeInBits() == 32);
6832 Elements.reserve(NumElements);
6833
6834 for(unsigned i = 0, e = E->getLength(); i != e; ++i)
6835 Elements.push_back(E->getCodeUnit(i));
6836 Elements.resize(NumElements);
6837 return llvm::ConstantDataArray::get(VMContext, Elements);
6838}
6839
6840static llvm::GlobalVariable *
6841GenerateStringLiteral(llvm::Constant *C, llvm::GlobalValue::LinkageTypes LT,
6842 CodeGenModule &CGM, StringRef GlobalName,
6843 CharUnits Alignment) {
6844 unsigned AddrSpace = CGM.getContext().getTargetAddressSpace(
6846
6847 llvm::Module &M = CGM.getModule();
6848 // Create a global variable for this string
6849 auto *GV = new llvm::GlobalVariable(
6850 M, C->getType(), !CGM.getLangOpts().WritableStrings, LT, C, GlobalName,
6851 nullptr, llvm::GlobalVariable::NotThreadLocal, AddrSpace);
6852 GV->setAlignment(Alignment.getAsAlign());
6853 GV->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
6854 if (GV->isWeakForLinker()) {
6855 assert(CGM.supportsCOMDAT() && "Only COFF uses weak string literals");
6856 GV->setComdat(M.getOrInsertComdat(GV->getName()));
6857 }
6858 CGM.setDSOLocal(GV);
6859
6860 return GV;
6861}
6862
6863/// GetAddrOfConstantStringFromLiteral - Return a pointer to a
6864/// constant array for the given string literal.
6867 StringRef Name) {
6868 CharUnits Alignment =
6869 getContext().getAlignOfGlobalVarInChars(S->getType(), /*VD=*/nullptr);
6870
6871 llvm::Constant *C = GetConstantArrayFromStringLiteral(S);
6872 llvm::GlobalVariable **Entry = nullptr;
6873 if (!LangOpts.WritableStrings) {
6874 Entry = &ConstantStringMap[C];
6875 if (auto GV = *Entry) {
6876 if (uint64_t(Alignment.getQuantity()) > GV->getAlignment())
6877 GV->setAlignment(Alignment.getAsAlign());
6879 GV->getValueType(), Alignment);
6880 }
6881 }
6882
6883 SmallString<256> MangledNameBuffer;
6884 StringRef GlobalVariableName;
6885 llvm::GlobalValue::LinkageTypes LT;
6886
6887 // Mangle the string literal if that's how the ABI merges duplicate strings.
6888 // Don't do it if they are writable, since we don't want writes in one TU to
6889 // affect strings in another.
6890 if (getCXXABI().getMangleContext().shouldMangleStringLiteral(S) &&
6891 !LangOpts.WritableStrings) {
6892 llvm::raw_svector_ostream Out(MangledNameBuffer);
6894 LT = llvm::GlobalValue::LinkOnceODRLinkage;
6895 GlobalVariableName = MangledNameBuffer;
6896 } else {
6897 LT = llvm::GlobalValue::PrivateLinkage;
6898 GlobalVariableName = Name;
6899 }
6900
6901 auto GV = GenerateStringLiteral(C, LT, *this, GlobalVariableName, Alignment);
6902
6904 if (DI && getCodeGenOpts().hasReducedDebugInfo())
6905 DI->AddStringLiteralDebugInfo(GV, S);
6906
6907 if (Entry)
6908 *Entry = GV;
6909
6910 SanitizerMD->reportGlobal(GV, S->getStrTokenLoc(0), "<string literal>");
6911
6913 GV->getValueType(), Alignment);
6914}
6915
6916/// GetAddrOfConstantStringFromObjCEncode - Return a pointer to a constant
6917/// array for the given ObjCEncodeExpr node.
6920 std::string Str;
6921 getContext().getObjCEncodingForType(E->getEncodedType(), Str);
6922
6923 return GetAddrOfConstantCString(Str);
6924}
6925
6926/// GetAddrOfConstantCString - Returns a pointer to a character array containing
6927/// the literal and a terminating '\0' character.
6928/// The result has pointer to array type.
6930 const std::string &Str, const char *GlobalName) {
6931 StringRef StrWithNull(Str.c_str(), Str.size() + 1);
6933 getContext().CharTy, /*VD=*/nullptr);
6934
6935 llvm::Constant *C =
6936 llvm::ConstantDataArray::getString(getLLVMContext(), StrWithNull, false);
6937
6938 // Don't share any string literals if strings aren't constant.
6939 llvm::GlobalVariable **Entry = nullptr;
6940 if (!LangOpts.WritableStrings) {
6941 Entry = &ConstantStringMap[C];
6942 if (auto GV = *Entry) {
6943 if (uint64_t(Alignment.getQuantity()) > GV->getAlignment())
6944 GV->setAlignment(Alignment.getAsAlign());
6946 GV->getValueType(), Alignment);
6947 }
6948 }
6949
6950 // Get the default prefix if a name wasn't specified.
6951 if (!GlobalName)
6952 GlobalName = ".str";
6953 // Create a global variable for this.
6954 auto GV = GenerateStringLiteral(C, llvm::GlobalValue::PrivateLinkage, *this,
6955 GlobalName, Alignment);
6956 if (Entry)
6957 *Entry = GV;
6958
6960 GV->getValueType(), Alignment);
6961}
6962
6964 const MaterializeTemporaryExpr *E, const Expr *Init) {
6965 assert((E->getStorageDuration() == SD_Static ||
6966 E->getStorageDuration() == SD_Thread) && "not a global temporary");
6967 const auto *VD = cast<VarDecl>(E->getExtendingDecl());
6968
6969 // If we're not materializing a subobject of the temporary, keep the
6970 // cv-qualifiers from the type of the MaterializeTemporaryExpr.
6971 QualType MaterializedType = Init->getType();
6972 if (Init == E->getSubExpr())
6973 MaterializedType = E->getType();
6974
6975 CharUnits Align = getContext().getTypeAlignInChars(MaterializedType);
6976
6977 auto InsertResult = MaterializedGlobalTemporaryMap.insert({E, nullptr});
6978 if (!InsertResult.second) {
6979 // We've seen this before: either we already created it or we're in the
6980 // process of doing so.
6981 if (!InsertResult.first->second) {
6982 // We recursively re-entered this function, probably during emission of
6983 // the initializer. Create a placeholder. We'll clean this up in the
6984 // outer call, at the end of this function.
6985 llvm::Type *Type = getTypes().ConvertTypeForMem(MaterializedType);
6986 InsertResult.first->second = new llvm::GlobalVariable(
6987 getModule(), Type, false, llvm::GlobalVariable::InternalLinkage,
6988 nullptr);
6989 }
6990 return ConstantAddress(InsertResult.first->second,
6991 llvm::cast<llvm::GlobalVariable>(
6992 InsertResult.first->second->stripPointerCasts())
6993 ->getValueType(),
6994 Align);
6995 }
6996
6997 // FIXME: If an externally-visible declaration extends multiple temporaries,
6998 // we need to give each temporary the same name in every translation unit (and
6999 // we also need to make the temporaries externally-visible).
7000 SmallString<256> Name;
7001 llvm::raw_svector_ostream Out(Name);
7003 VD, E->getManglingNumber(), Out);
7004
7005 APValue *Value = nullptr;
7006 if (E->getStorageDuration() == SD_Static && VD->evaluateValue()) {
7007 // If the initializer of the extending declaration is a constant
7008 // initializer, we should have a cached constant initializer for this
7009 // temporary. Note that this might have a different value from the value
7010 // computed by evaluating the initializer if the surrounding constant
7011 // expression modifies the temporary.
7012 Value = E->getOrCreateValue(false);
7013 }
7014
7015 // Try evaluating it now, it might have a constant initializer.
7016 Expr::EvalResult EvalResult;
7017 if (!Value && Init->EvaluateAsRValue(EvalResult, getContext()) &&
7018 !EvalResult.hasSideEffects())
7019 Value = &EvalResult.Val;
7020
7021 LangAS AddrSpace = GetGlobalVarAddressSpace(VD);
7022
7023 std::optional<ConstantEmitter> emitter;
7024 llvm::Constant *InitialValue = nullptr;
7025 bool Constant = false;
7026 llvm::Type *Type;
7027 if (Value) {
7028 // The temporary has a constant initializer, use it.
7029 emitter.emplace(*this);
7030 InitialValue = emitter->emitForInitializer(*Value, AddrSpace,
7031 MaterializedType);
7032 Constant =
7033 MaterializedType.isConstantStorage(getContext(), /*ExcludeCtor*/ Value,
7034 /*ExcludeDtor*/ false);
7035 Type = InitialValue->getType();
7036 } else {
7037 // No initializer, the initialization will be provided when we
7038 // initialize the declaration which performed lifetime extension.
7039 Type = getTypes().ConvertTypeForMem(MaterializedType);
7040 }
7041
7042 // Create a global variable for this lifetime-extended temporary.
7043 llvm::GlobalValue::LinkageTypes Linkage = getLLVMLinkageVarDefinition(VD);
7044 if (Linkage == llvm::GlobalVariable::ExternalLinkage) {
7045 const VarDecl *InitVD;
7046 if (VD->isStaticDataMember() && VD->getAnyInitializer(InitVD) &&
7047 isa<CXXRecordDecl>(InitVD->getLexicalDeclContext())) {
7048 // Temporaries defined inside a class get linkonce_odr linkage because the
7049 // class can be defined in multiple translation units.
7050 Linkage = llvm::GlobalVariable::LinkOnceODRLinkage;
7051 } else {
7052 // There is no need for this temporary to have external linkage if the
7053 // VarDecl has external linkage.
7054 Linkage = llvm::GlobalVariable::InternalLinkage;
7055 }
7056 }
7057 auto TargetAS = getContext().getTargetAddressSpace(AddrSpace);
7058 auto *GV = new llvm::GlobalVariable(
7059 getModule(), Type, Constant, Linkage, InitialValue, Name.c_str(),
7060 /*InsertBefore=*/nullptr, llvm::GlobalVariable::NotThreadLocal, TargetAS);
7061 if (emitter) emitter->finalize(GV);
7062 // Don't assign dllimport or dllexport to local linkage globals.
7063 if (!llvm::GlobalValue::isLocalLinkage(Linkage)) {
7064 setGVProperties(GV, VD);
7065 if (GV->getDLLStorageClass() == llvm::GlobalVariable::DLLExportStorageClass)
7066 // The reference temporary should never be dllexport.
7067 GV->setDLLStorageClass(llvm::GlobalVariable::DefaultStorageClass);
7068 }
7069 GV->setAlignment(Align.getAsAlign());
7070 if (supportsCOMDAT() && GV->isWeakForLinker())
7071 GV->setComdat(TheModule.getOrInsertComdat(GV->getName()));
7072 if (VD->getTLSKind())
7073 setTLSMode(GV, *VD);
7074 llvm::Constant *CV = GV;
7075 if (AddrSpace != LangAS::Default)
7077 *this, GV, AddrSpace,
7078 llvm::PointerType::get(
7080 getContext().getTargetAddressSpace(LangAS::Default)));
7081
7082 // Update the map with the new temporary. If we created a placeholder above,
7083 // replace it with the new global now.
7084 llvm::Constant *&Entry = MaterializedGlobalTemporaryMap[E];
7085 if (Entry) {
7086 Entry->replaceAllUsesWith(CV);
7087 llvm::cast<llvm::GlobalVariable>(Entry)->eraseFromParent();
7088 }
7089 Entry = CV;
7090
7091 return ConstantAddress(CV, Type, Align);
7092}
7093
7094/// EmitObjCPropertyImplementations - Emit information for synthesized
7095/// properties for an implementation.
7096void CodeGenModule::EmitObjCPropertyImplementations(const
7098 for (const auto *PID : D->property_impls()) {
7099 // Dynamic is just for type-checking.
7100 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
7101 ObjCPropertyDecl *PD = PID->getPropertyDecl();
7102
7103 // Determine which methods need to be implemented, some may have
7104 // been overridden. Note that ::isPropertyAccessor is not the method
7105 // we want, that just indicates if the decl came from a
7106 // property. What we want to know is if the method is defined in
7107 // this implementation.
7108 auto *Getter = PID->getGetterMethodDecl();
7109 if (!Getter || Getter->isSynthesizedAccessorStub())
7111 const_cast<ObjCImplementationDecl *>(D), PID);
7112 auto *Setter = PID->getSetterMethodDecl();
7113 if (!PD->isReadOnly() && (!Setter || Setter->isSynthesizedAccessorStub()))
7115 const_cast<ObjCImplementationDecl *>(D), PID);
7116 }
7117 }
7118}
7119
7121 const ObjCInterfaceDecl *iface = impl->getClassInterface();
7122 for (const ObjCIvarDecl *ivar = iface->all_declared_ivar_begin();
7123 ivar; ivar = ivar->getNextIvar())
7124 if (ivar->getType().isDestructedType())
7125 return true;
7126
7127 return false;
7128}
7129
7132 CodeGenFunction CGF(CGM);
7133 for (ObjCImplementationDecl::init_iterator B = D->init_begin(),
7134 E = D->init_end(); B != E; ++B) {
7135 CXXCtorInitializer *CtorInitExp = *B;
7136 Expr *Init = CtorInitExp->getInit();
7137 if (!CGF.isTrivialInitializer(Init))
7138 return false;
7139 }
7140 return true;
7141}
7142
7143/// EmitObjCIvarInitializations - Emit information for ivar initialization
7144/// for an implementation.
7145void CodeGenModule::EmitObjCIvarInitializations(ObjCImplementationDecl *D) {
7146 // We might need a .cxx_destruct even if we don't have any ivar initializers.
7147 if (needsDestructMethod(D)) {
7148 const IdentifierInfo *II = &getContext().Idents.get(".cxx_destruct");
7149 Selector cxxSelector = getContext().Selectors.getSelector(0, &II);
7151 getContext(), D->getLocation(), D->getLocation(), cxxSelector,
7152 getContext().VoidTy, nullptr, D,
7153 /*isInstance=*/true, /*isVariadic=*/false,
7154 /*isPropertyAccessor=*/true, /*isSynthesizedAccessorStub=*/false,
7155 /*isImplicitlyDeclared=*/true,
7156 /*isDefined=*/false, ObjCImplementationControl::Required);
7157 D->addInstanceMethod(DTORMethod);
7158 CodeGenFunction(*this).GenerateObjCCtorDtorMethod(D, DTORMethod, false);
7159 D->setHasDestructors(true);
7160 }
7161
7162 // If the implementation doesn't have any ivar initializers, we don't need
7163 // a .cxx_construct.
7164 if (D->getNumIvarInitializers() == 0 ||
7165 AllTrivialInitializers(*this, D))
7166 return;
7167
7168 const IdentifierInfo *II = &getContext().Idents.get(".cxx_construct");
7169 Selector cxxSelector = getContext().Selectors.getSelector(0, &II);
7170 // The constructor returns 'self'.
7172 getContext(), D->getLocation(), D->getLocation(), cxxSelector,
7173 getContext().getObjCIdType(), nullptr, D, /*isInstance=*/true,
7174 /*isVariadic=*/false,
7175 /*isPropertyAccessor=*/true, /*isSynthesizedAccessorStub=*/false,
7176 /*isImplicitlyDeclared=*/true,
7177 /*isDefined=*/false, ObjCImplementationControl::Required);
7178 D->addInstanceMethod(CTORMethod);
7179 CodeGenFunction(*this).GenerateObjCCtorDtorMethod(D, CTORMethod, true);
7180 D->setHasNonZeroConstructors(true);
7181}
7182
7183// EmitLinkageSpec - Emit all declarations in a linkage spec.
7184void CodeGenModule::EmitLinkageSpec(const LinkageSpecDecl *LSD) {
7185 if (LSD->getLanguage() != LinkageSpecLanguageIDs::C &&
7187 ErrorUnsupported(LSD, "linkage spec");
7188 return;
7189 }
7190
7191 EmitDeclContext(LSD);
7192}
7193
7194void CodeGenModule::EmitTopLevelStmt(const TopLevelStmtDecl *D) {
7195 // Device code should not be at top level.
7196 if (LangOpts.CUDA && LangOpts.CUDAIsDevice)
7197 return;
7198
7199 std::unique_ptr<CodeGenFunction> &CurCGF =
7200 GlobalTopLevelStmtBlockInFlight.first;
7201
7202 // We emitted a top-level stmt but after it there is initialization.
7203 // Stop squashing the top-level stmts into a single function.
7204 if (CurCGF && CXXGlobalInits.back() != CurCGF->CurFn) {
7205 CurCGF->FinishFunction(D->getEndLoc());
7206 CurCGF = nullptr;
7207 }
7208
7209 if (!CurCGF) {
7210 // void __stmts__N(void)
7211 // FIXME: Ask the ABI name mangler to pick a name.
7212 std::string Name = "__stmts__" + llvm::utostr(CXXGlobalInits.size());
7213 FunctionArgList Args;
7214 QualType RetTy = getContext().VoidTy;
7215 const CGFunctionInfo &FnInfo =
7217 llvm::FunctionType *FnTy = getTypes().GetFunctionType(FnInfo);
7218 llvm::Function *Fn = llvm::Function::Create(
7219 FnTy, llvm::GlobalValue::InternalLinkage, Name, &getModule());
7220
7221 CurCGF.reset(new CodeGenFunction(*this));
7222 GlobalTopLevelStmtBlockInFlight.second = D;
7223 CurCGF->StartFunction(GlobalDecl(), RetTy, Fn, FnInfo, Args,
7224 D->getBeginLoc(), D->getBeginLoc());
7225 CXXGlobalInits.push_back(Fn);
7226 }
7227
7228 CurCGF->EmitStmt(D->getStmt());
7229}
7230
7231void CodeGenModule::EmitDeclContext(const DeclContext *DC) {
7232 for (auto *I : DC->decls()) {
7233 // Unlike other DeclContexts, the contents of an ObjCImplDecl at TU scope
7234 // are themselves considered "top-level", so EmitTopLevelDecl on an
7235 // ObjCImplDecl does not recursively visit them. We need to do that in
7236 // case they're nested inside another construct (LinkageSpecDecl /
7237 // ExportDecl) that does stop them from being considered "top-level".
7238 if (auto *OID = dyn_cast<ObjCImplDecl>(I)) {
7239 for (auto *M : OID->methods())
7241 }
7242
7244 }
7245}
7246
7247/// EmitTopLevelDecl - Emit code for a single top level declaration.
7249 // Ignore dependent declarations.
7250 if (D->isTemplated())
7251 return;
7252
7253 // Consteval function shouldn't be emitted.
7254 if (auto *FD = dyn_cast<FunctionDecl>(D); FD && FD->isImmediateFunction())
7255 return;
7256
7257 switch (D->getKind()) {
7258 case Decl::CXXConversion:
7259 case Decl::CXXMethod:
7260 case Decl::Function:
7261 EmitGlobal(cast<FunctionDecl>(D));
7262 // Always provide some coverage mapping
7263 // even for the functions that aren't emitted.
7265 break;
7266
7267 case Decl::CXXDeductionGuide:
7268 // Function-like, but does not result in code emission.
7269 break;
7270
7271 case Decl::Var:
7272 case Decl::Decomposition:
7273 case Decl::VarTemplateSpecialization:
7274 EmitGlobal(cast<VarDecl>(D));
7275 if (auto *DD = dyn_cast<DecompositionDecl>(D))
7276 for (auto *B : DD->flat_bindings())
7277 if (auto *HD = B->getHoldingVar())
7278 EmitGlobal(HD);
7279
7280 break;
7281
7282 // Indirect fields from global anonymous structs and unions can be
7283 // ignored; only the actual variable requires IR gen support.
7284 case Decl::IndirectField:
7285 break;
7286
7287 // C++ Decls
7288 case Decl::Namespace:
7289 EmitDeclContext(cast<NamespaceDecl>(D));
7290 break;
7291 case Decl::ClassTemplateSpecialization: {
7292 const auto *Spec = cast<ClassTemplateSpecializationDecl>(D);
7293 if (CGDebugInfo *DI = getModuleDebugInfo())
7294 if (Spec->getSpecializationKind() ==
7296 Spec->hasDefinition())
7297 DI->completeTemplateDefinition(*Spec);
7298 } [[fallthrough]];
7299 case Decl::CXXRecord: {
7300 CXXRecordDecl *CRD = cast<CXXRecordDecl>(D);
7301 if (CGDebugInfo *DI = getModuleDebugInfo()) {
7302 if (CRD->hasDefinition())
7303 DI->EmitAndRetainType(
7304 getContext().getCanonicalTagType(cast<RecordDecl>(D)));
7305 if (auto *ES = D->getASTContext().getExternalSource())
7306 if (ES->hasExternalDefinitions(D) == ExternalASTSource::EK_Never)
7307 DI->completeUnusedClass(*CRD);
7308 }
7309 // Emit any static data members, they may be definitions.
7310 for (auto *I : CRD->decls())
7311 if (isa<VarDecl>(I) || isa<CXXRecordDecl>(I) || isa<EnumDecl>(I))
7313 break;
7314 }
7315 // No code generation needed.
7316 case Decl::UsingShadow:
7317 case Decl::ClassTemplate:
7318 case Decl::VarTemplate:
7319 case Decl::Concept:
7320 case Decl::VarTemplatePartialSpecialization:
7321 case Decl::FunctionTemplate:
7322 case Decl::TypeAliasTemplate:
7323 case Decl::Block:
7324 case Decl::Empty:
7325 case Decl::Binding:
7326 break;
7327 case Decl::Using: // using X; [C++]
7328 if (CGDebugInfo *DI = getModuleDebugInfo())
7329 DI->EmitUsingDecl(cast<UsingDecl>(*D));
7330 break;
7331 case Decl::UsingEnum: // using enum X; [C++]
7332 if (CGDebugInfo *DI = getModuleDebugInfo())
7333 DI->EmitUsingEnumDecl(cast<UsingEnumDecl>(*D));
7334 break;
7335 case Decl::NamespaceAlias:
7336 if (CGDebugInfo *DI = getModuleDebugInfo())
7337 DI->EmitNamespaceAlias(cast<NamespaceAliasDecl>(*D));
7338 break;
7339 case Decl::UsingDirective: // using namespace X; [C++]
7340 if (CGDebugInfo *DI = getModuleDebugInfo())
7341 DI->EmitUsingDirective(cast<UsingDirectiveDecl>(*D));
7342 break;
7343 case Decl::CXXConstructor:
7344 getCXXABI().EmitCXXConstructors(cast<CXXConstructorDecl>(D));
7345 break;
7346 case Decl::CXXDestructor:
7347 getCXXABI().EmitCXXDestructors(cast<CXXDestructorDecl>(D));
7348 break;
7349
7350 case Decl::StaticAssert:
7351 // Nothing to do.
7352 break;
7353
7354 // Objective-C Decls
7355
7356 // Forward declarations, no (immediate) code generation.
7357 case Decl::ObjCInterface:
7358 case Decl::ObjCCategory:
7359 break;
7360
7361 case Decl::ObjCProtocol: {
7362 auto *Proto = cast<ObjCProtocolDecl>(D);
7363 if (Proto->isThisDeclarationADefinition())
7364 ObjCRuntime->GenerateProtocol(Proto);
7365 break;
7366 }
7367
7368 case Decl::ObjCCategoryImpl:
7369 // Categories have properties but don't support synthesize so we
7370 // can ignore them here.
7371 ObjCRuntime->GenerateCategory(cast<ObjCCategoryImplDecl>(D));
7372 break;
7373
7374 case Decl::ObjCImplementation: {
7375 auto *OMD = cast<ObjCImplementationDecl>(D);
7376 EmitObjCPropertyImplementations(OMD);
7377 EmitObjCIvarInitializations(OMD);
7378 ObjCRuntime->GenerateClass(OMD);
7379 // Emit global variable debug information.
7380 if (CGDebugInfo *DI = getModuleDebugInfo())
7381 if (getCodeGenOpts().hasReducedDebugInfo())
7382 DI->getOrCreateInterfaceType(getContext().getObjCInterfaceType(
7383 OMD->getClassInterface()), OMD->getLocation());
7384 break;
7385 }
7386 case Decl::ObjCMethod: {
7387 auto *OMD = cast<ObjCMethodDecl>(D);
7388 // If this is not a prototype, emit the body.
7389 if (OMD->getBody())
7391 break;
7392 }
7393 case Decl::ObjCCompatibleAlias:
7394 ObjCRuntime->RegisterAlias(cast<ObjCCompatibleAliasDecl>(D));
7395 break;
7396
7397 case Decl::PragmaComment: {
7398 const auto *PCD = cast<PragmaCommentDecl>(D);
7399 switch (PCD->getCommentKind()) {
7400 case PCK_Unknown:
7401 llvm_unreachable("unexpected pragma comment kind");
7402 case PCK_Linker:
7403 AppendLinkerOptions(PCD->getArg());
7404 break;
7405 case PCK_Lib:
7406 AddDependentLib(PCD->getArg());
7407 break;
7408 case PCK_Compiler:
7409 case PCK_ExeStr:
7410 case PCK_User:
7411 break; // We ignore all of these.
7412 }
7413 break;
7414 }
7415
7416 case Decl::PragmaDetectMismatch: {
7417 const auto *PDMD = cast<PragmaDetectMismatchDecl>(D);
7418 AddDetectMismatch(PDMD->getName(), PDMD->getValue());
7419 break;
7420 }
7421
7422 case Decl::LinkageSpec:
7423 EmitLinkageSpec(cast<LinkageSpecDecl>(D));
7424 break;
7425
7426 case Decl::FileScopeAsm: {
7427 // File-scope asm is ignored during device-side CUDA compilation.
7428 if (LangOpts.CUDA && LangOpts.CUDAIsDevice)
7429 break;
7430 // File-scope asm is ignored during device-side OpenMP compilation.
7431 if (LangOpts.OpenMPIsTargetDevice)
7432 break;
7433 // File-scope asm is ignored during device-side SYCL compilation.
7434 if (LangOpts.SYCLIsDevice)
7435 break;
7436 auto *AD = cast<FileScopeAsmDecl>(D);
7437 getModule().appendModuleInlineAsm(AD->getAsmString());
7438 break;
7439 }
7440
7441 case Decl::TopLevelStmt:
7442 EmitTopLevelStmt(cast<TopLevelStmtDecl>(D));
7443 break;
7444
7445 case Decl::Import: {
7446 auto *Import = cast<ImportDecl>(D);
7447
7448 // If we've already imported this module, we're done.
7449 if (!ImportedModules.insert(Import->getImportedModule()))
7450 break;
7451
7452 // Emit debug information for direct imports.
7453 if (!Import->getImportedOwningModule()) {
7454 if (CGDebugInfo *DI = getModuleDebugInfo())
7455 DI->EmitImportDecl(*Import);
7456 }
7457
7458 // For C++ standard modules we are done - we will call the module
7459 // initializer for imported modules, and that will likewise call those for
7460 // any imports it has.
7461 if (CXX20ModuleInits && Import->getImportedModule() &&
7462 Import->getImportedModule()->isNamedModule())
7463 break;
7464
7465 // For clang C++ module map modules the initializers for sub-modules are
7466 // emitted here.
7467
7468 // Find all of the submodules and emit the module initializers.
7471 Visited.insert(Import->getImportedModule());
7472 Stack.push_back(Import->getImportedModule());
7473
7474 while (!Stack.empty()) {
7475 clang::Module *Mod = Stack.pop_back_val();
7476 if (!EmittedModuleInitializers.insert(Mod).second)
7477 continue;
7478
7479 for (auto *D : Context.getModuleInitializers(Mod))
7481
7482 // Visit the submodules of this module.
7483 for (auto *Submodule : Mod->submodules()) {
7484 // Skip explicit children; they need to be explicitly imported to emit
7485 // the initializers.
7486 if (Submodule->IsExplicit)
7487 continue;
7488
7489 if (Visited.insert(Submodule).second)
7490 Stack.push_back(Submodule);
7491 }
7492 }
7493 break;
7494 }
7495
7496 case Decl::Export:
7497 EmitDeclContext(cast<ExportDecl>(D));
7498 break;
7499
7500 case Decl::OMPThreadPrivate:
7501 EmitOMPThreadPrivateDecl(cast<OMPThreadPrivateDecl>(D));
7502 break;
7503
7504 case Decl::OMPAllocate:
7505 EmitOMPAllocateDecl(cast<OMPAllocateDecl>(D));
7506 break;
7507
7508 case Decl::OMPDeclareReduction:
7509 EmitOMPDeclareReduction(cast<OMPDeclareReductionDecl>(D));
7510 break;
7511
7512 case Decl::OMPDeclareMapper:
7513 EmitOMPDeclareMapper(cast<OMPDeclareMapperDecl>(D));
7514 break;
7515
7516 case Decl::OMPRequires:
7517 EmitOMPRequiresDecl(cast<OMPRequiresDecl>(D));
7518 break;
7519
7520 case Decl::Typedef:
7521 case Decl::TypeAlias: // using foo = bar; [C++11]
7522 if (CGDebugInfo *DI = getModuleDebugInfo())
7523 DI->EmitAndRetainType(getContext().getTypedefType(
7524 ElaboratedTypeKeyword::None, /*Qualifier=*/std::nullopt,
7525 cast<TypedefNameDecl>(D)));
7526 break;
7527
7528 case Decl::Record:
7529 if (CGDebugInfo *DI = getModuleDebugInfo())
7530 if (cast<RecordDecl>(D)->getDefinition())
7531 DI->EmitAndRetainType(
7532 getContext().getCanonicalTagType(cast<RecordDecl>(D)));
7533 break;
7534
7535 case Decl::Enum:
7536 if (CGDebugInfo *DI = getModuleDebugInfo())
7537 if (cast<EnumDecl>(D)->getDefinition())
7538 DI->EmitAndRetainType(
7539 getContext().getCanonicalTagType(cast<EnumDecl>(D)));
7540 break;
7541
7542 case Decl::HLSLRootSignature:
7543 // Will be handled by attached function
7544 break;
7545 case Decl::HLSLBuffer:
7546 getHLSLRuntime().addBuffer(cast<HLSLBufferDecl>(D));
7547 break;
7548
7549 case Decl::OpenACCDeclare:
7550 EmitOpenACCDeclare(cast<OpenACCDeclareDecl>(D));
7551 break;
7552 case Decl::OpenACCRoutine:
7553 EmitOpenACCRoutine(cast<OpenACCRoutineDecl>(D));
7554 break;
7555
7556 default:
7557 // Make sure we handled everything we should, every other kind is a
7558 // non-top-level decl. FIXME: Would be nice to have an isTopLevelDeclKind
7559 // function. Need to recode Decl::Kind to do that easily.
7560 assert(isa<TypeDecl>(D) && "Unsupported decl kind");
7561 break;
7562 }
7563}
7564
7566 // Do we need to generate coverage mapping?
7567 if (!CodeGenOpts.CoverageMapping)
7568 return;
7569 switch (D->getKind()) {
7570 case Decl::CXXConversion:
7571 case Decl::CXXMethod:
7572 case Decl::Function:
7573 case Decl::ObjCMethod:
7574 case Decl::CXXConstructor:
7575 case Decl::CXXDestructor: {
7576 if (!cast<FunctionDecl>(D)->doesThisDeclarationHaveABody())
7577 break;
7579 if (LimitedCoverage && SM.getMainFileID() != SM.getFileID(D->getBeginLoc()))
7580 break;
7582 SM.isInSystemHeader(D->getBeginLoc()))
7583 break;
7584 DeferredEmptyCoverageMappingDecls.try_emplace(D, true);
7585 break;
7586 }
7587 default:
7588 break;
7589 };
7590}
7591
7593 // Do we need to generate coverage mapping?
7594 if (!CodeGenOpts.CoverageMapping)
7595 return;
7596 if (const auto *Fn = dyn_cast<FunctionDecl>(D)) {
7597 if (Fn->isTemplateInstantiation())
7598 ClearUnusedCoverageMapping(Fn->getTemplateInstantiationPattern());
7599 }
7600 DeferredEmptyCoverageMappingDecls.insert_or_assign(D, false);
7601}
7602
7604 // We call takeVector() here to avoid use-after-free.
7605 // FIXME: DeferredEmptyCoverageMappingDecls is getting mutated because
7606 // we deserialize function bodies to emit coverage info for them, and that
7607 // deserializes more declarations. How should we handle that case?
7608 for (const auto &Entry : DeferredEmptyCoverageMappingDecls.takeVector()) {
7609 if (!Entry.second)
7610 continue;
7611 const Decl *D = Entry.first;
7612 switch (D->getKind()) {
7613 case Decl::CXXConversion:
7614 case Decl::CXXMethod:
7615 case Decl::Function:
7616 case Decl::ObjCMethod: {
7617 CodeGenPGO PGO(*this);
7618 GlobalDecl GD(cast<FunctionDecl>(D));
7620 getFunctionLinkage(GD));
7621 break;
7622 }
7623 case Decl::CXXConstructor: {
7624 CodeGenPGO PGO(*this);
7625 GlobalDecl GD(cast<CXXConstructorDecl>(D), Ctor_Base);
7627 getFunctionLinkage(GD));
7628 break;
7629 }
7630 case Decl::CXXDestructor: {
7631 CodeGenPGO PGO(*this);
7632 GlobalDecl GD(cast<CXXDestructorDecl>(D), Dtor_Base);
7634 getFunctionLinkage(GD));
7635 break;
7636 }
7637 default:
7638 break;
7639 };
7640 }
7641}
7642
7644 // In order to transition away from "__original_main" gracefully, emit an
7645 // alias for "main" in the no-argument case so that libc can detect when
7646 // new-style no-argument main is in used.
7647 if (llvm::Function *F = getModule().getFunction("main")) {
7648 if (!F->isDeclaration() && F->arg_size() == 0 && !F->isVarArg() &&
7649 F->getReturnType()->isIntegerTy(Context.getTargetInfo().getIntWidth())) {
7650 auto *GA = llvm::GlobalAlias::create("__main_void", F);
7651 GA->setVisibility(llvm::GlobalValue::HiddenVisibility);
7652 }
7653 }
7654}
7655
7656/// Turns the given pointer into a constant.
7657static llvm::Constant *GetPointerConstant(llvm::LLVMContext &Context,
7658 const void *Ptr) {
7659 uintptr_t PtrInt = reinterpret_cast<uintptr_t>(Ptr);
7660 llvm::Type *i64 = llvm::Type::getInt64Ty(Context);
7661 return llvm::ConstantInt::get(i64, PtrInt);
7662}
7663
7665 llvm::NamedMDNode *&GlobalMetadata,
7666 GlobalDecl D,
7667 llvm::GlobalValue *Addr) {
7668 if (!GlobalMetadata)
7669 GlobalMetadata =
7670 CGM.getModule().getOrInsertNamedMetadata("clang.global.decl.ptrs");
7671
7672 // TODO: should we report variant information for ctors/dtors?
7673 llvm::Metadata *Ops[] = {llvm::ConstantAsMetadata::get(Addr),
7674 llvm::ConstantAsMetadata::get(GetPointerConstant(
7675 CGM.getLLVMContext(), D.getDecl()))};
7676 GlobalMetadata->addOperand(llvm::MDNode::get(CGM.getLLVMContext(), Ops));
7677}
7678
7679bool CodeGenModule::CheckAndReplaceExternCIFuncs(llvm::GlobalValue *Elem,
7680 llvm::GlobalValue *CppFunc) {
7681 // Store the list of ifuncs we need to replace uses in.
7683 // List of ConstantExprs that we should be able to delete when we're done
7684 // here.
7686
7687 // It isn't valid to replace the extern-C ifuncs if all we find is itself!
7688 if (Elem == CppFunc)
7689 return false;
7690
7691 // First make sure that all users of this are ifuncs (or ifuncs via a
7692 // bitcast), and collect the list of ifuncs and CEs so we can work on them
7693 // later.
7694 for (llvm::User *User : Elem->users()) {
7695 // Users can either be a bitcast ConstExpr that is used by the ifuncs, OR an
7696 // ifunc directly. In any other case, just give up, as we don't know what we
7697 // could break by changing those.
7698 if (auto *ConstExpr = dyn_cast<llvm::ConstantExpr>(User)) {
7699 if (ConstExpr->getOpcode() != llvm::Instruction::BitCast)
7700 return false;
7701
7702 for (llvm::User *CEUser : ConstExpr->users()) {
7703 if (auto *IFunc = dyn_cast<llvm::GlobalIFunc>(CEUser)) {
7704 IFuncs.push_back(IFunc);
7705 } else {
7706 return false;
7707 }
7708 }
7709 CEs.push_back(ConstExpr);
7710 } else if (auto *IFunc = dyn_cast<llvm::GlobalIFunc>(User)) {
7711 IFuncs.push_back(IFunc);
7712 } else {
7713 // This user is one we don't know how to handle, so fail redirection. This
7714 // will result in an ifunc retaining a resolver name that will ultimately
7715 // fail to be resolved to a defined function.
7716 return false;
7717 }
7718 }
7719
7720 // Now we know this is a valid case where we can do this alias replacement, we
7721 // need to remove all of the references to Elem (and the bitcasts!) so we can
7722 // delete it.
7723 for (llvm::GlobalIFunc *IFunc : IFuncs)
7724 IFunc->setResolver(nullptr);
7725 for (llvm::ConstantExpr *ConstExpr : CEs)
7726 ConstExpr->destroyConstant();
7727
7728 // We should now be out of uses for the 'old' version of this function, so we
7729 // can erase it as well.
7730 Elem->eraseFromParent();
7731
7732 for (llvm::GlobalIFunc *IFunc : IFuncs) {
7733 // The type of the resolver is always just a function-type that returns the
7734 // type of the IFunc, so create that here. If the type of the actual
7735 // resolver doesn't match, it just gets bitcast to the right thing.
7736 auto *ResolverTy =
7737 llvm::FunctionType::get(IFunc->getType(), /*isVarArg*/ false);
7738 llvm::Constant *Resolver = GetOrCreateLLVMFunction(
7739 CppFunc->getName(), ResolverTy, {}, /*ForVTable*/ false);
7740 IFunc->setResolver(Resolver);
7741 }
7742 return true;
7743}
7744
7745/// For each function which is declared within an extern "C" region and marked
7746/// as 'used', but has internal linkage, create an alias from the unmangled
7747/// name to the mangled name if possible. People expect to be able to refer
7748/// to such functions with an unmangled name from inline assembly within the
7749/// same translation unit.
7750void CodeGenModule::EmitStaticExternCAliases() {
7751 if (!getTargetCodeGenInfo().shouldEmitStaticExternCAliases())
7752 return;
7753 for (auto &I : StaticExternCValues) {
7754 const IdentifierInfo *Name = I.first;
7755 llvm::GlobalValue *Val = I.second;
7756
7757 // If Val is null, that implies there were multiple declarations that each
7758 // had a claim to the unmangled name. In this case, generation of the alias
7759 // is suppressed. See CodeGenModule::MaybeHandleStaticInExternC.
7760 if (!Val)
7761 break;
7762
7763 llvm::GlobalValue *ExistingElem =
7764 getModule().getNamedValue(Name->getName());
7765
7766 // If there is either not something already by this name, or we were able to
7767 // replace all uses from IFuncs, create the alias.
7768 if (!ExistingElem || CheckAndReplaceExternCIFuncs(ExistingElem, Val))
7769 addCompilerUsedGlobal(llvm::GlobalAlias::create(Name->getName(), Val));
7770 }
7771}
7772
7774 GlobalDecl &Result) const {
7775 auto Res = Manglings.find(MangledName);
7776 if (Res == Manglings.end())
7777 return false;
7778 Result = Res->getValue();
7779 return true;
7780}
7781
7782/// Emits metadata nodes associating all the global values in the
7783/// current module with the Decls they came from. This is useful for
7784/// projects using IR gen as a subroutine.
7785///
7786/// Since there's currently no way to associate an MDNode directly
7787/// with an llvm::GlobalValue, we create a global named metadata
7788/// with the name 'clang.global.decl.ptrs'.
7789void CodeGenModule::EmitDeclMetadata() {
7790 llvm::NamedMDNode *GlobalMetadata = nullptr;
7791
7792 for (auto &I : MangledDeclNames) {
7793 llvm::GlobalValue *Addr = getModule().getNamedValue(I.second);
7794 // Some mangled names don't necessarily have an associated GlobalValue
7795 // in this module, e.g. if we mangled it for DebugInfo.
7796 if (Addr)
7797 EmitGlobalDeclMetadata(*this, GlobalMetadata, I.first, Addr);
7798 }
7799}
7800
7801/// Emits metadata nodes for all the local variables in the current
7802/// function.
7803void CodeGenFunction::EmitDeclMetadata() {
7804 if (LocalDeclMap.empty()) return;
7805
7806 llvm::LLVMContext &Context = getLLVMContext();
7807
7808 // Find the unique metadata ID for this name.
7809 unsigned DeclPtrKind = Context.getMDKindID("clang.decl.ptr");
7810
7811 llvm::NamedMDNode *GlobalMetadata = nullptr;
7812
7813 for (auto &I : LocalDeclMap) {
7814 const Decl *D = I.first;
7815 llvm::Value *Addr = I.second.emitRawPointer(*this);
7816 if (auto *Alloca = dyn_cast<llvm::AllocaInst>(Addr)) {
7817 llvm::Value *DAddr = GetPointerConstant(getLLVMContext(), D);
7818 Alloca->setMetadata(
7819 DeclPtrKind, llvm::MDNode::get(
7820 Context, llvm::ValueAsMetadata::getConstant(DAddr)));
7821 } else if (auto *GV = dyn_cast<llvm::GlobalValue>(Addr)) {
7822 GlobalDecl GD = GlobalDecl(cast<VarDecl>(D));
7823 EmitGlobalDeclMetadata(CGM, GlobalMetadata, GD, GV);
7824 }
7825 }
7826}
7827
7828void CodeGenModule::EmitVersionIdentMetadata() {
7829 llvm::NamedMDNode *IdentMetadata =
7830 TheModule.getOrInsertNamedMetadata("llvm.ident");
7831 std::string Version = getClangFullVersion();
7832 llvm::LLVMContext &Ctx = TheModule.getContext();
7833
7834 llvm::Metadata *IdentNode[] = {llvm::MDString::get(Ctx, Version)};
7835 IdentMetadata->addOperand(llvm::MDNode::get(Ctx, IdentNode));
7836}
7837
7838void CodeGenModule::EmitCommandLineMetadata() {
7839 llvm::NamedMDNode *CommandLineMetadata =
7840 TheModule.getOrInsertNamedMetadata("llvm.commandline");
7841 std::string CommandLine = getCodeGenOpts().RecordCommandLine;
7842 llvm::LLVMContext &Ctx = TheModule.getContext();
7843
7844 llvm::Metadata *CommandLineNode[] = {llvm::MDString::get(Ctx, CommandLine)};
7845 CommandLineMetadata->addOperand(llvm::MDNode::get(Ctx, CommandLineNode));
7846}
7847
7848void CodeGenModule::EmitCoverageFile() {
7849 llvm::NamedMDNode *CUNode = TheModule.getNamedMetadata("llvm.dbg.cu");
7850 if (!CUNode)
7851 return;
7852
7853 llvm::NamedMDNode *GCov = TheModule.getOrInsertNamedMetadata("llvm.gcov");
7854 llvm::LLVMContext &Ctx = TheModule.getContext();
7855 auto *CoverageDataFile =
7856 llvm::MDString::get(Ctx, getCodeGenOpts().CoverageDataFile);
7857 auto *CoverageNotesFile =
7858 llvm::MDString::get(Ctx, getCodeGenOpts().CoverageNotesFile);
7859 for (int i = 0, e = CUNode->getNumOperands(); i != e; ++i) {
7860 llvm::MDNode *CU = CUNode->getOperand(i);
7861 llvm::Metadata *Elts[] = {CoverageNotesFile, CoverageDataFile, CU};
7862 GCov->addOperand(llvm::MDNode::get(Ctx, Elts));
7863 }
7864}
7865
7867 bool ForEH) {
7868 // Return a bogus pointer if RTTI is disabled, unless it's for EH.
7869 // FIXME: should we even be calling this method if RTTI is disabled
7870 // and it's not for EH?
7871 if (!shouldEmitRTTI(ForEH))
7872 return llvm::Constant::getNullValue(GlobalsInt8PtrTy);
7873
7874 if (ForEH && Ty->isObjCObjectPointerType() &&
7875 LangOpts.ObjCRuntime.isGNUFamily())
7876 return ObjCRuntime->GetEHType(Ty);
7877
7879}
7880
7882 // Do not emit threadprivates in simd-only mode.
7883 if (LangOpts.OpenMP && LangOpts.OpenMPSimd)
7884 return;
7885 for (auto RefExpr : D->varlist()) {
7886 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(RefExpr)->getDecl());
7887 bool PerformInit =
7888 VD->getAnyInitializer() &&
7889 !VD->getAnyInitializer()->isConstantInitializer(getContext(),
7890 /*ForRef=*/false);
7891
7893 getTypes().ConvertTypeForMem(VD->getType()),
7894 getContext().getDeclAlign(VD));
7895 if (auto InitFunction = getOpenMPRuntime().emitThreadPrivateVarDefinition(
7896 VD, Addr, RefExpr->getBeginLoc(), PerformInit))
7897 CXXGlobalInits.push_back(InitFunction);
7898 }
7899}
7900
7901llvm::Metadata *
7902CodeGenModule::CreateMetadataIdentifierImpl(QualType T, MetadataTypeMap &Map,
7903 StringRef Suffix) {
7904 if (auto *FnType = T->getAs<FunctionProtoType>())
7906 FnType->getReturnType(), FnType->getParamTypes(),
7907 FnType->getExtProtoInfo().withExceptionSpec(EST_None));
7908
7909 llvm::Metadata *&InternalId = Map[T.getCanonicalType()];
7910 if (InternalId)
7911 return InternalId;
7912
7914 std::string OutName;
7915 llvm::raw_string_ostream Out(OutName);
7917 T, Out, getCodeGenOpts().SanitizeCfiICallNormalizeIntegers);
7918
7919 if (getCodeGenOpts().SanitizeCfiICallNormalizeIntegers)
7920 Out << ".normalized";
7921
7922 Out << Suffix;
7923
7924 InternalId = llvm::MDString::get(getLLVMContext(), Out.str());
7925 } else {
7926 InternalId = llvm::MDNode::getDistinct(getLLVMContext(),
7928 }
7929
7930 return InternalId;
7931}
7932
7934 return CreateMetadataIdentifierImpl(T, MetadataIdMap, "");
7935}
7936
7937llvm::Metadata *
7939 return CreateMetadataIdentifierImpl(T, VirtualMetadataIdMap, ".virtual");
7940}
7941
7943 return CreateMetadataIdentifierImpl(GeneralizeFunctionType(getContext(), T),
7944 GeneralizedMetadataIdMap, ".generalized");
7945}
7946
7947/// Returns whether this module needs the "all-vtables" type identifier.
7949 // Returns true if at least one of vtable-based CFI checkers is enabled and
7950 // is not in the trapping mode.
7951 return ((LangOpts.Sanitize.has(SanitizerKind::CFIVCall) &&
7952 !CodeGenOpts.SanitizeTrap.has(SanitizerKind::CFIVCall)) ||
7953 (LangOpts.Sanitize.has(SanitizerKind::CFINVCall) &&
7954 !CodeGenOpts.SanitizeTrap.has(SanitizerKind::CFINVCall)) ||
7955 (LangOpts.Sanitize.has(SanitizerKind::CFIDerivedCast) &&
7956 !CodeGenOpts.SanitizeTrap.has(SanitizerKind::CFIDerivedCast)) ||
7957 (LangOpts.Sanitize.has(SanitizerKind::CFIUnrelatedCast) &&
7958 !CodeGenOpts.SanitizeTrap.has(SanitizerKind::CFIUnrelatedCast)));
7959}
7960
7961void CodeGenModule::AddVTableTypeMetadata(llvm::GlobalVariable *VTable,
7962 CharUnits Offset,
7963 const CXXRecordDecl *RD) {
7965 llvm::Metadata *MD = CreateMetadataIdentifierForType(T);
7966 VTable->addTypeMetadata(Offset.getQuantity(), MD);
7967
7968 if (CodeGenOpts.SanitizeCfiCrossDso)
7969 if (auto CrossDsoTypeId = CreateCrossDsoCfiTypeId(MD))
7970 VTable->addTypeMetadata(Offset.getQuantity(),
7971 llvm::ConstantAsMetadata::get(CrossDsoTypeId));
7972
7973 if (NeedAllVtablesTypeId()) {
7974 llvm::Metadata *MD = llvm::MDString::get(getLLVMContext(), "all-vtables");
7975 VTable->addTypeMetadata(Offset.getQuantity(), MD);
7976 }
7977}
7978
7979llvm::SanitizerStatReport &CodeGenModule::getSanStats() {
7980 if (!SanStats)
7981 SanStats = std::make_unique<llvm::SanitizerStatReport>(&getModule());
7982
7983 return *SanStats;
7984}
7985
7986llvm::Value *
7988 CodeGenFunction &CGF) {
7989 llvm::Constant *C = ConstantEmitter(CGF).emitAbstract(E, E->getType());
7990 auto *SamplerT = getOpenCLRuntime().getSamplerType(E->getType().getTypePtr());
7991 auto *FTy = llvm::FunctionType::get(SamplerT, {C->getType()}, false);
7992 auto *Call = CGF.EmitRuntimeCall(
7993 CreateRuntimeFunction(FTy, "__translate_sampler_initializer"), {C});
7994 return Call;
7995}
7996
7998 QualType T, LValueBaseInfo *BaseInfo, TBAAAccessInfo *TBAAInfo) {
7999 return getNaturalTypeAlignment(T->getPointeeType(), BaseInfo, TBAAInfo,
8000 /* forPointeeType= */ true);
8001}
8002
8004 LValueBaseInfo *BaseInfo,
8005 TBAAAccessInfo *TBAAInfo,
8006 bool forPointeeType) {
8007 if (TBAAInfo)
8008 *TBAAInfo = getTBAAAccessInfo(T);
8009
8010 // FIXME: This duplicates logic in ASTContext::getTypeAlignIfKnown. But
8011 // that doesn't return the information we need to compute BaseInfo.
8012
8013 // Honor alignment typedef attributes even on incomplete types.
8014 // We also honor them straight for C++ class types, even as pointees;
8015 // there's an expressivity gap here.
8016 if (auto TT = T->getAs<TypedefType>()) {
8017 if (auto Align = TT->getDecl()->getMaxAlignment()) {
8018 if (BaseInfo)
8020 return getContext().toCharUnitsFromBits(Align);
8021 }
8022 }
8023
8024 bool AlignForArray = T->isArrayType();
8025
8026 // Analyze the base element type, so we don't get confused by incomplete
8027 // array types.
8029
8030 if (T->isIncompleteType()) {
8031 // We could try to replicate the logic from
8032 // ASTContext::getTypeAlignIfKnown, but nothing uses the alignment if the
8033 // type is incomplete, so it's impossible to test. We could try to reuse
8034 // getTypeAlignIfKnown, but that doesn't return the information we need
8035 // to set BaseInfo. So just ignore the possibility that the alignment is
8036 // greater than one.
8037 if (BaseInfo)
8039 return CharUnits::One();
8040 }
8041
8042 if (BaseInfo)
8044
8045 CharUnits Alignment;
8046 const CXXRecordDecl *RD;
8047 if (T.getQualifiers().hasUnaligned()) {
8048 Alignment = CharUnits::One();
8049 } else if (forPointeeType && !AlignForArray &&
8050 (RD = T->getAsCXXRecordDecl())) {
8051 // For C++ class pointees, we don't know whether we're pointing at a
8052 // base or a complete object, so we generally need to use the
8053 // non-virtual alignment.
8054 Alignment = getClassPointerAlignment(RD);
8055 } else {
8056 Alignment = getContext().getTypeAlignInChars(T);
8057 }
8058
8059 // Cap to the global maximum type alignment unless the alignment
8060 // was somehow explicit on the type.
8061 if (unsigned MaxAlign = getLangOpts().MaxTypeAlign) {
8062 if (Alignment.getQuantity() > MaxAlign &&
8063 !getContext().isAlignmentRequired(T))
8064 Alignment = CharUnits::fromQuantity(MaxAlign);
8065 }
8066 return Alignment;
8067}
8068
8070 unsigned StopAfter = getContext().getLangOpts().TrivialAutoVarInitStopAfter;
8071 if (StopAfter) {
8072 // This number is positive only when -ftrivial-auto-var-init-stop-after=* is
8073 // used
8074 if (NumAutoVarInit >= StopAfter) {
8075 return true;
8076 }
8077 if (!NumAutoVarInit) {
8078 unsigned DiagID = getDiags().getCustomDiagID(
8080 "-ftrivial-auto-var-init-stop-after=%0 has been enabled to limit the "
8081 "number of times ftrivial-auto-var-init=%1 gets applied.");
8082 getDiags().Report(DiagID)
8083 << StopAfter
8084 << (getContext().getLangOpts().getTrivialAutoVarInit() ==
8086 ? "zero"
8087 : "pattern");
8088 }
8089 ++NumAutoVarInit;
8090 }
8091 return false;
8092}
8093
8095 const Decl *D) const {
8096 // ptxas does not allow '.' in symbol names. On the other hand, HIP prefers
8097 // postfix beginning with '.' since the symbol name can be demangled.
8098 if (LangOpts.HIP)
8099 OS << (isa<VarDecl>(D) ? ".static." : ".intern.");
8100 else
8101 OS << (isa<VarDecl>(D) ? "__static__" : "__intern__");
8102
8103 // If the CUID is not specified we try to generate a unique postfix.
8104 if (getLangOpts().CUID.empty()) {
8106 PresumedLoc PLoc = SM.getPresumedLoc(D->getLocation());
8107 assert(PLoc.isValid() && "Source location is expected to be valid.");
8108
8109 // Get the hash of the user defined macros.
8110 llvm::MD5 Hash;
8111 llvm::MD5::MD5Result Result;
8112 for (const auto &Arg : PreprocessorOpts.Macros)
8113 Hash.update(Arg.first);
8114 Hash.final(Result);
8115
8116 // Get the UniqueID for the file containing the decl.
8117 llvm::sys::fs::UniqueID ID;
8118 if (llvm::sys::fs::getUniqueID(PLoc.getFilename(), ID)) {
8119 PLoc = SM.getPresumedLoc(D->getLocation(), /*UseLineDirectives=*/false);
8120 assert(PLoc.isValid() && "Source location is expected to be valid.");
8121 if (auto EC = llvm::sys::fs::getUniqueID(PLoc.getFilename(), ID))
8122 SM.getDiagnostics().Report(diag::err_cannot_open_file)
8123 << PLoc.getFilename() << EC.message();
8124 }
8125 OS << llvm::format("%x", ID.getFile()) << llvm::format("%x", ID.getDevice())
8126 << "_" << llvm::utohexstr(Result.low(), /*LowerCase=*/true, /*Width=*/8);
8127 } else {
8128 OS << getContext().getCUIDHash();
8129 }
8130}
8131
8133 assert(DeferredDeclsToEmit.empty() &&
8134 "Should have emitted all decls deferred to emit.");
8135 assert(NewBuilder->DeferredDecls.empty() &&
8136 "Newly created module should not have deferred decls");
8137 NewBuilder->DeferredDecls = std::move(DeferredDecls);
8138 assert(EmittedDeferredDecls.empty() &&
8139 "Still have (unmerged) EmittedDeferredDecls deferred decls");
8140
8141 assert(NewBuilder->DeferredVTables.empty() &&
8142 "Newly created module should not have deferred vtables");
8143 NewBuilder->DeferredVTables = std::move(DeferredVTables);
8144
8145 assert(NewBuilder->MangledDeclNames.empty() &&
8146 "Newly created module should not have mangled decl names");
8147 assert(NewBuilder->Manglings.empty() &&
8148 "Newly created module should not have manglings");
8149 NewBuilder->Manglings = std::move(Manglings);
8150
8151 NewBuilder->WeakRefReferences = std::move(WeakRefReferences);
8152
8153 NewBuilder->ABI->MangleCtx = std::move(ABI->MangleCtx);
8154}
Defines the clang::ASTContext interface.
#define V(N, I)
Definition: ASTContext.h:3597
ASTImporterLookupTable & LT
This file provides some common utility functions for processing Lambda related AST Constructs.
Defines the Diagnostic-related interfaces.
Defines enum values for all the target-independent builtin functions.
static bool shouldAssumeDSOLocal(const CIRGenModule &cgm, cir::CIRGlobalValueInterface gv)
static bool shouldBeInCOMDAT(CIRGenModule &cgm, const Decl &d)
static std::string getMangledNameImpl(CIRGenModule &cgm, GlobalDecl gd, const NamedDecl *nd)
static CIRGenCXXABI * createCXXABI(CIRGenModule &cgm)
static bool isVarDeclStrongDefinition(const ASTContext &astContext, CIRGenModule &cgm, const VarDecl *vd, bool noCommon)
static void setLinkageForGV(cir::GlobalOp &gv, const NamedDecl *nd)
const Decl * D
IndirectLocalPath & Path
Expr * E
static void AppendCPUSpecificCPUDispatchMangling(const CodeGenModule &CGM, const CPUSpecificAttr *Attr, unsigned CPUIndex, raw_ostream &Out)
static bool AllTrivialInitializers(CodeGenModule &CGM, ObjCImplementationDecl *D)
static const FunctionDecl * GetRuntimeFunctionDecl(ASTContext &C, StringRef Name)
static GlobalDecl getBaseVariantGlobalDecl(const NamedDecl *D)
static void checkAliasForTocData(llvm::GlobalVariable *GVar, const CodeGenOptions &CodeGenOpts, DiagnosticsEngine &Diags, SourceLocation Location)
static bool hasImplicitAttr(const ValueDecl *D)
static void emitUsed(CodeGenModule &CGM, StringRef Name, std::vector< llvm::WeakTrackingVH > &List)
static bool HasNonDllImportDtor(QualType T)
static llvm::Constant * GetPointerConstant(llvm::LLVMContext &Context, const void *Ptr)
Turns the given pointer into a constant.
static llvm::GlobalVariable::ThreadLocalMode GetLLVMTLSModel(StringRef S)
static llvm::GlobalValue::LinkageTypes getMultiversionLinkage(CodeGenModule &CGM, GlobalDecl GD)
static void setVisibilityFromDLLStorageClass(const clang::LangOptions &LO, llvm::Module &M)
static std::string getCPUSpecificMangling(const CodeGenModule &CGM, StringRef Name)
static void setWindowsItaniumDLLImport(CodeGenModule &CGM, bool Local, llvm::Function *F, StringRef Name)
static const char AnnotationSection[]
static bool isUniqueInternalLinkageDecl(GlobalDecl GD, CodeGenModule &CGM)
static QualType GeneralizeFunctionType(ASTContext &Ctx, QualType Ty)
static bool allowKCFIIdentifier(StringRef Name)
static void replaceUsesOfNonProtoConstant(llvm::Constant *old, llvm::Function *newFn)
Replace the uses of a function that was declared with a non-proto type.
static llvm::Constant * castStringLiteralToDefaultAddressSpace(CodeGenModule &CGM, llvm::GlobalVariable *GV)
static void checkDataLayoutConsistency(const TargetInfo &Target, llvm::LLVMContext &Context, const LangOptions &Opts)
static bool needsDestructMethod(ObjCImplementationDecl *impl)
static bool isStackProtectorOn(const LangOptions &LangOpts, const llvm::Triple &Triple, clang::LangOptions::StackProtectorMode Mode)
static void removeImageAccessQualifier(std::string &TyName)
static llvm::StringMapEntry< llvm::GlobalVariable * > & GetConstantCFStringEntry(llvm::StringMap< llvm::GlobalVariable * > &Map, const StringLiteral *Literal, bool TargetIsLSB, bool &IsUTF16, unsigned &StringLength)
static void setLLVMVisibility(llvm::GlobalValue &GV, std::optional< llvm::GlobalValue::VisibilityTypes > V)
static llvm::GlobalVariable * GenerateStringLiteral(llvm::Constant *C, llvm::GlobalValue::LinkageTypes LT, CodeGenModule &CGM, StringRef GlobalName, CharUnits Alignment)
static bool hasUnwindExceptions(const LangOptions &LangOpts)
Determines whether the language options require us to model unwind exceptions.
static llvm::APInt getFMVPriority(const TargetInfo &TI, const CodeGenFunction::FMVResolverOption &RO)
static void addLinkOptionsPostorder(CodeGenModule &CGM, Module *Mod, SmallVectorImpl< llvm::MDNode * > &Metadata, llvm::SmallPtrSet< Module *, 16 > &Visited)
Add link options implied by the given module, including modules it depends on, using a postorder walk...
static llvm::cl::opt< bool > LimitedCoverage("limited-coverage-experimental", llvm::cl::Hidden, llvm::cl::desc("Emit limited coverage mapping information (experimental)"))
static CGCXXABI * createCXXABI(CodeGenModule &CGM)
static std::unique_ptr< TargetCodeGenInfo > createTargetCodeGenInfo(CodeGenModule &CGM)
static const llvm::GlobalValue * getAliasedGlobal(const llvm::GlobalValue *GV)
static unsigned ArgInfoAddressSpace(LangAS AS)
static void replaceDeclarationWith(llvm::GlobalValue *Old, llvm::Constant *New)
static QualType GeneralizeType(ASTContext &Ctx, QualType Ty)
static void ReplaceUsesOfNonProtoTypeWithRealFunction(llvm::GlobalValue *Old, llvm::Function *NewFn)
ReplaceUsesOfNonProtoTypeWithRealFunction - This function is called when we implement a function with...
static std::optional< llvm::GlobalValue::VisibilityTypes > getLLVMVisibility(clang::LangOptions::VisibilityFromDLLStorageClassKinds K)
static bool requiresMemberFunctionPointerTypeMetadata(CodeGenModule &CGM, const CXXMethodDecl *MD)
static bool checkAliasedGlobal(const ASTContext &Context, DiagnosticsEngine &Diags, SourceLocation Location, bool IsIFunc, const llvm::GlobalValue *Alias, const llvm::GlobalValue *&GV, const llvm::MapVector< GlobalDecl, StringRef > &MangledDeclNames, SourceRange AliasRange)
static void EmitGlobalDeclMetadata(CodeGenModule &CGM, llvm::NamedMDNode *&GlobalMetadata, GlobalDecl D, llvm::GlobalValue *Addr)
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate....
Defines the C++ template declaration subclasses.
int Priority
Definition: Format.cpp:3181
int Category
Definition: Format.cpp:3180
llvm::DenseSet< const void * > Visited
Definition: HTMLLogger.cpp:145
#define X(type, name)
Definition: Value.h:145
llvm::MachO::Target Target
Definition: MachO.h:51
llvm::MachO::Record Record
Definition: MachO.h:31
Defines the clang::Module class, which describes a module in the source code.
#define SM(sm)
Definition: OffloadArch.cpp:16
uint32_t Id
Definition: SemaARM.cpp:1179
static const NamedDecl * getDefinition(const Decl *D)
Definition: SemaDecl.cpp:2958
SourceRange Range
Definition: SemaObjC.cpp:753
SourceLocation Loc
Definition: SemaObjC.cpp:754
Defines the SourceManager interface.
static CharUnits getTypeAllocSize(CodeGenModule &CGM, llvm::Type *type)
Defines version macros and version-related utility functions for Clang.
__device__ __2f16 float __ockl_bool s
APValue - This class implements a discriminated union of [uninitialized] [APSInt] [APFloat],...
Definition: APValue.h:122
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:188
SourceManager & getSourceManager()
Definition: ASTContext.h:801
TranslationUnitDecl * getTranslationUnitDecl() const
Definition: ASTContext.h:1201
const ConstantArrayType * getAsConstantArrayType(QualType T) const
Definition: ASTContext.h:3056
CharUnits getTypeAlignInChars(QualType T) const
Return the ABI-specified alignment of a (complete) type T, in characters.
@ WeakUnknown
Weak for now, might become strong later in this TU.
const ProfileList & getProfileList() const
Definition: ASTContext.h:913
llvm::StringMap< SectionInfo > SectionInfos
Definition: ASTContext.h:3711
void getObjCEncodingForType(QualType T, std::string &S, const FieldDecl *Field=nullptr, QualType *NotEncodedT=nullptr) const
Emit the Objective-CC type encoding for the given type T into S.
QualType getFunctionNoProtoType(QualType ResultTy, const FunctionType::ExtInfo &Info) const
Return a K&R style C function type like 'int()'.
CanQualType getCanonicalType(QualType T) const
Return the canonical (structural) type corresponding to the specified potentially non-canonical type ...
Definition: ASTContext.h:2851
bool shouldExternalize(const Decl *D) const
Whether a C++ static variable or CUDA/HIP kernel should be externalized.
FullSourceLoc getFullLoc(SourceLocation Loc) const
Definition: ASTContext.h:917
const XRayFunctionFilter & getXRayFilter() const
Definition: ASTContext.h:909
ArrayRef< Decl * > getModuleInitializers(Module *M)
Get the initializations to perform when importing a module, if any.
bool DeclMustBeEmitted(const Decl *D)
Determines if the decl can be CodeGen'ed or deserialized from PCH lazily, only when used; this is onl...
RecordDecl * buildImplicitRecord(StringRef Name, RecordDecl::TagKind TK=RecordDecl::TagKind::Struct) const
Create a new implicit TU-level CXXRecordDecl or RecordDecl declaration.
QualType getPointerType(QualType T) const
Return the uniqued reference to the type for a pointer to the specified type.
StringRef getCUIDHash() const
bool isMSStaticDataMemberInlineDefinition(const VarDecl *VD) const
Returns true if this is an inline-initialized static data member which is treated as a definition for...
IdentifierTable & Idents
Definition: ASTContext.h:740
Builtin::Context & BuiltinInfo
Definition: ASTContext.h:742
QualType getConstantArrayType(QualType EltTy, const llvm::APInt &ArySize, const Expr *SizeExpr, ArraySizeModifier ASM, unsigned IndexTypeQuals) const
Return the unique reference to the type for a constant array of the specified element type.
const LangOptions & getLangOpts() const
Definition: ASTContext.h:894
SelectorTable & Selectors
Definition: ASTContext.h:741
void forEachMultiversionedFunctionVersion(const FunctionDecl *FD, llvm::function_ref< void(FunctionDecl *)> Pred) const
Visits all versions of a multiversioned function with the passed predicate.
QualType getBaseElementType(const ArrayType *VAT) const
Return the innermost element type of an array type.
llvm::SetVector< const ValueDecl * > CUDAExternalDeviceDeclODRUsedByHost
Keep track of CUDA/HIP external kernels or device variables ODR-used by host code.
Definition: ASTContext.h:1306
QualType getCFConstantStringType() const
Return the C structure type used to represent constant CFStrings.
const NoSanitizeList & getNoSanitizeList() const
Definition: ASTContext.h:904
GVALinkage GetGVALinkageForFunction(const FunctionDecl *FD) const
const TargetInfo * getAuxTargetInfo() const
Definition: ASTContext.h:860
CanQualType UnsignedLongTy
Definition: ASTContext.h:1232
CanQualType CharTy
Definition: ASTContext.h:1224
bool isAlignmentRequired(const Type *T) const
Determine if the alignment the type has was required using an alignment attribute.
CharUnits getDeclAlign(const Decl *D, bool ForAlignof=false) const
Return a conservative estimate of the alignment of the specified decl D.
int64_t toBits(CharUnits CharSize) const
Convert a size in characters to a size in bits.
const clang::PrintingPolicy & getPrintingPolicy() const
Definition: ASTContext.h:793
CharUnits getAlignOfGlobalVarInChars(QualType T, const VarDecl *VD) const
Return the alignment in characters that should be given to a global variable with type T.
GVALinkage GetGVALinkageForVariable(const VarDecl *VD) const
QualType getObjCIdType() const
Represents the Objective-CC id type.
Definition: ASTContext.h:2333
CharUnits getTypeSizeInChars(QualType T) const
Return the size of the specified (complete) type T, in characters.
CanQualType VoidTy
Definition: ASTContext.h:1222
unsigned getTypeAlignIfKnown(QualType T, bool NeedsPreferredAlignment=false) const
Return the alignment of a type, in bits, or 0 if the type is incomplete and we cannot determine the a...
QualType getFunctionType(QualType ResultTy, ArrayRef< QualType > Args, const FunctionProtoType::ExtProtoInfo &EPI) const
Return a normal function type with a typed argument list.
Definition: ASTContext.h:1750
QualType getMemberPointerType(QualType T, NestedNameSpecifier Qualifier, const CXXRecordDecl *Cls) const
Return the uniqued reference to the type for a member pointer to the specified type in the specified ...
CanQualType ShortTy
Definition: ASTContext.h:1231
DiagnosticsEngine & getDiagnostics() const
const TargetInfo & getTargetInfo() const
Definition: ASTContext.h:859
CharUnits toCharUnitsFromBits(int64_t BitSize) const
Convert a size in bits to a size in characters.
void getFunctionFeatureMap(llvm::StringMap< bool > &FeatureMap, const FunctionDecl *) const
TargetCXXABI::Kind getCXXABIKind() const
Return the C++ ABI kind that should be used.
Definition: ASTContext.cpp:884
ExternalASTSource * getExternalSource() const
Retrieve a pointer to the external AST source associated with this AST context, if any.
Definition: ASTContext.h:1339
CanQualType getCanonicalTagType(const TagDecl *TD) const
unsigned getTargetAddressSpace(LangAS AS) const
QualType getWideCharType() const
Return the type of wide characters.
Definition: ASTContext.h:2060
InlineVariableDefinitionKind getInlineVariableDefinitionKind(const VarDecl *VD) const
Determine whether a definition of this inline variable should be treated as a weak or strong definiti...
Module * getCurrentNamedModule() const
Get module under construction, nullptr if this is not a C++20 module.
Definition: ASTContext.h:1193
uint64_t getCharWidth() const
Return the size of the character type, in bits.
Definition: ASTContext.h:2629
Attr - This represents one attribute.
Definition: Attr.h:44
Represents a block literal declaration, which is like an unnamed FunctionDecl.
Definition: Decl.h:4634
Holds information about both target-independent and target-specific builtins, allowing easy queries b...
Definition: Builtins.h:228
bool isLibFunction(unsigned ID) const
Return true if this is a builtin for a libc/libm function, with a "__builtin_" prefix (e....
Definition: Builtins.h:302
std::string getName(unsigned ID) const
Return the identifier name for the specified builtin, e.g.
Definition: Builtins.cpp:80
Represents a base class of a C++ class.
Definition: DeclCXX.h:146
Represents binding an expression to a temporary.
Definition: ExprCXX.h:1494
Represents a call to a C++ constructor.
Definition: ExprCXX.h:1549
Represents a C++ base or member initializer.
Definition: DeclCXX.h:2369
Expr * getInit() const
Get the initializer.
Definition: DeclCXX.h:2571
Represents a delete expression for memory deallocation and destructor calls, e.g.
Definition: ExprCXX.h:2620
Represents a C++ destructor within a class.
Definition: DeclCXX.h:2869
Represents a call to a member function that may be written either with member call syntax (e....
Definition: ExprCXX.h:179
Represents a static or instance method of a struct/union/class.
Definition: DeclCXX.h:2129
bool isImplicitObjectMemberFunction() const
[C++2b][dcl.fct]/p7 An implicit object member function is a non-static member function without an exp...
Definition: DeclCXX.cpp:2710
bool isVirtual() const
Definition: DeclCXX.h:2184
const CXXRecordDecl * getParent() const
Return the parent of this method declaration, which is the class in which this method is defined.
Definition: DeclCXX.h:2255
Represents a new-expression for memory allocation and constructor calls, e.g: "new CXXNewExpr(foo)".
Definition: ExprCXX.h:2349
Represents a C++ struct/union/class.
Definition: DeclCXX.h:258
bool hasMutableFields() const
Determine whether this class, or any of its class subobjects, contains a mutable field.
Definition: DeclCXX.h:1233
base_class_range bases()
Definition: DeclCXX.h:608
unsigned getNumBases() const
Retrieves the number of base classes of this class.
Definition: DeclCXX.h:602
bool hasDefinition() const
Definition: DeclCXX.h:561
CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).
Definition: Expr.h:2879
CanProxy< U > castAs() const
CharUnits - This is an opaque type for sizes expressed in character units.
Definition: CharUnits.h:38
llvm::Align getAsAlign() const
getAsAlign - Returns Quantity as a valid llvm::Align, Beware llvm::Align assumes power of two 8-bit b...
Definition: CharUnits.h:189
QuantityType getQuantity() const
getQuantity - Get the raw integer representation of this quantity.
Definition: CharUnits.h:185
static CharUnits One()
One - Construct a CharUnits quantity of one.
Definition: CharUnits.h:58
static CharUnits fromQuantity(QuantityType Quantity)
fromQuantity - Construct a CharUnits quantity from a raw integer type.
Definition: CharUnits.h:63
CodeGenOptions - Track various options which control how the code is optimized and passed to the back...
std::string UniqueSourceFileIdentifier
If non-empty, allow the compiler to assume that the given source file identifier is unique at link ti...
std::string MSSecureHotPatchFunctionsFile
The name of a file that contains functions which will be compiled for hotpatching.
std::string RecordCommandLine
The string containing the commandline for the llvm.commandline metadata, if non-empty.
std::string FloatABI
The ABI to use for passing floating point arguments.
llvm::Reloc::Model RelocationModel
The name of the relocation model to use.
std::string CoverageNotesFile
The filename with path we use for coverage notes files.
std::string ProfileInstrumentUsePath
Name of the profile file to use as input for -fprofile-instr-use.
std::string MemoryProfileOutput
Name of the profile file to use as output for with -fmemory-profile.
std::vector< std::string > TocDataVarsUserSpecified
List of global variables explicitly specified by the user as toc-data.
std::string CoverageDataFile
The filename with path we use for coverage data files.
PointerAuthOptions PointerAuth
Configuration for pointer-signing.
llvm::DenormalMode FP32DenormalMode
The floating-point denormal mode to use, for float.
SanitizerSet SanitizeTrap
Set of sanitizer checks that trap rather than diagnose.
std::string ProfileRemappingFile
Name of the profile remapping file to apply to the profile data supplied by -fprofile-sample-use or -...
std::vector< std::string > MSSecureHotPatchFunctionsList
A list of functions which will be compiled for hotpatching.
bool hasProfileClangUse() const
Check if Clang profile use is on.
std::string SymbolPartition
The name of the partition that symbols are assigned to, specified with -fsymbol-partition (see https:...
ABIInfo - Target specific hooks for defining how a type should be passed or returned from functions.
Definition: ABIInfo.h:48
virtual void appendAttributeMangling(TargetAttr *Attr, raw_ostream &Out) const
Definition: ABIInfo.cpp:186
Like RawAddress, an abstract representation of an aligned address, but the pointer contained in this ...
Definition: Address.h:128
virtual void handleVarRegistration(const VarDecl *VD, llvm::GlobalVariable &Var)=0
Check whether a variable is a device variable and register it if true.
virtual llvm::GlobalValue * getKernelHandle(llvm::Function *Stub, GlobalDecl GD)=0
Get kernel handle by stub function.
virtual void internalizeDeviceSideVar(const VarDecl *D, llvm::GlobalValue::LinkageTypes &Linkage)=0
Adjust linkage of shadow variables in host compilation.
Implements C++ ABI-specific code generation functions.
Definition: CGCXXABI.h:43
virtual void EmitCXXConstructors(const CXXConstructorDecl *D)=0
Emit constructor variants required by this ABI.
virtual llvm::Constant * getAddrOfRTTIDescriptor(QualType Ty)=0
virtual void EmitCXXDestructors(const CXXDestructorDecl *D)=0
Emit destructor variants required by this ABI.
virtual void setCXXDestructorDLLStorage(llvm::GlobalValue *GV, const CXXDestructorDecl *Dtor, CXXDtorType DT) const
Definition: CGCXXABI.cpp:309
virtual llvm::GlobalValue::LinkageTypes getCXXDestructorLinkage(GVALinkage Linkage, const CXXDestructorDecl *Dtor, CXXDtorType DT) const
Definition: CGCXXABI.cpp:316
MangleContext & getMangleContext()
Gets the mangle context.
Definition: CGCXXABI.h:113
This class gathers all debug information during compilation and is responsible for emitting to llvm g...
Definition: CGDebugInfo.h:59
void EmitExternalVariable(llvm::GlobalVariable *GV, const VarDecl *Decl)
Emit information about an external variable.
void EmitFunctionDecl(GlobalDecl GD, SourceLocation Loc, QualType FnType, llvm::Function *Fn=nullptr)
Emit debug info for a function declaration.
void AddStringLiteralDebugInfo(llvm::GlobalVariable *GV, const StringLiteral *S)
DebugInfo isn't attached to string literals by default.
CGFunctionInfo - Class to encapsulate the information about a function definition.
void handleGlobalVarDefinition(const VarDecl *VD, llvm::GlobalVariable *Var)
void addBuffer(const HLSLBufferDecl *D)
llvm::Type * getSamplerType(const Type *T)
void emitDeferredTargetDecls() const
Emit deferred declare target variables marked for deferred emission.
virtual void emitDeclareTargetFunction(const FunctionDecl *FD, llvm::GlobalValue *GV)
Emit code for handling declare target functions in the runtime.
virtual ConstantAddress getAddrOfDeclareTargetVar(const VarDecl *VD)
Returns the address of the variable marked as declare target with link clause OR as declare target wi...
bool hasRequiresUnifiedSharedMemory() const
Return whether the unified_shared_memory has been specified.
virtual void emitDeclareSimdFunction(const FunctionDecl *FD, llvm::Function *Fn)
Marks function Fn with properly mangled versions of vector functions.
virtual void registerTargetGlobalVariable(const VarDecl *VD, llvm::Constant *Addr)
Checks if the provided global decl GD is a declare target variable and registers it when emitting cod...
Class supports emissionof SIMD-only code.
CodeGenFunction - This class organizes the per-function state that is used while generating LLVM code...
void GenerateCode(GlobalDecl GD, llvm::Function *Fn, const CGFunctionInfo &FnInfo)
void EmitCfiCheckFail()
Emit a cross-DSO CFI failure handling function.
Definition: CGExpr.cpp:4020
void GenerateObjCCtorDtorMethod(ObjCImplementationDecl *IMP, ObjCMethodDecl *MD, bool ctor)
Definition: CGObjC.cpp:1752
void GenerateObjCGetter(ObjCImplementationDecl *IMP, const ObjCPropertyImplDecl *PID)
GenerateObjCGetter - Synthesize an Objective-C property getter function.
Definition: CGObjC.cpp:1049
void EmitCfiCheckStub()
Emit a stub for the cross-DSO CFI check function.
Definition: CGExpr.cpp:3981
void GenerateObjCMethod(const ObjCMethodDecl *OMD)
Generate an Objective-C method.
Definition: CGObjC.cpp:807
llvm::CallInst * EmitRuntimeCall(llvm::FunctionCallee callee, const Twine &name="")
void GenerateObjCSetter(ObjCImplementationDecl *IMP, const ObjCPropertyImplDecl *PID)
GenerateObjCSetter - Synthesize an Objective-C property setter function for the given property.
Definition: CGObjC.cpp:1672
llvm::LLVMContext & getLLVMContext()
bool isTrivialInitializer(const Expr *Init)
Determine whether the given initializer is trivial in the sense that it requires no code to be genera...
Definition: CGDecl.cpp:1808
This class organizes the cross-function state that is used while generating LLVM code.
StringRef getBlockMangledName(GlobalDecl GD, const BlockDecl *BD)
ConstantAddress GetAddrOfMSGuidDecl(const MSGuidDecl *GD)
Get the address of a GUID.
void setGVProperties(llvm::GlobalValue *GV, GlobalDecl GD) const
Set visibility, dllimport/dllexport and dso_local.
void AddVTableTypeMetadata(llvm::GlobalVariable *VTable, CharUnits Offset, const CXXRecordDecl *RD)
Create and attach type metadata for the given vtable.
void UpdateCompletedType(const TagDecl *TD)
llvm::MDNode * getTBAAAccessTagInfo(TBAAAccessInfo Info)
getTBAAAccessTagInfo - Get TBAA tag for a given memory access.
llvm::GlobalVariable::ThreadLocalMode GetDefaultLLVMTLSModel() const
Get LLVM TLS mode from CodeGenOptions.
void SetInternalFunctionAttributes(GlobalDecl GD, llvm::Function *F, const CGFunctionInfo &FI)
Set the attributes on the LLVM function for the given decl and function info.
void setDSOLocal(llvm::GlobalValue *GV) const
llvm::MDNode * getTBAAStructInfo(QualType QTy)
CGHLSLRuntime & getHLSLRuntime()
Return a reference to the configured HLSL runtime.
llvm::Constant * EmitAnnotationArgs(const AnnotateAttr *Attr)
Emit additional args of the annotation.
llvm::Module & getModule() const
llvm::FunctionCallee CreateRuntimeFunction(llvm::FunctionType *Ty, StringRef Name, llvm::AttributeList ExtraAttrs=llvm::AttributeList(), bool Local=false, bool AssumeConvergent=false)
Create or return a runtime function declaration with the specified type and name.
llvm::ConstantInt * CreateKCFITypeId(QualType T, StringRef Salt)
Generate a KCFI type identifier for T.
CGDebugInfo * getModuleDebugInfo()
bool NeedAllVtablesTypeId() const
Returns whether this module needs the "all-vtables" type identifier.
void addCompilerUsedGlobal(llvm::GlobalValue *GV)
Add a global to a list to be added to the llvm.compiler.used metadata.
CodeGenVTables & getVTables()
llvm::ConstantInt * CreateCrossDsoCfiTypeId(llvm::Metadata *MD)
Generate a cross-DSO type identifier for MD.
CharUnits GetTargetTypeStoreSize(llvm::Type *Ty) const
Return the store size, in character units, of the given LLVM type.
void createFunctionTypeMetadataForIcall(const FunctionDecl *FD, llvm::Function *F)
Create and attach type metadata to the given function.
bool getExpressionLocationsEnabled() const
Return true if we should emit location information for expressions.
void addGlobalValReplacement(llvm::GlobalValue *GV, llvm::Constant *C)
llvm::Constant * GetAddrOfRTTIDescriptor(QualType Ty, bool ForEH=false)
Get the address of the RTTI descriptor for the given type.
llvm::Constant * GetAddrOfFunction(GlobalDecl GD, llvm::Type *Ty=nullptr, bool ForVTable=false, bool DontDefer=false, ForDefinition_t IsForDefinition=NotForDefinition)
Return the address of the given function.
void setGVPropertiesAux(llvm::GlobalValue *GV, const NamedDecl *D) const
void EmitMainVoidAlias()
Emit an alias for "main" if it has no arguments (needed for wasm).
void DecorateInstructionWithInvariantGroup(llvm::Instruction *I, const CXXRecordDecl *RD)
Adds !invariant.barrier !tag to instruction.
DiagnosticsEngine & getDiags() const
bool isInNoSanitizeList(SanitizerMask Kind, llvm::Function *Fn, SourceLocation Loc) const
void runWithSufficientStackSpace(SourceLocation Loc, llvm::function_ref< void()> Fn)
Run some code with "sufficient" stack space.
llvm::Constant * getAddrOfCXXStructor(GlobalDecl GD, const CGFunctionInfo *FnInfo=nullptr, llvm::FunctionType *FnType=nullptr, bool DontDefer=false, ForDefinition_t IsForDefinition=NotForDefinition)
Return the address of the constructor/destructor of the given type.
void ErrorUnsupported(const Stmt *S, const char *Type)
Print out an error that codegen doesn't support the specified stmt yet.
llvm::Constant * EmitAnnotateAttr(llvm::GlobalValue *GV, const AnnotateAttr *AA, SourceLocation L)
Generate the llvm::ConstantStruct which contains the annotation information for a given GlobalValue.
void EmitOpenACCDeclare(const OpenACCDeclareDecl *D, CodeGenFunction *CGF=nullptr)
Definition: CGDecl.cpp:2880
llvm::GlobalValue::LinkageTypes getLLVMLinkageForDeclarator(const DeclaratorDecl *D, GVALinkage Linkage)
Returns LLVM linkage for a declarator.
TBAAAccessInfo mergeTBAAInfoForMemoryTransfer(TBAAAccessInfo DestInfo, TBAAAccessInfo SrcInfo)
mergeTBAAInfoForMemoryTransfer - Get merged TBAA information for the purposes of memory transfer call...
const LangOptions & getLangOpts() const
CGCUDARuntime & getCUDARuntime()
Return a reference to the configured CUDA runtime.
llvm::Constant * EmitAnnotationLineNo(SourceLocation L)
Emit the annotation line number.
QualType getObjCFastEnumerationStateType()
Retrieve the record type that describes the state of an Objective-C fast enumeration loop (for....
CharUnits getNaturalTypeAlignment(QualType T, LValueBaseInfo *BaseInfo=nullptr, TBAAAccessInfo *TBAAInfo=nullptr, bool forPointeeType=false)
bool shouldMapVisibilityToDLLExport(const NamedDecl *D) const
CGOpenCLRuntime & getOpenCLRuntime()
Return a reference to the configured OpenCL runtime.
const std::string & getModuleNameHash() const
const TargetInfo & getTarget() const
bool shouldEmitRTTI(bool ForEH=false)
void EmitGlobal(GlobalDecl D)
Emit code for a single global function or var decl.
llvm::Metadata * CreateMetadataIdentifierForType(QualType T)
Create a metadata identifier for the given type.
void addUsedGlobal(llvm::GlobalValue *GV)
Add a global to a list to be added to the llvm.used metadata.
void AppendLinkerOptions(StringRef Opts)
Appends Opts to the "llvm.linker.options" metadata value.
void EmitExternalDeclaration(const DeclaratorDecl *D)
void AddDependentLib(StringRef Lib)
Appends a dependent lib to the appropriate metadata value.
void Release()
Finalize LLVM code generation.
ProfileList::ExclusionType isFunctionBlockedByProfileList(llvm::Function *Fn, SourceLocation Loc) const
llvm::MDNode * getTBAABaseTypeInfo(QualType QTy)
getTBAABaseTypeInfo - Get metadata that describes the given base access type.
bool lookupRepresentativeDecl(StringRef MangledName, GlobalDecl &Result) const
void EmitOMPAllocateDecl(const OMPAllocateDecl *D)
Emit a code for the allocate directive.
Definition: CGDecl.cpp:2894
void setGlobalVisibility(llvm::GlobalValue *GV, const NamedDecl *D) const
Set the visibility for the given LLVM GlobalValue.
llvm::GlobalValue::LinkageTypes getLLVMLinkageVarDefinition(const VarDecl *VD)
Returns LLVM linkage for a declarator.
bool HasHiddenLTOVisibility(const CXXRecordDecl *RD)
Returns whether the given record has hidden LTO visibility and therefore may participate in (single-m...
Definition: CGVTables.cpp:1312
const llvm::DataLayout & getDataLayout() const
void Error(SourceLocation loc, StringRef error)
Emit a general error that something can't be done.
TBAAAccessInfo getTBAAVTablePtrAccessInfo(llvm::Type *VTablePtrType)
getTBAAVTablePtrAccessInfo - Get the TBAA information that describes an access to a virtual table poi...
CGCXXABI & getCXXABI() const
ConstantAddress GetWeakRefReference(const ValueDecl *VD)
Get a reference to the target of VD.
llvm::Constant * GetFunctionStart(const ValueDecl *Decl)
static llvm::GlobalValue::VisibilityTypes GetLLVMVisibility(Visibility V)
void EmitTentativeDefinition(const VarDecl *D)
void EmitDeferredUnusedCoverageMappings()
Emit all the deferred coverage mappings for the uninstrumented functions.
void addUsedOrCompilerUsedGlobal(llvm::GlobalValue *GV)
Add a global to a list to be added to the llvm.compiler.used metadata.
CGOpenMPRuntime & getOpenMPRuntime()
Return a reference to the configured OpenMP runtime.
bool imbueXRayAttrs(llvm::Function *Fn, SourceLocation Loc, StringRef Category=StringRef()) const
Imbue XRay attributes to a function, applying the always/never attribute lists in the process.
SanitizerMetadata * getSanitizerMetadata()
llvm::Metadata * CreateMetadataIdentifierGeneralized(QualType T)
Create a metadata identifier for the generalization of the given type.
void EmitGlobalAnnotations()
Emit all the global annotations.
CharUnits getClassPointerAlignment(const CXXRecordDecl *CD)
Returns the assumed alignment of an opaque pointer to the given class.
Definition: CGClass.cpp:40
const llvm::Triple & getTriple() const
SmallVector< const CXXRecordDecl *, 0 > getMostBaseClasses(const CXXRecordDecl *RD)
Return a vector of most-base classes for RD.
void AddDeferredUnusedCoverageMapping(Decl *D)
Stored a deferred empty coverage mapping for an unused and thus uninstrumented top level declaration.
void MaybeHandleStaticInExternC(const SomeDecl *D, llvm::GlobalValue *GV)
If the declaration has internal linkage but is inside an extern "C" linkage specification,...
void DecorateInstructionWithTBAA(llvm::Instruction *Inst, TBAAAccessInfo TBAAInfo)
DecorateInstructionWithTBAA - Decorate the instruction with a TBAA tag.
llvm::GlobalVariable::LinkageTypes getFunctionLinkage(GlobalDecl GD)
void AddGlobalDtor(llvm::Function *Dtor, int Priority=65535, bool IsDtorAttrFunc=false)
AddGlobalDtor - Add a function to the list that will be called when the module is unloaded.
llvm::Constant * CreateRuntimeVariable(llvm::Type *Ty, StringRef Name)
Create a new runtime global variable with the specified type and name.
void ConstructAttributeList(StringRef Name, const CGFunctionInfo &Info, CGCalleeInfo CalleeInfo, llvm::AttributeList &Attrs, unsigned &CallingConv, bool AttrOnCallSite, bool IsThunk)
Get the LLVM attributes and calling convention to use for a particular function type.
Definition: CGCall.cpp:2410
llvm::Constant * GetOrCreateLLVMGlobal(StringRef MangledName, llvm::Type *Ty, LangAS AddrSpace, const VarDecl *D, ForDefinition_t IsForDefinition=NotForDefinition)
GetOrCreateLLVMGlobal - If the specified mangled name is not in the module, create and return an llvm...
TBAAAccessInfo getTBAAAccessInfo(QualType AccessType)
getTBAAAccessInfo - Get TBAA information that describes an access to an object of the given type.
void setFunctionLinkage(GlobalDecl GD, llvm::Function *F)
llvm::Constant * GetAddrOfGlobal(GlobalDecl GD, ForDefinition_t IsForDefinition=NotForDefinition)
ConstantAddress GetAddrOfConstantCFString(const StringLiteral *Literal)
Return a pointer to a constant CFString object for the given string.
ProfileList::ExclusionType isFunctionBlockedFromProfileInstr(llvm::Function *Fn, SourceLocation Loc) const
void AddGlobalAnnotations(const ValueDecl *D, llvm::GlobalValue *GV)
Add global annotations that are set on D, for the global GV.
void setTLSMode(llvm::GlobalValue *GV, const VarDecl &D) const
Set the TLS mode for the given LLVM GlobalValue for the thread-local variable declaration D.
ConstantAddress GetAddrOfConstantStringFromLiteral(const StringLiteral *S, StringRef Name=".str")
Return a pointer to a constant array for the given string literal.
ASTContext & getContext() const
ConstantAddress GetAddrOfTemplateParamObject(const TemplateParamObjectDecl *TPO)
Get the address of a template parameter object.
void EmitOMPThreadPrivateDecl(const OMPThreadPrivateDecl *D)
Emit a code for threadprivate directive.
ConstantAddress GetAddrOfUnnamedGlobalConstantDecl(const UnnamedGlobalConstantDecl *GCD)
Get the address of a UnnamedGlobalConstant.
TBAAAccessInfo mergeTBAAInfoForCast(TBAAAccessInfo SourceInfo, TBAAAccessInfo TargetInfo)
mergeTBAAInfoForCast - Get merged TBAA information for the purposes of type casts.
llvm::Constant * GetAddrOfGlobalVar(const VarDecl *D, llvm::Type *Ty=nullptr, ForDefinition_t IsForDefinition=NotForDefinition)
Return the llvm::Constant for the address of the given global variable.
llvm::SanitizerStatReport & getSanStats()
llvm::Constant * EmitAnnotationString(StringRef Str)
Emit an annotation string.
void EmitOMPDeclareMapper(const OMPDeclareMapperDecl *D, CodeGenFunction *CGF=nullptr)
Emit a code for declare mapper construct.
Definition: CGDecl.cpp:2872
void RefreshTypeCacheForClass(const CXXRecordDecl *Class)
llvm::MDNode * getTBAATypeInfo(QualType QTy)
getTBAATypeInfo - Get metadata used to describe accesses to objects of the given type.
void EmitOMPRequiresDecl(const OMPRequiresDecl *D)
Emit a code for requires directive.
Definition: CGDecl.cpp:2890
void HandleCXXStaticMemberVarInstantiation(VarDecl *VD)
Tell the consumer that this variable has been instantiated.
const TargetCodeGenInfo & getTargetCodeGenInfo()
const CodeGenOptions & getCodeGenOpts() const
StringRef getMangledName(GlobalDecl GD)
llvm::Constant * GetConstantArrayFromStringLiteral(const StringLiteral *E)
Return a constant array for the given string.
void SetCommonAttributes(GlobalDecl GD, llvm::GlobalValue *GV)
Set attributes which are common to any form of a global definition (alias, Objective-C method,...
std::optional< CharUnits > getOMPAllocateAlignment(const VarDecl *VD)
Return the alignment specified in an allocate directive, if present.
Definition: CGDecl.cpp:2945
llvm::GlobalVariable * CreateOrReplaceCXXRuntimeVariable(StringRef Name, llvm::Type *Ty, llvm::GlobalValue::LinkageTypes Linkage, llvm::Align Alignment)
Will return a global variable of the given type.
CharUnits getNaturalPointeeTypeAlignment(QualType T, LValueBaseInfo *BaseInfo=nullptr, TBAAAccessInfo *TBAAInfo=nullptr)
TBAAAccessInfo mergeTBAAInfoForConditionalOperator(TBAAAccessInfo InfoA, TBAAAccessInfo InfoB)
mergeTBAAInfoForConditionalOperator - Get merged TBAA information for the purposes of conditional ope...
llvm::LLVMContext & getLLVMContext()
llvm::GlobalValue * GetGlobalValue(StringRef Ref)
void GenKernelArgMetadata(llvm::Function *FN, const FunctionDecl *FD=nullptr, CodeGenFunction *CGF=nullptr)
OpenCL v1.2 s5.6.4.6 allows the compiler to store kernel argument information in the program executab...
void setKCFIType(const FunctionDecl *FD, llvm::Function *F)
Set type metadata to the given function.
void maybeSetTrivialComdat(const Decl &D, llvm::GlobalObject &GO)
void EmitOMPDeclareReduction(const OMPDeclareReductionDecl *D, CodeGenFunction *CGF=nullptr)
Emit a code for declare reduction construct.
Definition: CGDecl.cpp:2865
llvm::Function * getIntrinsic(unsigned IID, ArrayRef< llvm::Type * > Tys={})
void AddDetectMismatch(StringRef Name, StringRef Value)
Appends a detect mismatch command to the linker options.
void setDLLImportDLLExport(llvm::GlobalValue *GV, GlobalDecl D) const
llvm::Value * createOpenCLIntToSamplerConversion(const Expr *E, CodeGenFunction &CGF)
ConstantAddress GetAddrOfGlobalTemporary(const MaterializeTemporaryExpr *E, const Expr *Inner)
Returns a pointer to a global variable representing a temporary with static or thread storage duratio...
llvm::Constant * EmitNullConstant(QualType T)
Return the result of value-initializing the given type, i.e.
LangAS GetGlobalConstantAddressSpace() const
Return the AST address space of constant literal, which is used to emit the constant literal as globa...
LangAS GetGlobalVarAddressSpace(const VarDecl *D)
Return the AST address space of the underlying global variable for D, as determined by its declaratio...
void SetLLVMFunctionAttributes(GlobalDecl GD, const CGFunctionInfo &Info, llvm::Function *F, bool IsThunk)
Set the LLVM function attributes (sext, zext, etc).
void EmitOpenACCRoutine(const OpenACCRoutineDecl *D, CodeGenFunction *CGF=nullptr)
Definition: CGDecl.cpp:2885
void addReplacement(StringRef Name, llvm::Constant *C)
llvm::Constant * getConstantSignedPointer(llvm::Constant *Pointer, const PointerAuthSchema &Schema, llvm::Constant *StorageAddress, GlobalDecl SchemaDecl, QualType SchemaType)
Sign a constant pointer using the given scheme, producing a constant with the same IR type.
void AddGlobalCtor(llvm::Function *Ctor, int Priority=65535, unsigned LexOrder=~0U, llvm::Constant *AssociatedData=nullptr)
AddGlobalCtor - Add a function to the list that will be called before main() runs.
void SetLLVMFunctionAttributesForDefinition(const Decl *D, llvm::Function *F)
Set the LLVM function attributes which only apply to a function definition.
llvm::Metadata * CreateMetadataIdentifierForVirtualMemPtrType(QualType T)
Create a metadata identifier that is intended to be used to check virtual calls via a member function...
ConstantAddress GetAddrOfConstantStringFromObjCEncode(const ObjCEncodeExpr *)
Return a pointer to a constant array for the given ObjCEncodeExpr node.
const GlobalDecl getMangledNameDecl(StringRef)
void ClearUnusedCoverageMapping(const Decl *D)
Remove the deferred empty coverage mapping as this declaration is actually instrumented.
void EmitTopLevelDecl(Decl *D)
Emit code for a single top level declaration.
llvm::Constant * EmitAnnotationUnit(SourceLocation Loc)
Emit the annotation's translation unit.
ConstantAddress GetAddrOfConstantCString(const std::string &Str, const char *GlobalName=nullptr)
Returns a pointer to a character array containing the literal and a terminating '\0' character.
void printPostfixForExternalizedDecl(llvm::raw_ostream &OS, const Decl *D) const
Print the postfix for externalized static variable or kernels for single source offloading languages ...
void moveLazyEmissionStates(CodeGenModule *NewBuilder)
Move some lazily-emitted states to the NewBuilder.
llvm::ConstantInt * getSize(CharUnits numChars)
Emit the given number of characters as a value of type size_t.
void finalizeKCFITypes()
Emit KCFI type identifier constants and remove unused identifiers.
Per-function PGO state.
Definition: CodeGenPGO.h:29
void setValueProfilingFlag(llvm::Module &M)
void setProfileVersion(llvm::Module &M)
void emitEmptyCounterMapping(const Decl *D, StringRef FuncName, llvm::GlobalValue::LinkageTypes Linkage)
Emit a coverage mapping range with a counter zero for an unused declaration.
CodeGenTBAA - This class organizes the cross-module state that is used while lowering AST types to LL...
Definition: CodeGenTBAA.h:117
This class organizes the cross-module state that is used while lowering AST types to LLVM types.
Definition: CodeGenTypes.h:54
llvm::Type * ConvertType(QualType T)
ConvertType - Convert type T into a llvm::Type.
const CGFunctionInfo & arrangeCXXMethodDeclaration(const CXXMethodDecl *MD)
C++ methods have some special rules and also have implicit parameters.
Definition: CGCall.cpp:373
const CGFunctionInfo & arrangeFreeFunctionType(CanQual< FunctionProtoType > Ty)
Arrange the argument and result information for a value of the given freestanding function type.
Definition: CGCall.cpp:249
llvm::FunctionType * GetFunctionType(const CGFunctionInfo &Info)
GetFunctionType - Get the LLVM function type for.
Definition: CGCall.cpp:1702
const CGFunctionInfo & arrangeBuiltinFunctionDeclaration(QualType resultType, const FunctionArgList &args)
A builtin function is a freestanding function using the default C conventions.
Definition: CGCall.cpp:739
unsigned getTargetAddressSpace(QualType T) const
void RefreshTypeCacheForClass(const CXXRecordDecl *RD)
Remove stale types from the type cache when an inheritance model gets assigned to a class.
llvm::Type * ConvertTypeForMem(QualType T)
ConvertTypeForMem - Convert type T into a llvm::Type.
void UpdateCompletedType(const TagDecl *TD)
UpdateCompletedType - When we find the full definition for a TagDecl, replace the 'opaque' type we pr...
const CGFunctionInfo & arrangeGlobalDeclaration(GlobalDecl GD)
Definition: CGCall.cpp:611
void GenerateClassData(const CXXRecordDecl *RD)
GenerateClassData - Generate all the class data required to be generated upon definition of a KeyFunc...
Definition: CGVTables.cpp:1198
void EmitThunks(GlobalDecl GD)
EmitThunks - Emit the associated thunks for the given global decl.
Definition: CGVTables.cpp:625
A specialization of Address that requires the address to be an LLVM Constant.
Definition: Address.h:296
static ConstantAddress invalid()
Definition: Address.h:304
llvm::Constant * tryEmitForInitializer(const VarDecl &D)
Try to emit the initiaizer of the given declaration as an abstract constant.
void finalize(llvm::GlobalVariable *global)
llvm::Constant * emitAbstract(const Expr *E, QualType T)
Emit the result of the given expression as an abstract constant, asserting that it succeeded.
The standard implementation of ConstantInitBuilder used in Clang.
Organizes the cross-function state that is used while generating code coverage mapping data.
FunctionArgList - Type for representing both the decl and type of parameters to a function.
Definition: CGCall.h:375
bool hasDiagnostics()
Whether or not the stats we've gathered indicate any potential problems.
void reportDiagnostics(DiagnosticsEngine &Diags, StringRef MainFile)
Report potential problems we've found to Diags.
void disableSanitizerForGlobal(llvm::GlobalVariable *GV)
TargetCodeGenInfo - This class organizes various target-specific codegeneration issues,...
Definition: TargetInfo.h:47
virtual void getDependentLibraryOption(llvm::StringRef Lib, llvm::SmallString< 24 > &Opt) const
Gets the linker options necessary to link a dependent library on this platform.
Definition: TargetInfo.cpp:104
Address performAddrSpaceCast(CodeGen::CodeGenFunction &CGF, Address Addr, LangAS SrcAddr, llvm::Type *DestTy, bool IsNonNull=false) const
const T & getABIInfo() const
Definition: TargetInfo.h:57
virtual LangAS getGlobalVarAddressSpace(CodeGenModule &CGM, const VarDecl *D) const
Get target favored AST address space of a global variable for languages other than OpenCL and CUDA.
Definition: TargetInfo.cpp:141
virtual void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const
setTargetAttributes - Provides a convenient hook to handle extra target-specific attributes for the g...
Definition: TargetInfo.h:80
virtual LangAS getASTAllocaAddressSpace() const
Get the AST address space for alloca.
Definition: TargetInfo.h:320
virtual void emitTargetMetadata(CodeGen::CodeGenModule &CGM, const llvm::MapVector< GlobalDecl, StringRef > &MangledDeclNames) const
emitTargetMetadata - Provides a convenient hook to handle extra target-specific metadata for the give...
Definition: TargetInfo.h:85
virtual void emitTargetGlobals(CodeGen::CodeGenModule &CGM) const
Provides a convenient hook to handle extra target-specific globals.
Definition: TargetInfo.h:90
virtual void getDetectMismatchOption(llvm::StringRef Name, llvm::StringRef Value, llvm::SmallString< 32 > &Opt) const
Gets the linker options necessary to detect object file mismatches on this platform.
Definition: TargetInfo.h:297
ConstStmtVisitor - This class implements a simple visitor for Stmt subclasses.
Definition: StmtVisitor.h:196
Represents the canonical version of C arrays with a specified constant size.
Definition: TypeBase.h:3776
uint64_t getZExtSize() const
Return the size zero-extended as a uint64_t.
Definition: TypeBase.h:3852
Stores additional source code information like skipped ranges which is required by the coverage mappi...
DeclContext - This is used only as base class of specific decl types that can act as declaration cont...
Definition: DeclBase.h:1449
lookup_result lookup(DeclarationName Name) const
lookup - Find the declarations (if any) with the given Name in this context.
Definition: DeclBase.cpp:1879
decl_range decls() const
decls_begin/decls_end - Iterate over the declarations stored in this context.
Definition: DeclBase.h:2373
A reference to a declared variable, function, enum, etc.
Definition: Expr.h:1272
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:86
Decl * getMostRecentDecl()
Retrieve the most recent declaration that declares the same entity as this declaration (which may be ...
Definition: DeclBase.h:1076
SourceLocation getEndLoc() const LLVM_READONLY
Definition: DeclBase.h:435
T * getAttr() const
Definition: DeclBase.h:573
ASTContext & getASTContext() const LLVM_READONLY
Definition: DeclBase.cpp:524
bool isImplicit() const
isImplicit - Indicates whether the declaration was implicitly generated by the implementation.
Definition: DeclBase.h:593
bool isWeakImported() const
Determine whether this is a weak-imported symbol.
Definition: DeclBase.cpp:848
unsigned getMaxAlignment() const
getMaxAlignment - return the maximum alignment specified by attributes on this decl,...
Definition: DeclBase.cpp:538
bool isTemplated() const
Determine whether this declaration is a templated entity (whether it is.
Definition: DeclBase.cpp:286
bool isInExportDeclContext() const
Whether this declaration was exported in a lexical context.
Definition: DeclBase.cpp:1121
FunctionDecl * getAsFunction() LLVM_READONLY
Returns the function itself, or the templated function if this is a function template.
Definition: DeclBase.cpp:251
const Attr * getDefiningAttr() const
Return this declaration's defining attribute if it has one.
Definition: DeclBase.cpp:616
llvm::iterator_range< specific_attr_iterator< T > > specific_attrs() const
Definition: DeclBase.h:559
SourceLocation getLocation() const
Definition: DeclBase.h:439
SourceLocation getBeginLoc() const LLVM_READONLY
Definition: DeclBase.h:431
DeclContext * getLexicalDeclContext()
getLexicalDeclContext - The declaration context where this Decl was lexically declared (LexicalDC).
Definition: DeclBase.h:918
bool hasAttr() const
Definition: DeclBase.h:577
virtual Decl * getCanonicalDecl()
Retrieves the "canonical" declaration of the given declaration.
Definition: DeclBase.h:978
Kind getKind() const
Definition: DeclBase.h:442
Represents a ValueDecl that came out of a declarator.
Definition: Decl.h:779
Concrete class used by the front-end to report problems and issues.
Definition: Diagnostic.h:231
DiagnosticBuilder Report(SourceLocation Loc, unsigned DiagID)
Issue the message to the client.
Definition: Diagnostic.h:1529
unsigned getCustomDiagID(Level L, const char(&FormatString)[N])
Return an ID for a diagnostic with the specified format string and level.
Definition: Diagnostic.h:904
This represents one expression.
Definition: Expr.h:112
QualType getType() const
Definition: Expr.h:144
Represents a member of a struct/union/class.
Definition: Decl.h:3157
static FieldDecl * Create(const ASTContext &C, DeclContext *DC, SourceLocation StartLoc, SourceLocation IdLoc, const IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo, Expr *BW, bool Mutable, InClassInitStyle InitStyle)
Definition: Decl.cpp:4641
A reference to a FileEntry that includes the name of the file as it was accessed by the FileManager's...
Definition: FileEntry.h:57
StringRef getName() const
The name of this FileEntry.
Definition: FileEntry.h:61
static FixItHint CreateReplacement(CharSourceRange RemoveRange, StringRef Code)
Create a code modification hint that replaces the given source range with the given code string.
Definition: Diagnostic.h:139
Represents a function declaration or definition.
Definition: Decl.h:1999
bool isTargetClonesMultiVersion() const
True if this function is a multiversioned dispatch function as a part of the target-clones functional...
Definition: Decl.cpp:3665
bool isMultiVersion() const
True if this function is considered a multiversioned function.
Definition: Decl.h:2686
const ParmVarDecl * getParamDecl(unsigned i) const
Definition: Decl.h:2794
Stmt * getBody(const FunctionDecl *&Definition) const
Retrieve the body (definition) of the function.
Definition: Decl.cpp:3271
bool isImmediateFunction() const
Definition: Decl.cpp:3328
unsigned getBuiltinID(bool ConsiderWrapperFunctions=false) const
Returns a value indicating whether this function corresponds to a builtin function.
Definition: Decl.cpp:3703
bool isInlined() const
Determine whether this function should be inlined, because it is either marked "inline" or "constexpr...
Definition: Decl.h:2918
bool isCPUSpecificMultiVersion() const
True if this function is a multiversioned processor specific function as a part of the cpu_specific/c...
Definition: Decl.cpp:3647
FunctionDecl * getTemplateInstantiationPattern(bool ForDefinition=true) const
Retrieve the function declaration from which this function could be instantiated, if it is an instant...
Definition: Decl.cpp:4205
bool isReplaceableGlobalAllocationFunction(UnsignedOrNone *AlignmentParam=nullptr, bool *IsNothrow=nullptr) const
Determines whether this function is one of the replaceable global allocation functions: void *operato...
Definition: Decl.h:2593
bool doesThisDeclarationHaveABody() const
Returns whether this specific declaration of the function has a body.
Definition: Decl.h:2325
bool isInlineBuiltinDeclaration() const
Determine if this function provides an inline implementation of a builtin.
Definition: Decl.cpp:3514
bool isConstexpr() const
Whether this is a (C++11) constexpr function or constexpr constructor.
Definition: Decl.h:2469
FunctionDecl * getDefinition()
Get the definition for this declaration.
Definition: Decl.h:2281
bool isTargetVersionMultiVersion() const
True if this function is a multiversioned dispatch function as a part of the target-version functiona...
Definition: Decl.cpp:3669
bool isCPUDispatchMultiVersion() const
True if this function is a multiversioned dispatch function as a part of the cpu_specific/cpu_dispatc...
Definition: Decl.cpp:3643
TemplateSpecializationKind getTemplateSpecializationKind() const
Determine what kind of template instantiation this function represents.
Definition: Decl.cpp:4358
bool doesDeclarationForceExternallyVisibleDefinition() const
For a function declaration in C or C++, determine whether this declaration causes the definition to b...
Definition: Decl.cpp:3882
bool isTargetMultiVersion() const
True if this function is a multiversioned dispatch function as a part of the target functionality.
Definition: Decl.cpp:3651
unsigned getNumParams() const
Return the number of parameters this function must have based on its FunctionType.
Definition: Decl.cpp:3767
bool hasBody(const FunctionDecl *&Definition) const
Returns true if the function has a body.
Definition: Decl.cpp:3191
bool isDefined(const FunctionDecl *&Definition, bool CheckForPendingFriendDefinition=false) const
Returns true if the function has a definition that does not need to be instantiated.
Definition: Decl.cpp:3238
MultiVersionKind getMultiVersionKind() const
Gets the kind of multiversioning attribute this declaration has.
Definition: Decl.cpp:3629
void getNameForDiagnostic(raw_ostream &OS, const PrintingPolicy &Policy, bool Qualified) const override
Appends a human-readable name for this declaration into the given stream.
Definition: Decl.cpp:3117
Represents a K&R-style 'int foo()' function, which has no information available about its arguments.
Definition: TypeBase.h:4860
Represents a prototype with parameter type info, e.g.
Definition: TypeBase.h:5282
FunctionType - C99 6.7.5.3 - Function Declarators.
Definition: TypeBase.h:4478
CallingConv getCallConv() const
Definition: TypeBase.h:4833
GlobalDecl - represents a global declaration.
Definition: GlobalDecl.h:57
GlobalDecl getWithMultiVersionIndex(unsigned Index)
Definition: GlobalDecl.h:192
CXXCtorType getCtorType() const
Definition: GlobalDecl.h:108
GlobalDecl getWithKernelReferenceKind(KernelReferenceKind Kind)
Definition: GlobalDecl.h:203
GlobalDecl getCanonicalDecl() const
Definition: GlobalDecl.h:97
KernelReferenceKind getKernelReferenceKind() const
Definition: GlobalDecl.h:135
GlobalDecl getWithDecl(const Decl *D)
Definition: GlobalDecl.h:172
unsigned getMultiVersionIndex() const
Definition: GlobalDecl.h:125
CXXDtorType getDtorType() const
Definition: GlobalDecl.h:113
const Decl * getDecl() const
Definition: GlobalDecl.h:106
HeaderSearchOptions - Helper class for storing options related to the initialization of the HeaderSea...
One of these records is kept for each identifier that is lexed.
StringRef getName() const
Return the actual identifier string.
IdentifierInfo & get(StringRef Name)
Return the identifier token info for the specified named identifier.
@ Swift5_0
Interoperability with the Swift 5.0 runtime.
@ Swift
Interoperability with the latest known version of the Swift runtime.
@ Swift4_2
Interoperability with the Swift 4.2 runtime.
@ Swift4_1
Interoperability with the Swift 4.1 runtime.
@ Protected
Override the IR-gen assigned visibility with protected visibility.
@ Default
Override the IR-gen assigned visibility with default visibility.
@ Hidden
Override the IR-gen assigned visibility with hidden visibility.
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:434
bool isSignReturnAddressWithAKey() const
Check if return address signing uses AKey.
Definition: LangOptions.h:693
clang::ObjCRuntime ObjCRuntime
Definition: LangOptions.h:469
CoreFoundationABI CFRuntime
Definition: LangOptions.h:471
SanitizerSet Sanitize
Set of enabled sanitizers.
Definition: LangOptions.h:440
bool hasSignReturnAddress() const
Check if return address signing is enabled.
Definition: LangOptions.h:688
std::string OMPHostIRFile
Name of the IR file that contains the result of the OpenMP target host code generation.
Definition: LangOptions.h:512
std::map< std::string, std::string, std::greater< std::string > > MacroPrefixMap
A prefix map for FILE, BASE_FILE and __builtin_FILE().
Definition: LangOptions.h:504
LangStandard::Kind LangStd
The used language standard.
Definition: LangOptions.h:437
bool isSignReturnAddressScopeAll() const
Check if leaf functions are also signed.
Definition: LangOptions.h:698
std::string CUID
The user provided compilation unit ID, if non-empty.
Definition: LangOptions.h:518
unsigned getOpenCLCompatibleVersion() const
Return the OpenCL version that kernel language is compatible with.
Definition: LangOptions.cpp:65
Visibility getVisibility() const
Definition: Visibility.h:89
void setLinkage(Linkage L)
Definition: Visibility.h:92
Linkage getLinkage() const
Definition: Visibility.h:88
bool isVisibilityExplicit() const
Definition: Visibility.h:90
Represents a linkage specification.
Definition: DeclCXX.h:3009
LinkageSpecLanguageIDs getLanguage() const
Return the language specified by this linkage specification.
Definition: DeclCXX.h:3032
A global _GUID constant.
Definition: DeclCXX.h:4392
Parts getParts() const
Get the decomposed parts of this declaration.
Definition: DeclCXX.h:4422
APValue & getAsAPValue() const
Get the value of this MSGuidDecl as an APValue.
Definition: DeclCXX.cpp:3745
MangleContext - Context for tracking state which persists across multiple calls to the C++ name mangl...
Definition: Mangle.h:52
void mangleBlock(const DeclContext *DC, const BlockDecl *BD, raw_ostream &Out)
Definition: Mangle.cpp:310
void mangleCtorBlock(const CXXConstructorDecl *CD, CXXCtorType CT, const BlockDecl *BD, raw_ostream &Out)
Definition: Mangle.cpp:292
void mangleGlobalBlock(const BlockDecl *BD, const NamedDecl *ID, raw_ostream &Out)
Definition: Mangle.cpp:275
bool shouldMangleDeclName(const NamedDecl *D)
Definition: Mangle.cpp:121
void mangleName(GlobalDecl GD, raw_ostream &)
Definition: Mangle.cpp:155
virtual void mangleCanonicalTypeName(QualType T, raw_ostream &, bool NormalizeIntegers=false)=0
Generates a unique string for an externally visible type for use with TBAA or type uniquing.
virtual void mangleStringLiteral(const StringLiteral *SL, raw_ostream &)=0
ManglerKind getKind() const
Definition: Mangle.h:72
virtual void needsUniqueInternalLinkageNames()
Definition: Mangle.h:132
virtual void mangleReferenceTemporary(const VarDecl *D, unsigned ManglingNumber, raw_ostream &)=0
void mangleDtorBlock(const CXXDestructorDecl *CD, CXXDtorType DT, const BlockDecl *BD, raw_ostream &Out)
Definition: Mangle.cpp:301
Represents a prvalue temporary that is written into memory so that a reference can bind to it.
Definition: ExprCXX.h:4914
Describes a module or submodule.
Definition: Module.h:144
bool isInterfaceOrPartition() const
Definition: Module.h:671
bool isNamedModuleUnit() const
Is this a C++20 named module unit.
Definition: Module.h:676
Module * Parent
The parent of this module.
Definition: Module.h:193
Module * getPrivateModuleFragment() const
Get the Private Module Fragment (sub-module) for this module, it there is one.
Definition: Module.cpp:372
llvm::SmallSetVector< Module *, 2 > Imports
The set of modules imported by this module, and on which this module depends.
Definition: Module.h:458
Module * getGlobalModuleFragment() const
Get the Global Module Fragment (sub-module) for this module, it there is one.
Definition: Module.cpp:361
llvm::iterator_range< submodule_iterator > submodules()
Definition: Module.h:838
llvm::SmallVector< LinkLibrary, 2 > LinkLibraries
The set of libraries or frameworks to link against when an entity from this module is used.
Definition: Module.h:520
bool isHeaderLikeModule() const
Is this module have similar semantics as headers.
Definition: Module.h:648
bool UseExportAsModuleLinkName
Autolinking uses the framework name for linking purposes when this is false and the export_as name ot...
Definition: Module.h:524
This represents a decl that may have a name.
Definition: Decl.h:273
IdentifierInfo * getIdentifier() const
Get the identifier that names this declaration, if there is one.
Definition: Decl.h:294
LinkageInfo getLinkageAndVisibility() const
Determines the linkage and visibility of this entity.
Definition: Decl.cpp:1226
StringRef getName() const
Get the name of identifier for this declaration as a StringRef.
Definition: Decl.h:300
Linkage getFormalLinkage() const
Get the linkage from a semantic point of view.
Definition: Decl.cpp:1206
Represent a C++ namespace.
Definition: Decl.h:591
This represents '#pragma omp threadprivate ...' directive.
Definition: DeclOpenMP.h:110
ObjCEncodeExpr, used for @encode in Objective-C.
Definition: ExprObjC.h:409
const ObjCInterfaceDecl * getClassInterface() const
Definition: DeclObjC.h:2486
ObjCImplementationDecl - Represents a class definition - this is where method definitions are specifi...
Definition: DeclObjC.h:2597
Represents an ObjC class declaration.
Definition: DeclObjC.h:1154
ObjCIvarDecl * all_declared_ivar_begin()
all_declared_ivar_begin - return first ivar declared in this class, its extensions and its implementa...
Definition: DeclObjC.cpp:1669
ObjCIvarDecl - Represents an ObjC instance variable.
Definition: DeclObjC.h:1952
ObjCIvarDecl * getNextIvar()
Definition: DeclObjC.h:1987
ObjCMethodDecl - Represents an instance or class method declaration.
Definition: DeclObjC.h:140
static ObjCMethodDecl * Create(ASTContext &C, SourceLocation beginLoc, SourceLocation endLoc, Selector SelInfo, QualType T, TypeSourceInfo *ReturnTInfo, DeclContext *contextDecl, bool isInstance=true, bool isVariadic=false, bool isPropertyAccessor=false, bool isSynthesizedAccessorStub=false, bool isImplicitlyDeclared=false, bool isDefined=false, ObjCImplementationControl impControl=ObjCImplementationControl::None, bool HasRelatedResultType=false)
Definition: DeclObjC.cpp:849
Represents one property declaration in an Objective-C interface.
Definition: DeclObjC.h:731
ObjCMethodDecl * getGetterMethodDecl() const
Definition: DeclObjC.h:901
bool isReadOnly() const
isReadOnly - Return true iff the property has a setter.
Definition: DeclObjC.h:838
The basic abstraction for the target Objective-C runtime.
Definition: ObjCRuntime.h:28
bool hasUnwindExceptions() const
Does this runtime use zero-cost exceptions?
Definition: ObjCRuntime.h:392
Kind getKind() const
Definition: ObjCRuntime.h:77
bool isGNUFamily() const
Is this runtime basically of the GNU family of runtimes?
Definition: ObjCRuntime.h:127
@ MacOSX
'macosx' is the Apple-provided NeXT-derived runtime on Mac OS X platforms that use the non-fragile AB...
Definition: ObjCRuntime.h:35
@ FragileMacOSX
'macosx-fragile' is the Apple-provided NeXT-derived runtime on Mac OS X platforms that use the fragil...
Definition: ObjCRuntime.h:40
@ GNUstep
'gnustep' is the modern non-fragile GNUstep runtime.
Definition: ObjCRuntime.h:56
@ ObjFW
'objfw' is the Objective-C runtime included in ObjFW
Definition: ObjCRuntime.h:59
@ iOS
'ios' is the Apple-provided NeXT-derived runtime on iOS or the iOS simulator; it is always non-fragil...
Definition: ObjCRuntime.h:45
@ GCC
'gcc' is the Objective-C runtime shipped with GCC, implementing a fragile Objective-C ABI
Definition: ObjCRuntime.h:53
@ WatchOS
'watchos' is a variant of iOS for Apple's watchOS.
Definition: ObjCRuntime.h:49
Represents a parameter to a function.
Definition: Decl.h:1789
ParsedAttr - Represents a syntactic attribute.
Definition: ParsedAttr.h:119
PipeType - OpenCL20.
Definition: TypeBase.h:8161
uint16_t getConstantDiscrimination() const
PreprocessorOptions - This class is used for passing the various options used in preprocessor initial...
std::optional< uint64_t > SourceDateEpoch
If set, the UNIX timestamp specified by SOURCE_DATE_EPOCH.
std::vector< std::pair< std::string, bool > > Macros
Represents an unpacked "presumed" location which can be presented to the user.
const char * getFilename() const
Return the presumed filename of this location.
bool isValid() const
unsigned getLine() const
Return the presumed line number of this location.
PrettyStackTraceDecl - If a crash occurs, indicate that it happened when doing something to a specifi...
Definition: DeclBase.h:1300
ExclusionType getDefault(llvm::driver::ProfileInstrKind Kind) const
Definition: ProfileList.cpp:89
std::optional< ExclusionType > isFunctionExcluded(StringRef FunctionName, llvm::driver::ProfileInstrKind Kind) const
bool isEmpty() const
Definition: ProfileList.h:51
std::optional< ExclusionType > isFileExcluded(StringRef FileName, llvm::driver::ProfileInstrKind Kind) const
ExclusionType
Represents if an how something should be excluded from profiling.
Definition: ProfileList.h:31
@ Skip
Profiling is skipped using the skipprofile attribute.
Definition: ProfileList.h:35
@ Allow
Profiling is allowed.
Definition: ProfileList.h:33
std::optional< ExclusionType > isLocationExcluded(SourceLocation Loc, llvm::driver::ProfileInstrKind Kind) const
A (possibly-)qualified type.
Definition: TypeBase.h:937
bool isVolatileQualified() const
Determine whether this type is volatile-qualified.
Definition: TypeBase.h:8427
bool isRestrictQualified() const
Determine whether this type is restrict-qualified.
Definition: TypeBase.h:8421
bool isNull() const
Return true if this QualType doesn't point to a type yet.
Definition: TypeBase.h:1004
const Type * getTypePtr() const
Retrieves a pointer to the underlying (unqualified) type.
Definition: TypeBase.h:8343
LangAS getAddressSpace() const
Return the address space of this type.
Definition: TypeBase.h:8469
QualType getCanonicalType() const
Definition: TypeBase.h:8395
QualType getUnqualifiedType() const
Retrieve the unqualified variant of the given type, removing as little sugar as possible.
Definition: TypeBase.h:8437
QualType withCVRQualifiers(unsigned CVR) const
Definition: TypeBase.h:1179
bool isConstQualified() const
Determine whether this type is const-qualified.
Definition: TypeBase.h:8416
bool isConstantStorage(const ASTContext &Ctx, bool ExcludeCtor, bool ExcludeDtor)
Definition: TypeBase.h:1036
unsigned getCVRQualifiers() const
Retrieve the set of CVR (const-volatile-restrict) qualifiers applied to this type.
Definition: TypeBase.h:8389
static std::string getAsString(SplitQualType split, const PrintingPolicy &Policy)
Definition: TypeBase.h:1332
Represents a struct/union/class.
Definition: Decl.h:4309
A helper class that allows the use of isa/cast/dyncast to detect TagType objects of structs/unions/cl...
Definition: TypeBase.h:6502
A class that does preorder or postorder depth-first traversal on the entire Clang AST and visits each...
decl_type * getPreviousDecl()
Return the previous declaration of this declaration or NULL if this is the first declaration.
Definition: Redeclarable.h:201
decl_type * getMostRecentDecl()
Returns the most recent (re)declaration of this declaration.
Definition: Redeclarable.h:223
redecl_range redecls() const
Returns an iterator range for all the redeclarations of the same decl.
Definition: Redeclarable.h:293
Selector getSelector(unsigned NumArgs, const IdentifierInfo **IIV)
Can create any sort of selector.
Smart pointer class that efficiently represents Objective-C method names.
Encodes a location in the source.
bool isValid() const
Return true if this is a valid SourceLocation object.
This class handles loading and caching of source files into memory.
A trivial tuple used to represent a source range.
void runWithSufficientStackSpace(SourceLocation Loc, llvm::function_ref< void()> Fn)
Run some code with "sufficient" stack space.
Stmt - This represents one statement.
Definition: Stmt.h:85
StringLiteral - This represents a string literal expression, e.g.
Definition: Expr.h:1801
Represents the declaration of a struct/union/class/enum.
Definition: Decl.h:3714
bool isMicrosoft() const
Is this ABI an MSVC-compatible ABI?
Definition: TargetCXXABI.h:136
Exposes information about the current target.
Definition: TargetInfo.h:226
TargetOptions & getTargetOpts() const
Retrieve the target options.
Definition: TargetInfo.h:323
const llvm::Triple & getTriple() const
Returns the target triple of the primary target.
Definition: TargetInfo.h:1288
bool isReadOnlyFeature(StringRef Feature) const
Determine whether the given target feature is read only.
Definition: TargetInfo.h:1531
unsigned getIntWidth() const
getIntWidth/Align - Return the size of 'signed int' and 'unsigned int' for this target,...
Definition: TargetInfo.h:527
virtual llvm::APInt getFMVPriority(ArrayRef< StringRef > Features) const
Definition: TargetInfo.h:1567
bool supportsIFunc() const
Identify whether this target supports IFuncs.
Definition: TargetInfo.h:1543
TargetCXXABI getCXXABI() const
Get the C++ ABI currently in use.
Definition: TargetInfo.h:1360
unsigned getLongWidth() const
getLongWidth/Align - Return the size of 'signed long' and 'unsigned long' for this target,...
Definition: TargetInfo.h:532
virtual bool hasFeature(StringRef Feature) const
Determine whether the given target has the given feature.
Definition: TargetInfo.h:1526
std::vector< std::string > Features
The list of target specific features to enable or disable – this should be a list of strings starting...
Definition: TargetOptions.h:58
std::string TuneCPU
If given, the name of the target CPU to tune code for.
Definition: TargetOptions.h:39
std::string CPU
If given, the name of the target CPU to generate code for.
Definition: TargetOptions.h:36
@ Hostcall
printf lowering scheme involving hostcalls, currently used by HIP programs by default
A template parameter object.
const APValue & getValue() const
A declaration that models statements at global scope.
Definition: Decl.h:4597
The top declaration context.
Definition: Decl.h:104
static DeclContext * castToDeclContext(const TranslationUnitDecl *D)
Definition: Decl.h:150
The base class of the type hierarchy.
Definition: TypeBase.h:1833
CXXRecordDecl * getAsCXXRecordDecl() const
Retrieves the CXXRecordDecl that this type refers to, either because the type is a RecordType or beca...
Definition: Type.h:26
RecordDecl * getAsRecordDecl() const
Retrieves the RecordDecl this type refers to.
Definition: Type.h:41
bool isArrayType() const
Definition: TypeBase.h:8679
bool isPointerType() const
Definition: TypeBase.h:8580
const T * castAs() const
Member-template castAs<specific type>.
Definition: TypeBase.h:9226
bool isCUDADeviceBuiltinSurfaceType() const
Check if the type is the CUDA device builtin surface type.
Definition: Type.cpp:5379
QualType getPointeeType() const
If this is a pointer, ObjC object pointer, or block pointer, this returns the respective pointee.
Definition: Type.cpp:752
bool isImageType() const
Definition: TypeBase.h:8834
bool isPipeType() const
Definition: TypeBase.h:8841
const Type * getBaseElementTypeUnsafe() const
Get the base element type of this type, potentially discarding type qualifiers.
Definition: TypeBase.h:9109
bool isCUDADeviceBuiltinTextureType() const
Check if the type is the CUDA device builtin texture type.
Definition: Type.cpp:5388
bool isIncompleteType(NamedDecl **Def=nullptr) const
Types are partitioned into 3 broad categories (C99 6.2.5p1): object types, function types,...
Definition: Type.cpp:2440
bool isObjCObjectPointerType() const
Definition: TypeBase.h:8749
const T * getAsCanonical() const
If this type is canonically the specified type, return its canonical type cast to that specified type...
Definition: TypeBase.h:2939
Linkage getLinkage() const
Determine the linkage of this type.
Definition: Type.cpp:4943
bool isSamplerT() const
Definition: TypeBase.h:8814
const T * getAs() const
Member-template getAs<specific type>'.
Definition: TypeBase.h:9159
bool isRecordType() const
Definition: TypeBase.h:8707
An artificial decl, representing a global anonymous constant value which is uniquified by value withi...
Definition: DeclCXX.h:4449
const APValue & getValue() const
Definition: DeclCXX.h:4475
Represent the declaration of a variable (in which case it is an lvalue) a function (in which case it ...
Definition: Decl.h:711
QualType getType() const
Definition: Decl.h:722
Represents a variable declaration or definition.
Definition: Decl.h:925
TLSKind getTLSKind() const
Definition: Decl.cpp:2168
bool hasInit() const
Definition: Decl.cpp:2398
DefinitionKind isThisDeclarationADefinition(ASTContext &) const
Check whether this declaration is a definition.
Definition: Decl.cpp:2260
bool hasFlexibleArrayInit(const ASTContext &Ctx) const
Whether this variable has a flexible array member initialized with one or more elements.
Definition: Decl.cpp:2862
CharUnits getFlexibleArrayInitChars(const ASTContext &Ctx) const
If hasFlexibleArrayInit is true, compute the number of additional bytes necessary to store those elem...
Definition: Decl.cpp:2877
VarDecl * getDefinition(ASTContext &)
Get the real (not just tentative) definition for this declaration.
Definition: Decl.cpp:2366
@ TLS_Dynamic
TLS with a dynamic initializer.
Definition: Decl.h:951
@ DeclarationOnly
This declaration is only a declaration.
Definition: Decl.h:1294
@ Definition
This declaration is definitely a definition.
Definition: Decl.h:1300
TemplateSpecializationKind getTemplateSpecializationKind() const
If this variable is an instantiation of a variable template or a static data member of a class templa...
Definition: Decl.cpp:2779
Defines the clang::TargetInfo interface.
#define INT_MAX
Definition: limits.h:50
#define UINT_MAX
Definition: limits.h:64
std::unique_ptr< TargetCodeGenInfo > createARMTargetCodeGenInfo(CodeGenModule &CGM, ARMABIKind Kind)
Definition: ARM.cpp:846
std::unique_ptr< TargetCodeGenInfo > createM68kTargetCodeGenInfo(CodeGenModule &CGM)
Definition: M68k.cpp:53
@ AttributedType
The l-value was considered opaque, so the alignment was determined from a type, but that type was an ...
@ Type
The l-value was considered opaque, so the alignment was determined from a type.
@ Decl
The l-value was an access to a declared entity or something equivalently strong, like the address of ...
std::unique_ptr< TargetCodeGenInfo > createBPFTargetCodeGenInfo(CodeGenModule &CGM)
Definition: BPF.cpp:102
std::unique_ptr< TargetCodeGenInfo > createMSP430TargetCodeGenInfo(CodeGenModule &CGM)
Definition: MSP430.cpp:96
std::unique_ptr< TargetCodeGenInfo > createX86_64TargetCodeGenInfo(CodeGenModule &CGM, X86AVXABILevel AVXLevel)
Definition: X86.cpp:3532
std::unique_ptr< TargetCodeGenInfo > createWebAssemblyTargetCodeGenInfo(CodeGenModule &CGM, WebAssemblyABIKind K)
std::unique_ptr< TargetCodeGenInfo > createPPC64_SVR4_TargetCodeGenInfo(CodeGenModule &CGM, PPC64_SVR4_ABIKind Kind, bool SoftFloatABI)
Definition: PPC.cpp:1056
std::unique_ptr< TargetCodeGenInfo > createMIPSTargetCodeGenInfo(CodeGenModule &CGM, bool IsOS32)
Definition: Mips.cpp:455
std::unique_ptr< TargetCodeGenInfo > createHexagonTargetCodeGenInfo(CodeGenModule &CGM)
Definition: Hexagon.cpp:420
std::unique_ptr< TargetCodeGenInfo > createNVPTXTargetCodeGenInfo(CodeGenModule &CGM)
Definition: NVPTX.cpp:361
std::unique_ptr< TargetCodeGenInfo > createSystemZTargetCodeGenInfo(CodeGenModule &CGM, bool HasVector, bool SoftFloatABI)
Definition: SystemZ.cpp:548
std::unique_ptr< TargetCodeGenInfo > createWinX86_32TargetCodeGenInfo(CodeGenModule &CGM, bool DarwinVectorABI, bool Win32StructABI, unsigned NumRegisterParameters)
Definition: X86.cpp:3521
std::unique_ptr< TargetCodeGenInfo > createAIXTargetCodeGenInfo(CodeGenModule &CGM, bool Is64Bit)
Definition: PPC.cpp:1039
std::unique_ptr< TargetCodeGenInfo > createAMDGPUTargetCodeGenInfo(CodeGenModule &CGM)
Definition: AMDGPU.cpp:758
CGObjCRuntime * CreateMacObjCRuntime(CodeGenModule &CGM)
X86AVXABILevel
The AVX ABI level for X86 targets.
Definition: TargetInfo.h:604
std::unique_ptr< TargetCodeGenInfo > createTCETargetCodeGenInfo(CodeGenModule &CGM)
Definition: TCE.cpp:77
CGObjCRuntime * CreateGNUObjCRuntime(CodeGenModule &CGM)
Creates an instance of an Objective-C runtime class.
Definition: CGObjCGNU.cpp:4434
std::unique_ptr< TargetCodeGenInfo > createWindowsARMTargetCodeGenInfo(CodeGenModule &CGM, ARMABIKind K)
Definition: ARM.cpp:851
std::unique_ptr< TargetCodeGenInfo > createAVRTargetCodeGenInfo(CodeGenModule &CGM, unsigned NPR, unsigned NRR)
Definition: AVR.cpp:151
std::unique_ptr< TargetCodeGenInfo > createDirectXTargetCodeGenInfo(CodeGenModule &CGM)
Definition: DirectX.cpp:97
std::unique_ptr< TargetCodeGenInfo > createARCTargetCodeGenInfo(CodeGenModule &CGM)
Definition: ARC.cpp:160
std::unique_ptr< TargetCodeGenInfo > createDefaultTargetCodeGenInfo(CodeGenModule &CGM)
Definition: TargetInfo.cpp:309
std::unique_ptr< TargetCodeGenInfo > createAArch64TargetCodeGenInfo(CodeGenModule &CGM, AArch64ABIKind Kind)
Definition: AArch64.cpp:1345
std::unique_ptr< TargetCodeGenInfo > createSPIRVTargetCodeGenInfo(CodeGenModule &CGM)
Definition: SPIR.cpp:624
std::unique_ptr< TargetCodeGenInfo > createWindowsMIPSTargetCodeGenInfo(CodeGenModule &CGM, bool IsOS32)
Definition: Mips.cpp:460
std::unique_ptr< TargetCodeGenInfo > createSparcV8TargetCodeGenInfo(CodeGenModule &CGM)
Definition: Sparc.cpp:411
std::unique_ptr< TargetCodeGenInfo > createVETargetCodeGenInfo(CodeGenModule &CGM)
Definition: VE.cpp:69
std::unique_ptr< TargetCodeGenInfo > createCommonSPIRTargetCodeGenInfo(CodeGenModule &CGM)
Definition: SPIR.cpp:619
std::unique_ptr< TargetCodeGenInfo > createRISCVTargetCodeGenInfo(CodeGenModule &CGM, unsigned XLen, unsigned FLen, bool EABI)
Definition: RISCV.cpp:1026
std::unique_ptr< TargetCodeGenInfo > createWindowsAArch64TargetCodeGenInfo(CodeGenModule &CGM, AArch64ABIKind K)
Definition: AArch64.cpp:1351
std::unique_ptr< TargetCodeGenInfo > createSparcV9TargetCodeGenInfo(CodeGenModule &CGM)
Definition: Sparc.cpp:416
std::unique_ptr< TargetCodeGenInfo > createX86_32TargetCodeGenInfo(CodeGenModule &CGM, bool DarwinVectorABI, bool Win32StructABI, unsigned NumRegisterParameters, bool SoftFloatABI)
Definition: X86.cpp:3511
std::unique_ptr< TargetCodeGenInfo > createLanaiTargetCodeGenInfo(CodeGenModule &CGM)
Definition: Lanai.cpp:157
std::unique_ptr< TargetCodeGenInfo > createPPC32TargetCodeGenInfo(CodeGenModule &CGM, bool SoftFloatABI)
Definition: PPC.cpp:1044
CGCUDARuntime * CreateNVCUDARuntime(CodeGenModule &CGM)
Creates an instance of a CUDA runtime class.
Definition: CGCUDANV.cpp:1078
std::unique_ptr< TargetCodeGenInfo > createLoongArchTargetCodeGenInfo(CodeGenModule &CGM, unsigned GRLen, unsigned FLen)
Definition: LoongArch.cpp:459
std::unique_ptr< TargetCodeGenInfo > createPPC64TargetCodeGenInfo(CodeGenModule &CGM)
Definition: PPC.cpp:1052
std::unique_ptr< TargetCodeGenInfo > createWinX86_64TargetCodeGenInfo(CodeGenModule &CGM, X86AVXABILevel AVXLevel)
Definition: X86.cpp:3538
std::unique_ptr< TargetCodeGenInfo > createXCoreTargetCodeGenInfo(CodeGenModule &CGM)
Definition: XCore.cpp:658
std::unique_ptr< TargetCodeGenInfo > createCSKYTargetCodeGenInfo(CodeGenModule &CGM, unsigned FLen)
Definition: CSKY.cpp:173
llvm::PointerUnion< const Decl *, const Expr * > DeclTy
Definition: Descriptor.h:29
The JSON file list parser is used to communicate input to InstallAPI.
CXXCtorType
C++ constructor types.
Definition: ABI.h:24
@ Ctor_Base
Base object ctor.
Definition: ABI.h:26
@ Ctor_Complete
Complete object ctor.
Definition: ABI.h:25
@ OpenCL
Definition: LangStandard.h:65
@ CPlusPlus
Definition: LangStandard.h:55
if(T->getSizeExpr()) TRY_TO(TraverseStmt(const_cast< Expr * >(T -> getSizeExpr())))
void EmbedObject(llvm::Module *M, const CodeGenOptions &CGOpts, DiagnosticsEngine &Diags)
GVALinkage
A more specific kind of linkage than enum Linkage.
Definition: Linkage.h:72
@ GVA_StrongODR
Definition: Linkage.h:77
@ GVA_StrongExternal
Definition: Linkage.h:76
@ GVA_AvailableExternally
Definition: Linkage.h:74
@ GVA_DiscardableODR
Definition: Linkage.h:75
@ GVA_Internal
Definition: Linkage.h:73
std::string getClangVendor()
Retrieves the Clang vendor tag.
Definition: Version.cpp:60
@ PCK_ExeStr
Definition: PragmaKinds.h:19
@ PCK_Compiler
Definition: PragmaKinds.h:18
@ PCK_Linker
Definition: PragmaKinds.h:16
@ PCK_Lib
Definition: PragmaKinds.h:17
@ PCK_Unknown
Definition: PragmaKinds.h:15
@ PCK_User
Definition: PragmaKinds.h:20
@ ICIS_NoInit
No in-class initializer.
Definition: Specifiers.h:272
CXXABI * CreateMicrosoftCXXABI(ASTContext &Ctx)
@ AS_public
Definition: Specifiers.h:124
@ CLanguageLinkage
Definition: Linkage.h:64
CXXABI * CreateItaniumCXXABI(ASTContext &Ctx)
Creates an instance of a C++ ABI class.
Linkage
Describes the different kinds of linkage (C++ [basic.link], C99 6.2.2) that an entity may have.
Definition: Linkage.h:24
@ Internal
Internal linkage, which indicates that the entity can be referred to from within the translation unit...
@ Asm
Assembly: we accept this only so that we can preprocess it.
@ SD_Thread
Thread storage duration.
Definition: Specifiers.h:342
@ SD_Static
Static storage duration.
Definition: Specifiers.h:343
bool isLambdaCallOperator(const CXXMethodDecl *MD)
Definition: ASTLambda.h:28
@ Result
The result type of a method or function.
StringRef languageToString(Language L)
@ Dtor_Base
Base object dtor.
Definition: ABI.h:36
@ Dtor_Complete
Complete object dtor.
Definition: ABI.h:35
LangAS
Defines the address space values used by the address space qualifier of QualType.
Definition: AddressSpaces.h:25
static const char * getCFBranchLabelSchemeFlagVal(const CFBranchLabelSchemeKind Scheme)
const FunctionProtoType * T
TemplateSpecializationKind
Describes the kind of template specialization that a particular template specialization declaration r...
Definition: Specifiers.h:188
@ TSK_ExplicitInstantiationDefinition
This template specialization was instantiated from a template due to an explicit instantiation defini...
Definition: Specifiers.h:206
@ TSK_ImplicitInstantiation
This template specialization was implicitly instantiated from a template.
Definition: Specifiers.h:194
CallingConv
CallingConv - Specifies the calling convention that a function uses.
Definition: Specifiers.h:278
@ CC_X86RegCall
Definition: Specifiers.h:287
@ None
No keyword precedes the qualified type name.
@ Struct
The "struct" keyword introduces the elaborated-type-specifier.
bool isExternallyVisible(Linkage L)
Definition: Linkage.h:90
@ EST_None
no exception specification
std::string getClangFullVersion()
Retrieves a string representing the complete clang version, which includes the clang version number,...
Definition: Version.cpp:96
@ HiddenVisibility
Objects with "hidden" visibility are not seen by the dynamic linker.
Definition: Visibility.h:37
@ DefaultVisibility
Objects with "default" visibility are seen by the dynamic linker and act like normal objects.
Definition: Visibility.h:46
cl::opt< bool > SystemHeadersCoverage
int const char * function
Definition: c++config.h:31
__UINTPTR_TYPE__ uintptr_t
An unsigned integer type with the property that any valid pointer to void can be converted to this ty...
llvm::PointerType * ConstGlobalsPtrTy
void* in the address space for constant globals
llvm::IntegerType * Int8Ty
i8, i16, i32, and i64
llvm::IntegerType * CharTy
char
unsigned char PointerWidthInBits
The width of a pointer into the generic address space.
llvm::Type * HalfTy
half, bfloat, float, double
llvm::CallingConv::ID getRuntimeCC() const
llvm::PointerType * GlobalsInt8PtrTy
llvm::IntegerType * IntTy
int
llvm::PointerType * AllocaInt8PtrTy
EvalResult is a struct with detailed info about an evaluated expression.
Definition: Expr.h:645
APValue Val
Val - This is the value the expression can be folded to.
Definition: Expr.h:647
bool hasSideEffects() const
Definition: Expr.h:639
Extra information about a function prototype.
Definition: TypeBase.h:5367
clang::Language Language
Definition: LangStandard.h:82
static const LangStandard & getLangStandardForKind(Kind K)
Parts of a decomposed MSGuidDecl.
Definition: DeclCXX.h:4367
uint16_t Part2
...-89ab-...
Definition: DeclCXX.h:4371
uint32_t Part1
{01234567-...
Definition: DeclCXX.h:4369
uint16_t Part3
...-cdef-...
Definition: DeclCXX.h:4373
uint8_t Part4And5[8]
...-0123-456789abcdef}
Definition: DeclCXX.h:4375
A library or framework to link against when an entity from this module is used.
Definition: Module.h:503
Contains information gathered from parsing the contents of TargetAttr.
Definition: TargetInfo.h:60
PointerAuthSchema InitFiniPointers
The ABI for function addresses in .init_array and .fini_array.
Describes how types, statements, expressions, and declarations should be printed.
Definition: PrettyPrinter.h:57
bool has(SanitizerMask K) const
Check if a certain (single) sanitizer is enabled.
Definition: Sanitizers.h:174
bool hasOneOf(SanitizerMask K) const
Check if one or more sanitizers are enabled.
Definition: Sanitizers.h:184