Skip to content

Optimize binary operators with equal children even if side effect #7460

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

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Prev Previous commit
Next Next commit
Update
  • Loading branch information
xuruiyang2002 committed Apr 7, 2025
commit f7b9d49e67ee1d8fb3bd93c7e339cea0321c91f8
19 changes: 8 additions & 11 deletions src/passes/OptimizeInstructions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -856,14 +856,11 @@ struct OptimizeInstructions
if (auto* ret = replaceZeroBitsWithZero(curr)) {
return replaceCurrent(ret);
}
// finally, try more expensive operations on the curr in
// the case that they have no side effects
if (!effects(curr->left).hasSideEffects()) {
if (ExpressionAnalyzer::equal(curr->left, curr->right)) {
if (auto* ret = optimizeBinaryWithEqualEffectlessChildren(curr)) {
return replaceCurrent(ret);
}
}
// finally, try more expensive operations on the curr
// regardless of whether they have side effects or not.
if (areConsecutiveInputsEqual(curr->left, curr->right)) {
if (auto* ret = optimizeBinaryWithEqualChildren(curr)) {
return replaceCurrent(ret);
}

if (auto* ret = deduplicateBinary(curr)) {
Expand Down Expand Up @@ -5177,9 +5174,9 @@ struct OptimizeInstructions
return nullptr;
}

// given a binary expression with equal children and no side effects in
// either, we can fold various things
Expression* optimizeBinaryWithEqualEffectlessChildren(Binary* binary) {
// given a binary expression with equal children, we can fold various things
// regardless of side effects.
Expression* optimizeBinaryWithEqualChildren(Binary* binary) {
// TODO add: perhaps worth doing 2*x if x is quite large?
switch (binary->op) {
case SubInt32:
Expand Down