src/share/vm/shark/sharkTopLevelBlock.cpp

Wed, 07 Dec 2011 11:35:03 +0100

author
stefank
date
Wed, 07 Dec 2011 11:35:03 +0100
changeset 3391
069ab3f976d3
parent 2314
f95d63e2154a
child 4037
da91efe96a93
permissions
-rw-r--r--

7118863: Move sizeof(klassOopDesc) into the *Klass::*_offset_in_bytes() functions
Summary: Moved sizeof(klassOopDesc), changed the return type to ByteSize and removed the _in_bytes suffix.
Reviewed-by: never, bdelsart, coleenp, jrose

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

mercurial