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