1; NOTE: Assertions have been autogenerated by utils/update_test_checks.py 2; RUN: opt < %s -instcombine -S | FileCheck %s 3 4target datalayout = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-a0:0:64-f80:128:128" 5 6define i32 @test1(i32 %A) { 7; CHECK-LABEL: @test1( 8; CHECK-NEXT: ret i32 %A 9; 10 %B = or i32 %A, 0 11 ret i32 %B 12} 13 14define i32 @test2(i32 %A) { 15; CHECK-LABEL: @test2( 16; CHECK-NEXT: ret i32 -1 17; 18 %B = or i32 %A, -1 19 ret i32 %B 20} 21 22define i8 @test2a(i8 %A) { 23; CHECK-LABEL: @test2a( 24; CHECK-NEXT: ret i8 -1 25; 26 %B = or i8 %A, -1 27 ret i8 %B 28} 29 30define i1 @test3(i1 %A) { 31; CHECK-LABEL: @test3( 32; CHECK-NEXT: ret i1 %A 33; 34 %B = or i1 %A, false 35 ret i1 %B 36} 37 38define i1 @test4(i1 %A) { 39; CHECK-LABEL: @test4( 40; CHECK-NEXT: ret i1 true 41; 42 %B = or i1 %A, true 43 ret i1 %B 44} 45 46define i1 @test5(i1 %A) { 47; CHECK-LABEL: @test5( 48; CHECK-NEXT: ret i1 %A 49; 50 %B = or i1 %A, %A 51 ret i1 %B 52} 53 54define i32 @test6(i32 %A) { 55; CHECK-LABEL: @test6( 56; CHECK-NEXT: ret i32 %A 57; 58 %B = or i32 %A, %A 59 ret i32 %B 60} 61 62; A | ~A == -1 63define i32 @test7(i32 %A) { 64; CHECK-LABEL: @test7( 65; CHECK-NEXT: ret i32 -1 66; 67 %NotA = xor i32 -1, %A 68 %B = or i32 %A, %NotA 69 ret i32 %B 70} 71 72define i8 @test8(i8 %A) { 73; CHECK-LABEL: @test8( 74; CHECK-NEXT: ret i8 -1 75; 76 %B = or i8 %A, -2 77 %C = or i8 %B, 1 78 ret i8 %C 79} 80 81; Test that (A|c1)|(B|c2) == (A|B)|(c1|c2) 82define i8 @test9(i8 %A, i8 %B) { 83; CHECK-LABEL: @test9( 84; CHECK-NEXT: ret i8 -1 85; 86 %C = or i8 %A, 1 87 %D = or i8 %B, -2 88 %E = or i8 %C, %D 89 ret i8 %E 90} 91 92define i8 @test10(i8 %A) { 93; CHECK-LABEL: @test10( 94; CHECK-NEXT: ret i8 -2 95; 96 %B = or i8 %A, 1 97 %C = and i8 %B, -2 98 ; (X & C1) | C2 --> (X | C2) & (C1|C2) 99 %D = or i8 %C, -2 100 ret i8 %D 101} 102 103define i8 @test11(i8 %A) { 104; CHECK-LABEL: @test11( 105; CHECK-NEXT: ret i8 -1 106; 107 %B = or i8 %A, -2 108 %C = xor i8 %B, 13 109 ; (X ^ C1) | C2 --> (X | C2) ^ (C1&~C2) 110 %D = or i8 %C, 1 111 %E = xor i8 %D, 12 112 ret i8 %E 113} 114 115define i32 @test12(i32 %A) { 116 ; Should be eliminated 117; CHECK-LABEL: @test12( 118; CHECK-NEXT: [[C:%.*]] = and i32 %A, 8 119; CHECK-NEXT: ret i32 [[C]] 120; 121 %B = or i32 %A, 4 122 %C = and i32 %B, 8 123 ret i32 %C 124} 125 126define i32 @test13(i32 %A) { 127; CHECK-LABEL: @test13( 128; CHECK-NEXT: ret i32 8 129; 130 %B = or i32 %A, 12 131 ; Always equal to 8 132 %C = and i32 %B, 8 133 ret i32 %C 134} 135 136define i1 @test14(i32 %A, i32 %B) { 137; CHECK-LABEL: @test14( 138; CHECK-NEXT: [[TMP1:%.*]] = icmp ne i32 %A, %B 139; CHECK-NEXT: ret i1 [[TMP1]] 140; 141 %C1 = icmp ult i32 %A, %B 142 %C2 = icmp ugt i32 %A, %B 143 ; (A < B) | (A > B) === A != B 144 %D = or i1 %C1, %C2 145 ret i1 %D 146} 147 148define i1 @test15(i32 %A, i32 %B) { 149; CHECK-LABEL: @test15( 150; CHECK-NEXT: [[TMP1:%.*]] = icmp ule i32 %A, %B 151; CHECK-NEXT: ret i1 [[TMP1]] 152; 153 %C1 = icmp ult i32 %A, %B 154 %C2 = icmp eq i32 %A, %B 155 ; (A < B) | (A == B) === A <= B 156 %D = or i1 %C1, %C2 157 ret i1 %D 158} 159 160define i32 @test16(i32 %A) { 161; CHECK-LABEL: @test16( 162; CHECK-NEXT: ret i32 %A 163; 164 %B = and i32 %A, 1 165 ; -2 = ~1 166 %C = and i32 %A, -2 167 ; %D = and int %B, -1 == %B 168 %D = or i32 %B, %C 169 ret i32 %D 170} 171 172define i32 @test17(i32 %A) { 173; CHECK-LABEL: @test17( 174; CHECK-NEXT: [[D:%.*]] = and i32 %A, 5 175; CHECK-NEXT: ret i32 [[D]] 176; 177 %B = and i32 %A, 1 178 %C = and i32 %A, 4 179 ; %D = and int %B, 5 180 %D = or i32 %B, %C 181 ret i32 %D 182} 183 184define i1 @test18(i32 %A) { 185; CHECK-LABEL: @test18( 186; CHECK-NEXT: [[A_OFF:%.*]] = add i32 %A, -50 187; CHECK-NEXT: [[TMP1:%.*]] = icmp ugt i32 [[A_OFF]], 49 188; CHECK-NEXT: ret i1 [[TMP1]] 189; 190 %B = icmp sge i32 %A, 100 191 %C = icmp slt i32 %A, 50 192 ;; (A-50) >u 50 193 %D = or i1 %B, %C 194 ret i1 %D 195} 196 197define i1 @test19(i32 %A) { 198; CHECK-LABEL: @test19( 199; CHECK-NEXT: [[TMP1:%.*]] = or i32 %A, 1 200; CHECK-NEXT: [[TMP2:%.*]] = icmp eq i32 [[TMP1]], 51 201; CHECK-NEXT: ret i1 [[TMP2]] 202; 203 %B = icmp eq i32 %A, 50 204 %C = icmp eq i32 %A, 51 205 ;; (A&-2) == 50 206 %D = or i1 %B, %C 207 ret i1 %D 208} 209 210define i32 @test20(i32 %x) { 211; CHECK-LABEL: @test20( 212; CHECK-NEXT: ret i32 %x 213; 214 %y = and i32 %x, 123 215 %z = or i32 %y, %x 216 ret i32 %z 217} 218 219define i32 @test21(i32 %tmp.1) { 220; CHECK-LABEL: @test21( 221; CHECK-NEXT: [[TMP_1_MASK1:%.*]] = add i32 %tmp.1, 2 222; CHECK-NEXT: ret i32 [[TMP_1_MASK1]] 223; 224 %tmp.1.mask1 = add i32 %tmp.1, 2 225 %tmp.3 = and i32 %tmp.1.mask1, -2 226 %tmp.5 = and i32 %tmp.1, 1 227 ;; add tmp.1, 2 228 %tmp.6 = or i32 %tmp.5, %tmp.3 229 ret i32 %tmp.6 230} 231 232define i32 @test22(i32 %B) { 233; CHECK-LABEL: @test22( 234; CHECK-NEXT: ret i32 %B 235; 236 %ELIM41 = and i32 %B, 1 237 %ELIM7 = and i32 %B, -2 238 %ELIM5 = or i32 %ELIM41, %ELIM7 239 ret i32 %ELIM5 240} 241 242define i16 @test23(i16 %A) { 243; CHECK-LABEL: @test23( 244; CHECK-NEXT: [[B:%.*]] = lshr i16 %A, 1 245; CHECK-NEXT: [[D:%.*]] = xor i16 [[B]], -24575 246; CHECK-NEXT: ret i16 [[D]] 247; 248 %B = lshr i16 %A, 1 249 ;; fold or into xor 250 %C = or i16 %B, -32768 251 %D = xor i16 %C, 8193 252 ret i16 %D 253} 254 255; PR1738 256define i1 @test24(double %X, double %Y) { 257; CHECK-LABEL: @test24( 258; CHECK-NEXT: [[TMP1:%.*]] = fcmp uno double %Y, %X 259; CHECK-NEXT: ret i1 [[TMP1]] 260; 261 %tmp9 = fcmp uno double %X, 0.000000e+00 262 %tmp13 = fcmp uno double %Y, 0.000000e+00 263 %bothcond = or i1 %tmp13, %tmp9 264 ret i1 %bothcond 265} 266 267; PR3266 & PR5276 268define i1 @test25(i32 %A, i32 %B) { 269; CHECK-LABEL: @test25( 270; CHECK-NEXT: [[NOTLHS:%.*]] = icmp ne i32 %A, 0 271; CHECK-NEXT: [[NOTRHS:%.*]] = icmp ne i32 %B, 57 272; CHECK-NEXT: [[F:%.*]] = and i1 [[NOTRHS]], [[NOTLHS]] 273; CHECK-NEXT: ret i1 [[F]] 274; 275 %C = icmp eq i32 %A, 0 276 %D = icmp eq i32 %B, 57 277 %E = or i1 %C, %D 278 %F = xor i1 %E, -1 279 ret i1 %F 280} 281 282; PR5634 283define i1 @test26(i32 %A, i32 %B) { 284; CHECK-LABEL: @test26( 285; CHECK-NEXT: [[TMP1:%.*]] = or i32 %A, %B 286; CHECK-NEXT: [[TMP2:%.*]] = icmp eq i32 [[TMP1]], 0 287; CHECK-NEXT: ret i1 [[TMP2]] 288; 289 %C1 = icmp eq i32 %A, 0 290 %C2 = icmp eq i32 %B, 0 291 ; (A == 0) & (A == 0) --> (A|B) == 0 292 %D = and i1 %C1, %C2 293 ret i1 %D 294} 295 296define i1 @test27(i32* %A, i32* %B) { 297; CHECK-LABEL: @test27( 298; CHECK-NEXT: [[TMP1:%.*]] = icmp eq i32* %A, null 299; CHECK-NEXT: [[TMP2:%.*]] = icmp eq i32* %B, null 300; CHECK-NEXT: [[E:%.*]] = and i1 [[TMP1]], [[TMP2]] 301; CHECK-NEXT: ret i1 [[E]] 302; 303 %C1 = ptrtoint i32* %A to i32 304 %C2 = ptrtoint i32* %B to i32 305 %D = or i32 %C1, %C2 306 %E = icmp eq i32 %D, 0 307 ret i1 %E 308} 309 310; PR5634 311define i1 @test28(i32 %A, i32 %B) { 312; CHECK-LABEL: @test28( 313; CHECK-NEXT: [[TMP1:%.*]] = or i32 %A, %B 314; CHECK-NEXT: [[TMP2:%.*]] = icmp ne i32 [[TMP1]], 0 315; CHECK-NEXT: ret i1 [[TMP2]] 316; 317 %C1 = icmp ne i32 %A, 0 318 %C2 = icmp ne i32 %B, 0 319 ; (A != 0) | (A != 0) --> (A|B) != 0 320 %D = or i1 %C1, %C2 321 ret i1 %D 322} 323 324define i1 @test29(i32* %A, i32* %B) { 325; CHECK-LABEL: @test29( 326; CHECK-NEXT: [[TMP1:%.*]] = icmp ne i32* %A, null 327; CHECK-NEXT: [[TMP2:%.*]] = icmp ne i32* %B, null 328; CHECK-NEXT: [[E:%.*]] = or i1 [[TMP1]], [[TMP2]] 329; CHECK-NEXT: ret i1 [[E]] 330; 331 %C1 = ptrtoint i32* %A to i32 332 %C2 = ptrtoint i32* %B to i32 333 %D = or i32 %C1, %C2 334 %E = icmp ne i32 %D, 0 335 ret i1 %E 336} 337 338; PR4216 339define i32 @test30(i32 %A) { 340; CHECK-LABEL: @test30( 341; CHECK-NEXT: [[D:%.*]] = and i32 %A, -58312 342; CHECK-NEXT: [[E:%.*]] = or i32 [[D]], 32962 343; CHECK-NEXT: ret i32 [[E]] 344; 345 %B = or i32 %A, 32962 346 %C = and i32 %A, -65536 347 %D = and i32 %B, 40186 348 %E = or i32 %D, %C 349 ret i32 %E 350} 351 352; PR4216 353define i64 @test31(i64 %A) { 354; CHECK-LABEL: @test31( 355; CHECK-NEXT: [[E:%.*]] = and i64 %A, 4294908984 356; CHECK-NEXT: [[F:%.*]] = or i64 [[E]], 32962 357; CHECK-NEXT: ret i64 [[F]] 358; 359 %B = or i64 %A, 194 360 %D = and i64 %B, 250 361 362 %C = or i64 %A, 32768 363 %E = and i64 %C, 4294941696 364 365 %F = or i64 %D, %E 366 ret i64 %F 367} 368 369; codegen is mature enough to handle vector selects. 370define <4 x i32> @test32(<4 x i1> %and.i1352, <4 x i32> %vecinit6.i176, <4 x i32> %vecinit6.i191) { 371; CHECK-LABEL: @test32( 372; CHECK-NEXT: [[OR_I:%.*]] = select <4 x i1> %and.i1352, <4 x i32> %vecinit6.i176, <4 x i32> %vecinit6.i191 373; CHECK-NEXT: ret <4 x i32> [[OR_I]] 374; 375 %and.i135 = sext <4 x i1> %and.i1352 to <4 x i32> 376 %and.i129 = and <4 x i32> %vecinit6.i176, %and.i135 377 %neg.i = xor <4 x i32> %and.i135, <i32 -1, i32 -1, i32 -1, i32 -1> 378 %and.i = and <4 x i32> %vecinit6.i191, %neg.i 379 %or.i = or <4 x i32> %and.i, %and.i129 380 ret <4 x i32> %or.i 381} 382 383define i1 @test33(i1 %X, i1 %Y) { 384; CHECK-LABEL: @test33( 385; CHECK-NEXT: [[B:%.*]] = or i1 %X, %Y 386; CHECK-NEXT: ret i1 [[B]] 387; 388 %a = or i1 %X, %Y 389 %b = or i1 %a, %X 390 ret i1 %b 391} 392 393define i32 @test34(i32 %X, i32 %Y) { 394; CHECK-LABEL: @test34( 395; CHECK-NEXT: [[B:%.*]] = or i32 %X, %Y 396; CHECK-NEXT: ret i32 [[B]] 397; 398 %a = or i32 %X, %Y 399 %b = or i32 %Y, %a 400 ret i32 %b 401} 402 403define i32 @test35(i32 %a, i32 %b) { 404; CHECK-LABEL: @test35( 405; CHECK-NEXT: [[TMP1:%.*]] = or i32 %a, %b 406; CHECK-NEXT: [[TMP2:%.*]] = or i32 [[TMP1]], 1135 407; CHECK-NEXT: ret i32 [[TMP2]] 408; 409 %1 = or i32 %a, 1135 410 %2 = or i32 %1, %b 411 ret i32 %2 412} 413 414define i1 @test36(i32 %x) { 415; CHECK-LABEL: @test36( 416; CHECK-NEXT: [[X_OFF:%.*]] = add i32 %x, -23 417; CHECK-NEXT: [[TMP1:%.*]] = icmp ult i32 [[X_OFF]], 3 418; CHECK-NEXT: ret i1 [[TMP1]] 419; 420 %cmp1 = icmp eq i32 %x, 23 421 %cmp2 = icmp eq i32 %x, 24 422 %ret1 = or i1 %cmp1, %cmp2 423 %cmp3 = icmp eq i32 %x, 25 424 %ret2 = or i1 %ret1, %cmp3 425 ret i1 %ret2 426} 427 428define i32 @orsext_to_sel(i32 %x, i1 %y) { 429; CHECK-LABEL: @orsext_to_sel( 430; CHECK-NEXT: [[OR:%.*]] = select i1 %y, i32 -1, i32 %x 431; CHECK-NEXT: ret i32 [[OR]] 432; 433 %sext = sext i1 %y to i32 434 %or = or i32 %sext, %x 435 ret i32 %or 436} 437 438define i32 @orsext_to_sel_swap(i32 %x, i1 %y) { 439; CHECK-LABEL: @orsext_to_sel_swap( 440; CHECK-NEXT: [[OR:%.*]] = select i1 %y, i32 -1, i32 %x 441; CHECK-NEXT: ret i32 [[OR]] 442; 443 %sext = sext i1 %y to i32 444 %or = or i32 %x, %sext 445 ret i32 %or 446} 447 448define i32 @orsext_to_sel_multi_use(i32 %x, i1 %y) { 449; CHECK-LABEL: @orsext_to_sel_multi_use( 450; CHECK-NEXT: [[SEXT:%.*]] = sext i1 %y to i32 451; CHECK-NEXT: [[OR:%.*]] = or i32 [[SEXT]], %x 452; CHECK-NEXT: [[ADD:%.*]] = add i32 [[SEXT]], [[OR]] 453; CHECK-NEXT: ret i32 [[ADD]] 454; 455 %sext = sext i1 %y to i32 456 %or = or i32 %sext, %x 457 %add = add i32 %sext, %or 458 ret i32 %add 459} 460 461define <2 x i32> @orsext_to_sel_vec(<2 x i32> %x, <2 x i1> %y) { 462; CHECK-LABEL: @orsext_to_sel_vec( 463; CHECK-NEXT: [[OR:%.*]] = select <2 x i1> %y, <2 x i32> <i32 -1, i32 -1>, <2 x i32> %x 464; CHECK-NEXT: ret <2 x i32> [[OR]] 465; 466 %sext = sext <2 x i1> %y to <2 x i32> 467 %or = or <2 x i32> %sext, %x 468 ret <2 x i32> %or 469} 470 471define <2 x i132> @orsext_to_sel_vec_swap(<2 x i132> %x, <2 x i1> %y) { 472; CHECK-LABEL: @orsext_to_sel_vec_swap( 473; CHECK-NEXT: [[OR:%.*]] = select <2 x i1> %y, <2 x i132> <i132 -1, i132 -1>, <2 x i132> %x 474; CHECK-NEXT: ret <2 x i132> [[OR]] 475; 476 %sext = sext <2 x i1> %y to <2 x i132> 477 %or = or <2 x i132> %x, %sext 478 ret <2 x i132> %or 479} 480 481define i32 @test39(i32 %a, i32 %b) { 482; CHECK-LABEL: @test39( 483; CHECK-NEXT: [[OR:%.*]] = or i32 %a, %b 484; CHECK-NEXT: ret i32 [[OR]] 485; 486 %xor = xor i32 %a, -1 487 %and = and i32 %xor, %b 488 %or = or i32 %and, %a 489 ret i32 %or 490} 491 492define i32 @test40(i32 %a, i32 %b) { 493; CHECK-LABEL: @test40( 494; CHECK-NEXT: [[TMP1:%.*]] = xor i32 %a, -1 495; CHECK-NEXT: [[OR:%.*]] = or i32 [[TMP1]], %b 496; CHECK-NEXT: ret i32 [[OR]] 497; 498 %and = and i32 %a, %b 499 %xor = xor i32 %a, -1 500 %or = or i32 %and, %xor 501 ret i32 %or 502} 503 504define i32 @test41(i32 %a, i32 %b) { 505; CHECK-LABEL: @test41( 506; CHECK-NEXT: [[TMP1:%.*]] = xor i32 %a, -1 507; CHECK-NEXT: [[OR:%.*]] = xor i32 [[TMP1]], %b 508; CHECK-NEXT: ret i32 [[OR]] 509; 510 %and = and i32 %a, %b 511 %nega = xor i32 %a, -1 512 %xor = xor i32 %nega, %b 513 %or = or i32 %and, %xor 514 ret i32 %or 515} 516 517define i32 @test42(i32 %a, i32 %b) { 518; CHECK-LABEL: @test42( 519; CHECK-NEXT: [[TMP1:%.*]] = xor i32 %a, -1 520; CHECK-NEXT: [[OR:%.*]] = xor i32 [[TMP1]], %b 521; CHECK-NEXT: ret i32 [[OR]] 522; 523 %nega = xor i32 %a, -1 524 %xor = xor i32 %nega, %b 525 %and = and i32 %a, %b 526 %or = or i32 %xor, %and 527 ret i32 %or 528} 529 530define i32 @test43(i32 %a, i32 %b) { 531; CHECK-LABEL: @test43( 532; CHECK-NEXT: [[OR:%.*]] = xor i32 %a, %b 533; CHECK-NEXT: ret i32 [[OR]] 534; 535 %neg = xor i32 %b, -1 536 %and = and i32 %a, %neg 537 %xor = xor i32 %a, %b 538 %or = or i32 %and, %xor 539 ret i32 %or 540} 541 542define i32 @test44(i32 %a, i32 %b) { 543; CHECK-LABEL: @test44( 544; CHECK-NEXT: [[OR:%.*]] = xor i32 %a, %b 545; CHECK-NEXT: ret i32 [[OR]] 546; 547 %xor = xor i32 %a, %b 548 %neg = xor i32 %b, -1 549 %and = and i32 %a, %neg 550 %or = or i32 %xor, %and 551 ret i32 %or 552} 553 554define i32 @test45(i32 %x, i32 %y, i32 %z) { 555; CHECK-LABEL: @test45( 556; CHECK-NEXT: [[TMP1:%.*]] = and i32 %x, %z 557; CHECK-NEXT: [[OR1:%.*]] = or i32 [[TMP1]], %y 558; CHECK-NEXT: ret i32 [[OR1]] 559; 560 %or = or i32 %y, %z 561 %and = and i32 %x, %or 562 %or1 = or i32 %and, %y 563 ret i32 %or1 564} 565 566define i1 @test46(i8 signext %c) { 567; CHECK-LABEL: @test46( 568; CHECK-NEXT: [[TMP1:%.*]] = and i8 %c, -33 569; CHECK-NEXT: [[TMP2:%.*]] = add i8 [[TMP1]], -65 570; CHECK-NEXT: [[TMP3:%.*]] = icmp ult i8 [[TMP2]], 26 571; CHECK-NEXT: ret i1 [[TMP3]] 572; 573 %c.off = add i8 %c, -97 574 %cmp1 = icmp ult i8 %c.off, 26 575 %c.off17 = add i8 %c, -65 576 %cmp2 = icmp ult i8 %c.off17, 26 577 %or = or i1 %cmp1, %cmp2 578 ret i1 %or 579} 580 581define i1 @test47(i8 signext %c) { 582; CHECK-LABEL: @test47( 583; CHECK-NEXT: [[TMP1:%.*]] = and i8 %c, -33 584; CHECK-NEXT: [[TMP2:%.*]] = add i8 [[TMP1]], -65 585; CHECK-NEXT: [[TMP3:%.*]] = icmp ult i8 [[TMP2]], 27 586; CHECK-NEXT: ret i1 [[TMP3]] 587; 588 %c.off = add i8 %c, -65 589 %cmp1 = icmp ule i8 %c.off, 26 590 %c.off17 = add i8 %c, -97 591 %cmp2 = icmp ule i8 %c.off17, 26 592 %or = or i1 %cmp1, %cmp2 593 ret i1 %or 594} 595 596define i1 @test48(i64 %x, i1 %b) { 597; CHECK-LABEL: @test48( 598; CHECK-NEXT: ret i1 true 599; 600 %1 = icmp ult i64 %x, 2305843009213693952 601 %2 = icmp ugt i64 %x, 2305843009213693951 602 %.b = or i1 %2, %b 603 %3 = or i1 %1, %.b 604 ret i1 %3 605} 606