-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[ValueTracking] Handle non-canonical operand order in isImpliedCondICmps
#85575
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8471,26 +8471,12 @@ isImpliedCondOperands(CmpInst::Predicate Pred, const Value *ALHS, | |
} | ||
} | ||
|
||
/// Return true if the operands of two compares (expanded as "L0 pred L1" and | ||
/// "R0 pred R1") match. IsSwappedOps is true when the operands match, but are | ||
/// swapped. | ||
static bool areMatchingOperands(const Value *L0, const Value *L1, const Value *R0, | ||
const Value *R1, bool &AreSwappedOps) { | ||
bool AreMatchingOps = (L0 == R0 && L1 == R1); | ||
AreSwappedOps = (L0 == R1 && L1 == R0); | ||
return AreMatchingOps || AreSwappedOps; | ||
} | ||
|
||
/// Return true if "icmp1 LPred X, Y" implies "icmp2 RPred X, Y" is true. | ||
/// Return false if "icmp1 LPred X, Y" implies "icmp2 RPred X, Y" is false. | ||
/// Otherwise, return std::nullopt if we can't infer anything. | ||
static std::optional<bool> | ||
isImpliedCondMatchingOperands(CmpInst::Predicate LPred, | ||
CmpInst::Predicate RPred, bool AreSwappedOps) { | ||
// Canonicalize the predicate as if the operands were not commuted. | ||
if (AreSwappedOps) | ||
RPred = ICmpInst::getSwappedPredicate(RPred); | ||
|
||
CmpInst::Predicate RPred) { | ||
if (CmpInst::isImpliedTrueByMatchingCmp(LPred, RPred)) | ||
return true; | ||
if (CmpInst::isImpliedFalseByMatchingCmp(LPred, RPred)) | ||
|
@@ -8532,39 +8518,41 @@ static std::optional<bool> isImpliedCondICmps(const ICmpInst *LHS, | |
CmpInst::Predicate LPred = | ||
LHSIsTrue ? LHS->getPredicate() : LHS->getInversePredicate(); | ||
|
||
// We can have non-canonical operands, so try to normalize any common operand | ||
// to L0/R0. | ||
if (L0 == R1) { | ||
std::swap(R0, R1); | ||
RPred = ICmpInst::getSwappedPredicate(RPred); | ||
} | ||
if (R0 == L1) { | ||
std::swap(L0, L1); | ||
LPred = ICmpInst::getSwappedPredicate(LPred); | ||
} | ||
if (L1 == R1) { | ||
// If we have L0 == R0 and L1 == R1, then make L1/R1 the constants. | ||
if (L0 != R0 || match(L0, m_ImmConstant())) { | ||
std::swap(L0, L1); | ||
LPred = ICmpInst::getSwappedPredicate(LPred); | ||
std::swap(R0, R1); | ||
RPred = ICmpInst::getSwappedPredicate(RPred); | ||
} | ||
} | ||
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. nit: Add a newline here? |
||
// Can we infer anything when the 0-operands match and the 1-operands are | ||
// constants (not necessarily matching)? | ||
const APInt *LC, *RC; | ||
if (L0 == R0 && match(L1, m_APInt(LC)) && match(R1, m_APInt(RC))) | ||
return isImpliedCondCommonOperandWithConstants(LPred, *LC, RPred, *RC); | ||
|
||
// Can we infer anything when the two compares have matching operands? | ||
bool AreSwappedOps; | ||
if (areMatchingOperands(L0, L1, R0, R1, AreSwappedOps)) | ||
return isImpliedCondMatchingOperands(LPred, RPred, AreSwappedOps); | ||
if (L0 == R0 && L1 == R1) | ||
return isImpliedCondMatchingOperands(LPred, RPred); | ||
|
||
// L0 = R0 = L1 + R1, L0 >=u L1 implies R0 >=u R1, L0 <u L1 implies R0 <u R1 | ||
if (ICmpInst::isUnsigned(LPred) && ICmpInst::isUnsigned(RPred)) { | ||
if (L0 == R1) { | ||
std::swap(R0, R1); | ||
RPred = ICmpInst::getSwappedPredicate(RPred); | ||
} | ||
if (L1 == R0) { | ||
std::swap(L0, L1); | ||
LPred = ICmpInst::getSwappedPredicate(LPred); | ||
} | ||
if (L1 == R1) { | ||
std::swap(L0, L1); | ||
LPred = ICmpInst::getSwappedPredicate(LPred); | ||
std::swap(R0, R1); | ||
RPred = ICmpInst::getSwappedPredicate(RPred); | ||
} | ||
if (L0 == R0 && | ||
(LPred == ICmpInst::ICMP_ULT || LPred == ICmpInst::ICMP_UGE) && | ||
(RPred == ICmpInst::ICMP_ULT || RPred == ICmpInst::ICMP_UGE) && | ||
match(L0, m_c_Add(m_Specific(L1), m_Specific(R1)))) | ||
return LPred == RPred; | ||
} | ||
if (L0 == R0 && | ||
(LPred == ICmpInst::ICMP_ULT || LPred == ICmpInst::ICMP_UGE) && | ||
(RPred == ICmpInst::ICMP_ULT || RPred == ICmpInst::ICMP_UGE) && | ||
match(L0, m_c_Add(m_Specific(L1), m_Specific(R1)))) | ||
return LPred == RPred; | ||
|
||
if (LPred == RPred) | ||
return isImpliedCondOperands(LPred, L0, L1, R0, R1, DL, Depth); | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
llvm/test/Transforms/InstCombine/icmp-select-implies-common-op.ll
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py | ||
; RUN: opt -S -passes=instcombine < %s | FileCheck %s | ||
|
||
define i1 @sgt_3_impliesF_eq_2(i8 %x, i8 %y) { | ||
; CHECK-LABEL: @sgt_3_impliesF_eq_2( | ||
; CHECK-NEXT: [[CMP:%.*]] = icmp slt i8 [[X:%.*]], 4 | ||
; CHECK-NEXT: [[CMP2:%.*]] = icmp eq i8 [[SEL:%.*]], [[X]] | ||
; CHECK-NEXT: [[CMP3:%.*]] = select i1 [[CMP]], i1 [[CMP2]], i1 false | ||
; CHECK-NEXT: ret i1 [[CMP3]] | ||
; | ||
%cmp = icmp sgt i8 %x, 3 | ||
%sel = select i1 %cmp, i8 2, i8 %y | ||
%cmp2 = icmp eq i8 %sel, %x | ||
ret i1 %cmp2 | ||
} | ||
|
||
define i1 @sgt_3_impliesT_sgt_2(i8 %x, i8 %y) { | ||
; CHECK-LABEL: @sgt_3_impliesT_sgt_2( | ||
; CHECK-NEXT: [[CMP:%.*]] = icmp slt i8 [[X:%.*]], 4 | ||
; CHECK-NEXT: [[CMP2:%.*]] = icmp sgt i8 [[SEL:%.*]], [[X]] | ||
; CHECK-NEXT: [[CMP3:%.*]] = select i1 [[CMP]], i1 [[CMP2]], i1 false | ||
; CHECK-NEXT: ret i1 [[CMP3]] | ||
; | ||
%cmp = icmp sgt i8 %x, 3 | ||
%sel = select i1 %cmp, i8 2, i8 %y | ||
%cmp2 = icmp sgt i8 %sel, %x | ||
ret i1 %cmp2 | ||
} | ||
|
||
define i1 @sgt_x_impliesF_eq_smin_todo(i8 %x, i8 %y, i8 %z) { | ||
; CHECK-LABEL: @sgt_x_impliesF_eq_smin_todo( | ||
; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i8 [[X:%.*]], [[Z:%.*]] | ||
; CHECK-NEXT: [[SEL:%.*]] = select i1 [[CMP]], i8 -128, i8 [[Y:%.*]] | ||
; CHECK-NEXT: [[CMP2:%.*]] = icmp eq i8 [[SEL]], [[X]] | ||
; CHECK-NEXT: ret i1 [[CMP2]] | ||
; | ||
%cmp = icmp sgt i8 %x, %z | ||
%sel = select i1 %cmp, i8 -128, i8 %y | ||
%cmp2 = icmp eq i8 %sel, %x | ||
ret i1 %cmp2 | ||
} | ||
|
||
define i1 @slt_x_impliesT_ne_smin_todo(i8 %x, i8 %y, i8 %z) { | ||
; CHECK-LABEL: @slt_x_impliesT_ne_smin_todo( | ||
; CHECK-NEXT: [[CMP:%.*]] = icmp slt i8 [[X:%.*]], [[Z:%.*]] | ||
; CHECK-NEXT: [[SEL:%.*]] = select i1 [[CMP]], i8 127, i8 [[Y:%.*]] | ||
; CHECK-NEXT: [[CMP2:%.*]] = icmp ne i8 [[SEL]], [[X]] | ||
; CHECK-NEXT: ret i1 [[CMP2]] | ||
; | ||
%cmp = icmp slt i8 %x, %z | ||
%sel = select i1 %cmp, i8 127, i8 %y | ||
%cmp2 = icmp ne i8 %x, %sel | ||
ret i1 %cmp2 | ||
} | ||
|
||
define i1 @ult_x_impliesT_eq_umax_todo(i8 %x, i8 %y, i8 %z) { | ||
; CHECK-LABEL: @ult_x_impliesT_eq_umax_todo( | ||
; CHECK-NEXT: [[CMP:%.*]] = icmp ugt i8 [[Z:%.*]], [[X:%.*]] | ||
; CHECK-NEXT: [[SEL:%.*]] = select i1 [[CMP]], i8 -1, i8 [[Y:%.*]] | ||
; CHECK-NEXT: [[CMP2:%.*]] = icmp ne i8 [[SEL]], [[X]] | ||
; CHECK-NEXT: ret i1 [[CMP2]] | ||
; | ||
%cmp = icmp ugt i8 %z, %x | ||
%sel = select i1 %cmp, i8 255, i8 %y | ||
%cmp2 = icmp ne i8 %sel, %x | ||
ret i1 %cmp2 | ||
} | ||
|
||
define i1 @ult_1_impliesF_eq_1(i8 %x, i8 %y) { | ||
; CHECK-LABEL: @ult_1_impliesF_eq_1( | ||
; CHECK-NEXT: [[CMP:%.*]] = icmp ne i8 [[SEL:%.*]], 0 | ||
; CHECK-NEXT: [[CMP2:%.*]] = icmp eq i8 [[X:%.*]], [[SEL]] | ||
; CHECK-NEXT: [[CMP3:%.*]] = select i1 [[CMP]], i1 [[CMP2]], i1 false | ||
; CHECK-NEXT: ret i1 [[CMP3]] | ||
; | ||
%cmp = icmp ult i8 %x, 1 | ||
%sel = select i1 %cmp, i8 1, i8 %y | ||
%cmp2 = icmp eq i8 %x, %sel | ||
ret i1 %cmp2 | ||
} | ||
|
||
define i1 @ugt_x_impliesF_eq_umin_todo(i8 %x, i8 %y, i8 %z) { | ||
; CHECK-LABEL: @ugt_x_impliesF_eq_umin_todo( | ||
; CHECK-NEXT: [[CMP:%.*]] = icmp ugt i8 [[Z:%.*]], [[X:%.*]] | ||
; CHECK-NEXT: [[SEL:%.*]] = select i1 [[CMP]], i8 0, i8 [[Y:%.*]] | ||
; CHECK-NEXT: [[CMP2:%.*]] = icmp eq i8 [[SEL]], [[X]] | ||
; CHECK-NEXT: ret i1 [[CMP2]] | ||
; | ||
%cmp = icmp ugt i8 %z, %x | ||
%sel = select i1 %cmp, i8 0, i8 %y | ||
%cmp2 = icmp eq i8 %x, %sel | ||
ret i1 %cmp2 | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 checking operand equality instead, as you did in the other patch? I'm thinking there is some cleanup to be done here: We have areMatchingOperands checking for swap, and then we also have it in the isUnsigned() code path below, and then this adds another case.
Or does that run afoul of the case where the constants are equal?
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.
Checking matching does work (didn't see any difference between the two). Ill update.
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 drop the swapping code below now?
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.
Checking.
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.
Yup with a little bit more work in the initial swaps.