blob: e6360f8ba64ed22a4feacdadf74f78531685ec40 [file] [log] [blame]
Dinesh Dwivedi99281a02014-06-26 08:57:33 +00001; RUN: opt < %s -instcombine -S | FileCheck %s
Duncan Sandsee3ec6e2010-12-21 13:32:22 +00002
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
Dinesh Dwivedi99281a02014-06-26 08:57:33 +000031; CHECK: %z = or i32 %b, %x
32; CHECK: ret i32 %z
Duncan Sandsa45cfbd2010-12-22 17:15:25 +000033}
34
Duncan Sands9b8e2bd2011-01-18 09:24:58 +000035define i32 @factorize4(i32 %x, i32 %y) {
Stephen Linc1c7a132013-07-14 01:42:54 +000036; CHECK-LABEL: @factorize4(
Dinesh Dwivedi99281a02014-06-26 08:57:33 +000037; ((Y << 1) * X) - (X * Y) -> (X * (Y * 2 - Y)) -> (X * Y)
Duncan Sands9b8e2bd2011-01-18 09:24:58 +000038 %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
Dinesh Dwivedi99281a02014-06-26 08:57:33 +000043; CHECK: %s = mul i32 %y, %x
44; CHECK: ret i32 %s
Duncan Sands9b8e2bd2011-01-18 09:24:58 +000045}
46
47define i32 @factorize5(i32 %x, i32 %y) {
Stephen Linc1c7a132013-07-14 01:42:54 +000048; CHECK-LABEL: @factorize5(
Dinesh Dwivedi99281a02014-06-26 08:57:33 +000049; ((Y * 2) * X) - (X * Y) -> (X * Y)
Duncan Sands9b8e2bd2011-01-18 09:24:58 +000050 %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
Dinesh Dwivedi99281a02014-06-26 08:57:33 +000055; CHECK: %s = mul i32 %y, %x
56; CHECK: ret i32 %s
Duncan Sands9b8e2bd2011-01-18 09:24:58 +000057}
58
Duncan Sandsee3ec6e2010-12-21 13:32:22 +000059define i32 @expand(i32 %x) {
Stephen Linc1c7a132013-07-14 01:42:54 +000060; CHECK-LABEL: @expand(
Duncan Sandsee3ec6e2010-12-21 13:32:22 +000061; ((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
Dinesh Dwivedi99281a02014-06-26 08:57:33 +000066; CHECK: %a = and i32 %x, 1
Duncan Sandsee3ec6e2010-12-21 13:32:22 +000067; CHECK: ret i32 %a
68}