narrow-math.ll 20.8 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 622 623 624 625 626 627 628 629 630
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
; RUN: opt < %s -instcombine -S | FileCheck %s

declare i32 @callee()

declare void @use(i64)

define i64 @sext_sext_add(i32 %A) {
; CHECK-LABEL: @sext_sext_add(
; CHECK-NEXT:    [[B:%.*]] = ashr i32 [[A:%.*]], 7
; CHECK-NEXT:    [[C:%.*]] = ashr i32 [[A]], 9
; CHECK-NEXT:    [[NARROW:%.*]] = add nsw i32 [[B]], [[C]]
; CHECK-NEXT:    [[F:%.*]] = sext i32 [[NARROW]] to i64
; CHECK-NEXT:    ret i64 [[F]]
;
  %B = ashr i32 %A, 7
  %C = ashr i32 %A, 9
  %D = sext i32 %B to i64
  %E = sext i32 %C to i64
  %F = add i64 %D, %E
  ret i64 %F
}

; Negative test

define i64 @sext_zext_add_mismatched_exts(i32 %A) {
; CHECK-LABEL: @sext_zext_add_mismatched_exts(
; CHECK-NEXT:    [[B:%.*]] = ashr i32 [[A:%.*]], 7
; CHECK-NEXT:    [[C:%.*]] = lshr i32 [[A]], 9
; CHECK-NEXT:    [[D:%.*]] = sext i32 [[B]] to i64
; CHECK-NEXT:    [[E:%.*]] = zext i32 [[C]] to i64
; CHECK-NEXT:    [[F:%.*]] = add nsw i64 [[D]], [[E]]
; CHECK-NEXT:    ret i64 [[F]]
;
  %B = ashr i32 %A, 7
  %C = lshr i32 %A, 9
  %D = sext i32 %B to i64
  %E = zext i32 %C to i64
  %F = add i64 %D, %E
  ret i64 %F
}

; Negative test

define i64 @sext_sext_add_mismatched_types(i16 %A, i32 %x) {
; CHECK-LABEL: @sext_sext_add_mismatched_types(
; CHECK-NEXT:    [[B:%.*]] = ashr i16 [[A:%.*]], 7
; CHECK-NEXT:    [[C:%.*]] = ashr i32 [[X:%.*]], 9
; CHECK-NEXT:    [[D:%.*]] = sext i16 [[B]] to i64
; CHECK-NEXT:    [[E:%.*]] = sext i32 [[C]] to i64
; CHECK-NEXT:    [[F:%.*]] = add nsw i64 [[D]], [[E]]
; CHECK-NEXT:    ret i64 [[F]]
;
  %B = ashr i16 %A, 7
  %C = ashr i32 %x, 9
  %D = sext i16 %B to i64
  %E = sext i32 %C to i64
  %F = add i64 %D, %E
  ret i64 %F
}

define i64 @sext_sext_add_extra_use1(i32 %A) {
; CHECK-LABEL: @sext_sext_add_extra_use1(
; CHECK-NEXT:    [[B:%.*]] = ashr i32 [[A:%.*]], 7
; CHECK-NEXT:    [[C:%.*]] = ashr i32 [[A]], 9
; CHECK-NEXT:    [[D:%.*]] = sext i32 [[B]] to i64
; CHECK-NEXT:    call void @use(i64 [[D]])
; CHECK-NEXT:    [[NARROW:%.*]] = add nsw i32 [[B]], [[C]]
; CHECK-NEXT:    [[F:%.*]] = sext i32 [[NARROW]] to i64
; CHECK-NEXT:    ret i64 [[F]]
;
  %B = ashr i32 %A, 7
  %C = ashr i32 %A, 9
  %D = sext i32 %B to i64
  call void @use(i64 %D)
  %E = sext i32 %C to i64
  %F = add i64 %D, %E
  ret i64 %F
}

define i64 @sext_sext_add_extra_use2(i32 %A) {
; CHECK-LABEL: @sext_sext_add_extra_use2(
; CHECK-NEXT:    [[B:%.*]] = ashr i32 [[A:%.*]], 7
; CHECK-NEXT:    [[C:%.*]] = ashr i32 [[A]], 9
; CHECK-NEXT:    [[E:%.*]] = sext i32 [[C]] to i64
; CHECK-NEXT:    call void @use(i64 [[E]])
; CHECK-NEXT:    [[NARROW:%.*]] = add nsw i32 [[B]], [[C]]
; CHECK-NEXT:    [[F:%.*]] = sext i32 [[NARROW]] to i64
; CHECK-NEXT:    ret i64 [[F]]
;
  %B = ashr i32 %A, 7
  %C = ashr i32 %A, 9
  %D = sext i32 %B to i64
  %E = sext i32 %C to i64
  call void @use(i64 %E)
  %F = add i64 %D, %E
  ret i64 %F
}

; Negative test - if both extends have extra uses, we need an extra instruction.

define i64 @sext_sext_add_extra_use3(i32 %A) {
; CHECK-LABEL: @sext_sext_add_extra_use3(
; CHECK-NEXT:    [[B:%.*]] = ashr i32 [[A:%.*]], 7
; CHECK-NEXT:    [[C:%.*]] = ashr i32 [[A]], 9
; CHECK-NEXT:    [[D:%.*]] = sext i32 [[B]] to i64
; CHECK-NEXT:    call void @use(i64 [[D]])
; CHECK-NEXT:    [[E:%.*]] = sext i32 [[C]] to i64
; CHECK-NEXT:    call void @use(i64 [[E]])
; CHECK-NEXT:    [[F:%.*]] = add nsw i64 [[D]], [[E]]
; CHECK-NEXT:    ret i64 [[F]]
;
  %B = ashr i32 %A, 7
  %C = ashr i32 %A, 9
  %D = sext i32 %B to i64
  call void @use(i64 %D)
  %E = sext i32 %C to i64
  call void @use(i64 %E)
  %F = add i64 %D, %E
  ret i64 %F
}

define i64 @test1(i32 %V) {
; CHECK-LABEL: @test1(
; CHECK-NEXT:    [[CALL1:%.*]] = call i32 @callee(), !range !0
; CHECK-NEXT:    [[CALL2:%.*]] = call i32 @callee(), !range !0
; CHECK-NEXT:    [[NARROW:%.*]] = add nuw nsw i32 [[CALL1]], [[CALL2]]
; CHECK-NEXT:    [[ADD:%.*]] = zext i32 [[NARROW]] to i64
; CHECK-NEXT:    ret i64 [[ADD]]
;
  %call1 = call i32 @callee(), !range !0
  %call2 = call i32 @callee(), !range !0
  %zext1 = sext i32 %call1 to i64
  %zext2 = sext i32 %call2 to i64
  %add = add i64 %zext1, %zext2
  ret i64 %add
}

define i64 @test2(i32 %V) {
; CHECK-LABEL: @test2(
; CHECK-NEXT:    [[CALL1:%.*]] = call i32 @callee(), !range !0
; CHECK-NEXT:    [[CALL2:%.*]] = call i32 @callee(), !range !0
; CHECK-NEXT:    [[ADD:%.*]] = add nuw nsw i32 [[CALL1]], [[CALL2]]
; CHECK-NEXT:    [[TMP1:%.*]] = zext i32 [[ADD]] to i64
; CHECK-NEXT:    ret i64 [[TMP1]]
;
  %call1 = call i32 @callee(), !range !0
  %call2 = call i32 @callee(), !range !0
  %add = add i32 %call1, %call2
  %zext = sext i32 %add to i64
  ret i64 %zext
}

define i64 @test3(i32 %V) {
; CHECK-LABEL: @test3(
; CHECK-NEXT:    [[CALL1:%.*]] = call i32 @callee(), !range !0
; CHECK-NEXT:    [[CALL2:%.*]] = call i32 @callee(), !range !0
; CHECK-NEXT:    [[NARROW:%.*]] = mul nuw nsw i32 [[CALL1]], [[CALL2]]
; CHECK-NEXT:    [[ADD:%.*]] = zext i32 [[NARROW]] to i64
; CHECK-NEXT:    ret i64 [[ADD]]
;
  %call1 = call i32 @callee(), !range !0
  %call2 = call i32 @callee(), !range !0
  %zext1 = sext i32 %call1 to i64
  %zext2 = sext i32 %call2 to i64
  %add = mul i64 %zext1, %zext2
  ret i64 %add
}

define i64 @test4(i32 %V) {
; CHECK-LABEL: @test4(
; CHECK-NEXT:    [[CALL1:%.*]] = call i32 @callee(), !range !0
; CHECK-NEXT:    [[CALL2:%.*]] = call i32 @callee(), !range !0
; CHECK-NEXT:    [[ADD:%.*]] = mul nuw nsw i32 [[CALL1]], [[CALL2]]
; CHECK-NEXT:    [[TMP1:%.*]] = zext i32 [[ADD]] to i64
; CHECK-NEXT:    ret i64 [[TMP1]]
;
  %call1 = call i32 @callee(), !range !0
  %call2 = call i32 @callee(), !range !0
  %add = mul i32 %call1, %call2
  %zext = sext i32 %add to i64
  ret i64 %zext
}

define i64 @test5(i32 %V) {
; CHECK-LABEL: @test5(
; CHECK-NEXT:    [[ASHR:%.*]] = ashr i32 [[V:%.*]], 1
; CHECK-NEXT:    [[NARROW:%.*]] = add nsw i32 [[ASHR]], 1073741823
; CHECK-NEXT:    [[ADD:%.*]] = sext i32 [[NARROW]] to i64
; CHECK-NEXT:    ret i64 [[ADD]]
;
  %ashr = ashr i32 %V, 1
  %sext = sext i32 %ashr to i64
  %add = add i64 %sext, 1073741823
  ret i64 %add
}

; Negative test - extra use means we'd have more instructions than we started with.

define i64 @sext_add_constant_extra_use(i32 %V) {
; CHECK-LABEL: @sext_add_constant_extra_use(
; CHECK-NEXT:    [[ASHR:%.*]] = ashr i32 [[V:%.*]], 1
; CHECK-NEXT:    [[SEXT:%.*]] = sext i32 [[ASHR]] to i64
; CHECK-NEXT:    call void @use(i64 [[SEXT]])
; CHECK-NEXT:    [[ADD:%.*]] = add nsw i64 [[SEXT]], 1073741823
; CHECK-NEXT:    ret i64 [[ADD]]
;
  %ashr = ashr i32 %V, 1
  %sext = sext i32 %ashr to i64
  call void @use(i64 %sext)
  %add = add i64 %sext, 1073741823
  ret i64 %add
}

define <2 x i64> @test5_splat(<2 x i32> %V) {
; CHECK-LABEL: @test5_splat(
; CHECK-NEXT:    [[ASHR:%.*]] = ashr <2 x i32> [[V:%.*]], <i32 1, i32 1>
; CHECK-NEXT:    [[NARROW:%.*]] = add nsw <2 x i32> [[ASHR]], <i32 1073741823, i32 1073741823>
; CHECK-NEXT:    [[ADD:%.*]] = sext <2 x i32> [[NARROW]] to <2 x i64>
; CHECK-NEXT:    ret <2 x i64> [[ADD]]
;
  %ashr = ashr <2 x i32> %V, <i32 1, i32 1>
  %sext = sext <2 x i32> %ashr to <2 x i64>
  %add = add <2 x i64> %sext, <i64 1073741823, i64 1073741823>
  ret <2 x i64> %add
}

define <2 x i64> @test5_vec(<2 x i32> %V) {
; CHECK-LABEL: @test5_vec(
; CHECK-NEXT:    [[ASHR:%.*]] = ashr <2 x i32> [[V:%.*]], <i32 1, i32 1>
; CHECK-NEXT:    [[NARROW:%.*]] = add nsw <2 x i32> [[ASHR]], <i32 1, i32 2>
; CHECK-NEXT:    [[ADD:%.*]] = sext <2 x i32> [[NARROW]] to <2 x i64>
; CHECK-NEXT:    ret <2 x i64> [[ADD]]
;
  %ashr = ashr <2 x i32> %V, <i32 1, i32 1>
  %sext = sext <2 x i32> %ashr to <2 x i64>
  %add = add <2 x i64> %sext, <i64 1, i64 2>
  ret <2 x i64> %add
}

define i64 @test6(i32 %V) {
; CHECK-LABEL: @test6(
; CHECK-NEXT:    [[ASHR:%.*]] = ashr i32 [[V:%.*]], 1
; CHECK-NEXT:    [[NARROW:%.*]] = add nsw i32 [[ASHR]], -1073741824
; CHECK-NEXT:    [[ADD:%.*]] = sext i32 [[NARROW]] to i64
; CHECK-NEXT:    ret i64 [[ADD]]
;
  %ashr = ashr i32 %V, 1
  %sext = sext i32 %ashr to i64
  %add = add i64 %sext, -1073741824
  ret i64 %add
}

define <2 x i64> @test6_splat(<2 x i32> %V) {
; CHECK-LABEL: @test6_splat(
; CHECK-NEXT:    [[ASHR:%.*]] = ashr <2 x i32> [[V:%.*]], <i32 1, i32 1>
; CHECK-NEXT:    [[NARROW:%.*]] = add nsw <2 x i32> [[ASHR]], <i32 -1073741824, i32 -1073741824>
; CHECK-NEXT:    [[ADD:%.*]] = sext <2 x i32> [[NARROW]] to <2 x i64>
; CHECK-NEXT:    ret <2 x i64> [[ADD]]
;
  %ashr = ashr <2 x i32> %V, <i32 1, i32 1>
  %sext = sext <2 x i32> %ashr to <2 x i64>
  %add = add <2 x i64> %sext, <i64 -1073741824, i64 -1073741824>
  ret <2 x i64> %add
}

define <2 x i64> @test6_vec(<2 x i32> %V) {
; CHECK-LABEL: @test6_vec(
; CHECK-NEXT:    [[ASHR:%.*]] = ashr <2 x i32> [[V:%.*]], <i32 1, i32 1>
; CHECK-NEXT:    [[NARROW:%.*]] = add nsw <2 x i32> [[ASHR]], <i32 -1, i32 -2>
; CHECK-NEXT:    [[ADD:%.*]] = sext <2 x i32> [[NARROW]] to <2 x i64>
; CHECK-NEXT:    ret <2 x i64> [[ADD]]
;
  %ashr = ashr <2 x i32> %V, <i32 1, i32 1>
  %sext = sext <2 x i32> %ashr to <2 x i64>
  %add = add <2 x i64> %sext, <i64 -1, i64 -2>
  ret <2 x i64> %add
}

define <2 x i64> @test6_vec2(<2 x i32> %V) {
; CHECK-LABEL: @test6_vec2(
; CHECK-NEXT:    [[ASHR:%.*]] = ashr <2 x i32> [[V:%.*]], <i32 1, i32 1>
; CHECK-NEXT:    [[NARROW:%.*]] = add nsw <2 x i32> [[ASHR]], <i32 -1, i32 1>
; CHECK-NEXT:    [[ADD:%.*]] = sext <2 x i32> [[NARROW]] to <2 x i64>
; CHECK-NEXT:    ret <2 x i64> [[ADD]]
;
  %ashr = ashr <2 x i32> %V, <i32 1, i32 1>
  %sext = sext <2 x i32> %ashr to <2 x i64>
  %add = add <2 x i64> %sext, <i64 -1, i64 1>
  ret <2 x i64> %add
}

define i64 @test7(i32 %V) {
; CHECK-LABEL: @test7(
; CHECK-NEXT:    [[LSHR:%.*]] = lshr i32 [[V:%.*]], 1
; CHECK-NEXT:    [[NARROW:%.*]] = add nuw i32 [[LSHR]], 2147483647
; CHECK-NEXT:    [[ADD:%.*]] = zext i32 [[NARROW]] to i64
; CHECK-NEXT:    ret i64 [[ADD]]
;
  %lshr = lshr i32 %V, 1
  %zext = zext i32 %lshr to i64
  %add = add i64 %zext, 2147483647
  ret i64 %add
}

define <2 x i64> @test7_splat(<2 x i32> %V) {
; CHECK-LABEL: @test7_splat(
; CHECK-NEXT:    [[LSHR:%.*]] = lshr <2 x i32> [[V:%.*]], <i32 1, i32 1>
; CHECK-NEXT:    [[NARROW:%.*]] = add nuw <2 x i32> [[LSHR]], <i32 2147483647, i32 2147483647>
; CHECK-NEXT:    [[ADD:%.*]] = zext <2 x i32> [[NARROW]] to <2 x i64>
; CHECK-NEXT:    ret <2 x i64> [[ADD]]
;
  %lshr = lshr <2 x i32> %V, <i32 1, i32 1>
  %zext = zext <2 x i32> %lshr to <2 x i64>
  %add = add <2 x i64> %zext, <i64 2147483647, i64 2147483647>
  ret <2 x i64> %add
}

define <2 x i64> @test7_vec(<2 x i32> %V) {
; CHECK-LABEL: @test7_vec(
; CHECK-NEXT:    [[LSHR:%.*]] = lshr <2 x i32> [[V:%.*]], <i32 1, i32 1>
; CHECK-NEXT:    [[NARROW:%.*]] = add nuw <2 x i32> [[LSHR]], <i32 1, i32 2>
; CHECK-NEXT:    [[ADD:%.*]] = zext <2 x i32> [[NARROW]] to <2 x i64>
; CHECK-NEXT:    ret <2 x i64> [[ADD]]
;
  %lshr = lshr <2 x i32> %V, <i32 1, i32 1>
  %zext = zext <2 x i32> %lshr to <2 x i64>
  %add = add <2 x i64> %zext, <i64 1, i64 2>
  ret <2 x i64> %add
}

define i64 @test8(i32 %V) {
; CHECK-LABEL: @test8(
; CHECK-NEXT:    [[ASHR:%.*]] = ashr i32 [[V:%.*]], 16
; CHECK-NEXT:    [[NARROW:%.*]] = mul nsw i32 [[ASHR]], 32767
; CHECK-NEXT:    [[MUL:%.*]] = sext i32 [[NARROW]] to i64
; CHECK-NEXT:    ret i64 [[MUL]]
;
  %ashr = ashr i32 %V, 16
  %sext = sext i32 %ashr to i64
  %mul = mul i64 %sext, 32767
  ret i64 %mul
}

define <2 x i64> @test8_splat(<2 x i32> %V) {
; CHECK-LABEL: @test8_splat(
; CHECK-NEXT:    [[ASHR:%.*]] = ashr <2 x i32> [[V:%.*]], <i32 16, i32 16>
; CHECK-NEXT:    [[NARROW:%.*]] = mul nsw <2 x i32> [[ASHR]], <i32 32767, i32 32767>
; CHECK-NEXT:    [[MUL:%.*]] = sext <2 x i32> [[NARROW]] to <2 x i64>
; CHECK-NEXT:    ret <2 x i64> [[MUL]]
;
  %ashr = ashr <2 x i32> %V, <i32 16, i32 16>
  %sext = sext <2 x i32> %ashr to <2 x i64>
  %mul = mul <2 x i64> %sext, <i64 32767, i64 32767>
  ret <2 x i64> %mul
}

define <2 x i64> @test8_vec(<2 x i32> %V) {
; CHECK-LABEL: @test8_vec(
; CHECK-NEXT:    [[ASHR:%.*]] = ashr <2 x i32> [[V:%.*]], <i32 16, i32 16>
; CHECK-NEXT:    [[NARROW:%.*]] = mul nsw <2 x i32> [[ASHR]], <i32 32767, i32 16384>
; CHECK-NEXT:    [[MUL:%.*]] = sext <2 x i32> [[NARROW]] to <2 x i64>
; CHECK-NEXT:    ret <2 x i64> [[MUL]]
;
  %ashr = ashr <2 x i32> %V, <i32 16, i32 16>
  %sext = sext <2 x i32> %ashr to <2 x i64>
  %mul = mul <2 x i64> %sext, <i64 32767, i64 16384>
  ret <2 x i64> %mul
}

define <2 x i64> @test8_vec2(<2 x i32> %V) {
; CHECK-LABEL: @test8_vec2(
; CHECK-NEXT:    [[ASHR:%.*]] = ashr <2 x i32> [[V:%.*]], <i32 16, i32 16>
; CHECK-NEXT:    [[NARROW:%.*]] = mul nsw <2 x i32> [[ASHR]], <i32 32767, i32 -32767>
; CHECK-NEXT:    [[MUL:%.*]] = sext <2 x i32> [[NARROW]] to <2 x i64>
; CHECK-NEXT:    ret <2 x i64> [[MUL]]
;
  %ashr = ashr <2 x i32> %V, <i32 16, i32 16>
  %sext = sext <2 x i32> %ashr to <2 x i64>
  %mul = mul <2 x i64> %sext, <i64 32767, i64 -32767>
  ret <2 x i64> %mul
}

define i64 @test9(i32 %V) {
; CHECK-LABEL: @test9(
; CHECK-NEXT:    [[ASHR:%.*]] = ashr i32 [[V:%.*]], 16
; CHECK-NEXT:    [[NARROW:%.*]] = mul nsw i32 [[ASHR]], -32767
; CHECK-NEXT:    [[MUL:%.*]] = sext i32 [[NARROW]] to i64
; CHECK-NEXT:    ret i64 [[MUL]]
;
  %ashr = ashr i32 %V, 16
  %sext = sext i32 %ashr to i64
  %mul = mul i64 %sext, -32767
  ret i64 %mul
}

define <2 x i64> @test9_splat(<2 x i32> %V) {
; CHECK-LABEL: @test9_splat(
; CHECK-NEXT:    [[ASHR:%.*]] = ashr <2 x i32> [[V:%.*]], <i32 16, i32 16>
; CHECK-NEXT:    [[NARROW:%.*]] = mul nsw <2 x i32> [[ASHR]], <i32 -32767, i32 -32767>
; CHECK-NEXT:    [[MUL:%.*]] = sext <2 x i32> [[NARROW]] to <2 x i64>
; CHECK-NEXT:    ret <2 x i64> [[MUL]]
;
  %ashr = ashr <2 x i32> %V, <i32 16, i32 16>
  %sext = sext <2 x i32> %ashr to <2 x i64>
  %mul = mul <2 x i64> %sext, <i64 -32767, i64 -32767>
  ret <2 x i64> %mul
}

define <2 x i64> @test9_vec(<2 x i32> %V) {
; CHECK-LABEL: @test9_vec(
; CHECK-NEXT:    [[ASHR:%.*]] = ashr <2 x i32> [[V:%.*]], <i32 16, i32 16>
; CHECK-NEXT:    [[NARROW:%.*]] = mul nsw <2 x i32> [[ASHR]], <i32 -32767, i32 -10>
; CHECK-NEXT:    [[MUL:%.*]] = sext <2 x i32> [[NARROW]] to <2 x i64>
; CHECK-NEXT:    ret <2 x i64> [[MUL]]
;
  %ashr = ashr <2 x i32> %V, <i32 16, i32 16>
  %sext = sext <2 x i32> %ashr to <2 x i64>
  %mul = mul <2 x i64> %sext, <i64 -32767, i64 -10>
  ret <2 x i64> %mul
}

define i64 @test10(i32 %V) {
; CHECK-LABEL: @test10(
; CHECK-NEXT:    [[LSHR:%.*]] = lshr i32 [[V:%.*]], 16
; CHECK-NEXT:    [[NARROW:%.*]] = mul nuw i32 [[LSHR]], 65535
; CHECK-NEXT:    [[MUL:%.*]] = zext i32 [[NARROW]] to i64
; CHECK-NEXT:    ret i64 [[MUL]]
;
  %lshr = lshr i32 %V, 16
  %zext = zext i32 %lshr to i64
  %mul = mul i64 %zext, 65535
  ret i64 %mul
}

define <2 x i64> @test10_splat(<2 x i32> %V) {
; CHECK-LABEL: @test10_splat(
; CHECK-NEXT:    [[LSHR:%.*]] = lshr <2 x i32> [[V:%.*]], <i32 16, i32 16>
; CHECK-NEXT:    [[NARROW:%.*]] = mul nuw <2 x i32> [[LSHR]], <i32 65535, i32 65535>
; CHECK-NEXT:    [[MUL:%.*]] = zext <2 x i32> [[NARROW]] to <2 x i64>
; CHECK-NEXT:    ret <2 x i64> [[MUL]]
;
  %lshr = lshr <2 x i32> %V, <i32 16, i32 16>
  %zext = zext <2 x i32> %lshr to <2 x i64>
  %mul = mul <2 x i64> %zext, <i64 65535, i64 65535>
  ret <2 x i64> %mul
}

define <2 x i64> @test10_vec(<2 x i32> %V) {
; CHECK-LABEL: @test10_vec(
; CHECK-NEXT:    [[LSHR:%.*]] = lshr <2 x i32> [[V:%.*]], <i32 16, i32 16>
; CHECK-NEXT:    [[NARROW:%.*]] = mul nuw <2 x i32> [[LSHR]], <i32 65535, i32 2>
; CHECK-NEXT:    [[MUL:%.*]] = zext <2 x i32> [[NARROW]] to <2 x i64>
; CHECK-NEXT:    ret <2 x i64> [[MUL]]
;
  %lshr = lshr <2 x i32> %V, <i32 16, i32 16>
  %zext = zext <2 x i32> %lshr to <2 x i64>
  %mul = mul <2 x i64> %zext, <i64 65535, i64 2>
  ret <2 x i64> %mul
}

define i64 @test11(i32 %V) {
; CHECK-LABEL: @test11(
; CHECK-NEXT:    [[CALL1:%.*]] = call i32 @callee(), !range !1
; CHECK-NEXT:    [[CALL2:%.*]] = call i32 @callee(), !range !1
; CHECK-NEXT:    [[NARROW:%.*]] = add nsw i32 [[CALL1]], [[CALL2]]
; CHECK-NEXT:    [[ADD:%.*]] = sext i32 [[NARROW]] to i64
; CHECK-NEXT:    ret i64 [[ADD]]
;
  %call1 = call i32 @callee(), !range !1
  %call2 = call i32 @callee(), !range !1
  %sext1 = sext i32 %call1 to i64
  %sext2 = sext i32 %call2 to i64
  %add = add i64 %sext1, %sext2
  ret i64 %add
}

define i64 @test12(i32 %V) {
; CHECK-LABEL: @test12(
; CHECK-NEXT:    [[CALL1:%.*]] = call i32 @callee(), !range !1
; CHECK-NEXT:    [[CALL2:%.*]] = call i32 @callee(), !range !1
; CHECK-NEXT:    [[NARROW:%.*]] = mul nsw i32 [[CALL1]], [[CALL2]]
; CHECK-NEXT:    [[TMP1:%.*]] = zext i32 [[NARROW]] to i64
; CHECK-NEXT:    ret i64 [[TMP1]]
;
  %call1 = call i32 @callee(), !range !1
  %call2 = call i32 @callee(), !range !1
  %sext1 = sext i32 %call1 to i64
  %sext2 = sext i32 %call2 to i64
  %add = mul i64 %sext1, %sext2
  ret i64 %add
}

define i64 @test13(i32 %V) {
; CHECK-LABEL: @test13(
; CHECK-NEXT:    [[CALL1:%.*]] = call i32 @callee(), !range !2
; CHECK-NEXT:    [[CALL2:%.*]] = call i32 @callee(), !range !3
; CHECK-NEXT:    [[SUBCONV:%.*]] = sub nsw i32 [[CALL1]], [[CALL2]]
; CHECK-NEXT:    [[SUB:%.*]] = sext i32 [[SUBCONV]] to i64
; CHECK-NEXT:    ret i64 [[SUB]]
;
  %call1 = call i32 @callee(), !range !2
  %call2 = call i32 @callee(), !range !3
  %sext1 = sext i32 %call1 to i64
  %sext2 = sext i32 %call2 to i64
  %sub = sub i64 %sext1, %sext2
  ret i64 %sub
}

define i64 @test14(i32 %V) {
; CHECK-LABEL: @test14(
; CHECK-NEXT:    [[CALL1:%.*]] = call i32 @callee(), !range !2
; CHECK-NEXT:    [[CALL2:%.*]] = call i32 @callee(), !range !0
; CHECK-NEXT:    [[SUBCONV:%.*]] = sub nuw nsw i32 [[CALL1]], [[CALL2]]
; CHECK-NEXT:    [[SUB:%.*]] = zext i32 [[SUBCONV]] to i64
; CHECK-NEXT:    ret i64 [[SUB]]
;
  %call1 = call i32 @callee(), !range !2
  %call2 = call i32 @callee(), !range !0
  %zext1 = zext i32 %call1 to i64
  %zext2 = zext i32 %call2 to i64
  %sub = sub i64 %zext1, %zext2
  ret i64 %sub
}

define i64 @test15(i32 %V) {
; CHECK-LABEL: @test15(
; CHECK-NEXT:    [[ASHR:%.*]] = ashr i32 [[V:%.*]], 1
; CHECK-NEXT:    [[SUBCONV:%.*]] = sub nsw i32 8, [[ASHR]]
; CHECK-NEXT:    [[SUB:%.*]] = sext i32 [[SUBCONV]] to i64
; CHECK-NEXT:    ret i64 [[SUB]]
;
  %ashr = ashr i32 %V, 1
  %sext = sext i32 %ashr to i64
  %sub = sub i64 8, %sext
  ret i64 %sub
}

define <2 x i64> @test15vec(<2 x i32> %V) {
; CHECK-LABEL: @test15vec(
; CHECK-NEXT:    [[ASHR:%.*]] = ashr <2 x i32> [[V:%.*]], <i32 1, i32 1>
; CHECK-NEXT:    [[SUBCONV:%.*]] = sub nsw <2 x i32> <i32 8, i32 8>, [[ASHR]]
; CHECK-NEXT:    [[SUB:%.*]] = sext <2 x i32> [[SUBCONV]] to <2 x i64>
; CHECK-NEXT:    ret <2 x i64> [[SUB]]
;
  %ashr = ashr <2 x i32> %V, <i32 1, i32 1>
  %sext = sext <2 x i32> %ashr to <2 x i64>
  %sub = sub <2 x i64> <i64 8, i64 8>, %sext
  ret <2 x i64> %sub
}

define i64 @test16(i32 %V) {
; CHECK-LABEL: @test16(
; CHECK-NEXT:    [[LSHR:%.*]] = lshr i32 [[V:%.*]], 1
; CHECK-NEXT:    [[SUBCONV:%.*]] = sub nuw i32 -2, [[LSHR]]
; CHECK-NEXT:    [[SUB:%.*]] = zext i32 [[SUBCONV]] to i64
; CHECK-NEXT:    ret i64 [[SUB]]
;
  %lshr = lshr i32 %V, 1
  %zext = zext i32 %lshr to i64
  %sub = sub i64 4294967294, %zext
  ret i64 %sub
}

define <2 x i64> @test16vec(<2 x i32> %V) {
; CHECK-LABEL: @test16vec(
; CHECK-NEXT:    [[LSHR:%.*]] = lshr <2 x i32> [[V:%.*]], <i32 1, i32 1>
; CHECK-NEXT:    [[SUBCONV:%.*]] = sub nuw <2 x i32> <i32 -2, i32 -2>, [[LSHR]]
; CHECK-NEXT:    [[SUB:%.*]] = zext <2 x i32> [[SUBCONV]] to <2 x i64>
; CHECK-NEXT:    ret <2 x i64> [[SUB]]
;
  %lshr = lshr <2 x i32> %V, <i32 1, i32 1>
  %zext = zext <2 x i32> %lshr to <2 x i64>
  %sub = sub <2 x i64> <i64 4294967294, i64 4294967294>, %zext
  ret <2 x i64> %sub
}

; Negative test. Both have the same range so we can't guarantee the subtract
; won't wrap.
define i64 @test17(i32 %V) {
; CHECK-LABEL: @test17(
; CHECK-NEXT:    [[CALL1:%.*]] = call i32 @callee(), !range !0
; CHECK-NEXT:    [[CALL2:%.*]] = call i32 @callee(), !range !0
; CHECK-NEXT:    [[SEXT1:%.*]] = zext i32 [[CALL1]] to i64
; CHECK-NEXT:    [[SEXT2:%.*]] = zext i32 [[CALL2]] to i64
; CHECK-NEXT:    [[SUB:%.*]] = sub nsw i64 [[SEXT1]], [[SEXT2]]
; CHECK-NEXT:    ret i64 [[SUB]]
;
  %call1 = call i32 @callee(), !range !0
  %call2 = call i32 @callee(), !range !0
  %sext1 = zext i32 %call1 to i64
  %sext2 = zext i32 %call2 to i64
  %sub = sub i64 %sext1, %sext2
  ret i64 %sub
}

; Negative test. LHS is large positive 32-bit number. Range of callee can
; cause overflow.
define i64 @test18(i32 %V) {
; CHECK-LABEL: @test18(
; CHECK-NEXT:    [[CALL1:%.*]] = call i32 @callee(), !range !1
; CHECK-NEXT:    [[SEXT1:%.*]] = sext i32 [[CALL1]] to i64
; CHECK-NEXT:    [[SUB:%.*]] = sub nsw i64 2147481648, [[SEXT1]]
; CHECK-NEXT:    ret i64 [[SUB]]
;
  %call1 = call i32 @callee(), !range !1
  %sext1 = sext i32 %call1 to i64
  %sub = sub i64 2147481648, %sext1
  ret i64 %sub
}

; Negative test. LHS is large negative 32-bit number. Range of callee can
; cause overflow.
define i64 @test19(i32 %V) {
; CHECK-LABEL: @test19(
; CHECK-NEXT:    [[CALL1:%.*]] = call i32 @callee(), !range !0
; CHECK-NEXT:    [[TMP1:%.*]] = zext i32 [[CALL1]] to i64
; CHECK-NEXT:    [[SUB:%.*]] = sub nuw nsw i64 -2147481648, [[TMP1]]
; CHECK-NEXT:    ret i64 [[SUB]]
;
  %call1 = call i32 @callee(), !range !0
  %sext1 = sext i32 %call1 to i64
  %sub = sub i64 -2147481648, %sext1
  ret i64 %sub
}

!0 = !{ i32 0, i32 2000 }
!1 = !{ i32 -2000, i32 0 }
!2 = !{ i32 -512, i32 -255 }
!3 = !{ i32 -128, i32 0 }