comparison src/share/vm/ci/bcEscapeAnalyzer.cpp @ 0:a61af66fc99e jdk7-b24

Initial load
author duke
date Sat, 01 Dec 2007 00:00:00 +0000
parents
children 48a3fa21394b
comparison
equal deleted inserted replaced
-1:000000000000 0:a61af66fc99e
1 /*
2 * Copyright 2005-2006 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 *
23 */
24
25
26 #include "incls/_precompiled.incl"
27 #include "incls/_bcEscapeAnalyzer.cpp.incl"
28
29
30 #ifndef PRODUCT
31 #define TRACE_BCEA(level, code) \
32 if (EstimateArgEscape && BCEATraceLevel >= level) { \
33 code; \
34 }
35 #else
36 #define TRACE_BCEA(level, code)
37 #endif
38
39 // Maintain a map of which aguments a local variable or
40 // stack slot may contain. In addition to tracking
41 // arguments, it tracks two special values, "allocated"
42 // which represents any object allocated in the current
43 // method, and "unknown" which is any other object.
44 // Up to 30 arguments are handled, with the last one
45 // representing summary information for any extra arguments
46 class BCEscapeAnalyzer::ArgumentMap {
47 uint _bits;
48 enum {MAXBIT = 29,
49 ALLOCATED = 1,
50 UNKNOWN = 2};
51
52 uint int_to_bit(uint e) const {
53 if (e > MAXBIT)
54 e = MAXBIT;
55 return (1 << (e + 2));
56 }
57
58 public:
59 ArgumentMap() { _bits = 0;}
60 void set_bits(uint bits) { _bits = bits;}
61 uint get_bits() const { return _bits;}
62 void clear() { _bits = 0;}
63 void set_all() { _bits = ~0u; }
64 bool is_empty() const { return _bits == 0; }
65 bool contains(uint var) const { return (_bits & int_to_bit(var)) != 0; }
66 bool is_singleton(uint var) const { return (_bits == int_to_bit(var)); }
67 bool contains_unknown() const { return (_bits & UNKNOWN) != 0; }
68 bool contains_allocated() const { return (_bits & ALLOCATED) != 0; }
69 bool contains_vars() const { return (_bits & (((1 << MAXBIT) -1) << 2)) != 0; }
70 void set(uint var) { _bits = int_to_bit(var); }
71 void add(uint var) { _bits |= int_to_bit(var); }
72 void add_unknown() { _bits = UNKNOWN; }
73 void add_allocated() { _bits = ALLOCATED; }
74 void set_union(const ArgumentMap &am) { _bits |= am._bits; }
75 void set_intersect(const ArgumentMap &am) { _bits |= am._bits; }
76 void set_difference(const ArgumentMap &am) { _bits &= ~am._bits; }
77 void operator=(const ArgumentMap &am) { _bits = am._bits; }
78 bool operator==(const ArgumentMap &am) { return _bits == am._bits; }
79 bool operator!=(const ArgumentMap &am) { return _bits != am._bits; }
80 };
81
82 class BCEscapeAnalyzer::StateInfo {
83 public:
84 ArgumentMap *_vars;
85 ArgumentMap *_stack;
86 short _stack_height;
87 short _max_stack;
88 bool _initialized;
89 ArgumentMap empty_map;
90
91 StateInfo() {
92 empty_map.clear();
93 }
94
95 ArgumentMap raw_pop() { assert(_stack_height > 0, "stack underflow"); return _stack[--_stack_height]; }
96 ArgumentMap apop() { return raw_pop(); }
97 void spop() { raw_pop(); }
98 void lpop() { spop(); spop(); }
99 void raw_push(ArgumentMap i) { assert(_stack_height < _max_stack, "stack overflow"); _stack[_stack_height++] = i; }
100 void apush(ArgumentMap i) { raw_push(i); }
101 void spush() { raw_push(empty_map); }
102 void lpush() { spush(); spush(); }
103
104 };
105
106 void BCEscapeAnalyzer::set_returned(ArgumentMap vars) {
107 for (int i = 0; i <= _arg_size; i++) {
108 if (vars.contains(i))
109 _arg_returned.set_bit(i);
110 }
111 _return_local = _return_local && !(vars.contains_unknown() || vars.contains_allocated());
112 _return_allocated = _return_allocated && vars.contains_allocated() && !(vars.contains_unknown() || vars.contains_vars());
113 }
114
115
116 // return true if any element of vars is an argument
117 bool BCEscapeAnalyzer::is_argument(ArgumentMap vars) {
118 for (int i = 0; i <= _arg_size; i++) {
119 if (vars.contains(i))
120 return true;
121 }
122 return false;
123 }
124
125 // return true if any element of vars is an arg_stack argument
126 bool BCEscapeAnalyzer::is_arg_stack(ArgumentMap vars){
127 if (_conservative)
128 return true;
129 for (int i = 0; i <= _arg_size; i++) {
130 if (vars.contains(i) && _arg_stack.at(i))
131 return true;
132 }
133 return false;
134 }
135
136 void BCEscapeAnalyzer::clear_bits(ArgumentMap vars, BitMap &bm) {
137 for (int i = 0; i <= _arg_size; i++) {
138 if (vars.contains(i)) {
139 bm.clear_bit(i);
140 }
141 }
142 }
143 void BCEscapeAnalyzer::set_method_escape(ArgumentMap vars) {
144 clear_bits(vars, _arg_local);
145 }
146
147 void BCEscapeAnalyzer::set_global_escape(ArgumentMap vars) {
148 clear_bits(vars, _arg_local);
149 clear_bits(vars, _arg_stack);
150 if (vars.contains_allocated())
151 _allocated_escapes = true;
152 }
153
154 void BCEscapeAnalyzer::set_dirty(ArgumentMap vars) {
155 clear_bits(vars, _dirty);
156 }
157
158 bool BCEscapeAnalyzer::is_recursive_call(ciMethod* callee) {
159 for (BCEscapeAnalyzer* scope = this; scope != NULL; scope = scope->_parent) {
160 if (scope->method() == callee) {
161 return true;
162 }
163 }
164 return false;
165 }
166
167 void BCEscapeAnalyzer::invoke(StateInfo &state, Bytecodes::Code code, ciMethod* target, ciKlass* holder) {
168 int i;
169
170 // retrieve information about the callee
171 ciInstanceKlass* klass = target->holder();
172 ciInstanceKlass* calling_klass = method()->holder();
173 ciInstanceKlass* callee_holder = ciEnv::get_instance_klass_for_declared_method_holder(holder);
174 ciInstanceKlass* actual_recv = callee_holder;
175
176 // compute size of arguments
177 int arg_size = target->arg_size();
178 if (!target->is_loaded() && code == Bytecodes::_invokestatic) {
179 arg_size--;
180 }
181 int arg_base = MAX2(state._stack_height - arg_size, 0);
182
183 // direct recursive calls are skipped if they can be bound statically without introducing
184 // dependencies and if parameters are passed at the same position as in the current method
185 // other calls are skipped if there are no unescaped arguments passed to them
186 bool directly_recursive = (method() == target) &&
187 (code != Bytecodes::_invokevirtual || target->is_final_method() || state._stack[arg_base] .is_empty());
188
189 // check if analysis of callee can safely be skipped
190 bool skip_callee = true;
191 for (i = state._stack_height - 1; i >= arg_base && skip_callee; i--) {
192 ArgumentMap arg = state._stack[i];
193 skip_callee = !is_argument(arg) || !is_arg_stack(arg) || (directly_recursive && arg.is_singleton(i - arg_base));
194 }
195 if (skip_callee) {
196 TRACE_BCEA(3, tty->print_cr("[EA] skipping method %s::%s", holder->name()->as_utf8(), target->name()->as_utf8()));
197 for (i = 0; i < arg_size; i++) {
198 set_method_escape(state.raw_pop());
199 }
200 return;
201 }
202
203 // determine actual method (use CHA if necessary)
204 ciMethod* inline_target = NULL;
205 if (target->is_loaded() && klass->is_loaded()
206 && (klass->is_initialized() || klass->is_interface() && target->holder()->is_initialized())
207 && target->will_link(klass, callee_holder, code)) {
208 if (code == Bytecodes::_invokestatic
209 || code == Bytecodes::_invokespecial
210 || code == Bytecodes::_invokevirtual && target->is_final_method()) {
211 inline_target = target;
212 } else {
213 inline_target = target->find_monomorphic_target(calling_klass, callee_holder, actual_recv);
214 }
215 }
216
217 if (inline_target != NULL && !is_recursive_call(inline_target)) {
218 // analyze callee
219 BCEscapeAnalyzer analyzer(inline_target, this);
220
221 // adjust escape state of actual parameters
222 bool must_record_dependencies = false;
223 for (i = arg_size - 1; i >= 0; i--) {
224 ArgumentMap arg = state.raw_pop();
225 if (!is_argument(arg))
226 continue;
227 if (!is_arg_stack(arg)) {
228 // arguments have already been recognized as escaping
229 } else if (analyzer.is_arg_stack(i) && !analyzer.is_arg_returned(i)) {
230 set_method_escape(arg);
231 must_record_dependencies = true;
232 } else {
233 set_global_escape(arg);
234 }
235 }
236
237 // record dependencies if at least one parameter retained stack-allocatable
238 if (must_record_dependencies) {
239 if (code == Bytecodes::_invokeinterface || code == Bytecodes::_invokevirtual && !target->is_final_method()) {
240 _dependencies.append(actual_recv);
241 _dependencies.append(inline_target);
242 }
243 _dependencies.appendAll(analyzer.dependencies());
244 }
245 } else {
246 TRACE_BCEA(1, tty->print_cr("[EA] virtual method %s is not monomorphic.",
247 target->name()->as_utf8()));
248 // conservatively mark all actual parameters as escaping globally
249 for (i = 0; i < arg_size; i++) {
250 ArgumentMap arg = state.raw_pop();
251 if (!is_argument(arg))
252 continue;
253 set_global_escape(arg);
254 }
255 }
256 }
257
258 bool BCEscapeAnalyzer::contains(uint arg_set1, uint arg_set2) {
259 return ((~arg_set1) | arg_set2) == 0;
260 }
261
262
263 void BCEscapeAnalyzer::iterate_one_block(ciBlock *blk, StateInfo &state, GrowableArray<ciBlock *> &successors) {
264
265 blk->set_processed();
266 ciBytecodeStream s(method());
267 int limit_bci = blk->limit_bci();
268 bool fall_through = false;
269 ArgumentMap allocated_obj;
270 allocated_obj.add_allocated();
271 ArgumentMap unknown_obj;
272 unknown_obj.add_unknown();
273 ArgumentMap empty_map;
274
275 s.reset_to_bci(blk->start_bci());
276 while (s.next() != ciBytecodeStream::EOBC() && s.cur_bci() < limit_bci) {
277 fall_through = true;
278 switch (s.cur_bc()) {
279 case Bytecodes::_nop:
280 break;
281 case Bytecodes::_aconst_null:
282 state.apush(empty_map);
283 break;
284 case Bytecodes::_iconst_m1:
285 case Bytecodes::_iconst_0:
286 case Bytecodes::_iconst_1:
287 case Bytecodes::_iconst_2:
288 case Bytecodes::_iconst_3:
289 case Bytecodes::_iconst_4:
290 case Bytecodes::_iconst_5:
291 case Bytecodes::_fconst_0:
292 case Bytecodes::_fconst_1:
293 case Bytecodes::_fconst_2:
294 case Bytecodes::_bipush:
295 case Bytecodes::_sipush:
296 state.spush();
297 break;
298 case Bytecodes::_lconst_0:
299 case Bytecodes::_lconst_1:
300 case Bytecodes::_dconst_0:
301 case Bytecodes::_dconst_1:
302 state.lpush();
303 break;
304 case Bytecodes::_ldc:
305 case Bytecodes::_ldc_w:
306 case Bytecodes::_ldc2_w:
307 if (type2size[s.get_constant().basic_type()] == 1) {
308 state.spush();
309 } else {
310 state.lpush();
311 }
312 break;
313 case Bytecodes::_aload:
314 state.apush(state._vars[s.get_index()]);
315 break;
316 case Bytecodes::_iload:
317 case Bytecodes::_fload:
318 case Bytecodes::_iload_0:
319 case Bytecodes::_iload_1:
320 case Bytecodes::_iload_2:
321 case Bytecodes::_iload_3:
322 case Bytecodes::_fload_0:
323 case Bytecodes::_fload_1:
324 case Bytecodes::_fload_2:
325 case Bytecodes::_fload_3:
326 state.spush();
327 break;
328 case Bytecodes::_lload:
329 case Bytecodes::_dload:
330 case Bytecodes::_lload_0:
331 case Bytecodes::_lload_1:
332 case Bytecodes::_lload_2:
333 case Bytecodes::_lload_3:
334 case Bytecodes::_dload_0:
335 case Bytecodes::_dload_1:
336 case Bytecodes::_dload_2:
337 case Bytecodes::_dload_3:
338 state.lpush();
339 break;
340 case Bytecodes::_aload_0:
341 state.apush(state._vars[0]);
342 break;
343 case Bytecodes::_aload_1:
344 state.apush(state._vars[1]);
345 break;
346 case Bytecodes::_aload_2:
347 state.apush(state._vars[2]);
348 break;
349 case Bytecodes::_aload_3:
350 state.apush(state._vars[3]);
351 break;
352 case Bytecodes::_iaload:
353 case Bytecodes::_faload:
354 case Bytecodes::_baload:
355 case Bytecodes::_caload:
356 case Bytecodes::_saload:
357 state.spop();
358 set_method_escape(state.apop());
359 state.spush();
360 break;
361 case Bytecodes::_laload:
362 case Bytecodes::_daload:
363 state.spop();
364 set_method_escape(state.apop());
365 state.lpush();
366 break;
367 case Bytecodes::_aaload:
368 { state.spop();
369 ArgumentMap array = state.apop();
370 set_method_escape(array);
371 state.apush(unknown_obj);
372 set_dirty(array);
373 }
374 break;
375 case Bytecodes::_istore:
376 case Bytecodes::_fstore:
377 case Bytecodes::_istore_0:
378 case Bytecodes::_istore_1:
379 case Bytecodes::_istore_2:
380 case Bytecodes::_istore_3:
381 case Bytecodes::_fstore_0:
382 case Bytecodes::_fstore_1:
383 case Bytecodes::_fstore_2:
384 case Bytecodes::_fstore_3:
385 state.spop();
386 break;
387 case Bytecodes::_lstore:
388 case Bytecodes::_dstore:
389 case Bytecodes::_lstore_0:
390 case Bytecodes::_lstore_1:
391 case Bytecodes::_lstore_2:
392 case Bytecodes::_lstore_3:
393 case Bytecodes::_dstore_0:
394 case Bytecodes::_dstore_1:
395 case Bytecodes::_dstore_2:
396 case Bytecodes::_dstore_3:
397 state.lpop();
398 break;
399 case Bytecodes::_astore:
400 state._vars[s.get_index()] = state.apop();
401 break;
402 case Bytecodes::_astore_0:
403 state._vars[0] = state.apop();
404 break;
405 case Bytecodes::_astore_1:
406 state._vars[1] = state.apop();
407 break;
408 case Bytecodes::_astore_2:
409 state._vars[2] = state.apop();
410 break;
411 case Bytecodes::_astore_3:
412 state._vars[3] = state.apop();
413 break;
414 case Bytecodes::_iastore:
415 case Bytecodes::_fastore:
416 case Bytecodes::_bastore:
417 case Bytecodes::_castore:
418 case Bytecodes::_sastore:
419 {
420 state.spop();
421 state.spop();
422 ArgumentMap arr = state.apop();
423 set_method_escape(arr);
424 break;
425 }
426 case Bytecodes::_lastore:
427 case Bytecodes::_dastore:
428 {
429 state.lpop();
430 state.spop();
431 ArgumentMap arr = state.apop();
432 set_method_escape(arr);
433 break;
434 }
435 case Bytecodes::_aastore:
436 {
437 set_global_escape(state.apop());
438 state.spop();
439 ArgumentMap arr = state.apop();
440 break;
441 }
442 case Bytecodes::_pop:
443 state.raw_pop();
444 break;
445 case Bytecodes::_pop2:
446 state.raw_pop();
447 state.raw_pop();
448 break;
449 case Bytecodes::_dup:
450 { ArgumentMap w1 = state.raw_pop();
451 state.raw_push(w1);
452 state.raw_push(w1);
453 }
454 break;
455 case Bytecodes::_dup_x1:
456 { ArgumentMap w1 = state.raw_pop();
457 ArgumentMap w2 = state.raw_pop();
458 state.raw_push(w1);
459 state.raw_push(w2);
460 state.raw_push(w1);
461 }
462 break;
463 case Bytecodes::_dup_x2:
464 { ArgumentMap w1 = state.raw_pop();
465 ArgumentMap w2 = state.raw_pop();
466 ArgumentMap w3 = state.raw_pop();
467 state.raw_push(w1);
468 state.raw_push(w3);
469 state.raw_push(w2);
470 state.raw_push(w1);
471 }
472 break;
473 case Bytecodes::_dup2:
474 { ArgumentMap w1 = state.raw_pop();
475 ArgumentMap w2 = state.raw_pop();
476 state.raw_push(w2);
477 state.raw_push(w1);
478 state.raw_push(w2);
479 state.raw_push(w1);
480 }
481 break;
482 case Bytecodes::_dup2_x1:
483 { ArgumentMap w1 = state.raw_pop();
484 ArgumentMap w2 = state.raw_pop();
485 ArgumentMap w3 = state.raw_pop();
486 state.raw_push(w2);
487 state.raw_push(w1);
488 state.raw_push(w3);
489 state.raw_push(w2);
490 state.raw_push(w1);
491 }
492 break;
493 case Bytecodes::_dup2_x2:
494 { ArgumentMap w1 = state.raw_pop();
495 ArgumentMap w2 = state.raw_pop();
496 ArgumentMap w3 = state.raw_pop();
497 ArgumentMap w4 = state.raw_pop();
498 state.raw_push(w2);
499 state.raw_push(w1);
500 state.raw_push(w4);
501 state.raw_push(w3);
502 state.raw_push(w2);
503 state.raw_push(w1);
504 }
505 break;
506 case Bytecodes::_swap:
507 { ArgumentMap w1 = state.raw_pop();
508 ArgumentMap w2 = state.raw_pop();
509 state.raw_push(w1);
510 state.raw_push(w2);
511 }
512 break;
513 case Bytecodes::_iadd:
514 case Bytecodes::_fadd:
515 case Bytecodes::_isub:
516 case Bytecodes::_fsub:
517 case Bytecodes::_imul:
518 case Bytecodes::_fmul:
519 case Bytecodes::_idiv:
520 case Bytecodes::_fdiv:
521 case Bytecodes::_irem:
522 case Bytecodes::_frem:
523 case Bytecodes::_iand:
524 case Bytecodes::_ior:
525 case Bytecodes::_ixor:
526 state.spop();
527 state.spop();
528 state.spush();
529 break;
530 case Bytecodes::_ladd:
531 case Bytecodes::_dadd:
532 case Bytecodes::_lsub:
533 case Bytecodes::_dsub:
534 case Bytecodes::_lmul:
535 case Bytecodes::_dmul:
536 case Bytecodes::_ldiv:
537 case Bytecodes::_ddiv:
538 case Bytecodes::_lrem:
539 case Bytecodes::_drem:
540 case Bytecodes::_land:
541 case Bytecodes::_lor:
542 case Bytecodes::_lxor:
543 state.lpop();
544 state.lpop();
545 state.lpush();
546 break;
547 case Bytecodes::_ishl:
548 case Bytecodes::_ishr:
549 case Bytecodes::_iushr:
550 state.spop();
551 state.spop();
552 state.spush();
553 break;
554 case Bytecodes::_lshl:
555 case Bytecodes::_lshr:
556 case Bytecodes::_lushr:
557 state.spop();
558 state.lpop();
559 state.lpush();
560 break;
561 case Bytecodes::_ineg:
562 case Bytecodes::_fneg:
563 state.spop();
564 state.spush();
565 break;
566 case Bytecodes::_lneg:
567 case Bytecodes::_dneg:
568 state.lpop();
569 state.lpush();
570 break;
571 case Bytecodes::_iinc:
572 break;
573 case Bytecodes::_i2l:
574 case Bytecodes::_i2d:
575 case Bytecodes::_f2l:
576 case Bytecodes::_f2d:
577 state.spop();
578 state.lpush();
579 break;
580 case Bytecodes::_i2f:
581 case Bytecodes::_f2i:
582 state.spop();
583 state.spush();
584 break;
585 case Bytecodes::_l2i:
586 case Bytecodes::_l2f:
587 case Bytecodes::_d2i:
588 case Bytecodes::_d2f:
589 state.lpop();
590 state.spush();
591 break;
592 case Bytecodes::_l2d:
593 case Bytecodes::_d2l:
594 state.lpop();
595 state.lpush();
596 break;
597 case Bytecodes::_i2b:
598 case Bytecodes::_i2c:
599 case Bytecodes::_i2s:
600 state.spop();
601 state.spush();
602 break;
603 case Bytecodes::_lcmp:
604 case Bytecodes::_dcmpl:
605 case Bytecodes::_dcmpg:
606 state.lpop();
607 state.lpop();
608 state.spush();
609 break;
610 case Bytecodes::_fcmpl:
611 case Bytecodes::_fcmpg:
612 state.spop();
613 state.spop();
614 state.spush();
615 break;
616 case Bytecodes::_ifeq:
617 case Bytecodes::_ifne:
618 case Bytecodes::_iflt:
619 case Bytecodes::_ifge:
620 case Bytecodes::_ifgt:
621 case Bytecodes::_ifle:
622 {
623 state.spop();
624 int dest_bci = s.get_dest();
625 assert(_methodBlocks->is_block_start(dest_bci), "branch destination must start a block");
626 assert(s.next_bci() == limit_bci, "branch must end block");
627 successors.push(_methodBlocks->block_containing(dest_bci));
628 break;
629 }
630 case Bytecodes::_if_icmpeq:
631 case Bytecodes::_if_icmpne:
632 case Bytecodes::_if_icmplt:
633 case Bytecodes::_if_icmpge:
634 case Bytecodes::_if_icmpgt:
635 case Bytecodes::_if_icmple:
636 {
637 state.spop();
638 state.spop();
639 int dest_bci = s.get_dest();
640 assert(_methodBlocks->is_block_start(dest_bci), "branch destination must start a block");
641 assert(s.next_bci() == limit_bci, "branch must end block");
642 successors.push(_methodBlocks->block_containing(dest_bci));
643 break;
644 }
645 case Bytecodes::_if_acmpeq:
646 case Bytecodes::_if_acmpne:
647 {
648 set_method_escape(state.apop());
649 set_method_escape(state.apop());
650 int dest_bci = s.get_dest();
651 assert(_methodBlocks->is_block_start(dest_bci), "branch destination must start a block");
652 assert(s.next_bci() == limit_bci, "branch must end block");
653 successors.push(_methodBlocks->block_containing(dest_bci));
654 break;
655 }
656 case Bytecodes::_goto:
657 {
658 int dest_bci = s.get_dest();
659 assert(_methodBlocks->is_block_start(dest_bci), "branch destination must start a block");
660 assert(s.next_bci() == limit_bci, "branch must end block");
661 successors.push(_methodBlocks->block_containing(dest_bci));
662 fall_through = false;
663 break;
664 }
665 case Bytecodes::_jsr:
666 {
667 int dest_bci = s.get_dest();
668 assert(_methodBlocks->is_block_start(dest_bci), "branch destination must start a block");
669 assert(s.next_bci() == limit_bci, "branch must end block");
670 state.apush(empty_map);
671 successors.push(_methodBlocks->block_containing(dest_bci));
672 fall_through = false;
673 break;
674 }
675 case Bytecodes::_ret:
676 // we don't track the destination of a "ret" instruction
677 assert(s.next_bci() == limit_bci, "branch must end block");
678 fall_through = false;
679 break;
680 case Bytecodes::_return:
681 assert(s.next_bci() == limit_bci, "return must end block");
682 fall_through = false;
683 break;
684 case Bytecodes::_tableswitch:
685 {
686 state.spop();
687 Bytecode_tableswitch* switch_ = Bytecode_tableswitch_at(s.cur_bcp());
688 int len = switch_->length();
689 int dest_bci;
690 for (int i = 0; i < len; i++) {
691 dest_bci = s.cur_bci() + switch_->dest_offset_at(i);
692 assert(_methodBlocks->is_block_start(dest_bci), "branch destination must start a block");
693 successors.push(_methodBlocks->block_containing(dest_bci));
694 }
695 dest_bci = s.cur_bci() + switch_->default_offset();
696 assert(_methodBlocks->is_block_start(dest_bci), "branch destination must start a block");
697 successors.push(_methodBlocks->block_containing(dest_bci));
698 assert(s.next_bci() == limit_bci, "branch must end block");
699 fall_through = false;
700 break;
701 }
702 case Bytecodes::_lookupswitch:
703 {
704 state.spop();
705 Bytecode_lookupswitch* switch_ = Bytecode_lookupswitch_at(s.cur_bcp());
706 int len = switch_->number_of_pairs();
707 int dest_bci;
708 for (int i = 0; i < len; i++) {
709 dest_bci = s.cur_bci() + switch_->pair_at(i)->offset();
710 assert(_methodBlocks->is_block_start(dest_bci), "branch destination must start a block");
711 successors.push(_methodBlocks->block_containing(dest_bci));
712 }
713 dest_bci = s.cur_bci() + switch_->default_offset();
714 assert(_methodBlocks->is_block_start(dest_bci), "branch destination must start a block");
715 successors.push(_methodBlocks->block_containing(dest_bci));
716 fall_through = false;
717 break;
718 }
719 case Bytecodes::_ireturn:
720 case Bytecodes::_freturn:
721 state.spop();
722 fall_through = false;
723 break;
724 case Bytecodes::_lreturn:
725 case Bytecodes::_dreturn:
726 state.lpop();
727 fall_through = false;
728 break;
729 case Bytecodes::_areturn:
730 set_returned(state.apop());
731 fall_through = false;
732 break;
733 case Bytecodes::_getstatic:
734 case Bytecodes::_getfield:
735 { bool will_link;
736 ciField* field = s.get_field(will_link);
737 BasicType field_type = field->type()->basic_type();
738 if (s.cur_bc() != Bytecodes::_getstatic) {
739 set_method_escape(state.apop());
740 }
741 if (field_type == T_OBJECT || field_type == T_ARRAY) {
742 state.apush(unknown_obj);
743 } else if (type2size[field_type] == 1) {
744 state.spush();
745 } else {
746 state.lpush();
747 }
748 }
749 break;
750 case Bytecodes::_putstatic:
751 case Bytecodes::_putfield:
752 { bool will_link;
753 ciField* field = s.get_field(will_link);
754 BasicType field_type = field->type()->basic_type();
755 if (field_type == T_OBJECT || field_type == T_ARRAY) {
756 set_global_escape(state.apop());
757 } else if (type2size[field_type] == 1) {
758 state.spop();
759 } else {
760 state.lpop();
761 }
762 if (s.cur_bc() != Bytecodes::_putstatic) {
763 ArgumentMap p = state.apop();
764 set_method_escape(p);
765 }
766 }
767 break;
768 case Bytecodes::_invokevirtual:
769 case Bytecodes::_invokespecial:
770 case Bytecodes::_invokestatic:
771 case Bytecodes::_invokeinterface:
772 { bool will_link;
773 ciMethod* target = s.get_method(will_link);
774 ciKlass* holder = s.get_declared_method_holder();
775 invoke(state, s.cur_bc(), target, holder);
776 ciType* return_type = target->return_type();
777 if (!return_type->is_primitive_type()) {
778 state.apush(unknown_obj);
779 } else if (return_type->is_one_word()) {
780 state.spush();
781 } else if (return_type->is_two_word()) {
782 state.lpush();
783 }
784 }
785 break;
786 case Bytecodes::_xxxunusedxxx:
787 ShouldNotReachHere();
788 break;
789 case Bytecodes::_new:
790 state.apush(allocated_obj);
791 break;
792 case Bytecodes::_newarray:
793 case Bytecodes::_anewarray:
794 state.spop();
795 state.apush(allocated_obj);
796 break;
797 case Bytecodes::_multianewarray:
798 { int i = s.cur_bcp()[3];
799 while (i-- > 0) state.spop();
800 state.apush(allocated_obj);
801 }
802 break;
803 case Bytecodes::_arraylength:
804 set_method_escape(state.apop());
805 state.spush();
806 break;
807 case Bytecodes::_athrow:
808 set_global_escape(state.apop());
809 fall_through = false;
810 break;
811 case Bytecodes::_checkcast:
812 { ArgumentMap obj = state.apop();
813 set_method_escape(obj);
814 state.apush(obj);
815 }
816 break;
817 case Bytecodes::_instanceof:
818 set_method_escape(state.apop());
819 state.spush();
820 break;
821 case Bytecodes::_monitorenter:
822 case Bytecodes::_monitorexit:
823 state.apop();
824 break;
825 case Bytecodes::_wide:
826 ShouldNotReachHere();
827 break;
828 case Bytecodes::_ifnull:
829 case Bytecodes::_ifnonnull:
830 {
831 set_method_escape(state.apop());
832 int dest_bci = s.get_dest();
833 assert(_methodBlocks->is_block_start(dest_bci), "branch destination must start a block");
834 assert(s.next_bci() == limit_bci, "branch must end block");
835 successors.push(_methodBlocks->block_containing(dest_bci));
836 break;
837 }
838 case Bytecodes::_goto_w:
839 {
840 int dest_bci = s.get_far_dest();
841 assert(_methodBlocks->is_block_start(dest_bci), "branch destination must start a block");
842 assert(s.next_bci() == limit_bci, "branch must end block");
843 successors.push(_methodBlocks->block_containing(dest_bci));
844 fall_through = false;
845 break;
846 }
847 case Bytecodes::_jsr_w:
848 {
849 int dest_bci = s.get_far_dest();
850 assert(_methodBlocks->is_block_start(dest_bci), "branch destination must start a block");
851 assert(s.next_bci() == limit_bci, "branch must end block");
852 state.apush(empty_map);
853 successors.push(_methodBlocks->block_containing(dest_bci));
854 fall_through = false;
855 break;
856 }
857 case Bytecodes::_breakpoint:
858 break;
859 default:
860 ShouldNotReachHere();
861 break;
862 }
863
864 }
865 if (fall_through) {
866 int fall_through_bci = s.cur_bci();
867 if (fall_through_bci < _method->code_size()) {
868 assert(_methodBlocks->is_block_start(fall_through_bci), "must fall through to block start.");
869 successors.push(_methodBlocks->block_containing(fall_through_bci));
870 }
871 }
872 }
873
874 void BCEscapeAnalyzer::merge_block_states(StateInfo *blockstates, ciBlock *dest, StateInfo *s_state) {
875 StateInfo *d_state = blockstates+dest->index();
876 int nlocals = _method->max_locals();
877
878 // exceptions may cause transfer of control to handlers in the middle of a
879 // block, so we don't merge the incoming state of exception handlers
880 if (dest->is_handler())
881 return;
882 if (!d_state->_initialized ) {
883 // destination not initialized, just copy
884 for (int i = 0; i < nlocals; i++) {
885 d_state->_vars[i] = s_state->_vars[i];
886 }
887 for (int i = 0; i < s_state->_stack_height; i++) {
888 d_state->_stack[i] = s_state->_stack[i];
889 }
890 d_state->_stack_height = s_state->_stack_height;
891 d_state->_max_stack = s_state->_max_stack;
892 d_state->_initialized = true;
893 } else if (!dest->processed()) {
894 // we have not yet walked the bytecodes of dest, we can merge
895 // the states
896 assert(d_state->_stack_height == s_state->_stack_height, "computed stack heights must match");
897 for (int i = 0; i < nlocals; i++) {
898 d_state->_vars[i].set_union(s_state->_vars[i]);
899 }
900 for (int i = 0; i < s_state->_stack_height; i++) {
901 d_state->_stack[i].set_union(s_state->_stack[i]);
902 }
903 } else {
904 // the bytecodes of dest have already been processed, mark any
905 // arguments in the source state which are not in the dest state
906 // as global escape.
907 // Future refinement: we only need to mark these variable to the
908 // maximum escape of any variables in dest state
909 assert(d_state->_stack_height == s_state->_stack_height, "computed stack heights must match");
910 ArgumentMap extra_vars;
911 for (int i = 0; i < nlocals; i++) {
912 ArgumentMap t;
913 t = s_state->_vars[i];
914 t.set_difference(d_state->_vars[i]);
915 extra_vars.set_union(t);
916 }
917 for (int i = 0; i < s_state->_stack_height; i++) {
918 ArgumentMap t;
919 t.clear();
920 t = s_state->_stack[i];
921 t.set_difference(d_state->_stack[i]);
922 extra_vars.set_union(t);
923 }
924 set_global_escape(extra_vars);
925 }
926 }
927
928 void BCEscapeAnalyzer::iterate_blocks(Arena *arena) {
929 int numblocks = _methodBlocks->num_blocks();
930 int stkSize = _method->max_stack();
931 int numLocals = _method->max_locals();
932 StateInfo state;
933
934 int datacount = (numblocks + 1) * (stkSize + numLocals);
935 int datasize = datacount * sizeof(ArgumentMap);
936 StateInfo *blockstates = (StateInfo *) arena->Amalloc(_methodBlocks->num_blocks() * sizeof(StateInfo));
937 ArgumentMap *statedata = (ArgumentMap *) arena->Amalloc(datasize);
938 for (int i = 0; i < datacount; i++) ::new ((void*)&statedata[i]) ArgumentMap();
939 ArgumentMap *dp = statedata;
940 state._vars = dp;
941 dp += numLocals;
942 state._stack = dp;
943 dp += stkSize;
944 state._initialized = false;
945 state._max_stack = stkSize;
946 for (int i = 0; i < numblocks; i++) {
947 blockstates[i]._vars = dp;
948 dp += numLocals;
949 blockstates[i]._stack = dp;
950 dp += stkSize;
951 blockstates[i]._initialized = false;
952 blockstates[i]._stack_height = 0;
953 blockstates[i]._max_stack = stkSize;
954 }
955 GrowableArray<ciBlock *> worklist(arena, numblocks / 4, 0, NULL);
956 GrowableArray<ciBlock *> successors(arena, 4, 0, NULL);
957
958 _methodBlocks->clear_processed();
959
960 // initialize block 0 state from method signature
961 ArgumentMap allVars; // all oop arguments to method
962 ciSignature* sig = method()->signature();
963 int j = 0;
964 if (!method()->is_static()) {
965 // record information for "this"
966 blockstates[0]._vars[j].set(j);
967 allVars.add(j);
968 j++;
969 }
970 for (int i = 0; i < sig->count(); i++) {
971 ciType* t = sig->type_at(i);
972 if (!t->is_primitive_type()) {
973 blockstates[0]._vars[j].set(j);
974 allVars.add(j);
975 }
976 j += t->size();
977 }
978 blockstates[0]._initialized = true;
979 assert(j == _arg_size, "just checking");
980
981 ArgumentMap unknown_map;
982 unknown_map.add_unknown();
983
984 worklist.push(_methodBlocks->block_containing(0));
985 while(worklist.length() > 0) {
986 ciBlock *blk = worklist.pop();
987 StateInfo *blkState = blockstates+blk->index();
988 if (blk->is_handler() || blk->is_ret_target()) {
989 // for an exception handler or a target of a ret instruction, we assume the worst case,
990 // that any variable or stack slot could contain any argument
991 for (int i = 0; i < numLocals; i++) {
992 state._vars[i] = allVars;
993 }
994 if (blk->is_handler()) {
995 state._stack_height = 1;
996 } else {
997 state._stack_height = blkState->_stack_height;
998 }
999 for (int i = 0; i < state._stack_height; i++) {
1000 state._stack[i] = allVars;
1001 }
1002 } else {
1003 for (int i = 0; i < numLocals; i++) {
1004 state._vars[i] = blkState->_vars[i];
1005 }
1006 for (int i = 0; i < blkState->_stack_height; i++) {
1007 state._stack[i] = blkState->_stack[i];
1008 }
1009 state._stack_height = blkState->_stack_height;
1010 }
1011 iterate_one_block(blk, state, successors);
1012 // if this block has any exception handlers, push them
1013 // onto successor list
1014 if (blk->has_handler()) {
1015 DEBUG_ONLY(int handler_count = 0;)
1016 int blk_start = blk->start_bci();
1017 int blk_end = blk->limit_bci();
1018 for (int i = 0; i < numblocks; i++) {
1019 ciBlock *b = _methodBlocks->block(i);
1020 if (b->is_handler()) {
1021 int ex_start = b->ex_start_bci();
1022 int ex_end = b->ex_limit_bci();
1023 if ((ex_start >= blk_start && ex_start < blk_end) ||
1024 (ex_end > blk_start && ex_end <= blk_end)) {
1025 successors.push(b);
1026 }
1027 DEBUG_ONLY(handler_count++;)
1028 }
1029 }
1030 assert(handler_count > 0, "must find at least one handler");
1031 }
1032 // merge computed variable state with successors
1033 while(successors.length() > 0) {
1034 ciBlock *succ = successors.pop();
1035 merge_block_states(blockstates, succ, &state);
1036 if (!succ->processed())
1037 worklist.push(succ);
1038 }
1039 }
1040 }
1041
1042 bool BCEscapeAnalyzer::do_analysis() {
1043 Arena* arena = CURRENT_ENV->arena();
1044 // identify basic blocks
1045 _methodBlocks = _method->get_method_blocks();
1046
1047 iterate_blocks(arena);
1048 // TEMPORARY
1049 return true;
1050 }
1051
1052 vmIntrinsics::ID BCEscapeAnalyzer::known_intrinsic() {
1053 vmIntrinsics::ID iid = method()->intrinsic_id();
1054
1055 if (iid == vmIntrinsics::_getClass ||
1056 iid == vmIntrinsics::_hashCode)
1057 return iid;
1058 else
1059 return vmIntrinsics::_none;
1060 }
1061
1062 bool BCEscapeAnalyzer::compute_escape_for_intrinsic(vmIntrinsics::ID iid) {
1063 ArgumentMap empty;
1064 empty.clear();
1065 switch (iid) {
1066 case vmIntrinsics::_getClass:
1067 _return_local = false;
1068 break;
1069 case vmIntrinsics::_hashCode:
1070 // initialized state is correct
1071 break;
1072 default:
1073 assert(false, "unexpected intrinsic");
1074 }
1075 return true;
1076 }
1077
1078 void BCEscapeAnalyzer::initialize() {
1079 int i;
1080
1081 // clear escape information (method may have been deoptimized)
1082 methodData()->clear_escape_info();
1083
1084 // initialize escape state of object parameters
1085 ciSignature* sig = method()->signature();
1086 int j = 0;
1087 if (!method()->is_static()) {
1088 _arg_local.set_bit(0);
1089 _arg_stack.set_bit(0);
1090 j++;
1091 }
1092 for (i = 0; i < sig->count(); i++) {
1093 ciType* t = sig->type_at(i);
1094 if (!t->is_primitive_type()) {
1095 _arg_local.set_bit(j);
1096 _arg_stack.set_bit(j);
1097 }
1098 j += t->size();
1099 }
1100 assert(j == _arg_size, "just checking");
1101
1102 // start with optimistic assumption
1103 ciType *rt = _method->return_type();
1104 if (rt->is_primitive_type()) {
1105 _return_local = false;
1106 _return_allocated = false;
1107 } else {
1108 _return_local = true;
1109 _return_allocated = true;
1110 }
1111 _allocated_escapes = false;
1112 }
1113
1114 void BCEscapeAnalyzer::clear_escape_info() {
1115 ciSignature* sig = method()->signature();
1116 int arg_count = sig->count();
1117 ArgumentMap var;
1118 for (int i = 0; i < arg_count; i++) {
1119 var.clear();
1120 var.set(i);
1121 set_global_escape(var);
1122 }
1123 _arg_local.clear();
1124 _arg_stack.clear();
1125 _arg_returned.clear();
1126 _return_local = false;
1127 _return_allocated = false;
1128 _allocated_escapes = true;
1129 }
1130
1131
1132 void BCEscapeAnalyzer::compute_escape_info() {
1133 int i;
1134 assert(!methodData()->has_escape_info(), "do not overwrite escape info");
1135
1136 vmIntrinsics::ID iid = known_intrinsic();
1137
1138 // check if method can be analyzed
1139 if (iid == vmIntrinsics::_none && (method()->is_abstract() || method()->is_native() || !method()->holder()->is_initialized()
1140 || _level > MaxBCEAEstimateLevel
1141 || method()->code_size() > MaxBCEAEstimateSize)) {
1142 if (BCEATraceLevel >= 1) {
1143 tty->print("Skipping method because: ");
1144 if (method()->is_abstract())
1145 tty->print_cr("method is abstract.");
1146 else if (method()->is_native())
1147 tty->print_cr("method is native.");
1148 else if (!method()->holder()->is_initialized())
1149 tty->print_cr("class of method is not initialized.");
1150 else if (_level > MaxBCEAEstimateLevel)
1151 tty->print_cr("level (%d) exceeds MaxBCEAEstimateLevel (%d).",
1152 _level, MaxBCEAEstimateLevel);
1153 else if (method()->code_size() > MaxBCEAEstimateSize)
1154 tty->print_cr("code size (%d) exceeds MaxBCEAEstimateSize.",
1155 method()->code_size(), MaxBCEAEstimateSize);
1156 else
1157 ShouldNotReachHere();
1158 }
1159 clear_escape_info();
1160
1161 return;
1162 }
1163
1164 if (BCEATraceLevel >= 1) {
1165 tty->print("[EA] estimating escape information for");
1166 if (iid != vmIntrinsics::_none)
1167 tty->print(" intrinsic");
1168 method()->print_short_name();
1169 tty->print_cr(" (%d bytes)", method()->code_size());
1170 }
1171
1172 bool success;
1173
1174 initialize();
1175
1176 // do not scan method if it has no object parameters
1177 if (_arg_local.is_empty()) {
1178 methodData()->set_eflag(methodDataOopDesc::estimated);
1179 return;
1180 }
1181
1182 if (iid != vmIntrinsics::_none)
1183 success = compute_escape_for_intrinsic(iid);
1184 else {
1185 success = do_analysis();
1186 }
1187
1188 // dump result of bytecode analysis
1189 #ifndef PRODUCT
1190 if (BCEATraceLevel >= 3) {
1191 tty->print("[EA] estimated escape information for");
1192 if (iid != vmIntrinsics::_none)
1193 tty->print(" intrinsic");
1194 method()->print_short_name();
1195 tty->print_cr(has_dependencies() ? " (not stored)" : "");
1196 tty->print(" non-escaping args: ");
1197 _arg_local.print_on(tty);
1198 tty->print(" stack-allocatable args: ");
1199 _arg_stack.print_on(tty);
1200 if (_return_local) {
1201 tty->print(" returned args: ");
1202 _arg_returned.print_on(tty);
1203 } else if (is_return_allocated()) {
1204 tty->print_cr(" allocated return values");
1205 } else {
1206 tty->print_cr(" non-local return values");
1207 }
1208 tty->cr();
1209 tty->print(" flags: ");
1210 if (_return_allocated)
1211 tty->print(" return_allocated");
1212 tty->cr();
1213 }
1214
1215 #endif
1216 // don't store interprocedural escape information if it introduces dependencies
1217 // or if method data is empty
1218 //
1219 if (!has_dependencies() && !methodData()->is_empty()) {
1220 for (i = 0; i < _arg_size; i++) {
1221 if (_arg_local.at(i)) {
1222 assert(_arg_stack.at(i), "inconsistent escape info");
1223 methodData()->set_arg_local(i);
1224 methodData()->set_arg_stack(i);
1225 } else if (_arg_stack.at(i)) {
1226 methodData()->set_arg_stack(i);
1227 }
1228 if (_arg_returned.at(i)) {
1229 methodData()->set_arg_returned(i);
1230 }
1231 }
1232 if (_return_local) {
1233 methodData()->set_eflag(methodDataOopDesc::return_local);
1234 }
1235 methodData()->set_eflag(methodDataOopDesc::estimated);
1236 }
1237 }
1238
1239 void BCEscapeAnalyzer::read_escape_info() {
1240 assert(methodData()->has_escape_info(), "no escape info available");
1241
1242 // read escape information from method descriptor
1243 for (int i = 0; i < _arg_size; i++) {
1244 _arg_local.at_put(i, methodData()->is_arg_local(i));
1245 _arg_stack.at_put(i, methodData()->is_arg_stack(i));
1246 _arg_returned.at_put(i, methodData()->is_arg_returned(i));
1247 }
1248 _return_local = methodData()->eflag_set(methodDataOopDesc::return_local);
1249
1250 // dump result of loaded escape information
1251 #ifndef PRODUCT
1252 if (BCEATraceLevel >= 4) {
1253 tty->print(" non-escaping args: ");
1254 _arg_local.print_on(tty);
1255 tty->print(" stack-allocatable args: ");
1256 _arg_stack.print_on(tty);
1257 if (_return_local) {
1258 tty->print(" returned args: ");
1259 _arg_returned.print_on(tty);
1260 } else {
1261 tty->print_cr(" non-local return values");
1262 }
1263 tty->print(" modified args: ");
1264 tty->cr();
1265 }
1266 #endif
1267
1268 }
1269
1270
1271 BCEscapeAnalyzer::BCEscapeAnalyzer(ciMethod* method, BCEscapeAnalyzer* parent)
1272 : _conservative(method == NULL || !EstimateArgEscape)
1273 , _method(method)
1274 , _methodData(method ? method->method_data() : NULL)
1275 , _arg_size(method ? method->arg_size() : 0)
1276 , _stack()
1277 , _arg_local(_arg_size)
1278 , _arg_stack(_arg_size)
1279 , _arg_returned(_arg_size)
1280 , _dirty(_arg_size)
1281 , _return_local(false)
1282 , _return_allocated(false)
1283 , _allocated_escapes(false)
1284 , _dependencies()
1285 , _parent(parent)
1286 , _level(parent == NULL ? 0 : parent->level() + 1) {
1287 if (!_conservative) {
1288 _arg_local.clear();
1289 _arg_stack.clear();
1290 _arg_returned.clear();
1291 _dirty.clear();
1292 Arena* arena = CURRENT_ENV->arena();
1293
1294 if (methodData() == NULL)
1295 return;
1296 bool printit = _method->should_print_assembly();
1297 if (methodData()->has_escape_info()) {
1298 TRACE_BCEA(2, tty->print_cr("[EA] Reading previous results for %s.%s",
1299 method->holder()->name()->as_utf8(),
1300 method->name()->as_utf8()));
1301 read_escape_info();
1302 } else {
1303 TRACE_BCEA(2, tty->print_cr("[EA] computing results for %s.%s",
1304 method->holder()->name()->as_utf8(),
1305 method->name()->as_utf8()));
1306
1307 compute_escape_info();
1308 methodData()->update_escape_info();
1309 }
1310 }
1311 }
1312
1313 void BCEscapeAnalyzer::copy_dependencies(Dependencies *deps) {
1314 if(!has_dependencies())
1315 return;
1316 for (int i = 0; i < _dependencies.length(); i+=2) {
1317 ciKlass *k = _dependencies[i]->as_klass();
1318 ciMethod *m = _dependencies[i+1]->as_method();
1319 deps->assert_unique_concrete_method(k, m);
1320 }
1321 }