; 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) {
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.