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 Cell cell = state->next_cell(state->tos());
408 state->set_stack_size(0);
409 int limit = state->limit_cell();
410 for (; cell < limit; cell = state->next_cell(cell)) {
411 state->set_type_at(cell, state->bottom_type());
412 }
413 // Lock an object, if necessary.
414 state->set_monitor_count(method()->is_synchronized() ? 1 : 0);
415 return state;
416 }
417
418 // ------------------------------------------------------------------
530
531 return different;
532 }
533
534 // ------------------------------------------------------------------
535 // ciTypeFlow::StateVector::push_translate
536 void ciTypeFlow::StateVector::push_translate(ciType* type) {
537 BasicType basic_type = type->basic_type();
538 if (basic_type == T_BOOLEAN || basic_type == T_CHAR ||
539 basic_type == T_BYTE || basic_type == T_SHORT) {
540 push_int();
541 } else {
542 push(type);
543 if (type->is_two_word()) {
544 push(half_type(type));
545 }
546 }
547 }
548
549 // ------------------------------------------------------------------
550 // ciTypeFlow::StateVector::do_aaload
551 void ciTypeFlow::StateVector::do_aaload(ciBytecodeStream* str) {
552 pop_int();
553 ciObjArrayKlass* array_klass = pop_objArray();
554 if (array_klass == nullptr) {
555 // Did aaload on a null reference; push a null and ignore the exception.
556 // This instruction will never continue normally. All we have to do
557 // is report a value that will meet correctly with any downstream
558 // reference types on paths that will truly be executed. This null type
559 // meets with any reference type to yield that same reference type.
560 // (The compiler will generate an unconditional exception here.)
561 push(null_type());
562 return;
563 }
564 if (!array_klass->is_loaded()) {
565 // Only fails for some -Xcomp runs
566 trap(str, array_klass,
567 Deoptimization::make_trap_request
568 (Deoptimization::Reason_unloaded,
569 Deoptimization::Action_reinterpret));
570 return;
571 }
572 ciKlass* element_klass = array_klass->element_klass();
573 if (!element_klass->is_loaded() && element_klass->is_instance_klass()) {
574 Untested("unloaded array element class in ciTypeFlow");
575 trap(str, element_klass,
576 Deoptimization::make_trap_request
577 (Deoptimization::Reason_unloaded,
578 Deoptimization::Action_reinterpret));
579 } else {
580 push_object(element_klass);
581 }
582 }
583
584
585 // ------------------------------------------------------------------
586 // ciTypeFlow::StateVector::do_checkcast
587 void ciTypeFlow::StateVector::do_checkcast(ciBytecodeStream* str) {
588 bool will_link;
589 ciKlass* klass = str->get_klass(will_link);
590 if (!will_link) {
591 // VM's interpreter will not load 'klass' if object is null.
592 // Type flow after this block may still be needed in two situations:
593 // 1) C2 uses do_null_assert() and continues compilation for later blocks
594 // 2) C2 does an OSR compile in a later block (see bug 4778368).
595 pop_object();
596 do_null_assert(klass);
597 } else {
598 pop_object();
599 push_object(klass);
600 }
601 }
602
603 // ------------------------------------------------------------------
604 // ciTypeFlow::StateVector::do_getfield
605 void ciTypeFlow::StateVector::do_getfield(ciBytecodeStream* str) {
606 // could add assert here for type of object.
607 pop_object();
608 do_getstatic(str);
609 }
610
611 // ------------------------------------------------------------------
612 // ciTypeFlow::StateVector::do_getstatic
613 void ciTypeFlow::StateVector::do_getstatic(ciBytecodeStream* str) {
614 bool will_link;
615 ciField* field = str->get_field(will_link);
616 if (!will_link) {
617 trap(str, field->holder(), str->get_field_holder_index());
618 } else {
619 ciType* field_type = field->type();
620 if (!field_type->is_loaded()) {
621 // Normally, we need the field's type to be loaded if we are to
622 // do anything interesting with its value.
623 // We used to do this: trap(str, str->get_field_signature_index());
624 //
625 // There is one good reason not to trap here. Execution can
626 // get past this "getfield" or "getstatic" if the value of
627 // the field is null. As long as the value is null, the class
628 // does not need to be loaded! The compiler must assume that
629 // the value of the unloaded class reference is null; if the code
630 // ever sees a non-null value, loading has occurred.
631 //
632 // This actually happens often enough to be annoying. If the
633 // compiler throws an uncommon trap at this bytecode, you can
634 // get an endless loop of recompilations, when all the code
635 // needs to do is load a series of null values. Also, a trap
636 // here can make an OSR entry point unreachable, triggering the
637 // assert on non_osr_block in ciTypeFlow::get_start_state.
638 // (See bug 4379915.)
639 do_null_assert(field_type->as_klass());
640 } else {
641 push_translate(field_type);
642 }
643 }
644 }
645
646 // ------------------------------------------------------------------
647 // ciTypeFlow::StateVector::do_invoke
648 void ciTypeFlow::StateVector::do_invoke(ciBytecodeStream* str,
649 bool has_receiver) {
650 bool will_link;
651 ciSignature* declared_signature = nullptr;
652 ciMethod* callee = str->get_method(will_link, &declared_signature);
653 assert(declared_signature != nullptr, "cannot be null");
654 if (!will_link) {
655 // We weren't able to find the method.
656 if (str->cur_bc() == Bytecodes::_invokedynamic) {
657 trap(str, nullptr,
658 Deoptimization::make_trap_request
659 (Deoptimization::Reason_uninitialized,
660 Deoptimization::Action_reinterpret));
686 }
687 if (has_receiver) {
688 // Check this?
689 pop_object();
690 }
691 assert(!sigstr.is_done(), "must have return type");
692 ciType* return_type = sigstr.type();
693 if (!return_type->is_void()) {
694 if (!return_type->is_loaded()) {
695 // As in do_getstatic(), generally speaking, we need the return type to
696 // be loaded if we are to do anything interesting with its value.
697 // We used to do this: trap(str, str->get_method_signature_index());
698 //
699 // We do not trap here since execution can get past this invoke if
700 // the return value is null. As long as the value is null, the class
701 // does not need to be loaded! The compiler must assume that
702 // the value of the unloaded class reference is null; if the code
703 // ever sees a non-null value, loading has occurred.
704 //
705 // See do_getstatic() for similar explanation, as well as bug 4684993.
706 do_null_assert(return_type->as_klass());
707 } else {
708 push_translate(return_type);
709 }
710 }
711 }
712 }
713
714 // ------------------------------------------------------------------
715 // ciTypeFlow::StateVector::do_jsr
716 void ciTypeFlow::StateVector::do_jsr(ciBytecodeStream* str) {
717 push(ciReturnAddress::make(str->next_bci()));
718 }
719
720 // ------------------------------------------------------------------
721 // ciTypeFlow::StateVector::do_ldc
722 void ciTypeFlow::StateVector::do_ldc(ciBytecodeStream* str) {
723 if (str->is_in_error()) {
724 trap(str, nullptr, Deoptimization::make_trap_request(Deoptimization::Reason_unhandled,
725 Deoptimization::Action_none));
726 return;
727 }
728 ciConstant con = str->get_constant();
729 if (con.is_valid()) {
730 int cp_index = str->get_constant_pool_index();
731 BasicType basic_type = str->get_basic_type_for_constant_at(cp_index);
732 if (is_reference_type(basic_type)) {
733 ciObject* obj = con.as_object();
734 if (obj->is_null_object()) {
735 push_null();
736 } else {
737 assert(obj->is_instance() || obj->is_array(), "must be java_mirror of klass");
738 push_object(obj->klass());
739 }
740 } else {
741 assert(basic_type == con.basic_type() || con.basic_type() == T_OBJECT,
742 "not a boxed form: %s vs %s", type2name(basic_type), type2name(con.basic_type()));
743 push_translate(ciType::make(basic_type));
744 }
745 } else {
746 // OutOfMemoryError in the CI while loading a String constant.
747 push_null();
748 outer()->record_failure("ldc did not link");
749 }
750 }
751
752 // ------------------------------------------------------------------
753 // ciTypeFlow::StateVector::do_multianewarray
754 void ciTypeFlow::StateVector::do_multianewarray(ciBytecodeStream* str) {
755 int dimensions = str->get_dimensions();
756 bool will_link;
757 ciArrayKlass* array_klass = str->get_klass(will_link)->as_array_klass();
758 if (!will_link) {
865 // class later.
866 push_null();
867 }
868 }
869
870
871 // ------------------------------------------------------------------
872 // ciTypeFlow::StateVector::apply_one_bytecode
873 //
874 // Apply the effect of one bytecode to this StateVector
875 bool ciTypeFlow::StateVector::apply_one_bytecode(ciBytecodeStream* str) {
876 _trap_bci = -1;
877 _trap_index = 0;
878
879 if (CITraceTypeFlow) {
880 tty->print_cr(">> Interpreting bytecode %d:%s", str->cur_bci(),
881 Bytecodes::name(str->cur_bc()));
882 }
883
884 switch(str->cur_bc()) {
885 case Bytecodes::_aaload: do_aaload(str); break;
886
887 case Bytecodes::_aastore:
888 {
889 pop_object();
890 pop_int();
891 pop_objArray();
892 break;
893 }
894 case Bytecodes::_aconst_null:
895 {
896 push_null();
897 break;
898 }
899 case Bytecodes::_aload: load_local_object(str->get_index()); break;
900 case Bytecodes::_aload_0: load_local_object(0); break;
901 case Bytecodes::_aload_1: load_local_object(1); break;
902 case Bytecodes::_aload_2: load_local_object(2); break;
903 case Bytecodes::_aload_3: load_local_object(3); break;
904
905 case Bytecodes::_anewarray:
906 {
907 pop_int();
908 bool will_link;
909 ciKlass* element_klass = str->get_klass(will_link);
910 if (!will_link) {
911 trap(str, element_klass, str->get_klass_index());
912 } else {
913 push_object(ciObjArrayKlass::make(element_klass));
914 }
915 break;
916 }
917 case Bytecodes::_areturn:
918 case Bytecodes::_ifnonnull:
919 case Bytecodes::_ifnull:
920 {
921 pop_object();
922 break;
923 }
924 case Bytecodes::_monitorenter:
925 {
926 pop_object();
927 set_monitor_count(monitor_count() + 1);
928 break;
929 }
930 case Bytecodes::_monitorexit:
931 {
932 pop_object();
933 assert(monitor_count() > 0, "must be a monitor to exit from");
1452 case Bytecodes::_pop2:
1453 {
1454 pop();
1455 pop();
1456 break;
1457 }
1458
1459 case Bytecodes::_putfield: do_putfield(str); break;
1460 case Bytecodes::_putstatic: do_putstatic(str); break;
1461
1462 case Bytecodes::_ret: do_ret(str); break;
1463
1464 case Bytecodes::_swap:
1465 {
1466 ciType* value1 = pop_value();
1467 ciType* value2 = pop_value();
1468 push(value1);
1469 push(value2);
1470 break;
1471 }
1472 case Bytecodes::_wide:
1473 default:
1474 {
1475 // The iterator should skip this.
1476 ShouldNotReachHere();
1477 break;
1478 }
1479 }
1480
1481 if (CITraceTypeFlow) {
1482 print_on(tty);
1483 }
1484
1485 return (_trap_bci != -1);
1486 }
1487
1488 #ifndef PRODUCT
1489 // ------------------------------------------------------------------
1490 // ciTypeFlow::StateVector::print_cell_on
1491 void ciTypeFlow::StateVector::print_cell_on(outputStream* st, Cell c) const {
1492 ciType* type = type_at(c);
1493 if (type == top_type()) {
1494 st->print("top");
1495 } else if (type == bottom_type()) {
1496 st->print("bottom");
1497 } else if (type == null_type()) {
1498 st->print("null");
1499 } else if (type == long2_type()) {
1500 st->print("long2");
1501 } else if (type == double2_type()) {
1502 st->print("double2");
1503 } else if (is_int(type)) {
1504 st->print("int");
1505 } else if (is_long(type)) {
1506 st->print("long");
1507 } else if (is_float(type)) {
1508 st->print("float");
1509 } else if (is_double(type)) {
1510 st->print("double");
1511 } else if (type->is_return_address()) {
1512 st->print("address(%d)", type->as_return_address()->bci());
1735 case Bytecodes::_lookupswitch: {
1736 Bytecode_lookupswitch lookupswitch(str);
1737
1738 int npairs = lookupswitch.number_of_pairs();
1739 _successors =
1740 new (arena) GrowableArray<Block*>(arena, npairs+1, 0, nullptr);
1741 int bci = current_bci + lookupswitch.default_offset();
1742 Block* block = analyzer->block_at(bci, jsrs);
1743 assert(_successors->length() == SWITCH_DEFAULT, "");
1744 _successors->append(block);
1745 while(--npairs >= 0) {
1746 LookupswitchPair pair = lookupswitch.pair_at(npairs);
1747 int bci = current_bci + pair.offset();
1748 Block* block = analyzer->block_at(bci, jsrs);
1749 assert(_successors->length() >= SWITCH_CASES, "");
1750 _successors->append_if_missing(block);
1751 }
1752 break;
1753 }
1754
1755 case Bytecodes::_athrow: case Bytecodes::_ireturn:
1756 case Bytecodes::_lreturn: case Bytecodes::_freturn:
1757 case Bytecodes::_dreturn: case Bytecodes::_areturn:
1758 case Bytecodes::_return:
1759 _successors =
1760 new (arena) GrowableArray<Block*>(arena, 1, 0, nullptr);
1761 // No successors
1762 break;
1763
1764 case Bytecodes::_ret: {
1765 _successors =
1766 new (arena) GrowableArray<Block*>(arena, 1, 0, nullptr);
1767
1768 Cell local = state->local(str->get_index());
1769 ciType* return_address = state->type_at(local);
1770 assert(return_address->is_return_address(), "verify: wrong type");
1771 int bci = return_address->as_return_address()->bci();
1772 assert(_successors->length() == GOTO_TARGET, "");
1773 _successors->append(analyzer->block_at(bci, jsrs));
1774 break;
1775 }
1776
1777 case Bytecodes::_wide:
3131
3132 // ------------------------------------------------------------------
3133 // ciTypeFlow::record_failure()
3134 // The ciTypeFlow object keeps track of failure reasons separately from the ciEnv.
3135 // This is required because there is not a 1-1 relation between the ciEnv and
3136 // the TypeFlow passes within a compilation task. For example, if the compiler
3137 // is considering inlining a method, it will request a TypeFlow. If that fails,
3138 // the compilation as a whole may continue without the inlining. Some TypeFlow
3139 // requests are not optional; if they fail the requestor is responsible for
3140 // copying the failure reason up to the ciEnv. (See Parse::Parse.)
3141 void ciTypeFlow::record_failure(const char* reason) {
3142 if (env()->log() != nullptr) {
3143 env()->log()->elem("failure reason='%s' phase='typeflow'", reason);
3144 }
3145 if (_failure_reason == nullptr) {
3146 // Record the first failure reason.
3147 _failure_reason = reason;
3148 }
3149 }
3150
3151 #ifndef PRODUCT
3152 void ciTypeFlow::print() const { print_on(tty); }
3153
3154 // ------------------------------------------------------------------
3155 // ciTypeFlow::print_on
3156 void ciTypeFlow::print_on(outputStream* st) const {
3157 // Walk through CI blocks
3158 st->print_cr("********************************************************");
3159 st->print ("TypeFlow for ");
3160 method()->name()->print_symbol_on(st);
3161 int limit_bci = code_size();
3162 st->print_cr(" %d bytes", limit_bci);
3163 ciMethodBlocks* mblks = _method->get_method_blocks();
3164 ciBlock* current = nullptr;
3165 for (int bci = 0; bci < limit_bci; bci++) {
3166 ciBlock* blk = mblks->block_containing(bci);
3167 if (blk != nullptr && blk != current) {
3168 current = blk;
3169 current->print_on(st);
3170
|
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 bool null_free = k1->as_array_klass()->is_elem_null_free() &&
328 k2->as_array_klass()->is_elem_null_free();
329 ciType* elem1 = k1->as_array_klass()->element_klass();
330 ciType* elem2 = k2->as_array_klass()->element_klass();
331 ciType* elem = elem1;
332 if (elem1 != elem2) {
333 elem = type_meet_internal(elem1, elem2, analyzer)->as_klass();
334 }
335 // Do an easy shortcut if one type is a super of the other.
336 if (elem == elem1 && !elem->is_inlinetype()) {
337 assert(k1 == ciArrayKlass::make(elem, null_free), "shortcut is OK");
338 return k1;
339 } else if (elem == elem2 && !elem->is_inlinetype()) {
340 assert(k2 == ciArrayKlass::make(elem, null_free), "shortcut is OK");
341 return k2;
342 } else {
343 // TODO 8325106 Remove
344 assert(!null_free, "should be dead");
345 return ciArrayKlass::make(elem);
346 }
347 } else {
348 return object_klass;
349 }
350 } else {
351 // Must be two plain old instance klasses.
352 assert(k1->is_instance_klass(), "previous cases handle non-instances");
353 assert(k2->is_instance_klass(), "previous cases handle non-instances");
354 ciType* result = k1->least_common_ancestor(k2);
355 if (null_free1 && null_free2 && result->is_inlinetype()) {
356 result = analyzer->mark_as_null_free(result);
357 }
358 return result;
359 }
360 }
361
362
363 // ------------------------------------------------------------------
364 // ciTypeFlow::StateVector::StateVector
365 //
366 // Build a new state vector
367 ciTypeFlow::StateVector::StateVector(ciTypeFlow* analyzer) {
368 _outer = analyzer;
369 _stack_size = -1;
370 _monitor_count = -1;
371 // Allocate the _types array
372 int max_cells = analyzer->max_cells();
373 _types = (ciType**)analyzer->arena()->Amalloc(sizeof(ciType*) * max_cells);
374 for (int i=0; i<max_cells; i++) {
375 _types[i] = top_type();
376 }
377 _trap_bci = -1;
378 _trap_index = 0;
400 }
401 // load up the non-OSR state at this point
402 non_osr_block->copy_state_into(state);
403 int non_osr_start = non_osr_block->start();
404 if (non_osr_start != start_bci()) {
405 // must flow forward from it
406 if (CITraceTypeFlow) {
407 tty->print_cr(">> Interpreting pre-OSR block %d:", non_osr_start);
408 }
409 Block* block = block_at(non_osr_start, jsrs);
410 assert(block->limit() == start_bci(), "must flow forward to start");
411 flow_block(block, state, jsrs);
412 }
413 return state;
414 // Note: The code below would be an incorrect for an OSR flow,
415 // even if it were possible for an OSR entry point to be at bci zero.
416 }
417 // "Push" the method signature into the first few locals.
418 state->set_stack_size(-max_locals());
419 if (!method()->is_static()) {
420 ciType* holder = method()->holder();
421 if (holder->is_inlinetype()) {
422 // The receiver is null-free
423 holder = mark_as_null_free(holder);
424 }
425 state->push(holder);
426 assert(state->tos() == state->local(0), "");
427 }
428 for (ciSignatureStream str(method()->signature());
429 !str.at_return_type();
430 str.next()) {
431 state->push_translate(str.type());
432 }
433 // Set the rest of the locals to bottom.
434 Cell cell = state->next_cell(state->tos());
435 state->set_stack_size(0);
436 int limit = state->limit_cell();
437 for (; cell < limit; cell = state->next_cell(cell)) {
438 state->set_type_at(cell, state->bottom_type());
439 }
440 // Lock an object, if necessary.
441 state->set_monitor_count(method()->is_synchronized() ? 1 : 0);
442 return state;
443 }
444
445 // ------------------------------------------------------------------
557
558 return different;
559 }
560
561 // ------------------------------------------------------------------
562 // ciTypeFlow::StateVector::push_translate
563 void ciTypeFlow::StateVector::push_translate(ciType* type) {
564 BasicType basic_type = type->basic_type();
565 if (basic_type == T_BOOLEAN || basic_type == T_CHAR ||
566 basic_type == T_BYTE || basic_type == T_SHORT) {
567 push_int();
568 } else {
569 push(type);
570 if (type->is_two_word()) {
571 push(half_type(type));
572 }
573 }
574 }
575
576 // ------------------------------------------------------------------
577 // ciTypeFlow::StateVector::do_aload
578 void ciTypeFlow::StateVector::do_aload(ciBytecodeStream* str) {
579 pop_int();
580 ciArrayKlass* array_klass = pop_objOrFlatArray();
581 if (array_klass == nullptr) {
582 // Did aload on a null reference; push a null and ignore the exception.
583 // This instruction will never continue normally. All we have to do
584 // is report a value that will meet correctly with any downstream
585 // reference types on paths that will truly be executed. This null type
586 // meets with any reference type to yield that same reference type.
587 // (The compiler will generate an unconditional exception here.)
588 push(null_type());
589 return;
590 }
591 if (!array_klass->is_loaded()) {
592 // Only fails for some -Xcomp runs
593 trap(str, array_klass,
594 Deoptimization::make_trap_request
595 (Deoptimization::Reason_unloaded,
596 Deoptimization::Action_reinterpret));
597 return;
598 }
599 ciKlass* element_klass = array_klass->element_klass();
600 if (!element_klass->is_loaded() && element_klass->is_instance_klass()) {
601 Untested("unloaded array element class in ciTypeFlow");
602 trap(str, element_klass,
603 Deoptimization::make_trap_request
604 (Deoptimization::Reason_unloaded,
605 Deoptimization::Action_reinterpret));
606 } else {
607 if (array_klass->is_elem_null_free()) {
608 // TODO 8325106 Is this dead?
609 push(outer()->mark_as_null_free(element_klass));
610 } else {
611 push_object(element_klass);
612 }
613 }
614 }
615
616
617 // ------------------------------------------------------------------
618 // ciTypeFlow::StateVector::do_checkcast
619 void ciTypeFlow::StateVector::do_checkcast(ciBytecodeStream* str) {
620 bool will_link;
621 ciKlass* klass = str->get_klass(will_link);
622 if (!will_link) {
623 // VM's interpreter will not load 'klass' if object is nullptr.
624 // Type flow after this block may still be needed in two situations:
625 // 1) C2 uses do_null_assert() and continues compilation for later blocks
626 // 2) C2 does an OSR compile in a later block (see bug 4778368).
627 pop_object();
628 do_null_assert(klass);
629 } else {
630 ciType* type = pop_value();
631 type = type->unwrap();
632 if (type->is_loaded() && klass->is_loaded() &&
633 type != klass && type->is_subtype_of(klass)) {
634 // Useless cast, propagate more precise type of object
635 klass = type->as_klass();
636 }
637 if (klass->is_inlinetype() && type->is_null_free()) {
638 // TODO 8325106 Is this dead?
639 push(outer()->mark_as_null_free(klass));
640 } else {
641 push_object(klass);
642 }
643 }
644 }
645
646 // ------------------------------------------------------------------
647 // ciTypeFlow::StateVector::do_getfield
648 void ciTypeFlow::StateVector::do_getfield(ciBytecodeStream* str) {
649 // could add assert here for type of object.
650 pop_object();
651 do_getstatic(str);
652 }
653
654 // ------------------------------------------------------------------
655 // ciTypeFlow::StateVector::do_getstatic
656 void ciTypeFlow::StateVector::do_getstatic(ciBytecodeStream* str) {
657 bool will_link;
658 ciField* field = str->get_field(will_link);
659 if (!will_link) {
660 trap(str, field->holder(), str->get_field_holder_index());
661 } else {
662 ciType* field_type = field->type();
663 if (field->is_static() && field->is_null_free() &&
664 !field_type->as_instance_klass()->is_initialized()) {
665 // Deoptimize if we load from a static field with an uninitialized inline type
666 // because we need to throw an exception if initialization of the type failed.
667 trap(str, field_type->as_klass(),
668 Deoptimization::make_trap_request
669 (Deoptimization::Reason_unloaded,
670 Deoptimization::Action_reinterpret));
671 return;
672 } else if (!field_type->is_loaded()) {
673 // Normally, we need the field's type to be loaded if we are to
674 // do anything interesting with its value.
675 // We used to do this: trap(str, str->get_field_signature_index());
676 //
677 // There is one good reason not to trap here. Execution can
678 // get past this "getfield" or "getstatic" if the value of
679 // the field is null. As long as the value is null, the class
680 // does not need to be loaded! The compiler must assume that
681 // the value of the unloaded class reference is null; if the code
682 // ever sees a non-null value, loading has occurred.
683 //
684 // This actually happens often enough to be annoying. If the
685 // compiler throws an uncommon trap at this bytecode, you can
686 // get an endless loop of recompilations, when all the code
687 // needs to do is load a series of null values. Also, a trap
688 // here can make an OSR entry point unreachable, triggering the
689 // assert on non_osr_block in ciTypeFlow::get_start_state.
690 // (See bug 4379915.)
691 do_null_assert(field_type->as_klass());
692 } else {
693 if (field->is_null_free()) {
694 field_type = outer()->mark_as_null_free(field_type);
695 }
696 push_translate(field_type);
697 }
698 }
699 }
700
701 // ------------------------------------------------------------------
702 // ciTypeFlow::StateVector::do_invoke
703 void ciTypeFlow::StateVector::do_invoke(ciBytecodeStream* str,
704 bool has_receiver) {
705 bool will_link;
706 ciSignature* declared_signature = nullptr;
707 ciMethod* callee = str->get_method(will_link, &declared_signature);
708 assert(declared_signature != nullptr, "cannot be null");
709 if (!will_link) {
710 // We weren't able to find the method.
711 if (str->cur_bc() == Bytecodes::_invokedynamic) {
712 trap(str, nullptr,
713 Deoptimization::make_trap_request
714 (Deoptimization::Reason_uninitialized,
715 Deoptimization::Action_reinterpret));
741 }
742 if (has_receiver) {
743 // Check this?
744 pop_object();
745 }
746 assert(!sigstr.is_done(), "must have return type");
747 ciType* return_type = sigstr.type();
748 if (!return_type->is_void()) {
749 if (!return_type->is_loaded()) {
750 // As in do_getstatic(), generally speaking, we need the return type to
751 // be loaded if we are to do anything interesting with its value.
752 // We used to do this: trap(str, str->get_method_signature_index());
753 //
754 // We do not trap here since execution can get past this invoke if
755 // the return value is null. As long as the value is null, the class
756 // does not need to be loaded! The compiler must assume that
757 // the value of the unloaded class reference is null; if the code
758 // ever sees a non-null value, loading has occurred.
759 //
760 // See do_getstatic() for similar explanation, as well as bug 4684993.
761 if (InlineTypeReturnedAsFields) {
762 // Return might be in scalarized form but we can't handle it because we
763 // don't know the type. This can happen due to a missing preload attribute.
764 // TODO 8284443 Use PhaseMacroExpand::expand_mh_intrinsic_return for this
765 trap(str, nullptr,
766 Deoptimization::make_trap_request
767 (Deoptimization::Reason_uninitialized,
768 Deoptimization::Action_reinterpret));
769 } else {
770 do_null_assert(return_type->as_klass());
771 }
772 } else {
773 push_translate(return_type);
774 }
775 }
776 }
777 }
778
779 // ------------------------------------------------------------------
780 // ciTypeFlow::StateVector::do_jsr
781 void ciTypeFlow::StateVector::do_jsr(ciBytecodeStream* str) {
782 push(ciReturnAddress::make(str->next_bci()));
783 }
784
785 // ------------------------------------------------------------------
786 // ciTypeFlow::StateVector::do_ldc
787 void ciTypeFlow::StateVector::do_ldc(ciBytecodeStream* str) {
788 if (str->is_in_error()) {
789 trap(str, nullptr, Deoptimization::make_trap_request(Deoptimization::Reason_unhandled,
790 Deoptimization::Action_none));
791 return;
792 }
793 ciConstant con = str->get_constant();
794 if (con.is_valid()) {
795 int cp_index = str->get_constant_pool_index();
796 BasicType basic_type = str->get_basic_type_for_constant_at(cp_index);
797 if (is_reference_type(basic_type)) {
798 ciObject* obj = con.as_object();
799 if (obj->is_null_object()) {
800 push_null();
801 } else {
802 assert(obj->is_instance() || obj->is_array(), "must be java_mirror of klass");
803 ciType* type = obj->klass();
804 if (type->is_inlinetype()) {
805 type = outer()->mark_as_null_free(type);
806 }
807 push(type);
808 }
809 } else {
810 assert(basic_type == con.basic_type() || con.basic_type() == T_OBJECT,
811 "not a boxed form: %s vs %s", type2name(basic_type), type2name(con.basic_type()));
812 push_translate(ciType::make(basic_type));
813 }
814 } else {
815 // OutOfMemoryError in the CI while loading a String constant.
816 push_null();
817 outer()->record_failure("ldc did not link");
818 }
819 }
820
821 // ------------------------------------------------------------------
822 // ciTypeFlow::StateVector::do_multianewarray
823 void ciTypeFlow::StateVector::do_multianewarray(ciBytecodeStream* str) {
824 int dimensions = str->get_dimensions();
825 bool will_link;
826 ciArrayKlass* array_klass = str->get_klass(will_link)->as_array_klass();
827 if (!will_link) {
934 // class later.
935 push_null();
936 }
937 }
938
939
940 // ------------------------------------------------------------------
941 // ciTypeFlow::StateVector::apply_one_bytecode
942 //
943 // Apply the effect of one bytecode to this StateVector
944 bool ciTypeFlow::StateVector::apply_one_bytecode(ciBytecodeStream* str) {
945 _trap_bci = -1;
946 _trap_index = 0;
947
948 if (CITraceTypeFlow) {
949 tty->print_cr(">> Interpreting bytecode %d:%s", str->cur_bci(),
950 Bytecodes::name(str->cur_bc()));
951 }
952
953 switch(str->cur_bc()) {
954 case Bytecodes::_aaload: do_aload(str); break;
955
956 case Bytecodes::_aastore:
957 {
958 pop_object();
959 pop_int();
960 pop_objOrFlatArray();
961 break;
962 }
963 case Bytecodes::_aconst_null:
964 {
965 push_null();
966 break;
967 }
968 case Bytecodes::_aload: load_local_object(str->get_index()); break;
969 case Bytecodes::_aload_0: load_local_object(0); break;
970 case Bytecodes::_aload_1: load_local_object(1); break;
971 case Bytecodes::_aload_2: load_local_object(2); break;
972 case Bytecodes::_aload_3: load_local_object(3); break;
973
974 case Bytecodes::_anewarray:
975 {
976 pop_int();
977 bool will_link;
978 ciKlass* element_klass = str->get_klass(will_link);
979 if (!will_link) {
980 trap(str, element_klass, str->get_klass_index());
981 } else {
982 push_object(ciArrayKlass::make(element_klass));
983 }
984 break;
985 }
986 case Bytecodes::_areturn:
987 case Bytecodes::_ifnonnull:
988 case Bytecodes::_ifnull:
989 {
990 pop_object();
991 break;
992 }
993 case Bytecodes::_monitorenter:
994 {
995 pop_object();
996 set_monitor_count(monitor_count() + 1);
997 break;
998 }
999 case Bytecodes::_monitorexit:
1000 {
1001 pop_object();
1002 assert(monitor_count() > 0, "must be a monitor to exit from");
1521 case Bytecodes::_pop2:
1522 {
1523 pop();
1524 pop();
1525 break;
1526 }
1527
1528 case Bytecodes::_putfield: do_putfield(str); break;
1529 case Bytecodes::_putstatic: do_putstatic(str); break;
1530
1531 case Bytecodes::_ret: do_ret(str); break;
1532
1533 case Bytecodes::_swap:
1534 {
1535 ciType* value1 = pop_value();
1536 ciType* value2 = pop_value();
1537 push(value1);
1538 push(value2);
1539 break;
1540 }
1541
1542 case Bytecodes::_wide:
1543 default:
1544 {
1545 // The iterator should skip this.
1546 ShouldNotReachHere();
1547 break;
1548 }
1549 }
1550
1551 if (CITraceTypeFlow) {
1552 print_on(tty);
1553 }
1554
1555 return (_trap_bci != -1);
1556 }
1557
1558 #ifndef PRODUCT
1559 // ------------------------------------------------------------------
1560 // ciTypeFlow::StateVector::print_cell_on
1561 void ciTypeFlow::StateVector::print_cell_on(outputStream* st, Cell c) const {
1562 ciType* type = type_at(c)->unwrap();
1563 if (type == top_type()) {
1564 st->print("top");
1565 } else if (type == bottom_type()) {
1566 st->print("bottom");
1567 } else if (type == null_type()) {
1568 st->print("null");
1569 } else if (type == long2_type()) {
1570 st->print("long2");
1571 } else if (type == double2_type()) {
1572 st->print("double2");
1573 } else if (is_int(type)) {
1574 st->print("int");
1575 } else if (is_long(type)) {
1576 st->print("long");
1577 } else if (is_float(type)) {
1578 st->print("float");
1579 } else if (is_double(type)) {
1580 st->print("double");
1581 } else if (type->is_return_address()) {
1582 st->print("address(%d)", type->as_return_address()->bci());
1805 case Bytecodes::_lookupswitch: {
1806 Bytecode_lookupswitch lookupswitch(str);
1807
1808 int npairs = lookupswitch.number_of_pairs();
1809 _successors =
1810 new (arena) GrowableArray<Block*>(arena, npairs+1, 0, nullptr);
1811 int bci = current_bci + lookupswitch.default_offset();
1812 Block* block = analyzer->block_at(bci, jsrs);
1813 assert(_successors->length() == SWITCH_DEFAULT, "");
1814 _successors->append(block);
1815 while(--npairs >= 0) {
1816 LookupswitchPair pair = lookupswitch.pair_at(npairs);
1817 int bci = current_bci + pair.offset();
1818 Block* block = analyzer->block_at(bci, jsrs);
1819 assert(_successors->length() >= SWITCH_CASES, "");
1820 _successors->append_if_missing(block);
1821 }
1822 break;
1823 }
1824
1825 case Bytecodes::_athrow:
1826 case Bytecodes::_ireturn:
1827 case Bytecodes::_lreturn:
1828 case Bytecodes::_freturn:
1829 case Bytecodes::_dreturn:
1830 case Bytecodes::_areturn:
1831 case Bytecodes::_return:
1832 _successors =
1833 new (arena) GrowableArray<Block*>(arena, 1, 0, nullptr);
1834 // No successors
1835 break;
1836
1837 case Bytecodes::_ret: {
1838 _successors =
1839 new (arena) GrowableArray<Block*>(arena, 1, 0, nullptr);
1840
1841 Cell local = state->local(str->get_index());
1842 ciType* return_address = state->type_at(local);
1843 assert(return_address->is_return_address(), "verify: wrong type");
1844 int bci = return_address->as_return_address()->bci();
1845 assert(_successors->length() == GOTO_TARGET, "");
1846 _successors->append(analyzer->block_at(bci, jsrs));
1847 break;
1848 }
1849
1850 case Bytecodes::_wide:
3204
3205 // ------------------------------------------------------------------
3206 // ciTypeFlow::record_failure()
3207 // The ciTypeFlow object keeps track of failure reasons separately from the ciEnv.
3208 // This is required because there is not a 1-1 relation between the ciEnv and
3209 // the TypeFlow passes within a compilation task. For example, if the compiler
3210 // is considering inlining a method, it will request a TypeFlow. If that fails,
3211 // the compilation as a whole may continue without the inlining. Some TypeFlow
3212 // requests are not optional; if they fail the requestor is responsible for
3213 // copying the failure reason up to the ciEnv. (See Parse::Parse.)
3214 void ciTypeFlow::record_failure(const char* reason) {
3215 if (env()->log() != nullptr) {
3216 env()->log()->elem("failure reason='%s' phase='typeflow'", reason);
3217 }
3218 if (_failure_reason == nullptr) {
3219 // Record the first failure reason.
3220 _failure_reason = reason;
3221 }
3222 }
3223
3224 ciType* ciTypeFlow::mark_as_null_free(ciType* type) {
3225 // Wrap the type to carry the information that it is null-free
3226 return env()->make_null_free_wrapper(type);
3227 }
3228
3229 #ifndef PRODUCT
3230 void ciTypeFlow::print() const { print_on(tty); }
3231
3232 // ------------------------------------------------------------------
3233 // ciTypeFlow::print_on
3234 void ciTypeFlow::print_on(outputStream* st) const {
3235 // Walk through CI blocks
3236 st->print_cr("********************************************************");
3237 st->print ("TypeFlow for ");
3238 method()->name()->print_symbol_on(st);
3239 int limit_bci = code_size();
3240 st->print_cr(" %d bytes", limit_bci);
3241 ciMethodBlocks* mblks = _method->get_method_blocks();
3242 ciBlock* current = nullptr;
3243 for (int bci = 0; bci < limit_bci; bci++) {
3244 ciBlock* blk = mblks->block_containing(bci);
3245 if (blk != nullptr && blk != current) {
3246 current = blk;
3247 current->print_on(st);
3248
|