< 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, int invocation) :
  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),

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










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

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

 482       // first IGVN optimization when escape information is still available.
 483       record_for_optimizer(n);
 484     } else if (n->is_Allocate()) {
 485       add_call_node(n->as_Call());
 486       record_for_optimizer(n);
 487     } else {
 488       if (n->is_CallStaticJava()) {
 489         const char* name = n->as_CallStaticJava()->_name;
 490         if (name != NULL && strcmp(name, "uncommon_trap") == 0) {
 491           return; // Skip uncommon traps
 492         }
 493       }
 494       // Don't mark as processed since call's arguments have to be processed.
 495       delayed_worklist->push(n);
 496       // Check if a call returns an object.
 497       if ((n->as_Call()->returns_pointer() &&
 498            n->as_Call()->proj_out_or_null(TypeFunc::Parms) != NULL) ||
 499           (n->is_CallStaticJava() &&
 500            n->as_CallStaticJava()->is_boxing_method())) {
 501         add_call_node(n->as_Call());











 502       }
 503     }
 504     return;
 505   }
 506   // Put this check here to process call arguments since some call nodes
 507   // point to phantom_obj.
 508   if (n_ptn == phantom_obj || n_ptn == null_obj) {
 509     return; // Skip predefined nodes.
 510   }
 511   switch (opcode) {
 512     case Op_AddP: {
 513       Node* base = get_addp_base(n);
 514       PointsToNode* ptn_base = ptnode_adr(base->_idx);
 515       // Field nodes are created for all field types. They are used in
 516       // adjust_scalar_replaceable_state() and split_unique_types().
 517       // Note, non-oop fields will have only base edges in Connection
 518       // Graph because such fields are not used for oop loads and stores.
 519       int offset = address_offset(n, igvn);
 520       add_field(n, PointsToNode::NoEscape, offset);
 521       if (ptn_base == NULL) {
 522         delayed_worklist->push(n); // Process it later.
 523       } else {
 524         n_ptn = ptnode_adr(n_idx);
 525         add_base(n_ptn->as_Field(), ptn_base);
 526       }
 527       break;
 528     }
 529     case Op_CastX2P: {
 530       map_ideal_node(n, phantom_obj);
 531       break;
 532     }

 533     case Op_CastPP:
 534     case Op_CheckCastPP:
 535     case Op_EncodeP:
 536     case Op_DecodeN:
 537     case Op_EncodePKlass:
 538     case Op_DecodeNKlass: {
 539       add_local_var_and_edge(n, PointsToNode::NoEscape, n->in(1), delayed_worklist);
 540       break;
 541     }
 542     case Op_CMoveP: {
 543       add_local_var(n, PointsToNode::NoEscape);
 544       // Do not add edges during first iteration because some could be
 545       // not defined yet.
 546       delayed_worklist->push(n);
 547       break;
 548     }
 549     case Op_ConP:
 550     case Op_ConN:
 551     case Op_ConNKlass: {
 552       // assume all oop constants globally escape except for null

 584     case Op_PartialSubtypeCheck: {
 585       // Produces Null or notNull and is used in only in CmpP so
 586       // phantom_obj could be used.
 587       map_ideal_node(n, phantom_obj); // Result is unknown
 588       break;
 589     }
 590     case Op_Phi: {
 591       // Using isa_ptr() instead of isa_oopptr() for LoadP and Phi because
 592       // ThreadLocal has RawPtr type.
 593       const Type* t = n->as_Phi()->type();
 594       if (t->make_ptr() != NULL) {
 595         add_local_var(n, PointsToNode::NoEscape);
 596         // Do not add edges during first iteration because some could be
 597         // not defined yet.
 598         delayed_worklist->push(n);
 599       }
 600       break;
 601     }
 602     case Op_Proj: {
 603       // we are only interested in the oop result projection from a call
 604       if (n->as_Proj()->_con == TypeFunc::Parms && n->in(0)->is_Call() &&
 605           n->in(0)->as_Call()->returns_pointer()) {


 606         add_local_var_and_edge(n, PointsToNode::NoEscape, n->in(0), delayed_worklist);
 607       }
 608       break;
 609     }
 610     case Op_Rethrow: // Exception object escapes
 611     case Op_Return: {
 612       if (n->req() > TypeFunc::Parms &&
 613           igvn->type(n->in(TypeFunc::Parms))->isa_oopptr()) {
 614         // Treat Return value as LocalVar with GlobalEscape escape state.
 615         add_local_var_and_edge(n, PointsToNode::GlobalEscape, n->in(TypeFunc::Parms), delayed_worklist);
 616       }
 617       break;
 618     }
 619     case Op_CompareAndExchangeP:
 620     case Op_CompareAndExchangeN:
 621     case Op_GetAndSetP:
 622     case Op_GetAndSetN: {
 623       add_objload_to_connection_graph(n, delayed_worklist);
 624       // fall-through
 625     }

 668   if (n->is_Call()) {
 669     process_call_arguments(n->as_Call());
 670     return;
 671   }
 672   assert(n->is_Store() || n->is_LoadStore() ||
 673          (n_ptn != NULL) && (n_ptn->ideal_node() != NULL),
 674          "node should be registered already");
 675   int opcode = n->Opcode();
 676   bool gc_handled = BarrierSet::barrier_set()->barrier_set_c2()->escape_add_final_edges(this, _igvn, n, opcode);
 677   if (gc_handled) {
 678     return; // Ignore node if already handled by GC.
 679   }
 680   switch (opcode) {
 681     case Op_AddP: {
 682       Node* base = get_addp_base(n);
 683       PointsToNode* ptn_base = ptnode_adr(base->_idx);
 684       assert(ptn_base != NULL, "field's base should be registered");
 685       add_base(n_ptn->as_Field(), ptn_base);
 686       break;
 687     }

 688     case Op_CastPP:
 689     case Op_CheckCastPP:
 690     case Op_EncodeP:
 691     case Op_DecodeN:
 692     case Op_EncodePKlass:
 693     case Op_DecodeNKlass: {
 694       add_local_var_and_edge(n, PointsToNode::NoEscape, n->in(1), NULL);
 695       break;
 696     }
 697     case Op_CMoveP: {
 698       for (uint i = CMoveNode::IfFalse; i < n->req(); i++) {
 699         Node* in = n->in(i);
 700         if (in == NULL) {
 701           continue;  // ignore NULL
 702         }
 703         Node* uncast_in = in->uncast();
 704         if (uncast_in->is_top() || uncast_in == n) {
 705           continue;  // ignore top or inputs which go back this node
 706         }
 707         PointsToNode* ptn = ptnode_adr(in->_idx);

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

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

 952     //
 953     //    - all oop arguments are escaping globally;
 954     //
 955     // 2. CallStaticJavaNode (execute bytecode analysis if possible):
 956     //
 957     //    - the same as CallDynamicJavaNode if can't do bytecode analysis;
 958     //
 959     //    - mapped to GlobalEscape JavaObject node if unknown oop is returned;
 960     //    - mapped to NoEscape JavaObject node if non-escaping object allocated
 961     //      during call is returned;
 962     //    - mapped to ArgEscape LocalVar node pointed to object arguments
 963     //      which are returned and does not escape during call;
 964     //
 965     //    - oop arguments escaping status is defined by bytecode analysis;
 966     //
 967     // For a static call, we know exactly what method is being called.
 968     // Use bytecode estimator to record whether the call's return value escapes.
 969     ciMethod* meth = call->as_CallJava()->method();
 970     if (meth == NULL) {
 971       const char* name = call->as_CallStaticJava()->_name;
 972       assert(strncmp(name, "_multianewarray", 15) == 0, "TODO: add failed case check");

 973       // Returns a newly allocated non-escaped object.
 974       add_java_object(call, PointsToNode::NoEscape);
 975       set_not_scalar_replaceable(ptnode_adr(call_idx) NOT_PRODUCT(COMMA "is result of multinewarray"));
 976     } else if (meth->is_boxing_method()) {
 977       // Returns boxing object
 978       PointsToNode::EscapeState es;
 979       vmIntrinsics::ID intr = meth->intrinsic_id();
 980       if (intr == vmIntrinsics::_floatValue || intr == vmIntrinsics::_doubleValue) {
 981         // It does not escape if object is always allocated.
 982         es = PointsToNode::NoEscape;
 983       } else {
 984         // It escapes globally if object could be loaded from cache.
 985         es = PointsToNode::GlobalEscape;
 986       }
 987       add_java_object(call, es);
 988     } else {
 989       BCEscapeAnalyzer* call_analyzer = meth->get_bcea();
 990       call_analyzer->copy_dependencies(_compile->dependencies());
 991       if (call_analyzer->is_return_allocated()) {
 992         // Returns a newly allocated non-escaped object, simply
 993         // update dependency information.
 994         // Mark it as NoEscape so that objects referenced by
 995         // it's fields will be marked as NoEscape at least.
 996         add_java_object(call, PointsToNode::NoEscape);
 997         set_not_scalar_replaceable(ptnode_adr(call_idx) NOT_PRODUCT(COMMA "is result of call"));
 998       } else {
 999         // Determine whether any arguments are returned.
1000         const TypeTuple* d = call->tf()->domain();
1001         bool ret_arg = false;
1002         for (uint i = TypeFunc::Parms; i < d->cnt(); i++) {
1003           if (d->field_at(i)->isa_ptr() != NULL &&
1004               call_analyzer->is_arg_returned(i - TypeFunc::Parms)) {
1005             ret_arg = true;
1006             break;
1007           }
1008         }
1009         if (ret_arg) {
1010           add_local_var(call, PointsToNode::ArgEscape);
1011         } else {
1012           // Returns unknown object.
1013           map_ideal_node(call, phantom_obj);
1014         }
1015       }
1016     }
1017   } else {
1018     // An other type of call, assume the worst case:
1019     // returned value is unknown and globally escapes.
1020     assert(call->Opcode() == Op_CallDynamicJava, "add failed case check");

1028 #ifdef ASSERT
1029     case Op_Allocate:
1030     case Op_AllocateArray:
1031     case Op_Lock:
1032     case Op_Unlock:
1033       assert(false, "should be done already");
1034       break;
1035 #endif
1036     case Op_ArrayCopy:
1037     case Op_CallLeafNoFP:
1038       // Most array copies are ArrayCopy nodes at this point but there
1039       // are still a few direct calls to the copy subroutines (See
1040       // PhaseStringOpts::copy_string())
1041       is_arraycopy = (call->Opcode() == Op_ArrayCopy) ||
1042         call->as_CallLeaf()->is_call_to_arraycopystub();
1043       // fall through
1044     case Op_CallLeafVector:
1045     case Op_CallLeaf: {
1046       // Stub calls, objects do not escape but they are not scale replaceable.
1047       // Adjust escape state for outgoing arguments.
1048       const TypeTuple * d = call->tf()->domain();
1049       bool src_has_oops = false;
1050       for (uint i = TypeFunc::Parms; i < d->cnt(); i++) {
1051         const Type* at = d->field_at(i);
1052         Node *arg = call->in(i);
1053         if (arg == NULL) {
1054           continue;
1055         }
1056         const Type *aat = _igvn->type(arg);
1057         if (arg->is_top() || !at->isa_ptr() || !aat->isa_ptr()) {
1058           continue;
1059         }
1060         if (arg->is_AddP()) {
1061           //
1062           // The inline_native_clone() case when the arraycopy stub is called
1063           // after the allocation before Initialize and CheckCastPP nodes.
1064           // Or normal arraycopy for object arrays case.
1065           //
1066           // Set AddP's base (Allocate) as not scalar replaceable since
1067           // pointer to the base (with offset) is passed as argument.
1068           //
1069           arg = get_addp_base(arg);
1070         }
1071         PointsToNode* arg_ptn = ptnode_adr(arg->_idx);
1072         assert(arg_ptn != NULL, "should be registered");
1073         PointsToNode::EscapeState arg_esc = arg_ptn->escape_state();
1074         if (is_arraycopy || arg_esc < PointsToNode::ArgEscape) {
1075           assert(aat == Type::TOP || aat == TypePtr::NULL_PTR ||
1076                  aat->isa_ptr() != NULL, "expecting an Ptr");
1077           bool arg_has_oops = aat->isa_oopptr() &&
1078                               (aat->isa_oopptr()->klass() == NULL || aat->isa_instptr() ||
1079                                (aat->isa_aryptr() && aat->isa_aryptr()->klass()->is_obj_array_klass()));



1080           if (i == TypeFunc::Parms) {
1081             src_has_oops = arg_has_oops;
1082           }
1083           //
1084           // src or dst could be j.l.Object when other is basic type array:
1085           //
1086           //   arraycopy(char[],0,Object*,0,size);
1087           //   arraycopy(Object*,0,char[],0,size);
1088           //
1089           // Don't add edges in such cases.
1090           //
1091           bool arg_is_arraycopy_dest = src_has_oops && is_arraycopy &&
1092                                        arg_has_oops && (i > TypeFunc::Parms);
1093 #ifdef ASSERT
1094           if (!(is_arraycopy ||
1095                 BarrierSet::barrier_set()->barrier_set_c2()->is_gc_barrier_node(call) ||
1096                 (call->as_CallLeaf()->_name != NULL &&
1097                  (strcmp(call->as_CallLeaf()->_name, "updateBytesCRC32") == 0 ||
1098                   strcmp(call->as_CallLeaf()->_name, "updateBytesCRC32C") == 0 ||
1099                   strcmp(call->as_CallLeaf()->_name, "updateBytesAdler32") == 0 ||

1106                   strcmp(call->as_CallLeaf()->_name, "counterMode_AESCrypt") == 0 ||
1107                   strcmp(call->as_CallLeaf()->_name, "galoisCounterMode_AESCrypt") == 0 ||
1108                   strcmp(call->as_CallLeaf()->_name, "ghash_processBlocks") == 0 ||
1109                   strcmp(call->as_CallLeaf()->_name, "encodeBlock") == 0 ||
1110                   strcmp(call->as_CallLeaf()->_name, "decodeBlock") == 0 ||
1111                   strcmp(call->as_CallLeaf()->_name, "md5_implCompress") == 0 ||
1112                   strcmp(call->as_CallLeaf()->_name, "md5_implCompressMB") == 0 ||
1113                   strcmp(call->as_CallLeaf()->_name, "sha1_implCompress") == 0 ||
1114                   strcmp(call->as_CallLeaf()->_name, "sha1_implCompressMB") == 0 ||
1115                   strcmp(call->as_CallLeaf()->_name, "sha256_implCompress") == 0 ||
1116                   strcmp(call->as_CallLeaf()->_name, "sha256_implCompressMB") == 0 ||
1117                   strcmp(call->as_CallLeaf()->_name, "sha512_implCompress") == 0 ||
1118                   strcmp(call->as_CallLeaf()->_name, "sha512_implCompressMB") == 0 ||
1119                   strcmp(call->as_CallLeaf()->_name, "sha3_implCompress") == 0 ||
1120                   strcmp(call->as_CallLeaf()->_name, "sha3_implCompressMB") == 0 ||
1121                   strcmp(call->as_CallLeaf()->_name, "multiplyToLen") == 0 ||
1122                   strcmp(call->as_CallLeaf()->_name, "squareToLen") == 0 ||
1123                   strcmp(call->as_CallLeaf()->_name, "mulAdd") == 0 ||
1124                   strcmp(call->as_CallLeaf()->_name, "montgomery_multiply") == 0 ||
1125                   strcmp(call->as_CallLeaf()->_name, "montgomery_square") == 0 ||



1126                   strcmp(call->as_CallLeaf()->_name, "bigIntegerRightShiftWorker") == 0 ||
1127                   strcmp(call->as_CallLeaf()->_name, "bigIntegerLeftShiftWorker") == 0 ||
1128                   strcmp(call->as_CallLeaf()->_name, "vectorizedMismatch") == 0 ||
1129                   strcmp(call->as_CallLeaf()->_name, "get_class_id_intrinsic") == 0)
1130                  ))) {
1131             call->dump();
1132             fatal("EA unexpected CallLeaf %s", call->as_CallLeaf()->_name);
1133           }
1134 #endif
1135           // Always process arraycopy's destination object since
1136           // we need to add all possible edges to references in
1137           // source object.
1138           if (arg_esc >= PointsToNode::ArgEscape &&
1139               !arg_is_arraycopy_dest) {
1140             continue;
1141           }
1142           PointsToNode::EscapeState es = PointsToNode::ArgEscape;
1143           if (call->is_ArrayCopy()) {
1144             ArrayCopyNode* ac = call->as_ArrayCopy();
1145             if (ac->is_clonebasic() ||

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

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

1625     PointsToNode* base = i.get();
1626     if (base->is_JavaObject()) {
1627       // Skip Allocate's fields which will be processed later.
1628       if (base->ideal_node()->is_Allocate()) {
1629         return 0;
1630       }
1631       assert(base == null_obj, "only NULL ptr base expected here");
1632     }
1633   }
1634   if (add_edge(field, phantom_obj)) {
1635     // New edge was added
1636     new_edges++;
1637     add_field_uses_to_worklist(field);
1638   }
1639   return new_edges;
1640 }
1641 
1642 // Find fields initializing values for allocations.
1643 int ConnectionGraph::find_init_values_phantom(JavaObjectNode* pta) {
1644   assert(pta->escape_state() == PointsToNode::NoEscape, "Not escaped Allocate nodes only");

1645   Node* alloc = pta->ideal_node();
1646 
1647   // Do nothing for Allocate nodes since its fields values are
1648   // "known" unless they are initialized by arraycopy/clone.
1649   if (alloc->is_Allocate() && !pta->arraycopy_dst()) {
1650     return 0;







1651   }
1652   assert(pta->arraycopy_dst() || alloc->as_CallStaticJava(), "sanity");

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

1657   }
1658 #endif
1659   // Non-escaped allocation returned from Java or runtime call have unknown values in fields.
1660   int new_edges = 0;
1661   for (EdgeIterator i(pta); i.has_next(); i.next()) {
1662     PointsToNode* field = i.get();
1663     if (field->is_Field() && field->as_Field()->is_oop()) {
1664       if (add_edge(field, phantom_obj)) {
1665         // New edge was added
1666         new_edges++;
1667         add_field_uses_to_worklist(field->as_Field());
1668       }
1669     }
1670   }
1671   return new_edges;
1672 }
1673 
1674 // Find fields initializing values for allocations.
1675 int ConnectionGraph::find_init_values_null(JavaObjectNode* pta, PhaseTransform* phase) {
1676   assert(pta->escape_state() == PointsToNode::NoEscape, "Not escaped Allocate nodes only");
1677   Node* alloc = pta->ideal_node();
1678   // Do nothing for Call nodes since its fields values are unknown.
1679   if (!alloc->is_Allocate()) {
1680     return 0;
1681   }
1682   InitializeNode* ini = alloc->as_Allocate()->initialization();
1683   bool visited_bottom_offset = false;
1684   GrowableArray<int> offsets_worklist;
1685   int new_edges = 0;
1686 
1687   // Check if an oop field's initializing value is recorded and add
1688   // a corresponding NULL if field's value if it is not recorded.
1689   // Connection Graph does not record a default initialization by NULL
1690   // captured by Initialize node.
1691   //
1692   for (EdgeIterator i(pta); i.has_next(); i.next()) {
1693     PointsToNode* field = i.get(); // Field (AddP)
1694     if (!field->is_Field() || !field->as_Field()->is_oop()) {
1695       continue; // Not oop field
1696     }
1697     int offset = field->as_Field()->offset();
1698     if (offset == Type::OffsetBot) {
1699       if (!visited_bottom_offset) {

1745               } else {
1746                 if (!val->is_LocalVar() || (val->edge_count() == 0)) {
1747                   tty->print_cr("----------init store has invalid value -----");
1748                   store->dump();
1749                   val->dump();
1750                   assert(val->is_LocalVar() && (val->edge_count() > 0), "should be processed already");
1751                 }
1752                 for (EdgeIterator j(val); j.has_next(); j.next()) {
1753                   PointsToNode* obj = j.get();
1754                   if (obj->is_JavaObject()) {
1755                     if (!field->points_to(obj->as_JavaObject())) {
1756                       missed_obj = obj;
1757                       break;
1758                     }
1759                   }
1760                 }
1761               }
1762               if (missed_obj != NULL) {
1763                 tty->print_cr("----------field---------------------------------");
1764                 field->dump();
1765                 tty->print_cr("----------missed referernce to object-----------");
1766                 missed_obj->dump();
1767                 tty->print_cr("----------object referernced by init store -----");
1768                 store->dump();
1769                 val->dump();
1770                 assert(!field->points_to(missed_obj->as_JavaObject()), "missed JavaObject reference");
1771               }
1772             }
1773 #endif
1774           } else {
1775             // There could be initializing stores which follow allocation.
1776             // For example, a volatile field store is not collected
1777             // by Initialize node.
1778             //
1779             // Need to check for dependent loads to separate such stores from
1780             // stores which follow loads. For now, add initial value NULL so
1781             // that compare pointers optimization works correctly.
1782           }
1783         }
1784         if (value == NULL) {
1785           // A field's initializing value was not recorded. Add NULL.
1786           if (add_edge(field, null_obj)) {
1787             // New edge was added

1979         assert(field->edge_count() > 0, "sanity");
1980       }
1981     }
1982   }
1983 }
1984 #endif
1985 
1986 // Optimize ideal graph.
1987 void ConnectionGraph::optimize_ideal_graph(GrowableArray<Node*>& ptr_cmp_worklist,
1988                                            GrowableArray<MemBarStoreStoreNode*>& storestore_worklist) {
1989   Compile* C = _compile;
1990   PhaseIterGVN* igvn = _igvn;
1991   if (EliminateLocks) {
1992     // Mark locks before changing ideal graph.
1993     int cnt = C->macro_count();
1994     for (int i = 0; i < cnt; i++) {
1995       Node *n = C->macro_node(i);
1996       if (n->is_AbstractLock()) { // Lock and Unlock nodes
1997         AbstractLockNode* alock = n->as_AbstractLock();
1998         if (!alock->is_non_esc_obj()) {
1999           if (not_global_escape(alock->obj_node())) {


2000             assert(!alock->is_eliminated() || alock->is_coarsened(), "sanity");
2001             // The lock could be marked eliminated by lock coarsening
2002             // code during first IGVN before EA. Replace coarsened flag
2003             // to eliminate all associated locks/unlocks.
2004 #ifdef ASSERT
2005             alock->log_lock_optimization(C, "eliminate_lock_set_non_esc3");
2006 #endif
2007             alock->set_non_esc_obj();
2008           }
2009         }
2010       }
2011     }
2012   }
2013 
2014   if (OptimizePtrCompare) {
2015     for (int i = 0; i < ptr_cmp_worklist.length(); i++) {
2016       Node *n = ptr_cmp_worklist.at(i);
2017       const TypeInt* tcmp = optimize_ptr_compare(n);
2018       if (tcmp->singleton()) {
2019         Node* cmp = igvn->makecon(tcmp);
2020 #ifndef PRODUCT
2021         if (PrintOptimizePtrCompare) {
2022           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"));
2023           if (Verbose) {
2024             n->dump(1);
2025           }
2026         }
2027 #endif
2028         igvn->replace_node(n, cmp);
2029       }
2030     }
2031   }
2032 
2033   // For MemBarStoreStore nodes added in library_call.cpp, check
2034   // escape status of associated AllocateNode and optimize out
2035   // MemBarStoreStore node if the allocated object never escapes.
2036   for (int i = 0; i < storestore_worklist.length(); i++) {
2037     Node* storestore = storestore_worklist.at(i);
2038     Node* alloc = storestore->in(MemBarNode::Precedent)->in(0);
2039     if (alloc->is_Allocate() && not_global_escape(alloc)) {
2040       MemBarNode* mb = MemBarNode::make(C, Op_MemBarCPUOrder, Compile::AliasIdxBot);
2041       mb->init_req(TypeFunc::Memory,  storestore->in(TypeFunc::Memory));
2042       mb->init_req(TypeFunc::Control, storestore->in(TypeFunc::Control));
2043       igvn->register_new_node_with_optimizer(mb);
2044       igvn->replace_node(storestore, mb);





2045     }
2046   }
2047 }
2048 
2049 // Optimize objects compare.
2050 const TypeInt* ConnectionGraph::optimize_ptr_compare(Node* n) {
2051   assert(OptimizePtrCompare, "sanity");
2052   assert(n->Opcode() == Op_CmpN || n->Opcode() == Op_CmpP, "must be");
2053   const TypeInt* EQ = TypeInt::CC_EQ; // [0] == ZERO
2054   const TypeInt* NE = TypeInt::CC_GT; // [1] == ONE
2055   const TypeInt* UNKNOWN = TypeInt::CC;    // [-1, 0,1]
2056 
2057   PointsToNode* ptn1 = ptnode_adr(n->in(1)->_idx);
2058   PointsToNode* ptn2 = ptnode_adr(n->in(2)->_idx);
2059   JavaObjectNode* jobj1 = unique_java_object(n->in(1));
2060   JavaObjectNode* jobj2 = unique_java_object(n->in(2));
2061   assert(ptn1->is_JavaObject() || ptn1->is_LocalVar(), "sanity");
2062   assert(ptn2->is_JavaObject() || ptn2->is_LocalVar(), "sanity");
2063 
2064   // Check simple cases first.

2177   assert(!src->is_Field() && !dst->is_Field(), "only for JavaObject and LocalVar");
2178   assert((src != null_obj) && (dst != null_obj), "not for ConP NULL");
2179   PointsToNode* ptadr = _nodes.at(n->_idx);
2180   if (ptadr != NULL) {
2181     assert(ptadr->is_Arraycopy() && ptadr->ideal_node() == n, "sanity");
2182     return;
2183   }
2184   Compile* C = _compile;
2185   ptadr = new (C->comp_arena()) ArraycopyNode(this, n, es);
2186   map_ideal_node(n, ptadr);
2187   // Add edge from arraycopy node to source object.
2188   (void)add_edge(ptadr, src);
2189   src->set_arraycopy_src();
2190   // Add edge from destination object to arraycopy node.
2191   (void)add_edge(dst, ptadr);
2192   dst->set_arraycopy_dst();
2193 }
2194 
2195 bool ConnectionGraph::is_oop_field(Node* n, int offset, bool* unsafe) {
2196   const Type* adr_type = n->as_AddP()->bottom_type();

2197   BasicType bt = T_INT;
2198   if (offset == Type::OffsetBot) {
2199     // Check only oop fields.
2200     if (!adr_type->isa_aryptr() ||
2201         (adr_type->isa_aryptr()->klass() == NULL) ||
2202          adr_type->isa_aryptr()->klass()->is_obj_array_klass()) {
2203       // OffsetBot is used to reference array's element. Ignore first AddP.
2204       if (find_second_addp(n, n->in(AddPNode::Base)) == NULL) {
2205         bt = T_OBJECT;
2206       }
2207     }
2208   } else if (offset != oopDesc::klass_offset_in_bytes()) {
2209     if (adr_type->isa_instptr()) {
2210       ciField* field = _compile->alias_type(adr_type->isa_instptr())->field();
2211       if (field != NULL) {
2212         bt = field->layout_type();
2213       } else {
2214         // Check for unsafe oop field access
2215         if (n->has_out_with(Op_StoreP, Op_LoadP, Op_StoreN, Op_LoadN) ||
2216             n->has_out_with(Op_GetAndSetP, Op_GetAndSetN, Op_CompareAndExchangeP, Op_CompareAndExchangeN) ||
2217             n->has_out_with(Op_CompareAndSwapP, Op_CompareAndSwapN, Op_WeakCompareAndSwapP, Op_WeakCompareAndSwapN) ||
2218             BarrierSet::barrier_set()->barrier_set_c2()->escape_has_out_with_unsafe_object(n)) {
2219           bt = T_OBJECT;
2220           (*unsafe) = true;
2221         }
2222       }
2223     } else if (adr_type->isa_aryptr()) {
2224       if (offset == arrayOopDesc::length_offset_in_bytes()) {
2225         // Ignore array length load.
2226       } else if (find_second_addp(n, n->in(AddPNode::Base)) != NULL) {
2227         // Ignore first AddP.
2228       } else {
2229         const Type* elemtype = adr_type->isa_aryptr()->elem();
2230         bt = elemtype->array_element_basic_type();






2231       }
2232     } else if (adr_type->isa_rawptr() || adr_type->isa_klassptr()) {
2233       // Allocation initialization, ThreadLocal field access, unsafe access
2234       if (n->has_out_with(Op_StoreP, Op_LoadP, Op_StoreN, Op_LoadN) ||
2235           n->has_out_with(Op_GetAndSetP, Op_GetAndSetN, Op_CompareAndExchangeP, Op_CompareAndExchangeN) ||
2236           n->has_out_with(Op_CompareAndSwapP, Op_CompareAndSwapN, Op_WeakCompareAndSwapP, Op_WeakCompareAndSwapN) ||
2237           BarrierSet::barrier_set()->barrier_set_c2()->escape_has_out_with_unsafe_object(n)) {
2238         bt = T_OBJECT;
2239       }
2240     }
2241   }
2242   // Note: T_NARROWOOP is not classed as a real reference type
2243   return (is_reference_type(bt) || bt == T_NARROWOOP);
2244 }
2245 
2246 // Returns unique pointed java object or NULL.
2247 JavaObjectNode* ConnectionGraph::unique_java_object(Node *n) {
2248   assert(!_collecting, "should not call when constructed graph");
2249   // If the node was created after the escape computation we can't answer.
2250   uint idx = n->_idx;

2394             return true;
2395           }
2396         }
2397       }
2398     }
2399   }
2400   return false;
2401 }
2402 
2403 int ConnectionGraph::address_offset(Node* adr, PhaseTransform *phase) {
2404   const Type *adr_type = phase->type(adr);
2405   if (adr->is_AddP() && adr_type->isa_oopptr() == NULL && is_captured_store_address(adr)) {
2406     // We are computing a raw address for a store captured by an Initialize
2407     // compute an appropriate address type. AddP cases #3 and #5 (see below).
2408     int offs = (int)phase->find_intptr_t_con(adr->in(AddPNode::Offset), Type::OffsetBot);
2409     assert(offs != Type::OffsetBot ||
2410            adr->in(AddPNode::Address)->in(0)->is_AllocateArray(),
2411            "offset must be a constant or it is initialization of array");
2412     return offs;
2413   }
2414   const TypePtr *t_ptr = adr_type->isa_ptr();
2415   assert(t_ptr != NULL, "must be a pointer type");
2416   return t_ptr->offset();
2417 }
2418 
2419 Node* ConnectionGraph::get_addp_base(Node *addp) {
2420   assert(addp->is_AddP(), "must be AddP");
2421   //
2422   // AddP cases for Base and Address inputs:
2423   // case #1. Direct object's field reference:
2424   //     Allocate
2425   //       |
2426   //     Proj #5 ( oop result )
2427   //       |
2428   //     CheckCastPP (cast to instance type)
2429   //      | |
2430   //     AddP  ( base == address )
2431   //
2432   // case #2. Indirect object's field reference:
2433   //      Phi
2434   //       |
2435   //     CastPP (cast to instance type)
2436   //      | |

2550   }
2551   return NULL;
2552 }
2553 
2554 //
2555 // Adjust the type and inputs of an AddP which computes the
2556 // address of a field of an instance
2557 //
2558 bool ConnectionGraph::split_AddP(Node *addp, Node *base) {
2559   PhaseGVN* igvn = _igvn;
2560   const TypeOopPtr *base_t = igvn->type(base)->isa_oopptr();
2561   assert(base_t != NULL && base_t->is_known_instance(), "expecting instance oopptr");
2562   const TypeOopPtr *t = igvn->type(addp)->isa_oopptr();
2563   if (t == NULL) {
2564     // We are computing a raw address for a store captured by an Initialize
2565     // compute an appropriate address type (cases #3 and #5).
2566     assert(igvn->type(addp) == TypeRawPtr::NOTNULL, "must be raw pointer");
2567     assert(addp->in(AddPNode::Address)->is_Proj(), "base of raw address must be result projection from allocation");
2568     intptr_t offs = (int)igvn->find_intptr_t_con(addp->in(AddPNode::Offset), Type::OffsetBot);
2569     assert(offs != Type::OffsetBot, "offset must be a constant");
2570     t = base_t->add_offset(offs)->is_oopptr();







2571   }
2572   int inst_id =  base_t->instance_id();
2573   assert(!t->is_known_instance() || t->instance_id() == inst_id,
2574                              "old type must be non-instance or match new type");
2575 
2576   // The type 't' could be subclass of 'base_t'.
2577   // As result t->offset() could be large then base_t's size and it will
2578   // cause the failure in add_offset() with narrow oops since TypeOopPtr()
2579   // constructor verifies correctness of the offset.
2580   //
2581   // It could happened on subclass's branch (from the type profiling
2582   // inlining) which was not eliminated during parsing since the exactness
2583   // of the allocation type was not propagated to the subclass type check.
2584   //
2585   // Or the type 't' could be not related to 'base_t' at all.
2586   // It could happened when CHA type is different from MDO type on a dead path
2587   // (for example, from instanceof check) which is not collapsed during parsing.
2588   //
2589   // Do nothing for such AddP node and don't process its users since
2590   // this code branch will go away.
2591   //
2592   if (!t->is_known_instance() &&
2593       !base_t->klass()->is_subtype_of(t->klass())) {
2594      return false; // bail out
2595   }
2596   const TypeOopPtr *tinst = base_t->add_offset(t->offset())->is_oopptr();











2597   // Do NOT remove the next line: ensure a new alias index is allocated
2598   // for the instance type. Note: C++ will not remove it since the call
2599   // has side effect.
2600   int alias_idx = _compile->get_alias_index(tinst);
2601   igvn->set_type(addp, tinst);
2602   // record the allocation in the node map
2603   set_map(addp, get_map(base->_idx));
2604   // Set addp's Base and Address to 'base'.
2605   Node *abase = addp->in(AddPNode::Base);
2606   Node *adr   = addp->in(AddPNode::Address);
2607   if (adr->is_Proj() && adr->in(0)->is_Allocate() &&
2608       adr->in(0)->_idx == (uint)inst_id) {
2609     // Skip AddP cases #3 and #5.
2610   } else {
2611     assert(!abase->is_top(), "sanity"); // AddP case #3
2612     if (abase != base) {
2613       igvn->hash_delete(addp);
2614       addp->set_req(AddPNode::Base, base);
2615       if (abase == adr) {
2616         addp->set_req(AddPNode::Address, base);

3258         ptnode_adr(n->_idx)->dump();
3259         assert(jobj != NULL && jobj != phantom_obj, "escaped allocation");
3260 #endif
3261         _compile->record_failure(_invocation > 0 ? C2Compiler::retry_no_iterative_escape_analysis() : C2Compiler::retry_no_escape_analysis());
3262         return;
3263       } else {
3264         Node *val = get_map(jobj->idx());   // CheckCastPP node
3265         TypeNode *tn = n->as_Type();
3266         const TypeOopPtr* tinst = igvn->type(val)->isa_oopptr();
3267         assert(tinst != NULL && tinst->is_known_instance() &&
3268                tinst->instance_id() == jobj->idx() , "instance type expected.");
3269 
3270         const Type *tn_type = igvn->type(tn);
3271         const TypeOopPtr *tn_t;
3272         if (tn_type->isa_narrowoop()) {
3273           tn_t = tn_type->make_ptr()->isa_oopptr();
3274         } else {
3275           tn_t = tn_type->isa_oopptr();
3276         }
3277         if (tn_t != NULL && tinst->klass()->is_subtype_of(tn_t->klass())) {







3278           if (tn_type->isa_narrowoop()) {
3279             tn_type = tinst->make_narrowoop();
3280           } else {
3281             tn_type = tinst;
3282           }
3283           igvn->hash_delete(tn);
3284           igvn->set_type(tn, tn_type);
3285           tn->set_type(tn_type);
3286           igvn->hash_insert(tn);
3287           record_for_optimizer(n);
3288         } else {
3289           assert(tn_type == TypePtr::NULL_PTR ||
3290                  tn_t != NULL && !tinst->klass()->is_subtype_of(tn_t->klass()),
3291                  "unexpected type");
3292           continue; // Skip dead path with different type
3293         }
3294       }
3295     } else {
3296       debug_only(n->dump();)
3297       assert(false, "EA: unexpected node");
3298       continue;
3299     }
3300     // push allocation's users on appropriate worklist
3301     for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
3302       Node *use = n->fast_out(i);
3303       if(use->is_Mem() && use->in(MemNode::Address) == n) {
3304         // Load/store to instance's field
3305         memnode_worklist.append_if_missing(use);
3306       } else if (use->is_MemBar()) {
3307         if (use->in(TypeFunc::Memory) == n) { // Ignore precedent edge
3308           memnode_worklist.append_if_missing(use);
3309         }
3310       } else if (use->is_AddP() && use->outcnt() > 0) { // No dead nodes
3311         Node* addp2 = find_second_addp(use, n);
3312         if (addp2 != NULL) {
3313           alloc_worklist.append_if_missing(addp2);
3314         }
3315         alloc_worklist.append_if_missing(use);
3316       } else if (use->is_Phi() ||
3317                  use->is_CheckCastPP() ||
3318                  use->is_EncodeNarrowPtr() ||
3319                  use->is_DecodeNarrowPtr() ||
3320                  (use->is_ConstraintCast() && use->Opcode() == Op_CastPP)) {
3321         alloc_worklist.append_if_missing(use);
3322 #ifdef ASSERT
3323       } else if (use->is_Mem()) {
3324         assert(use->in(MemNode::Address) != n, "EA: missing allocation reference path");
3325       } else if (use->is_MergeMem()) {
3326         assert(mergemem_worklist.contains(use->as_MergeMem()), "EA: missing MergeMem node in the worklist");
3327       } else if (use->is_SafePoint()) {
3328         // Look for MergeMem nodes for calls which reference unique allocation
3329         // (through CheckCastPP nodes) even for debug info.
3330         Node* m = use->in(TypeFunc::Memory);
3331         if (m->is_MergeMem()) {
3332           assert(mergemem_worklist.contains(m->as_MergeMem()), "EA: missing MergeMem node in the worklist");
3333         }
3334       } else if (use->Opcode() == Op_EncodeISOArray) {
3335         if (use->in(MemNode::Memory) == n || use->in(3) == n) {
3336           // EncodeISOArray overwrites destination array
3337           memnode_worklist.append_if_missing(use);
3338         }



3339       } else {
3340         uint op = use->Opcode();
3341         if ((op == Op_StrCompressedCopy || op == Op_StrInflatedCopy) &&
3342             (use->in(MemNode::Memory) == n)) {
3343           // They overwrite memory edge corresponding to destination array,
3344           memnode_worklist.append_if_missing(use);
3345         } else if (!(op == Op_CmpP || op == Op_Conv2B ||
3346               op == Op_CastP2X || op == Op_StoreCM ||
3347               op == Op_FastLock || op == Op_AryEq || op == Op_StrComp ||
3348               op == Op_CountPositives ||
3349               op == Op_StrCompressedCopy || op == Op_StrInflatedCopy ||
3350               op == Op_StrEquals || op == Op_StrIndexOf || op == Op_StrIndexOfChar ||
3351               op == Op_SubTypeCheck ||
3352               BarrierSet::barrier_set()->barrier_set_c2()->is_gc_barrier_node(use))) {
3353           n->dump();
3354           use->dump();
3355           assert(false, "EA: missing allocation reference path");
3356         }
3357 #endif
3358       }
3359     }
3360 
3361   }
3362 
3363   // Go over all ArrayCopy nodes and if one of the inputs has a unique
3364   // type, record it in the ArrayCopy node so we know what memory this
3365   // node uses/modified.
3366   for (int next = 0; next < arraycopy_worklist.length(); next++) {
3367     ArrayCopyNode* ac = arraycopy_worklist.at(next);
3368     Node* dest = ac->in(ArrayCopyNode::Dest);
3369     if (dest->is_AddP()) {
3370       dest = get_addp_base(dest);
3371     }

3401   if (memnode_worklist.length() == 0)
3402     return;  // nothing to do
3403   while (memnode_worklist.length() != 0) {
3404     Node *n = memnode_worklist.pop();
3405     if (visited.test_set(n->_idx)) {
3406       continue;
3407     }
3408     if (n->is_Phi() || n->is_ClearArray()) {
3409       // we don't need to do anything, but the users must be pushed
3410     } else if (n->is_MemBar()) { // Initialize, MemBar nodes
3411       // we don't need to do anything, but the users must be pushed
3412       n = n->as_MemBar()->proj_out_or_null(TypeFunc::Memory);
3413       if (n == NULL) {
3414         continue;
3415       }
3416     } else if (n->Opcode() == Op_StrCompressedCopy ||
3417                n->Opcode() == Op_EncodeISOArray) {
3418       // get the memory projection
3419       n = n->find_out_with(Op_SCMemProj);
3420       assert(n != NULL && n->Opcode() == Op_SCMemProj, "memory projection required");



3421     } else {
3422       assert(n->is_Mem(), "memory node required.");
3423       Node *addr = n->in(MemNode::Address);
3424       const Type *addr_t = igvn->type(addr);
3425       if (addr_t == Type::TOP) {
3426         continue;
3427       }
3428       assert (addr_t->isa_ptr() != NULL, "pointer type required.");
3429       int alias_idx = _compile->get_alias_index(addr_t->is_ptr());
3430       assert ((uint)alias_idx < new_index_end, "wrong alias index");
3431       Node *mem = find_inst_mem(n->in(MemNode::Memory), alias_idx, orig_phis);
3432       if (_compile->failing()) {
3433         return;
3434       }
3435       if (mem != n->in(MemNode::Memory)) {
3436         // We delay the memory edge update since we need old one in
3437         // MergeMem code below when instances memory slices are separated.
3438         set_map(n, mem);
3439       }
3440       if (n->is_Load()) {

3443         // get the memory projection
3444         n = n->find_out_with(Op_SCMemProj);
3445         assert(n != NULL && n->Opcode() == Op_SCMemProj, "memory projection required");
3446       }
3447     }
3448     // push user on appropriate worklist
3449     for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
3450       Node *use = n->fast_out(i);
3451       if (use->is_Phi() || use->is_ClearArray()) {
3452         memnode_worklist.append_if_missing(use);
3453       } else if (use->is_Mem() && use->in(MemNode::Memory) == n) {
3454         if (use->Opcode() == Op_StoreCM) { // Ignore cardmark stores
3455           continue;
3456         }
3457         memnode_worklist.append_if_missing(use);
3458       } else if (use->is_MemBar()) {
3459         if (use->in(TypeFunc::Memory) == n) { // Ignore precedent edge
3460           memnode_worklist.append_if_missing(use);
3461         }
3462 #ifdef ASSERT
3463       } else if(use->is_Mem()) {
3464         assert(use->in(MemNode::Memory) != n, "EA: missing memory path");
3465       } else if (use->is_MergeMem()) {
3466         assert(mergemem_worklist.contains(use->as_MergeMem()), "EA: missing MergeMem node in the worklist");
3467       } else if (use->Opcode() == Op_EncodeISOArray) {
3468         if (use->in(MemNode::Memory) == n || use->in(3) == n) {
3469           // EncodeISOArray overwrites destination array
3470           memnode_worklist.append_if_missing(use);
3471         }




3472       } else {
3473         uint op = use->Opcode();
3474         if ((use->in(MemNode::Memory) == n) &&
3475             (op == Op_StrCompressedCopy || op == Op_StrInflatedCopy)) {
3476           // They overwrite memory edge corresponding to destination array,
3477           memnode_worklist.append_if_missing(use);
3478         } else if (!(BarrierSet::barrier_set()->barrier_set_c2()->is_gc_barrier_node(use) ||
3479               op == Op_AryEq || op == Op_StrComp || op == Op_CountPositives ||
3480               op == Op_StrCompressedCopy || op == Op_StrInflatedCopy ||
3481               op == Op_StrEquals || op == Op_StrIndexOf || op == Op_StrIndexOfChar)) {
3482           n->dump();
3483           use->dump();
3484           assert(false, "EA: missing memory path");
3485         }
3486 #endif
3487       }
3488     }
3489   }
3490 
3491   //  Phase 3:  Process MergeMem nodes from mergemem_worklist.
3492   //            Walk each memory slice moving the first node encountered of each
3493   //            instance type to the the input corresponding to its alias index.
3494   uint length = mergemem_worklist.length();
3495   for( uint next = 0; next < length; ++next ) {
3496     MergeMemNode* nmm = mergemem_worklist.at(next);
3497     assert(!visited.test_set(nmm->_idx), "should not be visited before");
3498     // Note: we don't want to use MergeMemStream here because we only want to
3499     // scan inputs which exist at the start, not ones we add during processing.
3500     // Note 2: MergeMem may already contains instance memory slices added
3501     // during find_inst_mem() call when memory nodes were processed above.
3502     igvn->hash_delete(nmm);
3503     uint nslices = MIN2(nmm->req(), new_index_start);
3504     for (uint i = Compile::AliasIdxRaw+1; i < nslices; i++) {
3505       Node* mem = nmm->in(i);
3506       Node* cur = NULL;
3507       if (mem == NULL || mem->is_top()) {
3508         continue;
3509       }
3510       // First, update mergemem by moving memory nodes to corresponding slices
3511       // if their type became more precise since this mergemem was created.
3512       while (mem->is_Mem()) {
3513         const Type *at = igvn->type(mem->in(MemNode::Address));

3548       Node* result = step_through_mergemem(nmm, ni, tinst);
3549       if (result == nmm->base_memory()) {
3550         // Didn't find instance memory, search through general slice recursively.
3551         result = nmm->memory_at(_compile->get_general_index(ni));
3552         result = find_inst_mem(result, ni, orig_phis);
3553         if (_compile->failing()) {
3554           return;
3555         }
3556         nmm->set_memory_at(ni, result);
3557       }
3558     }
3559     igvn->hash_insert(nmm);
3560     record_for_optimizer(nmm);
3561   }
3562 
3563   //  Phase 4:  Update the inputs of non-instance memory Phis and
3564   //            the Memory input of memnodes
3565   // First update the inputs of any non-instance Phi's from
3566   // which we split out an instance Phi.  Note we don't have
3567   // to recursively process Phi's encountered on the input memory
3568   // chains as is done in split_memory_phi() since they  will
3569   // also be processed here.
3570   for (int j = 0; j < orig_phis.length(); j++) {
3571     PhiNode *phi = orig_phis.at(j);
3572     int alias_idx = _compile->get_alias_index(phi->adr_type());
3573     igvn->hash_delete(phi);
3574     for (uint i = 1; i < phi->req(); i++) {
3575       Node *mem = phi->in(i);
3576       Node *new_mem = find_inst_mem(mem, alias_idx, orig_phis);
3577       if (_compile->failing()) {
3578         return;
3579       }
3580       if (mem != new_mem) {
3581         phi->set_req(i, new_mem);
3582       }
3583     }
3584     igvn->hash_insert(phi);
3585     record_for_optimizer(phi);
3586   }
3587 
3588   // 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, int invocation) :
  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),

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

 419   return false;
 420 }
 421 
 422 // Returns true if at least one of the arguments to the call is an object
 423 // that does not escape globally.
 424 bool ConnectionGraph::has_arg_escape(CallJavaNode* call) {
 425   if (call->method() != NULL) {
 426     uint max_idx = TypeFunc::Parms + call->method()->arg_size();
 427     for (uint idx = TypeFunc::Parms; idx < max_idx; idx++) {
 428       Node* p = call->in(idx);
 429       if (not_global_escape(p)) {
 430         return true;
 431       }
 432     }
 433   } else {
 434     const char* name = call->as_CallStaticJava()->_name;
 435     assert(name != NULL, "no name");
 436     // no arg escapes through uncommon traps
 437     if (strcmp(name, "uncommon_trap") != 0) {
 438       // process_call_arguments() assumes that all arguments escape globally
 439       const TypeTuple* d = call->tf()->domain_sig();
 440       for (uint i = TypeFunc::Parms; i < d->cnt(); i++) {
 441         const Type* at = d->field_at(i);
 442         if (at->isa_oopptr() != NULL) {
 443           return true;
 444         }
 445       }
 446     }
 447   }
 448   return false;
 449 }
 450 
 451 
 452 
 453 // Utility function for nodes that load an object
 454 void ConnectionGraph::add_objload_to_connection_graph(Node *n, Unique_Node_List *delayed_worklist) {
 455   // Using isa_ptr() instead of isa_oopptr() for LoadP and Phi because
 456   // ThreadLocal has RawPtr type.
 457   const Type* t = _igvn->type(n);
 458   if (t->make_ptr() != NULL) {
 459     Node* adr = n->in(MemNode::Address);

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

 607     case Op_PartialSubtypeCheck: {
 608       // Produces Null or notNull and is used in only in CmpP so
 609       // phantom_obj could be used.
 610       map_ideal_node(n, phantom_obj); // Result is unknown
 611       break;
 612     }
 613     case Op_Phi: {
 614       // Using isa_ptr() instead of isa_oopptr() for LoadP and Phi because
 615       // ThreadLocal has RawPtr type.
 616       const Type* t = n->as_Phi()->type();
 617       if (t->make_ptr() != NULL) {
 618         add_local_var(n, PointsToNode::NoEscape);
 619         // Do not add edges during first iteration because some could be
 620         // not defined yet.
 621         delayed_worklist->push(n);
 622       }
 623       break;
 624     }
 625     case Op_Proj: {
 626       // we are only interested in the oop result projection from a call
 627       if (n->as_Proj()->_con >= TypeFunc::Parms && n->in(0)->is_Call() &&
 628           (n->in(0)->as_Call()->returns_pointer() || n->bottom_type()->isa_ptr())) {
 629         assert((n->as_Proj()->_con == TypeFunc::Parms && n->in(0)->as_Call()->returns_pointer()) ||
 630                n->in(0)->as_Call()->tf()->returns_inline_type_as_fields(), "what kind of oop return is it?");
 631         add_local_var_and_edge(n, PointsToNode::NoEscape, n->in(0), delayed_worklist);
 632       }
 633       break;
 634     }
 635     case Op_Rethrow: // Exception object escapes
 636     case Op_Return: {
 637       if (n->req() > TypeFunc::Parms &&
 638           igvn->type(n->in(TypeFunc::Parms))->isa_oopptr()) {
 639         // Treat Return value as LocalVar with GlobalEscape escape state.
 640         add_local_var_and_edge(n, PointsToNode::GlobalEscape, n->in(TypeFunc::Parms), delayed_worklist);
 641       }
 642       break;
 643     }
 644     case Op_CompareAndExchangeP:
 645     case Op_CompareAndExchangeN:
 646     case Op_GetAndSetP:
 647     case Op_GetAndSetN: {
 648       add_objload_to_connection_graph(n, delayed_worklist);
 649       // fall-through
 650     }

 693   if (n->is_Call()) {
 694     process_call_arguments(n->as_Call());
 695     return;
 696   }
 697   assert(n->is_Store() || n->is_LoadStore() ||
 698          (n_ptn != NULL) && (n_ptn->ideal_node() != NULL),
 699          "node should be registered already");
 700   int opcode = n->Opcode();
 701   bool gc_handled = BarrierSet::barrier_set()->barrier_set_c2()->escape_add_final_edges(this, _igvn, n, opcode);
 702   if (gc_handled) {
 703     return; // Ignore node if already handled by GC.
 704   }
 705   switch (opcode) {
 706     case Op_AddP: {
 707       Node* base = get_addp_base(n);
 708       PointsToNode* ptn_base = ptnode_adr(base->_idx);
 709       assert(ptn_base != NULL, "field's base should be registered");
 710       add_base(n_ptn->as_Field(), ptn_base);
 711       break;
 712     }
 713     case Op_InlineTypePtr:
 714     case Op_CastPP:
 715     case Op_CheckCastPP:
 716     case Op_EncodeP:
 717     case Op_DecodeN:
 718     case Op_EncodePKlass:
 719     case Op_DecodeNKlass: {
 720       add_local_var_and_edge(n, PointsToNode::NoEscape, n->in(1), NULL);
 721       break;
 722     }
 723     case Op_CMoveP: {
 724       for (uint i = CMoveNode::IfFalse; i < n->req(); i++) {
 725         Node* in = n->in(i);
 726         if (in == NULL) {
 727           continue;  // ignore NULL
 728         }
 729         Node* uncast_in = in->uncast();
 730         if (uncast_in->is_top() || uncast_in == n) {
 731           continue;  // ignore top or inputs which go back this node
 732         }
 733         PointsToNode* ptn = ptnode_adr(in->_idx);

 749       // Using isa_ptr() instead of isa_oopptr() for LoadP and Phi because
 750       // ThreadLocal has RawPtr type.
 751       assert(n->as_Phi()->type()->make_ptr() != NULL, "Unexpected node type");
 752       for (uint i = 1; i < n->req(); i++) {
 753         Node* in = n->in(i);
 754         if (in == NULL) {
 755           continue;  // ignore NULL
 756         }
 757         Node* uncast_in = in->uncast();
 758         if (uncast_in->is_top() || uncast_in == n) {
 759           continue;  // ignore top or inputs which go back this node
 760         }
 761         PointsToNode* ptn = ptnode_adr(in->_idx);
 762         assert(ptn != NULL, "node should be registered");
 763         add_edge(n_ptn, ptn);
 764       }
 765       break;
 766     }
 767     case Op_Proj: {
 768       // we are only interested in the oop result projection from a call
 769       assert((n->as_Proj()->_con == TypeFunc::Parms && n->in(0)->as_Call()->returns_pointer()) ||
 770              n->in(0)->as_Call()->tf()->returns_inline_type_as_fields(), "what kind of oop return is it?");
 771       add_local_var_and_edge(n, PointsToNode::NoEscape, n->in(0), NULL);
 772       break;
 773     }
 774     case Op_Rethrow: // Exception object escapes
 775     case Op_Return: {
 776       assert(n->req() > TypeFunc::Parms && _igvn->type(n->in(TypeFunc::Parms))->isa_oopptr(),
 777              "Unexpected node type");
 778       // Treat Return value as LocalVar with GlobalEscape escape state.
 779       add_local_var_and_edge(n, PointsToNode::GlobalEscape, n->in(TypeFunc::Parms), NULL);
 780       break;
 781     }
 782     case Op_CompareAndExchangeP:
 783     case Op_CompareAndExchangeN:
 784     case Op_GetAndSetP:
 785     case Op_GetAndSetN:{
 786       assert(_igvn->type(n)->make_ptr() != NULL, "Unexpected node type");
 787       add_local_var_and_edge(n, PointsToNode::NoEscape, n->in(MemNode::Address), NULL);
 788       // fall-through
 789     }
 790     case Op_CompareAndSwapP:

 906     PointsToNode* ptn = ptnode_adr(val->_idx);
 907     assert(ptn != NULL, "node should be registered");
 908     set_escape_state(ptn, PointsToNode::GlobalEscape NOT_PRODUCT(COMMA "stored at raw address"));
 909     // Add edge to object for unsafe access with offset.
 910     PointsToNode* adr_ptn = ptnode_adr(adr->_idx);
 911     assert(adr_ptn != NULL, "node should be registered");
 912     if (adr_ptn->is_Field()) {
 913       assert(adr_ptn->as_Field()->is_oop(), "should be oop field");
 914       add_edge(adr_ptn, ptn);
 915     }
 916     return true;
 917   }
 918 #ifdef ASSERT
 919   n->dump(1);
 920   assert(false, "not unsafe");
 921 #endif
 922   return false;
 923 }
 924 
 925 void ConnectionGraph::add_call_node(CallNode* call) {
 926   assert(call->returns_pointer() || call->tf()->returns_inline_type_as_fields(), "only for call which returns pointer");
 927   uint call_idx = call->_idx;
 928   if (call->is_Allocate()) {
 929     Node* k = call->in(AllocateNode::KlassNode);
 930     const TypeKlassPtr* kt = k->bottom_type()->isa_klassptr();
 931     assert(kt != NULL, "TypeKlassPtr  required.");
 932     ciKlass* cik = kt->klass();
 933     PointsToNode::EscapeState es = PointsToNode::NoEscape;
 934     bool scalar_replaceable = true;
 935     NOT_PRODUCT(const char* nsr_reason = "");
 936     if (call->is_AllocateArray()) {
 937       if (!cik->is_array_klass()) { // StressReflectiveCode
 938         es = PointsToNode::GlobalEscape;
 939       } else {
 940         int length = call->in(AllocateNode::ALength)->find_int_con(-1);
 941         if (length < 0) {
 942           // Not scalar replaceable if the length is not constant.
 943           scalar_replaceable = false;
 944           NOT_PRODUCT(nsr_reason = "has a non-constant length");
 945         } else if (length > EliminateAllocationArraySizeLimit) {
 946           // Not scalar replaceable if the length is too big.

 978     //
 979     //    - all oop arguments are escaping globally;
 980     //
 981     // 2. CallStaticJavaNode (execute bytecode analysis if possible):
 982     //
 983     //    - the same as CallDynamicJavaNode if can't do bytecode analysis;
 984     //
 985     //    - mapped to GlobalEscape JavaObject node if unknown oop is returned;
 986     //    - mapped to NoEscape JavaObject node if non-escaping object allocated
 987     //      during call is returned;
 988     //    - mapped to ArgEscape LocalVar node pointed to object arguments
 989     //      which are returned and does not escape during call;
 990     //
 991     //    - oop arguments escaping status is defined by bytecode analysis;
 992     //
 993     // For a static call, we know exactly what method is being called.
 994     // Use bytecode estimator to record whether the call's return value escapes.
 995     ciMethod* meth = call->as_CallJava()->method();
 996     if (meth == NULL) {
 997       const char* name = call->as_CallStaticJava()->_name;
 998       assert(strncmp(name, "_multianewarray", 15) == 0 ||
 999              strncmp(name, "_load_unknown_inline", 20) == 0, "TODO: add failed case check");
1000       // Returns a newly allocated non-escaped object.
1001       add_java_object(call, PointsToNode::NoEscape);
1002       set_not_scalar_replaceable(ptnode_adr(call_idx) NOT_PRODUCT(COMMA "is result of multinewarray"));
1003     } else if (meth->is_boxing_method()) {
1004       // Returns boxing object
1005       PointsToNode::EscapeState es;
1006       vmIntrinsics::ID intr = meth->intrinsic_id();
1007       if (intr == vmIntrinsics::_floatValue || intr == vmIntrinsics::_doubleValue) {
1008         // It does not escape if object is always allocated.
1009         es = PointsToNode::NoEscape;
1010       } else {
1011         // It escapes globally if object could be loaded from cache.
1012         es = PointsToNode::GlobalEscape;
1013       }
1014       add_java_object(call, es);
1015     } else {
1016       BCEscapeAnalyzer* call_analyzer = meth->get_bcea();
1017       call_analyzer->copy_dependencies(_compile->dependencies());
1018       if (call_analyzer->is_return_allocated()) {
1019         // Returns a newly allocated non-escaped object, simply
1020         // update dependency information.
1021         // Mark it as NoEscape so that objects referenced by
1022         // it's fields will be marked as NoEscape at least.
1023         add_java_object(call, PointsToNode::NoEscape);
1024         set_not_scalar_replaceable(ptnode_adr(call_idx) NOT_PRODUCT(COMMA "is result of call"));
1025       } else {
1026         // Determine whether any arguments are returned.
1027         const TypeTuple* d = call->tf()->domain_cc();
1028         bool ret_arg = false;
1029         for (uint i = TypeFunc::Parms; i < d->cnt(); i++) {
1030           if (d->field_at(i)->isa_ptr() != NULL &&
1031               call_analyzer->is_arg_returned(i - TypeFunc::Parms)) {
1032             ret_arg = true;
1033             break;
1034           }
1035         }
1036         if (ret_arg) {
1037           add_local_var(call, PointsToNode::ArgEscape);
1038         } else {
1039           // Returns unknown object.
1040           map_ideal_node(call, phantom_obj);
1041         }
1042       }
1043     }
1044   } else {
1045     // An other type of call, assume the worst case:
1046     // returned value is unknown and globally escapes.
1047     assert(call->Opcode() == Op_CallDynamicJava, "add failed case check");

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

1136                   strcmp(call->as_CallLeaf()->_name, "counterMode_AESCrypt") == 0 ||
1137                   strcmp(call->as_CallLeaf()->_name, "galoisCounterMode_AESCrypt") == 0 ||
1138                   strcmp(call->as_CallLeaf()->_name, "ghash_processBlocks") == 0 ||
1139                   strcmp(call->as_CallLeaf()->_name, "encodeBlock") == 0 ||
1140                   strcmp(call->as_CallLeaf()->_name, "decodeBlock") == 0 ||
1141                   strcmp(call->as_CallLeaf()->_name, "md5_implCompress") == 0 ||
1142                   strcmp(call->as_CallLeaf()->_name, "md5_implCompressMB") == 0 ||
1143                   strcmp(call->as_CallLeaf()->_name, "sha1_implCompress") == 0 ||
1144                   strcmp(call->as_CallLeaf()->_name, "sha1_implCompressMB") == 0 ||
1145                   strcmp(call->as_CallLeaf()->_name, "sha256_implCompress") == 0 ||
1146                   strcmp(call->as_CallLeaf()->_name, "sha256_implCompressMB") == 0 ||
1147                   strcmp(call->as_CallLeaf()->_name, "sha512_implCompress") == 0 ||
1148                   strcmp(call->as_CallLeaf()->_name, "sha512_implCompressMB") == 0 ||
1149                   strcmp(call->as_CallLeaf()->_name, "sha3_implCompress") == 0 ||
1150                   strcmp(call->as_CallLeaf()->_name, "sha3_implCompressMB") == 0 ||
1151                   strcmp(call->as_CallLeaf()->_name, "multiplyToLen") == 0 ||
1152                   strcmp(call->as_CallLeaf()->_name, "squareToLen") == 0 ||
1153                   strcmp(call->as_CallLeaf()->_name, "mulAdd") == 0 ||
1154                   strcmp(call->as_CallLeaf()->_name, "montgomery_multiply") == 0 ||
1155                   strcmp(call->as_CallLeaf()->_name, "montgomery_square") == 0 ||
1156                   strcmp(call->as_CallLeaf()->_name, "vectorizedMismatch") == 0 ||
1157                   strcmp(call->as_CallLeaf()->_name, "load_unknown_inline") == 0 ||
1158                   strcmp(call->as_CallLeaf()->_name, "store_unknown_inline") == 0 ||
1159                   strcmp(call->as_CallLeaf()->_name, "bigIntegerRightShiftWorker") == 0 ||
1160                   strcmp(call->as_CallLeaf()->_name, "bigIntegerLeftShiftWorker") == 0 ||
1161                   strcmp(call->as_CallLeaf()->_name, "vectorizedMismatch") == 0 ||
1162                   strcmp(call->as_CallLeaf()->_name, "get_class_id_intrinsic") == 0)
1163                  ))) {
1164             call->dump();
1165             fatal("EA unexpected CallLeaf %s", call->as_CallLeaf()->_name);
1166           }
1167 #endif
1168           // Always process arraycopy's destination object since
1169           // we need to add all possible edges to references in
1170           // source object.
1171           if (arg_esc >= PointsToNode::ArgEscape &&
1172               !arg_is_arraycopy_dest) {
1173             continue;
1174           }
1175           PointsToNode::EscapeState es = PointsToNode::ArgEscape;
1176           if (call->is_ArrayCopy()) {
1177             ArrayCopyNode* ac = call->as_ArrayCopy();
1178             if (ac->is_clonebasic() ||

1201           }
1202         }
1203       }
1204       break;
1205     }
1206     case Op_CallStaticJava: {
1207       // For a static call, we know exactly what method is being called.
1208       // Use bytecode estimator to record the call's escape affects
1209 #ifdef ASSERT
1210       const char* name = call->as_CallStaticJava()->_name;
1211       assert((name == NULL || strcmp(name, "uncommon_trap") != 0), "normal calls only");
1212 #endif
1213       ciMethod* meth = call->as_CallJava()->method();
1214       if ((meth != NULL) && meth->is_boxing_method()) {
1215         break; // Boxing methods do not modify any oops.
1216       }
1217       BCEscapeAnalyzer* call_analyzer = (meth !=NULL) ? meth->get_bcea() : NULL;
1218       // fall-through if not a Java method or no analyzer information
1219       if (call_analyzer != NULL) {
1220         PointsToNode* call_ptn = ptnode_adr(call->_idx);
1221         const TypeTuple* d = call->tf()->domain_cc();
1222         for (uint i = TypeFunc::Parms; i < d->cnt(); i++) {
1223           const Type* at = d->field_at(i);
1224           int k = i - TypeFunc::Parms;
1225           Node* arg = call->in(i);
1226           PointsToNode* arg_ptn = ptnode_adr(arg->_idx);
1227           if (at->isa_ptr() != NULL &&
1228               call_analyzer->is_arg_returned(k)) {
1229             // The call returns arguments.
1230             if (call_ptn != NULL) { // Is call's result used?
1231               assert(call_ptn->is_LocalVar(), "node should be registered");
1232               assert(arg_ptn != NULL, "node should be registered");
1233               add_edge(call_ptn, arg_ptn);
1234             }
1235           }
1236           if (at->isa_oopptr() != NULL &&
1237               arg_ptn->escape_state() < PointsToNode::GlobalEscape) {
1238             if (!call_analyzer->is_arg_stack(k)) {
1239               // The argument global escapes
1240               set_escape_state(arg_ptn, PointsToNode::GlobalEscape NOT_PRODUCT(COMMA trace_arg_escape_message(call)));
1241             } else {

1245                 set_fields_escape_state(arg_ptn, PointsToNode::GlobalEscape NOT_PRODUCT(COMMA trace_arg_escape_message(call)));
1246               }
1247             }
1248           }
1249         }
1250         if (call_ptn != NULL && call_ptn->is_LocalVar()) {
1251           // The call returns arguments.
1252           assert(call_ptn->edge_count() > 0, "sanity");
1253           if (!call_analyzer->is_return_local()) {
1254             // Returns also unknown object.
1255             add_edge(call_ptn, phantom_obj);
1256           }
1257         }
1258         break;
1259       }
1260     }
1261     default: {
1262       // Fall-through here if not a Java method or no analyzer information
1263       // or some other type of call, assume the worst case: all arguments
1264       // globally escape.
1265       const TypeTuple* d = call->tf()->domain_cc();
1266       for (uint i = TypeFunc::Parms; i < d->cnt(); i++) {
1267         const Type* at = d->field_at(i);
1268         if (at->isa_oopptr() != NULL) {
1269           Node* arg = call->in(i);
1270           if (arg->is_AddP()) {
1271             arg = get_addp_base(arg);
1272           }
1273           assert(ptnode_adr(arg->_idx) != NULL, "should be defined already");
1274           set_escape_state(ptnode_adr(arg->_idx), PointsToNode::GlobalEscape NOT_PRODUCT(COMMA trace_arg_escape_message(call)));
1275         }
1276       }
1277     }
1278   }
1279 }
1280 
1281 
1282 // Finish Graph construction.
1283 bool ConnectionGraph::complete_connection_graph(
1284                          GrowableArray<PointsToNode*>&   ptnodes_worklist,
1285                          GrowableArray<JavaObjectNode*>& non_escaped_allocs_worklist,

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

1788               } else {
1789                 if (!val->is_LocalVar() || (val->edge_count() == 0)) {
1790                   tty->print_cr("----------init store has invalid value -----");
1791                   store->dump();
1792                   val->dump();
1793                   assert(val->is_LocalVar() && (val->edge_count() > 0), "should be processed already");
1794                 }
1795                 for (EdgeIterator j(val); j.has_next(); j.next()) {
1796                   PointsToNode* obj = j.get();
1797                   if (obj->is_JavaObject()) {
1798                     if (!field->points_to(obj->as_JavaObject())) {
1799                       missed_obj = obj;
1800                       break;
1801                     }
1802                   }
1803                 }
1804               }
1805               if (missed_obj != NULL) {
1806                 tty->print_cr("----------field---------------------------------");
1807                 field->dump();
1808                 tty->print_cr("----------missed reference to object------------");
1809                 missed_obj->dump();
1810                 tty->print_cr("----------object referenced by init store-------");
1811                 store->dump();
1812                 val->dump();
1813                 assert(!field->points_to(missed_obj->as_JavaObject()), "missed JavaObject reference");
1814               }
1815             }
1816 #endif
1817           } else {
1818             // There could be initializing stores which follow allocation.
1819             // For example, a volatile field store is not collected
1820             // by Initialize node.
1821             //
1822             // Need to check for dependent loads to separate such stores from
1823             // stores which follow loads. For now, add initial value NULL so
1824             // that compare pointers optimization works correctly.
1825           }
1826         }
1827         if (value == NULL) {
1828           // A field's initializing value was not recorded. Add NULL.
1829           if (add_edge(field, null_obj)) {
1830             // New edge was added

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

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

2451             return true;
2452           }
2453         }
2454       }
2455     }
2456   }
2457   return false;
2458 }
2459 
2460 int ConnectionGraph::address_offset(Node* adr, PhaseTransform *phase) {
2461   const Type *adr_type = phase->type(adr);
2462   if (adr->is_AddP() && adr_type->isa_oopptr() == NULL && is_captured_store_address(adr)) {
2463     // We are computing a raw address for a store captured by an Initialize
2464     // compute an appropriate address type. AddP cases #3 and #5 (see below).
2465     int offs = (int)phase->find_intptr_t_con(adr->in(AddPNode::Offset), Type::OffsetBot);
2466     assert(offs != Type::OffsetBot ||
2467            adr->in(AddPNode::Address)->in(0)->is_AllocateArray(),
2468            "offset must be a constant or it is initialization of array");
2469     return offs;
2470   }
2471   return adr_type->is_ptr()->flattened_offset();


2472 }
2473 
2474 Node* ConnectionGraph::get_addp_base(Node *addp) {
2475   assert(addp->is_AddP(), "must be AddP");
2476   //
2477   // AddP cases for Base and Address inputs:
2478   // case #1. Direct object's field reference:
2479   //     Allocate
2480   //       |
2481   //     Proj #5 ( oop result )
2482   //       |
2483   //     CheckCastPP (cast to instance type)
2484   //      | |
2485   //     AddP  ( base == address )
2486   //
2487   // case #2. Indirect object's field reference:
2488   //      Phi
2489   //       |
2490   //     CastPP (cast to instance type)
2491   //      | |

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

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

3484   if (memnode_worklist.length() == 0)
3485     return;  // nothing to do
3486   while (memnode_worklist.length() != 0) {
3487     Node *n = memnode_worklist.pop();
3488     if (visited.test_set(n->_idx)) {
3489       continue;
3490     }
3491     if (n->is_Phi() || n->is_ClearArray()) {
3492       // we don't need to do anything, but the users must be pushed
3493     } else if (n->is_MemBar()) { // Initialize, MemBar nodes
3494       // we don't need to do anything, but the users must be pushed
3495       n = n->as_MemBar()->proj_out_or_null(TypeFunc::Memory);
3496       if (n == NULL) {
3497         continue;
3498       }
3499     } else if (n->Opcode() == Op_StrCompressedCopy ||
3500                n->Opcode() == Op_EncodeISOArray) {
3501       // get the memory projection
3502       n = n->find_out_with(Op_SCMemProj);
3503       assert(n != NULL && n->Opcode() == Op_SCMemProj, "memory projection required");
3504     } else if (n->is_CallLeaf() && n->as_CallLeaf()->_name != NULL &&
3505                strcmp(n->as_CallLeaf()->_name, "store_unknown_inline") == 0) {
3506       n = n->as_CallLeaf()->proj_out(TypeFunc::Memory);
3507     } else {
3508       assert(n->is_Mem(), "memory node required.");
3509       Node *addr = n->in(MemNode::Address);
3510       const Type *addr_t = igvn->type(addr);
3511       if (addr_t == Type::TOP) {
3512         continue;
3513       }
3514       assert (addr_t->isa_ptr() != NULL, "pointer type required.");
3515       int alias_idx = _compile->get_alias_index(addr_t->is_ptr());
3516       assert ((uint)alias_idx < new_index_end, "wrong alias index");
3517       Node *mem = find_inst_mem(n->in(MemNode::Memory), alias_idx, orig_phis);
3518       if (_compile->failing()) {
3519         return;
3520       }
3521       if (mem != n->in(MemNode::Memory)) {
3522         // We delay the memory edge update since we need old one in
3523         // MergeMem code below when instances memory slices are separated.
3524         set_map(n, mem);
3525       }
3526       if (n->is_Load()) {

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

3638       Node* result = step_through_mergemem(nmm, ni, tinst);
3639       if (result == nmm->base_memory()) {
3640         // Didn't find instance memory, search through general slice recursively.
3641         result = nmm->memory_at(_compile->get_general_index(ni));
3642         result = find_inst_mem(result, ni, orig_phis);
3643         if (_compile->failing()) {
3644           return;
3645         }
3646         nmm->set_memory_at(ni, result);
3647       }
3648     }
3649     igvn->hash_insert(nmm);
3650     record_for_optimizer(nmm);
3651   }
3652 
3653   //  Phase 4:  Update the inputs of non-instance memory Phis and
3654   //            the Memory input of memnodes
3655   // First update the inputs of any non-instance Phi's from
3656   // which we split out an instance Phi.  Note we don't have
3657   // to recursively process Phi's encountered on the input memory
3658   // chains as is done in split_memory_phi() since they will
3659   // also be processed here.
3660   for (int j = 0; j < orig_phis.length(); j++) {
3661     PhiNode *phi = orig_phis.at(j);
3662     int alias_idx = _compile->get_alias_index(phi->adr_type());
3663     igvn->hash_delete(phi);
3664     for (uint i = 1; i < phi->req(); i++) {
3665       Node *mem = phi->in(i);
3666       Node *new_mem = find_inst_mem(mem, alias_idx, orig_phis);
3667       if (_compile->failing()) {
3668         return;
3669       }
3670       if (mem != new_mem) {
3671         phi->set_req(i, new_mem);
3672       }
3673     }
3674     igvn->hash_insert(phi);
3675     record_for_optimizer(phi);
3676   }
3677 
3678   // Update the memory inputs of MemNodes with the value we computed
< prev index next >