macro.cpp revision 196:d1605aabd0a1
1185089Sraj/*
2209131Sraj * Copyright 2005-2008 Sun Microsystems, Inc.  All Rights Reserved.
3209131Sraj * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4240489Sgber *
5185089Sraj * This code is free software; you can redistribute it and/or modify it
6185089Sraj * under the terms of the GNU General Public License version 2 only, as
7185089Sraj * published by the Free Software Foundation.
8185089Sraj *
9209131Sraj * This code is distributed in the hope that it will be useful, but WITHOUT
10209131Sraj * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11209131Sraj * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12185089Sraj * version 2 for more details (a copy is included in the LICENSE file that
13185089Sraj * accompanied this code).
14185089Sraj *
15185089Sraj * You should have received a copy of the GNU General Public License version
16185089Sraj * 2 along with this work; if not, write to the Free Software Foundation,
17185089Sraj * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18185089Sraj *
19185089Sraj * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20185089Sraj * CA 95054 USA or visit www.sun.com if you need additional information or
21185089Sraj * have any questions.
22185089Sraj *
23185089Sraj */
24185089Sraj
25185089Sraj#include "incls/_precompiled.incl"
26185089Sraj#include "incls/_macro.cpp.incl"
27185089Sraj
28185089Sraj
29185089Sraj//
30185089Sraj// Replace any references to "oldref" in inputs to "use" with "newref".
31185089Sraj// Returns the number of replacements made.
32185089Sraj//
33185089Srajint PhaseMacroExpand::replace_input(Node *use, Node *oldref, Node *newref) {
34185089Sraj  int nreplacements = 0;
35185089Sraj  uint req = use->req();
36185089Sraj  for (uint j = 0; j < use->len(); j++) {
37185089Sraj    Node *uin = use->in(j);
38185089Sraj    if (uin == oldref) {
39185089Sraj      if (j < req)
40185089Sraj        use->set_req(j, newref);
41185089Sraj      else
42185089Sraj        use->set_prec(j, newref);
43185089Sraj      nreplacements++;
44185089Sraj    } else if (j >= req && uin == NULL) {
45185089Sraj      break;
46185089Sraj    }
47185089Sraj  }
48185089Sraj  return nreplacements;
49185089Sraj}
50185089Sraj
51185089Srajvoid PhaseMacroExpand::copy_call_debug_info(CallNode *oldcall, CallNode * newcall) {
52185089Sraj  // Copy debug information and adjust JVMState information
53185089Sraj  uint old_dbg_start = oldcall->tf()->domain()->cnt();
54185089Sraj  uint new_dbg_start = newcall->tf()->domain()->cnt();
55185089Sraj  int jvms_adj  = new_dbg_start - old_dbg_start;
56240493Sgber  assert (new_dbg_start == newcall->req(), "argument count mismatch");
57240493Sgber
58185089Sraj  Dict* sosn_map = new Dict(cmpkey,hashkey);
59185089Sraj  for (uint i = old_dbg_start; i < oldcall->req(); i++) {
60185089Sraj    Node* old_in = oldcall->in(i);
61209131Sraj    // Clone old SafePointScalarObjectNodes, adjusting their field contents.
62209131Sraj    if (old_in->is_SafePointScalarObject()) {
63209131Sraj      SafePointScalarObjectNode* old_sosn = old_in->as_SafePointScalarObject();
64185089Sraj      uint old_unique = C->unique();
65185089Sraj      Node* new_in = old_sosn->clone(jvms_adj, sosn_map);
66185089Sraj      if (old_unique != C->unique()) {
67185089Sraj        new_in = transform_later(new_in); // Register new node.
68209131Sraj      }
69185089Sraj      old_in = new_in;
70185089Sraj    }
71185089Sraj    newcall->add_req(old_in);
72185089Sraj  }
73185089Sraj
74185089Sraj  newcall->set_jvms(oldcall->jvms());
75185089Sraj  for (JVMState *jvms = newcall->jvms(); jvms != NULL; jvms = jvms->caller()) {
76209131Sraj    jvms->set_map(newcall);
77185089Sraj    jvms->set_locoff(jvms->locoff()+jvms_adj);
78240493Sgber    jvms->set_stkoff(jvms->stkoff()+jvms_adj);
79240493Sgber    jvms->set_monoff(jvms->monoff()+jvms_adj);
80240493Sgber    jvms->set_scloff(jvms->scloff()+jvms_adj);
81240493Sgber    jvms->set_endoff(jvms->endoff()+jvms_adj);
82240493Sgber  }
83240493Sgber}
84185089Sraj
85185089SrajNode* PhaseMacroExpand::opt_iff(Node* region, Node* iff) {
86185089Sraj  IfNode *opt_iff = transform_later(iff)->as_If();
87185089Sraj
88185089Sraj  // Fast path taken; set region slot 2
89185089Sraj  Node *fast_taken = transform_later( new (C, 1) IfFalseNode(opt_iff) );
90185089Sraj  region->init_req(2,fast_taken); // Capture fast-control
91185089Sraj
92185089Sraj  // Fast path not-taken, i.e. slow path
93185089Sraj  Node *slow_taken = transform_later( new (C, 1) IfTrueNode(opt_iff) );
94185089Sraj  return slow_taken;
95185089Sraj}
96185089Sraj
97185089Sraj//--------------------copy_predefined_input_for_runtime_call--------------------
98185089Srajvoid PhaseMacroExpand::copy_predefined_input_for_runtime_call(Node * ctrl, CallNode* oldcall, CallNode* call) {
99185089Sraj  // Set fixed predefined input arguments
100240489Sgber  call->init_req( TypeFunc::Control, ctrl );
101240489Sgber  call->init_req( TypeFunc::I_O    , oldcall->in( TypeFunc::I_O) );
102185089Sraj  call->init_req( TypeFunc::Memory , oldcall->in( TypeFunc::Memory ) ); // ?????
103240489Sgber  call->init_req( TypeFunc::ReturnAdr, oldcall->in( TypeFunc::ReturnAdr ) );
104185089Sraj  call->init_req( TypeFunc::FramePtr, oldcall->in( TypeFunc::FramePtr ) );
105240489Sgber}
106240489Sgber
107185089Sraj//------------------------------make_slow_call---------------------------------
108240489SgberCallNode* PhaseMacroExpand::make_slow_call(CallNode *oldcall, const TypeFunc* slow_call_type, address slow_call, const char* leaf_name, Node* slow_path, Node* parm0, Node* parm1) {
109240489Sgber
110240489Sgber  // Slow-path call
111240489Sgber  int size = slow_call_type->domain()->cnt();
112240489Sgber CallNode *call = leaf_name
113240489Sgber   ? (CallNode*)new (C, size) CallLeafNode      ( slow_call_type, slow_call, leaf_name, TypeRawPtr::BOTTOM )
114209131Sraj   : (CallNode*)new (C, size) CallStaticJavaNode( slow_call_type, slow_call, OptoRuntime::stub_name(slow_call), oldcall->jvms()->bci(), TypeRawPtr::BOTTOM );
115185089Sraj
116185089Sraj  // Slow path call has no side-effects, uses few values
117209131Sraj  copy_predefined_input_for_runtime_call(slow_path, oldcall, call );
118209131Sraj  if (parm0 != NULL)  call->init_req(TypeFunc::Parms+0, parm0);
119209131Sraj  if (parm1 != NULL)  call->init_req(TypeFunc::Parms+1, parm1);
120240489Sgber  copy_call_debug_info(oldcall, call);
121240489Sgber  call->set_cnt(PROB_UNLIKELY_MAG(4));  // Same effect as RC_UNCOMMON.
122240489Sgber  _igvn.hash_delete(oldcall);
123209131Sraj  _igvn.subsume_node(oldcall, call);
124185089Sraj  transform_later(call);
125209131Sraj
126209131Sraj  return call;
127209131Sraj}
128240489Sgber
129240489Sgbervoid PhaseMacroExpand::extract_call_projections(CallNode *call) {
130209131Sraj  _fallthroughproj = NULL;
131185089Sraj  _fallthroughcatchproj = NULL;
132185089Sraj  _ioproj_fallthrough = NULL;
133185089Sraj  _ioproj_catchall = NULL;
134185089Sraj  _catchallcatchproj = NULL;
135185089Sraj  _memproj_fallthrough = NULL;
136185089Sraj  _memproj_catchall = NULL;
137240493Sgber  _resproj = NULL;
138240493Sgber  for (DUIterator_Fast imax, i = call->fast_outs(imax); i < imax; i++) {
139240493Sgber    ProjNode *pn = call->fast_out(i)->as_Proj();
140185089Sraj    switch (pn->_con) {
141185089Sraj      case TypeFunc::Control:
142209131Sraj      {
143240489Sgber        // For Control (fallthrough) and I_O (catch_all_index) we have CatchProj -> Catch -> Proj
144185089Sraj        _fallthroughproj = pn;
145209131Sraj        DUIterator_Fast jmax, j = pn->fast_outs(jmax);
146185089Sraj        const Node *cn = pn->fast_out(j);
147185089Sraj        if (cn->is_Catch()) {
148209131Sraj          ProjNode *cpn = NULL;
149209131Sraj          for (DUIterator_Fast kmax, k = cn->fast_outs(kmax); k < kmax; k++) {
150209131Sraj            cpn = cn->fast_out(k)->as_Proj();
151209131Sraj            assert(cpn->is_CatchProj(), "must be a CatchProjNode");
152209131Sraj            if (cpn->_con == CatchProjNode::fall_through_index)
153209131Sraj              _fallthroughcatchproj = cpn;
154209131Sraj            else {
155209131Sraj              assert(cpn->_con == CatchProjNode::catch_all_index, "must be correct index.");
156209131Sraj              _catchallcatchproj = cpn;
157209131Sraj            }
158209131Sraj          }
159209131Sraj        }
160240489Sgber        break;
161240489Sgber      }
162185089Sraj      case TypeFunc::I_O:
163209131Sraj        if (pn->_is_io_use)
164209131Sraj          _ioproj_catchall = pn;
165209131Sraj        else
166209131Sraj          _ioproj_fallthrough = pn;
167209131Sraj        break;
168185089Sraj      case TypeFunc::Memory:
169209131Sraj        if (pn->_is_io_use)
170185089Sraj          _memproj_catchall = pn;
171209131Sraj        else
172209131Sraj          _memproj_fallthrough = pn;
173185089Sraj        break;
174209131Sraj      case TypeFunc::Parms:
175209131Sraj        _resproj = pn;
176209131Sraj        break;
177185089Sraj      default:
178209131Sraj        assert(false, "unexpected projection from allocation node.");
179240493Sgber    }
180240493Sgber  }
181240493Sgber
182240493Sgber}
183240493Sgber
184185089Sraj// Eliminate a card mark sequence.  p2x is a ConvP2XNode
185185089Srajvoid PhaseMacroExpand::eliminate_card_mark(Node *p2x) {
186185089Sraj  assert(p2x->Opcode() == Op_CastP2X, "ConvP2XNode required");
187185089Sraj  Node *shift = p2x->unique_out();
188209131Sraj  Node *addp = shift->unique_out();
189185089Sraj  for (DUIterator_Last jmin, j = addp->last_outs(jmin); j >= jmin; --j) {
190209131Sraj    Node *st = addp->last_out(j);
191209131Sraj    assert(st->is_Store(), "store required");
192185089Sraj    _igvn.replace_node(st, st->in(MemNode::Memory));
193185089Sraj  }
194209131Sraj}
195209131Sraj
196209131Sraj// Search for a memory operation for the specified memory slice.
197209131Srajstatic Node *scan_mem_chain(Node *mem, int alias_idx, int offset, Node *start_mem, Node *alloc) {
198185089Sraj  Node *orig_mem = mem;
199185089Sraj  Node *alloc_mem = alloc->in(TypeFunc::Memory);
200185089Sraj  while (true) {
201185089Sraj    if (mem == alloc_mem || mem == start_mem ) {
202185089Sraj      return mem;  // hit one of our sentinals
203185089Sraj    } else if (mem->is_MergeMem()) {
204209131Sraj      mem = mem->as_MergeMem()->memory_at(alias_idx);
205209131Sraj    } else if (mem->is_Proj() && mem->as_Proj()->_con == TypeFunc::Memory) {
206209131Sraj      Node *in = mem->in(0);
207209131Sraj      // we can safely skip over safepoints, calls, locks and membars because we
208240493Sgber      // already know that the object is safe to eliminate.
209240493Sgber      if (in->is_Initialize() && in->as_Initialize()->allocation() == alloc) {
210240493Sgber        return in;
211240493Sgber      } else if (in->is_Call() || in->is_MemBar()) {
212240493Sgber        mem = in->in(TypeFunc::Memory);
213240493Sgber      } else {
214240493Sgber        assert(false, "unexpected projection");
215209131Sraj      }
216209131Sraj    } else if (mem->is_Store()) {
217209131Sraj      const TypePtr* atype = mem->as_Store()->adr_type();
218209131Sraj      int adr_idx = Compile::current()->get_alias_index(atype);
219209131Sraj      if (adr_idx == alias_idx) {
220209131Sraj        assert(atype->isa_oopptr(), "address type must be oopptr");
221209131Sraj        int adr_offset = atype->offset();
222227843Smarius        uint adr_iid = atype->is_oopptr()->instance_id();
223185089Sraj        // Array elements references have the same alias_idx
224185089Sraj        // but different offset and different instance_id.
225209131Sraj        if (adr_offset == offset && adr_iid == alloc->_idx)
226185089Sraj          return mem;
227209131Sraj      } else {
228209131Sraj        assert(adr_idx == Compile::AliasIdxRaw, "address must match or be raw");
229185089Sraj      }
230185089Sraj      mem = mem->in(MemNode::Memory);
231185089Sraj    } else {
232185089Sraj      return mem;
233209131Sraj    }
234185089Sraj    if (mem == orig_mem)
235185089Sraj      return mem;
236185089Sraj  }
237185089Sraj}
238209131Sraj
239185089Sraj//
240218228Smarcel// Given a Memory Phi, compute a value Phi containing the values from stores
241185089Sraj// on the input paths.
242218228Smarcel// Note: this function is recursive, its depth is limied by the "level" argument
243218228Smarcel// Returns the computed Phi, or NULL if it cannot compute it.
244209131SrajNode *PhaseMacroExpand::value_from_mem_phi(Node *mem, BasicType ft, const Type *phi_type, const TypeOopPtr *adr_t, Node *alloc, int level) {
245218228Smarcel
246218228Smarcel  if (level <= 0) {
247218228Smarcel    return NULL;
248209131Sraj  }
249185089Sraj  int alias_idx = C->get_alias_index(adr_t);
250209131Sraj  int offset = adr_t->offset();
251209131Sraj  int instance_id = adr_t->instance_id();
252185089Sraj
253185089Sraj  Node *start_mem = C->start()->proj_out(TypeFunc::Memory);
254185089Sraj  Node *alloc_mem = alloc->in(TypeFunc::Memory);
255209131Sraj
256185089Sraj  uint length = mem->req();
257209131Sraj  GrowableArray <Node *> values(length, length, NULL);
258209131Sraj
259240489Sgber  for (uint j = 1; j < length; j++) {
260209131Sraj    Node *in = mem->in(j);
261185089Sraj    if (in == NULL || in->is_top()) {
262185089Sraj      values.at_put(j, in);
263209131Sraj    } else  {
264240489Sgber      Node *val = scan_mem_chain(in, alias_idx, offset, start_mem, alloc);
265185089Sraj      if (val == start_mem || val == alloc_mem) {
266240489Sgber        // hit a sentinel, return appropriate 0 value
267218228Smarcel        values.at_put(j, _igvn.zerocon(ft));
268218228Smarcel        continue;
269218228Smarcel      }
270209131Sraj      if (val->is_Initialize()) {
271240489Sgber        val = val->as_Initialize()->find_captured_store(offset, type2aelembytes(ft), &_igvn);
272240489Sgber      }
273240489Sgber      if (val == NULL) {
274218228Smarcel        return NULL;  // can't find a value on this path
275209131Sraj      }
276240489Sgber      if (val == mem) {
277209131Sraj        values.at_put(j, mem);
278209131Sraj      } else if (val->is_Store()) {
279209131Sraj        values.at_put(j, val->in(MemNode::ValueIn));
280185089Sraj      } else if(val->is_Proj() && val->in(0) == alloc) {
281185089Sraj        values.at_put(j, _igvn.zerocon(ft));
282209131Sraj      } else if (val->is_Phi()) {
283209131Sraj        // Check if an appropriate node already exists.
284209131Sraj        Node* region = val->in(0);
285185089Sraj        Node* old_phi = NULL;
286185089Sraj        for (DUIterator_Fast kmax, k = region->fast_outs(kmax); k < kmax; k++) {
287185089Sraj          Node* phi = region->fast_out(k);
288185089Sraj          if (phi->is_Phi() && phi != val &&
289209131Sraj              phi->as_Phi()->is_same_inst_field(phi_type, instance_id, alias_idx, offset)) {
290185089Sraj            old_phi = phi;
291185089Sraj            break;
292185089Sraj          }
293185089Sraj        }
294185089Sraj        if (old_phi == NULL) {
295240489Sgber          val = value_from_mem_phi(val, ft, phi_type, adr_t, alloc, level-1);
296240489Sgber          if (val == NULL) {
297240489Sgber            return NULL;
298240489Sgber          }
299209131Sraj          values.at_put(j, val);
300240489Sgber        } else {
301240489Sgber          values.at_put(j, old_phi);
302240489Sgber        }
303240489Sgber      } else {
304240489Sgber        return NULL;  // unknown node  on this path
305240489Sgber      }
306240489Sgber    }
307240489Sgber  }
308240489Sgber  // create a new Phi for the value
309209131Sraj  PhiNode *phi = new (C, length) PhiNode(mem->in(0), phi_type, NULL, instance_id, alias_idx, offset);
310209131Sraj  for (uint j = 1; j < length; j++) {
311209131Sraj    if (values.at(j) == mem) {
312209131Sraj      phi->init_req(j, phi);
313209131Sraj    } else {
314209131Sraj      phi->init_req(j, values.at(j));
315209131Sraj    }
316209131Sraj  }
317240489Sgber  transform_later(phi);
318209131Sraj  return phi;
319240489Sgber}
320185089Sraj
321240489Sgber// Search the last value stored into the object's field.
322240489SgberNode *PhaseMacroExpand::value_from_mem(Node *sfpt_mem, BasicType ft, const Type *ftype, const TypeOopPtr *adr_t, Node *alloc) {
323240489Sgber  assert(adr_t->is_instance_field(), "instance required");
324240489Sgber  uint instance_id = adr_t->instance_id();
325240489Sgber  assert(instance_id == alloc->_idx, "wrong allocation");
326240489Sgber
327185089Sraj  int alias_idx = C->get_alias_index(adr_t);
328240489Sgber  int offset = adr_t->offset();
329240489Sgber  Node *start_mem = C->start()->proj_out(TypeFunc::Memory);
330240489Sgber  Node *alloc_ctrl = alloc->in(TypeFunc::Control);
331240489Sgber  Node *alloc_mem = alloc->in(TypeFunc::Memory);
332240489Sgber  VectorSet visited(Thread::current()->resource_area());
333240489Sgber
334240489Sgber
335240489Sgber  bool done = sfpt_mem == alloc_mem;
336240489Sgber  Node *mem = sfpt_mem;
337240489Sgber  while (!done) {
338240489Sgber    if (visited.test_set(mem->_idx)) {
339240489Sgber      return NULL;  // found a loop, give up
340240489Sgber    }
341240489Sgber    mem = scan_mem_chain(mem, alias_idx, offset, start_mem, alloc);
342240493Sgber    if (mem == start_mem || mem == alloc_mem) {
343240489Sgber      done = true;  // hit a sentinel, return appropriate 0 value
344240489Sgber    } else if (mem->is_Initialize()) {
345240489Sgber      mem = mem->as_Initialize()->find_captured_store(offset, type2aelembytes(ft), &_igvn);
346240489Sgber      if (mem == NULL) {
347240489Sgber        done = true; // Something go wrong.
348240489Sgber      } else if (mem->is_Store()) {
349240489Sgber        const TypePtr* atype = mem->as_Store()->adr_type();
350240489Sgber        assert(C->get_alias_index(atype) == Compile::AliasIdxRaw, "store is correct memory slice");
351240489Sgber        done = true;
352240489Sgber      }
353240489Sgber    } else if (mem->is_Store()) {
354240489Sgber      const TypeOopPtr* atype = mem->as_Store()->adr_type()->isa_oopptr();
355240489Sgber      assert(atype != NULL, "address type must be oopptr");
356240489Sgber      assert(C->get_alias_index(atype) == alias_idx &&
357240489Sgber             atype->is_instance_field() && atype->offset() == offset &&
358240489Sgber             atype->instance_id() == instance_id, "store is correct memory slice");
359240489Sgber      done = true;
360240489Sgber    } else if (mem->is_Phi()) {
361240489Sgber      // try to find a phi's unique input
362240489Sgber      Node *unique_input = NULL;
363240489Sgber      Node *top = C->top();
364240489Sgber      for (uint i = 1; i < mem->req(); i++) {
365240489Sgber        Node *n = scan_mem_chain(mem->in(i), alias_idx, offset, start_mem, alloc);
366240489Sgber        if (n == NULL || n == top || n == mem) {
367240489Sgber          continue;
368240489Sgber        } else if (unique_input == NULL) {
369240489Sgber          unique_input = n;
370240489Sgber        } else if (unique_input != n) {
371240489Sgber          unique_input = top;
372240489Sgber          break;
373240489Sgber        }
374240489Sgber      }
375240489Sgber      if (unique_input != NULL && unique_input != top) {
376240489Sgber        mem = unique_input;
377240489Sgber      } else {
378240489Sgber        done = true;
379240489Sgber      }
380240489Sgber    } else {
381240489Sgber      assert(false, "unexpected node");
382240489Sgber    }
383240489Sgber  }
384240489Sgber  if (mem != NULL) {
385240489Sgber    if (mem == start_mem || mem == alloc_mem) {
386240489Sgber      // hit a sentinel, return appropriate 0 value
387240489Sgber      return _igvn.zerocon(ft);
388240489Sgber    } else if (mem->is_Store()) {
389240489Sgber      return mem->in(MemNode::ValueIn);
390240489Sgber    } else if (mem->is_Phi()) {
391240489Sgber      // attempt to produce a Phi reflecting the values on the input paths of the Phi
392240489Sgber      Node * phi = value_from_mem_phi(mem, ft, ftype, adr_t, alloc, 8);
393240489Sgber      if (phi != NULL) {
394240489Sgber        return phi;
395240489Sgber      }
396240489Sgber    }
397240489Sgber  }
398240489Sgber  // Something go wrong.
399209131Sraj  return NULL;
400209131Sraj}
401186932Sraj
402186932Sraj// Check the possibility of scalar replacement.
403186932Srajbool PhaseMacroExpand::can_eliminate_allocation(AllocateNode *alloc, GrowableArray <SafePointNode *>& safepoints) {
404209131Sraj  //  Scan the uses of the allocation to check for anything that would
405209131Sraj  //  prevent us from eliminating it.
406186932Sraj  NOT_PRODUCT( const char* fail_eliminate = NULL; )
407209131Sraj  DEBUG_ONLY( Node* disq_node = NULL; )
408186932Sraj  bool  can_eliminate = true;
409186932Sraj
410186932Sraj  Node* res = alloc->result_cast();
411209131Sraj  const TypeOopPtr* res_type = NULL;
412209131Sraj  if (res == NULL) {
413186932Sraj    // All users were eliminated.
414186932Sraj  } else if (!res->is_CheckCastPP()) {
415186932Sraj    alloc->_is_scalar_replaceable = false;  // don't try again
416209131Sraj    NOT_PRODUCT(fail_eliminate = "Allocation does not have unique CheckCastPP";)
417209131Sraj    can_eliminate = false;
418186932Sraj  } else {
419186932Sraj    res_type = _igvn.type(res)->isa_oopptr();
420186932Sraj    if (res_type == NULL) {
421240489Sgber      NOT_PRODUCT(fail_eliminate = "Neither instance or array allocation";)
422185089Sraj      can_eliminate = false;
423186932Sraj    } else if (res_type->isa_aryptr()) {
424209131Sraj      int length = alloc->in(AllocateNode::ALength)->find_int_con(-1);
425209131Sraj      if (length < 0) {
426240489Sgber        NOT_PRODUCT(fail_eliminate = "Array's size is not constant";)
427186932Sraj        can_eliminate = false;
428185089Sraj      }
429185089Sraj    }
430240489Sgber  }
431240489Sgber
432240489Sgber  if (can_eliminate && res != NULL) {
433240489Sgber    for (DUIterator_Fast jmax, j = res->fast_outs(jmax);
434240489Sgber                               j < jmax && can_eliminate; j++) {
435240489Sgber      Node* use = res->fast_out(j);
436240489Sgber
437240489Sgber      if (use->is_AddP()) {
438240489Sgber        const TypePtr* addp_type = _igvn.type(use)->is_ptr();
439240489Sgber        int offset = addp_type->offset();
440240489Sgber
441240489Sgber        if (offset == Type::OffsetTop || offset == Type::OffsetBot) {
442240489Sgber          NOT_PRODUCT(fail_eliminate = "Undefined field referrence";)
443240489Sgber          can_eliminate = false;
444240489Sgber          break;
445240489Sgber        }
446240489Sgber        for (DUIterator_Fast kmax, k = use->fast_outs(kmax);
447240489Sgber                                   k < kmax && can_eliminate; k++) {
448240489Sgber          Node* n = use->fast_out(k);
449240489Sgber          if (!n->is_Store() && n->Opcode() != Op_CastP2X) {
450240489Sgber            DEBUG_ONLY(disq_node = n;)
451240489Sgber            if (n->is_Load()) {
452240489Sgber              NOT_PRODUCT(fail_eliminate = "Field load";)
453240489Sgber            } else {
454240489Sgber              NOT_PRODUCT(fail_eliminate = "Not store field referrence";)
455240489Sgber            }
456240489Sgber            can_eliminate = false;
457240489Sgber          }
458240489Sgber        }
459240489Sgber      } else if (use->is_SafePoint()) {
460240489Sgber        SafePointNode* sfpt = use->as_SafePoint();
461240489Sgber        if (sfpt->is_Call() && sfpt->as_Call()->has_non_debug_use(res)) {
462240489Sgber          // Object is passed as argument.
463240489Sgber          DEBUG_ONLY(disq_node = use;)
464240489Sgber          NOT_PRODUCT(fail_eliminate = "Object is passed as argument";)
465240489Sgber          can_eliminate = false;
466240489Sgber        }
467240489Sgber        Node* sfptMem = sfpt->memory();
468240489Sgber        if (sfptMem == NULL || sfptMem->is_top()) {
469240489Sgber          DEBUG_ONLY(disq_node = use;)
470240489Sgber          NOT_PRODUCT(fail_eliminate = "NULL or TOP memory";)
471240489Sgber          can_eliminate = false;
472240489Sgber        } else {
473240489Sgber          safepoints.append_if_missing(sfpt);
474240489Sgber        }
475240489Sgber      } else if (use->Opcode() != Op_CastP2X) { // CastP2X is used by card mark
476240489Sgber        if (use->is_Phi()) {
477240489Sgber          if (use->outcnt() == 1 && use->unique_out()->Opcode() == Op_Return) {
478240489Sgber            NOT_PRODUCT(fail_eliminate = "Object is return value";)
479240489Sgber          } else {
480240489Sgber            NOT_PRODUCT(fail_eliminate = "Object is referenced by Phi";)
481240489Sgber          }
482240489Sgber          DEBUG_ONLY(disq_node = use;)
483240489Sgber        } else {
484240489Sgber          if (use->Opcode() == Op_Return) {
485240489Sgber            NOT_PRODUCT(fail_eliminate = "Object is return value";)
486240489Sgber          }else {
487240489Sgber            NOT_PRODUCT(fail_eliminate = "Object is referenced by node";)
488240489Sgber          }
489240489Sgber          DEBUG_ONLY(disq_node = use;)
490240489Sgber        }
491240489Sgber        can_eliminate = false;
492240489Sgber      }
493240489Sgber    }
494240489Sgber  }
495240489Sgber
496240489Sgber#ifndef PRODUCT
497240489Sgber  if (PrintEliminateAllocations) {
498240489Sgber    if (can_eliminate) {
499240489Sgber      tty->print("Scalar ");
500240489Sgber      if (res == NULL)
501240489Sgber        alloc->dump();
502240489Sgber      else
503240489Sgber        res->dump();
504240489Sgber    } else {
505240489Sgber      tty->print("NotScalar (%s)", fail_eliminate);
506240489Sgber      if (res == NULL)
507185089Sraj        alloc->dump();
508209131Sraj      else
509185089Sraj        res->dump();
510185089Sraj#ifdef ASSERT
511240489Sgber      if (disq_node != NULL) {
512185089Sraj          tty->print("  >>>> ");
513185089Sraj          disq_node->dump();
514185089Sraj      }
515240489Sgber#endif /*ASSERT*/
516240489Sgber    }
517240489Sgber  }
518240489Sgber#endif
519240489Sgber  return can_eliminate;
520240489Sgber}
521209131Sraj
522185089Sraj// Do scalar replacement.
523185089Srajbool PhaseMacroExpand::scalar_replacement(AllocateNode *alloc, GrowableArray <SafePointNode *>& safepoints) {
524185089Sraj  GrowableArray <SafePointNode *> safepoints_done;
525185089Sraj
526185089Sraj  ciKlass* klass = NULL;
527185089Sraj  ciInstanceKlass* iklass = NULL;
528240489Sgber  int nfields = 0;
529240489Sgber  int array_base;
530185089Sraj  int element_size;
531185089Sraj  BasicType basic_elem_type;
532185089Sraj  ciType* elem_type;
533240489Sgber
534240489Sgber  Node* res = alloc->result_cast();
535185089Sraj  const TypeOopPtr* res_type = NULL;
536209131Sraj  if (res != NULL) { // Could be NULL when there are no users
537185089Sraj    res_type = _igvn.type(res)->isa_oopptr();
538209131Sraj  }
539185089Sraj
540185089Sraj  if (res != NULL) {
541185089Sraj    klass = res_type->klass();
542185089Sraj    if (res_type->isa_instptr()) {
543185089Sraj      // find the fields of the class which will be needed for safepoint debug information
544185089Sraj      assert(klass->is_instance_klass(), "must be an instance klass.");
545209131Sraj      iklass = klass->as_instance_klass();
546185089Sraj      nfields = iklass->nof_nonstatic_fields();
547185089Sraj    } else {
548185089Sraj      // find the array's elements which will be needed for safepoint debug information
549185089Sraj      nfields = alloc->in(AllocateNode::ALength)->find_int_con(-1);
550185089Sraj      assert(klass->is_array_klass() && nfields >= 0, "must be an array klass.");
551209131Sraj      elem_type = klass->as_array_klass()->element_type();
552209131Sraj      basic_elem_type = elem_type->basic_type();
553209131Sraj      array_base = arrayOopDesc::base_offset_in_bytes(basic_elem_type);
554209131Sraj      element_size = type2aelembytes(basic_elem_type);
555185089Sraj    }
556185089Sraj  }
557209131Sraj  //
558185639Sraj  // Process the safepoint uses
559209131Sraj  //
560185639Sraj  while (safepoints.length() > 0) {
561209131Sraj    SafePointNode* sfpt = safepoints.pop();
562185089Sraj    Node* mem = sfpt->memory();
563209131Sraj    uint first_ind = sfpt->req();
564185089Sraj    SafePointScalarObjectNode* sobj = new (C, 1) SafePointScalarObjectNode(res_type,
565185089Sraj#ifdef ASSERT
566185089Sraj                                                 alloc,
567209131Sraj#endif
568185089Sraj                                                 first_ind, nfields);
569209131Sraj    sobj->init_req(0, sfpt->in(TypeFunc::Control));
570185089Sraj    transform_later(sobj);
571185089Sraj
572185089Sraj    // Scan object's fields adding an input to the safepoint for each field.
573209131Sraj    for (int j = 0; j < nfields; j++) {
574185089Sraj      int offset;
575209131Sraj      ciField* field = NULL;
576185089Sraj      if (iklass != NULL) {
577209131Sraj        field = iklass->nonstatic_field_at(j);
578185089Sraj        offset = field->offset();
579209131Sraj        elem_type = field->type();
580185089Sraj        basic_elem_type = field->layout_type();
581185089Sraj      } else {
582209131Sraj        offset = array_base + j * element_size;
583185089Sraj      }
584185089Sraj
585185089Sraj      const Type *field_type;
586209131Sraj      // The next code is taken from Parse::do_get_xxx().
587185089Sraj      if (basic_elem_type == T_OBJECT || basic_elem_type == T_ARRAY) {
588185089Sraj        if (!elem_type->is_loaded()) {
589185089Sraj          field_type = TypeInstPtr::BOTTOM;
590209131Sraj        } else if (field != NULL && field->is_constant()) {
591185089Sraj          // This can happen if the constant oop is non-perm.
592185089Sraj          ciObject* con = field->constant_value().as_object();
593185089Sraj          // Do not "join" in the previous type; it doesn't add value,
594185089Sraj          // and may yield a vacuous result if the field is of interface type.
595185089Sraj          field_type = TypeOopPtr::make_from_constant(con)->isa_oopptr();
596185089Sraj          assert(field_type != NULL, "field singleton type must be consistent");
597185089Sraj        } else {
598209131Sraj          field_type = TypeOopPtr::make_from_klass(elem_type->as_klass());
599185089Sraj        }
600185089Sraj        if (UseCompressedOops) {
601185089Sraj          field_type = field_type->is_oopptr()->make_narrowoop();
602185089Sraj          basic_elem_type = T_NARROWOOP;
603185089Sraj        }
604185089Sraj      } else {
605185089Sraj        field_type = Type::get_const_basic_type(basic_elem_type);
606185089Sraj      }
607209131Sraj
608185089Sraj      const TypeOopPtr *field_addr_type = res_type->add_offset(offset)->isa_oopptr();
609185089Sraj
610209131Sraj      Node *field_val = value_from_mem(mem, basic_elem_type, field_type, field_addr_type, alloc);
611185089Sraj      if (field_val == NULL) {
612185089Sraj        // we weren't able to find a value for this field,
613209131Sraj        // give up on eliminating this allocation
614185089Sraj        alloc->_is_scalar_replaceable = false;  // don't try again
615185089Sraj        // remove any extra entries we added to the safepoint
616185089Sraj        uint last = sfpt->req() - 1;
617185089Sraj        for (int k = 0;  k < j; k++) {
618185089Sraj          sfpt->del_req(last--);
619185089Sraj        }
620185089Sraj        // rollback processed safepoints
621209131Sraj        while (safepoints_done.length() > 0) {
622185089Sraj          SafePointNode* sfpt_done = safepoints_done.pop();
623185089Sraj          // remove any extra entries we added to the safepoint
624185089Sraj          last = sfpt_done->req() - 1;
625209131Sraj          for (int k = 0;  k < nfields; k++) {
626185089Sraj            sfpt_done->del_req(last--);
627209131Sraj          }
628185089Sraj          JVMState *jvms = sfpt_done->jvms();
629185089Sraj          jvms->set_endoff(sfpt_done->req());
630185089Sraj          // Now make a pass over the debug information replacing any references
631185089Sraj          // to SafePointScalarObjectNode with the allocated object.
632185089Sraj          int start = jvms->debug_start();
633185089Sraj          int end   = jvms->debug_end();
634209131Sraj          for (int i = start; i < end; i++) {
635185089Sraj            if (sfpt_done->in(i)->is_SafePointScalarObject()) {
636185089Sraj              SafePointScalarObjectNode* scobj = sfpt_done->in(i)->as_SafePointScalarObject();
637185089Sraj              if (scobj->first_index() == sfpt_done->req() &&
638185089Sraj                  scobj->n_fields() == (uint)nfields) {
639185089Sraj                assert(scobj->alloc() == alloc, "sanity");
640185089Sraj                sfpt_done->set_req(i, res);
641185089Sraj              }
642185089Sraj            }
643185089Sraj          }
644209131Sraj        }
645209131Sraj#ifndef PRODUCT
646209131Sraj        if (PrintEliminateAllocations) {
647209131Sraj          if (field != NULL) {
648209131Sraj            tty->print("=== At SafePoint node %d can't find value of Field: ",
649209131Sraj                       sfpt->_idx);
650209131Sraj            field->print();
651209131Sraj            int field_idx = C->get_alias_index(field_addr_type);
652209131Sraj            tty->print(" (alias_idx=%d)", field_idx);
653209131Sraj          } else { // Array's element
654209131Sraj            tty->print("=== At SafePoint node %d can't find value of array element [%d]",
655209131Sraj                       sfpt->_idx, j);
656209131Sraj          }
657209131Sraj          tty->print(", which prevents elimination of: ");
658209131Sraj          if (res == NULL)
659209131Sraj            alloc->dump();
660209131Sraj          else
661209131Sraj            res->dump();
662209131Sraj        }
663209131Sraj#endif
664209131Sraj        return false;
665209131Sraj      }
666209131Sraj      if (UseCompressedOops && field_type->isa_narrowoop()) {
667185089Sraj        // Enable "DecodeN(EncodeP(Allocate)) --> Allocate" transformation
668209131Sraj        // to be able scalar replace the allocation.
669185089Sraj        _igvn.set_delay_transform(false);
670185089Sraj        field_val = DecodeNNode::decode(&_igvn, field_val);
671209131Sraj        _igvn.set_delay_transform(true);
672186932Sraj      }
673186932Sraj      sfpt->add_req(field_val);
674185089Sraj    }
675186932Sraj    JVMState *jvms = sfpt->jvms();
676186932Sraj    jvms->set_endoff(sfpt->req());
677209131Sraj    // Now make a pass over the debug information replacing any references
678186932Sraj    // to the allocated object with "sobj"
679186932Sraj    int start = jvms->debug_start();
680209131Sraj    int end   = jvms->debug_end();
681186932Sraj    for (int i = start; i < end; i++) {
682186932Sraj      if (sfpt->in(i) == res) {
683240489Sgber        sfpt->set_req(i, sobj);
684186932Sraj      }
685186932Sraj    }
686186932Sraj    safepoints_done.append_if_missing(sfpt); // keep it for rollback
687240489Sgber  }
688240489Sgber  return true;
689240489Sgber}
690240489Sgber
691240489Sgber// Process users of eliminated allocation.
692240489Sgbervoid PhaseMacroExpand::process_users_of_allocation(AllocateNode *alloc) {
693240489Sgber  Node* res = alloc->result_cast();
694240489Sgber  if (res != NULL) {
695240489Sgber    for (DUIterator_Last jmin, j = res->last_outs(jmin); j >= jmin; ) {
696240489Sgber      Node *use = res->last_out(j);
697186932Sraj      uint oc1 = res->outcnt();
698186932Sraj
699186932Sraj      if (use->is_AddP()) {
700186932Sraj        for (DUIterator_Last kmin, k = use->last_outs(kmin); k >= kmin; ) {
701186932Sraj          Node *n = use->last_out(k);
702209131Sraj          uint oc2 = use->outcnt();
703186932Sraj          if (n->is_Store()) {
704186932Sraj            _igvn.replace_node(n, n->in(MemNode::Memory));
705186932Sraj          } else {
706186932Sraj            assert( n->Opcode() == Op_CastP2X, "CastP2X required");
707186932Sraj            eliminate_card_mark(n);
708186932Sraj          }
709186932Sraj          k -= (oc2 - use->outcnt());
710186932Sraj        }
711186932Sraj      } else {
712185089Sraj        assert( !use->is_SafePoint(), "safepoint uses must have been already elimiated");
713185089Sraj        assert( use->Opcode() == Op_CastP2X, "CastP2X required");
714185089Sraj        eliminate_card_mark(use);
715209131Sraj      }
716185089Sraj      j -= (oc1 - res->outcnt());
717185089Sraj    }
718185089Sraj    assert(res->outcnt() == 0, "all uses of allocated objects must be deleted");
719186932Sraj    _igvn.remove_dead_node(res);
720186932Sraj  }
721186932Sraj
722186932Sraj  //
723186932Sraj  // Process other users of allocation's projections
724185089Sraj  //
725185089Sraj  if (_resproj != NULL && _resproj->outcnt() != 0) {
726185089Sraj    for (DUIterator_Last jmin, j = _resproj->last_outs(jmin); j >= jmin; ) {
727209131Sraj      Node *use = _resproj->last_out(j);
728185089Sraj      uint oc1 = _resproj->outcnt();
729209131Sraj      if (use->is_Initialize()) {
730185089Sraj        // Eliminate Initialize node.
731185089Sraj        InitializeNode *init = use->as_Initialize();
732185089Sraj        assert(init->outcnt() <= 2, "only a control and memory projection expected");
733185089Sraj        Node *ctrl_proj = init->proj_out(TypeFunc::Control);
734185089Sraj        if (ctrl_proj != NULL) {
735185089Sraj           assert(init->in(TypeFunc::Control) == _fallthroughcatchproj, "allocation control projection");
736185089Sraj          _igvn.replace_node(ctrl_proj, _fallthroughcatchproj);
737185089Sraj        }
738185089Sraj        Node *mem_proj = init->proj_out(TypeFunc::Memory);
739185089Sraj        if (mem_proj != NULL) {
740185089Sraj          Node *mem = init->in(TypeFunc::Memory);
741185089Sraj#ifdef ASSERT
742185089Sraj          if (mem->is_MergeMem()) {
743185089Sraj            assert(mem->in(TypeFunc::Memory) == _memproj_fallthrough, "allocation memory projection");
744209131Sraj          } else {
745185089Sraj            assert(mem == _memproj_fallthrough, "allocation memory projection");
746209131Sraj          }
747185089Sraj#endif
748185089Sraj          _igvn.replace_node(mem_proj, mem);
749185089Sraj        }
750185089Sraj      } else if (use->is_AddP()) {
751185089Sraj        // raw memory addresses used only by the initialization
752185089Sraj        _igvn.hash_delete(use);
753185089Sraj        _igvn.subsume_node(use, C->top());
754185089Sraj      } else  {
755185089Sraj        assert(false, "only Initialize or AddP expected");
756209131Sraj      }
757209131Sraj      j -= (oc1 - _resproj->outcnt());
758209131Sraj    }
759209131Sraj  }
760209131Sraj  if (_fallthroughcatchproj != NULL) {
761209131Sraj    _igvn.replace_node(_fallthroughcatchproj, alloc->in(TypeFunc::Control));
762209131Sraj  }
763209131Sraj  if (_memproj_fallthrough != NULL) {
764209131Sraj    _igvn.replace_node(_memproj_fallthrough, alloc->in(TypeFunc::Memory));
765209131Sraj  }
766209131Sraj  if (_memproj_catchall != NULL) {
767209131Sraj    _igvn.replace_node(_memproj_catchall, C->top());
768209131Sraj  }
769209131Sraj  if (_ioproj_fallthrough != NULL) {
770209131Sraj    _igvn.replace_node(_ioproj_fallthrough, alloc->in(TypeFunc::I_O));
771209131Sraj  }
772209131Sraj  if (_ioproj_catchall != NULL) {
773209131Sraj    _igvn.replace_node(_ioproj_catchall, C->top());
774209131Sraj  }
775209131Sraj  if (_catchallcatchproj != NULL) {
776209131Sraj    _igvn.replace_node(_catchallcatchproj, C->top());
777209131Sraj  }
778209131Sraj}
779209131Sraj
780209131Srajbool PhaseMacroExpand::eliminate_allocate_node(AllocateNode *alloc) {
781209131Sraj
782209131Sraj  if (!EliminateAllocations || !alloc->_is_scalar_replaceable) {
783209131Sraj    return false;
784209131Sraj  }
785209131Sraj
786209131Sraj  extract_call_projections(alloc);
787209131Sraj
788209131Sraj  GrowableArray <SafePointNode *> safepoints;
789209131Sraj  if (!can_eliminate_allocation(alloc, safepoints)) {
790209131Sraj    return false;
791209131Sraj  }
792209131Sraj
793209131Sraj  if (!scalar_replacement(alloc, safepoints)) {
794209131Sraj    return false;
795209131Sraj  }
796209131Sraj
797209131Sraj  process_users_of_allocation(alloc);
798209131Sraj
799209131Sraj#ifndef PRODUCT
800209131Srajif (PrintEliminateAllocations) {
801209131Sraj  if (alloc->is_AllocateArray())
802209131Sraj    tty->print_cr("++++ Eliminated: %d AllocateArray", alloc->_idx);
803209131Sraj  else
804209131Sraj    tty->print_cr("++++ Eliminated: %d Allocate", alloc->_idx);
805209131Sraj}
806209131Sraj#endif
807209131Sraj
808209131Sraj  return true;
809209131Sraj}
810209131Sraj
811209131Sraj
812209131Sraj//---------------------------set_eden_pointers-------------------------
813209131Srajvoid PhaseMacroExpand::set_eden_pointers(Node* &eden_top_adr, Node* &eden_end_adr) {
814209131Sraj  if (UseTLAB) {                // Private allocation: load from TLS
815209131Sraj    Node* thread = transform_later(new (C, 1) ThreadLocalNode());
816209131Sraj    int tlab_top_offset = in_bytes(JavaThread::tlab_top_offset());
817209131Sraj    int tlab_end_offset = in_bytes(JavaThread::tlab_end_offset());
818209131Sraj    eden_top_adr = basic_plus_adr(top()/*not oop*/, thread, tlab_top_offset);
819209131Sraj    eden_end_adr = basic_plus_adr(top()/*not oop*/, thread, tlab_end_offset);
820209131Sraj  } else {                      // Shared allocation: load from globals
821209131Sraj    CollectedHeap* ch = Universe::heap();
822209131Sraj    address top_adr = (address)ch->top_addr();
823209131Sraj    address end_adr = (address)ch->end_addr();
824209131Sraj    eden_top_adr = makecon(TypeRawPtr::make(top_adr));
825209131Sraj    eden_end_adr = basic_plus_adr(eden_top_adr, end_adr - top_adr);
826209131Sraj  }
827209131Sraj}
828209131Sraj
829209131Sraj
830209131SrajNode* PhaseMacroExpand::make_load(Node* ctl, Node* mem, Node* base, int offset, const Type* value_type, BasicType bt) {
831209131Sraj  Node* adr = basic_plus_adr(base, offset);
832209131Sraj  const TypePtr* adr_type = TypeRawPtr::BOTTOM;
833209131Sraj  Node* value = LoadNode::make(_igvn, ctl, mem, adr, adr_type, value_type, bt);
834209131Sraj  transform_later(value);
835209131Sraj  return value;
836209131Sraj}
837209131Sraj
838209131Sraj
839209131SrajNode* PhaseMacroExpand::make_store(Node* ctl, Node* mem, Node* base, int offset, Node* value, BasicType bt) {
840209131Sraj  Node* adr = basic_plus_adr(base, offset);
841209131Sraj  mem = StoreNode::make(_igvn, ctl, mem, adr, NULL, value, bt);
842209131Sraj  transform_later(mem);
843209131Sraj  return mem;
844209131Sraj}
845209131Sraj
846209131Sraj//=============================================================================
847209131Sraj//
848209131Sraj//                              A L L O C A T I O N
849209131Sraj//
850209131Sraj// Allocation attempts to be fast in the case of frequent small objects.
851209131Sraj// It breaks down like this:
852209131Sraj//
853209131Sraj// 1) Size in doublewords is computed.  This is a constant for objects and
854209131Sraj// variable for most arrays.  Doubleword units are used to avoid size
855209131Sraj// overflow of huge doubleword arrays.  We need doublewords in the end for
856209131Sraj// rounding.
857209131Sraj//
858209131Sraj// 2) Size is checked for being 'too large'.  Too-large allocations will go
859209131Sraj// the slow path into the VM.  The slow path can throw any required
860209131Sraj// exceptions, and does all the special checks for very large arrays.  The
861240489Sgber// size test can constant-fold away for objects.  For objects with
862240489Sgber// finalizers it constant-folds the otherway: you always go slow with
863240489Sgber// finalizers.
864209131Sraj//
865209131Sraj// 3) If NOT using TLABs, this is the contended loop-back point.
866209131Sraj// Load-Locked the heap top.  If using TLABs normal-load the heap top.
867209131Sraj//
868209131Sraj// 4) Check that heap top + size*8 < max.  If we fail go the slow ` route.
869209131Sraj// NOTE: "top+size*8" cannot wrap the 4Gig line!  Here's why: for largish
870209131Sraj// "size*8" we always enter the VM, where "largish" is a constant picked small
871209131Sraj// enough that there's always space between the eden max and 4Gig (old space is
872209131Sraj// there so it's quite large) and large enough that the cost of entering the VM
873209131Sraj// is dwarfed by the cost to initialize the space.
874209131Sraj//
875240489Sgber// 5) If NOT using TLABs, Store-Conditional the adjusted heap top back
876240489Sgber// down.  If contended, repeat at step 3.  If using TLABs normal-store
877240489Sgber// adjusted heap top back down; there is no contention.
878209131Sraj//
879209131Sraj// 6) If !ZeroTLAB then Bulk-clear the object/array.  Fill in klass & mark
880209131Sraj// fields.
881209131Sraj//
882209131Sraj// 7) Merge with the slow-path; cast the raw memory pointer to the correct
883209131Sraj// oop flavor.
884209131Sraj//
885209131Sraj//=============================================================================
886209131Sraj// FastAllocateSizeLimit value is in DOUBLEWORDS.
887209131Sraj// Allocations bigger than this always go the slow route.
888209131Sraj// This value must be small enough that allocation attempts that need to
889209131Sraj// trigger exceptions go the slow route.  Also, it must be small enough so
890209131Sraj// that heap_top + size_in_bytes does not wrap around the 4Gig limit.
891209131Sraj//=============================================================================j//
892209131Sraj// %%% Here is an old comment from parseHelper.cpp; is it outdated?
893209131Sraj// The allocator will coalesce int->oop copies away.  See comment in
894209131Sraj// coalesce.cpp about how this works.  It depends critically on the exact
895209131Sraj// code shape produced here, so if you are changing this code shape
896209131Sraj// make sure the GC info for the heap-top is correct in and around the
897209131Sraj// slow-path call.
898209131Sraj//
899209131Sraj
900209131Srajvoid PhaseMacroExpand::expand_allocate_common(
901209131Sraj            AllocateNode* alloc, // allocation node to be expanded
902209131Sraj            Node* length,  // array length for an array allocation
903209131Sraj            const TypeFunc* slow_call_type, // Type of slow call
904209131Sraj            address slow_call_address  // Address of slow call
905209131Sraj    )
906209131Sraj{
907209131Sraj
908209131Sraj  Node* ctrl = alloc->in(TypeFunc::Control);
909209131Sraj  Node* mem  = alloc->in(TypeFunc::Memory);
910209131Sraj  Node* i_o  = alloc->in(TypeFunc::I_O);
911209131Sraj  Node* size_in_bytes     = alloc->in(AllocateNode::AllocSize);
912209131Sraj  Node* klass_node        = alloc->in(AllocateNode::KlassNode);
913209131Sraj  Node* initial_slow_test = alloc->in(AllocateNode::InitialTest);
914209131Sraj
915209131Sraj  // With escape analysis, the entire memory state was needed to be able to
916240489Sgber  // eliminate the allocation.  Since the allocations cannot be eliminated,
917240489Sgber  // optimize it to the raw slice.
918209131Sraj  if (mem->is_MergeMem()) {
919209131Sraj    mem = mem->as_MergeMem()->memory_at(Compile::AliasIdxRaw);
920209131Sraj  }
921209131Sraj
922209131Sraj  Node* eden_top_adr;
923240489Sgber  Node* eden_end_adr;
924240489Sgber  set_eden_pointers(eden_top_adr, eden_end_adr);
925240489Sgber
926209131Sraj  uint raw_idx = C->get_alias_index(TypeRawPtr::BOTTOM);
927209131Sraj  assert(ctrl != NULL, "must have control");
928209131Sraj
929209131Sraj  // Load Eden::end.  Loop invariant and hoisted.
930209131Sraj  //
931209131Sraj  // Note: We set the control input on "eden_end" and "old_eden_top" when using
932209131Sraj  //       a TLAB to work around a bug where these values were being moved across
933209131Sraj  //       a safepoint.  These are not oops, so they cannot be include in the oop
934209131Sraj  //       map, but the can be changed by a GC.   The proper way to fix this would
935209131Sraj  //       be to set the raw memory state when generating a  SafepointNode.  However
936209131Sraj  //       this will require extensive changes to the loop optimization in order to
937209131Sraj  //       prevent a degradation of the optimization.
938209131Sraj  //       See comment in memnode.hpp, around line 227 in class LoadPNode.
939209131Sraj  Node* eden_end = make_load(ctrl, mem, eden_end_adr, 0, TypeRawPtr::BOTTOM, T_ADDRESS);
940209131Sraj
941209131Sraj  // We need a Region and corresponding Phi's to merge the slow-path and fast-path results.
942209131Sraj  // they will not be used if "always_slow" is set
943209131Sraj  enum { slow_result_path = 1, fast_result_path = 2 };
944209131Sraj  Node *result_region;
945209131Sraj  Node *result_phi_rawmem;
946209131Sraj  Node *result_phi_rawoop;
947209131Sraj  Node *result_phi_i_o;
948209131Sraj
949209131Sraj  // The initial slow comparison is a size check, the comparison
950209131Sraj  // we want to do is a BoolTest::gt
951209131Sraj  bool always_slow = false;
952240493Sgber  int tv = _igvn.find_int_con(initial_slow_test, -1);
953240493Sgber  if (tv >= 0) {
954240493Sgber    always_slow = (tv == 1);
955240493Sgber    initial_slow_test = NULL;
956240493Sgber  } else {
957240493Sgber    initial_slow_test = BoolNode::make_predicate(initial_slow_test, &_igvn);
958240493Sgber  }
959240493Sgber
960240493Sgber  if (DTraceAllocProbes) {
961240493Sgber    // Force slow-path allocation
962240493Sgber    always_slow = true;
963240493Sgber    initial_slow_test = NULL;
964240493Sgber  }
965240493Sgber
966240493Sgber  enum { too_big_or_final_path = 1, need_gc_path = 2 };
967240493Sgber  Node *slow_region = NULL;
968240493Sgber  Node *toobig_false = ctrl;
969240493Sgber
970240493Sgber  assert (initial_slow_test == NULL || !always_slow, "arguments must be consistent");
971240493Sgber  // generate the initial test if necessary
972240493Sgber  if (initial_slow_test != NULL ) {
973240493Sgber    slow_region = new (C, 3) RegionNode(3);
974240493Sgber
975240493Sgber    // Now make the initial failure test.  Usually a too-big test but
976240493Sgber    // might be a TRUE for finalizers or a fancy class check for
977240493Sgber    // newInstance0.
978240493Sgber    IfNode *toobig_iff = new (C, 2) IfNode(ctrl, initial_slow_test, PROB_MIN, COUNT_UNKNOWN);
979240493Sgber    transform_later(toobig_iff);
980240493Sgber    // Plug the failing-too-big test into the slow-path region
981240493Sgber    Node *toobig_true = new (C, 1) IfTrueNode( toobig_iff );
982240493Sgber    transform_later(toobig_true);
983240493Sgber    slow_region    ->init_req( too_big_or_final_path, toobig_true );
984240493Sgber    toobig_false = new (C, 1) IfFalseNode( toobig_iff );
985240493Sgber    transform_later(toobig_false);
986240493Sgber  } else {         // No initial test, just fall into next case
987240493Sgber    toobig_false = ctrl;
988240493Sgber    debug_only(slow_region = NodeSentinel);
989240493Sgber  }
990240493Sgber
991240493Sgber  Node *slow_mem = mem;  // save the current memory state for slow path
992240493Sgber  // generate the fast allocation code unless we know that the initial test will always go slow
993240493Sgber  if (!always_slow) {
994240493Sgber    // allocate the Region and Phi nodes for the result
995240493Sgber    result_region = new (C, 3) RegionNode(3);
996240493Sgber    result_phi_rawmem = new (C, 3) PhiNode( result_region, Type::MEMORY, TypeRawPtr::BOTTOM );
997240493Sgber    result_phi_rawoop = new (C, 3) PhiNode( result_region, TypeRawPtr::BOTTOM );
998240493Sgber    result_phi_i_o    = new (C, 3) PhiNode( result_region, Type::ABIO ); // I/O is used for Prefetch
999240493Sgber
1000240493Sgber    // We need a Region for the loop-back contended case.
1001240493Sgber    enum { fall_in_path = 1, contended_loopback_path = 2 };
1002240493Sgber    Node *contended_region;
1003240493Sgber    Node *contended_phi_rawmem;
1004240493Sgber    if( UseTLAB ) {
1005240493Sgber      contended_region = toobig_false;
1006240493Sgber      contended_phi_rawmem = mem;
1007240493Sgber    } else {
1008240493Sgber      contended_region = new (C, 3) RegionNode(3);
1009240493Sgber      contended_phi_rawmem = new (C, 3) PhiNode( contended_region, Type::MEMORY, TypeRawPtr::BOTTOM);
1010240493Sgber      // Now handle the passing-too-big test.  We fall into the contended
1011240493Sgber      // loop-back merge point.
1012240493Sgber      contended_region    ->init_req( fall_in_path, toobig_false );
1013240493Sgber      contended_phi_rawmem->init_req( fall_in_path, mem );
1014240493Sgber      transform_later(contended_region);
1015240493Sgber      transform_later(contended_phi_rawmem);
1016240493Sgber    }
1017240493Sgber
1018240493Sgber    // Load(-locked) the heap top.
1019240493Sgber    // See note above concerning the control input when using a TLAB
1020240493Sgber    Node *old_eden_top = UseTLAB
1021240493Sgber      ? new (C, 3) LoadPNode     ( ctrl, contended_phi_rawmem, eden_top_adr, TypeRawPtr::BOTTOM, TypeRawPtr::BOTTOM )
1022240493Sgber      : new (C, 3) LoadPLockedNode( contended_region, contended_phi_rawmem, eden_top_adr );
1023240493Sgber
1024240493Sgber    transform_later(old_eden_top);
1025240493Sgber    // Add to heap top to get a new heap top
1026240493Sgber    Node *new_eden_top = new (C, 4) AddPNode( top(), old_eden_top, size_in_bytes );
1027240493Sgber    transform_later(new_eden_top);
1028240493Sgber    // Check for needing a GC; compare against heap end
1029    Node *needgc_cmp = new (C, 3) CmpPNode( new_eden_top, eden_end );
1030    transform_later(needgc_cmp);
1031    Node *needgc_bol = new (C, 2) BoolNode( needgc_cmp, BoolTest::ge );
1032    transform_later(needgc_bol);
1033    IfNode *needgc_iff = new (C, 2) IfNode(contended_region, needgc_bol, PROB_UNLIKELY_MAG(4), COUNT_UNKNOWN );
1034    transform_later(needgc_iff);
1035
1036    // Plug the failing-heap-space-need-gc test into the slow-path region
1037    Node *needgc_true = new (C, 1) IfTrueNode( needgc_iff );
1038    transform_later(needgc_true);
1039    if( initial_slow_test ) {
1040      slow_region    ->init_req( need_gc_path, needgc_true );
1041      // This completes all paths into the slow merge point
1042      transform_later(slow_region);
1043    } else {                      // No initial slow path needed!
1044      // Just fall from the need-GC path straight into the VM call.
1045      slow_region    = needgc_true;
1046    }
1047    // No need for a GC.  Setup for the Store-Conditional
1048    Node *needgc_false = new (C, 1) IfFalseNode( needgc_iff );
1049    transform_later(needgc_false);
1050
1051    // Grab regular I/O before optional prefetch may change it.
1052    // Slow-path does no I/O so just set it to the original I/O.
1053    result_phi_i_o->init_req( slow_result_path, i_o );
1054
1055    i_o = prefetch_allocation(i_o, needgc_false, contended_phi_rawmem,
1056                              old_eden_top, new_eden_top, length);
1057
1058    // Store (-conditional) the modified eden top back down.
1059    // StorePConditional produces flags for a test PLUS a modified raw
1060    // memory state.
1061    Node *store_eden_top;
1062    Node *fast_oop_ctrl;
1063    if( UseTLAB ) {
1064      store_eden_top = new (C, 4) StorePNode( needgc_false, contended_phi_rawmem, eden_top_adr, TypeRawPtr::BOTTOM, new_eden_top );
1065      transform_later(store_eden_top);
1066      fast_oop_ctrl = needgc_false; // No contention, so this is the fast path
1067    } else {
1068      store_eden_top = new (C, 5) StorePConditionalNode( needgc_false, contended_phi_rawmem, eden_top_adr, new_eden_top, old_eden_top );
1069      transform_later(store_eden_top);
1070      Node *contention_check = new (C, 2) BoolNode( store_eden_top, BoolTest::ne );
1071      transform_later(contention_check);
1072      store_eden_top = new (C, 1) SCMemProjNode(store_eden_top);
1073      transform_later(store_eden_top);
1074
1075      // If not using TLABs, check to see if there was contention.
1076      IfNode *contention_iff = new (C, 2) IfNode ( needgc_false, contention_check, PROB_MIN, COUNT_UNKNOWN );
1077      transform_later(contention_iff);
1078      Node *contention_true = new (C, 1) IfTrueNode( contention_iff );
1079      transform_later(contention_true);
1080      // If contention, loopback and try again.
1081      contended_region->init_req( contended_loopback_path, contention_true );
1082      contended_phi_rawmem->init_req( contended_loopback_path, store_eden_top );
1083
1084      // Fast-path succeeded with no contention!
1085      Node *contention_false = new (C, 1) IfFalseNode( contention_iff );
1086      transform_later(contention_false);
1087      fast_oop_ctrl = contention_false;
1088    }
1089
1090    // Rename successful fast-path variables to make meaning more obvious
1091    Node* fast_oop        = old_eden_top;
1092    Node* fast_oop_rawmem = store_eden_top;
1093    fast_oop_rawmem = initialize_object(alloc,
1094                                        fast_oop_ctrl, fast_oop_rawmem, fast_oop,
1095                                        klass_node, length, size_in_bytes);
1096
1097    if (ExtendedDTraceProbes) {
1098      // Slow-path call
1099      int size = TypeFunc::Parms + 2;
1100      CallLeafNode *call = new (C, size) CallLeafNode(OptoRuntime::dtrace_object_alloc_Type(),
1101                                                      CAST_FROM_FN_PTR(address, SharedRuntime::dtrace_object_alloc_base),
1102                                                      "dtrace_object_alloc",
1103                                                      TypeRawPtr::BOTTOM);
1104
1105      // Get base of thread-local storage area
1106      Node* thread = new (C, 1) ThreadLocalNode();
1107      transform_later(thread);
1108
1109      call->init_req(TypeFunc::Parms+0, thread);
1110      call->init_req(TypeFunc::Parms+1, fast_oop);
1111      call->init_req( TypeFunc::Control, fast_oop_ctrl );
1112      call->init_req( TypeFunc::I_O    , top() )        ;   // does no i/o
1113      call->init_req( TypeFunc::Memory , fast_oop_rawmem );
1114      call->init_req( TypeFunc::ReturnAdr, alloc->in(TypeFunc::ReturnAdr) );
1115      call->init_req( TypeFunc::FramePtr, alloc->in(TypeFunc::FramePtr) );
1116      transform_later(call);
1117      fast_oop_ctrl = new (C, 1) ProjNode(call,TypeFunc::Control);
1118      transform_later(fast_oop_ctrl);
1119      fast_oop_rawmem = new (C, 1) ProjNode(call,TypeFunc::Memory);
1120      transform_later(fast_oop_rawmem);
1121    }
1122
1123    // Plug in the successful fast-path into the result merge point
1124    result_region    ->init_req( fast_result_path, fast_oop_ctrl );
1125    result_phi_rawoop->init_req( fast_result_path, fast_oop );
1126    result_phi_i_o   ->init_req( fast_result_path, i_o );
1127    result_phi_rawmem->init_req( fast_result_path, fast_oop_rawmem );
1128  } else {
1129    slow_region = ctrl;
1130  }
1131
1132  // Generate slow-path call
1133  CallNode *call = new (C, slow_call_type->domain()->cnt())
1134    CallStaticJavaNode(slow_call_type, slow_call_address,
1135                       OptoRuntime::stub_name(slow_call_address),
1136                       alloc->jvms()->bci(),
1137                       TypePtr::BOTTOM);
1138  call->init_req( TypeFunc::Control, slow_region );
1139  call->init_req( TypeFunc::I_O    , top() )     ;   // does no i/o
1140  call->init_req( TypeFunc::Memory , slow_mem ); // may gc ptrs
1141  call->init_req( TypeFunc::ReturnAdr, alloc->in(TypeFunc::ReturnAdr) );
1142  call->init_req( TypeFunc::FramePtr, alloc->in(TypeFunc::FramePtr) );
1143
1144  call->init_req(TypeFunc::Parms+0, klass_node);
1145  if (length != NULL) {
1146    call->init_req(TypeFunc::Parms+1, length);
1147  }
1148
1149  // Copy debug information and adjust JVMState information, then replace
1150  // allocate node with the call
1151  copy_call_debug_info((CallNode *) alloc,  call);
1152  if (!always_slow) {
1153    call->set_cnt(PROB_UNLIKELY_MAG(4));  // Same effect as RC_UNCOMMON.
1154  }
1155  _igvn.hash_delete(alloc);
1156  _igvn.subsume_node(alloc, call);
1157  transform_later(call);
1158
1159  // Identify the output projections from the allocate node and
1160  // adjust any references to them.
1161  // The control and io projections look like:
1162  //
1163  //        v---Proj(ctrl) <-----+   v---CatchProj(ctrl)
1164  //  Allocate                   Catch
1165  //        ^---Proj(io) <-------+   ^---CatchProj(io)
1166  //
1167  //  We are interested in the CatchProj nodes.
1168  //
1169  extract_call_projections(call);
1170
1171  // An allocate node has separate memory projections for the uses on the control and i_o paths
1172  // Replace uses of the control memory projection with result_phi_rawmem (unless we are only generating a slow call)
1173  if (!always_slow && _memproj_fallthrough != NULL) {
1174    for (DUIterator_Fast imax, i = _memproj_fallthrough->fast_outs(imax); i < imax; i++) {
1175      Node *use = _memproj_fallthrough->fast_out(i);
1176      _igvn.hash_delete(use);
1177      imax -= replace_input(use, _memproj_fallthrough, result_phi_rawmem);
1178      _igvn._worklist.push(use);
1179      // back up iterator
1180      --i;
1181    }
1182  }
1183  // Now change uses of _memproj_catchall to use _memproj_fallthrough and delete _memproj_catchall so
1184  // we end up with a call that has only 1 memory projection
1185  if (_memproj_catchall != NULL ) {
1186    if (_memproj_fallthrough == NULL) {
1187      _memproj_fallthrough = new (C, 1) ProjNode(call, TypeFunc::Memory);
1188      transform_later(_memproj_fallthrough);
1189    }
1190    for (DUIterator_Fast imax, i = _memproj_catchall->fast_outs(imax); i < imax; i++) {
1191      Node *use = _memproj_catchall->fast_out(i);
1192      _igvn.hash_delete(use);
1193      imax -= replace_input(use, _memproj_catchall, _memproj_fallthrough);
1194      _igvn._worklist.push(use);
1195      // back up iterator
1196      --i;
1197    }
1198  }
1199
1200  mem = result_phi_rawmem;
1201
1202  // An allocate node has separate i_o projections for the uses on the control and i_o paths
1203  // Replace uses of the control i_o projection with result_phi_i_o (unless we are only generating a slow call)
1204  if (_ioproj_fallthrough == NULL) {
1205    _ioproj_fallthrough = new (C, 1) ProjNode(call, TypeFunc::I_O);
1206    transform_later(_ioproj_fallthrough);
1207  } else if (!always_slow) {
1208    for (DUIterator_Fast imax, i = _ioproj_fallthrough->fast_outs(imax); i < imax; i++) {
1209      Node *use = _ioproj_fallthrough->fast_out(i);
1210
1211      _igvn.hash_delete(use);
1212      imax -= replace_input(use, _ioproj_fallthrough, result_phi_i_o);
1213      _igvn._worklist.push(use);
1214      // back up iterator
1215      --i;
1216    }
1217  }
1218  // Now change uses of _ioproj_catchall to use _ioproj_fallthrough and delete _ioproj_catchall so
1219  // we end up with a call that has only 1 control projection
1220  if (_ioproj_catchall != NULL ) {
1221    for (DUIterator_Fast imax, i = _ioproj_catchall->fast_outs(imax); i < imax; i++) {
1222      Node *use = _ioproj_catchall->fast_out(i);
1223      _igvn.hash_delete(use);
1224      imax -= replace_input(use, _ioproj_catchall, _ioproj_fallthrough);
1225      _igvn._worklist.push(use);
1226      // back up iterator
1227      --i;
1228    }
1229  }
1230
1231  // if we generated only a slow call, we are done
1232  if (always_slow)
1233    return;
1234
1235
1236  if (_fallthroughcatchproj != NULL) {
1237    ctrl = _fallthroughcatchproj->clone();
1238    transform_later(ctrl);
1239    _igvn.hash_delete(_fallthroughcatchproj);
1240    _igvn.subsume_node(_fallthroughcatchproj, result_region);
1241  } else {
1242    ctrl = top();
1243  }
1244  Node *slow_result;
1245  if (_resproj == NULL) {
1246    // no uses of the allocation result
1247    slow_result = top();
1248  } else {
1249    slow_result = _resproj->clone();
1250    transform_later(slow_result);
1251    _igvn.hash_delete(_resproj);
1252    _igvn.subsume_node(_resproj, result_phi_rawoop);
1253  }
1254
1255  // Plug slow-path into result merge point
1256  result_region    ->init_req( slow_result_path, ctrl );
1257  result_phi_rawoop->init_req( slow_result_path, slow_result);
1258  result_phi_rawmem->init_req( slow_result_path, _memproj_fallthrough );
1259  transform_later(result_region);
1260  transform_later(result_phi_rawoop);
1261  transform_later(result_phi_rawmem);
1262  transform_later(result_phi_i_o);
1263  // This completes all paths into the result merge point
1264}
1265
1266
1267// Helper for PhaseMacroExpand::expand_allocate_common.
1268// Initializes the newly-allocated storage.
1269Node*
1270PhaseMacroExpand::initialize_object(AllocateNode* alloc,
1271                                    Node* control, Node* rawmem, Node* object,
1272                                    Node* klass_node, Node* length,
1273                                    Node* size_in_bytes) {
1274  InitializeNode* init = alloc->initialization();
1275  // Store the klass & mark bits
1276  Node* mark_node = NULL;
1277  // For now only enable fast locking for non-array types
1278  if (UseBiasedLocking && (length == NULL)) {
1279    mark_node = make_load(NULL, rawmem, klass_node, Klass::prototype_header_offset_in_bytes() + sizeof(oopDesc), TypeRawPtr::BOTTOM, T_ADDRESS);
1280  } else {
1281    mark_node = makecon(TypeRawPtr::make((address)markOopDesc::prototype()));
1282  }
1283  rawmem = make_store(control, rawmem, object, oopDesc::mark_offset_in_bytes(), mark_node, T_ADDRESS);
1284
1285  rawmem = make_store(control, rawmem, object, oopDesc::klass_offset_in_bytes(), klass_node, T_OBJECT);
1286  int header_size = alloc->minimum_header_size();  // conservatively small
1287
1288  // Array length
1289  if (length != NULL) {         // Arrays need length field
1290    rawmem = make_store(control, rawmem, object, arrayOopDesc::length_offset_in_bytes(), length, T_INT);
1291    // conservatively small header size:
1292    header_size = arrayOopDesc::base_offset_in_bytes(T_BYTE);
1293    ciKlass* k = _igvn.type(klass_node)->is_klassptr()->klass();
1294    if (k->is_array_klass())    // we know the exact header size in most cases:
1295      header_size = Klass::layout_helper_header_size(k->layout_helper());
1296  }
1297
1298  // Clear the object body, if necessary.
1299  if (init == NULL) {
1300    // The init has somehow disappeared; be cautious and clear everything.
1301    //
1302    // This can happen if a node is allocated but an uncommon trap occurs
1303    // immediately.  In this case, the Initialize gets associated with the
1304    // trap, and may be placed in a different (outer) loop, if the Allocate
1305    // is in a loop.  If (this is rare) the inner loop gets unrolled, then
1306    // there can be two Allocates to one Initialize.  The answer in all these
1307    // edge cases is safety first.  It is always safe to clear immediately
1308    // within an Allocate, and then (maybe or maybe not) clear some more later.
1309    if (!ZeroTLAB)
1310      rawmem = ClearArrayNode::clear_memory(control, rawmem, object,
1311                                            header_size, size_in_bytes,
1312                                            &_igvn);
1313  } else {
1314    if (!init->is_complete()) {
1315      // Try to win by zeroing only what the init does not store.
1316      // We can also try to do some peephole optimizations,
1317      // such as combining some adjacent subword stores.
1318      rawmem = init->complete_stores(control, rawmem, object,
1319                                     header_size, size_in_bytes, &_igvn);
1320    }
1321    // We have no more use for this link, since the AllocateNode goes away:
1322    init->set_req(InitializeNode::RawAddress, top());
1323    // (If we keep the link, it just confuses the register allocator,
1324    // who thinks he sees a real use of the address by the membar.)
1325  }
1326
1327  return rawmem;
1328}
1329
1330// Generate prefetch instructions for next allocations.
1331Node* PhaseMacroExpand::prefetch_allocation(Node* i_o, Node*& needgc_false,
1332                                        Node*& contended_phi_rawmem,
1333                                        Node* old_eden_top, Node* new_eden_top,
1334                                        Node* length) {
1335   if( UseTLAB && AllocatePrefetchStyle == 2 ) {
1336      // Generate prefetch allocation with watermark check.
1337      // As an allocation hits the watermark, we will prefetch starting
1338      // at a "distance" away from watermark.
1339      enum { fall_in_path = 1, pf_path = 2 };
1340
1341      Node *pf_region = new (C, 3) RegionNode(3);
1342      Node *pf_phi_rawmem = new (C, 3) PhiNode( pf_region, Type::MEMORY,
1343                                                TypeRawPtr::BOTTOM );
1344      // I/O is used for Prefetch
1345      Node *pf_phi_abio = new (C, 3) PhiNode( pf_region, Type::ABIO );
1346
1347      Node *thread = new (C, 1) ThreadLocalNode();
1348      transform_later(thread);
1349
1350      Node *eden_pf_adr = new (C, 4) AddPNode( top()/*not oop*/, thread,
1351                   _igvn.MakeConX(in_bytes(JavaThread::tlab_pf_top_offset())) );
1352      transform_later(eden_pf_adr);
1353
1354      Node *old_pf_wm = new (C, 3) LoadPNode( needgc_false,
1355                                   contended_phi_rawmem, eden_pf_adr,
1356                                   TypeRawPtr::BOTTOM, TypeRawPtr::BOTTOM );
1357      transform_later(old_pf_wm);
1358
1359      // check against new_eden_top
1360      Node *need_pf_cmp = new (C, 3) CmpPNode( new_eden_top, old_pf_wm );
1361      transform_later(need_pf_cmp);
1362      Node *need_pf_bol = new (C, 2) BoolNode( need_pf_cmp, BoolTest::ge );
1363      transform_later(need_pf_bol);
1364      IfNode *need_pf_iff = new (C, 2) IfNode( needgc_false, need_pf_bol,
1365                                       PROB_UNLIKELY_MAG(4), COUNT_UNKNOWN );
1366      transform_later(need_pf_iff);
1367
1368      // true node, add prefetchdistance
1369      Node *need_pf_true = new (C, 1) IfTrueNode( need_pf_iff );
1370      transform_later(need_pf_true);
1371
1372      Node *need_pf_false = new (C, 1) IfFalseNode( need_pf_iff );
1373      transform_later(need_pf_false);
1374
1375      Node *new_pf_wmt = new (C, 4) AddPNode( top(), old_pf_wm,
1376                                    _igvn.MakeConX(AllocatePrefetchDistance) );
1377      transform_later(new_pf_wmt );
1378      new_pf_wmt->set_req(0, need_pf_true);
1379
1380      Node *store_new_wmt = new (C, 4) StorePNode( need_pf_true,
1381                                       contended_phi_rawmem, eden_pf_adr,
1382                                       TypeRawPtr::BOTTOM, new_pf_wmt );
1383      transform_later(store_new_wmt);
1384
1385      // adding prefetches
1386      pf_phi_abio->init_req( fall_in_path, i_o );
1387
1388      Node *prefetch_adr;
1389      Node *prefetch;
1390      uint lines = AllocatePrefetchDistance / AllocatePrefetchStepSize;
1391      uint step_size = AllocatePrefetchStepSize;
1392      uint distance = 0;
1393
1394      for ( uint i = 0; i < lines; i++ ) {
1395        prefetch_adr = new (C, 4) AddPNode( old_pf_wm, new_pf_wmt,
1396                                            _igvn.MakeConX(distance) );
1397        transform_later(prefetch_adr);
1398        prefetch = new (C, 3) PrefetchWriteNode( i_o, prefetch_adr );
1399        transform_later(prefetch);
1400        distance += step_size;
1401        i_o = prefetch;
1402      }
1403      pf_phi_abio->set_req( pf_path, i_o );
1404
1405      pf_region->init_req( fall_in_path, need_pf_false );
1406      pf_region->init_req( pf_path, need_pf_true );
1407
1408      pf_phi_rawmem->init_req( fall_in_path, contended_phi_rawmem );
1409      pf_phi_rawmem->init_req( pf_path, store_new_wmt );
1410
1411      transform_later(pf_region);
1412      transform_later(pf_phi_rawmem);
1413      transform_later(pf_phi_abio);
1414
1415      needgc_false = pf_region;
1416      contended_phi_rawmem = pf_phi_rawmem;
1417      i_o = pf_phi_abio;
1418   } else if( AllocatePrefetchStyle > 0 ) {
1419      // Insert a prefetch for each allocation only on the fast-path
1420      Node *prefetch_adr;
1421      Node *prefetch;
1422      // Generate several prefetch instructions only for arrays.
1423      uint lines = (length != NULL) ? AllocatePrefetchLines : 1;
1424      uint step_size = AllocatePrefetchStepSize;
1425      uint distance = AllocatePrefetchDistance;
1426      for ( uint i = 0; i < lines; i++ ) {
1427        prefetch_adr = new (C, 4) AddPNode( old_eden_top, new_eden_top,
1428                                            _igvn.MakeConX(distance) );
1429        transform_later(prefetch_adr);
1430        prefetch = new (C, 3) PrefetchWriteNode( i_o, prefetch_adr );
1431        // Do not let it float too high, since if eden_top == eden_end,
1432        // both might be null.
1433        if( i == 0 ) { // Set control for first prefetch, next follows it
1434          prefetch->init_req(0, needgc_false);
1435        }
1436        transform_later(prefetch);
1437        distance += step_size;
1438        i_o = prefetch;
1439      }
1440   }
1441   return i_o;
1442}
1443
1444
1445void PhaseMacroExpand::expand_allocate(AllocateNode *alloc) {
1446  expand_allocate_common(alloc, NULL,
1447                         OptoRuntime::new_instance_Type(),
1448                         OptoRuntime::new_instance_Java());
1449}
1450
1451void PhaseMacroExpand::expand_allocate_array(AllocateArrayNode *alloc) {
1452  Node* length = alloc->in(AllocateNode::ALength);
1453  expand_allocate_common(alloc, length,
1454                         OptoRuntime::new_array_Type(),
1455                         OptoRuntime::new_array_Java());
1456}
1457
1458
1459// we have determined that this lock/unlock can be eliminated, we simply
1460// eliminate the node without expanding it.
1461//
1462// Note:  The membar's associated with the lock/unlock are currently not
1463//        eliminated.  This should be investigated as a future enhancement.
1464//
1465bool PhaseMacroExpand::eliminate_locking_node(AbstractLockNode *alock) {
1466
1467  if (!alock->is_eliminated()) {
1468    return false;
1469  }
1470  // Mark the box lock as eliminated if all correspondent locks are eliminated
1471  // to construct correct debug info.
1472  BoxLockNode* box = alock->box_node()->as_BoxLock();
1473  if (!box->is_eliminated()) {
1474    bool eliminate = true;
1475    for (DUIterator_Fast imax, i = box->fast_outs(imax); i < imax; i++) {
1476      Node *lck = box->fast_out(i);
1477      if (lck->is_Lock() && !lck->as_AbstractLock()->is_eliminated()) {
1478        eliminate = false;
1479        break;
1480      }
1481    }
1482    if (eliminate)
1483      box->set_eliminated();
1484  }
1485
1486  #ifndef PRODUCT
1487  if (PrintEliminateLocks) {
1488    if (alock->is_Lock()) {
1489      tty->print_cr("++++ Eliminating: %d Lock", alock->_idx);
1490    } else {
1491      tty->print_cr("++++ Eliminating: %d Unlock", alock->_idx);
1492    }
1493  }
1494  #endif
1495
1496  Node* mem  = alock->in(TypeFunc::Memory);
1497  Node* ctrl = alock->in(TypeFunc::Control);
1498
1499  extract_call_projections(alock);
1500  // There are 2 projections from the lock.  The lock node will
1501  // be deleted when its last use is subsumed below.
1502  assert(alock->outcnt() == 2 &&
1503         _fallthroughproj != NULL &&
1504         _memproj_fallthrough != NULL,
1505         "Unexpected projections from Lock/Unlock");
1506
1507  Node* fallthroughproj = _fallthroughproj;
1508  Node* memproj_fallthrough = _memproj_fallthrough;
1509
1510  // The memory projection from a lock/unlock is RawMem
1511  // The input to a Lock is merged memory, so extract its RawMem input
1512  // (unless the MergeMem has been optimized away.)
1513  if (alock->is_Lock()) {
1514    // Seach for MemBarAcquire node and delete it also.
1515    MemBarNode* membar = fallthroughproj->unique_ctrl_out()->as_MemBar();
1516    assert(membar != NULL && membar->Opcode() == Op_MemBarAcquire, "");
1517    Node* ctrlproj = membar->proj_out(TypeFunc::Control);
1518    Node* memproj = membar->proj_out(TypeFunc::Memory);
1519    _igvn.hash_delete(ctrlproj);
1520    _igvn.subsume_node(ctrlproj, fallthroughproj);
1521    _igvn.hash_delete(memproj);
1522    _igvn.subsume_node(memproj, memproj_fallthrough);
1523  }
1524
1525  // Seach for MemBarRelease node and delete it also.
1526  if (alock->is_Unlock() && ctrl != NULL && ctrl->is_Proj() &&
1527      ctrl->in(0)->is_MemBar()) {
1528    MemBarNode* membar = ctrl->in(0)->as_MemBar();
1529    assert(membar->Opcode() == Op_MemBarRelease &&
1530           mem->is_Proj() && membar == mem->in(0), "");
1531    _igvn.hash_delete(fallthroughproj);
1532    _igvn.subsume_node(fallthroughproj, ctrl);
1533    _igvn.hash_delete(memproj_fallthrough);
1534    _igvn.subsume_node(memproj_fallthrough, mem);
1535    fallthroughproj = ctrl;
1536    memproj_fallthrough = mem;
1537    ctrl = membar->in(TypeFunc::Control);
1538    mem  = membar->in(TypeFunc::Memory);
1539  }
1540
1541  _igvn.hash_delete(fallthroughproj);
1542  _igvn.subsume_node(fallthroughproj, ctrl);
1543  _igvn.hash_delete(memproj_fallthrough);
1544  _igvn.subsume_node(memproj_fallthrough, mem);
1545  return true;
1546}
1547
1548
1549//------------------------------expand_lock_node----------------------
1550void PhaseMacroExpand::expand_lock_node(LockNode *lock) {
1551
1552  Node* ctrl = lock->in(TypeFunc::Control);
1553  Node* mem = lock->in(TypeFunc::Memory);
1554  Node* obj = lock->obj_node();
1555  Node* box = lock->box_node();
1556  Node* flock = lock->fastlock_node();
1557
1558  // Make the merge point
1559  Node *region = new (C, 3) RegionNode(3);
1560
1561  Node *bol = transform_later(new (C, 2) BoolNode(flock,BoolTest::ne));
1562  Node *iff = new (C, 2) IfNode( ctrl, bol, PROB_MIN, COUNT_UNKNOWN );
1563  // Optimize test; set region slot 2
1564  Node *slow_path = opt_iff(region,iff);
1565
1566  // Make slow path call
1567  CallNode *call = make_slow_call( (CallNode *) lock, OptoRuntime::complete_monitor_enter_Type(), OptoRuntime::complete_monitor_locking_Java(), NULL, slow_path, obj, box );
1568
1569  extract_call_projections(call);
1570
1571  // Slow path can only throw asynchronous exceptions, which are always
1572  // de-opted.  So the compiler thinks the slow-call can never throw an
1573  // exception.  If it DOES throw an exception we would need the debug
1574  // info removed first (since if it throws there is no monitor).
1575  assert ( _ioproj_fallthrough == NULL && _ioproj_catchall == NULL &&
1576           _memproj_catchall == NULL && _catchallcatchproj == NULL, "Unexpected projection from Lock");
1577
1578  // Capture slow path
1579  // disconnect fall-through projection from call and create a new one
1580  // hook up users of fall-through projection to region
1581  Node *slow_ctrl = _fallthroughproj->clone();
1582  transform_later(slow_ctrl);
1583  _igvn.hash_delete(_fallthroughproj);
1584  _fallthroughproj->disconnect_inputs(NULL);
1585  region->init_req(1, slow_ctrl);
1586  // region inputs are now complete
1587  transform_later(region);
1588  _igvn.subsume_node(_fallthroughproj, region);
1589
1590  // create a Phi for the memory state
1591  Node *mem_phi = new (C, 3) PhiNode( region, Type::MEMORY, TypeRawPtr::BOTTOM);
1592  Node *memproj = transform_later( new (C, 1) ProjNode(call, TypeFunc::Memory) );
1593  mem_phi->init_req(1, memproj );
1594  mem_phi->init_req(2, mem);
1595  transform_later(mem_phi);
1596    _igvn.hash_delete(_memproj_fallthrough);
1597  _igvn.subsume_node(_memproj_fallthrough, mem_phi);
1598
1599
1600}
1601
1602//------------------------------expand_unlock_node----------------------
1603void PhaseMacroExpand::expand_unlock_node(UnlockNode *unlock) {
1604
1605  Node* ctrl = unlock->in(TypeFunc::Control);
1606  Node* mem = unlock->in(TypeFunc::Memory);
1607  Node* obj = unlock->obj_node();
1608  Node* box = unlock->box_node();
1609
1610  // No need for a null check on unlock
1611
1612  // Make the merge point
1613  RegionNode *region = new (C, 3) RegionNode(3);
1614
1615  FastUnlockNode *funlock = new (C, 3) FastUnlockNode( ctrl, obj, box );
1616  funlock = transform_later( funlock )->as_FastUnlock();
1617  Node *bol = transform_later(new (C, 2) BoolNode(funlock,BoolTest::ne));
1618  Node *iff = new (C, 2) IfNode( ctrl, bol, PROB_MIN, COUNT_UNKNOWN );
1619  // Optimize test; set region slot 2
1620  Node *slow_path = opt_iff(region,iff);
1621
1622  CallNode *call = make_slow_call( (CallNode *) unlock, OptoRuntime::complete_monitor_exit_Type(), CAST_FROM_FN_PTR(address, SharedRuntime::complete_monitor_unlocking_C), "complete_monitor_unlocking_C", slow_path, obj, box );
1623
1624  extract_call_projections(call);
1625
1626  assert ( _ioproj_fallthrough == NULL && _ioproj_catchall == NULL &&
1627           _memproj_catchall == NULL && _catchallcatchproj == NULL, "Unexpected projection from Lock");
1628
1629  // No exceptions for unlocking
1630  // Capture slow path
1631  // disconnect fall-through projection from call and create a new one
1632  // hook up users of fall-through projection to region
1633  Node *slow_ctrl = _fallthroughproj->clone();
1634  transform_later(slow_ctrl);
1635  _igvn.hash_delete(_fallthroughproj);
1636  _fallthroughproj->disconnect_inputs(NULL);
1637  region->init_req(1, slow_ctrl);
1638  // region inputs are now complete
1639  transform_later(region);
1640  _igvn.subsume_node(_fallthroughproj, region);
1641
1642  // create a Phi for the memory state
1643  Node *mem_phi = new (C, 3) PhiNode( region, Type::MEMORY, TypeRawPtr::BOTTOM);
1644  Node *memproj = transform_later( new(C, 1) ProjNode(call, TypeFunc::Memory) );
1645  mem_phi->init_req(1, memproj );
1646  mem_phi->init_req(2, mem);
1647  transform_later(mem_phi);
1648    _igvn.hash_delete(_memproj_fallthrough);
1649  _igvn.subsume_node(_memproj_fallthrough, mem_phi);
1650
1651
1652}
1653
1654//------------------------------expand_macro_nodes----------------------
1655//  Returns true if a failure occurred.
1656bool PhaseMacroExpand::expand_macro_nodes() {
1657  if (C->macro_count() == 0)
1658    return false;
1659  // attempt to eliminate allocations
1660  bool progress = true;
1661  while (progress) {
1662    progress = false;
1663    for (int i = C->macro_count(); i > 0; i--) {
1664      Node * n = C->macro_node(i-1);
1665      bool success = false;
1666      debug_only(int old_macro_count = C->macro_count(););
1667      switch (n->class_id()) {
1668      case Node::Class_Allocate:
1669      case Node::Class_AllocateArray:
1670        success = eliminate_allocate_node(n->as_Allocate());
1671        break;
1672      case Node::Class_Lock:
1673      case Node::Class_Unlock:
1674        success = eliminate_locking_node(n->as_AbstractLock());
1675        break;
1676      default:
1677        assert(false, "unknown node type in macro list");
1678      }
1679      assert(success == (C->macro_count() < old_macro_count), "elimination reduces macro count");
1680      progress = progress || success;
1681    }
1682  }
1683  // Make sure expansion will not cause node limit to be exceeded.
1684  // Worst case is a macro node gets expanded into about 50 nodes.
1685  // Allow 50% more for optimization.
1686  if (C->check_node_count(C->macro_count() * 75, "out of nodes before macro expansion" ) )
1687    return true;
1688
1689  // expand "macro" nodes
1690  // nodes are removed from the macro list as they are processed
1691  while (C->macro_count() > 0) {
1692    int macro_count = C->macro_count();
1693    Node * n = C->macro_node(macro_count-1);
1694    assert(n->is_macro(), "only macro nodes expected here");
1695    if (_igvn.type(n) == Type::TOP || n->in(0)->is_top() ) {
1696      // node is unreachable, so don't try to expand it
1697      C->remove_macro_node(n);
1698      continue;
1699    }
1700    switch (n->class_id()) {
1701    case Node::Class_Allocate:
1702      expand_allocate(n->as_Allocate());
1703      break;
1704    case Node::Class_AllocateArray:
1705      expand_allocate_array(n->as_AllocateArray());
1706      break;
1707    case Node::Class_Lock:
1708      expand_lock_node(n->as_Lock());
1709      break;
1710    case Node::Class_Unlock:
1711      expand_unlock_node(n->as_Unlock());
1712      break;
1713    default:
1714      assert(false, "unknown node type in macro list");
1715    }
1716    assert(C->macro_count() < macro_count, "must have deleted a node from macro list");
1717    if (C->failing())  return true;
1718  }
1719
1720  _igvn.set_delay_transform(false);
1721  _igvn.optimize();
1722  return false;
1723}
1724