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