src/share/vm/opto/parseHelper.cpp

Wed, 03 Jun 2015 14:22:57 +0200

author
roland
date
Wed, 03 Jun 2015 14:22:57 +0200
changeset 7859
c1c199dde5c9
parent 7341
e7b3d177adda
child 7535
7ae4e26cb1e0
permissions
-rw-r--r--

8077504: Unsafe load can loose control dependency and cause crash
Summary: Node::depends_only_on_test() should return false for Unsafe loads
Reviewed-by: kvn, adinn

duke@435 1 /*
jiangli@4936 2 * Copyright (c) 1998, 2013, Oracle and/or its affiliates. All rights reserved.
duke@435 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@435 4 *
duke@435 5 * This code is free software; you can redistribute it and/or modify it
duke@435 6 * under the terms of the GNU General Public License version 2 only, as
duke@435 7 * published by the Free Software Foundation.
duke@435 8 *
duke@435 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@435 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@435 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@435 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@435 13 * accompanied this code).
duke@435 14 *
duke@435 15 * You should have received a copy of the GNU General Public License version
duke@435 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@435 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@435 18 *
trims@1907 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
trims@1907 20 * or visit www.oracle.com if you need additional information or have any
trims@1907 21 * questions.
duke@435 22 *
duke@435 23 */
duke@435 24
stefank@2314 25 #include "precompiled.hpp"
stefank@2314 26 #include "classfile/systemDictionary.hpp"
stefank@2314 27 #include "compiler/compileLog.hpp"
stefank@2314 28 #include "oops/objArrayKlass.hpp"
stefank@2314 29 #include "opto/addnode.hpp"
stefank@2314 30 #include "opto/memnode.hpp"
stefank@2314 31 #include "opto/mulnode.hpp"
stefank@2314 32 #include "opto/parse.hpp"
stefank@2314 33 #include "opto/rootnode.hpp"
stefank@2314 34 #include "opto/runtime.hpp"
stefank@2314 35 #include "runtime/sharedRuntime.hpp"
duke@435 36
duke@435 37 //------------------------------make_dtrace_method_entry_exit ----------------
duke@435 38 // Dtrace -- record entry or exit of a method if compiled with dtrace support
duke@435 39 void GraphKit::make_dtrace_method_entry_exit(ciMethod* method, bool is_entry) {
duke@435 40 const TypeFunc *call_type = OptoRuntime::dtrace_method_entry_exit_Type();
duke@435 41 address call_address = is_entry ? CAST_FROM_FN_PTR(address, SharedRuntime::dtrace_method_entry) :
duke@435 42 CAST_FROM_FN_PTR(address, SharedRuntime::dtrace_method_exit);
duke@435 43 const char *call_name = is_entry ? "dtrace_method_entry" : "dtrace_method_exit";
duke@435 44
duke@435 45 // Get base of thread-local storage area
kvn@4115 46 Node* thread = _gvn.transform( new (C) ThreadLocalNode() );
duke@435 47
duke@435 48 // Get method
coleenp@4037 49 const TypePtr* method_type = TypeMetadataPtr::make(method);
kvn@599 50 Node *method_node = _gvn.transform( ConNode::make(C, method_type) );
duke@435 51
duke@435 52 kill_dead_locals();
duke@435 53
duke@435 54 // For some reason, this call reads only raw memory.
duke@435 55 const TypePtr* raw_adr_type = TypeRawPtr::BOTTOM;
duke@435 56 make_runtime_call(RC_LEAF | RC_NARROW_MEM,
duke@435 57 call_type, call_address,
duke@435 58 call_name, raw_adr_type,
duke@435 59 thread, method_node);
duke@435 60 }
duke@435 61
duke@435 62
duke@435 63 //=============================================================================
duke@435 64 //------------------------------do_checkcast-----------------------------------
duke@435 65 void Parse::do_checkcast() {
duke@435 66 bool will_link;
duke@435 67 ciKlass* klass = iter().get_klass(will_link);
duke@435 68
duke@435 69 Node *obj = peek();
duke@435 70
duke@435 71 // Throw uncommon trap if class is not loaded or the value we are casting
duke@435 72 // _from_ is not loaded, and value is not null. If the value _is_ NULL,
duke@435 73 // then the checkcast does nothing.
roland@3450 74 const TypeOopPtr *tp = _gvn.type(obj)->isa_oopptr();
roland@3450 75 if (!will_link || (tp && tp->klass() && !tp->klass()->is_loaded())) {
duke@435 76 if (C->log() != NULL) {
duke@435 77 if (!will_link) {
duke@435 78 C->log()->elem("assert_null reason='checkcast' klass='%d'",
duke@435 79 C->log()->identify(klass));
duke@435 80 }
roland@3450 81 if (tp && tp->klass() && !tp->klass()->is_loaded()) {
duke@435 82 // %%% Cannot happen?
duke@435 83 C->log()->elem("assert_null reason='checkcast source' klass='%d'",
duke@435 84 C->log()->identify(tp->klass()));
duke@435 85 }
duke@435 86 }
twisti@4313 87 null_assert(obj);
duke@435 88 assert( stopped() || _gvn.type(peek())->higher_equal(TypePtr::NULL_PTR), "what's left behind is null" );
duke@435 89 if (!stopped()) {
duke@435 90 profile_null_checkcast();
duke@435 91 }
duke@435 92 return;
duke@435 93 }
duke@435 94
duke@435 95 Node *res = gen_checkcast(obj, makecon(TypeKlassPtr::make(klass)) );
duke@435 96
duke@435 97 // Pop from stack AFTER gen_checkcast because it can uncommon trap and
duke@435 98 // the debug info has to be correct.
duke@435 99 pop();
duke@435 100 push(res);
duke@435 101 }
duke@435 102
duke@435 103
duke@435 104 //------------------------------do_instanceof----------------------------------
duke@435 105 void Parse::do_instanceof() {
duke@435 106 if (stopped()) return;
duke@435 107 // We would like to return false if class is not loaded, emitting a
duke@435 108 // dependency, but Java requires instanceof to load its operand.
duke@435 109
duke@435 110 // Throw uncommon trap if class is not loaded
duke@435 111 bool will_link;
duke@435 112 ciKlass* klass = iter().get_klass(will_link);
duke@435 113
duke@435 114 if (!will_link) {
duke@435 115 if (C->log() != NULL) {
duke@435 116 C->log()->elem("assert_null reason='instanceof' klass='%d'",
duke@435 117 C->log()->identify(klass));
duke@435 118 }
twisti@4313 119 null_assert(peek());
duke@435 120 assert( stopped() || _gvn.type(peek())->higher_equal(TypePtr::NULL_PTR), "what's left behind is null" );
duke@435 121 if (!stopped()) {
duke@435 122 // The object is now known to be null.
duke@435 123 // Shortcut the effect of gen_instanceof and return "false" directly.
duke@435 124 pop(); // pop the null
duke@435 125 push(_gvn.intcon(0)); // push false answer
duke@435 126 }
duke@435 127 return;
duke@435 128 }
duke@435 129
duke@435 130 // Push the bool result back on stack
roland@5991 131 Node* res = gen_instanceof(peek(), makecon(TypeKlassPtr::make(klass)), true);
jrose@2101 132
jrose@2101 133 // Pop from stack AFTER gen_instanceof because it can uncommon trap.
jrose@2101 134 pop();
jrose@2101 135 push(res);
duke@435 136 }
duke@435 137
duke@435 138 //------------------------------array_store_check------------------------------
duke@435 139 // pull array from stack and check that the store is valid
duke@435 140 void Parse::array_store_check() {
duke@435 141
twisti@4313 142 // Shorthand access to array store elements without popping them.
twisti@4313 143 Node *obj = peek(0);
twisti@4313 144 Node *idx = peek(1);
twisti@4313 145 Node *ary = peek(2);
duke@435 146
duke@435 147 if (_gvn.type(obj) == TypePtr::NULL_PTR) {
duke@435 148 // There's never a type check on null values.
duke@435 149 // This cutout lets us avoid the uncommon_trap(Reason_array_check)
duke@435 150 // below, which turns into a performance liability if the
duke@435 151 // gen_checkcast folds up completely.
duke@435 152 return;
duke@435 153 }
duke@435 154
duke@435 155 // Extract the array klass type
duke@435 156 int klass_offset = oopDesc::klass_offset_in_bytes();
duke@435 157 Node* p = basic_plus_adr( ary, ary, klass_offset );
duke@435 158 // p's type is array-of-OOPS plus klass_offset
zmajo@7341 159 Node* array_klass = _gvn.transform(LoadKlassNode::make(_gvn, NULL, immutable_memory(), p, TypeInstPtr::KLASS));
duke@435 160 // Get the array klass
duke@435 161 const TypeKlassPtr *tak = _gvn.type(array_klass)->is_klassptr();
duke@435 162
zmajo@7341 163 // The type of array_klass is usually INexact array-of-oop. Heroically
zmajo@7341 164 // cast array_klass to EXACT array and uncommon-trap if the cast fails.
zmajo@7341 165 // Make constant out of the inexact array klass, but use it only if the cast
zmajo@7341 166 // succeeds.
duke@435 167 bool always_see_exact_class = false;
duke@435 168 if (MonomorphicArrayCheck
zmajo@7341 169 && !too_many_traps(Deoptimization::Reason_array_check)
zmajo@7341 170 && !tak->klass_is_exact()
zmajo@7341 171 && tak != TypeKlassPtr::OBJECT) {
zmajo@7341 172 // Regarding the fourth condition in the if-statement from above:
zmajo@7341 173 //
zmajo@7341 174 // If the compiler has determined that the type of array 'ary' (represented
zmajo@7341 175 // by 'array_klass') is java/lang/Object, the compiler must not assume that
zmajo@7341 176 // the array 'ary' is monomorphic.
zmajo@7341 177 //
zmajo@7341 178 // If 'ary' were of type java/lang/Object, this arraystore would have to fail,
zmajo@7341 179 // because it is not possible to perform a arraystore into an object that is not
zmajo@7341 180 // a "proper" array.
zmajo@7341 181 //
zmajo@7341 182 // Therefore, let's obtain at runtime the type of 'ary' and check if we can still
zmajo@7341 183 // successfully perform the store.
zmajo@7341 184 //
zmajo@7341 185 // The implementation reasons for the condition are the following:
zmajo@7341 186 //
zmajo@7341 187 // java/lang/Object is the superclass of all arrays, but it is represented by the VM
zmajo@7341 188 // as an InstanceKlass. The checks generated by gen_checkcast() (see below) expect
zmajo@7341 189 // 'array_klass' to be ObjArrayKlass, which can result in invalid memory accesses.
zmajo@7341 190 //
zmajo@7341 191 // See issue JDK-8057622 for details.
zmajo@7341 192
duke@435 193 always_see_exact_class = true;
duke@435 194 // (If no MDO at all, hope for the best, until a trap actually occurs.)
duke@435 195
duke@435 196 // Make a constant out of the inexact array klass
duke@435 197 const TypeKlassPtr *extak = tak->cast_to_exactness(true)->is_klassptr();
duke@435 198 Node* con = makecon(extak);
kvn@4115 199 Node* cmp = _gvn.transform(new (C) CmpPNode( array_klass, con ));
kvn@4115 200 Node* bol = _gvn.transform(new (C) BoolNode( cmp, BoolTest::eq ));
duke@435 201 Node* ctrl= control();
duke@435 202 { BuildCutout unless(this, bol, PROB_MAX);
duke@435 203 uncommon_trap(Deoptimization::Reason_array_check,
duke@435 204 Deoptimization::Action_maybe_recompile,
duke@435 205 tak->klass());
duke@435 206 }
duke@435 207 if (stopped()) { // MUST uncommon-trap?
duke@435 208 set_control(ctrl); // Then Don't Do It, just fall into the normal checking
duke@435 209 } else { // Cast array klass to exactness:
duke@435 210 // Use the exact constant value we know it is.
duke@435 211 replace_in_map(array_klass,con);
duke@435 212 CompileLog* log = C->log();
duke@435 213 if (log != NULL) {
duke@435 214 log->elem("cast_up reason='monomorphic_array' from='%d' to='(exact)'",
duke@435 215 log->identify(tak->klass()));
duke@435 216 }
duke@435 217 array_klass = con; // Use cast value moving forward
duke@435 218 }
duke@435 219 }
duke@435 220
duke@435 221 // Come here for polymorphic array klasses
duke@435 222
duke@435 223 // Extract the array element class
coleenp@4142 224 int element_klass_offset = in_bytes(ObjArrayKlass::element_klass_offset());
duke@435 225 Node *p2 = basic_plus_adr(array_klass, array_klass, element_klass_offset);
zmajo@7341 226 // We are allowed to use the constant type only if cast succeeded. If always_see_exact_class is true,
zmajo@7341 227 // we must set a control edge from the IfTrue node created by the uncommon_trap above to the
zmajo@7341 228 // LoadKlassNode.
zmajo@7341 229 Node* a_e_klass = _gvn.transform(LoadKlassNode::make(_gvn, always_see_exact_class ? control() : NULL,
zmajo@7341 230 immutable_memory(), p2, tak));
duke@435 231
duke@435 232 // Check (the hard way) and throw if not a subklass.
duke@435 233 // Result is ignored, we just need the CFG effects.
zmajo@7341 234 gen_checkcast(obj, a_e_klass);
duke@435 235 }
duke@435 236
duke@435 237
never@2000 238 void Parse::emit_guard_for_new(ciInstanceKlass* klass) {
never@2000 239 // Emit guarded new
never@2000 240 // if (klass->_init_thread != current_thread ||
never@2000 241 // klass->_init_state != being_initialized)
never@2000 242 // uncommon_trap
kvn@4115 243 Node* cur_thread = _gvn.transform( new (C) ThreadLocalNode() );
kvn@4115 244 Node* merge = new (C) RegionNode(3);
never@2000 245 _gvn.set_type(merge, Type::CONTROL);
never@2000 246 Node* kls = makecon(TypeKlassPtr::make(klass));
never@2000 247
coleenp@4037 248 Node* init_thread_offset = _gvn.MakeConX(in_bytes(InstanceKlass::init_thread_offset()));
never@2000 249 Node* adr_node = basic_plus_adr(kls, kls, init_thread_offset);
goetz@6479 250 Node* init_thread = make_load(NULL, adr_node, TypeRawPtr::BOTTOM, T_ADDRESS, MemNode::unordered);
never@2000 251 Node *tst = Bool( CmpP( init_thread, cur_thread), BoolTest::eq);
never@2000 252 IfNode* iff = create_and_map_if(control(), tst, PROB_ALWAYS, COUNT_UNKNOWN);
never@2000 253 set_control(IfTrue(iff));
never@2000 254 merge->set_req(1, IfFalse(iff));
never@2000 255
coleenp@4037 256 Node* init_state_offset = _gvn.MakeConX(in_bytes(InstanceKlass::init_state_offset()));
never@2000 257 adr_node = basic_plus_adr(kls, kls, init_state_offset);
coleenp@4037 258 // Use T_BOOLEAN for InstanceKlass::_init_state so the compiler
coleenp@3368 259 // can generate code to load it as unsigned byte.
goetz@6479 260 Node* init_state = make_load(NULL, adr_node, TypeInt::UBYTE, T_BOOLEAN, MemNode::unordered);
coleenp@4037 261 Node* being_init = _gvn.intcon(InstanceKlass::being_initialized);
never@2000 262 tst = Bool( CmpI( init_state, being_init), BoolTest::eq);
never@2000 263 iff = create_and_map_if(control(), tst, PROB_ALWAYS, COUNT_UNKNOWN);
never@2000 264 set_control(IfTrue(iff));
never@2000 265 merge->set_req(2, IfFalse(iff));
never@2000 266
never@2000 267 PreserveJVMState pjvms(this);
never@2000 268 record_for_igvn(merge);
never@2000 269 set_control(merge);
never@2000 270
never@2000 271 uncommon_trap(Deoptimization::Reason_uninitialized,
never@2000 272 Deoptimization::Action_reinterpret,
never@2000 273 klass);
never@2000 274 }
never@2000 275
never@2000 276
duke@435 277 //------------------------------do_new-----------------------------------------
duke@435 278 void Parse::do_new() {
duke@435 279 kill_dead_locals();
duke@435 280
duke@435 281 bool will_link;
duke@435 282 ciInstanceKlass* klass = iter().get_klass(will_link)->as_instance_klass();
duke@435 283 assert(will_link, "_new: typeflow responsibility");
duke@435 284
duke@435 285 // Should initialize, or throw an InstantiationError?
never@2000 286 if (!klass->is_initialized() && !klass->is_being_initialized() ||
duke@435 287 klass->is_abstract() || klass->is_interface() ||
duke@435 288 klass->name() == ciSymbol::java_lang_Class() ||
duke@435 289 iter().is_unresolved_klass()) {
duke@435 290 uncommon_trap(Deoptimization::Reason_uninitialized,
duke@435 291 Deoptimization::Action_reinterpret,
duke@435 292 klass);
duke@435 293 return;
duke@435 294 }
never@2000 295 if (klass->is_being_initialized()) {
never@2000 296 emit_guard_for_new(klass);
never@2000 297 }
duke@435 298
duke@435 299 Node* kls = makecon(TypeKlassPtr::make(klass));
duke@435 300 Node* obj = new_instance(kls);
duke@435 301
duke@435 302 // Push resultant oop onto stack
duke@435 303 push(obj);
never@1515 304
never@1515 305 // Keep track of whether opportunities exist for StringBuilder
never@1515 306 // optimizations.
never@1515 307 if (OptimizeStringConcat &&
never@1515 308 (klass == C->env()->StringBuilder_klass() ||
never@1515 309 klass == C->env()->StringBuffer_klass())) {
never@1515 310 C->set_has_stringbuilder(true);
never@1515 311 }
kvn@5110 312
kvn@5110 313 // Keep track of boxed values for EliminateAutoBox optimizations.
kvn@5110 314 if (C->eliminate_boxing() && klass->is_box_klass()) {
kvn@5110 315 C->set_has_boxed_value(true);
kvn@5110 316 }
duke@435 317 }
duke@435 318
duke@435 319 #ifndef PRODUCT
duke@435 320 //------------------------------dump_map_adr_mem-------------------------------
duke@435 321 // Debug dump of the mapping from address types to MergeMemNode indices.
duke@435 322 void Parse::dump_map_adr_mem() const {
duke@435 323 tty->print_cr("--- Mapping from address types to memory Nodes ---");
duke@435 324 MergeMemNode *mem = map() == NULL ? NULL : (map()->memory()->is_MergeMem() ?
duke@435 325 map()->memory()->as_MergeMem() : NULL);
duke@435 326 for (uint i = 0; i < (uint)C->num_alias_types(); i++) {
duke@435 327 C->alias_type(i)->print_on(tty);
duke@435 328 tty->print("\t");
duke@435 329 // Node mapping, if any
duke@435 330 if (mem && i < mem->req() && mem->in(i) && mem->in(i) != mem->empty_memory()) {
duke@435 331 mem->in(i)->dump();
duke@435 332 } else {
duke@435 333 tty->cr();
duke@435 334 }
duke@435 335 }
duke@435 336 }
duke@435 337
duke@435 338 #endif
duke@435 339
duke@435 340
duke@435 341 //=============================================================================
duke@435 342 //
duke@435 343 // parser methods for profiling
duke@435 344
duke@435 345
duke@435 346 //----------------------test_counter_against_threshold ------------------------
duke@435 347 void Parse::test_counter_against_threshold(Node* cnt, int limit) {
duke@435 348 // Test the counter against the limit and uncommon trap if greater.
duke@435 349
duke@435 350 // This code is largely copied from the range check code in
duke@435 351 // array_addressing()
duke@435 352
duke@435 353 // Test invocation count vs threshold
duke@435 354 Node *threshold = makecon(TypeInt::make(limit));
kvn@4115 355 Node *chk = _gvn.transform( new (C) CmpUNode( cnt, threshold) );
duke@435 356 BoolTest::mask btest = BoolTest::lt;
kvn@4115 357 Node *tst = _gvn.transform( new (C) BoolNode( chk, btest) );
duke@435 358 // Branch to failure if threshold exceeded
duke@435 359 { BuildCutout unless(this, tst, PROB_ALWAYS);
duke@435 360 uncommon_trap(Deoptimization::Reason_age,
duke@435 361 Deoptimization::Action_maybe_recompile);
duke@435 362 }
duke@435 363 }
duke@435 364
duke@435 365 //----------------------increment_and_test_invocation_counter-------------------
duke@435 366 void Parse::increment_and_test_invocation_counter(int limit) {
duke@435 367 if (!count_invocations()) return;
duke@435 368
coleenp@4037 369 // Get the Method* node.
jiangli@4936 370 ciMethod* m = method();
twisti@5907 371 MethodCounters* counters_adr = m->ensure_method_counters();
twisti@5907 372 if (counters_adr == NULL) {
twisti@5907 373 C->record_failure("method counters allocation failed");
twisti@5907 374 return;
twisti@5907 375 }
duke@435 376
jiangli@4936 377 Node* ctrl = control();
twisti@5907 378 const TypePtr* adr_type = TypeRawPtr::make((address) counters_adr);
jiangli@4936 379 Node *counters_node = makecon(adr_type);
jiangli@4936 380 Node* adr_iic_node = basic_plus_adr(counters_node, counters_node,
jiangli@4936 381 MethodCounters::interpreter_invocation_counter_offset_in_bytes());
goetz@6479 382 Node* cnt = make_load(ctrl, adr_iic_node, TypeInt::INT, T_INT, adr_type, MemNode::unordered);
duke@435 383
duke@435 384 test_counter_against_threshold(cnt, limit);
duke@435 385
duke@435 386 // Add one to the counter and store
kvn@4115 387 Node* incr = _gvn.transform(new (C) AddINode(cnt, _gvn.intcon(1)));
goetz@6479 388 store_to_memory(ctrl, adr_iic_node, incr, T_INT, adr_type, MemNode::unordered);
duke@435 389 }
duke@435 390
duke@435 391 //----------------------------method_data_addressing---------------------------
duke@435 392 Node* Parse::method_data_addressing(ciMethodData* md, ciProfileData* data, ByteSize counter_offset, Node* idx, uint stride) {
coleenp@4037 393 // Get offset within MethodData* of the data array
coleenp@4037 394 ByteSize data_offset = MethodData::data_offset();
duke@435 395
duke@435 396 // Get cell offset of the ProfileData within data array
duke@435 397 int cell_offset = md->dp_to_di(data->dp());
duke@435 398
duke@435 399 // Add in counter_offset, the # of bytes into the ProfileData of counter or flag
duke@435 400 int offset = in_bytes(data_offset) + cell_offset + in_bytes(counter_offset);
duke@435 401
coleenp@4037 402 const TypePtr* adr_type = TypeMetadataPtr::make(md);
duke@435 403 Node* mdo = makecon(adr_type);
duke@435 404 Node* ptr = basic_plus_adr(mdo, mdo, offset);
duke@435 405
duke@435 406 if (stride != 0) {
duke@435 407 Node* str = _gvn.MakeConX(stride);
kvn@4115 408 Node* scale = _gvn.transform( new (C) MulXNode( idx, str ) );
kvn@4115 409 ptr = _gvn.transform( new (C) AddPNode( mdo, ptr, scale ) );
duke@435 410 }
duke@435 411
duke@435 412 return ptr;
duke@435 413 }
duke@435 414
duke@435 415 //--------------------------increment_md_counter_at----------------------------
duke@435 416 void Parse::increment_md_counter_at(ciMethodData* md, ciProfileData* data, ByteSize counter_offset, Node* idx, uint stride) {
duke@435 417 Node* adr_node = method_data_addressing(md, data, counter_offset, idx, stride);
duke@435 418
duke@435 419 const TypePtr* adr_type = _gvn.type(adr_node)->is_ptr();
goetz@6479 420 Node* cnt = make_load(NULL, adr_node, TypeInt::INT, T_INT, adr_type, MemNode::unordered);
kvn@4115 421 Node* incr = _gvn.transform(new (C) AddINode(cnt, _gvn.intcon(DataLayout::counter_increment)));
goetz@6479 422 store_to_memory(NULL, adr_node, incr, T_INT, adr_type, MemNode::unordered);
duke@435 423 }
duke@435 424
duke@435 425 //--------------------------test_for_osr_md_counter_at-------------------------
duke@435 426 void Parse::test_for_osr_md_counter_at(ciMethodData* md, ciProfileData* data, ByteSize counter_offset, int limit) {
duke@435 427 Node* adr_node = method_data_addressing(md, data, counter_offset);
duke@435 428
duke@435 429 const TypePtr* adr_type = _gvn.type(adr_node)->is_ptr();
goetz@6479 430 Node* cnt = make_load(NULL, adr_node, TypeInt::INT, T_INT, adr_type, MemNode::unordered);
duke@435 431
duke@435 432 test_counter_against_threshold(cnt, limit);
duke@435 433 }
duke@435 434
duke@435 435 //-------------------------------set_md_flag_at--------------------------------
duke@435 436 void Parse::set_md_flag_at(ciMethodData* md, ciProfileData* data, int flag_constant) {
duke@435 437 Node* adr_node = method_data_addressing(md, data, DataLayout::flags_offset());
duke@435 438
duke@435 439 const TypePtr* adr_type = _gvn.type(adr_node)->is_ptr();
goetz@6479 440 Node* flags = make_load(NULL, adr_node, TypeInt::BYTE, T_BYTE, adr_type, MemNode::unordered);
kvn@4115 441 Node* incr = _gvn.transform(new (C) OrINode(flags, _gvn.intcon(flag_constant)));
goetz@6479 442 store_to_memory(NULL, adr_node, incr, T_BYTE, adr_type, MemNode::unordered);
duke@435 443 }
duke@435 444
duke@435 445 //----------------------------profile_taken_branch-----------------------------
duke@435 446 void Parse::profile_taken_branch(int target_bci, bool force_update) {
duke@435 447 // This is a potential osr_site if we have a backedge.
duke@435 448 int cur_bci = bci();
duke@435 449 bool osr_site =
duke@435 450 (target_bci <= cur_bci) && count_invocations() && UseOnStackReplacement;
duke@435 451
duke@435 452 // If we are going to OSR, restart at the target bytecode.
duke@435 453 set_bci(target_bci);
duke@435 454
duke@435 455 // To do: factor out the the limit calculations below. These duplicate
duke@435 456 // the similar limit calculations in the interpreter.
duke@435 457
duke@435 458 if (method_data_update() || force_update) {
duke@435 459 ciMethodData* md = method()->method_data();
duke@435 460 assert(md != NULL, "expected valid ciMethodData");
duke@435 461 ciProfileData* data = md->bci_to_data(cur_bci);
duke@435 462 assert(data->is_JumpData(), "need JumpData for taken branch");
duke@435 463 increment_md_counter_at(md, data, JumpData::taken_offset());
duke@435 464 }
duke@435 465
duke@435 466 // In the new tiered system this is all we need to do. In the old
duke@435 467 // (c2 based) tiered sytem we must do the code below.
duke@435 468 #ifndef TIERED
duke@435 469 if (method_data_update()) {
duke@435 470 ciMethodData* md = method()->method_data();
duke@435 471 if (osr_site) {
duke@435 472 ciProfileData* data = md->bci_to_data(cur_bci);
duke@435 473 int limit = (CompileThreshold
duke@435 474 * (OnStackReplacePercentage - InterpreterProfilePercentage)) / 100;
duke@435 475 test_for_osr_md_counter_at(md, data, JumpData::taken_offset(), limit);
duke@435 476 }
duke@435 477 } else {
duke@435 478 // With method data update off, use the invocation counter to trigger an
duke@435 479 // OSR compilation, as done in the interpreter.
duke@435 480 if (osr_site) {
duke@435 481 int limit = (CompileThreshold * OnStackReplacePercentage) / 100;
duke@435 482 increment_and_test_invocation_counter(limit);
duke@435 483 }
duke@435 484 }
duke@435 485 #endif // TIERED
duke@435 486
duke@435 487 // Restore the original bytecode.
duke@435 488 set_bci(cur_bci);
duke@435 489 }
duke@435 490
duke@435 491 //--------------------------profile_not_taken_branch---------------------------
duke@435 492 void Parse::profile_not_taken_branch(bool force_update) {
duke@435 493
duke@435 494 if (method_data_update() || force_update) {
duke@435 495 ciMethodData* md = method()->method_data();
duke@435 496 assert(md != NULL, "expected valid ciMethodData");
duke@435 497 ciProfileData* data = md->bci_to_data(bci());
duke@435 498 assert(data->is_BranchData(), "need BranchData for not taken branch");
duke@435 499 increment_md_counter_at(md, data, BranchData::not_taken_offset());
duke@435 500 }
duke@435 501
duke@435 502 }
duke@435 503
duke@435 504 //---------------------------------profile_call--------------------------------
duke@435 505 void Parse::profile_call(Node* receiver) {
duke@435 506 if (!method_data_update()) return;
duke@435 507
duke@435 508 switch (bc()) {
duke@435 509 case Bytecodes::_invokevirtual:
duke@435 510 case Bytecodes::_invokeinterface:
duke@435 511 profile_receiver_type(receiver);
duke@435 512 break;
duke@435 513 case Bytecodes::_invokestatic:
jrose@1161 514 case Bytecodes::_invokedynamic:
duke@435 515 case Bytecodes::_invokespecial:
kvn@1641 516 profile_generic_call();
duke@435 517 break;
duke@435 518 default: fatal("unexpected call bytecode");
duke@435 519 }
duke@435 520 }
duke@435 521
duke@435 522 //------------------------------profile_generic_call---------------------------
duke@435 523 void Parse::profile_generic_call() {
duke@435 524 assert(method_data_update(), "must be generating profile code");
duke@435 525
duke@435 526 ciMethodData* md = method()->method_data();
duke@435 527 assert(md != NULL, "expected valid ciMethodData");
duke@435 528 ciProfileData* data = md->bci_to_data(bci());
duke@435 529 assert(data->is_CounterData(), "need CounterData for not taken branch");
duke@435 530 increment_md_counter_at(md, data, CounterData::count_offset());
duke@435 531 }
duke@435 532
duke@435 533 //-----------------------------profile_receiver_type---------------------------
duke@435 534 void Parse::profile_receiver_type(Node* receiver) {
duke@435 535 assert(method_data_update(), "must be generating profile code");
duke@435 536
duke@435 537 ciMethodData* md = method()->method_data();
duke@435 538 assert(md != NULL, "expected valid ciMethodData");
duke@435 539 ciProfileData* data = md->bci_to_data(bci());
duke@435 540 assert(data->is_ReceiverTypeData(), "need ReceiverTypeData here");
kvn@1641 541
kvn@1641 542 // Skip if we aren't tracking receivers
kvn@1641 543 if (TypeProfileWidth < 1) {
kvn@1641 544 increment_md_counter_at(md, data, CounterData::count_offset());
kvn@1641 545 return;
kvn@1641 546 }
duke@435 547 ciReceiverTypeData* rdata = (ciReceiverTypeData*)data->as_ReceiverTypeData();
duke@435 548
duke@435 549 Node* method_data = method_data_addressing(md, rdata, in_ByteSize(0));
duke@435 550
duke@435 551 // Using an adr_type of TypePtr::BOTTOM to work around anti-dep problems.
duke@435 552 // A better solution might be to use TypeRawPtr::BOTTOM with RC_NARROW_MEM.
duke@435 553 make_runtime_call(RC_LEAF, OptoRuntime::profile_receiver_type_Type(),
duke@435 554 CAST_FROM_FN_PTR(address,
duke@435 555 OptoRuntime::profile_receiver_type_C),
duke@435 556 "profile_receiver_type_C",
duke@435 557 TypePtr::BOTTOM,
duke@435 558 method_data, receiver);
duke@435 559 }
duke@435 560
duke@435 561 //---------------------------------profile_ret---------------------------------
duke@435 562 void Parse::profile_ret(int target_bci) {
duke@435 563 if (!method_data_update()) return;
duke@435 564
duke@435 565 // Skip if we aren't tracking ret targets
duke@435 566 if (TypeProfileWidth < 1) return;
duke@435 567
duke@435 568 ciMethodData* md = method()->method_data();
duke@435 569 assert(md != NULL, "expected valid ciMethodData");
duke@435 570 ciProfileData* data = md->bci_to_data(bci());
duke@435 571 assert(data->is_RetData(), "need RetData for ret");
duke@435 572 ciRetData* ret_data = (ciRetData*)data->as_RetData();
duke@435 573
duke@435 574 // Look for the target_bci is already in the table
duke@435 575 uint row;
duke@435 576 bool table_full = true;
duke@435 577 for (row = 0; row < ret_data->row_limit(); row++) {
duke@435 578 int key = ret_data->bci(row);
duke@435 579 table_full &= (key != RetData::no_bci);
duke@435 580 if (key == target_bci) break;
duke@435 581 }
duke@435 582
duke@435 583 if (row >= ret_data->row_limit()) {
duke@435 584 // The target_bci was not found in the table.
duke@435 585 if (!table_full) {
duke@435 586 // XXX: Make slow call to update RetData
duke@435 587 }
duke@435 588 return;
duke@435 589 }
duke@435 590
duke@435 591 // the target_bci is already in the table
duke@435 592 increment_md_counter_at(md, data, RetData::bci_count_offset(row));
duke@435 593 }
duke@435 594
duke@435 595 //--------------------------profile_null_checkcast----------------------------
duke@435 596 void Parse::profile_null_checkcast() {
duke@435 597 // Set the null-seen flag, done in conjunction with the usual null check. We
duke@435 598 // never unset the flag, so this is a one-way switch.
duke@435 599 if (!method_data_update()) return;
duke@435 600
duke@435 601 ciMethodData* md = method()->method_data();
duke@435 602 assert(md != NULL, "expected valid ciMethodData");
duke@435 603 ciProfileData* data = md->bci_to_data(bci());
duke@435 604 assert(data->is_BitData(), "need BitData for checkcast");
duke@435 605 set_md_flag_at(md, data, BitData::null_seen_byte_constant());
duke@435 606 }
duke@435 607
duke@435 608 //-----------------------------profile_switch_case-----------------------------
duke@435 609 void Parse::profile_switch_case(int table_index) {
duke@435 610 if (!method_data_update()) return;
duke@435 611
duke@435 612 ciMethodData* md = method()->method_data();
duke@435 613 assert(md != NULL, "expected valid ciMethodData");
duke@435 614
duke@435 615 ciProfileData* data = md->bci_to_data(bci());
duke@435 616 assert(data->is_MultiBranchData(), "need MultiBranchData for switch case");
duke@435 617 if (table_index >= 0) {
duke@435 618 increment_md_counter_at(md, data, MultiBranchData::case_count_offset(table_index));
duke@435 619 } else {
duke@435 620 increment_md_counter_at(md, data, MultiBranchData::default_count_offset());
duke@435 621 }
duke@435 622 }

mercurial