1 ; RUN: opt < %s -instcombine -S | FileCheck %s 2 3 target datalayout = 4 "e-p:64:64:64-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-n8:16:32:64" 5 6 define i32 @test1(i32 %X) { 7 entry: 8 icmp slt i32 %X, 0 ; <i1>:0 [#uses=1] 9 zext i1 %0 to i32 ; <i32>:1 [#uses=1] 10 ret i32 %1 11 ; CHECK: @test1 12 ; CHECK: lshr i32 %X, 31 13 ; CHECK-NEXT: ret i32 14 } 15 16 define i32 @test2(i32 %X) { 17 entry: 18 icmp ult i32 %X, -2147483648 ; <i1>:0 [#uses=1] 19 zext i1 %0 to i32 ; <i32>:1 [#uses=1] 20 ret i32 %1 21 ; CHECK: @test2 22 ; CHECK: lshr i32 %X, 31 23 ; CHECK-NEXT: xor i32 24 ; CHECK-NEXT: ret i32 25 } 26 27 define i32 @test3(i32 %X) { 28 entry: 29 icmp slt i32 %X, 0 ; <i1>:0 [#uses=1] 30 sext i1 %0 to i32 ; <i32>:1 [#uses=1] 31 ret i32 %1 32 ; CHECK: @test3 33 ; CHECK: ashr i32 %X, 31 34 ; CHECK-NEXT: ret i32 35 } 36 37 define i32 @test4(i32 %X) { 38 entry: 39 icmp ult i32 %X, -2147483648 ; <i1>:0 [#uses=1] 40 sext i1 %0 to i32 ; <i32>:1 [#uses=1] 41 ret i32 %1 42 ; CHECK: @test4 43 ; CHECK: ashr i32 %X, 31 44 ; CHECK-NEXT: xor i32 45 ; CHECK-NEXT: ret i32 46 } 47 48 ; PR4837 49 define <2 x i1> @test5(<2 x i64> %x) { 50 entry: 51 %V = icmp eq <2 x i64> %x, undef 52 ret <2 x i1> %V 53 ; CHECK: @test5 54 ; CHECK: ret <2 x i1> <i1 true, i1 true> 55 } 56 57 define i32 @test6(i32 %a, i32 %b) { 58 %c = icmp sle i32 %a, -1 59 %d = zext i1 %c to i32 60 %e = sub i32 0, %d 61 %f = and i32 %e, %b 62 ret i32 %f 63 ; CHECK: @test6 64 ; CHECK-NEXT: ashr i32 %a, 31 65 ; CHECK-NEXT: %f = and i32 %e, %b 66 ; CHECK-NEXT: ret i32 %f 67 } 68 69 70 define i1 @test7(i32 %x) { 71 entry: 72 %a = add i32 %x, -1 73 %b = icmp ult i32 %a, %x 74 ret i1 %b 75 ; CHECK: @test7 76 ; CHECK: %b = icmp ne i32 %x, 0 77 ; CHECK: ret i1 %b 78 } 79 80 define i1 @test8(i32 %x){ 81 entry: 82 %a = add i32 %x, -1 83 %b = icmp eq i32 %a, %x 84 ret i1 %b 85 ; CHECK: @test8 86 ; CHECK: ret i1 false 87 } 88 89 define i1 @test9(i32 %x) { 90 entry: 91 %a = add i32 %x, -2 92 %b = icmp ugt i32 %x, %a 93 ret i1 %b 94 ; CHECK: @test9 95 ; CHECK: icmp ugt i32 %x, 1 96 ; CHECK: ret i1 %b 97 } 98 99 define i1 @test10(i32 %x){ 100 entry: 101 %a = add i32 %x, -1 102 %b = icmp slt i32 %a, %x 103 ret i1 %b 104 105 ; CHECK: @test10 106 ; CHECK: %b = icmp ne i32 %x, -2147483648 107 ; CHECK: ret i1 %b 108 } 109 110 define i1 @test11(i32 %x) { 111 %a = add nsw i32 %x, 8 112 %b = icmp slt i32 %x, %a 113 ret i1 %b 114 ; CHECK: @test11 115 ; CHECK: ret i1 true 116 } 117 118 ; PR6195 119 define i1 @test12(i1 %A) { 120 %S = select i1 %A, i64 -4294967295, i64 8589934591 121 %B = icmp ne i64 bitcast (<2 x i32> <i32 1, i32 -1> to i64), %S 122 ret i1 %B 123 ; CHECK: @test12 124 ; CHECK-NEXT: %B = select i1 125 ; CHECK-NEXT: ret i1 %B 126 } 127 128 ; PR6481 129 define i1 @test13(i8 %X) nounwind readnone { 130 entry: 131 %cmp = icmp slt i8 undef, %X 132 ret i1 %cmp 133 ; CHECK: @test13 134 ; CHECK: ret i1 false 135 } 136 137 define i1 @test14(i8 %X) nounwind readnone { 138 entry: 139 %cmp = icmp slt i8 undef, -128 140 ret i1 %cmp 141 ; CHECK: @test14 142 ; CHECK: ret i1 false 143 } 144 145 define i1 @test15() nounwind readnone { 146 entry: 147 %cmp = icmp eq i8 undef, -128 148 ret i1 %cmp 149 ; CHECK: @test15 150 ; CHECK: ret i1 undef 151 } 152 153 define i1 @test16() nounwind readnone { 154 entry: 155 %cmp = icmp ne i8 undef, -128 156 ret i1 %cmp 157 ; CHECK: @test16 158 ; CHECK: ret i1 undef 159 } 160 161 define i1 @test17(i32 %x) nounwind { 162 %shl = shl i32 1, %x 163 %and = and i32 %shl, 8 164 %cmp = icmp eq i32 %and, 0 165 ret i1 %cmp 166 ; CHECK: @test17 167 ; CHECK-NEXT: %cmp = icmp ne i32 %x, 3 168 } 169 170 171 define i1 @test18(i32 %x) nounwind { 172 %sh = lshr i32 8, %x 173 %and = and i32 %sh, 1 174 %cmp = icmp eq i32 %and, 0 175 ret i1 %cmp 176 ; CHECK: @test18 177 ; CHECK-NEXT: %cmp = icmp ne i32 %x, 3 178 } 179 180 define i1 @test19(i32 %x) nounwind { 181 %shl = shl i32 1, %x 182 %and = and i32 %shl, 8 183 %cmp = icmp eq i32 %and, 8 184 ret i1 %cmp 185 ; CHECK: @test19 186 ; CHECK-NEXT: %cmp = icmp eq i32 %x, 3 187 } 188 189 define i1 @test20(i32 %x) nounwind { 190 %shl = shl i32 1, %x 191 %and = and i32 %shl, 8 192 %cmp = icmp ne i32 %and, 0 193 ret i1 %cmp 194 ; CHECK: @test20 195 ; CHECK-NEXT: %cmp = icmp eq i32 %x, 3 196 } 197 198 define i1 @test21(i8 %x, i8 %y) { 199 ; CHECK: @test21 200 ; CHECK-NOT: or i8 201 ; CHECK: icmp ugt 202 %A = or i8 %x, 1 203 %B = icmp ugt i8 %A, 3 204 ret i1 %B 205 } 206 207 define i1 @test22(i8 %x, i8 %y) { 208 ; CHECK: @test22 209 ; CHECK-NOT: or i8 210 ; CHECK: icmp ult 211 %A = or i8 %x, 1 212 %B = icmp ult i8 %A, 4 213 ret i1 %B 214 } 215 216 ; PR2740 217 ; CHECK: @test23 218 ; CHECK: icmp sgt i32 %x, 1328634634 219 define i1 @test23(i32 %x) nounwind { 220 %i3 = sdiv i32 %x, -1328634635 221 %i4 = icmp eq i32 %i3, -1 222 ret i1 %i4 223 } 224 225 @X = global [1000 x i32] zeroinitializer 226 227 ; PR8882 228 ; CHECK: @test24 229 ; CHECK: %cmp = icmp eq i64 %i, 1000 230 ; CHECK: ret i1 %cmp 231 define i1 @test24(i64 %i) { 232 %p1 = getelementptr inbounds i32* getelementptr inbounds ([1000 x i32]* @X, i64 0, i64 0), i64 %i 233 %cmp = icmp eq i32* %p1, getelementptr inbounds ([1000 x i32]* @X, i64 1, i64 0) 234 ret i1 %cmp 235 } 236 237 ; CHECK: @test25 238 ; X + Z > Y + Z -> X > Y if there is no overflow. 239 ; CHECK: %c = icmp sgt i32 %x, %y 240 ; CHECK: ret i1 %c 241 define i1 @test25(i32 %x, i32 %y, i32 %z) { 242 %lhs = add nsw i32 %x, %z 243 %rhs = add nsw i32 %y, %z 244 %c = icmp sgt i32 %lhs, %rhs 245 ret i1 %c 246 } 247 248 ; CHECK: @test26 249 ; X + Z > Y + Z -> X > Y if there is no overflow. 250 ; CHECK: %c = icmp ugt i32 %x, %y 251 ; CHECK: ret i1 %c 252 define i1 @test26(i32 %x, i32 %y, i32 %z) { 253 %lhs = add nuw i32 %x, %z 254 %rhs = add nuw i32 %y, %z 255 %c = icmp ugt i32 %lhs, %rhs 256 ret i1 %c 257 } 258 259 ; CHECK: @test27 260 ; X - Z > Y - Z -> X > Y if there is no overflow. 261 ; CHECK: %c = icmp sgt i32 %x, %y 262 ; CHECK: ret i1 %c 263 define i1 @test27(i32 %x, i32 %y, i32 %z) { 264 %lhs = sub nsw i32 %x, %z 265 %rhs = sub nsw i32 %y, %z 266 %c = icmp sgt i32 %lhs, %rhs 267 ret i1 %c 268 } 269 270 ; CHECK: @test28 271 ; X - Z > Y - Z -> X > Y if there is no overflow. 272 ; CHECK: %c = icmp ugt i32 %x, %y 273 ; CHECK: ret i1 %c 274 define i1 @test28(i32 %x, i32 %y, i32 %z) { 275 %lhs = sub nuw i32 %x, %z 276 %rhs = sub nuw i32 %y, %z 277 %c = icmp ugt i32 %lhs, %rhs 278 ret i1 %c 279 } 280 281 ; CHECK: @test29 282 ; X + Y > X -> Y > 0 if there is no overflow. 283 ; CHECK: %c = icmp sgt i32 %y, 0 284 ; CHECK: ret i1 %c 285 define i1 @test29(i32 %x, i32 %y) { 286 %lhs = add nsw i32 %x, %y 287 %c = icmp sgt i32 %lhs, %x 288 ret i1 %c 289 } 290 291 ; CHECK: @test30 292 ; X + Y > X -> Y > 0 if there is no overflow. 293 ; CHECK: %c = icmp ne i32 %y, 0 294 ; CHECK: ret i1 %c 295 define i1 @test30(i32 %x, i32 %y) { 296 %lhs = add nuw i32 %x, %y 297 %c = icmp ugt i32 %lhs, %x 298 ret i1 %c 299 } 300 301 ; CHECK: @test31 302 ; X > X + Y -> 0 > Y if there is no overflow. 303 ; CHECK: %c = icmp slt i32 %y, 0 304 ; CHECK: ret i1 %c 305 define i1 @test31(i32 %x, i32 %y) { 306 %rhs = add nsw i32 %x, %y 307 %c = icmp sgt i32 %x, %rhs 308 ret i1 %c 309 } 310 311 ; CHECK: @test32 312 ; X > X + Y -> 0 > Y if there is no overflow. 313 ; CHECK: ret i1 false 314 define i1 @test32(i32 %x, i32 %y) { 315 %rhs = add nuw i32 %x, %y 316 %c = icmp ugt i32 %x, %rhs 317 ret i1 %c 318 } 319 320 ; CHECK: @test33 321 ; X - Y > X -> 0 > Y if there is no overflow. 322 ; CHECK: %c = icmp slt i32 %y, 0 323 ; CHECK: ret i1 %c 324 define i1 @test33(i32 %x, i32 %y) { 325 %lhs = sub nsw i32 %x, %y 326 %c = icmp sgt i32 %lhs, %x 327 ret i1 %c 328 } 329 330 ; CHECK: @test34 331 ; X - Y > X -> 0 > Y if there is no overflow. 332 ; CHECK: ret i1 false 333 define i1 @test34(i32 %x, i32 %y) { 334 %lhs = sub nuw i32 %x, %y 335 %c = icmp ugt i32 %lhs, %x 336 ret i1 %c 337 } 338 339 ; CHECK: @test35 340 ; X > X - Y -> Y > 0 if there is no overflow. 341 ; CHECK: %c = icmp sgt i32 %y, 0 342 ; CHECK: ret i1 %c 343 define i1 @test35(i32 %x, i32 %y) { 344 %rhs = sub nsw i32 %x, %y 345 %c = icmp sgt i32 %x, %rhs 346 ret i1 %c 347 } 348 349 ; CHECK: @test36 350 ; X > X - Y -> Y > 0 if there is no overflow. 351 ; CHECK: %c = icmp ne i32 %y, 0 352 ; CHECK: ret i1 %c 353 define i1 @test36(i32 %x, i32 %y) { 354 %rhs = sub nuw i32 %x, %y 355 %c = icmp ugt i32 %x, %rhs 356 ret i1 %c 357 } 358 359 ; CHECK: @test37 360 ; X - Y > X - Z -> Z > Y if there is no overflow. 361 ; CHECK: %c = icmp sgt i32 %z, %y 362 ; CHECK: ret i1 %c 363 define i1 @test37(i32 %x, i32 %y, i32 %z) { 364 %lhs = sub nsw i32 %x, %y 365 %rhs = sub nsw i32 %x, %z 366 %c = icmp sgt i32 %lhs, %rhs 367 ret i1 %c 368 } 369 370 ; CHECK: @test38 371 ; X - Y > X - Z -> Z > Y if there is no overflow. 372 ; CHECK: %c = icmp ugt i32 %z, %y 373 ; CHECK: ret i1 %c 374 define i1 @test38(i32 %x, i32 %y, i32 %z) { 375 %lhs = sub nuw i32 %x, %y 376 %rhs = sub nuw i32 %x, %z 377 %c = icmp ugt i32 %lhs, %rhs 378 ret i1 %c 379 } 380 381 ; PR9343 #1 382 ; CHECK: @test39 383 ; CHECK: %B = icmp eq i32 %X, 0 384 define i1 @test39(i32 %X, i32 %Y) { 385 %A = ashr exact i32 %X, %Y 386 %B = icmp eq i32 %A, 0 387 ret i1 %B 388 } 389 390 ; CHECK: @test40 391 ; CHECK: %B = icmp ne i32 %X, 0 392 define i1 @test40(i32 %X, i32 %Y) { 393 %A = lshr exact i32 %X, %Y 394 %B = icmp ne i32 %A, 0 395 ret i1 %B 396 } 397 398 ; PR9343 #3 399 ; CHECK: @test41 400 ; CHECK: ret i1 true 401 define i1 @test41(i32 %X, i32 %Y) { 402 %A = urem i32 %X, %Y 403 %B = icmp ugt i32 %Y, %A 404 ret i1 %B 405 } 406 407 ; CHECK: @test42 408 ; CHECK: %B = icmp sgt i32 %Y, -1 409 define i1 @test42(i32 %X, i32 %Y) { 410 %A = srem i32 %X, %Y 411 %B = icmp slt i32 %A, %Y 412 ret i1 %B 413 } 414 415 ; CHECK: @test43 416 ; CHECK: %B = icmp slt i32 %Y, 0 417 define i1 @test43(i32 %X, i32 %Y) { 418 %A = srem i32 %X, %Y 419 %B = icmp slt i32 %Y, %A 420 ret i1 %B 421 } 422 423 ; CHECK: @test44 424 ; CHECK: %B = icmp sgt i32 %Y, -1 425 define i1 @test44(i32 %X, i32 %Y) { 426 %A = srem i32 %X, %Y 427 %B = icmp slt i32 %A, %Y 428 ret i1 %B 429 } 430 431 ; CHECK: @test45 432 ; CHECK: %B = icmp slt i32 %Y, 0 433 define i1 @test45(i32 %X, i32 %Y) { 434 %A = srem i32 %X, %Y 435 %B = icmp slt i32 %Y, %A 436 ret i1 %B 437 } 438 439 ; PR9343 #4 440 ; CHECK: @test46 441 ; CHECK: %C = icmp ult i32 %X, %Y 442 define i1 @test46(i32 %X, i32 %Y, i32 %Z) { 443 %A = ashr exact i32 %X, %Z 444 %B = ashr exact i32 %Y, %Z 445 %C = icmp ult i32 %A, %B 446 ret i1 %C 447 } 448 449 ; PR9343 #5 450 ; CHECK: @test47 451 ; CHECK: %C = icmp ugt i32 %X, %Y 452 define i1 @test47(i32 %X, i32 %Y, i32 %Z) { 453 %A = ashr exact i32 %X, %Z 454 %B = ashr exact i32 %Y, %Z 455 %C = icmp ugt i32 %A, %B 456 ret i1 %C 457 } 458 459 ; PR9343 #8 460 ; CHECK: @test48 461 ; CHECK: %C = icmp eq i32 %X, %Y 462 define i1 @test48(i32 %X, i32 %Y, i32 %Z) { 463 %A = sdiv exact i32 %X, %Z 464 %B = sdiv exact i32 %Y, %Z 465 %C = icmp eq i32 %A, %B 466 ret i1 %C 467 } 468 469 ; PR8469 470 ; CHECK: @test49 471 ; CHECK: ret <2 x i1> <i1 true, i1 true> 472 define <2 x i1> @test49(<2 x i32> %tmp3) { 473 entry: 474 %tmp11 = and <2 x i32> %tmp3, <i32 3, i32 3> 475 %cmp = icmp ult <2 x i32> %tmp11, <i32 4, i32 4> 476 ret <2 x i1> %cmp 477 } 478 479 ; PR9343 #7 480 ; CHECK: @test50 481 ; CHECK: ret i1 true 482 define i1 @test50(i16 %X, i32 %Y) { 483 %A = zext i16 %X to i32 484 %B = srem i32 %A, %Y 485 %C = icmp sgt i32 %B, -1 486 ret i1 %C 487 } 488 489 ; CHECK: @test51 490 ; CHECK: ret i1 %C 491 define i1 @test51(i32 %X, i32 %Y) { 492 %A = and i32 %X, 2147483648 493 %B = srem i32 %A, %Y 494 %C = icmp sgt i32 %B, -1 495 ret i1 %C 496 } 497 498 ; CHECK: @test52 499 ; CHECK-NEXT: and i32 %x1, 16711935 500 ; CHECK-NEXT: icmp eq i32 {{.*}}, 4980863 501 ; CHECK-NEXT: ret i1 502 define i1 @test52(i32 %x1) nounwind { 503 %conv = and i32 %x1, 255 504 %cmp = icmp eq i32 %conv, 127 505 %tmp2 = lshr i32 %x1, 16 506 %tmp3 = trunc i32 %tmp2 to i8 507 %cmp15 = icmp eq i8 %tmp3, 76 508 509 %A = and i1 %cmp, %cmp15 510 ret i1 %A 511 } 512 513 ; PR9838 514 ; CHECK: @test53 515 ; CHECK-NEXT: ashr exact 516 ; CHECK-NEXT: ashr 517 ; CHECK-NEXT: icmp 518 define i1 @test53(i32 %a, i32 %b) nounwind { 519 %x = ashr exact i32 %a, 30 520 %y = ashr i32 %b, 30 521 %z = icmp eq i32 %x, %y 522 ret i1 %z 523 } 524 525 ; CHECK: @test54 526 ; CHECK-NEXT: %and = and i8 %a, -64 527 ; CHECK-NEXT icmp eq i8 %and, -128 528 define i1 @test54(i8 %a) nounwind { 529 %ext = zext i8 %a to i32 530 %and = and i32 %ext, 192 531 %ret = icmp eq i32 %and, 128 532 ret i1 %ret 533 } 534 535 ; CHECK: @test55 536 ; CHECK-NEXT: icmp eq i32 %a, -123 537 define i1 @test55(i32 %a) { 538 %sub = sub i32 0, %a 539 %cmp = icmp eq i32 %sub, 123 540 ret i1 %cmp 541 } 542 543 ; CHECK: @test56 544 ; CHECK-NEXT: icmp eq i32 %a, -113 545 define i1 @test56(i32 %a) { 546 %sub = sub i32 10, %a 547 %cmp = icmp eq i32 %sub, 123 548 ret i1 %cmp 549 } 550 551 ; PR10267 Don't make icmps more expensive when no other inst is subsumed. 552 declare void @foo(i32) 553 ; CHECK: @test57 554 ; CHECK: %and = and i32 %a, -2 555 ; CHECK: %cmp = icmp ne i32 %and, 0 556 define i1 @test57(i32 %a) { 557 %and = and i32 %a, -2 558 %cmp = icmp ne i32 %and, 0 559 call void @foo(i32 %and) 560 ret i1 %cmp 561 } 562