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