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