1; NOTE: Assertions have been autogenerated by utils/update_test_checks.py 2; RUN: opt < %s -passes=instcombine -S | FileCheck %s 3 4declare void @use(i8) 5 6define i32 @foo(i32 %a, i32 %b, i32 %c, i32 %d) { 7; CHECK-LABEL: @foo( 8; CHECK-NEXT: [[E:%.*]] = icmp slt i32 [[A:%.*]], [[B:%.*]] 9; CHECK-NEXT: [[TMP1:%.*]] = select i1 [[E]], i32 [[C:%.*]], i32 [[D:%.*]] 10; CHECK-NEXT: ret i32 [[TMP1]] 11; 12 %e = icmp slt i32 %a, %b 13 %f = sext i1 %e to i32 14 %g = and i32 %c, %f 15 %h = xor i32 %f, -1 16 %i = and i32 %d, %h 17 %j = or i32 %g, %i 18 ret i32 %j 19} 20 21define i32 @bar(i32 %a, i32 %b, i32 %c, i32 %d) { 22; CHECK-LABEL: @bar( 23; CHECK-NEXT: [[E:%.*]] = icmp slt i32 [[A:%.*]], [[B:%.*]] 24; CHECK-NEXT: [[TMP1:%.*]] = select i1 [[E]], i32 [[C:%.*]], i32 [[D:%.*]] 25; CHECK-NEXT: ret i32 [[TMP1]] 26; 27 %e = icmp slt i32 %a, %b 28 %f = sext i1 %e to i32 29 %g = and i32 %c, %f 30 %h = xor i32 %f, -1 31 %i = and i32 %d, %h 32 %j = or i32 %i, %g 33 ret i32 %j 34} 35 36define i32 @goo(i32 %a, i32 %b, i32 %c, i32 %d) { 37; CHECK-LABEL: @goo( 38; CHECK-NEXT: [[T0:%.*]] = icmp slt i32 [[A:%.*]], [[B:%.*]] 39; CHECK-NEXT: [[TMP1:%.*]] = select i1 [[T0]], i32 [[C:%.*]], i32 [[D:%.*]] 40; CHECK-NEXT: ret i32 [[TMP1]] 41; 42 %t0 = icmp slt i32 %a, %b 43 %iftmp.0.0 = select i1 %t0, i32 -1, i32 0 44 %t1 = and i32 %iftmp.0.0, %c 45 %not = xor i32 %iftmp.0.0, -1 46 %t2 = and i32 %not, %d 47 %t3 = or i32 %t1, %t2 48 ret i32 %t3 49} 50 51define i32 @poo(i32 %a, i32 %b, i32 %c, i32 %d) { 52; CHECK-LABEL: @poo( 53; CHECK-NEXT: [[T0:%.*]] = icmp slt i32 [[A:%.*]], [[B:%.*]] 54; CHECK-NEXT: [[T3:%.*]] = select i1 [[T0]], i32 [[C:%.*]], i32 [[D:%.*]] 55; CHECK-NEXT: ret i32 [[T3]] 56; 57 %t0 = icmp slt i32 %a, %b 58 %iftmp.0.0 = select i1 %t0, i32 -1, i32 0 59 %t1 = and i32 %iftmp.0.0, %c 60 %iftmp = select i1 %t0, i32 0, i32 -1 61 %t2 = and i32 %iftmp, %d 62 %t3 = or i32 %t1, %t2 63 ret i32 %t3 64} 65 66; PR32791 - https://bugs.llvm.org//show_bug.cgi?id=32791 67; The 2nd compare/select are canonicalized, so CSE and another round of instcombine or some other pass will fold this. 68 69define i32 @fold_inverted_icmp_preds(i32 %a, i32 %b, i32 %c, i32 %d) { 70; CHECK-LABEL: @fold_inverted_icmp_preds( 71; CHECK-NEXT: [[CMP1:%.*]] = icmp slt i32 [[A:%.*]], [[B:%.*]] 72; CHECK-NEXT: [[SEL1:%.*]] = select i1 [[CMP1]], i32 [[C:%.*]], i32 0 73; CHECK-NEXT: [[CMP2_NOT:%.*]] = icmp slt i32 [[A]], [[B]] 74; CHECK-NEXT: [[SEL2:%.*]] = select i1 [[CMP2_NOT]], i32 0, i32 [[D:%.*]] 75; CHECK-NEXT: [[OR:%.*]] = or i32 [[SEL1]], [[SEL2]] 76; CHECK-NEXT: ret i32 [[OR]] 77; 78 %cmp1 = icmp slt i32 %a, %b 79 %sel1 = select i1 %cmp1, i32 %c, i32 0 80 %cmp2 = icmp sge i32 %a, %b 81 %sel2 = select i1 %cmp2, i32 %d, i32 0 82 %or = or i32 %sel1, %sel2 83 ret i32 %or 84} 85 86; The 2nd compare/select are canonicalized, so CSE and another round of instcombine or some other pass will fold this. 87 88define i32 @fold_inverted_icmp_preds_reverse(i32 %a, i32 %b, i32 %c, i32 %d) { 89; CHECK-LABEL: @fold_inverted_icmp_preds_reverse( 90; CHECK-NEXT: [[CMP1:%.*]] = icmp slt i32 [[A:%.*]], [[B:%.*]] 91; CHECK-NEXT: [[SEL1:%.*]] = select i1 [[CMP1]], i32 0, i32 [[C:%.*]] 92; CHECK-NEXT: [[CMP2_NOT:%.*]] = icmp slt i32 [[A]], [[B]] 93; CHECK-NEXT: [[SEL2:%.*]] = select i1 [[CMP2_NOT]], i32 [[D:%.*]], i32 0 94; CHECK-NEXT: [[OR:%.*]] = or i32 [[SEL1]], [[SEL2]] 95; CHECK-NEXT: ret i32 [[OR]] 96; 97 %cmp1 = icmp slt i32 %a, %b 98 %sel1 = select i1 %cmp1, i32 0, i32 %c 99 %cmp2 = icmp sge i32 %a, %b 100 %sel2 = select i1 %cmp2, i32 0, i32 %d 101 %or = or i32 %sel1, %sel2 102 ret i32 %or 103} 104 105; TODO: Should fcmp have the same sort of predicate canonicalization as icmp? 106 107define i32 @fold_inverted_fcmp_preds(float %a, float %b, i32 %c, i32 %d) { 108; CHECK-LABEL: @fold_inverted_fcmp_preds( 109; CHECK-NEXT: [[CMP1:%.*]] = fcmp olt float [[A:%.*]], [[B:%.*]] 110; CHECK-NEXT: [[SEL1:%.*]] = select i1 [[CMP1]], i32 [[C:%.*]], i32 0 111; CHECK-NEXT: [[CMP2:%.*]] = fcmp uge float [[A]], [[B]] 112; CHECK-NEXT: [[SEL2:%.*]] = select i1 [[CMP2]], i32 [[D:%.*]], i32 0 113; CHECK-NEXT: [[OR:%.*]] = or i32 [[SEL1]], [[SEL2]] 114; CHECK-NEXT: ret i32 [[OR]] 115; 116 %cmp1 = fcmp olt float %a, %b 117 %sel1 = select i1 %cmp1, i32 %c, i32 0 118 %cmp2 = fcmp uge float %a, %b 119 %sel2 = select i1 %cmp2, i32 %d, i32 0 120 %or = or i32 %sel1, %sel2 121 ret i32 %or 122} 123 124; The 2nd compare/select are canonicalized, so CSE and another round of instcombine or some other pass will fold this. 125 126define <2 x i32> @fold_inverted_icmp_vector_preds(<2 x i32> %a, <2 x i32> %b, <2 x i32> %c, <2 x i32> %d) { 127; CHECK-LABEL: @fold_inverted_icmp_vector_preds( 128; CHECK-NEXT: [[CMP1_NOT:%.*]] = icmp eq <2 x i32> [[A:%.*]], [[B:%.*]] 129; CHECK-NEXT: [[SEL1:%.*]] = select <2 x i1> [[CMP1_NOT]], <2 x i32> zeroinitializer, <2 x i32> [[C:%.*]] 130; CHECK-NEXT: [[CMP2:%.*]] = icmp eq <2 x i32> [[A]], [[B]] 131; CHECK-NEXT: [[SEL2:%.*]] = select <2 x i1> [[CMP2]], <2 x i32> [[D:%.*]], <2 x i32> zeroinitializer 132; CHECK-NEXT: [[OR:%.*]] = or <2 x i32> [[SEL1]], [[SEL2]] 133; CHECK-NEXT: ret <2 x i32> [[OR]] 134; 135 %cmp1 = icmp ne <2 x i32> %a, %b 136 %sel1 = select <2 x i1> %cmp1, <2 x i32> %c, <2 x i32> <i32 0, i32 0> 137 %cmp2 = icmp eq <2 x i32> %a, %b 138 %sel2 = select <2 x i1> %cmp2, <2 x i32> %d, <2 x i32> <i32 0, i32 0> 139 %or = or <2 x i32> %sel1, %sel2 140 ret <2 x i32> %or 141} 142 143define i32 @par(i32 %a, i32 %b, i32 %c, i32 %d) { 144; CHECK-LABEL: @par( 145; CHECK-NEXT: [[T0:%.*]] = icmp slt i32 [[A:%.*]], [[B:%.*]] 146; CHECK-NEXT: [[TMP1:%.*]] = select i1 [[T0]], i32 [[C:%.*]], i32 [[D:%.*]] 147; CHECK-NEXT: ret i32 [[TMP1]] 148; 149 %t0 = icmp slt i32 %a, %b 150 %iftmp.1.0 = select i1 %t0, i32 -1, i32 0 151 %t1 = and i32 %iftmp.1.0, %c 152 %not = xor i32 %iftmp.1.0, -1 153 %t2 = and i32 %not, %d 154 %t3 = or i32 %t1, %t2 155 ret i32 %t3 156} 157 158; In the following tests (8 commutation variants), verify that a bitcast doesn't get 159; in the way of a select transform. These bitcasts are common in SSE/AVX and possibly 160; other vector code because of canonicalization to i64 elements for vectors. 161 162; The fptosi instructions are included to avoid commutation canonicalization based on 163; operator weight. Using another cast operator ensures that both operands of all logic 164; ops are equally weighted, and this ensures that we're testing all commutation 165; possibilities. 166 167define <2 x i64> @bitcast_select_swap0(<4 x i1> %cmp, <2 x double> %a, <2 x double> %b) { 168; CHECK-LABEL: @bitcast_select_swap0( 169; CHECK-NEXT: [[SIA:%.*]] = fptosi <2 x double> [[A:%.*]] to <2 x i64> 170; CHECK-NEXT: [[SIB:%.*]] = fptosi <2 x double> [[B:%.*]] to <2 x i64> 171; CHECK-NEXT: [[TMP1:%.*]] = bitcast <2 x i64> [[SIA]] to <4 x i32> 172; CHECK-NEXT: [[TMP2:%.*]] = bitcast <2 x i64> [[SIB]] to <4 x i32> 173; CHECK-NEXT: [[TMP3:%.*]] = select <4 x i1> [[CMP:%.*]], <4 x i32> [[TMP1]], <4 x i32> [[TMP2]] 174; CHECK-NEXT: [[TMP4:%.*]] = bitcast <4 x i32> [[TMP3]] to <2 x i64> 175; CHECK-NEXT: ret <2 x i64> [[TMP4]] 176; 177 %sia = fptosi <2 x double> %a to <2 x i64> 178 %sib = fptosi <2 x double> %b to <2 x i64> 179 %sext = sext <4 x i1> %cmp to <4 x i32> 180 %bc1 = bitcast <4 x i32> %sext to <2 x i64> 181 %and1 = and <2 x i64> %bc1, %sia 182 %neg = xor <4 x i32> %sext, <i32 -1, i32 -1, i32 -1, i32 -1> 183 %bc2 = bitcast <4 x i32> %neg to <2 x i64> 184 %and2 = and <2 x i64> %bc2, %sib 185 %or = or <2 x i64> %and1, %and2 186 ret <2 x i64> %or 187} 188 189define <2 x i64> @bitcast_select_swap1(<4 x i1> %cmp, <2 x double> %a, <2 x double> %b) { 190; CHECK-LABEL: @bitcast_select_swap1( 191; CHECK-NEXT: [[SIA:%.*]] = fptosi <2 x double> [[A:%.*]] to <2 x i64> 192; CHECK-NEXT: [[SIB:%.*]] = fptosi <2 x double> [[B:%.*]] to <2 x i64> 193; CHECK-NEXT: [[TMP1:%.*]] = bitcast <2 x i64> [[SIA]] to <4 x i32> 194; CHECK-NEXT: [[TMP2:%.*]] = bitcast <2 x i64> [[SIB]] to <4 x i32> 195; CHECK-NEXT: [[TMP3:%.*]] = select <4 x i1> [[CMP:%.*]], <4 x i32> [[TMP1]], <4 x i32> [[TMP2]] 196; CHECK-NEXT: [[TMP4:%.*]] = bitcast <4 x i32> [[TMP3]] to <2 x i64> 197; CHECK-NEXT: ret <2 x i64> [[TMP4]] 198; 199 %sia = fptosi <2 x double> %a to <2 x i64> 200 %sib = fptosi <2 x double> %b to <2 x i64> 201 %sext = sext <4 x i1> %cmp to <4 x i32> 202 %bc1 = bitcast <4 x i32> %sext to <2 x i64> 203 %and1 = and <2 x i64> %bc1, %sia 204 %neg = xor <4 x i32> %sext, <i32 -1, i32 -1, i32 -1, i32 -1> 205 %bc2 = bitcast <4 x i32> %neg to <2 x i64> 206 %and2 = and <2 x i64> %bc2, %sib 207 %or = or <2 x i64> %and2, %and1 208 ret <2 x i64> %or 209} 210 211define <2 x i64> @bitcast_select_swap2(<4 x i1> %cmp, <2 x double> %a, <2 x double> %b) { 212; CHECK-LABEL: @bitcast_select_swap2( 213; CHECK-NEXT: [[SIA:%.*]] = fptosi <2 x double> [[A:%.*]] to <2 x i64> 214; CHECK-NEXT: [[SIB:%.*]] = fptosi <2 x double> [[B:%.*]] to <2 x i64> 215; CHECK-NEXT: [[TMP1:%.*]] = bitcast <2 x i64> [[SIA]] to <4 x i32> 216; CHECK-NEXT: [[TMP2:%.*]] = bitcast <2 x i64> [[SIB]] to <4 x i32> 217; CHECK-NEXT: [[TMP3:%.*]] = select <4 x i1> [[CMP:%.*]], <4 x i32> [[TMP1]], <4 x i32> [[TMP2]] 218; CHECK-NEXT: [[TMP4:%.*]] = bitcast <4 x i32> [[TMP3]] to <2 x i64> 219; CHECK-NEXT: ret <2 x i64> [[TMP4]] 220; 221 %sia = fptosi <2 x double> %a to <2 x i64> 222 %sib = fptosi <2 x double> %b to <2 x i64> 223 %sext = sext <4 x i1> %cmp to <4 x i32> 224 %bc1 = bitcast <4 x i32> %sext to <2 x i64> 225 %and1 = and <2 x i64> %bc1, %sia 226 %neg = xor <4 x i32> %sext, <i32 -1, i32 -1, i32 -1, i32 -1> 227 %bc2 = bitcast <4 x i32> %neg to <2 x i64> 228 %and2 = and <2 x i64> %sib, %bc2 229 %or = or <2 x i64> %and1, %and2 230 ret <2 x i64> %or 231} 232 233define <2 x i64> @bitcast_select_swap3(<4 x i1> %cmp, <2 x double> %a, <2 x double> %b) { 234; CHECK-LABEL: @bitcast_select_swap3( 235; CHECK-NEXT: [[SIA:%.*]] = fptosi <2 x double> [[A:%.*]] to <2 x i64> 236; CHECK-NEXT: [[SIB:%.*]] = fptosi <2 x double> [[B:%.*]] to <2 x i64> 237; CHECK-NEXT: [[TMP1:%.*]] = bitcast <2 x i64> [[SIA]] to <4 x i32> 238; CHECK-NEXT: [[TMP2:%.*]] = bitcast <2 x i64> [[SIB]] to <4 x i32> 239; CHECK-NEXT: [[TMP3:%.*]] = select <4 x i1> [[CMP:%.*]], <4 x i32> [[TMP1]], <4 x i32> [[TMP2]] 240; CHECK-NEXT: [[TMP4:%.*]] = bitcast <4 x i32> [[TMP3]] to <2 x i64> 241; CHECK-NEXT: ret <2 x i64> [[TMP4]] 242; 243 %sia = fptosi <2 x double> %a to <2 x i64> 244 %sib = fptosi <2 x double> %b to <2 x i64> 245 %sext = sext <4 x i1> %cmp to <4 x i32> 246 %bc1 = bitcast <4 x i32> %sext to <2 x i64> 247 %and1 = and <2 x i64> %bc1, %sia 248 %neg = xor <4 x i32> %sext, <i32 -1, i32 -1, i32 -1, i32 -1> 249 %bc2 = bitcast <4 x i32> %neg to <2 x i64> 250 %and2 = and <2 x i64> %sib, %bc2 251 %or = or <2 x i64> %and2, %and1 252 ret <2 x i64> %or 253} 254 255define <2 x i64> @bitcast_select_swap4(<4 x i1> %cmp, <2 x double> %a, <2 x double> %b) { 256; CHECK-LABEL: @bitcast_select_swap4( 257; CHECK-NEXT: [[SIA:%.*]] = fptosi <2 x double> [[A:%.*]] to <2 x i64> 258; CHECK-NEXT: [[SIB:%.*]] = fptosi <2 x double> [[B:%.*]] to <2 x i64> 259; CHECK-NEXT: [[TMP1:%.*]] = bitcast <2 x i64> [[SIA]] to <4 x i32> 260; CHECK-NEXT: [[TMP2:%.*]] = bitcast <2 x i64> [[SIB]] to <4 x i32> 261; CHECK-NEXT: [[TMP3:%.*]] = select <4 x i1> [[CMP:%.*]], <4 x i32> [[TMP1]], <4 x i32> [[TMP2]] 262; CHECK-NEXT: [[TMP4:%.*]] = bitcast <4 x i32> [[TMP3]] to <2 x i64> 263; CHECK-NEXT: ret <2 x i64> [[TMP4]] 264; 265 %sia = fptosi <2 x double> %a to <2 x i64> 266 %sib = fptosi <2 x double> %b to <2 x i64> 267 %sext = sext <4 x i1> %cmp to <4 x i32> 268 %bc1 = bitcast <4 x i32> %sext to <2 x i64> 269 %and1 = and <2 x i64> %sia, %bc1 270 %neg = xor <4 x i32> %sext, <i32 -1, i32 -1, i32 -1, i32 -1> 271 %bc2 = bitcast <4 x i32> %neg to <2 x i64> 272 %and2 = and <2 x i64> %bc2, %sib 273 %or = or <2 x i64> %and1, %and2 274 ret <2 x i64> %or 275} 276 277define <2 x i64> @bitcast_select_swap5(<4 x i1> %cmp, <2 x double> %a, <2 x double> %b) { 278; CHECK-LABEL: @bitcast_select_swap5( 279; CHECK-NEXT: [[SIA:%.*]] = fptosi <2 x double> [[A:%.*]] to <2 x i64> 280; CHECK-NEXT: [[SIB:%.*]] = fptosi <2 x double> [[B:%.*]] to <2 x i64> 281; CHECK-NEXT: [[TMP1:%.*]] = bitcast <2 x i64> [[SIA]] to <4 x i32> 282; CHECK-NEXT: [[TMP2:%.*]] = bitcast <2 x i64> [[SIB]] to <4 x i32> 283; CHECK-NEXT: [[TMP3:%.*]] = select <4 x i1> [[CMP:%.*]], <4 x i32> [[TMP1]], <4 x i32> [[TMP2]] 284; CHECK-NEXT: [[TMP4:%.*]] = bitcast <4 x i32> [[TMP3]] to <2 x i64> 285; CHECK-NEXT: ret <2 x i64> [[TMP4]] 286; 287 %sia = fptosi <2 x double> %a to <2 x i64> 288 %sib = fptosi <2 x double> %b to <2 x i64> 289 %sext = sext <4 x i1> %cmp to <4 x i32> 290 %bc1 = bitcast <4 x i32> %sext to <2 x i64> 291 %and1 = and <2 x i64> %sia, %bc1 292 %neg = xor <4 x i32> %sext, <i32 -1, i32 -1, i32 -1, i32 -1> 293 %bc2 = bitcast <4 x i32> %neg to <2 x i64> 294 %and2 = and <2 x i64> %bc2, %sib 295 %or = or <2 x i64> %and2, %and1 296 ret <2 x i64> %or 297} 298 299define <2 x i64> @bitcast_select_swap6(<4 x i1> %cmp, <2 x double> %a, <2 x double> %b) { 300; CHECK-LABEL: @bitcast_select_swap6( 301; CHECK-NEXT: [[SIA:%.*]] = fptosi <2 x double> [[A:%.*]] to <2 x i64> 302; CHECK-NEXT: [[SIB:%.*]] = fptosi <2 x double> [[B:%.*]] to <2 x i64> 303; CHECK-NEXT: [[TMP1:%.*]] = bitcast <2 x i64> [[SIA]] to <4 x i32> 304; CHECK-NEXT: [[TMP2:%.*]] = bitcast <2 x i64> [[SIB]] to <4 x i32> 305; CHECK-NEXT: [[TMP3:%.*]] = select <4 x i1> [[CMP:%.*]], <4 x i32> [[TMP1]], <4 x i32> [[TMP2]] 306; CHECK-NEXT: [[TMP4:%.*]] = bitcast <4 x i32> [[TMP3]] to <2 x i64> 307; CHECK-NEXT: ret <2 x i64> [[TMP4]] 308; 309 %sia = fptosi <2 x double> %a to <2 x i64> 310 %sib = fptosi <2 x double> %b to <2 x i64> 311 %sext = sext <4 x i1> %cmp to <4 x i32> 312 %bc1 = bitcast <4 x i32> %sext to <2 x i64> 313 %and1 = and <2 x i64> %sia, %bc1 314 %neg = xor <4 x i32> %sext, <i32 -1, i32 -1, i32 -1, i32 -1> 315 %bc2 = bitcast <4 x i32> %neg to <2 x i64> 316 %and2 = and <2 x i64> %sib, %bc2 317 %or = or <2 x i64> %and1, %and2 318 ret <2 x i64> %or 319} 320 321define <2 x i64> @bitcast_select_swap7(<4 x i1> %cmp, <2 x double> %a, <2 x double> %b) { 322; CHECK-LABEL: @bitcast_select_swap7( 323; CHECK-NEXT: [[SIA:%.*]] = fptosi <2 x double> [[A:%.*]] to <2 x i64> 324; CHECK-NEXT: [[SIB:%.*]] = fptosi <2 x double> [[B:%.*]] to <2 x i64> 325; CHECK-NEXT: [[TMP1:%.*]] = bitcast <2 x i64> [[SIA]] to <4 x i32> 326; CHECK-NEXT: [[TMP2:%.*]] = bitcast <2 x i64> [[SIB]] to <4 x i32> 327; CHECK-NEXT: [[TMP3:%.*]] = select <4 x i1> [[CMP:%.*]], <4 x i32> [[TMP1]], <4 x i32> [[TMP2]] 328; CHECK-NEXT: [[TMP4:%.*]] = bitcast <4 x i32> [[TMP3]] to <2 x i64> 329; CHECK-NEXT: ret <2 x i64> [[TMP4]] 330; 331 %sia = fptosi <2 x double> %a to <2 x i64> 332 %sib = fptosi <2 x double> %b to <2 x i64> 333 %sext = sext <4 x i1> %cmp to <4 x i32> 334 %bc1 = bitcast <4 x i32> %sext to <2 x i64> 335 %and1 = and <2 x i64> %sia, %bc1 336 %neg = xor <4 x i32> %sext, <i32 -1, i32 -1, i32 -1, i32 -1> 337 %bc2 = bitcast <4 x i32> %neg to <2 x i64> 338 %and2 = and <2 x i64> %sib, %bc2 339 %or = or <2 x i64> %and2, %and1 340 ret <2 x i64> %or 341} 342 343define <2 x i64> @bitcast_select_multi_uses(<4 x i1> %cmp, <2 x i64> %a, <2 x i64> %b) { 344; CHECK-LABEL: @bitcast_select_multi_uses( 345; CHECK-NEXT: [[SEXT:%.*]] = sext <4 x i1> [[CMP:%.*]] to <4 x i32> 346; CHECK-NEXT: [[BC1:%.*]] = bitcast <4 x i32> [[SEXT]] to <2 x i64> 347; CHECK-NEXT: [[AND1:%.*]] = and <2 x i64> [[BC1]], [[A:%.*]] 348; CHECK-NEXT: [[TMP1:%.*]] = bitcast <4 x i32> [[SEXT]] to <2 x i64> 349; CHECK-NEXT: [[BC2:%.*]] = xor <2 x i64> [[TMP1]], <i64 -1, i64 -1> 350; CHECK-NEXT: [[AND2:%.*]] = and <2 x i64> [[BC2]], [[B:%.*]] 351; CHECK-NEXT: [[OR:%.*]] = or <2 x i64> [[AND2]], [[AND1]] 352; CHECK-NEXT: [[ADD:%.*]] = add <2 x i64> [[AND2]], [[BC2]] 353; CHECK-NEXT: [[SUB:%.*]] = sub <2 x i64> [[OR]], [[ADD]] 354; CHECK-NEXT: ret <2 x i64> [[SUB]] 355; 356 %sext = sext <4 x i1> %cmp to <4 x i32> 357 %bc1 = bitcast <4 x i32> %sext to <2 x i64> 358 %and1 = and <2 x i64> %a, %bc1 359 %neg = xor <4 x i32> %sext, <i32 -1, i32 -1, i32 -1, i32 -1> 360 %bc2 = bitcast <4 x i32> %neg to <2 x i64> 361 %and2 = and <2 x i64> %b, %bc2 362 %or = or <2 x i64> %and2, %and1 363 %add = add <2 x i64> %and2, %bc2 364 %sub = sub <2 x i64> %or, %add 365 ret <2 x i64> %sub 366} 367 368define i1 @bools(i1 %a, i1 %b, i1 %c) { 369; CHECK-LABEL: @bools( 370; CHECK-NEXT: [[TMP1:%.*]] = select i1 [[C:%.*]], i1 [[B:%.*]], i1 [[A:%.*]] 371; CHECK-NEXT: ret i1 [[TMP1]] 372; 373 %not = xor i1 %c, -1 374 %and1 = and i1 %not, %a 375 %and2 = and i1 %c, %b 376 %or = or i1 %and1, %and2 377 ret i1 %or 378} 379 380define i1 @bools_logical(i1 %a, i1 %b, i1 %c) { 381; CHECK-LABEL: @bools_logical( 382; CHECK-NEXT: [[OR:%.*]] = select i1 [[C:%.*]], i1 [[B:%.*]], i1 [[A:%.*]] 383; CHECK-NEXT: ret i1 [[OR]] 384; 385 %not = xor i1 %c, -1 386 %and1 = select i1 %not, i1 %a, i1 false 387 %and2 = select i1 %c, i1 %b, i1 false 388 %or = select i1 %and1, i1 true, i1 %and2 389 ret i1 %or 390} 391 392; Form a select if we know we can replace 2 simple logic ops. 393 394define i1 @bools_multi_uses1(i1 %a, i1 %b, i1 %c) { 395; CHECK-LABEL: @bools_multi_uses1( 396; CHECK-NEXT: [[NOT:%.*]] = xor i1 [[C:%.*]], true 397; CHECK-NEXT: [[AND1:%.*]] = and i1 [[NOT]], [[A:%.*]] 398; CHECK-NEXT: [[TMP1:%.*]] = select i1 [[C]], i1 [[B:%.*]], i1 [[A]] 399; CHECK-NEXT: [[XOR:%.*]] = xor i1 [[TMP1]], [[AND1]] 400; CHECK-NEXT: ret i1 [[XOR]] 401; 402 %not = xor i1 %c, -1 403 %and1 = and i1 %not, %a 404 %and2 = and i1 %c, %b 405 %or = or i1 %and1, %and2 406 %xor = xor i1 %or, %and1 407 ret i1 %xor 408} 409 410define i1 @bools_multi_uses1_logical(i1 %a, i1 %b, i1 %c) { 411; CHECK-LABEL: @bools_multi_uses1_logical( 412; CHECK-NEXT: [[NOT:%.*]] = xor i1 [[C:%.*]], true 413; CHECK-NEXT: [[AND1:%.*]] = select i1 [[NOT]], i1 [[A:%.*]], i1 false 414; CHECK-NEXT: [[OR:%.*]] = select i1 [[C]], i1 [[B:%.*]], i1 [[A]] 415; CHECK-NEXT: [[XOR:%.*]] = xor i1 [[OR]], [[AND1]] 416; CHECK-NEXT: ret i1 [[XOR]] 417; 418 %not = xor i1 %c, -1 419 %and1 = select i1 %not, i1 %a, i1 false 420 %and2 = select i1 %c, i1 %b, i1 false 421 %or = select i1 %and1, i1 true, i1 %and2 422 %xor = xor i1 %or, %and1 423 ret i1 %xor 424} 425 426; Don't replace a cheap logic op with a potentially expensive select 427; unless we can also eliminate one of the other original ops. 428 429define i1 @bools_multi_uses2(i1 %a, i1 %b, i1 %c) { 430; CHECK-LABEL: @bools_multi_uses2( 431; CHECK-NEXT: [[TMP1:%.*]] = select i1 [[C:%.*]], i1 [[B:%.*]], i1 [[A:%.*]] 432; CHECK-NEXT: ret i1 [[TMP1]] 433; 434 %not = xor i1 %c, -1 435 %and1 = and i1 %not, %a 436 %and2 = and i1 %c, %b 437 %or = or i1 %and1, %and2 438 %add = add i1 %and1, %and2 439 %and3 = and i1 %or, %add 440 ret i1 %and3 441} 442 443define i1 @bools_multi_uses2_logical(i1 %a, i1 %b, i1 %c) { 444; CHECK-LABEL: @bools_multi_uses2_logical( 445; CHECK-NEXT: [[NOT:%.*]] = xor i1 [[C:%.*]], true 446; CHECK-NEXT: [[AND1:%.*]] = select i1 [[NOT]], i1 [[A:%.*]], i1 false 447; CHECK-NEXT: [[AND2:%.*]] = select i1 [[C]], i1 [[B:%.*]], i1 false 448; CHECK-NEXT: [[OR:%.*]] = select i1 [[C]], i1 [[B]], i1 [[A]] 449; CHECK-NEXT: [[ADD:%.*]] = xor i1 [[AND1]], [[AND2]] 450; CHECK-NEXT: [[AND3:%.*]] = select i1 [[OR]], i1 [[ADD]], i1 false 451; CHECK-NEXT: ret i1 [[AND3]] 452; 453 %not = xor i1 %c, -1 454 %and1 = select i1 %not, i1 %a, i1 false 455 %and2 = select i1 %c, i1 %b, i1 false 456 %or = select i1 %and1, i1 true, i1 %and2 457 %add = add i1 %and1, %and2 458 %and3 = select i1 %or, i1 %add, i1 false 459 ret i1 %and3 460} 461 462define <4 x i1> @vec_of_bools(<4 x i1> %a, <4 x i1> %b, <4 x i1> %c) { 463; CHECK-LABEL: @vec_of_bools( 464; CHECK-NEXT: [[TMP1:%.*]] = select <4 x i1> [[C:%.*]], <4 x i1> [[B:%.*]], <4 x i1> [[A:%.*]] 465; CHECK-NEXT: ret <4 x i1> [[TMP1]] 466; 467 %not = xor <4 x i1> %c, <i1 true, i1 true, i1 true, i1 true> 468 %and1 = and <4 x i1> %not, %a 469 %and2 = and <4 x i1> %b, %c 470 %or = or <4 x i1> %and2, %and1 471 ret <4 x i1> %or 472} 473 474define i4 @vec_of_casted_bools(i4 %a, i4 %b, <4 x i1> %c) { 475; CHECK-LABEL: @vec_of_casted_bools( 476; CHECK-NEXT: [[TMP1:%.*]] = bitcast i4 [[B:%.*]] to <4 x i1> 477; CHECK-NEXT: [[TMP2:%.*]] = bitcast i4 [[A:%.*]] to <4 x i1> 478; CHECK-NEXT: [[TMP3:%.*]] = select <4 x i1> [[C:%.*]], <4 x i1> [[TMP1]], <4 x i1> [[TMP2]] 479; CHECK-NEXT: [[TMP4:%.*]] = bitcast <4 x i1> [[TMP3]] to i4 480; CHECK-NEXT: ret i4 [[TMP4]] 481; 482 %not = xor <4 x i1> %c, <i1 true, i1 true, i1 true, i1 true> 483 %bc1 = bitcast <4 x i1> %not to i4 484 %bc2 = bitcast <4 x i1> %c to i4 485 %and1 = and i4 %a, %bc1 486 %and2 = and i4 %bc2, %b 487 %or = or i4 %and1, %and2 488 ret i4 %or 489} 490 491; Inverted 'and' constants mean this is a select which is canonicalized to a shuffle. 492 493define <4 x i32> @vec_sel_consts(<4 x i32> %a, <4 x i32> %b) { 494; CHECK-LABEL: @vec_sel_consts( 495; CHECK-NEXT: [[TMP1:%.*]] = shufflevector <4 x i32> [[A:%.*]], <4 x i32> [[B:%.*]], <4 x i32> <i32 0, i32 5, i32 6, i32 3> 496; CHECK-NEXT: ret <4 x i32> [[TMP1]] 497; 498 %and1 = and <4 x i32> %a, <i32 -1, i32 0, i32 0, i32 -1> 499 %and2 = and <4 x i32> %b, <i32 0, i32 -1, i32 -1, i32 0> 500 %or = or <4 x i32> %and1, %and2 501 ret <4 x i32> %or 502} 503 504define <3 x i129> @vec_sel_consts_weird(<3 x i129> %a, <3 x i129> %b) { 505; CHECK-LABEL: @vec_sel_consts_weird( 506; CHECK-NEXT: [[TMP1:%.*]] = shufflevector <3 x i129> [[A:%.*]], <3 x i129> [[B:%.*]], <3 x i32> <i32 0, i32 4, i32 2> 507; CHECK-NEXT: ret <3 x i129> [[TMP1]] 508; 509 %and1 = and <3 x i129> %a, <i129 -1, i129 0, i129 -1> 510 %and2 = and <3 x i129> %b, <i129 0, i129 -1, i129 0> 511 %or = or <3 x i129> %and2, %and1 512 ret <3 x i129> %or 513} 514 515; The mask elements must be inverted for this to be a select. 516 517define <4 x i32> @vec_not_sel_consts(<4 x i32> %a, <4 x i32> %b) { 518; CHECK-LABEL: @vec_not_sel_consts( 519; CHECK-NEXT: [[AND1:%.*]] = and <4 x i32> [[A:%.*]], <i32 -1, i32 0, i32 0, i32 0> 520; CHECK-NEXT: [[AND2:%.*]] = and <4 x i32> [[B:%.*]], <i32 0, i32 -1, i32 0, i32 -1> 521; CHECK-NEXT: [[OR:%.*]] = or <4 x i32> [[AND1]], [[AND2]] 522; CHECK-NEXT: ret <4 x i32> [[OR]] 523; 524 %and1 = and <4 x i32> %a, <i32 -1, i32 0, i32 0, i32 0> 525 %and2 = and <4 x i32> %b, <i32 0, i32 -1, i32 0, i32 -1> 526 %or = or <4 x i32> %and1, %and2 527 ret <4 x i32> %or 528} 529 530define <4 x i32> @vec_not_sel_consts_undef_elts(<4 x i32> %a, <4 x i32> %b) { 531; CHECK-LABEL: @vec_not_sel_consts_undef_elts( 532; CHECK-NEXT: [[AND1:%.*]] = and <4 x i32> [[A:%.*]], <i32 -1, i32 undef, i32 0, i32 0> 533; CHECK-NEXT: [[AND2:%.*]] = and <4 x i32> [[B:%.*]], <i32 0, i32 -1, i32 0, i32 undef> 534; CHECK-NEXT: [[OR:%.*]] = or <4 x i32> [[AND1]], [[AND2]] 535; CHECK-NEXT: ret <4 x i32> [[OR]] 536; 537 %and1 = and <4 x i32> %a, <i32 -1, i32 undef, i32 0, i32 0> 538 %and2 = and <4 x i32> %b, <i32 0, i32 -1, i32 0, i32 undef> 539 %or = or <4 x i32> %and1, %and2 540 ret <4 x i32> %or 541} 542 543; The inverted constants may be operands of xor instructions. 544 545define <4 x i32> @vec_sel_xor(<4 x i32> %a, <4 x i32> %b, <4 x i1> %c) { 546; CHECK-LABEL: @vec_sel_xor( 547; CHECK-NEXT: [[TMP1:%.*]] = xor <4 x i1> [[C:%.*]], <i1 false, i1 true, i1 true, i1 true> 548; CHECK-NEXT: [[TMP2:%.*]] = select <4 x i1> [[TMP1]], <4 x i32> [[A:%.*]], <4 x i32> [[B:%.*]] 549; CHECK-NEXT: ret <4 x i32> [[TMP2]] 550; 551 %mask = sext <4 x i1> %c to <4 x i32> 552 %mask_flip1 = xor <4 x i32> %mask, <i32 -1, i32 0, i32 0, i32 0> 553 %not_mask_flip1 = xor <4 x i32> %mask, <i32 0, i32 -1, i32 -1, i32 -1> 554 %and1 = and <4 x i32> %not_mask_flip1, %a 555 %and2 = and <4 x i32> %mask_flip1, %b 556 %or = or <4 x i32> %and1, %and2 557 ret <4 x i32> %or 558} 559 560; Allow the transform even if the mask values have multiple uses because 561; there's still a net reduction of instructions from removing the and/and/or. 562 563define <4 x i32> @vec_sel_xor_multi_use(<4 x i32> %a, <4 x i32> %b, <4 x i1> %c) { 564; CHECK-LABEL: @vec_sel_xor_multi_use( 565; CHECK-NEXT: [[TMP1:%.*]] = xor <4 x i1> [[C:%.*]], <i1 true, i1 false, i1 false, i1 false> 566; CHECK-NEXT: [[MASK_FLIP1:%.*]] = sext <4 x i1> [[TMP1]] to <4 x i32> 567; CHECK-NEXT: [[TMP2:%.*]] = xor <4 x i1> [[C]], <i1 false, i1 true, i1 true, i1 true> 568; CHECK-NEXT: [[TMP3:%.*]] = select <4 x i1> [[TMP2]], <4 x i32> [[A:%.*]], <4 x i32> [[B:%.*]] 569; CHECK-NEXT: [[ADD:%.*]] = add <4 x i32> [[TMP3]], [[MASK_FLIP1]] 570; CHECK-NEXT: ret <4 x i32> [[ADD]] 571; 572 %mask = sext <4 x i1> %c to <4 x i32> 573 %mask_flip1 = xor <4 x i32> %mask, <i32 -1, i32 0, i32 0, i32 0> 574 %not_mask_flip1 = xor <4 x i32> %mask, <i32 0, i32 -1, i32 -1, i32 -1> 575 %and1 = and <4 x i32> %not_mask_flip1, %a 576 %and2 = and <4 x i32> %mask_flip1, %b 577 %or = or <4 x i32> %and1, %and2 578 %add = add <4 x i32> %or, %mask_flip1 579 ret <4 x i32> %add 580} 581 582; The 'ashr' guarantees that we have a bitmask, so this is select with truncated condition. 583 584define i32 @allSignBits(i32 %cond, i32 %tval, i32 %fval) { 585; CHECK-LABEL: @allSignBits( 586; CHECK-NEXT: [[ISNEG1:%.*]] = icmp slt i32 [[COND:%.*]], 0 587; CHECK-NEXT: [[A1:%.*]] = select i1 [[ISNEG1]], i32 [[TVAL:%.*]], i32 0 588; CHECK-NEXT: [[ISNEG:%.*]] = icmp slt i32 [[COND]], 0 589; CHECK-NEXT: [[A2:%.*]] = select i1 [[ISNEG]], i32 0, i32 [[FVAL:%.*]] 590; CHECK-NEXT: [[SEL:%.*]] = or i32 [[A1]], [[A2]] 591; CHECK-NEXT: ret i32 [[SEL]] 592; 593 %bitmask = ashr i32 %cond, 31 594 %not_bitmask = xor i32 %bitmask, -1 595 %a1 = and i32 %tval, %bitmask 596 %a2 = and i32 %not_bitmask, %fval 597 %sel = or i32 %a1, %a2 598 ret i32 %sel 599} 600 601define <4 x i8> @allSignBits_vec(<4 x i8> %cond, <4 x i8> %tval, <4 x i8> %fval) { 602; CHECK-LABEL: @allSignBits_vec( 603; CHECK-NEXT: [[ISNEG1:%.*]] = icmp slt <4 x i8> [[COND:%.*]], zeroinitializer 604; CHECK-NEXT: [[A1:%.*]] = select <4 x i1> [[ISNEG1]], <4 x i8> [[TVAL:%.*]], <4 x i8> zeroinitializer 605; CHECK-NEXT: [[ISNEG:%.*]] = icmp slt <4 x i8> [[COND]], zeroinitializer 606; CHECK-NEXT: [[A2:%.*]] = select <4 x i1> [[ISNEG]], <4 x i8> zeroinitializer, <4 x i8> [[FVAL:%.*]] 607; CHECK-NEXT: [[SEL:%.*]] = or <4 x i8> [[A2]], [[A1]] 608; CHECK-NEXT: ret <4 x i8> [[SEL]] 609; 610 %bitmask = ashr <4 x i8> %cond, <i8 7, i8 7, i8 7, i8 7> 611 %not_bitmask = xor <4 x i8> %bitmask, <i8 -1, i8 -1, i8 -1, i8 -1> 612 %a1 = and <4 x i8> %tval, %bitmask 613 %a2 = and <4 x i8> %fval, %not_bitmask 614 %sel = or <4 x i8> %a2, %a1 615 ret <4 x i8> %sel 616} 617 618; Negative test - make sure that bitcasts from FP do not cause a crash. 619 620define <2 x i64> @fp_bitcast(<4 x i1> %cmp, <2 x double> %a, <2 x double> %b) { 621; CHECK-LABEL: @fp_bitcast( 622; CHECK-NEXT: [[SIA:%.*]] = fptosi <2 x double> [[A:%.*]] to <2 x i64> 623; CHECK-NEXT: [[SIB:%.*]] = fptosi <2 x double> [[B:%.*]] to <2 x i64> 624; CHECK-NEXT: [[BC1:%.*]] = bitcast <2 x double> [[A]] to <2 x i64> 625; CHECK-NEXT: [[AND1:%.*]] = and <2 x i64> [[SIA]], [[BC1]] 626; CHECK-NEXT: [[BC2:%.*]] = bitcast <2 x double> [[B]] to <2 x i64> 627; CHECK-NEXT: [[AND2:%.*]] = and <2 x i64> [[SIB]], [[BC2]] 628; CHECK-NEXT: [[OR:%.*]] = or <2 x i64> [[AND2]], [[AND1]] 629; CHECK-NEXT: ret <2 x i64> [[OR]] 630; 631 %sia = fptosi <2 x double> %a to <2 x i64> 632 %sib = fptosi <2 x double> %b to <2 x i64> 633 %bc1 = bitcast <2 x double> %a to <2 x i64> 634 %and1 = and <2 x i64> %sia, %bc1 635 %bc2 = bitcast <2 x double> %b to <2 x i64> 636 %and2 = and <2 x i64> %sib, %bc2 637 %or = or <2 x i64> %and2, %and1 638 ret <2 x i64> %or 639} 640 641define <4 x i32> @computesignbits_through_shuffles(<4 x float> %x, <4 x float> %y, <4 x float> %z) { 642; CHECK-LABEL: @computesignbits_through_shuffles( 643; CHECK-NEXT: [[CMP:%.*]] = fcmp ole <4 x float> [[X:%.*]], [[Y:%.*]] 644; CHECK-NEXT: [[SEXT:%.*]] = sext <4 x i1> [[CMP]] to <4 x i32> 645; CHECK-NEXT: [[S1:%.*]] = shufflevector <4 x i32> [[SEXT]], <4 x i32> undef, <4 x i32> <i32 0, i32 0, i32 1, i32 1> 646; CHECK-NEXT: [[S2:%.*]] = shufflevector <4 x i32> [[SEXT]], <4 x i32> undef, <4 x i32> <i32 2, i32 2, i32 3, i32 3> 647; CHECK-NEXT: [[SHUF_OR1:%.*]] = or <4 x i32> [[S1]], [[S2]] 648; CHECK-NEXT: [[S3:%.*]] = shufflevector <4 x i32> [[SHUF_OR1]], <4 x i32> undef, <4 x i32> <i32 0, i32 0, i32 1, i32 1> 649; CHECK-NEXT: [[S4:%.*]] = shufflevector <4 x i32> [[SHUF_OR1]], <4 x i32> undef, <4 x i32> <i32 2, i32 2, i32 3, i32 3> 650; CHECK-NEXT: [[SHUF_OR2:%.*]] = or <4 x i32> [[S3]], [[S4]] 651; CHECK-NEXT: [[TMP1:%.*]] = trunc <4 x i32> [[SHUF_OR2]] to <4 x i1> 652; CHECK-NEXT: [[DOTV:%.*]] = select <4 x i1> [[TMP1]], <4 x float> [[Z:%.*]], <4 x float> [[X]] 653; CHECK-NEXT: [[TMP2:%.*]] = bitcast <4 x float> [[DOTV]] to <4 x i32> 654; CHECK-NEXT: ret <4 x i32> [[TMP2]] 655; 656 %cmp = fcmp ole <4 x float> %x, %y 657 %sext = sext <4 x i1> %cmp to <4 x i32> 658 %s1 = shufflevector <4 x i32> %sext, <4 x i32> undef, <4 x i32> <i32 0, i32 0, i32 1, i32 1> 659 %s2 = shufflevector <4 x i32> %sext, <4 x i32> undef, <4 x i32> <i32 2, i32 2, i32 3, i32 3> 660 %shuf_or1 = or <4 x i32> %s1, %s2 661 %s3 = shufflevector <4 x i32> %shuf_or1, <4 x i32> undef, <4 x i32> <i32 0, i32 0, i32 1, i32 1> 662 %s4 = shufflevector <4 x i32> %shuf_or1, <4 x i32> undef, <4 x i32> <i32 2, i32 2, i32 3, i32 3> 663 %shuf_or2 = or <4 x i32> %s3, %s4 664 %not_or2 = xor <4 x i32> %shuf_or2, <i32 -1, i32 -1, i32 -1, i32 -1> 665 %xbc = bitcast <4 x float> %x to <4 x i32> 666 %zbc = bitcast <4 x float> %z to <4 x i32> 667 %and1 = and <4 x i32> %not_or2, %xbc 668 %and2 = and <4 x i32> %shuf_or2, %zbc 669 %sel = or <4 x i32> %and1, %and2 670 ret <4 x i32> %sel 671} 672 673define <4 x i32> @computesignbits_through_two_input_shuffle(<4 x i32> %x, <4 x i32> %y, <4 x i1> %cond1, <4 x i1> %cond2) { 674; CHECK-LABEL: @computesignbits_through_two_input_shuffle( 675; CHECK-NEXT: [[SEXT1:%.*]] = sext <4 x i1> [[COND1:%.*]] to <4 x i32> 676; CHECK-NEXT: [[SEXT2:%.*]] = sext <4 x i1> [[COND2:%.*]] to <4 x i32> 677; CHECK-NEXT: [[COND:%.*]] = shufflevector <4 x i32> [[SEXT1]], <4 x i32> [[SEXT2]], <4 x i32> <i32 0, i32 2, i32 4, i32 6> 678; CHECK-NEXT: [[TMP1:%.*]] = trunc <4 x i32> [[COND]] to <4 x i1> 679; CHECK-NEXT: [[TMP2:%.*]] = select <4 x i1> [[TMP1]], <4 x i32> [[Y:%.*]], <4 x i32> [[X:%.*]] 680; CHECK-NEXT: ret <4 x i32> [[TMP2]] 681; 682 %sext1 = sext <4 x i1> %cond1 to <4 x i32> 683 %sext2 = sext <4 x i1> %cond2 to <4 x i32> 684 %cond = shufflevector <4 x i32> %sext1, <4 x i32> %sext2, <4 x i32> <i32 0, i32 2, i32 4, i32 6> 685 %notcond = xor <4 x i32> %cond, <i32 -1, i32 -1, i32 -1, i32 -1> 686 %and1 = and <4 x i32> %notcond, %x 687 %and2 = and <4 x i32> %cond, %y 688 %sel = or <4 x i32> %and1, %and2 689 ret <4 x i32> %sel 690} 691 692; Bitcast of condition from narrow source element type can be converted to select. 693 694define <2 x i64> @bitcast_vec_cond(<16 x i1> %cond, <2 x i64> %c, <2 x i64> %d) { 695; CHECK-LABEL: @bitcast_vec_cond( 696; CHECK-NEXT: [[TMP1:%.*]] = bitcast <2 x i64> [[D:%.*]] to <16 x i8> 697; CHECK-NEXT: [[TMP2:%.*]] = bitcast <2 x i64> [[C:%.*]] to <16 x i8> 698; CHECK-NEXT: [[TMP3:%.*]] = select <16 x i1> [[COND:%.*]], <16 x i8> [[TMP1]], <16 x i8> [[TMP2]] 699; CHECK-NEXT: [[TMP4:%.*]] = bitcast <16 x i8> [[TMP3]] to <2 x i64> 700; CHECK-NEXT: ret <2 x i64> [[TMP4]] 701; 702 %s = sext <16 x i1> %cond to <16 x i8> 703 %t9 = bitcast <16 x i8> %s to <2 x i64> 704 %nott9 = xor <2 x i64> %t9, <i64 -1, i64 -1> 705 %t11 = and <2 x i64> %nott9, %c 706 %t12 = and <2 x i64> %t9, %d 707 %r = or <2 x i64> %t11, %t12 708 ret <2 x i64> %r 709} 710 711; Negative test - bitcast of condition from wide source element type cannot be converted to select. 712 713define <8 x i3> @bitcast_vec_cond_commute1(<3 x i1> %cond, <8 x i3> %pc, <8 x i3> %d) { 714; CHECK-LABEL: @bitcast_vec_cond_commute1( 715; CHECK-NEXT: [[C:%.*]] = mul <8 x i3> [[PC:%.*]], [[PC]] 716; CHECK-NEXT: [[S:%.*]] = sext <3 x i1> [[COND:%.*]] to <3 x i8> 717; CHECK-NEXT: [[T9:%.*]] = bitcast <3 x i8> [[S]] to <8 x i3> 718; CHECK-NEXT: [[NOTT9:%.*]] = xor <8 x i3> [[T9]], <i3 -1, i3 -1, i3 -1, i3 -1, i3 -1, i3 -1, i3 -1, i3 -1> 719; CHECK-NEXT: [[T11:%.*]] = and <8 x i3> [[C]], [[NOTT9]] 720; CHECK-NEXT: [[T12:%.*]] = and <8 x i3> [[T9]], [[D:%.*]] 721; CHECK-NEXT: [[R:%.*]] = or <8 x i3> [[T11]], [[T12]] 722; CHECK-NEXT: ret <8 x i3> [[R]] 723; 724 %c = mul <8 x i3> %pc, %pc ; thwart complexity-based canonicalization 725 %s = sext <3 x i1> %cond to <3 x i8> 726 %t9 = bitcast <3 x i8> %s to <8 x i3> 727 %nott9 = xor <8 x i3> %t9, <i3 -1, i3 -1, i3 -1, i3 -1, i3 -1, i3 -1, i3 -1, i3 -1> 728 %t11 = and <8 x i3> %c, %nott9 729 %t12 = and <8 x i3> %t9, %d 730 %r = or <8 x i3> %t11, %t12 731 ret <8 x i3> %r 732} 733 734define <2 x i16> @bitcast_vec_cond_commute2(<4 x i1> %cond, <2 x i16> %pc, <2 x i16> %pd) { 735; CHECK-LABEL: @bitcast_vec_cond_commute2( 736; CHECK-NEXT: [[C:%.*]] = mul <2 x i16> [[PC:%.*]], [[PC]] 737; CHECK-NEXT: [[D:%.*]] = mul <2 x i16> [[PD:%.*]], [[PD]] 738; CHECK-NEXT: [[TMP1:%.*]] = bitcast <2 x i16> [[D]] to <4 x i8> 739; CHECK-NEXT: [[TMP2:%.*]] = bitcast <2 x i16> [[C]] to <4 x i8> 740; CHECK-NEXT: [[TMP3:%.*]] = select <4 x i1> [[COND:%.*]], <4 x i8> [[TMP1]], <4 x i8> [[TMP2]] 741; CHECK-NEXT: [[TMP4:%.*]] = bitcast <4 x i8> [[TMP3]] to <2 x i16> 742; CHECK-NEXT: ret <2 x i16> [[TMP4]] 743; 744 %c = mul <2 x i16> %pc, %pc ; thwart complexity-based canonicalization 745 %d = mul <2 x i16> %pd, %pd ; thwart complexity-based canonicalization 746 %s = sext <4 x i1> %cond to <4 x i8> 747 %t9 = bitcast <4 x i8> %s to <2 x i16> 748 %nott9 = xor <2 x i16> %t9, <i16 -1, i16 -1> 749 %t11 = and <2 x i16> %c, %nott9 750 %t12 = and <2 x i16> %d, %t9 751 %r = or <2 x i16> %t11, %t12 752 ret <2 x i16> %r 753} 754 755; Condition doesn't have to be a bool vec - just all signbits. 756 757define <2 x i16> @bitcast_vec_cond_commute3(<4 x i8> %cond, <2 x i16> %pc, <2 x i16> %pd) { 758; CHECK-LABEL: @bitcast_vec_cond_commute3( 759; CHECK-NEXT: [[C:%.*]] = mul <2 x i16> [[PC:%.*]], [[PC]] 760; CHECK-NEXT: [[D:%.*]] = mul <2 x i16> [[PD:%.*]], [[PD]] 761; CHECK-NEXT: [[TMP1:%.*]] = bitcast <2 x i16> [[D]] to <4 x i8> 762; CHECK-NEXT: [[TMP2:%.*]] = bitcast <2 x i16> [[C]] to <4 x i8> 763; CHECK-NEXT: [[DOTNOT2:%.*]] = icmp slt <4 x i8> [[COND:%.*]], zeroinitializer 764; CHECK-NEXT: [[TMP3:%.*]] = select <4 x i1> [[DOTNOT2]], <4 x i8> [[TMP1]], <4 x i8> [[TMP2]] 765; CHECK-NEXT: [[TMP4:%.*]] = bitcast <4 x i8> [[TMP3]] to <2 x i16> 766; CHECK-NEXT: ret <2 x i16> [[TMP4]] 767; 768 %c = mul <2 x i16> %pc, %pc ; thwart complexity-based canonicalization 769 %d = mul <2 x i16> %pd, %pd ; thwart complexity-based canonicalization 770 %s = ashr <4 x i8> %cond, <i8 7, i8 7, i8 7, i8 7> 771 %t9 = bitcast <4 x i8> %s to <2 x i16> 772 %nott9 = xor <2 x i16> %t9, <i16 -1, i16 -1> 773 %t11 = and <2 x i16> %c, %nott9 774 %t12 = and <2 x i16> %d, %t9 775 %r = or <2 x i16> %t11, %t12 776 ret <2 x i16> %r 777} 778 779; Don't crash on invalid type for compute signbits. 780 781define <2 x i64> @bitcast_fp_vec_cond(<2 x double> %s, <2 x i64> %c, <2 x i64> %d) { 782; CHECK-LABEL: @bitcast_fp_vec_cond( 783; CHECK-NEXT: [[T9:%.*]] = bitcast <2 x double> [[S:%.*]] to <2 x i64> 784; CHECK-NEXT: [[NOTT9:%.*]] = xor <2 x i64> [[T9]], <i64 -1, i64 -1> 785; CHECK-NEXT: [[T11:%.*]] = and <2 x i64> [[NOTT9]], [[C:%.*]] 786; CHECK-NEXT: [[T12:%.*]] = and <2 x i64> [[T9]], [[D:%.*]] 787; CHECK-NEXT: [[R:%.*]] = or <2 x i64> [[T11]], [[T12]] 788; CHECK-NEXT: ret <2 x i64> [[R]] 789; 790 %t9 = bitcast <2 x double> %s to <2 x i64> 791 %nott9 = xor <2 x i64> %t9, <i64 -1, i64 -1> 792 %t11 = and <2 x i64> %nott9, %c 793 %t12 = and <2 x i64> %t9, %d 794 %r = or <2 x i64> %t11, %t12 795 ret <2 x i64> %r 796} 797 798; Wider source type would be ok except poison could leak across elements. 799 800define <2 x i64> @bitcast_int_vec_cond(i1 %b, <2 x i64> %c, <2 x i64> %d) { 801; CHECK-LABEL: @bitcast_int_vec_cond( 802; CHECK-NEXT: [[S:%.*]] = sext i1 [[B:%.*]] to i128 803; CHECK-NEXT: [[T9:%.*]] = bitcast i128 [[S]] to <2 x i64> 804; CHECK-NEXT: [[NOTT9:%.*]] = xor <2 x i64> [[T9]], <i64 -1, i64 -1> 805; CHECK-NEXT: [[T11:%.*]] = and <2 x i64> [[NOTT9]], [[C:%.*]] 806; CHECK-NEXT: [[T12:%.*]] = and <2 x i64> [[T9]], [[D:%.*]] 807; CHECK-NEXT: [[R:%.*]] = or <2 x i64> [[T11]], [[T12]] 808; CHECK-NEXT: ret <2 x i64> [[R]] 809; 810 %s = sext i1 %b to i128 811 %t9 = bitcast i128 %s to <2 x i64> 812 %nott9 = xor <2 x i64> %t9, <i64 -1, i64 -1> 813 %t11 = and <2 x i64> %nott9, %c 814 %t12 = and <2 x i64> %t9, %d 815 %r = or <2 x i64> %t11, %t12 816 ret <2 x i64> %r 817} 818 819; Converting integer logic ops to vector select is allowed. 820 821define i64 @bitcast_int_scalar_cond(<2 x i1> %b, i64 %c, i64 %d) { 822; CHECK-LABEL: @bitcast_int_scalar_cond( 823; CHECK-NEXT: [[TMP1:%.*]] = bitcast i64 [[D:%.*]] to <2 x i32> 824; CHECK-NEXT: [[TMP2:%.*]] = bitcast i64 [[C:%.*]] to <2 x i32> 825; CHECK-NEXT: [[TMP3:%.*]] = select <2 x i1> [[B:%.*]], <2 x i32> [[TMP1]], <2 x i32> [[TMP2]] 826; CHECK-NEXT: [[TMP4:%.*]] = bitcast <2 x i32> [[TMP3]] to i64 827; CHECK-NEXT: ret i64 [[TMP4]] 828; 829 %s = sext <2 x i1> %b to <2 x i32> 830 %t9 = bitcast <2 x i32> %s to i64 831 %nott9 = xor i64 %t9, -1 832 %t11 = and i64 %nott9, %c 833 %t12 = and i64 %t9, %d 834 %r = or i64 %t11, %t12 835 ret i64 %r 836} 837 838; Peek through bitcasts and sexts to find negated bool condition. 839 840define <1 x i6> @bitcast_sext_cond(<2 x i1> %cmp, <1 x i6> %a, <1 x i6> %b) { 841; CHECK-LABEL: @bitcast_sext_cond( 842; CHECK-NEXT: [[TMP1:%.*]] = bitcast <1 x i6> [[A:%.*]] to <2 x i3> 843; CHECK-NEXT: [[TMP2:%.*]] = bitcast <1 x i6> [[B:%.*]] to <2 x i3> 844; CHECK-NEXT: [[TMP3:%.*]] = select <2 x i1> [[CMP:%.*]], <2 x i3> [[TMP1]], <2 x i3> [[TMP2]] 845; CHECK-NEXT: [[TMP4:%.*]] = bitcast <2 x i3> [[TMP3]] to <1 x i6> 846; CHECK-NEXT: ret <1 x i6> [[TMP4]] 847; 848 %sext = sext <2 x i1> %cmp to <2 x i3> 849 %bc1 = bitcast <2 x i3> %sext to <1 x i6> 850 %neg = xor <2 x i1> %cmp, <i1 -1, i1 -1> 851 %sext2 = sext <2 x i1> %neg to <2 x i3> 852 %bc2 = bitcast <2 x i3> %sext2 to <1 x i6> 853 %and1 = and <1 x i6> %bc1, %a 854 %and2 = and <1 x i6> %bc2, %b 855 %or = or <1 x i6> %and1, %and2 856 ret <1 x i6> %or 857} 858 859; Extra uses may prevent other transforms from creating the canonical patterns. 860 861define i8 @sext_cond_extra_uses(i1 %cmp, i8 %a, i8 %b) { 862; CHECK-LABEL: @sext_cond_extra_uses( 863; CHECK-NEXT: [[NEG:%.*]] = xor i1 [[CMP:%.*]], true 864; CHECK-NEXT: [[SEXT1:%.*]] = sext i1 [[CMP]] to i8 865; CHECK-NEXT: call void @use(i8 [[SEXT1]]) 866; CHECK-NEXT: [[SEXT2:%.*]] = sext i1 [[NEG]] to i8 867; CHECK-NEXT: call void @use(i8 [[SEXT2]]) 868; CHECK-NEXT: [[TMP1:%.*]] = select i1 [[CMP]], i8 [[A:%.*]], i8 [[B:%.*]] 869; CHECK-NEXT: ret i8 [[TMP1]] 870; 871 %neg = xor i1 %cmp, -1 872 %sext1 = sext i1 %cmp to i8 873 call void @use(i8 %sext1) 874 %sext2 = sext i1 %neg to i8 875 call void @use(i8 %sext2) 876 %and1 = and i8 %sext1, %a 877 %and2 = and i8 %sext2, %b 878 %or = or i8 %and1, %and2 879 ret i8 %or 880} 881