[llvm] [PowerPC] Optimize allocation of Conditional Register (PR #69299)

Kai Luo via llvm-commits llvm-commits at lists.llvm.org
Tue Oct 17 02:57:58 PDT 2023


https://github.com/bzEq updated https://github.com/llvm/llvm-project/pull/69299

>From a3a0cf0813b1429c2f5150feba1e1807edaa9acc Mon Sep 17 00:00:00 2001
From: Kai Luo <lkail at cn.ibm.com>
Date: Tue, 17 Oct 2023 08:18:24 +0000
Subject: [PATCH 1/2] Adjust CR allocation

---
 llvm/lib/Target/PowerPC/PPCRegisterInfo.cpp | 57 +++++++++++++++++++--
 1 file changed, 52 insertions(+), 5 deletions(-)

diff --git a/llvm/lib/Target/PowerPC/PPCRegisterInfo.cpp b/llvm/lib/Target/PowerPC/PPCRegisterInfo.cpp
index 7d913a77cc71550..28d9d09353a6382 100644
--- a/llvm/lib/Target/PowerPC/PPCRegisterInfo.cpp
+++ b/llvm/lib/Target/PowerPC/PPCRegisterInfo.cpp
@@ -565,6 +565,8 @@ bool PPCRegisterInfo::getRegAllocationHints(Register VirtReg,
                                             const VirtRegMap *VRM,
                                             const LiveRegMatrix *Matrix) const {
   const MachineRegisterInfo *MRI = &MF.getRegInfo();
+  const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
+  const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo();
 
   // Call the base implementation first to set any hints based on the usual
   // heuristics and decide what the return value should be. We want to return
@@ -582,15 +584,20 @@ bool PPCRegisterInfo::getRegAllocationHints(Register VirtReg,
   if (MF.getSubtarget<PPCSubtarget>().isISAFuture())
     return BaseImplRetVal;
 
-  // We are interested in instructions that copy values to ACC/UACC.
-  // The copy into UACC will be simply a COPY to a subreg so we
-  // want to allocate the corresponding physical subreg for the source.
-  // The copy into ACC will be a BUILD_UACC so we want to allocate
-  // the same number UACC for the source.
+  MachineBasicBlock *LastUseMBB = nullptr;
+  bool UseInOneMBB = true;
   const TargetRegisterClass *RegClass = MRI->getRegClass(VirtReg);
   for (MachineInstr &Use : MRI->reg_nodbg_instructions(VirtReg)) {
+    if (LastUseMBB && Use.getParent() != LastUseMBB)
+      UseInOneMBB = false;
+    LastUseMBB = Use.getParent();
     const MachineOperand *ResultOp = nullptr;
     Register ResultReg;
+    // We are interested in instructions that copy values to ACC/UACC.
+    // The copy into UACC will be simply a COPY to a subreg so we
+    // want to allocate the corresponding physical subreg for the source.
+    // The copy into ACC will be a BUILD_UACC so we want to allocate
+    // the same number UACC for the source.
     switch (Use.getOpcode()) {
     case TargetOpcode::COPY: {
       ResultOp = &Use.getOperand(0);
@@ -628,6 +635,46 @@ bool PPCRegisterInfo::getRegAllocationHints(Register VirtReg,
     }
     }
   }
+
+  // In single MBB, allocate different CRs for different definitions can improve
+  // performance.
+  if (UseInOneMBB && LastUseMBB &&
+      (RegClass->hasSuperClassEq(&PPC::CRRCRegClass) ||
+       RegClass->hasSuperClassEq(&PPC::CRBITRCRegClass))) {
+    std::set<MCPhysReg> ModifiedRegisters;
+    bool Skip = true;
+    // Scan from the last instruction writes VirtReg to the beginning of the
+    // MBB.
+    for (MachineInstr &MI :
+         llvm::make_range(LastUseMBB->rbegin(), LastUseMBB->rend())) {
+      if (MI.isDebugInstr())
+        continue;
+      if (MI.modifiesRegister(VirtReg, TRI))
+        Skip = false;
+      if (Skip)
+        continue;
+      for (MachineOperand &MO : MI.operands()) {
+        if (!MO.isReg() || !MO.getReg() || !MO.getReg().isVirtual() ||
+            !MO.isDef())
+          continue;
+        MCPhysReg PhysReg = VRM->getPhys(MO.getReg());
+        if (PhysReg == VirtRegMap::NO_PHYS_REG)
+          continue;
+        llvm::copy_if(
+            TRI->superregs_inclusive(PhysReg),
+            std::inserter(ModifiedRegisters, ModifiedRegisters.begin()),
+            [&](MCPhysReg SR) { return PPC::CRRCRegClass.contains(SR); });
+      }
+    }
+    llvm::copy_if(llvm::make_range(Order.begin(), Order.end()),
+                  std::back_inserter(Hints), [&](MCPhysReg Reg) {
+                    return llvm::all_of(TRI->superregs_inclusive(Reg),
+                                        [&](MCPhysReg SR) {
+                                          return !ModifiedRegisters.count(SR);
+                                        });
+                  });
+  }
+
   return BaseImplRetVal;
 }
 

>From e2891ff0d15f08e1fadd3dedc5cfb2ea7f84b2ea Mon Sep 17 00:00:00 2001
From: Kai Luo <lkail at cn.ibm.com>
Date: Tue, 17 Oct 2023 09:57:44 +0000
Subject: [PATCH 2/2] Minor

---
 llvm/lib/Target/PowerPC/PPCRegisterInfo.cpp | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/llvm/lib/Target/PowerPC/PPCRegisterInfo.cpp b/llvm/lib/Target/PowerPC/PPCRegisterInfo.cpp
index 28d9d09353a6382..3c27e82fb6cb9fb 100644
--- a/llvm/lib/Target/PowerPC/PPCRegisterInfo.cpp
+++ b/llvm/lib/Target/PowerPC/PPCRegisterInfo.cpp
@@ -655,11 +655,9 @@ bool PPCRegisterInfo::getRegAllocationHints(Register VirtReg,
         continue;
       for (MachineOperand &MO : MI.operands()) {
         if (!MO.isReg() || !MO.getReg() || !MO.getReg().isVirtual() ||
-            !MO.isDef())
+            !MO.isDef() || !VRM->hasPhys(MO.getReg()))
           continue;
         MCPhysReg PhysReg = VRM->getPhys(MO.getReg());
-        if (PhysReg == VirtRegMap::NO_PHYS_REG)
-          continue;
         llvm::copy_if(
             TRI->superregs_inclusive(PhysReg),
             std::inserter(ModifiedRegisters, ModifiedRegisters.begin()),
@@ -668,6 +666,10 @@ bool PPCRegisterInfo::getRegAllocationHints(Register VirtReg,
     }
     llvm::copy_if(llvm::make_range(Order.begin(), Order.end()),
                   std::back_inserter(Hints), [&](MCPhysReg Reg) {
+                    // if (TRI->regsOverlap(Reg, PPC::CR2) ||
+                    //     TRI->regsOverlap(Reg, PPC::CR3) ||
+                    //     TRI->regsOverlap(Reg, PPC::CR4))
+                    //   return false;
                     return llvm::all_of(TRI->superregs_inclusive(Reg),
                                         [&](MCPhysReg SR) {
                                           return !ModifiedRegisters.count(SR);



More information about the llvm-commits mailing list