-
Notifications
You must be signed in to change notification settings - Fork 13.5k
[InstCombine] Simplify and/or of icmp eq with op replacement #70335
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
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 |
---|---|---|
|
@@ -2025,6 +2025,52 @@ static Value *simplifyAndOrOfCmps(const SimplifyQuery &Q, Value *Op0, | |
return nullptr; | ||
} | ||
|
||
static Value *simplifyWithOpReplaced(Value *V, Value *Op, Value *RepOp, | ||
const SimplifyQuery &Q, | ||
bool AllowRefinement, | ||
SmallVectorImpl<Instruction *> *DropFlags, | ||
unsigned MaxRecurse); | ||
|
||
static Value *simplifyAndOrWithICmpEq(unsigned Opcode, Value *Op0, Value *Op1, | ||
const SimplifyQuery &Q, | ||
unsigned MaxRecurse) { | ||
assert((Opcode == Instruction::And || Opcode == Instruction::Or) && | ||
"Must be and/or"); | ||
ICmpInst::Predicate Pred; | ||
Value *A, *B; | ||
if (!match(Op0, m_ICmp(Pred, m_Value(A), m_Value(B))) || | ||
!ICmpInst::isEquality(Pred) || !MaxRecurse--) | ||
return nullptr; | ||
|
||
auto Simplify = [&](Value *Res) -> Value * { | ||
// and (icmp eq a, b), x implies (a==b) inside x. | ||
// or (icmp ne a, b), x implies (a==b) inside x. | ||
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. Do you get any benefit 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. If we had a way to say "simplify x under the assumption that a != b", I would expect that to be beneficial. But we can only do this for equalities right now, as in that case we can just replace operands. I think we need some generic way to say "evaluate this predicate under the assumption that v has range CR" -- some of @dtcxzyw's recent PRs go in that direction, but I feel like there must be a more generic solution to this, which integrates with existing range propagation logic, instead of reimplementing it in parts. This seems like something that LVI/CVP should be doing, but I don't see a good way to integrate it there. |
||
// If x simplifies to true/false, we can simplify the and/or. | ||
if (Pred == | ||
(Opcode == Instruction::And ? ICmpInst::ICMP_EQ : ICmpInst::ICMP_NE)) | ||
return simplifyBinOp(Opcode, Op0, Res, Q, MaxRecurse); | ||
// If we have and (icmp ne a, b), x and for a==b we can simplify x to false, | ||
// then we can drop the icmp, as x will already be false in the case where | ||
// the icmp is false. Similar for or and true. | ||
if (Res == ConstantExpr::getBinOpAbsorber(Opcode, Res->getType())) | ||
return Op1; | ||
return nullptr; | ||
}; | ||
|
||
// Increment MaxRecurse again, because simplifyWithOpReplaced() does its own | ||
// decrement. | ||
if (Value *Res = | ||
simplifyWithOpReplaced(Op1, A, B, Q, /* AllowRefinement */ true, | ||
/* DropFlags */ nullptr, MaxRecurse + 1)) | ||
return Simplify(Res); | ||
if (Value *Res = | ||
simplifyWithOpReplaced(Op1, B, A, Q, /* AllowRefinement */ true, | ||
/* DropFlags */ nullptr, MaxRecurse + 1)) | ||
return Simplify(Res); | ||
|
||
return nullptr; | ||
} | ||
|
||
/// Given a bitwise logic op, check if the operands are add/sub with a common | ||
/// source value and inverted constant (identity: C - X -> ~(X + ~C)). | ||
static Value *simplifyLogicOfAddSub(Value *Op0, Value *Op1, | ||
|
@@ -2159,6 +2205,13 @@ static Value *simplifyAndInst(Value *Op0, Value *Op1, const SimplifyQuery &Q, | |
isKnownToBeAPowerOfTwo(Op0, Q.DL, /*OrZero*/ true, 0, Q.AC, Q.CxtI, Q.DT)) | ||
return Constant::getNullValue(Op0->getType()); | ||
|
||
if (Value *V = | ||
simplifyAndOrWithICmpEq(Instruction::And, Op0, Op1, Q, MaxRecurse)) | ||
return V; | ||
if (Value *V = | ||
simplifyAndOrWithICmpEq(Instruction::And, Op1, Op0, Q, MaxRecurse)) | ||
return V; | ||
|
||
if (Value *V = simplifyAndOrOfCmps(Q, Op0, Op1, true)) | ||
return V; | ||
|
||
|
@@ -2435,6 +2488,13 @@ static Value *simplifyOrInst(Value *Op0, Value *Op1, const SimplifyQuery &Q, | |
match(Op0, m_LShr(m_Specific(X), m_Specific(Y)))) | ||
return Op1; | ||
|
||
if (Value *V = | ||
simplifyAndOrWithICmpEq(Instruction::Or, Op0, Op1, Q, MaxRecurse)) | ||
return V; | ||
if (Value *V = | ||
simplifyAndOrWithICmpEq(Instruction::Or, Op1, Op0, Q, MaxRecurse)) | ||
return V; | ||
|
||
if (Value *V = simplifyAndOrOfCmps(Q, Op0, Op1, false)) | ||
return V; | ||
|
||
|
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.
nit: Would have recursive check first.