<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 --- - Wrong code bug after GVN/PRE"
   href="https://llvm.org/bugs/show_bug.cgi?id=31651">31651</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Wrong code bug after GVN/PRE
          </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>normal
          </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>mikael.holmen@ericsson.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>Created <span class=""><a href="attachment.cgi?id=17844" name="attach_17844" title="Reproducer">attachment 17844</a> <a href="attachment.cgi?id=17844&action=edit" title="Reproducer">[details]</a></span>
Reproducer

Reproduce with
 opt -S -gvn pre_gvn.ll
Adding 
 -debug-only=gvn
We see e.g
 GVN REMOVING PRE LOAD:   %_tmp79 = load i16, i16* %_tmp78, align 2
and I think it goes wrong during this optimization.

In the input we have something like

    for (int step1 = 0; step1 < LOOP_AMOUNT; step1++)
    {
        lb[step1] = step1 + 7;
        ub[step1] = (step1 == 0 ? 10: ub[step1-1]*10);

        switch(lb[step1]) {
        case 7:    CVAL_VERIFY(step1 == 0);
            CVAL_VERIFY(ub[step1] == 10);
                        __label(511);
            break;
        case 8:    CVAL_VERIFY(step1 == 1);
            CVAL_VERIFY(ub[step1] == 100);
                        __label(512);
            break;
        case 9:    CVAL_VERIFY(step1 == 2);
            CVAL_VERIFY(ub[step1] == 1000);
                        __label(513);
            break;
        default:;
        }

        [...]
    }

    int i, j;
    for ( j = 10, i = 0; j < 101; j=j*10, i++ )
    {
        CVAL_VERIFY(ub[i] == j);
    }

So we have two loops. In the first one the array ub is written (low index
first, high index last), and then in the second loop the content of ub is
checked (low index first, high index last).

After gvn this is changed into something like

    int tmp;
    for (int step1 = 0; step1 < LOOP_AMOUNT; step1++)
    {
        lb[step1] = step1 + 7;
        ub[step1] = (step1 == 0 ? 10: ub[step1-1]*10);

        switch(lb[step1]) {
        case 7:    CVAL_VERIFY(step1 == 0);
                        tmp = ub[step1];
            CVAL_VERIFY(tmp == 10);
                        __label(511);
            break;
        case 8:    CVAL_VERIFY(step1 == 1);
                        tmp = ub[step1];
            CVAL_VERIFY(tmp == 100);
                        __label(512);
            break;
        case 9:    CVAL_VERIFY(step1 == 2);
                        tmp = ub[step1];
            CVAL_VERIFY(tmp == 1000);
                        __label(513);
            break;
        default:;
        }

        [...]
    }

    int i, j;
    for ( j = 10, i = 0; j < 101; j=j*10, i++ )
    {
        CVAL_VERIFY((i == 0 ? tmp : ub[i]) == j);
    }

Note the introduced variable tmp (%_tmp792 in the ll output).

Also note that with this rewrite, after the first loop tmp will contain the
value of the element at the highest index in ub, and then we use tmp in the
first round of the second loop, but there we expect the value of the lowest
index element in ub.</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>