Skip to content

Commit

Permalink
Merge pull request #7398 from dylanjtuttle/replaceRedundantShifts
Browse files Browse the repository at this point in the history
Replace shifts and rotations of constant 0 with constant 0
  • Loading branch information
hzongaro authored Jul 15, 2024
2 parents 37dce37 + 7b6f6c2 commit 3223d8d
Show file tree
Hide file tree
Showing 2 changed files with 155 additions and 21 deletions.
98 changes: 90 additions & 8 deletions compiler/optimizer/OMRSimplifierHandlers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10259,7 +10259,12 @@ TR::Node *ishlSimplifier(TR::Node * node, TR::Block * block, TR::Simplifier * s)
if (identity)
return identity;

if (secondChild->getOpCode().isLoadConst() &&
// Replace shift of constant zero with constant zero
if (firstChild->getOpCode().isLoadConst() && firstChild->getInt() == 0)
{
return s->replaceNode(node, firstChild, s->_curTree);
}
else if (secondChild->getOpCode().isLoadConst() &&
performTransformation(s->comp(), "%sChanged ishl by const into imul by const in node [%s]\n", s->optDetailString(), node->getName(s->getDebug())))
{
// Normalize shift by a constant into multiply by a constant
Expand Down Expand Up @@ -10301,7 +10306,12 @@ TR::Node *lshlSimplifier(TR::Node * node, TR::Block * block, TR::Simplifier * s)
if (identity)
return identity;

if (secondChild->getOpCode().isLoadConst())
// Replace shift of constant zero with constant zero
if (firstChild->getOpCode().isLoadConst() && firstChild->getLongInt() == 0)
{
return s->replaceNode(node, firstChild, s->_curTree);
}
else if (secondChild->getOpCode().isLoadConst())
{
// Canonicalize shift by a constant into multiply by a constant
//
Expand Down Expand Up @@ -10345,6 +10355,12 @@ TR::Node *bshlSimplifier(TR::Node * node, TR::Block * block, TR::Simplifier * s)
if (identity)
return identity;

// Replace shift of constant zero with constant zero
if (firstChild->getOpCode().isLoadConst() && firstChild->getByte() == 0)
{
return s->replaceNode(node, firstChild, s->_curTree);
}

return node;
}

Expand All @@ -10365,6 +10381,12 @@ TR::Node *sshlSimplifier(TR::Node * node, TR::Block * block, TR::Simplifier * s)
if (identity)
return identity;

// Replace shift of constant zero with constant zero
if (firstChild->getOpCode().isLoadConst() && firstChild->getShortInt() == 0)
{
return s->replaceNode(node, firstChild, s->_curTree);
}

return node;
}

Expand All @@ -10391,7 +10413,13 @@ TR::Node *ishrSimplifier(TR::Node * node, TR::Block * block, TR::Simplifier * s)
if (identity)
return identity;

normalizeShiftAmount(node, 31, s);
// Replace shift of constant zero with constant zero
if (firstChild->getOpCode().isLoadConst() && firstChild->getInt() == 0)
{
return s->replaceNode(node, firstChild, s->_curTree);
}
else
normalizeShiftAmount(node, 31, s);

return node;
}
Expand All @@ -10415,7 +10443,13 @@ TR::Node *lshrSimplifier(TR::Node * node, TR::Block * block, TR::Simplifier * s)
if (identity)
return identity;

normalizeShiftAmount(node, 63, s);
// Replace shift of constant zero with constant zero
if (firstChild->getOpCode().isLoadConst() && firstChild->getLongInt() == 0)
{
return s->replaceNode(node, firstChild, s->_curTree);
}
else
normalizeShiftAmount(node, 63, s);

return node;
}
Expand All @@ -10437,6 +10471,12 @@ TR::Node *bshrSimplifier(TR::Node * node, TR::Block * block, TR::Simplifier * s)
if (identity)
return identity;

// Replace shift of constant zero with constant zero
if (firstChild->getOpCode().isLoadConst() && firstChild->getByte() == 0)
{
return s->replaceNode(node, firstChild, s->_curTree);
}

return node;
}

Expand All @@ -10457,6 +10497,12 @@ TR::Node *sshrSimplifier(TR::Node * node, TR::Block * block, TR::Simplifier * s)
if (identity)
return identity;

// Replace shift of constant zero with constant zero
if (firstChild->getOpCode().isLoadConst() && firstChild->getShortInt() == 0)
{
return s->replaceNode(node, firstChild, s->_curTree);
}

return node;
}

Expand Down Expand Up @@ -10549,7 +10595,13 @@ TR::Node *iushrSimplifier(TR::Node * node, TR::Block * block, TR::Simplifier * s
}
}

normalizeShiftAmount(node, 31, s);
// Replace shift of constant zero with constant zero
if (firstChild->getOpCode().isLoadConst() && firstChild->getUnsignedInt() == 0)
{
return s->replaceNode(node, firstChild, s->_curTree);
}
else
normalizeShiftAmount(node, 31, s);

return node;
}
Expand Down Expand Up @@ -10691,7 +10743,13 @@ TR::Node *lushrSimplifier(TR::Node * node, TR::Block * block, TR::Simplifier * s
}
}

normalizeShiftAmount(node, 63, s);
// Replace shift of constant zero with constant zero
if (firstChild->getOpCode().isLoadConst() && firstChild->getUnsignedLongInt() == 0)
{
return s->replaceNode(node, firstChild, s->_curTree);
}
else
normalizeShiftAmount(node, 63, s);

return node;
}
Expand All @@ -10713,6 +10771,12 @@ TR::Node *bushrSimplifier(TR::Node * node, TR::Block * block, TR::Simplifier * s
if (identity)
return identity;

// Replace shift of constant zero with constant zero
if (firstChild->getOpCode().isLoadConst() && firstChild->getUnsignedByte() == 0)
{
return s->replaceNode(node, firstChild, s->_curTree);
}

return node;
}

Expand All @@ -10733,6 +10797,12 @@ TR::Node *sushrSimplifier(TR::Node * node, TR::Block * block, TR::Simplifier * s
if (identity)
return identity;

// Replace shift of constant zero with constant zero
if (firstChild->getOpCode().isLoadConst() && firstChild->getUnsignedShortInt() == 0)
{
return s->replaceNode(node, firstChild, s->_curTree);
}

return node;
}

Expand Down Expand Up @@ -10761,7 +10831,13 @@ TR::Node *irolSimplifier(TR::Node * node, TR::Block * block, TR::Simplifier * s)
return s->replaceNode(node, firstChild, s->_curTree);
}

normalizeShiftAmount(node, 31, s);
// Replace rotate of constant zero with constant zero
if (firstChild->getOpCode().isLoadConst() && firstChild->getInt() == 0)
{
return s->replaceNode(node, firstChild, s->_curTree);
}
else
normalizeShiftAmount(node, 31, s);
return node;
}

Expand All @@ -10785,7 +10861,13 @@ TR::Node *lrolSimplifier(TR::Node * node, TR::Block * block, TR::Simplifier * s)
return s->replaceNode(node, firstChild, s->_curTree);
}

normalizeShiftAmount(node, 63, s);
// Replace rotate of constant zero with constant zero
if (firstChild->getOpCode().isLoadConst() && firstChild->getLongInt() == 0)
{
return s->replaceNode(node, firstChild, s->_curTree);
}
else
normalizeShiftAmount(node, 63, s);
return node;
}

Expand Down
78 changes: 65 additions & 13 deletions compiler/optimizer/VPHandlers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7026,6 +7026,14 @@ TR::Node *constrainIshl(OMR::ValuePropagation *vp, TR::Node *node)
vp->replaceByConstant(node, constraint, lhsGlobal);
}

// Replace shift of constant zero with constant zero
if (lhs && lhs->asIntConst()
&& lhs->asIntConst()->getInt() == 0)
{
vp->replaceByConstant(node, lhs, lhsGlobal);
return node;
}

checkForNonNegativeAndOverflowProperties(vp, node);
return node;
}
Expand All @@ -7049,7 +7057,14 @@ TR::Node *constrainLshl(OMR::ValuePropagation *vp, TR::Node *node)
vp->replaceByConstant(node, constraint, lhsGlobal);
}

if (lhs && lhs->asLongConst() &&
// Replace shift of constant zero with constant zero
if (lhs && lhs->asLongConst()
&& lhs->asLongConst()->getLong() == 0)
{
vp->replaceByConstant(node, lhs, lhsGlobal);
return node;
}
else if (lhs && lhs->asLongConst() &&
lhs->asLongConst()->getLong() == 1)
{
TR::VPConstraint *constraint = TR::VPLongRange::create(vp, TR::getMinSigned<TR::Int64>(), TR::getMaxSigned<TR::Int64>(), true);
Expand Down Expand Up @@ -7190,17 +7205,24 @@ TR::Node *constrainIshr(OMR::ValuePropagation *vp, TR::Node *node)
return node;
constrainChildren(vp, node);

bool rhsGlobal;
bool lhsGlobal, rhsGlobal;
TR::VPConstraint *lhs = vp->getConstraint(node->getFirstChild(), lhsGlobal);
TR::VPConstraint *rhs = vp->getConstraint(node->getSecondChild(), rhsGlobal);
lhsGlobal &= rhsGlobal;

// Replace shift of constant zero with constant zero
if (lhs && lhs->asIntConst()
&& lhs->asIntConst()->getInt() == 0)
{
vp->replaceByConstant(node, lhs, lhsGlobal);
return node;
}

if (rhs && rhs->asIntConst())
{
int32_t rhsConst = rhs->asIntConst()->getInt();
int32_t shiftAmount = rhsConst & 0x01F;

bool lhsGlobal;
TR::VPConstraint *lhs = vp->getConstraint(node->getFirstChild(), lhsGlobal);
lhsGlobal &= rhsGlobal;

int32_t low, high;
if (lhs)
{
Expand Down Expand Up @@ -7245,6 +7267,7 @@ TR::Node *constrainIshr(OMR::ValuePropagation *vp, TR::Node *node)
//lhs->decReferenceCount();
//rhs->decReferenceCount();
}

// this handler is not called for unsigned shifts
//#ifdef DEBUG
//else if(!firstChild->getType().isSignedInt()) dumpOptDetails(vp->comp(), "FIXME: Need to support ishr opt for Unsigned datatypes!\n");
Expand All @@ -7262,17 +7285,24 @@ TR::Node *constrainLshr(OMR::ValuePropagation *vp, TR::Node *node)

constrainChildren(vp, node);

bool rhsGlobal;
bool lhsGlobal, rhsGlobal;
TR::VPConstraint *lhs = vp->getConstraint(node->getFirstChild(), lhsGlobal);
TR::VPConstraint *rhs = vp->getConstraint(node->getSecondChild(), rhsGlobal);
lhsGlobal &= rhsGlobal;

// Replace shift of constant zero with constant zero
if (lhs && lhs->asLongConst()
&& lhs->asLongConst()->getLong() == 0)
{
vp->replaceByConstant(node, lhs, lhsGlobal);
return node;
}

if (rhs && rhs->asIntConst())
{
int32_t rhsConst = rhs->asIntConst()->getInt();
int32_t shiftAmount = rhsConst & 0x03F;

bool lhsGlobal;
TR::VPConstraint *lhs = vp->getConstraint(node->getFirstChild(), lhsGlobal);
lhsGlobal &= rhsGlobal;

int64_t low, high;
if (lhs)
{
Expand Down Expand Up @@ -7339,8 +7369,19 @@ TR::Node *constrainIushr(OMR::ValuePropagation *vp, TR::Node *node)
//if (parent && parent->getType().isUnsignedInt())
// isUnsigned = true;

bool rhsGlobal;
bool lhsGlobal, rhsGlobal;
TR::VPConstraint *lhs = vp->getConstraint(node->getFirstChild(), lhsGlobal);
TR::VPConstraint *rhs = vp->getConstraint(node->getSecondChild(), rhsGlobal);
lhsGlobal &= rhsGlobal;

// Replace shift of constant zero with constant zero
if (lhs && lhs->asIntConst()
&& lhs->asIntConst()->getInt() == 0)
{
vp->replaceByConstant(node, lhs, lhsGlobal);
return node;
}

if (rhs && rhs->asIntConst())
{
int32_t rhsConst = rhs->asIntConst()->getInt();
Expand Down Expand Up @@ -7416,8 +7457,19 @@ TR::Node *constrainLushr(OMR::ValuePropagation *vp, TR::Node *node)
return node;
constrainChildren(vp, node);

bool rhsGlobal;
bool lhsGlobal, rhsGlobal;
TR::VPConstraint *lhs = vp->getConstraint(node->getFirstChild(), lhsGlobal);
TR::VPConstraint *rhs = vp->getConstraint(node->getSecondChild(), rhsGlobal);
lhsGlobal &= rhsGlobal;

// Replace shift of constant zero with constant zero
if (lhs && lhs->asLongConst()
&& lhs->asLongConst()->getLong() == 0)
{
vp->replaceByConstant(node, lhs, lhsGlobal);
return node;
}

if (rhs && rhs->asIntConst())
{
int32_t rhsConst = rhs->asIntConst()->getInt();
Expand Down

0 comments on commit 3223d8d

Please sign in to comment.