src/share/vm/shark/sharkTopLevelBlock.cpp

Wed, 11 Aug 2010 05:51:21 -0700

author
twisti
date
Wed, 11 Aug 2010 05:51:21 -0700
changeset 2047
d2ede61b7a12
child 2314
f95d63e2154a
permissions
-rw-r--r--

6976186: integrate Shark HotSpot changes
Summary: Shark is a JIT compiler for Zero that uses the LLVM compiler infrastructure.
Reviewed-by: kvn, twisti
Contributed-by: Gary Benson <gbenson@redhat.com>

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

mercurial