[llvm] [SimplifyCFG] Handle `llvm.assume` in `passingValueIsAlwaysUndefined` (PR #89929)
Yingwei Zheng via llvm-commits
llvm-commits at lists.llvm.org
Wed Apr 24 07:20:25 PDT 2024
https://github.com/dtcxzyw updated https://github.com/llvm/llvm-project/pull/89929
>From 11df44e8b583d0a05cfd5ee2532d583d0bb51fdd Mon Sep 17 00:00:00 2001
From: Yingwei Zheng <dtcxzyw2333 at gmail.com>
Date: Wed, 24 Apr 2024 21:20:33 +0800
Subject: [PATCH 1/3] [SimplifyCFG] Add pre-commit tests. NFC.
---
.../SimplifyCFG/UnreachableEliminate.ll | 181 ++++++++++++++++++
1 file changed, 181 insertions(+)
diff --git a/llvm/test/Transforms/SimplifyCFG/UnreachableEliminate.ll b/llvm/test/Transforms/SimplifyCFG/UnreachableEliminate.ll
index 757340527ec030..75c4e6574d3587 100644
--- a/llvm/test/Transforms/SimplifyCFG/UnreachableEliminate.ll
+++ b/llvm/test/Transforms/SimplifyCFG/UnreachableEliminate.ll
@@ -627,7 +627,188 @@ else:
ret void
}
+define i32 @test_assume_false(i32 %cond) {
+; CHECK-LABEL: @test_assume_false(
+; CHECK-NEXT: entry:
+; CHECK-NEXT: switch i32 [[COND:%.*]], label [[DEFAULT:%.*]] [
+; CHECK-NEXT: i32 0, label [[EXIT:%.*]]
+; CHECK-NEXT: i32 1, label [[CASE1:%.*]]
+; CHECK-NEXT: i32 2, label [[CASE2:%.*]]
+; CHECK-NEXT: ]
+; CHECK: case1:
+; CHECK-NEXT: br label [[EXIT]]
+; CHECK: case2:
+; CHECK-NEXT: br label [[EXIT]]
+; CHECK: default:
+; CHECK-NEXT: br label [[EXIT]]
+; CHECK: exit:
+; CHECK-NEXT: [[BOOL:%.*]] = phi i1 [ false, [[DEFAULT]] ], [ true, [[CASE1]] ], [ true, [[CASE2]] ], [ true, [[ENTRY:%.*]] ]
+; CHECK-NEXT: [[RES:%.*]] = phi i32 [ 0, [[DEFAULT]] ], [ 2, [[CASE1]] ], [ 3, [[CASE2]] ], [ 1, [[ENTRY]] ]
+; CHECK-NEXT: call void @llvm.assume(i1 [[BOOL]])
+; CHECK-NEXT: ret i32 [[RES]]
+;
+entry:
+ switch i32 %cond, label %default [
+ i32 0, label %case0
+ i32 1, label %case1
+ i32 2, label %case2
+ ]
+
+case0:
+ br label %exit
+
+case1:
+ br label %exit
+
+case2:
+ br label %exit
+
+default:
+ br label %exit
+
+exit:
+ %bool = phi i1 [ false, %default ], [ true, %case0 ], [ true, %case1 ], [ true, %case2 ]
+ %res = phi i32 [ 0, %default ], [ 1, %case0 ], [ 2, %case1 ], [ 3, %case2 ]
+ call void @llvm.assume(i1 %bool)
+ ret i32 %res
+}
+
+define i32 @test_assume_undef(i32 %cond) {
+; CHECK-LABEL: @test_assume_undef(
+; CHECK-NEXT: entry:
+; CHECK-NEXT: switch i32 [[COND:%.*]], label [[DEFAULT:%.*]] [
+; CHECK-NEXT: i32 0, label [[EXIT:%.*]]
+; CHECK-NEXT: i32 1, label [[CASE1:%.*]]
+; CHECK-NEXT: i32 2, label [[CASE2:%.*]]
+; CHECK-NEXT: ]
+; CHECK: case1:
+; CHECK-NEXT: br label [[EXIT]]
+; CHECK: case2:
+; CHECK-NEXT: br label [[EXIT]]
+; CHECK: default:
+; CHECK-NEXT: unreachable
+; CHECK: exit:
+; CHECK-NEXT: [[RES:%.*]] = phi i32 [ 2, [[CASE1]] ], [ 3, [[CASE2]] ], [ 1, [[ENTRY:%.*]] ]
+; CHECK-NEXT: call void @llvm.assume(i1 true)
+; CHECK-NEXT: ret i32 [[RES]]
+;
+entry:
+ switch i32 %cond, label %default [
+ i32 0, label %case0
+ i32 1, label %case1
+ i32 2, label %case2
+ ]
+
+case0:
+ br label %exit
+
+case1:
+ br label %exit
+
+case2:
+ br label %exit
+
+default:
+ br label %exit
+
+exit:
+ %bool = phi i1 [ undef, %default ], [ true, %case0 ], [ true, %case1 ], [ true, %case2 ]
+ %res = phi i32 [ 0, %default ], [ 1, %case0 ], [ 2, %case1 ], [ 3, %case2 ]
+ call void @llvm.assume(i1 %bool)
+ ret i32 %res
+}
+define i32 @test_assume_var(i32 %cond, i1 %var) {
+; CHECK-LABEL: @test_assume_var(
+; CHECK-NEXT: entry:
+; CHECK-NEXT: switch i32 [[COND:%.*]], label [[DEFAULT:%.*]] [
+; CHECK-NEXT: i32 0, label [[EXIT:%.*]]
+; CHECK-NEXT: i32 1, label [[CASE1:%.*]]
+; CHECK-NEXT: i32 2, label [[CASE2:%.*]]
+; CHECK-NEXT: ]
+; CHECK: case1:
+; CHECK-NEXT: br label [[EXIT]]
+; CHECK: case2:
+; CHECK-NEXT: br label [[EXIT]]
+; CHECK: default:
+; CHECK-NEXT: br label [[EXIT]]
+; CHECK: exit:
+; CHECK-NEXT: [[BOOL:%.*]] = phi i1 [ [[VAR:%.*]], [[DEFAULT]] ], [ true, [[CASE1]] ], [ true, [[CASE2]] ], [ true, [[ENTRY:%.*]] ]
+; CHECK-NEXT: [[RES:%.*]] = phi i32 [ 0, [[DEFAULT]] ], [ 2, [[CASE1]] ], [ 3, [[CASE2]] ], [ 1, [[ENTRY]] ]
+; CHECK-NEXT: call void @llvm.assume(i1 [[BOOL]])
+; CHECK-NEXT: ret i32 [[RES]]
+;
+entry:
+ switch i32 %cond, label %default [
+ i32 0, label %case0
+ i32 1, label %case1
+ i32 2, label %case2
+ ]
+
+case0:
+ br label %exit
+
+case1:
+ br label %exit
+
+case2:
+ br label %exit
+
+default:
+ br label %exit
+
+exit:
+ %bool = phi i1 [ %var, %default ], [ true, %case0 ], [ true, %case1 ], [ true, %case2 ]
+ %res = phi i32 [ 0, %default ], [ 1, %case0 ], [ 2, %case1 ], [ 3, %case2 ]
+ call void @llvm.assume(i1 %bool)
+ ret i32 %res
+}
+
+define i32 @test_assume_bundle(i32 %cond, ptr nonnull %p) {
+; CHECK-LABEL: @test_assume_bundle(
+; CHECK-NEXT: entry:
+; CHECK-NEXT: switch i32 [[COND:%.*]], label [[DEFAULT:%.*]] [
+; CHECK-NEXT: i32 0, label [[EXIT:%.*]]
+; CHECK-NEXT: i32 1, label [[CASE1:%.*]]
+; CHECK-NEXT: i32 2, label [[CASE2:%.*]]
+; CHECK-NEXT: ]
+; CHECK: case1:
+; CHECK-NEXT: br label [[EXIT]]
+; CHECK: case2:
+; CHECK-NEXT: br label [[EXIT]]
+; CHECK: default:
+; CHECK-NEXT: br label [[EXIT]]
+; CHECK: exit:
+; CHECK-NEXT: [[PTR:%.*]] = phi ptr [ null, [[DEFAULT]] ], [ [[P:%.*]], [[CASE1]] ], [ [[P]], [[CASE2]] ], [ [[P]], [[ENTRY:%.*]] ]
+; CHECK-NEXT: [[RES:%.*]] = phi i32 [ 0, [[DEFAULT]] ], [ 2, [[CASE1]] ], [ 3, [[CASE2]] ], [ 1, [[ENTRY]] ]
+; CHECK-NEXT: call void @llvm.assume(i1 true) [ "nonnull"(ptr [[PTR]]) ]
+; CHECK-NEXT: ret i32 [[RES]]
+;
+entry:
+ switch i32 %cond, label %default [
+ i32 0, label %case0
+ i32 1, label %case1
+ i32 2, label %case2
+ ]
+
+case0:
+ br label %exit
+
+case1:
+ br label %exit
+
+case2:
+ br label %exit
+
+default:
+ br label %exit
+
+exit:
+ %ptr = phi ptr [ null, %default ], [ %p, %case0 ], [ %p, %case1 ], [ %p, %case2 ]
+ %res = phi i32 [ 0, %default ], [ 1, %case0 ], [ 2, %case1 ], [ 3, %case2 ]
+ call void @llvm.assume(i1 true) [ "nonnull"(ptr %ptr) ]
+ ret i32 %res
+}
attributes #0 = { null_pointer_is_valid }
;.
>From 41fc4c30235c0c78c9d685bb2b9cb235f0431448 Mon Sep 17 00:00:00 2001
From: Yingwei Zheng <dtcxzyw2333 at gmail.com>
Date: Wed, 24 Apr 2024 21:32:55 +0800
Subject: [PATCH 2/3] [SimplifyCFG] Handle `llvm.assume` in
`passingValueIsAlwaysUndefined`
---
llvm/lib/Transforms/Utils/SimplifyCFG.cpp | 7 +++++++
llvm/test/Transforms/SimplifyCFG/UnreachableEliminate.ll | 7 +++----
2 files changed, 10 insertions(+), 4 deletions(-)
diff --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
index 0826d748ba0d2a..625965edafc967 100644
--- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -7516,6 +7516,13 @@ static bool passingValueIsAlwaysUndefined(Value *V, Instruction *I, bool PtrValu
SI->getPointerAddressSpace())) &&
SI->getPointerOperand() == I;
+ // llvm.assume(false/undef) always triggers immediate UB.
+ if (auto *Assume = dyn_cast<AssumeInst>(Use)) {
+ // Ignore assume operand bundles.
+ if (I == Assume->getArgOperand(0))
+ return true;
+ }
+
if (auto *CB = dyn_cast<CallBase>(Use)) {
if (C->isNullValue() && NullPointerIsDefined(CB->getFunction()))
return false;
diff --git a/llvm/test/Transforms/SimplifyCFG/UnreachableEliminate.ll b/llvm/test/Transforms/SimplifyCFG/UnreachableEliminate.ll
index 75c4e6574d3587..9b477da6f6fdb3 100644
--- a/llvm/test/Transforms/SimplifyCFG/UnreachableEliminate.ll
+++ b/llvm/test/Transforms/SimplifyCFG/UnreachableEliminate.ll
@@ -640,11 +640,10 @@ define i32 @test_assume_false(i32 %cond) {
; CHECK: case2:
; CHECK-NEXT: br label [[EXIT]]
; CHECK: default:
-; CHECK-NEXT: br label [[EXIT]]
+; CHECK-NEXT: unreachable
; CHECK: exit:
-; CHECK-NEXT: [[BOOL:%.*]] = phi i1 [ false, [[DEFAULT]] ], [ true, [[CASE1]] ], [ true, [[CASE2]] ], [ true, [[ENTRY:%.*]] ]
-; CHECK-NEXT: [[RES:%.*]] = phi i32 [ 0, [[DEFAULT]] ], [ 2, [[CASE1]] ], [ 3, [[CASE2]] ], [ 1, [[ENTRY]] ]
-; CHECK-NEXT: call void @llvm.assume(i1 [[BOOL]])
+; CHECK-NEXT: [[RES:%.*]] = phi i32 [ 2, [[CASE1]] ], [ 3, [[CASE2]] ], [ 1, [[ENTRY:%.*]] ]
+; CHECK-NEXT: call void @llvm.assume(i1 true)
; CHECK-NEXT: ret i32 [[RES]]
;
entry:
>From c31691db36b88ca8ef7336c7d3c106afe574eea5 Mon Sep 17 00:00:00 2001
From: Yingwei Zheng <dtcxzyw2333 at gmail.com>
Date: Wed, 24 Apr 2024 22:19:41 +0800
Subject: [PATCH 3/3] [SimplifyCFG] Add additional tests. NFC.
---
.../SimplifyCFG/UnreachableEliminate.ll | 50 ++++++++++++++++++-
1 file changed, 48 insertions(+), 2 deletions(-)
diff --git a/llvm/test/Transforms/SimplifyCFG/UnreachableEliminate.ll b/llvm/test/Transforms/SimplifyCFG/UnreachableEliminate.ll
index 9b477da6f6fdb3..ef2d3219cca9b6 100644
--- a/llvm/test/Transforms/SimplifyCFG/UnreachableEliminate.ll
+++ b/llvm/test/Transforms/SimplifyCFG/UnreachableEliminate.ll
@@ -763,8 +763,8 @@ exit:
ret i32 %res
}
-define i32 @test_assume_bundle(i32 %cond, ptr nonnull %p) {
-; CHECK-LABEL: @test_assume_bundle(
+define i32 @test_assume_bundle_nonnull(i32 %cond, ptr nonnull %p) {
+; CHECK-LABEL: @test_assume_bundle_nonnull(
; CHECK-NEXT: entry:
; CHECK-NEXT: switch i32 [[COND:%.*]], label [[DEFAULT:%.*]] [
; CHECK-NEXT: i32 0, label [[EXIT:%.*]]
@@ -809,6 +809,52 @@ exit:
ret i32 %res
}
+define i32 @test_assume_bundle_align(i32 %cond, ptr nonnull %p) {
+; CHECK-LABEL: @test_assume_bundle_align(
+; CHECK-NEXT: entry:
+; CHECK-NEXT: switch i32 [[COND:%.*]], label [[DEFAULT:%.*]] [
+; CHECK-NEXT: i32 0, label [[EXIT:%.*]]
+; CHECK-NEXT: i32 1, label [[CASE1:%.*]]
+; CHECK-NEXT: i32 2, label [[CASE2:%.*]]
+; CHECK-NEXT: ]
+; CHECK: case1:
+; CHECK-NEXT: br label [[EXIT]]
+; CHECK: case2:
+; CHECK-NEXT: br label [[EXIT]]
+; CHECK: default:
+; CHECK-NEXT: br label [[EXIT]]
+; CHECK: exit:
+; CHECK-NEXT: [[PTR:%.*]] = phi ptr [ null, [[DEFAULT]] ], [ [[P:%.*]], [[CASE1]] ], [ [[P]], [[CASE2]] ], [ [[P]], [[ENTRY:%.*]] ]
+; CHECK-NEXT: [[RES:%.*]] = phi i32 [ 0, [[DEFAULT]] ], [ 2, [[CASE1]] ], [ 3, [[CASE2]] ], [ 1, [[ENTRY]] ]
+; CHECK-NEXT: call void @llvm.assume(i1 true) [ "align"(ptr [[PTR]], i32 8) ]
+; CHECK-NEXT: ret i32 [[RES]]
+;
+entry:
+ switch i32 %cond, label %default [
+ i32 0, label %case0
+ i32 1, label %case1
+ i32 2, label %case2
+ ]
+
+case0:
+ br label %exit
+
+case1:
+ br label %exit
+
+case2:
+ br label %exit
+
+default:
+ br label %exit
+
+exit:
+ %ptr = phi ptr [ null, %default ], [ %p, %case0 ], [ %p, %case1 ], [ %p, %case2 ]
+ %res = phi i32 [ 0, %default ], [ 1, %case0 ], [ 2, %case1 ], [ 3, %case2 ]
+ call void @llvm.assume(i1 true) [ "align"(ptr %ptr, i32 8) ]
+ ret i32 %res
+}
+
attributes #0 = { null_pointer_is_valid }
;.
; CHECK: attributes #[[ATTR0:[0-9]+]] = { nocallback nofree nosync nounwind willreturn memory(inaccessiblemem: write) }
More information about the llvm-commits
mailing list