Skip to content

Commit 39b2841

Browse files
committed
[CodeGen][NewPM] Port RegisterCoalescer to NPM
1 parent 0063b31 commit 39b2841

14 files changed

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

llvm/include/llvm/InitializePasses.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ void initializeRegionOnlyPrinterPass(PassRegistry &);
266266
void initializeRegionOnlyViewerPass(PassRegistry &);
267267
void initializeRegionPrinterPass(PassRegistry &);
268268
void initializeRegionViewerPass(PassRegistry &);
269-
void initializeRegisterCoalescerPass(PassRegistry &);
269+
void initializeRegisterCoalescerLegacyPass(PassRegistry &);
270270
void initializeRemoveLoadsIntoFakeUsesPass(PassRegistry &);
271271
void initializeRemoveRedundantDebugValuesPass(PassRegistry &);
272272
void initializeRenameIndependentSubregsPass(PassRegistry &);

llvm/include/llvm/Passes/CodeGenPassBuilder.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
#include "llvm/CodeGen/RegAllocFast.h"
5858
#include "llvm/CodeGen/RegUsageInfoCollector.h"
5959
#include "llvm/CodeGen/RegUsageInfoPropagate.h"
60+
#include "llvm/CodeGen/RegisterCoalescerPass.h"
6061
#include "llvm/CodeGen/RegisterUsageInfo.h"
6162
#include "llvm/CodeGen/ReplaceWithVeclib.h"
6263
#include "llvm/CodeGen/SafeStack.h"

llvm/include/llvm/Passes/MachinePassRegistry.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ MACHINE_FUNCTION_PASS("print<slot-indexes>", SlotIndexesPrinterPass(errs()))
164164
MACHINE_FUNCTION_PASS("print<virtregmap>", VirtRegMapPrinterPass(errs()))
165165
MACHINE_FUNCTION_PASS("reg-usage-collector", RegUsageInfoCollectorPass())
166166
MACHINE_FUNCTION_PASS("reg-usage-propagation", RegUsageInfoPropagationPass())
167+
MACHINE_FUNCTION_PASS("register-coalescer", RegisterCoalescerPass())
167168
MACHINE_FUNCTION_PASS("require-all-machine-function-properties",
168169
RequireAllMachineFunctionPropertiesPass())
169170
MACHINE_FUNCTION_PASS("stack-coloring", StackColoringPass())
@@ -265,7 +266,6 @@ DUMMY_MACHINE_FUNCTION_PASS("removeredundantdebugvalues", RemoveRedundantDebugVa
265266
DUMMY_MACHINE_FUNCTION_PASS("rename-independent-subregs", RenameIndependentSubregsPass)
266267
DUMMY_MACHINE_FUNCTION_PASS("reset-machine-function", ResetMachineFunctionPass)
267268
DUMMY_MACHINE_FUNCTION_PASS("shrink-wrap", ShrinkWrapPass)
268-
DUMMY_MACHINE_FUNCTION_PASS("simple-register-coalescing", RegisterCoalescerPass)
269269
DUMMY_MACHINE_FUNCTION_PASS("stack-frame-layout", StackFrameLayoutAnalysisPass)
270270
DUMMY_MACHINE_FUNCTION_PASS("stack-slot-coloring", StackSlotColoringPass)
271271
DUMMY_MACHINE_FUNCTION_PASS("stackmap-liveness", StackMapLivenessPass)

llvm/lib/CodeGen/CodeGen.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ void llvm::initializeCodeGen(PassRegistry &Registry) {
116116
initializeRegAllocFastPass(Registry);
117117
initializeRegUsageInfoCollectorLegacyPass(Registry);
118118
initializeRegUsageInfoPropagationLegacyPass(Registry);
119-
initializeRegisterCoalescerPass(Registry);
119+
initializeRegisterCoalescerLegacyPass(Registry);
120120
initializeRemoveLoadsIntoFakeUsesPass(Registry);
121121
initializeRemoveRedundantDebugValuesPass(Registry);
122122
initializeRenameIndependentSubregsPass(Registry);

llvm/lib/CodeGen/RegAllocBasic.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ INITIALIZE_PASS_BEGIN(RABasic, "regallocbasic", "Basic Register Allocator",
134134
INITIALIZE_PASS_DEPENDENCY(LiveDebugVariablesWrapperLegacy)
135135
INITIALIZE_PASS_DEPENDENCY(SlotIndexesWrapperPass)
136136
INITIALIZE_PASS_DEPENDENCY(LiveIntervalsWrapperPass)
137-
INITIALIZE_PASS_DEPENDENCY(RegisterCoalescer)
137+
INITIALIZE_PASS_DEPENDENCY(RegisterCoalescerLegacy)
138138
INITIALIZE_PASS_DEPENDENCY(MachineScheduler)
139139
INITIALIZE_PASS_DEPENDENCY(LiveStacksWrapperLegacy)
140140
INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)

llvm/lib/CodeGen/RegAllocGreedy.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ INITIALIZE_PASS_BEGIN(RAGreedy, "greedy",
154154
INITIALIZE_PASS_DEPENDENCY(LiveDebugVariablesWrapperLegacy)
155155
INITIALIZE_PASS_DEPENDENCY(SlotIndexesWrapperPass)
156156
INITIALIZE_PASS_DEPENDENCY(LiveIntervalsWrapperPass)
157-
INITIALIZE_PASS_DEPENDENCY(RegisterCoalescer)
157+
INITIALIZE_PASS_DEPENDENCY(RegisterCoalescerLegacy)
158158
INITIALIZE_PASS_DEPENDENCY(MachineScheduler)
159159
INITIALIZE_PASS_DEPENDENCY(LiveStacksWrapperLegacy)
160160
INITIALIZE_PASS_DEPENDENCY(MachineDominatorTreeWrapperPass)

llvm/lib/CodeGen/RegisterCoalescer.cpp

Lines changed: 60 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,18 @@
2525
#include "llvm/CodeGen/LiveIntervals.h"
2626
#include "llvm/CodeGen/LiveRangeEdit.h"
2727
#include "llvm/CodeGen/MachineBasicBlock.h"
28+
#include "llvm/CodeGen/MachineDominators.h"
2829
#include "llvm/CodeGen/MachineFunction.h"
2930
#include "llvm/CodeGen/MachineFunctionPass.h"
3031
#include "llvm/CodeGen/MachineInstr.h"
3132
#include "llvm/CodeGen/MachineInstrBuilder.h"
3233
#include "llvm/CodeGen/MachineLoopInfo.h"
3334
#include "llvm/CodeGen/MachineOperand.h"
35+
#include "llvm/CodeGen/MachinePassManager.h"
3436
#include "llvm/CodeGen/MachineRegisterInfo.h"
3537
#include "llvm/CodeGen/Passes.h"
3638
#include "llvm/CodeGen/RegisterClassInfo.h"
39+
#include "llvm/CodeGen/RegisterCoalescerPass.h"
3740
#include "llvm/CodeGen/SlotIndexes.h"
3841
#include "llvm/CodeGen/TargetInstrInfo.h"
3942
#include "llvm/CodeGen/TargetOpcodes.h"
@@ -122,15 +125,14 @@ namespace {
122125

123126
class JoinVals;
124127

125-
class RegisterCoalescer : public MachineFunctionPass,
126-
private LiveRangeEdit::Delegate {
128+
class RegisterCoalescer : private LiveRangeEdit::Delegate {
127129
MachineFunction *MF = nullptr;
128130
MachineRegisterInfo *MRI = nullptr;
129131
const TargetRegisterInfo *TRI = nullptr;
130132
const TargetInstrInfo *TII = nullptr;
131133
LiveIntervals *LIS = nullptr;
134+
SlotIndexes *SI = nullptr;
132135
const MachineLoopInfo *Loops = nullptr;
133-
AliasAnalysis *AA = nullptr;
134136
RegisterClassInfo RegClassInfo;
135137

136138
/// Position and VReg of a PHI instruction during coalescing.
@@ -374,11 +376,24 @@ class RegisterCoalescer : public MachineFunctionPass,
374376
void checkMergingChangesDbgValuesImpl(Register Reg, LiveRange &OtherRange,
375377
LiveRange &RegRange, JoinVals &Vals2);
376378

379+
public:
380+
RegisterCoalescer(LiveIntervals *LIS, SlotIndexes *SI,
381+
const MachineLoopInfo *Loops)
382+
: LIS(LIS), SI(SI), Loops(Loops) {}
383+
384+
void releaseMemory();
385+
void print(raw_ostream &O, const Module * = nullptr) const;
386+
bool run(MachineFunction &MF);
387+
};
388+
389+
class RegisterCoalescerLegacy : public MachineFunctionPass {
390+
std::unique_ptr<RegisterCoalescer> Impl;
391+
377392
public:
378393
static char ID; ///< Class identification, replacement for typeinfo
379394

380-
RegisterCoalescer() : MachineFunctionPass(ID) {
381-
initializeRegisterCoalescerPass(*PassRegistry::getPassRegistry());
395+
RegisterCoalescerLegacy() : MachineFunctionPass(ID) {
396+
initializeRegisterCoalescerLegacyPass(*PassRegistry::getPassRegistry());
382397
}
383398

384399
void getAnalysisUsage(AnalysisUsage &AU) const override;
@@ -388,28 +403,30 @@ class RegisterCoalescer : public MachineFunctionPass,
388403
MachineFunctionProperties::Property::IsSSA);
389404
}
390405

391-
void releaseMemory() override;
406+
void releaseMemory() override { Impl->releaseMemory(); }
392407

393408
/// This is the pass entry point.
394409
bool runOnMachineFunction(MachineFunction &) override;
395410

396411
/// Implement the dump method.
397-
void print(raw_ostream &O, const Module * = nullptr) const override;
412+
void print(raw_ostream &O, const Module * = nullptr) const override {
413+
Impl->print(O, nullptr);
414+
}
398415
};
399416

400417
} // end anonymous namespace
401418

402-
char RegisterCoalescer::ID = 0;
419+
char RegisterCoalescerLegacy::ID = 0;
403420

404-
char &llvm::RegisterCoalescerID = RegisterCoalescer::ID;
421+
char &llvm::RegisterCoalescerID = RegisterCoalescerLegacy::ID;
405422

406-
INITIALIZE_PASS_BEGIN(RegisterCoalescer, "register-coalescer",
423+
INITIALIZE_PASS_BEGIN(RegisterCoalescerLegacy, "register-coalescer",
407424
"Register Coalescer", false, false)
408425
INITIALIZE_PASS_DEPENDENCY(LiveIntervalsWrapperPass)
409426
INITIALIZE_PASS_DEPENDENCY(SlotIndexesWrapperPass)
410427
INITIALIZE_PASS_DEPENDENCY(MachineLoopInfoWrapperPass)
411428
INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
412-
INITIALIZE_PASS_END(RegisterCoalescer, "register-coalescer",
429+
INITIALIZE_PASS_END(RegisterCoalescerLegacy, "register-coalescer",
413430
"Register Coalescer", false, false)
414431

415432
[[nodiscard]] static bool isMoveInstr(const TargetRegisterInfo &tri,
@@ -586,8 +603,9 @@ bool CoalescerPair::isCoalescable(const MachineInstr *MI) const {
586603
}
587604
}
588605

589-
void RegisterCoalescer::getAnalysisUsage(AnalysisUsage &AU) const {
606+
void RegisterCoalescerLegacy::getAnalysisUsage(AnalysisUsage &AU) const {
590607
AU.setPreservesCFG();
608+
AU.addUsedIfAvailable<SlotIndexesWrapperPass>();
591609
AU.addRequired<AAResultsWrapperPass>();
592610
AU.addRequired<LiveIntervalsWrapperPass>();
593611
AU.addPreserved<LiveIntervalsWrapperPass>();
@@ -4241,7 +4259,33 @@ void RegisterCoalescer::releaseMemory() {
42414259
LargeLIVisitCounter.clear();
42424260
}
42434261

4244-
bool RegisterCoalescer::runOnMachineFunction(MachineFunction &fn) {
4262+
PreservedAnalyses
4263+
RegisterCoalescerPass::run(MachineFunction &MF,
4264+
MachineFunctionAnalysisManager &MFAM) {
4265+
auto &LIS = MFAM.getResult<LiveIntervalsAnalysis>(MF);
4266+
auto &Loops = MFAM.getResult<MachineLoopAnalysis>(MF);
4267+
auto *SI = MFAM.getCachedResult<SlotIndexesAnalysis>(MF);
4268+
RegisterCoalescer Impl(&LIS, SI, &Loops);
4269+
if (!Impl.run(MF))
4270+
return PreservedAnalyses::all();
4271+
auto PA = getMachineFunctionPassPreservedAnalyses();
4272+
PA.preserve<LiveIntervalsAnalysis>();
4273+
PA.preserve<SlotIndexesAnalysis>();
4274+
PA.preserve<MachineLoopAnalysis>();
4275+
PA.preserve<MachineDominatorTreeAnalysis>();
4276+
return PA;
4277+
}
4278+
4279+
bool RegisterCoalescerLegacy::runOnMachineFunction(MachineFunction &MF) {
4280+
auto *LIS = &getAnalysis<LiveIntervalsWrapperPass>().getLIS();
4281+
auto *Loops = &getAnalysis<MachineLoopInfoWrapperPass>().getLI();
4282+
auto *SIWrapper = getAnalysisIfAvailable<SlotIndexesWrapperPass>();
4283+
SlotIndexes *SI = SIWrapper ? &SIWrapper->getSI() : nullptr;
4284+
Impl.reset(new RegisterCoalescer(LIS, SI, Loops));
4285+
return Impl->run(MF);
4286+
}
4287+
4288+
bool RegisterCoalescer::run(MachineFunction &fn) {
42454289
LLVM_DEBUG(dbgs() << "********** REGISTER COALESCER **********\n"
42464290
<< "********** Function: " << fn.getName() << '\n');
42474291

@@ -4264,9 +4308,6 @@ bool RegisterCoalescer::runOnMachineFunction(MachineFunction &fn) {
42644308
const TargetSubtargetInfo &STI = fn.getSubtarget();
42654309
TRI = STI.getRegisterInfo();
42664310
TII = STI.getInstrInfo();
4267-
LIS = &getAnalysis<LiveIntervalsWrapperPass>().getLIS();
4268-
AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
4269-
Loops = &getAnalysis<MachineLoopInfoWrapperPass>().getLI();
42704311
if (EnableGlobalCopies == cl::BOU_UNSET)
42714312
JoinGlobalCopies = STI.enableJoinGlobalCopies();
42724313
else
@@ -4291,7 +4332,7 @@ bool RegisterCoalescer::runOnMachineFunction(MachineFunction &fn) {
42914332
JoinSplitEdges = EnableJoinSplits;
42924333

42934334
if (VerifyCoalescing)
4294-
MF->verify(this, "Before register coalescing", &errs());
4335+
MF->verify(LIS, SI, "Before register coalescing", &errs());
42954336

42964337
DbgVRegToValues.clear();
42974338
buildVRegToDbgValueMap(fn);
@@ -4349,9 +4390,9 @@ bool RegisterCoalescer::runOnMachineFunction(MachineFunction &fn) {
43494390
PHIValToPos.clear();
43504391
RegToPHIIdx.clear();
43514392

4352-
LLVM_DEBUG(dump());
4393+
LLVM_DEBUG(print(dbgs(), nullptr));
43534394
if (VerifyCoalescing)
4354-
MF->verify(this, "After register coalescing", &errs());
4395+
MF->verify(LIS, SI, "After register coalescing", &errs());
43554396
return true;
43564397
}
43574398

llvm/lib/CodeGen/RegisterCoalescer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#ifndef LLVM_LIB_CODEGEN_REGISTERCOALESCER_H
1515
#define LLVM_LIB_CODEGEN_REGISTERCOALESCER_H
1616

17+
#include "llvm/CodeGen/MachinePassManager.h"
1718
#include "llvm/CodeGen/Register.h"
1819

1920
namespace llvm {

llvm/lib/Passes/PassBuilder.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@
126126
#include "llvm/CodeGen/RegAllocFast.h"
127127
#include "llvm/CodeGen/RegUsageInfoCollector.h"
128128
#include "llvm/CodeGen/RegUsageInfoPropagate.h"
129+
#include "llvm/CodeGen/RegisterCoalescerPass.h"
129130
#include "llvm/CodeGen/RegisterUsageInfo.h"
130131
#include "llvm/CodeGen/SafeStack.h"
131132
#include "llvm/CodeGen/SelectOptimize.h"

llvm/test/CodeGen/AArch64/coalescer-drop-subreg-to-reg-imm-ops.mir

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py UTC_ARGS: --version 4
22
# RUN: llc -mtriple=arm64-apple-macosx -mcpu=apple-m1 -verify-coalescing -run-pass=register-coalescer -o - %s | FileCheck %s
3+
# RUN: llc -mtriple=arm64-apple-macosx -mcpu=apple-m1 -verify-coalescing -passes=register-coalescer -o - %s | FileCheck %s
34

45
# Hits assert "Trying to add an operand to a machine instr that is
56
# already done!" when rematerializing during greedy. This was because

llvm/test/CodeGen/AMDGPU/blender-coalescer-verifier-error-empty-subrange.mir

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py UTC_ARGS: --version 2
22
# RUN: llc -mtriple=amdgcn-amd-amdhsa -mcpu=gfx1031 -run-pass=register-coalescer -verify-coalescing -o - %s | FileCheck %s
3+
# RUN: llc -mtriple=amdgcn-amd-amdhsa -mcpu=gfx1031 -passes=register-coalescer -verify-coalescing -o - %s | FileCheck %s
34

45
# Testcase variants from
56
# liveout-implicit-def-subreg-redef-blender-verifier-error.mir which

llvm/test/CodeGen/X86/coalesce-dead-lanes.mir

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# RUN: llc -run-pass register-coalescer -O0 -mtriple x86_64-pc-linux-gnu -o - %s | FileCheck %s
2+
# RUN: llc -passes register-coalescer -O0 -mtriple x86_64-pc-linux-gnu -o - %s | FileCheck %s
23

34
---
45
name: foo

llvm/test/CodeGen/X86/late-remat-update.mir

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# REQUIRES: asserts
2-
# RUN: llc -mtriple=x86_64-- -run-pass=register-coalescer -late-remat-update-threshold=1 -stats %s -o /dev/null 2>&1 | FileCheck %s
2+
# RUN: llc -mtriple=x86_64-- -passes=register-coalescer -late-remat-update-threshold=1 -stats %s -o /dev/null 2>&1 | FileCheck %s
33
# Check the test will rematerialize for three copies, but will call shrinkToUses
44
# only once to update live range because of late rematerialization update.
55
# CHECK: 3 regalloc - Number of instructions re-materialized

0 commit comments

Comments
 (0)