7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #include "ci/ciConstant.hpp"
26 #include "ci/ciField.hpp"
27 #include "ci/ciMethod.hpp"
28 #include "ci/ciMethodData.hpp"
29 #include "ci/ciObjArrayKlass.hpp"
30 #include "ci/ciStreams.hpp"
31 #include "ci/ciTypeArrayKlass.hpp"
32 #include "ci/ciTypeFlow.hpp"
33 #include "compiler/compileLog.hpp"
34 #include "interpreter/bytecode.hpp"
35 #include "interpreter/bytecodes.hpp"
36 #include "memory/allocation.inline.hpp"
37 #include "memory/resourceArea.hpp"
38 #include "oops/oop.inline.hpp"
39 #include "opto/compile.hpp"
40 #include "runtime/deoptimization.hpp"
41 #include "utilities/growableArray.hpp"
42
43 // ciTypeFlow::JsrSet
44 //
45 // A JsrSet represents some set of JsrRecords. This class
46 // is used to record a set of all jsr routines which we permit
255 // ciTypeFlow::StateVector::type_meet
256 //
257 // Meet two types.
258 //
259 // The semi-lattice of types use by this analysis are modeled on those
260 // of the verifier. The lattice is as follows:
261 //
262 // top_type() >= all non-extremal types >= bottom_type
263 // and
264 // Every primitive type is comparable only with itself. The meet of
265 // reference types is determined by their kind: instance class,
266 // interface, or array class. The meet of two types of the same
267 // kind is their least common ancestor. The meet of two types of
268 // different kinds is always java.lang.Object.
269 ciType* ciTypeFlow::StateVector::type_meet_internal(ciType* t1, ciType* t2, ciTypeFlow* analyzer) {
270 assert(t1 != t2, "checked in caller");
271 if (t1->equals(top_type())) {
272 return t2;
273 } else if (t2->equals(top_type())) {
274 return t1;
275 } else if (t1->is_primitive_type() || t2->is_primitive_type()) {
276 // Special case null_type. null_type meet any reference type T
277 // is T. null_type meet null_type is null_type.
278 if (t1->equals(null_type())) {
279 if (!t2->is_primitive_type() || t2->equals(null_type())) {
280 return t2;
281 }
282 } else if (t2->equals(null_type())) {
283 if (!t1->is_primitive_type()) {
284 return t1;
285 }
286 }
287
288 // At least one of the two types is a non-top primitive type.
289 // The other type is not equal to it. Fall to bottom.
290 return bottom_type();
291 } else {
292 // Both types are non-top non-primitive types. That is,
293 // both types are either instanceKlasses or arrayKlasses.
294 ciKlass* object_klass = analyzer->env()->Object_klass();
295 ciKlass* k1 = t1->as_klass();
296 ciKlass* k2 = t2->as_klass();
297 if (k1->equals(object_klass) || k2->equals(object_klass)) {
298 return object_klass;
299 } else if (!k1->is_loaded() || !k2->is_loaded()) {
300 // Unloaded classes fall to java.lang.Object at a merge.
301 return object_klass;
302 } else if (k1->is_interface() != k2->is_interface()) {
303 // When an interface meets a non-interface, we get Object;
304 // This is what the verifier does.
305 return object_klass;
306 } else if (k1->is_array_klass() || k2->is_array_klass()) {
307 // When an array meets a non-array, we get Object.
308 // When objArray meets typeArray, we also get Object.
309 // And when typeArray meets different typeArray, we again get Object.
310 // But when objArray meets objArray, we look carefully at element types.
311 if (k1->is_obj_array_klass() && k2->is_obj_array_klass()) {
312 // Meet the element types, then construct the corresponding array type.
313 ciKlass* elem1 = k1->as_obj_array_klass()->element_klass();
314 ciKlass* elem2 = k2->as_obj_array_klass()->element_klass();
315 ciKlass* elem = type_meet_internal(elem1, elem2, analyzer)->as_klass();
316 // Do an easy shortcut if one type is a super of the other.
317 if (elem == elem1) {
318 assert(k1 == ciObjArrayKlass::make(elem), "shortcut is OK");
319 return k1;
320 } else if (elem == elem2) {
321 assert(k2 == ciObjArrayKlass::make(elem), "shortcut is OK");
322 return k2;
323 } else {
324 return ciObjArrayKlass::make(elem);
325 }
326 } else {
327 return object_klass;
328 }
329 } else {
330 // Must be two plain old instance klasses.
331 assert(k1->is_instance_klass(), "previous cases handle non-instances");
332 assert(k2->is_instance_klass(), "previous cases handle non-instances");
333 return k1->least_common_ancestor(k2);
334 }
335 }
336 }
337
338
339 // ------------------------------------------------------------------
340 // ciTypeFlow::StateVector::StateVector
341 //
342 // Build a new state vector
343 ciTypeFlow::StateVector::StateVector(ciTypeFlow* analyzer) {
344 _outer = analyzer;
345 _stack_size = -1;
346 _monitor_count = -1;
347 // Allocate the _types array
348 int max_cells = analyzer->max_cells();
349 _types = (ciType**)analyzer->arena()->Amalloc(sizeof(ciType*) * max_cells);
350 for (int i=0; i<max_cells; i++) {
351 _types[i] = top_type();
352 }
353 _trap_bci = -1;
354 _trap_index = 0;
376 }
377 // load up the non-OSR state at this point
378 non_osr_block->copy_state_into(state);
379 int non_osr_start = non_osr_block->start();
380 if (non_osr_start != start_bci()) {
381 // must flow forward from it
382 if (CITraceTypeFlow) {
383 tty->print_cr(">> Interpreting pre-OSR block %d:", non_osr_start);
384 }
385 Block* block = block_at(non_osr_start, jsrs);
386 assert(block->limit() == start_bci(), "must flow forward to start");
387 flow_block(block, state, jsrs);
388 }
389 return state;
390 // Note: The code below would be an incorrect for an OSR flow,
391 // even if it were possible for an OSR entry point to be at bci zero.
392 }
393 // "Push" the method signature into the first few locals.
394 state->set_stack_size(-max_locals());
395 if (!method()->is_static()) {
396 state->push(method()->holder());
397 assert(state->tos() == state->local(0), "");
398 }
399 for (ciSignatureStream str(method()->signature());
400 !str.at_return_type();
401 str.next()) {
402 state->push_translate(str.type());
403 }
404 // Set the rest of the locals to bottom.
405 assert(state->stack_size() <= 0, "stack size should not be strictly positive");
406 while (state->stack_size() < 0) {
407 state->push(state->bottom_type());
408 }
409 // Lock an object, if necessary.
410 state->set_monitor_count(method()->is_synchronized() ? 1 : 0);
411 return state;
412 }
413
414 // ------------------------------------------------------------------
415 // ciTypeFlow::StateVector::copy_into
416 //
526
527 return different;
528 }
529
530 // ------------------------------------------------------------------
531 // ciTypeFlow::StateVector::push_translate
532 void ciTypeFlow::StateVector::push_translate(ciType* type) {
533 BasicType basic_type = type->basic_type();
534 if (basic_type == T_BOOLEAN || basic_type == T_CHAR ||
535 basic_type == T_BYTE || basic_type == T_SHORT) {
536 push_int();
537 } else {
538 push(type);
539 if (type->is_two_word()) {
540 push(half_type(type));
541 }
542 }
543 }
544
545 // ------------------------------------------------------------------
546 // ciTypeFlow::StateVector::do_aaload
547 void ciTypeFlow::StateVector::do_aaload(ciBytecodeStream* str) {
548 pop_int();
549 ciObjArrayKlass* array_klass = pop_objArray();
550 if (array_klass == nullptr) {
551 // Did aaload on a null reference; push a null and ignore the exception.
552 // This instruction will never continue normally. All we have to do
553 // is report a value that will meet correctly with any downstream
554 // reference types on paths that will truly be executed. This null type
555 // meets with any reference type to yield that same reference type.
556 // (The compiler will generate an unconditional exception here.)
557 push(null_type());
558 return;
559 }
560 if (!array_klass->is_loaded()) {
561 // Only fails for some -Xcomp runs
562 trap(str, array_klass,
563 Deoptimization::make_trap_request
564 (Deoptimization::Reason_unloaded,
565 Deoptimization::Action_reinterpret));
566 return;
567 }
568 ciKlass* element_klass = array_klass->element_klass();
569 if (!element_klass->is_loaded() && element_klass->is_instance_klass()) {
570 Untested("unloaded array element class in ciTypeFlow");
571 trap(str, element_klass,
572 Deoptimization::make_trap_request
573 (Deoptimization::Reason_unloaded,
574 Deoptimization::Action_reinterpret));
575 } else {
576 push_object(element_klass);
577 }
578 }
579
580
581 // ------------------------------------------------------------------
582 // ciTypeFlow::StateVector::do_checkcast
583 void ciTypeFlow::StateVector::do_checkcast(ciBytecodeStream* str) {
584 bool will_link;
585 ciKlass* klass = str->get_klass(will_link);
586 if (!will_link) {
587 // VM's interpreter will not load 'klass' if object is null.
588 // Type flow after this block may still be needed in two situations:
589 // 1) C2 uses do_null_assert() and continues compilation for later blocks
590 // 2) C2 does an OSR compile in a later block (see bug 4778368).
591 pop_object();
592 do_null_assert(klass);
593 } else {
594 pop_object();
595 push_object(klass);
596 }
597 }
598
599 // ------------------------------------------------------------------
600 // ciTypeFlow::StateVector::do_getfield
601 void ciTypeFlow::StateVector::do_getfield(ciBytecodeStream* str) {
602 // could add assert here for type of object.
603 pop_object();
604 do_getstatic(str);
605 }
606
607 // ------------------------------------------------------------------
608 // ciTypeFlow::StateVector::do_getstatic
609 void ciTypeFlow::StateVector::do_getstatic(ciBytecodeStream* str) {
610 bool will_link;
611 ciField* field = str->get_field(will_link);
612 if (!will_link) {
613 trap(str, field->holder(), str->get_field_holder_index());
614 } else {
615 ciType* field_type = field->type();
616 if (!field_type->is_loaded()) {
617 // Normally, we need the field's type to be loaded if we are to
618 // do anything interesting with its value.
619 // We used to do this: trap(str, str->get_field_signature_index());
620 //
621 // There is one good reason not to trap here. Execution can
622 // get past this "getfield" or "getstatic" if the value of
623 // the field is null. As long as the value is null, the class
624 // does not need to be loaded! The compiler must assume that
625 // the value of the unloaded class reference is null; if the code
626 // ever sees a non-null value, loading has occurred.
627 //
628 // This actually happens often enough to be annoying. If the
629 // compiler throws an uncommon trap at this bytecode, you can
630 // get an endless loop of recompilations, when all the code
631 // needs to do is load a series of null values. Also, a trap
632 // here can make an OSR entry point unreachable, triggering the
633 // assert on non_osr_block in ciTypeFlow::get_start_state.
634 // (See bug 4379915.)
635 do_null_assert(field_type->as_klass());
636 } else {
637 push_translate(field_type);
638 }
639 }
640 }
641
642 // ------------------------------------------------------------------
643 // ciTypeFlow::StateVector::do_invoke
644 void ciTypeFlow::StateVector::do_invoke(ciBytecodeStream* str,
645 bool has_receiver) {
646 bool will_link;
647 ciSignature* declared_signature = nullptr;
648 ciMethod* callee = str->get_method(will_link, &declared_signature);
649 assert(declared_signature != nullptr, "cannot be null");
650 if (!will_link) {
651 // We weren't able to find the method.
652 if (str->cur_bc() == Bytecodes::_invokedynamic) {
653 trap(str, nullptr,
654 Deoptimization::make_trap_request
655 (Deoptimization::Reason_uninitialized,
656 Deoptimization::Action_reinterpret));
682 }
683 if (has_receiver) {
684 // Check this?
685 pop_object();
686 }
687 assert(!sigstr.is_done(), "must have return type");
688 ciType* return_type = sigstr.type();
689 if (!return_type->is_void()) {
690 if (!return_type->is_loaded()) {
691 // As in do_getstatic(), generally speaking, we need the return type to
692 // be loaded if we are to do anything interesting with its value.
693 // We used to do this: trap(str, str->get_method_signature_index());
694 //
695 // We do not trap here since execution can get past this invoke if
696 // the return value is null. As long as the value is null, the class
697 // does not need to be loaded! The compiler must assume that
698 // the value of the unloaded class reference is null; if the code
699 // ever sees a non-null value, loading has occurred.
700 //
701 // See do_getstatic() for similar explanation, as well as bug 4684993.
702 do_null_assert(return_type->as_klass());
703 } else {
704 push_translate(return_type);
705 }
706 }
707 }
708 }
709
710 // ------------------------------------------------------------------
711 // ciTypeFlow::StateVector::do_jsr
712 void ciTypeFlow::StateVector::do_jsr(ciBytecodeStream* str) {
713 push(ciReturnAddress::make(str->next_bci()));
714 }
715
716 // ------------------------------------------------------------------
717 // ciTypeFlow::StateVector::do_ldc
718 void ciTypeFlow::StateVector::do_ldc(ciBytecodeStream* str) {
719 if (str->is_in_error()) {
720 trap(str, nullptr, Deoptimization::make_trap_request(Deoptimization::Reason_unhandled,
721 Deoptimization::Action_none));
722 return;
723 }
724 ciConstant con = str->get_constant();
725 if (con.is_valid()) {
726 int cp_index = str->get_constant_pool_index();
727 if (!con.is_loaded()) {
728 trap(str, nullptr, Deoptimization::make_trap_request(Deoptimization::Reason_unloaded,
729 Deoptimization::Action_reinterpret,
730 cp_index));
731 return;
732 }
733 BasicType basic_type = str->get_basic_type_for_constant_at(cp_index);
734 if (is_reference_type(basic_type)) {
735 ciObject* obj = con.as_object();
736 if (obj->is_null_object()) {
737 push_null();
738 } else {
739 assert(obj->is_instance() || obj->is_array(), "must be java_mirror of klass");
740 push_object(obj->klass());
741 }
742 } else {
743 assert(basic_type == con.basic_type() || con.basic_type() == T_OBJECT,
744 "not a boxed form: %s vs %s", type2name(basic_type), type2name(con.basic_type()));
745 push_translate(ciType::make(basic_type));
746 }
747 } else {
748 // OutOfMemoryError in the CI while loading a String constant.
749 push_null();
750 outer()->record_failure("ldc did not link");
751 }
752 }
753
754 // ------------------------------------------------------------------
755 // ciTypeFlow::StateVector::do_multianewarray
756 void ciTypeFlow::StateVector::do_multianewarray(ciBytecodeStream* str) {
757 int dimensions = str->get_dimensions();
758 bool will_link;
759 ciArrayKlass* array_klass = str->get_klass(will_link)->as_array_klass();
760 if (!will_link) {
867 // class later.
868 push_null();
869 }
870 }
871
872
873 // ------------------------------------------------------------------
874 // ciTypeFlow::StateVector::apply_one_bytecode
875 //
876 // Apply the effect of one bytecode to this StateVector
877 bool ciTypeFlow::StateVector::apply_one_bytecode(ciBytecodeStream* str) {
878 _trap_bci = -1;
879 _trap_index = 0;
880
881 if (CITraceTypeFlow) {
882 tty->print_cr(">> Interpreting bytecode %d:%s", str->cur_bci(),
883 Bytecodes::name(str->cur_bc()));
884 }
885
886 switch(str->cur_bc()) {
887 case Bytecodes::_aaload: do_aaload(str); break;
888
889 case Bytecodes::_aastore:
890 {
891 pop_object();
892 pop_int();
893 pop_objArray();
894 break;
895 }
896 case Bytecodes::_aconst_null:
897 {
898 push_null();
899 break;
900 }
901 case Bytecodes::_aload: load_local_object(str->get_index()); break;
902 case Bytecodes::_aload_0: load_local_object(0); break;
903 case Bytecodes::_aload_1: load_local_object(1); break;
904 case Bytecodes::_aload_2: load_local_object(2); break;
905 case Bytecodes::_aload_3: load_local_object(3); break;
906
907 case Bytecodes::_anewarray:
908 {
909 pop_int();
910 bool will_link;
911 ciKlass* element_klass = str->get_klass(will_link);
912 if (!will_link) {
913 trap(str, element_klass, str->get_klass_index());
914 } else {
915 push_object(ciObjArrayKlass::make(element_klass));
916 }
917 break;
918 }
919 case Bytecodes::_areturn:
920 case Bytecodes::_ifnonnull:
921 case Bytecodes::_ifnull:
922 {
923 pop_object();
924 break;
925 }
926 case Bytecodes::_monitorenter:
927 {
928 pop_object();
929 set_monitor_count(monitor_count() + 1);
930 break;
931 }
932 case Bytecodes::_monitorexit:
933 {
934 pop_object();
935 assert(monitor_count() > 0, "must be a monitor to exit from");
1454 case Bytecodes::_pop2:
1455 {
1456 pop();
1457 pop();
1458 break;
1459 }
1460
1461 case Bytecodes::_putfield: do_putfield(str); break;
1462 case Bytecodes::_putstatic: do_putstatic(str); break;
1463
1464 case Bytecodes::_ret: do_ret(str); break;
1465
1466 case Bytecodes::_swap:
1467 {
1468 ciType* value1 = pop_value();
1469 ciType* value2 = pop_value();
1470 push(value1);
1471 push(value2);
1472 break;
1473 }
1474 case Bytecodes::_wide:
1475 default:
1476 {
1477 // The iterator should skip this.
1478 ShouldNotReachHere();
1479 break;
1480 }
1481 }
1482
1483 if (CITraceTypeFlow) {
1484 print_on(tty);
1485 }
1486
1487 return (_trap_bci != -1);
1488 }
1489
1490 #ifndef PRODUCT
1491 // ------------------------------------------------------------------
1492 // ciTypeFlow::StateVector::print_cell_on
1493 void ciTypeFlow::StateVector::print_cell_on(outputStream* st, Cell c) const {
1494 ciType* type = type_at(c);
1495 if (type == top_type()) {
1496 st->print("top");
1497 } else if (type == bottom_type()) {
1498 st->print("bottom");
1499 } else if (type == null_type()) {
1500 st->print("null");
1501 } else if (type == long2_type()) {
1502 st->print("long2");
1503 } else if (type == double2_type()) {
1504 st->print("double2");
1505 } else if (is_int(type)) {
1506 st->print("int");
1507 } else if (is_long(type)) {
1508 st->print("long");
1509 } else if (is_float(type)) {
1510 st->print("float");
1511 } else if (is_double(type)) {
1512 st->print("double");
1513 } else if (type->is_return_address()) {
1514 st->print("address(%d)", type->as_return_address()->bci());
1737 case Bytecodes::_lookupswitch: {
1738 Bytecode_lookupswitch lookupswitch(str);
1739
1740 int npairs = lookupswitch.number_of_pairs();
1741 _successors =
1742 new (arena) GrowableArray<Block*>(arena, npairs+1, 0, nullptr);
1743 int bci = current_bci + lookupswitch.default_offset();
1744 Block* block = analyzer->block_at(bci, jsrs);
1745 assert(_successors->length() == SWITCH_DEFAULT, "");
1746 _successors->append(block);
1747 while(--npairs >= 0) {
1748 LookupswitchPair pair = lookupswitch.pair_at(npairs);
1749 int bci = current_bci + pair.offset();
1750 Block* block = analyzer->block_at(bci, jsrs);
1751 assert(_successors->length() >= SWITCH_CASES, "");
1752 _successors->append_if_missing(block);
1753 }
1754 break;
1755 }
1756
1757 case Bytecodes::_athrow: case Bytecodes::_ireturn:
1758 case Bytecodes::_lreturn: case Bytecodes::_freturn:
1759 case Bytecodes::_dreturn: case Bytecodes::_areturn:
1760 case Bytecodes::_return:
1761 _successors =
1762 new (arena) GrowableArray<Block*>(arena, 1, 0, nullptr);
1763 // No successors
1764 break;
1765
1766 case Bytecodes::_ret: {
1767 _successors =
1768 new (arena) GrowableArray<Block*>(arena, 1, 0, nullptr);
1769
1770 Cell local = state->local(str->get_index());
1771 ciType* return_address = state->type_at(local);
1772 assert(return_address->is_return_address(), "verify: wrong type");
1773 int bci = return_address->as_return_address()->bci();
1774 assert(_successors->length() == GOTO_TARGET, "");
1775 _successors->append(analyzer->block_at(bci, jsrs));
1776 break;
1777 }
1778
1779 case Bytecodes::_wide:
3132
3133 // ------------------------------------------------------------------
3134 // ciTypeFlow::record_failure()
3135 // The ciTypeFlow object keeps track of failure reasons separately from the ciEnv.
3136 // This is required because there is not a 1-1 relation between the ciEnv and
3137 // the TypeFlow passes within a compilation task. For example, if the compiler
3138 // is considering inlining a method, it will request a TypeFlow. If that fails,
3139 // the compilation as a whole may continue without the inlining. Some TypeFlow
3140 // requests are not optional; if they fail the requestor is responsible for
3141 // copying the failure reason up to the ciEnv. (See Parse::Parse.)
3142 void ciTypeFlow::record_failure(const char* reason) {
3143 if (env()->log() != nullptr) {
3144 env()->log()->elem("failure reason='%s' phase='typeflow'", reason);
3145 }
3146 if (_failure_reason == nullptr) {
3147 // Record the first failure reason.
3148 _failure_reason = reason;
3149 }
3150 }
3151
3152 #ifndef PRODUCT
3153 void ciTypeFlow::print() const { print_on(tty); }
3154
3155 // ------------------------------------------------------------------
3156 // ciTypeFlow::print_on
3157 void ciTypeFlow::print_on(outputStream* st) const {
3158 // Walk through CI blocks
3159 st->print_cr("********************************************************");
3160 st->print ("TypeFlow for ");
3161 method()->name()->print_symbol_on(st);
3162 int limit_bci = code_size();
3163 st->print_cr(" %d bytes", limit_bci);
3164 ciMethodBlocks* mblks = _method->get_method_blocks();
3165 ciBlock* current = nullptr;
3166 for (int bci = 0; bci < limit_bci; bci++) {
3167 ciBlock* blk = mblks->block_containing(bci);
3168 if (blk != nullptr && blk != current) {
3169 current = blk;
3170 current->print_on(st);
3171
|
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #include "ci/ciConstant.hpp"
26 #include "ci/ciField.hpp"
27 #include "ci/ciInlineKlass.hpp"
28 #include "ci/ciMethod.hpp"
29 #include "ci/ciMethodData.hpp"
30 #include "ci/ciObjArrayKlass.hpp"
31 #include "ci/ciStreams.hpp"
32 #include "ci/ciTypeArrayKlass.hpp"
33 #include "ci/ciTypeFlow.hpp"
34 #include "compiler/compileLog.hpp"
35 #include "interpreter/bytecode.hpp"
36 #include "interpreter/bytecodes.hpp"
37 #include "memory/allocation.inline.hpp"
38 #include "memory/resourceArea.hpp"
39 #include "oops/oop.inline.hpp"
40 #include "opto/compile.hpp"
41 #include "runtime/deoptimization.hpp"
42 #include "utilities/growableArray.hpp"
43
44 // ciTypeFlow::JsrSet
45 //
46 // A JsrSet represents some set of JsrRecords. This class
47 // is used to record a set of all jsr routines which we permit
256 // ciTypeFlow::StateVector::type_meet
257 //
258 // Meet two types.
259 //
260 // The semi-lattice of types use by this analysis are modeled on those
261 // of the verifier. The lattice is as follows:
262 //
263 // top_type() >= all non-extremal types >= bottom_type
264 // and
265 // Every primitive type is comparable only with itself. The meet of
266 // reference types is determined by their kind: instance class,
267 // interface, or array class. The meet of two types of the same
268 // kind is their least common ancestor. The meet of two types of
269 // different kinds is always java.lang.Object.
270 ciType* ciTypeFlow::StateVector::type_meet_internal(ciType* t1, ciType* t2, ciTypeFlow* analyzer) {
271 assert(t1 != t2, "checked in caller");
272 if (t1->equals(top_type())) {
273 return t2;
274 } else if (t2->equals(top_type())) {
275 return t1;
276 }
277 // Unwrap after saving nullness information and handling top meets
278 bool null_free1 = t1->is_null_free();
279 bool null_free2 = t2->is_null_free();
280 if (t1->unwrap() == t2->unwrap() && null_free1 == null_free2) {
281 return t1;
282 }
283 t1 = t1->unwrap();
284 t2 = t2->unwrap();
285
286 if (t1->is_primitive_type() || t2->is_primitive_type()) {
287 // Special case null_type. null_type meet any reference type T
288 // is T. null_type meet null_type is null_type.
289 if (t1->equals(null_type())) {
290 if (!t2->is_primitive_type() || t2->equals(null_type())) {
291 return t2;
292 }
293 } else if (t2->equals(null_type())) {
294 if (!t1->is_primitive_type()) {
295 return t1;
296 }
297 }
298
299 // At least one of the two types is a non-top primitive type.
300 // The other type is not equal to it. Fall to bottom.
301 return bottom_type();
302 }
303
304 // Both types are non-top non-primitive types. That is,
305 // both types are either instanceKlasses or arrayKlasses.
306 ciKlass* object_klass = analyzer->env()->Object_klass();
307 ciKlass* k1 = t1->as_klass();
308 ciKlass* k2 = t2->as_klass();
309 if (k1->equals(object_klass) || k2->equals(object_klass)) {
310 return object_klass;
311 } else if (!k1->is_loaded() || !k2->is_loaded()) {
312 // Unloaded classes fall to java.lang.Object at a merge.
313 return object_klass;
314 } else if (k1->is_interface() != k2->is_interface()) {
315 // When an interface meets a non-interface, we get Object;
316 // This is what the verifier does.
317 return object_klass;
318 } else if (k1->is_array_klass() || k2->is_array_klass()) {
319 // When an array meets a non-array, we get Object.
320 // When (obj/flat)Array meets typeArray, we also get Object.
321 // And when typeArray meets different typeArray, we again get Object.
322 // But when (obj/flat)Array meets (obj/flat)Array, we look carefully at element types.
323 if ((k1->is_obj_array_klass() || k1->is_flat_array_klass()) &&
324 (k2->is_obj_array_klass() || k2->is_flat_array_klass())) {
325 ciType* elem1 = k1->as_array_klass()->element_klass();
326 ciType* elem2 = k2->as_array_klass()->element_klass();
327 ciType* elem = elem1;
328 if (elem1 != elem2) {
329 elem = type_meet_internal(elem1, elem2, analyzer)->as_klass();
330 }
331 // Do an easy shortcut if one type is a super of the other.
332 if (elem == elem1 && !elem->is_inlinetype()) {
333 assert(k1 == ciArrayKlass::make(elem), "shortcut is OK");
334 return k1;
335 } else if (elem == elem2 && !elem->is_inlinetype()) {
336 assert(k2 == ciArrayKlass::make(elem), "shortcut is OK");
337 return k2;
338 } else {
339 return ciArrayKlass::make(elem);
340 }
341 } else {
342 return object_klass;
343 }
344 } else {
345 // Must be two plain old instance klasses.
346 assert(k1->is_instance_klass(), "previous cases handle non-instances");
347 assert(k2->is_instance_klass(), "previous cases handle non-instances");
348 ciType* result = k1->least_common_ancestor(k2);
349 if (null_free1 && null_free2 && result->is_inlinetype()) {
350 result = analyzer->mark_as_null_free(result);
351 }
352 return result;
353 }
354 }
355
356
357 // ------------------------------------------------------------------
358 // ciTypeFlow::StateVector::StateVector
359 //
360 // Build a new state vector
361 ciTypeFlow::StateVector::StateVector(ciTypeFlow* analyzer) {
362 _outer = analyzer;
363 _stack_size = -1;
364 _monitor_count = -1;
365 // Allocate the _types array
366 int max_cells = analyzer->max_cells();
367 _types = (ciType**)analyzer->arena()->Amalloc(sizeof(ciType*) * max_cells);
368 for (int i=0; i<max_cells; i++) {
369 _types[i] = top_type();
370 }
371 _trap_bci = -1;
372 _trap_index = 0;
394 }
395 // load up the non-OSR state at this point
396 non_osr_block->copy_state_into(state);
397 int non_osr_start = non_osr_block->start();
398 if (non_osr_start != start_bci()) {
399 // must flow forward from it
400 if (CITraceTypeFlow) {
401 tty->print_cr(">> Interpreting pre-OSR block %d:", non_osr_start);
402 }
403 Block* block = block_at(non_osr_start, jsrs);
404 assert(block->limit() == start_bci(), "must flow forward to start");
405 flow_block(block, state, jsrs);
406 }
407 return state;
408 // Note: The code below would be an incorrect for an OSR flow,
409 // even if it were possible for an OSR entry point to be at bci zero.
410 }
411 // "Push" the method signature into the first few locals.
412 state->set_stack_size(-max_locals());
413 if (!method()->is_static()) {
414 ciType* holder = method()->holder();
415 if (holder->is_inlinetype()) {
416 // The receiver is null-free
417 holder = mark_as_null_free(holder);
418 }
419 state->push(holder);
420 assert(state->tos() == state->local(0), "");
421 }
422 for (ciSignatureStream str(method()->signature());
423 !str.at_return_type();
424 str.next()) {
425 state->push_translate(str.type());
426 }
427 // Set the rest of the locals to bottom.
428 assert(state->stack_size() <= 0, "stack size should not be strictly positive");
429 while (state->stack_size() < 0) {
430 state->push(state->bottom_type());
431 }
432 // Lock an object, if necessary.
433 state->set_monitor_count(method()->is_synchronized() ? 1 : 0);
434 return state;
435 }
436
437 // ------------------------------------------------------------------
438 // ciTypeFlow::StateVector::copy_into
439 //
549
550 return different;
551 }
552
553 // ------------------------------------------------------------------
554 // ciTypeFlow::StateVector::push_translate
555 void ciTypeFlow::StateVector::push_translate(ciType* type) {
556 BasicType basic_type = type->basic_type();
557 if (basic_type == T_BOOLEAN || basic_type == T_CHAR ||
558 basic_type == T_BYTE || basic_type == T_SHORT) {
559 push_int();
560 } else {
561 push(type);
562 if (type->is_two_word()) {
563 push(half_type(type));
564 }
565 }
566 }
567
568 // ------------------------------------------------------------------
569 // ciTypeFlow::StateVector::do_aload
570 void ciTypeFlow::StateVector::do_aload(ciBytecodeStream* str) {
571 pop_int();
572 ciArrayKlass* array_klass = pop_objOrFlatArray();
573 if (array_klass == nullptr) {
574 // Did aload on a null reference; push a null and ignore the exception.
575 // This instruction will never continue normally. All we have to do
576 // is report a value that will meet correctly with any downstream
577 // reference types on paths that will truly be executed. This null type
578 // meets with any reference type to yield that same reference type.
579 // (The compiler will generate an unconditional exception here.)
580 push(null_type());
581 return;
582 }
583 if (!array_klass->is_loaded()) {
584 // Only fails for some -Xcomp runs
585 trap(str, array_klass,
586 Deoptimization::make_trap_request
587 (Deoptimization::Reason_unloaded,
588 Deoptimization::Action_reinterpret));
589 return;
590 }
591 ciKlass* element_klass = array_klass->element_klass();
592 if (!element_klass->is_loaded() && element_klass->is_instance_klass()) {
593 Untested("unloaded array element class in ciTypeFlow");
594 trap(str, element_klass,
595 Deoptimization::make_trap_request
596 (Deoptimization::Reason_unloaded,
597 Deoptimization::Action_reinterpret));
598 } else {
599 push_object(element_klass);
600 }
601 }
602
603
604 // ------------------------------------------------------------------
605 // ciTypeFlow::StateVector::do_checkcast
606 void ciTypeFlow::StateVector::do_checkcast(ciBytecodeStream* str) {
607 bool will_link;
608 ciKlass* klass = str->get_klass(will_link);
609 if (!will_link) {
610 // VM's interpreter will not load 'klass' if object is nullptr.
611 // Type flow after this block may still be needed in two situations:
612 // 1) C2 uses do_null_assert() and continues compilation for later blocks
613 // 2) C2 does an OSR compile in a later block (see bug 4778368).
614 pop_object();
615 do_null_assert(klass);
616 } else {
617 ciType* type = pop_value();
618 type = type->unwrap();
619 if (type->is_loaded() && klass->is_loaded() &&
620 type != klass && type->is_subtype_of(klass)) {
621 // Useless cast, propagate more precise type of object
622 klass = type->as_klass();
623 }
624 push_object(klass);
625 }
626 }
627
628 // ------------------------------------------------------------------
629 // ciTypeFlow::StateVector::do_getfield
630 void ciTypeFlow::StateVector::do_getfield(ciBytecodeStream* str) {
631 // could add assert here for type of object.
632 pop_object();
633 do_getstatic(str);
634 }
635
636 // ------------------------------------------------------------------
637 // ciTypeFlow::StateVector::do_getstatic
638 void ciTypeFlow::StateVector::do_getstatic(ciBytecodeStream* str) {
639 bool will_link;
640 ciField* field = str->get_field(will_link);
641 if (!will_link) {
642 trap(str, field->holder(), str->get_field_holder_index());
643 } else {
644 ciType* field_type = field->type();
645 if (field->is_static() && field->is_null_free() &&
646 !field_type->as_instance_klass()->is_initialized()) {
647 // Deoptimize if we load from a static field with an uninitialized inline type
648 // because we need to throw an exception if initialization of the type failed.
649 trap(str, field_type->as_klass(),
650 Deoptimization::make_trap_request
651 (Deoptimization::Reason_unloaded,
652 Deoptimization::Action_reinterpret));
653 return;
654 } else if (!field_type->is_loaded()) {
655 // Normally, we need the field's type to be loaded if we are to
656 // do anything interesting with its value.
657 // We used to do this: trap(str, str->get_field_signature_index());
658 //
659 // There is one good reason not to trap here. Execution can
660 // get past this "getfield" or "getstatic" if the value of
661 // the field is null. As long as the value is null, the class
662 // does not need to be loaded! The compiler must assume that
663 // the value of the unloaded class reference is null; if the code
664 // ever sees a non-null value, loading has occurred.
665 //
666 // This actually happens often enough to be annoying. If the
667 // compiler throws an uncommon trap at this bytecode, you can
668 // get an endless loop of recompilations, when all the code
669 // needs to do is load a series of null values. Also, a trap
670 // here can make an OSR entry point unreachable, triggering the
671 // assert on non_osr_block in ciTypeFlow::get_start_state.
672 // (See bug 4379915.)
673 do_null_assert(field_type->as_klass());
674 } else {
675 if (field->is_null_free()) {
676 field_type = outer()->mark_as_null_free(field_type);
677 }
678 push_translate(field_type);
679 }
680 }
681 }
682
683 // ------------------------------------------------------------------
684 // ciTypeFlow::StateVector::do_invoke
685 void ciTypeFlow::StateVector::do_invoke(ciBytecodeStream* str,
686 bool has_receiver) {
687 bool will_link;
688 ciSignature* declared_signature = nullptr;
689 ciMethod* callee = str->get_method(will_link, &declared_signature);
690 assert(declared_signature != nullptr, "cannot be null");
691 if (!will_link) {
692 // We weren't able to find the method.
693 if (str->cur_bc() == Bytecodes::_invokedynamic) {
694 trap(str, nullptr,
695 Deoptimization::make_trap_request
696 (Deoptimization::Reason_uninitialized,
697 Deoptimization::Action_reinterpret));
723 }
724 if (has_receiver) {
725 // Check this?
726 pop_object();
727 }
728 assert(!sigstr.is_done(), "must have return type");
729 ciType* return_type = sigstr.type();
730 if (!return_type->is_void()) {
731 if (!return_type->is_loaded()) {
732 // As in do_getstatic(), generally speaking, we need the return type to
733 // be loaded if we are to do anything interesting with its value.
734 // We used to do this: trap(str, str->get_method_signature_index());
735 //
736 // We do not trap here since execution can get past this invoke if
737 // the return value is null. As long as the value is null, the class
738 // does not need to be loaded! The compiler must assume that
739 // the value of the unloaded class reference is null; if the code
740 // ever sees a non-null value, loading has occurred.
741 //
742 // See do_getstatic() for similar explanation, as well as bug 4684993.
743 if (InlineTypeReturnedAsFields) {
744 // Return might be in scalarized form but we can't handle it because we
745 // don't know the type. This can happen due to a missing preload attribute.
746 // TODO 8284443 Use PhaseMacroExpand::expand_mh_intrinsic_return for this
747 trap(str, nullptr,
748 Deoptimization::make_trap_request
749 (Deoptimization::Reason_uninitialized,
750 Deoptimization::Action_reinterpret));
751 } else {
752 do_null_assert(return_type->as_klass());
753 }
754 } else {
755 push_translate(return_type);
756 }
757 }
758 }
759 }
760
761 // ------------------------------------------------------------------
762 // ciTypeFlow::StateVector::do_jsr
763 void ciTypeFlow::StateVector::do_jsr(ciBytecodeStream* str) {
764 push(ciReturnAddress::make(str->next_bci()));
765 }
766
767 // ------------------------------------------------------------------
768 // ciTypeFlow::StateVector::do_ldc
769 void ciTypeFlow::StateVector::do_ldc(ciBytecodeStream* str) {
770 if (str->is_in_error()) {
771 trap(str, nullptr, Deoptimization::make_trap_request(Deoptimization::Reason_unhandled,
772 Deoptimization::Action_none));
773 return;
774 }
775 ciConstant con = str->get_constant();
776 if (con.is_valid()) {
777 int cp_index = str->get_constant_pool_index();
778 if (!con.is_loaded()) {
779 trap(str, nullptr, Deoptimization::make_trap_request(Deoptimization::Reason_unloaded,
780 Deoptimization::Action_reinterpret,
781 cp_index));
782 return;
783 }
784 BasicType basic_type = str->get_basic_type_for_constant_at(cp_index);
785 if (is_reference_type(basic_type)) {
786 ciObject* obj = con.as_object();
787 if (obj->is_null_object()) {
788 push_null();
789 } else {
790 assert(obj->is_instance() || obj->is_array(), "must be java_mirror of klass");
791 ciType* type = obj->klass();
792 if (type->is_inlinetype()) {
793 type = outer()->mark_as_null_free(type);
794 }
795 push(type);
796 }
797 } else {
798 assert(basic_type == con.basic_type() || con.basic_type() == T_OBJECT,
799 "not a boxed form: %s vs %s", type2name(basic_type), type2name(con.basic_type()));
800 push_translate(ciType::make(basic_type));
801 }
802 } else {
803 // OutOfMemoryError in the CI while loading a String constant.
804 push_null();
805 outer()->record_failure("ldc did not link");
806 }
807 }
808
809 // ------------------------------------------------------------------
810 // ciTypeFlow::StateVector::do_multianewarray
811 void ciTypeFlow::StateVector::do_multianewarray(ciBytecodeStream* str) {
812 int dimensions = str->get_dimensions();
813 bool will_link;
814 ciArrayKlass* array_klass = str->get_klass(will_link)->as_array_klass();
815 if (!will_link) {
922 // class later.
923 push_null();
924 }
925 }
926
927
928 // ------------------------------------------------------------------
929 // ciTypeFlow::StateVector::apply_one_bytecode
930 //
931 // Apply the effect of one bytecode to this StateVector
932 bool ciTypeFlow::StateVector::apply_one_bytecode(ciBytecodeStream* str) {
933 _trap_bci = -1;
934 _trap_index = 0;
935
936 if (CITraceTypeFlow) {
937 tty->print_cr(">> Interpreting bytecode %d:%s", str->cur_bci(),
938 Bytecodes::name(str->cur_bc()));
939 }
940
941 switch(str->cur_bc()) {
942 case Bytecodes::_aaload: do_aload(str); break;
943
944 case Bytecodes::_aastore:
945 {
946 pop_object();
947 pop_int();
948 pop_objOrFlatArray();
949 break;
950 }
951 case Bytecodes::_aconst_null:
952 {
953 push_null();
954 break;
955 }
956 case Bytecodes::_aload: load_local_object(str->get_index()); break;
957 case Bytecodes::_aload_0: load_local_object(0); break;
958 case Bytecodes::_aload_1: load_local_object(1); break;
959 case Bytecodes::_aload_2: load_local_object(2); break;
960 case Bytecodes::_aload_3: load_local_object(3); break;
961
962 case Bytecodes::_anewarray:
963 {
964 pop_int();
965 bool will_link;
966 ciKlass* element_klass = str->get_klass(will_link);
967 if (!will_link) {
968 trap(str, element_klass, str->get_klass_index());
969 } else {
970 push_object(ciArrayKlass::make(element_klass));
971 }
972 break;
973 }
974 case Bytecodes::_areturn:
975 case Bytecodes::_ifnonnull:
976 case Bytecodes::_ifnull:
977 {
978 pop_object();
979 break;
980 }
981 case Bytecodes::_monitorenter:
982 {
983 pop_object();
984 set_monitor_count(monitor_count() + 1);
985 break;
986 }
987 case Bytecodes::_monitorexit:
988 {
989 pop_object();
990 assert(monitor_count() > 0, "must be a monitor to exit from");
1509 case Bytecodes::_pop2:
1510 {
1511 pop();
1512 pop();
1513 break;
1514 }
1515
1516 case Bytecodes::_putfield: do_putfield(str); break;
1517 case Bytecodes::_putstatic: do_putstatic(str); break;
1518
1519 case Bytecodes::_ret: do_ret(str); break;
1520
1521 case Bytecodes::_swap:
1522 {
1523 ciType* value1 = pop_value();
1524 ciType* value2 = pop_value();
1525 push(value1);
1526 push(value2);
1527 break;
1528 }
1529
1530 case Bytecodes::_wide:
1531 default:
1532 {
1533 // The iterator should skip this.
1534 ShouldNotReachHere();
1535 break;
1536 }
1537 }
1538
1539 if (CITraceTypeFlow) {
1540 print_on(tty);
1541 }
1542
1543 return (_trap_bci != -1);
1544 }
1545
1546 #ifndef PRODUCT
1547 // ------------------------------------------------------------------
1548 // ciTypeFlow::StateVector::print_cell_on
1549 void ciTypeFlow::StateVector::print_cell_on(outputStream* st, Cell c) const {
1550 ciType* type = type_at(c)->unwrap();
1551 if (type == top_type()) {
1552 st->print("top");
1553 } else if (type == bottom_type()) {
1554 st->print("bottom");
1555 } else if (type == null_type()) {
1556 st->print("null");
1557 } else if (type == long2_type()) {
1558 st->print("long2");
1559 } else if (type == double2_type()) {
1560 st->print("double2");
1561 } else if (is_int(type)) {
1562 st->print("int");
1563 } else if (is_long(type)) {
1564 st->print("long");
1565 } else if (is_float(type)) {
1566 st->print("float");
1567 } else if (is_double(type)) {
1568 st->print("double");
1569 } else if (type->is_return_address()) {
1570 st->print("address(%d)", type->as_return_address()->bci());
1793 case Bytecodes::_lookupswitch: {
1794 Bytecode_lookupswitch lookupswitch(str);
1795
1796 int npairs = lookupswitch.number_of_pairs();
1797 _successors =
1798 new (arena) GrowableArray<Block*>(arena, npairs+1, 0, nullptr);
1799 int bci = current_bci + lookupswitch.default_offset();
1800 Block* block = analyzer->block_at(bci, jsrs);
1801 assert(_successors->length() == SWITCH_DEFAULT, "");
1802 _successors->append(block);
1803 while(--npairs >= 0) {
1804 LookupswitchPair pair = lookupswitch.pair_at(npairs);
1805 int bci = current_bci + pair.offset();
1806 Block* block = analyzer->block_at(bci, jsrs);
1807 assert(_successors->length() >= SWITCH_CASES, "");
1808 _successors->append_if_missing(block);
1809 }
1810 break;
1811 }
1812
1813 case Bytecodes::_athrow:
1814 case Bytecodes::_ireturn:
1815 case Bytecodes::_lreturn:
1816 case Bytecodes::_freturn:
1817 case Bytecodes::_dreturn:
1818 case Bytecodes::_areturn:
1819 case Bytecodes::_return:
1820 _successors =
1821 new (arena) GrowableArray<Block*>(arena, 1, 0, nullptr);
1822 // No successors
1823 break;
1824
1825 case Bytecodes::_ret: {
1826 _successors =
1827 new (arena) GrowableArray<Block*>(arena, 1, 0, nullptr);
1828
1829 Cell local = state->local(str->get_index());
1830 ciType* return_address = state->type_at(local);
1831 assert(return_address->is_return_address(), "verify: wrong type");
1832 int bci = return_address->as_return_address()->bci();
1833 assert(_successors->length() == GOTO_TARGET, "");
1834 _successors->append(analyzer->block_at(bci, jsrs));
1835 break;
1836 }
1837
1838 case Bytecodes::_wide:
3191
3192 // ------------------------------------------------------------------
3193 // ciTypeFlow::record_failure()
3194 // The ciTypeFlow object keeps track of failure reasons separately from the ciEnv.
3195 // This is required because there is not a 1-1 relation between the ciEnv and
3196 // the TypeFlow passes within a compilation task. For example, if the compiler
3197 // is considering inlining a method, it will request a TypeFlow. If that fails,
3198 // the compilation as a whole may continue without the inlining. Some TypeFlow
3199 // requests are not optional; if they fail the requestor is responsible for
3200 // copying the failure reason up to the ciEnv. (See Parse::Parse.)
3201 void ciTypeFlow::record_failure(const char* reason) {
3202 if (env()->log() != nullptr) {
3203 env()->log()->elem("failure reason='%s' phase='typeflow'", reason);
3204 }
3205 if (_failure_reason == nullptr) {
3206 // Record the first failure reason.
3207 _failure_reason = reason;
3208 }
3209 }
3210
3211 ciType* ciTypeFlow::mark_as_null_free(ciType* type) {
3212 // Wrap the type to carry the information that it is null-free
3213 return env()->make_null_free_wrapper(type);
3214 }
3215
3216 #ifndef PRODUCT
3217 void ciTypeFlow::print() const { print_on(tty); }
3218
3219 // ------------------------------------------------------------------
3220 // ciTypeFlow::print_on
3221 void ciTypeFlow::print_on(outputStream* st) const {
3222 // Walk through CI blocks
3223 st->print_cr("********************************************************");
3224 st->print ("TypeFlow for ");
3225 method()->name()->print_symbol_on(st);
3226 int limit_bci = code_size();
3227 st->print_cr(" %d bytes", limit_bci);
3228 ciMethodBlocks* mblks = _method->get_method_blocks();
3229 ciBlock* current = nullptr;
3230 for (int bci = 0; bci < limit_bci; bci++) {
3231 ciBlock* blk = mblks->block_containing(bci);
3232 if (blk != nullptr && blk != current) {
3233 current = blk;
3234 current->print_on(st);
3235
|