diff --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp --- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp +++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp @@ -7126,6 +7126,22 @@ return passingValueIsAlwaysUndefined(V, GEP, PtrValueMayBeModified); } + // Look through return. + if (ReturnInst *Ret = dyn_cast(Use)) { + // Return undefined to a noundef return value is undefined. + if (isa(C) && + Ret->getFunction()->hasRetAttribute(Attribute::NoUndef)) + return true; + if (C->isNullValue()) { + // Return null to a nonnnull return value is undefined. + if (Ret->getFunction()->hasRetAttribute(Attribute::NonNull)) + return true; + // Return null to a noundef return value may is undefined. + if (Ret->getFunction()->hasRetAttribute(Attribute::NoUndef)) + return !NullPointerIsDefined(Ret->getFunction()); + } + } + // Look through bitcasts. if (BitCastInst *BC = dyn_cast(Use)) return passingValueIsAlwaysUndefined(V, BC, PtrValueMayBeModified); diff --git a/llvm/test/Transforms/SimplifyCFG/unreachable-eliminate-on-ret.ll b/llvm/test/Transforms/SimplifyCFG/unreachable-eliminate-on-ret.ll new file mode 100644 --- /dev/null +++ b/llvm/test/Transforms/SimplifyCFG/unreachable-eliminate-on-ret.ll @@ -0,0 +1,105 @@ +; NOTE: Assertions have been autogenerated by utils/update_test_checks.py +; RUN: opt -passes=simplifycfg -simplifycfg-require-and-preserve-domtree=1 -S < %s | FileCheck %s + +define noundef i32 @test_ret_noundef(i1 %cond) { +; CHECK-LABEL: @test_ret_noundef( +; CHECK-NEXT: entry: +; CHECK-NEXT: ret i32 1 +; +entry: + br i1 %cond, label %bb1, label %bb2 + +bb1: + br label %bb2 + +bb2: + %r = phi i32 [ undef, %entry ], [ 1, %bb1 ] + ret i32 %r +} + +define i32 @test_ret(i1 %cond) { +; CHECK-LABEL: @test_ret( +; CHECK-NEXT: entry: +; CHECK-NEXT: [[SPEC_SELECT:%.*]] = select i1 [[COND:%.*]], i32 1, i32 undef +; CHECK-NEXT: ret i32 [[SPEC_SELECT]] +; +entry: + br i1 %cond, label %bb1, label %bb2 + +bb1: + br label %bb2 + +bb2: + %r = phi i32 [ undef, %entry ], [ 1, %bb1 ] + ret i32 %r +} + +define nonnull ptr @test_ret_nonnull(i1 %cond, ptr %x) { +; CHECK-LABEL: @test_ret_nonnull( +; CHECK-NEXT: entry: +; CHECK-NEXT: call void @llvm.assume(i1 [[COND:%.*]]) +; CHECK-NEXT: ret ptr [[X:%.*]] +; +entry: + br i1 %cond, label %bb1, label %bb2 + +bb1: + br label %bb2 + +bb2: + %r = phi ptr [ null, %entry ], [ %x, %bb1 ] + ret ptr %r +} + +define ptr @test_ret_ptr(i1 %cond, ptr %x) { +; CHECK-LABEL: @test_ret_ptr( +; CHECK-NEXT: entry: +; CHECK-NEXT: [[SPEC_SELECT:%.*]] = select i1 [[COND:%.*]], ptr [[X:%.*]], ptr null +; CHECK-NEXT: ret ptr [[SPEC_SELECT]] +; +entry: + br i1 %cond, label %bb1, label %bb2 + +bb1: + br label %bb2 + +bb2: + %r = phi ptr [ null, %entry ], [ %x, %bb1 ] + ret ptr %r +} + +define noundef ptr @test_ret_ptr_noundef(i1 %cond, ptr %x) { +; CHECK-LABEL: @test_ret_ptr_noundef( +; CHECK-NEXT: entry: +; CHECK-NEXT: call void @llvm.assume(i1 [[COND:%.*]]) +; CHECK-NEXT: ret ptr [[X:%.*]] +; +entry: + br i1 %cond, label %bb1, label %bb2 + +bb1: + br label %bb2 + +bb2: + %r = phi ptr [ null, %entry ], [ %x, %bb1 ] + ret ptr %r +} + +define noundef ptr @test_ret_ptr_noundef_2(i1 %cond, ptr %x) #0 { +; CHECK-LABEL: @test_ret_ptr_noundef_2( +; CHECK-NEXT: entry: +; CHECK-NEXT: [[SPEC_SELECT:%.*]] = select i1 [[COND:%.*]], ptr [[X:%.*]], ptr null +; CHECK-NEXT: ret ptr [[SPEC_SELECT]] +; +entry: + br i1 %cond, label %bb1, label %bb2 + +bb1: + br label %bb2 + +bb2: + %r = phi ptr [ null, %entry ], [ %x, %bb1 ] + ret ptr %r +} + +attributes #0 = { null_pointer_is_valid }