15 * version 2 for more details (a copy is included in the LICENSE file that
16 * accompanied this code).
17 *
18 * You should have received a copy of the GNU General Public License version
19 * 2 along with this work; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
21 *
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
24 * have any questions.
25 *
26 */
27
28 // Portions of code courtesy of Clifford Click
29
30 // Optimization - Graph Style
31
32 #include "incls/_precompiled.incl"
33 #include "incls/_memnode.cpp.incl"
34
35 //=============================================================================
36 uint MemNode::size_of() const { return sizeof(*this); }
37
38 const TypePtr *MemNode::adr_type() const {
39 Node* adr = in(Address);
40 const TypePtr* cross_check = NULL;
41 DEBUG_ONLY(cross_check = _adr_type);
42 return calculate_adr_type(adr->bottom_type(), cross_check);
43 }
44
45 #ifndef PRODUCT
46 void MemNode::dump_spec(outputStream *st) const {
47 if (in(Address) == NULL) return; // node is dead
48 #ifndef ASSERT
49 // fake the missing field
50 const TypePtr* _adr_type = NULL;
51 if (in(Address) != NULL)
52 _adr_type = in(Address)->bottom_type()->isa_ptr();
53 #endif
54 dump_adr_type(this, _adr_type, st);
73 st->print(", idx=Bot;");
74 else if (atp->index() == Compile::AliasIdxTop)
75 st->print(", idx=Top;");
76 else if (atp->index() == Compile::AliasIdxRaw)
77 st->print(", idx=Raw;");
78 else {
79 ciField* field = atp->field();
80 if (field) {
81 st->print(", name=");
82 field->print_name_on(st);
83 }
84 st->print(", idx=%d;", atp->index());
85 }
86 }
87 }
88
89 extern void print_alias_types();
90
91 #endif
92
93 //--------------------------Ideal_common---------------------------------------
94 // Look for degenerate control and memory inputs. Bypass MergeMem inputs.
95 // Unhook non-raw memories from complete (macro-expanded) initializations.
96 Node *MemNode::Ideal_common(PhaseGVN *phase, bool can_reshape) {
97 // If our control input is a dead region, kill all below the region
98 Node *ctl = in(MemNode::Control);
99 if (ctl && remove_dead_region(phase, can_reshape))
100 return this;
101
102 // Ignore if memory is dead, or self-loop
103 Node *mem = in(MemNode::Memory);
104 if( phase->type( mem ) == Type::TOP ) return NodeSentinel; // caller will return NULL
105 assert( mem != this, "dead loop in MemNode::Ideal" );
106
107 Node *address = in(MemNode::Address);
108 const Type *t_adr = phase->type( address );
109 if( t_adr == Type::TOP ) return NodeSentinel; // caller will return NULL
110
111 // Avoid independent memory operations
112 Node* old_mem = mem;
113
114 if (mem->is_Proj() && mem->in(0)->is_Initialize()) {
115 InitializeNode* init = mem->in(0)->as_Initialize();
116 if (init->is_complete()) { // i.e., after macro expansion
117 const TypePtr* tp = t_adr->is_ptr();
118 uint alias_idx = phase->C->get_alias_index(tp);
119 // Free this slice from the init. It was hooked, temporarily,
120 // by GraphKit::set_output_for_allocation.
121 if (alias_idx > Compile::AliasIdxRaw) {
122 mem = init->memory(alias_idx);
123 // ...but not with the raw-pointer slice.
124 }
125 }
126 }
127
128 if (mem->is_MergeMem()) {
129 MergeMemNode* mmem = mem->as_MergeMem();
130 const TypePtr *tp = t_adr->is_ptr();
131 uint alias_idx = phase->C->get_alias_index(tp);
132 #ifdef ASSERT
133 {
134 // Check that current type is consistent with the alias index used during graph construction
135 assert(alias_idx >= Compile::AliasIdxRaw, "must not be a bad alias_idx");
136 const TypePtr *adr_t = adr_type();
137 bool consistent = adr_t == NULL || adr_t->empty() || phase->C->must_alias(adr_t, alias_idx );
138 // Sometimes dead array references collapse to a[-1], a[-2], or a[-3]
139 if( !consistent && adr_t != NULL && !adr_t->empty() &&
140 tp->isa_aryptr() && tp->offset() == Type::OffsetBot &&
141 adr_t->isa_aryptr() && adr_t->offset() != Type::OffsetBot &&
142 ( adr_t->offset() == arrayOopDesc::length_offset_in_bytes() ||
143 adr_t->offset() == oopDesc::klass_offset_in_bytes() ||
144 adr_t->offset() == oopDesc::mark_offset_in_bytes() ) ) {
145 // don't assert if it is dead code.
146 consistent = true;
147 }
148 if( !consistent ) {
149 tty->print("alias_idx==%d, adr_type()==", alias_idx); if( adr_t == NULL ) { tty->print("NULL"); } else { adr_t->dump(); }
150 tty->cr();
151 print_alias_types();
152 assert(consistent, "adr_type must match alias idx");
153 }
154 }
155 #endif
156 // TypeInstPtr::NOTNULL+any is an OOP with unknown offset - generally
157 // means an array I have not precisely typed yet. Do not do any
158 // alias stuff with it any time soon.
159 const TypeInstPtr *tinst = tp->isa_instptr();
160 if( tp->base() != Type::AnyPtr &&
161 !(tinst &&
162 tinst->klass()->is_java_lang_Object() &&
163 tinst->offset() == Type::OffsetBot) ) {
164 // compress paths and change unreachable cycles to TOP
165 // If not, we can update the input infinitely along a MergeMem cycle
166 // Equivalent code in PhiNode::Ideal
167 Node* m = phase->transform(mmem);
168 // If tranformed to a MergeMem, get the desired slice
169 // Otherwise the returned node represents memory for every slice
170 mem = (m->is_MergeMem())? m->as_MergeMem()->memory_at(alias_idx) : m;
171 // Update input if it is progress over what we have now
172 }
173 }
174
175 if (mem != old_mem) {
176 set_req(MemNode::Memory, mem);
177 return this;
178 }
179
180 // let the subclass continue analyzing...
181 return NULL;
182 }
183
184 // Helper function for proving some simple control dominations.
185 // Attempt to prove that control input 'dom' dominates (or equals) 'sub'.
186 // Already assumes that 'dom' is available at 'sub', and that 'sub'
187 // is not a constant (dominated by the method's StartNode).
188 // Used by MemNode::find_previous_store to prove that the
189 // control input of a memory operation predates (dominates)
190 // an allocation it wants to look past.
191 bool MemNode::detect_dominating_control(Node* dom, Node* sub) {
192 if (dom == NULL) return false;
193 if (dom->is_Proj()) dom = dom->in(0);
194 if (dom->is_Start()) return true; // anything inside the method
195 if (dom->is_Root()) return true; // dom 'controls' a constant
196 int cnt = 20; // detect cycle or too much effort
197 while (sub != NULL) { // walk 'sub' up the chain to 'dom'
198 if (--cnt < 0) return false; // in a cycle or too complex
199 if (sub == dom) return true;
200 if (sub->is_Start()) return false;
201 if (sub->is_Root()) return false;
202 Node* up = sub->in(0);
203 if (sub == up && sub->is_Region()) {
204 for (uint i = 1; i < sub->req(); i++) {
205 Node* in = sub->in(i);
206 if (in != NULL && !in->is_top() && in != sub) {
207 up = in; break; // take any path on the way up to 'dom'
208 }
209 }
210 }
211 if (sub == up) return false; // some kind of tight cycle
212 sub = up;
213 }
214 return false;
215 }
216
217 //---------------------detect_ptr_independence---------------------------------
218 // Used by MemNode::find_previous_store to prove that two base
219 // pointers are never equal.
220 // The pointers are accompanied by their associated allocations,
221 // if any, which have been previously discovered by the caller.
222 bool MemNode::detect_ptr_independence(Node* p1, AllocateNode* a1,
223 Node* p2, AllocateNode* a2,
224 PhaseTransform* phase) {
225 // Attempt to prove that these two pointers cannot be aliased.
226 // They may both manifestly be allocations, and they should differ.
227 // Or, if they are not both allocations, they can be distinct constants.
228 // Otherwise, one is an allocation and the other a pre-existing value.
229 if (a1 == NULL && a2 == NULL) { // neither an allocation
230 return (p1 != p2) && p1->is_Con() && p2->is_Con();
231 } else if (a1 != NULL && a2 != NULL) { // both allocations
232 return (a1 != a2);
233 } else if (a1 != NULL) { // one allocation a1
234 // (Note: p2->is_Con implies p2->in(0)->is_Root, which dominates.)
235 return detect_dominating_control(p2->in(0), a1->in(0));
236 } else { //(a2 != NULL) // one allocation a2
237 return detect_dominating_control(p1->in(0), a2->in(0));
238 }
239 return false;
240 }
241
242
243 // The logic for reordering loads and stores uses four steps:
244 // (a) Walk carefully past stores and initializations which we
245 // can prove are independent of this load.
246 // (b) Observe that the next memory state makes an exact match
247 // with self (load or store), and locate the relevant store.
248 // (c) Ensure that, if we were to wire self directly to the store,
249 // the optimizer would fold it up somehow.
250 // (d) Do the rewiring, and return, depending on some other part of
251 // the optimizer to fold up the load.
252 // This routine handles steps (a) and (b). Steps (c) and (d) are
253 // specific to loads and stores, so they are handled by the callers.
254 // (Currently, only LoadNode::Ideal has steps (c), (d). More later.)
255 //
256 Node* MemNode::find_previous_store(PhaseTransform* phase) {
257 Node* ctrl = in(MemNode::Control);
258 Node* adr = in(MemNode::Address);
259 intptr_t offset = 0;
260 Node* base = AddPNode::Ideal_base_and_offset(adr, phase, offset);
261 AllocateNode* alloc = AllocateNode::Ideal_allocation(base, phase);
262
263 if (offset == Type::OffsetBot)
264 return NULL; // cannot unalias unless there are precise offsets
265
266 intptr_t size_in_bytes = memory_size();
267
268 Node* mem = in(MemNode::Memory); // start searching here...
269
270 int cnt = 50; // Cycle limiter
271 for (;;) { // While we can dance past unrelated stores...
272 if (--cnt < 0) break; // Caught in cycle or a complicated dance?
273
274 if (mem->is_Store()) {
275 Node* st_adr = mem->in(MemNode::Address);
276 intptr_t st_offset = 0;
277 Node* st_base = AddPNode::Ideal_base_and_offset(st_adr, phase, st_offset);
278 if (st_base == NULL)
279 break; // inscrutable pointer
280 if (st_offset != offset && st_offset != Type::OffsetBot) {
281 const int MAX_STORE = BytesPerLong;
282 if (st_offset >= offset + size_in_bytes ||
283 st_offset <= offset - MAX_STORE ||
284 st_offset <= offset - mem->as_Store()->memory_size()) {
285 // Success: The offsets are provably independent.
301 continue; // (a) advance through independent store memory
302 }
303
304 // (b) At this point, if the bases or offsets do not agree, we lose,
305 // since we have not managed to prove 'this' and 'mem' independent.
306 if (st_base == base && st_offset == offset) {
307 return mem; // let caller handle steps (c), (d)
308 }
309
310 } else if (mem->is_Proj() && mem->in(0)->is_Initialize()) {
311 InitializeNode* st_init = mem->in(0)->as_Initialize();
312 AllocateNode* st_alloc = st_init->allocation();
313 if (st_alloc == NULL)
314 break; // something degenerated
315 bool known_identical = false;
316 bool known_independent = false;
317 if (alloc == st_alloc)
318 known_identical = true;
319 else if (alloc != NULL)
320 known_independent = true;
321 else if (ctrl != NULL &&
322 detect_dominating_control(ctrl, st_alloc->in(0)))
323 known_independent = true;
324
325 if (known_independent) {
326 // The bases are provably independent: Either they are
327 // manifestly distinct allocations, or else the control
328 // of this load dominates the store's allocation.
329 int alias_idx = phase->C->get_alias_index(adr_type());
330 if (alias_idx == Compile::AliasIdxRaw) {
331 mem = st_alloc->in(TypeFunc::Memory);
332 } else {
333 mem = st_init->memory(alias_idx);
334 }
335 continue; // (a) advance through independent store memory
336 }
337
338 // (b) at this point, if we are not looking at a store initializing
339 // the same allocation we are loading from, we lose.
340 if (known_identical) {
341 // From caller, can_see_stored_value will consult find_captured_store.
342 return mem; // let caller handle steps (c), (d)
343 }
344
345 }
346
347 // Unless there is an explicit 'continue', we must bail out here,
348 // because 'mem' is an inscrutable memory state (e.g., a call).
349 break;
350 }
351
352 return NULL; // bail out
353 }
354
355 //----------------------calculate_adr_type-------------------------------------
356 // Helper function. Notices when the given type of address hits top or bottom.
357 // Also, asserts a cross-check of the type against the expected address type.
358 const TypePtr* MemNode::calculate_adr_type(const Type* t, const TypePtr* cross_check) {
359 if (t == Type::TOP) return NULL; // does not touch memory any more?
360 #ifdef PRODUCT
361 cross_check = NULL;
362 #else
363 if (!VerifyAliases || is_error_reported() || Node::in_dump()) cross_check = NULL;
364 #endif
429 if( n->is_ConstraintCast() ){
430 worklist.push(n->in(1));
431 } else if( n->is_Phi() ) {
432 for( uint i = 1; i < n->req(); i++ ) {
433 worklist.push(n->in(i));
434 }
435 } else {
436 return false;
437 }
438 }
439 }
440
441 // Quit when the worklist is empty, and we've found no offending nodes.
442 return true;
443 }
444
445 //------------------------------Ideal_DU_postCCP-------------------------------
446 // Find any cast-away of null-ness and keep its control. Null cast-aways are
447 // going away in this pass and we need to make this memory op depend on the
448 // gating null check.
449
450 // I tried to leave the CastPP's in. This makes the graph more accurate in
451 // some sense; we get to keep around the knowledge that an oop is not-null
452 // after some test. Alas, the CastPP's interfere with GVN (some values are
453 // the regular oop, some are the CastPP of the oop, all merge at Phi's which
454 // cannot collapse, etc). This cost us 10% on SpecJVM, even when I removed
455 // some of the more trivial cases in the optimizer. Removing more useless
456 // Phi's started allowing Loads to illegally float above null checks. I gave
457 // up on this approach. CNC 10/20/2000
458 Node *MemNode::Ideal_DU_postCCP( PhaseCCP *ccp ) {
459 Node *ctr = in(MemNode::Control);
460 Node *mem = in(MemNode::Memory);
461 Node *adr = in(MemNode::Address);
462 Node *skipped_cast = NULL;
463 // Need a null check? Regular static accesses do not because they are
464 // from constant addresses. Array ops are gated by the range check (which
465 // always includes a NULL check). Just check field ops.
466 if( !ctr ) {
467 // Scan upwards for the highest location we can place this memory op.
468 while( true ) {
469 switch( adr->Opcode() ) {
470
471 case Op_AddP: // No change to NULL-ness, so peek thru AddP's
472 adr = adr->in(AddPNode::Base);
473 continue;
474
475 case Op_CastPP:
476 // If the CastPP is useless, just peek on through it.
477 if( ccp->type(adr) == ccp->type(adr->in(1)) ) {
478 // Remember the cast that we've peeked though. If we peek
479 // through more than one, then we end up remembering the highest
480 // one, that is, if in a loop, the one closest to the top.
481 skipped_cast = adr;
482 adr = adr->in(1);
483 continue;
484 }
485 // CastPP is going away in this pass! We need this memory op to be
486 // control-dependent on the test that is guarding the CastPP.
487 ccp->hash_delete(this);
488 set_req(MemNode::Control, adr->in(0));
489 ccp->hash_insert(this);
490 return this;
491
492 case Op_Phi:
493 // Attempt to float above a Phi to some dominating point.
494 if (adr->in(0) != NULL && adr->in(0)->is_CountedLoop()) {
495 // If we've already peeked through a Cast (which could have set the
496 // control), we can't float above a Phi, because the skipped Cast
497 // may not be loop invariant.
498 if (adr_phi_is_loop_invariant(adr, skipped_cast)) {
499 adr = adr->in(1);
500 continue;
501 }
502 }
503
504 // Intentional fallthrough!
505
506 // No obvious dominating point. The mem op is pinned below the Phi
507 // by the Phi itself. If the Phi goes away (no true value is merged)
508 // then the mem op can float, but not indefinitely. It must be pinned
509 // behind the controls leading to the Phi.
510 case Op_CheckCastPP:
511 // These usually stick around to change address type, however a
512 // useless one can be elided and we still need to pick up a control edge
513 if (adr->in(0) == NULL) {
514 // This CheckCastPP node has NO control and is likely useless. But we
515 // need check further up the ancestor chain for a control input to keep
516 // the node in place. 4959717.
517 skipped_cast = adr;
518 adr = adr->in(1);
519 continue;
520 }
521 ccp->hash_delete(this);
522 set_req(MemNode::Control, adr->in(0));
523 ccp->hash_insert(this);
524 return this;
525
526 // List of "safe" opcodes; those that implicitly block the memory
527 // op below any null check.
528 case Op_CastX2P: // no null checks on native pointers
529 case Op_Parm: // 'this' pointer is not null
530 case Op_LoadP: // Loading from within a klass
531 case Op_LoadKlass: // Loading from within a klass
532 case Op_ConP: // Loading from a klass
533 case Op_CreateEx: // Sucking up the guts of an exception oop
534 case Op_Con: // Reading from TLS
535 case Op_CMoveP: // CMoveP is pinned
536 break; // No progress
537
538 case Op_Proj: // Direct call to an allocation routine
539 case Op_SCMemProj: // Memory state from store conditional ops
540 #ifdef ASSERT
541 {
542 assert(adr->as_Proj()->_con == TypeFunc::Parms, "must be return value");
543 const Node* call = adr->in(0);
544 if (call->is_CallStaticJava()) {
545 const CallStaticJavaNode* call_java = call->as_CallStaticJava();
546 assert(call_java && call_java->method() == NULL, "must be runtime call");
547 // We further presume that this is one of
548 // new_instance_Java, new_array_Java, or
549 // the like, but do not assert for this.
550 } else if (call->is_Allocate()) {
551 // similar case to new_instance_Java, etc.
552 } else if (!call->is_CallLeaf()) {
553 // Projections from fetch_oop (OSR) are allowed as well.
554 ShouldNotReachHere();
555 }
556 }
557 #endif
558 break;
559 default:
560 ShouldNotReachHere();
561 }
562 break;
563 }
564 }
565
566 return NULL; // No progress
572 uint LoadNode::cmp( const Node &n ) const
573 { return !Type::cmp( _type, ((LoadNode&)n)._type ); }
574 const Type *LoadNode::bottom_type() const { return _type; }
575 uint LoadNode::ideal_reg() const {
576 return Matcher::base2reg[_type->base()];
577 }
578
579 #ifndef PRODUCT
580 void LoadNode::dump_spec(outputStream *st) const {
581 MemNode::dump_spec(st);
582 if( !Verbose && !WizardMode ) {
583 // standard dump does this in Verbose and WizardMode
584 st->print(" #"); _type->dump_on(st);
585 }
586 }
587 #endif
588
589
590 //----------------------------LoadNode::make-----------------------------------
591 // Polymorphic factory method:
592 LoadNode *LoadNode::make( Compile *C, Node *ctl, Node *mem, Node *adr, const TypePtr* adr_type, const Type *rt, BasicType bt ) {
593 // sanity check the alias category against the created node type
594 assert(!(adr_type->isa_oopptr() &&
595 adr_type->offset() == oopDesc::klass_offset_in_bytes()),
596 "use LoadKlassNode instead");
597 assert(!(adr_type->isa_aryptr() &&
598 adr_type->offset() == arrayOopDesc::length_offset_in_bytes()),
599 "use LoadRangeNode instead");
600 switch (bt) {
601 case T_BOOLEAN:
602 case T_BYTE: return new (C, 3) LoadBNode(ctl, mem, adr, adr_type, rt->is_int() );
603 case T_INT: return new (C, 3) LoadINode(ctl, mem, adr, adr_type, rt->is_int() );
604 case T_CHAR: return new (C, 3) LoadCNode(ctl, mem, adr, adr_type, rt->is_int() );
605 case T_SHORT: return new (C, 3) LoadSNode(ctl, mem, adr, adr_type, rt->is_int() );
606 case T_LONG: return new (C, 3) LoadLNode(ctl, mem, adr, adr_type, rt->is_long() );
607 case T_FLOAT: return new (C, 3) LoadFNode(ctl, mem, adr, adr_type, rt );
608 case T_DOUBLE: return new (C, 3) LoadDNode(ctl, mem, adr, adr_type, rt );
609 case T_ADDRESS: return new (C, 3) LoadPNode(ctl, mem, adr, adr_type, rt->is_ptr() );
610 case T_OBJECT: return new (C, 3) LoadPNode(ctl, mem, adr, adr_type, rt->is_oopptr());
611 }
612 ShouldNotReachHere();
613 return (LoadNode*)NULL;
614 }
615
616 LoadLNode* LoadLNode::make_atomic(Compile *C, Node* ctl, Node* mem, Node* adr, const TypePtr* adr_type, const Type* rt) {
617 bool require_atomic = true;
618 return new (C, 3) LoadLNode(ctl, mem, adr, adr_type, rt->is_long(), require_atomic);
619 }
620
621
622
623
624 //------------------------------hash-------------------------------------------
625 uint LoadNode::hash() const {
626 // unroll addition of interesting fields
627 return (uintptr_t)in(Control) + (uintptr_t)in(Memory) + (uintptr_t)in(Address);
628 }
629
630 //---------------------------can_see_stored_value------------------------------
724
725 // A load from an initialization barrier can match a captured store.
726 if (st->is_Proj() && st->in(0)->is_Initialize()) {
727 InitializeNode* init = st->in(0)->as_Initialize();
728 AllocateNode* alloc = init->allocation();
729 if (alloc != NULL &&
730 alloc == AllocateNode::Ideal_allocation(ld_adr, phase, offset)) {
731 // examine a captured store value
732 st = init->find_captured_store(offset, memory_size(), phase);
733 if (st != NULL)
734 continue; // take one more trip around
735 }
736 }
737
738 break;
739 }
740
741 return NULL;
742 }
743
744 //------------------------------Identity---------------------------------------
745 // Loads are identity if previous store is to same address
746 Node *LoadNode::Identity( PhaseTransform *phase ) {
747 // If the previous store-maker is the right kind of Store, and the store is
748 // to the same address, then we are equal to the value stored.
749 Node* mem = in(MemNode::Memory);
750 Node* value = can_see_stored_value(mem, phase);
751 if( value ) {
752 // byte, short & char stores truncate naturally.
753 // A load has to load the truncated value which requires
754 // some sort of masking operation and that requires an
755 // Ideal call instead of an Identity call.
756 if (memory_size() < BytesPerInt) {
757 // If the input to the store does not fit with the load's result type,
758 // it must be truncated via an Ideal call.
759 if (!phase->type(value)->higher_equal(phase->type(this)))
760 return this;
761 }
762 // (This works even when value is a Con, but LoadNode::Value
763 // usually runs first, producing the singleton type of the Con.)
764 return value;
765 }
766 return this;
767 }
768
769
770 // Returns true if the AliasType refers to the field that holds the
771 // cached box array. Currently only handles the IntegerCache case.
772 static bool is_autobox_cache(Compile::AliasType* atp) {
773 if (atp != NULL && atp->field() != NULL) {
774 ciField* field = atp->field();
775 ciSymbol* klass = field->holder()->name();
776 if (field->name() == ciSymbol::cache_field_name() &&
777 field->holder()->uses_default_loader() &&
778 klass == ciSymbol::java_lang_Integer_IntegerCache()) {
779 return true;
780 }
781 }
782 return false;
783 }
784
785 // Fetch the base value in the autobox array
825
826 // We're loading from an object which has autobox behaviour.
827 // If this object is result of a valueOf call we'll have a phi
828 // merging a newly allocated object and a load from the cache.
829 // We want to replace this load with the original incoming
830 // argument to the valueOf call.
831 Node* LoadNode::eliminate_autobox(PhaseGVN* phase) {
832 Node* base = in(Address)->in(AddPNode::Base);
833 if (base->is_Phi() && base->req() == 3) {
834 AllocateNode* allocation = NULL;
835 int allocation_index = -1;
836 int load_index = -1;
837 for (uint i = 1; i < base->req(); i++) {
838 allocation = AllocateNode::Ideal_allocation(base->in(i), phase);
839 if (allocation != NULL) {
840 allocation_index = i;
841 load_index = 3 - allocation_index;
842 break;
843 }
844 }
845 LoadNode* load = NULL;
846 if (allocation != NULL && base->in(load_index)->is_Load()) {
847 load = base->in(load_index)->as_Load();
848 }
849 if (load != NULL && in(Memory)->is_Phi() && in(Memory)->in(0) == base->in(0)) {
850 // Push the loads from the phi that comes from valueOf up
851 // through it to allow elimination of the loads and the recovery
852 // of the original value.
853 Node* mem_phi = in(Memory);
854 Node* offset = in(Address)->in(AddPNode::Offset);
855
856 Node* in1 = clone();
857 Node* in1_addr = in1->in(Address)->clone();
858 in1_addr->set_req(AddPNode::Base, base->in(allocation_index));
859 in1_addr->set_req(AddPNode::Address, base->in(allocation_index));
860 in1_addr->set_req(AddPNode::Offset, offset);
861 in1->set_req(0, base->in(allocation_index));
862 in1->set_req(Address, in1_addr);
863 in1->set_req(Memory, mem_phi->in(allocation_index));
864
865 Node* in2 = clone();
866 Node* in2_addr = in2->in(Address)->clone();
867 in2_addr->set_req(AddPNode::Base, base->in(load_index));
868 in2_addr->set_req(AddPNode::Address, base->in(load_index));
869 in2_addr->set_req(AddPNode::Offset, offset);
870 in2->set_req(0, base->in(load_index));
871 in2->set_req(Address, in2_addr);
872 in2->set_req(Memory, mem_phi->in(load_index));
873
874 in1_addr = phase->transform(in1_addr);
875 in1 = phase->transform(in1);
876 in2_addr = phase->transform(in2_addr);
877 in2 = phase->transform(in2);
878
879 PhiNode* result = PhiNode::make_blank(base->in(0), this);
880 result->set_req(allocation_index, in1);
881 result->set_req(load_index, in2);
882 return result;
883 }
884 } else if (base->is_Load()) {
885 // Eliminate the load of Integer.value for integers from the cache
886 // array by deriving the value from the index into the array.
887 // Capture the offset of the load and then reverse the computation.
888 Node* load_base = base->in(Address)->in(AddPNode::Base);
889 if (load_base != NULL) {
890 Compile::AliasType* atp = phase->C->alias_type(load_base->adr_type());
891 intptr_t cache_offset;
892 int shift = -1;
893 Node* cache = NULL;
894 if (is_autobox_cache(atp)) {
895 shift = exact_log2(type2aelembytes[T_OBJECT]);
896 cache = AddPNode::Ideal_base_and_offset(load_base->in(Address), phase, cache_offset);
897 }
898 if (cache != NULL && base->in(Address)->is_AddP()) {
899 Node* elements[4];
900 int count = base->in(Address)->as_AddP()->unpack_offsets(elements, ARRAY_SIZE(elements));
901 int cache_low;
902 if (count > 0 && fetch_autobox_base(atp, cache_low)) {
903 int offset = arrayOopDesc::base_offset_in_bytes(memory_type()) - (cache_low << shift);
904 // Add up all the offsets making of the address of the load
905 Node* result = elements[0];
906 for (int i = 1; i < count; i++) {
907 result = phase->transform(new (phase->C, 3) AddXNode(result, elements[i]));
908 }
909 // Remove the constant offset from the address and then
910 // remove the scaling of the offset to recover the original index.
911 result = phase->transform(new (phase->C, 3) AddXNode(result, phase->MakeConX(-offset)));
912 if (result->Opcode() == Op_LShiftX && result->in(2) == phase->intcon(shift)) {
913 // Peel the shift off directly but wrap it in a dummy node
914 // since Ideal can't return existing nodes
915 result = new (phase->C, 3) RShiftXNode(result->in(1), phase->intcon(0));
916 } else {
917 result = new (phase->C, 3) RShiftXNode(result, phase->intcon(shift));
918 }
919 #ifdef _LP64
920 result = new (phase->C, 2) ConvL2INode(phase->transform(result));
921 #endif
922 return result;
923 }
924 }
925 }
926 }
927 return NULL;
928 }
929
930
931 //------------------------------Ideal------------------------------------------
932 // If the load is from Field memory and the pointer is non-null, we can
933 // zero out the control input.
934 // If the offset is constant and the base is an object allocation,
935 // try to hook me up to the exact initializing store.
936 Node *LoadNode::Ideal(PhaseGVN *phase, bool can_reshape) {
937 Node* p = MemNode::Ideal_common(phase, can_reshape);
938 if (p) return (p == NodeSentinel) ? NULL : p;
939
940 Node* ctrl = in(MemNode::Control);
941 Node* address = in(MemNode::Address);
942
943 // Skip up past a SafePoint control. Cannot do this for Stores because
944 // pointer stores & cardmarks must stay on the same side of a SafePoint.
945 if( ctrl != NULL && ctrl->Opcode() == Op_SafePoint &&
946 phase->C->get_alias_index(phase->type(address)->is_ptr()) != Compile::AliasIdxRaw ) {
947 ctrl = ctrl->in(0);
948 set_req(MemNode::Control,ctrl);
949 }
950
951 // Check for useless control edge in some common special cases
952 if (in(MemNode::Control) != NULL) {
953 intptr_t ignore = 0;
954 Node* base = AddPNode::Ideal_base_and_offset(address, phase, ignore);
955 if (base != NULL
956 && phase->type(base)->higher_equal(TypePtr::NOTNULL)
957 && detect_dominating_control(base->in(0), phase->C->start())) {
958 // A method-invariant, non-null address (constant or 'this' argument).
959 set_req(MemNode::Control, NULL);
960 }
961 }
962
963 if (EliminateAutoBox && can_reshape && in(Address)->is_AddP()) {
964 Node* base = in(Address)->in(AddPNode::Base);
965 if (base != NULL) {
966 Compile::AliasType* atp = phase->C->alias_type(adr_type());
967 if (is_autobox_object(atp)) {
968 Node* result = eliminate_autobox(phase);
969 if (result != NULL) return result;
970 }
971 }
972 }
973
974 // Check for prior store with a different base or offset; make Load
975 // independent. Skip through any number of them. Bail out if the stores
976 // are in an endless dead cycle and report no progress. This is a key
977 // transform for Reflection. However, if after skipping through the Stores
978 // we can't then fold up against a prior store do NOT do the transform as
979 // this amounts to using the 'Oracle' model of aliasing. It leaves the same
980 // array memory alive twice: once for the hoisted Load and again after the
981 // bypassed Store. This situation only works if EVERYBODY who does
982 // anti-dependence work knows how to bypass. I.e. we need all
983 // anti-dependence checks to ask the same Oracle. Right now, that Oracle is
984 // the alias index stuff. So instead, peek through Stores and IFF we can
985 // fold up, do so.
986 Node* prev_mem = find_previous_store(phase);
987 // Steps (a), (b): Walk past independent stores to find an exact match.
988 if (prev_mem != NULL && prev_mem != in(MemNode::Memory)) {
989 // (c) See if we can fold up on the spot, but don't fold up here.
990 // Fold-up might require truncation (for LoadB/LoadS/LoadC) or
991 // just return a prior value, which is done by Identity calls.
992 if (can_see_stored_value(prev_mem, phase)) {
993 // Make ready for step (d):
1040
1041 // Try to guess loaded type from pointer type
1042 if (tp->base() == Type::AryPtr) {
1043 const Type *t = tp->is_aryptr()->elem();
1044 // Don't do this for integer types. There is only potential profit if
1045 // the element type t is lower than _type; that is, for int types, if _type is
1046 // more restrictive than t. This only happens here if one is short and the other
1047 // char (both 16 bits), and in those cases we've made an intentional decision
1048 // to use one kind of load over the other. See AndINode::Ideal and 4965907.
1049 // Also, do not try to narrow the type for a LoadKlass, regardless of offset.
1050 //
1051 // Yes, it is possible to encounter an expression like (LoadKlass p1:(AddP x x 8))
1052 // where the _gvn.type of the AddP is wider than 8. This occurs when an earlier
1053 // copy p0 of (AddP x x 8) has been proven equal to p1, and the p0 has been
1054 // subsumed by p1. If p1 is on the worklist but has not yet been re-transformed,
1055 // it is possible that p1 will have a type like Foo*[int+]:NotNull*+any.
1056 // In fact, that could have been the original type of p1, and p1 could have
1057 // had an original form like p1:(AddP x x (LShiftL quux 3)), where the
1058 // expression (LShiftL quux 3) independently optimized to the constant 8.
1059 if ((t->isa_int() == NULL) && (t->isa_long() == NULL)
1060 && Opcode() != Op_LoadKlass) {
1061 // t might actually be lower than _type, if _type is a unique
1062 // concrete subclass of abstract class t.
1063 // Make sure the reference is not into the header, by comparing
1064 // the offset against the offset of the start of the array's data.
1065 // Different array types begin at slightly different offsets (12 vs. 16).
1066 // We choose T_BYTE as an example base type that is least restrictive
1067 // as to alignment, which will therefore produce the smallest
1068 // possible base offset.
1069 const int min_base_off = arrayOopDesc::base_offset_in_bytes(T_BYTE);
1070 if ((uint)off >= (uint)min_base_off) { // is the offset beyond the header?
1071 const Type* jt = t->join(_type);
1072 // In any case, do not allow the join, per se, to empty out the type.
1073 if (jt->empty() && !t->empty()) {
1074 // This can happen if a interface-typed array narrows to a class type.
1075 jt = _type;
1076 }
1077
1078 if (EliminateAutoBox) {
1079 // The pointers in the autobox arrays are always non-null
1080 Node* base = in(Address)->in(AddPNode::Base);
1181 // Note: When interfaces are reliable, we can narrow the interface
1182 // test to (klass != Serializable && klass != Cloneable).
1183 assert(Opcode() == Op_LoadI, "must load an int from _layout_helper");
1184 jint min_size = Klass::instance_layout_helper(oopDesc::header_size(), false);
1185 // The key property of this type is that it folds up tests
1186 // for array-ness, since it proves that the layout_helper is positive.
1187 // Thus, a generic value like the basic object layout helper works fine.
1188 return TypeInt::make(min_size, max_jint, Type::WidenMin);
1189 }
1190 }
1191
1192 // If we are loading from a freshly-allocated object, produce a zero,
1193 // if the load is provably beyond the header of the object.
1194 // (Also allow a variable load from a fresh array to produce zero.)
1195 if (ReduceFieldZeroing) {
1196 Node* value = can_see_stored_value(mem,phase);
1197 if (value != NULL && value->is_Con())
1198 return value->bottom_type();
1199 }
1200
1201 return _type;
1202 }
1203
1204 //------------------------------match_edge-------------------------------------
1205 // Do we Match on this edge index or not? Match only the address.
1206 uint LoadNode::match_edge(uint idx) const {
1207 return idx == MemNode::Address;
1208 }
1209
1210 //--------------------------LoadBNode::Ideal--------------------------------------
1211 //
1212 // If the previous store is to the same address as this load,
1213 // and the value stored was larger than a byte, replace this load
1214 // with the value stored truncated to a byte. If no truncation is
1215 // needed, the replacement is done in LoadNode::Identity().
1216 //
1217 Node *LoadBNode::Ideal(PhaseGVN *phase, bool can_reshape) {
1218 Node* mem = in(MemNode::Memory);
1219 Node* value = can_see_stored_value(mem,phase);
1220 if( value && !phase->type(value)->higher_equal( _type ) ) {
1243
1244 //--------------------------LoadSNode::Ideal--------------------------------------
1245 //
1246 // If the previous store is to the same address as this load,
1247 // and the value stored was larger than a short, replace this load
1248 // with the value stored truncated to a short. If no truncation is
1249 // needed, the replacement is done in LoadNode::Identity().
1250 //
1251 Node *LoadSNode::Ideal(PhaseGVN *phase, bool can_reshape) {
1252 Node* mem = in(MemNode::Memory);
1253 Node* value = can_see_stored_value(mem,phase);
1254 if( value && !phase->type(value)->higher_equal( _type ) ) {
1255 Node *result = phase->transform( new (phase->C, 3) LShiftINode(value, phase->intcon(16)) );
1256 return new (phase->C, 3) RShiftINode(result, phase->intcon(16));
1257 }
1258 // Identity call will handle the case where truncation is not needed.
1259 return LoadNode::Ideal(phase, can_reshape);
1260 }
1261
1262 //=============================================================================
1263 //------------------------------Value------------------------------------------
1264 const Type *LoadKlassNode::Value( PhaseTransform *phase ) const {
1265 // Either input is TOP ==> the result is TOP
1266 const Type *t1 = phase->type( in(MemNode::Memory) );
1267 if (t1 == Type::TOP) return Type::TOP;
1268 Node *adr = in(MemNode::Address);
1269 const Type *t2 = phase->type( adr );
1270 if (t2 == Type::TOP) return Type::TOP;
1271 const TypePtr *tp = t2->is_ptr();
1272 if (TypePtr::above_centerline(tp->ptr()) ||
1273 tp->ptr() == TypePtr::Null) return Type::TOP;
1274
1275 // Return a more precise klass, if possible
1276 const TypeInstPtr *tinst = tp->isa_instptr();
1277 if (tinst != NULL) {
1278 ciInstanceKlass* ik = tinst->klass()->as_instance_klass();
1279 int offset = tinst->offset();
1280 if (ik == phase->C->env()->Class_klass()
1281 && (offset == java_lang_Class::klass_offset_in_bytes() ||
1282 offset == java_lang_Class::array_klass_offset_in_bytes())) {
1283 // We are loading a special hidden field from a Class mirror object,
1284 // the field which points to the VM's Klass metaobject.
1376 // according to the element type's subclassing.
1377 return TypeKlassPtr::make(tkls->ptr(), elem, 0/*offset*/);
1378 }
1379 if( klass->is_instance_klass() && tkls->klass_is_exact() &&
1380 (uint)tkls->offset() == Klass::super_offset_in_bytes() + sizeof(oopDesc)) {
1381 ciKlass* sup = klass->as_instance_klass()->super();
1382 // The field is Klass::_super. Return its (constant) value.
1383 // (Folds up the 2nd indirection in aClassConstant.getSuperClass().)
1384 return sup ? TypeKlassPtr::make(sup) : TypePtr::NULL_PTR;
1385 }
1386 }
1387
1388 // Bailout case
1389 return LoadNode::Value(phase);
1390 }
1391
1392 //------------------------------Identity---------------------------------------
1393 // To clean up reflective code, simplify k.java_mirror.as_klass to plain k.
1394 // Also feed through the klass in Allocate(...klass...)._klass.
1395 Node* LoadKlassNode::Identity( PhaseTransform *phase ) {
1396 Node* x = LoadNode::Identity(phase);
1397 if (x != this) return x;
1398
1399 // Take apart the address into an oop and and offset.
1400 // Return 'this' if we cannot.
1401 Node* adr = in(MemNode::Address);
1402 intptr_t offset = 0;
1403 Node* base = AddPNode::Ideal_base_and_offset(adr, phase, offset);
1404 if (base == NULL) return this;
1405 const TypeOopPtr* toop = phase->type(adr)->isa_oopptr();
1406 if (toop == NULL) return this;
1407
1408 // We can fetch the klass directly through an AllocateNode.
1409 // This works even if the klass is not constant (clone or newArray).
1410 if (offset == oopDesc::klass_offset_in_bytes()) {
1411 Node* allocated_klass = AllocateNode::Ideal_klass(base, phase);
1412 if (allocated_klass != NULL) {
1413 return allocated_klass;
1414 }
1415 }
1434 const TypeKlassPtr* tkls = phase->type(adr2)->isa_klassptr();
1435 if (tkls != NULL && !tkls->empty()
1436 && (tkls->klass()->is_instance_klass() ||
1437 tkls->klass()->is_array_klass())
1438 && adr2->is_AddP()
1439 ) {
1440 int mirror_field = Klass::java_mirror_offset_in_bytes();
1441 if (offset == java_lang_Class::array_klass_offset_in_bytes()) {
1442 mirror_field = in_bytes(arrayKlass::component_mirror_offset());
1443 }
1444 if (tkls->offset() == mirror_field + (int)sizeof(oopDesc)) {
1445 return adr2->in(AddPNode::Base);
1446 }
1447 }
1448 }
1449 }
1450
1451 return this;
1452 }
1453
1454 //------------------------------Value-----------------------------------------
1455 const Type *LoadRangeNode::Value( PhaseTransform *phase ) const {
1456 // Either input is TOP ==> the result is TOP
1457 const Type *t1 = phase->type( in(MemNode::Memory) );
1458 if( t1 == Type::TOP ) return Type::TOP;
1459 Node *adr = in(MemNode::Address);
1460 const Type *t2 = phase->type( adr );
1461 if( t2 == Type::TOP ) return Type::TOP;
1462 const TypePtr *tp = t2->is_ptr();
1463 if (TypePtr::above_centerline(tp->ptr())) return Type::TOP;
1464 const TypeAryPtr *tap = tp->isa_aryptr();
1465 if( !tap ) return _type;
1466 return tap->size();
1467 }
1468
1469 //------------------------------Identity---------------------------------------
1470 // Feed through the length in AllocateArray(...length...)._length.
1471 Node* LoadRangeNode::Identity( PhaseTransform *phase ) {
1472 Node* x = LoadINode::Identity(phase);
1473 if (x != this) return x;
1474
1475 // Take apart the address into an oop and and offset.
1476 // Return 'this' if we cannot.
1477 Node* adr = in(MemNode::Address);
1478 intptr_t offset = 0;
1479 Node* base = AddPNode::Ideal_base_and_offset(adr, phase, offset);
1480 if (base == NULL) return this;
1481 const TypeAryPtr* tary = phase->type(adr)->isa_aryptr();
1482 if (tary == NULL) return this;
1483
1484 // We can fetch the length directly through an AllocateArrayNode.
1485 // This works even if the length is not constant (clone or newArray).
1486 if (offset == arrayOopDesc::length_offset_in_bytes()) {
1487 Node* allocated_length = AllocateArrayNode::Ideal_length(base, phase);
1488 if (allocated_length != NULL) {
1489 return allocated_length;
1490 }
1491 }
1492
1493 return this;
1494
1495 }
1496 //=============================================================================
1497 //---------------------------StoreNode::make-----------------------------------
1498 // Polymorphic factory method:
1499 StoreNode* StoreNode::make( Compile *C, Node* ctl, Node* mem, Node* adr, const TypePtr* adr_type, Node* val, BasicType bt ) {
1500 switch (bt) {
1501 case T_BOOLEAN:
1502 case T_BYTE: return new (C, 4) StoreBNode(ctl, mem, adr, adr_type, val);
1503 case T_INT: return new (C, 4) StoreINode(ctl, mem, adr, adr_type, val);
1504 case T_CHAR:
1505 case T_SHORT: return new (C, 4) StoreCNode(ctl, mem, adr, adr_type, val);
1506 case T_LONG: return new (C, 4) StoreLNode(ctl, mem, adr, adr_type, val);
1507 case T_FLOAT: return new (C, 4) StoreFNode(ctl, mem, adr, adr_type, val);
1508 case T_DOUBLE: return new (C, 4) StoreDNode(ctl, mem, adr, adr_type, val);
1509 case T_ADDRESS:
1510 case T_OBJECT: return new (C, 4) StorePNode(ctl, mem, adr, adr_type, val);
1511 }
1512 ShouldNotReachHere();
1513 return (StoreNode*)NULL;
1514 }
1515
1516 StoreLNode* StoreLNode::make_atomic(Compile *C, Node* ctl, Node* mem, Node* adr, const TypePtr* adr_type, Node* val) {
1517 bool require_atomic = true;
1518 return new (C, 4) StoreLNode(ctl, mem, adr, adr_type, val, require_atomic);
1519 }
1520
1521
1522 //--------------------------bottom_type----------------------------------------
1523 const Type *StoreNode::bottom_type() const {
1524 return Type::MEMORY;
1525 }
1526
1527 //------------------------------hash-------------------------------------------
1528 uint StoreNode::hash() const {
1529 // unroll addition of interesting fields
1530 //return (uintptr_t)in(Control) + (uintptr_t)in(Memory) + (uintptr_t)in(Address) + (uintptr_t)in(ValueIn);
1704 if( t2 && t2->is_con() && (t2->get_con() == t->get_con()) ) {
1705 set_req(MemNode::ValueIn, shl->in(1));
1706 return this;
1707 }
1708 }
1709 }
1710 }
1711 return NULL;
1712 }
1713
1714 //------------------------------value_never_loaded-----------------------------------
1715 // Determine whether there are any possible loads of the value stored.
1716 // For simplicity, we actually check if there are any loads from the
1717 // address stored to, not just for loads of the value stored by this node.
1718 //
1719 bool StoreNode::value_never_loaded( PhaseTransform *phase) const {
1720 Node *adr = in(Address);
1721 const TypeOopPtr *adr_oop = phase->type(adr)->isa_oopptr();
1722 if (adr_oop == NULL)
1723 return false;
1724 if (!adr_oop->is_instance())
1725 return false; // if not a distinct instance, there may be aliases of the address
1726 for (DUIterator_Fast imax, i = adr->fast_outs(imax); i < imax; i++) {
1727 Node *use = adr->fast_out(i);
1728 int opc = use->Opcode();
1729 if (use->is_Load() || use->is_LoadStore()) {
1730 return false;
1731 }
1732 }
1733 return true;
1734 }
1735
1736 //=============================================================================
1737 //------------------------------Ideal------------------------------------------
1738 // If the store is from an AND mask that leaves the low bits untouched, then
1739 // we can skip the AND operation. If the store is from a sign-extension
1740 // (a left shift, then right shift) we can skip both.
1741 Node *StoreBNode::Ideal(PhaseGVN *phase, bool can_reshape){
1742 Node *progress = StoreNode::Ideal_masked_input(phase, 0xFF);
1743 if( progress != NULL ) return progress;
1744
1813
1814 }
1815
1816 //=============================================================================
1817 //-------------------------------adr_type--------------------------------------
1818 // Do we Match on this edge index or not? Do not match memory
1819 const TypePtr* ClearArrayNode::adr_type() const {
1820 Node *adr = in(3);
1821 return MemNode::calculate_adr_type(adr->bottom_type());
1822 }
1823
1824 //------------------------------match_edge-------------------------------------
1825 // Do we Match on this edge index or not? Do not match memory
1826 uint ClearArrayNode::match_edge(uint idx) const {
1827 return idx > 1;
1828 }
1829
1830 //------------------------------Identity---------------------------------------
1831 // Clearing a zero length array does nothing
1832 Node *ClearArrayNode::Identity( PhaseTransform *phase ) {
1833 return phase->type(in(2))->higher_equal(TypeInt::ZERO) ? in(1) : this;
1834 }
1835
1836 //------------------------------Idealize---------------------------------------
1837 // Clearing a short array is faster with stores
1838 Node *ClearArrayNode::Ideal(PhaseGVN *phase, bool can_reshape){
1839 const int unit = BytesPerLong;
1840 const TypeX* t = phase->type(in(2))->isa_intptr_t();
1841 if (!t) return NULL;
1842 if (!t->is_con()) return NULL;
1843 intptr_t raw_count = t->get_con();
1844 intptr_t size = raw_count;
1845 if (!Matcher::init_array_count_is_in_bytes) size *= unit;
1846 // Clearing nothing uses the Identity call.
1847 // Negative clears are possible on dead ClearArrays
1848 // (see jck test stmt114.stmt11402.val).
1849 if (size <= 0 || size % unit != 0) return NULL;
1850 intptr_t count = size / unit;
1851 // Length too long; use fast hardware clear
1852 if (size > Matcher::init_array_short_size) return NULL;
1853 Node *mem = in(1);
1872 adr = phase->transform(new (phase->C, 4) AddPNode(base,adr,off));
1873 mem = new (phase->C, 4) StoreLNode(in(0),mem,adr,atp,zero);
1874 }
1875 return mem;
1876 }
1877
1878 //----------------------------clear_memory-------------------------------------
1879 // Generate code to initialize object storage to zero.
1880 Node* ClearArrayNode::clear_memory(Node* ctl, Node* mem, Node* dest,
1881 intptr_t start_offset,
1882 Node* end_offset,
1883 PhaseGVN* phase) {
1884 Compile* C = phase->C;
1885 intptr_t offset = start_offset;
1886
1887 int unit = BytesPerLong;
1888 if ((offset % unit) != 0) {
1889 Node* adr = new (C, 4) AddPNode(dest, dest, phase->MakeConX(offset));
1890 adr = phase->transform(adr);
1891 const TypePtr* atp = TypeRawPtr::BOTTOM;
1892 mem = StoreNode::make(C, ctl, mem, adr, atp, phase->zerocon(T_INT), T_INT);
1893 mem = phase->transform(mem);
1894 offset += BytesPerInt;
1895 }
1896 assert((offset % unit) == 0, "");
1897
1898 // Initialize the remaining stuff, if any, with a ClearArray.
1899 return clear_memory(ctl, mem, dest, phase->MakeConX(offset), end_offset, phase);
1900 }
1901
1902 Node* ClearArrayNode::clear_memory(Node* ctl, Node* mem, Node* dest,
1903 Node* start_offset,
1904 Node* end_offset,
1905 PhaseGVN* phase) {
1906 Compile* C = phase->C;
1907 int unit = BytesPerLong;
1908 Node* zbase = start_offset;
1909 Node* zend = end_offset;
1910
1911 // Scale to the unit required by the CPU:
1912 if (!Matcher::init_array_count_is_in_bytes) {
1913 Node* shift = phase->intcon(exact_log2(unit));
1914 zbase = phase->transform( new(C,3) URShiftXNode(zbase, shift) );
1915 zend = phase->transform( new(C,3) URShiftXNode(zend, shift) );
1916 }
1917
1918 Node* zsize = phase->transform( new(C,3) SubXNode(zend, zbase) );
1919 Node* zinit = phase->zerocon((unit == BytesPerLong) ? T_LONG : T_INT);
1920
1921 // Bulk clear double-words
1922 Node* adr = phase->transform( new(C,4) AddPNode(dest, dest, start_offset) );
1923 mem = new (C, 4) ClearArrayNode(ctl, mem, zsize, adr);
1924 return phase->transform(mem);
1925 }
1926
1927 Node* ClearArrayNode::clear_memory(Node* ctl, Node* mem, Node* dest,
1928 intptr_t start_offset,
1929 intptr_t end_offset,
1930 PhaseGVN* phase) {
1931 Compile* C = phase->C;
1932 assert((end_offset % BytesPerInt) == 0, "odd end offset");
1933 intptr_t done_offset = end_offset;
1934 if ((done_offset % BytesPerLong) != 0) {
1935 done_offset -= BytesPerInt;
1936 }
1937 if (done_offset > start_offset) {
1938 mem = clear_memory(ctl, mem, dest,
1939 start_offset, phase->MakeConX(done_offset), phase);
1940 }
1941 if (done_offset < end_offset) { // emit the final 32-bit store
1942 Node* adr = new (C, 4) AddPNode(dest, dest, phase->MakeConX(done_offset));
1943 adr = phase->transform(adr);
1944 const TypePtr* atp = TypeRawPtr::BOTTOM;
1945 mem = StoreNode::make(C, ctl, mem, adr, atp, phase->zerocon(T_INT), T_INT);
1946 mem = phase->transform(mem);
1947 done_offset += BytesPerInt;
1948 }
1949 assert(done_offset == end_offset, "");
1950 return mem;
1951 }
1952
1953 //=============================================================================
1954 // Do we match on this edge? No memory edges
1955 uint StrCompNode::match_edge(uint idx) const {
1956 return idx == 5 || idx == 6;
1957 }
1958
1959 //------------------------------Ideal------------------------------------------
1960 // Return a node which is more "ideal" than the current node. Strip out
1961 // control copies
1962 Node *StrCompNode::Ideal(PhaseGVN *phase, bool can_reshape){
1963 return remove_dead_region(phase, can_reshape) ? this : NULL;
1964 }
1965
1966
1967 //=============================================================================
1968 MemBarNode::MemBarNode(Compile* C, int alias_idx, Node* precedent)
1969 : MultiNode(TypeFunc::Parms + (precedent == NULL? 0: 1)),
1970 _adr_type(C->get_adr_type(alias_idx))
1971 {
1972 init_class_id(Class_MemBar);
1973 Node* top = C->top();
1974 init_req(TypeFunc::I_O,top);
1975 init_req(TypeFunc::FramePtr,top);
1976 init_req(TypeFunc::ReturnAdr,top);
1977 if (precedent != NULL)
1978 init_req(TypeFunc::Parms, precedent);
1979 }
1980
1981 //------------------------------cmp--------------------------------------------
1982 uint MemBarNode::hash() const { return NO_HASH; }
1983 uint MemBarNode::cmp( const Node &n ) const {
1984 return (&n == this); // Always fail except on self
1985 }
1986
1987 //------------------------------make-------------------------------------------
1988 MemBarNode* MemBarNode::make(Compile* C, int opcode, int atp, Node* pn) {
1989 int len = Precedent + (pn == NULL? 0: 1);
1990 switch (opcode) {
1991 case Op_MemBarAcquire: return new(C, len) MemBarAcquireNode(C, atp, pn);
1992 case Op_MemBarRelease: return new(C, len) MemBarReleaseNode(C, atp, pn);
1993 case Op_MemBarVolatile: return new(C, len) MemBarVolatileNode(C, atp, pn);
1994 case Op_MemBarCPUOrder: return new(C, len) MemBarCPUOrderNode(C, atp, pn);
1995 case Op_Initialize: return new(C, len) InitializeNode(C, atp, pn);
1996 default: ShouldNotReachHere(); return NULL;
1997 }
1998 }
1999
2000 //------------------------------Ideal------------------------------------------
2001 // Return a node which is more "ideal" than the current node. Strip out
2002 // control copies
2003 Node *MemBarNode::Ideal(PhaseGVN *phase, bool can_reshape) {
2004 if (remove_dead_region(phase, can_reshape)) return this;
2005 return NULL;
2006 }
2007
2008 //------------------------------Value------------------------------------------
2009 const Type *MemBarNode::Value( PhaseTransform *phase ) const {
2010 if( !in(0) ) return Type::TOP;
2011 if( phase->type(in(0)) == Type::TOP )
2012 return Type::TOP;
2013 return TypeTuple::MEMBAR;
2014 }
2015
2016 //------------------------------match------------------------------------------
2017 // Construct projections for memory.
2018 Node *MemBarNode::match( const ProjNode *proj, const Matcher *m ) {
2019 switch (proj->_con) {
2020 case TypeFunc::Control:
2021 case TypeFunc::Memory:
2022 return new (m->C, 1) MachProjNode(this,proj->_con,RegMask::Empty,MachProjNode::unmatched_proj);
2023 }
2024 ShouldNotReachHere();
2025 return NULL;
2215 bool InitializeNode::detect_init_independence(Node* n,
2216 bool st_is_pinned,
2217 int& count) {
2218 if (n == NULL) return true; // (can this really happen?)
2219 if (n->is_Proj()) n = n->in(0);
2220 if (n == this) return false; // found a cycle
2221 if (n->is_Con()) return true;
2222 if (n->is_Start()) return true; // params, etc., are OK
2223 if (n->is_Root()) return true; // even better
2224
2225 Node* ctl = n->in(0);
2226 if (ctl != NULL && !ctl->is_top()) {
2227 if (ctl->is_Proj()) ctl = ctl->in(0);
2228 if (ctl == this) return false;
2229
2230 // If we already know that the enclosing memory op is pinned right after
2231 // the init, then any control flow that the store has picked up
2232 // must have preceded the init, or else be equal to the init.
2233 // Even after loop optimizations (which might change control edges)
2234 // a store is never pinned *before* the availability of its inputs.
2235 if (!MemNode::detect_dominating_control(ctl, this->in(0)))
2236 return false; // failed to prove a good control
2237
2238 }
2239
2240 // Check data edges for possible dependencies on 'this'.
2241 if ((count += 1) > 20) return false; // complexity limit
2242 for (uint i = 1; i < n->req(); i++) {
2243 Node* m = n->in(i);
2244 if (m == NULL || m == n || m->is_top()) continue;
2245 uint first_i = n->find_edge(m);
2246 if (i != first_i) continue; // process duplicate edge just once
2247 if (!detect_init_independence(m, st_is_pinned, count)) {
2248 return false;
2249 }
2250 }
2251
2252 return true;
2253 }
2254
2255 // Here are all the checks a Store must pass before it can be moved into
2282 }
2283
2284 // Find the captured store in(i) which corresponds to the range
2285 // [start..start+size) in the initialized object.
2286 // If there is one, return its index i. If there isn't, return the
2287 // negative of the index where it should be inserted.
2288 // Return 0 if the queried range overlaps an initialization boundary
2289 // or if dead code is encountered.
2290 // If size_in_bytes is zero, do not bother with overlap checks.
2291 int InitializeNode::captured_store_insertion_point(intptr_t start,
2292 int size_in_bytes,
2293 PhaseTransform* phase) {
2294 const int FAIL = 0, MAX_STORE = BytesPerLong;
2295
2296 if (is_complete())
2297 return FAIL; // arraycopy got here first; punt
2298
2299 assert(allocation() != NULL, "must be present");
2300
2301 // no negatives, no header fields:
2302 if (start < (intptr_t) sizeof(oopDesc)) return FAIL;
2303 if (start < (intptr_t) sizeof(arrayOopDesc) &&
2304 start < (intptr_t) allocation()->minimum_header_size()) return FAIL;
2305
2306 // after a certain size, we bail out on tracking all the stores:
2307 intptr_t ti_limit = (TrackedInitializationLimit * HeapWordSize);
2308 if (start >= ti_limit) return FAIL;
2309
2310 for (uint i = InitializeNode::RawStores, limit = req(); ; ) {
2311 if (i >= limit) return -(int)i; // not found; here is where to put it
2312
2313 Node* st = in(i);
2314 intptr_t st_off = get_store_offset(st, phase);
2315 if (st_off < 0) {
2316 if (st != zero_memory()) {
2317 return FAIL; // bail out if there is dead garbage
2318 }
2319 } else if (st_off > start) {
2320 // ...we are done, since stores are ordered
2321 if (st_off < start + size_in_bytes) {
2322 return FAIL; // the next store overlaps
2323 }
2324 return -(int)i; // not found; here is where to put it
2621 }
2622
2623 // Here's a case where init0 is neither 0 nor -1:
2624 // byte a[] = { ... 0,0,foo(),0, 0,0,0,42 ... }
2625 // Assuming big-endian memory, init0, init1 are 0x0000FF00, 0x000000FF.
2626 // In this case the tile is not split; it is (jlong)42.
2627 // The big tile is stored down, and then the foo() value is inserted.
2628 // (If there were foo(),foo() instead of foo(),0, init0 would be -1.)
2629
2630 Node* ctl = old->in(MemNode::Control);
2631 Node* adr = make_raw_address(offset, phase);
2632 const TypePtr* atp = TypeRawPtr::BOTTOM;
2633
2634 // One or two coalesced stores to plop down.
2635 Node* st[2];
2636 intptr_t off[2];
2637 int nst = 0;
2638 if (!split) {
2639 ++new_long;
2640 off[nst] = offset;
2641 st[nst++] = StoreNode::make(C, ctl, zmem, adr, atp,
2642 phase->longcon(con), T_LONG);
2643 } else {
2644 // Omit either if it is a zero.
2645 if (con0 != 0) {
2646 ++new_int;
2647 off[nst] = offset;
2648 st[nst++] = StoreNode::make(C, ctl, zmem, adr, atp,
2649 phase->intcon(con0), T_INT);
2650 }
2651 if (con1 != 0) {
2652 ++new_int;
2653 offset += BytesPerInt;
2654 adr = make_raw_address(offset, phase);
2655 off[nst] = offset;
2656 st[nst++] = StoreNode::make(C, ctl, zmem, adr, atp,
2657 phase->intcon(con1), T_INT);
2658 }
2659 }
2660
2661 // Insert second store first, then the first before the second.
2662 // Insert each one just before any overlapping non-constant stores.
2663 while (nst > 0) {
2664 Node* st1 = st[--nst];
2665 C->copy_node_notes_to(st1, old);
2666 st1 = phase->transform(st1);
2667 offset = off[nst];
2668 assert(offset >= header_size, "do not smash header");
2669 int ins_idx = captured_store_insertion_point(offset, /*size:*/0, phase);
2670 guarantee(ins_idx != 0, "must re-insert constant store");
2671 if (ins_idx < 0) ins_idx = -ins_idx; // never overlap
2672 if (ins_idx > InitializeNode::RawStores && in(ins_idx-1) == zmem)
2673 set_req(--ins_idx, st1);
2674 else
2675 ins_req(ins_idx, st1);
2676 }
2744 // At this point, we may perform additional optimizations.
2745 // Linearize the stores by ascending offset, to make memory
2746 // activity as coherent as possible.
2747 Node* InitializeNode::complete_stores(Node* rawctl, Node* rawmem, Node* rawptr,
2748 intptr_t header_size,
2749 Node* size_in_bytes,
2750 PhaseGVN* phase) {
2751 assert(!is_complete(), "not already complete");
2752 assert(stores_are_sane(phase), "");
2753 assert(allocation() != NULL, "must be present");
2754
2755 remove_extra_zeroes();
2756
2757 if (ReduceFieldZeroing || ReduceBulkZeroing)
2758 // reduce instruction count for common initialization patterns
2759 coalesce_subword_stores(header_size, size_in_bytes, phase);
2760
2761 Node* zmem = zero_memory(); // initially zero memory state
2762 Node* inits = zmem; // accumulating a linearized chain of inits
2763 #ifdef ASSERT
2764 intptr_t last_init_off = sizeof(oopDesc); // previous init offset
2765 intptr_t last_init_end = sizeof(oopDesc); // previous init offset+size
2766 intptr_t last_tile_end = sizeof(oopDesc); // previous tile offset+size
2767 #endif
2768 intptr_t zeroes_done = header_size;
2769
2770 bool do_zeroing = true; // we might give up if inits are very sparse
2771 int big_init_gaps = 0; // how many large gaps have we seen?
2772
2773 if (ZeroTLAB) do_zeroing = false;
2774 if (!ReduceFieldZeroing && !ReduceBulkZeroing) do_zeroing = false;
2775
2776 for (uint i = InitializeNode::RawStores, limit = req(); i < limit; i++) {
2777 Node* st = in(i);
2778 intptr_t st_off = get_store_offset(st, phase);
2779 if (st_off < 0)
2780 break; // unknown junk in the inits
2781 if (st->in(MemNode::Memory) != zmem)
2782 break; // complicated store chains somehow in list
2783
2784 int st_size = st->as_Store()->memory_size();
2785 intptr_t next_init_off = st_off + st_size;
2786
2881 Node* klass_node = allocation()->in(AllocateNode::KlassNode);
2882 ciKlass* k = phase->type(klass_node)->is_klassptr()->klass();
2883 if (zeroes_done == k->layout_helper())
2884 zeroes_done = size_limit;
2885 }
2886 if (zeroes_done < size_limit) {
2887 rawmem = ClearArrayNode::clear_memory(rawctl, rawmem, rawptr,
2888 zeroes_done, size_in_bytes, phase);
2889 }
2890 }
2891
2892 set_complete(phase);
2893 return rawmem;
2894 }
2895
2896
2897 #ifdef ASSERT
2898 bool InitializeNode::stores_are_sane(PhaseTransform* phase) {
2899 if (is_complete())
2900 return true; // stores could be anything at this point
2901 intptr_t last_off = sizeof(oopDesc);
2902 for (uint i = InitializeNode::RawStores; i < req(); i++) {
2903 Node* st = in(i);
2904 intptr_t st_off = get_store_offset(st, phase);
2905 if (st_off < 0) continue; // ignore dead garbage
2906 if (last_off > st_off) {
2907 tty->print_cr("*** bad store offset at %d: %d > %d", i, last_off, st_off);
2908 this->dump(2);
2909 assert(false, "ascending store offsets");
2910 return false;
2911 }
2912 last_off = st_off + st->as_Store()->memory_size();
2913 }
2914 return true;
2915 }
2916 #endif //ASSERT
2917
2918
2919
2920
2921 //============================MergeMemNode=====================================
3236 if( in(i) != empty_mem ) { set_req(i, empty_mem); }
3237 }
3238 }
3239 }
3240
3241 if( !progress && base_memory()->is_Phi() && can_reshape ) {
3242 // Check if PhiNode::Ideal's "Split phis through memory merges"
3243 // transform should be attempted. Look for this->phi->this cycle.
3244 uint merge_width = req();
3245 if (merge_width > Compile::AliasIdxRaw) {
3246 PhiNode* phi = base_memory()->as_Phi();
3247 for( uint i = 1; i < phi->req(); ++i ) {// For all paths in
3248 if (phi->in(i) == this) {
3249 phase->is_IterGVN()->_worklist.push(phi);
3250 break;
3251 }
3252 }
3253 }
3254 }
3255
3256 assert(verify_sparse(), "please, no dups of base");
3257 return progress;
3258 }
3259
3260 //-------------------------set_base_memory-------------------------------------
3261 void MergeMemNode::set_base_memory(Node *new_base) {
3262 Node* empty_mem = empty_memory();
3263 set_req(Compile::AliasIdxBot, new_base);
3264 assert(memory_at(req()) == new_base, "must set default memory");
3265 // Clear out other occurrences of new_base:
3266 if (new_base != empty_mem) {
3267 for (uint i = Compile::AliasIdxRaw; i < req(); i++) {
3268 if (in(i) == new_base) set_req(i, empty_mem);
3269 }
3270 }
3271 }
3272
3273 //------------------------------out_RegMask------------------------------------
3274 const RegMask &MergeMemNode::out_RegMask() const {
3275 return RegMask::Empty;
3276 }
|
15 * version 2 for more details (a copy is included in the LICENSE file that
16 * accompanied this code).
17 *
18 * You should have received a copy of the GNU General Public License version
19 * 2 along with this work; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
21 *
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
24 * have any questions.
25 *
26 */
27
28 // Portions of code courtesy of Clifford Click
29
30 // Optimization - Graph Style
31
32 #include "incls/_precompiled.incl"
33 #include "incls/_memnode.cpp.incl"
34
35 static Node *step_through_mergemem(PhaseGVN *phase, MergeMemNode *mmem, const TypePtr *tp, const TypePtr *adr_check, outputStream *st);
36
37 //=============================================================================
38 uint MemNode::size_of() const { return sizeof(*this); }
39
40 const TypePtr *MemNode::adr_type() const {
41 Node* adr = in(Address);
42 const TypePtr* cross_check = NULL;
43 DEBUG_ONLY(cross_check = _adr_type);
44 return calculate_adr_type(adr->bottom_type(), cross_check);
45 }
46
47 #ifndef PRODUCT
48 void MemNode::dump_spec(outputStream *st) const {
49 if (in(Address) == NULL) return; // node is dead
50 #ifndef ASSERT
51 // fake the missing field
52 const TypePtr* _adr_type = NULL;
53 if (in(Address) != NULL)
54 _adr_type = in(Address)->bottom_type()->isa_ptr();
55 #endif
56 dump_adr_type(this, _adr_type, st);
75 st->print(", idx=Bot;");
76 else if (atp->index() == Compile::AliasIdxTop)
77 st->print(", idx=Top;");
78 else if (atp->index() == Compile::AliasIdxRaw)
79 st->print(", idx=Raw;");
80 else {
81 ciField* field = atp->field();
82 if (field) {
83 st->print(", name=");
84 field->print_name_on(st);
85 }
86 st->print(", idx=%d;", atp->index());
87 }
88 }
89 }
90
91 extern void print_alias_types();
92
93 #endif
94
95 Node *MemNode::optimize_simple_memory_chain(Node *mchain, const TypePtr *t_adr, PhaseGVN *phase) {
96 const TypeOopPtr *tinst = t_adr->isa_oopptr();
97 if (tinst == NULL || !tinst->is_known_instance_field())
98 return mchain; // don't try to optimize non-instance types
99 uint instance_id = tinst->instance_id();
100 Node *start_mem = phase->C->start()->proj_out(TypeFunc::Memory);
101 Node *prev = NULL;
102 Node *result = mchain;
103 while (prev != result) {
104 prev = result;
105 if (result == start_mem)
106 break; // hit one of our sentinals
107 // skip over a call which does not affect this memory slice
108 if (result->is_Proj() && result->as_Proj()->_con == TypeFunc::Memory) {
109 Node *proj_in = result->in(0);
110 if (proj_in->is_Allocate() && proj_in->_idx == instance_id) {
111 break; // hit one of our sentinals
112 } else if (proj_in->is_Call()) {
113 CallNode *call = proj_in->as_Call();
114 if (!call->may_modify(t_adr, phase)) {
115 result = call->in(TypeFunc::Memory);
116 }
117 } else if (proj_in->is_Initialize()) {
118 AllocateNode* alloc = proj_in->as_Initialize()->allocation();
119 // Stop if this is the initialization for the object instance which
120 // which contains this memory slice, otherwise skip over it.
121 if (alloc != NULL && alloc->_idx != instance_id) {
122 result = proj_in->in(TypeFunc::Memory);
123 }
124 } else if (proj_in->is_MemBar()) {
125 result = proj_in->in(TypeFunc::Memory);
126 } else {
127 assert(false, "unexpected projection");
128 }
129 } else if (result->is_MergeMem()) {
130 result = step_through_mergemem(phase, result->as_MergeMem(), t_adr, NULL, tty);
131 }
132 }
133 return result;
134 }
135
136 Node *MemNode::optimize_memory_chain(Node *mchain, const TypePtr *t_adr, PhaseGVN *phase) {
137 const TypeOopPtr *t_oop = t_adr->isa_oopptr();
138 bool is_instance = (t_oop != NULL) && t_oop->is_known_instance_field();
139 PhaseIterGVN *igvn = phase->is_IterGVN();
140 Node *result = mchain;
141 result = optimize_simple_memory_chain(result, t_adr, phase);
142 if (is_instance && igvn != NULL && result->is_Phi()) {
143 PhiNode *mphi = result->as_Phi();
144 assert(mphi->bottom_type() == Type::MEMORY, "memory phi required");
145 const TypePtr *t = mphi->adr_type();
146 if (t == TypePtr::BOTTOM || t == TypeRawPtr::BOTTOM ||
147 t->isa_oopptr() && !t->is_oopptr()->is_known_instance() &&
148 t->is_oopptr()->cast_to_exactness(true)
149 ->is_oopptr()->cast_to_ptr_type(t_oop->ptr())
150 ->is_oopptr()->cast_to_instance_id(t_oop->instance_id()) == t_oop) {
151 // clone the Phi with our address type
152 result = mphi->split_out_instance(t_adr, igvn);
153 } else {
154 assert(phase->C->get_alias_index(t) == phase->C->get_alias_index(t_adr), "correct memory chain");
155 }
156 }
157 return result;
158 }
159
160 static Node *step_through_mergemem(PhaseGVN *phase, MergeMemNode *mmem, const TypePtr *tp, const TypePtr *adr_check, outputStream *st) {
161 uint alias_idx = phase->C->get_alias_index(tp);
162 Node *mem = mmem;
163 #ifdef ASSERT
164 {
165 // Check that current type is consistent with the alias index used during graph construction
166 assert(alias_idx >= Compile::AliasIdxRaw, "must not be a bad alias_idx");
167 bool consistent = adr_check == NULL || adr_check->empty() ||
168 phase->C->must_alias(adr_check, alias_idx );
169 // Sometimes dead array references collapse to a[-1], a[-2], or a[-3]
170 if( !consistent && adr_check != NULL && !adr_check->empty() &&
171 tp->isa_aryptr() && tp->offset() == Type::OffsetBot &&
172 adr_check->isa_aryptr() && adr_check->offset() != Type::OffsetBot &&
173 ( adr_check->offset() == arrayOopDesc::length_offset_in_bytes() ||
174 adr_check->offset() == oopDesc::klass_offset_in_bytes() ||
175 adr_check->offset() == oopDesc::mark_offset_in_bytes() ) ) {
176 // don't assert if it is dead code.
177 consistent = true;
178 }
179 if( !consistent ) {
180 st->print("alias_idx==%d, adr_check==", alias_idx);
181 if( adr_check == NULL ) {
182 st->print("NULL");
183 } else {
184 adr_check->dump();
185 }
186 st->cr();
187 print_alias_types();
188 assert(consistent, "adr_check must match alias idx");
189 }
190 }
191 #endif
192 // TypeInstPtr::NOTNULL+any is an OOP with unknown offset - generally
193 // means an array I have not precisely typed yet. Do not do any
194 // alias stuff with it any time soon.
195 const TypeOopPtr *tinst = tp->isa_oopptr();
196 if( tp->base() != Type::AnyPtr &&
197 !(tinst &&
198 tinst->klass()->is_java_lang_Object() &&
199 tinst->offset() == Type::OffsetBot) ) {
200 // compress paths and change unreachable cycles to TOP
201 // If not, we can update the input infinitely along a MergeMem cycle
202 // Equivalent code in PhiNode::Ideal
203 Node* m = phase->transform(mmem);
204 // If tranformed to a MergeMem, get the desired slice
205 // Otherwise the returned node represents memory for every slice
206 mem = (m->is_MergeMem())? m->as_MergeMem()->memory_at(alias_idx) : m;
207 // Update input if it is progress over what we have now
208 }
209 return mem;
210 }
211
212 //--------------------------Ideal_common---------------------------------------
213 // Look for degenerate control and memory inputs. Bypass MergeMem inputs.
214 // Unhook non-raw memories from complete (macro-expanded) initializations.
215 Node *MemNode::Ideal_common(PhaseGVN *phase, bool can_reshape) {
216 // If our control input is a dead region, kill all below the region
217 Node *ctl = in(MemNode::Control);
218 if (ctl && remove_dead_region(phase, can_reshape))
219 return this;
220 ctl = in(MemNode::Control);
221 // Don't bother trying to transform a dead node
222 if( ctl && ctl->is_top() ) return NodeSentinel;
223
224 // Ignore if memory is dead, or self-loop
225 Node *mem = in(MemNode::Memory);
226 if( phase->type( mem ) == Type::TOP ) return NodeSentinel; // caller will return NULL
227 assert( mem != this, "dead loop in MemNode::Ideal" );
228
229 Node *address = in(MemNode::Address);
230 const Type *t_adr = phase->type( address );
231 if( t_adr == Type::TOP ) return NodeSentinel; // caller will return NULL
232
233 PhaseIterGVN *igvn = phase->is_IterGVN();
234 if( can_reshape && igvn != NULL && igvn->_worklist.member(address) ) {
235 // The address's base and type may change when the address is processed.
236 // Delay this mem node transformation until the address is processed.
237 phase->is_IterGVN()->_worklist.push(this);
238 return NodeSentinel; // caller will return NULL
239 }
240
241 // Avoid independent memory operations
242 Node* old_mem = mem;
243
244 // The code which unhooks non-raw memories from complete (macro-expanded)
245 // initializations was removed. After macro-expansion all stores catched
246 // by Initialize node became raw stores and there is no information
247 // which memory slices they modify. So it is unsafe to move any memory
248 // operation above these stores. Also in most cases hooked non-raw memories
249 // were already unhooked by using information from detect_ptr_independence()
250 // and find_previous_store().
251
252 if (mem->is_MergeMem()) {
253 MergeMemNode* mmem = mem->as_MergeMem();
254 const TypePtr *tp = t_adr->is_ptr();
255
256 mem = step_through_mergemem(phase, mmem, tp, adr_type(), tty);
257 }
258
259 if (mem != old_mem) {
260 set_req(MemNode::Memory, mem);
261 if (phase->type( mem ) == Type::TOP) return NodeSentinel;
262 return this;
263 }
264
265 // let the subclass continue analyzing...
266 return NULL;
267 }
268
269 // Helper function for proving some simple control dominations.
270 // Attempt to prove that all control inputs of 'dom' dominate 'sub'.
271 // Already assumes that 'dom' is available at 'sub', and that 'sub'
272 // is not a constant (dominated by the method's StartNode).
273 // Used by MemNode::find_previous_store to prove that the
274 // control input of a memory operation predates (dominates)
275 // an allocation it wants to look past.
276 bool MemNode::all_controls_dominate(Node* dom, Node* sub) {
277 if (dom == NULL || dom->is_top() || sub == NULL || sub->is_top())
278 return false; // Conservative answer for dead code
279
280 // Check 'dom'. Skip Proj and CatchProj nodes.
281 dom = dom->find_exact_control(dom);
282 if (dom == NULL || dom->is_top())
283 return false; // Conservative answer for dead code
284
285 if (dom == sub) {
286 // For the case when, for example, 'sub' is Initialize and the original
287 // 'dom' is Proj node of the 'sub'.
288 return false;
289 }
290
291 if (dom->is_Con() || dom->is_Start() || dom->is_Root() || dom == sub)
292 return true;
293
294 // 'dom' dominates 'sub' if its control edge and control edges
295 // of all its inputs dominate or equal to sub's control edge.
296
297 // Currently 'sub' is either Allocate, Initialize or Start nodes.
298 // Or Region for the check in LoadNode::Ideal();
299 // 'sub' should have sub->in(0) != NULL.
300 assert(sub->is_Allocate() || sub->is_Initialize() || sub->is_Start() ||
301 sub->is_Region(), "expecting only these nodes");
302
303 // Get control edge of 'sub'.
304 Node* orig_sub = sub;
305 sub = sub->find_exact_control(sub->in(0));
306 if (sub == NULL || sub->is_top())
307 return false; // Conservative answer for dead code
308
309 assert(sub->is_CFG(), "expecting control");
310
311 if (sub == dom)
312 return true;
313
314 if (sub->is_Start() || sub->is_Root())
315 return false;
316
317 {
318 // Check all control edges of 'dom'.
319
320 ResourceMark rm;
321 Arena* arena = Thread::current()->resource_area();
322 Node_List nlist(arena);
323 Unique_Node_List dom_list(arena);
324
325 dom_list.push(dom);
326 bool only_dominating_controls = false;
327
328 for (uint next = 0; next < dom_list.size(); next++) {
329 Node* n = dom_list.at(next);
330 if (n == orig_sub)
331 return false; // One of dom's inputs dominated by sub.
332 if (!n->is_CFG() && n->pinned()) {
333 // Check only own control edge for pinned non-control nodes.
334 n = n->find_exact_control(n->in(0));
335 if (n == NULL || n->is_top())
336 return false; // Conservative answer for dead code
337 assert(n->is_CFG(), "expecting control");
338 dom_list.push(n);
339 } else if (n->is_Con() || n->is_Start() || n->is_Root()) {
340 only_dominating_controls = true;
341 } else if (n->is_CFG()) {
342 if (n->dominates(sub, nlist))
343 only_dominating_controls = true;
344 else
345 return false;
346 } else {
347 // First, own control edge.
348 Node* m = n->find_exact_control(n->in(0));
349 if (m != NULL) {
350 if (m->is_top())
351 return false; // Conservative answer for dead code
352 dom_list.push(m);
353 }
354 // Now, the rest of edges.
355 uint cnt = n->req();
356 for (uint i = 1; i < cnt; i++) {
357 m = n->find_exact_control(n->in(i));
358 if (m == NULL || m->is_top())
359 continue;
360 dom_list.push(m);
361 }
362 }
363 }
364 return only_dominating_controls;
365 }
366 }
367
368 //---------------------detect_ptr_independence---------------------------------
369 // Used by MemNode::find_previous_store to prove that two base
370 // pointers are never equal.
371 // The pointers are accompanied by their associated allocations,
372 // if any, which have been previously discovered by the caller.
373 bool MemNode::detect_ptr_independence(Node* p1, AllocateNode* a1,
374 Node* p2, AllocateNode* a2,
375 PhaseTransform* phase) {
376 // Attempt to prove that these two pointers cannot be aliased.
377 // They may both manifestly be allocations, and they should differ.
378 // Or, if they are not both allocations, they can be distinct constants.
379 // Otherwise, one is an allocation and the other a pre-existing value.
380 if (a1 == NULL && a2 == NULL) { // neither an allocation
381 return (p1 != p2) && p1->is_Con() && p2->is_Con();
382 } else if (a1 != NULL && a2 != NULL) { // both allocations
383 return (a1 != a2);
384 } else if (a1 != NULL) { // one allocation a1
385 // (Note: p2->is_Con implies p2->in(0)->is_Root, which dominates.)
386 return all_controls_dominate(p2, a1);
387 } else { //(a2 != NULL) // one allocation a2
388 return all_controls_dominate(p1, a2);
389 }
390 return false;
391 }
392
393
394 // The logic for reordering loads and stores uses four steps:
395 // (a) Walk carefully past stores and initializations which we
396 // can prove are independent of this load.
397 // (b) Observe that the next memory state makes an exact match
398 // with self (load or store), and locate the relevant store.
399 // (c) Ensure that, if we were to wire self directly to the store,
400 // the optimizer would fold it up somehow.
401 // (d) Do the rewiring, and return, depending on some other part of
402 // the optimizer to fold up the load.
403 // This routine handles steps (a) and (b). Steps (c) and (d) are
404 // specific to loads and stores, so they are handled by the callers.
405 // (Currently, only LoadNode::Ideal has steps (c), (d). More later.)
406 //
407 Node* MemNode::find_previous_store(PhaseTransform* phase) {
408 Node* ctrl = in(MemNode::Control);
409 Node* adr = in(MemNode::Address);
410 intptr_t offset = 0;
411 Node* base = AddPNode::Ideal_base_and_offset(adr, phase, offset);
412 AllocateNode* alloc = AllocateNode::Ideal_allocation(base, phase);
413
414 if (offset == Type::OffsetBot)
415 return NULL; // cannot unalias unless there are precise offsets
416
417 const TypeOopPtr *addr_t = adr->bottom_type()->isa_oopptr();
418
419 intptr_t size_in_bytes = memory_size();
420
421 Node* mem = in(MemNode::Memory); // start searching here...
422
423 int cnt = 50; // Cycle limiter
424 for (;;) { // While we can dance past unrelated stores...
425 if (--cnt < 0) break; // Caught in cycle or a complicated dance?
426
427 if (mem->is_Store()) {
428 Node* st_adr = mem->in(MemNode::Address);
429 intptr_t st_offset = 0;
430 Node* st_base = AddPNode::Ideal_base_and_offset(st_adr, phase, st_offset);
431 if (st_base == NULL)
432 break; // inscrutable pointer
433 if (st_offset != offset && st_offset != Type::OffsetBot) {
434 const int MAX_STORE = BytesPerLong;
435 if (st_offset >= offset + size_in_bytes ||
436 st_offset <= offset - MAX_STORE ||
437 st_offset <= offset - mem->as_Store()->memory_size()) {
438 // Success: The offsets are provably independent.
454 continue; // (a) advance through independent store memory
455 }
456
457 // (b) At this point, if the bases or offsets do not agree, we lose,
458 // since we have not managed to prove 'this' and 'mem' independent.
459 if (st_base == base && st_offset == offset) {
460 return mem; // let caller handle steps (c), (d)
461 }
462
463 } else if (mem->is_Proj() && mem->in(0)->is_Initialize()) {
464 InitializeNode* st_init = mem->in(0)->as_Initialize();
465 AllocateNode* st_alloc = st_init->allocation();
466 if (st_alloc == NULL)
467 break; // something degenerated
468 bool known_identical = false;
469 bool known_independent = false;
470 if (alloc == st_alloc)
471 known_identical = true;
472 else if (alloc != NULL)
473 known_independent = true;
474 else if (all_controls_dominate(this, st_alloc))
475 known_independent = true;
476
477 if (known_independent) {
478 // The bases are provably independent: Either they are
479 // manifestly distinct allocations, or else the control
480 // of this load dominates the store's allocation.
481 int alias_idx = phase->C->get_alias_index(adr_type());
482 if (alias_idx == Compile::AliasIdxRaw) {
483 mem = st_alloc->in(TypeFunc::Memory);
484 } else {
485 mem = st_init->memory(alias_idx);
486 }
487 continue; // (a) advance through independent store memory
488 }
489
490 // (b) at this point, if we are not looking at a store initializing
491 // the same allocation we are loading from, we lose.
492 if (known_identical) {
493 // From caller, can_see_stored_value will consult find_captured_store.
494 return mem; // let caller handle steps (c), (d)
495 }
496
497 } else if (addr_t != NULL && addr_t->is_known_instance_field()) {
498 // Can't use optimize_simple_memory_chain() since it needs PhaseGVN.
499 if (mem->is_Proj() && mem->in(0)->is_Call()) {
500 CallNode *call = mem->in(0)->as_Call();
501 if (!call->may_modify(addr_t, phase)) {
502 mem = call->in(TypeFunc::Memory);
503 continue; // (a) advance through independent call memory
504 }
505 } else if (mem->is_Proj() && mem->in(0)->is_MemBar()) {
506 mem = mem->in(0)->in(TypeFunc::Memory);
507 continue; // (a) advance through independent MemBar memory
508 } else if (mem->is_MergeMem()) {
509 int alias_idx = phase->C->get_alias_index(adr_type());
510 mem = mem->as_MergeMem()->memory_at(alias_idx);
511 continue; // (a) advance through independent MergeMem memory
512 }
513 }
514
515 // Unless there is an explicit 'continue', we must bail out here,
516 // because 'mem' is an inscrutable memory state (e.g., a call).
517 break;
518 }
519
520 return NULL; // bail out
521 }
522
523 //----------------------calculate_adr_type-------------------------------------
524 // Helper function. Notices when the given type of address hits top or bottom.
525 // Also, asserts a cross-check of the type against the expected address type.
526 const TypePtr* MemNode::calculate_adr_type(const Type* t, const TypePtr* cross_check) {
527 if (t == Type::TOP) return NULL; // does not touch memory any more?
528 #ifdef PRODUCT
529 cross_check = NULL;
530 #else
531 if (!VerifyAliases || is_error_reported() || Node::in_dump()) cross_check = NULL;
532 #endif
597 if( n->is_ConstraintCast() ){
598 worklist.push(n->in(1));
599 } else if( n->is_Phi() ) {
600 for( uint i = 1; i < n->req(); i++ ) {
601 worklist.push(n->in(i));
602 }
603 } else {
604 return false;
605 }
606 }
607 }
608
609 // Quit when the worklist is empty, and we've found no offending nodes.
610 return true;
611 }
612
613 //------------------------------Ideal_DU_postCCP-------------------------------
614 // Find any cast-away of null-ness and keep its control. Null cast-aways are
615 // going away in this pass and we need to make this memory op depend on the
616 // gating null check.
617 Node *MemNode::Ideal_DU_postCCP( PhaseCCP *ccp ) {
618 return Ideal_common_DU_postCCP(ccp, this, in(MemNode::Address));
619 }
620
621 // I tried to leave the CastPP's in. This makes the graph more accurate in
622 // some sense; we get to keep around the knowledge that an oop is not-null
623 // after some test. Alas, the CastPP's interfere with GVN (some values are
624 // the regular oop, some are the CastPP of the oop, all merge at Phi's which
625 // cannot collapse, etc). This cost us 10% on SpecJVM, even when I removed
626 // some of the more trivial cases in the optimizer. Removing more useless
627 // Phi's started allowing Loads to illegally float above null checks. I gave
628 // up on this approach. CNC 10/20/2000
629 // This static method may be called not from MemNode (EncodePNode calls it).
630 // Only the control edge of the node 'n' might be updated.
631 Node *MemNode::Ideal_common_DU_postCCP( PhaseCCP *ccp, Node* n, Node* adr ) {
632 Node *skipped_cast = NULL;
633 // Need a null check? Regular static accesses do not because they are
634 // from constant addresses. Array ops are gated by the range check (which
635 // always includes a NULL check). Just check field ops.
636 if( n->in(MemNode::Control) == NULL ) {
637 // Scan upwards for the highest location we can place this memory op.
638 while( true ) {
639 switch( adr->Opcode() ) {
640
641 case Op_AddP: // No change to NULL-ness, so peek thru AddP's
642 adr = adr->in(AddPNode::Base);
643 continue;
644
645 case Op_DecodeN: // No change to NULL-ness, so peek thru
646 adr = adr->in(1);
647 continue;
648
649 case Op_CastPP:
650 // If the CastPP is useless, just peek on through it.
651 if( ccp->type(adr) == ccp->type(adr->in(1)) ) {
652 // Remember the cast that we've peeked though. If we peek
653 // through more than one, then we end up remembering the highest
654 // one, that is, if in a loop, the one closest to the top.
655 skipped_cast = adr;
656 adr = adr->in(1);
657 continue;
658 }
659 // CastPP is going away in this pass! We need this memory op to be
660 // control-dependent on the test that is guarding the CastPP.
661 ccp->hash_delete(n);
662 n->set_req(MemNode::Control, adr->in(0));
663 ccp->hash_insert(n);
664 return n;
665
666 case Op_Phi:
667 // Attempt to float above a Phi to some dominating point.
668 if (adr->in(0) != NULL && adr->in(0)->is_CountedLoop()) {
669 // If we've already peeked through a Cast (which could have set the
670 // control), we can't float above a Phi, because the skipped Cast
671 // may not be loop invariant.
672 if (adr_phi_is_loop_invariant(adr, skipped_cast)) {
673 adr = adr->in(1);
674 continue;
675 }
676 }
677
678 // Intentional fallthrough!
679
680 // No obvious dominating point. The mem op is pinned below the Phi
681 // by the Phi itself. If the Phi goes away (no true value is merged)
682 // then the mem op can float, but not indefinitely. It must be pinned
683 // behind the controls leading to the Phi.
684 case Op_CheckCastPP:
685 // These usually stick around to change address type, however a
686 // useless one can be elided and we still need to pick up a control edge
687 if (adr->in(0) == NULL) {
688 // This CheckCastPP node has NO control and is likely useless. But we
689 // need check further up the ancestor chain for a control input to keep
690 // the node in place. 4959717.
691 skipped_cast = adr;
692 adr = adr->in(1);
693 continue;
694 }
695 ccp->hash_delete(n);
696 n->set_req(MemNode::Control, adr->in(0));
697 ccp->hash_insert(n);
698 return n;
699
700 // List of "safe" opcodes; those that implicitly block the memory
701 // op below any null check.
702 case Op_CastX2P: // no null checks on native pointers
703 case Op_Parm: // 'this' pointer is not null
704 case Op_LoadP: // Loading from within a klass
705 case Op_LoadN: // Loading from within a klass
706 case Op_LoadKlass: // Loading from within a klass
707 case Op_LoadNKlass: // Loading from within a klass
708 case Op_ConP: // Loading from a klass
709 case Op_ConN: // Loading from a klass
710 case Op_CreateEx: // Sucking up the guts of an exception oop
711 case Op_Con: // Reading from TLS
712 case Op_CMoveP: // CMoveP is pinned
713 case Op_CMoveN: // CMoveN is pinned
714 break; // No progress
715
716 case Op_Proj: // Direct call to an allocation routine
717 case Op_SCMemProj: // Memory state from store conditional ops
718 #ifdef ASSERT
719 {
720 assert(adr->as_Proj()->_con == TypeFunc::Parms, "must be return value");
721 const Node* call = adr->in(0);
722 if (call->is_CallJava()) {
723 const CallJavaNode* call_java = call->as_CallJava();
724 const TypeTuple *r = call_java->tf()->range();
725 assert(r->cnt() > TypeFunc::Parms, "must return value");
726 const Type* ret_type = r->field_at(TypeFunc::Parms);
727 assert(ret_type && ret_type->isa_ptr(), "must return pointer");
728 // We further presume that this is one of
729 // new_instance_Java, new_array_Java, or
730 // the like, but do not assert for this.
731 } else if (call->is_Allocate()) {
732 // similar case to new_instance_Java, etc.
733 } else if (!call->is_CallLeaf()) {
734 // Projections from fetch_oop (OSR) are allowed as well.
735 ShouldNotReachHere();
736 }
737 }
738 #endif
739 break;
740 default:
741 ShouldNotReachHere();
742 }
743 break;
744 }
745 }
746
747 return NULL; // No progress
753 uint LoadNode::cmp( const Node &n ) const
754 { return !Type::cmp( _type, ((LoadNode&)n)._type ); }
755 const Type *LoadNode::bottom_type() const { return _type; }
756 uint LoadNode::ideal_reg() const {
757 return Matcher::base2reg[_type->base()];
758 }
759
760 #ifndef PRODUCT
761 void LoadNode::dump_spec(outputStream *st) const {
762 MemNode::dump_spec(st);
763 if( !Verbose && !WizardMode ) {
764 // standard dump does this in Verbose and WizardMode
765 st->print(" #"); _type->dump_on(st);
766 }
767 }
768 #endif
769
770
771 //----------------------------LoadNode::make-----------------------------------
772 // Polymorphic factory method:
773 Node *LoadNode::make( PhaseGVN& gvn, Node *ctl, Node *mem, Node *adr, const TypePtr* adr_type, const Type *rt, BasicType bt ) {
774 Compile* C = gvn.C;
775
776 // sanity check the alias category against the created node type
777 assert(!(adr_type->isa_oopptr() &&
778 adr_type->offset() == oopDesc::klass_offset_in_bytes()),
779 "use LoadKlassNode instead");
780 assert(!(adr_type->isa_aryptr() &&
781 adr_type->offset() == arrayOopDesc::length_offset_in_bytes()),
782 "use LoadRangeNode instead");
783 switch (bt) {
784 case T_BOOLEAN:
785 case T_BYTE: return new (C, 3) LoadBNode(ctl, mem, adr, adr_type, rt->is_int() );
786 case T_INT: return new (C, 3) LoadINode(ctl, mem, adr, adr_type, rt->is_int() );
787 case T_CHAR: return new (C, 3) LoadCNode(ctl, mem, adr, adr_type, rt->is_int() );
788 case T_SHORT: return new (C, 3) LoadSNode(ctl, mem, adr, adr_type, rt->is_int() );
789 case T_LONG: return new (C, 3) LoadLNode(ctl, mem, adr, adr_type, rt->is_long() );
790 case T_FLOAT: return new (C, 3) LoadFNode(ctl, mem, adr, adr_type, rt );
791 case T_DOUBLE: return new (C, 3) LoadDNode(ctl, mem, adr, adr_type, rt );
792 case T_ADDRESS: return new (C, 3) LoadPNode(ctl, mem, adr, adr_type, rt->is_ptr() );
793 case T_OBJECT:
794 #ifdef _LP64
795 if (adr->bottom_type()->is_ptr_to_narrowoop()) {
796 Node* load = gvn.transform(new (C, 3) LoadNNode(ctl, mem, adr, adr_type, rt->make_narrowoop()));
797 return new (C, 2) DecodeNNode(load, load->bottom_type()->make_ptr());
798 } else
799 #endif
800 {
801 assert(!adr->bottom_type()->is_ptr_to_narrowoop(), "should have got back a narrow oop");
802 return new (C, 3) LoadPNode(ctl, mem, adr, adr_type, rt->is_oopptr());
803 }
804 }
805 ShouldNotReachHere();
806 return (LoadNode*)NULL;
807 }
808
809 LoadLNode* LoadLNode::make_atomic(Compile *C, Node* ctl, Node* mem, Node* adr, const TypePtr* adr_type, const Type* rt) {
810 bool require_atomic = true;
811 return new (C, 3) LoadLNode(ctl, mem, adr, adr_type, rt->is_long(), require_atomic);
812 }
813
814
815
816
817 //------------------------------hash-------------------------------------------
818 uint LoadNode::hash() const {
819 // unroll addition of interesting fields
820 return (uintptr_t)in(Control) + (uintptr_t)in(Memory) + (uintptr_t)in(Address);
821 }
822
823 //---------------------------can_see_stored_value------------------------------
917
918 // A load from an initialization barrier can match a captured store.
919 if (st->is_Proj() && st->in(0)->is_Initialize()) {
920 InitializeNode* init = st->in(0)->as_Initialize();
921 AllocateNode* alloc = init->allocation();
922 if (alloc != NULL &&
923 alloc == AllocateNode::Ideal_allocation(ld_adr, phase, offset)) {
924 // examine a captured store value
925 st = init->find_captured_store(offset, memory_size(), phase);
926 if (st != NULL)
927 continue; // take one more trip around
928 }
929 }
930
931 break;
932 }
933
934 return NULL;
935 }
936
937 //----------------------is_instance_field_load_with_local_phi------------------
938 bool LoadNode::is_instance_field_load_with_local_phi(Node* ctrl) {
939 if( in(MemNode::Memory)->is_Phi() && in(MemNode::Memory)->in(0) == ctrl &&
940 in(MemNode::Address)->is_AddP() ) {
941 const TypeOopPtr* t_oop = in(MemNode::Address)->bottom_type()->isa_oopptr();
942 // Only instances.
943 if( t_oop != NULL && t_oop->is_known_instance_field() &&
944 t_oop->offset() != Type::OffsetBot &&
945 t_oop->offset() != Type::OffsetTop) {
946 return true;
947 }
948 }
949 return false;
950 }
951
952 //------------------------------Identity---------------------------------------
953 // Loads are identity if previous store is to same address
954 Node *LoadNode::Identity( PhaseTransform *phase ) {
955 // If the previous store-maker is the right kind of Store, and the store is
956 // to the same address, then we are equal to the value stored.
957 Node* mem = in(MemNode::Memory);
958 Node* value = can_see_stored_value(mem, phase);
959 if( value ) {
960 // byte, short & char stores truncate naturally.
961 // A load has to load the truncated value which requires
962 // some sort of masking operation and that requires an
963 // Ideal call instead of an Identity call.
964 if (memory_size() < BytesPerInt) {
965 // If the input to the store does not fit with the load's result type,
966 // it must be truncated via an Ideal call.
967 if (!phase->type(value)->higher_equal(phase->type(this)))
968 return this;
969 }
970 // (This works even when value is a Con, but LoadNode::Value
971 // usually runs first, producing the singleton type of the Con.)
972 return value;
973 }
974
975 // Search for an existing data phi which was generated before for the same
976 // instance's field to avoid infinite genertion of phis in a loop.
977 Node *region = mem->in(0);
978 if (is_instance_field_load_with_local_phi(region)) {
979 const TypePtr *addr_t = in(MemNode::Address)->bottom_type()->isa_ptr();
980 int this_index = phase->C->get_alias_index(addr_t);
981 int this_offset = addr_t->offset();
982 int this_id = addr_t->is_oopptr()->instance_id();
983 const Type* this_type = bottom_type();
984 for (DUIterator_Fast imax, i = region->fast_outs(imax); i < imax; i++) {
985 Node* phi = region->fast_out(i);
986 if (phi->is_Phi() && phi != mem &&
987 phi->as_Phi()->is_same_inst_field(this_type, this_id, this_index, this_offset)) {
988 return phi;
989 }
990 }
991 }
992
993 return this;
994 }
995
996
997 // Returns true if the AliasType refers to the field that holds the
998 // cached box array. Currently only handles the IntegerCache case.
999 static bool is_autobox_cache(Compile::AliasType* atp) {
1000 if (atp != NULL && atp->field() != NULL) {
1001 ciField* field = atp->field();
1002 ciSymbol* klass = field->holder()->name();
1003 if (field->name() == ciSymbol::cache_field_name() &&
1004 field->holder()->uses_default_loader() &&
1005 klass == ciSymbol::java_lang_Integer_IntegerCache()) {
1006 return true;
1007 }
1008 }
1009 return false;
1010 }
1011
1012 // Fetch the base value in the autobox array
1052
1053 // We're loading from an object which has autobox behaviour.
1054 // If this object is result of a valueOf call we'll have a phi
1055 // merging a newly allocated object and a load from the cache.
1056 // We want to replace this load with the original incoming
1057 // argument to the valueOf call.
1058 Node* LoadNode::eliminate_autobox(PhaseGVN* phase) {
1059 Node* base = in(Address)->in(AddPNode::Base);
1060 if (base->is_Phi() && base->req() == 3) {
1061 AllocateNode* allocation = NULL;
1062 int allocation_index = -1;
1063 int load_index = -1;
1064 for (uint i = 1; i < base->req(); i++) {
1065 allocation = AllocateNode::Ideal_allocation(base->in(i), phase);
1066 if (allocation != NULL) {
1067 allocation_index = i;
1068 load_index = 3 - allocation_index;
1069 break;
1070 }
1071 }
1072 bool has_load = ( allocation != NULL &&
1073 (base->in(load_index)->is_Load() ||
1074 base->in(load_index)->is_DecodeN() &&
1075 base->in(load_index)->in(1)->is_Load()) );
1076 if (has_load && in(Memory)->is_Phi() && in(Memory)->in(0) == base->in(0)) {
1077 // Push the loads from the phi that comes from valueOf up
1078 // through it to allow elimination of the loads and the recovery
1079 // of the original value.
1080 Node* mem_phi = in(Memory);
1081 Node* offset = in(Address)->in(AddPNode::Offset);
1082 Node* region = base->in(0);
1083
1084 Node* in1 = clone();
1085 Node* in1_addr = in1->in(Address)->clone();
1086 in1_addr->set_req(AddPNode::Base, base->in(allocation_index));
1087 in1_addr->set_req(AddPNode::Address, base->in(allocation_index));
1088 in1_addr->set_req(AddPNode::Offset, offset);
1089 in1->set_req(0, region->in(allocation_index));
1090 in1->set_req(Address, in1_addr);
1091 in1->set_req(Memory, mem_phi->in(allocation_index));
1092
1093 Node* in2 = clone();
1094 Node* in2_addr = in2->in(Address)->clone();
1095 in2_addr->set_req(AddPNode::Base, base->in(load_index));
1096 in2_addr->set_req(AddPNode::Address, base->in(load_index));
1097 in2_addr->set_req(AddPNode::Offset, offset);
1098 in2->set_req(0, region->in(load_index));
1099 in2->set_req(Address, in2_addr);
1100 in2->set_req(Memory, mem_phi->in(load_index));
1101
1102 in1_addr = phase->transform(in1_addr);
1103 in1 = phase->transform(in1);
1104 in2_addr = phase->transform(in2_addr);
1105 in2 = phase->transform(in2);
1106
1107 PhiNode* result = PhiNode::make_blank(region, this);
1108 result->set_req(allocation_index, in1);
1109 result->set_req(load_index, in2);
1110 return result;
1111 }
1112 } else if (base->is_Load() ||
1113 base->is_DecodeN() && base->in(1)->is_Load()) {
1114 if (base->is_DecodeN()) {
1115 // Get LoadN node which loads cached Integer object
1116 base = base->in(1);
1117 }
1118 // Eliminate the load of Integer.value for integers from the cache
1119 // array by deriving the value from the index into the array.
1120 // Capture the offset of the load and then reverse the computation.
1121 Node* load_base = base->in(Address)->in(AddPNode::Base);
1122 if (load_base->is_DecodeN()) {
1123 // Get LoadN node which loads IntegerCache.cache field
1124 load_base = load_base->in(1);
1125 }
1126 if (load_base != NULL) {
1127 Compile::AliasType* atp = phase->C->alias_type(load_base->adr_type());
1128 intptr_t cache_offset;
1129 int shift = -1;
1130 Node* cache = NULL;
1131 if (is_autobox_cache(atp)) {
1132 shift = exact_log2(type2aelembytes(T_OBJECT));
1133 cache = AddPNode::Ideal_base_and_offset(load_base->in(Address), phase, cache_offset);
1134 }
1135 if (cache != NULL && base->in(Address)->is_AddP()) {
1136 Node* elements[4];
1137 int count = base->in(Address)->as_AddP()->unpack_offsets(elements, ARRAY_SIZE(elements));
1138 int cache_low;
1139 if (count > 0 && fetch_autobox_base(atp, cache_low)) {
1140 int offset = arrayOopDesc::base_offset_in_bytes(memory_type()) - (cache_low << shift);
1141 // Add up all the offsets making of the address of the load
1142 Node* result = elements[0];
1143 for (int i = 1; i < count; i++) {
1144 result = phase->transform(new (phase->C, 3) AddXNode(result, elements[i]));
1145 }
1146 // Remove the constant offset from the address and then
1147 // remove the scaling of the offset to recover the original index.
1148 result = phase->transform(new (phase->C, 3) AddXNode(result, phase->MakeConX(-offset)));
1149 if (result->Opcode() == Op_LShiftX && result->in(2) == phase->intcon(shift)) {
1150 // Peel the shift off directly but wrap it in a dummy node
1151 // since Ideal can't return existing nodes
1152 result = new (phase->C, 3) RShiftXNode(result->in(1), phase->intcon(0));
1153 } else {
1154 result = new (phase->C, 3) RShiftXNode(result, phase->intcon(shift));
1155 }
1156 #ifdef _LP64
1157 result = new (phase->C, 2) ConvL2INode(phase->transform(result));
1158 #endif
1159 return result;
1160 }
1161 }
1162 }
1163 }
1164 return NULL;
1165 }
1166
1167 //------------------------------split_through_phi------------------------------
1168 // Split instance field load through Phi.
1169 Node *LoadNode::split_through_phi(PhaseGVN *phase) {
1170 Node* mem = in(MemNode::Memory);
1171 Node* address = in(MemNode::Address);
1172 const TypePtr *addr_t = phase->type(address)->isa_ptr();
1173 const TypeOopPtr *t_oop = addr_t->isa_oopptr();
1174
1175 assert(mem->is_Phi() && (t_oop != NULL) &&
1176 t_oop->is_known_instance_field(), "invalide conditions");
1177
1178 Node *region = mem->in(0);
1179 if (region == NULL) {
1180 return NULL; // Wait stable graph
1181 }
1182 uint cnt = mem->req();
1183 for( uint i = 1; i < cnt; i++ ) {
1184 Node *in = mem->in(i);
1185 if( in == NULL ) {
1186 return NULL; // Wait stable graph
1187 }
1188 }
1189 // Check for loop invariant.
1190 if (cnt == 3) {
1191 for( uint i = 1; i < cnt; i++ ) {
1192 Node *in = mem->in(i);
1193 Node* m = MemNode::optimize_memory_chain(in, addr_t, phase);
1194 if (m == mem) {
1195 set_req(MemNode::Memory, mem->in(cnt - i)); // Skip this phi.
1196 return this;
1197 }
1198 }
1199 }
1200 // Split through Phi (see original code in loopopts.cpp).
1201 assert(phase->C->have_alias_type(addr_t), "instance should have alias type");
1202
1203 // Do nothing here if Identity will find a value
1204 // (to avoid infinite chain of value phis generation).
1205 if ( !phase->eqv(this, this->Identity(phase)) )
1206 return NULL;
1207
1208 // Skip the split if the region dominates some control edge of the address.
1209 if (cnt == 3 && !MemNode::all_controls_dominate(address, region))
1210 return NULL;
1211
1212 const Type* this_type = this->bottom_type();
1213 int this_index = phase->C->get_alias_index(addr_t);
1214 int this_offset = addr_t->offset();
1215 int this_iid = addr_t->is_oopptr()->instance_id();
1216 int wins = 0;
1217 PhaseIterGVN *igvn = phase->is_IterGVN();
1218 Node *phi = new (igvn->C, region->req()) PhiNode(region, this_type, NULL, this_iid, this_index, this_offset);
1219 for( uint i = 1; i < region->req(); i++ ) {
1220 Node *x;
1221 Node* the_clone = NULL;
1222 if( region->in(i) == phase->C->top() ) {
1223 x = phase->C->top(); // Dead path? Use a dead data op
1224 } else {
1225 x = this->clone(); // Else clone up the data op
1226 the_clone = x; // Remember for possible deletion.
1227 // Alter data node to use pre-phi inputs
1228 if( this->in(0) == region ) {
1229 x->set_req( 0, region->in(i) );
1230 } else {
1231 x->set_req( 0, NULL );
1232 }
1233 for( uint j = 1; j < this->req(); j++ ) {
1234 Node *in = this->in(j);
1235 if( in->is_Phi() && in->in(0) == region )
1236 x->set_req( j, in->in(i) ); // Use pre-Phi input for the clone
1237 }
1238 }
1239 // Check for a 'win' on some paths
1240 const Type *t = x->Value(igvn);
1241
1242 bool singleton = t->singleton();
1243
1244 // See comments in PhaseIdealLoop::split_thru_phi().
1245 if( singleton && t == Type::TOP ) {
1246 singleton &= region->is_Loop() && (i != LoopNode::EntryControl);
1247 }
1248
1249 if( singleton ) {
1250 wins++;
1251 x = igvn->makecon(t);
1252 } else {
1253 // We now call Identity to try to simplify the cloned node.
1254 // Note that some Identity methods call phase->type(this).
1255 // Make sure that the type array is big enough for
1256 // our new node, even though we may throw the node away.
1257 // (This tweaking with igvn only works because x is a new node.)
1258 igvn->set_type(x, t);
1259 // If x is a TypeNode, capture any more-precise type permanently into Node
1260 // othewise it will be not updated during igvn->transform since
1261 // igvn->type(x) is set to x->Value() already.
1262 x->raise_bottom_type(t);
1263 Node *y = x->Identity(igvn);
1264 if( y != x ) {
1265 wins++;
1266 x = y;
1267 } else {
1268 y = igvn->hash_find(x);
1269 if( y ) {
1270 wins++;
1271 x = y;
1272 } else {
1273 // Else x is a new node we are keeping
1274 // We do not need register_new_node_with_optimizer
1275 // because set_type has already been called.
1276 igvn->_worklist.push(x);
1277 }
1278 }
1279 }
1280 if (x != the_clone && the_clone != NULL)
1281 igvn->remove_dead_node(the_clone);
1282 phi->set_req(i, x);
1283 }
1284 if( wins > 0 ) {
1285 // Record Phi
1286 igvn->register_new_node_with_optimizer(phi);
1287 return phi;
1288 }
1289 igvn->remove_dead_node(phi);
1290 return NULL;
1291 }
1292
1293 //------------------------------Ideal------------------------------------------
1294 // If the load is from Field memory and the pointer is non-null, we can
1295 // zero out the control input.
1296 // If the offset is constant and the base is an object allocation,
1297 // try to hook me up to the exact initializing store.
1298 Node *LoadNode::Ideal(PhaseGVN *phase, bool can_reshape) {
1299 Node* p = MemNode::Ideal_common(phase, can_reshape);
1300 if (p) return (p == NodeSentinel) ? NULL : p;
1301
1302 Node* ctrl = in(MemNode::Control);
1303 Node* address = in(MemNode::Address);
1304
1305 // Skip up past a SafePoint control. Cannot do this for Stores because
1306 // pointer stores & cardmarks must stay on the same side of a SafePoint.
1307 if( ctrl != NULL && ctrl->Opcode() == Op_SafePoint &&
1308 phase->C->get_alias_index(phase->type(address)->is_ptr()) != Compile::AliasIdxRaw ) {
1309 ctrl = ctrl->in(0);
1310 set_req(MemNode::Control,ctrl);
1311 }
1312
1313 // Check for useless control edge in some common special cases
1314 if (in(MemNode::Control) != NULL) {
1315 intptr_t ignore = 0;
1316 Node* base = AddPNode::Ideal_base_and_offset(address, phase, ignore);
1317 if (base != NULL
1318 && phase->type(base)->higher_equal(TypePtr::NOTNULL)
1319 && all_controls_dominate(base, phase->C->start())) {
1320 // A method-invariant, non-null address (constant or 'this' argument).
1321 set_req(MemNode::Control, NULL);
1322 }
1323 }
1324
1325 if (EliminateAutoBox && can_reshape && in(Address)->is_AddP()) {
1326 Node* base = in(Address)->in(AddPNode::Base);
1327 if (base != NULL) {
1328 Compile::AliasType* atp = phase->C->alias_type(adr_type());
1329 if (is_autobox_object(atp)) {
1330 Node* result = eliminate_autobox(phase);
1331 if (result != NULL) return result;
1332 }
1333 }
1334 }
1335
1336 Node* mem = in(MemNode::Memory);
1337 const TypePtr *addr_t = phase->type(address)->isa_ptr();
1338
1339 if (addr_t != NULL) {
1340 // try to optimize our memory input
1341 Node* opt_mem = MemNode::optimize_memory_chain(mem, addr_t, phase);
1342 if (opt_mem != mem) {
1343 set_req(MemNode::Memory, opt_mem);
1344 if (phase->type( opt_mem ) == Type::TOP) return NULL;
1345 return this;
1346 }
1347 const TypeOopPtr *t_oop = addr_t->isa_oopptr();
1348 if (can_reshape && opt_mem->is_Phi() &&
1349 (t_oop != NULL) && t_oop->is_known_instance_field()) {
1350 // Split instance field load through Phi.
1351 Node* result = split_through_phi(phase);
1352 if (result != NULL) return result;
1353 }
1354 }
1355
1356 // Check for prior store with a different base or offset; make Load
1357 // independent. Skip through any number of them. Bail out if the stores
1358 // are in an endless dead cycle and report no progress. This is a key
1359 // transform for Reflection. However, if after skipping through the Stores
1360 // we can't then fold up against a prior store do NOT do the transform as
1361 // this amounts to using the 'Oracle' model of aliasing. It leaves the same
1362 // array memory alive twice: once for the hoisted Load and again after the
1363 // bypassed Store. This situation only works if EVERYBODY who does
1364 // anti-dependence work knows how to bypass. I.e. we need all
1365 // anti-dependence checks to ask the same Oracle. Right now, that Oracle is
1366 // the alias index stuff. So instead, peek through Stores and IFF we can
1367 // fold up, do so.
1368 Node* prev_mem = find_previous_store(phase);
1369 // Steps (a), (b): Walk past independent stores to find an exact match.
1370 if (prev_mem != NULL && prev_mem != in(MemNode::Memory)) {
1371 // (c) See if we can fold up on the spot, but don't fold up here.
1372 // Fold-up might require truncation (for LoadB/LoadS/LoadC) or
1373 // just return a prior value, which is done by Identity calls.
1374 if (can_see_stored_value(prev_mem, phase)) {
1375 // Make ready for step (d):
1422
1423 // Try to guess loaded type from pointer type
1424 if (tp->base() == Type::AryPtr) {
1425 const Type *t = tp->is_aryptr()->elem();
1426 // Don't do this for integer types. There is only potential profit if
1427 // the element type t is lower than _type; that is, for int types, if _type is
1428 // more restrictive than t. This only happens here if one is short and the other
1429 // char (both 16 bits), and in those cases we've made an intentional decision
1430 // to use one kind of load over the other. See AndINode::Ideal and 4965907.
1431 // Also, do not try to narrow the type for a LoadKlass, regardless of offset.
1432 //
1433 // Yes, it is possible to encounter an expression like (LoadKlass p1:(AddP x x 8))
1434 // where the _gvn.type of the AddP is wider than 8. This occurs when an earlier
1435 // copy p0 of (AddP x x 8) has been proven equal to p1, and the p0 has been
1436 // subsumed by p1. If p1 is on the worklist but has not yet been re-transformed,
1437 // it is possible that p1 will have a type like Foo*[int+]:NotNull*+any.
1438 // In fact, that could have been the original type of p1, and p1 could have
1439 // had an original form like p1:(AddP x x (LShiftL quux 3)), where the
1440 // expression (LShiftL quux 3) independently optimized to the constant 8.
1441 if ((t->isa_int() == NULL) && (t->isa_long() == NULL)
1442 && Opcode() != Op_LoadKlass && Opcode() != Op_LoadNKlass) {
1443 // t might actually be lower than _type, if _type is a unique
1444 // concrete subclass of abstract class t.
1445 // Make sure the reference is not into the header, by comparing
1446 // the offset against the offset of the start of the array's data.
1447 // Different array types begin at slightly different offsets (12 vs. 16).
1448 // We choose T_BYTE as an example base type that is least restrictive
1449 // as to alignment, which will therefore produce the smallest
1450 // possible base offset.
1451 const int min_base_off = arrayOopDesc::base_offset_in_bytes(T_BYTE);
1452 if ((uint)off >= (uint)min_base_off) { // is the offset beyond the header?
1453 const Type* jt = t->join(_type);
1454 // In any case, do not allow the join, per se, to empty out the type.
1455 if (jt->empty() && !t->empty()) {
1456 // This can happen if a interface-typed array narrows to a class type.
1457 jt = _type;
1458 }
1459
1460 if (EliminateAutoBox) {
1461 // The pointers in the autobox arrays are always non-null
1462 Node* base = in(Address)->in(AddPNode::Base);
1563 // Note: When interfaces are reliable, we can narrow the interface
1564 // test to (klass != Serializable && klass != Cloneable).
1565 assert(Opcode() == Op_LoadI, "must load an int from _layout_helper");
1566 jint min_size = Klass::instance_layout_helper(oopDesc::header_size(), false);
1567 // The key property of this type is that it folds up tests
1568 // for array-ness, since it proves that the layout_helper is positive.
1569 // Thus, a generic value like the basic object layout helper works fine.
1570 return TypeInt::make(min_size, max_jint, Type::WidenMin);
1571 }
1572 }
1573
1574 // If we are loading from a freshly-allocated object, produce a zero,
1575 // if the load is provably beyond the header of the object.
1576 // (Also allow a variable load from a fresh array to produce zero.)
1577 if (ReduceFieldZeroing) {
1578 Node* value = can_see_stored_value(mem,phase);
1579 if (value != NULL && value->is_Con())
1580 return value->bottom_type();
1581 }
1582
1583 const TypeOopPtr *tinst = tp->isa_oopptr();
1584 if (tinst != NULL && tinst->is_known_instance_field()) {
1585 // If we have an instance type and our memory input is the
1586 // programs's initial memory state, there is no matching store,
1587 // so just return a zero of the appropriate type
1588 Node *mem = in(MemNode::Memory);
1589 if (mem->is_Parm() && mem->in(0)->is_Start()) {
1590 assert(mem->as_Parm()->_con == TypeFunc::Memory, "must be memory Parm");
1591 return Type::get_zero_type(_type->basic_type());
1592 }
1593 }
1594 return _type;
1595 }
1596
1597 //------------------------------match_edge-------------------------------------
1598 // Do we Match on this edge index or not? Match only the address.
1599 uint LoadNode::match_edge(uint idx) const {
1600 return idx == MemNode::Address;
1601 }
1602
1603 //--------------------------LoadBNode::Ideal--------------------------------------
1604 //
1605 // If the previous store is to the same address as this load,
1606 // and the value stored was larger than a byte, replace this load
1607 // with the value stored truncated to a byte. If no truncation is
1608 // needed, the replacement is done in LoadNode::Identity().
1609 //
1610 Node *LoadBNode::Ideal(PhaseGVN *phase, bool can_reshape) {
1611 Node* mem = in(MemNode::Memory);
1612 Node* value = can_see_stored_value(mem,phase);
1613 if( value && !phase->type(value)->higher_equal( _type ) ) {
1636
1637 //--------------------------LoadSNode::Ideal--------------------------------------
1638 //
1639 // If the previous store is to the same address as this load,
1640 // and the value stored was larger than a short, replace this load
1641 // with the value stored truncated to a short. If no truncation is
1642 // needed, the replacement is done in LoadNode::Identity().
1643 //
1644 Node *LoadSNode::Ideal(PhaseGVN *phase, bool can_reshape) {
1645 Node* mem = in(MemNode::Memory);
1646 Node* value = can_see_stored_value(mem,phase);
1647 if( value && !phase->type(value)->higher_equal( _type ) ) {
1648 Node *result = phase->transform( new (phase->C, 3) LShiftINode(value, phase->intcon(16)) );
1649 return new (phase->C, 3) RShiftINode(result, phase->intcon(16));
1650 }
1651 // Identity call will handle the case where truncation is not needed.
1652 return LoadNode::Ideal(phase, can_reshape);
1653 }
1654
1655 //=============================================================================
1656 //----------------------------LoadKlassNode::make------------------------------
1657 // Polymorphic factory method:
1658 Node *LoadKlassNode::make( PhaseGVN& gvn, Node *mem, Node *adr, const TypePtr* at, const TypeKlassPtr *tk ) {
1659 Compile* C = gvn.C;
1660 Node *ctl = NULL;
1661 // sanity check the alias category against the created node type
1662 const TypeOopPtr *adr_type = adr->bottom_type()->isa_oopptr();
1663 assert(adr_type != NULL, "expecting TypeOopPtr");
1664 #ifdef _LP64
1665 if (adr_type->is_ptr_to_narrowoop()) {
1666 Node* load_klass = gvn.transform(new (C, 3) LoadNKlassNode(ctl, mem, adr, at, tk->make_narrowoop()));
1667 return new (C, 2) DecodeNNode(load_klass, load_klass->bottom_type()->make_ptr());
1668 }
1669 #endif
1670 assert(!adr_type->is_ptr_to_narrowoop(), "should have got back a narrow oop");
1671 return new (C, 3) LoadKlassNode(ctl, mem, adr, at, tk);
1672 }
1673
1674 //------------------------------Value------------------------------------------
1675 const Type *LoadKlassNode::Value( PhaseTransform *phase ) const {
1676 return klass_value_common(phase);
1677 }
1678
1679 const Type *LoadNode::klass_value_common( PhaseTransform *phase ) const {
1680 // Either input is TOP ==> the result is TOP
1681 const Type *t1 = phase->type( in(MemNode::Memory) );
1682 if (t1 == Type::TOP) return Type::TOP;
1683 Node *adr = in(MemNode::Address);
1684 const Type *t2 = phase->type( adr );
1685 if (t2 == Type::TOP) return Type::TOP;
1686 const TypePtr *tp = t2->is_ptr();
1687 if (TypePtr::above_centerline(tp->ptr()) ||
1688 tp->ptr() == TypePtr::Null) return Type::TOP;
1689
1690 // Return a more precise klass, if possible
1691 const TypeInstPtr *tinst = tp->isa_instptr();
1692 if (tinst != NULL) {
1693 ciInstanceKlass* ik = tinst->klass()->as_instance_klass();
1694 int offset = tinst->offset();
1695 if (ik == phase->C->env()->Class_klass()
1696 && (offset == java_lang_Class::klass_offset_in_bytes() ||
1697 offset == java_lang_Class::array_klass_offset_in_bytes())) {
1698 // We are loading a special hidden field from a Class mirror object,
1699 // the field which points to the VM's Klass metaobject.
1791 // according to the element type's subclassing.
1792 return TypeKlassPtr::make(tkls->ptr(), elem, 0/*offset*/);
1793 }
1794 if( klass->is_instance_klass() && tkls->klass_is_exact() &&
1795 (uint)tkls->offset() == Klass::super_offset_in_bytes() + sizeof(oopDesc)) {
1796 ciKlass* sup = klass->as_instance_klass()->super();
1797 // The field is Klass::_super. Return its (constant) value.
1798 // (Folds up the 2nd indirection in aClassConstant.getSuperClass().)
1799 return sup ? TypeKlassPtr::make(sup) : TypePtr::NULL_PTR;
1800 }
1801 }
1802
1803 // Bailout case
1804 return LoadNode::Value(phase);
1805 }
1806
1807 //------------------------------Identity---------------------------------------
1808 // To clean up reflective code, simplify k.java_mirror.as_klass to plain k.
1809 // Also feed through the klass in Allocate(...klass...)._klass.
1810 Node* LoadKlassNode::Identity( PhaseTransform *phase ) {
1811 return klass_identity_common(phase);
1812 }
1813
1814 Node* LoadNode::klass_identity_common(PhaseTransform *phase ) {
1815 Node* x = LoadNode::Identity(phase);
1816 if (x != this) return x;
1817
1818 // Take apart the address into an oop and and offset.
1819 // Return 'this' if we cannot.
1820 Node* adr = in(MemNode::Address);
1821 intptr_t offset = 0;
1822 Node* base = AddPNode::Ideal_base_and_offset(adr, phase, offset);
1823 if (base == NULL) return this;
1824 const TypeOopPtr* toop = phase->type(adr)->isa_oopptr();
1825 if (toop == NULL) return this;
1826
1827 // We can fetch the klass directly through an AllocateNode.
1828 // This works even if the klass is not constant (clone or newArray).
1829 if (offset == oopDesc::klass_offset_in_bytes()) {
1830 Node* allocated_klass = AllocateNode::Ideal_klass(base, phase);
1831 if (allocated_klass != NULL) {
1832 return allocated_klass;
1833 }
1834 }
1853 const TypeKlassPtr* tkls = phase->type(adr2)->isa_klassptr();
1854 if (tkls != NULL && !tkls->empty()
1855 && (tkls->klass()->is_instance_klass() ||
1856 tkls->klass()->is_array_klass())
1857 && adr2->is_AddP()
1858 ) {
1859 int mirror_field = Klass::java_mirror_offset_in_bytes();
1860 if (offset == java_lang_Class::array_klass_offset_in_bytes()) {
1861 mirror_field = in_bytes(arrayKlass::component_mirror_offset());
1862 }
1863 if (tkls->offset() == mirror_field + (int)sizeof(oopDesc)) {
1864 return adr2->in(AddPNode::Base);
1865 }
1866 }
1867 }
1868 }
1869
1870 return this;
1871 }
1872
1873
1874 //------------------------------Value------------------------------------------
1875 const Type *LoadNKlassNode::Value( PhaseTransform *phase ) const {
1876 const Type *t = klass_value_common(phase);
1877 if (t == Type::TOP)
1878 return t;
1879
1880 return t->make_narrowoop();
1881 }
1882
1883 //------------------------------Identity---------------------------------------
1884 // To clean up reflective code, simplify k.java_mirror.as_klass to narrow k.
1885 // Also feed through the klass in Allocate(...klass...)._klass.
1886 Node* LoadNKlassNode::Identity( PhaseTransform *phase ) {
1887 Node *x = klass_identity_common(phase);
1888
1889 const Type *t = phase->type( x );
1890 if( t == Type::TOP ) return x;
1891 if( t->isa_narrowoop()) return x;
1892
1893 return phase->transform(new (phase->C, 2) EncodePNode(x, t->make_narrowoop()));
1894 }
1895
1896 //------------------------------Value-----------------------------------------
1897 const Type *LoadRangeNode::Value( PhaseTransform *phase ) const {
1898 // Either input is TOP ==> the result is TOP
1899 const Type *t1 = phase->type( in(MemNode::Memory) );
1900 if( t1 == Type::TOP ) return Type::TOP;
1901 Node *adr = in(MemNode::Address);
1902 const Type *t2 = phase->type( adr );
1903 if( t2 == Type::TOP ) return Type::TOP;
1904 const TypePtr *tp = t2->is_ptr();
1905 if (TypePtr::above_centerline(tp->ptr())) return Type::TOP;
1906 const TypeAryPtr *tap = tp->isa_aryptr();
1907 if( !tap ) return _type;
1908 return tap->size();
1909 }
1910
1911 //-------------------------------Ideal---------------------------------------
1912 // Feed through the length in AllocateArray(...length...)._length.
1913 Node *LoadRangeNode::Ideal(PhaseGVN *phase, bool can_reshape) {
1914 Node* p = MemNode::Ideal_common(phase, can_reshape);
1915 if (p) return (p == NodeSentinel) ? NULL : p;
1916
1917 // Take apart the address into an oop and and offset.
1918 // Return 'this' if we cannot.
1919 Node* adr = in(MemNode::Address);
1920 intptr_t offset = 0;
1921 Node* base = AddPNode::Ideal_base_and_offset(adr, phase, offset);
1922 if (base == NULL) return NULL;
1923 const TypeAryPtr* tary = phase->type(adr)->isa_aryptr();
1924 if (tary == NULL) return NULL;
1925
1926 // We can fetch the length directly through an AllocateArrayNode.
1927 // This works even if the length is not constant (clone or newArray).
1928 if (offset == arrayOopDesc::length_offset_in_bytes()) {
1929 AllocateArrayNode* alloc = AllocateArrayNode::Ideal_array_allocation(base, phase);
1930 if (alloc != NULL) {
1931 Node* allocated_length = alloc->Ideal_length();
1932 Node* len = alloc->make_ideal_length(tary, phase);
1933 if (allocated_length != len) {
1934 // New CastII improves on this.
1935 return len;
1936 }
1937 }
1938 }
1939
1940 return NULL;
1941 }
1942
1943 //------------------------------Identity---------------------------------------
1944 // Feed through the length in AllocateArray(...length...)._length.
1945 Node* LoadRangeNode::Identity( PhaseTransform *phase ) {
1946 Node* x = LoadINode::Identity(phase);
1947 if (x != this) return x;
1948
1949 // Take apart the address into an oop and and offset.
1950 // Return 'this' if we cannot.
1951 Node* adr = in(MemNode::Address);
1952 intptr_t offset = 0;
1953 Node* base = AddPNode::Ideal_base_and_offset(adr, phase, offset);
1954 if (base == NULL) return this;
1955 const TypeAryPtr* tary = phase->type(adr)->isa_aryptr();
1956 if (tary == NULL) return this;
1957
1958 // We can fetch the length directly through an AllocateArrayNode.
1959 // This works even if the length is not constant (clone or newArray).
1960 if (offset == arrayOopDesc::length_offset_in_bytes()) {
1961 AllocateArrayNode* alloc = AllocateArrayNode::Ideal_array_allocation(base, phase);
1962 if (alloc != NULL) {
1963 Node* allocated_length = alloc->Ideal_length();
1964 // Do not allow make_ideal_length to allocate a CastII node.
1965 Node* len = alloc->make_ideal_length(tary, phase, false);
1966 if (allocated_length == len) {
1967 // Return allocated_length only if it would not be improved by a CastII.
1968 return allocated_length;
1969 }
1970 }
1971 }
1972
1973 return this;
1974
1975 }
1976
1977 //=============================================================================
1978 //---------------------------StoreNode::make-----------------------------------
1979 // Polymorphic factory method:
1980 StoreNode* StoreNode::make( PhaseGVN& gvn, Node* ctl, Node* mem, Node* adr, const TypePtr* adr_type, Node* val, BasicType bt ) {
1981 Compile* C = gvn.C;
1982
1983 switch (bt) {
1984 case T_BOOLEAN:
1985 case T_BYTE: return new (C, 4) StoreBNode(ctl, mem, adr, adr_type, val);
1986 case T_INT: return new (C, 4) StoreINode(ctl, mem, adr, adr_type, val);
1987 case T_CHAR:
1988 case T_SHORT: return new (C, 4) StoreCNode(ctl, mem, adr, adr_type, val);
1989 case T_LONG: return new (C, 4) StoreLNode(ctl, mem, adr, adr_type, val);
1990 case T_FLOAT: return new (C, 4) StoreFNode(ctl, mem, adr, adr_type, val);
1991 case T_DOUBLE: return new (C, 4) StoreDNode(ctl, mem, adr, adr_type, val);
1992 case T_ADDRESS:
1993 case T_OBJECT:
1994 #ifdef _LP64
1995 if (adr->bottom_type()->is_ptr_to_narrowoop() ||
1996 (UseCompressedOops && val->bottom_type()->isa_klassptr() &&
1997 adr->bottom_type()->isa_rawptr())) {
1998 val = gvn.transform(new (C, 2) EncodePNode(val, val->bottom_type()->make_narrowoop()));
1999 return new (C, 4) StoreNNode(ctl, mem, adr, adr_type, val);
2000 } else
2001 #endif
2002 {
2003 return new (C, 4) StorePNode(ctl, mem, adr, adr_type, val);
2004 }
2005 }
2006 ShouldNotReachHere();
2007 return (StoreNode*)NULL;
2008 }
2009
2010 StoreLNode* StoreLNode::make_atomic(Compile *C, Node* ctl, Node* mem, Node* adr, const TypePtr* adr_type, Node* val) {
2011 bool require_atomic = true;
2012 return new (C, 4) StoreLNode(ctl, mem, adr, adr_type, val, require_atomic);
2013 }
2014
2015
2016 //--------------------------bottom_type----------------------------------------
2017 const Type *StoreNode::bottom_type() const {
2018 return Type::MEMORY;
2019 }
2020
2021 //------------------------------hash-------------------------------------------
2022 uint StoreNode::hash() const {
2023 // unroll addition of interesting fields
2024 //return (uintptr_t)in(Control) + (uintptr_t)in(Memory) + (uintptr_t)in(Address) + (uintptr_t)in(ValueIn);
2198 if( t2 && t2->is_con() && (t2->get_con() == t->get_con()) ) {
2199 set_req(MemNode::ValueIn, shl->in(1));
2200 return this;
2201 }
2202 }
2203 }
2204 }
2205 return NULL;
2206 }
2207
2208 //------------------------------value_never_loaded-----------------------------------
2209 // Determine whether there are any possible loads of the value stored.
2210 // For simplicity, we actually check if there are any loads from the
2211 // address stored to, not just for loads of the value stored by this node.
2212 //
2213 bool StoreNode::value_never_loaded( PhaseTransform *phase) const {
2214 Node *adr = in(Address);
2215 const TypeOopPtr *adr_oop = phase->type(adr)->isa_oopptr();
2216 if (adr_oop == NULL)
2217 return false;
2218 if (!adr_oop->is_known_instance_field())
2219 return false; // if not a distinct instance, there may be aliases of the address
2220 for (DUIterator_Fast imax, i = adr->fast_outs(imax); i < imax; i++) {
2221 Node *use = adr->fast_out(i);
2222 int opc = use->Opcode();
2223 if (use->is_Load() || use->is_LoadStore()) {
2224 return false;
2225 }
2226 }
2227 return true;
2228 }
2229
2230 //=============================================================================
2231 //------------------------------Ideal------------------------------------------
2232 // If the store is from an AND mask that leaves the low bits untouched, then
2233 // we can skip the AND operation. If the store is from a sign-extension
2234 // (a left shift, then right shift) we can skip both.
2235 Node *StoreBNode::Ideal(PhaseGVN *phase, bool can_reshape){
2236 Node *progress = StoreNode::Ideal_masked_input(phase, 0xFF);
2237 if( progress != NULL ) return progress;
2238
2307
2308 }
2309
2310 //=============================================================================
2311 //-------------------------------adr_type--------------------------------------
2312 // Do we Match on this edge index or not? Do not match memory
2313 const TypePtr* ClearArrayNode::adr_type() const {
2314 Node *adr = in(3);
2315 return MemNode::calculate_adr_type(adr->bottom_type());
2316 }
2317
2318 //------------------------------match_edge-------------------------------------
2319 // Do we Match on this edge index or not? Do not match memory
2320 uint ClearArrayNode::match_edge(uint idx) const {
2321 return idx > 1;
2322 }
2323
2324 //------------------------------Identity---------------------------------------
2325 // Clearing a zero length array does nothing
2326 Node *ClearArrayNode::Identity( PhaseTransform *phase ) {
2327 return phase->type(in(2))->higher_equal(TypeX::ZERO) ? in(1) : this;
2328 }
2329
2330 //------------------------------Idealize---------------------------------------
2331 // Clearing a short array is faster with stores
2332 Node *ClearArrayNode::Ideal(PhaseGVN *phase, bool can_reshape){
2333 const int unit = BytesPerLong;
2334 const TypeX* t = phase->type(in(2))->isa_intptr_t();
2335 if (!t) return NULL;
2336 if (!t->is_con()) return NULL;
2337 intptr_t raw_count = t->get_con();
2338 intptr_t size = raw_count;
2339 if (!Matcher::init_array_count_is_in_bytes) size *= unit;
2340 // Clearing nothing uses the Identity call.
2341 // Negative clears are possible on dead ClearArrays
2342 // (see jck test stmt114.stmt11402.val).
2343 if (size <= 0 || size % unit != 0) return NULL;
2344 intptr_t count = size / unit;
2345 // Length too long; use fast hardware clear
2346 if (size > Matcher::init_array_short_size) return NULL;
2347 Node *mem = in(1);
2366 adr = phase->transform(new (phase->C, 4) AddPNode(base,adr,off));
2367 mem = new (phase->C, 4) StoreLNode(in(0),mem,adr,atp,zero);
2368 }
2369 return mem;
2370 }
2371
2372 //----------------------------clear_memory-------------------------------------
2373 // Generate code to initialize object storage to zero.
2374 Node* ClearArrayNode::clear_memory(Node* ctl, Node* mem, Node* dest,
2375 intptr_t start_offset,
2376 Node* end_offset,
2377 PhaseGVN* phase) {
2378 Compile* C = phase->C;
2379 intptr_t offset = start_offset;
2380
2381 int unit = BytesPerLong;
2382 if ((offset % unit) != 0) {
2383 Node* adr = new (C, 4) AddPNode(dest, dest, phase->MakeConX(offset));
2384 adr = phase->transform(adr);
2385 const TypePtr* atp = TypeRawPtr::BOTTOM;
2386 mem = StoreNode::make(*phase, ctl, mem, adr, atp, phase->zerocon(T_INT), T_INT);
2387 mem = phase->transform(mem);
2388 offset += BytesPerInt;
2389 }
2390 assert((offset % unit) == 0, "");
2391
2392 // Initialize the remaining stuff, if any, with a ClearArray.
2393 return clear_memory(ctl, mem, dest, phase->MakeConX(offset), end_offset, phase);
2394 }
2395
2396 Node* ClearArrayNode::clear_memory(Node* ctl, Node* mem, Node* dest,
2397 Node* start_offset,
2398 Node* end_offset,
2399 PhaseGVN* phase) {
2400 if (start_offset == end_offset) {
2401 // nothing to do
2402 return mem;
2403 }
2404
2405 Compile* C = phase->C;
2406 int unit = BytesPerLong;
2407 Node* zbase = start_offset;
2408 Node* zend = end_offset;
2409
2410 // Scale to the unit required by the CPU:
2411 if (!Matcher::init_array_count_is_in_bytes) {
2412 Node* shift = phase->intcon(exact_log2(unit));
2413 zbase = phase->transform( new(C,3) URShiftXNode(zbase, shift) );
2414 zend = phase->transform( new(C,3) URShiftXNode(zend, shift) );
2415 }
2416
2417 Node* zsize = phase->transform( new(C,3) SubXNode(zend, zbase) );
2418 Node* zinit = phase->zerocon((unit == BytesPerLong) ? T_LONG : T_INT);
2419
2420 // Bulk clear double-words
2421 Node* adr = phase->transform( new(C,4) AddPNode(dest, dest, start_offset) );
2422 mem = new (C, 4) ClearArrayNode(ctl, mem, zsize, adr);
2423 return phase->transform(mem);
2424 }
2425
2426 Node* ClearArrayNode::clear_memory(Node* ctl, Node* mem, Node* dest,
2427 intptr_t start_offset,
2428 intptr_t end_offset,
2429 PhaseGVN* phase) {
2430 if (start_offset == end_offset) {
2431 // nothing to do
2432 return mem;
2433 }
2434
2435 Compile* C = phase->C;
2436 assert((end_offset % BytesPerInt) == 0, "odd end offset");
2437 intptr_t done_offset = end_offset;
2438 if ((done_offset % BytesPerLong) != 0) {
2439 done_offset -= BytesPerInt;
2440 }
2441 if (done_offset > start_offset) {
2442 mem = clear_memory(ctl, mem, dest,
2443 start_offset, phase->MakeConX(done_offset), phase);
2444 }
2445 if (done_offset < end_offset) { // emit the final 32-bit store
2446 Node* adr = new (C, 4) AddPNode(dest, dest, phase->MakeConX(done_offset));
2447 adr = phase->transform(adr);
2448 const TypePtr* atp = TypeRawPtr::BOTTOM;
2449 mem = StoreNode::make(*phase, ctl, mem, adr, atp, phase->zerocon(T_INT), T_INT);
2450 mem = phase->transform(mem);
2451 done_offset += BytesPerInt;
2452 }
2453 assert(done_offset == end_offset, "");
2454 return mem;
2455 }
2456
2457 //=============================================================================
2458 // Do we match on this edge? No memory edges
2459 uint StrCompNode::match_edge(uint idx) const {
2460 return idx == 5 || idx == 6;
2461 }
2462
2463 //------------------------------Ideal------------------------------------------
2464 // Return a node which is more "ideal" than the current node. Strip out
2465 // control copies
2466 Node *StrCompNode::Ideal(PhaseGVN *phase, bool can_reshape){
2467 return remove_dead_region(phase, can_reshape) ? this : NULL;
2468 }
2469
2470 //------------------------------Ideal------------------------------------------
2471 // Return a node which is more "ideal" than the current node. Strip out
2472 // control copies
2473 Node *AryEqNode::Ideal(PhaseGVN *phase, bool can_reshape){
2474 return remove_dead_region(phase, can_reshape) ? this : NULL;
2475 }
2476
2477
2478 //=============================================================================
2479 MemBarNode::MemBarNode(Compile* C, int alias_idx, Node* precedent)
2480 : MultiNode(TypeFunc::Parms + (precedent == NULL? 0: 1)),
2481 _adr_type(C->get_adr_type(alias_idx))
2482 {
2483 init_class_id(Class_MemBar);
2484 Node* top = C->top();
2485 init_req(TypeFunc::I_O,top);
2486 init_req(TypeFunc::FramePtr,top);
2487 init_req(TypeFunc::ReturnAdr,top);
2488 if (precedent != NULL)
2489 init_req(TypeFunc::Parms, precedent);
2490 }
2491
2492 //------------------------------cmp--------------------------------------------
2493 uint MemBarNode::hash() const { return NO_HASH; }
2494 uint MemBarNode::cmp( const Node &n ) const {
2495 return (&n == this); // Always fail except on self
2496 }
2497
2498 //------------------------------make-------------------------------------------
2499 MemBarNode* MemBarNode::make(Compile* C, int opcode, int atp, Node* pn) {
2500 int len = Precedent + (pn == NULL? 0: 1);
2501 switch (opcode) {
2502 case Op_MemBarAcquire: return new(C, len) MemBarAcquireNode(C, atp, pn);
2503 case Op_MemBarRelease: return new(C, len) MemBarReleaseNode(C, atp, pn);
2504 case Op_MemBarVolatile: return new(C, len) MemBarVolatileNode(C, atp, pn);
2505 case Op_MemBarCPUOrder: return new(C, len) MemBarCPUOrderNode(C, atp, pn);
2506 case Op_Initialize: return new(C, len) InitializeNode(C, atp, pn);
2507 default: ShouldNotReachHere(); return NULL;
2508 }
2509 }
2510
2511 //------------------------------Ideal------------------------------------------
2512 // Return a node which is more "ideal" than the current node. Strip out
2513 // control copies
2514 Node *MemBarNode::Ideal(PhaseGVN *phase, bool can_reshape) {
2515 return remove_dead_region(phase, can_reshape) ? this : NULL;
2516 }
2517
2518 //------------------------------Value------------------------------------------
2519 const Type *MemBarNode::Value( PhaseTransform *phase ) const {
2520 if( !in(0) ) return Type::TOP;
2521 if( phase->type(in(0)) == Type::TOP )
2522 return Type::TOP;
2523 return TypeTuple::MEMBAR;
2524 }
2525
2526 //------------------------------match------------------------------------------
2527 // Construct projections for memory.
2528 Node *MemBarNode::match( const ProjNode *proj, const Matcher *m ) {
2529 switch (proj->_con) {
2530 case TypeFunc::Control:
2531 case TypeFunc::Memory:
2532 return new (m->C, 1) MachProjNode(this,proj->_con,RegMask::Empty,MachProjNode::unmatched_proj);
2533 }
2534 ShouldNotReachHere();
2535 return NULL;
2725 bool InitializeNode::detect_init_independence(Node* n,
2726 bool st_is_pinned,
2727 int& count) {
2728 if (n == NULL) return true; // (can this really happen?)
2729 if (n->is_Proj()) n = n->in(0);
2730 if (n == this) return false; // found a cycle
2731 if (n->is_Con()) return true;
2732 if (n->is_Start()) return true; // params, etc., are OK
2733 if (n->is_Root()) return true; // even better
2734
2735 Node* ctl = n->in(0);
2736 if (ctl != NULL && !ctl->is_top()) {
2737 if (ctl->is_Proj()) ctl = ctl->in(0);
2738 if (ctl == this) return false;
2739
2740 // If we already know that the enclosing memory op is pinned right after
2741 // the init, then any control flow that the store has picked up
2742 // must have preceded the init, or else be equal to the init.
2743 // Even after loop optimizations (which might change control edges)
2744 // a store is never pinned *before* the availability of its inputs.
2745 if (!MemNode::all_controls_dominate(n, this))
2746 return false; // failed to prove a good control
2747
2748 }
2749
2750 // Check data edges for possible dependencies on 'this'.
2751 if ((count += 1) > 20) return false; // complexity limit
2752 for (uint i = 1; i < n->req(); i++) {
2753 Node* m = n->in(i);
2754 if (m == NULL || m == n || m->is_top()) continue;
2755 uint first_i = n->find_edge(m);
2756 if (i != first_i) continue; // process duplicate edge just once
2757 if (!detect_init_independence(m, st_is_pinned, count)) {
2758 return false;
2759 }
2760 }
2761
2762 return true;
2763 }
2764
2765 // Here are all the checks a Store must pass before it can be moved into
2792 }
2793
2794 // Find the captured store in(i) which corresponds to the range
2795 // [start..start+size) in the initialized object.
2796 // If there is one, return its index i. If there isn't, return the
2797 // negative of the index where it should be inserted.
2798 // Return 0 if the queried range overlaps an initialization boundary
2799 // or if dead code is encountered.
2800 // If size_in_bytes is zero, do not bother with overlap checks.
2801 int InitializeNode::captured_store_insertion_point(intptr_t start,
2802 int size_in_bytes,
2803 PhaseTransform* phase) {
2804 const int FAIL = 0, MAX_STORE = BytesPerLong;
2805
2806 if (is_complete())
2807 return FAIL; // arraycopy got here first; punt
2808
2809 assert(allocation() != NULL, "must be present");
2810
2811 // no negatives, no header fields:
2812 if (start < (intptr_t) allocation()->minimum_header_size()) return FAIL;
2813
2814 // after a certain size, we bail out on tracking all the stores:
2815 intptr_t ti_limit = (TrackedInitializationLimit * HeapWordSize);
2816 if (start >= ti_limit) return FAIL;
2817
2818 for (uint i = InitializeNode::RawStores, limit = req(); ; ) {
2819 if (i >= limit) return -(int)i; // not found; here is where to put it
2820
2821 Node* st = in(i);
2822 intptr_t st_off = get_store_offset(st, phase);
2823 if (st_off < 0) {
2824 if (st != zero_memory()) {
2825 return FAIL; // bail out if there is dead garbage
2826 }
2827 } else if (st_off > start) {
2828 // ...we are done, since stores are ordered
2829 if (st_off < start + size_in_bytes) {
2830 return FAIL; // the next store overlaps
2831 }
2832 return -(int)i; // not found; here is where to put it
3129 }
3130
3131 // Here's a case where init0 is neither 0 nor -1:
3132 // byte a[] = { ... 0,0,foo(),0, 0,0,0,42 ... }
3133 // Assuming big-endian memory, init0, init1 are 0x0000FF00, 0x000000FF.
3134 // In this case the tile is not split; it is (jlong)42.
3135 // The big tile is stored down, and then the foo() value is inserted.
3136 // (If there were foo(),foo() instead of foo(),0, init0 would be -1.)
3137
3138 Node* ctl = old->in(MemNode::Control);
3139 Node* adr = make_raw_address(offset, phase);
3140 const TypePtr* atp = TypeRawPtr::BOTTOM;
3141
3142 // One or two coalesced stores to plop down.
3143 Node* st[2];
3144 intptr_t off[2];
3145 int nst = 0;
3146 if (!split) {
3147 ++new_long;
3148 off[nst] = offset;
3149 st[nst++] = StoreNode::make(*phase, ctl, zmem, adr, atp,
3150 phase->longcon(con), T_LONG);
3151 } else {
3152 // Omit either if it is a zero.
3153 if (con0 != 0) {
3154 ++new_int;
3155 off[nst] = offset;
3156 st[nst++] = StoreNode::make(*phase, ctl, zmem, adr, atp,
3157 phase->intcon(con0), T_INT);
3158 }
3159 if (con1 != 0) {
3160 ++new_int;
3161 offset += BytesPerInt;
3162 adr = make_raw_address(offset, phase);
3163 off[nst] = offset;
3164 st[nst++] = StoreNode::make(*phase, ctl, zmem, adr, atp,
3165 phase->intcon(con1), T_INT);
3166 }
3167 }
3168
3169 // Insert second store first, then the first before the second.
3170 // Insert each one just before any overlapping non-constant stores.
3171 while (nst > 0) {
3172 Node* st1 = st[--nst];
3173 C->copy_node_notes_to(st1, old);
3174 st1 = phase->transform(st1);
3175 offset = off[nst];
3176 assert(offset >= header_size, "do not smash header");
3177 int ins_idx = captured_store_insertion_point(offset, /*size:*/0, phase);
3178 guarantee(ins_idx != 0, "must re-insert constant store");
3179 if (ins_idx < 0) ins_idx = -ins_idx; // never overlap
3180 if (ins_idx > InitializeNode::RawStores && in(ins_idx-1) == zmem)
3181 set_req(--ins_idx, st1);
3182 else
3183 ins_req(ins_idx, st1);
3184 }
3252 // At this point, we may perform additional optimizations.
3253 // Linearize the stores by ascending offset, to make memory
3254 // activity as coherent as possible.
3255 Node* InitializeNode::complete_stores(Node* rawctl, Node* rawmem, Node* rawptr,
3256 intptr_t header_size,
3257 Node* size_in_bytes,
3258 PhaseGVN* phase) {
3259 assert(!is_complete(), "not already complete");
3260 assert(stores_are_sane(phase), "");
3261 assert(allocation() != NULL, "must be present");
3262
3263 remove_extra_zeroes();
3264
3265 if (ReduceFieldZeroing || ReduceBulkZeroing)
3266 // reduce instruction count for common initialization patterns
3267 coalesce_subword_stores(header_size, size_in_bytes, phase);
3268
3269 Node* zmem = zero_memory(); // initially zero memory state
3270 Node* inits = zmem; // accumulating a linearized chain of inits
3271 #ifdef ASSERT
3272 intptr_t first_offset = allocation()->minimum_header_size();
3273 intptr_t last_init_off = first_offset; // previous init offset
3274 intptr_t last_init_end = first_offset; // previous init offset+size
3275 intptr_t last_tile_end = first_offset; // previous tile offset+size
3276 #endif
3277 intptr_t zeroes_done = header_size;
3278
3279 bool do_zeroing = true; // we might give up if inits are very sparse
3280 int big_init_gaps = 0; // how many large gaps have we seen?
3281
3282 if (ZeroTLAB) do_zeroing = false;
3283 if (!ReduceFieldZeroing && !ReduceBulkZeroing) do_zeroing = false;
3284
3285 for (uint i = InitializeNode::RawStores, limit = req(); i < limit; i++) {
3286 Node* st = in(i);
3287 intptr_t st_off = get_store_offset(st, phase);
3288 if (st_off < 0)
3289 break; // unknown junk in the inits
3290 if (st->in(MemNode::Memory) != zmem)
3291 break; // complicated store chains somehow in list
3292
3293 int st_size = st->as_Store()->memory_size();
3294 intptr_t next_init_off = st_off + st_size;
3295
3390 Node* klass_node = allocation()->in(AllocateNode::KlassNode);
3391 ciKlass* k = phase->type(klass_node)->is_klassptr()->klass();
3392 if (zeroes_done == k->layout_helper())
3393 zeroes_done = size_limit;
3394 }
3395 if (zeroes_done < size_limit) {
3396 rawmem = ClearArrayNode::clear_memory(rawctl, rawmem, rawptr,
3397 zeroes_done, size_in_bytes, phase);
3398 }
3399 }
3400
3401 set_complete(phase);
3402 return rawmem;
3403 }
3404
3405
3406 #ifdef ASSERT
3407 bool InitializeNode::stores_are_sane(PhaseTransform* phase) {
3408 if (is_complete())
3409 return true; // stores could be anything at this point
3410 assert(allocation() != NULL, "must be present");
3411 intptr_t last_off = allocation()->minimum_header_size();
3412 for (uint i = InitializeNode::RawStores; i < req(); i++) {
3413 Node* st = in(i);
3414 intptr_t st_off = get_store_offset(st, phase);
3415 if (st_off < 0) continue; // ignore dead garbage
3416 if (last_off > st_off) {
3417 tty->print_cr("*** bad store offset at %d: %d > %d", i, last_off, st_off);
3418 this->dump(2);
3419 assert(false, "ascending store offsets");
3420 return false;
3421 }
3422 last_off = st_off + st->as_Store()->memory_size();
3423 }
3424 return true;
3425 }
3426 #endif //ASSERT
3427
3428
3429
3430
3431 //============================MergeMemNode=====================================
3746 if( in(i) != empty_mem ) { set_req(i, empty_mem); }
3747 }
3748 }
3749 }
3750
3751 if( !progress && base_memory()->is_Phi() && can_reshape ) {
3752 // Check if PhiNode::Ideal's "Split phis through memory merges"
3753 // transform should be attempted. Look for this->phi->this cycle.
3754 uint merge_width = req();
3755 if (merge_width > Compile::AliasIdxRaw) {
3756 PhiNode* phi = base_memory()->as_Phi();
3757 for( uint i = 1; i < phi->req(); ++i ) {// For all paths in
3758 if (phi->in(i) == this) {
3759 phase->is_IterGVN()->_worklist.push(phi);
3760 break;
3761 }
3762 }
3763 }
3764 }
3765
3766 assert(progress || verify_sparse(), "please, no dups of base");
3767 return progress;
3768 }
3769
3770 //-------------------------set_base_memory-------------------------------------
3771 void MergeMemNode::set_base_memory(Node *new_base) {
3772 Node* empty_mem = empty_memory();
3773 set_req(Compile::AliasIdxBot, new_base);
3774 assert(memory_at(req()) == new_base, "must set default memory");
3775 // Clear out other occurrences of new_base:
3776 if (new_base != empty_mem) {
3777 for (uint i = Compile::AliasIdxRaw; i < req(); i++) {
3778 if (in(i) == new_base) set_req(i, empty_mem);
3779 }
3780 }
3781 }
3782
3783 //------------------------------out_RegMask------------------------------------
3784 const RegMask &MergeMemNode::out_RegMask() const {
3785 return RegMask::Empty;
3786 }
|