@@ -31,30 +31,6 @@ static cl::opt<bool>
3131 " the codegen data generation or use. Local "
3232 " merging is still enabled within a module." ),
3333 cl::init(false ));
34- static cl::opt<unsigned > GlobalMergingMinInstrs (
35- " global-merging-min-instrs" ,
36- cl::desc (" The minimum instruction count required when merging functions." ),
37- cl::init(1 ), cl::Hidden);
38- static cl::opt<unsigned > GlobalMergingMaxParams (
39- " global-merging-max-params" ,
40- cl::desc (
41- " The maximum number of parameters allowed when merging functions." ),
42- cl::init(std::numeric_limits<unsigned >::max()), cl::Hidden);
43- static cl::opt<unsigned > GlobalMergingParamOverhead (
44- " global-merging-param-overhead" ,
45- cl::desc (" The overhead cost associated with each parameter when merging "
46- " functions." ),
47- cl::init(2 ), cl::Hidden);
48- static cl::opt<unsigned >
49- GlobalMergingCallOverhead (" global-merging-call-overhead" ,
50- cl::desc (" The overhead cost associated with each "
51- " function call when merging functions." ),
52- cl::init(1 ), cl::Hidden);
53- static cl::opt<unsigned > GlobalMergingExtraThreshold (
54- " global-merging-extra-threshold" ,
55- cl::desc (" An additional cost threshold that must be exceeded for merging "
56- " to be considered beneficial." ),
57- cl::init(0 ), cl::Hidden);
5834
5935STATISTIC (NumMismatchedFunctionHash,
6036 " Number of mismatched function hash for global merge function" );
@@ -442,40 +418,6 @@ static ParamLocsVecTy computeParamInfo(
442418 return ParamLocsVec;
443419}
444420
445- static bool isProfitable (
446- const SmallVector<std::unique_ptr<StableFunctionMap::StableFunctionEntry>>
447- &SFS,
448- const Function *F) {
449- // No interest if the number of candidates are less than 2.
450- unsigned StableFunctionCount = SFS.size ();
451- if (StableFunctionCount < 2 )
452- return false ;
453-
454- unsigned InstCount = SFS[0 ]->InstCount ;
455- if (InstCount < GlobalMergingMinInstrs)
456- return false ;
457-
458- unsigned ParamCount = SFS[0 ]->IndexOperandHashMap ->size ();
459- unsigned TotalParamCount = ParamCount + F->getFunctionType ()->getNumParams ();
460- if (TotalParamCount > GlobalMergingMaxParams)
461- return false ;
462-
463- unsigned Benefit = InstCount * (StableFunctionCount - 1 );
464- unsigned Cost =
465- (GlobalMergingParamOverhead * ParamCount + GlobalMergingCallOverhead) *
466- StableFunctionCount +
467- GlobalMergingExtraThreshold;
468-
469- bool Result = Benefit > Cost;
470- LLVM_DEBUG (dbgs () << " isProfitable: Function = " << F->getName () << " , "
471- << " StableFunctionCount = " << StableFunctionCount
472- << " , InstCount = " << InstCount
473- << " , ParamCount = " << ParamCount
474- << " , Benefit = " << Benefit << " , Cost = " << Cost
475- << " , Result = " << (Result ? " true" : " false" ) << " \n " );
476- return Result;
477- }
478-
479421bool GlobalMergeFunc::merge (Module &M, const StableFunctionMap *FunctionMap) {
480422 bool Changed = false ;
481423
@@ -488,12 +430,9 @@ bool GlobalMergeFunc::merge(Module &M, const StableFunctionMap *FunctionMap) {
488430
489431 auto ModId = M.getModuleIdentifier ();
490432 for (auto &[Hash, SFS] : FunctionMap->getFunctionMap ()) {
491- // Compute the parameter locations based on the unique hash sequences
433+ // Parameter locations based on the unique hash sequences
492434 // across the candidates.
493- auto ParamLocsVec = computeParamInfo (SFS);
494- LLVM_DEBUG (dbgs () << " [GlobalMergeFunc] Merging hash: " << Hash
495- << " with Params " << ParamLocsVec.size () << " \n " );
496-
435+ std::optional<ParamLocsVecTy> ParamLocsVec;
497436 Function *MergedFunc = nullptr ;
498437 std::string MergedModId;
499438 SmallVector<FuncMergeInfo> FuncMergeInfos;
@@ -542,15 +481,17 @@ bool GlobalMergeFunc::merge(Module &M, const StableFunctionMap *FunctionMap) {
542481 ++NumMismatchedConstHash;
543482 continue ;
544483 }
484+ if (!ParamLocsVec.has_value ()) {
485+ ParamLocsVec = computeParamInfo (SFS);
486+ LLVM_DEBUG (dbgs () << " [GlobalMergeFunc] Merging hash: " << Hash
487+ << " with Params " << ParamLocsVec->size () << " \n " );
488+ }
545489 if (!checkConstLocationCompatible (*SF, *FI.IndexInstruction ,
546- ParamLocsVec)) {
490+ * ParamLocsVec)) {
547491 ++NumMismatchedConstHash;
548492 continue ;
549493 }
550494
551- if (!isProfitable (SFS, F))
552- break ;
553-
554495 if (MergedFunc) {
555496 // Check if the matched functions fall into the same (first) module.
556497 // This module check is not strictly necessary as the functions can move
@@ -583,7 +524,7 @@ bool GlobalMergeFunc::merge(Module &M, const StableFunctionMap *FunctionMap) {
583524 // the parameters. Populate parameters pointing to the original constants.
584525 SmallVector<Constant *> Params;
585526 SmallVector<Type *> ParamTypes;
586- for (auto &ParamLocs : ParamLocsVec) {
527+ for (auto &ParamLocs : * ParamLocsVec) {
587528 assert (!ParamLocs.empty ());
588529 auto &[InstIndex, OpndIndex] = ParamLocs[0 ];
589530 auto *Inst = FMI.IndexInstruction ->lookup (InstIndex);
@@ -594,7 +535,7 @@ bool GlobalMergeFunc::merge(Module &M, const StableFunctionMap *FunctionMap) {
594535
595536 // Create a merged function derived from the current function.
596537 Function *MergedFunc =
597- createMergedFunction (FMI, ParamTypes, ParamLocsVec);
538+ createMergedFunction (FMI, ParamTypes, * ParamLocsVec);
598539
599540 LLVM_DEBUG ({
600541 dbgs () << " [GlobalMergeFunc] Merged function (hash:" << FMI.SF ->Hash
0 commit comments