<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/78512>78512</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Clang does not propagate __builtin_expect probability information outside of function
</td>
</tr>
<tr>
<th>Labels</th>
<td>
clang
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
vient
</td>
</tr>
</table>
<pre>
Hi, I've created a small test to see how __builtin_expect variants work in Clang and GCC and noticed the difference which can be seen here https://godbolt.org/z/8ccTP7rY1
The gist of it is as follows: function `f` returns bool with expectation. Function `g` uses this bool in if statement where in both branches something non-trivial is executed. I expected Clang to propagate probability info outside of `f` function but in this case it does not, both probabilities 0 and 1 produce the same code. GCC produces different code for these cases, swapping branches when probability is flipped.
Optimizations are turned on, `f` is inlined in `g`. I also tried LTO with the same result.
If I manually inline `f` in `g`, Clang starts using probability info https://godbolt.org/z/1qvfbjbqG
Is my testcase too simple for Clang? Is it a problem in Clang, or maybe a known and expected difference in implementations between Clang and GCC?
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyElM-O2zgMxp9GuRAT2Mq_ycGH7hTuBlige5jLngayTMdsZckV6bjp0y-kZDKZzqGXKIgYkt_3I2WY6egRK7X5S20-L8wkfYjVidDLogntufqblH6Cg9K7E4KNaARbMMCDcQ4EWUACMCL0YYaXl2YiJ-Rf8OeIVuBkIhkvDHOI34E8PDnjj2B8C1-envLpg5DFFqRHaKnrMKK3CHNPtgdrPDSY8nvoMSL0IiOr1Sela6XrY2ib4GQZ4lHp-pfS9aO1z__u4n-lKj6r4tPl87lHOBILhA5IgBgMQxecC3PKBd3krVDwoLZFp7YFRJQpeoYmBAczSQ8XPSZFLaG-iz-m-ImRQXq6_oM8UAcsRnBALzDn1slDE6SHJhpve2TgMKD05I_gg3-QSCcyLnWHP9FOgu0SDtfC2F6dkwBjDKM5GsH0rTENOZIzkO8ChEmYWkw6X6XctDWTpBZyl9YwJifagJwAJMS5t7eMhAxFBlSmX9vJYkbEZkCwocVlJni94hs6yZfQhZjCGXMtTgV4NuOY1N4MmHv070UwdI7GEdvlPb-vo9BAv7L7DCYiJDzYQvAp8atUYiDvKF3QjU3y0DgOIJGwhX-ev16A3rRE5MnJu3qHDg4wGD8Z587XnG9VbqlT7QsVFhOFYeIk7wOVP81s-ePUNd-aH1_e9cAwnPOCZVgSAjANo7tYm8uqVQ0HThxNLupwuK1Y6i1EGMy5QTDw3YfZZ5q3ebrbtTSvKXUa1qvJDcqclu7dvqpVvWirVbtf7c0Cq3JXbDarYr0vFn1lt2vdmN2-0duV2a422upip8u1Lvab0m67BVW60OuiLHda68eyWO7afaexbItSF3r3uFLrAgdDbuncaUj2LIh5wmr3uCn1wpkGHed3Smt7kajTkxWrFP_QTEdW68IRC79lEBKH1UXE67DfLdCH9-p3dnHIftwv1utCLaboqt_QkvRTs7RhULpOPVyPhzGGb2hF6TpLYqXrrOr_AAAA__9IUtyl">