<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 - Replace loop-based variable with loop invariant variable"
   href="https://bugs.llvm.org/show_bug.cgi?id=42700">42700</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Replace loop-based variable with loop invariant variable
          </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>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>Loop Optimizer
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>david.bolvansky@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>int f(bool* arr, bool x) {

    int ss = 0;
    for (int i = 0; i < 16; ++i) {
        if (arr[i] == x) { 
            // [0]
            ss += (arr[i]) ? 1 : -1;
        }
    }
    return ss;
}


[0] x is not modified in this branch so try to replace:
ss += (arr[i]) ? 1 : -1;
with:
ss += (x) ? 1 : -1;


int f2(bool* arr, bool x) {

    int ss = 0;
    for (int i = 0; i < 16; ++i) {
        if (arr[i] == x) { 
            ss += (x) ? 1 : -1;
        }
    }
    return ss;
}


It may help LICM to hoist some code, help Loop
distribution/unswitch/vectorization pass, etc..

For my motivating example, current Clang trunk -O3 just unrolls 'f' (for N=16)
/ vectorizes 'f' (N=64+). Clang vectorizes 'f2' even for N=16+.

Godbolt: <a href="https://godbolt.org/z/c7HwFi">https://godbolt.org/z/c7HwFi</a></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>