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