MachineRegisterInfo.cpp 13 KB

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