annotate src/share/vm/opto/library_call.cpp @ 5613:a57a165b8296

8027353: Exact intrinsics: assert(n != NULL) failed: must not be null Reviewed-by: kvn, roland
author rbackman
date Mon, 28 Oct 2013 08:34:10 +0100
parents 59e8ad757e19
children
rev   line source
duke@0 1 /*
sla@4802 2 * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.
duke@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@0 4 *
duke@0 5 * This code is free software; you can redistribute it and/or modify it
duke@0 6 * under the terms of the GNU General Public License version 2 only, as
duke@0 7 * published by the Free Software Foundation.
duke@0 8 *
duke@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@0 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@0 13 * accompanied this code).
duke@0 14 *
duke@0 15 * You should have received a copy of the GNU General Public License version
duke@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@0 18 *
trims@1472 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
trims@1472 20 * or visit www.oracle.com if you need additional information or have any
trims@1472 21 * questions.
duke@0 22 *
duke@0 23 */
duke@0 24
stefank@1879 25 #include "precompiled.hpp"
stefank@1879 26 #include "classfile/systemDictionary.hpp"
stefank@1879 27 #include "classfile/vmSymbols.hpp"
twisti@2252 28 #include "compiler/compileBroker.hpp"
stefank@1879 29 #include "compiler/compileLog.hpp"
stefank@1879 30 #include "oops/objArrayKlass.hpp"
stefank@1879 31 #include "opto/addnode.hpp"
stefank@1879 32 #include "opto/callGenerator.hpp"
stefank@1879 33 #include "opto/cfgnode.hpp"
stefank@1879 34 #include "opto/idealKit.hpp"
rbackman@5356 35 #include "opto/mathexactnode.hpp"
stefank@1879 36 #include "opto/mulnode.hpp"
stefank@1879 37 #include "opto/parse.hpp"
stefank@1879 38 #include "opto/runtime.hpp"
stefank@1879 39 #include "opto/subnode.hpp"
stefank@1879 40 #include "prims/nativeLookup.hpp"
stefank@1879 41 #include "runtime/sharedRuntime.hpp"
sla@4802 42 #include "trace/traceMacros.hpp"
duke@0 43
duke@0 44 class LibraryIntrinsic : public InlineCallGenerator {
duke@0 45 // Extend the set of intrinsics known to the runtime:
duke@0 46 public:
duke@0 47 private:
duke@0 48 bool _is_virtual;
kvn@3770 49 bool _is_predicted;
shade@5363 50 bool _does_virtual_dispatch;
duke@0 51 vmIntrinsics::ID _intrinsic_id;
duke@0 52
duke@0 53 public:
shade@5363 54 LibraryIntrinsic(ciMethod* m, bool is_virtual, bool is_predicted, bool does_virtual_dispatch, vmIntrinsics::ID id)
duke@0 55 : InlineCallGenerator(m),
duke@0 56 _is_virtual(is_virtual),
kvn@3770 57 _is_predicted(is_predicted),
shade@5363 58 _does_virtual_dispatch(does_virtual_dispatch),
duke@0 59 _intrinsic_id(id)
duke@0 60 {
duke@0 61 }
duke@0 62 virtual bool is_intrinsic() const { return true; }
duke@0 63 virtual bool is_virtual() const { return _is_virtual; }
kvn@3770 64 virtual bool is_predicted() const { return _is_predicted; }
shade@5363 65 virtual bool does_virtual_dispatch() const { return _does_virtual_dispatch; }
roland@5546 66 virtual JVMState* generate(JVMState* jvms, Parse* parent_parser);
kvn@3770 67 virtual Node* generate_predicate(JVMState* jvms);
duke@0 68 vmIntrinsics::ID intrinsic_id() const { return _intrinsic_id; }
duke@0 69 };
duke@0 70
duke@0 71
duke@0 72 // Local helper class for LibraryIntrinsic:
duke@0 73 class LibraryCallKit : public GraphKit {
duke@0 74 private:
twisti@3878 75 LibraryIntrinsic* _intrinsic; // the library intrinsic being called
twisti@3878 76 Node* _result; // the result node, if any
twisti@3878 77 int _reexecute_sp; // the stack pointer when bytecode needs to be reexecuted
duke@0 78
roland@3671 79 const TypeOopPtr* sharpen_unsafe_type(Compile::AliasType* alias_type, const TypePtr *adr_type, bool is_native_ptr = false);
roland@3671 80
duke@0 81 public:
twisti@3878 82 LibraryCallKit(JVMState* jvms, LibraryIntrinsic* intrinsic)
twisti@3878 83 : GraphKit(jvms),
twisti@3878 84 _intrinsic(intrinsic),
twisti@3878 85 _result(NULL)
duke@0 86 {
twisti@3884 87 // Check if this is a root compile. In that case we don't have a caller.
twisti@3884 88 if (!jvms->has_method()) {
twisti@3884 89 _reexecute_sp = sp();
twisti@3884 90 } else {
twisti@3884 91 // Find out how many arguments the interpreter needs when deoptimizing
twisti@3884 92 // and save the stack pointer value so it can used by uncommon_trap.
twisti@3884 93 // We find the argument count by looking at the declared signature.
twisti@3884 94 bool ignored_will_link;
twisti@3884 95 ciSignature* declared_signature = NULL;
twisti@3884 96 ciMethod* ignored_callee = caller()->get_method_at_bci(bci(), ignored_will_link, &declared_signature);
twisti@3884 97 const int nargs = declared_signature->arg_size_for_bc(caller()->java_code_at_bci(bci()));
twisti@3884 98 _reexecute_sp = sp() + nargs; // "push" arguments back on stack
twisti@3884 99 }
duke@0 100 }
duke@0 101
twisti@3878 102 virtual LibraryCallKit* is_LibraryCallKit() const { return (LibraryCallKit*)this; }
twisti@3878 103
duke@0 104 ciMethod* caller() const { return jvms()->method(); }
duke@0 105 int bci() const { return jvms()->bci(); }
duke@0 106 LibraryIntrinsic* intrinsic() const { return _intrinsic; }
duke@0 107 vmIntrinsics::ID intrinsic_id() const { return _intrinsic->intrinsic_id(); }
duke@0 108 ciMethod* callee() const { return _intrinsic->method(); }
duke@0 109
duke@0 110 bool try_to_inline();
kvn@3770 111 Node* try_to_predicate();
duke@0 112
twisti@3878 113 void push_result() {
twisti@3878 114 // Push the result onto the stack.
twisti@3878 115 if (!stopped() && result() != NULL) {
twisti@3878 116 BasicType bt = result()->bottom_type()->basic_type();
twisti@3878 117 push_node(bt, result());
twisti@3878 118 }
twisti@3878 119 }
twisti@3878 120
twisti@3878 121 private:
twisti@3878 122 void fatal_unexpected_iid(vmIntrinsics::ID iid) {
twisti@3878 123 fatal(err_msg_res("unexpected intrinsic %d: %s", iid, vmIntrinsics::name_at(iid)));
twisti@3878 124 }
twisti@3878 125
twisti@3878 126 void set_result(Node* n) { assert(_result == NULL, "only set once"); _result = n; }
twisti@3878 127 void set_result(RegionNode* region, PhiNode* value);
twisti@3878 128 Node* result() { return _result; }
twisti@3878 129
twisti@3878 130 virtual int reexecute_sp() { return _reexecute_sp; }
twisti@3878 131
duke@0 132 // Helper functions to inline natives
duke@0 133 Node* generate_guard(Node* test, RegionNode* region, float true_prob);
duke@0 134 Node* generate_slow_guard(Node* test, RegionNode* region);
duke@0 135 Node* generate_fair_guard(Node* test, RegionNode* region);
duke@0 136 Node* generate_negative_guard(Node* index, RegionNode* region,
duke@0 137 // resulting CastII of index:
duke@0 138 Node* *pos_index = NULL);
duke@0 139 Node* generate_nonpositive_guard(Node* index, bool never_negative,
duke@0 140 // resulting CastII of index:
duke@0 141 Node* *pos_index = NULL);
duke@0 142 Node* generate_limit_guard(Node* offset, Node* subseq_length,
duke@0 143 Node* array_length,
duke@0 144 RegionNode* region);
duke@0 145 Node* generate_current_thread(Node* &tls_output);
duke@0 146 address basictype2arraycopy(BasicType t, Node *src_offset, Node *dest_offset,
iveresov@2171 147 bool disjoint_bases, const char* &name, bool dest_uninitialized);
duke@0 148 Node* load_mirror_from_klass(Node* klass);
duke@0 149 Node* load_klass_from_mirror_common(Node* mirror, bool never_see_null,
duke@0 150 RegionNode* region, int null_path,
duke@0 151 int offset);
twisti@3878 152 Node* load_klass_from_mirror(Node* mirror, bool never_see_null,
duke@0 153 RegionNode* region, int null_path) {
duke@0 154 int offset = java_lang_Class::klass_offset_in_bytes();
twisti@3878 155 return load_klass_from_mirror_common(mirror, never_see_null,
duke@0 156 region, null_path,
duke@0 157 offset);
duke@0 158 }
duke@0 159 Node* load_array_klass_from_mirror(Node* mirror, bool never_see_null,
duke@0 160 RegionNode* region, int null_path) {
duke@0 161 int offset = java_lang_Class::array_klass_offset_in_bytes();
twisti@3878 162 return load_klass_from_mirror_common(mirror, never_see_null,
duke@0 163 region, null_path,
duke@0 164 offset);
duke@0 165 }
duke@0 166 Node* generate_access_flags_guard(Node* kls,
duke@0 167 int modifier_mask, int modifier_bits,
duke@0 168 RegionNode* region);
duke@0 169 Node* generate_interface_guard(Node* kls, RegionNode* region);
duke@0 170 Node* generate_array_guard(Node* kls, RegionNode* region) {
duke@0 171 return generate_array_guard_common(kls, region, false, false);
duke@0 172 }
duke@0 173 Node* generate_non_array_guard(Node* kls, RegionNode* region) {
duke@0 174 return generate_array_guard_common(kls, region, false, true);
duke@0 175 }
duke@0 176 Node* generate_objArray_guard(Node* kls, RegionNode* region) {
duke@0 177 return generate_array_guard_common(kls, region, true, false);
duke@0 178 }
duke@0 179 Node* generate_non_objArray_guard(Node* kls, RegionNode* region) {
duke@0 180 return generate_array_guard_common(kls, region, true, true);
duke@0 181 }
duke@0 182 Node* generate_array_guard_common(Node* kls, RegionNode* region,
duke@0 183 bool obj_array, bool not_array);
duke@0 184 Node* generate_virtual_guard(Node* obj_klass, RegionNode* slow_region);
duke@0 185 CallJavaNode* generate_method_call(vmIntrinsics::ID method_id,
duke@0 186 bool is_virtual = false, bool is_static = false);
duke@0 187 CallJavaNode* generate_method_call_static(vmIntrinsics::ID method_id) {
duke@0 188 return generate_method_call(method_id, false, true);
duke@0 189 }
duke@0 190 CallJavaNode* generate_method_call_virtual(vmIntrinsics::ID method_id) {
duke@0 191 return generate_method_call(method_id, true, false);
duke@0 192 }
kvn@3770 193 Node * load_field_from_object(Node * fromObj, const char * fieldName, const char * fieldTypeString, bool is_exact, bool is_static);
duke@0 194
kvn@3325 195 Node* make_string_method_node(int opcode, Node* str1_start, Node* cnt1, Node* str2_start, Node* cnt2);
kvn@3325 196 Node* make_string_method_node(int opcode, Node* str1, Node* str2);
duke@0 197 bool inline_string_compareTo();
duke@0 198 bool inline_string_indexOf();
duke@0 199 Node* string_indexOf(Node* string_object, ciTypeArray* target_array, jint offset, jint cache_i, jint md2_i);
cfang@681 200 bool inline_string_equals();
twisti@3878 201 Node* round_double_node(Node* n);
duke@0 202 bool runtime_math(const TypeFunc* call_type, address funcAddr, const char* funcName);
duke@0 203 bool inline_math_native(vmIntrinsics::ID id);
duke@0 204 bool inline_trig(vmIntrinsics::ID id);
twisti@3878 205 bool inline_math(vmIntrinsics::ID id);
rbackman@5562 206 void inline_math_mathExact(Node* math);
rbackman@5562 207 bool inline_math_addExactI(bool is_increment);
rbackman@5562 208 bool inline_math_addExactL(bool is_increment);
rbackman@5562 209 bool inline_math_multiplyExactI();
rbackman@5562 210 bool inline_math_multiplyExactL();
rbackman@5562 211 bool inline_math_negateExactI();
rbackman@5562 212 bool inline_math_negateExactL();
rbackman@5562 213 bool inline_math_subtractExactI(bool is_decrement);
rbackman@5562 214 bool inline_math_subtractExactL(bool is_decrement);
twisti@3878 215 bool inline_exp();
twisti@3878 216 bool inline_pow();
roland@3473 217 void finish_pow_exp(Node* result, Node* x, Node* y, const TypeFunc* call_type, address funcAddr, const char* funcName);
duke@0 218 bool inline_min_max(vmIntrinsics::ID id);
duke@0 219 Node* generate_min_max(vmIntrinsics::ID id, Node* x, Node* y);
duke@0 220 // This returns Type::AnyPtr, RawPtr, or OopPtr.
duke@0 221 int classify_unsafe_addr(Node* &base, Node* &offset);
duke@0 222 Node* make_unsafe_address(Node* base, Node* offset);
johnc@2346 223 // Helper for inline_unsafe_access.
johnc@2346 224 // Generates the guards that check whether the result of
johnc@2346 225 // Unsafe.getObject should be recorded in an SATB log buffer.
twisti@3878 226 void insert_pre_barrier(Node* base_oop, Node* offset, Node* pre_val, bool need_mem_bar);
duke@0 227 bool inline_unsafe_access(bool is_native_ptr, bool is_store, BasicType type, bool is_volatile);
duke@0 228 bool inline_unsafe_prefetch(bool is_native_ptr, bool is_store, bool is_static);
rbackman@5111 229 static bool klass_needs_init_guard(Node* kls);
duke@0 230 bool inline_unsafe_allocate();
duke@0 231 bool inline_unsafe_copyMemory();
duke@0 232 bool inline_native_currentThread();
rbackman@3274 233 #ifdef TRACE_HAVE_INTRINSICS
rbackman@3274 234 bool inline_native_classID();
rbackman@3274 235 bool inline_native_threadID();
rbackman@3274 236 #endif
rbackman@3274 237 bool inline_native_time_funcs(address method, const char* funcName);
duke@0 238 bool inline_native_isInterrupted();
duke@0 239 bool inline_native_Class_query(vmIntrinsics::ID id);
duke@0 240 bool inline_native_subtype_check();
duke@0 241
duke@0 242 bool inline_native_newArray();
duke@0 243 bool inline_native_getLength();
duke@0 244 bool inline_array_copyOf(bool is_copyOfRange);
rasbold@169 245 bool inline_array_equals();
kvn@833 246 void copy_to_clone(Node* obj, Node* alloc_obj, Node* obj_size, bool is_array, bool card_mark);
duke@0 247 bool inline_native_clone(bool is_virtual);
duke@0 248 bool inline_native_Reflection_getCallerClass();
duke@0 249 // Helper function for inlining native object hash method
duke@0 250 bool inline_native_hashcode(bool is_virtual, bool is_static);
duke@0 251 bool inline_native_getClass();
duke@0 252
duke@0 253 // Helper functions for inlining arraycopy
duke@0 254 bool inline_arraycopy();
duke@0 255 void generate_arraycopy(const TypePtr* adr_type,
duke@0 256 BasicType basic_elem_type,
duke@0 257 Node* src, Node* src_offset,
duke@0 258 Node* dest, Node* dest_offset,
duke@0 259 Node* copy_length,
duke@0 260 bool disjoint_bases = false,
duke@0 261 bool length_never_negative = false,
duke@0 262 RegionNode* slow_region = NULL);
duke@0 263 AllocateArrayNode* tightly_coupled_allocation(Node* ptr,
duke@0 264 RegionNode* slow_region);
duke@0 265 void generate_clear_array(const TypePtr* adr_type,
duke@0 266 Node* dest,
duke@0 267 BasicType basic_elem_type,
duke@0 268 Node* slice_off,
duke@0 269 Node* slice_len,
duke@0 270 Node* slice_end);
duke@0 271 bool generate_block_arraycopy(const TypePtr* adr_type,
duke@0 272 BasicType basic_elem_type,
duke@0 273 AllocateNode* alloc,
duke@0 274 Node* src, Node* src_offset,
duke@0 275 Node* dest, Node* dest_offset,
iveresov@2171 276 Node* dest_size, bool dest_uninitialized);
duke@0 277 void generate_slow_arraycopy(const TypePtr* adr_type,
duke@0 278 Node* src, Node* src_offset,
duke@0 279 Node* dest, Node* dest_offset,
iveresov@2171 280 Node* copy_length, bool dest_uninitialized);
duke@0 281 Node* generate_checkcast_arraycopy(const TypePtr* adr_type,
duke@0 282 Node* dest_elem_klass,
duke@0 283 Node* src, Node* src_offset,
duke@0 284 Node* dest, Node* dest_offset,
iveresov@2171 285 Node* copy_length, bool dest_uninitialized);
duke@0 286 Node* generate_generic_arraycopy(const TypePtr* adr_type,
duke@0 287 Node* src, Node* src_offset,
duke@0 288 Node* dest, Node* dest_offset,
iveresov@2171 289 Node* copy_length, bool dest_uninitialized);
duke@0 290 void generate_unchecked_arraycopy(const TypePtr* adr_type,
duke@0 291 BasicType basic_elem_type,
duke@0 292 bool disjoint_bases,
duke@0 293 Node* src, Node* src_offset,
duke@0 294 Node* dest, Node* dest_offset,
iveresov@2171 295 Node* copy_length, bool dest_uninitialized);
roland@3671 296 typedef enum { LS_xadd, LS_xchg, LS_cmpxchg } LoadStoreKind;
roland@3671 297 bool inline_unsafe_load_store(BasicType type, LoadStoreKind kind);
duke@0 298 bool inline_unsafe_ordered_store(BasicType type);
kvn@3926 299 bool inline_unsafe_fence(vmIntrinsics::ID id);
duke@0 300 bool inline_fp_conversions(vmIntrinsics::ID id);
twisti@3878 301 bool inline_number_methods(vmIntrinsics::ID id);
johnc@2346 302 bool inline_reference_get();
kvn@3770 303 bool inline_aescrypt_Block(vmIntrinsics::ID id);
kvn@3770 304 bool inline_cipherBlockChaining_AESCrypt(vmIntrinsics::ID id);
kvn@3770 305 Node* inline_cipherBlockChaining_AESCrypt_predicate(bool decrypting);
kvn@3770 306 Node* get_key_start_from_aescrypt_object(Node* aescrypt_object);
kvn@4044 307 bool inline_encodeISOArray();
drchase@4918 308 bool inline_updateCRC32();
drchase@4918 309 bool inline_updateBytesCRC32();
drchase@4918 310 bool inline_updateByteBufferCRC32();
duke@0 311 };
duke@0 312
duke@0 313
duke@0 314 //---------------------------make_vm_intrinsic----------------------------
duke@0 315 CallGenerator* Compile::make_vm_intrinsic(ciMethod* m, bool is_virtual) {
duke@0 316 vmIntrinsics::ID id = m->intrinsic_id();
duke@0 317 assert(id != vmIntrinsics::_none, "must be a VM intrinsic");
duke@0 318
duke@0 319 if (DisableIntrinsic[0] != '\0'
duke@0 320 && strstr(DisableIntrinsic, vmIntrinsics::name_at(id)) != NULL) {
duke@0 321 // disabled by a user request on the command line:
duke@0 322 // example: -XX:DisableIntrinsic=_hashCode,_getClass
duke@0 323 return NULL;
duke@0 324 }
duke@0 325
duke@0 326 if (!m->is_loaded()) {
duke@0 327 // do not attempt to inline unloaded methods
duke@0 328 return NULL;
duke@0 329 }
duke@0 330
duke@0 331 // Only a few intrinsics implement a virtual dispatch.
duke@0 332 // They are expensive calls which are also frequently overridden.
duke@0 333 if (is_virtual) {
duke@0 334 switch (id) {
duke@0 335 case vmIntrinsics::_hashCode:
duke@0 336 case vmIntrinsics::_clone:
duke@0 337 // OK, Object.hashCode and Object.clone intrinsics come in both flavors
duke@0 338 break;
duke@0 339 default:
duke@0 340 return NULL;
duke@0 341 }
duke@0 342 }
duke@0 343
duke@0 344 // -XX:-InlineNatives disables nearly all intrinsics:
duke@0 345 if (!InlineNatives) {
duke@0 346 switch (id) {
duke@0 347 case vmIntrinsics::_indexOf:
duke@0 348 case vmIntrinsics::_compareTo:
cfang@681 349 case vmIntrinsics::_equals:
rasbold@169 350 case vmIntrinsics::_equalsC:
roland@3671 351 case vmIntrinsics::_getAndAddInt:
roland@3671 352 case vmIntrinsics::_getAndAddLong:
roland@3671 353 case vmIntrinsics::_getAndSetInt:
roland@3671 354 case vmIntrinsics::_getAndSetLong:
roland@3671 355 case vmIntrinsics::_getAndSetObject:
kvn@3926 356 case vmIntrinsics::_loadFence:
kvn@3926 357 case vmIntrinsics::_storeFence:
kvn@3926 358 case vmIntrinsics::_fullFence:
duke@0 359 break; // InlineNatives does not control String.compareTo
kvn@3567 360 case vmIntrinsics::_Reference_get:
kvn@3567 361 break; // InlineNatives does not control Reference.get
duke@0 362 default:
duke@0 363 return NULL;
duke@0 364 }
duke@0 365 }
duke@0 366
kvn@3770 367 bool is_predicted = false;
shade@5363 368 bool does_virtual_dispatch = false;
kvn@3770 369
duke@0 370 switch (id) {
duke@0 371 case vmIntrinsics::_compareTo:
duke@0 372 if (!SpecialStringCompareTo) return NULL;
twisti@3878 373 if (!Matcher::match_rule_supported(Op_StrComp)) return NULL;
duke@0 374 break;
duke@0 375 case vmIntrinsics::_indexOf:
duke@0 376 if (!SpecialStringIndexOf) return NULL;
duke@0 377 break;
cfang@681 378 case vmIntrinsics::_equals:
cfang@681 379 if (!SpecialStringEquals) return NULL;
twisti@3878 380 if (!Matcher::match_rule_supported(Op_StrEquals)) return NULL;
cfang@681 381 break;
rasbold@169 382 case vmIntrinsics::_equalsC:
rasbold@169 383 if (!SpecialArraysEquals) return NULL;
twisti@3878 384 if (!Matcher::match_rule_supported(Op_AryEq)) return NULL;
rasbold@169 385 break;
duke@0 386 case vmIntrinsics::_arraycopy:
duke@0 387 if (!InlineArrayCopy) return NULL;
duke@0 388 break;
duke@0 389 case vmIntrinsics::_copyMemory:
duke@0 390 if (StubRoutines::unsafe_arraycopy() == NULL) return NULL;
duke@0 391 if (!InlineArrayCopy) return NULL;
duke@0 392 break;
duke@0 393 case vmIntrinsics::_hashCode:
duke@0 394 if (!InlineObjectHash) return NULL;
shade@5363 395 does_virtual_dispatch = true;
duke@0 396 break;
duke@0 397 case vmIntrinsics::_clone:
shade@5363 398 does_virtual_dispatch = true;
duke@0 399 case vmIntrinsics::_copyOf:
duke@0 400 case vmIntrinsics::_copyOfRange:
duke@0 401 if (!InlineObjectCopy) return NULL;
duke@0 402 // These also use the arraycopy intrinsic mechanism:
duke@0 403 if (!InlineArrayCopy) return NULL;
duke@0 404 break;
kvn@4044 405 case vmIntrinsics::_encodeISOArray:
kvn@4044 406 if (!SpecialEncodeISOArray) return NULL;
kvn@4044 407 if (!Matcher::match_rule_supported(Op_EncodeISOArray)) return NULL;
kvn@4044 408 break;
duke@0 409 case vmIntrinsics::_checkIndex:
duke@0 410 // We do not intrinsify this. The optimizer does fine with it.
duke@0 411 return NULL;
duke@0 412
duke@0 413 case vmIntrinsics::_getCallerClass:
duke@0 414 if (!UseNewReflection) return NULL;
duke@0 415 if (!InlineReflectionGetCallerClass) return NULL;
twisti@4431 416 if (SystemDictionary::reflect_CallerSensitive_klass() == NULL) return NULL;
duke@0 417 break;
duke@0 418
twisti@643 419 case vmIntrinsics::_bitCount_i:
never@3202 420 if (!Matcher::match_rule_supported(Op_PopCountI)) return NULL;
never@3196 421 break;
never@3196 422
twisti@643 423 case vmIntrinsics::_bitCount_l:
never@3202 424 if (!Matcher::match_rule_supported(Op_PopCountL)) return NULL;
never@3196 425 break;
never@3196 426
never@3196 427 case vmIntrinsics::_numberOfLeadingZeros_i:
never@3196 428 if (!Matcher::match_rule_supported(Op_CountLeadingZerosI)) return NULL;
never@3196 429 break;
never@3196 430
never@3196 431 case vmIntrinsics::_numberOfLeadingZeros_l:
never@3196 432 if (!Matcher::match_rule_supported(Op_CountLeadingZerosL)) return NULL;
never@3196 433 break;
never@3196 434
never@3196 435 case vmIntrinsics::_numberOfTrailingZeros_i:
never@3196 436 if (!Matcher::match_rule_supported(Op_CountTrailingZerosI)) return NULL;
never@3196 437 break;
never@3196 438
never@3196 439 case vmIntrinsics::_numberOfTrailingZeros_l:
never@3196 440 if (!Matcher::match_rule_supported(Op_CountTrailingZerosL)) return NULL;
twisti@643 441 break;
twisti@643 442
twisti@3878 443 case vmIntrinsics::_reverseBytes_c:
roland@3922 444 if (!Matcher::match_rule_supported(Op_ReverseBytesUS)) return NULL;
twisti@3878 445 break;
twisti@3878 446 case vmIntrinsics::_reverseBytes_s:
roland@3922 447 if (!Matcher::match_rule_supported(Op_ReverseBytesS)) return NULL;
twisti@3878 448 break;
twisti@3878 449 case vmIntrinsics::_reverseBytes_i:
roland@3922 450 if (!Matcher::match_rule_supported(Op_ReverseBytesI)) return NULL;
twisti@3878 451 break;
twisti@3878 452 case vmIntrinsics::_reverseBytes_l:
roland@3922 453 if (!Matcher::match_rule_supported(Op_ReverseBytesL)) return NULL;
twisti@3878 454 break;
twisti@3878 455
johnc@2346 456 case vmIntrinsics::_Reference_get:
kvn@3567 457 // Use the intrinsic version of Reference.get() so that the value in
kvn@3567 458 // the referent field can be registered by the G1 pre-barrier code.
kvn@3567 459 // Also add memory barrier to prevent commoning reads from this field
kvn@3567 460 // across safepoint since GC can change it value.
johnc@2346 461 break;
johnc@2346 462
roland@3671 463 case vmIntrinsics::_compareAndSwapObject:
roland@3671 464 #ifdef _LP64
roland@3671 465 if (!UseCompressedOops && !Matcher::match_rule_supported(Op_CompareAndSwapP)) return NULL;
roland@3671 466 #endif
roland@3671 467 break;
roland@3671 468
roland@3671 469 case vmIntrinsics::_compareAndSwapLong:
roland@3671 470 if (!Matcher::match_rule_supported(Op_CompareAndSwapL)) return NULL;
roland@3671 471 break;
roland@3671 472
roland@3671 473 case vmIntrinsics::_getAndAddInt:
roland@3671 474 if (!Matcher::match_rule_supported(Op_GetAndAddI)) return NULL;
roland@3671 475 break;
roland@3671 476
roland@3671 477 case vmIntrinsics::_getAndAddLong:
roland@3671 478 if (!Matcher::match_rule_supported(Op_GetAndAddL)) return NULL;
roland@3671 479 break;
roland@3671 480
roland@3671 481 case vmIntrinsics::_getAndSetInt:
roland@3671 482 if (!Matcher::match_rule_supported(Op_GetAndSetI)) return NULL;
roland@3671 483 break;
roland@3671 484
roland@3671 485 case vmIntrinsics::_getAndSetLong:
roland@3671 486 if (!Matcher::match_rule_supported(Op_GetAndSetL)) return NULL;
roland@3671 487 break;
roland@3671 488
roland@3671 489 case vmIntrinsics::_getAndSetObject:
roland@3671 490 #ifdef _LP64
roland@3671 491 if (!UseCompressedOops && !Matcher::match_rule_supported(Op_GetAndSetP)) return NULL;
roland@3671 492 if (UseCompressedOops && !Matcher::match_rule_supported(Op_GetAndSetN)) return NULL;
roland@3671 493 break;
roland@3671 494 #else
roland@3671 495 if (!Matcher::match_rule_supported(Op_GetAndSetP)) return NULL;
roland@3671 496 break;
roland@3671 497 #endif
roland@3671 498
kvn@3770 499 case vmIntrinsics::_aescrypt_encryptBlock:
kvn@3770 500 case vmIntrinsics::_aescrypt_decryptBlock:
kvn@3770 501 if (!UseAESIntrinsics) return NULL;
kvn@3770 502 break;
kvn@3770 503
kvn@3770 504 case vmIntrinsics::_cipherBlockChaining_encryptAESCrypt:
kvn@3770 505 case vmIntrinsics::_cipherBlockChaining_decryptAESCrypt:
kvn@3770 506 if (!UseAESIntrinsics) return NULL;
kvn@3770 507 // these two require the predicated logic
kvn@3770 508 is_predicted = true;
kvn@3770 509 break;
kvn@3770 510
drchase@4918 511 case vmIntrinsics::_updateCRC32:
drchase@4918 512 case vmIntrinsics::_updateBytesCRC32:
drchase@4918 513 case vmIntrinsics::_updateByteBufferCRC32:
drchase@4918 514 if (!UseCRC32Intrinsics) return NULL;
drchase@4918 515 break;
drchase@4918 516
rbackman@5562 517 case vmIntrinsics::_incrementExactI:
rbackman@5562 518 case vmIntrinsics::_addExactI:
rbackman@5562 519 if (!Matcher::match_rule_supported(Op_AddExactI) || !UseMathExactIntrinsics) return NULL;
rbackman@5562 520 break;
rbackman@5562 521 case vmIntrinsics::_incrementExactL:
rbackman@5562 522 case vmIntrinsics::_addExactL:
rbackman@5562 523 if (!Matcher::match_rule_supported(Op_AddExactL) || !UseMathExactIntrinsics) return NULL;
rbackman@5562 524 break;
rbackman@5562 525 case vmIntrinsics::_decrementExactI:
rbackman@5562 526 case vmIntrinsics::_subtractExactI:
rbackman@5562 527 if (!Matcher::match_rule_supported(Op_SubExactI) || !UseMathExactIntrinsics) return NULL;
rbackman@5562 528 break;
rbackman@5562 529 case vmIntrinsics::_decrementExactL:
rbackman@5562 530 case vmIntrinsics::_subtractExactL:
rbackman@5562 531 if (!Matcher::match_rule_supported(Op_SubExactL) || !UseMathExactIntrinsics) return NULL;
rbackman@5562 532 break;
rbackman@5562 533 case vmIntrinsics::_negateExactI:
rbackman@5562 534 if (!Matcher::match_rule_supported(Op_NegExactI) || !UseMathExactIntrinsics) return NULL;
rbackman@5562 535 break;
rbackman@5562 536 case vmIntrinsics::_negateExactL:
rbackman@5562 537 if (!Matcher::match_rule_supported(Op_NegExactL) || !UseMathExactIntrinsics) return NULL;
rbackman@5562 538 break;
rbackman@5562 539 case vmIntrinsics::_multiplyExactI:
rbackman@5562 540 if (!Matcher::match_rule_supported(Op_MulExactI) || !UseMathExactIntrinsics) return NULL;
rbackman@5562 541 break;
rbackman@5562 542 case vmIntrinsics::_multiplyExactL:
rbackman@5562 543 if (!Matcher::match_rule_supported(Op_MulExactL) || !UseMathExactIntrinsics) return NULL;
rbackman@5356 544 break;
rbackman@5356 545
duke@0 546 default:
jrose@856 547 assert(id <= vmIntrinsics::LAST_COMPILER_INLINE, "caller responsibility");
jrose@856 548 assert(id != vmIntrinsics::_Object_init && id != vmIntrinsics::_invoke, "enum out of order?");
duke@0 549 break;
duke@0 550 }
duke@0 551
duke@0 552 // -XX:-InlineClassNatives disables natives from the Class class.
duke@0 553 // The flag applies to all reflective calls, notably Array.newArray
duke@0 554 // (visible to Java programmers as Array.newInstance).
duke@0 555 if (m->holder()->name() == ciSymbol::java_lang_Class() ||
duke@0 556 m->holder()->name() == ciSymbol::java_lang_reflect_Array()) {
duke@0 557 if (!InlineClassNatives) return NULL;
duke@0 558 }
duke@0 559
duke@0 560 // -XX:-InlineThreadNatives disables natives from the Thread class.
duke@0 561 if (m->holder()->name() == ciSymbol::java_lang_Thread()) {
duke@0 562 if (!InlineThreadNatives) return NULL;
duke@0 563 }
duke@0 564
duke@0 565 // -XX:-InlineMathNatives disables natives from the Math,Float and Double classes.
duke@0 566 if (m->holder()->name() == ciSymbol::java_lang_Math() ||
duke@0 567 m->holder()->name() == ciSymbol::java_lang_Float() ||
duke@0 568 m->holder()->name() == ciSymbol::java_lang_Double()) {
duke@0 569 if (!InlineMathNatives) return NULL;
duke@0 570 }
duke@0 571
duke@0 572 // -XX:-InlineUnsafeOps disables natives from the Unsafe class.
duke@0 573 if (m->holder()->name() == ciSymbol::sun_misc_Unsafe()) {
duke@0 574 if (!InlineUnsafeOps) return NULL;
duke@0 575 }
duke@0 576
shade@5363 577 return new LibraryIntrinsic(m, is_virtual, is_predicted, does_virtual_dispatch, (vmIntrinsics::ID) id);
duke@0 578 }
duke@0 579
duke@0 580 //----------------------register_library_intrinsics-----------------------
duke@0 581 // Initialize this file's data structures, for each Compile instance.
duke@0 582 void Compile::register_library_intrinsics() {
duke@0 583 // Nothing to do here.
duke@0 584 }
duke@0 585
roland@5546 586 JVMState* LibraryIntrinsic::generate(JVMState* jvms, Parse* parent_parser) {
duke@0 587 LibraryCallKit kit(jvms, this);
duke@0 588 Compile* C = kit.C;
duke@0 589 int nodes = C->unique();
duke@0 590 #ifndef PRODUCT
kvn@5328 591 if ((C->print_intrinsics() || C->print_inlining()) && Verbose) {
duke@0 592 char buf[1000];
duke@0 593 const char* str = vmIntrinsics::short_name_as_C_string(intrinsic_id(), buf, sizeof(buf));
duke@0 594 tty->print_cr("Intrinsic %s", str);
duke@0 595 }
duke@0 596 #endif
twisti@3878 597 ciMethod* callee = kit.callee();
twisti@3878 598 const int bci = kit.bci();
twisti@3878 599
twisti@3878 600 // Try to inline the intrinsic.
duke@0 601 if (kit.try_to_inline()) {
kvn@5328 602 if (C->print_intrinsics() || C->print_inlining()) {
roland@3922 603 C->print_inlining(callee, jvms->depth() - 1, bci, is_virtual() ? "(intrinsic, virtual)" : "(intrinsic)");
duke@0 604 }
duke@0 605 C->gather_intrinsic_statistics(intrinsic_id(), is_virtual(), Compile::_intrinsic_worked);
duke@0 606 if (C->log()) {
duke@0 607 C->log()->elem("intrinsic id='%s'%s nodes='%d'",
duke@0 608 vmIntrinsics::name_at(intrinsic_id()),
duke@0 609 (is_virtual() ? " virtual='1'" : ""),
duke@0 610 C->unique() - nodes);
duke@0 611 }
twisti@3878 612 // Push the result from the inlined method onto the stack.
twisti@3878 613 kit.push_result();
duke@0 614 return kit.transfer_exceptions_into_jvms();
duke@0 615 }
duke@0 616
never@3196 617 // The intrinsic bailed out
kvn@5328 618 if (C->print_intrinsics() || C->print_inlining()) {
johnc@2346 619 if (jvms->has_method()) {
johnc@2346 620 // Not a root compile.
never@3196 621 const char* msg = is_virtual() ? "failed to inline (intrinsic, virtual)" : "failed to inline (intrinsic)";
roland@3922 622 C->print_inlining(callee, jvms->depth() - 1, bci, msg);
johnc@2346 623 } else {
johnc@2346 624 // Root compile
johnc@2346 625 tty->print("Did not generate intrinsic %s%s at bci:%d in",
jrose@856 626 vmIntrinsics::name_at(intrinsic_id()),
twisti@3878 627 (is_virtual() ? " (virtual)" : ""), bci);
johnc@2346 628 }
duke@0 629 }
duke@0 630 C->gather_intrinsic_statistics(intrinsic_id(), is_virtual(), Compile::_intrinsic_failed);
duke@0 631 return NULL;
duke@0 632 }
duke@0 633
kvn@3770 634 Node* LibraryIntrinsic::generate_predicate(JVMState* jvms) {
kvn@3770 635 LibraryCallKit kit(jvms, this);
kvn@3770 636 Compile* C = kit.C;
kvn@3770 637 int nodes = C->unique();
kvn@3770 638 #ifndef PRODUCT
kvn@3770 639 assert(is_predicted(), "sanity");
kvn@5328 640 if ((C->print_intrinsics() || C->print_inlining()) && Verbose) {
kvn@3770 641 char buf[1000];
kvn@3770 642 const char* str = vmIntrinsics::short_name_as_C_string(intrinsic_id(), buf, sizeof(buf));
kvn@3770 643 tty->print_cr("Predicate for intrinsic %s", str);
kvn@3770 644 }
kvn@3770 645 #endif
twisti@3878 646 ciMethod* callee = kit.callee();
twisti@3878 647 const int bci = kit.bci();
kvn@3770 648
kvn@3770 649 Node* slow_ctl = kit.try_to_predicate();
kvn@3770 650 if (!kit.failing()) {
kvn@5328 651 if (C->print_intrinsics() || C->print_inlining()) {
roland@3922 652 C->print_inlining(callee, jvms->depth() - 1, bci, is_virtual() ? "(intrinsic, virtual)" : "(intrinsic)");
twisti@3878 653 }
twisti@3878 654 C->gather_intrinsic_statistics(intrinsic_id(), is_virtual(), Compile::_intrinsic_worked);
kvn@3770 655 if (C->log()) {
kvn@3770 656 C->log()->elem("predicate_intrinsic id='%s'%s nodes='%d'",
kvn@3770 657 vmIntrinsics::name_at(intrinsic_id()),
kvn@3770 658 (is_virtual() ? " virtual='1'" : ""),
kvn@3770 659 C->unique() - nodes);
kvn@3770 660 }
kvn@3770 661 return slow_ctl; // Could be NULL if the check folds.
kvn@3770 662 }
kvn@3770 663
kvn@3770 664 // The intrinsic bailed out
kvn@5328 665 if (C->print_intrinsics() || C->print_inlining()) {
kvn@3770 666 if (jvms->has_method()) {
kvn@3770 667 // Not a root compile.
kvn@3770 668 const char* msg = "failed to generate predicate for intrinsic";
roland@3922 669 C->print_inlining(kit.callee(), jvms->depth() - 1, bci, msg);
kvn@3770 670 } else {
kvn@3770 671 // Root compile
roland@3922 672 C->print_inlining_stream()->print("Did not generate predicate for intrinsic %s%s at bci:%d in",
roland@3922 673 vmIntrinsics::name_at(intrinsic_id()),
roland@3922 674 (is_virtual() ? " (virtual)" : ""), bci);
kvn@3770 675 }
kvn@3770 676 }
kvn@3770 677 C->gather_intrinsic_statistics(intrinsic_id(), is_virtual(), Compile::_intrinsic_failed);
kvn@3770 678 return NULL;
kvn@3770 679 }
kvn@3770 680
duke@0 681 bool LibraryCallKit::try_to_inline() {
duke@0 682 // Handle symbolic names for otherwise undistinguished boolean switches:
duke@0 683 const bool is_store = true;
duke@0 684 const bool is_native_ptr = true;
duke@0 685 const bool is_static = true;
twisti@3878 686 const bool is_volatile = true;
duke@0 687
johnc@2346 688 if (!jvms()->has_method()) {
johnc@2346 689 // Root JVMState has a null method.
johnc@2346 690 assert(map()->memory()->Opcode() == Op_Parm, "");
johnc@2346 691 // Insert the memory aliasing node
johnc@2346 692 set_all_memory(reset_memory());
johnc@2346 693 }
johnc@2346 694 assert(merged_memory(), "");
johnc@2346 695
twisti@3878 696
duke@0 697 switch (intrinsic_id()) {
twisti@3878 698 case vmIntrinsics::_hashCode: return inline_native_hashcode(intrinsic()->is_virtual(), !is_static);
twisti@3878 699 case vmIntrinsics::_identityHashCode: return inline_native_hashcode(/*!virtual*/ false, is_static);
twisti@3878 700 case vmIntrinsics::_getClass: return inline_native_getClass();
duke@0 701
duke@0 702 case vmIntrinsics::_dsin:
duke@0 703 case vmIntrinsics::_dcos:
duke@0 704 case vmIntrinsics::_dtan:
duke@0 705 case vmIntrinsics::_dabs:
duke@0 706 case vmIntrinsics::_datan2:
duke@0 707 case vmIntrinsics::_dsqrt:
duke@0 708 case vmIntrinsics::_dexp:
duke@0 709 case vmIntrinsics::_dlog:
duke@0 710 case vmIntrinsics::_dlog10:
twisti@3878 711 case vmIntrinsics::_dpow: return inline_math_native(intrinsic_id());
duke@0 712
duke@0 713 case vmIntrinsics::_min:
twisti@3878 714 case vmIntrinsics::_max: return inline_min_max(intrinsic_id());
twisti@3878 715
rbackman@5562 716 case vmIntrinsics::_addExactI: return inline_math_addExactI(false /* add */);
rbackman@5562 717 case vmIntrinsics::_addExactL: return inline_math_addExactL(false /* add */);
rbackman@5562 718 case vmIntrinsics::_decrementExactI: return inline_math_subtractExactI(true /* decrement */);
rbackman@5562 719 case vmIntrinsics::_decrementExactL: return inline_math_subtractExactL(true /* decrement */);
rbackman@5562 720 case vmIntrinsics::_incrementExactI: return inline_math_addExactI(true /* increment */);
rbackman@5562 721 case vmIntrinsics::_incrementExactL: return inline_math_addExactL(true /* increment */);
rbackman@5562 722 case vmIntrinsics::_multiplyExactI: return inline_math_multiplyExactI();
rbackman@5562 723 case vmIntrinsics::_multiplyExactL: return inline_math_multiplyExactL();
rbackman@5562 724 case vmIntrinsics::_negateExactI: return inline_math_negateExactI();
rbackman@5562 725 case vmIntrinsics::_negateExactL: return inline_math_negateExactL();
rbackman@5562 726 case vmIntrinsics::_subtractExactI: return inline_math_subtractExactI(false /* subtract */);
rbackman@5562 727 case vmIntrinsics::_subtractExactL: return inline_math_subtractExactL(false /* subtract */);
rbackman@5356 728
twisti@3878 729 case vmIntrinsics::_arraycopy: return inline_arraycopy();
twisti@3878 730
twisti@3878 731 case vmIntrinsics::_compareTo: return inline_string_compareTo();
twisti@3878 732 case vmIntrinsics::_indexOf: return inline_string_indexOf();
twisti@3878 733 case vmIntrinsics::_equals: return inline_string_equals();
twisti@3878 734
twisti@3878 735 case vmIntrinsics::_getObject: return inline_unsafe_access(!is_native_ptr, !is_store, T_OBJECT, !is_volatile);
twisti@3878 736 case vmIntrinsics::_getBoolean: return inline_unsafe_access(!is_native_ptr, !is_store, T_BOOLEAN, !is_volatile);
twisti@3878 737 case vmIntrinsics::_getByte: return inline_unsafe_access(!is_native_ptr, !is_store, T_BYTE, !is_volatile);
twisti@3878 738 case vmIntrinsics::_getShort: return inline_unsafe_access(!is_native_ptr, !is_store, T_SHORT, !is_volatile);
twisti@3878 739 case vmIntrinsics::_getChar: return inline_unsafe_access(!is_native_ptr, !is_store, T_CHAR, !is_volatile);
twisti@3878 740 case vmIntrinsics::_getInt: return inline_unsafe_access(!is_native_ptr, !is_store, T_INT, !is_volatile);
twisti@3878 741 case vmIntrinsics::_getLong: return inline_unsafe_access(!is_native_ptr, !is_store, T_LONG, !is_volatile);
twisti@3878 742 case vmIntrinsics::_getFloat: return inline_unsafe_access(!is_native_ptr, !is_store, T_FLOAT, !is_volatile);
twisti@3878 743 case vmIntrinsics::_getDouble: return inline_unsafe_access(!is_native_ptr, !is_store, T_DOUBLE, !is_volatile);
twisti@3878 744
twisti@3878 745 case vmIntrinsics::_putObject: return inline_unsafe_access(!is_native_ptr, is_store, T_OBJECT, !is_volatile);
twisti@3878 746 case vmIntrinsics::_putBoolean: return inline_unsafe_access(!is_native_ptr, is_store, T_BOOLEAN, !is_volatile);
twisti@3878 747 case vmIntrinsics::_putByte: return inline_unsafe_access(!is_native_ptr, is_store, T_BYTE, !is_volatile);
twisti@3878 748 case vmIntrinsics::_putShort: return inline_unsafe_access(!is_native_ptr, is_store, T_SHORT, !is_volatile);
twisti@3878 749 case vmIntrinsics::_putChar: return inline_unsafe_access(!is_native_ptr, is_store, T_CHAR, !is_volatile);
twisti@3878 750 case vmIntrinsics::_putInt: return inline_unsafe_access(!is_native_ptr, is_store, T_INT, !is_volatile);
twisti@3878 751 case vmIntrinsics::_putLong: return inline_unsafe_access(!is_native_ptr, is_store, T_LONG, !is_volatile);
twisti@3878 752 case vmIntrinsics::_putFloat: return inline_unsafe_access(!is_native_ptr, is_store, T_FLOAT, !is_volatile);
twisti@3878 753 case vmIntrinsics::_putDouble: return inline_unsafe_access(!is_native_ptr, is_store, T_DOUBLE, !is_volatile);
twisti@3878 754
twisti@3878 755 case vmIntrinsics::_getByte_raw: return inline_unsafe_access( is_native_ptr, !is_store, T_BYTE, !is_volatile);
twisti@3878 756 case vmIntrinsics::_getShort_raw: return inline_unsafe_access( is_native_ptr, !is_store, T_SHORT, !is_volatile);
twisti@3878 757 case vmIntrinsics::_getChar_raw: return inline_unsafe_access( is_native_ptr, !is_store, T_CHAR, !is_volatile);
twisti@3878 758 case vmIntrinsics::_getInt_raw: return inline_unsafe_access( is_native_ptr, !is_store, T_INT, !is_volatile);
twisti@3878 759 case vmIntrinsics::_getLong_raw: return inline_unsafe_access( is_native_ptr, !is_store, T_LONG, !is_volatile);
twisti@3878 760 case vmIntrinsics::_getFloat_raw: return inline_unsafe_access( is_native_ptr, !is_store, T_FLOAT, !is_volatile);
twisti@3878 761 case vmIntrinsics::_getDouble_raw: return inline_unsafe_access( is_native_ptr, !is_store, T_DOUBLE, !is_volatile);
twisti@3878 762 case vmIntrinsics::_getAddress_raw: return inline_unsafe_access( is_native_ptr, !is_store, T_ADDRESS, !is_volatile);
twisti@3878 763
twisti@3878 764 case vmIntrinsics::_putByte_raw: return inline_unsafe_access( is_native_ptr, is_store, T_BYTE, !is_volatile);
twisti@3878 765 case vmIntrinsics::_putShort_raw: return inline_unsafe_access( is_native_ptr, is_store, T_SHORT, !is_volatile);
twisti@3878 766 case vmIntrinsics::_putChar_raw: return inline_unsafe_access( is_native_ptr, is_store, T_CHAR, !is_volatile);
twisti@3878 767 case vmIntrinsics::_putInt_raw: return inline_unsafe_access( is_native_ptr, is_store, T_INT, !is_volatile);
twisti@3878 768 case vmIntrinsics::_putLong_raw: return inline_unsafe_access( is_native_ptr, is_store, T_LONG, !is_volatile);
twisti@3878 769 case vmIntrinsics::_putFloat_raw: return inline_unsafe_access( is_native_ptr, is_store, T_FLOAT, !is_volatile);
twisti@3878 770 case vmIntrinsics::_putDouble_raw: return inline_unsafe_access( is_native_ptr, is_store, T_DOUBLE, !is_volatile);
twisti@3878 771 case vmIntrinsics::_putAddress_raw: return inline_unsafe_access( is_native_ptr, is_store, T_ADDRESS, !is_volatile);
twisti@3878 772
twisti@3878 773 case vmIntrinsics::_getObjectVolatile: return inline_unsafe_access(!is_native_ptr, !is_store, T_OBJECT, is_volatile);
twisti@3878 774 case vmIntrinsics::_getBooleanVolatile: return inline_unsafe_access(!is_native_ptr, !is_store, T_BOOLEAN, is_volatile);
twisti@3878 775 case vmIntrinsics::_getByteVolatile: return inline_unsafe_access(!is_native_ptr, !is_store, T_BYTE, is_volatile);
twisti@3878 776 case vmIntrinsics::_getShortVolatile: return inline_unsafe_access(!is_native_ptr, !is_store, T_SHORT, is_volatile);
twisti@3878 777 case vmIntrinsics::_getCharVolatile: return inline_unsafe_access(!is_native_ptr, !is_store, T_CHAR, is_volatile);
twisti@3878 778 case vmIntrinsics::_getIntVolatile: return inline_unsafe_access(!is_native_ptr, !is_store, T_INT, is_volatile);
twisti@3878 779 case vmIntrinsics::_getLongVolatile: return inline_unsafe_access(!is_native_ptr, !is_store, T_LONG, is_volatile);
twisti@3878 780 case vmIntrinsics::_getFloatVolatile: return inline_unsafe_access(!is_native_ptr, !is_store, T_FLOAT, is_volatile);
twisti@3878 781 case vmIntrinsics::_getDoubleVolatile: return inline_unsafe_access(!is_native_ptr, !is_store, T_DOUBLE, is_volatile);
twisti@3878 782
twisti@3878 783 case vmIntrinsics::_putObjectVolatile: return inline_unsafe_access(!is_native_ptr, is_store, T_OBJECT, is_volatile);
twisti@3878 784 case vmIntrinsics::_putBooleanVolatile: return inline_unsafe_access(!is_native_ptr, is_store, T_BOOLEAN, is_volatile);
twisti@3878 785 case vmIntrinsics::_putByteVolatile: return inline_unsafe_access(!is_native_ptr, is_store, T_BYTE, is_volatile);
twisti@3878 786 case vmIntrinsics::_putShortVolatile: return inline_unsafe_access(!is_native_ptr, is_store, T_SHORT, is_volatile);
twisti@3878 787 case vmIntrinsics::_putCharVolatile: return inline_unsafe_access(!is_native_ptr, is_store, T_CHAR, is_volatile);
twisti@3878 788 case vmIntrinsics::_putIntVolatile: return inline_unsafe_access(!is_native_ptr, is_store, T_INT, is_volatile);
twisti@3878 789 case vmIntrinsics::_putLongVolatile: return inline_unsafe_access(!is_native_ptr, is_store, T_LONG, is_volatile);
twisti@3878 790 case vmIntrinsics::_putFloatVolatile: return inline_unsafe_access(!is_native_ptr, is_store, T_FLOAT, is_volatile);
twisti@3878 791 case vmIntrinsics::_putDoubleVolatile: return inline_unsafe_access(!is_native_ptr, is_store, T_DOUBLE, is_volatile);
twisti@3878 792
twisti@3878 793 case vmIntrinsics::_prefetchRead: return inline_unsafe_prefetch(!is_native_ptr, !is_store, !is_static);
twisti@3878 794 case vmIntrinsics::_prefetchWrite: return inline_unsafe_prefetch(!is_native_ptr, is_store, !is_static);
twisti@3878 795 case vmIntrinsics::_prefetchReadStatic: return inline_unsafe_prefetch(!is_native_ptr, !is_store, is_static);
twisti@3878 796 case vmIntrinsics::_prefetchWriteStatic: return inline_unsafe_prefetch(!is_native_ptr, is_store, is_static);
twisti@3878 797
twisti@3878 798 case vmIntrinsics::_compareAndSwapObject: return inline_unsafe_load_store(T_OBJECT, LS_cmpxchg);
twisti@3878 799 case vmIntrinsics::_compareAndSwapInt: return inline_unsafe_load_store(T_INT, LS_cmpxchg);
twisti@3878 800 case vmIntrinsics::_compareAndSwapLong: return inline_unsafe_load_store(T_LONG, LS_cmpxchg);
twisti@3878 801
twisti@3878 802 case vmIntrinsics::_putOrderedObject: return inline_unsafe_ordered_store(T_OBJECT);
twisti@3878 803 case vmIntrinsics::_putOrderedInt: return inline_unsafe_ordered_store(T_INT);
twisti@3878 804 case vmIntrinsics::_putOrderedLong: return inline_unsafe_ordered_store(T_LONG);
twisti@3878 805
twisti@3878 806 case vmIntrinsics::_getAndAddInt: return inline_unsafe_load_store(T_INT, LS_xadd);
twisti@3878 807 case vmIntrinsics::_getAndAddLong: return inline_unsafe_load_store(T_LONG, LS_xadd);
twisti@3878 808 case vmIntrinsics::_getAndSetInt: return inline_unsafe_load_store(T_INT, LS_xchg);
twisti@3878 809 case vmIntrinsics::_getAndSetLong: return inline_unsafe_load_store(T_LONG, LS_xchg);
twisti@3878 810 case vmIntrinsics::_getAndSetObject: return inline_unsafe_load_store(T_OBJECT, LS_xchg);
twisti@3878 811
kvn@3926 812 case vmIntrinsics::_loadFence:
kvn@3926 813 case vmIntrinsics::_storeFence:
kvn@3926 814 case vmIntrinsics::_fullFence: return inline_unsafe_fence(intrinsic_id());
kvn@3926 815
twisti@3878 816 case vmIntrinsics::_currentThread: return inline_native_currentThread();
twisti@3878 817 case vmIntrinsics::_isInterrupted: return inline_native_isInterrupted();
duke@0 818
rbackman@3274 819 #ifdef TRACE_HAVE_INTRINSICS
twisti@3878 820 case vmIntrinsics::_classID: return inline_native_classID();
twisti@3878 821 case vmIntrinsics::_threadID: return inline_native_threadID();
twisti@3878 822 case vmIntrinsics::_counterTime: return inline_native_time_funcs(CAST_FROM_FN_PTR(address, TRACE_TIME_METHOD), "counterTime");
rbackman@3274 823 #endif
twisti@3878 824 case vmIntrinsics::_currentTimeMillis: return inline_native_time_funcs(CAST_FROM_FN_PTR(address, os::javaTimeMillis), "currentTimeMillis");
twisti@3878 825 case vmIntrinsics::_nanoTime: return inline_native_time_funcs(CAST_FROM_FN_PTR(address, os::javaTimeNanos), "nanoTime");
twisti@3878 826 case vmIntrinsics::_allocateInstance: return inline_unsafe_allocate();
twisti@3878 827 case vmIntrinsics::_copyMemory: return inline_unsafe_copyMemory();
twisti@3878 828 case vmIntrinsics::_newArray: return inline_native_newArray();
twisti@3878 829 case vmIntrinsics::_getLength: return inline_native_getLength();
twisti@3878 830 case vmIntrinsics::_copyOf: return inline_array_copyOf(false);
twisti@3878 831 case vmIntrinsics::_copyOfRange: return inline_array_copyOf(true);
twisti@3878 832 case vmIntrinsics::_equalsC: return inline_array_equals();
twisti@3878 833 case vmIntrinsics::_clone: return inline_native_clone(intrinsic()->is_virtual());
twisti@3878 834
twisti@3878 835 case vmIntrinsics::_isAssignableFrom: return inline_native_subtype_check();
duke@0 836
duke@0 837 case vmIntrinsics::_isInstance:
duke@0 838 case vmIntrinsics::_getModifiers:
duke@0 839 case vmIntrinsics::_isInterface:
duke@0 840 case vmIntrinsics::_isArray:
duke@0 841 case vmIntrinsics::_isPrimitive:
duke@0 842 case vmIntrinsics::_getSuperclass:
duke@0 843 case vmIntrinsics::_getComponentType:
twisti@3878 844 case vmIntrinsics::_getClassAccessFlags: return inline_native_Class_query(intrinsic_id());
duke@0 845
duke@0 846 case vmIntrinsics::_floatToRawIntBits:
duke@0 847 case vmIntrinsics::_floatToIntBits:
duke@0 848 case vmIntrinsics::_intBitsToFloat:
duke@0 849 case vmIntrinsics::_doubleToRawLongBits:
duke@0 850 case vmIntrinsics::_doubleToLongBits:
twisti@3878 851 case vmIntrinsics::_longBitsToDouble: return inline_fp_conversions(intrinsic_id());
duke@0 852
twisti@775 853 case vmIntrinsics::_numberOfLeadingZeros_i:
twisti@775 854 case vmIntrinsics::_numberOfLeadingZeros_l:
twisti@775 855 case vmIntrinsics::_numberOfTrailingZeros_i:
twisti@775 856 case vmIntrinsics::_numberOfTrailingZeros_l:
twisti@643 857 case vmIntrinsics::_bitCount_i:
twisti@643 858 case vmIntrinsics::_bitCount_l:
duke@0 859 case vmIntrinsics::_reverseBytes_i:
duke@0 860 case vmIntrinsics::_reverseBytes_l:
never@1396 861 case vmIntrinsics::_reverseBytes_s:
twisti@3878 862 case vmIntrinsics::_reverseBytes_c: return inline_number_methods(intrinsic_id());
twisti@3878 863
twisti@3878 864 case vmIntrinsics::_getCallerClass: return inline_native_Reflection_getCallerClass();
twisti@3878 865
twisti@3878 866 case vmIntrinsics::_Reference_get: return inline_reference_get();
johnc@2346 867
kvn@3770 868 case vmIntrinsics::_aescrypt_encryptBlock:
twisti@3878 869 case vmIntrinsics::_aescrypt_decryptBlock: return inline_aescrypt_Block(intrinsic_id());
kvn@3770 870
kvn@3770 871 case vmIntrinsics::_cipherBlockChaining_encryptAESCrypt:
kvn@3770 872 case vmIntrinsics::_cipherBlockChaining_decryptAESCrypt:
kvn@3770 873 return inline_cipherBlockChaining_AESCrypt(intrinsic_id());
kvn@3770 874
kvn@4044 875 case vmIntrinsics::_encodeISOArray:
kvn@4044 876 return inline_encodeISOArray();
kvn@4044 877
drchase@4918 878 case vmIntrinsics::_updateCRC32:
drchase@4918 879 return inline_updateCRC32();
drchase@4918 880 case vmIntrinsics::_updateBytesCRC32:
drchase@4918 881 return inline_updateBytesCRC32();
drchase@4918 882 case vmIntrinsics::_updateByteBufferCRC32:
drchase@4918 883 return inline_updateByteBufferCRC32();
drchase@4918 884
duke@0 885 default:
duke@0 886 // If you get here, it may be that someone has added a new intrinsic
duke@0 887 // to the list in vmSymbols.hpp without implementing it here.
duke@0 888 #ifndef PRODUCT
duke@0 889 if ((PrintMiscellaneous && (Verbose || WizardMode)) || PrintOpto) {
duke@0 890 tty->print_cr("*** Warning: Unimplemented intrinsic %s(%d)",
duke@0 891 vmIntrinsics::name_at(intrinsic_id()), intrinsic_id());
duke@0 892 }
duke@0 893 #endif
duke@0 894 return false;
duke@0 895 }
duke@0 896 }
duke@0 897
kvn@3770 898 Node* LibraryCallKit::try_to_predicate() {
kvn@3770 899 if (!jvms()->has_method()) {
kvn@3770 900 // Root JVMState has a null method.
kvn@3770 901 assert(map()->memory()->Opcode() == Op_Parm, "");
kvn@3770 902 // Insert the memory aliasing node
kvn@3770 903 set_all_memory(reset_memory());
kvn@3770 904 }
kvn@3770 905 assert(merged_memory(), "");
kvn@3770 906
kvn@3770 907 switch (intrinsic_id()) {
kvn@3770 908 case vmIntrinsics::_cipherBlockChaining_encryptAESCrypt:
kvn@3770 909 return inline_cipherBlockChaining_AESCrypt_predicate(false);
kvn@3770 910 case vmIntrinsics::_cipherBlockChaining_decryptAESCrypt:
kvn@3770 911 return inline_cipherBlockChaining_AESCrypt_predicate(true);
kvn@3770 912
kvn@3770 913 default:
kvn@3770 914 // If you get here, it may be that someone has added a new intrinsic
kvn@3770 915 // to the list in vmSymbols.hpp without implementing it here.
kvn@3770 916 #ifndef PRODUCT
kvn@3770 917 if ((PrintMiscellaneous && (Verbose || WizardMode)) || PrintOpto) {
kvn@3770 918 tty->print_cr("*** Warning: Unimplemented predicate for intrinsic %s(%d)",
kvn@3770 919 vmIntrinsics::name_at(intrinsic_id()), intrinsic_id());
kvn@3770 920 }
kvn@3770 921 #endif
kvn@3770 922 Node* slow_ctl = control();
kvn@3770 923 set_control(top()); // No fast path instrinsic
kvn@3770 924 return slow_ctl;
kvn@3770 925 }
kvn@3770 926 }
kvn@3770 927
twisti@3878 928 //------------------------------set_result-------------------------------
duke@0 929 // Helper function for finishing intrinsics.
twisti@3878 930 void LibraryCallKit::set_result(RegionNode* region, PhiNode* value) {
duke@0 931 record_for_igvn(region);
duke@0 932 set_control(_gvn.transform(region));
twisti@3878 933 set_result( _gvn.transform(value));
twisti@3878 934 assert(value->type()->basic_type() == result()->bottom_type()->basic_type(), "sanity");
duke@0 935 }
duke@0 936
duke@0 937 //------------------------------generate_guard---------------------------
duke@0 938 // Helper function for generating guarded fast-slow graph structures.
duke@0 939 // The given 'test', if true, guards a slow path. If the test fails
duke@0 940 // then a fast path can be taken. (We generally hope it fails.)
duke@0 941 // In all cases, GraphKit::control() is updated to the fast path.
duke@0 942 // The returned value represents the control for the slow path.
duke@0 943 // The return value is never 'top'; it is either a valid control
duke@0 944 // or NULL if it is obvious that the slow path can never be taken.
duke@0 945 // Also, if region and the slow control are not NULL, the slow edge
duke@0 946 // is appended to the region.
duke@0 947 Node* LibraryCallKit::generate_guard(Node* test, RegionNode* region, float true_prob) {
duke@0 948 if (stopped()) {
duke@0 949 // Already short circuited.
duke@0 950 return NULL;
duke@0 951 }
duke@0 952
duke@0 953 // Build an if node and its projections.
duke@0 954 // If test is true we take the slow path, which we assume is uncommon.
duke@0 955 if (_gvn.type(test) == TypeInt::ZERO) {
duke@0 956 // The slow branch is never taken. No need to build this guard.
duke@0 957 return NULL;
duke@0 958 }
duke@0 959
duke@0 960 IfNode* iff = create_and_map_if(control(), test, true_prob, COUNT_UNKNOWN);
duke@0 961
drchase@4918 962 Node* if_slow = _gvn.transform(new (C) IfTrueNode(iff));
duke@0 963 if (if_slow == top()) {
duke@0 964 // The slow branch is never taken. No need to build this guard.
duke@0 965 return NULL;
duke@0 966 }
duke@0 967
duke@0 968 if (region != NULL)
duke@0 969 region->add_req(if_slow);
duke@0 970
drchase@4918 971 Node* if_fast = _gvn.transform(new (C) IfFalseNode(iff));
duke@0 972 set_control(if_fast);
duke@0 973
duke@0 974 return if_slow;
duke@0 975 }
duke@0 976
duke@0 977 inline Node* LibraryCallKit::generate_slow_guard(Node* test, RegionNode* region) {
duke@0 978 return generate_guard(test, region, PROB_UNLIKELY_MAG(3));
duke@0 979 }
duke@0 980 inline Node* LibraryCallKit::generate_fair_guard(Node* test, RegionNode* region) {
duke@0 981 return generate_guard(test, region, PROB_FAIR);
duke@0 982 }
duke@0 983
duke@0 984 inline Node* LibraryCallKit::generate_negative_guard(Node* index, RegionNode* region,
duke@0 985 Node* *pos_index) {
duke@0 986 if (stopped())
duke@0 987 return NULL; // already stopped
duke@0 988 if (_gvn.type(index)->higher_equal(TypeInt::POS)) // [0,maxint]
duke@0 989 return NULL; // index is already adequately typed
drchase@4918 990 Node* cmp_lt = _gvn.transform(new (C) CmpINode(index, intcon(0)));
drchase@4918 991 Node* bol_lt = _gvn.transform(new (C) BoolNode(cmp_lt, BoolTest::lt));
duke@0 992 Node* is_neg = generate_guard(bol_lt, region, PROB_MIN);
duke@0 993 if (is_neg != NULL && pos_index != NULL) {
duke@0 994 // Emulate effect of Parse::adjust_map_after_if.
kvn@3680 995 Node* ccast = new (C) CastIINode(index, TypeInt::POS);
duke@0 996 ccast->set_req(0, control());
duke@0 997 (*pos_index) = _gvn.transform(ccast);
duke@0 998 }
duke@0 999 return is_neg;
duke@0 1000 }
duke@0 1001
duke@0 1002 inline Node* LibraryCallKit::generate_nonpositive_guard(Node* index, bool never_negative,
duke@0 1003 Node* *pos_index) {
duke@0 1004 if (stopped())
duke@0 1005 return NULL; // already stopped
duke@0 1006 if (_gvn.type(index)->higher_equal(TypeInt::POS1)) // [1,maxint]
duke@0 1007 return NULL; // index is already adequately typed
drchase@4918 1008 Node* cmp_le = _gvn.transform(new (C) CmpINode(index, intcon(0)));
duke@0 1009 BoolTest::mask le_or_eq = (never_negative ? BoolTest::eq : BoolTest::le);
drchase@4918 1010 Node* bol_le = _gvn.transform(new (C) BoolNode(cmp_le, le_or_eq));
duke@0 1011 Node* is_notp = generate_guard(bol_le, NULL, PROB_MIN);
duke@0 1012 if (is_notp != NULL && pos_index != NULL) {
duke@0 1013 // Emulate effect of Parse::adjust_map_after_if.
kvn@3680 1014 Node* ccast = new (C) CastIINode(index, TypeInt::POS1);
duke@0 1015 ccast->set_req(0, control());
duke@0 1016 (*pos_index) = _gvn.transform(ccast);
duke@0 1017 }
duke@0 1018 return is_notp;
duke@0 1019 }
duke@0 1020
duke@0 1021 // Make sure that 'position' is a valid limit index, in [0..length].
duke@0 1022 // There are two equivalent plans for checking this:
duke@0 1023 // A. (offset + copyLength) unsigned<= arrayLength
duke@0 1024 // B. offset <= (arrayLength - copyLength)
duke@0 1025 // We require that all of the values above, except for the sum and
duke@0 1026 // difference, are already known to be non-negative.
duke@0 1027 // Plan A is robust in the face of overflow, if offset and copyLength
duke@0 1028 // are both hugely positive.
duke@0 1029 //
duke@0 1030 // Plan B is less direct and intuitive, but it does not overflow at
duke@0 1031 // all, since the difference of two non-negatives is always
duke@0 1032 // representable. Whenever Java methods must perform the equivalent
duke@0 1033 // check they generally use Plan B instead of Plan A.
duke@0 1034 // For the moment we use Plan A.
duke@0 1035 inline Node* LibraryCallKit::generate_limit_guard(Node* offset,
duke@0 1036 Node* subseq_length,
duke@0 1037 Node* array_length,
duke@0 1038 RegionNode* region) {
duke@0 1039 if (stopped())
duke@0 1040 return NULL; // already stopped
duke@0 1041 bool zero_offset = _gvn.type(offset) == TypeInt::ZERO;
kvn@2972 1042 if (zero_offset && subseq_length->eqv_uncast(array_length))
duke@0 1043 return NULL; // common case of whole-array copy
duke@0 1044 Node* last = subseq_length;
duke@0 1045 if (!zero_offset) // last += offset
drchase@4918 1046 last = _gvn.transform(new (C) AddINode(last, offset));
drchase@4918 1047 Node* cmp_lt = _gvn.transform(new (C) CmpUNode(array_length, last));
drchase@4918 1048 Node* bol_lt = _gvn.transform(new (C) BoolNode(cmp_lt, BoolTest::lt));
duke@0 1049 Node* is_over = generate_guard(bol_lt, region, PROB_MIN);
duke@0 1050 return is_over;
duke@0 1051 }
duke@0 1052
duke@0 1053
duke@0 1054 //--------------------------generate_current_thread--------------------
duke@0 1055 Node* LibraryCallKit::generate_current_thread(Node* &tls_output) {
duke@0 1056 ciKlass* thread_klass = env()->Thread_klass();
duke@0 1057 const Type* thread_type = TypeOopPtr::make_from_klass(thread_klass)->cast_to_ptr_type(TypePtr::NotNull);
kvn@3680 1058 Node* thread = _gvn.transform(new (C) ThreadLocalNode());
duke@0 1059 Node* p = basic_plus_adr(top()/*!oop*/, thread, in_bytes(JavaThread::threadObj_offset()));
duke@0 1060 Node* threadObj = make_load(NULL, p, thread_type, T_OBJECT);
duke@0 1061 tls_output = thread;
duke@0 1062 return threadObj;
duke@0 1063 }
duke@0 1064
duke@0 1065
kvn@986 1066 //------------------------------make_string_method_node------------------------
kvn@3325 1067 // Helper method for String intrinsic functions. This version is called
kvn@3325 1068 // with str1 and str2 pointing to String object nodes.
kvn@3325 1069 //
kvn@3325 1070 Node* LibraryCallKit::make_string_method_node(int opcode, Node* str1, Node* str2) {
kvn@986 1071 Node* no_ctrl = NULL;
kvn@986 1072
kvn@3325 1073 // Get start addr of string
kvn@3325 1074 Node* str1_value = load_String_value(no_ctrl, str1);
kvn@3325 1075 Node* str1_offset = load_String_offset(no_ctrl, str1);
kvn@986 1076 Node* str1_start = array_element_address(str1_value, str1_offset, T_CHAR);
kvn@986 1077
kvn@3325 1078 // Get length of string 1
kvn@3325 1079 Node* str1_len = load_String_length(no_ctrl, str1);
kvn@3325 1080
kvn@3325 1081 Node* str2_value = load_String_value(no_ctrl, str2);
kvn@3325 1082 Node* str2_offset = load_String_offset(no_ctrl, str2);
kvn@986 1083 Node* str2_start = array_element_address(str2_value, str2_offset, T_CHAR);
kvn@986 1084
kvn@3325 1085 Node* str2_len = NULL;
kvn@986 1086 Node* result = NULL;
kvn@3325 1087
kvn@986 1088 switch (opcode) {
kvn@986 1089 case Op_StrIndexOf:
kvn@3325 1090 // Get length of string 2
kvn@3325 1091 str2_len = load_String_length(no_ctrl, str2);
kvn@3325 1092
kvn@3680 1093 result = new (C) StrIndexOfNode(control(), memory(TypeAryPtr::CHARS),
kvn@3325 1094 str1_start, str1_len, str2_start, str2_len);
kvn@986 1095 break;
kvn@986 1096 case Op_StrComp:
kvn@3325 1097 // Get length of string 2
kvn@3325 1098 str2_len = load_String_length(no_ctrl, str2);
kvn@3325 1099
kvn@3680 1100 result = new (C) StrCompNode(control(), memory(TypeAryPtr::CHARS),
kvn@3325 1101 str1_start, str1_len, str2_start, str2_len);
kvn@986 1102 break;
kvn@986 1103 case Op_StrEquals:
kvn@3680 1104 result = new (C) StrEqualsNode(control(), memory(TypeAryPtr::CHARS),
kvn@3325 1105 str1_start, str2_start, str1_len);
kvn@986 1106 break;
kvn@986 1107 default:
kvn@986 1108 ShouldNotReachHere();
kvn@986 1109 return NULL;
kvn@986 1110 }
kvn@986 1111
kvn@986 1112 // All these intrinsics have checks.
kvn@986 1113 C->set_has_split_ifs(true); // Has chance for split-if optimization
kvn@986 1114
kvn@986 1115 return _gvn.transform(result);
kvn@986 1116 }
kvn@986 1117
kvn@3325 1118 // Helper method for String intrinsic functions. This version is called
kvn@3325 1119 // with str1 and str2 pointing to char[] nodes, with cnt1 and cnt2 pointing
kvn@3325 1120 // to Int nodes containing the lenghts of str1 and str2.
kvn@3325 1121 //
kvn@3325 1122 Node* LibraryCallKit::make_string_method_node(int opcode, Node* str1_start, Node* cnt1, Node* str2_start, Node* cnt2) {
kvn@3325 1123 Node* result = NULL;
kvn@3325 1124 switch (opcode) {
kvn@3325 1125 case Op_StrIndexOf:
kvn@3680 1126 result = new (C) StrIndexOfNode(control(), memory(TypeAryPtr::CHARS),
kvn@3325 1127 str1_start, cnt1, str2_start, cnt2);
kvn@3325 1128 break;
kvn@3325 1129 case Op_StrComp:
kvn@3680 1130 result = new (C) StrCompNode(control(), memory(TypeAryPtr::CHARS),
kvn@3325 1131 str1_start, cnt1, str2_start, cnt2);
kvn@3325 1132 break;
kvn@3325 1133 case Op_StrEquals:
kvn@3680 1134 result = new (C) StrEqualsNode(control(), memory(TypeAryPtr::CHARS),
kvn@3325 1135 str1_start, str2_start, cnt1);
kvn@3325 1136 break;
kvn@3325 1137 default:
kvn@3325 1138 ShouldNotReachHere();
kvn@3325 1139 return NULL;
kvn@3325 1140 }
kvn@3325 1141
kvn@3325 1142 // All these intrinsics have checks.
kvn@3325 1143 C->set_has_split_ifs(true); // Has chance for split-if optimization
kvn@3325 1144
kvn@3325 1145 return _gvn.transform(result);
kvn@3325 1146 }
kvn@3325 1147
duke@0 1148 //------------------------------inline_string_compareTo------------------------
twisti@3878 1149 // public int java.lang.String.compareTo(String anotherString);
duke@0 1150 bool LibraryCallKit::inline_string_compareTo() {
twisti@3878 1151 Node* receiver = null_check(argument(0));
twisti@3878 1152 Node* arg = null_check(argument(1));
duke@0 1153 if (stopped()) {
duke@0 1154 return true;
duke@0 1155 }
twisti@3878 1156 set_result(make_string_method_node(Op_StrComp, receiver, arg));
duke@0 1157 return true;
duke@0 1158 }
duke@0 1159
cfang@681 1160 //------------------------------inline_string_equals------------------------
cfang@681 1161 bool LibraryCallKit::inline_string_equals() {
twisti@3878 1162 Node* receiver = null_check_receiver();
twisti@3878 1163 // NOTE: Do not null check argument for String.equals() because spec
twisti@3878 1164 // allows to specify NULL as argument.
twisti@3878 1165 Node* argument = this->argument(1);
cfang@681 1166 if (stopped()) {
cfang@681 1167 return true;
cfang@681 1168 }
cfang@681 1169
kvn@986 1170 // paths (plus control) merge
kvn@3680 1171 RegionNode* region = new (C) RegionNode(5);
kvn@3680 1172 Node* phi = new (C) PhiNode(region, TypeInt::BOOL);
kvn@986 1173
kvn@986 1174 // does source == target string?
kvn@3680 1175 Node* cmp = _gvn.transform(new (C) CmpPNode(receiver, argument));
kvn@3680 1176 Node* bol = _gvn.transform(new (C) BoolNode(cmp, BoolTest::eq));
kvn@986 1177
kvn@986 1178 Node* if_eq = generate_slow_guard(bol, NULL);
kvn@986 1179 if (if_eq != NULL) {
kvn@986 1180 // receiver == argument
kvn@986 1181 phi->init_req(2, intcon(1));
kvn@986 1182 region->init_req(2, if_eq);
kvn@986 1183 }
kvn@986 1184
cfang@681 1185 // get String klass for instanceOf
cfang@681 1186 ciInstanceKlass* klass = env()->String_klass();
cfang@681 1187
kvn@986 1188 if (!stopped()) {
kvn@986 1189 Node* inst = gen_instanceof(argument, makecon(TypeKlassPtr::make(klass)));
kvn@3680 1190 Node* cmp = _gvn.transform(new (C) CmpINode(inst, intcon(1)));
kvn@3680 1191 Node* bol = _gvn.transform(new (C) BoolNode(cmp, BoolTest::ne));
kvn@986 1192
kvn@986 1193 Node* inst_false = generate_guard(bol, NULL, PROB_MIN);
kvn@986 1194 //instanceOf == true, fallthrough
kvn@986 1195
kvn@986 1196 if (inst_false != NULL) {
kvn@986 1197 phi->init_req(3, intcon(0));
kvn@986 1198 region->init_req(3, inst_false);
kvn@986 1199 }
kvn@986 1200 }
cfang@681 1201
kvn@986 1202 if (!stopped()) {
kvn@3325 1203 const TypeOopPtr* string_type = TypeOopPtr::make_from_klass(klass);
kvn@3325 1204
never@1416 1205 // Properly cast the argument to String
kvn@3680 1206 argument = _gvn.transform(new (C) CheckCastPPNode(control(), argument, string_type));
kvn@2434 1207 // This path is taken only when argument's type is String:NotNull.
kvn@2434 1208 argument = cast_not_null(argument, false);
never@1416 1209
kvn@3325 1210 Node* no_ctrl = NULL;
kvn@3325 1211
kvn@3325 1212 // Get start addr of receiver
kvn@3325 1213 Node* receiver_val = load_String_value(no_ctrl, receiver);
kvn@3325 1214 Node* receiver_offset = load_String_offset(no_ctrl, receiver);
kvn@3325 1215 Node* receiver_start = array_element_address(receiver_val, receiver_offset, T_CHAR);
kvn@3325 1216
kvn@3325 1217 // Get length of receiver
kvn@3325 1218 Node* receiver_cnt = load_String_length(no_ctrl, receiver);
kvn@3325 1219
kvn@3325 1220 // Get start addr of argument
twisti@3878 1221 Node* argument_val = load_String_value(no_ctrl, argument);
kvn@3325 1222 Node* argument_offset = load_String_offset(no_ctrl, argument);
kvn@3325 1223 Node* argument_start = array_element_address(argument_val, argument_offset, T_CHAR);
kvn@3325 1224
kvn@3325 1225 // Get length of argument
kvn@3325 1226 Node* argument_cnt = load_String_length(no_ctrl, argument);
kvn@986 1227
kvn@986 1228 // Check for receiver count != argument count
drchase@4918 1229 Node* cmp = _gvn.transform(new(C) CmpINode(receiver_cnt, argument_cnt));
drchase@4918 1230 Node* bol = _gvn.transform(new(C) BoolNode(cmp, BoolTest::ne));
kvn@986 1231 Node* if_ne = generate_slow_guard(bol, NULL);
kvn@986 1232 if (if_ne != NULL) {
kvn@986 1233 phi->init_req(4, intcon(0));
kvn@986 1234 region->init_req(4, if_ne);
kvn@986 1235 }
kvn@3325 1236
kvn@3325 1237 // Check for count == 0 is done by assembler code for StrEquals.
kvn@3325 1238
kvn@3325 1239 if (!stopped()) {
kvn@3325 1240 Node* equals = make_string_method_node(Op_StrEquals, receiver_start, receiver_cnt, argument_start, argument_cnt);
kvn@3325 1241 phi->init_req(1, equals);
kvn@3325 1242 region->init_req(1, control());
kvn@3325 1243 }
kvn@986 1244 }
cfang@681 1245
cfang@681 1246 // post merge
cfang@681 1247 set_control(_gvn.transform(region));
cfang@681 1248 record_for_igvn(region);
cfang@681 1249
twisti@3878 1250 set_result(_gvn.transform(phi));
cfang@681 1251 return true;
cfang@681 1252 }
cfang@681 1253
rasbold@169 1254 //------------------------------inline_array_equals----------------------------
rasbold@169 1255 bool LibraryCallKit::inline_array_equals() {
twisti@3878 1256 Node* arg1 = argument(0);
twisti@3878 1257 Node* arg2 = argument(1);
twisti@3878 1258 set_result(_gvn.transform(new (C) AryEqNode(control(), memory(TypeAryPtr::CHARS), arg1, arg2)));
rasbold@169 1259 return true;
rasbold@169 1260 }
rasbold@169 1261
duke@0 1262 // Java version of String.indexOf(constant string)
duke@0 1263 // class StringDecl {
duke@0 1264 // StringDecl(char[] ca) {
duke@0 1265 // offset = 0;
duke@0 1266 // count = ca.length;
duke@0 1267 // value = ca;
duke@0 1268 // }
duke@0 1269 // int offset;
duke@0 1270 // int count;
duke@0 1271 // char[] value;
duke@0 1272 // }
duke@0 1273 //
duke@0 1274 // static int string_indexOf_J(StringDecl string_object, char[] target_object,
duke@0 1275 // int targetOffset, int cache_i, int md2) {
duke@0 1276 // int cache = cache_i;
duke@0 1277 // int sourceOffset = string_object.offset;
duke@0 1278 // int sourceCount = string_object.count;
duke@0 1279 // int targetCount = target_object.length;
duke@0 1280 //
duke@0 1281 // int targetCountLess1 = targetCount - 1;
duke@0 1282 // int sourceEnd = sourceOffset + sourceCount - targetCountLess1;
duke@0 1283 //
duke@0 1284 // char[] source = string_object.value;
duke@0 1285 // char[] target = target_object;
duke@0 1286 // int lastChar = target[targetCountLess1];
duke@0 1287 //
duke@0 1288 // outer_loop:
duke@0 1289 // for (int i = sourceOffset; i < sourceEnd; ) {
duke@0 1290 // int src = source[i + targetCountLess1];
duke@0 1291 // if (src == lastChar) {
duke@0 1292 // // With random strings and a 4-character alphabet,
duke@0 1293 // // reverse matching at this point sets up 0.8% fewer
duke@0 1294 // // frames, but (paradoxically) makes 0.3% more probes.
duke@0 1295 // // Since those probes are nearer the lastChar probe,
duke@0 1296 // // there is may be a net D$ win with reverse matching.
duke@0 1297 // // But, reversing loop inhibits unroll of inner loop
duke@0 1298 // // for unknown reason. So, does running outer loop from
duke@0 1299 // // (sourceOffset - targetCountLess1) to (sourceOffset + sourceCount)
duke@0 1300 // for (int j = 0; j < targetCountLess1; j++) {
duke@0 1301 // if (target[targetOffset + j] != source[i+j]) {
duke@0 1302 // if ((cache & (1 << source[i+j])) == 0) {
duke@0 1303 // if (md2 < j+1) {
duke@0 1304 // i += j+1;
duke@0 1305 // continue outer_loop;
duke@0 1306 // }
duke@0 1307 // }
duke@0 1308 // i += md2;
duke@0 1309 // continue outer_loop;
duke@0 1310 // }
duke@0 1311 // }
duke@0 1312 // return i - sourceOffset;
duke@0 1313 // }
duke@0 1314 // if ((cache & (1 << src)) == 0) {
duke@0 1315 // i += targetCountLess1;
duke@0 1316 // } // using "i += targetCount;" and an "else i++;" causes a jump to jump.
duke@0 1317 // i++;
duke@0 1318 // }
duke@0 1319 // return -1;
duke@0 1320 // }
duke@0 1321
duke@0 1322 //------------------------------string_indexOf------------------------
duke@0 1323 Node* LibraryCallKit::string_indexOf(Node* string_object, ciTypeArray* target_array, jint targetOffset_i,
duke@0 1324 jint cache_i, jint md2_i) {
duke@0 1325
duke@0 1326 Node* no_ctrl = NULL;
duke@0 1327 float likely = PROB_LIKELY(0.9);
duke@0 1328 float unlikely = PROB_UNLIKELY(0.9);
duke@0 1329
twisti@3878 1330 const int nargs = 0; // no arguments to push back for uncommon trap in predicate
kvn@2230 1331
kvn@3325 1332 Node* source = load_String_value(no_ctrl, string_object);
kvn@3325 1333 Node* sourceOffset = load_String_offset(no_ctrl, string_object);
kvn@3325 1334 Node* sourceCount = load_String_length(no_ctrl, string_object);
duke@0 1335
drchase@4918 1336 Node* target = _gvn.transform( makecon(TypeOopPtr::make_from_constant(target_array, true)));
duke@0 1337 jint target_length = target_array->length();
duke@0 1338 const TypeAry* target_array_type = TypeAry::make(TypeInt::CHAR, TypeInt::make(0, target_length, Type::WidenMin));
duke@0 1339 const TypeAryPtr* target_type = TypeAryPtr::make(TypePtr::BotPTR, target_array_type, target_array->klass(), true, Type::OffsetBot);
duke@0 1340
vlivanov@5223 1341 // String.value field is known to be @Stable.
vlivanov@5223 1342 if (UseImplicitStableValues) {
vlivanov@5223 1343 target = cast_array_to_stable(target, target_type);
vlivanov@5223 1344 }
vlivanov@5223 1345
kvn@2291 1346 IdealKit kit(this, false, true);
duke@0 1347 #define __ kit.
duke@0 1348 Node* zero = __ ConI(0);
duke@0 1349 Node* one = __ ConI(1);
duke@0 1350 Node* cache = __ ConI(cache_i);
duke@0 1351 Node* md2 = __ ConI(md2_i);
duke@0 1352 Node* lastChar = __ ConI(target_array->char_at(target_length - 1));
duke@0 1353 Node* targetCount = __ ConI(target_length);
duke@0 1354 Node* targetCountLess1 = __ ConI(target_length - 1);
duke@0 1355 Node* targetOffset = __ ConI(targetOffset_i);
duke@0 1356 Node* sourceEnd = __ SubI(__ AddI(sourceOffset, sourceCount), targetCountLess1);
duke@0 1357
kvn@851 1358 IdealVariable rtn(kit), i(kit), j(kit); __ declarations_done();
duke@0 1359 Node* outer_loop = __ make_label(2 /* goto */);
duke@0 1360 Node* return_ = __ make_label(1);
duke@0 1361
duke@0 1362 __ set(rtn,__ ConI(-1));
kvn@2230 1363 __ loop(this, nargs, i, sourceOffset, BoolTest::lt, sourceEnd); {
duke@0 1364 Node* i2 = __ AddI(__ value(i), targetCountLess1);
duke@0 1365 // pin to prohibit loading of "next iteration" value which may SEGV (rare)
duke@0 1366 Node* src = load_array_element(__ ctrl(), source, i2, TypeAryPtr::CHARS);
duke@0 1367 __ if_then(src, BoolTest::eq, lastChar, unlikely); {
kvn@2230 1368 __ loop(this, nargs, j, zero, BoolTest::lt, targetCountLess1); {
duke@0 1369 Node* tpj = __ AddI(targetOffset, __ value(j));
duke@0 1370 Node* targ = load_array_element(no_ctrl, target, tpj, target_type);
duke@0 1371 Node* ipj = __ AddI(__ value(i), __ value(j));
duke@0 1372 Node* src2 = load_array_element(no_ctrl, source, ipj, TypeAryPtr::CHARS);
duke@0 1373 __ if_then(targ, BoolTest::ne, src2); {
duke@0 1374 __ if_then(__ AndI(cache, __ LShiftI(one, src2)), BoolTest::eq, zero); {
duke@0 1375 __ if_then(md2, BoolTest::lt, __ AddI(__ value(j), one)); {
duke@0 1376 __ increment(i, __ AddI(__ value(j), one));
duke@0 1377 __ goto_(outer_loop);
duke@0 1378 } __ end_if(); __ dead(j);
duke@0 1379 }__ end_if(); __ dead(j);
duke@0 1380 __ increment(i, md2);
duke@0 1381 __ goto_(outer_loop);
duke@0 1382 }__ end_if();
duke@0 1383 __ increment(j, one);
duke@0 1384 }__ end_loop(); __ dead(j);
duke@0 1385 __ set(rtn, __ SubI(__ value(i), sourceOffset)); __ dead(i);
duke@0 1386 __ goto_(return_);
duke@0 1387 }__ end_if();
duke@0 1388 __ if_then(__ AndI(cache, __ LShiftI(one, src)), BoolTest::eq, zero, likely); {
duke@0 1389 __ increment(i, targetCountLess1);
duke@0 1390 }__ end_if();
duke@0 1391 __ increment(i, one);
duke@0 1392 __ bind(outer_loop);
duke@0 1393 }__ end_loop(); __ dead(i);
duke@0 1394 __ bind(return_);
kvn@851 1395
kvn@851 1396 // Final sync IdealKit and GraphKit.
kvn@2291 1397 final_sync(kit);
duke@0 1398 Node* result = __ value(rtn);
duke@0 1399 #undef __
duke@0 1400 C->set_has_loops(true);
duke@0 1401 return result;
duke@0 1402 }
duke@0 1403
duke@0 1404 //------------------------------inline_string_indexOf------------------------
duke@0 1405 bool LibraryCallKit::inline_string_indexOf() {
twisti@3878 1406 Node* receiver = argument(0);
twisti@3878 1407 Node* arg = argument(1);
duke@0 1408
cfang@681 1409 Node* result;
iveresov@1424 1410 // Disable the use of pcmpestri until it can be guaranteed that
iveresov@1424 1411 // the load doesn't cross into the uncommited space.
kvn@2167 1412 if (Matcher::has_match_rule(Op_StrIndexOf) &&
cfang@681 1413 UseSSE42Intrinsics) {
cfang@681 1414 // Generate SSE4.2 version of indexOf
cfang@681 1415 // We currently only have match rules that use SSE4.2
cfang@681 1416
twisti@3878 1417 receiver = null_check(receiver);
twisti@3878 1418 arg = null_check(arg);
cfang@681 1419 if (stopped()) {
cfang@681 1420 return true;
cfang@681 1421 }
cfang@681 1422
kvn@2167 1423 ciInstanceKlass* str_klass = env()->String_klass();
kvn@2167 1424 const TypeOopPtr* string_type = TypeOopPtr::make_from_klass(str_klass);
kvn@2167 1425
kvn@986 1426 // Make the merge point
kvn@3680 1427 RegionNode* result_rgn = new (C) RegionNode(4);
kvn@3680 1428 Node* result_phi = new (C) PhiNode(result_rgn, TypeInt::INT);
kvn@986 1429 Node* no_ctrl = NULL;
kvn@986 1430
kvn@3325 1431 // Get start addr of source string
kvn@3325 1432 Node* source = load_String_value(no_ctrl, receiver);
kvn@3325 1433 Node* source_offset = load_String_offset(no_ctrl, receiver);
kvn@3325 1434 Node* source_start = array_element_address(source, source_offset, T_CHAR);
kvn@3325 1435
kvn@3325 1436 // Get length of source string
kvn@3325 1437 Node* source_cnt = load_String_length(no_ctrl, receiver);
kvn@3325 1438
kvn@3325 1439 // Get start addr of substring
twisti@3878 1440 Node* substr = load_String_value(no_ctrl, arg);
twisti@3878 1441 Node* substr_offset = load_String_offset(no_ctrl, arg);
kvn@3325 1442 Node* substr_start = array_element_address(substr, substr_offset, T_CHAR);
kvn@3325 1443
kvn@3325 1444 // Get length of source string
twisti@3878 1445 Node* substr_cnt = load_String_length(no_ctrl, arg);
kvn@986 1446
kvn@986 1447 // Check for substr count > string count
drchase@4918 1448 Node* cmp = _gvn.transform(new(C) CmpINode(substr_cnt, source_cnt));
drchase@4918 1449 Node* bol = _gvn.transform(new(C) BoolNode(cmp, BoolTest::gt));
kvn@986 1450 Node* if_gt = generate_slow_guard(bol, NULL);
kvn@986 1451 if (if_gt != NULL) {
kvn@986 1452 result_phi->init_req(2, intcon(-1));
kvn@986 1453 result_rgn->init_req(2, if_gt);
kvn@986 1454 }
kvn@986 1455
kvn@986 1456 if (!stopped()) {
kvn@2167 1457 // Check for substr count == 0
drchase@4918 1458 cmp = _gvn.transform(new(C) CmpINode(substr_cnt, intcon(0)));
drchase@4918 1459 bol = _gvn.transform(new(C) BoolNode(cmp, BoolTest::eq));
kvn@2167 1460 Node* if_zero = generate_slow_guard(bol, NULL);
kvn@2167 1461 if (if_zero != NULL) {
kvn@2167 1462 result_phi->init_req(3, intcon(0));
kvn@2167 1463 result_rgn->init_req(3, if_zero);
kvn@2167 1464 }
kvn@2167 1465 }
kvn@2167 1466
kvn@2167 1467 if (!stopped()) {
kvn@3325 1468 result = make_string_method_node(Op_StrIndexOf, source_start, source_cnt, substr_start, substr_cnt);
kvn@986 1469 result_phi->init_req(1, result);
kvn@986 1470 result_rgn->init_req(1, control());
kvn@986 1471 }
kvn@986 1472 set_control(_gvn.transform(result_rgn));
kvn@986 1473 record_for_igvn(result_rgn);
kvn@986 1474 result = _gvn.transform(result_phi);
kvn@986 1475
kvn@2167 1476 } else { // Use LibraryCallKit::string_indexOf
kvn@2167 1477 // don't intrinsify if argument isn't a constant string.
twisti@3878 1478 if (!arg->is_Con()) {
cfang@681 1479 return false;
cfang@681 1480 }
twisti@3878 1481 const TypeOopPtr* str_type = _gvn.type(arg)->isa_oopptr();
cfang@681 1482 if (str_type == NULL) {
cfang@681 1483 return false;
cfang@681 1484 }
cfang@681 1485 ciInstanceKlass* klass = env()->String_klass();
cfang@681 1486 ciObject* str_const = str_type->const_oop();
cfang@681 1487 if (str_const == NULL || str_const->klass() != klass) {
cfang@681 1488 return false;
cfang@681 1489 }
cfang@681 1490 ciInstance* str = str_const->as_instance();
cfang@681 1491 assert(str != NULL, "must be instance");
cfang@681 1492
kvn@3325 1493 ciObject* v = str->field_value_by_offset(java_lang_String::value_offset_in_bytes()).as_object();
cfang@681 1494 ciTypeArray* pat = v->as_type_array(); // pattern (argument) character array
cfang@681 1495
kvn@3325 1496 int o;
kvn@3325 1497 int c;
kvn@3325 1498 if (java_lang_String::has_offset_field()) {
kvn@3325 1499 o = str->field_value_by_offset(java_lang_String::offset_offset_in_bytes()).as_int();
kvn@3325 1500 c = str->field_value_by_offset(java_lang_String::count_offset_in_bytes()).as_int();
kvn@3325 1501 } else {
kvn@3325 1502 o = 0;
kvn@3325 1503 c = pat->length();
kvn@3325 1504 }
kvn@3325 1505
cfang@681 1506 // constant strings have no offset and count == length which
cfang@681 1507 // simplifies the resulting code somewhat so lets optimize for that.
cfang@681 1508 if (o != 0 || c != pat->length()) {
cfang@681 1509 return false;
cfang@681 1510 }
cfang@681 1511
twisti@3878 1512 receiver = null_check(receiver, T_OBJECT);
twisti@3878 1513 // NOTE: No null check on the argument is needed since it's a constant String oop.
cfang@681 1514 if (stopped()) {
kvn@2167 1515 return true;
cfang@681 1516 }
cfang@681 1517
cfang@681 1518 // The null string as a pattern always returns 0 (match at beginning of string)
cfang@681 1519 if (c == 0) {
twisti@3878 1520 set_result(intcon(0));
cfang@681 1521 return true;
cfang@681 1522 }
cfang@681 1523
cfang@681 1524 // Generate default indexOf
cfang@681 1525 jchar lastChar = pat->char_at(o + (c - 1));
cfang@681 1526 int cache = 0;
cfang@681 1527 int i;
cfang@681 1528 for (i = 0; i < c - 1; i++) {
cfang@681 1529 assert(i < pat->length(), "out of range");
cfang@681 1530 cache |= (1 << (pat->char_at(o + i) & (sizeof(cache) * BitsPerByte - 1)));
cfang@681 1531 }
cfang@681 1532
cfang@681 1533 int md2 = c;
cfang@681 1534 for (i = 0; i < c - 1; i++) {
cfang@681 1535 assert(i < pat->length(), "out of range");
cfang@681 1536 if (pat->char_at(o + i) == lastChar) {
cfang@681 1537 md2 = (c - 1) - i;
cfang@681 1538 }
cfang@681 1539 }
cfang@681 1540
cfang@681 1541 result = string_indexOf(receiver, pat, o, cache, md2);
duke@0 1542 }
twisti@3878 1543 set_result(result);
duke@0 1544 return true;
duke@0 1545 }
duke@0 1546
twisti@3878 1547 //--------------------------round_double_node--------------------------------
twisti@3878 1548 // Round a double node if necessary.
twisti@3878 1549 Node* LibraryCallKit::round_double_node(Node* n) {
twisti@3878 1550 if (Matcher::strict_fp_requires_explicit_rounding && UseSSE <= 1)
twisti@3878 1551 n = _gvn.transform(new (C) RoundDoubleNode(0, n));
twisti@3878 1552 return n;
twisti@3878 1553 }
twisti@3878 1554
twisti@3878 1555 //------------------------------inline_math-----------------------------------
twisti@3878 1556 // public static double Math.abs(double)
twisti@3878 1557 // public static double Math.sqrt(double)
twisti@3878 1558 // public static double Math.log(double)
twisti@3878 1559 // public static double Math.log10(double)
twisti@3878 1560 bool LibraryCallKit::inline_math(vmIntrinsics::ID id) {
twisti@3878 1561 Node* arg = round_double_node(argument(0));
twisti@3878 1562 Node* n;
twisti@3878 1563 switch (id) {
roland@4182 1564 case vmIntrinsics::_dabs: n = new (C) AbsDNode( arg); break;
roland@4182 1565 case vmIntrinsics::_dsqrt: n = new (C) SqrtDNode(C, control(), arg); break;
roland@4182 1566 case vmIntrinsics::_dlog: n = new (C) LogDNode(C, control(), arg); break;
roland@4182 1567 case vmIntrinsics::_dlog10: n = new (C) Log10DNode(C, control(), arg); break;
twisti@3878 1568 default: fatal_unexpected_iid(id); break;
twisti@3878 1569 }
twisti@3878 1570 set_result(_gvn.transform(n));
twisti@3878 1571 return true;
duke@0 1572 }
duke@0 1573
duke@0 1574 //------------------------------inline_trig----------------------------------
duke@0 1575 // Inline sin/cos/tan instructions, if possible. If rounding is required, do
duke@0 1576 // argument reduction which will turn into a fast/slow diamond.
duke@0 1577 bool LibraryCallKit::inline_trig(vmIntrinsics::ID id) {
twisti@3878 1578 Node* arg = round_double_node(argument(0));
twisti@3878 1579 Node* n = NULL;
duke@0 1580
duke@0 1581 switch (id) {
roland@4182 1582 case vmIntrinsics::_dsin: n = new (C) SinDNode(C, control(), arg); break;
roland@4182 1583 case vmIntrinsics::_dcos: n = new (C) CosDNode(C, control(), arg); break;
roland@4182 1584 case vmIntrinsics::_dtan: n = new (C) TanDNode(C, control(), arg); break;
twisti@3878 1585 default: fatal_unexpected_iid(id); break;
duke@0 1586 }
twisti@3878 1587 n = _gvn.transform(n);
duke@0 1588
duke@0 1589 // Rounding required? Check for argument reduction!
twisti@3878 1590 if (Matcher::strict_fp_requires_explicit_rounding) {
duke@0 1591 static const double pi_4 = 0.7853981633974483;
duke@0 1592 static const double neg_pi_4 = -0.7853981633974483;
duke@0 1593 // pi/2 in 80-bit extended precision
duke@0 1594 // static const unsigned char pi_2_bits_x[] = {0x35,0xc2,0x68,0x21,0xa2,0xda,0x0f,0xc9,0xff,0x3f,0x00,0x00,0x00,0x00,0x00,0x00};
duke@0 1595 // -pi/2 in 80-bit extended precision
duke@0 1596 // static const unsigned char neg_pi_2_bits_x[] = {0x35,0xc2,0x68,0x21,0xa2,0xda,0x0f,0xc9,0xff,0xbf,0x00,0x00,0x00,0x00,0x00,0x00};
duke@0 1597 // Cutoff value for using this argument reduction technique
duke@0 1598 //static const double pi_2_minus_epsilon = 1.564660403643354;
duke@0 1599 //static const double neg_pi_2_plus_epsilon = -1.564660403643354;
duke@0 1600
duke@0 1601 // Pseudocode for sin:
duke@0 1602 // if (x <= Math.PI / 4.0) {
duke@0 1603 // if (x >= -Math.PI / 4.0) return fsin(x);
duke@0 1604 // if (x >= -Math.PI / 2.0) return -fcos(x + Math.PI / 2.0);
duke@0 1605 // } else {
duke@0 1606 // if (x <= Math.PI / 2.0) return fcos(x - Math.PI / 2.0);
duke@0 1607 // }
duke@0 1608 // return StrictMath.sin(x);
duke@0 1609
duke@0 1610 // Pseudocode for cos:
duke@0 1611 // if (x <= Math.PI / 4.0) {
duke@0 1612 // if (x >= -Math.PI / 4.0) return fcos(x);
duke@0 1613 // if (x >= -Math.PI / 2.0) return fsin(x + Math.PI / 2.0);
duke@0 1614 // } else {
duke@0 1615 // if (x <= Math.PI / 2.0) return -fsin(x - Math.PI / 2.0);
duke@0 1616 // }
duke@0 1617 // return StrictMath.cos(x);
duke@0 1618
duke@0 1619 // Actually, sticking in an 80-bit Intel value into C2 will be tough; it
duke@0 1620 // requires a special machine instruction to load it. Instead we'll try
duke@0 1621 // the 'easy' case. If we really need the extra range +/- PI/2 we'll
duke@0 1622 // probably do the math inside the SIN encoding.
duke@0 1623
duke@0 1624 // Make the merge point
twisti@3878 1625 RegionNode* r = new (C) RegionNode(3);
twisti@3878 1626 Node* phi = new (C) PhiNode(r, Type::DOUBLE);
duke@0 1627
duke@0 1628 // Flatten arg so we need only 1 test
kvn@3680 1629 Node *abs = _gvn.transform(new (C) AbsDNode(arg));
duke@0 1630 // Node for PI/4 constant
duke@0 1631 Node *pi4 = makecon(TypeD::make(pi_4));
duke@0 1632 // Check PI/4 : abs(arg)
kvn@3680 1633 Node *cmp = _gvn.transform(new (C) CmpDNode(pi4,abs));
duke@0 1634 // Check: If PI/4 < abs(arg) then go slow
drchase@4918 1635 Node *bol = _gvn.transform(new (C) BoolNode( cmp, BoolTest::lt ));
duke@0 1636 // Branch either way
duke@0 1637 IfNode *iff = create_and_xform_if(control(),bol, PROB_STATIC_FREQUENT, COUNT_UNKNOWN);
duke@0 1638 set_control(opt_iff(r,iff));
duke@0 1639
duke@0 1640 // Set fast path result
twisti@3878 1641 phi->init_req(2, n);
duke@0 1642
duke@0 1643 // Slow path - non-blocking leaf call
duke@0 1644 Node* call = NULL;
duke@0 1645 switch (id) {
duke@0 1646 case vmIntrinsics::_dsin:
duke@0 1647 call = make_runtime_call(RC_LEAF, OptoRuntime::Math_D_D_Type(),
duke@0 1648 CAST_FROM_FN_PTR(address, SharedRuntime::dsin),
duke@0 1649 "Sin", NULL, arg, top());
duke@0 1650 break;
duke@0 1651 case vmIntrinsics::_dcos:
duke@0 1652 call = make_runtime_call(RC_LEAF, OptoRuntime::Math_D_D_Type(),
duke@0 1653 CAST_FROM_FN_PTR(address, SharedRuntime::dcos),
duke@0 1654 "Cos", NULL, arg, top());
duke@0 1655 break;
duke@0 1656 case vmIntrinsics::_dtan:
duke@0 1657 call = make_runtime_call(RC_LEAF, OptoRuntime::Math_D_D_Type(),
duke@0 1658 CAST_FROM_FN_PTR(address, SharedRuntime::dtan),
duke@0 1659 "Tan", NULL, arg, top());
duke@0 1660 break;
duke@0 1661 }
duke@0 1662 assert(control()->in(0) == call, "");
twisti@3878 1663 Node* slow_result = _gvn.transform(new (C) ProjNode(call, TypeFunc::Parms));
twisti@3878 1664 r->init_req(1, control());
twisti@3878 1665 phi->init_req(1, slow_result);
duke@0 1666
duke@0 1667 // Post-merge
duke@0 1668 set_control(_gvn.transform(r));
duke@0 1669 record_for_igvn(r);
twisti@3878 1670 n = _gvn.transform(phi);
duke@0 1671
duke@0 1672 C->set_has_split_ifs(true); // Has chance for split-if optimization
duke@0 1673 }
twisti@3878 1674 set_result(n);
duke@0 1675 return true;
duke@0 1676 }
duke@0 1677
roland@3473 1678 void LibraryCallKit::finish_pow_exp(Node* result, Node* x, Node* y, const TypeFunc* call_type, address funcAddr, const char* funcName) {
roland@3473 1679 //-------------------
roland@3473 1680 //result=(result.isNaN())? funcAddr():result;
roland@3473 1681 // Check: If isNaN() by checking result!=result? then either trap
roland@3473 1682 // or go to runtime
twisti@3878 1683 Node* cmpisnan = _gvn.transform(new (C) CmpDNode(result, result));
roland@3473 1684 // Build the boolean node
twisti@3878 1685 Node* bolisnum = _gvn.transform(new (C) BoolNode(cmpisnan, BoolTest::eq));
roland@3473 1686
roland@3473 1687 if (!too_many_traps(Deoptimization::Reason_intrinsic)) {
twisti@3878 1688 { BuildCutout unless(this, bolisnum, PROB_STATIC_FREQUENT);
roland@3473 1689 // The pow or exp intrinsic returned a NaN, which requires a call
roland@3473 1690 // to the runtime. Recompile with the runtime call.
roland@3473 1691 uncommon_trap(Deoptimization::Reason_intrinsic,
roland@3473 1692 Deoptimization::Action_make_not_entrant);
roland@3473 1693 }
twisti@3878 1694 set_result(result);
roland@3473 1695 } else {
roland@3473 1696 // If this inlining ever returned NaN in the past, we compile a call
roland@3473 1697 // to the runtime to properly handle corner cases
roland@3473 1698
roland@3473 1699 IfNode* iff = create_and_xform_if(control(), bolisnum, PROB_STATIC_FREQUENT, COUNT_UNKNOWN);
drchase@4918 1700 Node* if_slow = _gvn.transform(new (C) IfFalseNode(iff));
drchase@4918 1701 Node* if_fast = _gvn.transform(new (C) IfTrueNode(iff));
roland@3473 1702
roland@3473 1703 if (!if_slow->is_top()) {
twisti@3878 1704 RegionNode* result_region = new (C) RegionNode(3);
kvn@3680 1705 PhiNode* result_val = new (C) PhiNode(result_region, Type::DOUBLE);
roland@3473 1706
roland@3473 1707 result_region->init_req(1, if_fast);
roland@3473 1708 result_val->init_req(1, result);
roland@3473 1709
roland@3473 1710 set_control(if_slow);
roland@3473 1711
roland@3473 1712 const TypePtr* no_memory_effects = NULL;
roland@3473 1713 Node* rt = make_runtime_call(RC_LEAF, call_type, funcAddr, funcName,
roland@3473 1714 no_memory_effects,
roland@3473 1715 x, top(), y, y ? top() : NULL);
kvn@3680 1716 Node* value = _gvn.transform(new (C) ProjNode(rt, TypeFunc::Parms+0));
roland@3473 1717 #ifdef ASSERT
kvn@3680 1718 Node* value_top = _gvn.transform(new (C) ProjNode(rt, TypeFunc::Parms+1));
roland@3473 1719 assert(value_top == top(), "second value must be top");
roland@3473 1720 #endif
roland@3473 1721
roland@3473 1722 result_region->init_req(2, control());
roland@3473 1723 result_val->init_req(2, value);
twisti@3878 1724 set_result(result_region, result_val);
roland@3473 1725 } else {
twisti@3878 1726 set_result(result);
roland@3473 1727 }
roland@3473 1728 }
roland@3473 1729 }
roland@3473 1730
duke@0 1731 //------------------------------inline_exp-------------------------------------
duke@0 1732 // Inline exp instructions, if possible. The Intel hardware only misses
duke@0 1733 // really odd corner cases (+/- Infinity). Just uncommon-trap them.
twisti@3878 1734 bool LibraryCallKit::inline_exp() {
twisti@3878 1735 Node* arg = round_double_node(argument(0));
roland@4154 1736 Node* n = _gvn.transform(new (C) ExpDNode(C, control(), arg));
twisti@3878 1737
twisti@3878 1738 finish_pow_exp(n, arg, NULL, OptoRuntime::Math_D_D_Type(), CAST_FROM_FN_PTR(address, SharedRuntime::dexp), "EXP");
duke@0 1739
duke@0 1740 C->set_has_split_ifs(true); // Has chance for split-if optimization
duke@0 1741 return true;
duke@0 1742 }
duke@0 1743
duke@0 1744 //------------------------------inline_pow-------------------------------------
duke@0 1745 // Inline power instructions, if possible.
twisti@3878 1746 bool LibraryCallKit::inline_pow() {
duke@0 1747 // Pseudocode for pow
duke@0 1748 // if (x <= 0.0) {
roland@3473 1749 // long longy = (long)y;
roland@3473 1750 // if ((double)longy == y) { // if y is long
roland@3473 1751 // if (y + 1 == y) longy = 0; // huge number: even
roland@3473 1752 // result = ((1&longy) == 0)?-DPow(abs(x), y):DPow(abs(x), y);
duke@0 1753 // } else {
duke@0 1754 // result = NaN;
duke@0 1755 // }
duke@0 1756 // } else {
duke@0 1757 // result = DPow(x,y);
duke@0 1758 // }
duke@0 1759 // if (result != result)? {
roland@3473 1760 // result = uncommon_trap() or runtime_call();
duke@0 1761 // }
duke@0 1762 // return result;
duke@0 1763
twisti@3878 1764 Node* x = round_double_node(argument(0));
twisti@3878 1765 Node* y = round_double_node(argument(2));
duke@0 1766
roland@3473 1767 Node* result = NULL;
roland@3473 1768
roland@3473 1769 if (!too_many_traps(Deoptimization::Reason_intrinsic)) {
roland@3473 1770 // Short form: skip the fancy tests and just check for NaN result.
roland@4154 1771 result = _gvn.transform(new (C) PowDNode(C, control(), x, y));
duke@0 1772 } else {
roland@3473 1773 // If this inlining ever returned NaN in the past, include all
roland@3473 1774 // checks + call to the runtime.
duke@0 1775
duke@0 1776 // Set the merge point for If node with condition of (x <= 0.0)
duke@0 1777 // There are four possible paths to region node and phi node
kvn@3680 1778 RegionNode *r = new (C) RegionNode(4);
kvn@3680 1779 Node *phi = new (C) PhiNode(r, Type::DOUBLE);
duke@0 1780
duke@0 1781 // Build the first if node: if (x <= 0.0)
duke@0 1782 // Node for 0 constant
duke@0 1783 Node *zeronode = makecon(TypeD::ZERO);
duke@0 1784 // Check x:0
kvn@3680 1785 Node *cmp = _gvn.transform(new (C) CmpDNode(x, zeronode));
duke@0 1786 // Check: If (x<=0) then go complex path
drchase@4918 1787 Node *bol1 = _gvn.transform(new (C) BoolNode( cmp, BoolTest::le ));
duke@0 1788 // Branch either way
duke@0 1789 IfNode *if1 = create_and_xform_if(control(),bol1, PROB_STATIC_INFREQUENT, COUNT_UNKNOWN);
duke@0 1790 // Fast path taken; set region slot 3
drchase@4918 1791 Node *fast_taken = _gvn.transform(new (C) IfFalseNode(if1));
duke@0 1792 r->init_req(3,fast_taken); // Capture fast-control
duke@0 1793
duke@0 1794 // Fast path not-taken, i.e. slow path
drchase@4918 1795 Node *complex_path = _gvn.transform(new (C) IfTrueNode(if1));
duke@0 1796
duke@0 1797 // Set fast path result
drchase@4918 1798 Node *fast_result = _gvn.transform(new (C) PowDNode(C, control(), x, y));
duke@0 1799 phi->init_req(3, fast_result);
duke@0 1800
duke@0 1801 // Complex path
roland@3473 1802 // Build the second if node (if y is long)
roland@3473 1803 // Node for (long)y
drchase@4918 1804 Node *longy = _gvn.transform(new (C) ConvD2LNode(y));
roland@3473 1805 // Node for (double)((long) y)
drchase@4918 1806 Node *doublelongy= _gvn.transform(new (C) ConvL2DNode(longy));
roland@3473 1807 // Check (double)((long) y) : y
kvn@3680 1808 Node *cmplongy= _gvn.transform(new (C) CmpDNode(doublelongy, y));
roland@3473 1809 // Check if (y isn't long) then go to slow path
roland@3473 1810
drchase@4918 1811 Node *bol2 = _gvn.transform(new (C) BoolNode( cmplongy, BoolTest::ne ));
twisti@605 1812 // Branch either way
duke@0 1813 IfNode *if2 = create_and_xform_if(complex_path,bol2, PROB_STATIC_INFREQUENT, COUNT_UNKNOWN);
drchase@4918 1814 Node* ylong_path = _gvn.transform(new (C) IfFalseNode(if2));
drchase@4918 1815
drchase@4918 1816 Node *slow_path = _gvn.transform(new (C) IfTrueNode(if2));
roland@3473 1817
roland@3473 1818 // Calculate DPow(abs(x), y)*(1 & (long)y)
duke@0 1819 // Node for constant 1
roland@3473 1820 Node *conone = longcon(1);
roland@3473 1821 // 1& (long)y
drchase@4918 1822 Node *signnode= _gvn.transform(new (C) AndLNode(conone, longy));
roland@3473 1823
roland@3473 1824 // A huge number is always even. Detect a huge number by checking
roland@3473 1825 // if y + 1 == y and set integer to be tested for parity to 0.
roland@3473 1826 // Required for corner case:
roland@3473 1827 // (long)9.223372036854776E18 = max_jlong
roland@3473 1828 // (double)(long)9.223372036854776E18 = 9.223372036854776E18
roland@3473 1829 // max_jlong is odd but 9.223372036854776E18 is even
drchase@4918 1830 Node* yplus1 = _gvn.transform(new (C) AddDNode(y, makecon(TypeD::make(1))));
kvn@3680 1831 Node *cmpyplus1= _gvn.transform(new (C) CmpDNode(yplus1, y));
drchase@4918 1832 Node *bolyplus1 = _gvn.transform(new (C) BoolNode( cmpyplus1, BoolTest::eq ));
roland@3473 1833 Node* correctedsign = NULL;
roland@3473 1834 if (ConditionalMoveLimit != 0) {
roland@3473 1835 correctedsign = _gvn.transform( CMoveNode::make(C, NULL, bolyplus1, signnode, longcon(0), TypeLong::LONG));
roland@3473 1836 } else {
roland@3473 1837 IfNode *ifyplus1 = create_and_xform_if(ylong_path,bolyplus1, PROB_FAIR, COUNT_UNKNOWN);
kvn@3680 1838 RegionNode *r = new (C) RegionNode(3);
kvn@3680 1839 Node *phi = new (C) PhiNode(r, TypeLong::LONG);
drchase@4918 1840 r->init_req(1, _gvn.transform(new (C) IfFalseNode(ifyplus1)));
drchase@4918 1841 r->init_req(2, _gvn.transform(new (C) IfTrueNode(ifyplus1)));
roland@3473 1842 phi->init_req(1, signnode);
roland@3473 1843 phi->init_req(2, longcon(0));
roland@3473 1844 correctedsign = _gvn.transform(phi);
roland@3473 1845 ylong_path = _gvn.transform(r);
roland@3473 1846 record_for_igvn(r);
roland@3473 1847 }
roland@3473 1848
duke@0 1849 // zero node
roland@3473 1850 Node *conzero = longcon(0);
roland@3473 1851 // Check (1&(long)y)==0?
kvn@3680 1852 Node *cmpeq1 = _gvn.transform(new (C) CmpLNode(correctedsign, conzero));
roland@3473 1853 // Check if (1&(long)y)!=0?, if so the result is negative
drchase@4918 1854 Node *bol3 = _gvn.transform(new (C) BoolNode( cmpeq1, BoolTest::ne ));
duke@0 1855 // abs(x)
drchase@4918 1856 Node *absx=_gvn.transform(new (C) AbsDNode(x));
duke@0 1857 // abs(x)^y
drchase@4918 1858 Node *absxpowy = _gvn.transform(new (C) PowDNode(C, control(), absx, y));
duke@0 1859 // -abs(x)^y
kvn@3680 1860 Node *negabsxpowy = _gvn.transform(new (C) NegDNode (absxpowy));
roland@3473 1861 // (1&(long)y)==1?-DPow(abs(x), y):DPow(abs(x), y)
roland@3473 1862 Node *signresult = NULL;
roland@3473 1863 if (ConditionalMoveLimit != 0) {
roland@3473 1864 signresult = _gvn.transform( CMoveNode::make(C, NULL, bol3, absxpowy, negabsxpowy, Type::DOUBLE));
roland@3473 1865 } else {
roland@3473 1866 IfNode *ifyeven = create_and_xform_if(ylong_path,bol3, PROB_FAIR, COUNT_UNKNOWN);
kvn@3680 1867 RegionNode *r = new (C) RegionNode(3);
kvn@3680 1868 Node *phi = new (C) PhiNode(r, Type::DOUBLE);
drchase@4918 1869 r->init_req(1, _gvn.transform(new (C) IfFalseNode(ifyeven)));
drchase@4918 1870 r->init_req(2, _gvn.transform(new (C) IfTrueNode(ifyeven)));
roland@3473 1871 phi->init_req(1, absxpowy);
roland@3473 1872 phi->init_req(2, negabsxpowy);
roland@3473 1873 signresult = _gvn.transform(phi);
roland@3473 1874 ylong_path = _gvn.transform(r);
roland@3473 1875 record_for_igvn(r);
roland@3473 1876 }
duke@0 1877 // Set complex path fast result
roland@3473 1878 r->init_req(2, ylong_path);
duke@0 1879 phi->init_req(2, signresult);
duke@0 1880
duke@0 1881 static const jlong nan_bits = CONST64(0x7ff8000000000000);
duke@0 1882 Node *slow_result = makecon(TypeD::make(*(double*)&nan_bits)); // return NaN
duke@0 1883 r->init_req(1,slow_path);
duke@0 1884 phi->init_req(1,slow_result);
duke@0 1885
duke@0 1886 // Post merge
duke@0 1887 set_control(_gvn.transform(r));
duke@0 1888 record_for_igvn(r);
twisti@3878 1889 result = _gvn.transform(phi);
duke@0 1890 }
duke@0 1891
roland@3473 1892 finish_pow_exp(result, x, y, OptoRuntime::Math_DD_D_Type(), CAST_FROM_FN_PTR(address, SharedRuntime::dpow), "POW");
duke@0 1893
duke@0 1894 C->set_has_split_ifs(true); // Has chance for split-if optimization
duke@0 1895 return true;
duke@0 1896 }
duke@0 1897
duke@0 1898 //------------------------------runtime_math-----------------------------
duke@0 1899 bool LibraryCallKit::runtime_math(const TypeFunc* call_type, address funcAddr, const char* funcName) {
duke@0 1900 assert(call_type == OptoRuntime::Math_DD_D_Type() || call_type == OptoRuntime::Math_D_D_Type(),
duke@0 1901 "must be (DD)D or (D)D type");
duke@0 1902
duke@0 1903 // Inputs
twisti@3878 1904 Node* a = round_double_node(argument(0));
twisti@3878 1905 Node* b = (call_type == OptoRuntime::Math_DD_D_Type()) ? round_double_node(argument(2)) : NULL;
duke@0 1906
duke@0 1907 const TypePtr* no_memory_effects = NULL;
duke@0 1908 Node* trig = make_runtime_call(RC_LEAF, call_type, funcAddr, funcName,
duke@0 1909 no_memory_effects,
duke@0 1910 a, top(), b, b ? top() : NULL);
kvn@3680 1911 Node* value = _gvn.transform(new (C) ProjNode(trig, TypeFunc::Parms+0));
duke@0 1912 #ifdef ASSERT
kvn@3680 1913 Node* value_top = _gvn.transform(new (C) ProjNode(trig, TypeFunc::Parms+1));
duke@0 1914 assert(value_top == top(), "second value must be top");
duke@0 1915 #endif
duke@0 1916
twisti@3878 1917 set_result(value);
duke@0 1918 return true;
duke@0 1919 }
duke@0 1920
duke@0 1921 //------------------------------inline_math_native-----------------------------
duke@0 1922 bool LibraryCallKit::inline_math_native(vmIntrinsics::ID id) {
twisti@3878 1923 #define FN_PTR(f) CAST_FROM_FN_PTR(address, f)
duke@0 1924 switch (id) {
duke@0 1925 // These intrinsics are not properly supported on all hardware
twisti@3878 1926 case vmIntrinsics::_dcos: return Matcher::has_match_rule(Op_CosD) ? inline_trig(id) :
twisti@3878 1927 runtime_math(OptoRuntime::Math_D_D_Type(), FN_PTR(SharedRuntime::dcos), "COS");
twisti@3878 1928 case vmIntrinsics::_dsin: return Matcher::has_match_rule(Op_SinD) ? inline_trig(id) :
twisti@3878 1929 runtime_math(OptoRuntime::Math_D_D_Type(), FN_PTR(SharedRuntime::dsin), "SIN");
twisti@3878 1930 case vmIntrinsics::_dtan: return Matcher::has_match_rule(Op_TanD) ? inline_trig(id) :
twisti@3878 1931 runtime_math(OptoRuntime::Math_D_D_Type(), FN_PTR(SharedRuntime::dtan), "TAN");
twisti@3878 1932
twisti@3878 1933 case vmIntrinsics::_dlog: return Matcher::has_match_rule(Op_LogD) ? inline_math(id) :
twisti@3878 1934 runtime_math(OptoRuntime::Math_D_D_Type(), FN_PTR(SharedRuntime::dlog), "LOG");
twisti@3878 1935 case vmIntrinsics::_dlog10: return Matcher::has_match_rule(Op_Log10D) ? inline_math(id) :
twisti@3878 1936 runtime_math(OptoRuntime::Math_D_D_Type(), FN_PTR(SharedRuntime::dlog10), "LOG10");
duke@0 1937
duke@0 1938 // These intrinsics are supported on all hardware
twisti@3878 1939 case vmIntrinsics::_dsqrt: return Matcher::has_match_rule(Op_SqrtD) ? inline_math(id) : false;
twisti@3878 1940 case vmIntrinsics::_dabs: return Matcher::has_match_rule(Op_AbsD) ? inline_math(id) : false;
twisti@3878 1941
twisti@3878 1942 case vmIntrinsics::_dexp: return Matcher::has_match_rule(Op_ExpD) ? inline_exp() :
twisti@3878 1943 runtime_math(OptoRuntime::Math_D_D_Type(), FN_PTR(SharedRuntime::dexp), "EXP");
twisti@3878 1944 case vmIntrinsics::_dpow: return Matcher::has_match_rule(Op_PowD) ? inline_pow() :
twisti@3878 1945 runtime_math(OptoRuntime::Math_DD_D_Type(), FN_PTR(SharedRuntime::dpow), "POW");
twisti@3878 1946 #undef FN_PTR
duke@0 1947
duke@0 1948 // These intrinsics are not yet correctly implemented
duke@0 1949 case vmIntrinsics::_datan2:
duke@0 1950 return false;
duke@0 1951
duke@0 1952 default:
twisti@3878 1953 fatal_unexpected_iid(id);
duke@0 1954 return false;
duke@0 1955 }
duke@0 1956 }
duke@0 1957
duke@0 1958 static bool is_simple_name(Node* n) {
duke@0 1959 return (n->req() == 1 // constant
duke@0 1960 || (n->is_Type() && n->as_Type()->type()->singleton())
duke@0 1961 || n->is_Proj() // parameter or return value
duke@0 1962 || n->is_Phi() // local of some sort
duke@0 1963 );
duke@0 1964 }
duke@0 1965
duke@0 1966 //----------------------------inline_min_max-----------------------------------
duke@0 1967 bool LibraryCallKit::inline_min_max(vmIntrinsics::ID id) {
twisti@3878 1968 set_result(generate_min_max(id, argument(0), argument(1)));
duke@0 1969 return true;
duke@0 1970 }
duke@0 1971
rbackman@5562 1972 void LibraryCallKit::inline_math_mathExact(Node* math) {
rbackman@5562 1973 // If we didn't get the expected opcode it means we have optimized
rbackman@5562 1974 // the node to something else and don't need the exception edge.
rbackman@5562 1975 if (!math->is_MathExact()) {
rbackman@5562 1976 set_result(math);
rbackman@5562 1977 return;
rbackman@5562 1978 }
rbackman@5562 1979
rbackman@5356 1980 Node* result = _gvn.transform( new(C) ProjNode(math, MathExactNode::result_proj_node));
rbackman@5356 1981 Node* flags = _gvn.transform( new(C) FlagsProjNode(math, MathExactNode::flags_proj_node));
rbackman@5356 1982
rbackman@5356 1983 Node* bol = _gvn.transform( new (C) BoolNode(flags, BoolTest::overflow) );
rbackman@5356 1984 IfNode* check = create_and_map_if(control(), bol, PROB_UNLIKELY_MAG(3), COUNT_UNKNOWN);
rbackman@5356 1985 Node* fast_path = _gvn.transform( new (C) IfFalseNode(check));
rbackman@5356 1986 Node* slow_path = _gvn.transform( new (C) IfTrueNode(check) );
rbackman@5356 1987
rbackman@5356 1988 {
rbackman@5356 1989 PreserveJVMState pjvms(this);
rbackman@5356 1990 PreserveReexecuteState preexecs(this);
rbackman@5356 1991 jvms()->set_should_reexecute(true);
rbackman@5356 1992
rbackman@5356 1993 set_control(slow_path);
rbackman@5356 1994 set_i_o(i_o());
rbackman@5356 1995
rbackman@5356 1996 uncommon_trap(Deoptimization::Reason_intrinsic,
rbackman@5356 1997 Deoptimization::Action_none);
rbackman@5356 1998 }
rbackman@5356 1999
rbackman@5356 2000 set_control(fast_path);
rbackman@5356 2001 set_result(result);
rbackman@5562 2002 }
rbackman@5562 2003
rbackman@5562 2004 bool LibraryCallKit::inline_math_addExactI(bool is_increment) {
rbackman@5562 2005 Node* arg1 = argument(0);
rbackman@5562 2006 Node* arg2 = NULL;
rbackman@5562 2007
rbackman@5562 2008 if (is_increment) {
rbackman@5613 2009 arg2 = intcon(1);
rbackman@5562 2010 } else {
rbackman@5613 2011 arg2 = argument(1);
rbackman@5562 2012 }
rbackman@5562 2013
rbackman@5562 2014 Node* add = _gvn.transform( new(C) AddExactINode(NULL, arg1, arg2) );
rbackman@5562 2015 inline_math_mathExact(add);
rbackman@5356 2016 return true;
rbackman@5356 2017 }
rbackman@5356 2018
rbackman@5562 2019 bool LibraryCallKit::inline_math_addExactL(bool is_increment) {
rbackman@5562 2020 Node* arg1 = argument(0); // type long
rbackman@5562 2021 // argument(1) == TOP
rbackman@5562 2022 Node* arg2 = NULL;
rbackman@5562 2023
rbackman@5562 2024 if (is_increment) {
rbackman@5562 2025 arg2 = longcon(1);
rbackman@5562 2026 } else {
rbackman@5562 2027 arg2 = argument(2); // type long
rbackman@5562 2028 // argument(3) == TOP
rbackman@5562 2029 }
rbackman@5562 2030
rbackman@5562 2031 Node* add = _gvn.transform(new(C) AddExactLNode(NULL, arg1, arg2));
rbackman@5562 2032 inline_math_mathExact(add);
rbackman@5562 2033 return true;
rbackman@5562 2034 }
rbackman@5562 2035
rbackman@5562 2036 bool LibraryCallKit::inline_math_subtractExactI(bool is_decrement) {
rbackman@5562 2037 Node* arg1 = argument(0);
rbackman@5562 2038 Node* arg2 = NULL;
rbackman@5562 2039
rbackman@5562 2040 if (is_decrement) {
rbackman@5562 2041 arg2 = intcon(1);
rbackman@5562 2042 } else {
rbackman@5562 2043 arg2 = argument(1);
rbackman@5562 2044 }
rbackman@5562 2045
rbackman@5562 2046 Node* sub = _gvn.transform(new(C) SubExactINode(NULL, arg1, arg2));
rbackman@5562 2047 inline_math_mathExact(sub);
rbackman@5562 2048 return true;
rbackman@5562 2049 }
rbackman@5562 2050
rbackman@5562 2051 bool LibraryCallKit::inline_math_subtractExactL(bool is_decrement) {
rbackman@5562 2052 Node* arg1 = argument(0); // type long
rbackman@5562 2053 // argument(1) == TOP
rbackman@5562 2054 Node* arg2 = NULL;
rbackman@5562 2055
rbackman@5562 2056 if (is_decrement) {
rbackman@5562 2057 arg2 = longcon(1);
rbackman@5562 2058 } else {
rbackman@5613 2059 arg2 = argument(2); // type long
rbackman@5562 2060 // argument(3) == TOP
rbackman@5562 2061 }
rbackman@5562 2062
rbackman@5562 2063 Node* sub = _gvn.transform(new(C) SubExactLNode(NULL, arg1, arg2));
rbackman@5562 2064 inline_math_mathExact(sub);
rbackman@5562 2065 return true;
rbackman@5562 2066 }
rbackman@5562 2067
rbackman@5562 2068 bool LibraryCallKit::inline_math_negateExactI() {
rbackman@5562 2069 Node* arg1 = argument(0);
rbackman@5562 2070
rbackman@5562 2071 Node* neg = _gvn.transform(new(C) NegExactINode(NULL, arg1));
rbackman@5562 2072 inline_math_mathExact(neg);
rbackman@5562 2073 return true;
rbackman@5562 2074 }
rbackman@5562 2075
rbackman@5562 2076 bool LibraryCallKit::inline_math_negateExactL() {
rbackman@5562 2077 Node* arg1 = argument(0);
rbackman@5562 2078 // argument(1) == TOP
rbackman@5562 2079
rbackman@5562 2080 Node* neg = _gvn.transform(new(C) NegExactLNode(NULL, arg1));
rbackman@5562 2081 inline_math_mathExact(neg);
rbackman@5562 2082 return true;
rbackman@5562 2083 }
rbackman@5562 2084
rbackman@5562 2085 bool LibraryCallKit::inline_math_multiplyExactI() {
rbackman@5356 2086 Node* arg1 = argument(0);
rbackman@5356 2087 Node* arg2 = argument(1);
rbackman@5356 2088
rbackman@5562 2089 Node* mul = _gvn.transform(new(C) MulExactINode(NULL, arg1, arg2));
rbackman@5562 2090 inline_math_mathExact(mul);
rbackman@5562 2091 return true;
rbackman@5562 2092 }
rbackman@5562 2093
rbackman@5562 2094 bool LibraryCallKit::inline_math_multiplyExactL() {
rbackman@5562 2095 Node* arg1 = argument(0);
rbackman@5562 2096 // argument(1) == TOP
rbackman@5562 2097 Node* arg2 = argument(2);
rbackman@5562 2098 // argument(3) == TOP
rbackman@5562 2099
rbackman@5562 2100 Node* mul = _gvn.transform(new(C) MulExactLNode(NULL, arg1, arg2));
rbackman@5562 2101 inline_math_mathExact(mul);
rbackman@5356 2102 return true;
rbackman@5356 2103 }
rbackman@5356 2104
duke@0 2105 Node*
duke@0 2106 LibraryCallKit::generate_min_max(vmIntrinsics::ID id, Node* x0, Node* y0) {
duke@0 2107 // These are the candidate return value:
duke@0 2108 Node* xvalue = x0;
duke@0 2109 Node* yvalue = y0;
duke@0 2110
duke@0 2111 if (xvalue == yvalue) {
duke@0 2112 return xvalue;
duke@0 2113 }
duke@0 2114
duke@0 2115 bool want_max = (id == vmIntrinsics::_max);
duke@0 2116
duke@0 2117 const TypeInt* txvalue = _gvn.type(xvalue)->isa_int();
duke@0 2118 const TypeInt* tyvalue = _gvn.type(yvalue)->isa_int();
duke@0 2119 if (txvalue == NULL || tyvalue == NULL) return top();
duke@0 2120 // This is not really necessary, but it is consistent with a
duke@0 2121 // hypothetical MaxINode::Value method:
duke@0 2122 int widen = MAX2(txvalue->_widen, tyvalue->_widen);
duke@0 2123
duke@0 2124 // %%% This folding logic should (ideally) be in a different place.
duke@0 2125 // Some should be inside IfNode, and there to be a more reliable
duke@0 2126 // transformation of ?: style patterns into cmoves. We also want
duke@0 2127 // more powerful optimizations around cmove and min/max.
duke@0 2128
duke@0 2129 // Try to find a dominating comparison of these guys.
duke@0 2130 // It can simplify the index computation for Arrays.copyOf
duke@0 2131 // and similar uses of System.arraycopy.
duke@0 2132 // First, compute the normalized version of CmpI(x, y).
duke@0 2133 int cmp_op = Op_CmpI;
duke@0 2134 Node* xkey = xvalue;
duke@0 2135 Node* ykey = yvalue;
drchase@4918 2136 Node* ideal_cmpxy = _gvn.transform(new(C) CmpINode(xkey, ykey));
duke@0 2137 if (ideal_cmpxy->is_Cmp()) {
duke@0 2138 // E.g., if we have CmpI(length - offset, count),
duke@0 2139 // it might idealize to CmpI(length, count + offset)
duke@0 2140 cmp_op = ideal_cmpxy->Opcode();
duke@0 2141 xkey = ideal_cmpxy->in(1);
duke@0 2142 ykey = ideal_cmpxy->in(2);
duke@0 2143 }
duke@0 2144
duke@0 2145 // Start by locating any relevant comparisons.
duke@0 2146 Node* start_from = (xkey->outcnt() < ykey->outcnt()) ? xkey : ykey;
duke@0 2147 Node* cmpxy = NULL;
duke@0 2148 Node* cmpyx = NULL;
duke@0 2149 for (DUIterator_Fast kmax, k = start_from->fast_outs(kmax); k < kmax; k++) {
duke@0 2150 Node* cmp = start_from->fast_out(k);
duke@0 2151 if (cmp->outcnt() > 0 && // must have prior uses
duke@0 2152 cmp->in(0) == NULL && // must be context-independent
duke@0 2153 cmp->Opcode() == cmp_op) { // right kind of compare
duke@0 2154 if (cmp->in(1) == xkey && cmp->in(2) == ykey) cmpxy = cmp;
duke@0 2155 if (cmp->in(1) == ykey && cmp->in(2) == xkey) cmpyx = cmp;
duke@0 2156 }
duke@0 2157 }
duke@0 2158
duke@0 2159 const int NCMPS = 2;
duke@0 2160 Node* cmps[NCMPS] = { cmpxy, cmpyx };
duke@0 2161 int cmpn;
duke@0 2162 for (cmpn = 0; cmpn < NCMPS; cmpn++) {
duke@0 2163 if (cmps[cmpn] != NULL) break; // find a result
duke@0 2164 }
duke@0 2165 if (cmpn < NCMPS) {
duke@0 2166 // Look for a dominating test that tells us the min and max.
duke@0 2167 int depth = 0; // Limit search depth for speed
duke@0 2168 Node* dom = control();
duke@0 2169 for (; dom != NULL; dom = IfNode::up_one_dom(dom, true)) {
duke@0 2170 if (++depth >= 100) break;
duke@0 2171 Node* ifproj = dom;
duke@0 2172 if (!ifproj->is_Proj()) continue;
duke@0 2173 Node* iff = ifproj->in(0);
duke@0 2174 if (!iff->is_If()) continue;
duke@0 2175 Node* bol = iff->in(1);
duke@0 2176 if (!bol->is_Bool()) continue;
duke@0 2177 Node* cmp = bol->in(1);
duke@0 2178 if (cmp == NULL) continue;
duke@0 2179 for (cmpn = 0; cmpn < NCMPS; cmpn++)
duke@0 2180 if (cmps[cmpn] == cmp) break;
duke@0 2181 if (cmpn == NCMPS) continue;
duke@0 2182 BoolTest::mask btest = bol->as_Bool()->_test._test;
duke@0 2183 if (ifproj->is_IfFalse()) btest = BoolTest(btest).negate();
duke@0 2184 if (cmp->in(1) == ykey) btest = BoolTest(btest).commute();
duke@0 2185 // At this point, we know that 'x btest y' is true.
duke@0 2186 switch (btest) {
duke@0 2187 case BoolTest::eq:
duke@0 2188 // They are proven equal, so we can collapse the min/max.
duke@0 2189 // Either value is the answer. Choose the simpler.
duke@0 2190 if (is_simple_name(yvalue) && !is_simple_name(xvalue))
duke@0 2191 return yvalue;
duke@0 2192 return xvalue;
duke@0 2193 case BoolTest::lt: // x < y
duke@0 2194 case BoolTest::le: // x <= y
duke@0 2195 return (want_max ? yvalue : xvalue);
duke@0 2196 case BoolTest::gt: // x > y
duke@0 2197 case BoolTest::ge: // x >= y
duke@0 2198 return (want_max ? xvalue : yvalue);
duke@0 2199 }
duke@0 2200 }
duke@0 2201 }
duke@0 2202
duke@0 2203 // We failed to find a dominating test.
duke@0 2204 // Let's pick a test that might GVN with prior tests.
duke@0 2205 Node* best_bol = NULL;
duke@0 2206 BoolTest::mask best_btest = BoolTest::illegal;
duke@0 2207 for (cmpn = 0; cmpn < NCMPS; cmpn++) {
duke@0 2208 Node* cmp = cmps[cmpn];
duke@0 2209 if (cmp == NULL) continue;
duke@0 2210 for (DUIterator_Fast jmax, j = cmp->fast_outs(jmax); j < jmax; j++) {
duke@0 2211 Node* bol = cmp->fast_out(j);
duke@0 2212 if (!bol->is_Bool()) continue;
duke@0 2213 BoolTest::mask btest = bol->as_Bool()->_test._test;
duke@0 2214 if (btest == BoolTest::eq || btest == BoolTest::ne) continue;
duke@0 2215 if (cmp->in(1) == ykey) btest = BoolTest(btest).commute();
duke@0 2216 if (bol->outcnt() > (best_bol == NULL ? 0 : best_bol->outcnt())) {
duke@0 2217 best_bol = bol->as_Bool();
duke@0 2218 best_btest = btest;
duke@0 2219 }
duke@0 2220 }
duke@0 2221 }
duke@0 2222
duke@0 2223 Node* answer_if_true = NULL;
duke@0 2224 Node* answer_if_false = NULL;
duke@0 2225 switch (best_btest) {
duke@0 2226 default:
duke@0 2227 if (cmpxy == NULL)
duke@0 2228 cmpxy = ideal_cmpxy;
drchase@4918 2229 best_bol = _gvn.transform(new(C) BoolNode(cmpxy, BoolTest::lt));
duke@0 2230 // and fall through:
duke@0 2231 case BoolTest::lt: // x < y
duke@0 2232 case BoolTest::le: // x <= y
duke@0 2233 answer_if_true = (want_max ? yvalue : xvalue);
duke@0 2234 answer_if_false = (want_max ? xvalue : yvalue);
duke@0 2235 break;
duke@0 2236 case BoolTest::gt: // x > y
duke@0 2237 case BoolTest::ge: // x >= y
duke@0 2238 answer_if_true = (want_max ? xvalue : yvalue);
duke@0 2239 answer_if_false = (want_max ? yvalue : xvalue);
duke@0 2240 break;
duke@0 2241 }
duke@0 2242
duke@0 2243 jint hi, lo;
duke@0 2244 if (want_max) {
duke@0 2245 // We can sharpen the minimum.
duke@0 2246 hi = MAX2(txvalue->_hi, tyvalue->_hi);
duke@0 2247 lo = MAX2(txvalue->_lo, tyvalue->_lo);
duke@0 2248 } else {
duke@0 2249 // We can sharpen the maximum.
duke@0 2250 hi = MIN2(txvalue->_hi, tyvalue->_hi);
duke@0 2251 lo = MIN2(txvalue->_lo, tyvalue->_lo);
duke@0 2252 }
duke@0 2253
duke@0 2254 // Use a flow-free graph structure, to avoid creating excess control edges
duke@0 2255 // which could hinder other optimizations.
duke@0 2256 // Since Math.min/max is often used with arraycopy, we want
duke@0 2257 // tightly_coupled_allocation to be able to see beyond min/max expressions.
duke@0 2258 Node* cmov = CMoveNode::make(C, NULL, best_bol,
duke@0 2259 answer_if_false, answer_if_true,
duke@0 2260 TypeInt::make(lo, hi, widen));
duke@0 2261
duke@0 2262 return _gvn.transform(cmov);
duke@0 2263
duke@0 2264 /*
duke@0 2265 // This is not as desirable as it may seem, since Min and Max
duke@0 2266 // nodes do not have a full set of optimizations.
duke@0 2267 // And they would interfere, anyway, with 'if' optimizations
duke@0 2268 // and with CMoveI canonical forms.
duke@0 2269 switch (id) {
duke@0 2270 case vmIntrinsics::_min:
duke@0 2271 result_val = _gvn.transform(new (C, 3) MinINode(x,y)); break;
duke@0 2272 case vmIntrinsics::_max:
duke@0 2273 result_val = _gvn.transform(new (C, 3) MaxINode(x,y)); break;
duke@0 2274 default:
duke@0 2275 ShouldNotReachHere();
duke@0 2276 }
duke@0 2277 */
duke@0 2278 }
duke@0 2279
duke@0 2280 inline int
duke@0 2281 LibraryCallKit::classify_unsafe_addr(Node* &base, Node* &offset) {
duke@0 2282 const TypePtr* base_type = TypePtr::NULL_PTR;
duke@0 2283 if (base != NULL) base_type = _gvn.type(base)->isa_ptr();
duke@0 2284 if (base_type == NULL) {
duke@0 2285 // Unknown type.
duke@0 2286 return Type::AnyPtr;
duke@0 2287 } else if (base_type == TypePtr::NULL_PTR) {
duke@0 2288 // Since this is a NULL+long form, we have to switch to a rawptr.
drchase@4918 2289 base = _gvn.transform(new (C) CastX2PNode(offset));
duke@0 2290 offset = MakeConX(0);
duke@0 2291 return Type::RawPtr;
duke@0 2292 } else if (base_type->base() == Type::RawPtr) {
duke@0 2293 return Type::RawPtr;
duke@0 2294 } else if (base_type->isa_oopptr()) {
duke@0 2295 // Base is never null => always a heap address.
duke@0 2296 if (base_type->ptr() == TypePtr::NotNull) {
duke@0 2297 return Type::OopPtr;
duke@0 2298 }
duke@0 2299 // Offset is small => always a heap address.
duke@0 2300 const TypeX* offset_type = _gvn.type(offset)->isa_intptr_t();
duke@0 2301 if (offset_type != NULL &&
duke@0 2302 base_type->offset() == 0 && // (should always be?)
duke@0 2303 offset_type->_lo >= 0 &&
duke@0 2304 !MacroAssembler::needs_explicit_null_check(offset_type->_hi)) {
duke@0 2305 return Type::OopPtr;
duke@0 2306 }
duke@0 2307 // Otherwise, it might either be oop+off or NULL+addr.
duke@0 2308 return Type::AnyPtr;
duke@0 2309 } else {
duke@0 2310 // No information:
duke@0 2311 return Type::AnyPtr;
duke@0 2312 }
duke@0 2313 }
duke@0 2314
duke@0 2315 inline Node* LibraryCallKit::make_unsafe_address(Node* base, Node* offset) {
duke@0 2316 int kind = classify_unsafe_addr(base, offset);
duke@0 2317 if (kind == Type::RawPtr) {
duke@0 2318 return basic_plus_adr(top(), base, offset);
duke@0 2319 } else {
duke@0 2320 return basic_plus_adr(base, offset);
duke@0 2321 }
duke@0 2322 }
duke@0 2323
twisti@3878 2324 //--------------------------inline_number_methods-----------------------------
twisti@3878 2325 // inline int Integer.numberOfLeadingZeros(int)
twisti@3878 2326 // inline int Long.numberOfLeadingZeros(long)
twisti@3878 2327 //
twisti@3878 2328 // inline int Integer.numberOfTrailingZeros(int)
twisti@3878 2329 // inline int Long.numberOfTrailingZeros(long)
twisti@3878 2330 //
twisti@3878 2331 // inline int Integer.bitCount(int)
twisti@3878 2332 // inline int Long.bitCount(long)
twisti@3878 2333 //
twisti@3878 2334 // inline char Character.reverseBytes(char)
twisti@3878 2335 // inline short Short.reverseBytes(short)
twisti@3878 2336 // inline int Integer.reverseBytes(int)
twisti@3878 2337 // inline long Long.reverseBytes(long)
twisti@3878 2338 bool LibraryCallKit::inline_number_methods(vmIntrinsics::ID id) {
twisti@3878 2339 Node* arg = argument(0);
twisti@3878 2340 Node* n;
twisti@775 2341 switch (id) {
twisti@3878 2342 case vmIntrinsics::_numberOfLeadingZeros_i: n = new (C) CountLeadingZerosINode( arg); break;
twisti@3878 2343 case vmIntrinsics::_numberOfLeadingZeros_l: n = new (C) CountLeadingZerosLNode( arg); break;
twisti@3878 2344 case vmIntrinsics::_numberOfTrailingZeros_i: n = new (C) CountTrailingZerosINode(arg); break;
twisti@3878 2345 case vmIntrinsics::_numberOfTrailingZeros_l: n = new (C) CountTrailingZerosLNode(arg); break;
twisti@3878 2346 case vmIntrinsics::_bitCount_i: n = new (C) PopCountINode( arg); break;
twisti@3878 2347 case vmIntrinsics::_bitCount_l: n = new (C) PopCountLNode( arg); break;
twisti@3878 2348 case vmIntrinsics::_reverseBytes_c: n = new (C) ReverseBytesUSNode(0, arg); break;
twisti@3878 2349 case vmIntrinsics::_reverseBytes_s: n = new (C) ReverseBytesSNode( 0, arg); break;
twisti@3878 2350 case vmIntrinsics::_reverseBytes_i: n = new (C) ReverseBytesINode( 0, arg); break;
twisti@3878 2351 case vmIntrinsics::_reverseBytes_l: n = new (C) ReverseBytesLNode( 0, arg); break;
twisti@3878 2352 default: fatal_unexpected_iid(id); break;
twisti@775 2353 }
twisti@3878 2354 set_result(_gvn.transform(n));
twisti@775 2355 return true;
twisti@775 2356 }
twisti@775 2357
duke@0 2358 //----------------------------inline_unsafe_access----------------------------
duke@0 2359
duke@0 2360 const static BasicType T_ADDRESS_HOLDER = T_LONG;
duke@0 2361
kvn@3567 2362 // Helper that guards and inserts a pre-barrier.
kvn@3567 2363 void LibraryCallKit::insert_pre_barrier(Node* base_oop, Node* offset,
twisti@3878 2364 Node* pre_val, bool need_mem_bar) {
johnc@2346 2365 // We could be accessing the referent field of a reference object. If so, when G1
johnc@2346 2366 // is enabled, we need to log the value in the referent field in an SATB buffer.
johnc@2346 2367 // This routine performs some compile time filters and generates suitable
johnc@2346 2368 // runtime filters that guard the pre-barrier code.
kvn@3567 2369 // Also add memory barrier for non volatile load from the referent field
kvn@3567 2370 // to prevent commoning of loads across safepoint.
kvn@3567 2371 if (!UseG1GC && !need_mem_bar)
kvn@3567 2372 return;
johnc@2346 2373
johnc@2346 2374 // Some compile time checks.
johnc@2346 2375
johnc@2346 2376 // If offset is a constant, is it java_lang_ref_Reference::_reference_offset?
johnc@2346 2377 const TypeX* otype = offset->find_intptr_t_type();
johnc@2346 2378 if (otype != NULL && otype->is_con() &&
johnc@2346 2379 otype->get_con() != java_lang_ref_Reference::referent_offset) {
johnc@2346 2380 // Constant offset but not the reference_offset so just return
johnc@2346 2381 return;
johnc@2346 2382 }
johnc@2346 2383
johnc@2346 2384 // We only need to generate the runtime guards for instances.
johnc@2346 2385 const TypeOopPtr* btype = base_oop->bottom_type()->isa_oopptr();
johnc@2346 2386 if (btype != NULL) {
johnc@2346 2387 if (btype->isa_aryptr()) {
johnc@2346 2388 // Array type so nothing to do
johnc@2346 2389 return;
johnc@2346 2390 }
johnc@2346 2391
johnc@2346 2392 const TypeInstPtr* itype = btype->isa_instptr();
johnc@2346 2393 if (itype != NULL) {
kvn@3567 2394 // Can the klass of base_oop be statically determined to be
kvn@3567 2395 // _not_ a sub-class of Reference and _not_ Object?
johnc@2346 2396 ciKlass* klass = itype->klass();
kvn@3567 2397 if ( klass->is_loaded() &&
kvn@3567 2398 !klass->is_subtype_of(env()->Reference_klass()) &&
kvn@3567 2399 !env()->Object_klass()->is_subtype_of(klass)) {
johnc@2346 2400 return;
johnc@2346 2401 }
johnc@2346 2402 }
johnc@2346 2403 }
johnc@2346 2404
johnc@2346 2405 // The compile time filters did not reject base_oop/offset so
johnc@2346 2406 // we need to generate the following runtime filters
johnc@2346 2407 //
johnc@2346 2408 // if (offset == java_lang_ref_Reference::_reference_offset) {
kvn@3567 2409 // if (instance_of(base, java.lang.ref.Reference)) {
kvn@3567 2410 // pre_barrier(_, pre_val, ...);
johnc@2346 2411 // }
johnc@2346 2412 // }
johnc@2346 2413
twisti@3878 2414 float likely = PROB_LIKELY( 0.999);
twisti@3878 2415 float unlikely = PROB_UNLIKELY(0.999);
johnc@2346 2416
johnc@2352 2417 IdealKit ideal(this);
johnc@2346 2418 #define __ ideal.
johnc@2346 2419
johnc@2351 2420 Node* referent_off = __ ConX(java_lang_ref_Reference::referent_offset);
johnc@2346 2421
johnc@2346 2422 __ if_then(offset, BoolTest::eq, referent_off, unlikely); {
johnc@2346 2423 // Update graphKit memory and control from IdealKit.
johnc@2352 2424 sync_kit(ideal);
johnc@2346 2425
johnc@2346 2426 Node* ref_klass_con = makecon(TypeKlassPtr::make(env()->Reference_klass()));
johnc@2346 2427 Node* is_instof = gen_instanceof(base_oop, ref_klass_con);
johnc@2346 2428
johnc@2346 2429 // Update IdealKit memory and control from graphKit.
johnc@2352 2430 __ sync_kit(this);
johnc@2346 2431
johnc@2346 2432 Node* one = __ ConI(1);
kvn@3567 2433 // is_instof == 0 if base_oop == NULL
johnc@2346 2434 __ if_then(is_instof, BoolTest::eq, one, unlikely); {
johnc@2346 2435
johnc@2346 2436 // Update graphKit from IdeakKit.
johnc@2352 2437 sync_kit(ideal);
johnc@2346 2438
johnc@2346 2439 // Use the pre-barrier to record the value in the referent field
johnc@2346 2440 pre_barrier(false /* do_load */,
johnc@2346 2441 __ ctrl(),
johnc@2355 2442 NULL /* obj */, NULL /* adr */, max_juint /* alias_idx */, NULL /* val */, NULL /* val_type */,
johnc@2346 2443 pre_val /* pre_val */,
johnc@2346 2444 T_OBJECT);
kvn@3567 2445 if (need_mem_bar) {
kvn@3567 2446 // Add memory barrier to prevent commoning reads from this field
kvn@3567 2447 // across safepoint since GC can change its value.
kvn@3567 2448 insert_mem_bar(Op_MemBarCPUOrder);
kvn@3567 2449 }
johnc@2346 2450 // Update IdealKit from graphKit.
johnc@2352 2451 __ sync_kit(this);
johnc@2346 2452
johnc@2346 2453 } __ end_if(); // _ref_type != ref_none
johnc@2346 2454 } __ end_if(); // offset == referent_offset
johnc@2346 2455
johnc@2346 2456 // Final sync IdealKit and GraphKit.
johnc@2352 2457 final_sync(ideal);
johnc@2346 2458 #undef __
johnc@2346 2459 }
johnc@2346 2460
johnc@2346 2461
duke@0 2462 // Interpret Unsafe.fieldOffset cookies correctly:
duke@0 2463 extern jlong Unsafe_field_offset_to_byte_offset(jlong field_offset);
duke@0 2464
roland@3671 2465 const TypeOopPtr* LibraryCallKit::sharpen_unsafe_type(Compile::AliasType* alias_type, const TypePtr *adr_type, bool is_native_ptr) {
roland@3671 2466 // Attempt to infer a sharper value type from the offset and base type.
roland@3671 2467 ciKlass* sharpened_klass = NULL;
roland@3671 2468
roland@3671 2469 // See if it is an instance field, with an object type.
roland@3671 2470 if (alias_type->field() != NULL) {
roland@3671 2471 assert(!is_native_ptr, "native pointer op cannot use a java address");
roland@3671 2472 if (alias_type->field()->type()->is_klass()) {
roland@3671 2473 sharpened_klass = alias_type->field()->type()->as_klass();
roland@3671 2474 }
roland@3671 2475 }
roland@3671 2476
roland@3671 2477 // See if it is a narrow oop array.
roland@3671 2478 if (adr_type->isa_aryptr()) {
roland@3671 2479 if (adr_type->offset() >= objArrayOopDesc::base_offset_in_bytes()) {
roland@3671 2480 const TypeOopPtr *elem_type = adr_type->is_aryptr()->elem()->isa_oopptr();
roland@3671 2481 if (elem_type != NULL) {
roland@3671 2482 sharpened_klass = elem_type->klass();
roland@3671 2483 }
roland@3671 2484 }
roland@3671 2485 }
roland@3671 2486
twisti@3723 2487 // The sharpened class might be unloaded if there is no class loader
twisti@3723 2488 // contraint in place.
twisti@3723 2489 if (sharpened_klass != NULL && sharpened_klass->is_loaded()) {
roland@3671 2490 const TypeOopPtr* tjp = TypeOopPtr::make_from_klass(sharpened_klass);
roland@3671 2491
roland@3671 2492 #ifndef PRODUCT
kvn@5328 2493 if (C->print_intrinsics() || C->print_inlining()) {
twisti@3723 2494 tty->print(" from base type: "); adr_type->dump();
twisti@3723 2495 tty->print(" sharpened value: "); tjp->dump();
roland@3671 2496 }
roland@3671 2497 #endif
roland@3671 2498 // Sharpen the value type.
roland@3671 2499 return tjp;
roland@3671 2500 }
roland@3671 2501 return NULL;
roland@3671 2502 }
roland@3671 2503
duke@0 2504 bool LibraryCallKit::inline_unsafe_access(bool is_native_ptr, bool is_store, BasicType type, bool is_volatile) {
duke@0 2505 if (callee()->is_static()) return false; // caller must have the capability!
duke@0 2506
duke@0 2507 #ifndef PRODUCT
duke@0 2508 {
duke@0 2509 ResourceMark rm;
duke@0 2510 // Check the signatures.
twisti@3878 2511 ciSignature* sig = callee()->signature();
duke@0 2512 #ifdef ASSERT
duke@0 2513 if (!is_store) {
duke@0 2514 // Object getObject(Object base, int/long offset), etc.
duke@0 2515 BasicType rtype = sig->return_type()->basic_type();
duke@0 2516 if (rtype == T_ADDRESS_HOLDER && callee()->name() == ciSymbol::getAddress_name())
duke@0 2517 rtype = T_ADDRESS; // it is really a C void*
duke@0 2518 assert(rtype == type, "getter must return the expected value");
duke@0 2519 if (!is_native_ptr) {
duke@0 2520 assert(sig->count() == 2, "oop getter has 2 arguments");
duke@0 2521 assert(sig->type_at(0)->basic_type() == T_OBJECT, "getter base is object");
duke@0 2522 assert(sig->type_at(1)->basic_type() == T_LONG, "getter offset is correct");
duke@0 2523 } else {
duke@0 2524 assert(sig->count() == 1, "native getter has 1 argument");
duke@0 2525 assert(sig->type_at(0)->basic_type() == T_LONG, "getter base is long");
duke@0 2526 }
duke@0 2527 } else {
duke@0 2528 // void putObject(Object base, int/long offset, Object x), etc.
duke@0 2529 assert(sig->return_type()->basic_type() == T_VOID, "putter must not return a value");
duke@0 2530 if (!is_native_ptr) {
duke@0 2531 assert(sig->count() == 3, "oop putter has 3 arguments");
duke@0 2532 assert(sig->type_at(0)->basic_type() == T_OBJECT, "putter base is object");
duke@0 2533 assert(sig->type_at(1)->basic_type() == T_LONG, "putter offset is correct");
duke@0 2534 } else {
duke@0 2535 assert(sig->count() == 2, "native putter has 2 arguments");
duke@0 2536 assert(sig->type_at(0)->basic_type() == T_LONG, "putter base is long");
duke@0 2537 }
duke@0 2538 BasicType vtype = sig->type_at(sig->count()-1)->basic_type();
duke@0 2539 if (vtype == T_ADDRESS_HOLDER && callee()->name() == ciSymbol::putAddress_name())
duke@0 2540 vtype = T_ADDRESS; // it is really a C void*
duke@0 2541 assert(vtype == type, "putter must accept the expected value");
duke@0 2542 }
duke@0 2543 #endif // ASSERT
duke@0 2544 }
duke@0 2545 #endif //PRODUCT
duke@0 2546
duke@0 2547 C->set_has_unsafe_access(true); // Mark eventual nmethod as "unsafe".
duke@0 2548
twisti@3878 2549 Node* receiver = argument(0); // type: oop
twisti@3878 2550
twisti@3878 2551 // Build address expression. See the code in inline_unsafe_prefetch.
twisti@3878 2552 Node* adr;
twisti@3878 2553 Node* heap_base_oop = top();
twisti@3878 2554 Node* offset = top();
duke@0 2555 Node* val;
johnc@2346 2556
duke@0 2557 if (!is_native_ptr) {
twisti@3878 2558 // The base is either a Java object or a value produced by Unsafe.staticFieldBase
twisti@3878 2559 Node* base = argument(1); // type: oop
duke@0 2560 // The offset is a value produced by Unsafe.staticFieldOffset or Unsafe.objectFieldOffset
twisti@3878 2561 offset = argument(2); // type: long
duke@0 2562 // We currently rely on the cookies produced by Unsafe.xxxFieldOffset
duke@0 2563 // to be plain byte offsets, which are also the same as those accepted
duke@0 2564 // by oopDesc::field_base.
duke@0 2565 assert(Unsafe_field_offset_to_byte_offset(11) == 11,
duke@0 2566 "fieldOffset must be byte-scaled");
duke@0 2567 // 32-bit machines ignore the high half!
duke@0 2568 offset = ConvL2X(offset);
duke@0 2569 adr = make_unsafe_address(base, offset);
duke@0 2570 heap_base_oop = base;
twisti@3878 2571 val = is_store ? argument(4) : NULL;
duke@0 2572 } else {
twisti@3878 2573 Node* ptr = argument(1); // type: long
twisti@3878 2574 ptr = ConvL2X(ptr); // adjust Java long to machine word
duke@0 2575 adr = make_unsafe_address(NULL, ptr);
twisti@3878 2576 val = is_store ? argument(3) : NULL;
duke@0 2577 }
duke@0 2578
duke@0 2579 const TypePtr *adr_type = _gvn.type(adr)->isa_ptr();
duke@0 2580
duke@0 2581 // First guess at the value type.
duke@0 2582 const Type *value_type = Type::get_const_basic_type(type);
duke@0 2583
duke@0 2584 // Try to categorize the address. If it comes up as TypeJavaPtr::BOTTOM,
duke@0 2585 // there was not enough information to nail it down.
duke@0 2586 Compile::AliasType* alias_type = C->alias_type(adr_type);
duke@0 2587 assert(alias_type->index() != Compile::AliasIdxBot, "no bare pointers here");
duke@0 2588
duke@0 2589 // We will need memory barriers unless we can determine a unique
duke@0 2590 // alias category for this reference. (Note: If for some reason
duke@0 2591 // the barriers get omitted and the unsafe reference begins to "pollute"
duke@0 2592 // the alias analysis of the rest of the graph, either Compile::can_alias
duke@0 2593 // or Compile::must_alias will throw a diagnostic assert.)
duke@0 2594 bool need_mem_bar = (alias_type->adr_type() == TypeOopPtr::BOTTOM);
duke@0 2595
johnc@2346 2596 // If we are reading the value of the referent field of a Reference
johnc@2346 2597 // object (either by using Unsafe directly or through reflection)
johnc@2346 2598 // then, if G1 is enabled, we need to record the referent in an
johnc@2346 2599 // SATB log buffer using the pre-barrier mechanism.
kvn@3567 2600 // Also we need to add memory barrier to prevent commoning reads
kvn@3567 2601 // from this field across safepoint since GC can change its value.
kvn@3567 2602 bool need_read_barrier = !is_native_ptr && !is_store &&
johnc@2346 2603 offset != top() && heap_base_oop != top();
johnc@2346 2604
duke@0 2605 if (!is_store && type == T_OBJECT) {
roland@3671 2606 const TypeOopPtr* tjp = sharpen_unsafe_type(alias_type, adr_type, is_native_ptr);
roland@3671 2607 if (tjp != NULL) {
duke@0 2608 value_type = tjp;
duke@0 2609 }
duke@0 2610 }
duke@0 2611
twisti@3878 2612 receiver = null_check(receiver);
duke@0 2613 if (stopped()) {
duke@0 2614 return true;
duke@0 2615 }
duke@0 2616 // Heap pointers get a null-check from the interpreter,
duke@0 2617 // as a courtesy. However, this is not guaranteed by Unsafe,
duke@0 2618 // and it is not possible to fully distinguish unintended nulls
duke@0 2619 // from intended ones in this API.
duke@0 2620
duke@0 2621 if (is_volatile) {
duke@0 2622 // We need to emit leading and trailing CPU membars (see below) in
duke@0 2623 // addition to memory membars when is_volatile. This is a little
duke@0 2624 // too strong, but avoids the need to insert per-alias-type
duke@0 2625 // volatile membars (for stores; compare Parse::do_put_xxx), which
twisti@605 2626 // we cannot do effectively here because we probably only have a
duke@0 2627 // rough approximation of type.
duke@0 2628 need_mem_bar = true;
duke@0 2629 // For Stores, place a memory ordering barrier now.
duke@0 2630 if (is_store)
duke@0 2631 insert_mem_bar(Op_MemBarRelease);
duke@0 2632 }
duke@0 2633
duke@0 2634 // Memory barrier to prevent normal and 'unsafe' accesses from
duke@0 2635 // bypassing each other. Happens after null checks, so the
duke@0 2636 // exception paths do not take memory state from the memory barrier,
duke@0 2637 // so there's no problems making a strong assert about mixing users
duke@0 2638 // of safe & unsafe memory. Otherwise fails in a CTW of rt.jar
duke@0 2639 // around 5701, class sun/reflect/UnsafeBooleanFieldAccessorImpl.
duke@0 2640 if (need_mem_bar) insert_mem_bar(Op_MemBarCPUOrder);
duke@0 2641
duke@0 2642 if (!is_store) {
duke@0 2643 Node* p = make_load(control(), adr, value_type, type, adr_type, is_volatile);
twisti@3878 2644 // load value
duke@0 2645 switch (type) {
duke@0 2646 case T_BOOLEAN:
duke@0 2647 case T_CHAR:
duke@0 2648 case T_BYTE:
duke@0 2649 case T_SHORT:
duke@0 2650 case T_INT:
twisti@3878 2651 case T_LONG:
duke@0 2652 case T_FLOAT:
twisti@3878 2653 case T_DOUBLE:
johnc@2346 2654 break;
duke@0 2655 case T_OBJECT:
johnc@2346 2656 if (need_read_barrier) {
twisti@3878 2657 insert_pre_barrier(heap_base_oop, offset, p, !(is_volatile || need_mem_bar));
johnc@2346 2658 }
duke@0 2659 break;
duke@0 2660 case T_ADDRESS:
duke@0 2661 // Cast to an int type.
twisti@3878 2662 p = _gvn.transform(new (C) CastP2XNode(NULL, p));
duke@0 2663 p = ConvX2L(p);
duke@0 2664 break;
twisti@3878 2665 default:
twisti@3878 2666 fatal(err_msg_res("unexpected type %d: %s", type, type2name(type)));
duke@0 2667 break;
duke@0 2668 }
twisti@3878 2669 // The load node has the control of the preceding MemBarCPUOrder. All
twisti@3878 2670 // following nodes will have the control of the MemBarCPUOrder inserted at
twisti@3878 2671 // the end of this method. So, pushing the load onto the stack at a later
twisti@3878 2672 // point is fine.
twisti@3878 2673 set_result(p);
duke@0 2674 } else {
duke@0 2675 // place effect of store into memory
duke@0 2676 switch (type) {
duke@0 2677 case T_DOUBLE:
duke@0 2678 val = dstore_rounding(val);
duke@0 2679 break;
duke@0 2680 case T_ADDRESS:
duke@0 2681 // Repackage the long as a pointer.
duke@0 2682 val = ConvL2X(val);
drchase@4918 2683 val = _gvn.transform(new (C) CastX2PNode(val));
duke@0 2684 break;
duke@0 2685 }
duke@0 2686
duke@0 2687 if (type != T_OBJECT ) {
duke@0 2688 (void) store_to_memory(control(), adr, val, type, adr_type, is_volatile);
duke@0 2689 } else {
duke@0 2690 // Possibly an oop being stored to Java heap or native memory
duke@0 2691 if (!TypePtr::NULL_PTR->higher_equal(_gvn.type(heap_base_oop))) {
duke@0 2692 // oop to Java heap.
never@825 2693 (void) store_oop_to_unknown(control(), heap_base_oop, adr, adr_type, val, type);
duke@0 2694 } else {
duke@0 2695 // We can't tell at compile time if we are storing in the Java heap or outside
duke@0 2696 // of it. So we need to emit code to conditionally do the proper type of
duke@0 2697 // store.
duke@0 2698
kvn@2291 2699 IdealKit ideal(this);
kvn@851 2700 #define __ ideal.
duke@0 2701 // QQQ who knows what probability is here??
kvn@851 2702 __ if_then(heap_base_oop, BoolTest::ne, null(), PROB_UNLIKELY(0.999)); {
kvn@851 2703 // Sync IdealKit and graphKit.
kvn@2291 2704 sync_kit(ideal);
kvn@851 2705 Node* st = store_oop_to_unknown(control(), heap_base_oop, adr, adr_type, val, type);
kvn@851 2706 // Update IdealKit memory.
kvn@2291 2707 __ sync_kit(this);
kvn@851 2708 } __ else_(); {
kvn@851 2709 __ store(__ ctrl(), adr, val, type, alias_type->index(), is_volatile);
kvn@851 2710 } __ end_if();
kvn@851 2711 // Final sync IdealKit and GraphKit.
kvn@2291 2712 final_sync(ideal);
kvn@851 2713 #undef __
duke@0 2714 }
duke@0 2715 }
duke@0 2716 }
duke@0 2717
duke@0 2718 if (is_volatile) {
duke@0 2719 if (!is_store)
duke@0 2720 insert_mem_bar(Op_MemBarAcquire);
duke@0 2721 else
duke@0 2722 insert_mem_bar(Op_MemBarVolatile);
duke@0 2723 }
duke@0 2724
duke@0 2725 if (need_mem_bar) insert_mem_bar(Op_MemBarCPUOrder);
duke@0 2726
duke@0 2727 return true;
duke@0 2728 }
duke@0 2729
duke@0 2730 //----------------------------inline_unsafe_prefetch----------------------------
duke@0 2731
duke@0 2732 bool LibraryCallKit::inline_unsafe_prefetch(bool is_native_ptr, bool is_store, bool is_static) {
duke@0 2733 #ifndef PRODUCT
duke@0 2734 {
duke@0 2735 ResourceMark rm;
duke@0 2736 // Check the signatures.
twisti@3878 2737 ciSignature* sig = callee()->signature();
duke@0 2738 #ifdef ASSERT
duke@0 2739 // Object getObject(Object base, int/long offset), etc.
duke@0 2740 BasicType rtype = sig->return_type()->basic_type();
duke@0 2741 if (!is_native_ptr) {
duke@0 2742 assert(sig->count() == 2, "oop prefetch has 2 arguments");
duke@0 2743 assert(sig->type_at(0)->basic_type() == T_OBJECT, "prefetch base is object");
duke@0 2744 assert(sig->type_at(1)->basic_type() == T_LONG, "prefetcha offset is correct");
duke@0 2745 } else {
duke@0 2746 assert(sig->count() == 1, "native prefetch has 1 argument");
duke@0 2747 assert(sig->type_at(0)->basic_type() == T_LONG, "prefetch base is long");
duke@0 2748 }
duke@0 2749 #endif // ASSERT
duke@0 2750 }
duke@0 2751 #endif // !PRODUCT
duke@0 2752
duke@0 2753 C->set_has_unsafe_access(true); // Mark eventual nmethod as "unsafe".
duke@0 2754
twisti@3878 2755 const int idx = is_static ? 0 : 1;
twisti@3878 2756 if (!is_static) {
twisti@3878 2757 null_check_receiver();
twisti@3878 2758 if (stopped()) {
twisti@3878 2759 return true;
twisti@3878 2760 }
twisti@3878 2761 }
duke@0 2762
duke@0 2763 // Build address expression. See the code in inline_unsafe_access.
duke@0 2764 Node *adr;
duke@0 2765 if (!is_native_ptr) {
twisti@3878 2766 // The base is either a Java object or a value produced by Unsafe.staticFieldBase
twisti@3878 2767 Node* base = argument(idx + 0); // type: oop
duke@0 2768 // The offset is a value produced by Unsafe.staticFieldOffset or Unsafe.objectFieldOffset
twisti@3878 2769 Node* offset = argument(idx + 1); // type: long
duke@0 2770 // We currently rely on the cookies produced by Unsafe.xxxFieldOffset
duke@0 2771 // to be plain byte offsets, which are also the same as those accepted
duke@0 2772 // by oopDesc::field_base.
duke@0 2773 assert(Unsafe_field_offset_to_byte_offset(11) == 11,
duke@0 2774 "fieldOffset must be byte-scaled");
duke@0 2775 // 32-bit machines ignore the high half!
duke@0 2776 offset = ConvL2X(offset);
duke@0 2777 adr = make_unsafe_address(base, offset);
duke@0 2778 } else {
twisti@3878 2779 Node* ptr = argument(idx + 0); // type: long
twisti@3878 2780 ptr = ConvL2X(ptr); // adjust Java long to machine word
duke@0 2781 adr = make_unsafe_address(NULL, ptr);
duke@0 2782 }
duke@0 2783
duke@0 2784 // Generate the read or write prefetch
duke@0 2785 Node *prefetch;
duke@0 2786 if (is_store) {
kvn@3680 2787 prefetch = new (C) PrefetchWriteNode(i_o(), adr);
duke@0 2788 } else {
kvn@3680 2789 prefetch = new (C) PrefetchReadNode(i_o(), adr);
duke@0 2790 }
duke@0 2791 prefetch->init_req(0, control());
duke@0 2792 set_i_o(_gvn.transform(prefetch));
duke@0 2793
duke@0 2794 return true;
duke@0 2795 }
duke@0 2796
roland@3671 2797 //----------------------------inline_unsafe_load_store----------------------------
twisti@3878 2798 // This method serves a couple of different customers (depending on LoadStoreKind):
twisti@3878 2799 //
twisti@3878 2800 // LS_cmpxchg:
twisti@3878 2801 // public final native boolean compareAndSwapObject(Object o, long offset, Object expected, Object x);
twisti@3878 2802 // public final native boolean compareAndSwapInt( Object o, long offset, int expected, int x);
twisti@3878 2803 // public final native boolean compareAndSwapLong( Object o, long offset, long expected, long x);
twisti@3878 2804 //
twisti@3878 2805 // LS_xadd:
twisti@3878 2806 // public int getAndAddInt( Object o, long offset, int delta)
twisti@3878 2807 // public long getAndAddLong(Object o, long offset, long delta)
twisti@3878 2808 //
twisti@3878 2809 // LS_xchg:
twisti@3878 2810 // int getAndSet(Object o, long offset, int newValue)
twisti@3878 2811 // long getAndSet(Object o, long offset, long newValue)
twisti@3878 2812 // Object getAndSet(Object o, long offset, Object newValue)
twisti@3878 2813 //
roland@3671 2814 bool LibraryCallKit::inline_unsafe_load_store(BasicType type, LoadStoreKind kind) {
duke@0 2815 // This basic scheme here is the same as inline_unsafe_access, but
duke@0 2816 // differs in enough details that combining them would make the code
duke@0 2817 // overly confusing. (This is a true fact! I originally combined
duke@0 2818 // them, but even I was confused by it!) As much code/comments as
duke@0 2819 // possible are retained from inline_unsafe_access though to make
twisti@605 2820 // the correspondences clearer. - dl
duke@0 2821
duke@0 2822 if (callee()->is_static()) return false; // caller must have the capability!
duke@0 2823
duke@0 2824 #ifndef PRODUCT
roland@3671 2825 BasicType rtype;
duke@0 2826 {
duke@0 2827 ResourceMark rm;
twisti@3878 2828 // Check the signatures.
twisti@3878 2829 ciSignature* sig = callee()->signature();
roland@3671 2830 rtype = sig->return_type()->basic_type();
roland@3671 2831 if (kind == LS_xadd || kind == LS_xchg) {
roland@3671 2832 // Check the signatures.
duke@0 2833 #ifdef ASSERT
roland@3671 2834 assert(rtype == type, "get and set must return the expected type");
roland@3671 2835 assert(sig->count() == 3, "get and set has 3 arguments");
roland@3671 2836 assert(sig->type_at(0)->basic_type() == T_OBJECT, "get and set base is object");
roland@3671 2837 assert(sig->type_at(1)->basic_type() == T_LONG, "get and set offset is long");
roland@3671 2838 assert(sig->type_at(2)->basic_type() == type, "get and set must take expected type as new value/delta");
duke@0 2839 #endif // ASSERT
roland@3671 2840 } else if (kind == LS_cmpxchg) {
roland@3671 2841 // Check the signatures.
roland@3671 2842 #ifdef ASSERT
roland@3671 2843 assert(rtype == T_BOOLEAN, "CAS must return boolean");
roland@3671 2844 assert(sig->count() == 4, "CAS has 4 arguments");
roland@3671 2845 assert(sig->type_at(0)->basic_type() == T_OBJECT, "CAS base is object");
roland@3671 2846 assert(sig->type_at(1)->basic_type() == T_LONG, "CAS offset is long");
roland@3671 2847 #endif // ASSERT
roland@3671 2848 } else {
roland@3671 2849 ShouldNotReachHere();
roland@3671 2850 }
duke@0 2851 }
duke@0 2852 #endif //PRODUCT
duke@0 2853
duke@0 2854 C->set_has_unsafe_access(true); // Mark eventual nmethod as "unsafe".
duke@0 2855
twisti@3878 2856 // Get arguments:
twisti@3878 2857 Node* receiver = NULL;
twisti@3878 2858 Node* base = NULL;
twisti@3878 2859 Node* offset = NULL;
twisti@3878 2860 Node* oldval = NULL;
twisti@3878 2861 Node* newval = NULL;
twisti@3878 2862 if (kind == LS_cmpxchg) {
twisti@3878 2863 const bool two_slot_type = type2size[type] == 2;
twisti@3878 2864 receiver = argument(0); // type: oop
twisti@3878 2865 base = argument(1); // type: oop
twisti@3878 2866 offset = argument(2); // type: long
twisti@3878 2867 oldval = argument(4); // type: oop, int, or long
twisti@3878 2868 newval = argument(two_slot_type ? 6 : 5); // type: oop, int, or long
twisti@3878 2869 } else if (kind == LS_xadd || kind == LS_xchg){
twisti@3878 2870 receiver = argument(0); // type: oop
twisti@3878 2871 base = argument(1); // type: oop
twisti@3878 2872 offset = argument(2); // type: long
twisti@3878 2873 oldval = NULL;
twisti@3878 2874 newval = argument(4); // type: oop, int, or long
twisti@3878 2875 }
twisti@3878 2876
twisti@3878 2877 // Null check receiver.
twisti@3878 2878 receiver = null_check(receiver);
duke@0 2879 if (stopped()) {
duke@0 2880 return true;
duke@0 2881 }
duke@0 2882
duke@0 2883 // Build field offset expression.
duke@0 2884 // We currently rely on the cookies produced by Unsafe.xxxFieldOffset
duke@0 2885 // to be plain byte offsets, which are also the same as those accepted
duke@0 2886 // by oopDesc::field_base.
duke@0 2887 assert(Unsafe_field_offset_to_byte_offset(11) == 11, "fieldOffset must be byte-scaled");
duke@0 2888 // 32-bit machines ignore the high half of long offsets
duke@0 2889 offset = ConvL2X(offset);
duke@0 2890 Node* adr = make_unsafe_address(base, offset);
duke@0 2891 const TypePtr *adr_type = _gvn.type(adr)->isa_ptr();
duke@0 2892
roland@3671 2893 // For CAS, unlike inline_unsafe_access, there seems no point in
roland@3671 2894 // trying to refine types. Just use the coarse types here.
duke@0 2895 const Type *value_type = Type::get_const_basic_type(type);
duke@0 2896 Compile::AliasType* alias_type = C->alias_type(adr_type);
duke@0 2897 assert(alias_type->index() != Compile::AliasIdxBot, "no bare pointers here");
roland@3671 2898
roland@3671 2899 if (kind == LS_xchg && type == T_OBJECT) {
roland@3671 2900 const TypeOopPtr* tjp = sharpen_unsafe_type(alias_type, adr_type);
roland@3671 2901 if (tjp != NULL) {
roland@3671 2902 value_type = tjp;
roland@3671 2903 }
roland@3671 2904 }
roland@3671 2905
duke@0 2906 int alias_idx = C->get_alias_index(adr_type);
duke@0 2907
roland@3671 2908 // Memory-model-wise, a LoadStore acts like a little synchronized
roland@3671 2909 // block, so needs barriers on each side. These don't translate
roland@3671 2910 // into actual barriers on most machines, but we still need rest of
duke@0 2911 // compiler to respect ordering.
duke@0 2912
duke@0 2913 insert_mem_bar(Op_MemBarRelease);
duke@0 2914 insert_mem_bar(Op_MemBarCPUOrder);
duke@0 2915
duke@0 2916 // 4984716: MemBars must be inserted before this
duke@0 2917 // memory node in order to avoid a false
duke@0 2918 // dependency which will confuse the scheduler.
duke@0 2919 Node *mem = memory(alias_idx);
duke@0 2920
duke@0 2921 // For now, we handle only those cases that actually exist: ints,
duke@0 2922 // longs, and Object. Adding others should be straightforward.
roland@3671 2923 Node* load_store;
duke@0 2924 switch(type) {
duke@0 2925 case T_INT:
roland@3671 2926 if (kind == LS_xadd) {
kvn@3680 2927 load_store = _gvn.transform(new (C) GetAndAddINode(control(), mem, adr, newval, adr_type));
roland@3671 2928 } else if (kind == LS_xchg) {
kvn@3680 2929 load_store = _gvn.transform(new (C) GetAndSetINode(control(), mem, adr, newval, adr_type));
roland@3671 2930 } else if (kind == LS_cmpxchg) {
kvn@3680 2931 load_store = _gvn.transform(new (C) CompareAndSwapINode(control(), mem, adr, newval, oldval));
roland@3671 2932 } else {
roland@3671 2933 ShouldNotReachHere();
roland@3671 2934 }
duke@0 2935 break;
duke@0 2936 case T_LONG:
roland@3671 2937 if (kind == LS_xadd) {
kvn@3680 2938 load_store = _gvn.transform(new (C) GetAndAddLNode(control(), mem, adr, newval, adr_type));
roland@3671 2939 } else if (kind == LS_xchg) {
kvn@3680 2940 load_store = _gvn.transform(new (C) GetAndSetLNode(control(), mem, adr, newval, adr_type));
roland@3671 2941 } else if (kind == LS_cmpxchg) {
kvn@3680 2942 load_store = _gvn.transform(new (C) CompareAndSwapLNode(control(), mem, adr, newval, oldval));
roland@3671 2943 } else {
roland@3671 2944 ShouldNotReachHere();
roland@3671 2945 }
duke@0 2946 break;
duke@0 2947 case T_OBJECT:
kvn@3086 2948 // Transformation of a value which could be NULL pointer (CastPP #NULL)
kvn@3086 2949 // could be delayed during Parse (for example, in adjust_map_after_if()).
kvn@3086 2950 // Execute transformation here to avoid barrier generation in such case.
kvn@3086 2951 if (_gvn.type(newval) == TypePtr::NULL_PTR)
kvn@3086 2952 newval = _gvn.makecon(TypePtr::NULL_PTR);
kvn@3086 2953
kvn@3086 2954 // Reference stores need a store barrier.
kvn@5202 2955 if (kind == LS_xchg) {
kvn@5202 2956 // If pre-barrier must execute before the oop store, old value will require do_load here.
kvn@5202 2957 if (!can_move_pre_barrier()) {
kvn@5202 2958 pre_barrier(true /* do_load*/,
kvn@5202 2959 control(), base, adr, alias_idx, newval, value_type->make_oopptr(),
kvn@5202 2960 NULL /* pre_val*/,
kvn@5202 2961 T_OBJECT);
kvn@5202 2962 } // Else move pre_barrier to use load_store value, see below.
kvn@5202 2963 } else if (kind == LS_cmpxchg) {
kvn@5202 2964 // Same as for newval above:
kvn@5202 2965 if (_gvn.type(oldval) == TypePtr::NULL_PTR) {
kvn@5202 2966 oldval = _gvn.makecon(TypePtr::NULL_PTR);
kvn@5202 2967 }
kvn@5202 2968 // The only known value which might get overwritten is oldval.
kvn@5202 2969 pre_barrier(false /* do_load */,
kvn@5202 2970 control(), NULL, NULL, max_juint, NULL, NULL,
kvn@5202 2971 oldval /* pre_val */,
kvn@5202 2972 T_OBJECT);
kvn@5202 2973 } else {
kvn@5202 2974 ShouldNotReachHere();
kvn@5202 2975 }
kvn@5202 2976
coleenp@113 2977 #ifdef _LP64
kvn@163 2978 if (adr->bottom_type()->is_ptr_to_narrowoop()) {
kvn@3680 2979 Node *newval_enc = _gvn.transform(new (C) EncodePNode(newval, newval->bottom_type()->make_narrowoop()));
roland@3671 2980 if (kind == LS_xchg) {
kvn@3680 2981 load_store = _gvn.transform(new (C) GetAndSetNNode(control(), mem, adr,
roland@3671 2982 newval_enc, adr_type, value_type->make_narrowoop()));
roland@3671 2983 } else {
roland@3671 2984 assert(kind == LS_cmpxchg, "wrong LoadStore operation");
kvn@3680 2985 Node *oldval_enc = _gvn.transform(new (C) EncodePNode(oldval, oldval->bottom_type()->make_narrowoop()));
kvn@3680 2986 load_store = _gvn.transform(new (C) CompareAndSwapNNode(control(), mem, adr,
roland@3671 2987 newval_enc, oldval_enc));
roland@3671 2988 }
coleenp@113 2989 } else
coleenp@113 2990 #endif
kvn@221 2991 {
roland@3671 2992 if (kind == LS_xchg) {
kvn@3680 2993 load_store = _gvn.transform(new (C) GetAndSetPNode(control(), mem, adr, newval, adr_type, value_type->is_oopptr()));
roland@3671 2994 } else {
roland@3671 2995 assert(kind == LS_cmpxchg, "wrong LoadStore operation");
kvn@3680 2996 load_store = _gvn.transform(new (C) CompareAndSwapPNode(control(), mem, adr, newval, oldval));
roland@3671 2997 }
kvn@221 2998 }
roland@3671 2999 post_barrier(control(), load_store, base, adr, alias_idx, newval, T_OBJECT, true);
duke@0 3000 break;
duke@0 3001 default:
twisti@3878 3002 fatal(err_msg_res("unexpected type %d: %s", type, type2name(type)));
duke@0 3003 break;
duke@0 3004 }
duke@0 3005
roland@3671 3006 // SCMemProjNodes represent the memory state of a LoadStore. Their
roland@3671 3007 // main role is to prevent LoadStore nodes from being optimized away
roland@3671 3008 // when their results aren't used.
drchase@4918 3009 Node* proj = _gvn.transform(new (C) SCMemProjNode(load_store));
duke@0 3010 set_memory(proj, alias_idx);
duke@0 3011
kvn@5202 3012 if (type == T_OBJECT && kind == LS_xchg) {
kvn@5202 3013 #ifdef _LP64
kvn@5202 3014 if (adr->bottom_type()->is_ptr_to_narrowoop()) {
kvn@5202 3015 load_store = _gvn.transform(new (C) DecodeNNode(load_store, load_store->get_ptr_type()));
kvn@5202 3016 }
kvn@5202 3017 #endif
kvn@5202 3018 if (can_move_pre_barrier()) {
kvn@5202 3019 // Don't need to load pre_val. The old value is returned by load_store.
kvn@5202 3020 // The pre_barrier can execute after the xchg as long as no safepoint
kvn@5202 3021 // gets inserted between them.
kvn@5202 3022 pre_barrier(false /* do_load */,
kvn@5202 3023 control(), NULL, NULL, max_juint, NULL, NULL,
kvn@5202 3024 load_store /* pre_val */,
kvn@5202 3025 T_OBJECT);
kvn@5202 3026 }
kvn@5202 3027 }
kvn@5202 3028
duke@0 3029 // Add the trailing membar surrounding the access
duke@0 3030 insert_mem_bar(Op_MemBarCPUOrder);
duke@0 3031 insert_mem_bar(Op_MemBarAcquire);
duke@0 3032
roland@3671 3033 assert(type2size[load_store->bottom_type()->basic_type()] == type2size[rtype], "result type should match");
twisti@3878 3034 set_result(load_store);
duke@0 3035 return true;
duke@0 3036 }
duke@0 3037
twisti@3878 3038 //----------------------------inline_unsafe_ordered_store----------------------
twisti@3878 3039 // public native void sun.misc.Unsafe.putOrderedObject(Object o, long offset, Object x);
twisti@3878 3040 // public native void sun.misc.Unsafe.putOrderedInt(Object o, long offset, int x);
twisti@3878 3041 // public native void sun.misc.Unsafe.putOrderedLong(Object o, long offset, long x);
duke@0 3042 bool LibraryCallKit::inline_unsafe_ordered_store(BasicType type) {
duke@0 3043 // This is another variant of inline_unsafe_access, differing in
duke@0 3044 // that it always issues store-store ("release") barrier and ensures
duke@0 3045 // store-atomicity (which only matters for "long").
duke@0 3046
duke@0 3047 if (callee()->is_static()) return false; // caller must have the capability!
duke@0 3048
duke@0 3049 #ifndef PRODUCT
duke@0 3050 {
duke@0 3051 ResourceMark rm;
duke@0 3052 // Check the signatures.
twisti@3878 3053 ciSignature* sig = callee()->signature();
duke@0 3054 #ifdef ASSERT
duke@0 3055 BasicType rtype = sig->return_type()->basic_type();
duke@0 3056 assert(rtype == T_VOID, "must return void");
duke@0 3057 assert(sig->count() == 3, "has 3 arguments");
duke@0 3058 assert(sig->type_at(0)->basic_type() == T_OBJECT, "base is object");
duke@0 3059 assert(sig->type_at(1)->basic_type() == T_LONG, "offset is long");
duke@0 3060 #endif // ASSERT
duke@0 3061 }
duke@0 3062 #endif //PRODUCT
duke@0 3063
duke@0 3064 C->set_has_unsafe_access(true); // Mark eventual nmethod as "unsafe".
duke@0 3065
twisti@3878 3066 // Get arguments:
twisti@3878 3067 Node* receiver = argument(0); // type: oop
twisti@3878 3068 Node* base = argument(1); // type: oop
twisti@3878 3069 Node* offset = argument(2); // type: long
twisti@3878 3070 Node* val = argument(4); // type: oop, int, or long
twisti@3878 3071
twisti@3878 3072 // Null check receiver.
twisti@3878 3073 receiver = null_check(receiver);
duke@0 3074 if (stopped()) {
duke@0 3075 return true;
duke@0 3076 }
duke@0 3077
duke@0 3078 // Build field offset expression.
duke@0 3079 assert(Unsafe_field_offset_to_byte_offset(11) == 11, "fieldOffset must be byte-scaled");
duke@0 3080 // 32-bit machines ignore the high half of long offsets
duke@0 3081 offset = ConvL2X(offset);
duke@0 3082 Node* adr = make_unsafe_address(base, offset);
duke@0 3083 const TypePtr *adr_type = _gvn.type(adr)->isa_ptr();
duke@0 3084 const Type *value_type = Type::get_const_basic_type(type);
duke@0 3085 Compile::AliasType* alias_type = C->alias_type(adr_type);
duke@0 3086
duke@0 3087 insert_mem_bar(Op_MemBarRelease);
duke@0 3088 insert_mem_bar(Op_MemBarCPUOrder);
duke@0 3089 // Ensure that the store is atomic for longs:
twisti@3878 3090 const bool require_atomic_access = true;
duke@0 3091 Node* store;
duke@0 3092 if (type == T_OBJECT) // reference stores need a store barrier.
never@825 3093 store = store_oop_to_unknown(control(), base, adr, adr_type, val, type);
duke@0 3094 else {
duke@0 3095 store = store_to_memory(control(), adr, val, type, adr_type, require_atomic_access);
duke@0 3096 }
duke@0 3097 insert_mem_bar(Op_MemBarCPUOrder);
duke@0 3098 return true;
duke@0 3099 }
duke@0 3100
kvn@3926 3101 bool LibraryCallKit::inline_unsafe_fence(vmIntrinsics::ID id) {
kvn@3926 3102 // Regardless of form, don't allow previous ld/st to move down,
kvn@3926 3103 // then issue acquire, release, or volatile mem_bar.
kvn@3926 3104 insert_mem_bar(Op_MemBarCPUOrder);
kvn@3926 3105 switch(id) {
kvn@3926 3106 case vmIntrinsics::_loadFence:
kvn@3926 3107 insert_mem_bar(Op_MemBarAcquire);
kvn@3926 3108 return true;
kvn@3926 3109 case vmIntrinsics::_storeFence:
kvn@3926 3110 insert_mem_bar(Op_MemBarRelease);
kvn@3926 3111 return true;
kvn@3926 3112 case vmIntrinsics::_fullFence:
kvn@3926 3113 insert_mem_bar(Op_MemBarVolatile);
kvn@3926 3114 return true;
kvn@3926 3115 default:
kvn@3926 3116 fatal_unexpected_iid(id);
kvn@3926 3117 return false;
kvn@3926 3118 }
kvn@3926 3119 }
kvn@3926 3120
rbackman@5111 3121 bool LibraryCallKit::klass_needs_init_guard(Node* kls) {
rbackman@5111 3122 if (!kls->is_Con()) {
rbackman@5111 3123 return true;
rbackman@5111 3124 }
rbackman@5111 3125 const TypeKlassPtr* klsptr = kls->bottom_type()->isa_klassptr();
rbackman@5111 3126 if (klsptr == NULL) {
rbackman@5111 3127 return true;
rbackman@5111 3128 }
rbackman@5111 3129 ciInstanceKlass* ik = klsptr->klass()->as_instance_klass();
rbackman@5111 3130 // don't need a guard for a klass that is already initialized
rbackman@5111 3131 return !ik->is_initialized();
rbackman@5111 3132 }
rbackman@5111 3133
twisti@3878 3134 //----------------------------inline_unsafe_allocate---------------------------
rbackman@5111 3135 // public native Object sun.misc.Unsafe.allocateInstance(Class<?> cls);
duke@0 3136 bool LibraryCallKit::inline_unsafe_allocate() {
duke@0 3137 if (callee()->is_static()) return false; // caller must have the capability!
twisti@3878 3138
twisti@3878 3139 null_check_receiver(); // null-check, then ignore
twisti@3878 3140 Node* cls = null_check(argument(1));
duke@0 3141 if (stopped()) return true;
duke@0 3142
twisti@3878 3143 Node* kls = load_klass_from_mirror(cls, false, NULL, 0);
twisti@3878 3144 kls = null_check(kls);
duke@0 3145 if (stopped()) return true; // argument was like int.class
duke@0 3146
rbackman@5111 3147 Node* test = NULL;
rbackman@5111 3148 if (LibraryCallKit::klass_needs_init_guard(kls)) {
rbackman@5111 3149 // Note: The argument might still be an illegal value like
rbackman@5111 3150 // Serializable.class or Object[].class. The runtime will handle it.
rbackman@5111 3151 // But we must make an explicit check for initialization.
rbackman@5111 3152 Node* insp = basic_plus_adr(kls, in_bytes(InstanceKlass::init_state_offset()));
rbackman@5111 3153 // Use T_BOOLEAN for InstanceKlass::_init_state so the compiler
rbackman@5111 3154 // can generate code to load it as unsigned byte.
rbackman@5111 3155 Node* inst = make_load(NULL, insp, TypeInt::UBYTE, T_BOOLEAN);
rbackman@5111 3156 Node* bits = intcon(InstanceKlass::fully_initialized);
rbackman@5111 3157 test = _gvn.transform(new (C) SubINode(inst, bits));
rbackman@5111 3158 // The 'test' is non-zero if we need to take a slow path.
rbackman@5111 3159 }
duke@0 3160
duke@0 3161 Node* obj = new_instance(kls, test);
twisti@3878 3162 set_result(obj);
duke@0 3163 return true;
duke@0 3164 }
duke@0 3165
rbackman@3274 3166 #ifdef TRACE_HAVE_INTRINSICS
rbackman@3274 3167 /*
rbackman@3274 3168 * oop -> myklass
rbackman@3274 3169 * myklass->trace_id |= USED
rbackman@3274 3170 * return myklass->trace_id & ~0x3
rbackman@3274 3171 */
rbackman@3274 3172 bool LibraryCallKit::inline_native_classID() {
twisti@3878 3173 null_check_receiver(); // null-check, then ignore
twisti@3878 3174 Node* cls = null_check(argument(1), T_OBJECT);
twisti@3878 3175 Node* kls = load_klass_from_mirror(cls, false, NULL, 0);
twisti@3878 3176 kls = null_check(kls, T_OBJECT);
rbackman@3274 3177 ByteSize offset = TRACE_ID_OFFSET;
rbackman@3274 3178 Node* insp = basic_plus_adr(kls, in_bytes(offset));
rbackman@3274 3179 Node* tvalue = make_load(NULL, insp, TypeLong::LONG, T_LONG);
rbackman@3274 3180 Node* bits = longcon(~0x03l); // ignore bit 0 & 1
kvn@3680 3181 Node* andl = _gvn.transform(new (C) AndLNode(tvalue, bits));
rbackman@3274 3182 Node* clsused = longcon(0x01l); // set the class bit
kvn@3680 3183 Node* orl = _gvn.transform(new (C) OrLNode(tvalue, clsused));
rbackman@3274 3184
rbackman@3274 3185 const TypePtr *adr_type = _gvn.type(insp)->isa_ptr();
rbackman@3274 3186 store_to_memory(control(), insp, orl, T_LONG, adr_type);
twisti@3878 3187 set_result(andl);
rbackman@3274 3188 return true;
rbackman@3274 3189 }
rbackman@3274 3190
rbackman@3274 3191 bool LibraryCallKit::inline_native_threadID() {
rbackman@3274 3192 Node* tls_ptr = NULL;
rbackman@3274 3193 Node* cur_thr = generate_current_thread(tls_ptr);
rbackman@3274 3194 Node* p = basic_plus_adr(top()/*!oop*/, tls_ptr, in_bytes(JavaThread::osthread_offset()));
rbackman@3274 3195 Node* osthread = make_load(NULL, p, TypeRawPtr::NOTNULL, T_ADDRESS);
rbackman@3274 3196 p = basic_plus_adr(top()/*!oop*/, osthread, in_bytes(OSThread::thread_id_offset()));
rbackman@3274 3197
rbackman@3274 3198 Node* threadid = NULL;
rbackman@3274 3199 size_t thread_id_size = OSThread::thread_id_size();
rbackman@3274 3200 if (thread_id_size == (size_t) BytesPerLong) {
rbackman@3274 3201 threadid = ConvL2I(make_load(control(), p, TypeLong::LONG, T_LONG));
rbackman@3274 3202 } else if (thread_id_size == (size_t) BytesPerInt) {
rbackman@3274 3203 threadid = make_load(control(), p, TypeInt::INT, T_INT);
rbackman@3274 3204 } else {
rbackman@3274 3205 ShouldNotReachHere();
rbackman@3274 3206 }
twisti@3878 3207 set_result(threadid);
rbackman@3274 3208 return true;
rbackman@3274 3209 }
rbackman@3274 3210 #endif
rbackman@3274 3211
duke@0 3212 //------------------------inline_native_time_funcs--------------
duke@0 3213 // inline code for System.currentTimeMillis() and System.nanoTime()
duke@0 3214 // these have the same type and signature
rbackman@3274 3215 bool LibraryCallKit::inline_native_time_funcs(address funcAddr, const char* funcName) {
twisti@3878 3216 const TypeFunc* tf = OptoRuntime::void_long_Type();
duke@0 3217 const TypePtr* no_memory_effects = NULL;
duke@0 3218 Node* time = make_runtime_call(RC_LEAF, tf, funcAddr, funcName, no_memory_effects);
kvn@3680 3219 Node* value = _gvn.transform(new (C) ProjNode(time, TypeFunc::Parms+0));
duke@0 3220 #ifdef ASSERT
twisti@3878 3221 Node* value_top = _gvn.transform(new (C) ProjNode(time, TypeFunc::Parms+1));
duke@0 3222 assert(value_top == top(), "second value must be top");
duke@0 3223 #endif
twisti@3878 3224 set_result(value);
duke@0 3225 return true;
duke@0 3226 }
duke@0 3227
duke@0 3228 //------------------------inline_native_currentThread------------------
duke@0 3229 bool LibraryCallKit::inline_native_currentThread() {
duke@0 3230 Node* junk = NULL;
twisti@3878 3231 set_result(generate_current_thread(junk));
duke@0 3232 return true;
duke@0 3233 }
duke@0 3234
duke@0 3235 //------------------------inline_native_isInterrupted------------------
twisti@3878 3236 // private native boolean java.lang.Thread.isInterrupted(boolean ClearInterrupted);
duke@0 3237 bool LibraryCallKit::inline_native_isInterrupted() {
duke@0 3238 // Add a fast path to t.isInterrupted(clear_int):
duke@0 3239 // (t == Thread.current() && (!TLS._osthread._interrupted || !clear_int))
duke@0 3240 // ? TLS._osthread._interrupted : /*slow path:*/ t.isInterrupted(clear_int)
duke@0 3241 // So, in the common case that the interrupt bit is false,
duke@0 3242 // we avoid making a call into the VM. Even if the interrupt bit
duke@0 3243 // is true, if the clear_int argument is false, we avoid the VM call.
duke@0 3244 // However, if the receiver is not currentThread, we must call the VM,
duke@0 3245 // because there must be some locking done around the operation.
duke@0 3246
duke@0 3247 // We only go to the fast case code if we pass two guards.
duke@0 3248 // Paths which do not pass are accumulated in the slow_region.
vlivanov@3923 3249
vlivanov@3923 3250 enum {
vlivanov@3923 3251 no_int_result_path = 1, // t == Thread.current() && !TLS._osthread._interrupted
vlivanov@3923 3252 no_clear_result_path = 2, // t == Thread.current() && TLS._osthread._interrupted && !clear_int
vlivanov@3923 3253 slow_result_path = 3, // slow path: t.isInterrupted(clear_int)
vlivanov@3923 3254 PATH_LIMIT
vlivanov@3923 3255 };
vlivanov@3923 3256
vlivanov@3923 3257 // Ensure that it's not possible to move the load of TLS._osthread._interrupted flag
vlivanov@3923 3258 // out of the function.
vlivanov@3923 3259 insert_mem_bar(Op_MemBarCPUOrder);
vlivanov@3923 3260
vlivanov@3923 3261 RegionNode* result_rgn = new (C) RegionNode(PATH_LIMIT);
vlivanov@3923 3262 PhiNode* result_val = new (C) PhiNode(result_rgn, TypeInt::BOOL);
vlivanov@3923 3263
kvn@3680 3264 RegionNode* slow_region = new (C) RegionNode(1);
duke@0 3265 record_for_igvn(slow_region);
duke@0 3266
duke@0 3267 // (a) Receiving thread must be the current thread.
duke@0 3268 Node* rec_thr = argument(0);
duke@0 3269 Node* tls_ptr = NULL;
duke@0 3270 Node* cur_thr = generate_current_thread(tls_ptr);
drchase@4918 3271 Node* cmp_thr = _gvn.transform(new (C) CmpPNode(cur_thr, rec_thr));
drchase@4918 3272 Node* bol_thr = _gvn.transform(new (C) BoolNode(cmp_thr, BoolTest::ne));
duke@0 3273
vlivanov@3923 3274 generate_slow_guard(bol_thr, slow_region);
duke@0 3275
duke@0 3276 // (b) Interrupt bit on TLS must be false.
duke@0 3277 Node* p = basic_plus_adr(top()/*!oop*/, tls_ptr, in_bytes(JavaThread::osthread_offset()));
duke@0 3278 Node* osthread = make_load(NULL, p, TypeRawPtr::NOTNULL, T_ADDRESS);
duke@0 3279 p = basic_plus_adr(top()/*!oop*/, osthread, in_bytes(OSThread::interrupted_offset()));
vlivanov@3923 3280
kvn@787 3281 // Set the control input on the field _interrupted read to prevent it floating up.
kvn@787 3282 Node* int_bit = make_load(control(), p, TypeInt::BOOL, T_INT);
drchase@4918 3283 Node* cmp_bit = _gvn.transform(new (C) CmpINode(int_bit, intcon(0)));
drchase@4918 3284 Node* bol_bit = _gvn.transform(new (C) BoolNode(cmp_bit, BoolTest::ne));
duke@0 3285
duke@0 3286 IfNode* iff_bit = create_and_map_if(control(), bol_bit, PROB_UNLIKELY_MAG(3), COUNT_UNKNOWN);
duke@0 3287
duke@0 3288 // First fast path: if (!TLS._interrupted) return false;
drchase@4918 3289 Node* false_bit = _gvn.transform(new (C) IfFalseNode(iff_bit));
duke@0 3290 result_rgn->init_req(no_int_result_path, false_bit);
duke@0 3291 result_val->init_req(no_int_result_path, intcon(0));
duke@0 3292
duke@0 3293 // drop through to next case
drchase@4918 3294 set_control( _gvn.transform(new (C) IfTrueNode(iff_bit)));
duke@0 3295
duke@0 3296 // (c) Or, if interrupt bit is set and clear_int is false, use 2nd fast path.
duke@0 3297 Node* clr_arg = argument(1);
drchase@4918 3298 Node* cmp_arg = _gvn.transform(new (C) CmpINode(clr_arg, intcon(0)));
drchase@4918 3299 Node* bol_arg = _gvn.transform(new (C) BoolNode(cmp_arg, BoolTest::ne));
duke@0 3300 IfNode* iff_arg = create_and_map_if(control(), bol_arg, PROB_FAIR, COUNT_UNKNOWN);
duke@0 3301
duke@0 3302 // Second fast path: ... else if (!clear_int) return true;
drchase@4918 3303 Node* false_arg = _gvn.transform(new (C) IfFalseNode(iff_arg));
duke@0 3304 result_rgn->init_req(no_clear_result_path, false_arg);
duke@0 3305 result_val->init_req(no_clear_result_path, intcon(1));
duke@0 3306
duke@0 3307 // drop through to next case
drchase@4918 3308 set_control( _gvn.transform(new (C) IfTrueNode(iff_arg)));
duke@0 3309
duke@0 3310 // (d) Otherwise, go to the slow path.
duke@0 3311 slow_region->add_req(control());
drchase@4918 3312 set_control( _gvn.transform(slow_region));
duke@0 3313
duke@0 3314 if (stopped()) {
duke@0 3315 // There is no slow path.
duke@0 3316 result_rgn->init_req(slow_result_path, top());
duke@0 3317 result_val->init_req(slow_result_path, top());
duke@0 3318 } else {
duke@0 3319 // non-virtual because it is a private non-static
duke@0 3320 CallJavaNode* slow_call = generate_method_call(vmIntrinsics::_isInterrupted);
duke@0 3321
duke@0 3322 Node* slow_val = set_results_for_java_call(slow_call);
duke@0 3323 // this->control() comes from set_results_for_java_call
duke@0 3324
duke@0 3325 Node* fast_io = slow_call->in(TypeFunc::I_O);
duke@0 3326 Node* fast_mem = slow_call->in(TypeFunc::Memory);
vlivanov@3923 3327
duke@0 3328 // These two phis are pre-filled with cop