Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/jdk17u
Path: blob/master/src/hotspot/share/ci/ciMethodBlocks.cpp
64440 views
1
/*
2
* Copyright (c) 2006, 2022, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*
23
*/
24
25
#include "precompiled.hpp"
26
#include "ci/ciMethodBlocks.hpp"
27
#include "ci/ciStreams.hpp"
28
#include "interpreter/bytecode.hpp"
29
#include "utilities/copy.hpp"
30
31
// ciMethodBlocks
32
33
34
35
ciBlock *ciMethodBlocks::block_containing(int bci) {
36
assert(bci >= 0 && bci < _code_size, "valid bytecode range");
37
ciBlock *blk = _bci_to_block[bci];
38
return blk;
39
}
40
41
bool ciMethodBlocks::is_block_start(int bci) {
42
assert(bci >= 0 && bci < _code_size, "valid bytecode range");
43
ciBlock *b = _bci_to_block[bci];
44
assert(b != NULL, "must have block for bytecode");
45
return b->start_bci() == bci;
46
}
47
48
// ------------------------------------------------------------------
49
// ciMethodBlocks::split_block_at
50
//
51
// Split the block spanning bci into two separate ranges. The former
52
// block becomes the second half and a new range is created for the
53
// first half. Returns the range beginning at bci.
54
ciBlock *ciMethodBlocks::split_block_at(int bci) {
55
ciBlock *former_block = block_containing(bci);
56
ciBlock *new_block = new(_arena) ciBlock(_method, _num_blocks++, former_block->start_bci());
57
_blocks->append(new_block);
58
assert(former_block != NULL, "must not be NULL");
59
new_block->set_limit_bci(bci);
60
former_block->set_start_bci(bci);
61
for (int pos=bci-1; pos >= 0; pos--) {
62
ciBlock *current_block = block_containing(pos);
63
if (current_block == former_block) {
64
// Replace it.
65
_bci_to_block[pos] = new_block;
66
} else if (current_block == NULL) {
67
// Non-bytecode start. Skip.
68
continue;
69
} else {
70
// We are done with our backwards walk
71
break;
72
}
73
}
74
// Move an exception handler information if needed.
75
if (former_block->is_handler()) {
76
int ex_start = former_block->ex_start_bci();
77
int ex_end = former_block->ex_limit_bci();
78
new_block->set_exception_range(ex_start, ex_end);
79
// Clear information in former_block.
80
former_block->clear_exception_handler();
81
}
82
return former_block;
83
}
84
85
ciBlock *ciMethodBlocks::make_block_at(int bci) {
86
ciBlock *cb = block_containing(bci);
87
if (cb == NULL ) {
88
// This is our first time visiting this bytecode. Create
89
// a fresh block and assign it this starting point.
90
ciBlock *nb = new(_arena) ciBlock(_method, _num_blocks++, bci);
91
_blocks->append(nb);
92
_bci_to_block[bci] = nb;
93
return nb;
94
} else if (cb->start_bci() == bci) {
95
// The block begins at bci. Simply return it.
96
return cb;
97
} else {
98
// We have already created a block containing bci but
99
// not starting at bci. This existing block needs to
100
// be split into two.
101
return split_block_at(bci);
102
}
103
}
104
105
ciBlock *ciMethodBlocks::make_dummy_block() {
106
ciBlock *dum = new(_arena) ciBlock(_method, -1, 0);
107
return dum;
108
}
109
110
void ciMethodBlocks::do_analysis() {
111
ciBytecodeStream s(_method);
112
ciBlock *cur_block = block_containing(0);
113
int limit_bci = _method->code_size();
114
115
while (s.next() != ciBytecodeStream::EOBC()) {
116
int bci = s.cur_bci();
117
// Determine if a new block has been made at the current bci. If
118
// this block differs from our current range, switch to the new
119
// one and end the old one.
120
assert(cur_block != NULL, "must always have a current block");
121
ciBlock *new_block = block_containing(bci);
122
if (new_block == NULL || new_block == cur_block) {
123
// We have not marked this bci as the start of a new block.
124
// Keep interpreting the current_range.
125
_bci_to_block[bci] = cur_block;
126
} else {
127
cur_block->set_limit_bci(bci);
128
cur_block = new_block;
129
}
130
131
switch (s.cur_bc()) {
132
case Bytecodes::_ifeq :
133
case Bytecodes::_ifne :
134
case Bytecodes::_iflt :
135
case Bytecodes::_ifge :
136
case Bytecodes::_ifgt :
137
case Bytecodes::_ifle :
138
case Bytecodes::_if_icmpeq :
139
case Bytecodes::_if_icmpne :
140
case Bytecodes::_if_icmplt :
141
case Bytecodes::_if_icmpge :
142
case Bytecodes::_if_icmpgt :
143
case Bytecodes::_if_icmple :
144
case Bytecodes::_if_acmpeq :
145
case Bytecodes::_if_acmpne :
146
case Bytecodes::_ifnull :
147
case Bytecodes::_ifnonnull :
148
{
149
cur_block->set_control_bci(bci);
150
if (s.next_bci() < limit_bci) {
151
ciBlock *fall_through = make_block_at(s.next_bci());
152
}
153
int dest_bci = s.get_dest();
154
ciBlock *dest = make_block_at(dest_bci);
155
break;
156
}
157
158
case Bytecodes::_goto :
159
{
160
cur_block->set_control_bci(bci);
161
if (s.next_bci() < limit_bci) {
162
(void) make_block_at(s.next_bci());
163
}
164
int dest_bci = s.get_dest();
165
ciBlock *dest = make_block_at(dest_bci);
166
break;
167
}
168
169
case Bytecodes::_jsr :
170
{
171
cur_block->set_control_bci(bci);
172
if (s.next_bci() < limit_bci) {
173
ciBlock *ret = make_block_at(s.next_bci());
174
}
175
int dest_bci = s.get_dest();
176
ciBlock *dest = make_block_at(dest_bci);
177
break;
178
}
179
180
case Bytecodes::_tableswitch :
181
{
182
cur_block->set_control_bci(bci);
183
Bytecode_tableswitch sw(&s);
184
int len = sw.length();
185
ciBlock *dest;
186
int dest_bci;
187
for (int i = 0; i < len; i++) {
188
dest_bci = s.cur_bci() + sw.dest_offset_at(i);
189
dest = make_block_at(dest_bci);
190
}
191
dest_bci = s.cur_bci() + sw.default_offset();
192
make_block_at(dest_bci);
193
if (s.next_bci() < limit_bci) {
194
dest = make_block_at(s.next_bci());
195
}
196
}
197
break;
198
199
case Bytecodes::_lookupswitch:
200
{
201
cur_block->set_control_bci(bci);
202
Bytecode_lookupswitch sw(&s);
203
int len = sw.number_of_pairs();
204
ciBlock *dest;
205
int dest_bci;
206
for (int i = 0; i < len; i++) {
207
dest_bci = s.cur_bci() + sw.pair_at(i).offset();
208
dest = make_block_at(dest_bci);
209
}
210
dest_bci = s.cur_bci() + sw.default_offset();
211
dest = make_block_at(dest_bci);
212
if (s.next_bci() < limit_bci) {
213
dest = make_block_at(s.next_bci());
214
}
215
}
216
break;
217
218
case Bytecodes::_goto_w :
219
{
220
cur_block->set_control_bci(bci);
221
if (s.next_bci() < limit_bci) {
222
(void) make_block_at(s.next_bci());
223
}
224
int dest_bci = s.get_far_dest();
225
ciBlock *dest = make_block_at(dest_bci);
226
break;
227
}
228
229
case Bytecodes::_jsr_w :
230
{
231
cur_block->set_control_bci(bci);
232
if (s.next_bci() < limit_bci) {
233
ciBlock *ret = make_block_at(s.next_bci());
234
}
235
int dest_bci = s.get_far_dest();
236
ciBlock *dest = make_block_at(dest_bci);
237
break;
238
}
239
240
case Bytecodes::_athrow :
241
cur_block->set_may_throw();
242
// fall-through
243
case Bytecodes::_ret :
244
case Bytecodes::_ireturn :
245
case Bytecodes::_lreturn :
246
case Bytecodes::_freturn :
247
case Bytecodes::_dreturn :
248
case Bytecodes::_areturn :
249
case Bytecodes::_return :
250
cur_block->set_control_bci(bci);
251
if (s.next_bci() < limit_bci) {
252
(void) make_block_at(s.next_bci());
253
}
254
break;
255
256
default:
257
break;
258
}
259
}
260
// End the last block
261
cur_block->set_limit_bci(limit_bci);
262
}
263
264
ciMethodBlocks::ciMethodBlocks(Arena *arena, ciMethod *meth): _method(meth),
265
_arena(arena), _num_blocks(0), _code_size(meth->code_size()) {
266
int block_estimate = _code_size / 8;
267
268
_blocks = new(_arena) GrowableArray<ciBlock *>(_arena, block_estimate, 0, NULL);
269
int b2bsize = _code_size * sizeof(ciBlock **);
270
_bci_to_block = (ciBlock **) arena->Amalloc(b2bsize);
271
Copy::zero_to_words((HeapWord*) _bci_to_block, b2bsize / sizeof(HeapWord));
272
273
// create initial block covering the entire method
274
ciBlock *b = new(arena) ciBlock(_method, _num_blocks++, 0);
275
_blocks->append(b);
276
_bci_to_block[0] = b;
277
278
// create blocks for exception handlers
279
if (meth->has_exception_handlers()) {
280
for(ciExceptionHandlerStream str(meth); !str.is_done(); str.next()) {
281
ciExceptionHandler* handler = str.handler();
282
ciBlock *eb = make_block_at(handler->handler_bci());
283
//
284
// Several exception handlers can have the same handler_bci:
285
//
286
// try {
287
// if (a.foo(b) < 0) {
288
// return a.error();
289
// }
290
// return CoderResult.UNDERFLOW;
291
// } finally {
292
// a.position(b);
293
// }
294
//
295
// The try block above is divided into 2 exception blocks
296
// separated by 'areturn' bci.
297
//
298
int ex_start = handler->start();
299
int ex_end = handler->limit();
300
// ensure a block at the start of exception range and start of following code
301
(void) make_block_at(ex_start);
302
if (ex_end < _code_size)
303
(void) make_block_at(ex_end);
304
305
if (eb->is_handler()) {
306
// Extend old handler exception range to cover additional range.
307
int old_ex_start = eb->ex_start_bci();
308
int old_ex_end = eb->ex_limit_bci();
309
if (ex_start > old_ex_start)
310
ex_start = old_ex_start;
311
if (ex_end < old_ex_end)
312
ex_end = old_ex_end;
313
eb->clear_exception_handler(); // Reset exception information
314
}
315
eb->set_exception_range(ex_start, ex_end);
316
}
317
}
318
319
// scan the bytecodes and identify blocks
320
do_analysis();
321
322
// mark blocks that have exception handlers
323
if (meth->has_exception_handlers()) {
324
for(ciExceptionHandlerStream str(meth); !str.is_done(); str.next()) {
325
ciExceptionHandler* handler = str.handler();
326
int ex_start = handler->start();
327
int ex_end = handler->limit();
328
329
int bci = ex_start;
330
while (bci < ex_end) {
331
ciBlock *b = block_containing(bci);
332
b->set_has_handler();
333
bci = b->limit_bci();
334
}
335
}
336
}
337
}
338
339
void ciMethodBlocks::clear_processed() {
340
for (int i = 0; i < _blocks->length(); i++)
341
_blocks->at(i)->clear_processed();
342
}
343
344
#ifndef PRODUCT
345
void ciMethodBlocks::dump() {
346
tty->print("---- blocks for method: ");
347
_method->print();
348
tty->cr();
349
for (int i = 0; i < _blocks->length(); i++) {
350
tty->print(" B%d: ", i); _blocks->at(i)->dump();
351
}
352
}
353
#endif
354
355
ciBlock::ciBlock(ciMethod *method, int index, int start_bci) :
356
_idx(index), _start_bci(start_bci), _limit_bci(-1), _control_bci(fall_through_bci),
357
_flags(0), _ex_start_bci(-1), _ex_limit_bci(-1)
358
#ifndef PRODUCT
359
, _method(method)
360
#endif
361
{
362
}
363
364
void ciBlock::set_exception_range(int start_bci, int limit_bci) {
365
assert(limit_bci >= start_bci, "valid range");
366
assert(!is_handler() && _ex_start_bci == -1 && _ex_limit_bci == -1, "must not be handler");
367
_ex_start_bci = start_bci;
368
_ex_limit_bci = limit_bci;
369
set_handler();
370
}
371
372
#ifndef PRODUCT
373
static const char *flagnames[] = {
374
"Processed",
375
"Handler",
376
"MayThrow",
377
"Jsr",
378
"Ret",
379
"RetTarget",
380
"HasHandler",
381
};
382
383
void ciBlock::dump() {
384
tty->print(" [%d .. %d), {", _start_bci, _limit_bci);
385
for (int i = 0; i < 7; i++) {
386
if ((_flags & (1 << i)) != 0) {
387
tty->print(" %s", flagnames[i]);
388
}
389
}
390
tty->print(" ]");
391
if (is_handler())
392
tty->print(" handles(%d..%d)", _ex_start_bci, _ex_limit_bci);
393
tty->cr();
394
}
395
396
// ------------------------------------------------------------------
397
// ciBlock::print_on
398
void ciBlock::print_on(outputStream* st) const {
399
st->print_cr("--------------------------------------------------------");
400
st->print ("ciBlock [%d - %d) control : ", start_bci(), limit_bci());
401
if (control_bci() == fall_through_bci) {
402
st->print_cr("%d:fall through", limit_bci());
403
} else {
404
st->print_cr("%d:%s", control_bci(),
405
Bytecodes::name(method()->java_code_at_bci(control_bci())));
406
}
407
408
if (Verbose || WizardMode) {
409
method()->print_codes_on(start_bci(), limit_bci(), st);
410
}
411
}
412
#endif
413
414