-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
Gtneg mul divoptimizations #45604
Merged
sandreenko
merged 19 commits into
dotnet:master
from
alexcovington:gtneg-mul-divoptimizations
Jan 12, 2021
Merged
Gtneg mul divoptimizations #45604
Changes from 3 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
e487fe3
GT_NEG optimization for multiplication and division
alexcovington 7f04f55
Distribute negation over parenthetical multiplication or division.
alexcovington bbaf21f
Removing duplicate logic that I had put in accidently.
alexcovington bc91dcc
Check overflow and other conditions before performing morph
alexcovington 084a285
Merging change that removes src/coreclr/src directory
alexcovington b575ab6
Merge branch 'master' of https://github.com/dotnet/runtime into gtneg…
alexcovington 9727f8e
Resolved merge conflict and cleanup morph.cpp
alexcovington 7829712
Resolved merge conflict
alexcovington b4bf7e9
Formatting morph.cpp
alexcovington bd9a464
Returning tree after performing smpop again to fix flags
alexcovington 9136999
Formatting
alexcovington 2e72f94
Added check for optimizations, formatting.
alexcovington 6467520
Using gtIsActiveCSE_Candidate instead of fgGlobalMorph
alexcovington 4a7f9b5
Update src/coreclr/jit/morph.cpp
alexcovington cb71a90
Formatting
alexcovington fea92de
delete formatting changes.
8d2dc8e
Add a test.
46b8fff
Change the conditions a bit.
55d54e6
Better names for the tests.
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 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 |
---|---|---|
|
@@ -11843,7 +11843,22 @@ GenTree* Compiler::fgMorphSmpOp(GenTree* tree, MorphAddrContext* mac) | |
case GT_CAST: | ||
return fgMorphCast(tree); | ||
|
||
case GT_MUL: | ||
case GT_MUL: | ||
// -a * C => a * -C, where C is constant | ||
// MUL(NEG(a), C) => MUL(a, NEG(C)) | ||
if (op1->OperIs(GT_NEG) && !op1->IsCnsIntOrI() && op2->IsCnsIntOrI()) | ||
{ | ||
// tree: MUL | ||
// op1: a | ||
// op2: NEG | ||
// op2Child: C | ||
op1 = op1->AsOp()->gtOp1; // a | ||
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. I'd rewrite to be: tree->AsOp()->gtOp1 = op1->gtGetOp1();
DEBUG_DESTROY_NODE(op1);
op2->AsIntCon()->SetIconValue(-op2->AsIntCon()->IconValue());
return tree; |
||
op2->AsIntCon()->SetIconValue(-op2->AsIntCon()->IconValue()); // -C | ||
|
||
tree->AsOp()->gtOp1 = op1; | ||
tree->AsOp()->gtOp2 = op2; | ||
return tree; | ||
} | ||
|
||
#ifndef TARGET_64BIT | ||
if (typ == TYP_LONG) | ||
|
@@ -11991,6 +12006,22 @@ GenTree* Compiler::fgMorphSmpOp(GenTree* tree, MorphAddrContext* mac) | |
return fgMorphSmpOp(tree, mac); | ||
} | ||
|
||
// -a / C => a / -C, where C is constant | ||
// DIV(NEG(a), C) => DIV(a, NEG(C)) | ||
if (op1->OperIs(GT_NEG) && !op1->IsCnsIntOrI() && op2->IsCnsIntOrI()) | ||
{ | ||
// tree: DIV | ||
// op1: a | ||
// op2: NEG | ||
// op2Child: C | ||
op1 = op1->AsOp()->gtOp1; // a | ||
op2->AsIntCon()->SetIconValue(-op2->AsIntCon()->IconValue()); // -C | ||
|
||
tree->AsOp()->gtOp1 = op1; | ||
tree->AsOp()->gtOp2 = op2; | ||
return tree; | ||
} | ||
|
||
#ifndef TARGET_64BIT | ||
if (typ == TYP_LONG) | ||
{ | ||
|
@@ -12011,6 +12042,7 @@ GenTree* Compiler::fgMorphSmpOp(GenTree* tree, MorphAddrContext* mac) | |
{ | ||
op2 = gtFoldExprConst(op2); | ||
} | ||
|
||
break; | ||
|
||
case GT_UDIV: | ||
|
@@ -13367,6 +13399,7 @@ GenTree* Compiler::fgMorphSmpOp(GenTree* tree, MorphAddrContext* mac) | |
|
||
case GT_MUL: | ||
|
||
|
||
#ifndef TARGET_64BIT | ||
if (typ == TYP_LONG) | ||
{ | ||
|
@@ -13855,6 +13888,26 @@ GenTree* Compiler::fgMorphSmpOp(GenTree* tree, MorphAddrContext* mac) | |
return child; | ||
} | ||
|
||
// Distribute negation over simple multiplication/division expressions | ||
if (op1->OperIs(GT_MUL) || op1->OperIs(GT_DIV)) { | ||
if (!op1->AsOp()->gtOp1->IsCnsIntOrI() && op1->AsOp()->gtOp2->IsCnsIntOrI()) | ||
{ | ||
// -(a * C) => a * -C | ||
// -(a / C) => a / -C | ||
oper = op1->gtOper; | ||
tree->SetOper(op1->gtOper); | ||
GenTree* newOp1 = op1->AsOp()->gtOp1; // a | ||
GenTree* newOp2 = op1->AsOp()->gtOp2; // C | ||
newOp2->AsIntCon()->SetIconValue(-newOp2->AsIntCon()->IconValue()); | ||
|
||
// Only need to destroy op1 since op2 will be null already | ||
DEBUG_DESTROY_NODE(op1); | ||
|
||
tree->AsOp()->gtOp1 = op1 = newOp1; | ||
tree->AsOp()->gtOp2 = op2 = newOp2; | ||
} | ||
} | ||
|
||
/* Any constant cases should have been folded earlier */ | ||
noway_assert(!op1->OperIsConst() || !opts.OptEnabled(CLFLG_CONSTANTFOLD) || optValnumCSE_phase); | ||
break; | ||
|
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.
!op1->IsCnsIntOrI()
- I think you meantop1->gtOp1()->IsCnsIntOrI()
here.