hotspot/src/share/vm/prims/methodHandleWalk.cpp
changeset 10514 e229a19078cf
parent 10008 d84de97ad847
child 10735 67a139f9f954
equal deleted inserted replaced
10511:22b3580bd8bb 10514:e229a19078cf
   180 
   180 
   181 void MethodHandleChain::print(oopDesc* m) {
   181 void MethodHandleChain::print(oopDesc* m) {
   182   HandleMark hm;
   182   HandleMark hm;
   183   ResourceMark rm;
   183   ResourceMark rm;
   184   Handle mh(m);
   184   Handle mh(m);
   185   print(mh);
       
   186 }
       
   187 
       
   188 void MethodHandleChain::print(Handle mh) {
       
   189   EXCEPTION_MARK;
   185   EXCEPTION_MARK;
   190   MethodHandleChain mhc(mh, THREAD);
   186   MethodHandleChain mhc(mh, THREAD);
   191   if (HAS_PENDING_EXCEPTION) {
   187   if (HAS_PENDING_EXCEPTION) {
   192     oop ex = THREAD->pending_exception();
   188     oop ex = THREAD->pending_exception();
   193     CLEAR_PENDING_EXCEPTION;
   189     CLEAR_PENDING_EXCEPTION;
   220                  chain.bound_arg_slot());
   216                  chain.bound_arg_slot());
   221       oop o = chain.bound_arg_oop();
   217       oop o = chain.bound_arg_oop();
   222       if (o != NULL) {
   218       if (o != NULL) {
   223         if (o->is_instance()) {
   219         if (o->is_instance()) {
   224           tty->print(" instance %s", o->klass()->klass_part()->internal_name());
   220           tty->print(" instance %s", o->klass()->klass_part()->internal_name());
       
   221           if (java_lang_invoke_CountingMethodHandle::is_instance(o)) {
       
   222             tty->print(" vmcount: %d", java_lang_invoke_CountingMethodHandle::vmcount(o));
       
   223           }
   225         } else {
   224         } else {
   226           o->print();
   225           o->print();
       
   226         }
       
   227       }
       
   228       oop vmt = chain.vmtarget_oop();
       
   229       if (vmt != NULL) {
       
   230         if (vmt->is_method()) {
       
   231           tty->print(" ");
       
   232           methodOop(vmt)->print_short_name(tty);
       
   233         } else if (java_lang_invoke_MethodHandle::is_instance(vmt)) {
       
   234           tty->print(" method handle " INTPTR_FORMAT, vmt);
       
   235         } else {
       
   236           ShouldNotReachHere();
   227         }
   237         }
   228       }
   238       }
   229     } else if (chain.is_adapter()) {
   239     } else if (chain.is_adapter()) {
   230       tty->print("adapter: arg_slot %d conversion op %s",
   240       tty->print("adapter: arg_slot %d conversion op %s",
   231                  chain.adapter_arg_slot(),
   241                  chain.adapter_arg_slot(),
   232                  adapter_op_to_string(chain.adapter_conversion_op()));
   242                  adapter_op_to_string(chain.adapter_conversion_op()));
   233       switch (chain.adapter_conversion_op()) {
   243       switch (chain.adapter_conversion_op()) {
   234         case java_lang_invoke_AdapterMethodHandle::OP_RETYPE_ONLY:
   244         case java_lang_invoke_AdapterMethodHandle::OP_RETYPE_ONLY:
       
   245           if (java_lang_invoke_CountingMethodHandle::is_instance(chain.method_handle_oop())) {
       
   246             tty->print(" vmcount: %d", java_lang_invoke_CountingMethodHandle::vmcount(chain.method_handle_oop()));
       
   247           }
   235         case java_lang_invoke_AdapterMethodHandle::OP_RETYPE_RAW:
   248         case java_lang_invoke_AdapterMethodHandle::OP_RETYPE_RAW:
   236         case java_lang_invoke_AdapterMethodHandle::OP_CHECK_CAST:
   249         case java_lang_invoke_AdapterMethodHandle::OP_CHECK_CAST:
   237         case java_lang_invoke_AdapterMethodHandle::OP_PRIM_TO_PRIM:
   250         case java_lang_invoke_AdapterMethodHandle::OP_PRIM_TO_PRIM:
   238         case java_lang_invoke_AdapterMethodHandle::OP_REF_TO_PRIM:
   251         case java_lang_invoke_AdapterMethodHandle::OP_REF_TO_PRIM:
   239           break;
   252           break;
   905     _bytecode(THREAD, 50),
   918     _bytecode(THREAD, 50),
   906     _constants(THREAD, 10),
   919     _constants(THREAD, 10),
   907     _non_bcp_klasses(THREAD, 5),
   920     _non_bcp_klasses(THREAD, 5),
   908     _cur_stack(0),
   921     _cur_stack(0),
   909     _max_stack(0),
   922     _max_stack(0),
   910     _rtype(T_ILLEGAL)
   923     _rtype(T_ILLEGAL),
       
   924     _selectAlternative_bci(-1),
       
   925     _taken_count(0),
       
   926     _not_taken_count(0)
   911 {
   927 {
   912 
   928 
   913   // Element zero is always the null constant.
   929   // Element zero is always the null constant.
   914   (void) _constants.append(NULL);
   930   (void) _constants.append(NULL);
   915 
   931 
  1113     _bytecode.push(index);
  1129     _bytecode.push(index);
  1114     _bytecode.push(args_size);
  1130     _bytecode.push(args_size);
  1115     _bytecode.push(0);
  1131     _bytecode.push(0);
  1116     break;
  1132     break;
  1117 
  1133 
       
  1134   case Bytecodes::_ifeq:
       
  1135     assert((unsigned short) index == index, "index does not fit in 16-bit");
       
  1136     _bytecode.push(op);
       
  1137     _bytecode.push(index >> 8);
       
  1138     _bytecode.push(index);
       
  1139     break;
       
  1140 
  1118   default:
  1141   default:
  1119     ShouldNotReachHere();
  1142     ShouldNotReachHere();
       
  1143   }
       
  1144 }
       
  1145 
       
  1146 void MethodHandleCompiler::update_branch_dest(int src, int dst) {
       
  1147   switch (_bytecode.at(src)) {
       
  1148     case Bytecodes::_ifeq:
       
  1149       dst -= src; // compute the offset
       
  1150       assert((unsigned short) dst == dst, "index does not fit in 16-bit");
       
  1151       _bytecode.at_put(src + 1, dst >> 8);
       
  1152       _bytecode.at_put(src + 2, dst);
       
  1153       break;
       
  1154     default:
       
  1155       ShouldNotReachHere();
       
  1156   }
       
  1157 }
       
  1158 
       
  1159 void MethodHandleCompiler::emit_load(ArgToken arg) {
       
  1160   TokenType tt = arg.token_type();
       
  1161   BasicType bt = arg.basic_type();
       
  1162 
       
  1163   switch (tt) {
       
  1164     case tt_parameter:
       
  1165     case tt_temporary:
       
  1166       emit_load(bt, arg.index());
       
  1167       break;
       
  1168     case tt_constant:
       
  1169       emit_load_constant(arg);
       
  1170       break;
       
  1171     case tt_illegal:
       
  1172     case tt_void:
       
  1173     default:
       
  1174       ShouldNotReachHere();
  1120   }
  1175   }
  1121 }
  1176 }
  1122 
  1177 
  1123 
  1178 
  1124 void MethodHandleCompiler::emit_load(BasicType bt, int index) {
  1179 void MethodHandleCompiler::emit_load(BasicType bt, int index) {
  1315 //
  1370 //
  1316 
  1371 
  1317 // Values used by the compiler.
  1372 // Values used by the compiler.
  1318 jvalue MethodHandleCompiler::zero_jvalue = { 0 };
  1373 jvalue MethodHandleCompiler::zero_jvalue = { 0 };
  1319 jvalue MethodHandleCompiler::one_jvalue  = { 1 };
  1374 jvalue MethodHandleCompiler::one_jvalue  = { 1 };
       
  1375 
       
  1376 // Fetch any values from CountingMethodHandles and capture them for profiles
       
  1377 bool MethodHandleCompiler::fetch_counts(ArgToken arg1, ArgToken arg2) {
       
  1378   int count1 = -1, count2 = -1;
       
  1379   if (arg1.token_type() == tt_constant && arg1.basic_type() == T_OBJECT &&
       
  1380       java_lang_invoke_CountingMethodHandle::is_instance(arg1.object()())) {
       
  1381     count1 = java_lang_invoke_CountingMethodHandle::vmcount(arg1.object()());
       
  1382   }
       
  1383   if (arg2.token_type() == tt_constant && arg2.basic_type() == T_OBJECT &&
       
  1384       java_lang_invoke_CountingMethodHandle::is_instance(arg2.object()())) {
       
  1385     count2 = java_lang_invoke_CountingMethodHandle::vmcount(arg2.object()());
       
  1386   }
       
  1387   int total = count1 + count2;
       
  1388   if (count1 != -1 && count2 != -1 && total != 0) {
       
  1389     // Normalize the collect counts to the invoke_count
       
  1390     tty->print("counts %d %d scaled by %d = ", count2, count1, _invoke_count);
       
  1391     if (count1 != 0) _not_taken_count = (int)(_invoke_count * count1 / (double)total);
       
  1392     if (count2 != 0) _taken_count = (int)(_invoke_count * count2 / (double)total);
       
  1393     tty->print_cr("%d %d", _taken_count, _not_taken_count);
       
  1394     return true;
       
  1395   }
       
  1396   return false;
       
  1397 }
  1320 
  1398 
  1321 // Emit bytecodes for the given invoke instruction.
  1399 // Emit bytecodes for the given invoke instruction.
  1322 MethodHandleWalker::ArgToken
  1400 MethodHandleWalker::ArgToken
  1323 MethodHandleCompiler::make_invoke(methodHandle m, vmIntrinsics::ID iid,
  1401 MethodHandleCompiler::make_invoke(methodHandle m, vmIntrinsics::ID iid,
  1324                                   Bytecodes::Code op, bool tailcall,
  1402                                   Bytecodes::Code op, bool tailcall,
  1365       m = m2;
  1443       m = m2;
  1366       signature = m->signature();
  1444       signature = m->signature();
  1367     }
  1445     }
  1368   }
  1446   }
  1369 
  1447 
       
  1448   if (m->intrinsic_id() == vmIntrinsics::_selectAlternative &&
       
  1449       fetch_counts(argv[1], argv[2])) {
       
  1450     assert(argc == 3, "three arguments");
       
  1451     assert(tailcall, "only");
       
  1452 
       
  1453     // do inline bytecodes so we can drop profile data into it,
       
  1454     //   0:   iload_0
       
  1455     emit_load(argv[0]);
       
  1456     //   1:   ifeq    8
       
  1457     _selectAlternative_bci = _bytecode.length();
       
  1458     emit_bc(Bytecodes::_ifeq, 0); // emit placeholder offset
       
  1459     //   4:   aload_1
       
  1460     emit_load(argv[1]);
       
  1461     //   5:   areturn;
       
  1462     emit_bc(Bytecodes::_areturn);
       
  1463     //   8:   aload_2
       
  1464     update_branch_dest(_selectAlternative_bci, cur_bci());
       
  1465     emit_load(argv[2]);
       
  1466     //   9:   areturn
       
  1467     emit_bc(Bytecodes::_areturn);
       
  1468     return ArgToken();  // Dummy return value.
       
  1469   }
       
  1470 
  1370   check_non_bcp_klass(klass, CHECK_(zero));
  1471   check_non_bcp_klass(klass, CHECK_(zero));
  1371   if (m->is_method_handle_invoke()) {
  1472   if (m->is_method_handle_invoke()) {
  1372     check_non_bcp_klasses(m->method_handle_type(), CHECK_(zero));
  1473     check_non_bcp_klasses(m->method_handle_type(), CHECK_(zero));
  1373   }
  1474   }
  1374 
  1475 
  1375   // Count the number of arguments, not the size
  1476   // Count the number of arguments, not the size
  1376   ArgumentCount asc(signature);
  1477   ArgumentCount asc(signature);
  1377   assert(argc == asc.size() + ((op == Bytecodes::_invokestatic || op == Bytecodes::_invokedynamic) ? 0 : 1),
  1478   assert(argc == asc.size() + ((op == Bytecodes::_invokestatic || op == Bytecodes::_invokedynamic) ? 0 : 1),
  1378          "argc mismatch");
  1479          "argc mismatch");
  1379 
       
  1380   // Inline the method.
       
  1381   InvocationCounter* ic = m->invocation_counter();
       
  1382   ic->set_carry_flag();
       
  1383 
  1480 
  1384   for (int i = 0; i < argc; i++) {
  1481   for (int i = 0; i < argc; i++) {
  1385     ArgToken arg = argv[i];
  1482     ArgToken arg = argv[i];
  1386     TokenType tt = arg.token_type();
  1483     TokenType tt = arg.token_type();
  1387     BasicType bt = arg.basic_type();
  1484     BasicType bt = arg.basic_type();
  1684 
  1781 
  1685   return cpool;
  1782   return cpool;
  1686 }
  1783 }
  1687 
  1784 
  1688 
  1785 
  1689 methodHandle MethodHandleCompiler::get_method_oop(TRAPS) const {
  1786 methodHandle MethodHandleCompiler::get_method_oop(TRAPS) {
  1690   methodHandle empty;
  1787   methodHandle empty;
  1691   // Create a method that holds the generated bytecode.  invokedynamic
  1788   // Create a method that holds the generated bytecode.  invokedynamic
  1692   // has no receiver, normal MH calls do.
  1789   // has no receiver, normal MH calls do.
  1693   int flags_bits;
  1790   int flags_bits;
  1694   if (for_invokedynamic())
  1791   if (for_invokedynamic())
  1763   {
  1860   {
  1764     methodDataOop mdo = oopFactory::new_methodData(m, CHECK_(empty));
  1861     methodDataOop mdo = oopFactory::new_methodData(m, CHECK_(empty));
  1765     assert(m->method_data() == NULL, "there should not be an MDO yet");
  1862     assert(m->method_data() == NULL, "there should not be an MDO yet");
  1766     m->set_method_data(mdo);
  1863     m->set_method_data(mdo);
  1767 
  1864 
       
  1865     bool found_selectAlternative = false;
  1768     // Iterate over all profile data and set the count of the counter
  1866     // Iterate over all profile data and set the count of the counter
  1769     // data entries to the original call site counter.
  1867     // data entries to the original call site counter.
  1770     for (ProfileData* profile_data = mdo->first_data();
  1868     for (ProfileData* profile_data = mdo->first_data();
  1771          mdo->is_valid(profile_data);
  1869          mdo->is_valid(profile_data);
  1772          profile_data = mdo->next_data(profile_data)) {
  1870          profile_data = mdo->next_data(profile_data)) {
  1773       if (profile_data->is_CounterData()) {
  1871       if (profile_data->is_CounterData()) {
  1774         CounterData* counter_data = profile_data->as_CounterData();
  1872         CounterData* counter_data = profile_data->as_CounterData();
  1775         counter_data->set_count(_invoke_count);
  1873         counter_data->set_count(_invoke_count);
  1776       }
  1874       }
  1777     }
  1875       if (profile_data->is_BranchData() &&
       
  1876           profile_data->bci() == _selectAlternative_bci) {
       
  1877         BranchData* bd = profile_data->as_BranchData();
       
  1878         bd->set_taken(_taken_count);
       
  1879         bd->set_not_taken(_not_taken_count);
       
  1880         found_selectAlternative = true;
       
  1881       }
       
  1882     }
       
  1883     assert(_selectAlternative_bci == -1 || found_selectAlternative, "must have found profile entry");
  1778   }
  1884   }
  1779 
  1885 
  1780 #ifndef PRODUCT
  1886 #ifndef PRODUCT
  1781   if (TraceMethodHandles) {
  1887   if (TraceMethodHandles) {
  1782     m->print();
  1888     m->print();