Refactor UpdatePredRedefs and StepForward to avoid duplication. NFC

Note, this is a reapplication of r236515 with a fix to not assert on non-register operands, but instead only handle them until the subsequent commit.  Original commit message follows.

The code was basically the same here already.  Just added an out parameter for a vector of seen defs so that UpdatePredRedefs can call StepForward first, then do its own post processing on the seen defs.

Will be used in the next commit to also handle regmasks.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@236538 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Pete Cooper 2015-05-05 20:14:22 +00:00
parent 08c531db5f
commit 2bce3aa5f1
3 changed files with 35 additions and 32 deletions

View File

@ -93,7 +93,8 @@ public:
} }
/// \brief Removes physical registers clobbered by the regmask operand @p MO. /// \brief Removes physical registers clobbered by the regmask operand @p MO.
void removeRegsInMask(const MachineOperand &MO); void removeRegsInMask(const MachineOperand &MO,
SmallVectorImpl<std::pair<unsigned, const MachineOperand*>> *Clobbers);
/// \brief Returns true if register @p Reg is contained in the set. This also /// \brief Returns true if register @p Reg is contained in the set. This also
/// works if only the super register of @p Reg has been defined, because we /// works if only the super register of @p Reg has been defined, because we
@ -109,7 +110,11 @@ public:
/// instruction(bundle): Remove killed-uses, add defs. This is the not /// instruction(bundle): Remove killed-uses, add defs. This is the not
/// recommended way, because it depends on accurate kill flags. If possible /// recommended way, because it depends on accurate kill flags. If possible
/// use stepBackwards() instead of this function. /// use stepBackwards() instead of this function.
void stepForward(const MachineInstr &MI); /// The clobbers set will be the list of registers either defined or clobbered
/// by a regmask. The operand will identify whether this is a regmask or
/// register operand.
void stepForward(const MachineInstr &MI,
SmallVectorImpl<std::pair<unsigned, const MachineOperand*>> &Clobbers);
/// \brief Adds all live-in registers of basic block @p MBB. /// \brief Adds all live-in registers of basic block @p MBB.
void addLiveIns(const MachineBasicBlock *MBB) { void addLiveIns(const MachineBasicBlock *MBB) {

View File

@ -975,26 +975,18 @@ void IfConverter::RemoveExtraEdges(BBInfo &BBI) {
/// Behaves like LiveRegUnits::StepForward() but also adds implicit uses to all /// Behaves like LiveRegUnits::StepForward() but also adds implicit uses to all
/// values defined in MI which are not live/used by MI. /// values defined in MI which are not live/used by MI.
static void UpdatePredRedefs(MachineInstr *MI, LivePhysRegs &Redefs) { static void UpdatePredRedefs(MachineInstr *MI, LivePhysRegs &Redefs) {
for (ConstMIBundleOperands Ops(MI); Ops.isValid(); ++Ops) { SmallVector<std::pair<unsigned, const MachineOperand*>, 4> Clobbers;
if (!Ops->isReg() || !Ops->isKill()) Redefs.stepForward(*MI, Clobbers);
continue;
unsigned Reg = Ops->getReg();
if (Reg == 0)
continue;
Redefs.removeReg(Reg);
}
for (MIBundleOperands Ops(MI); Ops.isValid(); ++Ops) {
if (!Ops->isReg() || !Ops->isDef())
continue;
unsigned Reg = Ops->getReg();
if (Reg == 0 || Redefs.contains(Reg))
continue;
Redefs.addReg(Reg);
MachineOperand &Op = *Ops; // Now add the implicit uses for each of the clobbered values.
MachineInstr *MI = Op.getParent(); for (auto Reg : Clobbers) {
MachineInstrBuilder MIB(*MI->getParent()->getParent(), MI); const MachineOperand &Op = *Reg.second;
MIB.addReg(Reg, RegState::Implicit | RegState::Undef); // FIXME: Const cast here is nasty, but better than making StepForward
// take a mutable instruction instead of const.
MachineInstr *OpMI = const_cast<MachineInstr*>(Op.getParent());
MachineInstrBuilder MIB(*OpMI->getParent()->getParent(), OpMI);
if (Op.isReg())
MIB.addReg(Reg.first, RegState::Implicit | RegState::Undef);
} }
} }
@ -1374,7 +1366,8 @@ bool IfConverter::IfConvertDiamond(BBInfo &BBI, IfcvtKind Kind,
for (MachineBasicBlock::const_iterator I = BBI1->BB->begin(), E = DI1; I != E; for (MachineBasicBlock::const_iterator I = BBI1->BB->begin(), E = DI1; I != E;
++I) { ++I) {
Redefs.stepForward(*I); SmallVector<std::pair<unsigned, const MachineOperand*>, 4> IgnoredClobbers;
Redefs.stepForward(*I, IgnoredClobbers);
} }
BBI.BB->splice(BBI.BB->end(), BBI1->BB, BBI1->BB->begin(), DI1); BBI.BB->splice(BBI.BB->end(), BBI1->BB, BBI1->BB->begin(), DI1);
BBI2->BB->erase(BBI2->BB->begin(), DI2); BBI2->BB->erase(BBI2->BB->begin(), DI2);

View File

@ -22,12 +22,17 @@ using namespace llvm;
/// \brief Remove all registers from the set that get clobbered by the register /// \brief Remove all registers from the set that get clobbered by the register
/// mask. /// mask.
void LivePhysRegs::removeRegsInMask(const MachineOperand &MO) { /// The clobbers set will be the list of live registers clobbered
/// by the regmask.
void LivePhysRegs::removeRegsInMask(const MachineOperand &MO,
SmallVectorImpl<std::pair<unsigned, const MachineOperand*>> *Clobbers) {
SparseSet<unsigned>::iterator LRI = LiveRegs.begin(); SparseSet<unsigned>::iterator LRI = LiveRegs.begin();
while (LRI != LiveRegs.end()) { while (LRI != LiveRegs.end()) {
if (MO.clobbersPhysReg(*LRI)) if (MO.clobbersPhysReg(*LRI)) {
if (Clobbers)
Clobbers->push_back(std::make_pair(*LRI, &MO));
LRI = LiveRegs.erase(LRI); LRI = LiveRegs.erase(LRI);
else } else
++LRI; ++LRI;
} }
} }
@ -45,7 +50,7 @@ void LivePhysRegs::stepBackward(const MachineInstr &MI) {
continue; continue;
removeReg(Reg); removeReg(Reg);
} else if (O->isRegMask()) } else if (O->isRegMask())
removeRegsInMask(*O); removeRegsInMask(*O, nullptr);
} }
// Add uses to the set. // Add uses to the set.
@ -63,8 +68,8 @@ void LivePhysRegs::stepBackward(const MachineInstr &MI) {
/// killed-uses, add defs. This is the not recommended way, because it depends /// killed-uses, add defs. This is the not recommended way, because it depends
/// on accurate kill flags. If possible use stepBackwards() instead of this /// on accurate kill flags. If possible use stepBackwards() instead of this
/// function. /// function.
void LivePhysRegs::stepForward(const MachineInstr &MI) { void LivePhysRegs::stepForward(const MachineInstr &MI,
SmallVector<unsigned, 4> Defs; SmallVectorImpl<std::pair<unsigned, const MachineOperand*>> &Clobbers) {
// Remove killed registers from the set. // Remove killed registers from the set.
for (ConstMIBundleOperands O(&MI); O.isValid(); ++O) { for (ConstMIBundleOperands O(&MI); O.isValid(); ++O) {
if (O->isReg()) { if (O->isReg()) {
@ -73,7 +78,7 @@ void LivePhysRegs::stepForward(const MachineInstr &MI) {
continue; continue;
if (O->isDef()) { if (O->isDef()) {
if (!O->isDead()) if (!O->isDead())
Defs.push_back(Reg); Clobbers.push_back(std::make_pair(Reg, &*O));
} else { } else {
if (!O->isKill()) if (!O->isKill())
continue; continue;
@ -81,12 +86,12 @@ void LivePhysRegs::stepForward(const MachineInstr &MI) {
removeReg(Reg); removeReg(Reg);
} }
} else if (O->isRegMask()) } else if (O->isRegMask())
removeRegsInMask(*O); removeRegsInMask(*O, &Clobbers);
} }
// Add defs to the set. // Add defs to the set.
for (unsigned i = 0, e = Defs.size(); i != e; ++i) for (auto Reg : Clobbers)
addReg(Defs[i]); addReg(Reg.first);
} }
/// Prin the currently live registers to OS. /// Prin the currently live registers to OS.