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

Move ASG rationalization to just after global morph #85494

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
fa1dd73
If Conversion
SingleAccretion Apr 23, 2023
a3f9240
SSA descriptor
SingleAccretion Apr 26, 2023
d7576ca
VN-based dead store removal
SingleAccretion Apr 26, 2023
b294d7a
Range Check
SingleAccretion Apr 26, 2023
d1b6555
Global assertion prop
SingleAccretion Apr 26, 2023
a4cf8a8
CSE
SingleAccretion Apr 26, 2023
c15eac7
Redundant branch opts
SingleAccretion Apr 26, 2023
c0df763
Copy propagation
SingleAccretion Apr 26, 2023
138362a
Loop hoisting
SingleAccretion Apr 26, 2023
95f5575
Value numbering
SingleAccretion Apr 26, 2023
6d5ef64
Early prop
SingleAccretion Apr 26, 2023
aff16fe
SSA
SingleAccretion Apr 26, 2023
cbfffaf
Delete GTF_IND_ASG_LHS
SingleAccretion Apr 27, 2023
0bfb076
Liveness
SingleAccretion Apr 27, 2023
53a824e
Redundant zero inits
SingleAccretion Apr 27, 2023
a64f4aa
Move assignment rationalization to before the opt phases
SingleAccretion Apr 26, 2023
3ef97e4
Ref counting
SingleAccretion Apr 27, 2023
3cbdc7b
Cast morph opt
SingleAccretion Apr 27, 2023
1d53a25
Optimized STOREIND(LCL_ADDR, ...)
SingleAccretion Apr 28, 2023
4fb67b6
Move assignment rationalization to before loop opts
SingleAccretion Apr 27, 2023
8e7fbef
MD array morphing
SingleAccretion Apr 27, 2023
32b8f62
Loop unrolling
SingleAccretion Apr 27, 2023
adcf9d6
Loop cloning
SingleAccretion Apr 27, 2023
1cfe88c
Loop recognition
SingleAccretion Apr 27, 2023
691a6f1
Delete code from fgRationalizeAssignment
SingleAccretion Apr 27, 2023
1aa7ffd
GS security
SingleAccretion Apr 27, 2023
bab6b66
Fix formatting
SingleAccretion May 2, 2023
29311f1
Merge branch 'main' of https://github.com/dotnet/runtime into Op-Req-…
SingleAccretion May 3, 2023
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
21 changes: 10 additions & 11 deletions src/coreclr/jit/assertionprop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2212,7 +2212,7 @@ AssertionIndex Compiler::optAssertionGenPhiDefn(GenTree* tree)

// Try to find if all phi arguments are known to be non-null.
bool isNonNull = true;
for (GenTreePhi::Use& use : tree->AsOp()->gtGetOp2()->AsPhi()->Uses())
for (GenTreePhi::Use& use : tree->AsLclVar()->Data()->AsPhi()->Uses())
{
if (!vnStore->IsKnownNonNull(use.GetNode()->gtVNPair.GetConservative()))
{
Expand Down Expand Up @@ -2269,14 +2269,16 @@ void Compiler::optAssertionGen(GenTree* tree)
{
assertionInfo = optCreateAssertion(tree->AsOp()->gtOp1, tree->AsOp()->gtOp2, OAK_EQUAL);
}
else
{
assertionInfo = optAssertionGenPhiDefn(tree);
}
break;

case GT_STORE_LCL_VAR:
assertionInfo = optAssertionGenPhiDefn(tree);
break;

case GT_BLK:
case GT_IND:
case GT_STOREIND:
case GT_STORE_BLK:
// R-value indirections create non-null assertions, but not all indirections are R-values.
// Those under ADDR nodes or on the LHS of ASGs are "locations", and will not end up
// dereferencing their operands. We cannot reliably detect them here, however, and so
Expand Down Expand Up @@ -4739,6 +4741,8 @@ GenTree* Compiler::optAssertionProp(ASSERT_VALARG_TP assertions, GenTree* tree,

case GT_BLK:
case GT_IND:
case GT_STOREIND:
case GT_STORE_BLK:
case GT_NULLCHECK:
case GT_STORE_DYN_BLK:
return optAssertionProp_Ind(assertions, tree, stmt);
Expand Down Expand Up @@ -5720,7 +5724,7 @@ Compiler::fgWalkResult Compiler::optVNConstantPropCurStmt(BasicBlock* block, Sta
case GT_IND:
{
const ValueNum vn = tree->GetVN(VNK_Conservative);
if ((tree->gtFlags & GTF_IND_ASG_LHS) || (vnStore->VNNormalValue(vn) != vn))
if (vnStore->VNNormalValue(vn) != vn)
{
return WALK_CONTINUE;
}
Expand All @@ -5740,11 +5744,6 @@ Compiler::fgWalkResult Compiler::optVNConstantPropCurStmt(BasicBlock* block, Sta

case GT_LCL_VAR:
case GT_LCL_FLD:
// Make sure the local variable is an R-value.
if ((tree->gtFlags & (GTF_VAR_USEASG | GTF_VAR_DEF | GTF_DONT_CSE)) != GTF_EMPTY)
{
return WALK_CONTINUE;
}
// Let's not conflict with CSE (to save the movw/movt).
if (lclNumIsCSE(tree->AsLclVarCommon()->GetLclNum()))
{
Expand Down
8 changes: 2 additions & 6 deletions src/coreclr/jit/compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4797,6 +4797,8 @@ void Compiler::compCompile(void** methodCodePtr, uint32_t* methodCodeSize, JitFl
};
DoPhase(this, PHASE_MORPH_GLOBAL, morphGlobalPhase);

DoPhase(this, PHASE_RATIONALIZE_ASSIGNMENTS, &Compiler::fgRationalizeAssignments);

// GS security checks for unsafe buffers
//
DoPhase(this, PHASE_GS_COOKIE, &Compiler::gsPhase);
Expand Down Expand Up @@ -5033,8 +5035,6 @@ void Compiler::compCompile(void** methodCodePtr, uint32_t* methodCodeSize, JitFl
optLoopTableValid = false;
optLoopsRequirePreHeaders = false;

DoPhase(this, PHASE_RATIONALIZE_ASSIGNMENTS, &Compiler::fgRationalizeAssignments);

#ifdef DEBUG
DoPhase(this, PHASE_STRESS_SPLIT_TREE, &Compiler::StressSplitTree);
#endif
Expand Down Expand Up @@ -9744,10 +9744,6 @@ void cTreeFlags(Compiler* comp, GenTree* tree)
{
chars += printf("[IND_REQ_ADDR_IN_REG]");
}
if (tree->gtFlags & GTF_IND_ASG_LHS)
{
chars += printf("[IND_ASG_LHS]");
}
if (tree->gtFlags & GTF_IND_UNALIGNED)
{
chars += printf("[IND_UNALIGNED]");
Expand Down
25 changes: 14 additions & 11 deletions src/coreclr/jit/compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,9 @@ class LclSsaVarDsc
// SsaBuilder and changed to fgFirstBB during value numbering. It would be useful to
// investigate and perhaps eliminate this rather unexpected behavior.
BasicBlock* m_block = nullptr;
// The GT_ASG node that generates the definition, or nullptr for definitions
// The store node that generates the definition, or nullptr for definitions
// of uninitialized variables.
GenTreeOp* m_asg = nullptr;
GenTreeLclVarCommon* m_defNode = nullptr;
// The SSA number associated with the previous definition for partial (GTF_USEASG) defs.
unsigned m_useDefSsaNum = SsaConfig::RESERVED_SSA_NUM;
// Number of uses of this SSA def (may be an over-estimate).
Expand All @@ -238,9 +238,9 @@ class LclSsaVarDsc
{
}

LclSsaVarDsc(BasicBlock* block, GenTreeOp* asg) : m_block(block), m_asg(asg)
LclSsaVarDsc(BasicBlock* block, GenTreeLclVarCommon* defNode) : m_block(block)
{
assert((asg == nullptr) || asg->OperIs(GT_ASG));
SetAssignment(defNode);
}

BasicBlock* GetBlock() const
Expand All @@ -253,15 +253,17 @@ class LclSsaVarDsc
m_block = block;
}

GenTreeOp* GetAssignment() const
// TODO-ASG: rename to "GetDefNode".
GenTreeLclVarCommon* GetAssignment() const
{
return m_asg;
return m_defNode;
}

void SetAssignment(GenTreeOp* asg)
// TODO-ASG: rename to "SetDefNode".
void SetAssignment(GenTreeLclVarCommon* defNode)
{
assert((asg == nullptr) || asg->OperIs(GT_ASG));
m_asg = asg;
assert((defNode == nullptr) || defNode->OperIsLocalStore());
m_defNode = defNode;
}

unsigned GetUseDefSsaNum() const
Expand Down Expand Up @@ -5095,7 +5097,7 @@ class Compiler
// assignment.)
void fgValueNumberTree(GenTree* tree);

void fgValueNumberAssignment(GenTreeOp* tree);
void fgValueNumberStore(GenTree* tree);

void fgValueNumberSsaVarDef(GenTreeLclVarCommon* lcl);

Expand Down Expand Up @@ -5986,8 +5988,9 @@ class Compiler
private:
GenTree* fgMorphSmpOp(GenTree* tree, MorphAddrContext* mac, bool* optAssertionPropDone = nullptr);
void fgTryReplaceStructLocalWithField(GenTree* tree);
GenTree* fgOptimizeIndir(GenTreeIndir* indir);
GenTree* fgOptimizeCast(GenTreeCast* cast);
GenTree* fgOptimizeCastOnAssignment(GenTreeOp* asg);
GenTree* fgOptimizeCastOnStore(GenTree* store);
GenTree* fgOptimizeBitCast(GenTreeUnOp* bitCast);
GenTree* fgOptimizeEqualityComparisonWithConst(GenTreeOp* cmp);
GenTree* fgOptimizeRelationalComparisonWithConst(GenTreeOp* cmp);
Expand Down
41 changes: 11 additions & 30 deletions src/coreclr/jit/compiler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3195,28 +3195,15 @@ inline void Compiler::LoopDsc::VERIFY_lpIterTree() const
#ifdef DEBUG
assert(lpFlags & LPFLG_ITER);

// iterTree should be "lcl ASG lcl <op> const"
// iterTree should be "lcl = lcl <op> const"

assert(lpIterTree->OperIs(GT_ASG));
assert(lpIterTree->OperIs(GT_STORE_LCL_VAR));

const GenTree* lhs = lpIterTree->AsOp()->gtOp1;
const GenTree* rhs = lpIterTree->AsOp()->gtOp2;
assert(lhs->OperGet() == GT_LCL_VAR);

switch (rhs->gtOper)
{
case GT_ADD:
case GT_SUB:
case GT_MUL:
case GT_RSH:
case GT_LSH:
break;
default:
assert(!"Unknown operator for loop increment");
}
assert(rhs->AsOp()->gtOp1->OperGet() == GT_LCL_VAR);
assert(rhs->AsOp()->gtOp1->AsLclVarCommon()->GetLclNum() == lhs->AsLclVarCommon()->GetLclNum());
assert(rhs->AsOp()->gtOp2->OperGet() == GT_CNS_INT);
const GenTree* value = lpIterTree->AsLclVar()->Data();
assert(value->OperIs(GT_ADD, GT_SUB, GT_MUL, GT_RSH, GT_LSH));
assert(value->AsOp()->gtOp1->OperGet() == GT_LCL_VAR);
assert(value->AsOp()->gtOp1->AsLclVar()->GetLclNum() == lpIterTree->AsLclVar()->GetLclNum());
assert(value->AsOp()->gtOp2->OperGet() == GT_CNS_INT);
#endif
}

Expand All @@ -3225,25 +3212,24 @@ inline void Compiler::LoopDsc::VERIFY_lpIterTree() const
inline unsigned Compiler::LoopDsc::lpIterVar() const
{
VERIFY_lpIterTree();
return lpIterTree->AsOp()->gtOp1->AsLclVarCommon()->GetLclNum();
return lpIterTree->AsLclVar()->GetLclNum();
}

//-----------------------------------------------------------------------------

inline int Compiler::LoopDsc::lpIterConst() const
{
VERIFY_lpIterTree();
GenTree* rhs = lpIterTree->AsOp()->gtOp2;
return (int)rhs->AsOp()->gtOp2->AsIntCon()->gtIconVal;
GenTree* value = lpIterTree->AsLclVar()->Data();
return (int)value->AsOp()->gtOp2->AsIntCon()->gtIconVal;
}

//-----------------------------------------------------------------------------

inline genTreeOps Compiler::LoopDsc::lpIterOper() const
{
VERIFY_lpIterTree();
GenTree* rhs = lpIterTree->AsOp()->gtOp2;
return rhs->OperGet();
return lpIterTree->AsLclVar()->Data()->OperGet();
}

inline var_types Compiler::LoopDsc::lpIterOperType() const
Expand All @@ -3253,11 +3239,6 @@ inline var_types Compiler::LoopDsc::lpIterOperType() const
var_types type = lpIterTree->TypeGet();
assert(genActualType(type) == TYP_INT);

if ((lpIterTree->gtFlags & GTF_UNSIGNED) && type == TYP_INT)
{
type = TYP_UINT;
}

return type;
}

Expand Down
14 changes: 5 additions & 9 deletions src/coreclr/jit/copyprop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,11 +192,8 @@ bool Compiler::optCopyProp(
continue;
}

// Do not copy propagate if the old and new lclVar have different 'doNotEnregister' settings.
// This is primarily to avoid copy propagating to IND(ADDR(LCL_VAR)) where the replacement lclVar
// is not marked 'lvDoNotEnregister'.
// However, in addition, it may not be profitable to propagate a 'doNotEnregister' lclVar to an
// existing use of an enregisterable lclVar.
// It may not be profitable to propagate a 'doNotEnregister' lclVar to an existing use of an
// enregisterable lclVar.
LclVarDsc* const newLclVarDsc = lvaGetDesc(newLclNum);
if (varDsc->lvDoNotEnregister != newLclVarDsc->lvDoNotEnregister)
{
Expand Down Expand Up @@ -285,8 +282,8 @@ bool Compiler::optCopyProp(
// optCopyPropPushDef: Push the new live SSA def on the stack for "lclNode".
//
// Arguments:
// defNode - The definition node for this def (GT_ASG/GT_CALL) (will be "nullptr" for "use" defs)
// lclNode - The local tree representing "the def" (that can actually be a use)
// defNode - The definition node for this def (store/GT_CALL) (will be "nullptr" for "use" defs)
// lclNode - The local tree representing "the def"
// curSsaName - The map of local numbers to stacks of their defs
//
void Compiler::optCopyPropPushDef(GenTree* defNode, GenTreeLclVarCommon* lclNode, LclNumToLiveDefsMap* curSsaName)
Expand Down Expand Up @@ -392,8 +389,7 @@ bool Compiler::optBlockCopyProp(BasicBlock* block, LclNumToLiveDefsMap* curSsaNa
{
optCopyPropPushDef(tree, lclDefNode, curSsaName);
}
else if (tree->OperIs(GT_LCL_VAR, GT_LCL_FLD) && ((tree->gtFlags & GTF_VAR_DEF) == 0) &&
tree->AsLclVarCommon()->HasSsaName())
else if (tree->OperIs(GT_LCL_VAR, GT_LCL_FLD) && tree->AsLclVarCommon()->HasSsaName())
{
unsigned lclNum = tree->AsLclVarCommon()->GetLclNum();

Expand Down
61 changes: 29 additions & 32 deletions src/coreclr/jit/earlyprop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -361,32 +361,31 @@ GenTree* Compiler::optPropGetValueRec(unsigned lclNum, unsigned ssaNum, optPropK
}

// Track along the use-def chain to get the array length
LclSsaVarDsc* ssaVarDsc = lvaTable[lclNum].GetPerSsaData(ssaNum);
GenTreeOp* ssaDefAsg = ssaVarDsc->GetAssignment();
LclSsaVarDsc* ssaVarDsc = lvaTable[lclNum].GetPerSsaData(ssaNum);
GenTreeLclVarCommon* ssaDefStore = ssaVarDsc->GetAssignment();

// Incoming parameters or live-in variables don't have actual definition tree node for
// their FIRST_SSA_NUM. Definitions induced by calls do not record the store node. See
// SsaBuilder::RenameDef.
if (ssaDefAsg != nullptr)
if (ssaDefStore != nullptr)
{
assert(ssaDefAsg->OperIs(GT_ASG));
assert(ssaDefStore->OperIsLocalStore());

GenTree* treeLhs = ssaDefAsg->gtGetOp1();
GenTree* treeRhs = ssaDefAsg->gtGetOp2();
GenTree* data = ssaDefStore->Data();

// Recursively track the Rhs for "entire" stores.
if (treeLhs->OperIs(GT_LCL_VAR) && (treeLhs->AsLclVar()->GetLclNum() == lclNum) && treeRhs->OperIs(GT_LCL_VAR))
if (ssaDefStore->OperIs(GT_STORE_LCL_VAR) && (ssaDefStore->GetLclNum() == lclNum) && data->OperIs(GT_LCL_VAR))
{
unsigned rhsLclNum = treeRhs->AsLclVarCommon()->GetLclNum();
unsigned rhsSsaNum = treeRhs->AsLclVarCommon()->GetSsaNum();
unsigned dataLclNum = data->AsLclVarCommon()->GetLclNum();
unsigned dataSsaNum = data->AsLclVarCommon()->GetSsaNum();

value = optPropGetValueRec(rhsLclNum, rhsSsaNum, valueKind, walkDepth + 1);
value = optPropGetValueRec(dataLclNum, dataSsaNum, valueKind, walkDepth + 1);
}
else
{
if (valueKind == optPropKind::OPK_ARRAYLEN)
{
value = getArrayLengthFromAllocation(treeRhs DEBUGARG(ssaVarDsc->GetBlock()));
value = getArrayLengthFromAllocation(data DEBUGARG(ssaVarDsc->GetBlock()));
if (value != nullptr)
{
if (!value->IsCnsIntOrI())
Expand Down Expand Up @@ -566,26 +565,20 @@ GenTree* Compiler::optFindNullCheckToFold(GenTree* tree, LocalNumberToNullCheckT
return nullptr;
}

GenTree* defNode = defLoc->GetAssignment();
if (defNode == nullptr)
GenTreeLclVarCommon* defNode = defLoc->GetAssignment();
if ((defNode == nullptr) || !defNode->OperIs(GT_STORE_LCL_VAR) || (defNode->GetLclNum() != lclNum))
{
return nullptr;
}

GenTree* defLHS = defNode->gtGetOp1();
if (!defLHS->OperIs(GT_LCL_VAR) || (defLHS->AsLclVar()->GetLclNum() != lclNum))
{
return nullptr;
}

GenTree* defRHS = defNode->gtGetOp2();
if (defRHS->OperGet() != GT_COMMA)
GenTree* defValue = defNode->Data();
if (defValue->OperGet() != GT_COMMA)
{
return nullptr;
}

const bool commaOnly = true;
GenTree* commaOp1EffectiveValue = defRHS->gtGetOp1()->gtEffectiveVal(commaOnly);
GenTree* commaOp1EffectiveValue = defValue->gtGetOp1()->gtEffectiveVal(commaOnly);

if (commaOp1EffectiveValue->OperGet() != GT_NULLCHECK)
{
Expand All @@ -594,14 +587,14 @@ GenTree* Compiler::optFindNullCheckToFold(GenTree* tree, LocalNumberToNullCheckT

GenTree* nullCheckAddress = commaOp1EffectiveValue->gtGetOp1();

if ((nullCheckAddress->OperGet() != GT_LCL_VAR) || (defRHS->gtGetOp2()->OperGet() != GT_ADD))
if ((nullCheckAddress->OperGet() != GT_LCL_VAR) || (defValue->gtGetOp2()->OperGet() != GT_ADD))
{
return nullptr;
}

// We found a candidate for 'y' in the pattern above.

GenTree* additionNode = defRHS->gtGetOp2();
GenTree* additionNode = defValue->gtGetOp2();
GenTree* additionOp1 = additionNode->gtGetOp1();
GenTree* additionOp2 = additionNode->gtGetOp2();
if ((additionOp1->OperGet() == GT_LCL_VAR) &&
Expand Down Expand Up @@ -762,24 +755,28 @@ bool Compiler::optCanMoveNullCheckPastTree(GenTree* tree,

if (result && ((tree->gtFlags & GTF_ASG) != 0))
{
if (tree->OperGet() == GT_ASG)
if (tree->OperIsStore())
{
GenTree* lhs = tree->gtGetOp1();
GenTree* rhs = tree->gtGetOp2();
if (checkSideEffectSummary && ((rhs->gtFlags & GTF_ASG) != 0))
if (checkSideEffectSummary && ((tree->Data()->gtFlags & GTF_ASG) != 0))
{
result = false;
}
else if (isInsideTry)
{
// Inside try we allow only assignments to locals not live in handlers.
// Inside try we allow only stores to locals not live in handlers.
// lvVolatileHint is set to true on variables that are line in handlers.
result = (lhs->OperGet() == GT_LCL_VAR) && !lvaTable[lhs->AsLclVarCommon()->GetLclNum()].lvVolatileHint;
result = tree->OperIs(GT_STORE_LCL_VAR) && !lvaTable[tree->AsLclVar()->GetLclNum()].lvVolatileHint;
}
else
{
// We disallow only assignments to global memory.
result = ((lhs->gtFlags & GTF_GLOB_REF) == 0);
// We disallow stores to global memory.
result = tree->OperIsLocalStore() && !lvaGetDesc(tree->AsLclVarCommon())->IsAddressExposed();

// TODO-ASG-Cleanup: delete this zero-diff quirk. Some setup args for by-ref args do not have GLOB_REF.
if ((tree->gtFlags & GTF_GLOB_REF) == 0)
{
result = true;
}
}
}
else if (checkSideEffectSummary)
Expand Down
Loading