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

    <tr>
        <th>Summary</th>
        <td>
            [CodeGen] Eliminate branches using `undef`
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            llvm:codegen,
            missed-optimization
      </td>
    </tr>

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

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

<pre>
    I tried this IR:

```llvm
define { i64, i64 } @src(i64 noundef %arg, i64 noundef %arg1) local_unnamed_addr #0 {
bb:
  switch i64 %arg1, label %bb7 [
    i64 2, label %bb3
    i64 3, label %bb2
 ]

bb2:                                              ; preds = %bb
 br label %bb3

bb7:                                              ; preds = %bb
  %i8 = udiv i64 %arg, %arg1
  %i13 = shl nuw i64 %i8, 1
  br label %bb3

bb3: ; preds = %bb7, %bb2, %bb
  %i = phi i64 [ %i13, %bb7 ], [ %arg, %bb2 ], [ undef, %bb ]
  %i4 = phi i64 [ 1, %bb7 ], [ 1, %bb2 ], [ %arg1, %bb ]
 %i5 = insertvalue { i64, i64 } poison, i64 %i4, 0
  %i6 = insertvalue { i64, i64 } %i5, i64 %i, 1
  ret { i64, i64 } %i6
}
```

The assembly code is:

```asm
src: # @src
  cmp rsi, 2
  je .LBB0_1
  cmp rsi, 3
  je .LBB0_4
  mov rax, rdi
  xor edx, edx
  div rsi
  mov rdi, rax
  add rdi, rax
.LBB0_4: # %bb3
 mov esi, 1
  jmp .LBB0_5
.LBB0_1:
.LBB0_5: # %bb3
  mov rax, rsi
  mov rdx, rdi
  ret
```

We can merge `.LBB0_1` and `.LBB0_5`.

This MIR is:

```llvm
bb.1:
; predecessors: %bb.0
  successors: %bb.5(0x80000000); %bb.5(100.00%)
  liveins: $rsi
  renamable $rdi = IMPLICIT_DEF
```

I think we can remove this BB because we are assigning `undef` to `$rdi`.

GodBolt: https://llvm.godbolt.org/z/joff5fYs9.
From: https://github.com/rust-lang/rust/issues/126585.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJysVl-P2ygQ_zTkBW2EwXacBz80m6aKrpXuqkqne1qBGTvs2RABTrf99CewnTjZNOrDWVEMM8PvN39gMHdONRqgRNkGZdsF7_3B2HKruP7rj4Uw8ke5x94qkNgflMP7r4h9QGSLyPSfk-HXtqduEEmolQaMVhus8hTR5_DCaLXFKCXOVogWQaBNryXUGNGM22Yyu5YmiK5xayrevvRa8w7kC5fSYkQZCQQDoRBnpzB235WvDgPlhPGMWy6gDQIhVjjEOlrjaEhvTNi1mt2o6ahG2XaeiqBgcdHvP4ht8NGCdBix7YA-ggt7x6OJaPU_EoWJKqK4l-o0y1wIe8rhzDZh0dgdWqz775O9KoL52fCh-yy4f8ej1cgYMjmN5szR8nhQA2e2Gd05265iScJs0F1iEILOdXGPnVWXQg406Tue5D5Fch99tu9u8QN8FuGVdmD9ibf93aNyNMoZfZYEr8KEzP3MfwcoMs5xrspkwf9qUT7WbLW9OenzWn47AObOQSfaH7gyErByv-oQ3I0NIvSAsAEomzrC6E3VHbF10cPpjOFXwMvPmw15Se5YsXdW6STpzAlb_hasrFST9M1YDDJKw2uUhm0fEOdLZSQICKOUS_lOOnFO0cybR0ABd53u1-44-pnNAZJzyiblPcCrkG69vQ3Ugn9Qtr8BV1zjDmwDGOVkciMnmGt5kWRhdF1v5fCX_dcHZb5cBEIsL5FNxx0qcM5YN4SYCbE872nXV--UGaIFeSvI8CC6DjgXVULIMoizoBlhWnUCpUeMdJYpC5p3XLQQ5XJoJ_svf37eP--_vWw_7h5kbB_uP_0v_j5kzkJnTjDciZsNFlDx3kHQchtPhGq00k3I5NBrcoK9CdOB-javn4zcmNYHnw_eH2Ny6Q7RXcjmsjFSmNYvTehnu5-I7l5NXWf1P249ouys6d4vbpQ_9GJZmQ7Rne2df2q5bsYxojvlXA8O0V1C86zIlgtZMrlma76AMlklxSpZJYwsDmXNhJBFzlNIa5bUGc0J0ByKJM85ITJfqJISmpI8KRJGM0aW67qmNZPrlFR5IesKpQQ6rtplDMjYZhHJy3W2pskiXhUufoRQGjcQ-xC6SQMa0XAXIEo75RzIJ3P0qlM_uVcm6rLtwpZhyZPoG4dS0irn3YXGK9_Gz5tnI-ETaJRt8cdWdUpzD1hYrqsDONy7m3ItetuWD9IZvRxeT0drXqG6yugQ16mk_wUAAP__Mo555w">