MachineRegisterInfo.h 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234
  1. //===- llvm/CodeGen/MachineRegisterInfo.h -----------------------*- C++ -*-===//
  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. // This file defines the MachineRegisterInfo class.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #ifndef LLVM_CODEGEN_MACHINEREGISTERINFO_H
  13. #define LLVM_CODEGEN_MACHINEREGISTERINFO_H
  14. #include "llvm/ADT/ArrayRef.h"
  15. #include "llvm/ADT/BitVector.h"
  16. #include "llvm/ADT/DenseMap.h"
  17. #include "llvm/ADT/IndexedMap.h"
  18. #include "llvm/ADT/PointerUnion.h"
  19. #include "llvm/ADT/SmallVector.h"
  20. #include "llvm/ADT/StringSet.h"
  21. #include "llvm/ADT/iterator_range.h"
  22. #include "llvm/CodeGen/GlobalISel/RegisterBank.h"
  23. #include "llvm/CodeGen/LowLevelType.h"
  24. #include "llvm/CodeGen/MachineBasicBlock.h"
  25. #include "llvm/CodeGen/MachineFunction.h"
  26. #include "llvm/CodeGen/MachineInstrBundle.h"
  27. #include "llvm/CodeGen/MachineOperand.h"
  28. #include "llvm/CodeGen/TargetRegisterInfo.h"
  29. #include "llvm/CodeGen/TargetSubtargetInfo.h"
  30. #include "llvm/MC/LaneBitmask.h"
  31. #include <cassert>
  32. #include <cstddef>
  33. #include <cstdint>
  34. #include <iterator>
  35. #include <memory>
  36. #include <utility>
  37. #include <vector>
  38. namespace llvm {
  39. class PSetIterator;
  40. /// Convenient type to represent either a register class or a register bank.
  41. using RegClassOrRegBank =
  42. PointerUnion<const TargetRegisterClass *, const RegisterBank *>;
  43. /// MachineRegisterInfo - Keep track of information for virtual and physical
  44. /// registers, including vreg register classes, use/def chains for registers,
  45. /// etc.
  46. class MachineRegisterInfo {
  47. public:
  48. class Delegate {
  49. virtual void anchor();
  50. public:
  51. virtual ~Delegate() = default;
  52. virtual void MRI_NoteNewVirtualRegister(Register Reg) = 0;
  53. };
  54. private:
  55. MachineFunction *MF;
  56. Delegate *TheDelegate = nullptr;
  57. /// True if subregister liveness is tracked.
  58. const bool TracksSubRegLiveness;
  59. /// VRegInfo - Information we keep for each virtual register.
  60. ///
  61. /// Each element in this list contains the register class of the vreg and the
  62. /// start of the use/def list for the register.
  63. IndexedMap<std::pair<RegClassOrRegBank, MachineOperand *>,
  64. VirtReg2IndexFunctor>
  65. VRegInfo;
  66. /// Map for recovering vreg name from vreg number.
  67. /// This map is used by the MIR Printer.
  68. IndexedMap<std::string, VirtReg2IndexFunctor> VReg2Name;
  69. /// StringSet that is used to unique vreg names.
  70. StringSet<> VRegNames;
  71. /// The flag is true upon \p UpdatedCSRs initialization
  72. /// and false otherwise.
  73. bool IsUpdatedCSRsInitialized;
  74. /// Contains the updated callee saved register list.
  75. /// As opposed to the static list defined in register info,
  76. /// all registers that were disabled are removed from the list.
  77. SmallVector<MCPhysReg, 16> UpdatedCSRs;
  78. /// RegAllocHints - This vector records register allocation hints for
  79. /// virtual registers. For each virtual register, it keeps a pair of hint
  80. /// type and hints vector making up the allocation hints. Only the first
  81. /// hint may be target specific, and in that case this is reflected by the
  82. /// first member of the pair being non-zero. If the hinted register is
  83. /// virtual, it means the allocator should prefer the physical register
  84. /// allocated to it if any.
  85. IndexedMap<std::pair<Register, SmallVector<Register, 4>>,
  86. VirtReg2IndexFunctor> RegAllocHints;
  87. /// PhysRegUseDefLists - This is an array of the head of the use/def list for
  88. /// physical registers.
  89. std::unique_ptr<MachineOperand *[]> PhysRegUseDefLists;
  90. /// getRegUseDefListHead - Return the head pointer for the register use/def
  91. /// list for the specified virtual or physical register.
  92. MachineOperand *&getRegUseDefListHead(Register RegNo) {
  93. if (RegNo.isVirtual())
  94. return VRegInfo[RegNo.id()].second;
  95. return PhysRegUseDefLists[RegNo.id()];
  96. }
  97. MachineOperand *getRegUseDefListHead(Register RegNo) const {
  98. if (RegNo.isVirtual())
  99. return VRegInfo[RegNo.id()].second;
  100. return PhysRegUseDefLists[RegNo.id()];
  101. }
  102. /// Get the next element in the use-def chain.
  103. static MachineOperand *getNextOperandForReg(const MachineOperand *MO) {
  104. assert(MO && MO->isReg() && "This is not a register operand!");
  105. return MO->Contents.Reg.Next;
  106. }
  107. /// UsedPhysRegMask - Additional used physregs including aliases.
  108. /// This bit vector represents all the registers clobbered by function calls.
  109. BitVector UsedPhysRegMask;
  110. /// ReservedRegs - This is a bit vector of reserved registers. The target
  111. /// may change its mind about which registers should be reserved. This
  112. /// vector is the frozen set of reserved registers when register allocation
  113. /// started.
  114. BitVector ReservedRegs;
  115. using VRegToTypeMap = IndexedMap<LLT, VirtReg2IndexFunctor>;
  116. /// Map generic virtual registers to their low-level type.
  117. VRegToTypeMap VRegToType;
  118. /// Keep track of the physical registers that are live in to the function.
  119. /// Live in values are typically arguments in registers. LiveIn values are
  120. /// allowed to have virtual registers associated with them, stored in the
  121. /// second element.
  122. std::vector<std::pair<MCRegister, Register>> LiveIns;
  123. public:
  124. explicit MachineRegisterInfo(MachineFunction *MF);
  125. MachineRegisterInfo(const MachineRegisterInfo &) = delete;
  126. MachineRegisterInfo &operator=(const MachineRegisterInfo &) = delete;
  127. const TargetRegisterInfo *getTargetRegisterInfo() const {
  128. return MF->getSubtarget().getRegisterInfo();
  129. }
  130. void resetDelegate(Delegate *delegate) {
  131. // Ensure another delegate does not take over unless the current
  132. // delegate first unattaches itself. If we ever need to multicast
  133. // notifications, we will need to change to using a list.
  134. assert(TheDelegate == delegate &&
  135. "Only the current delegate can perform reset!");
  136. TheDelegate = nullptr;
  137. }
  138. void setDelegate(Delegate *delegate) {
  139. assert(delegate && !TheDelegate &&
  140. "Attempted to set delegate to null, or to change it without "
  141. "first resetting it!");
  142. TheDelegate = delegate;
  143. }
  144. //===--------------------------------------------------------------------===//
  145. // Function State
  146. //===--------------------------------------------------------------------===//
  147. // isSSA - Returns true when the machine function is in SSA form. Early
  148. // passes require the machine function to be in SSA form where every virtual
  149. // register has a single defining instruction.
  150. //
  151. // The TwoAddressInstructionPass and PHIElimination passes take the machine
  152. // function out of SSA form when they introduce multiple defs per virtual
  153. // register.
  154. bool isSSA() const {
  155. return MF->getProperties().hasProperty(
  156. MachineFunctionProperties::Property::IsSSA);
  157. }
  158. // leaveSSA - Indicates that the machine function is no longer in SSA form.
  159. void leaveSSA() {
  160. MF->getProperties().reset(MachineFunctionProperties::Property::IsSSA);
  161. }
  162. /// tracksLiveness - Returns true when tracking register liveness accurately.
  163. /// (see MachineFUnctionProperties::Property description for details)
  164. bool tracksLiveness() const {
  165. return MF->getProperties().hasProperty(
  166. MachineFunctionProperties::Property::TracksLiveness);
  167. }
  168. /// invalidateLiveness - Indicates that register liveness is no longer being
  169. /// tracked accurately.
  170. ///
  171. /// This should be called by late passes that invalidate the liveness
  172. /// information.
  173. void invalidateLiveness() {
  174. MF->getProperties().reset(
  175. MachineFunctionProperties::Property::TracksLiveness);
  176. }
  177. /// Returns true if liveness for register class @p RC should be tracked at
  178. /// the subregister level.
  179. bool shouldTrackSubRegLiveness(const TargetRegisterClass &RC) const {
  180. return subRegLivenessEnabled() && RC.HasDisjunctSubRegs;
  181. }
  182. bool shouldTrackSubRegLiveness(Register VReg) const {
  183. assert(VReg.isVirtual() && "Must pass a VReg");
  184. return shouldTrackSubRegLiveness(*getRegClass(VReg));
  185. }
  186. bool subRegLivenessEnabled() const {
  187. return TracksSubRegLiveness;
  188. }
  189. //===--------------------------------------------------------------------===//
  190. // Register Info
  191. //===--------------------------------------------------------------------===//
  192. /// Returns true if the updated CSR list was initialized and false otherwise.
  193. bool isUpdatedCSRsInitialized() const { return IsUpdatedCSRsInitialized; }
  194. /// Disables the register from the list of CSRs.
  195. /// I.e. the register will not appear as part of the CSR mask.
  196. /// \see UpdatedCalleeSavedRegs.
  197. void disableCalleeSavedRegister(MCRegister Reg);
  198. /// Returns list of callee saved registers.
  199. /// The function returns the updated CSR list (after taking into account
  200. /// registers that are disabled from the CSR list).
  201. const MCPhysReg *getCalleeSavedRegs() const;
  202. /// Sets the updated Callee Saved Registers list.
  203. /// Notice that it will override ant previously disabled/saved CSRs.
  204. void setCalleeSavedRegs(ArrayRef<MCPhysReg> CSRs);
  205. // Strictly for use by MachineInstr.cpp.
  206. void addRegOperandToUseList(MachineOperand *MO);
  207. // Strictly for use by MachineInstr.cpp.
  208. void removeRegOperandFromUseList(MachineOperand *MO);
  209. // Strictly for use by MachineInstr.cpp.
  210. void moveOperands(MachineOperand *Dst, MachineOperand *Src, unsigned NumOps);
  211. /// Verify the sanity of the use list for Reg.
  212. void verifyUseList(Register Reg) const;
  213. /// Verify the use list of all registers.
  214. void verifyUseLists() const;
  215. /// reg_begin/reg_end - Provide iteration support to walk over all definitions
  216. /// and uses of a register within the MachineFunction that corresponds to this
  217. /// MachineRegisterInfo object.
  218. template<bool Uses, bool Defs, bool SkipDebug,
  219. bool ByOperand, bool ByInstr, bool ByBundle>
  220. class defusechain_iterator;
  221. template<bool Uses, bool Defs, bool SkipDebug,
  222. bool ByOperand, bool ByInstr, bool ByBundle>
  223. class defusechain_instr_iterator;
  224. // Make it a friend so it can access getNextOperandForReg().
  225. template<bool, bool, bool, bool, bool, bool>
  226. friend class defusechain_iterator;
  227. template<bool, bool, bool, bool, bool, bool>
  228. friend class defusechain_instr_iterator;
  229. /// reg_iterator/reg_begin/reg_end - Walk all defs and uses of the specified
  230. /// register.
  231. using reg_iterator =
  232. defusechain_iterator<true, true, false, true, false, false>;
  233. reg_iterator reg_begin(Register RegNo) const {
  234. return reg_iterator(getRegUseDefListHead(RegNo));
  235. }
  236. static reg_iterator reg_end() { return reg_iterator(nullptr); }
  237. inline iterator_range<reg_iterator> reg_operands(Register Reg) const {
  238. return make_range(reg_begin(Reg), reg_end());
  239. }
  240. /// reg_instr_iterator/reg_instr_begin/reg_instr_end - Walk all defs and uses
  241. /// of the specified register, stepping by MachineInstr.
  242. using reg_instr_iterator =
  243. defusechain_instr_iterator<true, true, false, false, true, false>;
  244. reg_instr_iterator reg_instr_begin(Register RegNo) const {
  245. return reg_instr_iterator(getRegUseDefListHead(RegNo));
  246. }
  247. static reg_instr_iterator reg_instr_end() {
  248. return reg_instr_iterator(nullptr);
  249. }
  250. inline iterator_range<reg_instr_iterator>
  251. reg_instructions(Register Reg) const {
  252. return make_range(reg_instr_begin(Reg), reg_instr_end());
  253. }
  254. /// reg_bundle_iterator/reg_bundle_begin/reg_bundle_end - Walk all defs and uses
  255. /// of the specified register, stepping by bundle.
  256. using reg_bundle_iterator =
  257. defusechain_instr_iterator<true, true, false, false, false, true>;
  258. reg_bundle_iterator reg_bundle_begin(Register RegNo) const {
  259. return reg_bundle_iterator(getRegUseDefListHead(RegNo));
  260. }
  261. static reg_bundle_iterator reg_bundle_end() {
  262. return reg_bundle_iterator(nullptr);
  263. }
  264. inline iterator_range<reg_bundle_iterator> reg_bundles(Register Reg) const {
  265. return make_range(reg_bundle_begin(Reg), reg_bundle_end());
  266. }
  267. /// reg_empty - Return true if there are no instructions using or defining the
  268. /// specified register (it may be live-in).
  269. bool reg_empty(Register RegNo) const { return reg_begin(RegNo) == reg_end(); }
  270. /// reg_nodbg_iterator/reg_nodbg_begin/reg_nodbg_end - Walk all defs and uses
  271. /// of the specified register, skipping those marked as Debug.
  272. using reg_nodbg_iterator =
  273. defusechain_iterator<true, true, true, true, false, false>;
  274. reg_nodbg_iterator reg_nodbg_begin(Register RegNo) const {
  275. return reg_nodbg_iterator(getRegUseDefListHead(RegNo));
  276. }
  277. static reg_nodbg_iterator reg_nodbg_end() {
  278. return reg_nodbg_iterator(nullptr);
  279. }
  280. inline iterator_range<reg_nodbg_iterator>
  281. reg_nodbg_operands(Register Reg) const {
  282. return make_range(reg_nodbg_begin(Reg), reg_nodbg_end());
  283. }
  284. /// reg_instr_nodbg_iterator/reg_instr_nodbg_begin/reg_instr_nodbg_end - Walk
  285. /// all defs and uses of the specified register, stepping by MachineInstr,
  286. /// skipping those marked as Debug.
  287. using reg_instr_nodbg_iterator =
  288. defusechain_instr_iterator<true, true, true, false, true, false>;
  289. reg_instr_nodbg_iterator reg_instr_nodbg_begin(Register RegNo) const {
  290. return reg_instr_nodbg_iterator(getRegUseDefListHead(RegNo));
  291. }
  292. static reg_instr_nodbg_iterator reg_instr_nodbg_end() {
  293. return reg_instr_nodbg_iterator(nullptr);
  294. }
  295. inline iterator_range<reg_instr_nodbg_iterator>
  296. reg_nodbg_instructions(Register Reg) const {
  297. return make_range(reg_instr_nodbg_begin(Reg), reg_instr_nodbg_end());
  298. }
  299. /// reg_bundle_nodbg_iterator/reg_bundle_nodbg_begin/reg_bundle_nodbg_end - Walk
  300. /// all defs and uses of the specified register, stepping by bundle,
  301. /// skipping those marked as Debug.
  302. using reg_bundle_nodbg_iterator =
  303. defusechain_instr_iterator<true, true, true, false, false, true>;
  304. reg_bundle_nodbg_iterator reg_bundle_nodbg_begin(Register RegNo) const {
  305. return reg_bundle_nodbg_iterator(getRegUseDefListHead(RegNo));
  306. }
  307. static reg_bundle_nodbg_iterator reg_bundle_nodbg_end() {
  308. return reg_bundle_nodbg_iterator(nullptr);
  309. }
  310. inline iterator_range<reg_bundle_nodbg_iterator>
  311. reg_nodbg_bundles(Register Reg) const {
  312. return make_range(reg_bundle_nodbg_begin(Reg), reg_bundle_nodbg_end());
  313. }
  314. /// reg_nodbg_empty - Return true if the only instructions using or defining
  315. /// Reg are Debug instructions.
  316. bool reg_nodbg_empty(Register RegNo) const {
  317. return reg_nodbg_begin(RegNo) == reg_nodbg_end();
  318. }
  319. /// def_iterator/def_begin/def_end - Walk all defs of the specified register.
  320. using def_iterator =
  321. defusechain_iterator<false, true, false, true, false, false>;
  322. def_iterator def_begin(Register RegNo) const {
  323. return def_iterator(getRegUseDefListHead(RegNo));
  324. }
  325. static def_iterator def_end() { return def_iterator(nullptr); }
  326. inline iterator_range<def_iterator> def_operands(Register Reg) const {
  327. return make_range(def_begin(Reg), def_end());
  328. }
  329. /// def_instr_iterator/def_instr_begin/def_instr_end - Walk all defs of the
  330. /// specified register, stepping by MachineInst.
  331. using def_instr_iterator =
  332. defusechain_instr_iterator<false, true, false, false, true, false>;
  333. def_instr_iterator def_instr_begin(Register RegNo) const {
  334. return def_instr_iterator(getRegUseDefListHead(RegNo));
  335. }
  336. static def_instr_iterator def_instr_end() {
  337. return def_instr_iterator(nullptr);
  338. }
  339. inline iterator_range<def_instr_iterator>
  340. def_instructions(Register Reg) const {
  341. return make_range(def_instr_begin(Reg), def_instr_end());
  342. }
  343. /// def_bundle_iterator/def_bundle_begin/def_bundle_end - Walk all defs of the
  344. /// specified register, stepping by bundle.
  345. using def_bundle_iterator =
  346. defusechain_instr_iterator<false, true, false, false, false, true>;
  347. def_bundle_iterator def_bundle_begin(Register RegNo) const {
  348. return def_bundle_iterator(getRegUseDefListHead(RegNo));
  349. }
  350. static def_bundle_iterator def_bundle_end() {
  351. return def_bundle_iterator(nullptr);
  352. }
  353. inline iterator_range<def_bundle_iterator> def_bundles(Register Reg) const {
  354. return make_range(def_bundle_begin(Reg), def_bundle_end());
  355. }
  356. /// def_empty - Return true if there are no instructions defining the
  357. /// specified register (it may be live-in).
  358. bool def_empty(Register RegNo) const { return def_begin(RegNo) == def_end(); }
  359. StringRef getVRegName(Register Reg) const {
  360. return VReg2Name.inBounds(Reg) ? StringRef(VReg2Name[Reg]) : "";
  361. }
  362. void insertVRegByName(StringRef Name, Register Reg) {
  363. assert((Name.empty() || VRegNames.find(Name) == VRegNames.end()) &&
  364. "Named VRegs Must be Unique.");
  365. if (!Name.empty()) {
  366. VRegNames.insert(Name);
  367. VReg2Name.grow(Reg);
  368. VReg2Name[Reg] = Name.str();
  369. }
  370. }
  371. /// Return true if there is exactly one operand defining the specified
  372. /// register.
  373. bool hasOneDef(Register RegNo) const {
  374. return hasSingleElement(def_operands(RegNo));
  375. }
  376. /// Returns the defining operand if there is exactly one operand defining the
  377. /// specified register, otherwise nullptr.
  378. MachineOperand *getOneDef(Register Reg) const {
  379. def_iterator DI = def_begin(Reg);
  380. if (DI == def_end()) // No defs.
  381. return nullptr;
  382. def_iterator OneDef = DI;
  383. if (++DI == def_end())
  384. return &*OneDef;
  385. return nullptr; // Multiple defs.
  386. }
  387. /// use_iterator/use_begin/use_end - Walk all uses of the specified register.
  388. using use_iterator =
  389. defusechain_iterator<true, false, false, true, false, false>;
  390. use_iterator use_begin(Register RegNo) const {
  391. return use_iterator(getRegUseDefListHead(RegNo));
  392. }
  393. static use_iterator use_end() { return use_iterator(nullptr); }
  394. inline iterator_range<use_iterator> use_operands(Register Reg) const {
  395. return make_range(use_begin(Reg), use_end());
  396. }
  397. /// use_instr_iterator/use_instr_begin/use_instr_end - Walk all uses of the
  398. /// specified register, stepping by MachineInstr.
  399. using use_instr_iterator =
  400. defusechain_instr_iterator<true, false, false, false, true, false>;
  401. use_instr_iterator use_instr_begin(Register RegNo) const {
  402. return use_instr_iterator(getRegUseDefListHead(RegNo));
  403. }
  404. static use_instr_iterator use_instr_end() {
  405. return use_instr_iterator(nullptr);
  406. }
  407. inline iterator_range<use_instr_iterator>
  408. use_instructions(Register Reg) const {
  409. return make_range(use_instr_begin(Reg), use_instr_end());
  410. }
  411. /// use_bundle_iterator/use_bundle_begin/use_bundle_end - Walk all uses of the
  412. /// specified register, stepping by bundle.
  413. using use_bundle_iterator =
  414. defusechain_instr_iterator<true, false, false, false, false, true>;
  415. use_bundle_iterator use_bundle_begin(Register RegNo) const {
  416. return use_bundle_iterator(getRegUseDefListHead(RegNo));
  417. }
  418. static use_bundle_iterator use_bundle_end() {
  419. return use_bundle_iterator(nullptr);
  420. }
  421. inline iterator_range<use_bundle_iterator> use_bundles(Register Reg) const {
  422. return make_range(use_bundle_begin(Reg), use_bundle_end());
  423. }
  424. /// use_empty - Return true if there are no instructions using the specified
  425. /// register.
  426. bool use_empty(Register RegNo) const { return use_begin(RegNo) == use_end(); }
  427. /// hasOneUse - Return true if there is exactly one instruction using the
  428. /// specified register.
  429. bool hasOneUse(Register RegNo) const {
  430. return hasSingleElement(use_operands(RegNo));
  431. }
  432. /// use_nodbg_iterator/use_nodbg_begin/use_nodbg_end - Walk all uses of the
  433. /// specified register, skipping those marked as Debug.
  434. using use_nodbg_iterator =
  435. defusechain_iterator<true, false, true, true, false, false>;
  436. use_nodbg_iterator use_nodbg_begin(Register RegNo) const {
  437. return use_nodbg_iterator(getRegUseDefListHead(RegNo));
  438. }
  439. static use_nodbg_iterator use_nodbg_end() {
  440. return use_nodbg_iterator(nullptr);
  441. }
  442. inline iterator_range<use_nodbg_iterator>
  443. use_nodbg_operands(Register Reg) const {
  444. return make_range(use_nodbg_begin(Reg), use_nodbg_end());
  445. }
  446. /// use_instr_nodbg_iterator/use_instr_nodbg_begin/use_instr_nodbg_end - Walk
  447. /// all uses of the specified register, stepping by MachineInstr, skipping
  448. /// those marked as Debug.
  449. using use_instr_nodbg_iterator =
  450. defusechain_instr_iterator<true, false, true, false, true, false>;
  451. use_instr_nodbg_iterator use_instr_nodbg_begin(Register RegNo) const {
  452. return use_instr_nodbg_iterator(getRegUseDefListHead(RegNo));
  453. }
  454. static use_instr_nodbg_iterator use_instr_nodbg_end() {
  455. return use_instr_nodbg_iterator(nullptr);
  456. }
  457. inline iterator_range<use_instr_nodbg_iterator>
  458. use_nodbg_instructions(Register Reg) const {
  459. return make_range(use_instr_nodbg_begin(Reg), use_instr_nodbg_end());
  460. }
  461. /// use_bundle_nodbg_iterator/use_bundle_nodbg_begin/use_bundle_nodbg_end - Walk
  462. /// all uses of the specified register, stepping by bundle, skipping
  463. /// those marked as Debug.
  464. using use_bundle_nodbg_iterator =
  465. defusechain_instr_iterator<true, false, true, false, false, true>;
  466. use_bundle_nodbg_iterator use_bundle_nodbg_begin(Register RegNo) const {
  467. return use_bundle_nodbg_iterator(getRegUseDefListHead(RegNo));
  468. }
  469. static use_bundle_nodbg_iterator use_bundle_nodbg_end() {
  470. return use_bundle_nodbg_iterator(nullptr);
  471. }
  472. inline iterator_range<use_bundle_nodbg_iterator>
  473. use_nodbg_bundles(Register Reg) const {
  474. return make_range(use_bundle_nodbg_begin(Reg), use_bundle_nodbg_end());
  475. }
  476. /// use_nodbg_empty - Return true if there are no non-Debug instructions
  477. /// using the specified register.
  478. bool use_nodbg_empty(Register RegNo) const {
  479. return use_nodbg_begin(RegNo) == use_nodbg_end();
  480. }
  481. /// hasOneNonDBGUse - Return true if there is exactly one non-Debug
  482. /// use of the specified register.
  483. bool hasOneNonDBGUse(Register RegNo) const;
  484. /// hasOneNonDBGUse - Return true if there is exactly one non-Debug
  485. /// instruction using the specified register. Said instruction may have
  486. /// multiple uses.
  487. bool hasOneNonDBGUser(Register RegNo) const;
  488. /// replaceRegWith - Replace all instances of FromReg with ToReg in the
  489. /// machine function. This is like llvm-level X->replaceAllUsesWith(Y),
  490. /// except that it also changes any definitions of the register as well.
  491. ///
  492. /// Note that it is usually necessary to first constrain ToReg's register
  493. /// class and register bank to match the FromReg constraints using one of the
  494. /// methods:
  495. ///
  496. /// constrainRegClass(ToReg, getRegClass(FromReg))
  497. /// constrainRegAttrs(ToReg, FromReg)
  498. /// RegisterBankInfo::constrainGenericRegister(ToReg,
  499. /// *MRI.getRegClass(FromReg), MRI)
  500. ///
  501. /// These functions will return a falsy result if the virtual registers have
  502. /// incompatible constraints.
  503. ///
  504. /// Note that if ToReg is a physical register the function will replace and
  505. /// apply sub registers to ToReg in order to obtain a final/proper physical
  506. /// register.
  507. void replaceRegWith(Register FromReg, Register ToReg);
  508. /// getVRegDef - Return the machine instr that defines the specified virtual
  509. /// register or null if none is found. This assumes that the code is in SSA
  510. /// form, so there should only be one definition.
  511. MachineInstr *getVRegDef(Register Reg) const;
  512. /// getUniqueVRegDef - Return the unique machine instr that defines the
  513. /// specified virtual register or null if none is found. If there are
  514. /// multiple definitions or no definition, return null.
  515. MachineInstr *getUniqueVRegDef(Register Reg) const;
  516. /// clearKillFlags - Iterate over all the uses of the given register and
  517. /// clear the kill flag from the MachineOperand. This function is used by
  518. /// optimization passes which extend register lifetimes and need only
  519. /// preserve conservative kill flag information.
  520. void clearKillFlags(Register Reg) const;
  521. void dumpUses(Register RegNo) const;
  522. /// Returns true if PhysReg is unallocatable and constant throughout the
  523. /// function. Writing to a constant register has no effect.
  524. bool isConstantPhysReg(MCRegister PhysReg) const;
  525. /// Get an iterator over the pressure sets affected by the given physical or
  526. /// virtual register. If RegUnit is physical, it must be a register unit (from
  527. /// MCRegUnitIterator).
  528. PSetIterator getPressureSets(Register RegUnit) const;
  529. //===--------------------------------------------------------------------===//
  530. // Virtual Register Info
  531. //===--------------------------------------------------------------------===//
  532. /// Return the register class of the specified virtual register.
  533. /// This shouldn't be used directly unless \p Reg has a register class.
  534. /// \see getRegClassOrNull when this might happen.
  535. const TargetRegisterClass *getRegClass(Register Reg) const {
  536. assert(VRegInfo[Reg.id()].first.is<const TargetRegisterClass *>() &&
  537. "Register class not set, wrong accessor");
  538. return VRegInfo[Reg.id()].first.get<const TargetRegisterClass *>();
  539. }
  540. /// Return the register class of \p Reg, or null if Reg has not been assigned
  541. /// a register class yet.
  542. ///
  543. /// \note A null register class can only happen when these two
  544. /// conditions are met:
  545. /// 1. Generic virtual registers are created.
  546. /// 2. The machine function has not completely been through the
  547. /// instruction selection process.
  548. /// None of this condition is possible without GlobalISel for now.
  549. /// In other words, if GlobalISel is not used or if the query happens after
  550. /// the select pass, using getRegClass is safe.
  551. const TargetRegisterClass *getRegClassOrNull(Register Reg) const {
  552. const RegClassOrRegBank &Val = VRegInfo[Reg].first;
  553. return Val.dyn_cast<const TargetRegisterClass *>();
  554. }
  555. /// Return the register bank of \p Reg, or null if Reg has not been assigned
  556. /// a register bank or has been assigned a register class.
  557. /// \note It is possible to get the register bank from the register class via
  558. /// RegisterBankInfo::getRegBankFromRegClass.
  559. const RegisterBank *getRegBankOrNull(Register Reg) const {
  560. const RegClassOrRegBank &Val = VRegInfo[Reg].first;
  561. return Val.dyn_cast<const RegisterBank *>();
  562. }
  563. /// Return the register bank or register class of \p Reg.
  564. /// \note Before the register bank gets assigned (i.e., before the
  565. /// RegBankSelect pass) \p Reg may not have either.
  566. const RegClassOrRegBank &getRegClassOrRegBank(Register Reg) const {
  567. return VRegInfo[Reg].first;
  568. }
  569. /// setRegClass - Set the register class of the specified virtual register.
  570. void setRegClass(Register Reg, const TargetRegisterClass *RC);
  571. /// Set the register bank to \p RegBank for \p Reg.
  572. void setRegBank(Register Reg, const RegisterBank &RegBank);
  573. void setRegClassOrRegBank(Register Reg,
  574. const RegClassOrRegBank &RCOrRB){
  575. VRegInfo[Reg].first = RCOrRB;
  576. }
  577. /// constrainRegClass - Constrain the register class of the specified virtual
  578. /// register to be a common subclass of RC and the current register class,
  579. /// but only if the new class has at least MinNumRegs registers. Return the
  580. /// new register class, or NULL if no such class exists.
  581. /// This should only be used when the constraint is known to be trivial, like
  582. /// GR32 -> GR32_NOSP. Beware of increasing register pressure.
  583. ///
  584. /// \note Assumes that the register has a register class assigned.
  585. /// Use RegisterBankInfo::constrainGenericRegister in GlobalISel's
  586. /// InstructionSelect pass and constrainRegAttrs in every other pass,
  587. /// including non-select passes of GlobalISel, instead.
  588. const TargetRegisterClass *constrainRegClass(Register Reg,
  589. const TargetRegisterClass *RC,
  590. unsigned MinNumRegs = 0);
  591. /// Constrain the register class or the register bank of the virtual register
  592. /// \p Reg (and low-level type) to be a common subclass or a common bank of
  593. /// both registers provided respectively (and a common low-level type). Do
  594. /// nothing if any of the attributes (classes, banks, or low-level types) of
  595. /// the registers are deemed incompatible, or if the resulting register will
  596. /// have a class smaller than before and of size less than \p MinNumRegs.
  597. /// Return true if such register attributes exist, false otherwise.
  598. ///
  599. /// \note Use this method instead of constrainRegClass and
  600. /// RegisterBankInfo::constrainGenericRegister everywhere but SelectionDAG
  601. /// ISel / FastISel and GlobalISel's InstructionSelect pass respectively.
  602. bool constrainRegAttrs(Register Reg, Register ConstrainingReg,
  603. unsigned MinNumRegs = 0);
  604. /// recomputeRegClass - Try to find a legal super-class of Reg's register
  605. /// class that still satisfies the constraints from the instructions using
  606. /// Reg. Returns true if Reg was upgraded.
  607. ///
  608. /// This method can be used after constraints have been removed from a
  609. /// virtual register, for example after removing instructions or splitting
  610. /// the live range.
  611. bool recomputeRegClass(Register Reg);
  612. /// createVirtualRegister - Create and return a new virtual register in the
  613. /// function with the specified register class.
  614. Register createVirtualRegister(const TargetRegisterClass *RegClass,
  615. StringRef Name = "");
  616. /// Create and return a new virtual register in the function with the same
  617. /// attributes as the given register.
  618. Register cloneVirtualRegister(Register VReg, StringRef Name = "");
  619. /// Get the low-level type of \p Reg or LLT{} if Reg is not a generic
  620. /// (target independent) virtual register.
  621. LLT getType(Register Reg) const {
  622. if (Register::isVirtualRegister(Reg) && VRegToType.inBounds(Reg))
  623. return VRegToType[Reg];
  624. return LLT{};
  625. }
  626. /// Set the low-level type of \p VReg to \p Ty.
  627. void setType(Register VReg, LLT Ty);
  628. /// Create and return a new generic virtual register with low-level
  629. /// type \p Ty.
  630. Register createGenericVirtualRegister(LLT Ty, StringRef Name = "");
  631. /// Remove all types associated to virtual registers (after instruction
  632. /// selection and constraining of all generic virtual registers).
  633. void clearVirtRegTypes();
  634. /// Creates a new virtual register that has no register class, register bank
  635. /// or size assigned yet. This is only allowed to be used
  636. /// temporarily while constructing machine instructions. Most operations are
  637. /// undefined on an incomplete register until one of setRegClass(),
  638. /// setRegBank() or setSize() has been called on it.
  639. Register createIncompleteVirtualRegister(StringRef Name = "");
  640. /// getNumVirtRegs - Return the number of virtual registers created.
  641. unsigned getNumVirtRegs() const { return VRegInfo.size(); }
  642. /// clearVirtRegs - Remove all virtual registers (after physreg assignment).
  643. void clearVirtRegs();
  644. /// setRegAllocationHint - Specify a register allocation hint for the
  645. /// specified virtual register. This is typically used by target, and in case
  646. /// of an earlier hint it will be overwritten.
  647. void setRegAllocationHint(Register VReg, unsigned Type, Register PrefReg) {
  648. assert(VReg.isVirtual());
  649. RegAllocHints[VReg].first = Type;
  650. RegAllocHints[VReg].second.clear();
  651. RegAllocHints[VReg].second.push_back(PrefReg);
  652. }
  653. /// addRegAllocationHint - Add a register allocation hint to the hints
  654. /// vector for VReg.
  655. void addRegAllocationHint(Register VReg, Register PrefReg) {
  656. assert(Register::isVirtualRegister(VReg));
  657. RegAllocHints[VReg].second.push_back(PrefReg);
  658. }
  659. /// Specify the preferred (target independent) register allocation hint for
  660. /// the specified virtual register.
  661. void setSimpleHint(Register VReg, Register PrefReg) {
  662. setRegAllocationHint(VReg, /*Type=*/0, PrefReg);
  663. }
  664. void clearSimpleHint(Register VReg) {
  665. assert (!RegAllocHints[VReg].first &&
  666. "Expected to clear a non-target hint!");
  667. RegAllocHints[VReg].second.clear();
  668. }
  669. /// getRegAllocationHint - Return the register allocation hint for the
  670. /// specified virtual register. If there are many hints, this returns the
  671. /// one with the greatest weight.
  672. std::pair<Register, Register>
  673. getRegAllocationHint(Register VReg) const {
  674. assert(VReg.isVirtual());
  675. Register BestHint = (RegAllocHints[VReg.id()].second.size() ?
  676. RegAllocHints[VReg.id()].second[0] : Register());
  677. return std::pair<Register, Register>(RegAllocHints[VReg.id()].first,
  678. BestHint);
  679. }
  680. /// getSimpleHint - same as getRegAllocationHint except it will only return
  681. /// a target independent hint.
  682. Register getSimpleHint(Register VReg) const {
  683. assert(VReg.isVirtual());
  684. std::pair<Register, Register> Hint = getRegAllocationHint(VReg);
  685. return Hint.first ? Register() : Hint.second;
  686. }
  687. /// getRegAllocationHints - Return a reference to the vector of all
  688. /// register allocation hints for VReg.
  689. const std::pair<Register, SmallVector<Register, 4>>
  690. &getRegAllocationHints(Register VReg) const {
  691. assert(VReg.isVirtual());
  692. return RegAllocHints[VReg];
  693. }
  694. /// markUsesInDebugValueAsUndef - Mark every DBG_VALUE referencing the
  695. /// specified register as undefined which causes the DBG_VALUE to be
  696. /// deleted during LiveDebugVariables analysis.
  697. void markUsesInDebugValueAsUndef(Register Reg) const;
  698. /// updateDbgUsersToReg - Update a collection of DBG_VALUE instructions
  699. /// to refer to the designated register.
  700. void updateDbgUsersToReg(MCRegister OldReg, MCRegister NewReg,
  701. ArrayRef<MachineInstr *> Users) const {
  702. SmallSet<MCRegister, 4> OldRegUnits;
  703. for (MCRegUnitIterator RUI(OldReg, getTargetRegisterInfo()); RUI.isValid();
  704. ++RUI)
  705. OldRegUnits.insert(*RUI);
  706. for (MachineInstr *MI : Users) {
  707. assert(MI->isDebugValue());
  708. for (auto &Op : MI->debug_operands()) {
  709. if (Op.isReg()) {
  710. for (MCRegUnitIterator RUI(OldReg, getTargetRegisterInfo());
  711. RUI.isValid(); ++RUI) {
  712. if (OldRegUnits.contains(*RUI)) {
  713. Op.setReg(NewReg);
  714. break;
  715. }
  716. }
  717. }
  718. }
  719. assert(MI->hasDebugOperandForReg(NewReg) &&
  720. "Expected debug value to have some overlap with OldReg");
  721. }
  722. }
  723. /// Return true if the specified register is modified in this function.
  724. /// This checks that no defining machine operands exist for the register or
  725. /// any of its aliases. Definitions found on functions marked noreturn are
  726. /// ignored, to consider them pass 'true' for optional parameter
  727. /// SkipNoReturnDef. The register is also considered modified when it is set
  728. /// in the UsedPhysRegMask.
  729. bool isPhysRegModified(MCRegister PhysReg, bool SkipNoReturnDef = false) const;
  730. /// Return true if the specified register is modified or read in this
  731. /// function. This checks that no machine operands exist for the register or
  732. /// any of its aliases. The register is also considered used when it is set
  733. /// in the UsedPhysRegMask.
  734. bool isPhysRegUsed(MCRegister PhysReg) const;
  735. /// addPhysRegsUsedFromRegMask - Mark any registers not in RegMask as used.
  736. /// This corresponds to the bit mask attached to register mask operands.
  737. void addPhysRegsUsedFromRegMask(const uint32_t *RegMask) {
  738. UsedPhysRegMask.setBitsNotInMask(RegMask);
  739. }
  740. const BitVector &getUsedPhysRegsMask() const { return UsedPhysRegMask; }
  741. //===--------------------------------------------------------------------===//
  742. // Reserved Register Info
  743. //===--------------------------------------------------------------------===//
  744. //
  745. // The set of reserved registers must be invariant during register
  746. // allocation. For example, the target cannot suddenly decide it needs a
  747. // frame pointer when the register allocator has already used the frame
  748. // pointer register for something else.
  749. //
  750. // These methods can be used by target hooks like hasFP() to avoid changing
  751. // the reserved register set during register allocation.
  752. /// freezeReservedRegs - Called by the register allocator to freeze the set
  753. /// of reserved registers before allocation begins.
  754. void freezeReservedRegs(const MachineFunction&);
  755. /// reservedRegsFrozen - Returns true after freezeReservedRegs() was called
  756. /// to ensure the set of reserved registers stays constant.
  757. bool reservedRegsFrozen() const {
  758. return !ReservedRegs.empty();
  759. }
  760. /// canReserveReg - Returns true if PhysReg can be used as a reserved
  761. /// register. Any register can be reserved before freezeReservedRegs() is
  762. /// called.
  763. bool canReserveReg(MCRegister PhysReg) const {
  764. return !reservedRegsFrozen() || ReservedRegs.test(PhysReg);
  765. }
  766. /// getReservedRegs - Returns a reference to the frozen set of reserved
  767. /// registers. This method should always be preferred to calling
  768. /// TRI::getReservedRegs() when possible.
  769. const BitVector &getReservedRegs() const {
  770. assert(reservedRegsFrozen() &&
  771. "Reserved registers haven't been frozen yet. "
  772. "Use TRI::getReservedRegs().");
  773. return ReservedRegs;
  774. }
  775. /// isReserved - Returns true when PhysReg is a reserved register.
  776. ///
  777. /// Reserved registers may belong to an allocatable register class, but the
  778. /// target has explicitly requested that they are not used.
  779. bool isReserved(MCRegister PhysReg) const {
  780. return getReservedRegs().test(PhysReg.id());
  781. }
  782. /// Returns true when the given register unit is considered reserved.
  783. ///
  784. /// Register units are considered reserved when for at least one of their
  785. /// root registers, the root register and all super registers are reserved.
  786. /// This currently iterates the register hierarchy and may be slower than
  787. /// expected.
  788. bool isReservedRegUnit(unsigned Unit) const;
  789. /// isAllocatable - Returns true when PhysReg belongs to an allocatable
  790. /// register class and it hasn't been reserved.
  791. ///
  792. /// Allocatable registers may show up in the allocation order of some virtual
  793. /// register, so a register allocator needs to track its liveness and
  794. /// availability.
  795. bool isAllocatable(MCRegister PhysReg) const {
  796. return getTargetRegisterInfo()->isInAllocatableClass(PhysReg) &&
  797. !isReserved(PhysReg);
  798. }
  799. //===--------------------------------------------------------------------===//
  800. // LiveIn Management
  801. //===--------------------------------------------------------------------===//
  802. /// addLiveIn - Add the specified register as a live-in. Note that it
  803. /// is an error to add the same register to the same set more than once.
  804. void addLiveIn(MCRegister Reg, Register vreg = Register()) {
  805. LiveIns.push_back(std::make_pair(Reg, vreg));
  806. }
  807. // Iteration support for the live-ins set. It's kept in sorted order
  808. // by register number.
  809. using livein_iterator =
  810. std::vector<std::pair<MCRegister,Register>>::const_iterator;
  811. livein_iterator livein_begin() const { return LiveIns.begin(); }
  812. livein_iterator livein_end() const { return LiveIns.end(); }
  813. bool livein_empty() const { return LiveIns.empty(); }
  814. ArrayRef<std::pair<MCRegister, Register>> liveins() const {
  815. return LiveIns;
  816. }
  817. bool isLiveIn(Register Reg) const;
  818. /// getLiveInPhysReg - If VReg is a live-in virtual register, return the
  819. /// corresponding live-in physical register.
  820. MCRegister getLiveInPhysReg(Register VReg) const;
  821. /// getLiveInVirtReg - If PReg is a live-in physical register, return the
  822. /// corresponding live-in physical register.
  823. Register getLiveInVirtReg(MCRegister PReg) const;
  824. /// EmitLiveInCopies - Emit copies to initialize livein virtual registers
  825. /// into the given entry block.
  826. void EmitLiveInCopies(MachineBasicBlock *EntryMBB,
  827. const TargetRegisterInfo &TRI,
  828. const TargetInstrInfo &TII);
  829. /// Returns a mask covering all bits that can appear in lane masks of
  830. /// subregisters of the virtual register @p Reg.
  831. LaneBitmask getMaxLaneMaskForVReg(Register Reg) const;
  832. /// defusechain_iterator - This class provides iterator support for machine
  833. /// operands in the function that use or define a specific register. If
  834. /// ReturnUses is true it returns uses of registers, if ReturnDefs is true it
  835. /// returns defs. If neither are true then you are silly and it always
  836. /// returns end(). If SkipDebug is true it skips uses marked Debug
  837. /// when incrementing.
  838. template <bool ReturnUses, bool ReturnDefs, bool SkipDebug, bool ByOperand,
  839. bool ByInstr, bool ByBundle>
  840. class defusechain_iterator {
  841. friend class MachineRegisterInfo;
  842. public:
  843. using iterator_category = std::forward_iterator_tag;
  844. using value_type = MachineOperand;
  845. using difference_type = std::ptrdiff_t;
  846. using pointer = value_type *;
  847. using reference = value_type &;
  848. private:
  849. MachineOperand *Op = nullptr;
  850. explicit defusechain_iterator(MachineOperand *op) : Op(op) {
  851. // If the first node isn't one we're interested in, advance to one that
  852. // we are interested in.
  853. if (op) {
  854. if ((!ReturnUses && op->isUse()) ||
  855. (!ReturnDefs && op->isDef()) ||
  856. (SkipDebug && op->isDebug()))
  857. advance();
  858. }
  859. }
  860. void advance() {
  861. assert(Op && "Cannot increment end iterator!");
  862. Op = getNextOperandForReg(Op);
  863. // All defs come before the uses, so stop def_iterator early.
  864. if (!ReturnUses) {
  865. if (Op) {
  866. if (Op->isUse())
  867. Op = nullptr;
  868. else
  869. assert(!Op->isDebug() && "Can't have debug defs");
  870. }
  871. } else {
  872. // If this is an operand we don't care about, skip it.
  873. while (Op && ((!ReturnDefs && Op->isDef()) ||
  874. (SkipDebug && Op->isDebug())))
  875. Op = getNextOperandForReg(Op);
  876. }
  877. }
  878. public:
  879. defusechain_iterator() = default;
  880. bool operator==(const defusechain_iterator &x) const {
  881. return Op == x.Op;
  882. }
  883. bool operator!=(const defusechain_iterator &x) const {
  884. return !operator==(x);
  885. }
  886. /// atEnd - return true if this iterator is equal to reg_end() on the value.
  887. bool atEnd() const { return Op == nullptr; }
  888. // Iterator traversal: forward iteration only
  889. defusechain_iterator &operator++() { // Preincrement
  890. assert(Op && "Cannot increment end iterator!");
  891. if (ByOperand)
  892. advance();
  893. else if (ByInstr) {
  894. MachineInstr *P = Op->getParent();
  895. do {
  896. advance();
  897. } while (Op && Op->getParent() == P);
  898. } else if (ByBundle) {
  899. MachineBasicBlock::instr_iterator P =
  900. getBundleStart(Op->getParent()->getIterator());
  901. do {
  902. advance();
  903. } while (Op && getBundleStart(Op->getParent()->getIterator()) == P);
  904. }
  905. return *this;
  906. }
  907. defusechain_iterator operator++(int) { // Postincrement
  908. defusechain_iterator tmp = *this; ++*this; return tmp;
  909. }
  910. /// getOperandNo - Return the operand # of this MachineOperand in its
  911. /// MachineInstr.
  912. unsigned getOperandNo() const {
  913. assert(Op && "Cannot dereference end iterator!");
  914. return Op - &Op->getParent()->getOperand(0);
  915. }
  916. // Retrieve a reference to the current operand.
  917. MachineOperand &operator*() const {
  918. assert(Op && "Cannot dereference end iterator!");
  919. return *Op;
  920. }
  921. MachineOperand *operator->() const {
  922. assert(Op && "Cannot dereference end iterator!");
  923. return Op;
  924. }
  925. };
  926. /// defusechain_iterator - This class provides iterator support for machine
  927. /// operands in the function that use or define a specific register. If
  928. /// ReturnUses is true it returns uses of registers, if ReturnDefs is true it
  929. /// returns defs. If neither are true then you are silly and it always
  930. /// returns end(). If SkipDebug is true it skips uses marked Debug
  931. /// when incrementing.
  932. template <bool ReturnUses, bool ReturnDefs, bool SkipDebug, bool ByOperand,
  933. bool ByInstr, bool ByBundle>
  934. class defusechain_instr_iterator {
  935. friend class MachineRegisterInfo;
  936. public:
  937. using iterator_category = std::forward_iterator_tag;
  938. using value_type = MachineInstr;
  939. using difference_type = std::ptrdiff_t;
  940. using pointer = value_type *;
  941. using reference = value_type &;
  942. private:
  943. MachineOperand *Op = nullptr;
  944. explicit defusechain_instr_iterator(MachineOperand *op) : Op(op) {
  945. // If the first node isn't one we're interested in, advance to one that
  946. // we are interested in.
  947. if (op) {
  948. if ((!ReturnUses && op->isUse()) ||
  949. (!ReturnDefs && op->isDef()) ||
  950. (SkipDebug && op->isDebug()))
  951. advance();
  952. }
  953. }
  954. void advance() {
  955. assert(Op && "Cannot increment end iterator!");
  956. Op = getNextOperandForReg(Op);
  957. // All defs come before the uses, so stop def_iterator early.
  958. if (!ReturnUses) {
  959. if (Op) {
  960. if (Op->isUse())
  961. Op = nullptr;
  962. else
  963. assert(!Op->isDebug() && "Can't have debug defs");
  964. }
  965. } else {
  966. // If this is an operand we don't care about, skip it.
  967. while (Op && ((!ReturnDefs && Op->isDef()) ||
  968. (SkipDebug && Op->isDebug())))
  969. Op = getNextOperandForReg(Op);
  970. }
  971. }
  972. public:
  973. defusechain_instr_iterator() = default;
  974. bool operator==(const defusechain_instr_iterator &x) const {
  975. return Op == x.Op;
  976. }
  977. bool operator!=(const defusechain_instr_iterator &x) const {
  978. return !operator==(x);
  979. }
  980. /// atEnd - return true if this iterator is equal to reg_end() on the value.
  981. bool atEnd() const { return Op == nullptr; }
  982. // Iterator traversal: forward iteration only
  983. defusechain_instr_iterator &operator++() { // Preincrement
  984. assert(Op && "Cannot increment end iterator!");
  985. if (ByOperand)
  986. advance();
  987. else if (ByInstr) {
  988. MachineInstr *P = Op->getParent();
  989. do {
  990. advance();
  991. } while (Op && Op->getParent() == P);
  992. } else if (ByBundle) {
  993. MachineBasicBlock::instr_iterator P =
  994. getBundleStart(Op->getParent()->getIterator());
  995. do {
  996. advance();
  997. } while (Op && getBundleStart(Op->getParent()->getIterator()) == P);
  998. }
  999. return *this;
  1000. }
  1001. defusechain_instr_iterator operator++(int) { // Postincrement
  1002. defusechain_instr_iterator tmp = *this; ++*this; return tmp;
  1003. }
  1004. // Retrieve a reference to the current operand.
  1005. MachineInstr &operator*() const {
  1006. assert(Op && "Cannot dereference end iterator!");
  1007. if (ByBundle)
  1008. return *getBundleStart(Op->getParent()->getIterator());
  1009. return *Op->getParent();
  1010. }
  1011. MachineInstr *operator->() const { return &operator*(); }
  1012. };
  1013. };
  1014. /// Iterate over the pressure sets affected by the given physical or virtual
  1015. /// register. If Reg is physical, it must be a register unit (from
  1016. /// MCRegUnitIterator).
  1017. class PSetIterator {
  1018. const int *PSet = nullptr;
  1019. unsigned Weight = 0;
  1020. public:
  1021. PSetIterator() = default;
  1022. PSetIterator(Register RegUnit, const MachineRegisterInfo *MRI) {
  1023. const TargetRegisterInfo *TRI = MRI->getTargetRegisterInfo();
  1024. if (RegUnit.isVirtual()) {
  1025. const TargetRegisterClass *RC = MRI->getRegClass(RegUnit);
  1026. PSet = TRI->getRegClassPressureSets(RC);
  1027. Weight = TRI->getRegClassWeight(RC).RegWeight;
  1028. } else {
  1029. PSet = TRI->getRegUnitPressureSets(RegUnit);
  1030. Weight = TRI->getRegUnitWeight(RegUnit);
  1031. }
  1032. if (*PSet == -1)
  1033. PSet = nullptr;
  1034. }
  1035. bool isValid() const { return PSet; }
  1036. unsigned getWeight() const { return Weight; }
  1037. unsigned operator*() const { return *PSet; }
  1038. void operator++() {
  1039. assert(isValid() && "Invalid PSetIterator.");
  1040. ++PSet;
  1041. if (*PSet == -1)
  1042. PSet = nullptr;
  1043. }
  1044. };
  1045. inline PSetIterator
  1046. MachineRegisterInfo::getPressureSets(Register RegUnit) const {
  1047. return PSetIterator(RegUnit, this);
  1048. }
  1049. } // end namespace llvm
  1050. #endif // LLVM_CODEGEN_MACHINEREGISTERINFO_H