diff --git a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp index 03fc1a09edc9..cfae47fbd7e9 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp @@ -424,20 +424,24 @@ static bool adjustMinMax(SelectInst &Sel, ICmpInst &Cmp) { Value *FalseVal = Sel.getFalseValue(); // We may move or edit the compare, so make sure the select is the only user. - const APInt *CmpC; - if (!Cmp.hasOneUse() || !match(CmpRHS, m_APInt(CmpC))) + if (!Cmp.hasOneUse()) return false; - // These transforms only work for selects of integers or vector integers. - auto *SelEltTy = dyn_cast(Sel.getType()->getScalarType()); - if (!SelEltTy) + // FIXME: Use m_APInt to allow vector folds. + auto *CI = dyn_cast(CmpRHS); + if (!CI) + return false; + + // These transformations only work for selects over integers. + IntegerType *SelectTy = dyn_cast(Sel.getType()); + if (!SelectTy) return false; Constant *AdjustedRHS; if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_SGT) - AdjustedRHS = ConstantInt::get(CmpRHS->getType(), *CmpC + 1); + AdjustedRHS = ConstantInt::get(CI->getContext(), CI->getValue() + 1); else if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_SLT) - AdjustedRHS = ConstantInt::get(CmpRHS->getType(), *CmpC - 1); + AdjustedRHS = ConstantInt::get(CI->getContext(), CI->getValue() - 1); else return false; @@ -450,8 +454,8 @@ static bool adjustMinMax(SelectInst &Sel, ICmpInst &Cmp) { // Types do not match. Instead of calculating this with mixed types, promote // all to the larger type. This enables scalar evolution to analyze this // expression. - else if (CmpRHS->getType()->getScalarSizeInBits() < SelEltTy->getBitWidth()) { - Constant *SextRHS = ConstantExpr::getSExt(AdjustedRHS, Sel.getType()); + else if (CmpRHS->getType()->getScalarSizeInBits() < SelectTy->getBitWidth()) { + Constant *SextRHS = ConstantExpr::getSExt(AdjustedRHS, SelectTy); // X = sext x; x >s c ? X : C+1 --> X = sext x; X X = sext x; X >s C-1 ? C-1 : X @@ -465,7 +469,7 @@ static bool adjustMinMax(SelectInst &Sel, ICmpInst &Cmp) { CmpLHS = FalseVal; AdjustedRHS = SextRHS; } else if (Cmp.isUnsigned()) { - Constant *ZextRHS = ConstantExpr::getZExt(AdjustedRHS, Sel.getType()); + Constant *ZextRHS = ConstantExpr::getZExt(AdjustedRHS, SelectTy); // X = zext x; x >u c ? X : C+1 --> X = zext x; X X = zext x; X >u C-1 ? C-1 : X // zext + signed compare cannot be changed: diff --git a/llvm/test/Transforms/InstCombine/adjust-for-minmax.ll b/llvm/test/Transforms/InstCombine/adjust-for-minmax.ll index 1cb9322e57d7..813171b80857 100644 --- a/llvm/test/Transforms/InstCombine/adjust-for-minmax.ll +++ b/llvm/test/Transforms/InstCombine/adjust-for-minmax.ll @@ -68,12 +68,13 @@ define i32 @smax3(i32 %n) { ret i32 %m } +; FIXME ; Swap vector signed pred and select ops. define <2 x i32> @smax3_vec(<2 x i32> %n) { ; CHECK-LABEL: @smax3_vec( -; CHECK-NEXT: [[T:%.*]] = icmp slt <2 x i32> %n, zeroinitializer -; CHECK-NEXT: [[M:%.*]] = select <2 x i1> [[T]], <2 x i32> zeroinitializer, <2 x i32> %n +; CHECK-NEXT: [[T:%.*]] = icmp sgt <2 x i32> %n, +; CHECK-NEXT: [[M:%.*]] = select <2 x i1> [[T]], <2 x i32> %n, <2 x i32> zeroinitializer ; CHECK-NEXT: ret <2 x i32> [[M]] ; %t = icmp sgt <2 x i32> %n, @@ -94,12 +95,13 @@ define i32 @smin3(i32 %n) { ret i32 %m } +; FIXME ; Swap vector signed pred and select ops. define <2 x i32> @smin3_vec(<2 x i32> %n) { ; CHECK-LABEL: @smin3_vec( -; CHECK-NEXT: [[T:%.*]] = icmp sgt <2 x i32> %n, zeroinitializer -; CHECK-NEXT: [[M:%.*]] = select <2 x i1> [[T]], <2 x i32> zeroinitializer, <2 x i32> %n +; CHECK-NEXT: [[T:%.*]] = icmp slt <2 x i32> %n, +; CHECK-NEXT: [[M:%.*]] = select <2 x i1> [[T]], <2 x i32> %n, <2 x i32> zeroinitializer ; CHECK-NEXT: ret <2 x i32> [[M]] ; %t = icmp slt <2 x i32> %n, @@ -120,12 +122,13 @@ define i32 @umax3(i32 %n) { ret i32 %m } +; FIXME ; Swap vector unsigned pred and select ops. define <2 x i32> @umax3_vec(<2 x i32> %n) { ; CHECK-LABEL: @umax3_vec( -; CHECK-NEXT: [[T:%.*]] = icmp ult <2 x i32> %n, -; CHECK-NEXT: [[M:%.*]] = select <2 x i1> [[T]], <2 x i32> , <2 x i32> %n +; CHECK-NEXT: [[T:%.*]] = icmp ugt <2 x i32> %n, +; CHECK-NEXT: [[M:%.*]] = select <2 x i1> [[T]], <2 x i32> %n, <2 x i32> ; CHECK-NEXT: ret <2 x i32> [[M]] ; %t = icmp ugt <2 x i32> %n, @@ -146,12 +149,13 @@ define i32 @umin3(i32 %n) { ret i32 %m } +; FIXME ; Swap vector unsigned pred and select ops. define <2 x i32> @umin3_vec(<2 x i32> %n) { ; CHECK-LABEL: @umin3_vec( -; CHECK-NEXT: [[T:%.*]] = icmp ugt <2 x i32> %n, -; CHECK-NEXT: [[M:%.*]] = select <2 x i1> [[T]], <2 x i32> , <2 x i32> %n +; CHECK-NEXT: [[T:%.*]] = icmp ult <2 x i32> %n, +; CHECK-NEXT: [[M:%.*]] = select <2 x i1> [[T]], <2 x i32> %n, <2 x i32> ; CHECK-NEXT: ret <2 x i32> [[M]] ; %t = icmp ult <2 x i32> %n, @@ -172,12 +176,13 @@ define i32 @smax4(i32 %n) { ret i32 %m } +; FIXME ; Canonicalize vector signed pred and swap pred and select ops. define <2 x i32> @smax4_vec(<2 x i32> %n) { ; CHECK-LABEL: @smax4_vec( -; CHECK-NEXT: [[T:%.*]] = icmp slt <2 x i32> %n, zeroinitializer -; CHECK-NEXT: [[M:%.*]] = select <2 x i1> [[T]], <2 x i32> zeroinitializer, <2 x i32> %n +; CHECK-NEXT: [[T:%.*]] = icmp sgt <2 x i32> %n, +; CHECK-NEXT: [[M:%.*]] = select <2 x i1> [[T]], <2 x i32> %n, <2 x i32> zeroinitializer ; CHECK-NEXT: ret <2 x i32> [[M]] ; %t = icmp sge <2 x i32> %n, zeroinitializer @@ -198,12 +203,13 @@ define i32 @smin4(i32 %n) { ret i32 %m } +; FIXME ; Canonicalize vector signed pred and swap pred and select ops. define <2 x i32> @smin4_vec(<2 x i32> %n) { ; CHECK-LABEL: @smin4_vec( -; CHECK-NEXT: [[T:%.*]] = icmp sgt <2 x i32> %n, zeroinitializer -; CHECK-NEXT: [[M:%.*]] = select <2 x i1> [[T]], <2 x i32> zeroinitializer, <2 x i32> %n +; CHECK-NEXT: [[T:%.*]] = icmp slt <2 x i32> %n, +; CHECK-NEXT: [[M:%.*]] = select <2 x i1> [[T]], <2 x i32> %n, <2 x i32> zeroinitializer ; CHECK-NEXT: ret <2 x i32> [[M]] ; %t = icmp sle <2 x i32> %n, zeroinitializer @@ -224,12 +230,13 @@ define i32 @umax4(i32 %n) { ret i32 %m } +; FIXME ; Canonicalize vector unsigned pred and swap pred and select ops. define <2 x i32> @umax4_vec(<2 x i32> %n) { ; CHECK-LABEL: @umax4_vec( -; CHECK-NEXT: [[T:%.*]] = icmp ult <2 x i32> %n, -; CHECK-NEXT: [[M:%.*]] = select <2 x i1> [[T]], <2 x i32> , <2 x i32> %n +; CHECK-NEXT: [[T:%.*]] = icmp ugt <2 x i32> %n, +; CHECK-NEXT: [[M:%.*]] = select <2 x i1> [[T]], <2 x i32> %n, <2 x i32> ; CHECK-NEXT: ret <2 x i32> [[M]] ; %t = icmp uge <2 x i32> %n, @@ -250,12 +257,13 @@ define i32 @umin4(i32 %n) { ret i32 %m } +; FIXME ; Canonicalize vector unsigned pred and swap pred and select ops. define <2 x i32> @umin4_vec(<2 x i32> %n) { ; CHECK-LABEL: @umin4_vec( -; CHECK-NEXT: [[T:%.*]] = icmp ugt <2 x i32> %n, -; CHECK-NEXT: [[M:%.*]] = select <2 x i1> [[T]], <2 x i32> , <2 x i32> %n +; CHECK-NEXT: [[T:%.*]] = icmp ult <2 x i32> %n, +; CHECK-NEXT: [[M:%.*]] = select <2 x i1> [[T]], <2 x i32> %n, <2 x i32> ; CHECK-NEXT: ret <2 x i32> [[M]] ; %t = icmp ule <2 x i32> %n, @@ -276,11 +284,12 @@ define i64 @smax_sext(i32 %a) { ret i64 %max } +; FIXME define <2 x i64> @smax_sext_vec(<2 x i32> %a) { ; CHECK-LABEL: @smax_sext_vec( ; CHECK-NEXT: [[A_EXT:%.*]] = sext <2 x i32> %a to <2 x i64> -; CHECK-NEXT: [[CMP:%.*]] = icmp slt <2 x i64> [[A_EXT]], zeroinitializer -; CHECK-NEXT: [[MAX:%.*]] = select <2 x i1> [[CMP]], <2 x i64> zeroinitializer, <2 x i64> [[A_EXT]] +; CHECK-NEXT: [[CMP:%.*]] = icmp sgt <2 x i32> %a, +; CHECK-NEXT: [[MAX:%.*]] = select <2 x i1> [[CMP]], <2 x i64> [[A_EXT]], <2 x i64> zeroinitializer ; CHECK-NEXT: ret <2 x i64> [[MAX]] ; %a_ext = sext <2 x i32> %a to <2 x i64> @@ -302,11 +311,12 @@ define i64 @smin_sext(i32 %a) { ret i64 %min } +; FIXME define <2 x i64>@smin_sext_vec(<2 x i32> %a) { ; CHECK-LABEL: @smin_sext_vec( ; CHECK-NEXT: [[A_EXT:%.*]] = sext <2 x i32> %a to <2 x i64> -; CHECK-NEXT: [[CMP:%.*]] = icmp sgt <2 x i64> [[A_EXT]], zeroinitializer -; CHECK-NEXT: [[MIN:%.*]] = select <2 x i1> [[CMP]], <2 x i64> zeroinitializer, <2 x i64> [[A_EXT]] +; CHECK-NEXT: [[CMP:%.*]] = icmp slt <2 x i32> %a, +; CHECK-NEXT: [[MIN:%.*]] = select <2 x i1> [[CMP]], <2 x i64> [[A_EXT]], <2 x i64> zeroinitializer ; CHECK-NEXT: ret <2 x i64> [[MIN]] ; %a_ext = sext <2 x i32> %a to <2 x i64> @@ -328,11 +338,12 @@ define i64 @umax_sext(i32 %a) { ret i64 %max } +; FIXME define <2 x i64> @umax_sext_vec(<2 x i32> %a) { ; CHECK-LABEL: @umax_sext_vec( ; CHECK-NEXT: [[A_EXT:%.*]] = sext <2 x i32> %a to <2 x i64> -; CHECK-NEXT: [[CMP:%.*]] = icmp ult <2 x i64> [[A_EXT]], -; CHECK-NEXT: [[MAX:%.*]] = select <2 x i1> [[CMP]], <2 x i64> , <2 x i64> [[A_EXT]] +; CHECK-NEXT: [[CMP:%.*]] = icmp ugt <2 x i32> %a, +; CHECK-NEXT: [[MAX:%.*]] = select <2 x i1> [[CMP]], <2 x i64> [[A_EXT]], <2 x i64> ; CHECK-NEXT: ret <2 x i64> [[MAX]] ; %a_ext = sext <2 x i32> %a to <2 x i64> @@ -354,11 +365,12 @@ define i64 @umin_sext(i32 %a) { ret i64 %min } +; FIXME define <2 x i64> @umin_sext_vec(<2 x i32> %a) { ; CHECK-LABEL: @umin_sext_vec( ; CHECK-NEXT: [[A_EXT:%.*]] = sext <2 x i32> %a to <2 x i64> -; CHECK-NEXT: [[CMP:%.*]] = icmp ugt <2 x i64> [[A_EXT]], -; CHECK-NEXT: [[MIN:%.*]] = select <2 x i1> [[CMP]], <2 x i64> , <2 x i64> [[A_EXT]] +; CHECK-NEXT: [[CMP:%.*]] = icmp ult <2 x i32> %a, +; CHECK-NEXT: [[MIN:%.*]] = select <2 x i1> [[CMP]], <2 x i64> [[A_EXT]], <2 x i64> ; CHECK-NEXT: ret <2 x i64> [[MIN]] ; %a_ext = sext <2 x i32> %a to <2 x i64> @@ -380,11 +392,12 @@ define i64 @umax_sext2(i32 %a) { ret i64 %min } +; FIXME define <2 x i64> @umax_sext2_vec(<2 x i32> %a) { ; CHECK-LABEL: @umax_sext2_vec( ; CHECK-NEXT: [[A_EXT:%.*]] = sext <2 x i32> %a to <2 x i64> -; CHECK-NEXT: [[CMP:%.*]] = icmp ugt <2 x i64> [[A_EXT]], -; CHECK-NEXT: [[MIN:%.*]] = select <2 x i1> [[CMP]], <2 x i64> [[A_EXT]], <2 x i64> +; CHECK-NEXT: [[CMP:%.*]] = icmp ult <2 x i32> %a, +; CHECK-NEXT: [[MIN:%.*]] = select <2 x i1> [[CMP]], <2 x i64> , <2 x i64> [[A_EXT]] ; CHECK-NEXT: ret <2 x i64> [[MIN]] ; %a_ext = sext <2 x i32> %a to <2 x i64> @@ -406,11 +419,12 @@ define i64 @umin_sext2(i32 %a) { ret i64 %min } +; FIXME define <2 x i64> @umin_sext2_vec(<2 x i32> %a) { ; CHECK-LABEL: @umin_sext2_vec( ; CHECK-NEXT: [[A_EXT:%.*]] = sext <2 x i32> %a to <2 x i64> -; CHECK-NEXT: [[CMP:%.*]] = icmp ult <2 x i64> [[A_EXT]], -; CHECK-NEXT: [[MIN:%.*]] = select <2 x i1> [[CMP]], <2 x i64> [[A_EXT]], <2 x i64> +; CHECK-NEXT: [[CMP:%.*]] = icmp ugt <2 x i32> %a, +; CHECK-NEXT: [[MIN:%.*]] = select <2 x i1> [[CMP]], <2 x i64> , <2 x i64> [[A_EXT]] ; CHECK-NEXT: ret <2 x i64> [[MIN]] ; %a_ext = sext <2 x i32> %a to <2 x i64> @@ -432,11 +446,12 @@ define i64 @umax_zext(i32 %a) { ret i64 %max } +; FIXME define <2 x i64> @umax_zext_vec(<2 x i32> %a) { ; CHECK-LABEL: @umax_zext_vec( ; CHECK-NEXT: [[A_EXT:%.*]] = zext <2 x i32> %a to <2 x i64> -; CHECK-NEXT: [[CMP:%.*]] = icmp ult <2 x i64> [[A_EXT]], -; CHECK-NEXT: [[MAX:%.*]] = select <2 x i1> [[CMP]], <2 x i64> , <2 x i64> [[A_EXT]] +; CHECK-NEXT: [[CMP:%.*]] = icmp ugt <2 x i32> %a, +; CHECK-NEXT: [[MAX:%.*]] = select <2 x i1> [[CMP]], <2 x i64> [[A_EXT]], <2 x i64> ; CHECK-NEXT: ret <2 x i64> [[MAX]] ; %a_ext = zext <2 x i32> %a to <2 x i64> @@ -458,11 +473,12 @@ define i64 @umin_zext(i32 %a) { ret i64 %min } +; FIXME define <2 x i64> @umin_zext_vec(<2 x i32> %a) { ; CHECK-LABEL: @umin_zext_vec( ; CHECK-NEXT: [[A_EXT:%.*]] = zext <2 x i32> %a to <2 x i64> -; CHECK-NEXT: [[CMP:%.*]] = icmp ugt <2 x i64> [[A_EXT]], -; CHECK-NEXT: [[MIN:%.*]] = select <2 x i1> [[CMP]], <2 x i64> , <2 x i64> [[A_EXT]] +; CHECK-NEXT: [[CMP:%.*]] = icmp ult <2 x i32> %a, +; CHECK-NEXT: [[MIN:%.*]] = select <2 x i1> [[CMP]], <2 x i64> [[A_EXT]], <2 x i64> ; CHECK-NEXT: ret <2 x i64> [[MIN]] ; %a_ext = zext <2 x i32> %a to <2 x i64>