[llvm] a4ade9e - [MachineVerifier] Handle the PHI node for verifyLiveVariables()

Kang Zhang via llvm-commits llvm-commits at lists.llvm.org
Wed Jul 29 08:49:00 PDT 2020


Author: Kang Zhang
Date: 2020-07-29T15:43:47Z
New Revision: a4ade9ed21e55946c52961a549ca4d23f9058b08

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

LOG: [MachineVerifier] Handle the PHI node for verifyLiveVariables()

Summary:
When doing MachineVerifier for LiveVariables, the MachineVerifier pass
will calculate the LiveVariables, and compares the result with the
result livevars pass gave. If they are different, verifyLiveVariables()
will give error.

But when we calculate the LiveVariables in MachineVerifier, we don't
consider the PHI node, while livevars considers.

This patch is to fix above bug.

Reviewed By: bjope

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

Added: 
    

Modified: 
    llvm/lib/CodeGen/MachineVerifier.cpp
    llvm/test/CodeGen/PowerPC/livevars-crash1.mir
    llvm/test/CodeGen/PowerPC/livevars-crash2.mir

Removed: 
    


################################################################################
diff  --git a/llvm/lib/CodeGen/MachineVerifier.cpp b/llvm/lib/CodeGen/MachineVerifier.cpp
index c1a2c4e0bc6e..e45e6963edb1 100644
--- a/llvm/lib/CodeGen/MachineVerifier.cpp
+++ b/llvm/lib/CodeGen/MachineVerifier.cpp
@@ -132,7 +132,8 @@ namespace {
       bool reachable = false;
 
       // Vregs that must be live in because they are used without being
-      // defined. Map value is the user.
+      // defined. Map value is the user. vregsLiveIn doesn't include regs
+      // that only are used by PHI nodes.
       RegMap vregsLiveIn;
 
       // Regs killed in MBB. They may be defined again, and will then be in both
@@ -2302,6 +2303,23 @@ void MachineVerifier::calcRegsRequired() {
       if (PInfo.addRequired(MInfo.vregsLiveIn))
         todo.insert(Pred);
     }
+
+    // Handle the PHI node.
+    for (const MachineInstr &MI : MBB.phis()) {
+      for (unsigned i = 1, e = MI.getNumOperands(); i != e; i += 2) {
+        // Skip those Operands which are undef regs or not regs.
+        if (!MI.getOperand(i).isReg() || !MI.getOperand(i).readsReg())
+          continue;
+
+        // Get register and predecessor for one PHI edge.
+        Register Reg = MI.getOperand(i).getReg();
+        const MachineBasicBlock *Pred = MI.getOperand(i + 1).getMBB();
+
+        BBInfo &PInfo = MBBInfoMap[Pred];
+        if (PInfo.addRequired(Reg))
+          todo.insert(Pred);
+      }
+    }
   }
 
   // Iteratively push vregsRequired to predecessors. This will converge to the

diff  --git a/llvm/test/CodeGen/PowerPC/livevars-crash1.mir b/llvm/test/CodeGen/PowerPC/livevars-crash1.mir
index 53337040e70a..6457a3d24236 100644
--- a/llvm/test/CodeGen/PowerPC/livevars-crash1.mir
+++ b/llvm/test/CodeGen/PowerPC/livevars-crash1.mir
@@ -1,6 +1,6 @@
-# RUN: not --crash llc -mtriple powerpc64le-unknown-linux-gnu %s -o - 2>&1 \
-# RUN:   -run-pass=livevars,phi-node-elimination -verify-machineinstrs \
-# RUN:   | FileCheck %s
+# RUN: llc -mtriple powerpc64le-unknown-linux-gnu %s -o - 2>&1 \
+# RUN:   -run-pass=livevars,phi-node-elimination -verify-machineinstrs | \
+# RUN:  FileCheck %s
 
 --- |
   ; Function Attrs: noreturn nounwind
@@ -82,9 +82,46 @@ body:             |
     STD %3, 0, %4 :: (store 8 into %ir.p)
     B %bb.1
 
+  ; CHECK-LABEL: name: zext_free
+  ; CHECK: bb.0.entry:
+  ; CHECK:   successors: %bb.1(0x80000000)
+  ; CHECK:   liveins: $x3
+
+  ; CHECK:   %4:g8rc_and_g8rc_nox0 = COPY killed $x3
+  ; CHECK:   %0:g8rc = LD 0, %4 :: (dereferenceable load 8 from %ir.p)
+  ; CHECK:    %12:g8rc_and_g8rc_nox0 = COPY killed %0
+
+  ; CHECK: bb.1.loop:
+  ; CHECK:   successors: %bb.1(0x20000000), %bb.2(0x60000000)
+
+  ; CHECK:   %1:g8rc_and_g8rc_nox0 = COPY killed %12
+  ; CHECK:   %5:gprc = LBZ 0, %1 :: (load 1 from %ir.0)
+  ; CHECK:   %6:crrc = CMPWI killed %5, 0
+  ; CHEXK:   %7:crbitrc = COPY killed %6.sub_eq
+  ; CHECK:   %2:g8rc = nuw ADDI8 %1, 1
+  ; CHECK:   STD %2, 0, %4 :: (store 8 into %ir.p)
+  ; CHECK:   %8:gprc = LBZ 1, %1 :: (load 1 from %ir.incdec.ptr)
+  ; CHECK:   %12:g8rc_and_g8rc_nox0 = COPY %2
+  ; CHECK:   BCn killed %7, %bb.1
+  ; CHECK:   B %bb.2
+
+  ; CHECK: bb.2.loop:
+  ; CHECK:   successors: %bb.3(0x55555555), %bb.1(0x2aaaaaab)
+
+  ; CHECK:   %9:crrc = CMPWI killed %8, 0
+  ; CHECK:   %10:crbitrc = COPY killed %9.sub_eq
+  ; CHECK:   %12:g8rc_and_g8rc_nox0 = COPY killed %2
+  ; CHECK:   BC killed %10, %bb.1
+  ; CHECK:   B %bb.3
+
+  ; CHECK: bb.3.if.then3:
+  ; CHECK:   successors: %bb.1(0x80000000)
+
+  ; CHECK:   %3:g8rc = nuw ADDI8 killed %1, 2
+  ; CHECK:   STD %3, 0, %4 :: (store 8 into %ir.p)
+  ; CHECK:   %12:g8rc_and_g8rc_nox0 = COPY killed %3
+  ; CHECK:   B %bb.1
+
+
 ...
-# CHECK-LABEL: Bad machine code: LiveVariables: Block should not be in AliveBlocks
-# CHECK-NEXT:  - function:    zext_free
-# CHECK-NEXT:  - basic block: %bb.2 loop
-# CHECK-NEXT:  Virtual register %2 is not needed live through the block.
-# CHECK-NEXT:  LLVM ERROR: Found 1 machine code errors.
+

diff  --git a/llvm/test/CodeGen/PowerPC/livevars-crash2.mir b/llvm/test/CodeGen/PowerPC/livevars-crash2.mir
index e62db1ed4cb2..1bdf0419a8f5 100644
--- a/llvm/test/CodeGen/PowerPC/livevars-crash2.mir
+++ b/llvm/test/CodeGen/PowerPC/livevars-crash2.mir
@@ -1,6 +1,6 @@
-# RUN: not --crash llc -mtriple powerpc64le-unknown-linux-gnu %s -o - 2>&1 \
-# RUN:   -run-pass=livevars,phi-node-elimination -verify-machineinstrs \
-# RUN:   | FileCheck %s
+# RUN: llc -mtriple powerpc64le-unknown-linux-gnu %s -o - 2>&1 \
+# RUN:   -run-pass=livevars,phi-node-elimination -verify-machineinstrs | \
+# RUN:   FileCheck %s
 
 --- |
   define float @testfloatslt(float %c1, float %c2, float %c3, float %c4, float %a1, float %a2) {
@@ -176,20 +176,44 @@ body:             |
     STD %3, 0, %4 :: (store 8 into %ir.p)
     B %bb.1
 
-...
+  ; CHECK-LABEL: name: testfloatslt
+  ; CHECK: bb.0.entry:
+  ; CHECK:   successors: %bb.1(0x80000000)
+  ; CHECK:   liveins: $x3
+
+  ; CHECK:   %4:g8rc_and_g8rc_nox0 = COPY killed $x3
+  ; CHECK:   %0:g8rc = LD 0, %4 :: (dereferenceable load 8 from %ir.p)
+  ; CHECK:   %12:g8rc_and_g8rc_nox0 = COPY killed %0
+
+  ; CHECK: bb.1.loop:
+  ; CHECK:   successors: %bb.1(0x20000000), %bb.2(0x60000000)
+
+  ; CHECK:   %1:g8rc_and_g8rc_nox0 = COPY killed %12
+  ; CHECK:   %5:gprc = LBZ 0, %1 :: (load 1 from %ir.0)
+  ; CHECK:   %6:crrc = CMPWI killed %5, 0
+  ; CEHCK:   %7:crbitrc = COPY killed %6.sub_eq
+  ; CHECK:   %2:g8rc = nuw ADDI8 %1, 1
+  ; CHECK:   STD %2, 0, %4 :: (store 8 into %ir.p)
+  ; CHECK:   %8:gprc = LBZ 1, %1 :: (load 1 from %ir.incdec.ptr)
+  ; CHECK:   %12:g8rc_and_g8rc_nox0 = COPY %2
+  ; CHECK:   BCn killed %7, %bb.1
+  ; CHECK:   B %bb.2
 
-# CHECK-LABEL: Bad machine code: LiveVariables: Block should not be in AliveBlocks
-# CHECK-NEXT:  - function:    testfloatslt
-# CHECK-NEXT:  - basic block: %bb.1 entry
-# CHECK-NEXT:  Virtual register %4 is not needed live through the block.
+  ; CHECK: bb.2.loop:
+  ; CHECK:   successors: %bb.3(0x55555555), %bb.1(0x2aaaaaab)
 
-# CHECK-LABEL: Bad machine code: LiveVariables: Block should not be in AliveBlocks
-# CHECK-NEXT:  - function:    testfloatslt
-# CHECK-NEXT:  - basic block: %bb.1 entry
-# CHECK-NEXT:  Virtual register %5 is not needed live through the block.
+  ; CHECK:   %9:crrc = CMPWI killed %8, 0
+  ; CHECK:   %10:crbitrc = COPY killed %9.sub_eq
+  ; CHECK:   %12:g8rc_and_g8rc_nox0 = COPY killed %2
+  ; CHECK:   BC killed %10, %bb.1
+  ; CHECK:   B %bb.3
 
-# CHECK-LABEL: Bad machine code: LiveVariables: Block should not be in AliveBlocks
-# CHECK-NEXT:  - function:    testfloatslt
-# CHECK-NEXT:  - basic block: %bb.2 entry
-# CHECK-NEXT:  Virtual register %5 is not needed live through the block.
-# CHECK-NEXT:  LLVM ERROR: Found 3 machine code errors.
+  ; CHECK: bb.3.if.then3:
+  ; CHECK:   successors: %bb.1(0x80000000)
+
+  ; CHECK:   %3:g8rc = nuw ADDI8 killed %1, 2
+  ; CHECK:   STD %3, 0, %4 :: (store 8 into %ir.p)
+  ; CHECK:   %12:g8rc_and_g8rc_nox0 = COPY killed %3
+  ; CHECK:   B %bb.1
+
+...


        


More information about the llvm-commits mailing list