MachineRegisterInfo.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674
  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/ADT/iterator_range.h"
  15. #include "llvm/CodeGen/LowLevelType.h"
  16. #include "llvm/CodeGen/MachineBasicBlock.h"
  17. #include "llvm/CodeGen/MachineFunction.h"
  18. #include "llvm/CodeGen/MachineInstr.h"
  19. #include "llvm/CodeGen/MachineInstrBuilder.h"
  20. #include "llvm/CodeGen/MachineOperand.h"
  21. #include "llvm/CodeGen/TargetInstrInfo.h"
  22. #include "llvm/CodeGen/TargetRegisterInfo.h"
  23. #include "llvm/CodeGen/TargetSubtargetInfo.h"
  24. #include "llvm/Config/llvm-config.h"
  25. #include "llvm/IR/Attributes.h"
  26. #include "llvm/IR/DebugLoc.h"
  27. #include "llvm/IR/Function.h"
  28. #include "llvm/MC/MCRegisterInfo.h"
  29. #include "llvm/Support/Casting.h"
  30. #include "llvm/Support/CommandLine.h"
  31. #include "llvm/Support/Compiler.h"
  32. #include "llvm/Support/ErrorHandling.h"
  33. #include "llvm/Support/raw_ostream.h"
  34. #include <cassert>
  35. using namespace llvm;
  36. static cl::opt<bool> EnableSubRegLiveness("enable-subreg-liveness", cl::Hidden,
  37. cl::init(true), cl::desc("Enable subregister liveness tracking."));
  38. // Pin the vtable to this file.
  39. void MachineRegisterInfo::Delegate::anchor() {}
  40. MachineRegisterInfo::MachineRegisterInfo(MachineFunction *MF)
  41. : MF(MF), TracksSubRegLiveness(MF->getSubtarget().enableSubRegLiveness() &&
  42. EnableSubRegLiveness),
  43. IsUpdatedCSRsInitialized(false) {
  44. unsigned NumRegs = getTargetRegisterInfo()->getNumRegs();
  45. VRegInfo.reserve(256);
  46. RegAllocHints.reserve(256);
  47. UsedPhysRegMask.resize(NumRegs);
  48. PhysRegUseDefLists.reset(new MachineOperand*[NumRegs]());
  49. }
  50. /// setRegClass - Set the register class of the specified virtual register.
  51. ///
  52. void
  53. MachineRegisterInfo::setRegClass(unsigned Reg, const TargetRegisterClass *RC) {
  54. assert(RC && RC->isAllocatable() && "Invalid RC for virtual register");
  55. VRegInfo[Reg].first = RC;
  56. }
  57. void MachineRegisterInfo::setRegBank(unsigned Reg,
  58. const RegisterBank &RegBank) {
  59. VRegInfo[Reg].first = &RegBank;
  60. }
  61. static const TargetRegisterClass *
  62. constrainRegClass(MachineRegisterInfo &MRI, unsigned Reg,
  63. const TargetRegisterClass *OldRC,
  64. const TargetRegisterClass *RC, unsigned MinNumRegs) {
  65. if (OldRC == RC)
  66. return RC;
  67. const TargetRegisterClass *NewRC =
  68. MRI.getTargetRegisterInfo()->getCommonSubClass(OldRC, RC);
  69. if (!NewRC || NewRC == OldRC)
  70. return NewRC;
  71. if (NewRC->getNumRegs() < MinNumRegs)
  72. return nullptr;
  73. MRI.setRegClass(Reg, NewRC);
  74. return NewRC;
  75. }
  76. const TargetRegisterClass *
  77. MachineRegisterInfo::constrainRegClass(unsigned Reg,
  78. const TargetRegisterClass *RC,
  79. unsigned MinNumRegs) {
  80. return ::constrainRegClass(*this, Reg, getRegClass(Reg), RC, MinNumRegs);
  81. }
  82. bool
  83. MachineRegisterInfo::constrainRegAttrs(unsigned Reg,
  84. unsigned ConstrainingReg,
  85. unsigned MinNumRegs) {
  86. auto const *OldRC = getRegClassOrNull(Reg);
  87. auto const *RC = getRegClassOrNull(ConstrainingReg);
  88. // A virtual register at any point must have either a low-level type
  89. // or a class assigned, but not both. The only exception is the internals of
  90. // GlobalISel's instruction selection pass, which is allowed to temporarily
  91. // introduce registers with types and classes both.
  92. assert((OldRC || getType(Reg).isValid()) && "Reg has neither class nor type");
  93. assert((!OldRC || !getType(Reg).isValid()) && "Reg has class and type both");
  94. assert((RC || getType(ConstrainingReg).isValid()) &&
  95. "ConstrainingReg has neither class nor type");
  96. assert((!RC || !getType(ConstrainingReg).isValid()) &&
  97. "ConstrainingReg has class and type both");
  98. if (OldRC && RC)
  99. return ::constrainRegClass(*this, Reg, OldRC, RC, MinNumRegs);
  100. // If one of the virtual registers is generic (used in generic machine
  101. // instructions, has a low-level type, doesn't have a class), and the other is
  102. // concrete (used in target specific instructions, doesn't have a low-level
  103. // type, has a class), we can not unify them.
  104. if (OldRC || RC)
  105. return false;
  106. // At this point, both registers are guaranteed to have a valid low-level
  107. // type, and they must agree.
  108. if (getType(Reg) != getType(ConstrainingReg))
  109. return false;
  110. auto const *OldRB = getRegBankOrNull(Reg);
  111. auto const *RB = getRegBankOrNull(ConstrainingReg);
  112. if (OldRB)
  113. return !RB || RB == OldRB;
  114. if (RB)
  115. setRegBank(Reg, *RB);
  116. return true;
  117. }
  118. bool
  119. MachineRegisterInfo::recomputeRegClass(unsigned Reg) {
  120. const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo();
  121. const TargetRegisterClass *OldRC = getRegClass(Reg);
  122. const TargetRegisterClass *NewRC =
  123. getTargetRegisterInfo()->getLargestLegalSuperClass(OldRC, *MF);
  124. // Stop early if there is no room to grow.
  125. if (NewRC == OldRC)
  126. return false;
  127. // Accumulate constraints from all uses.
  128. for (MachineOperand &MO : reg_nodbg_operands(Reg)) {
  129. // Apply the effect of the given operand to NewRC.
  130. MachineInstr *MI = MO.getParent();
  131. unsigned OpNo = &MO - &MI->getOperand(0);
  132. NewRC = MI->getRegClassConstraintEffect(OpNo, NewRC, TII,
  133. getTargetRegisterInfo());
  134. if (!NewRC || NewRC == OldRC)
  135. return false;
  136. }
  137. setRegClass(Reg, NewRC);
  138. return true;
  139. }
  140. unsigned MachineRegisterInfo::createIncompleteVirtualRegister(StringRef Name) {
  141. unsigned Reg = TargetRegisterInfo::index2VirtReg(getNumVirtRegs());
  142. VRegInfo.grow(Reg);
  143. RegAllocHints.grow(Reg);
  144. insertVRegByName(Name, Reg);
  145. return Reg;
  146. }
  147. /// createVirtualRegister - Create and return a new virtual register in the
  148. /// function with the specified register class.
  149. ///
  150. unsigned
  151. MachineRegisterInfo::createVirtualRegister(const TargetRegisterClass *RegClass,
  152. StringRef Name) {
  153. assert(RegClass && "Cannot create register without RegClass!");
  154. assert(RegClass->isAllocatable() &&
  155. "Virtual register RegClass must be allocatable.");
  156. // New virtual register number.
  157. unsigned Reg = createIncompleteVirtualRegister(Name);
  158. VRegInfo[Reg].first = RegClass;
  159. if (TheDelegate)
  160. TheDelegate->MRI_NoteNewVirtualRegister(Reg);
  161. return Reg;
  162. }
  163. LLT MachineRegisterInfo::getType(unsigned VReg) const {
  164. VRegToTypeMap::const_iterator TypeIt = getVRegToType().find(VReg);
  165. return TypeIt != getVRegToType().end() ? TypeIt->second : LLT{};
  166. }
  167. void MachineRegisterInfo::setType(unsigned VReg, LLT Ty) {
  168. // Check that VReg doesn't have a class.
  169. assert((getRegClassOrRegBank(VReg).isNull() ||
  170. !getRegClassOrRegBank(VReg).is<const TargetRegisterClass *>()) &&
  171. "Can't set the size of a non-generic virtual register");
  172. getVRegToType()[VReg] = Ty;
  173. }
  174. unsigned
  175. MachineRegisterInfo::createGenericVirtualRegister(LLT Ty, StringRef Name) {
  176. // New virtual register number.
  177. unsigned Reg = createIncompleteVirtualRegister(Name);
  178. // FIXME: Should we use a dummy register class?
  179. VRegInfo[Reg].first = static_cast<RegisterBank *>(nullptr);
  180. getVRegToType()[Reg] = Ty;
  181. if (TheDelegate)
  182. TheDelegate->MRI_NoteNewVirtualRegister(Reg);
  183. return Reg;
  184. }
  185. void MachineRegisterInfo::clearVirtRegTypes() {
  186. getVRegToType().clear();
  187. }
  188. /// clearVirtRegs - Remove all virtual registers (after physreg assignment).
  189. void MachineRegisterInfo::clearVirtRegs() {
  190. #ifndef NDEBUG
  191. for (unsigned i = 0, e = getNumVirtRegs(); i != e; ++i) {
  192. unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
  193. if (!VRegInfo[Reg].second)
  194. continue;
  195. verifyUseList(Reg);
  196. llvm_unreachable("Remaining virtual register operands");
  197. }
  198. #endif
  199. VRegInfo.clear();
  200. for (auto &I : LiveIns)
  201. I.second = 0;
  202. }
  203. void MachineRegisterInfo::verifyUseList(unsigned Reg) const {
  204. #ifndef NDEBUG
  205. bool Valid = true;
  206. for (MachineOperand &M : reg_operands(Reg)) {
  207. MachineOperand *MO = &M;
  208. MachineInstr *MI = MO->getParent();
  209. if (!MI) {
  210. errs() << printReg(Reg, getTargetRegisterInfo())
  211. << " use list MachineOperand " << MO
  212. << " has no parent instruction.\n";
  213. Valid = false;
  214. continue;
  215. }
  216. MachineOperand *MO0 = &MI->getOperand(0);
  217. unsigned NumOps = MI->getNumOperands();
  218. if (!(MO >= MO0 && MO < MO0+NumOps)) {
  219. errs() << printReg(Reg, getTargetRegisterInfo())
  220. << " use list MachineOperand " << MO
  221. << " doesn't belong to parent MI: " << *MI;
  222. Valid = false;
  223. }
  224. if (!MO->isReg()) {
  225. errs() << printReg(Reg, getTargetRegisterInfo())
  226. << " MachineOperand " << MO << ": " << *MO
  227. << " is not a register\n";
  228. Valid = false;
  229. }
  230. if (MO->getReg() != Reg) {
  231. errs() << printReg(Reg, getTargetRegisterInfo())
  232. << " use-list MachineOperand " << MO << ": "
  233. << *MO << " is the wrong register\n";
  234. Valid = false;
  235. }
  236. }
  237. assert(Valid && "Invalid use list");
  238. #endif
  239. }
  240. void MachineRegisterInfo::verifyUseLists() const {
  241. #ifndef NDEBUG
  242. for (unsigned i = 0, e = getNumVirtRegs(); i != e; ++i)
  243. verifyUseList(TargetRegisterInfo::index2VirtReg(i));
  244. for (unsigned i = 1, e = getTargetRegisterInfo()->getNumRegs(); i != e; ++i)
  245. verifyUseList(i);
  246. #endif
  247. }
  248. /// Add MO to the linked list of operands for its register.
  249. void MachineRegisterInfo::addRegOperandToUseList(MachineOperand *MO) {
  250. assert(!MO->isOnRegUseList() && "Already on list");
  251. MachineOperand *&HeadRef = getRegUseDefListHead(MO->getReg());
  252. MachineOperand *const Head = HeadRef;
  253. // Head points to the first list element.
  254. // Next is NULL on the last list element.
  255. // Prev pointers are circular, so Head->Prev == Last.
  256. // Head is NULL for an empty list.
  257. if (!Head) {
  258. MO->Contents.Reg.Prev = MO;
  259. MO->Contents.Reg.Next = nullptr;
  260. HeadRef = MO;
  261. return;
  262. }
  263. assert(MO->getReg() == Head->getReg() && "Different regs on the same list!");
  264. // Insert MO between Last and Head in the circular Prev chain.
  265. MachineOperand *Last = Head->Contents.Reg.Prev;
  266. assert(Last && "Inconsistent use list");
  267. assert(MO->getReg() == Last->getReg() && "Different regs on the same list!");
  268. Head->Contents.Reg.Prev = MO;
  269. MO->Contents.Reg.Prev = Last;
  270. // Def operands always precede uses. This allows def_iterator to stop early.
  271. // Insert def operands at the front, and use operands at the back.
  272. if (MO->isDef()) {
  273. // Insert def at the front.
  274. MO->Contents.Reg.Next = Head;
  275. HeadRef = MO;
  276. } else {
  277. // Insert use at the end.
  278. MO->Contents.Reg.Next = nullptr;
  279. Last->Contents.Reg.Next = MO;
  280. }
  281. }
  282. /// Remove MO from its use-def list.
  283. void MachineRegisterInfo::removeRegOperandFromUseList(MachineOperand *MO) {
  284. assert(MO->isOnRegUseList() && "Operand not on use list");
  285. MachineOperand *&HeadRef = getRegUseDefListHead(MO->getReg());
  286. MachineOperand *const Head = HeadRef;
  287. assert(Head && "List already empty");
  288. // Unlink this from the doubly linked list of operands.
  289. MachineOperand *Next = MO->Contents.Reg.Next;
  290. MachineOperand *Prev = MO->Contents.Reg.Prev;
  291. // Prev links are circular, next link is NULL instead of looping back to Head.
  292. if (MO == Head)
  293. HeadRef = Next;
  294. else
  295. Prev->Contents.Reg.Next = Next;
  296. (Next ? Next : Head)->Contents.Reg.Prev = Prev;
  297. MO->Contents.Reg.Prev = nullptr;
  298. MO->Contents.Reg.Next = nullptr;
  299. }
  300. /// Move NumOps operands from Src to Dst, updating use-def lists as needed.
  301. ///
  302. /// The Dst range is assumed to be uninitialized memory. (Or it may contain
  303. /// operands that won't be destroyed, which is OK because the MO destructor is
  304. /// trivial anyway).
  305. ///
  306. /// The Src and Dst ranges may overlap.
  307. void MachineRegisterInfo::moveOperands(MachineOperand *Dst,
  308. MachineOperand *Src,
  309. unsigned NumOps) {
  310. assert(Src != Dst && NumOps && "Noop moveOperands");
  311. // Copy backwards if Dst is within the Src range.
  312. int Stride = 1;
  313. if (Dst >= Src && Dst < Src + NumOps) {
  314. Stride = -1;
  315. Dst += NumOps - 1;
  316. Src += NumOps - 1;
  317. }
  318. // Copy one operand at a time.
  319. do {
  320. new (Dst) MachineOperand(*Src);
  321. // Dst takes Src's place in the use-def chain.
  322. if (Src->isReg()) {
  323. MachineOperand *&Head = getRegUseDefListHead(Src->getReg());
  324. MachineOperand *Prev = Src->Contents.Reg.Prev;
  325. MachineOperand *Next = Src->Contents.Reg.Next;
  326. assert(Head && "List empty, but operand is chained");
  327. assert(Prev && "Operand was not on use-def list");
  328. // Prev links are circular, next link is NULL instead of looping back to
  329. // Head.
  330. if (Src == Head)
  331. Head = Dst;
  332. else
  333. Prev->Contents.Reg.Next = Dst;
  334. // Update Prev pointer. This also works when Src was pointing to itself
  335. // in a 1-element list. In that case Head == Dst.
  336. (Next ? Next : Head)->Contents.Reg.Prev = Dst;
  337. }
  338. Dst += Stride;
  339. Src += Stride;
  340. } while (--NumOps);
  341. }
  342. /// replaceRegWith - Replace all instances of FromReg with ToReg in the
  343. /// machine function. This is like llvm-level X->replaceAllUsesWith(Y),
  344. /// except that it also changes any definitions of the register as well.
  345. /// If ToReg is a physical register we apply the sub register to obtain the
  346. /// final/proper physical register.
  347. void MachineRegisterInfo::replaceRegWith(unsigned FromReg, unsigned ToReg) {
  348. assert(FromReg != ToReg && "Cannot replace a reg with itself");
  349. const TargetRegisterInfo *TRI = getTargetRegisterInfo();
  350. // TODO: This could be more efficient by bulk changing the operands.
  351. for (reg_iterator I = reg_begin(FromReg), E = reg_end(); I != E; ) {
  352. MachineOperand &O = *I;
  353. ++I;
  354. if (TargetRegisterInfo::isPhysicalRegister(ToReg)) {
  355. O.substPhysReg(ToReg, *TRI);
  356. } else {
  357. O.setReg(ToReg);
  358. }
  359. }
  360. }
  361. /// getVRegDef - Return the machine instr that defines the specified virtual
  362. /// register or null if none is found. This assumes that the code is in SSA
  363. /// form, so there should only be one definition.
  364. MachineInstr *MachineRegisterInfo::getVRegDef(unsigned Reg) const {
  365. // Since we are in SSA form, we can use the first definition.
  366. def_instr_iterator I = def_instr_begin(Reg);
  367. assert((I.atEnd() || std::next(I) == def_instr_end()) &&
  368. "getVRegDef assumes a single definition or no definition");
  369. return !I.atEnd() ? &*I : nullptr;
  370. }
  371. /// getUniqueVRegDef - Return the unique machine instr that defines the
  372. /// specified virtual register or null if none is found. If there are
  373. /// multiple definitions or no definition, return null.
  374. MachineInstr *MachineRegisterInfo::getUniqueVRegDef(unsigned Reg) const {
  375. if (def_empty(Reg)) return nullptr;
  376. def_instr_iterator I = def_instr_begin(Reg);
  377. if (std::next(I) != def_instr_end())
  378. return nullptr;
  379. return &*I;
  380. }
  381. bool MachineRegisterInfo::hasOneNonDBGUse(unsigned RegNo) const {
  382. use_nodbg_iterator UI = use_nodbg_begin(RegNo);
  383. if (UI == use_nodbg_end())
  384. return false;
  385. return ++UI == use_nodbg_end();
  386. }
  387. /// clearKillFlags - Iterate over all the uses of the given register and
  388. /// clear the kill flag from the MachineOperand. This function is used by
  389. /// optimization passes which extend register lifetimes and need only
  390. /// preserve conservative kill flag information.
  391. void MachineRegisterInfo::clearKillFlags(unsigned Reg) const {
  392. for (MachineOperand &MO : use_operands(Reg))
  393. MO.setIsKill(false);
  394. }
  395. bool MachineRegisterInfo::isLiveIn(unsigned Reg) const {
  396. for (livein_iterator I = livein_begin(), E = livein_end(); I != E; ++I)
  397. if (I->first == Reg || I->second == Reg)
  398. return true;
  399. return false;
  400. }
  401. /// getLiveInPhysReg - If VReg is a live-in virtual register, return the
  402. /// corresponding live-in physical register.
  403. unsigned MachineRegisterInfo::getLiveInPhysReg(unsigned VReg) const {
  404. for (livein_iterator I = livein_begin(), E = livein_end(); I != E; ++I)
  405. if (I->second == VReg)
  406. return I->first;
  407. return 0;
  408. }
  409. /// getLiveInVirtReg - If PReg is a live-in physical register, return the
  410. /// corresponding live-in physical register.
  411. unsigned MachineRegisterInfo::getLiveInVirtReg(unsigned PReg) const {
  412. for (livein_iterator I = livein_begin(), E = livein_end(); I != E; ++I)
  413. if (I->first == PReg)
  414. return I->second;
  415. return 0;
  416. }
  417. /// EmitLiveInCopies - Emit copies to initialize livein virtual registers
  418. /// into the given entry block.
  419. void
  420. MachineRegisterInfo::EmitLiveInCopies(MachineBasicBlock *EntryMBB,
  421. const TargetRegisterInfo &TRI,
  422. const TargetInstrInfo &TII) {
  423. // Emit the copies into the top of the block.
  424. for (unsigned i = 0, e = LiveIns.size(); i != e; ++i)
  425. if (LiveIns[i].second) {
  426. if (use_nodbg_empty(LiveIns[i].second)) {
  427. // The livein has no non-dbg uses. Drop it.
  428. //
  429. // It would be preferable to have isel avoid creating live-in
  430. // records for unused arguments in the first place, but it's
  431. // complicated by the debug info code for arguments.
  432. LiveIns.erase(LiveIns.begin() + i);
  433. --i; --e;
  434. } else {
  435. // Emit a copy.
  436. BuildMI(*EntryMBB, EntryMBB->begin(), DebugLoc(),
  437. TII.get(TargetOpcode::COPY), LiveIns[i].second)
  438. .addReg(LiveIns[i].first);
  439. // Add the register to the entry block live-in set.
  440. EntryMBB->addLiveIn(LiveIns[i].first);
  441. }
  442. } else {
  443. // Add the register to the entry block live-in set.
  444. EntryMBB->addLiveIn(LiveIns[i].first);
  445. }
  446. }
  447. LaneBitmask MachineRegisterInfo::getMaxLaneMaskForVReg(unsigned Reg) const {
  448. // Lane masks are only defined for vregs.
  449. assert(TargetRegisterInfo::isVirtualRegister(Reg));
  450. const TargetRegisterClass &TRC = *getRegClass(Reg);
  451. return TRC.getLaneMask();
  452. }
  453. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  454. LLVM_DUMP_METHOD void MachineRegisterInfo::dumpUses(unsigned Reg) const {
  455. for (MachineInstr &I : use_instructions(Reg))
  456. I.dump();
  457. }
  458. #endif
  459. void MachineRegisterInfo::freezeReservedRegs(const MachineFunction &MF) {
  460. ReservedRegs = getTargetRegisterInfo()->getReservedRegs(MF);
  461. assert(ReservedRegs.size() == getTargetRegisterInfo()->getNumRegs() &&
  462. "Invalid ReservedRegs vector from target");
  463. }
  464. bool MachineRegisterInfo::isConstantPhysReg(unsigned PhysReg) const {
  465. assert(TargetRegisterInfo::isPhysicalRegister(PhysReg));
  466. const TargetRegisterInfo *TRI = getTargetRegisterInfo();
  467. if (TRI->isConstantPhysReg(PhysReg))
  468. return true;
  469. // Check if any overlapping register is modified, or allocatable so it may be
  470. // used later.
  471. for (MCRegAliasIterator AI(PhysReg, TRI, true);
  472. AI.isValid(); ++AI)
  473. if (!def_empty(*AI) || isAllocatable(*AI))
  474. return false;
  475. return true;
  476. }
  477. bool
  478. MachineRegisterInfo::isCallerPreservedOrConstPhysReg(unsigned PhysReg) const {
  479. const TargetRegisterInfo *TRI = getTargetRegisterInfo();
  480. return isConstantPhysReg(PhysReg) ||
  481. TRI->isCallerPreservedPhysReg(PhysReg, *MF);
  482. }
  483. /// markUsesInDebugValueAsUndef - Mark every DBG_VALUE referencing the
  484. /// specified register as undefined which causes the DBG_VALUE to be
  485. /// deleted during LiveDebugVariables analysis.
  486. void MachineRegisterInfo::markUsesInDebugValueAsUndef(unsigned Reg) const {
  487. // Mark any DBG_VALUE that uses Reg as undef (but don't delete it.)
  488. MachineRegisterInfo::use_instr_iterator nextI;
  489. for (use_instr_iterator I = use_instr_begin(Reg), E = use_instr_end();
  490. I != E; I = nextI) {
  491. nextI = std::next(I); // I is invalidated by the setReg
  492. MachineInstr *UseMI = &*I;
  493. if (UseMI->isDebugValue())
  494. UseMI->getOperand(0).setReg(0U);
  495. }
  496. }
  497. static const Function *getCalledFunction(const MachineInstr &MI) {
  498. for (const MachineOperand &MO : MI.operands()) {
  499. if (!MO.isGlobal())
  500. continue;
  501. const Function *Func = dyn_cast<Function>(MO.getGlobal());
  502. if (Func != nullptr)
  503. return Func;
  504. }
  505. return nullptr;
  506. }
  507. static bool isNoReturnDef(const MachineOperand &MO) {
  508. // Anything which is not a noreturn function is a real def.
  509. const MachineInstr &MI = *MO.getParent();
  510. if (!MI.isCall())
  511. return false;
  512. const MachineBasicBlock &MBB = *MI.getParent();
  513. if (!MBB.succ_empty())
  514. return false;
  515. const MachineFunction &MF = *MBB.getParent();
  516. // We need to keep correct unwind information even if the function will
  517. // not return, since the runtime may need it.
  518. if (MF.getFunction().hasFnAttribute(Attribute::UWTable))
  519. return false;
  520. const Function *Called = getCalledFunction(MI);
  521. return !(Called == nullptr || !Called->hasFnAttribute(Attribute::NoReturn) ||
  522. !Called->hasFnAttribute(Attribute::NoUnwind));
  523. }
  524. bool MachineRegisterInfo::isPhysRegModified(unsigned PhysReg,
  525. bool SkipNoReturnDef) const {
  526. if (UsedPhysRegMask.test(PhysReg))
  527. return true;
  528. const TargetRegisterInfo *TRI = getTargetRegisterInfo();
  529. for (MCRegAliasIterator AI(PhysReg, TRI, true); AI.isValid(); ++AI) {
  530. for (const MachineOperand &MO : make_range(def_begin(*AI), def_end())) {
  531. if (!SkipNoReturnDef && isNoReturnDef(MO))
  532. continue;
  533. return true;
  534. }
  535. }
  536. return false;
  537. }
  538. bool MachineRegisterInfo::isPhysRegUsed(unsigned PhysReg) const {
  539. if (UsedPhysRegMask.test(PhysReg))
  540. return true;
  541. const TargetRegisterInfo *TRI = getTargetRegisterInfo();
  542. for (MCRegAliasIterator AliasReg(PhysReg, TRI, true); AliasReg.isValid();
  543. ++AliasReg) {
  544. if (!reg_nodbg_empty(*AliasReg))
  545. return true;
  546. }
  547. return false;
  548. }
  549. void MachineRegisterInfo::disableCalleeSavedRegister(unsigned Reg) {
  550. const TargetRegisterInfo *TRI = getTargetRegisterInfo();
  551. assert(Reg && (Reg < TRI->getNumRegs()) &&
  552. "Trying to disable an invalid register");
  553. if (!IsUpdatedCSRsInitialized) {
  554. const MCPhysReg *CSR = TRI->getCalleeSavedRegs(MF);
  555. for (const MCPhysReg *I = CSR; *I; ++I)
  556. UpdatedCSRs.push_back(*I);
  557. // Zero value represents the end of the register list
  558. // (no more registers should be pushed).
  559. UpdatedCSRs.push_back(0);
  560. IsUpdatedCSRsInitialized = true;
  561. }
  562. // Remove the register (and its aliases from the list).
  563. for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI)
  564. UpdatedCSRs.erase(std::remove(UpdatedCSRs.begin(), UpdatedCSRs.end(), *AI),
  565. UpdatedCSRs.end());
  566. }
  567. const MCPhysReg *MachineRegisterInfo::getCalleeSavedRegs() const {
  568. if (IsUpdatedCSRsInitialized)
  569. return UpdatedCSRs.data();
  570. return getTargetRegisterInfo()->getCalleeSavedRegs(MF);
  571. }
  572. void MachineRegisterInfo::setCalleeSavedRegs(ArrayRef<MCPhysReg> CSRs) {
  573. if (IsUpdatedCSRsInitialized)
  574. UpdatedCSRs.clear();
  575. for (MCPhysReg Reg : CSRs)
  576. UpdatedCSRs.push_back(Reg);
  577. // Zero value represents the end of the register list
  578. // (no more registers should be pushed).
  579. UpdatedCSRs.push_back(0);
  580. IsUpdatedCSRsInitialized = true;
  581. }
  582. bool MachineRegisterInfo::isReservedRegUnit(unsigned Unit) const {
  583. const TargetRegisterInfo *TRI = getTargetRegisterInfo();
  584. for (MCRegUnitRootIterator Root(Unit, TRI); Root.isValid(); ++Root) {
  585. bool IsRootReserved = true;
  586. for (MCSuperRegIterator Super(*Root, TRI, /*IncludeSelf=*/true);
  587. Super.isValid(); ++Super) {
  588. unsigned Reg = *Super;
  589. if (!isReserved(Reg)) {
  590. IsRootReserved = false;
  591. break;
  592. }
  593. }
  594. if (IsRootReserved)
  595. return true;
  596. }
  597. return false;
  598. }