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