Support register masks in MachineLICM.

Only PostRA LICM is affected.

llvm-svn: 148589
This commit is contained in:
Jakob Stoklund Olesen 2012-01-20 22:27:12 +00:00
parent 58614f2f5a
commit 6b17ef58a1
1 changed files with 36 additions and 23 deletions

View File

@ -163,7 +163,9 @@ namespace {
/// ProcessMI - Examine the instruction for potentai LICM candidate. Also /// ProcessMI - Examine the instruction for potentai LICM candidate. Also
/// gather register def and frame object update information. /// gather register def and frame object update information.
void ProcessMI(MachineInstr *MI, unsigned *PhysRegDefs, void ProcessMI(MachineInstr *MI,
BitVector &PhysRegDefs,
BitVector &PhysRegClobbers,
SmallSet<int, 32> &StoredFIs, SmallSet<int, 32> &StoredFIs,
SmallVector<CandidateInfo, 32> &Candidates); SmallVector<CandidateInfo, 32> &Candidates);
@ -392,7 +394,8 @@ static bool InstructionStoresToFI(const MachineInstr *MI, int FI) {
/// ProcessMI - Examine the instruction for potentai LICM candidate. Also /// ProcessMI - Examine the instruction for potentai LICM candidate. Also
/// gather register def and frame object update information. /// gather register def and frame object update information.
void MachineLICM::ProcessMI(MachineInstr *MI, void MachineLICM::ProcessMI(MachineInstr *MI,
unsigned *PhysRegDefs, BitVector &PhysRegDefs,
BitVector &PhysRegClobbers,
SmallSet<int, 32> &StoredFIs, SmallSet<int, 32> &StoredFIs,
SmallVector<CandidateInfo, 32> &Candidates) { SmallVector<CandidateInfo, 32> &Candidates) {
bool RuledOut = false; bool RuledOut = false;
@ -411,6 +414,16 @@ void MachineLICM::ProcessMI(MachineInstr *MI,
continue; continue;
} }
// We can't hoist an instruction defining a physreg that is clobbered in
// the loop.
if (MO.isRegMask()) {
if (const uint32_t *Mask = MO.getRegMask())
PhysRegClobbers.setBitsNotInMask(Mask);
else
PhysRegClobbers.set();
continue;
}
if (!MO.isReg()) if (!MO.isReg())
continue; continue;
unsigned Reg = MO.getReg(); unsigned Reg = MO.getReg();
@ -420,7 +433,7 @@ void MachineLICM::ProcessMI(MachineInstr *MI,
"Not expecting virtual register!"); "Not expecting virtual register!");
if (!MO.isDef()) { if (!MO.isDef()) {
if (Reg && PhysRegDefs[Reg]) if (Reg && (PhysRegDefs.test(Reg) || PhysRegClobbers.test(Reg)))
// If it's using a non-loop-invariant register, then it's obviously not // If it's using a non-loop-invariant register, then it's obviously not
// safe to hoist. // safe to hoist.
HasNonInvariantUse = true; HasNonInvariantUse = true;
@ -428,9 +441,8 @@ void MachineLICM::ProcessMI(MachineInstr *MI,
} }
if (MO.isImplicit()) { if (MO.isImplicit()) {
++PhysRegDefs[Reg]; for (const unsigned *AS = TRI->getOverlaps(Reg); *AS; ++AS)
for (const unsigned *AS = TRI->getAliasSet(Reg); *AS; ++AS) PhysRegClobbers.set(*AS);
++PhysRegDefs[*AS];
if (!MO.isDead()) if (!MO.isDead())
// Non-dead implicit def? This cannot be hoisted. // Non-dead implicit def? This cannot be hoisted.
RuledOut = true; RuledOut = true;
@ -447,14 +459,17 @@ void MachineLICM::ProcessMI(MachineInstr *MI,
Def = Reg; Def = Reg;
// If we have already seen another instruction that defines the same // If we have already seen another instruction that defines the same
// register, then this is not safe. // register, then this is not safe. Two defs is indicated by setting a
if (++PhysRegDefs[Reg] > 1) // PhysRegClobbers bit.
// MI defined register is seen defined by another instruction in for (const unsigned *AS = TRI->getOverlaps(Reg); *AS; ++AS) {
// the loop, it cannot be a LICM candidate. if (PhysRegDefs.test(Reg))
RuledOut = true; PhysRegClobbers.set(Reg);
for (const unsigned *AS = TRI->getAliasSet(Reg); *AS; ++AS) if (PhysRegClobbers.test(Reg))
if (++PhysRegDefs[*AS] > 1) // MI defined register is seen defined by another instruction in
// the loop, it cannot be a LICM candidate.
RuledOut = true; RuledOut = true;
PhysRegDefs.set(Reg);
}
} }
// Only consider reloads for now and remats which do not have register // Only consider reloads for now and remats which do not have register
@ -471,8 +486,8 @@ void MachineLICM::ProcessMI(MachineInstr *MI,
/// invariants out to the preheader. /// invariants out to the preheader.
void MachineLICM::HoistRegionPostRA() { void MachineLICM::HoistRegionPostRA() {
unsigned NumRegs = TRI->getNumRegs(); unsigned NumRegs = TRI->getNumRegs();
unsigned *PhysRegDefs = new unsigned[NumRegs]; BitVector PhysRegDefs(NumRegs); // Regs defined once in the loop.
std::fill(PhysRegDefs, PhysRegDefs + NumRegs, 0); BitVector PhysRegClobbers(NumRegs); // Regs defined more than once.
SmallVector<CandidateInfo, 32> Candidates; SmallVector<CandidateInfo, 32> Candidates;
SmallSet<int, 32> StoredFIs; SmallSet<int, 32> StoredFIs;
@ -494,16 +509,15 @@ void MachineLICM::HoistRegionPostRA() {
for (MachineBasicBlock::livein_iterator I = BB->livein_begin(), for (MachineBasicBlock::livein_iterator I = BB->livein_begin(),
E = BB->livein_end(); I != E; ++I) { E = BB->livein_end(); I != E; ++I) {
unsigned Reg = *I; unsigned Reg = *I;
++PhysRegDefs[Reg]; for (const unsigned *AS = TRI->getOverlaps(Reg); *AS; ++AS)
for (const unsigned *AS = TRI->getAliasSet(Reg); *AS; ++AS) PhysRegDefs.set(*AS);
++PhysRegDefs[*AS];
} }
SpeculationState = SpeculateUnknown; SpeculationState = SpeculateUnknown;
for (MachineBasicBlock::iterator for (MachineBasicBlock::iterator
MII = BB->begin(), E = BB->end(); MII != E; ++MII) { MII = BB->begin(), E = BB->end(); MII != E; ++MII) {
MachineInstr *MI = &*MII; MachineInstr *MI = &*MII;
ProcessMI(MI, PhysRegDefs, StoredFIs, Candidates); ProcessMI(MI, PhysRegDefs, PhysRegClobbers, StoredFIs, Candidates);
} }
} }
@ -517,14 +531,15 @@ void MachineLICM::HoistRegionPostRA() {
StoredFIs.count(Candidates[i].FI)) StoredFIs.count(Candidates[i].FI))
continue; continue;
if (PhysRegDefs[Candidates[i].Def] == 1) { if (!PhysRegClobbers.test(Candidates[i].Def)) {
bool Safe = true; bool Safe = true;
MachineInstr *MI = Candidates[i].MI; MachineInstr *MI = Candidates[i].MI;
for (unsigned j = 0, ee = MI->getNumOperands(); j != ee; ++j) { for (unsigned j = 0, ee = MI->getNumOperands(); j != ee; ++j) {
const MachineOperand &MO = MI->getOperand(j); const MachineOperand &MO = MI->getOperand(j);
if (!MO.isReg() || MO.isDef() || !MO.getReg()) if (!MO.isReg() || MO.isDef() || !MO.getReg())
continue; continue;
if (PhysRegDefs[MO.getReg()]) { if (PhysRegDefs.test(MO.getReg()) ||
PhysRegClobbers.test(MO.getReg())) {
// If it's using a non-loop-invariant register, then it's obviously // If it's using a non-loop-invariant register, then it's obviously
// not safe to hoist. // not safe to hoist.
Safe = false; Safe = false;
@ -535,8 +550,6 @@ void MachineLICM::HoistRegionPostRA() {
HoistPostRA(MI, Candidates[i].Def); HoistPostRA(MI, Candidates[i].Def);
} }
} }
delete[] PhysRegDefs;
} }
/// AddToLiveIns - Add register 'Reg' to the livein sets of BBs in the current /// AddToLiveIns - Add register 'Reg' to the livein sets of BBs in the current