Skip to content

Conversation

@mgudim
Copy link
Contributor

@mgudim mgudim commented Nov 6, 2025

We plan to use ReachingDefinitionsAnalysis in
PrologEpilogInsertion. In some tests this will be a problem because ReachingDefinitionAnalysis will have to run on a function that does not have live-in info. This PR lets ReachingDefinitionsAnalysis recompute live-ins.

@llvmbot
Copy link
Member

llvmbot commented Nov 6, 2025

@llvm/pr-subscribers-backend-risc-v

Author: Mikhail Gudim (mgudim)

Changes

We plan to use ReachingDefinitionsAnalysis in
PrologEpilogInsertion. In some tests this will be a problem because ReachingDefinitionAnalysis will have to run on a function that does not have live-in info. This PR lets ReachingDefinitionsAnalysis recompute live-ins.


Full diff: https://github.com/llvm/llvm-project/pull/166773.diff

3 Files Affected:

  • (modified) llvm/lib/CodeGen/ReachingDefAnalysis.cpp (+12)
  • (modified) llvm/test/CodeGen/RISCV/pr53662.mir (+4)
  • (modified) llvm/test/CodeGen/RISCV/rvv/fixed-vectors-emergency-slot.mir (+1-1)
diff --git a/llvm/lib/CodeGen/ReachingDefAnalysis.cpp b/llvm/lib/CodeGen/ReachingDefAnalysis.cpp
index 40a89078bcf59..b4b7e0b64ab4e 100644
--- a/llvm/lib/CodeGen/ReachingDefAnalysis.cpp
+++ b/llvm/lib/CodeGen/ReachingDefAnalysis.cpp
@@ -7,8 +7,10 @@
 //===----------------------------------------------------------------------===//
 
 #include "llvm/CodeGen/ReachingDefAnalysis.h"
+#include "llvm/ADT/PostOrderIterator.h"
 #include "llvm/ADT/SetOperations.h"
 #include "llvm/ADT/SmallSet.h"
+#include "llvm/CodeGen/LivePhysRegs.h"
 #include "llvm/CodeGen/LiveRegUnits.h"
 #include "llvm/CodeGen/MachineFrameInfo.h"
 #include "llvm/CodeGen/TargetInstrInfo.h"
@@ -287,6 +289,16 @@ void ReachingDefInfo::run(MachineFunction &mf) {
   TRI = STI.getRegisterInfo();
   TII = STI.getInstrInfo();
   LLVM_DEBUG(dbgs() << "********** REACHING DEFINITION ANALYSIS **********\n");
+
+  MachineFunctionProperties &Props = MF->getProperties();
+  if (!Props.hasTracksLiveness()) {
+    Props.setTracksLiveness();
+
+    SmallVector<MachineBasicBlock *> AllMBBsInPostOrder;
+    for (MachineBasicBlock *MBB : post_order(MF))
+      AllMBBsInPostOrder.push_back(MBB);
+    fullyRecomputeLiveIns(AllMBBsInPostOrder);
+  }
   init();
   traverse();
 }
diff --git a/llvm/test/CodeGen/RISCV/pr53662.mir b/llvm/test/CodeGen/RISCV/pr53662.mir
index dccad40368111..834bcbc1cf82c 100644
--- a/llvm/test/CodeGen/RISCV/pr53662.mir
+++ b/llvm/test/CodeGen/RISCV/pr53662.mir
@@ -18,15 +18,19 @@ body:             |
   ; CHECK-LABEL: name: b
   ; CHECK: bb.0:
   ; CHECK-NEXT:   successors: %bb.1(0x80000000)
+  ; CHECK-NEXT:   liveins: $x10
   ; CHECK-NEXT: {{  $}}
   ; CHECK-NEXT:   PseudoBR %bb.1
   ; CHECK-NEXT: {{  $}}
   ; CHECK-NEXT: bb.1:
   ; CHECK-NEXT:   successors: %bb.2(0x80000000)
+  ; CHECK-NEXT:   liveins: $x10
   ; CHECK-NEXT: {{  $}}
   ; CHECK-NEXT:   DBG_VALUE $noreg
   ; CHECK-NEXT: {{  $}}
   ; CHECK-NEXT: bb.2:
+  ; CHECK-NEXT:   liveins: $x10
+  ; CHECK-NEXT: {{  $}}
   ; CHECK-NEXT:   PseudoRET implicit killed $x10
   bb.0 :
     PseudoBR %bb.1
diff --git a/llvm/test/CodeGen/RISCV/rvv/fixed-vectors-emergency-slot.mir b/llvm/test/CodeGen/RISCV/rvv/fixed-vectors-emergency-slot.mir
index c728fcb8d8b0d..44f60a43a2790 100644
--- a/llvm/test/CodeGen/RISCV/rvv/fixed-vectors-emergency-slot.mir
+++ b/llvm/test/CodeGen/RISCV/rvv/fixed-vectors-emergency-slot.mir
@@ -47,7 +47,7 @@ body:             |
 
     SD $x10, %stack.0, 0
     SD $x10, %stack.2, 0
-    dead renamable $x15 = PseudoVSETIVLI 1, 72, implicit-def $vl, implicit-def $vtype
+    renamable $x15 = PseudoVSETIVLI 1, 72, implicit-def $vl, implicit-def $vtype
     VS1R_V killed renamable $v25, %stack.1 :: (store (<vscale x 1 x s64>) into %stack.1, align 8)
     ; This is here just to make all the eligible registers live at this point.
     ; This way when we replace the frame index %stack.1 with its actual address

SD $x10, %stack.0, 0
SD $x10, %stack.2, 0
dead renamable $x15 = PseudoVSETIVLI 1, 72, implicit-def $vl, implicit-def $vtype
renamable $x15 = PseudoVSETIVLI 1, 72, implicit-def $vl, implicit-def $vtype
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed dead here because it was wrong - $x15 is used later.

@mgudim
Copy link
Contributor Author

mgudim commented Nov 6, 2025

I am not sure that this is a proper way to solve the problem. My concern is that RDA is an analysis pass, but it may modify MIR by adding live-ins for every block. I suspect it could be a problem.

Please suggest other approaches if you have any ideas!

@mgudim mgudim force-pushed the rda_recompute_liveness branch from 86cae03 to 92ebb42 Compare November 6, 2025 14:45
We plan to use `ReachingDefinitionsAnalysis` in
`PrologEpilogInsertion`. In some tests this will be a problem because
`ReachingDefinitionAnalysis` will have to run on a function that  does
not have live-in info. This PR lets `ReachingDefinitionsAnalysis`
recompute live-ins.
@mgudim mgudim force-pushed the rda_recompute_liveness branch from 92ebb42 to 2e65450 Compare November 6, 2025 14:55
@lenary
Copy link
Member

lenary commented Nov 7, 2025

IIRC, RDA doesn't track function live-ins particularly well. ReachingDefAnalysis::getGlobalReachingDefs(...) doesn't model function live-ins at all, so you end up needing to check live-in-ness of a register separately.

I've worked around this in the past, but this issue would give me pause for using RDA in PEI, before the issue is resolved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants