-
Notifications
You must be signed in to change notification settings - Fork 14.4k
[InstCombine] fold (Binop phi(a, b) phi(b, a)) -> (Binop a, b) while Binop is commutative. #75765
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 9 commits
7c3ffb0
5f0eced
0034323
64c39fa
acb9f6c
f749206
038aae2
4d11a1e
03d6d95
f7356f6
9a4c93d
0bbb932
3d5d863
ea041eb
c9cba55
c71d176
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 |
---|---|---|
|
@@ -1096,6 +1096,50 @@ Value *InstCombinerImpl::foldUsingDistributiveLaws(BinaryOperator &I) { | |
return SimplifySelectsFeedingBinaryOp(I, LHS, RHS); | ||
} | ||
|
||
bool InstCombinerImpl::matchSymmetricPhiNodesPair(PHINode *LHS, PHINode *RHS) { | ||
|
||
if (LHS->getNumIncomingValues() != 2 || RHS->getNumIncomingValues() != 2) | ||
nikic marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return false; | ||
|
||
BasicBlock *B0 = LHS->getIncomingBlock(0); | ||
BasicBlock *B1 = LHS->getIncomingBlock(1); | ||
|
||
bool RHSContainB0 = RHS->getBasicBlockIndex(B0) != -1; | ||
bool RHSContainB1 = RHS->getBasicBlockIndex(B1) != -1; | ||
|
||
if (!RHSContainB0 || !RHSContainB1) | ||
nikic marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return false; | ||
|
||
Value *N1 = LHS->getIncomingValueForBlock(B0); | ||
Value *N2 = LHS->getIncomingValueForBlock(B1); | ||
Value *N3 = RHS->getIncomingValueForBlock(B0); | ||
Value *N4 = RHS->getIncomingValueForBlock(B1); | ||
|
||
return N1 == N4 && N2 == N3; | ||
} | ||
|
||
Value *InstCombinerImpl::SimplifyPhiCommutativeBinaryOp(BinaryOperator &I, | ||
Value *Op0, | ||
Value *Op1) { | ||
assert(I.isCommutative() && "Instruction should be commutative"); | ||
|
||
PHINode *LHS = dyn_cast<PHINode>(Op0); | ||
PHINode *RHS = dyn_cast<PHINode>(Op1); | ||
|
||
if (!LHS || !RHS) | ||
return nullptr; | ||
|
||
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 need to restrict to 2 incoming values per phi here? matchSymmetricPhiNodesPair could return true for phis with 10^10 incoming values. 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. Could you explain more ? I think current |
||
if (matchSymmetricPhiNodesPair(LHS, RHS)) { | ||
Value *BI = Builder.CreateBinOp(I.getOpcode(), LHS->getIncomingValue(0), | ||
LHS->getIncomingValue(1)); | ||
if (auto *BO = dyn_cast<BinaryOperator>(BI)) | ||
BO->copyIRFlags(&I); | ||
dtcxzyw marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return BI; | ||
} | ||
|
||
return nullptr; | ||
} | ||
|
||
Value *InstCombinerImpl::SimplifySelectsFeedingBinaryOp(BinaryOperator &I, | ||
Value *LHS, | ||
Value *RHS) { | ||
|
Uh oh!
There was an error while loading. Please reload this page.