479 lines · plain
1; NOTE: Assertions have been autogenerated by utils/update_test_checks.py2; RUN: opt < %s -passes=instcombine -S | FileCheck %s3 4define i32 @sub1(i32 %x) {5; CHECK-LABEL: @sub1(6; CHECK-NEXT: [[Y:%.*]] = sub i32 0, [[X:%.*]]7; CHECK-NEXT: [[Z:%.*]] = sdiv i32 [[Y]], 3378; CHECK-NEXT: ret i32 [[Z]]9;10 %y = sub i32 0, %x11 %z = sdiv i32 %y, 33712 ret i32 %z13}14 15define i32 @sub2(i32 %x) {16; CHECK-LABEL: @sub2(17; CHECK-NEXT: [[Z:%.*]] = sdiv i32 [[X:%.*]], -33718; CHECK-NEXT: ret i32 [[Z]]19;20 %y = sub nsw i32 0, %x21 %z = sdiv i32 %y, 33722 ret i32 %z23}24 25define i1 @shl_icmp(i64 %X) {26; CHECK-LABEL: @shl_icmp(27; CHECK-NEXT: [[B:%.*]] = icmp eq i64 [[X:%.*]], 028; CHECK-NEXT: ret i1 [[B]]29;30 %A = shl nuw i64 %X, 2 ; X/431 %B = icmp eq i64 %A, 032 ret i1 %B33}34 35define i64 @shl1(i64 %X, ptr %P) {36; CHECK-LABEL: @shl1(37; CHECK-NEXT: [[A:%.*]] = and i64 [[X:%.*]], 31238; CHECK-NEXT: store i64 [[A]], ptr [[P:%.*]], align 439; CHECK-NEXT: [[B:%.*]] = shl nuw nsw i64 [[A]], 840; CHECK-NEXT: ret i64 [[B]]41;42 %A = and i64 %X, 31243 store i64 %A, ptr %P ; multiple uses of A.44 %B = shl i64 %A, 845 ret i64 %B46}47 48define i32 @preserve1(i32 %x) {49; CHECK-LABEL: @preserve1(50; CHECK-NEXT: [[ADD3:%.*]] = add nsw i32 [[X:%.*]], 551; CHECK-NEXT: ret i32 [[ADD3]]52;53 %add = add nsw i32 %x, 254 %add3 = add nsw i32 %add, 355 ret i32 %add356}57 58define i8 @nopreserve1(i8 %x) {59; CHECK-LABEL: @nopreserve1(60; CHECK-NEXT: [[ADD3:%.*]] = add i8 [[X:%.*]], -12661; CHECK-NEXT: ret i8 [[ADD3]]62;63 %add = add nsw i8 %x, 12764 %add3 = add nsw i8 %add, 365 ret i8 %add366}67 68define i8 @nopreserve2(i8 %x) {69; CHECK-LABEL: @nopreserve2(70; CHECK-NEXT: [[ADD3:%.*]] = add i8 [[X:%.*]], 371; CHECK-NEXT: ret i8 [[ADD3]]72;73 %add = add i8 %x, 174 %add3 = add nsw i8 %add, 275 ret i8 %add376}77 78define i8 @nopreserve3(i8 %A, i8 %B) {79; CHECK-LABEL: @nopreserve3(80; CHECK-NEXT: [[Y:%.*]] = add i8 [[A:%.*]], [[B:%.*]]81; CHECK-NEXT: [[ADD:%.*]] = add i8 [[Y]], 2082; CHECK-NEXT: ret i8 [[ADD]]83;84 %x = add i8 %A, 1085 %y = add i8 %B, 1086 %add = add nsw i8 %x, %y87 ret i8 %add88}89 90define i8 @nopreserve4(i8 %A, i8 %B) {91; CHECK-LABEL: @nopreserve4(92; CHECK-NEXT: [[Y:%.*]] = add i8 [[A:%.*]], [[B:%.*]]93; CHECK-NEXT: [[ADD:%.*]] = add i8 [[Y]], 2094; CHECK-NEXT: ret i8 [[ADD]]95;96 %x = add nsw i8 %A, 1097 %y = add nsw i8 %B, 1098 %add = add nsw i8 %x, %y99 ret i8 %add100}101 102define <3 x i32> @shl_nuw_nsw_shuffle_splat_vec(<2 x i8> %x) {103; CHECK-LABEL: @shl_nuw_nsw_shuffle_splat_vec(104; CHECK-NEXT: [[T2:%.*]] = zext <2 x i8> [[X:%.*]] to <2 x i32>105; CHECK-NEXT: [[SHUF:%.*]] = shufflevector <2 x i32> [[T2]], <2 x i32> poison, <3 x i32> <i32 1, i32 0, i32 1>106; CHECK-NEXT: [[T3:%.*]] = shl nuw nsw <3 x i32> [[SHUF]], splat (i32 17)107; CHECK-NEXT: ret <3 x i32> [[T3]]108;109 %t2 = zext <2 x i8> %x to <2 x i32>110 %shuf = shufflevector <2 x i32> %t2, <2 x i32> undef, <3 x i32> <i32 1, i32 0, i32 1>111 %t3 = shl <3 x i32> %shuf, <i32 17, i32 17, i32 17>112 ret <3 x i32> %t3113}114 115; Negative test - if the shuffle mask contains an undef, we bail out to116; avoid propagating information that may not be used consistently by callers.117 118define <3 x i32> @shl_nuw_nsw_shuffle_undef_elt_splat_vec(<2 x i8> %x) {119; CHECK-LABEL: @shl_nuw_nsw_shuffle_undef_elt_splat_vec(120; CHECK-NEXT: [[T2:%.*]] = zext <2 x i8> [[X:%.*]] to <2 x i32>121; CHECK-NEXT: [[SHUF:%.*]] = shufflevector <2 x i32> [[T2]], <2 x i32> poison, <3 x i32> <i32 1, i32 poison, i32 0>122; CHECK-NEXT: [[T3:%.*]] = shl <3 x i32> [[SHUF]], splat (i32 17)123; CHECK-NEXT: ret <3 x i32> [[T3]]124;125 %t2 = zext <2 x i8> %x to <2 x i32>126 %shuf = shufflevector <2 x i32> %t2, <2 x i32> undef, <3 x i32> <i32 1, i32 undef, i32 0>127 %t3 = shl <3 x i32> %shuf, <i32 17, i32 17, i32 17>128 ret <3 x i32> %t3129}130 131; Make sure we don't crash on a ConstantExpr shufflevector132define <vscale x 2 x i64> @mul_nuw_nsw_shuffle_constant_expr(<vscale x 2 x i8> %z) {133; CHECK-LABEL: @mul_nuw_nsw_shuffle_constant_expr(134; CHECK-NEXT: [[XX:%.*]] = zext <vscale x 2 x i8> [[Z:%.*]] to <vscale x 2 x i64>135; CHECK-NEXT: [[T3:%.*]] = mul <vscale x 2 x i64> [[XX]], shufflevector (<vscale x 2 x i64> insertelement (<vscale x 2 x i64> undef, i64 3, i32 0), <vscale x 2 x i64> zeroinitializer, <vscale x 2 x i32> zeroinitializer)136; CHECK-NEXT: ret <vscale x 2 x i64> [[T3]]137;138 %xx = zext <vscale x 2 x i8> %z to <vscale x 2 x i64>139 %shuf = shufflevector <vscale x 2 x i64> insertelement (<vscale x 2 x i64> undef, i64 3, i32 0), <vscale x 2 x i64> zeroinitializer, <vscale x 2 x i32> zeroinitializer140 %t3 = mul <vscale x 2 x i64> %shuf, %xx141 ret <vscale x 2 x i64> %t3142}143 144; This could propagate nsw.145 146define i32 @neg_sub0_sub_nsw_nsw(i32 %a, i32 %b) {147; CHECK-LABEL: @neg_sub0_sub_nsw_nsw(148; CHECK-NEXT: [[C_NEG:%.*]] = sub nsw i32 [[B:%.*]], [[A:%.*]]149; CHECK-NEXT: ret i32 [[C_NEG]]150;151 %c = sub nsw i32 %a, %b152 %d = sub nsw i32 0, %c153 ret i32 %d154}155 156; Must not propagate nsw.157 158define i32 @neg_sub_sub_nsw0(i32 %a, i32 %b) {159; CHECK-LABEL: @neg_sub_sub_nsw0(160; CHECK-NEXT: [[C_NEG:%.*]] = sub i32 [[B:%.*]], [[A:%.*]]161; CHECK-NEXT: ret i32 [[C_NEG]]162;163 %c = sub nsw i32 %a, %b164 %d = sub i32 0, %c165 ret i32 %d166}167 168; Must not propagate nsw.169 170define i32 @neg_sub_sub_nsw1(i32 %a, i32 %b) {171; CHECK-LABEL: @neg_sub_sub_nsw1(172; CHECK-NEXT: [[C_NEG:%.*]] = sub i32 [[B:%.*]], [[A:%.*]]173; CHECK-NEXT: ret i32 [[C_NEG]]174;175 %c = sub i32 %a, %b176 %d = sub nsw i32 0, %c177 ret i32 %d178}179 180; This could propagate nsw.181 182define i32 @neg_mul_sub_nsw_nsw(i32 %a, i32 %b) {183; CHECK-LABEL: @neg_mul_sub_nsw_nsw(184; CHECK-NEXT: [[C_NEG:%.*]] = sub nsw i32 [[B:%.*]], [[A:%.*]]185; CHECK-NEXT: ret i32 [[C_NEG]]186;187 %c = sub nsw i32 %a, %b188 %d = mul nsw i32 -1, %c189 ret i32 %d190}191 192; Must not propagate nsw.193 194define i32 @neg_mul_sub_nsw0(i32 %a, i32 %b) {195; CHECK-LABEL: @neg_mul_sub_nsw0(196; CHECK-NEXT: [[C_NEG:%.*]] = sub i32 [[B:%.*]], [[A:%.*]]197; CHECK-NEXT: ret i32 [[C_NEG]]198;199 %c = sub nsw i32 %a, %b200 %d = mul i32 -1, %c201 ret i32 %d202}203 204; Must not propagate nsw.205 206define i32 @neg_mul_sub_nsw1(i32 %a, i32 %b) {207; CHECK-LABEL: @neg_mul_sub_nsw1(208; CHECK-NEXT: [[C_NEG:%.*]] = sub i32 [[B:%.*]], [[A:%.*]]209; CHECK-NEXT: ret i32 [[C_NEG]]210;211 %c = sub i32 %a, %b212 %d = mul nsw i32 -1, %c213 ret i32 %d214}215 216; Must not propagate nsw.217 218define i8 @neg_sub_sub2(i16 %a, i16 %b) {219; CHECK-LABEL: @neg_sub_sub2(220; CHECK-NEXT: [[C_NEG:%.*]] = sub i16 [[B:%.*]], [[A:%.*]]221; CHECK-NEXT: [[D_NEG:%.*]] = trunc i16 [[C_NEG]] to i8222; CHECK-NEXT: ret i8 [[D_NEG]]223;224 %c = sub nsw i16 %a, %b225 %d = trunc i16 %c to i8226 %e = sub nsw i8 0, %d227 ret i8 %e228}229 230; Must not propagate nsw.231 232define i32 @sub_sub0_nsw_nsw(i32 %a, i32 %b, i32 %c) {233; CHECK-LABEL: @sub_sub0_nsw_nsw(234; CHECK-NEXT: [[TMP1:%.*]] = add i32 [[B:%.*]], [[C:%.*]]235; CHECK-NEXT: [[S2:%.*]] = sub i32 [[A:%.*]], [[TMP1]]236; CHECK-NEXT: ret i32 [[S2]]237;238 %s1 = sub nsw i32 %a, %b239 %s2 = sub nsw i32 %s1, %c240 ret i32 %s2241}242 243; Must not propagate nsw.244 245define i32 @sub_sub1_nsw_nsw(i32 %a, i32 %b, i32 %c) {246; CHECK-LABEL: @sub_sub1_nsw_nsw(247; CHECK-NEXT: [[S1_NEG:%.*]] = sub i32 [[B:%.*]], [[A:%.*]]248; CHECK-NEXT: [[S2:%.*]] = add i32 [[S1_NEG]], [[C:%.*]]249; CHECK-NEXT: ret i32 [[S2]]250;251 %s1 = sub nsw i32 %a, %b252 %s2 = sub nsw i32 %c, %s1253 ret i32 %s2254}255 256define i8 @neg_nsw_freeze(i8 %a1, i8 %a2) {257; CHECK-LABEL: @neg_nsw_freeze(258; CHECK-NEXT: [[A1_FR:%.*]] = freeze i8 [[A1:%.*]]259; CHECK-NEXT: [[A2_FR:%.*]] = freeze i8 [[A2:%.*]]260; CHECK-NEXT: [[A_NEG:%.*]] = sub i8 [[A2_FR]], [[A1_FR]]261; CHECK-NEXT: ret i8 [[A_NEG]]262;263 %a = sub nsw i8 %a1, %a2264 %fr = freeze i8 %a265 %neg = sub nsw i8 0, %fr266 ret i8 %neg267}268 269define i8 @neg_nsw_phi(i1 %c, i8 %a1, i8 %a2, i8 %b1, i8 %b2) {270; CHECK-LABEL: @neg_nsw_phi(271; CHECK-NEXT: br i1 [[C:%.*]], label [[IF:%.*]], label [[ELSE:%.*]]272; CHECK: if:273; CHECK-NEXT: [[A_NEG:%.*]] = sub nsw i8 [[A2:%.*]], [[A1:%.*]]274; CHECK-NEXT: br label [[JOIN:%.*]]275; CHECK: else:276; CHECK-NEXT: [[B_NEG:%.*]] = sub nsw i8 [[B2:%.*]], [[B1:%.*]]277; CHECK-NEXT: br label [[JOIN]]278; CHECK: join:279; CHECK-NEXT: [[PHI_NEG:%.*]] = phi i8 [ [[A_NEG]], [[IF]] ], [ [[B_NEG]], [[ELSE]] ]280; CHECK-NEXT: ret i8 [[PHI_NEG]]281;282 br i1 %c, label %if, label %else283 284if:285 %a = sub nsw i8 %a1, %a2286 br label %join287 288else:289 %b = sub nsw i8 %b1, %b2290 br label %join291 292join:293 %phi = phi i8 [ %a, %if ], [ %b, %else ]294 %neg = sub nsw i8 0, %phi295 ret i8 %neg296}297 298define i8 @neg_nsw_select(i1 %c, i8 %a1, i8 %a2, i8 %b1, i8 %b2) {299; CHECK-LABEL: @neg_nsw_select(300; CHECK-NEXT: [[A_NEG:%.*]] = sub nsw i8 [[A2:%.*]], [[A1:%.*]]301; CHECK-NEXT: [[B_NEG:%.*]] = sub nsw i8 [[B2:%.*]], [[B1:%.*]]302; CHECK-NEXT: [[SEL_NEG:%.*]] = select i1 [[C:%.*]], i8 [[A_NEG]], i8 [[B_NEG]]303; CHECK-NEXT: ret i8 [[SEL_NEG]]304;305 %a = sub nsw i8 %a1, %a2306 %b = sub nsw i8 %b1, %b2307 %sel = select i1 %c, i8 %a, i8 %b308 %neg = sub nsw i8 0, %sel309 ret i8 %neg310}311 312define <4 x i8> @neg_nsw_shufflevector(<2 x i8> %a1, <2 x i8> %a2, <2 x i8> %b1, <2 x i8> %b2) {313; CHECK-LABEL: @neg_nsw_shufflevector(314; CHECK-NEXT: [[A_NEG:%.*]] = sub nsw <2 x i8> [[A2:%.*]], [[A1:%.*]]315; CHECK-NEXT: [[B_NEG:%.*]] = sub nsw <2 x i8> [[B2:%.*]], [[B1:%.*]]316; CHECK-NEXT: [[SHUF_NEG:%.*]] = shufflevector <2 x i8> [[A_NEG]], <2 x i8> [[B_NEG]], <4 x i32> <i32 0, i32 1, i32 2, i32 3>317; CHECK-NEXT: ret <4 x i8> [[SHUF_NEG]]318;319 %a = sub nsw <2 x i8> %a1, %a2320 %b = sub nsw <2 x i8> %b1, %b2321 %shuf = shufflevector <2 x i8> %a, <2 x i8> %b, <4 x i32> <i32 0, i32 1, i32 2, i32 3>322 %neg = sub nsw <4 x i8> zeroinitializer, %shuf323 ret <4 x i8> %neg324}325 326define i8 @neg_nsw_extractelement(<2 x i8> %a1, <2 x i8> %a2) {327; CHECK-LABEL: @neg_nsw_extractelement(328; CHECK-NEXT: [[A_NEG:%.*]] = sub nsw <2 x i8> [[A2:%.*]], [[A1:%.*]]329; CHECK-NEXT: [[EXTR_NEG:%.*]] = extractelement <2 x i8> [[A_NEG]], i64 0330; CHECK-NEXT: ret i8 [[EXTR_NEG]]331;332 %a = sub nsw <2 x i8> %a1, %a2333 %extr = extractelement <2 x i8> %a, i32 0334 %neg = sub nsw i8 0, %extr335 ret i8 %neg336}337 338define <2 x i8> @neg_nsw_insertelement(<2 x i8> %a1, <2 x i8> %a2, i8 %b1, i8 %b2) {339; CHECK-LABEL: @neg_nsw_insertelement(340; CHECK-NEXT: [[A_NEG:%.*]] = sub nsw <2 x i8> [[A2:%.*]], [[A1:%.*]]341; CHECK-NEXT: [[B_NEG:%.*]] = sub nsw i8 [[B2:%.*]], [[B1:%.*]]342; CHECK-NEXT: [[INSERT_NEG:%.*]] = insertelement <2 x i8> [[A_NEG]], i8 [[B_NEG]], i64 0343; CHECK-NEXT: ret <2 x i8> [[INSERT_NEG]]344;345 %a = sub nsw <2 x i8> %a1, %a2346 %b = sub nsw i8 %b1, %b2347 %insert = insertelement <2 x i8> %a, i8 %b, i32 0348 %neg = sub nsw <2 x i8> zeroinitializer, %insert349 ret <2 x i8> %neg350}351 352define i8 @neg_nsw_shl(i8 %a1, i8 %a2, i8 %b) {353; CHECK-LABEL: @neg_nsw_shl(354; CHECK-NEXT: [[A_NEG:%.*]] = sub nsw i8 [[A2:%.*]], [[A1:%.*]]355; CHECK-NEXT: [[SHL_NEG:%.*]] = shl nsw i8 [[A_NEG]], [[B:%.*]]356; CHECK-NEXT: ret i8 [[SHL_NEG]]357;358 %a = sub nsw i8 %a1, %a2359 %shl = shl nsw i8 %a, %b360 %neg = sub nsw i8 0, %shl361 ret i8 %neg362}363 364define i8 @neg_nsw_shl_missing_nsw_on_shl(i8 %a1, i8 %a2, i8 %b) {365; CHECK-LABEL: @neg_nsw_shl_missing_nsw_on_shl(366; CHECK-NEXT: [[A_NEG:%.*]] = sub i8 [[A2:%.*]], [[A1:%.*]]367; CHECK-NEXT: [[SHL_NEG:%.*]] = shl i8 [[A_NEG]], [[B:%.*]]368; CHECK-NEXT: ret i8 [[SHL_NEG]]369;370 %a = sub nsw i8 %a1, %a2371 %shl = shl i8 %a, %b372 %neg = sub nsw i8 0, %shl373 ret i8 %neg374}375 376define i8 @neg_nsw_shl_to_mul(i8 %a, i8 %b) {377; CHECK-LABEL: @neg_nsw_shl_to_mul(378; CHECK-NEXT: [[SHL_NEG:%.*]] = mul nsw i8 [[A:%.*]], -2379; CHECK-NEXT: ret i8 [[SHL_NEG]]380;381 %shl = shl nsw i8 %a, 1382 %neg = sub nsw i8 0, %shl383 ret i8 %neg384}385 386define i8 @neg_nsw_shl_to_mul_missing_nsw_on_shl(i8 %a, i8 %b) {387; CHECK-LABEL: @neg_nsw_shl_to_mul_missing_nsw_on_shl(388; CHECK-NEXT: [[SHL_NEG:%.*]] = mul i8 [[A:%.*]], -2389; CHECK-NEXT: ret i8 [[SHL_NEG]]390;391 %shl = shl i8 %a, 1392 %neg = sub nsw i8 0, %shl393 ret i8 %neg394}395 396define i8 @neg_nsw_mul(i8 %a1, i8 %a2, i8 %b) {397; CHECK-LABEL: @neg_nsw_mul(398; CHECK-NEXT: [[A_NEG:%.*]] = sub i8 [[A2:%.*]], [[A1:%.*]]399; CHECK-NEXT: [[SHL_NEG:%.*]] = mul nsw i8 [[A_NEG]], [[B:%.*]]400; CHECK-NEXT: ret i8 [[SHL_NEG]]401;402 %a = sub nsw i8 %a1, %a2403 %shl = mul nsw i8 %a, %b404 %neg = sub nsw i8 0, %shl405 ret i8 %neg406}407 408define i8 @neg_nsw_mul_missing_nsw_on_mul(i8 %a1, i8 %a2, i8 %b) {409; CHECK-LABEL: @neg_nsw_mul_missing_nsw_on_mul(410; CHECK-NEXT: [[A_NEG:%.*]] = sub i8 [[A2:%.*]], [[A1:%.*]]411; CHECK-NEXT: [[SHL_NEG:%.*]] = mul i8 [[A_NEG]], [[B:%.*]]412; CHECK-NEXT: ret i8 [[SHL_NEG]]413;414 %a = sub nsw i8 %a1, %a2415 %shl = mul i8 %a, %b416 %neg = sub nsw i8 0, %shl417 ret i8 %neg418}419 420; This could propagate nsw.421 422define i16 @mul_nsw_reassoc_prop(i16 %x) {423; CHECK-LABEL: @mul_nsw_reassoc_prop(424; CHECK-NEXT: [[B:%.*]] = mul nsw i16 [[X:%.*]], 6425; CHECK-NEXT: ret i16 [[B]]426;427 %a = mul nsw i16 %x, 3428 %b = mul nsw i16 %a, 2429 ret i16 %b430}431 432; This could propagate nsw.433 434define i16 @mul_nsw_reassoc_prop_neg(i16 %x) {435; CHECK-LABEL: @mul_nsw_reassoc_prop_neg(436; CHECK-NEXT: [[B:%.*]] = mul nsw i16 [[X:%.*]], -2201437; CHECK-NEXT: ret i16 [[B]]438;439 %a = mul nsw i16 %x, -71440 %b = mul nsw i16 %a, 31441 ret i16 %b442}443 444; Must not propagate nsw.445 446define i16 @mul_nsw_reassoc_prop_no_nsw1(i16 %x) {447; CHECK-LABEL: @mul_nsw_reassoc_prop_no_nsw1(448; CHECK-NEXT: [[B:%.*]] = mul i16 [[X:%.*]], 6449; CHECK-NEXT: ret i16 [[B]]450;451 %a = mul i16 %x, 3452 %b = mul nsw i16 %a, 2453 ret i16 %b454}455 456; Must not propagate nsw.457 458define i16 @mul_nsw_reassoc_prop_no_nsw2(i16 %x) {459; CHECK-LABEL: @mul_nsw_reassoc_prop_no_nsw2(460; CHECK-NEXT: [[B:%.*]] = mul i16 [[X:%.*]], 6461; CHECK-NEXT: ret i16 [[B]]462;463 %a = mul nsw i16 %x, 3464 %b = mul i16 %a, 2465 ret i16 %b466}467 468; Must not propagate nsw.469 470define i16 @mul_nsw_reassoc_prop_overflow(i16 %x) {471; CHECK-LABEL: @mul_nsw_reassoc_prop_overflow(472; CHECK-NEXT: [[B:%.*]] = mul i16 [[X:%.*]], -31777473; CHECK-NEXT: ret i16 [[B]]474;475 %a = mul nsw i16 %x, 1023476 %b = mul nsw i16 %a, 33477 ret i16 %b478}479