Skip to content

Commit a0108c3

Browse files
committed
WIP genset calculation
1 parent c0b0e97 commit a0108c3

File tree

2 files changed

+32
-25
lines changed

2 files changed

+32
-25
lines changed

llvm/lib/Target/RISCV/RISCVLiveVariables.cpp

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,7 @@
2222
#include "RISCVInstrInfo.h"
2323
#include "RISCVSubtarget.h"
2424
#include "llvm/ADT/DenseMap.h"
25-
#include "llvm/ADT/DepthFirstIterator.h"
2625
#include "llvm/ADT/PostOrderIterator.h"
27-
#include "llvm/ADT/SmallPtrSet.h"
28-
#include "llvm/ADT/SmallVector.h"
2926
#include "llvm/ADT/Statistic.h"
3027
#include "llvm/CodeGen/MachineBasicBlock.h"
3128
#include "llvm/CodeGen/MachineFunctionPass.h"
@@ -59,7 +56,7 @@ struct LivenessInfo {
5956
std::set<Register> LiveOut;
6057

6158
/// Registers that are defined in this block
62-
std::set<Register> Def;
59+
std::set<Register> Gen;
6360

6461
/// Registers that are used in this block before being defined (if at all).
6562
std::set<Register> Use;
@@ -174,29 +171,16 @@ void RISCVLiveVariables::processInstruction(const MachineInstr &MI,
174171
if (!isTrackableRegister(Reg, TRI, MRI))
175172
continue;
176173

177-
if (MO.isDef()) {
178-
// This is a definition
179-
Info.Def.insert(Reg);
180-
181-
// Also handle sub-registers for physical registers
182-
if (Reg.isPhysical()) {
183-
for (MCSubRegIterator SubRegs(Reg, TRI, /*IncludeSelf=*/false);
184-
SubRegs.isValid(); ++SubRegs) {
185-
Info.Def.insert(*SubRegs);
186-
}
187-
}
188-
}
189-
190174
if (MO.isUse()) {
191175
// This is a use - only add to Use set if not already defined in this block
192-
if (Info.Def.find(Reg) == Info.Def.end()) {
176+
if (Info.Gen.find(Reg) == Info.Gen.end()) {
193177
Info.Use.insert(Reg);
194178

195179
// Also handle sub-registers for physical registers
196180
if (Reg.isPhysical()) {
197181
for (MCSubRegIterator SubRegs(Reg, TRI, /*IncludeSelf=*/false);
198182
SubRegs.isValid(); ++SubRegs) {
199-
if (Info.Def.find(*SubRegs) == Info.Def.end()) {
183+
if (Info.Gen.find(*SubRegs) == Info.Gen.end()) {
200184
Info.Use.insert(*SubRegs);
201185
}
202186
}
@@ -206,10 +190,23 @@ void RISCVLiveVariables::processInstruction(const MachineInstr &MI,
206190

207191
// Handle implicit operands (like condition codes, stack pointer updates)
208192
if (MO.isImplicit() && MO.isUse() && Reg.isPhysical()) {
209-
if (Info.Def.find(Reg) == Info.Def.end()) {
193+
if (Info.Gen.find(Reg) == Info.Gen.end()) {
210194
Info.Use.insert(Reg);
211195
}
212196
}
197+
198+
if (MO.isDef()) {
199+
// This is a definition
200+
Info.Gen.insert(Reg);
201+
202+
// Also handle sub-registers for physical registers
203+
if (Reg.isPhysical()) {
204+
for (MCSubRegIterator SubRegs(Reg, TRI, /*IncludeSelf=*/false);
205+
SubRegs.isValid(); ++SubRegs) {
206+
Info.Gen.insert(*SubRegs);
207+
}
208+
}
209+
}
213210
}
214211

215212
// Handle RegMasks (from calls) - they kill all non-preserved registers
@@ -225,7 +222,7 @@ void RISCVLiveVariables::processInstruction(const MachineInstr &MI,
225222

226223
// Mark as defined (clobbered)
227224
if (isTrackableRegister(Register(PhysReg), TRI, MRI)) {
228-
Info.Def.insert(Register(PhysReg));
225+
Info.Gen.insert(Register(PhysReg));
229226
}
230227
}
231228
}
@@ -238,7 +235,7 @@ void RISCVLiveVariables::computeLocalLiveness(MachineFunction &MF) {
238235
// Process each basic block
239236
for (MachineBasicBlock &MBB : MF) {
240237
LivenessInfo &Info = BlockLiveness[&MBB];
241-
Info.Def.clear();
238+
Info.Gen.clear();
242239
Info.Use.clear();
243240

244241
// Process instructions in forward order to build Use and Def sets
@@ -267,7 +264,7 @@ void RISCVLiveVariables::computeGlobalLiveness(MachineFunction &MF) {
267264
LLVM_DEBUG(dbgs() << "Computing global liveness (fixed-point iteration)\n");
268265

269266
bool Changed = true;
270-
unsigned Iterations = 0;
267+
[[maybe_unused]] unsigned Iterations = 0;
271268

272269
// Iterate until we reach a fixed point
273270
// Live-out[B] = Union of Live-in[S] for all successors S of B
@@ -296,7 +293,7 @@ void RISCVLiveVariables::computeGlobalLiveness(MachineFunction &MF) {
296293
std::set<Register> NewLiveIn = Info.Use;
297294

298295
for (Register Reg : Info.LiveOut) {
299-
if (Info.Def.find(Reg) == Info.Def.end()) {
296+
if (Info.Gen.find(Reg) == Info.Gen.end()) {
300297
NewLiveIn.insert(Reg);
301298
}
302299
}
@@ -420,7 +417,7 @@ void RISCVLiveVariables::print(raw_ostream &OS, const Module *M) const {
420417
OS << "}\n";
421418

422419
OS << " Def: { ";
423-
for (Register Reg : Info.Def) {
420+
for (Register Reg : Info.Gen) {
424421
OS << printReg(Reg, TRI) << " ";
425422
}
426423
OS << "}\n\n";

llvm/test/CodeGen/RISCV/live-variables-basic.mir

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,16 @@
3636
%t4 = add i64 %t3, %b
3737
ret i64 %t4
3838
}
39+
40+
define i64 @test_gen_kill(i64 %a, i64 %b, i64 %c) {
41+
entry:
42+
%t1 = add i64 %a, %b
43+
%t2 = mul i64 %t1, %c
44+
%t3 = sub i64 %t2, %a
45+
%t4 = add i64 %t3, %b
46+
ret i64 %t4
47+
}
48+
3949
...
4050
---
4151
name: test_simple_add

0 commit comments

Comments
 (0)