3131#include " llvm/CodeGen/TargetRegisterInfo.h"
3232#include " llvm/InitializePasses.h"
3333#include " llvm/Support/Debug.h"
34+ #include " llvm/Support/ErrorHandling.h"
3435#include " llvm/Support/raw_ostream.h"
3536#include < set>
3637
@@ -99,6 +100,7 @@ class RISCVLiveVariables : public MachineFunctionPass {
99100 // / Print liveness information for debugging
100101 void print (raw_ostream &OS, const Module *M = nullptr ) const override ;
101102
103+ void verifyLiveness (MachineFunction &MF) const ;
102104private:
103105 // / Compute local liveness information (Use and Def sets) for each block
104106 void computeLocalLiveness (MachineFunction &MF);
@@ -160,7 +162,7 @@ bool RISCVLiveVariables::isTrackableRegister(
160162void RISCVLiveVariables::processInstruction (const MachineInstr &MI,
161163 LivenessInfo &Info,
162164 const TargetRegisterInfo *TRI) {
163- // Process all operands
165+ std::vector<Register> GenVec;
164166 for (const MachineOperand &MO : MI.operands ()) {
165167 if (!MO.isReg () || !MO.getReg ())
166168 continue ;
@@ -195,16 +197,16 @@ void RISCVLiveVariables::processInstruction(const MachineInstr &MI,
195197 }
196198 }
197199
198- if (MO.isDef ()) {
199- // This is a definition
200- Info. Gen . insert (Reg);
200+ if (MO.isDef ()) // Collect defs for later processing.
201+ GenVec. push_back (Reg);
202+ }
201203
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- }
204+ for ( auto Reg : GenVec) {
205+ Info. Gen . insert (Reg);
206+ if ( Reg. isPhysical ()) {
207+ for (MCSubRegIterator SubRegs (Reg, TRI, /* IncludeSelf= */ false );
208+ SubRegs. isValid (); ++SubRegs) {
209+ Info. Gen . insert (*SubRegs);
208210 }
209211 }
210212 }
@@ -253,7 +255,7 @@ void RISCVLiveVariables::computeLocalLiveness(MachineFunction &MF) {
253255 for (Register Reg : Info.Use )
254256 dbgs () << printReg (Reg, TRI) << " " ;
255257 dbgs () << " \n Def: " ;
256- for (Register Reg : Info.Def )
258+ for (Register Reg : Info.Gen )
257259 dbgs () << printReg (Reg, TRI) << " " ;
258260 dbgs () << " \n " ;
259261 });
@@ -273,7 +275,7 @@ void RISCVLiveVariables::computeGlobalLiveness(MachineFunction &MF) {
273275 Changed = false ;
274276 ++Iterations;
275277
276- // Process blocks in reverse post-order for better convergence
278+ // Process blocks in ** post-order** for better convergence
277279 ReversePostOrderTraversal<MachineFunction *> RPOT (&MF);
278280
279281 for (MachineBasicBlock *MBB : RPOT) {
@@ -320,13 +322,6 @@ void RISCVLiveVariables::computeGlobalLiveness(MachineFunction &MF) {
320322 const MachineBasicBlock &EntryBB = MF.front ();
321323 NumLiveRegsAtEntry += BlockLiveness[&EntryBB].LiveIn .size ();
322324 }
323-
324- for (auto &BB : MF) {
325- auto &computedLivein = BlockLiveness[&BB].LiveIn ;
326- for (auto &LI : BB.getLiveIns ()) {
327- assert (0 && computedLivein.count (LI.PhysReg ));
328- }
329- }
330325}
331326
332327bool RISCVLiveVariables::isLiveAt (Register Reg,
@@ -355,8 +350,25 @@ bool RISCVLiveVariables::isLiveAt(Register Reg,
355350 return false ;
356351}
357352
353+ void RISCVLiveVariables::verifyLiveness (MachineFunction &MF) const {
354+ for (auto &BB : MF) {
355+ auto BBLiveness = BlockLiveness.find (&BB);
356+ assert (BBLiveness != BlockLiveness.end () && " Missing Liveness" );
357+ auto &ComputedLivein = BBLiveness->second .LiveIn ;
358+ for (auto &LI : BB.getLiveIns ()) {
359+ if (!ComputedLivein.count (LI.PhysReg )) {
360+ LLVM_DEBUG (dbgs () << " Warning: Live-in register "
361+ << printReg (LI.PhysReg , TRI)
362+ << " missing from computed live-in set of block "
363+ << BB.getName () << " \n " );
364+ llvm_unreachable (" Computed live-in set is inconsistent with MBB." );
365+ }
366+ }
367+ }
368+ }
369+
358370bool RISCVLiveVariables::runOnMachineFunction (MachineFunction &MF) {
359- if (skipFunction (MF.getFunction ()))
371+ if (skipFunction (MF.getFunction ()) || MF. empty () )
360372 return false ;
361373
362374 const RISCVSubtarget &Subtarget = MF.getSubtarget <RISCVSubtarget>();
@@ -373,8 +385,6 @@ bool RISCVLiveVariables::runOnMachineFunction(MachineFunction &MF) {
373385
374386 LLVM_DEBUG (dbgs () << " ***** RISC-V Live Variable Analysis *****\n " );
375387 LLVM_DEBUG (dbgs () << " Function: " << MF.getName () << " \n " );
376- LLVM_DEBUG (dbgs () << " Target: RV" << (Subtarget.is64Bit () ? " 64" : " 32" )
377- << " \n " );
378388
379389 // Clear any previous analysis
380390 BlockLiveness.clear ();
@@ -390,6 +400,7 @@ bool RISCVLiveVariables::runOnMachineFunction(MachineFunction &MF) {
390400 print (dbgs ());
391401 });
392402
403+ verifyLiveness (MF);
393404 // This is an analysis pass, it doesn't modify the function
394405 return false ;
395406}
0 commit comments