1; NOTE: Assertions have been autogenerated by utils/update_test_checks.py 2; RUN: opt -S -instcombine < %s | FileCheck %s 3 4; This is the canonical form for a type-changing min/max. 5define i64 @t1(i32 %a) { 6; CHECK-LABEL: @t1( 7; CHECK-NEXT: [[TMP1:%.*]] = icmp slt i32 [[A:%.*]], 5 8; CHECK-NEXT: [[TMP2:%.*]] = select i1 [[TMP1]], i32 [[A]], i32 5 9; CHECK-NEXT: [[TMP3:%.*]] = sext i32 [[TMP2]] to i64 10; CHECK-NEXT: ret i64 [[TMP3]] 11; 12 %1 = icmp slt i32 %a, 5 13 %2 = select i1 %1, i32 %a, i32 5 14 %3 = sext i32 %2 to i64 15 ret i64 %3 16} 17 18; Check this is converted into canonical form, as above. 19define i64 @t2(i32 %a) { 20; CHECK-LABEL: @t2( 21; CHECK-NEXT: [[TMP1:%.*]] = icmp slt i32 [[A:%.*]], 5 22; CHECK-NEXT: [[NARROW:%.*]] = select i1 [[TMP1]], i32 [[A]], i32 5 23; CHECK-NEXT: [[TMP2:%.*]] = sext i32 [[NARROW]] to i64 24; CHECK-NEXT: ret i64 [[TMP2]] 25; 26 %1 = icmp slt i32 %a, 5 27 %2 = sext i32 %a to i64 28 %3 = select i1 %1, i64 %2, i64 5 29 ret i64 %3 30} 31 32; Same as @t2, with flipped operands and zext instead of sext. 33define i64 @t3(i32 %a) { 34; CHECK-LABEL: @t3( 35; CHECK-NEXT: [[TMP1:%.*]] = icmp ugt i32 [[A:%.*]], 5 36; CHECK-NEXT: [[NARROW:%.*]] = select i1 [[TMP1]], i32 [[A]], i32 5 37; CHECK-NEXT: [[TMP2:%.*]] = zext i32 [[NARROW]] to i64 38; CHECK-NEXT: ret i64 [[TMP2]] 39; 40 %1 = icmp ult i32 %a, 5 41 %2 = zext i32 %a to i64 42 %3 = select i1 %1, i64 5, i64 %2 43 ret i64 %3 44} 45 46; Same again, with trunc. 47define i32 @t4(i64 %a) { 48; CHECK-LABEL: @t4( 49; CHECK-NEXT: [[TMP1:%.*]] = icmp slt i64 [[A:%.*]], 5 50; CHECK-NEXT: [[TMP2:%.*]] = select i1 [[TMP1]], i64 [[A]], i64 5 51; CHECK-NEXT: [[TMP3:%.*]] = trunc i64 [[TMP2]] to i32 52; CHECK-NEXT: ret i32 [[TMP3]] 53; 54 %1 = icmp slt i64 %a, 5 55 %2 = trunc i64 %a to i32 56 %3 = select i1 %1, i32 %2, i32 5 57 ret i32 %3 58} 59 60; Same as @t3, but with mismatched signedness between icmp and zext. 61define i64 @t5(i32 %a) { 62; CHECK-LABEL: @t5( 63; CHECK-NEXT: [[TMP1:%.*]] = icmp sgt i32 [[A:%.*]], 5 64; CHECK-NEXT: [[NARROW:%.*]] = select i1 [[TMP1]], i32 [[A]], i32 5 65; CHECK-NEXT: [[TMP2:%.*]] = zext i32 [[NARROW]] to i64 66; CHECK-NEXT: ret i64 [[TMP2]] 67; 68 %1 = icmp slt i32 %a, 5 69 %2 = zext i32 %a to i64 70 %3 = select i1 %1, i64 5, i64 %2 71 ret i64 %3 72} 73 74define float @t6(i32 %a) { 75; CHECK-LABEL: @t6( 76; CHECK-NEXT: [[TMP1:%.*]] = icmp slt i32 [[A:%.*]], 0 77; CHECK-NEXT: [[TMP2:%.*]] = select i1 [[TMP1]], i32 [[A]], i32 0 78; CHECK-NEXT: [[TMP3:%.*]] = sitofp i32 [[TMP2]] to float 79; CHECK-NEXT: ret float [[TMP3]] 80; 81 %1 = icmp slt i32 %a, 0 82 %2 = select i1 %1, i32 %a, i32 0 83 %3 = sitofp i32 %2 to float 84 ret float %3 85} 86 87define i16 @t7(i32 %a) { 88; CHECK-LABEL: @t7( 89; CHECK-NEXT: [[TMP1:%.*]] = icmp slt i32 [[A:%.*]], -32768 90; CHECK-NEXT: [[TMP2:%.*]] = select i1 [[TMP1]], i32 [[A]], i32 -32768 91; CHECK-NEXT: [[TMP3:%.*]] = trunc i32 [[TMP2]] to i16 92; CHECK-NEXT: ret i16 [[TMP3]] 93; 94 %1 = icmp slt i32 %a, -32768 95 %2 = trunc i32 %a to i16 96 %3 = select i1 %1, i16 %2, i16 -32768 97 ret i16 %3 98} 99 100; Just check for no infinite loop. InstSimplify liked to 101; "simplify" -32767 by removing all the sign bits, 102; which led to a canonicalization fight between different 103; parts of instcombine. 104define i32 @t8(i64 %a, i32 %b) { 105; CHECK-LABEL: @t8( 106; CHECK-NEXT: [[TMP1:%.*]] = icmp slt i64 [[A:%.*]], -32767 107; CHECK-NEXT: [[TMP2:%.*]] = select i1 [[TMP1]], i64 [[A]], i64 -32767 108; CHECK-NEXT: [[TMP3:%.*]] = trunc i64 [[TMP2]] to i32 109; CHECK-NEXT: [[TMP4:%.*]] = icmp slt i32 [[B:%.*]], 42 110; CHECK-NEXT: [[TMP5:%.*]] = select i1 [[TMP4]], i32 42, i32 [[TMP3]] 111; CHECK-NEXT: [[TMP6:%.*]] = icmp ne i32 [[TMP5]], [[B]] 112; CHECK-NEXT: [[TMP7:%.*]] = zext i1 [[TMP6]] to i32 113; CHECK-NEXT: ret i32 [[TMP7]] 114; 115 %1 = icmp slt i64 %a, -32767 116 %2 = select i1 %1, i64 %a, i64 -32767 117 %3 = trunc i64 %2 to i32 118 %4 = icmp slt i32 %b, 42 119 %5 = select i1 %4, i32 42, i32 %3 120 %6 = icmp ne i32 %5, %b 121 %7 = zext i1 %6 to i32 122 ret i32 %7 123} 124 125; Ensure this doesn't get converted to a min/max. 126define i64 @t9(i32 %a) { 127; CHECK-LABEL: @t9( 128; CHECK-NEXT: [[TMP1:%.*]] = icmp sgt i32 [[A:%.*]], -1 129; CHECK-NEXT: [[TMP2:%.*]] = sext i32 [[A]] to i64 130; CHECK-NEXT: [[TMP3:%.*]] = select i1 [[TMP1]], i64 [[TMP2]], i64 4294967295 131; CHECK-NEXT: ret i64 [[TMP3]] 132; 133 %1 = icmp sgt i32 %a, -1 134 %2 = sext i32 %a to i64 135 %3 = select i1 %1, i64 %2, i64 4294967295 136 ret i64 %3 137} 138 139define float @t10(i32 %x) { 140; CHECK-LABEL: @t10( 141; CHECK-NEXT: [[TMP1:%.*]] = icmp sgt i32 [[X:%.*]], 255 142; CHECK-NEXT: [[R1:%.*]] = select i1 [[TMP1]], i32 [[X]], i32 255 143; CHECK-NEXT: [[TMP2:%.*]] = sitofp i32 [[R1]] to float 144; CHECK-NEXT: ret float [[TMP2]] 145; 146 %f_x = sitofp i32 %x to float 147 %cmp = icmp sgt i32 %x, 255 148 %r = select i1 %cmp, float %f_x, float 255.0 149 ret float %r 150} 151 152define float @t11(i64 %x) { 153; CHECK-LABEL: @t11( 154; CHECK-NEXT: [[TMP1:%.*]] = icmp sgt i64 [[X:%.*]], 255 155; CHECK-NEXT: [[R1:%.*]] = select i1 [[TMP1]], i64 [[X]], i64 255 156; CHECK-NEXT: [[TMP2:%.*]] = sitofp i64 [[R1]] to float 157; CHECK-NEXT: ret float [[TMP2]] 158; 159 %f_x = sitofp i64 %x to float 160 %cmp = icmp sgt i64 %x, 255 161 %r = select i1 %cmp, float %f_x, float 255.0 162 ret float %r 163} 164 165; Reuse the first 2 bitcasts as the select operands. 166 167define <4 x i32> @bitcasts_fcmp_1(<2 x i64> %a, <2 x i64> %b) { 168; CHECK-LABEL: @bitcasts_fcmp_1( 169; CHECK-NEXT: [[T0:%.*]] = bitcast <2 x i64> [[A:%.*]] to <4 x float> 170; CHECK-NEXT: [[T1:%.*]] = bitcast <2 x i64> [[B:%.*]] to <4 x float> 171; CHECK-NEXT: [[T2:%.*]] = fcmp olt <4 x float> [[T1]], [[T0]] 172; CHECK-NEXT: [[TMP1:%.*]] = select <4 x i1> [[T2]], <4 x float> [[T0]], <4 x float> [[T1]] 173; CHECK-NEXT: [[T5:%.*]] = bitcast <4 x float> [[TMP1]] to <4 x i32> 174; CHECK-NEXT: ret <4 x i32> [[T5]] 175; 176 %t0 = bitcast <2 x i64> %a to <4 x float> 177 %t1 = bitcast <2 x i64> %b to <4 x float> 178 %t2 = fcmp olt <4 x float> %t1, %t0 179 %t3 = bitcast <2 x i64> %a to <4 x i32> 180 %t4 = bitcast <2 x i64> %b to <4 x i32> 181 %t5 = select <4 x i1> %t2, <4 x i32> %t3, <4 x i32> %t4 182 ret <4 x i32> %t5 183} 184 185; Switch cmp operand order. 186 187define <4 x i32> @bitcasts_fcmp_2(<2 x i64> %a, <2 x i64> %b) { 188; CHECK-LABEL: @bitcasts_fcmp_2( 189; CHECK-NEXT: [[T0:%.*]] = bitcast <2 x i64> [[A:%.*]] to <4 x float> 190; CHECK-NEXT: [[T1:%.*]] = bitcast <2 x i64> [[B:%.*]] to <4 x float> 191; CHECK-NEXT: [[T2:%.*]] = fcmp olt <4 x float> [[T0]], [[T1]] 192; CHECK-NEXT: [[TMP1:%.*]] = select <4 x i1> [[T2]], <4 x float> [[T0]], <4 x float> [[T1]] 193; CHECK-NEXT: [[T5:%.*]] = bitcast <4 x float> [[TMP1]] to <4 x i32> 194; CHECK-NEXT: ret <4 x i32> [[T5]] 195; 196 %t0 = bitcast <2 x i64> %a to <4 x float> 197 %t1 = bitcast <2 x i64> %b to <4 x float> 198 %t2 = fcmp olt <4 x float> %t0, %t1 199 %t3 = bitcast <2 x i64> %a to <4 x i32> 200 %t4 = bitcast <2 x i64> %b to <4 x i32> 201 %t5 = select <4 x i1> %t2, <4 x i32> %t3, <4 x i32> %t4 202 ret <4 x i32> %t5 203} 204 205; Integer cmp should have the same transforms. 206 207define <4 x float> @bitcasts_icmp(<2 x i64> %a, <2 x i64> %b) { 208; CHECK-LABEL: @bitcasts_icmp( 209; CHECK-NEXT: [[T0:%.*]] = bitcast <2 x i64> [[A:%.*]] to <4 x i32> 210; CHECK-NEXT: [[T1:%.*]] = bitcast <2 x i64> [[B:%.*]] to <4 x i32> 211; CHECK-NEXT: [[T2:%.*]] = icmp slt <4 x i32> [[T1]], [[T0]] 212; CHECK-NEXT: [[TMP1:%.*]] = select <4 x i1> [[T2]], <4 x i32> [[T0]], <4 x i32> [[T1]] 213; CHECK-NEXT: [[T5:%.*]] = bitcast <4 x i32> [[TMP1]] to <4 x float> 214; CHECK-NEXT: ret <4 x float> [[T5]] 215; 216 %t0 = bitcast <2 x i64> %a to <4 x i32> 217 %t1 = bitcast <2 x i64> %b to <4 x i32> 218 %t2 = icmp slt <4 x i32> %t1, %t0 219 %t3 = bitcast <2 x i64> %a to <4 x float> 220 %t4 = bitcast <2 x i64> %b to <4 x float> 221 %t5 = select <4 x i1> %t2, <4 x float> %t3, <4 x float> %t4 222 ret <4 x float> %t5 223} 224 225; SMIN(SMIN(X, 11), 92) -> SMIN(X, 11) 226define i32 @test68(i32 %x) { 227; CHECK-LABEL: @test68( 228; CHECK-NEXT: [[TMP1:%.*]] = icmp slt i32 [[X:%.*]], 11 229; CHECK-NEXT: [[COND:%.*]] = select i1 [[TMP1]], i32 [[X]], i32 11 230; CHECK-NEXT: ret i32 [[COND]] 231; 232 %cmp = icmp slt i32 11, %x 233 %cond = select i1 %cmp, i32 11, i32 %x 234 %cmp3 = icmp slt i32 92, %cond 235 %retval = select i1 %cmp3, i32 92, i32 %cond 236 ret i32 %retval 237} 238 239define <2 x i32> @test68vec(<2 x i32> %x) { 240; CHECK-LABEL: @test68vec( 241; CHECK-NEXT: [[TMP1:%.*]] = icmp slt <2 x i32> [[X:%.*]], <i32 11, i32 11> 242; CHECK-NEXT: [[COND:%.*]] = select <2 x i1> [[TMP1]], <2 x i32> [[X]], <2 x i32> <i32 11, i32 11> 243; CHECK-NEXT: ret <2 x i32> [[COND]] 244; 245 %cmp = icmp slt <2 x i32> <i32 11, i32 11>, %x 246 %cond = select <2 x i1> %cmp, <2 x i32> <i32 11, i32 11>, <2 x i32> %x 247 %cmp3 = icmp slt <2 x i32> <i32 92, i32 92>, %cond 248 %retval = select <2 x i1> %cmp3, <2 x i32> <i32 92, i32 92>, <2 x i32> %cond 249 ret <2 x i32> %retval 250} 251 252; MIN(MIN(X, 24), 83) -> MIN(X, 24) 253define i32 @test69(i32 %x) { 254; CHECK-LABEL: @test69( 255; CHECK-NEXT: [[TMP1:%.*]] = icmp ult i32 [[X:%.*]], 24 256; CHECK-NEXT: [[COND:%.*]] = select i1 [[TMP1]], i32 [[X]], i32 24 257; CHECK-NEXT: ret i32 [[COND]] 258; 259 %cmp = icmp ult i32 24, %x 260 %cond = select i1 %cmp, i32 24, i32 %x 261 %cmp3 = icmp ult i32 83, %cond 262 %retval = select i1 %cmp3, i32 83, i32 %cond 263 ret i32 %retval 264} 265 266; SMAX(SMAX(X, 75), 36) -> SMAX(X, 75) 267define i32 @test70(i32 %x) { 268; CHECK-LABEL: @test70( 269; CHECK-NEXT: [[TMP1:%.*]] = icmp sgt i32 [[X:%.*]], 75 270; CHECK-NEXT: [[COND:%.*]] = select i1 [[TMP1]], i32 [[X]], i32 75 271; CHECK-NEXT: ret i32 [[COND]] 272; 273 %cmp = icmp slt i32 %x, 75 274 %cond = select i1 %cmp, i32 75, i32 %x 275 %cmp3 = icmp slt i32 %cond, 36 276 %retval = select i1 %cmp3, i32 36, i32 %cond 277 ret i32 %retval 278} 279 280; MAX(MAX(X, 68), 47) -> MAX(X, 68) 281define i32 @test71(i32 %x) { 282; CHECK-LABEL: @test71( 283; CHECK-NEXT: [[TMP1:%.*]] = icmp ugt i32 [[X:%.*]], 68 284; CHECK-NEXT: [[COND:%.*]] = select i1 [[TMP1]], i32 [[X]], i32 68 285; CHECK-NEXT: ret i32 [[COND]] 286; 287 %cmp = icmp ult i32 %x, 68 288 %cond = select i1 %cmp, i32 68, i32 %x 289 %cmp3 = icmp ult i32 %cond, 47 290 %retval = select i1 %cmp3, i32 47, i32 %cond 291 ret i32 %retval 292} 293 294; SMIN(SMIN(X, 92), 11) -> SMIN(X, 11) 295define i32 @test72(i32 %x) { 296; CHECK-LABEL: @test72( 297; CHECK-NEXT: [[TMP1:%.*]] = icmp slt i32 [[X:%.*]], 11 298; CHECK-NEXT: [[RETVAL:%.*]] = select i1 [[TMP1]], i32 [[X]], i32 11 299; CHECK-NEXT: ret i32 [[RETVAL]] 300; 301 %cmp = icmp sgt i32 %x, 92 302 %cond = select i1 %cmp, i32 92, i32 %x 303 %cmp3 = icmp sgt i32 %cond, 11 304 %retval = select i1 %cmp3, i32 11, i32 %cond 305 ret i32 %retval 306} 307 308define <2 x i32> @test72vec(<2 x i32> %x) { 309; CHECK-LABEL: @test72vec( 310; CHECK-NEXT: [[TMP1:%.*]] = icmp slt <2 x i32> [[X:%.*]], <i32 11, i32 11> 311; CHECK-NEXT: [[RETVAL:%.*]] = select <2 x i1> [[TMP1]], <2 x i32> [[X]], <2 x i32> <i32 11, i32 11> 312; CHECK-NEXT: ret <2 x i32> [[RETVAL]] 313; 314 %cmp = icmp sgt <2 x i32> %x, <i32 92, i32 92> 315 %cond = select <2 x i1> %cmp, <2 x i32> <i32 92, i32 92>, <2 x i32> %x 316 %cmp3 = icmp sgt <2 x i32> %cond, <i32 11, i32 11> 317 %retval = select <2 x i1> %cmp3, <2 x i32> <i32 11, i32 11>, <2 x i32> %cond 318 ret <2 x i32> %retval 319} 320 321; MIN(MIN(X, 83), 24) -> MIN(X, 24) 322define i32 @test73(i32 %x) { 323; CHECK-LABEL: @test73( 324; CHECK-NEXT: [[TMP1:%.*]] = icmp ult i32 [[X:%.*]], 24 325; CHECK-NEXT: [[RETVAL:%.*]] = select i1 [[TMP1]], i32 [[X]], i32 24 326; CHECK-NEXT: ret i32 [[RETVAL]] 327; 328 %cmp = icmp ugt i32 %x, 83 329 %cond = select i1 %cmp, i32 83, i32 %x 330 %cmp3 = icmp ugt i32 %cond, 24 331 %retval = select i1 %cmp3, i32 24, i32 %cond 332 ret i32 %retval 333} 334 335; SMAX(SMAX(X, 36), 75) -> SMAX(X, 75) 336define i32 @test74(i32 %x) { 337; CHECK-LABEL: @test74( 338; CHECK-NEXT: [[TMP1:%.*]] = icmp sgt i32 [[X:%.*]], 75 339; CHECK-NEXT: [[RETVAL:%.*]] = select i1 [[TMP1]], i32 [[X]], i32 75 340; CHECK-NEXT: ret i32 [[RETVAL]] 341; 342 %cmp = icmp slt i32 %x, 36 343 %cond = select i1 %cmp, i32 36, i32 %x 344 %cmp3 = icmp slt i32 %cond, 75 345 %retval = select i1 %cmp3, i32 75, i32 %cond 346 ret i32 %retval 347} 348 349; MAX(MAX(X, 47), 68) -> MAX(X, 68) 350define i32 @test75(i32 %x) { 351; CHECK-LABEL: @test75( 352; CHECK-NEXT: [[TMP1:%.*]] = icmp ugt i32 [[X:%.*]], 68 353; CHECK-NEXT: [[RETVAL:%.*]] = select i1 [[TMP1]], i32 [[X]], i32 68 354; CHECK-NEXT: ret i32 [[RETVAL]] 355; 356 %cmp = icmp ult i32 %x, 47 357 %cond = select i1 %cmp, i32 47, i32 %x 358 %cmp3 = icmp ult i32 %cond, 68 359 %retval = select i1 %cmp3, i32 68, i32 %cond 360 ret i32 %retval 361} 362 363; The next 10 tests are value clamping with constants: 364; https://llvm.org/bugs/show_bug.cgi?id=31693 365 366; (X <s C1) ? C1 : SMIN(X, C2) ==> SMAX(SMIN(X, C2), C1) 367 368define i32 @clamp_signed1(i32 %x) { 369; CHECK-LABEL: @clamp_signed1( 370; CHECK-NEXT: [[CMP2:%.*]] = icmp slt i32 [[X:%.*]], 255 371; CHECK-NEXT: [[MIN:%.*]] = select i1 [[CMP2]], i32 [[X]], i32 255 372; CHECK-NEXT: [[TMP1:%.*]] = icmp sgt i32 [[MIN]], 15 373; CHECK-NEXT: [[R:%.*]] = select i1 [[TMP1]], i32 [[MIN]], i32 15 374; CHECK-NEXT: ret i32 [[R]] 375; 376 %cmp2 = icmp slt i32 %x, 255 377 %min = select i1 %cmp2, i32 %x, i32 255 378 %cmp1 = icmp slt i32 %x, 15 379 %r = select i1 %cmp1, i32 15, i32 %min 380 ret i32 %r 381} 382 383; (X >s C1) ? C1 : SMAX(X, C2) ==> SMIN(SMAX(X, C2), C1) 384 385define i32 @clamp_signed2(i32 %x) { 386; CHECK-LABEL: @clamp_signed2( 387; CHECK-NEXT: [[CMP2:%.*]] = icmp sgt i32 [[X:%.*]], 15 388; CHECK-NEXT: [[MAX:%.*]] = select i1 [[CMP2]], i32 [[X]], i32 15 389; CHECK-NEXT: [[TMP1:%.*]] = icmp slt i32 [[MAX]], 255 390; CHECK-NEXT: [[R:%.*]] = select i1 [[TMP1]], i32 [[MAX]], i32 255 391; CHECK-NEXT: ret i32 [[R]] 392; 393 %cmp2 = icmp sgt i32 %x, 15 394 %max = select i1 %cmp2, i32 %x, i32 15 395 %cmp1 = icmp sgt i32 %x, 255 396 %r = select i1 %cmp1, i32 255, i32 %max 397 ret i32 %r 398} 399 400; (X >s C1) ? SMIN(X, C2) : C1 ==> SMAX(SMIN(X, C2), C1) 401 402define i32 @clamp_signed3(i32 %x) { 403; CHECK-LABEL: @clamp_signed3( 404; CHECK-NEXT: [[CMP2:%.*]] = icmp slt i32 [[X:%.*]], 255 405; CHECK-NEXT: [[MIN:%.*]] = select i1 [[CMP2]], i32 [[X]], i32 255 406; CHECK-NEXT: [[TMP1:%.*]] = icmp sgt i32 [[MIN]], 15 407; CHECK-NEXT: [[R:%.*]] = select i1 [[TMP1]], i32 [[MIN]], i32 15 408; CHECK-NEXT: ret i32 [[R]] 409; 410 %cmp2 = icmp slt i32 %x, 255 411 %min = select i1 %cmp2, i32 %x, i32 255 412 %cmp1 = icmp sgt i32 %x, 15 413 %r = select i1 %cmp1, i32 %min, i32 15 414 ret i32 %r 415} 416 417; (X <s C1) ? SMAX(X, C2) : C1 ==> SMIN(SMAX(X, C1), C2) 418 419define i32 @clamp_signed4(i32 %x) { 420; CHECK-LABEL: @clamp_signed4( 421; CHECK-NEXT: [[CMP2:%.*]] = icmp sgt i32 [[X:%.*]], 15 422; CHECK-NEXT: [[MAX:%.*]] = select i1 [[CMP2]], i32 [[X]], i32 15 423; CHECK-NEXT: [[TMP1:%.*]] = icmp slt i32 [[MAX]], 255 424; CHECK-NEXT: [[R:%.*]] = select i1 [[TMP1]], i32 [[MAX]], i32 255 425; CHECK-NEXT: ret i32 [[R]] 426; 427 %cmp2 = icmp sgt i32 %x, 15 428 %max = select i1 %cmp2, i32 %x, i32 15 429 %cmp1 = icmp slt i32 %x, 255 430 %r = select i1 %cmp1, i32 %max, i32 255 431 ret i32 %r 432} 433 434; (X <u C1) ? C1 : UMIN(X, C2) ==> UMAX(UMIN(X, C2), C1) 435 436define i32 @clamp_unsigned1(i32 %x) { 437; CHECK-LABEL: @clamp_unsigned1( 438; CHECK-NEXT: [[CMP2:%.*]] = icmp ult i32 [[X:%.*]], 255 439; CHECK-NEXT: [[MIN:%.*]] = select i1 [[CMP2]], i32 [[X]], i32 255 440; CHECK-NEXT: [[TMP1:%.*]] = icmp ugt i32 [[MIN]], 15 441; CHECK-NEXT: [[R:%.*]] = select i1 [[TMP1]], i32 [[MIN]], i32 15 442; CHECK-NEXT: ret i32 [[R]] 443; 444 %cmp2 = icmp ult i32 %x, 255 445 %min = select i1 %cmp2, i32 %x, i32 255 446 %cmp1 = icmp ult i32 %x, 15 447 %r = select i1 %cmp1, i32 15, i32 %min 448 ret i32 %r 449} 450 451; (X >u C1) ? C1 : UMAX(X, C2) ==> UMIN(UMAX(X, C2), C1) 452 453define i32 @clamp_unsigned2(i32 %x) { 454; CHECK-LABEL: @clamp_unsigned2( 455; CHECK-NEXT: [[CMP2:%.*]] = icmp ugt i32 [[X:%.*]], 15 456; CHECK-NEXT: [[MAX:%.*]] = select i1 [[CMP2]], i32 [[X]], i32 15 457; CHECK-NEXT: [[TMP1:%.*]] = icmp ult i32 [[MAX]], 255 458; CHECK-NEXT: [[R:%.*]] = select i1 [[TMP1]], i32 [[MAX]], i32 255 459; CHECK-NEXT: ret i32 [[R]] 460; 461 %cmp2 = icmp ugt i32 %x, 15 462 %max = select i1 %cmp2, i32 %x, i32 15 463 %cmp1 = icmp ugt i32 %x, 255 464 %r = select i1 %cmp1, i32 255, i32 %max 465 ret i32 %r 466} 467 468; (X >u C1) ? UMIN(X, C2) : C1 ==> UMAX(UMIN(X, C2), C1) 469 470define i32 @clamp_unsigned3(i32 %x) { 471; CHECK-LABEL: @clamp_unsigned3( 472; CHECK-NEXT: [[CMP2:%.*]] = icmp ult i32 [[X:%.*]], 255 473; CHECK-NEXT: [[MIN:%.*]] = select i1 [[CMP2]], i32 [[X]], i32 255 474; CHECK-NEXT: [[TMP1:%.*]] = icmp ugt i32 [[MIN]], 15 475; CHECK-NEXT: [[R:%.*]] = select i1 [[TMP1]], i32 [[MIN]], i32 15 476; CHECK-NEXT: ret i32 [[R]] 477; 478 %cmp2 = icmp ult i32 %x, 255 479 %min = select i1 %cmp2, i32 %x, i32 255 480 %cmp1 = icmp ugt i32 %x, 15 481 %r = select i1 %cmp1, i32 %min, i32 15 482 ret i32 %r 483} 484 485; (X <u C1) ? UMAX(X, C2) : C1 ==> UMIN(UMAX(X, C2), C1) 486 487define i32 @clamp_unsigned4(i32 %x) { 488; CHECK-LABEL: @clamp_unsigned4( 489; CHECK-NEXT: [[CMP2:%.*]] = icmp ugt i32 [[X:%.*]], 15 490; CHECK-NEXT: [[MAX:%.*]] = select i1 [[CMP2]], i32 [[X]], i32 15 491; CHECK-NEXT: [[TMP1:%.*]] = icmp ult i32 [[MAX]], 255 492; CHECK-NEXT: [[R:%.*]] = select i1 [[TMP1]], i32 [[MAX]], i32 255 493; CHECK-NEXT: ret i32 [[R]] 494; 495 %cmp2 = icmp ugt i32 %x, 15 496 %max = select i1 %cmp2, i32 %x, i32 15 497 %cmp1 = icmp ult i32 %x, 255 498 %r = select i1 %cmp1, i32 %max, i32 255 499 ret i32 %r 500} 501 502; Check that clamp is recognized and there is no infinite 503; loop because of reverse cmp transformation: 504; (icmp sgt smin(PositiveA, B) 0) -> (icmp sgt B 0) 505define i32 @clamp_check_for_no_infinite_loop1(i32 %i) { 506; CHECK-LABEL: @clamp_check_for_no_infinite_loop1( 507; CHECK-NEXT: [[CMP1:%.*]] = icmp slt i32 [[I:%.*]], 255 508; CHECK-NEXT: [[SEL1:%.*]] = select i1 [[CMP1]], i32 [[I]], i32 255 509; CHECK-NEXT: [[TMP1:%.*]] = icmp sgt i32 [[SEL1]], 0 510; CHECK-NEXT: [[RES:%.*]] = select i1 [[TMP1]], i32 [[SEL1]], i32 0 511; CHECK-NEXT: ret i32 [[RES]] 512; 513 %cmp1 = icmp slt i32 %i, 255 514 %sel1 = select i1 %cmp1, i32 %i, i32 255 515 %cmp2 = icmp slt i32 %i, 0 516 %res = select i1 %cmp2, i32 0, i32 %sel1 517 ret i32 %res 518} 519; Check that there is no infinite loop in case of: 520; (icmp slt smax(NegativeA, B) 0) -> (icmp slt B 0) 521define i32 @clamp_check_for_no_infinite_loop2(i32 %i) { 522; CHECK-LABEL: @clamp_check_for_no_infinite_loop2( 523; CHECK-NEXT: [[CMP1:%.*]] = icmp sgt i32 [[I:%.*]], -255 524; CHECK-NEXT: [[SEL1:%.*]] = select i1 [[CMP1]], i32 [[I]], i32 -255 525; CHECK-NEXT: [[TMP1:%.*]] = icmp slt i32 [[SEL1]], 0 526; CHECK-NEXT: [[RES:%.*]] = select i1 [[TMP1]], i32 [[SEL1]], i32 0 527; CHECK-NEXT: ret i32 [[RES]] 528; 529 %cmp1 = icmp sgt i32 %i, -255 530 %sel1 = select i1 %cmp1, i32 %i, i32 -255 531 %cmp2 = icmp slt i32 %i, 0 532 %res = select i1 %cmp2, i32 %sel1, i32 0 533 ret i32 %res 534} 535 536; The next 3 min tests should canonicalize to the same form...and not infinite loop. 537 538define double @PR31751_umin1(i32 %x) { 539; CHECK-LABEL: @PR31751_umin1( 540; CHECK-NEXT: [[TMP1:%.*]] = icmp ult i32 [[X:%.*]], 2147483647 541; CHECK-NEXT: [[SEL:%.*]] = select i1 [[TMP1]], i32 [[X]], i32 2147483647 542; CHECK-NEXT: [[CONV:%.*]] = sitofp i32 [[SEL]] to double 543; CHECK-NEXT: ret double [[CONV]] 544; 545 %cmp = icmp slt i32 %x, 0 546 %sel = select i1 %cmp, i32 2147483647, i32 %x 547 %conv = sitofp i32 %sel to double 548 ret double %conv 549} 550 551define double @PR31751_umin2(i32 %x) { 552; CHECK-LABEL: @PR31751_umin2( 553; CHECK-NEXT: [[CMP:%.*]] = icmp ult i32 [[X:%.*]], 2147483647 554; CHECK-NEXT: [[SEL:%.*]] = select i1 [[CMP]], i32 [[X]], i32 2147483647 555; CHECK-NEXT: [[CONV:%.*]] = sitofp i32 [[SEL]] to double 556; CHECK-NEXT: ret double [[CONV]] 557; 558 %cmp = icmp ult i32 %x, 2147483647 559 %sel = select i1 %cmp, i32 %x, i32 2147483647 560 %conv = sitofp i32 %sel to double 561 ret double %conv 562} 563 564define double @PR31751_umin3(i32 %x) { 565; CHECK-LABEL: @PR31751_umin3( 566; CHECK-NEXT: [[TMP1:%.*]] = icmp ult i32 [[X:%.*]], 2147483647 567; CHECK-NEXT: [[SEL:%.*]] = select i1 [[TMP1]], i32 [[X]], i32 2147483647 568; CHECK-NEXT: [[CONV:%.*]] = sitofp i32 [[SEL]] to double 569; CHECK-NEXT: ret double [[CONV]] 570; 571 %cmp = icmp ugt i32 %x, 2147483647 572 %sel = select i1 %cmp, i32 2147483647, i32 %x 573 %conv = sitofp i32 %sel to double 574 ret double %conv 575} 576 577; The next 3 max tests should canonicalize to the same form...and not infinite loop. 578 579define double @PR31751_umax1(i32 %x) { 580; CHECK-LABEL: @PR31751_umax1( 581; CHECK-NEXT: [[TMP1:%.*]] = icmp ugt i32 [[X:%.*]], -2147483648 582; CHECK-NEXT: [[SEL:%.*]] = select i1 [[TMP1]], i32 [[X]], i32 -2147483648 583; CHECK-NEXT: [[CONV:%.*]] = sitofp i32 [[SEL]] to double 584; CHECK-NEXT: ret double [[CONV]] 585; 586 %cmp = icmp sgt i32 %x, -1 587 %sel = select i1 %cmp, i32 2147483648, i32 %x 588 %conv = sitofp i32 %sel to double 589 ret double %conv 590} 591 592define double @PR31751_umax2(i32 %x) { 593; CHECK-LABEL: @PR31751_umax2( 594; CHECK-NEXT: [[CMP:%.*]] = icmp ugt i32 [[X:%.*]], -2147483648 595; CHECK-NEXT: [[SEL:%.*]] = select i1 [[CMP]], i32 [[X]], i32 -2147483648 596; CHECK-NEXT: [[CONV:%.*]] = sitofp i32 [[SEL]] to double 597; CHECK-NEXT: ret double [[CONV]] 598; 599 %cmp = icmp ugt i32 %x, 2147483648 600 %sel = select i1 %cmp, i32 %x, i32 2147483648 601 %conv = sitofp i32 %sel to double 602 ret double %conv 603} 604 605define double @PR31751_umax3(i32 %x) { 606; CHECK-LABEL: @PR31751_umax3( 607; CHECK-NEXT: [[TMP1:%.*]] = icmp ugt i32 [[X:%.*]], -2147483648 608; CHECK-NEXT: [[SEL:%.*]] = select i1 [[TMP1]], i32 [[X]], i32 -2147483648 609; CHECK-NEXT: [[CONV:%.*]] = sitofp i32 [[SEL]] to double 610; CHECK-NEXT: ret double [[CONV]] 611; 612 %cmp = icmp ult i32 %x, 2147483648 613 %sel = select i1 %cmp, i32 2147483648, i32 %x 614 %conv = sitofp i32 %sel to double 615 ret double %conv 616} 617 618; The icmp/select form a canonical smax, so don't hide that by folding the final bitcast into the select. 619 620define float @bitcast_scalar_smax(float %x, float %y) { 621; CHECK-LABEL: @bitcast_scalar_smax( 622; CHECK-NEXT: [[BCX:%.*]] = bitcast float [[X:%.*]] to i32 623; CHECK-NEXT: [[BCY:%.*]] = bitcast float [[Y:%.*]] to i32 624; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i32 [[BCX]], [[BCY]] 625; CHECK-NEXT: [[SEL:%.*]] = select i1 [[CMP]], i32 [[BCX]], i32 [[BCY]] 626; CHECK-NEXT: [[BCS:%.*]] = bitcast i32 [[SEL]] to float 627; CHECK-NEXT: ret float [[BCS]] 628; 629 %bcx = bitcast float %x to i32 630 %bcy = bitcast float %y to i32 631 %cmp = icmp sgt i32 %bcx, %bcy 632 %sel = select i1 %cmp, i32 %bcx, i32 %bcy 633 %bcs = bitcast i32 %sel to float 634 ret float %bcs 635} 636 637; FIXME: Create a canonical umax by bitcasting the select. 638 639define float @bitcast_scalar_umax(float %x, float %y) { 640; CHECK-LABEL: @bitcast_scalar_umax( 641; CHECK-NEXT: [[BCX:%.*]] = bitcast float [[X:%.*]] to i32 642; CHECK-NEXT: [[BCY:%.*]] = bitcast float [[Y:%.*]] to i32 643; CHECK-NEXT: [[CMP:%.*]] = icmp ugt i32 [[BCX]], [[BCY]] 644; CHECK-NEXT: [[SEL:%.*]] = select i1 [[CMP]], float [[X]], float [[Y]] 645; CHECK-NEXT: ret float [[SEL]] 646; 647 %bcx = bitcast float %x to i32 648 %bcy = bitcast float %y to i32 649 %cmp = icmp ugt i32 %bcx, %bcy 650 %sel = select i1 %cmp, float %x, float %y 651 ret float %sel 652} 653 654; PR32306 - https://bugs.llvm.org/show_bug.cgi?id=32306 655; The icmp/select form a canonical smin, so don't hide that by folding the final bitcast into the select. 656 657define <8 x float> @bitcast_vector_smin(<8 x float> %x, <8 x float> %y) { 658; CHECK-LABEL: @bitcast_vector_smin( 659; CHECK-NEXT: [[BCX:%.*]] = bitcast <8 x float> [[X:%.*]] to <8 x i32> 660; CHECK-NEXT: [[BCY:%.*]] = bitcast <8 x float> [[Y:%.*]] to <8 x i32> 661; CHECK-NEXT: [[CMP:%.*]] = icmp slt <8 x i32> [[BCX]], [[BCY]] 662; CHECK-NEXT: [[SEL:%.*]] = select <8 x i1> [[CMP]], <8 x i32> [[BCX]], <8 x i32> [[BCY]] 663; CHECK-NEXT: [[BCS:%.*]] = bitcast <8 x i32> [[SEL]] to <8 x float> 664; CHECK-NEXT: ret <8 x float> [[BCS]] 665; 666 %bcx = bitcast <8 x float> %x to <8 x i32> 667 %bcy = bitcast <8 x float> %y to <8 x i32> 668 %cmp = icmp slt <8 x i32> %bcx, %bcy 669 %sel = select <8 x i1> %cmp, <8 x i32> %bcx, <8 x i32> %bcy 670 %bcs = bitcast <8 x i32> %sel to <8 x float> 671 ret <8 x float> %bcs 672} 673 674; FIXME: Create a canonical umin by bitcasting the select. 675 676define <8 x float> @bitcast_vector_umin(<8 x float> %x, <8 x float> %y) { 677; CHECK-LABEL: @bitcast_vector_umin( 678; CHECK-NEXT: [[BCX:%.*]] = bitcast <8 x float> [[X:%.*]] to <8 x i32> 679; CHECK-NEXT: [[BCY:%.*]] = bitcast <8 x float> [[Y:%.*]] to <8 x i32> 680; CHECK-NEXT: [[CMP:%.*]] = icmp slt <8 x i32> [[BCX]], [[BCY]] 681; CHECK-NEXT: [[SEL:%.*]] = select <8 x i1> [[CMP]], <8 x float> [[X]], <8 x float> [[Y]] 682; CHECK-NEXT: ret <8 x float> [[SEL]] 683; 684 %bcx = bitcast <8 x float> %x to <8 x i32> 685 %bcy = bitcast <8 x float> %y to <8 x i32> 686 %cmp = icmp slt <8 x i32> %bcx, %bcy 687 %sel = select <8 x i1> %cmp, <8 x float> %x, <8 x float> %y 688 ret <8 x float> %sel 689} 690 691; Check that we look through cast and recognize min idiom. 692 693define zeroext i8 @look_through_cast1(i32 %x) { 694; CHECK-LABEL: @look_through_cast1( 695; CHECK-NEXT: [[TMP1:%.*]] = icmp slt i32 [[X:%.*]], 511 696; CHECK-NEXT: [[RES1:%.*]] = select i1 [[TMP1]], i32 [[X]], i32 511 697; CHECK-NEXT: [[TMP2:%.*]] = trunc i32 [[RES1]] to i8 698; CHECK-NEXT: ret i8 [[TMP2]] 699; 700 %cmp1 = icmp slt i32 %x, 511 701 %x_trunc = trunc i32 %x to i8 702 %res = select i1 %cmp1, i8 %x_trunc, i8 255 703 ret i8 %res 704} 705 706; Check that we look through cast but min is not recognized. 707 708define zeroext i8 @look_through_cast2(i32 %x) { 709; CHECK-LABEL: @look_through_cast2( 710; CHECK-NEXT: [[CMP1:%.*]] = icmp slt i32 [[X:%.*]], 510 711; CHECK-NEXT: [[X_TRUNC:%.*]] = trunc i32 [[X]] to i8 712; CHECK-NEXT: [[RES:%.*]] = select i1 [[CMP1]], i8 [[X_TRUNC]], i8 -1 713; CHECK-NEXT: ret i8 [[RES]] 714; 715 %cmp1 = icmp slt i32 %x, 510 716 %x_trunc = trunc i32 %x to i8 717 %res = select i1 %cmp1, i8 %x_trunc, i8 255 718 ret i8 %res 719} 720 721define <2 x i8> @min_through_cast_vec1(<2 x i32> %x) { 722; CHECK-LABEL: @min_through_cast_vec1( 723; CHECK-NEXT: [[TMP1:%.*]] = icmp slt <2 x i32> [[X:%.*]], <i32 510, i32 511> 724; CHECK-NEXT: [[RES1:%.*]] = select <2 x i1> [[TMP1]], <2 x i32> [[X]], <2 x i32> <i32 510, i32 511> 725; CHECK-NEXT: [[TMP2:%.*]] = trunc <2 x i32> [[RES1]] to <2 x i8> 726; CHECK-NEXT: ret <2 x i8> [[TMP2]] 727; 728 %cmp = icmp slt <2 x i32> %x, <i32 510, i32 511> 729 %x_trunc = trunc <2 x i32> %x to <2 x i8> 730 %res = select <2 x i1> %cmp, <2 x i8> %x_trunc, <2 x i8> <i8 254, i8 255> 731 ret <2 x i8> %res 732} 733 734define <2 x i8> @min_through_cast_vec2(<2 x i32> %x) { 735; CHECK-LABEL: @min_through_cast_vec2( 736; CHECK-NEXT: [[TMP1:%.*]] = icmp slt <2 x i32> [[X:%.*]], <i32 511, i32 511> 737; CHECK-NEXT: [[RES1:%.*]] = select <2 x i1> [[TMP1]], <2 x i32> [[X]], <2 x i32> <i32 511, i32 511> 738; CHECK-NEXT: [[TMP2:%.*]] = trunc <2 x i32> [[RES1]] to <2 x i8> 739; CHECK-NEXT: ret <2 x i8> [[TMP2]] 740; 741 %cmp = icmp slt <2 x i32> %x, <i32 511, i32 511> 742 %x_trunc = trunc <2 x i32> %x to <2 x i8> 743 %res = select <2 x i1> %cmp, <2 x i8> %x_trunc, <2 x i8> <i8 255, i8 255> 744 ret <2 x i8> %res 745} 746 747; Remove a min/max op in a sequence with a common operand. 748; PR35717: https://bugs.llvm.org/show_bug.cgi?id=35717 749 750; min(min(a, b), min(b, c)) --> min(min(a, b), c) 751 752define i32 @common_factor_smin(i32 %a, i32 %b, i32 %c) { 753; CHECK-LABEL: @common_factor_smin( 754; CHECK-NEXT: [[CMP_AB:%.*]] = icmp slt i32 [[A:%.*]], [[B:%.*]] 755; CHECK-NEXT: [[MIN_AB:%.*]] = select i1 [[CMP_AB]], i32 [[A]], i32 [[B]] 756; CHECK-NEXT: [[TMP1:%.*]] = icmp slt i32 [[MIN_AB]], [[C:%.*]] 757; CHECK-NEXT: [[MIN_ABC:%.*]] = select i1 [[TMP1]], i32 [[MIN_AB]], i32 [[C]] 758; CHECK-NEXT: ret i32 [[MIN_ABC]] 759; 760 %cmp_ab = icmp slt i32 %a, %b 761 %min_ab = select i1 %cmp_ab, i32 %a, i32 %b 762 %cmp_bc = icmp slt i32 %b, %c 763 %min_bc = select i1 %cmp_bc, i32 %b, i32 %c 764 %cmp_ab_bc = icmp slt i32 %min_ab, %min_bc 765 %min_abc = select i1 %cmp_ab_bc, i32 %min_ab, i32 %min_bc 766 ret i32 %min_abc 767} 768 769; max(max(a, b), max(c, b)) --> max(max(a, b), c) 770 771define <2 x i32> @common_factor_smax(<2 x i32> %a, <2 x i32> %b, <2 x i32> %c) { 772; CHECK-LABEL: @common_factor_smax( 773; CHECK-NEXT: [[CMP_AB:%.*]] = icmp sgt <2 x i32> [[A:%.*]], [[B:%.*]] 774; CHECK-NEXT: [[MAX_AB:%.*]] = select <2 x i1> [[CMP_AB]], <2 x i32> [[A]], <2 x i32> [[B]] 775; CHECK-NEXT: [[TMP1:%.*]] = icmp sgt <2 x i32> [[MAX_AB]], [[C:%.*]] 776; CHECK-NEXT: [[MAX_ABC:%.*]] = select <2 x i1> [[TMP1]], <2 x i32> [[MAX_AB]], <2 x i32> [[C]] 777; CHECK-NEXT: ret <2 x i32> [[MAX_ABC]] 778; 779 %cmp_ab = icmp sgt <2 x i32> %a, %b 780 %max_ab = select <2 x i1> %cmp_ab, <2 x i32> %a, <2 x i32> %b 781 %cmp_cb = icmp sgt <2 x i32> %c, %b 782 %max_cb = select <2 x i1> %cmp_cb, <2 x i32> %c, <2 x i32> %b 783 %cmp_ab_cb = icmp sgt <2 x i32> %max_ab, %max_cb 784 %max_abc = select <2 x i1> %cmp_ab_cb, <2 x i32> %max_ab, <2 x i32> %max_cb 785 ret <2 x i32> %max_abc 786} 787 788; min(min(b, c), min(a, b)) --> min(min(b, c), a) 789 790define <2 x i32> @common_factor_umin(<2 x i32> %a, <2 x i32> %b, <2 x i32> %c) { 791; CHECK-LABEL: @common_factor_umin( 792; CHECK-NEXT: [[CMP_BC:%.*]] = icmp ult <2 x i32> [[B:%.*]], [[C:%.*]] 793; CHECK-NEXT: [[MIN_BC:%.*]] = select <2 x i1> [[CMP_BC]], <2 x i32> [[B]], <2 x i32> [[C]] 794; CHECK-NEXT: [[TMP1:%.*]] = icmp ult <2 x i32> [[MIN_BC]], [[A:%.*]] 795; CHECK-NEXT: [[MIN_ABC:%.*]] = select <2 x i1> [[TMP1]], <2 x i32> [[MIN_BC]], <2 x i32> [[A]] 796; CHECK-NEXT: ret <2 x i32> [[MIN_ABC]] 797; 798 %cmp_bc = icmp ult <2 x i32> %b, %c 799 %min_bc = select <2 x i1> %cmp_bc, <2 x i32> %b, <2 x i32> %c 800 %cmp_ab = icmp ult <2 x i32> %a, %b 801 %min_ab = select <2 x i1> %cmp_ab, <2 x i32> %a, <2 x i32> %b 802 %cmp_bc_ab = icmp ult <2 x i32> %min_bc, %min_ab 803 %min_abc = select <2 x i1> %cmp_bc_ab, <2 x i32> %min_bc, <2 x i32> %min_ab 804 ret <2 x i32> %min_abc 805} 806 807; max(max(b, c), max(b, a)) --> max(max(b, c), a) 808 809define i32 @common_factor_umax(i32 %a, i32 %b, i32 %c) { 810; CHECK-LABEL: @common_factor_umax( 811; CHECK-NEXT: [[CMP_BC:%.*]] = icmp ugt i32 [[B:%.*]], [[C:%.*]] 812; CHECK-NEXT: [[MAX_BC:%.*]] = select i1 [[CMP_BC]], i32 [[B]], i32 [[C]] 813; CHECK-NEXT: [[TMP1:%.*]] = icmp ugt i32 [[MAX_BC]], [[A:%.*]] 814; CHECK-NEXT: [[MAX_ABC:%.*]] = select i1 [[TMP1]], i32 [[MAX_BC]], i32 [[A]] 815; CHECK-NEXT: ret i32 [[MAX_ABC]] 816; 817 %cmp_bc = icmp ugt i32 %b, %c 818 %max_bc = select i1 %cmp_bc, i32 %b, i32 %c 819 %cmp_ba = icmp ugt i32 %b, %a 820 %max_ba = select i1 %cmp_ba, i32 %b, i32 %a 821 %cmp_bc_ba = icmp ugt i32 %max_bc, %max_ba 822 %max_abc = select i1 %cmp_bc_ba, i32 %max_bc, i32 %max_ba 823 ret i32 %max_abc 824} 825 826declare void @extra_use(i32) 827 828define i32 @common_factor_umax_extra_use_lhs(i32 %a, i32 %b, i32 %c) { 829; CHECK-LABEL: @common_factor_umax_extra_use_lhs( 830; CHECK-NEXT: [[CMP_BC:%.*]] = icmp ugt i32 [[B:%.*]], [[C:%.*]] 831; CHECK-NEXT: [[MAX_BC:%.*]] = select i1 [[CMP_BC]], i32 [[B]], i32 [[C]] 832; CHECK-NEXT: [[TMP1:%.*]] = icmp ugt i32 [[MAX_BC]], [[A:%.*]] 833; CHECK-NEXT: [[MAX_ABC:%.*]] = select i1 [[TMP1]], i32 [[MAX_BC]], i32 [[A]] 834; CHECK-NEXT: call void @extra_use(i32 [[MAX_BC]]) 835; CHECK-NEXT: ret i32 [[MAX_ABC]] 836; 837 %cmp_bc = icmp ugt i32 %b, %c 838 %max_bc = select i1 %cmp_bc, i32 %b, i32 %c 839 %cmp_ba = icmp ugt i32 %b, %a 840 %max_ba = select i1 %cmp_ba, i32 %b, i32 %a 841 %cmp_bc_ba = icmp ugt i32 %max_bc, %max_ba 842 %max_abc = select i1 %cmp_bc_ba, i32 %max_bc, i32 %max_ba 843 call void @extra_use(i32 %max_bc) 844 ret i32 %max_abc 845} 846 847define i32 @common_factor_umax_extra_use_rhs(i32 %a, i32 %b, i32 %c) { 848; CHECK-LABEL: @common_factor_umax_extra_use_rhs( 849; CHECK-NEXT: [[CMP_BA:%.*]] = icmp ugt i32 [[B:%.*]], [[A:%.*]] 850; CHECK-NEXT: [[MAX_BA:%.*]] = select i1 [[CMP_BA]], i32 [[B]], i32 [[A]] 851; CHECK-NEXT: [[TMP1:%.*]] = icmp ugt i32 [[MAX_BA]], [[C:%.*]] 852; CHECK-NEXT: [[MAX_ABC:%.*]] = select i1 [[TMP1]], i32 [[MAX_BA]], i32 [[C]] 853; CHECK-NEXT: call void @extra_use(i32 [[MAX_BA]]) 854; CHECK-NEXT: ret i32 [[MAX_ABC]] 855; 856 %cmp_bc = icmp ugt i32 %b, %c 857 %max_bc = select i1 %cmp_bc, i32 %b, i32 %c 858 %cmp_ba = icmp ugt i32 %b, %a 859 %max_ba = select i1 %cmp_ba, i32 %b, i32 %a 860 %cmp_bc_ba = icmp ugt i32 %max_bc, %max_ba 861 %max_abc = select i1 %cmp_bc_ba, i32 %max_bc, i32 %max_ba 862 call void @extra_use(i32 %max_ba) 863 ret i32 %max_abc 864} 865 866define i32 @common_factor_umax_extra_use_both(i32 %a, i32 %b, i32 %c) { 867; CHECK-LABEL: @common_factor_umax_extra_use_both( 868; CHECK-NEXT: [[CMP_BC:%.*]] = icmp ugt i32 [[B:%.*]], [[C:%.*]] 869; CHECK-NEXT: [[MAX_BC:%.*]] = select i1 [[CMP_BC]], i32 [[B]], i32 [[C]] 870; CHECK-NEXT: [[CMP_BA:%.*]] = icmp ugt i32 [[B]], [[A:%.*]] 871; CHECK-NEXT: [[MAX_BA:%.*]] = select i1 [[CMP_BA]], i32 [[B]], i32 [[A]] 872; CHECK-NEXT: [[CMP_BC_BA:%.*]] = icmp ugt i32 [[MAX_BC]], [[MAX_BA]] 873; CHECK-NEXT: [[MAX_ABC:%.*]] = select i1 [[CMP_BC_BA]], i32 [[MAX_BC]], i32 [[MAX_BA]] 874; CHECK-NEXT: call void @extra_use(i32 [[MAX_BC]]) 875; CHECK-NEXT: call void @extra_use(i32 [[MAX_BA]]) 876; CHECK-NEXT: ret i32 [[MAX_ABC]] 877; 878 %cmp_bc = icmp ugt i32 %b, %c 879 %max_bc = select i1 %cmp_bc, i32 %b, i32 %c 880 %cmp_ba = icmp ugt i32 %b, %a 881 %max_ba = select i1 %cmp_ba, i32 %b, i32 %a 882 %cmp_bc_ba = icmp ugt i32 %max_bc, %max_ba 883 %max_abc = select i1 %cmp_bc_ba, i32 %max_bc, i32 %max_ba 884 call void @extra_use(i32 %max_bc) 885 call void @extra_use(i32 %max_ba) 886 ret i32 %max_abc 887} 888 889; This would assert. Don't assume that earlier min/max types match a possible later min/max. 890 891define float @not_min_of_min(i8 %i, float %x) { 892; CHECK-LABEL: @not_min_of_min( 893; CHECK-NEXT: [[CMP1_INV:%.*]] = fcmp fast oge float [[X:%.*]], 1.000000e+00 894; CHECK-NEXT: [[MIN1:%.*]] = select i1 [[CMP1_INV]], float 1.000000e+00, float [[X]] 895; CHECK-NEXT: [[CMP2_INV:%.*]] = fcmp fast oge float [[X]], 2.000000e+00 896; CHECK-NEXT: [[MIN2:%.*]] = select i1 [[CMP2_INV]], float 2.000000e+00, float [[X]] 897; CHECK-NEXT: [[CMP3:%.*]] = icmp ult i8 [[I:%.*]], 16 898; CHECK-NEXT: [[R:%.*]] = select i1 [[CMP3]], float [[MIN1]], float [[MIN2]] 899; CHECK-NEXT: ret float [[R]] 900; 901 %cmp1 = fcmp fast ult float %x, 1.0 902 %min1 = select i1 %cmp1, float %x, float 1.0 903 %cmp2 = fcmp fast ult float %x, 2.0 904 %min2 = select i1 %cmp2, float %x, float 2.0 905 %cmp3 = icmp ult i8 %i, 16 906 %r = select i1 %cmp3, float %min1, float %min2 907 ret float %r 908} 909 910