<html>
    <head>
      <base href="https://bugs.llvm.org/">
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - [LVI] Miscompile by CorrelatedValuePropagation: LVI doesn't take nsw into account"
   href="https://bugs.llvm.org/show_bug.cgi?id=43802">43802</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>[LVI] Miscompile by CorrelatedValuePropagation: LVI doesn't take nsw into account
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>libraries
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>trunk
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>Linux
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>enhancement
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>Scalar Optimizations
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>suc-daniil@yandex.ru
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Consider following code:
define i32 @foo(i32 %arg) {
bb:
  %tmp = sub nsw i32 0, %arg
  %tmp1 = icmp eq i32 %arg, -2147483648
  br i1 %tmp1, label %bb2, label %bb3

bb2:                                              ; preds = %bb
  br label %bb3

bb3:                                              ; preds = %bb2, %bb
  %tmp4 = phi i32 [ -2147483648, %bb2 ], [ %tmp, %bb ]
  ret i32 %tmp4
}

This cmd:
opt -correlated-propagation -S repro.ll

Produces following result:
define i32 @foo(i32 %arg) {
bb:
  %tmp = sub nsw i32 0, %arg
  %tmp1 = icmp eq i32 %arg, -2147483648
  br i1 %tmp1, label %bb2, label %bb3

bb2:                                              ; preds = %bb
  br label %bb3

bb3:                                              ; preds = %bb2, %bb
  ret i32 %tmp
}

Before the transformation result of `foo` was defined for all `%arg` values,
after the transformation result of `foo` when `%arg == MIN_INT` is `undef`.

This regression was introduced by following commit:
commit 3b6d46761f139e7ee3057758698efa4487323829
Author: Sanjay Patel <<a href="mailto:spatel@rotateright.com">spatel@rotateright.com</a>>
Date:   Tue Apr 10 20:42:39 2018 +0000

    [CVP] simplify phi with constant incoming values that match common variable
edge values

    This is based on an example that was recently posted on llvm-dev:

    void *propagate_null(void* b, int* g) {
      if (!b) {
        return 0;
      }
      (*g)++;
      return b;
    }

    <a href="https://godbolt.org/g/xYk3qG">https://godbolt.org/g/xYk3qG</a>

    The original code or constant propagation in other passes has obscured the
fact
    that the phi can be removed completely.

    Differential Revision: <a href="https://reviews.llvm.org/D45448">https://reviews.llvm.org/D45448</a>

    llvm-svn: 329755


The transformation introduced by the commit replaces phi when all incoming
values except one are constants and that one non-constant incoming value is
equal to every constant when coming over the corresponding edge. To check this
condition it queries LazyValueInfo::getConstantOnEdge for the non-constant
value and the incoming edge and compares the result with the corresponding
incoming constant. It appears that LazyValueInfo::getConstantOnEdge doesn't
take the `nsw` flag into account.</pre>
        </div>
      </p>


      <hr>
      <span>You are receiving this mail because:</span>

      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>