2
0

MachineCopyPropagation.cpp 12 KB

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