Index: lib/CodeGen/IfConversion.cpp =================================================================== --- lib/CodeGen/IfConversion.cpp +++ lib/CodeGen/IfConversion.cpp @@ -1048,6 +1048,17 @@ /// Behaves like LiveRegUnits::StepForward() but also adds implicit uses to all /// values defined in MI which are not live/used by MI. static void UpdatePredRedefs(MachineInstr &MI, LivePhysRegs &Redefs) { + const TargetRegisterInfo *TRI = MI.getParent()->getParent() + ->getSubtarget().getRegisterInfo(); + + // Before stepping forward past MI, remember which regs were live + // before MI. This is needed to set the Undef flag only when reg is + // dead. + SparseSet LiveBeforeMI; + LiveBeforeMI.setUniverse(TRI->getNumRegs()); + for (auto &Reg : Redefs) + LiveBeforeMI.insert(Reg); + SmallVector, 4> Clobbers; Redefs.stepForward(MI, Clobbers); @@ -1061,7 +1072,9 @@ if (Op.isRegMask()) { // First handle regmasks. They clobber any entries in the mask which // means that we need a def for those registers. - MIB.addReg(Reg.first, RegState::Implicit | RegState::Undef); + MIB.addReg(Reg.first, + (RegState::Implicit + | (!LiveBeforeMI.count(Reg.first) ? (RegState::Undef) : 0))); // We also need to add an implicit def of this register for the later // use to read from. @@ -1078,7 +1091,9 @@ if (Redefs.contains(Op.getReg())) Op.setIsDead(false); } - MIB.addReg(Reg.first, RegState::Implicit | RegState::Undef); + MIB.addReg(Reg.first, + (RegState::Implicit + | (!LiveBeforeMI.count(Reg.first) ? (RegState::Undef) : 0))); } }