PageRenderTime 62ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/CodeGen/RegisterScavenging.cpp

https://github.com/lygstate/llvm-mirror
C++ | 396 lines | 270 code | 52 blank | 74 comment | 95 complexity | cf915b848b67b952889920cad2e0e3e2 MD5 | raw file
  1. //===-- RegisterScavenging.cpp - Machine register scavenging --------------===//
  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. //
  10. // This file implements the machine register scavenger. It can provide
  11. // information, such as unused registers, at any point in a machine basic block.
  12. // It also provides a mechanism to make registers available by evicting them to
  13. // spill slots.
  14. //
  15. //===----------------------------------------------------------------------===//
  16. #define DEBUG_TYPE "reg-scavenging"
  17. #include "llvm/CodeGen/RegisterScavenging.h"
  18. #include "llvm/CodeGen/MachineFrameInfo.h"
  19. #include "llvm/CodeGen/MachineFunction.h"
  20. #include "llvm/CodeGen/MachineBasicBlock.h"
  21. #include "llvm/CodeGen/MachineInstr.h"
  22. #include "llvm/CodeGen/MachineRegisterInfo.h"
  23. #include "llvm/Support/Debug.h"
  24. #include "llvm/Support/ErrorHandling.h"
  25. #include "llvm/Support/raw_ostream.h"
  26. #include "llvm/Target/TargetRegisterInfo.h"
  27. #include "llvm/Target/TargetInstrInfo.h"
  28. #include "llvm/Target/TargetMachine.h"
  29. #include "llvm/ADT/DenseMap.h"
  30. #include "llvm/ADT/SmallPtrSet.h"
  31. #include "llvm/ADT/SmallVector.h"
  32. #include "llvm/ADT/STLExtras.h"
  33. using namespace llvm;
  34. /// setUsed - Set the register and its sub-registers as being used.
  35. void RegScavenger::setUsed(unsigned Reg) {
  36. RegsAvailable.reset(Reg);
  37. for (const unsigned *SubRegs = TRI->getSubRegisters(Reg);
  38. unsigned SubReg = *SubRegs; ++SubRegs)
  39. RegsAvailable.reset(SubReg);
  40. }
  41. bool RegScavenger::isAliasUsed(unsigned Reg) const {
  42. if (isUsed(Reg))
  43. return true;
  44. for (const unsigned *R = TRI->getAliasSet(Reg); *R; ++R)
  45. if (isUsed(*R))
  46. return true;
  47. return false;
  48. }
  49. void RegScavenger::initRegState() {
  50. ScavengedReg = 0;
  51. ScavengedRC = NULL;
  52. ScavengeRestore = NULL;
  53. // All registers started out unused.
  54. RegsAvailable.set();
  55. // Reserved registers are always used.
  56. RegsAvailable ^= ReservedRegs;
  57. if (!MBB)
  58. return;
  59. // Live-in registers are in use.
  60. for (MachineBasicBlock::livein_iterator I = MBB->livein_begin(),
  61. E = MBB->livein_end(); I != E; ++I)
  62. setUsed(*I);
  63. // Pristine CSRs are also unavailable.
  64. BitVector PR = MBB->getParent()->getFrameInfo()->getPristineRegs(MBB);
  65. for (int I = PR.find_first(); I>0; I = PR.find_next(I))
  66. setUsed(I);
  67. }
  68. void RegScavenger::enterBasicBlock(MachineBasicBlock *mbb) {
  69. MachineFunction &MF = *mbb->getParent();
  70. const TargetMachine &TM = MF.getTarget();
  71. TII = TM.getInstrInfo();
  72. TRI = TM.getRegisterInfo();
  73. MRI = &MF.getRegInfo();
  74. assert((NumPhysRegs == 0 || NumPhysRegs == TRI->getNumRegs()) &&
  75. "Target changed?");
  76. // Self-initialize.
  77. if (!MBB) {
  78. NumPhysRegs = TRI->getNumRegs();
  79. RegsAvailable.resize(NumPhysRegs);
  80. // Create reserved registers bitvector.
  81. ReservedRegs = TRI->getReservedRegs(MF);
  82. // Create callee-saved registers bitvector.
  83. CalleeSavedRegs.resize(NumPhysRegs);
  84. const unsigned *CSRegs = TRI->getCalleeSavedRegs();
  85. if (CSRegs != NULL)
  86. for (unsigned i = 0; CSRegs[i]; ++i)
  87. CalleeSavedRegs.set(CSRegs[i]);
  88. }
  89. MBB = mbb;
  90. initRegState();
  91. Tracking = false;
  92. }
  93. void RegScavenger::addRegWithSubRegs(BitVector &BV, unsigned Reg) {
  94. BV.set(Reg);
  95. for (const unsigned *R = TRI->getSubRegisters(Reg); *R; R++)
  96. BV.set(*R);
  97. }
  98. void RegScavenger::addRegWithAliases(BitVector &BV, unsigned Reg) {
  99. BV.set(Reg);
  100. for (const unsigned *R = TRI->getAliasSet(Reg); *R; R++)
  101. BV.set(*R);
  102. }
  103. void RegScavenger::forward() {
  104. // Move ptr forward.
  105. if (!Tracking) {
  106. MBBI = MBB->begin();
  107. Tracking = true;
  108. } else {
  109. assert(MBBI != MBB->end() && "Already past the end of the basic block!");
  110. MBBI = llvm::next(MBBI);
  111. }
  112. assert(MBBI != MBB->end() && "Already at the end of the basic block!");
  113. MachineInstr *MI = MBBI;
  114. if (MI == ScavengeRestore) {
  115. ScavengedReg = 0;
  116. ScavengedRC = NULL;
  117. ScavengeRestore = NULL;
  118. }
  119. if (MI->isDebugValue())
  120. return;
  121. // Find out which registers are early clobbered, killed, defined, and marked
  122. // def-dead in this instruction.
  123. // FIXME: The scavenger is not predication aware. If the instruction is
  124. // predicated, conservatively assume "kill" markers do not actually kill the
  125. // register. Similarly ignores "dead" markers.
  126. bool isPred = TII->isPredicated(MI);
  127. BitVector EarlyClobberRegs(NumPhysRegs);
  128. BitVector KillRegs(NumPhysRegs);
  129. BitVector DefRegs(NumPhysRegs);
  130. BitVector DeadRegs(NumPhysRegs);
  131. for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
  132. const MachineOperand &MO = MI->getOperand(i);
  133. if (!MO.isReg())
  134. continue;
  135. unsigned Reg = MO.getReg();
  136. if (!Reg || isReserved(Reg))
  137. continue;
  138. if (MO.isUse()) {
  139. // Ignore undef uses.
  140. if (MO.isUndef())
  141. continue;
  142. // Two-address operands implicitly kill.
  143. if (!isPred && (MO.isKill() || MI->isRegTiedToDefOperand(i)))
  144. addRegWithSubRegs(KillRegs, Reg);
  145. } else {
  146. assert(MO.isDef());
  147. if (!isPred && MO.isDead())
  148. addRegWithSubRegs(DeadRegs, Reg);
  149. else
  150. addRegWithSubRegs(DefRegs, Reg);
  151. if (MO.isEarlyClobber())
  152. addRegWithAliases(EarlyClobberRegs, Reg);
  153. }
  154. }
  155. // Verify uses and defs.
  156. for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
  157. const MachineOperand &MO = MI->getOperand(i);
  158. if (!MO.isReg())
  159. continue;
  160. unsigned Reg = MO.getReg();
  161. if (!Reg || isReserved(Reg))
  162. continue;
  163. if (MO.isUse()) {
  164. if (MO.isUndef())
  165. continue;
  166. if (!isUsed(Reg)) {
  167. // Check if it's partial live: e.g.
  168. // D0 = insert_subreg D0<undef>, S0
  169. // ... D0
  170. // The problem is the insert_subreg could be eliminated. The use of
  171. // D0 is using a partially undef value. This is not *incorrect* since
  172. // S1 is can be freely clobbered.
  173. // Ideally we would like a way to model this, but leaving the
  174. // insert_subreg around causes both correctness and performance issues.
  175. bool SubUsed = false;
  176. for (const unsigned *SubRegs = TRI->getSubRegisters(Reg);
  177. unsigned SubReg = *SubRegs; ++SubRegs)
  178. if (isUsed(SubReg)) {
  179. SubUsed = true;
  180. break;
  181. }
  182. assert(SubUsed && "Using an undefined register!");
  183. (void)SubUsed;
  184. }
  185. assert((!EarlyClobberRegs.test(Reg) || MI->isRegTiedToDefOperand(i)) &&
  186. "Using an early clobbered register!");
  187. } else {
  188. assert(MO.isDef());
  189. #if 0
  190. // FIXME: Enable this once we've figured out how to correctly transfer
  191. // implicit kills during codegen passes like the coalescer.
  192. assert((KillRegs.test(Reg) || isUnused(Reg) ||
  193. isLiveInButUnusedBefore(Reg, MI, MBB, TRI, MRI)) &&
  194. "Re-defining a live register!");
  195. #endif
  196. }
  197. }
  198. // Commit the changes.
  199. setUnused(KillRegs);
  200. setUnused(DeadRegs);
  201. setUsed(DefRegs);
  202. }
  203. void RegScavenger::getRegsUsed(BitVector &used, bool includeReserved) {
  204. if (includeReserved)
  205. used = ~RegsAvailable;
  206. else
  207. used = ~RegsAvailable & ~ReservedRegs;
  208. }
  209. unsigned RegScavenger::FindUnusedReg(const TargetRegisterClass *RC) const {
  210. for (TargetRegisterClass::iterator I = RC->begin(), E = RC->end();
  211. I != E; ++I)
  212. if (!isAliasUsed(*I)) {
  213. DEBUG(dbgs() << "Scavenger found unused reg: " << TRI->getName(*I) <<
  214. "\n");
  215. return *I;
  216. }
  217. return 0;
  218. }
  219. /// getRegsAvailable - Return all available registers in the register class
  220. /// in Mask.
  221. BitVector RegScavenger::getRegsAvailable(const TargetRegisterClass *RC) {
  222. BitVector Mask(TRI->getNumRegs());
  223. for (TargetRegisterClass::iterator I = RC->begin(), E = RC->end();
  224. I != E; ++I)
  225. if (!isAliasUsed(*I))
  226. Mask.set(*I);
  227. return Mask;
  228. }
  229. /// findSurvivorReg - Return the candidate register that is unused for the
  230. /// longest after StargMII. UseMI is set to the instruction where the search
  231. /// stopped.
  232. ///
  233. /// No more than InstrLimit instructions are inspected.
  234. ///
  235. unsigned RegScavenger::findSurvivorReg(MachineBasicBlock::iterator StartMI,
  236. BitVector &Candidates,
  237. unsigned InstrLimit,
  238. MachineBasicBlock::iterator &UseMI) {
  239. int Survivor = Candidates.find_first();
  240. assert(Survivor > 0 && "No candidates for scavenging");
  241. MachineBasicBlock::iterator ME = MBB->getFirstTerminator();
  242. assert(StartMI != ME && "MI already at terminator");
  243. MachineBasicBlock::iterator RestorePointMI = StartMI;
  244. MachineBasicBlock::iterator MI = StartMI;
  245. bool inVirtLiveRange = false;
  246. for (++MI; InstrLimit > 0 && MI != ME; ++MI, --InstrLimit) {
  247. if (MI->isDebugValue()) {
  248. ++InstrLimit; // Don't count debug instructions
  249. continue;
  250. }
  251. bool isVirtKillInsn = false;
  252. bool isVirtDefInsn = false;
  253. // Remove any candidates touched by instruction.
  254. for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
  255. const MachineOperand &MO = MI->getOperand(i);
  256. if (!MO.isReg() || MO.isUndef() || !MO.getReg())
  257. continue;
  258. if (TargetRegisterInfo::isVirtualRegister(MO.getReg())) {
  259. if (MO.isDef())
  260. isVirtDefInsn = true;
  261. else if (MO.isKill())
  262. isVirtKillInsn = true;
  263. continue;
  264. }
  265. Candidates.reset(MO.getReg());
  266. for (const unsigned *R = TRI->getAliasSet(MO.getReg()); *R; R++)
  267. Candidates.reset(*R);
  268. }
  269. // If we're not in a virtual reg's live range, this is a valid
  270. // restore point.
  271. if (!inVirtLiveRange) RestorePointMI = MI;
  272. // Update whether we're in the live range of a virtual register
  273. if (isVirtKillInsn) inVirtLiveRange = false;
  274. if (isVirtDefInsn) inVirtLiveRange = true;
  275. // Was our survivor untouched by this instruction?
  276. if (Candidates.test(Survivor))
  277. continue;
  278. // All candidates gone?
  279. if (Candidates.none())
  280. break;
  281. Survivor = Candidates.find_first();
  282. }
  283. // If we ran off the end, that's where we want to restore.
  284. if (MI == ME) RestorePointMI = ME;
  285. assert (RestorePointMI != StartMI &&
  286. "No available scavenger restore location!");
  287. // We ran out of candidates, so stop the search.
  288. UseMI = RestorePointMI;
  289. return Survivor;
  290. }
  291. unsigned RegScavenger::scavengeRegister(const TargetRegisterClass *RC,
  292. MachineBasicBlock::iterator I,
  293. int SPAdj) {
  294. // Consider all allocatable registers in the register class initially
  295. BitVector Candidates =
  296. TRI->getAllocatableSet(*I->getParent()->getParent(), RC);
  297. // Exclude all the registers being used by the instruction.
  298. for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
  299. MachineOperand &MO = I->getOperand(i);
  300. if (MO.isReg() && MO.getReg() != 0 &&
  301. !TargetRegisterInfo::isVirtualRegister(MO.getReg()))
  302. Candidates.reset(MO.getReg());
  303. }
  304. // Try to find a register that's unused if there is one, as then we won't
  305. // have to spill. Search explicitly rather than masking out based on
  306. // RegsAvailable, as RegsAvailable does not take aliases into account.
  307. // That's what getRegsAvailable() is for.
  308. BitVector Available = getRegsAvailable(RC);
  309. if ((Candidates & Available).any())
  310. Candidates &= Available;
  311. // Find the register whose use is furthest away.
  312. MachineBasicBlock::iterator UseMI;
  313. unsigned SReg = findSurvivorReg(I, Candidates, 25, UseMI);
  314. // If we found an unused register there is no reason to spill it.
  315. if (!isAliasUsed(SReg)) {
  316. DEBUG(dbgs() << "Scavenged register: " << TRI->getName(SReg) << "\n");
  317. return SReg;
  318. }
  319. assert(ScavengedReg == 0 &&
  320. "Scavenger slot is live, unable to scavenge another register!");
  321. // Avoid infinite regress
  322. ScavengedReg = SReg;
  323. // If the target knows how to save/restore the register, let it do so;
  324. // otherwise, use the emergency stack spill slot.
  325. if (!TRI->saveScavengerRegister(*MBB, I, UseMI, RC, SReg)) {
  326. // Spill the scavenged register before I.
  327. assert(ScavengingFrameIndex >= 0 &&
  328. "Cannot scavenge register without an emergency spill slot!");
  329. TII->storeRegToStackSlot(*MBB, I, SReg, true, ScavengingFrameIndex, RC,TRI);
  330. MachineBasicBlock::iterator II = prior(I);
  331. TRI->eliminateFrameIndex(II, SPAdj, this);
  332. // Restore the scavenged register before its use (or first terminator).
  333. TII->loadRegFromStackSlot(*MBB, UseMI, SReg, ScavengingFrameIndex, RC, TRI);
  334. II = prior(UseMI);
  335. TRI->eliminateFrameIndex(II, SPAdj, this);
  336. }
  337. ScavengeRestore = prior(UseMI);
  338. // Doing this here leads to infinite regress.
  339. // ScavengedReg = SReg;
  340. ScavengedRC = RC;
  341. DEBUG(dbgs() << "Scavenged register (with spill): " << TRI->getName(SReg) <<
  342. "\n");
  343. return SReg;
  344. }