< prev index next >

src/hotspot/share/opto/escape.cpp

Print this page

  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "ci/bcEscapeAnalyzer.hpp"
  27 #include "compiler/compileLog.hpp"
  28 #include "gc/shared/barrierSet.hpp"
  29 #include "gc/shared/c2/barrierSetC2.hpp"
  30 #include "libadt/vectset.hpp"
  31 #include "memory/allocation.hpp"

  32 #include "memory/resourceArea.hpp"
  33 #include "opto/c2compiler.hpp"
  34 #include "opto/arraycopynode.hpp"
  35 #include "opto/callnode.hpp"
  36 #include "opto/cfgnode.hpp"
  37 #include "opto/compile.hpp"
  38 #include "opto/escape.hpp"
  39 #include "opto/phaseX.hpp"
  40 #include "opto/movenode.hpp"
  41 #include "opto/rootnode.hpp"
  42 #include "utilities/macros.hpp"
  43 
  44 ConnectionGraph::ConnectionGraph(Compile * C, PhaseIterGVN *igvn) :
  45   _nodes(C->comp_arena(), C->unique(), C->unique(), NULL),
  46   _in_worklist(C->comp_arena()),
  47   _next_pidx(0),
  48   _collecting(true),
  49   _verify(false),
  50   _compile(C),
  51   _igvn(igvn),

 128   GrowableArray<SafePointNode*>  sfn_worklist;
 129   GrowableArray<MergeMemNode*>   mergemem_worklist;
 130   DEBUG_ONLY( GrowableArray<Node*> addp_worklist; )
 131 
 132   { Compile::TracePhase tp("connectionGraph", &Phase::timers[Phase::_t_connectionGraph]);
 133 
 134   // 1. Populate Connection Graph (CG) with PointsTo nodes.
 135   ideal_nodes.map(C->live_nodes(), NULL);  // preallocate space
 136   // Initialize worklist
 137   if (C->root() != NULL) {
 138     ideal_nodes.push(C->root());
 139   }
 140   // Processed ideal nodes are unique on ideal_nodes list
 141   // but several ideal nodes are mapped to the phantom_obj.
 142   // To avoid duplicated entries on the following worklists
 143   // add the phantom_obj only once to them.
 144   ptnodes_worklist.append(phantom_obj);
 145   java_objects_worklist.append(phantom_obj);
 146   for( uint next = 0; next < ideal_nodes.size(); ++next ) {
 147     Node* n = ideal_nodes.at(next);










 148     // Create PointsTo nodes and add them to Connection Graph. Called
 149     // only once per ideal node since ideal_nodes is Unique_Node list.
 150     add_node_to_connection_graph(n, &delayed_worklist);
 151     PointsToNode* ptn = ptnode_adr(n->_idx);
 152     if (ptn != NULL && ptn != phantom_obj) {
 153       ptnodes_worklist.append(ptn);
 154       if (ptn->is_JavaObject()) {
 155         java_objects_worklist.append(ptn->as_JavaObject());
 156         if ((n->is_Allocate() || n->is_CallStaticJava()) &&
 157             (ptn->escape_state() < PointsToNode::GlobalEscape)) {
 158           // Only allocations and java static calls results are interesting.
 159           non_escaped_allocs_worklist.append(ptn->as_JavaObject());
 160         }
 161       } else if (ptn->is_Field() && ptn->as_Field()->is_oop()) {
 162         oop_fields_worklist.append(ptn->as_Field());
 163       }
 164     }
 165     // Collect some interesting nodes for futher use.
 166     switch (n->Opcode()) {
 167       case Op_MergeMem:

 387   return false;
 388 }
 389 
 390 // Returns true if at least one of the arguments to the call is an object
 391 // that does not escape globally.
 392 bool ConnectionGraph::has_arg_escape(CallJavaNode* call) {
 393   if (call->method() != NULL) {
 394     uint max_idx = TypeFunc::Parms + call->method()->arg_size();
 395     for (uint idx = TypeFunc::Parms; idx < max_idx; idx++) {
 396       Node* p = call->in(idx);
 397       if (not_global_escape(p)) {
 398         return true;
 399       }
 400     }
 401   } else {
 402     const char* name = call->as_CallStaticJava()->_name;
 403     assert(name != NULL, "no name");
 404     // no arg escapes through uncommon traps
 405     if (strcmp(name, "uncommon_trap") != 0) {
 406       // process_call_arguments() assumes that all arguments escape globally
 407       const TypeTuple* d = call->tf()->domain();
 408       for (uint i = TypeFunc::Parms; i < d->cnt(); i++) {
 409         const Type* at = d->field_at(i);
 410         if (at->isa_oopptr() != NULL) {
 411           return true;
 412         }
 413       }
 414     }
 415   }
 416   return false;
 417 }
 418 
 419 
 420 
 421 // Utility function for nodes that load an object
 422 void ConnectionGraph::add_objload_to_connection_graph(Node *n, Unique_Node_List *delayed_worklist) {
 423   // Using isa_ptr() instead of isa_oopptr() for LoadP and Phi because
 424   // ThreadLocal has RawPtr type.
 425   const Type* t = _igvn->type(n);
 426   if (t->make_ptr() != NULL) {
 427     Node* adr = n->in(MemNode::Address);

 461       // first IGVN optimization when escape information is still available.
 462       record_for_optimizer(n);
 463     } else if (n->is_Allocate()) {
 464       add_call_node(n->as_Call());
 465       record_for_optimizer(n);
 466     } else {
 467       if (n->is_CallStaticJava()) {
 468         const char* name = n->as_CallStaticJava()->_name;
 469         if (name != NULL && strcmp(name, "uncommon_trap") == 0) {
 470           return; // Skip uncommon traps
 471         }
 472       }
 473       // Don't mark as processed since call's arguments have to be processed.
 474       delayed_worklist->push(n);
 475       // Check if a call returns an object.
 476       if ((n->as_Call()->returns_pointer() &&
 477            n->as_Call()->proj_out_or_null(TypeFunc::Parms) != NULL) ||
 478           (n->is_CallStaticJava() &&
 479            n->as_CallStaticJava()->is_boxing_method())) {
 480         add_call_node(n->as_Call());











 481       }
 482     }
 483     return;
 484   }
 485   // Put this check here to process call arguments since some call nodes
 486   // point to phantom_obj.
 487   if (n_ptn == phantom_obj || n_ptn == null_obj) {
 488     return; // Skip predefined nodes.
 489   }
 490   switch (opcode) {
 491     case Op_AddP: {
 492       Node* base = get_addp_base(n);
 493       PointsToNode* ptn_base = ptnode_adr(base->_idx);
 494       // Field nodes are created for all field types. They are used in
 495       // adjust_scalar_replaceable_state() and split_unique_types().
 496       // Note, non-oop fields will have only base edges in Connection
 497       // Graph because such fields are not used for oop loads and stores.
 498       int offset = address_offset(n, igvn);
 499       add_field(n, PointsToNode::NoEscape, offset);
 500       if (ptn_base == NULL) {
 501         delayed_worklist->push(n); // Process it later.
 502       } else {
 503         n_ptn = ptnode_adr(n_idx);
 504         add_base(n_ptn->as_Field(), ptn_base);
 505       }
 506       break;
 507     }
 508     case Op_CastX2P: {
 509       map_ideal_node(n, phantom_obj);
 510       break;
 511     }

 512     case Op_CastPP:
 513     case Op_CheckCastPP:
 514     case Op_EncodeP:
 515     case Op_DecodeN:
 516     case Op_EncodePKlass:
 517     case Op_DecodeNKlass: {
 518       add_local_var_and_edge(n, PointsToNode::NoEscape, n->in(1), delayed_worklist);
 519       break;
 520     }
 521     case Op_CMoveP: {
 522       add_local_var(n, PointsToNode::NoEscape);
 523       // Do not add edges during first iteration because some could be
 524       // not defined yet.
 525       delayed_worklist->push(n);
 526       break;
 527     }
 528     case Op_ConP:
 529     case Op_ConN:
 530     case Op_ConNKlass: {
 531       // assume all oop constants globally escape except for null

 563     case Op_PartialSubtypeCheck: {
 564       // Produces Null or notNull and is used in only in CmpP so
 565       // phantom_obj could be used.
 566       map_ideal_node(n, phantom_obj); // Result is unknown
 567       break;
 568     }
 569     case Op_Phi: {
 570       // Using isa_ptr() instead of isa_oopptr() for LoadP and Phi because
 571       // ThreadLocal has RawPtr type.
 572       const Type* t = n->as_Phi()->type();
 573       if (t->make_ptr() != NULL) {
 574         add_local_var(n, PointsToNode::NoEscape);
 575         // Do not add edges during first iteration because some could be
 576         // not defined yet.
 577         delayed_worklist->push(n);
 578       }
 579       break;
 580     }
 581     case Op_Proj: {
 582       // we are only interested in the oop result projection from a call
 583       if (n->as_Proj()->_con == TypeFunc::Parms && n->in(0)->is_Call() &&
 584           n->in(0)->as_Call()->returns_pointer()) {


 585         add_local_var_and_edge(n, PointsToNode::NoEscape, n->in(0), delayed_worklist);
 586       }
 587       break;
 588     }
 589     case Op_Rethrow: // Exception object escapes
 590     case Op_Return: {
 591       if (n->req() > TypeFunc::Parms &&
 592           igvn->type(n->in(TypeFunc::Parms))->isa_oopptr()) {
 593         // Treat Return value as LocalVar with GlobalEscape escape state.
 594         add_local_var_and_edge(n, PointsToNode::GlobalEscape, n->in(TypeFunc::Parms), delayed_worklist);
 595       }
 596       break;
 597     }
 598     case Op_CompareAndExchangeP:
 599     case Op_CompareAndExchangeN:
 600     case Op_GetAndSetP:
 601     case Op_GetAndSetN: {
 602       add_objload_to_connection_graph(n, delayed_worklist);
 603       // fall-through
 604     }

 647   if (n->is_Call()) {
 648     process_call_arguments(n->as_Call());
 649     return;
 650   }
 651   assert(n->is_Store() || n->is_LoadStore() ||
 652          (n_ptn != NULL) && (n_ptn->ideal_node() != NULL),
 653          "node should be registered already");
 654   int opcode = n->Opcode();
 655   bool gc_handled = BarrierSet::barrier_set()->barrier_set_c2()->escape_add_final_edges(this, _igvn, n, opcode);
 656   if (gc_handled) {
 657     return; // Ignore node if already handled by GC.
 658   }
 659   switch (opcode) {
 660     case Op_AddP: {
 661       Node* base = get_addp_base(n);
 662       PointsToNode* ptn_base = ptnode_adr(base->_idx);
 663       assert(ptn_base != NULL, "field's base should be registered");
 664       add_base(n_ptn->as_Field(), ptn_base);
 665       break;
 666     }

 667     case Op_CastPP:
 668     case Op_CheckCastPP:
 669     case Op_EncodeP:
 670     case Op_DecodeN:
 671     case Op_EncodePKlass:
 672     case Op_DecodeNKlass: {
 673       add_local_var_and_edge(n, PointsToNode::NoEscape, n->in(1), NULL);
 674       break;
 675     }
 676     case Op_CMoveP: {
 677       for (uint i = CMoveNode::IfFalse; i < n->req(); i++) {
 678         Node* in = n->in(i);
 679         if (in == NULL) {
 680           continue;  // ignore NULL
 681         }
 682         Node* uncast_in = in->uncast();
 683         if (uncast_in->is_top() || uncast_in == n) {
 684           continue;  // ignore top or inputs which go back this node
 685         }
 686         PointsToNode* ptn = ptnode_adr(in->_idx);

 702       // Using isa_ptr() instead of isa_oopptr() for LoadP and Phi because
 703       // ThreadLocal has RawPtr type.
 704       assert(n->as_Phi()->type()->make_ptr() != NULL, "Unexpected node type");
 705       for (uint i = 1; i < n->req(); i++) {
 706         Node* in = n->in(i);
 707         if (in == NULL) {
 708           continue;  // ignore NULL
 709         }
 710         Node* uncast_in = in->uncast();
 711         if (uncast_in->is_top() || uncast_in == n) {
 712           continue;  // ignore top or inputs which go back this node
 713         }
 714         PointsToNode* ptn = ptnode_adr(in->_idx);
 715         assert(ptn != NULL, "node should be registered");
 716         add_edge(n_ptn, ptn);
 717       }
 718       break;
 719     }
 720     case Op_Proj: {
 721       // we are only interested in the oop result projection from a call
 722       assert(n->as_Proj()->_con == TypeFunc::Parms && n->in(0)->is_Call() &&
 723              n->in(0)->as_Call()->returns_pointer(), "Unexpected node type");
 724       add_local_var_and_edge(n, PointsToNode::NoEscape, n->in(0), NULL);
 725       break;
 726     }
 727     case Op_Rethrow: // Exception object escapes
 728     case Op_Return: {
 729       assert(n->req() > TypeFunc::Parms && _igvn->type(n->in(TypeFunc::Parms))->isa_oopptr(),
 730              "Unexpected node type");
 731       // Treat Return value as LocalVar with GlobalEscape escape state.
 732       add_local_var_and_edge(n, PointsToNode::GlobalEscape, n->in(TypeFunc::Parms), NULL);
 733       break;
 734     }
 735     case Op_CompareAndExchangeP:
 736     case Op_CompareAndExchangeN:
 737     case Op_GetAndSetP:
 738     case Op_GetAndSetN:{
 739       assert(_igvn->type(n)->make_ptr() != NULL, "Unexpected node type");
 740       add_local_var_and_edge(n, PointsToNode::NoEscape, n->in(MemNode::Address), NULL);
 741       // fall-through
 742     }
 743     case Op_CompareAndSwapP:

 859     PointsToNode* ptn = ptnode_adr(val->_idx);
 860     assert(ptn != NULL, "node should be registered");
 861     set_escape_state(ptn, PointsToNode::GlobalEscape);
 862     // Add edge to object for unsafe access with offset.
 863     PointsToNode* adr_ptn = ptnode_adr(adr->_idx);
 864     assert(adr_ptn != NULL, "node should be registered");
 865     if (adr_ptn->is_Field()) {
 866       assert(adr_ptn->as_Field()->is_oop(), "should be oop field");
 867       add_edge(adr_ptn, ptn);
 868     }
 869     return true;
 870   }
 871 #ifdef ASSERT
 872   n->dump(1);
 873   assert(false, "not unsafe");
 874 #endif
 875   return false;
 876 }
 877 
 878 void ConnectionGraph::add_call_node(CallNode* call) {
 879   assert(call->returns_pointer(), "only for call which returns pointer");
 880   uint call_idx = call->_idx;
 881   if (call->is_Allocate()) {
 882     Node* k = call->in(AllocateNode::KlassNode);
 883     const TypeKlassPtr* kt = k->bottom_type()->isa_klassptr();
 884     assert(kt != NULL, "TypeKlassPtr  required.");
 885     ciKlass* cik = kt->klass();
 886     PointsToNode::EscapeState es = PointsToNode::NoEscape;
 887     bool scalar_replaceable = true;
 888     if (call->is_AllocateArray()) {
 889       if (!cik->is_array_klass()) { // StressReflectiveCode
 890         es = PointsToNode::GlobalEscape;
 891       } else {
 892         int length = call->in(AllocateNode::ALength)->find_int_con(-1);
 893         if (length < 0 || length > EliminateAllocationArraySizeLimit) {
 894           // Not scalar replaceable if the length is not constant or too big.
 895           scalar_replaceable = false;
 896         }
 897       }
 898     } else {  // Allocate instance
 899       if (cik->is_subclass_of(_compile->env()->Thread_klass()) ||

 924     //
 925     //    - all oop arguments are escaping globally;
 926     //
 927     // 2. CallStaticJavaNode (execute bytecode analysis if possible):
 928     //
 929     //    - the same as CallDynamicJavaNode if can't do bytecode analysis;
 930     //
 931     //    - mapped to GlobalEscape JavaObject node if unknown oop is returned;
 932     //    - mapped to NoEscape JavaObject node if non-escaping object allocated
 933     //      during call is returned;
 934     //    - mapped to ArgEscape LocalVar node pointed to object arguments
 935     //      which are returned and does not escape during call;
 936     //
 937     //    - oop arguments escaping status is defined by bytecode analysis;
 938     //
 939     // For a static call, we know exactly what method is being called.
 940     // Use bytecode estimator to record whether the call's return value escapes.
 941     ciMethod* meth = call->as_CallJava()->method();
 942     if (meth == NULL) {
 943       const char* name = call->as_CallStaticJava()->_name;
 944       assert(strncmp(name, "_multianewarray", 15) == 0, "TODO: add failed case check");

 945       // Returns a newly allocated non-escaped object.
 946       add_java_object(call, PointsToNode::NoEscape);
 947       ptnode_adr(call_idx)->set_scalar_replaceable(false);
 948     } else if (meth->is_boxing_method()) {
 949       // Returns boxing object
 950       PointsToNode::EscapeState es;
 951       vmIntrinsics::ID intr = meth->intrinsic_id();
 952       if (intr == vmIntrinsics::_floatValue || intr == vmIntrinsics::_doubleValue) {
 953         // It does not escape if object is always allocated.
 954         es = PointsToNode::NoEscape;
 955       } else {
 956         // It escapes globally if object could be loaded from cache.
 957         es = PointsToNode::GlobalEscape;
 958       }
 959       add_java_object(call, es);
 960     } else {
 961       BCEscapeAnalyzer* call_analyzer = meth->get_bcea();
 962       call_analyzer->copy_dependencies(_compile->dependencies());
 963       if (call_analyzer->is_return_allocated()) {
 964         // Returns a newly allocated non-escaped object, simply
 965         // update dependency information.
 966         // Mark it as NoEscape so that objects referenced by
 967         // it's fields will be marked as NoEscape at least.
 968         add_java_object(call, PointsToNode::NoEscape);
 969         ptnode_adr(call_idx)->set_scalar_replaceable(false);
 970       } else {
 971         // Determine whether any arguments are returned.
 972         const TypeTuple* d = call->tf()->domain();
 973         bool ret_arg = false;
 974         for (uint i = TypeFunc::Parms; i < d->cnt(); i++) {
 975           if (d->field_at(i)->isa_ptr() != NULL &&
 976               call_analyzer->is_arg_returned(i - TypeFunc::Parms)) {
 977             ret_arg = true;
 978             break;
 979           }
 980         }
 981         if (ret_arg) {
 982           add_local_var(call, PointsToNode::ArgEscape);
 983         } else {
 984           // Returns unknown object.
 985           map_ideal_node(call, phantom_obj);
 986         }
 987       }
 988     }
 989   } else {
 990     // An other type of call, assume the worst case:
 991     // returned value is unknown and globally escapes.
 992     assert(call->Opcode() == Op_CallDynamicJava, "add failed case check");

1000 #ifdef ASSERT
1001     case Op_Allocate:
1002     case Op_AllocateArray:
1003     case Op_Lock:
1004     case Op_Unlock:
1005       assert(false, "should be done already");
1006       break;
1007 #endif
1008     case Op_ArrayCopy:
1009     case Op_CallLeafNoFP:
1010       // Most array copies are ArrayCopy nodes at this point but there
1011       // are still a few direct calls to the copy subroutines (See
1012       // PhaseStringOpts::copy_string())
1013       is_arraycopy = (call->Opcode() == Op_ArrayCopy) ||
1014         call->as_CallLeaf()->is_call_to_arraycopystub();
1015       // fall through
1016     case Op_CallLeafVector:
1017     case Op_CallLeaf: {
1018       // Stub calls, objects do not escape but they are not scale replaceable.
1019       // Adjust escape state for outgoing arguments.
1020       const TypeTuple * d = call->tf()->domain();
1021       bool src_has_oops = false;
1022       for (uint i = TypeFunc::Parms; i < d->cnt(); i++) {
1023         const Type* at = d->field_at(i);
1024         Node *arg = call->in(i);
1025         if (arg == NULL) {
1026           continue;
1027         }
1028         const Type *aat = _igvn->type(arg);
1029         if (arg->is_top() || !at->isa_ptr() || !aat->isa_ptr()) {
1030           continue;
1031         }
1032         if (arg->is_AddP()) {
1033           //
1034           // The inline_native_clone() case when the arraycopy stub is called
1035           // after the allocation before Initialize and CheckCastPP nodes.
1036           // Or normal arraycopy for object arrays case.
1037           //
1038           // Set AddP's base (Allocate) as not scalar replaceable since
1039           // pointer to the base (with offset) is passed as argument.
1040           //
1041           arg = get_addp_base(arg);
1042         }
1043         PointsToNode* arg_ptn = ptnode_adr(arg->_idx);
1044         assert(arg_ptn != NULL, "should be registered");
1045         PointsToNode::EscapeState arg_esc = arg_ptn->escape_state();
1046         if (is_arraycopy || arg_esc < PointsToNode::ArgEscape) {
1047           assert(aat == Type::TOP || aat == TypePtr::NULL_PTR ||
1048                  aat->isa_ptr() != NULL, "expecting an Ptr");
1049           bool arg_has_oops = aat->isa_oopptr() &&
1050                               (aat->isa_oopptr()->klass() == NULL || aat->isa_instptr() ||
1051                                (aat->isa_aryptr() && aat->isa_aryptr()->klass()->is_obj_array_klass()));



1052           if (i == TypeFunc::Parms) {
1053             src_has_oops = arg_has_oops;
1054           }
1055           //
1056           // src or dst could be j.l.Object when other is basic type array:
1057           //
1058           //   arraycopy(char[],0,Object*,0,size);
1059           //   arraycopy(Object*,0,char[],0,size);
1060           //
1061           // Don't add edges in such cases.
1062           //
1063           bool arg_is_arraycopy_dest = src_has_oops && is_arraycopy &&
1064                                        arg_has_oops && (i > TypeFunc::Parms);
1065 #ifdef ASSERT
1066           if (!(is_arraycopy ||
1067                 BarrierSet::barrier_set()->barrier_set_c2()->is_gc_barrier_node(call) ||
1068                 (call->as_CallLeaf()->_name != NULL &&
1069                  (strcmp(call->as_CallLeaf()->_name, "updateBytesCRC32") == 0 ||
1070                   strcmp(call->as_CallLeaf()->_name, "updateBytesCRC32C") == 0 ||
1071                   strcmp(call->as_CallLeaf()->_name, "updateBytesAdler32") == 0 ||

1078                   strcmp(call->as_CallLeaf()->_name, "counterMode_AESCrypt") == 0 ||
1079                   strcmp(call->as_CallLeaf()->_name, "galoisCounterMode_AESCrypt") == 0 ||
1080                   strcmp(call->as_CallLeaf()->_name, "ghash_processBlocks") == 0 ||
1081                   strcmp(call->as_CallLeaf()->_name, "encodeBlock") == 0 ||
1082                   strcmp(call->as_CallLeaf()->_name, "decodeBlock") == 0 ||
1083                   strcmp(call->as_CallLeaf()->_name, "md5_implCompress") == 0 ||
1084                   strcmp(call->as_CallLeaf()->_name, "md5_implCompressMB") == 0 ||
1085                   strcmp(call->as_CallLeaf()->_name, "sha1_implCompress") == 0 ||
1086                   strcmp(call->as_CallLeaf()->_name, "sha1_implCompressMB") == 0 ||
1087                   strcmp(call->as_CallLeaf()->_name, "sha256_implCompress") == 0 ||
1088                   strcmp(call->as_CallLeaf()->_name, "sha256_implCompressMB") == 0 ||
1089                   strcmp(call->as_CallLeaf()->_name, "sha512_implCompress") == 0 ||
1090                   strcmp(call->as_CallLeaf()->_name, "sha512_implCompressMB") == 0 ||
1091                   strcmp(call->as_CallLeaf()->_name, "sha3_implCompress") == 0 ||
1092                   strcmp(call->as_CallLeaf()->_name, "sha3_implCompressMB") == 0 ||
1093                   strcmp(call->as_CallLeaf()->_name, "multiplyToLen") == 0 ||
1094                   strcmp(call->as_CallLeaf()->_name, "squareToLen") == 0 ||
1095                   strcmp(call->as_CallLeaf()->_name, "mulAdd") == 0 ||
1096                   strcmp(call->as_CallLeaf()->_name, "montgomery_multiply") == 0 ||
1097                   strcmp(call->as_CallLeaf()->_name, "montgomery_square") == 0 ||



1098                   strcmp(call->as_CallLeaf()->_name, "bigIntegerRightShiftWorker") == 0 ||
1099                   strcmp(call->as_CallLeaf()->_name, "bigIntegerLeftShiftWorker") == 0 ||
1100                   strcmp(call->as_CallLeaf()->_name, "vectorizedMismatch") == 0 ||
1101                   strcmp(call->as_CallLeaf()->_name, "get_class_id_intrinsic") == 0)
1102                  ))) {
1103             call->dump();
1104             fatal("EA unexpected CallLeaf %s", call->as_CallLeaf()->_name);
1105           }
1106 #endif
1107           // Always process arraycopy's destination object since
1108           // we need to add all possible edges to references in
1109           // source object.
1110           if (arg_esc >= PointsToNode::ArgEscape &&
1111               !arg_is_arraycopy_dest) {
1112             continue;
1113           }
1114           PointsToNode::EscapeState es = PointsToNode::ArgEscape;
1115           if (call->is_ArrayCopy()) {
1116             ArrayCopyNode* ac = call->as_ArrayCopy();
1117             if (ac->is_clonebasic() ||

1140           }
1141         }
1142       }
1143       break;
1144     }
1145     case Op_CallStaticJava: {
1146       // For a static call, we know exactly what method is being called.
1147       // Use bytecode estimator to record the call's escape affects
1148 #ifdef ASSERT
1149       const char* name = call->as_CallStaticJava()->_name;
1150       assert((name == NULL || strcmp(name, "uncommon_trap") != 0), "normal calls only");
1151 #endif
1152       ciMethod* meth = call->as_CallJava()->method();
1153       if ((meth != NULL) && meth->is_boxing_method()) {
1154         break; // Boxing methods do not modify any oops.
1155       }
1156       BCEscapeAnalyzer* call_analyzer = (meth !=NULL) ? meth->get_bcea() : NULL;
1157       // fall-through if not a Java method or no analyzer information
1158       if (call_analyzer != NULL) {
1159         PointsToNode* call_ptn = ptnode_adr(call->_idx);
1160         const TypeTuple* d = call->tf()->domain();
1161         for (uint i = TypeFunc::Parms; i < d->cnt(); i++) {
1162           const Type* at = d->field_at(i);
1163           int k = i - TypeFunc::Parms;
1164           Node* arg = call->in(i);
1165           PointsToNode* arg_ptn = ptnode_adr(arg->_idx);
1166           if (at->isa_ptr() != NULL &&
1167               call_analyzer->is_arg_returned(k)) {
1168             // The call returns arguments.
1169             if (call_ptn != NULL) { // Is call's result used?
1170               assert(call_ptn->is_LocalVar(), "node should be registered");
1171               assert(arg_ptn != NULL, "node should be registered");
1172               add_edge(call_ptn, arg_ptn);
1173             }
1174           }
1175           if (at->isa_oopptr() != NULL &&
1176               arg_ptn->escape_state() < PointsToNode::GlobalEscape) {
1177             if (!call_analyzer->is_arg_stack(k)) {
1178               // The argument global escapes
1179               set_escape_state(arg_ptn, PointsToNode::GlobalEscape);
1180             } else {

1184                 set_fields_escape_state(arg_ptn, PointsToNode::GlobalEscape);
1185               }
1186             }
1187           }
1188         }
1189         if (call_ptn != NULL && call_ptn->is_LocalVar()) {
1190           // The call returns arguments.
1191           assert(call_ptn->edge_count() > 0, "sanity");
1192           if (!call_analyzer->is_return_local()) {
1193             // Returns also unknown object.
1194             add_edge(call_ptn, phantom_obj);
1195           }
1196         }
1197         break;
1198       }
1199     }
1200     default: {
1201       // Fall-through here if not a Java method or no analyzer information
1202       // or some other type of call, assume the worst case: all arguments
1203       // globally escape.
1204       const TypeTuple* d = call->tf()->domain();
1205       for (uint i = TypeFunc::Parms; i < d->cnt(); i++) {
1206         const Type* at = d->field_at(i);
1207         if (at->isa_oopptr() != NULL) {
1208           Node* arg = call->in(i);
1209           if (arg->is_AddP()) {
1210             arg = get_addp_base(arg);
1211           }
1212           assert(ptnode_adr(arg->_idx) != NULL, "should be defined already");
1213           set_escape_state(ptnode_adr(arg->_idx), PointsToNode::GlobalEscape);
1214         }
1215       }
1216     }
1217   }
1218 }
1219 
1220 
1221 // Finish Graph construction.
1222 bool ConnectionGraph::complete_connection_graph(
1223                          GrowableArray<PointsToNode*>&   ptnodes_worklist,
1224                          GrowableArray<JavaObjectNode*>& non_escaped_allocs_worklist,

1603     PointsToNode* base = i.get();
1604     if (base->is_JavaObject()) {
1605       // Skip Allocate's fields which will be processed later.
1606       if (base->ideal_node()->is_Allocate()) {
1607         return 0;
1608       }
1609       assert(base == null_obj, "only NULL ptr base expected here");
1610     }
1611   }
1612   if (add_edge(field, phantom_obj)) {
1613     // New edge was added
1614     new_edges++;
1615     add_field_uses_to_worklist(field);
1616   }
1617   return new_edges;
1618 }
1619 
1620 // Find fields initializing values for allocations.
1621 int ConnectionGraph::find_init_values_phantom(JavaObjectNode* pta) {
1622   assert(pta->escape_state() == PointsToNode::NoEscape, "Not escaped Allocate nodes only");

1623   Node* alloc = pta->ideal_node();
1624 
1625   // Do nothing for Allocate nodes since its fields values are
1626   // "known" unless they are initialized by arraycopy/clone.
1627   if (alloc->is_Allocate() && !pta->arraycopy_dst()) {
1628     return 0;







1629   }
1630   assert(pta->arraycopy_dst() || alloc->as_CallStaticJava(), "sanity");

1631 #ifdef ASSERT
1632   if (!pta->arraycopy_dst() && alloc->as_CallStaticJava()->method() == NULL) {
1633     const char* name = alloc->as_CallStaticJava()->_name;
1634     assert(strncmp(name, "_multianewarray", 15) == 0, "sanity");

1635   }
1636 #endif
1637   // Non-escaped allocation returned from Java or runtime call have unknown values in fields.
1638   int new_edges = 0;
1639   for (EdgeIterator i(pta); i.has_next(); i.next()) {
1640     PointsToNode* field = i.get();
1641     if (field->is_Field() && field->as_Field()->is_oop()) {
1642       if (add_edge(field, phantom_obj)) {
1643         // New edge was added
1644         new_edges++;
1645         add_field_uses_to_worklist(field->as_Field());
1646       }
1647     }
1648   }
1649   return new_edges;
1650 }
1651 
1652 // Find fields initializing values for allocations.
1653 int ConnectionGraph::find_init_values_null(JavaObjectNode* pta, PhaseTransform* phase) {
1654   assert(pta->escape_state() == PointsToNode::NoEscape, "Not escaped Allocate nodes only");
1655   Node* alloc = pta->ideal_node();
1656   // Do nothing for Call nodes since its fields values are unknown.
1657   if (!alloc->is_Allocate()) {
1658     return 0;
1659   }
1660   InitializeNode* ini = alloc->as_Allocate()->initialization();
1661   bool visited_bottom_offset = false;
1662   GrowableArray<int> offsets_worklist;
1663   int new_edges = 0;
1664 
1665   // Check if an oop field's initializing value is recorded and add
1666   // a corresponding NULL if field's value if it is not recorded.
1667   // Connection Graph does not record a default initialization by NULL
1668   // captured by Initialize node.
1669   //
1670   for (EdgeIterator i(pta); i.has_next(); i.next()) {
1671     PointsToNode* field = i.get(); // Field (AddP)
1672     if (!field->is_Field() || !field->as_Field()->is_oop()) {
1673       continue; // Not oop field
1674     }
1675     int offset = field->as_Field()->offset();
1676     if (offset == Type::OffsetBot) {
1677       if (!visited_bottom_offset) {

1723               } else {
1724                 if (!val->is_LocalVar() || (val->edge_count() == 0)) {
1725                   tty->print_cr("----------init store has invalid value -----");
1726                   store->dump();
1727                   val->dump();
1728                   assert(val->is_LocalVar() && (val->edge_count() > 0), "should be processed already");
1729                 }
1730                 for (EdgeIterator j(val); j.has_next(); j.next()) {
1731                   PointsToNode* obj = j.get();
1732                   if (obj->is_JavaObject()) {
1733                     if (!field->points_to(obj->as_JavaObject())) {
1734                       missed_obj = obj;
1735                       break;
1736                     }
1737                   }
1738                 }
1739               }
1740               if (missed_obj != NULL) {
1741                 tty->print_cr("----------field---------------------------------");
1742                 field->dump();
1743                 tty->print_cr("----------missed referernce to object-----------");
1744                 missed_obj->dump();
1745                 tty->print_cr("----------object referernced by init store -----");
1746                 store->dump();
1747                 val->dump();
1748                 assert(!field->points_to(missed_obj->as_JavaObject()), "missed JavaObject reference");
1749               }
1750             }
1751 #endif
1752           } else {
1753             // There could be initializing stores which follow allocation.
1754             // For example, a volatile field store is not collected
1755             // by Initialize node.
1756             //
1757             // Need to check for dependent loads to separate such stores from
1758             // stores which follow loads. For now, add initial value NULL so
1759             // that compare pointers optimization works correctly.
1760           }
1761         }
1762         if (value == NULL) {
1763           // A field's initializing value was not recorded. Add NULL.
1764           if (add_edge(field, null_obj)) {
1765             // New edge was added

1794       }
1795       // 2. An object is not scalar replaceable if the field into which it is
1796       // stored has multiple bases one of which is null.
1797       if (field->base_count() > 1) {
1798         for (BaseIterator i(field); i.has_next(); i.next()) {
1799           PointsToNode* base = i.get();
1800           if (base == null_obj) {
1801             jobj->set_scalar_replaceable(false);
1802             return;
1803           }
1804         }
1805       }
1806     }
1807     assert(use->is_Field() || use->is_LocalVar(), "sanity");
1808     // 3. An object is not scalar replaceable if it is merged with other objects.
1809     for (EdgeIterator j(use); j.has_next(); j.next()) {
1810       PointsToNode* ptn = j.get();
1811       if (ptn->is_JavaObject() && ptn != jobj) {
1812         // Mark all objects.
1813         jobj->set_scalar_replaceable(false);
1814          ptn->set_scalar_replaceable(false);
1815       }
1816     }
1817     if (!jobj->scalar_replaceable()) {
1818       return;
1819     }
1820   }
1821 
1822   for (EdgeIterator j(jobj); j.has_next(); j.next()) {
1823     if (j.get()->is_Arraycopy()) {
1824       continue;
1825     }
1826 
1827     // Non-escaping object node should point only to field nodes.
1828     FieldNode* field = j.get()->as_Field();
1829     int offset = field->as_Field()->offset();
1830 
1831     // 4. An object is not scalar replaceable if it has a field with unknown
1832     // offset (array's element is accessed in loop).
1833     if (offset == Type::OffsetBot) {
1834       jobj->set_scalar_replaceable(false);

1957         assert(field->edge_count() > 0, "sanity");
1958       }
1959     }
1960   }
1961 }
1962 #endif
1963 
1964 // Optimize ideal graph.
1965 void ConnectionGraph::optimize_ideal_graph(GrowableArray<Node*>& ptr_cmp_worklist,
1966                                            GrowableArray<MemBarStoreStoreNode*>& storestore_worklist) {
1967   Compile* C = _compile;
1968   PhaseIterGVN* igvn = _igvn;
1969   if (EliminateLocks) {
1970     // Mark locks before changing ideal graph.
1971     int cnt = C->macro_count();
1972     for (int i = 0; i < cnt; i++) {
1973       Node *n = C->macro_node(i);
1974       if (n->is_AbstractLock()) { // Lock and Unlock nodes
1975         AbstractLockNode* alock = n->as_AbstractLock();
1976         if (!alock->is_non_esc_obj()) {
1977           if (not_global_escape(alock->obj_node())) {


1978             assert(!alock->is_eliminated() || alock->is_coarsened(), "sanity");
1979             // The lock could be marked eliminated by lock coarsening
1980             // code during first IGVN before EA. Replace coarsened flag
1981             // to eliminate all associated locks/unlocks.
1982 #ifdef ASSERT
1983             alock->log_lock_optimization(C, "eliminate_lock_set_non_esc3");
1984 #endif
1985             alock->set_non_esc_obj();
1986           }
1987         }
1988       }
1989     }
1990   }
1991 
1992   if (OptimizePtrCompare) {
1993     for (int i = 0; i < ptr_cmp_worklist.length(); i++) {
1994       Node *n = ptr_cmp_worklist.at(i);
1995       const TypeInt* tcmp = optimize_ptr_compare(n);
1996       if (tcmp->singleton()) {
1997         Node* cmp = igvn->makecon(tcmp);
1998 #ifndef PRODUCT
1999         if (PrintOptimizePtrCompare) {
2000           tty->print_cr("++++ Replaced: %d %s(%d,%d) --> %s", n->_idx, (n->Opcode() == Op_CmpP ? "CmpP" : "CmpN"), n->in(1)->_idx, n->in(2)->_idx, (tcmp == TypeInt::CC_EQ ? "EQ" : "NotEQ"));
2001           if (Verbose) {
2002             n->dump(1);
2003           }
2004         }
2005 #endif
2006         igvn->replace_node(n, cmp);
2007       }
2008     }
2009   }
2010 
2011   // For MemBarStoreStore nodes added in library_call.cpp, check
2012   // escape status of associated AllocateNode and optimize out
2013   // MemBarStoreStore node if the allocated object never escapes.
2014   for (int i = 0; i < storestore_worklist.length(); i++) {
2015     Node* storestore = storestore_worklist.at(i);
2016     Node* alloc = storestore->in(MemBarNode::Precedent)->in(0);
2017     if (alloc->is_Allocate() && not_global_escape(alloc)) {
2018       MemBarNode* mb = MemBarNode::make(C, Op_MemBarCPUOrder, Compile::AliasIdxBot);
2019       mb->init_req(TypeFunc::Memory,  storestore->in(TypeFunc::Memory));
2020       mb->init_req(TypeFunc::Control, storestore->in(TypeFunc::Control));
2021       igvn->register_new_node_with_optimizer(mb);
2022       igvn->replace_node(storestore, mb);





2023     }
2024   }
2025 }
2026 
2027 // Optimize objects compare.
2028 const TypeInt* ConnectionGraph::optimize_ptr_compare(Node* n) {
2029   assert(OptimizePtrCompare, "sanity");
2030   assert(n->Opcode() == Op_CmpN || n->Opcode() == Op_CmpP, "must be");
2031   const TypeInt* EQ = TypeInt::CC_EQ; // [0] == ZERO
2032   const TypeInt* NE = TypeInt::CC_GT; // [1] == ONE
2033   const TypeInt* UNKNOWN = TypeInt::CC;    // [-1, 0,1]
2034 
2035   PointsToNode* ptn1 = ptnode_adr(n->in(1)->_idx);
2036   PointsToNode* ptn2 = ptnode_adr(n->in(2)->_idx);
2037   JavaObjectNode* jobj1 = unique_java_object(n->in(1));
2038   JavaObjectNode* jobj2 = unique_java_object(n->in(2));
2039   assert(ptn1->is_JavaObject() || ptn1->is_LocalVar(), "sanity");
2040   assert(ptn2->is_JavaObject() || ptn2->is_LocalVar(), "sanity");
2041 
2042   // Check simple cases first.

2155   assert(!src->is_Field() && !dst->is_Field(), "only for JavaObject and LocalVar");
2156   assert((src != null_obj) && (dst != null_obj), "not for ConP NULL");
2157   PointsToNode* ptadr = _nodes.at(n->_idx);
2158   if (ptadr != NULL) {
2159     assert(ptadr->is_Arraycopy() && ptadr->ideal_node() == n, "sanity");
2160     return;
2161   }
2162   Compile* C = _compile;
2163   ptadr = new (C->comp_arena()) ArraycopyNode(this, n, es);
2164   map_ideal_node(n, ptadr);
2165   // Add edge from arraycopy node to source object.
2166   (void)add_edge(ptadr, src);
2167   src->set_arraycopy_src();
2168   // Add edge from destination object to arraycopy node.
2169   (void)add_edge(dst, ptadr);
2170   dst->set_arraycopy_dst();
2171 }
2172 
2173 bool ConnectionGraph::is_oop_field(Node* n, int offset, bool* unsafe) {
2174   const Type* adr_type = n->as_AddP()->bottom_type();

2175   BasicType bt = T_INT;
2176   if (offset == Type::OffsetBot) {
2177     // Check only oop fields.
2178     if (!adr_type->isa_aryptr() ||
2179         (adr_type->isa_aryptr()->klass() == NULL) ||
2180          adr_type->isa_aryptr()->klass()->is_obj_array_klass()) {
2181       // OffsetBot is used to reference array's element. Ignore first AddP.
2182       if (find_second_addp(n, n->in(AddPNode::Base)) == NULL) {
2183         bt = T_OBJECT;
2184       }
2185     }
2186   } else if (offset != oopDesc::klass_offset_in_bytes()) {
2187     if (adr_type->isa_instptr()) {
2188       ciField* field = _compile->alias_type(adr_type->isa_instptr())->field();
2189       if (field != NULL) {
2190         bt = field->layout_type();
2191       } else {
2192         // Check for unsafe oop field access
2193         if (n->has_out_with(Op_StoreP, Op_LoadP, Op_StoreN, Op_LoadN) ||
2194             n->has_out_with(Op_GetAndSetP, Op_GetAndSetN, Op_CompareAndExchangeP, Op_CompareAndExchangeN) ||
2195             n->has_out_with(Op_CompareAndSwapP, Op_CompareAndSwapN, Op_WeakCompareAndSwapP, Op_WeakCompareAndSwapN) ||
2196             BarrierSet::barrier_set()->barrier_set_c2()->escape_has_out_with_unsafe_object(n)) {
2197           bt = T_OBJECT;
2198           (*unsafe) = true;
2199         }
2200       }
2201     } else if (adr_type->isa_aryptr()) {
2202       if (offset == arrayOopDesc::length_offset_in_bytes()) {
2203         // Ignore array length load.
2204       } else if (find_second_addp(n, n->in(AddPNode::Base)) != NULL) {
2205         // Ignore first AddP.
2206       } else {
2207         const Type* elemtype = adr_type->isa_aryptr()->elem();
2208         bt = elemtype->array_element_basic_type();






2209       }
2210     } else if (adr_type->isa_rawptr() || adr_type->isa_klassptr()) {
2211       // Allocation initialization, ThreadLocal field access, unsafe access
2212       if (n->has_out_with(Op_StoreP, Op_LoadP, Op_StoreN, Op_LoadN) ||
2213           n->has_out_with(Op_GetAndSetP, Op_GetAndSetN, Op_CompareAndExchangeP, Op_CompareAndExchangeN) ||
2214           n->has_out_with(Op_CompareAndSwapP, Op_CompareAndSwapN, Op_WeakCompareAndSwapP, Op_WeakCompareAndSwapN) ||
2215           BarrierSet::barrier_set()->barrier_set_c2()->escape_has_out_with_unsafe_object(n)) {
2216         bt = T_OBJECT;
2217       }
2218     }
2219   }
2220   // Note: T_NARROWOOP is not classed as a real reference type
2221   return (is_reference_type(bt) || bt == T_NARROWOOP);
2222 }
2223 
2224 // Returns unique pointed java object or NULL.
2225 JavaObjectNode* ConnectionGraph::unique_java_object(Node *n) {
2226   assert(!_collecting, "should not call when constructed graph");
2227   // If the node was created after the escape computation we can't answer.
2228   uint idx = n->_idx;

2372             return true;
2373           }
2374         }
2375       }
2376     }
2377   }
2378   return false;
2379 }
2380 
2381 int ConnectionGraph::address_offset(Node* adr, PhaseTransform *phase) {
2382   const Type *adr_type = phase->type(adr);
2383   if (adr->is_AddP() && adr_type->isa_oopptr() == NULL && is_captured_store_address(adr)) {
2384     // We are computing a raw address for a store captured by an Initialize
2385     // compute an appropriate address type. AddP cases #3 and #5 (see below).
2386     int offs = (int)phase->find_intptr_t_con(adr->in(AddPNode::Offset), Type::OffsetBot);
2387     assert(offs != Type::OffsetBot ||
2388            adr->in(AddPNode::Address)->in(0)->is_AllocateArray(),
2389            "offset must be a constant or it is initialization of array");
2390     return offs;
2391   }
2392   const TypePtr *t_ptr = adr_type->isa_ptr();
2393   assert(t_ptr != NULL, "must be a pointer type");
2394   return t_ptr->offset();
2395 }
2396 
2397 Node* ConnectionGraph::get_addp_base(Node *addp) {
2398   assert(addp->is_AddP(), "must be AddP");
2399   //
2400   // AddP cases for Base and Address inputs:
2401   // case #1. Direct object's field reference:
2402   //     Allocate
2403   //       |
2404   //     Proj #5 ( oop result )
2405   //       |
2406   //     CheckCastPP (cast to instance type)
2407   //      | |
2408   //     AddP  ( base == address )
2409   //
2410   // case #2. Indirect object's field reference:
2411   //      Phi
2412   //       |
2413   //     CastPP (cast to instance type)
2414   //      | |

2528   }
2529   return NULL;
2530 }
2531 
2532 //
2533 // Adjust the type and inputs of an AddP which computes the
2534 // address of a field of an instance
2535 //
2536 bool ConnectionGraph::split_AddP(Node *addp, Node *base) {
2537   PhaseGVN* igvn = _igvn;
2538   const TypeOopPtr *base_t = igvn->type(base)->isa_oopptr();
2539   assert(base_t != NULL && base_t->is_known_instance(), "expecting instance oopptr");
2540   const TypeOopPtr *t = igvn->type(addp)->isa_oopptr();
2541   if (t == NULL) {
2542     // We are computing a raw address for a store captured by an Initialize
2543     // compute an appropriate address type (cases #3 and #5).
2544     assert(igvn->type(addp) == TypeRawPtr::NOTNULL, "must be raw pointer");
2545     assert(addp->in(AddPNode::Address)->is_Proj(), "base of raw address must be result projection from allocation");
2546     intptr_t offs = (int)igvn->find_intptr_t_con(addp->in(AddPNode::Offset), Type::OffsetBot);
2547     assert(offs != Type::OffsetBot, "offset must be a constant");
2548     t = base_t->add_offset(offs)->is_oopptr();







2549   }
2550   int inst_id =  base_t->instance_id();
2551   assert(!t->is_known_instance() || t->instance_id() == inst_id,
2552                              "old type must be non-instance or match new type");
2553 
2554   // The type 't' could be subclass of 'base_t'.
2555   // As result t->offset() could be large then base_t's size and it will
2556   // cause the failure in add_offset() with narrow oops since TypeOopPtr()
2557   // constructor verifies correctness of the offset.
2558   //
2559   // It could happened on subclass's branch (from the type profiling
2560   // inlining) which was not eliminated during parsing since the exactness
2561   // of the allocation type was not propagated to the subclass type check.
2562   //
2563   // Or the type 't' could be not related to 'base_t' at all.
2564   // It could happened when CHA type is different from MDO type on a dead path
2565   // (for example, from instanceof check) which is not collapsed during parsing.
2566   //
2567   // Do nothing for such AddP node and don't process its users since
2568   // this code branch will go away.
2569   //
2570   if (!t->is_known_instance() &&
2571       !base_t->klass()->is_subtype_of(t->klass())) {
2572      return false; // bail out
2573   }
2574   const TypeOopPtr *tinst = base_t->add_offset(t->offset())->is_oopptr();











2575   // Do NOT remove the next line: ensure a new alias index is allocated
2576   // for the instance type. Note: C++ will not remove it since the call
2577   // has side effect.
2578   int alias_idx = _compile->get_alias_index(tinst);
2579   igvn->set_type(addp, tinst);
2580   // record the allocation in the node map
2581   set_map(addp, get_map(base->_idx));
2582   // Set addp's Base and Address to 'base'.
2583   Node *abase = addp->in(AddPNode::Base);
2584   Node *adr   = addp->in(AddPNode::Address);
2585   if (adr->is_Proj() && adr->in(0)->is_Allocate() &&
2586       adr->in(0)->_idx == (uint)inst_id) {
2587     // Skip AddP cases #3 and #5.
2588   } else {
2589     assert(!abase->is_top(), "sanity"); // AddP case #3
2590     if (abase != base) {
2591       igvn->hash_delete(addp);
2592       addp->set_req(AddPNode::Base, base);
2593       if (abase == adr) {
2594         addp->set_req(AddPNode::Address, base);

3236         ptnode_adr(n->_idx)->dump();
3237         assert(jobj != NULL && jobj != phantom_obj, "escaped allocation");
3238 #endif
3239         _compile->record_failure(C2Compiler::retry_no_escape_analysis());
3240         return;
3241       } else {
3242         Node *val = get_map(jobj->idx());   // CheckCastPP node
3243         TypeNode *tn = n->as_Type();
3244         const TypeOopPtr* tinst = igvn->type(val)->isa_oopptr();
3245         assert(tinst != NULL && tinst->is_known_instance() &&
3246                tinst->instance_id() == jobj->idx() , "instance type expected.");
3247 
3248         const Type *tn_type = igvn->type(tn);
3249         const TypeOopPtr *tn_t;
3250         if (tn_type->isa_narrowoop()) {
3251           tn_t = tn_type->make_ptr()->isa_oopptr();
3252         } else {
3253           tn_t = tn_type->isa_oopptr();
3254         }
3255         if (tn_t != NULL && tinst->klass()->is_subtype_of(tn_t->klass())) {







3256           if (tn_type->isa_narrowoop()) {
3257             tn_type = tinst->make_narrowoop();
3258           } else {
3259             tn_type = tinst;
3260           }
3261           igvn->hash_delete(tn);
3262           igvn->set_type(tn, tn_type);
3263           tn->set_type(tn_type);
3264           igvn->hash_insert(tn);
3265           record_for_optimizer(n);
3266         } else {
3267           assert(tn_type == TypePtr::NULL_PTR ||
3268                  tn_t != NULL && !tinst->klass()->is_subtype_of(tn_t->klass()),
3269                  "unexpected type");
3270           continue; // Skip dead path with different type
3271         }
3272       }
3273     } else {
3274       debug_only(n->dump();)
3275       assert(false, "EA: unexpected node");
3276       continue;
3277     }
3278     // push allocation's users on appropriate worklist
3279     for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
3280       Node *use = n->fast_out(i);
3281       if(use->is_Mem() && use->in(MemNode::Address) == n) {
3282         // Load/store to instance's field
3283         memnode_worklist.append_if_missing(use);
3284       } else if (use->is_MemBar()) {
3285         if (use->in(TypeFunc::Memory) == n) { // Ignore precedent edge
3286           memnode_worklist.append_if_missing(use);
3287         }
3288       } else if (use->is_AddP() && use->outcnt() > 0) { // No dead nodes
3289         Node* addp2 = find_second_addp(use, n);
3290         if (addp2 != NULL) {
3291           alloc_worklist.append_if_missing(addp2);
3292         }
3293         alloc_worklist.append_if_missing(use);
3294       } else if (use->is_Phi() ||
3295                  use->is_CheckCastPP() ||
3296                  use->is_EncodeNarrowPtr() ||
3297                  use->is_DecodeNarrowPtr() ||
3298                  (use->is_ConstraintCast() && use->Opcode() == Op_CastPP)) {
3299         alloc_worklist.append_if_missing(use);
3300 #ifdef ASSERT
3301       } else if (use->is_Mem()) {
3302         assert(use->in(MemNode::Address) != n, "EA: missing allocation reference path");
3303       } else if (use->is_MergeMem()) {
3304         assert(mergemem_worklist.contains(use->as_MergeMem()), "EA: missing MergeMem node in the worklist");
3305       } else if (use->is_SafePoint()) {
3306         // Look for MergeMem nodes for calls which reference unique allocation
3307         // (through CheckCastPP nodes) even for debug info.
3308         Node* m = use->in(TypeFunc::Memory);
3309         if (m->is_MergeMem()) {
3310           assert(mergemem_worklist.contains(m->as_MergeMem()), "EA: missing MergeMem node in the worklist");
3311         }
3312       } else if (use->Opcode() == Op_EncodeISOArray) {
3313         if (use->in(MemNode::Memory) == n || use->in(3) == n) {
3314           // EncodeISOArray overwrites destination array
3315           memnode_worklist.append_if_missing(use);
3316         }



3317       } else {
3318         uint op = use->Opcode();
3319         if ((op == Op_StrCompressedCopy || op == Op_StrInflatedCopy) &&
3320             (use->in(MemNode::Memory) == n)) {
3321           // They overwrite memory edge corresponding to destination array,
3322           memnode_worklist.append_if_missing(use);
3323         } else if (!(op == Op_CmpP || op == Op_Conv2B ||
3324               op == Op_CastP2X || op == Op_StoreCM ||
3325               op == Op_FastLock || op == Op_AryEq || op == Op_StrComp || op == Op_HasNegatives ||
3326               op == Op_StrCompressedCopy || op == Op_StrInflatedCopy ||
3327               op == Op_StrEquals || op == Op_StrIndexOf || op == Op_StrIndexOfChar ||
3328               op == Op_SubTypeCheck ||
3329               BarrierSet::barrier_set()->barrier_set_c2()->is_gc_barrier_node(use))) {
3330           n->dump();
3331           use->dump();
3332           assert(false, "EA: missing allocation reference path");
3333         }
3334 #endif
3335       }
3336     }
3337 
3338   }
3339 
3340   // Go over all ArrayCopy nodes and if one of the inputs has a unique
3341   // type, record it in the ArrayCopy node so we know what memory this
3342   // node uses/modified.
3343   for (int next = 0; next < arraycopy_worklist.length(); next++) {
3344     ArrayCopyNode* ac = arraycopy_worklist.at(next);
3345     Node* dest = ac->in(ArrayCopyNode::Dest);
3346     if (dest->is_AddP()) {
3347       dest = get_addp_base(dest);
3348     }

3378   if (memnode_worklist.length() == 0)
3379     return;  // nothing to do
3380   while (memnode_worklist.length() != 0) {
3381     Node *n = memnode_worklist.pop();
3382     if (visited.test_set(n->_idx)) {
3383       continue;
3384     }
3385     if (n->is_Phi() || n->is_ClearArray()) {
3386       // we don't need to do anything, but the users must be pushed
3387     } else if (n->is_MemBar()) { // Initialize, MemBar nodes
3388       // we don't need to do anything, but the users must be pushed
3389       n = n->as_MemBar()->proj_out_or_null(TypeFunc::Memory);
3390       if (n == NULL) {
3391         continue;
3392       }
3393     } else if (n->Opcode() == Op_StrCompressedCopy ||
3394                n->Opcode() == Op_EncodeISOArray) {
3395       // get the memory projection
3396       n = n->find_out_with(Op_SCMemProj);
3397       assert(n != NULL && n->Opcode() == Op_SCMemProj, "memory projection required");



3398     } else {
3399       assert(n->is_Mem(), "memory node required.");
3400       Node *addr = n->in(MemNode::Address);
3401       const Type *addr_t = igvn->type(addr);
3402       if (addr_t == Type::TOP) {
3403         continue;
3404       }
3405       assert (addr_t->isa_ptr() != NULL, "pointer type required.");
3406       int alias_idx = _compile->get_alias_index(addr_t->is_ptr());
3407       assert ((uint)alias_idx < new_index_end, "wrong alias index");
3408       Node *mem = find_inst_mem(n->in(MemNode::Memory), alias_idx, orig_phis);
3409       if (_compile->failing()) {
3410         return;
3411       }
3412       if (mem != n->in(MemNode::Memory)) {
3413         // We delay the memory edge update since we need old one in
3414         // MergeMem code below when instances memory slices are separated.
3415         set_map(n, mem);
3416       }
3417       if (n->is_Load()) {

3420         // get the memory projection
3421         n = n->find_out_with(Op_SCMemProj);
3422         assert(n != NULL && n->Opcode() == Op_SCMemProj, "memory projection required");
3423       }
3424     }
3425     // push user on appropriate worklist
3426     for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
3427       Node *use = n->fast_out(i);
3428       if (use->is_Phi() || use->is_ClearArray()) {
3429         memnode_worklist.append_if_missing(use);
3430       } else if (use->is_Mem() && use->in(MemNode::Memory) == n) {
3431         if (use->Opcode() == Op_StoreCM) { // Ignore cardmark stores
3432           continue;
3433         }
3434         memnode_worklist.append_if_missing(use);
3435       } else if (use->is_MemBar()) {
3436         if (use->in(TypeFunc::Memory) == n) { // Ignore precedent edge
3437           memnode_worklist.append_if_missing(use);
3438         }
3439 #ifdef ASSERT
3440       } else if(use->is_Mem()) {
3441         assert(use->in(MemNode::Memory) != n, "EA: missing memory path");
3442       } else if (use->is_MergeMem()) {
3443         assert(mergemem_worklist.contains(use->as_MergeMem()), "EA: missing MergeMem node in the worklist");
3444       } else if (use->Opcode() == Op_EncodeISOArray) {
3445         if (use->in(MemNode::Memory) == n || use->in(3) == n) {
3446           // EncodeISOArray overwrites destination array
3447           memnode_worklist.append_if_missing(use);
3448         }




3449       } else {
3450         uint op = use->Opcode();
3451         if ((use->in(MemNode::Memory) == n) &&
3452             (op == Op_StrCompressedCopy || op == Op_StrInflatedCopy)) {
3453           // They overwrite memory edge corresponding to destination array,
3454           memnode_worklist.append_if_missing(use);
3455         } else if (!(BarrierSet::barrier_set()->barrier_set_c2()->is_gc_barrier_node(use) ||
3456               op == Op_AryEq || op == Op_StrComp || op == Op_HasNegatives ||
3457               op == Op_StrCompressedCopy || op == Op_StrInflatedCopy ||
3458               op == Op_StrEquals || op == Op_StrIndexOf || op == Op_StrIndexOfChar)) {
3459           n->dump();
3460           use->dump();
3461           assert(false, "EA: missing memory path");
3462         }
3463 #endif
3464       }
3465     }
3466   }
3467 
3468   //  Phase 3:  Process MergeMem nodes from mergemem_worklist.
3469   //            Walk each memory slice moving the first node encountered of each
3470   //            instance type to the the input corresponding to its alias index.
3471   uint length = mergemem_worklist.length();
3472   for( uint next = 0; next < length; ++next ) {
3473     MergeMemNode* nmm = mergemem_worklist.at(next);
3474     assert(!visited.test_set(nmm->_idx), "should not be visited before");
3475     // Note: we don't want to use MergeMemStream here because we only want to
3476     // scan inputs which exist at the start, not ones we add during processing.
3477     // Note 2: MergeMem may already contains instance memory slices added
3478     // during find_inst_mem() call when memory nodes were processed above.
3479     igvn->hash_delete(nmm);
3480     uint nslices = MIN2(nmm->req(), new_index_start);
3481     for (uint i = Compile::AliasIdxRaw+1; i < nslices; i++) {
3482       Node* mem = nmm->in(i);
3483       Node* cur = NULL;
3484       if (mem == NULL || mem->is_top()) {
3485         continue;
3486       }
3487       // First, update mergemem by moving memory nodes to corresponding slices
3488       // if their type became more precise since this mergemem was created.
3489       while (mem->is_Mem()) {
3490         const Type *at = igvn->type(mem->in(MemNode::Address));

3525       Node* result = step_through_mergemem(nmm, ni, tinst);
3526       if (result == nmm->base_memory()) {
3527         // Didn't find instance memory, search through general slice recursively.
3528         result = nmm->memory_at(_compile->get_general_index(ni));
3529         result = find_inst_mem(result, ni, orig_phis);
3530         if (_compile->failing()) {
3531           return;
3532         }
3533         nmm->set_memory_at(ni, result);
3534       }
3535     }
3536     igvn->hash_insert(nmm);
3537     record_for_optimizer(nmm);
3538   }
3539 
3540   //  Phase 4:  Update the inputs of non-instance memory Phis and
3541   //            the Memory input of memnodes
3542   // First update the inputs of any non-instance Phi's from
3543   // which we split out an instance Phi.  Note we don't have
3544   // to recursively process Phi's encountered on the input memory
3545   // chains as is done in split_memory_phi() since they  will
3546   // also be processed here.
3547   for (int j = 0; j < orig_phis.length(); j++) {
3548     PhiNode *phi = orig_phis.at(j);
3549     int alias_idx = _compile->get_alias_index(phi->adr_type());
3550     igvn->hash_delete(phi);
3551     for (uint i = 1; i < phi->req(); i++) {
3552       Node *mem = phi->in(i);
3553       Node *new_mem = find_inst_mem(mem, alias_idx, orig_phis);
3554       if (_compile->failing()) {
3555         return;
3556       }
3557       if (mem != new_mem) {
3558         phi->set_req(i, new_mem);
3559       }
3560     }
3561     igvn->hash_insert(phi);
3562     record_for_optimizer(phi);
3563   }
3564 
3565   // Update the memory inputs of MemNodes with the value we computed

  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "ci/bcEscapeAnalyzer.hpp"
  27 #include "compiler/compileLog.hpp"
  28 #include "gc/shared/barrierSet.hpp"
  29 #include "gc/shared/c2/barrierSetC2.hpp"
  30 #include "libadt/vectset.hpp"
  31 #include "memory/allocation.hpp"
  32 #include "memory/metaspace.hpp"
  33 #include "memory/resourceArea.hpp"
  34 #include "opto/c2compiler.hpp"
  35 #include "opto/arraycopynode.hpp"
  36 #include "opto/callnode.hpp"
  37 #include "opto/cfgnode.hpp"
  38 #include "opto/compile.hpp"
  39 #include "opto/escape.hpp"
  40 #include "opto/phaseX.hpp"
  41 #include "opto/movenode.hpp"
  42 #include "opto/rootnode.hpp"
  43 #include "utilities/macros.hpp"
  44 
  45 ConnectionGraph::ConnectionGraph(Compile * C, PhaseIterGVN *igvn) :
  46   _nodes(C->comp_arena(), C->unique(), C->unique(), NULL),
  47   _in_worklist(C->comp_arena()),
  48   _next_pidx(0),
  49   _collecting(true),
  50   _verify(false),
  51   _compile(C),
  52   _igvn(igvn),

 129   GrowableArray<SafePointNode*>  sfn_worklist;
 130   GrowableArray<MergeMemNode*>   mergemem_worklist;
 131   DEBUG_ONLY( GrowableArray<Node*> addp_worklist; )
 132 
 133   { Compile::TracePhase tp("connectionGraph", &Phase::timers[Phase::_t_connectionGraph]);
 134 
 135   // 1. Populate Connection Graph (CG) with PointsTo nodes.
 136   ideal_nodes.map(C->live_nodes(), NULL);  // preallocate space
 137   // Initialize worklist
 138   if (C->root() != NULL) {
 139     ideal_nodes.push(C->root());
 140   }
 141   // Processed ideal nodes are unique on ideal_nodes list
 142   // but several ideal nodes are mapped to the phantom_obj.
 143   // To avoid duplicated entries on the following worklists
 144   // add the phantom_obj only once to them.
 145   ptnodes_worklist.append(phantom_obj);
 146   java_objects_worklist.append(phantom_obj);
 147   for( uint next = 0; next < ideal_nodes.size(); ++next ) {
 148     Node* n = ideal_nodes.at(next);
 149     if ((n->Opcode() == Op_LoadX || n->Opcode() == Op_StoreX) &&
 150         !n->in(MemNode::Address)->is_AddP() &&
 151         _igvn->type(n->in(MemNode::Address))->isa_oopptr()) {
 152       // Load/Store at mark work address is at offset 0 so has no AddP which confuses EA
 153       Node* addp = new AddPNode(n->in(MemNode::Address), n->in(MemNode::Address), _igvn->MakeConX(0));
 154       _igvn->register_new_node_with_optimizer(addp);
 155       _igvn->replace_input_of(n, MemNode::Address, addp);
 156       ideal_nodes.push(addp);
 157       _nodes.at_put_grow(addp->_idx, NULL, NULL);
 158     }
 159     // Create PointsTo nodes and add them to Connection Graph. Called
 160     // only once per ideal node since ideal_nodes is Unique_Node list.
 161     add_node_to_connection_graph(n, &delayed_worklist);
 162     PointsToNode* ptn = ptnode_adr(n->_idx);
 163     if (ptn != NULL && ptn != phantom_obj) {
 164       ptnodes_worklist.append(ptn);
 165       if (ptn->is_JavaObject()) {
 166         java_objects_worklist.append(ptn->as_JavaObject());
 167         if ((n->is_Allocate() || n->is_CallStaticJava()) &&
 168             (ptn->escape_state() < PointsToNode::GlobalEscape)) {
 169           // Only allocations and java static calls results are interesting.
 170           non_escaped_allocs_worklist.append(ptn->as_JavaObject());
 171         }
 172       } else if (ptn->is_Field() && ptn->as_Field()->is_oop()) {
 173         oop_fields_worklist.append(ptn->as_Field());
 174       }
 175     }
 176     // Collect some interesting nodes for futher use.
 177     switch (n->Opcode()) {
 178       case Op_MergeMem:

 398   return false;
 399 }
 400 
 401 // Returns true if at least one of the arguments to the call is an object
 402 // that does not escape globally.
 403 bool ConnectionGraph::has_arg_escape(CallJavaNode* call) {
 404   if (call->method() != NULL) {
 405     uint max_idx = TypeFunc::Parms + call->method()->arg_size();
 406     for (uint idx = TypeFunc::Parms; idx < max_idx; idx++) {
 407       Node* p = call->in(idx);
 408       if (not_global_escape(p)) {
 409         return true;
 410       }
 411     }
 412   } else {
 413     const char* name = call->as_CallStaticJava()->_name;
 414     assert(name != NULL, "no name");
 415     // no arg escapes through uncommon traps
 416     if (strcmp(name, "uncommon_trap") != 0) {
 417       // process_call_arguments() assumes that all arguments escape globally
 418       const TypeTuple* d = call->tf()->domain_sig();
 419       for (uint i = TypeFunc::Parms; i < d->cnt(); i++) {
 420         const Type* at = d->field_at(i);
 421         if (at->isa_oopptr() != NULL) {
 422           return true;
 423         }
 424       }
 425     }
 426   }
 427   return false;
 428 }
 429 
 430 
 431 
 432 // Utility function for nodes that load an object
 433 void ConnectionGraph::add_objload_to_connection_graph(Node *n, Unique_Node_List *delayed_worklist) {
 434   // Using isa_ptr() instead of isa_oopptr() for LoadP and Phi because
 435   // ThreadLocal has RawPtr type.
 436   const Type* t = _igvn->type(n);
 437   if (t->make_ptr() != NULL) {
 438     Node* adr = n->in(MemNode::Address);

 472       // first IGVN optimization when escape information is still available.
 473       record_for_optimizer(n);
 474     } else if (n->is_Allocate()) {
 475       add_call_node(n->as_Call());
 476       record_for_optimizer(n);
 477     } else {
 478       if (n->is_CallStaticJava()) {
 479         const char* name = n->as_CallStaticJava()->_name;
 480         if (name != NULL && strcmp(name, "uncommon_trap") == 0) {
 481           return; // Skip uncommon traps
 482         }
 483       }
 484       // Don't mark as processed since call's arguments have to be processed.
 485       delayed_worklist->push(n);
 486       // Check if a call returns an object.
 487       if ((n->as_Call()->returns_pointer() &&
 488            n->as_Call()->proj_out_or_null(TypeFunc::Parms) != NULL) ||
 489           (n->is_CallStaticJava() &&
 490            n->as_CallStaticJava()->is_boxing_method())) {
 491         add_call_node(n->as_Call());
 492       } else if (n->as_Call()->tf()->returns_inline_type_as_fields()) {
 493         bool returns_oop = false;
 494         for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax && !returns_oop; i++) {
 495           ProjNode* pn = n->fast_out(i)->as_Proj();
 496           if (pn->_con >= TypeFunc::Parms && pn->bottom_type()->isa_ptr()) {
 497             returns_oop = true;
 498           }
 499         }
 500         if (returns_oop) {
 501           add_call_node(n->as_Call());
 502         }
 503       }
 504     }
 505     return;
 506   }
 507   // Put this check here to process call arguments since some call nodes
 508   // point to phantom_obj.
 509   if (n_ptn == phantom_obj || n_ptn == null_obj) {
 510     return; // Skip predefined nodes.
 511   }
 512   switch (opcode) {
 513     case Op_AddP: {
 514       Node* base = get_addp_base(n);
 515       PointsToNode* ptn_base = ptnode_adr(base->_idx);
 516       // Field nodes are created for all field types. They are used in
 517       // adjust_scalar_replaceable_state() and split_unique_types().
 518       // Note, non-oop fields will have only base edges in Connection
 519       // Graph because such fields are not used for oop loads and stores.
 520       int offset = address_offset(n, igvn);
 521       add_field(n, PointsToNode::NoEscape, offset);
 522       if (ptn_base == NULL) {
 523         delayed_worklist->push(n); // Process it later.
 524       } else {
 525         n_ptn = ptnode_adr(n_idx);
 526         add_base(n_ptn->as_Field(), ptn_base);
 527       }
 528       break;
 529     }
 530     case Op_CastX2P: {
 531       map_ideal_node(n, phantom_obj);
 532       break;
 533     }
 534     case Op_InlineTypePtr:
 535     case Op_CastPP:
 536     case Op_CheckCastPP:
 537     case Op_EncodeP:
 538     case Op_DecodeN:
 539     case Op_EncodePKlass:
 540     case Op_DecodeNKlass: {
 541       add_local_var_and_edge(n, PointsToNode::NoEscape, n->in(1), delayed_worklist);
 542       break;
 543     }
 544     case Op_CMoveP: {
 545       add_local_var(n, PointsToNode::NoEscape);
 546       // Do not add edges during first iteration because some could be
 547       // not defined yet.
 548       delayed_worklist->push(n);
 549       break;
 550     }
 551     case Op_ConP:
 552     case Op_ConN:
 553     case Op_ConNKlass: {
 554       // assume all oop constants globally escape except for null

 586     case Op_PartialSubtypeCheck: {
 587       // Produces Null or notNull and is used in only in CmpP so
 588       // phantom_obj could be used.
 589       map_ideal_node(n, phantom_obj); // Result is unknown
 590       break;
 591     }
 592     case Op_Phi: {
 593       // Using isa_ptr() instead of isa_oopptr() for LoadP and Phi because
 594       // ThreadLocal has RawPtr type.
 595       const Type* t = n->as_Phi()->type();
 596       if (t->make_ptr() != NULL) {
 597         add_local_var(n, PointsToNode::NoEscape);
 598         // Do not add edges during first iteration because some could be
 599         // not defined yet.
 600         delayed_worklist->push(n);
 601       }
 602       break;
 603     }
 604     case Op_Proj: {
 605       // we are only interested in the oop result projection from a call
 606       if (n->as_Proj()->_con >= TypeFunc::Parms && n->in(0)->is_Call() &&
 607           (n->in(0)->as_Call()->returns_pointer() || n->bottom_type()->isa_ptr())) {
 608         assert((n->as_Proj()->_con == TypeFunc::Parms && n->in(0)->as_Call()->returns_pointer()) ||
 609                n->in(0)->as_Call()->tf()->returns_inline_type_as_fields(), "what kind of oop return is it?");
 610         add_local_var_and_edge(n, PointsToNode::NoEscape, n->in(0), delayed_worklist);
 611       }
 612       break;
 613     }
 614     case Op_Rethrow: // Exception object escapes
 615     case Op_Return: {
 616       if (n->req() > TypeFunc::Parms &&
 617           igvn->type(n->in(TypeFunc::Parms))->isa_oopptr()) {
 618         // Treat Return value as LocalVar with GlobalEscape escape state.
 619         add_local_var_and_edge(n, PointsToNode::GlobalEscape, n->in(TypeFunc::Parms), delayed_worklist);
 620       }
 621       break;
 622     }
 623     case Op_CompareAndExchangeP:
 624     case Op_CompareAndExchangeN:
 625     case Op_GetAndSetP:
 626     case Op_GetAndSetN: {
 627       add_objload_to_connection_graph(n, delayed_worklist);
 628       // fall-through
 629     }

 672   if (n->is_Call()) {
 673     process_call_arguments(n->as_Call());
 674     return;
 675   }
 676   assert(n->is_Store() || n->is_LoadStore() ||
 677          (n_ptn != NULL) && (n_ptn->ideal_node() != NULL),
 678          "node should be registered already");
 679   int opcode = n->Opcode();
 680   bool gc_handled = BarrierSet::barrier_set()->barrier_set_c2()->escape_add_final_edges(this, _igvn, n, opcode);
 681   if (gc_handled) {
 682     return; // Ignore node if already handled by GC.
 683   }
 684   switch (opcode) {
 685     case Op_AddP: {
 686       Node* base = get_addp_base(n);
 687       PointsToNode* ptn_base = ptnode_adr(base->_idx);
 688       assert(ptn_base != NULL, "field's base should be registered");
 689       add_base(n_ptn->as_Field(), ptn_base);
 690       break;
 691     }
 692     case Op_InlineTypePtr:
 693     case Op_CastPP:
 694     case Op_CheckCastPP:
 695     case Op_EncodeP:
 696     case Op_DecodeN:
 697     case Op_EncodePKlass:
 698     case Op_DecodeNKlass: {
 699       add_local_var_and_edge(n, PointsToNode::NoEscape, n->in(1), NULL);
 700       break;
 701     }
 702     case Op_CMoveP: {
 703       for (uint i = CMoveNode::IfFalse; i < n->req(); i++) {
 704         Node* in = n->in(i);
 705         if (in == NULL) {
 706           continue;  // ignore NULL
 707         }
 708         Node* uncast_in = in->uncast();
 709         if (uncast_in->is_top() || uncast_in == n) {
 710           continue;  // ignore top or inputs which go back this node
 711         }
 712         PointsToNode* ptn = ptnode_adr(in->_idx);

 728       // Using isa_ptr() instead of isa_oopptr() for LoadP and Phi because
 729       // ThreadLocal has RawPtr type.
 730       assert(n->as_Phi()->type()->make_ptr() != NULL, "Unexpected node type");
 731       for (uint i = 1; i < n->req(); i++) {
 732         Node* in = n->in(i);
 733         if (in == NULL) {
 734           continue;  // ignore NULL
 735         }
 736         Node* uncast_in = in->uncast();
 737         if (uncast_in->is_top() || uncast_in == n) {
 738           continue;  // ignore top or inputs which go back this node
 739         }
 740         PointsToNode* ptn = ptnode_adr(in->_idx);
 741         assert(ptn != NULL, "node should be registered");
 742         add_edge(n_ptn, ptn);
 743       }
 744       break;
 745     }
 746     case Op_Proj: {
 747       // we are only interested in the oop result projection from a call
 748       assert((n->as_Proj()->_con == TypeFunc::Parms && n->in(0)->as_Call()->returns_pointer()) ||
 749              n->in(0)->as_Call()->tf()->returns_inline_type_as_fields(), "what kind of oop return is it?");
 750       add_local_var_and_edge(n, PointsToNode::NoEscape, n->in(0), NULL);
 751       break;
 752     }
 753     case Op_Rethrow: // Exception object escapes
 754     case Op_Return: {
 755       assert(n->req() > TypeFunc::Parms && _igvn->type(n->in(TypeFunc::Parms))->isa_oopptr(),
 756              "Unexpected node type");
 757       // Treat Return value as LocalVar with GlobalEscape escape state.
 758       add_local_var_and_edge(n, PointsToNode::GlobalEscape, n->in(TypeFunc::Parms), NULL);
 759       break;
 760     }
 761     case Op_CompareAndExchangeP:
 762     case Op_CompareAndExchangeN:
 763     case Op_GetAndSetP:
 764     case Op_GetAndSetN:{
 765       assert(_igvn->type(n)->make_ptr() != NULL, "Unexpected node type");
 766       add_local_var_and_edge(n, PointsToNode::NoEscape, n->in(MemNode::Address), NULL);
 767       // fall-through
 768     }
 769     case Op_CompareAndSwapP:

 885     PointsToNode* ptn = ptnode_adr(val->_idx);
 886     assert(ptn != NULL, "node should be registered");
 887     set_escape_state(ptn, PointsToNode::GlobalEscape);
 888     // Add edge to object for unsafe access with offset.
 889     PointsToNode* adr_ptn = ptnode_adr(adr->_idx);
 890     assert(adr_ptn != NULL, "node should be registered");
 891     if (adr_ptn->is_Field()) {
 892       assert(adr_ptn->as_Field()->is_oop(), "should be oop field");
 893       add_edge(adr_ptn, ptn);
 894     }
 895     return true;
 896   }
 897 #ifdef ASSERT
 898   n->dump(1);
 899   assert(false, "not unsafe");
 900 #endif
 901   return false;
 902 }
 903 
 904 void ConnectionGraph::add_call_node(CallNode* call) {
 905   assert(call->returns_pointer() || call->tf()->returns_inline_type_as_fields(), "only for call which returns pointer");
 906   uint call_idx = call->_idx;
 907   if (call->is_Allocate()) {
 908     Node* k = call->in(AllocateNode::KlassNode);
 909     const TypeKlassPtr* kt = k->bottom_type()->isa_klassptr();
 910     assert(kt != NULL, "TypeKlassPtr  required.");
 911     ciKlass* cik = kt->klass();
 912     PointsToNode::EscapeState es = PointsToNode::NoEscape;
 913     bool scalar_replaceable = true;
 914     if (call->is_AllocateArray()) {
 915       if (!cik->is_array_klass()) { // StressReflectiveCode
 916         es = PointsToNode::GlobalEscape;
 917       } else {
 918         int length = call->in(AllocateNode::ALength)->find_int_con(-1);
 919         if (length < 0 || length > EliminateAllocationArraySizeLimit) {
 920           // Not scalar replaceable if the length is not constant or too big.
 921           scalar_replaceable = false;
 922         }
 923       }
 924     } else {  // Allocate instance
 925       if (cik->is_subclass_of(_compile->env()->Thread_klass()) ||

 950     //
 951     //    - all oop arguments are escaping globally;
 952     //
 953     // 2. CallStaticJavaNode (execute bytecode analysis if possible):
 954     //
 955     //    - the same as CallDynamicJavaNode if can't do bytecode analysis;
 956     //
 957     //    - mapped to GlobalEscape JavaObject node if unknown oop is returned;
 958     //    - mapped to NoEscape JavaObject node if non-escaping object allocated
 959     //      during call is returned;
 960     //    - mapped to ArgEscape LocalVar node pointed to object arguments
 961     //      which are returned and does not escape during call;
 962     //
 963     //    - oop arguments escaping status is defined by bytecode analysis;
 964     //
 965     // For a static call, we know exactly what method is being called.
 966     // Use bytecode estimator to record whether the call's return value escapes.
 967     ciMethod* meth = call->as_CallJava()->method();
 968     if (meth == NULL) {
 969       const char* name = call->as_CallStaticJava()->_name;
 970       assert(strncmp(name, "_multianewarray", 15) == 0 ||
 971              strncmp(name, "_load_unknown_inline", 20) == 0, "TODO: add failed case check");
 972       // Returns a newly allocated non-escaped object.
 973       add_java_object(call, PointsToNode::NoEscape);
 974       ptnode_adr(call_idx)->set_scalar_replaceable(false);
 975     } else if (meth->is_boxing_method()) {
 976       // Returns boxing object
 977       PointsToNode::EscapeState es;
 978       vmIntrinsics::ID intr = meth->intrinsic_id();
 979       if (intr == vmIntrinsics::_floatValue || intr == vmIntrinsics::_doubleValue) {
 980         // It does not escape if object is always allocated.
 981         es = PointsToNode::NoEscape;
 982       } else {
 983         // It escapes globally if object could be loaded from cache.
 984         es = PointsToNode::GlobalEscape;
 985       }
 986       add_java_object(call, es);
 987     } else {
 988       BCEscapeAnalyzer* call_analyzer = meth->get_bcea();
 989       call_analyzer->copy_dependencies(_compile->dependencies());
 990       if (call_analyzer->is_return_allocated()) {
 991         // Returns a newly allocated non-escaped object, simply
 992         // update dependency information.
 993         // Mark it as NoEscape so that objects referenced by
 994         // it's fields will be marked as NoEscape at least.
 995         add_java_object(call, PointsToNode::NoEscape);
 996         ptnode_adr(call_idx)->set_scalar_replaceable(false);
 997       } else {
 998         // Determine whether any arguments are returned.
 999         const TypeTuple* d = call->tf()->domain_cc();
1000         bool ret_arg = false;
1001         for (uint i = TypeFunc::Parms; i < d->cnt(); i++) {
1002           if (d->field_at(i)->isa_ptr() != NULL &&
1003               call_analyzer->is_arg_returned(i - TypeFunc::Parms)) {
1004             ret_arg = true;
1005             break;
1006           }
1007         }
1008         if (ret_arg) {
1009           add_local_var(call, PointsToNode::ArgEscape);
1010         } else {
1011           // Returns unknown object.
1012           map_ideal_node(call, phantom_obj);
1013         }
1014       }
1015     }
1016   } else {
1017     // An other type of call, assume the worst case:
1018     // returned value is unknown and globally escapes.
1019     assert(call->Opcode() == Op_CallDynamicJava, "add failed case check");

1027 #ifdef ASSERT
1028     case Op_Allocate:
1029     case Op_AllocateArray:
1030     case Op_Lock:
1031     case Op_Unlock:
1032       assert(false, "should be done already");
1033       break;
1034 #endif
1035     case Op_ArrayCopy:
1036     case Op_CallLeafNoFP:
1037       // Most array copies are ArrayCopy nodes at this point but there
1038       // are still a few direct calls to the copy subroutines (See
1039       // PhaseStringOpts::copy_string())
1040       is_arraycopy = (call->Opcode() == Op_ArrayCopy) ||
1041         call->as_CallLeaf()->is_call_to_arraycopystub();
1042       // fall through
1043     case Op_CallLeafVector:
1044     case Op_CallLeaf: {
1045       // Stub calls, objects do not escape but they are not scale replaceable.
1046       // Adjust escape state for outgoing arguments.
1047       const TypeTuple * d = call->tf()->domain_sig();
1048       bool src_has_oops = false;
1049       for (uint i = TypeFunc::Parms; i < d->cnt(); i++) {
1050         const Type* at = d->field_at(i);
1051         Node *arg = call->in(i);
1052         if (arg == NULL) {
1053           continue;
1054         }
1055         const Type *aat = _igvn->type(arg);
1056         if (arg->is_top() || !at->isa_ptr() || !aat->isa_ptr()) {
1057           continue;
1058         }
1059         if (arg->is_AddP()) {
1060           //
1061           // The inline_native_clone() case when the arraycopy stub is called
1062           // after the allocation before Initialize and CheckCastPP nodes.
1063           // Or normal arraycopy for object arrays case.
1064           //
1065           // Set AddP's base (Allocate) as not scalar replaceable since
1066           // pointer to the base (with offset) is passed as argument.
1067           //
1068           arg = get_addp_base(arg);
1069         }
1070         PointsToNode* arg_ptn = ptnode_adr(arg->_idx);
1071         assert(arg_ptn != NULL, "should be registered");
1072         PointsToNode::EscapeState arg_esc = arg_ptn->escape_state();
1073         if (is_arraycopy || arg_esc < PointsToNode::ArgEscape) {
1074           assert(aat == Type::TOP || aat == TypePtr::NULL_PTR ||
1075                  aat->isa_ptr() != NULL, "expecting an Ptr");
1076           bool arg_has_oops = aat->isa_oopptr() &&
1077                               (aat->isa_oopptr()->klass() == NULL || aat->isa_instptr() ||
1078                                (aat->isa_aryptr() && aat->isa_aryptr()->klass()->is_obj_array_klass()) ||
1079                                (aat->isa_aryptr() && aat->isa_aryptr()->elem() != NULL &&
1080                                 aat->isa_aryptr()->is_flat() &&
1081                                 aat->isa_aryptr()->elem()->inline_klass()->contains_oops()));
1082           if (i == TypeFunc::Parms) {
1083             src_has_oops = arg_has_oops;
1084           }
1085           //
1086           // src or dst could be j.l.Object when other is basic type array:
1087           //
1088           //   arraycopy(char[],0,Object*,0,size);
1089           //   arraycopy(Object*,0,char[],0,size);
1090           //
1091           // Don't add edges in such cases.
1092           //
1093           bool arg_is_arraycopy_dest = src_has_oops && is_arraycopy &&
1094                                        arg_has_oops && (i > TypeFunc::Parms);
1095 #ifdef ASSERT
1096           if (!(is_arraycopy ||
1097                 BarrierSet::barrier_set()->barrier_set_c2()->is_gc_barrier_node(call) ||
1098                 (call->as_CallLeaf()->_name != NULL &&
1099                  (strcmp(call->as_CallLeaf()->_name, "updateBytesCRC32") == 0 ||
1100                   strcmp(call->as_CallLeaf()->_name, "updateBytesCRC32C") == 0 ||
1101                   strcmp(call->as_CallLeaf()->_name, "updateBytesAdler32") == 0 ||

1108                   strcmp(call->as_CallLeaf()->_name, "counterMode_AESCrypt") == 0 ||
1109                   strcmp(call->as_CallLeaf()->_name, "galoisCounterMode_AESCrypt") == 0 ||
1110                   strcmp(call->as_CallLeaf()->_name, "ghash_processBlocks") == 0 ||
1111                   strcmp(call->as_CallLeaf()->_name, "encodeBlock") == 0 ||
1112                   strcmp(call->as_CallLeaf()->_name, "decodeBlock") == 0 ||
1113                   strcmp(call->as_CallLeaf()->_name, "md5_implCompress") == 0 ||
1114                   strcmp(call->as_CallLeaf()->_name, "md5_implCompressMB") == 0 ||
1115                   strcmp(call->as_CallLeaf()->_name, "sha1_implCompress") == 0 ||
1116                   strcmp(call->as_CallLeaf()->_name, "sha1_implCompressMB") == 0 ||
1117                   strcmp(call->as_CallLeaf()->_name, "sha256_implCompress") == 0 ||
1118                   strcmp(call->as_CallLeaf()->_name, "sha256_implCompressMB") == 0 ||
1119                   strcmp(call->as_CallLeaf()->_name, "sha512_implCompress") == 0 ||
1120                   strcmp(call->as_CallLeaf()->_name, "sha512_implCompressMB") == 0 ||
1121                   strcmp(call->as_CallLeaf()->_name, "sha3_implCompress") == 0 ||
1122                   strcmp(call->as_CallLeaf()->_name, "sha3_implCompressMB") == 0 ||
1123                   strcmp(call->as_CallLeaf()->_name, "multiplyToLen") == 0 ||
1124                   strcmp(call->as_CallLeaf()->_name, "squareToLen") == 0 ||
1125                   strcmp(call->as_CallLeaf()->_name, "mulAdd") == 0 ||
1126                   strcmp(call->as_CallLeaf()->_name, "montgomery_multiply") == 0 ||
1127                   strcmp(call->as_CallLeaf()->_name, "montgomery_square") == 0 ||
1128                   strcmp(call->as_CallLeaf()->_name, "vectorizedMismatch") == 0 ||
1129                   strcmp(call->as_CallLeaf()->_name, "load_unknown_inline") == 0 ||
1130                   strcmp(call->as_CallLeaf()->_name, "store_unknown_inline") == 0 ||
1131                   strcmp(call->as_CallLeaf()->_name, "bigIntegerRightShiftWorker") == 0 ||
1132                   strcmp(call->as_CallLeaf()->_name, "bigIntegerLeftShiftWorker") == 0 ||
1133                   strcmp(call->as_CallLeaf()->_name, "vectorizedMismatch") == 0 ||
1134                   strcmp(call->as_CallLeaf()->_name, "get_class_id_intrinsic") == 0)
1135                  ))) {
1136             call->dump();
1137             fatal("EA unexpected CallLeaf %s", call->as_CallLeaf()->_name);
1138           }
1139 #endif
1140           // Always process arraycopy's destination object since
1141           // we need to add all possible edges to references in
1142           // source object.
1143           if (arg_esc >= PointsToNode::ArgEscape &&
1144               !arg_is_arraycopy_dest) {
1145             continue;
1146           }
1147           PointsToNode::EscapeState es = PointsToNode::ArgEscape;
1148           if (call->is_ArrayCopy()) {
1149             ArrayCopyNode* ac = call->as_ArrayCopy();
1150             if (ac->is_clonebasic() ||

1173           }
1174         }
1175       }
1176       break;
1177     }
1178     case Op_CallStaticJava: {
1179       // For a static call, we know exactly what method is being called.
1180       // Use bytecode estimator to record the call's escape affects
1181 #ifdef ASSERT
1182       const char* name = call->as_CallStaticJava()->_name;
1183       assert((name == NULL || strcmp(name, "uncommon_trap") != 0), "normal calls only");
1184 #endif
1185       ciMethod* meth = call->as_CallJava()->method();
1186       if ((meth != NULL) && meth->is_boxing_method()) {
1187         break; // Boxing methods do not modify any oops.
1188       }
1189       BCEscapeAnalyzer* call_analyzer = (meth !=NULL) ? meth->get_bcea() : NULL;
1190       // fall-through if not a Java method or no analyzer information
1191       if (call_analyzer != NULL) {
1192         PointsToNode* call_ptn = ptnode_adr(call->_idx);
1193         const TypeTuple* d = call->tf()->domain_cc();
1194         for (uint i = TypeFunc::Parms; i < d->cnt(); i++) {
1195           const Type* at = d->field_at(i);
1196           int k = i - TypeFunc::Parms;
1197           Node* arg = call->in(i);
1198           PointsToNode* arg_ptn = ptnode_adr(arg->_idx);
1199           if (at->isa_ptr() != NULL &&
1200               call_analyzer->is_arg_returned(k)) {
1201             // The call returns arguments.
1202             if (call_ptn != NULL) { // Is call's result used?
1203               assert(call_ptn->is_LocalVar(), "node should be registered");
1204               assert(arg_ptn != NULL, "node should be registered");
1205               add_edge(call_ptn, arg_ptn);
1206             }
1207           }
1208           if (at->isa_oopptr() != NULL &&
1209               arg_ptn->escape_state() < PointsToNode::GlobalEscape) {
1210             if (!call_analyzer->is_arg_stack(k)) {
1211               // The argument global escapes
1212               set_escape_state(arg_ptn, PointsToNode::GlobalEscape);
1213             } else {

1217                 set_fields_escape_state(arg_ptn, PointsToNode::GlobalEscape);
1218               }
1219             }
1220           }
1221         }
1222         if (call_ptn != NULL && call_ptn->is_LocalVar()) {
1223           // The call returns arguments.
1224           assert(call_ptn->edge_count() > 0, "sanity");
1225           if (!call_analyzer->is_return_local()) {
1226             // Returns also unknown object.
1227             add_edge(call_ptn, phantom_obj);
1228           }
1229         }
1230         break;
1231       }
1232     }
1233     default: {
1234       // Fall-through here if not a Java method or no analyzer information
1235       // or some other type of call, assume the worst case: all arguments
1236       // globally escape.
1237       const TypeTuple* d = call->tf()->domain_cc();
1238       for (uint i = TypeFunc::Parms; i < d->cnt(); i++) {
1239         const Type* at = d->field_at(i);
1240         if (at->isa_oopptr() != NULL) {
1241           Node* arg = call->in(i);
1242           if (arg->is_AddP()) {
1243             arg = get_addp_base(arg);
1244           }
1245           assert(ptnode_adr(arg->_idx) != NULL, "should be defined already");
1246           set_escape_state(ptnode_adr(arg->_idx), PointsToNode::GlobalEscape);
1247         }
1248       }
1249     }
1250   }
1251 }
1252 
1253 
1254 // Finish Graph construction.
1255 bool ConnectionGraph::complete_connection_graph(
1256                          GrowableArray<PointsToNode*>&   ptnodes_worklist,
1257                          GrowableArray<JavaObjectNode*>& non_escaped_allocs_worklist,

1636     PointsToNode* base = i.get();
1637     if (base->is_JavaObject()) {
1638       // Skip Allocate's fields which will be processed later.
1639       if (base->ideal_node()->is_Allocate()) {
1640         return 0;
1641       }
1642       assert(base == null_obj, "only NULL ptr base expected here");
1643     }
1644   }
1645   if (add_edge(field, phantom_obj)) {
1646     // New edge was added
1647     new_edges++;
1648     add_field_uses_to_worklist(field);
1649   }
1650   return new_edges;
1651 }
1652 
1653 // Find fields initializing values for allocations.
1654 int ConnectionGraph::find_init_values_phantom(JavaObjectNode* pta) {
1655   assert(pta->escape_state() == PointsToNode::NoEscape, "Not escaped Allocate nodes only");
1656   PointsToNode* init_val = phantom_obj;
1657   Node* alloc = pta->ideal_node();
1658 
1659   // Do nothing for Allocate nodes since its fields values are
1660   // "known" unless they are initialized by arraycopy/clone.
1661   if (alloc->is_Allocate() && !pta->arraycopy_dst()) {
1662     if (alloc->as_Allocate()->in(AllocateNode::DefaultValue) != NULL) {
1663       // Non-flattened inline type arrays are initialized with
1664       // the default value instead of null. Handle them here.
1665       init_val = ptnode_adr(alloc->as_Allocate()->in(AllocateNode::DefaultValue)->_idx);
1666       assert(init_val != NULL, "default value should be registered");
1667     } else {
1668       return 0;
1669     }
1670   }
1671   // Non-escaped allocation returned from Java or runtime call has unknown values in fields.
1672   assert(pta->arraycopy_dst() || alloc->is_CallStaticJava() || init_val != phantom_obj, "sanity");
1673 #ifdef ASSERT
1674   if (alloc->is_CallStaticJava() && alloc->as_CallStaticJava()->method() == NULL) {
1675     const char* name = alloc->as_CallStaticJava()->_name;
1676     assert(strncmp(name, "_multianewarray", 15) == 0 ||
1677            strncmp(name, "_load_unknown_inline", 20) == 0, "sanity");
1678   }
1679 #endif
1680   // Non-escaped allocation returned from Java or runtime call have unknown values in fields.
1681   int new_edges = 0;
1682   for (EdgeIterator i(pta); i.has_next(); i.next()) {
1683     PointsToNode* field = i.get();
1684     if (field->is_Field() && field->as_Field()->is_oop()) {
1685       if (add_edge(field, init_val)) {
1686         // New edge was added
1687         new_edges++;
1688         add_field_uses_to_worklist(field->as_Field());
1689       }
1690     }
1691   }
1692   return new_edges;
1693 }
1694 
1695 // Find fields initializing values for allocations.
1696 int ConnectionGraph::find_init_values_null(JavaObjectNode* pta, PhaseTransform* phase) {
1697   assert(pta->escape_state() == PointsToNode::NoEscape, "Not escaped Allocate nodes only");
1698   Node* alloc = pta->ideal_node();
1699   // Do nothing for Call nodes since its fields values are unknown.
1700   if (!alloc->is_Allocate() || alloc->as_Allocate()->in(AllocateNode::DefaultValue) != NULL) {
1701     return 0;
1702   }
1703   InitializeNode* ini = alloc->as_Allocate()->initialization();
1704   bool visited_bottom_offset = false;
1705   GrowableArray<int> offsets_worklist;
1706   int new_edges = 0;
1707 
1708   // Check if an oop field's initializing value is recorded and add
1709   // a corresponding NULL if field's value if it is not recorded.
1710   // Connection Graph does not record a default initialization by NULL
1711   // captured by Initialize node.
1712   //
1713   for (EdgeIterator i(pta); i.has_next(); i.next()) {
1714     PointsToNode* field = i.get(); // Field (AddP)
1715     if (!field->is_Field() || !field->as_Field()->is_oop()) {
1716       continue; // Not oop field
1717     }
1718     int offset = field->as_Field()->offset();
1719     if (offset == Type::OffsetBot) {
1720       if (!visited_bottom_offset) {

1766               } else {
1767                 if (!val->is_LocalVar() || (val->edge_count() == 0)) {
1768                   tty->print_cr("----------init store has invalid value -----");
1769                   store->dump();
1770                   val->dump();
1771                   assert(val->is_LocalVar() && (val->edge_count() > 0), "should be processed already");
1772                 }
1773                 for (EdgeIterator j(val); j.has_next(); j.next()) {
1774                   PointsToNode* obj = j.get();
1775                   if (obj->is_JavaObject()) {
1776                     if (!field->points_to(obj->as_JavaObject())) {
1777                       missed_obj = obj;
1778                       break;
1779                     }
1780                   }
1781                 }
1782               }
1783               if (missed_obj != NULL) {
1784                 tty->print_cr("----------field---------------------------------");
1785                 field->dump();
1786                 tty->print_cr("----------missed reference to object------------");
1787                 missed_obj->dump();
1788                 tty->print_cr("----------object referenced by init store-------");
1789                 store->dump();
1790                 val->dump();
1791                 assert(!field->points_to(missed_obj->as_JavaObject()), "missed JavaObject reference");
1792               }
1793             }
1794 #endif
1795           } else {
1796             // There could be initializing stores which follow allocation.
1797             // For example, a volatile field store is not collected
1798             // by Initialize node.
1799             //
1800             // Need to check for dependent loads to separate such stores from
1801             // stores which follow loads. For now, add initial value NULL so
1802             // that compare pointers optimization works correctly.
1803           }
1804         }
1805         if (value == NULL) {
1806           // A field's initializing value was not recorded. Add NULL.
1807           if (add_edge(field, null_obj)) {
1808             // New edge was added

1837       }
1838       // 2. An object is not scalar replaceable if the field into which it is
1839       // stored has multiple bases one of which is null.
1840       if (field->base_count() > 1) {
1841         for (BaseIterator i(field); i.has_next(); i.next()) {
1842           PointsToNode* base = i.get();
1843           if (base == null_obj) {
1844             jobj->set_scalar_replaceable(false);
1845             return;
1846           }
1847         }
1848       }
1849     }
1850     assert(use->is_Field() || use->is_LocalVar(), "sanity");
1851     // 3. An object is not scalar replaceable if it is merged with other objects.
1852     for (EdgeIterator j(use); j.has_next(); j.next()) {
1853       PointsToNode* ptn = j.get();
1854       if (ptn->is_JavaObject() && ptn != jobj) {
1855         // Mark all objects.
1856         jobj->set_scalar_replaceable(false);
1857         ptn->set_scalar_replaceable(false);
1858       }
1859     }
1860     if (!jobj->scalar_replaceable()) {
1861       return;
1862     }
1863   }
1864 
1865   for (EdgeIterator j(jobj); j.has_next(); j.next()) {
1866     if (j.get()->is_Arraycopy()) {
1867       continue;
1868     }
1869 
1870     // Non-escaping object node should point only to field nodes.
1871     FieldNode* field = j.get()->as_Field();
1872     int offset = field->as_Field()->offset();
1873 
1874     // 4. An object is not scalar replaceable if it has a field with unknown
1875     // offset (array's element is accessed in loop).
1876     if (offset == Type::OffsetBot) {
1877       jobj->set_scalar_replaceable(false);

2000         assert(field->edge_count() > 0, "sanity");
2001       }
2002     }
2003   }
2004 }
2005 #endif
2006 
2007 // Optimize ideal graph.
2008 void ConnectionGraph::optimize_ideal_graph(GrowableArray<Node*>& ptr_cmp_worklist,
2009                                            GrowableArray<MemBarStoreStoreNode*>& storestore_worklist) {
2010   Compile* C = _compile;
2011   PhaseIterGVN* igvn = _igvn;
2012   if (EliminateLocks) {
2013     // Mark locks before changing ideal graph.
2014     int cnt = C->macro_count();
2015     for (int i = 0; i < cnt; i++) {
2016       Node *n = C->macro_node(i);
2017       if (n->is_AbstractLock()) { // Lock and Unlock nodes
2018         AbstractLockNode* alock = n->as_AbstractLock();
2019         if (!alock->is_non_esc_obj()) {
2020           const Type* obj_type = igvn->type(alock->obj_node());
2021           if (not_global_escape(alock->obj_node()) &&
2022               !obj_type->isa_inlinetype() && !obj_type->is_inlinetypeptr()) {
2023             assert(!alock->is_eliminated() || alock->is_coarsened(), "sanity");
2024             // The lock could be marked eliminated by lock coarsening
2025             // code during first IGVN before EA. Replace coarsened flag
2026             // to eliminate all associated locks/unlocks.
2027 #ifdef ASSERT
2028             alock->log_lock_optimization(C, "eliminate_lock_set_non_esc3");
2029 #endif
2030             alock->set_non_esc_obj();
2031           }
2032         }
2033       }
2034     }
2035   }
2036 
2037   if (OptimizePtrCompare) {
2038     for (int i = 0; i < ptr_cmp_worklist.length(); i++) {
2039       Node *n = ptr_cmp_worklist.at(i);
2040       const TypeInt* tcmp = optimize_ptr_compare(n);
2041       if (tcmp->singleton()) {
2042         Node* cmp = igvn->makecon(tcmp);
2043 #ifndef PRODUCT
2044         if (PrintOptimizePtrCompare) {
2045           tty->print_cr("++++ Replaced: %d %s(%d,%d) --> %s", n->_idx, (n->Opcode() == Op_CmpP ? "CmpP" : "CmpN"), n->in(1)->_idx, n->in(2)->_idx, (tcmp == TypeInt::CC_EQ ? "EQ" : "NotEQ"));
2046           if (Verbose) {
2047             n->dump(1);
2048           }
2049         }
2050 #endif
2051         igvn->replace_node(n, cmp);
2052       }
2053     }
2054   }
2055 
2056   // For MemBarStoreStore nodes added in library_call.cpp, check
2057   // escape status of associated AllocateNode and optimize out
2058   // MemBarStoreStore node if the allocated object never escapes.
2059   for (int i = 0; i < storestore_worklist.length(); i++) {
2060     Node* storestore = storestore_worklist.at(i);
2061     Node* alloc = storestore->in(MemBarNode::Precedent)->in(0);
2062     if (alloc->is_Allocate() && not_global_escape(alloc)) {
2063       if (alloc->in(AllocateNode::InlineTypeNode) != NULL) {
2064         // Non-escaping inline type buffer allocations don't require a membar
2065         storestore->as_MemBar()->remove(_igvn);
2066       } else {
2067         MemBarNode* mb = MemBarNode::make(C, Op_MemBarCPUOrder, Compile::AliasIdxBot);
2068         mb->init_req(TypeFunc::Memory,  storestore->in(TypeFunc::Memory));
2069         mb->init_req(TypeFunc::Control, storestore->in(TypeFunc::Control));
2070         igvn->register_new_node_with_optimizer(mb);
2071         igvn->replace_node(storestore, mb);
2072       }
2073     }
2074   }
2075 }
2076 
2077 // Optimize objects compare.
2078 const TypeInt* ConnectionGraph::optimize_ptr_compare(Node* n) {
2079   assert(OptimizePtrCompare, "sanity");
2080   assert(n->Opcode() == Op_CmpN || n->Opcode() == Op_CmpP, "must be");
2081   const TypeInt* EQ = TypeInt::CC_EQ; // [0] == ZERO
2082   const TypeInt* NE = TypeInt::CC_GT; // [1] == ONE
2083   const TypeInt* UNKNOWN = TypeInt::CC;    // [-1, 0,1]
2084 
2085   PointsToNode* ptn1 = ptnode_adr(n->in(1)->_idx);
2086   PointsToNode* ptn2 = ptnode_adr(n->in(2)->_idx);
2087   JavaObjectNode* jobj1 = unique_java_object(n->in(1));
2088   JavaObjectNode* jobj2 = unique_java_object(n->in(2));
2089   assert(ptn1->is_JavaObject() || ptn1->is_LocalVar(), "sanity");
2090   assert(ptn2->is_JavaObject() || ptn2->is_LocalVar(), "sanity");
2091 
2092   // Check simple cases first.

2205   assert(!src->is_Field() && !dst->is_Field(), "only for JavaObject and LocalVar");
2206   assert((src != null_obj) && (dst != null_obj), "not for ConP NULL");
2207   PointsToNode* ptadr = _nodes.at(n->_idx);
2208   if (ptadr != NULL) {
2209     assert(ptadr->is_Arraycopy() && ptadr->ideal_node() == n, "sanity");
2210     return;
2211   }
2212   Compile* C = _compile;
2213   ptadr = new (C->comp_arena()) ArraycopyNode(this, n, es);
2214   map_ideal_node(n, ptadr);
2215   // Add edge from arraycopy node to source object.
2216   (void)add_edge(ptadr, src);
2217   src->set_arraycopy_src();
2218   // Add edge from destination object to arraycopy node.
2219   (void)add_edge(dst, ptadr);
2220   dst->set_arraycopy_dst();
2221 }
2222 
2223 bool ConnectionGraph::is_oop_field(Node* n, int offset, bool* unsafe) {
2224   const Type* adr_type = n->as_AddP()->bottom_type();
2225   int field_offset = adr_type->isa_aryptr() ? adr_type->isa_aryptr()->field_offset().get() : Type::OffsetBot;
2226   BasicType bt = T_INT;
2227   if (offset == Type::OffsetBot && field_offset == Type::OffsetBot) {
2228     // Check only oop fields.
2229     if (!adr_type->isa_aryptr() ||
2230         (adr_type->isa_aryptr()->klass() == NULL) ||
2231          adr_type->isa_aryptr()->klass()->is_obj_array_klass()) {
2232       // OffsetBot is used to reference array's element. Ignore first AddP.
2233       if (find_second_addp(n, n->in(AddPNode::Base)) == NULL) {
2234         bt = T_OBJECT;
2235       }
2236     }
2237   } else if (offset != oopDesc::klass_offset_in_bytes()) {
2238     if (adr_type->isa_instptr()) {
2239       ciField* field = _compile->alias_type(adr_type->is_ptr())->field();
2240       if (field != NULL) {
2241         bt = field->layout_type();
2242       } else {
2243         // Check for unsafe oop field access
2244         if (n->has_out_with(Op_StoreP, Op_LoadP, Op_StoreN, Op_LoadN) ||
2245             n->has_out_with(Op_GetAndSetP, Op_GetAndSetN, Op_CompareAndExchangeP, Op_CompareAndExchangeN) ||
2246             n->has_out_with(Op_CompareAndSwapP, Op_CompareAndSwapN, Op_WeakCompareAndSwapP, Op_WeakCompareAndSwapN) ||
2247             BarrierSet::barrier_set()->barrier_set_c2()->escape_has_out_with_unsafe_object(n)) {
2248           bt = T_OBJECT;
2249           (*unsafe) = true;
2250         }
2251       }
2252     } else if (adr_type->isa_aryptr()) {
2253       if (offset == arrayOopDesc::length_offset_in_bytes()) {
2254         // Ignore array length load.
2255       } else if (find_second_addp(n, n->in(AddPNode::Base)) != NULL) {
2256         // Ignore first AddP.
2257       } else {
2258         const Type* elemtype = adr_type->isa_aryptr()->elem();
2259         if (elemtype->isa_inlinetype() && field_offset != Type::OffsetBot) {
2260           ciInlineKlass* vk = elemtype->inline_klass();
2261           field_offset += vk->first_field_offset();
2262           bt = vk->get_field_by_offset(field_offset, false)->layout_type();
2263         } else {
2264           bt = elemtype->array_element_basic_type();
2265         }
2266       }
2267     } else if (adr_type->isa_rawptr() || adr_type->isa_klassptr()) {
2268       // Allocation initialization, ThreadLocal field access, unsafe access
2269       if (n->has_out_with(Op_StoreP, Op_LoadP, Op_StoreN, Op_LoadN) ||
2270           n->has_out_with(Op_GetAndSetP, Op_GetAndSetN, Op_CompareAndExchangeP, Op_CompareAndExchangeN) ||
2271           n->has_out_with(Op_CompareAndSwapP, Op_CompareAndSwapN, Op_WeakCompareAndSwapP, Op_WeakCompareAndSwapN) ||
2272           BarrierSet::barrier_set()->barrier_set_c2()->escape_has_out_with_unsafe_object(n)) {
2273         bt = T_OBJECT;
2274       }
2275     }
2276   }
2277   // Note: T_NARROWOOP is not classed as a real reference type
2278   return (is_reference_type(bt) || bt == T_NARROWOOP);
2279 }
2280 
2281 // Returns unique pointed java object or NULL.
2282 JavaObjectNode* ConnectionGraph::unique_java_object(Node *n) {
2283   assert(!_collecting, "should not call when constructed graph");
2284   // If the node was created after the escape computation we can't answer.
2285   uint idx = n->_idx;

2429             return true;
2430           }
2431         }
2432       }
2433     }
2434   }
2435   return false;
2436 }
2437 
2438 int ConnectionGraph::address_offset(Node* adr, PhaseTransform *phase) {
2439   const Type *adr_type = phase->type(adr);
2440   if (adr->is_AddP() && adr_type->isa_oopptr() == NULL && is_captured_store_address(adr)) {
2441     // We are computing a raw address for a store captured by an Initialize
2442     // compute an appropriate address type. AddP cases #3 and #5 (see below).
2443     int offs = (int)phase->find_intptr_t_con(adr->in(AddPNode::Offset), Type::OffsetBot);
2444     assert(offs != Type::OffsetBot ||
2445            adr->in(AddPNode::Address)->in(0)->is_AllocateArray(),
2446            "offset must be a constant or it is initialization of array");
2447     return offs;
2448   }
2449   return adr_type->is_ptr()->flattened_offset();


2450 }
2451 
2452 Node* ConnectionGraph::get_addp_base(Node *addp) {
2453   assert(addp->is_AddP(), "must be AddP");
2454   //
2455   // AddP cases for Base and Address inputs:
2456   // case #1. Direct object's field reference:
2457   //     Allocate
2458   //       |
2459   //     Proj #5 ( oop result )
2460   //       |
2461   //     CheckCastPP (cast to instance type)
2462   //      | |
2463   //     AddP  ( base == address )
2464   //
2465   // case #2. Indirect object's field reference:
2466   //      Phi
2467   //       |
2468   //     CastPP (cast to instance type)
2469   //      | |

2583   }
2584   return NULL;
2585 }
2586 
2587 //
2588 // Adjust the type and inputs of an AddP which computes the
2589 // address of a field of an instance
2590 //
2591 bool ConnectionGraph::split_AddP(Node *addp, Node *base) {
2592   PhaseGVN* igvn = _igvn;
2593   const TypeOopPtr *base_t = igvn->type(base)->isa_oopptr();
2594   assert(base_t != NULL && base_t->is_known_instance(), "expecting instance oopptr");
2595   const TypeOopPtr *t = igvn->type(addp)->isa_oopptr();
2596   if (t == NULL) {
2597     // We are computing a raw address for a store captured by an Initialize
2598     // compute an appropriate address type (cases #3 and #5).
2599     assert(igvn->type(addp) == TypeRawPtr::NOTNULL, "must be raw pointer");
2600     assert(addp->in(AddPNode::Address)->is_Proj(), "base of raw address must be result projection from allocation");
2601     intptr_t offs = (int)igvn->find_intptr_t_con(addp->in(AddPNode::Offset), Type::OffsetBot);
2602     assert(offs != Type::OffsetBot, "offset must be a constant");
2603     if (base_t->isa_aryptr() != NULL) {
2604       // In the case of a flattened inline type array, each field has its
2605       // own slice so we need to extract the field being accessed from
2606       // the address computation
2607       t = base_t->isa_aryptr()->add_field_offset_and_offset(offs)->is_oopptr();
2608     } else {
2609       t = base_t->add_offset(offs)->is_oopptr();
2610     }
2611   }
2612   int inst_id = base_t->instance_id();
2613   assert(!t->is_known_instance() || t->instance_id() == inst_id,
2614                              "old type must be non-instance or match new type");
2615 
2616   // The type 't' could be subclass of 'base_t'.
2617   // As result t->offset() could be large then base_t's size and it will
2618   // cause the failure in add_offset() with narrow oops since TypeOopPtr()
2619   // constructor verifies correctness of the offset.
2620   //
2621   // It could happened on subclass's branch (from the type profiling
2622   // inlining) which was not eliminated during parsing since the exactness
2623   // of the allocation type was not propagated to the subclass type check.
2624   //
2625   // Or the type 't' could be not related to 'base_t' at all.
2626   // It could happen when CHA type is different from MDO type on a dead path
2627   // (for example, from instanceof check) which is not collapsed during parsing.
2628   //
2629   // Do nothing for such AddP node and don't process its users since
2630   // this code branch will go away.
2631   //
2632   if (!t->is_known_instance() &&
2633       !base_t->klass()->is_subtype_of(t->klass())) {
2634      return false; // bail out
2635   }
2636   const TypePtr* tinst = base_t->add_offset(t->offset());
2637   if (tinst->isa_aryptr() && t->isa_aryptr()) {
2638     // In the case of a flattened inline type array, each field has its
2639     // own slice so we need to keep track of the field being accessed.
2640     tinst = tinst->is_aryptr()->with_field_offset(t->is_aryptr()->field_offset().get());
2641     // Keep array properties (not flat/null-free)
2642     tinst = tinst->is_aryptr()->update_properties(t->is_aryptr());
2643     if (tinst == NULL) {
2644       return false; // Skip dead path with inconsistent properties
2645     }
2646   }
2647 
2648   // Do NOT remove the next line: ensure a new alias index is allocated
2649   // for the instance type. Note: C++ will not remove it since the call
2650   // has side effect.
2651   int alias_idx = _compile->get_alias_index(tinst);
2652   igvn->set_type(addp, tinst);
2653   // record the allocation in the node map
2654   set_map(addp, get_map(base->_idx));
2655   // Set addp's Base and Address to 'base'.
2656   Node *abase = addp->in(AddPNode::Base);
2657   Node *adr   = addp->in(AddPNode::Address);
2658   if (adr->is_Proj() && adr->in(0)->is_Allocate() &&
2659       adr->in(0)->_idx == (uint)inst_id) {
2660     // Skip AddP cases #3 and #5.
2661   } else {
2662     assert(!abase->is_top(), "sanity"); // AddP case #3
2663     if (abase != base) {
2664       igvn->hash_delete(addp);
2665       addp->set_req(AddPNode::Base, base);
2666       if (abase == adr) {
2667         addp->set_req(AddPNode::Address, base);

3309         ptnode_adr(n->_idx)->dump();
3310         assert(jobj != NULL && jobj != phantom_obj, "escaped allocation");
3311 #endif
3312         _compile->record_failure(C2Compiler::retry_no_escape_analysis());
3313         return;
3314       } else {
3315         Node *val = get_map(jobj->idx());   // CheckCastPP node
3316         TypeNode *tn = n->as_Type();
3317         const TypeOopPtr* tinst = igvn->type(val)->isa_oopptr();
3318         assert(tinst != NULL && tinst->is_known_instance() &&
3319                tinst->instance_id() == jobj->idx() , "instance type expected.");
3320 
3321         const Type *tn_type = igvn->type(tn);
3322         const TypeOopPtr *tn_t;
3323         if (tn_type->isa_narrowoop()) {
3324           tn_t = tn_type->make_ptr()->isa_oopptr();
3325         } else {
3326           tn_t = tn_type->isa_oopptr();
3327         }
3328         if (tn_t != NULL && tinst->klass()->is_subtype_of(tn_t->klass())) {
3329           if (tn_t->isa_aryptr()) {
3330             // Keep array properties (not flat/null-free)
3331             tinst = tinst->is_aryptr()->update_properties(tn_t->is_aryptr());
3332             if (tinst == NULL) {
3333               continue; // Skip dead path with inconsistent properties
3334             }
3335           }
3336           if (tn_type->isa_narrowoop()) {
3337             tn_type = tinst->make_narrowoop();
3338           } else {
3339             tn_type = tinst;
3340           }
3341           igvn->hash_delete(tn);
3342           igvn->set_type(tn, tn_type);
3343           tn->set_type(tn_type);
3344           igvn->hash_insert(tn);
3345           record_for_optimizer(n);
3346         } else {
3347           assert(tn_type == TypePtr::NULL_PTR ||
3348                  tn_t != NULL && !tinst->klass()->is_subtype_of(tn_t->klass()),
3349                  "unexpected type");
3350           continue; // Skip dead path with different type
3351         }
3352       }
3353     } else {
3354       debug_only(n->dump();)
3355       assert(false, "EA: unexpected node");
3356       continue;
3357     }
3358     // push allocation's users on appropriate worklist
3359     for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
3360       Node *use = n->fast_out(i);
3361       if (use->is_Mem() && use->in(MemNode::Address) == n) {
3362         // Load/store to instance's field
3363         memnode_worklist.append_if_missing(use);
3364       } else if (use->is_MemBar()) {
3365         if (use->in(TypeFunc::Memory) == n) { // Ignore precedent edge
3366           memnode_worklist.append_if_missing(use);
3367         }
3368       } else if (use->is_AddP() && use->outcnt() > 0) { // No dead nodes
3369         Node* addp2 = find_second_addp(use, n);
3370         if (addp2 != NULL) {
3371           alloc_worklist.append_if_missing(addp2);
3372         }
3373         alloc_worklist.append_if_missing(use);
3374       } else if (use->is_Phi() ||
3375                  use->is_CheckCastPP() ||
3376                  use->is_EncodeNarrowPtr() ||
3377                  use->is_DecodeNarrowPtr() ||
3378                  (use->is_ConstraintCast() && use->Opcode() == Op_CastPP)) {
3379         alloc_worklist.append_if_missing(use);
3380 #ifdef ASSERT
3381       } else if (use->is_Mem()) {
3382         assert(use->in(MemNode::Address) != n, "EA: missing allocation reference path");
3383       } else if (use->is_MergeMem()) {
3384         assert(mergemem_worklist.contains(use->as_MergeMem()), "EA: missing MergeMem node in the worklist");
3385       } else if (use->is_SafePoint()) {
3386         // Look for MergeMem nodes for calls which reference unique allocation
3387         // (through CheckCastPP nodes) even for debug info.
3388         Node* m = use->in(TypeFunc::Memory);
3389         if (m->is_MergeMem()) {
3390           assert(mergemem_worklist.contains(m->as_MergeMem()), "EA: missing MergeMem node in the worklist");
3391         }
3392       } else if (use->Opcode() == Op_EncodeISOArray) {
3393         if (use->in(MemNode::Memory) == n || use->in(3) == n) {
3394           // EncodeISOArray overwrites destination array
3395           memnode_worklist.append_if_missing(use);
3396         }
3397       } else if (use->Opcode() == Op_Return) {
3398         // Allocation is referenced by field of returned inline type
3399         assert(_compile->tf()->returns_inline_type_as_fields(), "EA: unexpected reference by ReturnNode");
3400       } else {
3401         uint op = use->Opcode();
3402         if ((op == Op_StrCompressedCopy || op == Op_StrInflatedCopy) &&
3403             (use->in(MemNode::Memory) == n)) {
3404           // They overwrite memory edge corresponding to destination array,
3405           memnode_worklist.append_if_missing(use);
3406         } else if (!(op == Op_CmpP || op == Op_Conv2B ||
3407               op == Op_CastP2X || op == Op_StoreCM ||
3408               op == Op_FastLock || op == Op_AryEq || op == Op_StrComp || op == Op_HasNegatives ||
3409               op == Op_StrCompressedCopy || op == Op_StrInflatedCopy ||
3410               op == Op_StrEquals || op == Op_StrIndexOf || op == Op_StrIndexOfChar ||
3411               op == Op_SubTypeCheck || op == Op_InlineType || op == Op_InlineTypePtr || op == Op_FlatArrayCheck ||
3412               BarrierSet::barrier_set()->barrier_set_c2()->is_gc_barrier_node(use))) {
3413           n->dump();
3414           use->dump();
3415           assert(false, "EA: missing allocation reference path");
3416         }
3417 #endif
3418       }
3419     }
3420 
3421   }
3422 
3423   // Go over all ArrayCopy nodes and if one of the inputs has a unique
3424   // type, record it in the ArrayCopy node so we know what memory this
3425   // node uses/modified.
3426   for (int next = 0; next < arraycopy_worklist.length(); next++) {
3427     ArrayCopyNode* ac = arraycopy_worklist.at(next);
3428     Node* dest = ac->in(ArrayCopyNode::Dest);
3429     if (dest->is_AddP()) {
3430       dest = get_addp_base(dest);
3431     }

3461   if (memnode_worklist.length() == 0)
3462     return;  // nothing to do
3463   while (memnode_worklist.length() != 0) {
3464     Node *n = memnode_worklist.pop();
3465     if (visited.test_set(n->_idx)) {
3466       continue;
3467     }
3468     if (n->is_Phi() || n->is_ClearArray()) {
3469       // we don't need to do anything, but the users must be pushed
3470     } else if (n->is_MemBar()) { // Initialize, MemBar nodes
3471       // we don't need to do anything, but the users must be pushed
3472       n = n->as_MemBar()->proj_out_or_null(TypeFunc::Memory);
3473       if (n == NULL) {
3474         continue;
3475       }
3476     } else if (n->Opcode() == Op_StrCompressedCopy ||
3477                n->Opcode() == Op_EncodeISOArray) {
3478       // get the memory projection
3479       n = n->find_out_with(Op_SCMemProj);
3480       assert(n != NULL && n->Opcode() == Op_SCMemProj, "memory projection required");
3481     } else if (n->is_CallLeaf() && n->as_CallLeaf()->_name != NULL &&
3482                strcmp(n->as_CallLeaf()->_name, "store_unknown_inline") == 0) {
3483       n = n->as_CallLeaf()->proj_out(TypeFunc::Memory);
3484     } else {
3485       assert(n->is_Mem(), "memory node required.");
3486       Node *addr = n->in(MemNode::Address);
3487       const Type *addr_t = igvn->type(addr);
3488       if (addr_t == Type::TOP) {
3489         continue;
3490       }
3491       assert (addr_t->isa_ptr() != NULL, "pointer type required.");
3492       int alias_idx = _compile->get_alias_index(addr_t->is_ptr());
3493       assert ((uint)alias_idx < new_index_end, "wrong alias index");
3494       Node *mem = find_inst_mem(n->in(MemNode::Memory), alias_idx, orig_phis);
3495       if (_compile->failing()) {
3496         return;
3497       }
3498       if (mem != n->in(MemNode::Memory)) {
3499         // We delay the memory edge update since we need old one in
3500         // MergeMem code below when instances memory slices are separated.
3501         set_map(n, mem);
3502       }
3503       if (n->is_Load()) {

3506         // get the memory projection
3507         n = n->find_out_with(Op_SCMemProj);
3508         assert(n != NULL && n->Opcode() == Op_SCMemProj, "memory projection required");
3509       }
3510     }
3511     // push user on appropriate worklist
3512     for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
3513       Node *use = n->fast_out(i);
3514       if (use->is_Phi() || use->is_ClearArray()) {
3515         memnode_worklist.append_if_missing(use);
3516       } else if (use->is_Mem() && use->in(MemNode::Memory) == n) {
3517         if (use->Opcode() == Op_StoreCM) { // Ignore cardmark stores
3518           continue;
3519         }
3520         memnode_worklist.append_if_missing(use);
3521       } else if (use->is_MemBar()) {
3522         if (use->in(TypeFunc::Memory) == n) { // Ignore precedent edge
3523           memnode_worklist.append_if_missing(use);
3524         }
3525 #ifdef ASSERT
3526       } else if (use->is_Mem()) {
3527         assert(use->in(MemNode::Memory) != n, "EA: missing memory path");
3528       } else if (use->is_MergeMem()) {
3529         assert(mergemem_worklist.contains(use->as_MergeMem()), "EA: missing MergeMem node in the worklist");
3530       } else if (use->Opcode() == Op_EncodeISOArray) {
3531         if (use->in(MemNode::Memory) == n || use->in(3) == n) {
3532           // EncodeISOArray overwrites destination array
3533           memnode_worklist.append_if_missing(use);
3534         }
3535       } else if (use->is_CallLeaf() && use->as_CallLeaf()->_name != NULL &&
3536                  strcmp(use->as_CallLeaf()->_name, "store_unknown_inline") == 0) {
3537         // store_unknown_inline overwrites destination array
3538         memnode_worklist.append_if_missing(use);
3539       } else {
3540         uint op = use->Opcode();
3541         if ((use->in(MemNode::Memory) == n) &&
3542             (op == Op_StrCompressedCopy || op == Op_StrInflatedCopy)) {
3543           // They overwrite memory edge corresponding to destination array,
3544           memnode_worklist.append_if_missing(use);
3545         } else if (!(BarrierSet::barrier_set()->barrier_set_c2()->is_gc_barrier_node(use) ||
3546               op == Op_AryEq || op == Op_StrComp || op == Op_HasNegatives ||
3547               op == Op_StrCompressedCopy || op == Op_StrInflatedCopy ||
3548               op == Op_StrEquals || op == Op_StrIndexOf || op == Op_StrIndexOfChar || op == Op_FlatArrayCheck)) {
3549           n->dump();
3550           use->dump();
3551           assert(false, "EA: missing memory path");
3552         }
3553 #endif
3554       }
3555     }
3556   }
3557 
3558   //  Phase 3:  Process MergeMem nodes from mergemem_worklist.
3559   //            Walk each memory slice moving the first node encountered of each
3560   //            instance type to the input corresponding to its alias index.
3561   uint length = mergemem_worklist.length();
3562   for( uint next = 0; next < length; ++next ) {
3563     MergeMemNode* nmm = mergemem_worklist.at(next);
3564     assert(!visited.test_set(nmm->_idx), "should not be visited before");
3565     // Note: we don't want to use MergeMemStream here because we only want to
3566     // scan inputs which exist at the start, not ones we add during processing.
3567     // Note 2: MergeMem may already contains instance memory slices added
3568     // during find_inst_mem() call when memory nodes were processed above.
3569     igvn->hash_delete(nmm);
3570     uint nslices = MIN2(nmm->req(), new_index_start);
3571     for (uint i = Compile::AliasIdxRaw+1; i < nslices; i++) {
3572       Node* mem = nmm->in(i);
3573       Node* cur = NULL;
3574       if (mem == NULL || mem->is_top()) {
3575         continue;
3576       }
3577       // First, update mergemem by moving memory nodes to corresponding slices
3578       // if their type became more precise since this mergemem was created.
3579       while (mem->is_Mem()) {
3580         const Type *at = igvn->type(mem->in(MemNode::Address));

3615       Node* result = step_through_mergemem(nmm, ni, tinst);
3616       if (result == nmm->base_memory()) {
3617         // Didn't find instance memory, search through general slice recursively.
3618         result = nmm->memory_at(_compile->get_general_index(ni));
3619         result = find_inst_mem(result, ni, orig_phis);
3620         if (_compile->failing()) {
3621           return;
3622         }
3623         nmm->set_memory_at(ni, result);
3624       }
3625     }
3626     igvn->hash_insert(nmm);
3627     record_for_optimizer(nmm);
3628   }
3629 
3630   //  Phase 4:  Update the inputs of non-instance memory Phis and
3631   //            the Memory input of memnodes
3632   // First update the inputs of any non-instance Phi's from
3633   // which we split out an instance Phi.  Note we don't have
3634   // to recursively process Phi's encountered on the input memory
3635   // chains as is done in split_memory_phi() since they will
3636   // also be processed here.
3637   for (int j = 0; j < orig_phis.length(); j++) {
3638     PhiNode *phi = orig_phis.at(j);
3639     int alias_idx = _compile->get_alias_index(phi->adr_type());
3640     igvn->hash_delete(phi);
3641     for (uint i = 1; i < phi->req(); i++) {
3642       Node *mem = phi->in(i);
3643       Node *new_mem = find_inst_mem(mem, alias_idx, orig_phis);
3644       if (_compile->failing()) {
3645         return;
3646       }
3647       if (mem != new_mem) {
3648         phi->set_req(i, new_mem);
3649       }
3650     }
3651     igvn->hash_insert(phi);
3652     record_for_optimizer(phi);
3653   }
3654 
3655   // Update the memory inputs of MemNodes with the value we computed
< prev index next >