1 //===---- GlobalMergeFunctions.cpp - Global merge functions -------*- C++ -===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This pass implements the global merge function pass. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "llvm/CodeGen/GlobalMergeFunctions.h" 14 #include "llvm/ADT/Statistic.h" 15 #include "llvm/Analysis/ModuleSummaryAnalysis.h" 16 #include "llvm/CGData/CodeGenData.h" 17 #include "llvm/IR/IRBuilder.h" 18 #include "llvm/IR/StructuralHash.h" 19 #include "llvm/InitializePasses.h" 20 #include "llvm/Support/CommandLine.h" 21 #include "llvm/Transforms/Utils/ModuleUtils.h" 22 23 #define DEBUG_TYPE "global-merge-func" 24 25 using namespace llvm; 26 using namespace llvm::support; 27 28 static cl::opt<bool> DisableCGDataForMerging( 29 "disable-cgdata-for-merging", cl::Hidden, 30 cl::desc("Disable codegen data for function merging. Local " 31 "merging is still enabled within a module."), 32 cl::init(false)); 33 34 STATISTIC(NumMergedFunctions, 35 "Number of functions that are actually merged using function hash"); 36 STATISTIC(NumAnalyzedModues, "Number of modules that are analyzed"); 37 STATISTIC(NumAnalyzedFunctions, "Number of functions that are analyzed"); 38 STATISTIC(NumEligibleFunctions, "Number of functions that are eligible"); 39 40 /// Returns true if the \OpIdx operand of \p CI is the callee operand. 41 static bool isCalleeOperand(const CallBase *CI, unsigned OpIdx) { 42 return &CI->getCalledOperandUse() == &CI->getOperandUse(OpIdx); 43 } 44 45 static bool canParameterizeCallOperand(const CallBase *CI, unsigned OpIdx) { 46 if (CI->isInlineAsm()) 47 return false; 48 Function *Callee = CI->getCalledOperand() 49 ? dyn_cast_or_null<Function>( 50 CI->getCalledOperand()->stripPointerCasts()) 51 : nullptr; 52 if (Callee) { 53 if (Callee->isIntrinsic()) 54 return false; 55 auto Name = Callee->getName(); 56 // objc_msgSend stubs must be called, and can't have their address taken. 57 if (Name.starts_with("objc_msgSend$")) 58 return false; 59 // Calls to dtrace probes must generate unique patchpoints. 60 if (Name.starts_with("__dtrace")) 61 return false; 62 } 63 if (isCalleeOperand(CI, OpIdx)) { 64 // The operand is the callee and it has already been signed. Ignore this 65 // because we cannot add another ptrauth bundle to the call instruction. 66 if (CI->getOperandBundle(LLVMContext::OB_ptrauth).has_value()) 67 return false; 68 } else { 69 // The target of the arc-attached call must be a constant and cannot be 70 // parameterized. 71 if (CI->isOperandBundleOfType(LLVMContext::OB_clang_arc_attachedcall, 72 OpIdx)) 73 return false; 74 } 75 return true; 76 } 77 78 /// Returns true if function \p F is eligible for merging. 79 bool isEligibleFunction(Function *F) { 80 if (F->isDeclaration()) 81 return false; 82 83 if (F->hasFnAttribute(llvm::Attribute::NoMerge) || 84 F->hasFnAttribute(llvm::Attribute::AlwaysInline)) 85 return false; 86 87 if (F->hasAvailableExternallyLinkage()) 88 return false; 89 90 if (F->getFunctionType()->isVarArg()) 91 return false; 92 93 if (F->getCallingConv() == CallingConv::SwiftTail) 94 return false; 95 96 // If function contains callsites with musttail, if we merge 97 // it, the merged function will have the musttail callsite, but 98 // the number of parameters can change, thus the parameter count 99 // of the callsite will mismatch with the function itself. 100 for (const BasicBlock &BB : *F) { 101 for (const Instruction &I : BB) { 102 const auto *CB = dyn_cast<CallBase>(&I); 103 if (CB && CB->isMustTailCall()) 104 return false; 105 } 106 } 107 108 return true; 109 } 110 111 static bool isEligibleInstructionForConstantSharing(const Instruction *I) { 112 switch (I->getOpcode()) { 113 case Instruction::Load: 114 case Instruction::Store: 115 case Instruction::Call: 116 case Instruction::Invoke: 117 return true; 118 default: 119 return false; 120 } 121 } 122 123 // This function takes an instruction, \p I, and an operand index, \p OpIdx. 124 // It returns true if the operand should be ignored in the hash computation. 125 // If \p OpIdx is out of range based on the other instruction context, it cannot 126 // be ignored. 127 static bool ignoreOp(const Instruction *I, unsigned OpIdx) { 128 if (OpIdx >= I->getNumOperands()) 129 return false; 130 131 if (!isEligibleInstructionForConstantSharing(I)) 132 return false; 133 134 if (!isa<Constant>(I->getOperand(OpIdx))) 135 return false; 136 137 if (const auto *CI = dyn_cast<CallBase>(I)) 138 return canParameterizeCallOperand(CI, OpIdx); 139 140 return true; 141 } 142 143 static Value *createCast(IRBuilder<> &Builder, Value *V, Type *DestTy) { 144 Type *SrcTy = V->getType(); 145 if (SrcTy->isStructTy()) { 146 assert(DestTy->isStructTy()); 147 assert(SrcTy->getStructNumElements() == DestTy->getStructNumElements()); 148 Value *Result = PoisonValue::get(DestTy); 149 for (unsigned int I = 0, E = SrcTy->getStructNumElements(); I < E; ++I) { 150 Value *Element = 151 createCast(Builder, Builder.CreateExtractValue(V, ArrayRef(I)), 152 DestTy->getStructElementType(I)); 153 154 Result = Builder.CreateInsertValue(Result, Element, ArrayRef(I)); 155 } 156 return Result; 157 } 158 assert(!DestTy->isStructTy()); 159 if (auto *SrcAT = dyn_cast<ArrayType>(SrcTy)) { 160 auto *DestAT = dyn_cast<ArrayType>(DestTy); 161 assert(DestAT); 162 assert(SrcAT->getNumElements() == DestAT->getNumElements()); 163 Value *Result = PoisonValue::get(DestTy); 164 for (unsigned int I = 0, E = SrcAT->getNumElements(); I < E; ++I) { 165 Value *Element = 166 createCast(Builder, Builder.CreateExtractValue(V, ArrayRef(I)), 167 DestAT->getElementType()); 168 169 Result = Builder.CreateInsertValue(Result, Element, ArrayRef(I)); 170 } 171 return Result; 172 } 173 assert(!DestTy->isArrayTy()); 174 if (SrcTy->isIntegerTy() && DestTy->isPointerTy()) 175 return Builder.CreateIntToPtr(V, DestTy); 176 if (SrcTy->isPointerTy() && DestTy->isIntegerTy()) 177 return Builder.CreatePtrToInt(V, DestTy); 178 return Builder.CreateBitCast(V, DestTy); 179 } 180 181 void GlobalMergeFunc::analyze(Module &M) { 182 ++NumAnalyzedModues; 183 for (Function &Func : M) { 184 ++NumAnalyzedFunctions; 185 if (isEligibleFunction(&Func)) { 186 ++NumEligibleFunctions; 187 188 auto FI = llvm::StructuralHashWithDifferences(Func, ignoreOp); 189 190 // Convert the operand map to a vector for a serialization-friendly 191 // format. 192 IndexOperandHashVecType IndexOperandHashes; 193 for (auto &Pair : *FI.IndexOperandHashMap) 194 IndexOperandHashes.emplace_back(Pair); 195 196 StableFunction SF(FI.FunctionHash, get_stable_name(Func.getName()).str(), 197 M.getModuleIdentifier(), FI.IndexInstruction->size(), 198 std::move(IndexOperandHashes)); 199 200 LocalFunctionMap->insert(SF); 201 } 202 } 203 } 204 205 /// Tuple to hold function info to process merging. 206 struct FuncMergeInfo { 207 StableFunctionMap::StableFunctionEntry *SF; 208 Function *F; 209 IndexInstrMap *IndexInstruction; 210 FuncMergeInfo(StableFunctionMap::StableFunctionEntry *SF, Function *F, 211 IndexInstrMap *IndexInstruction) 212 : SF(SF), F(F), IndexInstruction(std::move(IndexInstruction)) {} 213 }; 214 215 // Given the func info, and the parameterized locations, create and return 216 // a new merged function by replacing the original constants with the new 217 // parameters. 218 static Function *createMergedFunction(FuncMergeInfo &FI, 219 ArrayRef<Type *> ConstParamTypes, 220 const ParamLocsVecTy &ParamLocsVec) { 221 // Synthesize a new merged function name by appending ".Tgm" to the root 222 // function's name. 223 auto *MergedFunc = FI.F; 224 std::string NewFunctionName = 225 MergedFunc->getName().str() + GlobalMergeFunc::MergingInstanceSuffix; 226 auto *M = MergedFunc->getParent(); 227 assert(!M->getFunction(NewFunctionName)); 228 229 FunctionType *OrigTy = MergedFunc->getFunctionType(); 230 // Get the original params' types. 231 SmallVector<Type *> ParamTypes(OrigTy->param_begin(), OrigTy->param_end()); 232 // Append const parameter types that are passed in. 233 ParamTypes.append(ConstParamTypes.begin(), ConstParamTypes.end()); 234 FunctionType *FuncType = FunctionType::get(OrigTy->getReturnType(), 235 ParamTypes, /*isVarArg=*/false); 236 237 // Declare a new function 238 Function *NewFunction = 239 Function::Create(FuncType, MergedFunc->getLinkage(), NewFunctionName); 240 if (auto *SP = MergedFunc->getSubprogram()) 241 NewFunction->setSubprogram(SP); 242 NewFunction->copyAttributesFrom(MergedFunc); 243 NewFunction->setDLLStorageClass(GlobalValue::DefaultStorageClass); 244 245 NewFunction->setLinkage(GlobalValue::InternalLinkage); 246 NewFunction->addFnAttr(Attribute::NoInline); 247 248 // Add the new function before the root function. 249 M->getFunctionList().insert(MergedFunc->getIterator(), NewFunction); 250 251 // Move the body of MergedFunc into the NewFunction. 252 NewFunction->splice(NewFunction->begin(), MergedFunc); 253 254 // Update the original args by the new args. 255 auto NewArgIter = NewFunction->arg_begin(); 256 for (Argument &OrigArg : MergedFunc->args()) { 257 Argument &NewArg = *NewArgIter++; 258 OrigArg.replaceAllUsesWith(&NewArg); 259 } 260 261 // Replace the original Constants by the new args. 262 unsigned NumOrigArgs = MergedFunc->arg_size(); 263 for (unsigned ParamIdx = 0; ParamIdx < ParamLocsVec.size(); ++ParamIdx) { 264 Argument *NewArg = NewFunction->getArg(NumOrigArgs + ParamIdx); 265 for (auto [InstIndex, OpndIndex] : ParamLocsVec[ParamIdx]) { 266 auto *Inst = FI.IndexInstruction->lookup(InstIndex); 267 auto *OrigC = Inst->getOperand(OpndIndex); 268 if (OrigC->getType() != NewArg->getType()) { 269 IRBuilder<> Builder(Inst->getParent(), Inst->getIterator()); 270 Inst->setOperand(OpndIndex, 271 createCast(Builder, NewArg, OrigC->getType())); 272 } else { 273 Inst->setOperand(OpndIndex, NewArg); 274 } 275 } 276 } 277 278 return NewFunction; 279 } 280 281 // Given the original function (Thunk) and the merged function (ToFunc), create 282 // a thunk to the merged function. 283 static void createThunk(FuncMergeInfo &FI, ArrayRef<Constant *> Params, 284 Function *ToFunc) { 285 auto *Thunk = FI.F; 286 287 assert(Thunk->arg_size() + Params.size() == 288 ToFunc->getFunctionType()->getNumParams()); 289 Thunk->dropAllReferences(); 290 291 BasicBlock *BB = BasicBlock::Create(Thunk->getContext(), "", Thunk); 292 IRBuilder<> Builder(BB); 293 294 SmallVector<Value *> Args; 295 unsigned ParamIdx = 0; 296 FunctionType *ToFuncTy = ToFunc->getFunctionType(); 297 298 // Add arguments which are passed through Thunk. 299 for (Argument &AI : Thunk->args()) { 300 Args.push_back(createCast(Builder, &AI, ToFuncTy->getParamType(ParamIdx))); 301 ++ParamIdx; 302 } 303 304 // Add new arguments defined by Params. 305 for (auto *Param : Params) { 306 assert(ParamIdx < ToFuncTy->getNumParams()); 307 Args.push_back( 308 createCast(Builder, Param, ToFuncTy->getParamType(ParamIdx))); 309 ++ParamIdx; 310 } 311 312 CallInst *CI = Builder.CreateCall(ToFunc, Args); 313 bool isSwiftTailCall = ToFunc->getCallingConv() == CallingConv::SwiftTail && 314 Thunk->getCallingConv() == CallingConv::SwiftTail; 315 CI->setTailCallKind(isSwiftTailCall ? llvm::CallInst::TCK_MustTail 316 : llvm::CallInst::TCK_Tail); 317 CI->setCallingConv(ToFunc->getCallingConv()); 318 CI->setAttributes(ToFunc->getAttributes()); 319 if (Thunk->getReturnType()->isVoidTy()) 320 Builder.CreateRetVoid(); 321 else 322 Builder.CreateRet(createCast(Builder, CI, Thunk->getReturnType())); 323 } 324 325 // Check if the old merged/optimized IndexOperandHashMap is compatible with 326 // the current IndexOperandHashMap. An operand hash may not be stable across 327 // different builds due to varying modules combined. To address this, we relax 328 // the hash check condition by comparing Const hash patterns instead of absolute 329 // hash values. For example, let's assume we have three Consts located at idx1, 330 // idx3, and idx6, where their corresponding hashes are hash1, hash2, and hash1 331 // in the old merged map below: 332 // Old (Merged): [(idx1, hash1), (idx3, hash2), (idx6, hash1)] 333 // Current: [(idx1, hash1'), (idx3, hash2'), (idx6, hash1')] 334 // If the current function also has three Consts in the same locations, 335 // with hash sequences hash1', hash2', and hash1' where the first and third 336 // are the same as the old hash sequences, we consider them matched. 337 static bool checkConstHashCompatible( 338 const DenseMap<IndexPair, stable_hash> &OldInstOpndIndexToConstHash, 339 const DenseMap<IndexPair, stable_hash> &CurrInstOpndIndexToConstHash) { 340 341 DenseMap<stable_hash, stable_hash> OldHashToCurrHash; 342 for (const auto &[Index, OldHash] : OldInstOpndIndexToConstHash) { 343 auto It = CurrInstOpndIndexToConstHash.find(Index); 344 if (It == CurrInstOpndIndexToConstHash.end()) 345 return false; 346 347 auto CurrHash = It->second; 348 auto J = OldHashToCurrHash.find(OldHash); 349 if (J == OldHashToCurrHash.end()) 350 OldHashToCurrHash.insert({OldHash, CurrHash}); 351 else if (J->second != CurrHash) 352 return false; 353 } 354 355 return true; 356 } 357 358 // Validate the locations pointed by a param has the same hash and Constant. 359 static bool 360 checkConstLocationCompatible(const StableFunctionMap::StableFunctionEntry &SF, 361 const IndexInstrMap &IndexInstruction, 362 const ParamLocsVecTy &ParamLocsVec) { 363 for (auto &ParamLocs : ParamLocsVec) { 364 std::optional<stable_hash> OldHash; 365 std::optional<Constant *> OldConst; 366 for (auto &Loc : ParamLocs) { 367 assert(SF.IndexOperandHashMap->count(Loc)); 368 auto CurrHash = SF.IndexOperandHashMap.get()->at(Loc); 369 auto [InstIndex, OpndIndex] = Loc; 370 assert(InstIndex < IndexInstruction.size()); 371 const auto *Inst = IndexInstruction.lookup(InstIndex); 372 auto *CurrConst = cast<Constant>(Inst->getOperand(OpndIndex)); 373 if (!OldHash) { 374 OldHash = CurrHash; 375 OldConst = CurrConst; 376 } else if (CurrConst != *OldConst || CurrHash != *OldHash) { 377 return false; 378 } 379 } 380 } 381 return true; 382 } 383 384 static ParamLocsVecTy computeParamInfo( 385 const SmallVector<std::unique_ptr<StableFunctionMap::StableFunctionEntry>> 386 &SFS) { 387 std::map<std::vector<stable_hash>, ParamLocs> HashSeqToLocs; 388 auto &RSF = *SFS[0]; 389 unsigned StableFunctionCount = SFS.size(); 390 391 for (auto &[IndexPair, Hash] : *RSF.IndexOperandHashMap) { 392 // Const hash sequence across stable functions. 393 // We will allocate a parameter per unique hash squence. 394 // can't use SmallVector as key 395 std::vector<stable_hash> ConstHashSeq; 396 ConstHashSeq.push_back(Hash); 397 bool Identical = true; 398 for (unsigned J = 1; J < StableFunctionCount; ++J) { 399 auto &SF = SFS[J]; 400 auto SHash = SF->IndexOperandHashMap->at(IndexPair); 401 if (Hash != SHash) 402 Identical = false; 403 ConstHashSeq.push_back(SHash); 404 } 405 406 if (Identical) 407 continue; 408 409 // For each unique Const hash sequence (parameter), add the locations. 410 HashSeqToLocs[ConstHashSeq].push_back(IndexPair); 411 } 412 413 ParamLocsVecTy ParamLocsVec; 414 for (auto &[HashSeq, Locs] : HashSeqToLocs) 415 ParamLocsVec.push_back(std::move(Locs)); 416 417 llvm::sort(ParamLocsVec, [&](const ParamLocs &L, const ParamLocs &R) { 418 return L[0] < R[0]; 419 }); 420 421 return ParamLocsVec; 422 } 423 424 bool GlobalMergeFunc::merge(Module &M, const StableFunctionMap *FunctionMap) { 425 bool Changed = false; 426 427 // Collect stable functions related to the current module. 428 DenseMap<stable_hash, SmallVector<std::pair<Function *, FunctionHashInfo>>> 429 HashToFuncs; 430 auto &Maps = FunctionMap->getFunctionMap(); 431 for (auto &F : M) { 432 if (!isEligibleFunction(&F)) 433 continue; 434 auto FI = llvm::StructuralHashWithDifferences(F, ignoreOp); 435 if (Maps.contains(FI.FunctionHash)) 436 HashToFuncs[FI.FunctionHash].emplace_back(&F, std::move(FI)); 437 } 438 439 for (auto &[Hash, Funcs] : HashToFuncs) { 440 std::optional<ParamLocsVecTy> ParamLocsVec; 441 SmallVector<FuncMergeInfo> FuncMergeInfos; 442 auto &SFS = Maps.at(Hash); 443 assert(!SFS.empty()); 444 auto &RFS = SFS[0]; 445 446 // Iterate functions with the same hash. 447 for (auto &[F, FI] : Funcs) { 448 // Check if the function is compatible with any stable function 449 // in terms of the number of instructions and ignored operands. 450 if (RFS->InstCount != FI.IndexInstruction->size()) 451 continue; 452 453 auto hasValidSharedConst = [&](StableFunctionMap::StableFunctionEntry *SF, 454 FunctionHashInfo &FHI) { 455 for (auto &[Index, Hash] : *SF->IndexOperandHashMap) { 456 auto [InstIndex, OpndIndex] = Index; 457 assert(InstIndex < FHI.IndexInstruction->size()); 458 auto *Inst = FHI.IndexInstruction->lookup(InstIndex); 459 if (!ignoreOp(Inst, OpndIndex)) 460 return false; 461 } 462 return true; 463 }; 464 if (!hasValidSharedConst(RFS.get(), FI)) 465 continue; 466 467 for (auto &SF : SFS) { 468 assert(SF->InstCount == FI.IndexInstruction->size()); 469 assert(hasValidSharedConst(SF.get(), FI)); 470 // Check if there is any stable function that is compatiable with the 471 // current one. 472 if (!checkConstHashCompatible(*SF->IndexOperandHashMap, 473 *FI.IndexOperandHashMap)) 474 continue; 475 if (!ParamLocsVec.has_value()) { 476 ParamLocsVec = computeParamInfo(SFS); 477 LLVM_DEBUG(dbgs() << "[GlobalMergeFunc] Merging hash: " << Hash 478 << " with Params " << ParamLocsVec->size() << "\n"); 479 } 480 if (!checkConstLocationCompatible(*SF, *FI.IndexInstruction, 481 *ParamLocsVec)) 482 continue; 483 484 // If a stable function matching the current one is found, 485 // create a candidate for merging and proceed to the next function. 486 FuncMergeInfos.emplace_back(SF.get(), F, FI.IndexInstruction.get()); 487 break; 488 } 489 } 490 unsigned FuncMergeInfoSize = FuncMergeInfos.size(); 491 if (FuncMergeInfoSize == 0) 492 continue; 493 494 LLVM_DEBUG(dbgs() << "[GlobalMergeFunc] Merging function count " 495 << FuncMergeInfoSize << " for hash: " << Hash << "\n"); 496 497 for (auto &FMI : FuncMergeInfos) { 498 Changed = true; 499 500 // We've already validated all locations of constant operands pointed by 501 // the parameters. Populate parameters pointing to the original constants. 502 SmallVector<Constant *> Params; 503 SmallVector<Type *> ParamTypes; 504 for (auto &ParamLocs : *ParamLocsVec) { 505 assert(!ParamLocs.empty()); 506 auto &[InstIndex, OpndIndex] = ParamLocs[0]; 507 auto *Inst = FMI.IndexInstruction->lookup(InstIndex); 508 auto *Opnd = cast<Constant>(Inst->getOperand(OpndIndex)); 509 Params.push_back(Opnd); 510 ParamTypes.push_back(Opnd->getType()); 511 } 512 513 // Create a merged function derived from the current function. 514 Function *MergedFunc = 515 createMergedFunction(FMI, ParamTypes, *ParamLocsVec); 516 517 LLVM_DEBUG({ 518 dbgs() << "[GlobalMergeFunc] Merged function (hash:" << FMI.SF->Hash 519 << ") " << MergedFunc->getName() << " generated from " 520 << FMI.F->getName() << ":\n"; 521 MergedFunc->dump(); 522 }); 523 524 // Transform the current function into a thunk that calls the merged 525 // function. 526 createThunk(FMI, Params, MergedFunc); 527 LLVM_DEBUG({ 528 dbgs() << "[GlobalMergeFunc] Thunk generated: \n"; 529 FMI.F->dump(); 530 }); 531 ++NumMergedFunctions; 532 } 533 } 534 535 return Changed; 536 } 537 538 void GlobalMergeFunc::initializeMergerMode(const Module &M) { 539 // Initialize the local function map regardless of the merger mode. 540 LocalFunctionMap = std::make_unique<StableFunctionMap>(); 541 542 // Disable codegen data for merging. The local merge is still enabled. 543 if (DisableCGDataForMerging) 544 return; 545 546 // (Full)LTO module does not have functions added to the index. 547 // In this case, we run a local merger without using codegen data. 548 if (Index && !Index->hasExportedFunctions(M)) 549 return; 550 551 if (cgdata::emitCGData()) 552 MergerMode = HashFunctionMode::BuildingHashFuncion; 553 else if (cgdata::hasStableFunctionMap()) 554 MergerMode = HashFunctionMode::UsingHashFunction; 555 } 556 557 void GlobalMergeFunc::emitFunctionMap(Module &M) { 558 LLVM_DEBUG(dbgs() << "Emit function map. Size: " << LocalFunctionMap->size() 559 << "\n"); 560 // No need to emit the function map if it is empty. 561 if (LocalFunctionMap->empty()) 562 return; 563 SmallVector<char> Buf; 564 raw_svector_ostream OS(Buf); 565 566 StableFunctionMapRecord::serialize(OS, LocalFunctionMap.get()); 567 568 std::unique_ptr<MemoryBuffer> Buffer = MemoryBuffer::getMemBuffer( 569 OS.str(), "in-memory stable function map", false); 570 571 Triple TT(M.getTargetTriple()); 572 embedBufferInModule(M, *Buffer.get(), 573 getCodeGenDataSectionName(CG_merge, TT.getObjectFormat()), 574 Align(4)); 575 } 576 577 bool GlobalMergeFunc::run(Module &M) { 578 initializeMergerMode(M); 579 580 const StableFunctionMap *FuncMap; 581 if (MergerMode == HashFunctionMode::UsingHashFunction) { 582 // Use the prior CG data to optimistically create global merge candidates. 583 FuncMap = cgdata::getStableFunctionMap(); 584 } else { 585 analyze(M); 586 // Emit the local function map to the custom section, __llvm_merge before 587 // finalizing it. 588 if (MergerMode == HashFunctionMode::BuildingHashFuncion) 589 emitFunctionMap(M); 590 LocalFunctionMap->finalize(); 591 FuncMap = LocalFunctionMap.get(); 592 } 593 594 return merge(M, FuncMap); 595 } 596 597 namespace { 598 599 class GlobalMergeFuncPassWrapper : public ModulePass { 600 601 public: 602 static char ID; 603 604 GlobalMergeFuncPassWrapper(); 605 606 void getAnalysisUsage(AnalysisUsage &AU) const override { 607 AU.addUsedIfAvailable<ImmutableModuleSummaryIndexWrapperPass>(); 608 AU.setPreservesAll(); 609 ModulePass::getAnalysisUsage(AU); 610 } 611 612 StringRef getPassName() const override { return "Global Merge Functions"; } 613 614 bool runOnModule(Module &M) override; 615 }; 616 617 } // namespace 618 619 char GlobalMergeFuncPassWrapper::ID = 0; 620 INITIALIZE_PASS_BEGIN(GlobalMergeFuncPassWrapper, "global-merge-func", 621 "Global merge function pass", false, false) 622 INITIALIZE_PASS_END(GlobalMergeFuncPassWrapper, "global-merge-func", 623 "Global merge function pass", false, false) 624 625 namespace llvm { 626 ModulePass *createGlobalMergeFuncPass() { 627 return new GlobalMergeFuncPassWrapper(); 628 } 629 } // namespace llvm 630 631 GlobalMergeFuncPassWrapper::GlobalMergeFuncPassWrapper() : ModulePass(ID) { 632 initializeGlobalMergeFuncPassWrapperPass( 633 *llvm::PassRegistry::getPassRegistry()); 634 } 635 636 bool GlobalMergeFuncPassWrapper::runOnModule(Module &M) { 637 const ModuleSummaryIndex *Index = nullptr; 638 if (auto *IndexWrapperPass = 639 getAnalysisIfAvailable<ImmutableModuleSummaryIndexWrapperPass>()) 640 Index = IndexWrapperPass->getIndex(); 641 642 return GlobalMergeFunc(Index).run(M); 643 } 644 645 PreservedAnalyses GlobalMergeFuncPass::run(Module &M, 646 AnalysisManager<Module> &AM) { 647 bool Changed = GlobalMergeFunc(ImportSummary).run(M); 648 return Changed ? PreservedAnalyses::none() : PreservedAnalyses::all(); 649 } 650