MachineInstrBundle.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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/Target/TargetMachine.h"
  18. #include <utility>
  19. using namespace llvm;
  20. namespace {
  21. class UnpackMachineBundles : public MachineFunctionPass {
  22. public:
  23. static char ID; // Pass identification
  24. UnpackMachineBundles(
  25. std::function<bool(const MachineFunction &)> Ftor = nullptr)
  26. : MachineFunctionPass(ID), PredicateFtor(std::move(Ftor)) {
  27. initializeUnpackMachineBundlesPass(*PassRegistry::getPassRegistry());
  28. }
  29. bool runOnMachineFunction(MachineFunction &MF) override;
  30. private:
  31. std::function<bool(const MachineFunction &)> PredicateFtor;
  32. };
  33. } // end anonymous namespace
  34. char UnpackMachineBundles::ID = 0;
  35. char &llvm::UnpackMachineBundlesID = UnpackMachineBundles::ID;
  36. INITIALIZE_PASS(UnpackMachineBundles, "unpack-mi-bundles",
  37. "Unpack machine instruction bundles", false, false)
  38. bool UnpackMachineBundles::runOnMachineFunction(MachineFunction &MF) {
  39. if (PredicateFtor && !PredicateFtor(MF))
  40. return false;
  41. bool Changed = false;
  42. for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I) {
  43. MachineBasicBlock *MBB = &*I;
  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<unsigned, 32> LocalDefs;
  117. SmallSet<unsigned, 32> LocalDefSet;
  118. SmallSet<unsigned, 8> DeadDefSet;
  119. SmallSet<unsigned, 16> KilledDefSet;
  120. SmallVector<unsigned, 8> ExternUses;
  121. SmallSet<unsigned, 8> ExternUseSet;
  122. SmallSet<unsigned, 8> KilledUseSet;
  123. SmallSet<unsigned, 8> UndefUseSet;
  124. SmallVector<MachineOperand*, 4> Defs;
  125. for (auto MII = FirstMI; MII != LastMI; ++MII) {
  126. for (unsigned i = 0, e = MII->getNumOperands(); i != e; ++i) {
  127. MachineOperand &MO = MII->getOperand(i);
  128. if (!MO.isReg())
  129. continue;
  130. if (MO.isDef()) {
  131. Defs.push_back(&MO);
  132. continue;
  133. }
  134. Register Reg = MO.getReg();
  135. if (!Reg)
  136. continue;
  137. if (LocalDefSet.count(Reg)) {
  138. MO.setIsInternalRead();
  139. if (MO.isKill())
  140. // Internal def is now killed.
  141. KilledDefSet.insert(Reg);
  142. } else {
  143. if (ExternUseSet.insert(Reg).second) {
  144. ExternUses.push_back(Reg);
  145. if (MO.isUndef())
  146. UndefUseSet.insert(Reg);
  147. }
  148. if (MO.isKill())
  149. // External def is now killed.
  150. KilledUseSet.insert(Reg);
  151. }
  152. }
  153. for (unsigned i = 0, e = Defs.size(); i != e; ++i) {
  154. MachineOperand &MO = *Defs[i];
  155. Register Reg = MO.getReg();
  156. if (!Reg)
  157. continue;
  158. if (LocalDefSet.insert(Reg).second) {
  159. LocalDefs.push_back(Reg);
  160. if (MO.isDead()) {
  161. DeadDefSet.insert(Reg);
  162. }
  163. } else {
  164. // Re-defined inside the bundle, it's no longer killed.
  165. KilledDefSet.erase(Reg);
  166. if (!MO.isDead())
  167. // Previously defined but dead.
  168. DeadDefSet.erase(Reg);
  169. }
  170. if (!MO.isDead() && Register::isPhysicalRegister(Reg)) {
  171. for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs) {
  172. unsigned SubReg = *SubRegs;
  173. if (LocalDefSet.insert(SubReg).second)
  174. LocalDefs.push_back(SubReg);
  175. }
  176. }
  177. }
  178. Defs.clear();
  179. }
  180. SmallSet<unsigned, 32> Added;
  181. for (unsigned i = 0, e = LocalDefs.size(); i != e; ++i) {
  182. unsigned Reg = LocalDefs[i];
  183. if (Added.insert(Reg).second) {
  184. // If it's not live beyond end of the bundle, mark it dead.
  185. bool isDead = DeadDefSet.count(Reg) || KilledDefSet.count(Reg);
  186. MIB.addReg(Reg, getDefRegState(true) | getDeadRegState(isDead) |
  187. getImplRegState(true));
  188. }
  189. }
  190. for (unsigned i = 0, e = ExternUses.size(); i != e; ++i) {
  191. unsigned Reg = ExternUses[i];
  192. bool isKill = KilledUseSet.count(Reg);
  193. bool isUndef = UndefUseSet.count(Reg);
  194. MIB.addReg(Reg, getKillRegState(isKill) | getUndefRegState(isUndef) |
  195. getImplRegState(true));
  196. }
  197. // Set FrameSetup/FrameDestroy for the bundle. If any of the instructions got
  198. // the property, then also set it on the bundle.
  199. for (auto MII = FirstMI; MII != LastMI; ++MII) {
  200. if (MII->getFlag(MachineInstr::FrameSetup))
  201. MIB.setMIFlag(MachineInstr::FrameSetup);
  202. if (MII->getFlag(MachineInstr::FrameDestroy))
  203. MIB.setMIFlag(MachineInstr::FrameDestroy);
  204. }
  205. }
  206. /// finalizeBundle - Same functionality as the previous finalizeBundle except
  207. /// the last instruction in the bundle is not provided as an input. This is
  208. /// used in cases where bundles are pre-determined by marking instructions
  209. /// with 'InsideBundle' marker. It returns the MBB instruction iterator that
  210. /// points to the end of the bundle.
  211. MachineBasicBlock::instr_iterator
  212. llvm::finalizeBundle(MachineBasicBlock &MBB,
  213. MachineBasicBlock::instr_iterator FirstMI) {
  214. MachineBasicBlock::instr_iterator E = MBB.instr_end();
  215. MachineBasicBlock::instr_iterator LastMI = std::next(FirstMI);
  216. while (LastMI != E && LastMI->isInsideBundle())
  217. ++LastMI;
  218. finalizeBundle(MBB, FirstMI, LastMI);
  219. return LastMI;
  220. }
  221. /// finalizeBundles - Finalize instruction bundles in the specified
  222. /// MachineFunction. Return true if any bundles are finalized.
  223. bool llvm::finalizeBundles(MachineFunction &MF) {
  224. bool Changed = false;
  225. for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I) {
  226. MachineBasicBlock &MBB = *I;
  227. MachineBasicBlock::instr_iterator MII = MBB.instr_begin();
  228. MachineBasicBlock::instr_iterator MIE = MBB.instr_end();
  229. if (MII == MIE)
  230. continue;
  231. assert(!MII->isInsideBundle() &&
  232. "First instr cannot be inside bundle before finalization!");
  233. for (++MII; MII != MIE; ) {
  234. if (!MII->isInsideBundle())
  235. ++MII;
  236. else {
  237. MII = finalizeBundle(MBB, std::prev(MII));
  238. Changed = true;
  239. }
  240. }
  241. }
  242. return Changed;
  243. }
  244. //===----------------------------------------------------------------------===//
  245. // MachineOperand iterator
  246. //===----------------------------------------------------------------------===//
  247. MachineOperandIteratorBase::VirtRegInfo
  248. MachineOperandIteratorBase::analyzeVirtReg(unsigned Reg,
  249. SmallVectorImpl<std::pair<MachineInstr*, unsigned> > *Ops) {
  250. VirtRegInfo RI = { false, false, false };
  251. for(; isValid(); ++*this) {
  252. MachineOperand &MO = deref();
  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(), 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 && MO.getParent()->isRegTiedToDefOperand(getOperandNo()))
  268. RI.Tied = true;
  269. }
  270. return RI;
  271. }
  272. MachineOperandIteratorBase::PhysRegInfo
  273. MachineOperandIteratorBase::analyzePhysReg(unsigned Reg,
  274. const TargetRegisterInfo *TRI) {
  275. bool AllDefsDead = true;
  276. PhysRegInfo PRI = {false, false, false, false, false, false, false, false};
  277. assert(Register::isPhysicalRegister(Reg) &&
  278. "analyzePhysReg not given a physical register!");
  279. for (; isValid(); ++*this) {
  280. MachineOperand &MO = deref();
  281. if (MO.isRegMask() && MO.clobbersPhysReg(Reg)) {
  282. PRI.Clobbered = true;
  283. continue;
  284. }
  285. if (!MO.isReg())
  286. continue;
  287. Register MOReg = MO.getReg();
  288. if (!MOReg || !Register::isPhysicalRegister(MOReg))
  289. continue;
  290. if (!TRI->regsOverlap(MOReg, Reg))
  291. continue;
  292. bool Covered = TRI->isSuperRegisterEq(Reg, MOReg);
  293. if (MO.readsReg()) {
  294. PRI.Read = true;
  295. if (Covered) {
  296. PRI.FullyRead = true;
  297. if (MO.isKill())
  298. PRI.Killed = true;
  299. }
  300. } else if (MO.isDef()) {
  301. PRI.Defined = true;
  302. if (Covered)
  303. PRI.FullyDefined = true;
  304. if (!MO.isDead())
  305. AllDefsDead = false;
  306. }
  307. }
  308. if (AllDefsDead) {
  309. if (PRI.FullyDefined || PRI.Clobbered)
  310. PRI.DeadDef = true;
  311. else if (PRI.Defined)
  312. PRI.PartialDeadDef = true;
  313. }
  314. return PRI;
  315. }