src/share/vm/shark/sharkTopLevelBlock.cpp

Wed, 27 Apr 2016 01:25:04 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:25:04 +0800
changeset 0
f90c822e73f8
child 6876
710a3c8b516e
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/hotspot/
changeset: 6782:28b50d07f6f8
tag: jdk8u25-b17

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * Copyright 2008, 2009, 2010 Red Hat, Inc.
aoqi@0 4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 5 *
aoqi@0 6 * This code is free software; you can redistribute it and/or modify it
aoqi@0 7 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 8 * published by the Free Software Foundation.
aoqi@0 9 *
aoqi@0 10 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 13 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 14 * accompanied this code).
aoqi@0 15 *
aoqi@0 16 * You should have received a copy of the GNU General Public License version
aoqi@0 17 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 19 *
aoqi@0 20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 21 * or visit www.oracle.com if you need additional information or have any
aoqi@0 22 * questions.
aoqi@0 23 *
aoqi@0 24 */
aoqi@0 25
aoqi@0 26 #include "precompiled.hpp"
aoqi@0 27 #include "ci/ciField.hpp"
aoqi@0 28 #include "ci/ciInstance.hpp"
aoqi@0 29 #include "ci/ciObjArrayKlass.hpp"
aoqi@0 30 #include "ci/ciStreams.hpp"
aoqi@0 31 #include "ci/ciType.hpp"
aoqi@0 32 #include "ci/ciTypeFlow.hpp"
aoqi@0 33 #include "interpreter/bytecodes.hpp"
aoqi@0 34 #include "memory/allocation.hpp"
aoqi@0 35 #include "runtime/deoptimization.hpp"
aoqi@0 36 #include "shark/llvmHeaders.hpp"
aoqi@0 37 #include "shark/llvmValue.hpp"
aoqi@0 38 #include "shark/sharkBuilder.hpp"
aoqi@0 39 #include "shark/sharkCacheDecache.hpp"
aoqi@0 40 #include "shark/sharkConstant.hpp"
aoqi@0 41 #include "shark/sharkInliner.hpp"
aoqi@0 42 #include "shark/sharkState.hpp"
aoqi@0 43 #include "shark/sharkTopLevelBlock.hpp"
aoqi@0 44 #include "shark/sharkValue.hpp"
aoqi@0 45 #include "shark/shark_globals.hpp"
aoqi@0 46 #include "utilities/debug.hpp"
aoqi@0 47
aoqi@0 48 using namespace llvm;
aoqi@0 49
aoqi@0 50 void SharkTopLevelBlock::scan_for_traps() {
aoqi@0 51 // If typeflow found a trap then don't scan past it
aoqi@0 52 int limit_bci = ciblock()->has_trap() ? ciblock()->trap_bci() : limit();
aoqi@0 53
aoqi@0 54 // Scan the bytecode for traps that are always hit
aoqi@0 55 iter()->reset_to_bci(start());
aoqi@0 56 while (iter()->next_bci() < limit_bci) {
aoqi@0 57 iter()->next();
aoqi@0 58
aoqi@0 59 ciField *field;
aoqi@0 60 ciMethod *method;
aoqi@0 61 ciInstanceKlass *klass;
aoqi@0 62 bool will_link;
aoqi@0 63 bool is_field;
aoqi@0 64
aoqi@0 65 switch (bc()) {
aoqi@0 66 case Bytecodes::_ldc:
aoqi@0 67 case Bytecodes::_ldc_w:
aoqi@0 68 case Bytecodes::_ldc2_w:
aoqi@0 69 if (!SharkConstant::for_ldc(iter())->is_loaded()) {
aoqi@0 70 set_trap(
aoqi@0 71 Deoptimization::make_trap_request(
aoqi@0 72 Deoptimization::Reason_uninitialized,
aoqi@0 73 Deoptimization::Action_reinterpret), bci());
aoqi@0 74 return;
aoqi@0 75 }
aoqi@0 76 break;
aoqi@0 77
aoqi@0 78 case Bytecodes::_getfield:
aoqi@0 79 case Bytecodes::_getstatic:
aoqi@0 80 case Bytecodes::_putfield:
aoqi@0 81 case Bytecodes::_putstatic:
aoqi@0 82 field = iter()->get_field(will_link);
aoqi@0 83 assert(will_link, "typeflow responsibility");
aoqi@0 84 is_field = (bc() == Bytecodes::_getfield || bc() == Bytecodes::_putfield);
aoqi@0 85
aoqi@0 86 // If the bytecode does not match the field then bail out to
aoqi@0 87 // the interpreter to throw an IncompatibleClassChangeError
aoqi@0 88 if (is_field == field->is_static()) {
aoqi@0 89 set_trap(
aoqi@0 90 Deoptimization::make_trap_request(
aoqi@0 91 Deoptimization::Reason_unhandled,
aoqi@0 92 Deoptimization::Action_none), bci());
aoqi@0 93 return;
aoqi@0 94 }
aoqi@0 95
aoqi@0 96 // Bail out if we are trying to access a static variable
aoqi@0 97 // before the class initializer has completed.
aoqi@0 98 if (!is_field && !field->holder()->is_initialized()) {
aoqi@0 99 if (!static_field_ok_in_clinit(field)) {
aoqi@0 100 set_trap(
aoqi@0 101 Deoptimization::make_trap_request(
aoqi@0 102 Deoptimization::Reason_uninitialized,
aoqi@0 103 Deoptimization::Action_reinterpret), bci());
aoqi@0 104 return;
aoqi@0 105 }
aoqi@0 106 }
aoqi@0 107 break;
aoqi@0 108
aoqi@0 109 case Bytecodes::_invokestatic:
aoqi@0 110 case Bytecodes::_invokespecial:
aoqi@0 111 case Bytecodes::_invokevirtual:
aoqi@0 112 case Bytecodes::_invokeinterface:
aoqi@0 113 ciSignature* sig;
aoqi@0 114 method = iter()->get_method(will_link, &sig);
aoqi@0 115 assert(will_link, "typeflow responsibility");
aoqi@0 116 // We can't compile calls to method handle intrinsics, because we use
aoqi@0 117 // the interpreter entry points and they expect the top frame to be an
aoqi@0 118 // interpreter frame. We need to implement the intrinsics for Shark.
aoqi@0 119 if (method->is_method_handle_intrinsic() || method->is_compiled_lambda_form()) {
aoqi@0 120 if (SharkPerformanceWarnings) {
aoqi@0 121 warning("JSR292 optimization not yet implemented in Shark");
aoqi@0 122 }
aoqi@0 123 set_trap(
aoqi@0 124 Deoptimization::make_trap_request(
aoqi@0 125 Deoptimization::Reason_unhandled,
aoqi@0 126 Deoptimization::Action_make_not_compilable), bci());
aoqi@0 127 return;
aoqi@0 128 }
aoqi@0 129 if (!method->holder()->is_linked()) {
aoqi@0 130 set_trap(
aoqi@0 131 Deoptimization::make_trap_request(
aoqi@0 132 Deoptimization::Reason_uninitialized,
aoqi@0 133 Deoptimization::Action_reinterpret), bci());
aoqi@0 134 return;
aoqi@0 135 }
aoqi@0 136
aoqi@0 137 if (bc() == Bytecodes::_invokevirtual) {
aoqi@0 138 klass = ciEnv::get_instance_klass_for_declared_method_holder(
aoqi@0 139 iter()->get_declared_method_holder());
aoqi@0 140 if (!klass->is_linked()) {
aoqi@0 141 set_trap(
aoqi@0 142 Deoptimization::make_trap_request(
aoqi@0 143 Deoptimization::Reason_uninitialized,
aoqi@0 144 Deoptimization::Action_reinterpret), bci());
aoqi@0 145 return;
aoqi@0 146 }
aoqi@0 147 }
aoqi@0 148 break;
aoqi@0 149
aoqi@0 150 case Bytecodes::_new:
aoqi@0 151 klass = iter()->get_klass(will_link)->as_instance_klass();
aoqi@0 152 assert(will_link, "typeflow responsibility");
aoqi@0 153
aoqi@0 154 // Bail out if the class is unloaded
aoqi@0 155 if (iter()->is_unresolved_klass() || !klass->is_initialized()) {
aoqi@0 156 set_trap(
aoqi@0 157 Deoptimization::make_trap_request(
aoqi@0 158 Deoptimization::Reason_uninitialized,
aoqi@0 159 Deoptimization::Action_reinterpret), bci());
aoqi@0 160 return;
aoqi@0 161 }
aoqi@0 162
aoqi@0 163 // Bail out if the class cannot be instantiated
aoqi@0 164 if (klass->is_abstract() || klass->is_interface() ||
aoqi@0 165 klass->name() == ciSymbol::java_lang_Class()) {
aoqi@0 166 set_trap(
aoqi@0 167 Deoptimization::make_trap_request(
aoqi@0 168 Deoptimization::Reason_unhandled,
aoqi@0 169 Deoptimization::Action_reinterpret), bci());
aoqi@0 170 return;
aoqi@0 171 }
aoqi@0 172 break;
aoqi@0 173 case Bytecodes::_invokedynamic:
aoqi@0 174 case Bytecodes::_invokehandle:
aoqi@0 175 if (SharkPerformanceWarnings) {
aoqi@0 176 warning("JSR292 optimization not yet implemented in Shark");
aoqi@0 177 }
aoqi@0 178 set_trap(
aoqi@0 179 Deoptimization::make_trap_request(
aoqi@0 180 Deoptimization::Reason_unhandled,
aoqi@0 181 Deoptimization::Action_make_not_compilable), bci());
aoqi@0 182 return;
aoqi@0 183 }
aoqi@0 184 }
aoqi@0 185
aoqi@0 186 // Trap if typeflow trapped (and we didn't before)
aoqi@0 187 if (ciblock()->has_trap()) {
aoqi@0 188 set_trap(
aoqi@0 189 Deoptimization::make_trap_request(
aoqi@0 190 Deoptimization::Reason_unloaded,
aoqi@0 191 Deoptimization::Action_reinterpret,
aoqi@0 192 ciblock()->trap_index()), ciblock()->trap_bci());
aoqi@0 193 return;
aoqi@0 194 }
aoqi@0 195 }
aoqi@0 196
aoqi@0 197 bool SharkTopLevelBlock::static_field_ok_in_clinit(ciField* field) {
aoqi@0 198 assert(field->is_static(), "should be");
aoqi@0 199
aoqi@0 200 // This code is lifted pretty much verbatim from C2's
aoqi@0 201 // Parse::static_field_ok_in_clinit() in parse3.cpp.
aoqi@0 202 bool access_OK = false;
aoqi@0 203 if (target()->holder()->is_subclass_of(field->holder())) {
aoqi@0 204 if (target()->is_static()) {
aoqi@0 205 if (target()->name() == ciSymbol::class_initializer_name()) {
aoqi@0 206 // It's OK to access static fields from the class initializer
aoqi@0 207 access_OK = true;
aoqi@0 208 }
aoqi@0 209 }
aoqi@0 210 else {
aoqi@0 211 if (target()->name() == ciSymbol::object_initializer_name()) {
aoqi@0 212 // It's also OK to access static fields inside a constructor,
aoqi@0 213 // because any thread calling the constructor must first have
aoqi@0 214 // synchronized on the class by executing a "new" bytecode.
aoqi@0 215 access_OK = true;
aoqi@0 216 }
aoqi@0 217 }
aoqi@0 218 }
aoqi@0 219 return access_OK;
aoqi@0 220 }
aoqi@0 221
aoqi@0 222 SharkState* SharkTopLevelBlock::entry_state() {
aoqi@0 223 if (_entry_state == NULL) {
aoqi@0 224 assert(needs_phis(), "should do");
aoqi@0 225 _entry_state = new SharkPHIState(this);
aoqi@0 226 }
aoqi@0 227 return _entry_state;
aoqi@0 228 }
aoqi@0 229
aoqi@0 230 void SharkTopLevelBlock::add_incoming(SharkState* incoming_state) {
aoqi@0 231 if (needs_phis()) {
aoqi@0 232 ((SharkPHIState *) entry_state())->add_incoming(incoming_state);
aoqi@0 233 }
aoqi@0 234 else if (_entry_state == NULL) {
aoqi@0 235 _entry_state = incoming_state;
aoqi@0 236 }
aoqi@0 237 else {
aoqi@0 238 assert(entry_state()->equal_to(incoming_state), "should be");
aoqi@0 239 }
aoqi@0 240 }
aoqi@0 241
aoqi@0 242 void SharkTopLevelBlock::enter(SharkTopLevelBlock* predecessor,
aoqi@0 243 bool is_exception) {
aoqi@0 244 // This block requires phis:
aoqi@0 245 // - if it is entered more than once
aoqi@0 246 // - if it is an exception handler, because in which
aoqi@0 247 // case we assume it's entered more than once.
aoqi@0 248 // - if the predecessor will be compiled after this
aoqi@0 249 // block, in which case we can't simple propagate
aoqi@0 250 // the state forward.
aoqi@0 251 if (!needs_phis() &&
aoqi@0 252 (entered() ||
aoqi@0 253 is_exception ||
aoqi@0 254 (predecessor && predecessor->index() >= index())))
aoqi@0 255 _needs_phis = true;
aoqi@0 256
aoqi@0 257 // Recurse into the tree
aoqi@0 258 if (!entered()) {
aoqi@0 259 _entered = true;
aoqi@0 260
aoqi@0 261 scan_for_traps();
aoqi@0 262 if (!has_trap()) {
aoqi@0 263 for (int i = 0; i < num_successors(); i++) {
aoqi@0 264 successor(i)->enter(this, false);
aoqi@0 265 }
aoqi@0 266 }
aoqi@0 267 compute_exceptions();
aoqi@0 268 for (int i = 0; i < num_exceptions(); i++) {
aoqi@0 269 SharkTopLevelBlock *handler = exception(i);
aoqi@0 270 if (handler)
aoqi@0 271 handler->enter(this, true);
aoqi@0 272 }
aoqi@0 273 }
aoqi@0 274 }
aoqi@0 275
aoqi@0 276 void SharkTopLevelBlock::initialize() {
aoqi@0 277 char name[28];
aoqi@0 278 snprintf(name, sizeof(name),
aoqi@0 279 "bci_%d%s",
aoqi@0 280 start(), is_backedge_copy() ? "_backedge_copy" : "");
aoqi@0 281 _entry_block = function()->CreateBlock(name);
aoqi@0 282 }
aoqi@0 283
aoqi@0 284 void SharkTopLevelBlock::decache_for_Java_call(ciMethod *callee) {
aoqi@0 285 SharkJavaCallDecacher(function(), bci(), callee).scan(current_state());
aoqi@0 286 for (int i = 0; i < callee->arg_size(); i++)
aoqi@0 287 xpop();
aoqi@0 288 }
aoqi@0 289
aoqi@0 290 void SharkTopLevelBlock::cache_after_Java_call(ciMethod *callee) {
aoqi@0 291 if (callee->return_type()->size()) {
aoqi@0 292 ciType *type;
aoqi@0 293 switch (callee->return_type()->basic_type()) {
aoqi@0 294 case T_BOOLEAN:
aoqi@0 295 case T_BYTE:
aoqi@0 296 case T_CHAR:
aoqi@0 297 case T_SHORT:
aoqi@0 298 type = ciType::make(T_INT);
aoqi@0 299 break;
aoqi@0 300
aoqi@0 301 default:
aoqi@0 302 type = callee->return_type();
aoqi@0 303 }
aoqi@0 304
aoqi@0 305 push(SharkValue::create_generic(type, NULL, false));
aoqi@0 306 }
aoqi@0 307 SharkJavaCallCacher(function(), callee).scan(current_state());
aoqi@0 308 }
aoqi@0 309
aoqi@0 310 void SharkTopLevelBlock::decache_for_VM_call() {
aoqi@0 311 SharkVMCallDecacher(function(), bci()).scan(current_state());
aoqi@0 312 }
aoqi@0 313
aoqi@0 314 void SharkTopLevelBlock::cache_after_VM_call() {
aoqi@0 315 SharkVMCallCacher(function()).scan(current_state());
aoqi@0 316 }
aoqi@0 317
aoqi@0 318 void SharkTopLevelBlock::decache_for_trap() {
aoqi@0 319 SharkTrapDecacher(function(), bci()).scan(current_state());
aoqi@0 320 }
aoqi@0 321
aoqi@0 322 void SharkTopLevelBlock::emit_IR() {
aoqi@0 323 builder()->SetInsertPoint(entry_block());
aoqi@0 324
aoqi@0 325 // Parse the bytecode
aoqi@0 326 parse_bytecode(start(), limit());
aoqi@0 327
aoqi@0 328 // If this block falls through to the next then it won't have been
aoqi@0 329 // terminated by a bytecode and we have to add the branch ourselves
aoqi@0 330 if (falls_through() && !has_trap())
aoqi@0 331 do_branch(ciTypeFlow::FALL_THROUGH);
aoqi@0 332 }
aoqi@0 333
aoqi@0 334 SharkTopLevelBlock* SharkTopLevelBlock::bci_successor(int bci) const {
aoqi@0 335 // XXX now with Linear Search Technology (tm)
aoqi@0 336 for (int i = 0; i < num_successors(); i++) {
aoqi@0 337 ciTypeFlow::Block *successor = ciblock()->successors()->at(i);
aoqi@0 338 if (successor->start() == bci)
aoqi@0 339 return function()->block(successor->pre_order());
aoqi@0 340 }
aoqi@0 341 ShouldNotReachHere();
aoqi@0 342 }
aoqi@0 343
aoqi@0 344 void SharkTopLevelBlock::do_zero_check(SharkValue *value) {
aoqi@0 345 if (value->is_phi() && value->as_phi()->all_incomers_zero_checked()) {
aoqi@0 346 function()->add_deferred_zero_check(this, value);
aoqi@0 347 }
aoqi@0 348 else {
aoqi@0 349 BasicBlock *continue_block = function()->CreateBlock("not_zero");
aoqi@0 350 SharkState *saved_state = current_state();
aoqi@0 351 set_current_state(saved_state->copy());
aoqi@0 352 zero_check_value(value, continue_block);
aoqi@0 353 builder()->SetInsertPoint(continue_block);
aoqi@0 354 set_current_state(saved_state);
aoqi@0 355 }
aoqi@0 356
aoqi@0 357 value->set_zero_checked(true);
aoqi@0 358 }
aoqi@0 359
aoqi@0 360 void SharkTopLevelBlock::do_deferred_zero_check(SharkValue* value,
aoqi@0 361 int bci,
aoqi@0 362 SharkState* saved_state,
aoqi@0 363 BasicBlock* continue_block) {
aoqi@0 364 if (value->as_phi()->all_incomers_zero_checked()) {
aoqi@0 365 builder()->CreateBr(continue_block);
aoqi@0 366 }
aoqi@0 367 else {
aoqi@0 368 iter()->force_bci(start());
aoqi@0 369 set_current_state(saved_state);
aoqi@0 370 zero_check_value(value, continue_block);
aoqi@0 371 }
aoqi@0 372 }
aoqi@0 373
aoqi@0 374 void SharkTopLevelBlock::zero_check_value(SharkValue* value,
aoqi@0 375 BasicBlock* continue_block) {
aoqi@0 376 BasicBlock *zero_block = builder()->CreateBlock(continue_block, "zero");
aoqi@0 377
aoqi@0 378 Value *a, *b;
aoqi@0 379 switch (value->basic_type()) {
aoqi@0 380 case T_BYTE:
aoqi@0 381 case T_CHAR:
aoqi@0 382 case T_SHORT:
aoqi@0 383 case T_INT:
aoqi@0 384 a = value->jint_value();
aoqi@0 385 b = LLVMValue::jint_constant(0);
aoqi@0 386 break;
aoqi@0 387 case T_LONG:
aoqi@0 388 a = value->jlong_value();
aoqi@0 389 b = LLVMValue::jlong_constant(0);
aoqi@0 390 break;
aoqi@0 391 case T_OBJECT:
aoqi@0 392 case T_ARRAY:
aoqi@0 393 a = value->jobject_value();
aoqi@0 394 b = LLVMValue::LLVMValue::null();
aoqi@0 395 break;
aoqi@0 396 default:
aoqi@0 397 tty->print_cr("Unhandled type %s", type2name(value->basic_type()));
aoqi@0 398 ShouldNotReachHere();
aoqi@0 399 }
aoqi@0 400
aoqi@0 401 builder()->CreateCondBr(
aoqi@0 402 builder()->CreateICmpNE(a, b), continue_block, zero_block);
aoqi@0 403
aoqi@0 404 builder()->SetInsertPoint(zero_block);
aoqi@0 405 if (value->is_jobject()) {
aoqi@0 406 call_vm(
aoqi@0 407 builder()->throw_NullPointerException(),
aoqi@0 408 builder()->CreateIntToPtr(
aoqi@0 409 LLVMValue::intptr_constant((intptr_t) __FILE__),
aoqi@0 410 PointerType::getUnqual(SharkType::jbyte_type())),
aoqi@0 411 LLVMValue::jint_constant(__LINE__),
aoqi@0 412 EX_CHECK_NONE);
aoqi@0 413 }
aoqi@0 414 else {
aoqi@0 415 call_vm(
aoqi@0 416 builder()->throw_ArithmeticException(),
aoqi@0 417 builder()->CreateIntToPtr(
aoqi@0 418 LLVMValue::intptr_constant((intptr_t) __FILE__),
aoqi@0 419 PointerType::getUnqual(SharkType::jbyte_type())),
aoqi@0 420 LLVMValue::jint_constant(__LINE__),
aoqi@0 421 EX_CHECK_NONE);
aoqi@0 422 }
aoqi@0 423
aoqi@0 424 Value *pending_exception = get_pending_exception();
aoqi@0 425 clear_pending_exception();
aoqi@0 426 handle_exception(pending_exception, EX_CHECK_FULL);
aoqi@0 427 }
aoqi@0 428
aoqi@0 429 void SharkTopLevelBlock::check_bounds(SharkValue* array, SharkValue* index) {
aoqi@0 430 BasicBlock *out_of_bounds = function()->CreateBlock("out_of_bounds");
aoqi@0 431 BasicBlock *in_bounds = function()->CreateBlock("in_bounds");
aoqi@0 432
aoqi@0 433 Value *length = builder()->CreateArrayLength(array->jarray_value());
aoqi@0 434 // we use an unsigned comparison to catch negative values
aoqi@0 435 builder()->CreateCondBr(
aoqi@0 436 builder()->CreateICmpULT(index->jint_value(), length),
aoqi@0 437 in_bounds, out_of_bounds);
aoqi@0 438
aoqi@0 439 builder()->SetInsertPoint(out_of_bounds);
aoqi@0 440 SharkState *saved_state = current_state()->copy();
aoqi@0 441
aoqi@0 442 call_vm(
aoqi@0 443 builder()->throw_ArrayIndexOutOfBoundsException(),
aoqi@0 444 builder()->CreateIntToPtr(
aoqi@0 445 LLVMValue::intptr_constant((intptr_t) __FILE__),
aoqi@0 446 PointerType::getUnqual(SharkType::jbyte_type())),
aoqi@0 447 LLVMValue::jint_constant(__LINE__),
aoqi@0 448 index->jint_value(),
aoqi@0 449 EX_CHECK_NONE);
aoqi@0 450
aoqi@0 451 Value *pending_exception = get_pending_exception();
aoqi@0 452 clear_pending_exception();
aoqi@0 453 handle_exception(pending_exception, EX_CHECK_FULL);
aoqi@0 454
aoqi@0 455 set_current_state(saved_state);
aoqi@0 456
aoqi@0 457 builder()->SetInsertPoint(in_bounds);
aoqi@0 458 }
aoqi@0 459
aoqi@0 460 void SharkTopLevelBlock::check_pending_exception(int action) {
aoqi@0 461 assert(action & EAM_CHECK, "should be");
aoqi@0 462
aoqi@0 463 BasicBlock *exception = function()->CreateBlock("exception");
aoqi@0 464 BasicBlock *no_exception = function()->CreateBlock("no_exception");
aoqi@0 465
aoqi@0 466 Value *pending_exception = get_pending_exception();
aoqi@0 467 builder()->CreateCondBr(
aoqi@0 468 builder()->CreateICmpEQ(pending_exception, LLVMValue::null()),
aoqi@0 469 no_exception, exception);
aoqi@0 470
aoqi@0 471 builder()->SetInsertPoint(exception);
aoqi@0 472 SharkState *saved_state = current_state()->copy();
aoqi@0 473 if (action & EAM_MONITOR_FUDGE) {
aoqi@0 474 // The top monitor is marked live, but the exception was thrown
aoqi@0 475 // while setting it up so we need to mark it dead before we enter
aoqi@0 476 // any exception handlers as they will not expect it to be there.
aoqi@0 477 set_num_monitors(num_monitors() - 1);
aoqi@0 478 action ^= EAM_MONITOR_FUDGE;
aoqi@0 479 }
aoqi@0 480 clear_pending_exception();
aoqi@0 481 handle_exception(pending_exception, action);
aoqi@0 482 set_current_state(saved_state);
aoqi@0 483
aoqi@0 484 builder()->SetInsertPoint(no_exception);
aoqi@0 485 }
aoqi@0 486
aoqi@0 487 void SharkTopLevelBlock::compute_exceptions() {
aoqi@0 488 ciExceptionHandlerStream str(target(), start());
aoqi@0 489
aoqi@0 490 int exc_count = str.count();
aoqi@0 491 _exc_handlers = new GrowableArray<ciExceptionHandler*>(exc_count);
aoqi@0 492 _exceptions = new GrowableArray<SharkTopLevelBlock*>(exc_count);
aoqi@0 493
aoqi@0 494 int index = 0;
aoqi@0 495 for (; !str.is_done(); str.next()) {
aoqi@0 496 ciExceptionHandler *handler = str.handler();
aoqi@0 497 if (handler->handler_bci() == -1)
aoqi@0 498 break;
aoqi@0 499 _exc_handlers->append(handler);
aoqi@0 500
aoqi@0 501 // Try and get this exception's handler from typeflow. We should
aoqi@0 502 // do it this way always, really, except that typeflow sometimes
aoqi@0 503 // doesn't record exceptions, even loaded ones, and sometimes it
aoqi@0 504 // returns them with a different handler bci. Why???
aoqi@0 505 SharkTopLevelBlock *block = NULL;
aoqi@0 506 ciInstanceKlass* klass;
aoqi@0 507 if (handler->is_catch_all()) {
aoqi@0 508 klass = java_lang_Throwable_klass();
aoqi@0 509 }
aoqi@0 510 else {
aoqi@0 511 klass = handler->catch_klass();
aoqi@0 512 }
aoqi@0 513 for (int i = 0; i < ciblock()->exceptions()->length(); i++) {
aoqi@0 514 if (klass == ciblock()->exc_klasses()->at(i)) {
aoqi@0 515 block = function()->block(ciblock()->exceptions()->at(i)->pre_order());
aoqi@0 516 if (block->start() == handler->handler_bci())
aoqi@0 517 break;
aoqi@0 518 else
aoqi@0 519 block = NULL;
aoqi@0 520 }
aoqi@0 521 }
aoqi@0 522
aoqi@0 523 // If typeflow let us down then try and figure it out ourselves
aoqi@0 524 if (block == NULL) {
aoqi@0 525 for (int i = 0; i < function()->block_count(); i++) {
aoqi@0 526 SharkTopLevelBlock *candidate = function()->block(i);
aoqi@0 527 if (candidate->start() == handler->handler_bci()) {
aoqi@0 528 if (block != NULL) {
aoqi@0 529 NOT_PRODUCT(warning("there may be trouble ahead"));
aoqi@0 530 block = NULL;
aoqi@0 531 break;
aoqi@0 532 }
aoqi@0 533 block = candidate;
aoqi@0 534 }
aoqi@0 535 }
aoqi@0 536 }
aoqi@0 537 _exceptions->append(block);
aoqi@0 538 }
aoqi@0 539 }
aoqi@0 540
aoqi@0 541 void SharkTopLevelBlock::handle_exception(Value* exception, int action) {
aoqi@0 542 if (action & EAM_HANDLE && num_exceptions() != 0) {
aoqi@0 543 // Clear the stack and push the exception onto it
aoqi@0 544 while (xstack_depth())
aoqi@0 545 pop();
aoqi@0 546 push(SharkValue::create_jobject(exception, true));
aoqi@0 547
aoqi@0 548 // Work out how many options we have to check
aoqi@0 549 bool has_catch_all = exc_handler(num_exceptions() - 1)->is_catch_all();
aoqi@0 550 int num_options = num_exceptions();
aoqi@0 551 if (has_catch_all)
aoqi@0 552 num_options--;
aoqi@0 553
aoqi@0 554 // Marshal any non-catch-all handlers
aoqi@0 555 if (num_options > 0) {
aoqi@0 556 bool all_loaded = true;
aoqi@0 557 for (int i = 0; i < num_options; i++) {
aoqi@0 558 if (!exc_handler(i)->catch_klass()->is_loaded()) {
aoqi@0 559 all_loaded = false;
aoqi@0 560 break;
aoqi@0 561 }
aoqi@0 562 }
aoqi@0 563
aoqi@0 564 if (all_loaded)
aoqi@0 565 marshal_exception_fast(num_options);
aoqi@0 566 else
aoqi@0 567 marshal_exception_slow(num_options);
aoqi@0 568 }
aoqi@0 569
aoqi@0 570 // Install the catch-all handler, if present
aoqi@0 571 if (has_catch_all) {
aoqi@0 572 SharkTopLevelBlock* handler = this->exception(num_options);
aoqi@0 573 assert(handler != NULL, "catch-all handler cannot be unloaded");
aoqi@0 574
aoqi@0 575 builder()->CreateBr(handler->entry_block());
aoqi@0 576 handler->add_incoming(current_state());
aoqi@0 577 return;
aoqi@0 578 }
aoqi@0 579 }
aoqi@0 580
aoqi@0 581 // No exception handler was found; unwind and return
aoqi@0 582 handle_return(T_VOID, exception);
aoqi@0 583 }
aoqi@0 584
aoqi@0 585 void SharkTopLevelBlock::marshal_exception_fast(int num_options) {
aoqi@0 586 Value *exception_klass = builder()->CreateValueOfStructEntry(
aoqi@0 587 xstack(0)->jobject_value(),
aoqi@0 588 in_ByteSize(oopDesc::klass_offset_in_bytes()),
aoqi@0 589 SharkType::klass_type(),
aoqi@0 590 "exception_klass");
aoqi@0 591
aoqi@0 592 for (int i = 0; i < num_options; i++) {
aoqi@0 593 Value *check_klass =
aoqi@0 594 builder()->CreateInlineMetadata(exc_handler(i)->catch_klass(), SharkType::klass_type());
aoqi@0 595
aoqi@0 596 BasicBlock *not_exact = function()->CreateBlock("not_exact");
aoqi@0 597 BasicBlock *not_subtype = function()->CreateBlock("not_subtype");
aoqi@0 598
aoqi@0 599 builder()->CreateCondBr(
aoqi@0 600 builder()->CreateICmpEQ(check_klass, exception_klass),
aoqi@0 601 handler_for_exception(i), not_exact);
aoqi@0 602
aoqi@0 603 builder()->SetInsertPoint(not_exact);
aoqi@0 604 builder()->CreateCondBr(
aoqi@0 605 builder()->CreateICmpNE(
aoqi@0 606 builder()->CreateCall2(
aoqi@0 607 builder()->is_subtype_of(), check_klass, exception_klass),
aoqi@0 608 LLVMValue::jbyte_constant(0)),
aoqi@0 609 handler_for_exception(i), not_subtype);
aoqi@0 610
aoqi@0 611 builder()->SetInsertPoint(not_subtype);
aoqi@0 612 }
aoqi@0 613 }
aoqi@0 614
aoqi@0 615 void SharkTopLevelBlock::marshal_exception_slow(int num_options) {
aoqi@0 616 int *indexes = NEW_RESOURCE_ARRAY(int, num_options);
aoqi@0 617 for (int i = 0; i < num_options; i++)
aoqi@0 618 indexes[i] = exc_handler(i)->catch_klass_index();
aoqi@0 619
aoqi@0 620 Value *index = call_vm(
aoqi@0 621 builder()->find_exception_handler(),
aoqi@0 622 builder()->CreateInlineData(
aoqi@0 623 indexes,
aoqi@0 624 num_options * sizeof(int),
aoqi@0 625 PointerType::getUnqual(SharkType::jint_type())),
aoqi@0 626 LLVMValue::jint_constant(num_options),
aoqi@0 627 EX_CHECK_NO_CATCH);
aoqi@0 628
aoqi@0 629 BasicBlock *no_handler = function()->CreateBlock("no_handler");
aoqi@0 630 SwitchInst *switchinst = builder()->CreateSwitch(
aoqi@0 631 index, no_handler, num_options);
aoqi@0 632
aoqi@0 633 for (int i = 0; i < num_options; i++) {
aoqi@0 634 switchinst->addCase(
aoqi@0 635 LLVMValue::jint_constant(i),
aoqi@0 636 handler_for_exception(i));
aoqi@0 637 }
aoqi@0 638
aoqi@0 639 builder()->SetInsertPoint(no_handler);
aoqi@0 640 }
aoqi@0 641
aoqi@0 642 BasicBlock* SharkTopLevelBlock::handler_for_exception(int index) {
aoqi@0 643 SharkTopLevelBlock *successor = this->exception(index);
aoqi@0 644 if (successor) {
aoqi@0 645 successor->add_incoming(current_state());
aoqi@0 646 return successor->entry_block();
aoqi@0 647 }
aoqi@0 648 else {
aoqi@0 649 return make_trap(
aoqi@0 650 exc_handler(index)->handler_bci(),
aoqi@0 651 Deoptimization::make_trap_request(
aoqi@0 652 Deoptimization::Reason_unhandled,
aoqi@0 653 Deoptimization::Action_reinterpret));
aoqi@0 654 }
aoqi@0 655 }
aoqi@0 656
aoqi@0 657 void SharkTopLevelBlock::maybe_add_safepoint() {
aoqi@0 658 if (current_state()->has_safepointed())
aoqi@0 659 return;
aoqi@0 660
aoqi@0 661 BasicBlock *orig_block = builder()->GetInsertBlock();
aoqi@0 662 SharkState *orig_state = current_state()->copy();
aoqi@0 663
aoqi@0 664 BasicBlock *do_safepoint = function()->CreateBlock("do_safepoint");
aoqi@0 665 BasicBlock *safepointed = function()->CreateBlock("safepointed");
aoqi@0 666
aoqi@0 667 Value *state = builder()->CreateLoad(
aoqi@0 668 builder()->CreateIntToPtr(
aoqi@0 669 LLVMValue::intptr_constant(
aoqi@0 670 (intptr_t) SafepointSynchronize::address_of_state()),
aoqi@0 671 PointerType::getUnqual(SharkType::jint_type())),
aoqi@0 672 "state");
aoqi@0 673
aoqi@0 674 builder()->CreateCondBr(
aoqi@0 675 builder()->CreateICmpEQ(
aoqi@0 676 state,
aoqi@0 677 LLVMValue::jint_constant(SafepointSynchronize::_synchronizing)),
aoqi@0 678 do_safepoint, safepointed);
aoqi@0 679
aoqi@0 680 builder()->SetInsertPoint(do_safepoint);
aoqi@0 681 call_vm(builder()->safepoint(), EX_CHECK_FULL);
aoqi@0 682 BasicBlock *safepointed_block = builder()->GetInsertBlock();
aoqi@0 683 builder()->CreateBr(safepointed);
aoqi@0 684
aoqi@0 685 builder()->SetInsertPoint(safepointed);
aoqi@0 686 current_state()->merge(orig_state, orig_block, safepointed_block);
aoqi@0 687
aoqi@0 688 current_state()->set_has_safepointed(true);
aoqi@0 689 }
aoqi@0 690
aoqi@0 691 void SharkTopLevelBlock::maybe_add_backedge_safepoint() {
aoqi@0 692 if (current_state()->has_safepointed())
aoqi@0 693 return;
aoqi@0 694
aoqi@0 695 for (int i = 0; i < num_successors(); i++) {
aoqi@0 696 if (successor(i)->can_reach(this)) {
aoqi@0 697 maybe_add_safepoint();
aoqi@0 698 break;
aoqi@0 699 }
aoqi@0 700 }
aoqi@0 701 }
aoqi@0 702
aoqi@0 703 bool SharkTopLevelBlock::can_reach(SharkTopLevelBlock* other) {
aoqi@0 704 for (int i = 0; i < function()->block_count(); i++)
aoqi@0 705 function()->block(i)->_can_reach_visited = false;
aoqi@0 706
aoqi@0 707 return can_reach_helper(other);
aoqi@0 708 }
aoqi@0 709
aoqi@0 710 bool SharkTopLevelBlock::can_reach_helper(SharkTopLevelBlock* other) {
aoqi@0 711 if (this == other)
aoqi@0 712 return true;
aoqi@0 713
aoqi@0 714 if (_can_reach_visited)
aoqi@0 715 return false;
aoqi@0 716 _can_reach_visited = true;
aoqi@0 717
aoqi@0 718 if (!has_trap()) {
aoqi@0 719 for (int i = 0; i < num_successors(); i++) {
aoqi@0 720 if (successor(i)->can_reach_helper(other))
aoqi@0 721 return true;
aoqi@0 722 }
aoqi@0 723 }
aoqi@0 724
aoqi@0 725 for (int i = 0; i < num_exceptions(); i++) {
aoqi@0 726 SharkTopLevelBlock *handler = exception(i);
aoqi@0 727 if (handler && handler->can_reach_helper(other))
aoqi@0 728 return true;
aoqi@0 729 }
aoqi@0 730
aoqi@0 731 return false;
aoqi@0 732 }
aoqi@0 733
aoqi@0 734 BasicBlock* SharkTopLevelBlock::make_trap(int trap_bci, int trap_request) {
aoqi@0 735 BasicBlock *trap_block = function()->CreateBlock("trap");
aoqi@0 736 BasicBlock *orig_block = builder()->GetInsertBlock();
aoqi@0 737 builder()->SetInsertPoint(trap_block);
aoqi@0 738
aoqi@0 739 int orig_bci = bci();
aoqi@0 740 iter()->force_bci(trap_bci);
aoqi@0 741
aoqi@0 742 do_trap(trap_request);
aoqi@0 743
aoqi@0 744 builder()->SetInsertPoint(orig_block);
aoqi@0 745 iter()->force_bci(orig_bci);
aoqi@0 746
aoqi@0 747 return trap_block;
aoqi@0 748 }
aoqi@0 749
aoqi@0 750 void SharkTopLevelBlock::do_trap(int trap_request) {
aoqi@0 751 decache_for_trap();
aoqi@0 752 builder()->CreateRet(
aoqi@0 753 builder()->CreateCall2(
aoqi@0 754 builder()->uncommon_trap(),
aoqi@0 755 thread(),
aoqi@0 756 LLVMValue::jint_constant(trap_request)));
aoqi@0 757 }
aoqi@0 758
aoqi@0 759 void SharkTopLevelBlock::call_register_finalizer(Value *receiver) {
aoqi@0 760 BasicBlock *orig_block = builder()->GetInsertBlock();
aoqi@0 761 SharkState *orig_state = current_state()->copy();
aoqi@0 762
aoqi@0 763 BasicBlock *do_call = function()->CreateBlock("has_finalizer");
aoqi@0 764 BasicBlock *done = function()->CreateBlock("done");
aoqi@0 765
aoqi@0 766 Value *klass = builder()->CreateValueOfStructEntry(
aoqi@0 767 receiver,
aoqi@0 768 in_ByteSize(oopDesc::klass_offset_in_bytes()),
aoqi@0 769 SharkType::oop_type(),
aoqi@0 770 "klass");
aoqi@0 771
aoqi@0 772 Value *access_flags = builder()->CreateValueOfStructEntry(
aoqi@0 773 klass,
aoqi@0 774 Klass::access_flags_offset(),
aoqi@0 775 SharkType::jint_type(),
aoqi@0 776 "access_flags");
aoqi@0 777
aoqi@0 778 builder()->CreateCondBr(
aoqi@0 779 builder()->CreateICmpNE(
aoqi@0 780 builder()->CreateAnd(
aoqi@0 781 access_flags,
aoqi@0 782 LLVMValue::jint_constant(JVM_ACC_HAS_FINALIZER)),
aoqi@0 783 LLVMValue::jint_constant(0)),
aoqi@0 784 do_call, done);
aoqi@0 785
aoqi@0 786 builder()->SetInsertPoint(do_call);
aoqi@0 787 call_vm(builder()->register_finalizer(), receiver, EX_CHECK_FULL);
aoqi@0 788 BasicBlock *branch_block = builder()->GetInsertBlock();
aoqi@0 789 builder()->CreateBr(done);
aoqi@0 790
aoqi@0 791 builder()->SetInsertPoint(done);
aoqi@0 792 current_state()->merge(orig_state, orig_block, branch_block);
aoqi@0 793 }
aoqi@0 794
aoqi@0 795 void SharkTopLevelBlock::handle_return(BasicType type, Value* exception) {
aoqi@0 796 assert (exception == NULL || type == T_VOID, "exception OR result, please");
aoqi@0 797
aoqi@0 798 if (num_monitors()) {
aoqi@0 799 // Protect our exception across possible monitor release decaches
aoqi@0 800 if (exception)
aoqi@0 801 set_oop_tmp(exception);
aoqi@0 802
aoqi@0 803 // We don't need to check for exceptions thrown here. If
aoqi@0 804 // we're returning a value then we just carry on as normal:
aoqi@0 805 // the caller will see the pending exception and handle it.
aoqi@0 806 // If we're returning with an exception then that exception
aoqi@0 807 // takes priority and the release_lock one will be ignored.
aoqi@0 808 while (num_monitors())
aoqi@0 809 release_lock(EX_CHECK_NONE);
aoqi@0 810
aoqi@0 811 // Reload the exception we're throwing
aoqi@0 812 if (exception)
aoqi@0 813 exception = get_oop_tmp();
aoqi@0 814 }
aoqi@0 815
aoqi@0 816 if (exception) {
aoqi@0 817 builder()->CreateStore(exception, pending_exception_address());
aoqi@0 818 }
aoqi@0 819
aoqi@0 820 Value *result_addr = stack()->CreatePopFrame(type2size[type]);
aoqi@0 821 if (type != T_VOID) {
aoqi@0 822 builder()->CreateStore(
aoqi@0 823 pop_result(type)->generic_value(),
aoqi@0 824 builder()->CreateIntToPtr(
aoqi@0 825 result_addr,
aoqi@0 826 PointerType::getUnqual(SharkType::to_stackType(type))));
aoqi@0 827 }
aoqi@0 828
aoqi@0 829 builder()->CreateRet(LLVMValue::jint_constant(0));
aoqi@0 830 }
aoqi@0 831
aoqi@0 832 void SharkTopLevelBlock::do_arraylength() {
aoqi@0 833 SharkValue *array = pop();
aoqi@0 834 check_null(array);
aoqi@0 835 Value *length = builder()->CreateArrayLength(array->jarray_value());
aoqi@0 836 push(SharkValue::create_jint(length, false));
aoqi@0 837 }
aoqi@0 838
aoqi@0 839 void SharkTopLevelBlock::do_aload(BasicType basic_type) {
aoqi@0 840 SharkValue *index = pop();
aoqi@0 841 SharkValue *array = pop();
aoqi@0 842
aoqi@0 843 check_null(array);
aoqi@0 844 check_bounds(array, index);
aoqi@0 845
aoqi@0 846 Value *value = builder()->CreateLoad(
aoqi@0 847 builder()->CreateArrayAddress(
aoqi@0 848 array->jarray_value(), basic_type, index->jint_value()));
aoqi@0 849
aoqi@0 850 Type *stack_type = SharkType::to_stackType(basic_type);
aoqi@0 851 if (value->getType() != stack_type)
aoqi@0 852 value = builder()->CreateIntCast(value, stack_type, basic_type != T_CHAR);
aoqi@0 853
aoqi@0 854 switch (basic_type) {
aoqi@0 855 case T_BYTE:
aoqi@0 856 case T_CHAR:
aoqi@0 857 case T_SHORT:
aoqi@0 858 case T_INT:
aoqi@0 859 push(SharkValue::create_jint(value, false));
aoqi@0 860 break;
aoqi@0 861
aoqi@0 862 case T_LONG:
aoqi@0 863 push(SharkValue::create_jlong(value, false));
aoqi@0 864 break;
aoqi@0 865
aoqi@0 866 case T_FLOAT:
aoqi@0 867 push(SharkValue::create_jfloat(value));
aoqi@0 868 break;
aoqi@0 869
aoqi@0 870 case T_DOUBLE:
aoqi@0 871 push(SharkValue::create_jdouble(value));
aoqi@0 872 break;
aoqi@0 873
aoqi@0 874 case T_OBJECT:
aoqi@0 875 // You might expect that array->type()->is_array_klass() would
aoqi@0 876 // always be true, but it isn't. If ciTypeFlow detects that a
aoqi@0 877 // value is always null then that value becomes an untyped null
aoqi@0 878 // object. Shark doesn't presently support this, so a generic
aoqi@0 879 // T_OBJECT is created. In this case we guess the type using
aoqi@0 880 // the BasicType we were supplied. In reality the generated
aoqi@0 881 // code will never be used, as the null value will be caught
aoqi@0 882 // by the above null pointer check.
aoqi@0 883 // http://icedtea.classpath.org/bugzilla/show_bug.cgi?id=324
aoqi@0 884 push(
aoqi@0 885 SharkValue::create_generic(
aoqi@0 886 array->type()->is_array_klass() ?
aoqi@0 887 ((ciArrayKlass *) array->type())->element_type() :
aoqi@0 888 ciType::make(basic_type),
aoqi@0 889 value, false));
aoqi@0 890 break;
aoqi@0 891
aoqi@0 892 default:
aoqi@0 893 tty->print_cr("Unhandled type %s", type2name(basic_type));
aoqi@0 894 ShouldNotReachHere();
aoqi@0 895 }
aoqi@0 896 }
aoqi@0 897
aoqi@0 898 void SharkTopLevelBlock::do_astore(BasicType basic_type) {
aoqi@0 899 SharkValue *svalue = pop();
aoqi@0 900 SharkValue *index = pop();
aoqi@0 901 SharkValue *array = pop();
aoqi@0 902
aoqi@0 903 check_null(array);
aoqi@0 904 check_bounds(array, index);
aoqi@0 905
aoqi@0 906 Value *value;
aoqi@0 907 switch (basic_type) {
aoqi@0 908 case T_BYTE:
aoqi@0 909 case T_CHAR:
aoqi@0 910 case T_SHORT:
aoqi@0 911 case T_INT:
aoqi@0 912 value = svalue->jint_value();
aoqi@0 913 break;
aoqi@0 914
aoqi@0 915 case T_LONG:
aoqi@0 916 value = svalue->jlong_value();
aoqi@0 917 break;
aoqi@0 918
aoqi@0 919 case T_FLOAT:
aoqi@0 920 value = svalue->jfloat_value();
aoqi@0 921 break;
aoqi@0 922
aoqi@0 923 case T_DOUBLE:
aoqi@0 924 value = svalue->jdouble_value();
aoqi@0 925 break;
aoqi@0 926
aoqi@0 927 case T_OBJECT:
aoqi@0 928 value = svalue->jobject_value();
aoqi@0 929 // XXX assignability check
aoqi@0 930 break;
aoqi@0 931
aoqi@0 932 default:
aoqi@0 933 tty->print_cr("Unhandled type %s", type2name(basic_type));
aoqi@0 934 ShouldNotReachHere();
aoqi@0 935 }
aoqi@0 936
aoqi@0 937 Type *array_type = SharkType::to_arrayType(basic_type);
aoqi@0 938 if (value->getType() != array_type)
aoqi@0 939 value = builder()->CreateIntCast(value, array_type, basic_type != T_CHAR);
aoqi@0 940
aoqi@0 941 Value *addr = builder()->CreateArrayAddress(
aoqi@0 942 array->jarray_value(), basic_type, index->jint_value(), "addr");
aoqi@0 943
aoqi@0 944 builder()->CreateStore(value, addr);
aoqi@0 945
aoqi@0 946 if (basic_type == T_OBJECT) // XXX or T_ARRAY?
aoqi@0 947 builder()->CreateUpdateBarrierSet(oopDesc::bs(), addr);
aoqi@0 948 }
aoqi@0 949
aoqi@0 950 void SharkTopLevelBlock::do_return(BasicType type) {
aoqi@0 951 if (target()->intrinsic_id() == vmIntrinsics::_Object_init)
aoqi@0 952 call_register_finalizer(local(0)->jobject_value());
aoqi@0 953 maybe_add_safepoint();
aoqi@0 954 handle_return(type, NULL);
aoqi@0 955 }
aoqi@0 956
aoqi@0 957 void SharkTopLevelBlock::do_athrow() {
aoqi@0 958 SharkValue *exception = pop();
aoqi@0 959 check_null(exception);
aoqi@0 960 handle_exception(exception->jobject_value(), EX_CHECK_FULL);
aoqi@0 961 }
aoqi@0 962
aoqi@0 963 void SharkTopLevelBlock::do_goto() {
aoqi@0 964 do_branch(ciTypeFlow::GOTO_TARGET);
aoqi@0 965 }
aoqi@0 966
aoqi@0 967 void SharkTopLevelBlock::do_jsr() {
aoqi@0 968 push(SharkValue::address_constant(iter()->next_bci()));
aoqi@0 969 do_branch(ciTypeFlow::GOTO_TARGET);
aoqi@0 970 }
aoqi@0 971
aoqi@0 972 void SharkTopLevelBlock::do_ret() {
aoqi@0 973 assert(local(iter()->get_index())->address_value() ==
aoqi@0 974 successor(ciTypeFlow::GOTO_TARGET)->start(), "should be");
aoqi@0 975 do_branch(ciTypeFlow::GOTO_TARGET);
aoqi@0 976 }
aoqi@0 977
aoqi@0 978 // All propagation of state from one block to the next (via
aoqi@0 979 // dest->add_incoming) is handled by these methods:
aoqi@0 980 // do_branch
aoqi@0 981 // do_if_helper
aoqi@0 982 // do_switch
aoqi@0 983 // handle_exception
aoqi@0 984
aoqi@0 985 void SharkTopLevelBlock::do_branch(int successor_index) {
aoqi@0 986 SharkTopLevelBlock *dest = successor(successor_index);
aoqi@0 987 builder()->CreateBr(dest->entry_block());
aoqi@0 988 dest->add_incoming(current_state());
aoqi@0 989 }
aoqi@0 990
aoqi@0 991 void SharkTopLevelBlock::do_if(ICmpInst::Predicate p,
aoqi@0 992 SharkValue* b,
aoqi@0 993 SharkValue* a) {
aoqi@0 994 Value *llvm_a, *llvm_b;
aoqi@0 995 if (a->is_jobject()) {
aoqi@0 996 llvm_a = a->intptr_value(builder());
aoqi@0 997 llvm_b = b->intptr_value(builder());
aoqi@0 998 }
aoqi@0 999 else {
aoqi@0 1000 llvm_a = a->jint_value();
aoqi@0 1001 llvm_b = b->jint_value();
aoqi@0 1002 }
aoqi@0 1003 do_if_helper(p, llvm_b, llvm_a, current_state(), current_state());
aoqi@0 1004 }
aoqi@0 1005
aoqi@0 1006 void SharkTopLevelBlock::do_if_helper(ICmpInst::Predicate p,
aoqi@0 1007 Value* b,
aoqi@0 1008 Value* a,
aoqi@0 1009 SharkState* if_taken_state,
aoqi@0 1010 SharkState* not_taken_state) {
aoqi@0 1011 SharkTopLevelBlock *if_taken = successor(ciTypeFlow::IF_TAKEN);
aoqi@0 1012 SharkTopLevelBlock *not_taken = successor(ciTypeFlow::IF_NOT_TAKEN);
aoqi@0 1013
aoqi@0 1014 builder()->CreateCondBr(
aoqi@0 1015 builder()->CreateICmp(p, a, b),
aoqi@0 1016 if_taken->entry_block(), not_taken->entry_block());
aoqi@0 1017
aoqi@0 1018 if_taken->add_incoming(if_taken_state);
aoqi@0 1019 not_taken->add_incoming(not_taken_state);
aoqi@0 1020 }
aoqi@0 1021
aoqi@0 1022 void SharkTopLevelBlock::do_switch() {
aoqi@0 1023 int len = switch_table_length();
aoqi@0 1024
aoqi@0 1025 SharkTopLevelBlock *dest_block = successor(ciTypeFlow::SWITCH_DEFAULT);
aoqi@0 1026 SwitchInst *switchinst = builder()->CreateSwitch(
aoqi@0 1027 pop()->jint_value(), dest_block->entry_block(), len);
aoqi@0 1028 dest_block->add_incoming(current_state());
aoqi@0 1029
aoqi@0 1030 for (int i = 0; i < len; i++) {
aoqi@0 1031 int dest_bci = switch_dest(i);
aoqi@0 1032 if (dest_bci != switch_default_dest()) {
aoqi@0 1033 dest_block = bci_successor(dest_bci);
aoqi@0 1034 switchinst->addCase(
aoqi@0 1035 LLVMValue::jint_constant(switch_key(i)),
aoqi@0 1036 dest_block->entry_block());
aoqi@0 1037 dest_block->add_incoming(current_state());
aoqi@0 1038 }
aoqi@0 1039 }
aoqi@0 1040 }
aoqi@0 1041
aoqi@0 1042 ciMethod* SharkTopLevelBlock::improve_virtual_call(ciMethod* caller,
aoqi@0 1043 ciInstanceKlass* klass,
aoqi@0 1044 ciMethod* dest_method,
aoqi@0 1045 ciType* receiver_type) {
aoqi@0 1046 // If the method is obviously final then we are already done
aoqi@0 1047 if (dest_method->can_be_statically_bound())
aoqi@0 1048 return dest_method;
aoqi@0 1049
aoqi@0 1050 // Array methods are all inherited from Object and are monomorphic
aoqi@0 1051 if (receiver_type->is_array_klass() &&
aoqi@0 1052 dest_method->holder() == java_lang_Object_klass())
aoqi@0 1053 return dest_method;
aoqi@0 1054
aoqi@0 1055 // This code can replace a virtual call with a direct call if this
aoqi@0 1056 // class is the only one in the entire set of loaded classes that
aoqi@0 1057 // implements this method. This makes the compiled code dependent
aoqi@0 1058 // on other classes that implement the method not being loaded, a
aoqi@0 1059 // condition which is enforced by the dependency tracker. If the
aoqi@0 1060 // dependency tracker determines a method has become invalid it
aoqi@0 1061 // will mark it for recompilation, causing running copies to be
aoqi@0 1062 // deoptimized. Shark currently can't deoptimize arbitrarily like
aoqi@0 1063 // that, so this optimization cannot be used.
aoqi@0 1064 // http://icedtea.classpath.org/bugzilla/show_bug.cgi?id=481
aoqi@0 1065
aoqi@0 1066 // All other interesting cases are instance classes
aoqi@0 1067 if (!receiver_type->is_instance_klass())
aoqi@0 1068 return NULL;
aoqi@0 1069
aoqi@0 1070 // Attempt to improve the receiver
aoqi@0 1071 ciInstanceKlass* actual_receiver = klass;
aoqi@0 1072 ciInstanceKlass *improved_receiver = receiver_type->as_instance_klass();
aoqi@0 1073 if (improved_receiver->is_loaded() &&
aoqi@0 1074 improved_receiver->is_initialized() &&
aoqi@0 1075 !improved_receiver->is_interface() &&
aoqi@0 1076 improved_receiver->is_subtype_of(actual_receiver)) {
aoqi@0 1077 actual_receiver = improved_receiver;
aoqi@0 1078 }
aoqi@0 1079
aoqi@0 1080 // Attempt to find a monomorphic target for this call using
aoqi@0 1081 // class heirachy analysis.
aoqi@0 1082 ciInstanceKlass *calling_klass = caller->holder();
aoqi@0 1083 ciMethod* monomorphic_target =
aoqi@0 1084 dest_method->find_monomorphic_target(calling_klass, klass, actual_receiver);
aoqi@0 1085 if (monomorphic_target != NULL) {
aoqi@0 1086 assert(!monomorphic_target->is_abstract(), "shouldn't be");
aoqi@0 1087
aoqi@0 1088 function()->dependencies()->assert_unique_concrete_method(actual_receiver, monomorphic_target);
aoqi@0 1089
aoqi@0 1090 // Opto has a bunch of type checking here that I don't
aoqi@0 1091 // understand. It's to inhibit casting in one direction,
aoqi@0 1092 // possibly because objects in Opto can have inexact
aoqi@0 1093 // types, but I can't even tell which direction it
aoqi@0 1094 // doesn't like. For now I'm going to block *any* cast.
aoqi@0 1095 if (monomorphic_target != dest_method) {
aoqi@0 1096 if (SharkPerformanceWarnings) {
aoqi@0 1097 warning("found monomorphic target, but inhibited cast:");
aoqi@0 1098 tty->print(" dest_method = ");
aoqi@0 1099 dest_method->print_short_name(tty);
aoqi@0 1100 tty->cr();
aoqi@0 1101 tty->print(" monomorphic_target = ");
aoqi@0 1102 monomorphic_target->print_short_name(tty);
aoqi@0 1103 tty->cr();
aoqi@0 1104 }
aoqi@0 1105 monomorphic_target = NULL;
aoqi@0 1106 }
aoqi@0 1107 }
aoqi@0 1108
aoqi@0 1109 // Replace the virtual call with a direct one. This makes
aoqi@0 1110 // us dependent on that target method not getting overridden
aoqi@0 1111 // by dynamic class loading.
aoqi@0 1112 if (monomorphic_target != NULL) {
aoqi@0 1113 dependencies()->assert_unique_concrete_method(
aoqi@0 1114 actual_receiver, monomorphic_target);
aoqi@0 1115 return monomorphic_target;
aoqi@0 1116 }
aoqi@0 1117
aoqi@0 1118 // Because Opto distinguishes exact types from inexact ones
aoqi@0 1119 // it can perform a further optimization to replace calls
aoqi@0 1120 // with non-monomorphic targets if the receiver has an exact
aoqi@0 1121 // type. We don't mark types this way, so we can't do this.
aoqi@0 1122
aoqi@0 1123
aoqi@0 1124 return NULL;
aoqi@0 1125 }
aoqi@0 1126
aoqi@0 1127 Value *SharkTopLevelBlock::get_direct_callee(ciMethod* method) {
aoqi@0 1128 return builder()->CreateBitCast(
aoqi@0 1129 builder()->CreateInlineMetadata(method, SharkType::Method_type()),
aoqi@0 1130 SharkType::Method_type(),
aoqi@0 1131 "callee");
aoqi@0 1132 }
aoqi@0 1133
aoqi@0 1134 Value *SharkTopLevelBlock::get_virtual_callee(SharkValue* receiver,
aoqi@0 1135 int vtable_index) {
aoqi@0 1136 Value *klass = builder()->CreateValueOfStructEntry(
aoqi@0 1137 receiver->jobject_value(),
aoqi@0 1138 in_ByteSize(oopDesc::klass_offset_in_bytes()),
aoqi@0 1139 SharkType::oop_type(),
aoqi@0 1140 "klass");
aoqi@0 1141
aoqi@0 1142 return builder()->CreateLoad(
aoqi@0 1143 builder()->CreateArrayAddress(
aoqi@0 1144 klass,
aoqi@0 1145 SharkType::Method_type(),
aoqi@0 1146 vtableEntry::size() * wordSize,
aoqi@0 1147 in_ByteSize(InstanceKlass::vtable_start_offset() * wordSize),
aoqi@0 1148 LLVMValue::intptr_constant(vtable_index)),
aoqi@0 1149 "callee");
aoqi@0 1150 }
aoqi@0 1151
aoqi@0 1152 Value* SharkTopLevelBlock::get_interface_callee(SharkValue *receiver,
aoqi@0 1153 ciMethod* method) {
aoqi@0 1154 BasicBlock *loop = function()->CreateBlock("loop");
aoqi@0 1155 BasicBlock *got_null = function()->CreateBlock("got_null");
aoqi@0 1156 BasicBlock *not_null = function()->CreateBlock("not_null");
aoqi@0 1157 BasicBlock *next = function()->CreateBlock("next");
aoqi@0 1158 BasicBlock *got_entry = function()->CreateBlock("got_entry");
aoqi@0 1159
aoqi@0 1160 // Locate the receiver's itable
aoqi@0 1161 Value *object_klass = builder()->CreateValueOfStructEntry(
aoqi@0 1162 receiver->jobject_value(), in_ByteSize(oopDesc::klass_offset_in_bytes()),
aoqi@0 1163 SharkType::klass_type(),
aoqi@0 1164 "object_klass");
aoqi@0 1165
aoqi@0 1166 Value *vtable_start = builder()->CreateAdd(
aoqi@0 1167 builder()->CreatePtrToInt(object_klass, SharkType::intptr_type()),
aoqi@0 1168 LLVMValue::intptr_constant(
aoqi@0 1169 InstanceKlass::vtable_start_offset() * HeapWordSize),
aoqi@0 1170 "vtable_start");
aoqi@0 1171
aoqi@0 1172 Value *vtable_length = builder()->CreateValueOfStructEntry(
aoqi@0 1173 object_klass,
aoqi@0 1174 in_ByteSize(InstanceKlass::vtable_length_offset() * HeapWordSize),
aoqi@0 1175 SharkType::jint_type(),
aoqi@0 1176 "vtable_length");
aoqi@0 1177 vtable_length =
aoqi@0 1178 builder()->CreateIntCast(vtable_length, SharkType::intptr_type(), false);
aoqi@0 1179
aoqi@0 1180 bool needs_aligning = HeapWordsPerLong > 1;
aoqi@0 1181 Value *itable_start = builder()->CreateAdd(
aoqi@0 1182 vtable_start,
aoqi@0 1183 builder()->CreateShl(
aoqi@0 1184 vtable_length,
aoqi@0 1185 LLVMValue::intptr_constant(exact_log2(vtableEntry::size() * wordSize))),
aoqi@0 1186 needs_aligning ? "" : "itable_start");
aoqi@0 1187 if (needs_aligning) {
aoqi@0 1188 itable_start = builder()->CreateAnd(
aoqi@0 1189 builder()->CreateAdd(
aoqi@0 1190 itable_start, LLVMValue::intptr_constant(BytesPerLong - 1)),
aoqi@0 1191 LLVMValue::intptr_constant(~(BytesPerLong - 1)),
aoqi@0 1192 "itable_start");
aoqi@0 1193 }
aoqi@0 1194
aoqi@0 1195 // Locate this interface's entry in the table
aoqi@0 1196 Value *iklass = builder()->CreateInlineMetadata(method->holder(), SharkType::klass_type());
aoqi@0 1197 BasicBlock *loop_entry = builder()->GetInsertBlock();
aoqi@0 1198 builder()->CreateBr(loop);
aoqi@0 1199 builder()->SetInsertPoint(loop);
aoqi@0 1200 PHINode *itable_entry_addr = builder()->CreatePHI(
aoqi@0 1201 SharkType::intptr_type(), 0, "itable_entry_addr");
aoqi@0 1202 itable_entry_addr->addIncoming(itable_start, loop_entry);
aoqi@0 1203
aoqi@0 1204 Value *itable_entry = builder()->CreateIntToPtr(
aoqi@0 1205 itable_entry_addr, SharkType::itableOffsetEntry_type(), "itable_entry");
aoqi@0 1206
aoqi@0 1207 Value *itable_iklass = builder()->CreateValueOfStructEntry(
aoqi@0 1208 itable_entry,
aoqi@0 1209 in_ByteSize(itableOffsetEntry::interface_offset_in_bytes()),
aoqi@0 1210 SharkType::klass_type(),
aoqi@0 1211 "itable_iklass");
aoqi@0 1212
aoqi@0 1213 builder()->CreateCondBr(
aoqi@0 1214 builder()->CreateICmpEQ(itable_iklass, LLVMValue::nullKlass()),
aoqi@0 1215 got_null, not_null);
aoqi@0 1216
aoqi@0 1217 // A null entry means that the class doesn't implement the
aoqi@0 1218 // interface, and wasn't the same as the class checked when
aoqi@0 1219 // the interface was resolved.
aoqi@0 1220 builder()->SetInsertPoint(got_null);
aoqi@0 1221 builder()->CreateUnimplemented(__FILE__, __LINE__);
aoqi@0 1222 builder()->CreateUnreachable();
aoqi@0 1223
aoqi@0 1224 builder()->SetInsertPoint(not_null);
aoqi@0 1225 builder()->CreateCondBr(
aoqi@0 1226 builder()->CreateICmpEQ(itable_iklass, iklass),
aoqi@0 1227 got_entry, next);
aoqi@0 1228
aoqi@0 1229 builder()->SetInsertPoint(next);
aoqi@0 1230 Value *next_entry = builder()->CreateAdd(
aoqi@0 1231 itable_entry_addr,
aoqi@0 1232 LLVMValue::intptr_constant(itableOffsetEntry::size() * wordSize));
aoqi@0 1233 builder()->CreateBr(loop);
aoqi@0 1234 itable_entry_addr->addIncoming(next_entry, next);
aoqi@0 1235
aoqi@0 1236 // Locate the method pointer
aoqi@0 1237 builder()->SetInsertPoint(got_entry);
aoqi@0 1238 Value *offset = builder()->CreateValueOfStructEntry(
aoqi@0 1239 itable_entry,
aoqi@0 1240 in_ByteSize(itableOffsetEntry::offset_offset_in_bytes()),
aoqi@0 1241 SharkType::jint_type(),
aoqi@0 1242 "offset");
aoqi@0 1243 offset =
aoqi@0 1244 builder()->CreateIntCast(offset, SharkType::intptr_type(), false);
aoqi@0 1245
aoqi@0 1246 return builder()->CreateLoad(
aoqi@0 1247 builder()->CreateIntToPtr(
aoqi@0 1248 builder()->CreateAdd(
aoqi@0 1249 builder()->CreateAdd(
aoqi@0 1250 builder()->CreateAdd(
aoqi@0 1251 builder()->CreatePtrToInt(
aoqi@0 1252 object_klass, SharkType::intptr_type()),
aoqi@0 1253 offset),
aoqi@0 1254 LLVMValue::intptr_constant(
aoqi@0 1255 method->itable_index() * itableMethodEntry::size() * wordSize)),
aoqi@0 1256 LLVMValue::intptr_constant(
aoqi@0 1257 itableMethodEntry::method_offset_in_bytes())),
aoqi@0 1258 PointerType::getUnqual(SharkType::Method_type())),
aoqi@0 1259 "callee");
aoqi@0 1260 }
aoqi@0 1261
aoqi@0 1262 void SharkTopLevelBlock::do_call() {
aoqi@0 1263 // Set frequently used booleans
aoqi@0 1264 bool is_static = bc() == Bytecodes::_invokestatic;
aoqi@0 1265 bool is_virtual = bc() == Bytecodes::_invokevirtual;
aoqi@0 1266 bool is_interface = bc() == Bytecodes::_invokeinterface;
aoqi@0 1267
aoqi@0 1268 // Find the method being called
aoqi@0 1269 bool will_link;
aoqi@0 1270 ciSignature* sig;
aoqi@0 1271 ciMethod *dest_method = iter()->get_method(will_link, &sig);
aoqi@0 1272
aoqi@0 1273 assert(will_link, "typeflow responsibility");
aoqi@0 1274 assert(dest_method->is_static() == is_static, "must match bc");
aoqi@0 1275
aoqi@0 1276 // Find the class of the method being called. Note
aoqi@0 1277 // that the superclass check in the second assertion
aoqi@0 1278 // is to cope with a hole in the spec that allows for
aoqi@0 1279 // invokeinterface instructions where the resolved
aoqi@0 1280 // method is a virtual method in java.lang.Object.
aoqi@0 1281 // javac doesn't generate code like that, but there's
aoqi@0 1282 // no reason a compliant Java compiler might not.
aoqi@0 1283 ciInstanceKlass *holder_klass = dest_method->holder();
aoqi@0 1284 assert(holder_klass->is_loaded(), "scan_for_traps responsibility");
aoqi@0 1285 assert(holder_klass->is_interface() ||
aoqi@0 1286 holder_klass->super() == NULL ||
aoqi@0 1287 !is_interface, "must match bc");
aoqi@0 1288
aoqi@0 1289 bool is_forced_virtual = is_interface && holder_klass == java_lang_Object_klass();
aoqi@0 1290
aoqi@0 1291 ciKlass *holder = iter()->get_declared_method_holder();
aoqi@0 1292 ciInstanceKlass *klass =
aoqi@0 1293 ciEnv::get_instance_klass_for_declared_method_holder(holder);
aoqi@0 1294
aoqi@0 1295 if (is_forced_virtual) {
aoqi@0 1296 klass = java_lang_Object_klass();
aoqi@0 1297 }
aoqi@0 1298
aoqi@0 1299 // Find the receiver in the stack. We do this before
aoqi@0 1300 // trying to inline because the inliner can only use
aoqi@0 1301 // zero-checked values, not being able to perform the
aoqi@0 1302 // check itself.
aoqi@0 1303 SharkValue *receiver = NULL;
aoqi@0 1304 if (!is_static) {
aoqi@0 1305 receiver = xstack(dest_method->arg_size() - 1);
aoqi@0 1306 check_null(receiver);
aoqi@0 1307 }
aoqi@0 1308
aoqi@0 1309 // Try to improve non-direct calls
aoqi@0 1310 bool call_is_virtual = is_virtual || is_interface;
aoqi@0 1311 ciMethod *call_method = dest_method;
aoqi@0 1312 if (call_is_virtual) {
aoqi@0 1313 ciMethod *optimized_method = improve_virtual_call(
aoqi@0 1314 target(), klass, dest_method, receiver->type());
aoqi@0 1315 if (optimized_method) {
aoqi@0 1316 call_method = optimized_method;
aoqi@0 1317 call_is_virtual = false;
aoqi@0 1318 }
aoqi@0 1319 }
aoqi@0 1320
aoqi@0 1321 // Try to inline the call
aoqi@0 1322 if (!call_is_virtual) {
aoqi@0 1323 if (SharkInliner::attempt_inline(call_method, current_state())) {
aoqi@0 1324 return;
aoqi@0 1325 }
aoqi@0 1326 }
aoqi@0 1327
aoqi@0 1328 // Find the method we are calling
aoqi@0 1329 Value *callee;
aoqi@0 1330 if (call_is_virtual) {
aoqi@0 1331 if (is_virtual || is_forced_virtual) {
aoqi@0 1332 assert(klass->is_linked(), "scan_for_traps responsibility");
aoqi@0 1333 int vtable_index = call_method->resolve_vtable_index(
aoqi@0 1334 target()->holder(), klass);
aoqi@0 1335 assert(vtable_index >= 0, "should be");
aoqi@0 1336 callee = get_virtual_callee(receiver, vtable_index);
aoqi@0 1337 }
aoqi@0 1338 else {
aoqi@0 1339 assert(is_interface, "should be");
aoqi@0 1340 callee = get_interface_callee(receiver, call_method);
aoqi@0 1341 }
aoqi@0 1342 }
aoqi@0 1343 else {
aoqi@0 1344 callee = get_direct_callee(call_method);
aoqi@0 1345 }
aoqi@0 1346
aoqi@0 1347 // Load the SharkEntry from the callee
aoqi@0 1348 Value *base_pc = builder()->CreateValueOfStructEntry(
aoqi@0 1349 callee, Method::from_interpreted_offset(),
aoqi@0 1350 SharkType::intptr_type(),
aoqi@0 1351 "base_pc");
aoqi@0 1352
aoqi@0 1353 // Load the entry point from the SharkEntry
aoqi@0 1354 Value *entry_point = builder()->CreateLoad(
aoqi@0 1355 builder()->CreateIntToPtr(
aoqi@0 1356 builder()->CreateAdd(
aoqi@0 1357 base_pc,
aoqi@0 1358 LLVMValue::intptr_constant(in_bytes(ZeroEntry::entry_point_offset()))),
aoqi@0 1359 PointerType::getUnqual(
aoqi@0 1360 PointerType::getUnqual(SharkType::entry_point_type()))),
aoqi@0 1361 "entry_point");
aoqi@0 1362
aoqi@0 1363 // Make the call
aoqi@0 1364 decache_for_Java_call(call_method);
aoqi@0 1365 Value *deoptimized_frames = builder()->CreateCall3(
aoqi@0 1366 entry_point, callee, base_pc, thread());
aoqi@0 1367
aoqi@0 1368 // If the callee got deoptimized then reexecute in the interpreter
aoqi@0 1369 BasicBlock *reexecute = function()->CreateBlock("reexecute");
aoqi@0 1370 BasicBlock *call_completed = function()->CreateBlock("call_completed");
aoqi@0 1371 builder()->CreateCondBr(
aoqi@0 1372 builder()->CreateICmpNE(deoptimized_frames, LLVMValue::jint_constant(0)),
aoqi@0 1373 reexecute, call_completed);
aoqi@0 1374
aoqi@0 1375 builder()->SetInsertPoint(reexecute);
aoqi@0 1376 builder()->CreateCall2(
aoqi@0 1377 builder()->deoptimized_entry_point(),
aoqi@0 1378 builder()->CreateSub(deoptimized_frames, LLVMValue::jint_constant(1)),
aoqi@0 1379 thread());
aoqi@0 1380 builder()->CreateBr(call_completed);
aoqi@0 1381
aoqi@0 1382 // Cache after the call
aoqi@0 1383 builder()->SetInsertPoint(call_completed);
aoqi@0 1384 cache_after_Java_call(call_method);
aoqi@0 1385
aoqi@0 1386 // Check for pending exceptions
aoqi@0 1387 check_pending_exception(EX_CHECK_FULL);
aoqi@0 1388
aoqi@0 1389 // Mark that a safepoint check has occurred
aoqi@0 1390 current_state()->set_has_safepointed(true);
aoqi@0 1391 }
aoqi@0 1392
aoqi@0 1393 bool SharkTopLevelBlock::static_subtype_check(ciKlass* check_klass,
aoqi@0 1394 ciKlass* object_klass) {
aoqi@0 1395 // If the class we're checking against is java.lang.Object
aoqi@0 1396 // then this is a no brainer. Apparently this can happen
aoqi@0 1397 // in reflective code...
aoqi@0 1398 if (check_klass == java_lang_Object_klass())
aoqi@0 1399 return true;
aoqi@0 1400
aoqi@0 1401 // Perform a subtype check. NB in opto's code for this
aoqi@0 1402 // (GraphKit::static_subtype_check) it says that static
aoqi@0 1403 // interface types cannot be trusted, and if opto can't
aoqi@0 1404 // trust them then I assume we can't either.
aoqi@0 1405 if (object_klass->is_loaded() && !object_klass->is_interface()) {
aoqi@0 1406 if (object_klass == check_klass)
aoqi@0 1407 return true;
aoqi@0 1408
aoqi@0 1409 if (check_klass->is_loaded() && object_klass->is_subtype_of(check_klass))
aoqi@0 1410 return true;
aoqi@0 1411 }
aoqi@0 1412
aoqi@0 1413 return false;
aoqi@0 1414 }
aoqi@0 1415
aoqi@0 1416 void SharkTopLevelBlock::do_instance_check() {
aoqi@0 1417 // Get the class we're checking against
aoqi@0 1418 bool will_link;
aoqi@0 1419 ciKlass *check_klass = iter()->get_klass(will_link);
aoqi@0 1420
aoqi@0 1421 // Get the class of the object we're checking
aoqi@0 1422 ciKlass *object_klass = xstack(0)->type()->as_klass();
aoqi@0 1423
aoqi@0 1424 // Can we optimize this check away?
aoqi@0 1425 if (static_subtype_check(check_klass, object_klass)) {
aoqi@0 1426 if (bc() == Bytecodes::_instanceof) {
aoqi@0 1427 pop();
aoqi@0 1428 push(SharkValue::jint_constant(1));
aoqi@0 1429 }
aoqi@0 1430 return;
aoqi@0 1431 }
aoqi@0 1432
aoqi@0 1433 // Need to check this one at runtime
aoqi@0 1434 if (will_link)
aoqi@0 1435 do_full_instance_check(check_klass);
aoqi@0 1436 else
aoqi@0 1437 do_trapping_instance_check(check_klass);
aoqi@0 1438 }
aoqi@0 1439
aoqi@0 1440 bool SharkTopLevelBlock::maybe_do_instanceof_if() {
aoqi@0 1441 // Get the class we're checking against
aoqi@0 1442 bool will_link;
aoqi@0 1443 ciKlass *check_klass = iter()->get_klass(will_link);
aoqi@0 1444
aoqi@0 1445 // If the class is unloaded then the instanceof
aoqi@0 1446 // cannot possibly succeed.
aoqi@0 1447 if (!will_link)
aoqi@0 1448 return false;
aoqi@0 1449
aoqi@0 1450 // Keep a copy of the object we're checking
aoqi@0 1451 SharkValue *old_object = xstack(0);
aoqi@0 1452
aoqi@0 1453 // Get the class of the object we're checking
aoqi@0 1454 ciKlass *object_klass = old_object->type()->as_klass();
aoqi@0 1455
aoqi@0 1456 // If the instanceof can be optimized away at compile time
aoqi@0 1457 // then any subsequent checkcasts will be too so we handle
aoqi@0 1458 // it normally.
aoqi@0 1459 if (static_subtype_check(check_klass, object_klass))
aoqi@0 1460 return false;
aoqi@0 1461
aoqi@0 1462 // Perform the instance check
aoqi@0 1463 do_full_instance_check(check_klass);
aoqi@0 1464 Value *result = pop()->jint_value();
aoqi@0 1465
aoqi@0 1466 // Create the casted object
aoqi@0 1467 SharkValue *new_object = SharkValue::create_generic(
aoqi@0 1468 check_klass, old_object->jobject_value(), old_object->zero_checked());
aoqi@0 1469
aoqi@0 1470 // Create two copies of the current state, one with the
aoqi@0 1471 // original object and one with all instances of the
aoqi@0 1472 // original object replaced with the new, casted object.
aoqi@0 1473 SharkState *new_state = current_state();
aoqi@0 1474 SharkState *old_state = new_state->copy();
aoqi@0 1475 new_state->replace_all(old_object, new_object);
aoqi@0 1476
aoqi@0 1477 // Perform the check-and-branch
aoqi@0 1478 switch (iter()->next_bc()) {
aoqi@0 1479 case Bytecodes::_ifeq:
aoqi@0 1480 // branch if not an instance
aoqi@0 1481 do_if_helper(
aoqi@0 1482 ICmpInst::ICMP_EQ,
aoqi@0 1483 LLVMValue::jint_constant(0), result,
aoqi@0 1484 old_state, new_state);
aoqi@0 1485 break;
aoqi@0 1486
aoqi@0 1487 case Bytecodes::_ifne:
aoqi@0 1488 // branch if an instance
aoqi@0 1489 do_if_helper(
aoqi@0 1490 ICmpInst::ICMP_NE,
aoqi@0 1491 LLVMValue::jint_constant(0), result,
aoqi@0 1492 new_state, old_state);
aoqi@0 1493 break;
aoqi@0 1494
aoqi@0 1495 default:
aoqi@0 1496 ShouldNotReachHere();
aoqi@0 1497 }
aoqi@0 1498
aoqi@0 1499 return true;
aoqi@0 1500 }
aoqi@0 1501
aoqi@0 1502 void SharkTopLevelBlock::do_full_instance_check(ciKlass* klass) {
aoqi@0 1503 BasicBlock *not_null = function()->CreateBlock("not_null");
aoqi@0 1504 BasicBlock *subtype_check = function()->CreateBlock("subtype_check");
aoqi@0 1505 BasicBlock *is_instance = function()->CreateBlock("is_instance");
aoqi@0 1506 BasicBlock *not_instance = function()->CreateBlock("not_instance");
aoqi@0 1507 BasicBlock *merge1 = function()->CreateBlock("merge1");
aoqi@0 1508 BasicBlock *merge2 = function()->CreateBlock("merge2");
aoqi@0 1509
aoqi@0 1510 enum InstanceCheckStates {
aoqi@0 1511 IC_IS_NULL,
aoqi@0 1512 IC_IS_INSTANCE,
aoqi@0 1513 IC_NOT_INSTANCE,
aoqi@0 1514 };
aoqi@0 1515
aoqi@0 1516 // Pop the object off the stack
aoqi@0 1517 Value *object = pop()->jobject_value();
aoqi@0 1518
aoqi@0 1519 // Null objects aren't instances of anything
aoqi@0 1520 builder()->CreateCondBr(
aoqi@0 1521 builder()->CreateICmpEQ(object, LLVMValue::null()),
aoqi@0 1522 merge2, not_null);
aoqi@0 1523 BasicBlock *null_block = builder()->GetInsertBlock();
aoqi@0 1524
aoqi@0 1525 // Get the class we're checking against
aoqi@0 1526 builder()->SetInsertPoint(not_null);
aoqi@0 1527 Value *check_klass = builder()->CreateInlineMetadata(klass, SharkType::klass_type());
aoqi@0 1528
aoqi@0 1529 // Get the class of the object being tested
aoqi@0 1530 Value *object_klass = builder()->CreateValueOfStructEntry(
aoqi@0 1531 object, in_ByteSize(oopDesc::klass_offset_in_bytes()),
aoqi@0 1532 SharkType::klass_type(),
aoqi@0 1533 "object_klass");
aoqi@0 1534
aoqi@0 1535 // Perform the check
aoqi@0 1536 builder()->CreateCondBr(
aoqi@0 1537 builder()->CreateICmpEQ(check_klass, object_klass),
aoqi@0 1538 is_instance, subtype_check);
aoqi@0 1539
aoqi@0 1540 builder()->SetInsertPoint(subtype_check);
aoqi@0 1541 builder()->CreateCondBr(
aoqi@0 1542 builder()->CreateICmpNE(
aoqi@0 1543 builder()->CreateCall2(
aoqi@0 1544 builder()->is_subtype_of(), check_klass, object_klass),
aoqi@0 1545 LLVMValue::jbyte_constant(0)),
aoqi@0 1546 is_instance, not_instance);
aoqi@0 1547
aoqi@0 1548 builder()->SetInsertPoint(is_instance);
aoqi@0 1549 builder()->CreateBr(merge1);
aoqi@0 1550
aoqi@0 1551 builder()->SetInsertPoint(not_instance);
aoqi@0 1552 builder()->CreateBr(merge1);
aoqi@0 1553
aoqi@0 1554 // First merge
aoqi@0 1555 builder()->SetInsertPoint(merge1);
aoqi@0 1556 PHINode *nonnull_result = builder()->CreatePHI(
aoqi@0 1557 SharkType::jint_type(), 0, "nonnull_result");
aoqi@0 1558 nonnull_result->addIncoming(
aoqi@0 1559 LLVMValue::jint_constant(IC_IS_INSTANCE), is_instance);
aoqi@0 1560 nonnull_result->addIncoming(
aoqi@0 1561 LLVMValue::jint_constant(IC_NOT_INSTANCE), not_instance);
aoqi@0 1562 BasicBlock *nonnull_block = builder()->GetInsertBlock();
aoqi@0 1563 builder()->CreateBr(merge2);
aoqi@0 1564
aoqi@0 1565 // Second merge
aoqi@0 1566 builder()->SetInsertPoint(merge2);
aoqi@0 1567 PHINode *result = builder()->CreatePHI(
aoqi@0 1568 SharkType::jint_type(), 0, "result");
aoqi@0 1569 result->addIncoming(LLVMValue::jint_constant(IC_IS_NULL), null_block);
aoqi@0 1570 result->addIncoming(nonnull_result, nonnull_block);
aoqi@0 1571
aoqi@0 1572 // Handle the result
aoqi@0 1573 if (bc() == Bytecodes::_checkcast) {
aoqi@0 1574 BasicBlock *failure = function()->CreateBlock("failure");
aoqi@0 1575 BasicBlock *success = function()->CreateBlock("success");
aoqi@0 1576
aoqi@0 1577 builder()->CreateCondBr(
aoqi@0 1578 builder()->CreateICmpNE(
aoqi@0 1579 result, LLVMValue::jint_constant(IC_NOT_INSTANCE)),
aoqi@0 1580 success, failure);
aoqi@0 1581
aoqi@0 1582 builder()->SetInsertPoint(failure);
aoqi@0 1583 SharkState *saved_state = current_state()->copy();
aoqi@0 1584
aoqi@0 1585 call_vm(
aoqi@0 1586 builder()->throw_ClassCastException(),
aoqi@0 1587 builder()->CreateIntToPtr(
aoqi@0 1588 LLVMValue::intptr_constant((intptr_t) __FILE__),
aoqi@0 1589 PointerType::getUnqual(SharkType::jbyte_type())),
aoqi@0 1590 LLVMValue::jint_constant(__LINE__),
aoqi@0 1591 EX_CHECK_NONE);
aoqi@0 1592
aoqi@0 1593 Value *pending_exception = get_pending_exception();
aoqi@0 1594 clear_pending_exception();
aoqi@0 1595 handle_exception(pending_exception, EX_CHECK_FULL);
aoqi@0 1596
aoqi@0 1597 set_current_state(saved_state);
aoqi@0 1598 builder()->SetInsertPoint(success);
aoqi@0 1599 push(SharkValue::create_generic(klass, object, false));
aoqi@0 1600 }
aoqi@0 1601 else {
aoqi@0 1602 push(
aoqi@0 1603 SharkValue::create_jint(
aoqi@0 1604 builder()->CreateIntCast(
aoqi@0 1605 builder()->CreateICmpEQ(
aoqi@0 1606 result, LLVMValue::jint_constant(IC_IS_INSTANCE)),
aoqi@0 1607 SharkType::jint_type(), false), false));
aoqi@0 1608 }
aoqi@0 1609 }
aoqi@0 1610
aoqi@0 1611 void SharkTopLevelBlock::do_trapping_instance_check(ciKlass* klass) {
aoqi@0 1612 BasicBlock *not_null = function()->CreateBlock("not_null");
aoqi@0 1613 BasicBlock *is_null = function()->CreateBlock("null");
aoqi@0 1614
aoqi@0 1615 // Leave the object on the stack so it's there if we trap
aoqi@0 1616 builder()->CreateCondBr(
aoqi@0 1617 builder()->CreateICmpEQ(xstack(0)->jobject_value(), LLVMValue::null()),
aoqi@0 1618 is_null, not_null);
aoqi@0 1619 SharkState *saved_state = current_state()->copy();
aoqi@0 1620
aoqi@0 1621 // If it's not null then we need to trap
aoqi@0 1622 builder()->SetInsertPoint(not_null);
aoqi@0 1623 set_current_state(saved_state->copy());
aoqi@0 1624 do_trap(
aoqi@0 1625 Deoptimization::make_trap_request(
aoqi@0 1626 Deoptimization::Reason_uninitialized,
aoqi@0 1627 Deoptimization::Action_reinterpret));
aoqi@0 1628
aoqi@0 1629 // If it's null then we're ok
aoqi@0 1630 builder()->SetInsertPoint(is_null);
aoqi@0 1631 set_current_state(saved_state);
aoqi@0 1632 if (bc() == Bytecodes::_checkcast) {
aoqi@0 1633 push(SharkValue::create_generic(klass, pop()->jobject_value(), false));
aoqi@0 1634 }
aoqi@0 1635 else {
aoqi@0 1636 pop();
aoqi@0 1637 push(SharkValue::jint_constant(0));
aoqi@0 1638 }
aoqi@0 1639 }
aoqi@0 1640
aoqi@0 1641 void SharkTopLevelBlock::do_new() {
aoqi@0 1642 bool will_link;
aoqi@0 1643 ciInstanceKlass* klass = iter()->get_klass(will_link)->as_instance_klass();
aoqi@0 1644 assert(will_link, "typeflow responsibility");
aoqi@0 1645
aoqi@0 1646 BasicBlock *got_tlab = NULL;
aoqi@0 1647 BasicBlock *heap_alloc = NULL;
aoqi@0 1648 BasicBlock *retry = NULL;
aoqi@0 1649 BasicBlock *got_heap = NULL;
aoqi@0 1650 BasicBlock *initialize = NULL;
aoqi@0 1651 BasicBlock *got_fast = NULL;
aoqi@0 1652 BasicBlock *slow_alloc_and_init = NULL;
aoqi@0 1653 BasicBlock *got_slow = NULL;
aoqi@0 1654 BasicBlock *push_object = NULL;
aoqi@0 1655
aoqi@0 1656 SharkState *fast_state = NULL;
aoqi@0 1657
aoqi@0 1658 Value *tlab_object = NULL;
aoqi@0 1659 Value *heap_object = NULL;
aoqi@0 1660 Value *fast_object = NULL;
aoqi@0 1661 Value *slow_object = NULL;
aoqi@0 1662 Value *object = NULL;
aoqi@0 1663
aoqi@0 1664 // The fast path
aoqi@0 1665 if (!Klass::layout_helper_needs_slow_path(klass->layout_helper())) {
aoqi@0 1666 if (UseTLAB) {
aoqi@0 1667 got_tlab = function()->CreateBlock("got_tlab");
aoqi@0 1668 heap_alloc = function()->CreateBlock("heap_alloc");
aoqi@0 1669 }
aoqi@0 1670 retry = function()->CreateBlock("retry");
aoqi@0 1671 got_heap = function()->CreateBlock("got_heap");
aoqi@0 1672 initialize = function()->CreateBlock("initialize");
aoqi@0 1673 slow_alloc_and_init = function()->CreateBlock("slow_alloc_and_init");
aoqi@0 1674 push_object = function()->CreateBlock("push_object");
aoqi@0 1675
aoqi@0 1676 size_t size_in_bytes = klass->size_helper() << LogHeapWordSize;
aoqi@0 1677
aoqi@0 1678 // Thread local allocation
aoqi@0 1679 if (UseTLAB) {
aoqi@0 1680 Value *top_addr = builder()->CreateAddressOfStructEntry(
aoqi@0 1681 thread(), Thread::tlab_top_offset(),
aoqi@0 1682 PointerType::getUnqual(SharkType::intptr_type()),
aoqi@0 1683 "top_addr");
aoqi@0 1684
aoqi@0 1685 Value *end = builder()->CreateValueOfStructEntry(
aoqi@0 1686 thread(), Thread::tlab_end_offset(),
aoqi@0 1687 SharkType::intptr_type(),
aoqi@0 1688 "end");
aoqi@0 1689
aoqi@0 1690 Value *old_top = builder()->CreateLoad(top_addr, "old_top");
aoqi@0 1691 Value *new_top = builder()->CreateAdd(
aoqi@0 1692 old_top, LLVMValue::intptr_constant(size_in_bytes));
aoqi@0 1693
aoqi@0 1694 builder()->CreateCondBr(
aoqi@0 1695 builder()->CreateICmpULE(new_top, end),
aoqi@0 1696 got_tlab, heap_alloc);
aoqi@0 1697
aoqi@0 1698 builder()->SetInsertPoint(got_tlab);
aoqi@0 1699 tlab_object = builder()->CreateIntToPtr(
aoqi@0 1700 old_top, SharkType::oop_type(), "tlab_object");
aoqi@0 1701
aoqi@0 1702 builder()->CreateStore(new_top, top_addr);
aoqi@0 1703 builder()->CreateBr(initialize);
aoqi@0 1704
aoqi@0 1705 builder()->SetInsertPoint(heap_alloc);
aoqi@0 1706 }
aoqi@0 1707
aoqi@0 1708 // Heap allocation
aoqi@0 1709 Value *top_addr = builder()->CreateIntToPtr(
aoqi@0 1710 LLVMValue::intptr_constant((intptr_t) Universe::heap()->top_addr()),
aoqi@0 1711 PointerType::getUnqual(SharkType::intptr_type()),
aoqi@0 1712 "top_addr");
aoqi@0 1713
aoqi@0 1714 Value *end = builder()->CreateLoad(
aoqi@0 1715 builder()->CreateIntToPtr(
aoqi@0 1716 LLVMValue::intptr_constant((intptr_t) Universe::heap()->end_addr()),
aoqi@0 1717 PointerType::getUnqual(SharkType::intptr_type())),
aoqi@0 1718 "end");
aoqi@0 1719
aoqi@0 1720 builder()->CreateBr(retry);
aoqi@0 1721 builder()->SetInsertPoint(retry);
aoqi@0 1722
aoqi@0 1723 Value *old_top = builder()->CreateLoad(top_addr, "top");
aoqi@0 1724 Value *new_top = builder()->CreateAdd(
aoqi@0 1725 old_top, LLVMValue::intptr_constant(size_in_bytes));
aoqi@0 1726
aoqi@0 1727 builder()->CreateCondBr(
aoqi@0 1728 builder()->CreateICmpULE(new_top, end),
aoqi@0 1729 got_heap, slow_alloc_and_init);
aoqi@0 1730
aoqi@0 1731 builder()->SetInsertPoint(got_heap);
aoqi@0 1732 heap_object = builder()->CreateIntToPtr(
aoqi@0 1733 old_top, SharkType::oop_type(), "heap_object");
aoqi@0 1734
aoqi@0 1735 Value *check = builder()->CreateAtomicCmpXchg(top_addr, old_top, new_top, llvm::SequentiallyConsistent);
aoqi@0 1736 builder()->CreateCondBr(
aoqi@0 1737 builder()->CreateICmpEQ(old_top, check),
aoqi@0 1738 initialize, retry);
aoqi@0 1739
aoqi@0 1740 // Initialize the object
aoqi@0 1741 builder()->SetInsertPoint(initialize);
aoqi@0 1742 if (tlab_object) {
aoqi@0 1743 PHINode *phi = builder()->CreatePHI(
aoqi@0 1744 SharkType::oop_type(), 0, "fast_object");
aoqi@0 1745 phi->addIncoming(tlab_object, got_tlab);
aoqi@0 1746 phi->addIncoming(heap_object, got_heap);
aoqi@0 1747 fast_object = phi;
aoqi@0 1748 }
aoqi@0 1749 else {
aoqi@0 1750 fast_object = heap_object;
aoqi@0 1751 }
aoqi@0 1752
aoqi@0 1753 builder()->CreateMemset(
aoqi@0 1754 builder()->CreateBitCast(
aoqi@0 1755 fast_object, PointerType::getUnqual(SharkType::jbyte_type())),
aoqi@0 1756 LLVMValue::jbyte_constant(0),
aoqi@0 1757 LLVMValue::jint_constant(size_in_bytes),
aoqi@0 1758 LLVMValue::jint_constant(HeapWordSize));
aoqi@0 1759
aoqi@0 1760 Value *mark_addr = builder()->CreateAddressOfStructEntry(
aoqi@0 1761 fast_object, in_ByteSize(oopDesc::mark_offset_in_bytes()),
aoqi@0 1762 PointerType::getUnqual(SharkType::intptr_type()),
aoqi@0 1763 "mark_addr");
aoqi@0 1764
aoqi@0 1765 Value *klass_addr = builder()->CreateAddressOfStructEntry(
aoqi@0 1766 fast_object, in_ByteSize(oopDesc::klass_offset_in_bytes()),
aoqi@0 1767 PointerType::getUnqual(SharkType::klass_type()),
aoqi@0 1768 "klass_addr");
aoqi@0 1769
aoqi@0 1770 // Set the mark
aoqi@0 1771 intptr_t mark;
aoqi@0 1772 if (UseBiasedLocking) {
aoqi@0 1773 Unimplemented();
aoqi@0 1774 }
aoqi@0 1775 else {
aoqi@0 1776 mark = (intptr_t) markOopDesc::prototype();
aoqi@0 1777 }
aoqi@0 1778 builder()->CreateStore(LLVMValue::intptr_constant(mark), mark_addr);
aoqi@0 1779
aoqi@0 1780 // Set the class
aoqi@0 1781 Value *rtklass = builder()->CreateInlineMetadata(klass, SharkType::klass_type());
aoqi@0 1782 builder()->CreateStore(rtklass, klass_addr);
aoqi@0 1783 got_fast = builder()->GetInsertBlock();
aoqi@0 1784
aoqi@0 1785 builder()->CreateBr(push_object);
aoqi@0 1786 builder()->SetInsertPoint(slow_alloc_and_init);
aoqi@0 1787 fast_state = current_state()->copy();
aoqi@0 1788 }
aoqi@0 1789
aoqi@0 1790 // The slow path
aoqi@0 1791 call_vm(
aoqi@0 1792 builder()->new_instance(),
aoqi@0 1793 LLVMValue::jint_constant(iter()->get_klass_index()),
aoqi@0 1794 EX_CHECK_FULL);
aoqi@0 1795 slow_object = get_vm_result();
aoqi@0 1796 got_slow = builder()->GetInsertBlock();
aoqi@0 1797
aoqi@0 1798 // Push the object
aoqi@0 1799 if (push_object) {
aoqi@0 1800 builder()->CreateBr(push_object);
aoqi@0 1801 builder()->SetInsertPoint(push_object);
aoqi@0 1802 }
aoqi@0 1803 if (fast_object) {
aoqi@0 1804 PHINode *phi = builder()->CreatePHI(SharkType::oop_type(), 0, "object");
aoqi@0 1805 phi->addIncoming(fast_object, got_fast);
aoqi@0 1806 phi->addIncoming(slow_object, got_slow);
aoqi@0 1807 object = phi;
aoqi@0 1808 current_state()->merge(fast_state, got_fast, got_slow);
aoqi@0 1809 }
aoqi@0 1810 else {
aoqi@0 1811 object = slow_object;
aoqi@0 1812 }
aoqi@0 1813
aoqi@0 1814 push(SharkValue::create_jobject(object, true));
aoqi@0 1815 }
aoqi@0 1816
aoqi@0 1817 void SharkTopLevelBlock::do_newarray() {
aoqi@0 1818 BasicType type = (BasicType) iter()->get_index();
aoqi@0 1819
aoqi@0 1820 call_vm(
aoqi@0 1821 builder()->newarray(),
aoqi@0 1822 LLVMValue::jint_constant(type),
aoqi@0 1823 pop()->jint_value(),
aoqi@0 1824 EX_CHECK_FULL);
aoqi@0 1825
aoqi@0 1826 ciArrayKlass *array_klass = ciArrayKlass::make(ciType::make(type));
aoqi@0 1827 push(SharkValue::create_generic(array_klass, get_vm_result(), true));
aoqi@0 1828 }
aoqi@0 1829
aoqi@0 1830 void SharkTopLevelBlock::do_anewarray() {
aoqi@0 1831 bool will_link;
aoqi@0 1832 ciKlass *klass = iter()->get_klass(will_link);
aoqi@0 1833 assert(will_link, "typeflow responsibility");
aoqi@0 1834
aoqi@0 1835 ciObjArrayKlass *array_klass = ciObjArrayKlass::make(klass);
aoqi@0 1836 if (!array_klass->is_loaded()) {
aoqi@0 1837 Unimplemented();
aoqi@0 1838 }
aoqi@0 1839
aoqi@0 1840 call_vm(
aoqi@0 1841 builder()->anewarray(),
aoqi@0 1842 LLVMValue::jint_constant(iter()->get_klass_index()),
aoqi@0 1843 pop()->jint_value(),
aoqi@0 1844 EX_CHECK_FULL);
aoqi@0 1845
aoqi@0 1846 push(SharkValue::create_generic(array_klass, get_vm_result(), true));
aoqi@0 1847 }
aoqi@0 1848
aoqi@0 1849 void SharkTopLevelBlock::do_multianewarray() {
aoqi@0 1850 bool will_link;
aoqi@0 1851 ciArrayKlass *array_klass = iter()->get_klass(will_link)->as_array_klass();
aoqi@0 1852 assert(will_link, "typeflow responsibility");
aoqi@0 1853
aoqi@0 1854 // The dimensions are stack values, so we use their slots for the
aoqi@0 1855 // dimensions array. Note that we are storing them in the reverse
aoqi@0 1856 // of normal stack order.
aoqi@0 1857 int ndims = iter()->get_dimensions();
aoqi@0 1858
aoqi@0 1859 Value *dimensions = stack()->slot_addr(
aoqi@0 1860 stack()->stack_slots_offset() + max_stack() - xstack_depth(),
aoqi@0 1861 ArrayType::get(SharkType::jint_type(), ndims),
aoqi@0 1862 "dimensions");
aoqi@0 1863
aoqi@0 1864 for (int i = 0; i < ndims; i++) {
aoqi@0 1865 builder()->CreateStore(
aoqi@0 1866 xstack(ndims - 1 - i)->jint_value(),
aoqi@0 1867 builder()->CreateStructGEP(dimensions, i));
aoqi@0 1868 }
aoqi@0 1869
aoqi@0 1870 call_vm(
aoqi@0 1871 builder()->multianewarray(),
aoqi@0 1872 LLVMValue::jint_constant(iter()->get_klass_index()),
aoqi@0 1873 LLVMValue::jint_constant(ndims),
aoqi@0 1874 builder()->CreateStructGEP(dimensions, 0),
aoqi@0 1875 EX_CHECK_FULL);
aoqi@0 1876
aoqi@0 1877 // Now we can pop the dimensions off the stack
aoqi@0 1878 for (int i = 0; i < ndims; i++)
aoqi@0 1879 pop();
aoqi@0 1880
aoqi@0 1881 push(SharkValue::create_generic(array_klass, get_vm_result(), true));
aoqi@0 1882 }
aoqi@0 1883
aoqi@0 1884 void SharkTopLevelBlock::acquire_method_lock() {
aoqi@0 1885 Value *lockee;
aoqi@0 1886 if (target()->is_static()) {
aoqi@0 1887 lockee = builder()->CreateInlineOop(target()->holder()->java_mirror());
aoqi@0 1888 }
aoqi@0 1889 else
aoqi@0 1890 lockee = local(0)->jobject_value();
aoqi@0 1891
aoqi@0 1892 iter()->force_bci(start()); // for the decache in acquire_lock
aoqi@0 1893 acquire_lock(lockee, EX_CHECK_NO_CATCH);
aoqi@0 1894 }
aoqi@0 1895
aoqi@0 1896 void SharkTopLevelBlock::do_monitorenter() {
aoqi@0 1897 SharkValue *lockee = pop();
aoqi@0 1898 check_null(lockee);
aoqi@0 1899 acquire_lock(lockee->jobject_value(), EX_CHECK_FULL);
aoqi@0 1900 }
aoqi@0 1901
aoqi@0 1902 void SharkTopLevelBlock::do_monitorexit() {
aoqi@0 1903 pop(); // don't need this (monitors are block structured)
aoqi@0 1904 release_lock(EX_CHECK_NO_CATCH);
aoqi@0 1905 }
aoqi@0 1906
aoqi@0 1907 void SharkTopLevelBlock::acquire_lock(Value *lockee, int exception_action) {
aoqi@0 1908 BasicBlock *try_recursive = function()->CreateBlock("try_recursive");
aoqi@0 1909 BasicBlock *got_recursive = function()->CreateBlock("got_recursive");
aoqi@0 1910 BasicBlock *not_recursive = function()->CreateBlock("not_recursive");
aoqi@0 1911 BasicBlock *acquired_fast = function()->CreateBlock("acquired_fast");
aoqi@0 1912 BasicBlock *lock_acquired = function()->CreateBlock("lock_acquired");
aoqi@0 1913
aoqi@0 1914 int monitor = num_monitors();
aoqi@0 1915 Value *monitor_addr = stack()->monitor_addr(monitor);
aoqi@0 1916 Value *monitor_object_addr = stack()->monitor_object_addr(monitor);
aoqi@0 1917 Value *monitor_header_addr = stack()->monitor_header_addr(monitor);
aoqi@0 1918
aoqi@0 1919 // Store the object and mark the slot as live
aoqi@0 1920 builder()->CreateStore(lockee, monitor_object_addr);
aoqi@0 1921 set_num_monitors(monitor + 1);
aoqi@0 1922
aoqi@0 1923 // Try a simple lock
aoqi@0 1924 Value *mark_addr = builder()->CreateAddressOfStructEntry(
aoqi@0 1925 lockee, in_ByteSize(oopDesc::mark_offset_in_bytes()),
aoqi@0 1926 PointerType::getUnqual(SharkType::intptr_type()),
aoqi@0 1927 "mark_addr");
aoqi@0 1928
aoqi@0 1929 Value *mark = builder()->CreateLoad(mark_addr, "mark");
aoqi@0 1930 Value *disp = builder()->CreateOr(
aoqi@0 1931 mark, LLVMValue::intptr_constant(markOopDesc::unlocked_value), "disp");
aoqi@0 1932 builder()->CreateStore(disp, monitor_header_addr);
aoqi@0 1933
aoqi@0 1934 Value *lock = builder()->CreatePtrToInt(
aoqi@0 1935 monitor_header_addr, SharkType::intptr_type());
aoqi@0 1936 Value *check = builder()->CreateAtomicCmpXchg(mark_addr, disp, lock, llvm::Acquire);
aoqi@0 1937 builder()->CreateCondBr(
aoqi@0 1938 builder()->CreateICmpEQ(disp, check),
aoqi@0 1939 acquired_fast, try_recursive);
aoqi@0 1940
aoqi@0 1941 // Locking failed, but maybe this thread already owns it
aoqi@0 1942 builder()->SetInsertPoint(try_recursive);
aoqi@0 1943 Value *addr = builder()->CreateAnd(
aoqi@0 1944 disp,
aoqi@0 1945 LLVMValue::intptr_constant(~markOopDesc::lock_mask_in_place));
aoqi@0 1946
aoqi@0 1947 // NB we use the entire stack, but JavaThread::is_lock_owned()
aoqi@0 1948 // uses a more limited range. I don't think it hurts though...
aoqi@0 1949 Value *stack_limit = builder()->CreateValueOfStructEntry(
aoqi@0 1950 thread(), Thread::stack_base_offset(),
aoqi@0 1951 SharkType::intptr_type(),
aoqi@0 1952 "stack_limit");
aoqi@0 1953
aoqi@0 1954 assert(sizeof(size_t) == sizeof(intptr_t), "should be");
aoqi@0 1955 Value *stack_size = builder()->CreateValueOfStructEntry(
aoqi@0 1956 thread(), Thread::stack_size_offset(),
aoqi@0 1957 SharkType::intptr_type(),
aoqi@0 1958 "stack_size");
aoqi@0 1959
aoqi@0 1960 Value *stack_start =
aoqi@0 1961 builder()->CreateSub(stack_limit, stack_size, "stack_start");
aoqi@0 1962
aoqi@0 1963 builder()->CreateCondBr(
aoqi@0 1964 builder()->CreateAnd(
aoqi@0 1965 builder()->CreateICmpUGE(addr, stack_start),
aoqi@0 1966 builder()->CreateICmpULT(addr, stack_limit)),
aoqi@0 1967 got_recursive, not_recursive);
aoqi@0 1968
aoqi@0 1969 builder()->SetInsertPoint(got_recursive);
aoqi@0 1970 builder()->CreateStore(LLVMValue::intptr_constant(0), monitor_header_addr);
aoqi@0 1971 builder()->CreateBr(acquired_fast);
aoqi@0 1972
aoqi@0 1973 // Create an edge for the state merge
aoqi@0 1974 builder()->SetInsertPoint(acquired_fast);
aoqi@0 1975 SharkState *fast_state = current_state()->copy();
aoqi@0 1976 builder()->CreateBr(lock_acquired);
aoqi@0 1977
aoqi@0 1978 // It's not a recursive case so we need to drop into the runtime
aoqi@0 1979 builder()->SetInsertPoint(not_recursive);
aoqi@0 1980 call_vm(
aoqi@0 1981 builder()->monitorenter(), monitor_addr,
aoqi@0 1982 exception_action | EAM_MONITOR_FUDGE);
aoqi@0 1983 BasicBlock *acquired_slow = builder()->GetInsertBlock();
aoqi@0 1984 builder()->CreateBr(lock_acquired);
aoqi@0 1985
aoqi@0 1986 // All done
aoqi@0 1987 builder()->SetInsertPoint(lock_acquired);
aoqi@0 1988 current_state()->merge(fast_state, acquired_fast, acquired_slow);
aoqi@0 1989 }
aoqi@0 1990
aoqi@0 1991 void SharkTopLevelBlock::release_lock(int exception_action) {
aoqi@0 1992 BasicBlock *not_recursive = function()->CreateBlock("not_recursive");
aoqi@0 1993 BasicBlock *released_fast = function()->CreateBlock("released_fast");
aoqi@0 1994 BasicBlock *slow_path = function()->CreateBlock("slow_path");
aoqi@0 1995 BasicBlock *lock_released = function()->CreateBlock("lock_released");
aoqi@0 1996
aoqi@0 1997 int monitor = num_monitors() - 1;
aoqi@0 1998 Value *monitor_addr = stack()->monitor_addr(monitor);
aoqi@0 1999 Value *monitor_object_addr = stack()->monitor_object_addr(monitor);
aoqi@0 2000 Value *monitor_header_addr = stack()->monitor_header_addr(monitor);
aoqi@0 2001
aoqi@0 2002 // If it is recursive then we're already done
aoqi@0 2003 Value *disp = builder()->CreateLoad(monitor_header_addr);
aoqi@0 2004 builder()->CreateCondBr(
aoqi@0 2005 builder()->CreateICmpEQ(disp, LLVMValue::intptr_constant(0)),
aoqi@0 2006 released_fast, not_recursive);
aoqi@0 2007
aoqi@0 2008 // Try a simple unlock
aoqi@0 2009 builder()->SetInsertPoint(not_recursive);
aoqi@0 2010
aoqi@0 2011 Value *lock = builder()->CreatePtrToInt(
aoqi@0 2012 monitor_header_addr, SharkType::intptr_type());
aoqi@0 2013
aoqi@0 2014 Value *lockee = builder()->CreateLoad(monitor_object_addr);
aoqi@0 2015
aoqi@0 2016 Value *mark_addr = builder()->CreateAddressOfStructEntry(
aoqi@0 2017 lockee, in_ByteSize(oopDesc::mark_offset_in_bytes()),
aoqi@0 2018 PointerType::getUnqual(SharkType::intptr_type()),
aoqi@0 2019 "mark_addr");
aoqi@0 2020
aoqi@0 2021 Value *check = builder()->CreateAtomicCmpXchg(mark_addr, lock, disp, llvm::Release);
aoqi@0 2022 builder()->CreateCondBr(
aoqi@0 2023 builder()->CreateICmpEQ(lock, check),
aoqi@0 2024 released_fast, slow_path);
aoqi@0 2025
aoqi@0 2026 // Create an edge for the state merge
aoqi@0 2027 builder()->SetInsertPoint(released_fast);
aoqi@0 2028 SharkState *fast_state = current_state()->copy();
aoqi@0 2029 builder()->CreateBr(lock_released);
aoqi@0 2030
aoqi@0 2031 // Need to drop into the runtime to release this one
aoqi@0 2032 builder()->SetInsertPoint(slow_path);
aoqi@0 2033 call_vm(builder()->monitorexit(), monitor_addr, exception_action);
aoqi@0 2034 BasicBlock *released_slow = builder()->GetInsertBlock();
aoqi@0 2035 builder()->CreateBr(lock_released);
aoqi@0 2036
aoqi@0 2037 // All done
aoqi@0 2038 builder()->SetInsertPoint(lock_released);
aoqi@0 2039 current_state()->merge(fast_state, released_fast, released_slow);
aoqi@0 2040
aoqi@0 2041 // The object slot is now dead
aoqi@0 2042 set_num_monitors(monitor);
aoqi@0 2043 }

mercurial