src/share/vm/shark/sharkTopLevelBlock.cpp

Tue, 27 Nov 2012 12:48:52 -0800

author
twisti
date
Tue, 27 Nov 2012 12:48:52 -0800
changeset 4314
2cd5e15048e6
parent 4037
da91efe96a93
child 4442
c566b81b3323
permissions
-rw-r--r--

8003868: fix shark for latest HotSpot and LLVM
Reviewed-by: twisti
Contributed-by: Roman Kennke <rkennke@redhat.com>

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

mercurial