1 ; This test makes sure that these instructions are properly eliminated. 2 ; 3 ; RUN: opt < %s -instcombine -S | FileCheck %s 4 5 target datalayout = "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" 6 7 define i32 @test1(i32 %A, i1 %b) { 8 BB0: 9 br i1 %b, label %BB1, label %BB2 10 11 BB1: 12 ; Combine away one argument PHI nodes 13 %B = phi i32 [ %A, %BB0 ] 14 ret i32 %B 15 16 BB2: 17 ret i32 %A 18 ; CHECK-LABEL: @test1( 19 ; CHECK: BB1: 20 ; CHECK-NEXT: ret i32 %A 21 } 22 23 define i32 @test2(i32 %A, i1 %b) { 24 BB0: 25 br i1 %b, label %BB1, label %BB2 26 27 BB1: 28 br label %BB2 29 30 BB2: 31 ; Combine away PHI nodes with same values 32 %B = phi i32 [ %A, %BB0 ], [ %A, %BB1 ] 33 ret i32 %B 34 ; CHECK-LABEL: @test2( 35 ; CHECK: BB2: 36 ; CHECK-NEXT: ret i32 %A 37 } 38 39 define i32 @test3(i32 %A, i1 %b) { 40 BB0: 41 br label %Loop 42 43 Loop: 44 ; PHI has same value always. 45 %B = phi i32 [ %A, %BB0 ], [ %B, %Loop ] 46 br i1 %b, label %Loop, label %Exit 47 48 Exit: 49 ret i32 %B 50 ; CHECK-LABEL: @test3( 51 ; CHECK: Exit: 52 ; CHECK-NEXT: ret i32 %A 53 } 54 55 define i32 @test4(i1 %b) { 56 BB0: 57 ; Loop is unreachable 58 ret i32 7 59 60 Loop: ; preds = %L2, %Loop 61 ; PHI has same value always. 62 %B = phi i32 [ %B, %L2 ], [ %B, %Loop ] 63 br i1 %b, label %L2, label %Loop 64 65 L2: ; preds = %Loop 66 br label %Loop 67 ; CHECK-LABEL: @test4( 68 ; CHECK: Loop: 69 ; CHECK-NEXT: br i1 %b 70 } 71 72 define i32 @test5(i32 %A, i1 %b) { 73 BB0: 74 br label %Loop 75 76 Loop: ; preds = %Loop, %BB0 77 ; PHI has same value always. 78 %B = phi i32 [ %A, %BB0 ], [ undef, %Loop ] 79 br i1 %b, label %Loop, label %Exit 80 81 Exit: ; preds = %Loop 82 ret i32 %B 83 ; CHECK-LABEL: @test5( 84 ; CHECK: Loop: 85 ; CHECK-NEXT: br i1 %b 86 ; CHECK: Exit: 87 ; CHECK-NEXT: ret i32 %A 88 } 89 90 define i32 @test6(i16 %A, i1 %b) { 91 BB0: 92 %X = zext i16 %A to i32 93 br i1 %b, label %BB1, label %BB2 94 95 BB1: 96 %Y = zext i16 %A to i32 97 br label %BB2 98 99 BB2: 100 ;; Suck casts into phi 101 %B = phi i32 [ %X, %BB0 ], [ %Y, %BB1 ] 102 ret i32 %B 103 ; CHECK-LABEL: @test6( 104 ; CHECK: BB2: 105 ; CHECK: zext i16 %A to i32 106 ; CHECK-NEXT: ret i32 107 } 108 109 define i32 @test7(i32 %A, i1 %b) { 110 BB0: 111 br label %Loop 112 113 Loop: ; preds = %Loop, %BB0 114 ; PHI is dead. 115 %B = phi i32 [ %A, %BB0 ], [ %C, %Loop ] 116 %C = add i32 %B, 123 117 br i1 %b, label %Loop, label %Exit 118 119 Exit: ; preds = %Loop 120 ret i32 0 121 ; CHECK-LABEL: @test7( 122 ; CHECK: Loop: 123 ; CHECK-NEXT: br i1 %b 124 } 125 126 define i32* @test8({ i32, i32 } *%A, i1 %b) { 127 BB0: 128 %X = getelementptr inbounds { i32, i32 } *%A, i32 0, i32 1 129 br i1 %b, label %BB1, label %BB2 130 131 BB1: 132 %Y = getelementptr { i32, i32 } *%A, i32 0, i32 1 133 br label %BB2 134 135 BB2: 136 ;; Suck GEPs into phi 137 %B = phi i32* [ %X, %BB0 ], [ %Y, %BB1 ] 138 ret i32* %B 139 ; CHECK-LABEL: @test8( 140 ; CHECK-NOT: phi 141 ; CHECK: BB2: 142 ; CHECK-NEXT: %B = getelementptr { i32, i32 }* %A 143 ; CHECK-NEXT: ret i32* %B 144 } 145 146 define i32 @test9(i32* %A, i32* %B) { 147 entry: 148 %c = icmp eq i32* %A, null 149 br i1 %c, label %bb1, label %bb 150 151 bb: 152 %C = load i32* %B, align 1 153 br label %bb2 154 155 bb1: 156 %D = load i32* %A, align 1 157 br label %bb2 158 159 bb2: 160 %E = phi i32 [ %C, %bb ], [ %D, %bb1 ] 161 ret i32 %E 162 ; CHECK-LABEL: @test9( 163 ; CHECK: bb2: 164 ; CHECK-NEXT: phi i32* [ %B, %bb ], [ %A, %bb1 ] 165 ; CHECK-NEXT: %E = load i32* %{{[^,]*}}, align 1 166 ; CHECK-NEXT: ret i32 %E 167 168 } 169 170 define i32 @test10(i32* %A, i32* %B) { 171 entry: 172 %c = icmp eq i32* %A, null 173 br i1 %c, label %bb1, label %bb 174 175 bb: 176 %C = load i32* %B, align 16 177 br label %bb2 178 179 bb1: 180 %D = load i32* %A, align 32 181 br label %bb2 182 183 bb2: 184 %E = phi i32 [ %C, %bb ], [ %D, %bb1 ] 185 ret i32 %E 186 ; CHECK-LABEL: @test10( 187 ; CHECK: bb2: 188 ; CHECK-NEXT: phi i32* [ %B, %bb ], [ %A, %bb1 ] 189 ; CHECK-NEXT: %E = load i32* %{{[^,]*}}, align 16 190 ; CHECK-NEXT: ret i32 %E 191 } 192 193 194 ; PR1777 195 declare i1 @test11a() 196 197 define i1 @test11() { 198 entry: 199 %a = alloca i32 200 %i = ptrtoint i32* %a to i64 201 %b = call i1 @test11a() 202 br i1 %b, label %one, label %two 203 204 one: 205 %x = phi i64 [%i, %entry], [%y, %two] 206 %c = call i1 @test11a() 207 br i1 %c, label %two, label %end 208 209 two: 210 %y = phi i64 [%i, %entry], [%x, %one] 211 %d = call i1 @test11a() 212 br i1 %d, label %one, label %end 213 214 end: 215 %f = phi i64 [ %x, %one], [%y, %two] 216 ; Change the %f to %i, and the optimizer suddenly becomes a lot smarter 217 ; even though %f must equal %i at this point 218 %g = inttoptr i64 %f to i32* 219 store i32 10, i32* %g 220 %z = call i1 @test11a() 221 ret i1 %z 222 ; CHECK-LABEL: @test11( 223 ; CHECK-NOT: phi i32 224 ; CHECK: ret i1 %z 225 } 226 227 228 define i64 @test12(i1 %cond, i8* %Ptr, i64 %Val) { 229 entry: 230 %tmp41 = ptrtoint i8* %Ptr to i64 231 %tmp42 = zext i64 %tmp41 to i128 232 br i1 %cond, label %end, label %two 233 234 two: 235 %tmp36 = zext i64 %Val to i128 ; <i128> [#uses=1] 236 %tmp37 = shl i128 %tmp36, 64 ; <i128> [#uses=1] 237 %ins39 = or i128 %tmp42, %tmp37 ; <i128> [#uses=1] 238 br label %end 239 240 end: 241 %tmp869.0 = phi i128 [ %tmp42, %entry ], [ %ins39, %two ] 242 %tmp32 = trunc i128 %tmp869.0 to i64 ; <i64> [#uses=1] 243 %tmp29 = lshr i128 %tmp869.0, 64 ; <i128> [#uses=1] 244 %tmp30 = trunc i128 %tmp29 to i64 ; <i64> [#uses=1] 245 246 %tmp2 = add i64 %tmp32, %tmp30 247 ret i64 %tmp2 248 ; CHECK-LABEL: @test12( 249 ; CHECK-NOT: zext 250 ; CHECK: end: 251 ; CHECK-NEXT: phi i64 [ 0, %entry ], [ %Val, %two ] 252 ; CHECK-NOT: phi 253 ; CHECK: ret i64 254 } 255 256 declare void @test13f(double, i32) 257 258 define void @test13(i1 %cond, i32 %V1, double %Vald) { 259 entry: 260 %tmp42 = zext i32 %V1 to i128 261 br i1 %cond, label %end, label %two 262 263 two: 264 %Val = bitcast double %Vald to i64 265 %tmp36 = zext i64 %Val to i128 ; <i128> [#uses=1] 266 %tmp37 = shl i128 %tmp36, 64 ; <i128> [#uses=1] 267 %ins39 = or i128 %tmp42, %tmp37 ; <i128> [#uses=1] 268 br label %end 269 270 end: 271 %tmp869.0 = phi i128 [ %tmp42, %entry ], [ %ins39, %two ] 272 %tmp32 = trunc i128 %tmp869.0 to i32 273 %tmp29 = lshr i128 %tmp869.0, 64 ; <i128> [#uses=1] 274 %tmp30 = trunc i128 %tmp29 to i64 ; <i64> [#uses=1] 275 %tmp31 = bitcast i64 %tmp30 to double 276 277 call void @test13f(double %tmp31, i32 %tmp32) 278 ret void 279 ; CHECK-LABEL: @test13( 280 ; CHECK-NOT: zext 281 ; CHECK: end: 282 ; CHECK-NEXT: phi double [ 0.000000e+00, %entry ], [ %Vald, %two ] 283 ; CHECK-NEXT: call void @test13f(double {{[^,]*}}, i32 %V1) 284 ; CHECK: ret void 285 } 286 287 define i640 @test14a(i320 %A, i320 %B, i1 %b1) { 288 BB0: 289 %a = zext i320 %A to i640 290 %b = zext i320 %B to i640 291 br label %Loop 292 293 Loop: 294 %C = phi i640 [ %a, %BB0 ], [ %b, %Loop ] 295 br i1 %b1, label %Loop, label %Exit 296 297 Exit: ; preds = %Loop 298 ret i640 %C 299 ; CHECK-LABEL: @test14a( 300 ; CHECK: Loop: 301 ; CHECK-NEXT: phi i320 302 } 303 304 define i160 @test14b(i320 %A, i320 %B, i1 %b1) { 305 BB0: 306 %a = trunc i320 %A to i160 307 %b = trunc i320 %B to i160 308 br label %Loop 309 310 Loop: 311 %C = phi i160 [ %a, %BB0 ], [ %b, %Loop ] 312 br i1 %b1, label %Loop, label %Exit 313 314 Exit: ; preds = %Loop 315 ret i160 %C 316 ; CHECK-LABEL: @test14b( 317 ; CHECK: Loop: 318 ; CHECK-NEXT: phi i160 319 } 320 321 declare i64 @test15a(i64) 322 323 define i64 @test15b(i64 %A, i1 %b) { 324 ; CHECK-LABEL: @test15b( 325 entry: 326 %i0 = zext i64 %A to i128 327 %i1 = shl i128 %i0, 64 328 %i = or i128 %i1, %i0 329 br i1 %b, label %one, label %two 330 ; CHECK: entry: 331 ; CHECK-NEXT: br i1 %b 332 333 one: 334 %x = phi i128 [%i, %entry], [%y, %two] 335 %x1 = lshr i128 %x, 64 336 %x2 = trunc i128 %x1 to i64 337 %c = call i64 @test15a(i64 %x2) 338 %c1 = zext i64 %c to i128 339 br label %two 340 341 ; CHECK: one: 342 ; CHECK-NEXT: phi i64 343 ; CHECK-NEXT: %c = call i64 @test15a 344 345 two: 346 %y = phi i128 [%i, %entry], [%c1, %one] 347 %y1 = lshr i128 %y, 64 348 %y2 = trunc i128 %y1 to i64 349 %d = call i64 @test15a(i64 %y2) 350 %d1 = trunc i64 %d to i1 351 br i1 %d1, label %one, label %end 352 353 ; CHECK: two: 354 ; CHECK-NEXT: phi i64 355 ; CHECK-NEXT: phi i64 356 ; CHECK-NEXT: %d = call i64 @test15a 357 358 end: 359 %g = trunc i128 %y to i64 360 ret i64 %g 361 ; CHECK: end: 362 ; CHECK-NEXT: ret i64 363 } 364 365 ; PR6512 - Shouldn't merge loads from different addr spaces. 366 define i32 @test16(i32 addrspace(1)* %pointer1, i32 %flag, i32* %pointer2) 367 nounwind { 368 entry: 369 %retval = alloca i32, align 4 ; <i32*> [#uses=2] 370 %pointer1.addr = alloca i32 addrspace(1)*, align 4 ; <i32 addrspace(1)**> 371 %flag.addr = alloca i32, align 4 ; <i32*> [#uses=2] 372 %pointer2.addr = alloca i32*, align 4 ; <i32**> [#uses=2] 373 %res = alloca i32, align 4 ; <i32*> [#uses=4] 374 store i32 addrspace(1)* %pointer1, i32 addrspace(1)** %pointer1.addr 375 store i32 %flag, i32* %flag.addr 376 store i32* %pointer2, i32** %pointer2.addr 377 store i32 10, i32* %res 378 %tmp = load i32* %flag.addr ; <i32> [#uses=1] 379 %tobool = icmp ne i32 %tmp, 0 ; <i1> [#uses=1] 380 br i1 %tobool, label %if.then, label %if.else 381 382 return: ; preds = %if.end 383 %tmp7 = load i32* %retval ; <i32> [#uses=1] 384 ret i32 %tmp7 385 386 if.end: ; preds = %if.else, %if.then 387 %tmp6 = load i32* %res ; <i32> [#uses=1] 388 store i32 %tmp6, i32* %retval 389 br label %return 390 391 if.then: ; preds = %entry 392 %tmp1 = load i32 addrspace(1)** %pointer1.addr ; <i32 addrspace(1)*> 393 %arrayidx = getelementptr i32 addrspace(1)* %tmp1, i32 0 ; <i32 addrspace(1)*> [#uses=1] 394 %tmp2 = load i32 addrspace(1)* %arrayidx ; <i32> [#uses=1] 395 store i32 %tmp2, i32* %res 396 br label %if.end 397 398 if.else: ; preds = %entry 399 %tmp3 = load i32** %pointer2.addr ; <i32*> [#uses=1] 400 %arrayidx4 = getelementptr i32* %tmp3, i32 0 ; <i32*> [#uses=1] 401 %tmp5 = load i32* %arrayidx4 ; <i32> [#uses=1] 402 store i32 %tmp5, i32* %res 403 br label %if.end 404 } 405 406 ; PR4413 407 declare i32 @ext() 408 ; CHECK-LABEL: @test17( 409 define i32 @test17(i1 %a) { 410 entry: 411 br i1 %a, label %bb1, label %bb2 412 413 bb1: ; preds = %entry 414 %0 = tail call i32 @ext() ; <i32> [#uses=1] 415 br label %bb2 416 417 bb2: ; preds = %bb1, %entry 418 %cond = phi i1 [ true, %bb1 ], [ false, %entry ] ; <i1> [#uses=1] 419 ; CHECK-NOT: %val = phi i32 [ %0, %bb1 ], [ 0, %entry ] 420 %val = phi i32 [ %0, %bb1 ], [ 0, %entry ] ; <i32> [#uses=1] 421 %res = select i1 %cond, i32 %val, i32 0 ; <i32> [#uses=1] 422 ; CHECK: ret i32 %cond 423 ret i32 %res 424 } 425 426 define i1 @test18(i1 %cond) { 427 %zero = alloca i32 428 %one = alloca i32 429 br i1 %cond, label %true, label %false 430 true: 431 br label %ret 432 false: 433 br label %ret 434 ret: 435 %ptr = phi i32* [ %zero, %true ] , [ %one, %false ] 436 %isnull = icmp eq i32* %ptr, null 437 ret i1 %isnull 438 ; CHECK-LABEL: @test18( 439 ; CHECK: ret i1 false 440 } 441 442 define i1 @test19(i1 %cond, double %x) { 443 br i1 %cond, label %true, label %false 444 true: 445 br label %ret 446 false: 447 br label %ret 448 ret: 449 %p = phi double [ %x, %true ], [ 0x7FF0000000000000, %false ]; RHS = +infty 450 %cmp = fcmp ule double %x, %p 451 ret i1 %cmp 452 ; CHECK-LABEL: @test19( 453 ; CHECK: ret i1 true 454 } 455 456 define i1 @test20(i1 %cond) { 457 %a = alloca i32 458 %b = alloca i32 459 %c = alloca i32 460 br i1 %cond, label %true, label %false 461 true: 462 br label %ret 463 false: 464 br label %ret 465 ret: 466 %p = phi i32* [ %a, %true ], [ %b, %false ] 467 %r = icmp eq i32* %p, %c 468 ret i1 %r 469 ; CHECK-LABEL: @test20( 470 ; CHECK: ret i1 false 471 } 472 473 define i1 @test21(i1 %c1, i1 %c2) { 474 %a = alloca i32 475 %b = alloca i32 476 %c = alloca i32 477 br i1 %c1, label %true, label %false 478 true: 479 br label %loop 480 false: 481 br label %loop 482 loop: 483 %p = phi i32* [ %a, %true ], [ %b, %false ], [ %p, %loop ] 484 %r = icmp eq i32* %p, %c 485 br i1 %c2, label %ret, label %loop 486 ret: 487 ret i1 %r 488 ; CHECK-LABEL: @test21( 489 ; CHECK: ret i1 false 490 } 491 492 define void @test22() { 493 ; CHECK-LABEL: @test22( 494 entry: 495 br label %loop 496 loop: 497 %phi = phi i32 [ 0, %entry ], [ %y, %loop ] 498 %y = add i32 %phi, 1 499 %o = or i32 %y, %phi 500 %e = icmp eq i32 %o, %y 501 br i1 %e, label %loop, label %ret 502 ; CHECK: br i1 %e 503 ret: 504 ret void 505 } 506 507 define i32 @test23(i32 %A, i1 %b, i32 * %P) { 508 BB0: 509 br label %Loop 510 511 Loop: ; preds = %Loop, %BB0 512 ; PHI has same value always. 513 %B = phi i32 [ %A, %BB0 ], [ 42, %Loop ] 514 %D = add i32 %B, 19 515 store i32 %D, i32* %P 516 br i1 %b, label %Loop, label %Exit 517 518 Exit: ; preds = %Loop 519 %E = add i32 %B, 19 520 ret i32 %E 521 ; CHECK-LABEL: @test23( 522 ; CHECK: %phitmp = add i32 %A, 19 523 ; CHECK: Loop: 524 ; CHECK-NEXT: %B = phi i32 [ %phitmp, %BB0 ], [ 61, %Loop ] 525 ; CHECK: Exit: 526 ; CHECK-NEXT: ret i32 %B 527 } 528 529 define i32 @test24(i32 %A, i1 %cond) { 530 BB0: 531 %X = add nuw nsw i32 %A, 1 532 br i1 %cond, label %BB1, label %BB2 533 534 BB1: 535 %Y = add nuw i32 %A, 1 536 br label %BB2 537 538 BB2: 539 %C = phi i32 [ %X, %BB0 ], [ %Y, %BB1 ] 540 ret i32 %C 541 ; CHECK-LABEL: @test24( 542 ; CHECK-NOT: phi 543 ; CHECK: BB2: 544 ; CHECK-NEXT: %C = add nuw i32 %A, 1 545 ; CHECK-NEXT: ret i32 %C 546 } 547 548 ; Same as test11, but used to be missed due to a bug. 549 declare i1 @test25a() 550 551 define i1 @test25() { 552 entry: 553 %a = alloca i32 554 %i = ptrtoint i32* %a to i64 555 %b = call i1 @test25a() 556 br i1 %b, label %one, label %two 557 558 one: 559 %x = phi i64 [%y, %two], [%i, %entry] 560 %c = call i1 @test25a() 561 br i1 %c, label %two, label %end 562 563 two: 564 %y = phi i64 [%x, %one], [%i, %entry] 565 %d = call i1 @test25a() 566 br i1 %d, label %one, label %end 567 568 end: 569 %f = phi i64 [ %x, %one], [%y, %two] 570 ; Change the %f to %i, and the optimizer suddenly becomes a lot smarter 571 ; even though %f must equal %i at this point 572 %g = inttoptr i64 %f to i32* 573 store i32 10, i32* %g 574 %z = call i1 @test25a() 575 ret i1 %z 576 ; CHECK-LABEL: @test25( 577 ; CHECK-NOT: phi i32 578 ; CHECK: ret i1 %z 579 } 580 581 declare i1 @test26a() 582 583 define i1 @test26(i32 %n) { 584 entry: 585 %a = alloca i32 586 %i = ptrtoint i32* %a to i64 587 %b = call i1 @test26a() 588 br label %one 589 590 one: 591 %x = phi i64 [%y, %two], [%w, %three], [%i, %entry] 592 %c = call i1 @test26a() 593 switch i32 %n, label %end [ 594 i32 2, label %two 595 i32 3, label %three 596 ] 597 598 two: 599 %y = phi i64 [%x, %one], [%w, %three] 600 %d = call i1 @test26a() 601 switch i32 %n, label %end [ 602 i32 10, label %one 603 i32 30, label %three 604 ] 605 606 three: 607 %w = phi i64 [%y, %two], [%x, %one] 608 %e = call i1 @test26a() 609 br i1 %e, label %one, label %two 610 611 end: 612 %f = phi i64 [ %x, %one], [%y, %two] 613 ; Change the %f to %i, and the optimizer suddenly becomes a lot smarter 614 ; even though %f must equal %i at this point 615 %g = inttoptr i64 %f to i32* 616 store i32 10, i32* %g 617 %z = call i1 @test26a() 618 ret i1 %z 619 ; CHECK-LABEL: @test26( 620 ; CHECK-NOT: phi i32 621 ; CHECK: ret i1 %z 622 } 623 624 ; CHECK-LABEL: @test27( 625 ; CHECK: ret i32 undef 626 define i32 @test27(i1 %b) { 627 entry: 628 br label %done 629 done: 630 %y = phi i32 [ undef, %entry ] 631 ret i32 %y 632 } 633