src/share/vm/shark/sharkNativeWrapper.cpp

Tue, 18 Jun 2013 12:31:07 -0700

author
johnc
date
Tue, 18 Jun 2013 12:31:07 -0700
changeset 5277
01522ca68fc7
parent 4314
2cd5e15048e6
child 6876
710a3c8b516e
child 9669
32bc598624bd
permissions
-rw-r--r--

8015237: Parallelize string table scanning during strong root processing
Summary: Parallelize the scanning of the intern string table by having each GC worker claim a given number of buckets. Changes were also reviewed by Per Liden <per.liden@oracle.com>.
Reviewed-by: tschatzl, stefank, twisti

twisti@2047 1 /*
trims@2708 2 * Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved.
twisti@2047 3 * Copyright 2009, 2010 Red Hat, Inc.
twisti@2047 4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
twisti@2047 5 *
twisti@2047 6 * This code is free software; you can redistribute it and/or modify it
twisti@2047 7 * under the terms of the GNU General Public License version 2 only, as
twisti@2047 8 * published by the Free Software Foundation.
twisti@2047 9 *
twisti@2047 10 * This code is distributed in the hope that it will be useful, but WITHOUT
twisti@2047 11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
twisti@2047 12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
twisti@2047 13 * version 2 for more details (a copy is included in the LICENSE file that
twisti@2047 14 * accompanied this code).
twisti@2047 15 *
twisti@2047 16 * You should have received a copy of the GNU General Public License version
twisti@2047 17 * 2 along with this work; if not, write to the Free Software Foundation,
twisti@2047 18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
twisti@2047 19 *
twisti@2047 20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
twisti@2047 21 * or visit www.oracle.com if you need additional information or have any
twisti@2047 22 * questions.
twisti@2047 23 *
twisti@2047 24 */
twisti@2047 25
stefank@2314 26 #include "precompiled.hpp"
stefank@2314 27 #include "shark/llvmHeaders.hpp"
stefank@2314 28 #include "shark/sharkNativeWrapper.hpp"
stefank@2314 29 #include "shark/sharkType.hpp"
twisti@2047 30
twisti@2047 31 using namespace llvm;
twisti@2047 32
twisti@2047 33 void SharkNativeWrapper::initialize(const char *name) {
twisti@2047 34 // Create the function
twisti@2047 35 _function = Function::Create(
twisti@2047 36 SharkType::entry_point_type(),
twisti@2047 37 GlobalVariable::InternalLinkage,
twisti@2047 38 name);
twisti@2047 39
twisti@2047 40 // Get our arguments
twisti@2047 41 Function::arg_iterator ai = function()->arg_begin();
twisti@2047 42 Argument *method = ai++;
twisti@2047 43 method->setName("method");
twisti@2047 44 Argument *base_pc = ai++;
twisti@2047 45 base_pc->setName("base_pc");
twisti@2047 46 code_buffer()->set_base_pc(base_pc);
twisti@2047 47 Argument *thread = ai++;
twisti@2047 48 thread->setName("thread");
twisti@2047 49 set_thread(thread);
twisti@2047 50
twisti@2047 51 // Create and push our stack frame
twisti@2047 52 builder()->SetInsertPoint(CreateBlock());
twisti@2047 53 _stack = SharkStack::CreateBuildAndPushFrame(this, method);
twisti@2047 54 NOT_PRODUCT(method = NULL);
twisti@2047 55
twisti@2047 56 // Create the oopmap. We use the one oopmap for every call site in
twisti@2047 57 // the wrapper, which results in the odd mild inefficiency but is a
twisti@2047 58 // damn sight easier to code.
twisti@2047 59 OopMap *oopmap = new OopMap(
twisti@2047 60 SharkStack::oopmap_slot_munge(stack()->oopmap_frame_size()),
twisti@2047 61 SharkStack::oopmap_slot_munge(arg_size()));
twisti@2047 62
twisti@2047 63 // Set up the oop_tmp slot if required:
twisti@2047 64 // - For static methods we use it to handlize the class argument
twisti@2047 65 // for the call, and to protect the same during slow path locks
twisti@2047 66 // (if synchronized).
twisti@2047 67 // - For methods returning oops, we use it to protect the return
twisti@2047 68 // value across safepoints or slow path unlocking.
twisti@2047 69 if (is_static() || is_returning_oop()) {
twisti@2047 70 _oop_tmp_slot = stack()->slot_addr(
twisti@2047 71 stack()->oop_tmp_slot_offset(),
twisti@2047 72 SharkType::oop_type(),
twisti@2047 73 "oop_tmp_slot");
twisti@2047 74
twisti@2047 75 oopmap->set_oop(SharkStack::slot2reg(stack()->oop_tmp_slot_offset()));
twisti@2047 76 }
twisti@2047 77
twisti@2047 78 // Set up the monitor slot, for synchronized methods
twisti@2047 79 if (is_synchronized()) {
twisti@2047 80 Unimplemented();
twisti@2047 81 _lock_slot_offset = 23;
twisti@2047 82 }
twisti@2047 83
twisti@2047 84 // Start building the argument list
twisti@4314 85 std::vector<Type*> param_types;
twisti@2047 86 std::vector<Value*> param_values;
twisti@4314 87 PointerType *box_type = PointerType::getUnqual(SharkType::oop_type());
twisti@2047 88
twisti@2047 89 // First argument is the JNIEnv
twisti@2047 90 param_types.push_back(SharkType::jniEnv_type());
twisti@2047 91 param_values.push_back(
twisti@2047 92 builder()->CreateAddressOfStructEntry(
twisti@2047 93 thread,
twisti@2047 94 JavaThread::jni_environment_offset(),
twisti@2047 95 SharkType::jniEnv_type(),
twisti@2047 96 "jni_environment"));
twisti@2047 97
twisti@2047 98 // For static methods, the second argument is the class
twisti@2047 99 if (is_static()) {
twisti@2047 100 builder()->CreateStore(
twisti@2047 101 builder()->CreateInlineOop(
twisti@2047 102 JNIHandles::make_local(
never@2658 103 target()->method_holder()->java_mirror())),
twisti@2047 104 oop_tmp_slot());
twisti@2047 105
twisti@2047 106 param_types.push_back(box_type);
twisti@2047 107 param_values.push_back(oop_tmp_slot());
twisti@2047 108
twisti@2047 109 _receiver_slot_offset = stack()->oop_tmp_slot_offset();
twisti@2047 110 }
twisti@2047 111 else if (is_returning_oop()) {
twisti@2047 112 // The oop_tmp slot is registered in the oopmap,
twisti@2047 113 // so we need to clear it. This is one of the
twisti@2047 114 // mild inefficiencies I mentioned earlier.
twisti@2047 115 builder()->CreateStore(LLVMValue::null(), oop_tmp_slot());
twisti@2047 116 }
twisti@2047 117
twisti@2047 118 // Parse the arguments
twisti@2047 119 for (int i = 0; i < arg_size(); i++) {
twisti@2047 120 int slot_offset = stack()->locals_slots_offset() + arg_size() - 1 - i;
twisti@2047 121 int adjusted_offset = slot_offset;
twisti@2047 122 BasicBlock *null, *not_null, *merge;
twisti@2047 123 Value *box;
twisti@2047 124 PHINode *phi;
twisti@2047 125
twisti@2047 126 switch (arg_type(i)) {
twisti@2047 127 case T_VOID:
twisti@2047 128 break;
twisti@2047 129
twisti@2047 130 case T_OBJECT:
twisti@2047 131 case T_ARRAY:
twisti@2047 132 null = CreateBlock("null");
twisti@2047 133 not_null = CreateBlock("not_null");
twisti@2047 134 merge = CreateBlock("merge");
twisti@2047 135
twisti@2047 136 box = stack()->slot_addr(slot_offset, SharkType::oop_type());
twisti@2047 137 builder()->CreateCondBr(
twisti@2047 138 builder()->CreateICmp(
twisti@2047 139 ICmpInst::ICMP_EQ,
twisti@2047 140 builder()->CreateLoad(box),
twisti@2047 141 LLVMValue::null()),
twisti@2047 142 null, not_null);
twisti@2047 143
twisti@2047 144 builder()->SetInsertPoint(null);
twisti@2047 145 builder()->CreateBr(merge);
twisti@2047 146
twisti@2047 147 builder()->SetInsertPoint(not_null);
twisti@2047 148 builder()->CreateBr(merge);
twisti@2047 149
twisti@2047 150 builder()->SetInsertPoint(merge);
twisti@4314 151 phi = builder()->CreatePHI(box_type, 0, "boxed_object");
twisti@2047 152 phi->addIncoming(ConstantPointerNull::get(box_type), null);
twisti@2047 153 phi->addIncoming(box, not_null);
twisti@2047 154 box = phi;
twisti@2047 155
twisti@2047 156 param_types.push_back(box_type);
twisti@2047 157 param_values.push_back(box);
twisti@2047 158
twisti@2047 159 oopmap->set_oop(SharkStack::slot2reg(slot_offset));
twisti@2047 160
twisti@2047 161 if (i == 0 && !is_static())
twisti@2047 162 _receiver_slot_offset = slot_offset;
twisti@2047 163
twisti@2047 164 break;
twisti@2047 165
twisti@2047 166 case T_LONG:
twisti@2047 167 case T_DOUBLE:
twisti@2047 168 adjusted_offset--;
twisti@2047 169 // fall through
twisti@2047 170
twisti@2047 171 default:
twisti@4314 172 Type *param_type = SharkType::to_stackType(arg_type(i));
twisti@2047 173
twisti@2047 174 param_types.push_back(param_type);
twisti@2047 175 param_values.push_back(
twisti@2047 176 builder()->CreateLoad(stack()->slot_addr(adjusted_offset, param_type)));
twisti@2047 177 }
twisti@2047 178 }
twisti@2047 179
twisti@2047 180 // The oopmap is now complete, and everything is written
twisti@2047 181 // into the frame except the PC.
twisti@2047 182 int pc_offset = code_buffer()->create_unique_offset();
twisti@2047 183
twisti@2047 184 _oop_maps = new OopMapSet();
twisti@2047 185 oop_maps()->add_gc_map(pc_offset, oopmap);
twisti@2047 186
twisti@2047 187 builder()->CreateStore(
twisti@2047 188 builder()->code_buffer_address(pc_offset),
twisti@2047 189 stack()->slot_addr(stack()->pc_slot_offset()));
twisti@2047 190
twisti@2047 191 // Set up the Java frame anchor
twisti@2047 192 stack()->CreateSetLastJavaFrame();
twisti@2047 193
twisti@2047 194 // Lock if necessary
twisti@2047 195 if (is_synchronized())
twisti@2047 196 Unimplemented();
twisti@2047 197
twisti@2047 198 // Change the thread state to _thread_in_native
twisti@2047 199 CreateSetThreadState(_thread_in_native);
twisti@2047 200
twisti@2047 201 // Make the call
twisti@2047 202 BasicType result_type = target()->result_type();
twisti@4314 203 Type* return_type;
twisti@2047 204 if (result_type == T_VOID)
twisti@2047 205 return_type = SharkType::void_type();
twisti@2047 206 else if (is_returning_oop())
twisti@2047 207 return_type = box_type;
twisti@2047 208 else
twisti@2047 209 return_type = SharkType::to_arrayType(result_type);
twisti@2047 210 Value* native_function = builder()->CreateIntToPtr(
twisti@2047 211 LLVMValue::intptr_constant((intptr_t) target()->native_function()),
twisti@2047 212 PointerType::getUnqual(
twisti@2047 213 FunctionType::get(return_type, param_types, false)));
twisti@2047 214 Value *result = builder()->CreateCall(
twisti@4314 215 native_function, llvm::makeArrayRef(param_values));
twisti@2047 216
twisti@2047 217 // Start the transition back to _thread_in_Java
twisti@2047 218 CreateSetThreadState(_thread_in_native_trans);
twisti@2047 219
twisti@2047 220 // Make sure new state is visible in the GC thread
twisti@2047 221 if (os::is_MP()) {
twisti@2047 222 if (UseMembar)
twisti@4314 223 builder()->CreateFence(llvm::SequentiallyConsistent, llvm::CrossThread);
twisti@2047 224 else
twisti@2047 225 CreateWriteMemorySerializePage();
twisti@2047 226 }
twisti@2047 227
twisti@2047 228 // Handle safepoint operations, pending suspend requests,
twisti@2047 229 // and pending asynchronous exceptions.
twisti@2047 230 BasicBlock *check_thread = CreateBlock("check_thread");
twisti@2047 231 BasicBlock *do_safepoint = CreateBlock("do_safepoint");
twisti@2047 232 BasicBlock *safepointed = CreateBlock("safepointed");
twisti@2047 233
twisti@2047 234 Value *global_state = builder()->CreateLoad(
twisti@2047 235 builder()->CreateIntToPtr(
twisti@2047 236 LLVMValue::intptr_constant(
twisti@2047 237 (intptr_t) SafepointSynchronize::address_of_state()),
twisti@2047 238 PointerType::getUnqual(SharkType::jint_type())),
twisti@2047 239 "global_state");
twisti@2047 240
twisti@2047 241 builder()->CreateCondBr(
twisti@2047 242 builder()->CreateICmpNE(
twisti@2047 243 global_state,
twisti@2047 244 LLVMValue::jint_constant(SafepointSynchronize::_not_synchronized)),
twisti@2047 245 do_safepoint, check_thread);
twisti@2047 246
twisti@2047 247 builder()->SetInsertPoint(check_thread);
twisti@2047 248 Value *thread_state = builder()->CreateValueOfStructEntry(
twisti@2047 249 thread,
twisti@2047 250 JavaThread::suspend_flags_offset(),
twisti@2047 251 SharkType::jint_type(),
twisti@2047 252 "thread_state");
twisti@2047 253
twisti@2047 254 builder()->CreateCondBr(
twisti@2047 255 builder()->CreateICmpNE(
twisti@2047 256 thread_state,
twisti@2047 257 LLVMValue::jint_constant(0)),
twisti@2047 258 do_safepoint, safepointed);
twisti@2047 259
twisti@2047 260 builder()->SetInsertPoint(do_safepoint);
twisti@2047 261 builder()->CreateCall(
twisti@2047 262 builder()->check_special_condition_for_native_trans(), thread);
twisti@2047 263 builder()->CreateBr(safepointed);
twisti@2047 264
twisti@2047 265 // Finally we can change the thread state to _thread_in_Java
twisti@2047 266 builder()->SetInsertPoint(safepointed);
twisti@2047 267 CreateSetThreadState(_thread_in_Java);
twisti@2047 268
twisti@2047 269 // Clear the frame anchor
twisti@2047 270 stack()->CreateResetLastJavaFrame();
twisti@2047 271
twisti@2047 272 // If there is a pending exception then we can just unwind and
twisti@2047 273 // return. It seems totally wrong that unlocking is skipped here
twisti@2047 274 // but apparently the template interpreter does this so we do too.
twisti@2047 275 BasicBlock *exception = CreateBlock("exception");
twisti@2047 276 BasicBlock *no_exception = CreateBlock("no_exception");
twisti@2047 277
twisti@2047 278 builder()->CreateCondBr(
twisti@2047 279 builder()->CreateICmpEQ(
twisti@2047 280 CreateLoadPendingException(),
twisti@2047 281 LLVMValue::null()),
twisti@2047 282 no_exception, exception);
twisti@2047 283
twisti@2047 284 builder()->SetInsertPoint(exception);
twisti@2047 285 CreateResetHandleBlock();
twisti@2047 286 stack()->CreatePopFrame(0);
twisti@2047 287 builder()->CreateRet(LLVMValue::jint_constant(0));
twisti@2047 288
twisti@2047 289 builder()->SetInsertPoint(no_exception);
twisti@2047 290
twisti@2047 291 // If the result was an oop then unbox it before
twisti@2047 292 // releasing the handle it might be protected by
twisti@2047 293 if (is_returning_oop()) {
twisti@2047 294 BasicBlock *null = builder()->GetInsertBlock();
twisti@2047 295 BasicBlock *not_null = CreateBlock("not_null");
twisti@2047 296 BasicBlock *merge = CreateBlock("merge");
twisti@2047 297
twisti@2047 298 builder()->CreateCondBr(
twisti@2047 299 builder()->CreateICmpNE(result, ConstantPointerNull::get(box_type)),
twisti@2047 300 not_null, merge);
twisti@2047 301
twisti@2047 302 builder()->SetInsertPoint(not_null);
twisti@2047 303 Value *unboxed_result = builder()->CreateLoad(result);
twisti@2047 304 builder()->CreateBr(merge);
twisti@2047 305
twisti@2047 306 builder()->SetInsertPoint(merge);
twisti@4314 307 PHINode *phi = builder()->CreatePHI(SharkType::oop_type(), 0, "result");
twisti@2047 308 phi->addIncoming(LLVMValue::null(), null);
twisti@2047 309 phi->addIncoming(unboxed_result, not_null);
twisti@2047 310 result = phi;
twisti@2047 311 }
twisti@2047 312
twisti@2047 313 // Reset handle block
twisti@2047 314 CreateResetHandleBlock();
twisti@2047 315
twisti@2047 316 // Unlock if necessary.
twisti@2047 317 if (is_synchronized())
twisti@2047 318 Unimplemented();
twisti@2047 319
twisti@2047 320 // Unwind and return
twisti@2047 321 Value *result_addr = stack()->CreatePopFrame(type2size[result_type]);
twisti@2047 322 if (result_type != T_VOID) {
twisti@2047 323 bool needs_cast = false;
twisti@2047 324 bool is_signed = false;
twisti@2047 325 switch (result_type) {
twisti@2047 326 case T_BOOLEAN:
twisti@2047 327 result = builder()->CreateICmpNE(result, LLVMValue::jbyte_constant(0));
twisti@2047 328 needs_cast = true;
twisti@2047 329 break;
twisti@2047 330
twisti@2047 331 case T_CHAR:
twisti@2047 332 needs_cast = true;
twisti@2047 333 break;
twisti@2047 334
twisti@2047 335 case T_BYTE:
twisti@2047 336 case T_SHORT:
twisti@2047 337 needs_cast = true;
twisti@2047 338 is_signed = true;
twisti@2047 339 break;
twisti@2047 340 }
twisti@2047 341 if (needs_cast) {
twisti@2047 342 result = builder()->CreateIntCast(
twisti@2047 343 result, SharkType::to_stackType(result_type), is_signed);
twisti@2047 344 }
twisti@2047 345
twisti@2047 346 builder()->CreateStore(
twisti@2047 347 result,
twisti@2047 348 builder()->CreateIntToPtr(
twisti@2047 349 result_addr,
twisti@2047 350 PointerType::getUnqual(SharkType::to_stackType(result_type))));
twisti@2047 351 }
twisti@2047 352 builder()->CreateRet(LLVMValue::jint_constant(0));
twisti@2047 353 }

mercurial