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