1 /* 2 * Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #include "precompiled.hpp" 26 #include "ci/ciConstant.hpp" 27 #include "ci/ciField.hpp" 28 #include "ci/ciMethod.hpp" 29 #include "ci/ciMethodData.hpp" 30 #include "ci/ciObjArrayKlass.hpp" 31 #include "ci/ciStreams.hpp" 32 #include "ci/ciTypeArrayKlass.hpp" 33 #include "ci/ciTypeFlow.hpp" 34 #include "ci/ciValueKlass.hpp" 35 #include "compiler/compileLog.hpp" 36 #include "interpreter/bytecode.hpp" 37 #include "interpreter/bytecodes.hpp" 38 #include "memory/allocation.inline.hpp" 39 #include "memory/resourceArea.hpp" 40 #include "oops/oop.inline.hpp" 41 #include "opto/compile.hpp" 42 #include "opto/node.hpp" 43 #include "runtime/deoptimization.hpp" 44 #include "utilities/growableArray.hpp" 45 46 // ciTypeFlow::JsrSet 47 // 48 // A JsrSet represents some set of JsrRecords. This class 49 // is used to record a set of all jsr routines which we permit 50 // execution to return (ret) from. 51 // 52 // During abstract interpretation, JsrSets are used to determine 53 // whether two paths which reach a given block are unique, and 54 // should be cloned apart, or are compatible, and should merge 55 // together. 56 57 // ------------------------------------------------------------------ 58 // ciTypeFlow::JsrSet::JsrSet 59 ciTypeFlow::JsrSet::JsrSet(Arena* arena, int default_len) { 60 if (arena != NULL) { 61 // Allocate growable array in Arena. 62 _set = new (arena) GrowableArray<JsrRecord*>(arena, default_len, 0, NULL); 63 } else { 64 // Allocate growable array in current ResourceArea. 65 _set = new GrowableArray<JsrRecord*>(4, 0, NULL, false); 66 } 67 } 68 69 // ------------------------------------------------------------------ 70 // ciTypeFlow::JsrSet::copy_into 71 void ciTypeFlow::JsrSet::copy_into(JsrSet* jsrs) { 72 int len = size(); 73 jsrs->_set->clear(); 74 for (int i = 0; i < len; i++) { 75 jsrs->_set->append(_set->at(i)); 76 } 77 } 78 79 // ------------------------------------------------------------------ 80 // ciTypeFlow::JsrSet::is_compatible_with 81 // 82 // !!!! MISGIVINGS ABOUT THIS... disregard 83 // 84 // Is this JsrSet compatible with some other JsrSet? 85 // 86 // In set-theoretic terms, a JsrSet can be viewed as a partial function 87 // from entry addresses to return addresses. Two JsrSets A and B are 88 // compatible iff 89 // 90 // For any x, 91 // A(x) defined and B(x) defined implies A(x) == B(x) 92 // 93 // Less formally, two JsrSets are compatible when they have identical 94 // return addresses for any entry addresses they share in common. 95 bool ciTypeFlow::JsrSet::is_compatible_with(JsrSet* other) { 96 // Walk through both sets in parallel. If the same entry address 97 // appears in both sets, then the return address must match for 98 // the sets to be compatible. 99 int size1 = size(); 100 int size2 = other->size(); 101 102 // Special case. If nothing is on the jsr stack, then there can 103 // be no ret. 104 if (size2 == 0) { 105 return true; 106 } else if (size1 != size2) { 107 return false; 108 } else { 109 for (int i = 0; i < size1; i++) { 110 JsrRecord* record1 = record_at(i); 111 JsrRecord* record2 = other->record_at(i); 112 if (record1->entry_address() != record2->entry_address() || 113 record1->return_address() != record2->return_address()) { 114 return false; 115 } 116 } 117 return true; 118 } 119 120 #if 0 121 int pos1 = 0; 122 int pos2 = 0; 123 int size1 = size(); 124 int size2 = other->size(); 125 while (pos1 < size1 && pos2 < size2) { 126 JsrRecord* record1 = record_at(pos1); 127 JsrRecord* record2 = other->record_at(pos2); 128 int entry1 = record1->entry_address(); 129 int entry2 = record2->entry_address(); 130 if (entry1 < entry2) { 131 pos1++; 132 } else if (entry1 > entry2) { 133 pos2++; 134 } else { 135 if (record1->return_address() == record2->return_address()) { 136 pos1++; 137 pos2++; 138 } else { 139 // These two JsrSets are incompatible. 140 return false; 141 } 142 } 143 } 144 // The two JsrSets agree. 145 return true; 146 #endif 147 } 148 149 // ------------------------------------------------------------------ 150 // ciTypeFlow::JsrSet::insert_jsr_record 151 // 152 // Insert the given JsrRecord into the JsrSet, maintaining the order 153 // of the set and replacing any element with the same entry address. 154 void ciTypeFlow::JsrSet::insert_jsr_record(JsrRecord* record) { 155 int len = size(); 156 int entry = record->entry_address(); 157 int pos = 0; 158 for ( ; pos < len; pos++) { 159 JsrRecord* current = record_at(pos); 160 if (entry == current->entry_address()) { 161 // Stomp over this entry. 162 _set->at_put(pos, record); 163 assert(size() == len, "must be same size"); 164 return; 165 } else if (entry < current->entry_address()) { 166 break; 167 } 168 } 169 170 // Insert the record into the list. 171 JsrRecord* swap = record; 172 JsrRecord* temp = NULL; 173 for ( ; pos < len; pos++) { 174 temp = _set->at(pos); 175 _set->at_put(pos, swap); 176 swap = temp; 177 } 178 _set->append(swap); 179 assert(size() == len+1, "must be larger"); 180 } 181 182 // ------------------------------------------------------------------ 183 // ciTypeFlow::JsrSet::remove_jsr_record 184 // 185 // Remove the JsrRecord with the given return address from the JsrSet. 186 void ciTypeFlow::JsrSet::remove_jsr_record(int return_address) { 187 int len = size(); 188 for (int i = 0; i < len; i++) { 189 if (record_at(i)->return_address() == return_address) { 190 // We have found the proper entry. Remove it from the 191 // JsrSet and exit. 192 for (int j = i+1; j < len ; j++) { 193 _set->at_put(j-1, _set->at(j)); 194 } 195 _set->trunc_to(len-1); 196 assert(size() == len-1, "must be smaller"); 197 return; 198 } 199 } 200 assert(false, "verify: returning from invalid subroutine"); 201 } 202 203 // ------------------------------------------------------------------ 204 // ciTypeFlow::JsrSet::apply_control 205 // 206 // Apply the effect of a control-flow bytecode on the JsrSet. The 207 // only bytecodes that modify the JsrSet are jsr and ret. 208 void ciTypeFlow::JsrSet::apply_control(ciTypeFlow* analyzer, 209 ciBytecodeStream* str, 210 ciTypeFlow::StateVector* state) { 211 Bytecodes::Code code = str->cur_bc(); 212 if (code == Bytecodes::_jsr) { 213 JsrRecord* record = 214 analyzer->make_jsr_record(str->get_dest(), str->next_bci()); 215 insert_jsr_record(record); 216 } else if (code == Bytecodes::_jsr_w) { 217 JsrRecord* record = 218 analyzer->make_jsr_record(str->get_far_dest(), str->next_bci()); 219 insert_jsr_record(record); 220 } else if (code == Bytecodes::_ret) { 221 Cell local = state->local(str->get_index()); 222 ciType* return_address = state->type_at(local); 223 assert(return_address->is_return_address(), "verify: wrong type"); 224 if (size() == 0) { 225 // Ret-state underflow: Hit a ret w/o any previous jsrs. Bail out. 226 // This can happen when a loop is inside a finally clause (4614060). 227 analyzer->record_failure("OSR in finally clause"); 228 return; 229 } 230 remove_jsr_record(return_address->as_return_address()->bci()); 231 } 232 } 233 234 #ifndef PRODUCT 235 // ------------------------------------------------------------------ 236 // ciTypeFlow::JsrSet::print_on 237 void ciTypeFlow::JsrSet::print_on(outputStream* st) const { 238 st->print("{ "); 239 int num_elements = size(); 240 if (num_elements > 0) { 241 int i = 0; 242 for( ; i < num_elements - 1; i++) { 243 _set->at(i)->print_on(st); 244 st->print(", "); 245 } 246 _set->at(i)->print_on(st); 247 st->print(" "); 248 } 249 st->print("}"); 250 } 251 #endif 252 253 // ciTypeFlow::StateVector 254 // 255 // A StateVector summarizes the type information at some point in 256 // the program. 257 258 // ------------------------------------------------------------------ 259 // ciTypeFlow::StateVector::type_meet 260 // 261 // Meet two types. 262 // 263 // The semi-lattice of types use by this analysis are modeled on those 264 // of the verifier. The lattice is as follows: 265 // 266 // top_type() >= all non-extremal types >= bottom_type 267 // and 268 // Every primitive type is comparable only with itself. The meet of 269 // reference types is determined by their kind: instance class, 270 // interface, or array class. The meet of two types of the same 271 // kind is their least common ancestor. The meet of two types of 272 // different kinds is always java.lang.Object. 273 ciType* ciTypeFlow::StateVector::type_meet_internal(ciType* t1, ciType* t2, ciTypeFlow* analyzer) { 274 assert(t1 != t2, "checked in caller"); 275 if (t1->equals(top_type())) { 276 return t2; 277 } else if (t2->equals(top_type())) { 278 return t1; 279 } else if (t1->is_primitive_type() || t2->is_primitive_type()) { 280 // Special case null_type. null_type meet any reference type T 281 // is T. null_type meet null_type is null_type. 282 if (t1->equals(null_type())) { 283 if (!t2->is_primitive_type() || t2->equals(null_type())) { 284 return t2; 285 } 286 } else if (t2->equals(null_type())) { 287 if (!t1->is_primitive_type()) { 288 return t1; 289 } 290 } 291 292 // At least one of the two types is a non-top primitive type. 293 // The other type is not equal to it. Fall to bottom. 294 return bottom_type(); 295 } else { 296 // Both types are non-top non-primitive types. That is, 297 // both types are either instanceKlasses or arrayKlasses. 298 ciKlass* object_klass = analyzer->env()->Object_klass(); 299 ciKlass* k1 = t1->as_klass(); 300 ciKlass* k2 = t2->as_klass(); 301 if (k1->equals(object_klass) || k2->equals(object_klass)) { 302 return object_klass; 303 } else if (!k1->is_loaded() || !k2->is_loaded()) { 304 // Unloaded classes fall to java.lang.Object at a merge. 305 return object_klass; 306 } else if (k1->is_interface() != k2->is_interface()) { 307 // When an interface meets a non-interface, we get Object; 308 // This is what the verifier does. 309 return object_klass; 310 } else if (k1->is_array_klass() || k2->is_array_klass()) { 311 // When an array meets a non-array, we get Object. 312 // When objArray meets typeArray, we also get Object. 313 // And when typeArray meets different typeArray, we again get Object. 314 // But when objArray meets objArray, we look carefully at element types. 315 if (k1->is_obj_array_klass() && k2->is_obj_array_klass()) { 316 // Meet the element types, then construct the corresponding array type. 317 ciKlass* elem1 = k1->as_obj_array_klass()->element_klass(); 318 ciKlass* elem2 = k2->as_obj_array_klass()->element_klass(); 319 ciKlass* elem = type_meet_internal(elem1, elem2, analyzer)->as_klass(); 320 // Do an easy shortcut if one type is a super of the other. 321 if (elem == elem1) { 322 assert(k1 == ciObjArrayKlass::make(elem), "shortcut is OK"); 323 return k1; 324 } else if (elem == elem2) { 325 assert(k2 == ciObjArrayKlass::make(elem), "shortcut is OK"); 326 return k2; 327 } else { 328 return ciObjArrayKlass::make(elem); 329 } 330 } else { 331 return object_klass; 332 } 333 } else { 334 // Must be two plain old instance klasses. 335 assert(k1->is_instance_klass(), "previous cases handle non-instances"); 336 assert(k2->is_instance_klass(), "previous cases handle non-instances"); 337 return k1->least_common_ancestor(k2); 338 } 339 } 340 } 341 342 343 // ------------------------------------------------------------------ 344 // ciTypeFlow::StateVector::StateVector 345 // 346 // Build a new state vector 347 ciTypeFlow::StateVector::StateVector(ciTypeFlow* analyzer) { 348 _outer = analyzer; 349 _stack_size = -1; 350 _monitor_count = -1; 351 // Allocate the _types array 352 int max_cells = analyzer->max_cells(); 353 _types = (ciType**)analyzer->arena()->Amalloc(sizeof(ciType*) * max_cells); 354 for (int i=0; i<max_cells; i++) { 355 _types[i] = top_type(); 356 } 357 _trap_bci = -1; 358 _trap_index = 0; 359 _def_locals.clear(); 360 } 361 362 363 // ------------------------------------------------------------------ 364 // ciTypeFlow::get_start_state 365 // 366 // Set this vector to the method entry state. 367 const ciTypeFlow::StateVector* ciTypeFlow::get_start_state() { 368 StateVector* state = new StateVector(this); 369 if (is_osr_flow()) { 370 ciTypeFlow* non_osr_flow = method()->get_flow_analysis(); 371 if (non_osr_flow->failing()) { 372 record_failure(non_osr_flow->failure_reason()); 373 return NULL; 374 } 375 JsrSet* jsrs = new JsrSet(NULL, 16); 376 Block* non_osr_block = non_osr_flow->existing_block_at(start_bci(), jsrs); 377 if (non_osr_block == NULL) { 378 record_failure("cannot reach OSR point"); 379 return NULL; 380 } 381 // load up the non-OSR state at this point 382 non_osr_block->copy_state_into(state); 383 int non_osr_start = non_osr_block->start(); 384 if (non_osr_start != start_bci()) { 385 // must flow forward from it 386 if (CITraceTypeFlow) { 387 tty->print_cr(">> Interpreting pre-OSR block %d:", non_osr_start); 388 } 389 Block* block = block_at(non_osr_start, jsrs); 390 assert(block->limit() == start_bci(), "must flow forward to start"); 391 flow_block(block, state, jsrs); 392 } 393 return state; 394 // Note: The code below would be an incorrect for an OSR flow, 395 // even if it were possible for an OSR entry point to be at bci zero. 396 } 397 // "Push" the method signature into the first few locals. 398 state->set_stack_size(-max_locals()); 399 if (!method()->is_static()) { 400 state->push(method()->holder()); 401 assert(state->tos() == state->local(0), ""); 402 } 403 for (ciSignatureStream str(method()->signature()); 404 !str.at_return_type(); 405 str.next()) { 406 state->push_translate(str.type()); 407 } 408 // Set the rest of the locals to bottom. 409 Cell cell = state->next_cell(state->tos()); 410 state->set_stack_size(0); 411 int limit = state->limit_cell(); 412 for (; cell < limit; cell = state->next_cell(cell)) { 413 state->set_type_at(cell, state->bottom_type()); 414 } 415 // Lock an object, if necessary. 416 state->set_monitor_count(method()->is_synchronized() ? 1 : 0); 417 return state; 418 } 419 420 // ------------------------------------------------------------------ 421 // ciTypeFlow::StateVector::copy_into 422 // 423 // Copy our value into some other StateVector 424 void ciTypeFlow::StateVector::copy_into(ciTypeFlow::StateVector* copy) 425 const { 426 copy->set_stack_size(stack_size()); 427 copy->set_monitor_count(monitor_count()); 428 Cell limit = limit_cell(); 429 for (Cell c = start_cell(); c < limit; c = next_cell(c)) { 430 copy->set_type_at(c, type_at(c)); 431 } 432 } 433 434 // ------------------------------------------------------------------ 435 // ciTypeFlow::StateVector::meet 436 // 437 // Meets this StateVector with another, destructively modifying this 438 // one. Returns true if any modification takes place. 439 bool ciTypeFlow::StateVector::meet(const ciTypeFlow::StateVector* incoming) { 440 if (monitor_count() == -1) { 441 set_monitor_count(incoming->monitor_count()); 442 } 443 assert(monitor_count() == incoming->monitor_count(), "monitors must match"); 444 445 if (stack_size() == -1) { 446 set_stack_size(incoming->stack_size()); 447 Cell limit = limit_cell(); 448 #ifdef ASSERT 449 { for (Cell c = start_cell(); c < limit; c = next_cell(c)) { 450 assert(type_at(c) == top_type(), ""); 451 } } 452 #endif 453 // Make a simple copy of the incoming state. 454 for (Cell c = start_cell(); c < limit; c = next_cell(c)) { 455 set_type_at(c, incoming->type_at(c)); 456 } 457 return true; // it is always different the first time 458 } 459 #ifdef ASSERT 460 if (stack_size() != incoming->stack_size()) { 461 _outer->method()->print_codes(); 462 tty->print_cr("!!!! Stack size conflict"); 463 tty->print_cr("Current state:"); 464 print_on(tty); 465 tty->print_cr("Incoming state:"); 466 ((StateVector*)incoming)->print_on(tty); 467 } 468 #endif 469 assert(stack_size() == incoming->stack_size(), "sanity"); 470 471 bool different = false; 472 Cell limit = limit_cell(); 473 for (Cell c = start_cell(); c < limit; c = next_cell(c)) { 474 ciType* t1 = type_at(c); 475 ciType* t2 = incoming->type_at(c); 476 if (!t1->equals(t2)) { 477 ciType* new_type = type_meet(t1, t2); 478 if (!t1->equals(new_type)) { 479 set_type_at(c, new_type); 480 different = true; 481 } 482 } 483 } 484 return different; 485 } 486 487 // ------------------------------------------------------------------ 488 // ciTypeFlow::StateVector::meet_exception 489 // 490 // Meets this StateVector with another, destructively modifying this 491 // one. The incoming state is coming via an exception. Returns true 492 // if any modification takes place. 493 bool ciTypeFlow::StateVector::meet_exception(ciInstanceKlass* exc, 494 const ciTypeFlow::StateVector* incoming) { 495 if (monitor_count() == -1) { 496 set_monitor_count(incoming->monitor_count()); 497 } 498 assert(monitor_count() == incoming->monitor_count(), "monitors must match"); 499 500 if (stack_size() == -1) { 501 set_stack_size(1); 502 } 503 504 assert(stack_size() == 1, "must have one-element stack"); 505 506 bool different = false; 507 508 // Meet locals from incoming array. 509 Cell limit = local(_outer->max_locals()-1); 510 for (Cell c = start_cell(); c <= limit; c = next_cell(c)) { 511 ciType* t1 = type_at(c); 512 ciType* t2 = incoming->type_at(c); 513 if (!t1->equals(t2)) { 514 ciType* new_type = type_meet(t1, t2); 515 if (!t1->equals(new_type)) { 516 set_type_at(c, new_type); 517 different = true; 518 } 519 } 520 } 521 522 // Handle stack separately. When an exception occurs, the 523 // only stack entry is the exception instance. 524 ciType* tos_type = type_at_tos(); 525 if (!tos_type->equals(exc)) { 526 ciType* new_type = type_meet(tos_type, exc); 527 if (!tos_type->equals(new_type)) { 528 set_type_at_tos(new_type); 529 different = true; 530 } 531 } 532 533 return different; 534 } 535 536 // ------------------------------------------------------------------ 537 // ciTypeFlow::StateVector::push_translate 538 void ciTypeFlow::StateVector::push_translate(ciType* type) { 539 BasicType basic_type = type->basic_type(); 540 if (basic_type == T_BOOLEAN || basic_type == T_CHAR || 541 basic_type == T_BYTE || basic_type == T_SHORT) { 542 push_int(); 543 } else { 544 push(type); 545 if (type->is_two_word()) { 546 push(half_type(type)); 547 } 548 } 549 } 550 551 // ------------------------------------------------------------------ 552 // ciTypeFlow::StateVector::do_aload 553 void ciTypeFlow::StateVector::do_aload(ciBytecodeStream* str) { 554 pop_int(); 555 ciArrayKlass* array_klass = pop_objOrValueArray(); 556 if (array_klass == NULL) { 557 // Did aload on a null reference; push a null and ignore the exception. 558 // This instruction will never continue normally. All we have to do 559 // is report a value that will meet correctly with any downstream 560 // reference types on paths that will truly be executed. This null type 561 // meets with any reference type to yield that same reference type. 562 // (The compiler will generate an unconditional exception here.) 563 push(null_type()); 564 return; 565 } 566 if (!array_klass->is_loaded()) { 567 // Only fails for some -Xcomp runs 568 trap(str, array_klass, 569 Deoptimization::make_trap_request 570 (Deoptimization::Reason_unloaded, 571 Deoptimization::Action_reinterpret)); 572 return; 573 } 574 ciKlass* element_klass = array_klass->element_klass(); 575 if (!element_klass->is_loaded() && element_klass->is_instance_klass()) { 576 Untested("unloaded array element class in ciTypeFlow"); 577 trap(str, element_klass, 578 Deoptimization::make_trap_request 579 (Deoptimization::Reason_unloaded, 580 Deoptimization::Action_reinterpret)); 581 } else { 582 push_object(element_klass); 583 } 584 } 585 586 587 // ------------------------------------------------------------------ 588 // ciTypeFlow::StateVector::do_checkcast 589 void ciTypeFlow::StateVector::do_checkcast(ciBytecodeStream* str) { 590 bool will_link; 591 ciKlass* klass = str->get_klass(will_link); 592 if (!will_link) { 593 // VM's interpreter will not load 'klass' if object is NULL. 594 // Type flow after this block may still be needed in two situations: 595 // 1) C2 uses do_null_assert() and continues compilation for later blocks 596 // 2) C2 does an OSR compile in a later block (see bug 4778368). 597 pop_object(); 598 do_null_assert(klass); 599 } else { 600 pop_object(); 601 push_object(klass); 602 } 603 } 604 605 // ------------------------------------------------------------------ 606 // ciTypeFlow::StateVector::do_getfield 607 void ciTypeFlow::StateVector::do_getfield(ciBytecodeStream* str) { 608 // could add assert here for type of object. 609 pop_object(); 610 do_getstatic(str); 611 } 612 613 // ------------------------------------------------------------------ 614 // ciTypeFlow::StateVector::do_getstatic 615 void ciTypeFlow::StateVector::do_getstatic(ciBytecodeStream* str) { 616 bool will_link; 617 ciField* field = str->get_field(will_link); 618 if (!will_link) { 619 trap(str, field->holder(), str->get_field_holder_index()); 620 } else { 621 ciType* field_type = field->type(); 622 if (!field_type->is_loaded()) { 623 // Normally, we need the field's type to be loaded if we are to 624 // do anything interesting with its value. 625 // We used to do this: trap(str, str->get_field_signature_index()); 626 // 627 // There is one good reason not to trap here. Execution can 628 // get past this "getfield" or "getstatic" if the value of 629 // the field is null. As long as the value is null, the class 630 // does not need to be loaded! The compiler must assume that 631 // the value of the unloaded class reference is null; if the code 632 // ever sees a non-null value, loading has occurred. 633 // 634 // This actually happens often enough to be annoying. If the 635 // compiler throws an uncommon trap at this bytecode, you can 636 // get an endless loop of recompilations, when all the code 637 // needs to do is load a series of null values. Also, a trap 638 // here can make an OSR entry point unreachable, triggering the 639 // assert on non_osr_block in ciTypeFlow::get_start_state. 640 // (See bug 4379915.) 641 do_null_assert(field_type->as_klass()); 642 } else { 643 push_translate(field_type); 644 } 645 } 646 } 647 648 // ------------------------------------------------------------------ 649 // ciTypeFlow::StateVector::do_invoke 650 void ciTypeFlow::StateVector::do_invoke(ciBytecodeStream* str, 651 bool has_receiver) { 652 bool will_link; 653 ciSignature* declared_signature = NULL; 654 ciMethod* callee = str->get_method(will_link, &declared_signature); 655 assert(declared_signature != NULL, "cannot be null"); 656 if (!will_link) { 657 // We weren't able to find the method. 658 if (str->cur_bc() == Bytecodes::_invokedynamic) { 659 trap(str, NULL, 660 Deoptimization::make_trap_request 661 (Deoptimization::Reason_uninitialized, 662 Deoptimization::Action_reinterpret)); 663 } else { 664 ciKlass* unloaded_holder = callee->holder(); 665 trap(str, unloaded_holder, str->get_method_holder_index()); 666 } 667 } else { 668 // We are using the declared signature here because it might be 669 // different from the callee signature (Cf. invokedynamic and 670 // invokehandle). 671 ciSignatureStream sigstr(declared_signature); 672 const int arg_size = declared_signature->size(); 673 const int stack_base = stack_size() - arg_size; 674 int i = 0; 675 for( ; !sigstr.at_return_type(); sigstr.next()) { 676 ciType* type = sigstr.type(); 677 ciType* stack_type = type_at(stack(stack_base + i++)); 678 // Do I want to check this type? 679 // assert(stack_type->is_subtype_of(type), "bad type for field value"); 680 if (type->is_two_word()) { 681 ciType* stack_type2 = type_at(stack(stack_base + i++)); 682 assert(stack_type2->equals(half_type(type)), "must be 2nd half"); 683 } 684 } 685 assert(arg_size == i, "must match"); 686 for (int j = 0; j < arg_size; j++) { 687 pop(); 688 } 689 if (has_receiver) { 690 // Check this? 691 pop_object(); 692 } 693 assert(!sigstr.is_done(), "must have return type"); 694 ciType* return_type = sigstr.type(); 695 if (!return_type->is_void()) { 696 if (!return_type->is_loaded()) { 697 // As in do_getstatic(), generally speaking, we need the return type to 698 // be loaded if we are to do anything interesting with its value. 699 // We used to do this: trap(str, str->get_method_signature_index()); 700 // 701 // We do not trap here since execution can get past this invoke if 702 // the return value is null. As long as the value is null, the class 703 // does not need to be loaded! The compiler must assume that 704 // the value of the unloaded class reference is null; if the code 705 // ever sees a non-null value, loading has occurred. 706 // 707 // See do_getstatic() for similar explanation, as well as bug 4684993. 708 do_null_assert(return_type->as_klass()); 709 } else { 710 push_translate(return_type); 711 } 712 } 713 } 714 } 715 716 // ------------------------------------------------------------------ 717 // ciTypeFlow::StateVector::do_jsr 718 void ciTypeFlow::StateVector::do_jsr(ciBytecodeStream* str) { 719 push(ciReturnAddress::make(str->next_bci())); 720 } 721 722 // ------------------------------------------------------------------ 723 // ciTypeFlow::StateVector::do_ldc 724 void ciTypeFlow::StateVector::do_ldc(ciBytecodeStream* str) { 725 ciConstant con = str->get_constant(); 726 BasicType basic_type = con.basic_type(); 727 if (basic_type == T_ILLEGAL) { 728 // OutOfMemoryError in the CI while loading constant 729 push_null(); 730 outer()->record_failure("ldc did not link"); 731 return; 732 } 733 if (basic_type == T_OBJECT || basic_type == T_ARRAY) { 734 ciObject* obj = con.as_object(); 735 if (obj->is_null_object()) { 736 push_null(); 737 } else { 738 assert(obj->is_instance() || obj->is_array(), "must be java_mirror of klass"); 739 push_object(obj->klass()); 740 } 741 } else { 742 push_translate(ciType::make(basic_type)); 743 } 744 } 745 746 // ------------------------------------------------------------------ 747 // ciTypeFlow::StateVector::do_multianewarray 748 void ciTypeFlow::StateVector::do_multianewarray(ciBytecodeStream* str) { 749 int dimensions = str->get_dimensions(); 750 bool will_link; 751 ciArrayKlass* array_klass = str->get_klass(will_link)->as_array_klass(); 752 if (!will_link) { 753 trap(str, array_klass, str->get_klass_index()); 754 } else { 755 for (int i = 0; i < dimensions; i++) { 756 pop_int(); 757 } 758 push_object(array_klass); 759 } 760 } 761 762 // ------------------------------------------------------------------ 763 // ciTypeFlow::StateVector::do_new 764 void ciTypeFlow::StateVector::do_new(ciBytecodeStream* str) { 765 bool will_link; 766 ciKlass* klass = str->get_klass(will_link); 767 if (!will_link || str->is_unresolved_klass()) { 768 trap(str, klass, str->get_klass_index()); 769 } else { 770 push_object(klass); 771 } 772 } 773 774 // ------------------------------------------------------------------ 775 // ciTypeFlow::StateVector::do_vdefault 776 void ciTypeFlow::StateVector::do_vdefault(ciBytecodeStream* str) { 777 bool will_link; 778 ciKlass* klass = str->get_klass(will_link); 779 assert(klass->is_valuetype(), "should be value type"); 780 if (!will_link) { 781 trap(str, klass, str->get_klass_index()); 782 } else { 783 push_object(klass); 784 } 785 } 786 787 // ------------------------------------------------------------------ 788 // ciTypeFlow::StateVector::do_vwithfield 789 void ciTypeFlow::StateVector::do_vwithfield(ciBytecodeStream* str) { 790 bool will_link; 791 ciField* field = str->get_field(will_link); 792 ciKlass* klass = field->holder(); 793 assert(klass->is_valuetype(), "should be value type"); 794 if (!will_link) { 795 trap(str, klass, str->get_field_holder_index()); 796 } else { 797 ciType* type = pop_value(); 798 ciType* field_type = field->type(); 799 assert(field_type->is_loaded(), "field type must be loaded"); 800 if (field_type->is_two_word()) { 801 ciType* type2 = pop_value(); 802 assert(type2->is_two_word(), "must be 2nd half"); 803 assert(type == half_type(type2), "must be 2nd half"); 804 } 805 pop_object(); 806 push_object(klass); 807 } 808 } 809 810 // ------------------------------------------------------------------ 811 // ciTypeFlow::StateVector::do_newarray 812 void ciTypeFlow::StateVector::do_newarray(ciBytecodeStream* str) { 813 pop_int(); 814 ciKlass* klass = ciTypeArrayKlass::make((BasicType)str->get_index()); 815 push_object(klass); 816 } 817 818 // ------------------------------------------------------------------ 819 // ciTypeFlow::StateVector::do_putfield 820 void ciTypeFlow::StateVector::do_putfield(ciBytecodeStream* str) { 821 do_putstatic(str); 822 if (_trap_bci != -1) return; // unloaded field holder, etc. 823 // could add assert here for type of object. 824 pop_object(); 825 } 826 827 // ------------------------------------------------------------------ 828 // ciTypeFlow::StateVector::do_putstatic 829 void ciTypeFlow::StateVector::do_putstatic(ciBytecodeStream* str) { 830 bool will_link; 831 ciField* field = str->get_field(will_link); 832 if (!will_link) { 833 trap(str, field->holder(), str->get_field_holder_index()); 834 } else { 835 ciType* field_type = field->type(); 836 ciType* type = pop_value(); 837 // Do I want to check this type? 838 // assert(type->is_subtype_of(field_type), "bad type for field value"); 839 if (field_type->is_two_word()) { 840 ciType* type2 = pop_value(); 841 assert(type2->is_two_word(), "must be 2nd half"); 842 assert(type == half_type(type2), "must be 2nd half"); 843 } 844 } 845 } 846 847 // ------------------------------------------------------------------ 848 // ciTypeFlow::StateVector::do_ret 849 void ciTypeFlow::StateVector::do_ret(ciBytecodeStream* str) { 850 Cell index = local(str->get_index()); 851 852 ciType* address = type_at(index); 853 assert(address->is_return_address(), "bad return address"); 854 set_type_at(index, bottom_type()); 855 } 856 857 void ciTypeFlow::StateVector::do_vunbox(ciBytecodeStream* str) { 858 bool will_link; 859 ciKlass* klass = str->get_klass(will_link); 860 // TODO: Handle case when class is not loaded. 861 guarantee(will_link, "Class to which the value-capable class will unbox to must be loaded for JIT compilation"); 862 assert(klass->is_instance_klass(), "must be an instance class"); 863 pop_object(); 864 push_object(klass->as_instance_klass()); 865 } 866 867 void ciTypeFlow::StateVector::do_vbox(ciBytecodeStream* str) { 868 bool will_link; 869 ciKlass* klass = str->get_klass(will_link); 870 // TODO: Handle case when class is not loaded. 871 guarantee(will_link, "Class to which value type will box to must be loaded for JIT compilation"); 872 assert(klass->is_instance_klass(), "must be an instance class"); 873 pop_object(); 874 push_object(klass->as_instance_klass()); 875 } 876 877 // ------------------------------------------------------------------ 878 // ciTypeFlow::StateVector::trap 879 // 880 // Stop interpretation of this path with a trap. 881 void ciTypeFlow::StateVector::trap(ciBytecodeStream* str, ciKlass* klass, int index) { 882 _trap_bci = str->cur_bci(); 883 _trap_index = index; 884 885 // Log information about this trap: 886 CompileLog* log = outer()->env()->log(); 887 if (log != NULL) { 888 int mid = log->identify(outer()->method()); 889 int kid = (klass == NULL)? -1: log->identify(klass); 890 log->begin_elem("uncommon_trap method='%d' bci='%d'", mid, str->cur_bci()); 891 char buf[100]; 892 log->print(" %s", Deoptimization::format_trap_request(buf, sizeof(buf), 893 index)); 894 if (kid >= 0) 895 log->print(" klass='%d'", kid); 896 log->end_elem(); 897 } 898 } 899 900 // ------------------------------------------------------------------ 901 // ciTypeFlow::StateVector::do_null_assert 902 // Corresponds to graphKit::do_null_assert. 903 void ciTypeFlow::StateVector::do_null_assert(ciKlass* unloaded_klass) { 904 if (unloaded_klass->is_loaded()) { 905 // We failed to link, but we can still compute with this class, 906 // since it is loaded somewhere. The compiler will uncommon_trap 907 // if the object is not null, but the typeflow pass can not assume 908 // that the object will be null, otherwise it may incorrectly tell 909 // the parser that an object is known to be null. 4761344, 4807707 910 push_object(unloaded_klass); 911 } else { 912 // The class is not loaded anywhere. It is safe to model the 913 // null in the typestates, because we can compile in a null check 914 // which will deoptimize us if someone manages to load the 915 // class later. 916 push_null(); 917 } 918 } 919 920 921 // ------------------------------------------------------------------ 922 // ciTypeFlow::StateVector::apply_one_bytecode 923 // 924 // Apply the effect of one bytecode to this StateVector 925 bool ciTypeFlow::StateVector::apply_one_bytecode(ciBytecodeStream* str) { 926 _trap_bci = -1; 927 _trap_index = 0; 928 929 if (CITraceTypeFlow) { 930 tty->print_cr(">> Interpreting bytecode %d:%s", str->cur_bci(), 931 Bytecodes::name(str->cur_bc())); 932 } 933 934 switch(str->cur_bc()) { 935 case Bytecodes::_aaload: do_aload(str); break; 936 937 case Bytecodes::_aastore: 938 { 939 pop_object(); 940 pop_int(); 941 pop_objOrValueArray(); 942 break; 943 } 944 case Bytecodes::_aconst_null: 945 { 946 push_null(); 947 break; 948 } 949 case Bytecodes::_aload: load_local_object(str->get_index()); break; 950 case Bytecodes::_aload_0: load_local_object(0); break; 951 case Bytecodes::_aload_1: load_local_object(1); break; 952 case Bytecodes::_aload_2: load_local_object(2); break; 953 case Bytecodes::_aload_3: load_local_object(3); break; 954 955 case Bytecodes::_anewarray: 956 { 957 pop_int(); 958 bool will_link; 959 ciKlass* element_klass = str->get_klass(will_link); 960 if (!will_link) { 961 trap(str, element_klass, str->get_klass_index()); 962 } else { 963 push_object(ciArrayKlass::make(element_klass)); 964 } 965 break; 966 } 967 case Bytecodes::_areturn: 968 case Bytecodes::_ifnonnull: 969 case Bytecodes::_ifnull: 970 { 971 pop_object(); 972 break; 973 } 974 case Bytecodes::_monitorenter: 975 { 976 pop_object(); 977 set_monitor_count(monitor_count() + 1); 978 break; 979 } 980 case Bytecodes::_monitorexit: 981 { 982 pop_object(); 983 assert(monitor_count() > 0, "must be a monitor to exit from"); 984 set_monitor_count(monitor_count() - 1); 985 break; 986 } 987 case Bytecodes::_arraylength: 988 { 989 pop_array(); 990 push_int(); 991 break; 992 } 993 case Bytecodes::_astore: store_local_object(str->get_index()); break; 994 case Bytecodes::_astore_0: store_local_object(0); break; 995 case Bytecodes::_astore_1: store_local_object(1); break; 996 case Bytecodes::_astore_2: store_local_object(2); break; 997 case Bytecodes::_astore_3: store_local_object(3); break; 998 999 case Bytecodes::_athrow: 1000 { 1001 NEEDS_CLEANUP; 1002 pop_object(); 1003 break; 1004 } 1005 case Bytecodes::_baload: 1006 case Bytecodes::_caload: 1007 case Bytecodes::_iaload: 1008 case Bytecodes::_saload: 1009 { 1010 pop_int(); 1011 ciTypeArrayKlass* array_klass = pop_typeArray(); 1012 // Put assert here for right type? 1013 push_int(); 1014 break; 1015 } 1016 case Bytecodes::_bastore: 1017 case Bytecodes::_castore: 1018 case Bytecodes::_iastore: 1019 case Bytecodes::_sastore: 1020 { 1021 pop_int(); 1022 pop_int(); 1023 pop_typeArray(); 1024 // assert here? 1025 break; 1026 } 1027 case Bytecodes::_bipush: 1028 case Bytecodes::_iconst_m1: 1029 case Bytecodes::_iconst_0: 1030 case Bytecodes::_iconst_1: 1031 case Bytecodes::_iconst_2: 1032 case Bytecodes::_iconst_3: 1033 case Bytecodes::_iconst_4: 1034 case Bytecodes::_iconst_5: 1035 case Bytecodes::_sipush: 1036 { 1037 push_int(); 1038 break; 1039 } 1040 case Bytecodes::_checkcast: do_checkcast(str); break; 1041 1042 case Bytecodes::_d2f: 1043 { 1044 pop_double(); 1045 push_float(); 1046 break; 1047 } 1048 case Bytecodes::_d2i: 1049 { 1050 pop_double(); 1051 push_int(); 1052 break; 1053 } 1054 case Bytecodes::_d2l: 1055 { 1056 pop_double(); 1057 push_long(); 1058 break; 1059 } 1060 case Bytecodes::_dadd: 1061 case Bytecodes::_ddiv: 1062 case Bytecodes::_dmul: 1063 case Bytecodes::_drem: 1064 case Bytecodes::_dsub: 1065 { 1066 pop_double(); 1067 pop_double(); 1068 push_double(); 1069 break; 1070 } 1071 case Bytecodes::_daload: 1072 { 1073 pop_int(); 1074 ciTypeArrayKlass* array_klass = pop_typeArray(); 1075 // Put assert here for right type? 1076 push_double(); 1077 break; 1078 } 1079 case Bytecodes::_dastore: 1080 { 1081 pop_double(); 1082 pop_int(); 1083 pop_typeArray(); 1084 // assert here? 1085 break; 1086 } 1087 case Bytecodes::_dcmpg: 1088 case Bytecodes::_dcmpl: 1089 { 1090 pop_double(); 1091 pop_double(); 1092 push_int(); 1093 break; 1094 } 1095 case Bytecodes::_dconst_0: 1096 case Bytecodes::_dconst_1: 1097 { 1098 push_double(); 1099 break; 1100 } 1101 case Bytecodes::_dload: load_local_double(str->get_index()); break; 1102 case Bytecodes::_dload_0: load_local_double(0); break; 1103 case Bytecodes::_dload_1: load_local_double(1); break; 1104 case Bytecodes::_dload_2: load_local_double(2); break; 1105 case Bytecodes::_dload_3: load_local_double(3); break; 1106 1107 case Bytecodes::_dneg: 1108 { 1109 pop_double(); 1110 push_double(); 1111 break; 1112 } 1113 case Bytecodes::_dreturn: 1114 { 1115 pop_double(); 1116 break; 1117 } 1118 case Bytecodes::_dstore: store_local_double(str->get_index()); break; 1119 case Bytecodes::_dstore_0: store_local_double(0); break; 1120 case Bytecodes::_dstore_1: store_local_double(1); break; 1121 case Bytecodes::_dstore_2: store_local_double(2); break; 1122 case Bytecodes::_dstore_3: store_local_double(3); break; 1123 1124 case Bytecodes::_dup: 1125 { 1126 push(type_at_tos()); 1127 break; 1128 } 1129 case Bytecodes::_dup_x1: 1130 { 1131 ciType* value1 = pop_value(); 1132 ciType* value2 = pop_value(); 1133 push(value1); 1134 push(value2); 1135 push(value1); 1136 break; 1137 } 1138 case Bytecodes::_dup_x2: 1139 { 1140 ciType* value1 = pop_value(); 1141 ciType* value2 = pop_value(); 1142 ciType* value3 = pop_value(); 1143 push(value1); 1144 push(value3); 1145 push(value2); 1146 push(value1); 1147 break; 1148 } 1149 case Bytecodes::_dup2: 1150 { 1151 ciType* value1 = pop_value(); 1152 ciType* value2 = pop_value(); 1153 push(value2); 1154 push(value1); 1155 push(value2); 1156 push(value1); 1157 break; 1158 } 1159 case Bytecodes::_dup2_x1: 1160 { 1161 ciType* value1 = pop_value(); 1162 ciType* value2 = pop_value(); 1163 ciType* value3 = pop_value(); 1164 push(value2); 1165 push(value1); 1166 push(value3); 1167 push(value2); 1168 push(value1); 1169 break; 1170 } 1171 case Bytecodes::_dup2_x2: 1172 { 1173 ciType* value1 = pop_value(); 1174 ciType* value2 = pop_value(); 1175 ciType* value3 = pop_value(); 1176 ciType* value4 = pop_value(); 1177 push(value2); 1178 push(value1); 1179 push(value4); 1180 push(value3); 1181 push(value2); 1182 push(value1); 1183 break; 1184 } 1185 case Bytecodes::_f2d: 1186 { 1187 pop_float(); 1188 push_double(); 1189 break; 1190 } 1191 case Bytecodes::_f2i: 1192 { 1193 pop_float(); 1194 push_int(); 1195 break; 1196 } 1197 case Bytecodes::_f2l: 1198 { 1199 pop_float(); 1200 push_long(); 1201 break; 1202 } 1203 case Bytecodes::_fadd: 1204 case Bytecodes::_fdiv: 1205 case Bytecodes::_fmul: 1206 case Bytecodes::_frem: 1207 case Bytecodes::_fsub: 1208 { 1209 pop_float(); 1210 pop_float(); 1211 push_float(); 1212 break; 1213 } 1214 case Bytecodes::_faload: 1215 { 1216 pop_int(); 1217 ciTypeArrayKlass* array_klass = pop_typeArray(); 1218 // Put assert here. 1219 push_float(); 1220 break; 1221 } 1222 case Bytecodes::_fastore: 1223 { 1224 pop_float(); 1225 pop_int(); 1226 ciTypeArrayKlass* array_klass = pop_typeArray(); 1227 // Put assert here. 1228 break; 1229 } 1230 case Bytecodes::_fcmpg: 1231 case Bytecodes::_fcmpl: 1232 { 1233 pop_float(); 1234 pop_float(); 1235 push_int(); 1236 break; 1237 } 1238 case Bytecodes::_fconst_0: 1239 case Bytecodes::_fconst_1: 1240 case Bytecodes::_fconst_2: 1241 { 1242 push_float(); 1243 break; 1244 } 1245 case Bytecodes::_fload: load_local_float(str->get_index()); break; 1246 case Bytecodes::_fload_0: load_local_float(0); break; 1247 case Bytecodes::_fload_1: load_local_float(1); break; 1248 case Bytecodes::_fload_2: load_local_float(2); break; 1249 case Bytecodes::_fload_3: load_local_float(3); break; 1250 1251 case Bytecodes::_fneg: 1252 { 1253 pop_float(); 1254 push_float(); 1255 break; 1256 } 1257 case Bytecodes::_freturn: 1258 { 1259 pop_float(); 1260 break; 1261 } 1262 case Bytecodes::_fstore: store_local_float(str->get_index()); break; 1263 case Bytecodes::_fstore_0: store_local_float(0); break; 1264 case Bytecodes::_fstore_1: store_local_float(1); break; 1265 case Bytecodes::_fstore_2: store_local_float(2); break; 1266 case Bytecodes::_fstore_3: store_local_float(3); break; 1267 1268 case Bytecodes::_getfield: do_getfield(str); break; 1269 case Bytecodes::_getstatic: do_getstatic(str); break; 1270 1271 case Bytecodes::_goto: 1272 case Bytecodes::_goto_w: 1273 case Bytecodes::_nop: 1274 case Bytecodes::_return: 1275 { 1276 // do nothing. 1277 break; 1278 } 1279 case Bytecodes::_i2b: 1280 case Bytecodes::_i2c: 1281 case Bytecodes::_i2s: 1282 case Bytecodes::_ineg: 1283 { 1284 pop_int(); 1285 push_int(); 1286 break; 1287 } 1288 case Bytecodes::_i2d: 1289 { 1290 pop_int(); 1291 push_double(); 1292 break; 1293 } 1294 case Bytecodes::_i2f: 1295 { 1296 pop_int(); 1297 push_float(); 1298 break; 1299 } 1300 case Bytecodes::_i2l: 1301 { 1302 pop_int(); 1303 push_long(); 1304 break; 1305 } 1306 case Bytecodes::_iadd: 1307 case Bytecodes::_iand: 1308 case Bytecodes::_idiv: 1309 case Bytecodes::_imul: 1310 case Bytecodes::_ior: 1311 case Bytecodes::_irem: 1312 case Bytecodes::_ishl: 1313 case Bytecodes::_ishr: 1314 case Bytecodes::_isub: 1315 case Bytecodes::_iushr: 1316 case Bytecodes::_ixor: 1317 { 1318 pop_int(); 1319 pop_int(); 1320 push_int(); 1321 break; 1322 } 1323 case Bytecodes::_if_acmpeq: 1324 case Bytecodes::_if_acmpne: 1325 { 1326 pop_object(); 1327 pop_object(); 1328 break; 1329 } 1330 case Bytecodes::_if_icmpeq: 1331 case Bytecodes::_if_icmpge: 1332 case Bytecodes::_if_icmpgt: 1333 case Bytecodes::_if_icmple: 1334 case Bytecodes::_if_icmplt: 1335 case Bytecodes::_if_icmpne: 1336 { 1337 pop_int(); 1338 pop_int(); 1339 break; 1340 } 1341 case Bytecodes::_ifeq: 1342 case Bytecodes::_ifle: 1343 case Bytecodes::_iflt: 1344 case Bytecodes::_ifge: 1345 case Bytecodes::_ifgt: 1346 case Bytecodes::_ifne: 1347 case Bytecodes::_ireturn: 1348 case Bytecodes::_lookupswitch: 1349 case Bytecodes::_tableswitch: 1350 { 1351 pop_int(); 1352 break; 1353 } 1354 case Bytecodes::_iinc: 1355 { 1356 int lnum = str->get_index(); 1357 check_int(local(lnum)); 1358 store_to_local(lnum); 1359 break; 1360 } 1361 case Bytecodes::_iload: load_local_int(str->get_index()); break; 1362 case Bytecodes::_iload_0: load_local_int(0); break; 1363 case Bytecodes::_iload_1: load_local_int(1); break; 1364 case Bytecodes::_iload_2: load_local_int(2); break; 1365 case Bytecodes::_iload_3: load_local_int(3); break; 1366 1367 case Bytecodes::_instanceof: 1368 { 1369 // Check for uncommon trap: 1370 do_checkcast(str); 1371 pop_object(); 1372 push_int(); 1373 break; 1374 } 1375 case Bytecodes::_invokeinterface: do_invoke(str, true); break; 1376 case Bytecodes::_invokespecial: do_invoke(str, true); break; 1377 case Bytecodes::_invokestatic: do_invoke(str, false); break; 1378 case Bytecodes::_invokevirtual: do_invoke(str, true); break; 1379 case Bytecodes::_invokedynamic: do_invoke(str, false); break; 1380 1381 case Bytecodes::_istore: store_local_int(str->get_index()); break; 1382 case Bytecodes::_istore_0: store_local_int(0); break; 1383 case Bytecodes::_istore_1: store_local_int(1); break; 1384 case Bytecodes::_istore_2: store_local_int(2); break; 1385 case Bytecodes::_istore_3: store_local_int(3); break; 1386 1387 case Bytecodes::_jsr: 1388 case Bytecodes::_jsr_w: do_jsr(str); break; 1389 1390 case Bytecodes::_l2d: 1391 { 1392 pop_long(); 1393 push_double(); 1394 break; 1395 } 1396 case Bytecodes::_l2f: 1397 { 1398 pop_long(); 1399 push_float(); 1400 break; 1401 } 1402 case Bytecodes::_l2i: 1403 { 1404 pop_long(); 1405 push_int(); 1406 break; 1407 } 1408 case Bytecodes::_ladd: 1409 case Bytecodes::_land: 1410 case Bytecodes::_ldiv: 1411 case Bytecodes::_lmul: 1412 case Bytecodes::_lor: 1413 case Bytecodes::_lrem: 1414 case Bytecodes::_lsub: 1415 case Bytecodes::_lxor: 1416 { 1417 pop_long(); 1418 pop_long(); 1419 push_long(); 1420 break; 1421 } 1422 case Bytecodes::_laload: 1423 { 1424 pop_int(); 1425 ciTypeArrayKlass* array_klass = pop_typeArray(); 1426 // Put assert here for right type? 1427 push_long(); 1428 break; 1429 } 1430 case Bytecodes::_lastore: 1431 { 1432 pop_long(); 1433 pop_int(); 1434 pop_typeArray(); 1435 // assert here? 1436 break; 1437 } 1438 case Bytecodes::_lcmp: 1439 { 1440 pop_long(); 1441 pop_long(); 1442 push_int(); 1443 break; 1444 } 1445 case Bytecodes::_lconst_0: 1446 case Bytecodes::_lconst_1: 1447 { 1448 push_long(); 1449 break; 1450 } 1451 case Bytecodes::_ldc: 1452 case Bytecodes::_ldc_w: 1453 case Bytecodes::_ldc2_w: 1454 { 1455 do_ldc(str); 1456 break; 1457 } 1458 1459 case Bytecodes::_lload: load_local_long(str->get_index()); break; 1460 case Bytecodes::_lload_0: load_local_long(0); break; 1461 case Bytecodes::_lload_1: load_local_long(1); break; 1462 case Bytecodes::_lload_2: load_local_long(2); break; 1463 case Bytecodes::_lload_3: load_local_long(3); break; 1464 1465 case Bytecodes::_lneg: 1466 { 1467 pop_long(); 1468 push_long(); 1469 break; 1470 } 1471 case Bytecodes::_lreturn: 1472 { 1473 pop_long(); 1474 break; 1475 } 1476 case Bytecodes::_lshl: 1477 case Bytecodes::_lshr: 1478 case Bytecodes::_lushr: 1479 { 1480 pop_int(); 1481 pop_long(); 1482 push_long(); 1483 break; 1484 } 1485 case Bytecodes::_lstore: store_local_long(str->get_index()); break; 1486 case Bytecodes::_lstore_0: store_local_long(0); break; 1487 case Bytecodes::_lstore_1: store_local_long(1); break; 1488 case Bytecodes::_lstore_2: store_local_long(2); break; 1489 case Bytecodes::_lstore_3: store_local_long(3); break; 1490 1491 case Bytecodes::_multianewarray: do_multianewarray(str); break; 1492 1493 case Bytecodes::_new: do_new(str); break; 1494 1495 case Bytecodes::_defaultvalue: do_vdefault(str); break; 1496 case Bytecodes::_withfield: do_vwithfield(str); break; 1497 1498 case Bytecodes::_newarray: do_newarray(str); break; 1499 1500 case Bytecodes::_pop: 1501 { 1502 pop(); 1503 break; 1504 } 1505 case Bytecodes::_pop2: 1506 { 1507 pop(); 1508 pop(); 1509 break; 1510 } 1511 1512 case Bytecodes::_putfield: do_putfield(str); break; 1513 case Bytecodes::_putstatic: do_putstatic(str); break; 1514 1515 case Bytecodes::_ret: do_ret(str); break; 1516 1517 case Bytecodes::_swap: 1518 { 1519 ciType* value1 = pop_value(); 1520 ciType* value2 = pop_value(); 1521 push(value1); 1522 push(value2); 1523 break; 1524 } 1525 1526 case Bytecodes::_wide: 1527 default: 1528 { 1529 // The iterator should skip this. 1530 ShouldNotReachHere(); 1531 break; 1532 } 1533 } 1534 1535 if (CITraceTypeFlow) { 1536 print_on(tty); 1537 } 1538 1539 return (_trap_bci != -1); 1540 } 1541 1542 #ifndef PRODUCT 1543 // ------------------------------------------------------------------ 1544 // ciTypeFlow::StateVector::print_cell_on 1545 void ciTypeFlow::StateVector::print_cell_on(outputStream* st, Cell c) const { 1546 ciType* type = type_at(c); 1547 if (type == top_type()) { 1548 st->print("top"); 1549 } else if (type == bottom_type()) { 1550 st->print("bottom"); 1551 } else if (type == null_type()) { 1552 st->print("null"); 1553 } else if (type == long2_type()) { 1554 st->print("long2"); 1555 } else if (type == double2_type()) { 1556 st->print("double2"); 1557 } else if (is_int(type)) { 1558 st->print("int"); 1559 } else if (is_long(type)) { 1560 st->print("long"); 1561 } else if (is_float(type)) { 1562 st->print("float"); 1563 } else if (is_double(type)) { 1564 st->print("double"); 1565 } else if (type->is_return_address()) { 1566 st->print("address(%d)", type->as_return_address()->bci()); 1567 } else { 1568 if (type->is_klass()) { 1569 type->as_klass()->name()->print_symbol_on(st); 1570 } else { 1571 st->print("UNEXPECTED TYPE"); 1572 type->print(); 1573 } 1574 } 1575 } 1576 1577 // ------------------------------------------------------------------ 1578 // ciTypeFlow::StateVector::print_on 1579 void ciTypeFlow::StateVector::print_on(outputStream* st) const { 1580 int num_locals = _outer->max_locals(); 1581 int num_stack = stack_size(); 1582 int num_monitors = monitor_count(); 1583 st->print_cr(" State : locals %d, stack %d, monitors %d", num_locals, num_stack, num_monitors); 1584 if (num_stack >= 0) { 1585 int i; 1586 for (i = 0; i < num_locals; i++) { 1587 st->print(" local %2d : ", i); 1588 print_cell_on(st, local(i)); 1589 st->cr(); 1590 } 1591 for (i = 0; i < num_stack; i++) { 1592 st->print(" stack %2d : ", i); 1593 print_cell_on(st, stack(i)); 1594 st->cr(); 1595 } 1596 } 1597 } 1598 #endif 1599 1600 1601 // ------------------------------------------------------------------ 1602 // ciTypeFlow::SuccIter::next 1603 // 1604 void ciTypeFlow::SuccIter::next() { 1605 int succ_ct = _pred->successors()->length(); 1606 int next = _index + 1; 1607 if (next < succ_ct) { 1608 _index = next; 1609 _succ = _pred->successors()->at(next); 1610 return; 1611 } 1612 for (int i = next - succ_ct; i < _pred->exceptions()->length(); i++) { 1613 // Do not compile any code for unloaded exception types. 1614 // Following compiler passes are responsible for doing this also. 1615 ciInstanceKlass* exception_klass = _pred->exc_klasses()->at(i); 1616 if (exception_klass->is_loaded()) { 1617 _index = next; 1618 _succ = _pred->exceptions()->at(i); 1619 return; 1620 } 1621 next++; 1622 } 1623 _index = -1; 1624 _succ = NULL; 1625 } 1626 1627 // ------------------------------------------------------------------ 1628 // ciTypeFlow::SuccIter::set_succ 1629 // 1630 void ciTypeFlow::SuccIter::set_succ(Block* succ) { 1631 int succ_ct = _pred->successors()->length(); 1632 if (_index < succ_ct) { 1633 _pred->successors()->at_put(_index, succ); 1634 } else { 1635 int idx = _index - succ_ct; 1636 _pred->exceptions()->at_put(idx, succ); 1637 } 1638 } 1639 1640 // ciTypeFlow::Block 1641 // 1642 // A basic block. 1643 1644 // ------------------------------------------------------------------ 1645 // ciTypeFlow::Block::Block 1646 ciTypeFlow::Block::Block(ciTypeFlow* outer, 1647 ciBlock *ciblk, 1648 ciTypeFlow::JsrSet* jsrs) { 1649 _ciblock = ciblk; 1650 _exceptions = NULL; 1651 _exc_klasses = NULL; 1652 _successors = NULL; 1653 _predecessors = new (outer->arena()) GrowableArray<Block*>(outer->arena(), 1, 0, NULL); 1654 _state = new (outer->arena()) StateVector(outer); 1655 JsrSet* new_jsrs = 1656 new (outer->arena()) JsrSet(outer->arena(), jsrs->size()); 1657 jsrs->copy_into(new_jsrs); 1658 _jsrs = new_jsrs; 1659 _next = NULL; 1660 _on_work_list = false; 1661 _backedge_copy = false; 1662 _has_monitorenter = false; 1663 _trap_bci = -1; 1664 _trap_index = 0; 1665 df_init(); 1666 1667 if (CITraceTypeFlow) { 1668 tty->print_cr(">> Created new block"); 1669 print_on(tty); 1670 } 1671 1672 assert(this->outer() == outer, "outer link set up"); 1673 assert(!outer->have_block_count(), "must not have mapped blocks yet"); 1674 } 1675 1676 // ------------------------------------------------------------------ 1677 // ciTypeFlow::Block::df_init 1678 void ciTypeFlow::Block::df_init() { 1679 _pre_order = -1; assert(!has_pre_order(), ""); 1680 _post_order = -1; assert(!has_post_order(), ""); 1681 _loop = NULL; 1682 _irreducible_entry = false; 1683 _rpo_next = NULL; 1684 } 1685 1686 // ------------------------------------------------------------------ 1687 // ciTypeFlow::Block::successors 1688 // 1689 // Get the successors for this Block. 1690 GrowableArray<ciTypeFlow::Block*>* 1691 ciTypeFlow::Block::successors(ciBytecodeStream* str, 1692 ciTypeFlow::StateVector* state, 1693 ciTypeFlow::JsrSet* jsrs) { 1694 if (_successors == NULL) { 1695 if (CITraceTypeFlow) { 1696 tty->print(">> Computing successors for block "); 1697 print_value_on(tty); 1698 tty->cr(); 1699 } 1700 1701 ciTypeFlow* analyzer = outer(); 1702 Arena* arena = analyzer->arena(); 1703 Block* block = NULL; 1704 bool has_successor = !has_trap() && 1705 (control() != ciBlock::fall_through_bci || limit() < analyzer->code_size()); 1706 if (!has_successor) { 1707 _successors = 1708 new (arena) GrowableArray<Block*>(arena, 1, 0, NULL); 1709 // No successors 1710 } else if (control() == ciBlock::fall_through_bci) { 1711 assert(str->cur_bci() == limit(), "bad block end"); 1712 // This block simply falls through to the next. 1713 _successors = 1714 new (arena) GrowableArray<Block*>(arena, 1, 0, NULL); 1715 1716 Block* block = analyzer->block_at(limit(), _jsrs); 1717 assert(_successors->length() == FALL_THROUGH, ""); 1718 _successors->append(block); 1719 } else { 1720 int current_bci = str->cur_bci(); 1721 int next_bci = str->next_bci(); 1722 int branch_bci = -1; 1723 Block* target = NULL; 1724 assert(str->next_bci() == limit(), "bad block end"); 1725 // This block is not a simple fall-though. Interpret 1726 // the current bytecode to find our successors. 1727 switch (str->cur_bc()) { 1728 case Bytecodes::_ifeq: case Bytecodes::_ifne: 1729 case Bytecodes::_iflt: case Bytecodes::_ifge: 1730 case Bytecodes::_ifgt: case Bytecodes::_ifle: 1731 case Bytecodes::_if_icmpeq: case Bytecodes::_if_icmpne: 1732 case Bytecodes::_if_icmplt: case Bytecodes::_if_icmpge: 1733 case Bytecodes::_if_icmpgt: case Bytecodes::_if_icmple: 1734 case Bytecodes::_if_acmpeq: case Bytecodes::_if_acmpne: 1735 case Bytecodes::_ifnull: case Bytecodes::_ifnonnull: 1736 // Our successors are the branch target and the next bci. 1737 branch_bci = str->get_dest(); 1738 _successors = 1739 new (arena) GrowableArray<Block*>(arena, 2, 0, NULL); 1740 assert(_successors->length() == IF_NOT_TAKEN, ""); 1741 _successors->append(analyzer->block_at(next_bci, jsrs)); 1742 assert(_successors->length() == IF_TAKEN, ""); 1743 _successors->append(analyzer->block_at(branch_bci, jsrs)); 1744 break; 1745 1746 case Bytecodes::_goto: 1747 branch_bci = str->get_dest(); 1748 _successors = 1749 new (arena) GrowableArray<Block*>(arena, 1, 0, NULL); 1750 assert(_successors->length() == GOTO_TARGET, ""); 1751 _successors->append(analyzer->block_at(branch_bci, jsrs)); 1752 break; 1753 1754 case Bytecodes::_jsr: 1755 branch_bci = str->get_dest(); 1756 _successors = 1757 new (arena) GrowableArray<Block*>(arena, 1, 0, NULL); 1758 assert(_successors->length() == GOTO_TARGET, ""); 1759 _successors->append(analyzer->block_at(branch_bci, jsrs)); 1760 break; 1761 1762 case Bytecodes::_goto_w: 1763 case Bytecodes::_jsr_w: 1764 _successors = 1765 new (arena) GrowableArray<Block*>(arena, 1, 0, NULL); 1766 assert(_successors->length() == GOTO_TARGET, ""); 1767 _successors->append(analyzer->block_at(str->get_far_dest(), jsrs)); 1768 break; 1769 1770 case Bytecodes::_tableswitch: { 1771 Bytecode_tableswitch tableswitch(str); 1772 1773 int len = tableswitch.length(); 1774 _successors = 1775 new (arena) GrowableArray<Block*>(arena, len+1, 0, NULL); 1776 int bci = current_bci + tableswitch.default_offset(); 1777 Block* block = analyzer->block_at(bci, jsrs); 1778 assert(_successors->length() == SWITCH_DEFAULT, ""); 1779 _successors->append(block); 1780 while (--len >= 0) { 1781 int bci = current_bci + tableswitch.dest_offset_at(len); 1782 block = analyzer->block_at(bci, jsrs); 1783 assert(_successors->length() >= SWITCH_CASES, ""); 1784 _successors->append_if_missing(block); 1785 } 1786 break; 1787 } 1788 1789 case Bytecodes::_lookupswitch: { 1790 Bytecode_lookupswitch lookupswitch(str); 1791 1792 int npairs = lookupswitch.number_of_pairs(); 1793 _successors = 1794 new (arena) GrowableArray<Block*>(arena, npairs+1, 0, NULL); 1795 int bci = current_bci + lookupswitch.default_offset(); 1796 Block* block = analyzer->block_at(bci, jsrs); 1797 assert(_successors->length() == SWITCH_DEFAULT, ""); 1798 _successors->append(block); 1799 while(--npairs >= 0) { 1800 LookupswitchPair pair = lookupswitch.pair_at(npairs); 1801 int bci = current_bci + pair.offset(); 1802 Block* block = analyzer->block_at(bci, jsrs); 1803 assert(_successors->length() >= SWITCH_CASES, ""); 1804 _successors->append_if_missing(block); 1805 } 1806 break; 1807 } 1808 1809 case Bytecodes::_athrow: 1810 case Bytecodes::_ireturn: 1811 case Bytecodes::_lreturn: 1812 case Bytecodes::_freturn: 1813 case Bytecodes::_dreturn: 1814 case Bytecodes::_areturn: 1815 case Bytecodes::_return: 1816 _successors = 1817 new (arena) GrowableArray<Block*>(arena, 1, 0, NULL); 1818 // No successors 1819 break; 1820 1821 case Bytecodes::_ret: { 1822 _successors = 1823 new (arena) GrowableArray<Block*>(arena, 1, 0, NULL); 1824 1825 Cell local = state->local(str->get_index()); 1826 ciType* return_address = state->type_at(local); 1827 assert(return_address->is_return_address(), "verify: wrong type"); 1828 int bci = return_address->as_return_address()->bci(); 1829 assert(_successors->length() == GOTO_TARGET, ""); 1830 _successors->append(analyzer->block_at(bci, jsrs)); 1831 break; 1832 } 1833 1834 case Bytecodes::_wide: 1835 default: 1836 ShouldNotReachHere(); 1837 break; 1838 } 1839 } 1840 1841 // Set predecessor information 1842 for (int i = 0; i < _successors->length(); i++) { 1843 Block* block = _successors->at(i); 1844 block->predecessors()->append(this); 1845 } 1846 } 1847 return _successors; 1848 } 1849 1850 // ------------------------------------------------------------------ 1851 // ciTypeFlow::Block:compute_exceptions 1852 // 1853 // Compute the exceptional successors and types for this Block. 1854 void ciTypeFlow::Block::compute_exceptions() { 1855 assert(_exceptions == NULL && _exc_klasses == NULL, "repeat"); 1856 1857 if (CITraceTypeFlow) { 1858 tty->print(">> Computing exceptions for block "); 1859 print_value_on(tty); 1860 tty->cr(); 1861 } 1862 1863 ciTypeFlow* analyzer = outer(); 1864 Arena* arena = analyzer->arena(); 1865 1866 // Any bci in the block will do. 1867 ciExceptionHandlerStream str(analyzer->method(), start()); 1868 1869 // Allocate our growable arrays. 1870 int exc_count = str.count(); 1871 _exceptions = new (arena) GrowableArray<Block*>(arena, exc_count, 0, NULL); 1872 _exc_klasses = new (arena) GrowableArray<ciInstanceKlass*>(arena, exc_count, 1873 0, NULL); 1874 1875 for ( ; !str.is_done(); str.next()) { 1876 ciExceptionHandler* handler = str.handler(); 1877 int bci = handler->handler_bci(); 1878 ciInstanceKlass* klass = NULL; 1879 if (bci == -1) { 1880 // There is no catch all. It is possible to exit the method. 1881 break; 1882 } 1883 if (handler->is_catch_all()) { 1884 klass = analyzer->env()->Throwable_klass(); 1885 } else { 1886 klass = handler->catch_klass(); 1887 } 1888 Block* block = analyzer->block_at(bci, _jsrs); 1889 _exceptions->append(block); 1890 block->predecessors()->append(this); 1891 _exc_klasses->append(klass); 1892 } 1893 } 1894 1895 // ------------------------------------------------------------------ 1896 // ciTypeFlow::Block::set_backedge_copy 1897 // Use this only to make a pre-existing public block into a backedge copy. 1898 void ciTypeFlow::Block::set_backedge_copy(bool z) { 1899 assert(z || (z == is_backedge_copy()), "cannot make a backedge copy public"); 1900 _backedge_copy = z; 1901 } 1902 1903 // ------------------------------------------------------------------ 1904 // ciTypeFlow::Block::is_clonable_exit 1905 // 1906 // At most 2 normal successors, one of which continues looping, 1907 // and all exceptional successors must exit. 1908 bool ciTypeFlow::Block::is_clonable_exit(ciTypeFlow::Loop* lp) { 1909 int normal_cnt = 0; 1910 int in_loop_cnt = 0; 1911 for (SuccIter iter(this); !iter.done(); iter.next()) { 1912 Block* succ = iter.succ(); 1913 if (iter.is_normal_ctrl()) { 1914 if (++normal_cnt > 2) return false; 1915 if (lp->contains(succ->loop())) { 1916 if (++in_loop_cnt > 1) return false; 1917 } 1918 } else { 1919 if (lp->contains(succ->loop())) return false; 1920 } 1921 } 1922 return in_loop_cnt == 1; 1923 } 1924 1925 // ------------------------------------------------------------------ 1926 // ciTypeFlow::Block::looping_succ 1927 // 1928 ciTypeFlow::Block* ciTypeFlow::Block::looping_succ(ciTypeFlow::Loop* lp) { 1929 assert(successors()->length() <= 2, "at most 2 normal successors"); 1930 for (SuccIter iter(this); !iter.done(); iter.next()) { 1931 Block* succ = iter.succ(); 1932 if (lp->contains(succ->loop())) { 1933 return succ; 1934 } 1935 } 1936 return NULL; 1937 } 1938 1939 #ifndef PRODUCT 1940 // ------------------------------------------------------------------ 1941 // ciTypeFlow::Block::print_value_on 1942 void ciTypeFlow::Block::print_value_on(outputStream* st) const { 1943 if (has_pre_order()) st->print("#%-2d ", pre_order()); 1944 if (has_rpo()) st->print("rpo#%-2d ", rpo()); 1945 st->print("[%d - %d)", start(), limit()); 1946 if (is_loop_head()) st->print(" lphd"); 1947 if (is_irreducible_entry()) st->print(" irred"); 1948 if (_jsrs->size() > 0) { st->print("/"); _jsrs->print_on(st); } 1949 if (is_backedge_copy()) st->print("/backedge_copy"); 1950 } 1951 1952 // ------------------------------------------------------------------ 1953 // ciTypeFlow::Block::print_on 1954 void ciTypeFlow::Block::print_on(outputStream* st) const { 1955 if ((Verbose || WizardMode) && (limit() >= 0)) { 1956 // Don't print 'dummy' blocks (i.e. blocks with limit() '-1') 1957 outer()->method()->print_codes_on(start(), limit(), st); 1958 } 1959 st->print_cr(" ==================================================== "); 1960 st->print (" "); 1961 print_value_on(st); 1962 st->print(" Stored locals: "); def_locals()->print_on(st, outer()->method()->max_locals()); tty->cr(); 1963 if (loop() && loop()->parent() != NULL) { 1964 st->print(" loops:"); 1965 Loop* lp = loop(); 1966 do { 1967 st->print(" %d<-%d", lp->head()->pre_order(),lp->tail()->pre_order()); 1968 if (lp->is_irreducible()) st->print("(ir)"); 1969 lp = lp->parent(); 1970 } while (lp->parent() != NULL); 1971 } 1972 st->cr(); 1973 _state->print_on(st); 1974 if (_successors == NULL) { 1975 st->print_cr(" No successor information"); 1976 } else { 1977 int num_successors = _successors->length(); 1978 st->print_cr(" Successors : %d", num_successors); 1979 for (int i = 0; i < num_successors; i++) { 1980 Block* successor = _successors->at(i); 1981 st->print(" "); 1982 successor->print_value_on(st); 1983 st->cr(); 1984 } 1985 } 1986 if (_predecessors == NULL) { 1987 st->print_cr(" No predecessor information"); 1988 } else { 1989 int num_predecessors = _predecessors->length(); 1990 st->print_cr(" Predecessors : %d", num_predecessors); 1991 for (int i = 0; i < num_predecessors; i++) { 1992 Block* predecessor = _predecessors->at(i); 1993 st->print(" "); 1994 predecessor->print_value_on(st); 1995 st->cr(); 1996 } 1997 } 1998 if (_exceptions == NULL) { 1999 st->print_cr(" No exception information"); 2000 } else { 2001 int num_exceptions = _exceptions->length(); 2002 st->print_cr(" Exceptions : %d", num_exceptions); 2003 for (int i = 0; i < num_exceptions; i++) { 2004 Block* exc_succ = _exceptions->at(i); 2005 ciInstanceKlass* exc_klass = _exc_klasses->at(i); 2006 st->print(" "); 2007 exc_succ->print_value_on(st); 2008 st->print(" -- "); 2009 exc_klass->name()->print_symbol_on(st); 2010 st->cr(); 2011 } 2012 } 2013 if (has_trap()) { 2014 st->print_cr(" Traps on %d with trap index %d", trap_bci(), trap_index()); 2015 } 2016 st->print_cr(" ==================================================== "); 2017 } 2018 #endif 2019 2020 #ifndef PRODUCT 2021 // ------------------------------------------------------------------ 2022 // ciTypeFlow::LocalSet::print_on 2023 void ciTypeFlow::LocalSet::print_on(outputStream* st, int limit) const { 2024 st->print("{"); 2025 for (int i = 0; i < max; i++) { 2026 if (test(i)) st->print(" %d", i); 2027 } 2028 if (limit > max) { 2029 st->print(" %d..%d ", max, limit); 2030 } 2031 st->print(" }"); 2032 } 2033 #endif 2034 2035 // ciTypeFlow 2036 // 2037 // This is a pass over the bytecodes which computes the following: 2038 // basic block structure 2039 // interpreter type-states (a la the verifier) 2040 2041 // ------------------------------------------------------------------ 2042 // ciTypeFlow::ciTypeFlow 2043 ciTypeFlow::ciTypeFlow(ciEnv* env, ciMethod* method, int osr_bci) { 2044 _env = env; 2045 _method = method; 2046 _methodBlocks = method->get_method_blocks(); 2047 _max_locals = method->max_locals(); 2048 _max_stack = method->max_stack(); 2049 _code_size = method->code_size(); 2050 _has_irreducible_entry = false; 2051 _osr_bci = osr_bci; 2052 _failure_reason = NULL; 2053 assert(0 <= start_bci() && start_bci() < code_size() , "correct osr_bci argument: 0 <= %d < %d", start_bci(), code_size()); 2054 _work_list = NULL; 2055 2056 _ciblock_count = _methodBlocks->num_blocks(); 2057 _idx_to_blocklist = NEW_ARENA_ARRAY(arena(), GrowableArray<Block*>*, _ciblock_count); 2058 for (int i = 0; i < _ciblock_count; i++) { 2059 _idx_to_blocklist[i] = NULL; 2060 } 2061 _block_map = NULL; // until all blocks are seen 2062 _jsr_count = 0; 2063 _jsr_records = NULL; 2064 } 2065 2066 // ------------------------------------------------------------------ 2067 // ciTypeFlow::work_list_next 2068 // 2069 // Get the next basic block from our work list. 2070 ciTypeFlow::Block* ciTypeFlow::work_list_next() { 2071 assert(!work_list_empty(), "work list must not be empty"); 2072 Block* next_block = _work_list; 2073 _work_list = next_block->next(); 2074 next_block->set_next(NULL); 2075 next_block->set_on_work_list(false); 2076 return next_block; 2077 } 2078 2079 // ------------------------------------------------------------------ 2080 // ciTypeFlow::add_to_work_list 2081 // 2082 // Add a basic block to our work list. 2083 // List is sorted by decreasing postorder sort (same as increasing RPO) 2084 void ciTypeFlow::add_to_work_list(ciTypeFlow::Block* block) { 2085 assert(!block->is_on_work_list(), "must not already be on work list"); 2086 2087 if (CITraceTypeFlow) { 2088 tty->print(">> Adding block "); 2089 block->print_value_on(tty); 2090 tty->print_cr(" to the work list : "); 2091 } 2092 2093 block->set_on_work_list(true); 2094 2095 // decreasing post order sort 2096 2097 Block* prev = NULL; 2098 Block* current = _work_list; 2099 int po = block->post_order(); 2100 while (current != NULL) { 2101 if (!current->has_post_order() || po > current->post_order()) 2102 break; 2103 prev = current; 2104 current = current->next(); 2105 } 2106 if (prev == NULL) { 2107 block->set_next(_work_list); 2108 _work_list = block; 2109 } else { 2110 block->set_next(current); 2111 prev->set_next(block); 2112 } 2113 2114 if (CITraceTypeFlow) { 2115 tty->cr(); 2116 } 2117 } 2118 2119 // ------------------------------------------------------------------ 2120 // ciTypeFlow::block_at 2121 // 2122 // Return the block beginning at bci which has a JsrSet compatible 2123 // with jsrs. 2124 ciTypeFlow::Block* ciTypeFlow::block_at(int bci, ciTypeFlow::JsrSet* jsrs, CreateOption option) { 2125 // First find the right ciBlock. 2126 if (CITraceTypeFlow) { 2127 tty->print(">> Requesting block for %d/", bci); 2128 jsrs->print_on(tty); 2129 tty->cr(); 2130 } 2131 2132 ciBlock* ciblk = _methodBlocks->block_containing(bci); 2133 assert(ciblk->start_bci() == bci, "bad ciBlock boundaries"); 2134 Block* block = get_block_for(ciblk->index(), jsrs, option); 2135 2136 assert(block == NULL? (option == no_create): block->is_backedge_copy() == (option == create_backedge_copy), "create option consistent with result"); 2137 2138 if (CITraceTypeFlow) { 2139 if (block != NULL) { 2140 tty->print(">> Found block "); 2141 block->print_value_on(tty); 2142 tty->cr(); 2143 } else { 2144 tty->print_cr(">> No such block."); 2145 } 2146 } 2147 2148 return block; 2149 } 2150 2151 // ------------------------------------------------------------------ 2152 // ciTypeFlow::make_jsr_record 2153 // 2154 // Make a JsrRecord for a given (entry, return) pair, if such a record 2155 // does not already exist. 2156 ciTypeFlow::JsrRecord* ciTypeFlow::make_jsr_record(int entry_address, 2157 int return_address) { 2158 if (_jsr_records == NULL) { 2159 _jsr_records = new (arena()) GrowableArray<JsrRecord*>(arena(), 2160 _jsr_count, 2161 0, 2162 NULL); 2163 } 2164 JsrRecord* record = NULL; 2165 int len = _jsr_records->length(); 2166 for (int i = 0; i < len; i++) { 2167 JsrRecord* record = _jsr_records->at(i); 2168 if (record->entry_address() == entry_address && 2169 record->return_address() == return_address) { 2170 return record; 2171 } 2172 } 2173 2174 record = new (arena()) JsrRecord(entry_address, return_address); 2175 _jsr_records->append(record); 2176 return record; 2177 } 2178 2179 // ------------------------------------------------------------------ 2180 // ciTypeFlow::flow_exceptions 2181 // 2182 // Merge the current state into all exceptional successors at the 2183 // current point in the code. 2184 void ciTypeFlow::flow_exceptions(GrowableArray<ciTypeFlow::Block*>* exceptions, 2185 GrowableArray<ciInstanceKlass*>* exc_klasses, 2186 ciTypeFlow::StateVector* state) { 2187 int len = exceptions->length(); 2188 assert(exc_klasses->length() == len, "must have same length"); 2189 for (int i = 0; i < len; i++) { 2190 Block* block = exceptions->at(i); 2191 ciInstanceKlass* exception_klass = exc_klasses->at(i); 2192 2193 if (!exception_klass->is_loaded()) { 2194 // Do not compile any code for unloaded exception types. 2195 // Following compiler passes are responsible for doing this also. 2196 continue; 2197 } 2198 2199 if (block->meet_exception(exception_klass, state)) { 2200 // Block was modified and has PO. Add it to the work list. 2201 if (block->has_post_order() && 2202 !block->is_on_work_list()) { 2203 add_to_work_list(block); 2204 } 2205 } 2206 } 2207 } 2208 2209 // ------------------------------------------------------------------ 2210 // ciTypeFlow::flow_successors 2211 // 2212 // Merge the current state into all successors at the current point 2213 // in the code. 2214 void ciTypeFlow::flow_successors(GrowableArray<ciTypeFlow::Block*>* successors, 2215 ciTypeFlow::StateVector* state) { 2216 int len = successors->length(); 2217 for (int i = 0; i < len; i++) { 2218 Block* block = successors->at(i); 2219 if (block->meet(state)) { 2220 // Block was modified and has PO. Add it to the work list. 2221 if (block->has_post_order() && 2222 !block->is_on_work_list()) { 2223 add_to_work_list(block); 2224 } 2225 } 2226 } 2227 } 2228 2229 // ------------------------------------------------------------------ 2230 // ciTypeFlow::can_trap 2231 // 2232 // Tells if a given instruction is able to generate an exception edge. 2233 bool ciTypeFlow::can_trap(ciBytecodeStream& str) { 2234 // Cf. GenerateOopMap::do_exception_edge. 2235 if (!Bytecodes::can_trap(str.cur_bc())) return false; 2236 2237 switch (str.cur_bc()) { 2238 // %%% FIXME: ldc of Class can generate an exception 2239 case Bytecodes::_ldc: 2240 case Bytecodes::_ldc_w: 2241 case Bytecodes::_ldc2_w: 2242 case Bytecodes::_aload_0: 2243 // These bytecodes can trap for rewriting. We need to assume that 2244 // they do not throw exceptions to make the monitor analysis work. 2245 return false; 2246 2247 case Bytecodes::_ireturn: 2248 case Bytecodes::_lreturn: 2249 case Bytecodes::_freturn: 2250 case Bytecodes::_dreturn: 2251 case Bytecodes::_areturn: 2252 case Bytecodes::_return: 2253 // We can assume the monitor stack is empty in this analysis. 2254 return false; 2255 2256 case Bytecodes::_monitorexit: 2257 // We can assume monitors are matched in this analysis. 2258 return false; 2259 2260 default: 2261 return true; 2262 } 2263 } 2264 2265 // ------------------------------------------------------------------ 2266 // ciTypeFlow::clone_loop_heads 2267 // 2268 // Clone the loop heads 2269 bool ciTypeFlow::clone_loop_heads(Loop* lp, StateVector* temp_vector, JsrSet* temp_set) { 2270 bool rslt = false; 2271 for (PreorderLoops iter(loop_tree_root()); !iter.done(); iter.next()) { 2272 lp = iter.current(); 2273 Block* head = lp->head(); 2274 if (lp == loop_tree_root() || 2275 lp->is_irreducible() || 2276 !head->is_clonable_exit(lp)) 2277 continue; 2278 2279 // Avoid BoxLock merge. 2280 if (EliminateNestedLocks && head->has_monitorenter()) 2281 continue; 2282 2283 // check not already cloned 2284 if (head->backedge_copy_count() != 0) 2285 continue; 2286 2287 // Don't clone head of OSR loop to get correct types in start block. 2288 if (is_osr_flow() && head->start() == start_bci()) 2289 continue; 2290 2291 // check _no_ shared head below us 2292 Loop* ch; 2293 for (ch = lp->child(); ch != NULL && ch->head() != head; ch = ch->sibling()); 2294 if (ch != NULL) 2295 continue; 2296 2297 // Clone head 2298 Block* new_head = head->looping_succ(lp); 2299 Block* clone = clone_loop_head(lp, temp_vector, temp_set); 2300 // Update lp's info 2301 clone->set_loop(lp); 2302 lp->set_head(new_head); 2303 lp->set_tail(clone); 2304 // And move original head into outer loop 2305 head->set_loop(lp->parent()); 2306 2307 rslt = true; 2308 } 2309 return rslt; 2310 } 2311 2312 // ------------------------------------------------------------------ 2313 // ciTypeFlow::clone_loop_head 2314 // 2315 // Clone lp's head and replace tail's successors with clone. 2316 // 2317 // | 2318 // v 2319 // head <-> body 2320 // | 2321 // v 2322 // exit 2323 // 2324 // new_head 2325 // 2326 // | 2327 // v 2328 // head ----------\ 2329 // | | 2330 // | v 2331 // | clone <-> body 2332 // | | 2333 // | /--/ 2334 // | | 2335 // v v 2336 // exit 2337 // 2338 ciTypeFlow::Block* ciTypeFlow::clone_loop_head(Loop* lp, StateVector* temp_vector, JsrSet* temp_set) { 2339 Block* head = lp->head(); 2340 Block* tail = lp->tail(); 2341 if (CITraceTypeFlow) { 2342 tty->print(">> Requesting clone of loop head "); head->print_value_on(tty); 2343 tty->print(" for predecessor "); tail->print_value_on(tty); 2344 tty->cr(); 2345 } 2346 Block* clone = block_at(head->start(), head->jsrs(), create_backedge_copy); 2347 assert(clone->backedge_copy_count() == 1, "one backedge copy for all back edges"); 2348 2349 assert(!clone->has_pre_order(), "just created"); 2350 clone->set_next_pre_order(); 2351 2352 // Insert clone after (orig) tail in reverse post order 2353 clone->set_rpo_next(tail->rpo_next()); 2354 tail->set_rpo_next(clone); 2355 2356 // tail->head becomes tail->clone 2357 for (SuccIter iter(tail); !iter.done(); iter.next()) { 2358 if (iter.succ() == head) { 2359 iter.set_succ(clone); 2360 // Update predecessor information 2361 head->predecessors()->remove(tail); 2362 clone->predecessors()->append(tail); 2363 } 2364 } 2365 flow_block(tail, temp_vector, temp_set); 2366 if (head == tail) { 2367 // For self-loops, clone->head becomes clone->clone 2368 flow_block(clone, temp_vector, temp_set); 2369 for (SuccIter iter(clone); !iter.done(); iter.next()) { 2370 if (iter.succ() == head) { 2371 iter.set_succ(clone); 2372 // Update predecessor information 2373 head->predecessors()->remove(clone); 2374 clone->predecessors()->append(clone); 2375 break; 2376 } 2377 } 2378 } 2379 flow_block(clone, temp_vector, temp_set); 2380 2381 return clone; 2382 } 2383 2384 // ------------------------------------------------------------------ 2385 // ciTypeFlow::flow_block 2386 // 2387 // Interpret the effects of the bytecodes on the incoming state 2388 // vector of a basic block. Push the changed state to succeeding 2389 // basic blocks. 2390 void ciTypeFlow::flow_block(ciTypeFlow::Block* block, 2391 ciTypeFlow::StateVector* state, 2392 ciTypeFlow::JsrSet* jsrs) { 2393 if (CITraceTypeFlow) { 2394 tty->print("\n>> ANALYZING BLOCK : "); 2395 tty->cr(); 2396 block->print_on(tty); 2397 } 2398 assert(block->has_pre_order(), "pre-order is assigned before 1st flow"); 2399 2400 int start = block->start(); 2401 int limit = block->limit(); 2402 int control = block->control(); 2403 if (control != ciBlock::fall_through_bci) { 2404 limit = control; 2405 } 2406 2407 // Grab the state from the current block. 2408 block->copy_state_into(state); 2409 state->def_locals()->clear(); 2410 2411 GrowableArray<Block*>* exceptions = block->exceptions(); 2412 GrowableArray<ciInstanceKlass*>* exc_klasses = block->exc_klasses(); 2413 bool has_exceptions = exceptions->length() > 0; 2414 2415 bool exceptions_used = false; 2416 2417 ciBytecodeStream str(method()); 2418 str.reset_to_bci(start); 2419 Bytecodes::Code code; 2420 while ((code = str.next()) != ciBytecodeStream::EOBC() && 2421 str.cur_bci() < limit) { 2422 // Check for exceptional control flow from this point. 2423 if (has_exceptions && can_trap(str)) { 2424 flow_exceptions(exceptions, exc_klasses, state); 2425 exceptions_used = true; 2426 } 2427 // Apply the effects of the current bytecode to our state. 2428 bool res = state->apply_one_bytecode(&str); 2429 2430 // Watch for bailouts. 2431 if (failing()) return; 2432 2433 if (str.cur_bc() == Bytecodes::_monitorenter) { 2434 block->set_has_monitorenter(); 2435 } 2436 2437 if (res) { 2438 2439 // We have encountered a trap. Record it in this block. 2440 block->set_trap(state->trap_bci(), state->trap_index()); 2441 2442 if (CITraceTypeFlow) { 2443 tty->print_cr(">> Found trap"); 2444 block->print_on(tty); 2445 } 2446 2447 // Save set of locals defined in this block 2448 block->def_locals()->add(state->def_locals()); 2449 2450 // Record (no) successors. 2451 block->successors(&str, state, jsrs); 2452 2453 assert(!has_exceptions || exceptions_used, "Not removing exceptions"); 2454 2455 // Discontinue interpretation of this Block. 2456 return; 2457 } 2458 } 2459 2460 GrowableArray<Block*>* successors = NULL; 2461 if (control != ciBlock::fall_through_bci) { 2462 // Check for exceptional control flow from this point. 2463 if (has_exceptions && can_trap(str)) { 2464 flow_exceptions(exceptions, exc_klasses, state); 2465 exceptions_used = true; 2466 } 2467 2468 // Fix the JsrSet to reflect effect of the bytecode. 2469 block->copy_jsrs_into(jsrs); 2470 jsrs->apply_control(this, &str, state); 2471 2472 // Find successor edges based on old state and new JsrSet. 2473 successors = block->successors(&str, state, jsrs); 2474 2475 // Apply the control changes to the state. 2476 state->apply_one_bytecode(&str); 2477 } else { 2478 // Fall through control 2479 successors = block->successors(&str, NULL, NULL); 2480 } 2481 2482 // Save set of locals defined in this block 2483 block->def_locals()->add(state->def_locals()); 2484 2485 // Remove untaken exception paths 2486 if (!exceptions_used) 2487 exceptions->clear(); 2488 2489 // Pass our state to successors. 2490 flow_successors(successors, state); 2491 } 2492 2493 // ------------------------------------------------------------------ 2494 // ciTypeFlow::PostOrderLoops::next 2495 // 2496 // Advance to next loop tree using a postorder, left-to-right traversal. 2497 void ciTypeFlow::PostorderLoops::next() { 2498 assert(!done(), "must not be done."); 2499 if (_current->sibling() != NULL) { 2500 _current = _current->sibling(); 2501 while (_current->child() != NULL) { 2502 _current = _current->child(); 2503 } 2504 } else { 2505 _current = _current->parent(); 2506 } 2507 } 2508 2509 // ------------------------------------------------------------------ 2510 // ciTypeFlow::PreOrderLoops::next 2511 // 2512 // Advance to next loop tree using a preorder, left-to-right traversal. 2513 void ciTypeFlow::PreorderLoops::next() { 2514 assert(!done(), "must not be done."); 2515 if (_current->child() != NULL) { 2516 _current = _current->child(); 2517 } else if (_current->sibling() != NULL) { 2518 _current = _current->sibling(); 2519 } else { 2520 while (_current != _root && _current->sibling() == NULL) { 2521 _current = _current->parent(); 2522 } 2523 if (_current == _root) { 2524 _current = NULL; 2525 assert(done(), "must be done."); 2526 } else { 2527 assert(_current->sibling() != NULL, "must be more to do"); 2528 _current = _current->sibling(); 2529 } 2530 } 2531 } 2532 2533 // ------------------------------------------------------------------ 2534 // ciTypeFlow::Loop::sorted_merge 2535 // 2536 // Merge the branch lp into this branch, sorting on the loop head 2537 // pre_orders. Returns the leaf of the merged branch. 2538 // Child and sibling pointers will be setup later. 2539 // Sort is (looking from leaf towards the root) 2540 // descending on primary key: loop head's pre_order, and 2541 // ascending on secondary key: loop tail's pre_order. 2542 ciTypeFlow::Loop* ciTypeFlow::Loop::sorted_merge(Loop* lp) { 2543 Loop* leaf = this; 2544 Loop* prev = NULL; 2545 Loop* current = leaf; 2546 while (lp != NULL) { 2547 int lp_pre_order = lp->head()->pre_order(); 2548 // Find insertion point for "lp" 2549 while (current != NULL) { 2550 if (current == lp) 2551 return leaf; // Already in list 2552 if (current->head()->pre_order() < lp_pre_order) 2553 break; 2554 if (current->head()->pre_order() == lp_pre_order && 2555 current->tail()->pre_order() > lp->tail()->pre_order()) { 2556 break; 2557 } 2558 prev = current; 2559 current = current->parent(); 2560 } 2561 Loop* next_lp = lp->parent(); // Save future list of items to insert 2562 // Insert lp before current 2563 lp->set_parent(current); 2564 if (prev != NULL) { 2565 prev->set_parent(lp); 2566 } else { 2567 leaf = lp; 2568 } 2569 prev = lp; // Inserted item is new prev[ious] 2570 lp = next_lp; // Next item to insert 2571 } 2572 return leaf; 2573 } 2574 2575 // ------------------------------------------------------------------ 2576 // ciTypeFlow::build_loop_tree 2577 // 2578 // Incrementally build loop tree. 2579 void ciTypeFlow::build_loop_tree(Block* blk) { 2580 assert(!blk->is_post_visited(), "precondition"); 2581 Loop* innermost = NULL; // merge of loop tree branches over all successors 2582 2583 for (SuccIter iter(blk); !iter.done(); iter.next()) { 2584 Loop* lp = NULL; 2585 Block* succ = iter.succ(); 2586 if (!succ->is_post_visited()) { 2587 // Found backedge since predecessor post visited, but successor is not 2588 assert(succ->pre_order() <= blk->pre_order(), "should be backedge"); 2589 2590 // Create a LoopNode to mark this loop. 2591 lp = new (arena()) Loop(succ, blk); 2592 if (succ->loop() == NULL) 2593 succ->set_loop(lp); 2594 // succ->loop will be updated to innermost loop on a later call, when blk==succ 2595 2596 } else { // Nested loop 2597 lp = succ->loop(); 2598 2599 // If succ is loop head, find outer loop. 2600 while (lp != NULL && lp->head() == succ) { 2601 lp = lp->parent(); 2602 } 2603 if (lp == NULL) { 2604 // Infinite loop, it's parent is the root 2605 lp = loop_tree_root(); 2606 } 2607 } 2608 2609 // Check for irreducible loop. 2610 // Successor has already been visited. If the successor's loop head 2611 // has already been post-visited, then this is another entry into the loop. 2612 while (lp->head()->is_post_visited() && lp != loop_tree_root()) { 2613 _has_irreducible_entry = true; 2614 lp->set_irreducible(succ); 2615 if (!succ->is_on_work_list()) { 2616 // Assume irreducible entries need more data flow 2617 add_to_work_list(succ); 2618 } 2619 Loop* plp = lp->parent(); 2620 if (plp == NULL) { 2621 // This only happens for some irreducible cases. The parent 2622 // will be updated during a later pass. 2623 break; 2624 } 2625 lp = plp; 2626 } 2627 2628 // Merge loop tree branch for all successors. 2629 innermost = innermost == NULL ? lp : innermost->sorted_merge(lp); 2630 2631 } // end loop 2632 2633 if (innermost == NULL) { 2634 assert(blk->successors()->length() == 0, "CFG exit"); 2635 blk->set_loop(loop_tree_root()); 2636 } else if (innermost->head() == blk) { 2637 // If loop header, complete the tree pointers 2638 if (blk->loop() != innermost) { 2639 #ifdef ASSERT 2640 assert(blk->loop()->head() == innermost->head(), "same head"); 2641 Loop* dl; 2642 for (dl = innermost; dl != NULL && dl != blk->loop(); dl = dl->parent()); 2643 assert(dl == blk->loop(), "blk->loop() already in innermost list"); 2644 #endif 2645 blk->set_loop(innermost); 2646 } 2647 innermost->def_locals()->add(blk->def_locals()); 2648 Loop* l = innermost; 2649 Loop* p = l->parent(); 2650 while (p && l->head() == blk) { 2651 l->set_sibling(p->child()); // Put self on parents 'next child' 2652 p->set_child(l); // Make self the first child of parent 2653 p->def_locals()->add(l->def_locals()); 2654 l = p; // Walk up the parent chain 2655 p = l->parent(); 2656 } 2657 } else { 2658 blk->set_loop(innermost); 2659 innermost->def_locals()->add(blk->def_locals()); 2660 } 2661 } 2662 2663 // ------------------------------------------------------------------ 2664 // ciTypeFlow::Loop::contains 2665 // 2666 // Returns true if lp is nested loop. 2667 bool ciTypeFlow::Loop::contains(ciTypeFlow::Loop* lp) const { 2668 assert(lp != NULL, ""); 2669 if (this == lp || head() == lp->head()) return true; 2670 int depth1 = depth(); 2671 int depth2 = lp->depth(); 2672 if (depth1 > depth2) 2673 return false; 2674 while (depth1 < depth2) { 2675 depth2--; 2676 lp = lp->parent(); 2677 } 2678 return this == lp; 2679 } 2680 2681 // ------------------------------------------------------------------ 2682 // ciTypeFlow::Loop::depth 2683 // 2684 // Loop depth 2685 int ciTypeFlow::Loop::depth() const { 2686 int dp = 0; 2687 for (Loop* lp = this->parent(); lp != NULL; lp = lp->parent()) 2688 dp++; 2689 return dp; 2690 } 2691 2692 #ifndef PRODUCT 2693 // ------------------------------------------------------------------ 2694 // ciTypeFlow::Loop::print 2695 void ciTypeFlow::Loop::print(outputStream* st, int indent) const { 2696 for (int i = 0; i < indent; i++) st->print(" "); 2697 st->print("%d<-%d %s", 2698 is_root() ? 0 : this->head()->pre_order(), 2699 is_root() ? 0 : this->tail()->pre_order(), 2700 is_irreducible()?" irr":""); 2701 st->print(" defs: "); 2702 def_locals()->print_on(st, _head->outer()->method()->max_locals()); 2703 st->cr(); 2704 for (Loop* ch = child(); ch != NULL; ch = ch->sibling()) 2705 ch->print(st, indent+2); 2706 } 2707 #endif 2708 2709 // ------------------------------------------------------------------ 2710 // ciTypeFlow::df_flow_types 2711 // 2712 // Perform the depth first type flow analysis. Helper for flow_types. 2713 void ciTypeFlow::df_flow_types(Block* start, 2714 bool do_flow, 2715 StateVector* temp_vector, 2716 JsrSet* temp_set) { 2717 int dft_len = 100; 2718 GrowableArray<Block*> stk(dft_len); 2719 2720 ciBlock* dummy = _methodBlocks->make_dummy_block(); 2721 JsrSet* root_set = new JsrSet(NULL, 0); 2722 Block* root_head = new (arena()) Block(this, dummy, root_set); 2723 Block* root_tail = new (arena()) Block(this, dummy, root_set); 2724 root_head->set_pre_order(0); 2725 root_head->set_post_order(0); 2726 root_tail->set_pre_order(max_jint); 2727 root_tail->set_post_order(max_jint); 2728 set_loop_tree_root(new (arena()) Loop(root_head, root_tail)); 2729 2730 stk.push(start); 2731 2732 _next_pre_order = 0; // initialize pre_order counter 2733 _rpo_list = NULL; 2734 int next_po = 0; // initialize post_order counter 2735 2736 // Compute RPO and the control flow graph 2737 int size; 2738 while ((size = stk.length()) > 0) { 2739 Block* blk = stk.top(); // Leave node on stack 2740 if (!blk->is_visited()) { 2741 // forward arc in graph 2742 assert (!blk->has_pre_order(), ""); 2743 blk->set_next_pre_order(); 2744 2745 if (_next_pre_order >= (int)Compile::current()->max_node_limit() / 2) { 2746 // Too many basic blocks. Bail out. 2747 // This can happen when try/finally constructs are nested to depth N, 2748 // and there is O(2**N) cloning of jsr bodies. See bug 4697245! 2749 // "MaxNodeLimit / 2" is used because probably the parser will 2750 // generate at least twice that many nodes and bail out. 2751 record_failure("too many basic blocks"); 2752 return; 2753 } 2754 if (do_flow) { 2755 flow_block(blk, temp_vector, temp_set); 2756 if (failing()) return; // Watch for bailouts. 2757 } 2758 } else if (!blk->is_post_visited()) { 2759 // cross or back arc 2760 for (SuccIter iter(blk); !iter.done(); iter.next()) { 2761 Block* succ = iter.succ(); 2762 if (!succ->is_visited()) { 2763 stk.push(succ); 2764 } 2765 } 2766 if (stk.length() == size) { 2767 // There were no additional children, post visit node now 2768 stk.pop(); // Remove node from stack 2769 2770 build_loop_tree(blk); 2771 blk->set_post_order(next_po++); // Assign post order 2772 prepend_to_rpo_list(blk); 2773 assert(blk->is_post_visited(), ""); 2774 2775 if (blk->is_loop_head() && !blk->is_on_work_list()) { 2776 // Assume loop heads need more data flow 2777 add_to_work_list(blk); 2778 } 2779 } 2780 } else { 2781 stk.pop(); // Remove post-visited node from stack 2782 } 2783 } 2784 } 2785 2786 // ------------------------------------------------------------------ 2787 // ciTypeFlow::flow_types 2788 // 2789 // Perform the type flow analysis, creating and cloning Blocks as 2790 // necessary. 2791 void ciTypeFlow::flow_types() { 2792 ResourceMark rm; 2793 StateVector* temp_vector = new StateVector(this); 2794 JsrSet* temp_set = new JsrSet(NULL, 16); 2795 2796 // Create the method entry block. 2797 Block* start = block_at(start_bci(), temp_set); 2798 2799 // Load the initial state into it. 2800 const StateVector* start_state = get_start_state(); 2801 if (failing()) return; 2802 start->meet(start_state); 2803 2804 // Depth first visit 2805 df_flow_types(start, true /*do flow*/, temp_vector, temp_set); 2806 2807 if (failing()) return; 2808 assert(_rpo_list == start, "must be start"); 2809 2810 // Any loops found? 2811 if (loop_tree_root()->child() != NULL && 2812 env()->comp_level() >= CompLevel_full_optimization) { 2813 // Loop optimizations are not performed on Tier1 compiles. 2814 2815 bool changed = clone_loop_heads(loop_tree_root(), temp_vector, temp_set); 2816 2817 // If some loop heads were cloned, recompute postorder and loop tree 2818 if (changed) { 2819 loop_tree_root()->set_child(NULL); 2820 for (Block* blk = _rpo_list; blk != NULL;) { 2821 Block* next = blk->rpo_next(); 2822 blk->df_init(); 2823 blk = next; 2824 } 2825 df_flow_types(start, false /*no flow*/, temp_vector, temp_set); 2826 } 2827 } 2828 2829 if (CITraceTypeFlow) { 2830 tty->print_cr("\nLoop tree"); 2831 loop_tree_root()->print(); 2832 } 2833 2834 // Continue flow analysis until fixed point reached 2835 2836 debug_only(int max_block = _next_pre_order;) 2837 2838 while (!work_list_empty()) { 2839 Block* blk = work_list_next(); 2840 assert (blk->has_post_order(), "post order assigned above"); 2841 2842 flow_block(blk, temp_vector, temp_set); 2843 2844 assert (max_block == _next_pre_order, "no new blocks"); 2845 assert (!failing(), "no more bailouts"); 2846 } 2847 } 2848 2849 // ------------------------------------------------------------------ 2850 // ciTypeFlow::map_blocks 2851 // 2852 // Create the block map, which indexes blocks in reverse post-order. 2853 void ciTypeFlow::map_blocks() { 2854 assert(_block_map == NULL, "single initialization"); 2855 int block_ct = _next_pre_order; 2856 _block_map = NEW_ARENA_ARRAY(arena(), Block*, block_ct); 2857 assert(block_ct == block_count(), ""); 2858 2859 Block* blk = _rpo_list; 2860 for (int m = 0; m < block_ct; m++) { 2861 int rpo = blk->rpo(); 2862 assert(rpo == m, "should be sequential"); 2863 _block_map[rpo] = blk; 2864 blk = blk->rpo_next(); 2865 } 2866 assert(blk == NULL, "should be done"); 2867 2868 for (int j = 0; j < block_ct; j++) { 2869 assert(_block_map[j] != NULL, "must not drop any blocks"); 2870 Block* block = _block_map[j]; 2871 // Remove dead blocks from successor lists: 2872 for (int e = 0; e <= 1; e++) { 2873 GrowableArray<Block*>* l = e? block->exceptions(): block->successors(); 2874 for (int k = 0; k < l->length(); k++) { 2875 Block* s = l->at(k); 2876 if (!s->has_post_order()) { 2877 if (CITraceTypeFlow) { 2878 tty->print("Removing dead %s successor of #%d: ", (e? "exceptional": "normal"), block->pre_order()); 2879 s->print_value_on(tty); 2880 tty->cr(); 2881 } 2882 l->remove(s); 2883 --k; 2884 } 2885 } 2886 } 2887 } 2888 } 2889 2890 // ------------------------------------------------------------------ 2891 // ciTypeFlow::get_block_for 2892 // 2893 // Find a block with this ciBlock which has a compatible JsrSet. 2894 // If no such block exists, create it, unless the option is no_create. 2895 // If the option is create_backedge_copy, always create a fresh backedge copy. 2896 ciTypeFlow::Block* ciTypeFlow::get_block_for(int ciBlockIndex, ciTypeFlow::JsrSet* jsrs, CreateOption option) { 2897 Arena* a = arena(); 2898 GrowableArray<Block*>* blocks = _idx_to_blocklist[ciBlockIndex]; 2899 if (blocks == NULL) { 2900 // Query only? 2901 if (option == no_create) return NULL; 2902 2903 // Allocate the growable array. 2904 blocks = new (a) GrowableArray<Block*>(a, 4, 0, NULL); 2905 _idx_to_blocklist[ciBlockIndex] = blocks; 2906 } 2907 2908 if (option != create_backedge_copy) { 2909 int len = blocks->length(); 2910 for (int i = 0; i < len; i++) { 2911 Block* block = blocks->at(i); 2912 if (!block->is_backedge_copy() && block->is_compatible_with(jsrs)) { 2913 return block; 2914 } 2915 } 2916 } 2917 2918 // Query only? 2919 if (option == no_create) return NULL; 2920 2921 // We did not find a compatible block. Create one. 2922 Block* new_block = new (a) Block(this, _methodBlocks->block(ciBlockIndex), jsrs); 2923 if (option == create_backedge_copy) new_block->set_backedge_copy(true); 2924 blocks->append(new_block); 2925 return new_block; 2926 } 2927 2928 // ------------------------------------------------------------------ 2929 // ciTypeFlow::backedge_copy_count 2930 // 2931 int ciTypeFlow::backedge_copy_count(int ciBlockIndex, ciTypeFlow::JsrSet* jsrs) const { 2932 GrowableArray<Block*>* blocks = _idx_to_blocklist[ciBlockIndex]; 2933 2934 if (blocks == NULL) { 2935 return 0; 2936 } 2937 2938 int count = 0; 2939 int len = blocks->length(); 2940 for (int i = 0; i < len; i++) { 2941 Block* block = blocks->at(i); 2942 if (block->is_backedge_copy() && block->is_compatible_with(jsrs)) { 2943 count++; 2944 } 2945 } 2946 2947 return count; 2948 } 2949 2950 // ------------------------------------------------------------------ 2951 // ciTypeFlow::do_flow 2952 // 2953 // Perform type inference flow analysis. 2954 void ciTypeFlow::do_flow() { 2955 if (CITraceTypeFlow) { 2956 tty->print_cr("\nPerforming flow analysis on method"); 2957 method()->print(); 2958 if (is_osr_flow()) tty->print(" at OSR bci %d", start_bci()); 2959 tty->cr(); 2960 method()->print_codes(); 2961 } 2962 if (CITraceTypeFlow) { 2963 tty->print_cr("Initial CI Blocks"); 2964 print_on(tty); 2965 } 2966 flow_types(); 2967 // Watch for bailouts. 2968 if (failing()) { 2969 return; 2970 } 2971 2972 map_blocks(); 2973 2974 if (CIPrintTypeFlow || CITraceTypeFlow) { 2975 rpo_print_on(tty); 2976 } 2977 } 2978 2979 // ------------------------------------------------------------------ 2980 // ciTypeFlow::is_dominated_by 2981 // 2982 // Determine if the instruction at bci is dominated by the instruction at dom_bci. 2983 bool ciTypeFlow::is_dominated_by(int bci, int dom_bci) { 2984 assert(!method()->has_jsrs(), "jsrs are not supported"); 2985 2986 ResourceMark rm; 2987 JsrSet* jsrs = new ciTypeFlow::JsrSet(NULL); 2988 int index = _methodBlocks->block_containing(bci)->index(); 2989 int dom_index = _methodBlocks->block_containing(dom_bci)->index(); 2990 Block* block = get_block_for(index, jsrs, ciTypeFlow::no_create); 2991 Block* dom_block = get_block_for(dom_index, jsrs, ciTypeFlow::no_create); 2992 2993 // Start block dominates all other blocks 2994 if (start_block()->rpo() == dom_block->rpo()) { 2995 return true; 2996 } 2997 2998 // Dominated[i] is true if block i is dominated by dom_block 2999 int num_blocks = block_count(); 3000 bool* dominated = NEW_RESOURCE_ARRAY(bool, num_blocks); 3001 for (int i = 0; i < num_blocks; ++i) { 3002 dominated[i] = true; 3003 } 3004 dominated[start_block()->rpo()] = false; 3005 3006 // Iterative dominator algorithm 3007 bool changed = true; 3008 while (changed) { 3009 changed = false; 3010 // Use reverse postorder iteration 3011 for (Block* blk = _rpo_list; blk != NULL; blk = blk->rpo_next()) { 3012 if (blk->is_start()) { 3013 // Ignore start block 3014 continue; 3015 } 3016 // The block is dominated if it is the dominating block 3017 // itself or if all predecessors are dominated. 3018 int index = blk->rpo(); 3019 bool dom = (index == dom_block->rpo()); 3020 if (!dom) { 3021 // Check if all predecessors are dominated 3022 dom = true; 3023 for (int i = 0; i < blk->predecessors()->length(); ++i) { 3024 Block* pred = blk->predecessors()->at(i); 3025 if (!dominated[pred->rpo()]) { 3026 dom = false; 3027 break; 3028 } 3029 } 3030 } 3031 // Update dominator information 3032 if (dominated[index] != dom) { 3033 changed = true; 3034 dominated[index] = dom; 3035 } 3036 } 3037 } 3038 // block dominated by dom_block? 3039 return dominated[block->rpo()]; 3040 } 3041 3042 // ------------------------------------------------------------------ 3043 // ciTypeFlow::record_failure() 3044 // The ciTypeFlow object keeps track of failure reasons separately from the ciEnv. 3045 // This is required because there is not a 1-1 relation between the ciEnv and 3046 // the TypeFlow passes within a compilation task. For example, if the compiler 3047 // is considering inlining a method, it will request a TypeFlow. If that fails, 3048 // the compilation as a whole may continue without the inlining. Some TypeFlow 3049 // requests are not optional; if they fail the requestor is responsible for 3050 // copying the failure reason up to the ciEnv. (See Parse::Parse.) 3051 void ciTypeFlow::record_failure(const char* reason) { 3052 if (env()->log() != NULL) { 3053 env()->log()->elem("failure reason='%s' phase='typeflow'", reason); 3054 } 3055 if (_failure_reason == NULL) { 3056 // Record the first failure reason. 3057 _failure_reason = reason; 3058 } 3059 } 3060 3061 #ifndef PRODUCT 3062 // ------------------------------------------------------------------ 3063 // ciTypeFlow::print_on 3064 void ciTypeFlow::print_on(outputStream* st) const { 3065 // Walk through CI blocks 3066 st->print_cr("********************************************************"); 3067 st->print ("TypeFlow for "); 3068 method()->name()->print_symbol_on(st); 3069 int limit_bci = code_size(); 3070 st->print_cr(" %d bytes", limit_bci); 3071 ciMethodBlocks *mblks = _methodBlocks; 3072 ciBlock* current = NULL; 3073 for (int bci = 0; bci < limit_bci; bci++) { 3074 ciBlock* blk = mblks->block_containing(bci); 3075 if (blk != NULL && blk != current) { 3076 current = blk; 3077 current->print_on(st); 3078 3079 GrowableArray<Block*>* blocks = _idx_to_blocklist[blk->index()]; 3080 int num_blocks = (blocks == NULL) ? 0 : blocks->length(); 3081 3082 if (num_blocks == 0) { 3083 st->print_cr(" No Blocks"); 3084 } else { 3085 for (int i = 0; i < num_blocks; i++) { 3086 Block* block = blocks->at(i); 3087 block->print_on(st); 3088 } 3089 } 3090 st->print_cr("--------------------------------------------------------"); 3091 st->cr(); 3092 } 3093 } 3094 st->print_cr("********************************************************"); 3095 st->cr(); 3096 } 3097 3098 void ciTypeFlow::rpo_print_on(outputStream* st) const { 3099 st->print_cr("********************************************************"); 3100 st->print ("TypeFlow for "); 3101 method()->name()->print_symbol_on(st); 3102 int limit_bci = code_size(); 3103 st->print_cr(" %d bytes", limit_bci); 3104 for (Block* blk = _rpo_list; blk != NULL; blk = blk->rpo_next()) { 3105 blk->print_on(st); 3106 st->print_cr("--------------------------------------------------------"); 3107 st->cr(); 3108 } 3109 st->print_cr("********************************************************"); 3110 st->cr(); 3111 } 3112 #endif