1 target datalayout = "e-p:64:64:64-p1:16:16:16-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128" 2 3 ; Optimize subtracts. 4 ; 5 ; RUN: opt < %s -instcombine -S | FileCheck %s 6 7 define i32 @test1(i32 %A) { 8 %B = sub i32 %A, %A 9 ret i32 %B 10 ; CHECK-LABEL: @test1( 11 ; CHECK: ret i32 0 12 } 13 14 define i32 @test2(i32 %A) { 15 %B = sub i32 %A, 0 16 ret i32 %B 17 ; CHECK-LABEL: @test2( 18 ; CHECK: ret i32 %A 19 } 20 21 define i32 @test3(i32 %A) { 22 %B = sub i32 0, %A 23 %C = sub i32 0, %B 24 ret i32 %C 25 ; CHECK-LABEL: @test3( 26 ; CHECK: ret i32 %A 27 } 28 29 define i32 @test4(i32 %A, i32 %x) { 30 %B = sub i32 0, %A 31 %C = sub i32 %x, %B 32 ret i32 %C 33 ; CHECK-LABEL: @test4( 34 ; CHECK: %C = add i32 %x, %A 35 ; CHECK: ret i32 %C 36 } 37 38 define i32 @test5(i32 %A, i32 %B, i32 %C) { 39 %D = sub i32 %B, %C 40 %E = sub i32 %A, %D 41 ret i32 %E 42 ; CHECK-LABEL: @test5( 43 ; CHECK: %D1 = sub i32 %C, %B 44 ; CHECK: %E = add 45 ; CHECK: ret i32 %E 46 } 47 48 define i32 @test6(i32 %A, i32 %B) { 49 %C = and i32 %A, %B 50 %D = sub i32 %A, %C 51 ret i32 %D 52 ; CHECK-LABEL: @test6( 53 ; CHECK-NEXT: xor i32 %B, -1 54 ; CHECK-NEXT: %D = and i32 55 ; CHECK-NEXT: ret i32 %D 56 } 57 58 define i32 @test7(i32 %A) { 59 %B = sub i32 -1, %A 60 ret i32 %B 61 ; CHECK-LABEL: @test7( 62 ; CHECK: %B = xor i32 %A, -1 63 ; CHECK: ret i32 %B 64 } 65 66 define i32 @test8(i32 %A) { 67 %B = mul i32 9, %A 68 %C = sub i32 %B, %A 69 ret i32 %C 70 ; CHECK-LABEL: @test8( 71 ; CHECK: %C = shl i32 %A, 3 72 ; CHECK: ret i32 %C 73 } 74 75 define i32 @test9(i32 %A) { 76 %B = mul i32 3, %A 77 %C = sub i32 %A, %B 78 ret i32 %C 79 ; CHECK-LABEL: @test9( 80 ; CHECK: %C = mul i32 %A, -2 81 ; CHECK: ret i32 %C 82 } 83 84 define i32 @test10(i32 %A, i32 %B) { 85 %C = sub i32 0, %A 86 %D = sub i32 0, %B 87 %E = mul i32 %C, %D 88 ret i32 %E 89 ; CHECK-LABEL: @test10( 90 ; CHECK: %E = mul i32 %A, %B 91 ; CHECK: ret i32 %E 92 } 93 94 define i32 @test10a(i32 %A) { 95 %C = sub i32 0, %A 96 %E = mul i32 %C, 7 97 ret i32 %E 98 ; CHECK-LABEL: @test10a( 99 ; CHECK: %E = mul i32 %A, -7 100 ; CHECK: ret i32 %E 101 } 102 103 define i1 @test11(i8 %A, i8 %B) { 104 %C = sub i8 %A, %B 105 %cD = icmp ne i8 %C, 0 106 ret i1 %cD 107 ; CHECK-LABEL: @test11( 108 ; CHECK: %cD = icmp ne i8 %A, %B 109 ; CHECK: ret i1 %cD 110 } 111 112 define i32 @test12(i32 %A) { 113 %B = ashr i32 %A, 31 114 %C = sub i32 0, %B 115 ret i32 %C 116 ; CHECK-LABEL: @test12( 117 ; CHECK: %C = lshr i32 %A, 31 118 ; CHECK: ret i32 %C 119 } 120 121 define i32 @test13(i32 %A) { 122 %B = lshr i32 %A, 31 123 %C = sub i32 0, %B 124 ret i32 %C 125 ; CHECK-LABEL: @test13( 126 ; CHECK: %C = ashr i32 %A, 31 127 ; CHECK: ret i32 %C 128 } 129 130 define i32 @test14(i32 %A) { 131 %B = lshr i32 %A, 31 132 %C = bitcast i32 %B to i32 133 %D = sub i32 0, %C 134 ret i32 %D 135 ; CHECK-LABEL: @test14( 136 ; CHECK: %D = ashr i32 %A, 31 137 ; CHECK: ret i32 %D 138 } 139 140 define i32 @test15(i32 %A, i32 %B) { 141 %C = sub i32 0, %A 142 %D = srem i32 %B, %C 143 ret i32 %D 144 ; CHECK-LABEL: @test15( 145 ; CHECK: %D = srem i32 %B, %A 146 ; CHECK: ret i32 %D 147 } 148 149 define i32 @test16(i32 %A) { 150 %X = sdiv i32 %A, 1123 151 %Y = sub i32 0, %X 152 ret i32 %Y 153 ; CHECK-LABEL: @test16( 154 ; CHECK: %Y = sdiv i32 %A, -1123 155 ; CHECK: ret i32 %Y 156 } 157 158 ; Can't fold subtract here because negation it might oveflow. 159 ; PR3142 160 define i32 @test17(i32 %A) { 161 %B = sub i32 0, %A 162 %C = sdiv i32 %B, 1234 163 ret i32 %C 164 ; CHECK-LABEL: @test17( 165 ; CHECK: %B = sub i32 0, %A 166 ; CHECK: %C = sdiv i32 %B, 1234 167 ; CHECK: ret i32 %C 168 } 169 170 define i64 @test18(i64 %Y) { 171 %tmp.4 = shl i64 %Y, 2 172 %tmp.12 = shl i64 %Y, 2 173 %tmp.8 = sub i64 %tmp.4, %tmp.12 174 ret i64 %tmp.8 175 ; CHECK-LABEL: @test18( 176 ; CHECK: ret i64 0 177 } 178 179 define i32 @test19(i32 %X, i32 %Y) { 180 %Z = sub i32 %X, %Y 181 %Q = add i32 %Z, %Y 182 ret i32 %Q 183 ; CHECK-LABEL: @test19( 184 ; CHECK: ret i32 %X 185 } 186 187 define i1 @test20(i32 %g, i32 %h) { 188 %tmp.2 = sub i32 %g, %h 189 %tmp.4 = icmp ne i32 %tmp.2, %g 190 ret i1 %tmp.4 191 ; CHECK-LABEL: @test20( 192 ; CHECK: %tmp.4 = icmp ne i32 %h, 0 193 ; CHECK: ret i1 %tmp.4 194 } 195 196 define i1 @test21(i32 %g, i32 %h) { 197 %tmp.2 = sub i32 %g, %h 198 %tmp.4 = icmp ne i32 %tmp.2, %g 199 ret i1 %tmp.4 200 ; CHECK-LABEL: @test21( 201 ; CHECK: %tmp.4 = icmp ne i32 %h, 0 202 ; CHECK: ret i1 %tmp.4 203 } 204 205 ; PR2298 206 define zeroext i1 @test22(i32 %a, i32 %b) nounwind { 207 %tmp2 = sub i32 0, %a 208 %tmp4 = sub i32 0, %b 209 %tmp5 = icmp eq i32 %tmp2, %tmp4 210 ret i1 %tmp5 211 ; CHECK-LABEL: @test22( 212 ; CHECK: %tmp5 = icmp eq i32 %b, %a 213 ; CHECK: ret i1 %tmp5 214 } 215 216 ; rdar://7362831 217 define i32 @test23(i8* %P, i64 %A){ 218 %B = getelementptr inbounds i8* %P, i64 %A 219 %C = ptrtoint i8* %B to i64 220 %D = trunc i64 %C to i32 221 %E = ptrtoint i8* %P to i64 222 %F = trunc i64 %E to i32 223 %G = sub i32 %D, %F 224 ret i32 %G 225 ; CHECK-LABEL: @test23( 226 ; CHECK-NEXT: = trunc i64 %A to i32 227 ; CHECK-NEXT: ret i32 228 } 229 230 define i8 @test23_as1(i8 addrspace(1)* %P, i16 %A) { 231 ; CHECK: @test23_as1 232 ; CHECK-NEXT: = trunc i16 %A to i8 233 ; CHECK-NEXT: ret i8 234 %B = getelementptr inbounds i8 addrspace(1)* %P, i16 %A 235 %C = ptrtoint i8 addrspace(1)* %B to i16 236 %D = trunc i16 %C to i8 237 %E = ptrtoint i8 addrspace(1)* %P to i16 238 %F = trunc i16 %E to i8 239 %G = sub i8 %D, %F 240 ret i8 %G 241 } 242 243 define i64 @test24(i8* %P, i64 %A){ 244 %B = getelementptr inbounds i8* %P, i64 %A 245 %C = ptrtoint i8* %B to i64 246 %E = ptrtoint i8* %P to i64 247 %G = sub i64 %C, %E 248 ret i64 %G 249 ; CHECK-LABEL: @test24( 250 ; CHECK-NEXT: ret i64 %A 251 } 252 253 define i16 @test24_as1(i8 addrspace(1)* %P, i16 %A) { 254 ; CHECK: @test24_as1 255 ; CHECK-NEXT: ret i16 %A 256 %B = getelementptr inbounds i8 addrspace(1)* %P, i16 %A 257 %C = ptrtoint i8 addrspace(1)* %B to i16 258 %E = ptrtoint i8 addrspace(1)* %P to i16 259 %G = sub i16 %C, %E 260 ret i16 %G 261 } 262 263 define i64 @test24a(i8* %P, i64 %A){ 264 %B = getelementptr inbounds i8* %P, i64 %A 265 %C = ptrtoint i8* %B to i64 266 %E = ptrtoint i8* %P to i64 267 %G = sub i64 %E, %C 268 ret i64 %G 269 ; CHECK-LABEL: @test24a( 270 ; CHECK-NEXT: sub i64 0, %A 271 ; CHECK-NEXT: ret i64 272 } 273 274 define i16 @test24a_as1(i8 addrspace(1)* %P, i16 %A) { 275 ; CHECK: @test24a_as1 276 ; CHECK-NEXT: sub i16 0, %A 277 ; CHECK-NEXT: ret i16 278 %B = getelementptr inbounds i8 addrspace(1)* %P, i16 %A 279 %C = ptrtoint i8 addrspace(1)* %B to i16 280 %E = ptrtoint i8 addrspace(1)* %P to i16 281 %G = sub i16 %E, %C 282 ret i16 %G 283 } 284 285 286 @Arr = external global [42 x i16] 287 288 define i64 @test24b(i8* %P, i64 %A){ 289 %B = getelementptr inbounds [42 x i16]* @Arr, i64 0, i64 %A 290 %C = ptrtoint i16* %B to i64 291 %G = sub i64 %C, ptrtoint ([42 x i16]* @Arr to i64) 292 ret i64 %G 293 ; CHECK-LABEL: @test24b( 294 ; CHECK-NEXT: shl nuw i64 %A, 1 295 ; CHECK-NEXT: ret i64 296 } 297 298 299 define i64 @test25(i8* %P, i64 %A){ 300 %B = getelementptr inbounds [42 x i16]* @Arr, i64 0, i64 %A 301 %C = ptrtoint i16* %B to i64 302 %G = sub i64 %C, ptrtoint (i16* getelementptr ([42 x i16]* @Arr, i64 1, i64 0) to i64) 303 ret i64 %G 304 ; CHECK-LABEL: @test25( 305 ; CHECK-NEXT: shl nuw i64 %A, 1 306 ; CHECK-NEXT: add i64 {{.*}}, -84 307 ; CHECK-NEXT: ret i64 308 } 309 310 @Arr_as1 = external addrspace(1) global [42 x i16] 311 312 define i16 @test25_as1(i8 addrspace(1)* %P, i64 %A) { 313 ; CHECK: @test25_as1 314 ; CHECK-NEXT: %1 = trunc i64 %A to i16 315 ; CHECK-NEXT: shl nuw i16 %1, 1 316 ; CHECK-NEXT: add i16 {{.*}}, -84 317 ; CHECK-NEXT: ret i16 318 %B = getelementptr inbounds [42 x i16] addrspace(1)* @Arr_as1, i64 0, i64 %A 319 %C = ptrtoint i16 addrspace(1)* %B to i16 320 %G = sub i16 %C, ptrtoint (i16 addrspace(1)* getelementptr ([42 x i16] addrspace(1)* @Arr_as1, i64 1, i64 0) to i16) 321 ret i16 %G 322 } 323 324 define i32 @test26(i32 %x) { 325 %shl = shl i32 3, %x 326 %neg = sub i32 0, %shl 327 ret i32 %neg 328 ; CHECK-LABEL: @test26( 329 ; CHECK-NEXT: shl i32 -3 330 ; CHECK-NEXT: ret i32 331 } 332 333 define i32 @test27(i32 %x, i32 %y) { 334 %mul = mul i32 %y, -8 335 %sub = sub i32 %x, %mul 336 ret i32 %sub 337 ; CHECK-LABEL: @test27( 338 ; CHECK-NEXT: shl i32 %y, 3 339 ; CHECK-NEXT: add i32 340 ; CHECK-NEXT: ret i32 341 } 342 343 define i32 @test28(i32 %x, i32 %y, i32 %z) { 344 %neg = sub i32 0, %z 345 %mul = mul i32 %neg, %y 346 %sub = sub i32 %x, %mul 347 ret i32 %sub 348 ; CHECK-LABEL: @test28( 349 ; CHECK-NEXT: mul i32 %z, %y 350 ; CHECK-NEXT: add i32 351 ; CHECK-NEXT: ret i32 352 } 353 354 define i64 @test29(i8* %foo, i64 %i, i64 %j) { 355 %gep1 = getelementptr inbounds i8* %foo, i64 %i 356 %gep2 = getelementptr inbounds i8* %foo, i64 %j 357 %cast1 = ptrtoint i8* %gep1 to i64 358 %cast2 = ptrtoint i8* %gep2 to i64 359 %sub = sub i64 %cast1, %cast2 360 ret i64 %sub 361 ; CHECK-LABEL: @test29( 362 ; CHECK-NEXT: sub i64 %i, %j 363 ; CHECK-NEXT: ret i64 364 } 365 366 define i64 @test30(i8* %foo, i64 %i, i64 %j) { 367 %bit = bitcast i8* %foo to i32* 368 %gep1 = getelementptr inbounds i32* %bit, i64 %i 369 %gep2 = getelementptr inbounds i8* %foo, i64 %j 370 %cast1 = ptrtoint i32* %gep1 to i64 371 %cast2 = ptrtoint i8* %gep2 to i64 372 %sub = sub i64 %cast1, %cast2 373 ret i64 %sub 374 ; CHECK-LABEL: @test30( 375 ; CHECK-NEXT: %gep1.idx = shl nuw i64 %i, 2 376 ; CHECK-NEXT: sub i64 %gep1.idx, %j 377 ; CHECK-NEXT: ret i64 378 } 379 380 define i16 @test30_as1(i8 addrspace(1)* %foo, i16 %i, i16 %j) { 381 ; CHECK-LABEL: @test30_as1( 382 ; CHECK-NEXT: %gep1.idx = shl nuw i16 %i, 2 383 ; CHECK-NEXT: sub i16 %gep1.idx, %j 384 ; CHECK-NEXT: ret i16 385 %bit = bitcast i8 addrspace(1)* %foo to i32 addrspace(1)* 386 %gep1 = getelementptr inbounds i32 addrspace(1)* %bit, i16 %i 387 %gep2 = getelementptr inbounds i8 addrspace(1)* %foo, i16 %j 388 %cast1 = ptrtoint i32 addrspace(1)* %gep1 to i16 389 %cast2 = ptrtoint i8 addrspace(1)* %gep2 to i16 390 %sub = sub i16 %cast1, %cast2 391 ret i16 %sub 392 } 393 394 define <2 x i64> @test31(<2 x i64> %A) { 395 %xor = xor <2 x i64> %A, <i64 -1, i64 -1> 396 %sub = sub <2 x i64> <i64 2, i64 3>, %xor 397 ret <2 x i64> %sub 398 ; CHECK-LABEL: @test31( 399 ; CHECK-NEXT: %sub = add <2 x i64> %A, <i64 3, i64 4> 400 ; CHECK-NEXT: ret <2 x i64> %sub 401 } 402 403 define <2 x i64> @test32(<2 x i64> %A) { 404 %add = add <2 x i64> %A, <i64 -1, i64 -1> 405 %sub = sub <2 x i64> <i64 2, i64 3>, %add 406 ret <2 x i64> %sub 407 ; CHECK-LABEL: @test32( 408 ; CHECK-NEXT: %sub = sub <2 x i64> <i64 3, i64 4> 409 ; CHECK-NEXT: ret <2 x i64> %sub 410 } 411 412 define <2 x i64> @test33(<2 x i1> %A) { 413 %ext = zext <2 x i1> %A to <2 x i64> 414 %sub = sub <2 x i64> zeroinitializer, %ext 415 ret <2 x i64> %sub 416 ; CHECK-LABEL: @test33( 417 ; CHECK-NEXT: %sub = sext <2 x i1> %A to <2 x i64> 418 ; CHECK-NEXT: ret <2 x i64> %sub 419 } 420 421 define <2 x i64> @test34(<2 x i1> %A) { 422 %ext = sext <2 x i1> %A to <2 x i64> 423 %sub = sub <2 x i64> zeroinitializer, %ext 424 ret <2 x i64> %sub 425 ; CHECK-LABEL: @test34( 426 ; CHECK-NEXT: %sub = zext <2 x i1> %A to <2 x i64> 427 ; CHECK-NEXT: ret <2 x i64> %sub 428 } 429 430 define <2 x i64> @test35(<2 x i64> %A) { 431 %mul = mul <2 x i64> %A, <i64 3, i64 4> 432 %sub = sub <2 x i64> %A, %mul 433 ret <2 x i64> %sub 434 ; CHECK-LABEL: @test35( 435 ; CHECK-NEXT: %sub = mul <2 x i64> %A, <i64 -2, i64 -3> 436 ; CHECK-NEXT: ret <2 x i64> %sub 437 } 438 439 define <2 x i64> @test36(<2 x i64> %A) { 440 %shl = shl <2 x i64> %A, <i64 3, i64 4> 441 %sub = sub <2 x i64> %shl, %A 442 ret <2 x i64> %sub 443 ; CHECK-LABEL: @test36( 444 ; CHECK-NEXT: %sub = mul <2 x i64> %A, <i64 7, i64 15> 445 ; CHECK-NEXT: ret <2 x i64> %sub 446 } 447 448 define <2 x i32> @test37(<2 x i32> %A) { 449 %div = sdiv <2 x i32> %A, <i32 -2147483648, i32 -2147483648> 450 %sub = sub nsw <2 x i32> zeroinitializer, %div 451 ret <2 x i32> %sub 452 ; CHECK-LABEL: @test37( 453 ; CHECK-NEXT: [[ICMP:%.*]] = icmp eq <2 x i32> %A, <i32 -2147483648, i32 -2147483648> 454 ; CHECK-NEXT: [[SEXT:%.*]] = sext <2 x i1> [[ICMP]] to <2 x i32> 455 ; CHECK-NEXT: ret <2 x i32> [[SEXT]] 456 } 457 458 define i32 @test38(i32 %A) { 459 %div = sdiv i32 %A, -2147483648 460 %sub = sub nsw i32 0, %div 461 ret i32 %sub 462 ; CHECK-LABEL: @test38( 463 ; CHECK-NEXT: [[ICMP:%.*]] = icmp eq i32 %A, -2147483648 464 ; CHECK-NEXT: [[SEXT:%.*]] = sext i1 [[ICMP]] to i32 465 ; CHECK-NEXT: ret i32 [[SEXT]] 466 } 467