escape.cpp 77.7 KB
Newer Older
D
duke 已提交
1
/*
X
xdono 已提交
2
 * Copyright 2005-2008 Sun Microsystems, Inc.  All Rights Reserved.
D
duke 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
 * CA 95054 USA or visit www.sun.com if you need additional information or
 * have any questions.
 *
 */

#include "incls/_precompiled.incl"
#include "incls/_escape.cpp.incl"

void PointsToNode::add_edge(uint targIdx, PointsToNode::EdgeType et) {
  uint v = (targIdx << EdgeShift) + ((uint) et);
  if (_edges == NULL) {
     Arena *a = Compile::current()->comp_arena();
    _edges = new(a) GrowableArray<uint>(a, INITIAL_EDGE_COUNT, 0, 0);
  }
  _edges->append_if_missing(v);
}

void PointsToNode::remove_edge(uint targIdx, PointsToNode::EdgeType et) {
  uint v = (targIdx << EdgeShift) + ((uint) et);

  _edges->remove(v);
}

#ifndef PRODUCT
K
kvn 已提交
44
static const char *node_type_names[] = {
D
duke 已提交
45 46 47 48 49 50
  "UnknownType",
  "JavaObject",
  "LocalVar",
  "Field"
};

K
kvn 已提交
51
static const char *esc_names[] = {
D
duke 已提交
52
  "UnknownEscape",
53 54 55
  "NoEscape",
  "ArgEscape",
  "GlobalEscape"
D
duke 已提交
56 57
};

K
kvn 已提交
58
static const char *edge_type_suffix[] = {
D
duke 已提交
59 60 61 62 63 64 65 66 67
 "?", // UnknownEdge
 "P", // PointsToEdge
 "D", // DeferredEdge
 "F"  // FieldEdge
};

void PointsToNode::dump() const {
  NodeType nt = node_type();
  EscapeState es = escape_state();
68
  tty->print("%s %s %s [[", node_type_names[(int) nt], esc_names[(int) es], _scalar_replaceable ? "" : "NSR");
D
duke 已提交
69 70 71 72 73 74 75 76 77 78 79
  for (uint i = 0; i < edge_count(); i++) {
    tty->print(" %d%s", edge_target(i), edge_type_suffix[(int) edge_type(i)]);
  }
  tty->print("]]  ");
  if (_node == NULL)
    tty->print_cr("<null>");
  else
    _node->dump();
}
#endif

80 81 82 83 84 85 86
ConnectionGraph::ConnectionGraph(Compile * C) :
  _nodes(C->comp_arena(), C->unique(), C->unique(), PointsToNode()),
  _processed(C->comp_arena()),
  _collecting(true),
  _compile(C),
  _node_map(C->comp_arena()) {

D
duke 已提交
87 88
  _phantom_object = C->top()->_idx;
  PointsToNode *phn = ptnode_adr(_phantom_object);
89
  phn->_node = C->top();
D
duke 已提交
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
  phn->set_node_type(PointsToNode::JavaObject);
  phn->set_escape_state(PointsToNode::GlobalEscape);
}

void ConnectionGraph::add_pointsto_edge(uint from_i, uint to_i) {
  PointsToNode *f = ptnode_adr(from_i);
  PointsToNode *t = ptnode_adr(to_i);

  assert(f->node_type() != PointsToNode::UnknownType && t->node_type() != PointsToNode::UnknownType, "node types must be set");
  assert(f->node_type() == PointsToNode::LocalVar || f->node_type() == PointsToNode::Field, "invalid source of PointsTo edge");
  assert(t->node_type() == PointsToNode::JavaObject, "invalid destination of PointsTo edge");
  f->add_edge(to_i, PointsToNode::PointsToEdge);
}

void ConnectionGraph::add_deferred_edge(uint from_i, uint to_i) {
  PointsToNode *f = ptnode_adr(from_i);
  PointsToNode *t = ptnode_adr(to_i);

  assert(f->node_type() != PointsToNode::UnknownType && t->node_type() != PointsToNode::UnknownType, "node types must be set");
  assert(f->node_type() == PointsToNode::LocalVar || f->node_type() == PointsToNode::Field, "invalid source of Deferred edge");
  assert(t->node_type() == PointsToNode::LocalVar || t->node_type() == PointsToNode::Field, "invalid destination of Deferred edge");
  // don't add a self-referential edge, this can occur during removal of
  // deferred edges
  if (from_i != to_i)
    f->add_edge(to_i, PointsToNode::DeferredEdge);
}

117 118 119 120 121 122 123 124 125 126 127 128 129 130
int ConnectionGraph::address_offset(Node* adr, PhaseTransform *phase) {
  const Type *adr_type = phase->type(adr);
  if (adr->is_AddP() && adr_type->isa_oopptr() == NULL &&
      adr->in(AddPNode::Address)->is_Proj() &&
      adr->in(AddPNode::Address)->in(0)->is_Allocate()) {
    // We are computing a raw address for a store captured by an Initialize
    // compute an appropriate address type. AddP cases #3 and #5 (see below).
    int offs = (int)phase->find_intptr_t_con(adr->in(AddPNode::Offset), Type::OffsetBot);
    assert(offs != Type::OffsetBot ||
           adr->in(AddPNode::Address)->in(0)->is_AllocateArray(),
           "offset must be a constant or it is initialization of array");
    return offs;
  }
  const TypePtr *t_ptr = adr_type->isa_ptr();
D
duke 已提交
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
  assert(t_ptr != NULL, "must be a pointer type");
  return t_ptr->offset();
}

void ConnectionGraph::add_field_edge(uint from_i, uint to_i, int offset) {
  PointsToNode *f = ptnode_adr(from_i);
  PointsToNode *t = ptnode_adr(to_i);

  assert(f->node_type() != PointsToNode::UnknownType && t->node_type() != PointsToNode::UnknownType, "node types must be set");
  assert(f->node_type() == PointsToNode::JavaObject, "invalid destination of Field edge");
  assert(t->node_type() == PointsToNode::Field, "invalid destination of Field edge");
  assert (t->offset() == -1 || t->offset() == offset, "conflicting field offsets");
  t->set_offset(offset);

  f->add_edge(to_i, PointsToNode::FieldEdge);
}

void ConnectionGraph::set_escape_state(uint ni, PointsToNode::EscapeState es) {
  PointsToNode *npt = ptnode_adr(ni);
  PointsToNode::EscapeState old_es = npt->escape_state();
  if (es > old_es)
    npt->set_escape_state(es);
}

155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
void ConnectionGraph::add_node(Node *n, PointsToNode::NodeType nt,
                               PointsToNode::EscapeState es, bool done) {
  PointsToNode* ptadr = ptnode_adr(n->_idx);
  ptadr->_node = n;
  ptadr->set_node_type(nt);

  // inline set_escape_state(idx, es);
  PointsToNode::EscapeState old_es = ptadr->escape_state();
  if (es > old_es)
    ptadr->set_escape_state(es);

  if (done)
    _processed.set(n->_idx);
}

D
duke 已提交
170 171 172 173
PointsToNode::EscapeState ConnectionGraph::escape_state(Node *n, PhaseTransform *phase) {
  uint idx = n->_idx;
  PointsToNode::EscapeState es;

174 175
  // If we are still collecting or there were no non-escaping allocations
  // we don't know the answer yet
176
  if (_collecting)
D
duke 已提交
177 178 179 180
    return PointsToNode::UnknownEscape;

  // if the node was created after the escape computation, return
  // UnknownEscape
181
  if (idx >= nodes_size())
D
duke 已提交
182 183
    return PointsToNode::UnknownEscape;

184
  es = ptnode_adr(idx)->escape_state();
D
duke 已提交
185 186 187 188 189

  // if we have already computed a value, return it
  if (es != PointsToNode::UnknownEscape)
    return es;

190 191 192 193
  // PointsTo() calls n->uncast() which can return a new ideal node.
  if (n->uncast()->_idx >= nodes_size())
    return PointsToNode::UnknownEscape;

D
duke 已提交
194 195 196
  // compute max escape state of anything this node could point to
  VectorSet ptset(Thread::current()->resource_area());
  PointsTo(ptset, n, phase);
197
  for(VectorSetI i(&ptset); i.test() && es != PointsToNode::GlobalEscape; ++i) {
D
duke 已提交
198
    uint pt = i.elem;
199
    PointsToNode::EscapeState pes = ptnode_adr(pt)->escape_state();
D
duke 已提交
200 201 202 203 204
    if (pes > es)
      es = pes;
  }
  // cache the computed escape state
  assert(es != PointsToNode::UnknownEscape, "should have computed an escape state");
205
  ptnode_adr(idx)->set_escape_state(es);
D
duke 已提交
206 207 208 209 210 211 212
  return es;
}

void ConnectionGraph::PointsTo(VectorSet &ptset, Node * n, PhaseTransform *phase) {
  VectorSet visited(Thread::current()->resource_area());
  GrowableArray<uint>  worklist;

213 214 215 216
#ifdef ASSERT
  Node *orig_n = n;
#endif

217
  n = n->uncast();
218
  PointsToNode* npt = ptnode_adr(n->_idx);
D
duke 已提交
219 220

  // If we have a JavaObject, return just that object
221
  if (npt->node_type() == PointsToNode::JavaObject) {
D
duke 已提交
222 223 224
    ptset.set(n->_idx);
    return;
  }
225
#ifdef ASSERT
226
  if (npt->_node == NULL) {
227 228 229
    if (orig_n != n)
      orig_n->dump();
    n->dump();
230
    assert(npt->_node != NULL, "unregistered node");
231 232
  }
#endif
D
duke 已提交
233 234 235
  worklist.push(n->_idx);
  while(worklist.length() > 0) {
    int ni = worklist.pop();
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
    if (visited.test_set(ni))
      continue;

    PointsToNode* pn = ptnode_adr(ni);
    // ensure that all inputs of a Phi have been processed
    assert(!_collecting || !pn->_node->is_Phi() || _processed.test(ni),"");

    int edges_processed = 0;
    uint e_cnt = pn->edge_count();
    for (uint e = 0; e < e_cnt; e++) {
      uint etgt = pn->edge_target(e);
      PointsToNode::EdgeType et = pn->edge_type(e);
      if (et == PointsToNode::PointsToEdge) {
        ptset.set(etgt);
        edges_processed++;
      } else if (et == PointsToNode::DeferredEdge) {
        worklist.push(etgt);
        edges_processed++;
      } else {
        assert(false,"neither PointsToEdge or DeferredEdge");
D
duke 已提交
256 257
      }
    }
258 259 260 261 262
    if (edges_processed == 0) {
      // no deferred or pointsto edges found.  Assume the value was set
      // outside this method.  Add the phantom object to the pointsto set.
      ptset.set(_phantom_object);
    }
D
duke 已提交
263 264 265
  }
}

266 267 268 269 270
void ConnectionGraph::remove_deferred(uint ni, GrowableArray<uint>* deferred_edges, VectorSet* visited) {
  // This method is most expensive during ConnectionGraph construction.
  // Reuse vectorSet and an additional growable array for deferred edges.
  deferred_edges->clear();
  visited->Clear();
D
duke 已提交
271

272
  visited->set(ni);
D
duke 已提交
273 274
  PointsToNode *ptn = ptnode_adr(ni);

275
  // Mark current edges as visited and move deferred edges to separate array.
276
  for (uint i = 0; i < ptn->edge_count(); ) {
277
    uint t = ptn->edge_target(i);
278 279 280 281 282 283
#ifdef ASSERT
    assert(!visited->test_set(t), "expecting no duplications");
#else
    visited->set(t);
#endif
    if (ptn->edge_type(i) == PointsToNode::DeferredEdge) {
D
duke 已提交
284
      ptn->remove_edge(t, PointsToNode::DeferredEdge);
285
      deferred_edges->append(t);
286 287
    } else {
      i++;
288 289 290 291 292
    }
  }
  for (int next = 0; next < deferred_edges->length(); ++next) {
    uint t = deferred_edges->at(next);
    PointsToNode *ptt = ptnode_adr(t);
293 294 295 296
    uint e_cnt = ptt->edge_count();
    for (uint e = 0; e < e_cnt; e++) {
      uint etgt = ptt->edge_target(e);
      if (visited->test_set(etgt))
297
        continue;
298 299 300 301 302 303 304 305 306 307 308 309

      PointsToNode::EdgeType et = ptt->edge_type(e);
      if (et == PointsToNode::PointsToEdge) {
        add_pointsto_edge(ni, etgt);
        if(etgt == _phantom_object) {
          // Special case - field set outside (globally escaping).
          ptn->set_escape_state(PointsToNode::GlobalEscape);
        }
      } else if (et == PointsToNode::DeferredEdge) {
        deferred_edges->append(etgt);
      } else {
        assert(false,"invalid connection graph");
D
duke 已提交
310 311 312 313 314 315 316 317 318 319 320
      }
    }
  }
}


//  Add an edge to node given by "to_i" from any field of adr_i whose offset
//  matches "offset"  A deferred edge is added if to_i is a LocalVar, and
//  a pointsto edge is added if it is a JavaObject

void ConnectionGraph::add_edge_from_fields(uint adr_i, uint to_i, int offs) {
321 322 323 324 325 326 327 328 329
  PointsToNode* an = ptnode_adr(adr_i);
  PointsToNode* to = ptnode_adr(to_i);
  bool deferred = (to->node_type() == PointsToNode::LocalVar);

  for (uint fe = 0; fe < an->edge_count(); fe++) {
    assert(an->edge_type(fe) == PointsToNode::FieldEdge, "expecting a field edge");
    int fi = an->edge_target(fe);
    PointsToNode* pf = ptnode_adr(fi);
    int po = pf->offset();
D
duke 已提交
330 331 332 333 334 335 336 337 338
    if (po == offs || po == Type::OffsetBot || offs == Type::OffsetBot) {
      if (deferred)
        add_deferred_edge(fi, to_i);
      else
        add_pointsto_edge(fi, to_i);
    }
  }
}

339 340
// Add a deferred  edge from node given by "from_i" to any field of adr_i
// whose offset matches "offset".
D
duke 已提交
341
void ConnectionGraph::add_deferred_edge_to_fields(uint from_i, uint adr_i, int offs) {
342 343 344 345 346 347 348
  PointsToNode* an = ptnode_adr(adr_i);
  for (uint fe = 0; fe < an->edge_count(); fe++) {
    assert(an->edge_type(fe) == PointsToNode::FieldEdge, "expecting a field edge");
    int fi = an->edge_target(fe);
    PointsToNode* pf = ptnode_adr(fi);
    int po = pf->offset();
    if (pf->edge_count() == 0) {
D
duke 已提交
349 350 351 352 353 354 355 356 357
      // we have not seen any stores to this field, assume it was set outside this method
      add_pointsto_edge(fi, _phantom_object);
    }
    if (po == offs || po == Type::OffsetBot || offs == Type::OffsetBot) {
      add_deferred_edge(from_i, fi);
    }
  }
}

358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
// Helper functions

static Node* get_addp_base(Node *addp) {
  assert(addp->is_AddP(), "must be AddP");
  //
  // AddP cases for Base and Address inputs:
  // case #1. Direct object's field reference:
  //     Allocate
  //       |
  //     Proj #5 ( oop result )
  //       |
  //     CheckCastPP (cast to instance type)
  //      | |
  //     AddP  ( base == address )
  //
  // case #2. Indirect object's field reference:
  //      Phi
  //       |
  //     CastPP (cast to instance type)
  //      | |
  //     AddP  ( base == address )
  //
  // case #3. Raw object's field reference for Initialize node:
  //      Allocate
  //        |
  //      Proj #5 ( oop result )
  //  top   |
  //     \  |
  //     AddP  ( base == top )
  //
  // case #4. Array's element reference:
  //   {CheckCastPP | CastPP}
  //     |  | |
  //     |  AddP ( array's element offset )
  //     |  |
  //     AddP ( array's offset )
  //
  // case #5. Raw object's field reference for arraycopy stub call:
  //          The inline_native_clone() case when the arraycopy stub is called
  //          after the allocation before Initialize and CheckCastPP nodes.
  //      Allocate
  //        |
  //      Proj #5 ( oop result )
  //       | |
  //       AddP  ( base == address )
  //
K
kvn 已提交
404 405 406
  // case #6. Constant Pool, ThreadLocal, CastX2P or
  //          Raw object's field reference:
  //      {ConP, ThreadLocal, CastX2P, raw Load}
407 408 409 410
  //  top   |
  //     \  |
  //     AddP  ( base == top )
  //
K
kvn 已提交
411 412 413 414 415
  // case #7. Klass's field reference.
  //      LoadKlass
  //       | |
  //       AddP  ( base == address )
  //
416 417 418 419 420 421 422
  // case #8. narrow Klass's field reference.
  //      LoadNKlass
  //       |
  //      DecodeN
  //       | |
  //       AddP  ( base == address )
  //
423 424 425 426
  Node *base = addp->in(AddPNode::Base)->uncast();
  if (base->is_top()) { // The AddP case #3 and #6.
    base = addp->in(AddPNode::Address)->uncast();
    assert(base->Opcode() == Op_ConP || base->Opcode() == Op_ThreadLocal ||
427
           base->Opcode() == Op_CastX2P || base->is_DecodeN() ||
K
kvn 已提交
428 429
           (base->is_Mem() && base->bottom_type() == TypeRawPtr::NOTNULL) ||
           (base->is_Proj() && base->in(0)->is_Allocate()), "sanity");
D
duke 已提交
430
  }
431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
  return base;
}

static Node* find_second_addp(Node* addp, Node* n) {
  assert(addp->is_AddP() && addp->outcnt() > 0, "Don't process dead nodes");

  Node* addp2 = addp->raw_out(0);
  if (addp->outcnt() == 1 && addp2->is_AddP() &&
      addp2->in(AddPNode::Base) == n &&
      addp2->in(AddPNode::Address) == addp) {

    assert(addp->in(AddPNode::Base) == n, "expecting the same base");
    //
    // Find array's offset to push it on worklist first and
    // as result process an array's element offset first (pushed second)
    // to avoid CastPP for the array's offset.
    // Otherwise the inserted CastPP (LocalVar) will point to what
    // the AddP (Field) points to. Which would be wrong since
    // the algorithm expects the CastPP has the same point as
    // as AddP's base CheckCastPP (LocalVar).
    //
    //    ArrayAllocation
    //     |
    //    CheckCastPP
    //     |
    //    memProj (from ArrayAllocation CheckCastPP)
    //     |  ||
    //     |  ||   Int (element index)
    //     |  ||    |   ConI (log(element size))
    //     |  ||    |   /
    //     |  ||   LShift
    //     |  ||  /
    //     |  AddP (array's element offset)
    //     |  |
    //     |  | ConI (array's offset: #12(32-bits) or #24(64-bits))
    //     | / /
    //     AddP (array's offset)
    //      |
    //     Load/Store (memory operation on array's element)
    //
    return addp2;
  }
  return NULL;
D
duke 已提交
474 475 476 477 478 479 480 481
}

//
// Adjust the type and inputs of an AddP which computes the
// address of a field of an instance
//
void ConnectionGraph::split_AddP(Node *addp, Node *base,  PhaseGVN  *igvn) {
  const TypeOopPtr *base_t = igvn->type(base)->isa_oopptr();
482
  assert(base_t != NULL && base_t->is_known_instance(), "expecting instance oopptr");
483 484 485 486 487 488 489 490 491 492
  const TypeOopPtr *t = igvn->type(addp)->isa_oopptr();
  if (t == NULL) {
    // We are computing a raw address for a store captured by an Initialize
    // compute an appropriate address type.
    assert(igvn->type(addp) == TypeRawPtr::NOTNULL, "must be raw pointer");
    assert(addp->in(AddPNode::Address)->is_Proj(), "base of raw address must be result projection from allocation");
    int offs = (int)igvn->find_intptr_t_con(addp->in(AddPNode::Offset), Type::OffsetBot);
    assert(offs != Type::OffsetBot, "offset must be a constant");
    t = base_t->add_offset(offs)->is_oopptr();
  }
493 494
  int inst_id =  base_t->instance_id();
  assert(!t->is_known_instance() || t->instance_id() == inst_id,
D
duke 已提交
495 496
                             "old type must be non-instance or match new type");
  const TypeOopPtr *tinst = base_t->add_offset(t->offset())->is_oopptr();
497 498
  // Do NOT remove the next call: ensure an new alias index is allocated
  // for the instance type
D
duke 已提交
499 500 501 502
  int alias_idx = _compile->get_alias_index(tinst);
  igvn->set_type(addp, tinst);
  // record the allocation in the node map
  set_map(addp->_idx, get_map(base->_idx));
503 504
  // if the Address input is not the appropriate instance type
  // (due to intervening casts,) insert a cast
D
duke 已提交
505 506
  Node *adr = addp->in(AddPNode::Address);
  const TypeOopPtr  *atype = igvn->type(adr)->isa_oopptr();
507
  if (atype != NULL && atype->instance_id() != inst_id) {
508
    assert(!atype->is_known_instance(), "no conflicting instances");
D
duke 已提交
509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526
    const TypeOopPtr *new_atype = base_t->add_offset(atype->offset())->isa_oopptr();
    Node *acast = new (_compile, 2) CastPPNode(adr, new_atype);
    acast->set_req(0, adr->in(0));
    igvn->set_type(acast, new_atype);
    record_for_optimizer(acast);
    Node *bcast = acast;
    Node *abase = addp->in(AddPNode::Base);
    if (abase != adr) {
      bcast = new (_compile, 2) CastPPNode(abase, base_t);
      bcast->set_req(0, abase->in(0));
      igvn->set_type(bcast, base_t);
      record_for_optimizer(bcast);
    }
    igvn->hash_delete(addp);
    addp->set_req(AddPNode::Base, bcast);
    addp->set_req(AddPNode::Address, acast);
    igvn->hash_insert(addp);
  }
527 528
  // Put on IGVN worklist since at least addp's type was changed above.
  record_for_optimizer(addp);
D
duke 已提交
529 530 531 532 533 534 535 536 537 538 539 540
}

//
// Create a new version of orig_phi if necessary. Returns either the newly
// created phi or an existing phi.  Sets create_new to indicate wheter  a new
// phi was created.  Cache the last newly created phi in the node map.
//
PhiNode *ConnectionGraph::create_split_phi(PhiNode *orig_phi, int alias_idx, GrowableArray<PhiNode *>  &orig_phi_worklist, PhaseGVN  *igvn, bool &new_created) {
  Compile *C = _compile;
  new_created = false;
  int phi_alias_idx = C->get_alias_index(orig_phi->adr_type());
  // nothing to do if orig_phi is bottom memory or matches alias_idx
541
  if (phi_alias_idx == alias_idx) {
D
duke 已提交
542 543 544 545 546 547 548
    return orig_phi;
  }
  // have we already created a Phi for this alias index?
  PhiNode *result = get_map_phi(orig_phi->_idx);
  if (result != NULL && C->get_alias_index(result->adr_type()) == alias_idx) {
    return result;
  }
549 550 551 552 553 554 555 556 557
  if ((int)C->unique() + 2*NodeLimitFudgeFactor > MaxNodeLimit) {
    if (C->do_escape_analysis() == true && !C->failing()) {
      // Retry compilation without escape analysis.
      // If this is the first failure, the sentinel string will "stick"
      // to the Compile object, and the C2Compiler will see it and retry.
      C->record_failure(C2Compiler::retry_no_escape_analysis());
    }
    return NULL;
  }
D
duke 已提交
558
  orig_phi_worklist.append_if_missing(orig_phi);
559
  const TypePtr *atype = C->get_adr_type(alias_idx);
D
duke 已提交
560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576
  result = PhiNode::make(orig_phi->in(0), NULL, Type::MEMORY, atype);
  set_map_phi(orig_phi->_idx, result);
  igvn->set_type(result, result->bottom_type());
  record_for_optimizer(result);
  new_created = true;
  return result;
}

//
// Return a new version  of Memory Phi "orig_phi" with the inputs having the
// specified alias index.
//
PhiNode *ConnectionGraph::split_memory_phi(PhiNode *orig_phi, int alias_idx, GrowableArray<PhiNode *>  &orig_phi_worklist, PhaseGVN  *igvn) {

  assert(alias_idx != Compile::AliasIdxBot, "can't split out bottom memory");
  Compile *C = _compile;
  bool new_phi_created;
577
  PhiNode *result = create_split_phi(orig_phi, alias_idx, orig_phi_worklist, igvn, new_phi_created);
D
duke 已提交
578 579 580 581 582 583 584 585 586 587 588 589
  if (!new_phi_created) {
    return result;
  }

  GrowableArray<PhiNode *>  phi_list;
  GrowableArray<uint>  cur_input;

  PhiNode *phi = orig_phi;
  uint idx = 1;
  bool finished = false;
  while(!finished) {
    while (idx < phi->req()) {
590
      Node *mem = find_inst_mem(phi->in(idx), alias_idx, orig_phi_worklist, igvn);
D
duke 已提交
591
      if (mem != NULL && mem->is_Phi()) {
592
        PhiNode *newphi = create_split_phi(mem->as_Phi(), alias_idx, orig_phi_worklist, igvn, new_phi_created);
D
duke 已提交
593 594 595 596 597 598
        if (new_phi_created) {
          // found an phi for which we created a new split, push current one on worklist and begin
          // processing new one
          phi_list.push(phi);
          cur_input.push(idx);
          phi = mem->as_Phi();
599
          result = newphi;
D
duke 已提交
600 601 602
          idx = 1;
          continue;
        } else {
603
          mem = newphi;
D
duke 已提交
604 605
        }
      }
606 607 608
      if (C->failing()) {
        return NULL;
      }
D
duke 已提交
609 610 611 612 613 614
      result->set_req(idx++, mem);
    }
#ifdef ASSERT
    // verify that the new Phi has an input for each input of the original
    assert( phi->req() == result->req(), "must have same number of inputs.");
    assert( result->in(0) != NULL && result->in(0) == phi->in(0), "regions must match");
615 616 617
#endif
    // Check if all new phi's inputs have specified alias index.
    // Otherwise use old phi.
D
duke 已提交
618
    for (uint i = 1; i < phi->req(); i++) {
619 620
      Node* in = result->in(i);
      assert((phi->in(i) == NULL) == (in == NULL), "inputs must correspond.");
D
duke 已提交
621 622 623 624 625 626
    }
    // we have finished processing a Phi, see if there are any more to do
    finished = (phi_list.length() == 0 );
    if (!finished) {
      phi = phi_list.pop();
      idx = cur_input.pop();
627 628 629
      PhiNode *prev_result = get_map_phi(phi->_idx);
      prev_result->set_req(idx++, result);
      result = prev_result;
D
duke 已提交
630 631 632 633 634
    }
  }
  return result;
}

635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661

//
// The next methods are derived from methods in MemNode.
//
static Node *step_through_mergemem(MergeMemNode *mmem, int alias_idx, const TypeOopPtr *tinst) {
  Node *mem = mmem;
  // TypeInstPtr::NOTNULL+any is an OOP with unknown offset - generally
  // means an array I have not precisely typed yet.  Do not do any
  // alias stuff with it any time soon.
  if( tinst->base() != Type::AnyPtr &&
      !(tinst->klass()->is_java_lang_Object() &&
        tinst->offset() == Type::OffsetBot) ) {
    mem = mmem->memory_at(alias_idx);
    // Update input if it is progress over what we have now
  }
  return mem;
}

//
// Search memory chain of "mem" to find a MemNode whose address
// is the specified alias index.
//
Node* ConnectionGraph::find_inst_mem(Node *orig_mem, int alias_idx, GrowableArray<PhiNode *>  &orig_phis, PhaseGVN *phase) {
  if (orig_mem == NULL)
    return orig_mem;
  Compile* C = phase->C;
  const TypeOopPtr *tinst = C->get_adr_type(alias_idx)->isa_oopptr();
662
  bool is_instance = (tinst != NULL) && tinst->is_known_instance();
663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691
  Node *prev = NULL;
  Node *result = orig_mem;
  while (prev != result) {
    prev = result;
    if (result->is_Mem()) {
      MemNode *mem = result->as_Mem();
      const Type *at = phase->type(mem->in(MemNode::Address));
      if (at != Type::TOP) {
        assert (at->isa_ptr() != NULL, "pointer type required.");
        int idx = C->get_alias_index(at->is_ptr());
        if (idx == alias_idx)
          break;
      }
      result = mem->in(MemNode::Memory);
    }
    if (!is_instance)
      continue;  // don't search further for non-instance types
    // skip over a call which does not affect this memory slice
    if (result->is_Proj() && result->as_Proj()->_con == TypeFunc::Memory) {
      Node *proj_in = result->in(0);
      if (proj_in->is_Call()) {
        CallNode *call = proj_in->as_Call();
        if (!call->may_modify(tinst, phase)) {
          result = call->in(TypeFunc::Memory);
        }
      } else if (proj_in->is_Initialize()) {
        AllocateNode* alloc = proj_in->as_Initialize()->allocation();
        // Stop if this is the initialization for the object instance which
        // which contains this memory slice, otherwise skip over it.
692
        if (alloc == NULL || alloc->_idx != (uint)tinst->instance_id()) {
693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732
          result = proj_in->in(TypeFunc::Memory);
        }
      } else if (proj_in->is_MemBar()) {
        result = proj_in->in(TypeFunc::Memory);
      }
    } else if (result->is_MergeMem()) {
      MergeMemNode *mmem = result->as_MergeMem();
      result = step_through_mergemem(mmem, alias_idx, tinst);
      if (result == mmem->base_memory()) {
        // Didn't find instance memory, search through general slice recursively.
        result = mmem->memory_at(C->get_general_index(alias_idx));
        result = find_inst_mem(result, alias_idx, orig_phis, phase);
        if (C->failing()) {
          return NULL;
        }
        mmem->set_memory_at(alias_idx, result);
      }
    } else if (result->is_Phi() &&
               C->get_alias_index(result->as_Phi()->adr_type()) != alias_idx) {
      Node *un = result->as_Phi()->unique_input(phase);
      if (un != NULL) {
        result = un;
      } else {
        break;
      }
    }
  }
  if (is_instance && result->is_Phi()) {
    PhiNode *mphi = result->as_Phi();
    assert(mphi->bottom_type() == Type::MEMORY, "memory phi required");
    const TypePtr *t = mphi->adr_type();
    if (C->get_alias_index(t) != alias_idx) {
      result = split_memory_phi(mphi, alias_idx, orig_phis, phase);
    }
  }
  // the result is either MemNode, PhiNode, InitializeNode.
  return result;
}


D
duke 已提交
733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830
//
//  Convert the types of unescaped object to instance types where possible,
//  propagate the new type information through the graph, and update memory
//  edges and MergeMem inputs to reflect the new type.
//
//  We start with allocations (and calls which may be allocations)  on alloc_worklist.
//  The processing is done in 4 phases:
//
//  Phase 1:  Process possible allocations from alloc_worklist.  Create instance
//            types for the CheckCastPP for allocations where possible.
//            Propagate the the new types through users as follows:
//               casts and Phi:  push users on alloc_worklist
//               AddP:  cast Base and Address inputs to the instance type
//                      push any AddP users on alloc_worklist and push any memnode
//                      users onto memnode_worklist.
//  Phase 2:  Process MemNode's from memnode_worklist. compute new address type and
//            search the Memory chain for a store with the appropriate type
//            address type.  If a Phi is found, create a new version with
//            the approriate memory slices from each of the Phi inputs.
//            For stores, process the users as follows:
//               MemNode:  push on memnode_worklist
//               MergeMem: push on mergemem_worklist
//  Phase 3:  Process MergeMem nodes from mergemem_worklist.  Walk each memory slice
//            moving the first node encountered of each  instance type to the
//            the input corresponding to its alias index.
//            appropriate memory slice.
//  Phase 4:  Update the inputs of non-instance memory Phis and the Memory input of memnodes.
//
// In the following example, the CheckCastPP nodes are the cast of allocation
// results and the allocation of node 29 is unescaped and eligible to be an
// instance type.
//
// We start with:
//
//     7 Parm #memory
//    10  ConI  "12"
//    19  CheckCastPP   "Foo"
//    20  AddP  _ 19 19 10  Foo+12  alias_index=4
//    29  CheckCastPP   "Foo"
//    30  AddP  _ 29 29 10  Foo+12  alias_index=4
//
//    40  StoreP  25   7  20   ... alias_index=4
//    50  StoreP  35  40  30   ... alias_index=4
//    60  StoreP  45  50  20   ... alias_index=4
//    70  LoadP    _  60  30   ... alias_index=4
//    80  Phi     75  50  60   Memory alias_index=4
//    90  LoadP    _  80  30   ... alias_index=4
//   100  LoadP    _  80  20   ... alias_index=4
//
//
// Phase 1 creates an instance type for node 29 assigning it an instance id of 24
// and creating a new alias index for node 30.  This gives:
//
//     7 Parm #memory
//    10  ConI  "12"
//    19  CheckCastPP   "Foo"
//    20  AddP  _ 19 19 10  Foo+12  alias_index=4
//    29  CheckCastPP   "Foo"  iid=24
//    30  AddP  _ 29 29 10  Foo+12  alias_index=6  iid=24
//
//    40  StoreP  25   7  20   ... alias_index=4
//    50  StoreP  35  40  30   ... alias_index=6
//    60  StoreP  45  50  20   ... alias_index=4
//    70  LoadP    _  60  30   ... alias_index=6
//    80  Phi     75  50  60   Memory alias_index=4
//    90  LoadP    _  80  30   ... alias_index=6
//   100  LoadP    _  80  20   ... alias_index=4
//
// In phase 2, new memory inputs are computed for the loads and stores,
// And a new version of the phi is created.  In phase 4, the inputs to
// node 80 are updated and then the memory nodes are updated with the
// values computed in phase 2.  This results in:
//
//     7 Parm #memory
//    10  ConI  "12"
//    19  CheckCastPP   "Foo"
//    20  AddP  _ 19 19 10  Foo+12  alias_index=4
//    29  CheckCastPP   "Foo"  iid=24
//    30  AddP  _ 29 29 10  Foo+12  alias_index=6  iid=24
//
//    40  StoreP  25  7   20   ... alias_index=4
//    50  StoreP  35  7   30   ... alias_index=6
//    60  StoreP  45  40  20   ... alias_index=4
//    70  LoadP    _  50  30   ... alias_index=6
//    80  Phi     75  40  60   Memory alias_index=4
//   120  Phi     75  50  50   Memory alias_index=6
//    90  LoadP    _ 120  30   ... alias_index=6
//   100  LoadP    _  80  20   ... alias_index=4
//
void ConnectionGraph::split_unique_types(GrowableArray<Node *>  &alloc_worklist) {
  GrowableArray<Node *>  memnode_worklist;
  GrowableArray<Node *>  mergemem_worklist;
  GrowableArray<PhiNode *>  orig_phis;
  PhaseGVN  *igvn = _compile->initial_gvn();
  uint new_index_start = (uint) _compile->num_alias_types();
  VectorSet visited(Thread::current()->resource_area());
  VectorSet ptset(Thread::current()->resource_area());

831 832 833

  //  Phase 1:  Process possible allocations from alloc_worklist.
  //  Create instance types for the CheckCastPP for allocations where possible.
834 835 836 837 838
  //
  // (Note: don't forget to change the order of the second AddP node on
  //  the alloc_worklist if the order of the worklist processing is changed,
  //  see the comment in find_second_addp().)
  //
D
duke 已提交
839 840 841
  while (alloc_worklist.length() != 0) {
    Node *n = alloc_worklist.pop();
    uint ni = n->_idx;
842
    const TypeOopPtr* tinst = NULL;
D
duke 已提交
843 844 845
    if (n->is_Call()) {
      CallNode *alloc = n->as_Call();
      // copy escape information to call node
846
      PointsToNode* ptn = ptnode_adr(alloc->_idx);
D
duke 已提交
847
      PointsToNode::EscapeState es = escape_state(alloc, igvn);
848 849 850
      // We have an allocation or call which returns a Java object,
      // see if it is unescaped.
      if (es != PointsToNode::NoEscape || !ptn->_scalar_replaceable)
D
duke 已提交
851
        continue;
852 853 854 855
      if (alloc->is_Allocate()) {
        // Set the scalar_replaceable flag before the next check.
        alloc->as_Allocate()->_is_scalar_replaceable = true;
      }
856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885
      // find CheckCastPP of call return value
      n = alloc->result_cast();
      if (n == NULL ||          // No uses accept Initialize or
          !n->is_CheckCastPP()) // not unique CheckCastPP.
        continue;
      // The inline code for Object.clone() casts the allocation result to
      // java.lang.Object and then to the the actual type of the allocated
      // object. Detect this case and use the second cast.
      if (alloc->is_Allocate() && n->as_Type()->type() == TypeInstPtr::NOTNULL
          && igvn->type(alloc->in(AllocateNode::KlassNode)) != TypeKlassPtr::OBJECT) {
        Node *cast2 = NULL;
        for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
          Node *use = n->fast_out(i);
          if (use->is_CheckCastPP()) {
            cast2 = use;
            break;
          }
        }
        if (cast2 != NULL) {
          n = cast2;
        } else {
          continue;
        }
      }
      set_escape_state(n->_idx, es);
      // in order for an object to be stackallocatable, it must be:
      //   - a direct allocation (not a call returning an object)
      //   - non-escaping
      //   - eligible to be a unique type
      //   - not determined to be ineligible by escape analysis
D
duke 已提交
886 887
      set_map(alloc->_idx, n);
      set_map(n->_idx, alloc);
888 889
      const TypeOopPtr *t = igvn->type(n)->isa_oopptr();
      if (t == NULL)
D
duke 已提交
890
        continue;  // not a TypeInstPtr
891
      tinst = t->cast_to_instance_id(ni);
D
duke 已提交
892 893 894 895
      igvn->hash_delete(n);
      igvn->set_type(n,  tinst);
      n->raise_bottom_type(tinst);
      igvn->hash_insert(n);
896 897 898
      record_for_optimizer(n);
      if (alloc->is_Allocate() && ptn->_scalar_replaceable &&
          (t->isa_instptr() || t->isa_aryptr())) {
899 900 901 902

        // First, put on the worklist all Field edges from Connection Graph
        // which is more accurate then putting immediate users from Ideal Graph.
        for (uint e = 0; e < ptn->edge_count(); e++) {
903
          Node *use = ptnode_adr(ptn->edge_target(e))->_node;
904 905 906 907 908 909 910 911 912 913 914 915
          assert(ptn->edge_type(e) == PointsToNode::FieldEdge && use->is_AddP(),
                 "only AddP nodes are Field edges in CG");
          if (use->outcnt() > 0) { // Don't process dead nodes
            Node* addp2 = find_second_addp(use, use->in(AddPNode::Base));
            if (addp2 != NULL) {
              assert(alloc->is_AllocateArray(),"array allocation was expected");
              alloc_worklist.append_if_missing(addp2);
            }
            alloc_worklist.append_if_missing(use);
          }
        }

916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934
        // An allocation may have an Initialize which has raw stores. Scan
        // the users of the raw allocation result and push AddP users
        // on alloc_worklist.
        Node *raw_result = alloc->proj_out(TypeFunc::Parms);
        assert (raw_result != NULL, "must have an allocation result");
        for (DUIterator_Fast imax, i = raw_result->fast_outs(imax); i < imax; i++) {
          Node *use = raw_result->fast_out(i);
          if (use->is_AddP() && use->outcnt() > 0) { // Don't process dead nodes
            Node* addp2 = find_second_addp(use, raw_result);
            if (addp2 != NULL) {
              assert(alloc->is_AllocateArray(),"array allocation was expected");
              alloc_worklist.append_if_missing(addp2);
            }
            alloc_worklist.append_if_missing(use);
          } else if (use->is_Initialize()) {
            memnode_worklist.append_if_missing(use);
          }
        }
      }
D
duke 已提交
935 936
    } else if (n->is_AddP()) {
      ptset.Clear();
937
      PointsTo(ptset, get_addp_base(n), igvn);
D
duke 已提交
938
      assert(ptset.Size() == 1, "AddP address is unique");
939 940 941 942
      uint elem = ptset.getelem(); // Allocation node's index
      if (elem == _phantom_object)
        continue; // Assume the value was set outside this method.
      Node *base = get_map(elem);  // CheckCastPP node
D
duke 已提交
943
      split_AddP(n, base, igvn);
944 945 946
      tinst = igvn->type(base)->isa_oopptr();
    } else if (n->is_Phi() ||
               n->is_CheckCastPP() ||
947 948
               n->is_EncodeP() ||
               n->is_DecodeN() ||
949
               (n->is_ConstraintCast() && n->Opcode() == Op_CastPP)) {
D
duke 已提交
950 951 952 953 954 955 956
      if (visited.test_set(n->_idx)) {
        assert(n->is_Phi(), "loops only through Phi's");
        continue;  // already processed
      }
      ptset.Clear();
      PointsTo(ptset, n, igvn);
      if (ptset.Size() == 1) {
957 958 959 960
        uint elem = ptset.getelem(); // Allocation node's index
        if (elem == _phantom_object)
          continue; // Assume the value was set outside this method.
        Node *val = get_map(elem);   // CheckCastPP node
D
duke 已提交
961
        TypeNode *tn = n->as_Type();
962
        tinst = igvn->type(val)->isa_oopptr();
963 964
        assert(tinst != NULL && tinst->is_known_instance() &&
               (uint)tinst->instance_id() == elem , "instance type expected.");
965 966

        const Type *tn_type = igvn->type(tn);
967
        const TypeOopPtr *tn_t;
968
        if (tn_type->isa_narrowoop()) {
969
          tn_t = tn_type->make_ptr()->isa_oopptr();
970 971 972
        } else {
          tn_t = tn_type->isa_oopptr();
        }
D
duke 已提交
973

974
        if (tn_t != NULL &&
975
            tinst->cast_to_instance_id(TypeOopPtr::InstanceBot)->higher_equal(tn_t)) {
976 977 978 979 980
          if (tn_type->isa_narrowoop()) {
            tn_type = tinst->make_narrowoop();
          } else {
            tn_type = tinst;
          }
D
duke 已提交
981
          igvn->hash_delete(tn);
982 983
          igvn->set_type(tn, tn_type);
          tn->set_type(tn_type);
D
duke 已提交
984
          igvn->hash_insert(tn);
985
          record_for_optimizer(n);
D
duke 已提交
986 987 988 989 990 991 992 993 994
        }
      }
    } else {
      continue;
    }
    // push users on appropriate worklist
    for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
      Node *use = n->fast_out(i);
      if(use->is_Mem() && use->in(MemNode::Address) == n) {
995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019
        memnode_worklist.append_if_missing(use);
      } else if (use->is_Initialize()) {
        memnode_worklist.append_if_missing(use);
      } else if (use->is_MergeMem()) {
        mergemem_worklist.append_if_missing(use);
      } else if (use->is_Call() && tinst != NULL) {
        // Look for MergeMem nodes for calls which reference unique allocation
        // (through CheckCastPP nodes) even for debug info.
        Node* m = use->in(TypeFunc::Memory);
        uint iid = tinst->instance_id();
        while (m->is_Proj() && m->in(0)->is_Call() &&
               m->in(0) != use && !m->in(0)->_idx != iid) {
          m = m->in(0)->in(TypeFunc::Memory);
        }
        if (m->is_MergeMem()) {
          mergemem_worklist.append_if_missing(m);
        }
      } else if (use->is_AddP() && use->outcnt() > 0) { // No dead nodes
        Node* addp2 = find_second_addp(use, n);
        if (addp2 != NULL) {
          alloc_worklist.append_if_missing(addp2);
        }
        alloc_worklist.append_if_missing(use);
      } else if (use->is_Phi() ||
                 use->is_CheckCastPP() ||
1020 1021
                 use->is_EncodeP() ||
                 use->is_DecodeN() ||
1022 1023
                 (use->is_ConstraintCast() && use->Opcode() == Op_CastPP)) {
        alloc_worklist.append_if_missing(use);
D
duke 已提交
1024 1025 1026 1027
      }
    }

  }
1028
  // New alias types were created in split_AddP().
D
duke 已提交
1029 1030 1031 1032 1033 1034 1035 1036 1037 1038
  uint new_index_end = (uint) _compile->num_alias_types();

  //  Phase 2:  Process MemNode's from memnode_worklist. compute new address type and
  //            compute new values for Memory inputs  (the Memory inputs are not
  //            actually updated until phase 4.)
  if (memnode_worklist.length() == 0)
    return;  // nothing to do

  while (memnode_worklist.length() != 0) {
    Node *n = memnode_worklist.pop();
1039 1040
    if (visited.test_set(n->_idx))
      continue;
D
duke 已提交
1041 1042 1043 1044
    if (n->is_Phi()) {
      assert(n->as_Phi()->adr_type() != TypePtr::BOTTOM, "narrow memory slice required");
      // we don't need to do anything, but the users must be pushed if we haven't processed
      // this Phi before
1045 1046 1047 1048
    } else if (n->is_Initialize()) {
      // we don't need to do anything, but the users of the memory projection must be pushed
      n = n->as_Initialize()->proj_out(TypeFunc::Memory);
      if (n == NULL)
D
duke 已提交
1049 1050 1051 1052
        continue;
    } else {
      assert(n->is_Mem(), "memory node required.");
      Node *addr = n->in(MemNode::Address);
1053
      assert(addr->is_AddP(), "AddP required");
D
duke 已提交
1054 1055 1056 1057 1058
      const Type *addr_t = igvn->type(addr);
      if (addr_t == Type::TOP)
        continue;
      assert (addr_t->isa_ptr() != NULL, "pointer type required.");
      int alias_idx = _compile->get_alias_index(addr_t->is_ptr());
1059 1060
      assert ((uint)alias_idx < new_index_end, "wrong alias index");
      Node *mem = find_inst_mem(n->in(MemNode::Memory), alias_idx, orig_phis, igvn);
1061 1062 1063
      if (_compile->failing()) {
        return;
      }
1064
      if (mem != n->in(MemNode::Memory)) {
D
duke 已提交
1065
        set_map(n->_idx, mem);
1066
        ptnode_adr(n->_idx)->_node = n;
1067
      }
D
duke 已提交
1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085
      if (n->is_Load()) {
        continue;  // don't push users
      } else if (n->is_LoadStore()) {
        // get the memory projection
        for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
          Node *use = n->fast_out(i);
          if (use->Opcode() == Op_SCMemProj) {
            n = use;
            break;
          }
        }
        assert(n->Opcode() == Op_SCMemProj, "memory projection required");
      }
    }
    // push user on appropriate worklist
    for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
      Node *use = n->fast_out(i);
      if (use->is_Phi()) {
1086
        memnode_worklist.append_if_missing(use);
D
duke 已提交
1087
      } else if(use->is_Mem() && use->in(MemNode::Memory) == n) {
1088 1089 1090
        memnode_worklist.append_if_missing(use);
      } else if (use->is_Initialize()) {
        memnode_worklist.append_if_missing(use);
D
duke 已提交
1091
      } else if (use->is_MergeMem()) {
1092
        mergemem_worklist.append_if_missing(use);
D
duke 已提交
1093 1094 1095 1096
      }
    }
  }

1097 1098 1099
  //  Phase 3:  Process MergeMem nodes from mergemem_worklist.
  //            Walk each memory moving the first node encountered of each
  //            instance type to the the input corresponding to its alias index.
D
duke 已提交
1100 1101 1102
  while (mergemem_worklist.length() != 0) {
    Node *n = mergemem_worklist.pop();
    assert(n->is_MergeMem(), "MergeMem node required.");
1103 1104
    if (visited.test_set(n->_idx))
      continue;
D
duke 已提交
1105 1106
    MergeMemNode *nmm = n->as_MergeMem();
    // Note: we don't want to use MergeMemStream here because we only want to
1107
    //  scan inputs which exist at the start, not ones we add during processing.
D
duke 已提交
1108 1109 1110
    uint nslices = nmm->req();
    igvn->hash_delete(nmm);
    for (uint i = Compile::AliasIdxRaw+1; i < nslices; i++) {
1111 1112
      Node* mem = nmm->in(i);
      Node* cur = NULL;
D
duke 已提交
1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131
      if (mem == NULL || mem->is_top())
        continue;
      while (mem->is_Mem()) {
        const Type *at = igvn->type(mem->in(MemNode::Address));
        if (at != Type::TOP) {
          assert (at->isa_ptr() != NULL, "pointer type required.");
          uint idx = (uint)_compile->get_alias_index(at->is_ptr());
          if (idx == i) {
            if (cur == NULL)
              cur = mem;
          } else {
            if (idx >= nmm->req() || nmm->is_empty_memory(nmm->in(idx))) {
              nmm->set_memory_at(idx, mem);
            }
          }
        }
        mem = mem->in(MemNode::Memory);
      }
      nmm->set_memory_at(i, (cur != NULL) ? cur : mem);
1132 1133 1134 1135 1136 1137 1138 1139 1140
      // Find any instance of the current type if we haven't encountered
      // a value of the instance along the chain.
      for (uint ni = new_index_start; ni < new_index_end; ni++) {
        if((uint)_compile->get_general_index(ni) == i) {
          Node *m = (ni >= nmm->req()) ? nmm->empty_memory() : nmm->in(ni);
          if (nmm->is_empty_memory(m)) {
            Node* result = find_inst_mem(mem, ni, orig_phis, igvn);
            if (_compile->failing()) {
              return;
D
duke 已提交
1141
            }
1142
            nmm->set_memory_at(ni, result);
D
duke 已提交
1143 1144 1145 1146
          }
        }
      }
    }
1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160
    // Find the rest of instances values
    for (uint ni = new_index_start; ni < new_index_end; ni++) {
      const TypeOopPtr *tinst = igvn->C->get_adr_type(ni)->isa_oopptr();
      Node* result = step_through_mergemem(nmm, ni, tinst);
      if (result == nmm->base_memory()) {
        // Didn't find instance memory, search through general slice recursively.
        result = nmm->memory_at(igvn->C->get_general_index(ni));
        result = find_inst_mem(result, ni, orig_phis, igvn);
        if (_compile->failing()) {
          return;
        }
        nmm->set_memory_at(ni, result);
      }
    }
D
duke 已提交
1161 1162
    igvn->hash_insert(nmm);
    record_for_optimizer(nmm);
1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197

    // Propagate new memory slices to following MergeMem nodes.
    for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
      Node *use = n->fast_out(i);
      if (use->is_Call()) {
        CallNode* in = use->as_Call();
        if (in->proj_out(TypeFunc::Memory) != NULL) {
          Node* m = in->proj_out(TypeFunc::Memory);
          for (DUIterator_Fast jmax, j = m->fast_outs(jmax); j < jmax; j++) {
            Node* mm = m->fast_out(j);
            if (mm->is_MergeMem()) {
              mergemem_worklist.append_if_missing(mm);
            }
          }
        }
        if (use->is_Allocate()) {
          use = use->as_Allocate()->initialization();
          if (use == NULL) {
            continue;
          }
        }
      }
      if (use->is_Initialize()) {
        InitializeNode* in = use->as_Initialize();
        if (in->proj_out(TypeFunc::Memory) != NULL) {
          Node* m = in->proj_out(TypeFunc::Memory);
          for (DUIterator_Fast jmax, j = m->fast_outs(jmax); j < jmax; j++) {
            Node* mm = m->fast_out(j);
            if (mm->is_MergeMem()) {
              mergemem_worklist.append_if_missing(mm);
            }
          }
        }
      }
    }
D
duke 已提交
1198 1199
  }

1200 1201
  //  Phase 4:  Update the inputs of non-instance memory Phis and
  //            the Memory input of memnodes
D
duke 已提交
1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212
  // First update the inputs of any non-instance Phi's from
  // which we split out an instance Phi.  Note we don't have
  // to recursively process Phi's encounted on the input memory
  // chains as is done in split_memory_phi() since they  will
  // also be processed here.
  while (orig_phis.length() != 0) {
    PhiNode *phi = orig_phis.pop();
    int alias_idx = _compile->get_alias_index(phi->adr_type());
    igvn->hash_delete(phi);
    for (uint i = 1; i < phi->req(); i++) {
      Node *mem = phi->in(i);
1213 1214 1215 1216
      Node *new_mem = find_inst_mem(mem, alias_idx, orig_phis, igvn);
      if (_compile->failing()) {
        return;
      }
D
duke 已提交
1217 1218 1219 1220 1221 1222 1223 1224 1225 1226
      if (mem != new_mem) {
        phi->set_req(i, new_mem);
      }
    }
    igvn->hash_insert(phi);
    record_for_optimizer(phi);
  }

  // Update the memory inputs of MemNodes with the value we computed
  // in Phase 2.
1227
  for (uint i = 0; i < nodes_size(); i++) {
D
duke 已提交
1228 1229
    Node *nmem = get_map(i);
    if (nmem != NULL) {
1230
      Node *n = ptnode_adr(i)->_node;
D
duke 已提交
1231 1232 1233 1234 1235 1236 1237 1238 1239 1240
      if (n != NULL && n->is_Mem()) {
        igvn->hash_delete(n);
        n->set_req(MemNode::Memory, nmem);
        igvn->hash_insert(n);
        record_for_optimizer(n);
      }
    }
  }
}

1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259
bool ConnectionGraph::has_candidates(Compile *C) {
  // EA brings benefits only when the code has allocations and/or locks which
  // are represented by ideal Macro nodes.
  int cnt = C->macro_count();
  for( int i=0; i < cnt; i++ ) {
    Node *n = C->macro_node(i);
    if ( n->is_Allocate() )
      return true;
    if( n->is_Lock() ) {
      Node* obj = n->as_Lock()->obj_node()->uncast();
      if( !(obj->is_Parm() || obj->is_Con()) )
        return true;
    }
  }
  return false;
}

bool ConnectionGraph::compute_escape() {
  Compile* C = _compile;
D
duke 已提交
1260

1261
  // 1. Populate Connection Graph (CG) with Ideal nodes.
D
duke 已提交
1262

1263
  Unique_Node_List worklist_init;
1264
  worklist_init.map(C->unique(), NULL);  // preallocate space
1265 1266

  // Initialize worklist
1267 1268
  if (C->root() != NULL) {
    worklist_init.push(C->root());
1269 1270 1271
  }

  GrowableArray<int> cg_worklist;
1272
  PhaseGVN* igvn = C->initial_gvn();
1273 1274 1275 1276 1277 1278
  bool has_allocations = false;

  // Push all useful nodes onto CG list and set their type.
  for( uint next = 0; next < worklist_init.size(); ++next ) {
    Node* n = worklist_init.at(next);
    record_for_escape_analysis(n, igvn);
1279 1280 1281 1282
    // Only allocations and java static calls results are checked
    // for an escape status. See process_call_result() below.
    if (n->is_Allocate() || n->is_CallStaticJava() &&
        ptnode_adr(n->_idx)->node_type() == PointsToNode::JavaObject) {
1283 1284 1285 1286 1287 1288 1289 1290 1291 1292
      has_allocations = true;
    }
    if(n->is_AddP())
      cg_worklist.append(n->_idx);
    for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
      Node* m = n->fast_out(i);   // Get user
      worklist_init.push(m);
    }
  }

1293
  if (!has_allocations) {
1294
    _collecting = false;
1295
    return false; // Nothing to do.
1296 1297 1298
  }

  // 2. First pass to create simple CG edges (doesn't require to walk CG).
1299 1300
  uint delayed_size = _delayed_worklist.size();
  for( uint next = 0; next < delayed_size; ++next ) {
1301 1302 1303 1304 1305
    Node* n = _delayed_worklist.at(next);
    build_connection_graph(n, igvn);
  }

  // 3. Pass to create fields edges (Allocate -F-> AddP).
1306 1307
  uint cg_length = cg_worklist.length();
  for( uint next = 0; next < cg_length; ++next ) {
1308
    int ni = cg_worklist.at(next);
1309
    build_connection_graph(ptnode_adr(ni)->_node, igvn);
1310 1311 1312 1313 1314 1315 1316
  }

  cg_worklist.clear();
  cg_worklist.append(_phantom_object);

  // 4. Build Connection Graph which need
  //    to walk the connection graph.
1317 1318
  for (uint ni = 0; ni < nodes_size(); ni++) {
    PointsToNode* ptn = ptnode_adr(ni);
1319 1320 1321 1322 1323 1324
    Node *n = ptn->_node;
    if (n != NULL) { // Call, AddP, LoadP, StoreP
      build_connection_graph(n, igvn);
      if (ptn->node_type() != PointsToNode::UnknownType)
        cg_worklist.append(n->_idx); // Collect CG nodes
    }
D
duke 已提交
1325 1326 1327
  }

  VectorSet ptset(Thread::current()->resource_area());
1328 1329
  GrowableArray<uint>  deferred_edges;
  VectorSet visited(Thread::current()->resource_area());
D
duke 已提交
1330

1331 1332 1333 1334
  // 5. Remove deferred edges from the graph and collect
  //    information needed for type splitting.
  cg_length = cg_worklist.length();
  for( uint next = 0; next < cg_length; ++next ) {
1335
    int ni = cg_worklist.at(next);
1336
    PointsToNode* ptn = ptnode_adr(ni);
D
duke 已提交
1337 1338
    PointsToNode::NodeType nt = ptn->node_type();
    if (nt == PointsToNode::LocalVar || nt == PointsToNode::Field) {
1339
      remove_deferred(ni, &deferred_edges, &visited);
1340
      Node *n = ptn->_node;
D
duke 已提交
1341
      if (n->is_AddP()) {
1342
        // If this AddP computes an address which may point to more that one
1343 1344
        // object or more then one field (array's element), nothing the address
        // points to can be scalar replaceable.
1345
        Node *base = get_addp_base(n);
D
duke 已提交
1346 1347
        ptset.Clear();
        PointsTo(ptset, base, igvn);
1348 1349
        if (ptset.Size() > 1 ||
            (ptset.Size() != 0 && ptn->offset() == Type::OffsetBot)) {
D
duke 已提交
1350
          for( VectorSetI j(&ptset); j.test(); ++j ) {
1351
            ptnode_adr(j.elem)->_scalar_replaceable = false;
D
duke 已提交
1352 1353 1354 1355 1356
          }
        }
      }
    }
  }
1357

1358 1359 1360 1361
  // 6. Propagate escape states.
  GrowableArray<int>  worklist;
  bool has_non_escaping_obj = false;

D
duke 已提交
1362
  // push all GlobalEscape nodes on the worklist
1363
  for( uint next = 0; next < cg_length; ++next ) {
1364
    int nk = cg_worklist.at(next);
1365 1366
    if (ptnode_adr(nk)->escape_state() == PointsToNode::GlobalEscape)
      worklist.push(nk);
D
duke 已提交
1367
  }
1368
  // mark all nodes reachable from GlobalEscape nodes
D
duke 已提交
1369
  while(worklist.length() > 0) {
1370 1371 1372 1373
    PointsToNode* ptn = ptnode_adr(worklist.pop());
    uint e_cnt = ptn->edge_count();
    for (uint ei = 0; ei < e_cnt; ei++) {
      uint npi = ptn->edge_target(ei);
D
duke 已提交
1374
      PointsToNode *np = ptnode_adr(npi);
1375
      if (np->escape_state() < PointsToNode::GlobalEscape) {
D
duke 已提交
1376
        np->set_escape_state(PointsToNode::GlobalEscape);
1377
        worklist.push(npi);
D
duke 已提交
1378 1379 1380 1381 1382
      }
    }
  }

  // push all ArgEscape nodes on the worklist
1383
  for( uint next = 0; next < cg_length; ++next ) {
1384
    int nk = cg_worklist.at(next);
1385
    if (ptnode_adr(nk)->escape_state() == PointsToNode::ArgEscape)
D
duke 已提交
1386 1387
      worklist.push(nk);
  }
1388
  // mark all nodes reachable from ArgEscape nodes
D
duke 已提交
1389
  while(worklist.length() > 0) {
1390 1391 1392 1393 1394 1395
    PointsToNode* ptn = ptnode_adr(worklist.pop());
    if (ptn->node_type() == PointsToNode::JavaObject)
      has_non_escaping_obj = true; // Non GlobalEscape
    uint e_cnt = ptn->edge_count();
    for (uint ei = 0; ei < e_cnt; ei++) {
      uint npi = ptn->edge_target(ei);
D
duke 已提交
1396
      PointsToNode *np = ptnode_adr(npi);
1397
      if (np->escape_state() < PointsToNode::ArgEscape) {
D
duke 已提交
1398
        np->set_escape_state(PointsToNode::ArgEscape);
1399
        worklist.push(npi);
D
duke 已提交
1400 1401 1402 1403
      }
    }
  }

1404 1405
  GrowableArray<Node*> alloc_worklist;

1406
  // push all NoEscape nodes on the worklist
1407
  for( uint next = 0; next < cg_length; ++next ) {
1408
    int nk = cg_worklist.at(next);
1409
    if (ptnode_adr(nk)->escape_state() == PointsToNode::NoEscape)
1410 1411
      worklist.push(nk);
  }
1412
  // mark all nodes reachable from NoEscape nodes
1413
  while(worklist.length() > 0) {
1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425
    PointsToNode* ptn = ptnode_adr(worklist.pop());
    if (ptn->node_type() == PointsToNode::JavaObject)
      has_non_escaping_obj = true; // Non GlobalEscape
    Node* n = ptn->_node;
    if (n->is_Allocate() && ptn->_scalar_replaceable ) {
      // Push scalar replaceable alocations on alloc_worklist
      // for processing in split_unique_types().
      alloc_worklist.append(n);
    }
    uint e_cnt = ptn->edge_count();
    for (uint ei = 0; ei < e_cnt; ei++) {
      uint npi = ptn->edge_target(ei);
1426 1427 1428
      PointsToNode *np = ptnode_adr(npi);
      if (np->escape_state() < PointsToNode::NoEscape) {
        np->set_escape_state(PointsToNode::NoEscape);
1429
        worklist.push(npi);
1430 1431 1432
      }
    }
  }
1433

1434
  _collecting = false;
1435
  assert(C->unique() == nodes_size(), "there should be no new ideal nodes during ConnectionGraph build");
D
duke 已提交
1436

1437 1438 1439
  bool has_scalar_replaceable_candidates = alloc_worklist.length() > 0;
  if ( has_scalar_replaceable_candidates &&
       C->AliasLevel() >= 3 && EliminateAllocations ) {
D
duke 已提交
1440

1441
    // Now use the escape information to create unique types for
1442
    // scalar replaceable objects.
1443
    split_unique_types(alloc_worklist);
1444 1445

    if (C->failing())  return false;
D
duke 已提交
1446

1447 1448
    // Clean up after split unique types.
    ResourceMark rm;
1449 1450 1451
    PhaseRemoveUseless pru(C->initial_gvn(), C->for_igvn());

    C->print_method("After Escape Analysis", 2);
D
duke 已提交
1452

1453
#ifdef ASSERT
1454
  } else if (Verbose && (PrintEscapeAnalysis || PrintEliminateAllocations)) {
1455
    tty->print("=== No allocations eliminated for ");
1456
    C->method()->print_short_name();
1457 1458
    if(!EliminateAllocations) {
      tty->print(" since EliminateAllocations is off ===");
1459 1460 1461
    } else if(!has_scalar_replaceable_candidates) {
      tty->print(" since there are no scalar replaceable candidates ===");
    } else if(C->AliasLevel() < 3) {
1462
      tty->print(" since AliasLevel < 3 ===");
D
duke 已提交
1463
    }
1464 1465
    tty->cr();
#endif
D
duke 已提交
1466
  }
1467
  return has_non_escaping_obj;
D
duke 已提交
1468 1469 1470 1471 1472
}

void ConnectionGraph::process_call_arguments(CallNode *call, PhaseTransform *phase) {

    switch (call->Opcode()) {
1473
#ifdef ASSERT
D
duke 已提交
1474 1475 1476 1477
    case Op_Allocate:
    case Op_AllocateArray:
    case Op_Lock:
    case Op_Unlock:
1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512
      assert(false, "should be done already");
      break;
#endif
    case Op_CallLeafNoFP:
    {
      // Stub calls, objects do not escape but they are not scale replaceable.
      // Adjust escape state for outgoing arguments.
      const TypeTuple * d = call->tf()->domain();
      VectorSet ptset(Thread::current()->resource_area());
      for (uint i = TypeFunc::Parms; i < d->cnt(); i++) {
        const Type* at = d->field_at(i);
        Node *arg = call->in(i)->uncast();
        const Type *aat = phase->type(arg);
        if (!arg->is_top() && at->isa_ptr() && aat->isa_ptr()) {
          assert(aat == Type::TOP || aat == TypePtr::NULL_PTR ||
                 aat->isa_ptr() != NULL, "expecting an Ptr");
          set_escape_state(arg->_idx, PointsToNode::ArgEscape);
          if (arg->is_AddP()) {
            //
            // The inline_native_clone() case when the arraycopy stub is called
            // after the allocation before Initialize and CheckCastPP nodes.
            //
            // Set AddP's base (Allocate) as not scalar replaceable since
            // pointer to the base (with offset) is passed as argument.
            //
            arg = get_addp_base(arg);
          }
          ptset.Clear();
          PointsTo(ptset, arg, phase);
          for( VectorSetI j(&ptset); j.test(); ++j ) {
            uint pt = j.elem;
            set_escape_state(pt, PointsToNode::ArgEscape);
          }
        }
      }
D
duke 已提交
1513
      break;
1514
    }
D
duke 已提交
1515 1516 1517 1518 1519 1520

    case Op_CallStaticJava:
    // For a static call, we know exactly what method is being called.
    // Use bytecode estimator to record the call's escape affects
    {
      ciMethod *meth = call->as_CallJava()->method();
1521 1522 1523
      BCEscapeAnalyzer *call_analyzer = (meth !=NULL) ? meth->get_bcea() : NULL;
      // fall-through if not a Java method or no analyzer information
      if (call_analyzer != NULL) {
D
duke 已提交
1524 1525
        const TypeTuple * d = call->tf()->domain();
        VectorSet ptset(Thread::current()->resource_area());
1526
        bool copy_dependencies = false;
D
duke 已提交
1527 1528 1529 1530 1531
        for (uint i = TypeFunc::Parms; i < d->cnt(); i++) {
          const Type* at = d->field_at(i);
          int k = i - TypeFunc::Parms;

          if (at->isa_oopptr() != NULL) {
1532
            Node *arg = call->in(i)->uncast();
D
duke 已提交
1533

1534 1535 1536
            bool global_escapes = false;
            bool fields_escapes = false;
            if (!call_analyzer->is_arg_stack(k)) {
D
duke 已提交
1537
              // The argument global escapes, mark everything it could point to
1538 1539 1540 1541 1542 1543 1544 1545 1546 1547
              set_escape_state(arg->_idx, PointsToNode::GlobalEscape);
              global_escapes = true;
            } else {
              if (!call_analyzer->is_arg_local(k)) {
                // The argument itself doesn't escape, but any fields might
                fields_escapes = true;
              }
              set_escape_state(arg->_idx, PointsToNode::ArgEscape);
              copy_dependencies = true;
            }
D
duke 已提交
1548

1549 1550 1551 1552 1553 1554
            ptset.Clear();
            PointsTo(ptset, arg, phase);
            for( VectorSetI j(&ptset); j.test(); ++j ) {
              uint pt = j.elem;
              if (global_escapes) {
                //The argument global escapes, mark everything it could point to
D
duke 已提交
1555
                set_escape_state(pt, PointsToNode::GlobalEscape);
1556 1557 1558 1559 1560 1561
              } else {
                if (fields_escapes) {
                  // The argument itself doesn't escape, but any fields might
                  add_edge_from_fields(pt, _phantom_object, Type::OffsetBot);
                }
                set_escape_state(pt, PointsToNode::ArgEscape);
D
duke 已提交
1562 1563 1564 1565
              }
            }
          }
        }
1566
        if (copy_dependencies)
1567
          call_analyzer->copy_dependencies(_compile->dependencies());
D
duke 已提交
1568 1569 1570 1571 1572
        break;
      }
    }

    default:
1573 1574
    // Fall-through here if not a Java method or no analyzer information
    // or some other type of call, assume the worst case: all arguments
D
duke 已提交
1575 1576 1577 1578 1579 1580 1581 1582
    // globally escape.
    {
      // adjust escape state for  outgoing arguments
      const TypeTuple * d = call->tf()->domain();
      VectorSet ptset(Thread::current()->resource_area());
      for (uint i = TypeFunc::Parms; i < d->cnt(); i++) {
        const Type* at = d->field_at(i);
        if (at->isa_oopptr() != NULL) {
1583 1584
          Node *arg = call->in(i)->uncast();
          set_escape_state(arg->_idx, PointsToNode::GlobalEscape);
D
duke 已提交
1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596
          ptset.Clear();
          PointsTo(ptset, arg, phase);
          for( VectorSetI j(&ptset); j.test(); ++j ) {
            uint pt = j.elem;
            set_escape_state(pt, PointsToNode::GlobalEscape);
          }
        }
      }
    }
  }
}
void ConnectionGraph::process_call_result(ProjNode *resproj, PhaseTransform *phase) {
1597 1598 1599
  CallNode   *call = resproj->in(0)->as_Call();
  uint    call_idx = call->_idx;
  uint resproj_idx = resproj->_idx;
D
duke 已提交
1600 1601 1602 1603 1604 1605 1606 1607 1608

  switch (call->Opcode()) {
    case Op_Allocate:
    {
      Node *k = call->in(AllocateNode::KlassNode);
      const TypeKlassPtr *kt;
      if (k->Opcode() == Op_LoadKlass) {
        kt = k->as_Load()->type()->isa_klassptr();
      } else {
1609
        // Also works for DecodeN(LoadNKlass).
D
duke 已提交
1610 1611 1612 1613 1614 1615
        kt = k->as_Type()->type()->isa_klassptr();
      }
      assert(kt != NULL, "TypeKlassPtr  required.");
      ciKlass* cik = kt->klass();
      ciInstanceKlass* ciik = cik->as_instance_klass();

1616 1617
      PointsToNode::EscapeState es;
      uint edge_to;
D
duke 已提交
1618
      if (cik->is_subclass_of(_compile->env()->Thread_klass()) || ciik->has_finalizer()) {
1619 1620
        es = PointsToNode::GlobalEscape;
        edge_to = _phantom_object; // Could not be worse
D
duke 已提交
1621
      } else {
1622
        es = PointsToNode::NoEscape;
1623
        edge_to = call_idx;
D
duke 已提交
1624
      }
1625 1626 1627
      set_escape_state(call_idx, es);
      add_pointsto_edge(resproj_idx, edge_to);
      _processed.set(resproj_idx);
D
duke 已提交
1628 1629 1630 1631 1632
      break;
    }

    case Op_AllocateArray:
    {
1633 1634 1635
      int length = call->in(AllocateNode::ALength)->find_int_con(-1);
      if (length < 0 || length > EliminateAllocationArraySizeLimit) {
        // Not scalar replaceable if the length is not constant or too big.
1636
        ptnode_adr(call_idx)->_scalar_replaceable = false;
1637
      }
1638 1639 1640
      set_escape_state(call_idx, PointsToNode::NoEscape);
      add_pointsto_edge(resproj_idx, call_idx);
      _processed.set(resproj_idx);
D
duke 已提交
1641 1642 1643 1644 1645 1646 1647
      break;
    }

    case Op_CallStaticJava:
    // For a static call, we know exactly what method is being called.
    // Use bytecode estimator to record whether the call's return value escapes
    {
1648
      bool done = true;
D
duke 已提交
1649 1650 1651 1652 1653 1654 1655 1656
      const TypeTuple *r = call->tf()->range();
      const Type* ret_type = NULL;

      if (r->cnt() > TypeFunc::Parms)
        ret_type = r->field_at(TypeFunc::Parms);

      // Note:  we use isa_ptr() instead of isa_oopptr()  here because the
      //        _multianewarray functions return a TypeRawPtr.
1657
      if (ret_type == NULL || ret_type->isa_ptr() == NULL) {
1658
        _processed.set(resproj_idx);
D
duke 已提交
1659
        break;  // doesn't return a pointer type
1660
      }
D
duke 已提交
1661
      ciMethod *meth = call->as_CallJava()->method();
1662
      const TypeTuple * d = call->tf()->domain();
D
duke 已提交
1663 1664
      if (meth == NULL) {
        // not a Java method, assume global escape
1665 1666
        set_escape_state(call_idx, PointsToNode::GlobalEscape);
        add_pointsto_edge(resproj_idx, _phantom_object);
D
duke 已提交
1667
      } else {
1668 1669
        BCEscapeAnalyzer *call_analyzer = meth->get_bcea();
        bool copy_dependencies = false;
D
duke 已提交
1670

1671 1672 1673 1674 1675
        if (call_analyzer->is_return_allocated()) {
          // Returns a newly allocated unescaped object, simply
          // update dependency information.
          // Mark it as NoEscape so that objects referenced by
          // it's fields will be marked as NoEscape at least.
1676 1677
          set_escape_state(call_idx, PointsToNode::NoEscape);
          add_pointsto_edge(resproj_idx, call_idx);
1678
          copy_dependencies = true;
1679
        } else if (call_analyzer->is_return_local()) {
D
duke 已提交
1680
          // determine whether any arguments are returned
1681
          set_escape_state(call_idx, PointsToNode::NoEscape);
D
duke 已提交
1682 1683 1684 1685
          for (uint i = TypeFunc::Parms; i < d->cnt(); i++) {
            const Type* at = d->field_at(i);

            if (at->isa_oopptr() != NULL) {
1686
              Node *arg = call->in(i)->uncast();
D
duke 已提交
1687

1688
              if (call_analyzer->is_arg_returned(i - TypeFunc::Parms)) {
1689
                PointsToNode *arg_esp = ptnode_adr(arg->_idx);
1690 1691 1692
                if (arg_esp->node_type() == PointsToNode::UnknownType)
                  done = false;
                else if (arg_esp->node_type() == PointsToNode::JavaObject)
1693
                  add_pointsto_edge(resproj_idx, arg->_idx);
D
duke 已提交
1694
                else
1695
                  add_deferred_edge(resproj_idx, arg->_idx);
D
duke 已提交
1696 1697 1698 1699
                arg_esp->_hidden_alias = true;
              }
            }
          }
1700
          copy_dependencies = true;
D
duke 已提交
1701
        } else {
1702 1703
          set_escape_state(call_idx, PointsToNode::GlobalEscape);
          add_pointsto_edge(resproj_idx, _phantom_object);
1704 1705 1706 1707
          for (uint i = TypeFunc::Parms; i < d->cnt(); i++) {
            const Type* at = d->field_at(i);
            if (at->isa_oopptr() != NULL) {
              Node *arg = call->in(i)->uncast();
1708
              PointsToNode *arg_esp = ptnode_adr(arg->_idx);
1709 1710 1711
              arg_esp->_hidden_alias = true;
            }
          }
D
duke 已提交
1712
        }
1713
        if (copy_dependencies)
1714
          call_analyzer->copy_dependencies(_compile->dependencies());
D
duke 已提交
1715
      }
1716
      if (done)
1717
        _processed.set(resproj_idx);
D
duke 已提交
1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731
      break;
    }

    default:
    // Some other type of call, assume the worst case that the
    // returned value, if any, globally escapes.
    {
      const TypeTuple *r = call->tf()->range();
      if (r->cnt() > TypeFunc::Parms) {
        const Type* ret_type = r->field_at(TypeFunc::Parms);

        // Note:  we use isa_ptr() instead of isa_oopptr()  here because the
        //        _multianewarray functions return a TypeRawPtr.
        if (ret_type->isa_ptr() != NULL) {
1732 1733
          set_escape_state(call_idx, PointsToNode::GlobalEscape);
          add_pointsto_edge(resproj_idx, _phantom_object);
D
duke 已提交
1734 1735
        }
      }
1736
      _processed.set(resproj_idx);
D
duke 已提交
1737 1738 1739 1740
    }
  }
}

1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763
// Populate Connection Graph with Ideal nodes and create simple
// connection graph edges (do not need to check the node_type of inputs
// or to call PointsTo() to walk the connection graph).
void ConnectionGraph::record_for_escape_analysis(Node *n, PhaseTransform *phase) {
  if (_processed.test(n->_idx))
    return; // No need to redefine node's state.

  if (n->is_Call()) {
    // Arguments to allocation and locking don't escape.
    if (n->is_Allocate()) {
      add_node(n, PointsToNode::JavaObject, PointsToNode::UnknownEscape, true);
      record_for_optimizer(n);
    } else if (n->is_Lock() || n->is_Unlock()) {
      // Put Lock and Unlock nodes on IGVN worklist to process them during
      // the first IGVN optimization when escape information is still available.
      record_for_optimizer(n);
      _processed.set(n->_idx);
    } else {
      // Have to process call's arguments first.
      PointsToNode::NodeType nt = PointsToNode::UnknownType;

      // Check if a call returns an object.
      const TypeTuple *r = n->as_Call()->tf()->range();
1764
      if (n->is_CallStaticJava() && r->cnt() > TypeFunc::Parms &&
1765 1766 1767 1768 1769 1770
          n->as_Call()->proj_out(TypeFunc::Parms) != NULL) {
        // Note:  use isa_ptr() instead of isa_oopptr() here because
        //        the _multianewarray functions return a TypeRawPtr.
        if (r->field_at(TypeFunc::Parms)->isa_ptr() != NULL) {
          nt = PointsToNode::JavaObject;
        }
D
duke 已提交
1771
      }
1772
      add_node(n, nt, PointsToNode::UnknownEscape, false);
D
duke 已提交
1773
    }
1774
    return;
D
duke 已提交
1775 1776
  }

1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791
  // Using isa_ptr() instead of isa_oopptr() for LoadP and Phi because
  // ThreadLocal has RawPrt type.
  switch (n->Opcode()) {
    case Op_AddP:
    {
      add_node(n, PointsToNode::Field, PointsToNode::UnknownEscape, false);
      break;
    }
    case Op_CastX2P:
    { // "Unsafe" memory access.
      add_node(n, PointsToNode::JavaObject, PointsToNode::GlobalEscape, true);
      break;
    }
    case Op_CastPP:
    case Op_CheckCastPP:
1792 1793
    case Op_EncodeP:
    case Op_DecodeN:
1794 1795 1796
    {
      add_node(n, PointsToNode::LocalVar, PointsToNode::UnknownEscape, false);
      int ti = n->in(1)->_idx;
1797
      PointsToNode::NodeType nt = ptnode_adr(ti)->node_type();
1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816
      if (nt == PointsToNode::UnknownType) {
        _delayed_worklist.push(n); // Process it later.
        break;
      } else if (nt == PointsToNode::JavaObject) {
        add_pointsto_edge(n->_idx, ti);
      } else {
        add_deferred_edge(n->_idx, ti);
      }
      _processed.set(n->_idx);
      break;
    }
    case Op_ConP:
    {
      // assume all pointer constants globally escape except for null
      PointsToNode::EscapeState es;
      if (phase->type(n) == TypePtr::NULL_PTR)
        es = PointsToNode::NoEscape;
      else
        es = PointsToNode::GlobalEscape;
D
duke 已提交
1817

1818 1819 1820
      add_node(n, PointsToNode::JavaObject, es, true);
      break;
    }
1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832
    case Op_ConN:
    {
      // assume all narrow oop constants globally escape except for null
      PointsToNode::EscapeState es;
      if (phase->type(n) == TypeNarrowOop::NULL_PTR)
        es = PointsToNode::NoEscape;
      else
        es = PointsToNode::GlobalEscape;

      add_node(n, PointsToNode::JavaObject, es, true);
      break;
    }
1833 1834 1835 1836 1837 1838
    case Op_CreateEx:
    {
      // assume that all exception objects globally escape
      add_node(n, PointsToNode::JavaObject, PointsToNode::GlobalEscape, true);
      break;
    }
1839
    case Op_LoadKlass:
1840
    case Op_LoadNKlass:
1841 1842 1843 1844 1845
    {
      add_node(n, PointsToNode::JavaObject, PointsToNode::GlobalEscape, true);
      break;
    }
    case Op_LoadP:
1846
    case Op_LoadN:
1847 1848
    {
      const Type *t = phase->type(n);
1849
      if (!t->isa_narrowoop() && t->isa_ptr() == NULL) {
1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886
        _processed.set(n->_idx);
        return;
      }
      add_node(n, PointsToNode::LocalVar, PointsToNode::UnknownEscape, false);
      break;
    }
    case Op_Parm:
    {
      _processed.set(n->_idx); // No need to redefine it state.
      uint con = n->as_Proj()->_con;
      if (con < TypeFunc::Parms)
        return;
      const Type *t = n->in(0)->as_Start()->_domain->field_at(con);
      if (t->isa_ptr() == NULL)
        return;
      // We have to assume all input parameters globally escape
      // (Note: passing 'false' since _processed is already set).
      add_node(n, PointsToNode::JavaObject, PointsToNode::GlobalEscape, false);
      break;
    }
    case Op_Phi:
    {
      if (n->as_Phi()->type()->isa_ptr() == NULL) {
        // nothing to do if not an oop
        _processed.set(n->_idx);
        return;
      }
      add_node(n, PointsToNode::LocalVar, PointsToNode::UnknownEscape, false);
      uint i;
      for (i = 1; i < n->req() ; i++) {
        Node* in = n->in(i);
        if (in == NULL)
          continue;  // ignore NULL
        in = in->uncast();
        if (in->is_top() || in == n)
          continue;  // ignore top or inputs which go back this node
        int ti = in->_idx;
1887
        PointsToNode::NodeType nt = ptnode_adr(ti)->node_type();
1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924
        if (nt == PointsToNode::UnknownType) {
          break;
        } else if (nt == PointsToNode::JavaObject) {
          add_pointsto_edge(n->_idx, ti);
        } else {
          add_deferred_edge(n->_idx, ti);
        }
      }
      if (i >= n->req())
        _processed.set(n->_idx);
      else
        _delayed_worklist.push(n);
      break;
    }
    case Op_Proj:
    {
      // we are only interested in the result projection from a call
      if (n->as_Proj()->_con == TypeFunc::Parms && n->in(0)->is_Call() ) {
        add_node(n, PointsToNode::LocalVar, PointsToNode::UnknownEscape, false);
        process_call_result(n->as_Proj(), phase);
        if (!_processed.test(n->_idx)) {
          // The call's result may need to be processed later if the call
          // returns it's argument and the argument is not processed yet.
          _delayed_worklist.push(n);
        }
      } else {
        _processed.set(n->_idx);
      }
      break;
    }
    case Op_Return:
    {
      if( n->req() > TypeFunc::Parms &&
          phase->type(n->in(TypeFunc::Parms))->isa_oopptr() ) {
        // Treat Return value as LocalVar with GlobalEscape escape state.
        add_node(n, PointsToNode::LocalVar, PointsToNode::GlobalEscape, false);
        int ti = n->in(TypeFunc::Parms)->_idx;
1925
        PointsToNode::NodeType nt = ptnode_adr(ti)->node_type();
1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938
        if (nt == PointsToNode::UnknownType) {
          _delayed_worklist.push(n); // Process it later.
          break;
        } else if (nt == PointsToNode::JavaObject) {
          add_pointsto_edge(n->_idx, ti);
        } else {
          add_deferred_edge(n->_idx, ti);
        }
      }
      _processed.set(n->_idx);
      break;
    }
    case Op_StoreP:
1939
    case Op_StoreN:
1940 1941
    {
      const Type *adr_type = phase->type(n->in(MemNode::Address));
1942
      adr_type = adr_type->make_ptr();
1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963
      if (adr_type->isa_oopptr()) {
        add_node(n, PointsToNode::UnknownType, PointsToNode::UnknownEscape, false);
      } else {
        Node* adr = n->in(MemNode::Address);
        if (adr->is_AddP() && phase->type(adr) == TypeRawPtr::NOTNULL &&
            adr->in(AddPNode::Address)->is_Proj() &&
            adr->in(AddPNode::Address)->in(0)->is_Allocate()) {
          add_node(n, PointsToNode::UnknownType, PointsToNode::UnknownEscape, false);
          // We are computing a raw address for a store captured
          // by an Initialize compute an appropriate address type.
          int offs = (int)phase->find_intptr_t_con(adr->in(AddPNode::Offset), Type::OffsetBot);
          assert(offs != Type::OffsetBot, "offset must be a constant");
        } else {
          _processed.set(n->_idx);
          return;
        }
      }
      break;
    }
    case Op_StorePConditional:
    case Op_CompareAndSwapP:
1964
    case Op_CompareAndSwapN:
1965 1966
    {
      const Type *adr_type = phase->type(n->in(MemNode::Address));
1967
      adr_type = adr_type->make_ptr();
1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986
      if (adr_type->isa_oopptr()) {
        add_node(n, PointsToNode::UnknownType, PointsToNode::UnknownEscape, false);
      } else {
        _processed.set(n->_idx);
        return;
      }
      break;
    }
    case Op_ThreadLocal:
    {
      add_node(n, PointsToNode::JavaObject, PointsToNode::ArgEscape, true);
      break;
    }
    default:
      ;
      // nothing to do
  }
  return;
}
D
duke 已提交
1987

1988
void ConnectionGraph::build_connection_graph(Node *n, PhaseTransform *phase) {
1989 1990
  uint n_idx = n->_idx;

1991 1992
  // Don't set processed bit for AddP, LoadP, StoreP since
  // they may need more then one pass to process.
1993
  if (_processed.test(n_idx))
1994 1995
    return; // No need to redefine node's state.

D
duke 已提交
1996 1997 1998
  if (n->is_Call()) {
    CallNode *call = n->as_Call();
    process_call_arguments(call, phase);
1999
    _processed.set(n_idx);
D
duke 已提交
2000 2001 2002
    return;
  }

2003
  switch (n->Opcode()) {
D
duke 已提交
2004 2005
    case Op_AddP:
    {
2006 2007
      Node *base = get_addp_base(n);
      // Create a field edge to this node from everything base could point to.
D
duke 已提交
2008 2009 2010 2011
      VectorSet ptset(Thread::current()->resource_area());
      PointsTo(ptset, base, phase);
      for( VectorSetI i(&ptset); i.test(); ++i ) {
        uint pt = i.elem;
2012
        add_field_edge(pt, n_idx, address_offset(n, phase));
D
duke 已提交
2013 2014 2015
      }
      break;
    }
2016
    case Op_CastX2P:
D
duke 已提交
2017
    {
2018 2019 2020 2021 2022
      assert(false, "Op_CastX2P");
      break;
    }
    case Op_CastPP:
    case Op_CheckCastPP:
2023 2024
    case Op_EncodeP:
    case Op_DecodeN:
2025 2026
    {
      int ti = n->in(1)->_idx;
2027 2028
      if (ptnode_adr(ti)->node_type() == PointsToNode::JavaObject) {
        add_pointsto_edge(n_idx, ti);
D
duke 已提交
2029
      } else {
2030
        add_deferred_edge(n_idx, ti);
D
duke 已提交
2031
      }
2032
      _processed.set(n_idx);
D
duke 已提交
2033 2034
      break;
    }
2035
    case Op_ConP:
D
duke 已提交
2036
    {
2037
      assert(false, "Op_ConP");
D
duke 已提交
2038 2039
      break;
    }
2040 2041 2042 2043 2044
    case Op_ConN:
    {
      assert(false, "Op_ConN");
      break;
    }
D
duke 已提交
2045 2046
    case Op_CreateEx:
    {
2047
      assert(false, "Op_CreateEx");
D
duke 已提交
2048 2049 2050
      break;
    }
    case Op_LoadKlass:
2051
    case Op_LoadNKlass:
D
duke 已提交
2052
    {
2053
      assert(false, "Op_LoadKlass");
D
duke 已提交
2054 2055 2056
      break;
    }
    case Op_LoadP:
2057
    case Op_LoadN:
D
duke 已提交
2058 2059
    {
      const Type *t = phase->type(n);
2060
#ifdef ASSERT
2061
      if (!t->isa_narrowoop() && t->isa_ptr() == NULL)
2062 2063
        assert(false, "Op_LoadP");
#endif
D
duke 已提交
2064

2065
      Node* adr = n->in(MemNode::Address)->uncast();
D
duke 已提交
2066
      const Type *adr_type = phase->type(adr);
2067 2068 2069 2070 2071 2072
      Node* adr_base;
      if (adr->is_AddP()) {
        adr_base = get_addp_base(adr);
      } else {
        adr_base = adr;
      }
D
duke 已提交
2073

2074 2075
      // For everything "adr_base" could point to, create a deferred edge from
      // this node to each field with the same offset.
D
duke 已提交
2076 2077
      VectorSet ptset(Thread::current()->resource_area());
      PointsTo(ptset, adr_base, phase);
2078
      int offset = address_offset(adr, phase);
D
duke 已提交
2079 2080
      for( VectorSetI i(&ptset); i.test(); ++i ) {
        uint pt = i.elem;
2081
        add_deferred_edge_to_fields(n_idx, pt, offset);
D
duke 已提交
2082 2083 2084
      }
      break;
    }
2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103
    case Op_Parm:
    {
      assert(false, "Op_Parm");
      break;
    }
    case Op_Phi:
    {
#ifdef ASSERT
      if (n->as_Phi()->type()->isa_ptr() == NULL)
        assert(false, "Op_Phi");
#endif
      for (uint i = 1; i < n->req() ; i++) {
        Node* in = n->in(i);
        if (in == NULL)
          continue;  // ignore NULL
        in = in->uncast();
        if (in->is_top() || in == n)
          continue;  // ignore top or inputs which go back this node
        int ti = in->_idx;
2104 2105
        if (ptnode_adr(in->_idx)->node_type() == PointsToNode::JavaObject) {
          add_pointsto_edge(n_idx, ti);
2106
        } else {
2107
          add_deferred_edge(n_idx, ti);
2108 2109
        }
      }
2110
      _processed.set(n_idx);
2111 2112 2113 2114 2115 2116 2117
      break;
    }
    case Op_Proj:
    {
      // we are only interested in the result projection from a call
      if (n->as_Proj()->_con == TypeFunc::Parms && n->in(0)->is_Call() ) {
        process_call_result(n->as_Proj(), phase);
2118
        assert(_processed.test(n_idx), "all call results should be processed");
2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132
      } else {
        assert(false, "Op_Proj");
      }
      break;
    }
    case Op_Return:
    {
#ifdef ASSERT
      if( n->req() <= TypeFunc::Parms ||
          !phase->type(n->in(TypeFunc::Parms))->isa_oopptr() ) {
        assert(false, "Op_Return");
      }
#endif
      int ti = n->in(TypeFunc::Parms)->_idx;
2133 2134
      if (ptnode_adr(ti)->node_type() == PointsToNode::JavaObject) {
        add_pointsto_edge(n_idx, ti);
2135
      } else {
2136
        add_deferred_edge(n_idx, ti);
2137
      }
2138
      _processed.set(n_idx);
2139 2140
      break;
    }
D
duke 已提交
2141
    case Op_StoreP:
2142
    case Op_StoreN:
D
duke 已提交
2143 2144
    case Op_StorePConditional:
    case Op_CompareAndSwapP:
2145
    case Op_CompareAndSwapN:
D
duke 已提交
2146 2147
    {
      Node *adr = n->in(MemNode::Address);
2148
      const Type *adr_type = phase->type(adr)->make_ptr();
2149
#ifdef ASSERT
D
duke 已提交
2150
      if (!adr_type->isa_oopptr())
2151 2152
        assert(phase->type(adr) == TypeRawPtr::NOTNULL, "Op_StoreP");
#endif
D
duke 已提交
2153

2154 2155 2156 2157 2158
      assert(adr->is_AddP(), "expecting an AddP");
      Node *adr_base = get_addp_base(adr);
      Node *val = n->in(MemNode::ValueIn)->uncast();
      // For everything "adr_base" could point to, create a deferred edge
      // to "val" from each field with the same offset.
D
duke 已提交
2159 2160 2161 2162
      VectorSet ptset(Thread::current()->resource_area());
      PointsTo(ptset, adr_base, phase);
      for( VectorSetI i(&ptset); i.test(); ++i ) {
        uint pt = i.elem;
2163
        add_edge_from_fields(pt, val->_idx, address_offset(adr, phase));
D
duke 已提交
2164 2165 2166
      }
      break;
    }
2167
    case Op_ThreadLocal:
D
duke 已提交
2168
    {
2169
      assert(false, "Op_ThreadLocal");
D
duke 已提交
2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182
      break;
    }
    default:
      ;
      // nothing to do
  }
}

#ifndef PRODUCT
void ConnectionGraph::dump() {
  PhaseGVN  *igvn = _compile->initial_gvn();
  bool first = true;

2183
  uint size = nodes_size();
2184
  for (uint ni = 0; ni < size; ni++) {
2185
    PointsToNode *ptn = ptnode_adr(ni);
2186 2187 2188
    PointsToNode::NodeType ptn_type = ptn->node_type();

    if (ptn_type != PointsToNode::JavaObject || ptn->_node == NULL)
D
duke 已提交
2189
      continue;
2190 2191 2192 2193 2194
    PointsToNode::EscapeState es = escape_state(ptn->_node, igvn);
    if (ptn->_node->is_Allocate() && (es == PointsToNode::NoEscape || Verbose)) {
      if (first) {
        tty->cr();
        tty->print("======== Connection graph for ");
2195
        _compile->method()->print_short_name();
2196 2197 2198 2199 2200 2201 2202
        tty->cr();
        first = false;
      }
      tty->print("%6d ", ni);
      ptn->dump();
      // Print all locals which reference this allocation
      for (uint li = ni; li < size; li++) {
2203
        PointsToNode *ptn_loc = ptnode_adr(li);
2204 2205 2206 2207
        PointsToNode::NodeType ptn_loc_type = ptn_loc->node_type();
        if ( ptn_loc_type == PointsToNode::LocalVar && ptn_loc->_node != NULL &&
             ptn_loc->edge_count() == 1 && ptn_loc->edge_target(0) == ni ) {
          tty->print("%6d  LocalVar [[%d]]", li, ni);
2208
          ptnode_adr(li)->_node->dump();
2209 2210 2211 2212 2213 2214 2215
        }
      }
      if (Verbose) {
        // Print all fields which reference this allocation
        for (uint i = 0; i < ptn->edge_count(); i++) {
          uint ei = ptn->edge_target(i);
          tty->print("%6d  Field [[%d]]", ei, ni);
2216
          ptnode_adr(ei)->_node->dump();
D
duke 已提交
2217 2218
        }
      }
2219
      tty->cr();
D
duke 已提交
2220 2221 2222 2223
    }
  }
}
#endif