Index: lib/CodeGen/PHIElimination.cpp =================================================================== --- lib/CodeGen/PHIElimination.cpp +++ lib/CodeGen/PHIElimination.cpp @@ -197,12 +197,11 @@ /// EliminatePHINodes - Eliminate phi nodes by inserting copy instructions in /// predecessor basic blocks. bool PHIElimination::EliminatePHINodes(MachineFunction &MF, - MachineBasicBlock &MBB) { + MachineBasicBlock &MBB) { if (MBB.empty() || !MBB.front().isPHI()) return false; // Quick exit for basic blocks without PHIs. - // Get an iterator to the first instruction after the last PHI node (this may - // also be the end of the basic block). + // Get an iterator to the last PHI node. MachineBasicBlock::iterator LastPHIIt = std::prev(MBB.SkipPHIsAndLabels(MBB.begin())); Index: test/CodeGen/PowerPC/2013-07-01-PHIElimBug.ll =================================================================== --- test/CodeGen/PowerPC/2013-07-01-PHIElimBug.ll +++ test/CodeGen/PowerPC/2013-07-01-PHIElimBug.ll @@ -1,4 +1,8 @@ -; RUN: llc < %s -verify-machineinstrs | FileCheck %s +; Nowdays we need to use -O1 (or maybe also -O0) to reproduce the original problem from PR16508. +; I added a -O1 run here, but I also added a mir-reproducer as 2013-07-01-PHIElimBug.mir that +; should be less sensitive to changes in earlier passes and optimization level. +; RUN: llc < %s -O1 -verify-machineinstrs | FileCheck %s +; RUN: llc < %s -O3 -verify-machineinstrs | FileCheck %s target datalayout = "E-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-f128:128:128-v128:128:128-n32:64" target triple = "powerpc64-unknown-linux-gnu" Index: test/CodeGen/PowerPC/2013-07-01-PHIElimBug.mir =================================================================== --- /dev/null +++ test/CodeGen/PowerPC/2013-07-01-PHIElimBug.mir @@ -0,0 +1,83 @@ +# RUN: llc -mtriple powerpc64-unknown-linux-gnu -run-pass livevars -run-pass phi-node-elimination -verify-machineinstrs -o - %s | FileCheck %s + +--- | + target datalayout = "E-m:e-i64:64-n32:64" + target triple = "powerpc64-unknown-linux-gnu" + + @g_51 = external global [8 x i32], align 4 + define void @func_7() { + bb.0: + ret void + } +... +--- +name: func_7 +tracksRegLiveness: true +body: | + bb.0: + liveins: $x2 + + %0:g8rc_and_g8rc_nox0 = ADDIStocHA $x2, @g_51 + %1:g8rc_and_g8rc_nox0 = LDtocL @g_51, killed %0, implicit $x2 :: (load 8) + %2:gprc = LI 0 + %3:crrc = CMPLWI killed %2, 0 + BCC 76, killed %3, %bb.2 + + bb.1: + %4:g8rc = ADDI8 killed %1, 24 + B %bb.4 + + bb.2: + %5:g8rc = ADDI8 killed %1, 24 + %6:g8rc = LI8 0 + %7:crrc = CMPLWI undef %8:gprc, 20 + + bb.3: + %9:g8rc = PHI %5, %bb.2, %10, %bb.3 + %10:g8rc = PHI %6, %bb.2, undef %11:g8rc, %bb.3 + BCC 68, %7, %bb.3 + B %bb.4 + + bb.4: + %12:g8rc_and_g8rc_nox0 = PHI %4, %bb.1, %9, %bb.3 + %13:g8rc = LI8 0 + STW8 killed %13, 0, killed %12 :: (store 4) + BLR8 implicit $lr8, implicit $rm + +... + +# Original TR (and 2013-07-01-PHIElimBug.ll) was about verifier errors for bb.3. +# +# I got a feeling that we also need to have some checks to see that # the code +# is correct in some way. Hopefully this test case is stable enough to verify +# the full MIR like this. +# +# CHECK: bb.0: +# CHECK: %0:g8rc_and_g8rc_nox0 = ADDIStocHA $x2, @g_51 +# CHECK-NEXT: %1:g8rc_and_g8rc_nox0 = LDtocL @g_51, killed %0, implicit killed $x2 :: (load 8) +# CHECK-NEXT: %2:gprc = LI 0 +# CHECK-NEXT: %3:crrc = CMPLWI killed %2, 0 +# CHECK-NEXT: BCC 76, killed %3, %bb.2 +# CHECK: bb.1: +# CHECK: %4:g8rc = ADDI8 killed %1, 24 +# CHECK-NEXT: %16:g8rc_and_g8rc_nox0 = COPY killed %4 +# CHECK-NEXT: B %bb.4 +# CHECK: bb.2: +# CHECK: %5:g8rc = ADDI8 killed %1, 24 +# CHECK-NEXT: %6:g8rc = LI8 0 +# CHECK-NEXT: %7:crrc = CMPLWI undef %8:gprc, 20 +# CHECK-NEXT: %14:g8rc = COPY killed %5 +# CHECK-NEXT: %15:g8rc = COPY killed %6 +# CHECK: bb.3: +# CHECK: %10:g8rc = COPY killed %15 +# CHECK-NEXT: %9:g8rc = COPY killed %14 +# CHECK-NEXT: %14:g8rc = COPY killed %10 +# CHECK-NEXT: %15:g8rc = IMPLICIT_DEF +# CHECK-NEXT: %16:g8rc_and_g8rc_nox0 = COPY killed %9 +# CHECK-NEXT: BCC 68, %7, %bb.3 +# CHECK-NEXT: B %bb.4 +# CHECK: bb.4: +# CHECK: %12:g8rc_and_g8rc_nox0 = COPY killed %16 +# CHECK-NEXT: %13:g8rc = LI8 0 +# CHECK-NEXT: STW8 killed %13, 0, killed %12 :: (store 4) +# CHECK-NEXT: BLR8 implicit $lr8, implicit $rm