Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openj9
Path: blob/master/runtime/compiler/optimizer/EstimateCodeSize.cpp
6000 views
1
/*******************************************************************************
2
* Copyright (c) 2000, 2021 IBM Corp. and others
3
*
4
* This program and the accompanying materials are made available under
5
* the terms of the Eclipse Public License 2.0 which accompanies this
6
* distribution and is available at https://www.eclipse.org/legal/epl-2.0/
7
* or the Apache License, Version 2.0 which accompanies this distribution and
8
* is available at https://www.apache.org/licenses/LICENSE-2.0.
9
*
10
* This Source Code may also be made available under the following
11
* Secondary Licenses when the conditions for such availability set
12
* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU
13
* General Public License, version 2 with the GNU Classpath
14
* Exception [1] and GNU General Public License, version 2 with the
15
* OpenJDK Assembly Exception [2].
16
*
17
* [1] https://www.gnu.org/software/classpath/license.html
18
* [2] http://openjdk.java.net/legal/assembly-exception.html
19
*
20
* 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
21
*******************************************************************************/
22
23
#include "env/StackMemoryRegion.hpp"
24
#include "optimizer/EstimateCodeSize.hpp"
25
26
#include "env/FrontEnd.hpp"
27
#include "compile/Compilation.hpp"
28
#include "il/ResolvedMethodSymbol.hpp"
29
#include "infra/Assert.hpp"
30
#include "optimizer/CallInfo.hpp"
31
#include "ras/LogTracer.hpp"
32
#include "env/VMJ9.h"
33
#include "runtime/J9Profiler.hpp"
34
35
TR_EstimateCodeSize *
36
TR_EstimateCodeSize::get(TR_InlinerBase * inliner, TR_InlinerTracer *tracer, int32_t sizeThreshold)
37
{
38
TR::Compilation *comp = inliner->comp();
39
TR_EstimateCodeSize *estimator = comp->fej9()->getCodeEstimator(comp);
40
41
estimator->_inliner = inliner;
42
estimator->_tracer = tracer;
43
44
estimator->_isLeaf = false;
45
estimator->_foundThrow = false;
46
estimator->_hasExceptionHandlers = false;
47
estimator->_throwCount = 0;
48
estimator->_mayHaveVirtualCallProfileInfo = false;
49
50
TR_CatchBlockProfileInfo * catchInfo = TR_CatchBlockProfileInfo::get(comp);
51
estimator->_aggressivelyInlineThrows = catchInfo && catchInfo->getCatchCounter()
52
>= TR_CatchBlockProfileInfo::EDOThreshold;
53
54
estimator->_recursionDepth = 0;
55
estimator->_recursedTooDeep = false;
56
57
estimator->_sizeThreshold = sizeThreshold;
58
estimator->_realSize = 0;
59
estimator->_error = ECS_NORMAL;
60
61
estimator->_numOfEstimatedCalls = 0;
62
estimator->_hasNonColdCalls = true;
63
64
estimator->_totalBCSize = 0;
65
66
return estimator;
67
}
68
69
void
70
TR_EstimateCodeSize::release(TR_EstimateCodeSize *estimator)
71
{
72
TR::Compilation *comp = estimator->_inliner->comp();
73
comp->fej9()->releaseCodeEstimator(comp, estimator);
74
}
75
76
bool
77
TR_EstimateCodeSize::calculateCodeSize(TR_CallTarget *calltarget, TR_CallStack *callStack, bool recurseDown)
78
{
79
TR_InlinerDelimiter delimiter(tracer(), "calculateCodeSize");
80
81
_isLeaf = true;
82
_foundThrow = false;
83
_hasExceptionHandlers = false;
84
_throwCount = 0;
85
86
_mayHaveVirtualCallProfileInfo = (TR_ValueProfileInfoManager::get(comp()) != NULL);
87
88
bool retval = false;
89
90
{
91
TR::StackMemoryRegion stackMemoryRegion(*comp()->trMemory());
92
retval = estimateCodeSize(calltarget, callStack, recurseDown);
93
} // Stack memory region scope
94
95
if (_inliner->getPolicy()->tryToInline(calltarget, callStack, true))
96
{
97
heuristicTrace(tracer(),"tryToInline pattern matched. Assuming zero size for %s\n", tracer()->traceSignature(calltarget->_calleeSymbol));
98
_realSize = 0;
99
retval = true;
100
}
101
102
if (!retval && _inliner->forceInline(calltarget))
103
retval = true;
104
105
return retval;
106
}
107
108
const char *
109
TR_EstimateCodeSize::getError()
110
{
111
switch(_error)
112
{
113
case ECS_NORMAL: return "ECS_NORMAL";
114
case ECS_RECURSION_DEPTH_THRESHOLD_EXCEEDED: return "ECS_RECURSION_DEPTH_THRESHOLD_EXCEEDED";
115
case ECS_OPTIMISTIC_SIZE_THRESHOLD_EXCEEDED: return "ECS_OPTIMISTIC_SIZE_THRESHOLD_EXCEEDED";
116
case ECS_VISITED_COUNT_THRESHOLD_EXCEEDED: return "ECS_VISITED_COUNT_THRESHOLD_EXCEEDED";
117
case ECS_REAL_SIZE_THRESHOLD_EXCEEDED: return "ECS_REAL_SIZE_THRESHOLD_EXCEEDED";
118
case ECS_ARGUMENTS_INCOMPATIBLE: return "ECS_ARGUMENTS_INCOMPATIBLE";
119
case ECS_CALLSITES_CREATION_FAILED: return "ECS_CALLSITES_CREATION_FAILED";
120
default: return "ECS_UNKNOWN";
121
}
122
}
123
124
bool
125
TR_EstimateCodeSize::isInlineable(TR_CallStack * prevCallStack, TR_CallSite *callsite)
126
{
127
TR_ASSERT(callsite, "Estimate Code Size: callsite is null!");
128
129
heuristicTrace(tracer(),"Depth %d: Created Call Site %p for call found at bc index %d. Signature %s Looking for call targets.",
130
_recursionDepth, callsite, callsite->_byteCodeIndex, tracer()->traceSignature(callsite));
131
132
if (_inliner->getPolicy()->supressInliningRecognizedInitialCallee(callsite, _inliner->comp()))
133
{
134
heuristicTrace(tracer(),"Skip looking for call targets because supressInliningRecognizedInitialCallee is true for this call site %p\n", callsite);
135
return false;
136
}
137
138
callsite->findCallSiteTarget(prevCallStack, _inliner);
139
_inliner->applyPolicyToTargets(prevCallStack, callsite);
140
141
142
if (callsite->numTargets() <= 0)
143
{
144
if (tracer()->debugLevel())
145
tracer()->dumpCallSite(
146
callsite,
147
"Call About to be Dumped returned false from findInlineTargets in partialCodeSize estimation");
148
149
heuristicTrace(tracer(),"Depth %d: Did not find any targets to be inlined in callsite %p bc index %d. Signature %s",
150
_recursionDepth, callsite, callsite->_byteCodeIndex, tracer()->traceSignature(callsite));
151
152
_isLeaf = false;
153
return false;
154
}
155
156
if (tracer()->debugLevel())
157
tracer()->dumpCallSite(
158
callsite,
159
"Call About to be Dumped returns true from findInlineTargets in partialCodeSize estimation");
160
161
heuristicTrace(tracer(),"Depth %d: Found %d targets to inline for callsite %p bc index %d. Signature %s",
162
_recursionDepth, callsite->numTargets(),callsite, callsite->_byteCodeIndex, tracer()->traceSignature(callsite));
163
164
return true;
165
}
166
167
bool
168
TR_EstimateCodeSize::returnCleanup(EcsCleanupErrorStates errorState)
169
{
170
_error = errorState;
171
if (_mayHaveVirtualCallProfileInfo)
172
_inliner->comp()->decInlineDepth(true);
173
if (errorState > 0)
174
return false;
175
else
176
return true;
177
}
178
179