[PATCH] D10903: [LICM] Replace incoming values with undef if the incoming BB is unreachable

David Majnemer david.majnemer at gmail.com
Thu Jul 2 12:23:29 PDT 2015


majnemer created this revision.
majnemer added a reviewer: chandlerc.
majnemer added a subscriber: llvm-commits-list.

Unreachable BBs don't meaningfully contribute to the PHI node, replace
their incoming value with undef.

This fixes PR24013.

http://reviews.llvm.org/D10903

Files:
  lib/Transforms/Scalar/LICM.cpp
  test/Transforms/LICM/PR24013.ll

Index: test/Transforms/LICM/PR24013.ll
===================================================================
--- /dev/null
+++ test/Transforms/LICM/PR24013.ll
@@ -0,0 +1,19 @@
+; RUN: opt -licm -S < %s | FileCheck %s
+
+define void @f(i1 zeroext %p1) {
+; CHECK-LABEL: @f(
+entry:
+  br label %lbl
+
+lbl.loopexit:                                     ; No predecessors!
+  br label %lbl
+
+lbl:                                              ; preds = %lbl.loopexit, %entry
+  %phi = phi i32 [ %conv, %lbl.loopexit ], [ undef, %entry ]
+; CHECK: phi i32 [ undef, {{.*}} ], [ undef
+  br label %if.then.5
+
+if.then.5:                                        ; preds = %if.then.5, %lbl
+  %conv = zext i1 undef to i32
+  br label %if.then.5
+}
Index: lib/Transforms/Scalar/LICM.cpp
===================================================================
--- lib/Transforms/Scalar/LICM.cpp
+++ lib/Transforms/Scalar/LICM.cpp
@@ -602,14 +602,24 @@
   // PHI nodes in exit blocks due to LCSSA form. Just RAUW them with clones of
   // the instruction.
   while (!I.use_empty()) {
-    Instruction *User = I.user_back();
+    Value::user_iterator UI = I.user_begin();
+    auto *User = cast<Instruction>(*UI);
     if (!DT->isReachableFromEntry(User->getParent())) {
       User->replaceUsesOfWith(&I, UndefValue::get(I.getType()));
       continue;
     }
     // The user must be a PHI node.
     PHINode *PN = cast<PHINode>(User);
 
+    // Replace incoming values with undef if the incoming BB is unreachable.
+    Use &U = UI.getUse();
+    BasicBlock *BB = PN->getIncomingBlock(U);
+    if (!DT->isReachableFromEntry(BB)) {
+      // Replace the use with an undef value.
+      U = UndefValue::get(I.getType());
+      continue;
+    }
+
     BasicBlock *ExitBlock = PN->getParent();
     assert(ExitBlockSet.count(ExitBlock) &&
            "The LCSSA PHI is not in an exit block!");


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D10903.28967.patch
Type: text/x-patch
Size: 1876 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20150702/3a6174ef/attachment.bin>


More information about the llvm-commits mailing list