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