<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 - Incorrect use-list order is generated due to stale user created when attempting to constant fold"
   href="https://bugs.llvm.org/show_bug.cgi?id=39660">39660</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Incorrect use-list order is generated due to stale user created when attempting to constant fold
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>new-bugs
          </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>new bugs
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>david.stenberg@ericsson.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>htmldeveloper@gmail.com, llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Created <span class=""><a href="attachment.cgi?id=21119" name="attach_21119" title="IR reproducer.">attachment 21119</a> <a href="attachment.cgi?id=21119&action=edit" title="IR reproducer.">[details]</a></span>
IR reproducer.

Seen on trunk (r346809).

A run of verify-uselistorder on the attached IR reproducer fails, due to the
use-list order for i64 ptrtoint (i64 ()* @foo to i64) being incorrect.

$ verify-uselistorder reduced.ll -debug 2>&1 | grep -A11 fail:
 - fail: user mismatch: ID = 5
 - LHS value = i64 ptrtoint (i64 ()* @foo to i64)
   => use: op = 0, user-id = 7, user = i64 add (i64 ptrtoint (i64 ()* @foo to
i64), i64 ptrtoint (i64 ()* @foo to i64))
   => use: op = 1, user-id = 7, user = i64 add (i64 ptrtoint (i64 ()* @foo to
i64), i64 ptrtoint (i64 ()* @foo to i64))
   => use: op = 0, user-id = 9, user = i64 add (i64 ptrtoint (i64 ()* @foo to
i64), i64 1)
   => use: op = 0, user-id = 0, user = i64 add (i64 ptrtoint (i64 ()* @foo to
i64), i64 zext (i1 icmp eq (i64 add (i64 ptrtoint (i64 ()* @foo to i64), i64
1), i64 0) to i64))
 - RHS value = i64 ptrtoint (i64 ()* @foo to i64)
   => use: op = 0, user-id = 0, user = i64 add (i64 ptrtoint (i64 ()* @foo to
i64), i64 zext (i1 icmp eq (i64 add (i64 ptrtoint (i64 ()* @foo to i64), i64
1), i64 0) to i64))
   => use: op = 0, user-id = 9, user = i64 add (i64 ptrtoint (i64 ()* @foo to
i64), i64 1)
   => use: op = 1, user-id = 7, user = i64 add (i64 ptrtoint (i64 ()* @foo to
i64), i64 ptrtoint (i64 ()* @foo to i64))
   => use: op = 0, user-id = 7, user = i64 add (i64 ptrtoint (i64 ()* @foo to
i64), i64 ptrtoint (i64 ()* @foo to i64))

If you look in the file, you can see that the following user does not exist:

  user = i64 add (i64 ptrtoint (i64 ()* @foo to i64), i64 zext (i1 icmp eq (i64
add (i64 ptrtoint (i64 ()* @foo to i64), i64 1), i64 0) to i64))

That constant expression is created when parsing:

  i64 add (i64 add (i64 ptrtoint (i64 () * @foo to i64), i64 ptrtoint (i64 () *
@foo to i64)), i64 zext (i1 icmp eq (i64 add (i64 ptrtoint (i64 () * @foo to
i64), i64 1), i64 0) to i64))

That constant expression is attempted to be constant folded by
ConstantFoldBinaryInstruction(), and the new constant expression is created
when attempting to rewrite the outermost add:

  // Given ((a + b) + c), if (b + c) folds to something interesting, return
  // (a + (b + c)).
  if (Instruction::isAssociative(Opcode) && CE1->getOpcode() == Opcode) {
    Constant *T = ConstantExpr::get(Opcode, CE1->getOperand(1), C2);
    if (!isa<ConstantExpr>(T) || cast<ConstantExpr>(T)->getOpcode() != Opcode)
      return ConstantExpr::get(Opcode, CE1->getOperand(0), T);</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>