MachineInstrBundle.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. //===-- lib/CodeGen/MachineInstrBundle.cpp --------------------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #include "llvm/CodeGen/MachineInstrBundle.h"
  10. #include "llvm/CodeGen/MachineInstrBuilder.h"
  11. #include "llvm/CodeGen/Passes.h"
  12. #include "llvm/CodeGen/MachineFunctionPass.h"
  13. #include "llvm/Target/TargetInstrInfo.h"
  14. #include "llvm/Target/TargetMachine.h"
  15. #include "llvm/Target/TargetRegisterInfo.h"
  16. #include "llvm/ADT/SmallSet.h"
  17. #include "llvm/ADT/SmallVector.h"
  18. using namespace llvm;
  19. namespace {
  20. class UnpackMachineBundles : public MachineFunctionPass {
  21. public:
  22. static char ID; // Pass identification
  23. UnpackMachineBundles() : MachineFunctionPass(ID) {
  24. initializeUnpackMachineBundlesPass(*PassRegistry::getPassRegistry());
  25. }
  26. virtual bool runOnMachineFunction(MachineFunction &MF);
  27. };
  28. } // end anonymous namespace
  29. char UnpackMachineBundles::ID = 0;
  30. char &llvm::UnpackMachineBundlesID = UnpackMachineBundles::ID;
  31. INITIALIZE_PASS(UnpackMachineBundles, "unpack-mi-bundles",
  32. "Unpack machine instruction bundles", false, false)
  33. bool UnpackMachineBundles::runOnMachineFunction(MachineFunction &MF) {
  34. bool Changed = false;
  35. for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I) {
  36. MachineBasicBlock *MBB = &*I;
  37. for (MachineBasicBlock::instr_iterator MII = MBB->instr_begin(),
  38. MIE = MBB->instr_end(); MII != MIE; ) {
  39. MachineInstr *MI = &*MII;
  40. // Remove BUNDLE instruction and the InsideBundle flags from bundled
  41. // instructions.
  42. if (MI->isBundle()) {
  43. while (++MII != MIE && MII->isInsideBundle()) {
  44. MII->setIsInsideBundle(false);
  45. for (unsigned i = 0, e = MII->getNumOperands(); i != e; ++i) {
  46. MachineOperand &MO = MII->getOperand(i);
  47. if (MO.isReg() && MO.isInternalRead())
  48. MO.setIsInternalRead(false);
  49. }
  50. }
  51. MI->eraseFromParent();
  52. Changed = true;
  53. continue;
  54. }
  55. ++MII;
  56. }
  57. }
  58. return Changed;
  59. }
  60. namespace {
  61. class FinalizeMachineBundles : public MachineFunctionPass {
  62. public:
  63. static char ID; // Pass identification
  64. FinalizeMachineBundles() : MachineFunctionPass(ID) {
  65. initializeFinalizeMachineBundlesPass(*PassRegistry::getPassRegistry());
  66. }
  67. virtual bool runOnMachineFunction(MachineFunction &MF);
  68. };
  69. } // end anonymous namespace
  70. char FinalizeMachineBundles::ID = 0;
  71. char &llvm::FinalizeMachineBundlesID = FinalizeMachineBundles::ID;
  72. INITIALIZE_PASS(FinalizeMachineBundles, "finalize-mi-bundles",
  73. "Finalize machine instruction bundles", false, false)
  74. bool FinalizeMachineBundles::runOnMachineFunction(MachineFunction &MF) {
  75. return llvm::finalizeBundles(MF);
  76. }
  77. /// finalizeBundle - Finalize a machine instruction bundle which includes
  78. /// a sequence of instructions starting from FirstMI to LastMI (exclusive).
  79. /// This routine adds a BUNDLE instruction to represent the bundle, it adds
  80. /// IsInternalRead markers to MachineOperands which are defined inside the
  81. /// bundle, and it copies externally visible defs and uses to the BUNDLE
  82. /// instruction.
  83. void llvm::finalizeBundle(MachineBasicBlock &MBB,
  84. MachineBasicBlock::instr_iterator FirstMI,
  85. MachineBasicBlock::instr_iterator LastMI) {
  86. assert(FirstMI != LastMI && "Empty bundle?");
  87. const TargetMachine &TM = MBB.getParent()->getTarget();
  88. const TargetInstrInfo *TII = TM.getInstrInfo();
  89. const TargetRegisterInfo *TRI = TM.getRegisterInfo();
  90. MachineInstrBuilder MIB = BuildMI(MBB, FirstMI, FirstMI->getDebugLoc(),
  91. TII->get(TargetOpcode::BUNDLE));
  92. SmallVector<unsigned, 8> LocalDefs;
  93. SmallSet<unsigned, 8> LocalDefSet;
  94. SmallSet<unsigned, 8> DeadDefSet;
  95. SmallSet<unsigned, 8> KilledDefSet;
  96. SmallVector<unsigned, 8> ExternUses;
  97. SmallSet<unsigned, 8> ExternUseSet;
  98. SmallSet<unsigned, 8> KilledUseSet;
  99. SmallSet<unsigned, 8> UndefUseSet;
  100. SmallVector<MachineOperand*, 4> Defs;
  101. for (; FirstMI != LastMI; ++FirstMI) {
  102. for (unsigned i = 0, e = FirstMI->getNumOperands(); i != e; ++i) {
  103. MachineOperand &MO = FirstMI->getOperand(i);
  104. if (!MO.isReg())
  105. continue;
  106. if (MO.isDef()) {
  107. Defs.push_back(&MO);
  108. continue;
  109. }
  110. unsigned Reg = MO.getReg();
  111. if (!Reg)
  112. continue;
  113. assert(TargetRegisterInfo::isPhysicalRegister(Reg));
  114. if (LocalDefSet.count(Reg)) {
  115. MO.setIsInternalRead();
  116. if (MO.isKill())
  117. // Internal def is now killed.
  118. KilledDefSet.insert(Reg);
  119. } else {
  120. if (ExternUseSet.insert(Reg)) {
  121. ExternUses.push_back(Reg);
  122. if (MO.isUndef())
  123. UndefUseSet.insert(Reg);
  124. }
  125. if (MO.isKill())
  126. // External def is now killed.
  127. KilledUseSet.insert(Reg);
  128. }
  129. }
  130. for (unsigned i = 0, e = Defs.size(); i != e; ++i) {
  131. MachineOperand &MO = *Defs[i];
  132. unsigned Reg = MO.getReg();
  133. if (!Reg)
  134. continue;
  135. if (LocalDefSet.insert(Reg)) {
  136. LocalDefs.push_back(Reg);
  137. if (MO.isDead()) {
  138. DeadDefSet.insert(Reg);
  139. }
  140. } else {
  141. // Re-defined inside the bundle, it's no longer killed.
  142. KilledDefSet.erase(Reg);
  143. if (!MO.isDead())
  144. // Previously defined but dead.
  145. DeadDefSet.erase(Reg);
  146. }
  147. if (!MO.isDead()) {
  148. for (const unsigned *SubRegs = TRI->getSubRegisters(Reg);
  149. unsigned SubReg = *SubRegs; ++SubRegs) {
  150. if (LocalDefSet.insert(SubReg))
  151. LocalDefs.push_back(SubReg);
  152. }
  153. }
  154. }
  155. FirstMI->setIsInsideBundle();
  156. Defs.clear();
  157. }
  158. SmallSet<unsigned, 8> Added;
  159. for (unsigned i = 0, e = LocalDefs.size(); i != e; ++i) {
  160. unsigned Reg = LocalDefs[i];
  161. if (Added.insert(Reg)) {
  162. // If it's not live beyond end of the bundle, mark it dead.
  163. bool isDead = DeadDefSet.count(Reg) || KilledDefSet.count(Reg);
  164. MIB.addReg(Reg, getDefRegState(true) | getDeadRegState(isDead) |
  165. getImplRegState(true));
  166. }
  167. }
  168. for (unsigned i = 0, e = ExternUses.size(); i != e; ++i) {
  169. unsigned Reg = ExternUses[i];
  170. bool isKill = KilledUseSet.count(Reg);
  171. bool isUndef = UndefUseSet.count(Reg);
  172. MIB.addReg(Reg, getKillRegState(isKill) | getUndefRegState(isUndef) |
  173. getImplRegState(true));
  174. }
  175. }
  176. /// finalizeBundle - Same functionality as the previous finalizeBundle except
  177. /// the last instruction in the bundle is not provided as an input. This is
  178. /// used in cases where bundles are pre-determined by marking instructions
  179. /// with 'InsideBundle' marker. It returns the MBB instruction iterator that
  180. /// points to the end of the bundle.
  181. MachineBasicBlock::instr_iterator
  182. llvm::finalizeBundle(MachineBasicBlock &MBB,
  183. MachineBasicBlock::instr_iterator FirstMI) {
  184. MachineBasicBlock::instr_iterator E = MBB.instr_end();
  185. MachineBasicBlock::instr_iterator LastMI = llvm::next(FirstMI);
  186. while (LastMI != E && LastMI->isInsideBundle())
  187. ++LastMI;
  188. finalizeBundle(MBB, FirstMI, LastMI);
  189. return LastMI;
  190. }
  191. /// finalizeBundles - Finalize instruction bundles in the specified
  192. /// MachineFunction. Return true if any bundles are finalized.
  193. bool llvm::finalizeBundles(MachineFunction &MF) {
  194. bool Changed = false;
  195. for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I) {
  196. MachineBasicBlock &MBB = *I;
  197. MachineBasicBlock::instr_iterator MII = MBB.instr_begin();
  198. assert(!MII->isInsideBundle() &&
  199. "First instr cannot be inside bundle before finalization!");
  200. MachineBasicBlock::instr_iterator MIE = MBB.instr_end();
  201. for (++MII; MII != MIE; ) {
  202. if (!MII->isInsideBundle())
  203. ++MII;
  204. else {
  205. MII = finalizeBundle(MBB, llvm::prior(MII));
  206. Changed = true;
  207. }
  208. }
  209. }
  210. return Changed;
  211. }