Skip to content

Commit 4103318

Browse files
committed
[NewPM] Port MergeFunctions pass
This ports the MergeFunctions pass to the NewPM. This was rather straightforward, as no analyses are used. Additionally MergeFunctions needs to be conditionally enabled in the PassBuilder, but I left that part out of this patch. Differential Revision: https://reviews.llvm.org/D72537
1 parent 48bad08 commit 4103318

File tree

7 files changed

+70
-16
lines changed

7 files changed

+70
-16
lines changed

llvm/include/llvm/InitializePasses.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ void initializeMemoryDependenceWrapperPassPass(PassRegistry&);
288288
void initializeMemorySSAPrinterLegacyPassPass(PassRegistry&);
289289
void initializeMemorySSAWrapperPassPass(PassRegistry&);
290290
void initializeMemorySanitizerLegacyPassPass(PassRegistry&);
291-
void initializeMergeFunctionsPass(PassRegistry&);
291+
void initializeMergeFunctionsLegacyPassPass(PassRegistry&);
292292
void initializeMergeICmpsLegacyPassPass(PassRegistry &);
293293
void initializeMergedLoadStoreMotionLegacyPassPass(PassRegistry&);
294294
void initializeMetaRenamerPass(PassRegistry&);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//===- MergeFunctions.h - Merge Identical 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 transforms simple global variables that never have their address
10+
// taken. If obviously true, it marks read/write globals as constant, deletes
11+
// variables only stored to, etc.
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
#ifndef LLVM_TRANSFORMS_IPO_MERGEFUNCTIONS_H
16+
#define LLVM_TRANSFORMS_IPO_MERGEFUNCTIONS_H
17+
18+
#include "llvm/IR/PassManager.h"
19+
20+
namespace llvm {
21+
22+
class Module;
23+
24+
/// Merge identical functions.
25+
class MergeFunctionsPass : public PassInfoMixin<MergeFunctionsPass> {
26+
public:
27+
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);
28+
};
29+
30+
} // end namespace llvm
31+
32+
#endif // LLVM_TRANSFORMS_IPO_MERGEFUNCTIONS_H

llvm/lib/Passes/PassBuilder.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@
8686
#include "llvm/Transforms/IPO/Inliner.h"
8787
#include "llvm/Transforms/IPO/Internalize.h"
8888
#include "llvm/Transforms/IPO/LowerTypeTests.h"
89+
#include "llvm/Transforms/IPO/MergeFunctions.h"
8990
#include "llvm/Transforms/IPO/PartialInlining.h"
9091
#include "llvm/Transforms/IPO/SCCP.h"
9192
#include "llvm/Transforms/IPO/SampleProfile.h"

llvm/lib/Passes/PassRegistry.def

+1
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ MODULE_PASS("internalize", InternalizePass())
6464
MODULE_PASS("invalidate<all>", InvalidateAllAnalysesPass())
6565
MODULE_PASS("ipsccp", IPSCCPPass())
6666
MODULE_PASS("lowertypetests", LowerTypeTestsPass(nullptr, nullptr))
67+
MODULE_PASS("mergefunc", MergeFunctionsPass())
6768
MODULE_PASS("name-anon-globals", NameAnonGlobalPass())
6869
MODULE_PASS("no-op-module", NoOpModulePass())
6970
MODULE_PASS("partial-inliner", PartialInlinerPass())

llvm/lib/Transforms/IPO/IPO.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ void llvm::initializeIPO(PassRegistry &Registry) {
4343
initializeBlockExtractorPass(Registry);
4444
initializeSingleLoopExtractorPass(Registry);
4545
initializeLowerTypeTestsPass(Registry);
46-
initializeMergeFunctionsPass(Registry);
46+
initializeMergeFunctionsLegacyPassPass(Registry);
4747
initializePartialInlinerLegacyPassPass(Registry);
4848
initializeAttributorLegacyPassPass(Registry);
4949
initializePostOrderFunctionAttrsLegacyPassPass(Registry);

llvm/lib/Transforms/IPO/MergeFunctions.cpp

+33-14
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@
122122
#include "llvm/Support/Debug.h"
123123
#include "llvm/Support/raw_ostream.h"
124124
#include "llvm/Transforms/IPO.h"
125+
#include "llvm/Transforms/IPO/MergeFunctions.h"
125126
#include "llvm/Transforms/Utils/FunctionComparator.h"
126127
#include <algorithm>
127128
#include <cassert>
@@ -196,16 +197,12 @@ class FunctionNode {
196197
/// by considering all pointer types to be equivalent. Once identified,
197198
/// MergeFunctions will fold them by replacing a call to one to a call to a
198199
/// bitcast of the other.
199-
class MergeFunctions : public ModulePass {
200+
class MergeFunctions {
200201
public:
201-
static char ID;
202-
203-
MergeFunctions()
204-
: ModulePass(ID), FnTree(FunctionNodeCmp(&GlobalNumbers)) {
205-
initializeMergeFunctionsPass(*PassRegistry::getPassRegistry());
202+
MergeFunctions() : FnTree(FunctionNodeCmp(&GlobalNumbers)) {
206203
}
207204

208-
bool runOnModule(Module &M) override;
205+
bool runOnModule(Module &M);
209206

210207
private:
211208
// The function comparison operator is provided here so that FunctionNodes do
@@ -298,14 +295,39 @@ class MergeFunctions : public ModulePass {
298295
DenseMap<AssertingVH<Function>, FnTreeType::iterator> FNodesInTree;
299296
};
300297

301-
} // end anonymous namespace
298+
class MergeFunctionsLegacyPass : public ModulePass {
299+
public:
300+
static char ID;
302301

303-
char MergeFunctions::ID = 0;
302+
MergeFunctionsLegacyPass(): ModulePass(ID) {
303+
initializeMergeFunctionsLegacyPassPass(*PassRegistry::getPassRegistry());
304+
}
304305

305-
INITIALIZE_PASS(MergeFunctions, "mergefunc", "Merge Functions", false, false)
306+
bool runOnModule(Module &M) override {
307+
if (skipModule(M))
308+
return false;
309+
310+
MergeFunctions MF;
311+
return MF.runOnModule(M);
312+
}
313+
};
314+
315+
} // end anonymous namespace
316+
317+
char MergeFunctionsLegacyPass::ID = 0;
318+
INITIALIZE_PASS(MergeFunctionsLegacyPass, "mergefunc",
319+
"Merge Functions", false, false)
306320

307321
ModulePass *llvm::createMergeFunctionsPass() {
308-
return new MergeFunctions();
322+
return new MergeFunctionsLegacyPass();
323+
}
324+
325+
PreservedAnalyses MergeFunctionsPass::run(Module &M,
326+
ModuleAnalysisManager &AM) {
327+
MergeFunctions MF;
328+
if (!MF.runOnModule(M))
329+
return PreservedAnalyses::all();
330+
return PreservedAnalyses::none();
309331
}
310332

311333
#ifndef NDEBUG
@@ -387,9 +409,6 @@ static bool isEligibleForMerging(Function &F) {
387409
}
388410

389411
bool MergeFunctions::runOnModule(Module &M) {
390-
if (skipModule(M))
391-
return false;
392-
393412
bool Changed = false;
394413

395414
// All functions in the module, ordered by hash. Functions with a unique

llvm/test/Transforms/MergeFunc/merge-block-address.ll

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
; RUN: opt -S -mergefunc < %s | FileCheck %s
2+
; RUN: opt -S -passes=mergefunc < %s | FileCheck %s
23

34
; These two functions are identical. The basic block labels are the same, and
45
; induce the same CFG. We are testing that block addresses within different

0 commit comments

Comments
 (0)