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