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