MachineInstrBundle.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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, 32> LocalDefs;
  93. SmallSet<unsigned, 32> LocalDefSet;
  94. SmallSet<unsigned, 8> DeadDefSet;
  95. SmallSet<unsigned, 16> 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 (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs) {
  149. unsigned SubReg = *SubRegs;
  150. if (LocalDefSet.insert(SubReg))
  151. LocalDefs.push_back(SubReg);
  152. }
  153. }
  154. }
  155. FirstMI->setIsInsideBundle();
  156. Defs.clear();
  157. }
  158. SmallSet<unsigned, 32> 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. if (MII == MIE)
  202. continue;
  203. for (++MII; MII != MIE; ) {
  204. if (!MII->isInsideBundle())
  205. ++MII;
  206. else {
  207. MII = finalizeBundle(MBB, llvm::prior(MII));
  208. Changed = true;
  209. }
  210. }
  211. }
  212. return Changed;
  213. }
  214. //===----------------------------------------------------------------------===//
  215. // MachineOperand iterator
  216. //===----------------------------------------------------------------------===//
  217. MachineOperandIteratorBase::VirtRegInfo
  218. MachineOperandIteratorBase::analyzeVirtReg(unsigned Reg,
  219. SmallVectorImpl<std::pair<MachineInstr*, unsigned> > *Ops) {
  220. VirtRegInfo RI = { false, false, false };
  221. for(; isValid(); ++*this) {
  222. MachineOperand &MO = deref();
  223. if (!MO.isReg() || MO.getReg() != Reg)
  224. continue;
  225. // Remember each (MI, OpNo) that refers to Reg.
  226. if (Ops)
  227. Ops->push_back(std::make_pair(MO.getParent(), getOperandNo()));
  228. // Both defs and uses can read virtual registers.
  229. if (MO.readsReg()) {
  230. RI.Reads = true;
  231. if (MO.isDef())
  232. RI.Tied = true;
  233. }
  234. // Only defs can write.
  235. if (MO.isDef())
  236. RI.Writes = true;
  237. else if (!RI.Tied && MO.getParent()->isRegTiedToDefOperand(getOperandNo()))
  238. RI.Tied = true;
  239. }
  240. return RI;
  241. }
  242. MachineOperandIteratorBase::PhysRegInfo
  243. MachineOperandIteratorBase::analyzePhysReg(unsigned Reg,
  244. const TargetRegisterInfo *TRI) {
  245. bool AllDefsDead = true;
  246. PhysRegInfo PRI = {false, false, false, false, false, false, false};
  247. assert(TargetRegisterInfo::isPhysicalRegister(Reg) &&
  248. "analyzePhysReg not given a physical register!");
  249. for (; isValid(); ++*this) {
  250. MachineOperand &MO = deref();
  251. if (MO.isRegMask() && MO.clobbersPhysReg(Reg))
  252. PRI.Clobbers = true; // Regmask clobbers Reg.
  253. if (!MO.isReg())
  254. continue;
  255. unsigned MOReg = MO.getReg();
  256. if (!MOReg || !TargetRegisterInfo::isPhysicalRegister(MOReg))
  257. continue;
  258. bool IsRegOrSuperReg = MOReg == Reg || TRI->isSubRegister(MOReg, Reg);
  259. bool IsRegOrOverlapping = MOReg == Reg || TRI->regsOverlap(MOReg, Reg);
  260. if (IsRegOrSuperReg && MO.readsReg()) {
  261. // Reg or a super-reg is read, and perhaps killed also.
  262. PRI.Reads = true;
  263. PRI.Kills = MO.isKill();
  264. } if (IsRegOrOverlapping && MO.readsReg()) {
  265. PRI.ReadsOverlap = true;// Reg or an overlapping register is read.
  266. }
  267. if (!MO.isDef())
  268. continue;
  269. if (IsRegOrSuperReg) {
  270. PRI.Defines = true; // Reg or a super-register is defined.
  271. if (!MO.isDead())
  272. AllDefsDead = false;
  273. }
  274. if (IsRegOrOverlapping)
  275. PRI.Clobbers = true; // Reg or an overlapping reg is defined.
  276. }
  277. if (AllDefsDead && PRI.Defines)
  278. PRI.DefinesDead = true; // Reg or super-register was defined and was dead.
  279. return PRI;
  280. }