[AMDGPU] Fix crash in phi-elimination hook.
authorMichael Liao <michael.hliao@gmail.com>
Wed, 11 Sep 2019 19:55:20 +0000 (19:55 +0000)
committerMichael Liao <michael.hliao@gmail.com>
Wed, 11 Sep 2019 19:55:20 +0000 (19:55 +0000)
Summary: - Pre-check in case there's just a single PHI insn.

Reviewers: alex-t, rampitec, arsenm

Subscribers: kzhuravl, jvesely, wdng, nhaehnle, dstuttard, tpr, t-tye, hiraditya, llvm-commits, yaxunl

Tags: #llvm

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

llvm-svn: 371649

llvm/lib/Target/AMDGPU/SIInstrInfo.cpp
llvm/test/CodeGen/AMDGPU/phi-elimination-assertion.mir

index c8410d2..fe57435 100644 (file)
@@ -6415,11 +6415,13 @@ MachineInstr *SIInstrInfo::createPHIDestinationCopy(
     MachineBasicBlock &MBB, MachineBasicBlock::iterator LastPHIIt,
     const DebugLoc &DL, Register Src, Register Dst) const {
   auto Cur = MBB.begin();
-  do {
+  while (Cur != MBB.end()) {
     if (!Cur->isPHI() && Cur->readsRegister(Dst))
       return BuildMI(MBB, Cur, DL, get(TargetOpcode::COPY), Dst).addReg(Src);
     ++Cur;
-  } while (Cur != MBB.end() && Cur != LastPHIIt);
+    if (Cur == LastPHIIt)
+      break;
+  }
 
   return TargetInstrInfo::createPHIDestinationCopy(MBB, LastPHIIt, DL, Src,
                                                    Dst);
index 094d2ee..e2e6ea7 100644 (file)
@@ -67,3 +67,29 @@ body:             |
 # CHECK-NEXT:     dead %3:sreg_32_xm0 = IMPLICIT_DEF
 # CHECK-NEXT:     %2:sreg_32_xm0 = COPY killed %4
 # CHECK-NEXT:     S_NOP 0, implicit killed %2
+
+
+# The following test crashes in phi-elimination hooks.
+#
+
+---
+name:            bax
+tracksRegLiveness: true
+body:             |
+  bb.0:
+    S_CBRANCH_SCC0 %bb.2, implicit undef $scc
+
+  bb.1:
+    %1:sreg_32_xm0 = S_MOV_B32 255
+    S_BRANCH %bb.3
+
+  bb.2:
+    %2:sreg_32_xm0 = S_MOV_B32 254
+
+  bb.3:
+    %3:sreg_32_xm0 = PHI %2, %bb.2, %1, %bb.1
+...
+
+# CHECK-LABEL: name:            bax
+# CHECK:   bb.3:
+# CHECK-NEXT:     %2:sreg_32_xm0 = COPY killed %3