[llvm] 7ca168d - [SimplifyCFG] Add switch-to-select test with two equal cases (NFC)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Sun Apr 4 08:15:12 PDT 2021


Author: Nikita Popov
Date: 2021-04-04T17:14:59+02:00
New Revision: 7ca168dd5adabcc01e957fb8486ddf2e22dd37d7

URL: https://github.com/llvm/llvm-project/commit/7ca168dd5adabcc01e957fb8486ddf2e22dd37d7
DIFF: https://github.com/llvm/llvm-project/commit/7ca168dd5adabcc01e957fb8486ddf2e22dd37d7.diff

LOG: [SimplifyCFG] Add switch-to-select test with two equal cases (NFC)

We handle the case where we have two cases and a default all having
different values, but not the case where two cases happen to have
the same one.

The PhaseOrdering test is a particularly bad example where this
showed up.

Added: 
    llvm/test/Transforms/PhaseOrdering/partialord-ule.ll

Modified: 
    llvm/test/Transforms/SimplifyCFG/switch-to-select-two-case.ll

Removed: 
    


################################################################################
diff  --git a/llvm/test/Transforms/PhaseOrdering/partialord-ule.ll b/llvm/test/Transforms/PhaseOrdering/partialord-ule.ll
new file mode 100644
index 000000000000..5ad1905de7b0
--- /dev/null
+++ b/llvm/test/Transforms/PhaseOrdering/partialord-ule.ll
@@ -0,0 +1,43 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
+; RUN: opt -O1 -S < %s | FileCheck %s
+
+; This is a very convoluted way to write an icmp ule, which comes about by
+; matching against a three-way comparison result.
+define i1 @ule(i32 %a, i32 %b) {
+; CHECK-LABEL: @ule(
+; CHECK-NEXT:  start:
+; CHECK-NEXT:    [[CMP1:%.*]] = icmp eq i32 [[A:%.*]], [[B:%.*]]
+; CHECK-NEXT:    [[CMP2:%.*]] = icmp ult i32 [[A]], [[B]]
+; CHECK-NEXT:    [[CMP3:%.*]] = icmp ne i32 [[A]], [[B]]
+; CHECK-NEXT:    [[ZEXT:%.*]] = zext i1 [[CMP3]] to i64
+; CHECK-NEXT:    [[SEL1:%.*]] = select i1 [[CMP2]], i64 -1, i64 [[ZEXT]]
+; CHECK-NEXT:    [[SEL2:%.*]] = select i1 [[CMP1]], i64 0, i64 [[SEL1]]
+; CHECK-NEXT:    switch i64 [[SEL2]], label [[EXIT:%.*]] [
+; CHECK-NEXT:    i64 -1, label [[BB:%.*]]
+; CHECK-NEXT:    i64 0, label [[BB]]
+; CHECK-NEXT:    ]
+; CHECK:       bb:
+; CHECK-NEXT:    br label [[EXIT]]
+; CHECK:       exit:
+; CHECK-NEXT:    [[RES:%.*]] = phi i1 [ true, [[BB]] ], [ false, [[START:%.*]] ]
+; CHECK-NEXT:    ret i1 [[RES]]
+;
+start:
+  %cmp1 = icmp eq i32 %a, %b
+  %cmp2 = icmp ult i32 %a, %b
+  %cmp3 = icmp ne i32 %a, %b
+  %zext = zext i1 %cmp3 to i64
+  %sel1 = select i1 %cmp2, i64 -1, i64 %zext
+  %sel2 = select i1 %cmp1, i64 0, i64 %sel1
+  switch i64 %sel2, label %exit [
+  i64 -1, label %bb
+  i64 0, label %bb
+  ]
+
+bb:
+  br label %exit
+
+exit:
+  %res = phi i1 [ true, %bb ], [ false, %start ]
+  ret i1 %res
+}

diff  --git a/llvm/test/Transforms/SimplifyCFG/switch-to-select-two-case.ll b/llvm/test/Transforms/SimplifyCFG/switch-to-select-two-case.ll
index bd3d5dff94d3..5ac7284a7b5e 100644
--- a/llvm/test/Transforms/SimplifyCFG/switch-to-select-two-case.ll
+++ b/llvm/test/Transforms/SimplifyCFG/switch-to-select-two-case.ll
@@ -1,3 +1,4 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
 ; RUN: opt < %s -simplifycfg -simplifycfg-require-and-preserve-domtree=1 -S | FileCheck %s
 
 ; int foo1_with_default(int a) {
@@ -13,9 +14,9 @@
 define i32 @foo1_with_default(i32 %a) {
 ; CHECK-LABEL: @foo1_with_default(
 ; CHECK-NEXT:  entry:
-; CHECK-NEXT:    [[SWITCH_SELECTCMP:%.*]] = icmp eq i32 %a, 20
-; CHECK-NEXT:    [[SWITCH_SELECT:%.*]] = select i1 [[SWITCH_SELECTCMP:%.*]], i32 2, i32 4
-; CHECK-NEXT:    [[SWITCH_SELECTCMP1:%.*]] = icmp eq i32 %a, 10
+; CHECK-NEXT:    [[SWITCH_SELECTCMP:%.*]] = icmp eq i32 [[A:%.*]], 20
+; CHECK-NEXT:    [[SWITCH_SELECT:%.*]] = select i1 [[SWITCH_SELECTCMP]], i32 2, i32 4
+; CHECK-NEXT:    [[SWITCH_SELECTCMP1:%.*]] = icmp eq i32 [[A]], 10
 ; CHECK-NEXT:    [[SWITCH_SELECT2:%.*]] = select i1 [[SWITCH_SELECTCMP1]], i32 10, i32 [[SWITCH_SELECT]]
 ; CHECK-NEXT:    ret i32 [[SWITCH_SELECT2]]
 ;
@@ -39,3 +40,33 @@ return:
   ret i32 %retval.0
 }
 
+; Same as above, but both cases have the same value.
+define i32 @same_value(i32 %a) {
+; CHECK-LABEL: @same_value(
+; CHECK-NEXT:  entry:
+; CHECK-NEXT:    switch i32 [[A:%.*]], label [[SW_EPILOG:%.*]] [
+; CHECK-NEXT:    i32 10, label [[RETURN:%.*]]
+; CHECK-NEXT:    i32 20, label [[RETURN]]
+; CHECK-NEXT:    ]
+; CHECK:       sw.epilog:
+; CHECK-NEXT:    br label [[RETURN]]
+; CHECK:       return:
+; CHECK-NEXT:    [[RETVAL_0:%.*]] = phi i32 [ 4, [[SW_EPILOG]] ], [ 10, [[ENTRY:%.*]] ], [ 10, [[ENTRY]] ]
+; CHECK-NEXT:    ret i32 [[RETVAL_0]]
+;
+entry:
+  switch i32 %a, label %sw.epilog [
+  i32 10, label %sw.bb
+  i32 20, label %sw.bb
+  ]
+
+sw.bb:
+  br label %return
+
+sw.epilog:
+  br label %return
+
+return:
+  %retval.0 = phi i32 [ 4, %sw.epilog ], [ 10, %sw.bb ]
+  ret i32 %retval.0
+}


        


More information about the llvm-commits mailing list