[InstSimplify] Handle some overflow intrinsics in InstSimplify

This change does a few things:
- Move some InstCombine transforms to InstSimplify
- Run SimplifyCall from within InstCombine::visitCallInst
- Teach InstSimplify to fold [us]mul_with_overflow(X, undef) to 0.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@237995 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
David Majnemer
2015-05-22 03:56:46 +00:00
parent edcdc5cb6a
commit fe0d65bcc6
5 changed files with 75 additions and 19 deletions

View File

@ -3556,14 +3556,53 @@ static bool IsIdempotent(Intrinsic::ID ID) {
}
template <typename IterTy>
static Value *SimplifyIntrinsic(Intrinsic::ID IID, IterTy ArgBegin, IterTy ArgEnd,
static Value *SimplifyIntrinsic(Function *F, IterTy ArgBegin, IterTy ArgEnd,
const Query &Q, unsigned MaxRecurse) {
Intrinsic::ID IID = F->getIntrinsicID();
unsigned NumOperands = std::distance(ArgBegin, ArgEnd);
Type *ReturnType = F->getReturnType();
// Binary Ops
if (NumOperands == 2) {
Value *LHS = *ArgBegin;
Value *RHS = *(ArgBegin + 1);
if (IID == Intrinsic::usub_with_overflow ||
IID == Intrinsic::ssub_with_overflow) {
// X - X -> { 0, false }
if (LHS == RHS)
return Constant::getNullValue(ReturnType);
// X - undef -> undef
// undef - X -> undef
if (isa<UndefValue>(LHS) || isa<UndefValue>(RHS))
return UndefValue::get(ReturnType);
}
if (IID == Intrinsic::uadd_with_overflow ||
IID == Intrinsic::sadd_with_overflow) {
// X + undef -> undef
if (isa<UndefValue>(RHS))
return UndefValue::get(ReturnType);
}
if (IID == Intrinsic::umul_with_overflow ||
IID == Intrinsic::smul_with_overflow) {
// X * 0 -> { 0, false }
if (match(RHS, m_Zero()))
return Constant::getNullValue(ReturnType);
// X * undef -> { 0, false }
if (match(RHS, m_Undef()))
return Constant::getNullValue(ReturnType);
}
}
// Perform idempotent optimizations
if (!IsIdempotent(IID))
return nullptr;
// Unary Ops
if (std::distance(ArgBegin, ArgEnd) == 1)
if (NumOperands == 1)
if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(*ArgBegin))
if (II->getIntrinsicID() == IID)
return II;
@ -3587,9 +3626,8 @@ static Value *SimplifyCall(Value *V, IterTy ArgBegin, IterTy ArgEnd,
if (!F)
return nullptr;
if (Intrinsic::ID IID = F->getIntrinsicID())
if (Value *Ret =
SimplifyIntrinsic(IID, ArgBegin, ArgEnd, Q, MaxRecurse))
if (F->isIntrinsic())
if (Value *Ret = SimplifyIntrinsic(F, ArgBegin, ArgEnd, Q, MaxRecurse))
return Ret;
if (!canConstantFoldCallTo(F))