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

    <tr>
        <th>Summary</th>
        <td>
            [clang] unnecessary conditions marked with [[likely]] or [[unlikely]] are not removed by the optimizer
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            clang
      </td>
    </tr>

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

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

<pre>
    https://godbolt.org/z/jEM7nrKhs

With the following code

```cpp
struct A {
    int type;
};

struct B : A {
    int value;
};

struct C : A {
    int value;
    int value2;
};

int foo(A *p) {
    if (p->type == 0) [[likely]] return static_cast<B*>(p)->value;
    return static_cast<C*>(p)->value;
}

int bar(A *p) {
    if (p->type == 0) [[unlikely]] return static_cast<B*>(p)->value;
    return static_cast<C*>(p)->value;
}

int zeta(A *p) {
    if (p->type == 0) return static_cast<B*>(p)->value;
    return static_cast<C*>(p)->value;
}
```

The offset of `B::value` and `C::value` are identical, so the 2 branches should give the same code. This is the case with gcc and the zeta function:

```asm
mov     eax, DWORD PTR [rdi+4]
ret
```

On the other hand, the 2 branches using `[[(un)likely]]` retain the `cmp` instruction with clang:

```asm
cmp     dword ptr [rdi], 0
mov     eax, dword ptr [rdi + 4]
ret
```

Testes with clang-15, clang-16 and clang-trunk.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzMVU1v4zYQ_TWjyyCGRFmWddDBtuJLUWyxCLDHgiJHFhOKFEjKqfPrC0pZrOsam6A9tIBhy08zj_OG88G9VydDVEOxh6JJ-BR66-pna3jouRktaTJJa-Wl7kMYPeQ7YEdgx5OVrdVhZd0J2PEN2PH58dfSuF96D2kD6W75_qZCj6En7KzW9lWZEwor6doENunyEeO4ID64SQTcIZT7BUFEVCZguIwE-TsIZfPj-dpxj5Dv7rmfuZ4-4X_4nP9fUPYT2mjUWQtsu0NguxFYdcPdIbDt-AD5Y1SIkDeQN5jOhvFi9lq9kL5A0UDRoKMwOYM-8KDE74L7APlhD2wH-WPkAVZFqr9He9fx8IFj1HMjpuXu34iZzP9LzhsF_s_0_Fehf2-ZayVPPaHtOk8BbYewSfexWfPdwrFJkRsZ4cMt7AiVJBOU4BrYAb2dO5Zh67gRPXn0vZ20xJM60_zK84HmPl7hU688Kj_DgnvC19jxJyHm4yIas4vdZERQ1sSz7_U-98OCDPYc84PE_4ixNN--fG3wt6evsXScVMD261g2s62j8JN8fDHz8Tb05LDnRka-G2GTjxMpOs6FCWw7GWDVdXnGFDkKXC10cU4NYwSVWcaFsmYRLTQ3pw8FimGcBcpX6ySOwX1XVjQxwPRuGm6NEdgeP5uIJ_KB_FWMD1kRSd-fN_NNLX-Cm8zLKpF1Lqu84gnV2aYqt1Wel1XS15nkRcXbtCxE2cptRqzM1uuyEtTlrGMiUTVLWZ6lLMvylBXbVVdVHeXFOmtFKrbtBtYpDVzpldbnIS6PRHk_Ub2ptuss0bwl7eddxNiSTsbiWnJ1tH9op5OHdaqVD_4HQ1BBzwts8SganIwhQd5zd0FhjVTxkjwO3L2QXBJxZ6xad38-xQYxNqCjwZ5JYntZCmsMalBv5JLJ6dvtqEI_tSthB2DHGOj7z8Po7DOJAOw46_bAjrP0PwMAAP__TQ0oIA">