[llvm] [CodeGen] Have tryToSinkCopy detect regmask clobbers just like LiveRegUnitees does (PR #88023)

via llvm-commits llvm-commits at lists.llvm.org
Mon Apr 8 10:55:33 PDT 2024


https://github.com/AtariDreams updated https://github.com/llvm/llvm-project/pull/88023

>From b6afbcdb12bad0b4e5c561449ee8b9e474b32508 Mon Sep 17 00:00:00 2001
From: Rose <gfunni234 at gmail.com>
Date: Mon, 8 Apr 2024 11:31:07 -0400
Subject: [PATCH] [CodeGen] Have tryToSinkCopy detect regmask clobbers just
 like LiveRegUnits does

Otherwise, hasRegisterDependency will just skip the regmask operands that clobber registers.
---
 llvm/lib/CodeGen/MachineSink.cpp | 30 +++++++++++++++++++++---------
 1 file changed, 21 insertions(+), 9 deletions(-)

diff --git a/llvm/lib/CodeGen/MachineSink.cpp b/llvm/lib/CodeGen/MachineSink.cpp
index c3a1d3759882d8..81dea0e1651ec2 100644
--- a/llvm/lib/CodeGen/MachineSink.cpp
+++ b/llvm/lib/CodeGen/MachineSink.cpp
@@ -1958,27 +1958,39 @@ static bool hasRegisterDependency(MachineInstr *MI,
                                   SmallVectorImpl<unsigned> &UsedOpsInCopy,
                                   SmallVectorImpl<unsigned> &DefedRegsInCopy,
                                   LiveRegUnits &ModifiedRegUnits,
-                                  LiveRegUnits &UsedRegUnits) {
+                                  LiveRegUnits &UsedRegUnits,
+                                  const TargetRegisterInfo *TRI) {
   bool HasRegDependency = false;
   for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
     MachineOperand &MO = MI->getOperand(i);
+    if (MO.isRegMask()) {
+      for (unsigned U = 0, E = TRI->getNumRegUnits(); U != E; ++U) {
+        for (MCRegUnitRootIterator RootReg(U, TRI); RootReg.isValid();
+             ++RootReg) {
+          if (MachineOperand::clobbersPhysReg(MO.getRegMask(), *RootReg)) {
+            if (!UsedRegUnits.available(*RootReg)) {
+              HasRegDependency = true;
+              break;
+            }
+          }
+          DefedRegsInCopy.push_back(*RootReg);
+        }
+      }
+    }
     if (!MO.isReg())
       continue;
     Register Reg = MO.getReg();
     if (!Reg)
       continue;
     if (MO.isDef()) {
-      if (!ModifiedRegUnits.available(Reg) || !UsedRegUnits.available(Reg)) {
+      if (!UsedRegUnits.available(Reg)) {
         HasRegDependency = true;
         break;
       }
       DefedRegsInCopy.push_back(Reg);
 
-      // FIXME: instead of isUse(), readsReg() would be a better fix here,
-      // For example, we can ignore modifications in reg with undef. However,
-      // it's not perfectly clear if skipping the internal read is safe in all
-      // other targets.
-    } else if (MO.isUse()) {
+      // Ignore undef uses and internal reads.
+    } else if (MO.readsReg()) {
       if (!ModifiedRegUnits.available(Reg)) {
         HasRegDependency = true;
         break;
@@ -2028,7 +2040,7 @@ bool PostRAMachineSinking::tryToSinkCopy(MachineBasicBlock &CurBB,
           // Bail if we can already tell the sink would be rejected, rather
           // than needlessly accumulating lots of DBG_VALUEs.
           if (hasRegisterDependency(&MI, UsedOpsInCopy, DefedRegsInCopy,
-                                    ModifiedRegUnits, UsedRegUnits)) {
+                                    ModifiedRegUnits, UsedRegUnits, TRI)) {
             IsValid = false;
             break;
           }
@@ -2061,7 +2073,7 @@ bool PostRAMachineSinking::tryToSinkCopy(MachineBasicBlock &CurBB,
 
     // Don't sink the COPY if it would violate a register dependency.
     if (hasRegisterDependency(&MI, UsedOpsInCopy, DefedRegsInCopy,
-                              ModifiedRegUnits, UsedRegUnits)) {
+                              ModifiedRegUnits, UsedRegUnits, TRI)) {
       LiveRegUnits::accumulateUsedDefed(MI, ModifiedRegUnits, UsedRegUnits,
                                         TRI);
       continue;



More information about the llvm-commits mailing list