Skip to content

Commit

Permalink
Merge pull request #7364 from dylanjtuttle/replaceRedundantIshl
Browse files Browse the repository at this point in the history
Replace redundant shifts and rotations of const 0 with const 0
  • Loading branch information
hzongaro committed Jun 21, 2024
2 parents 47a9d24 + bd8fbc9 commit 9871e05
Show file tree
Hide file tree
Showing 2 changed files with 149 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
72 changes: 59 additions & 13 deletions compiler/optimizer/VPHandlers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7109,6 +7109,13 @@ 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)
{
return vp->replaceNode(node, node->getFirstChild(), vp->_curTree);
}

checkForNonNegativeAndOverflowProperties(vp, node);
return node;
}
Expand All @@ -7132,7 +7139,13 @@ 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)
{
return vp->replaceNode(node, node->getFirstChild(), vp->_curTree);
}
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 @@ -7273,17 +7286,23 @@ 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)
{
return vp->replaceNode(node, node->getFirstChild(), vp->_curTree);
}

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 @@ -7328,6 +7347,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 @@ -7345,17 +7365,23 @@ 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)
{
return vp->replaceNode(node, node->getFirstChild(), vp->_curTree);
}

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 @@ -7422,8 +7448,18 @@ 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)
{
return vp->replaceNode(node, node->getFirstChild(), vp->_curTree);
}

if (rhs && rhs->asIntConst())
{
int32_t rhsConst = rhs->asIntConst()->getInt();
Expand Down Expand Up @@ -7499,8 +7535,18 @@ 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)
{
return vp->replaceNode(node, node->getFirstChild(), vp->_curTree);
}

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

0 comments on commit 9871e05

Please sign in to comment.