Fri, 20 Aug 2010 23:40:30 -0700
6912064: type profiles need to be exploited more for dynamic language support
Reviewed-by: kvn
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 */
25 #include "incls/_precompiled.incl"
26 #include "incls/_parseHelper.cpp.incl"
28 //------------------------------make_dtrace_method_entry_exit ----------------
29 // Dtrace -- record entry or exit of a method if compiled with dtrace support
30 void GraphKit::make_dtrace_method_entry_exit(ciMethod* method, bool is_entry) {
31 const TypeFunc *call_type = OptoRuntime::dtrace_method_entry_exit_Type();
32 address call_address = is_entry ? CAST_FROM_FN_PTR(address, SharedRuntime::dtrace_method_entry) :
33 CAST_FROM_FN_PTR(address, SharedRuntime::dtrace_method_exit);
34 const char *call_name = is_entry ? "dtrace_method_entry" : "dtrace_method_exit";
36 // Get base of thread-local storage area
37 Node* thread = _gvn.transform( new (C, 1) ThreadLocalNode() );
39 // Get method
40 const TypeInstPtr* method_type = TypeInstPtr::make(TypePtr::Constant, method->klass(), true, method, 0);
41 Node *method_node = _gvn.transform( ConNode::make(C, method_type) );
43 kill_dead_locals();
45 // For some reason, this call reads only raw memory.
46 const TypePtr* raw_adr_type = TypeRawPtr::BOTTOM;
47 make_runtime_call(RC_LEAF | RC_NARROW_MEM,
48 call_type, call_address,
49 call_name, raw_adr_type,
50 thread, method_node);
51 }
54 //=============================================================================
55 //------------------------------do_checkcast-----------------------------------
56 void Parse::do_checkcast() {
57 bool will_link;
58 ciKlass* klass = iter().get_klass(will_link);
60 Node *obj = peek();
62 // Throw uncommon trap if class is not loaded or the value we are casting
63 // _from_ is not loaded, and value is not null. If the value _is_ NULL,
64 // then the checkcast does nothing.
65 const TypeInstPtr *tp = _gvn.type(obj)->isa_instptr();
66 if (!will_link || (tp && !tp->is_loaded())) {
67 if (C->log() != NULL) {
68 if (!will_link) {
69 C->log()->elem("assert_null reason='checkcast' klass='%d'",
70 C->log()->identify(klass));
71 }
72 if (tp && !tp->is_loaded()) {
73 // %%% Cannot happen?
74 C->log()->elem("assert_null reason='checkcast source' klass='%d'",
75 C->log()->identify(tp->klass()));
76 }
77 }
78 do_null_assert(obj, T_OBJECT);
79 assert( stopped() || _gvn.type(peek())->higher_equal(TypePtr::NULL_PTR), "what's left behind is null" );
80 if (!stopped()) {
81 profile_null_checkcast();
82 }
83 return;
84 }
86 Node *res = gen_checkcast(obj, makecon(TypeKlassPtr::make(klass)) );
88 // Pop from stack AFTER gen_checkcast because it can uncommon trap and
89 // the debug info has to be correct.
90 pop();
91 push(res);
92 }
95 //------------------------------do_instanceof----------------------------------
96 void Parse::do_instanceof() {
97 if (stopped()) return;
98 // We would like to return false if class is not loaded, emitting a
99 // dependency, but Java requires instanceof to load its operand.
101 // Throw uncommon trap if class is not loaded
102 bool will_link;
103 ciKlass* klass = iter().get_klass(will_link);
105 if (!will_link) {
106 if (C->log() != NULL) {
107 C->log()->elem("assert_null reason='instanceof' klass='%d'",
108 C->log()->identify(klass));
109 }
110 do_null_assert(peek(), T_OBJECT);
111 assert( stopped() || _gvn.type(peek())->higher_equal(TypePtr::NULL_PTR), "what's left behind is null" );
112 if (!stopped()) {
113 // The object is now known to be null.
114 // Shortcut the effect of gen_instanceof and return "false" directly.
115 pop(); // pop the null
116 push(_gvn.intcon(0)); // push false answer
117 }
118 return;
119 }
121 // Push the bool result back on stack
122 Node* res = gen_instanceof(peek(), makecon(TypeKlassPtr::make(klass)));
124 // Pop from stack AFTER gen_instanceof because it can uncommon trap.
125 pop();
126 push(res);
127 }
129 //------------------------------array_store_check------------------------------
130 // pull array from stack and check that the store is valid
131 void Parse::array_store_check() {
133 // Shorthand access to array store elements
134 Node *obj = stack(_sp-1);
135 Node *idx = stack(_sp-2);
136 Node *ary = stack(_sp-3);
138 if (_gvn.type(obj) == TypePtr::NULL_PTR) {
139 // There's never a type check on null values.
140 // This cutout lets us avoid the uncommon_trap(Reason_array_check)
141 // below, which turns into a performance liability if the
142 // gen_checkcast folds up completely.
143 return;
144 }
146 // Extract the array klass type
147 int klass_offset = oopDesc::klass_offset_in_bytes();
148 Node* p = basic_plus_adr( ary, ary, klass_offset );
149 // p's type is array-of-OOPS plus klass_offset
150 Node* array_klass = _gvn.transform( LoadKlassNode::make(_gvn, immutable_memory(), p, TypeInstPtr::KLASS) );
151 // Get the array klass
152 const TypeKlassPtr *tak = _gvn.type(array_klass)->is_klassptr();
154 // array_klass's type is generally INexact array-of-oop. Heroically
155 // cast the array klass to EXACT array and uncommon-trap if the cast
156 // fails.
157 bool always_see_exact_class = false;
158 if (MonomorphicArrayCheck
159 && !too_many_traps(Deoptimization::Reason_array_check)) {
160 always_see_exact_class = true;
161 // (If no MDO at all, hope for the best, until a trap actually occurs.)
162 }
164 // Is the array klass is exactly its defined type?
165 if (always_see_exact_class && !tak->klass_is_exact()) {
166 // Make a constant out of the inexact array klass
167 const TypeKlassPtr *extak = tak->cast_to_exactness(true)->is_klassptr();
168 Node* con = makecon(extak);
169 Node* cmp = _gvn.transform(new (C, 3) CmpPNode( array_klass, con ));
170 Node* bol = _gvn.transform(new (C, 2) BoolNode( cmp, BoolTest::eq ));
171 Node* ctrl= control();
172 { BuildCutout unless(this, bol, PROB_MAX);
173 uncommon_trap(Deoptimization::Reason_array_check,
174 Deoptimization::Action_maybe_recompile,
175 tak->klass());
176 }
177 if (stopped()) { // MUST uncommon-trap?
178 set_control(ctrl); // Then Don't Do It, just fall into the normal checking
179 } else { // Cast array klass to exactness:
180 // Use the exact constant value we know it is.
181 replace_in_map(array_klass,con);
182 CompileLog* log = C->log();
183 if (log != NULL) {
184 log->elem("cast_up reason='monomorphic_array' from='%d' to='(exact)'",
185 log->identify(tak->klass()));
186 }
187 array_klass = con; // Use cast value moving forward
188 }
189 }
191 // Come here for polymorphic array klasses
193 // Extract the array element class
194 int element_klass_offset = objArrayKlass::element_klass_offset_in_bytes() + sizeof(oopDesc);
195 Node *p2 = basic_plus_adr(array_klass, array_klass, element_klass_offset);
196 Node *a_e_klass = _gvn.transform( LoadKlassNode::make(_gvn, immutable_memory(), p2, tak) );
198 // Check (the hard way) and throw if not a subklass.
199 // Result is ignored, we just need the CFG effects.
200 gen_checkcast( obj, a_e_klass );
201 }
204 void Parse::emit_guard_for_new(ciInstanceKlass* klass) {
205 // Emit guarded new
206 // if (klass->_init_thread != current_thread ||
207 // klass->_init_state != being_initialized)
208 // uncommon_trap
209 Node* cur_thread = _gvn.transform( new (C, 1) ThreadLocalNode() );
210 Node* merge = new (C, 3) RegionNode(3);
211 _gvn.set_type(merge, Type::CONTROL);
212 Node* kls = makecon(TypeKlassPtr::make(klass));
214 Node* init_thread_offset = _gvn.MakeConX(instanceKlass::init_thread_offset_in_bytes() + klassOopDesc::klass_part_offset_in_bytes());
215 Node* adr_node = basic_plus_adr(kls, kls, init_thread_offset);
216 Node* init_thread = make_load(NULL, adr_node, TypeRawPtr::BOTTOM, T_ADDRESS);
217 Node *tst = Bool( CmpP( init_thread, cur_thread), BoolTest::eq);
218 IfNode* iff = create_and_map_if(control(), tst, PROB_ALWAYS, COUNT_UNKNOWN);
219 set_control(IfTrue(iff));
220 merge->set_req(1, IfFalse(iff));
222 Node* init_state_offset = _gvn.MakeConX(instanceKlass::init_state_offset_in_bytes() + klassOopDesc::klass_part_offset_in_bytes());
223 adr_node = basic_plus_adr(kls, kls, init_state_offset);
224 Node* init_state = make_load(NULL, adr_node, TypeInt::INT, T_INT);
225 Node* being_init = _gvn.intcon(instanceKlass::being_initialized);
226 tst = Bool( CmpI( init_state, being_init), BoolTest::eq);
227 iff = create_and_map_if(control(), tst, PROB_ALWAYS, COUNT_UNKNOWN);
228 set_control(IfTrue(iff));
229 merge->set_req(2, IfFalse(iff));
231 PreserveJVMState pjvms(this);
232 record_for_igvn(merge);
233 set_control(merge);
235 uncommon_trap(Deoptimization::Reason_uninitialized,
236 Deoptimization::Action_reinterpret,
237 klass);
238 }
241 //------------------------------do_new-----------------------------------------
242 void Parse::do_new() {
243 kill_dead_locals();
245 bool will_link;
246 ciInstanceKlass* klass = iter().get_klass(will_link)->as_instance_klass();
247 assert(will_link, "_new: typeflow responsibility");
249 // Should initialize, or throw an InstantiationError?
250 if (!klass->is_initialized() && !klass->is_being_initialized() ||
251 klass->is_abstract() || klass->is_interface() ||
252 klass->name() == ciSymbol::java_lang_Class() ||
253 iter().is_unresolved_klass()) {
254 uncommon_trap(Deoptimization::Reason_uninitialized,
255 Deoptimization::Action_reinterpret,
256 klass);
257 return;
258 }
259 if (klass->is_being_initialized()) {
260 emit_guard_for_new(klass);
261 }
263 Node* kls = makecon(TypeKlassPtr::make(klass));
264 Node* obj = new_instance(kls);
266 // Push resultant oop onto stack
267 push(obj);
269 // Keep track of whether opportunities exist for StringBuilder
270 // optimizations.
271 if (OptimizeStringConcat &&
272 (klass == C->env()->StringBuilder_klass() ||
273 klass == C->env()->StringBuffer_klass())) {
274 C->set_has_stringbuilder(true);
275 }
276 }
278 #ifndef PRODUCT
279 //------------------------------dump_map_adr_mem-------------------------------
280 // Debug dump of the mapping from address types to MergeMemNode indices.
281 void Parse::dump_map_adr_mem() const {
282 tty->print_cr("--- Mapping from address types to memory Nodes ---");
283 MergeMemNode *mem = map() == NULL ? NULL : (map()->memory()->is_MergeMem() ?
284 map()->memory()->as_MergeMem() : NULL);
285 for (uint i = 0; i < (uint)C->num_alias_types(); i++) {
286 C->alias_type(i)->print_on(tty);
287 tty->print("\t");
288 // Node mapping, if any
289 if (mem && i < mem->req() && mem->in(i) && mem->in(i) != mem->empty_memory()) {
290 mem->in(i)->dump();
291 } else {
292 tty->cr();
293 }
294 }
295 }
297 #endif
300 //=============================================================================
301 //
302 // parser methods for profiling
305 //----------------------test_counter_against_threshold ------------------------
306 void Parse::test_counter_against_threshold(Node* cnt, int limit) {
307 // Test the counter against the limit and uncommon trap if greater.
309 // This code is largely copied from the range check code in
310 // array_addressing()
312 // Test invocation count vs threshold
313 Node *threshold = makecon(TypeInt::make(limit));
314 Node *chk = _gvn.transform( new (C, 3) CmpUNode( cnt, threshold) );
315 BoolTest::mask btest = BoolTest::lt;
316 Node *tst = _gvn.transform( new (C, 2) BoolNode( chk, btest) );
317 // Branch to failure if threshold exceeded
318 { BuildCutout unless(this, tst, PROB_ALWAYS);
319 uncommon_trap(Deoptimization::Reason_age,
320 Deoptimization::Action_maybe_recompile);
321 }
322 }
324 //----------------------increment_and_test_invocation_counter-------------------
325 void Parse::increment_and_test_invocation_counter(int limit) {
326 if (!count_invocations()) return;
328 // Get the methodOop node.
329 const TypePtr* adr_type = TypeOopPtr::make_from_constant(method());
330 Node *methodOop_node = makecon(adr_type);
332 // Load the interpreter_invocation_counter from the methodOop.
333 int offset = methodOopDesc::interpreter_invocation_counter_offset_in_bytes();
334 Node* adr_node = basic_plus_adr(methodOop_node, methodOop_node, offset);
335 Node* cnt = make_load(NULL, adr_node, TypeInt::INT, T_INT, adr_type);
337 test_counter_against_threshold(cnt, limit);
339 // Add one to the counter and store
340 Node* incr = _gvn.transform(new (C, 3) AddINode(cnt, _gvn.intcon(1)));
341 store_to_memory( NULL, adr_node, incr, T_INT, adr_type );
342 }
344 //----------------------------method_data_addressing---------------------------
345 Node* Parse::method_data_addressing(ciMethodData* md, ciProfileData* data, ByteSize counter_offset, Node* idx, uint stride) {
346 // Get offset within methodDataOop of the data array
347 ByteSize data_offset = methodDataOopDesc::data_offset();
349 // Get cell offset of the ProfileData within data array
350 int cell_offset = md->dp_to_di(data->dp());
352 // Add in counter_offset, the # of bytes into the ProfileData of counter or flag
353 int offset = in_bytes(data_offset) + cell_offset + in_bytes(counter_offset);
355 const TypePtr* adr_type = TypeOopPtr::make_from_constant(md);
356 Node* mdo = makecon(adr_type);
357 Node* ptr = basic_plus_adr(mdo, mdo, offset);
359 if (stride != 0) {
360 Node* str = _gvn.MakeConX(stride);
361 Node* scale = _gvn.transform( new (C, 3) MulXNode( idx, str ) );
362 ptr = _gvn.transform( new (C, 4) AddPNode( mdo, ptr, scale ) );
363 }
365 return ptr;
366 }
368 //--------------------------increment_md_counter_at----------------------------
369 void Parse::increment_md_counter_at(ciMethodData* md, ciProfileData* data, ByteSize counter_offset, Node* idx, uint stride) {
370 Node* adr_node = method_data_addressing(md, data, counter_offset, idx, stride);
372 const TypePtr* adr_type = _gvn.type(adr_node)->is_ptr();
373 Node* cnt = make_load(NULL, adr_node, TypeInt::INT, T_INT, adr_type);
374 Node* incr = _gvn.transform(new (C, 3) AddINode(cnt, _gvn.intcon(DataLayout::counter_increment)));
375 store_to_memory(NULL, adr_node, incr, T_INT, adr_type );
376 }
378 //--------------------------test_for_osr_md_counter_at-------------------------
379 void Parse::test_for_osr_md_counter_at(ciMethodData* md, ciProfileData* data, ByteSize counter_offset, int limit) {
380 Node* adr_node = method_data_addressing(md, data, counter_offset);
382 const TypePtr* adr_type = _gvn.type(adr_node)->is_ptr();
383 Node* cnt = make_load(NULL, adr_node, TypeInt::INT, T_INT, adr_type);
385 test_counter_against_threshold(cnt, limit);
386 }
388 //-------------------------------set_md_flag_at--------------------------------
389 void Parse::set_md_flag_at(ciMethodData* md, ciProfileData* data, int flag_constant) {
390 Node* adr_node = method_data_addressing(md, data, DataLayout::flags_offset());
392 const TypePtr* adr_type = _gvn.type(adr_node)->is_ptr();
393 Node* flags = make_load(NULL, adr_node, TypeInt::BYTE, T_BYTE, adr_type);
394 Node* incr = _gvn.transform(new (C, 3) OrINode(flags, _gvn.intcon(flag_constant)));
395 store_to_memory(NULL, adr_node, incr, T_BYTE, adr_type);
396 }
398 //----------------------------profile_taken_branch-----------------------------
399 void Parse::profile_taken_branch(int target_bci, bool force_update) {
400 // This is a potential osr_site if we have a backedge.
401 int cur_bci = bci();
402 bool osr_site =
403 (target_bci <= cur_bci) && count_invocations() && UseOnStackReplacement;
405 // If we are going to OSR, restart at the target bytecode.
406 set_bci(target_bci);
408 // To do: factor out the the limit calculations below. These duplicate
409 // the similar limit calculations in the interpreter.
411 if (method_data_update() || force_update) {
412 ciMethodData* md = method()->method_data();
413 assert(md != NULL, "expected valid ciMethodData");
414 ciProfileData* data = md->bci_to_data(cur_bci);
415 assert(data->is_JumpData(), "need JumpData for taken branch");
416 increment_md_counter_at(md, data, JumpData::taken_offset());
417 }
419 // In the new tiered system this is all we need to do. In the old
420 // (c2 based) tiered sytem we must do the code below.
421 #ifndef TIERED
422 if (method_data_update()) {
423 ciMethodData* md = method()->method_data();
424 if (osr_site) {
425 ciProfileData* data = md->bci_to_data(cur_bci);
426 int limit = (CompileThreshold
427 * (OnStackReplacePercentage - InterpreterProfilePercentage)) / 100;
428 test_for_osr_md_counter_at(md, data, JumpData::taken_offset(), limit);
429 }
430 } else {
431 // With method data update off, use the invocation counter to trigger an
432 // OSR compilation, as done in the interpreter.
433 if (osr_site) {
434 int limit = (CompileThreshold * OnStackReplacePercentage) / 100;
435 increment_and_test_invocation_counter(limit);
436 }
437 }
438 #endif // TIERED
440 // Restore the original bytecode.
441 set_bci(cur_bci);
442 }
444 //--------------------------profile_not_taken_branch---------------------------
445 void Parse::profile_not_taken_branch(bool force_update) {
447 if (method_data_update() || force_update) {
448 ciMethodData* md = method()->method_data();
449 assert(md != NULL, "expected valid ciMethodData");
450 ciProfileData* data = md->bci_to_data(bci());
451 assert(data->is_BranchData(), "need BranchData for not taken branch");
452 increment_md_counter_at(md, data, BranchData::not_taken_offset());
453 }
455 }
457 //---------------------------------profile_call--------------------------------
458 void Parse::profile_call(Node* receiver) {
459 if (!method_data_update()) return;
461 switch (bc()) {
462 case Bytecodes::_invokevirtual:
463 case Bytecodes::_invokeinterface:
464 profile_receiver_type(receiver);
465 break;
466 case Bytecodes::_invokestatic:
467 case Bytecodes::_invokedynamic:
468 case Bytecodes::_invokespecial:
469 profile_generic_call();
470 break;
471 default: fatal("unexpected call bytecode");
472 }
473 }
475 //------------------------------profile_generic_call---------------------------
476 void Parse::profile_generic_call() {
477 assert(method_data_update(), "must be generating profile code");
479 ciMethodData* md = method()->method_data();
480 assert(md != NULL, "expected valid ciMethodData");
481 ciProfileData* data = md->bci_to_data(bci());
482 assert(data->is_CounterData(), "need CounterData for not taken branch");
483 increment_md_counter_at(md, data, CounterData::count_offset());
484 }
486 //-----------------------------profile_receiver_type---------------------------
487 void Parse::profile_receiver_type(Node* receiver) {
488 assert(method_data_update(), "must be generating profile code");
490 ciMethodData* md = method()->method_data();
491 assert(md != NULL, "expected valid ciMethodData");
492 ciProfileData* data = md->bci_to_data(bci());
493 assert(data->is_ReceiverTypeData(), "need ReceiverTypeData here");
495 // Skip if we aren't tracking receivers
496 if (TypeProfileWidth < 1) {
497 increment_md_counter_at(md, data, CounterData::count_offset());
498 return;
499 }
500 ciReceiverTypeData* rdata = (ciReceiverTypeData*)data->as_ReceiverTypeData();
502 Node* method_data = method_data_addressing(md, rdata, in_ByteSize(0));
504 // Using an adr_type of TypePtr::BOTTOM to work around anti-dep problems.
505 // A better solution might be to use TypeRawPtr::BOTTOM with RC_NARROW_MEM.
506 make_runtime_call(RC_LEAF, OptoRuntime::profile_receiver_type_Type(),
507 CAST_FROM_FN_PTR(address,
508 OptoRuntime::profile_receiver_type_C),
509 "profile_receiver_type_C",
510 TypePtr::BOTTOM,
511 method_data, receiver);
512 }
514 //---------------------------------profile_ret---------------------------------
515 void Parse::profile_ret(int target_bci) {
516 if (!method_data_update()) return;
518 // Skip if we aren't tracking ret targets
519 if (TypeProfileWidth < 1) return;
521 ciMethodData* md = method()->method_data();
522 assert(md != NULL, "expected valid ciMethodData");
523 ciProfileData* data = md->bci_to_data(bci());
524 assert(data->is_RetData(), "need RetData for ret");
525 ciRetData* ret_data = (ciRetData*)data->as_RetData();
527 // Look for the target_bci is already in the table
528 uint row;
529 bool table_full = true;
530 for (row = 0; row < ret_data->row_limit(); row++) {
531 int key = ret_data->bci(row);
532 table_full &= (key != RetData::no_bci);
533 if (key == target_bci) break;
534 }
536 if (row >= ret_data->row_limit()) {
537 // The target_bci was not found in the table.
538 if (!table_full) {
539 // XXX: Make slow call to update RetData
540 }
541 return;
542 }
544 // the target_bci is already in the table
545 increment_md_counter_at(md, data, RetData::bci_count_offset(row));
546 }
548 //--------------------------profile_null_checkcast----------------------------
549 void Parse::profile_null_checkcast() {
550 // Set the null-seen flag, done in conjunction with the usual null check. We
551 // never unset the flag, so this is a one-way switch.
552 if (!method_data_update()) return;
554 ciMethodData* md = method()->method_data();
555 assert(md != NULL, "expected valid ciMethodData");
556 ciProfileData* data = md->bci_to_data(bci());
557 assert(data->is_BitData(), "need BitData for checkcast");
558 set_md_flag_at(md, data, BitData::null_seen_byte_constant());
559 }
561 //-----------------------------profile_switch_case-----------------------------
562 void Parse::profile_switch_case(int table_index) {
563 if (!method_data_update()) return;
565 ciMethodData* md = method()->method_data();
566 assert(md != NULL, "expected valid ciMethodData");
568 ciProfileData* data = md->bci_to_data(bci());
569 assert(data->is_MultiBranchData(), "need MultiBranchData for switch case");
570 if (table_index >= 0) {
571 increment_md_counter_at(md, data, MultiBranchData::case_count_offset(table_index));
572 } else {
573 increment_md_counter_at(md, data, MultiBranchData::default_count_offset());
574 }
575 }