Skip to content

Commit 06e5544

Browse files
committed
[CalcSpillWeights] Simplify copy hint register collection. NFC.
1 parent c4e135e commit 06e5544

File tree

1 file changed

+22
-34
lines changed

1 file changed

+22
-34
lines changed

llvm/lib/CodeGen/CalcSpillWeights.cpp

Lines changed: 22 additions & 34 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();
@@ -260,8 +244,7 @@ float VirtRegAuxInfo::weightCalcHelper(LiveInterval &LI, SlotIndex *Start,
260244
return -1.0f;
261245
}
262246

263-
// Force Weight onto the stack so that x86 doesn't add hidden precision,
264-
// similar to HWeight below.
247+
// Force Weight onto the stack so that x86 doesn't add hidden precision.
265248
stack_float_t Weight = 1.0f;
266249
if (IsSpillable) {
267250
// Get loop info for mi.
@@ -287,29 +270,34 @@ float VirtRegAuxInfo::weightCalcHelper(LiveInterval &LI, SlotIndex *Start,
287270
if (!TII.isCopyInstr(*MI))
288271
continue;
289272
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));
273+
if (HintReg && (HintReg.isVirtual() || MRI.isAllocatable(HintReg)))
274+
Hint[HintReg] += Weight;
297275
}
298276

299277
// Pass all the sorted copy hints to mri.
300-
if (ShouldUpdateLI && CopyHints.size()) {
278+
if (ShouldUpdateLI && Hint.size()) {
301279
// Remove a generic hint if previously added by target.
302280
if (TargetHint.first == 0 && TargetHint.second)
303281
MRI.clearSimpleHint(LI.reg());
304282

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);
283+
// Don't add the target-type hint again.
284+
Register SkipReg = TargetHint.first != 0 ? TargetHint.second : Register();
285+
SmallVector<std::pair<float, Register>, 4> FRegHints, VRegHints;
286+
for (const auto &[Reg, Weight] : Hint) {
287+
if (Reg != SkipReg)
288+
(Reg.isPhysical() ? &FRegHints : &VRegHints)->emplace_back(Weight, Reg);
312289
}
290+
auto HeavyFirst = [](const auto &LHS, const auto &RHS) {
291+
if (LHS.first != RHS.first)
292+
return LHS.first > RHS.first;
293+
return LHS.second.id() < RHS.second.id();
294+
};
295+
sort(FRegHints, HeavyFirst);
296+
sort(VRegHints, HeavyFirst);
297+
for (const auto &[Weight, Reg] : FRegHints) // Prefer physregs hints first.
298+
MRI.addRegAllocationHint(LI.reg(), Reg);
299+
for (const auto &[Weight, Reg] : VRegHints)
300+
MRI.addRegAllocationHint(LI.reg(), Reg);
313301

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

0 commit comments

Comments
 (0)