MachineInstrBundle.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. //===-- lib/CodeGen/MachineInstrBundle.cpp --------------------------------===//
  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. #include "llvm/CodeGen/MachineInstrBundle.h"
  9. #include "llvm/ADT/SmallSet.h"
  10. #include "llvm/ADT/SmallVector.h"
  11. #include "llvm/CodeGen/MachineFunctionPass.h"
  12. #include "llvm/CodeGen/MachineInstrBuilder.h"
  13. #include "llvm/CodeGen/Passes.h"
  14. #include "llvm/CodeGen/TargetInstrInfo.h"
  15. #include "llvm/CodeGen/TargetRegisterInfo.h"
  16. #include "llvm/CodeGen/TargetSubtargetInfo.h"
  17. #include "llvm/InitializePasses.h"
  18. #include "llvm/Pass.h"
  19. #include "llvm/PassRegistry.h"
  20. #include <utility>
  21. using namespace llvm;
  22. namespace {
  23. class UnpackMachineBundles : public MachineFunctionPass {
  24. public:
  25. static char ID; // Pass identification
  26. UnpackMachineBundles(
  27. std::function<bool(const MachineFunction &)> Ftor = nullptr)
  28. : MachineFunctionPass(ID), PredicateFtor(std::move(Ftor)) {
  29. initializeUnpackMachineBundlesPass(*PassRegistry::getPassRegistry());
  30. }
  31. bool runOnMachineFunction(MachineFunction &MF) override;
  32. private:
  33. std::function<bool(const MachineFunction &)> PredicateFtor;
  34. };
  35. } // end anonymous namespace
  36. char UnpackMachineBundles::ID = 0;
  37. char &llvm::UnpackMachineBundlesID = UnpackMachineBundles::ID;
  38. INITIALIZE_PASS(UnpackMachineBundles, "unpack-mi-bundles",
  39. "Unpack machine instruction bundles", false, false)
  40. bool UnpackMachineBundles::runOnMachineFunction(MachineFunction &MF) {
  41. if (PredicateFtor && !PredicateFtor(MF))
  42. return false;
  43. bool Changed = false;
  44. for (MachineBasicBlock &MBB : MF) {
  45. for (MachineBasicBlock::instr_iterator MII = MBB.instr_begin(),
  46. MIE = MBB.instr_end(); MII != MIE; ) {
  47. MachineInstr *MI = &*MII;
  48. // Remove BUNDLE instruction and the InsideBundle flags from bundled
  49. // instructions.
  50. if (MI->isBundle()) {
  51. while (++MII != MIE && MII->isBundledWithPred()) {
  52. MII->unbundleFromPred();
  53. for (unsigned i = 0, e = MII->getNumOperands(); i != e; ++i) {
  54. MachineOperand &MO = MII->getOperand(i);
  55. if (MO.isReg() && MO.isInternalRead())
  56. MO.setIsInternalRead(false);
  57. }
  58. }
  59. MI->eraseFromParent();
  60. Changed = true;
  61. continue;
  62. }
  63. ++MII;
  64. }
  65. }
  66. return Changed;
  67. }
  68. FunctionPass *
  69. llvm::createUnpackMachineBundles(
  70. std::function<bool(const MachineFunction &)> Ftor) {
  71. return new UnpackMachineBundles(std::move(Ftor));
  72. }
  73. namespace {
  74. class FinalizeMachineBundles : public MachineFunctionPass {
  75. public:
  76. static char ID; // Pass identification
  77. FinalizeMachineBundles() : MachineFunctionPass(ID) {
  78. initializeFinalizeMachineBundlesPass(*PassRegistry::getPassRegistry());
  79. }
  80. bool runOnMachineFunction(MachineFunction &MF) override;
  81. };
  82. } // end anonymous namespace
  83. char FinalizeMachineBundles::ID = 0;
  84. char &llvm::FinalizeMachineBundlesID = FinalizeMachineBundles::ID;
  85. INITIALIZE_PASS(FinalizeMachineBundles, "finalize-mi-bundles",
  86. "Finalize machine instruction bundles", false, false)
  87. bool FinalizeMachineBundles::runOnMachineFunction(MachineFunction &MF) {
  88. return llvm::finalizeBundles(MF);
  89. }
  90. /// Return the first found DebugLoc that has a DILocation, given a range of
  91. /// instructions. The search range is from FirstMI to LastMI (exclusive). If no
  92. /// DILocation is found, then an empty location is returned.
  93. static DebugLoc getDebugLoc(MachineBasicBlock::instr_iterator FirstMI,
  94. MachineBasicBlock::instr_iterator LastMI) {
  95. for (auto MII = FirstMI; MII != LastMI; ++MII)
  96. if (MII->getDebugLoc())
  97. return MII->getDebugLoc();
  98. return DebugLoc();
  99. }
  100. /// finalizeBundle - Finalize a machine instruction bundle which includes
  101. /// a sequence of instructions starting from FirstMI to LastMI (exclusive).
  102. /// This routine adds a BUNDLE instruction to represent the bundle, it adds
  103. /// IsInternalRead markers to MachineOperands which are defined inside the
  104. /// bundle, and it copies externally visible defs and uses to the BUNDLE
  105. /// instruction.
  106. void llvm::finalizeBundle(MachineBasicBlock &MBB,
  107. MachineBasicBlock::instr_iterator FirstMI,
  108. MachineBasicBlock::instr_iterator LastMI) {
  109. assert(FirstMI != LastMI && "Empty bundle?");
  110. MIBundleBuilder Bundle(MBB, FirstMI, LastMI);
  111. MachineFunction &MF = *MBB.getParent();
  112. const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo();
  113. const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
  114. MachineInstrBuilder MIB =
  115. BuildMI(MF, getDebugLoc(FirstMI, LastMI), TII->get(TargetOpcode::BUNDLE));
  116. Bundle.prepend(MIB);
  117. SmallVector<Register, 32> LocalDefs;
  118. SmallSet<Register, 32> LocalDefSet;
  119. SmallSet<Register, 8> DeadDefSet;
  120. SmallSet<Register, 16> KilledDefSet;
  121. SmallVector<Register, 8> ExternUses;
  122. SmallSet<Register, 8> ExternUseSet;
  123. SmallSet<Register, 8> KilledUseSet;
  124. SmallSet<Register, 8> UndefUseSet;
  125. SmallVector<MachineOperand*, 4> Defs;
  126. for (auto MII = FirstMI; MII != LastMI; ++MII) {
  127. // Debug instructions have no effects to track.
  128. if (MII->isDebugInstr())
  129. continue;
  130. for (unsigned i = 0, e = MII->getNumOperands(); i != e; ++i) {
  131. MachineOperand &MO = MII->getOperand(i);
  132. if (!MO.isReg())
  133. continue;
  134. if (MO.isDef()) {
  135. Defs.push_back(&MO);
  136. continue;
  137. }
  138. Register Reg = MO.getReg();
  139. if (!Reg)
  140. continue;
  141. if (LocalDefSet.count(Reg)) {
  142. MO.setIsInternalRead();
  143. if (MO.isKill())
  144. // Internal def is now killed.
  145. KilledDefSet.insert(Reg);
  146. } else {
  147. if (ExternUseSet.insert(Reg).second) {
  148. ExternUses.push_back(Reg);
  149. if (MO.isUndef())
  150. UndefUseSet.insert(Reg);
  151. }
  152. if (MO.isKill())
  153. // External def is now killed.
  154. KilledUseSet.insert(Reg);
  155. }
  156. }
  157. for (unsigned i = 0, e = Defs.size(); i != e; ++i) {
  158. MachineOperand &MO = *Defs[i];
  159. Register Reg = MO.getReg();
  160. if (!Reg)
  161. continue;
  162. if (LocalDefSet.insert(Reg).second) {
  163. LocalDefs.push_back(Reg);
  164. if (MO.isDead()) {
  165. DeadDefSet.insert(Reg);
  166. }
  167. } else {
  168. // Re-defined inside the bundle, it's no longer killed.
  169. KilledDefSet.erase(Reg);
  170. if (!MO.isDead())
  171. // Previously defined but dead.
  172. DeadDefSet.erase(Reg);
  173. }
  174. if (!MO.isDead() && Reg.isPhysical()) {
  175. for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs) {
  176. unsigned SubReg = *SubRegs;
  177. if (LocalDefSet.insert(SubReg).second)
  178. LocalDefs.push_back(SubReg);
  179. }
  180. }
  181. }
  182. Defs.clear();
  183. }
  184. SmallSet<Register, 32> Added;
  185. for (unsigned i = 0, e = LocalDefs.size(); i != e; ++i) {
  186. Register Reg = LocalDefs[i];
  187. if (Added.insert(Reg).second) {
  188. // If it's not live beyond end of the bundle, mark it dead.
  189. bool isDead = DeadDefSet.count(Reg) || KilledDefSet.count(Reg);
  190. MIB.addReg(Reg, getDefRegState(true) | getDeadRegState(isDead) |
  191. getImplRegState(true));
  192. }
  193. }
  194. for (unsigned i = 0, e = ExternUses.size(); i != e; ++i) {
  195. Register Reg = ExternUses[i];
  196. bool isKill = KilledUseSet.count(Reg);
  197. bool isUndef = UndefUseSet.count(Reg);
  198. MIB.addReg(Reg, getKillRegState(isKill) | getUndefRegState(isUndef) |
  199. getImplRegState(true));
  200. }
  201. // Set FrameSetup/FrameDestroy for the bundle. If any of the instructions got
  202. // the property, then also set it on the bundle.
  203. for (auto MII = FirstMI; MII != LastMI; ++MII) {
  204. if (MII->getFlag(MachineInstr::FrameSetup))
  205. MIB.setMIFlag(MachineInstr::FrameSetup);
  206. if (MII->getFlag(MachineInstr::FrameDestroy))
  207. MIB.setMIFlag(MachineInstr::FrameDestroy);
  208. }
  209. }
  210. /// finalizeBundle - Same functionality as the previous finalizeBundle except
  211. /// the last instruction in the bundle is not provided as an input. This is
  212. /// used in cases where bundles are pre-determined by marking instructions
  213. /// with 'InsideBundle' marker. It returns the MBB instruction iterator that
  214. /// points to the end of the bundle.
  215. MachineBasicBlock::instr_iterator
  216. llvm::finalizeBundle(MachineBasicBlock &MBB,
  217. MachineBasicBlock::instr_iterator FirstMI) {
  218. MachineBasicBlock::instr_iterator E = MBB.instr_end();
  219. MachineBasicBlock::instr_iterator LastMI = std::next(FirstMI);
  220. while (LastMI != E && LastMI->isInsideBundle())
  221. ++LastMI;
  222. finalizeBundle(MBB, FirstMI, LastMI);
  223. return LastMI;
  224. }
  225. /// finalizeBundles - Finalize instruction bundles in the specified
  226. /// MachineFunction. Return true if any bundles are finalized.
  227. bool llvm::finalizeBundles(MachineFunction &MF) {
  228. bool Changed = false;
  229. for (MachineBasicBlock &MBB : MF) {
  230. MachineBasicBlock::instr_iterator MII = MBB.instr_begin();
  231. MachineBasicBlock::instr_iterator MIE = MBB.instr_end();
  232. if (MII == MIE)
  233. continue;
  234. assert(!MII->isInsideBundle() &&
  235. "First instr cannot be inside bundle before finalization!");
  236. for (++MII; MII != MIE; ) {
  237. if (!MII->isInsideBundle())
  238. ++MII;
  239. else {
  240. MII = finalizeBundle(MBB, std::prev(MII));
  241. Changed = true;
  242. }
  243. }
  244. }
  245. return Changed;
  246. }
  247. VirtRegInfo llvm::AnalyzeVirtRegInBundle(
  248. MachineInstr &MI, Register Reg,
  249. SmallVectorImpl<std::pair<MachineInstr *, unsigned>> *Ops) {
  250. VirtRegInfo RI = {false, false, false};
  251. for (MIBundleOperands O(MI); O.isValid(); ++O) {
  252. MachineOperand &MO = *O;
  253. if (!MO.isReg() || MO.getReg() != Reg)
  254. continue;
  255. // Remember each (MI, OpNo) that refers to Reg.
  256. if (Ops)
  257. Ops->push_back(std::make_pair(MO.getParent(), O.getOperandNo()));
  258. // Both defs and uses can read virtual registers.
  259. if (MO.readsReg()) {
  260. RI.Reads = true;
  261. if (MO.isDef())
  262. RI.Tied = true;
  263. }
  264. // Only defs can write.
  265. if (MO.isDef())
  266. RI.Writes = true;
  267. else if (!RI.Tied &&
  268. MO.getParent()->isRegTiedToDefOperand(O.getOperandNo()))
  269. RI.Tied = true;
  270. }
  271. return RI;
  272. }
  273. PhysRegInfo llvm::AnalyzePhysRegInBundle(const MachineInstr &MI, Register Reg,
  274. const TargetRegisterInfo *TRI) {
  275. bool AllDefsDead = true;
  276. PhysRegInfo PRI = {false, false, false, false, false, false, false, false};
  277. assert(Reg.isPhysical() && "analyzePhysReg not given a physical register!");
  278. for (ConstMIBundleOperands O(MI); O.isValid(); ++O) {
  279. const MachineOperand &MO = *O;
  280. if (MO.isRegMask() && MO.clobbersPhysReg(Reg)) {
  281. PRI.Clobbered = true;
  282. continue;
  283. }
  284. if (!MO.isReg())
  285. continue;
  286. Register MOReg = MO.getReg();
  287. if (!MOReg || !MOReg.isPhysical())
  288. continue;
  289. if (!TRI->regsOverlap(MOReg, Reg))
  290. continue;
  291. bool Covered = TRI->isSuperRegisterEq(Reg, MOReg);
  292. if (MO.readsReg()) {
  293. PRI.Read = true;
  294. if (Covered) {
  295. PRI.FullyRead = true;
  296. if (MO.isKill())
  297. PRI.Killed = true;
  298. }
  299. } else if (MO.isDef()) {
  300. PRI.Defined = true;
  301. if (Covered)
  302. PRI.FullyDefined = true;
  303. if (!MO.isDead())
  304. AllDefsDead = false;
  305. }
  306. }
  307. if (AllDefsDead) {
  308. if (PRI.FullyDefined || PRI.Clobbered)
  309. PRI.DeadDef = true;
  310. else if (PRI.Defined)
  311. PRI.PartialDeadDef = true;
  312. }
  313. return PRI;
  314. }