-
Notifications
You must be signed in to change notification settings - Fork 392
/
OMRResolvedMethodSymbol.cpp
2534 lines (2248 loc) · 101 KB
/
OMRResolvedMethodSymbol.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 (c) 2000, 2021 IBM Corp. and others
*
* 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 http://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] http://openjdk.java.net/legal/assembly-exception.html
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception
*******************************************************************************/
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "codegen/CodeGenerator.hpp"
#include "env/FrontEnd.hpp"
#include "codegen/Linkage.hpp"
#include "codegen/Linkage_inlines.hpp"
#include "codegen/LinkageConventionsEnum.hpp"
#include "codegen/RecognizedMethods.hpp"
#include "compile/Compilation.hpp"
#include "compile/Method.hpp"
#include "compile/OSRData.hpp"
#include "compile/ResolvedMethod.hpp"
#include "compile/SymbolReferenceTable.hpp"
#include "control/Options.hpp"
#include "control/Options_inlines.hpp"
#include "env/TRMemory.hpp"
#include "env/jittypes.h"
#include "env/CompilerEnv.hpp"
#include "env/VerboseLog.hpp"
#include "il/AutomaticSymbol.hpp"
#include "il/Block.hpp"
#include "il/DataTypes.hpp"
#include "il/ILOpCodes.hpp"
#include "il/ILOps.hpp"
#include "il/MethodSymbol.hpp"
#include "il/Node.hpp"
#include "il/Node_inlines.hpp"
#include "il/ParameterSymbol.hpp"
#include "il/RegisterMappedSymbol.hpp"
#include "il/ResolvedMethodSymbol.hpp"
#include "il/Symbol.hpp"
#include "il/SymbolReference.hpp"
#include "il/TreeTop.hpp"
#include "il/TreeTop_inlines.hpp"
#include "ilgen/IlGen.hpp"
#include "ilgen/IlGenRequest.hpp"
#include "infra/Array.hpp"
#include "infra/Assert.hpp"
#include "infra/BitVector.hpp"
#include "infra/Cfg.hpp"
#include "infra/Flags.hpp"
#include "infra/List.hpp"
#include "infra/Random.hpp"
#include "infra/CfgEdge.hpp"
#include "infra/CfgNode.hpp"
#include "optimizer/Optimizer.hpp"
#include "ras/Debug.hpp"
#include "runtime/Runtime.hpp"
#include "infra/SimpleRegex.hpp"
TR::ResolvedMethodSymbol *
OMR::ResolvedMethodSymbol::self()
{
return static_cast<TR::ResolvedMethodSymbol *>(this);
}
template <typename AllocatorType>
TR::ResolvedMethodSymbol * OMR::ResolvedMethodSymbol::create(AllocatorType m, TR_ResolvedMethod * rm, TR::Compilation * comp)
{
return new (m) TR::ResolvedMethodSymbol(rm,comp);
}
static
TR::TreeTop *findEndTreeTop(TR::ResolvedMethodSymbol *rms)
{
TR::TreeTop *end = NULL;
for (TR::TreeTop *tt = rms->getFirstTreeTop(); tt; tt = tt->getNextTreeTop())
{
tt = tt->getNode()->getBlock()->getExit();
end = tt;
}
return end;
}
void
OMR::ResolvedMethodSymbol::initForCompilation(TR::Compilation *comp)
{
TR::Region &heapRegion = comp->trMemory()->heapMemoryRegion();
self()->getParameterList().setRegion(heapRegion);
self()->getAutomaticList().setRegion(heapRegion);
self()->getVariableSizeSymbolList().setRegion(heapRegion);
}
OMR::ResolvedMethodSymbol::ResolvedMethodSymbol(TR_ResolvedMethod * method, TR::Compilation * comp)
: TR::MethodSymbol(TR_Private, method->convertToMethod()),
_resolvedMethod(method),
_flowGraph(0),
_unimplementedOpcode(0),
_firstTreeTop(0),
_autoSymRefs(0),
_pendingPushSymRefs(0),
_parmSymRefs(0),
_osrPoints(comp->trMemory()),
_tempIndex(-1),
_arrayCopyTempSlot(-1),
_syncObjectTemp(0),
_thisTempForObjectCtor(0),
_atcDeferredCountTemp(0),
_automaticList(comp->trMemory()),
_parameterList(comp->trMemory()),
_variableSizeSymbolList(comp->trMemory()),
_comp(comp),
_firstJitTempIndex(-1),
_cannotAttemptOSR(NULL),
_properties(0),
_variantParms(NULL),
_bytecodeProfilingOffsets(comp->allocator())
{
_flags.setValue(KindMask, IsResolvedMethod);
if (comp->isGPUCompileCPUCode())
{
self()->setLinkage(TR_System);
}
_methodIndex = comp->addOwningMethod(self());
if (comp->getOption(TR_TraceMethodIndex))
traceMsg(comp, "-- New symbol for method: M%p index: %d owningMethod: M%p sig: %s\n",
method, (int)_methodIndex.value(), method->owningMethod(), self()->signature(comp->trMemory()));
if (_methodIndex >= MAX_CALLER_INDEX)
{
comp->failCompilation<TR::MaxCallerIndexExceeded>("Exceeded MAX_CALLER_INDEX");
}
if (_resolvedMethod->isSynchronized())
self()->setSynchronised();
// Set the interpreted flag for an interpreted method unless we're calling
// the method that's being jitted
//
if ((_methodIndex > JITTED_METHOD_INDEX && !_resolvedMethod->isSameMethod(comp->getJittedMethodSymbol()->getResolvedMethod()))
|| comp->isDLT()
|| comp->compileRelocatableCode())
{
if (_resolvedMethod->isInterpreted())
{
self()->setInterpreted();
self()->setMethodAddress(_resolvedMethod->resolvedMethodAddress());
}
else
{
self()->setMethodAddress(_resolvedMethod->startAddressForJittedMethod());
}
}
if (!_resolvedMethod->isJNINative())
{
if (_resolvedMethod->isNative())
{
if (!self()->isInterpreted() && _resolvedMethod->isJITInternalNative())
{
self()->setMethodAddress(_resolvedMethod->startAddressForJITInternalNativeMethod());
self()->setJITInternalNative();
}
else
{
self()->setVMInternalNative();
}
}
}
if (_resolvedMethod->isFinal())
self()->setFinal();
if (_resolvedMethod->isStatic())
self()->setStatic();
self()->setParameterList();
_properties.set(CanSkipNullChecks , self()->safeToSkipNullChecks());
_properties.set(CanSkipBoundChecks , self()->safeToSkipBoundChecks());
_properties.set(CanSkipCheckCasts , self()->safeToSkipCheckCasts());
_properties.set(CanSkipDivChecks , self()->safeToSkipDivChecks());
_properties.set(CanSkipArrayStoreChecks , self()->safeToSkipArrayStoreChecks());
_properties.set(CanSkipChecksOnArrayCopies , self()->safeToSkipChecksOnArrayCopies());
_properties.set(CanSkipZeroInitializationOnNewarrays, self()->safeToSkipZeroInitializationOnNewarrays());
}
int32_t
OMR::ResolvedMethodSymbol::getSyncObjectTempIndex()
{
int32_t delta = 0;
if (self()->comp()->getOption(TR_MimicInterpreterFrameShape))
{
delta++;
}
return self()->getFirstJitTempIndex() - delta;
}
int32_t
OMR::ResolvedMethodSymbol::getThisTempForObjectCtorIndex()
{
int32_t delta = 0;
if (self()->comp()->getOption(TR_MimicInterpreterFrameShape))
{
delta++;
}
return self()->getFirstJitTempIndex() - delta;
}
template <typename AllocatorType>
TR::ResolvedMethodSymbol *
OMR::ResolvedMethodSymbol::createJittedMethodSymbol(AllocatorType m, TR_ResolvedMethod * method, TR::Compilation * comp)
{
auto sym = new (m) TR::ResolvedMethodSymbol(method, comp);
sym->_localMappingCursor = 0;
sym->_prologuePushSlots = 0;
sym->_scalarTempSlots = 0;
sym->_objectTempSlots = 0;
sym->_flags.set(IsJittedMethod);
return sym;
}
void
bcIndexForFakeInduce(TR::Compilation* comp, int16_t* callSiteInsertionPoint,
int16_t* bcIndexInsertionPoint, char* childPath)
{
static char * fakeInduceOSR = feGetEnv("TR_fakeInduceOSR");
char* induceOSR = comp->getOptions()->getInduceOSR();
if (induceOSR == NULL)
induceOSR = fakeInduceOSR;
char signatureRegex[500];
const char * mSignature = comp->signature();
char* p = induceOSR;
if (callSiteInsertionPoint)
*callSiteInsertionPoint = (int16_t)-2;
if (bcIndexInsertionPoint)
*bcIndexInsertionPoint = (int16_t)-1;
while (p)
{
char* temp;
temp = strchr(p, ':');
//p = strstr(p, mSignature);
if (!p) break;
strncpy(signatureRegex, p, temp - p);
signatureRegex[temp-p] = '\0';
char* tempRegex = signatureRegex;
TR::SimpleRegex* regex = TR::SimpleRegex::create(tempRegex);
if (regex)
{
if (!TR::SimpleRegex::match(regex, mSignature, true))
{
traceMsg(comp, "regex not matching\n");
break;
}
}
else if (strcmp(signatureRegex, mSignature))
{
traceMsg(comp, "signature not matching\n");
break;
}
p = temp+1;
int16_t tempInt = static_cast<int16_t>(strtol(p, &temp, 10));
p = temp;
if (callSiteInsertionPoint)
*callSiteInsertionPoint = tempInt;
p++; //skipping the second ,
tempInt = static_cast<int16_t>(strtol(p, &temp, 16));
p = temp;
if (bcIndexInsertionPoint)
*bcIndexInsertionPoint = tempInt;
p++; //skipping the third ,
char* q = childPath;
while (*p != '\0' && *p != '|')
{
*q = *p;
p++; q++;
}
*q = '\0';
traceMsg(comp, "signature: %s, callSiteInsertionPoint: %d, bcIndexInsertionPoint: %x\n",
mSignature,
(callSiteInsertionPoint?*callSiteInsertionPoint:-1),
(bcIndexInsertionPoint?*bcIndexInsertionPoint:-1));
break;
}
}
bool
OMR::ResolvedMethodSymbol::canInjectInduceOSR(TR::Node* node)
{
bool trace = self()->comp()->getOption(TR_TraceOSR);
if (node->getOpCodeValue() != TR::treetop
&& node->getOpCodeValue() != TR::NULLCHK
&& node->getOpCodeValue() != TR::ResolveAndNULLCHK)
{
if (trace)
traceMsg(self()->comp(), "node doesn't have a treetop, NULLCHK, or ResolveAndNULLCHK root\n");
return false;
}
if (node->getNumChildren() != 1
|| !node->getChild(0)->getOpCode().isCall())
{
if (trace)
traceMsg(self()->comp(), "there is no call under the treetop\n");
return false;
}
TR::Node* callNode = node->getChild(0);
if (callNode->getReferenceCount() != 1 && node->getOpCodeValue() == TR::treetop)
{
if (trace)
traceMsg(self()->comp(), "call node has a refcount larger than 1 and is under a treetop\n");
return false;
}
const char* rootSignature = self()->comp()->signature();
if (!strncmp(rootSignature, "java/lang/Object.newInstancePrototype", 37))
{
if (trace)
traceMsg(self()->comp(), "root method is a java/lang/Object.newInstancePrototype method\n");
return false;
}
if (!strncmp(rootSignature, "java/lang/Class.newInstancePrototype", 36))
{
if (trace)
traceMsg(self()->comp(), "root method is a java/lang/Class.newInstancePrototype method\n");
return false;
}
const char* currentSignature = self()->signature(self()->comp()->trMemory());
if (!strncmp(currentSignature, "com/ibm/jit/JITHelpers", 22))
{
if (trace)
traceMsg(self()->comp(), "node is a com/ibm/jit/jit helper method\n");
return false;
}
TR::Symbol * sym = callNode->getSymbolReference()->getSymbol();
if (sym->isMethod())
{
TR::MethodSymbol* methSym = sym->castToMethodSymbol();
if (methSym->getMethodKind() == TR::MethodSymbol::Helper ||
methSym->isNative() ||
methSym->getMethodKind() == TR::MethodSymbol::Special)
{
if (trace)
traceMsg(self()->comp(), "node is a helper, native, or a special call\n");
return false;
}
}
if (sym->isResolvedMethod())
{
auto * methSym = sym->castToResolvedMethodSymbol();
const char* signature = methSym->signature(self()->comp()->trMemory());
if (!strncmp(signature, "com/ibm/jit/JITHelpers", 22))
{
if (trace)
traceMsg(self()->comp(), "node is a com/ibm/jit/jit helper method\n");
return false;
}
}
return true;
}
/**
* Generate and prepend an induce OSR call tree.
*
* \param insertionPoint Tree to prepend the OSR call to. It may also copy children from this tree if it is a call.
* \param numChildren The number of children to copy from the insertionPoint to the induce call.
* \param copyChildren Flag controlling whether to copy children to the induce call.
* \param shouldSplitBlock Flag controlling whether to split the block after the induce call.
* \param cfg Specify the CFG to use when splitting the block. Defaults to the outermost CFG.
*/
TR::TreeTop *
OMR::ResolvedMethodSymbol::genInduceOSRCallNode(TR::TreeTop* insertionPoint,
int32_t numChildren,
bool copyChildren,
bool shouldSplitBlock,
TR::CFG *cfg)
{
// If not specified use the outermost CFG
if (!cfg)
cfg = self()->comp()->getFlowGraph();
//Now we need to put the induceOSR call as the first treetop of its block.
//Because if we don't do that and if there is a method call before induceOSR in induceOSR's block
//and that method gets inlined
//there is no guarantee that the future block containing the induceOSR call still will have an exception
//edge to the OSR catch block.
TR::SymbolReferenceTable* symRefTab = self()->comp()->getSymRefTab();
TR::SymbolReference *induceOSRSymRef = symRefTab->findOrCreateInduceOSRSymbolRef(TR_induceOSRAtCurrentPC);
TR::Node *refNode = insertionPoint->getNode();
if (self()->comp()->getOption(TR_TraceOSR))
traceMsg(self()->comp(), "O^O OSR: Inject induceOSR call for [%p] at %3d:%d\n", refNode, refNode->getInlinedSiteIndex(), refNode->getByteCodeIndex());
TR::Block * firstHalfBlock = insertionPoint->getEnclosingBlock();
if (shouldSplitBlock)
firstHalfBlock->split(insertionPoint, cfg, true);
firstHalfBlock->setIsOSRInduceBlock();
// The arguments for a transition to this bytecode index may have already been stashed.
// If they have, use these directly rather than copying the reference node.
TR_OSRMethodData *osrMethodData = self()->comp()->getOSRCompilationData()->findOrCreateOSRMethodData(
refNode->getByteCodeInfo().getCallerIndex(), self());
TR_Array<int32_t> *stashedArgs = osrMethodData->getArgInfo(refNode->getByteCodeIndex());
int32_t firstArgIndex = 0;
if (stashedArgs)
numChildren = stashedArgs->size();
else if ((refNode->getNumChildren() > 0) &&
refNode->getFirstChild()->getOpCode().isCall())
{
refNode = refNode->getFirstChild();
if (numChildren < 0)
{
numChildren = refNode->getNumChildren();
firstArgIndex = refNode->getFirstArgumentIndex();
}
}
TR::Node *induceOSRCallNode = TR::Node::createWithSymRef(refNode, TR::call, numChildren - firstArgIndex, induceOSRSymRef);
TR_OSRPoint *point = self()->findOSRPoint(refNode->getByteCodeInfo());
TR_ASSERT(point, "Could not find osr point for bytecode %d:%d!", refNode->getByteCodeInfo().getCallerIndex(), refNode->getByteCodeInfo().getByteCodeIndex());
// If arguments have been stashed against this BCI, copy them now, ignoring the copyChildren flag
if (stashedArgs)
{
for (int32_t i = 0; i < numChildren; ++i)
induceOSRCallNode->setAndIncChild(i,
TR::Node::createLoad(induceOSRCallNode, self()->comp()->getSymRefTab()->getSymRef((*stashedArgs)[i])));
}
else if (copyChildren)
{
for (int i = firstArgIndex; i < numChildren; i++)
induceOSRCallNode->setAndIncChild(i-firstArgIndex, refNode->getChild(i));
}
else
{
induceOSRCallNode->setNumChildren(0);
}
if (self()->comp()->getOptions()->getVerboseOption(TR_VerboseOSRDetails))
TR_VerboseLog::writeLineLocked(TR_Vlog_OSRD, "Injected induceOSR call at %3d:%d in %s", refNode->getInlinedSiteIndex(), refNode->getByteCodeIndex(), self()->comp()->signature());
TR::Node *induceOSRTreeTopNode = TR::Node::create(TR::treetop, 1, induceOSRCallNode);
insertionPoint->insertBefore(TR::TreeTop::create(self()->comp(), induceOSRTreeTopNode));
return insertionPoint->getPrevTreeTop();
}
/*
* \brief Generate the helper call to request for OSR transition and recompilation of the current compiled method.
*
* \return true if the OSR transition is supported at the requested \parm induceBCI and the OSR induction helper call
* is generated successfully
*/
bool
OMR::ResolvedMethodSymbol::induceOSRAfterAndRecompile(TR::TreeTop *insertionPoint, TR_ByteCodeInfo induceBCI, TR::TreeTop* branch,
bool extendRemainder, int32_t offset, TR::TreeTop ** lastTreeTop)
{
TR_ASSERT(self()->comp()->allowRecompilation(), "request OSR and recompilation at node %p when recomp is disabled\n", insertionPoint);
TR::TreeTop *induceOSRCallTree = self()->induceOSRAfterImpl(insertionPoint, induceBCI, branch, extendRemainder, offset, lastTreeTop);
if (!induceOSRCallTree)
return false;
TR::Node *induceOSRCallNode = induceOSRCallTree->getNode()->getFirstChild();
TR::SymbolReference *symRef = induceOSRCallNode->getSymbolReference();
TR_ASSERT_FATAL(induceOSRCallNode->getOpCode().isCall() &&
symRef->getReferenceNumber() == TR_induceOSRAtCurrentPC,
"induceOSRCallNode %p (n%dn) under induceOSRCallTree %p should be a call node with TR_induceOSRAtCurrentPC helper call", induceOSRCallNode, induceOSRCallNode->getGlobalIndex(), induceOSRCallTree->getNode());
induceOSRCallNode->setSymbolReference(self()->comp()->getSymRefTab()->findOrCreateInduceOSRSymbolRef(TR_induceOSRAtCurrentPCAndRecompile));
return true;
}
/*
* \brief Generate the helper call to request for OSR transition only.
*
* \return true if the OSR transition is supported at the requested \parm induceBCI and the OSR induction helper call
* is generated successfully
*/
bool
OMR::ResolvedMethodSymbol::induceOSRAfter(TR::TreeTop *insertionPoint, TR_ByteCodeInfo induceBCI, TR::TreeTop* branch,
bool extendRemainder, int32_t offset, TR::TreeTop ** lastTreeTop)
{
return self()->induceOSRAfterImpl(insertionPoint, induceBCI, branch, extendRemainder, offset, lastTreeTop) != NULL;
}
/*
* \brief Internal implementation for generating the helper call to request for OSR transition.
*
* \return The treetop of the inserted induceOSR helper call if OSR transition is supported at the requested \parm induceBCI.
* NULL if OSR transition is not supported at the requested \parm induceBCI.
*/
TR::TreeTop *
OMR::ResolvedMethodSymbol::induceOSRAfterImpl(TR::TreeTop *insertionPoint, TR_ByteCodeInfo induceBCI, TR::TreeTop* branch,
bool extendRemainder, int32_t offset, TR::TreeTop ** lastTreeTop)
{
TR::Block *block = insertionPoint->getEnclosingBlock();
if (self()->supportsInduceOSR(induceBCI, block, self()->comp()))
{
TR::CFG *cfg = self()->comp()->getFlowGraph();
cfg->setStructure(NULL);
TR::TreeTop *remainderTree = insertionPoint->getNextTreeTop();
if (remainderTree->getNode()->getOpCodeValue() != TR::BBEnd)
{
if (extendRemainder)
{
TR::Block *remainderBlock = block->split(remainderTree, cfg, false, true);
remainderBlock->setIsExtensionOfPreviousBlock(true);
if (self()->comp()->getOption(TR_TraceOSR))
traceMsg(self()->comp(), " Split of block_%d at n%dn produced block_%d which is an extension\n", block->getNumber(), remainderTree->getNode()->getGlobalIndex(), remainderBlock->getNumber());
}
else
{
TR::Block *remainderBlock = block->split(remainderTree, cfg, true, true);
if (self()->comp()->getOption(TR_TraceOSR))
traceMsg(self()->comp(), " Split of block_%d at n%dn produced block_%d\n", block->getNumber(), remainderTree->getNode()->getGlobalIndex(), remainderBlock->getNumber());
}
}
induceBCI.setByteCodeIndex(induceBCI.getByteCodeIndex() + offset);
// create a block that will be the target of the branch with BCI for the induceOSR
TR::Block *osrBlock = TR::Block::createEmptyBlock(self()->comp(), MAX_COLD_BLOCK_COUNT);
osrBlock->setIsCold();
osrBlock->getEntry()->getNode()->setByteCodeInfo(induceBCI);
osrBlock->getExit()->getNode()->setByteCodeInfo(induceBCI);
// Load the cached last treetop if available, and store it when finished
TR::TreeTop *end = lastTreeTop && (*lastTreeTop) ? (*lastTreeTop) : cfg->findLastTreeTop();
TR_ASSERT(end->getNextTreeTop() == NULL, "The last treetop must not have a treetop after it");
end->join(osrBlock->getEntry());
if (lastTreeTop)
(*lastTreeTop) = osrBlock->getExit();
cfg->addNode(osrBlock);
cfg->addEdge(block, osrBlock);
if (self()->comp()->getOption(TR_TraceOSR))
traceMsg(self()->comp(), " Created OSR block_%d and inserting it at the end of the method\n", osrBlock->getNumber());
branch->getNode()->setBranchDestination(osrBlock->getEntry());
block->append(branch);
cfg->copyExceptionSuccessors(block, osrBlock);
// induce OSR in the new block
return self()->genInduceOSRCallAndCleanUpFollowingTreesImmediately(osrBlock->getExit(), induceBCI, false, self()->comp());
}
return NULL;
}
TR::TreeTop *
OMR::ResolvedMethodSymbol::induceImmediateOSRWithoutChecksBefore(TR::TreeTop *insertionPoint)
{
if (self()->supportsInduceOSR(insertionPoint->getNode()->getByteCodeInfo(), insertionPoint->getEnclosingBlock(), self()->comp()))
return self()->genInduceOSRCallAndCleanUpFollowingTreesImmediately(insertionPoint, insertionPoint->getNode()->getByteCodeInfo(), false, self()->comp());
if (self()->comp()->getOption(TR_TraceOSR))
traceMsg(self()->comp(), "induceImmediateOSRWithoutChecksBefore n%dn failed - supportsInduceOSR returned false\n", insertionPoint->getNode()->getGlobalIndex());
return NULL;
}
TR::TreeTop *
OMR::ResolvedMethodSymbol::genInduceOSRCallAndCleanUpFollowingTreesImmediately(TR::TreeTop *insertionPoint, TR_ByteCodeInfo induceBCI, bool shouldSplitBlock, TR::Compilation *comp)
{
TR::TreeTop *induceOSRCallTree = self()->genInduceOSRCall(insertionPoint, induceBCI.getCallerIndex(), 0, false, shouldSplitBlock);
if (induceOSRCallTree)
{
TR::TreeTop *treeAfterInduceOSRCall = induceOSRCallTree->getNextTreeTop();
while (treeAfterInduceOSRCall)
{
if (((treeAfterInduceOSRCall->getNode()->getOpCodeValue() == TR::athrow) &&
treeAfterInduceOSRCall->getNode()->throwInsertedByOSR()) ||
(treeAfterInduceOSRCall->getNode()->getOpCodeValue() == TR::BBEnd))
break;
TR::TreeTop *next = treeAfterInduceOSRCall->getNextTreeTop();
induceOSRCallTree->join(next);
treeAfterInduceOSRCall->getNode()->recursivelyDecReferenceCount();
treeAfterInduceOSRCall = next;
}
}
return induceOSRCallTree;
}
/**
* Generate and prepend an induce OSR call. This function will also modify the block to fit OSR requirements, such as
* limiting successors to only the exit block and limiting exception successors to the matching OSR catch block.
*
* \param insertionPoint Tree to prepend the OSR call to. It may also copy children from this tree if it is a call.
* \param inlinedSiteIndex The inlined site index of the desired transition target. This should match the insertion points inlined site index.
* \param numChildren The number of children to copy from the insertionPoint to the induce OSR call.
* \param copyChildren Flag controlling whether to copy children to the induce OSR call.
* \param shouldSplitBlock Flag controlling whether to split the block after the induce OSR call.
* \param cfg CFG used for the block modifications. This will default to the outermost CFG, but can be specified if adding induce OSR calls
* when it is not fully formed, such as during inlining.
*/
TR::TreeTop *
OMR::ResolvedMethodSymbol::genInduceOSRCall(TR::TreeTop* insertionPoint,
int32_t inlinedSiteIndex,
int32_t numChildren,
bool copyChildren,
bool shouldSplitBlock,
TR::CFG *cfg)
{
TR_OSRMethodData *osrMethodData = self()->comp()->getOSRCompilationData()->findOrCreateOSRMethodData(inlinedSiteIndex, self());
return self()->genInduceOSRCall(insertionPoint, inlinedSiteIndex, osrMethodData, numChildren, copyChildren, shouldSplitBlock, cfg);
}
TR::TreeTop *
OMR::ResolvedMethodSymbol::genInduceOSRCall(TR::TreeTop* insertionPoint,
int32_t inlinedSiteIndex,
TR_OSRMethodData *osrMethodData,
int32_t numChildren,
bool copyChildren,
bool shouldSplitBlock,
TR::CFG *callerCFG)
{
// If not specified use the outermost CFG
if (!callerCFG)
callerCFG = self()->comp()->getFlowGraph();
TR::Node *insertionPointNode = insertionPoint->getNode();
if (self()->comp()->getOption(TR_TraceOSR))
traceMsg(self()->comp(), "Osr point added for %p, callerIndex=%d, bcindex=%d\n",
insertionPointNode, insertionPointNode->getByteCodeInfo().getCallerIndex(),
insertionPointNode->getByteCodeInfo().getByteCodeIndex());
TR::Block * OSRCatchBlock = osrMethodData->getOSRCatchBlock();
TR::TreeTop *induceOSRCallTree = self()->genInduceOSRCallNode(insertionPoint, numChildren, copyChildren, shouldSplitBlock, callerCFG);
TR::Block *enclosingBlock = insertionPoint->getEnclosingBlock();
if (!enclosingBlock->getLastRealTreeTop()->getNode()->getOpCode().isReturn())
{
callerCFG->addEdge(enclosingBlock, callerCFG->getEnd());
for (auto nextEdge = enclosingBlock->getSuccessors().begin(); nextEdge != enclosingBlock->getSuccessors().end();)
{
if ((*nextEdge)->getTo() != callerCFG->getEnd())
callerCFG->removeEdge(*(nextEdge++));
else
++nextEdge;
}
}
for (auto nextEdge = enclosingBlock->getExceptionSuccessors().begin(); nextEdge != enclosingBlock->getExceptionSuccessors().end();)
{
TR::CFGNode *succ = (*nextEdge)->getTo();
if (succ != OSRCatchBlock)
callerCFG->removeEdge(*(nextEdge++));
else
++nextEdge;
}
TR::SymbolReference * tempSymRef = 0;
TR::Node * loadExcpSymbol = TR::Node::createWithSymRef(insertionPointNode, TR::aload, 0, self()->comp()->getSymRefTab()->findOrCreateExcpSymbolRef());
TR::TreeTop *lastTreeInEnclosingBlock = enclosingBlock->getLastRealTreeTop();
if (lastTreeInEnclosingBlock != enclosingBlock->getLastNonControlFlowTreeTop())
{
TR::TreeTop *prev = lastTreeInEnclosingBlock->getPrevTreeTop();
TR::TreeTop *next = lastTreeInEnclosingBlock->getNextTreeTop();
prev->join(next);
lastTreeInEnclosingBlock->getNode()->recursivelyDecReferenceCount();
}
enclosingBlock->append(TR::TreeTop::create(self()->comp(), TR::Node::createWithSymRef(TR::athrow, 1, 1, loadExcpSymbol, self()->comp()->getSymRefTab()->findOrCreateAThrowSymbolRef(self()))));
enclosingBlock->getLastRealTreeTop()->getNode()->setThrowInsertedByOSR(true);
bool firstOSRPoint = false;
if (self()->getOSRPoints().isEmpty())
firstOSRPoint = true;
// TODO: The late addition of OSR infrastructure based on the lack of OSR points should not be possible, as we shouldn't have been able to induce without
// an OSR point in the first place. This should be removed, as well as the genOSRHelperCall's cfg argument.
if (firstOSRPoint)
{
TR::Block *OSRCodeBlock = osrMethodData->getOSRCodeBlock();
TR::Block *OSRCatchBlock = osrMethodData->getOSRCatchBlock();
if (self()->comp()->getOption(TR_TraceOSR))
traceMsg(self()->comp(), "code %p %d catch %p %d\n", OSRCodeBlock, OSRCodeBlock->getNumber(), OSRCatchBlock, OSRCatchBlock->getNumber());
self()->getLastTreeTop()->insertTreeTopsAfterMe(OSRCatchBlock->getEntry(), OSRCodeBlock->getExit());
self()->genOSRHelperCall(inlinedSiteIndex, self()->comp()->getSymRefTab(), callerCFG);
}
self()->insertRematableStoresFromCallSites(self()->comp(), inlinedSiteIndex, induceOSRCallTree);
self()->insertStoresForDeadStackSlotsBeforeInducingOSR(self()->comp(), inlinedSiteIndex, insertionPoint->getNode()->getByteCodeInfo(), induceOSRCallTree);
if (self()->comp()->getOption(TR_TraceOSR))
traceMsg(self()->comp(), "last real tree n%dn\n", enclosingBlock->getLastRealTreeTop()->getNode()->getGlobalIndex());
return induceOSRCallTree;
}
/**
* return values:
* 0 means there is no match
* 1 means there is a match and inserting induceOSR is legal at that point
* 2 means there is a match and inserting induceOSR may not be legal at that point
*/
int
OMR::ResolvedMethodSymbol::matchInduceOSRCall(TR::TreeTop* insertionPoint,
int16_t callerIndex,
int16_t byteCodeIndex,
const char* childPath)
{
TR::Node *refNode = insertionPoint->getNode();
static char* recipProbString = feGetEnv("TR_recipProb");
int recipProb = 10;
if (recipProbString)
recipProb = atoi(recipProbString);
TR::Node *refCallNode = NULL;
if (childPath[0] == 'b' || childPath[0] == 'a')
{
//callerIndex/byteCodeIndex value -3 means a wildcard *, i.e., do it for all caller/bytecode indices
if ((callerIndex != -3 && refNode->getInlinedSiteIndex() != callerIndex) ||
(byteCodeIndex != -3 && refNode->getByteCodeIndex() != byteCodeIndex))
return 0;
if (self()->canInjectInduceOSR(refNode))
return 1;
else
{
if (childPath[0] == 'b')
return 0;
else
return 2;
}
}
else if (childPath[0] == 'r')
{
if (callerIndex == -2) return 0;
if (!self()->canInjectInduceOSR(refNode)) return 0;
int32_t random = self()->comp()->adhocRandom().getRandom();
if (self()->comp()->getOption(TR_TraceOSR))
traceMsg(self()->comp(), "Random fake induceOSR injection: caller=%d bc=%x random=%d\n", callerIndex, byteCodeIndex, random);
if (self()->comp()->adhocRandom().getRandom() % recipProb != 0) return 0;
return 1;
}
else if (childPath[0] == 'g')
{
if ((callerIndex != -3 && refNode->getInlinedSiteIndex() != callerIndex) ||
(byteCodeIndex != -3 && refNode->getByteCodeIndex() < unsigned(byteCodeIndex)))
return 0;
if (self()->canInjectInduceOSR(refNode))
return 1;
else
return 0;
}
else
return 0;
TR_ASSERT(0, "should have returned by now\n");
}
void
OMR::ResolvedMethodSymbol::genAndAttachOSRCodeBlocks(int32_t currentInlinedSiteIndex)
{
bool trace = self()->comp()->getOption(TR_TraceOSR);
TR_ASSERT(self()->getFirstTreeTop(), "the method doesn't have any trees\n");
int16_t callSiteInsertionPoint, bcIndexInsertionPoint;
char childPath[10];
childPath[0] = '\0';
bcIndexForFakeInduce(self()->comp(), &callSiteInsertionPoint, &bcIndexInsertionPoint, childPath);
const char * mSignature = self()->signature(self()->comp()->trMemory());
TR::TreeTop *lastTreeTop = NULL;
TR_OSRMethodData *osrMethodData = self()->comp()->getOSRCompilationData()->findOrCreateOSRMethodData(currentInlinedSiteIndex, self());
// Under NextGenHCR, the method entry for the outermost method is an implicit OSR point
// To ensure a transition from this point is possible, it is necessary to generate and attach
// a catch and code block now, with an exception edge to the catch block from the first block
// This exception edge should remain until OSR guards have been inserted, at which point it is no
// longer needed
//
if (self()->comp()->isOutermostMethod() && self()->comp()->getHCRMode() == TR::osr)
{
// Add an exception edge to the OSR catch block from the first block
TR::Block *OSRCatchBlock = osrMethodData->findOrCreateOSRCatchBlock(self()->getFirstTreeTop()->getNode());
TR::Block *firstBlock = self()->getFirstTreeTop()->getEnclosingBlock();
if (!firstBlock->hasExceptionSuccessor(OSRCatchBlock))
self()->comp()->getFlowGraph()->addEdge(TR::CFGEdge::createExceptionEdge(firstBlock, OSRCatchBlock, self()->comp()->trMemory()));
// Create an OSR point with bytecode index 0
TR_ByteCodeInfo firstBCI = self()->getFirstTreeTop()->getNode()->getByteCodeInfo();
firstBCI.setByteCodeIndex(0);
TR_OSRPoint *osrPoint = new (self()->comp()->trHeapMemory()) TR_OSRPoint(firstBCI, osrMethodData, self()->comp()->trMemory());
osrPoint->setOSRIndex(self()->addOSRPoint(osrPoint));
}
//Using this flag we avoid processing twice a call before which we are injecting an induceOSR
bool skipNextTT = false;
for (TR::TreeTop* tt = self()->getFirstTreeTop(); tt; tt = tt->getNextTreeTop())
{
TR::Node* ttnode = tt->getNode();
if (!skipNextTT)
{
int matchCode = self()->matchInduceOSRCall(tt, callSiteInsertionPoint, bcIndexInsertionPoint, childPath);
if (matchCode > 0)
{
TR::Node *refNode = tt->getNode()->getFirstChild();
int32_t numChildren = refNode->getNumChildren();
int32_t firstArgIndex = refNode->getFirstArgumentIndex();
self()->genInduceOSRCallNode(tt, (numChildren - firstArgIndex), matchCode == 1);
tt = tt->getPrevTreeTop();
ttnode = tt->getNode();
if (trace)
{
const char * mSignature = self()->signature(self()->comp()->trMemory());
traceMsg(self()->comp(), "fake induce %p generated for %s at callsite %d bytecode %x\n",
ttnode, mSignature, callSiteInsertionPoint, bcIndexInsertionPoint);
}
skipNextTT = true;
}
}
else
skipNextTT = false;
// check potential OSR point using a node to avoid a check for the exception successor
// which we are responsible for creating if we decide to continue with this OSR point
if (self()->comp()->isPotentialOSRPoint(tt->getNode()))
{
TR::Block * block = tt->getEnclosingBlock();
// Add the OSR point to the list
TR::Block * OSRCatchBlock = osrMethodData->findOrCreateOSRCatchBlock(ttnode);
TR_OSRPoint *osrPoint = NULL;
if (self()->comp()->isOSRTransitionTarget(TR::preExecutionOSR) || self()->comp()->requiresAnalysisOSRPoint(ttnode))
{
osrPoint = new (self()->comp()->trHeapMemory()) TR_OSRPoint(ttnode->getByteCodeInfo(), osrMethodData, self()->comp()->trMemory());
osrPoint->setOSRIndex(self()->addOSRPoint(osrPoint));
if (self()->comp()->getOption(TR_TraceOSR))
traceMsg(self()->comp(), "pre osr point added for [%p] for bci %d:%d\n",
ttnode, ttnode->getByteCodeInfo().getCallerIndex(),
ttnode->getByteCodeInfo().getByteCodeIndex());
}
if (self()->comp()->isOSRTransitionTarget(TR::postExecutionOSR))
{
TR_ByteCodeInfo offsetBCI = ttnode->getByteCodeInfo();
offsetBCI.setByteCodeIndex(offsetBCI.getByteCodeIndex() + self()->comp()->getOSRInductionOffset(ttnode));
osrPoint = new (self()->comp()->trHeapMemory()) TR_OSRPoint(offsetBCI, osrMethodData, self()->comp()->trMemory());
osrPoint->setOSRIndex(self()->addOSRPoint(osrPoint));
if (self()->comp()->getOption(TR_TraceOSR))
traceMsg(self()->comp(), "post osr point added for [%p] for bci %d:%d\n",
ttnode, offsetBCI.getCallerIndex(), offsetBCI.getByteCodeIndex());
}
// Add an exception edge from the current block to the OSR catch block if there isn't already one
auto edge = block->getExceptionSuccessors().begin();
for (; edge != block->getExceptionSuccessors().end(); ++edge)
{
TR::Block *to = toBlock((*edge)->getTo());
if (to == OSRCatchBlock)
break;
}
if (edge == block->getExceptionSuccessors().end())
self()->comp()->getFlowGraph()->addEdge( TR::CFGEdge::createExceptionEdge(block, OSRCatchBlock, self()->comp()->trMemory()));
}
if (tt->getNextTreeTop() == NULL)
lastTreeTop = tt;
}
// If OSR code and catch blocks have been created, stitch them into the
// method's trees. Check for the OSR blocks directly, since they're specific
// to the current inlined call site, unlike getOSRPoints().
TR::Block * const osrCodeBlock = osrMethodData->getOSRCodeBlock();
TR::Block * const osrCatchBlock = osrMethodData->getOSRCatchBlock();
TR_ASSERT_FATAL(
(osrCodeBlock != NULL) == (osrCatchBlock != NULL),
"either OSR code or catch block was generated without the other\n");
if (osrCodeBlock != NULL)
lastTreeTop->insertTreeTopsAfterMe(osrCatchBlock->getEntry(), osrCodeBlock->getExit());
}
static void getNeighboringSymRefLists(int32_t index, TR_Array<List<TR::SymbolReference> >* listArray,
List<TR::SymbolReference> **listForPrevSlot,
List<TR::SymbolReference> **list,
List<TR::SymbolReference> **listForNextSlot)
{
*list = &(listArray->element(index));
if (index >= 1)
*listForPrevSlot = &(listArray->element(index-1));
else
*listForPrevSlot = NULL;
if (index < listArray->lastIndex())
*listForNextSlot = &(listArray->element(index+1));
else
*listForNextSlot = NULL;
}
static bool isTwoSlotSymRefIn(List<TR::SymbolReference> *symRefList)
{
if (symRefList)
{
ListIterator<TR::SymbolReference> iter(symRefList);
for (TR::SymbolReference* symRef = iter.getFirst(); symRef; symRef = iter.getNext())
{
switch (symRef->getSymbol()->getDataType())
{
case TR::Int64:
case TR::Double:
return true;
default:
break;
}
}
}
return false;
}
bool
OMR::ResolvedMethodSymbol::sharesStackSlot(TR::SymbolReference *symRef)
{
TR_ASSERT(symRef->getSymbol()->isAutoOrParm(), "sharesStackSlot requires #%d to be on the stack", symRef->getReferenceNumber());
int32_t slot = symRef->getCPIndex();
if (slot >= self()->getFirstJitTempIndex())
return false; // Jit temps don't share slots
TR::DataType dt = symRef->getSymbol()->getDataType();
bool takesTwoSlots = dt == TR::Int64 || dt == TR::Double;
List<TR::SymbolReference> *listForPrevSlot, *list, *listForNextSlot;
if (slot < 0)
getNeighboringSymRefLists(-slot - 1, self()->getPendingPushSymRefs(), &listForPrevSlot, &list, &listForNextSlot);
else
getNeighboringSymRefLists(slot, self()->getAutoSymRefs(), &listForPrevSlot, &list, &listForNextSlot);
if (list->isMultipleEntry())
return true;
else if (isTwoSlotSymRefIn(listForPrevSlot))
return true;
else if (takesTwoSlots && listForNextSlot && !listForNextSlot->isEmpty())
return true;
else
return false;
}
void
OMR::ResolvedMethodSymbol::genOSRHelperCall(int32_t currentInlinedSiteIndex, TR::SymbolReferenceTable* symRefTab, TR::CFG *cfg)
{
// If not specified use the outermost CFG
if (!cfg)
cfg = self()->comp()->getFlowGraph();
bool trace = self()->comp()->getOption(TR_TraceOSR);
// Use first node of the method for bytecode info
TR_ASSERT(self()->getFirstTreeTop(), "first tree top is NULL in %s", self()->signature(self()->comp()->trMemory()));
TR::Node *firstNode = self()->getFirstTreeTop()->getNode();
// Create the OSR helper symbol reference
TR::Node *vmThread = TR::Node::createWithSymRef(firstNode, TR::loadaddr, 0, new (self()->comp()->trHeapMemory()) TR::SymbolReference(symRefTab, TR::RegisterMappedSymbol::createMethodMetaDataSymbol(self()->comp()->trHeapMemory(), "vmThread")));
TR::SymbolReference *osrHelper = symRefTab->findOrCreateRuntimeHelper(TR_prepareForOSR, false, false, true);
#if defined(TR_TARGET_X86) || defined(TR_HOST_POWER) || defined(TR_HOST_S390) || defined(TR_HOST_ARM) || defined(TR_HOST_ARM64)
osrHelper->getSymbol()->castToMethodSymbol()->setLinkage(TR_System);
#else
osrHelper->getSymbol()->castToMethodSymbol()->setPreservesAllRegisters();
osrHelper->getSymbol()->castToMethodSymbol()->setSystemLinkageDispatch();
#endif
TR_OSRMethodData *osrMethodData = self()->comp()->getOSRCompilationData()->findOrCreateOSRMethodData(currentInlinedSiteIndex, self());
// Create children to the OSR helper call node and save them to a list
TR_Array<TR::Node*> loadNodes(self()->comp()->trMemory());
loadNodes.add(vmThread);
loadNodes.add(TR::Node::iconst(firstNode, osrMethodData->getInlinedSiteIndex()));
TR::Node *loadNode = NULL;
bool alreadyLoadedSyncObjectTemp = false;
bool alreadyLoadedThisTempForObjectCtor = false;
// Pending push temporaries