Index: llvm/lib/CodeGen/DeadMachineInstructionElim.cpp =================================================================== --- llvm/lib/CodeGen/DeadMachineInstructionElim.cpp +++ llvm/lib/CodeGen/DeadMachineInstructionElim.cpp @@ -12,6 +12,7 @@ #include "llvm/ADT/PostOrderIterator.h" #include "llvm/ADT/Statistic.h" +#include "llvm/CodeGen/LiveRegUnits.h" #include "llvm/CodeGen/MachineFunctionPass.h" #include "llvm/CodeGen/MachineRegisterInfo.h" #include "llvm/CodeGen/TargetSubtargetInfo.h" @@ -30,10 +31,9 @@ class DeadMachineInstructionElim : public MachineFunctionPass { bool runOnMachineFunction(MachineFunction &MF) override; - const TargetRegisterInfo *TRI; const MachineRegisterInfo *MRI; const TargetInstrInfo *TII; - BitVector LivePhysRegs; + LiveRegUnits LivePhysRegs; public: static char ID; // Pass identification, replacement for typeid @@ -78,9 +78,9 @@ for (const MachineOperand &MO : MI->operands()) { if (MO.isReg() && MO.isDef()) { Register Reg = MO.getReg(); - if (Register::isPhysicalRegister(Reg)) { + if (Reg.isPhysical()) { // Don't delete live physreg defs, or any reserved register defs. - if (LivePhysRegs.test(Reg) || MRI->isReserved(Reg)) + if (!LivePhysRegs.available(Reg) || MRI->isReserved(Reg)) return false; } else { if (MO.isDead()) { @@ -117,26 +117,19 @@ bool DeadMachineInstructionElim::eliminateDeadMI(MachineFunction &MF) { bool AnyChanges = false; MRI = &MF.getRegInfo(); - TRI = MF.getSubtarget().getRegisterInfo(); TII = MF.getSubtarget().getInstrInfo(); + LivePhysRegs.init(*MF.getSubtarget().getRegisterInfo()); + // Loop over all instructions in all blocks, from bottom to top, so that it's // more likely that chains of dependent but ultimately dead instructions will // be cleaned up. for (MachineBasicBlock *MBB : post_order(&MF)) { - // Start out assuming that reserved registers are live out of this block. - LivePhysRegs = MRI->getReservedRegs(); - - // Add live-ins from successors to LivePhysRegs. Normally, physregs are not - // live across blocks, but some targets (x86) can have flags live out of a - // block. - for (const MachineBasicBlock *Succ : MBB->successors()) - for (const auto &LI : Succ->liveins()) - LivePhysRegs.set(LI.PhysReg); + LivePhysRegs.addLiveOuts(*MBB); // Now scan the instructions and delete dead ones, tracking physreg // liveness as we go. - for (MachineInstr &MI : llvm::make_early_inc_range(llvm::reverse(*MBB))) { + for (MachineInstr &MI : make_early_inc_range(reverse(*MBB))) { // If the instruction is dead, delete it! if (isDead(&MI)) { LLVM_DEBUG(dbgs() << "DeadMachineInstructionElim: DELETING: " << MI); @@ -149,34 +142,7 @@ continue; } - // Record the physreg defs. - for (const MachineOperand &MO : MI.operands()) { - if (MO.isReg() && MO.isDef()) { - Register Reg = MO.getReg(); - if (Register::isPhysicalRegister(Reg)) { - // Check the subreg set, not the alias set, because a def - // of a super-register may still be partially live after - // this def. - for (MCSubRegIterator SR(Reg, TRI,/*IncludeSelf=*/true); - SR.isValid(); ++SR) - LivePhysRegs.reset(*SR); - } - } else if (MO.isRegMask()) { - // Register mask of preserved registers. All clobbers are dead. - LivePhysRegs.clearBitsNotInMask(MO.getRegMask()); - } - } - // Record the physreg uses, after the defs, in case a physreg is - // both defined and used in the same instruction. - for (const MachineOperand &MO : MI.operands()) { - if (MO.isReg() && MO.isUse()) { - Register Reg = MO.getReg(); - if (Register::isPhysicalRegister(Reg)) { - for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) - LivePhysRegs.set(*AI); - } - } - } + LivePhysRegs.stepBackward(MI); } } Index: llvm/test/CodeGen/AMDGPU/fold-immediate-operand-shrink.mir =================================================================== --- llvm/test/CodeGen/AMDGPU/fold-immediate-operand-shrink.mir +++ llvm/test/CodeGen/AMDGPU/fold-immediate-operand-shrink.mir @@ -160,6 +160,7 @@ ; GCN: bb.0: ; GCN-NEXT: successors: %bb.1(0x80000000) ; GCN-NEXT: {{ $}} + ; GCN-NEXT: $vcc = S_MOV_B64 -1 ; GCN-NEXT: [[S_MOV_B32_:%[0-9]+]]:sreg_32_xm0 = S_MOV_B32 12345 ; GCN-NEXT: [[DEF:%[0-9]+]]:vgpr_32 = IMPLICIT_DEF ; GCN-NEXT: [[V_ADD_CO_U32_e64_:%[0-9]+]]:vgpr_32, [[V_ADD_CO_U32_e64_1:%[0-9]+]]:sreg_64 = V_ADD_CO_U32_e64 [[S_MOV_B32_]], [[DEF]], 0, implicit $exec