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