CalcSpillWeights.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. //===------------------------ CalcSpillWeights.cpp ------------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #include "llvm/CodeGen/CalcSpillWeights.h"
  10. #include "llvm/CodeGen/LiveIntervalAnalysis.h"
  11. #include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
  12. #include "llvm/CodeGen/MachineFunction.h"
  13. #include "llvm/CodeGen/MachineLoopInfo.h"
  14. #include "llvm/CodeGen/MachineRegisterInfo.h"
  15. #include "llvm/Support/Debug.h"
  16. #include "llvm/Support/raw_ostream.h"
  17. #include "llvm/Target/TargetInstrInfo.h"
  18. #include "llvm/Target/TargetRegisterInfo.h"
  19. #include "llvm/Target/TargetSubtargetInfo.h"
  20. using namespace llvm;
  21. #define DEBUG_TYPE "calcspillweights"
  22. void llvm::calculateSpillWeightsAndHints(LiveIntervals &LIS,
  23. MachineFunction &MF,
  24. const MachineLoopInfo &MLI,
  25. const MachineBlockFrequencyInfo &MBFI,
  26. VirtRegAuxInfo::NormalizingFn norm) {
  27. DEBUG(dbgs() << "********** Compute Spill Weights **********\n"
  28. << "********** Function: " << MF.getName() << '\n');
  29. MachineRegisterInfo &MRI = MF.getRegInfo();
  30. VirtRegAuxInfo VRAI(MF, LIS, MLI, MBFI, norm);
  31. for (unsigned i = 0, e = MRI.getNumVirtRegs(); i != e; ++i) {
  32. unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
  33. if (MRI.reg_nodbg_empty(Reg))
  34. continue;
  35. VRAI.calculateSpillWeightAndHint(LIS.getInterval(Reg));
  36. }
  37. }
  38. // Return the preferred allocation register for reg, given a COPY instruction.
  39. static unsigned copyHint(const MachineInstr *mi, unsigned reg,
  40. const TargetRegisterInfo &tri,
  41. const MachineRegisterInfo &mri) {
  42. unsigned sub, hreg, hsub;
  43. if (mi->getOperand(0).getReg() == reg) {
  44. sub = mi->getOperand(0).getSubReg();
  45. hreg = mi->getOperand(1).getReg();
  46. hsub = mi->getOperand(1).getSubReg();
  47. } else {
  48. sub = mi->getOperand(1).getSubReg();
  49. hreg = mi->getOperand(0).getReg();
  50. hsub = mi->getOperand(0).getSubReg();
  51. }
  52. if (!hreg)
  53. return 0;
  54. if (TargetRegisterInfo::isVirtualRegister(hreg))
  55. return sub == hsub ? hreg : 0;
  56. const TargetRegisterClass *rc = mri.getRegClass(reg);
  57. // Only allow physreg hints in rc.
  58. if (sub == 0)
  59. return rc->contains(hreg) ? hreg : 0;
  60. // reg:sub should match the physreg hreg.
  61. return tri.getMatchingSuperReg(hreg, sub, rc);
  62. }
  63. // Check if all values in LI are rematerializable
  64. static bool isRematerializable(const LiveInterval &LI,
  65. const LiveIntervals &LIS,
  66. const TargetInstrInfo &TII) {
  67. for (LiveInterval::const_vni_iterator I = LI.vni_begin(), E = LI.vni_end();
  68. I != E; ++I) {
  69. const VNInfo *VNI = *I;
  70. if (VNI->isUnused())
  71. continue;
  72. if (VNI->isPHIDef())
  73. return false;
  74. MachineInstr *MI = LIS.getInstructionFromIndex(VNI->def);
  75. assert(MI && "Dead valno in interval");
  76. if (!TII.isTriviallyReMaterializable(MI, LIS.getAliasAnalysis()))
  77. return false;
  78. }
  79. return true;
  80. }
  81. void
  82. VirtRegAuxInfo::calculateSpillWeightAndHint(LiveInterval &li) {
  83. MachineRegisterInfo &mri = MF.getRegInfo();
  84. const TargetRegisterInfo &tri = *MF.getSubtarget().getRegisterInfo();
  85. MachineBasicBlock *mbb = nullptr;
  86. MachineLoop *loop = nullptr;
  87. bool isExiting = false;
  88. float totalWeight = 0;
  89. unsigned numInstr = 0; // Number of instructions using li
  90. SmallPtrSet<MachineInstr*, 8> visited;
  91. // Find the best physreg hint and the best virtreg hint.
  92. float bestPhys = 0, bestVirt = 0;
  93. unsigned hintPhys = 0, hintVirt = 0;
  94. // Don't recompute a target specific hint.
  95. bool noHint = mri.getRegAllocationHint(li.reg).first != 0;
  96. // Don't recompute spill weight for an unspillable register.
  97. bool Spillable = li.isSpillable();
  98. for (MachineRegisterInfo::reg_instr_iterator
  99. I = mri.reg_instr_begin(li.reg), E = mri.reg_instr_end();
  100. I != E; ) {
  101. MachineInstr *mi = &*(I++);
  102. numInstr++;
  103. if (mi->isIdentityCopy() || mi->isImplicitDef() || mi->isDebugValue())
  104. continue;
  105. if (!visited.insert(mi).second)
  106. continue;
  107. float weight = 1.0f;
  108. if (Spillable) {
  109. // Get loop info for mi.
  110. if (mi->getParent() != mbb) {
  111. mbb = mi->getParent();
  112. loop = Loops.getLoopFor(mbb);
  113. isExiting = loop ? loop->isLoopExiting(mbb) : false;
  114. }
  115. // Calculate instr weight.
  116. bool reads, writes;
  117. std::tie(reads, writes) = mi->readsWritesVirtualRegister(li.reg);
  118. weight = LiveIntervals::getSpillWeight(
  119. writes, reads, &MBFI, mi);
  120. // Give extra weight to what looks like a loop induction variable update.
  121. if (writes && isExiting && LIS.isLiveOutOfMBB(li, mbb))
  122. weight *= 3;
  123. totalWeight += weight;
  124. }
  125. // Get allocation hints from copies.
  126. if (noHint || !mi->isCopy())
  127. continue;
  128. unsigned hint = copyHint(mi, li.reg, tri, mri);
  129. if (!hint)
  130. continue;
  131. // Force hweight onto the stack so that x86 doesn't add hidden precision,
  132. // making the comparison incorrectly pass (i.e., 1 > 1 == true??).
  133. //
  134. // FIXME: we probably shouldn't use floats at all.
  135. volatile float hweight = Hint[hint] += weight;
  136. if (TargetRegisterInfo::isPhysicalRegister(hint)) {
  137. if (hweight > bestPhys && mri.isAllocatable(hint))
  138. bestPhys = hweight, hintPhys = hint;
  139. } else {
  140. if (hweight > bestVirt)
  141. bestVirt = hweight, hintVirt = hint;
  142. }
  143. }
  144. Hint.clear();
  145. // Always prefer the physreg hint.
  146. if (unsigned hint = hintPhys ? hintPhys : hintVirt) {
  147. mri.setRegAllocationHint(li.reg, 0, hint);
  148. // Weakly boost the spill weight of hinted registers.
  149. totalWeight *= 1.01F;
  150. }
  151. // If the live interval was already unspillable, leave it that way.
  152. if (!Spillable)
  153. return;
  154. // Mark li as unspillable if all live ranges are tiny.
  155. if (li.isZeroLength(LIS.getSlotIndexes())) {
  156. li.markNotSpillable();
  157. return;
  158. }
  159. // If all of the definitions of the interval are re-materializable,
  160. // it is a preferred candidate for spilling.
  161. // FIXME: this gets much more complicated once we support non-trivial
  162. // re-materialization.
  163. if (isRematerializable(li, LIS, *MF.getSubtarget().getInstrInfo()))
  164. totalWeight *= 0.5F;
  165. li.weight = normalize(totalWeight, li.getSize(), numInstr);
  166. }