[SimplifyCFG] Check if the return instruction causes undefined behavior
authorDianQK <dianqk@dianqk.net>
Tue, 21 Feb 2023 12:25:59 +0000 (20:25 +0800)
committerDianQK <dianqk@dianqk.net>
Tue, 21 Feb 2023 13:42:13 +0000 (21:42 +0800)
commitb6a0be8ce3114d0c57e7a7d6c3c222986ca506ad
tree31296cade6380e66fdccd2a14d17787bc710d4e8
parent92ef545b8da2af5ae2e2894d1efd61a49d4b20f9
[SimplifyCFG] Check if the return instruction causes undefined behavior

This should fix https://github.com/rust-lang/rust/issues/107681.

Return undefined to a noundef return value is undefined.

Example:

```
define noundef i32 @test_ret_noundef(i1 %cond) {
entry:
  br i1 %cond, label %bb1, label %bb2
bb1:
  br label %bb2
bb2:
  %r = phi i32 [ undef, %entry ], [ 1, %bb1 ]
  ret i32 %r
}
```

Reviewed By: nikic

Differential Revision: https://reviews.llvm.org/D144319
llvm/lib/Transforms/Utils/SimplifyCFG.cpp
llvm/test/Transforms/SimplifyCFG/unreachable-eliminate-on-ret.ll [new file with mode: 0644]