MachineInstrBundle.cpp 11 KB

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