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