Spiller.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. //===-- llvm/CodeGen/Spiller.cpp - Spiller -------------------------------===//
  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. #define DEBUG_TYPE "spiller"
  10. #include "Spiller.h"
  11. #include "VirtRegMap.h"
  12. #include "llvm/CodeGen/LiveIntervalAnalysis.h"
  13. #include "llvm/CodeGen/LiveStackAnalysis.h"
  14. #include "llvm/CodeGen/MachineFrameInfo.h"
  15. #include "llvm/CodeGen/MachineFunction.h"
  16. #include "llvm/CodeGen/MachineInstrBuilder.h"
  17. #include "llvm/CodeGen/MachineLoopInfo.h"
  18. #include "llvm/CodeGen/MachineRegisterInfo.h"
  19. #include "llvm/Target/TargetMachine.h"
  20. #include "llvm/Target/TargetInstrInfo.h"
  21. #include "llvm/Support/CommandLine.h"
  22. #include "llvm/Support/Debug.h"
  23. #include "llvm/Support/ErrorHandling.h"
  24. #include "llvm/Support/raw_ostream.h"
  25. #include <set>
  26. using namespace llvm;
  27. namespace {
  28. enum SpillerName { trivial, standard, inline_ };
  29. }
  30. static cl::opt<SpillerName>
  31. spillerOpt("spiller",
  32. cl::desc("Spiller to use: (default: standard)"),
  33. cl::Prefix,
  34. cl::values(clEnumVal(trivial, "trivial spiller"),
  35. clEnumVal(standard, "default spiller"),
  36. clEnumValN(inline_, "inline", "inline spiller"),
  37. clEnumValEnd),
  38. cl::init(standard));
  39. // Spiller virtual destructor implementation.
  40. Spiller::~Spiller() {}
  41. namespace {
  42. /// Utility class for spillers.
  43. class SpillerBase : public Spiller {
  44. protected:
  45. MachineFunctionPass *pass;
  46. MachineFunction *mf;
  47. VirtRegMap *vrm;
  48. LiveIntervals *lis;
  49. MachineFrameInfo *mfi;
  50. MachineRegisterInfo *mri;
  51. const TargetInstrInfo *tii;
  52. const TargetRegisterInfo *tri;
  53. /// Construct a spiller base.
  54. SpillerBase(MachineFunctionPass &pass, MachineFunction &mf, VirtRegMap &vrm)
  55. : pass(&pass), mf(&mf), vrm(&vrm)
  56. {
  57. lis = &pass.getAnalysis<LiveIntervals>();
  58. mfi = mf.getFrameInfo();
  59. mri = &mf.getRegInfo();
  60. tii = mf.getTarget().getInstrInfo();
  61. tri = mf.getTarget().getRegisterInfo();
  62. }
  63. /// Add spill ranges for every use/def of the live interval, inserting loads
  64. /// immediately before each use, and stores after each def. No folding or
  65. /// remat is attempted.
  66. void trivialSpillEverywhere(LiveInterval *li,
  67. SmallVectorImpl<LiveInterval*> &newIntervals) {
  68. DEBUG(dbgs() << "Spilling everywhere " << *li << "\n");
  69. assert(li->weight != HUGE_VALF &&
  70. "Attempting to spill already spilled value.");
  71. assert(!TargetRegisterInfo::isStackSlot(li->reg) &&
  72. "Trying to spill a stack slot.");
  73. DEBUG(dbgs() << "Trivial spill everywhere of reg" << li->reg << "\n");
  74. const TargetRegisterClass *trc = mri->getRegClass(li->reg);
  75. unsigned ss = vrm->assignVirt2StackSlot(li->reg);
  76. // Iterate over reg uses/defs.
  77. for (MachineRegisterInfo::reg_iterator
  78. regItr = mri->reg_begin(li->reg); regItr != mri->reg_end();) {
  79. // Grab the use/def instr.
  80. MachineInstr *mi = &*regItr;
  81. DEBUG(dbgs() << " Processing " << *mi);
  82. // Step regItr to the next use/def instr.
  83. do {
  84. ++regItr;
  85. } while (regItr != mri->reg_end() && (&*regItr == mi));
  86. // Collect uses & defs for this instr.
  87. SmallVector<unsigned, 2> indices;
  88. bool hasUse = false;
  89. bool hasDef = false;
  90. for (unsigned i = 0; i != mi->getNumOperands(); ++i) {
  91. MachineOperand &op = mi->getOperand(i);
  92. if (!op.isReg() || op.getReg() != li->reg)
  93. continue;
  94. hasUse |= mi->getOperand(i).isUse();
  95. hasDef |= mi->getOperand(i).isDef();
  96. indices.push_back(i);
  97. }
  98. // Create a new vreg & interval for this instr.
  99. unsigned newVReg = mri->createVirtualRegister(trc);
  100. vrm->grow();
  101. vrm->assignVirt2StackSlot(newVReg, ss);
  102. LiveInterval *newLI = &lis->getOrCreateInterval(newVReg);
  103. newLI->weight = HUGE_VALF;
  104. // Update the reg operands & kill flags.
  105. for (unsigned i = 0; i < indices.size(); ++i) {
  106. unsigned mopIdx = indices[i];
  107. MachineOperand &mop = mi->getOperand(mopIdx);
  108. mop.setReg(newVReg);
  109. if (mop.isUse() && !mi->isRegTiedToDefOperand(mopIdx)) {
  110. mop.setIsKill(true);
  111. }
  112. }
  113. assert(hasUse || hasDef);
  114. // Insert reload if necessary.
  115. MachineBasicBlock::iterator miItr(mi);
  116. if (hasUse) {
  117. tii->loadRegFromStackSlot(*mi->getParent(), miItr, newVReg, ss, trc,
  118. tri);
  119. MachineInstr *loadInstr(prior(miItr));
  120. SlotIndex loadIndex =
  121. lis->InsertMachineInstrInMaps(loadInstr).getDefIndex();
  122. vrm->addSpillSlotUse(ss, loadInstr);
  123. SlotIndex endIndex = loadIndex.getNextIndex();
  124. VNInfo *loadVNI =
  125. newLI->getNextValue(loadIndex, 0, lis->getVNInfoAllocator());
  126. newLI->addRange(LiveRange(loadIndex, endIndex, loadVNI));
  127. }
  128. // Insert store if necessary.
  129. if (hasDef) {
  130. tii->storeRegToStackSlot(*mi->getParent(), llvm::next(miItr), newVReg,
  131. true, ss, trc, tri);
  132. MachineInstr *storeInstr(llvm::next(miItr));
  133. SlotIndex storeIndex =
  134. lis->InsertMachineInstrInMaps(storeInstr).getDefIndex();
  135. vrm->addSpillSlotUse(ss, storeInstr);
  136. SlotIndex beginIndex = storeIndex.getPrevIndex();
  137. VNInfo *storeVNI =
  138. newLI->getNextValue(beginIndex, 0, lis->getVNInfoAllocator());
  139. newLI->addRange(LiveRange(beginIndex, storeIndex, storeVNI));
  140. }
  141. newIntervals.push_back(newLI);
  142. }
  143. }
  144. };
  145. } // end anonymous namespace
  146. namespace {
  147. /// Spills any live range using the spill-everywhere method with no attempt at
  148. /// folding.
  149. class TrivialSpiller : public SpillerBase {
  150. public:
  151. TrivialSpiller(MachineFunctionPass &pass, MachineFunction &mf,
  152. VirtRegMap &vrm)
  153. : SpillerBase(pass, mf, vrm) {}
  154. void spill(LiveInterval *li,
  155. SmallVectorImpl<LiveInterval*> &newIntervals,
  156. const SmallVectorImpl<LiveInterval*> &) {
  157. // Ignore spillIs - we don't use it.
  158. trivialSpillEverywhere(li, newIntervals);
  159. }
  160. };
  161. } // end anonymous namespace
  162. namespace {
  163. /// Falls back on LiveIntervals::addIntervalsForSpills.
  164. class StandardSpiller : public Spiller {
  165. protected:
  166. MachineFunction *mf;
  167. LiveIntervals *lis;
  168. LiveStacks *lss;
  169. MachineLoopInfo *loopInfo;
  170. VirtRegMap *vrm;
  171. public:
  172. StandardSpiller(MachineFunctionPass &pass, MachineFunction &mf,
  173. VirtRegMap &vrm)
  174. : mf(&mf),
  175. lis(&pass.getAnalysis<LiveIntervals>()),
  176. lss(&pass.getAnalysis<LiveStacks>()),
  177. loopInfo(pass.getAnalysisIfAvailable<MachineLoopInfo>()),
  178. vrm(&vrm) {}
  179. /// Falls back on LiveIntervals::addIntervalsForSpills.
  180. void spill(LiveInterval *li,
  181. SmallVectorImpl<LiveInterval*> &newIntervals,
  182. const SmallVectorImpl<LiveInterval*> &spillIs) {
  183. std::vector<LiveInterval*> added =
  184. lis->addIntervalsForSpills(*li, spillIs, loopInfo, *vrm);
  185. newIntervals.insert(newIntervals.end(), added.begin(), added.end());
  186. // Update LiveStacks.
  187. int SS = vrm->getStackSlot(li->reg);
  188. if (SS == VirtRegMap::NO_STACK_SLOT)
  189. return;
  190. const TargetRegisterClass *RC = mf->getRegInfo().getRegClass(li->reg);
  191. LiveInterval &SI = lss->getOrCreateInterval(SS, RC);
  192. if (!SI.hasAtLeastOneValue())
  193. SI.getNextValue(SlotIndex(), 0, lss->getVNInfoAllocator());
  194. SI.MergeRangesInAsValue(*li, SI.getValNumInfo(0));
  195. }
  196. };
  197. } // end anonymous namespace
  198. llvm::Spiller* llvm::createSpiller(MachineFunctionPass &pass,
  199. MachineFunction &mf,
  200. VirtRegMap &vrm) {
  201. switch (spillerOpt) {
  202. default: assert(0 && "unknown spiller");
  203. case trivial: return new TrivialSpiller(pass, mf, vrm);
  204. case standard: return new StandardSpiller(pass, mf, vrm);
  205. case inline_: return createInlineSpiller(pass, mf, vrm);
  206. }
  207. }