1; NOTE: Assertions have been autogenerated by utils/update_test_checks.py 2; RUN: opt < %s -instsimplify -S | FileCheck %s 3 4define i64 @pow2(i32 %x) { 5; CHECK-LABEL: @pow2( 6; CHECK-NEXT: [[NEGX:%.*]] = sub i32 0, %x 7; CHECK-NEXT: [[X2:%.*]] = and i32 %x, [[NEGX]] 8; CHECK-NEXT: [[E:%.*]] = zext i32 [[X2]] to i64 9; CHECK-NEXT: ret i64 [[E]] 10; 11 %negx = sub i32 0, %x 12 %x2 = and i32 %x, %negx 13 %e = zext i32 %x2 to i64 14 %nege = sub i64 0, %e 15 %e2 = and i64 %e, %nege 16 ret i64 %e2 17} 18 19define i64 @pow2b(i32 %x) { 20; CHECK-LABEL: @pow2b( 21; CHECK-NEXT: [[SH:%.*]] = shl i32 2, %x 22; CHECK-NEXT: [[E:%.*]] = zext i32 [[SH]] to i64 23; CHECK-NEXT: ret i64 [[E]] 24; 25 %sh = shl i32 2, %x 26 %e = zext i32 %sh to i64 27 %nege = sub i64 0, %e 28 %e2 = and i64 %e, %nege 29 ret i64 %e2 30} 31 32define i32 @sub_neg_nuw(i32 %x, i32 %y) { 33; CHECK-LABEL: @sub_neg_nuw( 34; CHECK-NEXT: ret i32 %x 35; 36 %neg = sub nuw i32 0, %y 37 %sub = sub i32 %x, %neg 38 ret i32 %sub 39} 40 41define i1 @and_of_icmps0(i32 %b) { 42; CHECK-LABEL: @and_of_icmps0( 43; CHECK-NEXT: ret i1 false 44; 45 %1 = add i32 %b, 2 46 %2 = icmp ult i32 %1, 4 47 %cmp3 = icmp sgt i32 %b, 2 48 %cmp = and i1 %2, %cmp3 49 ret i1 %cmp 50} 51 52define <2 x i1> @and_of_icmps0_vec(<2 x i32> %b) { 53; CHECK-LABEL: @and_of_icmps0_vec( 54; CHECK-NEXT: ret <2 x i1> zeroinitializer 55; 56 %1 = add <2 x i32> %b, <i32 2, i32 2> 57 %2 = icmp ult <2 x i32> %1, <i32 4, i32 4> 58 %cmp3 = icmp sgt <2 x i32> %b, <i32 2, i32 2> 59 %cmp = and <2 x i1> %2, %cmp3 60 ret <2 x i1> %cmp 61} 62 63define i1 @and_of_icmps1(i32 %b) { 64; CHECK-LABEL: @and_of_icmps1( 65; CHECK-NEXT: ret i1 false 66; 67 %1 = add nsw i32 %b, 2 68 %2 = icmp slt i32 %1, 4 69 %cmp3 = icmp sgt i32 %b, 2 70 %cmp = and i1 %2, %cmp3 71 ret i1 %cmp 72} 73 74define <2 x i1> @and_of_icmps1_vec(<2 x i32> %b) { 75; CHECK-LABEL: @and_of_icmps1_vec( 76; CHECK-NEXT: ret <2 x i1> zeroinitializer 77; 78 %1 = add nsw <2 x i32> %b, <i32 2, i32 2> 79 %2 = icmp slt <2 x i32> %1, <i32 4, i32 4> 80 %cmp3 = icmp sgt <2 x i32> %b, <i32 2, i32 2> 81 %cmp = and <2 x i1> %2, %cmp3 82 ret <2 x i1> %cmp 83} 84 85define i1 @and_of_icmps2(i32 %b) { 86; CHECK-LABEL: @and_of_icmps2( 87; CHECK-NEXT: ret i1 false 88; 89 %1 = add i32 %b, 2 90 %2 = icmp ule i32 %1, 3 91 %cmp3 = icmp sgt i32 %b, 2 92 %cmp = and i1 %2, %cmp3 93 ret i1 %cmp 94} 95 96define <2 x i1> @and_of_icmps2_vec(<2 x i32> %b) { 97; CHECK-LABEL: @and_of_icmps2_vec( 98; CHECK-NEXT: ret <2 x i1> zeroinitializer 99; 100 %1 = add <2 x i32> %b, <i32 2, i32 2> 101 %2 = icmp ule <2 x i32> %1, <i32 3, i32 3> 102 %cmp3 = icmp sgt <2 x i32> %b, <i32 2, i32 2> 103 %cmp = and <2 x i1> %2, %cmp3 104 ret <2 x i1> %cmp 105} 106 107define i1 @and_of_icmps3(i32 %b) { 108; CHECK-LABEL: @and_of_icmps3( 109; CHECK-NEXT: ret i1 false 110; 111 %1 = add nsw i32 %b, 2 112 %2 = icmp sle i32 %1, 3 113 %cmp3 = icmp sgt i32 %b, 2 114 %cmp = and i1 %2, %cmp3 115 ret i1 %cmp 116} 117 118define <2 x i1> @and_of_icmps3_vec(<2 x i32> %b) { 119; CHECK-LABEL: @and_of_icmps3_vec( 120; CHECK-NEXT: ret <2 x i1> zeroinitializer 121; 122 %1 = add nsw <2 x i32> %b, <i32 2, i32 2> 123 %2 = icmp sle <2 x i32> %1, <i32 3, i32 3> 124 %cmp3 = icmp sgt <2 x i32> %b, <i32 2, i32 2> 125 %cmp = and <2 x i1> %2, %cmp3 126 ret <2 x i1> %cmp 127} 128 129define i1 @and_of_icmps4(i32 %b) { 130; CHECK-LABEL: @and_of_icmps4( 131; CHECK-NEXT: ret i1 false 132; 133 %1 = add nuw i32 %b, 2 134 %2 = icmp ult i32 %1, 4 135 %cmp3 = icmp ugt i32 %b, 2 136 %cmp = and i1 %2, %cmp3 137 ret i1 %cmp 138} 139 140define <2 x i1> @and_of_icmps4_vec(<2 x i32> %b) { 141; CHECK-LABEL: @and_of_icmps4_vec( 142; CHECK-NEXT: ret <2 x i1> zeroinitializer 143; 144 %1 = add nuw <2 x i32> %b, <i32 2, i32 2> 145 %2 = icmp ult <2 x i32> %1, <i32 4, i32 4> 146 %cmp3 = icmp ugt <2 x i32> %b, <i32 2, i32 2> 147 %cmp = and <2 x i1> %2, %cmp3 148 ret <2 x i1> %cmp 149} 150 151define i1 @and_of_icmps5(i32 %b) { 152; CHECK-LABEL: @and_of_icmps5( 153; CHECK-NEXT: ret i1 false 154; 155 %1 = add nuw i32 %b, 2 156 %2 = icmp ule i32 %1, 3 157 %cmp3 = icmp ugt i32 %b, 2 158 %cmp = and i1 %2, %cmp3 159 ret i1 %cmp 160} 161 162define <2 x i1> @and_of_icmps5_vec(<2 x i32> %b) { 163; CHECK-LABEL: @and_of_icmps5_vec( 164; CHECK-NEXT: ret <2 x i1> zeroinitializer 165; 166 %1 = add nuw <2 x i32> %b, <i32 2, i32 2> 167 %2 = icmp ule <2 x i32> %1, <i32 3, i32 3> 168 %cmp3 = icmp ugt <2 x i32> %b, <i32 2, i32 2> 169 %cmp = and <2 x i1> %2, %cmp3 170 ret <2 x i1> %cmp 171} 172 173define i1 @or_of_icmps0(i32 %b) { 174; CHECK-LABEL: @or_of_icmps0( 175; CHECK-NEXT: ret i1 true 176; 177 %1 = add i32 %b, 2 178 %2 = icmp uge i32 %1, 4 179 %cmp3 = icmp sle i32 %b, 2 180 %cmp = or i1 %2, %cmp3 181 ret i1 %cmp 182} 183 184define <2 x i1> @or_of_icmps0_vec(<2 x i32> %b) { 185; CHECK-LABEL: @or_of_icmps0_vec( 186; CHECK-NEXT: ret <2 x i1> <i1 true, i1 true> 187; 188 %1 = add <2 x i32> %b, <i32 2, i32 2> 189 %2 = icmp uge <2 x i32> %1, <i32 4, i32 4> 190 %cmp3 = icmp sle <2 x i32> %b, <i32 2, i32 2> 191 %cmp = or <2 x i1> %2, %cmp3 192 ret <2 x i1> %cmp 193} 194 195define i1 @or_of_icmps1(i32 %b) { 196; CHECK-LABEL: @or_of_icmps1( 197; CHECK-NEXT: ret i1 true 198; 199 %1 = add nsw i32 %b, 2 200 %2 = icmp sge i32 %1, 4 201 %cmp3 = icmp sle i32 %b, 2 202 %cmp = or i1 %2, %cmp3 203 ret i1 %cmp 204} 205 206define <2 x i1> @or_of_icmps1_vec(<2 x i32> %b) { 207; CHECK-LABEL: @or_of_icmps1_vec( 208; CHECK-NEXT: ret <2 x i1> <i1 true, i1 true> 209; 210 %1 = add nsw <2 x i32> %b, <i32 2, i32 2> 211 %2 = icmp sge <2 x i32> %1, <i32 4, i32 4> 212 %cmp3 = icmp sle <2 x i32> %b, <i32 2, i32 2> 213 %cmp = or <2 x i1> %2, %cmp3 214 ret <2 x i1> %cmp 215} 216 217define i1 @or_of_icmps2(i32 %b) { 218; CHECK-LABEL: @or_of_icmps2( 219; CHECK-NEXT: ret i1 true 220; 221 %1 = add i32 %b, 2 222 %2 = icmp ugt i32 %1, 3 223 %cmp3 = icmp sle i32 %b, 2 224 %cmp = or i1 %2, %cmp3 225 ret i1 %cmp 226} 227 228define <2 x i1> @or_of_icmps2_vec(<2 x i32> %b) { 229; CHECK-LABEL: @or_of_icmps2_vec( 230; CHECK-NEXT: ret <2 x i1> <i1 true, i1 true> 231; 232 %1 = add <2 x i32> %b, <i32 2, i32 2> 233 %2 = icmp ugt <2 x i32> %1, <i32 3, i32 3> 234 %cmp3 = icmp sle <2 x i32> %b, <i32 2, i32 2> 235 %cmp = or <2 x i1> %2, %cmp3 236 ret <2 x i1> %cmp 237} 238 239define i1 @or_of_icmps3(i32 %b) { 240; CHECK-LABEL: @or_of_icmps3( 241; CHECK-NEXT: ret i1 true 242; 243 %1 = add nsw i32 %b, 2 244 %2 = icmp sgt i32 %1, 3 245 %cmp3 = icmp sle i32 %b, 2 246 %cmp = or i1 %2, %cmp3 247 ret i1 %cmp 248} 249 250define <2 x i1> @or_of_icmps3_vec(<2 x i32> %b) { 251; CHECK-LABEL: @or_of_icmps3_vec( 252; CHECK-NEXT: ret <2 x i1> <i1 true, i1 true> 253; 254 %1 = add nsw <2 x i32> %b, <i32 2, i32 2> 255 %2 = icmp sgt <2 x i32> %1, <i32 3, i32 3> 256 %cmp3 = icmp sle <2 x i32> %b, <i32 2, i32 2> 257 %cmp = or <2 x i1> %2, %cmp3 258 ret <2 x i1> %cmp 259} 260 261define i1 @or_of_icmps4(i32 %b) { 262; CHECK-LABEL: @or_of_icmps4( 263; CHECK-NEXT: ret i1 true 264; 265 %1 = add nuw i32 %b, 2 266 %2 = icmp uge i32 %1, 4 267 %cmp3 = icmp ule i32 %b, 2 268 %cmp = or i1 %2, %cmp3 269 ret i1 %cmp 270} 271 272define <2 x i1> @or_of_icmps4_vec(<2 x i32> %b) { 273; CHECK-LABEL: @or_of_icmps4_vec( 274; CHECK-NEXT: ret <2 x i1> <i1 true, i1 true> 275; 276 %1 = add nuw <2 x i32> %b, <i32 2, i32 2> 277 %2 = icmp uge <2 x i32> %1, <i32 4, i32 4> 278 %cmp3 = icmp ule <2 x i32> %b, <i32 2, i32 2> 279 %cmp = or <2 x i1> %2, %cmp3 280 ret <2 x i1> %cmp 281} 282 283define i1 @or_of_icmps5(i32 %b) { 284; CHECK-LABEL: @or_of_icmps5( 285; CHECK-NEXT: ret i1 true 286; 287 %1 = add nuw i32 %b, 2 288 %2 = icmp ugt i32 %1, 3 289 %cmp3 = icmp ule i32 %b, 2 290 %cmp = or i1 %2, %cmp3 291 ret i1 %cmp 292} 293 294define <2 x i1> @or_of_icmps5_vec(<2 x i32> %b) { 295; CHECK-LABEL: @or_of_icmps5_vec( 296; CHECK-NEXT: ret <2 x i1> <i1 true, i1 true> 297; 298 %1 = add nuw <2 x i32> %b, <i32 2, i32 2> 299 %2 = icmp ugt <2 x i32> %1, <i32 3, i32 3> 300 %cmp3 = icmp ule <2 x i32> %b, <i32 2, i32 2> 301 %cmp = or <2 x i1> %2, %cmp3 302 ret <2 x i1> %cmp 303} 304 305define i32 @neg_nuw(i32 %x) { 306; CHECK-LABEL: @neg_nuw( 307; CHECK-NEXT: ret i32 0 308; 309 %neg = sub nuw i32 0, %x 310 ret i32 %neg 311} 312 313define i1 @and_icmp1(i32 %x, i32 %y) { 314; CHECK-LABEL: @and_icmp1( 315; CHECK-NEXT: [[TMP1:%.*]] = icmp ult i32 %x, %y 316; CHECK-NEXT: ret i1 [[TMP1]] 317; 318 %1 = icmp ult i32 %x, %y 319 %2 = icmp ne i32 %y, 0 320 %3 = and i1 %1, %2 321 ret i1 %3 322} 323 324define i1 @and_icmp2(i32 %x, i32 %y) { 325; CHECK-LABEL: @and_icmp2( 326; CHECK-NEXT: ret i1 false 327; 328 %1 = icmp ult i32 %x, %y 329 %2 = icmp eq i32 %y, 0 330 %3 = and i1 %1, %2 331 ret i1 %3 332} 333 334define i1 @or_icmp1(i32 %x, i32 %y) { 335; CHECK-LABEL: @or_icmp1( 336; CHECK-NEXT: [[TMP1:%.*]] = icmp ne i32 %y, 0 337; CHECK-NEXT: ret i1 [[TMP1]] 338; 339 %1 = icmp ult i32 %x, %y 340 %2 = icmp ne i32 %y, 0 341 %3 = or i1 %1, %2 342 ret i1 %3 343} 344 345define i1 @or_icmp2(i32 %x, i32 %y) { 346; CHECK-LABEL: @or_icmp2( 347; CHECK-NEXT: ret i1 true 348; 349 %1 = icmp uge i32 %x, %y 350 %2 = icmp ne i32 %y, 0 351 %3 = or i1 %1, %2 352 ret i1 %3 353} 354 355define i1 @or_icmp3(i32 %x, i32 %y) { 356; CHECK-LABEL: @or_icmp3( 357; CHECK-NEXT: [[TMP1:%.*]] = icmp uge i32 %x, %y 358; CHECK-NEXT: ret i1 [[TMP1]] 359; 360 %1 = icmp uge i32 %x, %y 361 %2 = icmp eq i32 %y, 0 362 %3 = or i1 %1, %2 363 ret i1 %3 364} 365 366define i1 @disjoint_cmps(i32 %A) { 367; CHECK-LABEL: @disjoint_cmps( 368; CHECK-NEXT: ret i1 false 369; 370 %B = icmp eq i32 %A, 1 371 %C = icmp sge i32 %A, 3 372 %D = and i1 %B, %C 373 ret i1 %D 374} 375 376define i1 @disjoint_cmps2(i32 %X) { 377; CHECK-LABEL: @disjoint_cmps2( 378; CHECK-NEXT: ret i1 false 379; 380 %a = icmp ult i32 %X, 31 381 %b = icmp slt i32 %X, 0 382 %c = and i1 %a, %b 383 ret i1 %c 384} 385 386; PR27869 - Look through casts to eliminate cmps and bitwise logic. 387 388define i32 @and_of_zexted_icmps(i32 %i) { 389; CHECK-LABEL: @and_of_zexted_icmps( 390; CHECK-NEXT: ret i32 0 391; 392 %cmp0 = icmp eq i32 %i, 0 393 %conv0 = zext i1 %cmp0 to i32 394 %cmp1 = icmp ugt i32 %i, 4 395 %conv1 = zext i1 %cmp1 to i32 396 %and = and i32 %conv0, %conv1 397 ret i32 %and 398} 399 400; Make sure vectors work too. 401 402define <4 x i32> @and_of_zexted_icmps_vec(<4 x i32> %i) { 403; CHECK-LABEL: @and_of_zexted_icmps_vec( 404; CHECK-NEXT: ret <4 x i32> zeroinitializer 405; 406 %cmp0 = icmp eq <4 x i32> %i, zeroinitializer 407 %conv0 = zext <4 x i1> %cmp0 to <4 x i32> 408 %cmp1 = icmp slt <4 x i32> %i, zeroinitializer 409 %conv1 = zext <4 x i1> %cmp1 to <4 x i32> 410 %and = and <4 x i32> %conv0, %conv1 411 ret <4 x i32> %and 412} 413 414; Try a different cast and weird types. 415 416define i5 @and_of_sexted_icmps(i3 %i) { 417; CHECK-LABEL: @and_of_sexted_icmps( 418; CHECK-NEXT: ret i5 0 419; 420 %cmp0 = icmp eq i3 %i, 0 421 %conv0 = sext i1 %cmp0 to i5 422 %cmp1 = icmp ugt i3 %i, 1 423 %conv1 = sext i1 %cmp1 to i5 424 %and = and i5 %conv0, %conv1 425 ret i5 %and 426} 427 428; Try a different cast and weird vector types. 429 430define i3 @and_of_bitcast_icmps_vec(<3 x i65> %i) { 431; CHECK-LABEL: @and_of_bitcast_icmps_vec( 432; CHECK-NEXT: ret i3 0 433; 434 %cmp0 = icmp sgt <3 x i65> %i, zeroinitializer 435 %conv0 = bitcast <3 x i1> %cmp0 to i3 436 %cmp1 = icmp slt <3 x i65> %i, zeroinitializer 437 %conv1 = bitcast <3 x i1> %cmp1 to i3 438 %and = and i3 %conv0, %conv1 439 ret i3 %and 440} 441 442; We can't do this if the casts are different. 443 444define i16 @and_of_different_cast_icmps(i8 %i) { 445; CHECK-LABEL: @and_of_different_cast_icmps( 446; CHECK-NEXT: [[CMP0:%.*]] = icmp eq i8 %i, 0 447; CHECK-NEXT: [[CONV0:%.*]] = zext i1 [[CMP0]] to i16 448; CHECK-NEXT: [[CMP1:%.*]] = icmp eq i8 %i, 1 449; CHECK-NEXT: [[CONV1:%.*]] = sext i1 [[CMP1]] to i16 450; CHECK-NEXT: [[AND:%.*]] = and i16 [[CONV0]], [[CONV1]] 451; CHECK-NEXT: ret i16 [[AND]] 452; 453 %cmp0 = icmp eq i8 %i, 0 454 %conv0 = zext i1 %cmp0 to i16 455 %cmp1 = icmp eq i8 %i, 1 456 %conv1 = sext i1 %cmp1 to i16 457 %and = and i16 %conv0, %conv1 458 ret i16 %and 459} 460 461define <2 x i3> @and_of_different_cast_icmps_vec(<2 x i8> %i, <2 x i16> %j) { 462; CHECK-LABEL: @and_of_different_cast_icmps_vec( 463; CHECK-NEXT: [[CMP0:%.*]] = icmp eq <2 x i8> %i, zeroinitializer 464; CHECK-NEXT: [[CONV0:%.*]] = zext <2 x i1> [[CMP0]] to <2 x i3> 465; CHECK-NEXT: [[CMP1:%.*]] = icmp ugt <2 x i16> %j, <i16 1, i16 1> 466; CHECK-NEXT: [[CONV1:%.*]] = zext <2 x i1> [[CMP1]] to <2 x i3> 467; CHECK-NEXT: [[AND:%.*]] = and <2 x i3> [[CONV0]], [[CONV1]] 468; CHECK-NEXT: ret <2 x i3> [[AND]] 469; 470 %cmp0 = icmp eq <2 x i8> %i, zeroinitializer 471 %conv0 = zext <2 x i1> %cmp0 to <2 x i3> 472 %cmp1 = icmp ugt <2 x i16> %j, <i16 1, i16 1> 473 %conv1 = zext <2 x i1> %cmp1 to <2 x i3> 474 %and = and <2 x i3> %conv0, %conv1 475 ret <2 x i3> %and 476} 477 478