[PATCH] D34135: [LVI] Add initial result to avoid infinite getValueFromCondition recursion

Mikael Holmén via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Jun 13 01:19:49 PDT 2017


uabelho created this revision.

This fixes PR33357.

After a rewrite done by jump-threading, the code can contain instructions
like

  %0 = and i1 %0, %1

in basic blocks that are not reachable from the entry. This is valid
according to the verifier, but LazyValueInfo entered infinite recursion
when analyzing it.

Now we add an initial pessimistic result to the Visited map before doing
the recursive call, so if we find ourselves during the analysis it will
stop recursion.


https://reviews.llvm.org/D34135

Files:
  lib/Analysis/LazyValueInfo.cpp
  test/Transforms/JumpThreading/PR33357-lvi-recursion.ll


Index: test/Transforms/JumpThreading/PR33357-lvi-recursion.ll
===================================================================
--- /dev/null
+++ test/Transforms/JumpThreading/PR33357-lvi-recursion.ll
@@ -0,0 +1,42 @@
+; RUN: opt -S -jump-threading -verify -o - %s | FileCheck %s
+
+; Don't enter infinite recursion in LazyValueInfo.cpp
+
+ at a = external global i16, align 1
+
+define void @f(i32 %p1) {
+bb0:
+  %0 = icmp eq i32 %p1, 0
+  br i1 undef, label %bb6, label %bb1
+
+bb1:                                         ; preds = %bb0
+  br label %bb2
+
+bb2:                                         ; preds = %bb4, %bb1
+  %1 = phi i1 [ %0, %bb1 ], [ %2, %bb4 ]
+  %2 = and i1 %1, undef
+  br i1 %2, label %bb3, label %bb4
+
+bb3:                                         ; preds = %bb2
+  store i16 undef, i16* @a, align 1
+  br label %bb4
+
+bb4:                                         ; preds = %bb3, %bb2
+  br i1 %0, label %bb2, label %bb5
+
+bb5:                                         ; preds = %bb4
+  unreachable
+
+bb6:                                         ; preds = %bb0
+  ret void
+}
+
+; The
+;  br i1 undef, label %bb6, label %bb1
+; is replaced by
+;  br label %bb6
+; and the rest of the function is unreachable from entry.
+
+; CHECK:      define void @f(i32 %p1) {
+; CHECK-NEXT: bb6:
+; CHECK-NEXT:   ret void
Index: lib/Analysis/LazyValueInfo.cpp
===================================================================
--- lib/Analysis/LazyValueInfo.cpp
+++ lib/Analysis/LazyValueInfo.cpp
@@ -1344,6 +1344,15 @@
   if (I != Visited.end())
     return I->second;
 
+  // Add a pessimistic initial result to avoid infinite recursion in case
+  // we find self referencing stuff like:
+  //
+  //  %0 = and i1 %0, %1
+  //
+  // The above is legal, as long as the instruction is not reachable from the
+  // entry bb.
+  Visited[Cond] = LVILatticeVal::getOverdefined();
+
   auto Result = getValueFromConditionImpl(Val, Cond, isTrueDest, Visited);
   Visited[Cond] = Result;
   return Result;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D34135.102297.patch
Type: text/x-patch
Size: 2017 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170613/683d9a15/attachment.bin>


More information about the llvm-commits mailing list