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

Fix CQ regression & correctness bug in morphing of long muls #53566

Merged
merged 8 commits into from
Jun 25, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/coreclr/jit/compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -5810,6 +5810,12 @@ class Compiler
GenTree* fgRecognizeAndMorphBitwiseRotation(GenTree* tree);
bool fgOperIsBitwiseRotationRoot(genTreeOps oper);

#if !defined(TARGET_64BIT)
// Recognize and morph a long multiplication with 32 bit operands.
GenTreeOp* fgRecognizeAndMorphLongMul(GenTreeOp* mul);
GenTreeOp* fgMorphLongMul(GenTreeOp* mul);
#endif

//-------- Determine the order in which the trees will be evaluated -------

unsigned fgTreeSeqNum;
Expand Down
30 changes: 20 additions & 10 deletions src/coreclr/jit/decomposelongs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -600,12 +600,12 @@ GenTree* DecomposeLongs::DecomposeCast(LIR::Use& use)
{
//
// This int->long cast is used by a GT_MUL that will be transformed by DecomposeMul into a
// GT_LONG_MUL and as a result the high operand produced by the cast will become dead.
// GT_MUL_LONG and as a result the high operand produced by the cast will become dead.
// Skip cast decomposition so DecomposeMul doesn't need to bother with dead code removal,
// especially in the case of sign extending casts that also introduce new lclvars.
//

assert((use.User()->gtFlags & GTF_MUL_64RSLT) != 0);
assert(use.User()->Is64RsltMul());

skipDecomposition = true;
}
Expand Down Expand Up @@ -1533,19 +1533,29 @@ GenTree* DecomposeLongs::DecomposeMul(LIR::Use& use)
{
assert(use.IsInitialized());

GenTree* tree = use.Def();
genTreeOps oper = tree->OperGet();
GenTree* tree = use.Def();

assert(oper == GT_MUL);
assert((tree->gtFlags & GTF_MUL_64RSLT) != 0);
assert(tree->OperIs(GT_MUL));
assert(tree->Is64RsltMul());

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

// We expect both operands to be int->long casts. DecomposeCast specifically
// ignores such casts when they are used by GT_MULs.
assert((op1->OperGet() == GT_CAST) && (op1->TypeGet() == TYP_LONG));
assert((op2->OperGet() == GT_CAST) && (op2->TypeGet() == TYP_LONG));
assert(op1->TypeIs(TYP_LONG) && op2->TypeIs(TYP_LONG));

// We expect the first operand to be an int->long cast.
// DecomposeCast specifically ignores such casts when they are used by GT_MULs.
assert(op1->OperIs(GT_CAST));

// The second operand can be a cast or a constant.
if (!op2->OperIs(GT_CAST))
{
assert(op2->OperIs(GT_LONG));
assert(op2->gtGetOp1()->IsIntegralConst());
assert(op2->gtGetOp2()->IsIntegralConst());

Range().Remove(op2->gtGetOp2());
}

Range().Remove(op1);
Range().Remove(op2);
Expand Down
28 changes: 16 additions & 12 deletions src/coreclr/jit/gentree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2438,15 +2438,15 @@ GenTree* Compiler::gtReverseCond(GenTree* tree)

bool GenTree::gtIsValid64RsltMul()
{
if ((gtOper != GT_MUL) || !(gtFlags & GTF_MUL_64RSLT))
if (!OperIs(GT_MUL) || !Is64RsltMul())
{
return false;
}

GenTree* op1 = AsOp()->gtOp1;
GenTree* op2 = AsOp()->gtOp2;
GenTree* op1 = AsOp()->gtGetOp1();
GenTree* op2 = AsOp()->gtGetOp2();

if (TypeGet() != TYP_LONG || op1->TypeGet() != TYP_LONG || op2->TypeGet() != TYP_LONG)
if (!TypeIs(TYP_LONG) || !op1->TypeIs(TYP_LONG) || !op2->TypeIs(TYP_LONG))
{
return false;
}
Expand All @@ -2456,26 +2456,30 @@ bool GenTree::gtIsValid64RsltMul()
return false;
}

// op1 has to be conv.i8(i4Expr)
if ((op1->gtOper != GT_CAST) || (genActualType(op1->CastFromType()) != TYP_INT))
// op1 has to be CAST(long <- int).
if (!(op1->OperIs(GT_CAST) && genActualTypeIsInt(op1->AsCast()->CastOp())))
{
return false;
}

// op2 has to be conv.i8(i4Expr)
if ((op2->gtOper != GT_CAST) || (genActualType(op2->CastFromType()) != TYP_INT))
// op2 has to be CAST(long <- int) or a suitably small constant.
if (!(op2->OperIs(GT_CAST) && genActualTypeIsInt(op2->AsCast()->CastOp())) &&
!(op2->IsIntegralConst() && FitsIn<int32_t>(op2->AsIntConCommon()->IntegralValue())))
{
return false;
}

// The signedness of both casts must be the same
if (((op1->gtFlags & GTF_UNSIGNED) != 0) != ((op2->gtFlags & GTF_UNSIGNED) != 0))
// Both operands must extend the same way.
bool op1ZeroExtends = op1->IsUnsigned();
bool op2ZeroExtends = op2->OperIs(GT_CAST) ? op2->IsUnsigned() : op2->AsIntConCommon()->IntegralValue() >= 0;
bool op2AnyExtensionIsSuitable = op2->IsIntegralConst() && op2ZeroExtends;
if ((op1ZeroExtends != op2ZeroExtends) && !op2AnyExtensionIsSuitable)
{
return false;
}

// Do unsigned mul iff both the casts are unsigned
if (((op1->gtFlags & GTF_UNSIGNED) != 0) != ((gtFlags & GTF_UNSIGNED) != 0))
// Do unsigned mul iff both operands are zero-extending.
if (op1->IsUnsigned() != IsUnsigned())
{
return false;
}
Expand Down
57 changes: 57 additions & 0 deletions src/coreclr/jit/gentree.h
Original file line number Diff line number Diff line change
Expand Up @@ -2117,6 +2117,63 @@ struct GenTree
return ((gtFlags & GTF_UNSIGNED) != 0);
}

void SetUnsigned()
{
assert(OperIs(GT_ADD, GT_SUB, GT_MUL, GT_CAST));
gtFlags |= GTF_UNSIGNED;
}

void ClearUnsigned()
{
assert(OperIs(GT_ADD, GT_SUB, GT_MUL, GT_CAST));
gtFlags &= ~GTF_UNSIGNED;
}

void SetOverflow()
{
assert(OperMayOverflow());
gtFlags |= GTF_OVERFLOW;
}

void ClearOverflow()
{
assert(OperMayOverflow());
gtFlags &= ~GTF_OVERFLOW;
}

bool Is64RsltMul() const
{
return (gtFlags & GTF_MUL_64RSLT) != 0;
}

void Set64RsltMul()
{
gtFlags |= GTF_MUL_64RSLT;
}

void Clear64RsltMul()
{
gtFlags &= ~GTF_MUL_64RSLT;
}

void SetAllEffectsFlags(GenTree* source)
{
SetAllEffectsFlags(source->gtFlags & GTF_ALL_EFFECT);
}

void SetAllEffectsFlags(GenTree* source, GenTree* otherSource)
{
SetAllEffectsFlags((source->gtFlags | otherSource->gtFlags) & GTF_ALL_EFFECT);
}

void SetAllEffectsFlags(GenTreeFlags sourceFlags)
{
assert((sourceFlags & ~GTF_ALL_EFFECT) == 0);

gtFlags &= ~GTF_ALL_EFFECT;
gtFlags |= sourceFlags;
}

inline bool IsCnsIntOrI() const;

inline bool IsIntegralConst() const;
Expand Down
Loading