Index: lib/Analysis/InstructionSimplify.cpp =================================================================== --- lib/Analysis/InstructionSimplify.cpp +++ lib/Analysis/InstructionSimplify.cpp @@ -65,13 +65,6 @@ static Value *SimplifyGEPInst(Type *, ArrayRef, const SimplifyQuery &, unsigned); -/// Fold -/// %A = icmp ne/eq i8 %X, %V1 -/// %B = icmp ne/eq i8 %X, %V2 -/// %C = or/and i1 %A, %B -/// %D = select i1 %C, i8 %X, i8 %V1 -/// To -/// %X/%V1 static Value *foldSelectWithBinaryOp(Value *Cond, Value *TrueVal, Value *FalseVal) { BinaryOperator::BinaryOps BinOpCode; @@ -89,14 +82,34 @@ return nullptr; CmpInst::Predicate Pred1, Pred2; - if (!match( + // Fold + // %A = icmp ne/eq i8 %X, %V1 + // %B = icmp ne/eq i8 %X, %V2 + // %C = or/and i1 %A, %B + // %D = select i1 %C, i8 %X, i8 %V1 + // To + // %X/%V1 + if (match(Cond, m_c_BinOp(m_c_ICmp(Pred1, m_Specific(TrueVal), + m_Specific(FalseVal)), + m_c_ICmp(Pred2, m_Specific(TrueVal), m_Value()))) && + Pred1 == Pred2 && Pred1 == ExpectedPred) + return BinOpCode == BinaryOperator::Or ? TrueVal : FalseVal; + + // Fold + // %A = icmp ne/eq i8 %X, %Z + // %B = icmp ne/eq i8 %Y, %Z + // %C = or/and i1 %A, %B + // %D = select i1 %C, i8 %X, i8 %Z + // To + // %X + if (match( Cond, m_c_BinOp(m_c_ICmp(Pred1, m_Specific(TrueVal), m_Specific(FalseVal)), - m_c_ICmp(Pred2, m_Specific(TrueVal), m_Value()))) || - Pred1 != Pred2 || Pred1 != ExpectedPred) - return nullptr; - - return BinOpCode == BinaryOperator::Or ? TrueVal : FalseVal; + m_c_ICmp(Pred2, m_Value(), m_Specific(FalseVal)))) && + Pred1 == Pred2 && Pred1 == ExpectedPred) { + return BinOpCode == BinaryOperator::Or ? TrueVal : FalseVal; + } + return nullptr; } /// For a boolean type or a vector of boolean type, return false or a vector @@ -5119,4 +5132,4 @@ } template const SimplifyQuery getBestSimplifyQuery(AnalysisManager &, Function &); -} +} Index: test/Transforms/InstSimplify/select-and-cmp.ll =================================================================== --- test/Transforms/InstSimplify/select-and-cmp.ll +++ test/Transforms/InstSimplify/select-and-cmp.ll @@ -36,6 +36,50 @@ ret i32 %D } +define i32 @select_and_icmp_alt(i32 %x, i32 %y, i32 %z) { +; CHECK-LABEL: @select_and_icmp_alt( +; CHECK-NEXT: ret i32 [[Z:%.*]] +; + %A = icmp eq i32 %x, %z + %B = icmp eq i32 %y, %z + %C = and i1 %A, %B + %D = select i1 %C, i32 %x, i32 %z + ret i32 %D +} + +define i32 @select_and_icmp_alt2(i32 %x, i32 %y, i32 %z) { +; CHECK-LABEL: @select_and_icmp_alt2( +; CHECK-NEXT: ret i32 [[Z:%.*]] +; + %A = icmp eq i32 %x, %z + %B = icmp eq i32 %y, %z + %C = and i1 %A, %B + %D = select i1 %C, i32 %y, i32 %z + ret i32 %D +} + +define i32 @select_and_icmp_inv_alt(i32 %x, i32 %y, i32 %z) { +; CHECK-LABEL: @select_and_icmp_inv_alt( +; CHECK-NEXT: ret i32 [[Z:%.*]] +; + %A = icmp eq i32 %z, %x + %B = icmp eq i32 %z, %y + %C = and i1 %A, %B + %D = select i1 %C, i32 %x, i32 %z + ret i32 %D +} + +define i32 @select_and_inv_icmp_alt(i32 %x, i32 %y, i32 %z) { +; CHECK-LABEL: @select_and_inv_icmp_alt( +; CHECK-NEXT: ret i32 [[Z:%.*]] +; + %A = icmp eq i32 %x, %z + %B = icmp eq i32 %y, %z + %C = and i1 %B, %A + %D = select i1 %C, i32 %x, i32 %z + ret i32 %D +} + define i32 @select_and_inv_icmp(i32 %x, i32 %y, i32 %z) { ; CHECK-LABEL: @select_and_inv_icmp( ; CHECK-NEXT: ret i32 [[X:%.*]] @@ -47,6 +91,18 @@ ret i32 %D } +define <2 x i8> @select_and_icmp_alt_vec(<2 x i8> %x, <2 x i8> %y, <2 x i8> %z) { +; CHECK-LABEL: @select_and_icmp_alt_vec( +; CHECK-NEXT: ret <2 x i8> [[Z:%.*]] +; + %A = icmp eq <2 x i8> %x, %z + %B = icmp eq <2 x i8> %y, %z + %C = and <2 x i1> %A, %B + %D = select <2 x i1> %C, <2 x i8> %x, <2 x i8> %z + ret <2 x i8> %D +} + + define i32 @select_and_icmp_inv(i32 %x, i32 %y, i32 %z) { ; CHECK-LABEL: @select_and_icmp_inv( ; CHECK-NEXT: ret i32 [[X:%.*]] @@ -178,3 +234,108 @@ %D = select i1 %C, i32 %z, i32 %x ret i32 %D } + +define i32 @select_and_icmp_alt_bad_1(i32 %x, i32 %y, i32 %z) { +; CHECK-LABEL: @select_and_icmp_alt_bad_1( +; CHECK-NEXT: [[A:%.*]] = icmp eq i32 [[X:%.*]], [[Z:%.*]] +; CHECK-NEXT: [[B:%.*]] = icmp ne i32 [[Y:%.*]], [[Z]] +; CHECK-NEXT: [[C:%.*]] = and i1 [[A]], [[B]] +; CHECK-NEXT: [[D:%.*]] = select i1 [[C]], i32 [[X]], i32 [[Z]] +; CHECK-NEXT: ret i32 [[D]] +; + %A = icmp eq i32 %x, %z + %B = icmp ne i32 %y, %z + %C = and i1 %A, %B + %D = select i1 %C, i32 %x, i32 %z + ret i32 %D +} + +define i32 @select_and_icmp_alt_bad_2(i32 %x, i32 %y, i32 %z) { +; CHECK-LABEL: @select_and_icmp_alt_bad_2( +; CHECK-NEXT: [[A:%.*]] = icmp ne i32 [[X:%.*]], [[Z:%.*]] +; CHECK-NEXT: [[B:%.*]] = icmp eq i32 [[Y:%.*]], [[Z]] +; CHECK-NEXT: [[C:%.*]] = and i1 [[A]], [[B]] +; CHECK-NEXT: [[D:%.*]] = select i1 [[C]], i32 [[X]], i32 [[Z]] +; CHECK-NEXT: ret i32 [[D]] +; + %A = icmp ne i32 %x, %z + %B = icmp eq i32 %y, %z + %C = and i1 %A, %B + %D = select i1 %C, i32 %x, i32 %z + ret i32 %D +} + +define i32 @select_and_icmp_alt_bad_3(i32 %x, i32 %y, i32 %z) { +; CHECK-LABEL: @select_and_icmp_alt_bad_3( +; CHECK-NEXT: [[A:%.*]] = icmp ne i32 [[X:%.*]], [[Z:%.*]] +; CHECK-NEXT: [[B:%.*]] = icmp ne i32 [[Y:%.*]], [[Z]] +; CHECK-NEXT: [[C:%.*]] = and i1 [[A]], [[B]] +; CHECK-NEXT: [[D:%.*]] = select i1 [[C]], i32 [[X]], i32 [[Z]] +; CHECK-NEXT: ret i32 [[D]] +; + %A = icmp ne i32 %x, %z + %B = icmp ne i32 %y, %z + %C = and i1 %A, %B + %D = select i1 %C, i32 %x, i32 %z + ret i32 %D +} + +define i32 @select_and_icmp_alt_bad_4(i32 %x, i32 %y, i32 %z) { +; CHECK-LABEL: @select_and_icmp_alt_bad_4( +; CHECK-NEXT: [[A:%.*]] = icmp eq i32 [[X:%.*]], [[Z:%.*]] +; CHECK-NEXT: [[B:%.*]] = icmp eq i32 [[Y:%.*]], [[Z]] +; CHECK-NEXT: [[C:%.*]] = or i1 [[A]], [[B]] +; CHECK-NEXT: [[D:%.*]] = select i1 [[C]], i32 [[X]], i32 [[Z]] +; CHECK-NEXT: ret i32 [[D]] +; + %A = icmp eq i32 %x, %z + %B = icmp eq i32 %y, %z + %C = or i1 %A, %B + %D = select i1 %C, i32 %x, i32 %z + ret i32 %D +} + +define i32 @select_and_icmp_alt_bad_5(i32 %x, i32 %y, i32 %z, i32 %k) { +; CHECK-LABEL: @select_and_icmp_alt_bad_5( +; CHECK-NEXT: [[A:%.*]] = icmp eq i32 [[X:%.*]], [[K:%.*]] +; CHECK-NEXT: [[B:%.*]] = icmp eq i32 [[Y:%.*]], [[Z:%.*]] +; CHECK-NEXT: [[C:%.*]] = or i1 [[A]], [[B]] +; CHECK-NEXT: [[D:%.*]] = select i1 [[C]], i32 [[X]], i32 [[Z]] +; CHECK-NEXT: ret i32 [[D]] +; + %A = icmp eq i32 %x, %k + %B = icmp eq i32 %y, %z + %C = or i1 %A, %B + %D = select i1 %C, i32 %x, i32 %z + ret i32 %D +} + +define i32 @select_and_icmp_alt_bad_true_val(i32 %x, i32 %y, i32 %z, i32 %k) { +; CHECK-LABEL: @select_and_icmp_alt_bad_true_val( +; CHECK-NEXT: [[A:%.*]] = icmp eq i32 [[X:%.*]], [[Z:%.*]] +; CHECK-NEXT: [[B:%.*]] = icmp eq i32 [[Y:%.*]], [[Z]] +; CHECK-NEXT: [[C:%.*]] = and i1 [[A]], [[B]] +; CHECK-NEXT: [[D:%.*]] = select i1 [[C]], i32 [[K:%.*]], i32 [[Z]] +; CHECK-NEXT: ret i32 [[D]] +; + %A = icmp eq i32 %x, %z + %B = icmp eq i32 %y, %z + %C = and i1 %A, %B + %D = select i1 %C, i32 %k, i32 %z + ret i32 %D +} + +define i32 @select_and_icmp_alt_bad_false_val(i32 %x, i32 %y, i32 %z, i32 %k) { +; CHECK-LABEL: @select_and_icmp_alt_bad_false_val( +; CHECK-NEXT: [[A:%.*]] = icmp eq i32 [[X:%.*]], [[Z:%.*]] +; CHECK-NEXT: [[B:%.*]] = icmp eq i32 [[Y:%.*]], [[Z]] +; CHECK-NEXT: [[C:%.*]] = and i1 [[A]], [[B]] +; CHECK-NEXT: [[D:%.*]] = select i1 [[C]], i32 [[X]], i32 [[K:%.*]] +; CHECK-NEXT: ret i32 [[D]] +; + %A = icmp eq i32 %x, %z + %B = icmp eq i32 %y, %z + %C = and i1 %A, %B + %D = select i1 %C, i32 %x, i32 %k + ret i32 %D +} Index: test/Transforms/InstSimplify/select-or-cmp.ll =================================================================== --- test/Transforms/InstSimplify/select-or-cmp.ll +++ test/Transforms/InstSimplify/select-or-cmp.ll @@ -36,6 +36,61 @@ ret i32 %D } +define i32 @select_or_icmp_alt(i32 %x, i32 %y, i32 %z) { +; CHECK-LABEL: @select_or_icmp_alt( +; CHECK-NEXT: ret i32 [[X:%.*]] +; + %A = icmp ne i32 %x, %z + %B = icmp ne i32 %y, %z + %C = or i1 %A, %B + %D = select i1 %C, i32 %x, i32 %z + ret i32 %D +} + +define i32 @select_or_icmp_alt2(i32 %x, i32 %y, i32 %z) { +; CHECK-LABEL: @select_or_icmp_alt2( +; CHECK-NEXT: ret i32 [[Y:%.*]] +; + %A = icmp ne i32 %x, %z + %B = icmp ne i32 %y, %z + %C = or i1 %A, %B + %D = select i1 %C, i32 %y, i32 %z + ret i32 %D +} + +define i32 @select_or_icmp_inv_alt(i32 %x, i32 %y, i32 %z) { +; CHECK-LABEL: @select_or_icmp_inv_alt( +; CHECK-NEXT: ret i32 [[X:%.*]] +; + %A = icmp ne i32 %z, %x + %B = icmp ne i32 %z, %y + %C = or i1 %A, %B + %D = select i1 %C, i32 %x, i32 %z + ret i32 %D +} + +define i32 @select_or_inv_icmp_alt(i32 %x, i32 %y, i32 %z) { +; CHECK-LABEL: @select_or_inv_icmp_alt( +; CHECK-NEXT: ret i32 [[X:%.*]] +; + %A = icmp ne i32 %z, %x + %B = icmp ne i32 %z, %y + %C = or i1 %B, %A + %D = select i1 %C, i32 %x, i32 %z + ret i32 %D +} + +define <2 x i8> @select_or_icmp_alt_vec(<2 x i8> %x, <2 x i8> %y, <2 x i8> %z) { +; CHECK-LABEL: @select_or_icmp_alt_vec( +; CHECK-NEXT: ret <2 x i8> [[X:%.*]] +; + %A = icmp ne <2 x i8> %x, %z + %B = icmp ne <2 x i8> %y, %z + %C = or <2 x i1> %A, %B + %D = select <2 x i1> %C, <2 x i8> %x, <2 x i8> %z + ret <2 x i8> %D +} + define i32 @select_or_inv_icmp(i32 %x, i32 %y, i32 %z) { ; CHECK-LABEL: @select_or_inv_icmp( ; CHECK-NEXT: ret i32 [[Z:%.*]] @@ -179,3 +234,108 @@ %D = select i1 %C, i32 %z, i32 %x ret i32 %D } + +define i32 @select_or_icmp_alt_bad_1(i32 %x, i32 %y, i32 %z) { +; CHECK-LABEL: @select_or_icmp_alt_bad_1( +; CHECK-NEXT: [[A:%.*]] = icmp eq i32 [[X:%.*]], [[Z:%.*]] +; CHECK-NEXT: [[B:%.*]] = icmp ne i32 [[Y:%.*]], [[Z]] +; CHECK-NEXT: [[C:%.*]] = or i1 [[A]], [[B]] +; CHECK-NEXT: [[D:%.*]] = select i1 [[C]], i32 [[X]], i32 [[Z]] +; CHECK-NEXT: ret i32 [[D]] +; + %A = icmp eq i32 %x, %z + %B = icmp ne i32 %y, %z + %C = or i1 %A, %B + %D = select i1 %C, i32 %x, i32 %z + ret i32 %D +} + +define i32 @select_or_icmp_alt_bad_2(i32 %x, i32 %y, i32 %z) { +; CHECK-LABEL: @select_or_icmp_alt_bad_2( +; CHECK-NEXT: [[A:%.*]] = icmp ne i32 [[X:%.*]], [[Z:%.*]] +; CHECK-NEXT: [[B:%.*]] = icmp eq i32 [[Y:%.*]], [[Z]] +; CHECK-NEXT: [[C:%.*]] = or i1 [[A]], [[B]] +; CHECK-NEXT: [[D:%.*]] = select i1 [[C]], i32 [[X]], i32 [[Z]] +; CHECK-NEXT: ret i32 [[D]] +; + %A = icmp ne i32 %x, %z + %B = icmp eq i32 %y, %z + %C = or i1 %A, %B + %D = select i1 %C, i32 %x, i32 %z + ret i32 %D +} + +define i32 @select_or_icmp_alt_bad_3(i32 %x, i32 %y, i32 %z) { +; CHECK-LABEL: @select_or_icmp_alt_bad_3( +; CHECK-NEXT: [[A:%.*]] = icmp eq i32 [[X:%.*]], [[Z:%.*]] +; CHECK-NEXT: [[B:%.*]] = icmp eq i32 [[Y:%.*]], [[Z]] +; CHECK-NEXT: [[C:%.*]] = or i1 [[A]], [[B]] +; CHECK-NEXT: [[D:%.*]] = select i1 [[C]], i32 [[X]], i32 [[Z]] +; CHECK-NEXT: ret i32 [[D]] +; + %A = icmp eq i32 %x, %z + %B = icmp eq i32 %y, %z + %C = or i1 %A, %B + %D = select i1 %C, i32 %x, i32 %z + ret i32 %D +} + +define i32 @select_or_icmp_alt_bad_4(i32 %x, i32 %y, i32 %z) { +; CHECK-LABEL: @select_or_icmp_alt_bad_4( +; CHECK-NEXT: [[A:%.*]] = icmp ne i32 [[X:%.*]], [[Z:%.*]] +; CHECK-NEXT: [[B:%.*]] = icmp ne i32 [[Y:%.*]], [[Z]] +; CHECK-NEXT: [[C:%.*]] = and i1 [[A]], [[B]] +; CHECK-NEXT: [[D:%.*]] = select i1 [[C]], i32 [[X]], i32 [[Z]] +; CHECK-NEXT: ret i32 [[D]] +; + %A = icmp ne i32 %x, %z + %B = icmp ne i32 %y, %z + %C = and i1 %A, %B + %D = select i1 %C, i32 %x, i32 %z + ret i32 %D +} + +define i32 @select_or_icmp_alt_bad_5(i32 %x, i32 %y, i32 %z, i32 %k) { +; CHECK-LABEL: @select_or_icmp_alt_bad_5( +; CHECK-NEXT: [[A:%.*]] = icmp ne i32 [[X:%.*]], [[K:%.*]] +; CHECK-NEXT: [[B:%.*]] = icmp ne i32 [[Y:%.*]], [[Z:%.*]] +; CHECK-NEXT: [[C:%.*]] = or i1 [[A]], [[B]] +; CHECK-NEXT: [[D:%.*]] = select i1 [[C]], i32 [[X]], i32 [[Z]] +; CHECK-NEXT: ret i32 [[D]] +; + %A = icmp ne i32 %x, %k + %B = icmp ne i32 %y, %z + %C = or i1 %A, %B + %D = select i1 %C, i32 %x, i32 %z + ret i32 %D +} + +define i32 @select_or_icmp_alt_bad_true_val(i32 %x, i32 %y, i32 %z, i32 %k) { +; CHECK-LABEL: @select_or_icmp_alt_bad_true_val( +; CHECK-NEXT: [[A:%.*]] = icmp ne i32 [[X:%.*]], [[Z:%.*]] +; CHECK-NEXT: [[B:%.*]] = icmp ne i32 [[Y:%.*]], [[Z]] +; CHECK-NEXT: [[C:%.*]] = or i1 [[A]], [[B]] +; CHECK-NEXT: [[D:%.*]] = select i1 [[C]], i32 [[K:%.*]], i32 [[Z]] +; CHECK-NEXT: ret i32 [[D]] +; + %A = icmp ne i32 %x, %z + %B = icmp ne i32 %y, %z + %C = or i1 %A, %B + %D = select i1 %C, i32 %k, i32 %z + ret i32 %D +} + +define i32 @select_or_icmp_alt_bad_false_val(i32 %x, i32 %y, i32 %z, i32 %k) { +; CHECK-LABEL: @select_or_icmp_alt_bad_false_val( +; CHECK-NEXT: [[A:%.*]] = icmp ne i32 [[X:%.*]], [[Z:%.*]] +; CHECK-NEXT: [[B:%.*]] = icmp ne i32 [[Y:%.*]], [[Z]] +; CHECK-NEXT: [[C:%.*]] = or i1 [[A]], [[B]] +; CHECK-NEXT: [[D:%.*]] = select i1 [[C]], i32 [[X]], i32 [[K:%.*]] +; CHECK-NEXT: ret i32 [[D]] +; + %A = icmp ne i32 %x, %z + %B = icmp ne i32 %y, %z + %C = or i1 %A, %B + %D = select i1 %C, i32 %x, i32 %k + ret i32 %D +}