MachineInstrBundle.cpp 11 KB

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