1 //===- ThinLTOBitcodeWriter.cpp - Bitcode writing pass for ThinLTO --------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "llvm/Transforms/IPO/ThinLTOBitcodeWriter.h" 10 #include "llvm/Analysis/BasicAliasAnalysis.h" 11 #include "llvm/Analysis/ModuleSummaryAnalysis.h" 12 #include "llvm/Analysis/ProfileSummaryInfo.h" 13 #include "llvm/Analysis/TypeMetadataUtils.h" 14 #include "llvm/Bitcode/BitcodeWriter.h" 15 #include "llvm/IR/Constants.h" 16 #include "llvm/IR/DebugInfo.h" 17 #include "llvm/IR/Instructions.h" 18 #include "llvm/IR/Intrinsics.h" 19 #include "llvm/IR/Module.h" 20 #include "llvm/IR/PassManager.h" 21 #include "llvm/InitializePasses.h" 22 #include "llvm/Object/ModuleSymbolTable.h" 23 #include "llvm/Pass.h" 24 #include "llvm/Support/ScopedPrinter.h" 25 #include "llvm/Support/raw_ostream.h" 26 #include "llvm/Transforms/IPO.h" 27 #include "llvm/Transforms/IPO/FunctionAttrs.h" 28 #include "llvm/Transforms/IPO/FunctionImport.h" 29 #include "llvm/Transforms/IPO/LowerTypeTests.h" 30 #include "llvm/Transforms/Utils/Cloning.h" 31 #include "llvm/Transforms/Utils/ModuleUtils.h" 32 using namespace llvm; 33 34 namespace { 35 36 // Promote each local-linkage entity defined by ExportM and used by ImportM by 37 // changing visibility and appending the given ModuleId. 38 void promoteInternals(Module &ExportM, Module &ImportM, StringRef ModuleId, 39 SetVector<GlobalValue *> &PromoteExtra) { 40 DenseMap<const Comdat *, Comdat *> RenamedComdats; 41 for (auto &ExportGV : ExportM.global_values()) { 42 if (!ExportGV.hasLocalLinkage()) 43 continue; 44 45 auto Name = ExportGV.getName(); 46 GlobalValue *ImportGV = nullptr; 47 if (!PromoteExtra.count(&ExportGV)) { 48 ImportGV = ImportM.getNamedValue(Name); 49 if (!ImportGV) 50 continue; 51 ImportGV->removeDeadConstantUsers(); 52 if (ImportGV->use_empty()) { 53 ImportGV->eraseFromParent(); 54 continue; 55 } 56 } 57 58 std::string OldName = Name.str(); 59 std::string NewName = (Name + ModuleId).str(); 60 61 if (const auto *C = ExportGV.getComdat()) 62 if (C->getName() == Name) 63 RenamedComdats.try_emplace(C, ExportM.getOrInsertComdat(NewName)); 64 65 ExportGV.setName(NewName); 66 ExportGV.setLinkage(GlobalValue::ExternalLinkage); 67 ExportGV.setVisibility(GlobalValue::HiddenVisibility); 68 69 if (ImportGV) { 70 ImportGV->setName(NewName); 71 ImportGV->setVisibility(GlobalValue::HiddenVisibility); 72 } 73 74 if (Function *F = dyn_cast<Function>(&ExportGV)) { 75 // Create a local alias with the original name to avoid breaking 76 // references from inline assembly. 77 GlobalAlias *A = GlobalAlias::create( 78 F->getValueType(), F->getAddressSpace(), GlobalValue::InternalLinkage, 79 OldName, F, &ExportM); 80 appendToCompilerUsed(ExportM, A); 81 } 82 } 83 84 if (!RenamedComdats.empty()) 85 for (auto &GO : ExportM.global_objects()) 86 if (auto *C = GO.getComdat()) { 87 auto Replacement = RenamedComdats.find(C); 88 if (Replacement != RenamedComdats.end()) 89 GO.setComdat(Replacement->second); 90 } 91 } 92 93 // Promote all internal (i.e. distinct) type ids used by the module by replacing 94 // them with external type ids formed using the module id. 95 // 96 // Note that this needs to be done before we clone the module because each clone 97 // will receive its own set of distinct metadata nodes. 98 void promoteTypeIds(Module &M, StringRef ModuleId) { 99 DenseMap<Metadata *, Metadata *> LocalToGlobal; 100 auto ExternalizeTypeId = [&](CallInst *CI, unsigned ArgNo) { 101 Metadata *MD = 102 cast<MetadataAsValue>(CI->getArgOperand(ArgNo))->getMetadata(); 103 104 if (isa<MDNode>(MD) && cast<MDNode>(MD)->isDistinct()) { 105 Metadata *&GlobalMD = LocalToGlobal[MD]; 106 if (!GlobalMD) { 107 std::string NewName = (Twine(LocalToGlobal.size()) + ModuleId).str(); 108 GlobalMD = MDString::get(M.getContext(), NewName); 109 } 110 111 CI->setArgOperand(ArgNo, 112 MetadataAsValue::get(M.getContext(), GlobalMD)); 113 } 114 }; 115 116 if (Function *TypeTestFunc = 117 M.getFunction(Intrinsic::getName(Intrinsic::type_test))) { 118 for (const Use &U : TypeTestFunc->uses()) { 119 auto CI = cast<CallInst>(U.getUser()); 120 ExternalizeTypeId(CI, 1); 121 } 122 } 123 124 if (Function *TypeCheckedLoadFunc = 125 M.getFunction(Intrinsic::getName(Intrinsic::type_checked_load))) { 126 for (const Use &U : TypeCheckedLoadFunc->uses()) { 127 auto CI = cast<CallInst>(U.getUser()); 128 ExternalizeTypeId(CI, 2); 129 } 130 } 131 132 for (GlobalObject &GO : M.global_objects()) { 133 SmallVector<MDNode *, 1> MDs; 134 GO.getMetadata(LLVMContext::MD_type, MDs); 135 136 GO.eraseMetadata(LLVMContext::MD_type); 137 for (auto MD : MDs) { 138 auto I = LocalToGlobal.find(MD->getOperand(1)); 139 if (I == LocalToGlobal.end()) { 140 GO.addMetadata(LLVMContext::MD_type, *MD); 141 continue; 142 } 143 GO.addMetadata( 144 LLVMContext::MD_type, 145 *MDNode::get(M.getContext(), {MD->getOperand(0), I->second})); 146 } 147 } 148 } 149 150 // Drop unused globals, and drop type information from function declarations. 151 // FIXME: If we made functions typeless then there would be no need to do this. 152 void simplifyExternals(Module &M) { 153 FunctionType *EmptyFT = 154 FunctionType::get(Type::getVoidTy(M.getContext()), false); 155 156 for (auto I = M.begin(), E = M.end(); I != E;) { 157 Function &F = *I++; 158 if (F.isDeclaration() && F.use_empty()) { 159 F.eraseFromParent(); 160 continue; 161 } 162 163 if (!F.isDeclaration() || F.getFunctionType() == EmptyFT || 164 // Changing the type of an intrinsic may invalidate the IR. 165 F.getName().startswith("llvm.")) 166 continue; 167 168 Function *NewF = 169 Function::Create(EmptyFT, GlobalValue::ExternalLinkage, 170 F.getAddressSpace(), "", &M); 171 NewF->copyAttributesFrom(&F); 172 // Only copy function attribtues. 173 NewF->setAttributes( 174 AttributeList::get(M.getContext(), AttributeList::FunctionIndex, 175 F.getAttributes().getFnAttributes())); 176 NewF->takeName(&F); 177 F.replaceAllUsesWith(ConstantExpr::getBitCast(NewF, F.getType())); 178 F.eraseFromParent(); 179 } 180 181 for (auto I = M.global_begin(), E = M.global_end(); I != E;) { 182 GlobalVariable &GV = *I++; 183 if (GV.isDeclaration() && GV.use_empty()) { 184 GV.eraseFromParent(); 185 continue; 186 } 187 } 188 } 189 190 static void 191 filterModule(Module *M, 192 function_ref<bool(const GlobalValue *)> ShouldKeepDefinition) { 193 std::vector<GlobalValue *> V; 194 for (GlobalValue &GV : M->global_values()) 195 if (!ShouldKeepDefinition(&GV)) 196 V.push_back(&GV); 197 198 for (GlobalValue *GV : V) 199 if (!convertToDeclaration(*GV)) 200 GV->eraseFromParent(); 201 } 202 203 void forEachVirtualFunction(Constant *C, function_ref<void(Function *)> Fn) { 204 if (auto *F = dyn_cast<Function>(C)) 205 return Fn(F); 206 if (isa<GlobalValue>(C)) 207 return; 208 for (Value *Op : C->operands()) 209 forEachVirtualFunction(cast<Constant>(Op), Fn); 210 } 211 212 // Clone any @llvm[.compiler].used over to the new module and append 213 // values whose defs were cloned into that module. 214 static void cloneUsedGlobalVariables(const Module &SrcM, Module &DestM, 215 bool CompilerUsed) { 216 SmallVector<GlobalValue *, 4> Used, NewUsed; 217 // First collect those in the llvm[.compiler].used set. 218 collectUsedGlobalVariables(SrcM, Used, CompilerUsed); 219 // Next build a set of the equivalent values defined in DestM. 220 for (auto *V : Used) { 221 auto *GV = DestM.getNamedValue(V->getName()); 222 if (GV && !GV->isDeclaration()) 223 NewUsed.push_back(GV); 224 } 225 // Finally, add them to a llvm[.compiler].used variable in DestM. 226 if (CompilerUsed) 227 appendToCompilerUsed(DestM, NewUsed); 228 else 229 appendToUsed(DestM, NewUsed); 230 } 231 232 // If it's possible to split M into regular and thin LTO parts, do so and write 233 // a multi-module bitcode file with the two parts to OS. Otherwise, write only a 234 // regular LTO bitcode file to OS. 235 void splitAndWriteThinLTOBitcode( 236 raw_ostream &OS, raw_ostream *ThinLinkOS, 237 function_ref<AAResults &(Function &)> AARGetter, Module &M) { 238 std::string ModuleId = getUniqueModuleId(&M); 239 if (ModuleId.empty()) { 240 // We couldn't generate a module ID for this module, write it out as a 241 // regular LTO module with an index for summary-based dead stripping. 242 ProfileSummaryInfo PSI(M); 243 M.addModuleFlag(Module::Error, "ThinLTO", uint32_t(0)); 244 ModuleSummaryIndex Index = buildModuleSummaryIndex(M, nullptr, &PSI); 245 WriteBitcodeToFile(M, OS, /*ShouldPreserveUseListOrder=*/false, &Index); 246 247 if (ThinLinkOS) 248 // We don't have a ThinLTO part, but still write the module to the 249 // ThinLinkOS if requested so that the expected output file is produced. 250 WriteBitcodeToFile(M, *ThinLinkOS, /*ShouldPreserveUseListOrder=*/false, 251 &Index); 252 253 return; 254 } 255 256 promoteTypeIds(M, ModuleId); 257 258 // Returns whether a global or its associated global has attached type 259 // metadata. The former may participate in CFI or whole-program 260 // devirtualization, so they need to appear in the merged module instead of 261 // the thin LTO module. Similarly, globals that are associated with globals 262 // with type metadata need to appear in the merged module because they will 263 // reference the global's section directly. 264 auto HasTypeMetadata = [](const GlobalObject *GO) { 265 if (MDNode *MD = GO->getMetadata(LLVMContext::MD_associated)) 266 if (auto *AssocVM = dyn_cast_or_null<ValueAsMetadata>(MD->getOperand(0))) 267 if (auto *AssocGO = dyn_cast<GlobalObject>(AssocVM->getValue())) 268 if (AssocGO->hasMetadata(LLVMContext::MD_type)) 269 return true; 270 return GO->hasMetadata(LLVMContext::MD_type); 271 }; 272 273 // Collect the set of virtual functions that are eligible for virtual constant 274 // propagation. Each eligible function must not access memory, must return 275 // an integer of width <=64 bits, must take at least one argument, must not 276 // use its first argument (assumed to be "this") and all arguments other than 277 // the first one must be of <=64 bit integer type. 278 // 279 // Note that we test whether this copy of the function is readnone, rather 280 // than testing function attributes, which must hold for any copy of the 281 // function, even a less optimized version substituted at link time. This is 282 // sound because the virtual constant propagation optimizations effectively 283 // inline all implementations of the virtual function into each call site, 284 // rather than using function attributes to perform local optimization. 285 DenseSet<const Function *> EligibleVirtualFns; 286 // If any member of a comdat lives in MergedM, put all members of that 287 // comdat in MergedM to keep the comdat together. 288 DenseSet<const Comdat *> MergedMComdats; 289 for (GlobalVariable &GV : M.globals()) 290 if (HasTypeMetadata(&GV)) { 291 if (const auto *C = GV.getComdat()) 292 MergedMComdats.insert(C); 293 forEachVirtualFunction(GV.getInitializer(), [&](Function *F) { 294 auto *RT = dyn_cast<IntegerType>(F->getReturnType()); 295 if (!RT || RT->getBitWidth() > 64 || F->arg_empty() || 296 !F->arg_begin()->use_empty()) 297 return; 298 for (auto &Arg : drop_begin(F->args())) { 299 auto *ArgT = dyn_cast<IntegerType>(Arg.getType()); 300 if (!ArgT || ArgT->getBitWidth() > 64) 301 return; 302 } 303 if (!F->isDeclaration() && 304 computeFunctionBodyMemoryAccess(*F, AARGetter(*F)) == MAK_ReadNone) 305 EligibleVirtualFns.insert(F); 306 }); 307 } 308 309 ValueToValueMapTy VMap; 310 std::unique_ptr<Module> MergedM( 311 CloneModule(M, VMap, [&](const GlobalValue *GV) -> bool { 312 if (const auto *C = GV->getComdat()) 313 if (MergedMComdats.count(C)) 314 return true; 315 if (auto *F = dyn_cast<Function>(GV)) 316 return EligibleVirtualFns.count(F); 317 if (auto *GVar = dyn_cast_or_null<GlobalVariable>(GV->getBaseObject())) 318 return HasTypeMetadata(GVar); 319 return false; 320 })); 321 StripDebugInfo(*MergedM); 322 MergedM->setModuleInlineAsm(""); 323 324 // Clone any llvm.*used globals to ensure the included values are 325 // not deleted. 326 cloneUsedGlobalVariables(M, *MergedM, /*CompilerUsed*/ false); 327 cloneUsedGlobalVariables(M, *MergedM, /*CompilerUsed*/ true); 328 329 for (Function &F : *MergedM) 330 if (!F.isDeclaration()) { 331 // Reset the linkage of all functions eligible for virtual constant 332 // propagation. The canonical definitions live in the thin LTO module so 333 // that they can be imported. 334 F.setLinkage(GlobalValue::AvailableExternallyLinkage); 335 F.setComdat(nullptr); 336 } 337 338 SetVector<GlobalValue *> CfiFunctions; 339 for (auto &F : M) 340 if ((!F.hasLocalLinkage() || F.hasAddressTaken()) && HasTypeMetadata(&F)) 341 CfiFunctions.insert(&F); 342 343 // Remove all globals with type metadata, globals with comdats that live in 344 // MergedM, and aliases pointing to such globals from the thin LTO module. 345 filterModule(&M, [&](const GlobalValue *GV) { 346 if (auto *GVar = dyn_cast_or_null<GlobalVariable>(GV->getBaseObject())) 347 if (HasTypeMetadata(GVar)) 348 return false; 349 if (const auto *C = GV->getComdat()) 350 if (MergedMComdats.count(C)) 351 return false; 352 return true; 353 }); 354 355 promoteInternals(*MergedM, M, ModuleId, CfiFunctions); 356 promoteInternals(M, *MergedM, ModuleId, CfiFunctions); 357 358 auto &Ctx = MergedM->getContext(); 359 SmallVector<MDNode *, 8> CfiFunctionMDs; 360 for (auto V : CfiFunctions) { 361 Function &F = *cast<Function>(V); 362 SmallVector<MDNode *, 2> Types; 363 F.getMetadata(LLVMContext::MD_type, Types); 364 365 SmallVector<Metadata *, 4> Elts; 366 Elts.push_back(MDString::get(Ctx, F.getName())); 367 CfiFunctionLinkage Linkage; 368 if (lowertypetests::isJumpTableCanonical(&F)) 369 Linkage = CFL_Definition; 370 else if (F.hasExternalWeakLinkage()) 371 Linkage = CFL_WeakDeclaration; 372 else 373 Linkage = CFL_Declaration; 374 Elts.push_back(ConstantAsMetadata::get( 375 llvm::ConstantInt::get(Type::getInt8Ty(Ctx), Linkage))); 376 append_range(Elts, Types); 377 CfiFunctionMDs.push_back(MDTuple::get(Ctx, Elts)); 378 } 379 380 if(!CfiFunctionMDs.empty()) { 381 NamedMDNode *NMD = MergedM->getOrInsertNamedMetadata("cfi.functions"); 382 for (auto MD : CfiFunctionMDs) 383 NMD->addOperand(MD); 384 } 385 386 SmallVector<MDNode *, 8> FunctionAliases; 387 for (auto &A : M.aliases()) { 388 if (!isa<Function>(A.getAliasee())) 389 continue; 390 391 auto *F = cast<Function>(A.getAliasee()); 392 393 Metadata *Elts[] = { 394 MDString::get(Ctx, A.getName()), 395 MDString::get(Ctx, F->getName()), 396 ConstantAsMetadata::get( 397 ConstantInt::get(Type::getInt8Ty(Ctx), A.getVisibility())), 398 ConstantAsMetadata::get( 399 ConstantInt::get(Type::getInt8Ty(Ctx), A.isWeakForLinker())), 400 }; 401 402 FunctionAliases.push_back(MDTuple::get(Ctx, Elts)); 403 } 404 405 if (!FunctionAliases.empty()) { 406 NamedMDNode *NMD = MergedM->getOrInsertNamedMetadata("aliases"); 407 for (auto MD : FunctionAliases) 408 NMD->addOperand(MD); 409 } 410 411 SmallVector<MDNode *, 8> Symvers; 412 ModuleSymbolTable::CollectAsmSymvers(M, [&](StringRef Name, StringRef Alias) { 413 Function *F = M.getFunction(Name); 414 if (!F || F->use_empty()) 415 return; 416 417 Symvers.push_back(MDTuple::get( 418 Ctx, {MDString::get(Ctx, Name), MDString::get(Ctx, Alias)})); 419 }); 420 421 if (!Symvers.empty()) { 422 NamedMDNode *NMD = MergedM->getOrInsertNamedMetadata("symvers"); 423 for (auto MD : Symvers) 424 NMD->addOperand(MD); 425 } 426 427 simplifyExternals(*MergedM); 428 429 // FIXME: Try to re-use BSI and PFI from the original module here. 430 ProfileSummaryInfo PSI(M); 431 ModuleSummaryIndex Index = buildModuleSummaryIndex(M, nullptr, &PSI); 432 433 // Mark the merged module as requiring full LTO. We still want an index for 434 // it though, so that it can participate in summary-based dead stripping. 435 MergedM->addModuleFlag(Module::Error, "ThinLTO", uint32_t(0)); 436 ModuleSummaryIndex MergedMIndex = 437 buildModuleSummaryIndex(*MergedM, nullptr, &PSI); 438 439 SmallVector<char, 0> Buffer; 440 441 BitcodeWriter W(Buffer); 442 // Save the module hash produced for the full bitcode, which will 443 // be used in the backends, and use that in the minimized bitcode 444 // produced for the full link. 445 ModuleHash ModHash = {{0}}; 446 W.writeModule(M, /*ShouldPreserveUseListOrder=*/false, &Index, 447 /*GenerateHash=*/true, &ModHash); 448 W.writeModule(*MergedM, /*ShouldPreserveUseListOrder=*/false, &MergedMIndex); 449 W.writeSymtab(); 450 W.writeStrtab(); 451 OS << Buffer; 452 453 // If a minimized bitcode module was requested for the thin link, only 454 // the information that is needed by thin link will be written in the 455 // given OS (the merged module will be written as usual). 456 if (ThinLinkOS) { 457 Buffer.clear(); 458 BitcodeWriter W2(Buffer); 459 StripDebugInfo(M); 460 W2.writeThinLinkBitcode(M, Index, ModHash); 461 W2.writeModule(*MergedM, /*ShouldPreserveUseListOrder=*/false, 462 &MergedMIndex); 463 W2.writeSymtab(); 464 W2.writeStrtab(); 465 *ThinLinkOS << Buffer; 466 } 467 } 468 469 // Check if the LTO Unit splitting has been enabled. 470 bool enableSplitLTOUnit(Module &M) { 471 bool EnableSplitLTOUnit = false; 472 if (auto *MD = mdconst::extract_or_null<ConstantInt>( 473 M.getModuleFlag("EnableSplitLTOUnit"))) 474 EnableSplitLTOUnit = MD->getZExtValue(); 475 return EnableSplitLTOUnit; 476 } 477 478 // Returns whether this module needs to be split because it uses type metadata. 479 bool hasTypeMetadata(Module &M) { 480 for (auto &GO : M.global_objects()) { 481 if (GO.hasMetadata(LLVMContext::MD_type)) 482 return true; 483 } 484 return false; 485 } 486 487 void writeThinLTOBitcode(raw_ostream &OS, raw_ostream *ThinLinkOS, 488 function_ref<AAResults &(Function &)> AARGetter, 489 Module &M, const ModuleSummaryIndex *Index) { 490 std::unique_ptr<ModuleSummaryIndex> NewIndex = nullptr; 491 // See if this module has any type metadata. If so, we try to split it 492 // or at least promote type ids to enable WPD. 493 if (hasTypeMetadata(M)) { 494 if (enableSplitLTOUnit(M)) 495 return splitAndWriteThinLTOBitcode(OS, ThinLinkOS, AARGetter, M); 496 // Promote type ids as needed for index-based WPD. 497 std::string ModuleId = getUniqueModuleId(&M); 498 if (!ModuleId.empty()) { 499 promoteTypeIds(M, ModuleId); 500 // Need to rebuild the index so that it contains type metadata 501 // for the newly promoted type ids. 502 // FIXME: Probably should not bother building the index at all 503 // in the caller of writeThinLTOBitcode (which does so via the 504 // ModuleSummaryIndexAnalysis pass), since we have to rebuild it 505 // anyway whenever there is type metadata (here or in 506 // splitAndWriteThinLTOBitcode). Just always build it once via the 507 // buildModuleSummaryIndex when Module(s) are ready. 508 ProfileSummaryInfo PSI(M); 509 NewIndex = std::make_unique<ModuleSummaryIndex>( 510 buildModuleSummaryIndex(M, nullptr, &PSI)); 511 Index = NewIndex.get(); 512 } 513 } 514 515 // Write it out as an unsplit ThinLTO module. 516 517 // Save the module hash produced for the full bitcode, which will 518 // be used in the backends, and use that in the minimized bitcode 519 // produced for the full link. 520 ModuleHash ModHash = {{0}}; 521 WriteBitcodeToFile(M, OS, /*ShouldPreserveUseListOrder=*/false, Index, 522 /*GenerateHash=*/true, &ModHash); 523 // If a minimized bitcode module was requested for the thin link, only 524 // the information that is needed by thin link will be written in the 525 // given OS. 526 if (ThinLinkOS && Index) 527 WriteThinLinkBitcodeToFile(M, *ThinLinkOS, *Index, ModHash); 528 } 529 530 class WriteThinLTOBitcode : public ModulePass { 531 raw_ostream &OS; // raw_ostream to print on 532 // The output stream on which to emit a minimized module for use 533 // just in the thin link, if requested. 534 raw_ostream *ThinLinkOS; 535 536 public: 537 static char ID; // Pass identification, replacement for typeid 538 WriteThinLTOBitcode() : ModulePass(ID), OS(dbgs()), ThinLinkOS(nullptr) { 539 initializeWriteThinLTOBitcodePass(*PassRegistry::getPassRegistry()); 540 } 541 542 explicit WriteThinLTOBitcode(raw_ostream &o, raw_ostream *ThinLinkOS) 543 : ModulePass(ID), OS(o), ThinLinkOS(ThinLinkOS) { 544 initializeWriteThinLTOBitcodePass(*PassRegistry::getPassRegistry()); 545 } 546 547 StringRef getPassName() const override { return "ThinLTO Bitcode Writer"; } 548 549 bool runOnModule(Module &M) override { 550 const ModuleSummaryIndex *Index = 551 &(getAnalysis<ModuleSummaryIndexWrapperPass>().getIndex()); 552 writeThinLTOBitcode(OS, ThinLinkOS, LegacyAARGetter(*this), M, Index); 553 return true; 554 } 555 void getAnalysisUsage(AnalysisUsage &AU) const override { 556 AU.setPreservesAll(); 557 AU.addRequired<AssumptionCacheTracker>(); 558 AU.addRequired<ModuleSummaryIndexWrapperPass>(); 559 AU.addRequired<TargetLibraryInfoWrapperPass>(); 560 } 561 }; 562 } // anonymous namespace 563 564 char WriteThinLTOBitcode::ID = 0; 565 INITIALIZE_PASS_BEGIN(WriteThinLTOBitcode, "write-thinlto-bitcode", 566 "Write ThinLTO Bitcode", false, true) 567 INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker) 568 INITIALIZE_PASS_DEPENDENCY(ModuleSummaryIndexWrapperPass) 569 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass) 570 INITIALIZE_PASS_END(WriteThinLTOBitcode, "write-thinlto-bitcode", 571 "Write ThinLTO Bitcode", false, true) 572 573 ModulePass *llvm::createWriteThinLTOBitcodePass(raw_ostream &Str, 574 raw_ostream *ThinLinkOS) { 575 return new WriteThinLTOBitcode(Str, ThinLinkOS); 576 } 577 578 PreservedAnalyses 579 llvm::ThinLTOBitcodeWriterPass::run(Module &M, ModuleAnalysisManager &AM) { 580 FunctionAnalysisManager &FAM = 581 AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager(); 582 writeThinLTOBitcode(OS, ThinLinkOS, 583 [&FAM](Function &F) -> AAResults & { 584 return FAM.getResult<AAManager>(F); 585 }, 586 M, &AM.getResult<ModuleSummaryIndexAnalysis>(M)); 587 return PreservedAnalyses::all(); 588 } 589