1/*
2 * Copyright (c) 2005, 2018, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25#include "precompiled.hpp"
26#include "compiler/compileLog.hpp"
27#include "gc/shared/collectedHeap.inline.hpp"
28#include "libadt/vectset.hpp"
29#include "memory/universe.hpp"
30#include "opto/addnode.hpp"
31#include "opto/arraycopynode.hpp"
32#include "opto/callnode.hpp"
33#include "opto/castnode.hpp"
34#include "opto/cfgnode.hpp"
35#include "opto/compile.hpp"
36#include "opto/convertnode.hpp"
37#include "opto/graphKit.hpp"
38#include "opto/locknode.hpp"
39#include "opto/loopnode.hpp"
40#include "opto/macro.hpp"
41#include "opto/memnode.hpp"
42#include "opto/narrowptrnode.hpp"
43#include "opto/node.hpp"
44#include "opto/opaquenode.hpp"
45#include "opto/phaseX.hpp"
46#include "opto/rootnode.hpp"
47#include "opto/runtime.hpp"
48#include "opto/subnode.hpp"
49#include "opto/type.hpp"
50#include "runtime/sharedRuntime.hpp"
51#include "utilities/macros.hpp"
52#if INCLUDE_G1GC
53#include "gc/g1/g1ThreadLocalData.hpp"
54#endif // INCLUDE_G1GC
55#if INCLUDE_SHENANDOAHGC
56#include "gc/shenandoah/c2/shenandoahBarrierSetC2.hpp"
57#endif
58
59
60//
61// Replace any references to "oldref" in inputs to "use" with "newref".
62// Returns the number of replacements made.
63//
64int PhaseMacroExpand::replace_input(Node *use, Node *oldref, Node *newref) {
65 int nreplacements = 0;
66 uint req = use->req();
67 for (uint j = 0; j < use->len(); j++) {
68 Node *uin = use->in(j);
69 if (uin == oldref) {
70 if (j < req)
71 use->set_req(j, newref);
72 else
73 use->set_prec(j, newref);
74 nreplacements++;
75 } else if (j >= req && uin == NULL) {
76 break;
77 }
78 }
79 return nreplacements;
80}
81
82void PhaseMacroExpand::copy_call_debug_info(CallNode *oldcall, CallNode * newcall) {
83 // Copy debug information and adjust JVMState information
84 uint old_dbg_start = oldcall->tf()->domain()->cnt();
85 uint new_dbg_start = newcall->tf()->domain()->cnt();
86 int jvms_adj = new_dbg_start - old_dbg_start;
87 assert (new_dbg_start == newcall->req(), "argument count mismatch");
88
89 // SafePointScalarObject node could be referenced several times in debug info.
90 // Use Dict to record cloned nodes.
91 Dict* sosn_map = new Dict(cmpkey,hashkey);
92 for (uint i = old_dbg_start; i < oldcall->req(); i++) {
93 Node* old_in = oldcall->in(i);
94 // Clone old SafePointScalarObjectNodes, adjusting their field contents.
95 if (old_in != NULL && old_in->is_SafePointScalarObject()) {
96 SafePointScalarObjectNode* old_sosn = old_in->as_SafePointScalarObject();
97 uint old_unique = C->unique();
98 Node* new_in = old_sosn->clone(sosn_map);
99 if (old_unique != C->unique()) { // New node?
100 new_in->set_req(0, C->root()); // reset control edge
101 new_in = transform_later(new_in); // Register new node.
102 }
103 old_in = new_in;
104 }
105 newcall->add_req(old_in);
106 }
107
108 // JVMS may be shared so clone it before we modify it
109 newcall->set_jvms(oldcall->jvms() != NULL ? oldcall->jvms()->clone_deep(C) : NULL);
110 for (JVMState *jvms = newcall->jvms(); jvms != NULL; jvms = jvms->caller()) {
111 jvms->set_map(newcall);
112 jvms->set_locoff(jvms->locoff()+jvms_adj);
113 jvms->set_stkoff(jvms->stkoff()+jvms_adj);
114 jvms->set_monoff(jvms->monoff()+jvms_adj);
115 jvms->set_scloff(jvms->scloff()+jvms_adj);
116 jvms->set_endoff(jvms->endoff()+jvms_adj);
117 }
118}
119
120Node* PhaseMacroExpand::opt_bits_test(Node* ctrl, Node* region, int edge, Node* word, int mask, int bits, bool return_fast_path) {
121 Node* cmp;
122 if (mask != 0) {
123 Node* and_node = transform_later(new AndXNode(word, MakeConX(mask)));
124 cmp = transform_later(new CmpXNode(and_node, MakeConX(bits)));
125 } else {
126 cmp = word;
127 }
128 Node* bol = transform_later(new BoolNode(cmp, BoolTest::ne));
129 IfNode* iff = new IfNode( ctrl, bol, PROB_MIN, COUNT_UNKNOWN );
130 transform_later(iff);
131
132 // Fast path taken.
133 Node *fast_taken = transform_later(new IfFalseNode(iff));
134
135 // Fast path not-taken, i.e. slow path
136 Node *slow_taken = transform_later(new IfTrueNode(iff));
137
138 if (return_fast_path) {
139 region->init_req(edge, slow_taken); // Capture slow-control
140 return fast_taken;
141 } else {
142 region->init_req(edge, fast_taken); // Capture fast-control
143 return slow_taken;
144 }
145}
146
147//--------------------copy_predefined_input_for_runtime_call--------------------
148void PhaseMacroExpand::copy_predefined_input_for_runtime_call(Node * ctrl, CallNode* oldcall, CallNode* call) {
149 // Set fixed predefined input arguments
150 call->init_req( TypeFunc::Control, ctrl );
151 call->init_req( TypeFunc::I_O , oldcall->in( TypeFunc::I_O) );
152 call->init_req( TypeFunc::Memory , oldcall->in( TypeFunc::Memory ) ); // ?????
153 call->init_req( TypeFunc::ReturnAdr, oldcall->in( TypeFunc::ReturnAdr ) );
154 call->init_req( TypeFunc::FramePtr, oldcall->in( TypeFunc::FramePtr ) );
155}
156
157//------------------------------make_slow_call---------------------------------
158CallNode* PhaseMacroExpand::make_slow_call(CallNode *oldcall, const TypeFunc* slow_call_type,
159 address slow_call, const char* leaf_name, Node* slow_path,
160 Node* parm0, Node* parm1, Node* parm2) {
161
162 // Slow-path call
163 CallNode *call = leaf_name
164 ? (CallNode*)new CallLeafNode ( slow_call_type, slow_call, leaf_name, TypeRawPtr::BOTTOM )
165 : (CallNode*)new CallStaticJavaNode( slow_call_type, slow_call, OptoRuntime::stub_name(slow_call), oldcall->jvms()->bci(), TypeRawPtr::BOTTOM );
166
167 // Slow path call has no side-effects, uses few values
168 copy_predefined_input_for_runtime_call(slow_path, oldcall, call );
169 if (parm0 != NULL) call->init_req(TypeFunc::Parms+0, parm0);
170 if (parm1 != NULL) call->init_req(TypeFunc::Parms+1, parm1);
171 if (parm2 != NULL) call->init_req(TypeFunc::Parms+2, parm2);
172 copy_call_debug_info(oldcall, call);
173 call->set_cnt(PROB_UNLIKELY_MAG(4)); // Same effect as RC_UNCOMMON.
174 _igvn.replace_node(oldcall, call);
175 transform_later(call);
176
177 return call;
178}
179
180void PhaseMacroExpand::extract_call_projections(CallNode *call) {
181 _fallthroughproj = NULL;
182 _fallthroughcatchproj = NULL;
183 _ioproj_fallthrough = NULL;
184 _ioproj_catchall = NULL;
185 _catchallcatchproj = NULL;
186 _memproj_fallthrough = NULL;
187 _memproj_catchall = NULL;
188 _resproj = NULL;
189 for (DUIterator_Fast imax, i = call->fast_outs(imax); i < imax; i++) {
190 ProjNode *pn = call->fast_out(i)->as_Proj();
191 switch (pn->_con) {
192 case TypeFunc::Control:
193 {
194 // For Control (fallthrough) and I_O (catch_all_index) we have CatchProj -> Catch -> Proj
195 _fallthroughproj = pn;
196 DUIterator_Fast jmax, j = pn->fast_outs(jmax);
197 const Node *cn = pn->fast_out(j);
198 if (cn->is_Catch()) {
199 ProjNode *cpn = NULL;
200 for (DUIterator_Fast kmax, k = cn->fast_outs(kmax); k < kmax; k++) {
201 cpn = cn->fast_out(k)->as_Proj();
202 assert(cpn->is_CatchProj(), "must be a CatchProjNode");
203 if (cpn->_con == CatchProjNode::fall_through_index)
204 _fallthroughcatchproj = cpn;
205 else {
206 assert(cpn->_con == CatchProjNode::catch_all_index, "must be correct index.");
207 _catchallcatchproj = cpn;
208 }
209 }
210 }
211 break;
212 }
213 case TypeFunc::I_O:
214 if (pn->_is_io_use)
215 _ioproj_catchall = pn;
216 else
217 _ioproj_fallthrough = pn;
218 break;
219 case TypeFunc::Memory:
220 if (pn->_is_io_use)
221 _memproj_catchall = pn;
222 else
223 _memproj_fallthrough = pn;
224 break;
225 case TypeFunc::Parms:
226 _resproj = pn;
227 break;
228 default:
229 assert(false, "unexpected projection from allocation node.");
230 }
231 }
232
233}
234
235void PhaseMacroExpand::eliminate_gc_barrier(Node* p2x) {
236 BarrierSetC2 *bs = BarrierSet::barrier_set()->barrier_set_c2();
237 bs->eliminate_gc_barrier(this, p2x);
238}
239
240// Search for a memory operation for the specified memory slice.
241static Node *scan_mem_chain(Node *mem, int alias_idx, int offset, Node *start_mem, Node *alloc, PhaseGVN *phase) {
242 Node *orig_mem = mem;
243 Node *alloc_mem = alloc->in(TypeFunc::Memory);
244 const TypeOopPtr *tinst = phase->C->get_adr_type(alias_idx)->isa_oopptr();
245 while (true) {
246 if (mem == alloc_mem || mem == start_mem ) {
247 return mem; // hit one of our sentinels
248 } else if (mem->is_MergeMem()) {
249 mem = mem->as_MergeMem()->memory_at(alias_idx);
250 } else if (mem->is_Proj() && mem->as_Proj()->_con == TypeFunc::Memory) {
251 Node *in = mem->in(0);
252 // we can safely skip over safepoints, calls, locks and membars because we
253 // already know that the object is safe to eliminate.
254 if (in->is_Initialize() && in->as_Initialize()->allocation() == alloc) {
255 return in;
256 } else if (in->is_Call()) {
257 CallNode *call = in->as_Call();
258 if (call->may_modify(tinst, phase)) {
259 assert(call->is_ArrayCopy(), "ArrayCopy is the only call node that doesn't make allocation escape");
260 if (call->as_ArrayCopy()->modifies(offset, offset, phase, false)) {
261 return in;
262 }
263 }
264 mem = in->in(TypeFunc::Memory);
265 } else if (in->is_MemBar()) {
266 ArrayCopyNode* ac = NULL;
267 if (ArrayCopyNode::may_modify(tinst, in->as_MemBar(), phase, ac)) {
268 assert(ac != NULL && ac->is_clonebasic(), "Only basic clone is a non escaping clone");
269 return ac;
270 }
271 mem = in->in(TypeFunc::Memory);
272 } else {
273 assert(false, "unexpected projection");
274 }
275 } else if (mem->is_Store()) {
276 const TypePtr* atype = mem->as_Store()->adr_type();
277 int adr_idx = phase->C->get_alias_index(atype);
278 if (adr_idx == alias_idx) {
279 assert(atype->isa_oopptr(), "address type must be oopptr");
280 int adr_offset = atype->offset();
281 uint adr_iid = atype->is_oopptr()->instance_id();
282 // Array elements references have the same alias_idx
283 // but different offset and different instance_id.
284 if (adr_offset == offset && adr_iid == alloc->_idx)
285 return mem;
286 } else {
287 assert(adr_idx == Compile::AliasIdxRaw, "address must match or be raw");
288 }
289 mem = mem->in(MemNode::Memory);
290 } else if (mem->is_ClearArray()) {
291 if (!ClearArrayNode::step_through(&mem, alloc->_idx, phase)) {
292 // Can not bypass initialization of the instance
293 // we are looking.
294 debug_only(intptr_t offset;)
295 assert(alloc == AllocateNode::Ideal_allocation(mem->in(3), phase, offset), "sanity");
296 InitializeNode* init = alloc->as_Allocate()->initialization();
297 // We are looking for stored value, return Initialize node
298 // or memory edge from Allocate node.
299 if (init != NULL)
300 return init;
301 else
302 return alloc->in(TypeFunc::Memory); // It will produce zero value (see callers).
303 }
304 // Otherwise skip it (the call updated 'mem' value).
305 } else if (mem->Opcode() == Op_SCMemProj) {
306 mem = mem->in(0);
307 Node* adr = NULL;
308 if (mem->is_LoadStore()) {
309 adr = mem->in(MemNode::Address);
310 } else {
311 assert(mem->Opcode() == Op_EncodeISOArray ||
312 mem->Opcode() == Op_StrCompressedCopy, "sanity");
313 adr = mem->in(3); // Destination array
314 }
315 const TypePtr* atype = adr->bottom_type()->is_ptr();
316 int adr_idx = phase->C->get_alias_index(atype);
317 if (adr_idx == alias_idx) {
318 DEBUG_ONLY(mem->dump();)
319 assert(false, "Object is not scalar replaceable if a LoadStore node accesses its field");
320 return NULL;
321 }
322 mem = mem->in(MemNode::Memory);
323 } else if (mem->Opcode() == Op_StrInflatedCopy) {
324 Node* adr = mem->in(3); // Destination array
325 const TypePtr* atype = adr->bottom_type()->is_ptr();
326 int adr_idx = phase->C->get_alias_index(atype);
327 if (adr_idx == alias_idx) {
328 DEBUG_ONLY(mem->dump();)
329 assert(false, "Object is not scalar replaceable if a StrInflatedCopy node accesses its field");
330 return NULL;
331 }
332 mem = mem->in(MemNode::Memory);
333 } else {
334 return mem;
335 }
336 assert(mem != orig_mem, "dead memory loop");
337 }
338}
339
340// Generate loads from source of the arraycopy for fields of
341// destination needed at a deoptimization point
342Node* PhaseMacroExpand::make_arraycopy_load(ArrayCopyNode* ac, intptr_t offset, Node* ctl, Node* mem, BasicType ft, const Type *ftype, AllocateNode *alloc) {
343 BasicType bt = ft;
344 const Type *type = ftype;
345 if (ft == T_NARROWOOP) {
346 bt = T_OBJECT;
347 type = ftype->make_oopptr();
348 }
349 Node* res = NULL;
350 if (ac->is_clonebasic()) {
351 Node* base = ac->in(ArrayCopyNode::Src)->in(AddPNode::Base);
352 Node* adr = _igvn.transform(new AddPNode(base, base, MakeConX(offset)));
353 const TypePtr* adr_type = _igvn.type(base)->is_ptr()->add_offset(offset);
354 res = LoadNode::make(_igvn, ctl, mem, adr, adr_type, type, bt, MemNode::unordered, LoadNode::Pinned);
355 } else {
356 if (ac->modifies(offset, offset, &_igvn, true)) {
357 assert(ac->in(ArrayCopyNode::Dest) == alloc->result_cast(), "arraycopy destination should be allocation's result");
358 uint shift = exact_log2(type2aelembytes(bt));
359 Node* diff = _igvn.transform(new SubINode(ac->in(ArrayCopyNode::SrcPos), ac->in(ArrayCopyNode::DestPos)));
360#ifdef _LP64
361 diff = _igvn.transform(new ConvI2LNode(diff));
362#endif
363 diff = _igvn.transform(new LShiftXNode(diff, intcon(shift)));
364
365 Node* off = _igvn.transform(new AddXNode(MakeConX(offset), diff));
366 Node* base = ac->in(ArrayCopyNode::Src);
367 Node* adr = _igvn.transform(new AddPNode(base, base, off));
368 const TypePtr* adr_type = _igvn.type(base)->is_ptr()->add_offset(offset);
369 res = LoadNode::make(_igvn, ctl, mem, adr, adr_type, type, bt, MemNode::unordered, LoadNode::Pinned);
370 }
371 }
372 if (res != NULL) {
373 res = _igvn.transform(res);
374 if (ftype->isa_narrowoop()) {
375 // PhaseMacroExpand::scalar_replacement adds DecodeN nodes
376 res = _igvn.transform(new EncodePNode(res, ftype));
377 }
378 return res;
379 }
380 return NULL;
381}
382
383//
384// Given a Memory Phi, compute a value Phi containing the values from stores
385// on the input paths.
386// Note: this function is recursive, its depth is limited by the "level" argument
387// Returns the computed Phi, or NULL if it cannot compute it.
388Node *PhaseMacroExpand::value_from_mem_phi(Node *mem, BasicType ft, const Type *phi_type, const TypeOopPtr *adr_t, AllocateNode *alloc, Node_Stack *value_phis, int level) {
389 assert(mem->is_Phi(), "sanity");
390 int alias_idx = C->get_alias_index(adr_t);
391 int offset = adr_t->offset();
392 int instance_id = adr_t->instance_id();
393
394 // Check if an appropriate value phi already exists.
395 Node* region = mem->in(0);
396 for (DUIterator_Fast kmax, k = region->fast_outs(kmax); k < kmax; k++) {
397 Node* phi = region->fast_out(k);
398 if (phi->is_Phi() && phi != mem &&
399 phi->as_Phi()->is_same_inst_field(phi_type, (int)mem->_idx, instance_id, alias_idx, offset)) {
400 return phi;
401 }
402 }
403 // Check if an appropriate new value phi already exists.
404 Node* new_phi = value_phis->find(mem->_idx);
405 if (new_phi != NULL)
406 return new_phi;
407
408 if (level <= 0) {
409 return NULL; // Give up: phi tree too deep
410 }
411 Node *start_mem = C->start()->proj_out_or_null(TypeFunc::Memory);
412 Node *alloc_mem = alloc->in(TypeFunc::Memory);
413
414 uint length = mem->req();
415 GrowableArray <Node *> values(length, length, NULL, false);
416
417 // create a new Phi for the value
418 PhiNode *phi = new PhiNode(mem->in(0), phi_type, NULL, mem->_idx, instance_id, alias_idx, offset);
419 transform_later(phi);
420 value_phis->push(phi, mem->_idx);
421
422 for (uint j = 1; j < length; j++) {
423 Node *in = mem->in(j);
424 if (in == NULL || in->is_top()) {
425 values.at_put(j, in);
426 } else {
427 Node *val = scan_mem_chain(in, alias_idx, offset, start_mem, alloc, &_igvn);
428 if (val == start_mem || val == alloc_mem) {
429 // hit a sentinel, return appropriate 0 value
430 values.at_put(j, _igvn.zerocon(ft));
431 continue;
432 }
433 if (val->is_Initialize()) {
434 val = val->as_Initialize()->find_captured_store(offset, type2aelembytes(ft), &_igvn);
435 }
436 if (val == NULL) {
437 return NULL; // can't find a value on this path
438 }
439 if (val == mem) {
440 values.at_put(j, mem);
441 } else if (val->is_Store()) {
442 Node* n = val->in(MemNode::ValueIn);
443 BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
444 n = bs->step_over_gc_barrier(n);
445 values.at_put(j, n);
446 } else if(val->is_Proj() && val->in(0) == alloc) {
447 values.at_put(j, _igvn.zerocon(ft));
448 } else if (val->is_Phi()) {
449 val = value_from_mem_phi(val, ft, phi_type, adr_t, alloc, value_phis, level-1);
450 if (val == NULL) {
451 return NULL;
452 }
453 values.at_put(j, val);
454 } else if (val->Opcode() == Op_SCMemProj) {
455 assert(val->in(0)->is_LoadStore() ||
456 val->in(0)->Opcode() == Op_EncodeISOArray ||
457 val->in(0)->Opcode() == Op_StrCompressedCopy, "sanity");
458 assert(false, "Object is not scalar replaceable if a LoadStore node accesses its field");
459 return NULL;
460 } else if (val->is_ArrayCopy()) {
461 Node* res = make_arraycopy_load(val->as_ArrayCopy(), offset, val->in(0), val->in(TypeFunc::Memory), ft, phi_type, alloc);
462 if (res == NULL) {
463 return NULL;
464 }
465 values.at_put(j, res);
466 } else {
467#ifdef ASSERT
468 val->dump();
469 assert(false, "unknown node on this path");
470#endif
471 return NULL; // unknown node on this path
472 }
473 }
474 }
475 // Set Phi's inputs
476 for (uint j = 1; j < length; j++) {
477 if (values.at(j) == mem) {
478 phi->init_req(j, phi);
479 } else {
480 phi->init_req(j, values.at(j));
481 }
482 }
483 return phi;
484}
485
486// Search the last value stored into the object's field.
487Node *PhaseMacroExpand::value_from_mem(Node *sfpt_mem, Node *sfpt_ctl, BasicType ft, const Type *ftype, const TypeOopPtr *adr_t, AllocateNode *alloc) {
488 assert(adr_t->is_known_instance_field(), "instance required");
489 int instance_id = adr_t->instance_id();
490 assert((uint)instance_id == alloc->_idx, "wrong allocation");
491
492 int alias_idx = C->get_alias_index(adr_t);
493 int offset = adr_t->offset();
494 Node *start_mem = C->start()->proj_out_or_null(TypeFunc::Memory);
495 Node *alloc_ctrl = alloc->in(TypeFunc::Control);
496 Node *alloc_mem = alloc->in(TypeFunc::Memory);
497 Arena *a = Thread::current()->resource_area();
498 VectorSet visited(a);
499
500
501 bool done = sfpt_mem == alloc_mem;
502 Node *mem = sfpt_mem;
503 while (!done) {
504 if (visited.test_set(mem->_idx)) {
505 return NULL; // found a loop, give up
506 }
507 mem = scan_mem_chain(mem, alias_idx, offset, start_mem, alloc, &_igvn);
508 if (mem == start_mem || mem == alloc_mem) {
509 done = true; // hit a sentinel, return appropriate 0 value
510 } else if (mem->is_Initialize()) {
511 mem = mem->as_Initialize()->find_captured_store(offset, type2aelembytes(ft), &_igvn);
512 if (mem == NULL) {
513 done = true; // Something go wrong.
514 } else if (mem->is_Store()) {
515 const TypePtr* atype = mem->as_Store()->adr_type();
516 assert(C->get_alias_index(atype) == Compile::AliasIdxRaw, "store is correct memory slice");
517 done = true;
518 }
519 } else if (mem->is_Store()) {
520 const TypeOopPtr* atype = mem->as_Store()->adr_type()->isa_oopptr();
521 assert(atype != NULL, "address type must be oopptr");
522 assert(C->get_alias_index(atype) == alias_idx &&
523 atype->is_known_instance_field() && atype->offset() == offset &&
524 atype->instance_id() == instance_id, "store is correct memory slice");
525 done = true;
526 } else if (mem->is_Phi()) {
527 // try to find a phi's unique input
528 Node *unique_input = NULL;
529 Node *top = C->top();
530 for (uint i = 1; i < mem->req(); i++) {
531 Node *n = scan_mem_chain(mem->in(i), alias_idx, offset, start_mem, alloc, &_igvn);
532 if (n == NULL || n == top || n == mem) {
533 continue;
534 } else if (unique_input == NULL) {
535 unique_input = n;
536 } else if (unique_input != n) {
537 unique_input = top;
538 break;
539 }
540 }
541 if (unique_input != NULL && unique_input != top) {
542 mem = unique_input;
543 } else {
544 done = true;
545 }
546 } else if (mem->is_ArrayCopy()) {
547 done = true;
548 } else {
549 assert(false, "unexpected node");
550 }
551 }
552 if (mem != NULL) {
553 if (mem == start_mem || mem == alloc_mem) {
554 // hit a sentinel, return appropriate 0 value
555 return _igvn.zerocon(ft);
556 } else if (mem->is_Store()) {
557 Node* n = mem->in(MemNode::ValueIn);
558 BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
559 n = bs->step_over_gc_barrier(n);
560 return n;
561 } else if (mem->is_Phi()) {
562 // attempt to produce a Phi reflecting the values on the input paths of the Phi
563 Node_Stack value_phis(a, 8);
564 Node * phi = value_from_mem_phi(mem, ft, ftype, adr_t, alloc, &value_phis, ValueSearchLimit);
565 if (phi != NULL) {
566 return phi;
567 } else {
568 // Kill all new Phis
569 while(value_phis.is_nonempty()) {
570 Node* n = value_phis.node();
571 _igvn.replace_node(n, C->top());
572 value_phis.pop();
573 }
574 }
575 } else if (mem->is_ArrayCopy()) {
576 Node* ctl = mem->in(0);
577 Node* m = mem->in(TypeFunc::Memory);
578 if (sfpt_ctl->is_Proj() && sfpt_ctl->as_Proj()->is_uncommon_trap_proj(Deoptimization::Reason_none)) {
579 // pin the loads in the uncommon trap path
580 ctl = sfpt_ctl;
581 m = sfpt_mem;
582 }
583 return make_arraycopy_load(mem->as_ArrayCopy(), offset, ctl, m, ft, ftype, alloc);
584 }
585 }
586 // Something go wrong.
587 return NULL;
588}
589
590// Check the possibility of scalar replacement.
591bool PhaseMacroExpand::can_eliminate_allocation(AllocateNode *alloc, GrowableArray <SafePointNode *>& safepoints) {
592 // Scan the uses of the allocation to check for anything that would
593 // prevent us from eliminating it.
594 NOT_PRODUCT( const char* fail_eliminate = NULL; )
595 DEBUG_ONLY( Node* disq_node = NULL; )
596 bool can_eliminate = true;
597
598 Node* res = alloc->result_cast();
599 const TypeOopPtr* res_type = NULL;
600 if (res == NULL) {
601 // All users were eliminated.
602 } else if (!res->is_CheckCastPP()) {
603 NOT_PRODUCT(fail_eliminate = "Allocation does not have unique CheckCastPP";)
604 can_eliminate = false;
605 } else {
606 res_type = _igvn.type(res)->isa_oopptr();
607 if (res_type == NULL) {
608 NOT_PRODUCT(fail_eliminate = "Neither instance or array allocation";)
609 can_eliminate = false;
610 } else if (res_type->isa_aryptr()) {
611 int length = alloc->in(AllocateNode::ALength)->find_int_con(-1);
612 if (length < 0) {
613 NOT_PRODUCT(fail_eliminate = "Array's size is not constant";)
614 can_eliminate = false;
615 }
616 }
617 }
618
619 if (can_eliminate && res != NULL) {
620 for (DUIterator_Fast jmax, j = res->fast_outs(jmax);
621 j < jmax && can_eliminate; j++) {
622 Node* use = res->fast_out(j);
623
624 if (use->is_AddP()) {
625 const TypePtr* addp_type = _igvn.type(use)->is_ptr();
626 int offset = addp_type->offset();
627
628 if (offset == Type::OffsetTop || offset == Type::OffsetBot) {
629 NOT_PRODUCT(fail_eliminate = "Undefined field referrence";)
630 can_eliminate = false;
631 break;
632 }
633 for (DUIterator_Fast kmax, k = use->fast_outs(kmax);
634 k < kmax && can_eliminate; k++) {
635 Node* n = use->fast_out(k);
636 if (!n->is_Store() && n->Opcode() != Op_CastP2X &&
637 SHENANDOAHGC_ONLY((!UseShenandoahGC || !ShenandoahBarrierSetC2::is_shenandoah_wb_pre_call(n)) &&)
638 !(n->is_ArrayCopy() &&
639 n->as_ArrayCopy()->is_clonebasic() &&
640 n->in(ArrayCopyNode::Dest) == use)) {
641 DEBUG_ONLY(disq_node = n;)
642 if (n->is_Load() || n->is_LoadStore()) {
643 NOT_PRODUCT(fail_eliminate = "Field load";)
644 } else {
645 NOT_PRODUCT(fail_eliminate = "Not store field referrence";)
646 }
647 can_eliminate = false;
648 }
649 }
650 } else if (use->is_ArrayCopy() &&
651 (use->as_ArrayCopy()->is_arraycopy_validated() ||
652 use->as_ArrayCopy()->is_copyof_validated() ||
653 use->as_ArrayCopy()->is_copyofrange_validated()) &&
654 use->in(ArrayCopyNode::Dest) == res) {
655 // ok to eliminate
656 } else if (use->is_SafePoint()) {
657 SafePointNode* sfpt = use->as_SafePoint();
658 if (sfpt->is_Call() && sfpt->as_Call()->has_non_debug_use(res)) {
659 // Object is passed as argument.
660 DEBUG_ONLY(disq_node = use;)
661 NOT_PRODUCT(fail_eliminate = "Object is passed as argument";)
662 can_eliminate = false;
663 }
664 Node* sfptMem = sfpt->memory();
665 if (sfptMem == NULL || sfptMem->is_top()) {
666 DEBUG_ONLY(disq_node = use;)
667 NOT_PRODUCT(fail_eliminate = "NULL or TOP memory";)
668 can_eliminate = false;
669 } else {
670 safepoints.append_if_missing(sfpt);
671 }
672 } else if (use->Opcode() != Op_CastP2X) { // CastP2X is used by card mark
673 if (use->is_Phi()) {
674 if (use->outcnt() == 1 && use->unique_out()->Opcode() == Op_Return) {
675 NOT_PRODUCT(fail_eliminate = "Object is return value";)
676 } else {
677 NOT_PRODUCT(fail_eliminate = "Object is referenced by Phi";)
678 }
679 DEBUG_ONLY(disq_node = use;)
680 } else {
681 if (use->Opcode() == Op_Return) {
682 NOT_PRODUCT(fail_eliminate = "Object is return value";)
683 }else {
684 NOT_PRODUCT(fail_eliminate = "Object is referenced by node";)
685 }
686 DEBUG_ONLY(disq_node = use;)
687 }
688 can_eliminate = false;
689 }
690 }
691 }
692
693#ifndef PRODUCT
694 if (PrintEliminateAllocations) {
695 if (can_eliminate) {
696 tty->print("Scalar ");
697 if (res == NULL)
698 alloc->dump();
699 else
700 res->dump();
701 } else if (alloc->_is_scalar_replaceable) {
702 tty->print("NotScalar (%s)", fail_eliminate);
703 if (res == NULL)
704 alloc->dump();
705 else
706 res->dump();
707#ifdef ASSERT
708 if (disq_node != NULL) {
709 tty->print(" >>>> ");
710 disq_node->dump();
711 }
712#endif /*ASSERT*/
713 }
714 }
715#endif
716 return can_eliminate;
717}
718
719// Do scalar replacement.
720bool PhaseMacroExpand::scalar_replacement(AllocateNode *alloc, GrowableArray <SafePointNode *>& safepoints) {
721 GrowableArray <SafePointNode *> safepoints_done;
722
723 ciKlass* klass = NULL;
724 ciInstanceKlass* iklass = NULL;
725 int nfields = 0;
726 int array_base = 0;
727 int element_size = 0;
728 BasicType basic_elem_type = T_ILLEGAL;
729 ciType* elem_type = NULL;
730
731 Node* res = alloc->result_cast();
732 assert(res == NULL || res->is_CheckCastPP(), "unexpected AllocateNode result");
733 const TypeOopPtr* res_type = NULL;
734 if (res != NULL) { // Could be NULL when there are no users
735 res_type = _igvn.type(res)->isa_oopptr();
736 }
737
738 if (res != NULL) {
739 klass = res_type->klass();
740 if (res_type->isa_instptr()) {
741 // find the fields of the class which will be needed for safepoint debug information
742 assert(klass->is_instance_klass(), "must be an instance klass.");
743 iklass = klass->as_instance_klass();
744 nfields = iklass->nof_nonstatic_fields();
745 } else {
746 // find the array's elements which will be needed for safepoint debug information
747 nfields = alloc->in(AllocateNode::ALength)->find_int_con(-1);
748 assert(klass->is_array_klass() && nfields >= 0, "must be an array klass.");
749 elem_type = klass->as_array_klass()->element_type();
750 basic_elem_type = elem_type->basic_type();
751 array_base = arrayOopDesc::base_offset_in_bytes(basic_elem_type);
752 element_size = type2aelembytes(basic_elem_type);
753 }
754 }
755 //
756 // Process the safepoint uses
757 //
758 while (safepoints.length() > 0) {
759 SafePointNode* sfpt = safepoints.pop();
760 Node* mem = sfpt->memory();
761 Node* ctl = sfpt->control();
762 assert(sfpt->jvms() != NULL, "missed JVMS");
763 // Fields of scalar objs are referenced only at the end
764 // of regular debuginfo at the last (youngest) JVMS.
765 // Record relative start index.
766 uint first_ind = (sfpt->req() - sfpt->jvms()->scloff());
767 SafePointScalarObjectNode* sobj = new SafePointScalarObjectNode(res_type,
768#ifdef ASSERT
769 alloc,
770#endif
771 first_ind, nfields);
772 sobj->init_req(0, C->root());
773 transform_later(sobj);
774
775 // Scan object's fields adding an input to the safepoint for each field.
776 for (int j = 0; j < nfields; j++) {
777 intptr_t offset;
778 ciField* field = NULL;
779 if (iklass != NULL) {
780 field = iklass->nonstatic_field_at(j);
781 offset = field->offset();
782 elem_type = field->type();
783 basic_elem_type = field->layout_type();
784 } else {
785 offset = array_base + j * (intptr_t)element_size;
786 }
787
788 const Type *field_type;
789 // The next code is taken from Parse::do_get_xxx().
790 if (basic_elem_type == T_OBJECT || basic_elem_type == T_ARRAY) {
791 if (!elem_type->is_loaded()) {
792 field_type = TypeInstPtr::BOTTOM;
793 } else if (field != NULL && field->is_static_constant()) {
794 // This can happen if the constant oop is non-perm.
795 ciObject* con = field->constant_value().as_object();
796 // Do not "join" in the previous type; it doesn't add value,
797 // and may yield a vacuous result if the field is of interface type.
798 field_type = TypeOopPtr::make_from_constant(con)->isa_oopptr();
799 assert(field_type != NULL, "field singleton type must be consistent");
800 } else {
801 field_type = TypeOopPtr::make_from_klass(elem_type->as_klass());
802 }
803 if (UseCompressedOops) {
804 field_type = field_type->make_narrowoop();
805 basic_elem_type = T_NARROWOOP;
806 }
807 } else {
808 field_type = Type::get_const_basic_type(basic_elem_type);
809 }
810
811 const TypeOopPtr *field_addr_type = res_type->add_offset(offset)->isa_oopptr();
812
813 Node *field_val = value_from_mem(mem, ctl, basic_elem_type, field_type, field_addr_type, alloc);
814 if (field_val == NULL) {
815 // We weren't able to find a value for this field,
816 // give up on eliminating this allocation.
817
818 // Remove any extra entries we added to the safepoint.
819 uint last = sfpt->req() - 1;
820 for (int k = 0; k < j; k++) {
821 sfpt->del_req(last--);
822 }
823 _igvn._worklist.push(sfpt);
824 // rollback processed safepoints
825 while (safepoints_done.length() > 0) {
826 SafePointNode* sfpt_done = safepoints_done.pop();
827 // remove any extra entries we added to the safepoint
828 last = sfpt_done->req() - 1;
829 for (int k = 0; k < nfields; k++) {
830 sfpt_done->del_req(last--);
831 }
832 JVMState *jvms = sfpt_done->jvms();
833 jvms->set_endoff(sfpt_done->req());
834 // Now make a pass over the debug information replacing any references
835 // to SafePointScalarObjectNode with the allocated object.
836 int start = jvms->debug_start();
837 int end = jvms->debug_end();
838 for (int i = start; i < end; i++) {
839 if (sfpt_done->in(i)->is_SafePointScalarObject()) {
840 SafePointScalarObjectNode* scobj = sfpt_done->in(i)->as_SafePointScalarObject();
841 if (scobj->first_index(jvms) == sfpt_done->req() &&
842 scobj->n_fields() == (uint)nfields) {
843 assert(scobj->alloc() == alloc, "sanity");
844 sfpt_done->set_req(i, res);
845 }
846 }
847 }
848 _igvn._worklist.push(sfpt_done);
849 }
850#ifndef PRODUCT
851 if (PrintEliminateAllocations) {
852 if (field != NULL) {
853 tty->print("=== At SafePoint node %d can't find value of Field: ",
854 sfpt->_idx);
855 field->print();
856 int field_idx = C->get_alias_index(field_addr_type);
857 tty->print(" (alias_idx=%d)", field_idx);
858 } else { // Array's element
859 tty->print("=== At SafePoint node %d can't find value of array element [%d]",
860 sfpt->_idx, j);
861 }
862 tty->print(", which prevents elimination of: ");
863 if (res == NULL)
864 alloc->dump();
865 else
866 res->dump();
867 }
868#endif
869 return false;
870 }
871 if (UseCompressedOops && field_type->isa_narrowoop()) {
872 // Enable "DecodeN(EncodeP(Allocate)) --> Allocate" transformation
873 // to be able scalar replace the allocation.
874 if (field_val->is_EncodeP()) {
875 field_val = field_val->in(1);
876 } else {
877 field_val = transform_later(new DecodeNNode(field_val, field_val->get_ptr_type()));
878 }
879 }
880 sfpt->add_req(field_val);
881 }
882 JVMState *jvms = sfpt->jvms();
883 jvms->set_endoff(sfpt->req());
884 // Now make a pass over the debug information replacing any references
885 // to the allocated object with "sobj"
886 int start = jvms->debug_start();
887 int end = jvms->debug_end();
888 sfpt->replace_edges_in_range(res, sobj, start, end);
889 _igvn._worklist.push(sfpt);
890 safepoints_done.append_if_missing(sfpt); // keep it for rollback
891 }
892 return true;
893}
894
895static void disconnect_projections(MultiNode* n, PhaseIterGVN& igvn) {
896 Node* ctl_proj = n->proj_out_or_null(TypeFunc::Control);
897 Node* mem_proj = n->proj_out_or_null(TypeFunc::Memory);
898 if (ctl_proj != NULL) {
899 igvn.replace_node(ctl_proj, n->in(0));
900 }
901 if (mem_proj != NULL) {
902 igvn.replace_node(mem_proj, n->in(TypeFunc::Memory));
903 }
904}
905
906// Process users of eliminated allocation.
907void PhaseMacroExpand::process_users_of_allocation(CallNode *alloc) {
908 Node* res = alloc->result_cast();
909 if (res != NULL) {
910 for (DUIterator_Last jmin, j = res->last_outs(jmin); j >= jmin; ) {
911 Node *use = res->last_out(j);
912 uint oc1 = res->outcnt();
913
914 if (use->is_AddP()) {
915 for (DUIterator_Last kmin, k = use->last_outs(kmin); k >= kmin; ) {
916 Node *n = use->last_out(k);
917 uint oc2 = use->outcnt();
918 if (n->is_Store()) {
919#ifdef ASSERT
920 // Verify that there is no dependent MemBarVolatile nodes,
921 // they should be removed during IGVN, see MemBarNode::Ideal().
922 for (DUIterator_Fast pmax, p = n->fast_outs(pmax);
923 p < pmax; p++) {
924 Node* mb = n->fast_out(p);
925 assert(mb->is_Initialize() || !mb->is_MemBar() ||
926 mb->req() <= MemBarNode::Precedent ||
927 mb->in(MemBarNode::Precedent) != n,
928 "MemBarVolatile should be eliminated for non-escaping object");
929 }
930#endif
931 _igvn.replace_node(n, n->in(MemNode::Memory));
932 } else if (n->is_ArrayCopy()) {
933 // Disconnect ArrayCopy node
934 ArrayCopyNode* ac = n->as_ArrayCopy();
935 assert(ac->is_clonebasic(), "unexpected array copy kind");
936 Node* membar_after = ac->proj_out(TypeFunc::Control)->unique_ctrl_out();
937 disconnect_projections(ac, _igvn);
938 assert(alloc->in(0)->is_Proj() && alloc->in(0)->in(0)->Opcode() == Op_MemBarCPUOrder, "mem barrier expected before allocation");
939 Node* membar_before = alloc->in(0)->in(0);
940 disconnect_projections(membar_before->as_MemBar(), _igvn);
941 if (membar_after->is_MemBar()) {
942 disconnect_projections(membar_after->as_MemBar(), _igvn);
943 }
944 } else {
945 eliminate_gc_barrier(n);
946 }
947 k -= (oc2 - use->outcnt());
948 }
949 _igvn.remove_dead_node(use);
950 } else if (use->is_ArrayCopy()) {
951 // Disconnect ArrayCopy node
952 ArrayCopyNode* ac = use->as_ArrayCopy();
953 assert(ac->is_arraycopy_validated() ||
954 ac->is_copyof_validated() ||
955 ac->is_copyofrange_validated(), "unsupported");
956 CallProjections callprojs;
957 ac->extract_projections(&callprojs, true);
958
959 _igvn.replace_node(callprojs.fallthrough_ioproj, ac->in(TypeFunc::I_O));
960 _igvn.replace_node(callprojs.fallthrough_memproj, ac->in(TypeFunc::Memory));
961 _igvn.replace_node(callprojs.fallthrough_catchproj, ac->in(TypeFunc::Control));
962
963 // Set control to top. IGVN will remove the remaining projections
964 ac->set_req(0, top());
965 ac->replace_edge(res, top());
966
967 // Disconnect src right away: it can help find new
968 // opportunities for allocation elimination
969 Node* src = ac->in(ArrayCopyNode::Src);
970 ac->replace_edge(src, top());
971 // src can be top at this point if src and dest of the
972 // arraycopy were the same
973 if (src->outcnt() == 0 && !src->is_top()) {
974 _igvn.remove_dead_node(src);
975 }
976
977 _igvn._worklist.push(ac);
978 } else {
979 eliminate_gc_barrier(use);
980 }
981 j -= (oc1 - res->outcnt());
982 }
983 assert(res->outcnt() == 0, "all uses of allocated objects must be deleted");
984 _igvn.remove_dead_node(res);
985 }
986
987 //
988 // Process other users of allocation's projections
989 //
990 if (_resproj != NULL && _resproj->outcnt() != 0) {
991 // First disconnect stores captured by Initialize node.
992 // If Initialize node is eliminated first in the following code,
993 // it will kill such stores and DUIterator_Last will assert.
994 for (DUIterator_Fast jmax, j = _resproj->fast_outs(jmax); j < jmax; j++) {
995 Node *use = _resproj->fast_out(j);
996 if (use->is_AddP()) {
997 // raw memory addresses used only by the initialization
998 _igvn.replace_node(use, C->top());
999 --j; --jmax;
1000 }
1001 }
1002 for (DUIterator_Last jmin, j = _resproj->last_outs(jmin); j >= jmin; ) {
1003 Node *use = _resproj->last_out(j);
1004 uint oc1 = _resproj->outcnt();
1005 if (use->is_Initialize()) {
1006 // Eliminate Initialize node.
1007 InitializeNode *init = use->as_Initialize();
1008 assert(init->outcnt() <= 2, "only a control and memory projection expected");
1009 Node *ctrl_proj = init->proj_out_or_null(TypeFunc::Control);
1010 if (ctrl_proj != NULL) {
1011 _igvn.replace_node(ctrl_proj, init->in(TypeFunc::Control));
1012#ifdef ASSERT
1013 BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
1014 Node* tmp = init->in(TypeFunc::Control);
1015 while (bs->is_gc_barrier_node(tmp)) {
1016 Node* tmp2 = bs->step_over_gc_barrier_ctrl(tmp);
1017 assert(tmp != tmp2, "Must make progress");
1018 tmp = tmp2;
1019 }
1020 assert(tmp == _fallthroughcatchproj, "allocation control projection");
1021#endif
1022 }
1023 Node *mem_proj = init->proj_out_or_null(TypeFunc::Memory);
1024 if (mem_proj != NULL) {
1025 Node *mem = init->in(TypeFunc::Memory);
1026#ifdef ASSERT
1027 if (mem->is_MergeMem()) {
1028 assert(mem->in(TypeFunc::Memory) == _memproj_fallthrough, "allocation memory projection");
1029 } else {
1030 assert(mem == _memproj_fallthrough, "allocation memory projection");
1031 }
1032#endif
1033 _igvn.replace_node(mem_proj, mem);
1034 }
1035 } else {
1036 assert(false, "only Initialize or AddP expected");
1037 }
1038 j -= (oc1 - _resproj->outcnt());
1039 }
1040 }
1041 if (_fallthroughcatchproj != NULL) {
1042 _igvn.replace_node(_fallthroughcatchproj, alloc->in(TypeFunc::Control));
1043 }
1044 if (_memproj_fallthrough != NULL) {
1045 _igvn.replace_node(_memproj_fallthrough, alloc->in(TypeFunc::Memory));
1046 }
1047 if (_memproj_catchall != NULL) {
1048 _igvn.replace_node(_memproj_catchall, C->top());
1049 }
1050 if (_ioproj_fallthrough != NULL) {
1051 _igvn.replace_node(_ioproj_fallthrough, alloc->in(TypeFunc::I_O));
1052 }
1053 if (_ioproj_catchall != NULL) {
1054 _igvn.replace_node(_ioproj_catchall, C->top());
1055 }
1056 if (_catchallcatchproj != NULL) {
1057 _igvn.replace_node(_catchallcatchproj, C->top());
1058 }
1059}
1060
1061bool PhaseMacroExpand::eliminate_allocate_node(AllocateNode *alloc) {
1062 // Don't do scalar replacement if the frame can be popped by JVMTI:
1063 // if reallocation fails during deoptimization we'll pop all
1064 // interpreter frames for this compiled frame and that won't play
1065 // nice with JVMTI popframe.
1066 if (!EliminateAllocations || JvmtiExport::can_pop_frame() || !alloc->_is_non_escaping) {
1067 return false;
1068 }
1069 Node* klass = alloc->in(AllocateNode::KlassNode);
1070 const TypeKlassPtr* tklass = _igvn.type(klass)->is_klassptr();
1071 Node* res = alloc->result_cast();
1072 // Eliminate boxing allocations which are not used
1073 // regardless scalar replacable status.
1074 bool boxing_alloc = C->eliminate_boxing() &&
1075 tklass->klass()->is_instance_klass() &&
1076 tklass->klass()->as_instance_klass()->is_box_klass();
1077 if (!alloc->_is_scalar_replaceable && (!boxing_alloc || (res != NULL))) {
1078 return false;
1079 }
1080
1081 extract_call_projections(alloc);
1082
1083 GrowableArray <SafePointNode *> safepoints;
1084 if (!can_eliminate_allocation(alloc, safepoints)) {
1085 return false;
1086 }
1087
1088 if (!alloc->_is_scalar_replaceable) {
1089 assert(res == NULL, "sanity");
1090 // We can only eliminate allocation if all debug info references
1091 // are already replaced with SafePointScalarObject because
1092 // we can't search for a fields value without instance_id.
1093 if (safepoints.length() > 0) {
1094 return false;
1095 }
1096 }
1097
1098 if (!scalar_replacement(alloc, safepoints)) {
1099 return false;
1100 }
1101
1102 CompileLog* log = C->log();
1103 if (log != NULL) {
1104 log->head("eliminate_allocation type='%d'",
1105 log->identify(tklass->klass()));
1106 JVMState* p = alloc->jvms();
1107 while (p != NULL) {
1108 log->elem("jvms bci='%d' method='%d'", p->bci(), log->identify(p->method()));
1109 p = p->caller();
1110 }
1111 log->tail("eliminate_allocation");
1112 }
1113
1114 process_users_of_allocation(alloc);
1115
1116#ifndef PRODUCT
1117 if (PrintEliminateAllocations) {
1118 if (alloc->is_AllocateArray())
1119 tty->print_cr("++++ Eliminated: %d AllocateArray", alloc->_idx);
1120 else
1121 tty->print_cr("++++ Eliminated: %d Allocate", alloc->_idx);
1122 }
1123#endif
1124
1125 return true;
1126}
1127
1128bool PhaseMacroExpand::eliminate_boxing_node(CallStaticJavaNode *boxing) {
1129 // EA should remove all uses of non-escaping boxing node.
1130 if (!C->eliminate_boxing() || boxing->proj_out_or_null(TypeFunc::Parms) != NULL) {
1131 return false;
1132 }
1133
1134 assert(boxing->result_cast() == NULL, "unexpected boxing node result");
1135
1136 extract_call_projections(boxing);
1137
1138 const TypeTuple* r = boxing->tf()->range();
1139 assert(r->cnt() > TypeFunc::Parms, "sanity");
1140 const TypeInstPtr* t = r->field_at(TypeFunc::Parms)->isa_instptr();
1141 assert(t != NULL, "sanity");
1142
1143 CompileLog* log = C->log();
1144 if (log != NULL) {
1145 log->head("eliminate_boxing type='%d'",
1146 log->identify(t->klass()));
1147 JVMState* p = boxing->jvms();
1148 while (p != NULL) {
1149 log->elem("jvms bci='%d' method='%d'", p->bci(), log->identify(p->method()));
1150 p = p->caller();
1151 }
1152 log->tail("eliminate_boxing");
1153 }
1154
1155 process_users_of_allocation(boxing);
1156
1157#ifndef PRODUCT
1158 if (PrintEliminateAllocations) {
1159 tty->print("++++ Eliminated: %d ", boxing->_idx);
1160 boxing->method()->print_short_name(tty);
1161 tty->cr();
1162 }
1163#endif
1164
1165 return true;
1166}
1167
1168//---------------------------set_eden_pointers-------------------------
1169void PhaseMacroExpand::set_eden_pointers(Node* &eden_top_adr, Node* &eden_end_adr) {
1170 if (UseTLAB) { // Private allocation: load from TLS
1171 Node* thread = transform_later(new ThreadLocalNode());
1172 int tlab_top_offset = in_bytes(JavaThread::tlab_top_offset());
1173 int tlab_end_offset = in_bytes(JavaThread::tlab_end_offset());
1174 eden_top_adr = basic_plus_adr(top()/*not oop*/, thread, tlab_top_offset);
1175 eden_end_adr = basic_plus_adr(top()/*not oop*/, thread, tlab_end_offset);
1176 } else { // Shared allocation: load from globals
1177 CollectedHeap* ch = Universe::heap();
1178 address top_adr = (address)ch->top_addr();
1179 address end_adr = (address)ch->end_addr();
1180 eden_top_adr = makecon(TypeRawPtr::make(top_adr));
1181 eden_end_adr = basic_plus_adr(eden_top_adr, end_adr - top_adr);
1182 }
1183}
1184
1185
1186Node* PhaseMacroExpand::make_load(Node* ctl, Node* mem, Node* base, int offset, const Type* value_type, BasicType bt) {
1187 Node* adr = basic_plus_adr(base, offset);
1188 const TypePtr* adr_type = adr->bottom_type()->is_ptr();
1189 Node* value = LoadNode::make(_igvn, ctl, mem, adr, adr_type, value_type, bt, MemNode::unordered);
1190 transform_later(value);
1191 return value;
1192}
1193
1194
1195Node* PhaseMacroExpand::make_store(Node* ctl, Node* mem, Node* base, int offset, Node* value, BasicType bt) {
1196 Node* adr = basic_plus_adr(base, offset);
1197 mem = StoreNode::make(_igvn, ctl, mem, adr, NULL, value, bt, MemNode::unordered);
1198 transform_later(mem);
1199 return mem;
1200}
1201
1202//=============================================================================
1203//
1204// A L L O C A T I O N
1205//
1206// Allocation attempts to be fast in the case of frequent small objects.
1207// It breaks down like this:
1208//
1209// 1) Size in doublewords is computed. This is a constant for objects and
1210// variable for most arrays. Doubleword units are used to avoid size
1211// overflow of huge doubleword arrays. We need doublewords in the end for
1212// rounding.
1213//
1214// 2) Size is checked for being 'too large'. Too-large allocations will go
1215// the slow path into the VM. The slow path can throw any required
1216// exceptions, and does all the special checks for very large arrays. The
1217// size test can constant-fold away for objects. For objects with
1218// finalizers it constant-folds the otherway: you always go slow with
1219// finalizers.
1220//
1221// 3) If NOT using TLABs, this is the contended loop-back point.
1222// Load-Locked the heap top. If using TLABs normal-load the heap top.
1223//
1224// 4) Check that heap top + size*8 < max. If we fail go the slow ` route.
1225// NOTE: "top+size*8" cannot wrap the 4Gig line! Here's why: for largish
1226// "size*8" we always enter the VM, where "largish" is a constant picked small
1227// enough that there's always space between the eden max and 4Gig (old space is
1228// there so it's quite large) and large enough that the cost of entering the VM
1229// is dwarfed by the cost to initialize the space.
1230//
1231// 5) If NOT using TLABs, Store-Conditional the adjusted heap top back
1232// down. If contended, repeat at step 3. If using TLABs normal-store
1233// adjusted heap top back down; there is no contention.
1234//
1235// 6) If !ZeroTLAB then Bulk-clear the object/array. Fill in klass & mark
1236// fields.
1237//
1238// 7) Merge with the slow-path; cast the raw memory pointer to the correct
1239// oop flavor.
1240//
1241//=============================================================================
1242// FastAllocateSizeLimit value is in DOUBLEWORDS.
1243// Allocations bigger than this always go the slow route.
1244// This value must be small enough that allocation attempts that need to
1245// trigger exceptions go the slow route. Also, it must be small enough so
1246// that heap_top + size_in_bytes does not wrap around the 4Gig limit.
1247//=============================================================================j//
1248// %%% Here is an old comment from parseHelper.cpp; is it outdated?
1249// The allocator will coalesce int->oop copies away. See comment in
1250// coalesce.cpp about how this works. It depends critically on the exact
1251// code shape produced here, so if you are changing this code shape
1252// make sure the GC info for the heap-top is correct in and around the
1253// slow-path call.
1254//
1255
1256void PhaseMacroExpand::expand_allocate_common(
1257 AllocateNode* alloc, // allocation node to be expanded
1258 Node* length, // array length for an array allocation
1259 const TypeFunc* slow_call_type, // Type of slow call
1260 address slow_call_address // Address of slow call
1261 )
1262{
1263
1264 Node* ctrl = alloc->in(TypeFunc::Control);
1265 Node* mem = alloc->in(TypeFunc::Memory);
1266 Node* i_o = alloc->in(TypeFunc::I_O);
1267 Node* size_in_bytes = alloc->in(AllocateNode::AllocSize);
1268 Node* klass_node = alloc->in(AllocateNode::KlassNode);
1269 Node* initial_slow_test = alloc->in(AllocateNode::InitialTest);
1270
1271 assert(ctrl != NULL, "must have control");
1272 // We need a Region and corresponding Phi's to merge the slow-path and fast-path results.
1273 // they will not be used if "always_slow" is set
1274 enum { slow_result_path = 1, fast_result_path = 2 };
1275 Node *result_region = NULL;
1276 Node *result_phi_rawmem = NULL;
1277 Node *result_phi_rawoop = NULL;
1278 Node *result_phi_i_o = NULL;
1279
1280 // The initial slow comparison is a size check, the comparison
1281 // we want to do is a BoolTest::gt
1282 bool always_slow = false;
1283 int tv = _igvn.find_int_con(initial_slow_test, -1);
1284 if (tv >= 0) {
1285 always_slow = (tv == 1);
1286 initial_slow_test = NULL;
1287 } else {
1288 initial_slow_test = BoolNode::make_predicate(initial_slow_test, &_igvn);
1289 }
1290
1291 if (C->env()->dtrace_alloc_probes() ||
1292 (!UseTLAB && !Universe::heap()->supports_inline_contig_alloc())) {
1293 // Force slow-path allocation
1294 always_slow = true;
1295 initial_slow_test = NULL;
1296 }
1297
1298
1299 enum { too_big_or_final_path = 1, need_gc_path = 2 };
1300 Node *slow_region = NULL;
1301 Node *toobig_false = ctrl;
1302
1303 assert (initial_slow_test == NULL || !always_slow, "arguments must be consistent");
1304 // generate the initial test if necessary
1305 if (initial_slow_test != NULL ) {
1306 slow_region = new RegionNode(3);
1307
1308 // Now make the initial failure test. Usually a too-big test but
1309 // might be a TRUE for finalizers or a fancy class check for
1310 // newInstance0.
1311 IfNode *toobig_iff = new IfNode(ctrl, initial_slow_test, PROB_MIN, COUNT_UNKNOWN);
1312 transform_later(toobig_iff);
1313 // Plug the failing-too-big test into the slow-path region
1314 Node *toobig_true = new IfTrueNode( toobig_iff );
1315 transform_later(toobig_true);
1316 slow_region ->init_req( too_big_or_final_path, toobig_true );
1317 toobig_false = new IfFalseNode( toobig_iff );
1318 transform_later(toobig_false);
1319 } else { // No initial test, just fall into next case
1320 toobig_false = ctrl;
1321 debug_only(slow_region = NodeSentinel);
1322 }
1323
1324 Node *slow_mem = mem; // save the current memory state for slow path
1325 // generate the fast allocation code unless we know that the initial test will always go slow
1326 if (!always_slow) {
1327 // Fast path modifies only raw memory.
1328 if (mem->is_MergeMem()) {
1329 mem = mem->as_MergeMem()->memory_at(Compile::AliasIdxRaw);
1330 }
1331
1332 // allocate the Region and Phi nodes for the result
1333 result_region = new RegionNode(3);
1334 result_phi_rawmem = new PhiNode(result_region, Type::MEMORY, TypeRawPtr::BOTTOM);
1335 result_phi_rawoop = new PhiNode(result_region, TypeRawPtr::BOTTOM);
1336 result_phi_i_o = new PhiNode(result_region, Type::ABIO); // I/O is used for Prefetch
1337
1338 // Grab regular I/O before optional prefetch may change it.
1339 // Slow-path does no I/O so just set it to the original I/O.
1340 result_phi_i_o->init_req(slow_result_path, i_o);
1341
1342 Node* needgc_ctrl = NULL;
1343 // Name successful fast-path variables
1344 Node* fast_oop_ctrl;
1345 Node* fast_oop_rawmem;
1346
1347 intx prefetch_lines = length != NULL ? AllocatePrefetchLines : AllocateInstancePrefetchLines;
1348
1349 BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
1350 Node* fast_oop = bs->obj_allocate(this, ctrl, mem, toobig_false, size_in_bytes, i_o, needgc_ctrl,
1351 fast_oop_ctrl, fast_oop_rawmem,
1352 prefetch_lines);
1353
1354 if (initial_slow_test) {
1355 slow_region->init_req(need_gc_path, needgc_ctrl);
1356 // This completes all paths into the slow merge point
1357 transform_later(slow_region);
1358 } else { // No initial slow path needed!
1359 // Just fall from the need-GC path straight into the VM call.
1360 slow_region = needgc_ctrl;
1361 }
1362
1363 InitializeNode* init = alloc->initialization();
1364 fast_oop_rawmem = initialize_object(alloc,
1365 fast_oop_ctrl, fast_oop_rawmem, fast_oop,
1366 klass_node, length, size_in_bytes);
1367
1368 // If initialization is performed by an array copy, any required
1369 // MemBarStoreStore was already added. If the object does not
1370 // escape no need for a MemBarStoreStore. If the object does not
1371 // escape in its initializer and memory barrier (MemBarStoreStore or
1372 // stronger) is already added at exit of initializer, also no need
1373 // for a MemBarStoreStore. Otherwise we need a MemBarStoreStore
1374 // so that stores that initialize this object can't be reordered
1375 // with a subsequent store that makes this object accessible by
1376 // other threads.
1377 // Other threads include java threads and JVM internal threads
1378 // (for example concurrent GC threads). Current concurrent GC
1379 // implementation: CMS and G1 will not scan newly created object,
1380 // so it's safe to skip storestore barrier when allocation does
1381 // not escape.
1382 if (!alloc->does_not_escape_thread() &&
1383 !alloc->is_allocation_MemBar_redundant() &&
1384 (init == NULL || !init->is_complete_with_arraycopy())) {
1385 if (init == NULL || init->req() < InitializeNode::RawStores) {
1386 // No InitializeNode or no stores captured by zeroing
1387 // elimination. Simply add the MemBarStoreStore after object
1388 // initialization.
1389 MemBarNode* mb = MemBarNode::make(C, Op_MemBarStoreStore, Compile::AliasIdxBot);
1390 transform_later(mb);
1391
1392 mb->init_req(TypeFunc::Memory, fast_oop_rawmem);
1393 mb->init_req(TypeFunc::Control, fast_oop_ctrl);
1394 fast_oop_ctrl = new ProjNode(mb,TypeFunc::Control);
1395 transform_later(fast_oop_ctrl);
1396 fast_oop_rawmem = new ProjNode(mb,TypeFunc::Memory);
1397 transform_later(fast_oop_rawmem);
1398 } else {
1399 // Add the MemBarStoreStore after the InitializeNode so that
1400 // all stores performing the initialization that were moved
1401 // before the InitializeNode happen before the storestore
1402 // barrier.
1403
1404 Node* init_ctrl = init->proj_out_or_null(TypeFunc::Control);
1405 Node* init_mem = init->proj_out_or_null(TypeFunc::Memory);
1406
1407 MemBarNode* mb = MemBarNode::make(C, Op_MemBarStoreStore, Compile::AliasIdxBot);
1408 transform_later(mb);
1409
1410 Node* ctrl = new ProjNode(init,TypeFunc::Control);
1411 transform_later(ctrl);
1412 Node* mem = new ProjNode(init,TypeFunc::Memory);
1413 transform_later(mem);
1414
1415 // The MemBarStoreStore depends on control and memory coming
1416 // from the InitializeNode
1417 mb->init_req(TypeFunc::Memory, mem);
1418 mb->init_req(TypeFunc::Control, ctrl);
1419
1420 ctrl = new ProjNode(mb,TypeFunc::Control);
1421 transform_later(ctrl);
1422 mem = new ProjNode(mb,TypeFunc::Memory);
1423 transform_later(mem);
1424
1425 // All nodes that depended on the InitializeNode for control
1426 // and memory must now depend on the MemBarNode that itself
1427 // depends on the InitializeNode
1428 if (init_ctrl != NULL) {
1429 _igvn.replace_node(init_ctrl, ctrl);
1430 }
1431 if (init_mem != NULL) {
1432 _igvn.replace_node(init_mem, mem);
1433 }
1434 }
1435 }
1436
1437 if (C->env()->dtrace_extended_probes()) {
1438 // Slow-path call
1439 int size = TypeFunc::Parms + 2;
1440 CallLeafNode *call = new CallLeafNode(OptoRuntime::dtrace_object_alloc_Type(),
1441 CAST_FROM_FN_PTR(address, SharedRuntime::dtrace_object_alloc_base),
1442 "dtrace_object_alloc",
1443 TypeRawPtr::BOTTOM);
1444
1445 // Get base of thread-local storage area
1446 Node* thread = new ThreadLocalNode();
1447 transform_later(thread);
1448
1449 call->init_req(TypeFunc::Parms+0, thread);
1450 call->init_req(TypeFunc::Parms+1, fast_oop);
1451 call->init_req(TypeFunc::Control, fast_oop_ctrl);
1452 call->init_req(TypeFunc::I_O , top()); // does no i/o
1453 call->init_req(TypeFunc::Memory , fast_oop_rawmem);
1454 call->init_req(TypeFunc::ReturnAdr, alloc->in(TypeFunc::ReturnAdr));
1455 call->init_req(TypeFunc::FramePtr, alloc->in(TypeFunc::FramePtr));
1456 transform_later(call);
1457 fast_oop_ctrl = new ProjNode(call,TypeFunc::Control);
1458 transform_later(fast_oop_ctrl);
1459 fast_oop_rawmem = new ProjNode(call,TypeFunc::Memory);
1460 transform_later(fast_oop_rawmem);
1461 }
1462
1463 // Plug in the successful fast-path into the result merge point
1464 result_region ->init_req(fast_result_path, fast_oop_ctrl);
1465 result_phi_rawoop->init_req(fast_result_path, fast_oop);
1466 result_phi_i_o ->init_req(fast_result_path, i_o);
1467 result_phi_rawmem->init_req(fast_result_path, fast_oop_rawmem);
1468 } else {
1469 slow_region = ctrl;
1470 result_phi_i_o = i_o; // Rename it to use in the following code.
1471 }
1472
1473 // Generate slow-path call
1474 CallNode *call = new CallStaticJavaNode(slow_call_type, slow_call_address,
1475 OptoRuntime::stub_name(slow_call_address),
1476 alloc->jvms()->bci(),
1477 TypePtr::BOTTOM);
1478 call->init_req( TypeFunc::Control, slow_region );
1479 call->init_req( TypeFunc::I_O , top() ) ; // does no i/o
1480 call->init_req( TypeFunc::Memory , slow_mem ); // may gc ptrs
1481 call->init_req( TypeFunc::ReturnAdr, alloc->in(TypeFunc::ReturnAdr) );
1482 call->init_req( TypeFunc::FramePtr, alloc->in(TypeFunc::FramePtr) );
1483
1484 call->init_req(TypeFunc::Parms+0, klass_node);
1485 if (length != NULL) {
1486 call->init_req(TypeFunc::Parms+1, length);
1487 }
1488
1489 // Copy debug information and adjust JVMState information, then replace
1490 // allocate node with the call
1491 copy_call_debug_info((CallNode *) alloc, call);
1492 if (!always_slow) {
1493 call->set_cnt(PROB_UNLIKELY_MAG(4)); // Same effect as RC_UNCOMMON.
1494 } else {
1495 // Hook i_o projection to avoid its elimination during allocation
1496 // replacement (when only a slow call is generated).
1497 call->set_req(TypeFunc::I_O, result_phi_i_o);
1498 }
1499 _igvn.replace_node(alloc, call);
1500 transform_later(call);
1501
1502 // Identify the output projections from the allocate node and
1503 // adjust any references to them.
1504 // The control and io projections look like:
1505 //
1506 // v---Proj(ctrl) <-----+ v---CatchProj(ctrl)
1507 // Allocate Catch
1508 // ^---Proj(io) <-------+ ^---CatchProj(io)
1509 //
1510 // We are interested in the CatchProj nodes.
1511 //
1512 extract_call_projections(call);
1513
1514 // An allocate node has separate memory projections for the uses on
1515 // the control and i_o paths. Replace the control memory projection with
1516 // result_phi_rawmem (unless we are only generating a slow call when
1517 // both memory projections are combined)
1518 if (!always_slow && _memproj_fallthrough != NULL) {
1519 for (DUIterator_Fast imax, i = _memproj_fallthrough->fast_outs(imax); i < imax; i++) {
1520 Node *use = _memproj_fallthrough->fast_out(i);
1521 _igvn.rehash_node_delayed(use);
1522 imax -= replace_input(use, _memproj_fallthrough, result_phi_rawmem);
1523 // back up iterator
1524 --i;
1525 }
1526 }
1527 // Now change uses of _memproj_catchall to use _memproj_fallthrough and delete
1528 // _memproj_catchall so we end up with a call that has only 1 memory projection.
1529 if (_memproj_catchall != NULL ) {
1530 if (_memproj_fallthrough == NULL) {
1531 _memproj_fallthrough = new ProjNode(call, TypeFunc::Memory);
1532 transform_later(_memproj_fallthrough);
1533 }
1534 for (DUIterator_Fast imax, i = _memproj_catchall->fast_outs(imax); i < imax; i++) {
1535 Node *use = _memproj_catchall->fast_out(i);
1536 _igvn.rehash_node_delayed(use);
1537 imax -= replace_input(use, _memproj_catchall, _memproj_fallthrough);
1538 // back up iterator
1539 --i;
1540 }
1541 assert(_memproj_catchall->outcnt() == 0, "all uses must be deleted");
1542 _igvn.remove_dead_node(_memproj_catchall);
1543 }
1544
1545 // An allocate node has separate i_o projections for the uses on the control
1546 // and i_o paths. Always replace the control i_o projection with result i_o
1547 // otherwise incoming i_o become dead when only a slow call is generated
1548 // (it is different from memory projections where both projections are
1549 // combined in such case).
1550 if (_ioproj_fallthrough != NULL) {
1551 for (DUIterator_Fast imax, i = _ioproj_fallthrough->fast_outs(imax); i < imax; i++) {
1552 Node *use = _ioproj_fallthrough->fast_out(i);
1553 _igvn.rehash_node_delayed(use);
1554 imax -= replace_input(use, _ioproj_fallthrough, result_phi_i_o);
1555 // back up iterator
1556 --i;
1557 }
1558 }
1559 // Now change uses of _ioproj_catchall to use _ioproj_fallthrough and delete
1560 // _ioproj_catchall so we end up with a call that has only 1 i_o projection.
1561 if (_ioproj_catchall != NULL ) {
1562 if (_ioproj_fallthrough == NULL) {
1563 _ioproj_fallthrough = new ProjNode(call, TypeFunc::I_O);
1564 transform_later(_ioproj_fallthrough);
1565 }
1566 for (DUIterator_Fast imax, i = _ioproj_catchall->fast_outs(imax); i < imax; i++) {
1567 Node *use = _ioproj_catchall->fast_out(i);
1568 _igvn.rehash_node_delayed(use);
1569 imax -= replace_input(use, _ioproj_catchall, _ioproj_fallthrough);
1570 // back up iterator
1571 --i;
1572 }
1573 assert(_ioproj_catchall->outcnt() == 0, "all uses must be deleted");
1574 _igvn.remove_dead_node(_ioproj_catchall);
1575 }
1576
1577 // if we generated only a slow call, we are done
1578 if (always_slow) {
1579 // Now we can unhook i_o.
1580 if (result_phi_i_o->outcnt() > 1) {
1581 call->set_req(TypeFunc::I_O, top());
1582 } else {
1583 assert(result_phi_i_o->unique_ctrl_out() == call, "");
1584 // Case of new array with negative size known during compilation.
1585 // AllocateArrayNode::Ideal() optimization disconnect unreachable
1586 // following code since call to runtime will throw exception.
1587 // As result there will be no users of i_o after the call.
1588 // Leave i_o attached to this call to avoid problems in preceding graph.
1589 }
1590 return;
1591 }
1592
1593
1594 if (_fallthroughcatchproj != NULL) {
1595 ctrl = _fallthroughcatchproj->clone();
1596 transform_later(ctrl);
1597 _igvn.replace_node(_fallthroughcatchproj, result_region);
1598 } else {
1599 ctrl = top();
1600 }
1601 Node *slow_result;
1602 if (_resproj == NULL) {
1603 // no uses of the allocation result
1604 slow_result = top();
1605 } else {
1606 slow_result = _resproj->clone();
1607 transform_later(slow_result);
1608 _igvn.replace_node(_resproj, result_phi_rawoop);
1609 }
1610
1611 // Plug slow-path into result merge point
1612 result_region ->init_req( slow_result_path, ctrl );
1613 result_phi_rawoop->init_req( slow_result_path, slow_result);
1614 result_phi_rawmem->init_req( slow_result_path, _memproj_fallthrough );
1615 transform_later(result_region);
1616 transform_later(result_phi_rawoop);
1617 transform_later(result_phi_rawmem);
1618 transform_later(result_phi_i_o);
1619 // This completes all paths into the result merge point
1620}
1621
1622
1623// Helper for PhaseMacroExpand::expand_allocate_common.
1624// Initializes the newly-allocated storage.
1625Node*
1626PhaseMacroExpand::initialize_object(AllocateNode* alloc,
1627 Node* control, Node* rawmem, Node* object,
1628 Node* klass_node, Node* length,
1629 Node* size_in_bytes) {
1630 InitializeNode* init = alloc->initialization();
1631 // Store the klass & mark bits
1632 Node* mark_node = NULL;
1633 // For now only enable fast locking for non-array types
1634 if (UseBiasedLocking && (length == NULL)) {
1635 mark_node = make_load(control, rawmem, klass_node, in_bytes(Klass::prototype_header_offset()), TypeRawPtr::BOTTOM, T_ADDRESS);
1636 } else {
1637 mark_node = makecon(TypeRawPtr::make((address)markOopDesc::prototype()));
1638 }
1639 rawmem = make_store(control, rawmem, object, oopDesc::mark_offset_in_bytes(), mark_node, T_ADDRESS);
1640
1641 rawmem = make_store(control, rawmem, object, oopDesc::klass_offset_in_bytes(), klass_node, T_METADATA);
1642 int header_size = alloc->minimum_header_size(); // conservatively small
1643
1644 // Array length
1645 if (length != NULL) { // Arrays need length field
1646 rawmem = make_store(control, rawmem, object, arrayOopDesc::length_offset_in_bytes(), length, T_INT);
1647 // conservatively small header size:
1648 header_size = arrayOopDesc::base_offset_in_bytes(T_BYTE);
1649 ciKlass* k = _igvn.type(klass_node)->is_klassptr()->klass();
1650 if (k->is_array_klass()) // we know the exact header size in most cases:
1651 header_size = Klass::layout_helper_header_size(k->layout_helper());
1652 }
1653
1654 // Clear the object body, if necessary.
1655 if (init == NULL) {
1656 // The init has somehow disappeared; be cautious and clear everything.
1657 //
1658 // This can happen if a node is allocated but an uncommon trap occurs
1659 // immediately. In this case, the Initialize gets associated with the
1660 // trap, and may be placed in a different (outer) loop, if the Allocate
1661 // is in a loop. If (this is rare) the inner loop gets unrolled, then
1662 // there can be two Allocates to one Initialize. The answer in all these
1663 // edge cases is safety first. It is always safe to clear immediately
1664 // within an Allocate, and then (maybe or maybe not) clear some more later.
1665 if (!(UseTLAB && ZeroTLAB)) {
1666 rawmem = ClearArrayNode::clear_memory(control, rawmem, object,
1667 header_size, size_in_bytes,
1668 &_igvn);
1669 }
1670 } else {
1671 if (!init->is_complete()) {
1672 // Try to win by zeroing only what the init does not store.
1673 // We can also try to do some peephole optimizations,
1674 // such as combining some adjacent subword stores.
1675 rawmem = init->complete_stores(control, rawmem, object,
1676 header_size, size_in_bytes, &_igvn);
1677 }
1678 // We have no more use for this link, since the AllocateNode goes away:
1679 init->set_req(InitializeNode::RawAddress, top());
1680 // (If we keep the link, it just confuses the register allocator,
1681 // who thinks he sees a real use of the address by the membar.)
1682 }
1683
1684 return rawmem;
1685}
1686
1687// Generate prefetch instructions for next allocations.
1688Node* PhaseMacroExpand::prefetch_allocation(Node* i_o, Node*& needgc_false,
1689 Node*& contended_phi_rawmem,
1690 Node* old_eden_top, Node* new_eden_top,
1691 intx lines) {
1692 enum { fall_in_path = 1, pf_path = 2 };
1693 if( UseTLAB && AllocatePrefetchStyle == 2 ) {
1694 // Generate prefetch allocation with watermark check.
1695 // As an allocation hits the watermark, we will prefetch starting
1696 // at a "distance" away from watermark.
1697
1698 Node *pf_region = new RegionNode(3);
1699 Node *pf_phi_rawmem = new PhiNode( pf_region, Type::MEMORY,
1700 TypeRawPtr::BOTTOM );
1701 // I/O is used for Prefetch
1702 Node *pf_phi_abio = new PhiNode( pf_region, Type::ABIO );
1703
1704 Node *thread = new ThreadLocalNode();
1705 transform_later(thread);
1706
1707 Node *eden_pf_adr = new AddPNode( top()/*not oop*/, thread,
1708 _igvn.MakeConX(in_bytes(JavaThread::tlab_pf_top_offset())) );
1709 transform_later(eden_pf_adr);
1710
1711 Node *old_pf_wm = new LoadPNode(needgc_false,
1712 contended_phi_rawmem, eden_pf_adr,
1713 TypeRawPtr::BOTTOM, TypeRawPtr::BOTTOM,
1714 MemNode::unordered);
1715 transform_later(old_pf_wm);
1716
1717 // check against new_eden_top
1718 Node *need_pf_cmp = new CmpPNode( new_eden_top, old_pf_wm );
1719 transform_later(need_pf_cmp);
1720 Node *need_pf_bol = new BoolNode( need_pf_cmp, BoolTest::ge );
1721 transform_later(need_pf_bol);
1722 IfNode *need_pf_iff = new IfNode( needgc_false, need_pf_bol,
1723 PROB_UNLIKELY_MAG(4), COUNT_UNKNOWN );
1724 transform_later(need_pf_iff);
1725
1726 // true node, add prefetchdistance
1727 Node *need_pf_true = new IfTrueNode( need_pf_iff );
1728 transform_later(need_pf_true);
1729
1730 Node *need_pf_false = new IfFalseNode( need_pf_iff );
1731 transform_later(need_pf_false);
1732
1733 Node *new_pf_wmt = new AddPNode( top(), old_pf_wm,
1734 _igvn.MakeConX(AllocatePrefetchDistance) );
1735 transform_later(new_pf_wmt );
1736 new_pf_wmt->set_req(0, need_pf_true);
1737
1738 Node *store_new_wmt = new StorePNode(need_pf_true,
1739 contended_phi_rawmem, eden_pf_adr,
1740 TypeRawPtr::BOTTOM, new_pf_wmt,
1741 MemNode::unordered);
1742 transform_later(store_new_wmt);
1743
1744 // adding prefetches
1745 pf_phi_abio->init_req( fall_in_path, i_o );
1746
1747 Node *prefetch_adr;
1748 Node *prefetch;
1749 uint step_size = AllocatePrefetchStepSize;
1750 uint distance = 0;
1751
1752 for ( intx i = 0; i < lines; i++ ) {
1753 prefetch_adr = new AddPNode( old_pf_wm, new_pf_wmt,
1754 _igvn.MakeConX(distance) );
1755 transform_later(prefetch_adr);
1756 prefetch = new PrefetchAllocationNode( i_o, prefetch_adr );
1757 transform_later(prefetch);
1758 distance += step_size;
1759 i_o = prefetch;
1760 }
1761 pf_phi_abio->set_req( pf_path, i_o );
1762
1763 pf_region->init_req( fall_in_path, need_pf_false );
1764 pf_region->init_req( pf_path, need_pf_true );
1765
1766 pf_phi_rawmem->init_req( fall_in_path, contended_phi_rawmem );
1767 pf_phi_rawmem->init_req( pf_path, store_new_wmt );
1768
1769 transform_later(pf_region);
1770 transform_later(pf_phi_rawmem);
1771 transform_later(pf_phi_abio);
1772
1773 needgc_false = pf_region;
1774 contended_phi_rawmem = pf_phi_rawmem;
1775 i_o = pf_phi_abio;
1776 } else if( UseTLAB && AllocatePrefetchStyle == 3 ) {
1777 // Insert a prefetch instruction for each allocation.
1778 // This code is used to generate 1 prefetch instruction per cache line.
1779
1780 // Generate several prefetch instructions.
1781 uint step_size = AllocatePrefetchStepSize;
1782 uint distance = AllocatePrefetchDistance;
1783
1784 // Next cache address.
1785 Node *cache_adr = new AddPNode(old_eden_top, old_eden_top,
1786 _igvn.MakeConX(step_size + distance));
1787 transform_later(cache_adr);
1788 cache_adr = new CastP2XNode(needgc_false, cache_adr);
1789 transform_later(cache_adr);
1790 // Address is aligned to execute prefetch to the beginning of cache line size
1791 // (it is important when BIS instruction is used on SPARC as prefetch).
1792 Node* mask = _igvn.MakeConX(~(intptr_t)(step_size-1));
1793 cache_adr = new AndXNode(cache_adr, mask);
1794 transform_later(cache_adr);
1795 cache_adr = new CastX2PNode(cache_adr);
1796 transform_later(cache_adr);
1797
1798 // Prefetch
1799 Node *prefetch = new PrefetchAllocationNode( contended_phi_rawmem, cache_adr );
1800 prefetch->set_req(0, needgc_false);
1801 transform_later(prefetch);
1802 contended_phi_rawmem = prefetch;
1803 Node *prefetch_adr;
1804 distance = step_size;
1805 for ( intx i = 1; i < lines; i++ ) {
1806 prefetch_adr = new AddPNode( cache_adr, cache_adr,
1807 _igvn.MakeConX(distance) );
1808 transform_later(prefetch_adr);
1809 prefetch = new PrefetchAllocationNode( contended_phi_rawmem, prefetch_adr );
1810 transform_later(prefetch);
1811 distance += step_size;
1812 contended_phi_rawmem = prefetch;
1813 }
1814 } else if( AllocatePrefetchStyle > 0 ) {
1815 // Insert a prefetch for each allocation only on the fast-path
1816 Node *prefetch_adr;
1817 Node *prefetch;
1818 // Generate several prefetch instructions.
1819 uint step_size = AllocatePrefetchStepSize;
1820 uint distance = AllocatePrefetchDistance;
1821 for ( intx i = 0; i < lines; i++ ) {
1822 prefetch_adr = new AddPNode( old_eden_top, new_eden_top,
1823 _igvn.MakeConX(distance) );
1824 transform_later(prefetch_adr);
1825 prefetch = new PrefetchAllocationNode( i_o, prefetch_adr );
1826 // Do not let it float too high, since if eden_top == eden_end,
1827 // both might be null.
1828 if( i == 0 ) { // Set control for first prefetch, next follows it
1829 prefetch->init_req(0, needgc_false);
1830 }
1831 transform_later(prefetch);
1832 distance += step_size;
1833 i_o = prefetch;
1834 }
1835 }
1836 return i_o;
1837}
1838
1839
1840void PhaseMacroExpand::expand_allocate(AllocateNode *alloc) {
1841 expand_allocate_common(alloc, NULL,
1842 OptoRuntime::new_instance_Type(),
1843 OptoRuntime::new_instance_Java());
1844}
1845
1846void PhaseMacroExpand::expand_allocate_array(AllocateArrayNode *alloc) {
1847 Node* length = alloc->in(AllocateNode::ALength);
1848 InitializeNode* init = alloc->initialization();
1849 Node* klass_node = alloc->in(AllocateNode::KlassNode);
1850 ciKlass* k = _igvn.type(klass_node)->is_klassptr()->klass();
1851 address slow_call_address; // Address of slow call
1852 if (init != NULL && init->is_complete_with_arraycopy() &&
1853 k->is_type_array_klass()) {
1854 // Don't zero type array during slow allocation in VM since
1855 // it will be initialized later by arraycopy in compiled code.
1856 slow_call_address = OptoRuntime::new_array_nozero_Java();
1857 } else {
1858 slow_call_address = OptoRuntime::new_array_Java();
1859 }
1860 expand_allocate_common(alloc, length,
1861 OptoRuntime::new_array_Type(),
1862 slow_call_address);
1863}
1864
1865//-------------------mark_eliminated_box----------------------------------
1866//
1867// During EA obj may point to several objects but after few ideal graph
1868// transformations (CCP) it may point to only one non escaping object
1869// (but still using phi), corresponding locks and unlocks will be marked
1870// for elimination. Later obj could be replaced with a new node (new phi)
1871// and which does not have escape information. And later after some graph
1872// reshape other locks and unlocks (which were not marked for elimination
1873// before) are connected to this new obj (phi) but they still will not be
1874// marked for elimination since new obj has no escape information.
1875// Mark all associated (same box and obj) lock and unlock nodes for
1876// elimination if some of them marked already.
1877void PhaseMacroExpand::mark_eliminated_box(Node* oldbox, Node* obj) {
1878 if (oldbox->as_BoxLock()->is_eliminated())
1879 return; // This BoxLock node was processed already.
1880
1881 // New implementation (EliminateNestedLocks) has separate BoxLock
1882 // node for each locked region so mark all associated locks/unlocks as
1883 // eliminated even if different objects are referenced in one locked region
1884 // (for example, OSR compilation of nested loop inside locked scope).
1885 if (EliminateNestedLocks ||
1886 oldbox->as_BoxLock()->is_simple_lock_region(NULL, obj)) {
1887 // Box is used only in one lock region. Mark this box as eliminated.
1888 _igvn.hash_delete(oldbox);
1889 oldbox->as_BoxLock()->set_eliminated(); // This changes box's hash value
1890 _igvn.hash_insert(oldbox);
1891
1892 for (uint i = 0; i < oldbox->outcnt(); i++) {
1893 Node* u = oldbox->raw_out(i);
1894 if (u->is_AbstractLock() && !u->as_AbstractLock()->is_non_esc_obj()) {
1895 AbstractLockNode* alock = u->as_AbstractLock();
1896 // Check lock's box since box could be referenced by Lock's debug info.
1897 if (alock->box_node() == oldbox) {
1898 // Mark eliminated all related locks and unlocks.
1899#ifdef ASSERT
1900 alock->log_lock_optimization(C, "eliminate_lock_set_non_esc4");
1901#endif
1902 alock->set_non_esc_obj();
1903 }
1904 }
1905 }
1906 return;
1907 }
1908
1909 // Create new "eliminated" BoxLock node and use it in monitor debug info
1910 // instead of oldbox for the same object.
1911 BoxLockNode* newbox = oldbox->clone()->as_BoxLock();
1912
1913 // Note: BoxLock node is marked eliminated only here and it is used
1914 // to indicate that all associated lock and unlock nodes are marked
1915 // for elimination.
1916 newbox->set_eliminated();
1917 transform_later(newbox);
1918
1919 // Replace old box node with new box for all users of the same object.
1920 for (uint i = 0; i < oldbox->outcnt();) {
1921 bool next_edge = true;
1922
1923 Node* u = oldbox->raw_out(i);
1924 if (u->is_AbstractLock()) {
1925 AbstractLockNode* alock = u->as_AbstractLock();
1926 if (alock->box_node() == oldbox && alock->obj_node()->eqv_uncast(obj)) {
1927 // Replace Box and mark eliminated all related locks and unlocks.
1928#ifdef ASSERT
1929 alock->log_lock_optimization(C, "eliminate_lock_set_non_esc5");
1930#endif
1931 alock->set_non_esc_obj();
1932 _igvn.rehash_node_delayed(alock);
1933 alock->set_box_node(newbox);
1934 next_edge = false;
1935 }
1936 }
1937 if (u->is_FastLock() && u->as_FastLock()->obj_node()->eqv_uncast(obj)) {
1938 FastLockNode* flock = u->as_FastLock();
1939 assert(flock->box_node() == oldbox, "sanity");
1940 _igvn.rehash_node_delayed(flock);
1941 flock->set_box_node(newbox);
1942 next_edge = false;
1943 }
1944
1945 // Replace old box in monitor debug info.
1946 if (u->is_SafePoint() && u->as_SafePoint()->jvms()) {
1947 SafePointNode* sfn = u->as_SafePoint();
1948 JVMState* youngest_jvms = sfn->jvms();
1949 int max_depth = youngest_jvms->depth();
1950 for (int depth = 1; depth <= max_depth; depth++) {
1951 JVMState* jvms = youngest_jvms->of_depth(depth);
1952 int num_mon = jvms->nof_monitors();
1953 // Loop over monitors
1954 for (int idx = 0; idx < num_mon; idx++) {
1955 Node* obj_node = sfn->monitor_obj(jvms, idx);
1956 Node* box_node = sfn->monitor_box(jvms, idx);
1957 if (box_node == oldbox && obj_node->eqv_uncast(obj)) {
1958 int j = jvms->monitor_box_offset(idx);
1959 _igvn.replace_input_of(u, j, newbox);
1960 next_edge = false;
1961 }
1962 }
1963 }
1964 }
1965 if (next_edge) i++;
1966 }
1967}
1968
1969//-----------------------mark_eliminated_locking_nodes-----------------------
1970void PhaseMacroExpand::mark_eliminated_locking_nodes(AbstractLockNode *alock) {
1971 if (EliminateNestedLocks) {
1972 if (alock->is_nested()) {
1973 assert(alock->box_node()->as_BoxLock()->is_eliminated(), "sanity");
1974 return;
1975 } else if (!alock->is_non_esc_obj()) { // Not eliminated or coarsened
1976 // Only Lock node has JVMState needed here.
1977 // Not that preceding claim is documented anywhere else.
1978 if (alock->jvms() != NULL) {
1979 if (alock->as_Lock()->is_nested_lock_region()) {
1980 // Mark eliminated related nested locks and unlocks.
1981 Node* obj = alock->obj_node();
1982 BoxLockNode* box_node = alock->box_node()->as_BoxLock();
1983 assert(!box_node->is_eliminated(), "should not be marked yet");
1984 // Note: BoxLock node is marked eliminated only here
1985 // and it is used to indicate that all associated lock
1986 // and unlock nodes are marked for elimination.
1987 box_node->set_eliminated(); // Box's hash is always NO_HASH here
1988 for (uint i = 0; i < box_node->outcnt(); i++) {
1989 Node* u = box_node->raw_out(i);
1990 if (u->is_AbstractLock()) {
1991 alock = u->as_AbstractLock();
1992 if (alock->box_node() == box_node) {
1993 // Verify that this Box is referenced only by related locks.
1994 assert(alock->obj_node()->eqv_uncast(obj), "");
1995 // Mark all related locks and unlocks.
1996#ifdef ASSERT
1997 alock->log_lock_optimization(C, "eliminate_lock_set_nested");
1998#endif
1999 alock->set_nested();
2000 }
2001 }
2002 }
2003 } else {
2004#ifdef ASSERT
2005 alock->log_lock_optimization(C, "eliminate_lock_NOT_nested_lock_region");
2006 if (C->log() != NULL)
2007 alock->as_Lock()->is_nested_lock_region(C); // rerun for debugging output
2008#endif
2009 }
2010 }
2011 return;
2012 }
2013 // Process locks for non escaping object
2014 assert(alock->is_non_esc_obj(), "");
2015 } // EliminateNestedLocks
2016
2017 if (alock->is_non_esc_obj()) { // Lock is used for non escaping object
2018 // Look for all locks of this object and mark them and
2019 // corresponding BoxLock nodes as eliminated.
2020 Node* obj = alock->obj_node();
2021 for (uint j = 0; j < obj->outcnt(); j++) {
2022 Node* o = obj->raw_out(j);
2023 if (o->is_AbstractLock() &&
2024 o->as_AbstractLock()->obj_node()->eqv_uncast(obj)) {
2025 alock = o->as_AbstractLock();
2026 Node* box = alock->box_node();
2027 // Replace old box node with new eliminated box for all users
2028 // of the same object and mark related locks as eliminated.
2029 mark_eliminated_box(box, obj);
2030 }
2031 }
2032 }
2033}
2034
2035// we have determined that this lock/unlock can be eliminated, we simply
2036// eliminate the node without expanding it.
2037//
2038// Note: The membar's associated with the lock/unlock are currently not
2039// eliminated. This should be investigated as a future enhancement.
2040//
2041bool PhaseMacroExpand::eliminate_locking_node(AbstractLockNode *alock) {
2042
2043 if (!alock->is_eliminated()) {
2044 return false;
2045 }
2046#ifdef ASSERT
2047 if (!alock->is_coarsened()) {
2048 // Check that new "eliminated" BoxLock node is created.
2049 BoxLockNode* oldbox = alock->box_node()->as_BoxLock();
2050 assert(oldbox->is_eliminated(), "should be done already");
2051 }
2052#endif
2053
2054 alock->log_lock_optimization(C, "eliminate_lock");
2055
2056#ifndef PRODUCT
2057 if (PrintEliminateLocks) {
2058 if (alock->is_Lock()) {
2059 tty->print_cr("++++ Eliminated: %d Lock", alock->_idx);
2060 } else {
2061 tty->print_cr("++++ Eliminated: %d Unlock", alock->_idx);
2062 }
2063 }
2064#endif
2065
2066 Node* mem = alock->in(TypeFunc::Memory);
2067 Node* ctrl = alock->in(TypeFunc::Control);
2068 guarantee(ctrl != NULL, "missing control projection, cannot replace_node() with NULL");
2069
2070 extract_call_projections(alock);
2071 // There are 2 projections from the lock. The lock node will
2072 // be deleted when its last use is subsumed below.
2073 assert(alock->outcnt() == 2 &&
2074 _fallthroughproj != NULL &&
2075 _memproj_fallthrough != NULL,
2076 "Unexpected projections from Lock/Unlock");
2077
2078 Node* fallthroughproj = _fallthroughproj;
2079 Node* memproj_fallthrough = _memproj_fallthrough;
2080
2081 // The memory projection from a lock/unlock is RawMem
2082 // The input to a Lock is merged memory, so extract its RawMem input
2083 // (unless the MergeMem has been optimized away.)
2084 if (alock->is_Lock()) {
2085 // Seach for MemBarAcquireLock node and delete it also.
2086 MemBarNode* membar = fallthroughproj->unique_ctrl_out()->as_MemBar();
2087 assert(membar != NULL && membar->Opcode() == Op_MemBarAcquireLock, "");
2088 Node* ctrlproj = membar->proj_out(TypeFunc::Control);
2089 Node* memproj = membar->proj_out(TypeFunc::Memory);
2090 _igvn.replace_node(ctrlproj, fallthroughproj);
2091 _igvn.replace_node(memproj, memproj_fallthrough);
2092
2093 // Delete FastLock node also if this Lock node is unique user
2094 // (a loop peeling may clone a Lock node).
2095 Node* flock = alock->as_Lock()->fastlock_node();
2096 if (flock->outcnt() == 1) {
2097 assert(flock->unique_out() == alock, "sanity");
2098 _igvn.replace_node(flock, top());
2099 }
2100 }
2101
2102 // Seach for MemBarReleaseLock node and delete it also.
2103 if (alock->is_Unlock() && ctrl->is_Proj() && ctrl->in(0)->is_MemBar()) {
2104 MemBarNode* membar = ctrl->in(0)->as_MemBar();
2105 assert(membar->Opcode() == Op_MemBarReleaseLock &&
2106 mem->is_Proj() && membar == mem->in(0), "");
2107 _igvn.replace_node(fallthroughproj, ctrl);
2108 _igvn.replace_node(memproj_fallthrough, mem);
2109 fallthroughproj = ctrl;
2110 memproj_fallthrough = mem;
2111 ctrl = membar->in(TypeFunc::Control);
2112 mem = membar->in(TypeFunc::Memory);
2113 }
2114
2115 _igvn.replace_node(fallthroughproj, ctrl);
2116 _igvn.replace_node(memproj_fallthrough, mem);
2117 return true;
2118}
2119
2120
2121//------------------------------expand_lock_node----------------------
2122void PhaseMacroExpand::expand_lock_node(LockNode *lock) {
2123
2124 Node* ctrl = lock->in(TypeFunc::Control);
2125 Node* mem = lock->in(TypeFunc::Memory);
2126 Node* obj = lock->obj_node();
2127 Node* box = lock->box_node();
2128 Node* flock = lock->fastlock_node();
2129
2130 assert(!box->as_BoxLock()->is_eliminated(), "sanity");
2131
2132 // Make the merge point
2133 Node *region;
2134 Node *mem_phi;
2135 Node *slow_path;
2136
2137 if (UseOptoBiasInlining) {
2138 /*
2139 * See the full description in MacroAssembler::biased_locking_enter().
2140 *
2141 * if( (mark_word & biased_lock_mask) == biased_lock_pattern ) {
2142 * // The object is biased.
2143 * proto_node = klass->prototype_header;
2144 * o_node = thread | proto_node;
2145 * x_node = o_node ^ mark_word;
2146 * if( (x_node & ~age_mask) == 0 ) { // Biased to the current thread ?
2147 * // Done.
2148 * } else {
2149 * if( (x_node & biased_lock_mask) != 0 ) {
2150 * // The klass's prototype header is no longer biased.
2151 * cas(&mark_word, mark_word, proto_node)
2152 * goto cas_lock;
2153 * } else {
2154 * // The klass's prototype header is still biased.
2155 * if( (x_node & epoch_mask) != 0 ) { // Expired epoch?
2156 * old = mark_word;
2157 * new = o_node;
2158 * } else {
2159 * // Different thread or anonymous biased.
2160 * old = mark_word & (epoch_mask | age_mask | biased_lock_mask);
2161 * new = thread | old;
2162 * }
2163 * // Try to rebias.
2164 * if( cas(&mark_word, old, new) == 0 ) {
2165 * // Done.
2166 * } else {
2167 * goto slow_path; // Failed.
2168 * }
2169 * }
2170 * }
2171 * } else {
2172 * // The object is not biased.
2173 * cas_lock:
2174 * if( FastLock(obj) == 0 ) {
2175 * // Done.
2176 * } else {
2177 * slow_path:
2178 * OptoRuntime::complete_monitor_locking_Java(obj);
2179 * }
2180 * }
2181 */
2182
2183 region = new RegionNode(5);
2184 // create a Phi for the memory state
2185 mem_phi = new PhiNode( region, Type::MEMORY, TypeRawPtr::BOTTOM);
2186
2187 Node* fast_lock_region = new RegionNode(3);
2188 Node* fast_lock_mem_phi = new PhiNode( fast_lock_region, Type::MEMORY, TypeRawPtr::BOTTOM);
2189
2190 // First, check mark word for the biased lock pattern.
2191 Node* mark_node = make_load(ctrl, mem, obj, oopDesc::mark_offset_in_bytes(), TypeX_X, TypeX_X->basic_type());
2192
2193 // Get fast path - mark word has the biased lock pattern.
2194 ctrl = opt_bits_test(ctrl, fast_lock_region, 1, mark_node,
2195 markOopDesc::biased_lock_mask_in_place,
2196 markOopDesc::biased_lock_pattern, true);
2197 // fast_lock_region->in(1) is set to slow path.
2198 fast_lock_mem_phi->init_req(1, mem);
2199
2200 // Now check that the lock is biased to the current thread and has
2201 // the same epoch and bias as Klass::_prototype_header.
2202
2203 // Special-case a fresh allocation to avoid building nodes:
2204 Node* klass_node = AllocateNode::Ideal_klass(obj, &_igvn);
2205 if (klass_node == NULL) {
2206 Node* k_adr = basic_plus_adr(obj, oopDesc::klass_offset_in_bytes());
2207 klass_node = transform_later(LoadKlassNode::make(_igvn, NULL, mem, k_adr, _igvn.type(k_adr)->is_ptr()));
2208#ifdef _LP64
2209 if (UseCompressedClassPointers && klass_node->is_DecodeNKlass()) {
2210 assert(klass_node->in(1)->Opcode() == Op_LoadNKlass, "sanity");
2211 klass_node->in(1)->init_req(0, ctrl);
2212 } else
2213#endif
2214 klass_node->init_req(0, ctrl);
2215 }
2216 Node *proto_node = make_load(ctrl, mem, klass_node, in_bytes(Klass::prototype_header_offset()), TypeX_X, TypeX_X->basic_type());
2217
2218 Node* thread = transform_later(new ThreadLocalNode());
2219 Node* cast_thread = transform_later(new CastP2XNode(ctrl, thread));
2220 Node* o_node = transform_later(new OrXNode(cast_thread, proto_node));
2221 Node* x_node = transform_later(new XorXNode(o_node, mark_node));
2222
2223 // Get slow path - mark word does NOT match the value.
2224 Node* not_biased_ctrl = opt_bits_test(ctrl, region, 3, x_node,
2225 (~markOopDesc::age_mask_in_place), 0);
2226 // region->in(3) is set to fast path - the object is biased to the current thread.
2227 mem_phi->init_req(3, mem);
2228
2229
2230 // Mark word does NOT match the value (thread | Klass::_prototype_header).
2231
2232
2233 // First, check biased pattern.
2234 // Get fast path - _prototype_header has the same biased lock pattern.
2235 ctrl = opt_bits_test(not_biased_ctrl, fast_lock_region, 2, x_node,
2236 markOopDesc::biased_lock_mask_in_place, 0, true);
2237
2238 not_biased_ctrl = fast_lock_region->in(2); // Slow path
2239 // fast_lock_region->in(2) - the prototype header is no longer biased
2240 // and we have to revoke the bias on this object.
2241 // We are going to try to reset the mark of this object to the prototype
2242 // value and fall through to the CAS-based locking scheme.
2243 Node* adr = basic_plus_adr(obj, oopDesc::mark_offset_in_bytes());
2244 Node* cas = new StoreXConditionalNode(not_biased_ctrl, mem, adr,
2245 proto_node, mark_node);
2246 transform_later(cas);
2247 Node* proj = transform_later(new SCMemProjNode(cas));
2248 fast_lock_mem_phi->init_req(2, proj);
2249
2250
2251 // Second, check epoch bits.
2252 Node* rebiased_region = new RegionNode(3);
2253 Node* old_phi = new PhiNode( rebiased_region, TypeX_X);
2254 Node* new_phi = new PhiNode( rebiased_region, TypeX_X);
2255
2256 // Get slow path - mark word does NOT match epoch bits.
2257 Node* epoch_ctrl = opt_bits_test(ctrl, rebiased_region, 1, x_node,
2258 markOopDesc::epoch_mask_in_place, 0);
2259 // The epoch of the current bias is not valid, attempt to rebias the object
2260 // toward the current thread.
2261 rebiased_region->init_req(2, epoch_ctrl);
2262 old_phi->init_req(2, mark_node);
2263 new_phi->init_req(2, o_node);
2264
2265 // rebiased_region->in(1) is set to fast path.
2266 // The epoch of the current bias is still valid but we know
2267 // nothing about the owner; it might be set or it might be clear.
2268 Node* cmask = MakeConX(markOopDesc::biased_lock_mask_in_place |
2269 markOopDesc::age_mask_in_place |
2270 markOopDesc::epoch_mask_in_place);
2271 Node* old = transform_later(new AndXNode(mark_node, cmask));
2272 cast_thread = transform_later(new CastP2XNode(ctrl, thread));
2273 Node* new_mark = transform_later(new OrXNode(cast_thread, old));
2274 old_phi->init_req(1, old);
2275 new_phi->init_req(1, new_mark);
2276
2277 transform_later(rebiased_region);
2278 transform_later(old_phi);
2279 transform_later(new_phi);
2280
2281 // Try to acquire the bias of the object using an atomic operation.
2282 // If this fails we will go in to the runtime to revoke the object's bias.
2283 cas = new StoreXConditionalNode(rebiased_region, mem, adr, new_phi, old_phi);
2284 transform_later(cas);
2285 proj = transform_later(new SCMemProjNode(cas));
2286
2287 // Get slow path - Failed to CAS.
2288 not_biased_ctrl = opt_bits_test(rebiased_region, region, 4, cas, 0, 0);
2289 mem_phi->init_req(4, proj);
2290 // region->in(4) is set to fast path - the object is rebiased to the current thread.
2291
2292 // Failed to CAS.
2293 slow_path = new RegionNode(3);
2294 Node *slow_mem = new PhiNode( slow_path, Type::MEMORY, TypeRawPtr::BOTTOM);
2295
2296 slow_path->init_req(1, not_biased_ctrl); // Capture slow-control
2297 slow_mem->init_req(1, proj);
2298
2299 // Call CAS-based locking scheme (FastLock node).
2300
2301 transform_later(fast_lock_region);
2302 transform_later(fast_lock_mem_phi);
2303
2304 // Get slow path - FastLock failed to lock the object.
2305 ctrl = opt_bits_test(fast_lock_region, region, 2, flock, 0, 0);
2306 mem_phi->init_req(2, fast_lock_mem_phi);
2307 // region->in(2) is set to fast path - the object is locked to the current thread.
2308
2309 slow_path->init_req(2, ctrl); // Capture slow-control
2310 slow_mem->init_req(2, fast_lock_mem_phi);
2311
2312 transform_later(slow_path);
2313 transform_later(slow_mem);
2314 // Reset lock's memory edge.
2315 lock->set_req(TypeFunc::Memory, slow_mem);
2316
2317 } else {
2318 region = new RegionNode(3);
2319 // create a Phi for the memory state
2320 mem_phi = new PhiNode( region, Type::MEMORY, TypeRawPtr::BOTTOM);
2321
2322 // Optimize test; set region slot 2
2323 slow_path = opt_bits_test(ctrl, region, 2, flock, 0, 0);
2324 mem_phi->init_req(2, mem);
2325 }
2326
2327 // Make slow path call
2328 CallNode *call = make_slow_call((CallNode *) lock, OptoRuntime::complete_monitor_enter_Type(),
2329 OptoRuntime::complete_monitor_locking_Java(), NULL, slow_path,
2330 obj, box, NULL);
2331
2332 extract_call_projections(call);
2333
2334 // Slow path can only throw asynchronous exceptions, which are always
2335 // de-opted. So the compiler thinks the slow-call can never throw an
2336 // exception. If it DOES throw an exception we would need the debug
2337 // info removed first (since if it throws there is no monitor).
2338 assert ( _ioproj_fallthrough == NULL && _ioproj_catchall == NULL &&
2339 _memproj_catchall == NULL && _catchallcatchproj == NULL, "Unexpected projection from Lock");
2340
2341 // Capture slow path
2342 // disconnect fall-through projection from call and create a new one
2343 // hook up users of fall-through projection to region
2344 Node *slow_ctrl = _fallthroughproj->clone();
2345 transform_later(slow_ctrl);
2346 _igvn.hash_delete(_fallthroughproj);
2347 _fallthroughproj->disconnect_inputs(NULL, C);
2348 region->init_req(1, slow_ctrl);
2349 // region inputs are now complete
2350 transform_later(region);
2351 _igvn.replace_node(_fallthroughproj, region);
2352
2353 Node *memproj = transform_later(new ProjNode(call, TypeFunc::Memory));
2354 mem_phi->init_req(1, memproj );
2355 transform_later(mem_phi);
2356 _igvn.replace_node(_memproj_fallthrough, mem_phi);
2357}
2358
2359//------------------------------expand_unlock_node----------------------
2360void PhaseMacroExpand::expand_unlock_node(UnlockNode *unlock) {
2361
2362 Node* ctrl = unlock->in(TypeFunc::Control);
2363 Node* mem = unlock->in(TypeFunc::Memory);
2364 Node* obj = unlock->obj_node();
2365 Node* box = unlock->box_node();
2366
2367 assert(!box->as_BoxLock()->is_eliminated(), "sanity");
2368
2369 // No need for a null check on unlock
2370
2371 // Make the merge point
2372 Node *region;
2373 Node *mem_phi;
2374
2375 if (UseOptoBiasInlining) {
2376 // Check for biased locking unlock case, which is a no-op.
2377 // See the full description in MacroAssembler::biased_locking_exit().
2378 region = new RegionNode(4);
2379 // create a Phi for the memory state
2380 mem_phi = new PhiNode( region, Type::MEMORY, TypeRawPtr::BOTTOM);
2381 mem_phi->init_req(3, mem);
2382
2383 Node* mark_node = make_load(ctrl, mem, obj, oopDesc::mark_offset_in_bytes(), TypeX_X, TypeX_X->basic_type());
2384 ctrl = opt_bits_test(ctrl, region, 3, mark_node,
2385 markOopDesc::biased_lock_mask_in_place,
2386 markOopDesc::biased_lock_pattern);
2387 } else {
2388 region = new RegionNode(3);
2389 // create a Phi for the memory state
2390 mem_phi = new PhiNode( region, Type::MEMORY, TypeRawPtr::BOTTOM);
2391 }
2392
2393 FastUnlockNode *funlock = new FastUnlockNode( ctrl, obj, box );
2394 funlock = transform_later( funlock )->as_FastUnlock();
2395 // Optimize test; set region slot 2
2396 Node *slow_path = opt_bits_test(ctrl, region, 2, funlock, 0, 0);
2397 Node *thread = transform_later(new ThreadLocalNode());
2398
2399 CallNode *call = make_slow_call((CallNode *) unlock, OptoRuntime::complete_monitor_exit_Type(),
2400 CAST_FROM_FN_PTR(address, SharedRuntime::complete_monitor_unlocking_C),
2401 "complete_monitor_unlocking_C", slow_path, obj, box, thread);
2402
2403 extract_call_projections(call);
2404
2405 assert ( _ioproj_fallthrough == NULL && _ioproj_catchall == NULL &&
2406 _memproj_catchall == NULL && _catchallcatchproj == NULL, "Unexpected projection from Lock");
2407
2408 // No exceptions for unlocking
2409 // Capture slow path
2410 // disconnect fall-through projection from call and create a new one
2411 // hook up users of fall-through projection to region
2412 Node *slow_ctrl = _fallthroughproj->clone();
2413 transform_later(slow_ctrl);
2414 _igvn.hash_delete(_fallthroughproj);
2415 _fallthroughproj->disconnect_inputs(NULL, C);
2416 region->init_req(1, slow_ctrl);
2417 // region inputs are now complete
2418 transform_later(region);
2419 _igvn.replace_node(_fallthroughproj, region);
2420
2421 Node *memproj = transform_later(new ProjNode(call, TypeFunc::Memory) );
2422 mem_phi->init_req(1, memproj );
2423 mem_phi->init_req(2, mem);
2424 transform_later(mem_phi);
2425 _igvn.replace_node(_memproj_fallthrough, mem_phi);
2426}
2427
2428//---------------------------eliminate_macro_nodes----------------------
2429// Eliminate scalar replaced allocations and associated locks.
2430void PhaseMacroExpand::eliminate_macro_nodes() {
2431 if (C->macro_count() == 0)
2432 return;
2433
2434 // First, attempt to eliminate locks
2435 int cnt = C->macro_count();
2436 for (int i=0; i < cnt; i++) {
2437 Node *n = C->macro_node(i);
2438 if (n->is_AbstractLock()) { // Lock and Unlock nodes
2439 // Before elimination mark all associated (same box and obj)
2440 // lock and unlock nodes.
2441 mark_eliminated_locking_nodes(n->as_AbstractLock());
2442 }
2443 }
2444 bool progress = true;
2445 while (progress) {
2446 progress = false;
2447 for (int i = C->macro_count(); i > 0; i--) {
2448 Node * n = C->macro_node(i-1);
2449 bool success = false;
2450 debug_only(int old_macro_count = C->macro_count(););
2451 if (n->is_AbstractLock()) {
2452 success = eliminate_locking_node(n->as_AbstractLock());
2453 }
2454 assert(success == (C->macro_count() < old_macro_count), "elimination reduces macro count");
2455 progress = progress || success;
2456 }
2457 }
2458 // Next, attempt to eliminate allocations
2459 _has_locks = false;
2460 progress = true;
2461 while (progress) {
2462 progress = false;
2463 for (int i = C->macro_count(); i > 0; i--) {
2464 Node * n = C->macro_node(i-1);
2465 bool success = false;
2466 debug_only(int old_macro_count = C->macro_count(););
2467 switch (n->class_id()) {
2468 case Node::Class_Allocate:
2469 case Node::Class_AllocateArray:
2470 success = eliminate_allocate_node(n->as_Allocate());
2471 break;
2472 case Node::Class_CallStaticJava:
2473 success = eliminate_boxing_node(n->as_CallStaticJava());
2474 break;
2475 case Node::Class_Lock:
2476 case Node::Class_Unlock:
2477 assert(!n->as_AbstractLock()->is_eliminated(), "sanity");
2478 _has_locks = true;
2479 break;
2480 case Node::Class_ArrayCopy:
2481 break;
2482 case Node::Class_OuterStripMinedLoop:
2483 break;
2484 default:
2485 assert(n->Opcode() == Op_LoopLimit ||
2486 n->Opcode() == Op_Opaque1 ||
2487 n->Opcode() == Op_Opaque2 ||
2488 n->Opcode() == Op_Opaque3 ||
2489 BarrierSet::barrier_set()->barrier_set_c2()->is_gc_barrier_node(n),
2490 "unknown node type in macro list");
2491 }
2492 assert(success == (C->macro_count() < old_macro_count), "elimination reduces macro count");
2493 progress = progress || success;
2494 }
2495 }
2496}
2497
2498//------------------------------expand_macro_nodes----------------------
2499// Returns true if a failure occurred.
2500bool PhaseMacroExpand::expand_macro_nodes() {
2501 // Last attempt to eliminate macro nodes.
2502 eliminate_macro_nodes();
2503
2504 // Make sure expansion will not cause node limit to be exceeded.
2505 // Worst case is a macro node gets expanded into about 200 nodes.
2506 // Allow 50% more for optimization.
2507 if (C->check_node_count(C->macro_count() * 300, "out of nodes before macro expansion" ) )
2508 return true;
2509
2510 // Eliminate Opaque and LoopLimit nodes. Do it after all loop optimizations.
2511 bool progress = true;
2512 while (progress) {
2513 progress = false;
2514 for (int i = C->macro_count(); i > 0; i--) {
2515 Node * n = C->macro_node(i-1);
2516 bool success = false;
2517 debug_only(int old_macro_count = C->macro_count(););
2518 if (n->Opcode() == Op_LoopLimit) {
2519 // Remove it from macro list and put on IGVN worklist to optimize.
2520 C->remove_macro_node(n);
2521 _igvn._worklist.push(n);
2522 success = true;
2523 } else if (n->Opcode() == Op_CallStaticJava) {
2524 // Remove it from macro list and put on IGVN worklist to optimize.
2525 C->remove_macro_node(n);
2526 _igvn._worklist.push(n);
2527 success = true;
2528 } else if (n->Opcode() == Op_Opaque1 || n->Opcode() == Op_Opaque2) {
2529 _igvn.replace_node(n, n->in(1));
2530 success = true;
2531#if INCLUDE_RTM_OPT
2532 } else if ((n->Opcode() == Op_Opaque3) && ((Opaque3Node*)n)->rtm_opt()) {
2533 assert(C->profile_rtm(), "should be used only in rtm deoptimization code");
2534 assert((n->outcnt() == 1) && n->unique_out()->is_Cmp(), "");
2535 Node* cmp = n->unique_out();
2536#ifdef ASSERT
2537 // Validate graph.
2538 assert((cmp->outcnt() == 1) && cmp->unique_out()->is_Bool(), "");
2539 BoolNode* bol = cmp->unique_out()->as_Bool();
2540 assert((bol->outcnt() == 1) && bol->unique_out()->is_If() &&
2541 (bol->_test._test == BoolTest::ne), "");
2542 IfNode* ifn = bol->unique_out()->as_If();
2543 assert((ifn->outcnt() == 2) &&
2544 ifn->proj_out(1)->is_uncommon_trap_proj(Deoptimization::Reason_rtm_state_change) != NULL, "");
2545#endif
2546 Node* repl = n->in(1);
2547 if (!_has_locks) {
2548 // Remove RTM state check if there are no locks in the code.
2549 // Replace input to compare the same value.
2550 repl = (cmp->in(1) == n) ? cmp->in(2) : cmp->in(1);
2551 }
2552 _igvn.replace_node(n, repl);
2553 success = true;
2554#endif
2555 } else if (n->Opcode() == Op_OuterStripMinedLoop) {
2556 n->as_OuterStripMinedLoop()->adjust_strip_mined_loop(&_igvn);
2557 C->remove_macro_node(n);
2558 success = true;
2559 }
2560 assert(success == (C->macro_count() < old_macro_count), "elimination reduces macro count");
2561 progress = progress || success;
2562 }
2563 }
2564
2565 // expand arraycopy "macro" nodes first
2566 // For ReduceBulkZeroing, we must first process all arraycopy nodes
2567 // before the allocate nodes are expanded.
2568 int macro_idx = C->macro_count() - 1;
2569 while (macro_idx >= 0) {
2570 Node * n = C->macro_node(macro_idx);
2571 assert(n->is_macro(), "only macro nodes expected here");
2572 if (_igvn.type(n) == Type::TOP || (n->in(0) != NULL && n->in(0)->is_top())) {
2573 // node is unreachable, so don't try to expand it
2574 C->remove_macro_node(n);
2575 } else if (n->is_ArrayCopy()){
2576 int macro_count = C->macro_count();
2577 expand_arraycopy_node(n->as_ArrayCopy());
2578 assert(C->macro_count() < macro_count, "must have deleted a node from macro list");
2579 }
2580 if (C->failing()) return true;
2581 macro_idx --;
2582 }
2583
2584 // expand "macro" nodes
2585 // nodes are removed from the macro list as they are processed
2586 while (C->macro_count() > 0) {
2587 int macro_count = C->macro_count();
2588 Node * n = C->macro_node(macro_count-1);
2589 assert(n->is_macro(), "only macro nodes expected here");
2590 if (_igvn.type(n) == Type::TOP || (n->in(0) != NULL && n->in(0)->is_top())) {
2591 // node is unreachable, so don't try to expand it
2592 C->remove_macro_node(n);
2593 continue;
2594 }
2595 switch (n->class_id()) {
2596 case Node::Class_Allocate:
2597 expand_allocate(n->as_Allocate());
2598 break;
2599 case Node::Class_AllocateArray:
2600 expand_allocate_array(n->as_AllocateArray());
2601 break;
2602 case Node::Class_Lock:
2603 expand_lock_node(n->as_Lock());
2604 break;
2605 case Node::Class_Unlock:
2606 expand_unlock_node(n->as_Unlock());
2607 break;
2608 default:
2609 assert(false, "unknown node type in macro list");
2610 }
2611 assert(C->macro_count() < macro_count, "must have deleted a node from macro list");
2612 if (C->failing()) return true;
2613 }
2614
2615 _igvn.set_delay_transform(false);
2616 _igvn.optimize();
2617 if (C->failing()) return true;
2618 return false;
2619}
2620