Skip to content

Commit e29f986

Browse files
authored
[CodeGen][NPM] Port RemoveLoadsIntoFakeUses to NPM (#130068)
1 parent ed96e46 commit e29f986

File tree

8 files changed

+74
-12
lines changed

8 files changed

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

llvm/include/llvm/InitializePasses.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ void initializeRegionOnlyViewerPass(PassRegistry &);
267267
void initializeRegionPrinterPass(PassRegistry &);
268268
void initializeRegionViewerPass(PassRegistry &);
269269
void initializeRegisterCoalescerLegacyPass(PassRegistry &);
270-
void initializeRemoveLoadsIntoFakeUsesPass(PassRegistry &);
270+
void initializeRemoveLoadsIntoFakeUsesLegacyPass(PassRegistry &);
271271
void initializeRemoveRedundantDebugValuesLegacyPass(PassRegistry &);
272272
void initializeRenameIndependentSubregsLegacyPass(PassRegistry &);
273273
void initializeReplaceWithVeclibLegacyPass(PassRegistry &);

llvm/include/llvm/Passes/CodeGenPassBuilder.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
#include "llvm/CodeGen/RegUsageInfoPropagate.h"
7575
#include "llvm/CodeGen/RegisterCoalescerPass.h"
7676
#include "llvm/CodeGen/RegisterUsageInfo.h"
77+
#include "llvm/CodeGen/RemoveLoadsIntoFakeUses.h"
7778
#include "llvm/CodeGen/RemoveRedundantDebugValues.h"
7879
#include "llvm/CodeGen/RenameIndependentSubregs.h"
7980
#include "llvm/CodeGen/ReplaceWithVeclib.h"
@@ -1003,6 +1004,7 @@ Error CodeGenPassBuilder<Derived, TargetMachineT>::addMachinePasses(
10031004

10041005
addPass(FuncletLayoutPass());
10051006

1007+
addPass(RemoveLoadsIntoFakeUsesPass());
10061008
addPass(StackMapLivenessPass());
10071009
addPass(LiveDebugValuesPass(
10081010
getTM<TargetMachine>().Options.ShouldEmitDebugEntryValues()));

llvm/include/llvm/Passes/MachinePassRegistry.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ MACHINE_FUNCTION_PASS("reg-usage-collector", RegUsageInfoCollectorPass())
182182
MACHINE_FUNCTION_PASS("reg-usage-propagation", RegUsageInfoPropagationPass())
183183
MACHINE_FUNCTION_PASS("register-coalescer", RegisterCoalescerPass())
184184
MACHINE_FUNCTION_PASS("rename-independent-subregs", RenameIndependentSubregsPass())
185+
MACHINE_FUNCTION_PASS("remove-loads-into-fake-uses", RemoveLoadsIntoFakeUsesPass())
185186
MACHINE_FUNCTION_PASS("remove-redundant-debug-values", RemoveRedundantDebugValuesPass())
186187
MACHINE_FUNCTION_PASS("require-all-machine-function-properties",
187188
RequireAllMachineFunctionPropertiesPass())
@@ -311,7 +312,6 @@ DUMMY_MACHINE_FUNCTION_PASS("ra-pbqp", RAPBQPPass)
311312
DUMMY_MACHINE_FUNCTION_PASS("regalloc", RegAllocPass)
312313
DUMMY_MACHINE_FUNCTION_PASS("regallocscoringpass", RegAllocScoringPass)
313314
DUMMY_MACHINE_FUNCTION_PASS("regbankselect", RegBankSelectPass)
314-
DUMMY_MACHINE_FUNCTION_PASS("remove-loads-into-fake-uses", RemoveLoadsIntoFakeUsesPass)
315315
DUMMY_MACHINE_FUNCTION_PASS("reset-machine-function", ResetMachineFunctionPass)
316316
DUMMY_MACHINE_FUNCTION_PASS("shrink-wrap", ShrinkWrapPass)
317317
DUMMY_MACHINE_FUNCTION_PASS("stack-frame-layout", StackFrameLayoutAnalysisPass)

llvm/lib/CodeGen/CodeGen.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ void llvm::initializeCodeGen(PassRegistry &Registry) {
117117
initializeRegUsageInfoCollectorLegacyPass(Registry);
118118
initializeRegUsageInfoPropagationLegacyPass(Registry);
119119
initializeRegisterCoalescerLegacyPass(Registry);
120-
initializeRemoveLoadsIntoFakeUsesPass(Registry);
120+
initializeRemoveLoadsIntoFakeUsesLegacyPass(Registry);
121121
initializeRemoveRedundantDebugValuesLegacyPass(Registry);
122122
initializeRenameIndependentSubregsLegacyPass(Registry);
123123
initializeSafeStackLegacyPassPass(Registry);

llvm/lib/CodeGen/RemoveLoadsIntoFakeUses.cpp

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,13 @@
2222
///
2323
//===----------------------------------------------------------------------===//
2424

25+
#include "llvm/CodeGen/RemoveLoadsIntoFakeUses.h"
2526
#include "llvm/ADT/PostOrderIterator.h"
2627
#include "llvm/ADT/Statistic.h"
2728
#include "llvm/CodeGen/LiveRegUnits.h"
2829
#include "llvm/CodeGen/MachineFunction.h"
2930
#include "llvm/CodeGen/MachineFunctionPass.h"
31+
#include "llvm/CodeGen/MachinePassManager.h"
3032
#include "llvm/CodeGen/MachineRegisterInfo.h"
3133
#include "llvm/CodeGen/TargetSubtargetInfo.h"
3234
#include "llvm/IR/Function.h"
@@ -41,12 +43,13 @@ using namespace llvm;
4143
STATISTIC(NumLoadsDeleted, "Number of dead load instructions deleted");
4244
STATISTIC(NumFakeUsesDeleted, "Number of FAKE_USE instructions deleted");
4345

44-
class RemoveLoadsIntoFakeUses : public MachineFunctionPass {
46+
class RemoveLoadsIntoFakeUsesLegacy : public MachineFunctionPass {
4547
public:
4648
static char ID;
4749

48-
RemoveLoadsIntoFakeUses() : MachineFunctionPass(ID) {
49-
initializeRemoveLoadsIntoFakeUsesPass(*PassRegistry::getPassRegistry());
50+
RemoveLoadsIntoFakeUsesLegacy() : MachineFunctionPass(ID) {
51+
initializeRemoveLoadsIntoFakeUsesLegacyPass(
52+
*PassRegistry::getPassRegistry());
5053
}
5154

5255
void getAnalysisUsage(AnalysisUsage &AU) const override {
@@ -66,21 +69,45 @@ class RemoveLoadsIntoFakeUses : public MachineFunctionPass {
6669
bool runOnMachineFunction(MachineFunction &MF) override;
6770
};
6871

69-
char RemoveLoadsIntoFakeUses::ID = 0;
70-
char &llvm::RemoveLoadsIntoFakeUsesID = RemoveLoadsIntoFakeUses::ID;
72+
struct RemoveLoadsIntoFakeUses {
73+
bool run(MachineFunction &MF);
74+
};
75+
76+
char RemoveLoadsIntoFakeUsesLegacy::ID = 0;
77+
char &llvm::RemoveLoadsIntoFakeUsesID = RemoveLoadsIntoFakeUsesLegacy::ID;
7178

72-
INITIALIZE_PASS_BEGIN(RemoveLoadsIntoFakeUses, DEBUG_TYPE,
79+
INITIALIZE_PASS_BEGIN(RemoveLoadsIntoFakeUsesLegacy, DEBUG_TYPE,
7380
"Remove Loads Into Fake Uses", false, false)
74-
INITIALIZE_PASS_END(RemoveLoadsIntoFakeUses, DEBUG_TYPE,
81+
INITIALIZE_PASS_END(RemoveLoadsIntoFakeUsesLegacy, DEBUG_TYPE,
7582
"Remove Loads Into Fake Uses", false, false)
7683

77-
bool RemoveLoadsIntoFakeUses::runOnMachineFunction(MachineFunction &MF) {
84+
bool RemoveLoadsIntoFakeUsesLegacy::runOnMachineFunction(MachineFunction &MF) {
85+
if (skipFunction(MF.getFunction()))
86+
return false;
87+
88+
return RemoveLoadsIntoFakeUses().run(MF);
89+
}
90+
91+
PreservedAnalyses
92+
RemoveLoadsIntoFakeUsesPass::run(MachineFunction &MF,
93+
MachineFunctionAnalysisManager &MFAM) {
94+
MFPropsModifier _(*this, MF);
95+
96+
if (!RemoveLoadsIntoFakeUses().run(MF))
97+
return PreservedAnalyses::all();
98+
99+
auto PA = getMachineFunctionPassPreservedAnalyses();
100+
PA.preserveSet<CFGAnalyses>();
101+
return PA;
102+
}
103+
104+
bool RemoveLoadsIntoFakeUses::run(MachineFunction &MF) {
78105
// Skip this pass if we would use VarLoc-based LDV, as there may be DBG_VALUE
79106
// instructions of the restored values that would become invalid.
80107
if (!MF.useDebugInstrRef())
81108
return false;
82109
// Only run this for functions that have fake uses.
83-
if (!MF.hasFakeUses() || skipFunction(MF.getFunction()))
110+
if (!MF.hasFakeUses())
84111
return false;
85112

86113
bool AnyChanges = false;

llvm/lib/Passes/PassBuilder.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@
149149
#include "llvm/CodeGen/RegUsageInfoPropagate.h"
150150
#include "llvm/CodeGen/RegisterCoalescerPass.h"
151151
#include "llvm/CodeGen/RegisterUsageInfo.h"
152+
#include "llvm/CodeGen/RemoveLoadsIntoFakeUses.h"
152153
#include "llvm/CodeGen/RemoveRedundantDebugValues.h"
153154
#include "llvm/CodeGen/RenameIndependentSubregs.h"
154155
#include "llvm/CodeGen/SafeStack.h"

llvm/test/CodeGen/X86/fake-use-remove-loads.mir

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
# remove-loads-into-fake-uses pass, and that if the function does not use
44
# instruction referencing then no changes are made.
55
# RUN: llc %s -run-pass remove-loads-into-fake-uses -mtriple=x86_64-unknown-linux -debug-only=remove-loads-into-fake-uses 2>&1 -o - | FileCheck %s
6+
# RUN: llc %s -passes remove-loads-into-fake-uses -mtriple=x86_64-unknown-linux -debug-only=remove-loads-into-fake-uses 2>&1 -o - | FileCheck %s
7+
68
# REQUIRES: asserts
79
#
810
## We verify that:

0 commit comments

Comments
 (0)