Index: llvm/lib/Analysis/LazyValueInfo.cpp =================================================================== --- llvm/lib/Analysis/LazyValueInfo.cpp +++ llvm/lib/Analysis/LazyValueInfo.cpp @@ -426,6 +426,8 @@ BasicBlock *BB); bool solveBlockValueCast(ValueLatticeElement &BBLV, CastInst *CI, BasicBlock *BB); + bool solveBlockValueOverflowIntrinsic( + ValueLatticeElement &BBLV, IntrinsicInst *II, BasicBlock *BB); void intersectAssumeOrGuardBlockValueConstantRange(Value *Val, ValueLatticeElement &BBLV, Instruction *BBI); @@ -638,6 +640,11 @@ if (BinaryOperator *BO = dyn_cast(BBI)) return solveBlockValueBinaryOp(Res, BO, BB); + + if (auto *EVI = dyn_cast(BBI)) + if (auto *II = dyn_cast(EVI->getAggregateOperand())) + if (EVI->getNumIndices() == 1 && *EVI->idx_begin() == 0) + return solveBlockValueOverflowIntrinsic(Res, II, BB); } LLVM_DEBUG(dbgs() << " compute BB '" << BB->getName() @@ -1071,6 +1078,39 @@ return true; } +bool LazyValueInfoImpl::solveBlockValueOverflowIntrinsic( + ValueLatticeElement &BBLV, IntrinsicInst *II, BasicBlock *BB) { + Instruction::BinaryOps BinOp; + switch (II->getIntrinsicID()) { + case Intrinsic::uadd_with_overflow: + case Intrinsic::sadd_with_overflow: + BinOp = Instruction::Add; + break; + case Intrinsic::usub_with_overflow: + case Intrinsic::ssub_with_overflow: + BinOp = Instruction::Sub; + break; + case Intrinsic::umul_with_overflow: + case Intrinsic::smul_with_overflow: + BinOp = Instruction::Mul; + break; + default: + BBLV = ValueLatticeElement::getOverdefined(); + return true; + } + + Optional LHSRes = getRangeForOperand(0, II, BB); + Optional RHSRes = getRangeForOperand(1, II, BB); + if (!LHSRes.hasValue() || !RHSRes.hasValue()) + // More work to do before applying this transfer rule. + return false; + + ConstantRange LHSRange = LHSRes.getValue(); + ConstantRange RHSRange = RHSRes.getValue(); + BBLV = ValueLatticeElement::getRange(LHSRange.binaryOp(BinOp, RHSRange)); + return true; +} + static ValueLatticeElement getValueFromICmpCondition(Value *Val, ICmpInst *ICI, bool isTrueDest) { Value *LHS = ICI->getOperand(0); Index: llvm/test/Transforms/CorrelatedValuePropagation/overflow_predicate.ll =================================================================== --- llvm/test/Transforms/CorrelatedValuePropagation/overflow_predicate.ll +++ llvm/test/Transforms/CorrelatedValuePropagation/overflow_predicate.ll @@ -470,8 +470,7 @@ ; CHECK: split: ; CHECK-NEXT: [[C1:%.*]] = icmp ugt i8 [[VAL]], 100 ; CHECK-NEXT: store i1 [[C1]], i1* [[PC:%.*]] -; CHECK-NEXT: [[C2:%.*]] = icmp uge i8 [[VAL]], 100 -; CHECK-NEXT: ret i1 [[C2]] +; CHECK-NEXT: ret i1 true ; CHECK: trap: ; CHECK-NEXT: call void @llvm.trap() ; CHECK-NEXT: unreachable @@ -506,8 +505,7 @@ ; CHECK: split: ; CHECK-NEXT: [[C1:%.*]] = icmp sgt i8 [[VAL]], -28 ; CHECK-NEXT: store i1 [[C1]], i1* [[PC:%.*]] -; CHECK-NEXT: [[C2:%.*]] = icmp sge i8 [[VAL]], -28 -; CHECK-NEXT: ret i1 [[C2]] +; CHECK-NEXT: ret i1 true ; CHECK: trap: ; CHECK-NEXT: call void @llvm.trap() ; CHECK-NEXT: unreachable @@ -542,8 +540,7 @@ ; CHECK: split: ; CHECK-NEXT: [[C1:%.*]] = icmp ult i8 [[VAL]], -101 ; CHECK-NEXT: store i1 [[C1]], i1* [[PC:%.*]] -; CHECK-NEXT: [[C2:%.*]] = icmp ule i8 [[VAL]], -101 -; CHECK-NEXT: ret i1 [[C2]] +; CHECK-NEXT: ret i1 true ; CHECK: trap: ; CHECK-NEXT: call void @llvm.trap() ; CHECK-NEXT: unreachable @@ -578,8 +575,7 @@ ; CHECK: split: ; CHECK-NEXT: [[C1:%.*]] = icmp slt i8 [[VAL]], 27 ; CHECK-NEXT: store i1 [[C1]], i1* [[PC:%.*]] -; CHECK-NEXT: [[C2:%.*]] = icmp sle i8 [[VAL]], 27 -; CHECK-NEXT: ret i1 [[C2]] +; CHECK-NEXT: ret i1 true ; CHECK: trap: ; CHECK-NEXT: call void @llvm.trap() ; CHECK-NEXT: unreachable @@ -614,8 +610,7 @@ ; CHECK: split: ; CHECK-NEXT: [[C1:%.*]] = icmp ult i8 [[VAL]], -6 ; CHECK-NEXT: store i1 [[C1]], i1* [[PC:%.*]] -; CHECK-NEXT: [[C2:%.*]] = icmp ule i8 [[VAL]], -6 -; CHECK-NEXT: ret i1 [[C2]] +; CHECK-NEXT: ret i1 true ; CHECK: trap: ; CHECK-NEXT: call void @llvm.trap() ; CHECK-NEXT: unreachable @@ -650,8 +645,7 @@ ; CHECK: split: ; CHECK-NEXT: [[C1:%.*]] = icmp slt i8 [[VAL]], 120 ; CHECK-NEXT: store i1 [[C1]], i1* [[PC:%.*]] -; CHECK-NEXT: [[C2:%.*]] = icmp sle i8 [[VAL]], 120 -; CHECK-NEXT: ret i1 [[C2]] +; CHECK-NEXT: ret i1 true ; CHECK: trap: ; CHECK-NEXT: call void @llvm.trap() ; CHECK-NEXT: unreachable @@ -686,8 +680,7 @@ ; CHECK: split: ; CHECK-NEXT: [[C1:%.*]] = icmp sgt i8 [[VAL]], -120 ; CHECK-NEXT: store i1 [[C1]], i1* [[PC:%.*]] -; CHECK-NEXT: [[C2:%.*]] = icmp sge i8 [[VAL]], -120 -; CHECK-NEXT: ret i1 [[C2]] +; CHECK-NEXT: ret i1 true ; CHECK: trap: ; CHECK-NEXT: call void @llvm.trap() ; CHECK-NEXT: unreachable