Skip to content

Commit 9d575a5

Browse files
committed
Simplify copy hint register collection in
CalcSpillWeights.cpp. NFC.
1 parent c4e135e commit 9d575a5

File tree

1 file changed

+24
-43
lines changed

1 file changed

+24
-43
lines changed

llvm/lib/CodeGen/CalcSpillWeights.cpp

Lines changed: 24 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -207,24 +207,8 @@ float VirtRegAuxInfo::weightCalcHelper(LiveInterval &LI, SlotIndex *Start,
207207
NumInstr += 2;
208208
}
209209

210-
// CopyHint is a sortable hint derived from a COPY instruction.
211-
struct CopyHint {
212-
const Register Reg;
213-
const float Weight;
214-
CopyHint(Register R, float W) : Reg(R), Weight(W) {}
215-
bool operator<(const CopyHint &Rhs) const {
216-
// Always prefer any physreg hint.
217-
if (Reg.isPhysical() != Rhs.Reg.isPhysical())
218-
return Reg.isPhysical();
219-
if (Weight != Rhs.Weight)
220-
return (Weight > Rhs.Weight);
221-
return Reg.id() < Rhs.Reg.id(); // Tie-breaker.
222-
}
223-
};
224-
225210
bool IsExiting = false;
226-
std::set<CopyHint> CopyHints;
227-
SmallDenseMap<unsigned, float, 8> Hint;
211+
SmallDenseMap<Register, float, 8> Hint;
228212
for (MachineRegisterInfo::reg_instr_nodbg_iterator
229213
I = MRI.reg_instr_nodbg_begin(LI.reg()),
230214
E = MRI.reg_instr_nodbg_end();
@@ -238,16 +222,8 @@ float VirtRegAuxInfo::weightCalcHelper(LiveInterval &LI, SlotIndex *Start,
238222
continue;
239223

240224
NumInstr++;
241-
bool identityCopy = false;
242-
auto DestSrc = TII.isCopyInstr(*MI);
243-
if (DestSrc) {
244-
const MachineOperand *DestRegOp = DestSrc->Destination;
245-
const MachineOperand *SrcRegOp = DestSrc->Source;
246-
identityCopy = DestRegOp->getReg() == SrcRegOp->getReg() &&
247-
DestRegOp->getSubReg() == SrcRegOp->getSubReg();
248-
}
249225

250-
if (identityCopy || MI->isImplicitDef())
226+
if (MI->isIdentityCopy() || MI->isImplicitDef())
251227
continue;
252228
if (!Visited.insert(MI).second)
253229
continue;
@@ -260,8 +236,7 @@ float VirtRegAuxInfo::weightCalcHelper(LiveInterval &LI, SlotIndex *Start,
260236
return -1.0f;
261237
}
262238

263-
// Force Weight onto the stack so that x86 doesn't add hidden precision,
264-
// similar to HWeight below.
239+
// Force Weight onto the stack so that x86 doesn't add hidden precision.
265240
stack_float_t Weight = 1.0f;
266241
if (IsSpillable) {
267242
// Get loop info for mi.
@@ -287,29 +262,35 @@ float VirtRegAuxInfo::weightCalcHelper(LiveInterval &LI, SlotIndex *Start,
287262
if (!TII.isCopyInstr(*MI))
288263
continue;
289264
Register HintReg = copyHint(MI, LI.reg(), TRI, MRI);
290-
if (!HintReg)
291-
continue;
292-
// Force HWeight onto the stack so that x86 doesn't add hidden precision,
293-
// making the comparison incorrectly pass (i.e., 1 > 1 == true??).
294-
stack_float_t HWeight = Hint[HintReg] += Weight;
295-
if (HintReg.isVirtual() || MRI.isAllocatable(HintReg))
296-
CopyHints.insert(CopyHint(HintReg, HWeight));
265+
if (HintReg && (HintReg.isVirtual() || MRI.isAllocatable(HintReg)))
266+
Hint[HintReg] += Weight;
297267
}
298268

299269
// Pass all the sorted copy hints to mri.
300-
if (ShouldUpdateLI && CopyHints.size()) {
270+
if (ShouldUpdateLI && Hint.size()) {
301271
// Remove a generic hint if previously added by target.
302272
if (TargetHint.first == 0 && TargetHint.second)
303273
MRI.clearSimpleHint(LI.reg());
304274

305-
SmallSet<Register, 4> HintedRegs;
306-
for (const auto &Hint : CopyHints) {
307-
if (!HintedRegs.insert(Hint.Reg).second ||
308-
(TargetHint.first != 0 && Hint.Reg == TargetHint.second))
309-
// Don't add the same reg twice or the target-type hint again.
310-
continue;
311-
MRI.addRegAllocationHint(LI.reg(), Hint.Reg);
275+
// Don't add the same reg twice or the target-type hint again.
276+
Register SkipReg = TargetHint.first != 0 ? TargetHint.second : Register();
277+
SmallVector<std::pair<float, Register>, 4> FRegHints, VRegHints;
278+
for (const auto &[Reg, Weight] : Hint) {
279+
if (Reg != SkipReg)
280+
(Reg.isPhysical() ? &FRegHints : &VRegHints)
281+
->emplace_back(Weight, Reg);
312282
}
283+
auto HeavyFirst = [](const auto &LHS, const auto &RHS) {
284+
if (LHS.first != RHS.first)
285+
return LHS.first > RHS.first;
286+
return LHS.second.id() < RHS.second.id();
287+
};
288+
sort(FRegHints, HeavyFirst);
289+
sort(VRegHints, HeavyFirst);
290+
for (const auto &[Weight, Reg] : FRegHints) // Prefer physregs hints first.
291+
MRI.addRegAllocationHint(LI.reg(), Reg);
292+
for (const auto &[Weight, Reg] : VRegHints)
293+
MRI.addRegAllocationHint(LI.reg(), Reg);
313294

314295
// Weakly boost the spill weight of hinted registers.
315296
TotalWeight *= 1.01F;

0 commit comments

Comments
 (0)