[llvm] r360278 - [RegAllocFast] Scan physcial reg definitions before assigning virtual reg definitions

Quentin Colombet via llvm-commits llvm-commits at lists.llvm.org
Wed May 8 11:30:26 PDT 2019


Author: qcolombet
Date: Wed May  8 11:30:26 2019
New Revision: 360278

URL: http://llvm.org/viewvc/llvm-project?rev=360278&view=rev
Log:
[RegAllocFast] Scan physcial reg definitions before assigning virtual reg definitions

When assigning the definitions of an instruction we were updating
the available registers while walking the definitions. Some of
those definitions may be from physical registers and thus, they are
not available for other definitions to take, but by the time we see
that we may have already assign these registers to another
virtual register.

Fix that by walking through all the definitions and mark as unavailable
the physical register definitions, then do the virtual register assignments.

PR41790

Added:
    llvm/trunk/test/CodeGen/X86/virtreg-physreg-def-regallocfast.mir
Modified:
    llvm/trunk/lib/CodeGen/RegAllocFast.cpp

Modified: llvm/trunk/lib/CodeGen/RegAllocFast.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/RegAllocFast.cpp?rev=360278&r1=360277&r2=360278&view=diff
==============================================================================
--- llvm/trunk/lib/CodeGen/RegAllocFast.cpp (original)
+++ llvm/trunk/lib/CodeGen/RegAllocFast.cpp Wed May  8 11:30:26 2019
@@ -1058,6 +1058,20 @@ void RegAllocFast::allocateInstruction(M
   }
 
   // Third scan.
+  // Mark all physreg defs as used before allocating virtreg defs.
+  for (unsigned I = 0; I != DefOpEnd; ++I) {
+    const MachineOperand &MO = MI.getOperand(I);
+    if (!MO.isReg() || !MO.isDef() || !MO.getReg() || MO.isEarlyClobber())
+      continue;
+    unsigned Reg = MO.getReg();
+
+    if (!Reg || !TargetRegisterInfo::isPhysicalRegister(Reg) ||
+        !MRI->isAllocatable(Reg))
+      continue;
+    definePhysReg(MI, Reg, MO.isDead() ? regFree : regReserved);
+  }
+
+  // Fourth scan.
   // Allocate defs and collect dead defs.
   for (unsigned I = 0; I != DefOpEnd; ++I) {
     const MachineOperand &MO = MI.getOperand(I);
@@ -1065,11 +1079,9 @@ void RegAllocFast::allocateInstruction(M
       continue;
     unsigned Reg = MO.getReg();
 
-    if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
-      if (!MRI->isAllocatable(Reg)) continue;
-      definePhysReg(MI, Reg, MO.isDead() ? regFree : regReserved);
+    // We have already dealt with phys regs in the previous scan.
+    if (TargetRegisterInfo::isPhysicalRegister(Reg))
       continue;
-    }
     MCPhysReg PhysReg = defineVirtReg(MI, I, Reg, CopySrcReg);
     if (setPhysReg(MI, MI.getOperand(I), PhysReg)) {
       VirtDead.push_back(Reg);

Added: llvm/trunk/test/CodeGen/X86/virtreg-physreg-def-regallocfast.mir
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/CodeGen/X86/virtreg-physreg-def-regallocfast.mir?rev=360278&view=auto
==============================================================================
--- llvm/trunk/test/CodeGen/X86/virtreg-physreg-def-regallocfast.mir (added)
+++ llvm/trunk/test/CodeGen/X86/virtreg-physreg-def-regallocfast.mir Wed May  8 11:30:26 2019
@@ -0,0 +1,19 @@
+# RUN: llc -o - -mtriple=x86_64-- -run-pass=regallocfast %s | FileCheck %s
+# Fast regalloc used to not collect physical register definitions
+# before walking and assigning the virtual definition.
+# Therefore it was possible for a virtual definition to end up
+# using the same register as a later (in terms of operand list) physical
+# register.
+# Check this does not happen.
+#
+# PR41790
+---
+name: instruction_with_1virtreg_1physreg_defs
+tracksRegLiveness: true
+body: |
+  bb.0:
+    ; CHECK-NOT: $rax = KILL implicit-def dead $rax
+    %0:gr64 = KILL implicit-def dead $rax
+    KILL killed %0
+    RET 0
+...




More information about the llvm-commits mailing list