blob: d20abd68c2008ad35c34fc3503ab382dcccd8481 [file] [log] [blame]
Duncan Sands3421d902010-12-21 13:32:22 +00001; RUN: opt < %s -instsimplify -S | FileCheck %s
2
3define i32 @factorize(i32 %x, i32 %y) {
4; CHECK: @factorize
Duncan Sands9bd2c2e2010-12-21 13:39:20 +00005; (X | 1) & (X | 2) -> X | (1 & 2) -> X
Duncan Sands3421d902010-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 Sands025c98b2010-12-21 15:12:22 +000013define i32 @factorize2(i32 %x) {
14; CHECK: @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
Duncan Sands1cd05bb2010-12-22 17:15:25 +000023define i32 @factorize3(i32 %x, i32 %a, i32 %b) {
24; CHECK: @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: ret i32 %r
32}
33
Duncan Sandsfe02c692011-01-18 09:24:58 +000034define i32 @factorize4(i32 %x, i32 %y) {
35; CHECK: @factorize4
36 %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) {
45; CHECK: @factorize5
46 %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 Sands3421d902010-12-21 13:32:22 +000054define i32 @expand(i32 %x) {
55; CHECK: @expand
56; ((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}