MachineInstrBundle.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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. 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->isBundledWithPred()) {
  44. MII->unbundleFromPred();
  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. MIBundleBuilder Bundle(MBB, FirstMI, LastMI);
  88. const TargetMachine &TM = MBB.getParent()->getTarget();
  89. const TargetInstrInfo *TII = TM.getInstrInfo();
  90. const TargetRegisterInfo *TRI = TM.getRegisterInfo();
  91. MachineInstrBuilder MIB = BuildMI(*MBB.getParent(), FirstMI->getDebugLoc(),
  92. TII->get(TargetOpcode::BUNDLE));
  93. Bundle.prepend(MIB);
  94. SmallVector<unsigned, 32> LocalDefs;
  95. SmallSet<unsigned, 32> LocalDefSet;
  96. SmallSet<unsigned, 8> DeadDefSet;
  97. SmallSet<unsigned, 16> KilledDefSet;
  98. SmallVector<unsigned, 8> ExternUses;
  99. SmallSet<unsigned, 8> ExternUseSet;
  100. SmallSet<unsigned, 8> KilledUseSet;
  101. SmallSet<unsigned, 8> UndefUseSet;
  102. SmallVector<MachineOperand*, 4> Defs;
  103. for (; FirstMI != LastMI; ++FirstMI) {
  104. for (unsigned i = 0, e = FirstMI->getNumOperands(); i != e; ++i) {
  105. MachineOperand &MO = FirstMI->getOperand(i);
  106. if (!MO.isReg())
  107. continue;
  108. if (MO.isDef()) {
  109. Defs.push_back(&MO);
  110. continue;
  111. }
  112. unsigned Reg = MO.getReg();
  113. if (!Reg)
  114. continue;
  115. assert(TargetRegisterInfo::isPhysicalRegister(Reg));
  116. if (LocalDefSet.count(Reg)) {
  117. MO.setIsInternalRead();
  118. if (MO.isKill())
  119. // Internal def is now killed.
  120. KilledDefSet.insert(Reg);
  121. } else {
  122. if (ExternUseSet.insert(Reg)) {
  123. ExternUses.push_back(Reg);
  124. if (MO.isUndef())
  125. UndefUseSet.insert(Reg);
  126. }
  127. if (MO.isKill())
  128. // External def is now killed.
  129. KilledUseSet.insert(Reg);
  130. }
  131. }
  132. for (unsigned i = 0, e = Defs.size(); i != e; ++i) {
  133. MachineOperand &MO = *Defs[i];
  134. unsigned Reg = MO.getReg();
  135. if (!Reg)
  136. continue;
  137. if (LocalDefSet.insert(Reg)) {
  138. LocalDefs.push_back(Reg);
  139. if (MO.isDead()) {
  140. DeadDefSet.insert(Reg);
  141. }
  142. } else {
  143. // Re-defined inside the bundle, it's no longer killed.
  144. KilledDefSet.erase(Reg);
  145. if (!MO.isDead())
  146. // Previously defined but dead.
  147. DeadDefSet.erase(Reg);
  148. }
  149. if (!MO.isDead()) {
  150. for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs) {
  151. unsigned SubReg = *SubRegs;
  152. if (LocalDefSet.insert(SubReg))
  153. LocalDefs.push_back(SubReg);
  154. }
  155. }
  156. }
  157. Defs.clear();
  158. }
  159. SmallSet<unsigned, 32> Added;
  160. for (unsigned i = 0, e = LocalDefs.size(); i != e; ++i) {
  161. unsigned Reg = LocalDefs[i];
  162. if (Added.insert(Reg)) {
  163. // If it's not live beyond end of the bundle, mark it dead.
  164. bool isDead = DeadDefSet.count(Reg) || KilledDefSet.count(Reg);
  165. MIB.addReg(Reg, getDefRegState(true) | getDeadRegState(isDead) |
  166. getImplRegState(true));
  167. }
  168. }
  169. for (unsigned i = 0, e = ExternUses.size(); i != e; ++i) {
  170. unsigned Reg = ExternUses[i];
  171. bool isKill = KilledUseSet.count(Reg);
  172. bool isUndef = UndefUseSet.count(Reg);
  173. MIB.addReg(Reg, getKillRegState(isKill) | getUndefRegState(isUndef) |
  174. getImplRegState(true));
  175. }
  176. }
  177. /// finalizeBundle - Same functionality as the previous finalizeBundle except
  178. /// the last instruction in the bundle is not provided as an input. This is
  179. /// used in cases where bundles are pre-determined by marking instructions
  180. /// with 'InsideBundle' marker. It returns the MBB instruction iterator that
  181. /// points to the end of the bundle.
  182. MachineBasicBlock::instr_iterator
  183. llvm::finalizeBundle(MachineBasicBlock &MBB,
  184. MachineBasicBlock::instr_iterator FirstMI) {
  185. MachineBasicBlock::instr_iterator E = MBB.instr_end();
  186. MachineBasicBlock::instr_iterator LastMI = std::next(FirstMI);
  187. while (LastMI != E && LastMI->isInsideBundle())
  188. ++LastMI;
  189. finalizeBundle(MBB, FirstMI, LastMI);
  190. return LastMI;
  191. }
  192. /// finalizeBundles - Finalize instruction bundles in the specified
  193. /// MachineFunction. Return true if any bundles are finalized.
  194. bool llvm::finalizeBundles(MachineFunction &MF) {
  195. bool Changed = false;
  196. for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I) {
  197. MachineBasicBlock &MBB = *I;
  198. MachineBasicBlock::instr_iterator MII = MBB.instr_begin();
  199. MachineBasicBlock::instr_iterator MIE = MBB.instr_end();
  200. if (MII == MIE)
  201. continue;
  202. assert(!MII->isInsideBundle() &&
  203. "First instr cannot be inside bundle before finalization!");
  204. for (++MII; MII != MIE; ) {
  205. if (!MII->isInsideBundle())
  206. ++MII;
  207. else {
  208. MII = finalizeBundle(MBB, std::prev(MII));
  209. Changed = true;
  210. }
  211. }
  212. }
  213. return Changed;
  214. }
  215. //===----------------------------------------------------------------------===//
  216. // MachineOperand iterator
  217. //===----------------------------------------------------------------------===//
  218. MachineOperandIteratorBase::VirtRegInfo
  219. MachineOperandIteratorBase::analyzeVirtReg(unsigned Reg,
  220. SmallVectorImpl<std::pair<MachineInstr*, unsigned> > *Ops) {
  221. VirtRegInfo RI = { false, false, false };
  222. for(; isValid(); ++*this) {
  223. MachineOperand &MO = deref();
  224. if (!MO.isReg() || MO.getReg() != Reg)
  225. continue;
  226. // Remember each (MI, OpNo) that refers to Reg.
  227. if (Ops)
  228. Ops->push_back(std::make_pair(MO.getParent(), getOperandNo()));
  229. // Both defs and uses can read virtual registers.
  230. if (MO.readsReg()) {
  231. RI.Reads = true;
  232. if (MO.isDef())
  233. RI.Tied = true;
  234. }
  235. // Only defs can write.
  236. if (MO.isDef())
  237. RI.Writes = true;
  238. else if (!RI.Tied && MO.getParent()->isRegTiedToDefOperand(getOperandNo()))
  239. RI.Tied = true;
  240. }
  241. return RI;
  242. }
  243. MachineOperandIteratorBase::PhysRegInfo
  244. MachineOperandIteratorBase::analyzePhysReg(unsigned Reg,
  245. const TargetRegisterInfo *TRI) {
  246. bool AllDefsDead = true;
  247. PhysRegInfo PRI = {false, false, false, false, false, false};
  248. assert(TargetRegisterInfo::isPhysicalRegister(Reg) &&
  249. "analyzePhysReg not given a physical register!");
  250. for (; isValid(); ++*this) {
  251. MachineOperand &MO = deref();
  252. if (MO.isRegMask() && MO.clobbersPhysReg(Reg))
  253. PRI.Clobbers = true; // Regmask clobbers Reg.
  254. if (!MO.isReg())
  255. continue;
  256. unsigned MOReg = MO.getReg();
  257. if (!MOReg || !TargetRegisterInfo::isPhysicalRegister(MOReg))
  258. continue;
  259. bool IsRegOrSuperReg = MOReg == Reg || TRI->isSubRegister(MOReg, Reg);
  260. bool IsRegOrOverlapping = MOReg == Reg || TRI->regsOverlap(MOReg, Reg);
  261. if (IsRegOrSuperReg && MO.readsReg()) {
  262. // Reg or a super-reg is read, and perhaps killed also.
  263. PRI.Reads = true;
  264. PRI.Kills = MO.isKill();
  265. }
  266. if (IsRegOrOverlapping && MO.readsReg()) {
  267. PRI.ReadsOverlap = true;// Reg or an overlapping register is read.
  268. }
  269. if (!MO.isDef())
  270. continue;
  271. if (IsRegOrSuperReg) {
  272. PRI.Defines = true; // Reg or a super-register is defined.
  273. if (!MO.isDead())
  274. AllDefsDead = false;
  275. }
  276. if (IsRegOrOverlapping)
  277. PRI.Clobbers = true; // Reg or an overlapping reg is defined.
  278. }
  279. if (AllDefsDead && PRI.Defines)
  280. PRI.DefinesDead = true; // Reg or super-register was defined and was dead.
  281. return PRI;
  282. }