LivePhysRegs.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. //===--- LivePhysRegs.cpp - Live Physical Register Set --------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file implements the LivePhysRegs utility for tracking liveness of
  10. // physical registers across machine instructions in forward or backward order.
  11. // A more detailed description can be found in the corresponding header file.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "llvm/CodeGen/LivePhysRegs.h"
  15. #include "llvm/CodeGen/MachineFrameInfo.h"
  16. #include "llvm/CodeGen/MachineFunction.h"
  17. #include "llvm/CodeGen/MachineInstrBundle.h"
  18. #include "llvm/CodeGen/MachineRegisterInfo.h"
  19. #include "llvm/Config/llvm-config.h"
  20. #include "llvm/Support/Debug.h"
  21. #include "llvm/Support/raw_ostream.h"
  22. using namespace llvm;
  23. /// Remove all registers from the set that get clobbered by the register
  24. /// mask.
  25. /// The clobbers set will be the list of live registers clobbered
  26. /// by the regmask.
  27. void LivePhysRegs::removeRegsInMask(const MachineOperand &MO,
  28. SmallVectorImpl<std::pair<MCPhysReg, const MachineOperand*>> *Clobbers) {
  29. RegisterSet::iterator LRI = LiveRegs.begin();
  30. while (LRI != LiveRegs.end()) {
  31. if (MO.clobbersPhysReg(*LRI)) {
  32. if (Clobbers)
  33. Clobbers->push_back(std::make_pair(*LRI, &MO));
  34. LRI = LiveRegs.erase(LRI);
  35. } else
  36. ++LRI;
  37. }
  38. }
  39. /// Remove defined registers and regmask kills from the set.
  40. void LivePhysRegs::removeDefs(const MachineInstr &MI) {
  41. for (ConstMIBundleOperands O(MI); O.isValid(); ++O) {
  42. if (O->isReg()) {
  43. if (!O->isDef() || O->isDebug())
  44. continue;
  45. Register Reg = O->getReg();
  46. if (!Register::isPhysicalRegister(Reg))
  47. continue;
  48. removeReg(Reg);
  49. } else if (O->isRegMask())
  50. removeRegsInMask(*O);
  51. }
  52. }
  53. /// Add uses to the set.
  54. void LivePhysRegs::addUses(const MachineInstr &MI) {
  55. for (ConstMIBundleOperands O(MI); O.isValid(); ++O) {
  56. if (!O->isReg() || !O->readsReg() || O->isDebug())
  57. continue;
  58. Register Reg = O->getReg();
  59. if (!Register::isPhysicalRegister(Reg))
  60. continue;
  61. addReg(Reg);
  62. }
  63. }
  64. /// Simulates liveness when stepping backwards over an instruction(bundle):
  65. /// Remove Defs, add uses. This is the recommended way of calculating liveness.
  66. void LivePhysRegs::stepBackward(const MachineInstr &MI) {
  67. // Remove defined registers and regmask kills from the set.
  68. removeDefs(MI);
  69. // Add uses to the set.
  70. addUses(MI);
  71. }
  72. /// Simulates liveness when stepping forward over an instruction(bundle): Remove
  73. /// killed-uses, add defs. This is the not recommended way, because it depends
  74. /// on accurate kill flags. If possible use stepBackward() instead of this
  75. /// function.
  76. void LivePhysRegs::stepForward(const MachineInstr &MI,
  77. SmallVectorImpl<std::pair<MCPhysReg, const MachineOperand*>> &Clobbers) {
  78. // Remove killed registers from the set.
  79. for (ConstMIBundleOperands O(MI); O.isValid(); ++O) {
  80. if (O->isReg() && !O->isDebug()) {
  81. Register Reg = O->getReg();
  82. if (!Register::isPhysicalRegister(Reg))
  83. continue;
  84. if (O->isDef()) {
  85. // Note, dead defs are still recorded. The caller should decide how to
  86. // handle them.
  87. Clobbers.push_back(std::make_pair(Reg, &*O));
  88. } else {
  89. if (!O->isKill())
  90. continue;
  91. assert(O->isUse());
  92. removeReg(Reg);
  93. }
  94. } else if (O->isRegMask())
  95. removeRegsInMask(*O, &Clobbers);
  96. }
  97. // Add defs to the set.
  98. for (auto Reg : Clobbers) {
  99. // Skip dead defs and registers clobbered by regmasks. They shouldn't
  100. // be added to the set.
  101. if (Reg.second->isReg() && Reg.second->isDead())
  102. continue;
  103. if (Reg.second->isRegMask() &&
  104. MachineOperand::clobbersPhysReg(Reg.second->getRegMask(), Reg.first))
  105. continue;
  106. addReg(Reg.first);
  107. }
  108. }
  109. /// Prin the currently live registers to OS.
  110. void LivePhysRegs::print(raw_ostream &OS) const {
  111. OS << "Live Registers:";
  112. if (!TRI) {
  113. OS << " (uninitialized)\n";
  114. return;
  115. }
  116. if (empty()) {
  117. OS << " (empty)\n";
  118. return;
  119. }
  120. for (const_iterator I = begin(), E = end(); I != E; ++I)
  121. OS << " " << printReg(*I, TRI);
  122. OS << "\n";
  123. }
  124. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  125. LLVM_DUMP_METHOD void LivePhysRegs::dump() const {
  126. dbgs() << " " << *this;
  127. }
  128. #endif
  129. bool LivePhysRegs::available(const MachineRegisterInfo &MRI,
  130. MCPhysReg Reg) const {
  131. if (LiveRegs.count(Reg))
  132. return false;
  133. if (MRI.isReserved(Reg))
  134. return false;
  135. for (MCRegAliasIterator R(Reg, TRI, false); R.isValid(); ++R) {
  136. if (LiveRegs.count(*R))
  137. return false;
  138. }
  139. return true;
  140. }
  141. /// Add live-in registers of basic block \p MBB to \p LiveRegs.
  142. void LivePhysRegs::addBlockLiveIns(const MachineBasicBlock &MBB) {
  143. for (const auto &LI : MBB.liveins()) {
  144. MCPhysReg Reg = LI.PhysReg;
  145. LaneBitmask Mask = LI.LaneMask;
  146. MCSubRegIndexIterator S(Reg, TRI);
  147. assert(Mask.any() && "Invalid livein mask");
  148. if (Mask.all() || !S.isValid()) {
  149. addReg(Reg);
  150. continue;
  151. }
  152. for (; S.isValid(); ++S) {
  153. unsigned SI = S.getSubRegIndex();
  154. if ((Mask & TRI->getSubRegIndexLaneMask(SI)).any())
  155. addReg(S.getSubReg());
  156. }
  157. }
  158. }
  159. /// Adds all callee saved registers to \p LiveRegs.
  160. static void addCalleeSavedRegs(LivePhysRegs &LiveRegs,
  161. const MachineFunction &MF) {
  162. const MachineRegisterInfo &MRI = MF.getRegInfo();
  163. for (const MCPhysReg *CSR = MRI.getCalleeSavedRegs(); CSR && *CSR; ++CSR)
  164. LiveRegs.addReg(*CSR);
  165. }
  166. void LivePhysRegs::addPristines(const MachineFunction &MF) {
  167. const MachineFrameInfo &MFI = MF.getFrameInfo();
  168. if (!MFI.isCalleeSavedInfoValid())
  169. return;
  170. /// This function will usually be called on an empty object, handle this
  171. /// as a special case.
  172. if (empty()) {
  173. /// Add all callee saved regs, then remove the ones that are saved and
  174. /// restored.
  175. addCalleeSavedRegs(*this, MF);
  176. /// Remove the ones that are not saved/restored; they are pristine.
  177. for (const CalleeSavedInfo &Info : MFI.getCalleeSavedInfo())
  178. removeReg(Info.getReg());
  179. return;
  180. }
  181. /// If a callee-saved register that is not pristine is already present
  182. /// in the set, we should make sure that it stays in it. Precompute the
  183. /// set of pristine registers in a separate object.
  184. /// Add all callee saved regs, then remove the ones that are saved+restored.
  185. LivePhysRegs Pristine(*TRI);
  186. addCalleeSavedRegs(Pristine, MF);
  187. /// Remove the ones that are not saved/restored; they are pristine.
  188. for (const CalleeSavedInfo &Info : MFI.getCalleeSavedInfo())
  189. Pristine.removeReg(Info.getReg());
  190. for (MCPhysReg R : Pristine)
  191. addReg(R);
  192. }
  193. void LivePhysRegs::addLiveOutsNoPristines(const MachineBasicBlock &MBB) {
  194. // To get the live-outs we simply merge the live-ins of all successors.
  195. for (const MachineBasicBlock *Succ : MBB.successors())
  196. addBlockLiveIns(*Succ);
  197. if (MBB.isReturnBlock()) {
  198. // Return blocks are a special case because we currently don't mark up
  199. // return instructions completely: specifically, there is no explicit
  200. // use for callee-saved registers. So we add all callee saved registers
  201. // that are saved and restored (somewhere). This does not include
  202. // callee saved registers that are unused and hence not saved and
  203. // restored; they are called pristine.
  204. // FIXME: PEI should add explicit markings to return instructions
  205. // instead of implicitly handling them here.
  206. const MachineFunction &MF = *MBB.getParent();
  207. const MachineFrameInfo &MFI = MF.getFrameInfo();
  208. if (MFI.isCalleeSavedInfoValid()) {
  209. for (const CalleeSavedInfo &Info : MFI.getCalleeSavedInfo())
  210. if (Info.isRestored())
  211. addReg(Info.getReg());
  212. }
  213. }
  214. }
  215. void LivePhysRegs::addLiveOuts(const MachineBasicBlock &MBB) {
  216. const MachineFunction &MF = *MBB.getParent();
  217. addPristines(MF);
  218. addLiveOutsNoPristines(MBB);
  219. }
  220. void LivePhysRegs::addLiveIns(const MachineBasicBlock &MBB) {
  221. const MachineFunction &MF = *MBB.getParent();
  222. addPristines(MF);
  223. addBlockLiveIns(MBB);
  224. }
  225. void llvm::computeLiveIns(LivePhysRegs &LiveRegs,
  226. const MachineBasicBlock &MBB) {
  227. const MachineFunction &MF = *MBB.getParent();
  228. const MachineRegisterInfo &MRI = MF.getRegInfo();
  229. const TargetRegisterInfo &TRI = *MRI.getTargetRegisterInfo();
  230. LiveRegs.init(TRI);
  231. LiveRegs.addLiveOutsNoPristines(MBB);
  232. for (const MachineInstr &MI : make_range(MBB.rbegin(), MBB.rend()))
  233. LiveRegs.stepBackward(MI);
  234. }
  235. void llvm::addLiveIns(MachineBasicBlock &MBB, const LivePhysRegs &LiveRegs) {
  236. assert(MBB.livein_empty() && "Expected empty live-in list");
  237. const MachineFunction &MF = *MBB.getParent();
  238. const MachineRegisterInfo &MRI = MF.getRegInfo();
  239. const TargetRegisterInfo &TRI = *MRI.getTargetRegisterInfo();
  240. for (MCPhysReg Reg : LiveRegs) {
  241. if (MRI.isReserved(Reg))
  242. continue;
  243. // Skip the register if we are about to add one of its super registers.
  244. bool ContainsSuperReg = false;
  245. for (MCSuperRegIterator SReg(Reg, &TRI); SReg.isValid(); ++SReg) {
  246. if (LiveRegs.contains(*SReg) && !MRI.isReserved(*SReg)) {
  247. ContainsSuperReg = true;
  248. break;
  249. }
  250. }
  251. if (ContainsSuperReg)
  252. continue;
  253. MBB.addLiveIn(Reg);
  254. }
  255. }
  256. void llvm::recomputeLivenessFlags(MachineBasicBlock &MBB) {
  257. const MachineFunction &MF = *MBB.getParent();
  258. const MachineRegisterInfo &MRI = MF.getRegInfo();
  259. const TargetRegisterInfo &TRI = *MRI.getTargetRegisterInfo();
  260. // We walk through the block backwards and start with the live outs.
  261. LivePhysRegs LiveRegs;
  262. LiveRegs.init(TRI);
  263. LiveRegs.addLiveOutsNoPristines(MBB);
  264. for (MachineInstr &MI : make_range(MBB.rbegin(), MBB.rend())) {
  265. // Recompute dead flags.
  266. for (MIBundleOperands MO(MI); MO.isValid(); ++MO) {
  267. if (!MO->isReg() || !MO->isDef() || MO->isDebug())
  268. continue;
  269. Register Reg = MO->getReg();
  270. if (Reg == 0)
  271. continue;
  272. assert(Register::isPhysicalRegister(Reg));
  273. bool IsNotLive = LiveRegs.available(MRI, Reg);
  274. MO->setIsDead(IsNotLive);
  275. }
  276. // Step backward over defs.
  277. LiveRegs.removeDefs(MI);
  278. // Recompute kill flags.
  279. for (MIBundleOperands MO(MI); MO.isValid(); ++MO) {
  280. if (!MO->isReg() || !MO->readsReg() || MO->isDebug())
  281. continue;
  282. Register Reg = MO->getReg();
  283. if (Reg == 0)
  284. continue;
  285. assert(Register::isPhysicalRegister(Reg));
  286. bool IsNotLive = LiveRegs.available(MRI, Reg);
  287. MO->setIsKill(IsNotLive);
  288. }
  289. // Complete the stepbackward.
  290. LiveRegs.addUses(MI);
  291. }
  292. }
  293. void llvm::computeAndAddLiveIns(LivePhysRegs &LiveRegs,
  294. MachineBasicBlock &MBB) {
  295. computeLiveIns(LiveRegs, MBB);
  296. addLiveIns(MBB, LiveRegs);
  297. }