1 /*
2 * Copyright (c) 1997, 2020, 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 #include "precompiled.hpp"
26 #include "gc/shared/barrierSet.hpp"
27 #include "gc/shared/c2/barrierSetC2.hpp"
28 #include "libadt/vectset.hpp"
29 #include "memory/allocation.inline.hpp"
30 #include "memory/resourceArea.hpp"
31 #include "opto/ad.hpp"
32 #include "opto/callGenerator.hpp"
33 #include "opto/castnode.hpp"
34 #include "opto/cfgnode.hpp"
35 #include "opto/connode.hpp"
36 #include "opto/loopnode.hpp"
37 #include "opto/machnode.hpp"
38 #include "opto/matcher.hpp"
39 #include "opto/node.hpp"
40 #include "opto/opcodes.hpp"
41 #include "opto/regmask.hpp"
42 #include "opto/rootnode.hpp"
43 #include "opto/type.hpp"
44 #include "utilities/copy.hpp"
45 #include "utilities/macros.hpp"
46 #include "utilities/powerOfTwo.hpp"
47
48 class RegMask;
49 // #include "phase.hpp"
50 class PhaseTransform;
51 class PhaseGVN;
52
53 // Arena we are currently building Nodes in
54 const uint Node::NotAMachineReg = 0xffff0000;
55
56 #ifndef PRODUCT
57 extern int nodes_created;
58 #endif
59 #ifdef __clang__
60 #pragma clang diagnostic push
61 #pragma GCC diagnostic ignored "-Wuninitialized"
62 #endif
63
64 #ifdef ASSERT
65
66 //-------------------------- construct_node------------------------------------
67 // Set a breakpoint here to identify where a particular node index is built.
verify_construction()68 void Node::verify_construction() {
69 _debug_orig = NULL;
70 int old_debug_idx = Compile::debug_idx();
71 int new_debug_idx = old_debug_idx + 1;
72 if (new_debug_idx > 0) {
73 // Arrange that the lowest five decimal digits of _debug_idx
74 // will repeat those of _idx. In case this is somehow pathological,
75 // we continue to assign negative numbers (!) consecutively.
76 const int mod = 100000;
77 int bump = (int)(_idx - new_debug_idx) % mod;
78 if (bump < 0) {
79 bump += mod;
80 }
81 assert(bump >= 0 && bump < mod, "");
82 new_debug_idx += bump;
83 }
84 Compile::set_debug_idx(new_debug_idx);
85 set_debug_idx(new_debug_idx);
86 Compile* C = Compile::current();
87 assert(C->unique() < (INT_MAX - 1), "Node limit exceeded INT_MAX");
88 if (!C->phase_optimize_finished()) {
89 // Only check assert during parsing and optimization phase. Skip it while generating code.
90 assert(C->live_nodes() <= C->max_node_limit(), "Live Node limit exceeded limit");
91 }
92 if (BreakAtNode != 0 && (_debug_idx == BreakAtNode || (int)_idx == BreakAtNode)) {
93 tty->print_cr("BreakAtNode: _idx=%d _debug_idx=%d", _idx, _debug_idx);
94 BREAKPOINT;
95 }
96 #if OPTO_DU_ITERATOR_ASSERT
97 _last_del = NULL;
98 _del_tick = 0;
99 #endif
100 _hash_lock = 0;
101 }
102
103
104 // #ifdef ASSERT ...
105
106 #if OPTO_DU_ITERATOR_ASSERT
sample(const Node * node)107 void DUIterator_Common::sample(const Node* node) {
108 _vdui = VerifyDUIterators;
109 _node = node;
110 _outcnt = node->_outcnt;
111 _del_tick = node->_del_tick;
112 _last = NULL;
113 }
114
verify(const Node * node,bool at_end_ok)115 void DUIterator_Common::verify(const Node* node, bool at_end_ok) {
116 assert(_node == node, "consistent iterator source");
117 assert(_del_tick == node->_del_tick, "no unexpected deletions allowed");
118 }
119
verify_resync()120 void DUIterator_Common::verify_resync() {
121 // Ensure that the loop body has just deleted the last guy produced.
122 const Node* node = _node;
123 // Ensure that at least one copy of the last-seen edge was deleted.
124 // Note: It is OK to delete multiple copies of the last-seen edge.
125 // Unfortunately, we have no way to verify that all the deletions delete
126 // that same edge. On this point we must use the Honor System.
127 assert(node->_del_tick >= _del_tick+1, "must have deleted an edge");
128 assert(node->_last_del == _last, "must have deleted the edge just produced");
129 // We liked this deletion, so accept the resulting outcnt and tick.
130 _outcnt = node->_outcnt;
131 _del_tick = node->_del_tick;
132 }
133
reset(const DUIterator_Common & that)134 void DUIterator_Common::reset(const DUIterator_Common& that) {
135 if (this == &that) return; // ignore assignment to self
136 if (!_vdui) {
137 // We need to initialize everything, overwriting garbage values.
138 _last = that._last;
139 _vdui = that._vdui;
140 }
141 // Note: It is legal (though odd) for an iterator over some node x
142 // to be reassigned to iterate over another node y. Some doubly-nested
143 // progress loops depend on being able to do this.
144 const Node* node = that._node;
145 // Re-initialize everything, except _last.
146 _node = node;
147 _outcnt = node->_outcnt;
148 _del_tick = node->_del_tick;
149 }
150
sample(const Node * node)151 void DUIterator::sample(const Node* node) {
152 DUIterator_Common::sample(node); // Initialize the assertion data.
153 _refresh_tick = 0; // No refreshes have happened, as yet.
154 }
155
verify(const Node * node,bool at_end_ok)156 void DUIterator::verify(const Node* node, bool at_end_ok) {
157 DUIterator_Common::verify(node, at_end_ok);
158 assert(_idx < node->_outcnt + (uint)at_end_ok, "idx in range");
159 }
160
verify_increment()161 void DUIterator::verify_increment() {
162 if (_refresh_tick & 1) {
163 // We have refreshed the index during this loop.
164 // Fix up _idx to meet asserts.
165 if (_idx > _outcnt) _idx = _outcnt;
166 }
167 verify(_node, true);
168 }
169
verify_resync()170 void DUIterator::verify_resync() {
171 // Note: We do not assert on _outcnt, because insertions are OK here.
172 DUIterator_Common::verify_resync();
173 // Make sure we are still in sync, possibly with no more out-edges:
174 verify(_node, true);
175 }
176
reset(const DUIterator & that)177 void DUIterator::reset(const DUIterator& that) {
178 if (this == &that) return; // self assignment is always a no-op
179 assert(that._refresh_tick == 0, "assign only the result of Node::outs()");
180 assert(that._idx == 0, "assign only the result of Node::outs()");
181 assert(_idx == that._idx, "already assigned _idx");
182 if (!_vdui) {
183 // We need to initialize everything, overwriting garbage values.
184 sample(that._node);
185 } else {
186 DUIterator_Common::reset(that);
187 if (_refresh_tick & 1) {
188 _refresh_tick++; // Clear the "was refreshed" flag.
189 }
190 assert(_refresh_tick < 2*100000, "DU iteration must converge quickly");
191 }
192 }
193
refresh()194 void DUIterator::refresh() {
195 DUIterator_Common::sample(_node); // Re-fetch assertion data.
196 _refresh_tick |= 1; // Set the "was refreshed" flag.
197 }
198
verify_finish()199 void DUIterator::verify_finish() {
200 // If the loop has killed the node, do not require it to re-run.
201 if (_node->_outcnt == 0) _refresh_tick &= ~1;
202 // If this assert triggers, it means that a loop used refresh_out_pos
203 // to re-synch an iteration index, but the loop did not correctly
204 // re-run itself, using a "while (progress)" construct.
205 // This iterator enforces the rule that you must keep trying the loop
206 // until it "runs clean" without any need for refreshing.
207 assert(!(_refresh_tick & 1), "the loop must run once with no refreshing");
208 }
209
210
verify(const Node * node,bool at_end_ok)211 void DUIterator_Fast::verify(const Node* node, bool at_end_ok) {
212 DUIterator_Common::verify(node, at_end_ok);
213 Node** out = node->_out;
214 uint cnt = node->_outcnt;
215 assert(cnt == _outcnt, "no insertions allowed");
216 assert(_outp >= out && _outp <= out + cnt - !at_end_ok, "outp in range");
217 // This last check is carefully designed to work for NO_OUT_ARRAY.
218 }
219
verify_limit()220 void DUIterator_Fast::verify_limit() {
221 const Node* node = _node;
222 verify(node, true);
223 assert(_outp == node->_out + node->_outcnt, "limit still correct");
224 }
225
verify_resync()226 void DUIterator_Fast::verify_resync() {
227 const Node* node = _node;
228 if (_outp == node->_out + _outcnt) {
229 // Note that the limit imax, not the pointer i, gets updated with the
230 // exact count of deletions. (For the pointer it's always "--i".)
231 assert(node->_outcnt+node->_del_tick == _outcnt+_del_tick, "no insertions allowed with deletion(s)");
232 // This is a limit pointer, with a name like "imax".
233 // Fudge the _last field so that the common assert will be happy.
234 _last = (Node*) node->_last_del;
235 DUIterator_Common::verify_resync();
236 } else {
237 assert(node->_outcnt < _outcnt, "no insertions allowed with deletion(s)");
238 // A normal internal pointer.
239 DUIterator_Common::verify_resync();
240 // Make sure we are still in sync, possibly with no more out-edges:
241 verify(node, true);
242 }
243 }
244
verify_relimit(uint n)245 void DUIterator_Fast::verify_relimit(uint n) {
246 const Node* node = _node;
247 assert((int)n > 0, "use imax -= n only with a positive count");
248 // This must be a limit pointer, with a name like "imax".
249 assert(_outp == node->_out + node->_outcnt, "apply -= only to a limit (imax)");
250 // The reported number of deletions must match what the node saw.
251 assert(node->_del_tick == _del_tick + n, "must have deleted n edges");
252 // Fudge the _last field so that the common assert will be happy.
253 _last = (Node*) node->_last_del;
254 DUIterator_Common::verify_resync();
255 }
256
reset(const DUIterator_Fast & that)257 void DUIterator_Fast::reset(const DUIterator_Fast& that) {
258 assert(_outp == that._outp, "already assigned _outp");
259 DUIterator_Common::reset(that);
260 }
261
verify(const Node * node,bool at_end_ok)262 void DUIterator_Last::verify(const Node* node, bool at_end_ok) {
263 // at_end_ok means the _outp is allowed to underflow by 1
264 _outp += at_end_ok;
265 DUIterator_Fast::verify(node, at_end_ok); // check _del_tick, etc.
266 _outp -= at_end_ok;
267 assert(_outp == (node->_out + node->_outcnt) - 1, "pointer must point to end of nodes");
268 }
269
verify_limit()270 void DUIterator_Last::verify_limit() {
271 // Do not require the limit address to be resynched.
272 //verify(node, true);
273 assert(_outp == _node->_out, "limit still correct");
274 }
275
verify_step(uint num_edges)276 void DUIterator_Last::verify_step(uint num_edges) {
277 assert((int)num_edges > 0, "need non-zero edge count for loop progress");
278 _outcnt -= num_edges;
279 _del_tick += num_edges;
280 // Make sure we are still in sync, possibly with no more out-edges:
281 const Node* node = _node;
282 verify(node, true);
283 assert(node->_last_del == _last, "must have deleted the edge just produced");
284 }
285
286 #endif //OPTO_DU_ITERATOR_ASSERT
287
288
289 #endif //ASSERT
290
291
292 // This constant used to initialize _out may be any non-null value.
293 // The value NULL is reserved for the top node only.
294 #define NO_OUT_ARRAY ((Node**)-1)
295
296 // Out-of-line code from node constructors.
297 // Executed only when extra debug info. is being passed around.
init_node_notes(Compile * C,int idx,Node_Notes * nn)298 static void init_node_notes(Compile* C, int idx, Node_Notes* nn) {
299 C->set_node_notes_at(idx, nn);
300 }
301
302 // Shared initialization code.
Init(int req)303 inline int Node::Init(int req) {
304 Compile* C = Compile::current();
305 int idx = C->next_unique();
306
307 // Allocate memory for the necessary number of edges.
308 if (req > 0) {
309 // Allocate space for _in array to have double alignment.
310 _in = (Node **) ((char *) (C->node_arena()->Amalloc_D(req * sizeof(void*))));
311 }
312 // If there are default notes floating around, capture them:
313 Node_Notes* nn = C->default_node_notes();
314 if (nn != NULL) init_node_notes(C, idx, nn);
315
316 // Note: At this point, C is dead,
317 // and we begin to initialize the new Node.
318
319 _cnt = _max = req;
320 _outcnt = _outmax = 0;
321 _class_id = Class_Node;
322 _flags = 0;
323 _out = NO_OUT_ARRAY;
324 return idx;
325 }
326
327 //------------------------------Node-------------------------------------------
328 // Create a Node, with a given number of required edges.
Node(uint req)329 Node::Node(uint req)
330 : _idx(Init(req))
331 #ifdef ASSERT
332 , _parse_idx(_idx)
333 , _indent(0)
334 #endif
335 {
336 assert( req < Compile::current()->max_node_limit() - NodeLimitFudgeFactor, "Input limit exceeded" );
337 debug_only( verify_construction() );
338 NOT_PRODUCT(nodes_created++);
339 if (req == 0) {
340 _in = NULL;
341 } else {
342 Node** to = _in;
343 for(uint i = 0; i < req; i++) {
344 to[i] = NULL;
345 }
346 }
347 }
348
349 //------------------------------Node-------------------------------------------
Node(Node * n0)350 Node::Node(Node *n0)
351 : _idx(Init(1))
352 #ifdef ASSERT
353 , _parse_idx(_idx)
354 , _indent(0)
355 #endif
356 {
357 debug_only( verify_construction() );
358 NOT_PRODUCT(nodes_created++);
359 assert( is_not_dead(n0), "can not use dead node");
360 _in[0] = n0; if (n0 != NULL) n0->add_out((Node *)this);
361 }
362
363 //------------------------------Node-------------------------------------------
Node(Node * n0,Node * n1)364 Node::Node(Node *n0, Node *n1)
365 : _idx(Init(2))
366 #ifdef ASSERT
367 , _parse_idx(_idx)
368 , _indent(0)
369 #endif
370 {
371 debug_only( verify_construction() );
372 NOT_PRODUCT(nodes_created++);
373 assert( is_not_dead(n0), "can not use dead node");
374 assert( is_not_dead(n1), "can not use dead node");
375 _in[0] = n0; if (n0 != NULL) n0->add_out((Node *)this);
376 _in[1] = n1; if (n1 != NULL) n1->add_out((Node *)this);
377 }
378
379 //------------------------------Node-------------------------------------------
Node(Node * n0,Node * n1,Node * n2)380 Node::Node(Node *n0, Node *n1, Node *n2)
381 : _idx(Init(3))
382 #ifdef ASSERT
383 , _parse_idx(_idx)
384 , _indent(0)
385 #endif
386 {
387 debug_only( verify_construction() );
388 NOT_PRODUCT(nodes_created++);
389 assert( is_not_dead(n0), "can not use dead node");
390 assert( is_not_dead(n1), "can not use dead node");
391 assert( is_not_dead(n2), "can not use dead node");
392 _in[0] = n0; if (n0 != NULL) n0->add_out((Node *)this);
393 _in[1] = n1; if (n1 != NULL) n1->add_out((Node *)this);
394 _in[2] = n2; if (n2 != NULL) n2->add_out((Node *)this);
395 }
396
397 //------------------------------Node-------------------------------------------
Node(Node * n0,Node * n1,Node * n2,Node * n3)398 Node::Node(Node *n0, Node *n1, Node *n2, Node *n3)
399 : _idx(Init(4))
400 #ifdef ASSERT
401 , _parse_idx(_idx)
402 , _indent(0)
403 #endif
404 {
405 debug_only( verify_construction() );
406 NOT_PRODUCT(nodes_created++);
407 assert( is_not_dead(n0), "can not use dead node");
408 assert( is_not_dead(n1), "can not use dead node");
409 assert( is_not_dead(n2), "can not use dead node");
410 assert( is_not_dead(n3), "can not use dead node");
411 _in[0] = n0; if (n0 != NULL) n0->add_out((Node *)this);
412 _in[1] = n1; if (n1 != NULL) n1->add_out((Node *)this);
413 _in[2] = n2; if (n2 != NULL) n2->add_out((Node *)this);
414 _in[3] = n3; if (n3 != NULL) n3->add_out((Node *)this);
415 }
416
417 //------------------------------Node-------------------------------------------
Node(Node * n0,Node * n1,Node * n2,Node * n3,Node * n4)418 Node::Node(Node *n0, Node *n1, Node *n2, Node *n3, Node *n4)
419 : _idx(Init(5))
420 #ifdef ASSERT
421 , _parse_idx(_idx)
422 , _indent(0)
423 #endif
424 {
425 debug_only( verify_construction() );
426 NOT_PRODUCT(nodes_created++);
427 assert( is_not_dead(n0), "can not use dead node");
428 assert( is_not_dead(n1), "can not use dead node");
429 assert( is_not_dead(n2), "can not use dead node");
430 assert( is_not_dead(n3), "can not use dead node");
431 assert( is_not_dead(n4), "can not use dead node");
432 _in[0] = n0; if (n0 != NULL) n0->add_out((Node *)this);
433 _in[1] = n1; if (n1 != NULL) n1->add_out((Node *)this);
434 _in[2] = n2; if (n2 != NULL) n2->add_out((Node *)this);
435 _in[3] = n3; if (n3 != NULL) n3->add_out((Node *)this);
436 _in[4] = n4; if (n4 != NULL) n4->add_out((Node *)this);
437 }
438
439 //------------------------------Node-------------------------------------------
Node(Node * n0,Node * n1,Node * n2,Node * n3,Node * n4,Node * n5)440 Node::Node(Node *n0, Node *n1, Node *n2, Node *n3,
441 Node *n4, Node *n5)
442 : _idx(Init(6))
443 #ifdef ASSERT
444 , _parse_idx(_idx)
445 , _indent(0)
446 #endif
447 {
448 debug_only( verify_construction() );
449 NOT_PRODUCT(nodes_created++);
450 assert( is_not_dead(n0), "can not use dead node");
451 assert( is_not_dead(n1), "can not use dead node");
452 assert( is_not_dead(n2), "can not use dead node");
453 assert( is_not_dead(n3), "can not use dead node");
454 assert( is_not_dead(n4), "can not use dead node");
455 assert( is_not_dead(n5), "can not use dead node");
456 _in[0] = n0; if (n0 != NULL) n0->add_out((Node *)this);
457 _in[1] = n1; if (n1 != NULL) n1->add_out((Node *)this);
458 _in[2] = n2; if (n2 != NULL) n2->add_out((Node *)this);
459 _in[3] = n3; if (n3 != NULL) n3->add_out((Node *)this);
460 _in[4] = n4; if (n4 != NULL) n4->add_out((Node *)this);
461 _in[5] = n5; if (n5 != NULL) n5->add_out((Node *)this);
462 }
463
464 //------------------------------Node-------------------------------------------
Node(Node * n0,Node * n1,Node * n2,Node * n3,Node * n4,Node * n5,Node * n6)465 Node::Node(Node *n0, Node *n1, Node *n2, Node *n3,
466 Node *n4, Node *n5, Node *n6)
467 : _idx(Init(7))
468 #ifdef ASSERT
469 , _parse_idx(_idx)
470 , _indent(0)
471 #endif
472 {
473 debug_only( verify_construction() );
474 NOT_PRODUCT(nodes_created++);
475 assert( is_not_dead(n0), "can not use dead node");
476 assert( is_not_dead(n1), "can not use dead node");
477 assert( is_not_dead(n2), "can not use dead node");
478 assert( is_not_dead(n3), "can not use dead node");
479 assert( is_not_dead(n4), "can not use dead node");
480 assert( is_not_dead(n5), "can not use dead node");
481 assert( is_not_dead(n6), "can not use dead node");
482 _in[0] = n0; if (n0 != NULL) n0->add_out((Node *)this);
483 _in[1] = n1; if (n1 != NULL) n1->add_out((Node *)this);
484 _in[2] = n2; if (n2 != NULL) n2->add_out((Node *)this);
485 _in[3] = n3; if (n3 != NULL) n3->add_out((Node *)this);
486 _in[4] = n4; if (n4 != NULL) n4->add_out((Node *)this);
487 _in[5] = n5; if (n5 != NULL) n5->add_out((Node *)this);
488 _in[6] = n6; if (n6 != NULL) n6->add_out((Node *)this);
489 }
490
491 #ifdef __clang__
492 #pragma clang diagnostic pop
493 #endif
494
495
496 //------------------------------clone------------------------------------------
497 // Clone a Node.
clone() const498 Node *Node::clone() const {
499 Compile* C = Compile::current();
500 uint s = size_of(); // Size of inherited Node
501 Node *n = (Node*)C->node_arena()->Amalloc_D(size_of() + _max*sizeof(Node*));
502 Copy::conjoint_words_to_lower((HeapWord*)this, (HeapWord*)n, s);
503 // Set the new input pointer array
504 n->_in = (Node**)(((char*)n)+s);
505 // Cannot share the old output pointer array, so kill it
506 n->_out = NO_OUT_ARRAY;
507 // And reset the counters to 0
508 n->_outcnt = 0;
509 n->_outmax = 0;
510 // Unlock this guy, since he is not in any hash table.
511 debug_only(n->_hash_lock = 0);
512 // Walk the old node's input list to duplicate its edges
513 uint i;
514 for( i = 0; i < len(); i++ ) {
515 Node *x = in(i);
516 n->_in[i] = x;
517 if (x != NULL) x->add_out(n);
518 }
519 if (is_macro()) {
520 C->add_macro_node(n);
521 }
522 if (is_expensive()) {
523 C->add_expensive_node(n);
524 }
525 if (for_post_loop_opts_igvn()) {
526 // Don't add cloned node to Compile::_for_post_loop_opts_igvn list automatically.
527 // If it is applicable, it will happen anyway when the cloned node is registered with IGVN.
528 n->remove_flag(Node::NodeFlags::Flag_for_post_loop_opts_igvn);
529 }
530 BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
531 bs->register_potential_barrier_node(n);
532
533 n->set_idx(C->next_unique()); // Get new unique index as well
534 debug_only( n->verify_construction() );
535 NOT_PRODUCT(nodes_created++);
536 // Do not patch over the debug_idx of a clone, because it makes it
537 // impossible to break on the clone's moment of creation.
538 //debug_only( n->set_debug_idx( debug_idx() ) );
539
540 C->copy_node_notes_to(n, (Node*) this);
541
542 // MachNode clone
543 uint nopnds;
544 if (this->is_Mach() && (nopnds = this->as_Mach()->num_opnds()) > 0) {
545 MachNode *mach = n->as_Mach();
546 MachNode *mthis = this->as_Mach();
547 // Get address of _opnd_array.
548 // It should be the same offset since it is the clone of this node.
549 MachOper **from = mthis->_opnds;
550 MachOper **to = (MachOper **)((size_t)(&mach->_opnds) +
551 pointer_delta((const void*)from,
552 (const void*)(&mthis->_opnds), 1));
553 mach->_opnds = to;
554 for ( uint i = 0; i < nopnds; ++i ) {
555 to[i] = from[i]->clone();
556 }
557 }
558 if (n->is_Call()) {
559 // cloning CallNode may need to clone JVMState
560 n->as_Call()->clone_jvms(C);
561 // CallGenerator is linked to the original node.
562 CallGenerator* cg = n->as_Call()->generator();
563 if (cg != NULL) {
564 CallGenerator* cloned_cg = cg->with_call_node(n->as_Call());
565 n->as_Call()->set_generator(cloned_cg);
566 }
567 }
568 if (n->is_SafePoint()) {
569 n->as_SafePoint()->clone_replaced_nodes();
570 }
571 return n; // Return the clone
572 }
573
574 //---------------------------setup_is_top--------------------------------------
575 // Call this when changing the top node, to reassert the invariants
576 // required by Node::is_top. See Compile::set_cached_top_node.
setup_is_top()577 void Node::setup_is_top() {
578 if (this == (Node*)Compile::current()->top()) {
579 // This node has just become top. Kill its out array.
580 _outcnt = _outmax = 0;
581 _out = NULL; // marker value for top
582 assert(is_top(), "must be top");
583 } else {
584 if (_out == NULL) _out = NO_OUT_ARRAY;
585 assert(!is_top(), "must not be top");
586 }
587 }
588
589 //------------------------------~Node------------------------------------------
590 // Fancy destructor; eagerly attempt to reclaim Node numberings and storage
destruct(PhaseValues * phase)591 void Node::destruct(PhaseValues* phase) {
592 Compile* compile = (phase != NULL) ? phase->C : Compile::current();
593 if (phase != NULL && phase->is_IterGVN()) {
594 phase->is_IterGVN()->_worklist.remove(this);
595 }
596 // If this is the most recently created node, reclaim its index. Otherwise,
597 // record the node as dead to keep liveness information accurate.
598 if ((uint)_idx+1 == compile->unique()) {
599 compile->set_unique(compile->unique()-1);
600 } else {
601 compile->record_dead_node(_idx);
602 }
603 // Clear debug info:
604 Node_Notes* nn = compile->node_notes_at(_idx);
605 if (nn != NULL) nn->clear();
606 // Walk the input array, freeing the corresponding output edges
607 _cnt = _max; // forget req/prec distinction
608 uint i;
609 for( i = 0; i < _max; i++ ) {
610 set_req(i, NULL);
611 //assert(def->out(def->outcnt()-1) == (Node *)this,"bad def-use hacking in reclaim");
612 }
613 assert(outcnt() == 0, "deleting a node must not leave a dangling use");
614 // See if the input array was allocated just prior to the object
615 int edge_size = _max*sizeof(void*);
616 int out_edge_size = _outmax*sizeof(void*);
617 char *edge_end = ((char*)_in) + edge_size;
618 char *out_array = (char*)(_out == NO_OUT_ARRAY? NULL: _out);
619 int node_size = size_of();
620
621 // Free the output edge array
622 if (out_edge_size > 0) {
623 compile->node_arena()->Afree(out_array, out_edge_size);
624 }
625
626 // Free the input edge array and the node itself
627 if( edge_end == (char*)this ) {
628 // It was; free the input array and object all in one hit
629 #ifndef ASSERT
630 compile->node_arena()->Afree(_in,edge_size+node_size);
631 #endif
632 } else {
633 // Free just the input array
634 compile->node_arena()->Afree(_in,edge_size);
635
636 // Free just the object
637 #ifndef ASSERT
638 compile->node_arena()->Afree(this,node_size);
639 #endif
640 }
641 if (is_macro()) {
642 compile->remove_macro_node(this);
643 }
644 if (is_expensive()) {
645 compile->remove_expensive_node(this);
646 }
647 if (for_post_loop_opts_igvn()) {
648 compile->remove_from_post_loop_opts_igvn(this);
649 }
650
651 if (is_SafePoint()) {
652 as_SafePoint()->delete_replaced_nodes();
653 }
654 BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
655 bs->unregister_potential_barrier_node(this);
656 #ifdef ASSERT
657 // We will not actually delete the storage, but we'll make the node unusable.
658 *(address*)this = badAddress; // smash the C++ vtbl, probably
659 _in = _out = (Node**) badAddress;
660 _max = _cnt = _outmax = _outcnt = 0;
661 compile->remove_modified_node(this);
662 #endif
663 }
664
665 //------------------------------grow-------------------------------------------
666 // Grow the input array, making space for more edges
grow(uint len)667 void Node::grow(uint len) {
668 Arena* arena = Compile::current()->node_arena();
669 uint new_max = _max;
670 if( new_max == 0 ) {
671 _max = 4;
672 _in = (Node**)arena->Amalloc(4*sizeof(Node*));
673 Node** to = _in;
674 to[0] = NULL;
675 to[1] = NULL;
676 to[2] = NULL;
677 to[3] = NULL;
678 return;
679 }
680 new_max = next_power_of_2(len);
681 // Trimming to limit allows a uint8 to handle up to 255 edges.
682 // Previously I was using only powers-of-2 which peaked at 128 edges.
683 //if( new_max >= limit ) new_max = limit-1;
684 _in = (Node**)arena->Arealloc(_in, _max*sizeof(Node*), new_max*sizeof(Node*));
685 Copy::zero_to_bytes(&_in[_max], (new_max-_max)*sizeof(Node*)); // NULL all new space
686 _max = new_max; // Record new max length
687 // This assertion makes sure that Node::_max is wide enough to
688 // represent the numerical value of new_max.
689 assert(_max == new_max && _max > len, "int width of _max is too small");
690 }
691
692 //-----------------------------out_grow----------------------------------------
693 // Grow the input array, making space for more edges
out_grow(uint len)694 void Node::out_grow( uint len ) {
695 assert(!is_top(), "cannot grow a top node's out array");
696 Arena* arena = Compile::current()->node_arena();
697 uint new_max = _outmax;
698 if( new_max == 0 ) {
699 _outmax = 4;
700 _out = (Node **)arena->Amalloc(4*sizeof(Node*));
701 return;
702 }
703 new_max = next_power_of_2(len);
704 // Trimming to limit allows a uint8 to handle up to 255 edges.
705 // Previously I was using only powers-of-2 which peaked at 128 edges.
706 //if( new_max >= limit ) new_max = limit-1;
707 assert(_out != NULL && _out != NO_OUT_ARRAY, "out must have sensible value");
708 _out = (Node**)arena->Arealloc(_out,_outmax*sizeof(Node*),new_max*sizeof(Node*));
709 //Copy::zero_to_bytes(&_out[_outmax], (new_max-_outmax)*sizeof(Node*)); // NULL all new space
710 _outmax = new_max; // Record new max length
711 // This assertion makes sure that Node::_max is wide enough to
712 // represent the numerical value of new_max.
713 assert(_outmax == new_max && _outmax > len, "int width of _outmax is too small");
714 }
715
716 #ifdef ASSERT
717 //------------------------------is_dead----------------------------------------
is_dead() const718 bool Node::is_dead() const {
719 // Mach and pinch point nodes may look like dead.
720 if( is_top() || is_Mach() || (Opcode() == Op_Node && _outcnt > 0) )
721 return false;
722 for( uint i = 0; i < _max; i++ )
723 if( _in[i] != NULL )
724 return false;
725 dump();
726 return true;
727 }
728
is_reachable_from_root() const729 bool Node::is_reachable_from_root() const {
730 ResourceMark rm;
731 Unique_Node_List wq;
732 wq.push((Node*)this);
733 RootNode* root = Compile::current()->root();
734 for (uint i = 0; i < wq.size(); i++) {
735 Node* m = wq.at(i);
736 if (m == root) {
737 return true;
738 }
739 for (DUIterator_Fast jmax, j = m->fast_outs(jmax); j < jmax; j++) {
740 Node* u = m->fast_out(j);
741 wq.push(u);
742 }
743 }
744 return false;
745 }
746 #endif
747
748 //------------------------------is_unreachable---------------------------------
is_unreachable(PhaseIterGVN & igvn) const749 bool Node::is_unreachable(PhaseIterGVN &igvn) const {
750 assert(!is_Mach(), "doesn't work with MachNodes");
751 return outcnt() == 0 || igvn.type(this) == Type::TOP || (in(0) != NULL && in(0)->is_top());
752 }
753
754 //------------------------------add_req----------------------------------------
755 // Add a new required input at the end
add_req(Node * n)756 void Node::add_req( Node *n ) {
757 assert( is_not_dead(n), "can not use dead node");
758
759 // Look to see if I can move precedence down one without reallocating
760 if( (_cnt >= _max) || (in(_max-1) != NULL) )
761 grow( _max+1 );
762
763 // Find a precedence edge to move
764 if( in(_cnt) != NULL ) { // Next precedence edge is busy?
765 uint i;
766 for( i=_cnt; i<_max; i++ )
767 if( in(i) == NULL ) // Find the NULL at end of prec edge list
768 break; // There must be one, since we grew the array
769 _in[i] = in(_cnt); // Move prec over, making space for req edge
770 }
771 _in[_cnt++] = n; // Stuff over old prec edge
772 if (n != NULL) n->add_out((Node *)this);
773 }
774
775 //---------------------------add_req_batch-------------------------------------
776 // Add a new required input at the end
add_req_batch(Node * n,uint m)777 void Node::add_req_batch( Node *n, uint m ) {
778 assert( is_not_dead(n), "can not use dead node");
779 // check various edge cases
780 if ((int)m <= 1) {
781 assert((int)m >= 0, "oob");
782 if (m != 0) add_req(n);
783 return;
784 }
785
786 // Look to see if I can move precedence down one without reallocating
787 if( (_cnt+m) > _max || _in[_max-m] )
788 grow( _max+m );
789
790 // Find a precedence edge to move
791 if( _in[_cnt] != NULL ) { // Next precedence edge is busy?
792 uint i;
793 for( i=_cnt; i<_max; i++ )
794 if( _in[i] == NULL ) // Find the NULL at end of prec edge list
795 break; // There must be one, since we grew the array
796 // Slide all the precs over by m positions (assume #prec << m).
797 Copy::conjoint_words_to_higher((HeapWord*)&_in[_cnt], (HeapWord*)&_in[_cnt+m], ((i-_cnt)*sizeof(Node*)));
798 }
799
800 // Stuff over the old prec edges
801 for(uint i=0; i<m; i++ ) {
802 _in[_cnt++] = n;
803 }
804
805 // Insert multiple out edges on the node.
806 if (n != NULL && !n->is_top()) {
807 for(uint i=0; i<m; i++ ) {
808 n->add_out((Node *)this);
809 }
810 }
811 }
812
813 //------------------------------del_req----------------------------------------
814 // Delete the required edge and compact the edge array
del_req(uint idx)815 void Node::del_req( uint idx ) {
816 assert( idx < _cnt, "oob");
817 assert( !VerifyHashTableKeys || _hash_lock == 0,
818 "remove node from hash table before modifying it");
819 // First remove corresponding def-use edge
820 Node *n = in(idx);
821 if (n != NULL) n->del_out((Node *)this);
822 _in[idx] = in(--_cnt); // Compact the array
823 // Avoid spec violation: Gap in prec edges.
824 close_prec_gap_at(_cnt);
825 Compile::current()->record_modified_node(this);
826 }
827
828 //------------------------------del_req_ordered--------------------------------
829 // Delete the required edge and compact the edge array with preserved order
del_req_ordered(uint idx)830 void Node::del_req_ordered( uint idx ) {
831 assert( idx < _cnt, "oob");
832 assert( !VerifyHashTableKeys || _hash_lock == 0,
833 "remove node from hash table before modifying it");
834 // First remove corresponding def-use edge
835 Node *n = in(idx);
836 if (n != NULL) n->del_out((Node *)this);
837 if (idx < --_cnt) { // Not last edge ?
838 Copy::conjoint_words_to_lower((HeapWord*)&_in[idx+1], (HeapWord*)&_in[idx], ((_cnt-idx)*sizeof(Node*)));
839 }
840 // Avoid spec violation: Gap in prec edges.
841 close_prec_gap_at(_cnt);
842 Compile::current()->record_modified_node(this);
843 }
844
845 //------------------------------ins_req----------------------------------------
846 // Insert a new required input at the end
ins_req(uint idx,Node * n)847 void Node::ins_req( uint idx, Node *n ) {
848 assert( is_not_dead(n), "can not use dead node");
849 add_req(NULL); // Make space
850 assert( idx < _max, "Must have allocated enough space");
851 // Slide over
852 if(_cnt-idx-1 > 0) {
853 Copy::conjoint_words_to_higher((HeapWord*)&_in[idx], (HeapWord*)&_in[idx+1], ((_cnt-idx-1)*sizeof(Node*)));
854 }
855 _in[idx] = n; // Stuff over old required edge
856 if (n != NULL) n->add_out((Node *)this); // Add reciprocal def-use edge
857 }
858
859 //-----------------------------find_edge---------------------------------------
find_edge(Node * n)860 int Node::find_edge(Node* n) {
861 for (uint i = 0; i < len(); i++) {
862 if (_in[i] == n) return i;
863 }
864 return -1;
865 }
866
867 //----------------------------replace_edge-------------------------------------
replace_edge(Node * old,Node * neww)868 int Node::replace_edge(Node* old, Node* neww) {
869 if (old == neww) return 0; // nothing to do
870 uint nrep = 0;
871 for (uint i = 0; i < len(); i++) {
872 if (in(i) == old) {
873 if (i < req()) {
874 set_req(i, neww);
875 } else {
876 assert(find_prec_edge(neww) == -1, "spec violation: duplicated prec edge (node %d -> %d)", _idx, neww->_idx);
877 set_prec(i, neww);
878 }
879 nrep++;
880 }
881 }
882 return nrep;
883 }
884
885 /**
886 * Replace input edges in the range pointing to 'old' node.
887 */
replace_edges_in_range(Node * old,Node * neww,int start,int end)888 int Node::replace_edges_in_range(Node* old, Node* neww, int start, int end) {
889 if (old == neww) return 0; // nothing to do
890 uint nrep = 0;
891 for (int i = start; i < end; i++) {
892 if (in(i) == old) {
893 set_req(i, neww);
894 nrep++;
895 }
896 }
897 return nrep;
898 }
899
900 //-------------------------disconnect_inputs-----------------------------------
901 // NULL out all inputs to eliminate incoming Def-Use edges.
disconnect_inputs(Compile * C)902 void Node::disconnect_inputs(Compile* C) {
903 // the layout of Node::_in
904 // r: a required input, null is allowed
905 // p: a precedence, null values are all at the end
906 // -----------------------------------
907 // |r|...|r|p|...|p|null|...|null|
908 // | |
909 // req() len()
910 // -----------------------------------
911 for (uint i = 0; i < req(); ++i) {
912 if (in(i) != nullptr) {
913 set_req(i, nullptr);
914 }
915 }
916
917 // Remove precedence edges if any exist
918 // Note: Safepoints may have precedence edges, even during parsing
919 for (uint i = len(); i > req(); ) {
920 rm_prec(--i); // no-op if _in[i] is nullptr
921 }
922
923 #ifdef ASSERT
924 // sanity check
925 for (uint i = 0; i < len(); ++i) {
926 assert(_in[i] == nullptr, "disconnect_inputs() failed!");
927 }
928 #endif
929
930 // Node::destruct requires all out edges be deleted first
931 // debug_only(destruct();) // no reuse benefit expected
932 C->record_dead_node(_idx);
933 }
934
935 //-----------------------------uncast---------------------------------------
936 // %%% Temporary, until we sort out CheckCastPP vs. CastPP.
937 // Strip away casting. (It is depth-limited.)
938 // Optionally, keep casts with dependencies.
uncast(bool keep_deps) const939 Node* Node::uncast(bool keep_deps) const {
940 // Should be inline:
941 //return is_ConstraintCast() ? uncast_helper(this) : (Node*) this;
942 if (is_ConstraintCast()) {
943 return uncast_helper(this, keep_deps);
944 } else {
945 return (Node*) this;
946 }
947 }
948
949 // Find out of current node that matches opcode.
find_out_with(int opcode)950 Node* Node::find_out_with(int opcode) {
951 for (DUIterator_Fast imax, i = fast_outs(imax); i < imax; i++) {
952 Node* use = fast_out(i);
953 if (use->Opcode() == opcode) {
954 return use;
955 }
956 }
957 return NULL;
958 }
959
960 // Return true if the current node has an out that matches opcode.
has_out_with(int opcode)961 bool Node::has_out_with(int opcode) {
962 return (find_out_with(opcode) != NULL);
963 }
964
965 // Return true if the current node has an out that matches any of the opcodes.
has_out_with(int opcode1,int opcode2,int opcode3,int opcode4)966 bool Node::has_out_with(int opcode1, int opcode2, int opcode3, int opcode4) {
967 for (DUIterator_Fast imax, i = fast_outs(imax); i < imax; i++) {
968 int opcode = fast_out(i)->Opcode();
969 if (opcode == opcode1 || opcode == opcode2 || opcode == opcode3 || opcode == opcode4) {
970 return true;
971 }
972 }
973 return false;
974 }
975
976
977 //---------------------------uncast_helper-------------------------------------
uncast_helper(const Node * p,bool keep_deps)978 Node* Node::uncast_helper(const Node* p, bool keep_deps) {
979 #ifdef ASSERT
980 uint depth_count = 0;
981 const Node* orig_p = p;
982 #endif
983
984 while (true) {
985 #ifdef ASSERT
986 if (depth_count >= K) {
987 orig_p->dump(4);
988 if (p != orig_p)
989 p->dump(1);
990 }
991 assert(depth_count++ < K, "infinite loop in Node::uncast_helper");
992 #endif
993 if (p == NULL || p->req() != 2) {
994 break;
995 } else if (p->is_ConstraintCast()) {
996 if (keep_deps && p->as_ConstraintCast()->carry_dependency()) {
997 break; // stop at casts with dependencies
998 }
999 p = p->in(1);
1000 } else {
1001 break;
1002 }
1003 }
1004 return (Node*) p;
1005 }
1006
1007 //------------------------------add_prec---------------------------------------
1008 // Add a new precedence input. Precedence inputs are unordered, with
1009 // duplicates removed and NULLs packed down at the end.
add_prec(Node * n)1010 void Node::add_prec( Node *n ) {
1011 assert( is_not_dead(n), "can not use dead node");
1012
1013 // Check for NULL at end
1014 if( _cnt >= _max || in(_max-1) )
1015 grow( _max+1 );
1016
1017 // Find a precedence edge to move
1018 uint i = _cnt;
1019 while( in(i) != NULL ) {
1020 if (in(i) == n) return; // Avoid spec violation: duplicated prec edge.
1021 i++;
1022 }
1023 _in[i] = n; // Stuff prec edge over NULL
1024 if ( n != NULL) n->add_out((Node *)this); // Add mirror edge
1025
1026 #ifdef ASSERT
1027 while ((++i)<_max) { assert(_in[i] == NULL, "spec violation: Gap in prec edges (node %d)", _idx); }
1028 #endif
1029 }
1030
1031 //------------------------------rm_prec----------------------------------------
1032 // Remove a precedence input. Precedence inputs are unordered, with
1033 // duplicates removed and NULLs packed down at the end.
rm_prec(uint j)1034 void Node::rm_prec( uint j ) {
1035 assert(j < _max, "oob: i=%d, _max=%d", j, _max);
1036 assert(j >= _cnt, "not a precedence edge");
1037 if (_in[j] == NULL) return; // Avoid spec violation: Gap in prec edges.
1038 _in[j]->del_out((Node *)this);
1039 close_prec_gap_at(j);
1040 }
1041
1042 //------------------------------size_of----------------------------------------
size_of() const1043 uint Node::size_of() const { return sizeof(*this); }
1044
1045 //------------------------------ideal_reg--------------------------------------
ideal_reg() const1046 uint Node::ideal_reg() const { return 0; }
1047
1048 //------------------------------jvms-------------------------------------------
jvms() const1049 JVMState* Node::jvms() const { return NULL; }
1050
1051 #ifdef ASSERT
1052 //------------------------------jvms-------------------------------------------
verify_jvms(const JVMState * using_jvms) const1053 bool Node::verify_jvms(const JVMState* using_jvms) const {
1054 for (JVMState* jvms = this->jvms(); jvms != NULL; jvms = jvms->caller()) {
1055 if (jvms == using_jvms) return true;
1056 }
1057 return false;
1058 }
1059
1060 //------------------------------init_NodeProperty------------------------------
init_NodeProperty()1061 void Node::init_NodeProperty() {
1062 assert(_max_classes <= max_juint, "too many NodeProperty classes");
1063 assert(max_flags() <= max_juint, "too many NodeProperty flags");
1064 }
1065
1066 //-----------------------------max_flags---------------------------------------
max_flags()1067 juint Node::max_flags() {
1068 return (PD::_last_flag << 1) - 1; // allow flags combination
1069 }
1070 #endif
1071
1072 //------------------------------format-----------------------------------------
1073 // Print as assembly
format(PhaseRegAlloc *,outputStream * st) const1074 void Node::format( PhaseRegAlloc *, outputStream *st ) const {}
1075 //------------------------------emit-------------------------------------------
1076 // Emit bytes starting at parameter 'ptr'.
emit(CodeBuffer & cbuf,PhaseRegAlloc * ra_) const1077 void Node::emit(CodeBuffer &cbuf, PhaseRegAlloc *ra_) const {}
1078 //------------------------------size-------------------------------------------
1079 // Size of instruction in bytes
size(PhaseRegAlloc * ra_) const1080 uint Node::size(PhaseRegAlloc *ra_) const { return 0; }
1081
1082 //------------------------------CFG Construction-------------------------------
1083 // Nodes that end basic blocks, e.g. IfTrue/IfFalse, JumpProjNode, Root,
1084 // Goto and Return.
is_block_proj() const1085 const Node *Node::is_block_proj() const { return 0; }
1086
1087 // Minimum guaranteed type
bottom_type() const1088 const Type *Node::bottom_type() const { return Type::BOTTOM; }
1089
1090
1091 //------------------------------raise_bottom_type------------------------------
1092 // Get the worst-case Type output for this Node.
raise_bottom_type(const Type * new_type)1093 void Node::raise_bottom_type(const Type* new_type) {
1094 if (is_Type()) {
1095 TypeNode *n = this->as_Type();
1096 if (VerifyAliases) {
1097 assert(new_type->higher_equal_speculative(n->type()), "new type must refine old type");
1098 }
1099 n->set_type(new_type);
1100 } else if (is_Load()) {
1101 LoadNode *n = this->as_Load();
1102 if (VerifyAliases) {
1103 assert(new_type->higher_equal_speculative(n->type()), "new type must refine old type");
1104 }
1105 n->set_type(new_type);
1106 }
1107 }
1108
1109 //------------------------------Identity---------------------------------------
1110 // Return a node that the given node is equivalent to.
Identity(PhaseGVN * phase)1111 Node* Node::Identity(PhaseGVN* phase) {
1112 return this; // Default to no identities
1113 }
1114
1115 //------------------------------Value------------------------------------------
1116 // Compute a new Type for a node using the Type of the inputs.
Value(PhaseGVN * phase) const1117 const Type* Node::Value(PhaseGVN* phase) const {
1118 return bottom_type(); // Default to worst-case Type
1119 }
1120
1121 //------------------------------Ideal------------------------------------------
1122 //
1123 // 'Idealize' the graph rooted at this Node.
1124 //
1125 // In order to be efficient and flexible there are some subtle invariants
1126 // these Ideal calls need to hold. Running with '+VerifyIterativeGVN' checks
1127 // these invariants, although its too slow to have on by default. If you are
1128 // hacking an Ideal call, be sure to test with +VerifyIterativeGVN!
1129 //
1130 // The Ideal call almost arbitrarily reshape the graph rooted at the 'this'
1131 // pointer. If ANY change is made, it must return the root of the reshaped
1132 // graph - even if the root is the same Node. Example: swapping the inputs
1133 // to an AddINode gives the same answer and same root, but you still have to
1134 // return the 'this' pointer instead of NULL.
1135 //
1136 // You cannot return an OLD Node, except for the 'this' pointer. Use the
1137 // Identity call to return an old Node; basically if Identity can find
1138 // another Node have the Ideal call make no change and return NULL.
1139 // Example: AddINode::Ideal must check for add of zero; in this case it
1140 // returns NULL instead of doing any graph reshaping.
1141 //
1142 // You cannot modify any old Nodes except for the 'this' pointer. Due to
1143 // sharing there may be other users of the old Nodes relying on their current
1144 // semantics. Modifying them will break the other users.
1145 // Example: when reshape "(X+3)+4" into "X+7" you must leave the Node for
1146 // "X+3" unchanged in case it is shared.
1147 //
1148 // If you modify the 'this' pointer's inputs, you should use
1149 // 'set_req'. If you are making a new Node (either as the new root or
1150 // some new internal piece) you may use 'init_req' to set the initial
1151 // value. You can make a new Node with either 'new' or 'clone'. In
1152 // either case, def-use info is correctly maintained.
1153 //
1154 // Example: reshape "(X+3)+4" into "X+7":
1155 // set_req(1, in(1)->in(1));
1156 // set_req(2, phase->intcon(7));
1157 // return this;
1158 // Example: reshape "X*4" into "X<<2"
1159 // return new LShiftINode(in(1), phase->intcon(2));
1160 //
1161 // You must call 'phase->transform(X)' on any new Nodes X you make, except
1162 // for the returned root node. Example: reshape "X*31" with "(X<<5)-X".
1163 // Node *shift=phase->transform(new LShiftINode(in(1),phase->intcon(5)));
1164 // return new AddINode(shift, in(1));
1165 //
1166 // When making a Node for a constant use 'phase->makecon' or 'phase->intcon'.
1167 // These forms are faster than 'phase->transform(new ConNode())' and Do
1168 // The Right Thing with def-use info.
1169 //
1170 // You cannot bury the 'this' Node inside of a graph reshape. If the reshaped
1171 // graph uses the 'this' Node it must be the root. If you want a Node with
1172 // the same Opcode as the 'this' pointer use 'clone'.
1173 //
Ideal(PhaseGVN * phase,bool can_reshape)1174 Node *Node::Ideal(PhaseGVN *phase, bool can_reshape) {
1175 return NULL; // Default to being Ideal already
1176 }
1177
1178 // Some nodes have specific Ideal subgraph transformations only if they are
1179 // unique users of specific nodes. Such nodes should be put on IGVN worklist
1180 // for the transformations to happen.
has_special_unique_user() const1181 bool Node::has_special_unique_user() const {
1182 assert(outcnt() == 1, "match only for unique out");
1183 Node* n = unique_out();
1184 int op = Opcode();
1185 if (this->is_Store()) {
1186 // Condition for back-to-back stores folding.
1187 return n->Opcode() == op && n->in(MemNode::Memory) == this;
1188 } else if (this->is_Load() || this->is_DecodeN() || this->is_Phi()) {
1189 // Condition for removing an unused LoadNode or DecodeNNode from the MemBarAcquire precedence input
1190 return n->Opcode() == Op_MemBarAcquire;
1191 } else if (op == Op_AddL) {
1192 // Condition for convL2I(addL(x,y)) ==> addI(convL2I(x),convL2I(y))
1193 return n->Opcode() == Op_ConvL2I && n->in(1) == this;
1194 } else if (op == Op_SubI || op == Op_SubL) {
1195 // Condition for subI(x,subI(y,z)) ==> subI(addI(x,z),y)
1196 return n->Opcode() == op && n->in(2) == this;
1197 } else if (is_If() && (n->is_IfFalse() || n->is_IfTrue())) {
1198 // See IfProjNode::Identity()
1199 return true;
1200 } else {
1201 return false;
1202 }
1203 };
1204
1205 //--------------------------find_exact_control---------------------------------
1206 // Skip Proj and CatchProj nodes chains. Check for Null and Top.
find_exact_control(Node * ctrl)1207 Node* Node::find_exact_control(Node* ctrl) {
1208 if (ctrl == NULL && this->is_Region())
1209 ctrl = this->as_Region()->is_copy();
1210
1211 if (ctrl != NULL && ctrl->is_CatchProj()) {
1212 if (ctrl->as_CatchProj()->_con == CatchProjNode::fall_through_index)
1213 ctrl = ctrl->in(0);
1214 if (ctrl != NULL && !ctrl->is_top())
1215 ctrl = ctrl->in(0);
1216 }
1217
1218 if (ctrl != NULL && ctrl->is_Proj())
1219 ctrl = ctrl->in(0);
1220
1221 return ctrl;
1222 }
1223
1224 //--------------------------dominates------------------------------------------
1225 // Helper function for MemNode::all_controls_dominate().
1226 // Check if 'this' control node dominates or equal to 'sub' control node.
1227 // We already know that if any path back to Root or Start reaches 'this',
1228 // then all paths so, so this is a simple search for one example,
1229 // not an exhaustive search for a counterexample.
dominates(Node * sub,Node_List & nlist)1230 bool Node::dominates(Node* sub, Node_List &nlist) {
1231 assert(this->is_CFG(), "expecting control");
1232 assert(sub != NULL && sub->is_CFG(), "expecting control");
1233
1234 // detect dead cycle without regions
1235 int iterations_without_region_limit = DominatorSearchLimit;
1236
1237 Node* orig_sub = sub;
1238 Node* dom = this;
1239 bool met_dom = false;
1240 nlist.clear();
1241
1242 // Walk 'sub' backward up the chain to 'dom', watching for regions.
1243 // After seeing 'dom', continue up to Root or Start.
1244 // If we hit a region (backward split point), it may be a loop head.
1245 // Keep going through one of the region's inputs. If we reach the
1246 // same region again, go through a different input. Eventually we
1247 // will either exit through the loop head, or give up.
1248 // (If we get confused, break out and return a conservative 'false'.)
1249 while (sub != NULL) {
1250 if (sub->is_top()) break; // Conservative answer for dead code.
1251 if (sub == dom) {
1252 if (nlist.size() == 0) {
1253 // No Region nodes except loops were visited before and the EntryControl
1254 // path was taken for loops: it did not walk in a cycle.
1255 return true;
1256 } else if (met_dom) {
1257 break; // already met before: walk in a cycle
1258 } else {
1259 // Region nodes were visited. Continue walk up to Start or Root
1260 // to make sure that it did not walk in a cycle.
1261 met_dom = true; // first time meet
1262 iterations_without_region_limit = DominatorSearchLimit; // Reset
1263 }
1264 }
1265 if (sub->is_Start() || sub->is_Root()) {
1266 // Success if we met 'dom' along a path to Start or Root.
1267 // We assume there are no alternative paths that avoid 'dom'.
1268 // (This assumption is up to the caller to ensure!)
1269 return met_dom;
1270 }
1271 Node* up = sub->in(0);
1272 // Normalize simple pass-through regions and projections:
1273 up = sub->find_exact_control(up);
1274 // If sub == up, we found a self-loop. Try to push past it.
1275 if (sub == up && sub->is_Loop()) {
1276 // Take loop entry path on the way up to 'dom'.
1277 up = sub->in(1); // in(LoopNode::EntryControl);
1278 } else if (sub == up && sub->is_Region() && sub->req() == 2) {
1279 // Take in(1) path on the way up to 'dom' for regions with only one input
1280 up = sub->in(1);
1281 } else if (sub == up && sub->is_Region() && sub->req() == 3) {
1282 // Try both paths for Regions with 2 input paths (it may be a loop head).
1283 // It could give conservative 'false' answer without information
1284 // which region's input is the entry path.
1285 iterations_without_region_limit = DominatorSearchLimit; // Reset
1286
1287 bool region_was_visited_before = false;
1288 // Was this Region node visited before?
1289 // If so, we have reached it because we accidentally took a
1290 // loop-back edge from 'sub' back into the body of the loop,
1291 // and worked our way up again to the loop header 'sub'.
1292 // So, take the first unexplored path on the way up to 'dom'.
1293 for (int j = nlist.size() - 1; j >= 0; j--) {
1294 intptr_t ni = (intptr_t)nlist.at(j);
1295 Node* visited = (Node*)(ni & ~1);
1296 bool visited_twice_already = ((ni & 1) != 0);
1297 if (visited == sub) {
1298 if (visited_twice_already) {
1299 // Visited 2 paths, but still stuck in loop body. Give up.
1300 return false;
1301 }
1302 // The Region node was visited before only once.
1303 // (We will repush with the low bit set, below.)
1304 nlist.remove(j);
1305 // We will find a new edge and re-insert.
1306 region_was_visited_before = true;
1307 break;
1308 }
1309 }
1310
1311 // Find an incoming edge which has not been seen yet; walk through it.
1312 assert(up == sub, "");
1313 uint skip = region_was_visited_before ? 1 : 0;
1314 for (uint i = 1; i < sub->req(); i++) {
1315 Node* in = sub->in(i);
1316 if (in != NULL && !in->is_top() && in != sub) {
1317 if (skip == 0) {
1318 up = in;
1319 break;
1320 }
1321 --skip; // skip this nontrivial input
1322 }
1323 }
1324
1325 // Set 0 bit to indicate that both paths were taken.
1326 nlist.push((Node*)((intptr_t)sub + (region_was_visited_before ? 1 : 0)));
1327 }
1328
1329 if (up == sub) {
1330 break; // some kind of tight cycle
1331 }
1332 if (up == orig_sub && met_dom) {
1333 // returned back after visiting 'dom'
1334 break; // some kind of cycle
1335 }
1336 if (--iterations_without_region_limit < 0) {
1337 break; // dead cycle
1338 }
1339 sub = up;
1340 }
1341
1342 // Did not meet Root or Start node in pred. chain.
1343 // Conservative answer for dead code.
1344 return false;
1345 }
1346
1347 //------------------------------remove_dead_region-----------------------------
1348 // This control node is dead. Follow the subgraph below it making everything
1349 // using it dead as well. This will happen normally via the usual IterGVN
1350 // worklist but this call is more efficient. Do not update use-def info
1351 // inside the dead region, just at the borders.
kill_dead_code(Node * dead,PhaseIterGVN * igvn)1352 static void kill_dead_code( Node *dead, PhaseIterGVN *igvn ) {
1353 // Con's are a popular node to re-hit in the hash table again.
1354 if( dead->is_Con() ) return;
1355
1356 ResourceMark rm;
1357 Node_List nstack;
1358
1359 Node *top = igvn->C->top();
1360 nstack.push(dead);
1361 bool has_irreducible_loop = igvn->C->has_irreducible_loop();
1362
1363 while (nstack.size() > 0) {
1364 dead = nstack.pop();
1365 if (dead->Opcode() == Op_SafePoint) {
1366 dead->as_SafePoint()->disconnect_from_root(igvn);
1367 }
1368 if (dead->outcnt() > 0) {
1369 // Keep dead node on stack until all uses are processed.
1370 nstack.push(dead);
1371 // For all Users of the Dead... ;-)
1372 for (DUIterator_Last kmin, k = dead->last_outs(kmin); k >= kmin; ) {
1373 Node* use = dead->last_out(k);
1374 igvn->hash_delete(use); // Yank from hash table prior to mod
1375 if (use->in(0) == dead) { // Found another dead node
1376 assert (!use->is_Con(), "Control for Con node should be Root node.");
1377 use->set_req(0, top); // Cut dead edge to prevent processing
1378 nstack.push(use); // the dead node again.
1379 } else if (!has_irreducible_loop && // Backedge could be alive in irreducible loop
1380 use->is_Loop() && !use->is_Root() && // Don't kill Root (RootNode extends LoopNode)
1381 use->in(LoopNode::EntryControl) == dead) { // Dead loop if its entry is dead
1382 use->set_req(LoopNode::EntryControl, top); // Cut dead edge to prevent processing
1383 use->set_req(0, top); // Cut self edge
1384 nstack.push(use);
1385 } else { // Else found a not-dead user
1386 // Dead if all inputs are top or null
1387 bool dead_use = !use->is_Root(); // Keep empty graph alive
1388 for (uint j = 1; j < use->req(); j++) {
1389 Node* in = use->in(j);
1390 if (in == dead) { // Turn all dead inputs into TOP
1391 use->set_req(j, top);
1392 } else if (in != NULL && !in->is_top()) {
1393 dead_use = false;
1394 }
1395 }
1396 if (dead_use) {
1397 if (use->is_Region()) {
1398 use->set_req(0, top); // Cut self edge
1399 }
1400 nstack.push(use);
1401 } else {
1402 igvn->_worklist.push(use);
1403 }
1404 }
1405 // Refresh the iterator, since any number of kills might have happened.
1406 k = dead->last_outs(kmin);
1407 }
1408 } else { // (dead->outcnt() == 0)
1409 // Done with outputs.
1410 igvn->hash_delete(dead);
1411 igvn->_worklist.remove(dead);
1412 igvn->set_type(dead, Type::TOP);
1413 // Kill all inputs to the dead guy
1414 for (uint i=0; i < dead->req(); i++) {
1415 Node *n = dead->in(i); // Get input to dead guy
1416 if (n != NULL && !n->is_top()) { // Input is valid?
1417 dead->set_req(i, top); // Smash input away
1418 if (n->outcnt() == 0) { // Input also goes dead?
1419 if (!n->is_Con())
1420 nstack.push(n); // Clear it out as well
1421 } else if (n->outcnt() == 1 &&
1422 n->has_special_unique_user()) {
1423 igvn->add_users_to_worklist( n );
1424 } else if (n->outcnt() <= 2 && n->is_Store()) {
1425 // Push store's uses on worklist to enable folding optimization for
1426 // store/store and store/load to the same address.
1427 // The restriction (outcnt() <= 2) is the same as in set_req_X()
1428 // and remove_globally_dead_node().
1429 igvn->add_users_to_worklist( n );
1430 } else {
1431 BarrierSet::barrier_set()->barrier_set_c2()->enqueue_useful_gc_barrier(igvn, n);
1432 }
1433 }
1434 }
1435 igvn->C->remove_useless_node(dead);
1436 } // (dead->outcnt() == 0)
1437 } // while (nstack.size() > 0) for outputs
1438 return;
1439 }
1440
1441 //------------------------------remove_dead_region-----------------------------
remove_dead_region(PhaseGVN * phase,bool can_reshape)1442 bool Node::remove_dead_region(PhaseGVN *phase, bool can_reshape) {
1443 Node *n = in(0);
1444 if( !n ) return false;
1445 // Lost control into this guy? I.e., it became unreachable?
1446 // Aggressively kill all unreachable code.
1447 if (can_reshape && n->is_top()) {
1448 kill_dead_code(this, phase->is_IterGVN());
1449 return false; // Node is dead.
1450 }
1451
1452 if( n->is_Region() && n->as_Region()->is_copy() ) {
1453 Node *m = n->nonnull_req();
1454 set_req(0, m);
1455 return true;
1456 }
1457 return false;
1458 }
1459
1460 //------------------------------hash-------------------------------------------
1461 // Hash function over Nodes.
hash() const1462 uint Node::hash() const {
1463 uint sum = 0;
1464 for( uint i=0; i<_cnt; i++ ) // Add in all inputs
1465 sum = (sum<<1)-(uintptr_t)in(i); // Ignore embedded NULLs
1466 return (sum>>2) + _cnt + Opcode();
1467 }
1468
1469 //------------------------------cmp--------------------------------------------
1470 // Compare special parts of simple Nodes
cmp(const Node & n) const1471 bool Node::cmp( const Node &n ) const {
1472 return true; // Must be same
1473 }
1474
1475 //------------------------------rematerialize-----------------------------------
1476 // Should we clone rather than spill this instruction?
rematerialize() const1477 bool Node::rematerialize() const {
1478 if ( is_Mach() )
1479 return this->as_Mach()->rematerialize();
1480 else
1481 return (_flags & Flag_rematerialize) != 0;
1482 }
1483
1484 //------------------------------needs_anti_dependence_check---------------------
1485 // Nodes which use memory without consuming it, hence need antidependences.
needs_anti_dependence_check() const1486 bool Node::needs_anti_dependence_check() const {
1487 if (req() < 2 || (_flags & Flag_needs_anti_dependence_check) == 0) {
1488 return false;
1489 }
1490 return in(1)->bottom_type()->has_memory();
1491 }
1492
1493 // Get an integer constant from a ConNode (or CastIINode).
1494 // Return a default value if there is no apparent constant here.
find_int_type() const1495 const TypeInt* Node::find_int_type() const {
1496 if (this->is_Type()) {
1497 return this->as_Type()->type()->isa_int();
1498 } else if (this->is_Con()) {
1499 assert(is_Mach(), "should be ConNode(TypeNode) or else a MachNode");
1500 return this->bottom_type()->isa_int();
1501 }
1502 return NULL;
1503 }
1504
find_integer_type(BasicType bt) const1505 const TypeInteger* Node::find_integer_type(BasicType bt) const {
1506 if (this->is_Type()) {
1507 return this->as_Type()->type()->isa_integer(bt);
1508 } else if (this->is_Con()) {
1509 assert(is_Mach(), "should be ConNode(TypeNode) or else a MachNode");
1510 return this->bottom_type()->isa_integer(bt);
1511 }
1512 return NULL;
1513 }
1514
1515 // Get a pointer constant from a ConstNode.
1516 // Returns the constant if it is a pointer ConstNode
get_ptr() const1517 intptr_t Node::get_ptr() const {
1518 assert( Opcode() == Op_ConP, "" );
1519 return ((ConPNode*)this)->type()->is_ptr()->get_con();
1520 }
1521
1522 // Get a narrow oop constant from a ConNNode.
get_narrowcon() const1523 intptr_t Node::get_narrowcon() const {
1524 assert( Opcode() == Op_ConN, "" );
1525 return ((ConNNode*)this)->type()->is_narrowoop()->get_con();
1526 }
1527
1528 // Get a long constant from a ConNode.
1529 // Return a default value if there is no apparent constant here.
find_long_type() const1530 const TypeLong* Node::find_long_type() const {
1531 if (this->is_Type()) {
1532 return this->as_Type()->type()->isa_long();
1533 } else if (this->is_Con()) {
1534 assert(is_Mach(), "should be ConNode(TypeNode) or else a MachNode");
1535 return this->bottom_type()->isa_long();
1536 }
1537 return NULL;
1538 }
1539
1540
1541 /**
1542 * Return a ptr type for nodes which should have it.
1543 */
get_ptr_type() const1544 const TypePtr* Node::get_ptr_type() const {
1545 const TypePtr* tp = this->bottom_type()->make_ptr();
1546 #ifdef ASSERT
1547 if (tp == NULL) {
1548 this->dump(1);
1549 assert((tp != NULL), "unexpected node type");
1550 }
1551 #endif
1552 return tp;
1553 }
1554
1555 // Get a double constant from a ConstNode.
1556 // Returns the constant if it is a double ConstNode
getd() const1557 jdouble Node::getd() const {
1558 assert( Opcode() == Op_ConD, "" );
1559 return ((ConDNode*)this)->type()->is_double_constant()->getd();
1560 }
1561
1562 // Get a float constant from a ConstNode.
1563 // Returns the constant if it is a float ConstNode
getf() const1564 jfloat Node::getf() const {
1565 assert( Opcode() == Op_ConF, "" );
1566 return ((ConFNode*)this)->type()->is_float_constant()->getf();
1567 }
1568
1569 #ifndef PRODUCT
1570
1571 // Call this from debugger:
find_node(Node * n,const int idx)1572 Node* find_node(Node* n, const int idx) {
1573 return n->find(idx);
1574 }
1575
1576 // Call this from debugger with root node as default:
find_node(const int idx)1577 Node* find_node(const int idx) {
1578 return Compile::current()->root()->find(idx);
1579 }
1580
1581 // Call this from debugger:
find_ctrl(Node * n,const int idx)1582 Node* find_ctrl(Node* n, const int idx) {
1583 return n->find_ctrl(idx);
1584 }
1585
1586 // Call this from debugger with root node as default:
find_ctrl(const int idx)1587 Node* find_ctrl(const int idx) {
1588 return Compile::current()->root()->find_ctrl(idx);
1589 }
1590
1591 //------------------------------find_ctrl--------------------------------------
1592 // Find an ancestor to this node in the control history with given _idx
find_ctrl(int idx)1593 Node* Node::find_ctrl(int idx) {
1594 return find(idx, true);
1595 }
1596
1597 //------------------------------find-------------------------------------------
1598 // Tries to find the node with the index |idx| starting from this node. If idx is negative,
1599 // the search also includes forward (out) edges. Returns NULL if not found.
1600 // If only_ctrl is set, the search will only be done on control nodes. Returns NULL if
1601 // not found or if the node to be found is not a control node (search will not find it).
find(const int idx,bool only_ctrl)1602 Node* Node::find(const int idx, bool only_ctrl) {
1603 ResourceMark rm;
1604 VectorSet old_space;
1605 VectorSet new_space;
1606 Node_List worklist;
1607 Arena* old_arena = Compile::current()->old_arena();
1608 add_to_worklist(this, &worklist, old_arena, &old_space, &new_space);
1609 Node* result = NULL;
1610 int node_idx = (idx >= 0) ? idx : -idx;
1611
1612 for (uint list_index = 0; list_index < worklist.size(); list_index++) {
1613 Node* n = worklist[list_index];
1614
1615 if ((int)n->_idx == node_idx debug_only(|| n->debug_idx() == node_idx)) {
1616 if (result != NULL) {
1617 tty->print("find: " INTPTR_FORMAT " and " INTPTR_FORMAT " both have idx==%d\n",
1618 (uintptr_t)result, (uintptr_t)n, node_idx);
1619 }
1620 result = n;
1621 }
1622
1623 for (uint i = 0; i < n->len(); i++) {
1624 if (!only_ctrl || n->is_Region() || (n->Opcode() == Op_Root) || (i == TypeFunc::Control)) {
1625 // If only_ctrl is set: Add regions, the root node, or control inputs only
1626 add_to_worklist(n->in(i), &worklist, old_arena, &old_space, &new_space);
1627 }
1628 }
1629
1630 // Also search along forward edges if idx is negative and the search is not done on control nodes only
1631 if (idx < 0 && !only_ctrl) {
1632 for (uint i = 0; i < n->outcnt(); i++) {
1633 add_to_worklist(n->raw_out(i), &worklist, old_arena, &old_space, &new_space);
1634 }
1635 }
1636 #ifdef ASSERT
1637 // Search along debug_orig edges last
1638 Node* orig = n->debug_orig();
1639 while (orig != NULL && add_to_worklist(orig, &worklist, old_arena, &old_space, &new_space)) {
1640 orig = orig->debug_orig();
1641 }
1642 #endif // ASSERT
1643 }
1644 return result;
1645 }
1646
add_to_worklist(Node * n,Node_List * worklist,Arena * old_arena,VectorSet * old_space,VectorSet * new_space)1647 bool Node::add_to_worklist(Node* n, Node_List* worklist, Arena* old_arena, VectorSet* old_space, VectorSet* new_space) {
1648 if (NotANode(n)) {
1649 return false; // Gracefully handle NULL, -1, 0xabababab, etc.
1650 }
1651
1652 // Contained in new_space or old_space? Check old_arena first since it's mostly empty.
1653 VectorSet* v = old_arena->contains(n) ? old_space : new_space;
1654 if (!v->test_set(n->_idx)) {
1655 worklist->push(n);
1656 return true;
1657 }
1658 return false;
1659 }
1660
1661 // -----------------------------Name-------------------------------------------
1662 extern const char *NodeClassNames[];
Name() const1663 const char *Node::Name() const { return NodeClassNames[Opcode()]; }
1664
is_disconnected(const Node * n)1665 static bool is_disconnected(const Node* n) {
1666 for (uint i = 0; i < n->req(); i++) {
1667 if (n->in(i) != NULL) return false;
1668 }
1669 return true;
1670 }
1671
1672 #ifdef ASSERT
dump_orig(outputStream * st,bool print_key) const1673 void Node::dump_orig(outputStream *st, bool print_key) const {
1674 Compile* C = Compile::current();
1675 Node* orig = _debug_orig;
1676 if (NotANode(orig)) orig = NULL;
1677 if (orig != NULL && !C->node_arena()->contains(orig)) orig = NULL;
1678 if (orig == NULL) return;
1679 if (print_key) {
1680 st->print(" !orig=");
1681 }
1682 Node* fast = orig->debug_orig(); // tortoise & hare algorithm to detect loops
1683 if (NotANode(fast)) fast = NULL;
1684 while (orig != NULL) {
1685 bool discon = is_disconnected(orig); // if discon, print [123] else 123
1686 if (discon) st->print("[");
1687 if (!Compile::current()->node_arena()->contains(orig))
1688 st->print("o");
1689 st->print("%d", orig->_idx);
1690 if (discon) st->print("]");
1691 orig = orig->debug_orig();
1692 if (NotANode(orig)) orig = NULL;
1693 if (orig != NULL && !C->node_arena()->contains(orig)) orig = NULL;
1694 if (orig != NULL) st->print(",");
1695 if (fast != NULL) {
1696 // Step fast twice for each single step of orig:
1697 fast = fast->debug_orig();
1698 if (NotANode(fast)) fast = NULL;
1699 if (fast != NULL && fast != orig) {
1700 fast = fast->debug_orig();
1701 if (NotANode(fast)) fast = NULL;
1702 }
1703 if (fast == orig) {
1704 st->print("...");
1705 break;
1706 }
1707 }
1708 }
1709 }
1710
set_debug_orig(Node * orig)1711 void Node::set_debug_orig(Node* orig) {
1712 _debug_orig = orig;
1713 if (BreakAtNode == 0) return;
1714 if (NotANode(orig)) orig = NULL;
1715 int trip = 10;
1716 while (orig != NULL) {
1717 if (orig->debug_idx() == BreakAtNode || (int)orig->_idx == BreakAtNode) {
1718 tty->print_cr("BreakAtNode: _idx=%d _debug_idx=%d orig._idx=%d orig._debug_idx=%d",
1719 this->_idx, this->debug_idx(), orig->_idx, orig->debug_idx());
1720 BREAKPOINT;
1721 }
1722 orig = orig->debug_orig();
1723 if (NotANode(orig)) orig = NULL;
1724 if (trip-- <= 0) break;
1725 }
1726 }
1727 #endif //ASSERT
1728
1729 //------------------------------dump------------------------------------------
1730 // Dump a Node
dump(const char * suffix,bool mark,outputStream * st) const1731 void Node::dump(const char* suffix, bool mark, outputStream *st) const {
1732 Compile* C = Compile::current();
1733 bool is_new = C->node_arena()->contains(this);
1734 C->_in_dump_cnt++;
1735
1736 if (_indent > 0) {
1737 st->print("%*s", (_indent << 1), " ");
1738 }
1739
1740 st->print("%c%d%s%s === ", is_new ? ' ' : 'o', _idx, mark ? " >" : " ", Name());
1741
1742 // Dump the required and precedence inputs
1743 dump_req(st);
1744 dump_prec(st);
1745 // Dump the outputs
1746 dump_out(st);
1747
1748 if (is_disconnected(this)) {
1749 #ifdef ASSERT
1750 st->print(" [%d]",debug_idx());
1751 dump_orig(st);
1752 #endif
1753 st->cr();
1754 C->_in_dump_cnt--;
1755 return; // don't process dead nodes
1756 }
1757
1758 if (C->clone_map().value(_idx) != 0) {
1759 C->clone_map().dump(_idx);
1760 }
1761 // Dump node-specific info
1762 dump_spec(st);
1763 #ifdef ASSERT
1764 // Dump the non-reset _debug_idx
1765 if (Verbose && WizardMode) {
1766 st->print(" [%d]",debug_idx());
1767 }
1768 #endif
1769
1770 const Type *t = bottom_type();
1771
1772 if (t != NULL && (t->isa_instptr() || t->isa_klassptr())) {
1773 const TypeInstPtr *toop = t->isa_instptr();
1774 const TypeKlassPtr *tkls = t->isa_klassptr();
1775 ciKlass* klass = toop ? toop->klass() : (tkls ? tkls->klass() : NULL );
1776 if (klass && klass->is_loaded() && klass->is_interface()) {
1777 st->print(" Interface:");
1778 } else if (toop) {
1779 st->print(" Oop:");
1780 } else if (tkls) {
1781 st->print(" Klass:");
1782 }
1783 t->dump_on(st);
1784 } else if (t == Type::MEMORY) {
1785 st->print(" Memory:");
1786 MemNode::dump_adr_type(this, adr_type(), st);
1787 } else if (Verbose || WizardMode) {
1788 st->print(" Type:");
1789 if (t) {
1790 t->dump_on(st);
1791 } else {
1792 st->print("no type");
1793 }
1794 } else if (t->isa_vect() && this->is_MachSpillCopy()) {
1795 // Dump MachSpillcopy vector type.
1796 t->dump_on(st);
1797 }
1798 if (is_new) {
1799 DEBUG_ONLY(dump_orig(st));
1800 Node_Notes* nn = C->node_notes_at(_idx);
1801 if (nn != NULL && !nn->is_clear()) {
1802 if (nn->jvms() != NULL) {
1803 st->print(" !jvms:");
1804 nn->jvms()->dump_spec(st);
1805 }
1806 }
1807 }
1808 if (suffix) st->print("%s", suffix);
1809 C->_in_dump_cnt--;
1810 }
1811
1812 //------------------------------dump_req--------------------------------------
dump_req(outputStream * st) const1813 void Node::dump_req(outputStream *st) const {
1814 // Dump the required input edges
1815 for (uint i = 0; i < req(); i++) { // For all required inputs
1816 Node* d = in(i);
1817 if (d == NULL) {
1818 st->print("_ ");
1819 } else if (NotANode(d)) {
1820 st->print("NotANode "); // uninitialized, sentinel, garbage, etc.
1821 } else {
1822 st->print("%c%d ", Compile::current()->node_arena()->contains(d) ? ' ' : 'o', d->_idx);
1823 }
1824 }
1825 }
1826
1827
1828 //------------------------------dump_prec-------------------------------------
dump_prec(outputStream * st) const1829 void Node::dump_prec(outputStream *st) const {
1830 // Dump the precedence edges
1831 int any_prec = 0;
1832 for (uint i = req(); i < len(); i++) { // For all precedence inputs
1833 Node* p = in(i);
1834 if (p != NULL) {
1835 if (!any_prec++) st->print(" |");
1836 if (NotANode(p)) { st->print("NotANode "); continue; }
1837 st->print("%c%d ", Compile::current()->node_arena()->contains(in(i)) ? ' ' : 'o', in(i)->_idx);
1838 }
1839 }
1840 }
1841
1842 //------------------------------dump_out--------------------------------------
dump_out(outputStream * st) const1843 void Node::dump_out(outputStream *st) const {
1844 // Delimit the output edges
1845 st->print(" [[");
1846 // Dump the output edges
1847 for (uint i = 0; i < _outcnt; i++) { // For all outputs
1848 Node* u = _out[i];
1849 if (u == NULL) {
1850 st->print("_ ");
1851 } else if (NotANode(u)) {
1852 st->print("NotANode ");
1853 } else {
1854 st->print("%c%d ", Compile::current()->node_arena()->contains(u) ? ' ' : 'o', u->_idx);
1855 }
1856 }
1857 st->print("]] ");
1858 }
1859
1860 //----------------------------collect_nodes_i----------------------------------
1861 // Collects nodes from an Ideal graph, starting from a given start node and
1862 // moving in a given direction until a certain depth (distance from the start
1863 // node) is reached. Duplicates are ignored.
1864 // Arguments:
1865 // queue: the nodes are collected into this array.
1866 // start: the node at which to start collecting.
1867 // direction: if this is a positive number, collect input nodes; if it is
1868 // a negative number, collect output nodes.
1869 // depth: collect nodes up to this distance from the start node.
1870 // include_start: whether to include the start node in the result collection.
1871 // only_ctrl: whether to regard control edges only during traversal.
1872 // only_data: whether to regard data edges only during traversal.
collect_nodes_i(GrowableArray<Node * > * queue,const Node * start,int direction,uint depth,bool include_start,bool only_ctrl,bool only_data)1873 static void collect_nodes_i(GrowableArray<Node*>* queue, const Node* start, int direction, uint depth, bool include_start, bool only_ctrl, bool only_data) {
1874 bool indent = depth <= PrintIdealIndentThreshold;
1875 Node* s = (Node*) start; // remove const
1876 queue->append(s);
1877 int begin = 0;
1878 int end = 0;
1879
1880 s->set_indent(0);
1881 for(uint i = 0; i < depth; i++) {
1882 end = queue->length();
1883 for(int j = begin; j < end; j++) {
1884 Node* tp = queue->at(j);
1885 uint limit = direction > 0 ? tp->len() : tp->outcnt();
1886 for(uint k = 0; k < limit; k++) {
1887 Node* n = direction > 0 ? tp->in(k) : tp->raw_out(k);
1888
1889 if (NotANode(n)) continue;
1890 // do not recurse through top or the root (would reach unrelated stuff)
1891 if (n->is_Root() || n->is_top()) continue;
1892 if (only_ctrl && !n->is_CFG()) continue;
1893 if (only_data && n->is_CFG()) continue;
1894 bool in_queue = queue->contains(n);
1895 if (!in_queue) {
1896 queue->append(n);
1897 n->set_indent(indent ? (i + 1) : 0);
1898 }
1899 }
1900 }
1901 begin = end;
1902 }
1903 if (!include_start) {
1904 queue->remove(s);
1905 }
1906 }
1907
1908 //------------------------------dump_nodes-------------------------------------
dump_nodes(const Node * start,int d,bool only_ctrl)1909 static void dump_nodes(const Node* start, int d, bool only_ctrl) {
1910 if (NotANode(start)) return;
1911
1912 GrowableArray <Node *> queue(Compile::current()->live_nodes());
1913 collect_nodes_i(&queue, start, d, (uint) ABS(d), true, only_ctrl, false);
1914
1915 int end = queue.length();
1916 if (d > 0) {
1917 for(int j = end-1; j >= 0; j--) {
1918 queue.at(j)->dump();
1919 }
1920 } else {
1921 for(int j = 0; j < end; j++) {
1922 queue.at(j)->dump();
1923 }
1924 }
1925 }
1926
1927 //------------------------------dump-------------------------------------------
dump(int d) const1928 void Node::dump(int d) const {
1929 dump_nodes(this, d, false);
1930 }
1931
1932 //------------------------------dump_ctrl--------------------------------------
1933 // Dump a Node's control history to depth
dump_ctrl(int d) const1934 void Node::dump_ctrl(int d) const {
1935 dump_nodes(this, d, true);
1936 }
1937
1938 //-----------------------------dump_compact------------------------------------
dump_comp() const1939 void Node::dump_comp() const {
1940 this->dump_comp("\n");
1941 }
1942
1943 //-----------------------------dump_compact------------------------------------
1944 // Dump a Node in compact representation, i.e., just print its name and index.
1945 // Nodes can specify additional specifics to print in compact representation by
1946 // implementing dump_compact_spec.
dump_comp(const char * suffix,outputStream * st) const1947 void Node::dump_comp(const char* suffix, outputStream *st) const {
1948 Compile* C = Compile::current();
1949 C->_in_dump_cnt++;
1950 st->print("%s(%d)", Name(), _idx);
1951 this->dump_compact_spec(st);
1952 if (suffix) {
1953 st->print("%s", suffix);
1954 }
1955 C->_in_dump_cnt--;
1956 }
1957
1958 //----------------------------dump_related-------------------------------------
1959 // Dump a Node's related nodes - the notion of "related" depends on the Node at
1960 // hand and is determined by the implementation of the virtual method rel.
dump_related() const1961 void Node::dump_related() const {
1962 Compile* C = Compile::current();
1963 GrowableArray <Node *> in_rel(C->unique());
1964 GrowableArray <Node *> out_rel(C->unique());
1965 this->related(&in_rel, &out_rel, false);
1966 for (int i = in_rel.length() - 1; i >= 0; i--) {
1967 in_rel.at(i)->dump();
1968 }
1969 this->dump("\n", true);
1970 for (int i = 0; i < out_rel.length(); i++) {
1971 out_rel.at(i)->dump();
1972 }
1973 }
1974
1975 //----------------------------dump_related-------------------------------------
1976 // Dump a Node's related nodes up to a given depth (distance from the start
1977 // node).
1978 // Arguments:
1979 // d_in: depth for input nodes.
1980 // d_out: depth for output nodes (note: this also is a positive number).
dump_related(uint d_in,uint d_out) const1981 void Node::dump_related(uint d_in, uint d_out) const {
1982 Compile* C = Compile::current();
1983 GrowableArray <Node *> in_rel(C->unique());
1984 GrowableArray <Node *> out_rel(C->unique());
1985
1986 // call collect_nodes_i directly
1987 collect_nodes_i(&in_rel, this, 1, d_in, false, false, false);
1988 collect_nodes_i(&out_rel, this, -1, d_out, false, false, false);
1989
1990 for (int i = in_rel.length() - 1; i >= 0; i--) {
1991 in_rel.at(i)->dump();
1992 }
1993 this->dump("\n", true);
1994 for (int i = 0; i < out_rel.length(); i++) {
1995 out_rel.at(i)->dump();
1996 }
1997 }
1998
1999 //------------------------dump_related_compact---------------------------------
2000 // Dump a Node's related nodes in compact representation. The notion of
2001 // "related" depends on the Node at hand and is determined by the implementation
2002 // of the virtual method rel.
dump_related_compact() const2003 void Node::dump_related_compact() const {
2004 Compile* C = Compile::current();
2005 GrowableArray <Node *> in_rel(C->unique());
2006 GrowableArray <Node *> out_rel(C->unique());
2007 this->related(&in_rel, &out_rel, true);
2008 int n_in = in_rel.length();
2009 int n_out = out_rel.length();
2010
2011 this->dump_comp(n_in == 0 ? "\n" : " ");
2012 for (int i = 0; i < n_in; i++) {
2013 in_rel.at(i)->dump_comp(i == n_in - 1 ? "\n" : " ");
2014 }
2015 for (int i = 0; i < n_out; i++) {
2016 out_rel.at(i)->dump_comp(i == n_out - 1 ? "\n" : " ");
2017 }
2018 }
2019
2020 //------------------------------related----------------------------------------
2021 // Collect a Node's related nodes. The default behaviour just collects the
2022 // inputs and outputs at depth 1, including both control and data flow edges,
2023 // regardless of whether the presentation is compact or not. For data nodes,
2024 // the default is to collect all data inputs (till level 1 if compact), and
2025 // outputs till level 1.
related(GrowableArray<Node * > * in_rel,GrowableArray<Node * > * out_rel,bool compact) const2026 void Node::related(GrowableArray<Node*> *in_rel, GrowableArray<Node*> *out_rel, bool compact) const {
2027 if (this->is_CFG()) {
2028 collect_nodes_i(in_rel, this, 1, 1, false, false, false);
2029 collect_nodes_i(out_rel, this, -1, 1, false, false, false);
2030 } else {
2031 if (compact) {
2032 this->collect_nodes(in_rel, 1, false, true);
2033 } else {
2034 this->collect_nodes_in_all_data(in_rel, false);
2035 }
2036 this->collect_nodes(out_rel, -1, false, false);
2037 }
2038 }
2039
2040 //---------------------------collect_nodes-------------------------------------
2041 // An entry point to the low-level node collection facility, to start from a
2042 // given node in the graph. The start node is by default not included in the
2043 // result.
2044 // Arguments:
2045 // ns: collect the nodes into this data structure.
2046 // d: the depth (distance from start node) to which nodes should be
2047 // collected. A value >0 indicates input nodes, a value <0, output
2048 // nodes.
2049 // ctrl: include only control nodes.
2050 // data: include only data nodes.
collect_nodes(GrowableArray<Node * > * ns,int d,bool ctrl,bool data) const2051 void Node::collect_nodes(GrowableArray<Node*> *ns, int d, bool ctrl, bool data) const {
2052 if (ctrl && data) {
2053 // ignore nonsensical combination
2054 return;
2055 }
2056 collect_nodes_i(ns, this, d, (uint) ABS(d), false, ctrl, data);
2057 }
2058
2059 //--------------------------collect_nodes_in-----------------------------------
collect_nodes_in(Node * start,GrowableArray<Node * > * ns,bool primary_is_data,bool collect_secondary)2060 static void collect_nodes_in(Node* start, GrowableArray<Node*> *ns, bool primary_is_data, bool collect_secondary) {
2061 // The maximum depth is determined using a BFS that visits all primary (data
2062 // or control) inputs and increments the depth at each level.
2063 uint d_in = 0;
2064 GrowableArray<Node*> nodes(Compile::current()->unique());
2065 nodes.push(start);
2066 int nodes_at_current_level = 1;
2067 int n_idx = 0;
2068 while (nodes_at_current_level > 0) {
2069 // Add all primary inputs reachable from the current level to the list, and
2070 // increase the depth if there were any.
2071 int nodes_at_next_level = 0;
2072 bool nodes_added = false;
2073 while (nodes_at_current_level > 0) {
2074 nodes_at_current_level--;
2075 Node* current = nodes.at(n_idx++);
2076 for (uint i = 0; i < current->len(); i++) {
2077 Node* n = current->in(i);
2078 if (NotANode(n)) {
2079 continue;
2080 }
2081 if ((primary_is_data && n->is_CFG()) || (!primary_is_data && !n->is_CFG())) {
2082 continue;
2083 }
2084 if (!nodes.contains(n)) {
2085 nodes.push(n);
2086 nodes_added = true;
2087 nodes_at_next_level++;
2088 }
2089 }
2090 }
2091 if (nodes_added) {
2092 d_in++;
2093 }
2094 nodes_at_current_level = nodes_at_next_level;
2095 }
2096 start->collect_nodes(ns, d_in, !primary_is_data, primary_is_data);
2097 if (collect_secondary) {
2098 // Now, iterate over the secondary nodes in ns and add the respective
2099 // boundary reachable from them.
2100 GrowableArray<Node*> sns(Compile::current()->unique());
2101 for (GrowableArrayIterator<Node*> it = ns->begin(); it != ns->end(); ++it) {
2102 Node* n = *it;
2103 n->collect_nodes(&sns, 1, primary_is_data, !primary_is_data);
2104 for (GrowableArrayIterator<Node*> d = sns.begin(); d != sns.end(); ++d) {
2105 ns->append_if_missing(*d);
2106 }
2107 sns.clear();
2108 }
2109 }
2110 }
2111
2112 //---------------------collect_nodes_in_all_data-------------------------------
2113 // Collect the entire data input graph. Include the control boundary if
2114 // requested.
2115 // Arguments:
2116 // ns: collect the nodes into this data structure.
2117 // ctrl: if true, include the control boundary.
collect_nodes_in_all_data(GrowableArray<Node * > * ns,bool ctrl) const2118 void Node::collect_nodes_in_all_data(GrowableArray<Node*> *ns, bool ctrl) const {
2119 collect_nodes_in((Node*) this, ns, true, ctrl);
2120 }
2121
2122 //--------------------------collect_nodes_in_all_ctrl--------------------------
2123 // Collect the entire control input graph. Include the data boundary if
2124 // requested.
2125 // ns: collect the nodes into this data structure.
2126 // data: if true, include the control boundary.
collect_nodes_in_all_ctrl(GrowableArray<Node * > * ns,bool data) const2127 void Node::collect_nodes_in_all_ctrl(GrowableArray<Node*> *ns, bool data) const {
2128 collect_nodes_in((Node*) this, ns, false, data);
2129 }
2130
2131 //------------------collect_nodes_out_all_ctrl_boundary------------------------
2132 // Collect the entire output graph until hitting control node boundaries, and
2133 // include those.
collect_nodes_out_all_ctrl_boundary(GrowableArray<Node * > * ns) const2134 void Node::collect_nodes_out_all_ctrl_boundary(GrowableArray<Node*> *ns) const {
2135 // Perform a BFS and stop at control nodes.
2136 GrowableArray<Node*> nodes(Compile::current()->unique());
2137 nodes.push((Node*) this);
2138 while (nodes.length() > 0) {
2139 Node* current = nodes.pop();
2140 if (NotANode(current)) {
2141 continue;
2142 }
2143 ns->append_if_missing(current);
2144 if (!current->is_CFG()) {
2145 for (DUIterator i = current->outs(); current->has_out(i); i++) {
2146 nodes.push(current->out(i));
2147 }
2148 }
2149 }
2150 ns->remove((Node*) this);
2151 }
2152
2153 // VERIFICATION CODE
2154 // For each input edge to a node (ie - for each Use-Def edge), verify that
2155 // there is a corresponding Def-Use edge.
2156 //------------------------------verify_edges-----------------------------------
verify_edges(Unique_Node_List & visited)2157 void Node::verify_edges(Unique_Node_List &visited) {
2158 uint i, j, idx;
2159 int cnt;
2160 Node *n;
2161
2162 // Recursive termination test
2163 if (visited.member(this)) return;
2164 visited.push(this);
2165
2166 // Walk over all input edges, checking for correspondence
2167 for( i = 0; i < len(); i++ ) {
2168 n = in(i);
2169 if (n != NULL && !n->is_top()) {
2170 // Count instances of (Node *)this
2171 cnt = 0;
2172 for (idx = 0; idx < n->_outcnt; idx++ ) {
2173 if (n->_out[idx] == (Node *)this) cnt++;
2174 }
2175 assert( cnt > 0,"Failed to find Def-Use edge." );
2176 // Check for duplicate edges
2177 // walk the input array downcounting the input edges to n
2178 for( j = 0; j < len(); j++ ) {
2179 if( in(j) == n ) cnt--;
2180 }
2181 assert( cnt == 0,"Mismatched edge count.");
2182 } else if (n == NULL) {
2183 assert(i >= req() || i == 0 || is_Region() || is_Phi() || is_ArrayCopy() || (is_Unlock() && i == req()-1)
2184 || (is_MemBar() && i == 5), // the precedence edge to a membar can be removed during macro node expansion
2185 "only region, phi, arraycopy, unlock or membar nodes have null data edges");
2186 } else {
2187 assert(n->is_top(), "sanity");
2188 // Nothing to check.
2189 }
2190 }
2191 // Recursive walk over all input edges
2192 for( i = 0; i < len(); i++ ) {
2193 n = in(i);
2194 if( n != NULL )
2195 in(i)->verify_edges(visited);
2196 }
2197 }
2198
2199 // Verify all nodes if verify_depth is negative
verify(Node * n,int verify_depth)2200 void Node::verify(Node* n, int verify_depth) {
2201 assert(verify_depth != 0, "depth should not be 0");
2202 ResourceMark rm;
2203 VectorSet old_space;
2204 VectorSet new_space;
2205 Node_List worklist;
2206 worklist.push(n);
2207 Compile* C = Compile::current();
2208 uint last_index_on_current_depth = 0;
2209 verify_depth--; // Visiting the first node on depth 1
2210 // Only add nodes to worklist if verify_depth is negative (visit all nodes) or greater than 0
2211 bool add_to_worklist = verify_depth != 0;
2212
2213
2214 for (uint list_index = 0; list_index < worklist.size(); list_index++) {
2215 n = worklist[list_index];
2216
2217 if (n->is_Con() && n->bottom_type() == Type::TOP) {
2218 if (C->cached_top_node() == NULL) {
2219 C->set_cached_top_node((Node*)n);
2220 }
2221 assert(C->cached_top_node() == n, "TOP node must be unique");
2222 }
2223
2224 for (uint i = 0; i < n->len(); i++) {
2225 Node* x = n->in(i);
2226 if (!x || x->is_top()) {
2227 continue;
2228 }
2229
2230 // Verify my input has a def-use edge to me
2231 // Count use-def edges from n to x
2232 int cnt = 0;
2233 for (uint j = 0; j < n->len(); j++) {
2234 if (n->in(j) == x) {
2235 cnt++;
2236 }
2237 }
2238
2239 // Count def-use edges from x to n
2240 uint max = x->_outcnt;
2241 for (uint k = 0; k < max; k++) {
2242 if (x->_out[k] == n) {
2243 cnt--;
2244 }
2245 }
2246 assert(cnt == 0, "mismatched def-use edge counts");
2247
2248 // Contained in new_space or old_space?
2249 VectorSet* v = C->node_arena()->contains(x) ? &new_space : &old_space;
2250 // Check for visited in the proper space. Numberings are not unique
2251 // across spaces so we need a separate VectorSet for each space.
2252 if (add_to_worklist && !v->test_set(x->_idx)) {
2253 worklist.push(x);
2254 }
2255 }
2256
2257 if (verify_depth > 0 && list_index == last_index_on_current_depth) {
2258 // All nodes on this depth were processed and its inputs are on the worklist. Decrement verify_depth and
2259 // store the current last list index which is the last node in the list with the new depth. All nodes
2260 // added afterwards will have a new depth again. Stop adding new nodes if depth limit is reached (=0).
2261 verify_depth--;
2262 if (verify_depth == 0) {
2263 add_to_worklist = false;
2264 }
2265 last_index_on_current_depth = worklist.size() - 1;
2266 }
2267 }
2268 }
2269 #endif // not PRODUCT
2270
2271 //------------------------------Registers--------------------------------------
2272 // Do we Match on this edge index or not? Generally false for Control
2273 // and true for everything else. Weird for calls & returns.
match_edge(uint idx) const2274 uint Node::match_edge(uint idx) const {
2275 return idx; // True for other than index 0 (control)
2276 }
2277
2278 // Register classes are defined for specific machines
out_RegMask() const2279 const RegMask &Node::out_RegMask() const {
2280 ShouldNotCallThis();
2281 return RegMask::Empty;
2282 }
2283
in_RegMask(uint) const2284 const RegMask &Node::in_RegMask(uint) const {
2285 ShouldNotCallThis();
2286 return RegMask::Empty;
2287 }
2288
grow(uint i)2289 void Node_Array::grow(uint i) {
2290 assert(_max > 0, "invariant");
2291 uint old = _max;
2292 _max = next_power_of_2(i);
2293 _nodes = (Node**)_a->Arealloc( _nodes, old*sizeof(Node*),_max*sizeof(Node*));
2294 Copy::zero_to_bytes( &_nodes[old], (_max-old)*sizeof(Node*) );
2295 }
2296
insert(uint i,Node * n)2297 void Node_Array::insert(uint i, Node* n) {
2298 if (_nodes[_max - 1]) {
2299 grow(_max);
2300 }
2301 Copy::conjoint_words_to_higher((HeapWord*)&_nodes[i], (HeapWord*)&_nodes[i + 1], ((_max - i - 1) * sizeof(Node*)));
2302 _nodes[i] = n;
2303 }
2304
remove(uint i)2305 void Node_Array::remove(uint i) {
2306 Copy::conjoint_words_to_lower((HeapWord*)&_nodes[i + 1], (HeapWord*)&_nodes[i], ((_max - i - 1) * sizeof(Node*)));
2307 _nodes[_max - 1] = NULL;
2308 }
2309
dump() const2310 void Node_Array::dump() const {
2311 #ifndef PRODUCT
2312 for (uint i = 0; i < _max; i++) {
2313 Node* nn = _nodes[i];
2314 if (nn != NULL) {
2315 tty->print("%5d--> ",i); nn->dump();
2316 }
2317 }
2318 #endif
2319 }
2320
2321 //--------------------------is_iteratively_computed------------------------------
2322 // Operation appears to be iteratively computed (such as an induction variable)
2323 // It is possible for this operation to return false for a loop-varying
2324 // value, if it appears (by local graph inspection) to be computed by a simple conditional.
is_iteratively_computed()2325 bool Node::is_iteratively_computed() {
2326 if (ideal_reg()) { // does operation have a result register?
2327 for (uint i = 1; i < req(); i++) {
2328 Node* n = in(i);
2329 if (n != NULL && n->is_Phi()) {
2330 for (uint j = 1; j < n->req(); j++) {
2331 if (n->in(j) == this) {
2332 return true;
2333 }
2334 }
2335 }
2336 }
2337 }
2338 return false;
2339 }
2340
2341 //--------------------------find_similar------------------------------
2342 // Return a node with opcode "opc" and same inputs as "this" if one can
2343 // be found; Otherwise return NULL;
find_similar(int opc)2344 Node* Node::find_similar(int opc) {
2345 if (req() >= 2) {
2346 Node* def = in(1);
2347 if (def && def->outcnt() >= 2) {
2348 for (DUIterator_Fast dmax, i = def->fast_outs(dmax); i < dmax; i++) {
2349 Node* use = def->fast_out(i);
2350 if (use != this &&
2351 use->Opcode() == opc &&
2352 use->req() == req()) {
2353 uint j;
2354 for (j = 0; j < use->req(); j++) {
2355 if (use->in(j) != in(j)) {
2356 break;
2357 }
2358 }
2359 if (j == use->req()) {
2360 return use;
2361 }
2362 }
2363 }
2364 }
2365 }
2366 return NULL;
2367 }
2368
2369
2370 //--------------------------unique_ctrl_out------------------------------
2371 // Return the unique control out if only one. Null if none or more than one.
unique_ctrl_out() const2372 Node* Node::unique_ctrl_out() const {
2373 Node* found = NULL;
2374 for (uint i = 0; i < outcnt(); i++) {
2375 Node* use = raw_out(i);
2376 if (use->is_CFG() && use != this) {
2377 if (found != NULL) {
2378 return NULL;
2379 }
2380 found = use;
2381 }
2382 }
2383 return found;
2384 }
2385
ensure_control_or_add_prec(Node * c)2386 void Node::ensure_control_or_add_prec(Node* c) {
2387 if (in(0) == NULL) {
2388 set_req(0, c);
2389 } else if (in(0) != c) {
2390 add_prec(c);
2391 }
2392 }
2393
is_dead_loop_safe() const2394 bool Node::is_dead_loop_safe() const {
2395 if (is_Phi()) {
2396 return true;
2397 }
2398 if (is_Proj() && in(0) == NULL) {
2399 return true;
2400 }
2401 if ((_flags & (Flag_is_dead_loop_safe | Flag_is_Con)) != 0) {
2402 if (!is_Proj()) {
2403 return true;
2404 }
2405 if (in(0)->is_Allocate()) {
2406 return false;
2407 }
2408 // MemNode::can_see_stored_value() peeks through the boxing call
2409 if (in(0)->is_CallStaticJava() && in(0)->as_CallStaticJava()->is_boxing_method()) {
2410 return false;
2411 }
2412 return true;
2413 }
2414 return false;
2415 }
2416
2417 //=============================================================================
2418 //------------------------------yank-------------------------------------------
2419 // Find and remove
yank(Node * n)2420 void Node_List::yank( Node *n ) {
2421 uint i;
2422 for (i = 0; i < _cnt; i++) {
2423 if (_nodes[i] == n) {
2424 break;
2425 }
2426 }
2427
2428 if (i < _cnt) {
2429 _nodes[i] = _nodes[--_cnt];
2430 }
2431 }
2432
2433 //------------------------------dump-------------------------------------------
dump() const2434 void Node_List::dump() const {
2435 #ifndef PRODUCT
2436 for (uint i = 0; i < _cnt; i++) {
2437 if (_nodes[i]) {
2438 tty->print("%5d--> ", i);
2439 _nodes[i]->dump();
2440 }
2441 }
2442 #endif
2443 }
2444
dump_simple() const2445 void Node_List::dump_simple() const {
2446 #ifndef PRODUCT
2447 for (uint i = 0; i < _cnt; i++) {
2448 if( _nodes[i] ) {
2449 tty->print(" %d", _nodes[i]->_idx);
2450 } else {
2451 tty->print(" NULL");
2452 }
2453 }
2454 #endif
2455 }
2456
2457 //=============================================================================
2458 //------------------------------remove-----------------------------------------
remove(Node * n)2459 void Unique_Node_List::remove(Node* n) {
2460 if (_in_worklist.test(n->_idx)) {
2461 for (uint i = 0; i < size(); i++) {
2462 if (_nodes[i] == n) {
2463 map(i, Node_List::pop());
2464 _in_worklist.remove(n->_idx);
2465 return;
2466 }
2467 }
2468 ShouldNotReachHere();
2469 }
2470 }
2471
2472 //-----------------------remove_useless_nodes----------------------------------
2473 // Remove useless nodes from worklist
remove_useless_nodes(VectorSet & useful)2474 void Unique_Node_List::remove_useless_nodes(VectorSet &useful) {
2475 for (uint i = 0; i < size(); ++i) {
2476 Node *n = at(i);
2477 assert( n != NULL, "Did not expect null entries in worklist");
2478 if (!useful.test(n->_idx)) {
2479 _in_worklist.remove(n->_idx);
2480 map(i, Node_List::pop());
2481 --i; // Visit popped node
2482 // If it was last entry, loop terminates since size() was also reduced
2483 }
2484 }
2485 }
2486
2487 //=============================================================================
grow()2488 void Node_Stack::grow() {
2489 size_t old_top = pointer_delta(_inode_top,_inodes,sizeof(INode)); // save _top
2490 size_t old_max = pointer_delta(_inode_max,_inodes,sizeof(INode));
2491 size_t max = old_max << 1; // max * 2
2492 _inodes = REALLOC_ARENA_ARRAY(_a, INode, _inodes, old_max, max);
2493 _inode_max = _inodes + max;
2494 _inode_top = _inodes + old_top; // restore _top
2495 }
2496
2497 // Node_Stack is used to map nodes.
find(uint idx) const2498 Node* Node_Stack::find(uint idx) const {
2499 uint sz = size();
2500 for (uint i = 0; i < sz; i++) {
2501 if (idx == index_at(i)) {
2502 return node_at(i);
2503 }
2504 }
2505 return NULL;
2506 }
2507
2508 //=============================================================================
size_of() const2509 uint TypeNode::size_of() const { return sizeof(*this); }
2510 #ifndef PRODUCT
dump_spec(outputStream * st) const2511 void TypeNode::dump_spec(outputStream *st) const {
2512 if (!Verbose && !WizardMode) {
2513 // standard dump does this in Verbose and WizardMode
2514 st->print(" #"); _type->dump_on(st);
2515 }
2516 }
2517
dump_compact_spec(outputStream * st) const2518 void TypeNode::dump_compact_spec(outputStream *st) const {
2519 st->print("#");
2520 _type->dump_on(st);
2521 }
2522 #endif
hash() const2523 uint TypeNode::hash() const {
2524 return Node::hash() + _type->hash();
2525 }
cmp(const Node & n) const2526 bool TypeNode::cmp(const Node& n) const {
2527 return !Type::cmp(_type, ((TypeNode&)n)._type);
2528 }
bottom_type() const2529 const Type* TypeNode::bottom_type() const { return _type; }
Value(PhaseGVN * phase) const2530 const Type* TypeNode::Value(PhaseGVN* phase) const { return _type; }
2531
2532 //------------------------------ideal_reg--------------------------------------
ideal_reg() const2533 uint TypeNode::ideal_reg() const {
2534 return _type->ideal_reg();
2535 }
2536