1 ; This test makes sure that these instructions are properly eliminated. 2 ; 3 4 ; RUN: opt < %s -instcombine -S | not grep and 5 6 define i32 @test1(i32 %A) { 7 ; zero result 8 %B = and i32 %A, 0 ; <i32> [#uses=1] 9 ret i32 %B 10 } 11 12 define i32 @test2(i32 %A) { 13 ; noop 14 %B = and i32 %A, -1 ; <i32> [#uses=1] 15 ret i32 %B 16 } 17 18 define i1 @test3(i1 %A) { 19 ; always = false 20 %B = and i1 %A, false ; <i1> [#uses=1] 21 ret i1 %B 22 } 23 24 define i1 @test4(i1 %A) { 25 ; noop 26 %B = and i1 %A, true ; <i1> [#uses=1] 27 ret i1 %B 28 } 29 30 define i32 @test5(i32 %A) { 31 %B = and i32 %A, %A ; <i32> [#uses=1] 32 ret i32 %B 33 } 34 35 define i1 @test6(i1 %A) { 36 %B = and i1 %A, %A ; <i1> [#uses=1] 37 ret i1 %B 38 } 39 40 ; A & ~A == 0 41 define i32 @test7(i32 %A) { 42 %NotA = xor i32 %A, -1 ; <i32> [#uses=1] 43 %B = and i32 %A, %NotA ; <i32> [#uses=1] 44 ret i32 %B 45 } 46 47 ; AND associates 48 define i8 @test8(i8 %A) { 49 %B = and i8 %A, 3 ; <i8> [#uses=1] 50 %C = and i8 %B, 4 ; <i8> [#uses=1] 51 ret i8 %C 52 } 53 54 define i1 @test9(i32 %A) { 55 ; Test of sign bit, convert to setle %A, 0 56 %B = and i32 %A, -2147483648 ; <i32> [#uses=1] 57 %C = icmp ne i32 %B, 0 ; <i1> [#uses=1] 58 ret i1 %C 59 } 60 61 define i1 @test9a(i32 %A) { 62 ; Test of sign bit, convert to setle %A, 0 63 %B = and i32 %A, -2147483648 ; <i32> [#uses=1] 64 %C = icmp ne i32 %B, 0 ; <i1> [#uses=1] 65 ret i1 %C 66 } 67 68 define i32 @test10(i32 %A) { 69 %B = and i32 %A, 12 ; <i32> [#uses=1] 70 %C = xor i32 %B, 15 ; <i32> [#uses=1] 71 ; (X ^ C1) & C2 --> (X & C2) ^ (C1&C2) 72 %D = and i32 %C, 1 ; <i32> [#uses=1] 73 ret i32 %D 74 } 75 76 define i32 @test11(i32 %A, i32* %P) { 77 %B = or i32 %A, 3 ; <i32> [#uses=1] 78 %C = xor i32 %B, 12 ; <i32> [#uses=2] 79 ; additional use of C 80 store i32 %C, i32* %P 81 ; %C = and uint %B, 3 --> 3 82 %D = and i32 %C, 3 ; <i32> [#uses=1] 83 ret i32 %D 84 } 85 86 define i1 @test12(i32 %A, i32 %B) { 87 %C1 = icmp ult i32 %A, %B ; <i1> [#uses=1] 88 %C2 = icmp ule i32 %A, %B ; <i1> [#uses=1] 89 ; (A < B) & (A <= B) === (A < B) 90 %D = and i1 %C1, %C2 ; <i1> [#uses=1] 91 ret i1 %D 92 } 93 94 define i1 @test13(i32 %A, i32 %B) { 95 %C1 = icmp ult i32 %A, %B ; <i1> [#uses=1] 96 %C2 = icmp ugt i32 %A, %B ; <i1> [#uses=1] 97 ; (A < B) & (A > B) === false 98 %D = and i1 %C1, %C2 ; <i1> [#uses=1] 99 ret i1 %D 100 } 101 102 define i1 @test14(i8 %A) { 103 %B = and i8 %A, -128 ; <i8> [#uses=1] 104 %C = icmp ne i8 %B, 0 ; <i1> [#uses=1] 105 ret i1 %C 106 } 107 108 define i8 @test15(i8 %A) { 109 %B = lshr i8 %A, 7 ; <i8> [#uses=1] 110 ; Always equals zero 111 %C = and i8 %B, 2 ; <i8> [#uses=1] 112 ret i8 %C 113 } 114 115 define i8 @test16(i8 %A) { 116 %B = shl i8 %A, 2 ; <i8> [#uses=1] 117 %C = and i8 %B, 3 ; <i8> [#uses=1] 118 ret i8 %C 119 } 120 121 ;; ~(~X & Y) --> (X | ~Y) 122 define i8 @test17(i8 %X, i8 %Y) { 123 %B = xor i8 %X, -1 ; <i8> [#uses=1] 124 %C = and i8 %B, %Y ; <i8> [#uses=1] 125 %D = xor i8 %C, -1 ; <i8> [#uses=1] 126 ret i8 %D 127 } 128 129 define i1 @test18(i32 %A) { 130 %B = and i32 %A, -128 ; <i32> [#uses=1] 131 ;; C >= 128 132 %C = icmp ne i32 %B, 0 ; <i1> [#uses=1] 133 ret i1 %C 134 } 135 136 define i1 @test18a(i8 %A) { 137 %B = and i8 %A, -2 ; <i8> [#uses=1] 138 %C = icmp eq i8 %B, 0 ; <i1> [#uses=1] 139 ret i1 %C 140 } 141 142 define i32 @test19(i32 %A) { 143 %B = shl i32 %A, 3 ; <i32> [#uses=1] 144 ;; Clearing a zero bit 145 %C = and i32 %B, -2 ; <i32> [#uses=1] 146 ret i32 %C 147 } 148 149 define i8 @test20(i8 %A) { 150 %C = lshr i8 %A, 7 ; <i8> [#uses=1] 151 ;; Unneeded 152 %D = and i8 %C, 1 ; <i8> [#uses=1] 153 ret i8 %D 154 } 155 156 define i1 @test22(i32 %A) { 157 %B = icmp eq i32 %A, 1 ; <i1> [#uses=1] 158 %C = icmp sge i32 %A, 3 ; <i1> [#uses=1] 159 ;; false 160 %D = and i1 %B, %C ; <i1> [#uses=1] 161 ret i1 %D 162 } 163 164 define i1 @test23(i32 %A) { 165 %B = icmp sgt i32 %A, 1 ; <i1> [#uses=1] 166 %C = icmp sle i32 %A, 2 ; <i1> [#uses=1] 167 ;; A == 2 168 %D = and i1 %B, %C ; <i1> [#uses=1] 169 ret i1 %D 170 } 171 172 define i1 @test24(i32 %A) { 173 %B = icmp sgt i32 %A, 1 ; <i1> [#uses=1] 174 %C = icmp ne i32 %A, 2 ; <i1> [#uses=1] 175 ;; A > 2 176 %D = and i1 %B, %C ; <i1> [#uses=1] 177 ret i1 %D 178 } 179 180 define i1 @test25(i32 %A) { 181 %B = icmp sge i32 %A, 50 ; <i1> [#uses=1] 182 %C = icmp slt i32 %A, 100 ; <i1> [#uses=1] 183 ;; (A-50) <u 50 184 %D = and i1 %B, %C ; <i1> [#uses=1] 185 ret i1 %D 186 } 187 188 define i1 @test26(i32 %A) { 189 %B = icmp ne i32 %A, 50 ; <i1> [#uses=1] 190 %C = icmp ne i32 %A, 51 ; <i1> [#uses=1] 191 ;; (A-50) > 1 192 %D = and i1 %B, %C ; <i1> [#uses=1] 193 ret i1 %D 194 } 195 196 define i8 @test27(i8 %A) { 197 %B = and i8 %A, 4 ; <i8> [#uses=1] 198 %C = sub i8 %B, 16 ; <i8> [#uses=1] 199 ;; 0xF0 200 %D = and i8 %C, -16 ; <i8> [#uses=1] 201 %E = add i8 %D, 16 ; <i8> [#uses=1] 202 ret i8 %E 203 } 204 205 ;; This is juse a zero extending shr. 206 define i32 @test28(i32 %X) { 207 ;; Sign extend 208 %Y = ashr i32 %X, 24 ; <i32> [#uses=1] 209 ;; Mask out sign bits 210 %Z = and i32 %Y, 255 ; <i32> [#uses=1] 211 ret i32 %Z 212 } 213 214 define i32 @test29(i8 %X) { 215 %Y = zext i8 %X to i32 ; <i32> [#uses=1] 216 ;; Zero extend makes this unneeded. 217 %Z = and i32 %Y, 255 ; <i32> [#uses=1] 218 ret i32 %Z 219 } 220 221 define i32 @test30(i1 %X) { 222 %Y = zext i1 %X to i32 ; <i32> [#uses=1] 223 %Z = and i32 %Y, 1 ; <i32> [#uses=1] 224 ret i32 %Z 225 } 226 227 define i32 @test31(i1 %X) { 228 %Y = zext i1 %X to i32 ; <i32> [#uses=1] 229 %Z = shl i32 %Y, 4 ; <i32> [#uses=1] 230 %A = and i32 %Z, 16 ; <i32> [#uses=1] 231 ret i32 %A 232 } 233 234 define i32 @test32(i32 %In) { 235 %Y = and i32 %In, 16 ; <i32> [#uses=1] 236 %Z = lshr i32 %Y, 2 ; <i32> [#uses=1] 237 %A = and i32 %Z, 1 ; <i32> [#uses=1] 238 ret i32 %A 239 } 240 241 ;; Code corresponding to one-bit bitfield ^1. 242 define i32 @test33(i32 %b) { 243 %tmp.4.mask = and i32 %b, 1 ; <i32> [#uses=1] 244 %tmp.10 = xor i32 %tmp.4.mask, 1 ; <i32> [#uses=1] 245 %tmp.12 = and i32 %b, -2 ; <i32> [#uses=1] 246 %tmp.13 = or i32 %tmp.12, %tmp.10 ; <i32> [#uses=1] 247 ret i32 %tmp.13 248 } 249 250 define i32 @test34(i32 %A, i32 %B) { 251 %tmp.2 = or i32 %B, %A ; <i32> [#uses=1] 252 %tmp.4 = and i32 %tmp.2, %B ; <i32> [#uses=1] 253 ret i32 %tmp.4 254 } 255 256