<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=http://email.email.llvm.org/c/eJy1ldtu2zgQhp9GvhlE0MFOrAtduE5btM3uFuhibwOKGllsaI5AUrHTp98hpdhOusAuti1A60AO5_R_lBtqn-qkKE8DvvRkPTyidYpMkt0m2eYLIvgewYn9oBEktQgf-LYfyGELykCyerPlV6XRwtvjoMmiTVa3SbHuvR9cUm6S4h2PnZTpjtqGtE_J7njmG_8-V5LEJ3uskqJKp5Dz9TKxOzK7l3l98OAQ946TEx6kFmzQEjow5GGHBq3wc7rCwT0eB5Qe23voyMaCOtKaDoq3DcJ7tCYkOkW-zqYhh2GaeSTVgkfnuSjOcza7eTM9AFw8AjREmgObFpLylsPQvKm8sFEd8Oz9fTMq7ZWZ0-Ops3mxhTzeK5j6B5w495qHVg-on0KPeZx8AuzIE3x9z9eLYMnNyWb20wibQ5rO3Z7tN98bFWejk5NTb6bXP7mRB7LtZYN7tAh7FIa1IcC98rHfLTplmZjGCiP7UD57CVGCMy7SjTwbxeQNyoFGx5dYafDjxq5jwDpL-2cXA7tT0jMSMKAR2j-l8IeB4_o6NI-9uJ5G3cKghUQ4RwOOMbm4YliuvHhAEyDoA828O-Rm1a73IDoGAwR8lJLXnLdjDBfcM1Ts_hDxDxX_P-JfQ7-BAypuZyek52NmUWj1LRyzmfPtmfP_wvhMNhyU51ZMOrjRWhpNG8hvNMmHH-D-31H_FaD_M-Y_ne0z4cpwjU45j4bJwV48Kk6RuthP-fzto9EyktROjERZQm09DdiNmim-u_vrN5DCQKeOjOcL4X8nj5woay60o9hzlt33LHwQD95vtzPU4UA5UC3noqTQk_xB8IbYblY8XbR12VZlJRZiZO1t_Xaj7GfktcVodf2KU44wNilXwi9aPz7frricr1Gud8q5ER0_rMoyWy_6Om_WxbJoZL4ub6r1qlyt18uVqDDPs-pa5suFFg1qVwchi8LgAaILfmYtFz-egaqLrCiyvMiyVV5mebrOqmUmuqK8Ft2yLVbJMsO9UDoNfsLxW9g6umzGneNFzXq686JwTu0MYkyYM_TKa6y3L_9XejLc6Nc0gwgH4HtgubI4OZrLaTj0_LkZw59nVDbSzB8eMZ3GK-f5UO9Z3XQRS65jvX8DbipYsg>53308</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            Clang does not honor __builtin_expect and [[likely]]/[[unlikely]] when used with goto in a block-statement.
        </td>
    </tr>

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

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

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

<pre>
    #### Short version
See the sample code I composed in [Compiler Explorer](https://gcc.godbolt.org/z/P9coaKrx9).

#### Long version
It seems that clang does not generate code as _expected_ for the following pattern:
```cpp
void test()
{
  {
    bool cond = foo();
    if (__builtin_expect(foo(), 1)) // or [[likely]]
      goto jGoto;
  }
  // bar1 ...
jGoto:
  // bar2 ...
}
```
The word _expected_ here means to emit the desired branch (`bar2`) such that it is less likely to suffer from branch prediction penalty. On x86, it should place `bar2` at branch-not-taken path in x86 (right after a Jcc instruction, as shown [here](https://gcc.godbolt.org/z/P9coaKrx9)).

A weird fact I realized is that Clang does generate code as _expected_ for the pattern without the surrounding block:
```cpp
void test()
{
  bool cond = foo();
  if (__builtin_expect(foo(), 1)) // or [[likely]]
    goto jGoto;
  // bar1 ...
jGoto:
  // bar2 ...
}
```

The inconsistent behavior of the compiler our production code, hopefully LLVM can fix it.

Note: I also tested this with GCC, it emits identical code for both pattern.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJy1VU1v2zgQ_TXyZRBBlmLXPujgOm3RNrtboEWvAUWOLDY0RyCp2Omv3yGl2E5aoMV-ALREUcOZx_feyA2pxzorq9OAzx25AA_ovCabFTdZsfmMCKFD8GLfGwRJCuE93_Y9eVSgLWSL11t-1AYdvDn2hhy6bHGTlasuhN5n1SYr3_LYSZnvSDVkQk5uxyvf-fdpL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