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

    <tr>
        <th>Summary</th>
        <td>
            [llvm] Potential overflow and incorrect branch weight update
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            llvm:optimizations
      </td>
    </tr>

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

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

<pre>
    The branch weight update of `SwitchInst` in `llvm::ConstantFoldTerminator` (`llvm/lib/Transforms/Utils/Local.cpp`) looks incorrect in two ways:
1. Two `uint32_t` are summed up, which can potentially overflow and lead to incorrect results. Other places that work with these weights perform their computations with 64 bit values. 
2. The newly updated branch weights should not swap the order of the weights, instead it should drop the weight for the removed case and keep the order of the weights. 
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJx0k0-L2zAQxT-NfBlqFPnPxgcfthsChUILTc9FliaxurLGSOOY9NMXebPsLrQnI_lpZt7TTzoldwmIvWg-i-ZQ6IVHiv3BBZc4UjGQvfWnEWGIOpgRVnSXkWGZrWYEOoNo5Y_VsRm_hMSileBC3vP-OonqUVSPTxQS68BH8vaEcXJBM8WsFGr_qlRH7wahjqeoQzpTnJJQx5_sfP5-JaN9aeZZtFKoDjzRcwIXDMWIhnNHXglWfUu5pTwI-bgr4bRSnmRxgSv1a5tNR4S0TBNaWGahnmAdnRnB6AAzMQZ22vsb0BXj2dMKOljwqC0wvesXMS2eUwnfeMQIs9cGE_CoGVaKz7A6HoFHTHiPK8GMMbvKuy6CoWleWLOjkF7UbQ2DY7hqv2Aq4cWDKiEnH3D1t3vi9uM9JEgjLd5CIIa06jk3AIoWY76bvLgLs1kXEmczjl-P2UjzOxWcKW7LiBNd0YLRCbcQnhH_X7uEwvaV7apOF9jv2oeqkftGdsXYn3dam4daSeyqoZHnQRozSGtVI9tu38rC9UqqSu52Sj6overKupat2ledrWu5l6YStcRJO19mTkqKl8KltGDfdF3TFl4P6NMGr1J35mhmN7k_L_EKpTLWsc8_Pw3LJYlaepc4vRVkx357AFuB5gDfX1n4SMIbAf96DMUSfT8yzxuE6ijU8eJ4XIbS0Ab4nfM8xxzpNxoW6riZyZBvfv4GAAD__5cxNRY">