MachineRegisterInfo.h 48 KB

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