Skip to content

Commit

Permalink
[DCE] Ensure reserved simplifiable registers are live across call bou…
Browse files Browse the repository at this point in the history
…ndaries.
  • Loading branch information
SagarMaheshwari99 committed Nov 25, 2024
1 parent 787d7e5 commit d7831da
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 8 deletions.
29 changes: 25 additions & 4 deletions llvm/lib/CodeGen/DeadMachineInstructionElim.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,24 @@ bool DeadMachineInstructionElimImpl::runImpl(MachineFunction &MF,
return AnyChanges;
}

SmallVector<MCPhysReg, 16>
getSimplifiableReservedRegs(const MachineRegisterInfo *MRI) {
BitVector ReservedRegs = MRI->getReservedRegs();
SmallVector<MCPhysReg, 16> SimplifiableReservedRegs;
for (MCPhysReg PhysReg : ReservedRegs.set_bits()) {
if (MRI->canSimplifyPhysReg(PhysReg)) {
SimplifiableReservedRegs.push_back(PhysReg);
}
}
return SimplifiableReservedRegs;
}

bool DeadMachineInstructionElimImpl::eliminateDeadMI(MachineFunction &MF) {
bool AnyChanges = false;

SmallVector<MCPhysReg, 16> SimplifiableReservedRegs =
getSimplifiableReservedRegs(MRI);

// 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.
Expand All @@ -165,10 +180,8 @@ bool DeadMachineInstructionElimImpl::eliminateDeadMI(MachineFunction &MF) {

// Reserved registers are considered always live, so consider them as
// live-outs for MBB. Inside MBB, dead assignments can still be detected.
for (MCPhysReg PhysReg : MRI->getReservedRegs().set_bits()) {
if (MRI->canSimplifyPhysReg(PhysReg)) {
LivePhysRegs.addReg(PhysReg);
}
for (MCPhysReg PhysReg : SimplifiableReservedRegs) {
LivePhysRegs.addReg(PhysReg);
}

// Now scan the instructions and delete dead ones, tracking physreg
Expand All @@ -187,6 +200,14 @@ bool DeadMachineInstructionElimImpl::eliminateDeadMI(MachineFunction &MF) {
}

LivePhysRegs.stepBackward(MI);

// If the instruction is a call, conservatively assume that it reads
// reserved registers.
if (MI.isCall()) {
for (MCPhysReg PhysReg : SimplifiableReservedRegs) {
LivePhysRegs.addReg(PhysReg);
}
}
}
}

Expand Down
3 changes: 3 additions & 0 deletions llvm/test/CodeGen/AIE/aie2/GlobalISel/dead-mi-elim.mir
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,9 @@ body: |
; CHECK-LABEL: name: live_control_regs_call
; CHECK: liveins: $r1, $p0
; CHECK-NEXT: {{ $}}
; CHECK-NEXT: [[MOV_RLC_imm10_pseudo:%[0-9]+]]:er = MOV_RLC_imm10_pseudo 0
; CHECK-NEXT: $crsat = COPY [[MOV_RLC_imm10_pseudo]]
; CHECK-NEXT: $crrnd = COPY [[MOV_RLC_imm10_pseudo]]
; CHECK-NEXT: PseudoJL 0, csr_aie2
%0:er = MOV_RLC_imm10_pseudo 0
$crsat = COPY %0:er
Expand Down
8 changes: 4 additions & 4 deletions llvm/test/CodeGen/AIE/aie2/live-reserved-regs-call.ll
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ define void @caller1() {
; CHECK-LABEL: caller1:
; CHECK: .p2align 4
; CHECK-NEXT: // %bb.0: // %entry
; CHECK-NEXT: nopb ; nopa ; nops ; jl #callee1; nopv
; CHECK-NEXT: nopx // Delay Slot 5
; CHECK-NEXT: nopa ; nopb ; jl #callee1; nops
; CHECK-NEXT: nop // Delay Slot 5
; CHECK-NEXT: paddb [sp], #32 // Delay Slot 4
; CHECK-NEXT: st lr, [sp, #-32] // 4-byte Folded Spill Delay Slot 3
; CHECK-NEXT: nop // Delay Slot 2
; CHECK-NEXT: nop // Delay Slot 1
; CHECK-NEXT: mov crSat, #1 // Delay Slot 2
; CHECK-NEXT: mov crRnd, #12 // Delay Slot 1
; CHECK-NEXT: lda lr, [sp, #-32] // 4-byte Folded Reload
; CHECK-NEXT: nop
; CHECK-NEXT: nop
Expand Down

0 comments on commit d7831da

Please sign in to comment.