1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/vm/shark/sharkBuilder.cpp Wed Apr 27 01:25:04 2016 +0800 1.3 @@ -0,0 +1,526 @@ 1.4 +/* 1.5 + * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved. 1.6 + * Copyright 2008, 2009, 2010 Red Hat, Inc. 1.7 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.8 + * 1.9 + * This code is free software; you can redistribute it and/or modify it 1.10 + * under the terms of the GNU General Public License version 2 only, as 1.11 + * published by the Free Software Foundation. 1.12 + * 1.13 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.14 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.15 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.16 + * version 2 for more details (a copy is included in the LICENSE file that 1.17 + * accompanied this code). 1.18 + * 1.19 + * You should have received a copy of the GNU General Public License version 1.20 + * 2 along with this work; if not, write to the Free Software Foundation, 1.21 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.22 + * 1.23 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.24 + * or visit www.oracle.com if you need additional information or have any 1.25 + * questions. 1.26 + * 1.27 + */ 1.28 + 1.29 +#include "precompiled.hpp" 1.30 +#include "ci/ciMethod.hpp" 1.31 +#include "memory/resourceArea.hpp" 1.32 +#include "oops/method.hpp" 1.33 +#include "runtime/os.hpp" 1.34 +#include "runtime/synchronizer.hpp" 1.35 +#include "runtime/thread.hpp" 1.36 +#include "shark/llvmHeaders.hpp" 1.37 +#include "shark/llvmValue.hpp" 1.38 +#include "shark/sharkBuilder.hpp" 1.39 +#include "shark/sharkContext.hpp" 1.40 +#include "shark/sharkRuntime.hpp" 1.41 +#include "utilities/debug.hpp" 1.42 + 1.43 +using namespace llvm; 1.44 + 1.45 +SharkBuilder::SharkBuilder(SharkCodeBuffer* code_buffer) 1.46 + : IRBuilder<>(SharkContext::current()), 1.47 + _code_buffer(code_buffer) { 1.48 +} 1.49 + 1.50 +// Helpers for accessing structures 1.51 +Value* SharkBuilder::CreateAddressOfStructEntry(Value* base, 1.52 + ByteSize offset, 1.53 + Type* type, 1.54 + const char* name) { 1.55 + return CreateBitCast(CreateStructGEP(base, in_bytes(offset)), type, name); 1.56 +} 1.57 + 1.58 +LoadInst* SharkBuilder::CreateValueOfStructEntry(Value* base, 1.59 + ByteSize offset, 1.60 + Type* type, 1.61 + const char* name) { 1.62 + return CreateLoad( 1.63 + CreateAddressOfStructEntry( 1.64 + base, offset, PointerType::getUnqual(type)), 1.65 + name); 1.66 +} 1.67 + 1.68 +// Helpers for accessing arrays 1.69 + 1.70 +LoadInst* SharkBuilder::CreateArrayLength(Value* arrayoop) { 1.71 + return CreateValueOfStructEntry( 1.72 + arrayoop, in_ByteSize(arrayOopDesc::length_offset_in_bytes()), 1.73 + SharkType::jint_type(), "length"); 1.74 +} 1.75 + 1.76 +Value* SharkBuilder::CreateArrayAddress(Value* arrayoop, 1.77 + Type* element_type, 1.78 + int element_bytes, 1.79 + ByteSize base_offset, 1.80 + Value* index, 1.81 + const char* name) { 1.82 + Value* offset = CreateIntCast(index, SharkType::intptr_type(), false); 1.83 + if (element_bytes != 1) 1.84 + offset = CreateShl( 1.85 + offset, 1.86 + LLVMValue::intptr_constant(exact_log2(element_bytes))); 1.87 + offset = CreateAdd( 1.88 + LLVMValue::intptr_constant(in_bytes(base_offset)), offset); 1.89 + 1.90 + return CreateIntToPtr( 1.91 + CreateAdd(CreatePtrToInt(arrayoop, SharkType::intptr_type()), offset), 1.92 + PointerType::getUnqual(element_type), 1.93 + name); 1.94 +} 1.95 + 1.96 +Value* SharkBuilder::CreateArrayAddress(Value* arrayoop, 1.97 + BasicType basic_type, 1.98 + ByteSize base_offset, 1.99 + Value* index, 1.100 + const char* name) { 1.101 + return CreateArrayAddress( 1.102 + arrayoop, 1.103 + SharkType::to_arrayType(basic_type), 1.104 + type2aelembytes(basic_type), 1.105 + base_offset, index, name); 1.106 +} 1.107 + 1.108 +Value* SharkBuilder::CreateArrayAddress(Value* arrayoop, 1.109 + BasicType basic_type, 1.110 + Value* index, 1.111 + const char* name) { 1.112 + return CreateArrayAddress( 1.113 + arrayoop, basic_type, 1.114 + in_ByteSize(arrayOopDesc::base_offset_in_bytes(basic_type)), 1.115 + index, name); 1.116 +} 1.117 + 1.118 +// Helpers for creating intrinsics and external functions. 1.119 + 1.120 +Type* SharkBuilder::make_type(char type, bool void_ok) { 1.121 + switch (type) { 1.122 + // Primitive types 1.123 + case 'c': 1.124 + return SharkType::jbyte_type(); 1.125 + case 'i': 1.126 + return SharkType::jint_type(); 1.127 + case 'l': 1.128 + return SharkType::jlong_type(); 1.129 + case 'x': 1.130 + return SharkType::intptr_type(); 1.131 + case 'f': 1.132 + return SharkType::jfloat_type(); 1.133 + case 'd': 1.134 + return SharkType::jdouble_type(); 1.135 + 1.136 + // Pointers to primitive types 1.137 + case 'C': 1.138 + case 'I': 1.139 + case 'L': 1.140 + case 'X': 1.141 + case 'F': 1.142 + case 'D': 1.143 + return PointerType::getUnqual(make_type(tolower(type), false)); 1.144 + 1.145 + // VM objects 1.146 + case 'T': 1.147 + return SharkType::thread_type(); 1.148 + case 'M': 1.149 + return PointerType::getUnqual(SharkType::monitor_type()); 1.150 + case 'O': 1.151 + return SharkType::oop_type(); 1.152 + case 'K': 1.153 + return SharkType::klass_type(); 1.154 + 1.155 + // Miscellaneous 1.156 + case 'v': 1.157 + assert(void_ok, "should be"); 1.158 + return SharkType::void_type(); 1.159 + case '1': 1.160 + return SharkType::bit_type(); 1.161 + 1.162 + default: 1.163 + ShouldNotReachHere(); 1.164 + } 1.165 +} 1.166 + 1.167 +FunctionType* SharkBuilder::make_ftype(const char* params, 1.168 + const char* ret) { 1.169 + std::vector<Type*> param_types; 1.170 + for (const char* c = params; *c; c++) 1.171 + param_types.push_back(make_type(*c, false)); 1.172 + 1.173 + assert(strlen(ret) == 1, "should be"); 1.174 + Type *return_type = make_type(*ret, true); 1.175 + 1.176 + return FunctionType::get(return_type, param_types, false); 1.177 +} 1.178 + 1.179 +// Create an object representing an intrinsic or external function by 1.180 +// referencing the symbol by name. This is the LLVM-style approach, 1.181 +// but it cannot be used on functions within libjvm.so its symbols 1.182 +// are not exported. Note that you cannot make this work simply by 1.183 +// exporting the symbols, as some symbols have the same names as 1.184 +// symbols in the standard libraries (eg, atan2, fabs) and would 1.185 +// obscure them were they visible. 1.186 +Value* SharkBuilder::make_function(const char* name, 1.187 + const char* params, 1.188 + const char* ret) { 1.189 + return SharkContext::current().get_external(name, make_ftype(params, ret)); 1.190 +} 1.191 + 1.192 +// Create an object representing an external function by inlining a 1.193 +// function pointer in the code. This is not the LLVM way, but it's 1.194 +// the only way to access functions in libjvm.so and functions like 1.195 +// __kernel_dmb on ARM which is accessed via an absolute address. 1.196 +Value* SharkBuilder::make_function(address func, 1.197 + const char* params, 1.198 + const char* ret) { 1.199 + return CreateIntToPtr( 1.200 + LLVMValue::intptr_constant((intptr_t) func), 1.201 + PointerType::getUnqual(make_ftype(params, ret))); 1.202 +} 1.203 + 1.204 +// VM calls 1.205 + 1.206 +Value* SharkBuilder::find_exception_handler() { 1.207 + return make_function( 1.208 + (address) SharkRuntime::find_exception_handler, "TIi", "i"); 1.209 +} 1.210 + 1.211 +Value* SharkBuilder::monitorenter() { 1.212 + return make_function((address) SharkRuntime::monitorenter, "TM", "v"); 1.213 +} 1.214 + 1.215 +Value* SharkBuilder::monitorexit() { 1.216 + return make_function((address) SharkRuntime::monitorexit, "TM", "v"); 1.217 +} 1.218 + 1.219 +Value* SharkBuilder::new_instance() { 1.220 + return make_function((address) SharkRuntime::new_instance, "Ti", "v"); 1.221 +} 1.222 + 1.223 +Value* SharkBuilder::newarray() { 1.224 + return make_function((address) SharkRuntime::newarray, "Tii", "v"); 1.225 +} 1.226 + 1.227 +Value* SharkBuilder::anewarray() { 1.228 + return make_function((address) SharkRuntime::anewarray, "Tii", "v"); 1.229 +} 1.230 + 1.231 +Value* SharkBuilder::multianewarray() { 1.232 + return make_function((address) SharkRuntime::multianewarray, "TiiI", "v"); 1.233 +} 1.234 + 1.235 +Value* SharkBuilder::register_finalizer() { 1.236 + return make_function((address) SharkRuntime::register_finalizer, "TO", "v"); 1.237 +} 1.238 + 1.239 +Value* SharkBuilder::safepoint() { 1.240 + return make_function((address) SafepointSynchronize::block, "T", "v"); 1.241 +} 1.242 + 1.243 +Value* SharkBuilder::throw_ArithmeticException() { 1.244 + return make_function( 1.245 + (address) SharkRuntime::throw_ArithmeticException, "TCi", "v"); 1.246 +} 1.247 + 1.248 +Value* SharkBuilder::throw_ArrayIndexOutOfBoundsException() { 1.249 + return make_function( 1.250 + (address) SharkRuntime::throw_ArrayIndexOutOfBoundsException, "TCii", "v"); 1.251 +} 1.252 + 1.253 +Value* SharkBuilder::throw_ClassCastException() { 1.254 + return make_function( 1.255 + (address) SharkRuntime::throw_ClassCastException, "TCi", "v"); 1.256 +} 1.257 + 1.258 +Value* SharkBuilder::throw_NullPointerException() { 1.259 + return make_function( 1.260 + (address) SharkRuntime::throw_NullPointerException, "TCi", "v"); 1.261 +} 1.262 + 1.263 +// High-level non-VM calls 1.264 + 1.265 +Value* SharkBuilder::f2i() { 1.266 + return make_function((address) SharedRuntime::f2i, "f", "i"); 1.267 +} 1.268 + 1.269 +Value* SharkBuilder::f2l() { 1.270 + return make_function((address) SharedRuntime::f2l, "f", "l"); 1.271 +} 1.272 + 1.273 +Value* SharkBuilder::d2i() { 1.274 + return make_function((address) SharedRuntime::d2i, "d", "i"); 1.275 +} 1.276 + 1.277 +Value* SharkBuilder::d2l() { 1.278 + return make_function((address) SharedRuntime::d2l, "d", "l"); 1.279 +} 1.280 + 1.281 +Value* SharkBuilder::is_subtype_of() { 1.282 + return make_function((address) SharkRuntime::is_subtype_of, "KK", "c"); 1.283 +} 1.284 + 1.285 +Value* SharkBuilder::current_time_millis() { 1.286 + return make_function((address) os::javaTimeMillis, "", "l"); 1.287 +} 1.288 + 1.289 +Value* SharkBuilder::sin() { 1.290 + return make_function("llvm.sin.f64", "d", "d"); 1.291 +} 1.292 + 1.293 +Value* SharkBuilder::cos() { 1.294 + return make_function("llvm.cos.f64", "d", "d"); 1.295 +} 1.296 + 1.297 +Value* SharkBuilder::tan() { 1.298 + return make_function((address) ::tan, "d", "d"); 1.299 +} 1.300 + 1.301 +Value* SharkBuilder::atan2() { 1.302 + return make_function((address) ::atan2, "dd", "d"); 1.303 +} 1.304 + 1.305 +Value* SharkBuilder::sqrt() { 1.306 + return make_function("llvm.sqrt.f64", "d", "d"); 1.307 +} 1.308 + 1.309 +Value* SharkBuilder::log() { 1.310 + return make_function("llvm.log.f64", "d", "d"); 1.311 +} 1.312 + 1.313 +Value* SharkBuilder::log10() { 1.314 + return make_function("llvm.log10.f64", "d", "d"); 1.315 +} 1.316 + 1.317 +Value* SharkBuilder::pow() { 1.318 + return make_function("llvm.pow.f64", "dd", "d"); 1.319 +} 1.320 + 1.321 +Value* SharkBuilder::exp() { 1.322 + return make_function("llvm.exp.f64", "d", "d"); 1.323 +} 1.324 + 1.325 +Value* SharkBuilder::fabs() { 1.326 + return make_function((address) ::fabs, "d", "d"); 1.327 +} 1.328 + 1.329 +Value* SharkBuilder::unsafe_field_offset_to_byte_offset() { 1.330 + extern jlong Unsafe_field_offset_to_byte_offset(jlong field_offset); 1.331 + return make_function((address) Unsafe_field_offset_to_byte_offset, "l", "l"); 1.332 +} 1.333 + 1.334 +Value* SharkBuilder::osr_migration_end() { 1.335 + return make_function((address) SharedRuntime::OSR_migration_end, "C", "v"); 1.336 +} 1.337 + 1.338 +// Semi-VM calls 1.339 + 1.340 +Value* SharkBuilder::throw_StackOverflowError() { 1.341 + return make_function((address) ZeroStack::handle_overflow, "T", "v"); 1.342 +} 1.343 + 1.344 +Value* SharkBuilder::uncommon_trap() { 1.345 + return make_function((address) SharkRuntime::uncommon_trap, "Ti", "i"); 1.346 +} 1.347 + 1.348 +Value* SharkBuilder::deoptimized_entry_point() { 1.349 + return make_function((address) CppInterpreter::main_loop, "iT", "v"); 1.350 +} 1.351 + 1.352 +// Native-Java transition 1.353 + 1.354 +Value* SharkBuilder::check_special_condition_for_native_trans() { 1.355 + return make_function( 1.356 + (address) JavaThread::check_special_condition_for_native_trans, 1.357 + "T", "v"); 1.358 +} 1.359 + 1.360 +Value* SharkBuilder::frame_address() { 1.361 + return make_function("llvm.frameaddress", "i", "C"); 1.362 +} 1.363 + 1.364 +Value* SharkBuilder::memset() { 1.365 + // LLVM 2.8 added a fifth isVolatile field for memset 1.366 + // introduced with LLVM r100304 1.367 + return make_function("llvm.memset.p0i8.i32", "Cciii", "v"); 1.368 +} 1.369 + 1.370 +Value* SharkBuilder::unimplemented() { 1.371 + return make_function((address) report_unimplemented, "Ci", "v"); 1.372 +} 1.373 + 1.374 +Value* SharkBuilder::should_not_reach_here() { 1.375 + return make_function((address) report_should_not_reach_here, "Ci", "v"); 1.376 +} 1.377 + 1.378 +Value* SharkBuilder::dump() { 1.379 + return make_function((address) SharkRuntime::dump, "Cx", "v"); 1.380 +} 1.381 + 1.382 +// Public interface to low-level non-VM calls 1.383 + 1.384 +CallInst* SharkBuilder::CreateGetFrameAddress() { 1.385 + return CreateCall(frame_address(), LLVMValue::jint_constant(0)); 1.386 +} 1.387 + 1.388 +CallInst* SharkBuilder::CreateMemset(Value* dst, 1.389 + Value* value, 1.390 + Value* len, 1.391 + Value* align) { 1.392 + return CreateCall5(memset(), dst, value, len, align, 1.393 + LLVMValue::jint_constant(0)); 1.394 +} 1.395 + 1.396 +CallInst* SharkBuilder::CreateUnimplemented(const char* file, int line) { 1.397 + return CreateCall2( 1.398 + unimplemented(), 1.399 + CreateIntToPtr( 1.400 + LLVMValue::intptr_constant((intptr_t) file), 1.401 + PointerType::getUnqual(SharkType::jbyte_type())), 1.402 + LLVMValue::jint_constant(line)); 1.403 +} 1.404 + 1.405 +CallInst* SharkBuilder::CreateShouldNotReachHere(const char* file, int line) { 1.406 + return CreateCall2( 1.407 + should_not_reach_here(), 1.408 + CreateIntToPtr( 1.409 + LLVMValue::intptr_constant((intptr_t) file), 1.410 + PointerType::getUnqual(SharkType::jbyte_type())), 1.411 + LLVMValue::jint_constant(line)); 1.412 +} 1.413 + 1.414 +#ifndef PRODUCT 1.415 +CallInst* SharkBuilder::CreateDump(Value* value) { 1.416 + const char *name; 1.417 + if (value->hasName()) 1.418 + // XXX this leaks, but it's only debug code 1.419 + name = strdup(value->getName().str().c_str()); 1.420 + else 1.421 + name = "unnamed_value"; 1.422 + 1.423 + if (isa<PointerType>(value->getType())) 1.424 + value = CreatePtrToInt(value, SharkType::intptr_type()); 1.425 + else if (value->getType()-> 1.426 + isIntegerTy() 1.427 + ) 1.428 + value = CreateIntCast(value, SharkType::intptr_type(), false); 1.429 + else 1.430 + Unimplemented(); 1.431 + 1.432 + return CreateCall2( 1.433 + dump(), 1.434 + CreateIntToPtr( 1.435 + LLVMValue::intptr_constant((intptr_t) name), 1.436 + PointerType::getUnqual(SharkType::jbyte_type())), 1.437 + value); 1.438 +} 1.439 +#endif // PRODUCT 1.440 + 1.441 +// HotSpot memory barriers 1.442 + 1.443 +void SharkBuilder::CreateUpdateBarrierSet(BarrierSet* bs, Value* field) { 1.444 + if (bs->kind() != BarrierSet::CardTableModRef) 1.445 + Unimplemented(); 1.446 + 1.447 + CreateStore( 1.448 + LLVMValue::jbyte_constant(CardTableModRefBS::dirty_card), 1.449 + CreateIntToPtr( 1.450 + CreateAdd( 1.451 + LLVMValue::intptr_constant( 1.452 + (intptr_t) ((CardTableModRefBS *) bs)->byte_map_base), 1.453 + CreateLShr( 1.454 + CreatePtrToInt(field, SharkType::intptr_type()), 1.455 + LLVMValue::intptr_constant(CardTableModRefBS::card_shift))), 1.456 + PointerType::getUnqual(SharkType::jbyte_type()))); 1.457 +} 1.458 + 1.459 +// Helpers for accessing the code buffer 1.460 + 1.461 +Value* SharkBuilder::code_buffer_address(int offset) { 1.462 + return CreateAdd( 1.463 + code_buffer()->base_pc(), 1.464 + LLVMValue::intptr_constant(offset)); 1.465 +} 1.466 + 1.467 +Value* SharkBuilder::CreateInlineOop(jobject object, const char* name) { 1.468 + return CreateLoad( 1.469 + CreateIntToPtr( 1.470 + code_buffer_address(code_buffer()->inline_oop(object)), 1.471 + PointerType::getUnqual(SharkType::oop_type())), 1.472 + name); 1.473 +} 1.474 + 1.475 +Value* SharkBuilder::CreateInlineMetadata(Metadata* metadata, llvm::PointerType* type, const char* name) { 1.476 + assert(metadata != NULL, "inlined metadata must not be NULL"); 1.477 + assert(metadata->is_metaspace_object(), "sanity check"); 1.478 + return CreateLoad( 1.479 + CreateIntToPtr( 1.480 + code_buffer_address(code_buffer()->inline_Metadata(metadata)), 1.481 + PointerType::getUnqual(type)), 1.482 + name); 1.483 +} 1.484 + 1.485 +Value* SharkBuilder::CreateInlineData(void* data, 1.486 + size_t size, 1.487 + Type* type, 1.488 + const char* name) { 1.489 + return CreateIntToPtr( 1.490 + code_buffer_address(code_buffer()->inline_data(data, size)), 1.491 + type, 1.492 + name); 1.493 +} 1.494 + 1.495 +// Helpers for creating basic blocks. 1.496 + 1.497 +BasicBlock* SharkBuilder::GetBlockInsertionPoint() const { 1.498 + BasicBlock *cur = GetInsertBlock(); 1.499 + 1.500 + // BasicBlock::Create takes an insertBefore argument, so 1.501 + // we need to find the block _after_ the current block 1.502 + Function::iterator iter = cur->getParent()->begin(); 1.503 + Function::iterator end = cur->getParent()->end(); 1.504 + while (iter != end) { 1.505 + iter++; 1.506 + if (&*iter == cur) { 1.507 + iter++; 1.508 + break; 1.509 + } 1.510 + } 1.511 + 1.512 + if (iter == end) 1.513 + return NULL; 1.514 + else 1.515 + return iter; 1.516 +} 1.517 + 1.518 +BasicBlock* SharkBuilder::CreateBlock(BasicBlock* ip, const char* name) const { 1.519 + return BasicBlock::Create( 1.520 + SharkContext::current(), name, GetInsertBlock()->getParent(), ip); 1.521 +} 1.522 + 1.523 +LoadInst* SharkBuilder::CreateAtomicLoad(Value* ptr, unsigned align, AtomicOrdering ordering, SynchronizationScope synchScope, bool isVolatile, const char* name) { 1.524 + return Insert(new LoadInst(ptr, name, isVolatile, align, ordering, synchScope), name); 1.525 +} 1.526 + 1.527 +StoreInst* SharkBuilder::CreateAtomicStore(Value* val, Value* ptr, unsigned align, AtomicOrdering ordering, SynchronizationScope synchScope, bool isVolatile, const char* name) { 1.528 + return Insert(new StoreInst(val, ptr, isVolatile, align, ordering, synchScope), name); 1.529 +}