MachineInstrBundle.cpp 11 KB

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