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