}
Instruction *InstCombiner::foldIntrinsicWithOverflowCommon(IntrinsicInst *II) {
- OverflowCheckFlavor OCF =
- IntrinsicIDToOverflowCheckFlavor(II->getIntrinsicID());
- assert(OCF != OCF_INVALID && "unexpected!");
-
+ WithOverflowInst *WO = cast<WithOverflowInst>(II);
Value *OperationResult = nullptr;
Constant *OverflowResult = nullptr;
- if (OptimizeOverflowCheck(OCF, II->getArgOperand(0), II->getArgOperand(1),
- *II, OperationResult, OverflowResult))
- return CreateOverflowTuple(II, OperationResult, OverflowResult);
+ if (OptimizeOverflowCheck(WO->getBinaryOp(), WO->isSigned(), WO->getLHS(),
+ WO->getRHS(), *WO, OperationResult, OverflowResult))
+ return CreateOverflowTuple(WO, OperationResult, OverflowResult);
return nullptr;
}
return BinaryOperator::CreateNot(Result);
}
-bool InstCombiner::OptimizeOverflowCheck(OverflowCheckFlavor OCF, Value *LHS,
- Value *RHS, Instruction &OrigI,
- Value *&Result, Constant *&Overflow) {
+bool InstCombiner::OptimizeOverflowCheck(
+ Instruction::BinaryOps BinaryOp, bool IsSigned, Value *LHS, Value *RHS,
+ Instruction &OrigI, Value *&Result, Constant *&Overflow) {
if (OrigI.isCommutative() && isa<Constant>(LHS) && !isa<Constant>(RHS))
std::swap(LHS, RHS);
// compare.
Builder.SetInsertPoint(&OrigI);
- switch (OCF) {
- case OCF_INVALID:
- llvm_unreachable("bad overflow check kind!");
+ switch (BinaryOp) {
+ default:
+ llvm_unreachable("unsupported binary op");
- case OCF_UNSIGNED_ADD:
- case OCF_SIGNED_ADD: {
+ case Instruction::Add: {
// X + 0 -> {X, false}
if (match(RHS, m_Zero()))
return SetResult(LHS, Builder.getFalse(), false);
OverflowResult OR;
- if (OCF == OCF_UNSIGNED_ADD) {
+ if (!IsSigned) {
OR = computeOverflowForUnsignedAdd(LHS, RHS, &OrigI);
if (OR == OverflowResult::NeverOverflows)
return SetResult(Builder.CreateNUWAdd(LHS, RHS), Builder.getFalse(),
break;
}
- case OCF_UNSIGNED_SUB:
- case OCF_SIGNED_SUB: {
+ case Instruction::Sub: {
// X - 0 -> {X, false}
if (match(RHS, m_Zero()))
return SetResult(LHS, Builder.getFalse(), false);
OverflowResult OR;
- if (OCF == OCF_UNSIGNED_SUB) {
+ if (!IsSigned) {
OR = computeOverflowForUnsignedSub(LHS, RHS, &OrigI);
if (OR == OverflowResult::NeverOverflows)
return SetResult(Builder.CreateNUWSub(LHS, RHS), Builder.getFalse(),
break;
}
- case OCF_UNSIGNED_MUL:
- case OCF_SIGNED_MUL: {
+ case Instruction::Mul: {
// X * 1 -> {X, false}
if (match(RHS, m_One()))
return SetResult(LHS, Builder.getFalse(), false);
OverflowResult OR;
- if (OCF == OCF_UNSIGNED_MUL) {
+ if (!IsSigned) {
OR = computeOverflowForUnsignedMul(LHS, RHS, &OrigI);
if (OR == OverflowResult::NeverOverflows)
return SetResult(Builder.CreateNUWMul(LHS, RHS), Builder.getFalse(),
isa<IntegerType>(A->getType())) {
Value *Result;
Constant *Overflow;
- if (OptimizeOverflowCheck(OCF_UNSIGNED_ADD, A, B, *AddI, Result,
- Overflow)) {
+ if (OptimizeOverflowCheck(Instruction::Add, /*Signed*/false, A, B,
+ *AddI, Result, Overflow)) {
replaceInstUsesWith(*AddI, Result);
return replaceInstUsesWith(I, Overflow);
}
return false;
}
-/// Specific patterns of overflow check idioms that we match.
-enum OverflowCheckFlavor {
- OCF_UNSIGNED_ADD,
- OCF_SIGNED_ADD,
- OCF_UNSIGNED_SUB,
- OCF_SIGNED_SUB,
- OCF_UNSIGNED_MUL,
- OCF_SIGNED_MUL,
-
- OCF_INVALID
-};
-
-/// Returns the OverflowCheckFlavor corresponding to a overflow_with_op
-/// intrinsic.
-static inline OverflowCheckFlavor
-IntrinsicIDToOverflowCheckFlavor(unsigned ID) {
- switch (ID) {
- default:
- return OCF_INVALID;
- case Intrinsic::uadd_with_overflow:
- return OCF_UNSIGNED_ADD;
- case Intrinsic::sadd_with_overflow:
- return OCF_SIGNED_ADD;
- case Intrinsic::usub_with_overflow:
- return OCF_UNSIGNED_SUB;
- case Intrinsic::ssub_with_overflow:
- return OCF_SIGNED_SUB;
- case Intrinsic::umul_with_overflow:
- return OCF_UNSIGNED_MUL;
- case Intrinsic::smul_with_overflow:
- return OCF_SIGNED_MUL;
- }
-}
-
/// Some binary operators require special handling to avoid poison and undefined
/// behavior. If a constant vector has undef elements, replace those undefs with
/// identity constants if possible because those are always safe to execute.
/// operation in OperationResult and result of the overflow check in
/// OverflowResult, and return true. If no simplification is possible,
/// returns false.
- bool OptimizeOverflowCheck(OverflowCheckFlavor OCF, Value *LHS, Value *RHS,
+ bool OptimizeOverflowCheck(Instruction::BinaryOps BinaryOp, bool IsSigned,
+ Value *LHS, Value *RHS,
Instruction &CtxI, Value *&OperationResult,
Constant *&OverflowResult);