Skip to content

Commit 6143ec2

Browse files
[WIP] Enhance Super-Reg-Rewriter to work with unassigned virtual reg
1 parent 00dcbc3 commit 6143ec2

File tree

1 file changed

+50
-19
lines changed

1 file changed

+50
-19
lines changed

llvm/lib/Target/AIE/AIESuperRegRewriter.cpp

Lines changed: 50 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
#include "AIEBaseInstrInfo.h"
1212
#include "AIEBaseRegisterInfo.h"
1313

14+
#include "aie2p/AIE2PRegisterBankInfo.h"
15+
#include "aie2p/AIE2PRegisterInfo.h"
16+
#include "aie2p/AIE2PSubtarget.h"
17+
1418
#include "llvm/ADT/MapVector.h"
1519
#include "llvm/ADT/SmallSet.h"
1620
#include "llvm/CodeGen/LiveDebugVariables.h"
@@ -65,8 +69,9 @@ class AIESuperRegRewriter : public MachineFunctionPass {
6569

6670
private:
6771
void rewriteSuperReg(Register Reg, Register AssignedPhysReg,
68-
MachineRegisterInfo &MRI, const AIEBaseRegisterInfo &TRI,
69-
VirtRegMap &VRM, LiveRegMatrix &LRM, LiveIntervals &LIS,
72+
MachineFunction &MF, MachineRegisterInfo &MRI,
73+
const AIEBaseRegisterInfo &TRI, VirtRegMap &VRM,
74+
LiveRegMatrix &LRM, LiveIntervals &LIS,
7075
SlotIndexes &Indexes, LiveDebugVariables &DebugVars);
7176
};
7277

@@ -149,17 +154,20 @@ bool AIESuperRegRewriter::runOnMachineFunction(MachineFunction &MF) {
149154
SlotIndexes &Indexes = getAnalysis<SlotIndexes>();
150155
LiveDebugVariables &DebugVars = getAnalysis<LiveDebugVariables>();
151156
std::map<Register, MCRegister> AssignedPhysRegs;
157+
std::list<Register> UnAssignedPhysRegs;
152158

153159
// Collect already-assigned VRegs that can be split into smaller ones.
154160
LLVM_DEBUG(VRM.dump());
155161
for (unsigned VRegIdx = 0, End = MRI.getNumVirtRegs(); VRegIdx != End;
156162
++VRegIdx) {
157163
Register Reg = Register::index2VirtReg(VRegIdx);
158164

159-
// Ignore un-used registers and un-allocated registers
160-
if (MRI.reg_nodbg_empty(Reg) || !VRM.hasPhys(Reg))
165+
// Ignore un-used registers registers
166+
if (MRI.reg_nodbg_empty(Reg))
161167
continue;
162168

169+
const bool VirtualRegIsAllocated = VRM.hasPhys(Reg);
170+
163171
// Skip vregs that are spilled, they would anyway be disregarded by
164172
// getRewritableSubRegs due to the spill instructions using the whole reg
165173
// without any subreg indices.
@@ -172,17 +180,32 @@ bool AIESuperRegRewriter::runOnMachineFunction(MachineFunction &MF) {
172180
LLVM_DEBUG(dbgs() << "Analysing " << printReg(Reg, &TRI, 0, &MRI) << ":"
173181
<< printRegClassOrBank(Reg, MRI, &TRI) << '\n');
174182
if (!getRewritableSubRegs(Reg, MRI, TRI).empty()) {
175-
AssignedPhysRegs[Reg] = VRM.getPhys(Reg);
176-
LRM.unassign(LIS.getInterval(Reg));
183+
if (VirtualRegIsAllocated) {
184+
AssignedPhysRegs[Reg] = VRM.getPhys(Reg);
185+
LRM.unassign(LIS.getInterval(Reg));
186+
} else {
187+
UnAssignedPhysRegs.push_back(Reg);
188+
}
177189
} else {
178190
LLVM_DEBUG(dbgs() << "Could not rewrite " << printReg(Reg, &TRI, 0, &MRI)
179191
<< '\n');
180192
}
181193
}
182194

183-
// Re-write all the collected VRegs
195+
// Re-write all the collected assigned VRegs
184196
for (auto &[VReg, PhysReg] : AssignedPhysRegs) {
185-
rewriteSuperReg(VReg, PhysReg, MRI, TRI, VRM, LRM, LIS, Indexes, DebugVars);
197+
rewriteSuperReg(VReg, PhysReg, MF, MRI, TRI, VRM, LRM, LIS, Indexes,
198+
DebugVars);
199+
}
200+
201+
// Re-write all the collected unassigned VRegs
202+
for (auto &VReg : UnAssignedPhysRegs) {
203+
MCRegister DummyPhysReg;
204+
const TargetRegisterClass *SuperRC = MRI.getRegClass(VReg);
205+
// TODO : Remove ARCH specific check
206+
if (SuperRC == &AIE2P::eDSRegClass)
207+
rewriteSuperReg(VReg, DummyPhysReg, MF, MRI, TRI, VRM, LRM, LIS, Indexes,
208+
DebugVars);
186209
}
187210

188211
LLVM_DEBUG(VRM.dump());
@@ -238,10 +261,13 @@ static void rewriteFullCopy(MachineInstr &MI, const std::set<int> &CopySubRegs,
238261
}
239262

240263
void AIESuperRegRewriter::rewriteSuperReg(
241-
Register Reg, Register AssignedPhysReg, MachineRegisterInfo &MRI,
242-
const AIEBaseRegisterInfo &TRI, VirtRegMap &VRM, LiveRegMatrix &LRM,
243-
LiveIntervals &LIS, SlotIndexes &Indexes, LiveDebugVariables &DebugVars) {
244-
LLVM_DEBUG(dbgs() << "Rewriting " << printReg(Reg, &TRI, 0, &MRI) << '\n');
264+
Register Reg, Register AssignedPhysReg, MachineFunction &MF,
265+
MachineRegisterInfo &MRI, const AIEBaseRegisterInfo &TRI, VirtRegMap &VRM,
266+
LiveRegMatrix &LRM, LiveIntervals &LIS, SlotIndexes &Indexes,
267+
LiveDebugVariables &DebugVars) {
268+
bool AssignPhysRegIsValid = AssignedPhysReg.isValid();
269+
LLVM_DEBUG(dbgs() << "Rewriting " << printReg(Reg, &TRI, 0, &MRI)
270+
<< " Assigned " << AssignPhysRegIsValid << '\n');
245271
auto *TII = static_cast<const AIEBaseInstrInfo *>(
246272
VRM.getMachineFunction().getSubtarget().getInstrInfo());
247273

@@ -251,7 +277,9 @@ void AIESuperRegRewriter::rewriteSuperReg(
251277
SmallSet<int, 8> SubRegs = getRewritableSubRegs(Reg, MRI, TRI);
252278
assert(!SubRegs.empty());
253279
for (int SubReg : SubRegs) {
254-
const TargetRegisterClass *SubRC = TRI.getSubRegisterClass(SuperRC, SubReg);
280+
const TargetRegisterClass *SubRC = TRI.getLargestLegalSuperClass(
281+
TRI.getSubRegisterClass(SuperRC, SubReg), MF);
282+
255283
SubRegToVReg[SubReg] = MRI.createVirtualRegister(SubRC);
256284
}
257285

@@ -289,7 +317,9 @@ void AIESuperRegRewriter::rewriteSuperReg(
289317
LIS.removeInterval(Reg);
290318

291319
for (auto &[SubRegIdx, VReg] : SubRegToVReg) {
292-
MCRegister SubPhysReg = TRI.getSubReg(AssignedPhysReg, SubRegIdx);
320+
MCRegister SubPhysReg;
321+
if (AssignPhysRegIsValid)
322+
SubPhysReg = TRI.getSubReg(AssignedPhysReg, SubRegIdx);
293323
LiveInterval &SubRegLI = LIS.getInterval(VReg);
294324
LLVM_DEBUG(dbgs() << " Assigning Range: " << SubRegLI << '\n');
295325

@@ -300,11 +330,12 @@ void AIESuperRegRewriter::rewriteSuperReg(
300330
LIComponents.push_back(&SubRegLI);
301331
VRM.grow();
302332

303-
for (LiveInterval *LI : LIComponents) {
304-
LRM.assign(*LI, SubPhysReg);
305-
VRM.setRequiredPhys(LI->reg(), SubPhysReg);
306-
LLVM_DEBUG(dbgs() << " Assigned " << printReg(LI->reg()) << "\n");
307-
}
333+
if (AssignPhysRegIsValid)
334+
for (LiveInterval *LI : LIComponents) {
335+
LRM.assign(*LI, SubPhysReg);
336+
VRM.setRequiredPhys(LI->reg(), SubPhysReg);
337+
LLVM_DEBUG(dbgs() << " Assigned " << printReg(LI->reg()) << "\n");
338+
}
308339
}
309340

310341
// Announce new VRegs so DBG locations can be updated.

0 commit comments

Comments
 (0)