Print this page
rev 1024 : imported patch indy-cleanup-6893081.patch
Split |
Close |
Expand all |
Collapse all |
--- old/src/share/vm/compiler/methodLiveness.cpp
+++ new/src/share/vm/compiler/methodLiveness.cpp
1 1 /*
2 2 * Copyright 1998-2006 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 21 * have any questions.
22 22 *
23 23 */
24 24
25 25 // The MethodLiveness class performs a simple liveness analysis on a method
26 26 // in order to decide which locals are live (that is, will be used again) at
27 27 // a particular bytecode index (bci).
28 28 //
29 29 // The algorithm goes:
30 30 //
31 31 // 1. Break the method into a set of basic blocks. For each basic block we
32 32 // also keep track of its set of predecessors through normal control flow
33 33 // and predecessors through exceptional control flow.
34 34 //
35 35 // 2. For each basic block, compute two sets, gen (the set of values used before
36 36 // they are defined) and kill (the set of values defined before they are used)
37 37 // in the basic block. A basic block "needs" the locals in its gen set to
38 38 // perform its computation. A basic block "provides" values for the locals in
39 39 // its kill set, allowing a need from a successor to be ignored.
40 40 //
41 41 // 3. Liveness information (the set of locals which are needed) is pushed backwards through
42 42 // the program, from blocks to their predecessors. We compute and store liveness
43 43 // information for the normal/exceptional exit paths for each basic block. When
44 44 // this process reaches a fixed point, we are done.
45 45 //
46 46 // 4. When we are asked about the liveness at a particular bci with a basic block, we
47 47 // compute gen/kill sets which represent execution from that bci to the exit of
48 48 // its blocks. We then compose this range gen/kill information with the normal
49 49 // and exceptional exit information for the block to produce liveness information
50 50 // at that bci.
51 51 //
52 52 // The algorithm is approximate in many respects. Notably:
53 53 //
54 54 // 1. We do not do the analysis necessary to match jsr's with the appropriate ret.
55 55 // Instead we make the conservative assumption that any ret can return to any
56 56 // jsr return site.
57 57 // 2. Instead of computing the effects of exceptions at every instruction, we
58 58 // summarize the effects of all exceptional continuations from the block as
59 59 // a single set (_exception_exit), losing some information but simplifying the
60 60 // analysis.
61 61
62 62
63 63 # include "incls/_precompiled.incl"
64 64 # include "incls/_methodLiveness.cpp.incl"
65 65
66 66 //--------------------------------------------------------------------------
67 67 // The BitCounter class is used for counting the number of bits set in
68 68 // some BitMap. It is only used when collecting liveness statistics.
69 69
70 70 #ifndef PRODUCT
71 71
72 72 class BitCounter: public BitMapClosure {
73 73 private:
74 74 int _count;
75 75 public:
76 76 BitCounter() : _count(0) {}
77 77
78 78 // Callback when bit in map is set
79 79 virtual bool do_bit(size_t offset) {
80 80 _count++;
81 81 return true;
82 82 }
83 83
84 84 int count() {
85 85 return _count;
86 86 }
87 87 };
88 88
89 89
90 90 //--------------------------------------------------------------------------
91 91
92 92
93 93 // Counts
94 94 long MethodLiveness::_total_bytes = 0;
95 95 int MethodLiveness::_total_methods = 0;
96 96
97 97 long MethodLiveness::_total_blocks = 0;
98 98 int MethodLiveness::_max_method_blocks = 0;
99 99
100 100 long MethodLiveness::_total_edges = 0;
101 101 int MethodLiveness::_max_block_edges = 0;
102 102
103 103 long MethodLiveness::_total_exc_edges = 0;
104 104 int MethodLiveness::_max_block_exc_edges = 0;
105 105
106 106 long MethodLiveness::_total_method_locals = 0;
107 107 int MethodLiveness::_max_method_locals = 0;
108 108
109 109 long MethodLiveness::_total_locals_queried = 0;
110 110 long MethodLiveness::_total_live_locals_queried = 0;
111 111
112 112 long MethodLiveness::_total_visits = 0;
113 113
114 114 #endif
115 115
116 116 // Timers
117 117 elapsedTimer MethodLiveness::_time_build_graph;
118 118 elapsedTimer MethodLiveness::_time_gen_kill;
119 119 elapsedTimer MethodLiveness::_time_flow;
120 120 elapsedTimer MethodLiveness::_time_query;
121 121 elapsedTimer MethodLiveness::_time_total;
122 122
123 123 MethodLiveness::MethodLiveness(Arena* arena, ciMethod* method)
124 124 #ifdef COMPILER1
125 125 : _bci_block_start((uintptr_t*)arena->Amalloc((method->code_size() >> LogBitsPerByte) + 1), method->code_size())
126 126 #endif
127 127 {
128 128 _arena = arena;
129 129 _method = method;
130 130 _bit_map_size_bits = method->max_locals();
131 131 _bit_map_size_words = (_bit_map_size_bits / sizeof(unsigned int)) + 1;
132 132
133 133 #ifdef COMPILER1
134 134 _bci_block_start.clear();
135 135 #endif
136 136 }
137 137
138 138 void MethodLiveness::compute_liveness() {
139 139 #ifndef PRODUCT
140 140 if (TraceLivenessGen) {
141 141 tty->print_cr("################################################################");
142 142 tty->print("# Computing liveness information for ");
143 143 method()->print_short_name();
144 144 }
145 145
146 146 if (TimeLivenessAnalysis) _time_total.start();
147 147 #endif
148 148
149 149 {
150 150 TraceTime buildGraph(NULL, &_time_build_graph, TimeLivenessAnalysis);
151 151 init_basic_blocks();
152 152 }
153 153 {
154 154 TraceTime genKill(NULL, &_time_gen_kill, TimeLivenessAnalysis);
155 155 init_gen_kill();
156 156 }
157 157 {
158 158 TraceTime flow(NULL, &_time_flow, TimeLivenessAnalysis);
159 159 propagate_liveness();
160 160 }
161 161
162 162 #ifndef PRODUCT
163 163 if (TimeLivenessAnalysis) _time_total.stop();
164 164
165 165 if (TimeLivenessAnalysis) {
166 166 // Collect statistics
167 167 _total_bytes += method()->code_size();
168 168 _total_methods++;
169 169
170 170 int num_blocks = _block_count;
171 171 _total_blocks += num_blocks;
172 172 _max_method_blocks = MAX2(num_blocks,_max_method_blocks);
173 173
174 174 for (int i=0; i<num_blocks; i++) {
175 175 BasicBlock *block = _block_list[i];
176 176
177 177 int numEdges = block->_normal_predecessors->length();
178 178 int numExcEdges = block->_exception_predecessors->length();
179 179
180 180 _total_edges += numEdges;
181 181 _total_exc_edges += numExcEdges;
182 182 _max_block_edges = MAX2(numEdges,_max_block_edges);
183 183 _max_block_exc_edges = MAX2(numExcEdges,_max_block_exc_edges);
184 184 }
185 185
186 186 int numLocals = _bit_map_size_bits;
187 187 _total_method_locals += numLocals;
188 188 _max_method_locals = MAX2(numLocals,_max_method_locals);
189 189 }
190 190 #endif
191 191 }
192 192
193 193
194 194 void MethodLiveness::init_basic_blocks() {
195 195 bool bailout = false;
196 196
197 197 int method_len = method()->code_size();
198 198 ciMethodBlocks *mblocks = method()->get_method_blocks();
199 199
200 200 // Create an array to store the bci->BasicBlock mapping.
201 201 _block_map = new (arena()) GrowableArray<BasicBlock*>(arena(), method_len, method_len, NULL);
202 202
203 203 _block_count = mblocks->num_blocks();
204 204 _block_list = (BasicBlock **) arena()->Amalloc(sizeof(BasicBlock *) * _block_count);
205 205
206 206 // Used for patching up jsr/ret control flow.
207 207 GrowableArray<BasicBlock*>* jsr_exit_list = new GrowableArray<BasicBlock*>(5);
208 208 GrowableArray<BasicBlock*>* ret_list = new GrowableArray<BasicBlock*>(5);
209 209
210 210 // generate our block list from ciMethodBlocks
211 211 for (int blk = 0; blk < _block_count; blk++) {
212 212 ciBlock *cib = mblocks->block(blk);
213 213 int start_bci = cib->start_bci();
214 214 _block_list[blk] = new (arena()) BasicBlock(this, start_bci, cib->limit_bci());
215 215 _block_map->at_put(start_bci, _block_list[blk]);
216 216 #ifdef COMPILER1
217 217 // mark all bcis where a new basic block starts
218 218 _bci_block_start.set_bit(start_bci);
219 219 #endif // COMPILER1
220 220 }
221 221 // fill in the predecessors of blocks
222 222 ciBytecodeStream bytes(method());
223 223
224 224 for (int blk = 0; blk < _block_count; blk++) {
225 225 BasicBlock *current_block = _block_list[blk];
226 226 int bci = mblocks->block(blk)->control_bci();
227 227
228 228 if (bci == ciBlock::fall_through_bci) {
229 229 int limit = current_block->limit_bci();
230 230 if (limit < method_len) {
231 231 BasicBlock *next = _block_map->at(limit);
232 232 assert( next != NULL, "must be a block immediately following this one.");
233 233 next->add_normal_predecessor(current_block);
234 234 }
235 235 continue;
236 236 }
237 237 bytes.reset_to_bci(bci);
238 238 Bytecodes::Code code = bytes.next();
239 239 BasicBlock *dest;
240 240
241 241 // Now we need to interpret the instruction's effect
242 242 // on control flow.
243 243 assert (current_block != NULL, "we must have a current block");
244 244 switch (code) {
245 245 case Bytecodes::_ifeq:
246 246 case Bytecodes::_ifne:
247 247 case Bytecodes::_iflt:
248 248 case Bytecodes::_ifge:
249 249 case Bytecodes::_ifgt:
250 250 case Bytecodes::_ifle:
251 251 case Bytecodes::_if_icmpeq:
252 252 case Bytecodes::_if_icmpne:
253 253 case Bytecodes::_if_icmplt:
254 254 case Bytecodes::_if_icmpge:
255 255 case Bytecodes::_if_icmpgt:
256 256 case Bytecodes::_if_icmple:
257 257 case Bytecodes::_if_acmpeq:
258 258 case Bytecodes::_if_acmpne:
259 259 case Bytecodes::_ifnull:
260 260 case Bytecodes::_ifnonnull:
261 261 // Two way branch. Set predecessors at each destination.
262 262 dest = _block_map->at(bytes.next_bci());
263 263 assert(dest != NULL, "must be a block immediately following this one.");
264 264 dest->add_normal_predecessor(current_block);
265 265
266 266 dest = _block_map->at(bytes.get_dest());
267 267 assert(dest != NULL, "branch desination must start a block.");
268 268 dest->add_normal_predecessor(current_block);
269 269 break;
270 270 case Bytecodes::_goto:
271 271 dest = _block_map->at(bytes.get_dest());
272 272 assert(dest != NULL, "branch desination must start a block.");
273 273 dest->add_normal_predecessor(current_block);
274 274 break;
275 275 case Bytecodes::_goto_w:
276 276 dest = _block_map->at(bytes.get_far_dest());
277 277 assert(dest != NULL, "branch desination must start a block.");
278 278 dest->add_normal_predecessor(current_block);
279 279 break;
280 280 case Bytecodes::_tableswitch:
281 281 {
282 282 Bytecode_tableswitch *tableswitch =
283 283 Bytecode_tableswitch_at(bytes.cur_bcp());
284 284
285 285 int len = tableswitch->length();
286 286
287 287 dest = _block_map->at(bci + tableswitch->default_offset());
288 288 assert(dest != NULL, "branch desination must start a block.");
289 289 dest->add_normal_predecessor(current_block);
290 290 while (--len >= 0) {
291 291 dest = _block_map->at(bci + tableswitch->dest_offset_at(len));
292 292 assert(dest != NULL, "branch desination must start a block.");
293 293 dest->add_normal_predecessor(current_block);
294 294 }
295 295 break;
296 296 }
297 297
298 298 case Bytecodes::_lookupswitch:
299 299 {
300 300 Bytecode_lookupswitch *lookupswitch =
301 301 Bytecode_lookupswitch_at(bytes.cur_bcp());
302 302
303 303 int npairs = lookupswitch->number_of_pairs();
304 304
305 305 dest = _block_map->at(bci + lookupswitch->default_offset());
306 306 assert(dest != NULL, "branch desination must start a block.");
307 307 dest->add_normal_predecessor(current_block);
308 308 while(--npairs >= 0) {
309 309 LookupswitchPair *pair = lookupswitch->pair_at(npairs);
310 310 dest = _block_map->at( bci + pair->offset());
311 311 assert(dest != NULL, "branch desination must start a block.");
312 312 dest->add_normal_predecessor(current_block);
313 313 }
314 314 break;
315 315 }
316 316
317 317 case Bytecodes::_jsr:
318 318 {
319 319 assert(bytes.is_wide()==false, "sanity check");
320 320 dest = _block_map->at(bytes.get_dest());
321 321 assert(dest != NULL, "branch desination must start a block.");
322 322 dest->add_normal_predecessor(current_block);
323 323 BasicBlock *jsrExit = _block_map->at(current_block->limit_bci());
324 324 assert(jsrExit != NULL, "jsr return bci must start a block.");
325 325 jsr_exit_list->append(jsrExit);
326 326 break;
327 327 }
328 328 case Bytecodes::_jsr_w:
329 329 {
330 330 dest = _block_map->at(bytes.get_far_dest());
331 331 assert(dest != NULL, "branch desination must start a block.");
332 332 dest->add_normal_predecessor(current_block);
333 333 BasicBlock *jsrExit = _block_map->at(current_block->limit_bci());
334 334 assert(jsrExit != NULL, "jsr return bci must start a block.");
335 335 jsr_exit_list->append(jsrExit);
336 336 break;
337 337 }
338 338
339 339 case Bytecodes::_wide:
340 340 assert(false, "wide opcodes should not be seen here");
341 341 break;
342 342 case Bytecodes::_athrow:
343 343 case Bytecodes::_ireturn:
344 344 case Bytecodes::_lreturn:
345 345 case Bytecodes::_freturn:
346 346 case Bytecodes::_dreturn:
347 347 case Bytecodes::_areturn:
348 348 case Bytecodes::_return:
349 349 // These opcodes are not the normal predecessors of any other opcodes.
350 350 break;
351 351 case Bytecodes::_ret:
352 352 // We will patch up jsr/rets in a subsequent pass.
353 353 ret_list->append(current_block);
354 354 break;
355 355 case Bytecodes::_breakpoint:
356 356 // Bail out of there are breakpoints in here.
357 357 bailout = true;
358 358 break;
359 359 default:
360 360 // Do nothing.
361 361 break;
362 362 }
363 363 }
364 364 // Patch up the jsr/ret's. We conservatively assume that any ret
365 365 // can return to any jsr site.
366 366 int ret_list_len = ret_list->length();
367 367 int jsr_exit_list_len = jsr_exit_list->length();
368 368 if (ret_list_len > 0 && jsr_exit_list_len > 0) {
369 369 for (int i = jsr_exit_list_len - 1; i >= 0; i--) {
370 370 BasicBlock *jsrExit = jsr_exit_list->at(i);
371 371 for (int i = ret_list_len - 1; i >= 0; i--) {
372 372 jsrExit->add_normal_predecessor(ret_list->at(i));
373 373 }
374 374 }
375 375 }
376 376
377 377 // Compute exception edges.
378 378 for (int b=_block_count-1; b >= 0; b--) {
379 379 BasicBlock *block = _block_list[b];
380 380 int block_start = block->start_bci();
381 381 int block_limit = block->limit_bci();
382 382 ciExceptionHandlerStream handlers(method());
383 383 for (; !handlers.is_done(); handlers.next()) {
384 384 ciExceptionHandler* handler = handlers.handler();
385 385 int start = handler->start();
386 386 int limit = handler->limit();
387 387 int handler_bci = handler->handler_bci();
388 388
389 389 int intersect_start = MAX2(block_start, start);
390 390 int intersect_limit = MIN2(block_limit, limit);
391 391 if (intersect_start < intersect_limit) {
392 392 // The catch range has a nonempty intersection with this
393 393 // basic block. That means this basic block can be an
394 394 // exceptional predecessor.
395 395 _block_map->at(handler_bci)->add_exception_predecessor(block);
396 396
397 397 if (handler->is_catch_all()) {
398 398 // This is a catch-all block.
399 399 if (intersect_start == block_start && intersect_limit == block_limit) {
400 400 // The basic block is entirely contained in this catch-all block.
401 401 // Skip the rest of the exception handlers -- they can never be
402 402 // reached in execution.
403 403 break;
404 404 }
405 405 }
406 406 }
407 407 }
408 408 }
409 409 }
410 410
411 411 void MethodLiveness::init_gen_kill() {
412 412 for (int i=_block_count-1; i >= 0; i--) {
413 413 _block_list[i]->compute_gen_kill(method());
414 414 }
415 415 }
416 416
417 417 void MethodLiveness::propagate_liveness() {
418 418 int num_blocks = _block_count;
419 419 BasicBlock *block;
420 420
421 421 // We start our work list off with all blocks in it.
422 422 // Alternately, we could start off the work list with the list of all
423 423 // blocks which could exit the method directly, along with one block
424 424 // from any infinite loop. If this matters, it can be changed. It
425 425 // may not be clear from looking at the code, but the order of the
426 426 // workList will be the opposite of the creation order of the basic
427 427 // blocks, which should be decent for quick convergence (with the
428 428 // possible exception of exception handlers, which are all created
429 429 // early).
430 430 _work_list = NULL;
431 431 for (int i = 0; i < num_blocks; i++) {
432 432 block = _block_list[i];
433 433 block->set_next(_work_list);
434 434 block->set_on_work_list(true);
435 435 _work_list = block;
436 436 }
437 437
438 438
439 439 while ((block = work_list_get()) != NULL) {
440 440 block->propagate(this);
441 441 NOT_PRODUCT(_total_visits++;)
442 442 }
443 443 }
444 444
445 445 void MethodLiveness::work_list_add(BasicBlock *block) {
446 446 if (!block->on_work_list()) {
447 447 block->set_next(_work_list);
448 448 block->set_on_work_list(true);
449 449 _work_list = block;
450 450 }
451 451 }
452 452
453 453 MethodLiveness::BasicBlock *MethodLiveness::work_list_get() {
454 454 BasicBlock *block = _work_list;
455 455 if (block != NULL) {
456 456 block->set_on_work_list(false);
457 457 _work_list = block->next();
458 458 }
459 459 return block;
460 460 }
461 461
462 462
463 463 MethodLivenessResult MethodLiveness::get_liveness_at(int entry_bci) {
464 464 int bci = entry_bci;
465 465 bool is_entry = false;
466 466 if (entry_bci == InvocationEntryBci) {
467 467 is_entry = true;
468 468 bci = 0;
469 469 }
470 470
471 471 MethodLivenessResult answer((uintptr_t*)NULL,0);
472 472
473 473 if (_block_count > 0) {
474 474 if (TimeLivenessAnalysis) _time_total.start();
475 475 if (TimeLivenessAnalysis) _time_query.start();
476 476
477 477 assert( 0 <= bci && bci < method()->code_size(), "bci out of range" );
478 478 BasicBlock *block = _block_map->at(bci);
479 479 // We may not be at the block start, so search backwards to find the block
480 480 // containing bci.
481 481 int t = bci;
482 482 while (block == NULL && t > 0) {
483 483 block = _block_map->at(--t);
484 484 }
485 485 assert( block != NULL, "invalid bytecode index; must be instruction index" );
486 486 assert(bci >= block->start_bci() && bci < block->limit_bci(), "block must contain bci.");
487 487
488 488 answer = block->get_liveness_at(method(), bci);
489 489
490 490 if (is_entry && method()->is_synchronized() && !method()->is_static()) {
491 491 // Synchronized methods use the receiver once on entry.
492 492 answer.at_put(0, true);
493 493 }
494 494
495 495 #ifndef PRODUCT
496 496 if (TraceLivenessQuery) {
497 497 tty->print("Liveness query of ");
498 498 method()->print_short_name();
499 499 tty->print(" @ %d : result is ", bci);
500 500 answer.print_on(tty);
501 501 }
502 502
503 503 if (TimeLivenessAnalysis) _time_query.stop();
504 504 if (TimeLivenessAnalysis) _time_total.stop();
505 505 #endif
506 506 }
507 507
508 508 #ifndef PRODUCT
509 509 if (TimeLivenessAnalysis) {
510 510 // Collect statistics.
511 511 _total_locals_queried += _bit_map_size_bits;
512 512 BitCounter counter;
513 513 answer.iterate(&counter);
514 514 _total_live_locals_queried += counter.count();
515 515 }
516 516 #endif
517 517
518 518 return answer;
519 519 }
520 520
521 521
522 522 #ifndef PRODUCT
523 523
524 524 void MethodLiveness::print_times() {
525 525 tty->print_cr ("Accumulated liveness analysis times/statistics:");
526 526 tty->print_cr ("-----------------------------------------------");
527 527 tty->print_cr (" Total : %3.3f sec.", _time_total.seconds());
528 528 tty->print_cr (" Build graph : %3.3f sec. (%2.2f%%)", _time_build_graph.seconds(),
529 529 _time_build_graph.seconds() * 100 / _time_total.seconds());
530 530 tty->print_cr (" Gen / Kill : %3.3f sec. (%2.2f%%)", _time_gen_kill.seconds(),
531 531 _time_gen_kill.seconds() * 100 / _time_total.seconds());
532 532 tty->print_cr (" Dataflow : %3.3f sec. (%2.2f%%)", _time_flow.seconds(),
533 533 _time_flow.seconds() * 100 / _time_total.seconds());
534 534 tty->print_cr (" Query : %3.3f sec. (%2.2f%%)", _time_query.seconds(),
535 535 _time_query.seconds() * 100 / _time_total.seconds());
536 536 tty->print_cr (" #bytes : %8d (%3.0f bytes per sec)",
537 537 _total_bytes,
538 538 _total_bytes / _time_total.seconds());
539 539 tty->print_cr (" #methods : %8d (%3.0f methods per sec)",
540 540 _total_methods,
541 541 _total_methods / _time_total.seconds());
542 542 tty->print_cr (" avg locals : %3.3f max locals : %3d",
543 543 (float)_total_method_locals / _total_methods,
544 544 _max_method_locals);
545 545 tty->print_cr (" avg blocks : %3.3f max blocks : %3d",
546 546 (float)_total_blocks / _total_methods,
547 547 _max_method_blocks);
548 548 tty->print_cr (" avg bytes : %3.3f",
549 549 (float)_total_bytes / _total_methods);
550 550 tty->print_cr (" #blocks : %8d",
551 551 _total_blocks);
552 552 tty->print_cr (" avg normal predecessors : %3.3f max normal predecessors : %3d",
553 553 (float)_total_edges / _total_blocks,
554 554 _max_block_edges);
555 555 tty->print_cr (" avg exception predecessors : %3.3f max exception predecessors : %3d",
556 556 (float)_total_exc_edges / _total_blocks,
557 557 _max_block_exc_edges);
558 558 tty->print_cr (" avg visits : %3.3f",
559 559 (float)_total_visits / _total_blocks);
560 560 tty->print_cr (" #locals queried : %8d #live : %8d %%live : %2.2f%%",
561 561 _total_locals_queried,
562 562 _total_live_locals_queried,
563 563 100.0 * _total_live_locals_queried / _total_locals_queried);
564 564 }
565 565
566 566 #endif
567 567
568 568
569 569 MethodLiveness::BasicBlock::BasicBlock(MethodLiveness *analyzer, int start, int limit) :
570 570 _gen((uintptr_t*)analyzer->arena()->Amalloc(BytesPerWord * analyzer->bit_map_size_words()),
571 571 analyzer->bit_map_size_bits()),
572 572 _kill((uintptr_t*)analyzer->arena()->Amalloc(BytesPerWord * analyzer->bit_map_size_words()),
573 573 analyzer->bit_map_size_bits()),
574 574 _entry((uintptr_t*)analyzer->arena()->Amalloc(BytesPerWord * analyzer->bit_map_size_words()),
575 575 analyzer->bit_map_size_bits()),
576 576 _normal_exit((uintptr_t*)analyzer->arena()->Amalloc(BytesPerWord * analyzer->bit_map_size_words()),
577 577 analyzer->bit_map_size_bits()),
578 578 _exception_exit((uintptr_t*)analyzer->arena()->Amalloc(BytesPerWord * analyzer->bit_map_size_words()),
579 579 analyzer->bit_map_size_bits()),
580 580 _last_bci(-1) {
581 581 _analyzer = analyzer;
582 582 _start_bci = start;
583 583 _limit_bci = limit;
584 584 _normal_predecessors =
585 585 new (analyzer->arena()) GrowableArray<MethodLiveness::BasicBlock*>(analyzer->arena(), 5, 0, NULL);
586 586 _exception_predecessors =
587 587 new (analyzer->arena()) GrowableArray<MethodLiveness::BasicBlock*>(analyzer->arena(), 5, 0, NULL);
588 588 _normal_exit.clear();
589 589 _exception_exit.clear();
590 590 _entry.clear();
591 591
592 592 // this initialization is not strictly necessary.
593 593 // _gen and _kill are cleared at the beginning of compute_gen_kill_range()
594 594 _gen.clear();
595 595 _kill.clear();
596 596 }
597 597
598 598
599 599
600 600 MethodLiveness::BasicBlock *MethodLiveness::BasicBlock::split(int split_bci) {
601 601 int start = _start_bci;
602 602 int limit = _limit_bci;
603 603
604 604 if (TraceLivenessGen) {
605 605 tty->print_cr(" ** Splitting block (%d,%d) at %d", start, limit, split_bci);
606 606 }
607 607
608 608 GrowableArray<BasicBlock*>* save_predecessors = _normal_predecessors;
609 609
610 610 assert (start < split_bci && split_bci < limit, "improper split");
611 611
612 612 // Make a new block to cover the first half of the range.
613 613 BasicBlock *first_half = new (_analyzer->arena()) BasicBlock(_analyzer, start, split_bci);
614 614
615 615 // Assign correct values to the second half (this)
616 616 _normal_predecessors = first_half->_normal_predecessors;
617 617 _start_bci = split_bci;
618 618 add_normal_predecessor(first_half);
619 619
620 620 // Assign correct predecessors to the new first half
621 621 first_half->_normal_predecessors = save_predecessors;
622 622
623 623 return first_half;
624 624 }
625 625
626 626 void MethodLiveness::BasicBlock::compute_gen_kill(ciMethod* method) {
627 627 ciBytecodeStream bytes(method);
628 628 bytes.reset_to_bci(start_bci());
629 629 bytes.set_max_bci(limit_bci());
630 630 compute_gen_kill_range(&bytes);
631 631
632 632 }
633 633
634 634 void MethodLiveness::BasicBlock::compute_gen_kill_range(ciBytecodeStream *bytes) {
635 635 _gen.clear();
636 636 _kill.clear();
637 637
638 638 while (bytes->next() != ciBytecodeStream::EOBC()) {
639 639 compute_gen_kill_single(bytes);
640 640 }
641 641 }
642 642
643 643 void MethodLiveness::BasicBlock::compute_gen_kill_single(ciBytecodeStream *instruction) {
644 644 int localNum;
645 645
646 646 // We prohibit _gen and _kill from having locals in common. If we
647 647 // know that one is definitely going to be applied before the other,
648 648 // we could save some computation time by relaxing this prohibition.
649 649
650 650 switch (instruction->cur_bc()) {
651 651 case Bytecodes::_nop:
652 652 case Bytecodes::_goto:
653 653 case Bytecodes::_goto_w:
654 654 case Bytecodes::_aconst_null:
655 655 case Bytecodes::_new:
656 656 case Bytecodes::_iconst_m1:
657 657 case Bytecodes::_iconst_0:
658 658 case Bytecodes::_iconst_1:
659 659 case Bytecodes::_iconst_2:
660 660 case Bytecodes::_iconst_3:
661 661 case Bytecodes::_iconst_4:
662 662 case Bytecodes::_iconst_5:
663 663 case Bytecodes::_fconst_0:
664 664 case Bytecodes::_fconst_1:
665 665 case Bytecodes::_fconst_2:
666 666 case Bytecodes::_bipush:
667 667 case Bytecodes::_sipush:
668 668 case Bytecodes::_lconst_0:
669 669 case Bytecodes::_lconst_1:
670 670 case Bytecodes::_dconst_0:
671 671 case Bytecodes::_dconst_1:
672 672 case Bytecodes::_ldc2_w:
673 673 case Bytecodes::_ldc:
674 674 case Bytecodes::_ldc_w:
675 675 case Bytecodes::_iaload:
676 676 case Bytecodes::_faload:
677 677 case Bytecodes::_baload:
678 678 case Bytecodes::_caload:
679 679 case Bytecodes::_saload:
680 680 case Bytecodes::_laload:
681 681 case Bytecodes::_daload:
682 682 case Bytecodes::_aaload:
683 683 case Bytecodes::_iastore:
684 684 case Bytecodes::_fastore:
685 685 case Bytecodes::_bastore:
686 686 case Bytecodes::_castore:
687 687 case Bytecodes::_sastore:
688 688 case Bytecodes::_lastore:
689 689 case Bytecodes::_dastore:
690 690 case Bytecodes::_aastore:
691 691 case Bytecodes::_pop:
692 692 case Bytecodes::_pop2:
693 693 case Bytecodes::_dup:
694 694 case Bytecodes::_dup_x1:
695 695 case Bytecodes::_dup_x2:
696 696 case Bytecodes::_dup2:
697 697 case Bytecodes::_dup2_x1:
698 698 case Bytecodes::_dup2_x2:
699 699 case Bytecodes::_swap:
700 700 case Bytecodes::_iadd:
701 701 case Bytecodes::_fadd:
702 702 case Bytecodes::_isub:
703 703 case Bytecodes::_fsub:
704 704 case Bytecodes::_imul:
705 705 case Bytecodes::_fmul:
706 706 case Bytecodes::_idiv:
707 707 case Bytecodes::_fdiv:
708 708 case Bytecodes::_irem:
709 709 case Bytecodes::_frem:
710 710 case Bytecodes::_ishl:
711 711 case Bytecodes::_ishr:
712 712 case Bytecodes::_iushr:
713 713 case Bytecodes::_iand:
714 714 case Bytecodes::_ior:
715 715 case Bytecodes::_ixor:
716 716 case Bytecodes::_l2f:
717 717 case Bytecodes::_l2i:
718 718 case Bytecodes::_d2f:
719 719 case Bytecodes::_d2i:
720 720 case Bytecodes::_fcmpl:
721 721 case Bytecodes::_fcmpg:
722 722 case Bytecodes::_ladd:
723 723 case Bytecodes::_dadd:
724 724 case Bytecodes::_lsub:
725 725 case Bytecodes::_dsub:
726 726 case Bytecodes::_lmul:
727 727 case Bytecodes::_dmul:
728 728 case Bytecodes::_ldiv:
729 729 case Bytecodes::_ddiv:
730 730 case Bytecodes::_lrem:
731 731 case Bytecodes::_drem:
732 732 case Bytecodes::_land:
733 733 case Bytecodes::_lor:
734 734 case Bytecodes::_lxor:
735 735 case Bytecodes::_ineg:
736 736 case Bytecodes::_fneg:
737 737 case Bytecodes::_i2f:
738 738 case Bytecodes::_f2i:
739 739 case Bytecodes::_i2c:
740 740 case Bytecodes::_i2s:
741 741 case Bytecodes::_i2b:
742 742 case Bytecodes::_lneg:
743 743 case Bytecodes::_dneg:
744 744 case Bytecodes::_l2d:
745 745 case Bytecodes::_d2l:
746 746 case Bytecodes::_lshl:
747 747 case Bytecodes::_lshr:
748 748 case Bytecodes::_lushr:
749 749 case Bytecodes::_i2l:
750 750 case Bytecodes::_i2d:
751 751 case Bytecodes::_f2l:
752 752 case Bytecodes::_f2d:
753 753 case Bytecodes::_lcmp:
754 754 case Bytecodes::_dcmpl:
755 755 case Bytecodes::_dcmpg:
756 756 case Bytecodes::_ifeq:
757 757 case Bytecodes::_ifne:
758 758 case Bytecodes::_iflt:
759 759 case Bytecodes::_ifge:
760 760 case Bytecodes::_ifgt:
761 761 case Bytecodes::_ifle:
762 762 case Bytecodes::_tableswitch:
763 763 case Bytecodes::_ireturn:
764 764 case Bytecodes::_freturn:
765 765 case Bytecodes::_if_icmpeq:
766 766 case Bytecodes::_if_icmpne:
767 767 case Bytecodes::_if_icmplt:
768 768 case Bytecodes::_if_icmpge:
769 769 case Bytecodes::_if_icmpgt:
770 770 case Bytecodes::_if_icmple:
771 771 case Bytecodes::_lreturn:
772 772 case Bytecodes::_dreturn:
773 773 case Bytecodes::_if_acmpeq:
774 774 case Bytecodes::_if_acmpne:
↓ open down ↓ |
774 lines elided |
↑ open up ↑ |
775 775 case Bytecodes::_jsr:
776 776 case Bytecodes::_jsr_w:
777 777 case Bytecodes::_getstatic:
778 778 case Bytecodes::_putstatic:
779 779 case Bytecodes::_getfield:
780 780 case Bytecodes::_putfield:
781 781 case Bytecodes::_invokevirtual:
782 782 case Bytecodes::_invokespecial:
783 783 case Bytecodes::_invokestatic:
784 784 case Bytecodes::_invokeinterface:
785 + case Bytecodes::_invokedynamic:
785 786 case Bytecodes::_newarray:
786 787 case Bytecodes::_anewarray:
787 788 case Bytecodes::_checkcast:
788 789 case Bytecodes::_arraylength:
789 790 case Bytecodes::_instanceof:
790 791 case Bytecodes::_athrow:
791 792 case Bytecodes::_areturn:
792 793 case Bytecodes::_monitorenter:
793 794 case Bytecodes::_monitorexit:
794 795 case Bytecodes::_ifnull:
795 796 case Bytecodes::_ifnonnull:
796 797 case Bytecodes::_multianewarray:
797 798 case Bytecodes::_lookupswitch:
798 799 // These bytecodes have no effect on the method's locals.
799 800 break;
800 801
801 802 case Bytecodes::_return:
802 803 if (instruction->method()->intrinsic_id() == vmIntrinsics::_Object_init) {
803 804 // return from Object.init implicitly registers a finalizer
804 805 // for the receiver if needed, so keep it alive.
805 806 load_one(0);
806 807 }
807 808 break;
808 809
809 810
810 811 case Bytecodes::_lload:
811 812 case Bytecodes::_dload:
812 813 load_two(instruction->get_index());
813 814 break;
814 815
815 816 case Bytecodes::_lload_0:
816 817 case Bytecodes::_dload_0:
817 818 load_two(0);
818 819 break;
819 820
820 821 case Bytecodes::_lload_1:
821 822 case Bytecodes::_dload_1:
822 823 load_two(1);
823 824 break;
824 825
825 826 case Bytecodes::_lload_2:
826 827 case Bytecodes::_dload_2:
827 828 load_two(2);
828 829 break;
829 830
830 831 case Bytecodes::_lload_3:
831 832 case Bytecodes::_dload_3:
832 833 load_two(3);
833 834 break;
834 835
835 836 case Bytecodes::_iload:
836 837 case Bytecodes::_iinc:
837 838 case Bytecodes::_fload:
838 839 case Bytecodes::_aload:
839 840 case Bytecodes::_ret:
840 841 load_one(instruction->get_index());
841 842 break;
842 843
843 844 case Bytecodes::_iload_0:
844 845 case Bytecodes::_fload_0:
845 846 case Bytecodes::_aload_0:
846 847 load_one(0);
847 848 break;
848 849
849 850 case Bytecodes::_iload_1:
850 851 case Bytecodes::_fload_1:
851 852 case Bytecodes::_aload_1:
852 853 load_one(1);
853 854 break;
854 855
855 856 case Bytecodes::_iload_2:
856 857 case Bytecodes::_fload_2:
857 858 case Bytecodes::_aload_2:
858 859 load_one(2);
859 860 break;
860 861
861 862 case Bytecodes::_iload_3:
862 863 case Bytecodes::_fload_3:
863 864 case Bytecodes::_aload_3:
864 865 load_one(3);
865 866 break;
866 867
867 868 case Bytecodes::_lstore:
868 869 case Bytecodes::_dstore:
869 870 store_two(localNum = instruction->get_index());
870 871 break;
871 872
872 873 case Bytecodes::_lstore_0:
873 874 case Bytecodes::_dstore_0:
874 875 store_two(0);
875 876 break;
876 877
877 878 case Bytecodes::_lstore_1:
878 879 case Bytecodes::_dstore_1:
879 880 store_two(1);
880 881 break;
881 882
882 883 case Bytecodes::_lstore_2:
883 884 case Bytecodes::_dstore_2:
884 885 store_two(2);
885 886 break;
886 887
887 888 case Bytecodes::_lstore_3:
888 889 case Bytecodes::_dstore_3:
889 890 store_two(3);
890 891 break;
891 892
892 893 case Bytecodes::_istore:
893 894 case Bytecodes::_fstore:
894 895 case Bytecodes::_astore:
895 896 store_one(instruction->get_index());
896 897 break;
897 898
898 899 case Bytecodes::_istore_0:
899 900 case Bytecodes::_fstore_0:
900 901 case Bytecodes::_astore_0:
901 902 store_one(0);
902 903 break;
903 904
904 905 case Bytecodes::_istore_1:
905 906 case Bytecodes::_fstore_1:
906 907 case Bytecodes::_astore_1:
907 908 store_one(1);
908 909 break;
909 910
910 911 case Bytecodes::_istore_2:
911 912 case Bytecodes::_fstore_2:
912 913 case Bytecodes::_astore_2:
913 914 store_one(2);
914 915 break;
915 916
916 917 case Bytecodes::_istore_3:
917 918 case Bytecodes::_fstore_3:
918 919 case Bytecodes::_astore_3:
919 920 store_one(3);
920 921 break;
921 922
922 923 case Bytecodes::_wide:
923 924 fatal("Iterator should skip this bytecode");
924 925 break;
925 926
926 927 default:
927 928 tty->print("unexpected opcode: %d\n", instruction->cur_bc());
928 929 ShouldNotReachHere();
929 930 break;
930 931 }
931 932 }
932 933
933 934 void MethodLiveness::BasicBlock::load_two(int local) {
934 935 load_one(local);
935 936 load_one(local+1);
936 937 }
937 938
938 939 void MethodLiveness::BasicBlock::load_one(int local) {
939 940 if (!_kill.at(local)) {
940 941 _gen.at_put(local, true);
941 942 }
942 943 }
943 944
944 945 void MethodLiveness::BasicBlock::store_two(int local) {
945 946 store_one(local);
946 947 store_one(local+1);
947 948 }
948 949
949 950 void MethodLiveness::BasicBlock::store_one(int local) {
950 951 if (!_gen.at(local)) {
951 952 _kill.at_put(local, true);
952 953 }
953 954 }
954 955
955 956 void MethodLiveness::BasicBlock::propagate(MethodLiveness *ml) {
956 957 // These set operations could be combined for efficiency if the
957 958 // performance of this analysis becomes an issue.
958 959 _entry.set_union(_normal_exit);
959 960 _entry.set_difference(_kill);
960 961 _entry.set_union(_gen);
961 962
962 963 // Note that we merge information from our exceptional successors
963 964 // just once, rather than at individual bytecodes.
964 965 _entry.set_union(_exception_exit);
965 966
966 967 if (TraceLivenessGen) {
967 968 tty->print_cr(" ** Visiting block at %d **", start_bci());
968 969 print_on(tty);
969 970 }
970 971
971 972 int i;
972 973 for (i=_normal_predecessors->length()-1; i>=0; i--) {
973 974 BasicBlock *block = _normal_predecessors->at(i);
974 975 if (block->merge_normal(_entry)) {
975 976 ml->work_list_add(block);
976 977 }
977 978 }
978 979 for (i=_exception_predecessors->length()-1; i>=0; i--) {
979 980 BasicBlock *block = _exception_predecessors->at(i);
980 981 if (block->merge_exception(_entry)) {
981 982 ml->work_list_add(block);
982 983 }
983 984 }
984 985 }
985 986
986 987 bool MethodLiveness::BasicBlock::merge_normal(BitMap other) {
987 988 return _normal_exit.set_union_with_result(other);
988 989 }
989 990
990 991 bool MethodLiveness::BasicBlock::merge_exception(BitMap other) {
991 992 return _exception_exit.set_union_with_result(other);
992 993 }
993 994
994 995 MethodLivenessResult MethodLiveness::BasicBlock::get_liveness_at(ciMethod* method, int bci) {
995 996 MethodLivenessResult answer(NEW_RESOURCE_ARRAY(uintptr_t, _analyzer->bit_map_size_words()),
996 997 _analyzer->bit_map_size_bits());
997 998 answer.set_is_valid();
998 999
999 1000 #ifndef ASSERT
1000 1001 if (bci == start_bci()) {
1001 1002 answer.set_from(_entry);
1002 1003 return answer;
1003 1004 }
1004 1005 #endif
1005 1006
1006 1007 #ifdef ASSERT
1007 1008 ResourceMark rm;
1008 1009 BitMap g(_gen.size()); g.set_from(_gen);
1009 1010 BitMap k(_kill.size()); k.set_from(_kill);
1010 1011 #endif
1011 1012 if (_last_bci != bci || trueInDebug) {
1012 1013 ciBytecodeStream bytes(method);
1013 1014 bytes.reset_to_bci(bci);
1014 1015 bytes.set_max_bci(limit_bci());
1015 1016 compute_gen_kill_range(&bytes);
1016 1017 assert(_last_bci != bci ||
1017 1018 (g.is_same(_gen) && k.is_same(_kill)), "cached computation is incorrect");
1018 1019 _last_bci = bci;
1019 1020 }
1020 1021
1021 1022 answer.clear();
1022 1023 answer.set_union(_normal_exit);
1023 1024 answer.set_difference(_kill);
1024 1025 answer.set_union(_gen);
1025 1026 answer.set_union(_exception_exit);
1026 1027
1027 1028 #ifdef ASSERT
1028 1029 if (bci == start_bci()) {
1029 1030 assert(answer.is_same(_entry), "optimized answer must be accurate");
1030 1031 }
1031 1032 #endif
1032 1033
1033 1034 return answer;
1034 1035 }
1035 1036
1036 1037 #ifndef PRODUCT
1037 1038
1038 1039 void MethodLiveness::BasicBlock::print_on(outputStream *os) const {
1039 1040 os->print_cr("===================================================================");
1040 1041 os->print_cr(" Block start: %4d, limit: %4d", _start_bci, _limit_bci);
1041 1042 os->print (" Normal predecessors (%2d) @", _normal_predecessors->length());
1042 1043 int i;
1043 1044 for (i=0; i < _normal_predecessors->length(); i++) {
1044 1045 os->print(" %4d", _normal_predecessors->at(i)->start_bci());
1045 1046 }
1046 1047 os->cr();
1047 1048 os->print (" Exceptional predecessors (%2d) @", _exception_predecessors->length());
1048 1049 for (i=0; i < _exception_predecessors->length(); i++) {
1049 1050 os->print(" %4d", _exception_predecessors->at(i)->start_bci());
1050 1051 }
1051 1052 os->cr();
1052 1053 os->print (" Normal Exit : ");
1053 1054 _normal_exit.print_on(os);
1054 1055 os->print (" Gen : ");
1055 1056 _gen.print_on(os);
1056 1057 os->print (" Kill : ");
1057 1058 _kill.print_on(os);
1058 1059 os->print (" Exception Exit: ");
1059 1060 _exception_exit.print_on(os);
1060 1061 os->print (" Entry : ");
1061 1062 _entry.print_on(os);
1062 1063 }
1063 1064
1064 1065 #endif // PRODUCT
↓ open down ↓ |
270 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX