MachineRegisterInfo.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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. UsedRegUnits.resize(TRI.getNumRegUnits());
  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. delete [] PhysRegUseDefLists;
  30. }
  31. /// setRegClass - Set the register class of the specified virtual register.
  32. ///
  33. void
  34. MachineRegisterInfo::setRegClass(unsigned Reg, const TargetRegisterClass *RC) {
  35. VRegInfo[Reg].first = RC;
  36. }
  37. const TargetRegisterClass *
  38. MachineRegisterInfo::constrainRegClass(unsigned Reg,
  39. const TargetRegisterClass *RC,
  40. unsigned MinNumRegs) {
  41. const TargetRegisterClass *OldRC = getRegClass(Reg);
  42. if (OldRC == RC)
  43. return RC;
  44. const TargetRegisterClass *NewRC = TRI->getCommonSubClass(OldRC, RC);
  45. if (!NewRC || NewRC == OldRC)
  46. return NewRC;
  47. if (NewRC->getNumRegs() < MinNumRegs)
  48. return 0;
  49. setRegClass(Reg, NewRC);
  50. return NewRC;
  51. }
  52. bool
  53. MachineRegisterInfo::recomputeRegClass(unsigned Reg, const TargetMachine &TM) {
  54. const TargetInstrInfo *TII = TM.getInstrInfo();
  55. const TargetRegisterClass *OldRC = getRegClass(Reg);
  56. const TargetRegisterClass *NewRC = TRI->getLargestLegalSuperClass(OldRC);
  57. // Stop early if there is no room to grow.
  58. if (NewRC == OldRC)
  59. return false;
  60. // Accumulate constraints from all uses.
  61. for (reg_nodbg_iterator I = reg_nodbg_begin(Reg), E = reg_nodbg_end(); I != E;
  62. ++I) {
  63. const TargetRegisterClass *OpRC =
  64. I->getRegClassConstraint(I.getOperandNo(), TII, TRI);
  65. if (unsigned SubIdx = I.getOperand().getSubReg()) {
  66. if (OpRC)
  67. NewRC = TRI->getMatchingSuperRegClass(NewRC, OpRC, SubIdx);
  68. else
  69. NewRC = TRI->getSubClassWithSubReg(NewRC, SubIdx);
  70. } else if (OpRC)
  71. NewRC = TRI->getCommonSubClass(NewRC, OpRC);
  72. if (!NewRC || NewRC == OldRC)
  73. return false;
  74. }
  75. setRegClass(Reg, NewRC);
  76. return true;
  77. }
  78. /// createVirtualRegister - Create and return a new virtual register in the
  79. /// function with the specified register class.
  80. ///
  81. unsigned
  82. MachineRegisterInfo::createVirtualRegister(const TargetRegisterClass *RegClass){
  83. assert(RegClass && "Cannot create register without RegClass!");
  84. assert(RegClass->isAllocatable() &&
  85. "Virtual register RegClass must be allocatable.");
  86. // New virtual register number.
  87. unsigned Reg = TargetRegisterInfo::index2VirtReg(getNumVirtRegs());
  88. VRegInfo.grow(Reg);
  89. VRegInfo[Reg].first = RegClass;
  90. RegAllocHints.grow(Reg);
  91. return Reg;
  92. }
  93. /// clearVirtRegs - Remove all virtual registers (after physreg assignment).
  94. void MachineRegisterInfo::clearVirtRegs() {
  95. #ifndef NDEBUG
  96. for (unsigned i = 0, e = getNumVirtRegs(); i != e; ++i)
  97. assert(VRegInfo[TargetRegisterInfo::index2VirtReg(i)].second == 0 &&
  98. "Vreg use list non-empty still?");
  99. #endif
  100. VRegInfo.clear();
  101. }
  102. /// Add MO to the linked list of operands for its register.
  103. void MachineRegisterInfo::addRegOperandToUseList(MachineOperand *MO) {
  104. assert(!MO->isOnRegUseList() && "Already on list");
  105. MachineOperand *&HeadRef = getRegUseDefListHead(MO->getReg());
  106. MachineOperand *const Head = HeadRef;
  107. // Head points to the first list element.
  108. // Next is NULL on the last list element.
  109. // Prev pointers are circular, so Head->Prev == Last.
  110. // Head is NULL for an empty list.
  111. if (!Head) {
  112. MO->Contents.Reg.Prev = MO;
  113. MO->Contents.Reg.Next = 0;
  114. HeadRef = MO;
  115. return;
  116. }
  117. assert(MO->getReg() == Head->getReg() && "Different regs on the same list!");
  118. // Insert MO between Last and Head in the circular Prev chain.
  119. MachineOperand *Last = Head->Contents.Reg.Prev;
  120. assert(Last && "Inconsistent use list");
  121. assert(MO->getReg() == Last->getReg() && "Different regs on the same list!");
  122. Head->Contents.Reg.Prev = MO;
  123. MO->Contents.Reg.Prev = Last;
  124. // Def operands always precede uses. This allows def_iterator to stop early.
  125. // Insert def operands at the front, and use operands at the back.
  126. if (MO->isDef()) {
  127. // Insert def at the front.
  128. MO->Contents.Reg.Next = Head;
  129. HeadRef = MO;
  130. } else {
  131. // Insert use at the end.
  132. MO->Contents.Reg.Next = 0;
  133. Last->Contents.Reg.Next = MO;
  134. }
  135. }
  136. /// Remove MO from its use-def list.
  137. void MachineRegisterInfo::removeRegOperandFromUseList(MachineOperand *MO) {
  138. assert(MO->isOnRegUseList() && "Operand not on use list");
  139. MachineOperand *&HeadRef = getRegUseDefListHead(MO->getReg());
  140. MachineOperand *const Head = HeadRef;
  141. assert(Head && "List already empty");
  142. // Unlink this from the doubly linked list of operands.
  143. MachineOperand *Next = MO->Contents.Reg.Next;
  144. MachineOperand *Prev = MO->Contents.Reg.Prev;
  145. // Prev links are circular, next link is NULL instead of looping back to Head.
  146. if (MO == Head)
  147. HeadRef = Next;
  148. else
  149. Prev->Contents.Reg.Next = Next;
  150. (Next ? Next : Head)->Contents.Reg.Prev = Prev;
  151. MO->Contents.Reg.Prev = 0;
  152. MO->Contents.Reg.Next = 0;
  153. }
  154. /// Move NumOps operands from Src to Dst, updating use-def lists as needed.
  155. ///
  156. /// The Dst range is assumed to be uninitialized memory. (Or it may contain
  157. /// operands that won't be destroyed, which is OK because the MO destructor is
  158. /// trivial anyway).
  159. ///
  160. /// The Src and Dst ranges may overlap.
  161. void MachineRegisterInfo::moveOperands(MachineOperand *Dst,
  162. MachineOperand *Src,
  163. unsigned NumOps) {
  164. assert(Src != Dst && NumOps && "Noop moveOperands");
  165. // Copy backwards if Dst is within the Src range.
  166. int Stride = 1;
  167. if (Dst >= Src && Dst < Src + NumOps) {
  168. Stride = -1;
  169. Dst += NumOps - 1;
  170. Src += NumOps - 1;
  171. }
  172. // Copy one operand at a time.
  173. do {
  174. new (Dst) MachineOperand(*Src);
  175. // Dst takes Src's place in the use-def chain.
  176. if (Src->isReg()) {
  177. MachineOperand *&Head = getRegUseDefListHead(Src->getReg());
  178. MachineOperand *Prev = Src->Contents.Reg.Prev;
  179. MachineOperand *Next = Src->Contents.Reg.Next;
  180. assert(Head && "List empty, but operand is chained");
  181. assert(Prev && "Operand was not on use-def list");
  182. // Prev links are circular, next link is NULL instead of looping back to
  183. // Head.
  184. if (Src == Head)
  185. Head = Dst;
  186. else
  187. Prev->Contents.Reg.Next = Dst;
  188. // Update Prev pointer. This also works when Src was pointing to itself
  189. // in a 1-element list. In that case Head == Dst.
  190. (Next ? Next : Head)->Contents.Reg.Prev = Dst;
  191. }
  192. Dst += Stride;
  193. Src += Stride;
  194. } while (--NumOps);
  195. }
  196. /// replaceRegWith - Replace all instances of FromReg with ToReg in the
  197. /// machine function. This is like llvm-level X->replaceAllUsesWith(Y),
  198. /// except that it also changes any definitions of the register as well.
  199. void MachineRegisterInfo::replaceRegWith(unsigned FromReg, unsigned ToReg) {
  200. assert(FromReg != ToReg && "Cannot replace a reg with itself");
  201. // TODO: This could be more efficient by bulk changing the operands.
  202. for (reg_iterator I = reg_begin(FromReg), E = reg_end(); I != E; ) {
  203. MachineOperand &O = I.getOperand();
  204. ++I;
  205. O.setReg(ToReg);
  206. }
  207. }
  208. /// getVRegDef - Return the machine instr that defines the specified virtual
  209. /// register or null if none is found. This assumes that the code is in SSA
  210. /// form, so there should only be one definition.
  211. MachineInstr *MachineRegisterInfo::getVRegDef(unsigned Reg) const {
  212. // Since we are in SSA form, we can use the first definition.
  213. def_iterator I = def_begin(Reg);
  214. assert((I.atEnd() || llvm::next(I) == def_end()) &&
  215. "getVRegDef assumes a single definition or no definition");
  216. return !I.atEnd() ? &*I : 0;
  217. }
  218. /// getUniqueVRegDef - Return the unique machine instr that defines the
  219. /// specified virtual register or null if none is found. If there are
  220. /// multiple definitions or no definition, return null.
  221. MachineInstr *MachineRegisterInfo::getUniqueVRegDef(unsigned Reg) const {
  222. if (def_empty(Reg)) return 0;
  223. def_iterator I = def_begin(Reg);
  224. if (llvm::next(I) != def_end())
  225. return 0;
  226. return &*I;
  227. }
  228. bool MachineRegisterInfo::hasOneNonDBGUse(unsigned RegNo) const {
  229. use_nodbg_iterator UI = use_nodbg_begin(RegNo);
  230. if (UI == use_nodbg_end())
  231. return false;
  232. return ++UI == use_nodbg_end();
  233. }
  234. /// clearKillFlags - Iterate over all the uses of the given register and
  235. /// clear the kill flag from the MachineOperand. This function is used by
  236. /// optimization passes which extend register lifetimes and need only
  237. /// preserve conservative kill flag information.
  238. void MachineRegisterInfo::clearKillFlags(unsigned Reg) const {
  239. for (use_iterator UI = use_begin(Reg), UE = use_end(); UI != UE; ++UI)
  240. UI.getOperand().setIsKill(false);
  241. }
  242. bool MachineRegisterInfo::isLiveIn(unsigned Reg) const {
  243. for (livein_iterator I = livein_begin(), E = livein_end(); I != E; ++I)
  244. if (I->first == Reg || I->second == Reg)
  245. return true;
  246. return false;
  247. }
  248. /// getLiveInPhysReg - If VReg is a live-in virtual register, return the
  249. /// corresponding live-in physical register.
  250. unsigned MachineRegisterInfo::getLiveInPhysReg(unsigned VReg) const {
  251. for (livein_iterator I = livein_begin(), E = livein_end(); I != E; ++I)
  252. if (I->second == VReg)
  253. return I->first;
  254. return 0;
  255. }
  256. /// getLiveInVirtReg - If PReg is a live-in physical register, return the
  257. /// corresponding live-in physical register.
  258. unsigned MachineRegisterInfo::getLiveInVirtReg(unsigned PReg) const {
  259. for (livein_iterator I = livein_begin(), E = livein_end(); I != E; ++I)
  260. if (I->first == PReg)
  261. return I->second;
  262. return 0;
  263. }
  264. /// EmitLiveInCopies - Emit copies to initialize livein virtual registers
  265. /// into the given entry block.
  266. void
  267. MachineRegisterInfo::EmitLiveInCopies(MachineBasicBlock *EntryMBB,
  268. const TargetRegisterInfo &TRI,
  269. const TargetInstrInfo &TII) {
  270. // Emit the copies into the top of the block.
  271. for (unsigned i = 0, e = LiveIns.size(); i != e; ++i)
  272. if (LiveIns[i].second) {
  273. if (use_empty(LiveIns[i].second)) {
  274. // The livein has no uses. Drop it.
  275. //
  276. // It would be preferable to have isel avoid creating live-in
  277. // records for unused arguments in the first place, but it's
  278. // complicated by the debug info code for arguments.
  279. LiveIns.erase(LiveIns.begin() + i);
  280. --i; --e;
  281. } else {
  282. // Emit a copy.
  283. BuildMI(*EntryMBB, EntryMBB->begin(), DebugLoc(),
  284. TII.get(TargetOpcode::COPY), LiveIns[i].second)
  285. .addReg(LiveIns[i].first);
  286. // Add the register to the entry block live-in set.
  287. EntryMBB->addLiveIn(LiveIns[i].first);
  288. }
  289. } else {
  290. // Add the register to the entry block live-in set.
  291. EntryMBB->addLiveIn(LiveIns[i].first);
  292. }
  293. }
  294. #ifndef NDEBUG
  295. void MachineRegisterInfo::dumpUses(unsigned Reg) const {
  296. for (use_iterator I = use_begin(Reg), E = use_end(); I != E; ++I)
  297. I.getOperand().getParent()->dump();
  298. }
  299. #endif
  300. void MachineRegisterInfo::freezeReservedRegs(const MachineFunction &MF) {
  301. ReservedRegs = TRI->getReservedRegs(MF);
  302. assert(ReservedRegs.size() == TRI->getNumRegs() &&
  303. "Invalid ReservedRegs vector from target");
  304. }
  305. bool MachineRegisterInfo::isConstantPhysReg(unsigned PhysReg,
  306. const MachineFunction &MF) const {
  307. assert(TargetRegisterInfo::isPhysicalRegister(PhysReg));
  308. // Check if any overlapping register is modified, or allocatable so it may be
  309. // used later.
  310. for (MCRegAliasIterator AI(PhysReg, TRI, true); AI.isValid(); ++AI)
  311. if (!def_empty(*AI) || isAllocatable(*AI))
  312. return false;
  313. return true;
  314. }