Skip to content

Commit da980bf

Browse files
authored
Merge pull request #19655 from a7ehuo/fix-ilgen-instanceof-3-release-46
(0.46) Move anchored instanceof to the correct position
2 parents 618a1d6 + c3f186f commit da980bf

File tree

2 files changed

+74
-1
lines changed

2 files changed

+74
-1
lines changed

runtime/compiler/ilgen/IlGenerator.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2652,6 +2652,20 @@ void TR_J9ByteCodeIlGenerator::expandUnresolvedClassInstanceof(TR::TreeTop *tree
26522652
TR_ASSERT(origClassNode->getSymbolReference()->isUnresolved(), "unresolved class instanceof n%un: expected symref of class child n%un to be unresolved\n", instanceofNode->getGlobalIndex(), origClassNode->getGlobalIndex());
26532653

26542654
bool trace = comp()->getOption(TR_TraceILGen);
2655+
2656+
// If the receiver of the instanceof is non-null, there is no need of the null case
2657+
if(instanceofNode->isReferenceNonNull() || objNode->isNonNull())
2658+
{
2659+
TR::Node *resolveCheckNode = genResolveCheck(origClassNode);
2660+
resolveCheckNode->copyByteCodeInfo(instanceofNode);
2661+
tree->insertBefore(TR::TreeTop::create(comp(), resolveCheckNode));
2662+
2663+
if (trace)
2664+
traceMsg(comp(), "%s: emit ResolveCHK n%dn before the unresolved class instanceof n%un in block_%d\n", __FUNCTION__,
2665+
resolveCheckNode->getGlobalIndex(), instanceofNode->getGlobalIndex(), tree->getEnclosingBlock()->getNumber());
2666+
return;
2667+
}
2668+
26552669
if (trace)
26562670
traceMsg(comp(), "expanding unresolved class instanceof n%un in block_%d\n", instanceofNode->getGlobalIndex(), tree->getEnclosingBlock()->getNumber());
26572671

runtime/compiler/ilgen/Walker.cpp

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3048,6 +3048,15 @@ TR_J9ByteCodeIlGenerator::genInvokeInterface(int32_t cpIndex)
30483048
}
30493049
else
30503050
{
3051+
/*
3052+
* The sequence of the nodes being generated below matters for the following reasons:
3053+
* - If OSR generates the call, it can also generate some pending push stores,
3054+
* and especially for involuntary OSR. The ZEROCHK needs to be between the
3055+
* pending push stores and the call
3056+
*
3057+
* - The generated instanceof depends on the NULLCHK on the receiver generated through
3058+
* the call node
3059+
*/
30513060
_methodSymbol->setMayHaveInlineableCall(true);
30523061
TR::TreeTop *prevLastTree = _block->getExit()->getPrevTreeTop();
30533062
TR::Node *callNode = NULL;
@@ -3092,13 +3101,63 @@ TR_J9ByteCodeIlGenerator::genInvokeInterface(int32_t cpIndex)
30923101
genInstanceof(interfaceCPIndex);
30933102
TR::Node *instanceof = pop();
30943103

3104+
// The receiver should not be NULL at this point because there should already be a NULLCHK
3105+
// before the call or there is no need of NULLCHK
3106+
instanceof->setReferenceIsNonNull(true);
3107+
30953108
TR::SymbolReference *icce =
30963109
symRefTab()->findOrCreateIncompatibleClassChangeErrorSymbolRef(_methodSymbol);
30973110

30983111
TR::Node *check =
30993112
TR::Node::createWithSymRef(TR::ZEROCHK, 1, 1, instanceof, icce);
31003113

3101-
callTree->insertBefore(TR::TreeTop::create(comp(), check));
3114+
TR::TreeTop *zeroCHKTT = callTree->insertBefore(TR::TreeTop::create(comp(), check));
3115+
3116+
/*
3117+
* If the class is unresolved, genInstanceof anchors instanceof under a treetop node.
3118+
* Then the anchored instanceof treetop appears after the callTree and it commons with
3119+
* the previous instanceof node under the ZEROCHK.
3120+
* This causes a problem because expandUnresolvedClassInstanceof does not expect the
3121+
* treetop that has the anchored instanceof to have already been evaluated when it appears
3122+
* under ZEROCHK. The transformation in expandUnresolvedClassInstanceof will not function correctly.
3123+
* Therefore, the anchored instanceof treetop needs to be moved up before ZEROCHK.
3124+
*
3125+
* preTT
3126+
* ZEROCHKTT
3127+
* instanceof
3128+
* callTree
3129+
* treetop
3130+
* ==>instanceof
3131+
* nextTT
3132+
*
3133+
* ||
3134+
* ||
3135+
* \/
3136+
*
3137+
* preTT
3138+
* treetop
3139+
* instanceof
3140+
* ZEROCHKTT
3141+
* ==>instanceof
3142+
* callTree
3143+
* nextTT
3144+
*
3145+
*/
3146+
TR::TreeTop *instanceOfTT = callTree->getNextTreeTop();
3147+
if (instanceOfTT &&
3148+
instanceOfTT->getNode()->getOpCodeValue() == TR::treetop &&
3149+
instanceOfTT->getNode()->getFirstChild() &&
3150+
instanceOfTT->getNode()->getFirstChild() == instanceof)
3151+
{
3152+
callTree->join(instanceOfTT->getNextTreeTop());
3153+
3154+
zeroCHKTT->insertBefore(instanceOfTT);
3155+
3156+
if (comp()->getOption(TR_TraceILGen))
3157+
{
3158+
traceMsg(comp(), "%s: move the anchored instanceof n%dn before ZEROCHK n%dn\n", __FUNCTION__, instanceOfTT->getNode()->getGlobalIndex(), zeroCHKTT->getNode()->getGlobalIndex());
3159+
}
3160+
}
31023161
}
31033162
}
31043163

0 commit comments

Comments
 (0)