LivePhysRegs.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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/LiveRegUnits.h"
  16. #include "llvm/CodeGen/MachineFrameInfo.h"
  17. #include "llvm/CodeGen/MachineFunction.h"
  18. #include "llvm/CodeGen/MachineInstrBundle.h"
  19. #include "llvm/CodeGen/MachineRegisterInfo.h"
  20. #include "llvm/Config/llvm-config.h"
  21. #include "llvm/Support/Debug.h"
  22. #include "llvm/Support/raw_ostream.h"
  23. using namespace llvm;
  24. /// Remove all registers from the set that get clobbered by the register
  25. /// mask.
  26. /// The clobbers set will be the list of live registers clobbered
  27. /// by the regmask.
  28. void LivePhysRegs::removeRegsInMask(const MachineOperand &MO,
  29. SmallVectorImpl<std::pair<MCPhysReg, const MachineOperand*>> *Clobbers) {
  30. RegisterSet::iterator LRI = LiveRegs.begin();
  31. while (LRI != LiveRegs.end()) {
  32. if (MO.clobbersPhysReg(*LRI)) {
  33. if (Clobbers)
  34. Clobbers->push_back(std::make_pair(*LRI, &MO));
  35. LRI = LiveRegs.erase(LRI);
  36. } else
  37. ++LRI;
  38. }
  39. }
  40. /// Remove defined registers and regmask kills from the set.
  41. void LivePhysRegs::removeDefs(const MachineInstr &MI) {
  42. for (const MachineOperand &MOP : phys_regs_and_masks(MI)) {
  43. if (MOP.isRegMask()) {
  44. removeRegsInMask(MOP);
  45. continue;
  46. }
  47. if (MOP.isDef())
  48. removeReg(MOP.getReg());
  49. }
  50. }
  51. /// Add uses to the set.
  52. void LivePhysRegs::addUses(const MachineInstr &MI) {
  53. for (const MachineOperand &MOP : phys_regs_and_masks(MI)) {
  54. if (!MOP.isReg() || !MOP.readsReg())
  55. continue;
  56. addReg(MOP.getReg());
  57. }
  58. }
  59. /// Simulates liveness when stepping backwards over an instruction(bundle):
  60. /// Remove Defs, add uses. This is the recommended way of calculating liveness.
  61. void LivePhysRegs::stepBackward(const MachineInstr &MI) {
  62. // Remove defined registers and regmask kills from the set.
  63. removeDefs(MI);
  64. // Add uses to the set.
  65. addUses(MI);
  66. }
  67. /// Simulates liveness when stepping forward over an instruction(bundle): Remove
  68. /// killed-uses, add defs. This is the not recommended way, because it depends
  69. /// on accurate kill flags. If possible use stepBackward() instead of this
  70. /// function.
  71. void LivePhysRegs::stepForward(const MachineInstr &MI,
  72. SmallVectorImpl<std::pair<MCPhysReg, const MachineOperand*>> &Clobbers) {
  73. // Remove killed registers from the set.
  74. for (ConstMIBundleOperands O(MI); O.isValid(); ++O) {
  75. if (O->isReg()) {
  76. if (O->isDebug())
  77. continue;
  78. Register Reg = O->getReg();
  79. if (!Reg.isPhysical())
  80. continue;
  81. if (O->isDef()) {
  82. // Note, dead defs are still recorded. The caller should decide how to
  83. // handle them.
  84. Clobbers.push_back(std::make_pair(Reg, &*O));
  85. } else {
  86. assert(O->isUse());
  87. if (O->isKill())
  88. removeReg(Reg);
  89. }
  90. } else if (O->isRegMask()) {
  91. removeRegsInMask(*O, &Clobbers);
  92. }
  93. }
  94. // Add defs to the set.
  95. for (auto Reg : Clobbers) {
  96. // Skip dead defs and registers clobbered by regmasks. They shouldn't
  97. // be added to the set.
  98. if (Reg.second->isReg() && Reg.second->isDead())
  99. continue;
  100. if (Reg.second->isRegMask() &&
  101. MachineOperand::clobbersPhysReg(Reg.second->getRegMask(), Reg.first))
  102. continue;
  103. addReg(Reg.first);
  104. }
  105. }
  106. /// Print the currently live registers to OS.
  107. void LivePhysRegs::print(raw_ostream &OS) const {
  108. OS << "Live Registers:";
  109. if (!TRI) {
  110. OS << " (uninitialized)\n";
  111. return;
  112. }
  113. if (empty()) {
  114. OS << " (empty)\n";
  115. return;
  116. }
  117. for (MCPhysReg R : *this)
  118. OS << " " << printReg(R, TRI);
  119. OS << "\n";
  120. }
  121. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  122. LLVM_DUMP_METHOD void LivePhysRegs::dump() const {
  123. dbgs() << " " << *this;
  124. }
  125. #endif
  126. bool LivePhysRegs::available(const MachineRegisterInfo &MRI,
  127. MCPhysReg Reg) const {
  128. if (LiveRegs.count(Reg))
  129. return false;
  130. if (MRI.isReserved(Reg))
  131. return false;
  132. for (MCRegAliasIterator R(Reg, TRI, false); R.isValid(); ++R) {
  133. if (LiveRegs.count(*R))
  134. return false;
  135. }
  136. return true;
  137. }
  138. /// Add live-in registers of basic block \p MBB to \p LiveRegs.
  139. void LivePhysRegs::addBlockLiveIns(const MachineBasicBlock &MBB) {
  140. for (const auto &LI : MBB.liveins()) {
  141. MCPhysReg Reg = LI.PhysReg;
  142. LaneBitmask Mask = LI.LaneMask;
  143. MCSubRegIndexIterator S(Reg, TRI);
  144. assert(Mask.any() && "Invalid livein mask");
  145. if (Mask.all() || !S.isValid()) {
  146. addReg(Reg);
  147. continue;
  148. }
  149. for (; S.isValid(); ++S) {
  150. unsigned SI = S.getSubRegIndex();
  151. if ((Mask & TRI->getSubRegIndexLaneMask(SI)).any())
  152. addReg(S.getSubReg());
  153. }
  154. }
  155. }
  156. /// Adds all callee saved registers to \p LiveRegs.
  157. static void addCalleeSavedRegs(LivePhysRegs &LiveRegs,
  158. const MachineFunction &MF) {
  159. const MachineRegisterInfo &MRI = MF.getRegInfo();
  160. for (const MCPhysReg *CSR = MRI.getCalleeSavedRegs(); CSR && *CSR; ++CSR)
  161. LiveRegs.addReg(*CSR);
  162. }
  163. void LivePhysRegs::addPristines(const MachineFunction &MF) {
  164. const MachineFrameInfo &MFI = MF.getFrameInfo();
  165. if (!MFI.isCalleeSavedInfoValid())
  166. return;
  167. /// This function will usually be called on an empty object, handle this
  168. /// as a special case.
  169. if (empty()) {
  170. /// Add all callee saved regs, then remove the ones that are saved and
  171. /// restored.
  172. addCalleeSavedRegs(*this, MF);
  173. /// Remove the ones that are not saved/restored; they are pristine.
  174. for (const CalleeSavedInfo &Info : MFI.getCalleeSavedInfo())
  175. removeReg(Info.getReg());
  176. return;
  177. }
  178. /// If a callee-saved register that is not pristine is already present
  179. /// in the set, we should make sure that it stays in it. Precompute the
  180. /// set of pristine registers in a separate object.
  181. /// Add all callee saved regs, then remove the ones that are saved+restored.
  182. LivePhysRegs Pristine(*TRI);
  183. addCalleeSavedRegs(Pristine, MF);
  184. /// Remove the ones that are not saved/restored; they are pristine.
  185. for (const CalleeSavedInfo &Info : MFI.getCalleeSavedInfo())
  186. Pristine.removeReg(Info.getReg());
  187. for (MCPhysReg R : Pristine)
  188. addReg(R);
  189. }
  190. void LivePhysRegs::addLiveOutsNoPristines(const MachineBasicBlock &MBB) {
  191. // To get the live-outs we simply merge the live-ins of all successors.
  192. for (const MachineBasicBlock *Succ : MBB.successors())
  193. addBlockLiveIns(*Succ);
  194. if (MBB.isReturnBlock()) {
  195. // Return blocks are a special case because we currently don't mark up
  196. // return instructions completely: specifically, there is no explicit
  197. // use for callee-saved registers. So we add all callee saved registers
  198. // that are saved and restored (somewhere). This does not include
  199. // callee saved registers that are unused and hence not saved and
  200. // restored; they are called pristine.
  201. // FIXME: PEI should add explicit markings to return instructions
  202. // instead of implicitly handling them here.
  203. const MachineFunction &MF = *MBB.getParent();
  204. const MachineFrameInfo &MFI = MF.getFrameInfo();
  205. if (MFI.isCalleeSavedInfoValid()) {
  206. for (const CalleeSavedInfo &Info : MFI.getCalleeSavedInfo())
  207. if (Info.isRestored())
  208. addReg(Info.getReg());
  209. }
  210. }
  211. }
  212. void LivePhysRegs::addLiveOuts(const MachineBasicBlock &MBB) {
  213. const MachineFunction &MF = *MBB.getParent();
  214. addPristines(MF);
  215. addLiveOutsNoPristines(MBB);
  216. }
  217. void LivePhysRegs::addLiveIns(const MachineBasicBlock &MBB) {
  218. const MachineFunction &MF = *MBB.getParent();
  219. addPristines(MF);
  220. addBlockLiveIns(MBB);
  221. }
  222. void LivePhysRegs::addLiveInsNoPristines(const MachineBasicBlock &MBB) {
  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 : llvm::reverse(MBB))
  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. const MachineFrameInfo &MFI = MF.getFrameInfo();
  261. // We walk through the block backwards and start with the live outs.
  262. LivePhysRegs LiveRegs;
  263. LiveRegs.init(TRI);
  264. LiveRegs.addLiveOutsNoPristines(MBB);
  265. for (MachineInstr &MI : llvm::reverse(MBB)) {
  266. // Recompute dead flags.
  267. for (MIBundleOperands MO(MI); MO.isValid(); ++MO) {
  268. if (!MO->isReg() || !MO->isDef() || MO->isDebug())
  269. continue;
  270. Register Reg = MO->getReg();
  271. if (Reg == 0)
  272. continue;
  273. assert(Reg.isPhysical());
  274. bool IsNotLive = LiveRegs.available(MRI, Reg);
  275. // Special-case return instructions for cases when a return is not
  276. // the last instruction in the block.
  277. if (MI.isReturn() && MFI.isCalleeSavedInfoValid()) {
  278. for (const CalleeSavedInfo &Info : MFI.getCalleeSavedInfo()) {
  279. if (Info.getReg() == Reg) {
  280. IsNotLive = !Info.isRestored();
  281. break;
  282. }
  283. }
  284. }
  285. MO->setIsDead(IsNotLive);
  286. }
  287. // Step backward over defs.
  288. LiveRegs.removeDefs(MI);
  289. // Recompute kill flags.
  290. for (MIBundleOperands MO(MI); MO.isValid(); ++MO) {
  291. if (!MO->isReg() || !MO->readsReg() || MO->isDebug())
  292. continue;
  293. Register Reg = MO->getReg();
  294. if (Reg == 0)
  295. continue;
  296. assert(Reg.isPhysical());
  297. bool IsNotLive = LiveRegs.available(MRI, Reg);
  298. MO->setIsKill(IsNotLive);
  299. }
  300. // Complete the stepbackward.
  301. LiveRegs.addUses(MI);
  302. }
  303. }
  304. void llvm::computeAndAddLiveIns(LivePhysRegs &LiveRegs,
  305. MachineBasicBlock &MBB) {
  306. computeLiveIns(LiveRegs, MBB);
  307. addLiveIns(MBB, LiveRegs);
  308. }