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