<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/121105>121105</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [LLVM] Inlining breaks __builtin_expect branch weight metadata
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          sillycross
      </td>
    </tr>
</table>

<pre>
    Minimal repro:
```
#define likely(expr) __builtin_expect((expr) != 0, 1)
bool f(int** p) {
  return *p != nullptr && **p == 1;
}
void h();
void i();
void g(int** p) {
  if (likely(f(p))) h(); else i();
}
```
Compile: `clang++ test.cpp -O3 -emit-llvm -S` gives following result ([https://godbolt.org/z/81rrxbhab](https://godbolt.org/z/81rrxbhab)):
```
... omitted ...
  %6 = icmp eq i32 %5, 1                    # <- **p == 1 check
  br i1 %6, label %7, label %8, !prof !49   # <- metadata broken
... omitted ...
  # -2147483648 means invalid. Should be "i32 2000, i32 1" (due to __builtin_expect)
  !49 = !{!"branch_weights", !"expected", i32 -2147483648, i32 0}  
```

After bisecting, this bug is introduced by commit [#a1b78fb](https://github.com/llvm/llvm-project/commit/a1b78fb929fccf96acaa0212cf68fee82298e747), authored by Evgeniy Brevnov (ybrevnov@azul.com) and reviewed by @xortator (Max Kazantsev). Reverting the commit fixes the issue.

Ironically, this commit is already breaking existing tests by producing invalid metadata (see [#a1b78fb](https://github.com/llvm/llvm-project/commit/a1b78fb929fccf96acaa0212cf68fee82298e747) at the end). Instead of fixing the issues, the author changed the expected test output to the invalid metadata.
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJzEVUGPozgT_TWVSykICkjgkEPS-SKNvhmttCPttWVDEbzt2Kxt0kn_-pWBnumZ7pF2T9tCHSi7Hu-9qjLCe3U2zDsoD1AeV2IMvXU7r7S-N856v5K2ve--KKMuQqPjwVnI95DuYZMu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