Index: lib/CodeGen/UnreachableBlockElim.cpp =================================================================== --- lib/CodeGen/UnreachableBlockElim.cpp +++ lib/CodeGen/UnreachableBlockElim.cpp @@ -207,11 +207,12 @@ MachineRegisterInfo &MRI = F.getRegInfo(); unsigned InputSub = Input.getSubReg(); if (InputSub == 0 && - MRI.constrainRegClass(InputReg, MRI.getRegClass(OutputReg))) { + MRI.constrainRegClass(InputReg, MRI.getRegClass(OutputReg)) && + !Input.isUndef()) { MRI.replaceRegWith(OutputReg, InputReg); } else { // The input register to the PHI has a subregister or it can't be - // constrained to the proper register class: + // constrained to the proper register class or it is undef: // insert a COPY instead of simply replacing the output // with the input. const TargetInstrInfo *TII = F.getSubtarget().getInstrInfo(); Index: test/CodeGen/X86/unreachable-mbb-undef-phi.ll =================================================================== --- /dev/null +++ test/CodeGen/X86/unreachable-mbb-undef-phi.ll @@ -0,0 +1,33 @@ +; RUN: llc -optimize-regalloc -O0 %s -o - -stop-after=unreachable-mbb-elimination | FileCheck %s + +define void @f() { +bb0: + br label %bb1 + +bb1: ; preds = %bb2, %bb0 + %inc1 = phi i32 [ %inc, %bb2 ], [ undef, %bb0 ] + %inc = add nsw i32 %inc1, 1 + %tobool = icmp eq i32 0, 0 + br i1 %tobool, label %bb3, label %bb2 + +bb2: ; preds = %bb1 + br label %bb1 + +bb3: ; preds = %bb1 + ret void +} + +; bb2 is dead and should be removed and the add input should be marked with +; undef since the bb0 value in the PHI is undef. Shouldn't crash. + +; CHECK: bb.0.bb0: +; CHECK: successors: %bb.1.bb1 +; CHECK: JMP_1 %bb.1.bb1 + +; CHECK: bb.1.bb1: +; CHECK: successors: %bb.2.bb3 +; CHECK: dead %eax = ADD32ri8 undef %eax, 1, implicit-def dead %eflags +; CHECK: JMP_1 %bb.2.bb3 + +; CHECK: bb.2.bb3: +; CHECK: RETQ