1 /* 2 * Copyright (c) 2000, 2017, 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 || str->is_unresolved_value_type()) { 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::_vaload: 936 case Bytecodes::_aaload: do_aload(str); break; 937 938 case Bytecodes::_vastore: 939 case Bytecodes::_aastore: 940 { 941 pop_object(); 942 pop_int(); 943 pop_objOrValueArray(); 944 break; 945 } 946 case Bytecodes::_aconst_null: 947 { 948 push_null(); 949 break; 950 } 951 case Bytecodes::_vload: 952 case Bytecodes::_aload: load_local_object(str->get_index()); break; 953 case Bytecodes::_aload_0: load_local_object(0); break; 954 case Bytecodes::_aload_1: load_local_object(1); break; 955 case Bytecodes::_aload_2: load_local_object(2); break; 956 case Bytecodes::_aload_3: load_local_object(3); break; 957 958 case Bytecodes::_anewarray: 959 { 960 pop_int(); 961 bool will_link; 962 ciKlass* element_klass = str->get_klass(will_link); 963 if (!will_link) { 964 trap(str, element_klass, str->get_klass_index()); 965 } else { 966 push_object(ciArrayKlass::make(element_klass)); 967 } 968 break; 969 } 970 case Bytecodes::_areturn: 971 case Bytecodes::_vreturn: 972 case Bytecodes::_ifnonnull: 973 case Bytecodes::_ifnull: 974 { 975 pop_object(); 976 break; 977 } 978 case Bytecodes::_monitorenter: 979 { 980 pop_object(); 981 set_monitor_count(monitor_count() + 1); 982 break; 983 } 984 case Bytecodes::_monitorexit: 985 { 986 pop_object(); 987 assert(monitor_count() > 0, "must be a monitor to exit from"); 988 set_monitor_count(monitor_count() - 1); 989 break; 990 } 991 case Bytecodes::_arraylength: 992 { 993 pop_array(); 994 push_int(); 995 break; 996 } 997 case Bytecodes::_vstore: 998 case Bytecodes::_astore: store_local_object(str->get_index()); break; 999 case Bytecodes::_astore_0: store_local_object(0); break; 1000 case Bytecodes::_astore_1: store_local_object(1); break; 1001 case Bytecodes::_astore_2: store_local_object(2); break; 1002 case Bytecodes::_astore_3: store_local_object(3); break; 1003 1004 case Bytecodes::_athrow: 1005 { 1006 NEEDS_CLEANUP; 1007 pop_object(); 1008 break; 1009 } 1010 case Bytecodes::_baload: 1011 case Bytecodes::_caload: 1012 case Bytecodes::_iaload: 1013 case Bytecodes::_saload: 1014 { 1015 pop_int(); 1016 ciTypeArrayKlass* array_klass = pop_typeArray(); 1017 // Put assert here for right type? 1018 push_int(); 1019 break; 1020 } 1021 case Bytecodes::_bastore: 1022 case Bytecodes::_castore: 1023 case Bytecodes::_iastore: 1024 case Bytecodes::_sastore: 1025 { 1026 pop_int(); 1027 pop_int(); 1028 pop_typeArray(); 1029 // assert here? 1030 break; 1031 } 1032 case Bytecodes::_bipush: 1033 case Bytecodes::_iconst_m1: 1034 case Bytecodes::_iconst_0: 1035 case Bytecodes::_iconst_1: 1036 case Bytecodes::_iconst_2: 1037 case Bytecodes::_iconst_3: 1038 case Bytecodes::_iconst_4: 1039 case Bytecodes::_iconst_5: 1040 case Bytecodes::_sipush: 1041 { 1042 push_int(); 1043 break; 1044 } 1045 case Bytecodes::_checkcast: do_checkcast(str); break; 1046 1047 case Bytecodes::_d2f: 1048 { 1049 pop_double(); 1050 push_float(); 1051 break; 1052 } 1053 case Bytecodes::_d2i: 1054 { 1055 pop_double(); 1056 push_int(); 1057 break; 1058 } 1059 case Bytecodes::_d2l: 1060 { 1061 pop_double(); 1062 push_long(); 1063 break; 1064 } 1065 case Bytecodes::_dadd: 1066 case Bytecodes::_ddiv: 1067 case Bytecodes::_dmul: 1068 case Bytecodes::_drem: 1069 case Bytecodes::_dsub: 1070 { 1071 pop_double(); 1072 pop_double(); 1073 push_double(); 1074 break; 1075 } 1076 case Bytecodes::_daload: 1077 { 1078 pop_int(); 1079 ciTypeArrayKlass* array_klass = pop_typeArray(); 1080 // Put assert here for right type? 1081 push_double(); 1082 break; 1083 } 1084 case Bytecodes::_dastore: 1085 { 1086 pop_double(); 1087 pop_int(); 1088 pop_typeArray(); 1089 // assert here? 1090 break; 1091 } 1092 case Bytecodes::_dcmpg: 1093 case Bytecodes::_dcmpl: 1094 { 1095 pop_double(); 1096 pop_double(); 1097 push_int(); 1098 break; 1099 } 1100 case Bytecodes::_dconst_0: 1101 case Bytecodes::_dconst_1: 1102 { 1103 push_double(); 1104 break; 1105 } 1106 case Bytecodes::_dload: load_local_double(str->get_index()); break; 1107 case Bytecodes::_dload_0: load_local_double(0); break; 1108 case Bytecodes::_dload_1: load_local_double(1); break; 1109 case Bytecodes::_dload_2: load_local_double(2); break; 1110 case Bytecodes::_dload_3: load_local_double(3); break; 1111 1112 case Bytecodes::_dneg: 1113 { 1114 pop_double(); 1115 push_double(); 1116 break; 1117 } 1118 case Bytecodes::_dreturn: 1119 { 1120 pop_double(); 1121 break; 1122 } 1123 case Bytecodes::_dstore: store_local_double(str->get_index()); break; 1124 case Bytecodes::_dstore_0: store_local_double(0); break; 1125 case Bytecodes::_dstore_1: store_local_double(1); break; 1126 case Bytecodes::_dstore_2: store_local_double(2); break; 1127 case Bytecodes::_dstore_3: store_local_double(3); break; 1128 1129 case Bytecodes::_dup: 1130 { 1131 push(type_at_tos()); 1132 break; 1133 } 1134 case Bytecodes::_dup_x1: 1135 { 1136 ciType* value1 = pop_value(); 1137 ciType* value2 = pop_value(); 1138 push(value1); 1139 push(value2); 1140 push(value1); 1141 break; 1142 } 1143 case Bytecodes::_dup_x2: 1144 { 1145 ciType* value1 = pop_value(); 1146 ciType* value2 = pop_value(); 1147 ciType* value3 = pop_value(); 1148 push(value1); 1149 push(value3); 1150 push(value2); 1151 push(value1); 1152 break; 1153 } 1154 case Bytecodes::_dup2: 1155 { 1156 ciType* value1 = pop_value(); 1157 ciType* value2 = pop_value(); 1158 push(value2); 1159 push(value1); 1160 push(value2); 1161 push(value1); 1162 break; 1163 } 1164 case Bytecodes::_dup2_x1: 1165 { 1166 ciType* value1 = pop_value(); 1167 ciType* value2 = pop_value(); 1168 ciType* value3 = pop_value(); 1169 push(value2); 1170 push(value1); 1171 push(value3); 1172 push(value2); 1173 push(value1); 1174 break; 1175 } 1176 case Bytecodes::_dup2_x2: 1177 { 1178 ciType* value1 = pop_value(); 1179 ciType* value2 = pop_value(); 1180 ciType* value3 = pop_value(); 1181 ciType* value4 = pop_value(); 1182 push(value2); 1183 push(value1); 1184 push(value4); 1185 push(value3); 1186 push(value2); 1187 push(value1); 1188 break; 1189 } 1190 case Bytecodes::_f2d: 1191 { 1192 pop_float(); 1193 push_double(); 1194 break; 1195 } 1196 case Bytecodes::_f2i: 1197 { 1198 pop_float(); 1199 push_int(); 1200 break; 1201 } 1202 case Bytecodes::_f2l: 1203 { 1204 pop_float(); 1205 push_long(); 1206 break; 1207 } 1208 case Bytecodes::_fadd: 1209 case Bytecodes::_fdiv: 1210 case Bytecodes::_fmul: 1211 case Bytecodes::_frem: 1212 case Bytecodes::_fsub: 1213 { 1214 pop_float(); 1215 pop_float(); 1216 push_float(); 1217 break; 1218 } 1219 case Bytecodes::_faload: 1220 { 1221 pop_int(); 1222 ciTypeArrayKlass* array_klass = pop_typeArray(); 1223 // Put assert here. 1224 push_float(); 1225 break; 1226 } 1227 case Bytecodes::_fastore: 1228 { 1229 pop_float(); 1230 pop_int(); 1231 ciTypeArrayKlass* array_klass = pop_typeArray(); 1232 // Put assert here. 1233 break; 1234 } 1235 case Bytecodes::_fcmpg: 1236 case Bytecodes::_fcmpl: 1237 { 1238 pop_float(); 1239 pop_float(); 1240 push_int(); 1241 break; 1242 } 1243 case Bytecodes::_fconst_0: 1244 case Bytecodes::_fconst_1: 1245 case Bytecodes::_fconst_2: 1246 { 1247 push_float(); 1248 break; 1249 } 1250 case Bytecodes::_fload: load_local_float(str->get_index()); break; 1251 case Bytecodes::_fload_0: load_local_float(0); break; 1252 case Bytecodes::_fload_1: load_local_float(1); break; 1253 case Bytecodes::_fload_2: load_local_float(2); break; 1254 case Bytecodes::_fload_3: load_local_float(3); break; 1255 1256 case Bytecodes::_fneg: 1257 { 1258 pop_float(); 1259 push_float(); 1260 break; 1261 } 1262 case Bytecodes::_freturn: 1263 { 1264 pop_float(); 1265 break; 1266 } 1267 case Bytecodes::_fstore: store_local_float(str->get_index()); break; 1268 case Bytecodes::_fstore_0: store_local_float(0); break; 1269 case Bytecodes::_fstore_1: store_local_float(1); break; 1270 case Bytecodes::_fstore_2: store_local_float(2); break; 1271 case Bytecodes::_fstore_3: store_local_float(3); break; 1272 1273 case Bytecodes::_vgetfield: 1274 case Bytecodes::_getfield: do_getfield(str); break; 1275 case Bytecodes::_getstatic: do_getstatic(str); break; 1276 1277 case Bytecodes::_goto: 1278 case Bytecodes::_goto_w: 1279 case Bytecodes::_nop: 1280 case Bytecodes::_return: 1281 { 1282 // do nothing. 1283 break; 1284 } 1285 case Bytecodes::_i2b: 1286 case Bytecodes::_i2c: 1287 case Bytecodes::_i2s: 1288 case Bytecodes::_ineg: 1289 { 1290 pop_int(); 1291 push_int(); 1292 break; 1293 } 1294 case Bytecodes::_i2d: 1295 { 1296 pop_int(); 1297 push_double(); 1298 break; 1299 } 1300 case Bytecodes::_i2f: 1301 { 1302 pop_int(); 1303 push_float(); 1304 break; 1305 } 1306 case Bytecodes::_i2l: 1307 { 1308 pop_int(); 1309 push_long(); 1310 break; 1311 } 1312 case Bytecodes::_iadd: 1313 case Bytecodes::_iand: 1314 case Bytecodes::_idiv: 1315 case Bytecodes::_imul: 1316 case Bytecodes::_ior: 1317 case Bytecodes::_irem: 1318 case Bytecodes::_ishl: 1319 case Bytecodes::_ishr: 1320 case Bytecodes::_isub: 1321 case Bytecodes::_iushr: 1322 case Bytecodes::_ixor: 1323 { 1324 pop_int(); 1325 pop_int(); 1326 push_int(); 1327 break; 1328 } 1329 case Bytecodes::_if_acmpeq: 1330 case Bytecodes::_if_acmpne: 1331 { 1332 pop_object(); 1333 pop_object(); 1334 break; 1335 } 1336 case Bytecodes::_if_icmpeq: 1337 case Bytecodes::_if_icmpge: 1338 case Bytecodes::_if_icmpgt: 1339 case Bytecodes::_if_icmple: 1340 case Bytecodes::_if_icmplt: 1341 case Bytecodes::_if_icmpne: 1342 { 1343 pop_int(); 1344 pop_int(); 1345 break; 1346 } 1347 case Bytecodes::_ifeq: 1348 case Bytecodes::_ifle: 1349 case Bytecodes::_iflt: 1350 case Bytecodes::_ifge: 1351 case Bytecodes::_ifgt: 1352 case Bytecodes::_ifne: 1353 case Bytecodes::_ireturn: 1354 case Bytecodes::_lookupswitch: 1355 case Bytecodes::_tableswitch: 1356 { 1357 pop_int(); 1358 break; 1359 } 1360 case Bytecodes::_iinc: 1361 { 1362 int lnum = str->get_index(); 1363 check_int(local(lnum)); 1364 store_to_local(lnum); 1365 break; 1366 } 1367 case Bytecodes::_iload: load_local_int(str->get_index()); break; 1368 case Bytecodes::_iload_0: load_local_int(0); break; 1369 case Bytecodes::_iload_1: load_local_int(1); break; 1370 case Bytecodes::_iload_2: load_local_int(2); break; 1371 case Bytecodes::_iload_3: load_local_int(3); break; 1372 1373 case Bytecodes::_instanceof: 1374 { 1375 // Check for uncommon trap: 1376 do_checkcast(str); 1377 pop_object(); 1378 push_int(); 1379 break; 1380 } 1381 case Bytecodes::_invokeinterface: do_invoke(str, true); break; 1382 case Bytecodes::_invokespecial: do_invoke(str, true); break; 1383 case Bytecodes::_invokestatic: do_invoke(str, false); break; 1384 case Bytecodes::_invokevirtual: do_invoke(str, true); break; 1385 case Bytecodes::_invokedynamic: do_invoke(str, false); break; 1386 1387 case Bytecodes::_istore: store_local_int(str->get_index()); break; 1388 case Bytecodes::_istore_0: store_local_int(0); break; 1389 case Bytecodes::_istore_1: store_local_int(1); break; 1390 case Bytecodes::_istore_2: store_local_int(2); break; 1391 case Bytecodes::_istore_3: store_local_int(3); break; 1392 1393 case Bytecodes::_jsr: 1394 case Bytecodes::_jsr_w: do_jsr(str); break; 1395 1396 case Bytecodes::_l2d: 1397 { 1398 pop_long(); 1399 push_double(); 1400 break; 1401 } 1402 case Bytecodes::_l2f: 1403 { 1404 pop_long(); 1405 push_float(); 1406 break; 1407 } 1408 case Bytecodes::_l2i: 1409 { 1410 pop_long(); 1411 push_int(); 1412 break; 1413 } 1414 case Bytecodes::_ladd: 1415 case Bytecodes::_land: 1416 case Bytecodes::_ldiv: 1417 case Bytecodes::_lmul: 1418 case Bytecodes::_lor: 1419 case Bytecodes::_lrem: 1420 case Bytecodes::_lsub: 1421 case Bytecodes::_lxor: 1422 { 1423 pop_long(); 1424 pop_long(); 1425 push_long(); 1426 break; 1427 } 1428 case Bytecodes::_laload: 1429 { 1430 pop_int(); 1431 ciTypeArrayKlass* array_klass = pop_typeArray(); 1432 // Put assert here for right type? 1433 push_long(); 1434 break; 1435 } 1436 case Bytecodes::_lastore: 1437 { 1438 pop_long(); 1439 pop_int(); 1440 pop_typeArray(); 1441 // assert here? 1442 break; 1443 } 1444 case Bytecodes::_lcmp: 1445 { 1446 pop_long(); 1447 pop_long(); 1448 push_int(); 1449 break; 1450 } 1451 case Bytecodes::_lconst_0: 1452 case Bytecodes::_lconst_1: 1453 { 1454 push_long(); 1455 break; 1456 } 1457 case Bytecodes::_ldc: 1458 case Bytecodes::_ldc_w: 1459 case Bytecodes::_ldc2_w: 1460 { 1461 do_ldc(str); 1462 break; 1463 } 1464 1465 case Bytecodes::_lload: load_local_long(str->get_index()); break; 1466 case Bytecodes::_lload_0: load_local_long(0); break; 1467 case Bytecodes::_lload_1: load_local_long(1); break; 1468 case Bytecodes::_lload_2: load_local_long(2); break; 1469 case Bytecodes::_lload_3: load_local_long(3); break; 1470 1471 case Bytecodes::_lneg: 1472 { 1473 pop_long(); 1474 push_long(); 1475 break; 1476 } 1477 case Bytecodes::_lreturn: 1478 { 1479 pop_long(); 1480 break; 1481 } 1482 case Bytecodes::_lshl: 1483 case Bytecodes::_lshr: 1484 case Bytecodes::_lushr: 1485 { 1486 pop_int(); 1487 pop_long(); 1488 push_long(); 1489 break; 1490 } 1491 case Bytecodes::_lstore: store_local_long(str->get_index()); break; 1492 case Bytecodes::_lstore_0: store_local_long(0); break; 1493 case Bytecodes::_lstore_1: store_local_long(1); break; 1494 case Bytecodes::_lstore_2: store_local_long(2); break; 1495 case Bytecodes::_lstore_3: store_local_long(3); break; 1496 1497 case Bytecodes::_multianewarray: do_multianewarray(str); break; 1498 1499 case Bytecodes::_new: do_new(str); break; 1500 1501 case Bytecodes::_vdefault: do_vdefault(str); break; 1502 case Bytecodes::_vwithfield: do_vwithfield(str); break; 1503 1504 case Bytecodes::_newarray: do_newarray(str); break; 1505 1506 case Bytecodes::_pop: 1507 { 1508 pop(); 1509 break; 1510 } 1511 case Bytecodes::_pop2: 1512 { 1513 pop(); 1514 pop(); 1515 break; 1516 } 1517 1518 case Bytecodes::_putfield: do_putfield(str); break; 1519 case Bytecodes::_putstatic: do_putstatic(str); break; 1520 1521 case Bytecodes::_ret: do_ret(str); break; 1522 1523 case Bytecodes::_swap: 1524 { 1525 ciType* value1 = pop_value(); 1526 ciType* value2 = pop_value(); 1527 push(value1); 1528 push(value2); 1529 break; 1530 } 1531 case Bytecodes::_vunbox: 1532 { 1533 do_vunbox(str); 1534 break; 1535 } 1536 case Bytecodes::_vbox: 1537 { 1538 do_vbox(str); 1539 break; 1540 } 1541 case Bytecodes::_wide: 1542 default: 1543 { 1544 // The iterator should skip this. 1545 ShouldNotReachHere(); 1546 break; 1547 } 1548 } 1549 1550 if (CITraceTypeFlow) { 1551 print_on(tty); 1552 } 1553 1554 return (_trap_bci != -1); 1555 } 1556 1557 #ifndef PRODUCT 1558 // ------------------------------------------------------------------ 1559 // ciTypeFlow::StateVector::print_cell_on 1560 void ciTypeFlow::StateVector::print_cell_on(outputStream* st, Cell c) const { 1561 ciType* type = type_at(c); 1562 if (type == top_type()) { 1563 st->print("top"); 1564 } else if (type == bottom_type()) { 1565 st->print("bottom"); 1566 } else if (type == null_type()) { 1567 st->print("null"); 1568 } else if (type == long2_type()) { 1569 st->print("long2"); 1570 } else if (type == double2_type()) { 1571 st->print("double2"); 1572 } else if (is_int(type)) { 1573 st->print("int"); 1574 } else if (is_long(type)) { 1575 st->print("long"); 1576 } else if (is_float(type)) { 1577 st->print("float"); 1578 } else if (is_double(type)) { 1579 st->print("double"); 1580 } else if (type->is_return_address()) { 1581 st->print("address(%d)", type->as_return_address()->bci()); 1582 } else { 1583 if (type->is_klass()) { 1584 type->as_klass()->name()->print_symbol_on(st); 1585 } else { 1586 st->print("UNEXPECTED TYPE"); 1587 type->print(); 1588 } 1589 } 1590 } 1591 1592 // ------------------------------------------------------------------ 1593 // ciTypeFlow::StateVector::print_on 1594 void ciTypeFlow::StateVector::print_on(outputStream* st) const { 1595 int num_locals = _outer->max_locals(); 1596 int num_stack = stack_size(); 1597 int num_monitors = monitor_count(); 1598 st->print_cr(" State : locals %d, stack %d, monitors %d", num_locals, num_stack, num_monitors); 1599 if (num_stack >= 0) { 1600 int i; 1601 for (i = 0; i < num_locals; i++) { 1602 st->print(" local %2d : ", i); 1603 print_cell_on(st, local(i)); 1604 st->cr(); 1605 } 1606 for (i = 0; i < num_stack; i++) { 1607 st->print(" stack %2d : ", i); 1608 print_cell_on(st, stack(i)); 1609 st->cr(); 1610 } 1611 } 1612 } 1613 #endif 1614 1615 1616 // ------------------------------------------------------------------ 1617 // ciTypeFlow::SuccIter::next 1618 // 1619 void ciTypeFlow::SuccIter::next() { 1620 int succ_ct = _pred->successors()->length(); 1621 int next = _index + 1; 1622 if (next < succ_ct) { 1623 _index = next; 1624 _succ = _pred->successors()->at(next); 1625 return; 1626 } 1627 for (int i = next - succ_ct; i < _pred->exceptions()->length(); i++) { 1628 // Do not compile any code for unloaded exception types. 1629 // Following compiler passes are responsible for doing this also. 1630 ciInstanceKlass* exception_klass = _pred->exc_klasses()->at(i); 1631 if (exception_klass->is_loaded()) { 1632 _index = next; 1633 _succ = _pred->exceptions()->at(i); 1634 return; 1635 } 1636 next++; 1637 } 1638 _index = -1; 1639 _succ = NULL; 1640 } 1641 1642 // ------------------------------------------------------------------ 1643 // ciTypeFlow::SuccIter::set_succ 1644 // 1645 void ciTypeFlow::SuccIter::set_succ(Block* succ) { 1646 int succ_ct = _pred->successors()->length(); 1647 if (_index < succ_ct) { 1648 _pred->successors()->at_put(_index, succ); 1649 } else { 1650 int idx = _index - succ_ct; 1651 _pred->exceptions()->at_put(idx, succ); 1652 } 1653 } 1654 1655 // ciTypeFlow::Block 1656 // 1657 // A basic block. 1658 1659 // ------------------------------------------------------------------ 1660 // ciTypeFlow::Block::Block 1661 ciTypeFlow::Block::Block(ciTypeFlow* outer, 1662 ciBlock *ciblk, 1663 ciTypeFlow::JsrSet* jsrs) { 1664 _ciblock = ciblk; 1665 _exceptions = NULL; 1666 _exc_klasses = NULL; 1667 _successors = NULL; 1668 _predecessors = new (outer->arena()) GrowableArray<Block*>(outer->arena(), 1, 0, NULL); 1669 _state = new (outer->arena()) StateVector(outer); 1670 JsrSet* new_jsrs = 1671 new (outer->arena()) JsrSet(outer->arena(), jsrs->size()); 1672 jsrs->copy_into(new_jsrs); 1673 _jsrs = new_jsrs; 1674 _next = NULL; 1675 _on_work_list = false; 1676 _backedge_copy = false; 1677 _has_monitorenter = false; 1678 _trap_bci = -1; 1679 _trap_index = 0; 1680 df_init(); 1681 1682 if (CITraceTypeFlow) { 1683 tty->print_cr(">> Created new block"); 1684 print_on(tty); 1685 } 1686 1687 assert(this->outer() == outer, "outer link set up"); 1688 assert(!outer->have_block_count(), "must not have mapped blocks yet"); 1689 } 1690 1691 // ------------------------------------------------------------------ 1692 // ciTypeFlow::Block::df_init 1693 void ciTypeFlow::Block::df_init() { 1694 _pre_order = -1; assert(!has_pre_order(), ""); 1695 _post_order = -1; assert(!has_post_order(), ""); 1696 _loop = NULL; 1697 _irreducible_entry = false; 1698 _rpo_next = NULL; 1699 } 1700 1701 // ------------------------------------------------------------------ 1702 // ciTypeFlow::Block::successors 1703 // 1704 // Get the successors for this Block. 1705 GrowableArray<ciTypeFlow::Block*>* 1706 ciTypeFlow::Block::successors(ciBytecodeStream* str, 1707 ciTypeFlow::StateVector* state, 1708 ciTypeFlow::JsrSet* jsrs) { 1709 if (_successors == NULL) { 1710 if (CITraceTypeFlow) { 1711 tty->print(">> Computing successors for block "); 1712 print_value_on(tty); 1713 tty->cr(); 1714 } 1715 1716 ciTypeFlow* analyzer = outer(); 1717 Arena* arena = analyzer->arena(); 1718 Block* block = NULL; 1719 bool has_successor = !has_trap() && 1720 (control() != ciBlock::fall_through_bci || limit() < analyzer->code_size()); 1721 if (!has_successor) { 1722 _successors = 1723 new (arena) GrowableArray<Block*>(arena, 1, 0, NULL); 1724 // No successors 1725 } else if (control() == ciBlock::fall_through_bci) { 1726 assert(str->cur_bci() == limit(), "bad block end"); 1727 // This block simply falls through to the next. 1728 _successors = 1729 new (arena) GrowableArray<Block*>(arena, 1, 0, NULL); 1730 1731 Block* block = analyzer->block_at(limit(), _jsrs); 1732 assert(_successors->length() == FALL_THROUGH, ""); 1733 _successors->append(block); 1734 } else { 1735 int current_bci = str->cur_bci(); 1736 int next_bci = str->next_bci(); 1737 int branch_bci = -1; 1738 Block* target = NULL; 1739 assert(str->next_bci() == limit(), "bad block end"); 1740 // This block is not a simple fall-though. Interpret 1741 // the current bytecode to find our successors. 1742 switch (str->cur_bc()) { 1743 case Bytecodes::_ifeq: case Bytecodes::_ifne: 1744 case Bytecodes::_iflt: case Bytecodes::_ifge: 1745 case Bytecodes::_ifgt: case Bytecodes::_ifle: 1746 case Bytecodes::_if_icmpeq: case Bytecodes::_if_icmpne: 1747 case Bytecodes::_if_icmplt: case Bytecodes::_if_icmpge: 1748 case Bytecodes::_if_icmpgt: case Bytecodes::_if_icmple: 1749 case Bytecodes::_if_acmpeq: case Bytecodes::_if_acmpne: 1750 case Bytecodes::_ifnull: case Bytecodes::_ifnonnull: 1751 // Our successors are the branch target and the next bci. 1752 branch_bci = str->get_dest(); 1753 _successors = 1754 new (arena) GrowableArray<Block*>(arena, 2, 0, NULL); 1755 assert(_successors->length() == IF_NOT_TAKEN, ""); 1756 _successors->append(analyzer->block_at(next_bci, jsrs)); 1757 assert(_successors->length() == IF_TAKEN, ""); 1758 _successors->append(analyzer->block_at(branch_bci, jsrs)); 1759 break; 1760 1761 case Bytecodes::_goto: 1762 branch_bci = str->get_dest(); 1763 _successors = 1764 new (arena) GrowableArray<Block*>(arena, 1, 0, NULL); 1765 assert(_successors->length() == GOTO_TARGET, ""); 1766 _successors->append(analyzer->block_at(branch_bci, jsrs)); 1767 break; 1768 1769 case Bytecodes::_jsr: 1770 branch_bci = str->get_dest(); 1771 _successors = 1772 new (arena) GrowableArray<Block*>(arena, 1, 0, NULL); 1773 assert(_successors->length() == GOTO_TARGET, ""); 1774 _successors->append(analyzer->block_at(branch_bci, jsrs)); 1775 break; 1776 1777 case Bytecodes::_goto_w: 1778 case Bytecodes::_jsr_w: 1779 _successors = 1780 new (arena) GrowableArray<Block*>(arena, 1, 0, NULL); 1781 assert(_successors->length() == GOTO_TARGET, ""); 1782 _successors->append(analyzer->block_at(str->get_far_dest(), jsrs)); 1783 break; 1784 1785 case Bytecodes::_tableswitch: { 1786 Bytecode_tableswitch tableswitch(str); 1787 1788 int len = tableswitch.length(); 1789 _successors = 1790 new (arena) GrowableArray<Block*>(arena, len+1, 0, NULL); 1791 int bci = current_bci + tableswitch.default_offset(); 1792 Block* block = analyzer->block_at(bci, jsrs); 1793 assert(_successors->length() == SWITCH_DEFAULT, ""); 1794 _successors->append(block); 1795 while (--len >= 0) { 1796 int bci = current_bci + tableswitch.dest_offset_at(len); 1797 block = analyzer->block_at(bci, jsrs); 1798 assert(_successors->length() >= SWITCH_CASES, ""); 1799 _successors->append_if_missing(block); 1800 } 1801 break; 1802 } 1803 1804 case Bytecodes::_lookupswitch: { 1805 Bytecode_lookupswitch lookupswitch(str); 1806 1807 int npairs = lookupswitch.number_of_pairs(); 1808 _successors = 1809 new (arena) GrowableArray<Block*>(arena, npairs+1, 0, NULL); 1810 int bci = current_bci + lookupswitch.default_offset(); 1811 Block* block = analyzer->block_at(bci, jsrs); 1812 assert(_successors->length() == SWITCH_DEFAULT, ""); 1813 _successors->append(block); 1814 while(--npairs >= 0) { 1815 LookupswitchPair pair = lookupswitch.pair_at(npairs); 1816 int bci = current_bci + pair.offset(); 1817 Block* block = analyzer->block_at(bci, jsrs); 1818 assert(_successors->length() >= SWITCH_CASES, ""); 1819 _successors->append_if_missing(block); 1820 } 1821 break; 1822 } 1823 1824 case Bytecodes::_athrow: 1825 case Bytecodes::_ireturn: 1826 case Bytecodes::_lreturn: 1827 case Bytecodes::_freturn: 1828 case Bytecodes::_dreturn: 1829 case Bytecodes::_areturn: 1830 case Bytecodes::_vreturn: 1831 case Bytecodes::_return: 1832 _successors = 1833 new (arena) GrowableArray<Block*>(arena, 1, 0, NULL); 1834 // No successors 1835 break; 1836 1837 case Bytecodes::_ret: { 1838 _successors = 1839 new (arena) GrowableArray<Block*>(arena, 1, 0, NULL); 1840 1841 Cell local = state->local(str->get_index()); 1842 ciType* return_address = state->type_at(local); 1843 assert(return_address->is_return_address(), "verify: wrong type"); 1844 int bci = return_address->as_return_address()->bci(); 1845 assert(_successors->length() == GOTO_TARGET, ""); 1846 _successors->append(analyzer->block_at(bci, jsrs)); 1847 break; 1848 } 1849 1850 case Bytecodes::_wide: 1851 default: 1852 ShouldNotReachHere(); 1853 break; 1854 } 1855 } 1856 1857 // Set predecessor information 1858 for (int i = 0; i < _successors->length(); i++) { 1859 Block* block = _successors->at(i); 1860 block->predecessors()->append(this); 1861 } 1862 } 1863 return _successors; 1864 } 1865 1866 // ------------------------------------------------------------------ 1867 // ciTypeFlow::Block:compute_exceptions 1868 // 1869 // Compute the exceptional successors and types for this Block. 1870 void ciTypeFlow::Block::compute_exceptions() { 1871 assert(_exceptions == NULL && _exc_klasses == NULL, "repeat"); 1872 1873 if (CITraceTypeFlow) { 1874 tty->print(">> Computing exceptions for block "); 1875 print_value_on(tty); 1876 tty->cr(); 1877 } 1878 1879 ciTypeFlow* analyzer = outer(); 1880 Arena* arena = analyzer->arena(); 1881 1882 // Any bci in the block will do. 1883 ciExceptionHandlerStream str(analyzer->method(), start()); 1884 1885 // Allocate our growable arrays. 1886 int exc_count = str.count(); 1887 _exceptions = new (arena) GrowableArray<Block*>(arena, exc_count, 0, NULL); 1888 _exc_klasses = new (arena) GrowableArray<ciInstanceKlass*>(arena, exc_count, 1889 0, NULL); 1890 1891 for ( ; !str.is_done(); str.next()) { 1892 ciExceptionHandler* handler = str.handler(); 1893 int bci = handler->handler_bci(); 1894 ciInstanceKlass* klass = NULL; 1895 if (bci == -1) { 1896 // There is no catch all. It is possible to exit the method. 1897 break; 1898 } 1899 if (handler->is_catch_all()) { 1900 klass = analyzer->env()->Throwable_klass(); 1901 } else { 1902 klass = handler->catch_klass(); 1903 } 1904 Block* block = analyzer->block_at(bci, _jsrs); 1905 _exceptions->append(block); 1906 block->predecessors()->append(this); 1907 _exc_klasses->append(klass); 1908 } 1909 } 1910 1911 // ------------------------------------------------------------------ 1912 // ciTypeFlow::Block::set_backedge_copy 1913 // Use this only to make a pre-existing public block into a backedge copy. 1914 void ciTypeFlow::Block::set_backedge_copy(bool z) { 1915 assert(z || (z == is_backedge_copy()), "cannot make a backedge copy public"); 1916 _backedge_copy = z; 1917 } 1918 1919 // ------------------------------------------------------------------ 1920 // ciTypeFlow::Block::is_clonable_exit 1921 // 1922 // At most 2 normal successors, one of which continues looping, 1923 // and all exceptional successors must exit. 1924 bool ciTypeFlow::Block::is_clonable_exit(ciTypeFlow::Loop* lp) { 1925 int normal_cnt = 0; 1926 int in_loop_cnt = 0; 1927 for (SuccIter iter(this); !iter.done(); iter.next()) { 1928 Block* succ = iter.succ(); 1929 if (iter.is_normal_ctrl()) { 1930 if (++normal_cnt > 2) return false; 1931 if (lp->contains(succ->loop())) { 1932 if (++in_loop_cnt > 1) return false; 1933 } 1934 } else { 1935 if (lp->contains(succ->loop())) return false; 1936 } 1937 } 1938 return in_loop_cnt == 1; 1939 } 1940 1941 // ------------------------------------------------------------------ 1942 // ciTypeFlow::Block::looping_succ 1943 // 1944 ciTypeFlow::Block* ciTypeFlow::Block::looping_succ(ciTypeFlow::Loop* lp) { 1945 assert(successors()->length() <= 2, "at most 2 normal successors"); 1946 for (SuccIter iter(this); !iter.done(); iter.next()) { 1947 Block* succ = iter.succ(); 1948 if (lp->contains(succ->loop())) { 1949 return succ; 1950 } 1951 } 1952 return NULL; 1953 } 1954 1955 #ifndef PRODUCT 1956 // ------------------------------------------------------------------ 1957 // ciTypeFlow::Block::print_value_on 1958 void ciTypeFlow::Block::print_value_on(outputStream* st) const { 1959 if (has_pre_order()) st->print("#%-2d ", pre_order()); 1960 if (has_rpo()) st->print("rpo#%-2d ", rpo()); 1961 st->print("[%d - %d)", start(), limit()); 1962 if (is_loop_head()) st->print(" lphd"); 1963 if (is_irreducible_entry()) st->print(" irred"); 1964 if (_jsrs->size() > 0) { st->print("/"); _jsrs->print_on(st); } 1965 if (is_backedge_copy()) st->print("/backedge_copy"); 1966 } 1967 1968 // ------------------------------------------------------------------ 1969 // ciTypeFlow::Block::print_on 1970 void ciTypeFlow::Block::print_on(outputStream* st) const { 1971 if ((Verbose || WizardMode) && (limit() >= 0)) { 1972 // Don't print 'dummy' blocks (i.e. blocks with limit() '-1') 1973 outer()->method()->print_codes_on(start(), limit(), st); 1974 } 1975 st->print_cr(" ==================================================== "); 1976 st->print (" "); 1977 print_value_on(st); 1978 st->print(" Stored locals: "); def_locals()->print_on(st, outer()->method()->max_locals()); tty->cr(); 1979 if (loop() && loop()->parent() != NULL) { 1980 st->print(" loops:"); 1981 Loop* lp = loop(); 1982 do { 1983 st->print(" %d<-%d", lp->head()->pre_order(),lp->tail()->pre_order()); 1984 if (lp->is_irreducible()) st->print("(ir)"); 1985 lp = lp->parent(); 1986 } while (lp->parent() != NULL); 1987 } 1988 st->cr(); 1989 _state->print_on(st); 1990 if (_successors == NULL) { 1991 st->print_cr(" No successor information"); 1992 } else { 1993 int num_successors = _successors->length(); 1994 st->print_cr(" Successors : %d", num_successors); 1995 for (int i = 0; i < num_successors; i++) { 1996 Block* successor = _successors->at(i); 1997 st->print(" "); 1998 successor->print_value_on(st); 1999 st->cr(); 2000 } 2001 } 2002 if (_predecessors == NULL) { 2003 st->print_cr(" No predecessor information"); 2004 } else { 2005 int num_predecessors = _predecessors->length(); 2006 st->print_cr(" Predecessors : %d", num_predecessors); 2007 for (int i = 0; i < num_predecessors; i++) { 2008 Block* predecessor = _predecessors->at(i); 2009 st->print(" "); 2010 predecessor->print_value_on(st); 2011 st->cr(); 2012 } 2013 } 2014 if (_exceptions == NULL) { 2015 st->print_cr(" No exception information"); 2016 } else { 2017 int num_exceptions = _exceptions->length(); 2018 st->print_cr(" Exceptions : %d", num_exceptions); 2019 for (int i = 0; i < num_exceptions; i++) { 2020 Block* exc_succ = _exceptions->at(i); 2021 ciInstanceKlass* exc_klass = _exc_klasses->at(i); 2022 st->print(" "); 2023 exc_succ->print_value_on(st); 2024 st->print(" -- "); 2025 exc_klass->name()->print_symbol_on(st); 2026 st->cr(); 2027 } 2028 } 2029 if (has_trap()) { 2030 st->print_cr(" Traps on %d with trap index %d", trap_bci(), trap_index()); 2031 } 2032 st->print_cr(" ==================================================== "); 2033 } 2034 #endif 2035 2036 #ifndef PRODUCT 2037 // ------------------------------------------------------------------ 2038 // ciTypeFlow::LocalSet::print_on 2039 void ciTypeFlow::LocalSet::print_on(outputStream* st, int limit) const { 2040 st->print("{"); 2041 for (int i = 0; i < max; i++) { 2042 if (test(i)) st->print(" %d", i); 2043 } 2044 if (limit > max) { 2045 st->print(" %d..%d ", max, limit); 2046 } 2047 st->print(" }"); 2048 } 2049 #endif 2050 2051 // ciTypeFlow 2052 // 2053 // This is a pass over the bytecodes which computes the following: 2054 // basic block structure 2055 // interpreter type-states (a la the verifier) 2056 2057 // ------------------------------------------------------------------ 2058 // ciTypeFlow::ciTypeFlow 2059 ciTypeFlow::ciTypeFlow(ciEnv* env, ciMethod* method, int osr_bci) { 2060 _env = env; 2061 _method = method; 2062 _methodBlocks = method->get_method_blocks(); 2063 _max_locals = method->max_locals(); 2064 _max_stack = method->max_stack(); 2065 _code_size = method->code_size(); 2066 _has_irreducible_entry = false; 2067 _osr_bci = osr_bci; 2068 _failure_reason = NULL; 2069 assert(0 <= start_bci() && start_bci() < code_size() , "correct osr_bci argument: 0 <= %d < %d", start_bci(), code_size()); 2070 _work_list = NULL; 2071 2072 _ciblock_count = _methodBlocks->num_blocks(); 2073 _idx_to_blocklist = NEW_ARENA_ARRAY(arena(), GrowableArray<Block*>*, _ciblock_count); 2074 for (int i = 0; i < _ciblock_count; i++) { 2075 _idx_to_blocklist[i] = NULL; 2076 } 2077 _block_map = NULL; // until all blocks are seen 2078 _jsr_count = 0; 2079 _jsr_records = NULL; 2080 } 2081 2082 // ------------------------------------------------------------------ 2083 // ciTypeFlow::work_list_next 2084 // 2085 // Get the next basic block from our work list. 2086 ciTypeFlow::Block* ciTypeFlow::work_list_next() { 2087 assert(!work_list_empty(), "work list must not be empty"); 2088 Block* next_block = _work_list; 2089 _work_list = next_block->next(); 2090 next_block->set_next(NULL); 2091 next_block->set_on_work_list(false); 2092 return next_block; 2093 } 2094 2095 // ------------------------------------------------------------------ 2096 // ciTypeFlow::add_to_work_list 2097 // 2098 // Add a basic block to our work list. 2099 // List is sorted by decreasing postorder sort (same as increasing RPO) 2100 void ciTypeFlow::add_to_work_list(ciTypeFlow::Block* block) { 2101 assert(!block->is_on_work_list(), "must not already be on work list"); 2102 2103 if (CITraceTypeFlow) { 2104 tty->print(">> Adding block "); 2105 block->print_value_on(tty); 2106 tty->print_cr(" to the work list : "); 2107 } 2108 2109 block->set_on_work_list(true); 2110 2111 // decreasing post order sort 2112 2113 Block* prev = NULL; 2114 Block* current = _work_list; 2115 int po = block->post_order(); 2116 while (current != NULL) { 2117 if (!current->has_post_order() || po > current->post_order()) 2118 break; 2119 prev = current; 2120 current = current->next(); 2121 } 2122 if (prev == NULL) { 2123 block->set_next(_work_list); 2124 _work_list = block; 2125 } else { 2126 block->set_next(current); 2127 prev->set_next(block); 2128 } 2129 2130 if (CITraceTypeFlow) { 2131 tty->cr(); 2132 } 2133 } 2134 2135 // ------------------------------------------------------------------ 2136 // ciTypeFlow::block_at 2137 // 2138 // Return the block beginning at bci which has a JsrSet compatible 2139 // with jsrs. 2140 ciTypeFlow::Block* ciTypeFlow::block_at(int bci, ciTypeFlow::JsrSet* jsrs, CreateOption option) { 2141 // First find the right ciBlock. 2142 if (CITraceTypeFlow) { 2143 tty->print(">> Requesting block for %d/", bci); 2144 jsrs->print_on(tty); 2145 tty->cr(); 2146 } 2147 2148 ciBlock* ciblk = _methodBlocks->block_containing(bci); 2149 assert(ciblk->start_bci() == bci, "bad ciBlock boundaries"); 2150 Block* block = get_block_for(ciblk->index(), jsrs, option); 2151 2152 assert(block == NULL? (option == no_create): block->is_backedge_copy() == (option == create_backedge_copy), "create option consistent with result"); 2153 2154 if (CITraceTypeFlow) { 2155 if (block != NULL) { 2156 tty->print(">> Found block "); 2157 block->print_value_on(tty); 2158 tty->cr(); 2159 } else { 2160 tty->print_cr(">> No such block."); 2161 } 2162 } 2163 2164 return block; 2165 } 2166 2167 // ------------------------------------------------------------------ 2168 // ciTypeFlow::make_jsr_record 2169 // 2170 // Make a JsrRecord for a given (entry, return) pair, if such a record 2171 // does not already exist. 2172 ciTypeFlow::JsrRecord* ciTypeFlow::make_jsr_record(int entry_address, 2173 int return_address) { 2174 if (_jsr_records == NULL) { 2175 _jsr_records = new (arena()) GrowableArray<JsrRecord*>(arena(), 2176 _jsr_count, 2177 0, 2178 NULL); 2179 } 2180 JsrRecord* record = NULL; 2181 int len = _jsr_records->length(); 2182 for (int i = 0; i < len; i++) { 2183 JsrRecord* record = _jsr_records->at(i); 2184 if (record->entry_address() == entry_address && 2185 record->return_address() == return_address) { 2186 return record; 2187 } 2188 } 2189 2190 record = new (arena()) JsrRecord(entry_address, return_address); 2191 _jsr_records->append(record); 2192 return record; 2193 } 2194 2195 // ------------------------------------------------------------------ 2196 // ciTypeFlow::flow_exceptions 2197 // 2198 // Merge the current state into all exceptional successors at the 2199 // current point in the code. 2200 void ciTypeFlow::flow_exceptions(GrowableArray<ciTypeFlow::Block*>* exceptions, 2201 GrowableArray<ciInstanceKlass*>* exc_klasses, 2202 ciTypeFlow::StateVector* state) { 2203 int len = exceptions->length(); 2204 assert(exc_klasses->length() == len, "must have same length"); 2205 for (int i = 0; i < len; i++) { 2206 Block* block = exceptions->at(i); 2207 ciInstanceKlass* exception_klass = exc_klasses->at(i); 2208 2209 if (!exception_klass->is_loaded()) { 2210 // Do not compile any code for unloaded exception types. 2211 // Following compiler passes are responsible for doing this also. 2212 continue; 2213 } 2214 2215 if (block->meet_exception(exception_klass, state)) { 2216 // Block was modified and has PO. Add it to the work list. 2217 if (block->has_post_order() && 2218 !block->is_on_work_list()) { 2219 add_to_work_list(block); 2220 } 2221 } 2222 } 2223 } 2224 2225 // ------------------------------------------------------------------ 2226 // ciTypeFlow::flow_successors 2227 // 2228 // Merge the current state into all successors at the current point 2229 // in the code. 2230 void ciTypeFlow::flow_successors(GrowableArray<ciTypeFlow::Block*>* successors, 2231 ciTypeFlow::StateVector* state) { 2232 int len = successors->length(); 2233 for (int i = 0; i < len; i++) { 2234 Block* block = successors->at(i); 2235 if (block->meet(state)) { 2236 // Block was modified and has PO. Add it to the work list. 2237 if (block->has_post_order() && 2238 !block->is_on_work_list()) { 2239 add_to_work_list(block); 2240 } 2241 } 2242 } 2243 } 2244 2245 // ------------------------------------------------------------------ 2246 // ciTypeFlow::can_trap 2247 // 2248 // Tells if a given instruction is able to generate an exception edge. 2249 bool ciTypeFlow::can_trap(ciBytecodeStream& str) { 2250 // Cf. GenerateOopMap::do_exception_edge. 2251 if (!Bytecodes::can_trap(str.cur_bc())) return false; 2252 2253 switch (str.cur_bc()) { 2254 // %%% FIXME: ldc of Class can generate an exception 2255 case Bytecodes::_ldc: 2256 case Bytecodes::_ldc_w: 2257 case Bytecodes::_ldc2_w: 2258 case Bytecodes::_aload_0: 2259 // These bytecodes can trap for rewriting. We need to assume that 2260 // they do not throw exceptions to make the monitor analysis work. 2261 return false; 2262 2263 case Bytecodes::_ireturn: 2264 case Bytecodes::_lreturn: 2265 case Bytecodes::_freturn: 2266 case Bytecodes::_dreturn: 2267 case Bytecodes::_areturn: 2268 case Bytecodes::_vreturn: 2269 case Bytecodes::_return: 2270 // We can assume the monitor stack is empty in this analysis. 2271 return false; 2272 2273 case Bytecodes::_monitorexit: 2274 // We can assume monitors are matched in this analysis. 2275 return false; 2276 } 2277 2278 return true; 2279 } 2280 2281 // ------------------------------------------------------------------ 2282 // ciTypeFlow::clone_loop_heads 2283 // 2284 // Clone the loop heads 2285 bool ciTypeFlow::clone_loop_heads(Loop* lp, StateVector* temp_vector, JsrSet* temp_set) { 2286 bool rslt = false; 2287 for (PreorderLoops iter(loop_tree_root()); !iter.done(); iter.next()) { 2288 lp = iter.current(); 2289 Block* head = lp->head(); 2290 if (lp == loop_tree_root() || 2291 lp->is_irreducible() || 2292 !head->is_clonable_exit(lp)) 2293 continue; 2294 2295 // Avoid BoxLock merge. 2296 if (EliminateNestedLocks && head->has_monitorenter()) 2297 continue; 2298 2299 // check not already cloned 2300 if (head->backedge_copy_count() != 0) 2301 continue; 2302 2303 // Don't clone head of OSR loop to get correct types in start block. 2304 if (is_osr_flow() && head->start() == start_bci()) 2305 continue; 2306 2307 // check _no_ shared head below us 2308 Loop* ch; 2309 for (ch = lp->child(); ch != NULL && ch->head() != head; ch = ch->sibling()); 2310 if (ch != NULL) 2311 continue; 2312 2313 // Clone head 2314 Block* new_head = head->looping_succ(lp); 2315 Block* clone = clone_loop_head(lp, temp_vector, temp_set); 2316 // Update lp's info 2317 clone->set_loop(lp); 2318 lp->set_head(new_head); 2319 lp->set_tail(clone); 2320 // And move original head into outer loop 2321 head->set_loop(lp->parent()); 2322 2323 rslt = true; 2324 } 2325 return rslt; 2326 } 2327 2328 // ------------------------------------------------------------------ 2329 // ciTypeFlow::clone_loop_head 2330 // 2331 // Clone lp's head and replace tail's successors with clone. 2332 // 2333 // | 2334 // v 2335 // head <-> body 2336 // | 2337 // v 2338 // exit 2339 // 2340 // new_head 2341 // 2342 // | 2343 // v 2344 // head ----------\ 2345 // | | 2346 // | v 2347 // | clone <-> body 2348 // | | 2349 // | /--/ 2350 // | | 2351 // v v 2352 // exit 2353 // 2354 ciTypeFlow::Block* ciTypeFlow::clone_loop_head(Loop* lp, StateVector* temp_vector, JsrSet* temp_set) { 2355 Block* head = lp->head(); 2356 Block* tail = lp->tail(); 2357 if (CITraceTypeFlow) { 2358 tty->print(">> Requesting clone of loop head "); head->print_value_on(tty); 2359 tty->print(" for predecessor "); tail->print_value_on(tty); 2360 tty->cr(); 2361 } 2362 Block* clone = block_at(head->start(), head->jsrs(), create_backedge_copy); 2363 assert(clone->backedge_copy_count() == 1, "one backedge copy for all back edges"); 2364 2365 assert(!clone->has_pre_order(), "just created"); 2366 clone->set_next_pre_order(); 2367 2368 // Insert clone after (orig) tail in reverse post order 2369 clone->set_rpo_next(tail->rpo_next()); 2370 tail->set_rpo_next(clone); 2371 2372 // tail->head becomes tail->clone 2373 for (SuccIter iter(tail); !iter.done(); iter.next()) { 2374 if (iter.succ() == head) { 2375 iter.set_succ(clone); 2376 // Update predecessor information 2377 head->predecessors()->remove(tail); 2378 clone->predecessors()->append(tail); 2379 } 2380 } 2381 flow_block(tail, temp_vector, temp_set); 2382 if (head == tail) { 2383 // For self-loops, clone->head becomes clone->clone 2384 flow_block(clone, temp_vector, temp_set); 2385 for (SuccIter iter(clone); !iter.done(); iter.next()) { 2386 if (iter.succ() == head) { 2387 iter.set_succ(clone); 2388 // Update predecessor information 2389 head->predecessors()->remove(clone); 2390 clone->predecessors()->append(clone); 2391 break; 2392 } 2393 } 2394 } 2395 flow_block(clone, temp_vector, temp_set); 2396 2397 return clone; 2398 } 2399 2400 // ------------------------------------------------------------------ 2401 // ciTypeFlow::flow_block 2402 // 2403 // Interpret the effects of the bytecodes on the incoming state 2404 // vector of a basic block. Push the changed state to succeeding 2405 // basic blocks. 2406 void ciTypeFlow::flow_block(ciTypeFlow::Block* block, 2407 ciTypeFlow::StateVector* state, 2408 ciTypeFlow::JsrSet* jsrs) { 2409 if (CITraceTypeFlow) { 2410 tty->print("\n>> ANALYZING BLOCK : "); 2411 tty->cr(); 2412 block->print_on(tty); 2413 } 2414 assert(block->has_pre_order(), "pre-order is assigned before 1st flow"); 2415 2416 int start = block->start(); 2417 int limit = block->limit(); 2418 int control = block->control(); 2419 if (control != ciBlock::fall_through_bci) { 2420 limit = control; 2421 } 2422 2423 // Grab the state from the current block. 2424 block->copy_state_into(state); 2425 state->def_locals()->clear(); 2426 2427 GrowableArray<Block*>* exceptions = block->exceptions(); 2428 GrowableArray<ciInstanceKlass*>* exc_klasses = block->exc_klasses(); 2429 bool has_exceptions = exceptions->length() > 0; 2430 2431 bool exceptions_used = false; 2432 2433 ciBytecodeStream str(method()); 2434 str.reset_to_bci(start); 2435 Bytecodes::Code code; 2436 while ((code = str.next()) != ciBytecodeStream::EOBC() && 2437 str.cur_bci() < limit) { 2438 // Check for exceptional control flow from this point. 2439 if (has_exceptions && can_trap(str)) { 2440 flow_exceptions(exceptions, exc_klasses, state); 2441 exceptions_used = true; 2442 } 2443 // Apply the effects of the current bytecode to our state. 2444 bool res = state->apply_one_bytecode(&str); 2445 2446 // Watch for bailouts. 2447 if (failing()) return; 2448 2449 if (str.cur_bc() == Bytecodes::_monitorenter) { 2450 block->set_has_monitorenter(); 2451 } 2452 2453 if (res) { 2454 2455 // We have encountered a trap. Record it in this block. 2456 block->set_trap(state->trap_bci(), state->trap_index()); 2457 2458 if (CITraceTypeFlow) { 2459 tty->print_cr(">> Found trap"); 2460 block->print_on(tty); 2461 } 2462 2463 // Save set of locals defined in this block 2464 block->def_locals()->add(state->def_locals()); 2465 2466 // Record (no) successors. 2467 block->successors(&str, state, jsrs); 2468 2469 assert(!has_exceptions || exceptions_used, "Not removing exceptions"); 2470 2471 // Discontinue interpretation of this Block. 2472 return; 2473 } 2474 } 2475 2476 GrowableArray<Block*>* successors = NULL; 2477 if (control != ciBlock::fall_through_bci) { 2478 // Check for exceptional control flow from this point. 2479 if (has_exceptions && can_trap(str)) { 2480 flow_exceptions(exceptions, exc_klasses, state); 2481 exceptions_used = true; 2482 } 2483 2484 // Fix the JsrSet to reflect effect of the bytecode. 2485 block->copy_jsrs_into(jsrs); 2486 jsrs->apply_control(this, &str, state); 2487 2488 // Find successor edges based on old state and new JsrSet. 2489 successors = block->successors(&str, state, jsrs); 2490 2491 // Apply the control changes to the state. 2492 state->apply_one_bytecode(&str); 2493 } else { 2494 // Fall through control 2495 successors = block->successors(&str, NULL, NULL); 2496 } 2497 2498 // Save set of locals defined in this block 2499 block->def_locals()->add(state->def_locals()); 2500 2501 // Remove untaken exception paths 2502 if (!exceptions_used) 2503 exceptions->clear(); 2504 2505 // Pass our state to successors. 2506 flow_successors(successors, state); 2507 } 2508 2509 // ------------------------------------------------------------------ 2510 // ciTypeFlow::PostOrderLoops::next 2511 // 2512 // Advance to next loop tree using a postorder, left-to-right traversal. 2513 void ciTypeFlow::PostorderLoops::next() { 2514 assert(!done(), "must not be done."); 2515 if (_current->sibling() != NULL) { 2516 _current = _current->sibling(); 2517 while (_current->child() != NULL) { 2518 _current = _current->child(); 2519 } 2520 } else { 2521 _current = _current->parent(); 2522 } 2523 } 2524 2525 // ------------------------------------------------------------------ 2526 // ciTypeFlow::PreOrderLoops::next 2527 // 2528 // Advance to next loop tree using a preorder, left-to-right traversal. 2529 void ciTypeFlow::PreorderLoops::next() { 2530 assert(!done(), "must not be done."); 2531 if (_current->child() != NULL) { 2532 _current = _current->child(); 2533 } else if (_current->sibling() != NULL) { 2534 _current = _current->sibling(); 2535 } else { 2536 while (_current != _root && _current->sibling() == NULL) { 2537 _current = _current->parent(); 2538 } 2539 if (_current == _root) { 2540 _current = NULL; 2541 assert(done(), "must be done."); 2542 } else { 2543 assert(_current->sibling() != NULL, "must be more to do"); 2544 _current = _current->sibling(); 2545 } 2546 } 2547 } 2548 2549 // ------------------------------------------------------------------ 2550 // ciTypeFlow::Loop::sorted_merge 2551 // 2552 // Merge the branch lp into this branch, sorting on the loop head 2553 // pre_orders. Returns the leaf of the merged branch. 2554 // Child and sibling pointers will be setup later. 2555 // Sort is (looking from leaf towards the root) 2556 // descending on primary key: loop head's pre_order, and 2557 // ascending on secondary key: loop tail's pre_order. 2558 ciTypeFlow::Loop* ciTypeFlow::Loop::sorted_merge(Loop* lp) { 2559 Loop* leaf = this; 2560 Loop* prev = NULL; 2561 Loop* current = leaf; 2562 while (lp != NULL) { 2563 int lp_pre_order = lp->head()->pre_order(); 2564 // Find insertion point for "lp" 2565 while (current != NULL) { 2566 if (current == lp) 2567 return leaf; // Already in list 2568 if (current->head()->pre_order() < lp_pre_order) 2569 break; 2570 if (current->head()->pre_order() == lp_pre_order && 2571 current->tail()->pre_order() > lp->tail()->pre_order()) { 2572 break; 2573 } 2574 prev = current; 2575 current = current->parent(); 2576 } 2577 Loop* next_lp = lp->parent(); // Save future list of items to insert 2578 // Insert lp before current 2579 lp->set_parent(current); 2580 if (prev != NULL) { 2581 prev->set_parent(lp); 2582 } else { 2583 leaf = lp; 2584 } 2585 prev = lp; // Inserted item is new prev[ious] 2586 lp = next_lp; // Next item to insert 2587 } 2588 return leaf; 2589 } 2590 2591 // ------------------------------------------------------------------ 2592 // ciTypeFlow::build_loop_tree 2593 // 2594 // Incrementally build loop tree. 2595 void ciTypeFlow::build_loop_tree(Block* blk) { 2596 assert(!blk->is_post_visited(), "precondition"); 2597 Loop* innermost = NULL; // merge of loop tree branches over all successors 2598 2599 for (SuccIter iter(blk); !iter.done(); iter.next()) { 2600 Loop* lp = NULL; 2601 Block* succ = iter.succ(); 2602 if (!succ->is_post_visited()) { 2603 // Found backedge since predecessor post visited, but successor is not 2604 assert(succ->pre_order() <= blk->pre_order(), "should be backedge"); 2605 2606 // Create a LoopNode to mark this loop. 2607 lp = new (arena()) Loop(succ, blk); 2608 if (succ->loop() == NULL) 2609 succ->set_loop(lp); 2610 // succ->loop will be updated to innermost loop on a later call, when blk==succ 2611 2612 } else { // Nested loop 2613 lp = succ->loop(); 2614 2615 // If succ is loop head, find outer loop. 2616 while (lp != NULL && lp->head() == succ) { 2617 lp = lp->parent(); 2618 } 2619 if (lp == NULL) { 2620 // Infinite loop, it's parent is the root 2621 lp = loop_tree_root(); 2622 } 2623 } 2624 2625 // Check for irreducible loop. 2626 // Successor has already been visited. If the successor's loop head 2627 // has already been post-visited, then this is another entry into the loop. 2628 while (lp->head()->is_post_visited() && lp != loop_tree_root()) { 2629 _has_irreducible_entry = true; 2630 lp->set_irreducible(succ); 2631 if (!succ->is_on_work_list()) { 2632 // Assume irreducible entries need more data flow 2633 add_to_work_list(succ); 2634 } 2635 Loop* plp = lp->parent(); 2636 if (plp == NULL) { 2637 // This only happens for some irreducible cases. The parent 2638 // will be updated during a later pass. 2639 break; 2640 } 2641 lp = plp; 2642 } 2643 2644 // Merge loop tree branch for all successors. 2645 innermost = innermost == NULL ? lp : innermost->sorted_merge(lp); 2646 2647 } // end loop 2648 2649 if (innermost == NULL) { 2650 assert(blk->successors()->length() == 0, "CFG exit"); 2651 blk->set_loop(loop_tree_root()); 2652 } else if (innermost->head() == blk) { 2653 // If loop header, complete the tree pointers 2654 if (blk->loop() != innermost) { 2655 #ifdef ASSERT 2656 assert(blk->loop()->head() == innermost->head(), "same head"); 2657 Loop* dl; 2658 for (dl = innermost; dl != NULL && dl != blk->loop(); dl = dl->parent()); 2659 assert(dl == blk->loop(), "blk->loop() already in innermost list"); 2660 #endif 2661 blk->set_loop(innermost); 2662 } 2663 innermost->def_locals()->add(blk->def_locals()); 2664 Loop* l = innermost; 2665 Loop* p = l->parent(); 2666 while (p && l->head() == blk) { 2667 l->set_sibling(p->child()); // Put self on parents 'next child' 2668 p->set_child(l); // Make self the first child of parent 2669 p->def_locals()->add(l->def_locals()); 2670 l = p; // Walk up the parent chain 2671 p = l->parent(); 2672 } 2673 } else { 2674 blk->set_loop(innermost); 2675 innermost->def_locals()->add(blk->def_locals()); 2676 } 2677 } 2678 2679 // ------------------------------------------------------------------ 2680 // ciTypeFlow::Loop::contains 2681 // 2682 // Returns true if lp is nested loop. 2683 bool ciTypeFlow::Loop::contains(ciTypeFlow::Loop* lp) const { 2684 assert(lp != NULL, ""); 2685 if (this == lp || head() == lp->head()) return true; 2686 int depth1 = depth(); 2687 int depth2 = lp->depth(); 2688 if (depth1 > depth2) 2689 return false; 2690 while (depth1 < depth2) { 2691 depth2--; 2692 lp = lp->parent(); 2693 } 2694 return this == lp; 2695 } 2696 2697 // ------------------------------------------------------------------ 2698 // ciTypeFlow::Loop::depth 2699 // 2700 // Loop depth 2701 int ciTypeFlow::Loop::depth() const { 2702 int dp = 0; 2703 for (Loop* lp = this->parent(); lp != NULL; lp = lp->parent()) 2704 dp++; 2705 return dp; 2706 } 2707 2708 #ifndef PRODUCT 2709 // ------------------------------------------------------------------ 2710 // ciTypeFlow::Loop::print 2711 void ciTypeFlow::Loop::print(outputStream* st, int indent) const { 2712 for (int i = 0; i < indent; i++) st->print(" "); 2713 st->print("%d<-%d %s", 2714 is_root() ? 0 : this->head()->pre_order(), 2715 is_root() ? 0 : this->tail()->pre_order(), 2716 is_irreducible()?" irr":""); 2717 st->print(" defs: "); 2718 def_locals()->print_on(st, _head->outer()->method()->max_locals()); 2719 st->cr(); 2720 for (Loop* ch = child(); ch != NULL; ch = ch->sibling()) 2721 ch->print(st, indent+2); 2722 } 2723 #endif 2724 2725 // ------------------------------------------------------------------ 2726 // ciTypeFlow::df_flow_types 2727 // 2728 // Perform the depth first type flow analysis. Helper for flow_types. 2729 void ciTypeFlow::df_flow_types(Block* start, 2730 bool do_flow, 2731 StateVector* temp_vector, 2732 JsrSet* temp_set) { 2733 int dft_len = 100; 2734 GrowableArray<Block*> stk(dft_len); 2735 2736 ciBlock* dummy = _methodBlocks->make_dummy_block(); 2737 JsrSet* root_set = new JsrSet(NULL, 0); 2738 Block* root_head = new (arena()) Block(this, dummy, root_set); 2739 Block* root_tail = new (arena()) Block(this, dummy, root_set); 2740 root_head->set_pre_order(0); 2741 root_head->set_post_order(0); 2742 root_tail->set_pre_order(max_jint); 2743 root_tail->set_post_order(max_jint); 2744 set_loop_tree_root(new (arena()) Loop(root_head, root_tail)); 2745 2746 stk.push(start); 2747 2748 _next_pre_order = 0; // initialize pre_order counter 2749 _rpo_list = NULL; 2750 int next_po = 0; // initialize post_order counter 2751 2752 // Compute RPO and the control flow graph 2753 int size; 2754 while ((size = stk.length()) > 0) { 2755 Block* blk = stk.top(); // Leave node on stack 2756 if (!blk->is_visited()) { 2757 // forward arc in graph 2758 assert (!blk->has_pre_order(), ""); 2759 blk->set_next_pre_order(); 2760 2761 if (_next_pre_order >= (int)Compile::current()->max_node_limit() / 2) { 2762 // Too many basic blocks. Bail out. 2763 // This can happen when try/finally constructs are nested to depth N, 2764 // and there is O(2**N) cloning of jsr bodies. See bug 4697245! 2765 // "MaxNodeLimit / 2" is used because probably the parser will 2766 // generate at least twice that many nodes and bail out. 2767 record_failure("too many basic blocks"); 2768 return; 2769 } 2770 if (do_flow) { 2771 flow_block(blk, temp_vector, temp_set); 2772 if (failing()) return; // Watch for bailouts. 2773 } 2774 } else if (!blk->is_post_visited()) { 2775 // cross or back arc 2776 for (SuccIter iter(blk); !iter.done(); iter.next()) { 2777 Block* succ = iter.succ(); 2778 if (!succ->is_visited()) { 2779 stk.push(succ); 2780 } 2781 } 2782 if (stk.length() == size) { 2783 // There were no additional children, post visit node now 2784 stk.pop(); // Remove node from stack 2785 2786 build_loop_tree(blk); 2787 blk->set_post_order(next_po++); // Assign post order 2788 prepend_to_rpo_list(blk); 2789 assert(blk->is_post_visited(), ""); 2790 2791 if (blk->is_loop_head() && !blk->is_on_work_list()) { 2792 // Assume loop heads need more data flow 2793 add_to_work_list(blk); 2794 } 2795 } 2796 } else { 2797 stk.pop(); // Remove post-visited node from stack 2798 } 2799 } 2800 } 2801 2802 // ------------------------------------------------------------------ 2803 // ciTypeFlow::flow_types 2804 // 2805 // Perform the type flow analysis, creating and cloning Blocks as 2806 // necessary. 2807 void ciTypeFlow::flow_types() { 2808 ResourceMark rm; 2809 StateVector* temp_vector = new StateVector(this); 2810 JsrSet* temp_set = new JsrSet(NULL, 16); 2811 2812 // Create the method entry block. 2813 Block* start = block_at(start_bci(), temp_set); 2814 2815 // Load the initial state into it. 2816 const StateVector* start_state = get_start_state(); 2817 if (failing()) return; 2818 start->meet(start_state); 2819 2820 // Depth first visit 2821 df_flow_types(start, true /*do flow*/, temp_vector, temp_set); 2822 2823 if (failing()) return; 2824 assert(_rpo_list == start, "must be start"); 2825 2826 // Any loops found? 2827 if (loop_tree_root()->child() != NULL && 2828 env()->comp_level() >= CompLevel_full_optimization) { 2829 // Loop optimizations are not performed on Tier1 compiles. 2830 2831 bool changed = clone_loop_heads(loop_tree_root(), temp_vector, temp_set); 2832 2833 // If some loop heads were cloned, recompute postorder and loop tree 2834 if (changed) { 2835 loop_tree_root()->set_child(NULL); 2836 for (Block* blk = _rpo_list; blk != NULL;) { 2837 Block* next = blk->rpo_next(); 2838 blk->df_init(); 2839 blk = next; 2840 } 2841 df_flow_types(start, false /*no flow*/, temp_vector, temp_set); 2842 } 2843 } 2844 2845 if (CITraceTypeFlow) { 2846 tty->print_cr("\nLoop tree"); 2847 loop_tree_root()->print(); 2848 } 2849 2850 // Continue flow analysis until fixed point reached 2851 2852 debug_only(int max_block = _next_pre_order;) 2853 2854 while (!work_list_empty()) { 2855 Block* blk = work_list_next(); 2856 assert (blk->has_post_order(), "post order assigned above"); 2857 2858 flow_block(blk, temp_vector, temp_set); 2859 2860 assert (max_block == _next_pre_order, "no new blocks"); 2861 assert (!failing(), "no more bailouts"); 2862 } 2863 } 2864 2865 // ------------------------------------------------------------------ 2866 // ciTypeFlow::map_blocks 2867 // 2868 // Create the block map, which indexes blocks in reverse post-order. 2869 void ciTypeFlow::map_blocks() { 2870 assert(_block_map == NULL, "single initialization"); 2871 int block_ct = _next_pre_order; 2872 _block_map = NEW_ARENA_ARRAY(arena(), Block*, block_ct); 2873 assert(block_ct == block_count(), ""); 2874 2875 Block* blk = _rpo_list; 2876 for (int m = 0; m < block_ct; m++) { 2877 int rpo = blk->rpo(); 2878 assert(rpo == m, "should be sequential"); 2879 _block_map[rpo] = blk; 2880 blk = blk->rpo_next(); 2881 } 2882 assert(blk == NULL, "should be done"); 2883 2884 for (int j = 0; j < block_ct; j++) { 2885 assert(_block_map[j] != NULL, "must not drop any blocks"); 2886 Block* block = _block_map[j]; 2887 // Remove dead blocks from successor lists: 2888 for (int e = 0; e <= 1; e++) { 2889 GrowableArray<Block*>* l = e? block->exceptions(): block->successors(); 2890 for (int k = 0; k < l->length(); k++) { 2891 Block* s = l->at(k); 2892 if (!s->has_post_order()) { 2893 if (CITraceTypeFlow) { 2894 tty->print("Removing dead %s successor of #%d: ", (e? "exceptional": "normal"), block->pre_order()); 2895 s->print_value_on(tty); 2896 tty->cr(); 2897 } 2898 l->remove(s); 2899 --k; 2900 } 2901 } 2902 } 2903 } 2904 } 2905 2906 // ------------------------------------------------------------------ 2907 // ciTypeFlow::get_block_for 2908 // 2909 // Find a block with this ciBlock which has a compatible JsrSet. 2910 // If no such block exists, create it, unless the option is no_create. 2911 // If the option is create_backedge_copy, always create a fresh backedge copy. 2912 ciTypeFlow::Block* ciTypeFlow::get_block_for(int ciBlockIndex, ciTypeFlow::JsrSet* jsrs, CreateOption option) { 2913 Arena* a = arena(); 2914 GrowableArray<Block*>* blocks = _idx_to_blocklist[ciBlockIndex]; 2915 if (blocks == NULL) { 2916 // Query only? 2917 if (option == no_create) return NULL; 2918 2919 // Allocate the growable array. 2920 blocks = new (a) GrowableArray<Block*>(a, 4, 0, NULL); 2921 _idx_to_blocklist[ciBlockIndex] = blocks; 2922 } 2923 2924 if (option != create_backedge_copy) { 2925 int len = blocks->length(); 2926 for (int i = 0; i < len; i++) { 2927 Block* block = blocks->at(i); 2928 if (!block->is_backedge_copy() && block->is_compatible_with(jsrs)) { 2929 return block; 2930 } 2931 } 2932 } 2933 2934 // Query only? 2935 if (option == no_create) return NULL; 2936 2937 // We did not find a compatible block. Create one. 2938 Block* new_block = new (a) Block(this, _methodBlocks->block(ciBlockIndex), jsrs); 2939 if (option == create_backedge_copy) new_block->set_backedge_copy(true); 2940 blocks->append(new_block); 2941 return new_block; 2942 } 2943 2944 // ------------------------------------------------------------------ 2945 // ciTypeFlow::backedge_copy_count 2946 // 2947 int ciTypeFlow::backedge_copy_count(int ciBlockIndex, ciTypeFlow::JsrSet* jsrs) const { 2948 GrowableArray<Block*>* blocks = _idx_to_blocklist[ciBlockIndex]; 2949 2950 if (blocks == NULL) { 2951 return 0; 2952 } 2953 2954 int count = 0; 2955 int len = blocks->length(); 2956 for (int i = 0; i < len; i++) { 2957 Block* block = blocks->at(i); 2958 if (block->is_backedge_copy() && block->is_compatible_with(jsrs)) { 2959 count++; 2960 } 2961 } 2962 2963 return count; 2964 } 2965 2966 // ------------------------------------------------------------------ 2967 // ciTypeFlow::do_flow 2968 // 2969 // Perform type inference flow analysis. 2970 void ciTypeFlow::do_flow() { 2971 if (CITraceTypeFlow) { 2972 tty->print_cr("\nPerforming flow analysis on method"); 2973 method()->print(); 2974 if (is_osr_flow()) tty->print(" at OSR bci %d", start_bci()); 2975 tty->cr(); 2976 method()->print_codes(); 2977 } 2978 if (CITraceTypeFlow) { 2979 tty->print_cr("Initial CI Blocks"); 2980 print_on(tty); 2981 } 2982 flow_types(); 2983 // Watch for bailouts. 2984 if (failing()) { 2985 return; 2986 } 2987 2988 map_blocks(); 2989 2990 if (CIPrintTypeFlow || CITraceTypeFlow) { 2991 rpo_print_on(tty); 2992 } 2993 } 2994 2995 // ------------------------------------------------------------------ 2996 // ciTypeFlow::is_dominated_by 2997 // 2998 // Determine if the instruction at bci is dominated by the instruction at dom_bci. 2999 bool ciTypeFlow::is_dominated_by(int bci, int dom_bci) { 3000 assert(!method()->has_jsrs(), "jsrs are not supported"); 3001 3002 ResourceMark rm; 3003 JsrSet* jsrs = new ciTypeFlow::JsrSet(NULL); 3004 int index = _methodBlocks->block_containing(bci)->index(); 3005 int dom_index = _methodBlocks->block_containing(dom_bci)->index(); 3006 Block* block = get_block_for(index, jsrs, ciTypeFlow::no_create); 3007 Block* dom_block = get_block_for(dom_index, jsrs, ciTypeFlow::no_create); 3008 3009 // Start block dominates all other blocks 3010 if (start_block()->rpo() == dom_block->rpo()) { 3011 return true; 3012 } 3013 3014 // Dominated[i] is true if block i is dominated by dom_block 3015 int num_blocks = block_count(); 3016 bool* dominated = NEW_RESOURCE_ARRAY(bool, num_blocks); 3017 for (int i = 0; i < num_blocks; ++i) { 3018 dominated[i] = true; 3019 } 3020 dominated[start_block()->rpo()] = false; 3021 3022 // Iterative dominator algorithm 3023 bool changed = true; 3024 while (changed) { 3025 changed = false; 3026 // Use reverse postorder iteration 3027 for (Block* blk = _rpo_list; blk != NULL; blk = blk->rpo_next()) { 3028 if (blk->is_start()) { 3029 // Ignore start block 3030 continue; 3031 } 3032 // The block is dominated if it is the dominating block 3033 // itself or if all predecessors are dominated. 3034 int index = blk->rpo(); 3035 bool dom = (index == dom_block->rpo()); 3036 if (!dom) { 3037 // Check if all predecessors are dominated 3038 dom = true; 3039 for (int i = 0; i < blk->predecessors()->length(); ++i) { 3040 Block* pred = blk->predecessors()->at(i); 3041 if (!dominated[pred->rpo()]) { 3042 dom = false; 3043 break; 3044 } 3045 } 3046 } 3047 // Update dominator information 3048 if (dominated[index] != dom) { 3049 changed = true; 3050 dominated[index] = dom; 3051 } 3052 } 3053 } 3054 // block dominated by dom_block? 3055 return dominated[block->rpo()]; 3056 } 3057 3058 // ------------------------------------------------------------------ 3059 // ciTypeFlow::record_failure() 3060 // The ciTypeFlow object keeps track of failure reasons separately from the ciEnv. 3061 // This is required because there is not a 1-1 relation between the ciEnv and 3062 // the TypeFlow passes within a compilation task. For example, if the compiler 3063 // is considering inlining a method, it will request a TypeFlow. If that fails, 3064 // the compilation as a whole may continue without the inlining. Some TypeFlow 3065 // requests are not optional; if they fail the requestor is responsible for 3066 // copying the failure reason up to the ciEnv. (See Parse::Parse.) 3067 void ciTypeFlow::record_failure(const char* reason) { 3068 if (env()->log() != NULL) { 3069 env()->log()->elem("failure reason='%s' phase='typeflow'", reason); 3070 } 3071 if (_failure_reason == NULL) { 3072 // Record the first failure reason. 3073 _failure_reason = reason; 3074 } 3075 } 3076 3077 #ifndef PRODUCT 3078 // ------------------------------------------------------------------ 3079 // ciTypeFlow::print_on 3080 void ciTypeFlow::print_on(outputStream* st) const { 3081 // Walk through CI blocks 3082 st->print_cr("********************************************************"); 3083 st->print ("TypeFlow for "); 3084 method()->name()->print_symbol_on(st); 3085 int limit_bci = code_size(); 3086 st->print_cr(" %d bytes", limit_bci); 3087 ciMethodBlocks *mblks = _methodBlocks; 3088 ciBlock* current = NULL; 3089 for (int bci = 0; bci < limit_bci; bci++) { 3090 ciBlock* blk = mblks->block_containing(bci); 3091 if (blk != NULL && blk != current) { 3092 current = blk; 3093 current->print_on(st); 3094 3095 GrowableArray<Block*>* blocks = _idx_to_blocklist[blk->index()]; 3096 int num_blocks = (blocks == NULL) ? 0 : blocks->length(); 3097 3098 if (num_blocks == 0) { 3099 st->print_cr(" No Blocks"); 3100 } else { 3101 for (int i = 0; i < num_blocks; i++) { 3102 Block* block = blocks->at(i); 3103 block->print_on(st); 3104 } 3105 } 3106 st->print_cr("--------------------------------------------------------"); 3107 st->cr(); 3108 } 3109 } 3110 st->print_cr("********************************************************"); 3111 st->cr(); 3112 } 3113 3114 void ciTypeFlow::rpo_print_on(outputStream* st) const { 3115 st->print_cr("********************************************************"); 3116 st->print ("TypeFlow for "); 3117 method()->name()->print_symbol_on(st); 3118 int limit_bci = code_size(); 3119 st->print_cr(" %d bytes", limit_bci); 3120 for (Block* blk = _rpo_list; blk != NULL; blk = blk->rpo_next()) { 3121 blk->print_on(st); 3122 st->print_cr("--------------------------------------------------------"); 3123 st->cr(); 3124 } 3125 st->print_cr("********************************************************"); 3126 st->cr(); 3127 } 3128 #endif