<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 --- - Incorrect block frequency generated for entries of irregular loops"
   href="https://llvm.org/bugs/show_bug.cgi?id=23525">23525</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Incorrect block frequency generated for entries of irregular loops
          </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>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>congh@google.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvmbugs@cs.uiuc.edu
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Created <span class=""><a href="attachment.cgi?id=14329" name="attach_14329" title="Test case">attachment 14329</a> <a href="attachment.cgi?id=14329&action=edit" title="Test case">[details]</a></span>
Test case

Normally the frequency of a basic block other than the function entry should be
equal to the sum of its incoming edge frequencies. However, this may not hold
for CFGs with irregular loops which have more than one loop entry. LLVM
incorrectly assigns the same frequency to those loop entries for the same loop.

To reproduce this issue, compile the given test case below using the following
instructions:


clang test.C -O2 -fprofile-instr-generate
./a.out
llvm-profdata merge -output=profdata default.profraw
clang test.C -O2 -fprofile-instr-use=profdata -emit-llvm -S
opt -analyze -block-freq -S test.ll


The output is:


Printing analysis 'Block Frequency Analysis' for function '_Z8hot_loopi':
block-frequency-info: _Z8hot_loopi
 - entry: float = 1.0, int = 8
 - for.cond: float = 25.52, int = 204
 - for.body: float = 24.52, int = 196
 - if.then4: float = 12.26499796, int = 98
 - Next: float = 25.52, int = 204
 - for.inc: float = 25.52, int = 204
 - for.end: float = 1.0, int = 8

Printing analysis 'Block Frequency Analysis' for function 'main':
block-frequency-info: main
 - entry: float = 1.0, int = 8
 - for.body: float = 51.0, int = 407
 - for.end: float = 1.0, int = 8


Note that in hot_loop(), Next shares the same frequency as for.inc, which is
incorrect. Next should have less frequency.

The test case:


int g;

__attribute__((noinline)) int hot_loop(int n) {
  int i = n / 2;

  if (n % 2 == 0) {
    goto Next;
  }

  #pragma nounroll
  for (; i < n; i++) {
    if (i % 2 == 0) {
      g *= 2;
    } else {
Next:
      g += n;
    }
  }

  return g;
}

int main() {
  for (int i = 1; i < 100; ++i)
    hot_loop(i);
  return 0;
}</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>