<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 - Coverage option(--coverage) with -O3 introduces wrong loop unroll"
   href="https://bugs.llvm.org/show_bug.cgi?id=47027">47027</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Coverage option(--coverage) with -O3 introduces wrong loop unroll
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>clang
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>10.0
          </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>unassignedclangbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>public.melg8@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>htmldeveloper@gmail.com, llvm-bugs@lists.llvm.org, neeilans@live.com, richard-llvm@metafoo.co.uk
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Minimal reproducible contains two functions ConvertInt and ConvertShort with
identical bodies, except ConvertShort uses short type for loop counter, it
compiled with --coverage and -O3 flags.

Expected behavior: two functions should give identical results on identical
inputs.

Observed behavior: ConvertShort function doesn't xor/assign to result[2] value,
so results from functions doesn't match.

How to reproduce:
x86-64 architecture,
clang version 6-10 is affected.

Compile this code with flags --coverage -O3:
#include <cstdint>
#include <cstdio>
#include <cstdlib>

void ConvertInt(const uint16_t* const initial_state, uint16_t* result) {
  uint16_t state[36] = {};

  for (int j = 0; j < 4; ++j) {
    state[j] = initial_state[j];
  }

  for (int j = 0; j < 32; ++j) {
    static const uint8_t table[2] = {0xab, 0xcd};
    const int position = (state[j + 1] + state[j + 3]) % 2;
    state[j + 4] = table[position] + state[j + 2];

    if (j == 16) {
      for (int i = 0; i < 4; ++i) {
        result[i] ^= state[j + i];
      }
    }
  }
}

void ConvertShort(const uint16_t* const initial_state, uint16_t* result) {
  uint16_t state[36] = {};

  for (int j = 0; j < 4; ++j) {
    state[j] = initial_state[j];
  }

  for (int j = 0; j < 32; ++j) {
    static const uint8_t table[2] = {0xab, 0xcd};
    const int position = (state[j + 1] + state[j + 3]) % 2;
    state[j + 4] = table[position] + state[j + 2];

    if (j == 16) {
      for (short i = 0; i < 4; ++i) {
        result[i] ^= state[j + i]; // result[2] always == 0. Bug?
      }
    }
  }
}

int main() {
  uint16_t initial_state[4] = {};

  uint16_t result_int[8] = {};
  ConvertInt(initial_state, result_int);

  uint16_t result_short[8] = {};
  ConvertShort(initial_state, result_short);

  for (int i = 0; i < 8; ++i) {
    if (result_int[i] != result_short[i]) {
      printf("Is bugged");
      exit(-1);
    }
  }
}</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>