[llvm] 701923a - [InstCombine] add tests for bitwise logic folds; NFC

Sanjay Patel via llvm-commits llvm-commits at lists.llvm.org
Mon Nov 1 05:48:17 PDT 2021


Author: Sanjay Patel
Date: 2021-11-01T08:26:41-04:00
New Revision: 701923a60fdb7b321cfd8e6ff5d5d9195daaec0e

URL: https://github.com/llvm/llvm-project/commit/701923a60fdb7b321cfd8e6ff5d5d9195daaec0e
DIFF: https://github.com/llvm/llvm-project/commit/701923a60fdb7b321cfd8e6ff5d5d9195daaec0e.diff

LOG: [InstCombine] add tests for bitwise logic folds; NFC

The extra uses are needed to prevent intermediate folds.
Without that, there would be no coverage currently.
The vector tests show an artificial limitation in the code.

Added: 
    

Modified: 
    llvm/test/Transforms/InstCombine/and-or.ll
    llvm/test/Transforms/InstCombine/or.ll

Removed: 
    


################################################################################
diff  --git a/llvm/test/Transforms/InstCombine/and-or.ll b/llvm/test/Transforms/InstCombine/and-or.ll
index 82d60ce89539e..7996fd3c63f61 100644
--- a/llvm/test/Transforms/InstCombine/and-or.ll
+++ b/llvm/test/Transforms/InstCombine/and-or.ll
@@ -1,6 +1,9 @@
 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
 ; RUN: opt < %s -instcombine -S | FileCheck %s
 
+declare void @use(i8)
+declare void @use_vec(<2 x i8>)
+
 ; ((b | a) & C1) | (b & C2) -> (a & C1) | b iff C1 == ~C2
 
 define i32 @or_and_not_constant_commute0(i32 %a, i32 %b) {
@@ -68,6 +71,141 @@ define <2 x i7> @or_and_not_constant_commute0_splat(<2 x i7> %a, <2 x i7> %b) {
   ret <2 x i7> %t3
 }
 
+; ((x | N) & C1) | (x & C2) --> (x | N) & (C1 | C2)
+; iff (C1 & C2) == 0 and (N & ~C1) == 0
+
+define i8 @or_and_or_commute0(i8 %x) {
+; CHECK-LABEL: @or_and_or_commute0(
+; CHECK-NEXT:    [[XN:%.*]] = or i8 [[X:%.*]], 16
+; CHECK-NEXT:    call void @use(i8 [[XN]])
+; CHECK-NEXT:    [[X1:%.*]] = and i8 [[XN]], 59
+; CHECK-NEXT:    call void @use(i8 [[X1]])
+; CHECK-NEXT:    [[X2:%.*]] = and i8 [[X]], 64
+; CHECK-NEXT:    call void @use(i8 [[X2]])
+; CHECK-NEXT:    [[R:%.*]] = and i8 [[XN]], 123
+; CHECK-NEXT:    ret i8 [[R]]
+;
+  %xn = or i8 %x, 16   ; 0001_0000
+  call void @use(i8 %xn)
+  %x1 = and i8 %xn, 59 ; 0011_1011
+  call void @use(i8 %x1)
+  %x2 = and i8 %x, 64  ; 0100_0000
+  call void @use(i8 %x2)
+  %r = or i8 %x1, %x2
+  ret i8 %r
+}
+
+define i8 @or_and_or_commute1(i8 %x) {
+; CHECK-LABEL: @or_and_or_commute1(
+; CHECK-NEXT:    [[XN:%.*]] = or i8 [[X:%.*]], 16
+; CHECK-NEXT:    call void @use(i8 [[XN]])
+; CHECK-NEXT:    [[X1:%.*]] = and i8 [[XN]], 59
+; CHECK-NEXT:    call void @use(i8 [[X1]])
+; CHECK-NEXT:    [[X2:%.*]] = and i8 [[X]], 64
+; CHECK-NEXT:    call void @use(i8 [[X2]])
+; CHECK-NEXT:    [[R:%.*]] = and i8 [[XN]], 123
+; CHECK-NEXT:    ret i8 [[R]]
+;
+  %xn = or i8 %x, 16   ; 0001_0000
+  call void @use(i8 %xn)
+  %x1 = and i8 %xn, 59 ; 0011_1011
+  call void @use(i8 %x1)
+  %x2 = and i8 %x, 64  ; 0100_0000
+  call void @use(i8 %x2)
+  %r = or i8 %x2, %x1
+  ret i8 %r
+}
+
+define <2 x i8> @or_and_or_commute1_splat(<2 x i8> %x) {
+; CHECK-LABEL: @or_and_or_commute1_splat(
+; CHECK-NEXT:    [[XN:%.*]] = or <2 x i8> [[X:%.*]], <i8 16, i8 16>
+; CHECK-NEXT:    call void @use_vec(<2 x i8> [[XN]])
+; CHECK-NEXT:    [[X1:%.*]] = and <2 x i8> [[XN]], <i8 59, i8 59>
+; CHECK-NEXT:    call void @use_vec(<2 x i8> [[X1]])
+; CHECK-NEXT:    [[X2:%.*]] = and <2 x i8> [[X]], <i8 64, i8 64>
+; CHECK-NEXT:    call void @use_vec(<2 x i8> [[X2]])
+; CHECK-NEXT:    [[R:%.*]] = or <2 x i8> [[X2]], [[X1]]
+; CHECK-NEXT:    ret <2 x i8> [[R]]
+;
+  %xn = or <2 x i8> %x, <i8 16, i8 16>
+  call void @use_vec(<2 x i8> %xn)
+  %x1 = and <2 x i8> %xn, <i8 59, i8 59>
+  call void @use_vec(<2 x i8> %x1)
+  %x2 = and <2 x i8> %x, <i8 64, i8 64>
+  call void @use_vec(<2 x i8> %x2)
+  %r = or <2 x i8> %x2, %x1
+  ret <2 x i8> %r
+}
+
+define i8 @or_and_or_commute2(i8 %x, i8 %y) {
+; CHECK-LABEL: @or_and_or_commute2(
+; CHECK-NEXT:    [[N:%.*]] = lshr i8 [[Y:%.*]], 6
+; CHECK-NEXT:    [[XN:%.*]] = or i8 [[N]], [[X:%.*]]
+; CHECK-NEXT:    call void @use(i8 [[XN]])
+; CHECK-NEXT:    [[X1:%.*]] = and i8 [[XN]], -69
+; CHECK-NEXT:    call void @use(i8 [[X1]])
+; CHECK-NEXT:    [[X2:%.*]] = and i8 [[X]], 64
+; CHECK-NEXT:    call void @use(i8 [[X2]])
+; CHECK-NEXT:    [[R:%.*]] = and i8 [[XN]], -5
+; CHECK-NEXT:    ret i8 [[R]]
+;
+  %n = lshr i8 %y, 6
+  %xn = or i8 %n, %x
+  call void @use(i8 %xn)
+  %x1 = and i8 %xn, 187
+  call void @use(i8 %x1)
+  %x2 = and i8 %x, 64
+  call void @use(i8 %x2)
+  %r = or i8 %x1, %x2
+  ret i8 %r
+}
+
+define <2 x i8> @or_and_or_commute2_splat(<2 x i8> %x, <2 x i8> %y) {
+; CHECK-LABEL: @or_and_or_commute2_splat(
+; CHECK-NEXT:    [[N:%.*]] = lshr <2 x i8> [[Y:%.*]], <i8 6, i8 6>
+; CHECK-NEXT:    [[XN:%.*]] = or <2 x i8> [[N]], [[X:%.*]]
+; CHECK-NEXT:    call void @use_vec(<2 x i8> [[XN]])
+; CHECK-NEXT:    [[X1:%.*]] = and <2 x i8> [[XN]], <i8 -69, i8 -69>
+; CHECK-NEXT:    call void @use_vec(<2 x i8> [[X1]])
+; CHECK-NEXT:    [[X2:%.*]] = and <2 x i8> [[X]], <i8 64, i8 64>
+; CHECK-NEXT:    call void @use_vec(<2 x i8> [[X2]])
+; CHECK-NEXT:    [[R:%.*]] = or <2 x i8> [[X1]], [[X2]]
+; CHECK-NEXT:    ret <2 x i8> [[R]]
+;
+  %n = lshr <2 x i8> %y, <i8 6, i8 6>
+  %xn = or <2 x i8> %n, %x
+  call void @use_vec(<2 x i8> %xn)
+  %x1 = and <2 x i8> %xn, <i8 187, i8 187>
+  call void @use_vec(<2 x i8> %x1)
+  %x2 = and <2 x i8> %x, <i8 64, i8 64>
+  call void @use_vec(<2 x i8> %x2)
+  %r = or <2 x i8> %x1, %x2
+  ret <2 x i8> %r
+}
+
+define i8 @or_and_or_commute3(i8 %x, i8 %y) {
+; CHECK-LABEL: @or_and_or_commute3(
+; CHECK-NEXT:    [[N:%.*]] = lshr i8 [[Y:%.*]], 6
+; CHECK-NEXT:    [[XN:%.*]] = or i8 [[N]], [[X:%.*]]
+; CHECK-NEXT:    call void @use(i8 [[XN]])
+; CHECK-NEXT:    [[X1:%.*]] = and i8 [[XN]], -69
+; CHECK-NEXT:    call void @use(i8 [[X1]])
+; CHECK-NEXT:    [[X2:%.*]] = and i8 [[X]], 64
+; CHECK-NEXT:    call void @use(i8 [[X2]])
+; CHECK-NEXT:    [[R:%.*]] = and i8 [[XN]], -5
+; CHECK-NEXT:    ret i8 [[R]]
+;
+  %n = lshr i8 %y, 6
+  %xn = or i8 %n, %x
+  call void @use(i8 %xn)
+  %x1 = and i8 %xn, 187
+  call void @use(i8 %x1)
+  %x2 = and i8 %x, 64
+  call void @use(i8 %x2)
+  %r = or i8 %x2, %x1
+  ret i8 %r
+}
+
 ; Check variants of:
 ; and ({x}or X, Y), C --> {x}or X, (and Y, C)
 ; ...in the following 5 tests.

diff  --git a/llvm/test/Transforms/InstCombine/or.ll b/llvm/test/Transforms/InstCombine/or.ll
index fcc6a0fbac6fd..ef76fb0d58b44 100644
--- a/llvm/test/Transforms/InstCombine/or.ll
+++ b/llvm/test/Transforms/InstCombine/or.ll
@@ -341,9 +341,9 @@ define i32 @test30(i32 %A) {
 ; CHECK-NEXT:    [[E:%.*]] = or i32 [[D]], 32962
 ; CHECK-NEXT:    ret i32 [[E]]
 ;
-  %B = or i32 %A, 32962
-  %C = and i32 %A, -65536
-  %D = and i32 %B, 40186
+  %B = or i32 %A, 32962   ; 0b1000_0000_1100_0010
+  %C = and i32 %A, -65536 ; 0xffff0000
+  %D = and i32 %B, 40186  ; 0b1001_1100_1111_1010
   %E = or i32 %D, %C
   ret i32 %E
 }


        


More information about the llvm-commits mailing list