<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 - `i = i % constant` for static local `i` is not optimized"
   href="https://bugs.llvm.org/show_bug.cgi?id=39509">39509</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>`i = i % constant` for static local `i` is not optimized
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>clang
          </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>C++
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>antoshkka@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>dgregor@apple.com, llvm-bugs@lists.llvm.org, richard-llvm@metafoo.co.uk
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Consider the following example:

unsigned next_trivial() {
    static int i = 0;

    auto ret = i;
    ++i;
    i = i % 10;
    return ret;
}

For that example a very suboptimal assembly with multiplication, many registers
usage and multiple instructions is generated.

However, the above example could be rewritten in the following way:
unsigned next_trivial_optim() {
    static int i = 0;

    auto ret = i;
    ++i;
    if (i == 10) { i = 0; }
    return ret;
}

For the above code snippet a very short and clear assembly is produced, without
any multiplications and unnecessary instructions:
  mov eax, dword ptr [rip + next_trivial_optim()::i]
  lea ecx, [rax + 1]
  xor edx, edx
  cmp ecx, 10
  cmovne edx, ecx
  mov dword ptr [rip + next_trivial_optim()::i], edx
  ret

Please, add an optimization to do that transformation.</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>