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