diff --git a/llvm/lib/CodeGen/CalcSpillWeights.cpp b/llvm/lib/CodeGen/CalcSpillWeights.cpp index f361c956092e8..d1b22b60c0e88 100644 --- a/llvm/lib/CodeGen/CalcSpillWeights.cpp +++ b/llvm/lib/CodeGen/CalcSpillWeights.cpp @@ -209,8 +209,8 @@ float VirtRegAuxInfo::weightCalcHelper(LiveInterval &LI, SlotIndex *Start, // CopyHint is a sortable hint derived from a COPY instruction. struct CopyHint { - const Register Reg; - const float Weight; + Register Reg; + float Weight; CopyHint(Register R, float W) : Reg(R), Weight(W) {} bool operator<(const CopyHint &Rhs) const { // Always prefer any physreg hint. @@ -223,8 +223,7 @@ float VirtRegAuxInfo::weightCalcHelper(LiveInterval &LI, SlotIndex *Start, }; bool IsExiting = false; - std::set CopyHints; - SmallDenseMap Hint; + SmallDenseMap Hint; for (MachineRegisterInfo::reg_instr_nodbg_iterator I = MRI.reg_instr_nodbg_begin(LI.reg()), E = MRI.reg_instr_nodbg_end(); @@ -260,8 +259,7 @@ float VirtRegAuxInfo::weightCalcHelper(LiveInterval &LI, SlotIndex *Start, return -1.0f; } - // Force Weight onto the stack so that x86 doesn't add hidden precision, - // similar to HWeight below. + // Force Weight onto the stack so that x86 doesn't add hidden precision. stack_float_t Weight = 1.0f; if (IsSpillable) { // Get loop info for mi. @@ -287,29 +285,26 @@ float VirtRegAuxInfo::weightCalcHelper(LiveInterval &LI, SlotIndex *Start, if (!TII.isCopyInstr(*MI)) continue; Register HintReg = copyHint(MI, LI.reg(), TRI, MRI); - if (!HintReg) - continue; - // Force HWeight onto the stack so that x86 doesn't add hidden precision, - // making the comparison incorrectly pass (i.e., 1 > 1 == true??). - stack_float_t HWeight = Hint[HintReg] += Weight; - if (HintReg.isVirtual() || MRI.isAllocatable(HintReg)) - CopyHints.insert(CopyHint(HintReg, HWeight)); + if (HintReg && (HintReg.isVirtual() || MRI.isAllocatable(HintReg))) + Hint[HintReg] += Weight; } // Pass all the sorted copy hints to mri. - if (ShouldUpdateLI && CopyHints.size()) { + if (ShouldUpdateLI && Hint.size()) { // Remove a generic hint if previously added by target. if (TargetHint.first == 0 && TargetHint.second) MRI.clearSimpleHint(LI.reg()); - SmallSet HintedRegs; - for (const auto &Hint : CopyHints) { - if (!HintedRegs.insert(Hint.Reg).second || - (TargetHint.first != 0 && Hint.Reg == TargetHint.second)) - // Don't add the same reg twice or the target-type hint again. - continue; - MRI.addRegAllocationHint(LI.reg(), Hint.Reg); + // Don't add the target-type hint again. + Register SkipReg = TargetHint.first != 0 ? TargetHint.second : Register(); + SmallVector RegHints; + for (const auto &[Reg, Weight] : Hint) { + if (Reg != SkipReg) + RegHints.emplace_back(Reg, Weight); } + sort(RegHints); + for (const auto &[Reg, Weight] : RegHints) + MRI.addRegAllocationHint(LI.reg(), Reg); // Weakly boost the spill weight of hinted registers. TotalWeight *= 1.01F;