1 /*
   2  * Copyright (c) 1997, 2026, 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_OPTO_CALLNODE_HPP
  26 #define SHARE_OPTO_CALLNODE_HPP
  27 
  28 #include "opto/connode.hpp"
  29 #include "opto/mulnode.hpp"
  30 #include "opto/multnode.hpp"
  31 #include "opto/opcodes.hpp"
  32 #include "opto/phaseX.hpp"
  33 #include "opto/replacednodes.hpp"
  34 #include "opto/type.hpp"
  35 #include "utilities/growableArray.hpp"
  36 
  37 // Portions of code courtesy of Clifford Click
  38 
  39 // Optimization - Graph Style
  40 
  41 class NamedCounter;
  42 class MultiNode;
  43 class  SafePointNode;
  44 class   CallNode;
  45 class     CallJavaNode;
  46 class       CallStaticJavaNode;
  47 class       CallDynamicJavaNode;
  48 class     CallRuntimeNode;
  49 class       CallLeafNode;
  50 class         CallLeafNoFPNode;
  51 class         CallLeafVectorNode;
  52 class     AllocateNode;
  53 class       AllocateArrayNode;
  54 class     AbstractLockNode;
  55 class       LockNode;
  56 class       UnlockNode;
  57 class FastLockNode;
  58 
  59 //------------------------------StartNode--------------------------------------
  60 // The method start node
  61 class StartNode : public MultiNode {
  62   virtual bool cmp( const Node &n ) const;
  63   virtual uint size_of() const; // Size is bigger
  64 public:
  65   const TypeTuple *_domain;
  66   StartNode( Node *root, const TypeTuple *domain ) : MultiNode(2), _domain(domain) {
  67     init_class_id(Class_Start);
  68     init_req(0,this);
  69     init_req(1,root);
  70   }
  71   virtual int Opcode() const;
  72   virtual bool pinned() const { return true; };
  73   virtual const Type *bottom_type() const;
  74   virtual const TypePtr *adr_type() const { return TypePtr::BOTTOM; }
  75   virtual const Type* Value(PhaseGVN* phase) const;
  76   virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
  77   virtual void  calling_convention( BasicType* sig_bt, VMRegPair *parm_reg, uint length ) const;
  78   virtual const RegMask &in_RegMask(uint) const;
  79   virtual Node *match( const ProjNode *proj, const Matcher *m );
  80   virtual uint ideal_reg() const { return 0; }
  81 #ifndef PRODUCT
  82   virtual void  dump_spec(outputStream *st) const;
  83   virtual void  dump_compact_spec(outputStream *st) const;
  84 #endif
  85 };
  86 
  87 //------------------------------StartOSRNode-----------------------------------
  88 // The method start node for on stack replacement code
  89 class StartOSRNode : public StartNode {
  90 public:
  91   StartOSRNode( Node *root, const TypeTuple *domain ) : StartNode(root, domain) {}
  92   virtual int   Opcode() const;
  93   static  const TypeTuple *osr_domain();
  94 };
  95 
  96 
  97 //------------------------------ParmNode---------------------------------------
  98 // Incoming parameters
  99 class ParmNode : public ProjNode {
 100   static const char * const names[TypeFunc::Parms+1];
 101 public:
 102   ParmNode( StartNode *src, uint con ) : ProjNode(src,con) {
 103     init_class_id(Class_Parm);
 104   }
 105   virtual int Opcode() const;
 106   virtual bool  is_CFG() const { return (_con == TypeFunc::Control); }
 107   virtual uint ideal_reg() const;
 108 #ifndef PRODUCT
 109   virtual void dump_spec(outputStream *st) const;
 110   virtual void dump_compact_spec(outputStream *st) const;
 111 #endif
 112 };
 113 
 114 
 115 //------------------------------ReturnNode-------------------------------------
 116 // Return from subroutine node
 117 class ReturnNode : public Node {
 118 public:
 119   ReturnNode(uint edges, Node* cntrl, Node* i_o, Node* memory, Node* frameptr, Node* retadr);
 120   virtual int Opcode() const;
 121   virtual bool  is_CFG() const { return true; }
 122   virtual uint hash() const { return NO_HASH; }  // CFG nodes do not hash
 123   virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
 124   virtual const Type* Value(PhaseGVN* phase) const;
 125   virtual uint ideal_reg() const { return NotAMachineReg; }
 126   virtual uint match_edge(uint idx) const;
 127 #ifndef PRODUCT
 128   virtual void dump_req(outputStream *st = tty, DumpConfig* dc = nullptr) const;
 129 #endif
 130 };
 131 
 132 
 133 //------------------------------RethrowNode------------------------------------
 134 // Rethrow of exception at call site.  Ends a procedure before rethrowing;
 135 // ends the current basic block like a ReturnNode.  Restores registers and
 136 // unwinds stack.  Rethrow happens in the caller's method.
 137 class RethrowNode : public Node {
 138  public:
 139   RethrowNode( Node *cntrl, Node *i_o, Node *memory, Node *frameptr, Node *ret_adr, Node *exception );
 140   virtual int Opcode() const;
 141   virtual bool  is_CFG() const { return true; }
 142   virtual uint hash() const { return NO_HASH; }  // CFG nodes do not hash
 143   virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
 144   virtual const Type* Value(PhaseGVN* phase) const;
 145   virtual uint match_edge(uint idx) const;
 146   virtual uint ideal_reg() const { return NotAMachineReg; }
 147 #ifndef PRODUCT
 148   virtual void dump_req(outputStream *st = tty, DumpConfig* dc = nullptr) const;
 149 #endif
 150 };
 151 
 152 
 153 //------------------------------ForwardExceptionNode---------------------------
 154 // Pop stack frame and jump to StubRoutines::forward_exception_entry()
 155 class ForwardExceptionNode : public ReturnNode {
 156 public:
 157   ForwardExceptionNode(Node* cntrl, Node* i_o, Node* memory, Node* frameptr, Node* retadr)
 158     : ReturnNode(TypeFunc::Parms, cntrl, i_o, memory, frameptr, retadr) {
 159   }
 160 
 161   virtual int Opcode() const;
 162 };
 163 
 164 //------------------------------TailCallNode-----------------------------------
 165 // Pop stack frame and jump indirect
 166 class TailCallNode : public ReturnNode {
 167 public:
 168   TailCallNode( Node *cntrl, Node *i_o, Node *memory, Node *frameptr, Node *retadr, Node *target, Node *moop )
 169     : ReturnNode( TypeFunc::Parms+2, cntrl, i_o, memory, frameptr, retadr ) {
 170     init_req(TypeFunc::Parms, target);
 171     init_req(TypeFunc::Parms+1, moop);
 172   }
 173 
 174   virtual int Opcode() const;
 175   virtual uint match_edge(uint idx) const;
 176 };
 177 
 178 //------------------------------TailJumpNode-----------------------------------
 179 // Pop stack frame and jump indirect
 180 class TailJumpNode : public ReturnNode {
 181 public:
 182   TailJumpNode( Node *cntrl, Node *i_o, Node *memory, Node *frameptr, Node *target, Node *ex_oop)
 183     : ReturnNode(TypeFunc::Parms+2, cntrl, i_o, memory, frameptr, Compile::current()->top()) {
 184     init_req(TypeFunc::Parms, target);
 185     init_req(TypeFunc::Parms+1, ex_oop);
 186   }
 187 
 188   virtual int Opcode() const;
 189   virtual uint match_edge(uint idx) const;
 190 };
 191 
 192 //-------------------------------JVMState-------------------------------------
 193 // A linked list of JVMState nodes captures the whole interpreter state,
 194 // plus GC roots, for all active calls at some call site in this compilation
 195 // unit.  (If there is no inlining, then the list has exactly one link.)
 196 // This provides a way to map the optimized program back into the interpreter,
 197 // or to let the GC mark the stack.
 198 class JVMState : public ResourceObj {
 199 public:
 200   typedef enum {
 201     Reexecute_Undefined = -1, // not defined -- will be translated into false later
 202     Reexecute_False     =  0, // false       -- do not reexecute
 203     Reexecute_True      =  1  // true        -- reexecute the bytecode
 204   } ReexecuteState; //Reexecute State
 205 
 206 private:
 207   JVMState*         _caller;    // List pointer for forming scope chains
 208   uint              _depth;     // One more than caller depth, or one.
 209   uint              _locoff;    // Offset to locals in input edge mapping
 210   uint              _stkoff;    // Offset to stack in input edge mapping
 211   uint              _monoff;    // Offset to monitors in input edge mapping
 212   uint              _scloff;    // Offset to fields of scalar objs in input edge mapping
 213   uint              _endoff;    // Offset to end of input edge mapping
 214   uint              _sp;        // Java Expression Stack Pointer for this state
 215   int               _bci;       // Byte Code Index of this JVM point
 216   ReexecuteState    _reexecute; // Whether this bytecode need to be re-executed
 217   ciMethod*         _method;    // Method Pointer
 218   ciInstance*       _receiver_info; // Constant receiver instance for compiled lambda forms
 219   SafePointNode*    _map;       // Map node associated with this scope
 220 public:
 221   friend class Compile;
 222   friend class PreserveReexecuteState;
 223 
 224   // Because JVMState objects live over the entire lifetime of the
 225   // Compile object, they are allocated into the comp_arena, which
 226   // does not get resource marked or reset during the compile process
 227   void *operator new( size_t x, Compile* C ) throw() { return C->comp_arena()->Amalloc(x); }
 228   void operator delete( void * ) { } // fast deallocation
 229 
 230   // Create a new JVMState, ready for abstract interpretation.
 231   JVMState(ciMethod* method, JVMState* caller);
 232   JVMState(int stack_size);  // root state; has a null method
 233 
 234   // Access functions for the JVM
 235   // ... --|--- loc ---|--- stk ---|--- arg ---|--- mon ---|--- scl ---|
 236   //       \ locoff    \ stkoff    \ argoff    \ monoff    \ scloff    \ endoff
 237   uint              locoff() const { return _locoff; }
 238   uint              stkoff() const { return _stkoff; }
 239   uint              argoff() const { return _stkoff + _sp; }
 240   uint              monoff() const { return _monoff; }
 241   uint              scloff() const { return _scloff; }
 242   uint              endoff() const { return _endoff; }
 243   uint              oopoff() const { return debug_end(); }
 244 
 245   int            loc_size() const { return stkoff() - locoff(); }
 246   int            stk_size() const { return monoff() - stkoff(); }
 247   int            mon_size() const { return scloff() - monoff(); }
 248   int            scl_size() const { return endoff() - scloff(); }
 249 
 250   bool        is_loc(uint i) const { return locoff() <= i && i < stkoff(); }
 251   bool        is_stk(uint i) const { return stkoff() <= i && i < monoff(); }
 252   bool        is_mon(uint i) const { return monoff() <= i && i < scloff(); }
 253   bool        is_scl(uint i) const { return scloff() <= i && i < endoff(); }
 254 
 255   uint                      sp() const { return _sp; }
 256   int                      bci() const { return _bci; }
 257   bool        should_reexecute() const { return _reexecute==Reexecute_True; }
 258   bool  is_reexecute_undefined() const { return _reexecute==Reexecute_Undefined; }
 259   bool              has_method() const { return _method != nullptr; }
 260   ciMethod*             method() const { assert(has_method(), ""); return _method; }
 261   ciInstance*    receiver_info() const { assert(has_method(), ""); return _receiver_info; }
 262   JVMState*             caller() const { return _caller; }
 263   SafePointNode*           map() const { return _map; }
 264   uint                   depth() const { return _depth; }
 265   uint             debug_start() const; // returns locoff of root caller
 266   uint               debug_end() const; // returns endoff of self
 267   uint              debug_size() const {
 268     return loc_size() + sp() + mon_size() + scl_size();
 269   }
 270   uint        debug_depth()  const; // returns sum of debug_size values at all depths
 271 
 272   // Returns the JVM state at the desired depth (1 == root).
 273   JVMState* of_depth(int d) const;
 274 
 275   // Tells if two JVM states have the same call chain (depth, methods, & bcis).
 276   bool same_calls_as(const JVMState* that) const;
 277 
 278   // Monitors (monitors are stored as (boxNode, objNode) pairs
 279   enum { logMonitorEdges = 1 };
 280   int  nof_monitors()              const { return mon_size() >> logMonitorEdges; }
 281   int  monitor_depth()             const { return nof_monitors() + (caller() ? caller()->monitor_depth() : 0); }
 282   int  monitor_box_offset(int idx) const { return monoff() + (idx << logMonitorEdges) + 0; }
 283   int  monitor_obj_offset(int idx) const { return monoff() + (idx << logMonitorEdges) + 1; }
 284   bool is_monitor_box(uint off)    const {
 285     assert(is_mon(off), "should be called only for monitor edge");
 286     return (0 == bitfield(off - monoff(), 0, logMonitorEdges));
 287   }
 288   bool is_monitor_use(uint off)    const { return (is_mon(off)
 289                                                    && is_monitor_box(off))
 290                                              || (caller() && caller()->is_monitor_use(off)); }
 291 
 292   // Initialization functions for the JVM
 293   void              set_locoff(uint off) { _locoff = off; }
 294   void              set_stkoff(uint off) { _stkoff = off; }
 295   void              set_monoff(uint off) { _monoff = off; }
 296   void              set_scloff(uint off) { _scloff = off; }
 297   void              set_endoff(uint off) { _endoff = off; }
 298   void              set_offsets(uint off) {
 299     _locoff = _stkoff = _monoff = _scloff = _endoff = off;
 300   }
 301   void              set_map(SafePointNode* map) { _map = map; }
 302   void              bind_map(SafePointNode* map); // set_map() and set_jvms() for the SafePointNode
 303   void              set_sp(uint sp) { _sp = sp; }
 304                     // _reexecute is initialized to "undefined" for a new bci
 305   void              set_bci(int bci) {if(_bci != bci)_reexecute=Reexecute_Undefined; _bci = bci; }
 306   void              set_should_reexecute(bool reexec) {_reexecute = reexec ? Reexecute_True : Reexecute_False;}
 307   void              set_receiver_info(ciInstance* recv) { assert(has_method() || recv == nullptr, ""); _receiver_info = recv; }
 308 
 309   // Miscellaneous utility functions
 310   JVMState* clone_deep(Compile* C) const;    // recursively clones caller chain
 311   JVMState* clone_shallow(Compile* C) const; // retains uncloned caller
 312   void      set_map_deep(SafePointNode *map);// reset map for all callers
 313   void      adapt_position(int delta);       // Adapt offsets in in-array after adding an edge.
 314   int       interpreter_frame_size() const;
 315   ciInstance* compute_receiver_info(ciMethod* callee) const;
 316 
 317 #ifndef PRODUCT
 318   void      print_method_with_lineno(outputStream* st, bool show_name) const;
 319   void      format(PhaseRegAlloc *regalloc, const Node *n, outputStream* st) const;
 320   void      dump_spec(outputStream *st) const;
 321   void      dump_on(outputStream* st) const;
 322   void      dump() const {
 323     dump_on(tty);
 324   }
 325 #endif
 326 };
 327 
 328 //------------------------------SafePointNode----------------------------------
 329 // A SafePointNode is a subclass of a MultiNode for convenience (and
 330 // potential code sharing) only - conceptually it is independent of
 331 // the Node semantics.
 332 class SafePointNode : public MultiNode {
 333   friend JVMState;
 334   friend class GraphKit;
 335   friend class LibraryCallKit;
 336 
 337   virtual bool           cmp( const Node &n ) const;
 338   virtual uint           size_of() const;       // Size is bigger
 339 
 340 protected:
 341   JVMState* const _jvms;      // Pointer to list of JVM State objects
 342   // Many calls take *all* of memory as input,
 343   // but some produce a limited subset of that memory as output.
 344   // The adr_type reports the call's behavior as a store, not a load.
 345   const TypePtr*  _adr_type;  // What type of memory does this node produce?
 346   ReplacedNodes   _replaced_nodes; // During parsing: list of pair of nodes from calls to GraphKit::replace_in_map()
 347   bool            _has_ea_local_in_scope; // NoEscape or ArgEscape objects in JVM States
 348 
 349   void set_jvms(JVMState* s) {
 350   assert(s != nullptr, "assign null value to _jvms");
 351     *(JVMState**)&_jvms = s;  // override const attribute in the accessor
 352   }
 353 public:
 354   SafePointNode(uint edges, JVMState* jvms,
 355                 // A plain safepoint advertises no memory effects (null):
 356                 const TypePtr* adr_type = nullptr)
 357     : MultiNode( edges ),
 358       _jvms(jvms),
 359       _adr_type(adr_type),
 360       _has_ea_local_in_scope(false)
 361   {
 362     init_class_id(Class_SafePoint);
 363   }
 364 
 365   JVMState* jvms() const { return _jvms; }
 366   virtual bool needs_deep_clone_jvms(Compile* C) { return false; }
 367   void clone_jvms(Compile* C) {
 368     if (jvms() != nullptr) {
 369       if (needs_deep_clone_jvms(C)) {
 370         set_jvms(jvms()->clone_deep(C));
 371         jvms()->set_map_deep(this);
 372       } else {
 373         jvms()->clone_shallow(C)->bind_map(this);
 374       }
 375     }
 376   }
 377 
 378  private:
 379   void verify_input(const JVMState* jvms, uint idx) const {
 380     assert(verify_jvms(jvms), "jvms must match");
 381     Node* n = in(idx);
 382     assert((!n->bottom_type()->isa_long() && !n->bottom_type()->isa_double()) ||
 383            in(idx + 1)->is_top(), "2nd half of long/double");
 384   }
 385 
 386  public:
 387   // Functionality from old debug nodes which has changed
 388   Node* local(const JVMState* jvms, uint idx) const {
 389     uint loc_idx = jvms->locoff() + idx;
 390     assert(jvms->is_loc(loc_idx), "not a local slot");
 391     verify_input(jvms, loc_idx);
 392     return in(loc_idx);
 393   }
 394   Node* stack(const JVMState* jvms, uint idx) const {
 395     uint stk_idx = jvms->stkoff() + idx;
 396     assert(jvms->is_stk(stk_idx), "not a stack slot");
 397     verify_input(jvms, stk_idx);
 398     return in(stk_idx);
 399   }
 400   Node* argument(const JVMState* jvms, uint idx) const {
 401     uint arg_idx = jvms->argoff() + idx;
 402     assert(jvms->is_stk(arg_idx), "not an argument slot");
 403     verify_input(jvms, arg_idx);
 404     return in(jvms->argoff() + idx);
 405   }
 406   Node* monitor_box(const JVMState* jvms, uint idx) const {
 407     assert(verify_jvms(jvms), "jvms must match");
 408     uint mon_box_idx = jvms->monitor_box_offset(idx);
 409     assert(jvms->is_monitor_box(mon_box_idx), "not a monitor box offset");
 410     return in(mon_box_idx);
 411   }
 412   Node* monitor_obj(const JVMState* jvms, uint idx) const {
 413     assert(verify_jvms(jvms), "jvms must match");
 414     uint mon_obj_idx = jvms->monitor_obj_offset(idx);
 415     assert(jvms->is_mon(mon_obj_idx) && !jvms->is_monitor_box(mon_obj_idx), "not a monitor obj offset");
 416     return in(mon_obj_idx);
 417   }
 418 
 419   void  set_local(const JVMState* jvms, uint idx, Node *c);
 420 
 421   void  set_stack(const JVMState* jvms, uint idx, Node *c) {
 422     assert(verify_jvms(jvms), "jvms must match");
 423     set_req(jvms->stkoff() + idx, c);
 424   }
 425   void  set_argument(const JVMState* jvms, uint idx, Node *c) {
 426     assert(verify_jvms(jvms), "jvms must match");
 427     set_req(jvms->argoff() + idx, c);
 428   }
 429   void ensure_stack(JVMState* jvms, uint stk_size) {
 430     assert(verify_jvms(jvms), "jvms must match");
 431     int grow_by = (int)stk_size - (int)jvms->stk_size();
 432     if (grow_by > 0)  grow_stack(jvms, grow_by);
 433   }
 434   void grow_stack(JVMState* jvms, uint grow_by);
 435   // Handle monitor stack
 436   void push_monitor( const FastLockNode *lock );
 437   void pop_monitor ();
 438   Node *peek_monitor_box() const;
 439   Node *peek_monitor_obj() const;
 440   // Peek Operand Stacks, JVMS 2.6.2
 441   Node* peek_operand(uint off = 0) const;
 442 
 443   // Access functions for the JVM
 444   Node *control  () const { return in(TypeFunc::Control  ); }
 445   Node *i_o      () const { return in(TypeFunc::I_O      ); }
 446   Node *memory   () const { return in(TypeFunc::Memory   ); }
 447   Node *returnadr() const { return in(TypeFunc::ReturnAdr); }
 448   Node *frameptr () const { return in(TypeFunc::FramePtr ); }
 449 
 450   void set_control  ( Node *c ) { set_req(TypeFunc::Control,c); }
 451   void set_i_o      ( Node *c ) { set_req(TypeFunc::I_O    ,c); }
 452   void set_memory   ( Node *c ) { set_req(TypeFunc::Memory ,c); }
 453 
 454   MergeMemNode* merged_memory() const {
 455     return in(TypeFunc::Memory)->as_MergeMem();
 456   }
 457 
 458   // The parser marks useless maps as dead when it's done with them:
 459   bool is_killed() { return in(TypeFunc::Control) == nullptr; }
 460 
 461   // Exception states bubbling out of subgraphs such as inlined calls
 462   // are recorded here.  (There might be more than one, hence the "next".)
 463   // This feature is used only for safepoints which serve as "maps"
 464   // for JVM states during parsing, intrinsic expansion, etc.
 465   SafePointNode*         next_exception() const;
 466   void               set_next_exception(SafePointNode* n);
 467   bool                   has_exceptions() const { return next_exception() != nullptr; }
 468 
 469   // Helper methods to operate on replaced nodes
 470   ReplacedNodes replaced_nodes() const {
 471     return _replaced_nodes;
 472   }
 473 
 474   void set_replaced_nodes(ReplacedNodes replaced_nodes) {
 475     _replaced_nodes = replaced_nodes;
 476   }
 477 
 478   void clone_replaced_nodes() {
 479     _replaced_nodes.clone();
 480   }
 481   void record_replaced_node(Node* initial, Node* improved) {
 482     _replaced_nodes.record(initial, improved);
 483   }
 484   void transfer_replaced_nodes_from(SafePointNode* sfpt, uint idx = 0) {
 485     _replaced_nodes.transfer_from(sfpt->_replaced_nodes, idx);
 486   }
 487   void delete_replaced_nodes() {
 488     _replaced_nodes.reset();
 489   }
 490   void apply_replaced_nodes(uint idx) {
 491     _replaced_nodes.apply(this, idx);
 492   }
 493   void merge_replaced_nodes_with(SafePointNode* sfpt) {
 494     _replaced_nodes.merge_with(sfpt->_replaced_nodes);
 495   }
 496   bool has_replaced_nodes() const {
 497     return !_replaced_nodes.is_empty();
 498   }
 499   void set_has_ea_local_in_scope(bool b) {
 500     _has_ea_local_in_scope = b;
 501   }
 502   bool has_ea_local_in_scope() const {
 503     return _has_ea_local_in_scope;
 504   }
 505 
 506   // A temporary storge for node edges.
 507   // Intended for a single use.
 508   class NodeEdgeTempStorage : public StackObj {
 509     friend class SafePointNode;
 510 
 511     PhaseIterGVN& _igvn;
 512     Node*         _node_hook;
 513 
 514 #ifdef ASSERT
 515     enum State { state_initial, state_populated, state_processed };
 516 
 517     State _state; // monotonically transitions from initial to processed state.
 518 #endif // ASSERT
 519 
 520     bool is_empty() const {
 521       return _node_hook == nullptr || _node_hook->req() == 1;
 522     }
 523     void push(Node* n) {
 524       assert(n != nullptr, "");
 525       if (_node_hook == nullptr) {
 526         _node_hook = new Node(nullptr);
 527       }
 528       _node_hook->add_req(n);
 529     }
 530     Node* pop() {
 531       assert(!is_empty(), "");
 532       int idx = _node_hook->req()-1;
 533       Node* r = _node_hook->in(idx);
 534       _node_hook->del_req(idx);
 535       assert(r != nullptr, "");
 536       return r;
 537     }
 538 
 539   public:
 540     NodeEdgeTempStorage(PhaseIterGVN &igvn) : _igvn(igvn), _node_hook(nullptr)
 541                                               DEBUG_ONLY(COMMA _state(state_initial)) {
 542       assert(is_empty(), "");
 543     }
 544 
 545     ~NodeEdgeTempStorage() {
 546       assert(_state == state_processed, "not processed");
 547       assert(is_empty(), "");
 548       if (_node_hook != nullptr) {
 549         _node_hook->destruct(&_igvn);
 550       }
 551     }
 552 
 553     void remove_edge_if_present(Node* n) {
 554       if (!is_empty()) {
 555         int idx = _node_hook->find_edge(n);
 556         if (idx > 0) {
 557           _node_hook->del_req(idx);
 558         }
 559       }
 560     }
 561   };
 562 
 563   void remove_non_debug_edges(NodeEdgeTempStorage& non_debug_edges);
 564   void restore_non_debug_edges(NodeEdgeTempStorage& non_debug_edges);
 565 
 566   void disconnect_from_root(PhaseIterGVN *igvn);
 567 
 568   // Standard Node stuff
 569   virtual int            Opcode() const;
 570   virtual bool           pinned() const { return true; }
 571   virtual const Type*    Value(PhaseGVN* phase) const;
 572   virtual const Type*    bottom_type() const { return Type::CONTROL; }
 573   virtual const TypePtr* adr_type() const { return _adr_type; }
 574   void set_adr_type(const TypePtr* adr_type) { _adr_type = adr_type; }
 575   virtual Node          *Ideal(PhaseGVN *phase, bool can_reshape);
 576   virtual Node*          Identity(PhaseGVN* phase);
 577   virtual uint           ideal_reg() const { return 0; }
 578   virtual const RegMask &in_RegMask(uint) const;
 579   virtual const RegMask &out_RegMask() const;
 580   virtual uint           match_edge(uint idx) const;
 581 
 582 #ifndef PRODUCT
 583   virtual void           dump_spec(outputStream *st) const;
 584 #endif
 585 };
 586 
 587 //------------------------------SafePointScalarObjectNode----------------------
 588 // A SafePointScalarObjectNode represents the state of a scalarized object
 589 // at a safepoint.
 590 class SafePointScalarObjectNode: public TypeNode {
 591   uint _first_index;              // First input edge relative index of a SafePoint node where
 592                                   // states of the scalarized object fields are collected.
 593   uint _depth;                    // Depth of the JVM state the _first_index field refers to
 594   uint _n_fields;                 // Number of non-static fields of the scalarized object.
 595 
 596   Node* _alloc;                   // Just for debugging purposes.
 597 
 598   virtual uint hash() const;
 599   virtual bool cmp( const Node &n ) const;
 600 
 601   uint first_index() const { return _first_index; }
 602 
 603 public:
 604   SafePointScalarObjectNode(const TypeOopPtr* tp, Node* alloc, uint first_index, uint depth, uint n_fields);
 605 
 606   virtual int Opcode() const;
 607   virtual uint           ideal_reg() const;
 608   virtual const RegMask &in_RegMask(uint) const;
 609   virtual const RegMask &out_RegMask() const;
 610   virtual uint           match_edge(uint idx) const;
 611 
 612   uint first_index(JVMState* jvms) const {
 613     assert(jvms != nullptr, "missed JVMS");
 614     return jvms->of_depth(_depth)->scloff() + _first_index;
 615   }
 616   uint n_fields()    const { return _n_fields; }
 617 
 618 #ifdef ASSERT
 619   Node* alloc() const { return _alloc; }
 620 #endif
 621 
 622   virtual uint size_of() const { return sizeof(*this); }
 623 
 624   // Assumes that "this" is an argument to a safepoint node "s", and that
 625   // "new_call" is being created to correspond to "s".  But the difference
 626   // between the start index of the jvmstates of "new_call" and "s" is
 627   // "jvms_adj".  Produce and return a SafePointScalarObjectNode that
 628   // corresponds appropriately to "this" in "new_call".  Assumes that
 629   // "sosn_map" is a map, specific to the translation of "s" to "new_call",
 630   // mapping old SafePointScalarObjectNodes to new, to avoid multiple copies.
 631   SafePointScalarObjectNode* clone(Dict* sosn_map, bool& new_node) const;
 632 
 633 #ifndef PRODUCT
 634   virtual void              dump_spec(outputStream *st) const;
 635 #endif
 636 };
 637 
 638 //------------------------------SafePointScalarMergeNode----------------------
 639 //
 640 // This class represents an allocation merge that is used as debug information
 641 // and had at least one of its input scalar replaced.
 642 //
 643 // The required inputs of this node, except the control, are pointers to
 644 // SafePointScalarObjectNodes that describe scalarized inputs of the original
 645 // allocation merge. The other(s) properties of the class are described below.
 646 //
 647 // _merge_pointer_idx : index in the SafePointNode's input array where the
 648 //   description of the _allocation merge_ starts. The index is zero based and
 649 //   relative to the SafePoint's scloff. The two entries in the SafePointNode's
 650 //   input array starting at '_merge_pointer_idx` are Phi nodes representing:
 651 //
 652 //   1) The original merge Phi. During rematerialization this input will only be
 653 //   used if the "selector Phi" (see below) indicates that the execution of the
 654 //   Phi took the path of a non scalarized input.
 655 //
 656 //   2) A "selector Phi". The output of this Phi will be '-1' if the execution
 657 //   of the method exercised a non scalarized input of the original Phi.
 658 //   Otherwise, the output will be >=0, and it will indicate the index-1 in the
 659 //   SafePointScalarMergeNode input array where the description of the
 660 //   scalarized object that should be used is.
 661 //
 662 // As an example, consider a Phi merging 3 inputs, of which the last 2 are
 663 // scalar replaceable.
 664 //
 665 //    Phi(Region, NSR, SR, SR)
 666 //
 667 // During scalar replacement the SR inputs will be changed to null:
 668 //
 669 //    Phi(Region, NSR, nullptr, nullptr)
 670 //
 671 // A corresponding selector Phi will be created with a configuration like this:
 672 //
 673 //    Phi(Region, -1, 0, 1)
 674 //
 675 // During execution of the compiled method, if the execution reaches a Trap, the
 676 // output of the selector Phi will tell if we need to rematerialize one of the
 677 // scalar replaced inputs or if we should just use the pointer returned by the
 678 // original Phi.
 679 
 680 class SafePointScalarMergeNode: public TypeNode {
 681   int _merge_pointer_idx;         // This is the first input edge relative
 682                                   // index of a SafePoint node where metadata information relative
 683                                   // to restoring the merge is stored. The corresponding input
 684                                   // in the associated SafePoint will point to a Phi representing
 685                                   // potential non-scalar replaced objects.
 686 
 687   virtual uint hash() const;
 688   virtual bool cmp( const Node &n ) const;
 689 
 690 public:
 691   SafePointScalarMergeNode(const TypeOopPtr* tp, int merge_pointer_idx);
 692 
 693   virtual int            Opcode() const;
 694   virtual uint           ideal_reg() const;
 695   virtual const RegMask &in_RegMask(uint) const;
 696   virtual const RegMask &out_RegMask() const;
 697   virtual uint           match_edge(uint idx) const;
 698 
 699   virtual uint size_of() const { return sizeof(*this); }
 700 
 701   int merge_pointer_idx(JVMState* jvms) const {
 702     assert(jvms != nullptr, "JVMS reference is null.");
 703     return jvms->scloff() + _merge_pointer_idx;
 704   }
 705 
 706   int selector_idx(JVMState* jvms) const {
 707     assert(jvms != nullptr, "JVMS reference is null.");
 708     return jvms->scloff() + _merge_pointer_idx + 1;
 709   }
 710 
 711   // Assumes that "this" is an argument to a safepoint node "s", and that
 712   // "new_call" is being created to correspond to "s".  But the difference
 713   // between the start index of the jvmstates of "new_call" and "s" is
 714   // "jvms_adj".  Produce and return a SafePointScalarObjectNode that
 715   // corresponds appropriately to "this" in "new_call".  Assumes that
 716   // "sosn_map" is a map, specific to the translation of "s" to "new_call",
 717   // mapping old SafePointScalarObjectNodes to new, to avoid multiple copies.
 718   SafePointScalarMergeNode* clone(Dict* sosn_map, bool& new_node) const;
 719 
 720 #ifndef PRODUCT
 721   virtual void              dump_spec(outputStream *st) const;
 722 #endif
 723 };
 724 
 725 // Simple container for the outgoing projections of a call.  Useful
 726 // for serious surgery on calls.
 727 class CallProjections : public StackObj {
 728 public:
 729   Node* fallthrough_proj;
 730   Node* fallthrough_catchproj;
 731   Node* fallthrough_memproj;
 732   Node* fallthrough_ioproj;
 733   Node* catchall_catchproj;
 734   Node* catchall_memproj;
 735   Node* catchall_ioproj;
 736   Node* resproj;
 737   Node* exobj;
 738 };
 739 
 740 class CallGenerator;
 741 
 742 //------------------------------CallNode---------------------------------------
 743 // Call nodes now subsume the function of debug nodes at callsites, so they
 744 // contain the functionality of a full scope chain of debug nodes.
 745 class CallNode : public SafePointNode {
 746 
 747 protected:
 748   bool may_modify_arraycopy_helper(const TypeOopPtr* dest_t, const TypeOopPtr* t_oop, PhaseValues* phase) const;
 749 
 750 public:
 751   const TypeFunc* _tf;          // Function type
 752   address         _entry_point; // Address of method being called
 753   float           _cnt;         // Estimate of number of times called
 754   CallGenerator*  _generator;   // corresponding CallGenerator for some late inline calls
 755   const char*     _name;        // Printable name, if _method is null
 756 
 757   CallNode(const TypeFunc* tf, address addr, const TypePtr* adr_type, JVMState* jvms = nullptr)
 758     : SafePointNode(tf->domain()->cnt(), jvms, adr_type),
 759       _tf(tf),
 760       _entry_point(addr),
 761       _cnt(COUNT_UNKNOWN),
 762       _generator(nullptr),
 763       _name(nullptr)
 764   {
 765     init_class_id(Class_Call);
 766   }
 767 
 768   const TypeFunc* tf()         const { return _tf; }
 769   address  entry_point()       const { return _entry_point; }
 770   float    cnt()               const { return _cnt; }
 771   CallGenerator* generator()   const { return _generator; }
 772 
 773   void set_tf(const TypeFunc* tf)       { _tf = tf; }
 774   void set_entry_point(address p)       { _entry_point = p; }
 775   void set_cnt(float c)                 { _cnt = c; }
 776   void set_generator(CallGenerator* cg) { _generator = cg; }
 777 
 778   virtual const Type* bottom_type() const;
 779   virtual const Type* Value(PhaseGVN* phase) const;
 780   virtual Node* Ideal(PhaseGVN* phase, bool can_reshape);
 781   virtual Node* Identity(PhaseGVN* phase) { return this; }
 782   virtual bool        cmp(const Node &n) const;
 783   virtual uint        size_of() const = 0;
 784   virtual void        calling_convention(BasicType* sig_bt, VMRegPair* parm_regs, uint argcnt) const;
 785   virtual Node*       match(const ProjNode* proj, const Matcher* m);
 786   virtual uint        ideal_reg() const { return NotAMachineReg; }
 787   // Are we guaranteed that this node is a safepoint?  Not true for leaf calls and
 788   // for some macro nodes whose expansion does not have a safepoint on the fast path.
 789   virtual bool        guaranteed_safepoint()  { return true; }
 790   // For macro nodes, the JVMState gets modified during expansion. If calls
 791   // use MachConstantBase, it gets modified during matching. If the call is
 792   // late inlined, it also needs the full JVMState. So when cloning the
 793   // node the JVMState must be deep cloned. Default is to shallow clone.
 794   virtual bool needs_deep_clone_jvms(Compile* C) { return _generator != nullptr || C->needs_deep_clone_jvms(); }
 795 
 796   // Returns true if the call may modify n
 797   virtual bool        may_modify(const TypeOopPtr* t_oop, PhaseValues* phase) const;
 798   // Does this node have a use of n other than in debug information?
 799   bool                has_non_debug_use(const Node* n);
 800   // Returns the unique CheckCastPP of a call
 801   // or result projection is there are several CheckCastPP
 802   // or returns null if there is no one.
 803   Node* result_cast();
 804   // Does this node returns pointer?
 805   bool returns_pointer() const {
 806     const TypeTuple* r = tf()->range();
 807     return (r->cnt() > TypeFunc::Parms &&
 808             r->field_at(TypeFunc::Parms)->isa_ptr());
 809   }
 810 
 811   // Collect all the interesting edges from a call for use in
 812   // replacing the call by something else.  Used by macro expansion
 813   // and the late inlining support.
 814   void extract_projections(CallProjections* projs,
 815                            bool separate_io_proj,
 816                            bool do_asserts = true,
 817                            bool allow_handlers = false) const;
 818 
 819   virtual uint match_edge(uint idx) const;
 820 
 821   bool is_call_to_arraycopystub() const;
 822   bool is_call_to_multianewarray_stub() const;
 823 
 824   virtual void copy_call_debug_info(PhaseIterGVN* phase, SafePointNode* sfpt) {}
 825 
 826 #ifndef PRODUCT
 827   virtual void        dump_req(outputStream* st = tty, DumpConfig* dc = nullptr) const;
 828   virtual void        dump_spec(outputStream* st) const;
 829 #endif
 830 };
 831 
 832 
 833 //------------------------------CallJavaNode-----------------------------------
 834 // Make a static or dynamic subroutine call node using Java calling
 835 // convention.  (The "Java" calling convention is the compiler's calling
 836 // convention, as opposed to the interpreter's or that of native C.)
 837 class CallJavaNode : public CallNode {
 838 protected:
 839   virtual bool cmp( const Node &n ) const;
 840   virtual uint size_of() const; // Size is bigger
 841 
 842   ciMethod* _method;               // Method being direct called
 843   bool    _optimized_virtual;
 844   bool    _override_symbolic_info; // Override symbolic call site info from bytecode
 845   bool    _arg_escape;             // ArgEscape in parameter list
 846 public:
 847   CallJavaNode(const TypeFunc* tf , address addr, ciMethod* method)
 848     : CallNode(tf, addr, TypePtr::BOTTOM),
 849       _method(method),
 850       _optimized_virtual(false),
 851       _override_symbolic_info(false),
 852       _arg_escape(false)
 853   {
 854     init_class_id(Class_CallJava);
 855   }
 856 
 857   virtual int   Opcode() const;
 858   ciMethod* method() const                 { return _method; }
 859   void  set_method(ciMethod *m)            { _method = m; }
 860   void  set_optimized_virtual(bool f)      { _optimized_virtual = f; }
 861   bool  is_optimized_virtual() const       { return _optimized_virtual; }
 862   void  set_override_symbolic_info(bool f) { _override_symbolic_info = f; }
 863   bool  override_symbolic_info() const     { return _override_symbolic_info; }
 864   void  set_arg_escape(bool f)             { _arg_escape = f; }
 865   bool  arg_escape() const                 { return _arg_escape; }
 866   void copy_call_debug_info(PhaseIterGVN* phase, SafePointNode *sfpt);
 867   void register_for_late_inline();
 868 
 869   DEBUG_ONLY( bool validate_symbolic_info() const; )
 870 
 871 #ifndef PRODUCT
 872   virtual void  dump_spec(outputStream *st) const;
 873   virtual void  dump_compact_spec(outputStream *st) const;
 874 #endif
 875 };
 876 
 877 //------------------------------CallStaticJavaNode-----------------------------
 878 // Make a direct subroutine call using Java calling convention (for static
 879 // calls and optimized virtual calls, plus calls to wrappers for run-time
 880 // routines); generates static stub.
 881 class CallStaticJavaNode : public CallJavaNode {
 882   virtual bool cmp( const Node &n ) const;
 883   virtual uint size_of() const; // Size is bigger
 884 public:
 885   CallStaticJavaNode(Compile* C, const TypeFunc* tf, address addr, ciMethod* method)
 886     : CallJavaNode(tf, addr, method) {
 887     init_class_id(Class_CallStaticJava);
 888     if (C->eliminate_boxing() && (method != nullptr) && method->is_boxing_method()) {
 889       init_flags(Flag_is_macro);
 890       C->add_macro_node(this);
 891     }
 892   }
 893   CallStaticJavaNode(const TypeFunc* tf, address addr, const char* name, const TypePtr* adr_type)
 894     : CallJavaNode(tf, addr, nullptr) {
 895     init_class_id(Class_CallStaticJava);
 896     // This node calls a runtime stub, which often has narrow memory effects.
 897     _adr_type = adr_type;
 898     _name = name;
 899   }
 900 
 901   // If this is an uncommon trap, return the request code, else zero.
 902   int uncommon_trap_request() const;
 903   bool is_uncommon_trap() const;
 904   static int extract_uncommon_trap_request(const Node* call);
 905 
 906   bool is_boxing_method() const {
 907     return is_macro() && (method() != nullptr) && method()->is_boxing_method();
 908   }
 909   // Late inlining modifies the JVMState, so we need to deep clone it
 910   // when the call node is cloned (because it is macro node).
 911   virtual bool needs_deep_clone_jvms(Compile* C) {
 912     return is_boxing_method() || CallNode::needs_deep_clone_jvms(C);
 913   }
 914 
 915   virtual int         Opcode() const;
 916   virtual Node* Ideal(PhaseGVN* phase, bool can_reshape);
 917 
 918 #ifndef PRODUCT
 919   virtual void        dump_spec(outputStream *st) const;
 920   virtual void        dump_compact_spec(outputStream *st) const;
 921 #endif
 922 };
 923 
 924 //------------------------------CallDynamicJavaNode----------------------------
 925 // Make a dispatched call using Java calling convention.
 926 class CallDynamicJavaNode : public CallJavaNode {
 927   virtual bool cmp( const Node &n ) const;
 928   virtual uint size_of() const; // Size is bigger
 929 public:
 930   CallDynamicJavaNode(const TypeFunc* tf , address addr, ciMethod* method, int vtable_index)
 931     : CallJavaNode(tf,addr,method), _vtable_index(vtable_index) {
 932     init_class_id(Class_CallDynamicJava);
 933   }
 934 
 935   // Late inlining modifies the JVMState, so we need to deep clone it
 936   // when the call node is cloned.
 937   virtual bool needs_deep_clone_jvms(Compile* C) {
 938     return IncrementalInlineVirtual || CallNode::needs_deep_clone_jvms(C);
 939   }
 940 
 941   int _vtable_index;
 942   virtual int   Opcode() const;
 943   virtual Node* Ideal(PhaseGVN* phase, bool can_reshape);
 944 #ifndef PRODUCT
 945   virtual void  dump_spec(outputStream *st) const;
 946 #endif
 947 };
 948 
 949 //------------------------------CallRuntimeNode--------------------------------
 950 // Make a direct subroutine call node into compiled C++ code.
 951 class CallRuntimeNode : public CallNode {
 952 protected:
 953   virtual bool cmp( const Node &n ) const;
 954   virtual uint size_of() const; // Size is bigger
 955 public:
 956   CallRuntimeNode(const TypeFunc* tf, address addr, const char* name,
 957                   const TypePtr* adr_type, JVMState* jvms = nullptr)
 958     : CallNode(tf, addr, adr_type, jvms)
 959   {
 960     init_class_id(Class_CallRuntime);
 961     _name = name;
 962   }
 963 
 964   virtual int   Opcode() const;
 965   virtual void  calling_convention( BasicType* sig_bt, VMRegPair *parm_regs, uint argcnt ) const;
 966 
 967 #ifndef PRODUCT
 968   virtual void  dump_spec(outputStream *st) const;
 969 #endif
 970 };
 971 
 972 //------------------------------CallLeafNode-----------------------------------
 973 // Make a direct subroutine call node into compiled C++ code, without
 974 // safepoints
 975 class CallLeafNode : public CallRuntimeNode {
 976 public:
 977   CallLeafNode(const TypeFunc* tf, address addr, const char* name,
 978                const TypePtr* adr_type)
 979     : CallRuntimeNode(tf, addr, name, adr_type)
 980   {
 981     init_class_id(Class_CallLeaf);
 982   }
 983   virtual int   Opcode() const;
 984   virtual bool        guaranteed_safepoint()  { return false; }
 985 #ifndef PRODUCT
 986   virtual void  dump_spec(outputStream *st) const;
 987 #endif
 988 };
 989 
 990 /* A pure function call, they are assumed not to be safepoints, not to read or write memory,
 991  * have no exception... They just take parameters, return a value without side effect. It is
 992  * always correct to create some, or remove them, if the result is not used.
 993  *
 994  * They still have control input to allow easy lowering into other kind of calls that require
 995  * a control, but this is more a technical than a moral constraint.
 996  *
 997  * Pure calls must have only control and data input and output: I/O, Memory and so on must be top.
 998  * Nevertheless, pure calls can typically be expensive math operations so care must be taken
 999  * when letting the node float.
1000  */
1001 class CallLeafPureNode : public CallLeafNode {
1002 protected:
1003   bool is_unused() const;
1004   bool is_dead() const;
1005   TupleNode* make_tuple_of_input_state_and_top_return_values(const Compile* C) const;
1006 
1007 public:
1008   CallLeafPureNode(const TypeFunc* tf, address addr, const char* name)
1009       : CallLeafNode(tf, addr, name, nullptr) {
1010     init_class_id(Class_CallLeafPure);
1011   }
1012   int Opcode() const override;
1013   Node* Ideal(PhaseGVN* phase, bool can_reshape) override;
1014 
1015   CallLeafPureNode* inline_call_leaf_pure_node(Node* control = nullptr) const;
1016 };
1017 
1018 //------------------------------CallLeafNoFPNode-------------------------------
1019 // CallLeafNode, not using floating point or using it in the same manner as
1020 // the generated code
1021 class CallLeafNoFPNode : public CallLeafNode {
1022 public:
1023   CallLeafNoFPNode(const TypeFunc* tf, address addr, const char* name,
1024                    const TypePtr* adr_type)
1025     : CallLeafNode(tf, addr, name, adr_type)
1026   {
1027     init_class_id(Class_CallLeafNoFP);
1028   }
1029   virtual int   Opcode() const;
1030 };
1031 
1032 //------------------------------CallLeafVectorNode-------------------------------
1033 // CallLeafNode but calling with vector calling convention instead.
1034 class CallLeafVectorNode : public CallLeafNode {
1035 private:
1036   uint _num_bits;
1037 protected:
1038   virtual bool cmp( const Node &n ) const;
1039   virtual uint size_of() const; // Size is bigger
1040 public:
1041   CallLeafVectorNode(const TypeFunc* tf, address addr, const char* name,
1042                    const TypePtr* adr_type, uint num_bits)
1043     : CallLeafNode(tf, addr, name, adr_type), _num_bits(num_bits)
1044   {
1045   }
1046   virtual int   Opcode() const;
1047   virtual void  calling_convention( BasicType* sig_bt, VMRegPair *parm_regs, uint argcnt ) const;
1048 };
1049 
1050 
1051 //------------------------------Allocate---------------------------------------
1052 // High-level memory allocation
1053 //
1054 //  AllocateNode and AllocateArrayNode are subclasses of CallNode because they will
1055 //  get expanded into a code sequence containing a call.  Unlike other CallNodes,
1056 //  they have 2 memory projections and 2 i_o projections (which are distinguished by
1057 //  the _is_io_use flag in the projection.)  This is needed when expanding the node in
1058 //  order to differentiate the uses of the projection on the normal control path from
1059 //  those on the exception return path.
1060 //
1061 class AllocateNode : public CallNode {
1062 public:
1063   enum {
1064     // Output:
1065     RawAddress  = TypeFunc::Parms,    // the newly-allocated raw address
1066     // Inputs:
1067     AllocSize   = TypeFunc::Parms,    // size (in bytes) of the new object
1068     KlassNode,                        // type (maybe dynamic) of the obj.
1069     InitialTest,                      // slow-path test (may be constant)
1070     ALength,                          // array length (or TOP if none)
1071     ValidLengthTest,
1072     ParmLimit
1073   };
1074 
1075   static const TypeFunc* alloc_type(const Type* t) {
1076     const Type** fields = TypeTuple::fields(ParmLimit - TypeFunc::Parms);
1077     fields[AllocSize]   = TypeInt::POS;
1078     fields[KlassNode]   = TypeInstPtr::NOTNULL;
1079     fields[InitialTest] = TypeInt::BOOL;
1080     fields[ALength]     = t;  // length (can be a bad length)
1081     fields[ValidLengthTest] = TypeInt::BOOL;
1082 
1083     const TypeTuple *domain = TypeTuple::make(ParmLimit, fields);
1084 
1085     // create result type (range)
1086     fields = TypeTuple::fields(1);
1087     fields[TypeFunc::Parms+0] = TypeRawPtr::NOTNULL; // Returned oop
1088 
1089     const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+1, fields);
1090 
1091     return TypeFunc::make(domain, range);
1092   }
1093 
1094   // Result of Escape Analysis
1095   bool _is_scalar_replaceable;
1096   bool _is_non_escaping;
1097   // True when MemBar for new is redundant with MemBar at initialzer exit
1098   bool _is_allocation_MemBar_redundant;
1099 
1100   virtual uint size_of() const; // Size is bigger
1101   AllocateNode(Compile* C, const TypeFunc *atype, Node *ctrl, Node *mem, Node *abio,
1102                Node *size, Node *klass_node, Node *initial_test);
1103   // Expansion modifies the JVMState, so we need to deep clone it
1104   virtual bool needs_deep_clone_jvms(Compile* C) { return true; }
1105   virtual int Opcode() const;
1106   virtual uint ideal_reg() const { return Op_RegP; }
1107   virtual bool        guaranteed_safepoint()  { return false; }
1108 
1109   // allocations do not modify their arguments
1110   virtual bool may_modify(const TypeOopPtr* t_oop, PhaseValues* phase) const { return false; }
1111 
1112   // Pattern-match a possible usage of AllocateNode.
1113   // Return null if no allocation is recognized.
1114   // The operand is the pointer produced by the (possible) allocation.
1115   // It must be a projection of the Allocate or its subsequent CastPP.
1116   // (Note:  This function is defined in file graphKit.cpp, near
1117   // GraphKit::new_instance/new_array, whose output it recognizes.)
1118   // The 'ptr' may not have an offset unless the 'offset' argument is given.
1119   static AllocateNode* Ideal_allocation(Node* ptr);
1120 
1121   // Fancy version which uses AddPNode::Ideal_base_and_offset to strip
1122   // an offset, which is reported back to the caller.
1123   // (Note:  AllocateNode::Ideal_allocation is defined in graphKit.cpp.)
1124   static AllocateNode* Ideal_allocation(Node* ptr, PhaseValues* phase,
1125                                         intptr_t& offset);
1126 
1127   // Dig the klass operand out of a (possible) allocation site.
1128   static Node* Ideal_klass(Node* ptr, PhaseValues* phase) {
1129     AllocateNode* allo = Ideal_allocation(ptr);
1130     return (allo == nullptr) ? nullptr : allo->in(KlassNode);
1131   }
1132 
1133   // Conservatively small estimate of offset of first non-header byte.
1134   int minimum_header_size() {
1135     return is_AllocateArray() ? arrayOopDesc::base_offset_in_bytes(T_BYTE) :
1136                                 instanceOopDesc::base_offset_in_bytes();
1137   }
1138 
1139   // Return the corresponding initialization barrier (or null if none).
1140   // Walks out edges to find it...
1141   // (Note: Both InitializeNode::allocation and AllocateNode::initialization
1142   // are defined in graphKit.cpp, which sets up the bidirectional relation.)
1143   InitializeNode* initialization();
1144 
1145   // Convenience for initialization->maybe_set_complete(phase)
1146   bool maybe_set_complete(PhaseGVN* phase);
1147 
1148   // Return true if allocation doesn't escape thread, its escape state
1149   // needs be noEscape or ArgEscape. InitializeNode._does_not_escape
1150   // is true when its allocation's escape state is noEscape or
1151   // ArgEscape. In case allocation's InitializeNode is null, check
1152   // AlllocateNode._is_non_escaping flag.
1153   // AlllocateNode._is_non_escaping is true when its escape state is
1154   // noEscape.
1155   bool does_not_escape_thread() {
1156     InitializeNode* init = nullptr;
1157     return _is_non_escaping || (((init = initialization()) != nullptr) && init->does_not_escape());
1158   }
1159 
1160   // If object doesn't escape in <.init> method and there is memory barrier
1161   // inserted at exit of its <.init>, memory barrier for new is not necessary.
1162   // Inovke this method when MemBar at exit of initializer and post-dominate
1163   // allocation node.
1164   void compute_MemBar_redundancy(ciMethod* initializer);
1165   bool is_allocation_MemBar_redundant() { return _is_allocation_MemBar_redundant; }
1166 
1167   Node* make_ideal_mark(PhaseGVN* phase, Node* control, Node* mem);
1168 
1169   NOT_PRODUCT(virtual void dump_spec(outputStream* st) const;)
1170 };
1171 
1172 //------------------------------AllocateArray---------------------------------
1173 //
1174 // High-level array allocation
1175 //
1176 class AllocateArrayNode : public AllocateNode {
1177 public:
1178   AllocateArrayNode(Compile* C, const TypeFunc* atype, Node* ctrl, Node* mem, Node* abio, Node* size, Node* klass_node,
1179                     Node* initial_test, Node* count_val, Node* valid_length_test)
1180     : AllocateNode(C, atype, ctrl, mem, abio, size, klass_node,
1181                    initial_test)
1182   {
1183     init_class_id(Class_AllocateArray);
1184     set_req(AllocateNode::ALength,        count_val);
1185     set_req(AllocateNode::ValidLengthTest, valid_length_test);
1186   }
1187   virtual int Opcode() const;
1188 
1189   // Dig the length operand out of a array allocation site.
1190   Node* Ideal_length() {
1191     return in(AllocateNode::ALength);
1192   }
1193 
1194   // Dig the length operand out of a array allocation site and narrow the
1195   // type with a CastII, if necesssary
1196   Node* make_ideal_length(const TypeOopPtr* ary_type, PhaseValues* phase, bool can_create = true);
1197 
1198   // Pattern-match a possible usage of AllocateArrayNode.
1199   // Return null if no allocation is recognized.
1200   static AllocateArrayNode* Ideal_array_allocation(Node* ptr) {
1201     AllocateNode* allo = Ideal_allocation(ptr);
1202     return (allo == nullptr || !allo->is_AllocateArray())
1203            ? nullptr : allo->as_AllocateArray();
1204   }
1205 };
1206 
1207 //------------------------------AbstractLockNode-----------------------------------
1208 class AbstractLockNode: public CallNode {
1209 private:
1210   enum {
1211     Regular = 0,  // Normal lock
1212     NonEscObj,    // Lock is used for non escaping object
1213     Coarsened,    // Lock was coarsened
1214     Nested        // Nested lock
1215   } _kind;
1216 
1217   static const char* _kind_names[Nested+1];
1218 
1219 #ifndef PRODUCT
1220   NamedCounter* _counter;
1221 #endif
1222 
1223 protected:
1224   // helper functions for lock elimination
1225   //
1226 
1227   bool find_matching_unlock(const Node* ctrl, LockNode* lock,
1228                             GrowableArray<AbstractLockNode*> &lock_ops);
1229   bool find_lock_and_unlock_through_if(Node* node, LockNode* lock,
1230                                        GrowableArray<AbstractLockNode*> &lock_ops);
1231   bool find_unlocks_for_region(const RegionNode* region, LockNode* lock,
1232                                GrowableArray<AbstractLockNode*> &lock_ops);
1233   LockNode *find_matching_lock(UnlockNode* unlock);
1234 
1235   // Update the counter to indicate that this lock was eliminated.
1236   void set_eliminated_lock_counter() PRODUCT_RETURN;
1237 
1238 public:
1239   AbstractLockNode(const TypeFunc *tf)
1240     : CallNode(tf, nullptr, TypeRawPtr::BOTTOM),
1241       _kind(Regular)
1242   {
1243 #ifndef PRODUCT
1244     _counter = nullptr;
1245 #endif
1246   }
1247   virtual int Opcode() const = 0;
1248   Node *   obj_node() const       {return in(TypeFunc::Parms + 0); }
1249   Node *   box_node() const       {return in(TypeFunc::Parms + 1); }
1250   Node *   fastlock_node() const  {return in(TypeFunc::Parms + 2); }
1251   void     set_box_node(Node* box) { set_req(TypeFunc::Parms + 1, box); }
1252 
1253   const Type *sub(const Type *t1, const Type *t2) const { return TypeInt::CC;}
1254 
1255   virtual uint size_of() const { return sizeof(*this); }
1256 
1257   bool is_eliminated()  const { return (_kind != Regular); }
1258   bool is_non_esc_obj() const { return (_kind == NonEscObj); }
1259   bool is_coarsened()   const { return (_kind == Coarsened); }
1260   bool is_nested()      const { return (_kind == Nested); }
1261 
1262   const char * kind_as_string() const;
1263   void log_lock_optimization(Compile* c, const char * tag, Node* bad_lock = nullptr) const;
1264 
1265   void set_non_esc_obj() { _kind = NonEscObj; set_eliminated_lock_counter(); }
1266   void set_coarsened()   { _kind = Coarsened; set_eliminated_lock_counter(); }
1267   void set_nested()      { _kind = Nested; set_eliminated_lock_counter(); }
1268 
1269   // Check that all locks/unlocks associated with object come from balanced regions.
1270   // They can become unbalanced after coarsening optimization or on OSR entry.
1271   bool is_balanced();
1272 
1273   // locking does not modify its arguments
1274   virtual bool may_modify(const TypeOopPtr* t_oop, PhaseValues* phase) const { return false; }
1275 
1276 #ifndef PRODUCT
1277   void create_lock_counter(JVMState* s);
1278   NamedCounter* counter() const { return _counter; }
1279   virtual void dump_spec(outputStream* st) const;
1280   virtual void dump_compact_spec(outputStream* st) const;
1281 #endif
1282 };
1283 
1284 //------------------------------Lock---------------------------------------
1285 // High-level lock operation
1286 //
1287 // This is a subclass of CallNode because it is a macro node which gets expanded
1288 // into a code sequence containing a call.  This node takes 3 "parameters":
1289 //    0  -  object to lock
1290 //    1 -   a BoxLockNode
1291 //    2 -   a FastLockNode
1292 //
1293 class LockNode : public AbstractLockNode {
1294   static const TypeFunc* _lock_type_Type;
1295 public:
1296 
1297   static inline const TypeFunc* lock_type() {
1298     assert(_lock_type_Type != nullptr, "should be initialized");
1299     return _lock_type_Type;
1300   }
1301 
1302   static void initialize_lock_Type() {
1303     assert(_lock_type_Type == nullptr, "should be called once");
1304     // create input type (domain)
1305     const Type **fields = TypeTuple::fields(3);
1306     fields[TypeFunc::Parms+0] = TypeInstPtr::NOTNULL;  // Object to be Locked
1307     fields[TypeFunc::Parms+1] = TypeRawPtr::BOTTOM;    // Address of stack location for lock
1308     fields[TypeFunc::Parms+2] = TypeInt::BOOL;         // FastLock
1309     const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+3,fields);
1310 
1311     // create result type (range)
1312     fields = TypeTuple::fields(0);
1313 
1314     const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+0,fields);
1315 
1316     _lock_type_Type = TypeFunc::make(domain,range);
1317   }
1318 
1319   virtual int Opcode() const;
1320   virtual uint size_of() const; // Size is bigger
1321   LockNode(Compile* C, const TypeFunc *tf) : AbstractLockNode( tf ) {
1322     init_class_id(Class_Lock);
1323     init_flags(Flag_is_macro);
1324     C->add_macro_node(this);
1325   }
1326   virtual bool        guaranteed_safepoint()  { return false; }
1327 
1328   virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
1329   // Expansion modifies the JVMState, so we need to deep clone it
1330   virtual bool needs_deep_clone_jvms(Compile* C) { return true; }
1331 
1332   bool is_nested_lock_region(); // Is this Lock nested?
1333   bool is_nested_lock_region(Compile * c); // Why isn't this Lock nested?
1334 };
1335 
1336 //------------------------------Unlock---------------------------------------
1337 // High-level unlock operation
1338 class UnlockNode : public AbstractLockNode {
1339 private:
1340 #ifdef ASSERT
1341   JVMState* const _dbg_jvms;      // Pointer to list of JVM State objects
1342 #endif
1343 public:
1344   virtual int Opcode() const;
1345   virtual uint size_of() const; // Size is bigger
1346   UnlockNode(Compile* C, const TypeFunc *tf) : AbstractLockNode( tf )
1347 #ifdef ASSERT
1348     , _dbg_jvms(nullptr)
1349 #endif
1350   {
1351     init_class_id(Class_Unlock);
1352     init_flags(Flag_is_macro);
1353     C->add_macro_node(this);
1354   }
1355   virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
1356   // unlock is never a safepoint
1357   virtual bool        guaranteed_safepoint()  { return false; }
1358 #ifdef ASSERT
1359   void set_dbg_jvms(JVMState* s) {
1360     *(JVMState**)&_dbg_jvms = s;  // override const attribute in the accessor
1361   }
1362   JVMState* dbg_jvms() const { return _dbg_jvms; }
1363 #else
1364   JVMState* dbg_jvms() const { return nullptr; }
1365 #endif
1366 };
1367 
1368 //------------------------------PowDNode--------------------------------------
1369 class PowDNode : public CallLeafPureNode {
1370   TupleNode* make_tuple_of_input_state_and_result(PhaseIterGVN* phase, Node* result, Node* control = nullptr);
1371 
1372 public:
1373   PowDNode(Compile* C, Node* base, Node* exp);
1374   int Opcode() const override;
1375   const Type* Value(PhaseGVN* phase) const override;
1376   Node* Ideal(PhaseGVN* phase, bool can_reshape) override;
1377 
1378   Node* base() const { return in(TypeFunc::Parms + 0); }
1379   Node* exp() const  { return in(TypeFunc::Parms + 2); }
1380 };
1381 
1382 #endif // SHARE_OPTO_CALLNODE_HPP