Skip to content

Commit

Permalink
[InstCombine] Simplify with.overflow intrinsics with assumption infor…
Browse files Browse the repository at this point in the history
…mation
  • Loading branch information
dtcxzyw committed Mar 5, 2024
1 parent 96ab2a0 commit fc350f0
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 12 deletions.
27 changes: 27 additions & 0 deletions llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -818,6 +818,33 @@ InstCombinerImpl::foldIntrinsicWithOverflowCommon(IntrinsicInst *II) {
if (OptimizeOverflowCheck(WO->getBinaryOp(), WO->isSigned(), WO->getLHS(),
WO->getRHS(), *WO, OperationResult, OverflowResult))
return createOverflowTuple(WO, OperationResult, OverflowResult);

// See whether we can optimize the overflow check with assumption information.
for (User *U : WO->users()) {
if (!match(U, m_ExtractValue<1>(m_Value())))
continue;

for (auto &AssumeVH : AC.assumptionsFor(U)) {
if (!AssumeVH)
continue;
CallInst *I = cast<CallInst>(AssumeVH);
if (!match(I->getArgOperand(0), m_Not(m_Specific(U))))
continue;
if (!isValidAssumeForContext(I, II, &DT))
continue;
Value *Result =
Builder.CreateBinOp(WO->getBinaryOp(), WO->getLHS(), WO->getRHS());
if (auto *Inst = dyn_cast<Instruction>(Result)) {
if (WO->isSigned())
Inst->setHasNoSignedWrap();
else
Inst->setHasNoUnsignedWrap();
}
return createOverflowTuple(WO, Result,
ConstantInt::getFalse(U->getType()));
}
}

return nullptr;
}

Expand Down
16 changes: 4 additions & 12 deletions llvm/test/Transforms/InstCombine/overflow.ll
Original file line number Diff line number Diff line change
Expand Up @@ -173,12 +173,8 @@ if.end:

define i32 @uadd_no_overflow(i32 %a, i32 %b) {
; CHECK-LABEL: @uadd_no_overflow(
; CHECK-NEXT: [[VAL:%.*]] = tail call { i32, i1 } @llvm.uadd.with.overflow.i32(i32 [[A:%.*]], i32 [[B:%.*]])
; CHECK-NEXT: [[OV:%.*]] = extractvalue { i32, i1 } [[VAL]], 1
; CHECK-NEXT: [[NOWRAP:%.*]] = xor i1 [[OV]], true
; CHECK-NEXT: tail call void @llvm.assume(i1 [[NOWRAP]])
; CHECK-NEXT: [[RES:%.*]] = extractvalue { i32, i1 } [[VAL]], 0
; CHECK-NEXT: ret i32 [[RES]]
; CHECK-NEXT: [[TMP1:%.*]] = add nuw i32 [[A:%.*]], [[B:%.*]]
; CHECK-NEXT: ret i32 [[TMP1]]
;
%val = tail call { i32, i1 } @llvm.uadd.with.overflow.i32(i32 %a, i32 %b)
%ov = extractvalue { i32, i1 } %val, 1
Expand All @@ -190,12 +186,8 @@ define i32 @uadd_no_overflow(i32 %a, i32 %b) {

define i32 @smul_no_overflow(i32 %a, i32 %b) {
; CHECK-LABEL: @smul_no_overflow(
; CHECK-NEXT: [[VAL:%.*]] = tail call { i32, i1 } @llvm.smul.with.overflow.i32(i32 [[A:%.*]], i32 [[B:%.*]])
; CHECK-NEXT: [[OV:%.*]] = extractvalue { i32, i1 } [[VAL]], 1
; CHECK-NEXT: [[NOWRAP:%.*]] = xor i1 [[OV]], true
; CHECK-NEXT: tail call void @llvm.assume(i1 [[NOWRAP]])
; CHECK-NEXT: [[RES:%.*]] = extractvalue { i32, i1 } [[VAL]], 0
; CHECK-NEXT: ret i32 [[RES]]
; CHECK-NEXT: [[TMP1:%.*]] = mul nsw i32 [[A:%.*]], [[B:%.*]]
; CHECK-NEXT: ret i32 [[TMP1]]
;
%val = tail call { i32, i1 } @llvm.smul.with.overflow.i32(i32 %a, i32 %b)
%ov = extractvalue { i32, i1 } %val, 1
Expand Down

0 comments on commit fc350f0

Please sign in to comment.