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