1 /*
   2  * Copyright (c) 2005, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #ifndef SHARE_VM_OPTO_MACRO_HPP
  26 #define SHARE_VM_OPTO_MACRO_HPP
  27 
  28 #include "opto/phase.hpp"
  29 
  30 class  AllocateNode;
  31 class  AllocateArrayNode;
  32 class  CallNode;
  33 class  Node;
  34 class  PhaseIterGVN;
  35 
  36 class PhaseMacroExpand : public Phase {
  37 private:
  38   PhaseIterGVN &_igvn;
  39 
  40   // Helper methods roughly modeled after GraphKit:
  41   Node* top()                   const { return C->top(); }
  42   Node* intcon(jint con)        const { return _igvn.intcon(con); }
  43   Node* longcon(jlong con)      const { return _igvn.longcon(con); }
  44   Node* makecon(const Type *t)  const { return _igvn.makecon(t); }
  45 
  46   Node* basic_plus_adr(Node* base, int offset) {
  47     return (offset == 0)? base: basic_plus_adr(base, MakeConX(offset));
  48   }
  49   Node* basic_plus_adr(Node* base, Node* ptr, int offset) {
  50     return (offset == 0)? ptr: basic_plus_adr(base, ptr, MakeConX(offset));
  51   }
  52   Node* basic_plus_adr(Node* base, Node* offset) {
  53     return basic_plus_adr(base, base, offset);
  54   }
  55   Node* basic_plus_adr(Node* base, Node* ptr, Node* offset) {
  56     Node* adr = new AddPNode(base, ptr, offset);
  57     return transform_later(adr);
  58   }
  59   Node* transform_later(Node* n) {
  60     // equivalent to _gvn.transform in GraphKit, Ideal, etc.
  61     _igvn.register_new_node_with_optimizer(n);
  62     return n;
  63   }
  64   void set_eden_pointers(Node* &eden_top_adr, Node* &eden_end_adr);
  65   Node* make_load( Node* ctl, Node* mem, Node* base, int offset,
  66                    const Type* value_type, BasicType bt);
  67   Node* make_store(Node* ctl, Node* mem, Node* base, int offset,
  68                    Node* value, BasicType bt);
  69 
  70   // projections extracted from a call node
  71   ProjNode *_fallthroughproj;
  72   ProjNode *_fallthroughcatchproj;
  73   ProjNode *_ioproj_fallthrough;
  74   ProjNode *_ioproj_catchall;
  75   ProjNode *_catchallcatchproj;
  76   ProjNode *_memproj_fallthrough;
  77   ProjNode *_memproj_catchall;
  78   ProjNode *_resproj;
  79 
  80   // Additional data collected during macro expansion
  81   bool _has_locks;
  82 
  83   void expand_allocate(AllocateNode *alloc);
  84   void expand_allocate_array(AllocateArrayNode *alloc);
  85   void expand_allocate_common(AllocateNode* alloc,
  86                               Node* length,
  87                               const TypeFunc* slow_call_type,
  88                               address slow_call_address);
  89   Node *value_from_mem(Node *mem, Node *ctl, BasicType ft, const Type *ftype, const TypeOopPtr *adr_t, AllocateNode *alloc);
  90   Node *value_from_mem_phi(Node *mem, BasicType ft, const Type *ftype, const TypeOopPtr *adr_t, AllocateNode *alloc, Node_Stack *value_phis, int level);
  91 
  92   bool eliminate_boxing_node(CallStaticJavaNode *boxing);
  93   bool eliminate_allocate_node(AllocateNode *alloc);
  94   bool can_eliminate_allocation(AllocateNode *alloc, GrowableArray <SafePointNode *>& safepoints);
  95   bool scalar_replacement(AllocateNode *alloc, GrowableArray <SafePointNode *>& safepoints_done);
  96   void process_users_of_allocation(CallNode *alloc);
  97 
  98   void eliminate_card_mark(Node *cm);
  99   void mark_eliminated_box(Node* box, Node* obj);
 100   void mark_eliminated_locking_nodes(AbstractLockNode *alock);
 101   bool eliminate_locking_node(AbstractLockNode *alock);
 102   void expand_lock_node(LockNode *lock);
 103   void expand_unlock_node(UnlockNode *unlock);
 104 
 105   // More helper methods modeled after GraphKit for array copy
 106   void insert_mem_bar(Node** ctrl, Node** mem, int opcode, Node* precedent = NULL);
 107   Node* array_element_address(Node* ary, Node* idx, BasicType elembt);
 108   Node* ConvI2L(Node* offset);
 109   Node* make_leaf_call(Node* ctrl, Node* mem,
 110                        const TypeFunc* call_type, address call_addr,
 111                        const char* call_name,
 112                        const TypePtr* adr_type,
 113                        Node* parm0 = NULL, Node* parm1 = NULL,
 114                        Node* parm2 = NULL, Node* parm3 = NULL,
 115                        Node* parm4 = NULL, Node* parm5 = NULL,
 116                        Node* parm6 = NULL, Node* parm7 = NULL);
 117 
 118   // helper methods modeled after LibraryCallKit for array copy
 119   Node* generate_guard(Node** ctrl, Node* test, RegionNode* region, float true_prob);
 120   Node* generate_slow_guard(Node** ctrl, Node* test, RegionNode* region);
 121   void generate_negative_guard(Node** ctrl, Node* index, RegionNode* region);
 122   void generate_limit_guard(Node** ctrl, Node* offset, Node* subseq_length, Node* array_length, RegionNode* region);
 123 
 124   // More helper methods for array copy
 125   Node* generate_nonpositive_guard(Node** ctrl, Node* index, bool never_negative);
 126   void finish_arraycopy_call(Node* call, Node** ctrl, MergeMemNode** mem, const TypePtr* adr_type);
 127   address basictype2arraycopy(BasicType t,
 128                               Node* src_offset,
 129                               Node* dest_offset,
 130                               bool disjoint_bases,
 131                               const char* &name,
 132                               bool dest_uninitialized);
 133   Node* generate_arraycopy(ArrayCopyNode *ac,
 134                            AllocateArrayNode* alloc,
 135                            Node** ctrl, MergeMemNode* mem, Node** io,
 136                            const TypePtr* adr_type,
 137                            BasicType basic_elem_type,
 138                            Node* src,  Node* src_offset,
 139                            Node* dest, Node* dest_offset,
 140                            Node* copy_length,
 141                            bool disjoint_bases = false,
 142                            bool length_never_negative = false,
 143                            RegionNode* slow_region = NULL);
 144   void generate_clear_array(Node* ctrl, MergeMemNode* merge_mem,
 145                             const TypePtr* adr_type,
 146                             Node* dest,
 147                             BasicType basic_elem_type,
 148                             Node* slice_idx,
 149                             Node* slice_len,
 150                             Node* dest_size);
 151   bool generate_block_arraycopy(Node** ctrl, MergeMemNode** mem, Node* io,
 152                                 const TypePtr* adr_type,
 153                                 BasicType basic_elem_type,
 154                                 AllocateNode* alloc,
 155                                 Node* src,  Node* src_offset,
 156                                 Node* dest, Node* dest_offset,
 157                                 Node* dest_size, bool dest_uninitialized);
 158   MergeMemNode* generate_slow_arraycopy(ArrayCopyNode *ac,
 159                                         Node** ctrl, Node* mem, Node** io,
 160                                         const TypePtr* adr_type,
 161                                         Node* src,  Node* src_offset,
 162                                         Node* dest, Node* dest_offset,
 163                                         Node* copy_length, bool dest_uninitialized);
 164   Node* generate_checkcast_arraycopy(Node** ctrl, MergeMemNode** mem,
 165                                      const TypePtr* adr_type,
 166                                      Node* dest_elem_klass,
 167                                      Node* src,  Node* src_offset,
 168                                      Node* dest, Node* dest_offset,
 169                                      Node* copy_length, bool dest_uninitialized);
 170   Node* generate_generic_arraycopy(Node** ctrl, MergeMemNode** mem,
 171                                    const TypePtr* adr_type,
 172                                    Node* src,  Node* src_offset,
 173                                    Node* dest, Node* dest_offset,
 174                                    Node* copy_length, bool dest_uninitialized);
 175   void generate_unchecked_arraycopy(Node** ctrl, MergeMemNode** mem,
 176                                     const TypePtr* adr_type,
 177                                     BasicType basic_elem_type,
 178                                     bool disjoint_bases,
 179                                     Node* src,  Node* src_offset,
 180                                     Node* dest, Node* dest_offset,
 181                                     Node* copy_length, bool dest_uninitialized);
 182 
 183   void expand_arraycopy_node(ArrayCopyNode *ac);
 184 
 185   int replace_input(Node *use, Node *oldref, Node *newref);
 186   void copy_call_debug_info(CallNode *oldcall, CallNode * newcall);
 187   Node* opt_bits_test(Node* ctrl, Node* region, int edge, Node* word, int mask, int bits, bool return_fast_path = false);
 188   void copy_predefined_input_for_runtime_call(Node * ctrl, CallNode* oldcall, CallNode* call);
 189   CallNode* make_slow_call(CallNode *oldcall, const TypeFunc* slow_call_type, address slow_call,
 190                            const char* leaf_name, Node* slow_path, Node* parm0, Node* parm1,
 191                            Node* parm2);
 192   void extract_call_projections(CallNode *call);
 193 
 194   Node* initialize_object(AllocateNode* alloc,
 195                           Node* control, Node* rawmem, Node* object,
 196                           Node* klass_node, Node* length,
 197                           Node* size_in_bytes);
 198 
 199   Node* prefetch_allocation(Node* i_o,
 200                             Node*& needgc_false, Node*& contended_phi_rawmem,
 201                             Node* old_eden_top, Node* new_eden_top,
 202                             Node* length);
 203 
 204   Node* make_arraycopy_load(ArrayCopyNode* ac, intptr_t offset, Node* ctl, BasicType ft, const Type *ftype, AllocateNode *alloc);
 205 
 206 public:
 207   PhaseMacroExpand(PhaseIterGVN &igvn) : Phase(Macro_Expand), _igvn(igvn), _has_locks(false) {
 208     _igvn.set_delay_transform(true);
 209   }
 210   void eliminate_macro_nodes();
 211   bool expand_macro_nodes();
 212 
 213 };
 214 
 215 #endif // SHARE_VM_OPTO_MACRO_HPP