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

    <tr>
        <th>Summary</th>
        <td>
            [SimplifyCFG] Missed optimization: merge switch branches into default if they hold the same value in phi as the default branch
        </td>
    </tr>

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

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

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

<pre>
    Alive2 proof: https://alive2.llvm.org/ce/z/8U-LhB

### Motivating example 

```llvm
define i64 @src(i32 %cmd) #0 {
entry:
  switch i32 %cmd, label %sw.default [
    i32 1, label %sw.bb2
    i32 11, label %sw.epilog
    i32 13, label %sw.bb1
    i32 0, label %sw.bb1
 ]

sw.bb1:                                         ; preds = %entry, %entry
  br label %sw.epilog

sw.bb2: ; preds = %entry
  br label %sw.epilog

sw.default: ; preds = %entry
  br label %sw.epilog

sw.epilog:                                     ; preds = %entry, %sw.default, %sw.bb2, %sw.bb1
  %ret = phi i64 [ 0, %sw.default ], [ 0, %sw.bb1 ], [ 1, %entry ], [ 1, %sw.bb2 ]
  ret i64 %ret
}
```
`switch(13)` and `switch(0)` produce the same `%ret` as default case:
```llvm
define i64 @tgt(i32 %cmd) #0 {
entry:
 switch i32 %cmd, label %sw.epilog [
    i32 1, label %sw.bb2
    i32 11, label %sw.bb1
  ]

sw.bb1: ; preds = %entry
  br label %sw.epilog

sw.bb2: ; preds = %entry
  br label %sw.epilog

sw.epilog:                                     ; preds = %entry, %sw.bb2, %sw.bb1
  %ret = phi i64 [ 1, %sw.bb1 ], [ 1, %sw.bb2 ], [ 0, %entry ]
  ret i64 %ret
}
```

### Real-world motivation

This snippet of IR is derived from [openssl/crypto/bio/bss_conn.c@conn_ctrl](https://github.com/openssl/openssl/blob/c91f0ca95881d03a54aedee197bbf5ffffc02935/crypto/bio/bss_conn.c#L447) (after O3 pipeline).
The example above is a reduced version. If you're interested in the original suboptimal IR and optimal IR, see also:https://godbolt.org/z/nndaqfM9e

**Let me know if you can confirm that it's an optimization opportunity, thanks.**
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJysVs9u4zYTfxr6MohBUVIsHXxIYvjDAht8wLYFeluQ4shilyJVknbqffqClGLL3ibZtDEEWeQM5-9vhsO9VzuDuCblPSk3C74PnXXr3x-640JYeVzfaXVABoOztiX5HXQhDJ7kd4RtCdvyRF1qfeiX1u0I2zZI2PY7Ydvqt5vP3T2hG0LvpjfLxwcebVAHHpTZAf7F-0EjXDDe0vGJcsctia0yCOq2AFJQ7xrCKpUzIKxseklYDYTlFMhq0ogmuGM0M60A_JMKTQfzIw-guUAd1_5pKbHlex0gxmE6A4k9u-YUgl1x_MCCg9J2d8WV_ygou2ShL3OQcjOP0ETL09Gf-pH8HgaH0gPJN1H8GCH2cP6ebBHuRV9myllU_oLQdwiawv4xwqbNn4zKqxGZmXbaiE7PFqfkEVY6DEnK0KkRo-X9mM1rcG3S5iVViGxOyeZJ-SfCaMoZEgBRfVKbLJmistpcVdNpORYDYVXEZE1uKXAjYU6g0_7grNw3CKFD8LzHyDQpiac8PHvWcI-ncnuzfsMuvK9-3yzfMfUfU73z1L5Udv8drB9WQh-L-neCPHsTxjO0XkH_DPB_A-OrK-ULcn3zZJ2W0E-3izVz1l875cEbNQwYwLbw6QuoiF-nDiihdbaPxtkBjfc6XmTuOARL2Fao9Pb-a2ONWTakoPHjaxOcTl5Vl3fiToVuL5aN7QnbnuWdv4S2Iiqos5Y2vC6rKpM052XBUSJm9UqItmzbtm0oq_PyVVtY_rkoVmP9VLwN6OD_OQxqQK0MElYvn73H01XLhT1gdJ6Dw1jeEg7ovLJmCZ9aONo9YSuHoExAhz6gBGVSC7BO7ZThGvxe2CGonusYx9g9zsuYXY8IXHtL8rur4FgprA7TsBDnBGMk_7N9rPEyr_H5jAF6hG_GPoFKhkHDDTTWtMr1EDoeQAX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