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