MachineCopyPropagation.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. //===- MachineCopyPropagation.cpp - Machine Copy Propagation Pass ---------===//
  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. //
  10. // This is an extremely simple MachineInstr-level copy propagation pass.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #define DEBUG_TYPE "codegen-cp"
  14. #include "llvm/CodeGen/Passes.h"
  15. #include "llvm/Pass.h"
  16. #include "llvm/CodeGen/MachineFunction.h"
  17. #include "llvm/CodeGen/MachineFunctionPass.h"
  18. #include "llvm/Target/TargetRegisterInfo.h"
  19. #include "llvm/Support/Debug.h"
  20. #include "llvm/Support/ErrorHandling.h"
  21. #include "llvm/Support/raw_ostream.h"
  22. #include "llvm/ADT/BitVector.h"
  23. #include "llvm/ADT/DenseMap.h"
  24. #include "llvm/ADT/SetVector.h"
  25. #include "llvm/ADT/SmallVector.h"
  26. #include "llvm/ADT/Statistic.h"
  27. using namespace llvm;
  28. STATISTIC(NumDeletes, "Number of dead copies deleted");
  29. namespace {
  30. class MachineCopyPropagation : public MachineFunctionPass {
  31. const TargetRegisterInfo *TRI;
  32. BitVector ReservedRegs;
  33. public:
  34. static char ID; // Pass identification, replacement for typeid
  35. MachineCopyPropagation() : MachineFunctionPass(ID) {
  36. initializeMachineCopyPropagationPass(*PassRegistry::getPassRegistry());
  37. }
  38. virtual bool runOnMachineFunction(MachineFunction &MF);
  39. private:
  40. void SourceNoLongerAvailable(unsigned Reg,
  41. DenseMap<unsigned, unsigned> &SrcMap,
  42. DenseMap<unsigned, MachineInstr*> &AvailCopyMap);
  43. bool CopyPropagateBlock(MachineBasicBlock &MBB);
  44. };
  45. }
  46. char MachineCopyPropagation::ID = 0;
  47. INITIALIZE_PASS(MachineCopyPropagation, "machine-cp",
  48. "Machine Copy Propagation Pass", false, false)
  49. FunctionPass *llvm::createMachineCopyPropagationPass() {
  50. return new MachineCopyPropagation();
  51. }
  52. void
  53. MachineCopyPropagation::SourceNoLongerAvailable(unsigned Reg,
  54. DenseMap<unsigned, unsigned> &SrcMap,
  55. DenseMap<unsigned, MachineInstr*> &AvailCopyMap) {
  56. DenseMap<unsigned, unsigned>::iterator SI = SrcMap.find(Reg);
  57. if (SI != SrcMap.end()) {
  58. unsigned MappedDef = SI->second;
  59. // Source of copy is no longer available for propagation.
  60. if (AvailCopyMap.erase(MappedDef)) {
  61. for (const unsigned *SR = TRI->getSubRegisters(MappedDef); *SR; ++SR)
  62. AvailCopyMap.erase(*SR);
  63. }
  64. }
  65. for (const unsigned *AS = TRI->getAliasSet(Reg); *AS; ++AS) {
  66. SI = SrcMap.find(*AS);
  67. if (SI != SrcMap.end()) {
  68. unsigned MappedDef = SI->second;
  69. if (AvailCopyMap.erase(MappedDef)) {
  70. for (const unsigned *SR = TRI->getSubRegisters(MappedDef); *SR; ++SR)
  71. AvailCopyMap.erase(*SR);
  72. }
  73. }
  74. }
  75. }
  76. static bool NoInterveningSideEffect(const MachineInstr *CopyMI,
  77. const MachineInstr *MI) {
  78. const MachineBasicBlock *MBB = CopyMI->getParent();
  79. if (MI->getParent() != MBB)
  80. return false;
  81. MachineBasicBlock::const_iterator I = CopyMI;
  82. MachineBasicBlock::const_iterator E = MBB->end();
  83. MachineBasicBlock::const_iterator E2 = MI;
  84. ++I;
  85. while (I != E && I != E2) {
  86. if (I->hasUnmodeledSideEffects() || I->isCall() ||
  87. I->isTerminator())
  88. return false;
  89. ++I;
  90. }
  91. return true;
  92. }
  93. bool MachineCopyPropagation::CopyPropagateBlock(MachineBasicBlock &MBB) {
  94. SmallSetVector<MachineInstr*, 8> MaybeDeadCopies; // Candidates for deletion
  95. DenseMap<unsigned, MachineInstr*> AvailCopyMap; // Def -> available copies map
  96. DenseMap<unsigned, MachineInstr*> CopyMap; // Def -> copies map
  97. DenseMap<unsigned, unsigned> SrcMap; // Src -> Def map
  98. bool Changed = false;
  99. for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); I != E; ) {
  100. MachineInstr *MI = &*I;
  101. ++I;
  102. if (MI->isCopy()) {
  103. unsigned Def = MI->getOperand(0).getReg();
  104. unsigned Src = MI->getOperand(1).getReg();
  105. if (TargetRegisterInfo::isVirtualRegister(Def) ||
  106. TargetRegisterInfo::isVirtualRegister(Src))
  107. report_fatal_error("MachineCopyPropagation should be run after"
  108. " register allocation!");
  109. DenseMap<unsigned, MachineInstr*>::iterator CI = AvailCopyMap.find(Src);
  110. if (CI != AvailCopyMap.end()) {
  111. MachineInstr *CopyMI = CI->second;
  112. unsigned SrcSrc = CopyMI->getOperand(1).getReg();
  113. if (!ReservedRegs.test(Def) &&
  114. (!ReservedRegs.test(Src) || NoInterveningSideEffect(CopyMI, MI)) &&
  115. (SrcSrc == Def || TRI->isSubRegister(SrcSrc, Def))) {
  116. // The two copies cancel out and the source of the first copy
  117. // hasn't been overridden, eliminate the second one. e.g.
  118. // %ECX<def> = COPY %EAX<kill>
  119. // ... nothing clobbered EAX.
  120. // %EAX<def> = COPY %ECX
  121. // =>
  122. // %ECX<def> = COPY %EAX
  123. //
  124. // Also avoid eliminating a copy from reserved registers unless the
  125. // definition is proven not clobbered. e.g.
  126. // %RSP<def> = COPY %RAX
  127. // CALL
  128. // %RAX<def> = COPY %RSP
  129. // Clear any kills of Def between CopyMI and MI. This extends the
  130. // live range.
  131. for (MachineBasicBlock::iterator I = CopyMI, E = MI; I != E; ++I)
  132. I->clearRegisterKills(Def, TRI);
  133. MI->eraseFromParent();
  134. Changed = true;
  135. ++NumDeletes;
  136. continue;
  137. }
  138. }
  139. // If Src is defined by a previous copy, it cannot be eliminated.
  140. CI = CopyMap.find(Src);
  141. if (CI != CopyMap.end())
  142. MaybeDeadCopies.remove(CI->second);
  143. for (const unsigned *AS = TRI->getAliasSet(Src); *AS; ++AS) {
  144. CI = CopyMap.find(*AS);
  145. if (CI != CopyMap.end())
  146. MaybeDeadCopies.remove(CI->second);
  147. }
  148. // Copy is now a candidate for deletion.
  149. MaybeDeadCopies.insert(MI);
  150. // If 'Src' is previously source of another copy, then this earlier copy's
  151. // source is no longer available. e.g.
  152. // %xmm9<def> = copy %xmm2
  153. // ...
  154. // %xmm2<def> = copy %xmm0
  155. // ...
  156. // %xmm2<def> = copy %xmm9
  157. SourceNoLongerAvailable(Def, SrcMap, AvailCopyMap);
  158. // Remember Def is defined by the copy.
  159. CopyMap[Def] = MI;
  160. AvailCopyMap[Def] = MI;
  161. for (const unsigned *SR = TRI->getSubRegisters(Def); *SR; ++SR) {
  162. CopyMap[*SR] = MI;
  163. AvailCopyMap[*SR] = MI;
  164. }
  165. // Remember source that's copied to Def. Once it's clobbered, then
  166. // it's no longer available for copy propagation.
  167. SrcMap[Src] = Def;
  168. continue;
  169. }
  170. // Not a copy.
  171. SmallVector<unsigned, 2> Defs;
  172. for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
  173. MachineOperand &MO = MI->getOperand(i);
  174. if (!MO.isReg())
  175. continue;
  176. unsigned Reg = MO.getReg();
  177. if (!Reg)
  178. continue;
  179. if (TargetRegisterInfo::isVirtualRegister(Reg))
  180. report_fatal_error("MachineCopyPropagation should be run after"
  181. " register allocation!");
  182. if (MO.isDef()) {
  183. Defs.push_back(Reg);
  184. continue;
  185. }
  186. // If 'Reg' is defined by a copy, the copy is no longer a candidate
  187. // for elimination.
  188. DenseMap<unsigned, MachineInstr*>::iterator CI = CopyMap.find(Reg);
  189. if (CI != CopyMap.end())
  190. MaybeDeadCopies.remove(CI->second);
  191. for (const unsigned *AS = TRI->getAliasSet(Reg); *AS; ++AS) {
  192. CI = CopyMap.find(*AS);
  193. if (CI != CopyMap.end())
  194. MaybeDeadCopies.remove(CI->second);
  195. }
  196. }
  197. for (unsigned i = 0, e = Defs.size(); i != e; ++i) {
  198. unsigned Reg = Defs[i];
  199. // No longer defined by a copy.
  200. CopyMap.erase(Reg);
  201. AvailCopyMap.erase(Reg);
  202. for (const unsigned *AS = TRI->getAliasSet(Reg); *AS; ++AS) {
  203. CopyMap.erase(*AS);
  204. AvailCopyMap.erase(*AS);
  205. }
  206. // If 'Reg' is previously source of a copy, it is no longer available for
  207. // copy propagation.
  208. SourceNoLongerAvailable(Reg, SrcMap, AvailCopyMap);
  209. }
  210. }
  211. // If MBB doesn't have successors, delete the copies whose defs are not used.
  212. // If MBB does have successors, then conservative assume the defs are live-out
  213. // since we don't want to trust live-in lists.
  214. if (MBB.succ_empty()) {
  215. for (SmallSetVector<MachineInstr*, 8>::iterator
  216. DI = MaybeDeadCopies.begin(), DE = MaybeDeadCopies.end();
  217. DI != DE; ++DI) {
  218. if (!ReservedRegs.test((*DI)->getOperand(0).getReg())) {
  219. (*DI)->eraseFromParent();
  220. Changed = true;
  221. ++NumDeletes;
  222. }
  223. }
  224. }
  225. return Changed;
  226. }
  227. bool MachineCopyPropagation::runOnMachineFunction(MachineFunction &MF) {
  228. bool Changed = false;
  229. TRI = MF.getTarget().getRegisterInfo();
  230. ReservedRegs = TRI->getReservedRegs(MF);
  231. for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I)
  232. Changed |= CopyPropagateBlock(*I);
  233. return Changed;
  234. }