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 BasicType basic_type = str->get_basic_type_for_constant_at(cp_index);
730 if (is_reference_type(basic_type)) {
731 ciObject* obj = con.as_object();
732 if (obj->is_null_object()) {
733 push_null();
734 } else {
735 assert(obj->is_instance() || obj->is_array(), "must be java_mirror of klass");
736 push_object(obj->klass());
737 }
738 } else {
739 assert(basic_type == con.basic_type() || con.basic_type() == T_OBJECT,
740 "not a boxed form: %s vs %s", type2name(basic_type), type2name(con.basic_type()));
741 push_translate(ciType::make(basic_type));
742 }
743 } else {
744 // OutOfMemoryError in the CI while loading a String constant.
745 push_null();
746 outer()->record_failure("ldc did not link");
747 }
748 }
749
750 // ------------------------------------------------------------------
751 // ciTypeFlow::StateVector::do_multianewarray
752 void ciTypeFlow::StateVector::do_multianewarray(ciBytecodeStream* str) {
753 int dimensions = str->get_dimensions();
754 bool will_link;
755 ciArrayKlass* array_klass = str->get_klass(will_link)->as_array_klass();
756 if (!will_link) {
863 // class later.
864 push_null();
865 }
866 }
867
868
869 // ------------------------------------------------------------------
870 // ciTypeFlow::StateVector::apply_one_bytecode
871 //
872 // Apply the effect of one bytecode to this StateVector
873 bool ciTypeFlow::StateVector::apply_one_bytecode(ciBytecodeStream* str) {
874 _trap_bci = -1;
875 _trap_index = 0;
876
877 if (CITraceTypeFlow) {
878 tty->print_cr(">> Interpreting bytecode %d:%s", str->cur_bci(),
879 Bytecodes::name(str->cur_bc()));
880 }
881
882 switch(str->cur_bc()) {
883 case Bytecodes::_aaload: do_aaload(str); break;
884
885 case Bytecodes::_aastore:
886 {
887 pop_object();
888 pop_int();
889 pop_objArray();
890 break;
891 }
892 case Bytecodes::_aconst_null:
893 {
894 push_null();
895 break;
896 }
897 case Bytecodes::_aload: load_local_object(str->get_index()); break;
898 case Bytecodes::_aload_0: load_local_object(0); break;
899 case Bytecodes::_aload_1: load_local_object(1); break;
900 case Bytecodes::_aload_2: load_local_object(2); break;
901 case Bytecodes::_aload_3: load_local_object(3); break;
902
903 case Bytecodes::_anewarray:
904 {
905 pop_int();
906 bool will_link;
907 ciKlass* element_klass = str->get_klass(will_link);
908 if (!will_link) {
909 trap(str, element_klass, str->get_klass_index());
910 } else {
911 push_object(ciObjArrayKlass::make(element_klass));
912 }
913 break;
914 }
915 case Bytecodes::_areturn:
916 case Bytecodes::_ifnonnull:
917 case Bytecodes::_ifnull:
918 {
919 pop_object();
920 break;
921 }
922 case Bytecodes::_monitorenter:
923 {
924 pop_object();
925 set_monitor_count(monitor_count() + 1);
926 break;
927 }
928 case Bytecodes::_monitorexit:
929 {
930 pop_object();
931 assert(monitor_count() > 0, "must be a monitor to exit from");
1450 case Bytecodes::_pop2:
1451 {
1452 pop();
1453 pop();
1454 break;
1455 }
1456
1457 case Bytecodes::_putfield: do_putfield(str); break;
1458 case Bytecodes::_putstatic: do_putstatic(str); break;
1459
1460 case Bytecodes::_ret: do_ret(str); break;
1461
1462 case Bytecodes::_swap:
1463 {
1464 ciType* value1 = pop_value();
1465 ciType* value2 = pop_value();
1466 push(value1);
1467 push(value2);
1468 break;
1469 }
1470 case Bytecodes::_wide:
1471 default:
1472 {
1473 // The iterator should skip this.
1474 ShouldNotReachHere();
1475 break;
1476 }
1477 }
1478
1479 if (CITraceTypeFlow) {
1480 print_on(tty);
1481 }
1482
1483 return (_trap_bci != -1);
1484 }
1485
1486 #ifndef PRODUCT
1487 // ------------------------------------------------------------------
1488 // ciTypeFlow::StateVector::print_cell_on
1489 void ciTypeFlow::StateVector::print_cell_on(outputStream* st, Cell c) const {
1490 ciType* type = type_at(c);
1491 if (type == top_type()) {
1492 st->print("top");
1493 } else if (type == bottom_type()) {
1494 st->print("bottom");
1495 } else if (type == null_type()) {
1496 st->print("null");
1497 } else if (type == long2_type()) {
1498 st->print("long2");
1499 } else if (type == double2_type()) {
1500 st->print("double2");
1501 } else if (is_int(type)) {
1502 st->print("int");
1503 } else if (is_long(type)) {
1504 st->print("long");
1505 } else if (is_float(type)) {
1506 st->print("float");
1507 } else if (is_double(type)) {
1508 st->print("double");
1509 } else if (type->is_return_address()) {
1510 st->print("address(%d)", type->as_return_address()->bci());
1733 case Bytecodes::_lookupswitch: {
1734 Bytecode_lookupswitch lookupswitch(str);
1735
1736 int npairs = lookupswitch.number_of_pairs();
1737 _successors =
1738 new (arena) GrowableArray<Block*>(arena, npairs+1, 0, nullptr);
1739 int bci = current_bci + lookupswitch.default_offset();
1740 Block* block = analyzer->block_at(bci, jsrs);
1741 assert(_successors->length() == SWITCH_DEFAULT, "");
1742 _successors->append(block);
1743 while(--npairs >= 0) {
1744 LookupswitchPair pair = lookupswitch.pair_at(npairs);
1745 int bci = current_bci + pair.offset();
1746 Block* block = analyzer->block_at(bci, jsrs);
1747 assert(_successors->length() >= SWITCH_CASES, "");
1748 _successors->append_if_missing(block);
1749 }
1750 break;
1751 }
1752
1753 case Bytecodes::_athrow: case Bytecodes::_ireturn:
1754 case Bytecodes::_lreturn: case Bytecodes::_freturn:
1755 case Bytecodes::_dreturn: case Bytecodes::_areturn:
1756 case Bytecodes::_return:
1757 _successors =
1758 new (arena) GrowableArray<Block*>(arena, 1, 0, nullptr);
1759 // No successors
1760 break;
1761
1762 case Bytecodes::_ret: {
1763 _successors =
1764 new (arena) GrowableArray<Block*>(arena, 1, 0, nullptr);
1765
1766 Cell local = state->local(str->get_index());
1767 ciType* return_address = state->type_at(local);
1768 assert(return_address->is_return_address(), "verify: wrong type");
1769 int bci = return_address->as_return_address()->bci();
1770 assert(_successors->length() == GOTO_TARGET, "");
1771 _successors->append(analyzer->block_at(bci, jsrs));
1772 break;
1773 }
1774
1775 case Bytecodes::_wide:
3129
3130 // ------------------------------------------------------------------
3131 // ciTypeFlow::record_failure()
3132 // The ciTypeFlow object keeps track of failure reasons separately from the ciEnv.
3133 // This is required because there is not a 1-1 relation between the ciEnv and
3134 // the TypeFlow passes within a compilation task. For example, if the compiler
3135 // is considering inlining a method, it will request a TypeFlow. If that fails,
3136 // the compilation as a whole may continue without the inlining. Some TypeFlow
3137 // requests are not optional; if they fail the requestor is responsible for
3138 // copying the failure reason up to the ciEnv. (See Parse::Parse.)
3139 void ciTypeFlow::record_failure(const char* reason) {
3140 if (env()->log() != nullptr) {
3141 env()->log()->elem("failure reason='%s' phase='typeflow'", reason);
3142 }
3143 if (_failure_reason == nullptr) {
3144 // Record the first failure reason.
3145 _failure_reason = reason;
3146 }
3147 }
3148
3149 #ifndef PRODUCT
3150 void ciTypeFlow::print() const { print_on(tty); }
3151
3152 // ------------------------------------------------------------------
3153 // ciTypeFlow::print_on
3154 void ciTypeFlow::print_on(outputStream* st) const {
3155 // Walk through CI blocks
3156 st->print_cr("********************************************************");
3157 st->print ("TypeFlow for ");
3158 method()->name()->print_symbol_on(st);
3159 int limit_bci = code_size();
3160 st->print_cr(" %d bytes", limit_bci);
3161 ciMethodBlocks* mblks = _method->get_method_blocks();
3162 ciBlock* current = nullptr;
3163 for (int bci = 0; bci < limit_bci; bci++) {
3164 ciBlock* blk = mblks->block_containing(bci);
3165 if (blk != nullptr && blk != current) {
3166 current = blk;
3167 current->print_on(st);
3168
|
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 BasicType basic_type = str->get_basic_type_for_constant_at(cp_index);
781 if (is_reference_type(basic_type)) {
782 ciObject* obj = con.as_object();
783 if (obj->is_null_object()) {
784 push_null();
785 } else {
786 assert(obj->is_instance() || obj->is_array(), "must be java_mirror of klass");
787 ciType* type = obj->klass();
788 if (type->is_inlinetype()) {
789 type = outer()->mark_as_null_free(type);
790 }
791 push(type);
792 }
793 } else {
794 assert(basic_type == con.basic_type() || con.basic_type() == T_OBJECT,
795 "not a boxed form: %s vs %s", type2name(basic_type), type2name(con.basic_type()));
796 push_translate(ciType::make(basic_type));
797 }
798 } else {
799 // OutOfMemoryError in the CI while loading a String constant.
800 push_null();
801 outer()->record_failure("ldc did not link");
802 }
803 }
804
805 // ------------------------------------------------------------------
806 // ciTypeFlow::StateVector::do_multianewarray
807 void ciTypeFlow::StateVector::do_multianewarray(ciBytecodeStream* str) {
808 int dimensions = str->get_dimensions();
809 bool will_link;
810 ciArrayKlass* array_klass = str->get_klass(will_link)->as_array_klass();
811 if (!will_link) {
918 // class later.
919 push_null();
920 }
921 }
922
923
924 // ------------------------------------------------------------------
925 // ciTypeFlow::StateVector::apply_one_bytecode
926 //
927 // Apply the effect of one bytecode to this StateVector
928 bool ciTypeFlow::StateVector::apply_one_bytecode(ciBytecodeStream* str) {
929 _trap_bci = -1;
930 _trap_index = 0;
931
932 if (CITraceTypeFlow) {
933 tty->print_cr(">> Interpreting bytecode %d:%s", str->cur_bci(),
934 Bytecodes::name(str->cur_bc()));
935 }
936
937 switch(str->cur_bc()) {
938 case Bytecodes::_aaload: do_aload(str); break;
939
940 case Bytecodes::_aastore:
941 {
942 pop_object();
943 pop_int();
944 pop_objOrFlatArray();
945 break;
946 }
947 case Bytecodes::_aconst_null:
948 {
949 push_null();
950 break;
951 }
952 case Bytecodes::_aload: load_local_object(str->get_index()); break;
953 case Bytecodes::_aload_0: load_local_object(0); break;
954 case Bytecodes::_aload_1: load_local_object(1); break;
955 case Bytecodes::_aload_2: load_local_object(2); break;
956 case Bytecodes::_aload_3: load_local_object(3); break;
957
958 case Bytecodes::_anewarray:
959 {
960 pop_int();
961 bool will_link;
962 ciKlass* element_klass = str->get_klass(will_link);
963 if (!will_link) {
964 trap(str, element_klass, str->get_klass_index());
965 } else {
966 push_object(ciArrayKlass::make(element_klass));
967 }
968 break;
969 }
970 case Bytecodes::_areturn:
971 case Bytecodes::_ifnonnull:
972 case Bytecodes::_ifnull:
973 {
974 pop_object();
975 break;
976 }
977 case Bytecodes::_monitorenter:
978 {
979 pop_object();
980 set_monitor_count(monitor_count() + 1);
981 break;
982 }
983 case Bytecodes::_monitorexit:
984 {
985 pop_object();
986 assert(monitor_count() > 0, "must be a monitor to exit from");
1505 case Bytecodes::_pop2:
1506 {
1507 pop();
1508 pop();
1509 break;
1510 }
1511
1512 case Bytecodes::_putfield: do_putfield(str); break;
1513 case Bytecodes::_putstatic: do_putstatic(str); break;
1514
1515 case Bytecodes::_ret: do_ret(str); break;
1516
1517 case Bytecodes::_swap:
1518 {
1519 ciType* value1 = pop_value();
1520 ciType* value2 = pop_value();
1521 push(value1);
1522 push(value2);
1523 break;
1524 }
1525
1526 case Bytecodes::_wide:
1527 default:
1528 {
1529 // The iterator should skip this.
1530 ShouldNotReachHere();
1531 break;
1532 }
1533 }
1534
1535 if (CITraceTypeFlow) {
1536 print_on(tty);
1537 }
1538
1539 return (_trap_bci != -1);
1540 }
1541
1542 #ifndef PRODUCT
1543 // ------------------------------------------------------------------
1544 // ciTypeFlow::StateVector::print_cell_on
1545 void ciTypeFlow::StateVector::print_cell_on(outputStream* st, Cell c) const {
1546 ciType* type = type_at(c)->unwrap();
1547 if (type == top_type()) {
1548 st->print("top");
1549 } else if (type == bottom_type()) {
1550 st->print("bottom");
1551 } else if (type == null_type()) {
1552 st->print("null");
1553 } else if (type == long2_type()) {
1554 st->print("long2");
1555 } else if (type == double2_type()) {
1556 st->print("double2");
1557 } else if (is_int(type)) {
1558 st->print("int");
1559 } else if (is_long(type)) {
1560 st->print("long");
1561 } else if (is_float(type)) {
1562 st->print("float");
1563 } else if (is_double(type)) {
1564 st->print("double");
1565 } else if (type->is_return_address()) {
1566 st->print("address(%d)", type->as_return_address()->bci());
1789 case Bytecodes::_lookupswitch: {
1790 Bytecode_lookupswitch lookupswitch(str);
1791
1792 int npairs = lookupswitch.number_of_pairs();
1793 _successors =
1794 new (arena) GrowableArray<Block*>(arena, npairs+1, 0, nullptr);
1795 int bci = current_bci + lookupswitch.default_offset();
1796 Block* block = analyzer->block_at(bci, jsrs);
1797 assert(_successors->length() == SWITCH_DEFAULT, "");
1798 _successors->append(block);
1799 while(--npairs >= 0) {
1800 LookupswitchPair pair = lookupswitch.pair_at(npairs);
1801 int bci = current_bci + pair.offset();
1802 Block* block = analyzer->block_at(bci, jsrs);
1803 assert(_successors->length() >= SWITCH_CASES, "");
1804 _successors->append_if_missing(block);
1805 }
1806 break;
1807 }
1808
1809 case Bytecodes::_athrow:
1810 case Bytecodes::_ireturn:
1811 case Bytecodes::_lreturn:
1812 case Bytecodes::_freturn:
1813 case Bytecodes::_dreturn:
1814 case Bytecodes::_areturn:
1815 case Bytecodes::_return:
1816 _successors =
1817 new (arena) GrowableArray<Block*>(arena, 1, 0, nullptr);
1818 // No successors
1819 break;
1820
1821 case Bytecodes::_ret: {
1822 _successors =
1823 new (arena) GrowableArray<Block*>(arena, 1, 0, nullptr);
1824
1825 Cell local = state->local(str->get_index());
1826 ciType* return_address = state->type_at(local);
1827 assert(return_address->is_return_address(), "verify: wrong type");
1828 int bci = return_address->as_return_address()->bci();
1829 assert(_successors->length() == GOTO_TARGET, "");
1830 _successors->append(analyzer->block_at(bci, jsrs));
1831 break;
1832 }
1833
1834 case Bytecodes::_wide:
3188
3189 // ------------------------------------------------------------------
3190 // ciTypeFlow::record_failure()
3191 // The ciTypeFlow object keeps track of failure reasons separately from the ciEnv.
3192 // This is required because there is not a 1-1 relation between the ciEnv and
3193 // the TypeFlow passes within a compilation task. For example, if the compiler
3194 // is considering inlining a method, it will request a TypeFlow. If that fails,
3195 // the compilation as a whole may continue without the inlining. Some TypeFlow
3196 // requests are not optional; if they fail the requestor is responsible for
3197 // copying the failure reason up to the ciEnv. (See Parse::Parse.)
3198 void ciTypeFlow::record_failure(const char* reason) {
3199 if (env()->log() != nullptr) {
3200 env()->log()->elem("failure reason='%s' phase='typeflow'", reason);
3201 }
3202 if (_failure_reason == nullptr) {
3203 // Record the first failure reason.
3204 _failure_reason = reason;
3205 }
3206 }
3207
3208 ciType* ciTypeFlow::mark_as_null_free(ciType* type) {
3209 // Wrap the type to carry the information that it is null-free
3210 return env()->make_null_free_wrapper(type);
3211 }
3212
3213 #ifndef PRODUCT
3214 void ciTypeFlow::print() const { print_on(tty); }
3215
3216 // ------------------------------------------------------------------
3217 // ciTypeFlow::print_on
3218 void ciTypeFlow::print_on(outputStream* st) const {
3219 // Walk through CI blocks
3220 st->print_cr("********************************************************");
3221 st->print ("TypeFlow for ");
3222 method()->name()->print_symbol_on(st);
3223 int limit_bci = code_size();
3224 st->print_cr(" %d bytes", limit_bci);
3225 ciMethodBlocks* mblks = _method->get_method_blocks();
3226 ciBlock* current = nullptr;
3227 for (int bci = 0; bci < limit_bci; bci++) {
3228 ciBlock* blk = mblks->block_containing(bci);
3229 if (blk != nullptr && blk != current) {
3230 current = blk;
3231 current->print_on(st);
3232
|