Skip to content
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
2 changes: 1 addition & 1 deletion src/coreclr/jit/compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -5695,7 +5695,7 @@ class Compiler
GenTree* fgMorphInitBlock(GenTree* tree);
GenTree* fgMorphPromoteLocalInitBlock(GenTreeLclVar* destLclNode, GenTree* initVal, unsigned blockSize);
GenTree* fgMorphGetStructAddr(GenTree** pTree, CORINFO_CLASS_HANDLE clsHnd, bool isRValue = false);
GenTree* fgMorphBlockOperand(GenTree* tree, var_types asgType, unsigned blockWidth, bool isBlkReqd);
GenTree* fgMorphBlockOperand(GenTree* tree, var_types asgType, ClassLayout* blockLayout, bool isBlkReqd);
GenTree* fgMorphCopyBlock(GenTree* tree);
GenTree* fgMorphStoreDynBlock(GenTreeStoreDynBlk* tree);
GenTree* fgMorphForRegisterFP(GenTree* tree);
Expand Down
65 changes: 19 additions & 46 deletions src/coreclr/jit/gentree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7353,15 +7353,15 @@ void Compiler::gtSetObjGcInfo(GenTreeObj* objNode)
}

//------------------------------------------------------------------------
// gtNewStructVal: Return a node that represents a struct value
// gtNewStructVal: Return a node that represents a struct or block value
//
// Arguments:
// layout - The struct's layout
// addr - The address of the struct
//
// Return Value:
// An "OBJ" node, or "LCL_VAR" node if "addr" points to a local with
// a layout compatible with "layout".
// An "OBJ/BLK" node, or "LCL_VAR" node if "addr" points to a local
// with a layout compatible with "layout".
//
GenTree* Compiler::gtNewStructVal(ClassLayout* layout, GenTree* addr)
{
Expand All @@ -7380,7 +7380,17 @@ GenTree* Compiler::gtNewStructVal(ClassLayout* layout, GenTree* addr)
}
}

return gtNewObjNode(layout, addr);
GenTreeBlk* blkNode;
if (layout->IsBlockLayout())
{
blkNode = new (this, GT_BLK) GenTreeBlk(GT_BLK, layout->GetType(), addr, layout);
}
else
{
blkNode = gtNewObjNode(layout, addr);
}

return blkNode;
}

//------------------------------------------------------------------------
Expand All @@ -7392,31 +7402,13 @@ GenTree* Compiler::gtNewStructVal(ClassLayout* layout, GenTree* addr)
//
// Return Value:
// A block, object or local node that represents the block value pointed to by 'addr'.

//
GenTree* Compiler::gtNewBlockVal(GenTree* addr, unsigned size)
{
// By default we treat this as an opaque struct type with known size.
var_types blkType = TYP_STRUCT;
if (addr->gtOper == GT_ADDR)
{
GenTree* val = addr->gtGetOp1();
#if FEATURE_SIMD
if (varTypeIsSIMD(val) && (genTypeSize(val) == size))
{
blkType = val->TypeGet();
}
#endif // FEATURE_SIMD
if (varTypeIsStruct(val) && val->OperIs(GT_LCL_VAR))
{
LclVarDsc* varDsc = lvaGetDesc(val->AsLclVarCommon());
unsigned varSize = varTypeIsStruct(varDsc) ? varDsc->lvExactSize : genTypeSize(varDsc);
if (varSize == size)
{
return val;
}
}
}
return new (this, GT_BLK) GenTreeBlk(GT_BLK, blkType, addr, typGetBlkLayout(size));
ClassLayout* layout = typGetBlkLayout(size);
GenTree* blkNode = gtNewStructVal(layout, addr);

return blkNode;
}

// Creates a new assignment node for a CpObj.
Expand Down Expand Up @@ -17262,25 +17254,6 @@ CORINFO_CLASS_HANDLE Compiler::gtGetStructHandleIfPresent(GenTree* tree)
if (addr->OperIs(GT_ARR_ADDR))
{
structHnd = addr->AsArrAddr()->GetElemClassHandle();
break;
}

FieldSeqNode* fieldSeq = nullptr;
if ((addr->OperGet() == GT_ADD) && addr->gtGetOp2()->OperIs(GT_CNS_INT))
{
fieldSeq = addr->gtGetOp2()->AsIntCon()->gtFieldSeq;
}
else
{
GetZeroOffsetFieldMap()->Lookup(addr, &fieldSeq);
}

if ((fieldSeq != nullptr) && (fieldSeq != FieldSeqStore::NotAField()))
{
fieldSeq = fieldSeq->GetTail();

// Note we may have a primitive here (and correctly fail to obtain the handle)
eeGetFieldType(fieldSeq->GetFieldHandle(), &structHnd);
}
}
break;
Expand Down
83 changes: 22 additions & 61 deletions src/coreclr/jit/morph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9044,7 +9044,7 @@ GenTree* Compiler::fgMorphOneAsgBlockOp(GenTree* tree)
}
if (isCopyBlock && destLclVarTree == nullptr && !src->OperIs(GT_LCL_VAR))
{
fgMorphBlockOperand(src, asgType, genTypeSize(asgType), false /*isBlkReqd*/);
fgMorphBlockOperand(src, asgType, nullptr, false /*isBlkReqd*/);
dest->gtFlags |= GTF_DONT_CSE;
return tree;
}
Expand Down Expand Up @@ -9266,13 +9266,9 @@ GenTree* Compiler::fgMorphOneAsgBlockOp(GenTree* tree)

if (dest->gtEffectiveVal()->OperIsIndir())
{
// If we have no information about the destination, we have to assume it could
// live anywhere (not just in the GC heap).
// Mark the GT_IND node so that we use the correct write barrier helper in case
// the field is a GC ref.

if (!fgIsIndirOfAddrOfLocal(dest))
{
// TODO-Bug: the GLOB_REF also needs to be set in case "src" is address-exposed.
dest->gtFlags |= GTF_GLOB_REF;
tree->gtFlags |= GTF_GLOB_REF;
}
Expand Down Expand Up @@ -9351,13 +9347,13 @@ GenTree* Compiler::fgMorphOneAsgBlockOp(GenTree* tree)
// Ensure that the dest is setup appropriately.
if (dest->gtEffectiveVal()->OperIsIndir())
{
dest = fgMorphBlockOperand(dest, asgType, size, false /*isBlkReqd*/);
dest = fgMorphBlockOperand(dest, asgType, nullptr, false /*isBlkReqd*/);
}

// Ensure that the rhs is setup appropriately.
if (isCopyBlock)
{
src = fgMorphBlockOperand(src, asgType, size, false /*isBlkReqd*/);
src = fgMorphBlockOperand(src, asgType, nullptr, false /*isBlkReqd*/);
}

// Set the lhs and rhs on the assignment.
Expand Down Expand Up @@ -9634,10 +9630,10 @@ GenTree* Compiler::fgMorphGetStructAddr(GenTree** pTree, CORINFO_CLASS_HANDLE cl
// fgMorphBlockOperand: Canonicalize an operand of a block assignment
//
// Arguments:
// tree - The block operand
// asgType - The type of the assignment
// blockWidth - The size of the block
// isBlkReqd - true iff this operand must remain a block node
// tree - The block operand
// asgType - The type of the assignment
// blockLayout - The struct layout of the block (for STRUCT "asgType"s)
// isBlkReqd - true iff this operand must remain a block node
//
// Return Value:
// Returns the morphed block operand
Expand All @@ -9648,13 +9644,15 @@ GenTree* Compiler::fgMorphGetStructAddr(GenTree** pTree, CORINFO_CLASS_HANDLE cl
// - Ensures that any COMMAs are above ADDR nodes.
// Although 'tree' WAS an operand of a block assignment, the assignment
// may have been retyped to be a scalar assignment.

GenTree* Compiler::fgMorphBlockOperand(GenTree* tree, var_types asgType, unsigned blockWidth, bool isBlkReqd)
//
GenTree* Compiler::fgMorphBlockOperand(GenTree* tree, var_types asgType, ClassLayout* blockLayout, bool isBlkReqd)
{
GenTree* effectiveVal = tree->gtEffectiveVal();

if (asgType != TYP_STRUCT)
{
unsigned blockWidth = genTypeSize(asgType);

if (effectiveVal->OperIsIndir())
{
if (!isBlkReqd)
Expand Down Expand Up @@ -9690,6 +9688,8 @@ GenTree* Compiler::fgMorphBlockOperand(GenTree* tree, var_types asgType, unsigne
}
else
{
assert(blockLayout != nullptr);

GenTreeIndir* indirTree = nullptr;
GenTreeLclVarCommon* lclNode = nullptr;
bool needsIndirection = true;
Expand All @@ -9713,7 +9713,7 @@ GenTree* Compiler::fgMorphBlockOperand(GenTree* tree, var_types asgType, unsigne
#ifdef DEBUG
GenTreeCall* call = effectiveVal->AsCall();
assert(call->TypeGet() == TYP_STRUCT);
assert(blockWidth == info.compCompHnd->getClassSize(call->gtRetClsHnd));
assert(blockLayout->GetSize() == info.compCompHnd->getClassSize(call->gtRetClsHnd));
#endif
}
#ifdef TARGET_ARM64
Expand All @@ -9731,7 +9731,7 @@ GenTree* Compiler::fgMorphBlockOperand(GenTree* tree, var_types asgType, unsigne
if (lclNode != nullptr)
{
const LclVarDsc* varDsc = lvaGetDesc(lclNode);
if (varTypeIsStruct(varDsc) && (varDsc->lvExactSize == blockWidth) && (varDsc->lvType == asgType))
if (varTypeIsStruct(varDsc) && ClassLayout::AreCompatible(varDsc->GetLayout(), blockLayout))
{
if (effectiveVal != lclNode)
{
Expand All @@ -9746,6 +9746,7 @@ GenTree* Compiler::fgMorphBlockOperand(GenTree* tree, var_types asgType, unsigne
effectiveVal->gtFlags |= (lclNode->gtFlags & GTF_ALL_EFFECT);
}
}

if (needsIndirection)
{
if ((indirTree != nullptr) && (indirTree->OperIsBlk() || !isBlkReqd))
Expand All @@ -9754,35 +9755,14 @@ GenTree* Compiler::fgMorphBlockOperand(GenTree* tree, var_types asgType, unsigne
}
else
{
GenTree* newTree;
GenTree* addr = gtNewOperNode(GT_ADDR, TYP_BYREF, effectiveVal);
if (isBlkReqd)
{
CORINFO_CLASS_HANDLE clsHnd = gtGetStructHandleIfPresent(effectiveVal);
if (clsHnd == NO_CLASS_HANDLE)
{
newTree = new (this, GT_BLK) GenTreeBlk(GT_BLK, TYP_STRUCT, addr, typGetBlkLayout(blockWidth));
}
else
{
newTree = gtNewObjNode(clsHnd, addr);
gtSetObjGcInfo(newTree->AsObj());
}

gtUpdateNodeSideEffects(newTree);
}
else
{
newTree = gtNewIndir(asgType, addr);
}

effectiveVal = newTree;
effectiveVal = gtNewStructVal(blockLayout, gtNewOperNode(GT_ADDR, TYP_BYREF, effectiveVal));
gtUpdateNodeSideEffects(effectiveVal);
}
}
}

assert(effectiveVal->TypeIs(asgType) || (varTypeIsSIMD(asgType) && varTypeIsStruct(effectiveVal)));
tree = effectiveVal;
return tree;
return effectiveVal;
}

// insert conversions and normalize to make tree amenable to register
Expand Down Expand Up @@ -11699,12 +11679,7 @@ GenTree* Compiler::fgMorphSmpOp(GenTree* tree, MorphAddrContext* mac)

temp = op1->AsOp()->gtOp1; // X

// In the test below, if they're both TYP_STRUCT, this of course does *not* mean that
// they are the *same* struct type. In fact, they almost certainly aren't. If the
// address has an associated field sequence, that identifies this case; go through
// the "lcl_fld" path rather than this one.
FieldSeqNode* addrFieldSeq = nullptr; // This is an unused out parameter below.
if (typ == temp->TypeGet() && !GetZeroOffsetFieldMap()->Lookup(op1, &addrFieldSeq))
if ((typ == temp->TypeGet()) && (typ != TYP_STRUCT))
{
foldAndReturnTemp = true;
}
Expand Down Expand Up @@ -11770,20 +11745,6 @@ GenTree* Compiler::fgMorphSmpOp(GenTree* tree, MorphAddrContext* mac)
tree->gtType = typ = temp->TypeGet();
foldAndReturnTemp = true;
}
else
{
// Assumes that when Lookup returns "false" it will leave "fieldSeq" unmodified (i.e.
// nullptr)
assert(fieldSeq == nullptr);
bool b = GetZeroOffsetFieldMap()->Lookup(op1, &fieldSeq);
assert(b || fieldSeq == nullptr);

if ((fieldSeq != nullptr) && (temp->OperGet() == GT_LCL_FLD))
{
temp->gtType = typ;
foldAndReturnTemp = true;
}
}
// Otherwise will will fold this into a GT_LCL_FLD below
// where we check (temp != nullptr)
}
Expand Down
53 changes: 23 additions & 30 deletions src/coreclr/jit/morphblock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class MorphInitBlockHelper
GenTree* m_src = nullptr;

unsigned m_blockSize = 0;
ClassLayout* m_blockLayout = nullptr;
unsigned m_dstLclNum = BAD_VAR_NUM;
GenTreeLclVarCommon* m_dstLclNode = nullptr;
LclVarDsc* m_dstVarDsc = nullptr;
Expand Down Expand Up @@ -196,31 +197,10 @@ void MorphInitBlockHelper::PrepareDst()
m_dstVarDsc = m_comp->lvaGetDesc(m_dstLclNode);
m_dstLclOffset = m_dstLclNode->GetLclOffs();

if (m_dst->OperIs(GT_LCL_VAR))
if (m_dst->TypeIs(TYP_STRUCT))
{
if (m_dstVarDsc->TypeGet() == TYP_STRUCT)
{
#ifdef DEBUG
const bool isSizeMistmatch =
(m_dstVarDsc->lvExactSize != m_comp->info.compCompHnd->getClassSize(m_dstVarDsc->GetStructHnd()));
const bool isStackAllocCandidate =
m_comp->compObjectStackAllocation() && !m_dstVarDsc->GetLayout()->IsValueClass();
// There were cases where for temps lvExactSize did not correspond to the struct size
// so we were using `getClassSize` result here, however, now this cases are gone and the only
// scenario when `getClassSize` != `lvExactSize` it is a class object optimized to be on stack
assert(!isSizeMistmatch || isStackAllocCandidate);
#endif // DEBUG
m_blockSize = m_dstVarDsc->lvExactSize;
}
else
{
m_blockSize = genTypeSize(m_dstVarDsc);
}
}
else
{
assert(m_dst->OperIs(GT_LCL_FLD) && !m_dst->TypeIs(TYP_STRUCT));
m_blockSize = m_dst->AsLclFld()->GetSize();
assert(m_dstVarDsc->GetLayout()->GetSize() == m_dstVarDsc->lvExactSize);
m_blockLayout = m_dstLclNode->GetLayout(m_comp);
}
}
else
Expand All @@ -239,7 +219,20 @@ void MorphInitBlockHelper::PrepareDst()
m_dstLclOffset = static_cast<unsigned>(dstLclOffset);
}

m_blockSize = m_dst->AsIndir()->Size();
if (m_dst->TypeIs(TYP_STRUCT))
{
m_blockLayout = m_dst->AsBlk()->GetLayout();
}
}

if (m_dst->TypeIs(TYP_STRUCT))
{
m_blockSize = m_blockLayout->GetSize();
}
else
{
assert(m_blockLayout == nullptr);
m_blockSize = genTypeSize(m_dst);
}

if (m_dstLclNode != nullptr)
Expand Down Expand Up @@ -332,7 +325,7 @@ void MorphInitBlockHelper::MorphStructCases()
if (m_transformationDecision == BlockTransformation::Undefined)
{
// For an InitBlock we always require a block operand.
m_dst = m_comp->fgMorphBlockOperand(m_dst, m_dst->TypeGet(), m_blockSize, true /*isBlkReqd*/);
m_dst = m_comp->fgMorphBlockOperand(m_dst, m_dst->TypeGet(), m_blockLayout, true /*isBlkReqd*/);
m_transformationDecision = BlockTransformation::StructBlock;
m_dst->gtFlags |= GTF_DONT_CSE;
m_result = m_asg;
Expand Down Expand Up @@ -906,11 +899,11 @@ void MorphCopyBlockHelper::MorphStructCases()
{
const var_types asgType = m_dst->TypeGet();
bool isBlkReqd = (asgType == TYP_STRUCT);
m_dst = m_comp->fgMorphBlockOperand(m_dst, asgType, m_blockSize, isBlkReqd);
m_dst = m_comp->fgMorphBlockOperand(m_dst, asgType, m_blockLayout, isBlkReqd);
m_dst->gtFlags |= GTF_DONT_CSE;
m_asg->gtOp1 = m_dst;

m_src = m_comp->fgMorphBlockOperand(m_src, asgType, m_blockSize, isBlkReqd);
m_src = m_comp->fgMorphBlockOperand(m_src, asgType, m_blockLayout, isBlkReqd);
m_asg->gtOp2 = m_src;

m_asg->SetAllEffectsFlags(m_dst, m_src);
Expand Down Expand Up @@ -991,7 +984,7 @@ GenTree* MorphCopyBlockHelper::CopyFieldByField()
else if (m_dstDoFldAsg)
{
fieldCnt = m_dstVarDsc->lvFieldCnt;
m_src = m_comp->fgMorphBlockOperand(m_src, m_asg->TypeGet(), m_blockSize, false /*isBlkReqd*/);
m_src = m_comp->fgMorphBlockOperand(m_src, m_asg->TypeGet(), m_blockLayout, false /*isBlkReqd*/);
m_srcUseLclFld = m_srcVarDsc != nullptr;

if (!m_srcUseLclFld && (m_srcAddr == nullptr))
Expand Down Expand Up @@ -1025,7 +1018,7 @@ GenTree* MorphCopyBlockHelper::CopyFieldByField()
{
assert(m_srcDoFldAsg);
fieldCnt = m_srcVarDsc->lvFieldCnt;
m_dst = m_comp->fgMorphBlockOperand(m_dst, m_dst->TypeGet(), m_blockSize, false /*isBlkReqd*/);
m_dst = m_comp->fgMorphBlockOperand(m_dst, m_dst->TypeGet(), m_blockLayout, false /*isBlkReqd*/);
m_dstUseLclFld = m_dstVarDsc != nullptr;

if (m_dst->OperIsBlk())
Expand Down