1; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2; RUN: opt < %s -instcombine -S | FileCheck %s
3
4define i32 @factorize(i32 %x, i32 %y) {
5; CHECK-LABEL: @factorize(
6; CHECK-NEXT:    ret i32 [[X:%.*]]
7;
8; (X | 1) & (X | 2) -> X | (1 & 2) -> X
9  %l = or i32 %x, 1
10  %r = or i32 %x, 2
11  %z = and i32 %l, %r
12  ret i32 %z
13}
14
15define i32 @factorize2(i32 %x) {
16; CHECK-LABEL: @factorize2(
17; CHECK-NEXT:    ret i32 [[X:%.*]]
18;
19; 3*X - 2*X -> X
20  %l = mul i32 3, %x
21  %r = mul i32 2, %x
22  %z = sub i32 %l, %r
23  ret i32 %z
24}
25
26define i32 @factorize3(i32 %x, i32 %a, i32 %b) {
27; CHECK-LABEL: @factorize3(
28; CHECK-NEXT:    [[Z:%.*]] = or i32 [[B:%.*]], [[X:%.*]]
29; CHECK-NEXT:    ret i32 [[Z]]
30;
31; (X | (A|B)) & (X | B) -> X | ((A|B) & B) -> X | B
32  %aORb = or i32 %a, %b
33  %l = or i32 %x, %aORb
34  %r = or i32 %x, %b
35  %z = and i32 %l, %r
36  ret i32 %z
37}
38
39define i32 @factorize4(i32 %x, i32 %y) {
40; CHECK-LABEL: @factorize4(
41; CHECK-NEXT:    [[S:%.*]] = mul i32 [[Y:%.*]], [[X:%.*]]
42; CHECK-NEXT:    ret i32 [[S]]
43;
44; ((Y << 1) * X) - (X * Y) -> (X * (Y * 2 - Y)) -> (X * Y)
45  %sh = shl i32 %y, 1
46  %ml = mul i32 %sh, %x
47  %mr = mul i32 %x, %y
48  %s = sub i32 %ml, %mr
49  ret i32 %s
50}
51
52define i32 @factorize5(i32 %x, i32 %y) {
53; CHECK-LABEL: @factorize5(
54; CHECK-NEXT:    [[S:%.*]] = mul i32 [[Y:%.*]], [[X:%.*]]
55; CHECK-NEXT:    ret i32 [[S]]
56;
57; ((Y * 2) * X) - (X * Y) -> (X * Y)
58  %sh = mul i32 %y, 2
59  %ml = mul i32 %sh, %x
60  %mr = mul i32 %x, %y
61  %s = sub i32 %ml, %mr
62  ret i32 %s
63}
64
65define i32 @expand(i32 %x) {
66; CHECK-LABEL: @expand(
67; CHECK-NEXT:    [[A:%.*]] = and i32 [[X:%.*]], 1
68; CHECK-NEXT:    ret i32 [[A]]
69;
70; ((X & 1) | 2) & 1 -> ((X & 1) & 1) | (2 & 1) -> (X & 1) | 0 -> X & 1
71  %a = and i32 %x, 1
72  %b = or i32 %a, 2
73  %c = and i32 %b, 1
74  ret i32 %c
75}
76