1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/vm/shark/sharkTopLevelBlock.cpp Wed Aug 11 05:51:21 2010 -0700 1.3 @@ -0,0 +1,1995 @@ 1.4 +/* 1.5 + * Copyright (c) 1999, 2007, Oracle and/or its affiliates. All rights reserved. 1.6 + * Copyright 2008, 2009, 2010 Red Hat, Inc. 1.7 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.8 + * 1.9 + * This code is free software; you can redistribute it and/or modify it 1.10 + * under the terms of the GNU General Public License version 2 only, as 1.11 + * published by the Free Software Foundation. 1.12 + * 1.13 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.14 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.15 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.16 + * version 2 for more details (a copy is included in the LICENSE file that 1.17 + * accompanied this code). 1.18 + * 1.19 + * You should have received a copy of the GNU General Public License version 1.20 + * 2 along with this work; if not, write to the Free Software Foundation, 1.21 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.22 + * 1.23 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.24 + * or visit www.oracle.com if you need additional information or have any 1.25 + * questions. 1.26 + * 1.27 + */ 1.28 + 1.29 +#include "incls/_precompiled.incl" 1.30 +#include "incls/_sharkTopLevelBlock.cpp.incl" 1.31 + 1.32 +using namespace llvm; 1.33 + 1.34 +void SharkTopLevelBlock::scan_for_traps() { 1.35 + // If typeflow found a trap then don't scan past it 1.36 + int limit_bci = ciblock()->has_trap() ? ciblock()->trap_bci() : limit(); 1.37 + 1.38 + // Scan the bytecode for traps that are always hit 1.39 + iter()->reset_to_bci(start()); 1.40 + while (iter()->next_bci() < limit_bci) { 1.41 + iter()->next(); 1.42 + 1.43 + ciField *field; 1.44 + ciMethod *method; 1.45 + ciInstanceKlass *klass; 1.46 + bool will_link; 1.47 + bool is_field; 1.48 + 1.49 + switch (bc()) { 1.50 + case Bytecodes::_ldc: 1.51 + case Bytecodes::_ldc_w: 1.52 + if (!SharkConstant::for_ldc(iter())->is_loaded()) { 1.53 + set_trap( 1.54 + Deoptimization::make_trap_request( 1.55 + Deoptimization::Reason_uninitialized, 1.56 + Deoptimization::Action_reinterpret), bci()); 1.57 + return; 1.58 + } 1.59 + break; 1.60 + 1.61 + case Bytecodes::_getfield: 1.62 + case Bytecodes::_getstatic: 1.63 + case Bytecodes::_putfield: 1.64 + case Bytecodes::_putstatic: 1.65 + field = iter()->get_field(will_link); 1.66 + assert(will_link, "typeflow responsibility"); 1.67 + is_field = (bc() == Bytecodes::_getfield || bc() == Bytecodes::_putfield); 1.68 + 1.69 + // If the bytecode does not match the field then bail out to 1.70 + // the interpreter to throw an IncompatibleClassChangeError 1.71 + if (is_field == field->is_static()) { 1.72 + set_trap( 1.73 + Deoptimization::make_trap_request( 1.74 + Deoptimization::Reason_unhandled, 1.75 + Deoptimization::Action_none), bci()); 1.76 + return; 1.77 + } 1.78 + 1.79 + // Bail out if we are trying to access a static variable 1.80 + // before the class initializer has completed. 1.81 + if (!is_field && !field->holder()->is_initialized()) { 1.82 + if (!static_field_ok_in_clinit(field)) { 1.83 + set_trap( 1.84 + Deoptimization::make_trap_request( 1.85 + Deoptimization::Reason_uninitialized, 1.86 + Deoptimization::Action_reinterpret), bci()); 1.87 + return; 1.88 + } 1.89 + } 1.90 + break; 1.91 + 1.92 + case Bytecodes::_invokestatic: 1.93 + case Bytecodes::_invokespecial: 1.94 + case Bytecodes::_invokevirtual: 1.95 + case Bytecodes::_invokeinterface: 1.96 + method = iter()->get_method(will_link); 1.97 + assert(will_link, "typeflow responsibility"); 1.98 + 1.99 + if (!method->holder()->is_linked()) { 1.100 + set_trap( 1.101 + Deoptimization::make_trap_request( 1.102 + Deoptimization::Reason_uninitialized, 1.103 + Deoptimization::Action_reinterpret), bci()); 1.104 + return; 1.105 + } 1.106 + 1.107 + if (bc() == Bytecodes::_invokevirtual) { 1.108 + klass = ciEnv::get_instance_klass_for_declared_method_holder( 1.109 + iter()->get_declared_method_holder()); 1.110 + if (!klass->is_linked()) { 1.111 + set_trap( 1.112 + Deoptimization::make_trap_request( 1.113 + Deoptimization::Reason_uninitialized, 1.114 + Deoptimization::Action_reinterpret), bci()); 1.115 + return; 1.116 + } 1.117 + } 1.118 + break; 1.119 + 1.120 + case Bytecodes::_new: 1.121 + klass = iter()->get_klass(will_link)->as_instance_klass(); 1.122 + assert(will_link, "typeflow responsibility"); 1.123 + 1.124 + // Bail out if the class is unloaded 1.125 + if (iter()->is_unresolved_klass() || !klass->is_initialized()) { 1.126 + set_trap( 1.127 + Deoptimization::make_trap_request( 1.128 + Deoptimization::Reason_uninitialized, 1.129 + Deoptimization::Action_reinterpret), bci()); 1.130 + return; 1.131 + } 1.132 + 1.133 + // Bail out if the class cannot be instantiated 1.134 + if (klass->is_abstract() || klass->is_interface() || 1.135 + klass->name() == ciSymbol::java_lang_Class()) { 1.136 + set_trap( 1.137 + Deoptimization::make_trap_request( 1.138 + Deoptimization::Reason_unhandled, 1.139 + Deoptimization::Action_reinterpret), bci()); 1.140 + return; 1.141 + } 1.142 + break; 1.143 + } 1.144 + } 1.145 + 1.146 + // Trap if typeflow trapped (and we didn't before) 1.147 + if (ciblock()->has_trap()) { 1.148 + set_trap( 1.149 + Deoptimization::make_trap_request( 1.150 + Deoptimization::Reason_unloaded, 1.151 + Deoptimization::Action_reinterpret, 1.152 + ciblock()->trap_index()), ciblock()->trap_bci()); 1.153 + return; 1.154 + } 1.155 +} 1.156 + 1.157 +bool SharkTopLevelBlock::static_field_ok_in_clinit(ciField* field) { 1.158 + assert(field->is_static(), "should be"); 1.159 + 1.160 + // This code is lifted pretty much verbatim from C2's 1.161 + // Parse::static_field_ok_in_clinit() in parse3.cpp. 1.162 + bool access_OK = false; 1.163 + if (target()->holder()->is_subclass_of(field->holder())) { 1.164 + if (target()->is_static()) { 1.165 + if (target()->name() == ciSymbol::class_initializer_name()) { 1.166 + // It's OK to access static fields from the class initializer 1.167 + access_OK = true; 1.168 + } 1.169 + } 1.170 + else { 1.171 + if (target()->name() == ciSymbol::object_initializer_name()) { 1.172 + // It's also OK to access static fields inside a constructor, 1.173 + // because any thread calling the constructor must first have 1.174 + // synchronized on the class by executing a "new" bytecode. 1.175 + access_OK = true; 1.176 + } 1.177 + } 1.178 + } 1.179 + return access_OK; 1.180 +} 1.181 + 1.182 +SharkState* SharkTopLevelBlock::entry_state() { 1.183 + if (_entry_state == NULL) { 1.184 + assert(needs_phis(), "should do"); 1.185 + _entry_state = new SharkPHIState(this); 1.186 + } 1.187 + return _entry_state; 1.188 +} 1.189 + 1.190 +void SharkTopLevelBlock::add_incoming(SharkState* incoming_state) { 1.191 + if (needs_phis()) { 1.192 + ((SharkPHIState *) entry_state())->add_incoming(incoming_state); 1.193 + } 1.194 + else if (_entry_state == NULL) { 1.195 + _entry_state = incoming_state; 1.196 + } 1.197 + else { 1.198 + assert(entry_state()->equal_to(incoming_state), "should be"); 1.199 + } 1.200 +} 1.201 + 1.202 +void SharkTopLevelBlock::enter(SharkTopLevelBlock* predecessor, 1.203 + bool is_exception) { 1.204 + // This block requires phis: 1.205 + // - if it is entered more than once 1.206 + // - if it is an exception handler, because in which 1.207 + // case we assume it's entered more than once. 1.208 + // - if the predecessor will be compiled after this 1.209 + // block, in which case we can't simple propagate 1.210 + // the state forward. 1.211 + if (!needs_phis() && 1.212 + (entered() || 1.213 + is_exception || 1.214 + (predecessor && predecessor->index() >= index()))) 1.215 + _needs_phis = true; 1.216 + 1.217 + // Recurse into the tree 1.218 + if (!entered()) { 1.219 + _entered = true; 1.220 + 1.221 + scan_for_traps(); 1.222 + if (!has_trap()) { 1.223 + for (int i = 0; i < num_successors(); i++) { 1.224 + successor(i)->enter(this, false); 1.225 + } 1.226 + } 1.227 + compute_exceptions(); 1.228 + for (int i = 0; i < num_exceptions(); i++) { 1.229 + SharkTopLevelBlock *handler = exception(i); 1.230 + if (handler) 1.231 + handler->enter(this, true); 1.232 + } 1.233 + } 1.234 +} 1.235 + 1.236 +void SharkTopLevelBlock::initialize() { 1.237 + char name[28]; 1.238 + snprintf(name, sizeof(name), 1.239 + "bci_%d%s", 1.240 + start(), is_backedge_copy() ? "_backedge_copy" : ""); 1.241 + _entry_block = function()->CreateBlock(name); 1.242 +} 1.243 + 1.244 +void SharkTopLevelBlock::decache_for_Java_call(ciMethod *callee) { 1.245 + SharkJavaCallDecacher(function(), bci(), callee).scan(current_state()); 1.246 + for (int i = 0; i < callee->arg_size(); i++) 1.247 + xpop(); 1.248 +} 1.249 + 1.250 +void SharkTopLevelBlock::cache_after_Java_call(ciMethod *callee) { 1.251 + if (callee->return_type()->size()) { 1.252 + ciType *type; 1.253 + switch (callee->return_type()->basic_type()) { 1.254 + case T_BOOLEAN: 1.255 + case T_BYTE: 1.256 + case T_CHAR: 1.257 + case T_SHORT: 1.258 + type = ciType::make(T_INT); 1.259 + break; 1.260 + 1.261 + default: 1.262 + type = callee->return_type(); 1.263 + } 1.264 + 1.265 + push(SharkValue::create_generic(type, NULL, false)); 1.266 + } 1.267 + SharkJavaCallCacher(function(), callee).scan(current_state()); 1.268 +} 1.269 + 1.270 +void SharkTopLevelBlock::decache_for_VM_call() { 1.271 + SharkVMCallDecacher(function(), bci()).scan(current_state()); 1.272 +} 1.273 + 1.274 +void SharkTopLevelBlock::cache_after_VM_call() { 1.275 + SharkVMCallCacher(function()).scan(current_state()); 1.276 +} 1.277 + 1.278 +void SharkTopLevelBlock::decache_for_trap() { 1.279 + SharkTrapDecacher(function(), bci()).scan(current_state()); 1.280 +} 1.281 + 1.282 +void SharkTopLevelBlock::emit_IR() { 1.283 + builder()->SetInsertPoint(entry_block()); 1.284 + 1.285 + // Parse the bytecode 1.286 + parse_bytecode(start(), limit()); 1.287 + 1.288 + // If this block falls through to the next then it won't have been 1.289 + // terminated by a bytecode and we have to add the branch ourselves 1.290 + if (falls_through() && !has_trap()) 1.291 + do_branch(ciTypeFlow::FALL_THROUGH); 1.292 +} 1.293 + 1.294 +SharkTopLevelBlock* SharkTopLevelBlock::bci_successor(int bci) const { 1.295 + // XXX now with Linear Search Technology (tm) 1.296 + for (int i = 0; i < num_successors(); i++) { 1.297 + ciTypeFlow::Block *successor = ciblock()->successors()->at(i); 1.298 + if (successor->start() == bci) 1.299 + return function()->block(successor->pre_order()); 1.300 + } 1.301 + ShouldNotReachHere(); 1.302 +} 1.303 + 1.304 +void SharkTopLevelBlock::do_zero_check(SharkValue *value) { 1.305 + if (value->is_phi() && value->as_phi()->all_incomers_zero_checked()) { 1.306 + function()->add_deferred_zero_check(this, value); 1.307 + } 1.308 + else { 1.309 + BasicBlock *continue_block = function()->CreateBlock("not_zero"); 1.310 + SharkState *saved_state = current_state(); 1.311 + set_current_state(saved_state->copy()); 1.312 + zero_check_value(value, continue_block); 1.313 + builder()->SetInsertPoint(continue_block); 1.314 + set_current_state(saved_state); 1.315 + } 1.316 + 1.317 + value->set_zero_checked(true); 1.318 +} 1.319 + 1.320 +void SharkTopLevelBlock::do_deferred_zero_check(SharkValue* value, 1.321 + int bci, 1.322 + SharkState* saved_state, 1.323 + BasicBlock* continue_block) { 1.324 + if (value->as_phi()->all_incomers_zero_checked()) { 1.325 + builder()->CreateBr(continue_block); 1.326 + } 1.327 + else { 1.328 + iter()->force_bci(start()); 1.329 + set_current_state(saved_state); 1.330 + zero_check_value(value, continue_block); 1.331 + } 1.332 +} 1.333 + 1.334 +void SharkTopLevelBlock::zero_check_value(SharkValue* value, 1.335 + BasicBlock* continue_block) { 1.336 + BasicBlock *zero_block = builder()->CreateBlock(continue_block, "zero"); 1.337 + 1.338 + Value *a, *b; 1.339 + switch (value->basic_type()) { 1.340 + case T_BYTE: 1.341 + case T_CHAR: 1.342 + case T_SHORT: 1.343 + case T_INT: 1.344 + a = value->jint_value(); 1.345 + b = LLVMValue::jint_constant(0); 1.346 + break; 1.347 + case T_LONG: 1.348 + a = value->jlong_value(); 1.349 + b = LLVMValue::jlong_constant(0); 1.350 + break; 1.351 + case T_OBJECT: 1.352 + case T_ARRAY: 1.353 + a = value->jobject_value(); 1.354 + b = LLVMValue::LLVMValue::null(); 1.355 + break; 1.356 + default: 1.357 + tty->print_cr("Unhandled type %s", type2name(value->basic_type())); 1.358 + ShouldNotReachHere(); 1.359 + } 1.360 + 1.361 + builder()->CreateCondBr( 1.362 + builder()->CreateICmpNE(a, b), continue_block, zero_block); 1.363 + 1.364 + builder()->SetInsertPoint(zero_block); 1.365 + if (value->is_jobject()) { 1.366 + call_vm( 1.367 + builder()->throw_NullPointerException(), 1.368 + builder()->CreateIntToPtr( 1.369 + LLVMValue::intptr_constant((intptr_t) __FILE__), 1.370 + PointerType::getUnqual(SharkType::jbyte_type())), 1.371 + LLVMValue::jint_constant(__LINE__), 1.372 + EX_CHECK_NONE); 1.373 + } 1.374 + else { 1.375 + call_vm( 1.376 + builder()->throw_ArithmeticException(), 1.377 + builder()->CreateIntToPtr( 1.378 + LLVMValue::intptr_constant((intptr_t) __FILE__), 1.379 + PointerType::getUnqual(SharkType::jbyte_type())), 1.380 + LLVMValue::jint_constant(__LINE__), 1.381 + EX_CHECK_NONE); 1.382 + } 1.383 + 1.384 + Value *pending_exception = get_pending_exception(); 1.385 + clear_pending_exception(); 1.386 + handle_exception(pending_exception, EX_CHECK_FULL); 1.387 +} 1.388 + 1.389 +void SharkTopLevelBlock::check_bounds(SharkValue* array, SharkValue* index) { 1.390 + BasicBlock *out_of_bounds = function()->CreateBlock("out_of_bounds"); 1.391 + BasicBlock *in_bounds = function()->CreateBlock("in_bounds"); 1.392 + 1.393 + Value *length = builder()->CreateArrayLength(array->jarray_value()); 1.394 + // we use an unsigned comparison to catch negative values 1.395 + builder()->CreateCondBr( 1.396 + builder()->CreateICmpULT(index->jint_value(), length), 1.397 + in_bounds, out_of_bounds); 1.398 + 1.399 + builder()->SetInsertPoint(out_of_bounds); 1.400 + SharkState *saved_state = current_state()->copy(); 1.401 + 1.402 + call_vm( 1.403 + builder()->throw_ArrayIndexOutOfBoundsException(), 1.404 + builder()->CreateIntToPtr( 1.405 + LLVMValue::intptr_constant((intptr_t) __FILE__), 1.406 + PointerType::getUnqual(SharkType::jbyte_type())), 1.407 + LLVMValue::jint_constant(__LINE__), 1.408 + index->jint_value(), 1.409 + EX_CHECK_NONE); 1.410 + 1.411 + Value *pending_exception = get_pending_exception(); 1.412 + clear_pending_exception(); 1.413 + handle_exception(pending_exception, EX_CHECK_FULL); 1.414 + 1.415 + set_current_state(saved_state); 1.416 + 1.417 + builder()->SetInsertPoint(in_bounds); 1.418 +} 1.419 + 1.420 +void SharkTopLevelBlock::check_pending_exception(int action) { 1.421 + assert(action & EAM_CHECK, "should be"); 1.422 + 1.423 + BasicBlock *exception = function()->CreateBlock("exception"); 1.424 + BasicBlock *no_exception = function()->CreateBlock("no_exception"); 1.425 + 1.426 + Value *pending_exception = get_pending_exception(); 1.427 + builder()->CreateCondBr( 1.428 + builder()->CreateICmpEQ(pending_exception, LLVMValue::null()), 1.429 + no_exception, exception); 1.430 + 1.431 + builder()->SetInsertPoint(exception); 1.432 + SharkState *saved_state = current_state()->copy(); 1.433 + if (action & EAM_MONITOR_FUDGE) { 1.434 + // The top monitor is marked live, but the exception was thrown 1.435 + // while setting it up so we need to mark it dead before we enter 1.436 + // any exception handlers as they will not expect it to be there. 1.437 + set_num_monitors(num_monitors() - 1); 1.438 + action ^= EAM_MONITOR_FUDGE; 1.439 + } 1.440 + clear_pending_exception(); 1.441 + handle_exception(pending_exception, action); 1.442 + set_current_state(saved_state); 1.443 + 1.444 + builder()->SetInsertPoint(no_exception); 1.445 +} 1.446 + 1.447 +void SharkTopLevelBlock::compute_exceptions() { 1.448 + ciExceptionHandlerStream str(target(), start()); 1.449 + 1.450 + int exc_count = str.count(); 1.451 + _exc_handlers = new GrowableArray<ciExceptionHandler*>(exc_count); 1.452 + _exceptions = new GrowableArray<SharkTopLevelBlock*>(exc_count); 1.453 + 1.454 + int index = 0; 1.455 + for (; !str.is_done(); str.next()) { 1.456 + ciExceptionHandler *handler = str.handler(); 1.457 + if (handler->handler_bci() == -1) 1.458 + break; 1.459 + _exc_handlers->append(handler); 1.460 + 1.461 + // Try and get this exception's handler from typeflow. We should 1.462 + // do it this way always, really, except that typeflow sometimes 1.463 + // doesn't record exceptions, even loaded ones, and sometimes it 1.464 + // returns them with a different handler bci. Why??? 1.465 + SharkTopLevelBlock *block = NULL; 1.466 + ciInstanceKlass* klass; 1.467 + if (handler->is_catch_all()) { 1.468 + klass = java_lang_Throwable_klass(); 1.469 + } 1.470 + else { 1.471 + klass = handler->catch_klass(); 1.472 + } 1.473 + for (int i = 0; i < ciblock()->exceptions()->length(); i++) { 1.474 + if (klass == ciblock()->exc_klasses()->at(i)) { 1.475 + block = function()->block(ciblock()->exceptions()->at(i)->pre_order()); 1.476 + if (block->start() == handler->handler_bci()) 1.477 + break; 1.478 + else 1.479 + block = NULL; 1.480 + } 1.481 + } 1.482 + 1.483 + // If typeflow let us down then try and figure it out ourselves 1.484 + if (block == NULL) { 1.485 + for (int i = 0; i < function()->block_count(); i++) { 1.486 + SharkTopLevelBlock *candidate = function()->block(i); 1.487 + if (candidate->start() == handler->handler_bci()) { 1.488 + if (block != NULL) { 1.489 + NOT_PRODUCT(warning("there may be trouble ahead")); 1.490 + block = NULL; 1.491 + break; 1.492 + } 1.493 + block = candidate; 1.494 + } 1.495 + } 1.496 + } 1.497 + _exceptions->append(block); 1.498 + } 1.499 +} 1.500 + 1.501 +void SharkTopLevelBlock::handle_exception(Value* exception, int action) { 1.502 + if (action & EAM_HANDLE && num_exceptions() != 0) { 1.503 + // Clear the stack and push the exception onto it 1.504 + while (xstack_depth()) 1.505 + pop(); 1.506 + push(SharkValue::create_jobject(exception, true)); 1.507 + 1.508 + // Work out how many options we have to check 1.509 + bool has_catch_all = exc_handler(num_exceptions() - 1)->is_catch_all(); 1.510 + int num_options = num_exceptions(); 1.511 + if (has_catch_all) 1.512 + num_options--; 1.513 + 1.514 + // Marshal any non-catch-all handlers 1.515 + if (num_options > 0) { 1.516 + bool all_loaded = true; 1.517 + for (int i = 0; i < num_options; i++) { 1.518 + if (!exc_handler(i)->catch_klass()->is_loaded()) { 1.519 + all_loaded = false; 1.520 + break; 1.521 + } 1.522 + } 1.523 + 1.524 + if (all_loaded) 1.525 + marshal_exception_fast(num_options); 1.526 + else 1.527 + marshal_exception_slow(num_options); 1.528 + } 1.529 + 1.530 + // Install the catch-all handler, if present 1.531 + if (has_catch_all) { 1.532 + SharkTopLevelBlock* handler = this->exception(num_options); 1.533 + assert(handler != NULL, "catch-all handler cannot be unloaded"); 1.534 + 1.535 + builder()->CreateBr(handler->entry_block()); 1.536 + handler->add_incoming(current_state()); 1.537 + return; 1.538 + } 1.539 + } 1.540 + 1.541 + // No exception handler was found; unwind and return 1.542 + handle_return(T_VOID, exception); 1.543 +} 1.544 + 1.545 +void SharkTopLevelBlock::marshal_exception_fast(int num_options) { 1.546 + Value *exception_klass = builder()->CreateValueOfStructEntry( 1.547 + xstack(0)->jobject_value(), 1.548 + in_ByteSize(oopDesc::klass_offset_in_bytes()), 1.549 + SharkType::oop_type(), 1.550 + "exception_klass"); 1.551 + 1.552 + for (int i = 0; i < num_options; i++) { 1.553 + Value *check_klass = 1.554 + builder()->CreateInlineOop(exc_handler(i)->catch_klass()); 1.555 + 1.556 + BasicBlock *not_exact = function()->CreateBlock("not_exact"); 1.557 + BasicBlock *not_subtype = function()->CreateBlock("not_subtype"); 1.558 + 1.559 + builder()->CreateCondBr( 1.560 + builder()->CreateICmpEQ(check_klass, exception_klass), 1.561 + handler_for_exception(i), not_exact); 1.562 + 1.563 + builder()->SetInsertPoint(not_exact); 1.564 + builder()->CreateCondBr( 1.565 + builder()->CreateICmpNE( 1.566 + builder()->CreateCall2( 1.567 + builder()->is_subtype_of(), check_klass, exception_klass), 1.568 + LLVMValue::jbyte_constant(0)), 1.569 + handler_for_exception(i), not_subtype); 1.570 + 1.571 + builder()->SetInsertPoint(not_subtype); 1.572 + } 1.573 +} 1.574 + 1.575 +void SharkTopLevelBlock::marshal_exception_slow(int num_options) { 1.576 + int *indexes = NEW_RESOURCE_ARRAY(int, num_options); 1.577 + for (int i = 0; i < num_options; i++) 1.578 + indexes[i] = exc_handler(i)->catch_klass_index(); 1.579 + 1.580 + Value *index = call_vm( 1.581 + builder()->find_exception_handler(), 1.582 + builder()->CreateInlineData( 1.583 + indexes, 1.584 + num_options * sizeof(int), 1.585 + PointerType::getUnqual(SharkType::jint_type())), 1.586 + LLVMValue::jint_constant(num_options), 1.587 + EX_CHECK_NO_CATCH); 1.588 + 1.589 + BasicBlock *no_handler = function()->CreateBlock("no_handler"); 1.590 + SwitchInst *switchinst = builder()->CreateSwitch( 1.591 + index, no_handler, num_options); 1.592 + 1.593 + for (int i = 0; i < num_options; i++) { 1.594 + switchinst->addCase( 1.595 + LLVMValue::jint_constant(i), 1.596 + handler_for_exception(i)); 1.597 + } 1.598 + 1.599 + builder()->SetInsertPoint(no_handler); 1.600 +} 1.601 + 1.602 +BasicBlock* SharkTopLevelBlock::handler_for_exception(int index) { 1.603 + SharkTopLevelBlock *successor = this->exception(index); 1.604 + if (successor) { 1.605 + successor->add_incoming(current_state()); 1.606 + return successor->entry_block(); 1.607 + } 1.608 + else { 1.609 + return make_trap( 1.610 + exc_handler(index)->handler_bci(), 1.611 + Deoptimization::make_trap_request( 1.612 + Deoptimization::Reason_unhandled, 1.613 + Deoptimization::Action_reinterpret)); 1.614 + } 1.615 +} 1.616 + 1.617 +void SharkTopLevelBlock::maybe_add_safepoint() { 1.618 + if (current_state()->has_safepointed()) 1.619 + return; 1.620 + 1.621 + BasicBlock *orig_block = builder()->GetInsertBlock(); 1.622 + SharkState *orig_state = current_state()->copy(); 1.623 + 1.624 + BasicBlock *do_safepoint = function()->CreateBlock("do_safepoint"); 1.625 + BasicBlock *safepointed = function()->CreateBlock("safepointed"); 1.626 + 1.627 + Value *state = builder()->CreateLoad( 1.628 + builder()->CreateIntToPtr( 1.629 + LLVMValue::intptr_constant( 1.630 + (intptr_t) SafepointSynchronize::address_of_state()), 1.631 + PointerType::getUnqual(SharkType::jint_type())), 1.632 + "state"); 1.633 + 1.634 + builder()->CreateCondBr( 1.635 + builder()->CreateICmpEQ( 1.636 + state, 1.637 + LLVMValue::jint_constant(SafepointSynchronize::_synchronizing)), 1.638 + do_safepoint, safepointed); 1.639 + 1.640 + builder()->SetInsertPoint(do_safepoint); 1.641 + call_vm(builder()->safepoint(), EX_CHECK_FULL); 1.642 + BasicBlock *safepointed_block = builder()->GetInsertBlock(); 1.643 + builder()->CreateBr(safepointed); 1.644 + 1.645 + builder()->SetInsertPoint(safepointed); 1.646 + current_state()->merge(orig_state, orig_block, safepointed_block); 1.647 + 1.648 + current_state()->set_has_safepointed(true); 1.649 +} 1.650 + 1.651 +void SharkTopLevelBlock::maybe_add_backedge_safepoint() { 1.652 + if (current_state()->has_safepointed()) 1.653 + return; 1.654 + 1.655 + for (int i = 0; i < num_successors(); i++) { 1.656 + if (successor(i)->can_reach(this)) { 1.657 + maybe_add_safepoint(); 1.658 + break; 1.659 + } 1.660 + } 1.661 +} 1.662 + 1.663 +bool SharkTopLevelBlock::can_reach(SharkTopLevelBlock* other) { 1.664 + for (int i = 0; i < function()->block_count(); i++) 1.665 + function()->block(i)->_can_reach_visited = false; 1.666 + 1.667 + return can_reach_helper(other); 1.668 +} 1.669 + 1.670 +bool SharkTopLevelBlock::can_reach_helper(SharkTopLevelBlock* other) { 1.671 + if (this == other) 1.672 + return true; 1.673 + 1.674 + if (_can_reach_visited) 1.675 + return false; 1.676 + _can_reach_visited = true; 1.677 + 1.678 + if (!has_trap()) { 1.679 + for (int i = 0; i < num_successors(); i++) { 1.680 + if (successor(i)->can_reach_helper(other)) 1.681 + return true; 1.682 + } 1.683 + } 1.684 + 1.685 + for (int i = 0; i < num_exceptions(); i++) { 1.686 + SharkTopLevelBlock *handler = exception(i); 1.687 + if (handler && handler->can_reach_helper(other)) 1.688 + return true; 1.689 + } 1.690 + 1.691 + return false; 1.692 +} 1.693 + 1.694 +BasicBlock* SharkTopLevelBlock::make_trap(int trap_bci, int trap_request) { 1.695 + BasicBlock *trap_block = function()->CreateBlock("trap"); 1.696 + BasicBlock *orig_block = builder()->GetInsertBlock(); 1.697 + builder()->SetInsertPoint(trap_block); 1.698 + 1.699 + int orig_bci = bci(); 1.700 + iter()->force_bci(trap_bci); 1.701 + 1.702 + do_trap(trap_request); 1.703 + 1.704 + builder()->SetInsertPoint(orig_block); 1.705 + iter()->force_bci(orig_bci); 1.706 + 1.707 + return trap_block; 1.708 +} 1.709 + 1.710 +void SharkTopLevelBlock::do_trap(int trap_request) { 1.711 + decache_for_trap(); 1.712 + builder()->CreateRet( 1.713 + builder()->CreateCall2( 1.714 + builder()->uncommon_trap(), 1.715 + thread(), 1.716 + LLVMValue::jint_constant(trap_request))); 1.717 +} 1.718 + 1.719 +void SharkTopLevelBlock::call_register_finalizer(Value *receiver) { 1.720 + BasicBlock *orig_block = builder()->GetInsertBlock(); 1.721 + SharkState *orig_state = current_state()->copy(); 1.722 + 1.723 + BasicBlock *do_call = function()->CreateBlock("has_finalizer"); 1.724 + BasicBlock *done = function()->CreateBlock("done"); 1.725 + 1.726 + Value *klass = builder()->CreateValueOfStructEntry( 1.727 + receiver, 1.728 + in_ByteSize(oopDesc::klass_offset_in_bytes()), 1.729 + SharkType::oop_type(), 1.730 + "klass"); 1.731 + 1.732 + Value *klass_part = builder()->CreateAddressOfStructEntry( 1.733 + klass, 1.734 + in_ByteSize(klassOopDesc::klass_part_offset_in_bytes()), 1.735 + SharkType::klass_type(), 1.736 + "klass_part"); 1.737 + 1.738 + Value *access_flags = builder()->CreateValueOfStructEntry( 1.739 + klass_part, 1.740 + in_ByteSize(Klass::access_flags_offset_in_bytes()), 1.741 + SharkType::jint_type(), 1.742 + "access_flags"); 1.743 + 1.744 + builder()->CreateCondBr( 1.745 + builder()->CreateICmpNE( 1.746 + builder()->CreateAnd( 1.747 + access_flags, 1.748 + LLVMValue::jint_constant(JVM_ACC_HAS_FINALIZER)), 1.749 + LLVMValue::jint_constant(0)), 1.750 + do_call, done); 1.751 + 1.752 + builder()->SetInsertPoint(do_call); 1.753 + call_vm(builder()->register_finalizer(), receiver, EX_CHECK_FULL); 1.754 + BasicBlock *branch_block = builder()->GetInsertBlock(); 1.755 + builder()->CreateBr(done); 1.756 + 1.757 + builder()->SetInsertPoint(done); 1.758 + current_state()->merge(orig_state, orig_block, branch_block); 1.759 +} 1.760 + 1.761 +void SharkTopLevelBlock::handle_return(BasicType type, Value* exception) { 1.762 + assert (exception == NULL || type == T_VOID, "exception OR result, please"); 1.763 + 1.764 + if (num_monitors()) { 1.765 + // Protect our exception across possible monitor release decaches 1.766 + if (exception) 1.767 + set_oop_tmp(exception); 1.768 + 1.769 + // We don't need to check for exceptions thrown here. If 1.770 + // we're returning a value then we just carry on as normal: 1.771 + // the caller will see the pending exception and handle it. 1.772 + // If we're returning with an exception then that exception 1.773 + // takes priority and the release_lock one will be ignored. 1.774 + while (num_monitors()) 1.775 + release_lock(EX_CHECK_NONE); 1.776 + 1.777 + // Reload the exception we're throwing 1.778 + if (exception) 1.779 + exception = get_oop_tmp(); 1.780 + } 1.781 + 1.782 + if (exception) { 1.783 + builder()->CreateStore(exception, pending_exception_address()); 1.784 + } 1.785 + 1.786 + Value *result_addr = stack()->CreatePopFrame(type2size[type]); 1.787 + if (type != T_VOID) { 1.788 + builder()->CreateStore( 1.789 + pop_result(type)->generic_value(), 1.790 + builder()->CreateIntToPtr( 1.791 + result_addr, 1.792 + PointerType::getUnqual(SharkType::to_stackType(type)))); 1.793 + } 1.794 + 1.795 + builder()->CreateRet(LLVMValue::jint_constant(0)); 1.796 +} 1.797 + 1.798 +void SharkTopLevelBlock::do_arraylength() { 1.799 + SharkValue *array = pop(); 1.800 + check_null(array); 1.801 + Value *length = builder()->CreateArrayLength(array->jarray_value()); 1.802 + push(SharkValue::create_jint(length, false)); 1.803 +} 1.804 + 1.805 +void SharkTopLevelBlock::do_aload(BasicType basic_type) { 1.806 + SharkValue *index = pop(); 1.807 + SharkValue *array = pop(); 1.808 + 1.809 + check_null(array); 1.810 + check_bounds(array, index); 1.811 + 1.812 + Value *value = builder()->CreateLoad( 1.813 + builder()->CreateArrayAddress( 1.814 + array->jarray_value(), basic_type, index->jint_value())); 1.815 + 1.816 + const Type *stack_type = SharkType::to_stackType(basic_type); 1.817 + if (value->getType() != stack_type) 1.818 + value = builder()->CreateIntCast(value, stack_type, basic_type != T_CHAR); 1.819 + 1.820 + switch (basic_type) { 1.821 + case T_BYTE: 1.822 + case T_CHAR: 1.823 + case T_SHORT: 1.824 + case T_INT: 1.825 + push(SharkValue::create_jint(value, false)); 1.826 + break; 1.827 + 1.828 + case T_LONG: 1.829 + push(SharkValue::create_jlong(value, false)); 1.830 + break; 1.831 + 1.832 + case T_FLOAT: 1.833 + push(SharkValue::create_jfloat(value)); 1.834 + break; 1.835 + 1.836 + case T_DOUBLE: 1.837 + push(SharkValue::create_jdouble(value)); 1.838 + break; 1.839 + 1.840 + case T_OBJECT: 1.841 + // You might expect that array->type()->is_array_klass() would 1.842 + // always be true, but it isn't. If ciTypeFlow detects that a 1.843 + // value is always null then that value becomes an untyped null 1.844 + // object. Shark doesn't presently support this, so a generic 1.845 + // T_OBJECT is created. In this case we guess the type using 1.846 + // the BasicType we were supplied. In reality the generated 1.847 + // code will never be used, as the null value will be caught 1.848 + // by the above null pointer check. 1.849 + // http://icedtea.classpath.org/bugzilla/show_bug.cgi?id=324 1.850 + push( 1.851 + SharkValue::create_generic( 1.852 + array->type()->is_array_klass() ? 1.853 + ((ciArrayKlass *) array->type())->element_type() : 1.854 + ciType::make(basic_type), 1.855 + value, false)); 1.856 + break; 1.857 + 1.858 + default: 1.859 + tty->print_cr("Unhandled type %s", type2name(basic_type)); 1.860 + ShouldNotReachHere(); 1.861 + } 1.862 +} 1.863 + 1.864 +void SharkTopLevelBlock::do_astore(BasicType basic_type) { 1.865 + SharkValue *svalue = pop(); 1.866 + SharkValue *index = pop(); 1.867 + SharkValue *array = pop(); 1.868 + 1.869 + check_null(array); 1.870 + check_bounds(array, index); 1.871 + 1.872 + Value *value; 1.873 + switch (basic_type) { 1.874 + case T_BYTE: 1.875 + case T_CHAR: 1.876 + case T_SHORT: 1.877 + case T_INT: 1.878 + value = svalue->jint_value(); 1.879 + break; 1.880 + 1.881 + case T_LONG: 1.882 + value = svalue->jlong_value(); 1.883 + break; 1.884 + 1.885 + case T_FLOAT: 1.886 + value = svalue->jfloat_value(); 1.887 + break; 1.888 + 1.889 + case T_DOUBLE: 1.890 + value = svalue->jdouble_value(); 1.891 + break; 1.892 + 1.893 + case T_OBJECT: 1.894 + value = svalue->jobject_value(); 1.895 + // XXX assignability check 1.896 + break; 1.897 + 1.898 + default: 1.899 + tty->print_cr("Unhandled type %s", type2name(basic_type)); 1.900 + ShouldNotReachHere(); 1.901 + } 1.902 + 1.903 + const Type *array_type = SharkType::to_arrayType(basic_type); 1.904 + if (value->getType() != array_type) 1.905 + value = builder()->CreateIntCast(value, array_type, basic_type != T_CHAR); 1.906 + 1.907 + Value *addr = builder()->CreateArrayAddress( 1.908 + array->jarray_value(), basic_type, index->jint_value(), "addr"); 1.909 + 1.910 + builder()->CreateStore(value, addr); 1.911 + 1.912 + if (basic_type == T_OBJECT) // XXX or T_ARRAY? 1.913 + builder()->CreateUpdateBarrierSet(oopDesc::bs(), addr); 1.914 +} 1.915 + 1.916 +void SharkTopLevelBlock::do_return(BasicType type) { 1.917 + if (target()->intrinsic_id() == vmIntrinsics::_Object_init) 1.918 + call_register_finalizer(local(0)->jobject_value()); 1.919 + maybe_add_safepoint(); 1.920 + handle_return(type, NULL); 1.921 +} 1.922 + 1.923 +void SharkTopLevelBlock::do_athrow() { 1.924 + SharkValue *exception = pop(); 1.925 + check_null(exception); 1.926 + handle_exception(exception->jobject_value(), EX_CHECK_FULL); 1.927 +} 1.928 + 1.929 +void SharkTopLevelBlock::do_goto() { 1.930 + do_branch(ciTypeFlow::GOTO_TARGET); 1.931 +} 1.932 + 1.933 +void SharkTopLevelBlock::do_jsr() { 1.934 + push(SharkValue::address_constant(iter()->next_bci())); 1.935 + do_branch(ciTypeFlow::GOTO_TARGET); 1.936 +} 1.937 + 1.938 +void SharkTopLevelBlock::do_ret() { 1.939 + assert(local(iter()->get_index())->address_value() == 1.940 + successor(ciTypeFlow::GOTO_TARGET)->start(), "should be"); 1.941 + do_branch(ciTypeFlow::GOTO_TARGET); 1.942 +} 1.943 + 1.944 +// All propagation of state from one block to the next (via 1.945 +// dest->add_incoming) is handled by these methods: 1.946 +// do_branch 1.947 +// do_if_helper 1.948 +// do_switch 1.949 +// handle_exception 1.950 + 1.951 +void SharkTopLevelBlock::do_branch(int successor_index) { 1.952 + SharkTopLevelBlock *dest = successor(successor_index); 1.953 + builder()->CreateBr(dest->entry_block()); 1.954 + dest->add_incoming(current_state()); 1.955 +} 1.956 + 1.957 +void SharkTopLevelBlock::do_if(ICmpInst::Predicate p, 1.958 + SharkValue* b, 1.959 + SharkValue* a) { 1.960 + Value *llvm_a, *llvm_b; 1.961 + if (a->is_jobject()) { 1.962 + llvm_a = a->intptr_value(builder()); 1.963 + llvm_b = b->intptr_value(builder()); 1.964 + } 1.965 + else { 1.966 + llvm_a = a->jint_value(); 1.967 + llvm_b = b->jint_value(); 1.968 + } 1.969 + do_if_helper(p, llvm_b, llvm_a, current_state(), current_state()); 1.970 +} 1.971 + 1.972 +void SharkTopLevelBlock::do_if_helper(ICmpInst::Predicate p, 1.973 + Value* b, 1.974 + Value* a, 1.975 + SharkState* if_taken_state, 1.976 + SharkState* not_taken_state) { 1.977 + SharkTopLevelBlock *if_taken = successor(ciTypeFlow::IF_TAKEN); 1.978 + SharkTopLevelBlock *not_taken = successor(ciTypeFlow::IF_NOT_TAKEN); 1.979 + 1.980 + builder()->CreateCondBr( 1.981 + builder()->CreateICmp(p, a, b), 1.982 + if_taken->entry_block(), not_taken->entry_block()); 1.983 + 1.984 + if_taken->add_incoming(if_taken_state); 1.985 + not_taken->add_incoming(not_taken_state); 1.986 +} 1.987 + 1.988 +void SharkTopLevelBlock::do_switch() { 1.989 + int len = switch_table_length(); 1.990 + 1.991 + SharkTopLevelBlock *dest_block = successor(ciTypeFlow::SWITCH_DEFAULT); 1.992 + SwitchInst *switchinst = builder()->CreateSwitch( 1.993 + pop()->jint_value(), dest_block->entry_block(), len); 1.994 + dest_block->add_incoming(current_state()); 1.995 + 1.996 + for (int i = 0; i < len; i++) { 1.997 + int dest_bci = switch_dest(i); 1.998 + if (dest_bci != switch_default_dest()) { 1.999 + dest_block = bci_successor(dest_bci); 1.1000 + switchinst->addCase( 1.1001 + LLVMValue::jint_constant(switch_key(i)), 1.1002 + dest_block->entry_block()); 1.1003 + dest_block->add_incoming(current_state()); 1.1004 + } 1.1005 + } 1.1006 +} 1.1007 + 1.1008 +ciMethod* SharkTopLevelBlock::improve_virtual_call(ciMethod* caller, 1.1009 + ciInstanceKlass* klass, 1.1010 + ciMethod* dest_method, 1.1011 + ciType* receiver_type) { 1.1012 + // If the method is obviously final then we are already done 1.1013 + if (dest_method->can_be_statically_bound()) 1.1014 + return dest_method; 1.1015 + 1.1016 + // Array methods are all inherited from Object and are monomorphic 1.1017 + if (receiver_type->is_array_klass() && 1.1018 + dest_method->holder() == java_lang_Object_klass()) 1.1019 + return dest_method; 1.1020 + 1.1021 +#ifdef SHARK_CAN_DEOPTIMIZE_ANYWHERE 1.1022 + // This code can replace a virtual call with a direct call if this 1.1023 + // class is the only one in the entire set of loaded classes that 1.1024 + // implements this method. This makes the compiled code dependent 1.1025 + // on other classes that implement the method not being loaded, a 1.1026 + // condition which is enforced by the dependency tracker. If the 1.1027 + // dependency tracker determines a method has become invalid it 1.1028 + // will mark it for recompilation, causing running copies to be 1.1029 + // deoptimized. Shark currently can't deoptimize arbitrarily like 1.1030 + // that, so this optimization cannot be used. 1.1031 + // http://icedtea.classpath.org/bugzilla/show_bug.cgi?id=481 1.1032 + 1.1033 + // All other interesting cases are instance classes 1.1034 + if (!receiver_type->is_instance_klass()) 1.1035 + return NULL; 1.1036 + 1.1037 + // Attempt to improve the receiver 1.1038 + ciInstanceKlass* actual_receiver = klass; 1.1039 + ciInstanceKlass *improved_receiver = receiver_type->as_instance_klass(); 1.1040 + if (improved_receiver->is_loaded() && 1.1041 + improved_receiver->is_initialized() && 1.1042 + !improved_receiver->is_interface() && 1.1043 + improved_receiver->is_subtype_of(actual_receiver)) { 1.1044 + actual_receiver = improved_receiver; 1.1045 + } 1.1046 + 1.1047 + // Attempt to find a monomorphic target for this call using 1.1048 + // class heirachy analysis. 1.1049 + ciInstanceKlass *calling_klass = caller->holder(); 1.1050 + ciMethod* monomorphic_target = 1.1051 + dest_method->find_monomorphic_target(calling_klass, klass, actual_receiver); 1.1052 + if (monomorphic_target != NULL) { 1.1053 + assert(!monomorphic_target->is_abstract(), "shouldn't be"); 1.1054 + 1.1055 + // Opto has a bunch of type checking here that I don't 1.1056 + // understand. It's to inhibit casting in one direction, 1.1057 + // possibly because objects in Opto can have inexact 1.1058 + // types, but I can't even tell which direction it 1.1059 + // doesn't like. For now I'm going to block *any* cast. 1.1060 + if (monomorphic_target != dest_method) { 1.1061 + if (SharkPerformanceWarnings) { 1.1062 + warning("found monomorphic target, but inhibited cast:"); 1.1063 + tty->print(" dest_method = "); 1.1064 + dest_method->print_short_name(tty); 1.1065 + tty->cr(); 1.1066 + tty->print(" monomorphic_target = "); 1.1067 + monomorphic_target->print_short_name(tty); 1.1068 + tty->cr(); 1.1069 + } 1.1070 + monomorphic_target = NULL; 1.1071 + } 1.1072 + } 1.1073 + 1.1074 + // Replace the virtual call with a direct one. This makes 1.1075 + // us dependent on that target method not getting overridden 1.1076 + // by dynamic class loading. 1.1077 + if (monomorphic_target != NULL) { 1.1078 + dependencies()->assert_unique_concrete_method( 1.1079 + actual_receiver, monomorphic_target); 1.1080 + return monomorphic_target; 1.1081 + } 1.1082 + 1.1083 + // Because Opto distinguishes exact types from inexact ones 1.1084 + // it can perform a further optimization to replace calls 1.1085 + // with non-monomorphic targets if the receiver has an exact 1.1086 + // type. We don't mark types this way, so we can't do this. 1.1087 + 1.1088 +#endif // SHARK_CAN_DEOPTIMIZE_ANYWHERE 1.1089 + 1.1090 + return NULL; 1.1091 +} 1.1092 + 1.1093 +Value *SharkTopLevelBlock::get_direct_callee(ciMethod* method) { 1.1094 + return builder()->CreateBitCast( 1.1095 + builder()->CreateInlineOop(method), 1.1096 + SharkType::methodOop_type(), 1.1097 + "callee"); 1.1098 +} 1.1099 + 1.1100 +Value *SharkTopLevelBlock::get_virtual_callee(SharkValue* receiver, 1.1101 + int vtable_index) { 1.1102 + Value *klass = builder()->CreateValueOfStructEntry( 1.1103 + receiver->jobject_value(), 1.1104 + in_ByteSize(oopDesc::klass_offset_in_bytes()), 1.1105 + SharkType::oop_type(), 1.1106 + "klass"); 1.1107 + 1.1108 + return builder()->CreateLoad( 1.1109 + builder()->CreateArrayAddress( 1.1110 + klass, 1.1111 + SharkType::methodOop_type(), 1.1112 + vtableEntry::size() * wordSize, 1.1113 + in_ByteSize(instanceKlass::vtable_start_offset() * wordSize), 1.1114 + LLVMValue::intptr_constant(vtable_index)), 1.1115 + "callee"); 1.1116 +} 1.1117 + 1.1118 +Value* SharkTopLevelBlock::get_interface_callee(SharkValue *receiver, 1.1119 + ciMethod* method) { 1.1120 + BasicBlock *loop = function()->CreateBlock("loop"); 1.1121 + BasicBlock *got_null = function()->CreateBlock("got_null"); 1.1122 + BasicBlock *not_null = function()->CreateBlock("not_null"); 1.1123 + BasicBlock *next = function()->CreateBlock("next"); 1.1124 + BasicBlock *got_entry = function()->CreateBlock("got_entry"); 1.1125 + 1.1126 + // Locate the receiver's itable 1.1127 + Value *object_klass = builder()->CreateValueOfStructEntry( 1.1128 + receiver->jobject_value(), in_ByteSize(oopDesc::klass_offset_in_bytes()), 1.1129 + SharkType::oop_type(), 1.1130 + "object_klass"); 1.1131 + 1.1132 + Value *vtable_start = builder()->CreateAdd( 1.1133 + builder()->CreatePtrToInt(object_klass, SharkType::intptr_type()), 1.1134 + LLVMValue::intptr_constant( 1.1135 + instanceKlass::vtable_start_offset() * HeapWordSize), 1.1136 + "vtable_start"); 1.1137 + 1.1138 + Value *vtable_length = builder()->CreateValueOfStructEntry( 1.1139 + object_klass, 1.1140 + in_ByteSize(instanceKlass::vtable_length_offset() * HeapWordSize), 1.1141 + SharkType::jint_type(), 1.1142 + "vtable_length"); 1.1143 + vtable_length = 1.1144 + builder()->CreateIntCast(vtable_length, SharkType::intptr_type(), false); 1.1145 + 1.1146 + bool needs_aligning = HeapWordsPerLong > 1; 1.1147 + Value *itable_start = builder()->CreateAdd( 1.1148 + vtable_start, 1.1149 + builder()->CreateShl( 1.1150 + vtable_length, 1.1151 + LLVMValue::intptr_constant(exact_log2(vtableEntry::size() * wordSize))), 1.1152 + needs_aligning ? "" : "itable_start"); 1.1153 + if (needs_aligning) { 1.1154 + itable_start = builder()->CreateAnd( 1.1155 + builder()->CreateAdd( 1.1156 + itable_start, LLVMValue::intptr_constant(BytesPerLong - 1)), 1.1157 + LLVMValue::intptr_constant(~(BytesPerLong - 1)), 1.1158 + "itable_start"); 1.1159 + } 1.1160 + 1.1161 + // Locate this interface's entry in the table 1.1162 + Value *iklass = builder()->CreateInlineOop(method->holder()); 1.1163 + BasicBlock *loop_entry = builder()->GetInsertBlock(); 1.1164 + builder()->CreateBr(loop); 1.1165 + builder()->SetInsertPoint(loop); 1.1166 + PHINode *itable_entry_addr = builder()->CreatePHI( 1.1167 + SharkType::intptr_type(), "itable_entry_addr"); 1.1168 + itable_entry_addr->addIncoming(itable_start, loop_entry); 1.1169 + 1.1170 + Value *itable_entry = builder()->CreateIntToPtr( 1.1171 + itable_entry_addr, SharkType::itableOffsetEntry_type(), "itable_entry"); 1.1172 + 1.1173 + Value *itable_iklass = builder()->CreateValueOfStructEntry( 1.1174 + itable_entry, 1.1175 + in_ByteSize(itableOffsetEntry::interface_offset_in_bytes()), 1.1176 + SharkType::oop_type(), 1.1177 + "itable_iklass"); 1.1178 + 1.1179 + builder()->CreateCondBr( 1.1180 + builder()->CreateICmpEQ(itable_iklass, LLVMValue::null()), 1.1181 + got_null, not_null); 1.1182 + 1.1183 + // A null entry means that the class doesn't implement the 1.1184 + // interface, and wasn't the same as the class checked when 1.1185 + // the interface was resolved. 1.1186 + builder()->SetInsertPoint(got_null); 1.1187 + builder()->CreateUnimplemented(__FILE__, __LINE__); 1.1188 + builder()->CreateUnreachable(); 1.1189 + 1.1190 + builder()->SetInsertPoint(not_null); 1.1191 + builder()->CreateCondBr( 1.1192 + builder()->CreateICmpEQ(itable_iklass, iklass), 1.1193 + got_entry, next); 1.1194 + 1.1195 + builder()->SetInsertPoint(next); 1.1196 + Value *next_entry = builder()->CreateAdd( 1.1197 + itable_entry_addr, 1.1198 + LLVMValue::intptr_constant(itableOffsetEntry::size() * wordSize)); 1.1199 + builder()->CreateBr(loop); 1.1200 + itable_entry_addr->addIncoming(next_entry, next); 1.1201 + 1.1202 + // Locate the method pointer 1.1203 + builder()->SetInsertPoint(got_entry); 1.1204 + Value *offset = builder()->CreateValueOfStructEntry( 1.1205 + itable_entry, 1.1206 + in_ByteSize(itableOffsetEntry::offset_offset_in_bytes()), 1.1207 + SharkType::jint_type(), 1.1208 + "offset"); 1.1209 + offset = 1.1210 + builder()->CreateIntCast(offset, SharkType::intptr_type(), false); 1.1211 + 1.1212 + return builder()->CreateLoad( 1.1213 + builder()->CreateIntToPtr( 1.1214 + builder()->CreateAdd( 1.1215 + builder()->CreateAdd( 1.1216 + builder()->CreateAdd( 1.1217 + builder()->CreatePtrToInt( 1.1218 + object_klass, SharkType::intptr_type()), 1.1219 + offset), 1.1220 + LLVMValue::intptr_constant( 1.1221 + method->itable_index() * itableMethodEntry::size() * wordSize)), 1.1222 + LLVMValue::intptr_constant( 1.1223 + itableMethodEntry::method_offset_in_bytes())), 1.1224 + PointerType::getUnqual(SharkType::methodOop_type())), 1.1225 + "callee"); 1.1226 +} 1.1227 + 1.1228 +void SharkTopLevelBlock::do_call() { 1.1229 + // Set frequently used booleans 1.1230 + bool is_static = bc() == Bytecodes::_invokestatic; 1.1231 + bool is_virtual = bc() == Bytecodes::_invokevirtual; 1.1232 + bool is_interface = bc() == Bytecodes::_invokeinterface; 1.1233 + 1.1234 + // Find the method being called 1.1235 + bool will_link; 1.1236 + ciMethod *dest_method = iter()->get_method(will_link); 1.1237 + assert(will_link, "typeflow responsibility"); 1.1238 + assert(dest_method->is_static() == is_static, "must match bc"); 1.1239 + 1.1240 + // Find the class of the method being called. Note 1.1241 + // that the superclass check in the second assertion 1.1242 + // is to cope with a hole in the spec that allows for 1.1243 + // invokeinterface instructions where the resolved 1.1244 + // method is a virtual method in java.lang.Object. 1.1245 + // javac doesn't generate code like that, but there's 1.1246 + // no reason a compliant Java compiler might not. 1.1247 + ciInstanceKlass *holder_klass = dest_method->holder(); 1.1248 + assert(holder_klass->is_loaded(), "scan_for_traps responsibility"); 1.1249 + assert(holder_klass->is_interface() || 1.1250 + holder_klass->super() == NULL || 1.1251 + !is_interface, "must match bc"); 1.1252 + ciKlass *holder = iter()->get_declared_method_holder(); 1.1253 + ciInstanceKlass *klass = 1.1254 + ciEnv::get_instance_klass_for_declared_method_holder(holder); 1.1255 + 1.1256 + // Find the receiver in the stack. We do this before 1.1257 + // trying to inline because the inliner can only use 1.1258 + // zero-checked values, not being able to perform the 1.1259 + // check itself. 1.1260 + SharkValue *receiver = NULL; 1.1261 + if (!is_static) { 1.1262 + receiver = xstack(dest_method->arg_size() - 1); 1.1263 + check_null(receiver); 1.1264 + } 1.1265 + 1.1266 + // Try to improve non-direct calls 1.1267 + bool call_is_virtual = is_virtual || is_interface; 1.1268 + ciMethod *call_method = dest_method; 1.1269 + if (call_is_virtual) { 1.1270 + ciMethod *optimized_method = improve_virtual_call( 1.1271 + target(), klass, dest_method, receiver->type()); 1.1272 + if (optimized_method) { 1.1273 + call_method = optimized_method; 1.1274 + call_is_virtual = false; 1.1275 + } 1.1276 + } 1.1277 + 1.1278 + // Try to inline the call 1.1279 + if (!call_is_virtual) { 1.1280 + if (SharkInliner::attempt_inline(call_method, current_state())) 1.1281 + return; 1.1282 + } 1.1283 + 1.1284 + // Find the method we are calling 1.1285 + Value *callee; 1.1286 + if (call_is_virtual) { 1.1287 + if (is_virtual) { 1.1288 + assert(klass->is_linked(), "scan_for_traps responsibility"); 1.1289 + int vtable_index = call_method->resolve_vtable_index( 1.1290 + target()->holder(), klass); 1.1291 + assert(vtable_index >= 0, "should be"); 1.1292 + callee = get_virtual_callee(receiver, vtable_index); 1.1293 + } 1.1294 + else { 1.1295 + assert(is_interface, "should be"); 1.1296 + callee = get_interface_callee(receiver, call_method); 1.1297 + } 1.1298 + } 1.1299 + else { 1.1300 + callee = get_direct_callee(call_method); 1.1301 + } 1.1302 + 1.1303 + // Load the SharkEntry from the callee 1.1304 + Value *base_pc = builder()->CreateValueOfStructEntry( 1.1305 + callee, methodOopDesc::from_interpreted_offset(), 1.1306 + SharkType::intptr_type(), 1.1307 + "base_pc"); 1.1308 + 1.1309 + // Load the entry point from the SharkEntry 1.1310 + Value *entry_point = builder()->CreateLoad( 1.1311 + builder()->CreateIntToPtr( 1.1312 + builder()->CreateAdd( 1.1313 + base_pc, 1.1314 + LLVMValue::intptr_constant(in_bytes(ZeroEntry::entry_point_offset()))), 1.1315 + PointerType::getUnqual( 1.1316 + PointerType::getUnqual(SharkType::entry_point_type()))), 1.1317 + "entry_point"); 1.1318 + 1.1319 + // Make the call 1.1320 + decache_for_Java_call(call_method); 1.1321 + Value *deoptimized_frames = builder()->CreateCall3( 1.1322 + entry_point, callee, base_pc, thread()); 1.1323 + 1.1324 + // If the callee got deoptimized then reexecute in the interpreter 1.1325 + BasicBlock *reexecute = function()->CreateBlock("reexecute"); 1.1326 + BasicBlock *call_completed = function()->CreateBlock("call_completed"); 1.1327 + builder()->CreateCondBr( 1.1328 + builder()->CreateICmpNE(deoptimized_frames, LLVMValue::jint_constant(0)), 1.1329 + reexecute, call_completed); 1.1330 + 1.1331 + builder()->SetInsertPoint(reexecute); 1.1332 + builder()->CreateCall2( 1.1333 + builder()->deoptimized_entry_point(), 1.1334 + builder()->CreateSub(deoptimized_frames, LLVMValue::jint_constant(1)), 1.1335 + thread()); 1.1336 + builder()->CreateBr(call_completed); 1.1337 + 1.1338 + // Cache after the call 1.1339 + builder()->SetInsertPoint(call_completed); 1.1340 + cache_after_Java_call(call_method); 1.1341 + 1.1342 + // Check for pending exceptions 1.1343 + check_pending_exception(EX_CHECK_FULL); 1.1344 + 1.1345 + // Mark that a safepoint check has occurred 1.1346 + current_state()->set_has_safepointed(true); 1.1347 +} 1.1348 + 1.1349 +bool SharkTopLevelBlock::static_subtype_check(ciKlass* check_klass, 1.1350 + ciKlass* object_klass) { 1.1351 + // If the class we're checking against is java.lang.Object 1.1352 + // then this is a no brainer. Apparently this can happen 1.1353 + // in reflective code... 1.1354 + if (check_klass == java_lang_Object_klass()) 1.1355 + return true; 1.1356 + 1.1357 + // Perform a subtype check. NB in opto's code for this 1.1358 + // (GraphKit::static_subtype_check) it says that static 1.1359 + // interface types cannot be trusted, and if opto can't 1.1360 + // trust them then I assume we can't either. 1.1361 + if (object_klass->is_loaded() && !object_klass->is_interface()) { 1.1362 + if (object_klass == check_klass) 1.1363 + return true; 1.1364 + 1.1365 + if (check_klass->is_loaded() && object_klass->is_subtype_of(check_klass)) 1.1366 + return true; 1.1367 + } 1.1368 + 1.1369 + return false; 1.1370 +} 1.1371 + 1.1372 +void SharkTopLevelBlock::do_instance_check() { 1.1373 + // Get the class we're checking against 1.1374 + bool will_link; 1.1375 + ciKlass *check_klass = iter()->get_klass(will_link); 1.1376 + 1.1377 + // Get the class of the object we're checking 1.1378 + ciKlass *object_klass = xstack(0)->type()->as_klass(); 1.1379 + 1.1380 + // Can we optimize this check away? 1.1381 + if (static_subtype_check(check_klass, object_klass)) { 1.1382 + if (bc() == Bytecodes::_instanceof) { 1.1383 + pop(); 1.1384 + push(SharkValue::jint_constant(1)); 1.1385 + } 1.1386 + return; 1.1387 + } 1.1388 + 1.1389 + // Need to check this one at runtime 1.1390 + if (will_link) 1.1391 + do_full_instance_check(check_klass); 1.1392 + else 1.1393 + do_trapping_instance_check(check_klass); 1.1394 +} 1.1395 + 1.1396 +bool SharkTopLevelBlock::maybe_do_instanceof_if() { 1.1397 + // Get the class we're checking against 1.1398 + bool will_link; 1.1399 + ciKlass *check_klass = iter()->get_klass(will_link); 1.1400 + 1.1401 + // If the class is unloaded then the instanceof 1.1402 + // cannot possibly succeed. 1.1403 + if (!will_link) 1.1404 + return false; 1.1405 + 1.1406 + // Keep a copy of the object we're checking 1.1407 + SharkValue *old_object = xstack(0); 1.1408 + 1.1409 + // Get the class of the object we're checking 1.1410 + ciKlass *object_klass = old_object->type()->as_klass(); 1.1411 + 1.1412 + // If the instanceof can be optimized away at compile time 1.1413 + // then any subsequent checkcasts will be too so we handle 1.1414 + // it normally. 1.1415 + if (static_subtype_check(check_klass, object_klass)) 1.1416 + return false; 1.1417 + 1.1418 + // Perform the instance check 1.1419 + do_full_instance_check(check_klass); 1.1420 + Value *result = pop()->jint_value(); 1.1421 + 1.1422 + // Create the casted object 1.1423 + SharkValue *new_object = SharkValue::create_generic( 1.1424 + check_klass, old_object->jobject_value(), old_object->zero_checked()); 1.1425 + 1.1426 + // Create two copies of the current state, one with the 1.1427 + // original object and one with all instances of the 1.1428 + // original object replaced with the new, casted object. 1.1429 + SharkState *new_state = current_state(); 1.1430 + SharkState *old_state = new_state->copy(); 1.1431 + new_state->replace_all(old_object, new_object); 1.1432 + 1.1433 + // Perform the check-and-branch 1.1434 + switch (iter()->next_bc()) { 1.1435 + case Bytecodes::_ifeq: 1.1436 + // branch if not an instance 1.1437 + do_if_helper( 1.1438 + ICmpInst::ICMP_EQ, 1.1439 + LLVMValue::jint_constant(0), result, 1.1440 + old_state, new_state); 1.1441 + break; 1.1442 + 1.1443 + case Bytecodes::_ifne: 1.1444 + // branch if an instance 1.1445 + do_if_helper( 1.1446 + ICmpInst::ICMP_NE, 1.1447 + LLVMValue::jint_constant(0), result, 1.1448 + new_state, old_state); 1.1449 + break; 1.1450 + 1.1451 + default: 1.1452 + ShouldNotReachHere(); 1.1453 + } 1.1454 + 1.1455 + return true; 1.1456 +} 1.1457 + 1.1458 +void SharkTopLevelBlock::do_full_instance_check(ciKlass* klass) { 1.1459 + BasicBlock *not_null = function()->CreateBlock("not_null"); 1.1460 + BasicBlock *subtype_check = function()->CreateBlock("subtype_check"); 1.1461 + BasicBlock *is_instance = function()->CreateBlock("is_instance"); 1.1462 + BasicBlock *not_instance = function()->CreateBlock("not_instance"); 1.1463 + BasicBlock *merge1 = function()->CreateBlock("merge1"); 1.1464 + BasicBlock *merge2 = function()->CreateBlock("merge2"); 1.1465 + 1.1466 + enum InstanceCheckStates { 1.1467 + IC_IS_NULL, 1.1468 + IC_IS_INSTANCE, 1.1469 + IC_NOT_INSTANCE, 1.1470 + }; 1.1471 + 1.1472 + // Pop the object off the stack 1.1473 + Value *object = pop()->jobject_value(); 1.1474 + 1.1475 + // Null objects aren't instances of anything 1.1476 + builder()->CreateCondBr( 1.1477 + builder()->CreateICmpEQ(object, LLVMValue::null()), 1.1478 + merge2, not_null); 1.1479 + BasicBlock *null_block = builder()->GetInsertBlock(); 1.1480 + 1.1481 + // Get the class we're checking against 1.1482 + builder()->SetInsertPoint(not_null); 1.1483 + Value *check_klass = builder()->CreateInlineOop(klass); 1.1484 + 1.1485 + // Get the class of the object being tested 1.1486 + Value *object_klass = builder()->CreateValueOfStructEntry( 1.1487 + object, in_ByteSize(oopDesc::klass_offset_in_bytes()), 1.1488 + SharkType::oop_type(), 1.1489 + "object_klass"); 1.1490 + 1.1491 + // Perform the check 1.1492 + builder()->CreateCondBr( 1.1493 + builder()->CreateICmpEQ(check_klass, object_klass), 1.1494 + is_instance, subtype_check); 1.1495 + 1.1496 + builder()->SetInsertPoint(subtype_check); 1.1497 + builder()->CreateCondBr( 1.1498 + builder()->CreateICmpNE( 1.1499 + builder()->CreateCall2( 1.1500 + builder()->is_subtype_of(), check_klass, object_klass), 1.1501 + LLVMValue::jbyte_constant(0)), 1.1502 + is_instance, not_instance); 1.1503 + 1.1504 + builder()->SetInsertPoint(is_instance); 1.1505 + builder()->CreateBr(merge1); 1.1506 + 1.1507 + builder()->SetInsertPoint(not_instance); 1.1508 + builder()->CreateBr(merge1); 1.1509 + 1.1510 + // First merge 1.1511 + builder()->SetInsertPoint(merge1); 1.1512 + PHINode *nonnull_result = builder()->CreatePHI( 1.1513 + SharkType::jint_type(), "nonnull_result"); 1.1514 + nonnull_result->addIncoming( 1.1515 + LLVMValue::jint_constant(IC_IS_INSTANCE), is_instance); 1.1516 + nonnull_result->addIncoming( 1.1517 + LLVMValue::jint_constant(IC_NOT_INSTANCE), not_instance); 1.1518 + BasicBlock *nonnull_block = builder()->GetInsertBlock(); 1.1519 + builder()->CreateBr(merge2); 1.1520 + 1.1521 + // Second merge 1.1522 + builder()->SetInsertPoint(merge2); 1.1523 + PHINode *result = builder()->CreatePHI( 1.1524 + SharkType::jint_type(), "result"); 1.1525 + result->addIncoming(LLVMValue::jint_constant(IC_IS_NULL), null_block); 1.1526 + result->addIncoming(nonnull_result, nonnull_block); 1.1527 + 1.1528 + // Handle the result 1.1529 + if (bc() == Bytecodes::_checkcast) { 1.1530 + BasicBlock *failure = function()->CreateBlock("failure"); 1.1531 + BasicBlock *success = function()->CreateBlock("success"); 1.1532 + 1.1533 + builder()->CreateCondBr( 1.1534 + builder()->CreateICmpNE( 1.1535 + result, LLVMValue::jint_constant(IC_NOT_INSTANCE)), 1.1536 + success, failure); 1.1537 + 1.1538 + builder()->SetInsertPoint(failure); 1.1539 + SharkState *saved_state = current_state()->copy(); 1.1540 + 1.1541 + call_vm( 1.1542 + builder()->throw_ClassCastException(), 1.1543 + builder()->CreateIntToPtr( 1.1544 + LLVMValue::intptr_constant((intptr_t) __FILE__), 1.1545 + PointerType::getUnqual(SharkType::jbyte_type())), 1.1546 + LLVMValue::jint_constant(__LINE__), 1.1547 + EX_CHECK_NONE); 1.1548 + 1.1549 + Value *pending_exception = get_pending_exception(); 1.1550 + clear_pending_exception(); 1.1551 + handle_exception(pending_exception, EX_CHECK_FULL); 1.1552 + 1.1553 + set_current_state(saved_state); 1.1554 + builder()->SetInsertPoint(success); 1.1555 + push(SharkValue::create_generic(klass, object, false)); 1.1556 + } 1.1557 + else { 1.1558 + push( 1.1559 + SharkValue::create_jint( 1.1560 + builder()->CreateIntCast( 1.1561 + builder()->CreateICmpEQ( 1.1562 + result, LLVMValue::jint_constant(IC_IS_INSTANCE)), 1.1563 + SharkType::jint_type(), false), false)); 1.1564 + } 1.1565 +} 1.1566 + 1.1567 +void SharkTopLevelBlock::do_trapping_instance_check(ciKlass* klass) { 1.1568 + BasicBlock *not_null = function()->CreateBlock("not_null"); 1.1569 + BasicBlock *is_null = function()->CreateBlock("null"); 1.1570 + 1.1571 + // Leave the object on the stack so it's there if we trap 1.1572 + builder()->CreateCondBr( 1.1573 + builder()->CreateICmpEQ(xstack(0)->jobject_value(), LLVMValue::null()), 1.1574 + is_null, not_null); 1.1575 + SharkState *saved_state = current_state()->copy(); 1.1576 + 1.1577 + // If it's not null then we need to trap 1.1578 + builder()->SetInsertPoint(not_null); 1.1579 + set_current_state(saved_state->copy()); 1.1580 + do_trap( 1.1581 + Deoptimization::make_trap_request( 1.1582 + Deoptimization::Reason_uninitialized, 1.1583 + Deoptimization::Action_reinterpret)); 1.1584 + 1.1585 + // If it's null then we're ok 1.1586 + builder()->SetInsertPoint(is_null); 1.1587 + set_current_state(saved_state); 1.1588 + if (bc() == Bytecodes::_checkcast) { 1.1589 + push(SharkValue::create_generic(klass, pop()->jobject_value(), false)); 1.1590 + } 1.1591 + else { 1.1592 + pop(); 1.1593 + push(SharkValue::jint_constant(0)); 1.1594 + } 1.1595 +} 1.1596 + 1.1597 +void SharkTopLevelBlock::do_new() { 1.1598 + bool will_link; 1.1599 + ciInstanceKlass* klass = iter()->get_klass(will_link)->as_instance_klass(); 1.1600 + assert(will_link, "typeflow responsibility"); 1.1601 + 1.1602 + BasicBlock *got_tlab = NULL; 1.1603 + BasicBlock *heap_alloc = NULL; 1.1604 + BasicBlock *retry = NULL; 1.1605 + BasicBlock *got_heap = NULL; 1.1606 + BasicBlock *initialize = NULL; 1.1607 + BasicBlock *got_fast = NULL; 1.1608 + BasicBlock *slow_alloc_and_init = NULL; 1.1609 + BasicBlock *got_slow = NULL; 1.1610 + BasicBlock *push_object = NULL; 1.1611 + 1.1612 + SharkState *fast_state = NULL; 1.1613 + 1.1614 + Value *tlab_object = NULL; 1.1615 + Value *heap_object = NULL; 1.1616 + Value *fast_object = NULL; 1.1617 + Value *slow_object = NULL; 1.1618 + Value *object = NULL; 1.1619 + 1.1620 + // The fast path 1.1621 + if (!Klass::layout_helper_needs_slow_path(klass->layout_helper())) { 1.1622 + if (UseTLAB) { 1.1623 + got_tlab = function()->CreateBlock("got_tlab"); 1.1624 + heap_alloc = function()->CreateBlock("heap_alloc"); 1.1625 + } 1.1626 + retry = function()->CreateBlock("retry"); 1.1627 + got_heap = function()->CreateBlock("got_heap"); 1.1628 + initialize = function()->CreateBlock("initialize"); 1.1629 + slow_alloc_and_init = function()->CreateBlock("slow_alloc_and_init"); 1.1630 + push_object = function()->CreateBlock("push_object"); 1.1631 + 1.1632 + size_t size_in_bytes = klass->size_helper() << LogHeapWordSize; 1.1633 + 1.1634 + // Thread local allocation 1.1635 + if (UseTLAB) { 1.1636 + Value *top_addr = builder()->CreateAddressOfStructEntry( 1.1637 + thread(), Thread::tlab_top_offset(), 1.1638 + PointerType::getUnqual(SharkType::intptr_type()), 1.1639 + "top_addr"); 1.1640 + 1.1641 + Value *end = builder()->CreateValueOfStructEntry( 1.1642 + thread(), Thread::tlab_end_offset(), 1.1643 + SharkType::intptr_type(), 1.1644 + "end"); 1.1645 + 1.1646 + Value *old_top = builder()->CreateLoad(top_addr, "old_top"); 1.1647 + Value *new_top = builder()->CreateAdd( 1.1648 + old_top, LLVMValue::intptr_constant(size_in_bytes)); 1.1649 + 1.1650 + builder()->CreateCondBr( 1.1651 + builder()->CreateICmpULE(new_top, end), 1.1652 + got_tlab, heap_alloc); 1.1653 + 1.1654 + builder()->SetInsertPoint(got_tlab); 1.1655 + tlab_object = builder()->CreateIntToPtr( 1.1656 + old_top, SharkType::oop_type(), "tlab_object"); 1.1657 + 1.1658 + builder()->CreateStore(new_top, top_addr); 1.1659 + builder()->CreateBr(initialize); 1.1660 + 1.1661 + builder()->SetInsertPoint(heap_alloc); 1.1662 + } 1.1663 + 1.1664 + // Heap allocation 1.1665 + Value *top_addr = builder()->CreateIntToPtr( 1.1666 + LLVMValue::intptr_constant((intptr_t) Universe::heap()->top_addr()), 1.1667 + PointerType::getUnqual(SharkType::intptr_type()), 1.1668 + "top_addr"); 1.1669 + 1.1670 + Value *end = builder()->CreateLoad( 1.1671 + builder()->CreateIntToPtr( 1.1672 + LLVMValue::intptr_constant((intptr_t) Universe::heap()->end_addr()), 1.1673 + PointerType::getUnqual(SharkType::intptr_type())), 1.1674 + "end"); 1.1675 + 1.1676 + builder()->CreateBr(retry); 1.1677 + builder()->SetInsertPoint(retry); 1.1678 + 1.1679 + Value *old_top = builder()->CreateLoad(top_addr, "top"); 1.1680 + Value *new_top = builder()->CreateAdd( 1.1681 + old_top, LLVMValue::intptr_constant(size_in_bytes)); 1.1682 + 1.1683 + builder()->CreateCondBr( 1.1684 + builder()->CreateICmpULE(new_top, end), 1.1685 + got_heap, slow_alloc_and_init); 1.1686 + 1.1687 + builder()->SetInsertPoint(got_heap); 1.1688 + heap_object = builder()->CreateIntToPtr( 1.1689 + old_top, SharkType::oop_type(), "heap_object"); 1.1690 + 1.1691 + Value *check = builder()->CreateCmpxchgPtr(new_top, top_addr, old_top); 1.1692 + builder()->CreateCondBr( 1.1693 + builder()->CreateICmpEQ(old_top, check), 1.1694 + initialize, retry); 1.1695 + 1.1696 + // Initialize the object 1.1697 + builder()->SetInsertPoint(initialize); 1.1698 + if (tlab_object) { 1.1699 + PHINode *phi = builder()->CreatePHI( 1.1700 + SharkType::oop_type(), "fast_object"); 1.1701 + phi->addIncoming(tlab_object, got_tlab); 1.1702 + phi->addIncoming(heap_object, got_heap); 1.1703 + fast_object = phi; 1.1704 + } 1.1705 + else { 1.1706 + fast_object = heap_object; 1.1707 + } 1.1708 + 1.1709 + builder()->CreateMemset( 1.1710 + builder()->CreateBitCast( 1.1711 + fast_object, PointerType::getUnqual(SharkType::jbyte_type())), 1.1712 + LLVMValue::jbyte_constant(0), 1.1713 + LLVMValue::jint_constant(size_in_bytes), 1.1714 + LLVMValue::jint_constant(HeapWordSize)); 1.1715 + 1.1716 + Value *mark_addr = builder()->CreateAddressOfStructEntry( 1.1717 + fast_object, in_ByteSize(oopDesc::mark_offset_in_bytes()), 1.1718 + PointerType::getUnqual(SharkType::intptr_type()), 1.1719 + "mark_addr"); 1.1720 + 1.1721 + Value *klass_addr = builder()->CreateAddressOfStructEntry( 1.1722 + fast_object, in_ByteSize(oopDesc::klass_offset_in_bytes()), 1.1723 + PointerType::getUnqual(SharkType::oop_type()), 1.1724 + "klass_addr"); 1.1725 + 1.1726 + // Set the mark 1.1727 + intptr_t mark; 1.1728 + if (UseBiasedLocking) { 1.1729 + Unimplemented(); 1.1730 + } 1.1731 + else { 1.1732 + mark = (intptr_t) markOopDesc::prototype(); 1.1733 + } 1.1734 + builder()->CreateStore(LLVMValue::intptr_constant(mark), mark_addr); 1.1735 + 1.1736 + // Set the class 1.1737 + Value *rtklass = builder()->CreateInlineOop(klass); 1.1738 + builder()->CreateStore(rtklass, klass_addr); 1.1739 + got_fast = builder()->GetInsertBlock(); 1.1740 + 1.1741 + builder()->CreateBr(push_object); 1.1742 + builder()->SetInsertPoint(slow_alloc_and_init); 1.1743 + fast_state = current_state()->copy(); 1.1744 + } 1.1745 + 1.1746 + // The slow path 1.1747 + call_vm( 1.1748 + builder()->new_instance(), 1.1749 + LLVMValue::jint_constant(iter()->get_klass_index()), 1.1750 + EX_CHECK_FULL); 1.1751 + slow_object = get_vm_result(); 1.1752 + got_slow = builder()->GetInsertBlock(); 1.1753 + 1.1754 + // Push the object 1.1755 + if (push_object) { 1.1756 + builder()->CreateBr(push_object); 1.1757 + builder()->SetInsertPoint(push_object); 1.1758 + } 1.1759 + if (fast_object) { 1.1760 + PHINode *phi = builder()->CreatePHI(SharkType::oop_type(), "object"); 1.1761 + phi->addIncoming(fast_object, got_fast); 1.1762 + phi->addIncoming(slow_object, got_slow); 1.1763 + object = phi; 1.1764 + current_state()->merge(fast_state, got_fast, got_slow); 1.1765 + } 1.1766 + else { 1.1767 + object = slow_object; 1.1768 + } 1.1769 + 1.1770 + push(SharkValue::create_jobject(object, true)); 1.1771 +} 1.1772 + 1.1773 +void SharkTopLevelBlock::do_newarray() { 1.1774 + BasicType type = (BasicType) iter()->get_index(); 1.1775 + 1.1776 + call_vm( 1.1777 + builder()->newarray(), 1.1778 + LLVMValue::jint_constant(type), 1.1779 + pop()->jint_value(), 1.1780 + EX_CHECK_FULL); 1.1781 + 1.1782 + ciArrayKlass *array_klass = ciArrayKlass::make(ciType::make(type)); 1.1783 + push(SharkValue::create_generic(array_klass, get_vm_result(), true)); 1.1784 +} 1.1785 + 1.1786 +void SharkTopLevelBlock::do_anewarray() { 1.1787 + bool will_link; 1.1788 + ciKlass *klass = iter()->get_klass(will_link); 1.1789 + assert(will_link, "typeflow responsibility"); 1.1790 + 1.1791 + ciObjArrayKlass *array_klass = ciObjArrayKlass::make(klass); 1.1792 + if (!array_klass->is_loaded()) { 1.1793 + Unimplemented(); 1.1794 + } 1.1795 + 1.1796 + call_vm( 1.1797 + builder()->anewarray(), 1.1798 + LLVMValue::jint_constant(iter()->get_klass_index()), 1.1799 + pop()->jint_value(), 1.1800 + EX_CHECK_FULL); 1.1801 + 1.1802 + push(SharkValue::create_generic(array_klass, get_vm_result(), true)); 1.1803 +} 1.1804 + 1.1805 +void SharkTopLevelBlock::do_multianewarray() { 1.1806 + bool will_link; 1.1807 + ciArrayKlass *array_klass = iter()->get_klass(will_link)->as_array_klass(); 1.1808 + assert(will_link, "typeflow responsibility"); 1.1809 + 1.1810 + // The dimensions are stack values, so we use their slots for the 1.1811 + // dimensions array. Note that we are storing them in the reverse 1.1812 + // of normal stack order. 1.1813 + int ndims = iter()->get_dimensions(); 1.1814 + 1.1815 + Value *dimensions = stack()->slot_addr( 1.1816 + stack()->stack_slots_offset() + max_stack() - xstack_depth(), 1.1817 + ArrayType::get(SharkType::jint_type(), ndims), 1.1818 + "dimensions"); 1.1819 + 1.1820 + for (int i = 0; i < ndims; i++) { 1.1821 + builder()->CreateStore( 1.1822 + xstack(ndims - 1 - i)->jint_value(), 1.1823 + builder()->CreateStructGEP(dimensions, i)); 1.1824 + } 1.1825 + 1.1826 + call_vm( 1.1827 + builder()->multianewarray(), 1.1828 + LLVMValue::jint_constant(iter()->get_klass_index()), 1.1829 + LLVMValue::jint_constant(ndims), 1.1830 + builder()->CreateStructGEP(dimensions, 0), 1.1831 + EX_CHECK_FULL); 1.1832 + 1.1833 + // Now we can pop the dimensions off the stack 1.1834 + for (int i = 0; i < ndims; i++) 1.1835 + pop(); 1.1836 + 1.1837 + push(SharkValue::create_generic(array_klass, get_vm_result(), true)); 1.1838 +} 1.1839 + 1.1840 +void SharkTopLevelBlock::acquire_method_lock() { 1.1841 + Value *lockee; 1.1842 + if (target()->is_static()) 1.1843 + lockee = builder()->CreateInlineOop(target()->holder()->java_mirror()); 1.1844 + else 1.1845 + lockee = local(0)->jobject_value(); 1.1846 + 1.1847 + iter()->force_bci(start()); // for the decache in acquire_lock 1.1848 + acquire_lock(lockee, EX_CHECK_NO_CATCH); 1.1849 +} 1.1850 + 1.1851 +void SharkTopLevelBlock::do_monitorenter() { 1.1852 + SharkValue *lockee = pop(); 1.1853 + check_null(lockee); 1.1854 + acquire_lock(lockee->jobject_value(), EX_CHECK_FULL); 1.1855 +} 1.1856 + 1.1857 +void SharkTopLevelBlock::do_monitorexit() { 1.1858 + pop(); // don't need this (monitors are block structured) 1.1859 + release_lock(EX_CHECK_NO_CATCH); 1.1860 +} 1.1861 + 1.1862 +void SharkTopLevelBlock::acquire_lock(Value *lockee, int exception_action) { 1.1863 + BasicBlock *try_recursive = function()->CreateBlock("try_recursive"); 1.1864 + BasicBlock *got_recursive = function()->CreateBlock("got_recursive"); 1.1865 + BasicBlock *not_recursive = function()->CreateBlock("not_recursive"); 1.1866 + BasicBlock *acquired_fast = function()->CreateBlock("acquired_fast"); 1.1867 + BasicBlock *lock_acquired = function()->CreateBlock("lock_acquired"); 1.1868 + 1.1869 + int monitor = num_monitors(); 1.1870 + Value *monitor_addr = stack()->monitor_addr(monitor); 1.1871 + Value *monitor_object_addr = stack()->monitor_object_addr(monitor); 1.1872 + Value *monitor_header_addr = stack()->monitor_header_addr(monitor); 1.1873 + 1.1874 + // Store the object and mark the slot as live 1.1875 + builder()->CreateStore(lockee, monitor_object_addr); 1.1876 + set_num_monitors(monitor + 1); 1.1877 + 1.1878 + // Try a simple lock 1.1879 + Value *mark_addr = builder()->CreateAddressOfStructEntry( 1.1880 + lockee, in_ByteSize(oopDesc::mark_offset_in_bytes()), 1.1881 + PointerType::getUnqual(SharkType::intptr_type()), 1.1882 + "mark_addr"); 1.1883 + 1.1884 + Value *mark = builder()->CreateLoad(mark_addr, "mark"); 1.1885 + Value *disp = builder()->CreateOr( 1.1886 + mark, LLVMValue::intptr_constant(markOopDesc::unlocked_value), "disp"); 1.1887 + builder()->CreateStore(disp, monitor_header_addr); 1.1888 + 1.1889 + Value *lock = builder()->CreatePtrToInt( 1.1890 + monitor_header_addr, SharkType::intptr_type()); 1.1891 + Value *check = builder()->CreateCmpxchgPtr(lock, mark_addr, disp); 1.1892 + builder()->CreateCondBr( 1.1893 + builder()->CreateICmpEQ(disp, check), 1.1894 + acquired_fast, try_recursive); 1.1895 + 1.1896 + // Locking failed, but maybe this thread already owns it 1.1897 + builder()->SetInsertPoint(try_recursive); 1.1898 + Value *addr = builder()->CreateAnd( 1.1899 + disp, 1.1900 + LLVMValue::intptr_constant(~markOopDesc::lock_mask_in_place)); 1.1901 + 1.1902 + // NB we use the entire stack, but JavaThread::is_lock_owned() 1.1903 + // uses a more limited range. I don't think it hurts though... 1.1904 + Value *stack_limit = builder()->CreateValueOfStructEntry( 1.1905 + thread(), Thread::stack_base_offset(), 1.1906 + SharkType::intptr_type(), 1.1907 + "stack_limit"); 1.1908 + 1.1909 + assert(sizeof(size_t) == sizeof(intptr_t), "should be"); 1.1910 + Value *stack_size = builder()->CreateValueOfStructEntry( 1.1911 + thread(), Thread::stack_size_offset(), 1.1912 + SharkType::intptr_type(), 1.1913 + "stack_size"); 1.1914 + 1.1915 + Value *stack_start = 1.1916 + builder()->CreateSub(stack_limit, stack_size, "stack_start"); 1.1917 + 1.1918 + builder()->CreateCondBr( 1.1919 + builder()->CreateAnd( 1.1920 + builder()->CreateICmpUGE(addr, stack_start), 1.1921 + builder()->CreateICmpULT(addr, stack_limit)), 1.1922 + got_recursive, not_recursive); 1.1923 + 1.1924 + builder()->SetInsertPoint(got_recursive); 1.1925 + builder()->CreateStore(LLVMValue::intptr_constant(0), monitor_header_addr); 1.1926 + builder()->CreateBr(acquired_fast); 1.1927 + 1.1928 + // Create an edge for the state merge 1.1929 + builder()->SetInsertPoint(acquired_fast); 1.1930 + SharkState *fast_state = current_state()->copy(); 1.1931 + builder()->CreateBr(lock_acquired); 1.1932 + 1.1933 + // It's not a recursive case so we need to drop into the runtime 1.1934 + builder()->SetInsertPoint(not_recursive); 1.1935 + call_vm( 1.1936 + builder()->monitorenter(), monitor_addr, 1.1937 + exception_action | EAM_MONITOR_FUDGE); 1.1938 + BasicBlock *acquired_slow = builder()->GetInsertBlock(); 1.1939 + builder()->CreateBr(lock_acquired); 1.1940 + 1.1941 + // All done 1.1942 + builder()->SetInsertPoint(lock_acquired); 1.1943 + current_state()->merge(fast_state, acquired_fast, acquired_slow); 1.1944 +} 1.1945 + 1.1946 +void SharkTopLevelBlock::release_lock(int exception_action) { 1.1947 + BasicBlock *not_recursive = function()->CreateBlock("not_recursive"); 1.1948 + BasicBlock *released_fast = function()->CreateBlock("released_fast"); 1.1949 + BasicBlock *slow_path = function()->CreateBlock("slow_path"); 1.1950 + BasicBlock *lock_released = function()->CreateBlock("lock_released"); 1.1951 + 1.1952 + int monitor = num_monitors() - 1; 1.1953 + Value *monitor_addr = stack()->monitor_addr(monitor); 1.1954 + Value *monitor_object_addr = stack()->monitor_object_addr(monitor); 1.1955 + Value *monitor_header_addr = stack()->monitor_header_addr(monitor); 1.1956 + 1.1957 + // If it is recursive then we're already done 1.1958 + Value *disp = builder()->CreateLoad(monitor_header_addr); 1.1959 + builder()->CreateCondBr( 1.1960 + builder()->CreateICmpEQ(disp, LLVMValue::intptr_constant(0)), 1.1961 + released_fast, not_recursive); 1.1962 + 1.1963 + // Try a simple unlock 1.1964 + builder()->SetInsertPoint(not_recursive); 1.1965 + 1.1966 + Value *lock = builder()->CreatePtrToInt( 1.1967 + monitor_header_addr, SharkType::intptr_type()); 1.1968 + 1.1969 + Value *lockee = builder()->CreateLoad(monitor_object_addr); 1.1970 + 1.1971 + Value *mark_addr = builder()->CreateAddressOfStructEntry( 1.1972 + lockee, in_ByteSize(oopDesc::mark_offset_in_bytes()), 1.1973 + PointerType::getUnqual(SharkType::intptr_type()), 1.1974 + "mark_addr"); 1.1975 + 1.1976 + Value *check = builder()->CreateCmpxchgPtr(disp, mark_addr, lock); 1.1977 + builder()->CreateCondBr( 1.1978 + builder()->CreateICmpEQ(lock, check), 1.1979 + released_fast, slow_path); 1.1980 + 1.1981 + // Create an edge for the state merge 1.1982 + builder()->SetInsertPoint(released_fast); 1.1983 + SharkState *fast_state = current_state()->copy(); 1.1984 + builder()->CreateBr(lock_released); 1.1985 + 1.1986 + // Need to drop into the runtime to release this one 1.1987 + builder()->SetInsertPoint(slow_path); 1.1988 + call_vm(builder()->monitorexit(), monitor_addr, exception_action); 1.1989 + BasicBlock *released_slow = builder()->GetInsertBlock(); 1.1990 + builder()->CreateBr(lock_released); 1.1991 + 1.1992 + // All done 1.1993 + builder()->SetInsertPoint(lock_released); 1.1994 + current_state()->merge(fast_state, released_fast, released_slow); 1.1995 + 1.1996 + // The object slot is now dead 1.1997 + set_num_monitors(monitor); 1.1998 +}