[PATCH] D38235: [UnreachableBlockElim] Use COPY if PHI input is undef

Mikael Holmén via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Sep 25 05:45:53 PDT 2017


uabelho created this revision.

If we have

  %vreg0<def> = PHI %vreg2<undef>, <BB#0>, %vreg3, <BB#2>; GR32:%vreg0,%vreg2,%vreg3
  %vreg3<def,tied1> = ADD32ri8 %vreg0<kill,tied0>, 1, %EFLAGS<imp-def>; GR32:%vreg3,%vreg0

then we can't just change %vreg0 into %vreg3, since %vreg2 is actually
undef. We would have to also copy the undef flag to be able to change the
register.

Instead we deal with this case like other cases where we can't just
replace the register: we insert a COPY. The code creating the COPY already
copied all flags from the PHI input, so the undef flag will be transferred
as it should.


https://reviews.llvm.org/D38235

Files:
  lib/CodeGen/UnreachableBlockElim.cpp
  test/CodeGen/X86/unreachable-mbb-undef-phi.ll


Index: test/CodeGen/X86/unreachable-mbb-undef-phi.ll
===================================================================
--- /dev/null
+++ test/CodeGen/X86/unreachable-mbb-undef-phi.ll
@@ -0,0 +1,33 @@
+; RUN: llc -optimize-regalloc -O0 %s -o - -stop-after=unreachable-mbb-elimination | FileCheck %s
+
+define void @f() {
+bb0:
+  br label %bb1
+
+bb1:                                           ; preds = %bb2, %bb0
+  %inc1 = phi i32 [ %inc, %bb2 ], [ undef, %bb0 ]
+  %inc = add nsw i32 %inc1, 1
+  %tobool = icmp eq i32 0, 0
+  br i1 %tobool, label %bb3, label %bb2
+
+bb2:                                           ; preds = %bb1
+  br label %bb1
+
+bb3:                                           ; preds = %bb1
+  ret void
+}
+
+; bb2 is dead and should be removed and the add input should be marked with
+; undef since the bb0 value in the PHI is undef. Shouldn't crash.
+
+; CHECK: bb.0.bb0:
+; CHECK:   successors: %bb.1.bb1
+; CHECK:   JMP_1 %bb.1.bb1
+
+; CHECK: bb.1.bb1:
+; CHECK:   successors: %bb.2.bb3
+; CHECK:   dead %eax = ADD32ri8 undef %eax, 1, implicit-def dead %eflags
+; CHECK:   JMP_1 %bb.2.bb3
+
+; CHECK: bb.2.bb3:
+; CHECK:   RETQ
Index: lib/CodeGen/UnreachableBlockElim.cpp
===================================================================
--- lib/CodeGen/UnreachableBlockElim.cpp
+++ lib/CodeGen/UnreachableBlockElim.cpp
@@ -207,11 +207,12 @@
           MachineRegisterInfo &MRI = F.getRegInfo();
           unsigned InputSub = Input.getSubReg();
           if (InputSub == 0 &&
-              MRI.constrainRegClass(InputReg, MRI.getRegClass(OutputReg))) {
+              MRI.constrainRegClass(InputReg, MRI.getRegClass(OutputReg)) &&
+              !Input.isUndef()) {
             MRI.replaceRegWith(OutputReg, InputReg);
           } else {
             // The input register to the PHI has a subregister or it can't be
-            // constrained to the proper register class:
+            // constrained to the proper register class or it is undef:
             // insert a COPY instead of simply replacing the output
             // with the input.
             const TargetInstrInfo *TII = F.getSubtarget().getInstrInfo();


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D38235.116541.patch
Type: text/x-patch
Size: 2161 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170925/d354fc8b/attachment.bin>


More information about the llvm-commits mailing list