-
Notifications
You must be signed in to change notification settings - Fork 139
/
Copy pathInliner.hpp
795 lines (658 loc) · 37.2 KB
/
Inliner.hpp
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
/*******************************************************************************
* 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
*******************************************************************************/
///////////////////////////////////////////////////////////////////////////
// Inliner.hpp contains the class definitions for:
// TR_Inliner
// TR_TransformInlinedFunction
// TR_ParameterToArgumentMapper
// TR_HandleInjectedBasicBlock
///////////////////////////////////////////////////////////////////////////
#ifndef INLINER_INCL
#define INLINER_INCL
#include "optimizer/CallInfo.hpp"
#include <stddef.h>
#include <stdint.h>
#include "env/KnownObjectTable.hpp"
#include "codegen/RecognizedMethods.hpp"
#include "compile/Compilation.hpp"
#include "control/Options.hpp"
#include "control/Options_inlines.hpp"
#include "env/TRMemory.hpp"
#include "env/jittypes.h"
#include "il/DataTypes.hpp"
#include "il/ILOpCodes.hpp"
#include "il/Node.hpp"
#include "infra/Assert.hpp"
#include "infra/Flags.hpp"
#include "infra/Link.hpp"
#include "infra/List.hpp"
#include "infra/Random.hpp"
#include "optimizer/InlinerFailureReason.hpp"
#include "optimizer/Optimization.hpp"
#include "optimizer/OptimizationManager.hpp"
#include "optimizer/Optimizer.hpp"
#include "ras/LogTracer.hpp"
const float SECOND_BEST_MIN_CALL_FREQUENCY = .2275f; //65% of 35%
#define partialTrace(r, ...) \
if (true) {\
if ((r)->partialLevel())\
{\
(r)->partialTraceM(__VA_ARGS__);\
}\
} else \
(void)0
class TR_BitVector;
class TR_CallStack;
class TR_FrontEnd;
class TR_HashTabInt;
class TR_InlineBlocks;
class TR_InnerAssumption;
class TR_PrexArgInfo;
class TR_ResolvedMethod;
class TR_TransformInlinedFunction;
namespace TR { class Block; }
namespace TR { class CFG; }
namespace TR { class Method; }
namespace TR { class Node; }
namespace TR { class ParameterSymbol; }
namespace TR { class ResolvedMethodSymbol; }
namespace TR { class Symbol; }
namespace TR { class SymbolReference; }
namespace TR { class TreeTop; }
class TR_CallSite;
struct TR_CallTarget;
struct TR_ParameterMapping;
struct TR_VirtualGuardSelection;
class OMR_InlinerPolicy;
class OMR_InlinerUtil;
class TR_InlinerTracer : public TR_LogTracer
{
public:
TR_InlinerTracer(TR::Compilation *comp, TR_FrontEnd *fe, TR::Optimization *opt);
TR_ALLOC(TR_Memory::Inliner);
TR_FrontEnd * fe() { return _fe; }
TR_Memory * trMemory() { return _trMemory; }
TR_StackMemory trStackMemory() { return _trMemory; }
TR_HeapMemory trHeapMemory() { return _trMemory; }
TR_PersistentMemory * trPersistentMemory(){ return _trMemory->trPersistentMemory(); }
// determine the tracing level
bool partialLevel()
{
static const bool enableTracePartialInlining = feGetEnv("TR_EnableTracePartialInlining") != NULL;
return enableTracePartialInlining;
}
// trace statements for specific tracing levels
void partialTraceM ( const char *fmt, ...); // trace statement for investigating partial ininling heuristic
// counter insertion
void insertCounter (TR_InlinerFailureReason reason, TR::TreeTop *tt);
// some dumping routines
void dumpCallGraphs (TR_LinkHead<TR_CallTarget> *targets);
void dumpCallStack (TR_CallStack *, const char *fmt, ...);
void dumpCallSite (TR_CallSite *, const char *fmt, ...);
void dumpCallTarget (TR_CallTarget *, const char *fmt, ...);
void dumpPartialInline (TR_InlineBlocks *partialInline);
void dumpDeadCalls(TR_LinkHead<TR_CallSite> *sites);
// failure handling assistance
const char * getFailureReasonString(TR_InlinerFailureReason reason) { return TR_InlinerFailureReasonStr[reason]; }
const char * getGuardKindString(TR_VirtualGuardSelection *guard);
const char * getGuardTypeString(TR_VirtualGuardSelection *guard);
template <typename ObjWithSignatureMethod>
const char* traceSignature(ObjWithSignatureMethod *obj)
{
return heuristicLevel() ? obj->signature(trMemory()) : "";
}
protected:
TR_Memory * _trMemory;
TR_FrontEnd * _fe;
};
class TR_InlinerDelimiter
{
public:
TR_InlinerDelimiter(TR_InlinerTracer *tracer, const char *tag);
~TR_InlinerDelimiter();
protected:
TR_InlinerTracer *_tracer;
const char *_tag;
};
class TR_Inliner : public TR::Optimization
{
public:
TR_Inliner(TR::OptimizationManager *manager);
static TR::Optimization *create(TR::OptimizationManager *manager)
{
return new (manager->allocator()) TR_Inliner(manager);
}
virtual int32_t perform();
virtual const char * optDetailString() const throw();
};
class TR_TrivialInliner : public TR::Optimization
{
public:
TR_TrivialInliner(TR::OptimizationManager *manager);
static TR::Optimization *create(TR::OptimizationManager *manager)
{
return new (manager->allocator()) TR_TrivialInliner(manager);
}
virtual int32_t perform();
virtual const char * optDetailString() const throw();
};
class TR_InnerPreexistenceInfo
{
public:
TR_ALLOC(TR_Memory::Inliner);
TR_InnerPreexistenceInfo(TR::Compilation * c, TR::ResolvedMethodSymbol *methodSymbol, TR_CallStack *callStack,
TR::TreeTop *callTree, TR::Node *callNode,
TR_VirtualGuardKind _guardKind);
TR::Compilation * comp() { return _comp; }
TR_Memory * trMemory() { return _trMemory; }
TR_StackMemory trStackMemory() { return _trMemory; }
TR_HeapMemory trHeapMemory() { return _trMemory; }
TR_PersistentMemory * trPersistentMemory(){ return _trMemory->trPersistentMemory(); }
bool hasInnerAssumptions() { return !_assumptions.isEmpty(); }
virtual bool perform(TR::Compilation *comp, TR::Node *guardNode, bool & disableTailRecursion);
protected:
TR::Compilation * _comp;
TR_Memory * _trMemory;
TR::ResolvedMethodSymbol *_methodSymbol; // the method being called
TR_CallStack *_callStack; // the stack of callers (not including _methodSymbol)
TR::TreeTop *_callTree;
TR::Node *_callNode;
int32_t _numArgs; // number of arguments of methodSymbol
TR_VirtualGuardKind _guardKind;
List<TR_InnerAssumption> _assumptions; // any inner assumptions
};
struct TR_VirtualGuardSelection
{
TR_ALLOC(TR_Memory::Inliner);
TR_VirtualGuardSelection
(TR_VirtualGuardKind kind, TR_VirtualGuardTestType type = TR_NonoverriddenTest, TR_OpaqueClassBlock *thisClass= 0)
: _kind(kind)
, _type(type)
, _thisClass(thisClass)
, _highProbabilityProfiledGuard(false)
, _forceTakenSideCold(false)
{}
TR_VirtualGuardKind _kind;
TR_VirtualGuardTestType _type;
// the type is provided only when the test type is a vft test
TR_OpaqueClassBlock *_thisClass;
// only for a MutableCallSiteTargetGuard
uintptr_t *_mutableCallSiteObject;
TR::KnownObjectTable::Index _mutableCallSiteEpoch;
// this flag is used to signify a profiled guard for which
// we believe it's very likely to be true. It's used to mark
// the slow path as cold so optimizations like escape analysis can
// work on the fast path.
bool _highProbabilityProfiledGuard;
// Force the taken side to be cold (even for guard kinds, e.g. profiled
// guard, that usually don't mark the taken side cold). This is for use in
// cases where where we have reason to believe that the guard will always
// pass as a practical matter, but where for correctness in some absurd
// corner cases it's not possible to generate a nop guard.
bool _forceTakenSideCold;
void setIsHighProbablityProfiledGuard(bool b=true)
{
TR_ASSERT(_kind==TR_ProfiledGuard, "only valid for profiled guards");
_highProbabilityProfiledGuard = b;
}
bool isHighProbablityProfiledGuard()
{
return _highProbabilityProfiledGuard;
}
};
class TR_InlinerBase: public TR_HasRandomGenerator
{
protected:
virtual bool supportsMultipleTargetInlining () { return false ; }
public:
vcount_t getVisitCount() { return _visitCount;}
void setPolicy(OMR_InlinerPolicy *policy){ _policy = policy; }
void setUtil(OMR_InlinerUtil *util){ _util = util; }
OMR_InlinerPolicy *getPolicy(){ TR_ASSERT(_policy != NULL, "inliner policy must be set"); return _policy; }
OMR_InlinerUtil *getUtil(){ TR_ASSERT(_util != NULL, "inliner util must be set"); return _util; }
bool allowBiggerMethods() { return comp()->isServerInlining(); }
bool isEDODisableInlinedProfilingInfo () {return _EDODisableInlinedProfilingInfo; }
void setInlineThresholds(TR::ResolvedMethodSymbol *callerSymbol);
void performInlining(TR::ResolvedMethodSymbol *);
int32_t getCurrentNumberOfNodes() { return _currentNumberOfNodes; }
int32_t getMaxInliningCallSites() { return _maxInliningCallSites; }
int32_t getNumAsyncChecks() { return _numAsyncChecks; }
int32_t getNodeCountThreshold() { return _nodeCountThreshold;}
int32_t getMaxRecursiveCallByteCodeSizeEstimate() { return _maxRecursiveCallByteCodeSizeEstimate;}
void incCurrentNumberOfNodes(int32_t i) { _currentNumberOfNodes+=i; }
bool inlineVirtuals() { return _flags.testAny(InlineVirtuals); }
void setInlineVirtuals(bool b) { _flags.set(InlineVirtuals, b); }
bool inlineSynchronized() { return _flags.testAny(InlineSynchronized); }
void setInlineSynchronized(bool b) { _flags.set(InlineSynchronized, b); }
bool firstPass() { return _flags.testAny(FirstPass); }
void setFirstPass(bool b=true) { _flags.set(FirstPass, b); }
uint32_t getSizeThreshold() { return _methodByteCodeSizeThreshold; }
void setSizeThreshold(uint32_t size);
virtual bool inlineRecognizedMethod(TR::RecognizedMethod method);
void createParmMap(TR::ResolvedMethodSymbol *calleeSymbol, TR_LinkHead<TR_ParameterMapping> &map);
TR::Compilation * comp() { return _optimizer->comp();}
TR_Memory * trMemory() { return _trMemory; }
TR_StackMemory trStackMemory() { return _trMemory; }
TR_HeapMemory trHeapMemory() { return _trMemory; }
TR_PersistentMemory * trPersistentMemory(){ return _trMemory->trPersistentMemory(); }
TR_FrontEnd * fe() { return _optimizer->comp()->fe(); }
TR::Optimizer * getOptimizer() { return _optimizer; }
TR_InlinerTracer* tracer() { return _tracer; }
void setStoreToCachedPrivateStatic(TR::Node *node) { _storeToCachedPrivateStatic = node; }
TR::Node *getStoreToCachedPrivateStatic() { return _storeToCachedPrivateStatic; }
bool alwaysWorthInlining(TR_ResolvedMethod * calleeMethod, TR::Node *callNode);
void getSymbolAndFindInlineTargets(TR_CallStack *, TR_CallSite *, bool findNewTargets=true);
void applyPolicyToTargets(TR_CallStack *, TR_CallSite *);
bool forceInline(TR_CallTarget *calltarget);
bool forceVarInitInlining(TR_CallTarget *calltarget);
bool forceCalcInlining(TR_CallTarget *calltarget);
protected:
TR_InlinerBase(TR::Optimizer *, TR::Optimization *);
TR_PrexArgInfo* buildPrexArgInfoForOutermostMethod(TR::ResolvedMethodSymbol* methodSymbol);
bool inlineCallTarget(TR_CallStack *,TR_CallTarget *calltarget, bool inlinefromgraph, TR_PrexArgInfo *argInfo = 0, TR::TreeTop** cursorTreeTop = NULL);
bool inlineCallTarget2(TR_CallStack *, TR_CallTarget *calltarget, TR::TreeTop** cursorTreeTop, bool inlinefromgraph, int32_t);
//inlineCallTarget2 methods
bool tryToInlineTrivialMethod (TR_CallStack* callStack, TR_CallTarget* calltarget);
bool tryToGenerateILForMethod (TR::ResolvedMethodSymbol* calleeSymbol, TR::ResolvedMethodSymbol* callerSymbol, TR_CallTarget* calltarget);
void inlineFromGraph(TR_CallStack *, TR_CallTarget *calltarget, TR_InnerPreexistenceInfo *);
TR_CallSite* findAndUpdateCallSiteInGraph(TR_CallStack *callStack, TR_ByteCodeInfo &bcInfo, TR::TreeTop *tt, TR::Node *parent, TR::Node *callNode, TR_CallTarget *calltarget);
void initializeControlFlowInfo(TR_CallStack *,TR::ResolvedMethodSymbol *);
void cleanup(TR::ResolvedMethodSymbol *, bool);
int checkInlineableWithoutInitialCalleeSymbol(TR_CallSite* callsite, TR::Compilation* comp);
void getBorderFrequencies(int32_t &hotBorderFrequency, int32_t &coldBorderFrequency, TR_ResolvedMethod * calleeResolvedMethod, TR::Node *callNode);
int32_t scaleSizeBasedOnBlockFrequency(int32_t bytecodeSize, int32_t frequency, int32_t borderFrequency, TR_ResolvedMethod * calleeResolvedMethod, TR::Node *callNode, int32_t coldBorderFrequency = 0); // not virtual because we want base class always calling base version of this
virtual bool exceedsSizeThreshold(TR_CallSite *callSite, int bytecodeSize, TR::Block * callNodeBlock, TR_ByteCodeInfo & bcInfo, int32_t numLocals=0, TR_ResolvedMethod * caller = 0, TR_ResolvedMethod * calleeResolvedMethod = 0, TR::Node * callNode = 0, bool allConsts = false);
virtual bool inlineCallTargets(TR::ResolvedMethodSymbol *, TR_CallStack *, TR_InnerPreexistenceInfo *info)
{
TR_ASSERT(0, "invalid call to TR_InlinerBase::inlineCallTargets");
return false;
}
TR::TreeTop * addGuardForVirtual(TR::ResolvedMethodSymbol *, TR::ResolvedMethodSymbol *, TR::TreeTop *, TR::Node *,
TR_OpaqueClassBlock *, TR::TreeTop *, TR::TreeTop *, TR::TreeTop *, TR_TransformInlinedFunction&,
List<TR::SymbolReference> &, TR_VirtualGuardSelection *, TR::TreeTop **, TR_CallTarget *calltarget=0);
void addAdditionalGuard(TR::Node *callNode, TR::ResolvedMethodSymbol * calleeSymbol, TR_OpaqueClassBlock *thisClass, TR::Block *prevBlock, TR::Block *inlinedBody, TR::Block *slowPath, TR_VirtualGuardKind kind, TR_VirtualGuardTestType type, bool favourVftCompare, TR::CFG *callerCFG);
void rematerializeCallArguments(TR_TransformInlinedFunction & tif,
TR_VirtualGuardSelection *guard, TR::Node *callNode, TR::Block *block1, TR::TreeTop *rematPoint);
void replaceCallNode(TR::ResolvedMethodSymbol *, TR::Node *, rcount_t, TR::TreeTop *, TR::Node *, TR::Node *);
void linkOSRCodeBlocks();
bool IsOSRFramesSizeUnderThreshold();
bool heuristicForUsingOSR(TR::Node *, TR::ResolvedMethodSymbol *, TR::ResolvedMethodSymbol *, bool);
TR::Node * createVirtualGuard(TR::Node *, TR::ResolvedMethodSymbol *, TR::TreeTop *, int16_t, TR_OpaqueClassBlock *, bool, TR_VirtualGuardSelection *);
private:
void replaceCallNodeReferences(TR::Node *, TR::Node *, uint32_t, TR::Node *, TR::Node *, rcount_t &, TR::NodeChecklist &visitedNodes);
void cloneChildren(TR::Node *, TR::Node *, uint32_t);
protected:
enum
{
//Available = 0x0001,
InlineVirtuals = 0x0002,
InlineSynchronized = 0x0004,
FirstPass = 0x0008,
lastDummyEnum
};
TR::Optimizer * _optimizer;
TR_Memory * _trMemory;
List<TR::SymbolReference> _availableTemps;
List<TR::SymbolReference> _availableBasicBlockTemps;
flags16_t _flags;
vcount_t _visitCount;
bool _inliningAsWeWalk;
bool _EDODisableInlinedProfilingInfo;
bool _disableTailRecursion;
bool _disableInnerPrex;
bool _isInLoop;
int32_t _maxRecursiveCallByteCodeSizeEstimate;
int32_t _callerWeightLimit;
int32_t _methodByteCodeSizeThreshold;
int32_t _methodInColdBlockByteCodeSizeThreshold;
int32_t _methodInWarmBlockByteCodeSizeThreshold;
int32_t _nodeCountThreshold;
int32_t _maxInliningCallSites;
int32_t _numAsyncChecks;
bool _aggressivelyInlineInLoops;
int32_t _currentNumberOfNodes;
TR_LinkHead<TR_CallSite> _deadCallSites;
TR::Node *_storeToCachedPrivateStatic;
TR_InlinerTracer* _tracer;
OMR_InlinerPolicy *_policy;
OMR_InlinerUtil*_util;
};
class TR_DumbInliner : public TR_InlinerBase
{
public:
TR_DumbInliner(TR::Optimizer *, TR::Optimization *, uint32_t initialSize, uint32_t dumbReductionIncrement = 5);
virtual bool inlineCallTargets(TR::ResolvedMethodSymbol *, TR_CallStack *, TR_InnerPreexistenceInfo *);
bool tryToInline(const char *message, TR_CallTarget *calltarget);
protected:
virtual bool analyzeCallSite(TR_CallStack *, TR::TreeTop *, TR::Node *, TR::Node *);
uint32_t _initialSize;
uint32_t _dumbReductionIncrement;
};
class TR_InlineCall : public TR_DumbInliner
{
public:
TR_InlineCall(TR::Optimizer *, TR::Optimization *);
virtual bool inlineRecognizedMethod(TR::RecognizedMethod method);
bool inlineCall(TR::TreeTop * callNodeTreeTop, TR_OpaqueClassBlock * thisType = 0, bool recursiveInlining = false, TR_PrexArgInfo *argInfo = 0, int32_t initialMaxSize = 0);
};
struct TR_ParameterMapping : TR_Link<TR_ParameterMapping>
{
TR_ParameterMapping(TR::ParameterSymbol * ps)
: _parmSymbol(ps),
_replacementSymRef(NULL),
_replacementSymRefForInlinedBody(NULL),
_parameterNode(NULL),
_parmIsModified(false),
_addressTaken(false),
_isConst(false)
{ }
TR::ParameterSymbol * _parmSymbol;
/*
* The symbol reference of the priv arg temp.
*
* Used for replacing both the parm symbol references in the inlined body and args on the taken side of the guard.
*/
TR::SymbolReference * _replacementSymRef;
/*
* Used for replacing parm symbol reference in the inlined body when this field not NULL.
* Otherwise, the \ref _replacementSymRef is used.
*/
TR::SymbolReference * _replacementSymRefForInlinedBody;
TR::Node * _parameterNode; //The Node under the call which matches the argument at argIndex
uint32_t _argIndex;
bool _parmIsModified;
bool _isConst;
bool _addressTaken;
};
class TR_ParameterToArgumentMapper
{
public:
TR_ALLOC(TR_Memory::Inliner)
TR_ParameterToArgumentMapper(TR::ResolvedMethodSymbol *, TR::ResolvedMethodSymbol *, TR::Node *, TR_PrexArgInfo *,
List<TR::SymbolReference> &, List<TR::SymbolReference> &,
List<TR::SymbolReference> &, TR_InlinerBase *);
void initialize(TR_CallStack *callStack);
TR::Node * map(TR::Node *, TR::ParameterSymbol *, bool);
void mapOSRCallSiteRematTable(uint32_t siteIndex);
void adjustReferenceCounts();
TR::Node * fixCallNodeArgs(bool);
TR::TreeTop * firstTempTreeTop() { return _firstTempTreeTop; }
TR::TreeTop * lastTempTreeTop() { return _lastTempTreeTop; }
TR_ParameterMapping * getFirstMapping() { return _mappings.getFirst(); }
void printMapping();
TR::Compilation * comp() { return _inliner->comp(); }
TR_Memory * trMemory() { return comp()->trMemory(); }
TR_StackMemory trStackMemory() { return trMemory(); }
TR_HeapMemory trHeapMemory() { return trMemory(); }
TR_InlinerTracer * tracer() { return _inliner->tracer(); }
TR_ParameterMapping * findMapping(TR::Symbol *);
private:
void lookForModifiedParameters();
void lookForModifiedParameters(TR::Node *);
TR::ResolvedMethodSymbol * _calleeSymbol;
TR::ResolvedMethodSymbol * _callerSymbol;
TR::SymbolReference * _vftReplacementSymRef;
TR::Node * _callNode;
TR_LinkHead<TR_ParameterMapping> _mappings;
TR::TreeTop * _firstTempTreeTop;
TR::TreeTop * _lastTempTreeTop;
List<TR::SymbolReference> & _tempList;
List<TR::SymbolReference> & _availableTemps;
List<TR::SymbolReference> & _availableTemps2;
TR_InlinerTracer * _tracer;
TR_InlinerBase * _inliner;
TR_PrexArgInfo * _argInfo;
};
class OMR_InlinerHelper
{
friend class TR_InlinerBase;
public:
TR_InlinerTracer * tracer() { return _inliner->tracer(); }
TR_InlinerBase *inliner() { return _inliner; }
protected:
TR_InlinerBase * _inliner;
void setInliner(TR_InlinerBase *inliner) { _inliner = inliner; }
};
class OMR_InlinerUtil : public TR::OptimizationUtil, public OMR_InlinerHelper
{
friend class TR_InlinerBase;
public:
OMR_InlinerUtil(TR::Compilation *comp);
static TR::TreeTop * storeValueInATemp(TR::Compilation *comp, TR::Node *, TR::SymbolReference * &, TR::TreeTop *, TR::ResolvedMethodSymbol *, List<TR::SymbolReference> & tempList, List<TR::SymbolReference> & availableTemps, List<TR::SymbolReference> * moreTemps, bool behavesLikeTemp = true, TR::TreeTop ** newStoreValueATreeTop = NULL, bool isIndirect = false, int32_t offset = 0);
virtual bool addTargetIfMethodIsNotOverridenInReceiversHierarchy(TR_IndirectCallSite *callsite);
virtual TR_ResolvedMethod *findSingleJittedImplementer(TR_IndirectCallSite *callsite);
virtual bool addTargetIfThereIsSingleImplementer (TR_IndirectCallSite *callsite);
virtual TR_PrexArgInfo* createPrexArgInfoForCallTarget(TR_VirtualGuardSelection *guard, TR_ResolvedMethod *implementer);
virtual TR_InnerPreexistenceInfo *createInnerPrexInfo(TR::Compilation * c, TR::ResolvedMethodSymbol *methodSymbol, TR_CallStack *callStack, TR::TreeTop *callTree, TR::Node *callNode, TR_VirtualGuardKind guardKind);
virtual TR_InlinerTracer * getInlinerTracer(TR::Optimization *optimization);
virtual void adjustByteCodeSize(TR_ResolvedMethod *calleeResolvedMethod, bool isInLoop, TR::Block *block, int &bytecodeSize){ return; }
virtual void adjustCallerWeightLimit(TR::ResolvedMethodSymbol *callSymbol, int &callerWeightLimit){ return; }
virtual void adjustMethodByteCodeSizeThreshold(TR::ResolvedMethodSymbol *callSymbol, int &methodByteCodeSizeThreshold){ return; }
virtual TR_PrexArgInfo *computePrexInfo(TR_CallTarget *target);
virtual TR_PrexArgInfo *computePrexInfo(TR_CallTarget *target, TR_PrexArgInfo *callerArgInfo);
virtual void clearArgInfoForNonInvariantArguments(TR_CallTarget *target, TR_InlinerTracer* tracer);
virtual void clearArgInfoForNonInvariantArguments(TR_PrexArgInfo* argInfo, TR::ResolvedMethodSymbol* methodSymbol, TR_InlinerTracer* tracer);
virtual void collectCalleeMethodClassInfo(TR_ResolvedMethod *calleeMethod);
/*
* Implemented by down stream projects to request for certain optimizations based on \parm target
*
* \parm calltarget
* The calltarget got inlined
*/
virtual void requestAdditionalOptimizations(TR_CallTarget *calltarget);
/**
* \brief
* Check if another pass of targeted inlining is needed if the given method is inlined.
*
* \parm callee
* The method symbol of the callee.
*
* \return
* True if another pass of targeted inlining is needed, otherwise false.
*
* \note
* Targeted inlining is meant to deal with method call chains where the receiver of current call stores the information
* of the next call, which is usually the receiver or the method of the next call. Knowing the receiver of the first call
* means we can devirtualize all the calls along the chain.
*
* Each method except the last one in the chain usually contains simple code that manipulates the arguments for the next
* call. The last method in the chain is usually the most important one, it does the real job and can contain arbitrary
* code.
*
* The goal of targeted inlining is to inline along the call chain to get the most important method inlined, which is the
* last one in the chain. It does not care about other callsites.
*/
virtual bool needTargetedInlining(TR::ResolvedMethodSymbol *callee);
protected:
virtual void refineColdness (TR::Node* node, bool& isCold);
virtual void computeMethodBranchProfileInfo (TR::Block * cfgBlock, TR_CallTarget* calltarget, TR::ResolvedMethodSymbol* callerSymbol);
virtual int32_t getCallCount(TR::Node *callNode);
virtual void calleeTreeTopPreMergeActions(TR::ResolvedMethodSymbol *calleeResolvedMethodSymbol, TR_CallTarget* calltarget);
virtual void refineInlineGuard(TR::Node *callNode, TR::Block *&block1, TR::Block *&block2,
bool &appendTestToBlock1, TR::ResolvedMethodSymbol * callerSymbol, TR::TreeTop *cursorTree,
TR::TreeTop *&virtualGuard, TR::Block *block4);
virtual void refineInliningThresholds(TR::Compilation *comp, int32_t &callerWeightLimit, int32_t &maxRecursiveCallByteCodeSizeEstimate, int32_t &methodByteCodeSizeThreshold, int32_t &methodInWarmBlockByteCodeSizeThreshold, int32_t &methodInColdBlockByteCodeSizeThreshold, int32_t &nodeCountThreshold, int32_t size){ }
virtual void estimateAndRefineBytecodeSize(TR_CallSite* callsite, TR_CallTarget* target, TR_CallStack *callStack, int32_t &bytecodeSize);
virtual TR_TransformInlinedFunction *getTransformInlinedFunction(TR::ResolvedMethodSymbol *, TR::ResolvedMethodSymbol *, TR::Block *, TR::TreeTop *,
TR::Node *, TR_ParameterToArgumentMapper &, TR_VirtualGuardSelection *, List<TR::SymbolReference> &,
List<TR::SymbolReference> &, List<TR::SymbolReference> &);
};
class OMR_InlinerPolicy : public TR::OptimizationPolicy, public OMR_InlinerHelper
{
friend class TR_InlinerBase;
public:
OMR_InlinerPolicy(TR::Compilation *comp);
virtual bool inlineRecognizedMethod(TR::RecognizedMethod method);
int32_t getInitialBytecodeSize(TR::ResolvedMethodSymbol * methodSymbol, TR::Compilation *comp);
virtual int32_t getInitialBytecodeSize(TR_ResolvedMethod *feMethod, TR::ResolvedMethodSymbol * methodSymbol, TR::Compilation *comp);
virtual bool tryToInline(TR_CallTarget *, TR_CallStack *, bool);
virtual bool inlineMethodEvenForColdBlocks(TR_ResolvedMethod *method);
bool aggressiveSmallAppOpts() { return comp()->getOption(TR_AggressiveOpts); }
virtual bool willBeInlinedInCodeGen(TR::RecognizedMethod method);
virtual bool canInlineMethodWhileInstrumenting(TR_ResolvedMethod *method);
virtual bool shouldRemoveDifferingTargets(TR::Node *callNode);
/** \brief
* Determines whether to skip generation of HCR guards for a particular callee during inlining.
*
* \param callee
* The callee to be inlined.
*
* \return
* true if we can skip generating HCR guards for this \p callee; false otherwise.
*/
virtual bool skipHCRGuardForCallee(TR_ResolvedMethod* callee)
{
return false;
}
virtual bool dontPrivatizeArgumentsForRecognizedMethod(TR::RecognizedMethod recognizedMethod);
virtual bool replaceSoftwareCheckWithHardwareCheck(TR_ResolvedMethod *calleeMethod);
/**
* \brief Answers whether the provided ResolvedMethodSymbol is a native method that can be inlined
*
* \param[in] comp : the TR::Compilation object
* \param[in] methodSymbol : the ResolvedMethodSymbol to check
*
* \return true if the method is inlineable; false otherwise
*/
virtual bool isInlineableNativeMethod(TR::Compilation *comp, TR::ResolvedMethodSymbol *methodSymbol) { return false; }
/**
* \brief sometimes we choose not to inline certain callsite because they will be handled specially in other optimizations or in codegen
*
* \return true if the method shouldn't be inlined in inliner
*/
virtual bool supressInliningRecognizedInitialCallee(TR_CallSite* callsite, TR::Compilation* comp);
protected:
virtual bool tryToInlineTrivialMethod (TR_CallStack* callStack, TR_CallTarget* calltarget);
virtual bool alwaysWorthInlining(TR_ResolvedMethod * calleeMethod, TR::Node *callNode);
virtual void determineInliningHeuristic(TR::ResolvedMethodSymbol *callerSymbol);
bool tryToInlineGeneral(TR_CallTarget *, TR_CallStack *, bool);
virtual bool callMustBeInlined(TR_CallTarget *calltarget);
bool mustBeInlinedEvenInDebug(TR_ResolvedMethod * calleeMethod, TR::TreeTop *callNodeTreeTop);
virtual TR_InlinerFailureReason checkIfTargetInlineable(TR_CallTarget* target, TR_CallSite* callsite, TR::Compilation* comp);
virtual bool suitableForRemat(TR::Compilation *comp, TR::Node *node, TR_VirtualGuardSelection *guard);
};
class TR_TransformInlinedFunction
{
public:
TR_TransformInlinedFunction(TR::Compilation*, TR_InlinerTracer *tracer, TR::ResolvedMethodSymbol *, TR::ResolvedMethodSymbol *, TR::Block *, TR::TreeTop *,
TR::Node *, TR_ParameterToArgumentMapper &, TR_VirtualGuardSelection *, List<TR::SymbolReference> &,
List<TR::SymbolReference> &, List<TR::SymbolReference> &);
virtual void transform();
TR::Node * resultNode() { return _resultNode; }
TR::TreeTop * firstBBEnd() { return _firstBBEnd; }
TR::TreeTop * lastBBStart() { return _lastBBStart; }
TR::TreeTop * lastMainLineTreeTop() { return _lastMainLineTreeTop; }
TR::Block * firstCatchBlock() { return _firstCatchBlock; }
bool crossedBasicBlock() { return _crossedBasicBlock; }
TR::TreeTop * simpleCallReferenceTreeTop() { return _simpleCallReferenceTreeTop; }
TR::SymbolReference * resultTempSymRef() { return _resultTempSymRef; }
List<TR::TreeTop> & treeTopsToRemove() { return _treeTopsToRemove; }
List<TR::Block> & blocksWithEdgesToTheEnd() { return _blocksWithEdgesToTheEnd; }
TR::TreeTop * callNodeTreeTop() { return _callNodeTreeTop; }
TR::Node * getCallNode() { return _callNode; }
void setResultNode(TR::Node * n) { _resultNode = n; }
bool favourVftCompare() { return _favourVftCompare; }
TR::ResolvedMethodSymbol *getCallerSymbol() { return _callerSymbol; }
TR_ParameterToArgumentMapper &getParameterMapper(){ return _parameterMapper; }
#define MAX_FIND_SIMPLE_CALL_REFERENCE_DEPTH 12
void setFindCallNodeRecursionDepthToMax() { findCallNodeRecursionDepth = MAX_FIND_SIMPLE_CALL_REFERENCE_DEPTH; }
void setMultiRefNodeIsCallRecursionDepthToMax() { onlyMultiRefNodeIsCallNodeRecursionDepth = MAX_FIND_SIMPLE_CALL_REFERENCE_DEPTH; }
bool findCallNodeInTree (TR::Node * callNode, TR::Node * node);
TR::TreeTop * findSimpleCallReference(TR::TreeTop * callNodeTreeTop, TR::Node * callNode);
bool findReturnValueInTree(TR::Node * rvStoreNode, TR::Node * node, TR::Compilation *comp);
bool onlyMultiRefNodeIsCallNode(TR::Node * callNode, TR::Node * node);
TR::Compilation * comp() { return _comp; }
TR_InlinerTracer * tracer() { return _tracer; }
TR_Memory * trMemory() { return comp()->trMemory(); }
TR_StackMemory trStackMemory() { return trMemory(); }
TR_HeapMemory trHeapMemory() { return trMemory(); }
protected:
void transformNode(TR::Node *, TR::Node *, uint32_t, TR::NodeChecklist &visitedNodes);
void transformReturn(TR::Node *, TR::Node *);
TR::Compilation * _comp;
TR_InlinerTracer * _tracer;
TR::ResolvedMethodSymbol * _calleeSymbol;
TR::ResolvedMethodSymbol * _callerSymbol;
TR::TreeTop * _callNodeTreeTop;
TR::Node * _callNode;
TR_ParameterToArgumentMapper & _parameterMapper;
TR::TreeTop * _currentTreeTop;
TR::TreeTop * _firstBBEnd;
TR::TreeTop * _lastBBStart;
TR::TreeTop * _penultimateTreeTop;
TR::TreeTop * _lastMainLineTreeTop;
TR::Node * _resultNode;
TR::SymbolReference * _resultTempSymRef;
TR::Block * _generatedLastBlock;
TR::Block * _firstCatchBlock;
TR::TreeTop * _simpleCallReferenceTreeTop;
List<TR::SymbolReference> & _tempList;
List<TR::SymbolReference> & _availableTemps;
List<TR::SymbolReference> & _availableTemps2;
List<TR::TreeTop> _treeTopsToRemove;
List<TR::Block > _blocksWithEdgesToTheEnd;
bool _processingExceptionHandlers;
bool _favourVftCompare;
bool _determineIfReturnCanBeReplacedWithCallNodeReference;
bool _crossedBasicBlock;
int32_t findCallNodeRecursionDepth;
int32_t onlyMultiRefNodeIsCallNodeRecursionDepth;
};
class TR_HandleInjectedBasicBlock
{
public:
TR_HandleInjectedBasicBlock(TR::Compilation *, TR_InlinerTracer *, TR::ResolvedMethodSymbol *, List<TR::SymbolReference> &, List<TR::SymbolReference> &,
List<TR::SymbolReference> &, TR_ParameterMapping * = 0);
void findAndReplaceReferences(TR::TreeTop *, TR::Block *, TR::Block *);
TR::Node * findNullCheckReferenceSymbolReference(TR::TreeTop *);
TR::Compilation *comp() { return _comp; }
TR_InlinerTracer *tracer() { return _tracer; }
private:
void printNodesWithMultipleReferences();
void collectNodesWithMultipleReferences(TR::TreeTop *, TR::Node *, TR::Node *);
void replaceNodesReferencedFromAbove(TR::Block *, TR::NodeChecklist &visitedNodes);
void replaceNodesReferencedFromAbove(TR::TreeTop *, TR::Node *, TR::Node *, uint32_t, TR::NodeChecklist &visitedNodes);
void createTemps(bool);
struct MultiplyReferencedNode : TR_Link<MultiplyReferencedNode>
{
MultiplyReferencedNode(TR::Node *node, TR::TreeTop *tt, uint32_t refsToBeFound,bool symCanBeReloaded);
TR::Node * _node;
TR::TreeTop * _treeTop;
TR::SymbolReference * _replacementSymRef;
uint32_t _referencesToBeFound;
bool _isConst;
bool _symbolCanBeReloaded;
};
MultiplyReferencedNode * find(TR::Node *);
void remove(MultiplyReferencedNode *);
void add(TR::TreeTop *, TR::Node *);
void replace(MultiplyReferencedNode *, TR::TreeTop *, TR::Node *, uint32_t);
TR::Compilation * _comp;
TR_InlinerTracer * _tracer;
TR_LinkHead<MultiplyReferencedNode> _multiplyReferencedNodes;
TR_LinkHead<MultiplyReferencedNode> _fixedNodes;
List<TR::SymbolReference> & _tempList;
List<TR::SymbolReference> & _injectedBasicBlockTemps;
List<TR::SymbolReference> & _availableTemps;
TR::ResolvedMethodSymbol * _methodSymbol;
TR_ParameterMapping * _mappings;
};
#endif