Index: test/Transforms/InstCombine/select-of-bittest.ll =================================================================== --- /dev/null +++ test/Transforms/InstCombine/select-of-bittest.ll @@ -0,0 +1,414 @@ +; NOTE: Assertions have been autogenerated by utils/update_test_checks.py +; RUN: opt < %s -instcombine -S | FileCheck %s + +; https://bugs.llvm.org/show_bug.cgi?id=36950 + +; These all should be just and+icmp, there should be no select. +; https://rise4fun.com/Alive/uiH + +define i32 @f_m2(i32) { +; CHECK-LABEL: @f_m2( +; CHECK-NEXT: [[TMP2:%.*]] = and i32 [[TMP0:%.*]], 1 +; CHECK-NEXT: [[TMP3:%.*]] = icmp eq i32 [[TMP2]], 0 +; CHECK-NEXT: [[TMP4:%.*]] = lshr i32 [[TMP0]], 1 +; CHECK-NEXT: [[TMP5:%.*]] = and i32 [[TMP4]], 1 +; CHECK-NEXT: [[TMP6:%.*]] = select i1 [[TMP3]], i32 [[TMP5]], i32 1 +; CHECK-NEXT: ret i32 [[TMP6]] +; + %2 = and i32 %0, 1 + %3 = icmp eq i32 %2, 0 + %4 = lshr i32 %0, 1 + %5 = and i32 %4, 1 + %6 = select i1 %3, i32 %5, i32 1 + ret i32 %6 +} + +define <2 x i32> @f_m2_vec(<2 x i32>) { +; CHECK-LABEL: @f_m2_vec( +; CHECK-NEXT: [[TMP2:%.*]] = and <2 x i32> [[TMP0:%.*]], +; CHECK-NEXT: [[TMP3:%.*]] = icmp eq <2 x i32> [[TMP2]], zeroinitializer +; CHECK-NEXT: [[TMP4:%.*]] = lshr <2 x i32> [[TMP0]], +; CHECK-NEXT: [[TMP5:%.*]] = and <2 x i32> [[TMP4]], +; CHECK-NEXT: [[TMP6:%.*]] = select <2 x i1> [[TMP3]], <2 x i32> [[TMP5]], <2 x i32> +; CHECK-NEXT: ret <2 x i32> [[TMP6]] +; + %2 = and <2 x i32> %0, + %3 = icmp eq <2 x i32> %2, + %4 = lshr <2 x i32> %0, + %5 = and <2 x i32> %4, + %6 = select <2 x i1> %3, <2 x i32> %5, <2 x i32> + ret <2 x i32> %6 +} + +define <3 x i32> @f_m2_vec_undef(<3 x i32>) { +; CHECK-LABEL: @f_m2_vec_undef( +; CHECK-NEXT: [[TMP2:%.*]] = and <3 x i32> [[TMP0:%.*]], +; CHECK-NEXT: [[TMP3:%.*]] = icmp eq <3 x i32> [[TMP2]], +; CHECK-NEXT: [[TMP4:%.*]] = lshr <3 x i32> [[TMP0]], +; CHECK-NEXT: [[TMP5:%.*]] = and <3 x i32> [[TMP4]], +; CHECK-NEXT: [[TMP6:%.*]] = select <3 x i1> [[TMP3]], <3 x i32> [[TMP5]], <3 x i32> +; CHECK-NEXT: ret <3 x i32> [[TMP6]] +; + %2 = and <3 x i32> %0, + %3 = icmp eq <3 x i32> %2, + %4 = lshr <3 x i32> %0, + %5 = and <3 x i32> %4, + %6 = select <3 x i1> %3, <3 x i32> %5, <3 x i32> + ret <3 x i32> %6 +} + +define i32 @f_m3(i32) { +; CHECK-LABEL: @f_m3( +; CHECK-NEXT: [[TMP2:%.*]] = and i32 [[TMP0:%.*]], 2 +; CHECK-NEXT: [[TMP3:%.*]] = icmp eq i32 [[TMP2]], 0 +; CHECK-NEXT: [[TMP4:%.*]] = and i32 [[TMP0]], 1 +; CHECK-NEXT: [[TMP5:%.*]] = select i1 [[TMP3]], i32 [[TMP4]], i32 1 +; CHECK-NEXT: ret i32 [[TMP5]] +; + %2 = and i32 %0, 2 + %3 = icmp eq i32 %2, 0 + %4 = and i32 %0, 1 + %5 = select i1 %3, i32 %4, i32 1 + ret i32 %5 +} + +define <2 x i32> @f_m3_vec(<2 x i32>) { +; CHECK-LABEL: @f_m3_vec( +; CHECK-NEXT: [[TMP2:%.*]] = and <2 x i32> [[TMP0:%.*]], +; CHECK-NEXT: [[TMP3:%.*]] = icmp eq <2 x i32> [[TMP2]], zeroinitializer +; CHECK-NEXT: [[TMP4:%.*]] = and <2 x i32> [[TMP0]], +; CHECK-NEXT: [[TMP5:%.*]] = select <2 x i1> [[TMP3]], <2 x i32> [[TMP4]], <2 x i32> +; CHECK-NEXT: ret <2 x i32> [[TMP5]] +; + %2 = and <2 x i32> %0, + %3 = icmp eq <2 x i32> %2, + %4 = and <2 x i32> %0, + %5 = select <2 x i1> %3, <2 x i32> %4, <2 x i32> + ret <2 x i32> %5 +} + +define <3 x i32> @f_m3_vec_undef(<3 x i32>) { +; CHECK-LABEL: @f_m3_vec_undef( +; CHECK-NEXT: [[TMP2:%.*]] = and <3 x i32> [[TMP0:%.*]], +; CHECK-NEXT: [[TMP3:%.*]] = icmp eq <3 x i32> [[TMP2]], +; CHECK-NEXT: [[TMP4:%.*]] = and <3 x i32> [[TMP0]], +; CHECK-NEXT: [[TMP5:%.*]] = select <3 x i1> [[TMP3]], <3 x i32> [[TMP4]], <3 x i32> +; CHECK-NEXT: ret <3 x i32> [[TMP5]] +; + %2 = and <3 x i32> %0, + %3 = icmp eq <3 x i32> %2, + %4 = and <3 x i32> %0, + %5 = select <3 x i1> %3, <3 x i32> %4, <3 x i32> + ret <3 x i32> %5 +} + +define i32 @f_m6(i32) { +; CHECK-LABEL: @f_m6( +; CHECK-NEXT: [[TMP2:%.*]] = and i32 [[TMP0:%.*]], 3 +; CHECK-NEXT: [[TMP3:%.*]] = icmp eq i32 [[TMP2]], 0 +; CHECK-NEXT: [[TMP4:%.*]] = lshr i32 [[TMP0]], 1 +; CHECK-NEXT: [[TMP5:%.*]] = and i32 [[TMP4]], 1 +; CHECK-NEXT: [[TMP6:%.*]] = select i1 [[TMP3]], i32 [[TMP5]], i32 1 +; CHECK-NEXT: ret i32 [[TMP6]] +; + %2 = and i32 %0, 3 + %3 = icmp eq i32 %2, 0 + %4 = lshr i32 %0, 1 + %5 = and i32 %4, 1 + %6 = select i1 %3, i32 %5, i32 1 + ret i32 %6 +} + +define i32 @f_m10(i32) { +; CHECK-LABEL: @f_m10( +; CHECK-NEXT: [[TMP2:%.*]] = and i32 [[TMP0:%.*]], 4 +; CHECK-NEXT: [[TMP3:%.*]] = icmp eq i32 [[TMP2]], 0 +; CHECK-NEXT: [[TMP4:%.*]] = lshr i32 [[TMP0]], 1 +; CHECK-NEXT: [[TMP5:%.*]] = and i32 [[TMP4]], 1 +; CHECK-NEXT: [[TMP6:%.*]] = select i1 [[TMP3]], i32 [[TMP5]], i32 1 +; CHECK-NEXT: ret i32 [[TMP6]] +; + %2 = and i32 %0, 4 + %3 = icmp eq i32 %2, 0 + %4 = lshr i32 %0, 1 + %5 = and i32 %4, 1 + %6 = select i1 %3, i32 %5, i32 1 + ret i32 %6 +} + +define i32 @f_m12(i32) { +; CHECK-LABEL: @f_m12( +; CHECK-NEXT: [[TMP2:%.*]] = and i32 [[TMP0:%.*]], 2 +; CHECK-NEXT: [[TMP3:%.*]] = icmp eq i32 [[TMP2]], 0 +; CHECK-NEXT: [[TMP4:%.*]] = lshr i32 [[TMP0]], 2 +; CHECK-NEXT: [[TMP5:%.*]] = and i32 [[TMP4]], 1 +; CHECK-NEXT: [[TMP6:%.*]] = select i1 [[TMP3]], i32 [[TMP5]], i32 1 +; CHECK-NEXT: ret i32 [[TMP6]] +; + %2 = and i32 %0, 2 + %3 = icmp eq i32 %2, 0 + %4 = lshr i32 %0, 2 + %5 = and i32 %4, 1 + %6 = select i1 %3, i32 %5, i32 1 + ret i32 %6 +} + +; ============================================================================ ; +; Mask can be a variable, too. +; ============================================================================ ; + +define i32 @f_var0(i32, i32) { +; CHECK-LABEL: @f_var0( +; CHECK-NEXT: [[TMP3:%.*]] = lshr i32 [[TMP0:%.*]], 1 +; CHECK-NEXT: [[TMP4:%.*]] = and i32 [[TMP3]], 1 +; CHECK-NEXT: ret i32 [[TMP4]] +; + %3 = and i32 %0, %1 + %4 = icmp eq i32 %3, 0 + %5 = lshr i32 %0, 1 + %6 = and i32 %5, 1 + %7 = select i1 %4, i32 %6, i32 1 + ret i32 %6 +} + +define <2 x i32> @f_var0_vec(<2 x i32>, <2 x i32>) { +; CHECK-LABEL: @f_var0_vec( +; CHECK-NEXT: [[TMP3:%.*]] = lshr <2 x i32> [[TMP0:%.*]], +; CHECK-NEXT: [[TMP4:%.*]] = and <2 x i32> [[TMP3]], +; CHECK-NEXT: ret <2 x i32> [[TMP4]] +; + %3 = and <2 x i32> %0, %1 + %4 = icmp eq <2 x i32> %3, + %5 = lshr <2 x i32> %0, + %6 = and <2 x i32> %5, + %7 = select <2 x i1> %4, <2 x i32> %6, <2 x i32> + ret <2 x i32> %6 +} + +define <3 x i32> @f_var0_vec_undef(<3 x i32>, <3 x i32>) { +; CHECK-LABEL: @f_var0_vec_undef( +; CHECK-NEXT: [[TMP3:%.*]] = lshr <3 x i32> [[TMP0:%.*]], +; CHECK-NEXT: [[TMP4:%.*]] = and <3 x i32> [[TMP3]], +; CHECK-NEXT: ret <3 x i32> [[TMP4]] +; + %3 = and <3 x i32> %0, %1 + %4 = icmp eq <3 x i32> %3, + %5 = lshr <3 x i32> %0, + %6 = and <3 x i32> %5, + %7 = select <3 x i1> %4, <3 x i32> %6, <3 x i32> + ret <3 x i32> %6 +} + +define i32 @f_var1(i32, i32) { +; CHECK-LABEL: @f_var1( +; CHECK-NEXT: [[TMP3:%.*]] = and i32 [[TMP0:%.*]], 1 +; CHECK-NEXT: ret i32 [[TMP3]] +; + %3 = and i32 %0, %1 + %4 = icmp eq i32 %3, 0 + %5 = and i32 %0, 1 + %6 = select i1 %4, i32 %5, i32 1 + ret i32 %5 +} + +define <2 x i32> @f_var1_vec(<2 x i32>, <2 x i32>) { +; CHECK-LABEL: @f_var1_vec( +; CHECK-NEXT: [[TMP3:%.*]] = and <2 x i32> [[TMP0:%.*]], +; CHECK-NEXT: ret <2 x i32> [[TMP3]] +; + %3 = and <2 x i32> %0, %1 + %4 = icmp eq <2 x i32> %3, + %5 = and <2 x i32> %0, + %6 = select <2 x i1> %4, <2 x i32> %5, <2 x i32> + ret <2 x i32> %5 +} + +define <3 x i32> @f_var1_vec_undef(<3 x i32>, <3 x i32>) { +; CHECK-LABEL: @f_var1_vec_undef( +; CHECK-NEXT: [[TMP3:%.*]] = and <3 x i32> [[TMP0:%.*]], +; CHECK-NEXT: ret <3 x i32> [[TMP3]] +; + %3 = and <3 x i32> %0, %1 + %4 = icmp eq <3 x i32> %3, + %5 = and <3 x i32> %0, + %6 = select <3 x i1> %4, <3 x i32> %5, <3 x i32> + ret <3 x i32> %5 +} + +; ============================================================================ ; +; Negative tests. Should not be folded. +; ============================================================================ ; + +; False-value is not 1 + +define i32 @n0(i32) { +; CHECK-LABEL: @n0( +; CHECK-NEXT: [[TMP2:%.*]] = and i32 [[TMP0:%.*]], 1 +; CHECK-NEXT: [[TMP3:%.*]] = icmp eq i32 [[TMP2]], 0 +; CHECK-NEXT: [[TMP4:%.*]] = lshr i32 [[TMP0]], 2 +; CHECK-NEXT: [[TMP5:%.*]] = and i32 [[TMP4]], 1 +; CHECK-NEXT: [[TMP6:%.*]] = select i1 [[TMP3]], i32 [[TMP5]], i32 0 +; CHECK-NEXT: ret i32 [[TMP6]] +; + %2 = and i32 %0, 1 + %3 = icmp eq i32 %2, 0 + %4 = lshr i32 %0, 2 + %5 = and i32 %4, 1 + %6 = select i1 %3, i32 %5, i32 0 + ret i32 %6 +} + +define i32 @n1(i32) { +; CHECK-LABEL: @n1( +; CHECK-NEXT: [[TMP2:%.*]] = and i32 [[TMP0:%.*]], 2 +; CHECK-NEXT: [[TMP3:%.*]] = icmp eq i32 [[TMP2]], 0 +; CHECK-NEXT: [[TMP4:%.*]] = and i32 [[TMP0]], 1 +; CHECK-NEXT: [[TMP5:%.*]] = select i1 [[TMP3]], i32 [[TMP4]], i32 0 +; CHECK-NEXT: ret i32 [[TMP5]] +; + %2 = and i32 %0, 2 + %3 = icmp eq i32 %2, 0 + %4 = and i32 %0, 1 + %5 = select i1 %3, i32 %4, i32 0 + ret i32 %5 +} + +; Mask of second and is not one + +define i32 @n2(i32) { +; CHECK-LABEL: @n2( +; CHECK-NEXT: [[TMP2:%.*]] = and i32 [[TMP0:%.*]], 1 +; CHECK-NEXT: [[TMP3:%.*]] = icmp eq i32 [[TMP2]], 0 +; CHECK-NEXT: [[TMP4:%.*]] = lshr i32 [[TMP0]], 2 +; CHECK-NEXT: [[TMP5:%.*]] = and i32 [[TMP4]], 2 +; CHECK-NEXT: [[TMP6:%.*]] = select i1 [[TMP3]], i32 [[TMP5]], i32 1 +; CHECK-NEXT: ret i32 [[TMP6]] +; + %2 = and i32 %0, 1 + %3 = icmp eq i32 %2, 0 + %4 = lshr i32 %0, 2 + %5 = and i32 %4, 2 + %6 = select i1 %3, i32 %5, i32 1 + ret i32 %6 +} + +define i32 @n3(i32) { +; CHECK-LABEL: @n3( +; CHECK-NEXT: [[TMP2:%.*]] = and i32 [[TMP0:%.*]], 2 +; CHECK-NEXT: [[TMP3:%.*]] = icmp eq i32 [[TMP2]], 0 +; CHECK-NEXT: [[TMP4:%.*]] = and i32 [[TMP0]], 2 +; CHECK-NEXT: [[TMP5:%.*]] = select i1 [[TMP3]], i32 [[TMP4]], i32 1 +; CHECK-NEXT: ret i32 [[TMP5]] +; + %2 = and i32 %0, 2 + %3 = icmp eq i32 %2, 0 + %4 = and i32 %0, 2 + %5 = select i1 %3, i32 %4, i32 1 + ret i32 %5 +} + +; Wrong icmp pred + +define i32 @n4(i32) { +; CHECK-LABEL: @n4( +; CHECK-NEXT: [[TMP2:%.*]] = and i32 [[TMP0:%.*]], 1 +; CHECK-NEXT: [[TMP3:%.*]] = icmp eq i32 [[TMP2]], 0 +; CHECK-NEXT: [[TMP4:%.*]] = lshr i32 [[TMP0]], 2 +; CHECK-NEXT: [[TMP5:%.*]] = and i32 [[TMP4]], 1 +; CHECK-NEXT: [[TMP6:%.*]] = select i1 [[TMP3]], i32 1, i32 [[TMP5]] +; CHECK-NEXT: ret i32 [[TMP6]] +; + %2 = and i32 %0, 1 + %3 = icmp ne i32 %2, 0 + %4 = lshr i32 %0, 2 + %5 = and i32 %4, 1 + %6 = select i1 %3, i32 %5, i32 1 + ret i32 %6 +} + +define i32 @n5(i32) { +; CHECK-LABEL: @n5( +; CHECK-NEXT: [[TMP2:%.*]] = and i32 [[TMP0:%.*]], 2 +; CHECK-NEXT: [[TMP3:%.*]] = icmp eq i32 [[TMP2]], 0 +; CHECK-NEXT: [[TMP4:%.*]] = and i32 [[TMP0]], 1 +; CHECK-NEXT: [[TMP5:%.*]] = select i1 [[TMP3]], i32 1, i32 [[TMP4]] +; CHECK-NEXT: ret i32 [[TMP5]] +; + %2 = and i32 %0, 2 + %3 = icmp ne i32 %2, 0 + %4 = and i32 %0, 1 + %5 = select i1 %3, i32 %4, i32 1 + ret i32 %5 +} + +; Wrong icmp second operand + +define i32 @n6_0(i32) { +; CHECK-LABEL: @n6_0( +; CHECK-NEXT: [[TMP2:%.*]] = and i32 [[TMP0:%.*]], 1 +; CHECK-NEXT: [[TMP3:%.*]] = icmp eq i32 [[TMP2]], 0 +; CHECK-NEXT: [[TMP4:%.*]] = lshr i32 [[TMP0]], 2 +; CHECK-NEXT: [[TMP5:%.*]] = and i32 [[TMP4]], 1 +; CHECK-NEXT: [[TMP6:%.*]] = select i1 [[TMP3]], i32 1, i32 [[TMP5]] +; CHECK-NEXT: ret i32 [[TMP6]] +; + %2 = and i32 %0, 1 + %3 = icmp eq i32 %2, 1 + %4 = lshr i32 %0, 2 + %5 = and i32 %4, 1 + %6 = select i1 %3, i32 %5, i32 1 + ret i32 %6 +} + +define i32 @n6_1(i32) { +; CHECK-LABEL: @n6_1( +; CHECK-NEXT: ret i32 1 +; + %2 = and i32 %0, 1 + %3 = icmp eq i32 %2, 2 + %4 = lshr i32 %0, 2 + %5 = and i32 %4, 1 + %6 = select i1 %3, i32 %5, i32 1 + ret i32 %6 +} + +define i32 @n7_0(i32) { +; CHECK-LABEL: @n7_0( +; CHECK-NEXT: ret i32 1 +; + %2 = and i32 %0, 2 + %3 = icmp eq i32 %2, 1 + %4 = and i32 %0, 1 + %5 = select i1 %3, i32 %4, i32 1 + ret i32 %5 +} + +define i32 @n7_1(i32) { +; CHECK-LABEL: @n7_1( +; CHECK-NEXT: [[TMP2:%.*]] = and i32 [[TMP0:%.*]], 2 +; CHECK-NEXT: [[TMP3:%.*]] = icmp eq i32 [[TMP2]], 0 +; CHECK-NEXT: [[TMP4:%.*]] = and i32 [[TMP0]], 1 +; CHECK-NEXT: [[TMP5:%.*]] = select i1 [[TMP3]], i32 1, i32 [[TMP4]] +; CHECK-NEXT: ret i32 [[TMP5]] +; + %2 = and i32 %0, 2 + %3 = icmp eq i32 %2, 2 + %4 = and i32 %0, 1 + %5 = select i1 %3, i32 %4, i32 1 + ret i32 %5 +} + +; Bad shift + +define i32 @n8(i32) { +; CHECK-LABEL: @n8( +; CHECK-NEXT: [[TMP2:%.*]] = and i32 [[TMP0:%.*]], 1 +; CHECK-NEXT: ret i32 [[TMP2]] +; + %2 = and i32 %0, 1 + %3 = icmp eq i32 %2, 0 + %4 = lshr i32 %0, 32 + %5 = and i32 %4, 1 + %6 = select i1 %3, i32 %5, i32 1 + ret i32 %6 +}