[InstCombine] add test for branch on logical-and; NFC
authorSanjay Patel <spatel@rotateright.com>
Mon, 31 Oct 2022 21:29:03 +0000 (17:29 -0400)
committerSanjay Patel <spatel@rotateright.com>
Mon, 31 Oct 2022 21:39:29 +0000 (17:39 -0400)
More coverage for the change 115d2f69a515cd756fa51 as
suggested in post-commit feedback.

Given that the transform is canonicalized with another
'not' op, we should adjust the use checks and create
that form directly.

llvm/test/Transforms/InstCombine/branch.ll

index f90c8a2..2961dec 100644 (file)
@@ -1,6 +1,8 @@
 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
 ; RUN: opt -passes=instcombine -S < %s | FileCheck %s
 
+declare void @use(i1)
+
 ; Check that we fold the condition of branches of the
 ; form: br <condition> dest1, dest2, where dest1 == dest2.
 define i32 @test(i32 %x) {
@@ -186,3 +188,56 @@ t:
 f:
   ret i32 3
 }
+
+define i32 @logical_and_not_use1(i1 %x, i1 %y) {
+; CHECK-LABEL: @logical_and_not_use1(
+; CHECK-NEXT:  entry:
+; CHECK-NEXT:    [[NOTY:%.*]] = xor i1 [[Y:%.*]], true
+; CHECK-NEXT:    call void @use(i1 [[NOTY]])
+; CHECK-NEXT:    [[NOT_X:%.*]] = xor i1 [[X:%.*]], true
+; CHECK-NEXT:    [[TMP0:%.*]] = select i1 [[NOT_X]], i1 true, i1 [[Y]]
+; CHECK-NEXT:    br i1 [[TMP0]], label [[F:%.*]], label [[T:%.*]]
+; CHECK:       t:
+; CHECK-NEXT:    ret i32 42
+; CHECK:       f:
+; CHECK-NEXT:    ret i32 3
+;
+entry:
+  %noty = xor i1 %y, true
+  call void @use(i1 %noty)
+  %and = select i1 %x, i1 %noty, i1 false
+  br i1 %and, label %t, label %f
+
+t:
+  ret i32 42
+
+f:
+  ret i32 3
+}
+
+; negative test
+
+define i32 @logical_and_not_use2(i1 %x, i1 %y) {
+; CHECK-LABEL: @logical_and_not_use2(
+; CHECK-NEXT:  entry:
+; CHECK-NEXT:    [[NOTY:%.*]] = xor i1 [[Y:%.*]], true
+; CHECK-NEXT:    [[AND:%.*]] = select i1 [[X:%.*]], i1 [[NOTY]], i1 false
+; CHECK-NEXT:    call void @use(i1 [[AND]])
+; CHECK-NEXT:    br i1 [[AND]], label [[T:%.*]], label [[F:%.*]]
+; CHECK:       t:
+; CHECK-NEXT:    ret i32 42
+; CHECK:       f:
+; CHECK-NEXT:    ret i32 3
+;
+entry:
+  %noty = xor i1 %y, true
+  %and = select i1 %x, i1 %noty, i1 false
+  call void @use(i1 %and)
+  br i1 %and, label %t, label %f
+
+t:
+  ret i32 42
+
+f:
+  ret i32 3
+}