1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/vm/shark/sharkIntrinsics.cpp Wed Apr 27 01:25:04 2016 +0800 1.3 @@ -0,0 +1,275 @@ 1.4 +/* 1.5 + * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved. 1.6 + * Copyright 2009 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 "shark/llvmHeaders.hpp" 1.32 +#include "shark/sharkIntrinsics.hpp" 1.33 +#include "shark/sharkState.hpp" 1.34 +#include "shark/sharkValue.hpp" 1.35 +#include "shark/shark_globals.hpp" 1.36 + 1.37 +using namespace llvm; 1.38 + 1.39 +bool SharkIntrinsics::is_intrinsic(ciMethod *target) { 1.40 + switch (target->intrinsic_id()) { 1.41 + case vmIntrinsics::_none: 1.42 + return false; 1.43 + 1.44 + // java.lang.Math 1.45 + case vmIntrinsics::_min: 1.46 + case vmIntrinsics::_max: 1.47 + case vmIntrinsics::_dabs: 1.48 + case vmIntrinsics::_dsin: 1.49 + case vmIntrinsics::_dcos: 1.50 + case vmIntrinsics::_dtan: 1.51 + case vmIntrinsics::_datan2: 1.52 + case vmIntrinsics::_dsqrt: 1.53 + case vmIntrinsics::_dlog: 1.54 + case vmIntrinsics::_dlog10: 1.55 + case vmIntrinsics::_dpow: 1.56 + case vmIntrinsics::_dexp: 1.57 + return true; 1.58 + 1.59 + // java.lang.Object 1.60 + case vmIntrinsics::_getClass: 1.61 + return true; 1.62 + 1.63 + // java.lang.System 1.64 + case vmIntrinsics::_currentTimeMillis: 1.65 + return true; 1.66 + 1.67 + // java.lang.Thread 1.68 + case vmIntrinsics::_currentThread: 1.69 + return true; 1.70 + 1.71 + // sun.misc.Unsafe 1.72 + case vmIntrinsics::_compareAndSwapInt: 1.73 + return true; 1.74 + 1.75 + default: 1.76 + if (SharkPerformanceWarnings) { 1.77 + warning( 1.78 + "unhandled intrinsic vmIntrinsic::%s", 1.79 + vmIntrinsics::name_at(target->intrinsic_id())); 1.80 + } 1.81 + } 1.82 + return false; 1.83 +} 1.84 + 1.85 +void SharkIntrinsics::inline_intrinsic(ciMethod *target, SharkState *state) { 1.86 + SharkIntrinsics intrinsic(state, target); 1.87 + intrinsic.do_intrinsic(); 1.88 +} 1.89 + 1.90 +void SharkIntrinsics::do_intrinsic() { 1.91 + switch (target()->intrinsic_id()) { 1.92 + // java.lang.Math 1.93 + case vmIntrinsics::_min: 1.94 + do_Math_minmax(llvm::ICmpInst::ICMP_SLE); 1.95 + break; 1.96 + case vmIntrinsics::_max: 1.97 + do_Math_minmax(llvm::ICmpInst::ICMP_SGE); 1.98 + break; 1.99 + case vmIntrinsics::_dabs: 1.100 + do_Math_1to1(builder()->fabs()); 1.101 + break; 1.102 + case vmIntrinsics::_dsin: 1.103 + do_Math_1to1(builder()->sin()); 1.104 + break; 1.105 + case vmIntrinsics::_dcos: 1.106 + do_Math_1to1(builder()->cos()); 1.107 + break; 1.108 + case vmIntrinsics::_dtan: 1.109 + do_Math_1to1(builder()->tan()); 1.110 + break; 1.111 + case vmIntrinsics::_datan2: 1.112 + do_Math_2to1(builder()->atan2()); 1.113 + break; 1.114 + case vmIntrinsics::_dsqrt: 1.115 + do_Math_1to1(builder()->sqrt()); 1.116 + break; 1.117 + case vmIntrinsics::_dlog: 1.118 + do_Math_1to1(builder()->log()); 1.119 + break; 1.120 + case vmIntrinsics::_dlog10: 1.121 + do_Math_1to1(builder()->log10()); 1.122 + break; 1.123 + case vmIntrinsics::_dpow: 1.124 + do_Math_2to1(builder()->pow()); 1.125 + break; 1.126 + case vmIntrinsics::_dexp: 1.127 + do_Math_1to1(builder()->exp()); 1.128 + break; 1.129 + 1.130 + // java.lang.Object 1.131 + case vmIntrinsics::_getClass: 1.132 + do_Object_getClass(); 1.133 + break; 1.134 + 1.135 + // java.lang.System 1.136 + case vmIntrinsics::_currentTimeMillis: 1.137 + do_System_currentTimeMillis(); 1.138 + break; 1.139 + 1.140 + // java.lang.Thread 1.141 + case vmIntrinsics::_currentThread: 1.142 + do_Thread_currentThread(); 1.143 + break; 1.144 + 1.145 + // sun.misc.Unsafe 1.146 + case vmIntrinsics::_compareAndSwapInt: 1.147 + do_Unsafe_compareAndSwapInt(); 1.148 + break; 1.149 + 1.150 + default: 1.151 + ShouldNotReachHere(); 1.152 + } 1.153 +} 1.154 + 1.155 +void SharkIntrinsics::do_Math_minmax(ICmpInst::Predicate p) { 1.156 + // Pop the arguments 1.157 + SharkValue *sb = state()->pop(); 1.158 + SharkValue *sa = state()->pop(); 1.159 + Value *a = sa->jint_value(); 1.160 + Value *b = sb->jint_value(); 1.161 + 1.162 + // Perform the test 1.163 + BasicBlock *ip = builder()->GetBlockInsertionPoint(); 1.164 + BasicBlock *return_a = builder()->CreateBlock(ip, "return_a"); 1.165 + BasicBlock *return_b = builder()->CreateBlock(ip, "return_b"); 1.166 + BasicBlock *done = builder()->CreateBlock(ip, "done"); 1.167 + 1.168 + builder()->CreateCondBr(builder()->CreateICmp(p, a, b), return_a, return_b); 1.169 + 1.170 + builder()->SetInsertPoint(return_a); 1.171 + builder()->CreateBr(done); 1.172 + 1.173 + builder()->SetInsertPoint(return_b); 1.174 + builder()->CreateBr(done); 1.175 + 1.176 + builder()->SetInsertPoint(done); 1.177 + PHINode *phi = builder()->CreatePHI(a->getType(), 0, "result"); 1.178 + phi->addIncoming(a, return_a); 1.179 + phi->addIncoming(b, return_b); 1.180 + 1.181 + // Push the result 1.182 + state()->push( 1.183 + SharkValue::create_jint( 1.184 + phi, 1.185 + sa->zero_checked() && sb->zero_checked())); 1.186 +} 1.187 + 1.188 +void SharkIntrinsics::do_Math_1to1(Value *function) { 1.189 + SharkValue *empty = state()->pop(); 1.190 + assert(empty == NULL, "should be"); 1.191 + state()->push( 1.192 + SharkValue::create_jdouble( 1.193 + builder()->CreateCall( 1.194 + function, state()->pop()->jdouble_value()))); 1.195 + state()->push(NULL); 1.196 +} 1.197 + 1.198 +void SharkIntrinsics::do_Math_2to1(Value *function) { 1.199 + SharkValue *empty = state()->pop(); 1.200 + assert(empty == NULL, "should be"); 1.201 + Value *y = state()->pop()->jdouble_value(); 1.202 + empty = state()->pop(); 1.203 + assert(empty == NULL, "should be"); 1.204 + Value *x = state()->pop()->jdouble_value(); 1.205 + 1.206 + state()->push( 1.207 + SharkValue::create_jdouble( 1.208 + builder()->CreateCall2(function, x, y))); 1.209 + state()->push(NULL); 1.210 +} 1.211 + 1.212 +void SharkIntrinsics::do_Object_getClass() { 1.213 + Value *klass = builder()->CreateValueOfStructEntry( 1.214 + state()->pop()->jobject_value(), 1.215 + in_ByteSize(oopDesc::klass_offset_in_bytes()), 1.216 + SharkType::klass_type(), 1.217 + "klass"); 1.218 + 1.219 + state()->push( 1.220 + SharkValue::create_jobject( 1.221 + builder()->CreateValueOfStructEntry( 1.222 + klass, 1.223 + Klass::java_mirror_offset(), 1.224 + SharkType::oop_type(), 1.225 + "java_mirror"), 1.226 + true)); 1.227 +} 1.228 + 1.229 +void SharkIntrinsics::do_System_currentTimeMillis() { 1.230 + state()->push( 1.231 + SharkValue::create_jlong( 1.232 + builder()->CreateCall(builder()->current_time_millis()), 1.233 + false)); 1.234 + state()->push(NULL); 1.235 +} 1.236 + 1.237 +void SharkIntrinsics::do_Thread_currentThread() { 1.238 + state()->push( 1.239 + SharkValue::create_jobject( 1.240 + builder()->CreateValueOfStructEntry( 1.241 + thread(), JavaThread::threadObj_offset(), 1.242 + SharkType::oop_type(), 1.243 + "threadObj"), 1.244 + true)); 1.245 +} 1.246 + 1.247 +void SharkIntrinsics::do_Unsafe_compareAndSwapInt() { 1.248 + // Pop the arguments 1.249 + Value *x = state()->pop()->jint_value(); 1.250 + Value *e = state()->pop()->jint_value(); 1.251 + SharkValue *empty = state()->pop(); 1.252 + assert(empty == NULL, "should be"); 1.253 + Value *offset = state()->pop()->jlong_value(); 1.254 + Value *object = state()->pop()->jobject_value(); 1.255 + Value *unsafe = state()->pop()->jobject_value(); 1.256 + 1.257 + // Convert the offset 1.258 + offset = builder()->CreateCall( 1.259 + builder()->unsafe_field_offset_to_byte_offset(), 1.260 + offset); 1.261 + 1.262 + // Locate the field 1.263 + Value *addr = builder()->CreateIntToPtr( 1.264 + builder()->CreateAdd( 1.265 + builder()->CreatePtrToInt(object, SharkType::intptr_type()), 1.266 + builder()->CreateIntCast(offset, SharkType::intptr_type(), true)), 1.267 + PointerType::getUnqual(SharkType::jint_type()), 1.268 + "addr"); 1.269 + 1.270 + // Perform the operation 1.271 + Value *result = builder()->CreateAtomicCmpXchg(addr, e, x, llvm::SequentiallyConsistent); 1.272 + // Push the result 1.273 + state()->push( 1.274 + SharkValue::create_jint( 1.275 + builder()->CreateIntCast( 1.276 + builder()->CreateICmpEQ(result, e), SharkType::jint_type(), true), 1.277 + false)); 1.278 +}