MachineCopyPropagation.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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. typedef SmallVector<unsigned, 4> DestList;
  41. typedef DenseMap<unsigned, DestList> SourceMap;
  42. void SourceNoLongerAvailable(unsigned Reg,
  43. SourceMap &SrcMap,
  44. DenseMap<unsigned, MachineInstr*> &AvailCopyMap);
  45. bool CopyPropagateBlock(MachineBasicBlock &MBB);
  46. };
  47. }
  48. char MachineCopyPropagation::ID = 0;
  49. char &llvm::MachineCopyPropagationID = MachineCopyPropagation::ID;
  50. INITIALIZE_PASS(MachineCopyPropagation, "machine-cp",
  51. "Machine Copy Propagation Pass", false, false)
  52. void
  53. MachineCopyPropagation::SourceNoLongerAvailable(unsigned Reg,
  54. SourceMap &SrcMap,
  55. DenseMap<unsigned, MachineInstr*> &AvailCopyMap) {
  56. for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) {
  57. SourceMap::iterator SI = SrcMap.find(*AI);
  58. if (SI != SrcMap.end()) {
  59. const DestList& Defs = SI->second;
  60. for (DestList::const_iterator I = Defs.begin(), E = Defs.end();
  61. I != E; ++I) {
  62. unsigned MappedDef = *I;
  63. // Source of copy is no longer available for propagation.
  64. if (AvailCopyMap.erase(MappedDef)) {
  65. for (MCSubRegIterator SR(MappedDef, TRI); SR.isValid(); ++SR)
  66. AvailCopyMap.erase(*SR);
  67. }
  68. }
  69. }
  70. }
  71. }
  72. static bool NoInterveningSideEffect(const MachineInstr *CopyMI,
  73. const MachineInstr *MI) {
  74. const MachineBasicBlock *MBB = CopyMI->getParent();
  75. if (MI->getParent() != MBB)
  76. return false;
  77. MachineBasicBlock::const_iterator I = CopyMI;
  78. MachineBasicBlock::const_iterator E = MBB->end();
  79. MachineBasicBlock::const_iterator E2 = MI;
  80. ++I;
  81. while (I != E && I != E2) {
  82. if (I->hasUnmodeledSideEffects() || I->isCall() ||
  83. I->isTerminator())
  84. return false;
  85. ++I;
  86. }
  87. return true;
  88. }
  89. /// isNopCopy - Return true if the specified copy is really a nop. That is
  90. /// if the source of the copy is the same of the definition of the copy that
  91. /// supplied the source. If the source of the copy is a sub-register than it
  92. /// must check the sub-indices match. e.g.
  93. /// ecx = mov eax
  94. /// al = mov cl
  95. /// But not
  96. /// ecx = mov eax
  97. /// al = mov ch
  98. static bool isNopCopy(MachineInstr *CopyMI, unsigned Def, unsigned Src,
  99. const TargetRegisterInfo *TRI) {
  100. unsigned SrcSrc = CopyMI->getOperand(1).getReg();
  101. if (Def == SrcSrc)
  102. return true;
  103. if (TRI->isSubRegister(SrcSrc, Def)) {
  104. unsigned SrcDef = CopyMI->getOperand(0).getReg();
  105. unsigned SubIdx = TRI->getSubRegIndex(SrcSrc, Def);
  106. if (!SubIdx)
  107. return false;
  108. return SubIdx == TRI->getSubRegIndex(SrcDef, Src);
  109. }
  110. return false;
  111. }
  112. bool MachineCopyPropagation::CopyPropagateBlock(MachineBasicBlock &MBB) {
  113. SmallSetVector<MachineInstr*, 8> MaybeDeadCopies; // Candidates for deletion
  114. DenseMap<unsigned, MachineInstr*> AvailCopyMap; // Def -> available copies map
  115. DenseMap<unsigned, MachineInstr*> CopyMap; // Def -> copies map
  116. SourceMap SrcMap; // Src -> Def map
  117. bool Changed = false;
  118. for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); I != E; ) {
  119. MachineInstr *MI = &*I;
  120. ++I;
  121. if (MI->isCopy()) {
  122. unsigned Def = MI->getOperand(0).getReg();
  123. unsigned Src = MI->getOperand(1).getReg();
  124. if (TargetRegisterInfo::isVirtualRegister(Def) ||
  125. TargetRegisterInfo::isVirtualRegister(Src))
  126. report_fatal_error("MachineCopyPropagation should be run after"
  127. " register allocation!");
  128. DenseMap<unsigned, MachineInstr*>::iterator CI = AvailCopyMap.find(Src);
  129. if (CI != AvailCopyMap.end()) {
  130. MachineInstr *CopyMI = CI->second;
  131. if (!ReservedRegs.test(Def) &&
  132. (!ReservedRegs.test(Src) || NoInterveningSideEffect(CopyMI, MI)) &&
  133. isNopCopy(CopyMI, Def, Src, TRI)) {
  134. // The two copies cancel out and the source of the first copy
  135. // hasn't been overridden, eliminate the second one. e.g.
  136. // %ECX<def> = COPY %EAX<kill>
  137. // ... nothing clobbered EAX.
  138. // %EAX<def> = COPY %ECX
  139. // =>
  140. // %ECX<def> = COPY %EAX
  141. //
  142. // Also avoid eliminating a copy from reserved registers unless the
  143. // definition is proven not clobbered. e.g.
  144. // %RSP<def> = COPY %RAX
  145. // CALL
  146. // %RAX<def> = COPY %RSP
  147. // Clear any kills of Def between CopyMI and MI. This extends the
  148. // live range.
  149. for (MachineBasicBlock::iterator I = CopyMI, E = MI; I != E; ++I)
  150. I->clearRegisterKills(Def, TRI);
  151. MI->eraseFromParent();
  152. Changed = true;
  153. ++NumDeletes;
  154. continue;
  155. }
  156. }
  157. // If Src is defined by a previous copy, it cannot be eliminated.
  158. for (MCRegAliasIterator AI(Src, TRI, true); AI.isValid(); ++AI) {
  159. CI = CopyMap.find(*AI);
  160. if (CI != CopyMap.end())
  161. MaybeDeadCopies.remove(CI->second);
  162. }
  163. // Copy is now a candidate for deletion.
  164. MaybeDeadCopies.insert(MI);
  165. // If 'Src' is previously source of another copy, then this earlier copy's
  166. // source is no longer available. e.g.
  167. // %xmm9<def> = copy %xmm2
  168. // ...
  169. // %xmm2<def> = copy %xmm0
  170. // ...
  171. // %xmm2<def> = copy %xmm9
  172. SourceNoLongerAvailable(Def, SrcMap, AvailCopyMap);
  173. // Remember Def is defined by the copy.
  174. // ... Make sure to clear the def maps of aliases first.
  175. for (MCRegAliasIterator AI(Def, TRI, false); AI.isValid(); ++AI) {
  176. CopyMap.erase(*AI);
  177. AvailCopyMap.erase(*AI);
  178. }
  179. CopyMap[Def] = MI;
  180. AvailCopyMap[Def] = MI;
  181. for (MCSubRegIterator SR(Def, TRI); SR.isValid(); ++SR) {
  182. CopyMap[*SR] = MI;
  183. AvailCopyMap[*SR] = MI;
  184. }
  185. // Remember source that's copied to Def. Once it's clobbered, then
  186. // it's no longer available for copy propagation.
  187. if (std::find(SrcMap[Src].begin(), SrcMap[Src].end(), Def) ==
  188. SrcMap[Src].end()) {
  189. SrcMap[Src].push_back(Def);
  190. }
  191. continue;
  192. }
  193. // Not a copy.
  194. SmallVector<unsigned, 2> Defs;
  195. int RegMaskOpNum = -1;
  196. for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
  197. MachineOperand &MO = MI->getOperand(i);
  198. if (MO.isRegMask())
  199. RegMaskOpNum = i;
  200. if (!MO.isReg())
  201. continue;
  202. unsigned Reg = MO.getReg();
  203. if (!Reg)
  204. continue;
  205. if (TargetRegisterInfo::isVirtualRegister(Reg))
  206. report_fatal_error("MachineCopyPropagation should be run after"
  207. " register allocation!");
  208. if (MO.isDef()) {
  209. Defs.push_back(Reg);
  210. continue;
  211. }
  212. // If 'Reg' is defined by a copy, the copy is no longer a candidate
  213. // for elimination.
  214. for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) {
  215. DenseMap<unsigned, MachineInstr*>::iterator CI = CopyMap.find(*AI);
  216. if (CI != CopyMap.end())
  217. MaybeDeadCopies.remove(CI->second);
  218. }
  219. }
  220. // The instruction has a register mask operand which means that it clobbers
  221. // a large set of registers. It is possible to use the register mask to
  222. // prune the available copies, but treat it like a basic block boundary for
  223. // now.
  224. if (RegMaskOpNum >= 0) {
  225. // Erase any MaybeDeadCopies whose destination register is clobbered.
  226. const MachineOperand &MaskMO = MI->getOperand(RegMaskOpNum);
  227. for (SmallSetVector<MachineInstr*, 8>::iterator
  228. DI = MaybeDeadCopies.begin(), DE = MaybeDeadCopies.end();
  229. DI != DE; ++DI) {
  230. unsigned Reg = (*DI)->getOperand(0).getReg();
  231. if (ReservedRegs.test(Reg) || !MaskMO.clobbersPhysReg(Reg))
  232. continue;
  233. (*DI)->eraseFromParent();
  234. Changed = true;
  235. ++NumDeletes;
  236. }
  237. // Clear all data structures as if we were beginning a new basic block.
  238. MaybeDeadCopies.clear();
  239. AvailCopyMap.clear();
  240. CopyMap.clear();
  241. SrcMap.clear();
  242. continue;
  243. }
  244. for (unsigned i = 0, e = Defs.size(); i != e; ++i) {
  245. unsigned Reg = Defs[i];
  246. // No longer defined by a copy.
  247. for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) {
  248. CopyMap.erase(*AI);
  249. AvailCopyMap.erase(*AI);
  250. }
  251. // If 'Reg' is previously source of a copy, it is no longer available for
  252. // copy propagation.
  253. SourceNoLongerAvailable(Reg, SrcMap, AvailCopyMap);
  254. }
  255. }
  256. // If MBB doesn't have successors, delete the copies whose defs are not used.
  257. // If MBB does have successors, then conservative assume the defs are live-out
  258. // since we don't want to trust live-in lists.
  259. if (MBB.succ_empty()) {
  260. for (SmallSetVector<MachineInstr*, 8>::iterator
  261. DI = MaybeDeadCopies.begin(), DE = MaybeDeadCopies.end();
  262. DI != DE; ++DI) {
  263. if (!ReservedRegs.test((*DI)->getOperand(0).getReg())) {
  264. (*DI)->eraseFromParent();
  265. Changed = true;
  266. ++NumDeletes;
  267. }
  268. }
  269. }
  270. return Changed;
  271. }
  272. bool MachineCopyPropagation::runOnMachineFunction(MachineFunction &MF) {
  273. bool Changed = false;
  274. TRI = MF.getTarget().getRegisterInfo();
  275. ReservedRegs = TRI->getReservedRegs(MF);
  276. for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I)
  277. Changed |= CopyPropagateBlock(*I);
  278. return Changed;
  279. }