Index: lib/Transforms/InstCombine/InstCombineCompares.cpp =================================================================== --- lib/Transforms/InstCombine/InstCombineCompares.cpp +++ lib/Transforms/InstCombine/InstCombineCompares.cpp @@ -3030,6 +3030,48 @@ return nullptr; } +/// Fold icmp Pred smin(X, Y), X. +Instruction *InstCombiner::foldICmpWithSMin(ICmpInst &Cmp) { + ICmpInst::Predicate Pred = Cmp.getPredicate(); + Value *Op0 = Cmp.getOperand(0); + Value *X = Cmp.getOperand(1); + + // Canonicalize minimum operand to LHS of the icmp. + if (match(X, m_SMin(m_Specific(Op0), m_Value())) || + match(X, m_SMin(m_Value(), m_Specific(Op0)))) { + std::swap(Op0, X); + Pred = Cmp.getSwappedPredicate(); + } + + Value *Y; + if (!match(Op0, m_SMin(m_Specific(X), m_Value(Y))) && + !match(Op0, m_SMin(m_Value(Y), m_Specific(X)))) + return nullptr; + + // TODO: The cases where we fold to a constant should be handled by + // InstSimplify, so we could assert on those? + switch (Pred) { + case CmpInst::ICMP_EQ: + case CmpInst::ICMP_SGE: + // smin(X, Y) == X --> X <= Y + // smin(X, Y) >= X --> X <= Y + return new ICmpInst(ICmpInst::ICMP_SLE, X, Y); + case CmpInst::ICMP_NE: + case CmpInst::ICMP_SLT: + // smin(X, Y) != X --> X > Y + // smin(X, Y) < X --> X > Y + return new ICmpInst(ICmpInst::ICMP_SGT, X, Y); + case CmpInst::ICMP_SLE: + // smin(X, Y) <= X --> true + return replaceInstUsesWith(Cmp, ConstantInt::getTrue(Cmp.getType())); + case CmpInst::ICMP_SGT: + // smin(X, Y) > X --> false + return replaceInstUsesWith(Cmp, ConstantInt::getFalse(Cmp.getType())); + default: + return nullptr; + } +} + Instruction *InstCombiner::foldICmpEquality(ICmpInst &I) { if (!I.isEquality()) return nullptr; @@ -4277,6 +4319,9 @@ if (Instruction *Res = foldICmpBinOp(I)) return Res; + if (Instruction *Res = foldICmpWithSMin(I)) + return Res; + { Value *A, *B; // Transform (A & ~B) == 0 --> (A & B) != 0 Index: lib/Transforms/InstCombine/InstCombineInternal.h =================================================================== --- lib/Transforms/InstCombine/InstCombineInternal.h +++ lib/Transforms/InstCombine/InstCombineInternal.h @@ -571,6 +571,7 @@ Instruction *foldICmpInstWithConstant(ICmpInst &Cmp); Instruction *foldICmpInstWithConstantNotInt(ICmpInst &Cmp); Instruction *foldICmpBinOp(ICmpInst &Cmp); + Instruction *foldICmpWithSMin(ICmpInst &Cmp); Instruction *foldICmpEquality(ICmpInst &Cmp); Instruction *foldICmpTruncConstant(ICmpInst &Cmp, Instruction *Trunc, Index: test/Transforms/InstCombine/smin-icmp.ll =================================================================== --- test/Transforms/InstCombine/smin-icmp.ll +++ test/Transforms/InstCombine/smin-icmp.ll @@ -11,9 +11,7 @@ define i1 @eq_smin1(i32 %x, i32 %y) { ; CHECK-LABEL: @eq_smin1( -; CHECK-NEXT: [[CMP1:%.*]] = icmp slt i32 %x, %y -; CHECK-NEXT: [[SEL:%.*]] = select i1 [[CMP1]], i32 %x, i32 %y -; CHECK-NEXT: [[CMP2:%.*]] = icmp eq i32 [[SEL]], %x +; CHECK-NEXT: [[CMP2:%.*]] = icmp sle i32 %x, %y ; CHECK-NEXT: ret i1 [[CMP2]] ; %cmp1 = icmp slt i32 %x, %y @@ -26,9 +24,7 @@ define i1 @eq_smin2(i32 %x, i32 %y) { ; CHECK-LABEL: @eq_smin2( -; CHECK-NEXT: [[CMP1:%.*]] = icmp slt i32 %y, %x -; CHECK-NEXT: [[SEL:%.*]] = select i1 [[CMP1]], i32 %y, i32 %x -; CHECK-NEXT: [[CMP2:%.*]] = icmp eq i32 [[SEL]], %x +; CHECK-NEXT: [[CMP2:%.*]] = icmp sle i32 %x, %y ; CHECK-NEXT: ret i1 [[CMP2]] ; %cmp1 = icmp slt i32 %y, %x @@ -42,9 +38,7 @@ define i1 @eq_smin3(i32 %a, i32 %y) { ; CHECK-LABEL: @eq_smin3( ; CHECK-NEXT: [[X:%.*]] = add i32 %a, 3 -; CHECK-NEXT: [[CMP1:%.*]] = icmp slt i32 [[X]], %y -; CHECK-NEXT: [[SEL:%.*]] = select i1 [[CMP1]], i32 [[X]], i32 %y -; CHECK-NEXT: [[CMP2:%.*]] = icmp eq i32 [[X]], [[SEL]] +; CHECK-NEXT: [[CMP2:%.*]] = icmp sle i32 [[X]], %y ; CHECK-NEXT: ret i1 [[CMP2]] ; %x = add i32 %a, 3 ; thwart complexity-based canonicalization @@ -59,9 +53,7 @@ define i1 @eq_smin4(i32 %a, i32 %y) { ; CHECK-LABEL: @eq_smin4( ; CHECK-NEXT: [[X:%.*]] = add i32 %a, 3 -; CHECK-NEXT: [[CMP1:%.*]] = icmp sgt i32 [[X]], %y -; CHECK-NEXT: [[SEL:%.*]] = select i1 [[CMP1]], i32 %y, i32 [[X]] -; CHECK-NEXT: [[CMP2:%.*]] = icmp eq i32 [[X]], [[SEL]] +; CHECK-NEXT: [[CMP2:%.*]] = icmp sle i32 [[X]], %y ; CHECK-NEXT: ret i1 [[CMP2]] ; %x = add i32 %a, 3 ; thwart complexity-based canonicalization @@ -75,9 +67,7 @@ define i1 @sge_smin1(i32 %x, i32 %y) { ; CHECK-LABEL: @sge_smin1( -; CHECK-NEXT: [[CMP1:%.*]] = icmp slt i32 %x, %y -; CHECK-NEXT: [[SEL:%.*]] = select i1 [[CMP1]], i32 %x, i32 %y -; CHECK-NEXT: [[CMP2:%.*]] = icmp sge i32 [[SEL]], %x +; CHECK-NEXT: [[CMP2:%.*]] = icmp sle i32 %x, %y ; CHECK-NEXT: ret i1 [[CMP2]] ; %cmp1 = icmp slt i32 %x, %y @@ -90,9 +80,7 @@ define i1 @sge_smin2(i32 %x, i32 %y) { ; CHECK-LABEL: @sge_smin2( -; CHECK-NEXT: [[CMP1:%.*]] = icmp slt i32 %y, %x -; CHECK-NEXT: [[SEL:%.*]] = select i1 [[CMP1]], i32 %y, i32 %x -; CHECK-NEXT: [[CMP2:%.*]] = icmp sge i32 [[SEL]], %x +; CHECK-NEXT: [[CMP2:%.*]] = icmp sle i32 %x, %y ; CHECK-NEXT: ret i1 [[CMP2]] ; %cmp1 = icmp slt i32 %y, %x @@ -106,9 +94,7 @@ define i1 @sge_smin3(i32 %a, i32 %y) { ; CHECK-LABEL: @sge_smin3( ; CHECK-NEXT: [[X:%.*]] = add i32 %a, 3 -; CHECK-NEXT: [[CMP1:%.*]] = icmp slt i32 [[X]], %y -; CHECK-NEXT: [[SEL:%.*]] = select i1 [[CMP1]], i32 [[X]], i32 %y -; CHECK-NEXT: [[CMP2:%.*]] = icmp sle i32 [[X]], [[SEL]] +; CHECK-NEXT: [[CMP2:%.*]] = icmp sle i32 [[X]], %y ; CHECK-NEXT: ret i1 [[CMP2]] ; %x = add i32 %a, 3 ; thwart complexity-based canonicalization @@ -123,9 +109,7 @@ define i1 @sge_smin4(i32 %a, i32 %y) { ; CHECK-LABEL: @sge_smin4( ; CHECK-NEXT: [[X:%.*]] = add i32 %a, 3 -; CHECK-NEXT: [[CMP1:%.*]] = icmp sgt i32 [[X]], %y -; CHECK-NEXT: [[SEL:%.*]] = select i1 [[CMP1]], i32 %y, i32 [[X]] -; CHECK-NEXT: [[CMP2:%.*]] = icmp sle i32 [[X]], [[SEL]] +; CHECK-NEXT: [[CMP2:%.*]] = icmp sle i32 [[X]], %y ; CHECK-NEXT: ret i1 [[CMP2]] ; %x = add i32 %a, 3 ; thwart complexity-based canonicalization @@ -139,9 +123,7 @@ define i1 @ne_smin1(i32 %x, i32 %y) { ; CHECK-LABEL: @ne_smin1( -; CHECK-NEXT: [[CMP1:%.*]] = icmp slt i32 %x, %y -; CHECK-NEXT: [[SEL:%.*]] = select i1 [[CMP1]], i32 %x, i32 %y -; CHECK-NEXT: [[CMP2:%.*]] = icmp ne i32 [[SEL]], %x +; CHECK-NEXT: [[CMP2:%.*]] = icmp sgt i32 %x, %y ; CHECK-NEXT: ret i1 [[CMP2]] ; %cmp1 = icmp slt i32 %x, %y @@ -168,9 +150,7 @@ define i1 @ne_smin3(i32 %a, i32 %y) { ; CHECK-LABEL: @ne_smin3( ; CHECK-NEXT: [[X:%.*]] = add i32 %a, 3 -; CHECK-NEXT: [[CMP1:%.*]] = icmp slt i32 [[X]], %y -; CHECK-NEXT: [[SEL:%.*]] = select i1 [[CMP1]], i32 [[X]], i32 %y -; CHECK-NEXT: [[CMP2:%.*]] = icmp ne i32 [[X]], [[SEL]] +; CHECK-NEXT: [[CMP2:%.*]] = icmp sgt i32 [[X]], %y ; CHECK-NEXT: ret i1 [[CMP2]] ; %x = add i32 %a, 3 ; thwart complexity-based canonicalization @@ -199,9 +179,7 @@ define i1 @slt_smin1(i32 %x, i32 %y) { ; CHECK-LABEL: @slt_smin1( -; CHECK-NEXT: [[CMP1:%.*]] = icmp slt i32 %x, %y -; CHECK-NEXT: [[SEL:%.*]] = select i1 [[CMP1]], i32 %x, i32 %y -; CHECK-NEXT: [[CMP2:%.*]] = icmp slt i32 [[SEL]], %x +; CHECK-NEXT: [[CMP2:%.*]] = icmp sgt i32 %x, %y ; CHECK-NEXT: ret i1 [[CMP2]] ; %cmp1 = icmp slt i32 %x, %y @@ -228,9 +206,7 @@ define i1 @slt_smin3(i32 %a, i32 %y) { ; CHECK-LABEL: @slt_smin3( ; CHECK-NEXT: [[X:%.*]] = add i32 %a, 3 -; CHECK-NEXT: [[CMP1:%.*]] = icmp slt i32 [[X]], %y -; CHECK-NEXT: [[SEL:%.*]] = select i1 [[CMP1]], i32 [[X]], i32 %y -; CHECK-NEXT: [[CMP2:%.*]] = icmp sgt i32 [[X]], [[SEL]] +; CHECK-NEXT: [[CMP2:%.*]] = icmp sgt i32 [[X]], %y ; CHECK-NEXT: ret i1 [[CMP2]] ; %x = add i32 %a, 3 ; thwart complexity-based canonicalization