1; This test makes sure that add instructions are properly eliminated. 2 3; RUN: opt < %s -instcombine -S | \ 4; RUN: grep -v OK | not grep add 5 6define i32 @test1(i32 %A) { 7 %B = add i32 %A, 0 ; <i32> [#uses=1] 8 ret i32 %B 9} 10 11define i32 @test2(i32 %A) { 12 %B = add i32 %A, 5 ; <i32> [#uses=1] 13 %C = add i32 %B, -5 ; <i32> [#uses=1] 14 ret i32 %C 15} 16 17define i32 @test3(i32 %A) { 18 %B = add i32 %A, 5 ; <i32> [#uses=1] 19 ;; This should get converted to an add 20 %C = sub i32 %B, 5 ; <i32> [#uses=1] 21 ret i32 %C 22} 23 24define i32 @test4(i32 %A, i32 %B) { 25 %C = sub i32 0, %A ; <i32> [#uses=1] 26 ; D = B + -A = B - A 27 %D = add i32 %B, %C ; <i32> [#uses=1] 28 ret i32 %D 29} 30 31define i32 @test5(i32 %A, i32 %B) { 32 %C = sub i32 0, %A ; <i32> [#uses=1] 33 ; D = -A + B = B - A 34 %D = add i32 %C, %B ; <i32> [#uses=1] 35 ret i32 %D 36} 37 38define i32 @test6(i32 %A) { 39 %B = mul i32 7, %A ; <i32> [#uses=1] 40 ; C = 7*A+A == 8*A == A << 3 41 %C = add i32 %B, %A ; <i32> [#uses=1] 42 ret i32 %C 43} 44 45define i32 @test7(i32 %A) { 46 %B = mul i32 7, %A ; <i32> [#uses=1] 47 ; C = A+7*A == 8*A == A << 3 48 %C = add i32 %A, %B ; <i32> [#uses=1] 49 ret i32 %C 50} 51 52; (A & C1)+(B & C2) -> (A & C1)|(B & C2) iff C1&C2 == 0 53define i32 @test8(i32 %A, i32 %B) { 54 %A1 = and i32 %A, 7 ; <i32> [#uses=1] 55 %B1 = and i32 %B, 128 ; <i32> [#uses=1] 56 %C = add i32 %A1, %B1 ; <i32> [#uses=1] 57 ret i32 %C 58} 59 60define i32 @test9(i32 %A) { 61 %B = shl i32 %A, 4 ; <i32> [#uses=2] 62 ; === shl int %A, 5 63 %C = add i32 %B, %B ; <i32> [#uses=1] 64 ret i32 %C 65} 66 67define i1 @test10(i8 %A, i8 %b) { 68 %B = add i8 %A, %b ; <i8> [#uses=1] 69 ; === A != -b 70 %c = icmp ne i8 %B, 0 ; <i1> [#uses=1] 71 ret i1 %c 72} 73 74define i1 @test11(i8 %A) { 75 %B = add i8 %A, -1 ; <i8> [#uses=1] 76 ; === A != 1 77 %c = icmp ne i8 %B, 0 ; <i1> [#uses=1] 78 ret i1 %c 79} 80 81define i32 @test12(i32 %A, i32 %B) { 82 ; Should be transformed into shl A, 1 83 %C_OK = add i32 %B, %A ; <i32> [#uses=1] 84 br label %X 85 86X: ; preds = %0 87 %D = add i32 %C_OK, %A ; <i32> [#uses=1] 88 ret i32 %D 89} 90 91define i32 @test13(i32 %A, i32 %B, i32 %C) { 92 %D_OK = add i32 %A, %B ; <i32> [#uses=1] 93 %E_OK = add i32 %D_OK, %C ; <i32> [#uses=1] 94 ;; shl A, 1 95 %F = add i32 %E_OK, %A ; <i32> [#uses=1] 96 ret i32 %F 97} 98 99define i32 @test14(i32 %offset, i32 %difference) { 100 %tmp.2 = and i32 %difference, 3 ; <i32> [#uses=1] 101 %tmp.3_OK = add i32 %tmp.2, %offset ; <i32> [#uses=1] 102 %tmp.5.mask = and i32 %difference, -4 ; <i32> [#uses=1] 103 ; == add %offset, %difference 104 %tmp.8 = add i32 %tmp.3_OK, %tmp.5.mask ; <i32> [#uses=1] 105 ret i32 %tmp.8 106} 107 108define i8 @test15(i8 %A) { 109 ; Does not effect result 110 %B = add i8 %A, -64 ; <i8> [#uses=1] 111 ; Only one bit set 112 %C = and i8 %B, 16 ; <i8> [#uses=1] 113 ret i8 %C 114} 115 116define i8 @test16(i8 %A) { 117 ; Turn this into a XOR 118 %B = add i8 %A, 16 ; <i8> [#uses=1] 119 ; Only one bit set 120 %C = and i8 %B, 16 ; <i8> [#uses=1] 121 ret i8 %C 122} 123 124define i32 @test17(i32 %A) { 125 %B = xor i32 %A, -1 ; <i32> [#uses=1] 126 ; == sub int 0, %A 127 %C = add i32 %B, 1 ; <i32> [#uses=1] 128 ret i32 %C 129} 130 131define i8 @test18(i8 %A) { 132 %B = xor i8 %A, -1 ; <i8> [#uses=1] 133 ; == sub ubyte 16, %A 134 %C = add i8 %B, 17 ; <i8> [#uses=1] 135 ret i8 %C 136} 137 138define i32 @test19(i1 %C) { 139 %A = select i1 %C, i32 1000, i32 10 ; <i32> [#uses=1] 140 %V = add i32 %A, 123 ; <i32> [#uses=1] 141 ret i32 %V 142} 143 144define i32 @test20(i32 %x) { 145 %tmp.2 = xor i32 %x, -2147483648 ; <i32> [#uses=1] 146 ;; Add of sign bit -> xor of sign bit. 147 %tmp.4 = add i32 %tmp.2, -2147483648 ; <i32> [#uses=1] 148 ret i32 %tmp.4 149} 150 151define i1 @test21(i32 %x) { 152 %t = add i32 %x, 4 ; <i32> [#uses=1] 153 %y = icmp eq i32 %t, 123 ; <i1> [#uses=1] 154 ret i1 %y 155} 156 157define i32 @test22(i32 %V) { 158 %V2 = add i32 %V, 10 ; <i32> [#uses=1] 159 switch i32 %V2, label %Default [ 160 i32 20, label %Lab1 161 i32 30, label %Lab2 162 ] 163 164Default: ; preds = %0 165 ret i32 123 166 167Lab1: ; preds = %0 168 ret i32 12312 169 170Lab2: ; preds = %0 171 ret i32 1231231 172} 173 174define i32 @test23(i1 %C, i32 %a) { 175entry: 176 br i1 %C, label %endif, label %else 177 178else: ; preds = %entry 179 br label %endif 180 181endif: ; preds = %else, %entry 182 %b.0 = phi i32 [ 0, %entry ], [ 1, %else ] ; <i32> [#uses=1] 183 %tmp.4 = add i32 %b.0, 1 ; <i32> [#uses=1] 184 ret i32 %tmp.4 185} 186 187define i32 @test24(i32 %A) { 188 %B = add i32 %A, 1 ; <i32> [#uses=1] 189 %C = shl i32 %B, 1 ; <i32> [#uses=1] 190 %D = sub i32 %C, 2 ; <i32> [#uses=1] 191 ret i32 %D 192} 193 194define i64 @test25(i64 %Y) { 195 %tmp.4 = shl i64 %Y, 2 ; <i64> [#uses=1] 196 %tmp.12 = shl i64 %Y, 2 ; <i64> [#uses=1] 197 %tmp.8 = add i64 %tmp.4, %tmp.12 ; <i64> [#uses=1] 198 ret i64 %tmp.8 199} 200 201define i32 @test26(i32 %A, i32 %B) { 202 %C = add i32 %A, %B ; <i32> [#uses=1] 203 %D = sub i32 %C, %B ; <i32> [#uses=1] 204 ret i32 %D 205} 206 207define i32 @test27(i1 %C, i32 %X, i32 %Y) { 208 %A = add i32 %X, %Y ; <i32> [#uses=1] 209 %B = add i32 %Y, 123 ; <i32> [#uses=1] 210 ;; Fold add through select. 211 %C.upgrd.1 = select i1 %C, i32 %A, i32 %B ; <i32> [#uses=1] 212 %D = sub i32 %C.upgrd.1, %Y ; <i32> [#uses=1] 213 ret i32 %D 214} 215 216define i32 @test28(i32 %X) { 217 %Y = add i32 %X, 1234 ; <i32> [#uses=1] 218 %Z = sub i32 42, %Y ; <i32> [#uses=1] 219 ret i32 %Z 220} 221 222define i32 @test29(i32 %X, i32 %x) { 223 %tmp.2 = sub i32 %X, %x ; <i32> [#uses=2] 224 %tmp.2.mask = and i32 %tmp.2, 63 ; <i32> [#uses=1] 225 %tmp.6 = add i32 %tmp.2.mask, %x ; <i32> [#uses=1] 226 %tmp.7 = and i32 %tmp.6, 63 ; <i32> [#uses=1] 227 %tmp.9 = and i32 %tmp.2, -64 ; <i32> [#uses=1] 228 %tmp.10 = or i32 %tmp.7, %tmp.9 ; <i32> [#uses=1] 229 ret i32 %tmp.10 230} 231 232define i64 @test30(i64 %x) { 233 %tmp.2 = xor i64 %x, -9223372036854775808 ; <i64> [#uses=1] 234 ;; Add of sign bit -> xor of sign bit. 235 %tmp.4 = add i64 %tmp.2, -9223372036854775808 ; <i64> [#uses=1] 236 ret i64 %tmp.4 237} 238 239define i32 @test31(i32 %A) { 240 %B = add i32 %A, 4 ; <i32> [#uses=1] 241 %C = mul i32 %B, 5 ; <i32> [#uses=1] 242 %D = sub i32 %C, 20 ; <i32> [#uses=1] 243 ret i32 %D 244} 245 246define i32 @test32(i32 %A) { 247 %B = add i32 %A, 4 ; <i32> [#uses=1] 248 %C = shl i32 %B, 2 ; <i32> [#uses=1] 249 %D = sub i32 %C, 16 ; <i32> [#uses=1] 250 ret i32 %D 251} 252 253define i8 @test33(i8 %A) { 254 %B = and i8 %A, -2 ; <i8> [#uses=1] 255 %C = add i8 %B, 1 ; <i8> [#uses=1] 256 ret i8 %C 257} 258 259define i8 @test34(i8 %A) { 260 %B = add i8 %A, 64 ; <i8> [#uses=1] 261 %C = and i8 %B, 12 ; <i8> [#uses=1] 262 ret i8 %C 263} 264 265define i32 @test35(i32 %a) { 266 %tmpnot = xor i32 %a, -1 ; <i32> [#uses=1] 267 %tmp2 = add i32 %tmpnot, %a ; <i32> [#uses=1] 268 ret i32 %tmp2 269} 270 271define i32 @test36(i32 %a) { 272 %x = and i32 %a, -2 273 %y = and i32 %a, -126 274 %z = add i32 %x, %y 275 %q = and i32 %z, 1 ; always zero 276 ret i32 %q 277} 278 279define i1 @test37(i32 %a, i32 %b) nounwind readnone { 280 %add = add i32 %a, %b 281 %cmp = icmp eq i32 %add, %a 282 ret i1 %cmp 283} 284 285define i1 @test38(i32 %a, i32 %b) nounwind readnone { 286 %add = add i32 %a, %b 287 %cmp = icmp eq i32 %add, %b 288 ret i1 %cmp 289} 290 291define i1 @test39(i32 %a, i32 %b) nounwind readnone { 292 %add = add i32 %b, %a 293 %cmp = icmp eq i32 %add, %a 294 ret i1 %cmp 295} 296 297define i1 @test40(i32 %a, i32 %b) nounwind readnone { 298 %add = add i32 %b, %a 299 %cmp = icmp eq i32 %add, %b 300 ret i1 %cmp 301} 302