Skip to content

Commit

Permalink
[CodeGenPrepare][RISCV] Correct the MathUsed flag for shouldFormOverf…
Browse files Browse the repository at this point in the history
…lowOp

For add, if we match the constant edge case the add isn't used by
the compare so we shouldn't check for 2 users.

For sub, the compare is not a user of the sub so the math is
used if the sub has any users.

This regresses RISC-V which I will work on other patches for.

Reviewed By: RKSimon

Differential Revision: https://reviews.llvm.org/D146786
  • Loading branch information
topperc committed Mar 27, 2023
1 parent 9b957b1 commit 697a28b
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 35 deletions.
6 changes: 4 additions & 2 deletions llvm/lib/CodeGen/CodeGenPrepare.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1579,6 +1579,7 @@ static bool matchUAddWithOverflowConstantEdgeCases(CmpInst *Cmp,
/// intrinsic. Return true if any changes were made.
bool CodeGenPrepare::combineToUAddWithOverflow(CmpInst *Cmp,
ModifyDT &ModifiedDT) {
bool EdgeCase = false;
Value *A, *B;
BinaryOperator *Add;
if (!match(Cmp, m_UAddWithOverflow(m_Value(A), m_Value(B), m_BinOp(Add)))) {
Expand All @@ -1587,11 +1588,12 @@ bool CodeGenPrepare::combineToUAddWithOverflow(CmpInst *Cmp,
// Set A and B in case we match matchUAddWithOverflowConstantEdgeCases.
A = Add->getOperand(0);
B = Add->getOperand(1);
EdgeCase = true;
}

if (!TLI->shouldFormOverflowOp(ISD::UADDO,
TLI->getValueType(*DL, Add->getType()),
Add->hasNUsesOrMore(2)))
Add->hasNUsesOrMore(EdgeCase ? 1 : 2)))
return false;

// We don't want to move around uses of condition values this late, so we
Expand Down Expand Up @@ -1660,7 +1662,7 @@ bool CodeGenPrepare::combineToUSubWithOverflow(CmpInst *Cmp,

if (!TLI->shouldFormOverflowOp(ISD::USUBO,
TLI->getValueType(*DL, Sub->getType()),
Sub->hasNUsesOrMore(2)))
Sub->hasNUsesOrMore(1)))
return false;

if (!replaceMathCmpWithIntrinsic(Sub, Sub->getOperand(0), Sub->getOperand(1),
Expand Down
71 changes: 38 additions & 33 deletions llvm/test/CodeGen/RISCV/overflow-intrinsics.ll
Original file line number Diff line number Diff line change
Expand Up @@ -612,13 +612,12 @@ define i1 @uaddo_i64_increment_alt(i64 %x, ptr %p) {
; RV32-LABEL: uaddo_i64_increment_alt:
; RV32: # %bb.0:
; RV32-NEXT: addi a3, a0, 1
; RV32-NEXT: seqz a4, a3
; RV32-NEXT: add a4, a1, a4
; RV32-NEXT: sw a3, 0(a2)
; RV32-NEXT: and a0, a0, a1
; RV32-NEXT: addi a0, a0, 1
; RV32-NEXT: seqz a0, a3
; RV32-NEXT: add a1, a1, a0
; RV32-NEXT: or a0, a3, a1
; RV32-NEXT: seqz a0, a0
; RV32-NEXT: sw a4, 4(a2)
; RV32-NEXT: sw a3, 0(a2)
; RV32-NEXT: sw a1, 4(a2)
; RV32-NEXT: ret
;
; RV64-LABEL: uaddo_i64_increment_alt:
Expand All @@ -638,15 +637,13 @@ define i1 @uaddo_i64_increment_alt(i64 %x, ptr %p) {
define i1 @uaddo_i64_increment_alt_dom(i64 %x, ptr %p) {
; RV32-LABEL: uaddo_i64_increment_alt_dom:
; RV32: # %bb.0:
; RV32-NEXT: and a3, a0, a1
; RV32-NEXT: addi a3, a3, 1
; RV32-NEXT: seqz a3, a3
; RV32-NEXT: addi a0, a0, 1
; RV32-NEXT: seqz a4, a0
; RV32-NEXT: add a1, a1, a4
; RV32-NEXT: sw a0, 0(a2)
; RV32-NEXT: addi a3, a0, 1
; RV32-NEXT: seqz a0, a3
; RV32-NEXT: add a1, a1, a0
; RV32-NEXT: or a0, a3, a1
; RV32-NEXT: seqz a0, a0
; RV32-NEXT: sw a3, 0(a2)
; RV32-NEXT: sw a1, 4(a2)
; RV32-NEXT: mv a0, a3
; RV32-NEXT: ret
;
; RV64-LABEL: uaddo_i64_increment_alt_dom:
Expand All @@ -666,19 +663,24 @@ define i1 @uaddo_i64_increment_alt_dom(i64 %x, ptr %p) {
define i1 @uaddo_i64_decrement_alt(i64 %x, ptr %p) {
; RV32-LABEL: uaddo_i64_decrement_alt:
; RV32: # %bb.0:
; RV32-NEXT: seqz a3, a0
; RV32-NEXT: sub a3, a1, a3
; RV32-NEXT: addi a4, a0, -1
; RV32-NEXT: sw a4, 0(a2)
; RV32-NEXT: or a0, a0, a1
; RV32-NEXT: snez a0, a0
; RV32-NEXT: sw a3, 4(a2)
; RV32-NEXT: addi a3, a0, -1
; RV32-NEXT: seqz a4, a0
; RV32-NEXT: sub a4, a1, a4
; RV32-NEXT: bnez a0, .LBB18_2
; RV32-NEXT: # %bb.1:
; RV32-NEXT: sltu a0, a4, a1
; RV32-NEXT: j .LBB18_3
; RV32-NEXT: .LBB18_2:
; RV32-NEXT: sltu a0, a3, a0
; RV32-NEXT: .LBB18_3:
; RV32-NEXT: sw a3, 0(a2)
; RV32-NEXT: sw a4, 4(a2)
; RV32-NEXT: ret
;
; RV64-LABEL: uaddo_i64_decrement_alt:
; RV64: # %bb.0:
; RV64-NEXT: addi a2, a0, -1
; RV64-NEXT: snez a0, a0
; RV64-NEXT: sltu a0, a2, a0
; RV64-NEXT: sd a2, 0(a1)
; RV64-NEXT: ret
%a = add i64 %x, -1
Expand All @@ -692,22 +694,25 @@ define i1 @uaddo_i64_decrement_alt(i64 %x, ptr %p) {
define i1 @uaddo_i64_decrement_alt_dom(i64 %x, ptr %p) {
; RV32-LABEL: uaddo_i64_decrement_alt_dom:
; RV32: # %bb.0:
; RV32-NEXT: or a3, a0, a1
; RV32-NEXT: snez a3, a3
; RV32-NEXT: addi a3, a0, -1
; RV32-NEXT: seqz a4, a0
; RV32-NEXT: sub a1, a1, a4
; RV32-NEXT: addi a0, a0, -1
; RV32-NEXT: sw a0, 0(a2)
; RV32-NEXT: sw a1, 4(a2)
; RV32-NEXT: mv a0, a3
; RV32-NEXT: sub a4, a1, a4
; RV32-NEXT: bnez a0, .LBB19_2
; RV32-NEXT: # %bb.1:
; RV32-NEXT: sltu a0, a4, a1
; RV32-NEXT: j .LBB19_3
; RV32-NEXT: .LBB19_2:
; RV32-NEXT: sltu a0, a3, a0
; RV32-NEXT: .LBB19_3:
; RV32-NEXT: sw a3, 0(a2)
; RV32-NEXT: sw a4, 4(a2)
; RV32-NEXT: ret
;
; RV64-LABEL: uaddo_i64_decrement_alt_dom:
; RV64: # %bb.0:
; RV64-NEXT: snez a2, a0
; RV64-NEXT: addi a0, a0, -1
; RV64-NEXT: sd a0, 0(a1)
; RV64-NEXT: mv a0, a2
; RV64-NEXT: addi a2, a0, -1
; RV64-NEXT: sltu a0, a2, a0
; RV64-NEXT: sd a2, 0(a1)
; RV64-NEXT: ret
%ov = icmp ne i64 %x, 0
%a = add i64 %x, -1
Expand Down

0 comments on commit 697a28b

Please sign in to comment.