MachineRegisterInfo.h 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045
  1. //===-- llvm/CodeGen/MachineRegisterInfo.h ----------------------*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file defines the MachineRegisterInfo class.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_CODEGEN_MACHINEREGISTERINFO_H
  14. #define LLVM_CODEGEN_MACHINEREGISTERINFO_H
  15. #include "llvm/ADT/BitVector.h"
  16. #include "llvm/ADT/IndexedMap.h"
  17. #include "llvm/ADT/iterator_range.h"
  18. #include "llvm/CodeGen/MachineFunction.h"
  19. #include "llvm/CodeGen/MachineInstrBundle.h"
  20. #include "llvm/Target/TargetRegisterInfo.h"
  21. #include "llvm/Target/TargetSubtargetInfo.h"
  22. #include <vector>
  23. namespace llvm {
  24. class PSetIterator;
  25. /// MachineRegisterInfo - Keep track of information for virtual and physical
  26. /// registers, including vreg register classes, use/def chains for registers,
  27. /// etc.
  28. class MachineRegisterInfo {
  29. public:
  30. class Delegate {
  31. virtual void anchor();
  32. public:
  33. virtual void MRI_NoteNewVirtualRegister(unsigned Reg) = 0;
  34. virtual ~Delegate() {}
  35. };
  36. private:
  37. const MachineFunction *MF;
  38. Delegate *TheDelegate;
  39. /// IsSSA - True when the machine function is in SSA form and virtual
  40. /// registers have a single def.
  41. bool IsSSA;
  42. /// TracksLiveness - True while register liveness is being tracked accurately.
  43. /// Basic block live-in lists, kill flags, and implicit defs may not be
  44. /// accurate when after this flag is cleared.
  45. bool TracksLiveness;
  46. /// True if subregister liveness is tracked.
  47. bool TracksSubRegLiveness;
  48. /// VRegInfo - Information we keep for each virtual register.
  49. ///
  50. /// Each element in this list contains the register class of the vreg and the
  51. /// start of the use/def list for the register.
  52. IndexedMap<std::pair<const TargetRegisterClass*, MachineOperand*>,
  53. VirtReg2IndexFunctor> VRegInfo;
  54. /// RegAllocHints - This vector records register allocation hints for virtual
  55. /// registers. For each virtual register, it keeps a register and hint type
  56. /// pair making up the allocation hint. Hint type is target specific except
  57. /// for the value 0 which means the second value of the pair is the preferred
  58. /// register for allocation. For example, if the hint is <0, 1024>, it means
  59. /// the allocator should prefer the physical register allocated to the virtual
  60. /// register of the hint.
  61. IndexedMap<std::pair<unsigned, unsigned>, VirtReg2IndexFunctor> RegAllocHints;
  62. /// PhysRegUseDefLists - This is an array of the head of the use/def list for
  63. /// physical registers.
  64. std::vector<MachineOperand *> PhysRegUseDefLists;
  65. /// getRegUseDefListHead - Return the head pointer for the register use/def
  66. /// list for the specified virtual or physical register.
  67. MachineOperand *&getRegUseDefListHead(unsigned RegNo) {
  68. if (TargetRegisterInfo::isVirtualRegister(RegNo))
  69. return VRegInfo[RegNo].second;
  70. return PhysRegUseDefLists[RegNo];
  71. }
  72. MachineOperand *getRegUseDefListHead(unsigned RegNo) const {
  73. if (TargetRegisterInfo::isVirtualRegister(RegNo))
  74. return VRegInfo[RegNo].second;
  75. return PhysRegUseDefLists[RegNo];
  76. }
  77. /// Get the next element in the use-def chain.
  78. static MachineOperand *getNextOperandForReg(const MachineOperand *MO) {
  79. assert(MO && MO->isReg() && "This is not a register operand!");
  80. return MO->Contents.Reg.Next;
  81. }
  82. /// UsedRegUnits - This is a bit vector that is computed and set by the
  83. /// register allocator, and must be kept up to date by passes that run after
  84. /// register allocation (though most don't modify this). This is used
  85. /// so that the code generator knows which callee save registers to save and
  86. /// for other target specific uses.
  87. /// This vector has bits set for register units that are modified in the
  88. /// current function. It doesn't include registers clobbered by function
  89. /// calls with register mask operands.
  90. BitVector UsedRegUnits;
  91. /// UsedPhysRegMask - Additional used physregs including aliases.
  92. /// This bit vector represents all the registers clobbered by function calls.
  93. /// It can model things that UsedRegUnits can't, such as function calls that
  94. /// clobber ymm7 but preserve the low half in xmm7.
  95. BitVector UsedPhysRegMask;
  96. /// ReservedRegs - This is a bit vector of reserved registers. The target
  97. /// may change its mind about which registers should be reserved. This
  98. /// vector is the frozen set of reserved registers when register allocation
  99. /// started.
  100. BitVector ReservedRegs;
  101. /// Keep track of the physical registers that are live in to the function.
  102. /// Live in values are typically arguments in registers. LiveIn values are
  103. /// allowed to have virtual registers associated with them, stored in the
  104. /// second element.
  105. std::vector<std::pair<unsigned, unsigned> > LiveIns;
  106. MachineRegisterInfo(const MachineRegisterInfo&) = delete;
  107. void operator=(const MachineRegisterInfo&) = delete;
  108. public:
  109. explicit MachineRegisterInfo(const MachineFunction *MF);
  110. const TargetRegisterInfo *getTargetRegisterInfo() const {
  111. return MF->getSubtarget().getRegisterInfo();
  112. }
  113. void resetDelegate(Delegate *delegate) {
  114. // Ensure another delegate does not take over unless the current
  115. // delegate first unattaches itself. If we ever need to multicast
  116. // notifications, we will need to change to using a list.
  117. assert(TheDelegate == delegate &&
  118. "Only the current delegate can perform reset!");
  119. TheDelegate = nullptr;
  120. }
  121. void setDelegate(Delegate *delegate) {
  122. assert(delegate && !TheDelegate &&
  123. "Attempted to set delegate to null, or to change it without "
  124. "first resetting it!");
  125. TheDelegate = delegate;
  126. }
  127. //===--------------------------------------------------------------------===//
  128. // Function State
  129. //===--------------------------------------------------------------------===//
  130. // isSSA - Returns true when the machine function is in SSA form. Early
  131. // passes require the machine function to be in SSA form where every virtual
  132. // register has a single defining instruction.
  133. //
  134. // The TwoAddressInstructionPass and PHIElimination passes take the machine
  135. // function out of SSA form when they introduce multiple defs per virtual
  136. // register.
  137. bool isSSA() const { return IsSSA; }
  138. // leaveSSA - Indicates that the machine function is no longer in SSA form.
  139. void leaveSSA() { IsSSA = false; }
  140. /// tracksLiveness - Returns true when tracking register liveness accurately.
  141. ///
  142. /// While this flag is true, register liveness information in basic block
  143. /// live-in lists and machine instruction operands is accurate. This means it
  144. /// can be used to change the code in ways that affect the values in
  145. /// registers, for example by the register scavenger.
  146. ///
  147. /// When this flag is false, liveness is no longer reliable.
  148. bool tracksLiveness() const { return TracksLiveness; }
  149. /// invalidateLiveness - Indicates that register liveness is no longer being
  150. /// tracked accurately.
  151. ///
  152. /// This should be called by late passes that invalidate the liveness
  153. /// information.
  154. void invalidateLiveness() { TracksLiveness = false; }
  155. /// Returns true if liveness for register class @p RC should be tracked at
  156. /// the subregister level.
  157. bool shouldTrackSubRegLiveness(const TargetRegisterClass &RC) const {
  158. return subRegLivenessEnabled() && RC.HasDisjunctSubRegs;
  159. }
  160. bool shouldTrackSubRegLiveness(unsigned VReg) const {
  161. assert(TargetRegisterInfo::isVirtualRegister(VReg) && "Must pass a VReg");
  162. return shouldTrackSubRegLiveness(*getRegClass(VReg));
  163. }
  164. bool subRegLivenessEnabled() const {
  165. return TracksSubRegLiveness;
  166. }
  167. void enableSubRegLiveness(bool Enable = true) {
  168. TracksSubRegLiveness = Enable;
  169. }
  170. //===--------------------------------------------------------------------===//
  171. // Register Info
  172. //===--------------------------------------------------------------------===//
  173. // Strictly for use by MachineInstr.cpp.
  174. void addRegOperandToUseList(MachineOperand *MO);
  175. // Strictly for use by MachineInstr.cpp.
  176. void removeRegOperandFromUseList(MachineOperand *MO);
  177. // Strictly for use by MachineInstr.cpp.
  178. void moveOperands(MachineOperand *Dst, MachineOperand *Src, unsigned NumOps);
  179. /// Verify the sanity of the use list for Reg.
  180. void verifyUseList(unsigned Reg) const;
  181. /// Verify the use list of all registers.
  182. void verifyUseLists() const;
  183. /// reg_begin/reg_end - Provide iteration support to walk over all definitions
  184. /// and uses of a register within the MachineFunction that corresponds to this
  185. /// MachineRegisterInfo object.
  186. template<bool Uses, bool Defs, bool SkipDebug,
  187. bool ByOperand, bool ByInstr, bool ByBundle>
  188. class defusechain_iterator;
  189. template<bool Uses, bool Defs, bool SkipDebug,
  190. bool ByOperand, bool ByInstr, bool ByBundle>
  191. class defusechain_instr_iterator;
  192. // Make it a friend so it can access getNextOperandForReg().
  193. template<bool, bool, bool, bool, bool, bool>
  194. friend class defusechain_iterator;
  195. template<bool, bool, bool, bool, bool, bool>
  196. friend class defusechain_instr_iterator;
  197. /// reg_iterator/reg_begin/reg_end - Walk all defs and uses of the specified
  198. /// register.
  199. typedef defusechain_iterator<true,true,false,true,false,false>
  200. reg_iterator;
  201. reg_iterator reg_begin(unsigned RegNo) const {
  202. return reg_iterator(getRegUseDefListHead(RegNo));
  203. }
  204. static reg_iterator reg_end() { return reg_iterator(nullptr); }
  205. inline iterator_range<reg_iterator> reg_operands(unsigned Reg) const {
  206. return iterator_range<reg_iterator>(reg_begin(Reg), reg_end());
  207. }
  208. /// reg_instr_iterator/reg_instr_begin/reg_instr_end - Walk all defs and uses
  209. /// of the specified register, stepping by MachineInstr.
  210. typedef defusechain_instr_iterator<true,true,false,false,true,false>
  211. reg_instr_iterator;
  212. reg_instr_iterator reg_instr_begin(unsigned RegNo) const {
  213. return reg_instr_iterator(getRegUseDefListHead(RegNo));
  214. }
  215. static reg_instr_iterator reg_instr_end() {
  216. return reg_instr_iterator(nullptr);
  217. }
  218. inline iterator_range<reg_instr_iterator>
  219. reg_instructions(unsigned Reg) const {
  220. return iterator_range<reg_instr_iterator>(reg_instr_begin(Reg),
  221. reg_instr_end());
  222. }
  223. /// reg_bundle_iterator/reg_bundle_begin/reg_bundle_end - Walk all defs and uses
  224. /// of the specified register, stepping by bundle.
  225. typedef defusechain_instr_iterator<true,true,false,false,false,true>
  226. reg_bundle_iterator;
  227. reg_bundle_iterator reg_bundle_begin(unsigned RegNo) const {
  228. return reg_bundle_iterator(getRegUseDefListHead(RegNo));
  229. }
  230. static reg_bundle_iterator reg_bundle_end() {
  231. return reg_bundle_iterator(nullptr);
  232. }
  233. inline iterator_range<reg_bundle_iterator> reg_bundles(unsigned Reg) const {
  234. return iterator_range<reg_bundle_iterator>(reg_bundle_begin(Reg),
  235. reg_bundle_end());
  236. }
  237. /// reg_empty - Return true if there are no instructions using or defining the
  238. /// specified register (it may be live-in).
  239. bool reg_empty(unsigned RegNo) const { return reg_begin(RegNo) == reg_end(); }
  240. /// reg_nodbg_iterator/reg_nodbg_begin/reg_nodbg_end - Walk all defs and uses
  241. /// of the specified register, skipping those marked as Debug.
  242. typedef defusechain_iterator<true,true,true,true,false,false>
  243. reg_nodbg_iterator;
  244. reg_nodbg_iterator reg_nodbg_begin(unsigned RegNo) const {
  245. return reg_nodbg_iterator(getRegUseDefListHead(RegNo));
  246. }
  247. static reg_nodbg_iterator reg_nodbg_end() {
  248. return reg_nodbg_iterator(nullptr);
  249. }
  250. inline iterator_range<reg_nodbg_iterator>
  251. reg_nodbg_operands(unsigned Reg) const {
  252. return iterator_range<reg_nodbg_iterator>(reg_nodbg_begin(Reg),
  253. reg_nodbg_end());
  254. }
  255. /// reg_instr_nodbg_iterator/reg_instr_nodbg_begin/reg_instr_nodbg_end - Walk
  256. /// all defs and uses of the specified register, stepping by MachineInstr,
  257. /// skipping those marked as Debug.
  258. typedef defusechain_instr_iterator<true,true,true,false,true,false>
  259. reg_instr_nodbg_iterator;
  260. reg_instr_nodbg_iterator reg_instr_nodbg_begin(unsigned RegNo) const {
  261. return reg_instr_nodbg_iterator(getRegUseDefListHead(RegNo));
  262. }
  263. static reg_instr_nodbg_iterator reg_instr_nodbg_end() {
  264. return reg_instr_nodbg_iterator(nullptr);
  265. }
  266. inline iterator_range<reg_instr_nodbg_iterator>
  267. reg_nodbg_instructions(unsigned Reg) const {
  268. return iterator_range<reg_instr_nodbg_iterator>(reg_instr_nodbg_begin(Reg),
  269. reg_instr_nodbg_end());
  270. }
  271. /// reg_bundle_nodbg_iterator/reg_bundle_nodbg_begin/reg_bundle_nodbg_end - Walk
  272. /// all defs and uses of the specified register, stepping by bundle,
  273. /// skipping those marked as Debug.
  274. typedef defusechain_instr_iterator<true,true,true,false,false,true>
  275. reg_bundle_nodbg_iterator;
  276. reg_bundle_nodbg_iterator reg_bundle_nodbg_begin(unsigned RegNo) const {
  277. return reg_bundle_nodbg_iterator(getRegUseDefListHead(RegNo));
  278. }
  279. static reg_bundle_nodbg_iterator reg_bundle_nodbg_end() {
  280. return reg_bundle_nodbg_iterator(nullptr);
  281. }
  282. inline iterator_range<reg_bundle_nodbg_iterator>
  283. reg_nodbg_bundles(unsigned Reg) const {
  284. return iterator_range<reg_bundle_nodbg_iterator>(reg_bundle_nodbg_begin(Reg),
  285. reg_bundle_nodbg_end());
  286. }
  287. /// reg_nodbg_empty - Return true if the only instructions using or defining
  288. /// Reg are Debug instructions.
  289. bool reg_nodbg_empty(unsigned RegNo) const {
  290. return reg_nodbg_begin(RegNo) == reg_nodbg_end();
  291. }
  292. /// def_iterator/def_begin/def_end - Walk all defs of the specified register.
  293. typedef defusechain_iterator<false,true,false,true,false,false>
  294. def_iterator;
  295. def_iterator def_begin(unsigned RegNo) const {
  296. return def_iterator(getRegUseDefListHead(RegNo));
  297. }
  298. static def_iterator def_end() { return def_iterator(nullptr); }
  299. inline iterator_range<def_iterator> def_operands(unsigned Reg) const {
  300. return iterator_range<def_iterator>(def_begin(Reg), def_end());
  301. }
  302. /// def_instr_iterator/def_instr_begin/def_instr_end - Walk all defs of the
  303. /// specified register, stepping by MachineInst.
  304. typedef defusechain_instr_iterator<false,true,false,false,true,false>
  305. def_instr_iterator;
  306. def_instr_iterator def_instr_begin(unsigned RegNo) const {
  307. return def_instr_iterator(getRegUseDefListHead(RegNo));
  308. }
  309. static def_instr_iterator def_instr_end() {
  310. return def_instr_iterator(nullptr);
  311. }
  312. inline iterator_range<def_instr_iterator>
  313. def_instructions(unsigned Reg) const {
  314. return iterator_range<def_instr_iterator>(def_instr_begin(Reg),
  315. def_instr_end());
  316. }
  317. /// def_bundle_iterator/def_bundle_begin/def_bundle_end - Walk all defs of the
  318. /// specified register, stepping by bundle.
  319. typedef defusechain_instr_iterator<false,true,false,false,false,true>
  320. def_bundle_iterator;
  321. def_bundle_iterator def_bundle_begin(unsigned RegNo) const {
  322. return def_bundle_iterator(getRegUseDefListHead(RegNo));
  323. }
  324. static def_bundle_iterator def_bundle_end() {
  325. return def_bundle_iterator(nullptr);
  326. }
  327. inline iterator_range<def_bundle_iterator> def_bundles(unsigned Reg) const {
  328. return iterator_range<def_bundle_iterator>(def_bundle_begin(Reg),
  329. def_bundle_end());
  330. }
  331. /// def_empty - Return true if there are no instructions defining the
  332. /// specified register (it may be live-in).
  333. bool def_empty(unsigned RegNo) const { return def_begin(RegNo) == def_end(); }
  334. /// hasOneDef - Return true if there is exactly one instruction defining the
  335. /// specified register.
  336. bool hasOneDef(unsigned RegNo) const {
  337. def_iterator DI = def_begin(RegNo);
  338. if (DI == def_end())
  339. return false;
  340. return ++DI == def_end();
  341. }
  342. /// use_iterator/use_begin/use_end - Walk all uses of the specified register.
  343. typedef defusechain_iterator<true,false,false,true,false,false>
  344. use_iterator;
  345. use_iterator use_begin(unsigned RegNo) const {
  346. return use_iterator(getRegUseDefListHead(RegNo));
  347. }
  348. static use_iterator use_end() { return use_iterator(nullptr); }
  349. inline iterator_range<use_iterator> use_operands(unsigned Reg) const {
  350. return iterator_range<use_iterator>(use_begin(Reg), use_end());
  351. }
  352. /// use_instr_iterator/use_instr_begin/use_instr_end - Walk all uses of the
  353. /// specified register, stepping by MachineInstr.
  354. typedef defusechain_instr_iterator<true,false,false,false,true,false>
  355. use_instr_iterator;
  356. use_instr_iterator use_instr_begin(unsigned RegNo) const {
  357. return use_instr_iterator(getRegUseDefListHead(RegNo));
  358. }
  359. static use_instr_iterator use_instr_end() {
  360. return use_instr_iterator(nullptr);
  361. }
  362. inline iterator_range<use_instr_iterator>
  363. use_instructions(unsigned Reg) const {
  364. return iterator_range<use_instr_iterator>(use_instr_begin(Reg),
  365. use_instr_end());
  366. }
  367. /// use_bundle_iterator/use_bundle_begin/use_bundle_end - Walk all uses of the
  368. /// specified register, stepping by bundle.
  369. typedef defusechain_instr_iterator<true,false,false,false,false,true>
  370. use_bundle_iterator;
  371. use_bundle_iterator use_bundle_begin(unsigned RegNo) const {
  372. return use_bundle_iterator(getRegUseDefListHead(RegNo));
  373. }
  374. static use_bundle_iterator use_bundle_end() {
  375. return use_bundle_iterator(nullptr);
  376. }
  377. inline iterator_range<use_bundle_iterator> use_bundles(unsigned Reg) const {
  378. return iterator_range<use_bundle_iterator>(use_bundle_begin(Reg),
  379. use_bundle_end());
  380. }
  381. /// use_empty - Return true if there are no instructions using the specified
  382. /// register.
  383. bool use_empty(unsigned RegNo) const { return use_begin(RegNo) == use_end(); }
  384. /// hasOneUse - Return true if there is exactly one instruction using the
  385. /// specified register.
  386. bool hasOneUse(unsigned RegNo) const {
  387. use_iterator UI = use_begin(RegNo);
  388. if (UI == use_end())
  389. return false;
  390. return ++UI == use_end();
  391. }
  392. /// use_nodbg_iterator/use_nodbg_begin/use_nodbg_end - Walk all uses of the
  393. /// specified register, skipping those marked as Debug.
  394. typedef defusechain_iterator<true,false,true,true,false,false>
  395. use_nodbg_iterator;
  396. use_nodbg_iterator use_nodbg_begin(unsigned RegNo) const {
  397. return use_nodbg_iterator(getRegUseDefListHead(RegNo));
  398. }
  399. static use_nodbg_iterator use_nodbg_end() {
  400. return use_nodbg_iterator(nullptr);
  401. }
  402. inline iterator_range<use_nodbg_iterator>
  403. use_nodbg_operands(unsigned Reg) const {
  404. return iterator_range<use_nodbg_iterator>(use_nodbg_begin(Reg),
  405. use_nodbg_end());
  406. }
  407. /// use_instr_nodbg_iterator/use_instr_nodbg_begin/use_instr_nodbg_end - Walk
  408. /// all uses of the specified register, stepping by MachineInstr, skipping
  409. /// those marked as Debug.
  410. typedef defusechain_instr_iterator<true,false,true,false,true,false>
  411. use_instr_nodbg_iterator;
  412. use_instr_nodbg_iterator use_instr_nodbg_begin(unsigned RegNo) const {
  413. return use_instr_nodbg_iterator(getRegUseDefListHead(RegNo));
  414. }
  415. static use_instr_nodbg_iterator use_instr_nodbg_end() {
  416. return use_instr_nodbg_iterator(nullptr);
  417. }
  418. inline iterator_range<use_instr_nodbg_iterator>
  419. use_nodbg_instructions(unsigned Reg) const {
  420. return iterator_range<use_instr_nodbg_iterator>(use_instr_nodbg_begin(Reg),
  421. use_instr_nodbg_end());
  422. }
  423. /// use_bundle_nodbg_iterator/use_bundle_nodbg_begin/use_bundle_nodbg_end - Walk
  424. /// all uses of the specified register, stepping by bundle, skipping
  425. /// those marked as Debug.
  426. typedef defusechain_instr_iterator<true,false,true,false,false,true>
  427. use_bundle_nodbg_iterator;
  428. use_bundle_nodbg_iterator use_bundle_nodbg_begin(unsigned RegNo) const {
  429. return use_bundle_nodbg_iterator(getRegUseDefListHead(RegNo));
  430. }
  431. static use_bundle_nodbg_iterator use_bundle_nodbg_end() {
  432. return use_bundle_nodbg_iterator(nullptr);
  433. }
  434. inline iterator_range<use_bundle_nodbg_iterator>
  435. use_nodbg_bundles(unsigned Reg) const {
  436. return iterator_range<use_bundle_nodbg_iterator>(use_bundle_nodbg_begin(Reg),
  437. use_bundle_nodbg_end());
  438. }
  439. /// use_nodbg_empty - Return true if there are no non-Debug instructions
  440. /// using the specified register.
  441. bool use_nodbg_empty(unsigned RegNo) const {
  442. return use_nodbg_begin(RegNo) == use_nodbg_end();
  443. }
  444. /// hasOneNonDBGUse - Return true if there is exactly one non-Debug
  445. /// instruction using the specified register.
  446. bool hasOneNonDBGUse(unsigned RegNo) const;
  447. /// replaceRegWith - Replace all instances of FromReg with ToReg in the
  448. /// machine function. This is like llvm-level X->replaceAllUsesWith(Y),
  449. /// except that it also changes any definitions of the register as well.
  450. ///
  451. /// Note that it is usually necessary to first constrain ToReg's register
  452. /// class to match the FromReg constraints using:
  453. ///
  454. /// constrainRegClass(ToReg, getRegClass(FromReg))
  455. ///
  456. /// That function will return NULL if the virtual registers have incompatible
  457. /// constraints.
  458. ///
  459. /// Note that if ToReg is a physical register the function will replace and
  460. /// apply sub registers to ToReg in order to obtain a final/proper physical
  461. /// register.
  462. void replaceRegWith(unsigned FromReg, unsigned ToReg);
  463. /// getVRegDef - Return the machine instr that defines the specified virtual
  464. /// register or null if none is found. This assumes that the code is in SSA
  465. /// form, so there should only be one definition.
  466. MachineInstr *getVRegDef(unsigned Reg) const;
  467. /// getUniqueVRegDef - Return the unique machine instr that defines the
  468. /// specified virtual register or null if none is found. If there are
  469. /// multiple definitions or no definition, return null.
  470. MachineInstr *getUniqueVRegDef(unsigned Reg) const;
  471. /// clearKillFlags - Iterate over all the uses of the given register and
  472. /// clear the kill flag from the MachineOperand. This function is used by
  473. /// optimization passes which extend register lifetimes and need only
  474. /// preserve conservative kill flag information.
  475. void clearKillFlags(unsigned Reg) const;
  476. #ifndef NDEBUG
  477. void dumpUses(unsigned RegNo) const;
  478. #endif
  479. /// isConstantPhysReg - Returns true if PhysReg is unallocatable and constant
  480. /// throughout the function. It is safe to move instructions that read such
  481. /// a physreg.
  482. bool isConstantPhysReg(unsigned PhysReg, const MachineFunction &MF) const;
  483. /// Get an iterator over the pressure sets affected by the given physical or
  484. /// virtual register. If RegUnit is physical, it must be a register unit (from
  485. /// MCRegUnitIterator).
  486. PSetIterator getPressureSets(unsigned RegUnit) const;
  487. //===--------------------------------------------------------------------===//
  488. // Virtual Register Info
  489. //===--------------------------------------------------------------------===//
  490. /// getRegClass - Return the register class of the specified virtual register.
  491. ///
  492. const TargetRegisterClass *getRegClass(unsigned Reg) const {
  493. return VRegInfo[Reg].first;
  494. }
  495. /// setRegClass - Set the register class of the specified virtual register.
  496. ///
  497. void setRegClass(unsigned Reg, const TargetRegisterClass *RC);
  498. /// constrainRegClass - Constrain the register class of the specified virtual
  499. /// register to be a common subclass of RC and the current register class,
  500. /// but only if the new class has at least MinNumRegs registers. Return the
  501. /// new register class, or NULL if no such class exists.
  502. /// This should only be used when the constraint is known to be trivial, like
  503. /// GR32 -> GR32_NOSP. Beware of increasing register pressure.
  504. ///
  505. const TargetRegisterClass *constrainRegClass(unsigned Reg,
  506. const TargetRegisterClass *RC,
  507. unsigned MinNumRegs = 0);
  508. /// recomputeRegClass - Try to find a legal super-class of Reg's register
  509. /// class that still satisfies the constraints from the instructions using
  510. /// Reg. Returns true if Reg was upgraded.
  511. ///
  512. /// This method can be used after constraints have been removed from a
  513. /// virtual register, for example after removing instructions or splitting
  514. /// the live range.
  515. ///
  516. bool recomputeRegClass(unsigned Reg);
  517. /// createVirtualRegister - Create and return a new virtual register in the
  518. /// function with the specified register class.
  519. ///
  520. unsigned createVirtualRegister(const TargetRegisterClass *RegClass);
  521. /// getNumVirtRegs - Return the number of virtual registers created.
  522. ///
  523. unsigned getNumVirtRegs() const { return VRegInfo.size(); }
  524. /// clearVirtRegs - Remove all virtual registers (after physreg assignment).
  525. void clearVirtRegs();
  526. /// setRegAllocationHint - Specify a register allocation hint for the
  527. /// specified virtual register.
  528. void setRegAllocationHint(unsigned VReg, unsigned Type, unsigned PrefReg) {
  529. assert(TargetRegisterInfo::isVirtualRegister(VReg));
  530. RegAllocHints[VReg].first = Type;
  531. RegAllocHints[VReg].second = PrefReg;
  532. }
  533. /// getRegAllocationHint - Return the register allocation hint for the
  534. /// specified virtual register.
  535. std::pair<unsigned, unsigned>
  536. getRegAllocationHint(unsigned VReg) const {
  537. assert(TargetRegisterInfo::isVirtualRegister(VReg));
  538. return RegAllocHints[VReg];
  539. }
  540. /// getSimpleHint - Return the preferred register allocation hint, or 0 if a
  541. /// standard simple hint (Type == 0) is not set.
  542. unsigned getSimpleHint(unsigned VReg) const {
  543. assert(TargetRegisterInfo::isVirtualRegister(VReg));
  544. std::pair<unsigned, unsigned> Hint = getRegAllocationHint(VReg);
  545. return Hint.first ? 0 : Hint.second;
  546. }
  547. /// markUsesInDebugValueAsUndef - Mark every DBG_VALUE referencing the
  548. /// specified register as undefined which causes the DBG_VALUE to be
  549. /// deleted during LiveDebugVariables analysis.
  550. void markUsesInDebugValueAsUndef(unsigned Reg) const;
  551. /// Return true if the specified register is modified in this function.
  552. /// This checks that no defining machine operands exist for the register or
  553. /// any of its aliases. Definitions found on functions marked noreturn are
  554. /// ignored.
  555. bool isPhysRegModified(unsigned PhysReg) const;
  556. //===--------------------------------------------------------------------===//
  557. // Physical Register Use Info
  558. //===--------------------------------------------------------------------===//
  559. /// isPhysRegUsed - Return true if the specified register is used in this
  560. /// function. Also check for clobbered aliases and registers clobbered by
  561. /// function calls with register mask operands.
  562. ///
  563. /// This only works after register allocation.
  564. bool isPhysRegUsed(unsigned Reg) const {
  565. if (UsedPhysRegMask.test(Reg))
  566. return true;
  567. for (MCRegUnitIterator Units(Reg, getTargetRegisterInfo());
  568. Units.isValid(); ++Units)
  569. if (UsedRegUnits.test(*Units))
  570. return true;
  571. return false;
  572. }
  573. /// Mark the specified register unit as used in this function.
  574. /// This should only be called during and after register allocation.
  575. void setRegUnitUsed(unsigned RegUnit) {
  576. UsedRegUnits.set(RegUnit);
  577. }
  578. /// setPhysRegUsed - Mark the specified register used in this function.
  579. /// This should only be called during and after register allocation.
  580. void setPhysRegUsed(unsigned Reg) {
  581. for (MCRegUnitIterator Units(Reg, getTargetRegisterInfo());
  582. Units.isValid(); ++Units)
  583. UsedRegUnits.set(*Units);
  584. }
  585. /// addPhysRegsUsedFromRegMask - Mark any registers not in RegMask as used.
  586. /// This corresponds to the bit mask attached to register mask operands.
  587. void addPhysRegsUsedFromRegMask(const uint32_t *RegMask) {
  588. UsedPhysRegMask.setBitsNotInMask(RegMask);
  589. }
  590. /// setPhysRegUnused - Mark the specified register unused in this function.
  591. /// This should only be called during and after register allocation.
  592. void setPhysRegUnused(unsigned Reg) {
  593. UsedPhysRegMask.reset(Reg);
  594. for (MCRegUnitIterator Units(Reg, getTargetRegisterInfo());
  595. Units.isValid(); ++Units)
  596. UsedRegUnits.reset(*Units);
  597. }
  598. //===--------------------------------------------------------------------===//
  599. // Reserved Register Info
  600. //===--------------------------------------------------------------------===//
  601. //
  602. // The set of reserved registers must be invariant during register
  603. // allocation. For example, the target cannot suddenly decide it needs a
  604. // frame pointer when the register allocator has already used the frame
  605. // pointer register for something else.
  606. //
  607. // These methods can be used by target hooks like hasFP() to avoid changing
  608. // the reserved register set during register allocation.
  609. /// freezeReservedRegs - Called by the register allocator to freeze the set
  610. /// of reserved registers before allocation begins.
  611. void freezeReservedRegs(const MachineFunction&);
  612. /// reservedRegsFrozen - Returns true after freezeReservedRegs() was called
  613. /// to ensure the set of reserved registers stays constant.
  614. bool reservedRegsFrozen() const {
  615. return !ReservedRegs.empty();
  616. }
  617. /// canReserveReg - Returns true if PhysReg can be used as a reserved
  618. /// register. Any register can be reserved before freezeReservedRegs() is
  619. /// called.
  620. bool canReserveReg(unsigned PhysReg) const {
  621. return !reservedRegsFrozen() || ReservedRegs.test(PhysReg);
  622. }
  623. /// getReservedRegs - Returns a reference to the frozen set of reserved
  624. /// registers. This method should always be preferred to calling
  625. /// TRI::getReservedRegs() when possible.
  626. const BitVector &getReservedRegs() const {
  627. assert(reservedRegsFrozen() &&
  628. "Reserved registers haven't been frozen yet. "
  629. "Use TRI::getReservedRegs().");
  630. return ReservedRegs;
  631. }
  632. /// isReserved - Returns true when PhysReg is a reserved register.
  633. ///
  634. /// Reserved registers may belong to an allocatable register class, but the
  635. /// target has explicitly requested that they are not used.
  636. ///
  637. bool isReserved(unsigned PhysReg) const {
  638. return getReservedRegs().test(PhysReg);
  639. }
  640. /// isAllocatable - Returns true when PhysReg belongs to an allocatable
  641. /// register class and it hasn't been reserved.
  642. ///
  643. /// Allocatable registers may show up in the allocation order of some virtual
  644. /// register, so a register allocator needs to track its liveness and
  645. /// availability.
  646. bool isAllocatable(unsigned PhysReg) const {
  647. return getTargetRegisterInfo()->isInAllocatableClass(PhysReg) &&
  648. !isReserved(PhysReg);
  649. }
  650. //===--------------------------------------------------------------------===//
  651. // LiveIn Management
  652. //===--------------------------------------------------------------------===//
  653. /// addLiveIn - Add the specified register as a live-in. Note that it
  654. /// is an error to add the same register to the same set more than once.
  655. void addLiveIn(unsigned Reg, unsigned vreg = 0) {
  656. LiveIns.push_back(std::make_pair(Reg, vreg));
  657. }
  658. // Iteration support for the live-ins set. It's kept in sorted order
  659. // by register number.
  660. typedef std::vector<std::pair<unsigned,unsigned> >::const_iterator
  661. livein_iterator;
  662. livein_iterator livein_begin() const { return LiveIns.begin(); }
  663. livein_iterator livein_end() const { return LiveIns.end(); }
  664. bool livein_empty() const { return LiveIns.empty(); }
  665. bool isLiveIn(unsigned Reg) const;
  666. /// getLiveInPhysReg - If VReg is a live-in virtual register, return the
  667. /// corresponding live-in physical register.
  668. unsigned getLiveInPhysReg(unsigned VReg) const;
  669. /// getLiveInVirtReg - If PReg is a live-in physical register, return the
  670. /// corresponding live-in physical register.
  671. unsigned getLiveInVirtReg(unsigned PReg) const;
  672. /// EmitLiveInCopies - Emit copies to initialize livein virtual registers
  673. /// into the given entry block.
  674. void EmitLiveInCopies(MachineBasicBlock *EntryMBB,
  675. const TargetRegisterInfo &TRI,
  676. const TargetInstrInfo &TII);
  677. /// Returns a mask covering all bits that can appear in lane masks of
  678. /// subregisters of the virtual register @p Reg.
  679. unsigned getMaxLaneMaskForVReg(unsigned Reg) const;
  680. /// defusechain_iterator - This class provides iterator support for machine
  681. /// operands in the function that use or define a specific register. If
  682. /// ReturnUses is true it returns uses of registers, if ReturnDefs is true it
  683. /// returns defs. If neither are true then you are silly and it always
  684. /// returns end(). If SkipDebug is true it skips uses marked Debug
  685. /// when incrementing.
  686. template<bool ReturnUses, bool ReturnDefs, bool SkipDebug,
  687. bool ByOperand, bool ByInstr, bool ByBundle>
  688. class defusechain_iterator
  689. : public std::iterator<std::forward_iterator_tag, MachineInstr, ptrdiff_t> {
  690. MachineOperand *Op;
  691. explicit defusechain_iterator(MachineOperand *op) : Op(op) {
  692. // If the first node isn't one we're interested in, advance to one that
  693. // we are interested in.
  694. if (op) {
  695. if ((!ReturnUses && op->isUse()) ||
  696. (!ReturnDefs && op->isDef()) ||
  697. (SkipDebug && op->isDebug()))
  698. advance();
  699. }
  700. }
  701. friend class MachineRegisterInfo;
  702. void advance() {
  703. assert(Op && "Cannot increment end iterator!");
  704. Op = getNextOperandForReg(Op);
  705. // All defs come before the uses, so stop def_iterator early.
  706. if (!ReturnUses) {
  707. if (Op) {
  708. if (Op->isUse())
  709. Op = nullptr;
  710. else
  711. assert(!Op->isDebug() && "Can't have debug defs");
  712. }
  713. } else {
  714. // If this is an operand we don't care about, skip it.
  715. while (Op && ((!ReturnDefs && Op->isDef()) ||
  716. (SkipDebug && Op->isDebug())))
  717. Op = getNextOperandForReg(Op);
  718. }
  719. }
  720. public:
  721. typedef std::iterator<std::forward_iterator_tag,
  722. MachineInstr, ptrdiff_t>::reference reference;
  723. typedef std::iterator<std::forward_iterator_tag,
  724. MachineInstr, ptrdiff_t>::pointer pointer;
  725. defusechain_iterator() : Op(nullptr) {}
  726. bool operator==(const defusechain_iterator &x) const {
  727. return Op == x.Op;
  728. }
  729. bool operator!=(const defusechain_iterator &x) const {
  730. return !operator==(x);
  731. }
  732. /// atEnd - return true if this iterator is equal to reg_end() on the value.
  733. bool atEnd() const { return Op == nullptr; }
  734. // Iterator traversal: forward iteration only
  735. defusechain_iterator &operator++() { // Preincrement
  736. assert(Op && "Cannot increment end iterator!");
  737. if (ByOperand)
  738. advance();
  739. else if (ByInstr) {
  740. MachineInstr *P = Op->getParent();
  741. do {
  742. advance();
  743. } while (Op && Op->getParent() == P);
  744. } else if (ByBundle) {
  745. MachineInstr *P = getBundleStart(Op->getParent());
  746. do {
  747. advance();
  748. } while (Op && getBundleStart(Op->getParent()) == P);
  749. }
  750. return *this;
  751. }
  752. defusechain_iterator operator++(int) { // Postincrement
  753. defusechain_iterator tmp = *this; ++*this; return tmp;
  754. }
  755. /// getOperandNo - Return the operand # of this MachineOperand in its
  756. /// MachineInstr.
  757. unsigned getOperandNo() const {
  758. assert(Op && "Cannot dereference end iterator!");
  759. return Op - &Op->getParent()->getOperand(0);
  760. }
  761. // Retrieve a reference to the current operand.
  762. MachineOperand &operator*() const {
  763. assert(Op && "Cannot dereference end iterator!");
  764. return *Op;
  765. }
  766. MachineOperand *operator->() const {
  767. assert(Op && "Cannot dereference end iterator!");
  768. return Op;
  769. }
  770. };
  771. /// defusechain_iterator - This class provides iterator support for machine
  772. /// operands in the function that use or define a specific register. If
  773. /// ReturnUses is true it returns uses of registers, if ReturnDefs is true it
  774. /// returns defs. If neither are true then you are silly and it always
  775. /// returns end(). If SkipDebug is true it skips uses marked Debug
  776. /// when incrementing.
  777. template<bool ReturnUses, bool ReturnDefs, bool SkipDebug,
  778. bool ByOperand, bool ByInstr, bool ByBundle>
  779. class defusechain_instr_iterator
  780. : public std::iterator<std::forward_iterator_tag, MachineInstr, ptrdiff_t> {
  781. MachineOperand *Op;
  782. explicit defusechain_instr_iterator(MachineOperand *op) : Op(op) {
  783. // If the first node isn't one we're interested in, advance to one that
  784. // we are interested in.
  785. if (op) {
  786. if ((!ReturnUses && op->isUse()) ||
  787. (!ReturnDefs && op->isDef()) ||
  788. (SkipDebug && op->isDebug()))
  789. advance();
  790. }
  791. }
  792. friend class MachineRegisterInfo;
  793. void advance() {
  794. assert(Op && "Cannot increment end iterator!");
  795. Op = getNextOperandForReg(Op);
  796. // All defs come before the uses, so stop def_iterator early.
  797. if (!ReturnUses) {
  798. if (Op) {
  799. if (Op->isUse())
  800. Op = nullptr;
  801. else
  802. assert(!Op->isDebug() && "Can't have debug defs");
  803. }
  804. } else {
  805. // If this is an operand we don't care about, skip it.
  806. while (Op && ((!ReturnDefs && Op->isDef()) ||
  807. (SkipDebug && Op->isDebug())))
  808. Op = getNextOperandForReg(Op);
  809. }
  810. }
  811. public:
  812. typedef std::iterator<std::forward_iterator_tag,
  813. MachineInstr, ptrdiff_t>::reference reference;
  814. typedef std::iterator<std::forward_iterator_tag,
  815. MachineInstr, ptrdiff_t>::pointer pointer;
  816. defusechain_instr_iterator() : Op(nullptr) {}
  817. bool operator==(const defusechain_instr_iterator &x) const {
  818. return Op == x.Op;
  819. }
  820. bool operator!=(const defusechain_instr_iterator &x) const {
  821. return !operator==(x);
  822. }
  823. /// atEnd - return true if this iterator is equal to reg_end() on the value.
  824. bool atEnd() const { return Op == nullptr; }
  825. // Iterator traversal: forward iteration only
  826. defusechain_instr_iterator &operator++() { // Preincrement
  827. assert(Op && "Cannot increment end iterator!");
  828. if (ByOperand)
  829. advance();
  830. else if (ByInstr) {
  831. MachineInstr *P = Op->getParent();
  832. do {
  833. advance();
  834. } while (Op && Op->getParent() == P);
  835. } else if (ByBundle) {
  836. MachineInstr *P = getBundleStart(Op->getParent());
  837. do {
  838. advance();
  839. } while (Op && getBundleStart(Op->getParent()) == P);
  840. }
  841. return *this;
  842. }
  843. defusechain_instr_iterator operator++(int) { // Postincrement
  844. defusechain_instr_iterator tmp = *this; ++*this; return tmp;
  845. }
  846. // Retrieve a reference to the current operand.
  847. MachineInstr &operator*() const {
  848. assert(Op && "Cannot dereference end iterator!");
  849. if (ByBundle) return *(getBundleStart(Op->getParent()));
  850. return *Op->getParent();
  851. }
  852. MachineInstr *operator->() const {
  853. assert(Op && "Cannot dereference end iterator!");
  854. if (ByBundle) return getBundleStart(Op->getParent());
  855. return Op->getParent();
  856. }
  857. };
  858. };
  859. /// Iterate over the pressure sets affected by the given physical or virtual
  860. /// register. If Reg is physical, it must be a register unit (from
  861. /// MCRegUnitIterator).
  862. class PSetIterator {
  863. const int *PSet;
  864. unsigned Weight;
  865. public:
  866. PSetIterator(): PSet(nullptr), Weight(0) {}
  867. PSetIterator(unsigned RegUnit, const MachineRegisterInfo *MRI) {
  868. const TargetRegisterInfo *TRI = MRI->getTargetRegisterInfo();
  869. if (TargetRegisterInfo::isVirtualRegister(RegUnit)) {
  870. const TargetRegisterClass *RC = MRI->getRegClass(RegUnit);
  871. PSet = TRI->getRegClassPressureSets(RC);
  872. Weight = TRI->getRegClassWeight(RC).RegWeight;
  873. }
  874. else {
  875. PSet = TRI->getRegUnitPressureSets(RegUnit);
  876. Weight = TRI->getRegUnitWeight(RegUnit);
  877. }
  878. if (*PSet == -1)
  879. PSet = nullptr;
  880. }
  881. bool isValid() const { return PSet; }
  882. unsigned getWeight() const { return Weight; }
  883. unsigned operator*() const { return *PSet; }
  884. void operator++() {
  885. assert(isValid() && "Invalid PSetIterator.");
  886. ++PSet;
  887. if (*PSet == -1)
  888. PSet = nullptr;
  889. }
  890. };
  891. inline PSetIterator MachineRegisterInfo::
  892. getPressureSets(unsigned RegUnit) const {
  893. return PSetIterator(RegUnit, this);
  894. }
  895. } // End llvm namespace
  896. #endif