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

Recognize and transform redundant calls in MH.invokeExact MethodHandle chain #11118

Merged
merged 2 commits into from
Apr 23, 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
2 changes: 2 additions & 0 deletions runtime/compiler/codegen/J9RecognizedMethodsEnum.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1011,6 +1011,8 @@
java_lang_invoke_InsertHandle_numValuesToInsert,
java_lang_invoke_InterfaceHandle_interfaceCall,
java_lang_invoke_InterfaceHandle_invokeExact,
java_lang_invoke_Invokers_checkCustomized,
java_lang_invoke_Invokers_checkExactType,
java_lang_invoke_MethodHandle_doCustomizationLogic,
java_lang_invoke_MethodHandle_asType,
java_lang_invoke_MethodHandle_asType_instance,
Expand Down
8 changes: 8 additions & 0 deletions runtime/compiler/env/j9method.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4037,6 +4037,13 @@ void TR_ResolvedJ9Method::construct()
{ TR::unknownMethod}
};

static X InvokersMethods[] =
{
{x(TR::java_lang_invoke_Invokers_checkCustomized, "checkCustomized", "(Ljava/lang/invoke/MethodHandle;)V")},
{x(TR::java_lang_invoke_Invokers_checkExactType, "checkExactType", "(Ljava/lang/invoke/MethodHandle;Ljava/lang/invoke/MethodType;)V")},
{TR::unknownMethod}
};

static X AsTypeHandleMethods[] =
{
{ TR::java_lang_invoke_AsTypeHandle_convertArgs, 11, "convertArgs", (int16_t)-1, "*"},
Expand Down Expand Up @@ -4375,6 +4382,7 @@ void TR_ResolvedJ9Method::construct()

static Y class25[] =
{
{ "java/lang/invoke/Invokers", InvokersMethods },
{ 0 }
};

Expand Down
127 changes: 127 additions & 0 deletions runtime/compiler/optimizer/MethodHandleTransformer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include "env/IO.hpp"
#include "env/VMJ9.h"
#include "env/j9method.h"
#include "env/VMAccessCriticalSection.hpp"
#include "il/ILOpCodes.hpp"
#include "il/ILOps.hpp"
#include "il/MethodSymbol.hpp"
Expand Down Expand Up @@ -487,6 +488,12 @@ void TR_MethodHandleTransformer::visitCall(TR::TreeTop* tt, TR::Node* node)
case TR::java_lang_invoke_MethodHandle_linkToStatic:
process_java_lang_invoke_MethodHandle_linkTo(tt, node);
break;
case TR::java_lang_invoke_Invokers_checkCustomized:
process_java_lang_invoke_Invokers_checkCustomized(tt, node);
break;
case TR::java_lang_invoke_Invokers_checkExactType:
process_java_lang_invoke_Invokers_checkExactType(tt, node);
break;
}
}

Expand Down Expand Up @@ -602,3 +609,123 @@ TR_MethodHandleTransformer::process_java_lang_invoke_MethodHandle_linkTo(TR::Tre
tt);
}
}

/*
Transforms calls to java/lang/invoke/Invokers.checkExactType to the more performant ZEROCHK.

Blocks before transformation: ==>

start Block_A
...
treetop
call java/lang/invoke/Invokers.checkExactType(Ljava/lang/invoke/MethodHandle;Ljava/lang/invoke/MethodType;)
==>aload
==>aload
...
end Block_A

Blocks after transformation: ==>

If MethodHandle and expected type are known objects at compile time and match: ==>

start Block_A
...
treetop
PassThrough
aload <MethodHandle>
...
end Block_A

else: ==>

start Block_A
...
treetop
ZEROCHK
acmpeq
==>aload <expected type>
aloadi <MethodHandle.type>
==>aload <MethodHandle>
...
end Block_A

*/
void
TR_MethodHandleTransformer::process_java_lang_invoke_Invokers_checkExactType(TR::TreeTop* tt, TR::Node* node)
{
nbhuiyan marked this conversation as resolved.
Show resolved Hide resolved
auto methodHandleNode = node->getArgument(0);
auto expectedTypeNode = node->getArgument(1);
TR_J9VMBase* fej9 = static_cast<TR_J9VMBase*>(comp()->fe());

TR::KnownObjectTable::Index mhIndex = getObjectInfoOfNode(methodHandleNode);
TR::KnownObjectTable::Index expectedTypeIndex = getObjectInfoOfNode(expectedTypeNode);
auto knot = comp()->getKnownObjectTable();
if (knot && isKnownObject(mhIndex) && isKnownObject(expectedTypeIndex))
{
TR::VMAccessCriticalSection vmAccess(fej9);
uintptr_t mhObject = knot->getPointer(mhIndex);
uintptr_t mtObject = fej9->getReferenceField(mhObject, "type", "Ljava/lang/invoke/MethodType;");
uintptr_t etObject = knot->getPointer(expectedTypeIndex);

if (etObject == mtObject && performTransformation(comp(), "%sChanging checkExactType call node n%dn to PassThrough\n", optDetailString(), node->getGlobalIndex()))
{
TR::TransformUtil::transformCallNodeToPassThrough(this, node, tt, node->getFirstArgument());
return;
}
}
if (!performTransformation(comp(), "%sChanging checkExactType call node n%dn to ZEROCHK\n", optDetailString(), node->getGlobalIndex()))
return;
uint32_t typeOffset = fej9->getInstanceFieldOffsetIncludingHeader("Ljava/lang/invoke/MethodHandle;", "type", "Ljava/lang/invoke/MethodType;", comp()->getCurrentMethod());
auto typeSymRef = comp()->getSymRefTab()->findOrFabricateShadowSymbol(comp()->getMethodSymbol(),
TR::Symbol::Java_lang_invoke_MethodHandle_type,
TR::Address,
typeOffset,
false,
true,
true,
"java/lang/invoke/MethodHandle.type Ljava/lang/invoke/MethodType;");
auto handleTypeNode = TR::Node::createWithSymRef(node, comp()->il.opCodeForIndirectLoad(TR::Address), 1, methodHandleNode, typeSymRef);
auto cmpEqNode = TR::Node::create(node, TR::acmpeq, 2, expectedTypeNode, handleTypeNode);
prepareToReplaceNode(node);
TR::Node::recreate(node, TR::ZEROCHK);
node->setSymbolReference(comp()->getSymRefTab()->findOrCreateMethodTypeCheckSymbolRef(comp()->getMethodSymbol()));
node->setNumChildren(1);
node->setAndIncChild(0, cmpEqNode);
}

/*
java/lang/invoke/Invokers.checkCustomized is redundant if its argument is a known object. This transformation
transforms calls to java/lang/invoke/Invokers.checkCustomized to a PassThrough.

Blocks before transformation: ==>

start Block_A
...
treetop
call java/lang/invoke/Invokers.checkCustomized(Ljava/lang/invoke/MethodHandle;)V
aload <MethodHandle>
...
end Block_A

Blocks after transformation ==>
start Block_A
...
treetop
PassThrough
aload <MethodHandle>
...
end Block_A

*/
void
TR_MethodHandleTransformer::process_java_lang_invoke_Invokers_checkCustomized(TR::TreeTop* tt, TR::Node* node)
{
TR::KnownObjectTable::Index objIndex = getObjectInfoOfNode(node->getFirstArgument());
auto knot = comp()->getKnownObjectTable();
if (isKnownObject(objIndex) && knot && !knot->isNull(objIndex))
{
if (!performTransformation(comp(), "%sRemoving checkCustomized call node n%dn as it is now redundant as MethodHandle has known object index\n", optDetailString(), node->getGlobalIndex()))
return;
TR::TransformUtil::transformCallNodeToPassThrough(this, node, tt, node->getFirstArgument());
}
}
24 changes: 24 additions & 0 deletions runtime/compiler/optimizer/MethodHandleTransformer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,30 @@ class TR_MethodHandleTransformer : public TR::Optimization
//
void process_java_lang_invoke_MethodHandle_linkTo(TR::TreeTop* tt, TR::Node* node);

/** \brief
* Transforms calls to java/lang/invoke/Invokers.checkExactType to ZEROCHK, or eliminated
* entirely if the check can be performed at compile time
*
* \param tt
* The treetop of the call node
*
* \param node
* The call node representing the call to java/lang/invoke/Invokers.checkExactType
*/
void process_java_lang_invoke_Invokers_checkExactType(TR::TreeTop* tt, TR::Node* node);

/** \brief
* Eliminates calls to java/lang/invoke/Invokers.checkCustomized when its argument
* is a known object
*
* \param tt
* The treetop of the call node
*
* \param node
* The call node representing the call to java/lang/invoke/Invokers.checkCustomized
*/
void process_java_lang_invoke_Invokers_checkCustomized(TR::TreeTop* tt, TR::Node* node);

private:
int32_t _numLocals; // Number of parms, autos and temps
ObjectInfo * _currentObjectInfo; // Object info for current block being processed
Expand Down