[PATCH] D144319: [SimplifyCFG] Check if the return instruction causes undefined behavior
DianQK via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Sat Feb 18 05:59:05 PST 2023
DianQK updated this revision to Diff 498584.
DianQK marked an inline comment as done.
DianQK added a comment.
Fix typo.
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D144319/new/
https://reviews.llvm.org/D144319
Files:
llvm/lib/Transforms/Utils/SimplifyCFG.cpp
llvm/test/Transforms/SimplifyCFG/unreachable-eliminate-on-ret.ll
Index: llvm/test/Transforms/SimplifyCFG/unreachable-eliminate-on-ret.ll
===================================================================
--- /dev/null
+++ llvm/test/Transforms/SimplifyCFG/unreachable-eliminate-on-ret.ll
@@ -0,0 +1,69 @@
+; 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 noundef ptr @test_ret_nonnull_noundef(i1 %cond, ptr %x) {
+; CHECK-LABEL: @test_ret_nonnull_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 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
+}
Index: llvm/lib/Transforms/Utils/SimplifyCFG.cpp
===================================================================
--- llvm/lib/Transforms/Utils/SimplifyCFG.cpp
+++ llvm/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -7126,6 +7126,20 @@
return passingValueIsAlwaysUndefined(V, GEP, PtrValueMayBeModified);
}
+ // Look through return.
+ if (ReturnInst *Ret = dyn_cast<ReturnInst>(Use)) {
+ bool HasNoUndefAttr =
+ Ret->getFunction()->hasRetAttribute(Attribute::NoUndef);
+ // Return undefined to a noundef return value is undefined.
+ if (isa<UndefValue>(C) && HasNoUndefAttr)
+ return true;
+ // Return null to a nonnull+noundef return value is undefined.
+ if (C->isNullValue() && HasNoUndefAttr &&
+ Ret->getFunction()->hasRetAttribute(Attribute::NonNull)) {
+ return true;
+ }
+ }
+
// Look through bitcasts.
if (BitCastInst *BC = dyn_cast<BitCastInst>(Use))
return passingValueIsAlwaysUndefined(V, BC, PtrValueMayBeModified);
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D144319.498584.patch
Type: text/x-patch
Size: 2848 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230218/e6bd65fb/attachment.bin>
More information about the llvm-commits
mailing list