48 using namespace llvm;
49
50 void SharkTopLevelBlock::scan_for_traps() {
51 // If typeflow found a trap then don't scan past it
52 int limit_bci = ciblock()->has_trap() ? ciblock()->trap_bci() : limit();
53
54 // Scan the bytecode for traps that are always hit
55 iter()->reset_to_bci(start());
56 while (iter()->next_bci() < limit_bci) {
57 iter()->next();
58
59 ciField *field;
60 ciMethod *method;
61 ciInstanceKlass *klass;
62 bool will_link;
63 bool is_field;
64
65 switch (bc()) {
66 case Bytecodes::_ldc:
67 case Bytecodes::_ldc_w:
68 if (!SharkConstant::for_ldc(iter())->is_loaded()) {
69 set_trap(
70 Deoptimization::make_trap_request(
71 Deoptimization::Reason_uninitialized,
72 Deoptimization::Action_reinterpret), bci());
73 return;
74 }
75 break;
76
77 case Bytecodes::_getfield:
78 case Bytecodes::_getstatic:
79 case Bytecodes::_putfield:
80 case Bytecodes::_putstatic:
81 field = iter()->get_field(will_link);
82 assert(will_link, "typeflow responsibility");
83 is_field = (bc() == Bytecodes::_getfield || bc() == Bytecodes::_putfield);
84
85 // If the bytecode does not match the field then bail out to
86 // the interpreter to throw an IncompatibleClassChangeError
87 if (is_field == field->is_static()) {
92 return;
93 }
94
95 // Bail out if we are trying to access a static variable
96 // before the class initializer has completed.
97 if (!is_field && !field->holder()->is_initialized()) {
98 if (!static_field_ok_in_clinit(field)) {
99 set_trap(
100 Deoptimization::make_trap_request(
101 Deoptimization::Reason_uninitialized,
102 Deoptimization::Action_reinterpret), bci());
103 return;
104 }
105 }
106 break;
107
108 case Bytecodes::_invokestatic:
109 case Bytecodes::_invokespecial:
110 case Bytecodes::_invokevirtual:
111 case Bytecodes::_invokeinterface:
112 method = iter()->get_method(will_link);
113 assert(will_link, "typeflow responsibility");
114
115 if (!method->holder()->is_linked()) {
116 set_trap(
117 Deoptimization::make_trap_request(
118 Deoptimization::Reason_uninitialized,
119 Deoptimization::Action_reinterpret), bci());
120 return;
121 }
122
123 if (bc() == Bytecodes::_invokevirtual) {
124 klass = ciEnv::get_instance_klass_for_declared_method_holder(
125 iter()->get_declared_method_holder());
126 if (!klass->is_linked()) {
127 set_trap(
128 Deoptimization::make_trap_request(
129 Deoptimization::Reason_uninitialized,
130 Deoptimization::Action_reinterpret), bci());
131 return;
132 }
545
546 // Install the catch-all handler, if present
547 if (has_catch_all) {
548 SharkTopLevelBlock* handler = this->exception(num_options);
549 assert(handler != NULL, "catch-all handler cannot be unloaded");
550
551 builder()->CreateBr(handler->entry_block());
552 handler->add_incoming(current_state());
553 return;
554 }
555 }
556
557 // No exception handler was found; unwind and return
558 handle_return(T_VOID, exception);
559 }
560
561 void SharkTopLevelBlock::marshal_exception_fast(int num_options) {
562 Value *exception_klass = builder()->CreateValueOfStructEntry(
563 xstack(0)->jobject_value(),
564 in_ByteSize(oopDesc::klass_offset_in_bytes()),
565 SharkType::oop_type(),
566 "exception_klass");
567
568 for (int i = 0; i < num_options; i++) {
569 Value *check_klass =
570 builder()->CreateInlineOop(exc_handler(i)->catch_klass());
571
572 BasicBlock *not_exact = function()->CreateBlock("not_exact");
573 BasicBlock *not_subtype = function()->CreateBlock("not_subtype");
574
575 builder()->CreateCondBr(
576 builder()->CreateICmpEQ(check_klass, exception_klass),
577 handler_for_exception(i), not_exact);
578
579 builder()->SetInsertPoint(not_exact);
580 builder()->CreateCondBr(
581 builder()->CreateICmpNE(
582 builder()->CreateCall2(
583 builder()->is_subtype_of(), check_klass, exception_klass),
584 LLVMValue::jbyte_constant(0)),
585 handler_for_exception(i), not_subtype);
586
587 builder()->SetInsertPoint(not_subtype);
588 }
589 }
590
806 }
807
808 void SharkTopLevelBlock::do_arraylength() {
809 SharkValue *array = pop();
810 check_null(array);
811 Value *length = builder()->CreateArrayLength(array->jarray_value());
812 push(SharkValue::create_jint(length, false));
813 }
814
815 void SharkTopLevelBlock::do_aload(BasicType basic_type) {
816 SharkValue *index = pop();
817 SharkValue *array = pop();
818
819 check_null(array);
820 check_bounds(array, index);
821
822 Value *value = builder()->CreateLoad(
823 builder()->CreateArrayAddress(
824 array->jarray_value(), basic_type, index->jint_value()));
825
826 const Type *stack_type = SharkType::to_stackType(basic_type);
827 if (value->getType() != stack_type)
828 value = builder()->CreateIntCast(value, stack_type, basic_type != T_CHAR);
829
830 switch (basic_type) {
831 case T_BYTE:
832 case T_CHAR:
833 case T_SHORT:
834 case T_INT:
835 push(SharkValue::create_jint(value, false));
836 break;
837
838 case T_LONG:
839 push(SharkValue::create_jlong(value, false));
840 break;
841
842 case T_FLOAT:
843 push(SharkValue::create_jfloat(value));
844 break;
845
846 case T_DOUBLE:
893 break;
894
895 case T_FLOAT:
896 value = svalue->jfloat_value();
897 break;
898
899 case T_DOUBLE:
900 value = svalue->jdouble_value();
901 break;
902
903 case T_OBJECT:
904 value = svalue->jobject_value();
905 // XXX assignability check
906 break;
907
908 default:
909 tty->print_cr("Unhandled type %s", type2name(basic_type));
910 ShouldNotReachHere();
911 }
912
913 const Type *array_type = SharkType::to_arrayType(basic_type);
914 if (value->getType() != array_type)
915 value = builder()->CreateIntCast(value, array_type, basic_type != T_CHAR);
916
917 Value *addr = builder()->CreateArrayAddress(
918 array->jarray_value(), basic_type, index->jint_value(), "addr");
919
920 builder()->CreateStore(value, addr);
921
922 if (basic_type == T_OBJECT) // XXX or T_ARRAY?
923 builder()->CreateUpdateBarrierSet(oopDesc::bs(), addr);
924 }
925
926 void SharkTopLevelBlock::do_return(BasicType type) {
927 if (target()->intrinsic_id() == vmIntrinsics::_Object_init)
928 call_register_finalizer(local(0)->jobject_value());
929 maybe_add_safepoint();
930 handle_return(type, NULL);
931 }
932
933 void SharkTopLevelBlock::do_athrow() {
1085 // us dependent on that target method not getting overridden
1086 // by dynamic class loading.
1087 if (monomorphic_target != NULL) {
1088 dependencies()->assert_unique_concrete_method(
1089 actual_receiver, monomorphic_target);
1090 return monomorphic_target;
1091 }
1092
1093 // Because Opto distinguishes exact types from inexact ones
1094 // it can perform a further optimization to replace calls
1095 // with non-monomorphic targets if the receiver has an exact
1096 // type. We don't mark types this way, so we can't do this.
1097
1098 #endif // SHARK_CAN_DEOPTIMIZE_ANYWHERE
1099
1100 return NULL;
1101 }
1102
1103 Value *SharkTopLevelBlock::get_direct_callee(ciMethod* method) {
1104 return builder()->CreateBitCast(
1105 builder()->CreateInlineOop(method),
1106 SharkType::Method*_type(),
1107 "callee");
1108 }
1109
1110 Value *SharkTopLevelBlock::get_virtual_callee(SharkValue* receiver,
1111 int vtable_index) {
1112 Value *klass = builder()->CreateValueOfStructEntry(
1113 receiver->jobject_value(),
1114 in_ByteSize(oopDesc::klass_offset_in_bytes()),
1115 SharkType::oop_type(),
1116 "klass");
1117
1118 return builder()->CreateLoad(
1119 builder()->CreateArrayAddress(
1120 klass,
1121 SharkType::Method*_type(),
1122 vtableEntry::size() * wordSize,
1123 in_ByteSize(InstanceKlass::vtable_start_offset() * wordSize),
1124 LLVMValue::intptr_constant(vtable_index)),
1125 "callee");
1126 }
1127
1128 Value* SharkTopLevelBlock::get_interface_callee(SharkValue *receiver,
1129 ciMethod* method) {
1130 BasicBlock *loop = function()->CreateBlock("loop");
1131 BasicBlock *got_null = function()->CreateBlock("got_null");
1132 BasicBlock *not_null = function()->CreateBlock("not_null");
1133 BasicBlock *next = function()->CreateBlock("next");
1134 BasicBlock *got_entry = function()->CreateBlock("got_entry");
1135
1136 // Locate the receiver's itable
1137 Value *object_klass = builder()->CreateValueOfStructEntry(
1138 receiver->jobject_value(), in_ByteSize(oopDesc::klass_offset_in_bytes()),
1139 SharkType::oop_type(),
1140 "object_klass");
1141
1142 Value *vtable_start = builder()->CreateAdd(
1143 builder()->CreatePtrToInt(object_klass, SharkType::intptr_type()),
1144 LLVMValue::intptr_constant(
1145 InstanceKlass::vtable_start_offset() * HeapWordSize),
1146 "vtable_start");
1147
1148 Value *vtable_length = builder()->CreateValueOfStructEntry(
1149 object_klass,
1150 in_ByteSize(InstanceKlass::vtable_length_offset() * HeapWordSize),
1151 SharkType::jint_type(),
1152 "vtable_length");
1153 vtable_length =
1154 builder()->CreateIntCast(vtable_length, SharkType::intptr_type(), false);
1155
1156 bool needs_aligning = HeapWordsPerLong > 1;
1157 Value *itable_start = builder()->CreateAdd(
1158 vtable_start,
1159 builder()->CreateShl(
1160 vtable_length,
1161 LLVMValue::intptr_constant(exact_log2(vtableEntry::size() * wordSize))),
1162 needs_aligning ? "" : "itable_start");
1163 if (needs_aligning) {
1164 itable_start = builder()->CreateAnd(
1165 builder()->CreateAdd(
1166 itable_start, LLVMValue::intptr_constant(BytesPerLong - 1)),
1167 LLVMValue::intptr_constant(~(BytesPerLong - 1)),
1168 "itable_start");
1169 }
1170
1171 // Locate this interface's entry in the table
1172 Value *iklass = builder()->CreateInlineOop(method->holder());
1173 BasicBlock *loop_entry = builder()->GetInsertBlock();
1174 builder()->CreateBr(loop);
1175 builder()->SetInsertPoint(loop);
1176 PHINode *itable_entry_addr = builder()->CreatePHI(
1177 SharkType::intptr_type(), "itable_entry_addr");
1178 itable_entry_addr->addIncoming(itable_start, loop_entry);
1179
1180 Value *itable_entry = builder()->CreateIntToPtr(
1181 itable_entry_addr, SharkType::itableOffsetEntry_type(), "itable_entry");
1182
1183 Value *itable_iklass = builder()->CreateValueOfStructEntry(
1184 itable_entry,
1185 in_ByteSize(itableOffsetEntry::interface_offset_in_bytes()),
1186 SharkType::oop_type(),
1187 "itable_iklass");
1188
1189 builder()->CreateCondBr(
1190 builder()->CreateICmpEQ(itable_iklass, LLVMValue::null()),
1191 got_null, not_null);
1192
1193 // A null entry means that the class doesn't implement the
1194 // interface, and wasn't the same as the class checked when
1195 // the interface was resolved.
1196 builder()->SetInsertPoint(got_null);
1197 builder()->CreateUnimplemented(__FILE__, __LINE__);
1198 builder()->CreateUnreachable();
1199
1200 builder()->SetInsertPoint(not_null);
1201 builder()->CreateCondBr(
1202 builder()->CreateICmpEQ(itable_iklass, iklass),
1203 got_entry, next);
1204
1205 builder()->SetInsertPoint(next);
1206 Value *next_entry = builder()->CreateAdd(
1207 itable_entry_addr,
1208 LLVMValue::intptr_constant(itableOffsetEntry::size() * wordSize));
1209 builder()->CreateBr(loop);
1210 itable_entry_addr->addIncoming(next_entry, next);
1214 Value *offset = builder()->CreateValueOfStructEntry(
1215 itable_entry,
1216 in_ByteSize(itableOffsetEntry::offset_offset_in_bytes()),
1217 SharkType::jint_type(),
1218 "offset");
1219 offset =
1220 builder()->CreateIntCast(offset, SharkType::intptr_type(), false);
1221
1222 return builder()->CreateLoad(
1223 builder()->CreateIntToPtr(
1224 builder()->CreateAdd(
1225 builder()->CreateAdd(
1226 builder()->CreateAdd(
1227 builder()->CreatePtrToInt(
1228 object_klass, SharkType::intptr_type()),
1229 offset),
1230 LLVMValue::intptr_constant(
1231 method->itable_index() * itableMethodEntry::size() * wordSize)),
1232 LLVMValue::intptr_constant(
1233 itableMethodEntry::method_offset_in_bytes())),
1234 PointerType::getUnqual(SharkType::Method*_type())),
1235 "callee");
1236 }
1237
1238 void SharkTopLevelBlock::do_call() {
1239 // Set frequently used booleans
1240 bool is_static = bc() == Bytecodes::_invokestatic;
1241 bool is_virtual = bc() == Bytecodes::_invokevirtual;
1242 bool is_interface = bc() == Bytecodes::_invokeinterface;
1243
1244 // Find the method being called
1245 bool will_link;
1246 ciMethod *dest_method = iter()->get_method(will_link);
1247 assert(will_link, "typeflow responsibility");
1248 assert(dest_method->is_static() == is_static, "must match bc");
1249
1250 // Find the class of the method being called. Note
1251 // that the superclass check in the second assertion
1252 // is to cope with a hole in the spec that allows for
1253 // invokeinterface instructions where the resolved
1254 // method is a virtual method in java.lang.Object.
1255 // javac doesn't generate code like that, but there's
1256 // no reason a compliant Java compiler might not.
1257 ciInstanceKlass *holder_klass = dest_method->holder();
1258 assert(holder_klass->is_loaded(), "scan_for_traps responsibility");
1259 assert(holder_klass->is_interface() ||
1260 holder_klass->super() == NULL ||
1261 !is_interface, "must match bc");
1262 ciKlass *holder = iter()->get_declared_method_holder();
1263 ciInstanceKlass *klass =
1264 ciEnv::get_instance_klass_for_declared_method_holder(holder);
1265
1266 // Find the receiver in the stack. We do this before
1267 // trying to inline because the inliner can only use
1268 // zero-checked values, not being able to perform the
1269 // check itself.
1270 SharkValue *receiver = NULL;
1271 if (!is_static) {
1272 receiver = xstack(dest_method->arg_size() - 1);
1273 check_null(receiver);
1274 }
1275
1276 // Try to improve non-direct calls
1277 bool call_is_virtual = is_virtual || is_interface;
1278 ciMethod *call_method = dest_method;
1279 if (call_is_virtual) {
1280 ciMethod *optimized_method = improve_virtual_call(
1281 target(), klass, dest_method, receiver->type());
1282 if (optimized_method) {
1283 call_method = optimized_method;
1284 call_is_virtual = false;
1285 }
1286 }
1287
1288 // Try to inline the call
1289 if (!call_is_virtual) {
1290 if (SharkInliner::attempt_inline(call_method, current_state()))
1291 return;
1292 }
1293
1294 // Find the method we are calling
1295 Value *callee;
1296 if (call_is_virtual) {
1297 if (is_virtual) {
1298 assert(klass->is_linked(), "scan_for_traps responsibility");
1299 int vtable_index = call_method->resolve_vtable_index(
1300 target()->holder(), klass);
1301 assert(vtable_index >= 0, "should be");
1302 callee = get_virtual_callee(receiver, vtable_index);
1303 }
1304 else {
1305 assert(is_interface, "should be");
1306 callee = get_interface_callee(receiver, call_method);
1307 }
1308 }
1309 else {
1310 callee = get_direct_callee(call_method);
1311 }
1312
1313 // Load the SharkEntry from the callee
1314 Value *base_pc = builder()->CreateValueOfStructEntry(
1315 callee, Method::from_interpreted_offset(),
1316 SharkType::intptr_type(),
1317 "base_pc");
1473 BasicBlock *merge1 = function()->CreateBlock("merge1");
1474 BasicBlock *merge2 = function()->CreateBlock("merge2");
1475
1476 enum InstanceCheckStates {
1477 IC_IS_NULL,
1478 IC_IS_INSTANCE,
1479 IC_NOT_INSTANCE,
1480 };
1481
1482 // Pop the object off the stack
1483 Value *object = pop()->jobject_value();
1484
1485 // Null objects aren't instances of anything
1486 builder()->CreateCondBr(
1487 builder()->CreateICmpEQ(object, LLVMValue::null()),
1488 merge2, not_null);
1489 BasicBlock *null_block = builder()->GetInsertBlock();
1490
1491 // Get the class we're checking against
1492 builder()->SetInsertPoint(not_null);
1493 Value *check_klass = builder()->CreateInlineOop(klass);
1494
1495 // Get the class of the object being tested
1496 Value *object_klass = builder()->CreateValueOfStructEntry(
1497 object, in_ByteSize(oopDesc::klass_offset_in_bytes()),
1498 SharkType::oop_type(),
1499 "object_klass");
1500
1501 // Perform the check
1502 builder()->CreateCondBr(
1503 builder()->CreateICmpEQ(check_klass, object_klass),
1504 is_instance, subtype_check);
1505
1506 builder()->SetInsertPoint(subtype_check);
1507 builder()->CreateCondBr(
1508 builder()->CreateICmpNE(
1509 builder()->CreateCall2(
1510 builder()->is_subtype_of(), check_klass, object_klass),
1511 LLVMValue::jbyte_constant(0)),
1512 is_instance, not_instance);
1513
1514 builder()->SetInsertPoint(is_instance);
1515 builder()->CreateBr(merge1);
1516
1517 builder()->SetInsertPoint(not_instance);
1518 builder()->CreateBr(merge1);
1519
1520 // First merge
1521 builder()->SetInsertPoint(merge1);
1522 PHINode *nonnull_result = builder()->CreatePHI(
1523 SharkType::jint_type(), "nonnull_result");
1524 nonnull_result->addIncoming(
1525 LLVMValue::jint_constant(IC_IS_INSTANCE), is_instance);
1526 nonnull_result->addIncoming(
1527 LLVMValue::jint_constant(IC_NOT_INSTANCE), not_instance);
1528 BasicBlock *nonnull_block = builder()->GetInsertBlock();
1529 builder()->CreateBr(merge2);
1530
1531 // Second merge
1532 builder()->SetInsertPoint(merge2);
1533 PHINode *result = builder()->CreatePHI(
1534 SharkType::jint_type(), "result");
1535 result->addIncoming(LLVMValue::jint_constant(IC_IS_NULL), null_block);
1536 result->addIncoming(nonnull_result, nonnull_block);
1537
1538 // Handle the result
1539 if (bc() == Bytecodes::_checkcast) {
1540 BasicBlock *failure = function()->CreateBlock("failure");
1541 BasicBlock *success = function()->CreateBlock("success");
1542
1543 builder()->CreateCondBr(
1544 builder()->CreateICmpNE(
1545 result, LLVMValue::jint_constant(IC_NOT_INSTANCE)),
1546 success, failure);
1547
1548 builder()->SetInsertPoint(failure);
1549 SharkState *saved_state = current_state()->copy();
1550
1551 call_vm(
1552 builder()->throw_ClassCastException(),
1553 builder()->CreateIntToPtr(
1554 LLVMValue::intptr_constant((intptr_t) __FILE__),
1681 builder()->CreateIntToPtr(
1682 LLVMValue::intptr_constant((intptr_t) Universe::heap()->end_addr()),
1683 PointerType::getUnqual(SharkType::intptr_type())),
1684 "end");
1685
1686 builder()->CreateBr(retry);
1687 builder()->SetInsertPoint(retry);
1688
1689 Value *old_top = builder()->CreateLoad(top_addr, "top");
1690 Value *new_top = builder()->CreateAdd(
1691 old_top, LLVMValue::intptr_constant(size_in_bytes));
1692
1693 builder()->CreateCondBr(
1694 builder()->CreateICmpULE(new_top, end),
1695 got_heap, slow_alloc_and_init);
1696
1697 builder()->SetInsertPoint(got_heap);
1698 heap_object = builder()->CreateIntToPtr(
1699 old_top, SharkType::oop_type(), "heap_object");
1700
1701 Value *check = builder()->CreateCmpxchgPtr(new_top, top_addr, old_top);
1702 builder()->CreateCondBr(
1703 builder()->CreateICmpEQ(old_top, check),
1704 initialize, retry);
1705
1706 // Initialize the object
1707 builder()->SetInsertPoint(initialize);
1708 if (tlab_object) {
1709 PHINode *phi = builder()->CreatePHI(
1710 SharkType::oop_type(), "fast_object");
1711 phi->addIncoming(tlab_object, got_tlab);
1712 phi->addIncoming(heap_object, got_heap);
1713 fast_object = phi;
1714 }
1715 else {
1716 fast_object = heap_object;
1717 }
1718
1719 builder()->CreateMemset(
1720 builder()->CreateBitCast(
1721 fast_object, PointerType::getUnqual(SharkType::jbyte_type())),
1722 LLVMValue::jbyte_constant(0),
1723 LLVMValue::jint_constant(size_in_bytes),
1724 LLVMValue::jint_constant(HeapWordSize));
1725
1726 Value *mark_addr = builder()->CreateAddressOfStructEntry(
1727 fast_object, in_ByteSize(oopDesc::mark_offset_in_bytes()),
1728 PointerType::getUnqual(SharkType::intptr_type()),
1729 "mark_addr");
1730
1731 Value *klass_addr = builder()->CreateAddressOfStructEntry(
1732 fast_object, in_ByteSize(oopDesc::klass_offset_in_bytes()),
1733 PointerType::getUnqual(SharkType::oop_type()),
1734 "klass_addr");
1735
1736 // Set the mark
1737 intptr_t mark;
1738 if (UseBiasedLocking) {
1739 Unimplemented();
1740 }
1741 else {
1742 mark = (intptr_t) markOopDesc::prototype();
1743 }
1744 builder()->CreateStore(LLVMValue::intptr_constant(mark), mark_addr);
1745
1746 // Set the class
1747 Value *rtklass = builder()->CreateInlineOop(klass);
1748 builder()->CreateStore(rtklass, klass_addr);
1749 got_fast = builder()->GetInsertBlock();
1750
1751 builder()->CreateBr(push_object);
1752 builder()->SetInsertPoint(slow_alloc_and_init);
1753 fast_state = current_state()->copy();
1754 }
1755
1756 // The slow path
1757 call_vm(
1758 builder()->new_instance(),
1759 LLVMValue::jint_constant(iter()->get_klass_index()),
1760 EX_CHECK_FULL);
1761 slow_object = get_vm_result();
1762 got_slow = builder()->GetInsertBlock();
1763
1764 // Push the object
1765 if (push_object) {
1766 builder()->CreateBr(push_object);
1767 builder()->SetInsertPoint(push_object);
1768 }
1769 if (fast_object) {
1770 PHINode *phi = builder()->CreatePHI(SharkType::oop_type(), "object");
1771 phi->addIncoming(fast_object, got_fast);
1772 phi->addIncoming(slow_object, got_slow);
1773 object = phi;
1774 current_state()->merge(fast_state, got_fast, got_slow);
1775 }
1776 else {
1777 object = slow_object;
1778 }
1779
1780 push(SharkValue::create_jobject(object, true));
1781 }
1782
1783 void SharkTopLevelBlock::do_newarray() {
1784 BasicType type = (BasicType) iter()->get_index();
1785
1786 call_vm(
1787 builder()->newarray(),
1788 LLVMValue::jint_constant(type),
1789 pop()->jint_value(),
1790 EX_CHECK_FULL);
1832 xstack(ndims - 1 - i)->jint_value(),
1833 builder()->CreateStructGEP(dimensions, i));
1834 }
1835
1836 call_vm(
1837 builder()->multianewarray(),
1838 LLVMValue::jint_constant(iter()->get_klass_index()),
1839 LLVMValue::jint_constant(ndims),
1840 builder()->CreateStructGEP(dimensions, 0),
1841 EX_CHECK_FULL);
1842
1843 // Now we can pop the dimensions off the stack
1844 for (int i = 0; i < ndims; i++)
1845 pop();
1846
1847 push(SharkValue::create_generic(array_klass, get_vm_result(), true));
1848 }
1849
1850 void SharkTopLevelBlock::acquire_method_lock() {
1851 Value *lockee;
1852 if (target()->is_static())
1853 lockee = builder()->CreateInlineOop(target()->holder()->java_mirror());
1854 else
1855 lockee = local(0)->jobject_value();
1856
1857 iter()->force_bci(start()); // for the decache in acquire_lock
1858 acquire_lock(lockee, EX_CHECK_NO_CATCH);
1859 }
1860
1861 void SharkTopLevelBlock::do_monitorenter() {
1862 SharkValue *lockee = pop();
1863 check_null(lockee);
1864 acquire_lock(lockee->jobject_value(), EX_CHECK_FULL);
1865 }
1866
1867 void SharkTopLevelBlock::do_monitorexit() {
1868 pop(); // don't need this (monitors are block structured)
1869 release_lock(EX_CHECK_NO_CATCH);
1870 }
1871
1872 void SharkTopLevelBlock::acquire_lock(Value *lockee, int exception_action) {
1873 BasicBlock *try_recursive = function()->CreateBlock("try_recursive");
1881 Value *monitor_object_addr = stack()->monitor_object_addr(monitor);
1882 Value *monitor_header_addr = stack()->monitor_header_addr(monitor);
1883
1884 // Store the object and mark the slot as live
1885 builder()->CreateStore(lockee, monitor_object_addr);
1886 set_num_monitors(monitor + 1);
1887
1888 // Try a simple lock
1889 Value *mark_addr = builder()->CreateAddressOfStructEntry(
1890 lockee, in_ByteSize(oopDesc::mark_offset_in_bytes()),
1891 PointerType::getUnqual(SharkType::intptr_type()),
1892 "mark_addr");
1893
1894 Value *mark = builder()->CreateLoad(mark_addr, "mark");
1895 Value *disp = builder()->CreateOr(
1896 mark, LLVMValue::intptr_constant(markOopDesc::unlocked_value), "disp");
1897 builder()->CreateStore(disp, monitor_header_addr);
1898
1899 Value *lock = builder()->CreatePtrToInt(
1900 monitor_header_addr, SharkType::intptr_type());
1901 Value *check = builder()->CreateCmpxchgPtr(lock, mark_addr, disp);
1902 builder()->CreateCondBr(
1903 builder()->CreateICmpEQ(disp, check),
1904 acquired_fast, try_recursive);
1905
1906 // Locking failed, but maybe this thread already owns it
1907 builder()->SetInsertPoint(try_recursive);
1908 Value *addr = builder()->CreateAnd(
1909 disp,
1910 LLVMValue::intptr_constant(~markOopDesc::lock_mask_in_place));
1911
1912 // NB we use the entire stack, but JavaThread::is_lock_owned()
1913 // uses a more limited range. I don't think it hurts though...
1914 Value *stack_limit = builder()->CreateValueOfStructEntry(
1915 thread(), Thread::stack_base_offset(),
1916 SharkType::intptr_type(),
1917 "stack_limit");
1918
1919 assert(sizeof(size_t) == sizeof(intptr_t), "should be");
1920 Value *stack_size = builder()->CreateValueOfStructEntry(
1921 thread(), Thread::stack_size_offset(),
1966
1967 // If it is recursive then we're already done
1968 Value *disp = builder()->CreateLoad(monitor_header_addr);
1969 builder()->CreateCondBr(
1970 builder()->CreateICmpEQ(disp, LLVMValue::intptr_constant(0)),
1971 released_fast, not_recursive);
1972
1973 // Try a simple unlock
1974 builder()->SetInsertPoint(not_recursive);
1975
1976 Value *lock = builder()->CreatePtrToInt(
1977 monitor_header_addr, SharkType::intptr_type());
1978
1979 Value *lockee = builder()->CreateLoad(monitor_object_addr);
1980
1981 Value *mark_addr = builder()->CreateAddressOfStructEntry(
1982 lockee, in_ByteSize(oopDesc::mark_offset_in_bytes()),
1983 PointerType::getUnqual(SharkType::intptr_type()),
1984 "mark_addr");
1985
1986 Value *check = builder()->CreateCmpxchgPtr(disp, mark_addr, lock);
1987 builder()->CreateCondBr(
1988 builder()->CreateICmpEQ(lock, check),
1989 released_fast, slow_path);
1990
1991 // Create an edge for the state merge
1992 builder()->SetInsertPoint(released_fast);
1993 SharkState *fast_state = current_state()->copy();
1994 builder()->CreateBr(lock_released);
1995
1996 // Need to drop into the runtime to release this one
1997 builder()->SetInsertPoint(slow_path);
1998 call_vm(builder()->monitorexit(), monitor_addr, exception_action);
1999 BasicBlock *released_slow = builder()->GetInsertBlock();
2000 builder()->CreateBr(lock_released);
2001
2002 // All done
2003 builder()->SetInsertPoint(lock_released);
2004 current_state()->merge(fast_state, released_fast, released_slow);
2005
2006 // The object slot is now dead
|
48 using namespace llvm;
49
50 void SharkTopLevelBlock::scan_for_traps() {
51 // If typeflow found a trap then don't scan past it
52 int limit_bci = ciblock()->has_trap() ? ciblock()->trap_bci() : limit();
53
54 // Scan the bytecode for traps that are always hit
55 iter()->reset_to_bci(start());
56 while (iter()->next_bci() < limit_bci) {
57 iter()->next();
58
59 ciField *field;
60 ciMethod *method;
61 ciInstanceKlass *klass;
62 bool will_link;
63 bool is_field;
64
65 switch (bc()) {
66 case Bytecodes::_ldc:
67 case Bytecodes::_ldc_w:
68 case Bytecodes::_ldc2_w:
69 if (!SharkConstant::for_ldc(iter())->is_loaded()) {
70 set_trap(
71 Deoptimization::make_trap_request(
72 Deoptimization::Reason_uninitialized,
73 Deoptimization::Action_reinterpret), bci());
74 return;
75 }
76 break;
77
78 case Bytecodes::_getfield:
79 case Bytecodes::_getstatic:
80 case Bytecodes::_putfield:
81 case Bytecodes::_putstatic:
82 field = iter()->get_field(will_link);
83 assert(will_link, "typeflow responsibility");
84 is_field = (bc() == Bytecodes::_getfield || bc() == Bytecodes::_putfield);
85
86 // If the bytecode does not match the field then bail out to
87 // the interpreter to throw an IncompatibleClassChangeError
88 if (is_field == field->is_static()) {
93 return;
94 }
95
96 // Bail out if we are trying to access a static variable
97 // before the class initializer has completed.
98 if (!is_field && !field->holder()->is_initialized()) {
99 if (!static_field_ok_in_clinit(field)) {
100 set_trap(
101 Deoptimization::make_trap_request(
102 Deoptimization::Reason_uninitialized,
103 Deoptimization::Action_reinterpret), bci());
104 return;
105 }
106 }
107 break;
108
109 case Bytecodes::_invokestatic:
110 case Bytecodes::_invokespecial:
111 case Bytecodes::_invokevirtual:
112 case Bytecodes::_invokeinterface:
113 ciSignature* sig;
114 method = iter()->get_method(will_link, &sig);
115 assert(will_link, "typeflow responsibility");
116
117 if (!method->holder()->is_linked()) {
118 set_trap(
119 Deoptimization::make_trap_request(
120 Deoptimization::Reason_uninitialized,
121 Deoptimization::Action_reinterpret), bci());
122 return;
123 }
124
125 if (bc() == Bytecodes::_invokevirtual) {
126 klass = ciEnv::get_instance_klass_for_declared_method_holder(
127 iter()->get_declared_method_holder());
128 if (!klass->is_linked()) {
129 set_trap(
130 Deoptimization::make_trap_request(
131 Deoptimization::Reason_uninitialized,
132 Deoptimization::Action_reinterpret), bci());
133 return;
134 }
547
548 // Install the catch-all handler, if present
549 if (has_catch_all) {
550 SharkTopLevelBlock* handler = this->exception(num_options);
551 assert(handler != NULL, "catch-all handler cannot be unloaded");
552
553 builder()->CreateBr(handler->entry_block());
554 handler->add_incoming(current_state());
555 return;
556 }
557 }
558
559 // No exception handler was found; unwind and return
560 handle_return(T_VOID, exception);
561 }
562
563 void SharkTopLevelBlock::marshal_exception_fast(int num_options) {
564 Value *exception_klass = builder()->CreateValueOfStructEntry(
565 xstack(0)->jobject_value(),
566 in_ByteSize(oopDesc::klass_offset_in_bytes()),
567 SharkType::klass_type(),
568 "exception_klass");
569
570 for (int i = 0; i < num_options; i++) {
571 Value *check_klass =
572 builder()->CreateInlineMetadata(exc_handler(i)->catch_klass(), SharkType::klass_type());
573
574 BasicBlock *not_exact = function()->CreateBlock("not_exact");
575 BasicBlock *not_subtype = function()->CreateBlock("not_subtype");
576
577 builder()->CreateCondBr(
578 builder()->CreateICmpEQ(check_klass, exception_klass),
579 handler_for_exception(i), not_exact);
580
581 builder()->SetInsertPoint(not_exact);
582 builder()->CreateCondBr(
583 builder()->CreateICmpNE(
584 builder()->CreateCall2(
585 builder()->is_subtype_of(), check_klass, exception_klass),
586 LLVMValue::jbyte_constant(0)),
587 handler_for_exception(i), not_subtype);
588
589 builder()->SetInsertPoint(not_subtype);
590 }
591 }
592
808 }
809
810 void SharkTopLevelBlock::do_arraylength() {
811 SharkValue *array = pop();
812 check_null(array);
813 Value *length = builder()->CreateArrayLength(array->jarray_value());
814 push(SharkValue::create_jint(length, false));
815 }
816
817 void SharkTopLevelBlock::do_aload(BasicType basic_type) {
818 SharkValue *index = pop();
819 SharkValue *array = pop();
820
821 check_null(array);
822 check_bounds(array, index);
823
824 Value *value = builder()->CreateLoad(
825 builder()->CreateArrayAddress(
826 array->jarray_value(), basic_type, index->jint_value()));
827
828 Type *stack_type = SharkType::to_stackType(basic_type);
829 if (value->getType() != stack_type)
830 value = builder()->CreateIntCast(value, stack_type, basic_type != T_CHAR);
831
832 switch (basic_type) {
833 case T_BYTE:
834 case T_CHAR:
835 case T_SHORT:
836 case T_INT:
837 push(SharkValue::create_jint(value, false));
838 break;
839
840 case T_LONG:
841 push(SharkValue::create_jlong(value, false));
842 break;
843
844 case T_FLOAT:
845 push(SharkValue::create_jfloat(value));
846 break;
847
848 case T_DOUBLE:
895 break;
896
897 case T_FLOAT:
898 value = svalue->jfloat_value();
899 break;
900
901 case T_DOUBLE:
902 value = svalue->jdouble_value();
903 break;
904
905 case T_OBJECT:
906 value = svalue->jobject_value();
907 // XXX assignability check
908 break;
909
910 default:
911 tty->print_cr("Unhandled type %s", type2name(basic_type));
912 ShouldNotReachHere();
913 }
914
915 Type *array_type = SharkType::to_arrayType(basic_type);
916 if (value->getType() != array_type)
917 value = builder()->CreateIntCast(value, array_type, basic_type != T_CHAR);
918
919 Value *addr = builder()->CreateArrayAddress(
920 array->jarray_value(), basic_type, index->jint_value(), "addr");
921
922 builder()->CreateStore(value, addr);
923
924 if (basic_type == T_OBJECT) // XXX or T_ARRAY?
925 builder()->CreateUpdateBarrierSet(oopDesc::bs(), addr);
926 }
927
928 void SharkTopLevelBlock::do_return(BasicType type) {
929 if (target()->intrinsic_id() == vmIntrinsics::_Object_init)
930 call_register_finalizer(local(0)->jobject_value());
931 maybe_add_safepoint();
932 handle_return(type, NULL);
933 }
934
935 void SharkTopLevelBlock::do_athrow() {
1087 // us dependent on that target method not getting overridden
1088 // by dynamic class loading.
1089 if (monomorphic_target != NULL) {
1090 dependencies()->assert_unique_concrete_method(
1091 actual_receiver, monomorphic_target);
1092 return monomorphic_target;
1093 }
1094
1095 // Because Opto distinguishes exact types from inexact ones
1096 // it can perform a further optimization to replace calls
1097 // with non-monomorphic targets if the receiver has an exact
1098 // type. We don't mark types this way, so we can't do this.
1099
1100 #endif // SHARK_CAN_DEOPTIMIZE_ANYWHERE
1101
1102 return NULL;
1103 }
1104
1105 Value *SharkTopLevelBlock::get_direct_callee(ciMethod* method) {
1106 return builder()->CreateBitCast(
1107 builder()->CreateInlineMetadata(method, SharkType::Method_type()),
1108 SharkType::Method_type(),
1109 "callee");
1110 }
1111
1112 Value *SharkTopLevelBlock::get_virtual_callee(SharkValue* receiver,
1113 int vtable_index) {
1114 Value *klass = builder()->CreateValueOfStructEntry(
1115 receiver->jobject_value(),
1116 in_ByteSize(oopDesc::klass_offset_in_bytes()),
1117 SharkType::oop_type(),
1118 "klass");
1119
1120 return builder()->CreateLoad(
1121 builder()->CreateArrayAddress(
1122 klass,
1123 SharkType::Method_type(),
1124 vtableEntry::size() * wordSize,
1125 in_ByteSize(InstanceKlass::vtable_start_offset() * wordSize),
1126 LLVMValue::intptr_constant(vtable_index)),
1127 "callee");
1128 }
1129
1130 Value* SharkTopLevelBlock::get_interface_callee(SharkValue *receiver,
1131 ciMethod* method) {
1132 BasicBlock *loop = function()->CreateBlock("loop");
1133 BasicBlock *got_null = function()->CreateBlock("got_null");
1134 BasicBlock *not_null = function()->CreateBlock("not_null");
1135 BasicBlock *next = function()->CreateBlock("next");
1136 BasicBlock *got_entry = function()->CreateBlock("got_entry");
1137
1138 // Locate the receiver's itable
1139 Value *object_klass = builder()->CreateValueOfStructEntry(
1140 receiver->jobject_value(), in_ByteSize(oopDesc::klass_offset_in_bytes()),
1141 SharkType::klass_type(),
1142 "object_klass");
1143
1144 Value *vtable_start = builder()->CreateAdd(
1145 builder()->CreatePtrToInt(object_klass, SharkType::intptr_type()),
1146 LLVMValue::intptr_constant(
1147 InstanceKlass::vtable_start_offset() * HeapWordSize),
1148 "vtable_start");
1149
1150 Value *vtable_length = builder()->CreateValueOfStructEntry(
1151 object_klass,
1152 in_ByteSize(InstanceKlass::vtable_length_offset() * HeapWordSize),
1153 SharkType::jint_type(),
1154 "vtable_length");
1155 vtable_length =
1156 builder()->CreateIntCast(vtable_length, SharkType::intptr_type(), false);
1157
1158 bool needs_aligning = HeapWordsPerLong > 1;
1159 Value *itable_start = builder()->CreateAdd(
1160 vtable_start,
1161 builder()->CreateShl(
1162 vtable_length,
1163 LLVMValue::intptr_constant(exact_log2(vtableEntry::size() * wordSize))),
1164 needs_aligning ? "" : "itable_start");
1165 if (needs_aligning) {
1166 itable_start = builder()->CreateAnd(
1167 builder()->CreateAdd(
1168 itable_start, LLVMValue::intptr_constant(BytesPerLong - 1)),
1169 LLVMValue::intptr_constant(~(BytesPerLong - 1)),
1170 "itable_start");
1171 }
1172
1173 // Locate this interface's entry in the table
1174 Value *iklass = builder()->CreateInlineMetadata(method->holder(), SharkType::klass_type());
1175 BasicBlock *loop_entry = builder()->GetInsertBlock();
1176 builder()->CreateBr(loop);
1177 builder()->SetInsertPoint(loop);
1178 PHINode *itable_entry_addr = builder()->CreatePHI(
1179 SharkType::intptr_type(), 0, "itable_entry_addr");
1180 itable_entry_addr->addIncoming(itable_start, loop_entry);
1181
1182 Value *itable_entry = builder()->CreateIntToPtr(
1183 itable_entry_addr, SharkType::itableOffsetEntry_type(), "itable_entry");
1184
1185 Value *itable_iklass = builder()->CreateValueOfStructEntry(
1186 itable_entry,
1187 in_ByteSize(itableOffsetEntry::interface_offset_in_bytes()),
1188 SharkType::klass_type(),
1189 "itable_iklass");
1190
1191 builder()->CreateCondBr(
1192 builder()->CreateICmpEQ(itable_iklass, LLVMValue::nullKlass()),
1193 got_null, not_null);
1194
1195 // A null entry means that the class doesn't implement the
1196 // interface, and wasn't the same as the class checked when
1197 // the interface was resolved.
1198 builder()->SetInsertPoint(got_null);
1199 builder()->CreateUnimplemented(__FILE__, __LINE__);
1200 builder()->CreateUnreachable();
1201
1202 builder()->SetInsertPoint(not_null);
1203 builder()->CreateCondBr(
1204 builder()->CreateICmpEQ(itable_iklass, iklass),
1205 got_entry, next);
1206
1207 builder()->SetInsertPoint(next);
1208 Value *next_entry = builder()->CreateAdd(
1209 itable_entry_addr,
1210 LLVMValue::intptr_constant(itableOffsetEntry::size() * wordSize));
1211 builder()->CreateBr(loop);
1212 itable_entry_addr->addIncoming(next_entry, next);
1216 Value *offset = builder()->CreateValueOfStructEntry(
1217 itable_entry,
1218 in_ByteSize(itableOffsetEntry::offset_offset_in_bytes()),
1219 SharkType::jint_type(),
1220 "offset");
1221 offset =
1222 builder()->CreateIntCast(offset, SharkType::intptr_type(), false);
1223
1224 return builder()->CreateLoad(
1225 builder()->CreateIntToPtr(
1226 builder()->CreateAdd(
1227 builder()->CreateAdd(
1228 builder()->CreateAdd(
1229 builder()->CreatePtrToInt(
1230 object_klass, SharkType::intptr_type()),
1231 offset),
1232 LLVMValue::intptr_constant(
1233 method->itable_index() * itableMethodEntry::size() * wordSize)),
1234 LLVMValue::intptr_constant(
1235 itableMethodEntry::method_offset_in_bytes())),
1236 PointerType::getUnqual(SharkType::Method_type())),
1237 "callee");
1238 }
1239
1240 void SharkTopLevelBlock::do_call() {
1241 // Set frequently used booleans
1242 bool is_static = bc() == Bytecodes::_invokestatic;
1243 bool is_virtual = bc() == Bytecodes::_invokevirtual;
1244 bool is_interface = bc() == Bytecodes::_invokeinterface;
1245
1246 // Find the method being called
1247 bool will_link;
1248 ciSignature* sig;
1249 ciMethod *dest_method = iter()->get_method(will_link, &sig);
1250
1251 assert(will_link, "typeflow responsibility");
1252 assert(dest_method->is_static() == is_static, "must match bc");
1253
1254 // Find the class of the method being called. Note
1255 // that the superclass check in the second assertion
1256 // is to cope with a hole in the spec that allows for
1257 // invokeinterface instructions where the resolved
1258 // method is a virtual method in java.lang.Object.
1259 // javac doesn't generate code like that, but there's
1260 // no reason a compliant Java compiler might not.
1261 ciInstanceKlass *holder_klass = dest_method->holder();
1262 assert(holder_klass->is_loaded(), "scan_for_traps responsibility");
1263 assert(holder_klass->is_interface() ||
1264 holder_klass->super() == NULL ||
1265 !is_interface, "must match bc");
1266
1267 bool is_forced_virtual = is_interface && holder_klass == java_lang_Object_klass();
1268
1269 ciKlass *holder = iter()->get_declared_method_holder();
1270 ciInstanceKlass *klass =
1271 ciEnv::get_instance_klass_for_declared_method_holder(holder);
1272
1273 if (is_forced_virtual) {
1274 klass = java_lang_Object_klass();
1275 }
1276
1277 // Find the receiver in the stack. We do this before
1278 // trying to inline because the inliner can only use
1279 // zero-checked values, not being able to perform the
1280 // check itself.
1281 SharkValue *receiver = NULL;
1282 if (!is_static) {
1283 receiver = xstack(dest_method->arg_size() - 1);
1284 check_null(receiver);
1285 }
1286
1287 // Try to improve non-direct calls
1288 bool call_is_virtual = is_virtual || is_interface;
1289 ciMethod *call_method = dest_method;
1290 if (call_is_virtual) {
1291 ciMethod *optimized_method = improve_virtual_call(
1292 target(), klass, dest_method, receiver->type());
1293 if (optimized_method) {
1294 call_method = optimized_method;
1295 call_is_virtual = false;
1296 }
1297 }
1298
1299 // Try to inline the call
1300 if (!call_is_virtual) {
1301 if (SharkInliner::attempt_inline(call_method, current_state()))
1302 return;
1303 }
1304
1305 // Find the method we are calling
1306 Value *callee;
1307 if (call_is_virtual) {
1308 if (is_virtual || is_forced_virtual) {
1309 assert(klass->is_linked(), "scan_for_traps responsibility");
1310 int vtable_index = call_method->resolve_vtable_index(
1311 target()->holder(), klass);
1312 assert(vtable_index >= 0, "should be");
1313 callee = get_virtual_callee(receiver, vtable_index);
1314 }
1315 else {
1316 assert(is_interface, "should be");
1317 callee = get_interface_callee(receiver, call_method);
1318 }
1319 }
1320 else {
1321 callee = get_direct_callee(call_method);
1322 }
1323
1324 // Load the SharkEntry from the callee
1325 Value *base_pc = builder()->CreateValueOfStructEntry(
1326 callee, Method::from_interpreted_offset(),
1327 SharkType::intptr_type(),
1328 "base_pc");
1484 BasicBlock *merge1 = function()->CreateBlock("merge1");
1485 BasicBlock *merge2 = function()->CreateBlock("merge2");
1486
1487 enum InstanceCheckStates {
1488 IC_IS_NULL,
1489 IC_IS_INSTANCE,
1490 IC_NOT_INSTANCE,
1491 };
1492
1493 // Pop the object off the stack
1494 Value *object = pop()->jobject_value();
1495
1496 // Null objects aren't instances of anything
1497 builder()->CreateCondBr(
1498 builder()->CreateICmpEQ(object, LLVMValue::null()),
1499 merge2, not_null);
1500 BasicBlock *null_block = builder()->GetInsertBlock();
1501
1502 // Get the class we're checking against
1503 builder()->SetInsertPoint(not_null);
1504 Value *check_klass = builder()->CreateInlineMetadata(klass, SharkType::klass_type());
1505
1506 // Get the class of the object being tested
1507 Value *object_klass = builder()->CreateValueOfStructEntry(
1508 object, in_ByteSize(oopDesc::klass_offset_in_bytes()),
1509 SharkType::klass_type(),
1510 "object_klass");
1511
1512 // Perform the check
1513 builder()->CreateCondBr(
1514 builder()->CreateICmpEQ(check_klass, object_klass),
1515 is_instance, subtype_check);
1516
1517 builder()->SetInsertPoint(subtype_check);
1518 builder()->CreateCondBr(
1519 builder()->CreateICmpNE(
1520 builder()->CreateCall2(
1521 builder()->is_subtype_of(), check_klass, object_klass),
1522 LLVMValue::jbyte_constant(0)),
1523 is_instance, not_instance);
1524
1525 builder()->SetInsertPoint(is_instance);
1526 builder()->CreateBr(merge1);
1527
1528 builder()->SetInsertPoint(not_instance);
1529 builder()->CreateBr(merge1);
1530
1531 // First merge
1532 builder()->SetInsertPoint(merge1);
1533 PHINode *nonnull_result = builder()->CreatePHI(
1534 SharkType::jint_type(), 0, "nonnull_result");
1535 nonnull_result->addIncoming(
1536 LLVMValue::jint_constant(IC_IS_INSTANCE), is_instance);
1537 nonnull_result->addIncoming(
1538 LLVMValue::jint_constant(IC_NOT_INSTANCE), not_instance);
1539 BasicBlock *nonnull_block = builder()->GetInsertBlock();
1540 builder()->CreateBr(merge2);
1541
1542 // Second merge
1543 builder()->SetInsertPoint(merge2);
1544 PHINode *result = builder()->CreatePHI(
1545 SharkType::jint_type(), 0, "result");
1546 result->addIncoming(LLVMValue::jint_constant(IC_IS_NULL), null_block);
1547 result->addIncoming(nonnull_result, nonnull_block);
1548
1549 // Handle the result
1550 if (bc() == Bytecodes::_checkcast) {
1551 BasicBlock *failure = function()->CreateBlock("failure");
1552 BasicBlock *success = function()->CreateBlock("success");
1553
1554 builder()->CreateCondBr(
1555 builder()->CreateICmpNE(
1556 result, LLVMValue::jint_constant(IC_NOT_INSTANCE)),
1557 success, failure);
1558
1559 builder()->SetInsertPoint(failure);
1560 SharkState *saved_state = current_state()->copy();
1561
1562 call_vm(
1563 builder()->throw_ClassCastException(),
1564 builder()->CreateIntToPtr(
1565 LLVMValue::intptr_constant((intptr_t) __FILE__),
1692 builder()->CreateIntToPtr(
1693 LLVMValue::intptr_constant((intptr_t) Universe::heap()->end_addr()),
1694 PointerType::getUnqual(SharkType::intptr_type())),
1695 "end");
1696
1697 builder()->CreateBr(retry);
1698 builder()->SetInsertPoint(retry);
1699
1700 Value *old_top = builder()->CreateLoad(top_addr, "top");
1701 Value *new_top = builder()->CreateAdd(
1702 old_top, LLVMValue::intptr_constant(size_in_bytes));
1703
1704 builder()->CreateCondBr(
1705 builder()->CreateICmpULE(new_top, end),
1706 got_heap, slow_alloc_and_init);
1707
1708 builder()->SetInsertPoint(got_heap);
1709 heap_object = builder()->CreateIntToPtr(
1710 old_top, SharkType::oop_type(), "heap_object");
1711
1712 Value *check = builder()->CreateAtomicCmpXchg(top_addr, old_top, new_top, llvm::SequentiallyConsistent);
1713 builder()->CreateCondBr(
1714 builder()->CreateICmpEQ(old_top, check),
1715 initialize, retry);
1716
1717 // Initialize the object
1718 builder()->SetInsertPoint(initialize);
1719 if (tlab_object) {
1720 PHINode *phi = builder()->CreatePHI(
1721 SharkType::oop_type(), 0, "fast_object");
1722 phi->addIncoming(tlab_object, got_tlab);
1723 phi->addIncoming(heap_object, got_heap);
1724 fast_object = phi;
1725 }
1726 else {
1727 fast_object = heap_object;
1728 }
1729
1730 builder()->CreateMemset(
1731 builder()->CreateBitCast(
1732 fast_object, PointerType::getUnqual(SharkType::jbyte_type())),
1733 LLVMValue::jbyte_constant(0),
1734 LLVMValue::jint_constant(size_in_bytes),
1735 LLVMValue::jint_constant(HeapWordSize));
1736
1737 Value *mark_addr = builder()->CreateAddressOfStructEntry(
1738 fast_object, in_ByteSize(oopDesc::mark_offset_in_bytes()),
1739 PointerType::getUnqual(SharkType::intptr_type()),
1740 "mark_addr");
1741
1742 Value *klass_addr = builder()->CreateAddressOfStructEntry(
1743 fast_object, in_ByteSize(oopDesc::klass_offset_in_bytes()),
1744 PointerType::getUnqual(SharkType::klass_type()),
1745 "klass_addr");
1746
1747 // Set the mark
1748 intptr_t mark;
1749 if (UseBiasedLocking) {
1750 Unimplemented();
1751 }
1752 else {
1753 mark = (intptr_t) markOopDesc::prototype();
1754 }
1755 builder()->CreateStore(LLVMValue::intptr_constant(mark), mark_addr);
1756
1757 // Set the class
1758 Value *rtklass = builder()->CreateInlineMetadata(klass, SharkType::klass_type());
1759 builder()->CreateStore(rtklass, klass_addr);
1760 got_fast = builder()->GetInsertBlock();
1761
1762 builder()->CreateBr(push_object);
1763 builder()->SetInsertPoint(slow_alloc_and_init);
1764 fast_state = current_state()->copy();
1765 }
1766
1767 // The slow path
1768 call_vm(
1769 builder()->new_instance(),
1770 LLVMValue::jint_constant(iter()->get_klass_index()),
1771 EX_CHECK_FULL);
1772 slow_object = get_vm_result();
1773 got_slow = builder()->GetInsertBlock();
1774
1775 // Push the object
1776 if (push_object) {
1777 builder()->CreateBr(push_object);
1778 builder()->SetInsertPoint(push_object);
1779 }
1780 if (fast_object) {
1781 PHINode *phi = builder()->CreatePHI(SharkType::oop_type(), 0, "object");
1782 phi->addIncoming(fast_object, got_fast);
1783 phi->addIncoming(slow_object, got_slow);
1784 object = phi;
1785 current_state()->merge(fast_state, got_fast, got_slow);
1786 }
1787 else {
1788 object = slow_object;
1789 }
1790
1791 push(SharkValue::create_jobject(object, true));
1792 }
1793
1794 void SharkTopLevelBlock::do_newarray() {
1795 BasicType type = (BasicType) iter()->get_index();
1796
1797 call_vm(
1798 builder()->newarray(),
1799 LLVMValue::jint_constant(type),
1800 pop()->jint_value(),
1801 EX_CHECK_FULL);
1843 xstack(ndims - 1 - i)->jint_value(),
1844 builder()->CreateStructGEP(dimensions, i));
1845 }
1846
1847 call_vm(
1848 builder()->multianewarray(),
1849 LLVMValue::jint_constant(iter()->get_klass_index()),
1850 LLVMValue::jint_constant(ndims),
1851 builder()->CreateStructGEP(dimensions, 0),
1852 EX_CHECK_FULL);
1853
1854 // Now we can pop the dimensions off the stack
1855 for (int i = 0; i < ndims; i++)
1856 pop();
1857
1858 push(SharkValue::create_generic(array_klass, get_vm_result(), true));
1859 }
1860
1861 void SharkTopLevelBlock::acquire_method_lock() {
1862 Value *lockee;
1863 if (target()->is_static()) {
1864 lockee = builder()->CreateInlineOop(target()->holder()->java_mirror());
1865 }
1866 else
1867 lockee = local(0)->jobject_value();
1868
1869 iter()->force_bci(start()); // for the decache in acquire_lock
1870 acquire_lock(lockee, EX_CHECK_NO_CATCH);
1871 }
1872
1873 void SharkTopLevelBlock::do_monitorenter() {
1874 SharkValue *lockee = pop();
1875 check_null(lockee);
1876 acquire_lock(lockee->jobject_value(), EX_CHECK_FULL);
1877 }
1878
1879 void SharkTopLevelBlock::do_monitorexit() {
1880 pop(); // don't need this (monitors are block structured)
1881 release_lock(EX_CHECK_NO_CATCH);
1882 }
1883
1884 void SharkTopLevelBlock::acquire_lock(Value *lockee, int exception_action) {
1885 BasicBlock *try_recursive = function()->CreateBlock("try_recursive");
1893 Value *monitor_object_addr = stack()->monitor_object_addr(monitor);
1894 Value *monitor_header_addr = stack()->monitor_header_addr(monitor);
1895
1896 // Store the object and mark the slot as live
1897 builder()->CreateStore(lockee, monitor_object_addr);
1898 set_num_monitors(monitor + 1);
1899
1900 // Try a simple lock
1901 Value *mark_addr = builder()->CreateAddressOfStructEntry(
1902 lockee, in_ByteSize(oopDesc::mark_offset_in_bytes()),
1903 PointerType::getUnqual(SharkType::intptr_type()),
1904 "mark_addr");
1905
1906 Value *mark = builder()->CreateLoad(mark_addr, "mark");
1907 Value *disp = builder()->CreateOr(
1908 mark, LLVMValue::intptr_constant(markOopDesc::unlocked_value), "disp");
1909 builder()->CreateStore(disp, monitor_header_addr);
1910
1911 Value *lock = builder()->CreatePtrToInt(
1912 monitor_header_addr, SharkType::intptr_type());
1913 Value *check = builder()->CreateAtomicCmpXchg(mark_addr, disp, lock, llvm::Acquire);
1914 builder()->CreateCondBr(
1915 builder()->CreateICmpEQ(disp, check),
1916 acquired_fast, try_recursive);
1917
1918 // Locking failed, but maybe this thread already owns it
1919 builder()->SetInsertPoint(try_recursive);
1920 Value *addr = builder()->CreateAnd(
1921 disp,
1922 LLVMValue::intptr_constant(~markOopDesc::lock_mask_in_place));
1923
1924 // NB we use the entire stack, but JavaThread::is_lock_owned()
1925 // uses a more limited range. I don't think it hurts though...
1926 Value *stack_limit = builder()->CreateValueOfStructEntry(
1927 thread(), Thread::stack_base_offset(),
1928 SharkType::intptr_type(),
1929 "stack_limit");
1930
1931 assert(sizeof(size_t) == sizeof(intptr_t), "should be");
1932 Value *stack_size = builder()->CreateValueOfStructEntry(
1933 thread(), Thread::stack_size_offset(),
1978
1979 // If it is recursive then we're already done
1980 Value *disp = builder()->CreateLoad(monitor_header_addr);
1981 builder()->CreateCondBr(
1982 builder()->CreateICmpEQ(disp, LLVMValue::intptr_constant(0)),
1983 released_fast, not_recursive);
1984
1985 // Try a simple unlock
1986 builder()->SetInsertPoint(not_recursive);
1987
1988 Value *lock = builder()->CreatePtrToInt(
1989 monitor_header_addr, SharkType::intptr_type());
1990
1991 Value *lockee = builder()->CreateLoad(monitor_object_addr);
1992
1993 Value *mark_addr = builder()->CreateAddressOfStructEntry(
1994 lockee, in_ByteSize(oopDesc::mark_offset_in_bytes()),
1995 PointerType::getUnqual(SharkType::intptr_type()),
1996 "mark_addr");
1997
1998 Value *check = builder()->CreateAtomicCmpXchg(mark_addr, lock, disp, llvm::Release);
1999 builder()->CreateCondBr(
2000 builder()->CreateICmpEQ(lock, check),
2001 released_fast, slow_path);
2002
2003 // Create an edge for the state merge
2004 builder()->SetInsertPoint(released_fast);
2005 SharkState *fast_state = current_state()->copy();
2006 builder()->CreateBr(lock_released);
2007
2008 // Need to drop into the runtime to release this one
2009 builder()->SetInsertPoint(slow_path);
2010 call_vm(builder()->monitorexit(), monitor_addr, exception_action);
2011 BasicBlock *released_slow = builder()->GetInsertBlock();
2012 builder()->CreateBr(lock_released);
2013
2014 // All done
2015 builder()->SetInsertPoint(lock_released);
2016 current_state()->merge(fast_state, released_fast, released_slow);
2017
2018 // The object slot is now dead
|