Fri, 16 Oct 2009 02:05:46 -0700
6888898: CMS: ReduceInitialCardMarks unsafe in the presence of cms precleaning
6889757: G1: enable card mark elision for initializing writes from compiled code (ReduceInitialCardMarks)
Summary: Defer the (compiler-elided) card-mark upon a slow-path allocation until after the store and before the next subsequent safepoint; G1 now answers yes to can_elide_tlab_write_barriers().
Reviewed-by: jcoomes, kvn, never
1 /*
2 * Copyright 1997-2009 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 *
23 */
25 // Portions of code courtesy of Clifford Click
27 // Optimization - Graph Style
29 #include "incls/_precompiled.incl"
30 #include "incls/_callnode.cpp.incl"
32 //=============================================================================
33 uint StartNode::size_of() const { return sizeof(*this); }
34 uint StartNode::cmp( const Node &n ) const
35 { return _domain == ((StartNode&)n)._domain; }
36 const Type *StartNode::bottom_type() const { return _domain; }
37 const Type *StartNode::Value(PhaseTransform *phase) const { return _domain; }
38 #ifndef PRODUCT
39 void StartNode::dump_spec(outputStream *st) const { st->print(" #"); _domain->dump_on(st);}
40 #endif
42 //------------------------------Ideal------------------------------------------
43 Node *StartNode::Ideal(PhaseGVN *phase, bool can_reshape){
44 return remove_dead_region(phase, can_reshape) ? this : NULL;
45 }
47 //------------------------------calling_convention-----------------------------
48 void StartNode::calling_convention( BasicType* sig_bt, VMRegPair *parm_regs, uint argcnt ) const {
49 Matcher::calling_convention( sig_bt, parm_regs, argcnt, false );
50 }
52 //------------------------------Registers--------------------------------------
53 const RegMask &StartNode::in_RegMask(uint) const {
54 return RegMask::Empty;
55 }
57 //------------------------------match------------------------------------------
58 // Construct projections for incoming parameters, and their RegMask info
59 Node *StartNode::match( const ProjNode *proj, const Matcher *match ) {
60 switch (proj->_con) {
61 case TypeFunc::Control:
62 case TypeFunc::I_O:
63 case TypeFunc::Memory:
64 return new (match->C, 1) MachProjNode(this,proj->_con,RegMask::Empty,MachProjNode::unmatched_proj);
65 case TypeFunc::FramePtr:
66 return new (match->C, 1) MachProjNode(this,proj->_con,Matcher::c_frame_ptr_mask, Op_RegP);
67 case TypeFunc::ReturnAdr:
68 return new (match->C, 1) MachProjNode(this,proj->_con,match->_return_addr_mask,Op_RegP);
69 case TypeFunc::Parms:
70 default: {
71 uint parm_num = proj->_con - TypeFunc::Parms;
72 const Type *t = _domain->field_at(proj->_con);
73 if (t->base() == Type::Half) // 2nd half of Longs and Doubles
74 return new (match->C, 1) ConNode(Type::TOP);
75 uint ideal_reg = Matcher::base2reg[t->base()];
76 RegMask &rm = match->_calling_convention_mask[parm_num];
77 return new (match->C, 1) MachProjNode(this,proj->_con,rm,ideal_reg);
78 }
79 }
80 return NULL;
81 }
83 //------------------------------StartOSRNode----------------------------------
84 // The method start node for an on stack replacement adapter
86 //------------------------------osr_domain-----------------------------
87 const TypeTuple *StartOSRNode::osr_domain() {
88 const Type **fields = TypeTuple::fields(2);
89 fields[TypeFunc::Parms+0] = TypeRawPtr::BOTTOM; // address of osr buffer
91 return TypeTuple::make(TypeFunc::Parms+1, fields);
92 }
94 //=============================================================================
95 const char * const ParmNode::names[TypeFunc::Parms+1] = {
96 "Control", "I_O", "Memory", "FramePtr", "ReturnAdr", "Parms"
97 };
99 #ifndef PRODUCT
100 void ParmNode::dump_spec(outputStream *st) const {
101 if( _con < TypeFunc::Parms ) {
102 st->print(names[_con]);
103 } else {
104 st->print("Parm%d: ",_con-TypeFunc::Parms);
105 // Verbose and WizardMode dump bottom_type for all nodes
106 if( !Verbose && !WizardMode ) bottom_type()->dump_on(st);
107 }
108 }
109 #endif
111 uint ParmNode::ideal_reg() const {
112 switch( _con ) {
113 case TypeFunc::Control : // fall through
114 case TypeFunc::I_O : // fall through
115 case TypeFunc::Memory : return 0;
116 case TypeFunc::FramePtr : // fall through
117 case TypeFunc::ReturnAdr: return Op_RegP;
118 default : assert( _con > TypeFunc::Parms, "" );
119 // fall through
120 case TypeFunc::Parms : {
121 // Type of argument being passed
122 const Type *t = in(0)->as_Start()->_domain->field_at(_con);
123 return Matcher::base2reg[t->base()];
124 }
125 }
126 ShouldNotReachHere();
127 return 0;
128 }
130 //=============================================================================
131 ReturnNode::ReturnNode(uint edges, Node *cntrl, Node *i_o, Node *memory, Node *frameptr, Node *retadr ) : Node(edges) {
132 init_req(TypeFunc::Control,cntrl);
133 init_req(TypeFunc::I_O,i_o);
134 init_req(TypeFunc::Memory,memory);
135 init_req(TypeFunc::FramePtr,frameptr);
136 init_req(TypeFunc::ReturnAdr,retadr);
137 }
139 Node *ReturnNode::Ideal(PhaseGVN *phase, bool can_reshape){
140 return remove_dead_region(phase, can_reshape) ? this : NULL;
141 }
143 const Type *ReturnNode::Value( PhaseTransform *phase ) const {
144 return ( phase->type(in(TypeFunc::Control)) == Type::TOP)
145 ? Type::TOP
146 : Type::BOTTOM;
147 }
149 // Do we Match on this edge index or not? No edges on return nodes
150 uint ReturnNode::match_edge(uint idx) const {
151 return 0;
152 }
155 #ifndef PRODUCT
156 void ReturnNode::dump_req() const {
157 // Dump the required inputs, enclosed in '(' and ')'
158 uint i; // Exit value of loop
159 for( i=0; i<req(); i++ ) { // For all required inputs
160 if( i == TypeFunc::Parms ) tty->print("returns");
161 if( in(i) ) tty->print("%c%d ", Compile::current()->node_arena()->contains(in(i)) ? ' ' : 'o', in(i)->_idx);
162 else tty->print("_ ");
163 }
164 }
165 #endif
167 //=============================================================================
168 RethrowNode::RethrowNode(
169 Node* cntrl,
170 Node* i_o,
171 Node* memory,
172 Node* frameptr,
173 Node* ret_adr,
174 Node* exception
175 ) : Node(TypeFunc::Parms + 1) {
176 init_req(TypeFunc::Control , cntrl );
177 init_req(TypeFunc::I_O , i_o );
178 init_req(TypeFunc::Memory , memory );
179 init_req(TypeFunc::FramePtr , frameptr );
180 init_req(TypeFunc::ReturnAdr, ret_adr);
181 init_req(TypeFunc::Parms , exception);
182 }
184 Node *RethrowNode::Ideal(PhaseGVN *phase, bool can_reshape){
185 return remove_dead_region(phase, can_reshape) ? this : NULL;
186 }
188 const Type *RethrowNode::Value( PhaseTransform *phase ) const {
189 return (phase->type(in(TypeFunc::Control)) == Type::TOP)
190 ? Type::TOP
191 : Type::BOTTOM;
192 }
194 uint RethrowNode::match_edge(uint idx) const {
195 return 0;
196 }
198 #ifndef PRODUCT
199 void RethrowNode::dump_req() const {
200 // Dump the required inputs, enclosed in '(' and ')'
201 uint i; // Exit value of loop
202 for( i=0; i<req(); i++ ) { // For all required inputs
203 if( i == TypeFunc::Parms ) tty->print("exception");
204 if( in(i) ) tty->print("%c%d ", Compile::current()->node_arena()->contains(in(i)) ? ' ' : 'o', in(i)->_idx);
205 else tty->print("_ ");
206 }
207 }
208 #endif
210 //=============================================================================
211 // Do we Match on this edge index or not? Match only target address & method
212 uint TailCallNode::match_edge(uint idx) const {
213 return TypeFunc::Parms <= idx && idx <= TypeFunc::Parms+1;
214 }
216 //=============================================================================
217 // Do we Match on this edge index or not? Match only target address & oop
218 uint TailJumpNode::match_edge(uint idx) const {
219 return TypeFunc::Parms <= idx && idx <= TypeFunc::Parms+1;
220 }
222 //=============================================================================
223 JVMState::JVMState(ciMethod* method, JVMState* caller) {
224 assert(method != NULL, "must be valid call site");
225 _method = method;
226 _reexecute = Reexecute_Undefined;
227 debug_only(_bci = -99); // random garbage value
228 debug_only(_map = (SafePointNode*)-1);
229 _caller = caller;
230 _depth = 1 + (caller == NULL ? 0 : caller->depth());
231 _locoff = TypeFunc::Parms;
232 _stkoff = _locoff + _method->max_locals();
233 _monoff = _stkoff + _method->max_stack();
234 _scloff = _monoff;
235 _endoff = _monoff;
236 _sp = 0;
237 }
238 JVMState::JVMState(int stack_size) {
239 _method = NULL;
240 _bci = InvocationEntryBci;
241 _reexecute = Reexecute_Undefined;
242 debug_only(_map = (SafePointNode*)-1);
243 _caller = NULL;
244 _depth = 1;
245 _locoff = TypeFunc::Parms;
246 _stkoff = _locoff;
247 _monoff = _stkoff + stack_size;
248 _scloff = _monoff;
249 _endoff = _monoff;
250 _sp = 0;
251 }
253 //--------------------------------of_depth-------------------------------------
254 JVMState* JVMState::of_depth(int d) const {
255 const JVMState* jvmp = this;
256 assert(0 < d && (uint)d <= depth(), "oob");
257 for (int skip = depth() - d; skip > 0; skip--) {
258 jvmp = jvmp->caller();
259 }
260 assert(jvmp->depth() == (uint)d, "found the right one");
261 return (JVMState*)jvmp;
262 }
264 //-----------------------------same_calls_as-----------------------------------
265 bool JVMState::same_calls_as(const JVMState* that) const {
266 if (this == that) return true;
267 if (this->depth() != that->depth()) return false;
268 const JVMState* p = this;
269 const JVMState* q = that;
270 for (;;) {
271 if (p->_method != q->_method) return false;
272 if (p->_method == NULL) return true; // bci is irrelevant
273 if (p->_bci != q->_bci) return false;
274 if (p->_reexecute != q->_reexecute) return false;
275 p = p->caller();
276 q = q->caller();
277 if (p == q) return true;
278 assert(p != NULL && q != NULL, "depth check ensures we don't run off end");
279 }
280 }
282 //------------------------------debug_start------------------------------------
283 uint JVMState::debug_start() const {
284 debug_only(JVMState* jvmroot = of_depth(1));
285 assert(jvmroot->locoff() <= this->locoff(), "youngest JVMState must be last");
286 return of_depth(1)->locoff();
287 }
289 //-------------------------------debug_end-------------------------------------
290 uint JVMState::debug_end() const {
291 debug_only(JVMState* jvmroot = of_depth(1));
292 assert(jvmroot->endoff() <= this->endoff(), "youngest JVMState must be last");
293 return endoff();
294 }
296 //------------------------------debug_depth------------------------------------
297 uint JVMState::debug_depth() const {
298 uint total = 0;
299 for (const JVMState* jvmp = this; jvmp != NULL; jvmp = jvmp->caller()) {
300 total += jvmp->debug_size();
301 }
302 return total;
303 }
305 #ifndef PRODUCT
307 //------------------------------format_helper----------------------------------
308 // Given an allocation (a Chaitin object) and a Node decide if the Node carries
309 // any defined value or not. If it does, print out the register or constant.
310 static void format_helper( PhaseRegAlloc *regalloc, outputStream* st, Node *n, const char *msg, uint i, GrowableArray<SafePointScalarObjectNode*> *scobjs ) {
311 if (n == NULL) { st->print(" NULL"); return; }
312 if (n->is_SafePointScalarObject()) {
313 // Scalar replacement.
314 SafePointScalarObjectNode* spobj = n->as_SafePointScalarObject();
315 scobjs->append_if_missing(spobj);
316 int sco_n = scobjs->find(spobj);
317 assert(sco_n >= 0, "");
318 st->print(" %s%d]=#ScObj" INT32_FORMAT, msg, i, sco_n);
319 return;
320 }
321 if( OptoReg::is_valid(regalloc->get_reg_first(n))) { // Check for undefined
322 char buf[50];
323 regalloc->dump_register(n,buf);
324 st->print(" %s%d]=%s",msg,i,buf);
325 } else { // No register, but might be constant
326 const Type *t = n->bottom_type();
327 switch (t->base()) {
328 case Type::Int:
329 st->print(" %s%d]=#"INT32_FORMAT,msg,i,t->is_int()->get_con());
330 break;
331 case Type::AnyPtr:
332 assert( t == TypePtr::NULL_PTR, "" );
333 st->print(" %s%d]=#NULL",msg,i);
334 break;
335 case Type::AryPtr:
336 case Type::KlassPtr:
337 case Type::InstPtr:
338 st->print(" %s%d]=#Ptr" INTPTR_FORMAT,msg,i,t->isa_oopptr()->const_oop());
339 break;
340 case Type::NarrowOop:
341 st->print(" %s%d]=#Ptr" INTPTR_FORMAT,msg,i,t->make_ptr()->isa_oopptr()->const_oop());
342 break;
343 case Type::RawPtr:
344 st->print(" %s%d]=#Raw" INTPTR_FORMAT,msg,i,t->is_rawptr());
345 break;
346 case Type::DoubleCon:
347 st->print(" %s%d]=#%fD",msg,i,t->is_double_constant()->_d);
348 break;
349 case Type::FloatCon:
350 st->print(" %s%d]=#%fF",msg,i,t->is_float_constant()->_f);
351 break;
352 case Type::Long:
353 st->print(" %s%d]=#"INT64_FORMAT,msg,i,t->is_long()->get_con());
354 break;
355 case Type::Half:
356 case Type::Top:
357 st->print(" %s%d]=_",msg,i);
358 break;
359 default: ShouldNotReachHere();
360 }
361 }
362 }
364 //------------------------------format-----------------------------------------
365 void JVMState::format(PhaseRegAlloc *regalloc, const Node *n, outputStream* st) const {
366 st->print(" #");
367 if( _method ) {
368 _method->print_short_name(st);
369 st->print(" @ bci:%d ",_bci);
370 } else {
371 st->print_cr(" runtime stub ");
372 return;
373 }
374 if (n->is_MachSafePoint()) {
375 GrowableArray<SafePointScalarObjectNode*> scobjs;
376 MachSafePointNode *mcall = n->as_MachSafePoint();
377 uint i;
378 // Print locals
379 for( i = 0; i < (uint)loc_size(); i++ )
380 format_helper( regalloc, st, mcall->local(this, i), "L[", i, &scobjs );
381 // Print stack
382 for (i = 0; i < (uint)stk_size(); i++) {
383 if ((uint)(_stkoff + i) >= mcall->len())
384 st->print(" oob ");
385 else
386 format_helper( regalloc, st, mcall->stack(this, i), "STK[", i, &scobjs );
387 }
388 for (i = 0; (int)i < nof_monitors(); i++) {
389 Node *box = mcall->monitor_box(this, i);
390 Node *obj = mcall->monitor_obj(this, i);
391 if ( OptoReg::is_valid(regalloc->get_reg_first(box)) ) {
392 while( !box->is_BoxLock() ) box = box->in(1);
393 format_helper( regalloc, st, box, "MON-BOX[", i, &scobjs );
394 } else {
395 OptoReg::Name box_reg = BoxLockNode::stack_slot(box);
396 st->print(" MON-BOX%d=%s+%d",
397 i,
398 OptoReg::regname(OptoReg::c_frame_pointer),
399 regalloc->reg2offset(box_reg));
400 }
401 const char* obj_msg = "MON-OBJ[";
402 if (EliminateLocks) {
403 while( !box->is_BoxLock() ) box = box->in(1);
404 if (box->as_BoxLock()->is_eliminated())
405 obj_msg = "MON-OBJ(LOCK ELIMINATED)[";
406 }
407 format_helper( regalloc, st, obj, obj_msg, i, &scobjs );
408 }
410 for (i = 0; i < (uint)scobjs.length(); i++) {
411 // Scalar replaced objects.
412 st->print_cr("");
413 st->print(" # ScObj" INT32_FORMAT " ", i);
414 SafePointScalarObjectNode* spobj = scobjs.at(i);
415 ciKlass* cik = spobj->bottom_type()->is_oopptr()->klass();
416 assert(cik->is_instance_klass() ||
417 cik->is_array_klass(), "Not supported allocation.");
418 ciInstanceKlass *iklass = NULL;
419 if (cik->is_instance_klass()) {
420 cik->print_name_on(st);
421 iklass = cik->as_instance_klass();
422 } else if (cik->is_type_array_klass()) {
423 cik->as_array_klass()->base_element_type()->print_name_on(st);
424 st->print("[%d]=", spobj->n_fields());
425 } else if (cik->is_obj_array_klass()) {
426 ciType* cie = cik->as_array_klass()->base_element_type();
427 int ndim = 1;
428 while (cie->is_obj_array_klass()) {
429 ndim += 1;
430 cie = cie->as_array_klass()->base_element_type();
431 }
432 cie->print_name_on(st);
433 while (ndim-- > 0) {
434 st->print("[]");
435 }
436 st->print("[%d]=", spobj->n_fields());
437 }
438 st->print("{");
439 uint nf = spobj->n_fields();
440 if (nf > 0) {
441 uint first_ind = spobj->first_index();
442 Node* fld_node = mcall->in(first_ind);
443 ciField* cifield;
444 if (iklass != NULL) {
445 st->print(" [");
446 cifield = iklass->nonstatic_field_at(0);
447 cifield->print_name_on(st);
448 format_helper( regalloc, st, fld_node, ":", 0, &scobjs );
449 } else {
450 format_helper( regalloc, st, fld_node, "[", 0, &scobjs );
451 }
452 for (uint j = 1; j < nf; j++) {
453 fld_node = mcall->in(first_ind+j);
454 if (iklass != NULL) {
455 st->print(", [");
456 cifield = iklass->nonstatic_field_at(j);
457 cifield->print_name_on(st);
458 format_helper( regalloc, st, fld_node, ":", j, &scobjs );
459 } else {
460 format_helper( regalloc, st, fld_node, ", [", j, &scobjs );
461 }
462 }
463 }
464 st->print(" }");
465 }
466 }
467 st->print_cr("");
468 if (caller() != NULL) caller()->format(regalloc, n, st);
469 }
472 void JVMState::dump_spec(outputStream *st) const {
473 if (_method != NULL) {
474 bool printed = false;
475 if (!Verbose) {
476 // The JVMS dumps make really, really long lines.
477 // Take out the most boring parts, which are the package prefixes.
478 char buf[500];
479 stringStream namest(buf, sizeof(buf));
480 _method->print_short_name(&namest);
481 if (namest.count() < sizeof(buf)) {
482 const char* name = namest.base();
483 if (name[0] == ' ') ++name;
484 const char* endcn = strchr(name, ':'); // end of class name
485 if (endcn == NULL) endcn = strchr(name, '(');
486 if (endcn == NULL) endcn = name + strlen(name);
487 while (endcn > name && endcn[-1] != '.' && endcn[-1] != '/')
488 --endcn;
489 st->print(" %s", endcn);
490 printed = true;
491 }
492 }
493 if (!printed)
494 _method->print_short_name(st);
495 st->print(" @ bci:%d",_bci);
496 if(_reexecute == Reexecute_True)
497 st->print(" reexecute");
498 } else {
499 st->print(" runtime stub");
500 }
501 if (caller() != NULL) caller()->dump_spec(st);
502 }
505 void JVMState::dump_on(outputStream* st) const {
506 if (_map && !((uintptr_t)_map & 1)) {
507 if (_map->len() > _map->req()) { // _map->has_exceptions()
508 Node* ex = _map->in(_map->req()); // _map->next_exception()
509 // skip the first one; it's already being printed
510 while (ex != NULL && ex->len() > ex->req()) {
511 ex = ex->in(ex->req()); // ex->next_exception()
512 ex->dump(1);
513 }
514 }
515 _map->dump(2);
516 }
517 st->print("JVMS depth=%d loc=%d stk=%d mon=%d scalar=%d end=%d mondepth=%d sp=%d bci=%d reexecute=%s method=",
518 depth(), locoff(), stkoff(), monoff(), scloff(), endoff(), monitor_depth(), sp(), bci(), should_reexecute()?"true":"false");
519 if (_method == NULL) {
520 st->print_cr("(none)");
521 } else {
522 _method->print_name(st);
523 st->cr();
524 if (bci() >= 0 && bci() < _method->code_size()) {
525 st->print(" bc: ");
526 _method->print_codes_on(bci(), bci()+1, st);
527 }
528 }
529 if (caller() != NULL) {
530 caller()->dump_on(st);
531 }
532 }
534 // Extra way to dump a jvms from the debugger,
535 // to avoid a bug with C++ member function calls.
536 void dump_jvms(JVMState* jvms) {
537 jvms->dump();
538 }
539 #endif
541 //--------------------------clone_shallow--------------------------------------
542 JVMState* JVMState::clone_shallow(Compile* C) const {
543 JVMState* n = has_method() ? new (C) JVMState(_method, _caller) : new (C) JVMState(0);
544 n->set_bci(_bci);
545 n->_reexecute = _reexecute;
546 n->set_locoff(_locoff);
547 n->set_stkoff(_stkoff);
548 n->set_monoff(_monoff);
549 n->set_scloff(_scloff);
550 n->set_endoff(_endoff);
551 n->set_sp(_sp);
552 n->set_map(_map);
553 return n;
554 }
556 //---------------------------clone_deep----------------------------------------
557 JVMState* JVMState::clone_deep(Compile* C) const {
558 JVMState* n = clone_shallow(C);
559 for (JVMState* p = n; p->_caller != NULL; p = p->_caller) {
560 p->_caller = p->_caller->clone_shallow(C);
561 }
562 assert(n->depth() == depth(), "sanity");
563 assert(n->debug_depth() == debug_depth(), "sanity");
564 return n;
565 }
567 //=============================================================================
568 uint CallNode::cmp( const Node &n ) const
569 { return _tf == ((CallNode&)n)._tf && _jvms == ((CallNode&)n)._jvms; }
570 #ifndef PRODUCT
571 void CallNode::dump_req() const {
572 // Dump the required inputs, enclosed in '(' and ')'
573 uint i; // Exit value of loop
574 for( i=0; i<req(); i++ ) { // For all required inputs
575 if( i == TypeFunc::Parms ) tty->print("(");
576 if( in(i) ) tty->print("%c%d ", Compile::current()->node_arena()->contains(in(i)) ? ' ' : 'o', in(i)->_idx);
577 else tty->print("_ ");
578 }
579 tty->print(")");
580 }
582 void CallNode::dump_spec(outputStream *st) const {
583 st->print(" ");
584 tf()->dump_on(st);
585 if (_cnt != COUNT_UNKNOWN) st->print(" C=%f",_cnt);
586 if (jvms() != NULL) jvms()->dump_spec(st);
587 }
588 #endif
590 const Type *CallNode::bottom_type() const { return tf()->range(); }
591 const Type *CallNode::Value(PhaseTransform *phase) const {
592 if (phase->type(in(0)) == Type::TOP) return Type::TOP;
593 return tf()->range();
594 }
596 //------------------------------calling_convention-----------------------------
597 void CallNode::calling_convention( BasicType* sig_bt, VMRegPair *parm_regs, uint argcnt ) const {
598 // Use the standard compiler calling convention
599 Matcher::calling_convention( sig_bt, parm_regs, argcnt, true );
600 }
603 //------------------------------match------------------------------------------
604 // Construct projections for control, I/O, memory-fields, ..., and
605 // return result(s) along with their RegMask info
606 Node *CallNode::match( const ProjNode *proj, const Matcher *match ) {
607 switch (proj->_con) {
608 case TypeFunc::Control:
609 case TypeFunc::I_O:
610 case TypeFunc::Memory:
611 return new (match->C, 1) MachProjNode(this,proj->_con,RegMask::Empty,MachProjNode::unmatched_proj);
613 case TypeFunc::Parms+1: // For LONG & DOUBLE returns
614 assert(tf()->_range->field_at(TypeFunc::Parms+1) == Type::HALF, "");
615 // 2nd half of doubles and longs
616 return new (match->C, 1) MachProjNode(this,proj->_con, RegMask::Empty, (uint)OptoReg::Bad);
618 case TypeFunc::Parms: { // Normal returns
619 uint ideal_reg = Matcher::base2reg[tf()->range()->field_at(TypeFunc::Parms)->base()];
620 OptoRegPair regs = is_CallRuntime()
621 ? match->c_return_value(ideal_reg,true) // Calls into C runtime
622 : match-> return_value(ideal_reg,true); // Calls into compiled Java code
623 RegMask rm = RegMask(regs.first());
624 if( OptoReg::is_valid(regs.second()) )
625 rm.Insert( regs.second() );
626 return new (match->C, 1) MachProjNode(this,proj->_con,rm,ideal_reg);
627 }
629 case TypeFunc::ReturnAdr:
630 case TypeFunc::FramePtr:
631 default:
632 ShouldNotReachHere();
633 }
634 return NULL;
635 }
637 // Do we Match on this edge index or not? Match no edges
638 uint CallNode::match_edge(uint idx) const {
639 return 0;
640 }
642 //
643 // Determine whether the call could modify the field of the specified
644 // instance at the specified offset.
645 //
646 bool CallNode::may_modify(const TypePtr *addr_t, PhaseTransform *phase) {
647 const TypeOopPtr *adrInst_t = addr_t->isa_oopptr();
649 // If not an OopPtr or not an instance type, assume the worst.
650 // Note: currently this method is called only for instance types.
651 if (adrInst_t == NULL || !adrInst_t->is_known_instance()) {
652 return true;
653 }
654 // The instance_id is set only for scalar-replaceable allocations which
655 // are not passed as arguments according to Escape Analysis.
656 return false;
657 }
659 // Does this call have a direct reference to n other than debug information?
660 bool CallNode::has_non_debug_use(Node *n) {
661 const TypeTuple * d = tf()->domain();
662 for (uint i = TypeFunc::Parms; i < d->cnt(); i++) {
663 Node *arg = in(i);
664 if (arg == n) {
665 return true;
666 }
667 }
668 return false;
669 }
671 // Returns the unique CheckCastPP of a call
672 // or 'this' if there are several CheckCastPP
673 // or returns NULL if there is no one.
674 Node *CallNode::result_cast() {
675 Node *cast = NULL;
677 Node *p = proj_out(TypeFunc::Parms);
678 if (p == NULL)
679 return NULL;
681 for (DUIterator_Fast imax, i = p->fast_outs(imax); i < imax; i++) {
682 Node *use = p->fast_out(i);
683 if (use->is_CheckCastPP()) {
684 if (cast != NULL) {
685 return this; // more than 1 CheckCastPP
686 }
687 cast = use;
688 }
689 }
690 return cast;
691 }
694 //=============================================================================
695 uint CallJavaNode::size_of() const { return sizeof(*this); }
696 uint CallJavaNode::cmp( const Node &n ) const {
697 CallJavaNode &call = (CallJavaNode&)n;
698 return CallNode::cmp(call) && _method == call._method;
699 }
700 #ifndef PRODUCT
701 void CallJavaNode::dump_spec(outputStream *st) const {
702 if( _method ) _method->print_short_name(st);
703 CallNode::dump_spec(st);
704 }
705 #endif
707 //=============================================================================
708 uint CallStaticJavaNode::size_of() const { return sizeof(*this); }
709 uint CallStaticJavaNode::cmp( const Node &n ) const {
710 CallStaticJavaNode &call = (CallStaticJavaNode&)n;
711 return CallJavaNode::cmp(call);
712 }
714 //----------------------------uncommon_trap_request----------------------------
715 // If this is an uncommon trap, return the request code, else zero.
716 int CallStaticJavaNode::uncommon_trap_request() const {
717 if (_name != NULL && !strcmp(_name, "uncommon_trap")) {
718 return extract_uncommon_trap_request(this);
719 }
720 return 0;
721 }
722 int CallStaticJavaNode::extract_uncommon_trap_request(const Node* call) {
723 #ifndef PRODUCT
724 if (!(call->req() > TypeFunc::Parms &&
725 call->in(TypeFunc::Parms) != NULL &&
726 call->in(TypeFunc::Parms)->is_Con())) {
727 assert(_in_dump_cnt != 0, "OK if dumping");
728 tty->print("[bad uncommon trap]");
729 return 0;
730 }
731 #endif
732 return call->in(TypeFunc::Parms)->bottom_type()->is_int()->get_con();
733 }
735 #ifndef PRODUCT
736 void CallStaticJavaNode::dump_spec(outputStream *st) const {
737 st->print("# Static ");
738 if (_name != NULL) {
739 st->print("%s", _name);
740 int trap_req = uncommon_trap_request();
741 if (trap_req != 0) {
742 char buf[100];
743 st->print("(%s)",
744 Deoptimization::format_trap_request(buf, sizeof(buf),
745 trap_req));
746 }
747 st->print(" ");
748 }
749 CallJavaNode::dump_spec(st);
750 }
751 #endif
753 //=============================================================================
754 uint CallDynamicJavaNode::size_of() const { return sizeof(*this); }
755 uint CallDynamicJavaNode::cmp( const Node &n ) const {
756 CallDynamicJavaNode &call = (CallDynamicJavaNode&)n;
757 return CallJavaNode::cmp(call);
758 }
759 #ifndef PRODUCT
760 void CallDynamicJavaNode::dump_spec(outputStream *st) const {
761 st->print("# Dynamic ");
762 CallJavaNode::dump_spec(st);
763 }
764 #endif
766 //=============================================================================
767 uint CallRuntimeNode::size_of() const { return sizeof(*this); }
768 uint CallRuntimeNode::cmp( const Node &n ) const {
769 CallRuntimeNode &call = (CallRuntimeNode&)n;
770 return CallNode::cmp(call) && !strcmp(_name,call._name);
771 }
772 #ifndef PRODUCT
773 void CallRuntimeNode::dump_spec(outputStream *st) const {
774 st->print("# ");
775 st->print(_name);
776 CallNode::dump_spec(st);
777 }
778 #endif
780 //------------------------------calling_convention-----------------------------
781 void CallRuntimeNode::calling_convention( BasicType* sig_bt, VMRegPair *parm_regs, uint argcnt ) const {
782 Matcher::c_calling_convention( sig_bt, parm_regs, argcnt );
783 }
785 //=============================================================================
786 //------------------------------calling_convention-----------------------------
789 //=============================================================================
790 #ifndef PRODUCT
791 void CallLeafNode::dump_spec(outputStream *st) const {
792 st->print("# ");
793 st->print(_name);
794 CallNode::dump_spec(st);
795 }
796 #endif
798 //=============================================================================
800 void SafePointNode::set_local(JVMState* jvms, uint idx, Node *c) {
801 assert(verify_jvms(jvms), "jvms must match");
802 int loc = jvms->locoff() + idx;
803 if (in(loc)->is_top() && idx > 0 && !c->is_top() ) {
804 // If current local idx is top then local idx - 1 could
805 // be a long/double that needs to be killed since top could
806 // represent the 2nd half ofthe long/double.
807 uint ideal = in(loc -1)->ideal_reg();
808 if (ideal == Op_RegD || ideal == Op_RegL) {
809 // set other (low index) half to top
810 set_req(loc - 1, in(loc));
811 }
812 }
813 set_req(loc, c);
814 }
816 uint SafePointNode::size_of() const { return sizeof(*this); }
817 uint SafePointNode::cmp( const Node &n ) const {
818 return (&n == this); // Always fail except on self
819 }
821 //-------------------------set_next_exception----------------------------------
822 void SafePointNode::set_next_exception(SafePointNode* n) {
823 assert(n == NULL || n->Opcode() == Op_SafePoint, "correct value for next_exception");
824 if (len() == req()) {
825 if (n != NULL) add_prec(n);
826 } else {
827 set_prec(req(), n);
828 }
829 }
832 //----------------------------next_exception-----------------------------------
833 SafePointNode* SafePointNode::next_exception() const {
834 if (len() == req()) {
835 return NULL;
836 } else {
837 Node* n = in(req());
838 assert(n == NULL || n->Opcode() == Op_SafePoint, "no other uses of prec edges");
839 return (SafePointNode*) n;
840 }
841 }
844 //------------------------------Ideal------------------------------------------
845 // Skip over any collapsed Regions
846 Node *SafePointNode::Ideal(PhaseGVN *phase, bool can_reshape) {
847 return remove_dead_region(phase, can_reshape) ? this : NULL;
848 }
850 //------------------------------Identity---------------------------------------
851 // Remove obviously duplicate safepoints
852 Node *SafePointNode::Identity( PhaseTransform *phase ) {
854 // If you have back to back safepoints, remove one
855 if( in(TypeFunc::Control)->is_SafePoint() )
856 return in(TypeFunc::Control);
858 if( in(0)->is_Proj() ) {
859 Node *n0 = in(0)->in(0);
860 // Check if he is a call projection (except Leaf Call)
861 if( n0->is_Catch() ) {
862 n0 = n0->in(0)->in(0);
863 assert( n0->is_Call(), "expect a call here" );
864 }
865 if( n0->is_Call() && n0->as_Call()->guaranteed_safepoint() ) {
866 // Useless Safepoint, so remove it
867 return in(TypeFunc::Control);
868 }
869 }
871 return this;
872 }
874 //------------------------------Value------------------------------------------
875 const Type *SafePointNode::Value( PhaseTransform *phase ) const {
876 if( phase->type(in(0)) == Type::TOP ) return Type::TOP;
877 if( phase->eqv( in(0), this ) ) return Type::TOP; // Dead infinite loop
878 return Type::CONTROL;
879 }
881 #ifndef PRODUCT
882 void SafePointNode::dump_spec(outputStream *st) const {
883 st->print(" SafePoint ");
884 }
885 #endif
887 const RegMask &SafePointNode::in_RegMask(uint idx) const {
888 if( idx < TypeFunc::Parms ) return RegMask::Empty;
889 // Values outside the domain represent debug info
890 return *(Compile::current()->matcher()->idealreg2debugmask[in(idx)->ideal_reg()]);
891 }
892 const RegMask &SafePointNode::out_RegMask() const {
893 return RegMask::Empty;
894 }
897 void SafePointNode::grow_stack(JVMState* jvms, uint grow_by) {
898 assert((int)grow_by > 0, "sanity");
899 int monoff = jvms->monoff();
900 int scloff = jvms->scloff();
901 int endoff = jvms->endoff();
902 assert(endoff == (int)req(), "no other states or debug info after me");
903 Node* top = Compile::current()->top();
904 for (uint i = 0; i < grow_by; i++) {
905 ins_req(monoff, top);
906 }
907 jvms->set_monoff(monoff + grow_by);
908 jvms->set_scloff(scloff + grow_by);
909 jvms->set_endoff(endoff + grow_by);
910 }
912 void SafePointNode::push_monitor(const FastLockNode *lock) {
913 // Add a LockNode, which points to both the original BoxLockNode (the
914 // stack space for the monitor) and the Object being locked.
915 const int MonitorEdges = 2;
916 assert(JVMState::logMonitorEdges == exact_log2(MonitorEdges), "correct MonitorEdges");
917 assert(req() == jvms()->endoff(), "correct sizing");
918 int nextmon = jvms()->scloff();
919 if (GenerateSynchronizationCode) {
920 add_req(lock->box_node());
921 add_req(lock->obj_node());
922 } else {
923 Node* top = Compile::current()->top();
924 add_req(top);
925 add_req(top);
926 }
927 jvms()->set_scloff(nextmon+MonitorEdges);
928 jvms()->set_endoff(req());
929 }
931 void SafePointNode::pop_monitor() {
932 // Delete last monitor from debug info
933 debug_only(int num_before_pop = jvms()->nof_monitors());
934 const int MonitorEdges = (1<<JVMState::logMonitorEdges);
935 int scloff = jvms()->scloff();
936 int endoff = jvms()->endoff();
937 int new_scloff = scloff - MonitorEdges;
938 int new_endoff = endoff - MonitorEdges;
939 jvms()->set_scloff(new_scloff);
940 jvms()->set_endoff(new_endoff);
941 while (scloff > new_scloff) del_req(--scloff);
942 assert(jvms()->nof_monitors() == num_before_pop-1, "");
943 }
945 Node *SafePointNode::peek_monitor_box() const {
946 int mon = jvms()->nof_monitors() - 1;
947 assert(mon >= 0, "most have a monitor");
948 return monitor_box(jvms(), mon);
949 }
951 Node *SafePointNode::peek_monitor_obj() const {
952 int mon = jvms()->nof_monitors() - 1;
953 assert(mon >= 0, "most have a monitor");
954 return monitor_obj(jvms(), mon);
955 }
957 // Do we Match on this edge index or not? Match no edges
958 uint SafePointNode::match_edge(uint idx) const {
959 if( !needs_polling_address_input() )
960 return 0;
962 return (TypeFunc::Parms == idx);
963 }
965 //============== SafePointScalarObjectNode ==============
967 SafePointScalarObjectNode::SafePointScalarObjectNode(const TypeOopPtr* tp,
968 #ifdef ASSERT
969 AllocateNode* alloc,
970 #endif
971 uint first_index,
972 uint n_fields) :
973 TypeNode(tp, 1), // 1 control input -- seems required. Get from root.
974 #ifdef ASSERT
975 _alloc(alloc),
976 #endif
977 _first_index(first_index),
978 _n_fields(n_fields)
979 {
980 init_class_id(Class_SafePointScalarObject);
981 }
983 bool SafePointScalarObjectNode::pinned() const { return true; }
984 bool SafePointScalarObjectNode::depends_only_on_test() const { return false; }
986 uint SafePointScalarObjectNode::ideal_reg() const {
987 return 0; // No matching to machine instruction
988 }
990 const RegMask &SafePointScalarObjectNode::in_RegMask(uint idx) const {
991 return *(Compile::current()->matcher()->idealreg2debugmask[in(idx)->ideal_reg()]);
992 }
994 const RegMask &SafePointScalarObjectNode::out_RegMask() const {
995 return RegMask::Empty;
996 }
998 uint SafePointScalarObjectNode::match_edge(uint idx) const {
999 return 0;
1000 }
1002 SafePointScalarObjectNode*
1003 SafePointScalarObjectNode::clone(int jvms_adj, Dict* sosn_map) const {
1004 void* cached = (*sosn_map)[(void*)this];
1005 if (cached != NULL) {
1006 return (SafePointScalarObjectNode*)cached;
1007 }
1008 Compile* C = Compile::current();
1009 SafePointScalarObjectNode* res = (SafePointScalarObjectNode*)Node::clone();
1010 res->_first_index += jvms_adj;
1011 sosn_map->Insert((void*)this, (void*)res);
1012 return res;
1013 }
1016 #ifndef PRODUCT
1017 void SafePointScalarObjectNode::dump_spec(outputStream *st) const {
1018 st->print(" # fields@[%d..%d]", first_index(),
1019 first_index() + n_fields() - 1);
1020 }
1022 #endif
1024 //=============================================================================
1025 uint AllocateNode::size_of() const { return sizeof(*this); }
1027 AllocateNode::AllocateNode(Compile* C, const TypeFunc *atype,
1028 Node *ctrl, Node *mem, Node *abio,
1029 Node *size, Node *klass_node, Node *initial_test)
1030 : CallNode(atype, NULL, TypeRawPtr::BOTTOM)
1031 {
1032 init_class_id(Class_Allocate);
1033 init_flags(Flag_is_macro);
1034 _is_scalar_replaceable = false;
1035 Node *topnode = C->top();
1037 init_req( TypeFunc::Control , ctrl );
1038 init_req( TypeFunc::I_O , abio );
1039 init_req( TypeFunc::Memory , mem );
1040 init_req( TypeFunc::ReturnAdr, topnode );
1041 init_req( TypeFunc::FramePtr , topnode );
1042 init_req( AllocSize , size);
1043 init_req( KlassNode , klass_node);
1044 init_req( InitialTest , initial_test);
1045 init_req( ALength , topnode);
1046 C->add_macro_node(this);
1047 }
1049 //=============================================================================
1050 uint AllocateArrayNode::size_of() const { return sizeof(*this); }
1052 Node* AllocateArrayNode::Ideal(PhaseGVN *phase, bool can_reshape) {
1053 if (remove_dead_region(phase, can_reshape)) return this;
1055 const Type* type = phase->type(Ideal_length());
1056 if (type->isa_int() && type->is_int()->_hi < 0) {
1057 if (can_reshape) {
1058 PhaseIterGVN *igvn = phase->is_IterGVN();
1059 // Unreachable fall through path (negative array length),
1060 // the allocation can only throw so disconnect it.
1061 Node* proj = proj_out(TypeFunc::Control);
1062 Node* catchproj = NULL;
1063 if (proj != NULL) {
1064 for (DUIterator_Fast imax, i = proj->fast_outs(imax); i < imax; i++) {
1065 Node *cn = proj->fast_out(i);
1066 if (cn->is_Catch()) {
1067 catchproj = cn->as_Multi()->proj_out(CatchProjNode::fall_through_index);
1068 break;
1069 }
1070 }
1071 }
1072 if (catchproj != NULL && catchproj->outcnt() > 0 &&
1073 (catchproj->outcnt() > 1 ||
1074 catchproj->unique_out()->Opcode() != Op_Halt)) {
1075 assert(catchproj->is_CatchProj(), "must be a CatchProjNode");
1076 Node* nproj = catchproj->clone();
1077 igvn->register_new_node_with_optimizer(nproj);
1079 Node *frame = new (phase->C, 1) ParmNode( phase->C->start(), TypeFunc::FramePtr );
1080 frame = phase->transform(frame);
1081 // Halt & Catch Fire
1082 Node *halt = new (phase->C, TypeFunc::Parms) HaltNode( nproj, frame );
1083 phase->C->root()->add_req(halt);
1084 phase->transform(halt);
1086 igvn->replace_node(catchproj, phase->C->top());
1087 return this;
1088 }
1089 } else {
1090 // Can't correct it during regular GVN so register for IGVN
1091 phase->C->record_for_igvn(this);
1092 }
1093 }
1094 return NULL;
1095 }
1097 // Retrieve the length from the AllocateArrayNode. Narrow the type with a
1098 // CastII, if appropriate. If we are not allowed to create new nodes, and
1099 // a CastII is appropriate, return NULL.
1100 Node *AllocateArrayNode::make_ideal_length(const TypeOopPtr* oop_type, PhaseTransform *phase, bool allow_new_nodes) {
1101 Node *length = in(AllocateNode::ALength);
1102 assert(length != NULL, "length is not null");
1104 const TypeInt* length_type = phase->find_int_type(length);
1105 const TypeAryPtr* ary_type = oop_type->isa_aryptr();
1107 if (ary_type != NULL && length_type != NULL) {
1108 const TypeInt* narrow_length_type = ary_type->narrow_size_type(length_type);
1109 if (narrow_length_type != length_type) {
1110 // Assert one of:
1111 // - the narrow_length is 0
1112 // - the narrow_length is not wider than length
1113 assert(narrow_length_type == TypeInt::ZERO ||
1114 (narrow_length_type->_hi <= length_type->_hi &&
1115 narrow_length_type->_lo >= length_type->_lo),
1116 "narrow type must be narrower than length type");
1118 // Return NULL if new nodes are not allowed
1119 if (!allow_new_nodes) return NULL;
1120 // Create a cast which is control dependent on the initialization to
1121 // propagate the fact that the array length must be positive.
1122 length = new (phase->C, 2) CastIINode(length, narrow_length_type);
1123 length->set_req(0, initialization()->proj_out(0));
1124 }
1125 }
1127 return length;
1128 }
1130 //=============================================================================
1131 uint LockNode::size_of() const { return sizeof(*this); }
1133 // Redundant lock elimination
1134 //
1135 // There are various patterns of locking where we release and
1136 // immediately reacquire a lock in a piece of code where no operations
1137 // occur in between that would be observable. In those cases we can
1138 // skip releasing and reacquiring the lock without violating any
1139 // fairness requirements. Doing this around a loop could cause a lock
1140 // to be held for a very long time so we concentrate on non-looping
1141 // control flow. We also require that the operations are fully
1142 // redundant meaning that we don't introduce new lock operations on
1143 // some paths so to be able to eliminate it on others ala PRE. This
1144 // would probably require some more extensive graph manipulation to
1145 // guarantee that the memory edges were all handled correctly.
1146 //
1147 // Assuming p is a simple predicate which can't trap in any way and s
1148 // is a synchronized method consider this code:
1149 //
1150 // s();
1151 // if (p)
1152 // s();
1153 // else
1154 // s();
1155 // s();
1156 //
1157 // 1. The unlocks of the first call to s can be eliminated if the
1158 // locks inside the then and else branches are eliminated.
1159 //
1160 // 2. The unlocks of the then and else branches can be eliminated if
1161 // the lock of the final call to s is eliminated.
1162 //
1163 // Either of these cases subsumes the simple case of sequential control flow
1164 //
1165 // Addtionally we can eliminate versions without the else case:
1166 //
1167 // s();
1168 // if (p)
1169 // s();
1170 // s();
1171 //
1172 // 3. In this case we eliminate the unlock of the first s, the lock
1173 // and unlock in the then case and the lock in the final s.
1174 //
1175 // Note also that in all these cases the then/else pieces don't have
1176 // to be trivial as long as they begin and end with synchronization
1177 // operations.
1178 //
1179 // s();
1180 // if (p)
1181 // s();
1182 // f();
1183 // s();
1184 // s();
1185 //
1186 // The code will work properly for this case, leaving in the unlock
1187 // before the call to f and the relock after it.
1188 //
1189 // A potentially interesting case which isn't handled here is when the
1190 // locking is partially redundant.
1191 //
1192 // s();
1193 // if (p)
1194 // s();
1195 //
1196 // This could be eliminated putting unlocking on the else case and
1197 // eliminating the first unlock and the lock in the then side.
1198 // Alternatively the unlock could be moved out of the then side so it
1199 // was after the merge and the first unlock and second lock
1200 // eliminated. This might require less manipulation of the memory
1201 // state to get correct.
1202 //
1203 // Additionally we might allow work between a unlock and lock before
1204 // giving up eliminating the locks. The current code disallows any
1205 // conditional control flow between these operations. A formulation
1206 // similar to partial redundancy elimination computing the
1207 // availability of unlocking and the anticipatability of locking at a
1208 // program point would allow detection of fully redundant locking with
1209 // some amount of work in between. I'm not sure how often I really
1210 // think that would occur though. Most of the cases I've seen
1211 // indicate it's likely non-trivial work would occur in between.
1212 // There may be other more complicated constructs where we could
1213 // eliminate locking but I haven't seen any others appear as hot or
1214 // interesting.
1215 //
1216 // Locking and unlocking have a canonical form in ideal that looks
1217 // roughly like this:
1218 //
1219 // <obj>
1220 // | \\------+
1221 // | \ \
1222 // | BoxLock \
1223 // | | | \
1224 // | | \ \
1225 // | | FastLock
1226 // | | /
1227 // | | /
1228 // | | |
1229 //
1230 // Lock
1231 // |
1232 // Proj #0
1233 // |
1234 // MembarAcquire
1235 // |
1236 // Proj #0
1237 //
1238 // MembarRelease
1239 // |
1240 // Proj #0
1241 // |
1242 // Unlock
1243 // |
1244 // Proj #0
1245 //
1246 //
1247 // This code proceeds by processing Lock nodes during PhaseIterGVN
1248 // and searching back through its control for the proper code
1249 // patterns. Once it finds a set of lock and unlock operations to
1250 // eliminate they are marked as eliminatable which causes the
1251 // expansion of the Lock and Unlock macro nodes to make the operation a NOP
1252 //
1253 //=============================================================================
1255 //
1256 // Utility function to skip over uninteresting control nodes. Nodes skipped are:
1257 // - copy regions. (These may not have been optimized away yet.)
1258 // - eliminated locking nodes
1259 //
1260 static Node *next_control(Node *ctrl) {
1261 if (ctrl == NULL)
1262 return NULL;
1263 while (1) {
1264 if (ctrl->is_Region()) {
1265 RegionNode *r = ctrl->as_Region();
1266 Node *n = r->is_copy();
1267 if (n == NULL)
1268 break; // hit a region, return it
1269 else
1270 ctrl = n;
1271 } else if (ctrl->is_Proj()) {
1272 Node *in0 = ctrl->in(0);
1273 if (in0->is_AbstractLock() && in0->as_AbstractLock()->is_eliminated()) {
1274 ctrl = in0->in(0);
1275 } else {
1276 break;
1277 }
1278 } else {
1279 break; // found an interesting control
1280 }
1281 }
1282 return ctrl;
1283 }
1284 //
1285 // Given a control, see if it's the control projection of an Unlock which
1286 // operating on the same object as lock.
1287 //
1288 bool AbstractLockNode::find_matching_unlock(const Node* ctrl, LockNode* lock,
1289 GrowableArray<AbstractLockNode*> &lock_ops) {
1290 ProjNode *ctrl_proj = (ctrl->is_Proj()) ? ctrl->as_Proj() : NULL;
1291 if (ctrl_proj != NULL && ctrl_proj->_con == TypeFunc::Control) {
1292 Node *n = ctrl_proj->in(0);
1293 if (n != NULL && n->is_Unlock()) {
1294 UnlockNode *unlock = n->as_Unlock();
1295 if ((lock->obj_node() == unlock->obj_node()) &&
1296 (lock->box_node() == unlock->box_node()) && !unlock->is_eliminated()) {
1297 lock_ops.append(unlock);
1298 return true;
1299 }
1300 }
1301 }
1302 return false;
1303 }
1305 //
1306 // Find the lock matching an unlock. Returns null if a safepoint
1307 // or complicated control is encountered first.
1308 LockNode *AbstractLockNode::find_matching_lock(UnlockNode* unlock) {
1309 LockNode *lock_result = NULL;
1310 // find the matching lock, or an intervening safepoint
1311 Node *ctrl = next_control(unlock->in(0));
1312 while (1) {
1313 assert(ctrl != NULL, "invalid control graph");
1314 assert(!ctrl->is_Start(), "missing lock for unlock");
1315 if (ctrl->is_top()) break; // dead control path
1316 if (ctrl->is_Proj()) ctrl = ctrl->in(0);
1317 if (ctrl->is_SafePoint()) {
1318 break; // found a safepoint (may be the lock we are searching for)
1319 } else if (ctrl->is_Region()) {
1320 // Check for a simple diamond pattern. Punt on anything more complicated
1321 if (ctrl->req() == 3 && ctrl->in(1) != NULL && ctrl->in(2) != NULL) {
1322 Node *in1 = next_control(ctrl->in(1));
1323 Node *in2 = next_control(ctrl->in(2));
1324 if (((in1->is_IfTrue() && in2->is_IfFalse()) ||
1325 (in2->is_IfTrue() && in1->is_IfFalse())) && (in1->in(0) == in2->in(0))) {
1326 ctrl = next_control(in1->in(0)->in(0));
1327 } else {
1328 break;
1329 }
1330 } else {
1331 break;
1332 }
1333 } else {
1334 ctrl = next_control(ctrl->in(0)); // keep searching
1335 }
1336 }
1337 if (ctrl->is_Lock()) {
1338 LockNode *lock = ctrl->as_Lock();
1339 if ((lock->obj_node() == unlock->obj_node()) &&
1340 (lock->box_node() == unlock->box_node())) {
1341 lock_result = lock;
1342 }
1343 }
1344 return lock_result;
1345 }
1347 // This code corresponds to case 3 above.
1349 bool AbstractLockNode::find_lock_and_unlock_through_if(Node* node, LockNode* lock,
1350 GrowableArray<AbstractLockNode*> &lock_ops) {
1351 Node* if_node = node->in(0);
1352 bool if_true = node->is_IfTrue();
1354 if (if_node->is_If() && if_node->outcnt() == 2 && (if_true || node->is_IfFalse())) {
1355 Node *lock_ctrl = next_control(if_node->in(0));
1356 if (find_matching_unlock(lock_ctrl, lock, lock_ops)) {
1357 Node* lock1_node = NULL;
1358 ProjNode* proj = if_node->as_If()->proj_out(!if_true);
1359 if (if_true) {
1360 if (proj->is_IfFalse() && proj->outcnt() == 1) {
1361 lock1_node = proj->unique_out();
1362 }
1363 } else {
1364 if (proj->is_IfTrue() && proj->outcnt() == 1) {
1365 lock1_node = proj->unique_out();
1366 }
1367 }
1368 if (lock1_node != NULL && lock1_node->is_Lock()) {
1369 LockNode *lock1 = lock1_node->as_Lock();
1370 if ((lock->obj_node() == lock1->obj_node()) &&
1371 (lock->box_node() == lock1->box_node()) && !lock1->is_eliminated()) {
1372 lock_ops.append(lock1);
1373 return true;
1374 }
1375 }
1376 }
1377 }
1379 lock_ops.trunc_to(0);
1380 return false;
1381 }
1383 bool AbstractLockNode::find_unlocks_for_region(const RegionNode* region, LockNode* lock,
1384 GrowableArray<AbstractLockNode*> &lock_ops) {
1385 // check each control merging at this point for a matching unlock.
1386 // in(0) should be self edge so skip it.
1387 for (int i = 1; i < (int)region->req(); i++) {
1388 Node *in_node = next_control(region->in(i));
1389 if (in_node != NULL) {
1390 if (find_matching_unlock(in_node, lock, lock_ops)) {
1391 // found a match so keep on checking.
1392 continue;
1393 } else if (find_lock_and_unlock_through_if(in_node, lock, lock_ops)) {
1394 continue;
1395 }
1397 // If we fall through to here then it was some kind of node we
1398 // don't understand or there wasn't a matching unlock, so give
1399 // up trying to merge locks.
1400 lock_ops.trunc_to(0);
1401 return false;
1402 }
1403 }
1404 return true;
1406 }
1408 #ifndef PRODUCT
1409 //
1410 // Create a counter which counts the number of times this lock is acquired
1411 //
1412 void AbstractLockNode::create_lock_counter(JVMState* state) {
1413 _counter = OptoRuntime::new_named_counter(state, NamedCounter::LockCounter);
1414 }
1415 #endif
1417 void AbstractLockNode::set_eliminated() {
1418 _eliminate = true;
1419 #ifndef PRODUCT
1420 if (_counter) {
1421 // Update the counter to indicate that this lock was eliminated.
1422 // The counter update code will stay around even though the
1423 // optimizer will eliminate the lock operation itself.
1424 _counter->set_tag(NamedCounter::EliminatedLockCounter);
1425 }
1426 #endif
1427 }
1429 //=============================================================================
1430 Node *LockNode::Ideal(PhaseGVN *phase, bool can_reshape) {
1432 // perform any generic optimizations first (returns 'this' or NULL)
1433 Node *result = SafePointNode::Ideal(phase, can_reshape);
1435 // Now see if we can optimize away this lock. We don't actually
1436 // remove the locking here, we simply set the _eliminate flag which
1437 // prevents macro expansion from expanding the lock. Since we don't
1438 // modify the graph, the value returned from this function is the
1439 // one computed above.
1440 if (result == NULL && can_reshape && EliminateLocks && !is_eliminated()) {
1441 //
1442 // If we are locking an unescaped object, the lock/unlock is unnecessary
1443 //
1444 ConnectionGraph *cgr = phase->C->congraph();
1445 PointsToNode::EscapeState es = PointsToNode::GlobalEscape;
1446 if (cgr != NULL)
1447 es = cgr->escape_state(obj_node(), phase);
1448 if (es != PointsToNode::UnknownEscape && es != PointsToNode::GlobalEscape) {
1449 // Mark it eliminated to update any counters
1450 this->set_eliminated();
1451 return result;
1452 }
1454 //
1455 // Try lock coarsening
1456 //
1457 PhaseIterGVN* iter = phase->is_IterGVN();
1458 if (iter != NULL) {
1460 GrowableArray<AbstractLockNode*> lock_ops;
1462 Node *ctrl = next_control(in(0));
1464 // now search back for a matching Unlock
1465 if (find_matching_unlock(ctrl, this, lock_ops)) {
1466 // found an unlock directly preceding this lock. This is the
1467 // case of single unlock directly control dependent on a
1468 // single lock which is the trivial version of case 1 or 2.
1469 } else if (ctrl->is_Region() ) {
1470 if (find_unlocks_for_region(ctrl->as_Region(), this, lock_ops)) {
1471 // found lock preceded by multiple unlocks along all paths
1472 // joining at this point which is case 3 in description above.
1473 }
1474 } else {
1475 // see if this lock comes from either half of an if and the
1476 // predecessors merges unlocks and the other half of the if
1477 // performs a lock.
1478 if (find_lock_and_unlock_through_if(ctrl, this, lock_ops)) {
1479 // found unlock splitting to an if with locks on both branches.
1480 }
1481 }
1483 if (lock_ops.length() > 0) {
1484 // add ourselves to the list of locks to be eliminated.
1485 lock_ops.append(this);
1487 #ifndef PRODUCT
1488 if (PrintEliminateLocks) {
1489 int locks = 0;
1490 int unlocks = 0;
1491 for (int i = 0; i < lock_ops.length(); i++) {
1492 AbstractLockNode* lock = lock_ops.at(i);
1493 if (lock->Opcode() == Op_Lock)
1494 locks++;
1495 else
1496 unlocks++;
1497 if (Verbose) {
1498 lock->dump(1);
1499 }
1500 }
1501 tty->print_cr("***Eliminated %d unlocks and %d locks", unlocks, locks);
1502 }
1503 #endif
1505 // for each of the identified locks, mark them
1506 // as eliminatable
1507 for (int i = 0; i < lock_ops.length(); i++) {
1508 AbstractLockNode* lock = lock_ops.at(i);
1510 // Mark it eliminated to update any counters
1511 lock->set_eliminated();
1512 lock->set_coarsened();
1513 }
1514 } else if (result != NULL && ctrl->is_Region() &&
1515 iter->_worklist.member(ctrl)) {
1516 // We weren't able to find any opportunities but the region this
1517 // lock is control dependent on hasn't been processed yet so put
1518 // this lock back on the worklist so we can check again once any
1519 // region simplification has occurred.
1520 iter->_worklist.push(this);
1521 }
1522 }
1523 }
1525 return result;
1526 }
1528 //=============================================================================
1529 uint UnlockNode::size_of() const { return sizeof(*this); }
1531 //=============================================================================
1532 Node *UnlockNode::Ideal(PhaseGVN *phase, bool can_reshape) {
1534 // perform any generic optimizations first (returns 'this' or NULL)
1535 Node * result = SafePointNode::Ideal(phase, can_reshape);
1537 // Now see if we can optimize away this unlock. We don't actually
1538 // remove the unlocking here, we simply set the _eliminate flag which
1539 // prevents macro expansion from expanding the unlock. Since we don't
1540 // modify the graph, the value returned from this function is the
1541 // one computed above.
1542 // Escape state is defined after Parse phase.
1543 if (result == NULL && can_reshape && EliminateLocks && !is_eliminated()) {
1544 //
1545 // If we are unlocking an unescaped object, the lock/unlock is unnecessary.
1546 //
1547 ConnectionGraph *cgr = phase->C->congraph();
1548 PointsToNode::EscapeState es = PointsToNode::GlobalEscape;
1549 if (cgr != NULL)
1550 es = cgr->escape_state(obj_node(), phase);
1551 if (es != PointsToNode::UnknownEscape && es != PointsToNode::GlobalEscape) {
1552 // Mark it eliminated to update any counters
1553 this->set_eliminated();
1554 }
1555 }
1556 return result;
1557 }