signed-truncation-check.ll 21.2 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
; RUN: opt < %s -instcombine -S | FileCheck %s

; General pattern:
;   X & Y
;
; Where Y is checking that all the high bits (covered by a mask 4294967168)
; are uniform, i.e.  %arg & 4294967168  can be either  4294967168  or  0
; Pattern can be one of:
;   %t = add        i32 %arg,    128
;   %r = icmp   ult i32 %t,      256
; Or
;   %t0 = shl       i32 %arg,    24
;   %t1 = ashr      i32 %t0,     24
;   %r  = icmp  eq  i32 %t1,     %arg
; Or
;   %t0 = trunc     i32 %arg  to i8
;   %t1 = sext      i8  %t0   to i32
;   %r  = icmp  eq  i32 %t1,     %arg
; This pattern is a signed truncation check.
;
; And X is checking that some bit in that same mask is zero.
; I.e. can be one of:
;   %r = icmp sgt i32   %arg,    -1
; Or
;   %t = and      i32   %arg,    2147483648
;   %r = icmp eq  i32   %t,      0
;
; Since we are checking that all the bits in that mask are the same,
; and a particular bit is zero, what we are really checking is that all the
; masked bits are zero.
; So this should be transformed to:
;   %r = icmp ult i32 %arg, 128

; ============================================================================ ;
; Basic positive test
; ============================================================================ ;

define i1 @positive_with_signbit(i32 %arg) {
; CHECK-LABEL: @positive_with_signbit(
; CHECK-NEXT:    [[T4_SIMPLIFIED:%.*]] = icmp ult i32 [[ARG:%.*]], 128
; CHECK-NEXT:    ret i1 [[T4_SIMPLIFIED]]
;
  %t1 = icmp sgt i32 %arg, -1
  %t2 = add i32 %arg, 128
  %t3 = icmp ult i32 %t2, 256
  %t4 = and i1 %t1, %t3
  ret i1 %t4
}

define i1 @positive_with_mask(i32 %arg) {
; CHECK-LABEL: @positive_with_mask(
; CHECK-NEXT:    [[T5_SIMPLIFIED:%.*]] = icmp ult i32 [[ARG:%.*]], 128
; CHECK-NEXT:    ret i1 [[T5_SIMPLIFIED]]
;
  %t1 = and i32 %arg, 1107296256
  %t2 = icmp eq i32 %t1, 0
  %t3 = add i32 %arg, 128
  %t4 = icmp ult i32 %t3, 256
  %t5 = and i1 %t2, %t4
  ret i1 %t5
}

define i1 @positive_with_icmp(i32 %arg) {
; CHECK-LABEL: @positive_with_icmp(
; CHECK-NEXT:    [[T4_SIMPLIFIED:%.*]] = icmp ult i32 [[ARG:%.*]], 128
; CHECK-NEXT:    ret i1 [[T4_SIMPLIFIED]]
;
  %t1 = icmp ult i32 %arg, 512
  %t2 = add i32 %arg, 128
  %t3 = icmp ult i32 %t2, 256
  %t4 = and i1 %t1, %t3
  ret i1 %t4
}

; Still the same
define i1 @positive_with_aggressive_icmp(i32 %arg) {
; CHECK-LABEL: @positive_with_aggressive_icmp(
; CHECK-NEXT:    [[T4_SIMPLIFIED:%.*]] = icmp ult i32 [[ARG:%.*]], 128
; CHECK-NEXT:    ret i1 [[T4_SIMPLIFIED]]
;
  %t1 = icmp ult i32 %arg, 128
  %t2 = add i32 %arg, 256
  %t3 = icmp ult i32 %t2, 512
  %t4 = and i1 %t1, %t3
  ret i1 %t4
}

; I'm sure there is a bunch more patterns possible :/

; This used to trigger an assert, because the icmp's are not direct
; operands of the and.
define i1 @positive_with_extra_and(i32 %arg, i1 %z) {
; CHECK-LABEL: @positive_with_extra_and(
; CHECK-NEXT:    [[T5_SIMPLIFIED:%.*]] = icmp ult i32 [[ARG:%.*]], 128
; CHECK-NEXT:    [[TMP1:%.*]] = and i1 [[T5_SIMPLIFIED]], [[Z:%.*]]
; CHECK-NEXT:    ret i1 [[TMP1]]
;
  %t1 = icmp sgt i32 %arg, -1
  %t2 = add i32 %arg, 128
  %t3 = icmp ult i32 %t2, 256
  %t4 = and i1 %t1, %z
  %t5 = and i1 %t3, %t4
  ret i1 %t5
}

; ============================================================================ ;
; Vector tests
; ============================================================================ ;

define <2 x i1> @positive_vec_splat(<2 x i32> %arg) {
; CHECK-LABEL: @positive_vec_splat(
; CHECK-NEXT:    [[T4_SIMPLIFIED:%.*]] = icmp ult <2 x i32> [[ARG:%.*]], <i32 128, i32 128>
; CHECK-NEXT:    ret <2 x i1> [[T4_SIMPLIFIED]]
;
  %t1 = icmp sgt <2 x i32> %arg, <i32 -1, i32 -1>
  %t2 = add <2 x i32> %arg, <i32 128, i32 128>
  %t3 = icmp ult <2 x i32> %t2, <i32 256, i32 256>
  %t4 = and <2 x i1> %t1, %t3
  ret <2 x i1> %t4
}

define <2 x i1> @positive_vec_nonsplat(<2 x i32> %arg) {
; CHECK-LABEL: @positive_vec_nonsplat(
; CHECK-NEXT:    [[T1:%.*]] = icmp sgt <2 x i32> [[ARG:%.*]], <i32 -1, i32 -1>
; CHECK-NEXT:    [[T2:%.*]] = add <2 x i32> [[ARG]], <i32 128, i32 256>
; CHECK-NEXT:    [[T3:%.*]] = icmp ult <2 x i32> [[T2]], <i32 256, i32 512>
; CHECK-NEXT:    [[T4:%.*]] = and <2 x i1> [[T1]], [[T3]]
; CHECK-NEXT:    ret <2 x i1> [[T4]]
;
  %t1 = icmp sgt <2 x i32> %arg, <i32 -1, i32 -1>
  %t2 = add <2 x i32> %arg, <i32 128, i32 256>
  %t3 = icmp ult <2 x i32> %t2, <i32 256, i32 512>
  %t4 = and <2 x i1> %t1, %t3
  ret <2 x i1> %t4
}

define <3 x i1> @positive_vec_undef0(<3 x i32> %arg) {
; CHECK-LABEL: @positive_vec_undef0(
; CHECK-NEXT:    [[T1:%.*]] = icmp sgt <3 x i32> [[ARG:%.*]], <i32 -1, i32 undef, i32 -1>
; CHECK-NEXT:    [[T2:%.*]] = add <3 x i32> [[ARG]], <i32 128, i32 128, i32 128>
; CHECK-NEXT:    [[T3:%.*]] = icmp ult <3 x i32> [[T2]], <i32 256, i32 256, i32 256>
; CHECK-NEXT:    [[T4:%.*]] = and <3 x i1> [[T1]], [[T3]]
; CHECK-NEXT:    ret <3 x i1> [[T4]]
;
  %t1 = icmp sgt <3 x i32> %arg, <i32 -1, i32 undef, i32 -1>
  %t2 = add <3 x i32> %arg, <i32 128, i32 128, i32 128>
  %t3 = icmp ult <3 x i32> %t2, <i32 256, i32 256, i32 256>
  %t4 = and <3 x i1> %t1, %t3
  ret <3 x i1> %t4
}

define <3 x i1> @positive_vec_undef1(<3 x i32> %arg) {
; CHECK-LABEL: @positive_vec_undef1(
; CHECK-NEXT:    [[T1:%.*]] = icmp sgt <3 x i32> [[ARG:%.*]], <i32 -1, i32 -1, i32 -1>
; CHECK-NEXT:    [[T2:%.*]] = add <3 x i32> [[ARG]], <i32 128, i32 undef, i32 128>
; CHECK-NEXT:    [[T3:%.*]] = icmp ult <3 x i32> [[T2]], <i32 256, i32 256, i32 256>
; CHECK-NEXT:    [[T4:%.*]] = and <3 x i1> [[T1]], [[T3]]
; CHECK-NEXT:    ret <3 x i1> [[T4]]
;
  %t1 = icmp sgt <3 x i32> %arg, <i32 -1, i32 -1, i32 -1>
  %t2 = add <3 x i32> %arg, <i32 128, i32 undef, i32 128>
  %t3 = icmp ult <3 x i32> %t2, <i32 256, i32 256, i32 256>
  %t4 = and <3 x i1> %t1, %t3
  ret <3 x i1> %t4
}

define <3 x i1> @positive_vec_undef2(<3 x i32> %arg) {
; CHECK-LABEL: @positive_vec_undef2(
; CHECK-NEXT:    [[T1:%.*]] = icmp sgt <3 x i32> [[ARG:%.*]], <i32 -1, i32 -1, i32 -1>
; CHECK-NEXT:    [[T2:%.*]] = add <3 x i32> [[ARG]], <i32 128, i32 128, i32 128>
; CHECK-NEXT:    [[T3:%.*]] = icmp ult <3 x i32> [[T2]], <i32 256, i32 undef, i32 256>
; CHECK-NEXT:    [[T4:%.*]] = and <3 x i1> [[T1]], [[T3]]
; CHECK-NEXT:    ret <3 x i1> [[T4]]
;
  %t1 = icmp sgt <3 x i32> %arg, <i32 -1, i32 -1, i32 -1>
  %t2 = add <3 x i32> %arg, <i32 128, i32 128, i32 128>
  %t3 = icmp ult <3 x i32> %t2, <i32 256, i32 undef, i32 256>
  %t4 = and <3 x i1> %t1, %t3
  ret <3 x i1> %t4
}

define <3 x i1> @positive_vec_undef3(<3 x i32> %arg) {
; CHECK-LABEL: @positive_vec_undef3(
; CHECK-NEXT:    [[T1:%.*]] = icmp sgt <3 x i32> [[ARG:%.*]], <i32 -1, i32 undef, i32 -1>
; CHECK-NEXT:    [[T2:%.*]] = add <3 x i32> [[ARG]], <i32 128, i32 undef, i32 128>
; CHECK-NEXT:    [[T3:%.*]] = icmp ult <3 x i32> [[T2]], <i32 256, i32 256, i32 256>
; CHECK-NEXT:    [[T4:%.*]] = and <3 x i1> [[T1]], [[T3]]
; CHECK-NEXT:    ret <3 x i1> [[T4]]
;
  %t1 = icmp sgt <3 x i32> %arg, <i32 -1, i32 undef, i32 -1>
  %t2 = add <3 x i32> %arg, <i32 128, i32 undef, i32 128>
  %t3 = icmp ult <3 x i32> %t2, <i32 256, i32 256, i32 256>
  %t4 = and <3 x i1> %t1, %t3
  ret <3 x i1> %t4
}

define <3 x i1> @positive_vec_undef4(<3 x i32> %arg) {
; CHECK-LABEL: @positive_vec_undef4(
; CHECK-NEXT:    [[T1:%.*]] = icmp sgt <3 x i32> [[ARG:%.*]], <i32 -1, i32 undef, i32 -1>
; CHECK-NEXT:    [[T2:%.*]] = add <3 x i32> [[ARG]], <i32 128, i32 128, i32 128>
; CHECK-NEXT:    [[T3:%.*]] = icmp ult <3 x i32> [[T2]], <i32 256, i32 undef, i32 256>
; CHECK-NEXT:    [[T4:%.*]] = and <3 x i1> [[T1]], [[T3]]
; CHECK-NEXT:    ret <3 x i1> [[T4]]
;
  %t1 = icmp sgt <3 x i32> %arg, <i32 -1, i32 undef, i32 -1>
  %t2 = add <3 x i32> %arg, <i32 128, i32 128, i32 128>
  %t3 = icmp ult <3 x i32> %t2, <i32 256, i32 undef, i32 256>
  %t4 = and <3 x i1> %t1, %t3
  ret <3 x i1> %t4
}

define <3 x i1> @positive_vec_undef5(<3 x i32> %arg) {
; CHECK-LABEL: @positive_vec_undef5(
; CHECK-NEXT:    [[T1:%.*]] = icmp sgt <3 x i32> [[ARG:%.*]], <i32 -1, i32 -1, i32 -1>
; CHECK-NEXT:    [[T2:%.*]] = add <3 x i32> [[ARG]], <i32 128, i32 undef, i32 128>
; CHECK-NEXT:    [[T3:%.*]] = icmp ult <3 x i32> [[T2]], <i32 256, i32 undef, i32 256>
; CHECK-NEXT:    [[T4:%.*]] = and <3 x i1> [[T1]], [[T3]]
; CHECK-NEXT:    ret <3 x i1> [[T4]]
;
  %t1 = icmp sgt <3 x i32> %arg, <i32 -1, i32 -1, i32 -1>
  %t2 = add <3 x i32> %arg, <i32 128, i32 undef, i32 128>
  %t3 = icmp ult <3 x i32> %t2, <i32 256, i32 undef, i32 256>
  %t4 = and <3 x i1> %t1, %t3
  ret <3 x i1> %t4
}

define <3 x i1> @positive_vec_undef6(<3 x i32> %arg) {
; CHECK-LABEL: @positive_vec_undef6(
; CHECK-NEXT:    [[T1:%.*]] = icmp sgt <3 x i32> [[ARG:%.*]], <i32 -1, i32 undef, i32 -1>
; CHECK-NEXT:    [[T2:%.*]] = add <3 x i32> [[ARG]], <i32 128, i32 undef, i32 128>
; CHECK-NEXT:    [[T3:%.*]] = icmp ult <3 x i32> [[T2]], <i32 256, i32 undef, i32 256>
; CHECK-NEXT:    [[T4:%.*]] = and <3 x i1> [[T1]], [[T3]]
; CHECK-NEXT:    ret <3 x i1> [[T4]]
;
  %t1 = icmp sgt <3 x i32> %arg, <i32 -1, i32 undef, i32 -1>
  %t2 = add <3 x i32> %arg, <i32 128, i32 undef, i32 128>
  %t3 = icmp ult <3 x i32> %t2, <i32 256, i32 undef, i32 256>
  %t4 = and <3 x i1> %t1, %t3
  ret <3 x i1> %t4
}

; ============================================================================ ;
; Commutativity tests.
; ============================================================================ ;

declare i32 @gen32()

define i1 @commutative() {
; CHECK-LABEL: @commutative(
; CHECK-NEXT:    [[ARG:%.*]] = call i32 @gen32()
; CHECK-NEXT:    [[T4_SIMPLIFIED:%.*]] = icmp ult i32 [[ARG]], 128
; CHECK-NEXT:    ret i1 [[T4_SIMPLIFIED]]
;
  %arg = call i32 @gen32()
  %t1 = icmp sgt i32 %arg, -1
  %t2 = add i32 %arg, 128
  %t3 = icmp ult i32 %t2, 256
  %t4 = and i1 %t3, %t1 ; swapped order
  ret i1 %t4
}

define i1 @commutative_with_icmp() {
; CHECK-LABEL: @commutative_with_icmp(
; CHECK-NEXT:    [[ARG:%.*]] = call i32 @gen32()
; CHECK-NEXT:    [[T4_SIMPLIFIED:%.*]] = icmp ult i32 [[ARG]], 128
; CHECK-NEXT:    ret i1 [[T4_SIMPLIFIED]]
;
  %arg = call i32 @gen32()
  %t1 = icmp ult i32 %arg, 512
  %t2 = add i32 %arg, 128
  %t3 = icmp ult i32 %t2, 256
  %t4 = and i1 %t3, %t1 ; swapped order
  ret i1 %t4
}

; ============================================================================ ;
; Truncations.
; ============================================================================ ;

define i1 @positive_trunc_signbit(i32 %arg) {
; CHECK-LABEL: @positive_trunc_signbit(
; CHECK-NEXT:    [[T5_SIMPLIFIED:%.*]] = icmp ult i32 [[ARG:%.*]], 128
; CHECK-NEXT:    ret i1 [[T5_SIMPLIFIED]]
;
  %t1 = trunc i32 %arg to i8
  %t2 = icmp sgt i8 %t1, -1
  %t3 = add i32 %arg, 128
  %t4 = icmp ult i32 %t3, 256
  %t5 = and i1 %t2, %t4
  ret i1 %t5
}

define i1 @positive_trunc_base(i32 %arg) {
; CHECK-LABEL: @positive_trunc_base(
; CHECK-NEXT:    [[T1:%.*]] = trunc i32 [[ARG:%.*]] to i16
; CHECK-NEXT:    [[T5_SIMPLIFIED:%.*]] = icmp ult i16 [[T1]], 128
; CHECK-NEXT:    ret i1 [[T5_SIMPLIFIED]]
;
  %t1 = trunc i32 %arg to i16
  %t2 = icmp sgt i16 %t1, -1
  %t3 = add i16 %t1, 128
  %t4 = icmp ult i16 %t3, 256
  %t5 = and i1 %t2, %t4
  ret i1 %t5
}

define i1 @positive_different_trunc_both(i32 %arg) {
; CHECK-LABEL: @positive_different_trunc_both(
; CHECK-NEXT:    [[T1:%.*]] = trunc i32 [[ARG:%.*]] to i15
; CHECK-NEXT:    [[T2:%.*]] = icmp sgt i15 [[T1]], -1
; CHECK-NEXT:    [[T3:%.*]] = trunc i32 [[ARG]] to i16
; CHECK-NEXT:    [[T4:%.*]] = add i16 [[T3]], 128
; CHECK-NEXT:    [[T5:%.*]] = icmp ult i16 [[T4]], 256
; CHECK-NEXT:    [[T6:%.*]] = and i1 [[T2]], [[T5]]
; CHECK-NEXT:    ret i1 [[T6]]
;
  %t1 = trunc i32 %arg to i15
  %t2 = icmp sgt i15 %t1, -1
  %t3 = trunc i32 %arg to i16
  %t4 = add i16 %t3, 128
  %t5 = icmp ult i16 %t4, 256
  %t6 = and i1 %t2, %t5
  ret i1 %t6
}

; ============================================================================ ;
; One-use tests.
;
; We will only produce one instruction, so we do not care about one-use.
; But, we *could* handle more patterns that we weren't able to canonicalize
; because of extra-uses.
; ============================================================================ ;

declare void @use32(i32)
declare void @use8(i8)
declare void @use1(i1)

define i1 @oneuse_with_signbit(i32 %arg) {
; CHECK-LABEL: @oneuse_with_signbit(
; CHECK-NEXT:    [[T1:%.*]] = icmp sgt i32 [[ARG:%.*]], -1
; CHECK-NEXT:    call void @use1(i1 [[T1]])
; CHECK-NEXT:    [[T2:%.*]] = add i32 [[ARG]], 128
; CHECK-NEXT:    call void @use32(i32 [[T2]])
; CHECK-NEXT:    [[T3:%.*]] = icmp ult i32 [[T2]], 256
; CHECK-NEXT:    call void @use1(i1 [[T3]])
; CHECK-NEXT:    [[T4_SIMPLIFIED:%.*]] = icmp ult i32 [[ARG]], 128
; CHECK-NEXT:    ret i1 [[T4_SIMPLIFIED]]
;
  %t1 = icmp sgt i32 %arg, -1
  call void @use1(i1 %t1)
  %t2 = add i32 %arg, 128
  call void @use32(i32 %t2)
  %t3 = icmp ult i32 %t2, 256
  call void @use1(i1 %t3)
  %t4 = and i1 %t1, %t3
  ret i1 %t4
}

define i1 @oneuse_with_mask(i32 %arg) {
; CHECK-LABEL: @oneuse_with_mask(
; CHECK-NEXT:    [[T1:%.*]] = and i32 [[ARG:%.*]], 603979776
; CHECK-NEXT:    call void @use32(i32 [[T1]])
; CHECK-NEXT:    [[T2:%.*]] = icmp eq i32 [[T1]], 0
; CHECK-NEXT:    call void @use1(i1 [[T2]])
; CHECK-NEXT:    [[T3:%.*]] = add i32 [[ARG]], 128
; CHECK-NEXT:    call void @use32(i32 [[T3]])
; CHECK-NEXT:    [[T4:%.*]] = icmp ult i32 [[T3]], 256
; CHECK-NEXT:    call void @use1(i1 [[T4]])
; CHECK-NEXT:    [[T5_SIMPLIFIED:%.*]] = icmp ult i32 [[ARG]], 128
; CHECK-NEXT:    ret i1 [[T5_SIMPLIFIED]]
;
  %t1 = and i32 %arg, 603979776 ; some bit within the target 4294967168 mask.
  call void @use32(i32 %t1)
  %t2 = icmp eq i32 %t1, 0
  call void @use1(i1 %t2)
  %t3 = add i32 %arg, 128
  call void @use32(i32 %t3)
  %t4 = icmp ult i32 %t3, 256
  call void @use1(i1 %t4)
  %t5 = and i1 %t2, %t4
  ret i1 %t5
}

define i1 @oneuse_shl_ashr(i32 %arg) {
; CHECK-LABEL: @oneuse_shl_ashr(
; CHECK-NEXT:    [[T1:%.*]] = trunc i32 [[ARG:%.*]] to i8
; CHECK-NEXT:    call void @use8(i8 [[T1]])
; CHECK-NEXT:    [[T2:%.*]] = icmp sgt i8 [[T1]], -1
; CHECK-NEXT:    call void @use1(i1 [[T2]])
; CHECK-NEXT:    [[T3:%.*]] = shl i32 [[ARG]], 24
; CHECK-NEXT:    call void @use32(i32 [[T3]])
; CHECK-NEXT:    [[T4:%.*]] = ashr exact i32 [[T3]], 24
; CHECK-NEXT:    call void @use32(i32 [[T4]])
; CHECK-NEXT:    [[T5:%.*]] = icmp eq i32 [[T4]], [[ARG]]
; CHECK-NEXT:    call void @use1(i1 [[T5]])
; CHECK-NEXT:    [[T6:%.*]] = and i1 [[T2]], [[T5]]
; CHECK-NEXT:    ret i1 [[T6]]
;
  %t1 = trunc i32 %arg to i8
  call void @use8(i8 %t1)
  %t2 = icmp sgt i8 %t1, -1
  call void @use1(i1 %t2)
  %t3 = shl i32 %arg, 24
  call void @use32(i32 %t3)
  %t4 = ashr i32 %t3, 24
  call void @use32(i32 %t4)
  %t5 = icmp eq i32 %t4, %arg
  call void @use1(i1 %t5)
  %t6 = and i1 %t2, %t5
  ret i1 %t6
}

define zeroext i1 @oneuse_trunc_sext(i32 %arg) {
; CHECK-LABEL: @oneuse_trunc_sext(
; CHECK-NEXT:    [[T1:%.*]] = trunc i32 [[ARG:%.*]] to i8
; CHECK-NEXT:    call void @use8(i8 [[T1]])
; CHECK-NEXT:    [[T2:%.*]] = icmp sgt i8 [[T1]], -1
; CHECK-NEXT:    call void @use1(i1 [[T2]])
; CHECK-NEXT:    [[T3:%.*]] = trunc i32 [[ARG]] to i8
; CHECK-NEXT:    call void @use8(i8 [[T3]])
; CHECK-NEXT:    [[T4:%.*]] = sext i8 [[T3]] to i32
; CHECK-NEXT:    call void @use32(i32 [[T4]])
; CHECK-NEXT:    [[T5:%.*]] = icmp eq i32 [[T4]], [[ARG]]
; CHECK-NEXT:    call void @use1(i1 [[T5]])
; CHECK-NEXT:    [[T6:%.*]] = and i1 [[T2]], [[T5]]
; CHECK-NEXT:    ret i1 [[T6]]
;
  %t1 = trunc i32 %arg to i8
  call void @use8(i8 %t1)
  %t2 = icmp sgt i8 %t1, -1
  call void @use1(i1 %t2)
  %t3 = trunc i32 %arg to i8
  call void @use8(i8 %t3)
  %t4 = sext i8 %t3 to i32
  call void @use32(i32 %t4)
  %t5 = icmp eq i32 %t4, %arg
  call void @use1(i1 %t5)
  %t6 = and i1 %t2, %t5
  ret i1 %t6
}

; ============================================================================ ;
; Negative tests
; ============================================================================ ;

define i1 @negative_not_arg(i32 %arg, i32 %arg2) {
; CHECK-LABEL: @negative_not_arg(
; CHECK-NEXT:    [[T1:%.*]] = icmp sgt i32 [[ARG:%.*]], -1
; CHECK-NEXT:    [[T2:%.*]] = add i32 [[ARG2:%.*]], 128
; CHECK-NEXT:    [[T3:%.*]] = icmp ult i32 [[T2]], 256
; CHECK-NEXT:    [[T4:%.*]] = and i1 [[T1]], [[T3]]
; CHECK-NEXT:    ret i1 [[T4]]
;
  %t1 = icmp sgt i32 %arg, -1
  %t2 = add i32 %arg2, 128 ; not %arg
  %t3 = icmp ult i32 %t2, 256
  %t4 = and i1 %t1, %t3
  ret i1 %t4
}

define i1 @negative_trunc_not_arg(i32 %arg, i32 %arg2) {
; CHECK-LABEL: @negative_trunc_not_arg(
; CHECK-NEXT:    [[T1:%.*]] = trunc i32 [[ARG:%.*]] to i8
; CHECK-NEXT:    [[T2:%.*]] = icmp sgt i8 [[T1]], -1
; CHECK-NEXT:    [[T3:%.*]] = add i32 [[ARG2:%.*]], 128
; CHECK-NEXT:    [[T4:%.*]] = icmp ult i32 [[T3]], 256
; CHECK-NEXT:    [[T5:%.*]] = and i1 [[T2]], [[T4]]
; CHECK-NEXT:    ret i1 [[T5]]
;
  %t1 = trunc i32 %arg to i8
  %t2 = icmp sgt i8 %t1, -1
  %t3 = add i32 %arg2, 128 ; not %arg
  %t4 = icmp ult i32 %t3, 256
  %t5 = and i1 %t2, %t4
  ret i1 %t5
}

define i1 @positive_with_mask_not_arg(i32 %arg, i32 %arg2) {
; CHECK-LABEL: @positive_with_mask_not_arg(
; CHECK-NEXT:    [[T1:%.*]] = and i32 [[ARG:%.*]], 1140850688
; CHECK-NEXT:    [[T2:%.*]] = icmp eq i32 [[T1]], 0
; CHECK-NEXT:    [[T3:%.*]] = add i32 [[ARG2:%.*]], 128
; CHECK-NEXT:    [[T4:%.*]] = icmp ult i32 [[T3]], 256
; CHECK-NEXT:    [[T5:%.*]] = and i1 [[T2]], [[T4]]
; CHECK-NEXT:    ret i1 [[T5]]
;
  %t1 = and i32 %arg, 1140850688
  %t2 = icmp eq i32 %t1, 0
  %t3 = add i32 %arg2, 128 ; not %arg
  %t4 = icmp ult i32 %t3, 256
  %t5 = and i1 %t2, %t4
  ret i1 %t5
}

define i1 @negative_with_nonuniform_bad_mask(i32 %arg) {
; CHECK-LABEL: @negative_with_nonuniform_bad_mask(
; CHECK-NEXT:    [[T1:%.*]] = and i32 [[ARG:%.*]], 1711276033
; CHECK-NEXT:    [[T2:%.*]] = icmp eq i32 [[T1]], 0
; CHECK-NEXT:    [[T3:%.*]] = add i32 [[ARG]], 128
; CHECK-NEXT:    [[T4:%.*]] = icmp ult i32 [[T3]], 256
; CHECK-NEXT:    [[T5:%.*]] = and i1 [[T2]], [[T4]]
; CHECK-NEXT:    ret i1 [[T5]]
;
  %t1 = and i32 %arg, 1711276033 ; lowest bit is set
  %t2 = icmp eq i32 %t1, 0
  %t3 = add i32 %arg, 128
  %t4 = icmp ult i32 %t3, 256
  %t5 = and i1 %t2, %t4
  ret i1 %t5
}

define i1 @negative_with_uniform_bad_mask(i32 %arg) {
; CHECK-LABEL: @negative_with_uniform_bad_mask(
; CHECK-NEXT:    [[T1:%.*]] = and i32 [[ARG:%.*]], -16777152
; CHECK-NEXT:    [[T2:%.*]] = icmp eq i32 [[T1]], 0
; CHECK-NEXT:    [[T3:%.*]] = add i32 [[ARG]], 128
; CHECK-NEXT:    [[T4:%.*]] = icmp ult i32 [[T3]], 256
; CHECK-NEXT:    [[T5:%.*]] = and i1 [[T2]], [[T4]]
; CHECK-NEXT:    ret i1 [[T5]]
;
  %t1 = and i32 %arg, 4278190144 ; 7'th bit is set
  %t2 = icmp eq i32 %t1, 0
  %t3 = add i32 %arg, 128
  %t4 = icmp ult i32 %t3, 256
  %t5 = and i1 %t2, %t4
  ret i1 %t5
}

define i1 @negative_with_wrong_mask(i32 %arg) {
; CHECK-LABEL: @negative_with_wrong_mask(
; CHECK-NEXT:    [[T1:%.*]] = and i32 [[ARG:%.*]], 1
; CHECK-NEXT:    [[T2:%.*]] = icmp eq i32 [[T1]], 0
; CHECK-NEXT:    [[T3:%.*]] = add i32 [[ARG]], 128
; CHECK-NEXT:    [[T4:%.*]] = icmp ult i32 [[T3]], 256
; CHECK-NEXT:    [[T5:%.*]] = and i1 [[T2]], [[T4]]
; CHECK-NEXT:    ret i1 [[T5]]
;
  %t1 = and i32 %arg, 1 ; not even checking the right mask
  %t2 = icmp eq i32 %t1, 0
  %t3 = add i32 %arg, 128
  %t4 = icmp ult i32 %t3, 256
  %t5 = and i1 %t2, %t4
  ret i1 %t5
}

define i1 @negative_not_less_than(i32 %arg) {
; CHECK-LABEL: @negative_not_less_than(
; CHECK-NEXT:    ret i1 false
;
  %t1 = icmp sgt i32 %arg, -1
  %t2 = add i32 %arg, 256 ; should be less than 256
  %t3 = icmp ult i32 %t2, 256
  %t4 = and i1 %t1, %t3
  ret i1 %t4
}

define i1 @negative_not_power_of_two(i32 %arg) {
; CHECK-LABEL: @negative_not_power_of_two(
; CHECK-NEXT:    [[T1:%.*]] = icmp sgt i32 [[ARG:%.*]], -1
; CHECK-NEXT:    [[T2:%.*]] = add i32 [[ARG]], 255
; CHECK-NEXT:    [[T3:%.*]] = icmp ult i32 [[T2]], 256
; CHECK-NEXT:    [[T4:%.*]] = and i1 [[T1]], [[T3]]
; CHECK-NEXT:    ret i1 [[T4]]
;
  %t1 = icmp sgt i32 %arg, -1
  %t2 = add i32 %arg, 255 ; should be power of two
  %t3 = icmp ult i32 %t2, 256
  %t4 = and i1 %t1, %t3
  ret i1 %t4
}

define i1 @negative_not_next_power_of_two(i32 %arg) {
; CHECK-LABEL: @negative_not_next_power_of_two(
; CHECK-NEXT:    [[T1:%.*]] = icmp sgt i32 [[ARG:%.*]], -1
; CHECK-NEXT:    [[T2:%.*]] = add i32 [[ARG]], 64
; CHECK-NEXT:    [[T3:%.*]] = icmp ult i32 [[T2]], 256
; CHECK-NEXT:    [[T4:%.*]] = and i1 [[T1]], [[T3]]
; CHECK-NEXT:    ret i1 [[T4]]
;
  %t1 = icmp sgt i32 %arg, -1
  %t2 = add i32 %arg, 64 ; should be 256 >> 1
  %t3 = icmp ult i32 %t2, 256
  %t4 = and i1 %t1, %t3
  ret i1 %t4
}

; I don't think this can be folded, at least not into single instruction.
define i1 @two_signed_truncation_checks(i32 %arg) {
; CHECK-LABEL: @two_signed_truncation_checks(
; CHECK-NEXT:    [[T1:%.*]] = add i32 [[ARG:%.*]], 512
; CHECK-NEXT:    [[T2:%.*]] = icmp ult i32 [[T1]], 1024
; CHECK-NEXT:    [[T3:%.*]] = add i32 [[ARG]], 128
; CHECK-NEXT:    [[T4:%.*]] = icmp ult i32 [[T3]], 256
; CHECK-NEXT:    [[T5:%.*]] = and i1 [[T2]], [[T4]]
; CHECK-NEXT:    ret i1 [[T5]]
;
  %t1 = add i32 %arg, 512
  %t2 = icmp ult i32 %t1, 1024
  %t3 = add i32 %arg, 128
  %t4 = icmp ult i32 %t3, 256
  %t5 = and i1 %t2, %t4
  ret i1 %t5
}

define i1 @bad_trunc_stc(i32 %arg) {
; CHECK-LABEL: @bad_trunc_stc(
; CHECK-NEXT:    [[T1:%.*]] = icmp sgt i32 [[ARG:%.*]], -1
; CHECK-NEXT:    [[T2:%.*]] = trunc i32 [[ARG]] to i16
; CHECK-NEXT:    [[T3:%.*]] = add i16 [[T2]], 128
; CHECK-NEXT:    [[T4:%.*]] = icmp ult i16 [[T3]], 256
; CHECK-NEXT:    [[T5:%.*]] = and i1 [[T1]], [[T4]]
; CHECK-NEXT:    ret i1 [[T5]]
;
  %t1 = icmp sgt i32 %arg, -1 ; checks a bit outside of the i16
  %t2 = trunc i32 %arg to i16
  %t3 = add i16 %t2, 128
  %t4 = icmp ult i16 %t3, 256
  %t5 = and i1 %t1, %t4
  ret i1 %t5
}