<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 - Loop layering based on a monotonic condition between two loop indvars"
   href="https://bugs.llvm.org/show_bug.cgi?id=44156">44156</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Loop layering based on a monotonic condition between two loop indvars
          </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>lebedev.ri@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>void init(int colIn, int colOut);
void sink(int colIn, int colOut);

void bad(int inputWidth, int outWidth) {
    for(int colIn = 0, colOut = 0; colOut < outWidth; ++colIn, ++colOut) {
        if(colIn == inputWidth) {
            init(colIn, colOut);
            colIn = 0;
        }

        sink(colIn, colOut);
    }
}

Every iteration of innermost branch we have a conditional branch.
But we can avoid it, by having two loops, with now-innermost
running for precomputed number of iterations with no such branching:

#include <algorithm>

void init(int colIn, int colOut);
void sink(int colIn, int colOut);

void good(int inputWidth, int outWidth) {
    for(int colOut = 0, colIn = 0; colOut < outWidth; ) {
        if(colIn == inputWidth) {
            init(colIn, colOut);
            colIn = 0;
        }

        int outColForNextInRow = outWidth - colIn;

        for( ; colOut < std::min(outWidth, outColForNextInRow)
             ; ++colIn, ++colOut)
            sink(colIn, colOut);
    }
}


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