Skip to content
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

Improve value numbering for casts #59841

Merged
merged 4 commits into from
Oct 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 15 additions & 1 deletion src/coreclr/jit/assertionprop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5425,7 +5425,14 @@ GenTree* Compiler::optExtractSideEffListFromConst(GenTree* tree)
{
// Do a sanity check to ensure persistent side effects aren't discarded and
// tell gtExtractSideEffList to ignore the root of the tree.
assert(!gtNodeHasSideEffects(tree, GTF_PERSISTENT_SIDE_EFFECTS));
// We are relying here on an invariant that VN will only fold non-throwing expressions.
const bool ignoreExceptions = true;
const bool ignoreCctors = false;
// We have to check "AsCall()->HasSideEffects()" here separately because "gtNodeHasSideEffects"
// also checks for side effects that arguments introduce (incosistently so, it otherwise only
// checks for the side effects the node itself has). TODO-Cleanup: change it to not do that?
assert(!gtNodeHasSideEffects(tree, GTF_PERSISTENT_SIDE_EFFECTS) ||
(tree->IsCall() && !tree->AsCall()->HasSideEffects(this, ignoreExceptions, ignoreCctors)));

// Exception side effects may be ignored because the root is known to be a constant
// (e.g. VN may evaluate a DIV/MOD node to a constant and the node may still
Expand Down Expand Up @@ -5625,6 +5632,13 @@ Compiler::fgWalkResult Compiler::optVNConstantPropCurStmt(BasicBlock* block, Sta
}
break;

case GT_CALL:
if (!tree->AsCall()->IsPure(this))
{
return WALK_CONTINUE;
}
break;

default:
// Unknown node, continue to walk.
return WALK_CONTINUE;
Expand Down
3 changes: 3 additions & 0 deletions src/coreclr/jit/compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -5494,6 +5494,9 @@ class Compiler
// Does value-numbering for a call. We interpret some helper calls.
void fgValueNumberCall(GenTreeCall* call);

// Does value-numbering for a helper representing a cast operation.
void fgValueNumberCastHelper(GenTreeCall* call);

// Does value-numbering for a helper "call" that has a VN function symbol "vnf".
void fgValueNumberHelperCallFunc(GenTreeCall* call, VNFunc vnf, ValueNumPair vnpExc);

Expand Down
18 changes: 14 additions & 4 deletions src/coreclr/jit/gentree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15924,7 +15924,7 @@ void Compiler::gtExtractSideEffList(GenTree* expr,
{
if (m_compiler->gtNodeHasSideEffects(node, m_flags))
{
m_sideEffects.Push(node);
PushSideEffects(node);
if (node->OperIsBlk() && !node->OperIsStoreBlk())
{
JITDUMP("Replace an unused OBJ/BLK node [%06d] with a NULLCHECK\n", dspTreeID(node));
Expand All @@ -15941,7 +15941,7 @@ void Compiler::gtExtractSideEffList(GenTree* expr,
// gtNodeHasSideEffects and make this check unconditionally.
if (node->OperIsAtomicOp())
{
m_sideEffects.Push(node);
PushSideEffects(node);
return Compiler::WALK_SKIP_SUBTREES;
}

Expand All @@ -15952,7 +15952,7 @@ void Compiler::gtExtractSideEffList(GenTree* expr,
(node->gtGetOp1()->TypeGet() == TYP_STRUCT))
{
JITDUMP("Keep the GT_ADDR and GT_IND together:\n");
m_sideEffects.Push(node);
PushSideEffects(node);
return Compiler::WALK_SKIP_SUBTREES;
}
}
Expand All @@ -15969,7 +15969,7 @@ void Compiler::gtExtractSideEffList(GenTree* expr,
// those need to be extracted as if they're side effects.
if (!UnmarkCSE(node))
{
m_sideEffects.Push(node);
PushSideEffects(node);
return Compiler::WALK_SKIP_SUBTREES;
}

Expand Down Expand Up @@ -16005,6 +16005,16 @@ void Compiler::gtExtractSideEffList(GenTree* expr,
return false;
}
}

void PushSideEffects(GenTree* node)
{
// The extracted side effect will no longer be an argument, so unmark it.
// This is safe to do because the side effects will be visited in pre-order,
// aborting as soon as any tree is extracted. Thus if an argument for a call
// is being extracted, it is guaranteed that the call itself will not be.
node->gtFlags &= ~GTF_LATE_ARG;
m_sideEffects.Push(node);
}
};

SideEffectExtractor extractor(this, flags);
Expand Down