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