<html>
    <head>
      <base href="https://llvm.org/bugs/" />
    </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 --- - SCEV Expander fails to reuse existing values"
   href="https://llvm.org/bugs/show_bug.cgi?id=24920">24920</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>SCEV Expander fails to reuse existing values
          </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>All
          </td>
        </tr>

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

        <tr>
          <th>Severity</th>
          <td>normal
          </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>michael.v.zolotukhin@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Currently SCEV expander only reuses existing values if they're exactly like the
one it tries to expand. However, it should be able to use similar values (not
exactly the same) to generate more optimal code.

Below is the example:
------------------
target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"

; Function Attrs: noinline nounwind ssp uwtable
define i8* @foo(i32 %y, i8* noalias %src, i8* noalias %dst) {
entry:
  %cmp = icmp slt i32 %y, 4096
  %sub = add nsw i32 %y, -1
  %tripcount = select i1 %cmp, i32 %sub, i32 4095
  %loop.entry.cond = icmp sgt i32 %tripcount, 0
  br i1 %loop.entry.cond, label %loop.ph, label %loop.exit

loop.ph:                                   ; preds = %entry
  br label %loop.body

loop.body:                                         ; preds = %loop.body,
%loop.ph
  %iv = phi i32 [ 0, %loop.ph ], [ %iv.next, %loop.body ]
  %src.iv = phi i8* [ %src, %loop.ph ], [ %src.iv.next, %loop.body ]
  %dst.iv = phi i8* [ %dst, %loop.ph ], [ %dst.iv.next, %loop.body ]

  %tmp = load i8, i8* %src.iv, align 1
  store i8 %tmp, i8* %dst.iv, align 1

  %src.iv.next = getelementptr inbounds i8, i8* %src.iv, i64 1
  %dst.iv.next = getelementptr inbounds i8, i8* %dst.iv, i64 1
  %iv.next = add nsw i32 %iv, 1

  %loop.cond = icmp slt i32 %iv.next, %tripcount
  br i1 %loop.cond, label %loop.body, label %loop.exit

loop.exit:                                 ; preds = %loop.exit, %entry
  %src.iv.lcssa = phi i8* [ %src.iv.next, %loop.body ], [ %src, %entry ]
  ret i8* undef
}

!llvm.module.flags = !{!0, !1}
!llvm.ident = !{!2}

!0 = !{i32 2, !"Debug Info Version", i32 3}
!1 = !{i32 1, !"PIC Level", i32 2}
!2 = !{!"clang version 3.8.0 (trunk 247767) (llvm/trunk 247769)"}
------------------
In this test it fails to rewrite exit value for %src, as its SCEV value
contains smax, which is considered expensive. However, it doesn't need to
generate code for smax, because such code already exists and the result is in
%tripcount value.

This bug was revealed in this review: <a href="http://reviews.llvm.org/D12765">http://reviews.llvm.org/D12765</a></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>