[llvm] [SimplifyCFG] Handle `llvm.assume` in `passingValueIsAlwaysUndefined` (PR #89929)
Nikita Popov via llvm-commits
llvm-commits at lists.llvm.org
Wed Apr 24 07:07:14 PDT 2024
================
@@ -627,7 +627,187 @@ 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: 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 [ 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) ]
----------------
nikic wrote:
It might make to pick a different bundle here (like aligned) for which null is actually valid, so this is a proper negative test. For nonnull specifically the fold would be fine.
https://github.com/llvm/llvm-project/pull/89929
More information about the llvm-commits
mailing list