Home | History | Annotate | Download | only in InstCombine
      1 ; 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 
     12 ; RUN: opt < %s -instcombine -S | \
     13 ; RUN:   not grep "or "
     14 
     15 define 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
     22 }
     23 
     24 define 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
     30 }
     31 
     32 define 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
     38 }
     39 
     40 define 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
     46 }
     47 
     48 define 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
     53 }
     54 
     55 define 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
     60 }
     61 
     62