Skip to content

Commit

Permalink
[RegisterCoalescer] Add wrapper for Erasing Instructions
Browse files Browse the repository at this point in the history
Summary:
      To delete an instruction the coalescer needs to call eraseFromParent()
      on the MachineInstr, insert it in the ErasedInstrs list and update the
      Live Ranges structure. This patch re-factors the code to do all that in
      one function. This will also fix cases where previous code wasn't
      inserting deleted instructions in the ErasedList.

Reviewers: qcolombet, kparzysz

Reviewed By: qcolombet

Subscribers: MatzeB, llvm-commits, qcolombet

Differential Revision: https://reviews.llvm.org/D36204

llvm-svn: 309915
  • Loading branch information
Sameer AbuAsal committed Aug 3, 2017
1 parent 993bbbf commit c8befe6
Showing 1 changed file with 16 additions and 14 deletions.
30 changes: 16 additions & 14 deletions llvm/lib/CodeGen/RegisterCoalescer.cpp
Expand Up @@ -248,6 +248,16 @@ namespace {
}
}

/// Wrapper Method to do all the necessary work when an Instruction is
/// deleted.
/// Optimizations should use this to make sure that deleted instructions
/// are always accounted for.
void deleteInstr(MachineInstr* MI) {
ErasedInstrs.insert(MI);
LIS->RemoveMachineInstrFromMaps(*MI);
MI->eraseFromParent();
}

public:
static char ID; ///< Class identification, replacement for typeinfo
RegisterCoalescer() : MachineFunctionPass(ID) {
Expand Down Expand Up @@ -797,9 +807,7 @@ bool RegisterCoalescer::removeCopyByCommutingDef(const CoalescerPair &CP,
S.MergeValueNumberInto(SubDVNI, SubBValNo);
}

ErasedInstrs.insert(UseMI);
LIS->RemoveMachineInstrFromMaps(*UseMI);
UseMI->eraseFromParent();
deleteInstr(UseMI);
}

// Extend BValNo by merging in IntA live segments of AValNo. Val# definition
Expand Down Expand Up @@ -993,10 +1001,8 @@ bool RegisterCoalescer::removePartialRedundancy(const CoalescerPair &CP,
// Note: This is fine to remove the copy before updating the live-ranges.
// While updating the live-ranges, we only look at slot indices and
// never go back to the instruction.
LIS->RemoveMachineInstrFromMaps(CopyMI);
// Mark instructions as deleted.
ErasedInstrs.insert(&CopyMI);
CopyMI.eraseFromParent();
deleteInstr(&CopyMI);

// Update the liveness.
SmallVector<SlotIndex, 8> EndPoints;
Expand Down Expand Up @@ -1578,8 +1584,7 @@ bool RegisterCoalescer::joinCopy(MachineInstr *CopyMI, bool &Again) {

// Eliminate undefs.
if (!CP.isPhys() && eliminateUndefCopy(CopyMI)) {
LIS->RemoveMachineInstrFromMaps(*CopyMI);
CopyMI->eraseFromParent();
deleteInstr(CopyMI);
return false; // Not coalescable.
}

Expand Down Expand Up @@ -1607,8 +1612,7 @@ bool RegisterCoalescer::joinCopy(MachineInstr *CopyMI, bool &Again) {
}
DEBUG(dbgs() << "\tMerged values: " << LI << '\n');
}
LIS->RemoveMachineInstrFromMaps(*CopyMI);
CopyMI->eraseFromParent();
deleteInstr(CopyMI);
return true;
}

Expand Down Expand Up @@ -1668,8 +1672,7 @@ bool RegisterCoalescer::joinCopy(MachineInstr *CopyMI, bool &Again) {
if (!CP.isPartial() && !CP.isPhys()) {
if (adjustCopiesBackFrom(CP, CopyMI) ||
removeCopyByCommutingDef(CP, CopyMI)) {
LIS->RemoveMachineInstrFromMaps(*CopyMI);
CopyMI->eraseFromParent();
deleteInstr(CopyMI);
DEBUG(dbgs() << "\tTrivial!\n");
return true;
}
Expand Down Expand Up @@ -1855,8 +1858,7 @@ bool RegisterCoalescer::joinReservedPhysReg(CoalescerPair &CP) {
}
}

LIS->RemoveMachineInstrFromMaps(*CopyMI);
CopyMI->eraseFromParent();
deleteInstr(CopyMI);

// We don't track kills for reserved registers.
MRI->clearKillFlags(CP.getSrcReg());
Expand Down

0 comments on commit c8befe6

Please sign in to comment.