[llvm] r330113 - [MIR-Canon] Adding ISA-Agnostic COPY Folding.

Puyan Lotfi via llvm-commits llvm-commits at lists.llvm.org
Mon Apr 16 02:03:03 PDT 2018


Author: zer0
Date: Mon Apr 16 02:03:03 2018
New Revision: 330113

URL: http://llvm.org/viewvc/llvm-project?rev=330113&view=rev
Log:
[MIR-Canon] Adding ISA-Agnostic COPY Folding.

Transforms the following:

    %vreg1234:gpr32 = COPY %42
    %vreg1235:gpr32 = COPY %vreg1234
    %vreg1236:gpr32 = COPY %vreg1235
    $w0 = COPY %vreg1236

into:

    $w0 = COPY %42

Assuming %42 is also a gpr32


Added:
    llvm/trunk/test/CodeGen/MIR/AArch64/mirCanonCopyCopyProp.mir
Modified:
    llvm/trunk/lib/CodeGen/MIRCanonicalizerPass.cpp

Modified: llvm/trunk/lib/CodeGen/MIRCanonicalizerPass.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/MIRCanonicalizerPass.cpp?rev=330113&r1=330112&r2=330113&view=diff
==============================================================================
--- llvm/trunk/lib/CodeGen/MIRCanonicalizerPass.cpp (original)
+++ llvm/trunk/lib/CodeGen/MIRCanonicalizerPass.cpp Mon Apr 16 02:03:03 2018
@@ -310,6 +310,45 @@ static bool rescheduleCanonically(unsign
   return Changed;
 }
 
+bool propagateLocalCopies(MachineBasicBlock *MBB) {
+  bool Changed = false;
+  MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
+
+  std::vector<MachineInstr *> Copies;
+  for (MachineInstr &MI : MBB->instrs()) {
+    if (MI.isCopy())
+      Copies.push_back(&MI);
+  }
+
+  for (MachineInstr *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;
+    if (MRI.getRegClass(Dst) != MRI.getRegClass(Src))
+      continue;
+
+    for (auto UI = MRI.use_begin(Dst); UI != MRI.use_end(); ++UI) {
+      MachineOperand *MO = &*UI;
+      MO->setReg(Src);
+      Changed = true;
+    }
+
+    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
@@ -616,6 +655,10 @@ static bool runOnBasicBlock(MachineBasic
   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);

Added: llvm/trunk/test/CodeGen/MIR/AArch64/mirCanonCopyCopyProp.mir
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/CodeGen/MIR/AArch64/mirCanonCopyCopyProp.mir?rev=330113&view=auto
==============================================================================
--- llvm/trunk/test/CodeGen/MIR/AArch64/mirCanonCopyCopyProp.mir (added)
+++ llvm/trunk/test/CodeGen/MIR/AArch64/mirCanonCopyCopyProp.mir Mon Apr 16 02:03:03 2018
@@ -0,0 +1,45 @@
+# RUN: llc -mtriple=arm64-apple-ios11.0.0 -o - -run-pass mir-canonicalizer %s | FileCheck %s
+
+...
+---
+name: Proc8
+stack:
+  - { id: 0, type: default, offset: 0, size: 4, alignment: 4,
+      stack-id: 0, callee-saved-register: '', callee-saved-restored: true,
+      local-offset: -4, di-variable: '', di-expression: '', di-location: '' }
+  - { id: 1, type: default, offset: 0, size: 8, alignment: 8,
+      stack-id: 0, callee-saved-register: '', callee-saved-restored: true,
+      local-offset: -16, di-variable: '', di-expression: '', di-location: '' }
+  - { id: 2, type: default, offset: 0, size: 8, alignment: 8,
+      stack-id: 0, callee-saved-register: '', callee-saved-restored: true,
+      local-offset: -24, di-variable: '', di-expression: '', di-location: '' }
+  - { id: 3, type: default, offset: 0, size: 8, alignment: 8,
+      stack-id: 0, callee-saved-register: '', callee-saved-restored: true,
+      local-offset: -32, di-variable: '', di-expression: '', di-location: '' }
+  - { id: 4, type: default, offset: 0, size: 8, alignment: 8,
+      stack-id: 0, callee-saved-register: '', callee-saved-restored: true,
+      local-offset: -40, di-variable: '', di-expression: '', di-location: '' }
+  - { id: 5, type: default, offset: 0, size: 8, alignment: 8,
+      stack-id: 0, callee-saved-register: '', callee-saved-restored: true,
+      local-offset: -48, di-variable: '', di-expression: '', di-location: '' }
+  - { id: 6, type: default, offset: 0, size: 8, alignment: 8,
+      stack-id: 0, callee-saved-register: '', callee-saved-restored: true,
+      local-offset: -56, di-variable: '', di-expression: '', di-location: '' }
+constants:
+body: |
+  bb.0:
+    liveins: $x0, $x1, $d0, $d1
+
+    %42:gpr32 = LDRWui %stack.0, 0 :: (dereferenceable load 8)
+
+    ;CHECK: %namedVReg1352:gpr32 = LDRWui %stack.0, 0 :: (dereferenceable load 8)
+    ;CHECK: $w0 = COPY %namedVReg1352
+    ;CHECK: RET_ReallyLR implicit $w0
+
+    %vreg1234:gpr32 = COPY %42
+    %vreg1235:gpr32 = COPY %vreg1234
+    %vreg1236:gpr32 = COPY %vreg1235
+    $w0 = COPY %vreg1236
+    RET_ReallyLR implicit $w0
+
+...




More information about the llvm-commits mailing list