-
Notifications
You must be signed in to change notification settings - Fork 746
/
Copy pathJ9Inliner.cpp
1580 lines (1359 loc) · 66.6 KB
/
J9Inliner.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*******************************************************************************
* Copyright IBM Corp. and others 2000
*
* This program and the accompanying materials are made available under
* the terms of the Eclipse Public License 2.0 which accompanies this
* distribution and is available at https://www.eclipse.org/legal/epl-2.0/
* or the Apache License, Version 2.0 which accompanies this distribution and
* is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* This Source Code may also be made available under the following
* Secondary Licenses when the conditions for such availability set
* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU
* General Public License, version 2 with the GNU Classpath
* Exception [1] and GNU General Public License, version 2 with the
* OpenJDK Assembly Exception [2].
*
* [1] https://www.gnu.org/software/classpath/license.html
* [2] https://openjdk.org/legal/assembly-exception.html
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0
*******************************************************************************/
#include "optimizer/Inliner.hpp"
#include "optimizer/J9Inliner.hpp"
#include <algorithm>
#include "env/KnownObjectTable.hpp"
#include "compile/OSRData.hpp"
#include "compile/ResolvedMethod.hpp"
#include "env/CompilerEnv.hpp"
#include "env/CHTable.hpp"
#include "env/HeuristicRegion.hpp"
#include "env/PersistentCHTable.hpp"
#include "env/VMJ9.h"
#include "env/jittypes.h"
#include "env/VMAccessCriticalSection.hpp"
#include "il/Block.hpp"
#include "il/Node.hpp"
#include "il/Node_inlines.hpp"
#include "il/ParameterSymbol.hpp"
#include "il/StaticSymbol.hpp"
#include "il/TreeTop.hpp"
#include "il/TreeTop_inlines.hpp"
#include "optimizer/CallInfo.hpp"
#include "optimizer/J9CallGraph.hpp"
#include "optimizer/PreExistence.hpp"
#include "optimizer/RematTools.hpp"
#include "optimizer/Structure.hpp"
#include "runtime/J9Profiler.hpp"
#include "runtime/J9ValueProfiler.hpp"
#include "codegen/CodeGenerator.hpp"
#include "ilgen/J9ByteCode.hpp"
#include "ilgen/J9ByteCodeIterator.hpp"
#define OPT_DETAILS "O^O INLINER: "
const float MIN_PROFILED_CALL_FREQUENCY = (.65f); // lowered this from .80f since opportunities were being missed in WAS; in those cases getting rid of the call even in 65% of the cases was beneficial probably due to the improved icache impact
extern int32_t *NumInlinedMethods; // Defined in Inliner.cpp
extern int32_t *InlinedSizes; // Defined in Inliner.cpp
//duplicated as long as there are two versions of findInlineTargets
static uintptr_t *failMCS(const char *reason, TR_CallSite *callSite, TR_InlinerBase* inliner)
{
debugTrace(inliner->tracer()," Fail isMutableCallSiteTargetInvokeExact(%p): %s", callSite, reason);
return NULL;
}
static uintptr_t *isMutableCallSiteTargetInvokeExact(TR_CallSite *callSite, TR_InlinerBase *inliner)
{
// Looking for either mcs.target.invokeExact(...) or mcs.getTarget().invokeExact(...)
// on some known/fixed MutableCallSite object mcs.
// Return NULL if it's neither of these.
if (inliner->comp()->getOption(TR_DisableMutableCallSiteGuards))
return NULL;
TR::Node *callNode = callSite->_callNode;
if (!callNode || !callNode->getOpCode().isCall())
return failMCS("No call node", callSite, inliner);
else if (callNode->getSymbolReference()->isUnresolved())
return failMCS("Call symref is unresolved", callSite, inliner);
else switch (callNode->getSymbol()->castToResolvedMethodSymbol()->getRecognizedMethod())
{
case TR::java_lang_invoke_MethodHandle_invokeExact:
break;
default:
return failMCS("Call symref is not invokeExact", callSite, inliner);
}
TR::Node *targetNode = callNode->getChild(callNode->getFirstArgumentIndex());
if (!targetNode->getOpCode().hasSymbolReference() || targetNode->getSymbolReference()->isUnresolved())
return failMCS("No target symref", callSite, inliner);
TR::Node *mcsNode = (TR::Node*)(intptr_t)0xdead;
if (targetNode->getOpCode().isCall())
{
switch (targetNode->getSymbol()->castToResolvedMethodSymbol()->getRecognizedMethod())
{
case TR::java_lang_invoke_MutableCallSite_getTarget:
mcsNode = targetNode->getChild(targetNode->getFirstArgumentIndex());
break;
default:
return failMCS("Call receiver isn't a call to getTarget", callSite, inliner);
}
}
else if (targetNode->getOpCode().isLoadIndirect() && targetNode->getDataType() == TR::Address)
{
switch (targetNode->getSymbol()->getRecognizedField())
{
case TR::Symbol::Java_lang_invoke_MutableCallSite_target:
mcsNode = targetNode->getFirstChild();
break;
default:
return failMCS("Call receiver isn't a load of target field", callSite, inliner);
}
}
else
{
return failMCS("Unsuitable call receiver", callSite, inliner);
}
if (mcsNode->getSymbolReference()->hasKnownObjectIndex())
{
uintptr_t *result = mcsNode->getSymbolReference()->getKnownObjectReferenceLocation(inliner->comp());
heuristicTrace(inliner->tracer(), " Success: isMutableCallSiteTargetInvokeExact(%p)=%p (obj%d)", callSite, result, mcsNode->getSymbolReference()->getKnownObjectIndex());
return result;
}
else if (mcsNode->getSymbol()->isFixedObjectRef())
{
uintptr_t *result = (uintptr_t*)mcsNode->getSymbol()->castToStaticSymbol()->getStaticAddress();
heuristicTrace(inliner->tracer()," Success: isMutableCallSiteTargetInvokeExact(%p)=%p (fixed object reference)", callSite, result);
return result;
}
else
{
return failMCS("Unknown MutableCallSite object", callSite, inliner);
}
}
TR_CallSite* TR_CallSite::create(TR::TreeTop* callNodeTreeTop,
TR::Node *parent,
TR::Node* callNode,
TR_OpaqueClassBlock *receiverClass,
TR::SymbolReference *symRef,
TR_ResolvedMethod *resolvedMethod,
TR::Compilation* comp,
TR_Memory* trMemory,
TR_AllocationKind kind,
TR_ResolvedMethod* caller,
int32_t depth,
bool allConsts)
{
TR::MethodSymbol *calleeSymbol = symRef->getSymbol()->castToMethodSymbol();
TR_ResolvedMethod* lCaller = caller ? caller : symRef->getOwningMethod(comp);
if (callNode->getOpCode().isCallIndirect())
{
if (calleeSymbol->isInterface() )
{
return new (trMemory, kind) TR_J9InterfaceCallSite (lCaller,
callNodeTreeTop,
parent,
callNode,
calleeSymbol->getMethod(),
receiverClass,
(int32_t)symRef->getOffset(),
symRef->getCPIndex(),
resolvedMethod,
calleeSymbol->getResolvedMethodSymbol(),
callNode->getOpCode().isCallIndirect(),
calleeSymbol->isInterface(),
callNode->getByteCodeInfo(),
comp,
depth,
allConsts);
}
else
{
if (calleeSymbol->getResolvedMethodSymbol() &&
calleeSymbol->getResolvedMethodSymbol()->getResolvedMethod()->convertToMethod()->isArchetypeSpecimen() &&
calleeSymbol->getResolvedMethodSymbol()->getResolvedMethod()->getMethodHandleLocation())
{
return new (trMemory, kind) TR_J9MethodHandleCallSite (lCaller,
callNodeTreeTop,
parent,
callNode,
calleeSymbol->getMethod(),
receiverClass,
(int32_t)symRef->getOffset(),
symRef->getCPIndex(),
resolvedMethod,
calleeSymbol->getResolvedMethodSymbol(),
callNode->getOpCode().isCallIndirect(),
calleeSymbol->isInterface(),
callNode->getByteCodeInfo(),
comp,
depth,
allConsts) ;
}
if (calleeSymbol->getResolvedMethodSymbol() && calleeSymbol->getResolvedMethodSymbol()->getRecognizedMethod() == TR::java_lang_invoke_MethodHandle_invokeExact)
{
return new (trMemory, kind) TR_J9MutableCallSite (lCaller,
callNodeTreeTop,
parent,
callNode,
calleeSymbol->getMethod(),
receiverClass,
(int32_t)symRef->getOffset(),
symRef->getCPIndex(),
resolvedMethod,
calleeSymbol->getResolvedMethodSymbol(),
callNode->getOpCode().isCallIndirect(),
calleeSymbol->isInterface(),
callNode->getByteCodeInfo(),
comp,
depth,
allConsts) ;
}
return new (trMemory, kind) TR_J9VirtualCallSite (lCaller,
callNodeTreeTop,
parent,
callNode,
calleeSymbol->getMethod(),
receiverClass,
(int32_t)symRef->getOffset(),
symRef->getCPIndex(),
resolvedMethod,
calleeSymbol->getResolvedMethodSymbol(),
callNode->getOpCode().isCallIndirect(),
calleeSymbol->isInterface(),
callNode->getByteCodeInfo(),
comp,
depth,
allConsts) ;
}
}
return new (trMemory, kind) TR_DirectCallSite (lCaller,
callNodeTreeTop,
parent,
callNode,
calleeSymbol->getMethod(),
resolvedMethod && !resolvedMethod->isStatic() ? receiverClass : NULL,
(int32_t)symRef->getOffset(),
symRef->getCPIndex(),
resolvedMethod,
calleeSymbol->getResolvedMethodSymbol(),
callNode->getOpCode().isCallIndirect(),
calleeSymbol->isInterface(),
callNode->getByteCodeInfo(),
comp,
depth,
allConsts) ;
}
static void computeNumLivePendingSlotsAndNestingDepth(TR::Optimizer* optimizer, TR_CallTarget* calltarget, TR_CallStack* callStack, int32_t& numLivePendingPushSlots, int32_t& nestingDepth)
{
TR::Compilation *comp = optimizer->comp();
if (comp->getOption(TR_EnableOSR))
{
TR::Block *containingBlock = calltarget->_myCallSite->_callNodeTreeTop->getEnclosingBlock();
int32_t weight = 1;
nestingDepth = weight/10;
TR::Node *callNode = calltarget->_myCallSite->_callNode;
int32_t callerIndex = callNode->getByteCodeInfo().getCallerIndex();
TR::ResolvedMethodSymbol *caller = (callerIndex == -1) ? comp->getMethodSymbol()
: comp->getInlinedResolvedMethodSymbol(callerIndex);
TR_OSRMethodData *osrMethodData = comp->getOSRCompilationData()->findOrCreateOSRMethodData(callerIndex, caller);
TR_Array<List<TR::SymbolReference> > *pendingPushSymRefs = caller->getPendingPushSymRefs();
int32_t numPendingSlots = 0;
if (pendingPushSymRefs)
numPendingSlots = pendingPushSymRefs->size();
TR_BitVector *deadSymRefs = osrMethodData->getLiveRangeInfo(calltarget->_myCallSite->_callNode->getByteCodeIndex());
for (int32_t i=0;i<numPendingSlots;i++)
{
List<TR::SymbolReference> symRefsAtThisSlot = (*pendingPushSymRefs)[i];
if (symRefsAtThisSlot.isEmpty()) continue;
ListIterator<TR::SymbolReference> symRefsIt(&symRefsAtThisSlot);
TR::SymbolReference *nextSymRef;
for (nextSymRef = symRefsIt.getCurrent(); nextSymRef; nextSymRef=symRefsIt.getNext())
{
if (!deadSymRefs || !deadSymRefs->get(nextSymRef->getReferenceNumber()))
numLivePendingPushSlots++;
}
}
optimizer->comp()->incNumLivePendingPushSlots(numLivePendingPushSlots);
optimizer->comp()->incNumLoopNestingLevels(nestingDepth);
}
}
/*
* Populate the OSRCallSiteRematTable using the pending push stores before this call.
* To achieve this, RematTools is applied to the pending pushes that correspond to the call,
* however, it is limited to using autos, parms and pending push temps as others may be
* modified within the call.
*/
static void populateOSRCallSiteRematTable(TR::Optimizer* optimizer, TR_CallTarget* calltarget,
TR_CallStack* callStack)
{
static const char *verboseCallSiteRemat = feGetEnv("TR_VerboseOSRCallSiteRemat");
TR::TreeTop *call = calltarget->_myCallSite->_callNodeTreeTop;
TR::ResolvedMethodSymbol *method = callStack->_methodSymbol;
TR::Compilation *comp = optimizer->comp();
TR_ByteCodeInfo &bci = method->getOSRByteCodeInfo(call->getNode());
TR::TreeTop *blockStart = call->getEnclosingBlock()->getFirstRealTreeTop();
TR::SparseBitVector scanTargets(comp->allocator());
RematSafetyInformation safetyInfo(comp);
TR::list<TR::TreeTop *> failedPP(getTypedAllocator<TR::TreeTop*>(comp->allocator()));
// Search through all of the PPS for those that can be remated
//
for (
TR::TreeTop *cursor = call->getPrevTreeTop();
cursor && method->isOSRRelatedNode(cursor->getNode(), bci);
cursor = cursor->getPrevTreeTop())
{
TR::Node *store = cursor->getNode();
if (!store->getOpCode().isStoreDirect() || !store->getSymbol()->isPendingPush())
continue;
TR::Node *child = store->getFirstChild();
// A PPS of an auto/parm. Necessary to scan to check if auto/parm has not been modified
// since it was anchored.
//
int32_t callerIndex = child->getByteCodeInfo().getCallerIndex();
if (child->getOpCode().hasSymbolReference()
&& (child->getSymbol()->isParm()
|| (child->getSymbol()->isAuto()
&& child->getSymbolReference()->getCPIndex() <
(( (callerIndex == -1) ? comp->getMethodSymbol()
: comp->getInlinedResolvedMethodSymbol(callerIndex) )->getFirstJitTempIndex()))))
{
if (comp->trace(OMR::inlining))
traceMsg(comp, "callSiteRemat: found potential pending push #%d with store #%d\n", store->getSymbolReference()->getReferenceNumber(),
child->getSymbolReference()->getReferenceNumber());
TR::SparseBitVector symRefsToCheck(comp->allocator());
symRefsToCheck[child->getSymbolReference()->getReferenceNumber()] = true;
scanTargets[child->getGlobalIndex()] = true;
safetyInfo.add(cursor, symRefsToCheck);
}
// Storing failures, will search for a double store that occurs before
//
else
{
if (comp->trace(OMR::inlining))
traceMsg(comp, "callSiteRemat: failed to find store for pending push #%d\n", store->getSymbolReference()->getReferenceNumber());
failedPP.push_back(cursor);
}
}
// Perform search for any double stores
// This goes from the start of the block to the call, as PPs may store
// duplicate values
//
if (failedPP.size() > 0)
RematTools::walkTreeTopsCalculatingRematFailureAlternatives(comp,
blockStart, call, failedPP, scanTargets, safetyInfo, verboseCallSiteRemat != NULL);
// Perform the safety check, to ensure symrefs haven't been
// modified.
//
TR::SparseBitVector unsafeSymRefs(comp->allocator());
if (!scanTargets.IsZero())
RematTools::walkTreesCalculatingRematSafety(comp, blockStart,
call, scanTargets, unsafeSymRefs, verboseCallSiteRemat != NULL);
// Perform place those without unsafe symrefs in the remat table
//
for (uint32_t i = 0; i < safetyInfo.size(); ++i)
{
TR::TreeTop *storeTree = safetyInfo.argStore(i);
TR::TreeTop *rematTree = safetyInfo.rematTreeTop(i);
TR::Node *node = rematTree->getNode();
TR::Node *child = node->getFirstChild();
if (!unsafeSymRefs.Intersects(safetyInfo.symRefDependencies(i)))
{
if (storeTree == rematTree)
{
if (comp->trace(OMR::inlining))
traceMsg(comp, "callSiteRemat: adding pending push #%d with store #%d to remat table\n",
storeTree->getNode()->getSymbolReference()->getReferenceNumber(),
child->getSymbolReference()->getReferenceNumber());
comp->setOSRCallSiteRemat(comp->getCurrentInlinedSiteIndex(),
storeTree->getNode()->getSymbolReference(),
child->getSymbolReference());
}
else
{
int32_t callerIndex = node->getByteCodeInfo().getCallerIndex();
if (node->getSymbol()->isParm()
|| node->getSymbol()->isPendingPush()
|| (node->getSymbol()->isAuto()
&& node->getSymbolReference()->getCPIndex() <
(( (callerIndex == -1) ? comp->getMethodSymbol()
: comp->getInlinedResolvedMethodSymbol(callerIndex) )->getFirstJitTempIndex())))
{
if (comp->trace(OMR::inlining))
traceMsg(comp, "callSiteRemat: adding pending push #%d with store #%d to remat table\n",
storeTree->getNode()->getSymbolReference()->getReferenceNumber(),
node->getSymbolReference()->getReferenceNumber());
comp->setOSRCallSiteRemat(comp->getCurrentInlinedSiteIndex(),
storeTree->getNode()->getSymbolReference(),
node->getSymbolReference());
}
}
}
}
}
bool TR_InlinerBase::inlineCallTarget(TR_CallStack *callStack, TR_CallTarget *calltarget, bool inlinefromgraph, TR_PrexArgInfo *argInfo, TR::TreeTop** cursorTreeTop)
{
TR_InlinerDelimiter delimiter(tracer(),"TR_InlinerBase::inlineCallTarget");
const char *sig = "multiLeafArrayCopy";
if (strncmp(calltarget->_calleeMethod->nameChars(), sig, strlen(sig)) == 0)
{
_nodeCountThreshold = 8192;
heuristicTrace(tracer(),"Setting _nodeCountThreshold to %d for multiLeafArrayCopy",_nodeCountThreshold);
}
if (!((TR_J9InlinerPolicy* )getPolicy())->doCorrectnessAndSizeChecksForInlineCallTarget(callStack, calltarget, inlinefromgraph, argInfo))
{
//@TODO do we need to undo _nodeCountThreshold???!
return false;
}
// Last chance to improve our prex info
//
if (!calltarget->_prexArgInfo)
calltarget->_prexArgInfo = getUtil()->computePrexInfo(calltarget);
argInfo = TR_PrexArgInfo::enhance(calltarget->_prexArgInfo, argInfo, comp());
calltarget->_prexArgInfo = argInfo;
bool tracePrex = comp()->trace(OMR::inlining) || comp()->trace(OMR::invariantArgumentPreexistence);
if (tracePrex && argInfo)
{
traceMsg(comp(), "Final prex argInfo:\n");
argInfo->dumpTrace();
}
if (!comp()->incInlineDepth(calltarget->_calleeSymbol,
calltarget->_myCallSite->_callNode,
!calltarget->_myCallSite->_isIndirectCall,
calltarget->_guard,
calltarget->_receiverClass,
argInfo))
{
return false;
}
//OSR
int32_t numLivePendingPushSlots = 0;
int32_t nestingDepth = 0;
if (comp()->getOption(TR_EnableOSR))
{
computeNumLivePendingSlotsAndNestingDepth(_optimizer, calltarget, callStack, numLivePendingPushSlots, nestingDepth);
}
// Add the pending pushes above this call to the OSRCallSiteRematTable
if (comp()->getOption(TR_EnableOSR)
&& !comp()->getOption(TR_DisableOSRCallSiteRemat)
&& comp()->getOSRMode() == TR::voluntaryOSR
&& comp()->isOSRTransitionTarget(TR::postExecutionOSR)
&& comp()->isPotentialOSRPointWithSupport(calltarget->_myCallSite->_callNodeTreeTop)
&& performTransformation(comp(), "O^O CALL SITE REMAT: populate OSR call site remat table for call [%p]\n", calltarget->_myCallSite->_callNode))
{
if (comp()->trace(OMR::inlining))
traceMsg(comp(), "callSiteRemat: populating OSR call site remat table for call [%p]\n", calltarget->_myCallSite->_callNode);
populateOSRCallSiteRematTable(_optimizer, calltarget, callStack);
}
bool successful = inlineCallTarget2(callStack, calltarget, cursorTreeTop, inlinefromgraph, 99);
// if inlining fails, we need to tell decInlineDepth to remove elements that
// we added during inlineCallTarget2
comp()->decInlineDepth(!successful);
if (comp()->getOption(TR_EnableOSR))
{
comp()->incNumLivePendingPushSlots(-numLivePendingPushSlots);
comp()->incNumLoopNestingLevels(-nestingDepth);
}
if (NumInlinedMethods != NULL)
{
NumInlinedMethods[comp()->getMethodHotness()]++;
InlinedSizes[comp()->getMethodHotness()] += TR::Compiler->mtd.bytecodeSize(calltarget->_calleeSymbol->getResolvedMethod()->getPersistentIdentifier());
}
return successful;
}
TR_ResolvedMethod* TR_J9VirtualCallSite::findSingleJittedImplementer (TR_InlinerBase *inliner)
{
return comp()->getPersistentInfo()->getPersistentCHTable()->findSingleJittedImplementer(_receiverClass,TR::Compiler->cls.isInterfaceClass(comp(), _receiverClass) ? _cpIndex : _vftSlot,_callerResolvedMethod, comp(), _initialCalleeSymbol);
}
bool TR_J9VirtualCallSite::findCallSiteForAbstractClass(TR_InlinerBase* inliner)
{
TR_PersistentCHTable *chTable = comp()->getPersistentInfo()->getPersistentCHTable();
TR_ResolvedMethod *implementer;
bool canInline = (!comp()->compileRelocatableCode() || comp()->getOption(TR_UseSymbolValidationManager));
if (canInline && TR::Compiler->cls.isAbstractClass(comp(), _receiverClass) &&!comp()->getOption(TR_DisableAbstractInlining) &&
(implementer = chTable->findSingleAbstractImplementer(_receiverClass, _vftSlot, _callerResolvedMethod, comp())))
{
heuristicTrace(inliner->tracer(),"Found a single Abstract Implementer %p, signature = %s",implementer,inliner->tracer()->traceSignature(implementer));
TR_VirtualGuardSelection *guard = new (comp()->trHeapMemory()) TR_VirtualGuardSelection(TR_AbstractGuard, TR_MethodTest);
addTarget(comp()->trMemory(),inliner,guard,implementer,_receiverClass,heapAlloc);
return true;
}
return false;
}
TR_OpaqueClassBlock* TR_J9VirtualCallSite::getClassFromMethod ()
{
return _initialCalleeMethod->classOfMethod();
}
// Ensure the call site is a basic invokevirtual; the bytecode is an 'invokevirtaul' and the
// cpIndex is the same as the call site _cpIndex. This will insure that the call site is not
// some type of transformed call site that may not be a valid case for allowing an isInstanceOf()
// call during AOT compiles
bool TR_J9VirtualCallSite::isBasicInvokeVirtual()
{
TR_OpaqueMethodBlock *method = ((TR_ResolvedJ9Method*)_initialCalleeMethod->owningMethod())->getPersistentIdentifier();
int32_t methodSize = TR::Compiler->mtd.bytecodeSize(method);
uintptr_t methodStart = TR::Compiler->mtd.bytecodeStart(method);
TR_ASSERT_FATAL(_bcInfo.getByteCodeIndex() >= 0 && _bcInfo.getByteCodeIndex()+2 < methodSize, "Bytecode index can't be less than zero or higher than the methodSize");
uint8_t *pc = (uint8_t *)(methodStart + _bcInfo.getByteCodeIndex());
TR_J9ByteCode bytecode = TR_J9ByteCodeIterator::convertOpCodeToByteCodeEnum(*pc);
//fprintf( stderr, "method %p, size %d, start %p, PC %p, BC: %d==%d? (%d)\n", method, methodSize, methodStart, pc, bytecode, J9BCinvokevirtual, (bytecode==J9BCinvokevirtual));
if (bytecode==J9BCinvokevirtual)
{
uint16_t cpIndex = *(uint16_t*)(pc + 1);
//fprintf( stderr, "BC cpIndex %d, callSite cpIndex %d\n", cpIndex, _cpIndex );
if (_cpIndex==cpIndex)
{
return true;
}
}
return false;
}
bool TR_J9VirtualCallSite::findCallSiteTarget(TR_CallStack *callStack, TR_InlinerBase* inliner)
{
if (hasFixedTypeArgInfo())
{
bool result = findCallTargetUsingArgumentPreexistence(inliner);
if (!result) //findCallTargetUsingArgumentPreexistence couldn't reconcile class types
{
heuristicTrace(inliner->tracer(), "Don't inline anything at the risk of inlining dead code");
return false;
}
if (numTargets()) //findCallTargetUsingArgumentPreexistence added a target
{
return true;
}
//findCallTargetUsingArgumentPreexistence couldn't use argInfo
//Clear _ecsPrexArgInfo so it isn't propagated down to callees of this callsite
//And try other techniques
_ecsPrexArgInfo->set(0, NULL);
}
tryToRefineReceiverClassBasedOnResolvedTypeArgInfo(inliner);
// Refine receiver class based on CP class
// When we have an invokevirtual on an abstract method defined in an interface class,
// the call site's class will be more concrete than class of method.
// This happens when an abstract class implements an interface class without providing
// implementation for the given method, and the call site is refering to the method of
// the abstract class, the cp entry of the method ref will be resolved to j9method of
// the interface class. However, the class ref from cp will be resolved to the abstract
// class, which is more concrete
//
if (_cpIndex != -1 && _receiverClass && TR::Compiler->cls.isInterfaceClass(comp(), _receiverClass) && isBasicInvokeVirtual())
{
TR_ResolvedMethod* owningMethod = _initialCalleeMethod->owningMethod();
TR_ResolvedJ9Method* j9OwningMethod = (TR_ResolvedJ9Method*)owningMethod;
int32_t nameLen=0, sigLen=0;
char *cpMethodName = j9OwningMethod->getMethodNameFromConstantPool(_cpIndex, nameLen);
char *cpMethodSig = j9OwningMethod->getMethodSignatureFromConstantPool(_cpIndex, sigLen);
char *methodName = _initialCalleeMethod->nameChars();
char *methodSig = _initialCalleeMethod->signatureChars();
if (nameLen && nameLen == _initialCalleeMethod->nameLength() && sigLen && sigLen == _initialCalleeMethod->signatureLength() &&
strncmp(cpMethodName, methodName, nameLen)==0 && strncmp(cpMethodSig, methodSig, sigLen)==0)
{
int32_t classRefCPIndex = owningMethod->classCPIndexOfMethod(_cpIndex);
TR_OpaqueClassBlock* callSiteClass = owningMethod->getClassFromConstantPool(comp(), classRefCPIndex, true);
if (callSiteClass && callSiteClass != _receiverClass)
{
if (comp()->fej9()->isJavaLangObject(callSiteClass))
_isCallingObjectMethod = TR_yes;
else
{
// Verify subtyping against the defining interface rather than
// _receiverClass, since the latter could have been refined to a
// more specific interface.
TR_OpaqueClassBlock *defInterface = getClassFromMethod();
TR_YesNoMaybe callSiteClassOk = fe()->isInstanceOf(
callSiteClass, defInterface, true, true, true);
TR_ASSERT_FATAL(
callSiteClassOk == TR_yes,
"class %p inherits a method from interface %p without implementing it",
callSiteClass,
defInterface);
_isCallingObjectMethod = TR_no;
if (comp()->trace(OMR::inlining))
{
char* oldClassSig = TR::Compiler->cls.classSignature(comp(), _receiverClass, comp()->trMemory());
char* callSiteClassSig = TR::Compiler->cls.classSignature(comp(), callSiteClass, comp()->trMemory());
traceMsg(comp(), "Receiver type %p sig %s is class of an interface method for invokevirtual, improve it to call site receiver type %p sig %s\n", _receiverClass, oldClassSig, callSiteClass, callSiteClassSig);
}
// Update receiver class
_receiverClass = callSiteClass;
}
}
}
}
if (addTargetIfMethodIsNotOverriden(inliner) ||
addTargetIfMethodIsNotOverridenInReceiversHierarchy(inliner) ||
findCallSiteForAbstractClass(inliner) ||
addTargetIfThereIsSingleImplementer(inliner))
{
return true;
}
return findProfiledCallTargets(callStack, inliner);
}
/*
static TR_ResolvedMethod * findSingleImplementer(
TR_OpaqueClassBlock * thisClass, int32_t cpIndexOrVftSlot, TR_ResolvedMethod * callerMethod, TR::Compilation * comp, bool locked, TR_YesNoMaybe useGetResolvedInterfaceMethod)
{
if (comp->getOption(TR_DisableCHOpts))
return 0;
TR_PersistentClassInfo * classInfo = comp->getPersistentInfo()->getPersistentCHTable()->findClassInfoAfterLocking(thisClass, comp, true);
if (!classInfo)
{
return 0;
}
TR_ResolvedMethod *implArray[2]; // collect maximum 2 implementers if you can
int32_t implCount = TR_ClassQueries::collectImplementorsCapped(classInfo, implArray, 2, cpIndexOrVftSlot, callerMethod, comp, locked, useGetResolvedInterfaceMethod);
return (implCount == 1 ? implArray[0] : 0);
}
*/
bool TR_J9InterfaceCallSite::findCallSiteTarget (TR_CallStack *callStack, TR_InlinerBase* inliner)
{
// First make sure that we can get the interface named at this call site. It
// may be missing in AOT, since it comes from getClassFromSignature(), which
// needs validation, and rememberClass() can fail at any time. The interface
// is necessary for safety conditions in findCallSiteTargetImpl() and also
// for the assertions below.
TR_OpaqueClassBlock *iface = getClassFromMethod();
if (iface == NULL)
return false;
bool result = findCallSiteTargetImpl(callStack, inliner, iface);
// A passing vgnop-based interface guard can guarantee the receiver type is
// as expected by the inlined body, but only if we already know before the
// guard that the receiver implements the expected interface. Here, an
// interface type bound is insufficient to know that, because bytecode
// verification does not guarantee any particular receiver type at
// invokeinterface.
//
// As such, in the absence of a class (i.e. non-interface) type bound on the
// receiver, all targets must use profiled guard. Additionally, the profiled
// guard must ensure on the hot side that the receiver is an instance of the
// interface expected at this call site.
//
// These requirements could be relaxed in the future by generating a type
// check against the interface type at the beginning of the inlined body
// (taking care to ensure that the exception successors are the same as
// those of the call, rather than those of the first block of the callee).
//
// ZEROCHK jitThrowIncompatibleClassChangeError
// instanceof
// <receiver>
// loadaddr <interface>
//
// In particular, such a type check would allow the following cases:
//
// - A default method inlined using a nonoverridden guard. Currently,
// default methods are never inlined (at interface call sites) because
// TR_ResolvedJ9Method::getResolvedInterfaceMethod() does not return them.
//
// - A profiled guard with method test for a method defined by a class that
// does not implement the expected interface. Subtypes may still implement
// the interface.
//
// If/when we want to start inlining in one or both of these cases, the
// assertions below can be relaxed accordingly. However, testing instanceof
// against the interface will be expensive, so there would have to be a
// considerable benefit to the inlining to motivate such a change.
//
if (_receiverClass != NULL
&& !TR::Compiler->cls.isInterfaceClass(comp(), _receiverClass))
{
TR_ASSERT_FATAL(
fe()->isInstanceOf(_receiverClass, iface, true, true, true) == TR_yes,
"interface call site %p receiver type %p "
"does not implement the expected interface %p",
this,
_receiverClass,
iface);
heuristicTrace(
inliner->tracer(),
"Interface call site %p has receiver class bound %p; nop guards ok",
this,
_receiverClass);
}
else
{
TR_Debug *debug = comp()->getDebug();
for (int32_t i = 0; i < numTargets(); i++)
{
TR_CallTarget *tgt = getTarget(i);
TR_VirtualGuardKind kind = tgt->_guard->_kind;
TR_ASSERT_FATAL(
kind == TR_ProfiledGuard,
"interface call site %p requires profiled guard (kind %d), "
"but target %d [%p] uses %s (kind %d)",
this,
(int)TR_ProfiledGuard,
i,
tgt,
debug == NULL ? "<unknown name>" : debug->getVirtualGuardKindName(kind),
(int)kind);
// Bound on the receiver types that pass the profiled guard
TR_OpaqueClassBlock *passClass = NULL;
TR_ResolvedMethod *callee = tgt->_calleeMethod;
if (tgt->_guard->_type == TR_VftTest)
passClass = tgt->_receiverClass;
else
passClass = callee->containingClass();
TR_ASSERT_FATAL(
fe()->isInstanceOf(passClass, iface, true, true, true) == TR_yes,
"interface call site %p target %d [%p] (J9Method %p) "
"accepts receivers of type %p, "
"which does not implement the expected interface %p",
this,
i,
tgt,
callee->getPersistentIdentifier(),
passClass,
iface);
}
}
return result;
}
bool TR_J9InterfaceCallSite::findCallSiteTargetImpl(
TR_CallStack *callStack, TR_InlinerBase *inliner, TR_OpaqueClassBlock *iface)
{
TR_ASSERT_FATAL(iface != NULL, "no declaring interface");
static char *minimizedInlineJIT = feGetEnv("TR_JITInlineMinimized");
if (minimizedInlineJIT)
return false;
if (hasFixedTypeArgInfo())
{
bool result = findCallTargetUsingArgumentPreexistence(inliner);
if (!result) //findCallTargetUsingArgumentPreexistence couldn't reconcile class types
{
heuristicTrace(inliner->tracer(), "Don't inline anything at the risk of inlining dead code");
return false;
}
if (numTargets()) //findCallTargetUsingArgumentPreexistence added a target
{
return true;
}
//findCallTargetUsingArgumentPreexistence couldn't use argInfo
//Clear _ecsPrexArgInfo so it wont be propagated down to callees of this callsite
//And try other techniques
_ecsPrexArgInfo->set(0, NULL);
}
if (!_receiverClass)
{
int32_t len = _interfaceMethod->classNameLength();
char * s = TR::Compiler->cls.classNameToSignature(_interfaceMethod->classNameChars(), len, comp());
_receiverClass = comp()->fej9()->getClassFromSignature(s, len, _callerResolvedMethod, true);
}
//TR_OpaqueClassBlock* _receiverClass = NULL;
tryToRefineReceiverClassBasedOnResolvedTypeArgInfo(inliner);
//TR_ResolvedMethod* calleeResolvedMethod = inliner->findInterfaceImplementationToInline(_interfaceMethod, _cpIndex, _callerResolvedMethod, _receiverClass);
TR_ResolvedMethod* calleeResolvedMethod = comp()->getPersistentInfo()->getPersistentCHTable()->findSingleImplementer(_receiverClass, _cpIndex, _callerResolvedMethod, inliner->comp(), false, TR_yes);
if (!comp()->performVirtualGuardNOPing() || (comp()->compileRelocatableCode() && !TR::Options::getCmdLineOptions()->allowRecompilation()))
{
calleeResolvedMethod = NULL;
}
heuristicTrace(inliner->tracer(), "Found a Single Interface Implementer with Resolved Method %p for callsite %p",calleeResolvedMethod,this);
if (calleeResolvedMethod && !calleeResolvedMethod->virtualMethodIsOverridden())
{
TR_VirtualGuardKind kind = TR_ProfiledGuard;
TR_VirtualGuardTestType testType = TR_DummyTest;
TR_OpaqueClassBlock *thisClass = _receiverClass;
if (_receiverClass != NULL
&& !TR::Compiler->cls.isInterfaceClass(comp(), _receiverClass))
{
kind = TR_InterfaceGuard;
testType = TR_MethodTest;
}
else
{
// Whether to try to choose VFT test based on a non-extended defClass
// or based on profiling. This does not affect the final defClass
// heuristic because in that case we can be certain that the class
// won't be extended later.
bool useVftTestHeuristics = true;
if (comp()->compileRelocatableCode() && !comp()->getOption(TR_UseSymbolValidationManager))
{
useVftTestHeuristics = false;
}
else if (TR::Compiler->vm.isVMInStartupPhase(comp()->fej9()->getJ9JITConfig()))
{
const static bool useVftTestHeuristicsDuringStartup =
feGetEnv("TR_useInterfaceVftTestHeuristicsDuringStartup") != NULL;
useVftTestHeuristics = useVftTestHeuristicsDuringStartup;
}
// Profiled guards must guarantee that passing receivers are instances
// of some class that implements the expected interface. See the
// comment in TR_J9InterfaceCallSite::findCallSiteTarget().
//
// The choice of VFT test vs. method test is irrelevant here, as
// either one would accept instances of defClass. However, a VFT test
// obtained by consulting the profiled receiver types would work.
//
TR_OpaqueClassBlock *defClass = calleeResolvedMethod->containingClass();
thisClass = defClass;
if (fe()->isInstanceOf(defClass, iface, true, true, true) != TR_yes)
{
calleeResolvedMethod = NULL; // hope to get a VFT test from profiling
}
// Heuristically choose between VFT test and method test. VFT test
// is cheaper, but method test can potentially allow the inlined
// body to be run for more receivers.
else if (TR::Compiler->cls.isClassFinal(comp(), defClass))
{
testType = TR_VftTest; // method test will never help
}
else if (useVftTestHeuristics && !fe()->classHasBeenExtended(defClass))
{
// Hope that defClass won't be extended in the future, or if it is,
// that its subtypes will override the inlined method anyway.
testType = TR_VftTest;
}
else
{
// There's already at least one subclass inheriting the single
// implementation. Choose method test because it covers the
// defining class and (so far) all of its subclasses. (If there
// were a subclass with its own override, then calleeResolvedMethod
// would be overridden.)
testType = TR_MethodTest;
// Still consult the profiling though, since it might reveal that
// one type is overwhelmingly frequent at this call site. In that
// case, change back to VFT test.
TR_ValueProfileInfoManager *profMgr =
TR_ValueProfileInfoManager::get(comp());
TR_AddressInfo *valueInfo = NULL;
if (profMgr != NULL)
{
valueInfo = static_cast<TR_AddressInfo*>(
profMgr->getValueInfo(_bcInfo, comp(), AddressInfo));
}
if (useVftTestHeuristics
&& valueInfo != NULL
&& !comp()->getOption(TR_DisableProfiledInlining))
{
TR_ASSERT_FATAL(!comp()->compileRelocatableCode() || comp()->getOption(TR_UseSymbolValidationManager),
"Cannot use VFT Test Heuristics in non-SVM AOT!\n");
TR_ScratchList<TR_ExtraAddressInfo> byFreqDesc(comp()->trMemory());
valueInfo->getSortedList(comp(), &byFreqDesc);
ListIterator<TR_ExtraAddressInfo> it(&byFreqDesc);
uint32_t remainingTotalFreq = valueInfo->getTotalFrequency();
TR_OpaqueClassBlock *topProfiledClass = NULL;
uint32_t topProfiledFreq = 0;
{
TR::HeuristicRegion heuristicRegion(comp());
TR::PersistentInfo *persistInfo = comp()->getPersistentInfo();
TR_ExtraAddressInfo *cur = it.getFirst();
for (; cur != NULL; cur = it.getNext())
{
auto *curClass =
reinterpret_cast<TR_OpaqueClassBlock*>(cur->_value);
if (persistInfo->isObsoleteClass(curClass, comp()->fe())
|| fe()->isInstanceOf(curClass, iface, true, true, true) != TR_yes)
{
remainingTotalFreq -= cur->_frequency;
}
else if (topProfiledClass == NULL)
{
topProfiledClass = curClass;
topProfiledFreq = cur->_frequency;
}
}
}
if (topProfiledClass != NULL
&& remainingTotalFreq >= 32
&& topProfiledFreq == remainingTotalFreq)
{
bool valid = true;
if (comp()->compileRelocatableCode())
{
TR::SymbolValidationManager *svm = comp()->getSymbolValidationManager();
valid = svm->addProfiledClassRecord(topProfiledClass)
&& svm->addClassInstanceOfClassRecord(topProfiledClass, iface, true, true, true);
}
if (valid)
{
testType = TR_VftTest;
thisClass = topProfiledClass;
}
}
}
}
}
if (calleeResolvedMethod != NULL)
{
TR_ASSERT_FATAL(testType != TR_DummyTest, "failed to select a guard test type");
TR_VirtualGuardSelection *guard =
new (comp()->trHeapMemory()) TR_VirtualGuardSelection(
kind, testType, thisClass);
if (kind == TR_ProfiledGuard)
{
// Almost all bytecode would pass type checking even including
// interface types. So even though this can't be a nop guard
// (because verification doesn't check interface types), treat it
// as much like a nop guard as possible. In particular, this will
// ensure that the block containing the cold call is actually