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