diff --git a/llvm/include/llvm/IR/Constants.h b/llvm/include/llvm/IR/Constants.h index 8aaedc3d082b0..b5e68054a1aea 100644 --- a/llvm/include/llvm/IR/Constants.h +++ b/llvm/include/llvm/IR/Constants.h @@ -1049,7 +1049,6 @@ class ConstantExpr : public Constant { static Constant *getAnd(Constant *C1, Constant *C2); static Constant *getOr(Constant *C1, Constant *C2); static Constant *getXor(Constant *C1, Constant *C2); - static Constant *getUMin(Constant *C1, Constant *C2); static Constant *getShl(Constant *C1, Constant *C2, bool HasNUW = false, bool HasNSW = false); static Constant *getLShr(Constant *C1, Constant *C2, bool isExact = false); diff --git a/llvm/lib/IR/Constants.cpp b/llvm/lib/IR/Constants.cpp index 6e25e224b1dbc..6a6209c37d393 100644 --- a/llvm/lib/IR/Constants.cpp +++ b/llvm/lib/IR/Constants.cpp @@ -2690,11 +2690,6 @@ Constant *ConstantExpr::getXor(Constant *C1, Constant *C2) { return get(Instruction::Xor, C1, C2); } -Constant *ConstantExpr::getUMin(Constant *C1, Constant *C2) { - Constant *Cmp = ConstantExpr::getICmp(CmpInst::ICMP_ULT, C1, C2); - return getSelect(Cmp, C1, C2); -} - Constant *ConstantExpr::getShl(Constant *C1, Constant *C2, bool HasNUW, bool HasNSW) { unsigned Flags = (HasNUW ? OverflowingBinaryOperator::NoUnsignedWrap : 0) | diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp index f399810ead2f6..2130177e8cf87 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp @@ -918,11 +918,18 @@ Instruction *InstCombinerImpl::visitTrunc(TruncInst &Trunc) { // removed by the trunc. if (match(C, m_SpecificInt_ICMP(ICmpInst::ICMP_ULE, APInt(SrcWidth, MaxShiftAmt)))) { + auto GetNewShAmt = [&](unsigned Width) { + Constant *MaxAmt = ConstantInt::get(SrcTy, Width - 1, false); + Constant *Cmp = + ConstantFoldCompareInstOperands(ICmpInst::ICMP_ULT, C, MaxAmt, DL); + Constant *ShAmt = ConstantFoldSelectInstruction(Cmp, C, MaxAmt); + return ConstantFoldCastOperand(Instruction::Trunc, ShAmt, A->getType(), + DL); + }; + // trunc (lshr (sext A), C) --> ashr A, C if (A->getType() == DestTy) { - Constant *MaxAmt = ConstantInt::get(SrcTy, DestWidth - 1, false); - Constant *ShAmt = ConstantExpr::getUMin(C, MaxAmt); - ShAmt = ConstantExpr::getTrunc(ShAmt, A->getType()); + Constant *ShAmt = GetNewShAmt(DestWidth); ShAmt = Constant::mergeUndefsWith(ShAmt, C); return IsExact ? BinaryOperator::CreateExactAShr(A, ShAmt) : BinaryOperator::CreateAShr(A, ShAmt); @@ -930,9 +937,7 @@ Instruction *InstCombinerImpl::visitTrunc(TruncInst &Trunc) { // The types are mismatched, so create a cast after shifting: // trunc (lshr (sext A), C) --> sext/trunc (ashr A, C) if (Src->hasOneUse()) { - Constant *MaxAmt = ConstantInt::get(SrcTy, AWidth - 1, false); - Constant *ShAmt = ConstantExpr::getUMin(C, MaxAmt); - ShAmt = ConstantExpr::getTrunc(ShAmt, A->getType()); + Constant *ShAmt = GetNewShAmt(AWidth); Value *Shift = Builder.CreateAShr(A, ShAmt, "", IsExact); return CastInst::CreateIntegerCast(Shift, DestTy, true); }