library_call.cpp revision 12076:ca56c5db7157
1155324Simp/*
2155324Simp * Copyright (c) 1999, 2016, Oracle and/or its affiliates. All rights reserved.
3213496Scognet * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4155324Simp *
5155324Simp * This code is free software; you can redistribute it and/or modify it
6155324Simp * under the terms of the GNU General Public License version 2 only, as
7155324Simp * published by the Free Software Foundation.
8155324Simp *
9155324Simp * This code is distributed in the hope that it will be useful, but WITHOUT
10155324Simp * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11155324Simp * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12155324Simp * version 2 for more details (a copy is included in the LICENSE file that
13155324Simp * accompanied this code).
14185265Simp *
15185265Simp * You should have received a copy of the GNU General Public License version
16185265Simp * 2 along with this work; if not, write to the Free Software Foundation,
17185265Simp * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18185265Simp *
19185265Simp * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20185265Simp * or visit www.oracle.com if you need additional information or have any
21185265Simp * questions.
22185265Simp *
23185265Simp */
24185265Simp
25155324Simp#include "precompiled.hpp"
26155324Simp#include "asm/macroAssembler.hpp"
27266277Sian#include "classfile/systemDictionary.hpp"
28266277Sian#include "classfile/vmSymbols.hpp"
29155324Simp#include "compiler/compileBroker.hpp"
30155324Simp#include "compiler/compileLog.hpp"
31155324Simp#include "memory/resourceArea.hpp"
32155324Simp#include "oops/objArrayKlass.hpp"
33155324Simp#include "opto/addnode.hpp"
34155324Simp#include "opto/arraycopynode.hpp"
35155324Simp#include "opto/c2compiler.hpp"
36155324Simp#include "opto/callGenerator.hpp"
37155324Simp#include "opto/castnode.hpp"
38155324Simp#include "opto/cfgnode.hpp"
39155324Simp#include "opto/convertnode.hpp"
40155324Simp#include "opto/countbitsnode.hpp"
41155324Simp#include "opto/intrinsicnode.hpp"
42155324Simp#include "opto/idealKit.hpp"
43155324Simp#include "opto/mathexactnode.hpp"
44155324Simp#include "opto/movenode.hpp"
45278613Sian#include "opto/mulnode.hpp"
46155324Simp#include "opto/narrowptrnode.hpp"
47155324Simp#include "opto/opaquenode.hpp"
48259364Sian#include "opto/parse.hpp"
49155324Simp#include "opto/runtime.hpp"
50155324Simp#include "opto/subnode.hpp"
51155324Simp#include "prims/nativeLookup.hpp"
52213496Scognet#include "prims/unsafe.hpp"
53213496Scognet#include "runtime/sharedRuntime.hpp"
54155324Simp#ifdef TRACE_HAVE_INTRINSICS
55213496Scognet#include "trace/traceMacros.hpp"
56187599Simp#endif
57155324Simp
58278727Sianclass LibraryIntrinsic : public InlineCallGenerator {
59155324Simp  // Extend the set of intrinsics known to the runtime:
60155324Simp public:
61155324Simp private:
62155324Simp  bool             _is_virtual;
63155324Simp  bool             _does_virtual_dispatch;
64235718Simp  int8_t           _predicates_count;  // Intrinsic is predicated by several conditions
65266097Sian  int8_t           _last_predicate; // Last generated predicate
66155324Simp  vmIntrinsics::ID _intrinsic_id;
67161704Scognet
68266097Sian public:
69266097Sian  LibraryIntrinsic(ciMethod* m, bool is_virtual, int predicates_count, bool does_virtual_dispatch, vmIntrinsics::ID id)
70161704Scognet    : InlineCallGenerator(m),
71266097Sian      _is_virtual(is_virtual),
72155324Simp      _does_virtual_dispatch(does_virtual_dispatch),
73155324Simp      _predicates_count((int8_t)predicates_count),
74266110Sian      _last_predicate((int8_t)-1),
75236989Simp      _intrinsic_id(id)
76155324Simp  {
77155324Simp  }
78155324Simp  virtual bool is_intrinsic() const { return true; }
79155324Simp  virtual bool is_virtual()   const { return _is_virtual; }
80278727Sian  virtual bool is_predicated() const { return _predicates_count > 0; }
81155324Simp  virtual int  predicates_count() const { return _predicates_count; }
82278727Sian  virtual bool does_virtual_dispatch()   const { return _does_virtual_dispatch; }
83155324Simp  virtual JVMState* generate(JVMState* jvms);
84278727Sian  virtual Node* generate_predicate(JVMState* jvms, int predicate);
85266277Sian  vmIntrinsics::ID intrinsic_id() const { return _intrinsic_id; }
86266277Sian};
87278727Sian
88155324Simp
89155324Simp// Local helper class for LibraryIntrinsic:
90155324Simpclass LibraryCallKit : public GraphKit {
91278727Sian private:
92155324Simp  LibraryIntrinsic* _intrinsic;     // the library intrinsic being called
93155324Simp  Node*             _result;        // the result node, if any
94155324Simp  int               _reexecute_sp;  // the stack pointer when bytecode needs to be reexecuted
95155324Simp
96155324Simp  const TypeOopPtr* sharpen_unsafe_type(Compile::AliasType* alias_type, const TypePtr *adr_type);
97155324Simp
98155324Simp public:
99156828Simp  LibraryCallKit(JVMState* jvms, LibraryIntrinsic* intrinsic)
100278727Sian    : GraphKit(jvms),
101156828Simp      _intrinsic(intrinsic),
102156828Simp      _result(NULL)
103156828Simp  {
104156828Simp    // Check if this is a root compile.  In that case we don't have a caller.
105213496Scognet    if (!jvms->has_method()) {
106213496Scognet      _reexecute_sp = sp();
107213496Scognet    } else {
108213496Scognet      // Find out how many arguments the interpreter needs when deoptimizing
109213496Scognet      // and save the stack pointer value so it can used by uncommon_trap.
110213496Scognet      // We find the argument count by looking at the declared signature.
111213496Scognet      bool ignored_will_link;
112213496Scognet      ciSignature* declared_signature = NULL;
113213496Scognet      ciMethod* ignored_callee = caller()->get_method_at_bci(bci(), ignored_will_link, &declared_signature);
114213496Scognet      const int nargs = declared_signature->arg_size_for_bc(caller()->java_code_at_bci(bci()));
115213496Scognet      _reexecute_sp = sp() + nargs;  // "push" arguments back on stack
116213496Scognet    }
117213496Scognet  }
118155324Simp
119155324Simp  virtual LibraryCallKit* is_LibraryCallKit() const { return (LibraryCallKit*)this; }
120155324Simp
121278727Sian  ciMethod*         caller()    const    { return jvms()->method(); }
122278727Sian  int               bci()       const    { return jvms()->bci(); }
123155324Simp  LibraryIntrinsic* intrinsic() const    { return _intrinsic; }
124155324Simp  vmIntrinsics::ID  intrinsic_id() const { return _intrinsic->intrinsic_id(); }
125278727Sian  ciMethod*         callee()    const    { return _intrinsic->method(); }
126278727Sian
127278727Sian  bool  try_to_inline(int predicate);
128155324Simp  Node* try_to_predicate(int predicate);
129155324Simp
130278727Sian  void push_result() {
131278727Sian    // Push the result onto the stack.
132155324Simp    if (!stopped() && result() != NULL) {
133155324Simp      BasicType bt = result()->bottom_type()->basic_type();
134278727Sian      push_node(bt, result());
135155324Simp    }
136155324Simp  }
137278727Sian
138278727Sian private:
139278727Sian  void fatal_unexpected_iid(vmIntrinsics::ID iid) {
140278727Sian    fatal("unexpected intrinsic %d: %s", iid, vmIntrinsics::name_at(iid));
141155324Simp  }
142155324Simp
143278727Sian  void  set_result(Node* n) { assert(_result == NULL, "only set once"); _result = n; }
144278727Sian  void  set_result(RegionNode* region, PhiNode* value);
145278727Sian  Node*     result() { return _result; }
146278727Sian
147155324Simp  virtual int reexecute_sp() { return _reexecute_sp; }
148155324Simp
149278727Sian  // Helper functions to inline natives
150278727Sian  Node* generate_guard(Node* test, RegionNode* region, float true_prob);
151278727Sian  Node* generate_slow_guard(Node* test, RegionNode* region);
152278727Sian  Node* generate_fair_guard(Node* test, RegionNode* region);
153155324Simp  Node* generate_negative_guard(Node* index, RegionNode* region,
154155324Simp                                // resulting CastII of index:
155278727Sian                                Node* *pos_index = NULL);
156278727Sian  Node* generate_limit_guard(Node* offset, Node* subseq_length,
157278727Sian                             Node* array_length,
158278727Sian                             RegionNode* region);
159155324Simp  void  generate_string_range_check(Node* array, Node* offset,
160155324Simp                                    Node* length, bool char_count);
161278727Sian  Node* generate_current_thread(Node* &tls_output);
162278727Sian  Node* load_mirror_from_klass(Node* klass);
163278727Sian  Node* load_klass_from_mirror_common(Node* mirror, bool never_see_null,
164278727Sian                                      RegionNode* region, int null_path,
165155324Simp                                      int offset);
166155324Simp  Node* load_klass_from_mirror(Node* mirror, bool never_see_null,
167278727Sian                               RegionNode* region, int null_path) {
168278727Sian    int offset = java_lang_Class::klass_offset_in_bytes();
169278727Sian    return load_klass_from_mirror_common(mirror, never_see_null,
170278727Sian                                         region, null_path,
171155324Simp                                         offset);
172155324Simp  }
173278727Sian  Node* load_array_klass_from_mirror(Node* mirror, bool never_see_null,
174278727Sian                                     RegionNode* region, int null_path) {
175278727Sian    int offset = java_lang_Class::array_klass_offset_in_bytes();
176278727Sian    return load_klass_from_mirror_common(mirror, never_see_null,
177155324Simp                                         region, null_path,
178155324Simp                                         offset);
179278727Sian  }
180278727Sian  Node* generate_access_flags_guard(Node* kls,
181278727Sian                                    int modifier_mask, int modifier_bits,
182278727Sian                                    RegionNode* region);
183155324Simp  Node* generate_interface_guard(Node* kls, RegionNode* region);
184155324Simp  Node* generate_array_guard(Node* kls, RegionNode* region) {
185278727Sian    return generate_array_guard_common(kls, region, false, false);
186278727Sian  }
187278727Sian  Node* generate_non_array_guard(Node* kls, RegionNode* region) {
188278727Sian    return generate_array_guard_common(kls, region, false, true);
189184310Sstas  }
190278727Sian  Node* generate_objArray_guard(Node* kls, RegionNode* region) {
191278727Sian    return generate_array_guard_common(kls, region, true, false);
192278727Sian  }
193278727Sian  Node* generate_non_objArray_guard(Node* kls, RegionNode* region) {
194278727Sian    return generate_array_guard_common(kls, region, true, true);
195184310Sstas  }
196184310Sstas  Node* generate_array_guard_common(Node* kls, RegionNode* region,
197278727Sian                                    bool obj_array, bool not_array);
198278727Sian  Node* generate_virtual_guard(Node* obj_klass, RegionNode* slow_region);
199278727Sian  CallJavaNode* generate_method_call(vmIntrinsics::ID method_id,
200278727Sian                                     bool is_virtual = false, bool is_static = false);
201184310Sstas  CallJavaNode* generate_method_call_static(vmIntrinsics::ID method_id) {
202184310Sstas    return generate_method_call(method_id, false, true);
203278727Sian  }
204278727Sian  CallJavaNode* generate_method_call_virtual(vmIntrinsics::ID method_id) {
205278727Sian    return generate_method_call(method_id, true, false);
206278727Sian  }
207184310Sstas  Node * load_field_from_object(Node * fromObj, const char * fieldName, const char * fieldTypeString, bool is_exact, bool is_static, ciInstanceKlass * fromKls);
208278727Sian  Node * field_address_from_object(Node * fromObj, const char * fieldName, const char * fieldTypeString, bool is_exact, bool is_static, ciInstanceKlass * fromKls);
209278727Sian
210278727Sian  Node* make_string_method_node(int opcode, Node* str1_start, Node* cnt1, Node* str2_start, Node* cnt2, StrIntrinsicNode::ArgEnc ae);
211278727Sian  bool inline_string_compareTo(StrIntrinsicNode::ArgEnc ae);
212278727Sian  bool inline_string_indexOf(StrIntrinsicNode::ArgEnc ae);
213184310Sstas  bool inline_string_indexOfI(StrIntrinsicNode::ArgEnc ae);
214184310Sstas  Node* make_indexOf_node(Node* src_start, Node* src_count, Node* tgt_start, Node* tgt_count,
215278727Sian                          RegionNode* region, Node* phi, StrIntrinsicNode::ArgEnc ae);
216278727Sian  bool inline_string_indexOfChar();
217278727Sian  bool inline_string_equals(StrIntrinsicNode::ArgEnc ae);
218278727Sian  bool inline_string_toBytesU();
219184310Sstas  bool inline_string_getCharsU();
220184310Sstas  bool inline_string_copy(bool compress);
221278727Sian  bool inline_string_char_access(bool is_store);
222278727Sian  Node* round_double_node(Node* n);
223278727Sian  bool runtime_math(const TypeFunc* call_type, address funcAddr, const char* funcName);
224278727Sian  bool inline_math_native(vmIntrinsics::ID id);
225155324Simp  bool inline_math(vmIntrinsics::ID id);
226155324Simp  template <typename OverflowOp>
227266277Sian  bool inline_math_overflow(Node* arg1, Node* arg2);
228266277Sian  void inline_math_mathExact(Node* math, Node* test);
229266277Sian  bool inline_math_addExactI(bool is_increment);
230266277Sian  bool inline_math_addExactL(bool is_increment);
231266277Sian  bool inline_math_multiplyExactI();
232266277Sian  bool inline_math_multiplyExactL();
233155324Simp  bool inline_math_negateExactI();
234155324Simp  bool inline_math_negateExactL();
235155324Simp  bool inline_math_subtractExactI(bool is_decrement);
236213496Scognet  bool inline_math_subtractExactL(bool is_decrement);
237266065Sian  bool inline_min_max(vmIntrinsics::ID id);
238265999Sian  bool inline_notify(vmIntrinsics::ID id);
239155324Simp  Node* generate_min_max(vmIntrinsics::ID id, Node* x, Node* y);
240155324Simp  // This returns Type::AnyPtr, RawPtr, or OopPtr.
241155324Simp  int classify_unsafe_addr(Node* &base, Node* &offset);
242155324Simp  Node* make_unsafe_address(Node* base, Node* offset);
243155324Simp  // Helper for inline_unsafe_access.
244155324Simp  // Generates the guards that check whether the result of
245155324Simp  // Unsafe.getObject should be recorded in an SATB log buffer.
246155324Simp  void insert_pre_barrier(Node* base_oop, Node* offset, Node* pre_val, bool need_mem_bar);
247155324Simp
248238390Simp  typedef enum { Relaxed, Opaque, Volatile, Acquire, Release } AccessKind;
249238390Simp  bool inline_unsafe_access(bool is_store, BasicType type, AccessKind kind, bool is_unaligned);
250238390Simp  static bool klass_needs_init_guard(Node* kls);
251238390Simp  bool inline_unsafe_allocate();
252238390Simp  bool inline_unsafe_newArray(bool uninitialized);
253238390Simp  bool inline_unsafe_copyMemory();
254238390Simp  bool inline_native_currentThread();
255238390Simp
256238390Simp  bool inline_native_time_funcs(address method, const char* funcName);
257238390Simp  bool inline_native_isInterrupted();
258238390Simp  bool inline_native_Class_query(vmIntrinsics::ID id);
259238390Simp  bool inline_native_subtype_check();
260155324Simp  bool inline_native_getLength();
261155324Simp  bool inline_array_copyOf(bool is_copyOfRange);
262155324Simp  bool inline_array_equals(StrIntrinsicNode::ArgEnc ae);
263155324Simp  bool inline_preconditions_checkIndex();
264155324Simp  void copy_to_clone(Node* obj, Node* alloc_obj, Node* obj_size, bool is_array, bool card_mark);
265265999Sian  bool inline_native_clone(bool is_virtual);
266265999Sian  bool inline_native_Reflection_getCallerClass();
267155324Simp  // Helper function for inlining native object hash method
268155324Simp  bool inline_native_hashcode(bool is_virtual, bool is_static);
269213496Scognet  bool inline_native_getClass();
270238389Simp
271155324Simp  // Helper functions for inlining arraycopy
272213496Scognet  bool inline_arraycopy();
273155324Simp  AllocateArrayNode* tightly_coupled_allocation(Node* ptr,
274164432Simp                                                RegionNode* slow_region);
275155324Simp  JVMState* arraycopy_restore_alloc_state(AllocateArrayNode* alloc, int& saved_reexecute_sp);
276155324Simp  void arraycopy_move_allocation_here(AllocateArrayNode* alloc, Node* dest, JVMState* saved_jvms, int saved_reexecute_sp);
277155324Simp
278213496Scognet  typedef enum { LS_get_add, LS_get_set, LS_cmp_swap, LS_cmp_swap_weak, LS_cmp_exchange } LoadStoreKind;
279213496Scognet  MemNode::MemOrd access_kind_to_memord_LS(AccessKind access_kind, bool is_store);
280213496Scognet  MemNode::MemOrd access_kind_to_memord(AccessKind access_kind);
281213496Scognet  bool inline_unsafe_load_store(BasicType type,  LoadStoreKind kind, AccessKind access_kind);
282155324Simp  bool inline_unsafe_fence(vmIntrinsics::ID id);
283266110Sian  bool inline_onspinwait();
284266110Sian  bool inline_fp_conversions(vmIntrinsics::ID id);
285266110Sian  bool inline_number_methods(vmIntrinsics::ID id);
286266110Sian  bool inline_reference_get();
287266110Sian  bool inline_Class_cast();
288266110Sian  bool inline_aescrypt_Block(vmIntrinsics::ID id);
289266110Sian  bool inline_cipherBlockChaining_AESCrypt(vmIntrinsics::ID id);
290266110Sian  bool inline_counterMode_AESCrypt(vmIntrinsics::ID id);
291266110Sian  Node* inline_cipherBlockChaining_AESCrypt_predicate(bool decrypting);
292155324Simp  Node* inline_counterMode_AESCrypt_predicate();
293238390Simp  Node* get_key_start_from_aescrypt_object(Node* aescrypt_object);
294238390Simp  Node* get_original_key_start_from_aescrypt_object(Node* aescrypt_object);
295238390Simp  bool inline_ghash_processBlocks();
296238390Simp  bool inline_sha_implCompress(vmIntrinsics::ID id);
297238398Simp  bool inline_digestBase_implCompressMB(int predicate);
298156828Simp  bool inline_sha_implCompressMB(Node* digestBaseObj, ciInstanceKlass* instklass_SHA,
299155324Simp                                 bool long_state, address stubAddr, const char *stubName,
300155324Simp                                 Node* src_start, Node* ofs, Node* limit);
301278613Sian  Node* get_state_from_sha_object(Node *sha_object);
302155324Simp  Node* get_state_from_sha5_object(Node *sha_object);
303155324Simp  Node* inline_digestBase_implCompressMB_predicate(int predicate);
304155324Simp  bool inline_encodeISOArray();
305155324Simp  bool inline_updateCRC32();
306155324Simp  bool inline_updateBytesCRC32();
307155324Simp  bool inline_updateByteBufferCRC32();
308155324Simp  Node* get_table_from_crc32c_class(ciInstanceKlass *crc32c_class);
309155324Simp  bool inline_updateBytesCRC32C();
310155324Simp  bool inline_updateDirectByteBufferCRC32C();
311155324Simp  bool inline_updateBytesAdler32();
312155324Simp  bool inline_updateByteBufferAdler32();
313266110Sian  bool inline_multiplyToLen();
314155324Simp  bool inline_hasNegatives();
315155324Simp  bool inline_squareToLen();
316155324Simp  bool inline_mulAdd();
317155324Simp  bool inline_montgomeryMultiply();
318155324Simp  bool inline_montgomerySquare();
319155324Simp  bool inline_vectorizedMismatch();
320155324Simp  bool inline_fma(vmIntrinsics::ID id);
321155324Simp
322155324Simp  bool inline_profileBoolean();
323155324Simp  bool inline_isCompileConstant();
324155324Simp};
325155324Simp
326155324Simp//---------------------------make_vm_intrinsic----------------------------
327155324SimpCallGenerator* Compile::make_vm_intrinsic(ciMethod* m, bool is_virtual) {
328155324Simp  vmIntrinsics::ID id = m->intrinsic_id();
329155324Simp  assert(id != vmIntrinsics::_none, "must be a VM intrinsic");
330155324Simp
331155324Simp  if (!m->is_loaded()) {
332155324Simp    // Do not attempt to inline unloaded methods.
333155324Simp    return NULL;
334155324Simp  }
335155324Simp
336182805Simp  C2Compiler* compiler = (C2Compiler*)CompileBroker::compiler(CompLevel_full_optimization);
337182805Simp  bool is_available = false;
338184309Sstas
339266110Sian  {
340266110Sian    // For calling is_intrinsic_supported and is_intrinsic_disabled_by_flag
341184309Sstas    // the compiler must transition to '_thread_in_vm' state because both
342266110Sian    // methods access VM-internal data.
343184309Sstas    VM_ENTRY_MARK;
344155324Simp    methodHandle mh(THREAD, m->get_Method());
345155324Simp    is_available = compiler->is_intrinsic_supported(mh, is_virtual) &&
346155324Simp                   !C->directive()->is_intrinsic_disabled(mh) &&
347155324Simp                   !vmIntrinsics::is_disabled_by_flags(mh);
348155324Simp
349155324Simp  }
350157891Simp
351155324Simp  if (is_available) {
352155324Simp    assert(id <= vmIntrinsics::LAST_COMPILER_INLINE, "caller responsibility");
353155324Simp    assert(id != vmIntrinsics::_Object_init && id != vmIntrinsics::_invoke, "enum out of order?");
354155324Simp    return new LibraryIntrinsic(m, is_virtual,
355155324Simp                                vmIntrinsics::predicates_needed(id),
356155324Simp                                vmIntrinsics::does_virtual_dispatch(id),
357155324Simp                                (vmIntrinsics::ID) id);
358155324Simp  } else {
359155324Simp    return NULL;
360155324Simp  }
361155324Simp}
362155324Simp
363155324Simp//----------------------register_library_intrinsics-----------------------
364155324Simp// Initialize this file's data structures, for each Compile instance.
365155324Simpvoid Compile::register_library_intrinsics() {
366155324Simp  // Nothing to do here.
367155324Simp}
368155324Simp
369155324SimpJVMState* LibraryIntrinsic::generate(JVMState* jvms) {
370155324Simp  LibraryCallKit kit(jvms, this);
371155324Simp  Compile* C = kit.C;
372155324Simp  int nodes = C->unique();
373155324Simp#ifndef PRODUCT
374155324Simp  if ((C->print_intrinsics() || C->print_inlining()) && Verbose) {
375155324Simp    char buf[1000];
376155324Simp    const char* str = vmIntrinsics::short_name_as_C_string(intrinsic_id(), buf, sizeof(buf));
377155324Simp    tty->print_cr("Intrinsic %s", str);
378155324Simp  }
379155324Simp#endif
380155324Simp  ciMethod* callee = kit.callee();
381155324Simp  const int bci    = kit.bci();
382155324Simp
383155324Simp  // Try to inline the intrinsic.
384236989Simp  if ((CheckIntrinsics ? callee->intrinsic_candidate() : true) &&
385236989Simp      kit.try_to_inline(_last_predicate)) {
386155324Simp    if (C->print_intrinsics() || C->print_inlining()) {
387226832Skevlo      C->print_inlining(callee, jvms->depth() - 1, bci, is_virtual() ? "(intrinsic, virtual)" : "(intrinsic)");
388161704Scognet    }
389238348Simp    C->gather_intrinsic_statistics(intrinsic_id(), is_virtual(), Compile::_intrinsic_worked);
390167261Spiso    if (C->log()) {
391226832Skevlo      C->log()->elem("intrinsic id='%s'%s nodes='%d'",
392226832Skevlo                     vmIntrinsics::name_at(intrinsic_id()),
393226832Skevlo                     (is_virtual() ? " virtual='1'" : ""),
394226832Skevlo                     C->unique() - nodes);
395226832Skevlo    }
396155324Simp    // Push the result from the inlined method onto the stack.
397155324Simp    kit.push_result();
398155324Simp    C->print_inlining_update(this);
399155324Simp    return kit.transfer_exceptions_into_jvms();
400155324Simp  }
401155324Simp
402155324Simp  // The intrinsic bailed out
403155324Simp  if (C->print_intrinsics() || C->print_inlining()) {
404155324Simp    if (jvms->has_method()) {
405236989Simp      // Not a root compile.
406155324Simp      const char* msg;
407155324Simp      if (callee->intrinsic_candidate()) {
408155324Simp        msg = is_virtual() ? "failed to inline (intrinsic, virtual)" : "failed to inline (intrinsic)";
409155324Simp      } else {
410155324Simp        msg = is_virtual() ? "failed to inline (intrinsic, virtual), method not annotated"
411155324Simp                           : "failed to inline (intrinsic), method not annotated";
412155324Simp      }
413155324Simp      C->print_inlining(callee, jvms->depth() - 1, bci, msg);
414155324Simp    } else {
415155324Simp      // Root compile
416155324Simp      tty->print("Did not generate intrinsic %s%s at bci:%d in",
417155324Simp               vmIntrinsics::name_at(intrinsic_id()),
418155324Simp               (is_virtual() ? " (virtual)" : ""), bci);
419155324Simp    }
420155324Simp  }
421236989Simp  C->gather_intrinsic_statistics(intrinsic_id(), is_virtual(), Compile::_intrinsic_failed);
422155324Simp  C->print_inlining_update(this);
423155324Simp  return NULL;
424155324Simp}
425155324Simp
426155324SimpNode* LibraryIntrinsic::generate_predicate(JVMState* jvms, int predicate) {
427155324Simp  LibraryCallKit kit(jvms, this);
428155324Simp  Compile* C = kit.C;
429155324Simp  int nodes = C->unique();
430155324Simp  _last_predicate = predicate;
431155324Simp#ifndef PRODUCT
432155324Simp  assert(is_predicated() && predicate < predicates_count(), "sanity");
433155324Simp  if ((C->print_intrinsics() || C->print_inlining()) && Verbose) {
434155324Simp    char buf[1000];
435155324Simp    const char* str = vmIntrinsics::short_name_as_C_string(intrinsic_id(), buf, sizeof(buf));
436155324Simp    tty->print_cr("Predicate for intrinsic %s", str);
437155324Simp  }
438155324Simp#endif
439155324Simp  ciMethod* callee = kit.callee();
440155324Simp  const int bci    = kit.bci();
441155324Simp
442155324Simp  Node* slow_ctl = kit.try_to_predicate(predicate);
443155324Simp  if (!kit.failing()) {
444155324Simp    if (C->print_intrinsics() || C->print_inlining()) {
445155324Simp      C->print_inlining(callee, jvms->depth() - 1, bci, is_virtual() ? "(intrinsic, virtual, predicate)" : "(intrinsic, predicate)");
446155324Simp    }
447155324Simp    C->gather_intrinsic_statistics(intrinsic_id(), is_virtual(), Compile::_intrinsic_worked);
448155324Simp    if (C->log()) {
449155324Simp      C->log()->elem("predicate_intrinsic id='%s'%s nodes='%d'",
450155324Simp                     vmIntrinsics::name_at(intrinsic_id()),
451155324Simp                     (is_virtual() ? " virtual='1'" : ""),
452178366Scognet                     C->unique() - nodes);
453178366Scognet    }
454178366Scognet    return slow_ctl; // Could be NULL if the check folds.
455213496Scognet  }
456178366Scognet
457178366Scognet  // The intrinsic bailed out
458178366Scognet  if (C->print_intrinsics() || C->print_inlining()) {
459238334Simp    if (jvms->has_method()) {
460238334Simp      // Not a root compile.
461238334Simp      const char* msg = "failed to generate predicate for intrinsic";
462238334Simp      C->print_inlining(kit.callee(), jvms->depth() - 1, bci, msg);
463238334Simp    } else {
464238334Simp      // Root compile
465238334Simp      C->print_inlining_stream()->print("Did not generate predicate for intrinsic %s%s at bci:%d in",
466238334Simp                                        vmIntrinsics::name_at(intrinsic_id()),
467238334Simp                                        (is_virtual() ? " (virtual)" : ""), bci);
468238334Simp    }
469238334Simp  }
470238334Simp  C->gather_intrinsic_statistics(intrinsic_id(), is_virtual(), Compile::_intrinsic_failed);
471238334Simp  return NULL;
472238334Simp}
473238334Simp
474238334Simpbool LibraryCallKit::try_to_inline(int predicate) {
475238334Simp  // Handle symbolic names for otherwise undistinguished boolean switches:
476238334Simp  const bool is_store       = true;
477238334Simp  const bool is_compress    = true;
478238334Simp  const bool is_static      = true;
479238334Simp  const bool is_volatile    = true;
480238334Simp
481238334Simp  if (!jvms()->has_method()) {
482238334Simp    // Root JVMState has a null method.
483238334Simp    assert(map()->memory()->Opcode() == Op_Parm, "");
484238334Simp    // Insert the memory aliasing node
485238334Simp    set_all_memory(reset_memory());
486238334Simp  }
487238334Simp  assert(merged_memory(), "");
488266110Sian
489266110Sian
490266110Sian  switch (intrinsic_id()) {
491266110Sian  case vmIntrinsics::_hashCode:                 return inline_native_hashcode(intrinsic()->is_virtual(), !is_static);
492266110Sian  case vmIntrinsics::_identityHashCode:         return inline_native_hashcode(/*!virtual*/ false,         is_static);
493266110Sian  case vmIntrinsics::_getClass:                 return inline_native_getClass();
494266110Sian
495266110Sian  case vmIntrinsics::_dsin:
496238334Simp  case vmIntrinsics::_dcos:
497238334Simp  case vmIntrinsics::_dtan:
498238334Simp  case vmIntrinsics::_dabs:
499238334Simp  case vmIntrinsics::_datan2:
500155324Simp  case vmIntrinsics::_dsqrt:
501155324Simp  case vmIntrinsics::_dexp:
502155324Simp  case vmIntrinsics::_dlog:
503155324Simp  case vmIntrinsics::_dlog10:
504155324Simp  case vmIntrinsics::_dpow:                     return inline_math_native(intrinsic_id());
505155324Simp
506155324Simp  case vmIntrinsics::_min:
507155324Simp  case vmIntrinsics::_max:                      return inline_min_max(intrinsic_id());
508155324Simp
509155324Simp  case vmIntrinsics::_notify:
510155324Simp  case vmIntrinsics::_notifyAll:
511155324Simp    if (InlineNotify) {
512155324Simp      return inline_notify(intrinsic_id());
513155324Simp    }
514155324Simp    return false;
515155324Simp
516155324Simp  case vmIntrinsics::_addExactI:                return inline_math_addExactI(false /* add */);
517155324Simp  case vmIntrinsics::_addExactL:                return inline_math_addExactL(false /* add */);
518155324Simp  case vmIntrinsics::_decrementExactI:          return inline_math_subtractExactI(true /* decrement */);
519155324Simp  case vmIntrinsics::_decrementExactL:          return inline_math_subtractExactL(true /* decrement */);
520155324Simp  case vmIntrinsics::_incrementExactI:          return inline_math_addExactI(true /* increment */);
521155324Simp  case vmIntrinsics::_incrementExactL:          return inline_math_addExactL(true /* increment */);
522155324Simp  case vmIntrinsics::_multiplyExactI:           return inline_math_multiplyExactI();
523155324Simp  case vmIntrinsics::_multiplyExactL:           return inline_math_multiplyExactL();
524213496Scognet  case vmIntrinsics::_negateExactI:             return inline_math_negateExactI();
525155324Simp  case vmIntrinsics::_negateExactL:             return inline_math_negateExactL();
526155324Simp  case vmIntrinsics::_subtractExactI:           return inline_math_subtractExactI(false /* subtract */);
527155324Simp  case vmIntrinsics::_subtractExactL:           return inline_math_subtractExactL(false /* subtract */);
528266277Sian
529  case vmIntrinsics::_arraycopy:                return inline_arraycopy();
530
531  case vmIntrinsics::_compareToL:               return inline_string_compareTo(StrIntrinsicNode::LL);
532  case vmIntrinsics::_compareToU:               return inline_string_compareTo(StrIntrinsicNode::UU);
533  case vmIntrinsics::_compareToLU:              return inline_string_compareTo(StrIntrinsicNode::LU);
534  case vmIntrinsics::_compareToUL:              return inline_string_compareTo(StrIntrinsicNode::UL);
535
536  case vmIntrinsics::_indexOfL:                 return inline_string_indexOf(StrIntrinsicNode::LL);
537  case vmIntrinsics::_indexOfU:                 return inline_string_indexOf(StrIntrinsicNode::UU);
538  case vmIntrinsics::_indexOfUL:                return inline_string_indexOf(StrIntrinsicNode::UL);
539  case vmIntrinsics::_indexOfIL:                return inline_string_indexOfI(StrIntrinsicNode::LL);
540  case vmIntrinsics::_indexOfIU:                return inline_string_indexOfI(StrIntrinsicNode::UU);
541  case vmIntrinsics::_indexOfIUL:               return inline_string_indexOfI(StrIntrinsicNode::UL);
542  case vmIntrinsics::_indexOfU_char:            return inline_string_indexOfChar();
543
544  case vmIntrinsics::_equalsL:                  return inline_string_equals(StrIntrinsicNode::LL);
545  case vmIntrinsics::_equalsU:                  return inline_string_equals(StrIntrinsicNode::UU);
546
547  case vmIntrinsics::_toBytesStringU:           return inline_string_toBytesU();
548  case vmIntrinsics::_getCharsStringU:          return inline_string_getCharsU();
549  case vmIntrinsics::_getCharStringU:           return inline_string_char_access(!is_store);
550  case vmIntrinsics::_putCharStringU:           return inline_string_char_access( is_store);
551
552  case vmIntrinsics::_compressStringC:
553  case vmIntrinsics::_compressStringB:          return inline_string_copy( is_compress);
554  case vmIntrinsics::_inflateStringC:
555  case vmIntrinsics::_inflateStringB:           return inline_string_copy(!is_compress);
556
557  case vmIntrinsics::_getObject:                return inline_unsafe_access(!is_store, T_OBJECT,   Relaxed, false);
558  case vmIntrinsics::_getBoolean:               return inline_unsafe_access(!is_store, T_BOOLEAN,  Relaxed, false);
559  case vmIntrinsics::_getByte:                  return inline_unsafe_access(!is_store, T_BYTE,     Relaxed, false);
560  case vmIntrinsics::_getShort:                 return inline_unsafe_access(!is_store, T_SHORT,    Relaxed, false);
561  case vmIntrinsics::_getChar:                  return inline_unsafe_access(!is_store, T_CHAR,     Relaxed, false);
562  case vmIntrinsics::_getInt:                   return inline_unsafe_access(!is_store, T_INT,      Relaxed, false);
563  case vmIntrinsics::_getLong:                  return inline_unsafe_access(!is_store, T_LONG,     Relaxed, false);
564  case vmIntrinsics::_getFloat:                 return inline_unsafe_access(!is_store, T_FLOAT,    Relaxed, false);
565  case vmIntrinsics::_getDouble:                return inline_unsafe_access(!is_store, T_DOUBLE,   Relaxed, false);
566
567  case vmIntrinsics::_putObject:                return inline_unsafe_access( is_store, T_OBJECT,   Relaxed, false);
568  case vmIntrinsics::_putBoolean:               return inline_unsafe_access( is_store, T_BOOLEAN,  Relaxed, false);
569  case vmIntrinsics::_putByte:                  return inline_unsafe_access( is_store, T_BYTE,     Relaxed, false);
570  case vmIntrinsics::_putShort:                 return inline_unsafe_access( is_store, T_SHORT,    Relaxed, false);
571  case vmIntrinsics::_putChar:                  return inline_unsafe_access( is_store, T_CHAR,     Relaxed, false);
572  case vmIntrinsics::_putInt:                   return inline_unsafe_access( is_store, T_INT,      Relaxed, false);
573  case vmIntrinsics::_putLong:                  return inline_unsafe_access( is_store, T_LONG,     Relaxed, false);
574  case vmIntrinsics::_putFloat:                 return inline_unsafe_access( is_store, T_FLOAT,    Relaxed, false);
575  case vmIntrinsics::_putDouble:                return inline_unsafe_access( is_store, T_DOUBLE,   Relaxed, false);
576
577  case vmIntrinsics::_getObjectVolatile:        return inline_unsafe_access(!is_store, T_OBJECT,   Volatile, false);
578  case vmIntrinsics::_getBooleanVolatile:       return inline_unsafe_access(!is_store, T_BOOLEAN,  Volatile, false);
579  case vmIntrinsics::_getByteVolatile:          return inline_unsafe_access(!is_store, T_BYTE,     Volatile, false);
580  case vmIntrinsics::_getShortVolatile:         return inline_unsafe_access(!is_store, T_SHORT,    Volatile, false);
581  case vmIntrinsics::_getCharVolatile:          return inline_unsafe_access(!is_store, T_CHAR,     Volatile, false);
582  case vmIntrinsics::_getIntVolatile:           return inline_unsafe_access(!is_store, T_INT,      Volatile, false);
583  case vmIntrinsics::_getLongVolatile:          return inline_unsafe_access(!is_store, T_LONG,     Volatile, false);
584  case vmIntrinsics::_getFloatVolatile:         return inline_unsafe_access(!is_store, T_FLOAT,    Volatile, false);
585  case vmIntrinsics::_getDoubleVolatile:        return inline_unsafe_access(!is_store, T_DOUBLE,   Volatile, false);
586
587  case vmIntrinsics::_putObjectVolatile:        return inline_unsafe_access( is_store, T_OBJECT,   Volatile, false);
588  case vmIntrinsics::_putBooleanVolatile:       return inline_unsafe_access( is_store, T_BOOLEAN,  Volatile, false);
589  case vmIntrinsics::_putByteVolatile:          return inline_unsafe_access( is_store, T_BYTE,     Volatile, false);
590  case vmIntrinsics::_putShortVolatile:         return inline_unsafe_access( is_store, T_SHORT,    Volatile, false);
591  case vmIntrinsics::_putCharVolatile:          return inline_unsafe_access( is_store, T_CHAR,     Volatile, false);
592  case vmIntrinsics::_putIntVolatile:           return inline_unsafe_access( is_store, T_INT,      Volatile, false);
593  case vmIntrinsics::_putLongVolatile:          return inline_unsafe_access( is_store, T_LONG,     Volatile, false);
594  case vmIntrinsics::_putFloatVolatile:         return inline_unsafe_access( is_store, T_FLOAT,    Volatile, false);
595  case vmIntrinsics::_putDoubleVolatile:        return inline_unsafe_access( is_store, T_DOUBLE,   Volatile, false);
596
597  case vmIntrinsics::_getShortUnaligned:        return inline_unsafe_access(!is_store, T_SHORT,    Relaxed, true);
598  case vmIntrinsics::_getCharUnaligned:         return inline_unsafe_access(!is_store, T_CHAR,     Relaxed, true);
599  case vmIntrinsics::_getIntUnaligned:          return inline_unsafe_access(!is_store, T_INT,      Relaxed, true);
600  case vmIntrinsics::_getLongUnaligned:         return inline_unsafe_access(!is_store, T_LONG,     Relaxed, true);
601
602  case vmIntrinsics::_putShortUnaligned:        return inline_unsafe_access( is_store, T_SHORT,    Relaxed, true);
603  case vmIntrinsics::_putCharUnaligned:         return inline_unsafe_access( is_store, T_CHAR,     Relaxed, true);
604  case vmIntrinsics::_putIntUnaligned:          return inline_unsafe_access( is_store, T_INT,      Relaxed, true);
605  case vmIntrinsics::_putLongUnaligned:         return inline_unsafe_access( is_store, T_LONG,     Relaxed, true);
606
607  case vmIntrinsics::_getObjectAcquire:         return inline_unsafe_access(!is_store, T_OBJECT,   Acquire, false);
608  case vmIntrinsics::_getBooleanAcquire:        return inline_unsafe_access(!is_store, T_BOOLEAN,  Acquire, false);
609  case vmIntrinsics::_getByteAcquire:           return inline_unsafe_access(!is_store, T_BYTE,     Acquire, false);
610  case vmIntrinsics::_getShortAcquire:          return inline_unsafe_access(!is_store, T_SHORT,    Acquire, false);
611  case vmIntrinsics::_getCharAcquire:           return inline_unsafe_access(!is_store, T_CHAR,     Acquire, false);
612  case vmIntrinsics::_getIntAcquire:            return inline_unsafe_access(!is_store, T_INT,      Acquire, false);
613  case vmIntrinsics::_getLongAcquire:           return inline_unsafe_access(!is_store, T_LONG,     Acquire, false);
614  case vmIntrinsics::_getFloatAcquire:          return inline_unsafe_access(!is_store, T_FLOAT,    Acquire, false);
615  case vmIntrinsics::_getDoubleAcquire:         return inline_unsafe_access(!is_store, T_DOUBLE,   Acquire, false);
616
617  case vmIntrinsics::_putObjectRelease:         return inline_unsafe_access( is_store, T_OBJECT,   Release, false);
618  case vmIntrinsics::_putBooleanRelease:        return inline_unsafe_access( is_store, T_BOOLEAN,  Release, false);
619  case vmIntrinsics::_putByteRelease:           return inline_unsafe_access( is_store, T_BYTE,     Release, false);
620  case vmIntrinsics::_putShortRelease:          return inline_unsafe_access( is_store, T_SHORT,    Release, false);
621  case vmIntrinsics::_putCharRelease:           return inline_unsafe_access( is_store, T_CHAR,     Release, false);
622  case vmIntrinsics::_putIntRelease:            return inline_unsafe_access( is_store, T_INT,      Release, false);
623  case vmIntrinsics::_putLongRelease:           return inline_unsafe_access( is_store, T_LONG,     Release, false);
624  case vmIntrinsics::_putFloatRelease:          return inline_unsafe_access( is_store, T_FLOAT,    Release, false);
625  case vmIntrinsics::_putDoubleRelease:         return inline_unsafe_access( is_store, T_DOUBLE,   Release, false);
626
627  case vmIntrinsics::_getObjectOpaque:          return inline_unsafe_access(!is_store, T_OBJECT,   Opaque, false);
628  case vmIntrinsics::_getBooleanOpaque:         return inline_unsafe_access(!is_store, T_BOOLEAN,  Opaque, false);
629  case vmIntrinsics::_getByteOpaque:            return inline_unsafe_access(!is_store, T_BYTE,     Opaque, false);
630  case vmIntrinsics::_getShortOpaque:           return inline_unsafe_access(!is_store, T_SHORT,    Opaque, false);
631  case vmIntrinsics::_getCharOpaque:            return inline_unsafe_access(!is_store, T_CHAR,     Opaque, false);
632  case vmIntrinsics::_getIntOpaque:             return inline_unsafe_access(!is_store, T_INT,      Opaque, false);
633  case vmIntrinsics::_getLongOpaque:            return inline_unsafe_access(!is_store, T_LONG,     Opaque, false);
634  case vmIntrinsics::_getFloatOpaque:           return inline_unsafe_access(!is_store, T_FLOAT,    Opaque, false);
635  case vmIntrinsics::_getDoubleOpaque:          return inline_unsafe_access(!is_store, T_DOUBLE,   Opaque, false);
636
637  case vmIntrinsics::_putObjectOpaque:          return inline_unsafe_access( is_store, T_OBJECT,   Opaque, false);
638  case vmIntrinsics::_putBooleanOpaque:         return inline_unsafe_access( is_store, T_BOOLEAN,  Opaque, false);
639  case vmIntrinsics::_putByteOpaque:            return inline_unsafe_access( is_store, T_BYTE,     Opaque, false);
640  case vmIntrinsics::_putShortOpaque:           return inline_unsafe_access( is_store, T_SHORT,    Opaque, false);
641  case vmIntrinsics::_putCharOpaque:            return inline_unsafe_access( is_store, T_CHAR,     Opaque, false);
642  case vmIntrinsics::_putIntOpaque:             return inline_unsafe_access( is_store, T_INT,      Opaque, false);
643  case vmIntrinsics::_putLongOpaque:            return inline_unsafe_access( is_store, T_LONG,     Opaque, false);
644  case vmIntrinsics::_putFloatOpaque:           return inline_unsafe_access( is_store, T_FLOAT,    Opaque, false);
645  case vmIntrinsics::_putDoubleOpaque:          return inline_unsafe_access( is_store, T_DOUBLE,   Opaque, false);
646
647  case vmIntrinsics::_compareAndSwapObject:             return inline_unsafe_load_store(T_OBJECT, LS_cmp_swap,      Volatile);
648  case vmIntrinsics::_compareAndSwapByte:               return inline_unsafe_load_store(T_BYTE,   LS_cmp_swap,      Volatile);
649  case vmIntrinsics::_compareAndSwapShort:              return inline_unsafe_load_store(T_SHORT,  LS_cmp_swap,      Volatile);
650  case vmIntrinsics::_compareAndSwapInt:                return inline_unsafe_load_store(T_INT,    LS_cmp_swap,      Volatile);
651  case vmIntrinsics::_compareAndSwapLong:               return inline_unsafe_load_store(T_LONG,   LS_cmp_swap,      Volatile);
652
653  case vmIntrinsics::_weakCompareAndSwapObject:         return inline_unsafe_load_store(T_OBJECT, LS_cmp_swap_weak, Relaxed);
654  case vmIntrinsics::_weakCompareAndSwapObjectAcquire:  return inline_unsafe_load_store(T_OBJECT, LS_cmp_swap_weak, Acquire);
655  case vmIntrinsics::_weakCompareAndSwapObjectRelease:  return inline_unsafe_load_store(T_OBJECT, LS_cmp_swap_weak, Release);
656  case vmIntrinsics::_weakCompareAndSwapObjectVolatile: return inline_unsafe_load_store(T_OBJECT, LS_cmp_swap_weak, Volatile);
657  case vmIntrinsics::_weakCompareAndSwapByte:           return inline_unsafe_load_store(T_BYTE,   LS_cmp_swap_weak, Relaxed);
658  case vmIntrinsics::_weakCompareAndSwapByteAcquire:    return inline_unsafe_load_store(T_BYTE,   LS_cmp_swap_weak, Acquire);
659  case vmIntrinsics::_weakCompareAndSwapByteRelease:    return inline_unsafe_load_store(T_BYTE,   LS_cmp_swap_weak, Release);
660  case vmIntrinsics::_weakCompareAndSwapByteVolatile:   return inline_unsafe_load_store(T_BYTE,   LS_cmp_swap_weak, Volatile);
661  case vmIntrinsics::_weakCompareAndSwapShort:          return inline_unsafe_load_store(T_SHORT,  LS_cmp_swap_weak, Relaxed);
662  case vmIntrinsics::_weakCompareAndSwapShortAcquire:   return inline_unsafe_load_store(T_SHORT,  LS_cmp_swap_weak, Acquire);
663  case vmIntrinsics::_weakCompareAndSwapShortRelease:   return inline_unsafe_load_store(T_SHORT,  LS_cmp_swap_weak, Release);
664  case vmIntrinsics::_weakCompareAndSwapShortVolatile:  return inline_unsafe_load_store(T_SHORT,  LS_cmp_swap_weak, Volatile);
665  case vmIntrinsics::_weakCompareAndSwapInt:            return inline_unsafe_load_store(T_INT,    LS_cmp_swap_weak, Relaxed);
666  case vmIntrinsics::_weakCompareAndSwapIntAcquire:     return inline_unsafe_load_store(T_INT,    LS_cmp_swap_weak, Acquire);
667  case vmIntrinsics::_weakCompareAndSwapIntRelease:     return inline_unsafe_load_store(T_INT,    LS_cmp_swap_weak, Release);
668  case vmIntrinsics::_weakCompareAndSwapIntVolatile:    return inline_unsafe_load_store(T_INT,    LS_cmp_swap_weak, Volatile);
669  case vmIntrinsics::_weakCompareAndSwapLong:           return inline_unsafe_load_store(T_LONG,   LS_cmp_swap_weak, Relaxed);
670  case vmIntrinsics::_weakCompareAndSwapLongAcquire:    return inline_unsafe_load_store(T_LONG,   LS_cmp_swap_weak, Acquire);
671  case vmIntrinsics::_weakCompareAndSwapLongRelease:    return inline_unsafe_load_store(T_LONG,   LS_cmp_swap_weak, Release);
672  case vmIntrinsics::_weakCompareAndSwapLongVolatile:   return inline_unsafe_load_store(T_LONG,   LS_cmp_swap_weak, Volatile);
673
674  case vmIntrinsics::_compareAndExchangeObjectVolatile: return inline_unsafe_load_store(T_OBJECT, LS_cmp_exchange,  Volatile);
675  case vmIntrinsics::_compareAndExchangeObjectAcquire:  return inline_unsafe_load_store(T_OBJECT, LS_cmp_exchange,  Acquire);
676  case vmIntrinsics::_compareAndExchangeObjectRelease:  return inline_unsafe_load_store(T_OBJECT, LS_cmp_exchange,  Release);
677  case vmIntrinsics::_compareAndExchangeByteVolatile:   return inline_unsafe_load_store(T_BYTE,   LS_cmp_exchange,  Volatile);
678  case vmIntrinsics::_compareAndExchangeByteAcquire:    return inline_unsafe_load_store(T_BYTE,   LS_cmp_exchange,  Acquire);
679  case vmIntrinsics::_compareAndExchangeByteRelease:    return inline_unsafe_load_store(T_BYTE,   LS_cmp_exchange,  Release);
680  case vmIntrinsics::_compareAndExchangeShortVolatile:  return inline_unsafe_load_store(T_SHORT,  LS_cmp_exchange,  Volatile);
681  case vmIntrinsics::_compareAndExchangeShortAcquire:   return inline_unsafe_load_store(T_SHORT,  LS_cmp_exchange,  Acquire);
682  case vmIntrinsics::_compareAndExchangeShortRelease:   return inline_unsafe_load_store(T_SHORT,  LS_cmp_exchange,  Release);
683  case vmIntrinsics::_compareAndExchangeIntVolatile:    return inline_unsafe_load_store(T_INT,    LS_cmp_exchange,  Volatile);
684  case vmIntrinsics::_compareAndExchangeIntAcquire:     return inline_unsafe_load_store(T_INT,    LS_cmp_exchange,  Acquire);
685  case vmIntrinsics::_compareAndExchangeIntRelease:     return inline_unsafe_load_store(T_INT,    LS_cmp_exchange,  Release);
686  case vmIntrinsics::_compareAndExchangeLongVolatile:   return inline_unsafe_load_store(T_LONG,   LS_cmp_exchange,  Volatile);
687  case vmIntrinsics::_compareAndExchangeLongAcquire:    return inline_unsafe_load_store(T_LONG,   LS_cmp_exchange,  Acquire);
688  case vmIntrinsics::_compareAndExchangeLongRelease:    return inline_unsafe_load_store(T_LONG,   LS_cmp_exchange,  Release);
689
690  case vmIntrinsics::_getAndAddByte:                    return inline_unsafe_load_store(T_BYTE,   LS_get_add,       Volatile);
691  case vmIntrinsics::_getAndAddShort:                   return inline_unsafe_load_store(T_SHORT,  LS_get_add,       Volatile);
692  case vmIntrinsics::_getAndAddInt:                     return inline_unsafe_load_store(T_INT,    LS_get_add,       Volatile);
693  case vmIntrinsics::_getAndAddLong:                    return inline_unsafe_load_store(T_LONG,   LS_get_add,       Volatile);
694
695  case vmIntrinsics::_getAndSetByte:                    return inline_unsafe_load_store(T_BYTE,   LS_get_set,       Volatile);
696  case vmIntrinsics::_getAndSetShort:                   return inline_unsafe_load_store(T_SHORT,  LS_get_set,       Volatile);
697  case vmIntrinsics::_getAndSetInt:                     return inline_unsafe_load_store(T_INT,    LS_get_set,       Volatile);
698  case vmIntrinsics::_getAndSetLong:                    return inline_unsafe_load_store(T_LONG,   LS_get_set,       Volatile);
699  case vmIntrinsics::_getAndSetObject:                  return inline_unsafe_load_store(T_OBJECT, LS_get_set,       Volatile);
700
701  case vmIntrinsics::_loadFence:
702  case vmIntrinsics::_storeFence:
703  case vmIntrinsics::_fullFence:                return inline_unsafe_fence(intrinsic_id());
704
705  case vmIntrinsics::_onSpinWait:               return inline_onspinwait();
706
707  case vmIntrinsics::_currentThread:            return inline_native_currentThread();
708  case vmIntrinsics::_isInterrupted:            return inline_native_isInterrupted();
709
710#ifdef TRACE_HAVE_INTRINSICS
711  case vmIntrinsics::_counterTime:              return inline_native_time_funcs(CAST_FROM_FN_PTR(address, TRACE_TIME_METHOD), "counterTime");
712#endif
713  case vmIntrinsics::_currentTimeMillis:        return inline_native_time_funcs(CAST_FROM_FN_PTR(address, os::javaTimeMillis), "currentTimeMillis");
714  case vmIntrinsics::_nanoTime:                 return inline_native_time_funcs(CAST_FROM_FN_PTR(address, os::javaTimeNanos), "nanoTime");
715  case vmIntrinsics::_allocateInstance:         return inline_unsafe_allocate();
716  case vmIntrinsics::_copyMemory:               return inline_unsafe_copyMemory();
717  case vmIntrinsics::_getLength:                return inline_native_getLength();
718  case vmIntrinsics::_copyOf:                   return inline_array_copyOf(false);
719  case vmIntrinsics::_copyOfRange:              return inline_array_copyOf(true);
720  case vmIntrinsics::_equalsB:                  return inline_array_equals(StrIntrinsicNode::LL);
721  case vmIntrinsics::_equalsC:                  return inline_array_equals(StrIntrinsicNode::UU);
722  case vmIntrinsics::_Preconditions_checkIndex: return inline_preconditions_checkIndex();
723  case vmIntrinsics::_clone:                    return inline_native_clone(intrinsic()->is_virtual());
724
725  case vmIntrinsics::_allocateUninitializedArray: return inline_unsafe_newArray(true);
726  case vmIntrinsics::_newArray:                   return inline_unsafe_newArray(false);
727
728  case vmIntrinsics::_isAssignableFrom:         return inline_native_subtype_check();
729
730  case vmIntrinsics::_isInstance:
731  case vmIntrinsics::_getModifiers:
732  case vmIntrinsics::_isInterface:
733  case vmIntrinsics::_isArray:
734  case vmIntrinsics::_isPrimitive:
735  case vmIntrinsics::_getSuperclass:
736  case vmIntrinsics::_getClassAccessFlags:      return inline_native_Class_query(intrinsic_id());
737
738  case vmIntrinsics::_floatToRawIntBits:
739  case vmIntrinsics::_floatToIntBits:
740  case vmIntrinsics::_intBitsToFloat:
741  case vmIntrinsics::_doubleToRawLongBits:
742  case vmIntrinsics::_doubleToLongBits:
743  case vmIntrinsics::_longBitsToDouble:         return inline_fp_conversions(intrinsic_id());
744
745  case vmIntrinsics::_numberOfLeadingZeros_i:
746  case vmIntrinsics::_numberOfLeadingZeros_l:
747  case vmIntrinsics::_numberOfTrailingZeros_i:
748  case vmIntrinsics::_numberOfTrailingZeros_l:
749  case vmIntrinsics::_bitCount_i:
750  case vmIntrinsics::_bitCount_l:
751  case vmIntrinsics::_reverseBytes_i:
752  case vmIntrinsics::_reverseBytes_l:
753  case vmIntrinsics::_reverseBytes_s:
754  case vmIntrinsics::_reverseBytes_c:           return inline_number_methods(intrinsic_id());
755
756  case vmIntrinsics::_getCallerClass:           return inline_native_Reflection_getCallerClass();
757
758  case vmIntrinsics::_Reference_get:            return inline_reference_get();
759
760  case vmIntrinsics::_Class_cast:               return inline_Class_cast();
761
762  case vmIntrinsics::_aescrypt_encryptBlock:
763  case vmIntrinsics::_aescrypt_decryptBlock:    return inline_aescrypt_Block(intrinsic_id());
764
765  case vmIntrinsics::_cipherBlockChaining_encryptAESCrypt:
766  case vmIntrinsics::_cipherBlockChaining_decryptAESCrypt:
767    return inline_cipherBlockChaining_AESCrypt(intrinsic_id());
768
769  case vmIntrinsics::_counterMode_AESCrypt:
770    return inline_counterMode_AESCrypt(intrinsic_id());
771
772  case vmIntrinsics::_sha_implCompress:
773  case vmIntrinsics::_sha2_implCompress:
774  case vmIntrinsics::_sha5_implCompress:
775    return inline_sha_implCompress(intrinsic_id());
776
777  case vmIntrinsics::_digestBase_implCompressMB:
778    return inline_digestBase_implCompressMB(predicate);
779
780  case vmIntrinsics::_multiplyToLen:
781    return inline_multiplyToLen();
782
783  case vmIntrinsics::_squareToLen:
784    return inline_squareToLen();
785
786  case vmIntrinsics::_mulAdd:
787    return inline_mulAdd();
788
789  case vmIntrinsics::_montgomeryMultiply:
790    return inline_montgomeryMultiply();
791  case vmIntrinsics::_montgomerySquare:
792    return inline_montgomerySquare();
793
794  case vmIntrinsics::_vectorizedMismatch:
795    return inline_vectorizedMismatch();
796
797  case vmIntrinsics::_ghash_processBlocks:
798    return inline_ghash_processBlocks();
799
800  case vmIntrinsics::_encodeISOArray:
801  case vmIntrinsics::_encodeByteISOArray:
802    return inline_encodeISOArray();
803
804  case vmIntrinsics::_updateCRC32:
805    return inline_updateCRC32();
806  case vmIntrinsics::_updateBytesCRC32:
807    return inline_updateBytesCRC32();
808  case vmIntrinsics::_updateByteBufferCRC32:
809    return inline_updateByteBufferCRC32();
810
811  case vmIntrinsics::_updateBytesCRC32C:
812    return inline_updateBytesCRC32C();
813  case vmIntrinsics::_updateDirectByteBufferCRC32C:
814    return inline_updateDirectByteBufferCRC32C();
815
816  case vmIntrinsics::_updateBytesAdler32:
817    return inline_updateBytesAdler32();
818  case vmIntrinsics::_updateByteBufferAdler32:
819    return inline_updateByteBufferAdler32();
820
821  case vmIntrinsics::_profileBoolean:
822    return inline_profileBoolean();
823  case vmIntrinsics::_isCompileConstant:
824    return inline_isCompileConstant();
825
826  case vmIntrinsics::_hasNegatives:
827    return inline_hasNegatives();
828
829  case vmIntrinsics::_fmaD:
830  case vmIntrinsics::_fmaF:
831    return inline_fma(intrinsic_id());
832
833  default:
834    // If you get here, it may be that someone has added a new intrinsic
835    // to the list in vmSymbols.hpp without implementing it here.
836#ifndef PRODUCT
837    if ((PrintMiscellaneous && (Verbose || WizardMode)) || PrintOpto) {
838      tty->print_cr("*** Warning: Unimplemented intrinsic %s(%d)",
839                    vmIntrinsics::name_at(intrinsic_id()), intrinsic_id());
840    }
841#endif
842    return false;
843  }
844}
845
846Node* LibraryCallKit::try_to_predicate(int predicate) {
847  if (!jvms()->has_method()) {
848    // Root JVMState has a null method.
849    assert(map()->memory()->Opcode() == Op_Parm, "");
850    // Insert the memory aliasing node
851    set_all_memory(reset_memory());
852  }
853  assert(merged_memory(), "");
854
855  switch (intrinsic_id()) {
856  case vmIntrinsics::_cipherBlockChaining_encryptAESCrypt:
857    return inline_cipherBlockChaining_AESCrypt_predicate(false);
858  case vmIntrinsics::_cipherBlockChaining_decryptAESCrypt:
859    return inline_cipherBlockChaining_AESCrypt_predicate(true);
860  case vmIntrinsics::_counterMode_AESCrypt:
861    return inline_counterMode_AESCrypt_predicate();
862  case vmIntrinsics::_digestBase_implCompressMB:
863    return inline_digestBase_implCompressMB_predicate(predicate);
864
865  default:
866    // If you get here, it may be that someone has added a new intrinsic
867    // to the list in vmSymbols.hpp without implementing it here.
868#ifndef PRODUCT
869    if ((PrintMiscellaneous && (Verbose || WizardMode)) || PrintOpto) {
870      tty->print_cr("*** Warning: Unimplemented predicate for intrinsic %s(%d)",
871                    vmIntrinsics::name_at(intrinsic_id()), intrinsic_id());
872    }
873#endif
874    Node* slow_ctl = control();
875    set_control(top()); // No fast path instrinsic
876    return slow_ctl;
877  }
878}
879
880//------------------------------set_result-------------------------------
881// Helper function for finishing intrinsics.
882void LibraryCallKit::set_result(RegionNode* region, PhiNode* value) {
883  record_for_igvn(region);
884  set_control(_gvn.transform(region));
885  set_result( _gvn.transform(value));
886  assert(value->type()->basic_type() == result()->bottom_type()->basic_type(), "sanity");
887}
888
889//------------------------------generate_guard---------------------------
890// Helper function for generating guarded fast-slow graph structures.
891// The given 'test', if true, guards a slow path.  If the test fails
892// then a fast path can be taken.  (We generally hope it fails.)
893// In all cases, GraphKit::control() is updated to the fast path.
894// The returned value represents the control for the slow path.
895// The return value is never 'top'; it is either a valid control
896// or NULL if it is obvious that the slow path can never be taken.
897// Also, if region and the slow control are not NULL, the slow edge
898// is appended to the region.
899Node* LibraryCallKit::generate_guard(Node* test, RegionNode* region, float true_prob) {
900  if (stopped()) {
901    // Already short circuited.
902    return NULL;
903  }
904
905  // Build an if node and its projections.
906  // If test is true we take the slow path, which we assume is uncommon.
907  if (_gvn.type(test) == TypeInt::ZERO) {
908    // The slow branch is never taken.  No need to build this guard.
909    return NULL;
910  }
911
912  IfNode* iff = create_and_map_if(control(), test, true_prob, COUNT_UNKNOWN);
913
914  Node* if_slow = _gvn.transform(new IfTrueNode(iff));
915  if (if_slow == top()) {
916    // The slow branch is never taken.  No need to build this guard.
917    return NULL;
918  }
919
920  if (region != NULL)
921    region->add_req(if_slow);
922
923  Node* if_fast = _gvn.transform(new IfFalseNode(iff));
924  set_control(if_fast);
925
926  return if_slow;
927}
928
929inline Node* LibraryCallKit::generate_slow_guard(Node* test, RegionNode* region) {
930  return generate_guard(test, region, PROB_UNLIKELY_MAG(3));
931}
932inline Node* LibraryCallKit::generate_fair_guard(Node* test, RegionNode* region) {
933  return generate_guard(test, region, PROB_FAIR);
934}
935
936inline Node* LibraryCallKit::generate_negative_guard(Node* index, RegionNode* region,
937                                                     Node* *pos_index) {
938  if (stopped())
939    return NULL;                // already stopped
940  if (_gvn.type(index)->higher_equal(TypeInt::POS)) // [0,maxint]
941    return NULL;                // index is already adequately typed
942  Node* cmp_lt = _gvn.transform(new CmpINode(index, intcon(0)));
943  Node* bol_lt = _gvn.transform(new BoolNode(cmp_lt, BoolTest::lt));
944  Node* is_neg = generate_guard(bol_lt, region, PROB_MIN);
945  if (is_neg != NULL && pos_index != NULL) {
946    // Emulate effect of Parse::adjust_map_after_if.
947    Node* ccast = new CastIINode(index, TypeInt::POS);
948    ccast->set_req(0, control());
949    (*pos_index) = _gvn.transform(ccast);
950  }
951  return is_neg;
952}
953
954// Make sure that 'position' is a valid limit index, in [0..length].
955// There are two equivalent plans for checking this:
956//   A. (offset + copyLength)  unsigned<=  arrayLength
957//   B. offset  <=  (arrayLength - copyLength)
958// We require that all of the values above, except for the sum and
959// difference, are already known to be non-negative.
960// Plan A is robust in the face of overflow, if offset and copyLength
961// are both hugely positive.
962//
963// Plan B is less direct and intuitive, but it does not overflow at
964// all, since the difference of two non-negatives is always
965// representable.  Whenever Java methods must perform the equivalent
966// check they generally use Plan B instead of Plan A.
967// For the moment we use Plan A.
968inline Node* LibraryCallKit::generate_limit_guard(Node* offset,
969                                                  Node* subseq_length,
970                                                  Node* array_length,
971                                                  RegionNode* region) {
972  if (stopped())
973    return NULL;                // already stopped
974  bool zero_offset = _gvn.type(offset) == TypeInt::ZERO;
975  if (zero_offset && subseq_length->eqv_uncast(array_length))
976    return NULL;                // common case of whole-array copy
977  Node* last = subseq_length;
978  if (!zero_offset)             // last += offset
979    last = _gvn.transform(new AddINode(last, offset));
980  Node* cmp_lt = _gvn.transform(new CmpUNode(array_length, last));
981  Node* bol_lt = _gvn.transform(new BoolNode(cmp_lt, BoolTest::lt));
982  Node* is_over = generate_guard(bol_lt, region, PROB_MIN);
983  return is_over;
984}
985
986// Emit range checks for the given String.value byte array
987void LibraryCallKit::generate_string_range_check(Node* array, Node* offset, Node* count, bool char_count) {
988  if (stopped()) {
989    return; // already stopped
990  }
991  RegionNode* bailout = new RegionNode(1);
992  record_for_igvn(bailout);
993  if (char_count) {
994    // Convert char count to byte count
995    count = _gvn.transform(new LShiftINode(count, intcon(1)));
996  }
997
998  // Offset and count must not be negative
999  generate_negative_guard(offset, bailout);
1000  generate_negative_guard(count, bailout);
1001  // Offset + count must not exceed length of array
1002  generate_limit_guard(offset, count, load_array_length(array), bailout);
1003
1004  if (bailout->req() > 1) {
1005    PreserveJVMState pjvms(this);
1006    set_control(_gvn.transform(bailout));
1007    uncommon_trap(Deoptimization::Reason_intrinsic,
1008                  Deoptimization::Action_maybe_recompile);
1009  }
1010}
1011
1012//--------------------------generate_current_thread--------------------
1013Node* LibraryCallKit::generate_current_thread(Node* &tls_output) {
1014  ciKlass*    thread_klass = env()->Thread_klass();
1015  const Type* thread_type  = TypeOopPtr::make_from_klass(thread_klass)->cast_to_ptr_type(TypePtr::NotNull);
1016  Node* thread = _gvn.transform(new ThreadLocalNode());
1017  Node* p = basic_plus_adr(top()/*!oop*/, thread, in_bytes(JavaThread::threadObj_offset()));
1018  Node* threadObj = make_load(NULL, p, thread_type, T_OBJECT, MemNode::unordered);
1019  tls_output = thread;
1020  return threadObj;
1021}
1022
1023
1024//------------------------------make_string_method_node------------------------
1025// Helper method for String intrinsic functions. This version is called with
1026// str1 and str2 pointing to byte[] nodes containing Latin1 or UTF16 encoded
1027// characters (depending on 'is_byte'). cnt1 and cnt2 are pointing to Int nodes
1028// containing the lengths of str1 and str2.
1029Node* LibraryCallKit::make_string_method_node(int opcode, Node* str1_start, Node* cnt1, Node* str2_start, Node* cnt2, StrIntrinsicNode::ArgEnc ae) {
1030  Node* result = NULL;
1031  switch (opcode) {
1032  case Op_StrIndexOf:
1033    result = new StrIndexOfNode(control(), memory(TypeAryPtr::BYTES),
1034                                str1_start, cnt1, str2_start, cnt2, ae);
1035    break;
1036  case Op_StrComp:
1037    result = new StrCompNode(control(), memory(TypeAryPtr::BYTES),
1038                             str1_start, cnt1, str2_start, cnt2, ae);
1039    break;
1040  case Op_StrEquals:
1041    // We already know that cnt1 == cnt2 here (checked in 'inline_string_equals').
1042    // Use the constant length if there is one because optimized match rule may exist.
1043    result = new StrEqualsNode(control(), memory(TypeAryPtr::BYTES),
1044                               str1_start, str2_start, cnt2->is_Con() ? cnt2 : cnt1, ae);
1045    break;
1046  default:
1047    ShouldNotReachHere();
1048    return NULL;
1049  }
1050
1051  // All these intrinsics have checks.
1052  C->set_has_split_ifs(true); // Has chance for split-if optimization
1053
1054  return _gvn.transform(result);
1055}
1056
1057//------------------------------inline_string_compareTo------------------------
1058bool LibraryCallKit::inline_string_compareTo(StrIntrinsicNode::ArgEnc ae) {
1059  Node* arg1 = argument(0);
1060  Node* arg2 = argument(1);
1061
1062  // Get start addr and length of first argument
1063  Node* arg1_start  = array_element_address(arg1, intcon(0), T_BYTE);
1064  Node* arg1_cnt    = load_array_length(arg1);
1065
1066  // Get start addr and length of second argument
1067  Node* arg2_start  = array_element_address(arg2, intcon(0), T_BYTE);
1068  Node* arg2_cnt    = load_array_length(arg2);
1069
1070  Node* result = make_string_method_node(Op_StrComp, arg1_start, arg1_cnt, arg2_start, arg2_cnt, ae);
1071  set_result(result);
1072  return true;
1073}
1074
1075//------------------------------inline_string_equals------------------------
1076bool LibraryCallKit::inline_string_equals(StrIntrinsicNode::ArgEnc ae) {
1077  Node* arg1 = argument(0);
1078  Node* arg2 = argument(1);
1079
1080  // paths (plus control) merge
1081  RegionNode* region = new RegionNode(3);
1082  Node* phi = new PhiNode(region, TypeInt::BOOL);
1083
1084  if (!stopped()) {
1085    // Get start addr and length of first argument
1086    Node* arg1_start  = array_element_address(arg1, intcon(0), T_BYTE);
1087    Node* arg1_cnt    = load_array_length(arg1);
1088
1089    // Get start addr and length of second argument
1090    Node* arg2_start  = array_element_address(arg2, intcon(0), T_BYTE);
1091    Node* arg2_cnt    = load_array_length(arg2);
1092
1093    // Check for arg1_cnt != arg2_cnt
1094    Node* cmp = _gvn.transform(new CmpINode(arg1_cnt, arg2_cnt));
1095    Node* bol = _gvn.transform(new BoolNode(cmp, BoolTest::ne));
1096    Node* if_ne = generate_slow_guard(bol, NULL);
1097    if (if_ne != NULL) {
1098      phi->init_req(2, intcon(0));
1099      region->init_req(2, if_ne);
1100    }
1101
1102    // Check for count == 0 is done by assembler code for StrEquals.
1103
1104    if (!stopped()) {
1105      Node* equals = make_string_method_node(Op_StrEquals, arg1_start, arg1_cnt, arg2_start, arg2_cnt, ae);
1106      phi->init_req(1, equals);
1107      region->init_req(1, control());
1108    }
1109  }
1110
1111  // post merge
1112  set_control(_gvn.transform(region));
1113  record_for_igvn(region);
1114
1115  set_result(_gvn.transform(phi));
1116  return true;
1117}
1118
1119//------------------------------inline_array_equals----------------------------
1120bool LibraryCallKit::inline_array_equals(StrIntrinsicNode::ArgEnc ae) {
1121  assert(ae == StrIntrinsicNode::UU || ae == StrIntrinsicNode::LL, "unsupported array types");
1122  Node* arg1 = argument(0);
1123  Node* arg2 = argument(1);
1124
1125  const TypeAryPtr* mtype = (ae == StrIntrinsicNode::UU) ? TypeAryPtr::CHARS : TypeAryPtr::BYTES;
1126  set_result(_gvn.transform(new AryEqNode(control(), memory(mtype), arg1, arg2, ae)));
1127  return true;
1128}
1129
1130//------------------------------inline_hasNegatives------------------------------
1131bool LibraryCallKit::inline_hasNegatives() {
1132  if (too_many_traps(Deoptimization::Reason_intrinsic)) {
1133    return false;
1134  }
1135
1136  assert(callee()->signature()->size() == 3, "hasNegatives has 3 parameters");
1137  // no receiver since it is static method
1138  Node* ba         = argument(0);
1139  Node* offset     = argument(1);
1140  Node* len        = argument(2);
1141
1142  // Range checks
1143  generate_string_range_check(ba, offset, len, false);
1144  if (stopped()) {
1145    return true;
1146  }
1147  Node* ba_start = array_element_address(ba, offset, T_BYTE);
1148  Node* result = new HasNegativesNode(control(), memory(TypeAryPtr::BYTES), ba_start, len);
1149  set_result(_gvn.transform(result));
1150  return true;
1151}
1152
1153bool LibraryCallKit::inline_preconditions_checkIndex() {
1154  Node* index = argument(0);
1155  Node* length = argument(1);
1156  if (too_many_traps(Deoptimization::Reason_intrinsic) || too_many_traps(Deoptimization::Reason_range_check)) {
1157    return false;
1158  }
1159
1160  Node* len_pos_cmp = _gvn.transform(new CmpINode(length, intcon(0)));
1161  Node* len_pos_bol = _gvn.transform(new BoolNode(len_pos_cmp, BoolTest::ge));
1162
1163  {
1164    BuildCutout unless(this, len_pos_bol, PROB_MAX);
1165    uncommon_trap(Deoptimization::Reason_intrinsic,
1166                  Deoptimization::Action_make_not_entrant);
1167  }
1168
1169  if (stopped()) {
1170    return false;
1171  }
1172
1173  Node* rc_cmp = _gvn.transform(new CmpUNode(index, length));
1174  BoolTest::mask btest = BoolTest::lt;
1175  Node* rc_bool = _gvn.transform(new BoolNode(rc_cmp, btest));
1176  RangeCheckNode* rc = new RangeCheckNode(control(), rc_bool, PROB_MAX, COUNT_UNKNOWN);
1177  _gvn.set_type(rc, rc->Value(&_gvn));
1178  if (!rc_bool->is_Con()) {
1179    record_for_igvn(rc);
1180  }
1181  set_control(_gvn.transform(new IfTrueNode(rc)));
1182  {
1183    PreserveJVMState pjvms(this);
1184    set_control(_gvn.transform(new IfFalseNode(rc)));
1185    uncommon_trap(Deoptimization::Reason_range_check,
1186                  Deoptimization::Action_make_not_entrant);
1187  }
1188
1189  if (stopped()) {
1190    return false;
1191  }
1192
1193  Node* result = new CastIINode(index, TypeInt::make(0, _gvn.type(length)->is_int()->_hi, Type::WidenMax));
1194  result->set_req(0, control());
1195  result = _gvn.transform(result);
1196  set_result(result);
1197  replace_in_map(index, result);
1198  return true;
1199}
1200
1201//------------------------------inline_string_indexOf------------------------
1202bool LibraryCallKit::inline_string_indexOf(StrIntrinsicNode::ArgEnc ae) {
1203  if (!Matcher::match_rule_supported(Op_StrIndexOf)) {
1204    return false;
1205  }
1206  Node* src = argument(0);
1207  Node* tgt = argument(1);
1208
1209  // Make the merge point
1210  RegionNode* result_rgn = new RegionNode(4);
1211  Node*       result_phi = new PhiNode(result_rgn, TypeInt::INT);
1212
1213  // Get start addr and length of source string
1214  Node* src_start = array_element_address(src, intcon(0), T_BYTE);
1215  Node* src_count = load_array_length(src);
1216
1217  // Get start addr and length of substring
1218  Node* tgt_start = array_element_address(tgt, intcon(0), T_BYTE);
1219  Node* tgt_count = load_array_length(tgt);
1220
1221  if (ae == StrIntrinsicNode::UU || ae == StrIntrinsicNode::UL) {
1222    // Divide src size by 2 if String is UTF16 encoded
1223    src_count = _gvn.transform(new RShiftINode(src_count, intcon(1)));
1224  }
1225  if (ae == StrIntrinsicNode::UU) {
1226    // Divide substring size by 2 if String is UTF16 encoded
1227    tgt_count = _gvn.transform(new RShiftINode(tgt_count, intcon(1)));
1228  }
1229
1230  Node* result = make_indexOf_node(src_start, src_count, tgt_start, tgt_count, result_rgn, result_phi, ae);
1231  if (result != NULL) {
1232    result_phi->init_req(3, result);
1233    result_rgn->init_req(3, control());
1234  }
1235  set_control(_gvn.transform(result_rgn));
1236  record_for_igvn(result_rgn);
1237  set_result(_gvn.transform(result_phi));
1238
1239  return true;
1240}
1241
1242//-----------------------------inline_string_indexOf-----------------------
1243bool LibraryCallKit::inline_string_indexOfI(StrIntrinsicNode::ArgEnc ae) {
1244  if (too_many_traps(Deoptimization::Reason_intrinsic)) {
1245    return false;
1246  }
1247  if (!Matcher::match_rule_supported(Op_StrIndexOf)) {
1248    return false;
1249  }
1250  assert(callee()->signature()->size() == 5, "String.indexOf() has 5 arguments");
1251  Node* src         = argument(0); // byte[]
1252  Node* src_count   = argument(1); // char count
1253  Node* tgt         = argument(2); // byte[]
1254  Node* tgt_count   = argument(3); // char count
1255  Node* from_index  = argument(4); // char index
1256
1257  // Multiply byte array index by 2 if String is UTF16 encoded
1258  Node* src_offset = (ae == StrIntrinsicNode::LL) ? from_index : _gvn.transform(new LShiftINode(from_index, intcon(1)));
1259  src_count = _gvn.transform(new SubINode(src_count, from_index));
1260  Node* src_start = array_element_address(src, src_offset, T_BYTE);
1261  Node* tgt_start = array_element_address(tgt, intcon(0), T_BYTE);
1262
1263  // Range checks
1264  generate_string_range_check(src, src_offset, src_count, ae != StrIntrinsicNode::LL);
1265  generate_string_range_check(tgt, intcon(0), tgt_count, ae == StrIntrinsicNode::UU);
1266  if (stopped()) {
1267    return true;
1268  }
1269
1270  RegionNode* region = new RegionNode(5);
1271  Node* phi = new PhiNode(region, TypeInt::INT);
1272
1273  Node* result = make_indexOf_node(src_start, src_count, tgt_start, tgt_count, region, phi, ae);
1274  if (result != NULL) {
1275    // The result is index relative to from_index if substring was found, -1 otherwise.
1276    // Generate code which will fold into cmove.
1277    Node* cmp = _gvn.transform(new CmpINode(result, intcon(0)));
1278    Node* bol = _gvn.transform(new BoolNode(cmp, BoolTest::lt));
1279
1280    Node* if_lt = generate_slow_guard(bol, NULL);
1281    if (if_lt != NULL) {
1282      // result == -1
1283      phi->init_req(3, result);
1284      region->init_req(3, if_lt);
1285    }
1286    if (!stopped()) {
1287      result = _gvn.transform(new AddINode(result, from_index));
1288      phi->init_req(4, result);
1289      region->init_req(4, control());
1290    }
1291  }
1292
1293  set_control(_gvn.transform(region));
1294  record_for_igvn(region);
1295  set_result(_gvn.transform(phi));
1296
1297  return true;
1298}
1299
1300// Create StrIndexOfNode with fast path checks
1301Node* LibraryCallKit::make_indexOf_node(Node* src_start, Node* src_count, Node* tgt_start, Node* tgt_count,
1302                                        RegionNode* region, Node* phi, StrIntrinsicNode::ArgEnc ae) {
1303  // Check for substr count > string count
1304  Node* cmp = _gvn.transform(new CmpINode(tgt_count, src_count));
1305  Node* bol = _gvn.transform(new BoolNode(cmp, BoolTest::gt));
1306  Node* if_gt = generate_slow_guard(bol, NULL);
1307  if (if_gt != NULL) {
1308    phi->init_req(1, intcon(-1));
1309    region->init_req(1, if_gt);
1310  }
1311  if (!stopped()) {
1312    // Check for substr count == 0
1313    cmp = _gvn.transform(new CmpINode(tgt_count, intcon(0)));
1314    bol = _gvn.transform(new BoolNode(cmp, BoolTest::eq));
1315    Node* if_zero = generate_slow_guard(bol, NULL);
1316    if (if_zero != NULL) {
1317      phi->init_req(2, intcon(0));
1318      region->init_req(2, if_zero);
1319    }
1320  }
1321  if (!stopped()) {
1322    return make_string_method_node(Op_StrIndexOf, src_start, src_count, tgt_start, tgt_count, ae);
1323  }
1324  return NULL;
1325}
1326
1327//-----------------------------inline_string_indexOfChar-----------------------
1328bool LibraryCallKit::inline_string_indexOfChar() {
1329  if (too_many_traps(Deoptimization::Reason_intrinsic)) {
1330    return false;
1331  }
1332  if (!Matcher::match_rule_supported(Op_StrIndexOfChar)) {
1333    return false;
1334  }
1335  assert(callee()->signature()->size() == 4, "String.indexOfChar() has 4 arguments");
1336  Node* src         = argument(0); // byte[]
1337  Node* tgt         = argument(1); // tgt is int ch
1338  Node* from_index  = argument(2);
1339  Node* max         = argument(3);
1340
1341  Node* src_offset = _gvn.transform(new LShiftINode(from_index, intcon(1)));
1342  Node* src_start = array_element_address(src, src_offset, T_BYTE);
1343  Node* src_count = _gvn.transform(new SubINode(max, from_index));
1344
1345  // Range checks
1346  generate_string_range_check(src, src_offset, src_count, true);
1347  if (stopped()) {
1348    return true;
1349  }
1350
1351  RegionNode* region = new RegionNode(3);
1352  Node* phi = new PhiNode(region, TypeInt::INT);
1353
1354  Node* result = new StrIndexOfCharNode(control(), memory(TypeAryPtr::BYTES), src_start, src_count, tgt, StrIntrinsicNode::none);
1355  C->set_has_split_ifs(true); // Has chance for split-if optimization
1356  _gvn.transform(result);
1357
1358  Node* cmp = _gvn.transform(new CmpINode(result, intcon(0)));
1359  Node* bol = _gvn.transform(new BoolNode(cmp, BoolTest::lt));
1360
1361  Node* if_lt = generate_slow_guard(bol, NULL);
1362  if (if_lt != NULL) {
1363    // result == -1
1364    phi->init_req(2, result);
1365    region->init_req(2, if_lt);
1366  }
1367  if (!stopped()) {
1368    result = _gvn.transform(new AddINode(result, from_index));
1369    phi->init_req(1, result);
1370    region->init_req(1, control());
1371  }
1372  set_control(_gvn.transform(region));
1373  record_for_igvn(region);
1374  set_result(_gvn.transform(phi));
1375
1376  return true;
1377}
1378//---------------------------inline_string_copy---------------------
1379// compressIt == true --> generate a compressed copy operation (compress char[]/byte[] to byte[])
1380//   int StringUTF16.compress(char[] src, int srcOff, byte[] dst, int dstOff, int len)
1381//   int StringUTF16.compress(byte[] src, int srcOff, byte[] dst, int dstOff, int len)
1382// compressIt == false --> generate an inflated copy operation (inflate byte[] to char[]/byte[])
1383//   void StringLatin1.inflate(byte[] src, int srcOff, char[] dst, int dstOff, int len)
1384//   void StringLatin1.inflate(byte[] src, int srcOff, byte[] dst, int dstOff, int len)
1385bool LibraryCallKit::inline_string_copy(bool compress) {
1386  if (too_many_traps(Deoptimization::Reason_intrinsic)) {
1387    return false;
1388  }
1389  int nargs = 5;  // 2 oops, 3 ints
1390  assert(callee()->signature()->size() == nargs, "string copy has 5 arguments");
1391
1392  Node* src         = argument(0);
1393  Node* src_offset  = argument(1);
1394  Node* dst         = argument(2);
1395  Node* dst_offset  = argument(3);
1396  Node* length      = argument(4);
1397
1398  // Check for allocation before we add nodes that would confuse
1399  // tightly_coupled_allocation()
1400  AllocateArrayNode* alloc = tightly_coupled_allocation(dst, NULL);
1401
1402  // Figure out the size and type of the elements we will be copying.
1403  const Type* src_type = src->Value(&_gvn);
1404  const Type* dst_type = dst->Value(&_gvn);
1405  BasicType src_elem = src_type->isa_aryptr()->klass()->as_array_klass()->element_type()->basic_type();
1406  BasicType dst_elem = dst_type->isa_aryptr()->klass()->as_array_klass()->element_type()->basic_type();
1407  assert((compress && dst_elem == T_BYTE && (src_elem == T_BYTE || src_elem == T_CHAR)) ||
1408         (!compress && src_elem == T_BYTE && (dst_elem == T_BYTE || dst_elem == T_CHAR)),
1409         "Unsupported array types for inline_string_copy");
1410
1411  // Convert char[] offsets to byte[] offsets
1412  bool convert_src = (compress && src_elem == T_BYTE);
1413  bool convert_dst = (!compress && dst_elem == T_BYTE);
1414  if (convert_src) {
1415    src_offset = _gvn.transform(new LShiftINode(src_offset, intcon(1)));
1416  } else if (convert_dst) {
1417    dst_offset = _gvn.transform(new LShiftINode(dst_offset, intcon(1)));
1418  }
1419
1420  // Range checks
1421  generate_string_range_check(src, src_offset, length, convert_src);
1422  generate_string_range_check(dst, dst_offset, length, convert_dst);
1423  if (stopped()) {
1424    return true;
1425  }
1426
1427  Node* src_start = array_element_address(src, src_offset, src_elem);
1428  Node* dst_start = array_element_address(dst, dst_offset, dst_elem);
1429  // 'src_start' points to src array + scaled offset
1430  // 'dst_start' points to dst array + scaled offset
1431  Node* count = NULL;
1432  if (compress) {
1433    count = compress_string(src_start, TypeAryPtr::get_array_body_type(src_elem), dst_start, length);
1434  } else {
1435    inflate_string(src_start, dst_start, TypeAryPtr::get_array_body_type(dst_elem), length);
1436  }
1437
1438  if (alloc != NULL) {
1439    if (alloc->maybe_set_complete(&_gvn)) {
1440      // "You break it, you buy it."
1441      InitializeNode* init = alloc->initialization();
1442      assert(init->is_complete(), "we just did this");
1443      init->set_complete_with_arraycopy();
1444      assert(dst->is_CheckCastPP(), "sanity");
1445      assert(dst->in(0)->in(0) == init, "dest pinned");
1446    }
1447    // Do not let stores that initialize this object be reordered with
1448    // a subsequent store that would make this object accessible by
1449    // other threads.
1450    // Record what AllocateNode this StoreStore protects so that
1451    // escape analysis can go from the MemBarStoreStoreNode to the
1452    // AllocateNode and eliminate the MemBarStoreStoreNode if possible
1453    // based on the escape status of the AllocateNode.
1454    insert_mem_bar(Op_MemBarStoreStore, alloc->proj_out(AllocateNode::RawAddress));
1455  }
1456  if (compress) {
1457    set_result(_gvn.transform(count));
1458  }
1459  return true;
1460}
1461
1462#ifdef _LP64
1463#define XTOP ,top() /*additional argument*/
1464#else  //_LP64
1465#define XTOP        /*no additional argument*/
1466#endif //_LP64
1467
1468//------------------------inline_string_toBytesU--------------------------
1469// public static byte[] StringUTF16.toBytes(char[] value, int off, int len)
1470bool LibraryCallKit::inline_string_toBytesU() {
1471  if (too_many_traps(Deoptimization::Reason_intrinsic)) {
1472    return false;
1473  }
1474  // Get the arguments.
1475  Node* value     = argument(0);
1476  Node* offset    = argument(1);
1477  Node* length    = argument(2);
1478
1479  Node* newcopy = NULL;
1480
1481  // Set the original stack and the reexecute bit for the interpreter to reexecute
1482  // the bytecode that invokes StringUTF16.toBytes() if deoptimization happens.
1483  { PreserveReexecuteState preexecs(this);
1484    jvms()->set_should_reexecute(true);
1485
1486    // Check if a null path was taken unconditionally.
1487    value = null_check(value);
1488
1489    RegionNode* bailout = new RegionNode(1);
1490    record_for_igvn(bailout);
1491
1492    // Range checks
1493    generate_negative_guard(offset, bailout);
1494    generate_negative_guard(length, bailout);
1495    generate_limit_guard(offset, length, load_array_length(value), bailout);
1496    // Make sure that resulting byte[] length does not overflow Integer.MAX_VALUE
1497    generate_limit_guard(length, intcon(0), intcon(max_jint/2), bailout);
1498
1499    if (bailout->req() > 1) {
1500      PreserveJVMState pjvms(this);
1501      set_control(_gvn.transform(bailout));
1502      uncommon_trap(Deoptimization::Reason_intrinsic,
1503                    Deoptimization::Action_maybe_recompile);
1504    }
1505    if (stopped()) {
1506      return true;
1507    }
1508
1509    Node* size = _gvn.transform(new LShiftINode(length, intcon(1)));
1510    Node* klass_node = makecon(TypeKlassPtr::make(ciTypeArrayKlass::make(T_BYTE)));
1511    newcopy = new_array(klass_node, size, 0);  // no arguments to push
1512    AllocateArrayNode* alloc = tightly_coupled_allocation(newcopy, NULL);
1513
1514    // Calculate starting addresses.
1515    Node* src_start = array_element_address(value, offset, T_CHAR);
1516    Node* dst_start = basic_plus_adr(newcopy, arrayOopDesc::base_offset_in_bytes(T_BYTE));
1517
1518    // Check if src array address is aligned to HeapWordSize (dst is always aligned)
1519    const TypeInt* toffset = gvn().type(offset)->is_int();
1520    bool aligned = toffset->is_con() && ((toffset->get_con() * type2aelembytes(T_CHAR)) % HeapWordSize == 0);
1521
1522    // Figure out which arraycopy runtime method to call (disjoint, uninitialized).
1523    const char* copyfunc_name = "arraycopy";
1524    address     copyfunc_addr = StubRoutines::select_arraycopy_function(T_CHAR, aligned, true, copyfunc_name, true);
1525    Node* call = make_runtime_call(RC_LEAF|RC_NO_FP,
1526                      OptoRuntime::fast_arraycopy_Type(),
1527                      copyfunc_addr, copyfunc_name, TypeRawPtr::BOTTOM,
1528                      src_start, dst_start, ConvI2X(length) XTOP);
1529    // Do not let reads from the cloned object float above the arraycopy.
1530    if (alloc != NULL) {
1531      if (alloc->maybe_set_complete(&_gvn)) {
1532        // "You break it, you buy it."
1533        InitializeNode* init = alloc->initialization();
1534        assert(init->is_complete(), "we just did this");
1535        init->set_complete_with_arraycopy();
1536        assert(newcopy->is_CheckCastPP(), "sanity");
1537        assert(newcopy->in(0)->in(0) == init, "dest pinned");
1538      }
1539      // Do not let stores that initialize this object be reordered with
1540      // a subsequent store that would make this object accessible by
1541      // other threads.
1542      // Record what AllocateNode this StoreStore protects so that
1543      // escape analysis can go from the MemBarStoreStoreNode to the
1544      // AllocateNode and eliminate the MemBarStoreStoreNode if possible
1545      // based on the escape status of the AllocateNode.
1546      insert_mem_bar(Op_MemBarStoreStore, alloc->proj_out(AllocateNode::RawAddress));
1547    } else {
1548      insert_mem_bar(Op_MemBarCPUOrder);
1549    }
1550  } // original reexecute is set back here
1551
1552  C->set_has_split_ifs(true); // Has chance for split-if optimization
1553  if (!stopped()) {
1554    set_result(newcopy);
1555  }
1556  return true;
1557}
1558
1559//------------------------inline_string_getCharsU--------------------------
1560// public void StringUTF16.getChars(byte[] src, int srcBegin, int srcEnd, char dst[], int dstBegin)
1561bool LibraryCallKit::inline_string_getCharsU() {
1562  if (too_many_traps(Deoptimization::Reason_intrinsic)) {
1563    return false;
1564  }
1565
1566  // Get the arguments.
1567  Node* src       = argument(0);
1568  Node* src_begin = argument(1);
1569  Node* src_end   = argument(2); // exclusive offset (i < src_end)
1570  Node* dst       = argument(3);
1571  Node* dst_begin = argument(4);
1572
1573  // Check for allocation before we add nodes that would confuse
1574  // tightly_coupled_allocation()
1575  AllocateArrayNode* alloc = tightly_coupled_allocation(dst, NULL);
1576
1577  // Check if a null path was taken unconditionally.
1578  src = null_check(src);
1579  dst = null_check(dst);
1580  if (stopped()) {
1581    return true;
1582  }
1583
1584  // Get length and convert char[] offset to byte[] offset
1585  Node* length = _gvn.transform(new SubINode(src_end, src_begin));
1586  src_begin = _gvn.transform(new LShiftINode(src_begin, intcon(1)));
1587
1588  // Range checks
1589  generate_string_range_check(src, src_begin, length, true);
1590  generate_string_range_check(dst, dst_begin, length, false);
1591  if (stopped()) {
1592    return true;
1593  }
1594
1595  if (!stopped()) {
1596    // Calculate starting addresses.
1597    Node* src_start = array_element_address(src, src_begin, T_BYTE);
1598    Node* dst_start = array_element_address(dst, dst_begin, T_CHAR);
1599
1600    // Check if array addresses are aligned to HeapWordSize
1601    const TypeInt* tsrc = gvn().type(src_begin)->is_int();
1602    const TypeInt* tdst = gvn().type(dst_begin)->is_int();
1603    bool aligned = tsrc->is_con() && ((tsrc->get_con() * type2aelembytes(T_BYTE)) % HeapWordSize == 0) &&
1604                   tdst->is_con() && ((tdst->get_con() * type2aelembytes(T_CHAR)) % HeapWordSize == 0);
1605
1606    // Figure out which arraycopy runtime method to call (disjoint, uninitialized).
1607    const char* copyfunc_name = "arraycopy";
1608    address     copyfunc_addr = StubRoutines::select_arraycopy_function(T_CHAR, aligned, true, copyfunc_name, true);
1609    Node* call = make_runtime_call(RC_LEAF|RC_NO_FP,
1610                      OptoRuntime::fast_arraycopy_Type(),
1611                      copyfunc_addr, copyfunc_name, TypeRawPtr::BOTTOM,
1612                      src_start, dst_start, ConvI2X(length) XTOP);
1613    // Do not let reads from the cloned object float above the arraycopy.
1614    if (alloc != NULL) {
1615      if (alloc->maybe_set_complete(&_gvn)) {
1616        // "You break it, you buy it."
1617        InitializeNode* init = alloc->initialization();
1618        assert(init->is_complete(), "we just did this");
1619        init->set_complete_with_arraycopy();
1620        assert(dst->is_CheckCastPP(), "sanity");
1621        assert(dst->in(0)->in(0) == init, "dest pinned");
1622      }
1623      // Do not let stores that initialize this object be reordered with
1624      // a subsequent store that would make this object accessible by
1625      // other threads.
1626      // Record what AllocateNode this StoreStore protects so that
1627      // escape analysis can go from the MemBarStoreStoreNode to the
1628      // AllocateNode and eliminate the MemBarStoreStoreNode if possible
1629      // based on the escape status of the AllocateNode.
1630      insert_mem_bar(Op_MemBarStoreStore, alloc->proj_out(AllocateNode::RawAddress));
1631    } else {
1632      insert_mem_bar(Op_MemBarCPUOrder);
1633    }
1634  }
1635
1636  C->set_has_split_ifs(true); // Has chance for split-if optimization
1637  return true;
1638}
1639
1640//----------------------inline_string_char_access----------------------------
1641// Store/Load char to/from byte[] array.
1642// static void StringUTF16.putChar(byte[] val, int index, int c)
1643// static char StringUTF16.getChar(byte[] val, int index)
1644bool LibraryCallKit::inline_string_char_access(bool is_store) {
1645  Node* value  = argument(0);
1646  Node* index  = argument(1);
1647  Node* ch = is_store ? argument(2) : NULL;
1648
1649  // This intrinsic accesses byte[] array as char[] array. Computing the offsets
1650  // correctly requires matched array shapes.
1651  assert (arrayOopDesc::base_offset_in_bytes(T_CHAR) == arrayOopDesc::base_offset_in_bytes(T_BYTE),
1652          "sanity: byte[] and char[] bases agree");
1653  assert (type2aelembytes(T_CHAR) == type2aelembytes(T_BYTE)*2,
1654          "sanity: byte[] and char[] scales agree");
1655
1656  // Bail when getChar over constants is requested: constant folding would
1657  // reject folding mismatched char access over byte[]. A normal inlining for getChar
1658  // Java method would constant fold nicely instead.
1659  if (!is_store && value->is_Con() && index->is_Con()) {
1660    return false;
1661  }
1662
1663  Node* adr = array_element_address(value, index, T_CHAR);
1664  if (is_store) {
1665    (void) store_to_memory(control(), adr, ch, T_CHAR, TypeAryPtr::BYTES, MemNode::unordered,
1666                           false, false, true /* mismatched */);
1667  } else {
1668    ch = make_load(control(), adr, TypeInt::CHAR, T_CHAR, TypeAryPtr::BYTES, MemNode::unordered,
1669                   LoadNode::DependsOnlyOnTest, false, false, true /* mismatched */);
1670    set_result(ch);
1671  }
1672  return true;
1673}
1674
1675//--------------------------round_double_node--------------------------------
1676// Round a double node if necessary.
1677Node* LibraryCallKit::round_double_node(Node* n) {
1678  if (Matcher::strict_fp_requires_explicit_rounding && UseSSE <= 1)
1679    n = _gvn.transform(new RoundDoubleNode(0, n));
1680  return n;
1681}
1682
1683//------------------------------inline_math-----------------------------------
1684// public static double Math.abs(double)
1685// public static double Math.sqrt(double)
1686// public static double Math.log(double)
1687// public static double Math.log10(double)
1688bool LibraryCallKit::inline_math(vmIntrinsics::ID id) {
1689  Node* arg = round_double_node(argument(0));
1690  Node* n = NULL;
1691  switch (id) {
1692  case vmIntrinsics::_dabs:   n = new AbsDNode(                arg);  break;
1693  case vmIntrinsics::_dsqrt:  n = new SqrtDNode(C, control(),  arg);  break;
1694  default:  fatal_unexpected_iid(id);  break;
1695  }
1696  set_result(_gvn.transform(n));
1697  return true;
1698}
1699
1700//------------------------------runtime_math-----------------------------
1701bool LibraryCallKit::runtime_math(const TypeFunc* call_type, address funcAddr, const char* funcName) {
1702  assert(call_type == OptoRuntime::Math_DD_D_Type() || call_type == OptoRuntime::Math_D_D_Type(),
1703         "must be (DD)D or (D)D type");
1704
1705  // Inputs
1706  Node* a = round_double_node(argument(0));
1707  Node* b = (call_type == OptoRuntime::Math_DD_D_Type()) ? round_double_node(argument(2)) : NULL;
1708
1709  const TypePtr* no_memory_effects = NULL;
1710  Node* trig = make_runtime_call(RC_LEAF, call_type, funcAddr, funcName,
1711                                 no_memory_effects,
1712                                 a, top(), b, b ? top() : NULL);
1713  Node* value = _gvn.transform(new ProjNode(trig, TypeFunc::Parms+0));
1714#ifdef ASSERT
1715  Node* value_top = _gvn.transform(new ProjNode(trig, TypeFunc::Parms+1));
1716  assert(value_top == top(), "second value must be top");
1717#endif
1718
1719  set_result(value);
1720  return true;
1721}
1722
1723//------------------------------inline_math_native-----------------------------
1724bool LibraryCallKit::inline_math_native(vmIntrinsics::ID id) {
1725#define FN_PTR(f) CAST_FROM_FN_PTR(address, f)
1726  switch (id) {
1727    // These intrinsics are not properly supported on all hardware
1728  case vmIntrinsics::_dsin:
1729    return StubRoutines::dsin() != NULL ?
1730      runtime_math(OptoRuntime::Math_D_D_Type(), StubRoutines::dsin(), "dsin") :
1731      runtime_math(OptoRuntime::Math_D_D_Type(), FN_PTR(SharedRuntime::dsin),   "SIN");
1732  case vmIntrinsics::_dcos:
1733    return StubRoutines::dcos() != NULL ?
1734      runtime_math(OptoRuntime::Math_D_D_Type(), StubRoutines::dcos(), "dcos") :
1735      runtime_math(OptoRuntime::Math_D_D_Type(), FN_PTR(SharedRuntime::dcos),   "COS");
1736  case vmIntrinsics::_dtan:
1737    return StubRoutines::dtan() != NULL ?
1738      runtime_math(OptoRuntime::Math_D_D_Type(), StubRoutines::dtan(), "dtan") :
1739      runtime_math(OptoRuntime::Math_D_D_Type(), FN_PTR(SharedRuntime::dtan), "TAN");
1740  case vmIntrinsics::_dlog:
1741    return StubRoutines::dlog() != NULL ?
1742      runtime_math(OptoRuntime::Math_D_D_Type(), StubRoutines::dlog(), "dlog") :
1743      runtime_math(OptoRuntime::Math_D_D_Type(), FN_PTR(SharedRuntime::dlog),   "LOG");
1744  case vmIntrinsics::_dlog10:
1745    return StubRoutines::dlog10() != NULL ?
1746      runtime_math(OptoRuntime::Math_D_D_Type(), StubRoutines::dlog10(), "dlog10") :
1747      runtime_math(OptoRuntime::Math_D_D_Type(), FN_PTR(SharedRuntime::dlog10), "LOG10");
1748
1749    // These intrinsics are supported on all hardware
1750  case vmIntrinsics::_dsqrt:  return Matcher::match_rule_supported(Op_SqrtD) ? inline_math(id) : false;
1751  case vmIntrinsics::_dabs:   return Matcher::has_match_rule(Op_AbsD)   ? inline_math(id) : false;
1752
1753  case vmIntrinsics::_dexp:
1754    return StubRoutines::dexp() != NULL ?
1755      runtime_math(OptoRuntime::Math_D_D_Type(), StubRoutines::dexp(),  "dexp") :
1756      runtime_math(OptoRuntime::Math_D_D_Type(), FN_PTR(SharedRuntime::dexp),  "EXP");
1757  case vmIntrinsics::_dpow:
1758    return StubRoutines::dpow() != NULL ?
1759      runtime_math(OptoRuntime::Math_DD_D_Type(), StubRoutines::dpow(), "dpow") :
1760      runtime_math(OptoRuntime::Math_DD_D_Type(), FN_PTR(SharedRuntime::dpow),  "POW");
1761#undef FN_PTR
1762
1763   // These intrinsics are not yet correctly implemented
1764  case vmIntrinsics::_datan2:
1765    return false;
1766
1767  default:
1768    fatal_unexpected_iid(id);
1769    return false;
1770  }
1771}
1772
1773static bool is_simple_name(Node* n) {
1774  return (n->req() == 1         // constant
1775          || (n->is_Type() && n->as_Type()->type()->singleton())
1776          || n->is_Proj()       // parameter or return value
1777          || n->is_Phi()        // local of some sort
1778          );
1779}
1780
1781//----------------------------inline_notify-----------------------------------*
1782bool LibraryCallKit::inline_notify(vmIntrinsics::ID id) {
1783  const TypeFunc* ftype = OptoRuntime::monitor_notify_Type();
1784  address func;
1785  if (id == vmIntrinsics::_notify) {
1786    func = OptoRuntime::monitor_notify_Java();
1787  } else {
1788    func = OptoRuntime::monitor_notifyAll_Java();
1789  }
1790  Node* call = make_runtime_call(RC_NO_LEAF, ftype, func, NULL, TypeRawPtr::BOTTOM, argument(0));
1791  make_slow_call_ex(call, env()->Throwable_klass(), false);
1792  return true;
1793}
1794
1795
1796//----------------------------inline_min_max-----------------------------------
1797bool LibraryCallKit::inline_min_max(vmIntrinsics::ID id) {
1798  set_result(generate_min_max(id, argument(0), argument(1)));
1799  return true;
1800}
1801
1802void LibraryCallKit::inline_math_mathExact(Node* math, Node *test) {
1803  Node* bol = _gvn.transform( new BoolNode(test, BoolTest::overflow) );
1804  IfNode* check = create_and_map_if(control(), bol, PROB_UNLIKELY_MAG(3), COUNT_UNKNOWN);
1805  Node* fast_path = _gvn.transform( new IfFalseNode(check));
1806  Node* slow_path = _gvn.transform( new IfTrueNode(check) );
1807
1808  {
1809    PreserveJVMState pjvms(this);
1810    PreserveReexecuteState preexecs(this);
1811    jvms()->set_should_reexecute(true);
1812
1813    set_control(slow_path);
1814    set_i_o(i_o());
1815
1816    uncommon_trap(Deoptimization::Reason_intrinsic,
1817                  Deoptimization::Action_none);
1818  }
1819
1820  set_control(fast_path);
1821  set_result(math);
1822}
1823
1824template <typename OverflowOp>
1825bool LibraryCallKit::inline_math_overflow(Node* arg1, Node* arg2) {
1826  typedef typename OverflowOp::MathOp MathOp;
1827
1828  MathOp* mathOp = new MathOp(arg1, arg2);
1829  Node* operation = _gvn.transform( mathOp );
1830  Node* ofcheck = _gvn.transform( new OverflowOp(arg1, arg2) );
1831  inline_math_mathExact(operation, ofcheck);
1832  return true;
1833}
1834
1835bool LibraryCallKit::inline_math_addExactI(bool is_increment) {
1836  return inline_math_overflow<OverflowAddINode>(argument(0), is_increment ? intcon(1) : argument(1));
1837}
1838
1839bool LibraryCallKit::inline_math_addExactL(bool is_increment) {
1840  return inline_math_overflow<OverflowAddLNode>(argument(0), is_increment ? longcon(1) : argument(2));
1841}
1842
1843bool LibraryCallKit::inline_math_subtractExactI(bool is_decrement) {
1844  return inline_math_overflow<OverflowSubINode>(argument(0), is_decrement ? intcon(1) : argument(1));
1845}
1846
1847bool LibraryCallKit::inline_math_subtractExactL(bool is_decrement) {
1848  return inline_math_overflow<OverflowSubLNode>(argument(0), is_decrement ? longcon(1) : argument(2));
1849}
1850
1851bool LibraryCallKit::inline_math_negateExactI() {
1852  return inline_math_overflow<OverflowSubINode>(intcon(0), argument(0));
1853}
1854
1855bool LibraryCallKit::inline_math_negateExactL() {
1856  return inline_math_overflow<OverflowSubLNode>(longcon(0), argument(0));
1857}
1858
1859bool LibraryCallKit::inline_math_multiplyExactI() {
1860  return inline_math_overflow<OverflowMulINode>(argument(0), argument(1));
1861}
1862
1863bool LibraryCallKit::inline_math_multiplyExactL() {
1864  return inline_math_overflow<OverflowMulLNode>(argument(0), argument(2));
1865}
1866
1867Node*
1868LibraryCallKit::generate_min_max(vmIntrinsics::ID id, Node* x0, Node* y0) {
1869  // These are the candidate return value:
1870  Node* xvalue = x0;
1871  Node* yvalue = y0;
1872
1873  if (xvalue == yvalue) {
1874    return xvalue;
1875  }
1876
1877  bool want_max = (id == vmIntrinsics::_max);
1878
1879  const TypeInt* txvalue = _gvn.type(xvalue)->isa_int();
1880  const TypeInt* tyvalue = _gvn.type(yvalue)->isa_int();
1881  if (txvalue == NULL || tyvalue == NULL)  return top();
1882  // This is not really necessary, but it is consistent with a
1883  // hypothetical MaxINode::Value method:
1884  int widen = MAX2(txvalue->_widen, tyvalue->_widen);
1885
1886  // %%% This folding logic should (ideally) be in a different place.
1887  // Some should be inside IfNode, and there to be a more reliable
1888  // transformation of ?: style patterns into cmoves.  We also want
1889  // more powerful optimizations around cmove and min/max.
1890
1891  // Try to find a dominating comparison of these guys.
1892  // It can simplify the index computation for Arrays.copyOf
1893  // and similar uses of System.arraycopy.
1894  // First, compute the normalized version of CmpI(x, y).
1895  int   cmp_op = Op_CmpI;
1896  Node* xkey = xvalue;
1897  Node* ykey = yvalue;
1898  Node* ideal_cmpxy = _gvn.transform(new CmpINode(xkey, ykey));
1899  if (ideal_cmpxy->is_Cmp()) {
1900    // E.g., if we have CmpI(length - offset, count),
1901    // it might idealize to CmpI(length, count + offset)
1902    cmp_op = ideal_cmpxy->Opcode();
1903    xkey = ideal_cmpxy->in(1);
1904    ykey = ideal_cmpxy->in(2);
1905  }
1906
1907  // Start by locating any relevant comparisons.
1908  Node* start_from = (xkey->outcnt() < ykey->outcnt()) ? xkey : ykey;
1909  Node* cmpxy = NULL;
1910  Node* cmpyx = NULL;
1911  for (DUIterator_Fast kmax, k = start_from->fast_outs(kmax); k < kmax; k++) {
1912    Node* cmp = start_from->fast_out(k);
1913    if (cmp->outcnt() > 0 &&            // must have prior uses
1914        cmp->in(0) == NULL &&           // must be context-independent
1915        cmp->Opcode() == cmp_op) {      // right kind of compare
1916      if (cmp->in(1) == xkey && cmp->in(2) == ykey)  cmpxy = cmp;
1917      if (cmp->in(1) == ykey && cmp->in(2) == xkey)  cmpyx = cmp;
1918    }
1919  }
1920
1921  const int NCMPS = 2;
1922  Node* cmps[NCMPS] = { cmpxy, cmpyx };
1923  int cmpn;
1924  for (cmpn = 0; cmpn < NCMPS; cmpn++) {
1925    if (cmps[cmpn] != NULL)  break;     // find a result
1926  }
1927  if (cmpn < NCMPS) {
1928    // Look for a dominating test that tells us the min and max.
1929    int depth = 0;                // Limit search depth for speed
1930    Node* dom = control();
1931    for (; dom != NULL; dom = IfNode::up_one_dom(dom, true)) {
1932      if (++depth >= 100)  break;
1933      Node* ifproj = dom;
1934      if (!ifproj->is_Proj())  continue;
1935      Node* iff = ifproj->in(0);
1936      if (!iff->is_If())  continue;
1937      Node* bol = iff->in(1);
1938      if (!bol->is_Bool())  continue;
1939      Node* cmp = bol->in(1);
1940      if (cmp == NULL)  continue;
1941      for (cmpn = 0; cmpn < NCMPS; cmpn++)
1942        if (cmps[cmpn] == cmp)  break;
1943      if (cmpn == NCMPS)  continue;
1944      BoolTest::mask btest = bol->as_Bool()->_test._test;
1945      if (ifproj->is_IfFalse())  btest = BoolTest(btest).negate();
1946      if (cmp->in(1) == ykey)    btest = BoolTest(btest).commute();
1947      // At this point, we know that 'x btest y' is true.
1948      switch (btest) {
1949      case BoolTest::eq:
1950        // They are proven equal, so we can collapse the min/max.
1951        // Either value is the answer.  Choose the simpler.
1952        if (is_simple_name(yvalue) && !is_simple_name(xvalue))
1953          return yvalue;
1954        return xvalue;
1955      case BoolTest::lt:          // x < y
1956      case BoolTest::le:          // x <= y
1957        return (want_max ? yvalue : xvalue);
1958      case BoolTest::gt:          // x > y
1959      case BoolTest::ge:          // x >= y
1960        return (want_max ? xvalue : yvalue);
1961      }
1962    }
1963  }
1964
1965  // We failed to find a dominating test.
1966  // Let's pick a test that might GVN with prior tests.
1967  Node*          best_bol   = NULL;
1968  BoolTest::mask best_btest = BoolTest::illegal;
1969  for (cmpn = 0; cmpn < NCMPS; cmpn++) {
1970    Node* cmp = cmps[cmpn];
1971    if (cmp == NULL)  continue;
1972    for (DUIterator_Fast jmax, j = cmp->fast_outs(jmax); j < jmax; j++) {
1973      Node* bol = cmp->fast_out(j);
1974      if (!bol->is_Bool())  continue;
1975      BoolTest::mask btest = bol->as_Bool()->_test._test;
1976      if (btest == BoolTest::eq || btest == BoolTest::ne)  continue;
1977      if (cmp->in(1) == ykey)   btest = BoolTest(btest).commute();
1978      if (bol->outcnt() > (best_bol == NULL ? 0 : best_bol->outcnt())) {
1979        best_bol   = bol->as_Bool();
1980        best_btest = btest;
1981      }
1982    }
1983  }
1984
1985  Node* answer_if_true  = NULL;
1986  Node* answer_if_false = NULL;
1987  switch (best_btest) {
1988  default:
1989    if (cmpxy == NULL)
1990      cmpxy = ideal_cmpxy;
1991    best_bol = _gvn.transform(new BoolNode(cmpxy, BoolTest::lt));
1992    // and fall through:
1993  case BoolTest::lt:          // x < y
1994  case BoolTest::le:          // x <= y
1995    answer_if_true  = (want_max ? yvalue : xvalue);
1996    answer_if_false = (want_max ? xvalue : yvalue);
1997    break;
1998  case BoolTest::gt:          // x > y
1999  case BoolTest::ge:          // x >= y
2000    answer_if_true  = (want_max ? xvalue : yvalue);
2001    answer_if_false = (want_max ? yvalue : xvalue);
2002    break;
2003  }
2004
2005  jint hi, lo;
2006  if (want_max) {
2007    // We can sharpen the minimum.
2008    hi = MAX2(txvalue->_hi, tyvalue->_hi);
2009    lo = MAX2(txvalue->_lo, tyvalue->_lo);
2010  } else {
2011    // We can sharpen the maximum.
2012    hi = MIN2(txvalue->_hi, tyvalue->_hi);
2013    lo = MIN2(txvalue->_lo, tyvalue->_lo);
2014  }
2015
2016  // Use a flow-free graph structure, to avoid creating excess control edges
2017  // which could hinder other optimizations.
2018  // Since Math.min/max is often used with arraycopy, we want
2019  // tightly_coupled_allocation to be able to see beyond min/max expressions.
2020  Node* cmov = CMoveNode::make(NULL, best_bol,
2021                               answer_if_false, answer_if_true,
2022                               TypeInt::make(lo, hi, widen));
2023
2024  return _gvn.transform(cmov);
2025
2026  /*
2027  // This is not as desirable as it may seem, since Min and Max
2028  // nodes do not have a full set of optimizations.
2029  // And they would interfere, anyway, with 'if' optimizations
2030  // and with CMoveI canonical forms.
2031  switch (id) {
2032  case vmIntrinsics::_min:
2033    result_val = _gvn.transform(new (C, 3) MinINode(x,y)); break;
2034  case vmIntrinsics::_max:
2035    result_val = _gvn.transform(new (C, 3) MaxINode(x,y)); break;
2036  default:
2037    ShouldNotReachHere();
2038  }
2039  */
2040}
2041
2042inline int
2043LibraryCallKit::classify_unsafe_addr(Node* &base, Node* &offset) {
2044  const TypePtr* base_type = TypePtr::NULL_PTR;
2045  if (base != NULL)  base_type = _gvn.type(base)->isa_ptr();
2046  if (base_type == NULL) {
2047    // Unknown type.
2048    return Type::AnyPtr;
2049  } else if (base_type == TypePtr::NULL_PTR) {
2050    // Since this is a NULL+long form, we have to switch to a rawptr.
2051    base   = _gvn.transform(new CastX2PNode(offset));
2052    offset = MakeConX(0);
2053    return Type::RawPtr;
2054  } else if (base_type->base() == Type::RawPtr) {
2055    return Type::RawPtr;
2056  } else if (base_type->isa_oopptr()) {
2057    // Base is never null => always a heap address.
2058    if (base_type->ptr() == TypePtr::NotNull) {
2059      return Type::OopPtr;
2060    }
2061    // Offset is small => always a heap address.
2062    const TypeX* offset_type = _gvn.type(offset)->isa_intptr_t();
2063    if (offset_type != NULL &&
2064        base_type->offset() == 0 &&     // (should always be?)
2065        offset_type->_lo >= 0 &&
2066        !MacroAssembler::needs_explicit_null_check(offset_type->_hi)) {
2067      return Type::OopPtr;
2068    }
2069    // Otherwise, it might either be oop+off or NULL+addr.
2070    return Type::AnyPtr;
2071  } else {
2072    // No information:
2073    return Type::AnyPtr;
2074  }
2075}
2076
2077inline Node* LibraryCallKit::make_unsafe_address(Node* base, Node* offset) {
2078  int kind = classify_unsafe_addr(base, offset);
2079  if (kind == Type::RawPtr) {
2080    return basic_plus_adr(top(), base, offset);
2081  } else {
2082    return basic_plus_adr(base, offset);
2083  }
2084}
2085
2086//--------------------------inline_number_methods-----------------------------
2087// inline int     Integer.numberOfLeadingZeros(int)
2088// inline int        Long.numberOfLeadingZeros(long)
2089//
2090// inline int     Integer.numberOfTrailingZeros(int)
2091// inline int        Long.numberOfTrailingZeros(long)
2092//
2093// inline int     Integer.bitCount(int)
2094// inline int        Long.bitCount(long)
2095//
2096// inline char  Character.reverseBytes(char)
2097// inline short     Short.reverseBytes(short)
2098// inline int     Integer.reverseBytes(int)
2099// inline long       Long.reverseBytes(long)
2100bool LibraryCallKit::inline_number_methods(vmIntrinsics::ID id) {
2101  Node* arg = argument(0);
2102  Node* n = NULL;
2103  switch (id) {
2104  case vmIntrinsics::_numberOfLeadingZeros_i:   n = new CountLeadingZerosINode( arg);  break;
2105  case vmIntrinsics::_numberOfLeadingZeros_l:   n = new CountLeadingZerosLNode( arg);  break;
2106  case vmIntrinsics::_numberOfTrailingZeros_i:  n = new CountTrailingZerosINode(arg);  break;
2107  case vmIntrinsics::_numberOfTrailingZeros_l:  n = new CountTrailingZerosLNode(arg);  break;
2108  case vmIntrinsics::_bitCount_i:               n = new PopCountINode(          arg);  break;
2109  case vmIntrinsics::_bitCount_l:               n = new PopCountLNode(          arg);  break;
2110  case vmIntrinsics::_reverseBytes_c:           n = new ReverseBytesUSNode(0,   arg);  break;
2111  case vmIntrinsics::_reverseBytes_s:           n = new ReverseBytesSNode( 0,   arg);  break;
2112  case vmIntrinsics::_reverseBytes_i:           n = new ReverseBytesINode( 0,   arg);  break;
2113  case vmIntrinsics::_reverseBytes_l:           n = new ReverseBytesLNode( 0,   arg);  break;
2114  default:  fatal_unexpected_iid(id);  break;
2115  }
2116  set_result(_gvn.transform(n));
2117  return true;
2118}
2119
2120//----------------------------inline_unsafe_access----------------------------
2121
2122// Helper that guards and inserts a pre-barrier.
2123void LibraryCallKit::insert_pre_barrier(Node* base_oop, Node* offset,
2124                                        Node* pre_val, bool need_mem_bar) {
2125  // We could be accessing the referent field of a reference object. If so, when G1
2126  // is enabled, we need to log the value in the referent field in an SATB buffer.
2127  // This routine performs some compile time filters and generates suitable
2128  // runtime filters that guard the pre-barrier code.
2129  // Also add memory barrier for non volatile load from the referent field
2130  // to prevent commoning of loads across safepoint.
2131  if (!UseG1GC && !need_mem_bar)
2132    return;
2133
2134  // Some compile time checks.
2135
2136  // If offset is a constant, is it java_lang_ref_Reference::_reference_offset?
2137  const TypeX* otype = offset->find_intptr_t_type();
2138  if (otype != NULL && otype->is_con() &&
2139      otype->get_con() != java_lang_ref_Reference::referent_offset) {
2140    // Constant offset but not the reference_offset so just return
2141    return;
2142  }
2143
2144  // We only need to generate the runtime guards for instances.
2145  const TypeOopPtr* btype = base_oop->bottom_type()->isa_oopptr();
2146  if (btype != NULL) {
2147    if (btype->isa_aryptr()) {
2148      // Array type so nothing to do
2149      return;
2150    }
2151
2152    const TypeInstPtr* itype = btype->isa_instptr();
2153    if (itype != NULL) {
2154      // Can the klass of base_oop be statically determined to be
2155      // _not_ a sub-class of Reference and _not_ Object?
2156      ciKlass* klass = itype->klass();
2157      if ( klass->is_loaded() &&
2158          !klass->is_subtype_of(env()->Reference_klass()) &&
2159          !env()->Object_klass()->is_subtype_of(klass)) {
2160        return;
2161      }
2162    }
2163  }
2164
2165  // The compile time filters did not reject base_oop/offset so
2166  // we need to generate the following runtime filters
2167  //
2168  // if (offset == java_lang_ref_Reference::_reference_offset) {
2169  //   if (instance_of(base, java.lang.ref.Reference)) {
2170  //     pre_barrier(_, pre_val, ...);
2171  //   }
2172  // }
2173
2174  float likely   = PROB_LIKELY(  0.999);
2175  float unlikely = PROB_UNLIKELY(0.999);
2176
2177  IdealKit ideal(this);
2178#define __ ideal.
2179
2180  Node* referent_off = __ ConX(java_lang_ref_Reference::referent_offset);
2181
2182  __ if_then(offset, BoolTest::eq, referent_off, unlikely); {
2183      // Update graphKit memory and control from IdealKit.
2184      sync_kit(ideal);
2185
2186      Node* ref_klass_con = makecon(TypeKlassPtr::make(env()->Reference_klass()));
2187      Node* is_instof = gen_instanceof(base_oop, ref_klass_con);
2188
2189      // Update IdealKit memory and control from graphKit.
2190      __ sync_kit(this);
2191
2192      Node* one = __ ConI(1);
2193      // is_instof == 0 if base_oop == NULL
2194      __ if_then(is_instof, BoolTest::eq, one, unlikely); {
2195
2196        // Update graphKit from IdeakKit.
2197        sync_kit(ideal);
2198
2199        // Use the pre-barrier to record the value in the referent field
2200        pre_barrier(false /* do_load */,
2201                    __ ctrl(),
2202                    NULL /* obj */, NULL /* adr */, max_juint /* alias_idx */, NULL /* val */, NULL /* val_type */,
2203                    pre_val /* pre_val */,
2204                    T_OBJECT);
2205        if (need_mem_bar) {
2206          // Add memory barrier to prevent commoning reads from this field
2207          // across safepoint since GC can change its value.
2208          insert_mem_bar(Op_MemBarCPUOrder);
2209        }
2210        // Update IdealKit from graphKit.
2211        __ sync_kit(this);
2212
2213      } __ end_if(); // _ref_type != ref_none
2214  } __ end_if(); // offset == referent_offset
2215
2216  // Final sync IdealKit and GraphKit.
2217  final_sync(ideal);
2218#undef __
2219}
2220
2221
2222const TypeOopPtr* LibraryCallKit::sharpen_unsafe_type(Compile::AliasType* alias_type, const TypePtr *adr_type) {
2223  // Attempt to infer a sharper value type from the offset and base type.
2224  ciKlass* sharpened_klass = NULL;
2225
2226  // See if it is an instance field, with an object type.
2227  if (alias_type->field() != NULL) {
2228    if (alias_type->field()->type()->is_klass()) {
2229      sharpened_klass = alias_type->field()->type()->as_klass();
2230    }
2231  }
2232
2233  // See if it is a narrow oop array.
2234  if (adr_type->isa_aryptr()) {
2235    if (adr_type->offset() >= objArrayOopDesc::base_offset_in_bytes()) {
2236      const TypeOopPtr *elem_type = adr_type->is_aryptr()->elem()->isa_oopptr();
2237      if (elem_type != NULL) {
2238        sharpened_klass = elem_type->klass();
2239      }
2240    }
2241  }
2242
2243  // The sharpened class might be unloaded if there is no class loader
2244  // contraint in place.
2245  if (sharpened_klass != NULL && sharpened_klass->is_loaded()) {
2246    const TypeOopPtr* tjp = TypeOopPtr::make_from_klass(sharpened_klass);
2247
2248#ifndef PRODUCT
2249    if (C->print_intrinsics() || C->print_inlining()) {
2250      tty->print("  from base type:  ");  adr_type->dump(); tty->cr();
2251      tty->print("  sharpened value: ");  tjp->dump();      tty->cr();
2252    }
2253#endif
2254    // Sharpen the value type.
2255    return tjp;
2256  }
2257  return NULL;
2258}
2259
2260bool LibraryCallKit::inline_unsafe_access(bool is_store, const BasicType type, const AccessKind kind, const bool unaligned) {
2261  if (callee()->is_static())  return false;  // caller must have the capability!
2262  guarantee(!is_store || kind != Acquire, "Acquire accesses can be produced only for loads");
2263  guarantee( is_store || kind != Release, "Release accesses can be produced only for stores");
2264  assert(type != T_OBJECT || !unaligned, "unaligned access not supported with object type");
2265
2266#ifndef PRODUCT
2267  {
2268    ResourceMark rm;
2269    // Check the signatures.
2270    ciSignature* sig = callee()->signature();
2271#ifdef ASSERT
2272    if (!is_store) {
2273      // Object getObject(Object base, int/long offset), etc.
2274      BasicType rtype = sig->return_type()->basic_type();
2275      assert(rtype == type, "getter must return the expected value");
2276      assert(sig->count() == 2, "oop getter has 2 arguments");
2277      assert(sig->type_at(0)->basic_type() == T_OBJECT, "getter base is object");
2278      assert(sig->type_at(1)->basic_type() == T_LONG, "getter offset is correct");
2279    } else {
2280      // void putObject(Object base, int/long offset, Object x), etc.
2281      assert(sig->return_type()->basic_type() == T_VOID, "putter must not return a value");
2282      assert(sig->count() == 3, "oop putter has 3 arguments");
2283      assert(sig->type_at(0)->basic_type() == T_OBJECT, "putter base is object");
2284      assert(sig->type_at(1)->basic_type() == T_LONG, "putter offset is correct");
2285      BasicType vtype = sig->type_at(sig->count()-1)->basic_type();
2286      assert(vtype == type, "putter must accept the expected value");
2287    }
2288#endif // ASSERT
2289 }
2290#endif //PRODUCT
2291
2292  C->set_has_unsafe_access(true);  // Mark eventual nmethod as "unsafe".
2293
2294  Node* receiver = argument(0);  // type: oop
2295
2296  // Build address expression.
2297  Node* adr;
2298  Node* heap_base_oop = top();
2299  Node* offset = top();
2300  Node* val;
2301
2302  // The base is either a Java object or a value produced by Unsafe.staticFieldBase
2303  Node* base = argument(1);  // type: oop
2304  // The offset is a value produced by Unsafe.staticFieldOffset or Unsafe.objectFieldOffset
2305  offset = argument(2);  // type: long
2306  // We currently rely on the cookies produced by Unsafe.xxxFieldOffset
2307  // to be plain byte offsets, which are also the same as those accepted
2308  // by oopDesc::field_base.
2309  assert(Unsafe_field_offset_to_byte_offset(11) == 11,
2310         "fieldOffset must be byte-scaled");
2311  // 32-bit machines ignore the high half!
2312  offset = ConvL2X(offset);
2313  adr = make_unsafe_address(base, offset);
2314  if (_gvn.type(base)->isa_ptr() != TypePtr::NULL_PTR) {
2315    heap_base_oop = base;
2316  } else if (type == T_OBJECT) {
2317    return false; // off-heap oop accesses are not supported
2318  }
2319
2320  // Can base be NULL? Otherwise, always on-heap access.
2321  bool can_access_non_heap = TypePtr::NULL_PTR->higher_equal(_gvn.type(heap_base_oop));
2322
2323  val = is_store ? argument(4) : NULL;
2324
2325  const TypePtr *adr_type = _gvn.type(adr)->isa_ptr();
2326
2327  // Try to categorize the address.
2328  Compile::AliasType* alias_type = C->alias_type(adr_type);
2329  assert(alias_type->index() != Compile::AliasIdxBot, "no bare pointers here");
2330
2331  if (alias_type->adr_type() == TypeInstPtr::KLASS ||
2332      alias_type->adr_type() == TypeAryPtr::RANGE) {
2333    return false; // not supported
2334  }
2335
2336  bool mismatched = false;
2337  BasicType bt = alias_type->basic_type();
2338  if (bt != T_ILLEGAL) {
2339    assert(alias_type->adr_type()->is_oopptr(), "should be on-heap access");
2340    if (bt == T_BYTE && adr_type->isa_aryptr()) {
2341      // Alias type doesn't differentiate between byte[] and boolean[]).
2342      // Use address type to get the element type.
2343      bt = adr_type->is_aryptr()->elem()->array_element_basic_type();
2344    }
2345    if (bt == T_ARRAY || bt == T_NARROWOOP) {
2346      // accessing an array field with getObject is not a mismatch
2347      bt = T_OBJECT;
2348    }
2349    if ((bt == T_OBJECT) != (type == T_OBJECT)) {
2350      // Don't intrinsify mismatched object accesses
2351      return false;
2352    }
2353    mismatched = (bt != type);
2354  } else if (alias_type->adr_type()->isa_oopptr()) {
2355    mismatched = true; // conservatively mark all "wide" on-heap accesses as mismatched
2356  }
2357
2358  assert(!mismatched || alias_type->adr_type()->is_oopptr(), "off-heap access can't be mismatched");
2359
2360  // First guess at the value type.
2361  const Type *value_type = Type::get_const_basic_type(type);
2362
2363  // We will need memory barriers unless we can determine a unique
2364  // alias category for this reference.  (Note:  If for some reason
2365  // the barriers get omitted and the unsafe reference begins to "pollute"
2366  // the alias analysis of the rest of the graph, either Compile::can_alias
2367  // or Compile::must_alias will throw a diagnostic assert.)
2368  bool need_mem_bar;
2369  switch (kind) {
2370      case Relaxed:
2371          need_mem_bar = mismatched || can_access_non_heap;
2372          break;
2373      case Opaque:
2374          // Opaque uses CPUOrder membars for protection against code movement.
2375      case Acquire:
2376      case Release:
2377      case Volatile:
2378          need_mem_bar = true;
2379          break;
2380      default:
2381          ShouldNotReachHere();
2382  }
2383
2384  // Some accesses require access atomicity for all types, notably longs and doubles.
2385  // When AlwaysAtomicAccesses is enabled, all accesses are atomic.
2386  bool requires_atomic_access = false;
2387  switch (kind) {
2388      case Relaxed:
2389          requires_atomic_access = AlwaysAtomicAccesses;
2390          break;
2391      case Opaque:
2392          // Opaque accesses are atomic.
2393      case Acquire:
2394      case Release:
2395      case Volatile:
2396          requires_atomic_access = true;
2397          break;
2398      default:
2399          ShouldNotReachHere();
2400  }
2401
2402  // Figure out the memory ordering.
2403  // Acquire/Release/Volatile accesses require marking the loads/stores with MemOrd
2404  MemNode::MemOrd mo = access_kind_to_memord_LS(kind, is_store);
2405
2406  // If we are reading the value of the referent field of a Reference
2407  // object (either by using Unsafe directly or through reflection)
2408  // then, if G1 is enabled, we need to record the referent in an
2409  // SATB log buffer using the pre-barrier mechanism.
2410  // Also we need to add memory barrier to prevent commoning reads
2411  // from this field across safepoint since GC can change its value.
2412  bool need_read_barrier = !is_store &&
2413                           offset != top() && heap_base_oop != top();
2414
2415  if (!is_store && type == T_OBJECT) {
2416    const TypeOopPtr* tjp = sharpen_unsafe_type(alias_type, adr_type);
2417    if (tjp != NULL) {
2418      value_type = tjp;
2419    }
2420  }
2421
2422  receiver = null_check(receiver);
2423  if (stopped()) {
2424    return true;
2425  }
2426  // Heap pointers get a null-check from the interpreter,
2427  // as a courtesy.  However, this is not guaranteed by Unsafe,
2428  // and it is not possible to fully distinguish unintended nulls
2429  // from intended ones in this API.
2430
2431  // We need to emit leading and trailing CPU membars (see below) in
2432  // addition to memory membars for special access modes. This is a little
2433  // too strong, but avoids the need to insert per-alias-type
2434  // volatile membars (for stores; compare Parse::do_put_xxx), which
2435  // we cannot do effectively here because we probably only have a
2436  // rough approximation of type.
2437
2438  switch(kind) {
2439    case Relaxed:
2440    case Opaque:
2441    case Acquire:
2442      break;
2443    case Release:
2444    case Volatile:
2445      if (is_store) {
2446        insert_mem_bar(Op_MemBarRelease);
2447      } else {
2448        if (support_IRIW_for_not_multiple_copy_atomic_cpu) {
2449          insert_mem_bar(Op_MemBarVolatile);
2450        }
2451      }
2452      break;
2453    default:
2454      ShouldNotReachHere();
2455  }
2456
2457  // Memory barrier to prevent normal and 'unsafe' accesses from
2458  // bypassing each other.  Happens after null checks, so the
2459  // exception paths do not take memory state from the memory barrier,
2460  // so there's no problems making a strong assert about mixing users
2461  // of safe & unsafe memory.
2462  if (need_mem_bar) insert_mem_bar(Op_MemBarCPUOrder);
2463
2464  if (!is_store) {
2465    Node* p = NULL;
2466    // Try to constant fold a load from a constant field
2467    ciField* field = alias_type->field();
2468    if (heap_base_oop != top() && field != NULL && field->is_constant() && !mismatched) {
2469      // final or stable field
2470      p = make_constant_from_field(field, heap_base_oop);
2471    }
2472    if (p == NULL) {
2473      // To be valid, unsafe loads may depend on other conditions than
2474      // the one that guards them: pin the Load node
2475      p = make_load(control(), adr, value_type, type, adr_type, mo, LoadNode::Pinned, requires_atomic_access, unaligned, mismatched);
2476      // load value
2477      switch (type) {
2478      case T_BOOLEAN:
2479      {
2480        // Normalize the value returned by getBoolean in the following cases
2481        if (mismatched ||
2482            heap_base_oop == top() ||                            // - heap_base_oop is NULL or
2483            (can_access_non_heap && alias_type->field() == NULL) // - heap_base_oop is potentially NULL
2484                                                                 //   and the unsafe access is made to large offset
2485                                                                 //   (i.e., larger than the maximum offset necessary for any
2486                                                                 //   field access)
2487            ) {
2488          IdealKit ideal = IdealKit(this);
2489#define __ ideal.
2490          IdealVariable normalized_result(ideal);
2491          __ declarations_done();
2492          __ set(normalized_result, p);
2493          __ if_then(p, BoolTest::ne, ideal.ConI(0));
2494          __ set(normalized_result, ideal.ConI(1));
2495          ideal.end_if();
2496          final_sync(ideal);
2497          p = __ value(normalized_result);
2498#undef __
2499        }
2500      }
2501      case T_CHAR:
2502      case T_BYTE:
2503      case T_SHORT:
2504      case T_INT:
2505      case T_LONG:
2506      case T_FLOAT:
2507      case T_DOUBLE:
2508        break;
2509      case T_OBJECT:
2510        if (need_read_barrier) {
2511          // We do not require a mem bar inside pre_barrier if need_mem_bar
2512          // is set: the barriers would be emitted by us.
2513          insert_pre_barrier(heap_base_oop, offset, p, !need_mem_bar);
2514        }
2515        break;
2516      case T_ADDRESS:
2517        // Cast to an int type.
2518        p = _gvn.transform(new CastP2XNode(NULL, p));
2519        p = ConvX2UL(p);
2520        break;
2521      default:
2522        fatal("unexpected type %d: %s", type, type2name(type));
2523        break;
2524      }
2525    }
2526    // The load node has the control of the preceding MemBarCPUOrder.  All
2527    // following nodes will have the control of the MemBarCPUOrder inserted at
2528    // the end of this method.  So, pushing the load onto the stack at a later
2529    // point is fine.
2530    set_result(p);
2531  } else {
2532    // place effect of store into memory
2533    switch (type) {
2534    case T_DOUBLE:
2535      val = dstore_rounding(val);
2536      break;
2537    case T_ADDRESS:
2538      // Repackage the long as a pointer.
2539      val = ConvL2X(val);
2540      val = _gvn.transform(new CastX2PNode(val));
2541      break;
2542    }
2543
2544    if (type == T_OBJECT) {
2545      store_oop_to_unknown(control(), heap_base_oop, adr, adr_type, val, type, mo, mismatched);
2546    } else {
2547      store_to_memory(control(), adr, val, type, adr_type, mo, requires_atomic_access, unaligned, mismatched);
2548    }
2549  }
2550
2551  switch(kind) {
2552    case Relaxed:
2553    case Opaque:
2554    case Release:
2555      break;
2556    case Acquire:
2557    case Volatile:
2558      if (!is_store) {
2559        insert_mem_bar(Op_MemBarAcquire);
2560      } else {
2561        if (!support_IRIW_for_not_multiple_copy_atomic_cpu) {
2562          insert_mem_bar(Op_MemBarVolatile);
2563        }
2564      }
2565      break;
2566    default:
2567      ShouldNotReachHere();
2568  }
2569
2570  if (need_mem_bar) insert_mem_bar(Op_MemBarCPUOrder);
2571
2572  return true;
2573}
2574
2575//----------------------------inline_unsafe_load_store----------------------------
2576// This method serves a couple of different customers (depending on LoadStoreKind):
2577//
2578// LS_cmp_swap:
2579//
2580//   boolean compareAndSwapObject(Object o, long offset, Object expected, Object x);
2581//   boolean compareAndSwapInt(   Object o, long offset, int    expected, int    x);
2582//   boolean compareAndSwapLong(  Object o, long offset, long   expected, long   x);
2583//
2584// LS_cmp_swap_weak:
2585//
2586//   boolean weakCompareAndSwapObject(       Object o, long offset, Object expected, Object x);
2587//   boolean weakCompareAndSwapObjectAcquire(Object o, long offset, Object expected, Object x);
2588//   boolean weakCompareAndSwapObjectRelease(Object o, long offset, Object expected, Object x);
2589//
2590//   boolean weakCompareAndSwapInt(          Object o, long offset, int    expected, int    x);
2591//   boolean weakCompareAndSwapIntAcquire(   Object o, long offset, int    expected, int    x);
2592//   boolean weakCompareAndSwapIntRelease(   Object o, long offset, int    expected, int    x);
2593//
2594//   boolean weakCompareAndSwapLong(         Object o, long offset, long   expected, long   x);
2595//   boolean weakCompareAndSwapLongAcquire(  Object o, long offset, long   expected, long   x);
2596//   boolean weakCompareAndSwapLongRelease(  Object o, long offset, long   expected, long   x);
2597//
2598// LS_cmp_exchange:
2599//
2600//   Object compareAndExchangeObjectVolatile(Object o, long offset, Object expected, Object x);
2601//   Object compareAndExchangeObjectAcquire( Object o, long offset, Object expected, Object x);
2602//   Object compareAndExchangeObjectRelease( Object o, long offset, Object expected, Object x);
2603//
2604//   Object compareAndExchangeIntVolatile(   Object o, long offset, Object expected, Object x);
2605//   Object compareAndExchangeIntAcquire(    Object o, long offset, Object expected, Object x);
2606//   Object compareAndExchangeIntRelease(    Object o, long offset, Object expected, Object x);
2607//
2608//   Object compareAndExchangeLongVolatile(  Object o, long offset, Object expected, Object x);
2609//   Object compareAndExchangeLongAcquire(   Object o, long offset, Object expected, Object x);
2610//   Object compareAndExchangeLongRelease(   Object o, long offset, Object expected, Object x);
2611//
2612// LS_get_add:
2613//
2614//   int  getAndAddInt( Object o, long offset, int  delta)
2615//   long getAndAddLong(Object o, long offset, long delta)
2616//
2617// LS_get_set:
2618//
2619//   int    getAndSet(Object o, long offset, int    newValue)
2620//   long   getAndSet(Object o, long offset, long   newValue)
2621//   Object getAndSet(Object o, long offset, Object newValue)
2622//
2623bool LibraryCallKit::inline_unsafe_load_store(const BasicType type, const LoadStoreKind kind, const AccessKind access_kind) {
2624  // This basic scheme here is the same as inline_unsafe_access, but
2625  // differs in enough details that combining them would make the code
2626  // overly confusing.  (This is a true fact! I originally combined
2627  // them, but even I was confused by it!) As much code/comments as
2628  // possible are retained from inline_unsafe_access though to make
2629  // the correspondences clearer. - dl
2630
2631  if (callee()->is_static())  return false;  // caller must have the capability!
2632
2633#ifndef PRODUCT
2634  BasicType rtype;
2635  {
2636    ResourceMark rm;
2637    // Check the signatures.
2638    ciSignature* sig = callee()->signature();
2639    rtype = sig->return_type()->basic_type();
2640    switch(kind) {
2641      case LS_get_add:
2642      case LS_get_set: {
2643      // Check the signatures.
2644#ifdef ASSERT
2645      assert(rtype == type, "get and set must return the expected type");
2646      assert(sig->count() == 3, "get and set has 3 arguments");
2647      assert(sig->type_at(0)->basic_type() == T_OBJECT, "get and set base is object");
2648      assert(sig->type_at(1)->basic_type() == T_LONG, "get and set offset is long");
2649      assert(sig->type_at(2)->basic_type() == type, "get and set must take expected type as new value/delta");
2650      assert(access_kind == Volatile, "mo is not passed to intrinsic nodes in current implementation");
2651#endif // ASSERT
2652        break;
2653      }
2654      case LS_cmp_swap:
2655      case LS_cmp_swap_weak: {
2656      // Check the signatures.
2657#ifdef ASSERT
2658      assert(rtype == T_BOOLEAN, "CAS must return boolean");
2659      assert(sig->count() == 4, "CAS has 4 arguments");
2660      assert(sig->type_at(0)->basic_type() == T_OBJECT, "CAS base is object");
2661      assert(sig->type_at(1)->basic_type() == T_LONG, "CAS offset is long");
2662#endif // ASSERT
2663        break;
2664      }
2665      case LS_cmp_exchange: {
2666      // Check the signatures.
2667#ifdef ASSERT
2668      assert(rtype == type, "CAS must return the expected type");
2669      assert(sig->count() == 4, "CAS has 4 arguments");
2670      assert(sig->type_at(0)->basic_type() == T_OBJECT, "CAS base is object");
2671      assert(sig->type_at(1)->basic_type() == T_LONG, "CAS offset is long");
2672#endif // ASSERT
2673        break;
2674      }
2675      default:
2676        ShouldNotReachHere();
2677    }
2678  }
2679#endif //PRODUCT
2680
2681  C->set_has_unsafe_access(true);  // Mark eventual nmethod as "unsafe".
2682
2683  // Get arguments:
2684  Node* receiver = NULL;
2685  Node* base     = NULL;
2686  Node* offset   = NULL;
2687  Node* oldval   = NULL;
2688  Node* newval   = NULL;
2689  switch(kind) {
2690    case LS_cmp_swap:
2691    case LS_cmp_swap_weak:
2692    case LS_cmp_exchange: {
2693      const bool two_slot_type = type2size[type] == 2;
2694      receiver = argument(0);  // type: oop
2695      base     = argument(1);  // type: oop
2696      offset   = argument(2);  // type: long
2697      oldval   = argument(4);  // type: oop, int, or long
2698      newval   = argument(two_slot_type ? 6 : 5);  // type: oop, int, or long
2699      break;
2700    }
2701    case LS_get_add:
2702    case LS_get_set: {
2703      receiver = argument(0);  // type: oop
2704      base     = argument(1);  // type: oop
2705      offset   = argument(2);  // type: long
2706      oldval   = NULL;
2707      newval   = argument(4);  // type: oop, int, or long
2708      break;
2709    }
2710    default:
2711      ShouldNotReachHere();
2712  }
2713
2714  // Build field offset expression.
2715  // We currently rely on the cookies produced by Unsafe.xxxFieldOffset
2716  // to be plain byte offsets, which are also the same as those accepted
2717  // by oopDesc::field_base.
2718  assert(Unsafe_field_offset_to_byte_offset(11) == 11, "fieldOffset must be byte-scaled");
2719  // 32-bit machines ignore the high half of long offsets
2720  offset = ConvL2X(offset);
2721  Node* adr = make_unsafe_address(base, offset);
2722  const TypePtr *adr_type = _gvn.type(adr)->isa_ptr();
2723
2724  Compile::AliasType* alias_type = C->alias_type(adr_type);
2725  BasicType bt = alias_type->basic_type();
2726  if (bt != T_ILLEGAL &&
2727      ((bt == T_OBJECT || bt == T_ARRAY) != (type == T_OBJECT))) {
2728    // Don't intrinsify mismatched object accesses.
2729    return false;
2730  }
2731
2732  // For CAS, unlike inline_unsafe_access, there seems no point in
2733  // trying to refine types. Just use the coarse types here.
2734  assert(alias_type->index() != Compile::AliasIdxBot, "no bare pointers here");
2735  const Type *value_type = Type::get_const_basic_type(type);
2736
2737  switch (kind) {
2738    case LS_get_set:
2739    case LS_cmp_exchange: {
2740      if (type == T_OBJECT) {
2741        const TypeOopPtr* tjp = sharpen_unsafe_type(alias_type, adr_type);
2742        if (tjp != NULL) {
2743          value_type = tjp;
2744        }
2745      }
2746      break;
2747    }
2748    case LS_cmp_swap:
2749    case LS_cmp_swap_weak:
2750    case LS_get_add:
2751      break;
2752    default:
2753      ShouldNotReachHere();
2754  }
2755
2756  // Null check receiver.
2757  receiver = null_check(receiver);
2758  if (stopped()) {
2759    return true;
2760  }
2761
2762  int alias_idx = C->get_alias_index(adr_type);
2763
2764  // Memory-model-wise, a LoadStore acts like a little synchronized
2765  // block, so needs barriers on each side.  These don't translate
2766  // into actual barriers on most machines, but we still need rest of
2767  // compiler to respect ordering.
2768
2769  switch (access_kind) {
2770    case Relaxed:
2771    case Acquire:
2772      break;
2773    case Release:
2774      insert_mem_bar(Op_MemBarRelease);
2775      break;
2776    case Volatile:
2777      if (support_IRIW_for_not_multiple_copy_atomic_cpu) {
2778        insert_mem_bar(Op_MemBarVolatile);
2779      } else {
2780        insert_mem_bar(Op_MemBarRelease);
2781      }
2782      break;
2783    default:
2784      ShouldNotReachHere();
2785  }
2786  insert_mem_bar(Op_MemBarCPUOrder);
2787
2788  // Figure out the memory ordering.
2789  MemNode::MemOrd mo = access_kind_to_memord(access_kind);
2790
2791  // 4984716: MemBars must be inserted before this
2792  //          memory node in order to avoid a false
2793  //          dependency which will confuse the scheduler.
2794  Node *mem = memory(alias_idx);
2795
2796  // For now, we handle only those cases that actually exist: ints,
2797  // longs, and Object. Adding others should be straightforward.
2798  Node* load_store = NULL;
2799  switch(type) {
2800  case T_BYTE:
2801    switch(kind) {
2802      case LS_get_add:
2803        load_store = _gvn.transform(new GetAndAddBNode(control(), mem, adr, newval, adr_type));
2804        break;
2805      case LS_get_set:
2806        load_store = _gvn.transform(new GetAndSetBNode(control(), mem, adr, newval, adr_type));
2807        break;
2808      case LS_cmp_swap_weak:
2809        load_store = _gvn.transform(new WeakCompareAndSwapBNode(control(), mem, adr, newval, oldval, mo));
2810        break;
2811      case LS_cmp_swap:
2812        load_store = _gvn.transform(new CompareAndSwapBNode(control(), mem, adr, newval, oldval, mo));
2813        break;
2814      case LS_cmp_exchange:
2815        load_store = _gvn.transform(new CompareAndExchangeBNode(control(), mem, adr, newval, oldval, adr_type, mo));
2816        break;
2817      default:
2818        ShouldNotReachHere();
2819    }
2820    break;
2821  case T_SHORT:
2822    switch(kind) {
2823      case LS_get_add:
2824        load_store = _gvn.transform(new GetAndAddSNode(control(), mem, adr, newval, adr_type));
2825        break;
2826      case LS_get_set:
2827        load_store = _gvn.transform(new GetAndSetSNode(control(), mem, adr, newval, adr_type));
2828        break;
2829      case LS_cmp_swap_weak:
2830        load_store = _gvn.transform(new WeakCompareAndSwapSNode(control(), mem, adr, newval, oldval, mo));
2831        break;
2832      case LS_cmp_swap:
2833        load_store = _gvn.transform(new CompareAndSwapSNode(control(), mem, adr, newval, oldval, mo));
2834        break;
2835      case LS_cmp_exchange:
2836        load_store = _gvn.transform(new CompareAndExchangeSNode(control(), mem, adr, newval, oldval, adr_type, mo));
2837        break;
2838      default:
2839        ShouldNotReachHere();
2840    }
2841    break;
2842  case T_INT:
2843    switch(kind) {
2844      case LS_get_add:
2845        load_store = _gvn.transform(new GetAndAddINode(control(), mem, adr, newval, adr_type));
2846        break;
2847      case LS_get_set:
2848        load_store = _gvn.transform(new GetAndSetINode(control(), mem, adr, newval, adr_type));
2849        break;
2850      case LS_cmp_swap_weak:
2851        load_store = _gvn.transform(new WeakCompareAndSwapINode(control(), mem, adr, newval, oldval, mo));
2852        break;
2853      case LS_cmp_swap:
2854        load_store = _gvn.transform(new CompareAndSwapINode(control(), mem, adr, newval, oldval, mo));
2855        break;
2856      case LS_cmp_exchange:
2857        load_store = _gvn.transform(new CompareAndExchangeINode(control(), mem, adr, newval, oldval, adr_type, mo));
2858        break;
2859      default:
2860        ShouldNotReachHere();
2861    }
2862    break;
2863  case T_LONG:
2864    switch(kind) {
2865      case LS_get_add:
2866        load_store = _gvn.transform(new GetAndAddLNode(control(), mem, adr, newval, adr_type));
2867        break;
2868      case LS_get_set:
2869        load_store = _gvn.transform(new GetAndSetLNode(control(), mem, adr, newval, adr_type));
2870        break;
2871      case LS_cmp_swap_weak:
2872        load_store = _gvn.transform(new WeakCompareAndSwapLNode(control(), mem, adr, newval, oldval, mo));
2873        break;
2874      case LS_cmp_swap:
2875        load_store = _gvn.transform(new CompareAndSwapLNode(control(), mem, adr, newval, oldval, mo));
2876        break;
2877      case LS_cmp_exchange:
2878        load_store = _gvn.transform(new CompareAndExchangeLNode(control(), mem, adr, newval, oldval, adr_type, mo));
2879        break;
2880      default:
2881        ShouldNotReachHere();
2882    }
2883    break;
2884  case T_OBJECT:
2885    // Transformation of a value which could be NULL pointer (CastPP #NULL)
2886    // could be delayed during Parse (for example, in adjust_map_after_if()).
2887    // Execute transformation here to avoid barrier generation in such case.
2888    if (_gvn.type(newval) == TypePtr::NULL_PTR)
2889      newval = _gvn.makecon(TypePtr::NULL_PTR);
2890
2891    // Reference stores need a store barrier.
2892    switch(kind) {
2893      case LS_get_set: {
2894        // If pre-barrier must execute before the oop store, old value will require do_load here.
2895        if (!can_move_pre_barrier()) {
2896          pre_barrier(true /* do_load*/,
2897                      control(), base, adr, alias_idx, newval, value_type->make_oopptr(),
2898                      NULL /* pre_val*/,
2899                      T_OBJECT);
2900        } // Else move pre_barrier to use load_store value, see below.
2901        break;
2902      }
2903      case LS_cmp_swap_weak:
2904      case LS_cmp_swap:
2905      case LS_cmp_exchange: {
2906        // Same as for newval above:
2907        if (_gvn.type(oldval) == TypePtr::NULL_PTR) {
2908          oldval = _gvn.makecon(TypePtr::NULL_PTR);
2909        }
2910        // The only known value which might get overwritten is oldval.
2911        pre_barrier(false /* do_load */,
2912                    control(), NULL, NULL, max_juint, NULL, NULL,
2913                    oldval /* pre_val */,
2914                    T_OBJECT);
2915        break;
2916      }
2917      default:
2918        ShouldNotReachHere();
2919    }
2920
2921#ifdef _LP64
2922    if (adr->bottom_type()->is_ptr_to_narrowoop()) {
2923      Node *newval_enc = _gvn.transform(new EncodePNode(newval, newval->bottom_type()->make_narrowoop()));
2924
2925      switch(kind) {
2926        case LS_get_set:
2927          load_store = _gvn.transform(new GetAndSetNNode(control(), mem, adr, newval_enc, adr_type, value_type->make_narrowoop()));
2928          break;
2929        case LS_cmp_swap_weak: {
2930          Node *oldval_enc = _gvn.transform(new EncodePNode(oldval, oldval->bottom_type()->make_narrowoop()));
2931          load_store = _gvn.transform(new WeakCompareAndSwapNNode(control(), mem, adr, newval_enc, oldval_enc, mo));
2932          break;
2933        }
2934        case LS_cmp_swap: {
2935          Node *oldval_enc = _gvn.transform(new EncodePNode(oldval, oldval->bottom_type()->make_narrowoop()));
2936          load_store = _gvn.transform(new CompareAndSwapNNode(control(), mem, adr, newval_enc, oldval_enc, mo));
2937          break;
2938        }
2939        case LS_cmp_exchange: {
2940          Node *oldval_enc = _gvn.transform(new EncodePNode(oldval, oldval->bottom_type()->make_narrowoop()));
2941          load_store = _gvn.transform(new CompareAndExchangeNNode(control(), mem, adr, newval_enc, oldval_enc, adr_type, value_type->make_narrowoop(), mo));
2942          break;
2943        }
2944        default:
2945          ShouldNotReachHere();
2946      }
2947    } else
2948#endif
2949    switch (kind) {
2950      case LS_get_set:
2951        load_store = _gvn.transform(new GetAndSetPNode(control(), mem, adr, newval, adr_type, value_type->is_oopptr()));
2952        break;
2953      case LS_cmp_swap_weak:
2954        load_store = _gvn.transform(new WeakCompareAndSwapPNode(control(), mem, adr, newval, oldval, mo));
2955        break;
2956      case LS_cmp_swap:
2957        load_store = _gvn.transform(new CompareAndSwapPNode(control(), mem, adr, newval, oldval, mo));
2958        break;
2959      case LS_cmp_exchange:
2960        load_store = _gvn.transform(new CompareAndExchangePNode(control(), mem, adr, newval, oldval, adr_type, value_type->is_oopptr(), mo));
2961        break;
2962      default:
2963        ShouldNotReachHere();
2964    }
2965
2966    // Emit the post barrier only when the actual store happened. This makes sense
2967    // to check only for LS_cmp_* that can fail to set the value.
2968    // LS_cmp_exchange does not produce any branches by default, so there is no
2969    // boolean result to piggyback on. TODO: When we merge CompareAndSwap with
2970    // CompareAndExchange and move branches here, it would make sense to conditionalize
2971    // post_barriers for LS_cmp_exchange as well.
2972    //
2973    // CAS success path is marked more likely since we anticipate this is a performance
2974    // critical path, while CAS failure path can use the penalty for going through unlikely
2975    // path as backoff. Which is still better than doing a store barrier there.
2976    switch (kind) {
2977      case LS_get_set:
2978      case LS_cmp_exchange: {
2979        post_barrier(control(), load_store, base, adr, alias_idx, newval, T_OBJECT, true);
2980        break;
2981      }
2982      case LS_cmp_swap_weak:
2983      case LS_cmp_swap: {
2984        IdealKit ideal(this);
2985        ideal.if_then(load_store, BoolTest::ne, ideal.ConI(0), PROB_STATIC_FREQUENT); {
2986          sync_kit(ideal);
2987          post_barrier(ideal.ctrl(), load_store, base, adr, alias_idx, newval, T_OBJECT, true);
2988          ideal.sync_kit(this);
2989        } ideal.end_if();
2990        final_sync(ideal);
2991        break;
2992      }
2993      default:
2994        ShouldNotReachHere();
2995    }
2996    break;
2997  default:
2998    fatal("unexpected type %d: %s", type, type2name(type));
2999    break;
3000  }
3001
3002  // SCMemProjNodes represent the memory state of a LoadStore. Their
3003  // main role is to prevent LoadStore nodes from being optimized away
3004  // when their results aren't used.
3005  Node* proj = _gvn.transform(new SCMemProjNode(load_store));
3006  set_memory(proj, alias_idx);
3007
3008  if (type == T_OBJECT && (kind == LS_get_set || kind == LS_cmp_exchange)) {
3009#ifdef _LP64
3010    if (adr->bottom_type()->is_ptr_to_narrowoop()) {
3011      load_store = _gvn.transform(new DecodeNNode(load_store, load_store->get_ptr_type()));
3012    }
3013#endif
3014    if (can_move_pre_barrier()) {
3015      // Don't need to load pre_val. The old value is returned by load_store.
3016      // The pre_barrier can execute after the xchg as long as no safepoint
3017      // gets inserted between them.
3018      pre_barrier(false /* do_load */,
3019                  control(), NULL, NULL, max_juint, NULL, NULL,
3020                  load_store /* pre_val */,
3021                  T_OBJECT);
3022    }
3023  }
3024
3025  // Add the trailing membar surrounding the access
3026  insert_mem_bar(Op_MemBarCPUOrder);
3027
3028  switch (access_kind) {
3029    case Relaxed:
3030    case Release:
3031      break; // do nothing
3032    case Acquire:
3033    case Volatile:
3034      insert_mem_bar(Op_MemBarAcquire);
3035      // !support_IRIW_for_not_multiple_copy_atomic_cpu handled in platform code
3036      break;
3037    default:
3038      ShouldNotReachHere();
3039  }
3040
3041  assert(type2size[load_store->bottom_type()->basic_type()] == type2size[rtype], "result type should match");
3042  set_result(load_store);
3043  return true;
3044}
3045
3046MemNode::MemOrd LibraryCallKit::access_kind_to_memord_LS(AccessKind kind, bool is_store) {
3047  MemNode::MemOrd mo = MemNode::unset;
3048  switch(kind) {
3049    case Opaque:
3050    case Relaxed:  mo = MemNode::unordered; break;
3051    case Acquire:  mo = MemNode::acquire;   break;
3052    case Release:  mo = MemNode::release;   break;
3053    case Volatile: mo = is_store ? MemNode::release : MemNode::acquire; break;
3054    default:
3055      ShouldNotReachHere();
3056  }
3057  guarantee(mo != MemNode::unset, "Should select memory ordering");
3058  return mo;
3059}
3060
3061MemNode::MemOrd LibraryCallKit::access_kind_to_memord(AccessKind kind) {
3062  MemNode::MemOrd mo = MemNode::unset;
3063  switch(kind) {
3064    case Opaque:
3065    case Relaxed:  mo = MemNode::unordered; break;
3066    case Acquire:  mo = MemNode::acquire;   break;
3067    case Release:  mo = MemNode::release;   break;
3068    case Volatile: mo = MemNode::seqcst;    break;
3069    default:
3070      ShouldNotReachHere();
3071  }
3072  guarantee(mo != MemNode::unset, "Should select memory ordering");
3073  return mo;
3074}
3075
3076bool LibraryCallKit::inline_unsafe_fence(vmIntrinsics::ID id) {
3077  // Regardless of form, don't allow previous ld/st to move down,
3078  // then issue acquire, release, or volatile mem_bar.
3079  insert_mem_bar(Op_MemBarCPUOrder);
3080  switch(id) {
3081    case vmIntrinsics::_loadFence:
3082      insert_mem_bar(Op_LoadFence);
3083      return true;
3084    case vmIntrinsics::_storeFence:
3085      insert_mem_bar(Op_StoreFence);
3086      return true;
3087    case vmIntrinsics::_fullFence:
3088      insert_mem_bar(Op_MemBarVolatile);
3089      return true;
3090    default:
3091      fatal_unexpected_iid(id);
3092      return false;
3093  }
3094}
3095
3096bool LibraryCallKit::inline_onspinwait() {
3097  insert_mem_bar(Op_OnSpinWait);
3098  return true;
3099}
3100
3101bool LibraryCallKit::klass_needs_init_guard(Node* kls) {
3102  if (!kls->is_Con()) {
3103    return true;
3104  }
3105  const TypeKlassPtr* klsptr = kls->bottom_type()->isa_klassptr();
3106  if (klsptr == NULL) {
3107    return true;
3108  }
3109  ciInstanceKlass* ik = klsptr->klass()->as_instance_klass();
3110  // don't need a guard for a klass that is already initialized
3111  return !ik->is_initialized();
3112}
3113
3114//----------------------------inline_unsafe_allocate---------------------------
3115// public native Object Unsafe.allocateInstance(Class<?> cls);
3116bool LibraryCallKit::inline_unsafe_allocate() {
3117  if (callee()->is_static())  return false;  // caller must have the capability!
3118
3119  null_check_receiver();  // null-check, then ignore
3120  Node* cls = null_check(argument(1));
3121  if (stopped())  return true;
3122
3123  Node* kls = load_klass_from_mirror(cls, false, NULL, 0);
3124  kls = null_check(kls);
3125  if (stopped())  return true;  // argument was like int.class
3126
3127  Node* test = NULL;
3128  if (LibraryCallKit::klass_needs_init_guard(kls)) {
3129    // Note:  The argument might still be an illegal value like
3130    // Serializable.class or Object[].class.   The runtime will handle it.
3131    // But we must make an explicit check for initialization.
3132    Node* insp = basic_plus_adr(kls, in_bytes(InstanceKlass::init_state_offset()));
3133    // Use T_BOOLEAN for InstanceKlass::_init_state so the compiler
3134    // can generate code to load it as unsigned byte.
3135    Node* inst = make_load(NULL, insp, TypeInt::UBYTE, T_BOOLEAN, MemNode::unordered);
3136    Node* bits = intcon(InstanceKlass::fully_initialized);
3137    test = _gvn.transform(new SubINode(inst, bits));
3138    // The 'test' is non-zero if we need to take a slow path.
3139  }
3140
3141  Node* obj = new_instance(kls, test);
3142  set_result(obj);
3143  return true;
3144}
3145
3146//------------------------inline_native_time_funcs--------------
3147// inline code for System.currentTimeMillis() and System.nanoTime()
3148// these have the same type and signature
3149bool LibraryCallKit::inline_native_time_funcs(address funcAddr, const char* funcName) {
3150  const TypeFunc* tf = OptoRuntime::void_long_Type();
3151  const TypePtr* no_memory_effects = NULL;
3152  Node* time = make_runtime_call(RC_LEAF, tf, funcAddr, funcName, no_memory_effects);
3153  Node* value = _gvn.transform(new ProjNode(time, TypeFunc::Parms+0));
3154#ifdef ASSERT
3155  Node* value_top = _gvn.transform(new ProjNode(time, TypeFunc::Parms+1));
3156  assert(value_top == top(), "second value must be top");
3157#endif
3158  set_result(value);
3159  return true;
3160}
3161
3162//------------------------inline_native_currentThread------------------
3163bool LibraryCallKit::inline_native_currentThread() {
3164  Node* junk = NULL;
3165  set_result(generate_current_thread(junk));
3166  return true;
3167}
3168
3169//------------------------inline_native_isInterrupted------------------
3170// private native boolean java.lang.Thread.isInterrupted(boolean ClearInterrupted);
3171bool LibraryCallKit::inline_native_isInterrupted() {
3172  // Add a fast path to t.isInterrupted(clear_int):
3173  //   (t == Thread.current() &&
3174  //    (!TLS._osthread._interrupted || WINDOWS_ONLY(false) NOT_WINDOWS(!clear_int)))
3175  //   ? TLS._osthread._interrupted : /*slow path:*/ t.isInterrupted(clear_int)
3176  // So, in the common case that the interrupt bit is false,
3177  // we avoid making a call into the VM.  Even if the interrupt bit
3178  // is true, if the clear_int argument is false, we avoid the VM call.
3179  // However, if the receiver is not currentThread, we must call the VM,
3180  // because there must be some locking done around the operation.
3181
3182  // We only go to the fast case code if we pass two guards.
3183  // Paths which do not pass are accumulated in the slow_region.
3184
3185  enum {
3186    no_int_result_path   = 1, // t == Thread.current() && !TLS._osthread._interrupted
3187    no_clear_result_path = 2, // t == Thread.current() &&  TLS._osthread._interrupted && !clear_int
3188    slow_result_path     = 3, // slow path: t.isInterrupted(clear_int)
3189    PATH_LIMIT
3190  };
3191
3192  // Ensure that it's not possible to move the load of TLS._osthread._interrupted flag
3193  // out of the function.
3194  insert_mem_bar(Op_MemBarCPUOrder);
3195
3196  RegionNode* result_rgn = new RegionNode(PATH_LIMIT);
3197  PhiNode*    result_val = new PhiNode(result_rgn, TypeInt::BOOL);
3198
3199  RegionNode* slow_region = new RegionNode(1);
3200  record_for_igvn(slow_region);
3201
3202  // (a) Receiving thread must be the current thread.
3203  Node* rec_thr = argument(0);
3204  Node* tls_ptr = NULL;
3205  Node* cur_thr = generate_current_thread(tls_ptr);
3206  Node* cmp_thr = _gvn.transform(new CmpPNode(cur_thr, rec_thr));
3207  Node* bol_thr = _gvn.transform(new BoolNode(cmp_thr, BoolTest::ne));
3208
3209  generate_slow_guard(bol_thr, slow_region);
3210
3211  // (b) Interrupt bit on TLS must be false.
3212  Node* p = basic_plus_adr(top()/*!oop*/, tls_ptr, in_bytes(JavaThread::osthread_offset()));
3213  Node* osthread = make_load(NULL, p, TypeRawPtr::NOTNULL, T_ADDRESS, MemNode::unordered);
3214  p = basic_plus_adr(top()/*!oop*/, osthread, in_bytes(OSThread::interrupted_offset()));
3215
3216  // Set the control input on the field _interrupted read to prevent it floating up.
3217  Node* int_bit = make_load(control(), p, TypeInt::BOOL, T_INT, MemNode::unordered);
3218  Node* cmp_bit = _gvn.transform(new CmpINode(int_bit, intcon(0)));
3219  Node* bol_bit = _gvn.transform(new BoolNode(cmp_bit, BoolTest::ne));
3220
3221  IfNode* iff_bit = create_and_map_if(control(), bol_bit, PROB_UNLIKELY_MAG(3), COUNT_UNKNOWN);
3222
3223  // First fast path:  if (!TLS._interrupted) return false;
3224  Node* false_bit = _gvn.transform(new IfFalseNode(iff_bit));
3225  result_rgn->init_req(no_int_result_path, false_bit);
3226  result_val->init_req(no_int_result_path, intcon(0));
3227
3228  // drop through to next case
3229  set_control( _gvn.transform(new IfTrueNode(iff_bit)));
3230
3231#ifndef _WINDOWS
3232  // (c) Or, if interrupt bit is set and clear_int is false, use 2nd fast path.
3233  Node* clr_arg = argument(1);
3234  Node* cmp_arg = _gvn.transform(new CmpINode(clr_arg, intcon(0)));
3235  Node* bol_arg = _gvn.transform(new BoolNode(cmp_arg, BoolTest::ne));
3236  IfNode* iff_arg = create_and_map_if(control(), bol_arg, PROB_FAIR, COUNT_UNKNOWN);
3237
3238  // Second fast path:  ... else if (!clear_int) return true;
3239  Node* false_arg = _gvn.transform(new IfFalseNode(iff_arg));
3240  result_rgn->init_req(no_clear_result_path, false_arg);
3241  result_val->init_req(no_clear_result_path, intcon(1));
3242
3243  // drop through to next case
3244  set_control( _gvn.transform(new IfTrueNode(iff_arg)));
3245#else
3246  // To return true on Windows you must read the _interrupted field
3247  // and check the event state i.e. take the slow path.
3248#endif // _WINDOWS
3249
3250  // (d) Otherwise, go to the slow path.
3251  slow_region->add_req(control());
3252  set_control( _gvn.transform(slow_region));
3253
3254  if (stopped()) {
3255    // There is no slow path.
3256    result_rgn->init_req(slow_result_path, top());
3257    result_val->init_req(slow_result_path, top());
3258  } else {
3259    // non-virtual because it is a private non-static
3260    CallJavaNode* slow_call = generate_method_call(vmIntrinsics::_isInterrupted);
3261
3262    Node* slow_val = set_results_for_java_call(slow_call);
3263    // this->control() comes from set_results_for_java_call
3264
3265    Node* fast_io  = slow_call->in(TypeFunc::I_O);
3266    Node* fast_mem = slow_call->in(TypeFunc::Memory);
3267
3268    // These two phis are pre-filled with copies of of the fast IO and Memory
3269    PhiNode* result_mem  = PhiNode::make(result_rgn, fast_mem, Type::MEMORY, TypePtr::BOTTOM);
3270    PhiNode* result_io   = PhiNode::make(result_rgn, fast_io,  Type::ABIO);
3271
3272    result_rgn->init_req(slow_result_path, control());
3273    result_io ->init_req(slow_result_path, i_o());
3274    result_mem->init_req(slow_result_path, reset_memory());
3275    result_val->init_req(slow_result_path, slow_val);
3276
3277    set_all_memory(_gvn.transform(result_mem));
3278    set_i_o(       _gvn.transform(result_io));
3279  }
3280
3281  C->set_has_split_ifs(true); // Has chance for split-if optimization
3282  set_result(result_rgn, result_val);
3283  return true;
3284}
3285
3286//---------------------------load_mirror_from_klass----------------------------
3287// Given a klass oop, load its java mirror (a java.lang.Class oop).
3288Node* LibraryCallKit::load_mirror_from_klass(Node* klass) {
3289  Node* p = basic_plus_adr(klass, in_bytes(Klass::java_mirror_offset()));
3290  return make_load(NULL, p, TypeInstPtr::MIRROR, T_OBJECT, MemNode::unordered);
3291}
3292
3293//-----------------------load_klass_from_mirror_common-------------------------
3294// Given a java mirror (a java.lang.Class oop), load its corresponding klass oop.
3295// Test the klass oop for null (signifying a primitive Class like Integer.TYPE),
3296// and branch to the given path on the region.
3297// If never_see_null, take an uncommon trap on null, so we can optimistically
3298// compile for the non-null case.
3299// If the region is NULL, force never_see_null = true.
3300Node* LibraryCallKit::load_klass_from_mirror_common(Node* mirror,
3301                                                    bool never_see_null,
3302                                                    RegionNode* region,
3303                                                    int null_path,
3304                                                    int offset) {
3305  if (region == NULL)  never_see_null = true;
3306  Node* p = basic_plus_adr(mirror, offset);
3307  const TypeKlassPtr*  kls_type = TypeKlassPtr::OBJECT_OR_NULL;
3308  Node* kls = _gvn.transform(LoadKlassNode::make(_gvn, NULL, immutable_memory(), p, TypeRawPtr::BOTTOM, kls_type));
3309  Node* null_ctl = top();
3310  kls = null_check_oop(kls, &null_ctl, never_see_null);
3311  if (region != NULL) {
3312    // Set region->in(null_path) if the mirror is a primitive (e.g, int.class).
3313    region->init_req(null_path, null_ctl);
3314  } else {
3315    assert(null_ctl == top(), "no loose ends");
3316  }
3317  return kls;
3318}
3319
3320//--------------------(inline_native_Class_query helpers)---------------------
3321// Use this for JVM_ACC_INTERFACE, JVM_ACC_IS_CLONEABLE_FAST, JVM_ACC_HAS_FINALIZER.
3322// Fall through if (mods & mask) == bits, take the guard otherwise.
3323Node* LibraryCallKit::generate_access_flags_guard(Node* kls, int modifier_mask, int modifier_bits, RegionNode* region) {
3324  // Branch around if the given klass has the given modifier bit set.
3325  // Like generate_guard, adds a new path onto the region.
3326  Node* modp = basic_plus_adr(kls, in_bytes(Klass::access_flags_offset()));
3327  Node* mods = make_load(NULL, modp, TypeInt::INT, T_INT, MemNode::unordered);
3328  Node* mask = intcon(modifier_mask);
3329  Node* bits = intcon(modifier_bits);
3330  Node* mbit = _gvn.transform(new AndINode(mods, mask));
3331  Node* cmp  = _gvn.transform(new CmpINode(mbit, bits));
3332  Node* bol  = _gvn.transform(new BoolNode(cmp, BoolTest::ne));
3333  return generate_fair_guard(bol, region);
3334}
3335Node* LibraryCallKit::generate_interface_guard(Node* kls, RegionNode* region) {
3336  return generate_access_flags_guard(kls, JVM_ACC_INTERFACE, 0, region);
3337}
3338
3339//-------------------------inline_native_Class_query-------------------
3340bool LibraryCallKit::inline_native_Class_query(vmIntrinsics::ID id) {
3341  const Type* return_type = TypeInt::BOOL;
3342  Node* prim_return_value = top();  // what happens if it's a primitive class?
3343  bool never_see_null = !too_many_traps(Deoptimization::Reason_null_check);
3344  bool expect_prim = false;     // most of these guys expect to work on refs
3345
3346  enum { _normal_path = 1, _prim_path = 2, PATH_LIMIT };
3347
3348  Node* mirror = argument(0);
3349  Node* obj    = top();
3350
3351  switch (id) {
3352  case vmIntrinsics::_isInstance:
3353    // nothing is an instance of a primitive type
3354    prim_return_value = intcon(0);
3355    obj = argument(1);
3356    break;
3357  case vmIntrinsics::_getModifiers:
3358    prim_return_value = intcon(JVM_ACC_ABSTRACT | JVM_ACC_FINAL | JVM_ACC_PUBLIC);
3359    assert(is_power_of_2((int)JVM_ACC_WRITTEN_FLAGS+1), "change next line");
3360    return_type = TypeInt::make(0, JVM_ACC_WRITTEN_FLAGS, Type::WidenMin);
3361    break;
3362  case vmIntrinsics::_isInterface:
3363    prim_return_value = intcon(0);
3364    break;
3365  case vmIntrinsics::_isArray:
3366    prim_return_value = intcon(0);
3367    expect_prim = true;  // cf. ObjectStreamClass.getClassSignature
3368    break;
3369  case vmIntrinsics::_isPrimitive:
3370    prim_return_value = intcon(1);
3371    expect_prim = true;  // obviously
3372    break;
3373  case vmIntrinsics::_getSuperclass:
3374    prim_return_value = null();
3375    return_type = TypeInstPtr::MIRROR->cast_to_ptr_type(TypePtr::BotPTR);
3376    break;
3377  case vmIntrinsics::_getClassAccessFlags:
3378    prim_return_value = intcon(JVM_ACC_ABSTRACT | JVM_ACC_FINAL | JVM_ACC_PUBLIC);
3379    return_type = TypeInt::INT;  // not bool!  6297094
3380    break;
3381  default:
3382    fatal_unexpected_iid(id);
3383    break;
3384  }
3385
3386  const TypeInstPtr* mirror_con = _gvn.type(mirror)->isa_instptr();
3387  if (mirror_con == NULL)  return false;  // cannot happen?
3388
3389#ifndef PRODUCT
3390  if (C->print_intrinsics() || C->print_inlining()) {
3391    ciType* k = mirror_con->java_mirror_type();
3392    if (k) {
3393      tty->print("Inlining %s on constant Class ", vmIntrinsics::name_at(intrinsic_id()));
3394      k->print_name();
3395      tty->cr();
3396    }
3397  }
3398#endif
3399
3400  // Null-check the mirror, and the mirror's klass ptr (in case it is a primitive).
3401  RegionNode* region = new RegionNode(PATH_LIMIT);
3402  record_for_igvn(region);
3403  PhiNode* phi = new PhiNode(region, return_type);
3404
3405  // The mirror will never be null of Reflection.getClassAccessFlags, however
3406  // it may be null for Class.isInstance or Class.getModifiers. Throw a NPE
3407  // if it is. See bug 4774291.
3408
3409  // For Reflection.getClassAccessFlags(), the null check occurs in
3410  // the wrong place; see inline_unsafe_access(), above, for a similar
3411  // situation.
3412  mirror = null_check(mirror);
3413  // If mirror or obj is dead, only null-path is taken.
3414  if (stopped())  return true;
3415
3416  if (expect_prim)  never_see_null = false;  // expect nulls (meaning prims)
3417
3418  // Now load the mirror's klass metaobject, and null-check it.
3419  // Side-effects region with the control path if the klass is null.
3420  Node* kls = load_klass_from_mirror(mirror, never_see_null, region, _prim_path);
3421  // If kls is null, we have a primitive mirror.
3422  phi->init_req(_prim_path, prim_return_value);
3423  if (stopped()) { set_result(region, phi); return true; }
3424  bool safe_for_replace = (region->in(_prim_path) == top());
3425
3426  Node* p;  // handy temp
3427  Node* null_ctl;
3428
3429  // Now that we have the non-null klass, we can perform the real query.
3430  // For constant classes, the query will constant-fold in LoadNode::Value.
3431  Node* query_value = top();
3432  switch (id) {
3433  case vmIntrinsics::_isInstance:
3434    // nothing is an instance of a primitive type
3435    query_value = gen_instanceof(obj, kls, safe_for_replace);
3436    break;
3437
3438  case vmIntrinsics::_getModifiers:
3439    p = basic_plus_adr(kls, in_bytes(Klass::modifier_flags_offset()));
3440    query_value = make_load(NULL, p, TypeInt::INT, T_INT, MemNode::unordered);
3441    break;
3442
3443  case vmIntrinsics::_isInterface:
3444    // (To verify this code sequence, check the asserts in JVM_IsInterface.)
3445    if (generate_interface_guard(kls, region) != NULL)
3446      // A guard was added.  If the guard is taken, it was an interface.
3447      phi->add_req(intcon(1));
3448    // If we fall through, it's a plain class.
3449    query_value = intcon(0);
3450    break;
3451
3452  case vmIntrinsics::_isArray:
3453    // (To verify this code sequence, check the asserts in JVM_IsArrayClass.)
3454    if (generate_array_guard(kls, region) != NULL)
3455      // A guard was added.  If the guard is taken, it was an array.
3456      phi->add_req(intcon(1));
3457    // If we fall through, it's a plain class.
3458    query_value = intcon(0);
3459    break;
3460
3461  case vmIntrinsics::_isPrimitive:
3462    query_value = intcon(0); // "normal" path produces false
3463    break;
3464
3465  case vmIntrinsics::_getSuperclass:
3466    // The rules here are somewhat unfortunate, but we can still do better
3467    // with random logic than with a JNI call.
3468    // Interfaces store null or Object as _super, but must report null.
3469    // Arrays store an intermediate super as _super, but must report Object.
3470    // Other types can report the actual _super.
3471    // (To verify this code sequence, check the asserts in JVM_IsInterface.)
3472    if (generate_interface_guard(kls, region) != NULL)
3473      // A guard was added.  If the guard is taken, it was an interface.
3474      phi->add_req(null());
3475    if (generate_array_guard(kls, region) != NULL)
3476      // A guard was added.  If the guard is taken, it was an array.
3477      phi->add_req(makecon(TypeInstPtr::make(env()->Object_klass()->java_mirror())));
3478    // If we fall through, it's a plain class.  Get its _super.
3479    p = basic_plus_adr(kls, in_bytes(Klass::super_offset()));
3480    kls = _gvn.transform(LoadKlassNode::make(_gvn, NULL, immutable_memory(), p, TypeRawPtr::BOTTOM, TypeKlassPtr::OBJECT_OR_NULL));
3481    null_ctl = top();
3482    kls = null_check_oop(kls, &null_ctl);
3483    if (null_ctl != top()) {
3484      // If the guard is taken, Object.superClass is null (both klass and mirror).
3485      region->add_req(null_ctl);
3486      phi   ->add_req(null());
3487    }
3488    if (!stopped()) {
3489      query_value = load_mirror_from_klass(kls);
3490    }
3491    break;
3492
3493  case vmIntrinsics::_getClassAccessFlags:
3494    p = basic_plus_adr(kls, in_bytes(Klass::access_flags_offset()));
3495    query_value = make_load(NULL, p, TypeInt::INT, T_INT, MemNode::unordered);
3496    break;
3497
3498  default:
3499    fatal_unexpected_iid(id);
3500    break;
3501  }
3502
3503  // Fall-through is the normal case of a query to a real class.
3504  phi->init_req(1, query_value);
3505  region->init_req(1, control());
3506
3507  C->set_has_split_ifs(true); // Has chance for split-if optimization
3508  set_result(region, phi);
3509  return true;
3510}
3511
3512//-------------------------inline_Class_cast-------------------
3513bool LibraryCallKit::inline_Class_cast() {
3514  Node* mirror = argument(0); // Class
3515  Node* obj    = argument(1);
3516  const TypeInstPtr* mirror_con = _gvn.type(mirror)->isa_instptr();
3517  if (mirror_con == NULL) {
3518    return false;  // dead path (mirror->is_top()).
3519  }
3520  if (obj == NULL || obj->is_top()) {
3521    return false;  // dead path
3522  }
3523  const TypeOopPtr* tp = _gvn.type(obj)->isa_oopptr();
3524
3525  // First, see if Class.cast() can be folded statically.
3526  // java_mirror_type() returns non-null for compile-time Class constants.
3527  ciType* tm = mirror_con->java_mirror_type();
3528  if (tm != NULL && tm->is_klass() &&
3529      tp != NULL && tp->klass() != NULL) {
3530    if (!tp->klass()->is_loaded()) {
3531      // Don't use intrinsic when class is not loaded.
3532      return false;
3533    } else {
3534      int static_res = C->static_subtype_check(tm->as_klass(), tp->klass());
3535      if (static_res == Compile::SSC_always_true) {
3536        // isInstance() is true - fold the code.
3537        set_result(obj);
3538        return true;
3539      } else if (static_res == Compile::SSC_always_false) {
3540        // Don't use intrinsic, have to throw ClassCastException.
3541        // If the reference is null, the non-intrinsic bytecode will
3542        // be optimized appropriately.
3543        return false;
3544      }
3545    }
3546  }
3547
3548  // Bailout intrinsic and do normal inlining if exception path is frequent.
3549  if (too_many_traps(Deoptimization::Reason_intrinsic)) {
3550    return false;
3551  }
3552
3553  // Generate dynamic checks.
3554  // Class.cast() is java implementation of _checkcast bytecode.
3555  // Do checkcast (Parse::do_checkcast()) optimizations here.
3556
3557  mirror = null_check(mirror);
3558  // If mirror is dead, only null-path is taken.
3559  if (stopped()) {
3560    return true;
3561  }
3562
3563  // Not-subtype or the mirror's klass ptr is NULL (in case it is a primitive).
3564  enum { _bad_type_path = 1, _prim_path = 2, PATH_LIMIT };
3565  RegionNode* region = new RegionNode(PATH_LIMIT);
3566  record_for_igvn(region);
3567
3568  // Now load the mirror's klass metaobject, and null-check it.
3569  // If kls is null, we have a primitive mirror and
3570  // nothing is an instance of a primitive type.
3571  Node* kls = load_klass_from_mirror(mirror, false, region, _prim_path);
3572
3573  Node* res = top();
3574  if (!stopped()) {
3575    Node* bad_type_ctrl = top();
3576    // Do checkcast optimizations.
3577    res = gen_checkcast(obj, kls, &bad_type_ctrl);
3578    region->init_req(_bad_type_path, bad_type_ctrl);
3579  }
3580  if (region->in(_prim_path) != top() ||
3581      region->in(_bad_type_path) != top()) {
3582    // Let Interpreter throw ClassCastException.
3583    PreserveJVMState pjvms(this);
3584    set_control(_gvn.transform(region));
3585    uncommon_trap(Deoptimization::Reason_intrinsic,
3586                  Deoptimization::Action_maybe_recompile);
3587  }
3588  if (!stopped()) {
3589    set_result(res);
3590  }
3591  return true;
3592}
3593
3594
3595//--------------------------inline_native_subtype_check------------------------
3596// This intrinsic takes the JNI calls out of the heart of
3597// UnsafeFieldAccessorImpl.set, which improves Field.set, readObject, etc.
3598bool LibraryCallKit::inline_native_subtype_check() {
3599  // Pull both arguments off the stack.
3600  Node* args[2];                // two java.lang.Class mirrors: superc, subc
3601  args[0] = argument(0);
3602  args[1] = argument(1);
3603  Node* klasses[2];             // corresponding Klasses: superk, subk
3604  klasses[0] = klasses[1] = top();
3605
3606  enum {
3607    // A full decision tree on {superc is prim, subc is prim}:
3608    _prim_0_path = 1,           // {P,N} => false
3609                                // {P,P} & superc!=subc => false
3610    _prim_same_path,            // {P,P} & superc==subc => true
3611    _prim_1_path,               // {N,P} => false
3612    _ref_subtype_path,          // {N,N} & subtype check wins => true
3613    _both_ref_path,             // {N,N} & subtype check loses => false
3614    PATH_LIMIT
3615  };
3616
3617  RegionNode* region = new RegionNode(PATH_LIMIT);
3618  Node*       phi    = new PhiNode(region, TypeInt::BOOL);
3619  record_for_igvn(region);
3620
3621  const TypePtr* adr_type = TypeRawPtr::BOTTOM;   // memory type of loads
3622  const TypeKlassPtr* kls_type = TypeKlassPtr::OBJECT_OR_NULL;
3623  int class_klass_offset = java_lang_Class::klass_offset_in_bytes();
3624
3625  // First null-check both mirrors and load each mirror's klass metaobject.
3626  int which_arg;
3627  for (which_arg = 0; which_arg <= 1; which_arg++) {
3628    Node* arg = args[which_arg];
3629    arg = null_check(arg);
3630    if (stopped())  break;
3631    args[which_arg] = arg;
3632
3633    Node* p = basic_plus_adr(arg, class_klass_offset);
3634    Node* kls = LoadKlassNode::make(_gvn, NULL, immutable_memory(), p, adr_type, kls_type);
3635    klasses[which_arg] = _gvn.transform(kls);
3636  }
3637
3638  // Having loaded both klasses, test each for null.
3639  bool never_see_null = !too_many_traps(Deoptimization::Reason_null_check);
3640  for (which_arg = 0; which_arg <= 1; which_arg++) {
3641    Node* kls = klasses[which_arg];
3642    Node* null_ctl = top();
3643    kls = null_check_oop(kls, &null_ctl, never_see_null);
3644    int prim_path = (which_arg == 0 ? _prim_0_path : _prim_1_path);
3645    region->init_req(prim_path, null_ctl);
3646    if (stopped())  break;
3647    klasses[which_arg] = kls;
3648  }
3649
3650  if (!stopped()) {
3651    // now we have two reference types, in klasses[0..1]
3652    Node* subk   = klasses[1];  // the argument to isAssignableFrom
3653    Node* superk = klasses[0];  // the receiver
3654    region->set_req(_both_ref_path, gen_subtype_check(subk, superk));
3655    // now we have a successful reference subtype check
3656    region->set_req(_ref_subtype_path, control());
3657  }
3658
3659  // If both operands are primitive (both klasses null), then
3660  // we must return true when they are identical primitives.
3661  // It is convenient to test this after the first null klass check.
3662  set_control(region->in(_prim_0_path)); // go back to first null check
3663  if (!stopped()) {
3664    // Since superc is primitive, make a guard for the superc==subc case.
3665    Node* cmp_eq = _gvn.transform(new CmpPNode(args[0], args[1]));
3666    Node* bol_eq = _gvn.transform(new BoolNode(cmp_eq, BoolTest::eq));
3667    generate_guard(bol_eq, region, PROB_FAIR);
3668    if (region->req() == PATH_LIMIT+1) {
3669      // A guard was added.  If the added guard is taken, superc==subc.
3670      region->swap_edges(PATH_LIMIT, _prim_same_path);
3671      region->del_req(PATH_LIMIT);
3672    }
3673    region->set_req(_prim_0_path, control()); // Not equal after all.
3674  }
3675
3676  // these are the only paths that produce 'true':
3677  phi->set_req(_prim_same_path,   intcon(1));
3678  phi->set_req(_ref_subtype_path, intcon(1));
3679
3680  // pull together the cases:
3681  assert(region->req() == PATH_LIMIT, "sane region");
3682  for (uint i = 1; i < region->req(); i++) {
3683    Node* ctl = region->in(i);
3684    if (ctl == NULL || ctl == top()) {
3685      region->set_req(i, top());
3686      phi   ->set_req(i, top());
3687    } else if (phi->in(i) == NULL) {
3688      phi->set_req(i, intcon(0)); // all other paths produce 'false'
3689    }
3690  }
3691
3692  set_control(_gvn.transform(region));
3693  set_result(_gvn.transform(phi));
3694  return true;
3695}
3696
3697//---------------------generate_array_guard_common------------------------
3698Node* LibraryCallKit::generate_array_guard_common(Node* kls, RegionNode* region,
3699                                                  bool obj_array, bool not_array) {
3700
3701  if (stopped()) {
3702    return NULL;
3703  }
3704
3705  // If obj_array/non_array==false/false:
3706  // Branch around if the given klass is in fact an array (either obj or prim).
3707  // If obj_array/non_array==false/true:
3708  // Branch around if the given klass is not an array klass of any kind.
3709  // If obj_array/non_array==true/true:
3710  // Branch around if the kls is not an oop array (kls is int[], String, etc.)
3711  // If obj_array/non_array==true/false:
3712  // Branch around if the kls is an oop array (Object[] or subtype)
3713  //
3714  // Like generate_guard, adds a new path onto the region.
3715  jint  layout_con = 0;
3716  Node* layout_val = get_layout_helper(kls, layout_con);
3717  if (layout_val == NULL) {
3718    bool query = (obj_array
3719                  ? Klass::layout_helper_is_objArray(layout_con)
3720                  : Klass::layout_helper_is_array(layout_con));
3721    if (query == not_array) {
3722      return NULL;                       // never a branch
3723    } else {                             // always a branch
3724      Node* always_branch = control();
3725      if (region != NULL)
3726        region->add_req(always_branch);
3727      set_control(top());
3728      return always_branch;
3729    }
3730  }
3731  // Now test the correct condition.
3732  jint  nval = (obj_array
3733                ? (jint)(Klass::_lh_array_tag_type_value
3734                   <<    Klass::_lh_array_tag_shift)
3735                : Klass::_lh_neutral_value);
3736  Node* cmp = _gvn.transform(new CmpINode(layout_val, intcon(nval)));
3737  BoolTest::mask btest = BoolTest::lt;  // correct for testing is_[obj]array
3738  // invert the test if we are looking for a non-array
3739  if (not_array)  btest = BoolTest(btest).negate();
3740  Node* bol = _gvn.transform(new BoolNode(cmp, btest));
3741  return generate_fair_guard(bol, region);
3742}
3743
3744
3745//-----------------------inline_native_newArray--------------------------
3746// private static native Object java.lang.reflect.newArray(Class<?> componentType, int length);
3747// private        native Object Unsafe.allocateUninitializedArray0(Class<?> cls, int size);
3748bool LibraryCallKit::inline_unsafe_newArray(bool uninitialized) {
3749  Node* mirror;
3750  Node* count_val;
3751  if (uninitialized) {
3752    mirror    = argument(1);
3753    count_val = argument(2);
3754  } else {
3755    mirror    = argument(0);
3756    count_val = argument(1);
3757  }
3758
3759  mirror = null_check(mirror);
3760  // If mirror or obj is dead, only null-path is taken.
3761  if (stopped())  return true;
3762
3763  enum { _normal_path = 1, _slow_path = 2, PATH_LIMIT };
3764  RegionNode* result_reg = new RegionNode(PATH_LIMIT);
3765  PhiNode*    result_val = new PhiNode(result_reg, TypeInstPtr::NOTNULL);
3766  PhiNode*    result_io  = new PhiNode(result_reg, Type::ABIO);
3767  PhiNode*    result_mem = new PhiNode(result_reg, Type::MEMORY, TypePtr::BOTTOM);
3768
3769  bool never_see_null = !too_many_traps(Deoptimization::Reason_null_check);
3770  Node* klass_node = load_array_klass_from_mirror(mirror, never_see_null,
3771                                                  result_reg, _slow_path);
3772  Node* normal_ctl   = control();
3773  Node* no_array_ctl = result_reg->in(_slow_path);
3774
3775  // Generate code for the slow case.  We make a call to newArray().
3776  set_control(no_array_ctl);
3777  if (!stopped()) {
3778    // Either the input type is void.class, or else the
3779    // array klass has not yet been cached.  Either the
3780    // ensuing call will throw an exception, or else it
3781    // will cache the array klass for next time.
3782    PreserveJVMState pjvms(this);
3783    CallJavaNode* slow_call = generate_method_call_static(vmIntrinsics::_newArray);
3784    Node* slow_result = set_results_for_java_call(slow_call);
3785    // this->control() comes from set_results_for_java_call
3786    result_reg->set_req(_slow_path, control());
3787    result_val->set_req(_slow_path, slow_result);
3788    result_io ->set_req(_slow_path, i_o());
3789    result_mem->set_req(_slow_path, reset_memory());
3790  }
3791
3792  set_control(normal_ctl);
3793  if (!stopped()) {
3794    // Normal case:  The array type has been cached in the java.lang.Class.
3795    // The following call works fine even if the array type is polymorphic.
3796    // It could be a dynamic mix of int[], boolean[], Object[], etc.
3797    Node* obj = new_array(klass_node, count_val, 0);  // no arguments to push
3798    result_reg->init_req(_normal_path, control());
3799    result_val->init_req(_normal_path, obj);
3800    result_io ->init_req(_normal_path, i_o());
3801    result_mem->init_req(_normal_path, reset_memory());
3802
3803    if (uninitialized) {
3804      // Mark the allocation so that zeroing is skipped
3805      AllocateArrayNode* alloc = AllocateArrayNode::Ideal_array_allocation(obj, &_gvn);
3806      alloc->maybe_set_complete(&_gvn);
3807    }
3808  }
3809
3810  // Return the combined state.
3811  set_i_o(        _gvn.transform(result_io)  );
3812  set_all_memory( _gvn.transform(result_mem));
3813
3814  C->set_has_split_ifs(true); // Has chance for split-if optimization
3815  set_result(result_reg, result_val);
3816  return true;
3817}
3818
3819//----------------------inline_native_getLength--------------------------
3820// public static native int java.lang.reflect.Array.getLength(Object array);
3821bool LibraryCallKit::inline_native_getLength() {
3822  if (too_many_traps(Deoptimization::Reason_intrinsic))  return false;
3823
3824  Node* array = null_check(argument(0));
3825  // If array is dead, only null-path is taken.
3826  if (stopped())  return true;
3827
3828  // Deoptimize if it is a non-array.
3829  Node* non_array = generate_non_array_guard(load_object_klass(array), NULL);
3830
3831  if (non_array != NULL) {
3832    PreserveJVMState pjvms(this);
3833    set_control(non_array);
3834    uncommon_trap(Deoptimization::Reason_intrinsic,
3835                  Deoptimization::Action_maybe_recompile);
3836  }
3837
3838  // If control is dead, only non-array-path is taken.
3839  if (stopped())  return true;
3840
3841  // The works fine even if the array type is polymorphic.
3842  // It could be a dynamic mix of int[], boolean[], Object[], etc.
3843  Node* result = load_array_length(array);
3844
3845  C->set_has_split_ifs(true);  // Has chance for split-if optimization
3846  set_result(result);
3847  return true;
3848}
3849
3850//------------------------inline_array_copyOf----------------------------
3851// public static <T,U> T[] java.util.Arrays.copyOf(     U[] original, int newLength,         Class<? extends T[]> newType);
3852// public static <T,U> T[] java.util.Arrays.copyOfRange(U[] original, int from,      int to, Class<? extends T[]> newType);
3853bool LibraryCallKit::inline_array_copyOf(bool is_copyOfRange) {
3854  if (too_many_traps(Deoptimization::Reason_intrinsic))  return false;
3855
3856  // Get the arguments.
3857  Node* original          = argument(0);
3858  Node* start             = is_copyOfRange? argument(1): intcon(0);
3859  Node* end               = is_copyOfRange? argument(2): argument(1);
3860  Node* array_type_mirror = is_copyOfRange? argument(3): argument(2);
3861
3862  Node* newcopy = NULL;
3863
3864  // Set the original stack and the reexecute bit for the interpreter to reexecute
3865  // the bytecode that invokes Arrays.copyOf if deoptimization happens.
3866  { PreserveReexecuteState preexecs(this);
3867    jvms()->set_should_reexecute(true);
3868
3869    array_type_mirror = null_check(array_type_mirror);
3870    original          = null_check(original);
3871
3872    // Check if a null path was taken unconditionally.
3873    if (stopped())  return true;
3874
3875    Node* orig_length = load_array_length(original);
3876
3877    Node* klass_node = load_klass_from_mirror(array_type_mirror, false, NULL, 0);
3878    klass_node = null_check(klass_node);
3879
3880    RegionNode* bailout = new RegionNode(1);
3881    record_for_igvn(bailout);
3882
3883    // Despite the generic type of Arrays.copyOf, the mirror might be int, int[], etc.
3884    // Bail out if that is so.
3885    Node* not_objArray = generate_non_objArray_guard(klass_node, bailout);
3886    if (not_objArray != NULL) {
3887      // Improve the klass node's type from the new optimistic assumption:
3888      ciKlass* ak = ciArrayKlass::make(env()->Object_klass());
3889      const Type* akls = TypeKlassPtr::make(TypePtr::NotNull, ak, 0/*offset*/);
3890      Node* cast = new CastPPNode(klass_node, akls);
3891      cast->init_req(0, control());
3892      klass_node = _gvn.transform(cast);
3893    }
3894
3895    // Bail out if either start or end is negative.
3896    generate_negative_guard(start, bailout, &start);
3897    generate_negative_guard(end,   bailout, &end);
3898
3899    Node* length = end;
3900    if (_gvn.type(start) != TypeInt::ZERO) {
3901      length = _gvn.transform(new SubINode(end, start));
3902    }
3903
3904    // Bail out if length is negative.
3905    // Without this the new_array would throw
3906    // NegativeArraySizeException but IllegalArgumentException is what
3907    // should be thrown
3908    generate_negative_guard(length, bailout, &length);
3909
3910    if (bailout->req() > 1) {
3911      PreserveJVMState pjvms(this);
3912      set_control(_gvn.transform(bailout));
3913      uncommon_trap(Deoptimization::Reason_intrinsic,
3914                    Deoptimization::Action_maybe_recompile);
3915    }
3916
3917    if (!stopped()) {
3918      // How many elements will we copy from the original?
3919      // The answer is MinI(orig_length - start, length).
3920      Node* orig_tail = _gvn.transform(new SubINode(orig_length, start));
3921      Node* moved = generate_min_max(vmIntrinsics::_min, orig_tail, length);
3922
3923      // Generate a direct call to the right arraycopy function(s).
3924      // We know the copy is disjoint but we might not know if the
3925      // oop stores need checking.
3926      // Extreme case:  Arrays.copyOf((Integer[])x, 10, String[].class).
3927      // This will fail a store-check if x contains any non-nulls.
3928
3929      // ArrayCopyNode:Ideal may transform the ArrayCopyNode to
3930      // loads/stores but it is legal only if we're sure the
3931      // Arrays.copyOf would succeed. So we need all input arguments
3932      // to the copyOf to be validated, including that the copy to the
3933      // new array won't trigger an ArrayStoreException. That subtype
3934      // check can be optimized if we know something on the type of
3935      // the input array from type speculation.
3936      if (_gvn.type(klass_node)->singleton()) {
3937        ciKlass* subk   = _gvn.type(load_object_klass(original))->is_klassptr()->klass();
3938        ciKlass* superk = _gvn.type(klass_node)->is_klassptr()->klass();
3939
3940        int test = C->static_subtype_check(superk, subk);
3941        if (test != Compile::SSC_always_true && test != Compile::SSC_always_false) {
3942          const TypeOopPtr* t_original = _gvn.type(original)->is_oopptr();
3943          if (t_original->speculative_type() != NULL) {
3944            original = maybe_cast_profiled_obj(original, t_original->speculative_type(), true);
3945          }
3946        }
3947      }
3948
3949      bool validated = false;
3950      // Reason_class_check rather than Reason_intrinsic because we
3951      // want to intrinsify even if this traps.
3952      if (!too_many_traps(Deoptimization::Reason_class_check)) {
3953        Node* not_subtype_ctrl = gen_subtype_check(load_object_klass(original),
3954                                                   klass_node);
3955
3956        if (not_subtype_ctrl != top()) {
3957          PreserveJVMState pjvms(this);
3958          set_control(not_subtype_ctrl);
3959          uncommon_trap(Deoptimization::Reason_class_check,
3960                        Deoptimization::Action_make_not_entrant);
3961          assert(stopped(), "Should be stopped");
3962        }
3963        validated = true;
3964      }
3965
3966      if (!stopped()) {
3967        newcopy = new_array(klass_node, length, 0);  // no arguments to push
3968
3969        ArrayCopyNode* ac = ArrayCopyNode::make(this, true, original, start, newcopy, intcon(0), moved, true,
3970                                                load_object_klass(original), klass_node);
3971        if (!is_copyOfRange) {
3972          ac->set_copyof(validated);
3973        } else {
3974          ac->set_copyofrange(validated);
3975        }
3976        Node* n = _gvn.transform(ac);
3977        if (n == ac) {
3978          ac->connect_outputs(this);
3979        } else {
3980          assert(validated, "shouldn't transform if all arguments not validated");
3981          set_all_memory(n);
3982        }
3983      }
3984    }
3985  } // original reexecute is set back here
3986
3987  C->set_has_split_ifs(true); // Has chance for split-if optimization
3988  if (!stopped()) {
3989    set_result(newcopy);
3990  }
3991  return true;
3992}
3993
3994
3995//----------------------generate_virtual_guard---------------------------
3996// Helper for hashCode and clone.  Peeks inside the vtable to avoid a call.
3997Node* LibraryCallKit::generate_virtual_guard(Node* obj_klass,
3998                                             RegionNode* slow_region) {
3999  ciMethod* method = callee();
4000  int vtable_index = method->vtable_index();
4001  assert(vtable_index >= 0 || vtable_index == Method::nonvirtual_vtable_index,
4002         "bad index %d", vtable_index);
4003  // Get the Method* out of the appropriate vtable entry.
4004  int entry_offset  = in_bytes(Klass::vtable_start_offset()) +
4005                     vtable_index*vtableEntry::size_in_bytes() +
4006                     vtableEntry::method_offset_in_bytes();
4007  Node* entry_addr  = basic_plus_adr(obj_klass, entry_offset);
4008  Node* target_call = make_load(NULL, entry_addr, TypePtr::NOTNULL, T_ADDRESS, MemNode::unordered);
4009
4010  // Compare the target method with the expected method (e.g., Object.hashCode).
4011  const TypePtr* native_call_addr = TypeMetadataPtr::make(method);
4012
4013  Node* native_call = makecon(native_call_addr);
4014  Node* chk_native  = _gvn.transform(new CmpPNode(target_call, native_call));
4015  Node* test_native = _gvn.transform(new BoolNode(chk_native, BoolTest::ne));
4016
4017  return generate_slow_guard(test_native, slow_region);
4018}
4019
4020//-----------------------generate_method_call----------------------------
4021// Use generate_method_call to make a slow-call to the real
4022// method if the fast path fails.  An alternative would be to
4023// use a stub like OptoRuntime::slow_arraycopy_Java.
4024// This only works for expanding the current library call,
4025// not another intrinsic.  (E.g., don't use this for making an
4026// arraycopy call inside of the copyOf intrinsic.)
4027CallJavaNode*
4028LibraryCallKit::generate_method_call(vmIntrinsics::ID method_id, bool is_virtual, bool is_static) {
4029  // When compiling the intrinsic method itself, do not use this technique.
4030  guarantee(callee() != C->method(), "cannot make slow-call to self");
4031
4032  ciMethod* method = callee();
4033  // ensure the JVMS we have will be correct for this call
4034  guarantee(method_id == method->intrinsic_id(), "must match");
4035
4036  const TypeFunc* tf = TypeFunc::make(method);
4037  CallJavaNode* slow_call;
4038  if (is_static) {
4039    assert(!is_virtual, "");
4040    slow_call = new CallStaticJavaNode(C, tf,
4041                           SharedRuntime::get_resolve_static_call_stub(),
4042                           method, bci());
4043  } else if (is_virtual) {
4044    null_check_receiver();
4045    int vtable_index = Method::invalid_vtable_index;
4046    if (UseInlineCaches) {
4047      // Suppress the vtable call
4048    } else {
4049      // hashCode and clone are not a miranda methods,
4050      // so the vtable index is fixed.
4051      // No need to use the linkResolver to get it.
4052       vtable_index = method->vtable_index();
4053       assert(vtable_index >= 0 || vtable_index == Method::nonvirtual_vtable_index,
4054              "bad index %d", vtable_index);
4055    }
4056    slow_call = new CallDynamicJavaNode(tf,
4057                          SharedRuntime::get_resolve_virtual_call_stub(),
4058                          method, vtable_index, bci());
4059  } else {  // neither virtual nor static:  opt_virtual
4060    null_check_receiver();
4061    slow_call = new CallStaticJavaNode(C, tf,
4062                                SharedRuntime::get_resolve_opt_virtual_call_stub(),
4063                                method, bci());
4064    slow_call->set_optimized_virtual(true);
4065  }
4066  set_arguments_for_java_call(slow_call);
4067  set_edges_for_java_call(slow_call);
4068  return slow_call;
4069}
4070
4071
4072/**
4073 * Build special case code for calls to hashCode on an object. This call may
4074 * be virtual (invokevirtual) or bound (invokespecial). For each case we generate
4075 * slightly different code.
4076 */
4077bool LibraryCallKit::inline_native_hashcode(bool is_virtual, bool is_static) {
4078  assert(is_static == callee()->is_static(), "correct intrinsic selection");
4079  assert(!(is_virtual && is_static), "either virtual, special, or static");
4080
4081  enum { _slow_path = 1, _fast_path, _null_path, PATH_LIMIT };
4082
4083  RegionNode* result_reg = new RegionNode(PATH_LIMIT);
4084  PhiNode*    result_val = new PhiNode(result_reg, TypeInt::INT);
4085  PhiNode*    result_io  = new PhiNode(result_reg, Type::ABIO);
4086  PhiNode*    result_mem = new PhiNode(result_reg, Type::MEMORY, TypePtr::BOTTOM);
4087  Node* obj = NULL;
4088  if (!is_static) {
4089    // Check for hashing null object
4090    obj = null_check_receiver();
4091    if (stopped())  return true;        // unconditionally null
4092    result_reg->init_req(_null_path, top());
4093    result_val->init_req(_null_path, top());
4094  } else {
4095    // Do a null check, and return zero if null.
4096    // System.identityHashCode(null) == 0
4097    obj = argument(0);
4098    Node* null_ctl = top();
4099    obj = null_check_oop(obj, &null_ctl);
4100    result_reg->init_req(_null_path, null_ctl);
4101    result_val->init_req(_null_path, _gvn.intcon(0));
4102  }
4103
4104  // Unconditionally null?  Then return right away.
4105  if (stopped()) {
4106    set_control( result_reg->in(_null_path));
4107    if (!stopped())
4108      set_result(result_val->in(_null_path));
4109    return true;
4110  }
4111
4112  // We only go to the fast case code if we pass a number of guards.  The
4113  // paths which do not pass are accumulated in the slow_region.
4114  RegionNode* slow_region = new RegionNode(1);
4115  record_for_igvn(slow_region);
4116
4117  // If this is a virtual call, we generate a funny guard.  We pull out
4118  // the vtable entry corresponding to hashCode() from the target object.
4119  // If the target method which we are calling happens to be the native
4120  // Object hashCode() method, we pass the guard.  We do not need this
4121  // guard for non-virtual calls -- the caller is known to be the native
4122  // Object hashCode().
4123  if (is_virtual) {
4124    // After null check, get the object's klass.
4125    Node* obj_klass = load_object_klass(obj);
4126    generate_virtual_guard(obj_klass, slow_region);
4127  }
4128
4129  // Get the header out of the object, use LoadMarkNode when available
4130  Node* header_addr = basic_plus_adr(obj, oopDesc::mark_offset_in_bytes());
4131  // The control of the load must be NULL. Otherwise, the load can move before
4132  // the null check after castPP removal.
4133  Node* no_ctrl = NULL;
4134  Node* header = make_load(no_ctrl, header_addr, TypeX_X, TypeX_X->basic_type(), MemNode::unordered);
4135
4136  // Test the header to see if it is unlocked.
4137  Node *lock_mask      = _gvn.MakeConX(markOopDesc::biased_lock_mask_in_place);
4138  Node *lmasked_header = _gvn.transform(new AndXNode(header, lock_mask));
4139  Node *unlocked_val   = _gvn.MakeConX(markOopDesc::unlocked_value);
4140  Node *chk_unlocked   = _gvn.transform(new CmpXNode( lmasked_header, unlocked_val));
4141  Node *test_unlocked  = _gvn.transform(new BoolNode( chk_unlocked, BoolTest::ne));
4142
4143  generate_slow_guard(test_unlocked, slow_region);
4144
4145  // Get the hash value and check to see that it has been properly assigned.
4146  // We depend on hash_mask being at most 32 bits and avoid the use of
4147  // hash_mask_in_place because it could be larger than 32 bits in a 64-bit
4148  // vm: see markOop.hpp.
4149  Node *hash_mask      = _gvn.intcon(markOopDesc::hash_mask);
4150  Node *hash_shift     = _gvn.intcon(markOopDesc::hash_shift);
4151  Node *hshifted_header= _gvn.transform(new URShiftXNode(header, hash_shift));
4152  // This hack lets the hash bits live anywhere in the mark object now, as long
4153  // as the shift drops the relevant bits into the low 32 bits.  Note that
4154  // Java spec says that HashCode is an int so there's no point in capturing
4155  // an 'X'-sized hashcode (32 in 32-bit build or 64 in 64-bit build).
4156  hshifted_header      = ConvX2I(hshifted_header);
4157  Node *hash_val       = _gvn.transform(new AndINode(hshifted_header, hash_mask));
4158
4159  Node *no_hash_val    = _gvn.intcon(markOopDesc::no_hash);
4160  Node *chk_assigned   = _gvn.transform(new CmpINode( hash_val, no_hash_val));
4161  Node *test_assigned  = _gvn.transform(new BoolNode( chk_assigned, BoolTest::eq));
4162
4163  generate_slow_guard(test_assigned, slow_region);
4164
4165  Node* init_mem = reset_memory();
4166  // fill in the rest of the null path:
4167  result_io ->init_req(_null_path, i_o());
4168  result_mem->init_req(_null_path, init_mem);
4169
4170  result_val->init_req(_fast_path, hash_val);
4171  result_reg->init_req(_fast_path, control());
4172  result_io ->init_req(_fast_path, i_o());
4173  result_mem->init_req(_fast_path, init_mem);
4174
4175  // Generate code for the slow case.  We make a call to hashCode().
4176  set_control(_gvn.transform(slow_region));
4177  if (!stopped()) {
4178    // No need for PreserveJVMState, because we're using up the present state.
4179    set_all_memory(init_mem);
4180    vmIntrinsics::ID hashCode_id = is_static ? vmIntrinsics::_identityHashCode : vmIntrinsics::_hashCode;
4181    CallJavaNode* slow_call = generate_method_call(hashCode_id, is_virtual, is_static);
4182    Node* slow_result = set_results_for_java_call(slow_call);
4183    // this->control() comes from set_results_for_java_call
4184    result_reg->init_req(_slow_path, control());
4185    result_val->init_req(_slow_path, slow_result);
4186    result_io  ->set_req(_slow_path, i_o());
4187    result_mem ->set_req(_slow_path, reset_memory());
4188  }
4189
4190  // Return the combined state.
4191  set_i_o(        _gvn.transform(result_io)  );
4192  set_all_memory( _gvn.transform(result_mem));
4193
4194  set_result(result_reg, result_val);
4195  return true;
4196}
4197
4198//---------------------------inline_native_getClass----------------------------
4199// public final native Class<?> java.lang.Object.getClass();
4200//
4201// Build special case code for calls to getClass on an object.
4202bool LibraryCallKit::inline_native_getClass() {
4203  Node* obj = null_check_receiver();
4204  if (stopped())  return true;
4205  set_result(load_mirror_from_klass(load_object_klass(obj)));
4206  return true;
4207}
4208
4209//-----------------inline_native_Reflection_getCallerClass---------------------
4210// public static native Class<?> sun.reflect.Reflection.getCallerClass();
4211//
4212// In the presence of deep enough inlining, getCallerClass() becomes a no-op.
4213//
4214// NOTE: This code must perform the same logic as JVM_GetCallerClass
4215// in that it must skip particular security frames and checks for
4216// caller sensitive methods.
4217bool LibraryCallKit::inline_native_Reflection_getCallerClass() {
4218#ifndef PRODUCT
4219  if ((C->print_intrinsics() || C->print_inlining()) && Verbose) {
4220    tty->print_cr("Attempting to inline sun.reflect.Reflection.getCallerClass");
4221  }
4222#endif
4223
4224  if (!jvms()->has_method()) {
4225#ifndef PRODUCT
4226    if ((C->print_intrinsics() || C->print_inlining()) && Verbose) {
4227      tty->print_cr("  Bailing out because intrinsic was inlined at top level");
4228    }
4229#endif
4230    return false;
4231  }
4232
4233  // Walk back up the JVM state to find the caller at the required
4234  // depth.
4235  JVMState* caller_jvms = jvms();
4236
4237  // Cf. JVM_GetCallerClass
4238  // NOTE: Start the loop at depth 1 because the current JVM state does
4239  // not include the Reflection.getCallerClass() frame.
4240  for (int n = 1; caller_jvms != NULL; caller_jvms = caller_jvms->caller(), n++) {
4241    ciMethod* m = caller_jvms->method();
4242    switch (n) {
4243    case 0:
4244      fatal("current JVM state does not include the Reflection.getCallerClass frame");
4245      break;
4246    case 1:
4247      // Frame 0 and 1 must be caller sensitive (see JVM_GetCallerClass).
4248      if (!m->caller_sensitive()) {
4249#ifndef PRODUCT
4250        if ((C->print_intrinsics() || C->print_inlining()) && Verbose) {
4251          tty->print_cr("  Bailing out: CallerSensitive annotation expected at frame %d", n);
4252        }
4253#endif
4254        return false;  // bail-out; let JVM_GetCallerClass do the work
4255      }
4256      break;
4257    default:
4258      if (!m->is_ignored_by_security_stack_walk()) {
4259        // We have reached the desired frame; return the holder class.
4260        // Acquire method holder as java.lang.Class and push as constant.
4261        ciInstanceKlass* caller_klass = caller_jvms->method()->holder();
4262        ciInstance* caller_mirror = caller_klass->java_mirror();
4263        set_result(makecon(TypeInstPtr::make(caller_mirror)));
4264
4265#ifndef PRODUCT
4266        if ((C->print_intrinsics() || C->print_inlining()) && Verbose) {
4267          tty->print_cr("  Succeeded: caller = %d) %s.%s, JVMS depth = %d", n, caller_klass->name()->as_utf8(), caller_jvms->method()->name()->as_utf8(), jvms()->depth());
4268          tty->print_cr("  JVM state at this point:");
4269          for (int i = jvms()->depth(), n = 1; i >= 1; i--, n++) {
4270            ciMethod* m = jvms()->of_depth(i)->method();
4271            tty->print_cr("   %d) %s.%s", n, m->holder()->name()->as_utf8(), m->name()->as_utf8());
4272          }
4273        }
4274#endif
4275        return true;
4276      }
4277      break;
4278    }
4279  }
4280
4281#ifndef PRODUCT
4282  if ((C->print_intrinsics() || C->print_inlining()) && Verbose) {
4283    tty->print_cr("  Bailing out because caller depth exceeded inlining depth = %d", jvms()->depth());
4284    tty->print_cr("  JVM state at this point:");
4285    for (int i = jvms()->depth(), n = 1; i >= 1; i--, n++) {
4286      ciMethod* m = jvms()->of_depth(i)->method();
4287      tty->print_cr("   %d) %s.%s", n, m->holder()->name()->as_utf8(), m->name()->as_utf8());
4288    }
4289  }
4290#endif
4291
4292  return false;  // bail-out; let JVM_GetCallerClass do the work
4293}
4294
4295bool LibraryCallKit::inline_fp_conversions(vmIntrinsics::ID id) {
4296  Node* arg = argument(0);
4297  Node* result = NULL;
4298
4299  switch (id) {
4300  case vmIntrinsics::_floatToRawIntBits:    result = new MoveF2INode(arg);  break;
4301  case vmIntrinsics::_intBitsToFloat:       result = new MoveI2FNode(arg);  break;
4302  case vmIntrinsics::_doubleToRawLongBits:  result = new MoveD2LNode(arg);  break;
4303  case vmIntrinsics::_longBitsToDouble:     result = new MoveL2DNode(arg);  break;
4304
4305  case vmIntrinsics::_doubleToLongBits: {
4306    // two paths (plus control) merge in a wood
4307    RegionNode *r = new RegionNode(3);
4308    Node *phi = new PhiNode(r, TypeLong::LONG);
4309
4310    Node *cmpisnan = _gvn.transform(new CmpDNode(arg, arg));
4311    // Build the boolean node
4312    Node *bolisnan = _gvn.transform(new BoolNode(cmpisnan, BoolTest::ne));
4313
4314    // Branch either way.
4315    // NaN case is less traveled, which makes all the difference.
4316    IfNode *ifisnan = create_and_xform_if(control(), bolisnan, PROB_STATIC_FREQUENT, COUNT_UNKNOWN);
4317    Node *opt_isnan = _gvn.transform(ifisnan);
4318    assert( opt_isnan->is_If(), "Expect an IfNode");
4319    IfNode *opt_ifisnan = (IfNode*)opt_isnan;
4320    Node *iftrue = _gvn.transform(new IfTrueNode(opt_ifisnan));
4321
4322    set_control(iftrue);
4323
4324    static const jlong nan_bits = CONST64(0x7ff8000000000000);
4325    Node *slow_result = longcon(nan_bits); // return NaN
4326    phi->init_req(1, _gvn.transform( slow_result ));
4327    r->init_req(1, iftrue);
4328
4329    // Else fall through
4330    Node *iffalse = _gvn.transform(new IfFalseNode(opt_ifisnan));
4331    set_control(iffalse);
4332
4333    phi->init_req(2, _gvn.transform(new MoveD2LNode(arg)));
4334    r->init_req(2, iffalse);
4335
4336    // Post merge
4337    set_control(_gvn.transform(r));
4338    record_for_igvn(r);
4339
4340    C->set_has_split_ifs(true); // Has chance for split-if optimization
4341    result = phi;
4342    assert(result->bottom_type()->isa_long(), "must be");
4343    break;
4344  }
4345
4346  case vmIntrinsics::_floatToIntBits: {
4347    // two paths (plus control) merge in a wood
4348    RegionNode *r = new RegionNode(3);
4349    Node *phi = new PhiNode(r, TypeInt::INT);
4350
4351    Node *cmpisnan = _gvn.transform(new CmpFNode(arg, arg));
4352    // Build the boolean node
4353    Node *bolisnan = _gvn.transform(new BoolNode(cmpisnan, BoolTest::ne));
4354
4355    // Branch either way.
4356    // NaN case is less traveled, which makes all the difference.
4357    IfNode *ifisnan = create_and_xform_if(control(), bolisnan, PROB_STATIC_FREQUENT, COUNT_UNKNOWN);
4358    Node *opt_isnan = _gvn.transform(ifisnan);
4359    assert( opt_isnan->is_If(), "Expect an IfNode");
4360    IfNode *opt_ifisnan = (IfNode*)opt_isnan;
4361    Node *iftrue = _gvn.transform(new IfTrueNode(opt_ifisnan));
4362
4363    set_control(iftrue);
4364
4365    static const jint nan_bits = 0x7fc00000;
4366    Node *slow_result = makecon(TypeInt::make(nan_bits)); // return NaN
4367    phi->init_req(1, _gvn.transform( slow_result ));
4368    r->init_req(1, iftrue);
4369
4370    // Else fall through
4371    Node *iffalse = _gvn.transform(new IfFalseNode(opt_ifisnan));
4372    set_control(iffalse);
4373
4374    phi->init_req(2, _gvn.transform(new MoveF2INode(arg)));
4375    r->init_req(2, iffalse);
4376
4377    // Post merge
4378    set_control(_gvn.transform(r));
4379    record_for_igvn(r);
4380
4381    C->set_has_split_ifs(true); // Has chance for split-if optimization
4382    result = phi;
4383    assert(result->bottom_type()->isa_int(), "must be");
4384    break;
4385  }
4386
4387  default:
4388    fatal_unexpected_iid(id);
4389    break;
4390  }
4391  set_result(_gvn.transform(result));
4392  return true;
4393}
4394
4395//----------------------inline_unsafe_copyMemory-------------------------
4396// public native void Unsafe.copyMemory0(Object srcBase, long srcOffset, Object destBase, long destOffset, long bytes);
4397bool LibraryCallKit::inline_unsafe_copyMemory() {
4398  if (callee()->is_static())  return false;  // caller must have the capability!
4399  null_check_receiver();  // null-check receiver
4400  if (stopped())  return true;
4401
4402  C->set_has_unsafe_access(true);  // Mark eventual nmethod as "unsafe".
4403
4404  Node* src_ptr =         argument(1);   // type: oop
4405  Node* src_off = ConvL2X(argument(2));  // type: long
4406  Node* dst_ptr =         argument(4);   // type: oop
4407  Node* dst_off = ConvL2X(argument(5));  // type: long
4408  Node* size    = ConvL2X(argument(7));  // type: long
4409
4410  assert(Unsafe_field_offset_to_byte_offset(11) == 11,
4411         "fieldOffset must be byte-scaled");
4412
4413  Node* src = make_unsafe_address(src_ptr, src_off);
4414  Node* dst = make_unsafe_address(dst_ptr, dst_off);
4415
4416  // Conservatively insert a memory barrier on all memory slices.
4417  // Do not let writes of the copy source or destination float below the copy.
4418  insert_mem_bar(Op_MemBarCPUOrder);
4419
4420  // Call it.  Note that the length argument is not scaled.
4421  make_runtime_call(RC_LEAF|RC_NO_FP,
4422                    OptoRuntime::fast_arraycopy_Type(),
4423                    StubRoutines::unsafe_arraycopy(),
4424                    "unsafe_arraycopy",
4425                    TypeRawPtr::BOTTOM,
4426                    src, dst, size XTOP);
4427
4428  // Do not let reads of the copy destination float above the copy.
4429  insert_mem_bar(Op_MemBarCPUOrder);
4430
4431  return true;
4432}
4433
4434//------------------------clone_coping-----------------------------------
4435// Helper function for inline_native_clone.
4436void LibraryCallKit::copy_to_clone(Node* obj, Node* alloc_obj, Node* obj_size, bool is_array, bool card_mark) {
4437  assert(obj_size != NULL, "");
4438  Node* raw_obj = alloc_obj->in(1);
4439  assert(alloc_obj->is_CheckCastPP() && raw_obj->is_Proj() && raw_obj->in(0)->is_Allocate(), "");
4440
4441  AllocateNode* alloc = NULL;
4442  if (ReduceBulkZeroing) {
4443    // We will be completely responsible for initializing this object -
4444    // mark Initialize node as complete.
4445    alloc = AllocateNode::Ideal_allocation(alloc_obj, &_gvn);
4446    // The object was just allocated - there should be no any stores!
4447    guarantee(alloc != NULL && alloc->maybe_set_complete(&_gvn), "");
4448    // Mark as complete_with_arraycopy so that on AllocateNode
4449    // expansion, we know this AllocateNode is initialized by an array
4450    // copy and a StoreStore barrier exists after the array copy.
4451    alloc->initialization()->set_complete_with_arraycopy();
4452  }
4453
4454  // Copy the fastest available way.
4455  // TODO: generate fields copies for small objects instead.
4456  Node* src  = obj;
4457  Node* dest = alloc_obj;
4458  Node* size = _gvn.transform(obj_size);
4459
4460  // Exclude the header but include array length to copy by 8 bytes words.
4461  // Can't use base_offset_in_bytes(bt) since basic type is unknown.
4462  int base_off = is_array ? arrayOopDesc::length_offset_in_bytes() :
4463                            instanceOopDesc::base_offset_in_bytes();
4464  // base_off:
4465  // 8  - 32-bit VM
4466  // 12 - 64-bit VM, compressed klass
4467  // 16 - 64-bit VM, normal klass
4468  if (base_off % BytesPerLong != 0) {
4469    assert(UseCompressedClassPointers, "");
4470    if (is_array) {
4471      // Exclude length to copy by 8 bytes words.
4472      base_off += sizeof(int);
4473    } else {
4474      // Include klass to copy by 8 bytes words.
4475      base_off = instanceOopDesc::klass_offset_in_bytes();
4476    }
4477    assert(base_off % BytesPerLong == 0, "expect 8 bytes alignment");
4478  }
4479  src  = basic_plus_adr(src,  base_off);
4480  dest = basic_plus_adr(dest, base_off);
4481
4482  // Compute the length also, if needed:
4483  Node* countx = size;
4484  countx = _gvn.transform(new SubXNode(countx, MakeConX(base_off)));
4485  countx = _gvn.transform(new URShiftXNode(countx, intcon(LogBytesPerLong) ));
4486
4487  const TypePtr* raw_adr_type = TypeRawPtr::BOTTOM;
4488
4489  ArrayCopyNode* ac = ArrayCopyNode::make(this, false, src, NULL, dest, NULL, countx, false);
4490  ac->set_clonebasic();
4491  Node* n = _gvn.transform(ac);
4492  if (n == ac) {
4493    set_predefined_output_for_runtime_call(ac, ac->in(TypeFunc::Memory), raw_adr_type);
4494  } else {
4495    set_all_memory(n);
4496  }
4497
4498  // If necessary, emit some card marks afterwards.  (Non-arrays only.)
4499  if (card_mark) {
4500    assert(!is_array, "");
4501    // Put in store barrier for any and all oops we are sticking
4502    // into this object.  (We could avoid this if we could prove
4503    // that the object type contains no oop fields at all.)
4504    Node* no_particular_value = NULL;
4505    Node* no_particular_field = NULL;
4506    int raw_adr_idx = Compile::AliasIdxRaw;
4507    post_barrier(control(),
4508                 memory(raw_adr_type),
4509                 alloc_obj,
4510                 no_particular_field,
4511                 raw_adr_idx,
4512                 no_particular_value,
4513                 T_OBJECT,
4514                 false);
4515  }
4516
4517  // Do not let reads from the cloned object float above the arraycopy.
4518  if (alloc != NULL) {
4519    // Do not let stores that initialize this object be reordered with
4520    // a subsequent store that would make this object accessible by
4521    // other threads.
4522    // Record what AllocateNode this StoreStore protects so that
4523    // escape analysis can go from the MemBarStoreStoreNode to the
4524    // AllocateNode and eliminate the MemBarStoreStoreNode if possible
4525    // based on the escape status of the AllocateNode.
4526    insert_mem_bar(Op_MemBarStoreStore, alloc->proj_out(AllocateNode::RawAddress));
4527  } else {
4528    insert_mem_bar(Op_MemBarCPUOrder);
4529  }
4530}
4531
4532//------------------------inline_native_clone----------------------------
4533// protected native Object java.lang.Object.clone();
4534//
4535// Here are the simple edge cases:
4536//  null receiver => normal trap
4537//  virtual and clone was overridden => slow path to out-of-line clone
4538//  not cloneable or finalizer => slow path to out-of-line Object.clone
4539//
4540// The general case has two steps, allocation and copying.
4541// Allocation has two cases, and uses GraphKit::new_instance or new_array.
4542//
4543// Copying also has two cases, oop arrays and everything else.
4544// Oop arrays use arrayof_oop_arraycopy (same as System.arraycopy).
4545// Everything else uses the tight inline loop supplied by CopyArrayNode.
4546//
4547// These steps fold up nicely if and when the cloned object's klass
4548// can be sharply typed as an object array, a type array, or an instance.
4549//
4550bool LibraryCallKit::inline_native_clone(bool is_virtual) {
4551  PhiNode* result_val;
4552
4553  // Set the reexecute bit for the interpreter to reexecute
4554  // the bytecode that invokes Object.clone if deoptimization happens.
4555  { PreserveReexecuteState preexecs(this);
4556    jvms()->set_should_reexecute(true);
4557
4558    Node* obj = null_check_receiver();
4559    if (stopped())  return true;
4560
4561    const TypeOopPtr* obj_type = _gvn.type(obj)->is_oopptr();
4562
4563    // If we are going to clone an instance, we need its exact type to
4564    // know the number and types of fields to convert the clone to
4565    // loads/stores. Maybe a speculative type can help us.
4566    if (!obj_type->klass_is_exact() &&
4567        obj_type->speculative_type() != NULL &&
4568        obj_type->speculative_type()->is_instance_klass()) {
4569      ciInstanceKlass* spec_ik = obj_type->speculative_type()->as_instance_klass();
4570      if (spec_ik->nof_nonstatic_fields() <= ArrayCopyLoadStoreMaxElem &&
4571          !spec_ik->has_injected_fields()) {
4572        ciKlass* k = obj_type->klass();
4573        if (!k->is_instance_klass() ||
4574            k->as_instance_klass()->is_interface() ||
4575            k->as_instance_klass()->has_subklass()) {
4576          obj = maybe_cast_profiled_obj(obj, obj_type->speculative_type(), false);
4577        }
4578      }
4579    }
4580
4581    Node* obj_klass = load_object_klass(obj);
4582    const TypeKlassPtr* tklass = _gvn.type(obj_klass)->isa_klassptr();
4583    const TypeOopPtr*   toop   = ((tklass != NULL)
4584                                ? tklass->as_instance_type()
4585                                : TypeInstPtr::NOTNULL);
4586
4587    // Conservatively insert a memory barrier on all memory slices.
4588    // Do not let writes into the original float below the clone.
4589    insert_mem_bar(Op_MemBarCPUOrder);
4590
4591    // paths into result_reg:
4592    enum {
4593      _slow_path = 1,     // out-of-line call to clone method (virtual or not)
4594      _objArray_path,     // plain array allocation, plus arrayof_oop_arraycopy
4595      _array_path,        // plain array allocation, plus arrayof_long_arraycopy
4596      _instance_path,     // plain instance allocation, plus arrayof_long_arraycopy
4597      PATH_LIMIT
4598    };
4599    RegionNode* result_reg = new RegionNode(PATH_LIMIT);
4600    result_val             = new PhiNode(result_reg, TypeInstPtr::NOTNULL);
4601    PhiNode*    result_i_o = new PhiNode(result_reg, Type::ABIO);
4602    PhiNode*    result_mem = new PhiNode(result_reg, Type::MEMORY, TypePtr::BOTTOM);
4603    record_for_igvn(result_reg);
4604
4605    const TypePtr* raw_adr_type = TypeRawPtr::BOTTOM;
4606    int raw_adr_idx = Compile::AliasIdxRaw;
4607
4608    Node* array_ctl = generate_array_guard(obj_klass, (RegionNode*)NULL);
4609    if (array_ctl != NULL) {
4610      // It's an array.
4611      PreserveJVMState pjvms(this);
4612      set_control(array_ctl);
4613      Node* obj_length = load_array_length(obj);
4614      Node* obj_size  = NULL;
4615      Node* alloc_obj = new_array(obj_klass, obj_length, 0, &obj_size);  // no arguments to push
4616
4617      if (!use_ReduceInitialCardMarks()) {
4618        // If it is an oop array, it requires very special treatment,
4619        // because card marking is required on each card of the array.
4620        Node* is_obja = generate_objArray_guard(obj_klass, (RegionNode*)NULL);
4621        if (is_obja != NULL) {
4622          PreserveJVMState pjvms2(this);
4623          set_control(is_obja);
4624          // Generate a direct call to the right arraycopy function(s).
4625          Node* alloc = tightly_coupled_allocation(alloc_obj, NULL);
4626          ArrayCopyNode* ac = ArrayCopyNode::make(this, true, obj, intcon(0), alloc_obj, intcon(0), obj_length, alloc != NULL);
4627          ac->set_cloneoop();
4628          Node* n = _gvn.transform(ac);
4629          assert(n == ac, "cannot disappear");
4630          ac->connect_outputs(this);
4631
4632          result_reg->init_req(_objArray_path, control());
4633          result_val->init_req(_objArray_path, alloc_obj);
4634          result_i_o ->set_req(_objArray_path, i_o());
4635          result_mem ->set_req(_objArray_path, reset_memory());
4636        }
4637      }
4638      // Otherwise, there are no card marks to worry about.
4639      // (We can dispense with card marks if we know the allocation
4640      //  comes out of eden (TLAB)...  In fact, ReduceInitialCardMarks
4641      //  causes the non-eden paths to take compensating steps to
4642      //  simulate a fresh allocation, so that no further
4643      //  card marks are required in compiled code to initialize
4644      //  the object.)
4645
4646      if (!stopped()) {
4647        copy_to_clone(obj, alloc_obj, obj_size, true, false);
4648
4649        // Present the results of the copy.
4650        result_reg->init_req(_array_path, control());
4651        result_val->init_req(_array_path, alloc_obj);
4652        result_i_o ->set_req(_array_path, i_o());
4653        result_mem ->set_req(_array_path, reset_memory());
4654      }
4655    }
4656
4657    // We only go to the instance fast case code if we pass a number of guards.
4658    // The paths which do not pass are accumulated in the slow_region.
4659    RegionNode* slow_region = new RegionNode(1);
4660    record_for_igvn(slow_region);
4661    if (!stopped()) {
4662      // It's an instance (we did array above).  Make the slow-path tests.
4663      // If this is a virtual call, we generate a funny guard.  We grab
4664      // the vtable entry corresponding to clone() from the target object.
4665      // If the target method which we are calling happens to be the
4666      // Object clone() method, we pass the guard.  We do not need this
4667      // guard for non-virtual calls; the caller is known to be the native
4668      // Object clone().
4669      if (is_virtual) {
4670        generate_virtual_guard(obj_klass, slow_region);
4671      }
4672
4673      // The object must be easily cloneable and must not have a finalizer.
4674      // Both of these conditions may be checked in a single test.
4675      // We could optimize the test further, but we don't care.
4676      generate_access_flags_guard(obj_klass,
4677                                  // Test both conditions:
4678                                  JVM_ACC_IS_CLONEABLE_FAST | JVM_ACC_HAS_FINALIZER,
4679                                  // Must be cloneable but not finalizer:
4680                                  JVM_ACC_IS_CLONEABLE_FAST,
4681                                  slow_region);
4682    }
4683
4684    if (!stopped()) {
4685      // It's an instance, and it passed the slow-path tests.
4686      PreserveJVMState pjvms(this);
4687      Node* obj_size  = NULL;
4688      // Need to deoptimize on exception from allocation since Object.clone intrinsic
4689      // is reexecuted if deoptimization occurs and there could be problems when merging
4690      // exception state between multiple Object.clone versions (reexecute=true vs reexecute=false).
4691      Node* alloc_obj = new_instance(obj_klass, NULL, &obj_size, /*deoptimize_on_exception=*/true);
4692
4693      copy_to_clone(obj, alloc_obj, obj_size, false, !use_ReduceInitialCardMarks());
4694
4695      // Present the results of the slow call.
4696      result_reg->init_req(_instance_path, control());
4697      result_val->init_req(_instance_path, alloc_obj);
4698      result_i_o ->set_req(_instance_path, i_o());
4699      result_mem ->set_req(_instance_path, reset_memory());
4700    }
4701
4702    // Generate code for the slow case.  We make a call to clone().
4703    set_control(_gvn.transform(slow_region));
4704    if (!stopped()) {
4705      PreserveJVMState pjvms(this);
4706      CallJavaNode* slow_call = generate_method_call(vmIntrinsics::_clone, is_virtual);
4707      Node* slow_result = set_results_for_java_call(slow_call);
4708      // this->control() comes from set_results_for_java_call
4709      result_reg->init_req(_slow_path, control());
4710      result_val->init_req(_slow_path, slow_result);
4711      result_i_o ->set_req(_slow_path, i_o());
4712      result_mem ->set_req(_slow_path, reset_memory());
4713    }
4714
4715    // Return the combined state.
4716    set_control(    _gvn.transform(result_reg));
4717    set_i_o(        _gvn.transform(result_i_o));
4718    set_all_memory( _gvn.transform(result_mem));
4719  } // original reexecute is set back here
4720
4721  set_result(_gvn.transform(result_val));
4722  return true;
4723}
4724
4725// If we have a tighly coupled allocation, the arraycopy may take care
4726// of the array initialization. If one of the guards we insert between
4727// the allocation and the arraycopy causes a deoptimization, an
4728// unitialized array will escape the compiled method. To prevent that
4729// we set the JVM state for uncommon traps between the allocation and
4730// the arraycopy to the state before the allocation so, in case of
4731// deoptimization, we'll reexecute the allocation and the
4732// initialization.
4733JVMState* LibraryCallKit::arraycopy_restore_alloc_state(AllocateArrayNode* alloc, int& saved_reexecute_sp) {
4734  if (alloc != NULL) {
4735    ciMethod* trap_method = alloc->jvms()->method();
4736    int trap_bci = alloc->jvms()->bci();
4737
4738    if (!C->too_many_traps(trap_method, trap_bci, Deoptimization::Reason_intrinsic) &
4739          !C->too_many_traps(trap_method, trap_bci, Deoptimization::Reason_null_check)) {
4740      // Make sure there's no store between the allocation and the
4741      // arraycopy otherwise visible side effects could be rexecuted
4742      // in case of deoptimization and cause incorrect execution.
4743      bool no_interfering_store = true;
4744      Node* mem = alloc->in(TypeFunc::Memory);
4745      if (mem->is_MergeMem()) {
4746        for (MergeMemStream mms(merged_memory(), mem->as_MergeMem()); mms.next_non_empty2(); ) {
4747          Node* n = mms.memory();
4748          if (n != mms.memory2() && !(n->is_Proj() && n->in(0) == alloc->initialization())) {
4749            assert(n->is_Store(), "what else?");
4750            no_interfering_store = false;
4751            break;
4752          }
4753        }
4754      } else {
4755        for (MergeMemStream mms(merged_memory()); mms.next_non_empty(); ) {
4756          Node* n = mms.memory();
4757          if (n != mem && !(n->is_Proj() && n->in(0) == alloc->initialization())) {
4758            assert(n->is_Store(), "what else?");
4759            no_interfering_store = false;
4760            break;
4761          }
4762        }
4763      }
4764
4765      if (no_interfering_store) {
4766        JVMState* old_jvms = alloc->jvms()->clone_shallow(C);
4767        uint size = alloc->req();
4768        SafePointNode* sfpt = new SafePointNode(size, old_jvms);
4769        old_jvms->set_map(sfpt);
4770        for (uint i = 0; i < size; i++) {
4771          sfpt->init_req(i, alloc->in(i));
4772        }
4773        // re-push array length for deoptimization
4774        sfpt->ins_req(old_jvms->stkoff() + old_jvms->sp(), alloc->in(AllocateNode::ALength));
4775        old_jvms->set_sp(old_jvms->sp()+1);
4776        old_jvms->set_monoff(old_jvms->monoff()+1);
4777        old_jvms->set_scloff(old_jvms->scloff()+1);
4778        old_jvms->set_endoff(old_jvms->endoff()+1);
4779        old_jvms->set_should_reexecute(true);
4780
4781        sfpt->set_i_o(map()->i_o());
4782        sfpt->set_memory(map()->memory());
4783        sfpt->set_control(map()->control());
4784
4785        JVMState* saved_jvms = jvms();
4786        saved_reexecute_sp = _reexecute_sp;
4787
4788        set_jvms(sfpt->jvms());
4789        _reexecute_sp = jvms()->sp();
4790
4791        return saved_jvms;
4792      }
4793    }
4794  }
4795  return NULL;
4796}
4797
4798// In case of a deoptimization, we restart execution at the
4799// allocation, allocating a new array. We would leave an uninitialized
4800// array in the heap that GCs wouldn't expect. Move the allocation
4801// after the traps so we don't allocate the array if we
4802// deoptimize. This is possible because tightly_coupled_allocation()
4803// guarantees there's no observer of the allocated array at this point
4804// and the control flow is simple enough.
4805void LibraryCallKit::arraycopy_move_allocation_here(AllocateArrayNode* alloc, Node* dest, JVMState* saved_jvms, int saved_reexecute_sp) {
4806  if (saved_jvms != NULL && !stopped()) {
4807    assert(alloc != NULL, "only with a tightly coupled allocation");
4808    // restore JVM state to the state at the arraycopy
4809    saved_jvms->map()->set_control(map()->control());
4810    assert(saved_jvms->map()->memory() == map()->memory(), "memory state changed?");
4811    assert(saved_jvms->map()->i_o() == map()->i_o(), "IO state changed?");
4812    // If we've improved the types of some nodes (null check) while
4813    // emitting the guards, propagate them to the current state
4814    map()->replaced_nodes().apply(saved_jvms->map());
4815    set_jvms(saved_jvms);
4816    _reexecute_sp = saved_reexecute_sp;
4817
4818    // Remove the allocation from above the guards
4819    CallProjections callprojs;
4820    alloc->extract_projections(&callprojs, true);
4821    InitializeNode* init = alloc->initialization();
4822    Node* alloc_mem = alloc->in(TypeFunc::Memory);
4823    C->gvn_replace_by(callprojs.fallthrough_ioproj, alloc->in(TypeFunc::I_O));
4824    C->gvn_replace_by(init->proj_out(TypeFunc::Memory), alloc_mem);
4825    C->gvn_replace_by(init->proj_out(TypeFunc::Control), alloc->in(0));
4826
4827    // move the allocation here (after the guards)
4828    _gvn.hash_delete(alloc);
4829    alloc->set_req(TypeFunc::Control, control());
4830    alloc->set_req(TypeFunc::I_O, i_o());
4831    Node *mem = reset_memory();
4832    set_all_memory(mem);
4833    alloc->set_req(TypeFunc::Memory, mem);
4834    set_control(init->proj_out(TypeFunc::Control));
4835    set_i_o(callprojs.fallthrough_ioproj);
4836
4837    // Update memory as done in GraphKit::set_output_for_allocation()
4838    const TypeInt* length_type = _gvn.find_int_type(alloc->in(AllocateNode::ALength));
4839    const TypeOopPtr* ary_type = _gvn.type(alloc->in(AllocateNode::KlassNode))->is_klassptr()->as_instance_type();
4840    if (ary_type->isa_aryptr() && length_type != NULL) {
4841      ary_type = ary_type->is_aryptr()->cast_to_size(length_type);
4842    }
4843    const TypePtr* telemref = ary_type->add_offset(Type::OffsetBot);
4844    int            elemidx  = C->get_alias_index(telemref);
4845    set_memory(init->proj_out(TypeFunc::Memory), Compile::AliasIdxRaw);
4846    set_memory(init->proj_out(TypeFunc::Memory), elemidx);
4847
4848    Node* allocx = _gvn.transform(alloc);
4849    assert(allocx == alloc, "where has the allocation gone?");
4850    assert(dest->is_CheckCastPP(), "not an allocation result?");
4851
4852    _gvn.hash_delete(dest);
4853    dest->set_req(0, control());
4854    Node* destx = _gvn.transform(dest);
4855    assert(destx == dest, "where has the allocation result gone?");
4856  }
4857}
4858
4859
4860//------------------------------inline_arraycopy-----------------------
4861// public static native void java.lang.System.arraycopy(Object src,  int  srcPos,
4862//                                                      Object dest, int destPos,
4863//                                                      int length);
4864bool LibraryCallKit::inline_arraycopy() {
4865  // Get the arguments.
4866  Node* src         = argument(0);  // type: oop
4867  Node* src_offset  = argument(1);  // type: int
4868  Node* dest        = argument(2);  // type: oop
4869  Node* dest_offset = argument(3);  // type: int
4870  Node* length      = argument(4);  // type: int
4871
4872
4873  // Check for allocation before we add nodes that would confuse
4874  // tightly_coupled_allocation()
4875  AllocateArrayNode* alloc = tightly_coupled_allocation(dest, NULL);
4876
4877  int saved_reexecute_sp = -1;
4878  JVMState* saved_jvms = arraycopy_restore_alloc_state(alloc, saved_reexecute_sp);
4879  // See arraycopy_restore_alloc_state() comment
4880  // if alloc == NULL we don't have to worry about a tightly coupled allocation so we can emit all needed guards
4881  // if saved_jvms != NULL (then alloc != NULL) then we can handle guards and a tightly coupled allocation
4882  // if saved_jvms == NULL and alloc != NULL, we can���t emit any guards
4883  bool can_emit_guards = (alloc == NULL || saved_jvms != NULL);
4884
4885  // The following tests must be performed
4886  // (1) src and dest are arrays.
4887  // (2) src and dest arrays must have elements of the same BasicType
4888  // (3) src and dest must not be null.
4889  // (4) src_offset must not be negative.
4890  // (5) dest_offset must not be negative.
4891  // (6) length must not be negative.
4892  // (7) src_offset + length must not exceed length of src.
4893  // (8) dest_offset + length must not exceed length of dest.
4894  // (9) each element of an oop array must be assignable
4895
4896  // (3) src and dest must not be null.
4897  // always do this here because we need the JVM state for uncommon traps
4898  Node* null_ctl = top();
4899  src  = saved_jvms != NULL ? null_check_oop(src, &null_ctl, true, true) : null_check(src,  T_ARRAY);
4900  assert(null_ctl->is_top(), "no null control here");
4901  dest = null_check(dest, T_ARRAY);
4902
4903  if (!can_emit_guards) {
4904    // if saved_jvms == NULL and alloc != NULL, we don't emit any
4905    // guards but the arraycopy node could still take advantage of a
4906    // tightly allocated allocation. tightly_coupled_allocation() is
4907    // called again to make sure it takes the null check above into
4908    // account: the null check is mandatory and if it caused an
4909    // uncommon trap to be emitted then the allocation can't be
4910    // considered tightly coupled in this context.
4911    alloc = tightly_coupled_allocation(dest, NULL);
4912  }
4913
4914  bool validated = false;
4915
4916  const Type* src_type  = _gvn.type(src);
4917  const Type* dest_type = _gvn.type(dest);
4918  const TypeAryPtr* top_src  = src_type->isa_aryptr();
4919  const TypeAryPtr* top_dest = dest_type->isa_aryptr();
4920
4921  // Do we have the type of src?
4922  bool has_src = (top_src != NULL && top_src->klass() != NULL);
4923  // Do we have the type of dest?
4924  bool has_dest = (top_dest != NULL && top_dest->klass() != NULL);
4925  // Is the type for src from speculation?
4926  bool src_spec = false;
4927  // Is the type for dest from speculation?
4928  bool dest_spec = false;
4929
4930  if ((!has_src || !has_dest) && can_emit_guards) {
4931    // We don't have sufficient type information, let's see if
4932    // speculative types can help. We need to have types for both src
4933    // and dest so that it pays off.
4934
4935    // Do we already have or could we have type information for src
4936    bool could_have_src = has_src;
4937    // Do we already have or could we have type information for dest
4938    bool could_have_dest = has_dest;
4939
4940    ciKlass* src_k = NULL;
4941    if (!has_src) {
4942      src_k = src_type->speculative_type_not_null();
4943      if (src_k != NULL && src_k->is_array_klass()) {
4944        could_have_src = true;
4945      }
4946    }
4947
4948    ciKlass* dest_k = NULL;
4949    if (!has_dest) {
4950      dest_k = dest_type->speculative_type_not_null();
4951      if (dest_k != NULL && dest_k->is_array_klass()) {
4952        could_have_dest = true;
4953      }
4954    }
4955
4956    if (could_have_src && could_have_dest) {
4957      // This is going to pay off so emit the required guards
4958      if (!has_src) {
4959        src = maybe_cast_profiled_obj(src, src_k, true);
4960        src_type  = _gvn.type(src);
4961        top_src  = src_type->isa_aryptr();
4962        has_src = (top_src != NULL && top_src->klass() != NULL);
4963        src_spec = true;
4964      }
4965      if (!has_dest) {
4966        dest = maybe_cast_profiled_obj(dest, dest_k, true);
4967        dest_type  = _gvn.type(dest);
4968        top_dest  = dest_type->isa_aryptr();
4969        has_dest = (top_dest != NULL && top_dest->klass() != NULL);
4970        dest_spec = true;
4971      }
4972    }
4973  }
4974
4975  if (has_src && has_dest && can_emit_guards) {
4976    BasicType src_elem  = top_src->klass()->as_array_klass()->element_type()->basic_type();
4977    BasicType dest_elem = top_dest->klass()->as_array_klass()->element_type()->basic_type();
4978    if (src_elem  == T_ARRAY)  src_elem  = T_OBJECT;
4979    if (dest_elem == T_ARRAY)  dest_elem = T_OBJECT;
4980
4981    if (src_elem == dest_elem && src_elem == T_OBJECT) {
4982      // If both arrays are object arrays then having the exact types
4983      // for both will remove the need for a subtype check at runtime
4984      // before the call and may make it possible to pick a faster copy
4985      // routine (without a subtype check on every element)
4986      // Do we have the exact type of src?
4987      bool could_have_src = src_spec;
4988      // Do we have the exact type of dest?
4989      bool could_have_dest = dest_spec;
4990      ciKlass* src_k = top_src->klass();
4991      ciKlass* dest_k = top_dest->klass();
4992      if (!src_spec) {
4993        src_k = src_type->speculative_type_not_null();
4994        if (src_k != NULL && src_k->is_array_klass()) {
4995          could_have_src = true;
4996        }
4997      }
4998      if (!dest_spec) {
4999        dest_k = dest_type->speculative_type_not_null();
5000        if (dest_k != NULL && dest_k->is_array_klass()) {
5001          could_have_dest = true;
5002        }
5003      }
5004      if (could_have_src && could_have_dest) {
5005        // If we can have both exact types, emit the missing guards
5006        if (could_have_src && !src_spec) {
5007          src = maybe_cast_profiled_obj(src, src_k, true);
5008        }
5009        if (could_have_dest && !dest_spec) {
5010          dest = maybe_cast_profiled_obj(dest, dest_k, true);
5011        }
5012      }
5013    }
5014  }
5015
5016  ciMethod* trap_method = method();
5017  int trap_bci = bci();
5018  if (saved_jvms != NULL) {
5019    trap_method = alloc->jvms()->method();
5020    trap_bci = alloc->jvms()->bci();
5021  }
5022
5023  if (!C->too_many_traps(trap_method, trap_bci, Deoptimization::Reason_intrinsic) &&
5024      can_emit_guards &&
5025      !src->is_top() && !dest->is_top()) {
5026    // validate arguments: enables transformation the ArrayCopyNode
5027    validated = true;
5028
5029    RegionNode* slow_region = new RegionNode(1);
5030    record_for_igvn(slow_region);
5031
5032    // (1) src and dest are arrays.
5033    generate_non_array_guard(load_object_klass(src), slow_region);
5034    generate_non_array_guard(load_object_klass(dest), slow_region);
5035
5036    // (2) src and dest arrays must have elements of the same BasicType
5037    // done at macro expansion or at Ideal transformation time
5038
5039    // (4) src_offset must not be negative.
5040    generate_negative_guard(src_offset, slow_region);
5041
5042    // (5) dest_offset must not be negative.
5043    generate_negative_guard(dest_offset, slow_region);
5044
5045    // (7) src_offset + length must not exceed length of src.
5046    generate_limit_guard(src_offset, length,
5047                         load_array_length(src),
5048                         slow_region);
5049
5050    // (8) dest_offset + length must not exceed length of dest.
5051    generate_limit_guard(dest_offset, length,
5052                         load_array_length(dest),
5053                         slow_region);
5054
5055    // (9) each element of an oop array must be assignable
5056    Node* src_klass  = load_object_klass(src);
5057    Node* dest_klass = load_object_klass(dest);
5058    Node* not_subtype_ctrl = gen_subtype_check(src_klass, dest_klass);
5059
5060    if (not_subtype_ctrl != top()) {
5061      PreserveJVMState pjvms(this);
5062      set_control(not_subtype_ctrl);
5063      uncommon_trap(Deoptimization::Reason_intrinsic,
5064                    Deoptimization::Action_make_not_entrant);
5065      assert(stopped(), "Should be stopped");
5066    }
5067    {
5068      PreserveJVMState pjvms(this);
5069      set_control(_gvn.transform(slow_region));
5070      uncommon_trap(Deoptimization::Reason_intrinsic,
5071                    Deoptimization::Action_make_not_entrant);
5072      assert(stopped(), "Should be stopped");
5073    }
5074  }
5075
5076  arraycopy_move_allocation_here(alloc, dest, saved_jvms, saved_reexecute_sp);
5077
5078  if (stopped()) {
5079    return true;
5080  }
5081
5082  ArrayCopyNode* ac = ArrayCopyNode::make(this, true, src, src_offset, dest, dest_offset, length, alloc != NULL,
5083                                          // Create LoadRange and LoadKlass nodes for use during macro expansion here
5084                                          // so the compiler has a chance to eliminate them: during macro expansion,
5085                                          // we have to set their control (CastPP nodes are eliminated).
5086                                          load_object_klass(src), load_object_klass(dest),
5087                                          load_array_length(src), load_array_length(dest));
5088
5089  ac->set_arraycopy(validated);
5090
5091  Node* n = _gvn.transform(ac);
5092  if (n == ac) {
5093    ac->connect_outputs(this);
5094  } else {
5095    assert(validated, "shouldn't transform if all arguments not validated");
5096    set_all_memory(n);
5097  }
5098
5099  return true;
5100}
5101
5102
5103// Helper function which determines if an arraycopy immediately follows
5104// an allocation, with no intervening tests or other escapes for the object.
5105AllocateArrayNode*
5106LibraryCallKit::tightly_coupled_allocation(Node* ptr,
5107                                           RegionNode* slow_region) {
5108  if (stopped())             return NULL;  // no fast path
5109  if (C->AliasLevel() == 0)  return NULL;  // no MergeMems around
5110
5111  AllocateArrayNode* alloc = AllocateArrayNode::Ideal_array_allocation(ptr, &_gvn);
5112  if (alloc == NULL)  return NULL;
5113
5114  Node* rawmem = memory(Compile::AliasIdxRaw);
5115  // Is the allocation's memory state untouched?
5116  if (!(rawmem->is_Proj() && rawmem->in(0)->is_Initialize())) {
5117    // Bail out if there have been raw-memory effects since the allocation.
5118    // (Example:  There might have been a call or safepoint.)
5119    return NULL;
5120  }
5121  rawmem = rawmem->in(0)->as_Initialize()->memory(Compile::AliasIdxRaw);
5122  if (!(rawmem->is_Proj() && rawmem->in(0) == alloc)) {
5123    return NULL;
5124  }
5125
5126  // There must be no unexpected observers of this allocation.
5127  for (DUIterator_Fast imax, i = ptr->fast_outs(imax); i < imax; i++) {
5128    Node* obs = ptr->fast_out(i);
5129    if (obs != this->map()) {
5130      return NULL;
5131    }
5132  }
5133
5134  // This arraycopy must unconditionally follow the allocation of the ptr.
5135  Node* alloc_ctl = ptr->in(0);
5136  assert(just_allocated_object(alloc_ctl) == ptr, "most recent allo");
5137
5138  Node* ctl = control();
5139  while (ctl != alloc_ctl) {
5140    // There may be guards which feed into the slow_region.
5141    // Any other control flow means that we might not get a chance
5142    // to finish initializing the allocated object.
5143    if ((ctl->is_IfFalse() || ctl->is_IfTrue()) && ctl->in(0)->is_If()) {
5144      IfNode* iff = ctl->in(0)->as_If();
5145      Node* not_ctl = iff->proj_out(1 - ctl->as_Proj()->_con);
5146      assert(not_ctl != NULL && not_ctl != ctl, "found alternate");
5147      if (slow_region != NULL && slow_region->find_edge(not_ctl) >= 1) {
5148        ctl = iff->in(0);       // This test feeds the known slow_region.
5149        continue;
5150      }
5151      // One more try:  Various low-level checks bottom out in
5152      // uncommon traps.  If the debug-info of the trap omits
5153      // any reference to the allocation, as we've already
5154      // observed, then there can be no objection to the trap.
5155      bool found_trap = false;
5156      for (DUIterator_Fast jmax, j = not_ctl->fast_outs(jmax); j < jmax; j++) {
5157        Node* obs = not_ctl->fast_out(j);
5158        if (obs->in(0) == not_ctl && obs->is_Call() &&
5159            (obs->as_Call()->entry_point() == SharedRuntime::uncommon_trap_blob()->entry_point())) {
5160          found_trap = true; break;
5161        }
5162      }
5163      if (found_trap) {
5164        ctl = iff->in(0);       // This test feeds a harmless uncommon trap.
5165        continue;
5166      }
5167    }
5168    return NULL;
5169  }
5170
5171  // If we get this far, we have an allocation which immediately
5172  // precedes the arraycopy, and we can take over zeroing the new object.
5173  // The arraycopy will finish the initialization, and provide
5174  // a new control state to which we will anchor the destination pointer.
5175
5176  return alloc;
5177}
5178
5179//-------------inline_encodeISOArray-----------------------------------
5180// encode char[] to byte[] in ISO_8859_1
5181bool LibraryCallKit::inline_encodeISOArray() {
5182  assert(callee()->signature()->size() == 5, "encodeISOArray has 5 parameters");
5183  // no receiver since it is static method
5184  Node *src         = argument(0);
5185  Node *src_offset  = argument(1);
5186  Node *dst         = argument(2);
5187  Node *dst_offset  = argument(3);
5188  Node *length      = argument(4);
5189
5190  const Type* src_type = src->Value(&_gvn);
5191  const Type* dst_type = dst->Value(&_gvn);
5192  const TypeAryPtr* top_src = src_type->isa_aryptr();
5193  const TypeAryPtr* top_dest = dst_type->isa_aryptr();
5194  if (top_src  == NULL || top_src->klass()  == NULL ||
5195      top_dest == NULL || top_dest->klass() == NULL) {
5196    // failed array check
5197    return false;
5198  }
5199
5200  // Figure out the size and type of the elements we will be copying.
5201  BasicType src_elem = src_type->isa_aryptr()->klass()->as_array_klass()->element_type()->basic_type();
5202  BasicType dst_elem = dst_type->isa_aryptr()->klass()->as_array_klass()->element_type()->basic_type();
5203  if (!((src_elem == T_CHAR) || (src_elem== T_BYTE)) || dst_elem != T_BYTE) {
5204    return false;
5205  }
5206
5207  Node* src_start = array_element_address(src, src_offset, T_CHAR);
5208  Node* dst_start = array_element_address(dst, dst_offset, dst_elem);
5209  // 'src_start' points to src array + scaled offset
5210  // 'dst_start' points to dst array + scaled offset
5211
5212  const TypeAryPtr* mtype = TypeAryPtr::BYTES;
5213  Node* enc = new EncodeISOArrayNode(control(), memory(mtype), src_start, dst_start, length);
5214  enc = _gvn.transform(enc);
5215  Node* res_mem = _gvn.transform(new SCMemProjNode(enc));
5216  set_memory(res_mem, mtype);
5217  set_result(enc);
5218  return true;
5219}
5220
5221//-------------inline_multiplyToLen-----------------------------------
5222bool LibraryCallKit::inline_multiplyToLen() {
5223  assert(UseMultiplyToLenIntrinsic, "not implemented on this platform");
5224
5225  address stubAddr = StubRoutines::multiplyToLen();
5226  if (stubAddr == NULL) {
5227    return false; // Intrinsic's stub is not implemented on this platform
5228  }
5229  const char* stubName = "multiplyToLen";
5230
5231  assert(callee()->signature()->size() == 5, "multiplyToLen has 5 parameters");
5232
5233  // no receiver because it is a static method
5234  Node* x    = argument(0);
5235  Node* xlen = argument(1);
5236  Node* y    = argument(2);
5237  Node* ylen = argument(3);
5238  Node* z    = argument(4);
5239
5240  const Type* x_type = x->Value(&_gvn);
5241  const Type* y_type = y->Value(&_gvn);
5242  const TypeAryPtr* top_x = x_type->isa_aryptr();
5243  const TypeAryPtr* top_y = y_type->isa_aryptr();
5244  if (top_x  == NULL || top_x->klass()  == NULL ||
5245      top_y == NULL || top_y->klass() == NULL) {
5246    // failed array check
5247    return false;
5248  }
5249
5250  BasicType x_elem = x_type->isa_aryptr()->klass()->as_array_klass()->element_type()->basic_type();
5251  BasicType y_elem = y_type->isa_aryptr()->klass()->as_array_klass()->element_type()->basic_type();
5252  if (x_elem != T_INT || y_elem != T_INT) {
5253    return false;
5254  }
5255
5256  // Set the original stack and the reexecute bit for the interpreter to reexecute
5257  // the bytecode that invokes BigInteger.multiplyToLen() if deoptimization happens
5258  // on the return from z array allocation in runtime.
5259  { PreserveReexecuteState preexecs(this);
5260    jvms()->set_should_reexecute(true);
5261
5262    Node* x_start = array_element_address(x, intcon(0), x_elem);
5263    Node* y_start = array_element_address(y, intcon(0), y_elem);
5264    // 'x_start' points to x array + scaled xlen
5265    // 'y_start' points to y array + scaled ylen
5266
5267    // Allocate the result array
5268    Node* zlen = _gvn.transform(new AddINode(xlen, ylen));
5269    ciKlass* klass = ciTypeArrayKlass::make(T_INT);
5270    Node* klass_node = makecon(TypeKlassPtr::make(klass));
5271
5272    IdealKit ideal(this);
5273
5274#define __ ideal.
5275     Node* one = __ ConI(1);
5276     Node* zero = __ ConI(0);
5277     IdealVariable need_alloc(ideal), z_alloc(ideal);  __ declarations_done();
5278     __ set(need_alloc, zero);
5279     __ set(z_alloc, z);
5280     __ if_then(z, BoolTest::eq, null()); {
5281       __ increment (need_alloc, one);
5282     } __ else_(); {
5283       // Update graphKit memory and control from IdealKit.
5284       sync_kit(ideal);
5285       Node* zlen_arg = load_array_length(z);
5286       // Update IdealKit memory and control from graphKit.
5287       __ sync_kit(this);
5288       __ if_then(zlen_arg, BoolTest::lt, zlen); {
5289         __ increment (need_alloc, one);
5290       } __ end_if();
5291     } __ end_if();
5292
5293     __ if_then(__ value(need_alloc), BoolTest::ne, zero); {
5294       // Update graphKit memory and control from IdealKit.
5295       sync_kit(ideal);
5296       Node * narr = new_array(klass_node, zlen, 1);
5297       // Update IdealKit memory and control from graphKit.
5298       __ sync_kit(this);
5299       __ set(z_alloc, narr);
5300     } __ end_if();
5301
5302     sync_kit(ideal);
5303     z = __ value(z_alloc);
5304     // Can't use TypeAryPtr::INTS which uses Bottom offset.
5305     _gvn.set_type(z, TypeOopPtr::make_from_klass(klass));
5306     // Final sync IdealKit and GraphKit.
5307     final_sync(ideal);
5308#undef __
5309
5310    Node* z_start = array_element_address(z, intcon(0), T_INT);
5311
5312    Node* call = make_runtime_call(RC_LEAF|RC_NO_FP,
5313                                   OptoRuntime::multiplyToLen_Type(),
5314                                   stubAddr, stubName, TypePtr::BOTTOM,
5315                                   x_start, xlen, y_start, ylen, z_start, zlen);
5316  } // original reexecute is set back here
5317
5318  C->set_has_split_ifs(true); // Has chance for split-if optimization
5319  set_result(z);
5320  return true;
5321}
5322
5323//-------------inline_squareToLen------------------------------------
5324bool LibraryCallKit::inline_squareToLen() {
5325  assert(UseSquareToLenIntrinsic, "not implemented on this platform");
5326
5327  address stubAddr = StubRoutines::squareToLen();
5328  if (stubAddr == NULL) {
5329    return false; // Intrinsic's stub is not implemented on this platform
5330  }
5331  const char* stubName = "squareToLen";
5332
5333  assert(callee()->signature()->size() == 4, "implSquareToLen has 4 parameters");
5334
5335  Node* x    = argument(0);
5336  Node* len  = argument(1);
5337  Node* z    = argument(2);
5338  Node* zlen = argument(3);
5339
5340  const Type* x_type = x->Value(&_gvn);
5341  const Type* z_type = z->Value(&_gvn);
5342  const TypeAryPtr* top_x = x_type->isa_aryptr();
5343  const TypeAryPtr* top_z = z_type->isa_aryptr();
5344  if (top_x  == NULL || top_x->klass()  == NULL ||
5345      top_z  == NULL || top_z->klass()  == NULL) {
5346    // failed array check
5347    return false;
5348  }
5349
5350  BasicType x_elem = x_type->isa_aryptr()->klass()->as_array_klass()->element_type()->basic_type();
5351  BasicType z_elem = z_type->isa_aryptr()->klass()->as_array_klass()->element_type()->basic_type();
5352  if (x_elem != T_INT || z_elem != T_INT) {
5353    return false;
5354  }
5355
5356
5357  Node* x_start = array_element_address(x, intcon(0), x_elem);
5358  Node* z_start = array_element_address(z, intcon(0), z_elem);
5359
5360  Node*  call = make_runtime_call(RC_LEAF|RC_NO_FP,
5361                                  OptoRuntime::squareToLen_Type(),
5362                                  stubAddr, stubName, TypePtr::BOTTOM,
5363                                  x_start, len, z_start, zlen);
5364
5365  set_result(z);
5366  return true;
5367}
5368
5369//-------------inline_mulAdd------------------------------------------
5370bool LibraryCallKit::inline_mulAdd() {
5371  assert(UseMulAddIntrinsic, "not implemented on this platform");
5372
5373  address stubAddr = StubRoutines::mulAdd();
5374  if (stubAddr == NULL) {
5375    return false; // Intrinsic's stub is not implemented on this platform
5376  }
5377  const char* stubName = "mulAdd";
5378
5379  assert(callee()->signature()->size() == 5, "mulAdd has 5 parameters");
5380
5381  Node* out      = argument(0);
5382  Node* in       = argument(1);
5383  Node* offset   = argument(2);
5384  Node* len      = argument(3);
5385  Node* k        = argument(4);
5386
5387  const Type* out_type = out->Value(&_gvn);
5388  const Type* in_type = in->Value(&_gvn);
5389  const TypeAryPtr* top_out = out_type->isa_aryptr();
5390  const TypeAryPtr* top_in = in_type->isa_aryptr();
5391  if (top_out  == NULL || top_out->klass()  == NULL ||
5392      top_in == NULL || top_in->klass() == NULL) {
5393    // failed array check
5394    return false;
5395  }
5396
5397  BasicType out_elem = out_type->isa_aryptr()->klass()->as_array_klass()->element_type()->basic_type();
5398  BasicType in_elem = in_type->isa_aryptr()->klass()->as_array_klass()->element_type()->basic_type();
5399  if (out_elem != T_INT || in_elem != T_INT) {
5400    return false;
5401  }
5402
5403  Node* outlen = load_array_length(out);
5404  Node* new_offset = _gvn.transform(new SubINode(outlen, offset));
5405  Node* out_start = array_element_address(out, intcon(0), out_elem);
5406  Node* in_start = array_element_address(in, intcon(0), in_elem);
5407
5408  Node*  call = make_runtime_call(RC_LEAF|RC_NO_FP,
5409                                  OptoRuntime::mulAdd_Type(),
5410                                  stubAddr, stubName, TypePtr::BOTTOM,
5411                                  out_start,in_start, new_offset, len, k);
5412  Node* result = _gvn.transform(new ProjNode(call, TypeFunc::Parms));
5413  set_result(result);
5414  return true;
5415}
5416
5417//-------------inline_montgomeryMultiply-----------------------------------
5418bool LibraryCallKit::inline_montgomeryMultiply() {
5419  address stubAddr = StubRoutines::montgomeryMultiply();
5420  if (stubAddr == NULL) {
5421    return false; // Intrinsic's stub is not implemented on this platform
5422  }
5423
5424  assert(UseMontgomeryMultiplyIntrinsic, "not implemented on this platform");
5425  const char* stubName = "montgomery_square";
5426
5427  assert(callee()->signature()->size() == 7, "montgomeryMultiply has 7 parameters");
5428
5429  Node* a    = argument(0);
5430  Node* b    = argument(1);
5431  Node* n    = argument(2);
5432  Node* len  = argument(3);
5433  Node* inv  = argument(4);
5434  Node* m    = argument(6);
5435
5436  const Type* a_type = a->Value(&_gvn);
5437  const TypeAryPtr* top_a = a_type->isa_aryptr();
5438  const Type* b_type = b->Value(&_gvn);
5439  const TypeAryPtr* top_b = b_type->isa_aryptr();
5440  const Type* n_type = a->Value(&_gvn);
5441  const TypeAryPtr* top_n = n_type->isa_aryptr();
5442  const Type* m_type = a->Value(&_gvn);
5443  const TypeAryPtr* top_m = m_type->isa_aryptr();
5444  if (top_a  == NULL || top_a->klass()  == NULL ||
5445      top_b == NULL || top_b->klass()  == NULL ||
5446      top_n == NULL || top_n->klass()  == NULL ||
5447      top_m == NULL || top_m->klass()  == NULL) {
5448    // failed array check
5449    return false;
5450  }
5451
5452  BasicType a_elem = a_type->isa_aryptr()->klass()->as_array_klass()->element_type()->basic_type();
5453  BasicType b_elem = b_type->isa_aryptr()->klass()->as_array_klass()->element_type()->basic_type();
5454  BasicType n_elem = n_type->isa_aryptr()->klass()->as_array_klass()->element_type()->basic_type();
5455  BasicType m_elem = m_type->isa_aryptr()->klass()->as_array_klass()->element_type()->basic_type();
5456  if (a_elem != T_INT || b_elem != T_INT || n_elem != T_INT || m_elem != T_INT) {
5457    return false;
5458  }
5459
5460  // Make the call
5461  {
5462    Node* a_start = array_element_address(a, intcon(0), a_elem);
5463    Node* b_start = array_element_address(b, intcon(0), b_elem);
5464    Node* n_start = array_element_address(n, intcon(0), n_elem);
5465    Node* m_start = array_element_address(m, intcon(0), m_elem);
5466
5467    Node* call = make_runtime_call(RC_LEAF,
5468                                   OptoRuntime::montgomeryMultiply_Type(),
5469                                   stubAddr, stubName, TypePtr::BOTTOM,
5470                                   a_start, b_start, n_start, len, inv, top(),
5471                                   m_start);
5472    set_result(m);
5473  }
5474
5475  return true;
5476}
5477
5478bool LibraryCallKit::inline_montgomerySquare() {
5479  address stubAddr = StubRoutines::montgomerySquare();
5480  if (stubAddr == NULL) {
5481    return false; // Intrinsic's stub is not implemented on this platform
5482  }
5483
5484  assert(UseMontgomerySquareIntrinsic, "not implemented on this platform");
5485  const char* stubName = "montgomery_square";
5486
5487  assert(callee()->signature()->size() == 6, "montgomerySquare has 6 parameters");
5488
5489  Node* a    = argument(0);
5490  Node* n    = argument(1);
5491  Node* len  = argument(2);
5492  Node* inv  = argument(3);
5493  Node* m    = argument(5);
5494
5495  const Type* a_type = a->Value(&_gvn);
5496  const TypeAryPtr* top_a = a_type->isa_aryptr();
5497  const Type* n_type = a->Value(&_gvn);
5498  const TypeAryPtr* top_n = n_type->isa_aryptr();
5499  const Type* m_type = a->Value(&_gvn);
5500  const TypeAryPtr* top_m = m_type->isa_aryptr();
5501  if (top_a  == NULL || top_a->klass()  == NULL ||
5502      top_n == NULL || top_n->klass()  == NULL ||
5503      top_m == NULL || top_m->klass()  == NULL) {
5504    // failed array check
5505    return false;
5506  }
5507
5508  BasicType a_elem = a_type->isa_aryptr()->klass()->as_array_klass()->element_type()->basic_type();
5509  BasicType n_elem = n_type->isa_aryptr()->klass()->as_array_klass()->element_type()->basic_type();
5510  BasicType m_elem = m_type->isa_aryptr()->klass()->as_array_klass()->element_type()->basic_type();
5511  if (a_elem != T_INT || n_elem != T_INT || m_elem != T_INT) {
5512    return false;
5513  }
5514
5515  // Make the call
5516  {
5517    Node* a_start = array_element_address(a, intcon(0), a_elem);
5518    Node* n_start = array_element_address(n, intcon(0), n_elem);
5519    Node* m_start = array_element_address(m, intcon(0), m_elem);
5520
5521    Node* call = make_runtime_call(RC_LEAF,
5522                                   OptoRuntime::montgomerySquare_Type(),
5523                                   stubAddr, stubName, TypePtr::BOTTOM,
5524                                   a_start, n_start, len, inv, top(),
5525                                   m_start);
5526    set_result(m);
5527  }
5528
5529  return true;
5530}
5531
5532//-------------inline_vectorizedMismatch------------------------------
5533bool LibraryCallKit::inline_vectorizedMismatch() {
5534  assert(UseVectorizedMismatchIntrinsic, "not implementated on this platform");
5535
5536  address stubAddr = StubRoutines::vectorizedMismatch();
5537  if (stubAddr == NULL) {
5538    return false; // Intrinsic's stub is not implemented on this platform
5539  }
5540  const char* stubName = "vectorizedMismatch";
5541  int size_l = callee()->signature()->size();
5542  assert(callee()->signature()->size() == 8, "vectorizedMismatch has 6 parameters");
5543
5544  Node* obja = argument(0);
5545  Node* aoffset = argument(1);
5546  Node* objb = argument(3);
5547  Node* boffset = argument(4);
5548  Node* length = argument(6);
5549  Node* scale = argument(7);
5550
5551  const Type* a_type = obja->Value(&_gvn);
5552  const Type* b_type = objb->Value(&_gvn);
5553  const TypeAryPtr* top_a = a_type->isa_aryptr();
5554  const TypeAryPtr* top_b = b_type->isa_aryptr();
5555  if (top_a == NULL || top_a->klass() == NULL ||
5556    top_b == NULL || top_b->klass() == NULL) {
5557    // failed array check
5558    return false;
5559  }
5560
5561  Node* call;
5562  jvms()->set_should_reexecute(true);
5563
5564  Node* obja_adr = make_unsafe_address(obja, aoffset);
5565  Node* objb_adr = make_unsafe_address(objb, boffset);
5566
5567  call = make_runtime_call(RC_LEAF,
5568    OptoRuntime::vectorizedMismatch_Type(),
5569    stubAddr, stubName, TypePtr::BOTTOM,
5570    obja_adr, objb_adr, length, scale);
5571
5572  Node* result = _gvn.transform(new ProjNode(call, TypeFunc::Parms));
5573  set_result(result);
5574  return true;
5575}
5576
5577/**
5578 * Calculate CRC32 for byte.
5579 * int java.util.zip.CRC32.update(int crc, int b)
5580 */
5581bool LibraryCallKit::inline_updateCRC32() {
5582  assert(UseCRC32Intrinsics, "need AVX and LCMUL instructions support");
5583  assert(callee()->signature()->size() == 2, "update has 2 parameters");
5584  // no receiver since it is static method
5585  Node* crc  = argument(0); // type: int
5586  Node* b    = argument(1); // type: int
5587
5588  /*
5589   *    int c = ~ crc;
5590   *    b = timesXtoThe32[(b ^ c) & 0xFF];
5591   *    b = b ^ (c >>> 8);
5592   *    crc = ~b;
5593   */
5594
5595  Node* M1 = intcon(-1);
5596  crc = _gvn.transform(new XorINode(crc, M1));
5597  Node* result = _gvn.transform(new XorINode(crc, b));
5598  result = _gvn.transform(new AndINode(result, intcon(0xFF)));
5599
5600  Node* base = makecon(TypeRawPtr::make(StubRoutines::crc_table_addr()));
5601  Node* offset = _gvn.transform(new LShiftINode(result, intcon(0x2)));
5602  Node* adr = basic_plus_adr(top(), base, ConvI2X(offset));
5603  result = make_load(control(), adr, TypeInt::INT, T_INT, MemNode::unordered);
5604
5605  crc = _gvn.transform(new URShiftINode(crc, intcon(8)));
5606  result = _gvn.transform(new XorINode(crc, result));
5607  result = _gvn.transform(new XorINode(result, M1));
5608  set_result(result);
5609  return true;
5610}
5611
5612/**
5613 * Calculate CRC32 for byte[] array.
5614 * int java.util.zip.CRC32.updateBytes(int crc, byte[] buf, int off, int len)
5615 */
5616bool LibraryCallKit::inline_updateBytesCRC32() {
5617  assert(UseCRC32Intrinsics, "need AVX and LCMUL instructions support");
5618  assert(callee()->signature()->size() == 4, "updateBytes has 4 parameters");
5619  // no receiver since it is static method
5620  Node* crc     = argument(0); // type: int
5621  Node* src     = argument(1); // type: oop
5622  Node* offset  = argument(2); // type: int
5623  Node* length  = argument(3); // type: int
5624
5625  const Type* src_type = src->Value(&_gvn);
5626  const TypeAryPtr* top_src = src_type->isa_aryptr();
5627  if (top_src  == NULL || top_src->klass()  == NULL) {
5628    // failed array check
5629    return false;
5630  }
5631
5632  // Figure out the size and type of the elements we will be copying.
5633  BasicType src_elem = src_type->isa_aryptr()->klass()->as_array_klass()->element_type()->basic_type();
5634  if (src_elem != T_BYTE) {
5635    return false;
5636  }
5637
5638  // 'src_start' points to src array + scaled offset
5639  Node* src_start = array_element_address(src, offset, src_elem);
5640
5641  // We assume that range check is done by caller.
5642  // TODO: generate range check (offset+length < src.length) in debug VM.
5643
5644  // Call the stub.
5645  address stubAddr = StubRoutines::updateBytesCRC32();
5646  const char *stubName = "updateBytesCRC32";
5647
5648  Node* call = make_runtime_call(RC_LEAF|RC_NO_FP, OptoRuntime::updateBytesCRC32_Type(),
5649                                 stubAddr, stubName, TypePtr::BOTTOM,
5650                                 crc, src_start, length);
5651  Node* result = _gvn.transform(new ProjNode(call, TypeFunc::Parms));
5652  set_result(result);
5653  return true;
5654}
5655
5656/**
5657 * Calculate CRC32 for ByteBuffer.
5658 * int java.util.zip.CRC32.updateByteBuffer(int crc, long buf, int off, int len)
5659 */
5660bool LibraryCallKit::inline_updateByteBufferCRC32() {
5661  assert(UseCRC32Intrinsics, "need AVX and LCMUL instructions support");
5662  assert(callee()->signature()->size() == 5, "updateByteBuffer has 4 parameters and one is long");
5663  // no receiver since it is static method
5664  Node* crc     = argument(0); // type: int
5665  Node* src     = argument(1); // type: long
5666  Node* offset  = argument(3); // type: int
5667  Node* length  = argument(4); // type: int
5668
5669  src = ConvL2X(src);  // adjust Java long to machine word
5670  Node* base = _gvn.transform(new CastX2PNode(src));
5671  offset = ConvI2X(offset);
5672
5673  // 'src_start' points to src array + scaled offset
5674  Node* src_start = basic_plus_adr(top(), base, offset);
5675
5676  // Call the stub.
5677  address stubAddr = StubRoutines::updateBytesCRC32();
5678  const char *stubName = "updateBytesCRC32";
5679
5680  Node* call = make_runtime_call(RC_LEAF|RC_NO_FP, OptoRuntime::updateBytesCRC32_Type(),
5681                                 stubAddr, stubName, TypePtr::BOTTOM,
5682                                 crc, src_start, length);
5683  Node* result = _gvn.transform(new ProjNode(call, TypeFunc::Parms));
5684  set_result(result);
5685  return true;
5686}
5687
5688//------------------------------get_table_from_crc32c_class-----------------------
5689Node * LibraryCallKit::get_table_from_crc32c_class(ciInstanceKlass *crc32c_class) {
5690  Node* table = load_field_from_object(NULL, "byteTable", "[I", /*is_exact*/ false, /*is_static*/ true, crc32c_class);
5691  assert (table != NULL, "wrong version of java.util.zip.CRC32C");
5692
5693  return table;
5694}
5695
5696//------------------------------inline_updateBytesCRC32C-----------------------
5697//
5698// Calculate CRC32C for byte[] array.
5699// int java.util.zip.CRC32C.updateBytes(int crc, byte[] buf, int off, int end)
5700//
5701bool LibraryCallKit::inline_updateBytesCRC32C() {
5702  assert(UseCRC32CIntrinsics, "need CRC32C instruction support");
5703  assert(callee()->signature()->size() == 4, "updateBytes has 4 parameters");
5704  assert(callee()->holder()->is_loaded(), "CRC32C class must be loaded");
5705  // no receiver since it is a static method
5706  Node* crc     = argument(0); // type: int
5707  Node* src     = argument(1); // type: oop
5708  Node* offset  = argument(2); // type: int
5709  Node* end     = argument(3); // type: int
5710
5711  Node* length = _gvn.transform(new SubINode(end, offset));
5712
5713  const Type* src_type = src->Value(&_gvn);
5714  const TypeAryPtr* top_src = src_type->isa_aryptr();
5715  if (top_src  == NULL || top_src->klass()  == NULL) {
5716    // failed array check
5717    return false;
5718  }
5719
5720  // Figure out the size and type of the elements we will be copying.
5721  BasicType src_elem = src_type->isa_aryptr()->klass()->as_array_klass()->element_type()->basic_type();
5722  if (src_elem != T_BYTE) {
5723    return false;
5724  }
5725
5726  // 'src_start' points to src array + scaled offset
5727  Node* src_start = array_element_address(src, offset, src_elem);
5728
5729  // static final int[] byteTable in class CRC32C
5730  Node* table = get_table_from_crc32c_class(callee()->holder());
5731  Node* table_start = array_element_address(table, intcon(0), T_INT);
5732
5733  // We assume that range check is done by caller.
5734  // TODO: generate range check (offset+length < src.length) in debug VM.
5735
5736  // Call the stub.
5737  address stubAddr = StubRoutines::updateBytesCRC32C();
5738  const char *stubName = "updateBytesCRC32C";
5739
5740  Node* call = make_runtime_call(RC_LEAF, OptoRuntime::updateBytesCRC32C_Type(),
5741                                 stubAddr, stubName, TypePtr::BOTTOM,
5742                                 crc, src_start, length, table_start);
5743  Node* result = _gvn.transform(new ProjNode(call, TypeFunc::Parms));
5744  set_result(result);
5745  return true;
5746}
5747
5748//------------------------------inline_updateDirectByteBufferCRC32C-----------------------
5749//
5750// Calculate CRC32C for DirectByteBuffer.
5751// int java.util.zip.CRC32C.updateDirectByteBuffer(int crc, long buf, int off, int end)
5752//
5753bool LibraryCallKit::inline_updateDirectByteBufferCRC32C() {
5754  assert(UseCRC32CIntrinsics, "need CRC32C instruction support");
5755  assert(callee()->signature()->size() == 5, "updateDirectByteBuffer has 4 parameters and one is long");
5756  assert(callee()->holder()->is_loaded(), "CRC32C class must be loaded");
5757  // no receiver since it is a static method
5758  Node* crc     = argument(0); // type: int
5759  Node* src     = argument(1); // type: long
5760  Node* offset  = argument(3); // type: int
5761  Node* end     = argument(4); // type: int
5762
5763  Node* length = _gvn.transform(new SubINode(end, offset));
5764
5765  src = ConvL2X(src);  // adjust Java long to machine word
5766  Node* base = _gvn.transform(new CastX2PNode(src));
5767  offset = ConvI2X(offset);
5768
5769  // 'src_start' points to src array + scaled offset
5770  Node* src_start = basic_plus_adr(top(), base, offset);
5771
5772  // static final int[] byteTable in class CRC32C
5773  Node* table = get_table_from_crc32c_class(callee()->holder());
5774  Node* table_start = array_element_address(table, intcon(0), T_INT);
5775
5776  // Call the stub.
5777  address stubAddr = StubRoutines::updateBytesCRC32C();
5778  const char *stubName = "updateBytesCRC32C";
5779
5780  Node* call = make_runtime_call(RC_LEAF, OptoRuntime::updateBytesCRC32C_Type(),
5781                                 stubAddr, stubName, TypePtr::BOTTOM,
5782                                 crc, src_start, length, table_start);
5783  Node* result = _gvn.transform(new ProjNode(call, TypeFunc::Parms));
5784  set_result(result);
5785  return true;
5786}
5787
5788//------------------------------inline_updateBytesAdler32----------------------
5789//
5790// Calculate Adler32 checksum for byte[] array.
5791// int java.util.zip.Adler32.updateBytes(int crc, byte[] buf, int off, int len)
5792//
5793bool LibraryCallKit::inline_updateBytesAdler32() {
5794  assert(UseAdler32Intrinsics, "Adler32 Instrinsic support need"); // check if we actually need to check this flag or check a different one
5795  assert(callee()->signature()->size() == 4, "updateBytes has 4 parameters");
5796  assert(callee()->holder()->is_loaded(), "Adler32 class must be loaded");
5797  // no receiver since it is static method
5798  Node* crc     = argument(0); // type: int
5799  Node* src     = argument(1); // type: oop
5800  Node* offset  = argument(2); // type: int
5801  Node* length  = argument(3); // type: int
5802
5803  const Type* src_type = src->Value(&_gvn);
5804  const TypeAryPtr* top_src = src_type->isa_aryptr();
5805  if (top_src  == NULL || top_src->klass()  == NULL) {
5806    // failed array check
5807    return false;
5808  }
5809
5810  // Figure out the size and type of the elements we will be copying.
5811  BasicType src_elem = src_type->isa_aryptr()->klass()->as_array_klass()->element_type()->basic_type();
5812  if (src_elem != T_BYTE) {
5813    return false;
5814  }
5815
5816  // 'src_start' points to src array + scaled offset
5817  Node* src_start = array_element_address(src, offset, src_elem);
5818
5819  // We assume that range check is done by caller.
5820  // TODO: generate range check (offset+length < src.length) in debug VM.
5821
5822  // Call the stub.
5823  address stubAddr = StubRoutines::updateBytesAdler32();
5824  const char *stubName = "updateBytesAdler32";
5825
5826  Node* call = make_runtime_call(RC_LEAF, OptoRuntime::updateBytesAdler32_Type(),
5827                                 stubAddr, stubName, TypePtr::BOTTOM,
5828                                 crc, src_start, length);
5829  Node* result = _gvn.transform(new ProjNode(call, TypeFunc::Parms));
5830  set_result(result);
5831  return true;
5832}
5833
5834//------------------------------inline_updateByteBufferAdler32---------------
5835//
5836// Calculate Adler32 checksum for DirectByteBuffer.
5837// int java.util.zip.Adler32.updateByteBuffer(int crc, long buf, int off, int len)
5838//
5839bool LibraryCallKit::inline_updateByteBufferAdler32() {
5840  assert(UseAdler32Intrinsics, "Adler32 Instrinsic support need"); // check if we actually need to check this flag or check a different one
5841  assert(callee()->signature()->size() == 5, "updateByteBuffer has 4 parameters and one is long");
5842  assert(callee()->holder()->is_loaded(), "Adler32 class must be loaded");
5843  // no receiver since it is static method
5844  Node* crc     = argument(0); // type: int
5845  Node* src     = argument(1); // type: long
5846  Node* offset  = argument(3); // type: int
5847  Node* length  = argument(4); // type: int
5848
5849  src = ConvL2X(src);  // adjust Java long to machine word
5850  Node* base = _gvn.transform(new CastX2PNode(src));
5851  offset = ConvI2X(offset);
5852
5853  // 'src_start' points to src array + scaled offset
5854  Node* src_start = basic_plus_adr(top(), base, offset);
5855
5856  // Call the stub.
5857  address stubAddr = StubRoutines::updateBytesAdler32();
5858  const char *stubName = "updateBytesAdler32";
5859
5860  Node* call = make_runtime_call(RC_LEAF, OptoRuntime::updateBytesAdler32_Type(),
5861                                 stubAddr, stubName, TypePtr::BOTTOM,
5862                                 crc, src_start, length);
5863
5864  Node* result = _gvn.transform(new ProjNode(call, TypeFunc::Parms));
5865  set_result(result);
5866  return true;
5867}
5868
5869//----------------------------inline_reference_get----------------------------
5870// public T java.lang.ref.Reference.get();
5871bool LibraryCallKit::inline_reference_get() {
5872  const int referent_offset = java_lang_ref_Reference::referent_offset;
5873  guarantee(referent_offset > 0, "should have already been set");
5874
5875  // Get the argument:
5876  Node* reference_obj = null_check_receiver();
5877  if (stopped()) return true;
5878
5879  Node* adr = basic_plus_adr(reference_obj, reference_obj, referent_offset);
5880
5881  ciInstanceKlass* klass = env()->Object_klass();
5882  const TypeOopPtr* object_type = TypeOopPtr::make_from_klass(klass);
5883
5884  Node* no_ctrl = NULL;
5885  Node* result = make_load(no_ctrl, adr, object_type, T_OBJECT, MemNode::unordered);
5886
5887  // Use the pre-barrier to record the value in the referent field
5888  pre_barrier(false /* do_load */,
5889              control(),
5890              NULL /* obj */, NULL /* adr */, max_juint /* alias_idx */, NULL /* val */, NULL /* val_type */,
5891              result /* pre_val */,
5892              T_OBJECT);
5893
5894  // Add memory barrier to prevent commoning reads from this field
5895  // across safepoint since GC can change its value.
5896  insert_mem_bar(Op_MemBarCPUOrder);
5897
5898  set_result(result);
5899  return true;
5900}
5901
5902
5903Node * LibraryCallKit::load_field_from_object(Node * fromObj, const char * fieldName, const char * fieldTypeString,
5904                                              bool is_exact=true, bool is_static=false,
5905                                              ciInstanceKlass * fromKls=NULL) {
5906  if (fromKls == NULL) {
5907    const TypeInstPtr* tinst = _gvn.type(fromObj)->isa_instptr();
5908    assert(tinst != NULL, "obj is null");
5909    assert(tinst->klass()->is_loaded(), "obj is not loaded");
5910    assert(!is_exact || tinst->klass_is_exact(), "klass not exact");
5911    fromKls = tinst->klass()->as_instance_klass();
5912  } else {
5913    assert(is_static, "only for static field access");
5914  }
5915  ciField* field = fromKls->get_field_by_name(ciSymbol::make(fieldName),
5916                                              ciSymbol::make(fieldTypeString),
5917                                              is_static);
5918
5919  assert (field != NULL, "undefined field");
5920  if (field == NULL) return (Node *) NULL;
5921
5922  if (is_static) {
5923    const TypeInstPtr* tip = TypeInstPtr::make(fromKls->java_mirror());
5924    fromObj = makecon(tip);
5925  }
5926
5927  // Next code  copied from Parse::do_get_xxx():
5928
5929  // Compute address and memory type.
5930  int offset  = field->offset_in_bytes();
5931  bool is_vol = field->is_volatile();
5932  ciType* field_klass = field->type();
5933  assert(field_klass->is_loaded(), "should be loaded");
5934  const TypePtr* adr_type = C->alias_type(field)->adr_type();
5935  Node *adr = basic_plus_adr(fromObj, fromObj, offset);
5936  BasicType bt = field->layout_type();
5937
5938  // Build the resultant type of the load
5939  const Type *type;
5940  if (bt == T_OBJECT) {
5941    type = TypeOopPtr::make_from_klass(field_klass->as_klass());
5942  } else {
5943    type = Type::get_const_basic_type(bt);
5944  }
5945
5946  if (support_IRIW_for_not_multiple_copy_atomic_cpu && is_vol) {
5947    insert_mem_bar(Op_MemBarVolatile);   // StoreLoad barrier
5948  }
5949  // Build the load.
5950  MemNode::MemOrd mo = is_vol ? MemNode::acquire : MemNode::unordered;
5951  Node* loadedField = make_load(NULL, adr, type, bt, adr_type, mo, LoadNode::DependsOnlyOnTest, is_vol);
5952  // If reference is volatile, prevent following memory ops from
5953  // floating up past the volatile read.  Also prevents commoning
5954  // another volatile read.
5955  if (is_vol) {
5956    // Memory barrier includes bogus read of value to force load BEFORE membar
5957    insert_mem_bar(Op_MemBarAcquire, loadedField);
5958  }
5959  return loadedField;
5960}
5961
5962Node * LibraryCallKit::field_address_from_object(Node * fromObj, const char * fieldName, const char * fieldTypeString,
5963                                                 bool is_exact = true, bool is_static = false,
5964                                                 ciInstanceKlass * fromKls = NULL) {
5965  if (fromKls == NULL) {
5966    const TypeInstPtr* tinst = _gvn.type(fromObj)->isa_instptr();
5967    assert(tinst != NULL, "obj is null");
5968    assert(tinst->klass()->is_loaded(), "obj is not loaded");
5969    assert(!is_exact || tinst->klass_is_exact(), "klass not exact");
5970    fromKls = tinst->klass()->as_instance_klass();
5971  }
5972  else {
5973    assert(is_static, "only for static field access");
5974  }
5975  ciField* field = fromKls->get_field_by_name(ciSymbol::make(fieldName),
5976    ciSymbol::make(fieldTypeString),
5977    is_static);
5978
5979  assert(field != NULL, "undefined field");
5980  assert(!field->is_volatile(), "not defined for volatile fields");
5981
5982  if (is_static) {
5983    const TypeInstPtr* tip = TypeInstPtr::make(fromKls->java_mirror());
5984    fromObj = makecon(tip);
5985  }
5986
5987  // Next code  copied from Parse::do_get_xxx():
5988
5989  // Compute address and memory type.
5990  int offset = field->offset_in_bytes();
5991  Node *adr = basic_plus_adr(fromObj, fromObj, offset);
5992
5993  return adr;
5994}
5995
5996//------------------------------inline_aescrypt_Block-----------------------
5997bool LibraryCallKit::inline_aescrypt_Block(vmIntrinsics::ID id) {
5998  address stubAddr = NULL;
5999  const char *stubName;
6000  assert(UseAES, "need AES instruction support");
6001
6002  switch(id) {
6003  case vmIntrinsics::_aescrypt_encryptBlock:
6004    stubAddr = StubRoutines::aescrypt_encryptBlock();
6005    stubName = "aescrypt_encryptBlock";
6006    break;
6007  case vmIntrinsics::_aescrypt_decryptBlock:
6008    stubAddr = StubRoutines::aescrypt_decryptBlock();
6009    stubName = "aescrypt_decryptBlock";
6010    break;
6011  }
6012  if (stubAddr == NULL) return false;
6013
6014  Node* aescrypt_object = argument(0);
6015  Node* src             = argument(1);
6016  Node* src_offset      = argument(2);
6017  Node* dest            = argument(3);
6018  Node* dest_offset     = argument(4);
6019
6020  // (1) src and dest are arrays.
6021  const Type* src_type = src->Value(&_gvn);
6022  const Type* dest_type = dest->Value(&_gvn);
6023  const TypeAryPtr* top_src = src_type->isa_aryptr();
6024  const TypeAryPtr* top_dest = dest_type->isa_aryptr();
6025  assert (top_src  != NULL && top_src->klass()  != NULL &&  top_dest != NULL && top_dest->klass() != NULL, "args are strange");
6026
6027  // for the quick and dirty code we will skip all the checks.
6028  // we are just trying to get the call to be generated.
6029  Node* src_start  = src;
6030  Node* dest_start = dest;
6031  if (src_offset != NULL || dest_offset != NULL) {
6032    assert(src_offset != NULL && dest_offset != NULL, "");
6033    src_start  = array_element_address(src,  src_offset,  T_BYTE);
6034    dest_start = array_element_address(dest, dest_offset, T_BYTE);
6035  }
6036
6037  // now need to get the start of its expanded key array
6038  // this requires a newer class file that has this array as littleEndian ints, otherwise we revert to java
6039  Node* k_start = get_key_start_from_aescrypt_object(aescrypt_object);
6040  if (k_start == NULL) return false;
6041
6042  if (Matcher::pass_original_key_for_aes()) {
6043    // on SPARC we need to pass the original key since key expansion needs to happen in intrinsics due to
6044    // compatibility issues between Java key expansion and SPARC crypto instructions
6045    Node* original_k_start = get_original_key_start_from_aescrypt_object(aescrypt_object);
6046    if (original_k_start == NULL) return false;
6047
6048    // Call the stub.
6049    make_runtime_call(RC_LEAF|RC_NO_FP, OptoRuntime::aescrypt_block_Type(),
6050                      stubAddr, stubName, TypePtr::BOTTOM,
6051                      src_start, dest_start, k_start, original_k_start);
6052  } else {
6053    // Call the stub.
6054    make_runtime_call(RC_LEAF|RC_NO_FP, OptoRuntime::aescrypt_block_Type(),
6055                      stubAddr, stubName, TypePtr::BOTTOM,
6056                      src_start, dest_start, k_start);
6057  }
6058
6059  return true;
6060}
6061
6062//------------------------------inline_cipherBlockChaining_AESCrypt-----------------------
6063bool LibraryCallKit::inline_cipherBlockChaining_AESCrypt(vmIntrinsics::ID id) {
6064  address stubAddr = NULL;
6065  const char *stubName = NULL;
6066
6067  assert(UseAES, "need AES instruction support");
6068
6069  switch(id) {
6070  case vmIntrinsics::_cipherBlockChaining_encryptAESCrypt:
6071    stubAddr = StubRoutines::cipherBlockChaining_encryptAESCrypt();
6072    stubName = "cipherBlockChaining_encryptAESCrypt";
6073    break;
6074  case vmIntrinsics::_cipherBlockChaining_decryptAESCrypt:
6075    stubAddr = StubRoutines::cipherBlockChaining_decryptAESCrypt();
6076    stubName = "cipherBlockChaining_decryptAESCrypt";
6077    break;
6078  }
6079  if (stubAddr == NULL) return false;
6080
6081  Node* cipherBlockChaining_object = argument(0);
6082  Node* src                        = argument(1);
6083  Node* src_offset                 = argument(2);
6084  Node* len                        = argument(3);
6085  Node* dest                       = argument(4);
6086  Node* dest_offset                = argument(5);
6087
6088  // (1) src and dest are arrays.
6089  const Type* src_type = src->Value(&_gvn);
6090  const Type* dest_type = dest->Value(&_gvn);
6091  const TypeAryPtr* top_src = src_type->isa_aryptr();
6092  const TypeAryPtr* top_dest = dest_type->isa_aryptr();
6093  assert (top_src  != NULL && top_src->klass()  != NULL
6094          &&  top_dest != NULL && top_dest->klass() != NULL, "args are strange");
6095
6096  // checks are the responsibility of the caller
6097  Node* src_start  = src;
6098  Node* dest_start = dest;
6099  if (src_offset != NULL || dest_offset != NULL) {
6100    assert(src_offset != NULL && dest_offset != NULL, "");
6101    src_start  = array_element_address(src,  src_offset,  T_BYTE);
6102    dest_start = array_element_address(dest, dest_offset, T_BYTE);
6103  }
6104
6105  // if we are in this set of code, we "know" the embeddedCipher is an AESCrypt object
6106  // (because of the predicated logic executed earlier).
6107  // so we cast it here safely.
6108  // this requires a newer class file that has this array as littleEndian ints, otherwise we revert to java
6109
6110  Node* embeddedCipherObj = load_field_from_object(cipherBlockChaining_object, "embeddedCipher", "Lcom/sun/crypto/provider/SymmetricCipher;", /*is_exact*/ false);
6111  if (embeddedCipherObj == NULL) return false;
6112
6113  // cast it to what we know it will be at runtime
6114  const TypeInstPtr* tinst = _gvn.type(cipherBlockChaining_object)->isa_instptr();
6115  assert(tinst != NULL, "CBC obj is null");
6116  assert(tinst->klass()->is_loaded(), "CBC obj is not loaded");
6117  ciKlass* klass_AESCrypt = tinst->klass()->as_instance_klass()->find_klass(ciSymbol::make("com/sun/crypto/provider/AESCrypt"));
6118  assert(klass_AESCrypt->is_loaded(), "predicate checks that this class is loaded");
6119
6120  ciInstanceKlass* instklass_AESCrypt = klass_AESCrypt->as_instance_klass();
6121  const TypeKlassPtr* aklass = TypeKlassPtr::make(instklass_AESCrypt);
6122  const TypeOopPtr* xtype = aklass->as_instance_type();
6123  Node* aescrypt_object = new CheckCastPPNode(control(), embeddedCipherObj, xtype);
6124  aescrypt_object = _gvn.transform(aescrypt_object);
6125
6126  // we need to get the start of the aescrypt_object's expanded key array
6127  Node* k_start = get_key_start_from_aescrypt_object(aescrypt_object);
6128  if (k_start == NULL) return false;
6129
6130  // similarly, get the start address of the r vector
6131  Node* objRvec = load_field_from_object(cipherBlockChaining_object, "r", "[B", /*is_exact*/ false);
6132  if (objRvec == NULL) return false;
6133  Node* r_start = array_element_address(objRvec, intcon(0), T_BYTE);
6134
6135  Node* cbcCrypt;
6136  if (Matcher::pass_original_key_for_aes()) {
6137    // on SPARC we need to pass the original key since key expansion needs to happen in intrinsics due to
6138    // compatibility issues between Java key expansion and SPARC crypto instructions
6139    Node* original_k_start = get_original_key_start_from_aescrypt_object(aescrypt_object);
6140    if (original_k_start == NULL) return false;
6141
6142    // Call the stub, passing src_start, dest_start, k_start, r_start, src_len and original_k_start
6143    cbcCrypt = make_runtime_call(RC_LEAF|RC_NO_FP,
6144                                 OptoRuntime::cipherBlockChaining_aescrypt_Type(),
6145                                 stubAddr, stubName, TypePtr::BOTTOM,
6146                                 src_start, dest_start, k_start, r_start, len, original_k_start);
6147  } else {
6148    // Call the stub, passing src_start, dest_start, k_start, r_start and src_len
6149    cbcCrypt = make_runtime_call(RC_LEAF|RC_NO_FP,
6150                                 OptoRuntime::cipherBlockChaining_aescrypt_Type(),
6151                                 stubAddr, stubName, TypePtr::BOTTOM,
6152                                 src_start, dest_start, k_start, r_start, len);
6153  }
6154
6155  // return cipher length (int)
6156  Node* retvalue = _gvn.transform(new ProjNode(cbcCrypt, TypeFunc::Parms));
6157  set_result(retvalue);
6158  return true;
6159}
6160
6161//------------------------------inline_counterMode_AESCrypt-----------------------
6162bool LibraryCallKit::inline_counterMode_AESCrypt(vmIntrinsics::ID id) {
6163  assert(UseAES, "need AES instruction support");
6164  if (!UseAESCTRIntrinsics) return false;
6165
6166  address stubAddr = NULL;
6167  const char *stubName = NULL;
6168  if (id == vmIntrinsics::_counterMode_AESCrypt) {
6169    stubAddr = StubRoutines::counterMode_AESCrypt();
6170    stubName = "counterMode_AESCrypt";
6171  }
6172  if (stubAddr == NULL) return false;
6173
6174  Node* counterMode_object = argument(0);
6175  Node* src = argument(1);
6176  Node* src_offset = argument(2);
6177  Node* len = argument(3);
6178  Node* dest = argument(4);
6179  Node* dest_offset = argument(5);
6180
6181  // (1) src and dest are arrays.
6182  const Type* src_type = src->Value(&_gvn);
6183  const Type* dest_type = dest->Value(&_gvn);
6184  const TypeAryPtr* top_src = src_type->isa_aryptr();
6185  const TypeAryPtr* top_dest = dest_type->isa_aryptr();
6186  assert(top_src != NULL && top_src->klass() != NULL &&
6187         top_dest != NULL && top_dest->klass() != NULL, "args are strange");
6188
6189  // checks are the responsibility of the caller
6190  Node* src_start = src;
6191  Node* dest_start = dest;
6192  if (src_offset != NULL || dest_offset != NULL) {
6193    assert(src_offset != NULL && dest_offset != NULL, "");
6194    src_start = array_element_address(src, src_offset, T_BYTE);
6195    dest_start = array_element_address(dest, dest_offset, T_BYTE);
6196  }
6197
6198  // if we are in this set of code, we "know" the embeddedCipher is an AESCrypt object
6199  // (because of the predicated logic executed earlier).
6200  // so we cast it here safely.
6201  // this requires a newer class file that has this array as littleEndian ints, otherwise we revert to java
6202  Node* embeddedCipherObj = load_field_from_object(counterMode_object, "embeddedCipher", "Lcom/sun/crypto/provider/SymmetricCipher;", /*is_exact*/ false);
6203  if (embeddedCipherObj == NULL) return false;
6204  // cast it to what we know it will be at runtime
6205  const TypeInstPtr* tinst = _gvn.type(counterMode_object)->isa_instptr();
6206  assert(tinst != NULL, "CTR obj is null");
6207  assert(tinst->klass()->is_loaded(), "CTR obj is not loaded");
6208  ciKlass* klass_AESCrypt = tinst->klass()->as_instance_klass()->find_klass(ciSymbol::make("com/sun/crypto/provider/AESCrypt"));
6209  assert(klass_AESCrypt->is_loaded(), "predicate checks that this class is loaded");
6210  ciInstanceKlass* instklass_AESCrypt = klass_AESCrypt->as_instance_klass();
6211  const TypeKlassPtr* aklass = TypeKlassPtr::make(instklass_AESCrypt);
6212  const TypeOopPtr* xtype = aklass->as_instance_type();
6213  Node* aescrypt_object = new CheckCastPPNode(control(), embeddedCipherObj, xtype);
6214  aescrypt_object = _gvn.transform(aescrypt_object);
6215  // we need to get the start of the aescrypt_object's expanded key array
6216  Node* k_start = get_key_start_from_aescrypt_object(aescrypt_object);
6217  if (k_start == NULL) return false;
6218  // similarly, get the start address of the r vector
6219  Node* obj_counter = load_field_from_object(counterMode_object, "counter", "[B", /*is_exact*/ false);
6220  if (obj_counter == NULL) return false;
6221  Node* cnt_start = array_element_address(obj_counter, intcon(0), T_BYTE);
6222
6223  Node* saved_encCounter = load_field_from_object(counterMode_object, "encryptedCounter", "[B", /*is_exact*/ false);
6224  if (saved_encCounter == NULL) return false;
6225  Node* saved_encCounter_start = array_element_address(saved_encCounter, intcon(0), T_BYTE);
6226  Node* used = field_address_from_object(counterMode_object, "used", "I", /*is_exact*/ false);
6227
6228  Node* ctrCrypt;
6229  if (Matcher::pass_original_key_for_aes()) {
6230    // no SPARC version for AES/CTR intrinsics now.
6231    return false;
6232  }
6233  // Call the stub, passing src_start, dest_start, k_start, r_start and src_len
6234  ctrCrypt = make_runtime_call(RC_LEAF|RC_NO_FP,
6235                               OptoRuntime::counterMode_aescrypt_Type(),
6236                               stubAddr, stubName, TypePtr::BOTTOM,
6237                               src_start, dest_start, k_start, cnt_start, len, saved_encCounter_start, used);
6238
6239  // return cipher length (int)
6240  Node* retvalue = _gvn.transform(new ProjNode(ctrCrypt, TypeFunc::Parms));
6241  set_result(retvalue);
6242  return true;
6243}
6244
6245//------------------------------get_key_start_from_aescrypt_object-----------------------
6246Node * LibraryCallKit::get_key_start_from_aescrypt_object(Node *aescrypt_object) {
6247#ifdef PPC64
6248  // MixColumns for decryption can be reduced by preprocessing MixColumns with round keys.
6249  // Intel's extention is based on this optimization and AESCrypt generates round keys by preprocessing MixColumns.
6250  // However, ppc64 vncipher processes MixColumns and requires the same round keys with encryption.
6251  // The ppc64 stubs of encryption and decryption use the same round keys (sessionK[0]).
6252  Node* objSessionK = load_field_from_object(aescrypt_object, "sessionK", "[[I", /*is_exact*/ false);
6253  assert (objSessionK != NULL, "wrong version of com.sun.crypto.provider.AESCrypt");
6254  if (objSessionK == NULL) {
6255    return (Node *) NULL;
6256  }
6257  Node* objAESCryptKey = load_array_element(control(), objSessionK, intcon(0), TypeAryPtr::OOPS);
6258#else
6259  Node* objAESCryptKey = load_field_from_object(aescrypt_object, "K", "[I", /*is_exact*/ false);
6260#endif // PPC64
6261  assert (objAESCryptKey != NULL, "wrong version of com.sun.crypto.provider.AESCrypt");
6262  if (objAESCryptKey == NULL) return (Node *) NULL;
6263
6264  // now have the array, need to get the start address of the K array
6265  Node* k_start = array_element_address(objAESCryptKey, intcon(0), T_INT);
6266  return k_start;
6267}
6268
6269//------------------------------get_original_key_start_from_aescrypt_object-----------------------
6270Node * LibraryCallKit::get_original_key_start_from_aescrypt_object(Node *aescrypt_object) {
6271  Node* objAESCryptKey = load_field_from_object(aescrypt_object, "lastKey", "[B", /*is_exact*/ false);
6272  assert (objAESCryptKey != NULL, "wrong version of com.sun.crypto.provider.AESCrypt");
6273  if (objAESCryptKey == NULL) return (Node *) NULL;
6274
6275  // now have the array, need to get the start address of the lastKey array
6276  Node* original_k_start = array_element_address(objAESCryptKey, intcon(0), T_BYTE);
6277  return original_k_start;
6278}
6279
6280//----------------------------inline_cipherBlockChaining_AESCrypt_predicate----------------------------
6281// Return node representing slow path of predicate check.
6282// the pseudo code we want to emulate with this predicate is:
6283// for encryption:
6284//    if (embeddedCipherObj instanceof AESCrypt) do_intrinsic, else do_javapath
6285// for decryption:
6286//    if ((embeddedCipherObj instanceof AESCrypt) && (cipher!=plain)) do_intrinsic, else do_javapath
6287//    note cipher==plain is more conservative than the original java code but that's OK
6288//
6289Node* LibraryCallKit::inline_cipherBlockChaining_AESCrypt_predicate(bool decrypting) {
6290  // The receiver was checked for NULL already.
6291  Node* objCBC = argument(0);
6292
6293  // Load embeddedCipher field of CipherBlockChaining object.
6294  Node* embeddedCipherObj = load_field_from_object(objCBC, "embeddedCipher", "Lcom/sun/crypto/provider/SymmetricCipher;", /*is_exact*/ false);
6295
6296  // get AESCrypt klass for instanceOf check
6297  // AESCrypt might not be loaded yet if some other SymmetricCipher got us to this compile point
6298  // will have same classloader as CipherBlockChaining object
6299  const TypeInstPtr* tinst = _gvn.type(objCBC)->isa_instptr();
6300  assert(tinst != NULL, "CBCobj is null");
6301  assert(tinst->klass()->is_loaded(), "CBCobj is not loaded");
6302
6303  // we want to do an instanceof comparison against the AESCrypt class
6304  ciKlass* klass_AESCrypt = tinst->klass()->as_instance_klass()->find_klass(ciSymbol::make("com/sun/crypto/provider/AESCrypt"));
6305  if (!klass_AESCrypt->is_loaded()) {
6306    // if AESCrypt is not even loaded, we never take the intrinsic fast path
6307    Node* ctrl = control();
6308    set_control(top()); // no regular fast path
6309    return ctrl;
6310  }
6311  ciInstanceKlass* instklass_AESCrypt = klass_AESCrypt->as_instance_klass();
6312
6313  Node* instof = gen_instanceof(embeddedCipherObj, makecon(TypeKlassPtr::make(instklass_AESCrypt)));
6314  Node* cmp_instof  = _gvn.transform(new CmpINode(instof, intcon(1)));
6315  Node* bool_instof  = _gvn.transform(new BoolNode(cmp_instof, BoolTest::ne));
6316
6317  Node* instof_false = generate_guard(bool_instof, NULL, PROB_MIN);
6318
6319  // for encryption, we are done
6320  if (!decrypting)
6321    return instof_false;  // even if it is NULL
6322
6323  // for decryption, we need to add a further check to avoid
6324  // taking the intrinsic path when cipher and plain are the same
6325  // see the original java code for why.
6326  RegionNode* region = new RegionNode(3);
6327  region->init_req(1, instof_false);
6328  Node* src = argument(1);
6329  Node* dest = argument(4);
6330  Node* cmp_src_dest = _gvn.transform(new CmpPNode(src, dest));
6331  Node* bool_src_dest = _gvn.transform(new BoolNode(cmp_src_dest, BoolTest::eq));
6332  Node* src_dest_conjoint = generate_guard(bool_src_dest, NULL, PROB_MIN);
6333  region->init_req(2, src_dest_conjoint);
6334
6335  record_for_igvn(region);
6336  return _gvn.transform(region);
6337}
6338
6339//----------------------------inline_counterMode_AESCrypt_predicate----------------------------
6340// Return node representing slow path of predicate check.
6341// the pseudo code we want to emulate with this predicate is:
6342// for encryption:
6343//    if (embeddedCipherObj instanceof AESCrypt) do_intrinsic, else do_javapath
6344// for decryption:
6345//    if ((embeddedCipherObj instanceof AESCrypt) && (cipher!=plain)) do_intrinsic, else do_javapath
6346//    note cipher==plain is more conservative than the original java code but that's OK
6347//
6348
6349Node* LibraryCallKit::inline_counterMode_AESCrypt_predicate() {
6350  // The receiver was checked for NULL already.
6351  Node* objCTR = argument(0);
6352
6353  // Load embeddedCipher field of CipherBlockChaining object.
6354  Node* embeddedCipherObj = load_field_from_object(objCTR, "embeddedCipher", "Lcom/sun/crypto/provider/SymmetricCipher;", /*is_exact*/ false);
6355
6356  // get AESCrypt klass for instanceOf check
6357  // AESCrypt might not be loaded yet if some other SymmetricCipher got us to this compile point
6358  // will have same classloader as CipherBlockChaining object
6359  const TypeInstPtr* tinst = _gvn.type(objCTR)->isa_instptr();
6360  assert(tinst != NULL, "CTRobj is null");
6361  assert(tinst->klass()->is_loaded(), "CTRobj is not loaded");
6362
6363  // we want to do an instanceof comparison against the AESCrypt class
6364  ciKlass* klass_AESCrypt = tinst->klass()->as_instance_klass()->find_klass(ciSymbol::make("com/sun/crypto/provider/AESCrypt"));
6365  if (!klass_AESCrypt->is_loaded()) {
6366    // if AESCrypt is not even loaded, we never take the intrinsic fast path
6367    Node* ctrl = control();
6368    set_control(top()); // no regular fast path
6369    return ctrl;
6370  }
6371
6372  ciInstanceKlass* instklass_AESCrypt = klass_AESCrypt->as_instance_klass();
6373  Node* instof = gen_instanceof(embeddedCipherObj, makecon(TypeKlassPtr::make(instklass_AESCrypt)));
6374  Node* cmp_instof = _gvn.transform(new CmpINode(instof, intcon(1)));
6375  Node* bool_instof = _gvn.transform(new BoolNode(cmp_instof, BoolTest::ne));
6376  Node* instof_false = generate_guard(bool_instof, NULL, PROB_MIN);
6377
6378  return instof_false; // even if it is NULL
6379}
6380
6381//------------------------------inline_ghash_processBlocks
6382bool LibraryCallKit::inline_ghash_processBlocks() {
6383  address stubAddr;
6384  const char *stubName;
6385  assert(UseGHASHIntrinsics, "need GHASH intrinsics support");
6386
6387  stubAddr = StubRoutines::ghash_processBlocks();
6388  stubName = "ghash_processBlocks";
6389
6390  Node* data           = argument(0);
6391  Node* offset         = argument(1);
6392  Node* len            = argument(2);
6393  Node* state          = argument(3);
6394  Node* subkeyH        = argument(4);
6395
6396  Node* state_start  = array_element_address(state, intcon(0), T_LONG);
6397  assert(state_start, "state is NULL");
6398  Node* subkeyH_start  = array_element_address(subkeyH, intcon(0), T_LONG);
6399  assert(subkeyH_start, "subkeyH is NULL");
6400  Node* data_start  = array_element_address(data, offset, T_BYTE);
6401  assert(data_start, "data is NULL");
6402
6403  Node* ghash = make_runtime_call(RC_LEAF|RC_NO_FP,
6404                                  OptoRuntime::ghash_processBlocks_Type(),
6405                                  stubAddr, stubName, TypePtr::BOTTOM,
6406                                  state_start, subkeyH_start, data_start, len);
6407  return true;
6408}
6409
6410//------------------------------inline_sha_implCompress-----------------------
6411//
6412// Calculate SHA (i.e., SHA-1) for single-block byte[] array.
6413// void com.sun.security.provider.SHA.implCompress(byte[] buf, int ofs)
6414//
6415// Calculate SHA2 (i.e., SHA-244 or SHA-256) for single-block byte[] array.
6416// void com.sun.security.provider.SHA2.implCompress(byte[] buf, int ofs)
6417//
6418// Calculate SHA5 (i.e., SHA-384 or SHA-512) for single-block byte[] array.
6419// void com.sun.security.provider.SHA5.implCompress(byte[] buf, int ofs)
6420//
6421bool LibraryCallKit::inline_sha_implCompress(vmIntrinsics::ID id) {
6422  assert(callee()->signature()->size() == 2, "sha_implCompress has 2 parameters");
6423
6424  Node* sha_obj = argument(0);
6425  Node* src     = argument(1); // type oop
6426  Node* ofs     = argument(2); // type int
6427
6428  const Type* src_type = src->Value(&_gvn);
6429  const TypeAryPtr* top_src = src_type->isa_aryptr();
6430  if (top_src  == NULL || top_src->klass()  == NULL) {
6431    // failed array check
6432    return false;
6433  }
6434  // Figure out the size and type of the elements we will be copying.
6435  BasicType src_elem = src_type->isa_aryptr()->klass()->as_array_klass()->element_type()->basic_type();
6436  if (src_elem != T_BYTE) {
6437    return false;
6438  }
6439  // 'src_start' points to src array + offset
6440  Node* src_start = array_element_address(src, ofs, src_elem);
6441  Node* state = NULL;
6442  address stubAddr;
6443  const char *stubName;
6444
6445  switch(id) {
6446  case vmIntrinsics::_sha_implCompress:
6447    assert(UseSHA1Intrinsics, "need SHA1 instruction support");
6448    state = get_state_from_sha_object(sha_obj);
6449    stubAddr = StubRoutines::sha1_implCompress();
6450    stubName = "sha1_implCompress";
6451    break;
6452  case vmIntrinsics::_sha2_implCompress:
6453    assert(UseSHA256Intrinsics, "need SHA256 instruction support");
6454    state = get_state_from_sha_object(sha_obj);
6455    stubAddr = StubRoutines::sha256_implCompress();
6456    stubName = "sha256_implCompress";
6457    break;
6458  case vmIntrinsics::_sha5_implCompress:
6459    assert(UseSHA512Intrinsics, "need SHA512 instruction support");
6460    state = get_state_from_sha5_object(sha_obj);
6461    stubAddr = StubRoutines::sha512_implCompress();
6462    stubName = "sha512_implCompress";
6463    break;
6464  default:
6465    fatal_unexpected_iid(id);
6466    return false;
6467  }
6468  if (state == NULL) return false;
6469
6470  // Call the stub.
6471  Node* call = make_runtime_call(RC_LEAF|RC_NO_FP, OptoRuntime::sha_implCompress_Type(),
6472                                 stubAddr, stubName, TypePtr::BOTTOM,
6473                                 src_start, state);
6474
6475  return true;
6476}
6477
6478//------------------------------inline_digestBase_implCompressMB-----------------------
6479//
6480// Calculate SHA/SHA2/SHA5 for multi-block byte[] array.
6481// int com.sun.security.provider.DigestBase.implCompressMultiBlock(byte[] b, int ofs, int limit)
6482//
6483bool LibraryCallKit::inline_digestBase_implCompressMB(int predicate) {
6484  assert(UseSHA1Intrinsics || UseSHA256Intrinsics || UseSHA512Intrinsics,
6485         "need SHA1/SHA256/SHA512 instruction support");
6486  assert((uint)predicate < 3, "sanity");
6487  assert(callee()->signature()->size() == 3, "digestBase_implCompressMB has 3 parameters");
6488
6489  Node* digestBase_obj = argument(0); // The receiver was checked for NULL already.
6490  Node* src            = argument(1); // byte[] array
6491  Node* ofs            = argument(2); // type int
6492  Node* limit          = argument(3); // type int
6493
6494  const Type* src_type = src->Value(&_gvn);
6495  const TypeAryPtr* top_src = src_type->isa_aryptr();
6496  if (top_src  == NULL || top_src->klass()  == NULL) {
6497    // failed array check
6498    return false;
6499  }
6500  // Figure out the size and type of the elements we will be copying.
6501  BasicType src_elem = src_type->isa_aryptr()->klass()->as_array_klass()->element_type()->basic_type();
6502  if (src_elem != T_BYTE) {
6503    return false;
6504  }
6505  // 'src_start' points to src array + offset
6506  Node* src_start = array_element_address(src, ofs, src_elem);
6507
6508  const char* klass_SHA_name = NULL;
6509  const char* stub_name = NULL;
6510  address     stub_addr = NULL;
6511  bool        long_state = false;
6512
6513  switch (predicate) {
6514  case 0:
6515    if (UseSHA1Intrinsics) {
6516      klass_SHA_name = "sun/security/provider/SHA";
6517      stub_name = "sha1_implCompressMB";
6518      stub_addr = StubRoutines::sha1_implCompressMB();
6519    }
6520    break;
6521  case 1:
6522    if (UseSHA256Intrinsics) {
6523      klass_SHA_name = "sun/security/provider/SHA2";
6524      stub_name = "sha256_implCompressMB";
6525      stub_addr = StubRoutines::sha256_implCompressMB();
6526    }
6527    break;
6528  case 2:
6529    if (UseSHA512Intrinsics) {
6530      klass_SHA_name = "sun/security/provider/SHA5";
6531      stub_name = "sha512_implCompressMB";
6532      stub_addr = StubRoutines::sha512_implCompressMB();
6533      long_state = true;
6534    }
6535    break;
6536  default:
6537    fatal("unknown SHA intrinsic predicate: %d", predicate);
6538  }
6539  if (klass_SHA_name != NULL) {
6540    // get DigestBase klass to lookup for SHA klass
6541    const TypeInstPtr* tinst = _gvn.type(digestBase_obj)->isa_instptr();
6542    assert(tinst != NULL, "digestBase_obj is not instance???");
6543    assert(tinst->klass()->is_loaded(), "DigestBase is not loaded");
6544
6545    ciKlass* klass_SHA = tinst->klass()->as_instance_klass()->find_klass(ciSymbol::make(klass_SHA_name));
6546    assert(klass_SHA->is_loaded(), "predicate checks that this class is loaded");
6547    ciInstanceKlass* instklass_SHA = klass_SHA->as_instance_klass();
6548    return inline_sha_implCompressMB(digestBase_obj, instklass_SHA, long_state, stub_addr, stub_name, src_start, ofs, limit);
6549  }
6550  return false;
6551}
6552//------------------------------inline_sha_implCompressMB-----------------------
6553bool LibraryCallKit::inline_sha_implCompressMB(Node* digestBase_obj, ciInstanceKlass* instklass_SHA,
6554                                               bool long_state, address stubAddr, const char *stubName,
6555                                               Node* src_start, Node* ofs, Node* limit) {
6556  const TypeKlassPtr* aklass = TypeKlassPtr::make(instklass_SHA);
6557  const TypeOopPtr* xtype = aklass->as_instance_type();
6558  Node* sha_obj = new CheckCastPPNode(control(), digestBase_obj, xtype);
6559  sha_obj = _gvn.transform(sha_obj);
6560
6561  Node* state;
6562  if (long_state) {
6563    state = get_state_from_sha5_object(sha_obj);
6564  } else {
6565    state = get_state_from_sha_object(sha_obj);
6566  }
6567  if (state == NULL) return false;
6568
6569  // Call the stub.
6570  Node* call = make_runtime_call(RC_LEAF|RC_NO_FP,
6571                                 OptoRuntime::digestBase_implCompressMB_Type(),
6572                                 stubAddr, stubName, TypePtr::BOTTOM,
6573                                 src_start, state, ofs, limit);
6574  // return ofs (int)
6575  Node* result = _gvn.transform(new ProjNode(call, TypeFunc::Parms));
6576  set_result(result);
6577
6578  return true;
6579}
6580
6581//------------------------------get_state_from_sha_object-----------------------
6582Node * LibraryCallKit::get_state_from_sha_object(Node *sha_object) {
6583  Node* sha_state = load_field_from_object(sha_object, "state", "[I", /*is_exact*/ false);
6584  assert (sha_state != NULL, "wrong version of sun.security.provider.SHA/SHA2");
6585  if (sha_state == NULL) return (Node *) NULL;
6586
6587  // now have the array, need to get the start address of the state array
6588  Node* state = array_element_address(sha_state, intcon(0), T_INT);
6589  return state;
6590}
6591
6592//------------------------------get_state_from_sha5_object-----------------------
6593Node * LibraryCallKit::get_state_from_sha5_object(Node *sha_object) {
6594  Node* sha_state = load_field_from_object(sha_object, "state", "[J", /*is_exact*/ false);
6595  assert (sha_state != NULL, "wrong version of sun.security.provider.SHA5");
6596  if (sha_state == NULL) return (Node *) NULL;
6597
6598  // now have the array, need to get the start address of the state array
6599  Node* state = array_element_address(sha_state, intcon(0), T_LONG);
6600  return state;
6601}
6602
6603//----------------------------inline_digestBase_implCompressMB_predicate----------------------------
6604// Return node representing slow path of predicate check.
6605// the pseudo code we want to emulate with this predicate is:
6606//    if (digestBaseObj instanceof SHA/SHA2/SHA5) do_intrinsic, else do_javapath
6607//
6608Node* LibraryCallKit::inline_digestBase_implCompressMB_predicate(int predicate) {
6609  assert(UseSHA1Intrinsics || UseSHA256Intrinsics || UseSHA512Intrinsics,
6610         "need SHA1/SHA256/SHA512 instruction support");
6611  assert((uint)predicate < 3, "sanity");
6612
6613  // The receiver was checked for NULL already.
6614  Node* digestBaseObj = argument(0);
6615
6616  // get DigestBase klass for instanceOf check
6617  const TypeInstPtr* tinst = _gvn.type(digestBaseObj)->isa_instptr();
6618  assert(tinst != NULL, "digestBaseObj is null");
6619  assert(tinst->klass()->is_loaded(), "DigestBase is not loaded");
6620
6621  const char* klass_SHA_name = NULL;
6622  switch (predicate) {
6623  case 0:
6624    if (UseSHA1Intrinsics) {
6625      // we want to do an instanceof comparison against the SHA class
6626      klass_SHA_name = "sun/security/provider/SHA";
6627    }
6628    break;
6629  case 1:
6630    if (UseSHA256Intrinsics) {
6631      // we want to do an instanceof comparison against the SHA2 class
6632      klass_SHA_name = "sun/security/provider/SHA2";
6633    }
6634    break;
6635  case 2:
6636    if (UseSHA512Intrinsics) {
6637      // we want to do an instanceof comparison against the SHA5 class
6638      klass_SHA_name = "sun/security/provider/SHA5";
6639    }
6640    break;
6641  default:
6642    fatal("unknown SHA intrinsic predicate: %d", predicate);
6643  }
6644
6645  ciKlass* klass_SHA = NULL;
6646  if (klass_SHA_name != NULL) {
6647    klass_SHA = tinst->klass()->as_instance_klass()->find_klass(ciSymbol::make(klass_SHA_name));
6648  }
6649  if ((klass_SHA == NULL) || !klass_SHA->is_loaded()) {
6650    // if none of SHA/SHA2/SHA5 is loaded, we never take the intrinsic fast path
6651    Node* ctrl = control();
6652    set_control(top()); // no intrinsic path
6653    return ctrl;
6654  }
6655  ciInstanceKlass* instklass_SHA = klass_SHA->as_instance_klass();
6656
6657  Node* instofSHA = gen_instanceof(digestBaseObj, makecon(TypeKlassPtr::make(instklass_SHA)));
6658  Node* cmp_instof = _gvn.transform(new CmpINode(instofSHA, intcon(1)));
6659  Node* bool_instof = _gvn.transform(new BoolNode(cmp_instof, BoolTest::ne));
6660  Node* instof_false = generate_guard(bool_instof, NULL, PROB_MIN);
6661
6662  return instof_false;  // even if it is NULL
6663}
6664
6665//-------------inline_fma-----------------------------------
6666bool LibraryCallKit::inline_fma(vmIntrinsics::ID id) {
6667  Node *a = NULL;
6668  Node *b = NULL;
6669  Node *c = NULL;
6670  Node* result = NULL;
6671  switch (id) {
6672  case vmIntrinsics::_fmaD:
6673    assert(callee()->signature()->size() == 6, "fma has 3 parameters of size 2 each.");
6674    // no receiver since it is static method
6675    a = round_double_node(argument(0));
6676    b = round_double_node(argument(2));
6677    c = round_double_node(argument(4));
6678    result = _gvn.transform(new FmaDNode(control(), a, b, c));
6679    break;
6680  case vmIntrinsics::_fmaF:
6681    assert(callee()->signature()->size() == 3, "fma has 3 parameters of size 1 each.");
6682    a = argument(0);
6683    b = argument(1);
6684    c = argument(2);
6685    result = _gvn.transform(new FmaFNode(control(), a, b, c));
6686    break;
6687  default:
6688    fatal_unexpected_iid(id);  break;
6689  }
6690  set_result(result);
6691  return true;
6692}
6693
6694bool LibraryCallKit::inline_profileBoolean() {
6695  Node* counts = argument(1);
6696  const TypeAryPtr* ary = NULL;
6697  ciArray* aobj = NULL;
6698  if (counts->is_Con()
6699      && (ary = counts->bottom_type()->isa_aryptr()) != NULL
6700      && (aobj = ary->const_oop()->as_array()) != NULL
6701      && (aobj->length() == 2)) {
6702    // Profile is int[2] where [0] and [1] correspond to false and true value occurrences respectively.
6703    jint false_cnt = aobj->element_value(0).as_int();
6704    jint  true_cnt = aobj->element_value(1).as_int();
6705
6706    if (C->log() != NULL) {
6707      C->log()->elem("observe source='profileBoolean' false='%d' true='%d'",
6708                     false_cnt, true_cnt);
6709    }
6710
6711    if (false_cnt + true_cnt == 0) {
6712      // According to profile, never executed.
6713      uncommon_trap_exact(Deoptimization::Reason_intrinsic,
6714                          Deoptimization::Action_reinterpret);
6715      return true;
6716    }
6717
6718    // result is a boolean (0 or 1) and its profile (false_cnt & true_cnt)
6719    // is a number of each value occurrences.
6720    Node* result = argument(0);
6721    if (false_cnt == 0 || true_cnt == 0) {
6722      // According to profile, one value has been never seen.
6723      int expected_val = (false_cnt == 0) ? 1 : 0;
6724
6725      Node* cmp  = _gvn.transform(new CmpINode(result, intcon(expected_val)));
6726      Node* test = _gvn.transform(new BoolNode(cmp, BoolTest::eq));
6727
6728      IfNode* check = create_and_map_if(control(), test, PROB_ALWAYS, COUNT_UNKNOWN);
6729      Node* fast_path = _gvn.transform(new IfTrueNode(check));
6730      Node* slow_path = _gvn.transform(new IfFalseNode(check));
6731
6732      { // Slow path: uncommon trap for never seen value and then reexecute
6733        // MethodHandleImpl::profileBoolean() to bump the count, so JIT knows
6734        // the value has been seen at least once.
6735        PreserveJVMState pjvms(this);
6736        PreserveReexecuteState preexecs(this);
6737        jvms()->set_should_reexecute(true);
6738
6739        set_control(slow_path);
6740        set_i_o(i_o());
6741
6742        uncommon_trap_exact(Deoptimization::Reason_intrinsic,
6743                            Deoptimization::Action_reinterpret);
6744      }
6745      // The guard for never seen value enables sharpening of the result and
6746      // returning a constant. It allows to eliminate branches on the same value
6747      // later on.
6748      set_control(fast_path);
6749      result = intcon(expected_val);
6750    }
6751    // Stop profiling.
6752    // MethodHandleImpl::profileBoolean() has profiling logic in its bytecode.
6753    // By replacing method body with profile data (represented as ProfileBooleanNode
6754    // on IR level) we effectively disable profiling.
6755    // It enables full speed execution once optimized code is generated.
6756    Node* profile = _gvn.transform(new ProfileBooleanNode(result, false_cnt, true_cnt));
6757    C->record_for_igvn(profile);
6758    set_result(profile);
6759    return true;
6760  } else {
6761    // Continue profiling.
6762    // Profile data isn't available at the moment. So, execute method's bytecode version.
6763    // Usually, when GWT LambdaForms are profiled it means that a stand-alone nmethod
6764    // is compiled and counters aren't available since corresponding MethodHandle
6765    // isn't a compile-time constant.
6766    return false;
6767  }
6768}
6769
6770bool LibraryCallKit::inline_isCompileConstant() {
6771  Node* n = argument(0);
6772  set_result(n->is_Con() ? intcon(1) : intcon(0));
6773  return true;
6774}
6775