PageRenderTime 829ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/javac_compiler/HotspotSource/JavaHotSpotSrc/src/vm/opto/bytecodeInfo.cpp

http://jvmnotebook.googlecode.com/
C++ | 551 lines | 381 code | 72 blank | 98 comment | 170 complexity | fccb061e14845db589f2af822a14442c MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, Apache-2.0, CPL-1.0, AGPL-1.0, JSON, LGPL-2.1, GPL-2.0, BSD-3-Clause, BSD-3-Clause-No-Nuclear-License-2014, Unlicense, LGPL-2.0
  1. /*
  2. * Copyright (c) 1998, 2010, Oracle and/or its affiliates. All rights reserved.
  3. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4. *
  5. * This code is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2 only, as
  7. * published by the Free Software Foundation.
  8. *
  9. * This code is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. * version 2 for more details (a copy is included in the LICENSE file that
  13. * accompanied this code).
  14. *
  15. * You should have received a copy of the GNU General Public License version
  16. * 2 along with this work; if not, write to the Free Software Foundation,
  17. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18. *
  19. * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20. * or visit www.oracle.com if you need additional information or have any
  21. * questions.
  22. *
  23. */
  24. #include "incls/_precompiled.incl"
  25. #include "incls/_bytecodeInfo.cpp.incl"
  26. //=============================================================================
  27. //------------------------------InlineTree-------------------------------------
  28. InlineTree::InlineTree( Compile* c,
  29. const InlineTree *caller_tree, ciMethod* callee,
  30. JVMState* caller_jvms, int caller_bci,
  31. float site_invoke_ratio, int site_depth_adjust)
  32. : C(c), _caller_jvms(caller_jvms),
  33. _caller_tree((InlineTree*)caller_tree),
  34. _method(callee), _site_invoke_ratio(site_invoke_ratio),
  35. _site_depth_adjust(site_depth_adjust),
  36. _count_inline_bcs(method()->code_size())
  37. {
  38. NOT_PRODUCT(_count_inlines = 0;)
  39. if (_caller_jvms != NULL) {
  40. // Keep a private copy of the caller_jvms:
  41. _caller_jvms = new (C) JVMState(caller_jvms->method(), caller_tree->caller_jvms());
  42. _caller_jvms->set_bci(caller_jvms->bci());
  43. assert(!caller_jvms->should_reexecute(), "there should be no reexecute bytecode with inlining");
  44. }
  45. assert(_caller_jvms->same_calls_as(caller_jvms), "consistent JVMS");
  46. assert((caller_tree == NULL ? 0 : caller_tree->stack_depth() + 1) == stack_depth(), "correct (redundant) depth parameter");
  47. assert(caller_bci == this->caller_bci(), "correct (redundant) bci parameter");
  48. if (UseOldInlining) {
  49. // Update hierarchical counts, count_inline_bcs() and count_inlines()
  50. InlineTree *caller = (InlineTree *)caller_tree;
  51. for( ; caller != NULL; caller = ((InlineTree *)(caller->caller_tree())) ) {
  52. caller->_count_inline_bcs += count_inline_bcs();
  53. NOT_PRODUCT(caller->_count_inlines++;)
  54. }
  55. }
  56. }
  57. InlineTree::InlineTree(Compile* c, ciMethod* callee_method, JVMState* caller_jvms,
  58. float site_invoke_ratio, int site_depth_adjust)
  59. : C(c), _caller_jvms(caller_jvms), _caller_tree(NULL),
  60. _method(callee_method), _site_invoke_ratio(site_invoke_ratio),
  61. _site_depth_adjust(site_depth_adjust),
  62. _count_inline_bcs(method()->code_size())
  63. {
  64. NOT_PRODUCT(_count_inlines = 0;)
  65. assert(!UseOldInlining, "do not use for old stuff");
  66. }
  67. static void print_indent(int depth) {
  68. tty->print(" ");
  69. for (int i = depth; i != 0; --i) tty->print(" ");
  70. }
  71. static bool is_init_with_ea(ciMethod* callee_method,
  72. ciMethod* caller_method, Compile* C) {
  73. // True when EA is ON and a java constructor is called or
  74. // a super constructor is called from an inlined java constructor.
  75. return C->do_escape_analysis() && EliminateAllocations &&
  76. ( callee_method->is_initializer() ||
  77. (caller_method->is_initializer() &&
  78. caller_method != C->method() &&
  79. caller_method->holder()->is_subclass_of(callee_method->holder()))
  80. );
  81. }
  82. // positive filter: should send be inlined? returns NULL, if yes, or rejection msg
  83. const char* InlineTree::shouldInline(ciMethod* callee_method, ciMethod* caller_method, int caller_bci, ciCallProfile& profile, WarmCallInfo* wci_result) const {
  84. // Allows targeted inlining
  85. if(callee_method->should_inline()) {
  86. *wci_result = *(WarmCallInfo::always_hot());
  87. if (PrintInlining && Verbose) {
  88. print_indent(inline_depth());
  89. tty->print_cr("Inlined method is hot: ");
  90. }
  91. return NULL;
  92. }
  93. // positive filter: should send be inlined? returns NULL (--> yes)
  94. // or rejection msg
  95. int max_size = C->max_inline_size();
  96. int size = callee_method->code_size();
  97. // Check for too many throws (and not too huge)
  98. if(callee_method->interpreter_throwout_count() > InlineThrowCount &&
  99. size < InlineThrowMaxSize ) {
  100. wci_result->set_profit(wci_result->profit() * 100);
  101. if (PrintInlining && Verbose) {
  102. print_indent(inline_depth());
  103. tty->print_cr("Inlined method with many throws (throws=%d):", callee_method->interpreter_throwout_count());
  104. }
  105. return NULL;
  106. }
  107. if (!UseOldInlining) {
  108. return NULL; // size and frequency are represented in a new way
  109. }
  110. int call_site_count = method()->scale_count(profile.count());
  111. int invoke_count = method()->interpreter_invocation_count();
  112. assert( invoke_count != 0, "Require invokation count greater than zero");
  113. int freq = call_site_count/invoke_count;
  114. // bump the max size if the call is frequent
  115. if ((freq >= InlineFrequencyRatio) ||
  116. (call_site_count >= InlineFrequencyCount) ||
  117. is_init_with_ea(callee_method, caller_method, C)) {
  118. max_size = C->freq_inline_size();
  119. if (size <= max_size && TraceFrequencyInlining) {
  120. print_indent(inline_depth());
  121. tty->print_cr("Inlined frequent method (freq=%d count=%d):", freq, call_site_count);
  122. print_indent(inline_depth());
  123. callee_method->print();
  124. tty->cr();
  125. }
  126. } else {
  127. // Not hot. Check for medium-sized pre-existing nmethod at cold sites.
  128. if (callee_method->has_compiled_code() &&
  129. callee_method->instructions_size() > InlineSmallCode/4)
  130. return "already compiled into a medium method";
  131. }
  132. if (size > max_size) {
  133. if (max_size > C->max_inline_size())
  134. return "hot method too big";
  135. return "too big";
  136. }
  137. return NULL;
  138. }
  139. // negative filter: should send NOT be inlined? returns NULL, ok to inline, or rejection msg
  140. const char* InlineTree::shouldNotInline(ciMethod *callee_method, ciMethod* caller_method, WarmCallInfo* wci_result) const {
  141. // negative filter: should send NOT be inlined? returns NULL (--> inline) or rejection msg
  142. if (!UseOldInlining) {
  143. const char* fail = NULL;
  144. if (callee_method->is_abstract()) fail = "abstract method";
  145. // note: we allow ik->is_abstract()
  146. if (!callee_method->holder()->is_initialized()) fail = "method holder not initialized";
  147. if (callee_method->is_native()) fail = "native method";
  148. if (fail) {
  149. *wci_result = *(WarmCallInfo::always_cold());
  150. return fail;
  151. }
  152. if (callee_method->has_unloaded_classes_in_signature()) {
  153. wci_result->set_profit(wci_result->profit() * 0.1);
  154. }
  155. // don't inline exception code unless the top method belongs to an
  156. // exception class
  157. if (callee_method->holder()->is_subclass_of(C->env()->Throwable_klass())) {
  158. ciMethod* top_method = caller_jvms() ? caller_jvms()->of_depth(1)->method() : method();
  159. if (!top_method->holder()->is_subclass_of(C->env()->Throwable_klass())) {
  160. wci_result->set_profit(wci_result->profit() * 0.1);
  161. }
  162. }
  163. if (callee_method->has_compiled_code() && callee_method->instructions_size() > InlineSmallCode) {
  164. wci_result->set_profit(wci_result->profit() * 0.1);
  165. // %%% adjust wci_result->size()?
  166. }
  167. return NULL;
  168. }
  169. // Always inline MethodHandle methods and generated MethodHandle adapters.
  170. if (callee_method->is_method_handle_invoke() || callee_method->is_method_handle_adapter())
  171. return NULL;
  172. // First check all inlining restrictions which are required for correctness
  173. if (callee_method->is_abstract()) return "abstract method";
  174. // note: we allow ik->is_abstract()
  175. if (!callee_method->holder()->is_initialized()) return "method holder not initialized";
  176. if (callee_method->is_native()) return "native method";
  177. if (callee_method->has_unloaded_classes_in_signature()) return "unloaded signature classes";
  178. if (callee_method->should_inline()) {
  179. // ignore heuristic controls on inlining
  180. return NULL;
  181. }
  182. // Now perform checks which are heuristic
  183. if( callee_method->has_compiled_code() && callee_method->instructions_size() > InlineSmallCode )
  184. return "already compiled into a big method";
  185. // don't inline exception code unless the top method belongs to an
  186. // exception class
  187. if (caller_tree() != NULL &&
  188. callee_method->holder()->is_subclass_of(C->env()->Throwable_klass())) {
  189. const InlineTree *top = this;
  190. while (top->caller_tree() != NULL) top = top->caller_tree();
  191. ciInstanceKlass* k = top->method()->holder();
  192. if (!k->is_subclass_of(C->env()->Throwable_klass()))
  193. return "exception method";
  194. }
  195. // use frequency-based objections only for non-trivial methods
  196. if (callee_method->code_size() <= MaxTrivialSize) return NULL;
  197. // don't use counts with -Xcomp or CTW
  198. if (UseInterpreter && !CompileTheWorld) {
  199. if (!callee_method->has_compiled_code() &&
  200. !callee_method->was_executed_more_than(0)) {
  201. return "never executed";
  202. }
  203. if (is_init_with_ea(callee_method, caller_method, C)) {
  204. // Escape Analysis: inline all executed constructors
  205. } else if (!callee_method->was_executed_more_than(MIN2(MinInliningThreshold,
  206. CompileThreshold >> 1))) {
  207. return "executed < MinInliningThreshold times";
  208. }
  209. }
  210. if (callee_method->should_not_inline()) {
  211. return "disallowed by CompilerOracle";
  212. }
  213. if (UseStringCache) {
  214. // Do not inline StringCache::profile() method used only at the beginning.
  215. if (callee_method->name() == ciSymbol::profile_name() &&
  216. callee_method->holder()->name() == ciSymbol::java_lang_StringCache()) {
  217. return "profiling method";
  218. }
  219. }
  220. return NULL;
  221. }
  222. //-----------------------------try_to_inline-----------------------------------
  223. // return NULL if ok, reason for not inlining otherwise
  224. // Relocated from "InliningClosure::try_to_inline"
  225. const char* InlineTree::try_to_inline(ciMethod* callee_method, ciMethod* caller_method, int caller_bci, ciCallProfile& profile, WarmCallInfo* wci_result) {
  226. // Old algorithm had funny accumulating BC-size counters
  227. if (UseOldInlining && ClipInlining
  228. && (int)count_inline_bcs() >= DesiredMethodLimit) {
  229. return "size > DesiredMethodLimit";
  230. }
  231. const char *msg = NULL;
  232. if ((msg = shouldInline(callee_method, caller_method, caller_bci,
  233. profile, wci_result)) != NULL) {
  234. return msg;
  235. }
  236. if ((msg = shouldNotInline(callee_method, caller_method,
  237. wci_result)) != NULL) {
  238. return msg;
  239. }
  240. if (InlineAccessors && callee_method->is_accessor()) {
  241. // accessor methods are not subject to any of the following limits.
  242. return NULL;
  243. }
  244. // suppress a few checks for accessors and trivial methods
  245. if (callee_method->code_size() > MaxTrivialSize) {
  246. // don't inline into giant methods
  247. if (C->unique() > (uint)NodeCountInliningCutoff) {
  248. return "NodeCountInliningCutoff";
  249. }
  250. if ((!UseInterpreter || CompileTheWorld) &&
  251. is_init_with_ea(callee_method, caller_method, C)) {
  252. // Escape Analysis stress testing when running Xcomp or CTW:
  253. // inline constructors even if they are not reached.
  254. } else if (profile.count() == 0) {
  255. // don't inline unreached call sites
  256. return "call site not reached";
  257. }
  258. }
  259. if (!C->do_inlining() && InlineAccessors) {
  260. return "not an accessor";
  261. }
  262. if( inline_depth() > MaxInlineLevel ) {
  263. return "inlining too deep";
  264. }
  265. if( method() == callee_method &&
  266. inline_depth() > MaxRecursiveInlineLevel ) {
  267. return "recursively inlining too deep";
  268. }
  269. int size = callee_method->code_size();
  270. if (UseOldInlining && ClipInlining
  271. && (int)count_inline_bcs() + size >= DesiredMethodLimit) {
  272. return "size > DesiredMethodLimit";
  273. }
  274. // ok, inline this method
  275. return NULL;
  276. }
  277. //------------------------------pass_initial_checks----------------------------
  278. bool pass_initial_checks(ciMethod* caller_method, int caller_bci, ciMethod* callee_method) {
  279. ciInstanceKlass *callee_holder = callee_method ? callee_method->holder() : NULL;
  280. // Check if a callee_method was suggested
  281. if( callee_method == NULL ) return false;
  282. // Check if klass of callee_method is loaded
  283. if( !callee_holder->is_loaded() ) return false;
  284. if( !callee_holder->is_initialized() ) return false;
  285. if( !UseInterpreter || CompileTheWorld /* running Xcomp or CTW */ ) {
  286. // Checks that constant pool's call site has been visited
  287. // stricter than callee_holder->is_initialized()
  288. ciBytecodeStream iter(caller_method);
  289. iter.force_bci(caller_bci);
  290. Bytecodes::Code call_bc = iter.cur_bc();
  291. // An invokedynamic instruction does not have a klass.
  292. if (call_bc != Bytecodes::_invokedynamic) {
  293. int index = iter.get_index_u2_cpcache();
  294. if (!caller_method->is_klass_loaded(index, true)) {
  295. return false;
  296. }
  297. // Try to do constant pool resolution if running Xcomp
  298. if( !caller_method->check_call(index, call_bc == Bytecodes::_invokestatic) ) {
  299. return false;
  300. }
  301. }
  302. }
  303. // We will attempt to see if a class/field/etc got properly loaded. If it
  304. // did not, it may attempt to throw an exception during our probing. Catch
  305. // and ignore such exceptions and do not attempt to compile the method.
  306. if( callee_method->should_exclude() ) return false;
  307. return true;
  308. }
  309. #ifndef PRODUCT
  310. //------------------------------print_inlining---------------------------------
  311. // Really, the failure_msg can be a success message also.
  312. void InlineTree::print_inlining(ciMethod *callee_method, int caller_bci, const char *failure_msg) const {
  313. print_indent(inline_depth());
  314. tty->print("@ %d ", caller_bci);
  315. if( callee_method ) callee_method->print_short_name();
  316. else tty->print(" callee not monotonic or profiled");
  317. tty->print(" %s", (failure_msg ? failure_msg : "inline"));
  318. if( Verbose && callee_method ) {
  319. const InlineTree *top = this;
  320. while( top->caller_tree() != NULL ) { top = top->caller_tree(); }
  321. tty->print(" bcs: %d+%d invoked: %d", top->count_inline_bcs(), callee_method->code_size(), callee_method->interpreter_invocation_count());
  322. }
  323. tty->cr();
  324. }
  325. #endif
  326. //------------------------------ok_to_inline-----------------------------------
  327. WarmCallInfo* InlineTree::ok_to_inline(ciMethod* callee_method, JVMState* jvms, ciCallProfile& profile, WarmCallInfo* initial_wci) {
  328. assert(callee_method != NULL, "caller checks for optimized virtual!");
  329. #ifdef ASSERT
  330. // Make sure the incoming jvms has the same information content as me.
  331. // This means that we can eventually make this whole class AllStatic.
  332. if (jvms->caller() == NULL) {
  333. assert(_caller_jvms == NULL, "redundant instance state");
  334. } else {
  335. assert(_caller_jvms->same_calls_as(jvms->caller()), "redundant instance state");
  336. }
  337. assert(_method == jvms->method(), "redundant instance state");
  338. #endif
  339. const char *failure_msg = NULL;
  340. int caller_bci = jvms->bci();
  341. ciMethod *caller_method = jvms->method();
  342. if( !pass_initial_checks(caller_method, caller_bci, callee_method)) {
  343. if( PrintInlining ) {
  344. failure_msg = "failed_initial_checks";
  345. print_inlining( callee_method, caller_bci, failure_msg);
  346. }
  347. return NULL;
  348. }
  349. // Check if inlining policy says no.
  350. WarmCallInfo wci = *(initial_wci);
  351. failure_msg = try_to_inline(callee_method, caller_method, caller_bci, profile, &wci);
  352. if (failure_msg != NULL && C->log() != NULL) {
  353. C->log()->begin_elem("inline_fail reason='");
  354. C->log()->text("%s", failure_msg);
  355. C->log()->end_elem("'");
  356. }
  357. #ifndef PRODUCT
  358. if (UseOldInlining && InlineWarmCalls
  359. && (PrintOpto || PrintOptoInlining || PrintInlining)) {
  360. bool cold = wci.is_cold();
  361. bool hot = !cold && wci.is_hot();
  362. bool old_cold = (failure_msg != NULL);
  363. if (old_cold != cold || (Verbose || WizardMode)) {
  364. tty->print(" OldInlining= %4s : %s\n WCI=",
  365. old_cold ? "cold" : "hot", failure_msg ? failure_msg : "OK");
  366. wci.print();
  367. }
  368. }
  369. #endif
  370. if (UseOldInlining) {
  371. if (failure_msg == NULL)
  372. wci = *(WarmCallInfo::always_hot());
  373. else
  374. wci = *(WarmCallInfo::always_cold());
  375. }
  376. if (!InlineWarmCalls) {
  377. if (!wci.is_cold() && !wci.is_hot()) {
  378. // Do not inline the warm calls.
  379. wci = *(WarmCallInfo::always_cold());
  380. }
  381. }
  382. if (!wci.is_cold()) {
  383. // In -UseOldInlining, the failure_msg may also be a success message.
  384. if (failure_msg == NULL) failure_msg = "inline (hot)";
  385. // Inline!
  386. if( PrintInlining ) print_inlining( callee_method, caller_bci, failure_msg);
  387. if (UseOldInlining)
  388. build_inline_tree_for_callee(callee_method, jvms, caller_bci);
  389. if (InlineWarmCalls && !wci.is_hot())
  390. return new (C) WarmCallInfo(wci); // copy to heap
  391. return WarmCallInfo::always_hot();
  392. }
  393. // Do not inline
  394. if (failure_msg == NULL) failure_msg = "too cold to inline";
  395. if( PrintInlining ) print_inlining( callee_method, caller_bci, failure_msg);
  396. return NULL;
  397. }
  398. //------------------------------compute_callee_frequency-----------------------
  399. float InlineTree::compute_callee_frequency( int caller_bci ) const {
  400. int count = method()->interpreter_call_site_count(caller_bci);
  401. int invcnt = method()->interpreter_invocation_count();
  402. float freq = (float)count/(float)invcnt;
  403. // Call-site count / interpreter invocation count, scaled recursively.
  404. // Always between 0.0 and 1.0. Represents the percentage of the method's
  405. // total execution time used at this call site.
  406. return freq;
  407. }
  408. //------------------------------build_inline_tree_for_callee-------------------
  409. InlineTree *InlineTree::build_inline_tree_for_callee( ciMethod* callee_method, JVMState* caller_jvms, int caller_bci) {
  410. float recur_frequency = _site_invoke_ratio * compute_callee_frequency(caller_bci);
  411. // Attempt inlining.
  412. InlineTree* old_ilt = callee_at(caller_bci, callee_method);
  413. if (old_ilt != NULL) {
  414. return old_ilt;
  415. }
  416. int new_depth_adjust = 0;
  417. if (caller_jvms->method() != NULL) {
  418. if (caller_jvms->method()->is_method_handle_adapter())
  419. new_depth_adjust -= 1; // don't count actions in MH or indy adapter frames
  420. else if (callee_method->is_method_handle_invoke()) {
  421. new_depth_adjust -= 1; // don't count method handle calls from java.dyn implem
  422. }
  423. if (new_depth_adjust != 0 && PrintInlining) {
  424. stringStream nm1; caller_jvms->method()->print_name(&nm1);
  425. stringStream nm2; callee_method->print_name(&nm2);
  426. tty->print_cr("discounting inlining depth from %s to %s", nm1.base(), nm2.base());
  427. }
  428. if (new_depth_adjust != 0 && C->log()) {
  429. int id1 = C->log()->identify(caller_jvms->method());
  430. int id2 = C->log()->identify(callee_method);
  431. C->log()->elem("inline_depth_discount caller='%d' callee='%d'", id1, id2);
  432. }
  433. }
  434. InlineTree *ilt = new InlineTree(C, this, callee_method, caller_jvms, caller_bci, recur_frequency, _site_depth_adjust + new_depth_adjust);
  435. _subtrees.append( ilt );
  436. NOT_PRODUCT( _count_inlines += 1; )
  437. return ilt;
  438. }
  439. //---------------------------------------callee_at-----------------------------
  440. InlineTree *InlineTree::callee_at(int bci, ciMethod* callee) const {
  441. for (int i = 0; i < _subtrees.length(); i++) {
  442. InlineTree* sub = _subtrees.at(i);
  443. if (sub->caller_bci() == bci && callee == sub->method()) {
  444. return sub;
  445. }
  446. }
  447. return NULL;
  448. }
  449. //------------------------------build_inline_tree_root-------------------------
  450. InlineTree *InlineTree::build_inline_tree_root() {
  451. Compile* C = Compile::current();
  452. // Root of inline tree
  453. InlineTree *ilt = new InlineTree(C, NULL, C->method(), NULL, -1, 1.0F, 0);
  454. return ilt;
  455. }
  456. //-------------------------find_subtree_from_root-----------------------------
  457. // Given a jvms, which determines a call chain from the root method,
  458. // find the corresponding inline tree.
  459. // Note: This method will be removed or replaced as InlineTree goes away.
  460. InlineTree* InlineTree::find_subtree_from_root(InlineTree* root, JVMState* jvms, ciMethod* callee, bool create_if_not_found) {
  461. InlineTree* iltp = root;
  462. uint depth = jvms && jvms->has_method() ? jvms->depth() : 0;
  463. for (uint d = 1; d <= depth; d++) {
  464. JVMState* jvmsp = jvms->of_depth(d);
  465. // Select the corresponding subtree for this bci.
  466. assert(jvmsp->method() == iltp->method(), "tree still in sync");
  467. ciMethod* d_callee = (d == depth) ? callee : jvms->of_depth(d+1)->method();
  468. InlineTree* sub = iltp->callee_at(jvmsp->bci(), d_callee);
  469. if (!sub) {
  470. if (create_if_not_found && d == depth) {
  471. return iltp->build_inline_tree_for_callee(d_callee, jvmsp, jvmsp->bci());
  472. }
  473. assert(sub != NULL, "should be a sub-ilt here");
  474. return NULL;
  475. }
  476. iltp = sub;
  477. }
  478. return iltp;
  479. }