[PATCH] D82638: [MachineCopyPropagation] BackwardPropagatableCopy: add check for hasOverlappingMultipleDef

Simon Wallis via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Jun 26 03:46:02 PDT 2020


simonwallis2 created this revision.
simonwallis2 added a reviewer: efriedma.
Herald added subscribers: llvm-commits, hiraditya, kristof.beyls.
Herald added a project: LLVM.

In MachineCopyPropagation::BackwardPropagatableCopy(),
a check is added for multiple destination registers.

The copy propagation is avoided if the copied destination register
is the same register as another destination on the same instruction.

A new test is added.  This used to fail on ARM like this:
error: unpredictable instruction, RdHi and RdLo must be different

  umull   r9, r9, lr, r0


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D82638

Files:
  llvm/lib/CodeGen/MachineCopyPropagation.cpp
  llvm/test/CodeGen/ARM/mcp-dest-regs-no-dup.ll


Index: llvm/test/CodeGen/ARM/mcp-dest-regs-no-dup.ll
===================================================================
--- /dev/null
+++ llvm/test/CodeGen/ARM/mcp-dest-regs-no-dup.ll
@@ -0,0 +1,51 @@
+; RUN: llc -mtriple=arm-eabi -O1 %s -verify-machineinstrs -o %t
+; RUN: llvm-mc < %t -triple thumbv7 -filetype=obj -o /dev/null
+
+ at a = hidden local_unnamed_addr global i32 0, align 4
+ at b = hidden local_unnamed_addr global i32 0, align 4
+ at f = hidden local_unnamed_addr global i32 0, align 4
+ at c = hidden local_unnamed_addr global i32 0, align 4
+ at d = hidden local_unnamed_addr global i32 0, align 4
+ at e = hidden local_unnamed_addr global i32 0, align 4
+ at m = hidden local_unnamed_addr global i32 0, align 4
+ at g = hidden local_unnamed_addr global i32* null, align 4
+
+define hidden void @h() local_unnamed_addr #0 {
+  call void asm sideeffect "", ""() #1
+  br label %11
+
+1:                                                ; preds = %13
+  call void asm sideeffect "", ""() #1
+  %2 = load i32, i32* @a, align 4
+  %3 = udiv i32 %2, 45
+  %4 = load i32, i32* @b, align 4
+  %5 = load i32, i32* @f, align 4
+  %6 = icmp ult i32 %4, %5
+  %7 = zext i1 %6 to i32
+  %8 = icmp ule i32 %3, %7
+  %9 = zext i1 %8 to i32
+  %10 = icmp ult i32 %14, %9
+  br i1 %10, label %18, label %11
+
+11:                                               ; preds = %0, %1
+  store i32 2, i32* @f, align 4
+  %12 = load i32*, i32** @g, align 4
+  br label %13
+
+13:                                               ; preds = %11, %13
+  %14 = load i32, i32* @c, align 4
+  store i32 11, i32* @d, align 4
+  store i32 0, i32* @e, align 4
+  store i32 1, i32* @b, align 4
+  %15 = load i32, i32* @m, align 4
+  store i32 %15, i32* %12, align 4
+  %16 = load i32, i32* @f, align 4
+  %17 = icmp eq i32 %16, 0
+  br i1 %17, label %1, label %13
+
+18:                                               ; preds = %1
+  ret void
+}
+
+attributes #0 = { "target-features"="+armv8-a,-fpregs" }
+attributes #1 = { nounwind }
Index: llvm/lib/CodeGen/MachineCopyPropagation.cpp
===================================================================
--- llvm/lib/CodeGen/MachineCopyPropagation.cpp
+++ llvm/lib/CodeGen/MachineCopyPropagation.cpp
@@ -288,6 +288,9 @@
                                           const MachineInstr &UseI,
                                           unsigned UseIdx);
   bool hasImplicitOverlap(const MachineInstr &MI, const MachineOperand &Use);
+  bool hasOverlappingMultipleDef(const MachineInstr &MI,
+                                 const MachineOperand &MODef,
+                                 Register Def);
 
   /// Candidates for deletion.
   SmallSetVector<MachineInstr *, 8> MaybeDeadCopies;
@@ -461,6 +464,19 @@
   return false;
 }
 
+bool MachineCopyPropagation::hasOverlappingMultipleDef(
+    const MachineInstr &MI, const MachineOperand &MODef, Register Def) {
+  if (MI.getDesc().getNumDefs() > 1) {
+    for (const MachineOperand &MIDef : MI.defs()) {
+      if ((&MIDef != &MODef) && MIDef.isReg() && MIDef.isDef() &&
+          TRI->regsOverlap(Def, MIDef.getReg()))
+        return true;
+    }
+  }
+
+  return false;
+}
+
 /// Look for available copies whose destination register is used by \p MI and
 /// replace the use in \p MI with the copy's source register.
 void MachineCopyPropagation::forwardUses(MachineInstr &MI) {
@@ -786,6 +802,9 @@
     if (hasImplicitOverlap(MI, MODef))
       continue;
 
+    if (hasOverlappingMultipleDef(MI, MODef, Def))
+      continue;
+
     LLVM_DEBUG(dbgs() << "MCP: Replacing " << printReg(MODef.getReg(), TRI)
                       << "\n     with " << printReg(Def, TRI) << "\n     in "
                       << MI << "     from " << *Copy);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D82638.273646.patch
Type: text/x-patch
Size: 3701 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200626/d7bdb180/attachment.bin>


More information about the llvm-commits mailing list