Lines Matching +full:test +full:- +full:part1

1 //===--- CodeGenModule.cpp - Emit LLVM Code from ASTs for a Module --------===//
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This coordinates the per-module state used while generating code.
11 //===----------------------------------------------------------------------===//
84 "limited-coverage-experimental", llvm::cl::Hidden,
150 else if (Target.getABI() == "aapcs-soft")
161 if (Target.getABI() == "experimental-mv")
175 if (ABIStr == "apcs-gnu")
212 assert(Triple.isOSBinFormatELF() && "PPC64 LE non-ELF not supported!");
303 bool IsSoftFloat = !Target.hasFeature("hard-float-abi");
524 NewF = dyn_cast<llvm::Function>(Alias->getAliasee());
527 assert(CE->getOpcode() == llvm::Instruction::BitCast ||
528 CE->getOpcode() == llvm::Instruction::GetElementPtr);
529 NewF = dyn_cast<llvm::Function>(CE->getOperand(0));
534 OldF->replaceAllUsesWith(Replacement);
536 NewF->removeFromParent();
537 OldF->getParent()->getFunctionList().insertAfter(OldF->getIterator(),
540 OldF->eraseFromParent();
553 GV->replaceAllUsesWith(C);
554 GV->eraseFromParent();
563 C = GA->getAliasee();
565 C = GI->getResolver();
569 const auto *AliaseeGV = dyn_cast<llvm::GlobalValue>(C->stripPointerCasts());
573 const llvm::GlobalValue *FinalGV = AliaseeGV->getAliaseeObject();
591 if (GV->hasCommonLinkage()) {
599 if (GV->isDeclaration()) {
607 if (ND->getName() == GV->getName()) {
629 llvm::FunctionType *FTy = F->getFunctionType();
630 if (!FTy->getReturnType()->isPointerTy()) {
639 // Emit a warning if toc-data attribute is requested for global variables that
640 // have aliases and remove the toc-data attribute.
645 if (GVar->hasAttribute("toc-data")) {
646 auto GVId = GVar->getName();
652 llvm::AttributeSet CurrAttributes = GVar->getAttributes();
654 CurrAttributes.removeAttribute(GVar->getContext(), "toc-data");
655 GVar->setAttributes(NewAttributes);
669 bool IsIFunc = D->hasAttr<IFuncAttr>();
670 if (const Attr *A = D->getDefiningAttr()) {
671 Location = A->getLocation();
672 Range = A->getRange();
692 IsIFunc ? cast<llvm::GlobalIFunc>(Alias)->getResolver()
693 : cast<llvm::GlobalAlias>(Alias)->getAliasee();
697 AliaseeGV = cast<llvm::GlobalValue>(CE->getOperand(0));
701 if (const SectionAttr *SA = D->getAttr<SectionAttr>()) {
702 StringRef AliasSection = SA->getName();
703 if (AliasSection != AliaseeGV->getSection())
704 Diags.Report(SA->getLocation(), diag::warn_alias_with_section)
714 if (GA->isInterposable()) {
716 << GV->getName() << GA->getName() << IsIFunc;
718 GA->getAliasee(), Alias->getType());
721 cast<llvm::GlobalIFunc>(Alias)->setResolver(Aliasee);
723 cast<llvm::GlobalAlias>(Alias)->setAliasee(Aliasee);
729 cast<llvm::Function>(Aliasee)->addFnAttr(
738 Alias->replaceAllUsesWith(llvm::UndefValue::get(Alias->getType()));
739 Alias->eraseFromParent();
748 OpenMPRuntime->clear();
847 if (CXX20ModuleInits && Primary && !Primary->isHeaderLikeModule())
861 GlobalTopLevelStmtBlockInFlight.first->FinishFunction(TLSD->getEndLoc());
867 if (CXX20ModuleInits && Primary && Primary->isInterfaceOrPartition())
875 if (llvm::Function *ObjCInitFunction = ObjCRuntime->ModuleInitFunction())
878 if (llvm::Function *CudaCtorFunction = CUDARuntime->finalizeModule())
882 OpenMPRuntime->createOffloadEntriesAndInfoMetadata();
883 OpenMPRuntime->clear();
887 PGOReader->getSummary(/* UseCS */ false).getMD(VMContext),
904 CoverageMapping->emit();
926 // Currently, "-mprintf-kind" option is only supported for HIP
972 SanStats->finish();
990 // Windows. Therefore emit llvm.dependent-libraries only for host.
992 auto *NMD = getModule().getOrInsertNamedMetadata("llvm.dependent-libraries");
994 NMD->addOperand(MD);
1029 getModule().addModuleFlag(llvm::Module::Warning, "ms-kernel", 1);
1101 getModule().addModuleFlag(llvm::Module::Error, "target-abi",
1113 llvm::Module::AppendUnique, "riscv-isa",
1115 Ctx, llvm::MDString::get(Ctx, (*ParseResult)->toString())));
1119 // Indicate that we want cross-DSO control flow integrity checks.
1120 getModule().addModuleFlag(llvm::Module::Override, "Cross-DSO CFI", 1);
1138 getModule().addModuleFlag(llvm::Module::Override, "cfi-normalize-integers",
1144 // KCFI assumes patchable-function-prefix is the same for all indirectly
1147 getModule().addModuleFlag(llvm::Module::Override, "kcfi-offset",
1154 getModule().addModuleFlag(llvm::Module::Min, "cf-protection-return",
1161 getModule().addModuleFlag(llvm::Module::Min, "cf-protection-branch",
1172 // non-leaf/all) and stack tagging. These are actually turned on by function
1184 "sign-return-address-buildattr", 1);
1187 "tag-stack-memory-buildattr", 1);
1191 getModule().addModuleFlag(llvm::Module::Min, "branch-target-enforcement",
1194 getModule().addModuleFlag(llvm::Module::Min, "branch-protection-pauth-lr",
1197 getModule().addModuleFlag(llvm::Module::Min, "guarded-control-stack", 1);
1199 getModule().addModuleFlag(llvm::Module::Min, "sign-return-address", 1);
1201 getModule().addModuleFlag(llvm::Module::Min, "sign-return-address-all",
1205 "sign-return-address-with-bkey", 1);
1230 "aarch64-elf-pauthabi-platform",
1233 "aarch64-elf-pauthabi-version",
1241 llvm::Module::Override, "probe-stack",
1242 llvm::MDString::get(TheModule.getContext(), "inline-asm"));
1245 getModule().addModuleFlag(llvm::Module::Min, "stack-probe-size",
1259 getModule().addModuleFlag(llvm::Module::Override, "nvvm-reflect-ftz",
1265 getModule().addModuleFlag(llvm::Module::Warning, "eh-asynch", 1);
1267 // Indicate whether this Module was compiled with -fopenmp
1271 getModule().addModuleFlag(llvm::Module::Max, "openmp-device",
1279 // SPIR v2.0 s2.12 - The SPIR version used by the module is stored in the
1292 SPIRVerMD->addOperand(llvm::MDNode::get(Ctx, SPIRVerElts));
1363 DI->finalize();
1411 if (!I.first->isDefined())
1416 if (!Entry || Entry->isWeakForLinker() ||
1417 Entry->isDeclarationForLinker())
1425 // SPIR v2.0 s2.13 - The OpenCL version used by the module is stored in the
1438 OCLVerMD->addOperand(llvm::MDNode::get(Ctx, OCLVerElts));
1469 return TBAA->getTypeInfo(QTy);
1478 if (AccessType->isCUDADeviceBuiltinSurfaceType()) {
1482 } else if (AccessType->isCUDADeviceBuiltinTextureType()) {
1488 return TBAA->getAccessInfo(AccessType);
1495 return TBAA->getVTablePtrAccessInfo(VTablePtrType);
1501 return TBAA->getTBAAStructInfo(QTy);
1507 return TBAA->getBaseTypeInfo(QTy);
1513 return TBAA->getAccessTagInfo(Info);
1520 return TBAA->mergeTBAAInfoForCast(SourceInfo, TargetInfo);
1528 return TBAA->mergeTBAAInfoForConditionalOperator(InfoA, InfoB);
1536 return TBAA->mergeTBAAInfoForConditionalOperator(DestInfo, SrcInfo);
1542 Inst->setMetadata(llvm::LLVMContext::MD_tbaa, Tag);
1547 I->setMetadata(llvm::LLVMContext::MD_invariant_group,
1556 /// ErrorUnsupported - Print out an error that codegen doesn't support the
1562 getDiags().Report(Context.getFullLoc(S->getBeginLoc()), DiagID)
1563 << Msg << S->getSourceRange();
1566 /// ErrorUnsupported - Print out an error that codegen doesn't support the
1572 getDiags().Report(Context.getFullLoc(D->getLocation()), DiagID) << Msg;
1582 if (GV->hasLocalLinkage()) {
1583 GV->setVisibility(llvm::GlobalValue::DefaultVisibility);
1591 LinkageInfo LV = D->getLinkageAndVisibility();
1598 D->hasAttr<OMPDeclareTargetDeclAttr>() &&
1599 D->getAttr<OMPDeclareTargetDeclAttr>()->getDevType() !=
1602 GV->setVisibility(llvm::GlobalValue::ProtectedVisibility);
1606 if (GV->hasDLLExportStorageClass() || GV->hasDLLImportStorageClass()) {
1610 if (GV->hasDLLExportStorageClass()) {
1612 getDiags().Report(D->getLocation(),
1615 getDiags().Report(D->getLocation(),
1622 !GV->isDeclarationForLinker())
1623 GV->setVisibility(GetLLVMVisibility(LV.getVisibility()));
1628 if (GV->hasLocalLinkage())
1631 if (!GV->hasDefaultVisibility() && !GV->hasExternalWeakLinkage())
1635 if (GV->hasDLLImportStorageClass())
1649 if (GV->isDeclarationForLinker() && isa<llvm::GlobalVariable>(GV) &&
1650 (!GV->isThreadLocal() || CGM.getCodeGenOpts().EmulatedTLS) &&
1658 if (TT.isOSBinFormatCOFF() && GV->hasExternalWeakLinkage())
1663 // *-win32-macho triples. This (accidentally?) produced windows relocations
1677 // On ELF, if -fno-semantic-interposition is specified and the target
1679 // -fsemantic-interposition nor -fhalf-no-semantic-interposition. Set
1682 if (!(isa<llvm::Function>(GV) && GV->canBenefitFromLocalAlias()))
1689 if (!GV->isDeclarationForLinker())
1695 if (RM == llvm::Reloc::PIC_ && GV->hasExternalWeakLinkage())
1703 // If -fdirect-access-external-data (default for -fno-pic), set dso_local
1704 // for non-thread-local variables. If the symbol is not defined in the
1706 // excluded for thread-local variables because they generally don't support
1709 if (!Var->isThreadLocal())
1712 // -fno-pic sets dso_local on a function declaration to allow direct
1715 // needed at link time. -fno-direct-access-external-data can avoid the
1716 // canonical PLT entry. We don't generalize this condition to -fpie/-fpic as
1729 GV->setDSOLocal(shouldAssumeDSOLocal(*this, GV));
1745 if (D && D->isExternallyVisible()) {
1746 if (D->hasAttr<DLLImportAttr>())
1747 GV->setDLLStorageClass(llvm::GlobalVariable::DLLImportStorageClass);
1748 else if ((D->hasAttr<DLLExportAttr>() ||
1750 !GV->isDeclarationForLinker())
1751 GV->setDLLStorageClass(llvm::GlobalVariable::DLLExportStorageClass);
1771 GV->setPartition(CodeGenOpts.SymbolPartition);
1776 .Case("global-dynamic", llvm::GlobalVariable::GeneralDynamicTLSModel)
1777 .Case("local-dynamic", llvm::GlobalVariable::LocalDynamicTLSModel)
1778 .Case("initial-exec", llvm::GlobalVariable::InitialExecTLSModel)
1779 .Case("local-exec", llvm::GlobalVariable::LocalExecTLSModel);
1798 assert(D.getTLSKind() && "setting TLS mode on non-TLS var!");
1805 TLM = GetLLVMTLSModel(Attr->getModel());
1808 GV->setThreadLocalMode(TLM);
1824 Out << getCPUSpecificMangling(CGM, Attr->getCPUName(CPUIndex)->getName());
1850 IdentifierInfo *II = ND->getIdentifier();
1855 FD->getType()->castAs<FunctionType>()->getCallConv() == CC_X86RegCall) {
1857 Out << "__regcall4__" << II->getName();
1859 Out << "__regcall3__" << II->getName();
1860 } else if (FD && FD->hasAttr<CUDAGlobalAttr>() &&
1862 Out << "__device_stub__" << II->getName();
1864 Out << II->getName();
1869 // symbols. This should come before multi-version target suffixes are
1883 if (FD->isMultiVersion() && !OmitMultiVersionMangling) {
1884 switch (FD->getMultiVersionKind()) {
1888 FD->getAttr<CPUSpecificAttr>(),
1892 auto *Attr = FD->getAttr<TargetAttr>();
1900 auto *Attr = FD->getAttr<TargetVersionAttr>();
1908 auto *Attr = FD->getAttr<TargetClonesAttr>();
1921 // Make unique name for device side static file-scope variable for HIP.
1933 if (!FD->isMultiVersion())
1945 ->getAsFunction()
1946 ->isMultiVersion() &&
1952 ->getAsFunction()
1953 ->getMostRecentDecl();
1965 Result.first->first();
1970 Entry->setName(OtherName);
1989 // In CUDA/HIP device compilation with -fgpu-rdc, the mangled name of a
1996 return FoundName->second;
2007 // very important as the device kernel name being mangled in host-compilation
2010 // directly between host- and device-compilations, the host- and
2011 // device-mangling in host compilation could help catching certain ones.
2012 assert(!isa<FunctionDecl>(ND) || !ND->hasAttr<CUDAGlobalAttr>() ||
2015 (getContext().getAuxTargetInfo()->getCXXABI() !=
2024 return MangledDeclNames[CanonicalGD] = Result.first->first();
2045 return Result.first->first();
2051 if (it->second == Name)
2052 return it->first;
2062 /// AddGlobalCtor - Add a function to the list that will be called before
2071 /// AddGlobalDtor - Add a function to the list that will be called
2118 list->setAlignment(std::nullopt);
2139 return llvm::ConstantInt::get(Int64Ty, llvm::MD5Hash(MDS->getString()));
2143 if (auto *FnType = T->getAs<FunctionProtoType>())
2145 FnType->getReturnType(), FnType->getParamTypes(),
2146 FnType->getExtProtoInfo().withExceptionSpec(EST_None));
2165 ConstructAttributeList(F->getName(), Info, GD, PAL, CallingConv,
2171 Loc = D->getLocation();
2175 F->setAttributes(PAL);
2176 F->setCallingConv(static_cast<llvm::CallingConv::ID>(CallingConv));
2228 "Incorrect use - FD and CGF should either be both null or not!");
2254 for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) {
2255 const ParmVarDecl *parm = FD->getParamDecl(i);
2257 argNames.push_back(llvm::MDString::get(VMContext, parm->getName()));
2261 QualType ty = parm->getType();
2265 if (ty->isImageType() || ty->isPipeType()) {
2267 if (const auto *TD = ty->getAs<TypedefType>())
2268 PDecl = TD->getDecl();
2269 const OpenCLAccessAttr *A = PDecl->getAttr<OpenCLAccessAttr>();
2270 if (A && A->isWriteOnly())
2272 else if (A && A->isReadWrite())
2294 if (ty->isPointerType()) {
2295 QualType pointeeTy = ty->getPointeeType();
2299 llvm::ConstantAsMetadata::get(CGF->Builder.getInt32(
2320 bool isPipe = ty->isPipeType();
2321 if (ty->isImageType() || isPipe)
2325 llvm::ConstantAsMetadata::get(CGF->Builder.getInt32(AddrSpc)));
2328 ty = isPipe ? ty->castAs<PipeType>()->getElementType() : ty;
2336 if (ty->isImageType()) {
2352 Fn->setMetadata("kernel_arg_addr_space",
2354 Fn->setMetadata("kernel_arg_access_qual",
2356 Fn->setMetadata("kernel_arg_type",
2358 Fn->setMetadata("kernel_arg_base_type",
2360 Fn->setMetadata("kernel_arg_type_qual",
2365 Fn->setMetadata("kernel_arg_name",
2370 /// unwind exceptions. We treat -fexceptions as mandating this
2372 /// enabled. This means, for example, that C with -fexceptions
2393 !CGM.HasHiddenLTOVisibility(MD->getParent()))
2398 return MD->isImplicitObjectMemberFunction() && !MD->isVirtual() &&
2408 if (RD->getNumBases() == 0)
2410 for (const CXXBaseSpecifier &B : RD->bases())
2411 CollectMostBases(B.getType()->getAsCXXRecordDecl());
2419 llvm::AttrBuilder B(F->getContext());
2421 if ((!D || !D->hasAttr<NoUwtableAttr>()) && CodeGenOpts.UnwindTables)
2425 B.addAttribute("probe-stack", "inline-asm");
2428 B.addAttribute("stack-probe-size",
2434 if (D && D->hasAttr<NoStackProtectorAttr>())
2436 else if (D && D->hasAttr<StrictGuardStackCheckAttr>() &&
2450 if (!F->hasFnAttribute(llvm::Attribute::AlwaysInline) &&
2454 F->addFnAttrs(B);
2460 if (D->hasAttr<ArmLocallyStreamingAttr>())
2463 if (auto *Attr = D->getAttr<ArmNewAttr>()) {
2464 if (Attr->isNewZA())
2466 if (Attr->isNewZT0())
2475 ShouldAddOptNone &= !D->hasAttr<MinSizeAttr>();
2476 ShouldAddOptNone &= !D->hasAttr<AlwaysInlineAttr>();
2479 if ((ShouldAddOptNone || D->hasAttr<OptimizeNoneAttr>()) &&
2480 !F->hasFnAttribute(llvm::Attribute::AlwaysInline)) {
2488 if (D->hasAttr<NakedAttr>())
2492 F->removeFnAttr(llvm::Attribute::OptimizeForSize);
2493 F->removeFnAttr(llvm::Attribute::MinSize);
2494 } else if (D->hasAttr<NakedAttr>()) {
2498 } else if (D->hasAttr<NoDuplicateAttr>()) {
2500 } else if (D->hasAttr<NoInlineAttr>() && !F->hasFnAttribute(llvm::Attribute::AlwaysInline)) {
2503 } else if (D->hasAttr<AlwaysInlineAttr>() &&
2504 !F->hasFnAttribute(llvm::Attribute::NoInline)) {
2510 if (!F->hasFnAttribute(llvm::Attribute::AlwaysInline))
2519 return Redecl->isInlineSpecified();
2521 if (any_of(FD->redecls(), CheckRedeclForInline))
2523 const FunctionDecl *Pattern = FD->getTemplateInstantiationPattern();
2526 return any_of(Pattern->redecls(), CheckRedeclForInline);
2532 !FD->isInlined() &&
2533 !F->hasFnAttribute(llvm::Attribute::AlwaysInline)) {
2541 if (!D->hasAttr<OptimizeNoneAttr>()) {
2542 if (D->hasAttr<ColdAttr>()) {
2547 if (D->hasAttr<HotAttr>())
2549 if (D->hasAttr<MinSizeAttr>())
2553 F->addFnAttrs(B);
2555 unsigned alignment = D->getMaxAlignment() / Context.getCharWidth();
2557 F->setAlignment(llvm::Align(alignment));
2559 if (!D->hasAttr<AlignedAttr>())
2561 F->setAlignment(llvm::Align(1ull << LangOpts.FunctionAlignment));
2563 // Some C++ ABIs require 2-byte alignment for member functions, in order to
2564 // reserve a bit for differentiating between virtual and non-virtual member
2568 if (isa<CXXMethodDecl>(D) && F->getPointerAlignment(getDataLayout()) < 2)
2569 F->setAlignment(std::max(llvm::Align(2), F->getAlign().valueOrOne()));
2572 // In the cross-dso CFI mode with canonical jump tables, we want !type
2589 for (const CXXRecordDecl *Base : getMostBaseClasses(MD->getParent())) {
2592 MD->getType(), Context.getRecordType(Base).getTypePtr()));
2593 F->addTypeMetadata(0, Id);
2603 GV->setVisibility(llvm::GlobalValue::DefaultVisibility);
2605 if (D && D->hasAttr<UsedAttr>())
2611 (VD->getStorageDuration() == SD_Static ||
2612 VD->getStorageDuration() == SD_Thread)) ||
2613 (CodeGenOpts.KeepStaticConsts && VD->getStorageDuration() == SD_Static &&
2614 VD->getType().isConstQualified())))
2621 // Add target-cpu and target-features attributes to functions. If
2628 FD = FD ? FD->getMostRecentDecl() : FD;
2629 const auto *TD = FD ? FD->getAttr<TargetAttr>() : nullptr;
2630 const auto *TV = FD ? FD->getAttr<TargetVersionAttr>() : nullptr;
2632 const auto *SD = FD ? FD->getAttr<CPUSpecificAttr>() : nullptr;
2633 const auto *TC = FD ? FD->getAttr<TargetClonesAttr>() : nullptr;
2641 Features.push_back((Entry.getValue() ? "+" : "-") + Entry.getKey().str());
2643 // Now add the target-cpu and target-features to the function.
2649 Target.parseTargetAttr(TD->getFeaturesStr());
2661 // Apply the given CPU name as the 'tune-cpu' so that the optimizer can
2663 TuneCPU = SD->getCPUName(GD.getMultiVersionIndex())->getName();
2672 Attrs.addAttribute("target-cpu", TargetCPU);
2676 Attrs.addAttribute("tune-cpu", TuneCPU);
2684 Attrs.addAttribute("target-features", llvm::join(Features, ","));
2698 if (D->hasAttr<RetainAttr>())
2700 if (auto *SA = D->getAttr<PragmaClangBSSSectionAttr>())
2701 GV->addAttribute("bss-section", SA->getName());
2702 if (auto *SA = D->getAttr<PragmaClangDataSectionAttr>())
2703 GV->addAttribute("data-section", SA->getName());
2704 if (auto *SA = D->getAttr<PragmaClangRodataSectionAttr>())
2705 GV->addAttribute("rodata-section", SA->getName());
2706 if (auto *SA = D->getAttr<PragmaClangRelroSectionAttr>())
2707 GV->addAttribute("relro-section", SA->getName());
2711 if (D->hasAttr<RetainAttr>())
2713 if (auto *SA = D->getAttr<PragmaClangTextSectionAttr>())
2714 if (!D->getAttr<SectionAttr>())
2715 F->setSection(SA->getName());
2717 llvm::AttrBuilder Attrs(F->getContext());
2723 RemoveAttrs.addAttribute("target-cpu");
2724 RemoveAttrs.addAttribute("target-features");
2725 RemoveAttrs.addAttribute("tune-cpu");
2726 F->removeFnAttrs(RemoveAttrs);
2727 F->addFnAttrs(Attrs);
2731 if (const auto *CSA = D->getAttr<CodeSegAttr>())
2732 GO->setSection(CSA->getName());
2733 else if (const auto *SA = D->getAttr<SectionAttr>())
2734 GO->setSection(SA->getName());
2747 F->setLinkage(llvm::Function::InternalLinkage);
2754 LinkageInfo LV = ND->getLinkageAndVisibility();
2759 (ND->hasAttr<WeakAttr>() || ND->isWeakImported()))
2760 GV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
2769 // Non-static class methods are handled via vtable or member function pointer
2771 if (isa<CXXMethodDecl>(FD) && !cast<CXXMethodDecl>(FD)->isStatic())
2774 llvm::Metadata *MD = CreateMetadataIdentifierForType(FD->getType());
2775 F->addTypeMetadata(0, MD);
2776 F->addTypeMetadata(0, CreateMetadataIdentifierGeneralized(FD->getType()));
2778 // Emit a hash-based bit set entry for cross-DSO calls.
2781 F->addTypeMetadata(0, llvm::ConstantAsMetadata::get(CrossDsoTypeId));
2785 llvm::LLVMContext &Ctx = F->getContext();
2787 F->setMetadata(llvm::LLVMContext::MD_kcfi_type,
2789 Ctx, MDB.createConstant(CreateKCFITypeId(FD->getType()))));
2804 // Remove KCFI type metadata from non-address-taken local functions.
2810 // address-taken function declarations to support annotating indirectly
2817 Type = llvm::mdconst::extract<llvm::ConstantInt>(MD->getOperand(0));
2826 Name + ", " + Twine(Type->getZExtValue()) + "\n")
2836 if (llvm::Intrinsic::ID IID = F->getIntrinsicID()) {
2839 F->setAttributes(llvm::Intrinsic::getAttributes(getLLVMContext(), IID));
2854 assert(!F->arg_empty() &&
2855 F->arg_begin()->getType()
2856 ->canLosslesslyBitCastTo(F->getReturnType()) &&
2858 F->addParamAttr(0, llvm::Attribute::Returned);
2867 // Setup target-specific attributes.
2868 if (!IsIncompleteFunction && F->isDeclaration())
2871 if (const auto *CSA = FD->getAttr<CodeSegAttr>())
2872 F->setSection(CSA->getName());
2873 else if (const auto *SA = FD->getAttr<SectionAttr>())
2874 F->setSection(SA->getName());
2876 if (const auto *EA = FD->getAttr<ErrorAttr>()) {
2877 if (EA->isError())
2878 F->addFnAttr("dontcall-error", EA->getUserDiagnostic());
2879 else if (EA->isWarning())
2880 F->addFnAttr("dontcall-warn", EA->getUserDiagnostic());
2884 if (FD->isInlineBuiltinDeclaration()) {
2886 bool HasBody = FD->hasBody(FDBody);
2891 F->addFnAttr(llvm::Attribute::NoBuiltin);
2894 if (FD->isReplaceableGlobalAllocationFunction()) {
2896 // default, only if it is invoked by a new-expression or delete-expression.
2897 F->addFnAttr(llvm::Attribute::NoBuiltin);
2901 F->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
2903 if (MD->isVirtual())
2904 F->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
2906 // Don't emit entries for function declarations in the cross-DSO mode. This
2908 // are non-canonical then we need type metadata in order to produce the local
2917 if (getLangOpts().OpenMP && FD->hasAttr<OMPDeclareSimdDeclAttr>())
2921 F->addFnAttr("inline-max-stacksize", llvm::utostr(CodeGenOpts.InlineMaxStackSize));
2923 if (const auto *CB = FD->getAttr<CallbackAttr>()) {
2925 // - The callback callee (as argument number).
2926 // - The callback payloads (as argument numbers).
2927 llvm::LLVMContext &Ctx = F->getContext();
2932 int CalleeIdx = *CB->encoding_begin();
2933 ArrayRef<int> PayloadIndices(CB->encoding_begin() + 1, CB->encoding_end());
2934 F->addMetadata(llvm::LLVMContext::MD_callback,
2942 assert((isa<llvm::Function>(GV) || !GV->isDeclaration()) &&
2948 assert(!GV->isDeclaration() &&
2954 assert((isa<llvm::Function>(GV) || !GV->isDeclaration()) &&
2985 GV->setSection("llvm.metadata");
3027 if (Mod->Parent && Visited.insert(Mod->Parent).second) {
3028 addLinkOptionsPostorder(CGM, Mod->Parent, Metadata, Visited);
3032 for (Module *Import : llvm::reverse(Mod->Imports)) {
3044 if (Mod->UseExportAsModuleLinkName)
3047 for (const Module::LinkLibrary &LL : llvm::reverse(Mod->LinkLibraries)) {
3051 llvm::Metadata *Args[2] = {llvm::MDString::get(Context, "-framework"),
3075 assert(Primary->isNamedModuleUnit() &&
3078 // Emit the initializers in the order that sub-modules appear in the
3080 if (auto GMF = Primary->getGlobalModuleFragment()) {
3096 if (auto PMF = Primary->getPrivateModuleFragment()) {
3110 // non-explicit child modules.
3119 if (M->getTopLevelModuleName() == getLangOpts().CurrentModule &&
3127 // non-leaf modules.
3134 for (const auto &SM : Mod->submodules()) {
3137 if (SM->IsExplicit)
3167 NMD->addOperand(MD);
3218 // Make sure GetGlobalValue returned non-null.
3223 // deferred-decls queue multiple times, and second, decls can end
3227 if (!GV->isDeclaration())
3231 if (LangOpts.OpenMP && OpenMPRuntime && OpenMPRuntime->emitTargetGlobal(D))
3278 Annotations[0]->getType(), Annotations.size()), Annotations);
3279 auto *gv = new llvm::GlobalVariable(getModule(), Array->getType(), false,
3282 gv->setSection(AnnotationSection);
3293 getModule(), s->getType(), true, llvm::GlobalValue::PrivateLinkage, s,
3295 ConstGlobalsPtrTy->getAddressSpace());
3296 gv->setSection(AnnotationSection);
3297 gv->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
3319 ArrayRef<Expr *> Exprs = {Attr->args_begin(), Attr->args_size()};
3325 ID.Add(cast<clang::ConstantExpr>(E)->getAPValueResult());
3336 return ConstEmiter.emitAbstract(CE->getBeginLoc(), CE->getAPValueResult(),
3337 CE->getType());
3340 auto *GV = new llvm::GlobalVariable(getModule(), Struct->getType(), true,
3343 GV->setSection(AnnotationSection);
3344 GV->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
3354 llvm::Constant *AnnoGV = EmitAnnotationString(AA->getAnnotation()),
3360 if (GV->getAddressSpace() !=
3365 GV->getContext(), getDataLayout().getDefaultGlobalsAddressSpace()));
3377 assert(D->hasAttr<AnnotateAttr>() && "no annotate attribute");
3379 for (const auto *I : D->specific_attrs<AnnotateAttr>())
3380 Annotations.push_back(EmitAnnotateAttr(GV, I, D->getLocation()));
3387 if (NoSanitizeL.containsFunction(Kind, Fn->getName()))
3398 // If location is unknown, this may be a compiler-generated function. Assume
3408 if (NoSanitizeL.containsGlobal(Kind, GV->getName(), Category))
3412 Kind, SM.getFileEntryRefForID(SM.getMainFileID())->getName(),
3423 Ty = AT->getElementType();
3426 if (Ty->isRecordType()) {
3443 Attr = XRayFilter.shouldImbueFunction(Fn->getName());
3448 Fn->addFnAttr("function-instrument", "xray-always");
3451 Fn->addFnAttr("function-instrument", "xray-always");
3452 Fn->addFnAttr("xray-log-args", "1");
3455 Fn->addFnAttr("function-instrument", "xray-never");
3470 if (auto V = ProfileList.isFunctionExcluded(Fn->getName(), Kind))
3476 // If location is unknown, this may be a compiler-generated function. Assume
3480 if (auto V = ProfileList.isFileExcluded(MainFile->getName(), Kind))
3494 auto Group = llvm::crc32(arrayRefFromStringRef(Fn->getName())) % NumGroups;
3509 (VD->getStorageDuration() == SD_Static ||
3510 VD->getStorageDuration() == SD_Thread)) ||
3511 (CodeGenOpts.KeepStaticConsts && VD->getStorageDuration() == SD_Static &&
3512 VD->getType().isConstQualified())))
3523 // we have if the level of the declare target attribute is -1. Note that we
3528 if (!ActiveAttr || (*ActiveAttr)->getLevel() != (unsigned)-1)
3533 if (FD->getTemplateSpecializationKind() == TSK_ImplicitInstantiation)
3538 if (FD->hasAttr<TargetVersionAttr>() && !FD->isMultiVersion())
3547 if (CXX20ModuleInits && VD->getOwningModule() &&
3548 !VD->getOwningModule()->isModuleMapModule()) {
3549 // For CXX20, module-owned initializers need to be deferred, since it is
3559 !Global->getType().isConstantStorage(getContext(), false, false) &&
3574 return ConstantAddress(GV, GV->getValueType(), Alignment);
3579 APValue &V = GD->getAsAPValue();
3584 GD->getAsAPValue(), GD->getType().getAddressSpace(), GD->getType());
3590 MSGuidDecl::Parts Parts = GD->getParts();
3592 llvm::ConstantInt::get(Int32Ty, Parts.Part1),
3602 getModule(), Init->getType(),
3605 GV->setComdat(TheModule.getOrInsertComdat(GV->getName()));
3610 return ConstantAddress(GV, GV->getValueType(), Alignment);
3613 llvm::Type *Ty = getTypes().ConvertTypeForMem(GD->getType());
3619 CharUnits Alignment = getContext().getTypeAlignInChars(GCD->getType());
3624 return ConstantAddress(*Entry, (*Entry)->getValueType(), Alignment);
3629 const APValue &V = GCD->getValue();
3632 Init = Emitter.emitForInitializer(V, GCD->getType().getAddressSpace(),
3633 GCD->getType());
3635 auto *GV = new llvm::GlobalVariable(getModule(), Init->getType(),
3639 GV->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
3640 GV->setAlignment(Alignment.getAsAlign());
3645 return ConstantAddress(GV, GV->getValueType(), Alignment);
3651 CharUnits Alignment = getNaturalTypeAlignment(TPO->getType());
3654 return ConstantAddress(GV, GV->getValueType(), Alignment);
3658 TPO->getValue(), TPO->getType().getAddressSpace(), TPO->getType());
3666 isExternallyVisible(TPO->getLinkageAndVisibility().getLinkage())
3669 auto *GV = new llvm::GlobalVariable(getModule(), Init->getType(),
3673 GV->setComdat(TheModule.getOrInsertComdat(GV->getName()));
3676 return ConstantAddress(GV, GV->getValueType(), Alignment);
3680 const AliasAttr *AA = VD->getAttr<AliasAttr>();
3684 llvm::Type *DeclTy = getTypes().ConvertTypeForMem(VD->getType());
3687 llvm::GlobalValue *Entry = GetGlobalValue(AA->getAliasee());
3693 Aliasee = GetOrCreateLLVMFunction(AA->getAliasee(), DeclTy,
3697 Aliasee = GetOrCreateLLVMGlobal(AA->getAliasee(), DeclTy, LangAS::Default,
3701 F->setLinkage(llvm::Function::ExternalWeakLinkage);
3710 if (auto *A = D->getAttr<AttrT>())
3711 return A->isImplicit();
3712 return D->isImplicit();
3716 assert(LangOpts.CUDA && "Should not be called by non-CUDA languages");
3717 // We need to emit host-side 'shadows' for all global
3718 // device-side variables because the CUDA runtime needs their
3719 // size and host-side address in order to provide access to
3720 // their device-side incarnations.
3721 return !LangOpts.CUDAIsDevice || Global->hasAttr<CUDADeviceAttr>() ||
3722 Global->hasAttr<CUDAConstantAttr>() ||
3723 Global->hasAttr<CUDASharedAttr>() ||
3724 Global->getType()->isCUDADeviceBuiltinSurfaceType() ||
3725 Global->getType()->isCUDADeviceBuiltinTextureType();
3732 if (Global->hasAttr<WeakRefAttr>())
3737 if (Global->hasAttr<AliasAttr>())
3741 if (Global->hasAttr<IFuncAttr>())
3745 if (Global->hasAttr<CPUDispatchAttr>())
3749 // Non-constexpr non-lambda implicit host device functions are not emitted
3759 if ((!Global->hasAttr<CUDADeviceAttr>() ||
3762 hasImplicitAttr<CUDADeviceAttr>(FD) && !FD->isConstexpr() &&
3765 !Global->hasAttr<CUDAGlobalAttr>() &&
3767 !Global->hasAttr<CUDAHostAttr>()))
3769 // Device-only functions are the only things we skip.
3770 } else if (!Global->hasAttr<CUDAHostAttr>() &&
3771 Global->hasAttr<CUDADeviceAttr>())
3777 if (OpenMPRuntime && OpenMPRuntime->emitTargetGlobal(GD))
3795 if (FD->hasAttr<AnnotateAttr>()) {
3802 if (!FD->doesThisDeclarationHaveABody()) {
3803 if (!FD->doesDeclarationForceExternallyVisibleDefinition() &&
3804 (!FD->isMultiVersion() || !getTarget().getTriple().isAArch64()))
3819 assert(VD->isFileVarDecl() && "Cannot emit local var decl as global.");
3820 if (VD->isThisDeclarationADefinition() != VarDecl::Definition &&
3823 // Emit declaration of the must-be-emitted declare target variable.
3829 if (VD->hasExternalStorage() &&
3873 cast<VarDecl>(Global)->hasInit()) {
3896 if (const auto *RT = T->getBaseElementTypeUnsafe()->getAs<RecordType>())
3897 if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl()))
3898 if (RD->getDestructor() && !RD->getDestructor()->hasAttr<DLLImportAttr>())
3913 const FunctionDecl *FD = E->getDirectCallee();
3916 AsmLabelAttr *Attr = FD->getAttr<AsmLabelAttr>();
3917 if (Attr && Name == Attr->getLabel())
3919 unsigned BuiltinID = FD->getBuiltinID();
3931 for (const Stmt *Child : S->children())
3932 if (Child && this->Visit(Child))
3938 // Make sure we're not referencing non-imported vars or functions.
3946 if (VD->getTLSKind()) {
3947 // A thread-local variable cannot be imported.
3953 if (VD->isThisDeclarationADefinition())
3954 SafeToInline = !HasNonDllImportDtor(VD->getType());
3960 if (const auto *D = E->getTemporary()->getDestructor())
3961 SafeToInline = D->hasAttr<DLLImportAttr>();
3966 ValueDecl *VD = E->getDecl();
3968 SafeToInline = VD->hasAttr<DLLImportAttr>();
3970 SafeToInline = !V->hasGlobalStorage() || V->hasAttr<DLLImportAttr>();
3975 SafeToInline = E->getConstructor()->hasAttr<DLLImportAttr>();
3980 CXXMethodDecl *M = E->getMethodDecl();
3985 SafeToInline = M->hasAttr<DLLImportAttr>();
3991 SafeToInline = E->getOperatorDelete()->hasAttr<DLLImportAttr>();
3996 SafeToInline = E->getOperatorNew()->hasAttr<DLLImportAttr>();
4002 // isTriviallyRecursive - Check if this function calls another
4010 AsmLabelAttr *Attr = FD->getAttr<AsmLabelAttr>();
4013 Name = Attr->getLabel();
4015 Name = FD->getName();
4019 const Stmt *Body = FD->getBody();
4028 if (CodeGenOpts.OptimizationLevel == 0 && !F->hasAttr<AlwaysInlineAttr>())
4033 if (const Module *M = F->getOwningModule();
4034 M && M->getTopLevelModule()->isNamedModule() &&
4035 getContext().getCurrentNamedModule() != M->getTopLevelModule()) {
4036 // There are practices to mark template member function as always-inline
4039 // from explicitly instantiation with always-inline.
4041 // See https://github.com/llvm/llvm-project/issues/86893 for details.
4043 // TODO: Maybe it is better to give it a warning if we call a non-inline
4044 // function from other module units which is marked as always-inline.
4045 if (!F->isTemplateInstantiation() || !F->hasAttr<AlwaysInlineAttr>()) {
4050 if (F->hasAttr<NoInlineAttr>())
4053 if (F->hasAttr<DLLImportAttr>() && !F->hasAttr<AlwaysInlineAttr>()) {
4063 for (const Decl *Member : Dtor->getParent()->decls())
4065 if (HasNonDllImportDtor(cast<FieldDecl>(Member)->getType()))
4067 for (const CXXBaseSpecifier &B : Dtor->getParent()->bases())
4075 if (F->isInlineBuiltinDeclaration())
4094 if (FD->isCPUSpecificMultiVersion()) {
4095 auto *Spec = FD->getAttr<CPUSpecificAttr>();
4096 for (unsigned I = 0; I < Spec->cpus_size(); ++I)
4098 } else if (auto *TC = FD->getAttr<TargetClonesAttr>()) {
4099 for (unsigned I = 0; I < TC->featuresStrs_size(); ++I)
4101 if ((!TC->isDefaultVersion(I) || !getTarget().getTriple().isAArch64()) &&
4102 TC->isFirstOfVersion(I))
4111 if (FD->isTargetVersionMultiVersion())
4118 PrettyStackTraceDecl CrashInfo(const_cast<ValueDecl *>(D), D->getLocation(),
4123 // At -O0, don't generate IR for functions with available_externally
4131 FD->getNameForDiagnostic(OS, getContext().getPrintingPolicy(),
4140 ABI->emitCXXStructor(GD);
4141 else if (FD->isMultiVersion())
4146 if (Method->isVirtual())
4152 if (FD->isMultiVersion())
4158 return EmitGlobalVarDefinition(VD, !VD->hasDefinition());
4193 if (FD->getFormalLinkage() == Linkage::Internal)
4206 GlobalDecl CurGD{Decl->isDefined() ? Decl->getDefinition() : Decl, MVIdx};
4210 if (Decl->isDefined()) {
4233 bool IsDefined = CurFD->doesThisDeclarationHaveABody();
4235 if (const auto *TA = CurFD->getAttr<TargetAttr>()) {
4236 TA->getAddedFeatures(Feats);
4238 Options.emplace_back(Func, TA->getArchitecture(), Feats);
4239 } else if (const auto *TVA = CurFD->getAttr<TargetVersionAttr>()) {
4240 if (TVA->isDefaultVersion() && IsDefined)
4242 TVA->getFeatures(Feats);
4245 } else if (const auto *TC = CurFD->getAttr<TargetClonesAttr>()) {
4248 for (unsigned I = 0; I < TC->featuresStrs_size(); ++I) {
4249 if (!TC->isFirstOfVersion(I))
4256 TC->getFeatures(Feats, I);
4258 StringRef Version = TC->getFeatureStr(I);
4260 Architecture = Version.drop_front(sizeof("arch=") - 1);
4275 ResolverConstant = IFunc->getResolver();
4276 if (FD->isTargetClonesMultiVersion() &&
4295 ResolverFunc->setLinkage(getMultiversionLinkage(*this, GD));
4297 if (!ResolverFunc->hasLocalLinkage() && supportsCOMDAT())
4298 ResolverFunc->setComdat(
4299 getModule().getOrInsertComdat(ResolverFunc->getName()));
4325 assert(cast<llvm::Function>(Old)->isDeclaration() && "Not a declaration");
4326 New->takeName(Old);
4327 Old->replaceAllUsesWith(New);
4328 Old->eraseFromParent();
4334 assert(FD->isCPUDispatchMultiVersion() && "Not a multiversion function?");
4335 const auto *DD = FD->getAttr<CPUDispatchAttr>();
4349 getTypes().getTargetAddressSpace(FD->getType())),
4359 ResolverFunc->setLinkage(getMultiversionLinkage(*this, GD));
4361 ResolverFunc->setComdat(
4362 getModule().getOrInsertComdat(ResolverFunc->getName()));
4367 for (const IdentifierInfo *II : DD->cpus()) {
4370 getCPUSpecificMangling(*this, II->getName());
4377 ExistingDecl.getDecl()->getAsFunction()->isDefined()) {
4392 Target.getCPUSpecificCPUDispatchFeatures(II->getName(), Features);
4415 (Options.end() - 2)->Conditions.Features),
4417 StringRef LHSName = (Options.end() - 2)->Function->getName();
4418 StringRef RHSName = (Options.end() - 1)->Function->getName();
4420 Options.erase(Options.end() - 2);
4422 Options.erase(Options.end() - 1);
4457 if (FD->isTargetVersionMultiVersion() || FD->isTargetClonesMultiVersion()) {
4480 switch (FD->getMultiVersionKind()) {
4492 } else if (FD->isTargetMultiVersion()) {
4512 if (FD->isTargetMultiVersion() || FD->isTargetClonesMultiVersion())
4517 if (getTarget().supportsIFunc() && !FD->isCPUSpecificMultiVersion()) {
4520 getTypes().getTargetAddressSpace(FD->getType())),
4528 GIF->setName(ResolverName);
4547 auto SC = GV->getDLLStorageClass();
4550 const Decl *MRD = D->getMostRecentDecl();
4552 !MRD->hasAttr<DLLImportAttr>()) ||
4554 !MRD->hasAttr<DLLExportAttr>())) &&
4558 /// GetOrCreateLLVMFunction - If the specified mangled name is not in the
4563 /// If D is non-null, it specifies a decl that correspond to this. This is used
4577 !OpenMPRuntime->markAsGlobalTarget(GD) && FD->isDefined() &&
4579 if (const FunctionDecl *FDDef = FD->getDefinition()) {
4591 if (FD->isMultiVersion()) {
4616 if (FD && !FD->hasAttr<WeakAttr>())
4617 Entry->setLinkage(llvm::Function::ExternalLinkage);
4622 Entry->setDLLStorageClass(llvm::GlobalValue::DefaultStorageClass);
4628 if (IsForDefinition && !Entry->isDeclaration()) {
4636 getDiags().Report(D->getLocation(), diag::err_duplicate_mangled_name)
4638 getDiags().Report(OtherGD.getDecl()->getLocation(),
4644 (Entry->getValueType() == Ty)) {
4674 if (D && D->hasAttr<AnnotateAttr>())
4684 F->takeName(Entry);
4692 if (!Entry->use_empty()) {
4694 Entry->removeDeadConstantUsers();
4700 assert(F->getName() == MangledName && "name was uniqued!");
4704 llvm::AttrBuilder B(F->getContext(), ExtraAttrs.getFnAttrs());
4705 F->addFnAttrs(B);
4710 // each other bottoming out with the base dtor. Therefore we emit non-base
4725 addDeferredDeclToEmit(DDI->second);
4730 // we might not find a top-level definition:
4731 // - member functions defined inline in their classes
4732 // - friend functions defined inline in some class
4733 // - special member functions with implicit definitions
4741 for (const auto *FD = cast<FunctionDecl>(D)->getMostRecentDecl(); FD;
4742 FD = FD->getPreviousDecl()) {
4743 if (isa<CXXRecordDecl>(FD->getLexicalDeclContext())) {
4744 if (FD->doesThisDeclarationHaveABody()) {
4755 assert(F->getFunctionType() == Ty);
4762 /// GetAddrOfFunction - Return the address of the given function. If Ty is
4763 /// non-null, then this function will use the specified type if it has to
4772 Ty = getTypes().ConvertType(FD->getType());
4781 DD->getParent()->getNumVBases() == 0)
4791 cast<FunctionDecl>(GD.getDecl())->hasAttr<CUDAGlobalAttr>()) {
4793 cast<llvm::Function>(F->stripPointerCasts()), GD);
4803 cast<llvm::GlobalValue>(GetAddrOfFunction(Decl)->stripPointerCasts());
4814 for (const auto *Result : DC->lookup(&CII))
4829 for (const auto *Result : DC->lookup(&NS)) {
4832 for (const auto *Result : LSD->lookup(&NS))
4837 for (const auto *Result : ND->lookup(&CXXII))
4846 /// CreateRuntimeFunction - Create a new runtime function with the specified
4863 if (F->empty()) {
4864 F->setCallingConv(getRuntimeCC());
4874 if (!FD || FD->hasAttr<DLLImportAttr>()) {
4875 F->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass);
4876 F->setLinkage(llvm::GlobalValue::ExternalLinkage);
4890 /// GetOrCreateLLVMGlobal - If the specified mangled name is not in the module,
4895 /// If D is non-null, it specifies a decl that correspond to this. This is used
4910 if (D && !D->hasAttr<WeakAttr>())
4911 Entry->setLinkage(llvm::Function::ExternalLinkage);
4916 Entry->setDLLStorageClass(llvm::GlobalValue::DefaultStorageClass);
4921 if (Entry->getValueType() == Ty && Entry->getAddressSpace() == TargetAS)
4926 if (IsForDefinition && !Entry->isDeclaration()) {
4933 (D->getCanonicalDecl() != OtherGD.getCanonicalDecl().getDecl()) &&
4935 OtherD->hasInit() &&
4937 getDiags().Report(D->getLocation(), diag::err_duplicate_mangled_name)
4939 getDiags().Report(OtherGD.getDecl()->getLocation(),
4945 if (Entry->getType()->getAddressSpace() != TargetAS)
4947 Entry, llvm::PointerType::get(Ty->getContext(), TargetAS));
4965 GV->takeName(Entry);
4967 if (!Entry->use_empty()) {
4968 Entry->replaceAllUsesWith(GV);
4971 Entry->eraseFromParent();
4981 addDeferredDeclToEmit(DDI->second);
4992 GV->setConstant(D->getType().isConstantStorage(getContext(), false, false));
4994 GV->setAlignment(getContext().getDeclAlign(D).getAsAlign());
4998 if (D->getTLSKind()) {
4999 if (D->getTLSKind() == VarDecl::TLS_Dynamic)
5013 if (D->hasExternalStorage()) {
5014 if (const SectionAttr *SA = D->getAttr<SectionAttr>())
5015 GV->setSection(SA->getName());
5020 D->getLanguageLinkage() == CLanguageLinkage &&
5021 D->getType().isConstant(Context) &&
5022 isExternallyVisible(D->getLinkageAndVisibility().getLinkage()))
5023 GV->setSection(".cp.rodata");
5026 if (const auto *CMA = D->getAttr<CodeModelAttr>())
5027 GV->setCodeModel(CMA->getModel());
5032 if (Context.getLangOpts().CPlusPlus && GV->hasExternalLinkage() &&
5033 D->getType().isConstQualified() && !GV->hasInitializer() &&
5034 !D->hasDefinition() && D->hasInit() && !D->hasAttr<DLLImportAttr>()) {
5036 Context.getBaseElementType(D->getType())->getAsCXXRecordDecl();
5037 bool HasMutableFields = Record && Record->hasMutableFields();
5040 const Expr *InitExpr = D->getAnyInitializer(InitDecl);
5045 auto *InitType = Init->getType();
5046 if (GV->getValueType() != InitType) {
5051 GV->setName(StringRef());
5056 ->stripPointerCasts());
5059 GV->eraseFromParent();
5062 GV->setInitializer(Init);
5063 GV->setConstant(true);
5064 GV->setLinkage(llvm::GlobalValue::AvailableExternallyLinkage);
5074 D->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly) {
5078 if (getLangOpts().CUDA && D && D->hasAttr<HIPManagedAttr>() &&
5079 D->hasExternalStorage())
5084 SanitizerMD->reportGlobal(GV, *D);
5087 D ? D->getType().getAddressSpace()
5133 if (GV->getValueType() == Ty)
5138 assert(GV->isDeclaration() && "Declaration has wrong type!");
5148 GV->takeName(OldGV);
5150 if (!OldGV->use_empty()) {
5151 OldGV->replaceAllUsesWith(GV);
5154 OldGV->eraseFromParent();
5157 if (supportsCOMDAT() && GV->isWeakForLinker() &&
5158 !GV->hasAvailableExternallyLinkage())
5159 GV->setComdat(TheModule.getOrInsertComdat(GV->getName()));
5161 GV->setAlignment(Alignment);
5166 /// GetAddrOfGlobalVar - Return the llvm::Constant for the address of the
5167 /// given global variable. If Ty is non-null and if the global doesn't exist,
5175 assert(D->hasGlobalStorage() && "Not a global variable");
5176 QualType ASTTy = D->getType();
5185 /// CreateRuntimeVariable - Create a new runtime global variable with the
5193 setDSOLocal(cast<llvm::GlobalValue>(Ret->stripPointerCasts()));
5198 assert(!D->getInit() && "Cannot emit definite definitions here!");
5206 if (GV && !GV->isDeclaration())
5234 LangAS AS = D ? D->getType().getAddressSpace() : LangAS::opencl_global;
5245 (!D || D->getType().getAddressSpace() == LangAS::Default))
5250 if (D->hasAttr<CUDAConstantAttr>())
5252 if (D->hasAttr<CUDASharedAttr>())
5254 if (D->hasAttr<CUDADeviceAttr>())
5256 if (D->getType().isConstQualified())
5264 if (OpenMPRuntime->hasAllocateAttributeForGlobalVar(D, AS))
5277 // For HIPSPV map literals to cuda_device (maps to CrossWorkGroup in SPIR-V)
5278 // instead of default AS (maps to Generic in SPIR-V). Otherwise, we end up
5280 // allowed (SPIR-V V1.6 s3.42.8). Also, mapping literals to SPIR-V
5321 if (!D->template hasAttr<UsedAttr>())
5325 if (!D->getIdentifier() || D->getFormalLinkage() != Linkage::Internal)
5330 const SomeDecl *First = D->getFirstDecl();
5331 if (First->getDeclContext()->isRecord() || !First->isInExternCContext())
5338 StaticExternCValues.insert(std::make_pair(D->getIdentifier(), GV));
5343 R.first->second = nullptr;
5391 QualType ASTTy = D->getType();
5392 if (getLangOpts().OpenCL && ASTTy->isSamplerT())
5398 OpenMPRuntime->emitTargetGlobalVariable(D))
5410 D->needsDestruction(getContext()) == QualType::DK_cxx_destructor;
5417 (!D->hasConstantInitialization() ||
5420 D->needsDestruction(getContext()) ||
5421 !D->getType().isConstantStorage(getContext(), true, true)))
5425 const Expr *InitExpr = D->getAnyInitializer(InitDecl);
5433 getLangOpts().CUDAIsDevice && D->hasAttr<CUDASharedAttr>();
5434 // Shadows of initialized device-side global variables are also left
5438 !getLangOpts().CUDAIsDevice && !D->hasAttr<HIPManagedAttr>() &&
5439 (D->hasAttr<CUDAConstantAttr>() || D->hasAttr<CUDADeviceAttr>() ||
5440 D->hasAttr<CUDASharedAttr>());
5442 getLangOpts().CUDAIsDevice && !D->hasAttr<HIPManagedAttr>() &&
5443 (D->getType()->isCUDADeviceBuiltinSurfaceType() ||
5444 D->getType()->isCUDADeviceBuiltinTextureType());
5448 else if (D->hasAttr<LoaderUninitializedAttr>())
5460 assert(!ASTTy->isIncompleteType() && "Unexpected incomplete type");
5461 Init = EmitNullConstant(D->getType());
5465 llvm::Constant *Initializer = emitter->tryEmitForInitializer(*InitDecl);
5467 QualType T = InitExpr->getType();
5468 if (D->getType()->isReferenceType())
5469 T = D->getType();
5472 if (InitDecl->hasFlexibleArrayInit(getContext()))
5492 InitDecl->getFlexibleArrayInitChars(getContext());
5494 getDataLayout().getTypeAllocSize(Init->getType()));
5500 llvm::Type* InitType = Init->getType();
5505 Entry = Entry->stripPointerCasts();
5519 if (!GV || GV->getValueType() != InitType ||
5520 GV->getType()->getAddressSpace() !=
5524 Entry->setName(StringRef());
5529 ->stripPointerCasts());
5534 Entry->getType());
5535 Entry->replaceAllUsesWith(NewPtrForOldDecl);
5538 cast<llvm::GlobalValue>(Entry)->eraseFromParent();
5543 if (D->hasAttr<AnnotateAttr>())
5559 (D->hasAttr<CUDADeviceAttr>() || D->hasAttr<CUDAConstantAttr>() ||
5560 D->getType()->isCUDADeviceBuiltinSurfaceType() ||
5561 D->getType()->isCUDADeviceBuiltinTextureType()))
5562 GV->setExternallyInitialized(true);
5569 GV->setInitializer(Init);
5571 emitter->finalize(GV);
5574 GV->setConstant(!NeedsGlobalCtor && !NeedsGlobalDtor &&
5575 D->getType().isConstantStorage(getContext(), true, true));
5577 // If it is in a read-only section, mark it 'constant'.
5578 if (const SectionAttr *SA = D->getAttr<SectionAttr>()) {
5579 const ASTContext::SectionInfo &SI = Context.SectionInfos[SA->getName()];
5581 GV->setConstant(true);
5589 GV->setAlignment(AlignVal.getAsAlign());
5591 // On Darwin, unlike other Itanium C++ ABI platforms, the thread-wrapper
5594 // thread-wrapper in order to ensure initialization has occurred, underlying
5595 // variable will never be used other than the thread-wrapper, so it can be
5599 // referenced directly, without calling the thread-wrapper, so the linkage
5603 // weak or linkonce, the de-duplication semantics are important to preserve,
5605 if (D->getTLSKind() == VarDecl::TLS_Dynamic &&
5608 !D->hasAttr<ConstInitAttr>())
5611 GV->setLinkage(Linkage);
5612 if (D->hasAttr<DLLImportAttr>())
5613 GV->setDLLStorageClass(llvm::GlobalVariable::DLLImportStorageClass);
5614 else if (D->hasAttr<DLLExportAttr>())
5615 GV->setDLLStorageClass(llvm::GlobalVariable::DLLExportStorageClass);
5617 GV->setDLLStorageClass(llvm::GlobalVariable::DefaultStorageClass);
5621 GV->setConstant(false);
5623 // non-zero null pointers. In this case they should have weak linkage
5625 // explicit section therefore cannot have non-zero initial value.
5626 if (!GV->getInitializer()->isNullValue())
5627 GV->setLinkage(llvm::GlobalVariable::WeakAnyLinkage);
5632 if (D->getTLSKind() && !GV->isThreadLocal()) {
5633 if (D->getTLSKind() == VarDecl::TLS_Dynamic)
5644 SanitizerMD->reportGlobal(GV, *D, NeedsGlobalCtor);
5649 DI->EmitGlobalVariable(GV, D);
5655 QualType ASTTy = D->getType();
5656 llvm::Type *Ty = getTypes().ConvertTypeForMem(D->getType());
5658 GetOrCreateLLVMGlobal(D->getName(), Ty, ASTTy.getAddressSpace(), D);
5659 DI->EmitExternalVariable(
5660 cast<llvm::GlobalVariable>(GV->stripPointerCasts()), D);
5667 auto *Ty = getTypes().ConvertType(FD->getType());
5671 if (!Fn->getSubprogram())
5672 DI->EmitFunctionDecl(FD, FD->getLocation(), FD->getType(), Fn);
5679 // Don't give variables common linkage if -fno-common was specified unless it
5681 if ((NoCommon || D->hasAttr<NoCommonAttr>()) && !D->hasAttr<CommonAttr>())
5686 // an initializer, and without a storage-class specifier or with the
5687 // storage-class specifier static, constitutes a tentative definition.
5688 if (D->getInit() || D->hasExternalStorage())
5692 if (D->hasAttr<SectionAttr>())
5696 // We don't try to determine which is the right section in the front-end.
5698 if (D->hasAttr<PragmaClangBSSSectionAttr>() ||
5699 D->hasAttr<PragmaClangDataSectionAttr>() ||
5700 D->hasAttr<PragmaClangRelroSectionAttr>() ||
5701 D->hasAttr<PragmaClangRodataSectionAttr>())
5705 if (D->getTLSKind())
5709 if (D->hasAttr<WeakImportAttr>())
5719 if (D->hasAttr<AlignedAttr>())
5721 QualType VarType = D->getType();
5725 if (const auto *RT = VarType->getAs<RecordType>()) {
5726 const RecordDecl *RD = RT->getDecl();
5727 for (const FieldDecl *FD : RD->fields()) {
5728 if (FD->isBitField())
5730 if (FD->hasAttr<AlignedAttr>())
5732 if (Context.isAlignmentRequired(FD->getType()))
5741 // Other COFF linkers (ld.bfd and LLD) support arbitrary power-of-two
5745 Context.getTypeAlignIfKnown(D->getType()) >
5758 if (D->hasAttr<WeakAttr>())
5761 if (const auto *FD = D->getAsFunction())
5762 if (FD->isMultiVersion() && Linkage == GVA_AvailableExternally)
5790 // CUDA/HIP: For -fno-gpu-rdc case, device code is limited to one TU,
5792 // This lets llvm perform aggressive inter-procedural optimizations. For
5793 // -fgpu-rdc case, device function calls across multiple TU's are allowed,
5800 return D->hasAttr<CUDAGlobalAttr>() ? llvm::Function::ExternalLinkage
5815 // http://msdn.microsoft.com/en-us/library/5tkz6s71.aspx
5816 if (D->hasAttr<SelectAnyAttr>())
5830 /// Replace the uses of a function that was declared with a non-proto type.
5835 if (old->use_empty())
5838 llvm::Type *newRetTy = newFn->getReturnType();
5843 for (llvm::Value::use_iterator ui = old->use_begin(), ue = old->use_end();
5845 llvm::User *user = ui->getUser();
5850 if (bitcast->getOpcode() == llvm::Instruction::BitCast)
5859 if (!callSite->isCallee(&*ui))
5864 if (callSite->getType() != newRetTy && !callSite->use_empty())
5869 llvm::AttributeList oldAttrs = callSite->getAttributes();
5872 unsigned newNumArgs = newFn->arg_size();
5873 if (callSite->arg_size() < newNumArgs)
5880 for (llvm::Argument &A : newFn->args()) {
5881 if (callSite->getArgOperand(argNo)->getType() != A.getType()) {
5895 newArgs.append(callSite->arg_begin(), callSite->arg_begin() + argNo);
5899 callSite->getOperandBundlesAsDefs(newBundles);
5907 newCall = llvm::InvokeInst::Create(newFn, oldInvoke->getNormalDest(),
5908 oldInvoke->getUnwindDest(), newArgs,
5913 if (!newCall->getType()->isVoidTy())
5914 newCall->takeName(callSite);
5915 newCall->setAttributes(
5916 llvm::AttributeList::get(newFn->getContext(), oldAttrs.getFnAttrs(),
5918 newCall->setCallingConv(callSite->getCallingConv());
5921 if (!callSite->use_empty())
5922 callSite->replaceAllUsesWith(newCall);
5925 if (callSite->getDebugLoc())
5926 newCall->setDebugLoc(callSite->getDebugLoc());
5932 callSite->eraseFromParent();
5936 /// ReplaceUsesOfNonProtoTypeWithRealFunction - This function is called when we
5944 /// run at -O0.
5954 auto DK = VD->isThisDeclarationADefinition();
5955 if ((DK == VarDecl::Definition && VD->hasAttr<DLLImportAttr>()) ||
5959 TemplateSpecializationKind TSK = VD->getTemplateSpecializationKind();
5962 if (VD->getDefinition() && TSK == TSK_ExplicitInstantiationDefinition)
5977 if (!GV || (GV->getValueType() != Ty))
5983 if (!GV->isDeclaration())
6005 if (const ConstructorAttr *CA = D->getAttr<ConstructorAttr>())
6006 AddGlobalCtor(Fn, CA->getPriority());
6007 if (const DestructorAttr *DA = D->getAttr<DestructorAttr>())
6008 AddGlobalDtor(Fn, DA->getPriority(), true);
6009 if (getLangOpts().OpenMP && D->hasAttr<OMPDeclareTargetDeclAttr>())
6015 const AliasAttr *AA = D->getAttr<AliasAttr>();
6020 if (AA->getAliasee() == MangledName) {
6021 Diags.Report(AA->getLocation(), diag::err_cyclic_alias) << 0;
6028 if (Entry && !Entry->isDeclaration())
6033 llvm::Type *DeclTy = getTypes().ConvertTypeForMem(D->getType());
6040 Aliasee = GetOrCreateLLVMFunction(AA->getAliasee(), DeclTy, GD,
6044 Aliasee = GetOrCreateLLVMGlobal(AA->getAliasee(), DeclTy, LangAS::Default,
6053 unsigned AS = Aliasee->getType()->getPointerAddressSpace();
6058 if (GA->getAliasee() == Entry) {
6059 Diags.Report(AA->getLocation(), diag::err_cyclic_alias) << 0;
6063 assert(Entry->isDeclaration());
6072 GA->takeName(Entry);
6074 Entry->replaceAllUsesWith(GA);
6075 Entry->eraseFromParent();
6077 GA->setName(MangledName);
6083 if (D->hasAttr<WeakAttr>() || D->hasAttr<WeakRefAttr>() ||
6084 D->isWeakImported()) {
6085 GA->setLinkage(llvm::Function::WeakAnyLinkage);
6089 if (VD->getTLSKind())
6097 DI->EmitGlobalAlias(cast<llvm::GlobalValue>(GA->getAliasee()->stripPointerCasts()), GD);
6102 const IFuncAttr *IFA = D->getAttr<IFuncAttr>();
6107 if (IFA->getResolver() == MangledName) {
6108 Diags.Report(IFA->getLocation(), diag::err_cyclic_alias) << 1;
6114 if (Entry && !Entry->isDeclaration()) {
6118 Diags.Report(D->getLocation(), diag::err_duplicate_mangled_name)
6120 Diags.Report(OtherGD.getDecl()->getLocation(),
6128 // The resolver might not be visited yet. Specify a dummy non-function type to
6133 GetOrCreateLLVMFunction(IFA->getResolver(), VoidTy, {},
6135 llvm::Type *DeclTy = getTypes().ConvertTypeForMem(D->getType());
6140 if (GIF->getResolver() == Entry) {
6141 Diags.Report(IFA->getLocation(), diag::err_cyclic_alias) << 1;
6144 assert(Entry->isDeclaration());
6148 // extern int test();
6150 // int test() __attribute__((ifunc("resolver")));
6153 GIF->takeName(Entry);
6155 Entry->replaceAllUsesWith(GIF);
6156 Entry->eraseFromParent();
6158 GIF->setName(MangledName);
6172 StringRef String = Literal->getString();
6176 if (!Literal->containsNonAsciiOrNull()) {
6192 StringLength = ToPtr - &ToBuf[0];
6213 C, C->getValueType(), CharUnits::fromQuantity(C->getAlignment()));
6259 IdentifierInfo &II = Context.Idents.get(GV->getName());
6264 for (const auto *Result : DC->lookup(&II))
6270 GV->setLinkage(llvm::GlobalValue::ExternalLinkage);
6272 GV->setLinkage(llvm::GlobalValue::ExternalLinkage);
6273 if (!VD || !VD->hasAttr<DLLExportAttr>())
6274 GV->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass);
6276 GV->setDLLStorageClass(llvm::GlobalValue::DLLExportStorageClass);
6283 // Decay array -> ptr
6317 // Note: -fwritable-strings doesn't make the backing store strings of
6320 new llvm::GlobalVariable(getModule(), C->getType(), /*isConstant=*/true,
6322 GV->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
6327 GV->setAlignment(Align.getAsAlign());
6333 GV->setSection(isUTF16 ? "__TEXT,__ustring"
6336 // the static linker to adjust permissions to read-only later on.
6338 GV->setSection(".rodata");
6356 // Swift ABI requires 8-byte alignment to ensure that the _Atomic(uint64_t) is
6357 // properly aligned on 32-bit platforms.
6365 GV->addAttribute("objc_arc_inert");
6377 GV->setSection("cfstring");
6380 GV->setSection("__DATA,__cfstring");
6385 return ConstantAddress(GV, GV->getValueType(), Alignment);
6395 D->startDefinition();
6412 Field->setAccess(AS_public);
6413 D->addDecl(Field);
6416 D->completeDefinition();
6425 assert(!E->getType()->isPointerType() && "Strings are always arrays");
6429 if (E->getCharByteWidth() == 1) {
6430 SmallString<64> Str(E->getString());
6433 const ConstantArrayType *CAT = Context.getAsConstantArrayType(E->getType());
6435 Str.resize(CAT->getZExtSize());
6439 auto *AType = cast<llvm::ArrayType>(getTypes().ConvertType(E->getType()));
6440 llvm::Type *ElemTy = AType->getElementType();
6441 unsigned NumElements = AType->getNumElements();
6443 // Wide strings have either 2-byte or 4-byte elements.
6444 if (ElemTy->getPrimitiveSizeInBits() == 16) {
6448 for(unsigned i = 0, e = E->getLength(); i != e; ++i)
6449 Elements.push_back(E->getCodeUnit(i));
6454 assert(ElemTy->getPrimitiveSizeInBits() == 32);
6458 for(unsigned i = 0, e = E->getLength(); i != e; ++i)
6459 Elements.push_back(E->getCodeUnit(i));
6474 M, C->getType(), !CGM.getLangOpts().WritableStrings, LT, C, GlobalName,
6476 GV->setAlignment(Alignment.getAsAlign());
6477 GV->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
6478 if (GV->isWeakForLinker()) {
6480 GV->setComdat(M.getOrInsertComdat(GV->getName()));
6487 /// GetAddrOfConstantStringFromLiteral - Return a pointer to a
6493 getContext().getAlignOfGlobalVarInChars(S->getType(), /*VD=*/nullptr);
6500 if (uint64_t(Alignment.getQuantity()) > GV->getAlignment())
6501 GV->setAlignment(Alignment.getAsAlign());
6503 GV->getValueType(), Alignment);
6529 DI->AddStringLiteralDebugInfo(GV, S);
6534 SanitizerMD->reportGlobal(GV, S->getStrTokenLoc(0), "<string literal>");
6537 GV->getValueType(), Alignment);
6540 /// GetAddrOfConstantStringFromObjCEncode - Return a pointer to a constant
6545 getContext().getObjCEncodingForType(E->getEncodedType(), Str);
6550 /// GetAddrOfConstantCString - Returns a pointer to a character array containing
6567 if (uint64_t(Alignment.getQuantity()) > GV->getAlignment())
6568 GV->setAlignment(Alignment.getAsAlign());
6570 GV->getValueType(), Alignment);
6584 GV->getValueType(), Alignment);
6589 assert((E->getStorageDuration() == SD_Static ||
6590 E->getStorageDuration() == SD_Thread) && "not a global temporary");
6591 const auto *VD = cast<VarDecl>(E->getExtendingDecl());
6594 // cv-qualifiers from the type of the MaterializeTemporaryExpr.
6595 QualType MaterializedType = Init->getType();
6596 if (Init == E->getSubExpr())
6597 MaterializedType = E->getType();
6605 if (!InsertResult.first->second) {
6606 // We recursively re-entered this function, probably during emission of
6610 InsertResult.first->second = new llvm::GlobalVariable(
6614 return ConstantAddress(InsertResult.first->second,
6616 InsertResult.first->second->stripPointerCasts())
6617 ->getValueType(),
6621 // FIXME: If an externally-visible declaration extends multiple temporaries,
6623 // we also need to make the temporaries externally-visible).
6627 VD, E->getManglingNumber(), Out);
6630 if (E->getStorageDuration() == SD_Static && VD->evaluateValue()) {
6636 Value = E->getOrCreateValue(false);
6641 if (!Value && Init->EvaluateAsRValue(EvalResult, getContext()) &&
6654 InitialValue = emitter->emitForInitializer(*Value, AddrSpace,
6659 Type = InitialValue->getType();
6666 // Create a global variable for this lifetime-extended temporary.
6670 if (VD->isStaticDataMember() && VD->getAnyInitializer(InitVD) &&
6671 isa<CXXRecordDecl>(InitVD->getLexicalDeclContext())) {
6685 if (emitter) emitter->finalize(GV);
6689 if (GV->getDLLStorageClass() == llvm::GlobalVariable::DLLExportStorageClass)
6691 GV->setDLLStorageClass(llvm::GlobalVariable::DefaultStorageClass);
6693 GV->setAlignment(Align.getAsAlign());
6694 if (supportsCOMDAT() && GV->isWeakForLinker())
6695 GV->setComdat(TheModule.getOrInsertComdat(GV->getName()));
6696 if (VD->getTLSKind())
6710 Entry->replaceAllUsesWith(CV);
6711 llvm::cast<llvm::GlobalVariable>(Entry)->eraseFromParent();
6718 /// EmitObjCPropertyImplementations - Emit information for synthesized
6722 for (const auto *PID : D->property_impls()) {
6723 // Dynamic is just for type-checking.
6724 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
6725 ObjCPropertyDecl *PD = PID->getPropertyDecl();
6732 auto *Getter = PID->getGetterMethodDecl();
6733 if (!Getter || Getter->isSynthesizedAccessorStub())
6736 auto *Setter = PID->getSetterMethodDecl();
6737 if (!PD->isReadOnly() && (!Setter || Setter->isSynthesizedAccessorStub()))
6745 const ObjCInterfaceDecl *iface = impl->getClassInterface();
6746 for (const ObjCIvarDecl *ivar = iface->all_declared_ivar_begin();
6747 ivar; ivar = ivar->getNextIvar())
6748 if (ivar->getType().isDestructedType())
6757 for (ObjCImplementationDecl::init_iterator B = D->init_begin(),
6758 E = D->init_end(); B != E; ++B) {
6760 Expr *Init = CtorInitExp->getInit();
6767 /// EmitObjCIvarInitializations - Emit information for ivar initialization
6775 getContext(), D->getLocation(), D->getLocation(), cxxSelector,
6781 D->addInstanceMethod(DTORMethod);
6783 D->setHasDestructors(true);
6788 if (D->getNumIvarInitializers() == 0 ||
6796 getContext(), D->getLocation(), D->getLocation(), cxxSelector,
6802 D->addInstanceMethod(CTORMethod);
6804 D->setHasNonZeroConstructors(true);
6807 // EmitLinkageSpec - Emit all declarations in a linkage spec.
6809 if (LSD->getLanguage() != LinkageSpecLanguageIDs::C &&
6810 LSD->getLanguage() != LinkageSpecLanguageIDs::CXX) {
6826 // We emitted a top-level stmt but after it there is initialization.
6827 // Stop squashing the top-level stmts into a single function.
6828 if (CurCGF && CXXGlobalInits.back() != CurCGF->CurFn) {
6829 CurCGF->FinishFunction(D->getEndLoc());
6847 CurCGF->StartFunction(GlobalDecl(), RetTy, Fn, FnInfo, Args,
6848 D->getBeginLoc(), D->getBeginLoc());
6852 CurCGF->EmitStmt(D->getStmt());
6856 for (auto *I : DC->decls()) {
6858 // are themselves considered "top-level", so EmitTopLevelDecl on an
6861 // ExportDecl) that does stop them from being considered "top-level".
6863 for (auto *M : OID->methods())
6871 /// EmitTopLevelDecl - Emit code for a single top level declaration.
6874 if (D->isTemplated())
6878 if (auto *FD = dyn_cast<FunctionDecl>(D); FD && FD->isImmediateFunction())
6881 switch (D->getKind()) {
6892 // Function-like, but does not result in code emission.
6900 for (auto *B : DD->bindings())
6901 if (auto *HD = B->getHoldingVar())
6917 if (Spec->getSpecializationKind() ==
6919 Spec->hasDefinition())
6920 DI->completeTemplateDefinition(*Spec);
6925 if (CRD->hasDefinition())
6926 DI->EmitAndRetainType(getContext().getRecordType(cast<RecordDecl>(D)));
6927 if (auto *ES = D->getASTContext().getExternalSource())
6928 if (ES->hasExternalDefinitions(D) == ExternalASTSource::EK_Never)
6929 DI->completeUnusedClass(*CRD);
6932 for (auto *I : CRD->decls())
6951 DI->EmitUsingDecl(cast<UsingDecl>(*D));
6955 DI->EmitUsingEnumDecl(cast<UsingEnumDecl>(*D));
6959 DI->EmitNamespaceAlias(cast<NamespaceAliasDecl>(*D));
6963 DI->EmitUsingDirective(cast<UsingDirectiveDecl>(*D));
6976 // Objective-C Decls
6985 if (Proto->isThisDeclarationADefinition())
6986 ObjCRuntime->GenerateProtocol(Proto);
6993 ObjCRuntime->GenerateCategory(cast<ObjCCategoryImplDecl>(D));
7000 ObjCRuntime->GenerateClass(OMD);
7004 DI->getOrCreateInterfaceType(getContext().getObjCInterfaceType(
7005 OMD->getClassInterface()), OMD->getLocation());
7011 if (OMD->getBody())
7016 ObjCRuntime->RegisterAlias(cast<ObjCCompatibleAliasDecl>(D));
7021 switch (PCD->getCommentKind()) {
7025 AppendLinkerOptions(PCD->getArg());
7028 AddDependentLib(PCD->getArg());
7040 AddDetectMismatch(PDMD->getName(), PDMD->getValue());
7049 // File-scope asm is ignored during device-side CUDA compilation.
7052 // File-scope asm is ignored during device-side OpenMP compilation.
7055 // File-scope asm is ignored during device-side SYCL compilation.
7059 getModule().appendModuleInlineAsm(AD->getAsmString()->getString());
7071 if (!ImportedModules.insert(Import->getImportedModule()))
7075 if (!Import->getImportedOwningModule()) {
7077 DI->EmitImportDecl(*Import);
7080 // For C++ standard modules we are done - we will call the module
7083 if (CXX20ModuleInits && Import->getImportedModule() &&
7084 Import->getImportedModule()->isNamedModule())
7087 // For clang C++ module map modules the initializers for sub-modules are
7093 Visited.insert(Import->getImportedModule());
7094 Stack.push_back(Import->getImportedModule());
7105 for (auto *Submodule : Mod->submodules()) {
7108 if (Submodule->IsExplicit)
7145 DI->EmitAndRetainType(
7151 if (cast<RecordDecl>(D)->getDefinition())
7152 DI->EmitAndRetainType(getContext().getRecordType(cast<RecordDecl>(D)));
7157 if (cast<EnumDecl>(D)->getDefinition())
7158 DI->EmitAndRetainType(getContext().getEnumType(cast<EnumDecl>(D)));
7167 // non-top-level decl. FIXME: Would be nice to have an isTopLevelDeclKind
7178 switch (D->getKind()) {
7185 if (!cast<FunctionDecl>(D)->doesThisDeclarationHaveABody())
7188 if (LimitedCoverage && SM.getMainFileID() != SM.getFileID(D->getBeginLoc()))
7191 SM.isInSystemHeader(D->getBeginLoc()))
7206 if (Fn->isTemplateInstantiation())
7207 ClearUnusedCoverageMapping(Fn->getTemplateInstantiationPattern());
7213 // We call takeVector() here to avoid use-after-free.
7221 switch (D->getKind()) {
7254 // alias for "main" in the no-argument case so that libc can detect when
7255 // new-style no-argument main is in used.
7257 if (!F->isDeclaration() && F->arg_size() == 0 && !F->isVarArg() &&
7258 F->getReturnType()->isIntegerTy(Context.getTargetInfo().getIntWidth())) {
7260 GA->setVisibility(llvm::GlobalValue::HiddenVisibility);
7285 GlobalMetadata->addOperand(llvm::MDNode::get(CGM.getLLVMContext(), Ops));
7296 // It isn't valid to replace the extern-C ifuncs if all we find is itself!
7303 for (llvm::User *User : Elem->users()) {
7308 if (ConstExpr->getOpcode() != llvm::Instruction::BitCast)
7311 for (llvm::User *CEUser : ConstExpr->users()) {
7333 IFunc->setResolver(nullptr);
7335 ConstExpr->destroyConstant();
7339 Elem->eraseFromParent();
7342 // The type of the resolver is always just a function-type that returns the
7346 llvm::FunctionType::get(IFunc->getType(), /*isVarArg*/ false);
7348 CppFunc->getName(), ResolverTy, {}, /*ForVTable*/ false);
7349 IFunc->setResolver(Resolver);
7373 getModule().getNamedValue(Name->getName());
7378 addCompilerUsedGlobal(llvm::GlobalAlias::create(Name->getName(), Val));
7387 Result = Res->getValue();
7427 Alloca->setMetadata(
7444 IdentMetadata->addOperand(llvm::MDNode::get(Ctx, IdentNode));
7454 CommandLineMetadata->addOperand(llvm::MDNode::get(Ctx, CommandLineNode));
7468 for (int i = 0, e = CUNode->getNumOperands(); i != e; ++i) {
7469 llvm::MDNode *CU = CUNode->getOperand(i);
7471 GCov->addOperand(llvm::MDNode::get(Ctx, Elts));
7483 if (ForEH && Ty->isObjCObjectPointerType() &&
7485 return ObjCRuntime->GetEHType(Ty);
7491 // Do not emit threadprivates in simd-only mode.
7494 for (auto RefExpr : D->varlists()) {
7495 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(RefExpr)->getDecl());
7497 VD->getAnyInitializer() &&
7498 !VD->getAnyInitializer()->isConstantInitializer(getContext(),
7502 getTypes().ConvertTypeForMem(VD->getType()),
7505 VD, Addr, RefExpr->getBeginLoc(), PerformInit))
7513 if (auto *FnType = T->getAs<FunctionProtoType>())
7515 FnType->getReturnType(), FnType->getParamTypes(),
7516 FnType->getExtProtoInfo().withExceptionSpec(EST_None));
7522 if (isExternallyVisible(T->getLinkage())) {
7552 // originally pointed-to type, e.g. 'const char *' and 'char * const *'
7556 if (!Ty->isPointerType())
7561 Ty->getPointeeType().getCVRQualifiers()));
7566 if (auto *FnType = Ty->getAs<FunctionProtoType>()) {
7568 for (auto &Param : FnType->param_types())
7572 GeneralizeType(Ctx, FnType->getReturnType()),
7573 GeneralizedParams, FnType->getExtProtoInfo());
7576 if (auto *FnType = Ty->getAs<FunctionNoProtoType>())
7578 GeneralizeType(Ctx, FnType->getReturnType()));
7588 /// Returns whether this module needs the "all-vtables" type identifier.
7590 // Returns true if at least one of vtable-based CFI checkers is enabled and
7606 CreateMetadataIdentifierForType(QualType(RD->getTypeForDecl(), 0));
7607 VTable->addTypeMetadata(Offset.getQuantity(), MD);
7611 VTable->addTypeMetadata(Offset.getQuantity(),
7615 llvm::Metadata *MD = llvm::MDString::get(getLLVMContext(), "all-vtables");
7616 VTable->addTypeMetadata(Offset.getQuantity(), MD);
7630 llvm::Constant *C = ConstantEmitter(CGF).emitAbstract(E, E->getType());
7631 auto *SamplerT = getOpenCLRuntime().getSamplerType(E->getType().getTypePtr());
7632 auto *FTy = llvm::FunctionType::get(SamplerT, {C->getType()}, false);
7640 return getNaturalTypeAlignment(T->getPointeeType(), BaseInfo, TBAAInfo,
7657 if (auto TT = T->getAs<TypedefType>()) {
7658 if (auto Align = TT->getDecl()->getMaxAlignment()) {
7665 bool AlignForArray = T->isArrayType();
7671 if (T->isIncompleteType()) {
7674 // type is incomplete, so it's impossible to test. We could try to reuse
7691 (RD = T->getAsCXXRecordDecl())) {
7694 // non-virtual alignment.
7713 // This number is positive only when -ftrivial-auto-var-init-stop-after=* is
7721 "-ftrivial-auto-var-init-stop-after=%0 has been enabled to limit the "
7722 "number of times ftrivial-auto-var-init=%1 gets applied.");
7747 PresumedLoc PLoc = SM.getPresumedLoc(D->getLocation());
7760 PLoc = SM.getPresumedLoc(D->getLocation(), /*UseLineDirectives=*/false);
7776 assert(NewBuilder->DeferredDecls.empty() &&
7778 NewBuilder->DeferredDecls = std::move(DeferredDecls);
7782 assert(NewBuilder->DeferredVTables.empty() &&
7784 NewBuilder->DeferredVTables = std::move(DeferredVTables);
7786 assert(NewBuilder->MangledDeclNames.empty() &&
7788 assert(NewBuilder->Manglings.empty() &&
7790 NewBuilder->Manglings = std::move(Manglings);
7792 NewBuilder->WeakRefReferences = std::move(WeakRefReferences);
7794 NewBuilder->ABI->MangleCtx = std::move(ABI->MangleCtx);