blob: 9ea0a5e10708d0d045957977b794556440422ba6 [file] [log] [blame]
Duncan Sandsee3ec6e2010-12-21 13:32:22 +00001; RUN: opt < %s -instsimplify -S | FileCheck %s
2
3define i32 @factorize(i32 %x, i32 %y) {
Stephen Linc1c7a132013-07-14 01:42:54 +00004; CHECK-LABEL: @factorize(
Duncan Sands07c17132010-12-21 13:39:20 +00005; (X | 1) & (X | 2) -> X | (1 & 2) -> X
Duncan Sandsee3ec6e2010-12-21 13:32:22 +00006 %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
Duncan Sands76befde2010-12-21 15:12:22 +000013define i32 @factorize2(i32 %x) {
Stephen Linc1c7a132013-07-14 01:42:54 +000014; CHECK-LABEL: @factorize2(
Duncan Sands76befde2010-12-21 15:12:22 +000015; 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
Duncan Sandsa45cfbd2010-12-22 17:15:25 +000023define i32 @factorize3(i32 %x, i32 %a, i32 %b) {
Stephen Linc1c7a132013-07-14 01:42:54 +000024; CHECK-LABEL: @factorize3(
Duncan Sandsa45cfbd2010-12-22 17:15:25 +000025; (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: ret i32 %r
32}
33
Duncan Sands9b8e2bd2011-01-18 09:24:58 +000034define i32 @factorize4(i32 %x, i32 %y) {
Stephen Linc1c7a132013-07-14 01:42:54 +000035; CHECK-LABEL: @factorize4(
Duncan Sands9b8e2bd2011-01-18 09:24:58 +000036 %sh = shl i32 %y, 1
37 %ml = mul i32 %sh, %x
38 %mr = mul i32 %x, %y
39 %s = sub i32 %ml, %mr
40 ret i32 %s
41; CHECK: ret i32 %mr
42}
43
44define i32 @factorize5(i32 %x, i32 %y) {
Stephen Linc1c7a132013-07-14 01:42:54 +000045; CHECK-LABEL: @factorize5(
Duncan Sands9b8e2bd2011-01-18 09:24:58 +000046 %sh = mul i32 %y, 2
47 %ml = mul i32 %sh, %x
48 %mr = mul i32 %x, %y
49 %s = sub i32 %ml, %mr
50 ret i32 %s
51; CHECK: ret i32 %mr
52}
53
Duncan Sandsee3ec6e2010-12-21 13:32:22 +000054define i32 @expand(i32 %x) {
Stephen Linc1c7a132013-07-14 01:42:54 +000055; CHECK-LABEL: @expand(
Duncan Sandsee3ec6e2010-12-21 13:32:22 +000056; ((X & 1) | 2) & 1 -> ((X & 1) & 1) | (2 & 1) -> (X & 1) | 0 -> X & 1
57 %a = and i32 %x, 1
58 %b = or i32 %a, 2
59 %c = and i32 %b, 1
60 ret i32 %c
61; CHECK: ret i32 %a
62}