Index: lib/Transforms/InstCombine/InstCombineSelect.cpp =================================================================== --- lib/Transforms/InstCombine/InstCombineSelect.cpp +++ lib/Transforms/InstCombine/InstCombineSelect.cpp @@ -396,6 +396,53 @@ return nullptr; } +/// We want to turn: +/// (select (icmp eq (and X, Y), 0), (and (lshr X, C), 1), 1) +/// into: +/// zext (icmp ne i32 (and X, (or Y, (shl 1, C))), 0) +/// Note: +/// C may be 0 if lshr is missing. +/// FIXME: do we want to allow C to be a variable too? +/// Worst case scenario is that we will replace 5 instructions with 5 different +/// instructions, but we got rid of select. +/// https://rise4fun.com/Alive/uiH +static Instruction *foldSelectICmpAndAnd(Type *SelType, const ICmpInst *IC, + Value *TrueVal, Value *FalseVal, + InstCombiner::BuilderTy &Builder) { + if (!(IC->hasOneUse() && IC->getOperand(0)->hasOneUse())) + return nullptr; + + Value *X, *Y; + ICmpInst::Predicate EqPred; + if (!(match(IC, m_ICmp(EqPred, m_And(m_Value(X), m_Value(Y)), m_Zero())) && + ICmpInst::Predicate::ICMP_EQ == EqPred && match(FalseVal, m_One()))) + return nullptr; + + // The TrueVal has general form of: + // and %B, 1 + Value *B; + if (!match(TrueVal, m_OneUse(m_And(m_Value(B), m_One())))) + return nullptr; + + // Where %B can be one of: + // %X + // or + // lshr %X, C + Value *MaskB, *C; + if (match(B, m_Specific(X))) { + MaskB = ConstantInt::get(SelType, 1); + } else if (match(B, m_OneUse(m_LShr(m_Specific(X), m_Value(C)))) && + match(C, m_NonNegative())) { + MaskB = Builder.CreateShl(ConstantInt::get(SelType, 1), C); + } else + return nullptr; + + Value *FullMask = Builder.CreateOr(Y, MaskB); + Value *MaskedX = Builder.CreateAnd(X, FullMask); + Value *ICmpNeZero = Builder.CreateIsNotNull(MaskedX); + return new ZExtInst(ICmpNeZero, SelType); +} + /// We want to turn: /// (select (icmp eq (and X, C1), 0), Y, (or Y, C2)) /// into: @@ -863,6 +910,10 @@ } } + if (Instruction *V = + foldSelectICmpAndAnd(SI.getType(), ICI, TrueVal, FalseVal, Builder)) + return V; + if (Value *V = foldSelectICmpAndOr(ICI, TrueVal, FalseVal, Builder)) return replaceInstUsesWith(SI, V); Index: test/Transforms/InstCombine/select-of-bittest.ll =================================================================== --- test/Transforms/InstCombine/select-of-bittest.ll +++ test/Transforms/InstCombine/select-of-bittest.ll @@ -8,12 +8,10 @@ define i32 @and_lshr_and(i32) { ; CHECK-LABEL: @and_lshr_and( -; 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]] +; CHECK-NEXT: [[TMP2:%.*]] = and i32 [[TMP0:%.*]], 3 +; CHECK-NEXT: [[TMP3:%.*]] = icmp ne i32 [[TMP2]], 0 +; CHECK-NEXT: [[TMP4:%.*]] = zext i1 [[TMP3]] to i32 +; CHECK-NEXT: ret i32 [[TMP4]] ; %2 = and i32 %0, 1 %3 = icmp eq i32 %2, 0 @@ -25,12 +23,10 @@ define <2 x i32> @and_lshr_and_splatvec(<2 x i32>) { ; CHECK-LABEL: @and_lshr_and_splatvec( -; 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]] +; CHECK-NEXT: [[TMP2:%.*]] = and <2 x i32> [[TMP0:%.*]], +; CHECK-NEXT: [[TMP3:%.*]] = icmp ne <2 x i32> [[TMP2]], zeroinitializer +; CHECK-NEXT: [[TMP4:%.*]] = zext <2 x i1> [[TMP3]] to <2 x i32> +; CHECK-NEXT: ret <2 x i32> [[TMP4]] ; %2 = and <2 x i32> %0, %3 = icmp eq <2 x i32> %2, @@ -42,12 +38,10 @@ define <2 x i32> @and_lshr_and_vec_v0(<2 x i32>) { ; CHECK-LABEL: @and_lshr_and_vec_v0( -; 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]] +; CHECK-NEXT: [[TMP2:%.*]] = and <2 x i32> [[TMP0:%.*]], +; CHECK-NEXT: [[TMP3:%.*]] = icmp ne <2 x i32> [[TMP2]], zeroinitializer +; CHECK-NEXT: [[TMP4:%.*]] = zext <2 x i1> [[TMP3]] to <2 x i32> +; CHECK-NEXT: ret <2 x i32> [[TMP4]] ; %2 = and <2 x i32> %0, ; mask is not splat %3 = icmp eq <2 x i32> %2, @@ -59,12 +53,10 @@ define <2 x i32> @and_lshr_and_vec_v1(<2 x i32>) { ; CHECK-LABEL: @and_lshr_and_vec_v1( -; 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]] +; CHECK-NEXT: [[TMP2:%.*]] = and <2 x i32> [[TMP0:%.*]], +; CHECK-NEXT: [[TMP3:%.*]] = icmp ne <2 x i32> [[TMP2]], zeroinitializer +; CHECK-NEXT: [[TMP4:%.*]] = zext <2 x i1> [[TMP3]] to <2 x i32> +; CHECK-NEXT: ret <2 x i32> [[TMP4]] ; %2 = and <2 x i32> %0, %3 = icmp eq <2 x i32> %2, @@ -76,12 +68,10 @@ define <2 x i32> @and_lshr_and_vec_v2(<2 x i32>) { ; CHECK-LABEL: @and_lshr_and_vec_v2( -; 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]] +; CHECK-NEXT: [[TMP2:%.*]] = and <2 x i32> [[TMP0:%.*]], +; CHECK-NEXT: [[TMP3:%.*]] = icmp ne <2 x i32> [[TMP2]], zeroinitializer +; CHECK-NEXT: [[TMP4:%.*]] = zext <2 x i1> [[TMP3]] to <2 x i32> +; CHECK-NEXT: ret <2 x i32> [[TMP4]] ; %2 = and <2 x i32> %0, ; mask is not splat %3 = icmp eq <2 x i32> %2, @@ -110,11 +100,10 @@ define i32 @and_and(i32) { ; CHECK-LABEL: @and_and( -; 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]] +; CHECK-NEXT: [[TMP2:%.*]] = and i32 [[TMP0:%.*]], 3 +; CHECK-NEXT: [[TMP3:%.*]] = icmp ne i32 [[TMP2]], 0 +; CHECK-NEXT: [[TMP4:%.*]] = zext i1 [[TMP3]] to i32 +; CHECK-NEXT: ret i32 [[TMP4]] ; %2 = and i32 %0, 2 %3 = icmp eq i32 %2, 0 @@ -125,11 +114,10 @@ define <2 x i32> @and_and_splatvec(<2 x i32>) { ; CHECK-LABEL: @and_and_splatvec( -; 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]] +; CHECK-NEXT: [[TMP2:%.*]] = and <2 x i32> [[TMP0:%.*]], +; CHECK-NEXT: [[TMP3:%.*]] = icmp ne <2 x i32> [[TMP2]], zeroinitializer +; CHECK-NEXT: [[TMP4:%.*]] = zext <2 x i1> [[TMP3]] to <2 x i32> +; CHECK-NEXT: ret <2 x i32> [[TMP4]] ; %2 = and <2 x i32> %0, %3 = icmp eq <2 x i32> %2, @@ -140,11 +128,10 @@ define <2 x i32> @and_and_vec(<2 x i32>) { ; CHECK-LABEL: @and_and_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]] +; CHECK-NEXT: [[TMP2:%.*]] = and <2 x i32> [[TMP0:%.*]], +; CHECK-NEXT: [[TMP3:%.*]] = icmp ne <2 x i32> [[TMP2]], zeroinitializer +; CHECK-NEXT: [[TMP4:%.*]] = zext <2 x i1> [[TMP3]] to <2 x i32> +; CHECK-NEXT: ret <2 x i32> [[TMP4]] ; %2 = and <2 x i32> %0, ; mask is not splat %3 = icmp eq <2 x i32> %2, @@ -174,12 +161,11 @@ define i32 @f_var0(i32, i32) { ; CHECK-LABEL: @f_var0( -; CHECK-NEXT: [[TMP3:%.*]] = and i32 [[TMP0:%.*]], [[TMP1:%.*]] -; CHECK-NEXT: [[TMP4:%.*]] = icmp eq i32 [[TMP3]], 0 -; CHECK-NEXT: [[TMP5:%.*]] = lshr i32 [[TMP0]], 1 -; CHECK-NEXT: [[TMP6:%.*]] = and i32 [[TMP5]], 1 -; CHECK-NEXT: [[TMP7:%.*]] = select i1 [[TMP4]], i32 [[TMP6]], i32 1 -; CHECK-NEXT: ret i32 [[TMP7]] +; CHECK-NEXT: [[TMP3:%.*]] = or i32 [[TMP1:%.*]], 2 +; CHECK-NEXT: [[TMP4:%.*]] = and i32 [[TMP3]], [[TMP0:%.*]] +; CHECK-NEXT: [[TMP5:%.*]] = icmp ne i32 [[TMP4]], 0 +; CHECK-NEXT: [[TMP6:%.*]] = zext i1 [[TMP5]] to i32 +; CHECK-NEXT: ret i32 [[TMP6]] ; %3 = and i32 %0, %1 %4 = icmp eq i32 %3, 0 @@ -191,12 +177,11 @@ define <2 x i32> @f_var0_splatvec(<2 x i32>, <2 x i32>) { ; CHECK-LABEL: @f_var0_splatvec( -; CHECK-NEXT: [[TMP3:%.*]] = and <2 x i32> [[TMP0:%.*]], [[TMP1:%.*]] -; CHECK-NEXT: [[TMP4:%.*]] = icmp eq <2 x i32> [[TMP3]], zeroinitializer -; CHECK-NEXT: [[TMP5:%.*]] = lshr <2 x i32> [[TMP0]], -; CHECK-NEXT: [[TMP6:%.*]] = and <2 x i32> [[TMP5]], -; CHECK-NEXT: [[TMP7:%.*]] = select <2 x i1> [[TMP4]], <2 x i32> [[TMP6]], <2 x i32> -; CHECK-NEXT: ret <2 x i32> [[TMP7]] +; CHECK-NEXT: [[TMP3:%.*]] = or <2 x i32> [[TMP1:%.*]], +; CHECK-NEXT: [[TMP4:%.*]] = and <2 x i32> [[TMP3]], [[TMP0:%.*]] +; CHECK-NEXT: [[TMP5:%.*]] = icmp ne <2 x i32> [[TMP4]], zeroinitializer +; CHECK-NEXT: [[TMP6:%.*]] = zext <2 x i1> [[TMP5]] to <2 x i32> +; CHECK-NEXT: ret <2 x i32> [[TMP6]] ; %3 = and <2 x i32> %0, %1 %4 = icmp eq <2 x i32> %3, @@ -208,12 +193,11 @@ define <2 x i32> @f_var0_vec(<2 x i32>, <2 x i32>) { ; CHECK-LABEL: @f_var0_vec( -; CHECK-NEXT: [[TMP3:%.*]] = and <2 x i32> [[TMP0:%.*]], [[TMP1:%.*]] -; CHECK-NEXT: [[TMP4:%.*]] = icmp eq <2 x i32> [[TMP3]], zeroinitializer -; CHECK-NEXT: [[TMP5:%.*]] = lshr <2 x i32> [[TMP0]], -; CHECK-NEXT: [[TMP6:%.*]] = and <2 x i32> [[TMP5]], -; CHECK-NEXT: [[TMP7:%.*]] = select <2 x i1> [[TMP4]], <2 x i32> [[TMP6]], <2 x i32> -; CHECK-NEXT: ret <2 x i32> [[TMP7]] +; CHECK-NEXT: [[TMP3:%.*]] = or <2 x i32> [[TMP1:%.*]], +; CHECK-NEXT: [[TMP4:%.*]] = and <2 x i32> [[TMP3]], [[TMP0:%.*]] +; CHECK-NEXT: [[TMP5:%.*]] = icmp ne <2 x i32> [[TMP4]], zeroinitializer +; CHECK-NEXT: [[TMP6:%.*]] = zext <2 x i1> [[TMP5]] to <2 x i32> +; CHECK-NEXT: ret <2 x i32> [[TMP6]] ; %3 = and <2 x i32> %0, %1 %4 = icmp eq <2 x i32> %3, @@ -242,10 +226,10 @@ define i32 @f_var1(i32, i32) { ; CHECK-LABEL: @f_var1( -; CHECK-NEXT: [[TMP3:%.*]] = and i32 [[TMP0:%.*]], [[TMP1:%.*]] -; CHECK-NEXT: [[TMP4:%.*]] = icmp eq i32 [[TMP3]], 0 -; CHECK-NEXT: [[TMP5:%.*]] = and i32 [[TMP0]], 1 -; CHECK-NEXT: [[TMP6:%.*]] = select i1 [[TMP4]], i32 [[TMP5]], i32 1 +; CHECK-NEXT: [[TMP3:%.*]] = or i32 [[TMP1:%.*]], 1 +; CHECK-NEXT: [[TMP4:%.*]] = and i32 [[TMP3]], [[TMP0:%.*]] +; CHECK-NEXT: [[TMP5:%.*]] = icmp ne i32 [[TMP4]], 0 +; CHECK-NEXT: [[TMP6:%.*]] = zext i1 [[TMP5]] to i32 ; CHECK-NEXT: ret i32 [[TMP6]] ; %3 = and i32 %0, %1 @@ -257,10 +241,10 @@ 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:%.*]], [[TMP1:%.*]] -; CHECK-NEXT: [[TMP4:%.*]] = icmp eq <2 x i32> [[TMP3]], zeroinitializer -; CHECK-NEXT: [[TMP5:%.*]] = and <2 x i32> [[TMP0]], -; CHECK-NEXT: [[TMP6:%.*]] = select <2 x i1> [[TMP4]], <2 x i32> [[TMP5]], <2 x i32> +; CHECK-NEXT: [[TMP3:%.*]] = or <2 x i32> [[TMP1:%.*]], +; CHECK-NEXT: [[TMP4:%.*]] = and <2 x i32> [[TMP3]], [[TMP0:%.*]] +; CHECK-NEXT: [[TMP5:%.*]] = icmp ne <2 x i32> [[TMP4]], zeroinitializer +; CHECK-NEXT: [[TMP6:%.*]] = zext <2 x i1> [[TMP5]] to <2 x i32> ; CHECK-NEXT: ret <2 x i32> [[TMP6]] ; %3 = and <2 x i32> %0, %1 @@ -289,6 +273,57 @@ ; Negative tests. Should not be folded. ; ============================================================================ ; +; One use only. + +declare void @use32(i32) +declare void @use1(i1) + +define i32 @n_var0_oneuse(i32, i32) { +; CHECK-LABEL: @n_var0_oneuse( +; CHECK-NEXT: [[TMP3:%.*]] = and i32 [[TMP0:%.*]], [[TMP1:%.*]] +; CHECK-NEXT: [[TMP4:%.*]] = icmp eq i32 [[TMP3]], 0 +; CHECK-NEXT: [[TMP5:%.*]] = lshr i32 [[TMP0]], 1 +; CHECK-NEXT: [[TMP6:%.*]] = and i32 [[TMP5]], 1 +; CHECK-NEXT: [[TMP7:%.*]] = select i1 [[TMP4]], i32 [[TMP6]], i32 1 +; CHECK-NEXT: call void @use32(i32 [[TMP3]]) +; CHECK-NEXT: call void @use1(i1 [[TMP4]]) +; CHECK-NEXT: call void @use32(i32 [[TMP5]]) +; CHECK-NEXT: call void @use32(i32 [[TMP6]]) +; CHECK-NEXT: ret i32 [[TMP7]] +; + %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 + call void @use32(i32 %3) + call void @use1(i1 %4) + call void @use32(i32 %5) + call void @use32(i32 %6) + ret i32 %7 +} + +define i32 @n_var1_oneuse(i32, i32) { +; CHECK-LABEL: @n_var1_oneuse( +; CHECK-NEXT: [[TMP3:%.*]] = and i32 [[TMP0:%.*]], [[TMP1:%.*]] +; CHECK-NEXT: [[TMP4:%.*]] = icmp eq i32 [[TMP3]], 0 +; CHECK-NEXT: [[TMP5:%.*]] = and i32 [[TMP0]], 1 +; CHECK-NEXT: [[TMP6:%.*]] = select i1 [[TMP4]], i32 [[TMP5]], i32 1 +; CHECK-NEXT: call void @use32(i32 [[TMP3]]) +; CHECK-NEXT: call void @use1(i1 [[TMP4]]) +; CHECK-NEXT: call void @use32(i32 [[TMP5]]) +; CHECK-NEXT: ret i32 [[TMP6]] +; + %3 = and i32 %0, %1 + %4 = icmp eq i32 %3, 0 + %5 = and i32 %0, 1 + %6 = select i1 %4, i32 %5, i32 1 + call void @use32(i32 %3) + call void @use1(i1 %4) + call void @use32(i32 %5) + ret i32 %6 +} + ; Different variables are used define i32 @n0(i32, i32) {