MachineRegisterInfo.cpp 22 KB

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