-
Notifications
You must be signed in to change notification settings - Fork 14.4k
Match the inverse of m_AddOverflow #147215
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7821,7 +7821,7 @@ Instruction *InstCombinerImpl::visitICmpInst(ICmpInst &I) { | |
isa<IntegerType>(X->getType())) { | ||
Value *Result; | ||
Constant *Overflow; | ||
// m_UAddWithOverflow can match patterns that do not include an explicit | ||
// m_UAddWithOverflow can match patterns that do not include an explicit | ||
// "add" instruction, so check the opcode of the matched op. | ||
if (AddI->getOpcode() == Instruction::Add && | ||
OptimizeOverflowCheck(Instruction::Add, /*Signed*/ false, X, Y, *AddI, | ||
|
@@ -7832,6 +7832,25 @@ Instruction *InstCombinerImpl::visitICmpInst(ICmpInst &I) { | |
} | ||
} | ||
|
||
if (match(&I, m_UAddWithOverflowInv(m_Value(X), m_Value(Y), | ||
m_Instruction(AddI))) && | ||
isa<IntegerType>(X->getType())) { | ||
Value *Result; | ||
Constant *Overflow; | ||
// m_UAddWithOverflowInv can match patterns that do not include an | ||
// explicit "add" instruction, so check the opcode of the matched op. | ||
if (AddI->getOpcode() == Instruction::Add && | ||
OptimizeOverflowCheck(Instruction::Add, /*Signed*/ false, X, Y, *AddI, | ||
Result, Overflow)) { | ||
Overflow = Overflow->isNullValue() | ||
? ConstantInt::getTrue(Overflow->getType()) | ||
: ConstantInt::getFalse(Overflow->getType()); | ||
replaceInstUsesWith(*AddI, Result); | ||
eraseInstFromFunction(*AddI); | ||
return replaceInstUsesWith(I, Overflow); | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This PR needs to be split into smaller parts. You can introduce the matcher and use it in one place with adequate test coverage. For example, you are adding code to InstCombine here, but there is not a single changed InstCombine test. |
||
|
||
// (zext X) * (zext Y) --> llvm.umul.with.overflow. | ||
if (match(Op0, m_NUWMul(m_ZExt(m_Value(X)), m_ZExt(m_Value(Y)))) && | ||
match(Op1, m_APInt(C))) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -664,8 +664,9 @@ define <4 x i32> @cmp_sge_not_with_vec(<4 x i32> %a, <4 x i32> %b) { | |
define i64 @cmp_uge_not(i64 %a, i64 %b) { | ||
; CHECK-LABEL: cmp_uge_not: | ||
; CHECK: # %bb.0: | ||
; CHECK-NEXT: notq %rsi | ||
; CHECK-NEXT: xorl %eax, %eax | ||
; CHECK-NEXT: cmpq %rdi, %rsi | ||
; CHECK-NEXT: addq %rdi, %rsi | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Regression. |
||
; CHECK-NEXT: adcq $-1, %rax | ||
; CHECK-NEXT: retq | ||
%na = xor i64 %a, -1 | ||
|
@@ -679,8 +680,8 @@ define i64 @cmp_uge_not_with_constant(i64 %a) { | |
; CHECK-LABEL: cmp_uge_not_with_constant: | ||
; CHECK: # %bb.0: | ||
; CHECK-NEXT: xorl %eax, %eax | ||
; CHECK-NEXT: cmpq $-42, %rdi | ||
; CHECK-NEXT: sbbq %rax, %rax | ||
; CHECK-NEXT: addq $42, %rdi | ||
; CHECK-NEXT: adcq $-1, %rax | ||
; CHECK-NEXT: retq | ||
%na = xor i64 %a, -1 | ||
%c = icmp uge i64 %na, 42 | ||
|
@@ -850,8 +851,9 @@ define <4 x i32> @cmp_ult_not_with_vec(<4 x i32> %a, <4 x i32> %b) { | |
define i64 @cmp_ule_not(i64 %a, i64 %b) { | ||
; CHECK-LABEL: cmp_ule_not: | ||
; CHECK: # %bb.0: | ||
; CHECK-NEXT: notq %rdi | ||
; CHECK-NEXT: xorl %eax, %eax | ||
; CHECK-NEXT: cmpq %rsi, %rdi | ||
; CHECK-NEXT: addq %rsi, %rdi | ||
; CHECK-NEXT: adcq $-1, %rax | ||
; CHECK-NEXT: retq | ||
%na = xor i64 %a, -1 | ||
|
@@ -983,8 +985,9 @@ define <4 x i32> @cmp_ne_not_with_vec(<4 x i32> %a, <4 x i32> %b) { | |
define i64 @cmp_uge_not_commute(i64 %b, i64 %a) { | ||
; CHECK-LABEL: cmp_uge_not_commute: | ||
; CHECK: # %bb.0: | ||
; CHECK-NEXT: notq %rdi | ||
; CHECK-NEXT: xorl %eax, %eax | ||
; CHECK-NEXT: cmpq %rsi, %rdi | ||
; CHECK-NEXT: addq %rsi, %rdi | ||
; CHECK-NEXT: adcq $-1, %rax | ||
; CHECK-NEXT: retq | ||
%na = xor i64 %a, -1 | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we handle this by adding an Invert template parameter to UAddWithOverflow which just inverts the Pred at the start and otherwise shares the code?