<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 - LLVM produces redundant IVs and fails to eliminate them"
   href="https://bugs.llvm.org/show_bug.cgi?id=43690">43690</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>LLVM produces redundant IVs and fails to eliminate them
          </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>enhancement
          </td>
        </tr>

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

        <tr>
          <th>Component</th>
          <td>Global Analyses
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>d.maljutin@yandex.ru
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Created <span class=""><a href="attachment.cgi?id=22677" name="attach_22677" title="Example">attachment 22677</a> <a href="attachment.cgi?id=22677&action=edit" title="Example">[details]</a></span>
Example

It looks like SCEVExpander can create redundant IVs when expanding some SCEV
because
1) It tries to expand SCEV that is identical to that of the IV PHI node, but
the IV is not in a canonical form. The PHI is not reused because it's never
added to ExprToValueMap (only to ValueToExpr), and since it's not in canonical
form (and expander is in canonical mode), it creates additional IV.
I.e. when trying to expand {1,+,1}<nuw><%for.body> with the same IV, it'll
create additional IV {0,+,1}<nuw><%for.body> + add+1 IR instruction.

See the attached IR for such an example.

2) If SCEVExpander is not in canonical mode, it deals with case above, however
it results in the similar problem in the opposite situation, since it doesn't
reuse addrec expressions in non-canonical mode:
<a href="https://github.com/llvm/llvm-project/blob/5c3bc3c930d3993a2c5f581112c18a9eb0ba6838/llvm/lib/Analysis/ScalarEvolutionExpander.cpp#L1787">https://github.com/llvm/llvm-project/blob/5c3bc3c930d3993a2c5f581112c18a9eb0ba6838/llvm/lib/Analysis/ScalarEvolutionExpander.cpp#L1787</a>

After such redundant IVs are generated, llvm fails to get rid of them (as
demonstrated by running opt -O3 on the attached IR).</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>