MachineRegisterInfo.cpp 14 KB

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