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

JIT: Straighten out flow during early jump threading #104603

Merged
merged 3 commits into from
Jul 15, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/coreclr/jit/compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -6128,6 +6128,7 @@ class Compiler
#endif // FEATURE_EH_WINDOWS_X86

bool fgOptimizeUncondBranchToSimpleCond(BasicBlock* block, BasicBlock* target);
bool fgFoldSimpleCondByForwardSub(BasicBlock* block);

bool fgBlockEndFavorsTailDuplication(BasicBlock* block, unsigned lclNum);

Expand Down
141 changes: 137 additions & 4 deletions src/coreclr/jit/fgopt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1966,6 +1966,12 @@ bool Compiler::fgBlockIsGoodTailDuplicationCandidate(BasicBlock* target, unsigne
return false;
}

// No point duplicating this block if it would not remove (part of) the join.
if (target->TrueTargetIs(target) || target->FalseTargetIs(target))
{
return false;
}

Statement* const lastStmt = target->lastStmt();
Statement* const firstStmt = target->FirstNonPhiDef();

Expand Down Expand Up @@ -2265,6 +2271,108 @@ bool Compiler::fgOptimizeUncondBranchToSimpleCond(BasicBlock* block, BasicBlock*
return true;
}

//-------------------------------------------------------------
// fgFoldSimpleCondByForwardSub:
// Try to refine the flow of a block that may have just been tail duplicated
// or compacted.
//
// Arguments:
// block - block that was tail duplicated or compacted
//
// Returns Value:
// true if control flow was changed
//
bool Compiler::fgFoldSimpleCondByForwardSub(BasicBlock* block)
{
assert(block->KindIs(BBJ_COND));
GenTree* jtrue = block->lastStmt()->GetRootNode();
assert(jtrue->OperIs(GT_JTRUE));

GenTree* relop = jtrue->gtGetOp1();
if (!relop->OperIsCompare())
{
return false;
}

GenTree* op1 = relop->gtGetOp1();
GenTree* op2 = relop->gtGetOp2();

GenTree** lclUse;
GenTreeLclVarCommon* lcl;

if (op1->OperIs(GT_LCL_VAR) && op2->IsIntegralConst())
{
lclUse = &relop->AsOp()->gtOp1;
lcl = op1->AsLclVarCommon();
}
else if (op2->OperIs(GT_LCL_VAR) && op1->IsIntegralConst())
{
lclUse = &relop->AsOp()->gtOp2;
lcl = op2->AsLclVarCommon();
}
else
{
return false;
}

Statement* secondLastStmt = block->lastStmt()->GetPrevStmt();
if ((secondLastStmt == nullptr) || (secondLastStmt == block->lastStmt()))
{
return false;
}

GenTree* prevTree = secondLastStmt->GetRootNode();
if (!prevTree->OperIs(GT_STORE_LCL_VAR))
{
return false;
}

GenTreeLclVarCommon* store = prevTree->AsLclVarCommon();
if (store->GetLclNum() != lcl->GetLclNum())
{
return false;
}

if (!store->Data()->IsIntegralConst())
{
return false;
}

if (genActualType(store) != genActualType(store->Data()) || (genActualType(store) != genActualType(lcl)))
{
return false;
}

JITDUMP("Forward substituting local after jump threading. Before:\n");
DISPSTMT(block->lastStmt());

JITDUMP("\nAfter:\n");

LclVarDsc* varDsc = lvaGetDesc(lcl);
GenTree* newData = gtCloneExpr(store->Data());
if (varTypeIsSmall(varDsc) && fgCastNeeded(store->Data(), varDsc->TypeGet()))
{
newData = gtNewCastNode(TYP_INT, newData, false, varDsc->TypeGet());
newData = gtFoldExpr(newData);
}

*lclUse = newData;
DISPSTMT(block->lastStmt());

JITDUMP("\nNow trying to fold...\n");
jtrue->AsUnOp()->gtOp1 = gtFoldExpr(relop);
DISPSTMT(block->lastStmt());

Compiler::FoldResult result = fgFoldConditional(block);
if (result != Compiler::FoldResult::FOLD_DID_NOTHING)
{
assert(block->KindIs(BBJ_ALWAYS));
return true;
}

return false;
}

//-------------------------------------------------------------
// fgRemoveConditionalJump:
// Optimize a BBJ_COND block that unconditionally jumps to the same target
Expand Down Expand Up @@ -5176,10 +5284,35 @@ bool Compiler::fgUpdateFlowGraph(bool doTailDuplication /* = false */,
{
assert(block->KindIs(BBJ_COND));
assert(bNext == block->Next());
change = true;
modified = true;
bDest = block->GetTrueTarget();
bFalseDest = block->GetFalseTarget();
change = true;
modified = true;

if (fgFoldSimpleCondByForwardSub(block))
{
// It is likely another pred of the target now can
// similarly have its control flow straightened out.
// Try to compact it and repeat the optimization for
// it.
if (bDest->bbRefs == 1)
{
BasicBlock* otherPred = bDest->bbPreds->getSourceBlock();
JITDUMP("Trying to compact last pred " FMT_BB " of " FMT_BB " that we now bypass\n",
otherPred->bbNum, bDest->bbNum);
if (fgCanCompactBlock(otherPred))
{
fgCompactBlock(otherPred);
fgFoldSimpleCondByForwardSub(otherPred);
}
}

assert(block->KindIs(BBJ_ALWAYS));
bDest = block->GetTarget();
}
else
{
bDest = block->GetTrueTarget();
bFalseDest = block->GetFalseTarget();
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/jit/switchrecognition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ bool IsConstantTestCondBlock(const BasicBlock* block,
}

// We're looking for "X EQ/NE CNS" or "CNS EQ/NE X" pattern
if (op1->IsCnsIntOrI() ^ op2->IsCnsIntOrI())
if ((op1->IsCnsIntOrI() && !op1->IsIconHandle()) ^ (op2->IsCnsIntOrI() && !op2->IsIconHandle()))
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was already merged as a separate change in #104634.

{
// TODO: relax this to support any side-effect free expression
if (!op1->OperIs(GT_LCL_VAR) && !op2->OperIs(GT_LCL_VAR))
Expand Down
Loading