<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 - [IndVarSimplify] Canonicalize to IV type by extending LIV operand"
   href="https://bugs.llvm.org/show_bug.cgi?id=37630">37630</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>[IndVarSimplify] Canonicalize to IV type by extending LIV operand
          </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>listmail@philipreames.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Looking at IR today, we end up with a bunch of patterns of the following form
which fail to get further reduced or optimized.  I'm still chasing where
exactly these come from - my current best guess is a pass ordering problem
between CVP and IndVars which results in us widening without learning overflow
facts - but there's no reason we shouldn't handle this case anyway.

My specific proposal is that we remove the trunc, widen the compare to 64 bits,
and insert a sext of the loop invariant operand.  We can do this when:
1) The trunc does not change range of the input.  Can be checked with SCEVs
range mechanism.
2) The right operand is loop invariant.
3) The comparison is signed. (We can use a zext when unsigned.)
4) The widened type is as cheap as the narrow type.  (See the same check in
IndVarSimplify widening logic.)

I believe this can be added to eliminateIVComparison in
Utils/SimplifyIndVars.cpp naturally.

target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

define void @test2(i32 %n) {
entry:
  br label %loop
loop:
  %iv = phi i64 [0, %entry], [%iv.next, %loop]
  %iv.next = add nuw nsw i64 %iv, 1
  %sext = trunc i64 %iv to i32
  %cmp = icmp slt i32 %sext, %n
  br i1 %cmp, label %loop, label %exit
exit:
  ret void
}</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>