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