blob: 216cd46775a5ff61e2a7485d29927f4b9f3554e1 [file] [log] [blame]
Chris Lattner584e3832005-01-01 16:14:18 +00001; If we have an 'and' of the result of an 'or', and one of the 'or' operands
2; cannot have contributed any of the resultant bits, delete the or. This
3; occurs for very common C/C++ code like this:
4;
5; struct foo { int A : 16; int B : 16; };
6; void test(struct foo *F, int X, int Y) {
7; F->A = X; F->B = Y;
8; }
9;
10; Which corresponds to test1.
11
Dan Gohman18800922009-09-11 18:01:28 +000012; RUN: opt < %s -instcombine -S | \
Reid Spencer91948d42007-04-14 20:13:02 +000013; RUN: not grep {or }
Chris Lattner584e3832005-01-01 16:14:18 +000014
Tanya Lattneraa6f5c92008-03-09 08:16:40 +000015define i32 @test1(i32 %X, i32 %Y) {
16 %A = and i32 %X, 7 ; <i32> [#uses=1]
17 %B = and i32 %Y, 8 ; <i32> [#uses=1]
18 %C = or i32 %A, %B ; <i32> [#uses=1]
19 ;; This cannot include any bits from %Y!
20 %D = and i32 %C, 7 ; <i32> [#uses=1]
21 ret i32 %D
Chris Lattner584e3832005-01-01 16:14:18 +000022}
23
Tanya Lattneraa6f5c92008-03-09 08:16:40 +000024define i32 @test2(i32 %X, i8 %Y) {
25 %B = zext i8 %Y to i32 ; <i32> [#uses=1]
26 %C = or i32 %X, %B ; <i32> [#uses=1]
27 ;; This cannot include any bits from %Y!
28 %D = and i32 %C, 65536 ; <i32> [#uses=1]
29 ret i32 %D
Chris Lattner584e3832005-01-01 16:14:18 +000030}
31
Tanya Lattneraa6f5c92008-03-09 08:16:40 +000032define i32 @test3(i32 %X, i32 %Y) {
33 %B = shl i32 %Y, 1 ; <i32> [#uses=1]
34 %C = or i32 %X, %B ; <i32> [#uses=1]
35 ;; This cannot include any bits from %Y!
36 %D = and i32 %C, 1 ; <i32> [#uses=1]
37 ret i32 %D
Chris Lattner584e3832005-01-01 16:14:18 +000038}
39
Tanya Lattneraa6f5c92008-03-09 08:16:40 +000040define i32 @test4(i32 %X, i32 %Y) {
41 %B = lshr i32 %Y, 31 ; <i32> [#uses=1]
42 %C = or i32 %X, %B ; <i32> [#uses=1]
43 ;; This cannot include any bits from %Y!
44 %D = and i32 %C, 2 ; <i32> [#uses=1]
45 ret i32 %D
Chris Lattner584e3832005-01-01 16:14:18 +000046}
47
Tanya Lattneraa6f5c92008-03-09 08:16:40 +000048define i32 @or_test1(i32 %X, i32 %Y) {
49 %A = and i32 %X, 1 ; <i32> [#uses=1]
50 ;; This cannot include any bits from X!
51 %B = or i32 %A, 1 ; <i32> [#uses=1]
52 ret i32 %B
Chris Lattner584e3832005-01-01 16:14:18 +000053}
54
Tanya Lattneraa6f5c92008-03-09 08:16:40 +000055define i8 @or_test2(i8 %X, i8 %Y) {
56 %A = shl i8 %X, 7 ; <i8> [#uses=1]
57 ;; This cannot include any bits from X!
58 %B = or i8 %A, -128 ; <i8> [#uses=1]
59 ret i8 %B
Chris Lattner584e3832005-01-01 16:14:18 +000060}
61