[llvm] 19ce5e5 - RAGreedyStats: Ignore identity COPYs; count COPYs from/to physregs

Matthias Braun via llvm-commits llvm-commits at lists.llvm.org
Wed Aug 17 12:55:34 PDT 2022


Author: Matthias Braun
Date: 2022-08-17T12:53:29-07:00
New Revision: 19ce5e515fc5059fad8ec9aee1389ac9d109b647

URL: https://github.com/llvm/llvm-project/commit/19ce5e515fc5059fad8ec9aee1389ac9d109b647
DIFF: https://github.com/llvm/llvm-project/commit/19ce5e515fc5059fad8ec9aee1389ac9d109b647.diff

LOG: RAGreedyStats: Ignore identity COPYs; count COPYs from/to physregs

Improve copy statistics:

- Count copies from or to physical registers: They are used to model function parameters and calling conventions and the register allocator optimizes for them.
- Check physical registers assigned to virtual registers and stop counting "identity" `COPY`s where source and destination is the same physical registers; they will be removed in the `virtregmap` pass anyway.

Differential Revision: https://reviews.llvm.org/D131932

Added: 
    

Modified: 
    llvm/lib/CodeGen/RegAllocGreedy.cpp
    llvm/test/CodeGen/AArch64/arm64-spill-remarks.ll
    llvm/test/CodeGen/X86/statepoint-ra.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/CodeGen/RegAllocGreedy.cpp b/llvm/lib/CodeGen/RegAllocGreedy.cpp
index 9c6cb7c3a4e2..fcd842b9aae5 100644
--- a/llvm/lib/CodeGen/RegAllocGreedy.cpp
+++ b/llvm/lib/CodeGen/RegAllocGreedy.cpp
@@ -2369,11 +2369,25 @@ RAGreedy::RAGreedyStats RAGreedy::computeStats(MachineBasicBlock &MBB) {
   };
   for (MachineInstr &MI : MBB) {
     if (MI.isCopy()) {
-      MachineOperand &Dest = MI.getOperand(0);
-      MachineOperand &Src = MI.getOperand(1);
-      if (Dest.isReg() && Src.isReg() && Dest.getReg().isVirtual() &&
-          Src.getReg().isVirtual())
-        ++Stats.Copies;
+      const MachineOperand &Dest = MI.getOperand(0);
+      const MachineOperand &Src = MI.getOperand(1);
+      Register SrcReg = Src.getReg();
+      Register DestReg = Dest.getReg();
+      // Only count `COPY`s with a virtual register as source or destination.
+      if (SrcReg.isVirtual() || DestReg.isVirtual()) {
+        if (SrcReg.isVirtual()) {
+          SrcReg = VRM->getPhys(SrcReg);
+          if (Src.getSubReg())
+            SrcReg = TRI->getSubReg(SrcReg, Src.getSubReg());
+        }
+        if (DestReg.isVirtual()) {
+          DestReg = VRM->getPhys(DestReg);
+          if (Dest.getSubReg())
+            DestReg = TRI->getSubReg(DestReg, Dest.getSubReg());
+        }
+        if (SrcReg != DestReg)
+          ++Stats.Copies;
+      }
       continue;
     }
 

diff  --git a/llvm/test/CodeGen/AArch64/arm64-spill-remarks.ll b/llvm/test/CodeGen/AArch64/arm64-spill-remarks.ll
index 72d486d7760d..647bffa69a08 100644
--- a/llvm/test/CodeGen/AArch64/arm64-spill-remarks.ll
+++ b/llvm/test/CodeGen/AArch64/arm64-spill-remarks.ll
@@ -21,16 +21,16 @@
 ; (loop2:)
 ; REMARK: remark: /tmp/kk.c:2:20: 1 spills 1.000000e+04 total spills cost 1 reloads 1.000000e+04 total reloads cost generated in loop{{$}}
 ; (loop:)
-; REMARK: remark: /tmp/kk.c:1:20: 2 spills 1.010000e+04 total spills cost 2 reloads 1.010000e+04 total reloads cost generated in loop{{$}}
+; REMARK: remark: /tmp/kk.c:1:20: 2 spills 1.010000e+04 total spills cost 2 reloads 1.010000e+04 total reloads cost 1 virtual registers copies 1.000000e+02 total copies cost generated in loop{{$}}
 ; (func:)
-; REMARK: remark: /tmp/kk.c:1:1: 3 spills 1.020000e+04 total spills cost 3 reloads 1.020000e+04 total reloads cost generated in function
+; REMARK: remark: /tmp/kk.c:1:1: 3 spills 1.020000e+04 total spills cost 3 reloads 1.020000e+04 total reloads cost 3 virtual registers copies 1.020000e+02 total copies cost generated in function{{$}}
 
 ; (loop3:)
 ; HOTNESS: remark: /tmp/kk.c:3:20: 1 spills 1.000000e+02 total spills cost 1 reloads 1.000000e+02 total reloads cost generated in loop (hotness: 300)
 ; (loop2:)
 ; HOTNESS: remark: /tmp/kk.c:2:20: 1 spills 1.000000e+04 total spills cost 1 reloads 1.000000e+04 total reloads cost generated in loop (hotness: 30000)
 ; (loop:)
-; HOTNESS: remark: /tmp/kk.c:1:20: 2 spills 1.010000e+04 total spills cost 2 reloads 1.010000e+04 total reloads cost generated in loop (hotness: 300)
+; HOTNESS: remark: /tmp/kk.c:1:20: 2 spills 1.010000e+04 total spills cost 2 reloads 1.010000e+04 total reloads cost 1 virtual registers copies 1.000000e+02 total copies cost generated in loop (hotness: 300)
 
 ; NO_REMARK-NOT: remark
 
@@ -86,6 +86,10 @@
 ; YAML:   - String:          ' reloads '
 ; YAML:   - TotalReloadsCost: '1.010000e+04'
 ; YAML:   - String:          ' total reloads cost '
+; YAML:   - NumVRCopies:     '1'
+; YAML:   - String:          ' virtual registers copies '
+; YAML:   - TotalCopiesCost: '1.000000e+02'
+; YAML:   - String:          ' total copies cost '
 ; YAML:   - String:          generated in loop
 ; YAML: ...
 ; YAML: --- !Missed
@@ -103,6 +107,10 @@
 ; YAML:   - String:          ' reloads '
 ; YAML:   - TotalReloadsCost: '1.020000e+04'
 ; YAML:   - String:          ' total reloads cost '
+; YAML:   - NumVRCopies:     '3'
+; YAML:   - String:          ' virtual registers copies '
+; YAML:   - TotalCopiesCost: '1.020000e+02'
+; YAML:   - String:          ' total copies cost '
 ; YAML:   - String:          generated in function
 ; YAML: ...
 

diff  --git a/llvm/test/CodeGen/X86/statepoint-ra.ll b/llvm/test/CodeGen/X86/statepoint-ra.ll
index 25e2f6a112ae..9edaed836a18 100644
--- a/llvm/test/CodeGen/X86/statepoint-ra.ll
+++ b/llvm/test/CodeGen/X86/statepoint-ra.ll
@@ -20,10 +20,6 @@ target triple = "x86_64-unknown-linux-gnu"
 ;YAML:   - String:          ' total reloads cost '
 ;YAML:   - NumZeroCostFoldedReloads: '20'
 ;YAML:   - String:          ' zero cost folded reloads '
-;YAML:   - NumVRCopies:     '2'
-;YAML:   - String:          ' virtual registers copies '
-;YAML:   - TotalCopiesCost: '8.882868e-16'
-;YAML:   - String:          ' total copies cost '
 ;YAML:   - String:          generated in function
 
 define void @barney(ptr addrspace(1) %arg, double %arg1, double %arg2, double %arg3, double %arg4, double %arg5, double %arg6, double %arg7, double %arg8, double %arg9, double %arg10, double %arg11, double %arg12) gc "statepoint-example" personality ptr @widget {


        


More information about the llvm-commits mailing list