[llvm] 716f7e2 - [SimplifyCFG] Add tests for switch over cmp intrinsic (NFC)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Thu Aug 22 02:52:30 PDT 2024


Author: Nikita Popov
Date: 2024-08-22T11:52:02+02:00
New Revision: 716f7e2d18d03039c13ad90d5b3cb4f65c413b74

URL: https://github.com/llvm/llvm-project/commit/716f7e2d18d03039c13ad90d5b3cb4f65c413b74
DIFF: https://github.com/llvm/llvm-project/commit/716f7e2d18d03039c13ad90d5b3cb4f65c413b74.diff

LOG: [SimplifyCFG] Add tests for switch over cmp intrinsic (NFC)

Added: 
    llvm/test/Transforms/SimplifyCFG/switch-on-cmp.ll

Modified: 
    

Removed: 
    


################################################################################
diff  --git a/llvm/test/Transforms/SimplifyCFG/switch-on-cmp.ll b/llvm/test/Transforms/SimplifyCFG/switch-on-cmp.ll
new file mode 100644
index 00000000000000..1ce18533d156d0
--- /dev/null
+++ b/llvm/test/Transforms/SimplifyCFG/switch-on-cmp.ll
@@ -0,0 +1,384 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
+; RUN: opt < %s -passes=simplifycfg -simplifycfg-require-and-preserve-domtree=1 -S | FileCheck %s
+
+define void @ucmp_gt1(i32 %a, i32 %b) {
+; CHECK-LABEL: define void @ucmp_gt1(
+; CHECK-SAME: i32 [[A:%.*]], i32 [[B:%.*]]) {
+; CHECK-NEXT:    [[RES:%.*]] = call i8 @llvm.ucmp.i8.i32(i32 [[A]], i32 [[B]])
+; CHECK-NEXT:    switch i8 [[RES]], label %[[BB1:.*]] [
+; CHECK-NEXT:      i8 -1, label %[[BB2:.*]]
+; CHECK-NEXT:      i8 0, label %[[BB2]]
+; CHECK-NEXT:    ]
+; CHECK:       [[BB1]]:
+; CHECK-NEXT:    call void @foo()
+; CHECK-NEXT:    br label %[[BB2]]
+; CHECK:       [[BB2]]:
+; CHECK-NEXT:    ret void
+;
+  %res = call i8 @llvm.ucmp.i8.i32(i32 %a, i32 %b)
+  switch i8 %res, label %bb1 [
+  i8 -1, label %bb2
+  i8 0, label %bb2
+  ]
+
+bb1:
+  call void @foo()
+  br label %bb2
+
+bb2:
+  ret void
+}
+
+define void @ucmp_gt2(i32 %a, i32 %b) {
+; CHECK-LABEL: define void @ucmp_gt2(
+; CHECK-SAME: i32 [[A:%.*]], i32 [[B:%.*]]) {
+; CHECK-NEXT:    [[RES:%.*]] = call i8 @llvm.ucmp.i8.i32(i32 [[A]], i32 [[B]])
+; CHECK-NEXT:    switch i8 [[RES]], label %[[BB1:.*]] [
+; CHECK-NEXT:      i8 0, label %[[BB2:.*]]
+; CHECK-NEXT:      i8 -1, label %[[BB2]]
+; CHECK-NEXT:    ]
+; CHECK:       [[BB1]]:
+; CHECK-NEXT:    call void @foo()
+; CHECK-NEXT:    br label %[[BB2]]
+; CHECK:       [[BB2]]:
+; CHECK-NEXT:    ret void
+;
+  %res = call i8 @llvm.ucmp.i8.i32(i32 %a, i32 %b)
+  switch i8 %res, label %bb1 [
+  i8 0, label %bb2
+  i8 -1, label %bb2
+  ]
+
+bb1:
+  call void @foo()
+  br label %bb2
+
+bb2:
+  ret void
+}
+
+define void @ucmp_lt1(i32 %a, i32 %b) {
+; CHECK-LABEL: define void @ucmp_lt1(
+; CHECK-SAME: i32 [[A:%.*]], i32 [[B:%.*]]) {
+; CHECK-NEXT:    [[RES:%.*]] = call i8 @llvm.ucmp.i8.i32(i32 [[A]], i32 [[B]])
+; CHECK-NEXT:    switch i8 [[RES]], label %[[BB2:.*]] [
+; CHECK-NEXT:      i8 1, label %[[BB1:.*]]
+; CHECK-NEXT:      i8 0, label %[[BB1]]
+; CHECK-NEXT:    ]
+; CHECK:       [[BB1]]:
+; CHECK-NEXT:    call void @foo()
+; CHECK-NEXT:    br label %[[BB2]]
+; CHECK:       [[BB2]]:
+; CHECK-NEXT:    ret void
+;
+  %res = call i8 @llvm.ucmp.i8.i32(i32 %a, i32 %b)
+  switch i8 %res, label %bb2 [
+  i8 1, label %bb1
+  i8 0, label %bb1
+  ]
+
+bb1:
+  call void @foo()
+  br label %bb2
+
+bb2:
+  ret void
+}
+
+define void @ucmp_lt2(i32 %a, i32 %b) {
+; CHECK-LABEL: define void @ucmp_lt2(
+; CHECK-SAME: i32 [[A:%.*]], i32 [[B:%.*]]) {
+; CHECK-NEXT:    [[RES:%.*]] = call i8 @llvm.ucmp.i8.i32(i32 [[A]], i32 [[B]])
+; CHECK-NEXT:    switch i8 [[RES]], label %[[BB2:.*]] [
+; CHECK-NEXT:      i8 0, label %[[BB1:.*]]
+; CHECK-NEXT:      i8 1, label %[[BB1]]
+; CHECK-NEXT:    ]
+; CHECK:       [[BB1]]:
+; CHECK-NEXT:    call void @foo()
+; CHECK-NEXT:    br label %[[BB2]]
+; CHECK:       [[BB2]]:
+; CHECK-NEXT:    ret void
+;
+  %res = call i8 @llvm.ucmp.i8.i32(i32 %a, i32 %b)
+  switch i8 %res, label %bb2 [
+  i8 0, label %bb1
+  i8 1, label %bb1
+  ]
+
+bb1:
+  call void @foo()
+  br label %bb2
+
+bb2:
+  ret void
+}
+
+define void @ucmp_eq1(i32 %a, i32 %b) {
+; CHECK-LABEL: define void @ucmp_eq1(
+; CHECK-SAME: i32 [[A:%.*]], i32 [[B:%.*]]) {
+; CHECK-NEXT:    [[RES:%.*]] = call i8 @llvm.ucmp.i8.i32(i32 [[A]], i32 [[B]])
+; CHECK-NEXT:    switch i8 [[RES]], label %[[BB1:.*]] [
+; CHECK-NEXT:      i8 -1, label %[[BB2:.*]]
+; CHECK-NEXT:      i8 1, label %[[BB2]]
+; CHECK-NEXT:    ]
+; CHECK:       [[BB1]]:
+; CHECK-NEXT:    call void @foo()
+; CHECK-NEXT:    br label %[[BB2]]
+; CHECK:       [[BB2]]:
+; CHECK-NEXT:    ret void
+;
+  %res = call i8 @llvm.ucmp.i8.i32(i32 %a, i32 %b)
+  switch i8 %res, label %bb1 [
+  i8 -1, label %bb2
+  i8 1, label %bb2
+  ]
+
+bb1:
+  call void @foo()
+  br label %bb2
+
+bb2:
+  ret void
+}
+
+define void @ucmp_eq2(i32 %a, i32 %b) {
+; CHECK-LABEL: define void @ucmp_eq2(
+; CHECK-SAME: i32 [[A:%.*]], i32 [[B:%.*]]) {
+; CHECK-NEXT:    [[RES:%.*]] = call i8 @llvm.ucmp.i8.i32(i32 [[A]], i32 [[B]])
+; CHECK-NEXT:    switch i8 [[RES]], label %[[BB1:.*]] [
+; CHECK-NEXT:      i8 1, label %[[BB2:.*]]
+; CHECK-NEXT:      i8 -1, label %[[BB2]]
+; CHECK-NEXT:    ]
+; CHECK:       [[BB1]]:
+; CHECK-NEXT:    call void @foo()
+; CHECK-NEXT:    br label %[[BB2]]
+; CHECK:       [[BB2]]:
+; CHECK-NEXT:    ret void
+;
+  %res = call i8 @llvm.ucmp.i8.i32(i32 %a, i32 %b)
+  switch i8 %res, label %bb1 [
+  i8 1, label %bb2
+  i8 -1, label %bb2
+  ]
+
+bb1:
+  call void @foo()
+  br label %bb2
+
+bb2:
+  ret void
+}
+
+define void @scmp_gt1(i32 %a, i32 %b) {
+; CHECK-LABEL: define void @scmp_gt1(
+; CHECK-SAME: i32 [[A:%.*]], i32 [[B:%.*]]) {
+; CHECK-NEXT:    [[RES:%.*]] = call i8 @llvm.scmp.i8.i32(i32 [[A]], i32 [[B]])
+; CHECK-NEXT:    switch i8 [[RES]], label %[[BB1:.*]] [
+; CHECK-NEXT:      i8 -1, label %[[BB2:.*]]
+; CHECK-NEXT:      i8 0, label %[[BB2]]
+; CHECK-NEXT:    ]
+; CHECK:       [[BB1]]:
+; CHECK-NEXT:    call void @foo()
+; CHECK-NEXT:    br label %[[BB2]]
+; CHECK:       [[BB2]]:
+; CHECK-NEXT:    ret void
+;
+  %res = call i8 @llvm.scmp.i8.i32(i32 %a, i32 %b)
+  switch i8 %res, label %bb1 [
+  i8 -1, label %bb2
+  i8 0, label %bb2
+  ]
+
+bb1:
+  call void @foo()
+  br label %bb2
+
+bb2:
+  ret void
+}
+
+define void @scmp_gt2(i32 %a, i32 %b) {
+; CHECK-LABEL: define void @scmp_gt2(
+; CHECK-SAME: i32 [[A:%.*]], i32 [[B:%.*]]) {
+; CHECK-NEXT:    [[RES:%.*]] = call i8 @llvm.scmp.i8.i32(i32 [[A]], i32 [[B]])
+; CHECK-NEXT:    switch i8 [[RES]], label %[[BB1:.*]] [
+; CHECK-NEXT:      i8 0, label %[[BB2:.*]]
+; CHECK-NEXT:      i8 -1, label %[[BB2]]
+; CHECK-NEXT:    ]
+; CHECK:       [[BB1]]:
+; CHECK-NEXT:    call void @foo()
+; CHECK-NEXT:    br label %[[BB2]]
+; CHECK:       [[BB2]]:
+; CHECK-NEXT:    ret void
+;
+  %res = call i8 @llvm.scmp.i8.i32(i32 %a, i32 %b)
+  switch i8 %res, label %bb1 [
+  i8 0, label %bb2
+  i8 -1, label %bb2
+  ]
+
+bb1:
+  call void @foo()
+  br label %bb2
+
+bb2:
+  ret void
+}
+
+define void @ucmp_gt_multiuse(i32 %a, i32 %b) {
+; CHECK-LABEL: define void @ucmp_gt_multiuse(
+; CHECK-SAME: i32 [[A:%.*]], i32 [[B:%.*]]) {
+; CHECK-NEXT:    [[RES:%.*]] = call i8 @llvm.ucmp.i8.i32(i32 [[A]], i32 [[B]])
+; CHECK-NEXT:    call void @use(i8 [[RES]])
+; CHECK-NEXT:    switch i8 [[RES]], label %[[BB1:.*]] [
+; CHECK-NEXT:      i8 -1, label %[[BB2:.*]]
+; CHECK-NEXT:      i8 0, label %[[BB2]]
+; CHECK-NEXT:    ]
+; CHECK:       [[BB1]]:
+; CHECK-NEXT:    call void @foo()
+; CHECK-NEXT:    br label %[[BB2]]
+; CHECK:       [[BB2]]:
+; CHECK-NEXT:    ret void
+;
+  %res = call i8 @llvm.ucmp.i8.i32(i32 %a, i32 %b)
+  call void @use(i8 %res)
+  switch i8 %res, label %bb1 [
+  i8 -1, label %bb2
+  i8 0, label %bb2
+  ]
+
+bb1:
+  call void @foo()
+  br label %bb2
+
+bb2:
+  ret void
+}
+
+define i32 @ucmp_gt_phi(i32 %a, i32 %b) {
+; CHECK-LABEL: define i32 @ucmp_gt_phi(
+; CHECK-SAME: i32 [[A:%.*]], i32 [[B:%.*]]) {
+; CHECK-NEXT:  [[ENTRY:.*]]:
+; CHECK-NEXT:    [[RES:%.*]] = call i8 @llvm.ucmp.i8.i32(i32 [[A]], i32 [[B]])
+; CHECK-NEXT:    switch i8 [[RES]], label %[[BB1:.*]] [
+; CHECK-NEXT:      i8 -1, label %[[BB2:.*]]
+; CHECK-NEXT:      i8 0, label %[[BB2]]
+; CHECK-NEXT:    ]
+; CHECK:       [[BB1]]:
+; CHECK-NEXT:    call void @foo()
+; CHECK-NEXT:    br label %[[BB2]]
+; CHECK:       [[BB2]]:
+; CHECK-NEXT:    [[PHI:%.*]] = phi i32 [ 0, %[[BB1]] ], [ 1, %[[ENTRY]] ], [ 1, %[[ENTRY]] ]
+; CHECK-NEXT:    ret i32 [[PHI]]
+;
+entry:
+  %res = call i8 @llvm.ucmp.i8.i32(i32 %a, i32 %b)
+  switch i8 %res, label %bb1 [
+  i8 -1, label %bb2
+  i8 0, label %bb2
+  ]
+
+bb1:
+  call void @foo()
+  br label %bb2
+
+bb2:
+  %phi = phi i32 [ 0, %bb1 ], [ 1, %entry ], [ 1, %entry ]
+  ret i32 %phi
+}
+
+define void @ucmp_gt_extra_case(i32 %a, i32 %b) {
+; CHECK-LABEL: define void @ucmp_gt_extra_case(
+; CHECK-SAME: i32 [[A:%.*]], i32 [[B:%.*]]) {
+; CHECK-NEXT:    [[RES:%.*]] = call i8 @llvm.ucmp.i8.i32(i32 [[A]], i32 [[B]])
+; CHECK-NEXT:    switch i8 [[RES]], label %[[BB1:.*]] [
+; CHECK-NEXT:      i8 -1, label %[[BB2:.*]]
+; CHECK-NEXT:      i8 0, label %[[BB2]]
+; CHECK-NEXT:      i8 1, label %[[BB2]]
+; CHECK-NEXT:    ]
+; CHECK:       [[BB1]]:
+; CHECK-NEXT:    call void @foo()
+; CHECK-NEXT:    br label %[[BB2]]
+; CHECK:       [[BB2]]:
+; CHECK-NEXT:    ret void
+;
+  %res = call i8 @llvm.ucmp.i8.i32(i32 %a, i32 %b)
+  switch i8 %res, label %bb1 [
+  i8 -1, label %bb2
+  i8 0, label %bb2
+  i8 1, label %bb2
+  ]
+
+bb1:
+  call void @foo()
+  br label %bb2
+
+bb2:
+  ret void
+}
+
+define void @ucmp_gt_wrong_case(i32 %a, i32 %b) {
+; CHECK-LABEL: define void @ucmp_gt_wrong_case(
+; CHECK-SAME: i32 [[A:%.*]], i32 [[B:%.*]]) {
+; CHECK-NEXT:    [[RES:%.*]] = call i8 @llvm.ucmp.i8.i32(i32 [[A]], i32 [[B]])
+; CHECK-NEXT:    switch i8 [[RES]], label %[[BB1:.*]] [
+; CHECK-NEXT:      i8 -2, label %[[BB2:.*]]
+; CHECK-NEXT:      i8 0, label %[[BB2]]
+; CHECK-NEXT:    ]
+; CHECK:       [[BB1]]:
+; CHECK-NEXT:    call void @foo()
+; CHECK-NEXT:    br label %[[BB2]]
+; CHECK:       [[BB2]]:
+; CHECK-NEXT:    ret void
+;
+  %res = call i8 @llvm.ucmp.i8.i32(i32 %a, i32 %b)
+  switch i8 %res, label %bb1 [
+  i8 -2, label %bb2
+  i8 0, label %bb2
+  ]
+
+bb1:
+  call void @foo()
+  br label %bb2
+
+bb2:
+  ret void
+}
+
+define void @ucmp_gt_not_same_succ(i32 %a, i32 %b) {
+; CHECK-LABEL: define void @ucmp_gt_not_same_succ(
+; CHECK-SAME: i32 [[A:%.*]], i32 [[B:%.*]]) {
+; CHECK-NEXT:    [[RES:%.*]] = call i8 @llvm.ucmp.i8.i32(i32 [[A]], i32 [[B]])
+; CHECK-NEXT:    switch i8 [[RES]], label %[[BB1:.*]] [
+; CHECK-NEXT:      i8 -1, label %[[BB2:.*]]
+; CHECK-NEXT:      i8 0, label %[[BB3:.*]]
+; CHECK-NEXT:    ]
+; CHECK:       [[BB1]]:
+; CHECK-NEXT:    call void @foo()
+; CHECK-NEXT:    br label %[[BB2]]
+; CHECK:       [[BB3]]:
+; CHECK-NEXT:    call void @foo()
+; CHECK-NEXT:    br label %[[BB2]]
+; CHECK:       [[BB2]]:
+; CHECK-NEXT:    ret void
+;
+  %res = call i8 @llvm.ucmp.i8.i32(i32 %a, i32 %b)
+  switch i8 %res, label %bb1 [
+  i8 -1, label %bb2
+  i8 0, label %bb3
+  ]
+
+bb1:
+  call void @foo()
+  br label %bb2
+
+bb3:
+  call void @foo()
+  br label %bb2
+
+bb2:
+  ret void
+}
+
+declare void @use(i8)
+declare void @foo()


        


More information about the llvm-commits mailing list