MachineRegisterInfo.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. //===-- lib/Codegen/MachineRegisterInfo.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. //
  10. // Implementation of the MachineRegisterInfo class.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/CodeGen/MachineRegisterInfo.h"
  14. #include "llvm/CodeGen/MachineInstrBuilder.h"
  15. #include "llvm/Target/TargetInstrInfo.h"
  16. #include "llvm/Target/TargetMachine.h"
  17. using namespace llvm;
  18. MachineRegisterInfo::MachineRegisterInfo(const TargetRegisterInfo &TRI)
  19. : TRI(&TRI), IsSSA(true), TracksLiveness(true) {
  20. VRegInfo.reserve(256);
  21. RegAllocHints.reserve(256);
  22. UsedPhysRegs.resize(TRI.getNumRegs());
  23. UsedPhysRegMask.resize(TRI.getNumRegs());
  24. // Create the physreg use/def lists.
  25. PhysRegUseDefLists = new MachineOperand*[TRI.getNumRegs()];
  26. memset(PhysRegUseDefLists, 0, sizeof(MachineOperand*)*TRI.getNumRegs());
  27. }
  28. MachineRegisterInfo::~MachineRegisterInfo() {
  29. #ifndef NDEBUG
  30. clearVirtRegs();
  31. for (unsigned i = 0, e = UsedPhysRegs.size(); i != e; ++i)
  32. assert(!PhysRegUseDefLists[i] &&
  33. "PhysRegUseDefLists has entries after all instructions are deleted");
  34. #endif
  35. delete [] PhysRegUseDefLists;
  36. }
  37. /// setRegClass - Set the register class of the specified virtual register.
  38. ///
  39. void
  40. MachineRegisterInfo::setRegClass(unsigned Reg, const TargetRegisterClass *RC) {
  41. VRegInfo[Reg].first = RC;
  42. }
  43. const TargetRegisterClass *
  44. MachineRegisterInfo::constrainRegClass(unsigned Reg,
  45. const TargetRegisterClass *RC,
  46. unsigned MinNumRegs) {
  47. const TargetRegisterClass *OldRC = getRegClass(Reg);
  48. if (OldRC == RC)
  49. return RC;
  50. const TargetRegisterClass *NewRC = TRI->getCommonSubClass(OldRC, RC);
  51. if (!NewRC || NewRC == OldRC)
  52. return NewRC;
  53. if (NewRC->getNumRegs() < MinNumRegs)
  54. return 0;
  55. setRegClass(Reg, NewRC);
  56. return NewRC;
  57. }
  58. bool
  59. MachineRegisterInfo::recomputeRegClass(unsigned Reg, const TargetMachine &TM) {
  60. const TargetInstrInfo *TII = TM.getInstrInfo();
  61. const TargetRegisterClass *OldRC = getRegClass(Reg);
  62. const TargetRegisterClass *NewRC = TRI->getLargestLegalSuperClass(OldRC);
  63. // Stop early if there is no room to grow.
  64. if (NewRC == OldRC)
  65. return false;
  66. // Accumulate constraints from all uses.
  67. for (reg_nodbg_iterator I = reg_nodbg_begin(Reg), E = reg_nodbg_end(); I != E;
  68. ++I) {
  69. const TargetRegisterClass *OpRC =
  70. I->getRegClassConstraint(I.getOperandNo(), TII, TRI);
  71. if (unsigned SubIdx = I.getOperand().getSubReg()) {
  72. if (OpRC)
  73. NewRC = TRI->getMatchingSuperRegClass(NewRC, OpRC, SubIdx);
  74. else
  75. NewRC = TRI->getSubClassWithSubReg(NewRC, SubIdx);
  76. } else if (OpRC)
  77. NewRC = TRI->getCommonSubClass(NewRC, OpRC);
  78. if (!NewRC || NewRC == OldRC)
  79. return false;
  80. }
  81. setRegClass(Reg, NewRC);
  82. return true;
  83. }
  84. /// createVirtualRegister - Create and return a new virtual register in the
  85. /// function with the specified register class.
  86. ///
  87. unsigned
  88. MachineRegisterInfo::createVirtualRegister(const TargetRegisterClass *RegClass){
  89. assert(RegClass && "Cannot create register without RegClass!");
  90. assert(RegClass->isAllocatable() &&
  91. "Virtual register RegClass must be allocatable.");
  92. // New virtual register number.
  93. unsigned Reg = TargetRegisterInfo::index2VirtReg(getNumVirtRegs());
  94. // Add a reg, but keep track of whether the vector reallocated or not.
  95. const unsigned FirstVirtReg = TargetRegisterInfo::index2VirtReg(0);
  96. void *ArrayBase = getNumVirtRegs() == 0 ? 0 : &VRegInfo[FirstVirtReg];
  97. VRegInfo.grow(Reg);
  98. VRegInfo[Reg].first = RegClass;
  99. RegAllocHints.grow(Reg);
  100. if (ArrayBase && &VRegInfo[FirstVirtReg] != ArrayBase)
  101. // The vector reallocated, handle this now.
  102. HandleVRegListReallocation();
  103. return Reg;
  104. }
  105. /// clearVirtRegs - Remove all virtual registers (after physreg assignment).
  106. void MachineRegisterInfo::clearVirtRegs() {
  107. #ifndef NDEBUG
  108. for (unsigned i = 0, e = getNumVirtRegs(); i != e; ++i)
  109. assert(VRegInfo[TargetRegisterInfo::index2VirtReg(i)].second == 0 &&
  110. "Vreg use list non-empty still?");
  111. #endif
  112. VRegInfo.clear();
  113. }
  114. /// HandleVRegListReallocation - We just added a virtual register to the
  115. /// VRegInfo info list and it reallocated. Update the use/def lists info
  116. /// pointers.
  117. void MachineRegisterInfo::HandleVRegListReallocation() {
  118. // The back pointers for the vreg lists point into the previous vector.
  119. // Update them to point to their correct slots.
  120. for (unsigned i = 0, e = getNumVirtRegs(); i != e; ++i) {
  121. unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
  122. MachineOperand *List = VRegInfo[Reg].second;
  123. if (!List) continue;
  124. // Update the back-pointer to be accurate once more.
  125. List->Contents.Reg.Prev = &VRegInfo[Reg].second;
  126. }
  127. }
  128. /// replaceRegWith - Replace all instances of FromReg with ToReg in the
  129. /// machine function. This is like llvm-level X->replaceAllUsesWith(Y),
  130. /// except that it also changes any definitions of the register as well.
  131. void MachineRegisterInfo::replaceRegWith(unsigned FromReg, unsigned ToReg) {
  132. assert(FromReg != ToReg && "Cannot replace a reg with itself");
  133. // TODO: This could be more efficient by bulk changing the operands.
  134. for (reg_iterator I = reg_begin(FromReg), E = reg_end(); I != E; ) {
  135. MachineOperand &O = I.getOperand();
  136. ++I;
  137. O.setReg(ToReg);
  138. }
  139. }
  140. /// getVRegDef - Return the machine instr that defines the specified virtual
  141. /// register or null if none is found. This assumes that the code is in SSA
  142. /// form, so there should only be one definition.
  143. MachineInstr *MachineRegisterInfo::getVRegDef(unsigned Reg) const {
  144. // Since we are in SSA form, we can use the first definition.
  145. def_iterator I = def_begin(Reg);
  146. return !I.atEnd() ? &*I : 0;
  147. }
  148. bool MachineRegisterInfo::hasOneUse(unsigned RegNo) const {
  149. use_iterator UI = use_begin(RegNo);
  150. if (UI == use_end())
  151. return false;
  152. return ++UI == use_end();
  153. }
  154. bool MachineRegisterInfo::hasOneNonDBGUse(unsigned RegNo) const {
  155. use_nodbg_iterator UI = use_nodbg_begin(RegNo);
  156. if (UI == use_nodbg_end())
  157. return false;
  158. return ++UI == use_nodbg_end();
  159. }
  160. /// clearKillFlags - Iterate over all the uses of the given register and
  161. /// clear the kill flag from the MachineOperand. This function is used by
  162. /// optimization passes which extend register lifetimes and need only
  163. /// preserve conservative kill flag information.
  164. void MachineRegisterInfo::clearKillFlags(unsigned Reg) const {
  165. for (use_iterator UI = use_begin(Reg), UE = use_end(); UI != UE; ++UI)
  166. UI.getOperand().setIsKill(false);
  167. }
  168. bool MachineRegisterInfo::isLiveIn(unsigned Reg) const {
  169. for (livein_iterator I = livein_begin(), E = livein_end(); I != E; ++I)
  170. if (I->first == Reg || I->second == Reg)
  171. return true;
  172. return false;
  173. }
  174. bool MachineRegisterInfo::isLiveOut(unsigned Reg) const {
  175. for (liveout_iterator I = liveout_begin(), E = liveout_end(); I != E; ++I)
  176. if (*I == Reg)
  177. return true;
  178. return false;
  179. }
  180. /// getLiveInPhysReg - If VReg is a live-in virtual register, return the
  181. /// corresponding live-in physical register.
  182. unsigned MachineRegisterInfo::getLiveInPhysReg(unsigned VReg) const {
  183. for (livein_iterator I = livein_begin(), E = livein_end(); I != E; ++I)
  184. if (I->second == VReg)
  185. return I->first;
  186. return 0;
  187. }
  188. /// getLiveInVirtReg - If PReg is a live-in physical register, return the
  189. /// corresponding live-in physical register.
  190. unsigned MachineRegisterInfo::getLiveInVirtReg(unsigned PReg) const {
  191. for (livein_iterator I = livein_begin(), E = livein_end(); I != E; ++I)
  192. if (I->first == PReg)
  193. return I->second;
  194. return 0;
  195. }
  196. /// EmitLiveInCopies - Emit copies to initialize livein virtual registers
  197. /// into the given entry block.
  198. void
  199. MachineRegisterInfo::EmitLiveInCopies(MachineBasicBlock *EntryMBB,
  200. const TargetRegisterInfo &TRI,
  201. const TargetInstrInfo &TII) {
  202. // Emit the copies into the top of the block.
  203. for (unsigned i = 0, e = LiveIns.size(); i != e; ++i)
  204. if (LiveIns[i].second) {
  205. if (use_empty(LiveIns[i].second)) {
  206. // The livein has no uses. Drop it.
  207. //
  208. // It would be preferable to have isel avoid creating live-in
  209. // records for unused arguments in the first place, but it's
  210. // complicated by the debug info code for arguments.
  211. LiveIns.erase(LiveIns.begin() + i);
  212. --i; --e;
  213. } else {
  214. // Emit a copy.
  215. BuildMI(*EntryMBB, EntryMBB->begin(), DebugLoc(),
  216. TII.get(TargetOpcode::COPY), LiveIns[i].second)
  217. .addReg(LiveIns[i].first);
  218. // Add the register to the entry block live-in set.
  219. EntryMBB->addLiveIn(LiveIns[i].first);
  220. }
  221. } else {
  222. // Add the register to the entry block live-in set.
  223. EntryMBB->addLiveIn(LiveIns[i].first);
  224. }
  225. }
  226. #ifndef NDEBUG
  227. void MachineRegisterInfo::dumpUses(unsigned Reg) const {
  228. for (use_iterator I = use_begin(Reg), E = use_end(); I != E; ++I)
  229. I.getOperand().getParent()->dump();
  230. }
  231. #endif
  232. void MachineRegisterInfo::freezeReservedRegs(const MachineFunction &MF) {
  233. ReservedRegs = TRI->getReservedRegs(MF);
  234. }
  235. bool MachineRegisterInfo::isConstantPhysReg(unsigned PhysReg,
  236. const MachineFunction &MF) const {
  237. assert(TargetRegisterInfo::isPhysicalRegister(PhysReg));
  238. // Check if any overlapping register is modified.
  239. for (MCRegAliasIterator AI(PhysReg, TRI, true); AI.isValid(); ++AI)
  240. if (!def_empty(*AI))
  241. return false;
  242. // Check if any overlapping register is allocatable so it may be used later.
  243. if (AllocatableRegs.empty())
  244. AllocatableRegs = TRI->getAllocatableSet(MF);
  245. for (MCRegAliasIterator AI(PhysReg, TRI, true); AI.isValid(); ++AI)
  246. if (AllocatableRegs.test(*AI))
  247. return false;
  248. return true;
  249. }