[PATCH] D45394: Canonical Copy Propagation

Puyan Lotfi via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Apr 6 15:53:57 PDT 2018


plotfi created this revision.
plotfi added a reviewer: bogner.
Herald added a subscriber: llvm-commits.

This is a new technique I thought of today, idea is to fold copies of vregs to vregs. Probably will support some cases where there are physregs too.


Repository:
  rL LLVM

https://reviews.llvm.org/D45394

Files:
  lib/CodeGen/MIRCanonicalizerPass.cpp


Index: lib/CodeGen/MIRCanonicalizerPass.cpp
===================================================================
--- lib/CodeGen/MIRCanonicalizerPass.cpp
+++ lib/CodeGen/MIRCanonicalizerPass.cpp
@@ -311,6 +311,47 @@
   return Changed;
 }
 
+bool propagateLocalCopies(MachineBasicBlock *MBB) {
+  bool Changed = false;
+  MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
+
+  std::vector<MachineInstr*> copies;
+  for (auto II = MBB->begin(), IE = MBB->end(); II != IE; ++II) {
+    if (II->isCopy())
+      copies.push_back(&*II);
+  }
+
+  for (auto MI : copies) {
+
+    if (!MI->getOperand(0).isReg())
+      continue;
+    if (!MI->getOperand(1).isReg())
+      continue;
+
+    const unsigned Dst = MI->getOperand(0).getReg();
+    const unsigned Src = MI->getOperand(1).getReg();
+
+    if (!TargetRegisterInfo::isVirtualRegister(Dst))
+      continue;
+    if (!TargetRegisterInfo::isVirtualRegister(Src))
+      continue;
+
+    std::vector<MachineOperand *> RenameMOs;
+    for (auto UI = MRI.use_begin(Dst); UI != MRI.use_end(); ++UI) {
+      RenameMOs.push_back(&*UI);
+    }
+
+    for (auto *MO : RenameMOs) {
+      Changed = true;
+      MO->setReg(Src);
+    }
+
+    MI->eraseFromParent();
+  }
+
+  return Changed;
+}
+
 /// Here we find our candidates. What makes an interesting candidate?
 /// An candidate for a canonicalization tree root is normally any kind of
 /// instruction that causes side effects such as a store to memory or a copy to
@@ -619,6 +660,10 @@
   bbNames.push_back(MBB->getName());
   DEBUG(dbgs() << "\n\n NEW BASIC BLOCK: " << MBB->getName() << "\n\n";);
 
+  DEBUG(dbgs() << "MBB Before Canonical Copy Propagation:\n"; MBB->dump(););
+  Changed |= propagateLocalCopies(MBB);
+  DEBUG(dbgs() << "MBB After Canonical Copy Propagation:\n"; MBB->dump(););
+
   DEBUG(dbgs() << "MBB Before Scheduling:\n"; MBB->dump(););
   unsigned IdempotentInstCount = 0;
   Changed |= rescheduleCanonically(IdempotentInstCount, MBB);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D45394.141447.patch
Type: text/x-patch
Size: 1971 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180406/49001c66/attachment.bin>


More information about the llvm-commits mailing list