[PATCH] D87965: [InstCombine] replace phi values from unreachable blocks with 'undef'

Sanjay Patel via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sun Sep 20 05:53:59 PDT 2020


spatel updated this revision to Diff 293018.
spatel added a comment.

Patch updated:

1. Use the direct approach to fix the bug - replace with undef before trying to delete.
2. Avoid branch on undef in crashing test case.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D87965/new/

https://reviews.llvm.org/D87965

Files:
  llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
  llvm/test/Transforms/InstCombine/phi.ll


Index: llvm/test/Transforms/InstCombine/phi.ll
===================================================================
--- llvm/test/Transforms/InstCombine/phi.ll
+++ llvm/test/Transforms/InstCombine/phi.ll
@@ -1181,3 +1181,39 @@
   %cmp1 = icmp ne i32 %a.0, 0
   ret i1  %cmp1
 }
+
+; This would crash trying to delete an instruction (conv)
+; that still had uses because the user (the phi) was not
+; updated to remove a use from an unreachable block (g.exit).
+
+define void @main(i1 %cond, i16 %x) {
+; CHECK-LABEL: @main(
+; CHECK-NEXT:  entry:
+; CHECK-NEXT:    br label [[FOR_COND:%.*]]
+; CHECK:       for.cond:
+; CHECK-NEXT:    br i1 [[COND:%.*]], label [[FOR_END:%.*]], label [[FOR_BODY:%.*]]
+; CHECK:       for.body:
+; CHECK-NEXT:    unreachable
+; CHECK:       g.exit:
+; CHECK-NEXT:    br label [[FOR_COND]]
+; CHECK:       for.end:
+; CHECK-NEXT:    ret void
+;
+entry:
+  br label %for.cond
+
+for.cond:
+  %p = phi double [ %conv, %g.exit ], [ undef, %entry ]
+  br i1 %cond, label %for.end, label %for.body
+
+for.body:
+  %conv = sitofp i16 %x to double
+  unreachable
+
+g.exit:
+  br label %for.cond
+
+for.end:
+  store double %p, double* undef
+  ret void
+}
Index: llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
===================================================================
--- llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
+++ llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
@@ -2813,6 +2813,7 @@
       if (SI->isVolatile())
         return nullptr;
 
+    replaceInstUsesWith(*Prev, UndefValue::get(Prev->getType()));
     eraseInstFromFunction(*Prev);
     return &I;
   }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D87965.293018.patch
Type: text/x-patch
Size: 1635 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200920/2fe80fef/attachment.bin>


More information about the llvm-commits mailing list