Skip to content

Commit 48677b8

Browse files
committed
[NFC][RA] Refactor RABasic into a Separate Header
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 b5e71d7 commit 48677b8

File tree

2 files changed

+108
-91
lines changed

2 files changed

+108
-91
lines changed

llvm/lib/CodeGen/RegAllocBasic.cpp

Lines changed: 1 addition & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -11,29 +11,20 @@
1111
//
1212
//===----------------------------------------------------------------------===//
1313

14-
#include "AllocationOrder.h"
15-
#include "RegAllocBase.h"
14+
#include "RegAllocBasic.h"
1615
#include "llvm/Analysis/AliasAnalysis.h"
1716
#include "llvm/Analysis/ProfileSummaryInfo.h"
1817
#include "llvm/CodeGen/CalcSpillWeights.h"
1918
#include "llvm/CodeGen/LiveDebugVariables.h"
20-
#include "llvm/CodeGen/LiveIntervals.h"
21-
#include "llvm/CodeGen/LiveRangeEdit.h"
22-
#include "llvm/CodeGen/LiveRegMatrix.h"
2319
#include "llvm/CodeGen/LiveStacks.h"
2420
#include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
2521
#include "llvm/CodeGen/MachineDominators.h"
26-
#include "llvm/CodeGen/MachineFunctionPass.h"
2722
#include "llvm/CodeGen/MachineLoopInfo.h"
2823
#include "llvm/CodeGen/Passes.h"
2924
#include "llvm/CodeGen/RegAllocRegistry.h"
30-
#include "llvm/CodeGen/Spiller.h"
31-
#include "llvm/CodeGen/TargetRegisterInfo.h"
32-
#include "llvm/CodeGen/VirtRegMap.h"
3325
#include "llvm/Pass.h"
3426
#include "llvm/Support/Debug.h"
3527
#include "llvm/Support/raw_ostream.h"
36-
#include <queue>
3728

3829
using namespace llvm;
3930

@@ -42,89 +33,8 @@ using namespace llvm;
4233
static RegisterRegAlloc basicRegAlloc("basic", "basic register allocator",
4334
createBasicRegisterAllocator);
4435

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-
12436
char RABasic::ID = 0;
12537

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

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

llvm/lib/CodeGen/RegAllocBasic.h

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

0 commit comments

Comments
 (0)