Skip to content

Commit c9fd738

Browse files
authored
[CodeGen] Port DeadMachineInstructionElim to new pass manager (#80582)
A simple enough op pass so we can test standard instrumentations in future.
1 parent 168002e commit c9fd738

File tree

6 files changed

+71
-25
lines changed

6 files changed

+71
-25
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//===- llvm/CodeGen/DeadMachineInstructionElim.h ----------------*- 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+
#ifndef LLVM_CODEGEN_DEADMACHINEINSTRUCTIONELIM_H
10+
#define LLVM_CODEGEN_DEADMACHINEINSTRUCTIONELIM_H
11+
12+
#include "llvm/CodeGen/MachinePassManager.h"
13+
14+
namespace llvm {
15+
16+
class DeadMachineInstructionElimPass
17+
: public MachinePassInfoMixin<DeadMachineInstructionElimPass> {
18+
public:
19+
PreservedAnalyses run(MachineFunction &MF,
20+
MachineFunctionAnalysisManager &MFAM);
21+
};
22+
23+
} // namespace llvm
24+
25+
#endif // LLVM_CODEGEN_DEADMACHINEINSTRUCTIONELIM_H

llvm/include/llvm/Passes/CodeGenPassBuilder.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "llvm/CodeGen/AssignmentTrackingAnalysis.h"
2727
#include "llvm/CodeGen/CallBrPrepare.h"
2828
#include "llvm/CodeGen/CodeGenPrepare.h"
29+
#include "llvm/CodeGen/DeadMachineInstructionElim.h"
2930
#include "llvm/CodeGen/DwarfEHPrepare.h"
3031
#include "llvm/CodeGen/ExpandMemCmp.h"
3132
#include "llvm/CodeGen/ExpandReductions.h"

llvm/include/llvm/Passes/MachinePassRegistry.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ MACHINE_FUNCTION_ANALYSIS("pass-instrumentation", PassInstrumentationAnalysis(PI
123123
#ifndef MACHINE_FUNCTION_PASS
124124
#define MACHINE_FUNCTION_PASS(NAME, CREATE_PASS)
125125
#endif
126+
MACHINE_FUNCTION_PASS("dead-mi-elimination", DeadMachineInstructionElimPass())
126127
// MACHINE_FUNCTION_PASS("free-machine-function", FreeMachineFunctionPass())
127128
MACHINE_FUNCTION_PASS("no-op-machine-function", NoOpMachineFunctionPass())
128129
MACHINE_FUNCTION_PASS("print", PrintMIRPass())
@@ -160,7 +161,6 @@ DUMMY_MACHINE_FUNCTION_PASS("break-false-deps", BreakFalseDepsPass)
160161
DUMMY_MACHINE_FUNCTION_PASS("cfguard-longjmp", CFGuardLongjmpPass)
161162
DUMMY_MACHINE_FUNCTION_PASS("cfi-fixup", CFIFixupPass)
162163
DUMMY_MACHINE_FUNCTION_PASS("cfi-instr-inserter", CFIInstrInserterPass)
163-
DUMMY_MACHINE_FUNCTION_PASS("dead-mi-elimination", DeadMachineInstructionElimPass)
164164
DUMMY_MACHINE_FUNCTION_PASS("detect-dead-lanes", DetectDeadLanesPass)
165165
DUMMY_MACHINE_FUNCTION_PASS("dot-machine-cfg", MachineCFGPrinter)
166166
DUMMY_MACHINE_FUNCTION_PASS("early-ifcvt", EarlyIfConverterPass)

llvm/lib/CodeGen/DeadMachineInstructionElim.cpp

Lines changed: 42 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13+
#include "llvm/CodeGen/DeadMachineInstructionElim.h"
1314
#include "llvm/ADT/PostOrderIterator.h"
1415
#include "llvm/ADT/Statistic.h"
1516
#include "llvm/CodeGen/LiveRegUnits.h"
@@ -28,37 +29,57 @@ using namespace llvm;
2829
STATISTIC(NumDeletes, "Number of dead instructions deleted");
2930

3031
namespace {
31-
class DeadMachineInstructionElim : public MachineFunctionPass {
32-
bool runOnMachineFunction(MachineFunction &MF) override;
32+
class DeadMachineInstructionElimImpl {
33+
const MachineRegisterInfo *MRI = nullptr;
34+
const TargetInstrInfo *TII = nullptr;
35+
LiveRegUnits LivePhysRegs;
3336

34-
const MachineRegisterInfo *MRI = nullptr;
35-
const TargetInstrInfo *TII = nullptr;
36-
LiveRegUnits LivePhysRegs;
37+
public:
38+
bool runImpl(MachineFunction &MF);
3739

38-
public:
39-
static char ID; // Pass identification, replacement for typeid
40-
DeadMachineInstructionElim() : MachineFunctionPass(ID) {
41-
initializeDeadMachineInstructionElimPass(*PassRegistry::getPassRegistry());
42-
}
40+
private:
41+
bool isDead(const MachineInstr *MI) const;
42+
bool eliminateDeadMI(MachineFunction &MF);
43+
};
4344

44-
void getAnalysisUsage(AnalysisUsage &AU) const override {
45-
AU.setPreservesCFG();
46-
MachineFunctionPass::getAnalysisUsage(AU);
47-
}
45+
class DeadMachineInstructionElim : public MachineFunctionPass {
46+
public:
47+
static char ID; // Pass identification, replacement for typeid
4848

49-
private:
50-
bool isDead(const MachineInstr *MI) const;
49+
DeadMachineInstructionElim() : MachineFunctionPass(ID) {
50+
initializeDeadMachineInstructionElimPass(*PassRegistry::getPassRegistry());
51+
}
52+
53+
bool runOnMachineFunction(MachineFunction &MF) override {
54+
if (skipFunction(MF.getFunction()))
55+
return false;
56+
return DeadMachineInstructionElimImpl().runImpl(MF);
57+
}
5158

52-
bool eliminateDeadMI(MachineFunction &MF);
53-
};
59+
void getAnalysisUsage(AnalysisUsage &AU) const override {
60+
AU.setPreservesCFG();
61+
MachineFunctionPass::getAnalysisUsage(AU);
62+
}
63+
};
64+
} // namespace
65+
66+
PreservedAnalyses
67+
DeadMachineInstructionElimPass::run(MachineFunction &MF,
68+
MachineFunctionAnalysisManager &) {
69+
if (!DeadMachineInstructionElimImpl().runImpl(MF))
70+
return PreservedAnalyses::all();
71+
PreservedAnalyses PA;
72+
PA.preserveSet<CFGAnalyses>();
73+
return PA;
5474
}
75+
5576
char DeadMachineInstructionElim::ID = 0;
5677
char &llvm::DeadMachineInstructionElimID = DeadMachineInstructionElim::ID;
5778

5879
INITIALIZE_PASS(DeadMachineInstructionElim, DEBUG_TYPE,
5980
"Remove dead machine instructions", false, false)
6081

61-
bool DeadMachineInstructionElim::isDead(const MachineInstr *MI) const {
82+
bool DeadMachineInstructionElimImpl::isDead(const MachineInstr *MI) const {
6283
// Technically speaking inline asm without side effects and no defs can still
6384
// be deleted. But there is so much bad inline asm code out there, we should
6485
// let them be.
@@ -102,10 +123,7 @@ bool DeadMachineInstructionElim::isDead(const MachineInstr *MI) const {
102123
return true;
103124
}
104125

105-
bool DeadMachineInstructionElim::runOnMachineFunction(MachineFunction &MF) {
106-
if (skipFunction(MF.getFunction()))
107-
return false;
108-
126+
bool DeadMachineInstructionElimImpl::runImpl(MachineFunction &MF) {
109127
MRI = &MF.getRegInfo();
110128

111129
const TargetSubtargetInfo &ST = MF.getSubtarget();
@@ -118,7 +136,7 @@ bool DeadMachineInstructionElim::runOnMachineFunction(MachineFunction &MF) {
118136
return AnyChanges;
119137
}
120138

121-
bool DeadMachineInstructionElim::eliminateDeadMI(MachineFunction &MF) {
139+
bool DeadMachineInstructionElimImpl::eliminateDeadMI(MachineFunction &MF) {
122140
bool AnyChanges = false;
123141

124142
// Loop over all instructions in all blocks, from bottom to top, so that it's

llvm/lib/Passes/PassBuilder.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
#include "llvm/CodeGen/BasicBlockSectionsProfileReader.h"
7777
#include "llvm/CodeGen/CallBrPrepare.h"
7878
#include "llvm/CodeGen/CodeGenPrepare.h"
79+
#include "llvm/CodeGen/DeadMachineInstructionElim.h"
7980
#include "llvm/CodeGen/DwarfEHPrepare.h"
8081
#include "llvm/CodeGen/ExpandLargeDivRem.h"
8182
#include "llvm/CodeGen/ExpandLargeFpConvert.h"

llvm/test/CodeGen/AArch64/elim-dead-mi.mir

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# RUN: llc -mtriple=aarch64 -o - %s \
22
# RUN: -run-pass dead-mi-elimination | FileCheck %s
3+
# RUN: llc -mtriple=aarch64 -o - %s -p dead-mi-elimination | FileCheck %s
34
--- |
45
@c = internal unnamed_addr global [3 x i8] zeroinitializer, align 4
56
@d = common dso_local local_unnamed_addr global i32 0, align 4

0 commit comments

Comments
 (0)