Skip to content

Commit 52bcc7b

Browse files
authored
[NFC][RA] Refactor RABasic into a Separate Header (llvm#149555)
This change refactors the RABasic type by moving it from RegAllocBasic.cpp to a new header file, RegAllocBasic.h. This separation of header and implementation aligns with the structure used by other register allocators, such as RegAllocGreedy. The refactoring is intended to facilitate future use of RABasic in other contexts.
1 parent dc89a91 commit 52bcc7b

File tree

2 files changed

+110
-91
lines changed

2 files changed

+110
-91
lines changed

llvm/lib/CodeGen/RegAllocBasic.cpp

Lines changed: 6 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -5,35 +5,31 @@
55
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
66
//
77
//===----------------------------------------------------------------------===//
8-
//
9-
// This file defines the RABasic function pass, which provides a minimal
10-
// implementation of the basic register allocator.
11-
//
8+
///
9+
/// \file
10+
/// This file defines the RABasic function pass, which provides a minimal
11+
/// implementation of the basic register allocator.
12+
///
1213
//===----------------------------------------------------------------------===//
1314

15+
#include "RegAllocBasic.h"
1416
#include "AllocationOrder.h"
15-
#include "RegAllocBase.h"
1617
#include "llvm/Analysis/AliasAnalysis.h"
1718
#include "llvm/Analysis/ProfileSummaryInfo.h"
1819
#include "llvm/CodeGen/CalcSpillWeights.h"
1920
#include "llvm/CodeGen/LiveDebugVariables.h"
2021
#include "llvm/CodeGen/LiveIntervals.h"
21-
#include "llvm/CodeGen/LiveRangeEdit.h"
2222
#include "llvm/CodeGen/LiveRegMatrix.h"
2323
#include "llvm/CodeGen/LiveStacks.h"
2424
#include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
2525
#include "llvm/CodeGen/MachineDominators.h"
26-
#include "llvm/CodeGen/MachineFunctionPass.h"
2726
#include "llvm/CodeGen/MachineLoopInfo.h"
2827
#include "llvm/CodeGen/Passes.h"
2928
#include "llvm/CodeGen/RegAllocRegistry.h"
30-
#include "llvm/CodeGen/Spiller.h"
31-
#include "llvm/CodeGen/TargetRegisterInfo.h"
3229
#include "llvm/CodeGen/VirtRegMap.h"
3330
#include "llvm/Pass.h"
3431
#include "llvm/Support/Debug.h"
3532
#include "llvm/Support/raw_ostream.h"
36-
#include <queue>
3733

3834
using namespace llvm;
3935

@@ -42,89 +38,8 @@ using namespace llvm;
4238
static RegisterRegAlloc basicRegAlloc("basic", "basic register allocator",
4339
createBasicRegisterAllocator);
4440

45-
namespace {
46-
struct CompSpillWeight {
47-
bool operator()(const LiveInterval *A, const LiveInterval *B) const {
48-
return A->weight() < B->weight();
49-
}
50-
};
51-
}
52-
53-
namespace {
54-
/// RABasic provides a minimal implementation of the basic register allocation
55-
/// algorithm. It prioritizes live virtual registers by spill weight and spills
56-
/// whenever a register is unavailable. This is not practical in production but
57-
/// provides a useful baseline both for measuring other allocators and comparing
58-
/// the speed of the basic algorithm against other styles of allocators.
59-
class RABasic : public MachineFunctionPass,
60-
public RegAllocBase,
61-
private LiveRangeEdit::Delegate {
62-
// context
63-
MachineFunction *MF = nullptr;
64-
65-
// state
66-
std::unique_ptr<Spiller> SpillerInstance;
67-
std::priority_queue<const LiveInterval *, std::vector<const LiveInterval *>,
68-
CompSpillWeight>
69-
Queue;
70-
71-
// Scratch space. Allocated here to avoid repeated malloc calls in
72-
// selectOrSplit().
73-
BitVector UsableRegs;
74-
75-
bool LRE_CanEraseVirtReg(Register) override;
76-
void LRE_WillShrinkVirtReg(Register) override;
77-
78-
public:
79-
RABasic(const RegAllocFilterFunc F = nullptr);
80-
81-
/// Return the pass name.
82-
StringRef getPassName() const override { return "Basic Register Allocator"; }
83-
84-
/// RABasic analysis usage.
85-
void getAnalysisUsage(AnalysisUsage &AU) const override;
86-
87-
void releaseMemory() override;
88-
89-
Spiller &spiller() override { return *SpillerInstance; }
90-
91-
void enqueueImpl(const LiveInterval *LI) override { Queue.push(LI); }
92-
93-
const LiveInterval *dequeue() override {
94-
if (Queue.empty())
95-
return nullptr;
96-
const LiveInterval *LI = Queue.top();
97-
Queue.pop();
98-
return LI;
99-
}
100-
101-
MCRegister selectOrSplit(const LiveInterval &VirtReg,
102-
SmallVectorImpl<Register> &SplitVRegs) override;
103-
104-
/// Perform register allocation.
105-
bool runOnMachineFunction(MachineFunction &mf) override;
106-
107-
MachineFunctionProperties getRequiredProperties() const override {
108-
return MachineFunctionProperties().setNoPHIs();
109-
}
110-
111-
MachineFunctionProperties getClearedProperties() const override {
112-
return MachineFunctionProperties().setIsSSA();
113-
}
114-
115-
// Helper for spilling all live virtual registers currently unified under preg
116-
// that interfere with the most recently queried lvr. Return true if spilling
117-
// was successful, and append any new spilled/split intervals to splitLVRs.
118-
bool spillInterferences(const LiveInterval &VirtReg, MCRegister PhysReg,
119-
SmallVectorImpl<Register> &SplitVRegs);
120-
121-
static char ID;
122-
};
123-
12441
char RABasic::ID = 0;
12542

126-
} // end anonymous namespace
127-
12843
char &llvm::RABasicID = RABasic::ID;
12944

13045
INITIALIZE_PASS_BEGIN(RABasic, "regallocbasic", "Basic Register Allocator",

llvm/lib/CodeGen/RegAllocBasic.h

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
//===-- RegAllocBasic.h - Basic Register Allocator Header -----------------===//
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+
/// \file
10+
/// This file declares the RABasic class, which provides a minimal
11+
/// implementation of the basic register allocator.
12+
///
13+
//===----------------------------------------------------------------------===//
14+
15+
#ifndef LLVM_CODEGEN_REGALLOCBASIC_H
16+
#define LLVM_CODEGEN_REGALLOCBASIC_H
17+
18+
#include "RegAllocBase.h"
19+
#include "llvm/CodeGen/LiveRangeEdit.h"
20+
#include "llvm/CodeGen/MachineFunctionPass.h"
21+
#include "llvm/CodeGen/Spiller.h"
22+
#include <queue>
23+
24+
namespace llvm {
25+
26+
struct CompSpillWeight {
27+
bool operator()(const LiveInterval *A, const LiveInterval *B) const {
28+
return A->weight() < B->weight();
29+
}
30+
};
31+
32+
/// RABasic provides a minimal implementation of the basic register allocation
33+
/// algorithm. It prioritizes live virtual registers by spill weight and spills
34+
/// whenever a register is unavailable. This is not practical in production but
35+
/// provides a useful baseline both for measuring other allocators and comparing
36+
/// the speed of the basic algorithm against other styles of allocators.
37+
class LLVM_LIBRARY_VISIBILITY RABasic : public MachineFunctionPass,
38+
public RegAllocBase,
39+
private LiveRangeEdit::Delegate {
40+
// context
41+
MachineFunction *MF = nullptr;
42+
43+
// state
44+
std::unique_ptr<Spiller> SpillerInstance;
45+
std::priority_queue<const LiveInterval *, std::vector<const LiveInterval *>,
46+
CompSpillWeight>
47+
Queue;
48+
49+
// Scratch space. Allocated here to avoid repeated malloc calls in
50+
// selectOrSplit().
51+
BitVector UsableRegs;
52+
53+
bool LRE_CanEraseVirtReg(Register) override;
54+
void LRE_WillShrinkVirtReg(Register) override;
55+
56+
public:
57+
RABasic(const RegAllocFilterFunc F = nullptr);
58+
59+
/// Return the pass name.
60+
StringRef getPassName() const override { return "Basic Register Allocator"; }
61+
62+
/// RABasic analysis usage.
63+
void getAnalysisUsage(AnalysisUsage &AU) const override;
64+
65+
void releaseMemory() override;
66+
67+
Spiller &spiller() override { return *SpillerInstance; }
68+
69+
void enqueueImpl(const LiveInterval *LI) override { Queue.push(LI); }
70+
71+
const LiveInterval *dequeue() override {
72+
if (Queue.empty())
73+
return nullptr;
74+
const LiveInterval *LI = Queue.top();
75+
Queue.pop();
76+
return LI;
77+
}
78+
79+
MCRegister selectOrSplit(const LiveInterval &VirtReg,
80+
SmallVectorImpl<Register> &SplitVRegs) override;
81+
82+
/// Perform register allocation.
83+
bool runOnMachineFunction(MachineFunction &mf) override;
84+
85+
MachineFunctionProperties getRequiredProperties() const override {
86+
return MachineFunctionProperties().set(
87+
MachineFunctionProperties::Property::NoPHIs);
88+
}
89+
90+
MachineFunctionProperties getClearedProperties() const override {
91+
return MachineFunctionProperties().set(
92+
MachineFunctionProperties::Property::IsSSA);
93+
}
94+
95+
// Helper for spilling all live virtual registers currently unified under preg
96+
// that interfere with the most recently queried lvr. Return true if spilling
97+
// was successful, and append any new spilled/split intervals to splitLVRs.
98+
bool spillInterferences(const LiveInterval &VirtReg, MCRegister PhysReg,
99+
SmallVectorImpl<Register> &SplitVRegs);
100+
101+
static char ID;
102+
};
103+
} // namespace llvm
104+
#endif

0 commit comments

Comments
 (0)