1010//
1111// ===----------------------------------------------------------------------===//
1212
13- #include " llvm/Transforms/IPO /GlobalMergeFunctions.h"
13+ #include " llvm/CodeGen /GlobalMergeFunctions.h"
1414#include " llvm/ADT/Statistic.h"
1515#include " llvm/Analysis/ModuleSummaryAnalysis.h"
1616#include " llvm/CGData/CodeGenData.h"
@@ -56,8 +56,6 @@ static cl::opt<unsigned> GlobalMergingExtraThreshold(
5656 " to be considered beneficial." ),
5757 cl::init(0 ), cl::Hidden);
5858
59- extern cl::opt<bool > EnableGlobalMergeFunc;
60-
6159STATISTIC (NumMismatchedFunctionHash,
6260 " Number of mismatched function hash for global merge function" );
6361STATISTIC (NumMismatchedInstCount,
@@ -87,8 +85,12 @@ static bool canParameterizeCallOperand(const CallBase *CI, unsigned OpIdx) {
8785 if (Callee) {
8886 if (Callee->isIntrinsic ())
8987 return false ;
88+ auto Name = Callee->getName ();
9089 // objc_msgSend stubs must be called, and can't have their address taken.
91- if (Callee->getName ().starts_with (" objc_msgSend$" ))
90+ if (Name.starts_with (" objc_msgSend$" ))
91+ return false ;
92+ // Calls to dtrace probes must generate unique patchpoints.
93+ if (Name.starts_with (" __dtrace" ))
9294 return false ;
9395 }
9496 if (isCalleeOperand (CI, OpIdx) &&
@@ -105,7 +107,8 @@ bool isEligibleFunction(Function *F) {
105107 if (F->isDeclaration ())
106108 return false ;
107109
108- if (F->hasFnAttribute (llvm::Attribute::NoMerge))
110+ if (F->hasFnAttribute (llvm::Attribute::NoMerge) ||
111+ F->hasFnAttribute (llvm::Attribute::AlwaysInline))
109112 return false ;
110113
111114 if (F->hasAvailableExternallyLinkage ())
@@ -552,8 +555,8 @@ bool GlobalMergeFunc::merge(Module &M, const StableFunctionMap *FunctionMap) {
552555 // Check if the matched functions fall into the same (first) module.
553556 // This module check is not strictly necessary as the functions can move
554557 // around. We just want to avoid merging functions from different
555- // modules than the first one in the functon map, as they may not end up
556- // with not being ICFed by the linker.
558+ // modules than the first one in the function map, as they may not end
559+ // up with being ICFed by the linker.
557560 if (MergedModId != *FunctionMap->getNameForId (SF->ModuleNameId )) {
558561 ++NumMismatchedModuleId;
559562 continue ;
@@ -614,30 +617,6 @@ bool GlobalMergeFunc::merge(Module &M, const StableFunctionMap *FunctionMap) {
614617 return Changed;
615618}
616619
617- char GlobalMergeFunc::ID = 0 ;
618- INITIALIZE_PASS_BEGIN (GlobalMergeFunc, " global-merge-func" ,
619- " Global merge function pass" , false , false )
620- INITIALIZE_PASS_END(GlobalMergeFunc, " global-merge-func" ,
621- " Global merge function pass" , false , false )
622-
623- StringRef GlobalMergeFunc::getPassName() const {
624- return " Global Merge Functions" ;
625- }
626-
627- void GlobalMergeFunc::getAnalysisUsage (AnalysisUsage &AU) const {
628- AU.addUsedIfAvailable <ImmutableModuleSummaryIndexWrapperPass>();
629- AU.setPreservesAll ();
630- ModulePass::getAnalysisUsage (AU);
631- }
632-
633- GlobalMergeFunc::GlobalMergeFunc () : ModulePass(ID) {
634- initializeGlobalMergeFuncPass (*llvm::PassRegistry::getPassRegistry ());
635- }
636-
637- namespace llvm {
638- Pass *createGlobalMergeFuncPass () { return new GlobalMergeFunc (); }
639- } // namespace llvm
640-
641620void GlobalMergeFunc::initializeMergerMode (const Module &M) {
642621 // Initialize the local function map regardless of the merger mode.
643622 LocalFunctionMap = std::make_unique<StableFunctionMap>();
@@ -646,14 +625,10 @@ void GlobalMergeFunc::initializeMergerMode(const Module &M) {
646625 if (DisableGlobalMerging)
647626 return ;
648627
649- if (auto *IndexWrapperPass =
650- getAnalysisIfAvailable<ImmutableModuleSummaryIndexWrapperPass>()) {
651- auto *TheIndex = IndexWrapperPass->getIndex ();
652- // (Full)LTO module does not have functions added to the index.
653- // In this case, we run a local merger without using codegen data.
654- if (TheIndex && !TheIndex->hasExportedFunctions (M))
655- return ;
656- }
628+ // (Full)LTO module does not have functions added to the index.
629+ // In this case, we run a local merger without using codegen data.
630+ if (Index && !Index->hasExportedFunctions (M))
631+ return ;
657632
658633 if (cgdata::emitCGData ())
659634 MergerMode = HashFunctionMode::BuildingHashFuncion;
@@ -681,7 +656,7 @@ void GlobalMergeFunc::emitFunctionMap(Module &M) {
681656 Align (4 ));
682657}
683658
684- bool GlobalMergeFunc::runOnModule (Module &M) {
659+ bool GlobalMergeFunc::run (Module &M) {
685660 initializeMergerMode (M);
686661
687662 const StableFunctionMap *FuncMap;
@@ -700,3 +675,58 @@ bool GlobalMergeFunc::runOnModule(Module &M) {
700675
701676 return merge (M, FuncMap);
702677}
678+
679+ namespace {
680+
681+ class GlobalMergeFuncPassWrapper : public ModulePass {
682+
683+ public:
684+ static char ID;
685+
686+ GlobalMergeFuncPassWrapper ();
687+
688+ void getAnalysisUsage (AnalysisUsage &AU) const override {
689+ AU.addUsedIfAvailable <ImmutableModuleSummaryIndexWrapperPass>();
690+ AU.setPreservesAll ();
691+ ModulePass::getAnalysisUsage (AU);
692+ }
693+
694+ StringRef getPassName () const override { return " Global Merge Functions" ; }
695+
696+ bool runOnModule (Module &M) override ;
697+ };
698+
699+ } // namespace
700+
701+ char GlobalMergeFuncPassWrapper::ID = 0 ;
702+ INITIALIZE_PASS_BEGIN (GlobalMergeFuncPassWrapper, " global-merge-func" ,
703+ " Global merge function pass" , false , false )
704+ INITIALIZE_PASS_END(GlobalMergeFuncPassWrapper, " global-merge-func" ,
705+ " Global merge function pass" , false , false )
706+
707+ namespace llvm {
708+ ModulePass *createGlobalMergeFuncPass () {
709+ return new GlobalMergeFuncPassWrapper ();
710+ }
711+ } // namespace llvm
712+
713+ GlobalMergeFuncPassWrapper::GlobalMergeFuncPassWrapper () : ModulePass(ID) {
714+ initializeGlobalMergeFuncPassWrapperPass (
715+ *llvm::PassRegistry::getPassRegistry ());
716+ }
717+
718+ bool GlobalMergeFuncPassWrapper::runOnModule (Module &M) {
719+ const ModuleSummaryIndex *Index = nullptr ;
720+ if (auto *IndexWrapperPass =
721+ getAnalysisIfAvailable<ImmutableModuleSummaryIndexWrapperPass>())
722+ Index = IndexWrapperPass->getIndex ();
723+
724+ return GlobalMergeFunc (Index).run (M);
725+ }
726+
727+ PreservedAnalyses GlobalMergeFuncPass::run (Module &M,
728+ AnalysisManager<Module> &AM) {
729+ ModuleSummaryIndex *Index = &(AM.getResult <ModuleSummaryIndexAnalysis>(M));
730+ bool Changed = GlobalMergeFunc (Index).run (M);
731+ return Changed ? PreservedAnalyses::none () : PreservedAnalyses::all ();
732+ }
0 commit comments