Skip to content

Commit 4313345

Browse files
authored
[CodeGen][NewPM] Port MachineCopyPropagation to NPM (#125202)
1 parent 2f2ac3d commit 4313345

12 files changed

+91
-27
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//===- llvm/CodeGen/MachineCopyPropagation.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_MACHINECOPYPROPAGATION_H
10+
#define LLVM_CODEGEN_MACHINECOPYPROPAGATION_H
11+
12+
#include "llvm/CodeGen/MachinePassManager.h"
13+
14+
namespace llvm {
15+
16+
class MachineCopyPropagationPass
17+
: public PassInfoMixin<MachineCopyPropagationPass> {
18+
bool UseCopyInstr;
19+
20+
public:
21+
MachineCopyPropagationPass(bool UseCopyInstr = false)
22+
: UseCopyInstr(UseCopyInstr) {}
23+
24+
PreservedAnalyses run(MachineFunction &MF,
25+
MachineFunctionAnalysisManager &MFAM);
26+
27+
MachineFunctionProperties getRequiredProperties() const {
28+
return MachineFunctionProperties().set(
29+
MachineFunctionProperties::Property::NoVRegs);
30+
}
31+
};
32+
33+
} // namespace llvm
34+
35+
#endif // LLVM_CODEGEN_MACHINECOPYPROPAGATION_H

llvm/include/llvm/InitializePasses.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ void initializeMachineBranchProbabilityInfoWrapperPassPass(PassRegistry &);
192192
void initializeMachineCFGPrinterPass(PassRegistry &);
193193
void initializeMachineCSELegacyPass(PassRegistry &);
194194
void initializeMachineCombinerPass(PassRegistry &);
195-
void initializeMachineCopyPropagationPass(PassRegistry &);
195+
void initializeMachineCopyPropagationLegacyPass(PassRegistry &);
196196
void initializeMachineCycleInfoPrinterPassPass(PassRegistry &);
197197
void initializeMachineCycleInfoWrapperPassPass(PassRegistry &);
198198
void initializeMachineDominanceFrontierPass(PassRegistry &);

llvm/include/llvm/Passes/CodeGenPassBuilder.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
#include "llvm/CodeGen/LowerEmuTLS.h"
4646
#include "llvm/CodeGen/MIRPrinter.h"
4747
#include "llvm/CodeGen/MachineCSE.h"
48+
#include "llvm/CodeGen/MachineCopyPropagation.h"
4849
#include "llvm/CodeGen/MachineFunctionAnalysis.h"
4950
#include "llvm/CodeGen/MachineLICM.h"
5051
#include "llvm/CodeGen/MachineModuleInfo.h"

llvm/include/llvm/Passes/MachinePassRegistry.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ MACHINE_FUNCTION_PASS("early-machinelicm", EarlyMachineLICMPass())
140140
MACHINE_FUNCTION_PASS("early-tailduplication", EarlyTailDuplicatePass())
141141
MACHINE_FUNCTION_PASS("finalize-isel", FinalizeISelPass())
142142
MACHINE_FUNCTION_PASS("localstackalloc", LocalStackSlotAllocationPass())
143+
MACHINE_FUNCTION_PASS("machine-cp", MachineCopyPropagationPass())
143144
MACHINE_FUNCTION_PASS("machine-cse", MachineCSEPass())
144145
MACHINE_FUNCTION_PASS("machinelicm", MachineLICMPass())
145146
MACHINE_FUNCTION_PASS("no-op-machine-function", NoOpMachineFunctionPass())
@@ -235,7 +236,6 @@ DUMMY_MACHINE_FUNCTION_PASS("legalizer", LegalizerPass)
235236
DUMMY_MACHINE_FUNCTION_PASS("livedebugvalues", LiveDebugValuesPass)
236237
DUMMY_MACHINE_FUNCTION_PASS("lrshrink", LiveRangeShrinkPass)
237238
DUMMY_MACHINE_FUNCTION_PASS("machine-combiner", MachineCombinerPass)
238-
DUMMY_MACHINE_FUNCTION_PASS("machine-cp", MachineCopyPropagationPass)
239239
DUMMY_MACHINE_FUNCTION_PASS("static-data-splitter", StaticDataSplitter)
240240
DUMMY_MACHINE_FUNCTION_PASS("machine-function-splitter", MachineFunctionSplitterPass)
241241
DUMMY_MACHINE_FUNCTION_PASS("machine-latecleanup", MachineLateInstrsCleanupPass)

llvm/lib/CodeGen/CodeGen.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ void llvm::initializeCodeGen(PassRegistry &Registry) {
7777
initializeMachineCFGPrinterPass(Registry);
7878
initializeMachineCSELegacyPass(Registry);
7979
initializeMachineCombinerPass(Registry);
80-
initializeMachineCopyPropagationPass(Registry);
80+
initializeMachineCopyPropagationLegacyPass(Registry);
8181
initializeMachineCycleInfoPrinterPassPass(Registry);
8282
initializeMachineCycleInfoWrapperPassPass(Registry);
8383
initializeMachineDominatorTreeWrapperPassPass(Registry);

llvm/lib/CodeGen/MachineCopyPropagation.cpp

Lines changed: 46 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
//
4949
//===----------------------------------------------------------------------===//
5050

51+
#include "llvm/CodeGen/MachineCopyPropagation.h"
5152
#include "llvm/ADT/DenseMap.h"
5253
#include "llvm/ADT/STLExtras.h"
5354
#include "llvm/ADT/SetVector.h"
@@ -449,7 +450,7 @@ class CopyTracker {
449450
}
450451
};
451452

452-
class MachineCopyPropagation : public MachineFunctionPass {
453+
class MachineCopyPropagation {
453454
const TargetRegisterInfo *TRI = nullptr;
454455
const TargetInstrInfo *TII = nullptr;
455456
const MachineRegisterInfo *MRI = nullptr;
@@ -461,21 +462,9 @@ class MachineCopyPropagation : public MachineFunctionPass {
461462
static char ID; // Pass identification, replacement for typeid
462463

463464
MachineCopyPropagation(bool CopyInstr = false)
464-
: MachineFunctionPass(ID), UseCopyInstr(CopyInstr || MCPUseCopyInstr) {
465-
initializeMachineCopyPropagationPass(*PassRegistry::getPassRegistry());
466-
}
465+
: UseCopyInstr(CopyInstr || MCPUseCopyInstr) {}
467466

468-
void getAnalysisUsage(AnalysisUsage &AU) const override {
469-
AU.setPreservesCFG();
470-
MachineFunctionPass::getAnalysisUsage(AU);
471-
}
472-
473-
bool runOnMachineFunction(MachineFunction &MF) override;
474-
475-
MachineFunctionProperties getRequiredProperties() const override {
476-
return MachineFunctionProperties().set(
477-
MachineFunctionProperties::Property::NoVRegs);
478-
}
467+
bool run(MachineFunction &MF);
479468

480469
private:
481470
typedef enum { DebugUse = false, RegularUse = true } DebugType;
@@ -510,13 +499,35 @@ class MachineCopyPropagation : public MachineFunctionPass {
510499
bool Changed = false;
511500
};
512501

502+
class MachineCopyPropagationLegacy : public MachineFunctionPass {
503+
bool UseCopyInstr;
504+
505+
public:
506+
static char ID; // pass identification
507+
508+
MachineCopyPropagationLegacy(bool UseCopyInstr = false)
509+
: MachineFunctionPass(ID), UseCopyInstr(UseCopyInstr) {}
510+
511+
void getAnalysisUsage(AnalysisUsage &AU) const override {
512+
AU.setPreservesCFG();
513+
MachineFunctionPass::getAnalysisUsage(AU);
514+
}
515+
516+
bool runOnMachineFunction(MachineFunction &MF) override;
517+
518+
MachineFunctionProperties getRequiredProperties() const override {
519+
return MachineFunctionProperties().set(
520+
MachineFunctionProperties::Property::NoVRegs);
521+
}
522+
};
523+
513524
} // end anonymous namespace
514525

515-
char MachineCopyPropagation::ID = 0;
526+
char MachineCopyPropagationLegacy::ID = 0;
516527

517-
char &llvm::MachineCopyPropagationID = MachineCopyPropagation::ID;
528+
char &llvm::MachineCopyPropagationID = MachineCopyPropagationLegacy::ID;
518529

519-
INITIALIZE_PASS(MachineCopyPropagation, DEBUG_TYPE,
530+
INITIALIZE_PASS(MachineCopyPropagationLegacy, DEBUG_TYPE,
520531
"Machine Copy Propagation Pass", false, false)
521532

522533
void MachineCopyPropagation::ReadRegister(MCRegister Reg, MachineInstr &Reader,
@@ -1563,10 +1574,25 @@ void MachineCopyPropagation::EliminateSpillageCopies(MachineBasicBlock &MBB) {
15631574
Tracker.clear();
15641575
}
15651576

1566-
bool MachineCopyPropagation::runOnMachineFunction(MachineFunction &MF) {
1577+
bool MachineCopyPropagationLegacy::runOnMachineFunction(MachineFunction &MF) {
15671578
if (skipFunction(MF.getFunction()))
15681579
return false;
15691580

1581+
return MachineCopyPropagation(UseCopyInstr).run(MF);
1582+
}
1583+
1584+
PreservedAnalyses
1585+
MachineCopyPropagationPass::run(MachineFunction &MF,
1586+
MachineFunctionAnalysisManager &) {
1587+
MFPropsModifier _(*this, MF);
1588+
if (!MachineCopyPropagation(UseCopyInstr).run(MF))
1589+
return PreservedAnalyses::all();
1590+
auto PA = getMachineFunctionPassPreservedAnalyses();
1591+
PA.preserveSet<CFGAnalyses>();
1592+
return PA;
1593+
}
1594+
1595+
bool MachineCopyPropagation::run(MachineFunction &MF) {
15701596
bool isSpillageCopyElimEnabled = false;
15711597
switch (EnableSpillageCopyElimination) {
15721598
case cl::BOU_UNSET:
@@ -1599,5 +1625,5 @@ bool MachineCopyPropagation::runOnMachineFunction(MachineFunction &MF) {
15991625

16001626
MachineFunctionPass *
16011627
llvm::createMachineCopyPropagationPass(bool UseCopyInstr = false) {
1602-
return new MachineCopyPropagation(UseCopyInstr);
1628+
return new MachineCopyPropagationLegacy(UseCopyInstr);
16031629
}

llvm/lib/Passes/PassBuilder.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@
110110
#include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
111111
#include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
112112
#include "llvm/CodeGen/MachineCSE.h"
113+
#include "llvm/CodeGen/MachineCopyPropagation.h"
113114
#include "llvm/CodeGen/MachineDominators.h"
114115
#include "llvm/CodeGen/MachineFunctionAnalysis.h"
115116
#include "llvm/CodeGen/MachineLICM.h"

llvm/test/CodeGen/AArch64/avoid-zero-copy.mir

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Check that we can remove the redundant save of constant registers such as $wzr
22
# RUN: llc -mtriple=aarch64-unknown-linux %s -verify-machineinstrs -start-before=machine-cp -o - | FileCheck %s --check-prefix ASM
33
# RUN: llc -mtriple=aarch64-unknown-linux %s -verify-machineinstrs -run-pass=machine-cp -o - | FileCheck %s
4+
5+
# RUN: llc -mtriple=aarch64-unknown-linux %s -passes=machine-cp -o - | FileCheck %s
46
--- |
57
target triple = "aarch64-unknown-linux"
68
declare i32 @bar(i32) nounwind

llvm/test/CodeGen/AMDGPU/dead_copy.mir

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# RUN: llc -o - %s -mtriple=amdgcn -mcpu=fiji -run-pass=machine-cp -verify-machineinstrs | FileCheck -check-prefix=GCN %s
2+
# RUN: llc -o - %s -mtriple=amdgcn -mcpu=fiji -passes=machine-cp | FileCheck -check-prefix=GCN %s
23

34
# GCN-LABEL: dead_copy
45
# GCN: bb.0

llvm/test/CodeGen/AMDGPU/remove-incompatible-s-time.ll

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
; RUN: llc -enable-new-pm -mtriple=amdgcn -mcpu=gfx1030 -stop-after=amdgpu-remove-incompatible-functions\
88
; RUN: -pass-remarks=amdgpu-remove-incompatible-functions %s -o - 2>%t | FileCheck -check-prefixes=COMPATIBLE,REALTIME,MEMTIME %s
99
; RUN: FileCheck -allow-empty --check-prefixes=WARN-REALTIME,WARN-MEMTIME %s < %t
10-
; RUN: llc -enable-new-pm -mtriple=amdgcn -mcpu=gfx1030 -verify-machineinstrs < %s
1110

1211
; RUN: llc -mtriple=amdgcn -mcpu=gfx1102 -stop-after=amdgpu-remove-incompatible-functions\
1312
; RUN: -pass-remarks=amdgpu-remove-incompatible-functions %s -o - 2>%t | FileCheck -check-prefixes=INCOMPATIBLE,NOREALTIME,NOMEMTIME %s
@@ -17,7 +16,6 @@
1716
; RUN: llc -enable-new-pm -mtriple=amdgcn -mcpu=gfx1102 -stop-after=amdgpu-remove-incompatible-functions\
1817
; RUN: -pass-remarks=amdgpu-remove-incompatible-functions %s -o - 2>%t | FileCheck -check-prefixes=INCOMPATIBLE,NOREALTIME,NOMEMTIME %s
1918
; RUN: FileCheck --check-prefixes=WARN-NOREALTIME,WARN-NOMEMTIME %s < %t
20-
; RUN: llc -enable-new-pm -mtriple=amdgcn -mcpu=gfx1102 -verify-machineinstrs < %s
2119

2220
; Note: This test checks the IR, but also has a run line to codegen the file just to check we
2321
; do not crash when trying to select those functions.

llvm/test/CodeGen/AMDGPU/remove-incompatible-wave32-feature.ll

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,13 @@
1414

1515
; RUN: llc -enable-new-pm -mtriple=amdgcn -mcpu=gfx1011 -mattr=-wavefrontsize32,+wavefrontsize64 -stop-after=amdgpu-remove-incompatible-functions\
1616
; RUN: -pass-remarks=amdgpu-remove-incompatible-functions < %s 2>%t | FileCheck -check-prefixes=GFX10 %s
17-
; RUN: llc -enable-new-pm -mtriple=amdgcn -mcpu=gfx1011 -mattr=-wavefrontsize32,+wavefrontsize64 -verify-machineinstrs < %s
1817

1918
; RUN: llc -mtriple=amdgcn -mcpu=gfx1100 -mattr=-wavefrontsize32,+wavefrontsize64 -stop-after=amdgpu-remove-incompatible-functions\
2019
; RUN: -pass-remarks=amdgpu-remove-incompatible-functions < %s 2>%t | FileCheck -check-prefixes=GFX11 %s
2120
; RUN: llc -mtriple=amdgcn -mcpu=gfx1100 -mattr=-wavefrontsize32,+wavefrontsize64 -verify-machineinstrs < %s
2221

2322
; RUN: llc -enable-new-pm -mtriple=amdgcn -mcpu=gfx1100 -mattr=-wavefrontsize32,+wavefrontsize64 -stop-after=amdgpu-remove-incompatible-functions\
2423
; RUN: -pass-remarks=amdgpu-remove-incompatible-functions < %s 2>%t | FileCheck -check-prefixes=GFX11 %s
25-
; RUN: llc -enable-new-pm -mtriple=amdgcn -mcpu=gfx1100 -mattr=-wavefrontsize32,+wavefrontsize64 -verify-machineinstrs < %s
2624

2725
; WARN-GFX906: removing function 'needs_wavefrontsize32': +wavefrontsize32 is not supported on the current target
2826
; WARN-GFX906-NOT: not supported

llvm/test/CodeGen/ARM/machine-copyprop.mir

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py
22
# RUN: llc -o - %s -mtriple=armv7s-- -run-pass=machine-cp | FileCheck %s
3+
4+
# RUN: llc -o - %s -mtriple=armv7s-- -passes=machine-cp | FileCheck %s
35
---
46
# Test that machine copy prop recognizes the implicit-def operands on a COPY
57
# as clobbering the register.

0 commit comments

Comments
 (0)