src/share/vm/opto/escape.cpp

Thu, 20 Sep 2012 16:49:17 +0200

author
roland
date
Thu, 20 Sep 2012 16:49:17 +0200
changeset 4106
7eca5de9e0b6
parent 3971
6c5b7a6becc8
child 4159
8e47bac5643a
permissions
-rw-r--r--

7023898: Intrinsify AtomicLongFieldUpdater.getAndIncrement()
Summary: use shorter instruction sequences for atomic add and atomic exchange when possible.
Reviewed-by: kvn, jrose

duke@435 1 /*
kvn@3564 2 * Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved.
duke@435 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@435 4 *
duke@435 5 * This code is free software; you can redistribute it and/or modify it
duke@435 6 * under the terms of the GNU General Public License version 2 only, as
duke@435 7 * published by the Free Software Foundation.
duke@435 8 *
duke@435 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@435 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@435 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@435 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@435 13 * accompanied this code).
duke@435 14 *
duke@435 15 * You should have received a copy of the GNU General Public License version
duke@435 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@435 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@435 18 *
trims@1907 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
trims@1907 20 * or visit www.oracle.com if you need additional information or have any
trims@1907 21 * questions.
duke@435 22 *
duke@435 23 */
duke@435 24
stefank@2314 25 #include "precompiled.hpp"
stefank@2314 26 #include "ci/bcEscapeAnalyzer.hpp"
kvn@3651 27 #include "compiler/compileLog.hpp"
stefank@2314 28 #include "libadt/vectset.hpp"
stefank@2314 29 #include "memory/allocation.hpp"
stefank@2314 30 #include "opto/c2compiler.hpp"
stefank@2314 31 #include "opto/callnode.hpp"
stefank@2314 32 #include "opto/cfgnode.hpp"
stefank@2314 33 #include "opto/compile.hpp"
stefank@2314 34 #include "opto/escape.hpp"
stefank@2314 35 #include "opto/phaseX.hpp"
stefank@2314 36 #include "opto/rootnode.hpp"
duke@435 37
kvn@1989 38 ConnectionGraph::ConnectionGraph(Compile * C, PhaseIterGVN *igvn) :
kvn@3651 39 _nodes(C->comp_arena(), C->unique(), C->unique(), NULL),
kvn@679 40 _collecting(true),
kvn@3651 41 _verify(false),
kvn@679 42 _compile(C),
kvn@1989 43 _igvn(igvn),
kvn@679 44 _node_map(C->comp_arena()) {
kvn@3651 45 // Add unknown java object.
kvn@3651 46 add_java_object(C->top(), PointsToNode::GlobalEscape);
kvn@3651 47 phantom_obj = ptnode_adr(C->top()->_idx)->as_JavaObject();
kvn@688 48 // Add ConP(#NULL) and ConN(#NULL) nodes.
kvn@688 49 Node* oop_null = igvn->zerocon(T_OBJECT);
kvn@3651 50 assert(oop_null->_idx < nodes_size(), "should be created already");
kvn@3651 51 add_java_object(oop_null, PointsToNode::NoEscape);
kvn@3651 52 null_obj = ptnode_adr(oop_null->_idx)->as_JavaObject();
kvn@688 53 if (UseCompressedOops) {
kvn@688 54 Node* noop_null = igvn->zerocon(T_NARROWOOP);
kvn@3651 55 assert(noop_null->_idx < nodes_size(), "should be created already");
kvn@3651 56 map_ideal_node(noop_null, null_obj);
kvn@688 57 }
kvn@3309 58 _pcmp_neq = NULL; // Should be initialized
kvn@3309 59 _pcmp_eq = NULL;
duke@435 60 }
duke@435 61
kvn@3651 62 bool ConnectionGraph::has_candidates(Compile *C) {
kvn@3651 63 // EA brings benefits only when the code has allocations and/or locks which
kvn@3651 64 // are represented by ideal Macro nodes.
kvn@3651 65 int cnt = C->macro_count();
kvn@3651 66 for( int i=0; i < cnt; i++ ) {
kvn@3651 67 Node *n = C->macro_node(i);
kvn@3651 68 if ( n->is_Allocate() )
kvn@3651 69 return true;
kvn@3651 70 if( n->is_Lock() ) {
kvn@3651 71 Node* obj = n->as_Lock()->obj_node()->uncast();
kvn@3651 72 if( !(obj->is_Parm() || obj->is_Con()) )
kvn@3651 73 return true;
kvn@3318 74 }
kvn@3318 75 }
kvn@3651 76 return false;
duke@435 77 }
duke@435 78
kvn@3651 79 void ConnectionGraph::do_analysis(Compile *C, PhaseIterGVN *igvn) {
kvn@3651 80 Compile::TracePhase t2("escapeAnalysis", &Phase::_t_escapeAnalysis, true);
kvn@3651 81 ResourceMark rm;
duke@435 82
kvn@3651 83 // Add ConP#NULL and ConN#NULL nodes before ConnectionGraph construction
kvn@3651 84 // to create space for them in ConnectionGraph::_nodes[].
kvn@3651 85 Node* oop_null = igvn->zerocon(T_OBJECT);
kvn@3651 86 Node* noop_null = igvn->zerocon(T_NARROWOOP);
kvn@3651 87 ConnectionGraph* congraph = new(C->comp_arena()) ConnectionGraph(C, igvn);
kvn@3651 88 // Perform escape analysis
kvn@3651 89 if (congraph->compute_escape()) {
kvn@3651 90 // There are non escaping objects.
kvn@3651 91 C->set_congraph(congraph);
kvn@3651 92 }
kvn@3651 93 // Cleanup.
kvn@3651 94 if (oop_null->outcnt() == 0)
kvn@3651 95 igvn->hash_delete(oop_null);
kvn@3651 96 if (noop_null->outcnt() == 0)
kvn@3651 97 igvn->hash_delete(noop_null);
duke@435 98 }
duke@435 99
kvn@3651 100 bool ConnectionGraph::compute_escape() {
kvn@3651 101 Compile* C = _compile;
kvn@3651 102 PhaseGVN* igvn = _igvn;
kvn@3651 103
kvn@3651 104 // Worklists used by EA.
kvn@3651 105 Unique_Node_List delayed_worklist;
kvn@3651 106 GrowableArray<Node*> alloc_worklist;
kvn@3651 107 GrowableArray<Node*> ptr_cmp_worklist;
kvn@3651 108 GrowableArray<Node*> storestore_worklist;
kvn@3651 109 GrowableArray<PointsToNode*> ptnodes_worklist;
kvn@3651 110 GrowableArray<JavaObjectNode*> java_objects_worklist;
kvn@3651 111 GrowableArray<JavaObjectNode*> non_escaped_worklist;
kvn@3651 112 GrowableArray<FieldNode*> oop_fields_worklist;
kvn@3651 113 DEBUG_ONLY( GrowableArray<Node*> addp_worklist; )
kvn@3651 114
kvn@3651 115 { Compile::TracePhase t3("connectionGraph", &Phase::_t_connectionGraph, true);
kvn@3651 116
kvn@3651 117 // 1. Populate Connection Graph (CG) with PointsTo nodes.
kvn@3651 118 ideal_nodes.map(C->unique(), NULL); // preallocate space
kvn@3651 119 // Initialize worklist
kvn@3651 120 if (C->root() != NULL) {
kvn@3651 121 ideal_nodes.push(C->root());
kvn@3651 122 }
kvn@3651 123 for( uint next = 0; next < ideal_nodes.size(); ++next ) {
kvn@3651 124 Node* n = ideal_nodes.at(next);
kvn@3651 125 // Create PointsTo nodes and add them to Connection Graph. Called
kvn@3651 126 // only once per ideal node since ideal_nodes is Unique_Node list.
kvn@3651 127 add_node_to_connection_graph(n, &delayed_worklist);
kvn@3651 128 PointsToNode* ptn = ptnode_adr(n->_idx);
kvn@3651 129 if (ptn != NULL) {
kvn@3651 130 ptnodes_worklist.append(ptn);
kvn@3651 131 if (ptn->is_JavaObject()) {
kvn@3651 132 java_objects_worklist.append(ptn->as_JavaObject());
kvn@3651 133 if ((n->is_Allocate() || n->is_CallStaticJava()) &&
kvn@3651 134 (ptn->escape_state() < PointsToNode::GlobalEscape)) {
kvn@3651 135 // Only allocations and java static calls results are interesting.
kvn@3651 136 non_escaped_worklist.append(ptn->as_JavaObject());
kvn@3651 137 }
kvn@3651 138 } else if (ptn->is_Field() && ptn->as_Field()->is_oop()) {
kvn@3651 139 oop_fields_worklist.append(ptn->as_Field());
kvn@3651 140 }
kvn@3651 141 }
kvn@3651 142 if (n->is_MergeMem()) {
kvn@3651 143 // Collect all MergeMem nodes to add memory slices for
kvn@3651 144 // scalar replaceable objects in split_unique_types().
kvn@3651 145 _mergemem_worklist.append(n->as_MergeMem());
kvn@3651 146 } else if (OptimizePtrCompare && n->is_Cmp() &&
kvn@3651 147 (n->Opcode() == Op_CmpP || n->Opcode() == Op_CmpN)) {
kvn@3651 148 // Collect compare pointers nodes.
kvn@3651 149 ptr_cmp_worklist.append(n);
kvn@3651 150 } else if (n->is_MemBarStoreStore()) {
kvn@3651 151 // Collect all MemBarStoreStore nodes so that depending on the
kvn@3651 152 // escape status of the associated Allocate node some of them
kvn@3651 153 // may be eliminated.
kvn@3651 154 storestore_worklist.append(n);
kvn@3651 155 #ifdef ASSERT
kvn@3651 156 } else if(n->is_AddP()) {
kvn@3651 157 // Collect address nodes for graph verification.
kvn@3651 158 addp_worklist.append(n);
kvn@3651 159 #endif
kvn@3651 160 }
kvn@3651 161 for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
kvn@3651 162 Node* m = n->fast_out(i); // Get user
kvn@3651 163 ideal_nodes.push(m);
kvn@3651 164 }
kvn@3651 165 }
kvn@3651 166 if (non_escaped_worklist.length() == 0) {
kvn@3651 167 _collecting = false;
kvn@3651 168 return false; // Nothing to do.
kvn@3651 169 }
kvn@3651 170 // Add final simple edges to graph.
kvn@3651 171 while(delayed_worklist.size() > 0) {
kvn@3651 172 Node* n = delayed_worklist.pop();
kvn@3651 173 add_final_edges(n);
kvn@3651 174 }
kvn@3651 175 int ptnodes_length = ptnodes_worklist.length();
kvn@3651 176
kvn@3651 177 #ifdef ASSERT
kvn@3651 178 if (VerifyConnectionGraph) {
kvn@3651 179 // Verify that no new simple edges could be created and all
kvn@3651 180 // local vars has edges.
kvn@3651 181 _verify = true;
kvn@3651 182 for (int next = 0; next < ptnodes_length; ++next) {
kvn@3651 183 PointsToNode* ptn = ptnodes_worklist.at(next);
kvn@3651 184 add_final_edges(ptn->ideal_node());
kvn@3651 185 if (ptn->is_LocalVar() && ptn->edge_count() == 0) {
kvn@3651 186 ptn->dump();
kvn@3651 187 assert(ptn->as_LocalVar()->edge_count() > 0, "sanity");
kvn@3651 188 }
kvn@3651 189 }
kvn@3651 190 _verify = false;
kvn@3651 191 }
kvn@3651 192 #endif
kvn@3651 193
kvn@3651 194 // 2. Finish Graph construction by propagating references to all
kvn@3651 195 // java objects through graph.
kvn@3651 196 if (!complete_connection_graph(ptnodes_worklist, non_escaped_worklist,
kvn@3651 197 java_objects_worklist, oop_fields_worklist)) {
kvn@3651 198 // All objects escaped or hit time or iterations limits.
kvn@3651 199 _collecting = false;
kvn@3651 200 return false;
kvn@3651 201 }
kvn@3651 202
kvn@3651 203 // 3. Adjust scalar_replaceable state of nonescaping objects and push
kvn@3651 204 // scalar replaceable allocations on alloc_worklist for processing
kvn@3651 205 // in split_unique_types().
kvn@3651 206 int non_escaped_length = non_escaped_worklist.length();
kvn@3651 207 for (int next = 0; next < non_escaped_length; next++) {
kvn@3651 208 JavaObjectNode* ptn = non_escaped_worklist.at(next);
kvn@3651 209 if (ptn->escape_state() == PointsToNode::NoEscape &&
kvn@3651 210 ptn->scalar_replaceable()) {
kvn@3651 211 adjust_scalar_replaceable_state(ptn);
kvn@3651 212 if (ptn->scalar_replaceable()) {
kvn@3651 213 alloc_worklist.append(ptn->ideal_node());
kvn@3651 214 }
kvn@3651 215 }
kvn@3651 216 }
kvn@3651 217
kvn@3651 218 #ifdef ASSERT
kvn@3651 219 if (VerifyConnectionGraph) {
kvn@3651 220 // Verify that graph is complete - no new edges could be added or needed.
kvn@3651 221 verify_connection_graph(ptnodes_worklist, non_escaped_worklist,
kvn@3651 222 java_objects_worklist, addp_worklist);
kvn@3651 223 }
kvn@3651 224 assert(C->unique() == nodes_size(), "no new ideal nodes should be added during ConnectionGraph build");
kvn@3651 225 assert(null_obj->escape_state() == PointsToNode::NoEscape &&
kvn@3651 226 null_obj->edge_count() == 0 &&
kvn@3651 227 !null_obj->arraycopy_src() &&
kvn@3651 228 !null_obj->arraycopy_dst(), "sanity");
kvn@3651 229 #endif
kvn@3651 230
kvn@3651 231 _collecting = false;
kvn@3651 232
kvn@3651 233 } // TracePhase t3("connectionGraph")
kvn@3651 234
kvn@3651 235 // 4. Optimize ideal graph based on EA information.
kvn@3651 236 bool has_non_escaping_obj = (non_escaped_worklist.length() > 0);
kvn@3651 237 if (has_non_escaping_obj) {
kvn@3651 238 optimize_ideal_graph(ptr_cmp_worklist, storestore_worklist);
kvn@3651 239 }
kvn@3651 240
kvn@3651 241 #ifndef PRODUCT
kvn@3651 242 if (PrintEscapeAnalysis) {
kvn@3651 243 dump(ptnodes_worklist); // Dump ConnectionGraph
kvn@3651 244 }
kvn@3651 245 #endif
kvn@3651 246
kvn@3651 247 bool has_scalar_replaceable_candidates = (alloc_worklist.length() > 0);
kvn@3651 248 #ifdef ASSERT
kvn@3651 249 if (VerifyConnectionGraph) {
kvn@3651 250 int alloc_length = alloc_worklist.length();
kvn@3651 251 for (int next = 0; next < alloc_length; ++next) {
kvn@3651 252 Node* n = alloc_worklist.at(next);
kvn@3651 253 PointsToNode* ptn = ptnode_adr(n->_idx);
kvn@3651 254 assert(ptn->escape_state() == PointsToNode::NoEscape && ptn->scalar_replaceable(), "sanity");
kvn@3651 255 }
kvn@3651 256 }
kvn@3651 257 #endif
kvn@3651 258
kvn@3651 259 // 5. Separate memory graph for scalar replaceable allcations.
kvn@3651 260 if (has_scalar_replaceable_candidates &&
kvn@3651 261 C->AliasLevel() >= 3 && EliminateAllocations) {
kvn@3651 262 // Now use the escape information to create unique types for
kvn@3651 263 // scalar replaceable objects.
kvn@3651 264 split_unique_types(alloc_worklist);
kvn@3651 265 if (C->failing()) return false;
kvn@3651 266 C->print_method("After Escape Analysis", 2);
kvn@3651 267
kvn@3651 268 #ifdef ASSERT
kvn@3651 269 } else if (Verbose && (PrintEscapeAnalysis || PrintEliminateAllocations)) {
kvn@3651 270 tty->print("=== No allocations eliminated for ");
kvn@3651 271 C->method()->print_short_name();
kvn@3651 272 if(!EliminateAllocations) {
kvn@3651 273 tty->print(" since EliminateAllocations is off ===");
kvn@3651 274 } else if(!has_scalar_replaceable_candidates) {
kvn@3651 275 tty->print(" since there are no scalar replaceable candidates ===");
kvn@3651 276 } else if(C->AliasLevel() < 3) {
kvn@3651 277 tty->print(" since AliasLevel < 3 ===");
kvn@3651 278 }
kvn@3651 279 tty->cr();
kvn@3651 280 #endif
kvn@3651 281 }
kvn@3651 282 return has_non_escaping_obj;
kvn@3651 283 }
kvn@3651 284
roland@4106 285 // Utility function for nodes that load an object
roland@4106 286 void ConnectionGraph::add_objload_to_connection_graph(Node *n, Unique_Node_List *delayed_worklist) {
roland@4106 287 // Using isa_ptr() instead of isa_oopptr() for LoadP and Phi because
roland@4106 288 // ThreadLocal has RawPtr type.
roland@4106 289 const Type* t = _igvn->type(n);
roland@4106 290 if (t->make_ptr() != NULL) {
roland@4106 291 Node* adr = n->in(MemNode::Address);
roland@4106 292 #ifdef ASSERT
roland@4106 293 if (!adr->is_AddP()) {
roland@4106 294 assert(_igvn->type(adr)->isa_rawptr(), "sanity");
roland@4106 295 } else {
roland@4106 296 assert((ptnode_adr(adr->_idx) == NULL ||
roland@4106 297 ptnode_adr(adr->_idx)->as_Field()->is_oop()), "sanity");
roland@4106 298 }
roland@4106 299 #endif
roland@4106 300 add_local_var_and_edge(n, PointsToNode::NoEscape,
roland@4106 301 adr, delayed_worklist);
roland@4106 302 }
roland@4106 303 }
roland@4106 304
kvn@3651 305 // Populate Connection Graph with PointsTo nodes and create simple
kvn@3651 306 // connection graph edges.
kvn@3651 307 void ConnectionGraph::add_node_to_connection_graph(Node *n, Unique_Node_List *delayed_worklist) {
kvn@3651 308 assert(!_verify, "this method sould not be called for verification");
kvn@3651 309 PhaseGVN* igvn = _igvn;
kvn@3651 310 uint n_idx = n->_idx;
kvn@3651 311 PointsToNode* n_ptn = ptnode_adr(n_idx);
kvn@3651 312 if (n_ptn != NULL)
kvn@3651 313 return; // No need to redefine PointsTo node during first iteration.
kvn@3651 314
kvn@3651 315 if (n->is_Call()) {
kvn@3651 316 // Arguments to allocation and locking don't escape.
kvn@3651 317 if (n->is_AbstractLock()) {
kvn@3651 318 // Put Lock and Unlock nodes on IGVN worklist to process them during
kvn@3651 319 // first IGVN optimization when escape information is still available.
kvn@3651 320 record_for_optimizer(n);
kvn@3651 321 } else if (n->is_Allocate()) {
kvn@3651 322 add_call_node(n->as_Call());
kvn@3651 323 record_for_optimizer(n);
kvn@3651 324 } else {
kvn@3651 325 if (n->is_CallStaticJava()) {
kvn@3651 326 const char* name = n->as_CallStaticJava()->_name;
kvn@3651 327 if (name != NULL && strcmp(name, "uncommon_trap") == 0)
kvn@3651 328 return; // Skip uncommon traps
kvn@3651 329 }
kvn@3651 330 // Don't mark as processed since call's arguments have to be processed.
kvn@3651 331 delayed_worklist->push(n);
kvn@3651 332 // Check if a call returns an object.
kvn@3651 333 if (n->as_Call()->returns_pointer() &&
kvn@3651 334 n->as_Call()->proj_out(TypeFunc::Parms) != NULL) {
kvn@3651 335 add_call_node(n->as_Call());
kvn@3651 336 }
kvn@3651 337 }
kvn@3651 338 return;
kvn@3651 339 }
kvn@3651 340 // Put this check here to process call arguments since some call nodes
kvn@3651 341 // point to phantom_obj.
kvn@3651 342 if (n_ptn == phantom_obj || n_ptn == null_obj)
kvn@3651 343 return; // Skip predefined nodes.
kvn@3651 344
kvn@3651 345 int opcode = n->Opcode();
kvn@3651 346 switch (opcode) {
kvn@3651 347 case Op_AddP: {
kvn@3651 348 Node* base = get_addp_base(n);
kvn@3651 349 PointsToNode* ptn_base = ptnode_adr(base->_idx);
kvn@3651 350 // Field nodes are created for all field types. They are used in
kvn@3651 351 // adjust_scalar_replaceable_state() and split_unique_types().
kvn@3651 352 // Note, non-oop fields will have only base edges in Connection
kvn@3651 353 // Graph because such fields are not used for oop loads and stores.
kvn@3651 354 int offset = address_offset(n, igvn);
kvn@3651 355 add_field(n, PointsToNode::NoEscape, offset);
kvn@3651 356 if (ptn_base == NULL) {
kvn@3651 357 delayed_worklist->push(n); // Process it later.
kvn@3651 358 } else {
kvn@3651 359 n_ptn = ptnode_adr(n_idx);
kvn@3651 360 add_base(n_ptn->as_Field(), ptn_base);
kvn@3651 361 }
kvn@3651 362 break;
kvn@3651 363 }
kvn@3651 364 case Op_CastX2P: {
kvn@3651 365 map_ideal_node(n, phantom_obj);
kvn@3651 366 break;
kvn@3651 367 }
kvn@3651 368 case Op_CastPP:
kvn@3651 369 case Op_CheckCastPP:
kvn@3651 370 case Op_EncodeP:
kvn@3651 371 case Op_DecodeN: {
kvn@3651 372 add_local_var_and_edge(n, PointsToNode::NoEscape,
kvn@3651 373 n->in(1), delayed_worklist);
kvn@3651 374 break;
kvn@3651 375 }
kvn@3651 376 case Op_CMoveP: {
kvn@3651 377 add_local_var(n, PointsToNode::NoEscape);
kvn@3651 378 // Do not add edges during first iteration because some could be
kvn@3651 379 // not defined yet.
kvn@3651 380 delayed_worklist->push(n);
kvn@3651 381 break;
kvn@3651 382 }
kvn@3651 383 case Op_ConP:
kvn@3651 384 case Op_ConN: {
kvn@3651 385 // assume all oop constants globally escape except for null
kvn@3651 386 PointsToNode::EscapeState es;
kvn@3651 387 if (igvn->type(n) == TypePtr::NULL_PTR ||
kvn@3651 388 igvn->type(n) == TypeNarrowOop::NULL_PTR) {
kvn@3651 389 es = PointsToNode::NoEscape;
kvn@3651 390 } else {
kvn@3651 391 es = PointsToNode::GlobalEscape;
kvn@3651 392 }
kvn@3651 393 add_java_object(n, es);
kvn@3651 394 break;
kvn@3651 395 }
kvn@3651 396 case Op_CreateEx: {
kvn@3651 397 // assume that all exception objects globally escape
kvn@3651 398 add_java_object(n, PointsToNode::GlobalEscape);
kvn@3651 399 break;
kvn@3651 400 }
kvn@3651 401 case Op_LoadKlass:
kvn@3651 402 case Op_LoadNKlass: {
kvn@3651 403 // Unknown class is loaded
kvn@3651 404 map_ideal_node(n, phantom_obj);
kvn@3651 405 break;
kvn@3651 406 }
kvn@3651 407 case Op_LoadP:
kvn@3651 408 case Op_LoadN:
kvn@3651 409 case Op_LoadPLocked: {
roland@4106 410 add_objload_to_connection_graph(n, delayed_worklist);
kvn@3651 411 break;
kvn@3651 412 }
kvn@3651 413 case Op_Parm: {
kvn@3651 414 map_ideal_node(n, phantom_obj);
kvn@3651 415 break;
kvn@3651 416 }
kvn@3651 417 case Op_PartialSubtypeCheck: {
kvn@3651 418 // Produces Null or notNull and is used in only in CmpP so
kvn@3651 419 // phantom_obj could be used.
kvn@3651 420 map_ideal_node(n, phantom_obj); // Result is unknown
kvn@3651 421 break;
kvn@3651 422 }
kvn@3651 423 case Op_Phi: {
kvn@3651 424 // Using isa_ptr() instead of isa_oopptr() for LoadP and Phi because
roland@4106 425 // ThreadLocal has RawPtr type.
kvn@3651 426 const Type* t = n->as_Phi()->type();
kvn@3651 427 if (t->make_ptr() != NULL) {
kvn@3651 428 add_local_var(n, PointsToNode::NoEscape);
kvn@3651 429 // Do not add edges during first iteration because some could be
kvn@3651 430 // not defined yet.
kvn@3651 431 delayed_worklist->push(n);
kvn@3651 432 }
kvn@3651 433 break;
kvn@3651 434 }
kvn@3651 435 case Op_Proj: {
kvn@3651 436 // we are only interested in the oop result projection from a call
kvn@3651 437 if (n->as_Proj()->_con == TypeFunc::Parms && n->in(0)->is_Call() &&
kvn@3651 438 n->in(0)->as_Call()->returns_pointer()) {
kvn@3651 439 add_local_var_and_edge(n, PointsToNode::NoEscape,
kvn@3651 440 n->in(0), delayed_worklist);
kvn@3651 441 }
kvn@3651 442 break;
kvn@3651 443 }
kvn@3651 444 case Op_Rethrow: // Exception object escapes
kvn@3651 445 case Op_Return: {
kvn@3651 446 if (n->req() > TypeFunc::Parms &&
kvn@3651 447 igvn->type(n->in(TypeFunc::Parms))->isa_oopptr()) {
kvn@3651 448 // Treat Return value as LocalVar with GlobalEscape escape state.
kvn@3651 449 add_local_var_and_edge(n, PointsToNode::GlobalEscape,
kvn@3651 450 n->in(TypeFunc::Parms), delayed_worklist);
kvn@3651 451 }
kvn@3651 452 break;
kvn@3651 453 }
roland@4106 454 case Op_GetAndSetP:
roland@4106 455 case Op_GetAndSetN: {
roland@4106 456 add_objload_to_connection_graph(n, delayed_worklist);
roland@4106 457 // fallthrough
roland@4106 458 }
kvn@3651 459 case Op_StoreP:
kvn@3651 460 case Op_StoreN:
kvn@3651 461 case Op_StorePConditional:
kvn@3651 462 case Op_CompareAndSwapP:
kvn@3651 463 case Op_CompareAndSwapN: {
kvn@3651 464 Node* adr = n->in(MemNode::Address);
kvn@3651 465 const Type *adr_type = igvn->type(adr);
kvn@3651 466 adr_type = adr_type->make_ptr();
kvn@3651 467 if (adr_type->isa_oopptr() ||
kvn@3651 468 (opcode == Op_StoreP || opcode == Op_StoreN) &&
kvn@3651 469 (adr_type == TypeRawPtr::NOTNULL &&
kvn@3651 470 adr->in(AddPNode::Address)->is_Proj() &&
kvn@3651 471 adr->in(AddPNode::Address)->in(0)->is_Allocate())) {
kvn@3651 472 delayed_worklist->push(n); // Process it later.
kvn@3651 473 #ifdef ASSERT
kvn@3651 474 assert(adr->is_AddP(), "expecting an AddP");
kvn@3651 475 if (adr_type == TypeRawPtr::NOTNULL) {
kvn@3651 476 // Verify a raw address for a store captured by Initialize node.
kvn@3651 477 int offs = (int)igvn->find_intptr_t_con(adr->in(AddPNode::Offset), Type::OffsetBot);
kvn@3651 478 assert(offs != Type::OffsetBot, "offset must be a constant");
kvn@3651 479 }
kvn@3657 480 #endif
kvn@3651 481 } else {
kvn@3651 482 // Ignore copy the displaced header to the BoxNode (OSR compilation).
kvn@3651 483 if (adr->is_BoxLock())
kvn@3651 484 break;
kvn@3657 485 // Stored value escapes in unsafe access.
kvn@3657 486 if ((opcode == Op_StoreP) && (adr_type == TypeRawPtr::BOTTOM)) {
kvn@3657 487 // Pointer stores in G1 barriers looks like unsafe access.
kvn@3657 488 // Ignore such stores to be able scalar replace non-escaping
kvn@3657 489 // allocations.
kvn@3657 490 if (UseG1GC && adr->is_AddP()) {
kvn@3657 491 Node* base = get_addp_base(adr);
kvn@3657 492 if (base->Opcode() == Op_LoadP &&
kvn@3657 493 base->in(MemNode::Address)->is_AddP()) {
kvn@3657 494 adr = base->in(MemNode::Address);
kvn@3657 495 Node* tls = get_addp_base(adr);
kvn@3657 496 if (tls->Opcode() == Op_ThreadLocal) {
kvn@3657 497 int offs = (int)igvn->find_intptr_t_con(adr->in(AddPNode::Offset), Type::OffsetBot);
kvn@3657 498 if (offs == in_bytes(JavaThread::satb_mark_queue_offset() +
kvn@3657 499 PtrQueue::byte_offset_of_buf())) {
kvn@3657 500 break; // G1 pre barier previous oop value store.
kvn@3657 501 }
kvn@3657 502 if (offs == in_bytes(JavaThread::dirty_card_queue_offset() +
kvn@3657 503 PtrQueue::byte_offset_of_buf())) {
kvn@3657 504 break; // G1 post barier card address store.
kvn@3657 505 }
kvn@3657 506 }
kvn@3657 507 }
kvn@3657 508 }
kvn@3657 509 delayed_worklist->push(n); // Process unsafe access later.
kvn@3657 510 break;
kvn@3651 511 }
kvn@3657 512 #ifdef ASSERT
kvn@3657 513 n->dump(1);
kvn@3657 514 assert(false, "not unsafe or G1 barrier raw StoreP");
kvn@3651 515 #endif
kvn@3651 516 }
kvn@3651 517 break;
kvn@3651 518 }
kvn@3651 519 case Op_AryEq:
kvn@3651 520 case Op_StrComp:
kvn@3651 521 case Op_StrEquals:
kvn@3651 522 case Op_StrIndexOf: {
kvn@3651 523 add_local_var(n, PointsToNode::ArgEscape);
kvn@3651 524 delayed_worklist->push(n); // Process it later.
kvn@3651 525 break;
kvn@3651 526 }
kvn@3651 527 case Op_ThreadLocal: {
kvn@3651 528 add_java_object(n, PointsToNode::ArgEscape);
kvn@3651 529 break;
kvn@3651 530 }
kvn@3651 531 default:
kvn@3651 532 ; // Do nothing for nodes not related to EA.
kvn@3651 533 }
kvn@3651 534 return;
kvn@3651 535 }
kvn@3651 536
kvn@3651 537 #ifdef ASSERT
kvn@3651 538 #define ELSE_FAIL(name) \
kvn@3651 539 /* Should not be called for not pointer type. */ \
kvn@3651 540 n->dump(1); \
kvn@3651 541 assert(false, name); \
kvn@3651 542 break;
kvn@3651 543 #else
kvn@3651 544 #define ELSE_FAIL(name) \
kvn@3651 545 break;
kvn@3651 546 #endif
kvn@3651 547
kvn@3651 548 // Add final simple edges to graph.
kvn@3651 549 void ConnectionGraph::add_final_edges(Node *n) {
kvn@3651 550 PointsToNode* n_ptn = ptnode_adr(n->_idx);
kvn@3651 551 #ifdef ASSERT
kvn@3651 552 if (_verify && n_ptn->is_JavaObject())
kvn@3651 553 return; // This method does not change graph for JavaObject.
kvn@3651 554 #endif
kvn@3651 555
kvn@3651 556 if (n->is_Call()) {
kvn@3651 557 process_call_arguments(n->as_Call());
kvn@3651 558 return;
kvn@3651 559 }
kvn@3651 560 assert(n->is_Store() || n->is_LoadStore() ||
kvn@3651 561 (n_ptn != NULL) && (n_ptn->ideal_node() != NULL),
kvn@3651 562 "node should be registered already");
kvn@3651 563 int opcode = n->Opcode();
kvn@3651 564 switch (opcode) {
kvn@3651 565 case Op_AddP: {
kvn@3651 566 Node* base = get_addp_base(n);
kvn@3651 567 PointsToNode* ptn_base = ptnode_adr(base->_idx);
kvn@3651 568 assert(ptn_base != NULL, "field's base should be registered");
kvn@3651 569 add_base(n_ptn->as_Field(), ptn_base);
kvn@3651 570 break;
kvn@3651 571 }
kvn@3651 572 case Op_CastPP:
kvn@3651 573 case Op_CheckCastPP:
kvn@3651 574 case Op_EncodeP:
kvn@3651 575 case Op_DecodeN: {
kvn@3651 576 add_local_var_and_edge(n, PointsToNode::NoEscape,
kvn@3651 577 n->in(1), NULL);
kvn@3651 578 break;
kvn@3651 579 }
kvn@3651 580 case Op_CMoveP: {
kvn@3651 581 for (uint i = CMoveNode::IfFalse; i < n->req(); i++) {
kvn@3651 582 Node* in = n->in(i);
kvn@3651 583 if (in == NULL)
kvn@3651 584 continue; // ignore NULL
kvn@3651 585 Node* uncast_in = in->uncast();
kvn@3651 586 if (uncast_in->is_top() || uncast_in == n)
kvn@3651 587 continue; // ignore top or inputs which go back this node
kvn@3651 588 PointsToNode* ptn = ptnode_adr(in->_idx);
kvn@3651 589 assert(ptn != NULL, "node should be registered");
kvn@3651 590 add_edge(n_ptn, ptn);
kvn@3651 591 }
kvn@3651 592 break;
kvn@3651 593 }
kvn@3651 594 case Op_LoadP:
kvn@3651 595 case Op_LoadN:
kvn@3651 596 case Op_LoadPLocked: {
kvn@3651 597 // Using isa_ptr() instead of isa_oopptr() for LoadP and Phi because
roland@4106 598 // ThreadLocal has RawPtr type.
kvn@3651 599 const Type* t = _igvn->type(n);
kvn@3651 600 if (t->make_ptr() != NULL) {
kvn@3651 601 Node* adr = n->in(MemNode::Address);
kvn@3651 602 add_local_var_and_edge(n, PointsToNode::NoEscape, adr, NULL);
kvn@3651 603 break;
kvn@3651 604 }
kvn@3651 605 ELSE_FAIL("Op_LoadP");
kvn@3651 606 }
kvn@3651 607 case Op_Phi: {
kvn@3651 608 // Using isa_ptr() instead of isa_oopptr() for LoadP and Phi because
roland@4106 609 // ThreadLocal has RawPtr type.
kvn@3651 610 const Type* t = n->as_Phi()->type();
kvn@3651 611 if (t->make_ptr() != NULL) {
kvn@3651 612 for (uint i = 1; i < n->req(); i++) {
kvn@3651 613 Node* in = n->in(i);
kvn@3651 614 if (in == NULL)
kvn@3651 615 continue; // ignore NULL
kvn@3651 616 Node* uncast_in = in->uncast();
kvn@3651 617 if (uncast_in->is_top() || uncast_in == n)
kvn@3651 618 continue; // ignore top or inputs which go back this node
kvn@3651 619 PointsToNode* ptn = ptnode_adr(in->_idx);
kvn@3651 620 assert(ptn != NULL, "node should be registered");
kvn@3651 621 add_edge(n_ptn, ptn);
kvn@3651 622 }
kvn@3651 623 break;
kvn@3651 624 }
kvn@3651 625 ELSE_FAIL("Op_Phi");
kvn@3651 626 }
kvn@3651 627 case Op_Proj: {
kvn@3651 628 // we are only interested in the oop result projection from a call
kvn@3651 629 if (n->as_Proj()->_con == TypeFunc::Parms && n->in(0)->is_Call() &&
kvn@3651 630 n->in(0)->as_Call()->returns_pointer()) {
kvn@3651 631 add_local_var_and_edge(n, PointsToNode::NoEscape, n->in(0), NULL);
kvn@3651 632 break;
kvn@3651 633 }
kvn@3651 634 ELSE_FAIL("Op_Proj");
kvn@3651 635 }
kvn@3651 636 case Op_Rethrow: // Exception object escapes
kvn@3651 637 case Op_Return: {
kvn@3651 638 if (n->req() > TypeFunc::Parms &&
kvn@3651 639 _igvn->type(n->in(TypeFunc::Parms))->isa_oopptr()) {
kvn@3651 640 // Treat Return value as LocalVar with GlobalEscape escape state.
kvn@3651 641 add_local_var_and_edge(n, PointsToNode::GlobalEscape,
kvn@3651 642 n->in(TypeFunc::Parms), NULL);
kvn@3651 643 break;
kvn@3651 644 }
kvn@3651 645 ELSE_FAIL("Op_Return");
kvn@3651 646 }
kvn@3651 647 case Op_StoreP:
kvn@3651 648 case Op_StoreN:
kvn@3651 649 case Op_StorePConditional:
kvn@3651 650 case Op_CompareAndSwapP:
roland@4106 651 case Op_CompareAndSwapN:
roland@4106 652 case Op_GetAndSetP:
roland@4106 653 case Op_GetAndSetN: {
kvn@3651 654 Node* adr = n->in(MemNode::Address);
roland@4106 655 if (opcode == Op_GetAndSetP || opcode == Op_GetAndSetN) {
roland@4106 656 const Type* t = _igvn->type(n);
roland@4106 657 if (t->make_ptr() != NULL) {
roland@4106 658 add_local_var_and_edge(n, PointsToNode::NoEscape, adr, NULL);
roland@4106 659 }
roland@4106 660 }
kvn@3651 661 const Type *adr_type = _igvn->type(adr);
kvn@3651 662 adr_type = adr_type->make_ptr();
kvn@3651 663 if (adr_type->isa_oopptr() ||
kvn@3651 664 (opcode == Op_StoreP || opcode == Op_StoreN) &&
kvn@3651 665 (adr_type == TypeRawPtr::NOTNULL &&
kvn@3651 666 adr->in(AddPNode::Address)->is_Proj() &&
kvn@3651 667 adr->in(AddPNode::Address)->in(0)->is_Allocate())) {
kvn@3651 668 // Point Address to Value
kvn@3651 669 PointsToNode* adr_ptn = ptnode_adr(adr->_idx);
kvn@3651 670 assert(adr_ptn != NULL &&
kvn@3651 671 adr_ptn->as_Field()->is_oop(), "node should be registered");
kvn@3651 672 Node *val = n->in(MemNode::ValueIn);
kvn@3651 673 PointsToNode* ptn = ptnode_adr(val->_idx);
kvn@3651 674 assert(ptn != NULL, "node should be registered");
kvn@3651 675 add_edge(adr_ptn, ptn);
kvn@3651 676 break;
kvn@3657 677 } else if ((opcode == Op_StoreP) && (adr_type == TypeRawPtr::BOTTOM)) {
kvn@3657 678 // Stored value escapes in unsafe access.
kvn@3657 679 Node *val = n->in(MemNode::ValueIn);
kvn@3657 680 PointsToNode* ptn = ptnode_adr(val->_idx);
kvn@3657 681 assert(ptn != NULL, "node should be registered");
kvn@3657 682 ptn->set_escape_state(PointsToNode::GlobalEscape);
kvn@3657 683 // Add edge to object for unsafe access with offset.
kvn@3657 684 PointsToNode* adr_ptn = ptnode_adr(adr->_idx);
kvn@3657 685 assert(adr_ptn != NULL, "node should be registered");
kvn@3657 686 if (adr_ptn->is_Field()) {
kvn@3657 687 assert(adr_ptn->as_Field()->is_oop(), "should be oop field");
kvn@3657 688 add_edge(adr_ptn, ptn);
kvn@3657 689 }
kvn@3657 690 break;
kvn@3651 691 }
kvn@3651 692 ELSE_FAIL("Op_StoreP");
kvn@3651 693 }
kvn@3651 694 case Op_AryEq:
kvn@3651 695 case Op_StrComp:
kvn@3651 696 case Op_StrEquals:
kvn@3651 697 case Op_StrIndexOf: {
kvn@3651 698 // char[] arrays passed to string intrinsic do not escape but
kvn@3651 699 // they are not scalar replaceable. Adjust escape state for them.
kvn@3651 700 // Start from in(2) edge since in(1) is memory edge.
kvn@3651 701 for (uint i = 2; i < n->req(); i++) {
kvn@3651 702 Node* adr = n->in(i);
kvn@3651 703 const Type* at = _igvn->type(adr);
kvn@3651 704 if (!adr->is_top() && at->isa_ptr()) {
kvn@3651 705 assert(at == Type::TOP || at == TypePtr::NULL_PTR ||
kvn@3651 706 at->isa_ptr() != NULL, "expecting a pointer");
kvn@3651 707 if (adr->is_AddP()) {
kvn@3651 708 adr = get_addp_base(adr);
kvn@3651 709 }
kvn@3651 710 PointsToNode* ptn = ptnode_adr(adr->_idx);
kvn@3651 711 assert(ptn != NULL, "node should be registered");
kvn@3651 712 add_edge(n_ptn, ptn);
kvn@3651 713 }
kvn@3651 714 }
kvn@3651 715 break;
kvn@3651 716 }
kvn@3651 717 default: {
kvn@3651 718 // This method should be called only for EA specific nodes which may
kvn@3651 719 // miss some edges when they were created.
kvn@3651 720 #ifdef ASSERT
kvn@3651 721 n->dump(1);
kvn@3651 722 #endif
kvn@3651 723 guarantee(false, "unknown node");
kvn@3651 724 }
kvn@3651 725 }
kvn@3651 726 return;
kvn@3651 727 }
kvn@3651 728
kvn@3651 729 void ConnectionGraph::add_call_node(CallNode* call) {
kvn@3651 730 assert(call->returns_pointer(), "only for call which returns pointer");
kvn@3651 731 uint call_idx = call->_idx;
kvn@3651 732 if (call->is_Allocate()) {
kvn@3651 733 Node* k = call->in(AllocateNode::KlassNode);
kvn@3651 734 const TypeKlassPtr* kt = k->bottom_type()->isa_klassptr();
kvn@3651 735 assert(kt != NULL, "TypeKlassPtr required.");
kvn@3651 736 ciKlass* cik = kt->klass();
kvn@3651 737 PointsToNode::EscapeState es = PointsToNode::NoEscape;
kvn@3651 738 bool scalar_replaceable = true;
kvn@3651 739 if (call->is_AllocateArray()) {
kvn@3651 740 if (!cik->is_array_klass()) { // StressReflectiveCode
kvn@3651 741 es = PointsToNode::GlobalEscape;
kvn@3651 742 } else {
kvn@3651 743 int length = call->in(AllocateNode::ALength)->find_int_con(-1);
kvn@3651 744 if (length < 0 || length > EliminateAllocationArraySizeLimit) {
kvn@3651 745 // Not scalar replaceable if the length is not constant or too big.
kvn@3651 746 scalar_replaceable = false;
kvn@3651 747 }
kvn@3651 748 }
kvn@3651 749 } else { // Allocate instance
kvn@3651 750 if (cik->is_subclass_of(_compile->env()->Thread_klass()) ||
kvn@3651 751 !cik->is_instance_klass() || // StressReflectiveCode
kvn@3651 752 cik->as_instance_klass()->has_finalizer()) {
kvn@3651 753 es = PointsToNode::GlobalEscape;
kvn@3651 754 }
kvn@3651 755 }
kvn@3651 756 add_java_object(call, es);
kvn@3651 757 PointsToNode* ptn = ptnode_adr(call_idx);
kvn@3651 758 if (!scalar_replaceable && ptn->scalar_replaceable()) {
kvn@3651 759 ptn->set_scalar_replaceable(false);
kvn@3651 760 }
kvn@3651 761 } else if (call->is_CallStaticJava()) {
kvn@3651 762 // Call nodes could be different types:
kvn@3651 763 //
kvn@3651 764 // 1. CallDynamicJavaNode (what happened during call is unknown):
kvn@3651 765 //
kvn@3651 766 // - mapped to GlobalEscape JavaObject node if oop is returned;
kvn@3651 767 //
kvn@3651 768 // - all oop arguments are escaping globally;
kvn@3651 769 //
kvn@3651 770 // 2. CallStaticJavaNode (execute bytecode analysis if possible):
kvn@3651 771 //
kvn@3651 772 // - the same as CallDynamicJavaNode if can't do bytecode analysis;
kvn@3651 773 //
kvn@3651 774 // - mapped to GlobalEscape JavaObject node if unknown oop is returned;
kvn@3651 775 // - mapped to NoEscape JavaObject node if non-escaping object allocated
kvn@3651 776 // during call is returned;
kvn@3651 777 // - mapped to ArgEscape LocalVar node pointed to object arguments
kvn@3651 778 // which are returned and does not escape during call;
kvn@3651 779 //
kvn@3651 780 // - oop arguments escaping status is defined by bytecode analysis;
kvn@3651 781 //
kvn@3651 782 // For a static call, we know exactly what method is being called.
kvn@3651 783 // Use bytecode estimator to record whether the call's return value escapes.
kvn@3651 784 ciMethod* meth = call->as_CallJava()->method();
kvn@3651 785 if (meth == NULL) {
kvn@3651 786 const char* name = call->as_CallStaticJava()->_name;
kvn@3651 787 assert(strncmp(name, "_multianewarray", 15) == 0, "TODO: add failed case check");
kvn@3651 788 // Returns a newly allocated unescaped object.
kvn@3651 789 add_java_object(call, PointsToNode::NoEscape);
kvn@3651 790 ptnode_adr(call_idx)->set_scalar_replaceable(false);
kvn@3651 791 } else {
kvn@3651 792 BCEscapeAnalyzer* call_analyzer = meth->get_bcea();
kvn@3651 793 call_analyzer->copy_dependencies(_compile->dependencies());
kvn@3651 794 if (call_analyzer->is_return_allocated()) {
kvn@3651 795 // Returns a newly allocated unescaped object, simply
kvn@3651 796 // update dependency information.
kvn@3651 797 // Mark it as NoEscape so that objects referenced by
kvn@3651 798 // it's fields will be marked as NoEscape at least.
kvn@3651 799 add_java_object(call, PointsToNode::NoEscape);
kvn@3651 800 ptnode_adr(call_idx)->set_scalar_replaceable(false);
kvn@3651 801 } else {
kvn@3651 802 // Determine whether any arguments are returned.
kvn@3651 803 const TypeTuple* d = call->tf()->domain();
kvn@3651 804 bool ret_arg = false;
kvn@3651 805 for (uint i = TypeFunc::Parms; i < d->cnt(); i++) {
kvn@3651 806 if (d->field_at(i)->isa_ptr() != NULL &&
kvn@3651 807 call_analyzer->is_arg_returned(i - TypeFunc::Parms)) {
kvn@3651 808 ret_arg = true;
kvn@3651 809 break;
kvn@3651 810 }
kvn@3651 811 }
kvn@3651 812 if (ret_arg) {
kvn@3651 813 add_local_var(call, PointsToNode::ArgEscape);
kvn@3651 814 } else {
kvn@3651 815 // Returns unknown object.
kvn@3651 816 map_ideal_node(call, phantom_obj);
kvn@3651 817 }
kvn@3651 818 }
kvn@3651 819 }
kvn@3651 820 } else {
kvn@3651 821 // An other type of call, assume the worst case:
kvn@3651 822 // returned value is unknown and globally escapes.
kvn@3651 823 assert(call->Opcode() == Op_CallDynamicJava, "add failed case check");
kvn@3651 824 map_ideal_node(call, phantom_obj);
kvn@3651 825 }
kvn@3651 826 }
kvn@3651 827
kvn@3651 828 void ConnectionGraph::process_call_arguments(CallNode *call) {
kvn@3651 829 bool is_arraycopy = false;
kvn@3651 830 switch (call->Opcode()) {
kvn@3651 831 #ifdef ASSERT
kvn@3651 832 case Op_Allocate:
kvn@3651 833 case Op_AllocateArray:
kvn@3651 834 case Op_Lock:
kvn@3651 835 case Op_Unlock:
kvn@3651 836 assert(false, "should be done already");
kvn@3651 837 break;
kvn@3651 838 #endif
kvn@3651 839 case Op_CallLeafNoFP:
kvn@3651 840 is_arraycopy = (call->as_CallLeaf()->_name != NULL &&
kvn@3651 841 strstr(call->as_CallLeaf()->_name, "arraycopy") != 0);
kvn@3651 842 // fall through
kvn@3651 843 case Op_CallLeaf: {
kvn@3651 844 // Stub calls, objects do not escape but they are not scale replaceable.
kvn@3651 845 // Adjust escape state for outgoing arguments.
kvn@3651 846 const TypeTuple * d = call->tf()->domain();
kvn@3651 847 bool src_has_oops = false;
kvn@3651 848 for (uint i = TypeFunc::Parms; i < d->cnt(); i++) {
kvn@3651 849 const Type* at = d->field_at(i);
kvn@3651 850 Node *arg = call->in(i);
kvn@3651 851 const Type *aat = _igvn->type(arg);
kvn@3651 852 if (arg->is_top() || !at->isa_ptr() || !aat->isa_ptr())
kvn@3651 853 continue;
kvn@3651 854 if (arg->is_AddP()) {
kvn@3651 855 //
kvn@3651 856 // The inline_native_clone() case when the arraycopy stub is called
kvn@3651 857 // after the allocation before Initialize and CheckCastPP nodes.
kvn@3651 858 // Or normal arraycopy for object arrays case.
kvn@3651 859 //
kvn@3651 860 // Set AddP's base (Allocate) as not scalar replaceable since
kvn@3651 861 // pointer to the base (with offset) is passed as argument.
kvn@3651 862 //
kvn@3651 863 arg = get_addp_base(arg);
kvn@3651 864 }
kvn@3651 865 PointsToNode* arg_ptn = ptnode_adr(arg->_idx);
kvn@3651 866 assert(arg_ptn != NULL, "should be registered");
kvn@3651 867 PointsToNode::EscapeState arg_esc = arg_ptn->escape_state();
kvn@3651 868 if (is_arraycopy || arg_esc < PointsToNode::ArgEscape) {
kvn@3651 869 assert(aat == Type::TOP || aat == TypePtr::NULL_PTR ||
kvn@3651 870 aat->isa_ptr() != NULL, "expecting an Ptr");
kvn@3651 871 bool arg_has_oops = aat->isa_oopptr() &&
kvn@3651 872 (aat->isa_oopptr()->klass() == NULL || aat->isa_instptr() ||
kvn@3651 873 (aat->isa_aryptr() && aat->isa_aryptr()->klass()->is_obj_array_klass()));
kvn@3651 874 if (i == TypeFunc::Parms) {
kvn@3651 875 src_has_oops = arg_has_oops;
kvn@3651 876 }
kvn@3651 877 //
kvn@3651 878 // src or dst could be j.l.Object when other is basic type array:
kvn@3651 879 //
kvn@3651 880 // arraycopy(char[],0,Object*,0,size);
kvn@3651 881 // arraycopy(Object*,0,char[],0,size);
kvn@3651 882 //
kvn@3651 883 // Don't add edges in such cases.
kvn@3651 884 //
kvn@3651 885 bool arg_is_arraycopy_dest = src_has_oops && is_arraycopy &&
kvn@3651 886 arg_has_oops && (i > TypeFunc::Parms);
kvn@3651 887 #ifdef ASSERT
kvn@3651 888 if (!(is_arraycopy ||
kvn@3651 889 call->as_CallLeaf()->_name != NULL &&
kvn@3651 890 (strcmp(call->as_CallLeaf()->_name, "g1_wb_pre") == 0 ||
kvn@3651 891 strcmp(call->as_CallLeaf()->_name, "g1_wb_post") == 0 ))
kvn@3651 892 ) {
kvn@3651 893 call->dump();
kvn@3651 894 assert(false, "EA: unexpected CallLeaf");
kvn@3651 895 }
kvn@3651 896 #endif
kvn@3651 897 // Always process arraycopy's destination object since
kvn@3651 898 // we need to add all possible edges to references in
kvn@3651 899 // source object.
kvn@3651 900 if (arg_esc >= PointsToNode::ArgEscape &&
kvn@3651 901 !arg_is_arraycopy_dest) {
kvn@3651 902 continue;
kvn@3651 903 }
kvn@3651 904 set_escape_state(arg_ptn, PointsToNode::ArgEscape);
kvn@3651 905 if (arg_is_arraycopy_dest) {
kvn@3651 906 Node* src = call->in(TypeFunc::Parms);
kvn@3651 907 if (src->is_AddP()) {
kvn@3651 908 src = get_addp_base(src);
kvn@3651 909 }
kvn@3651 910 PointsToNode* src_ptn = ptnode_adr(src->_idx);
kvn@3651 911 assert(src_ptn != NULL, "should be registered");
kvn@3651 912 if (arg_ptn != src_ptn) {
kvn@3651 913 // Special arraycopy edge:
kvn@3651 914 // A destination object's field can't have the source object
kvn@3651 915 // as base since objects escape states are not related.
kvn@3651 916 // Only escape state of destination object's fields affects
kvn@3651 917 // escape state of fields in source object.
kvn@3651 918 add_arraycopy(call, PointsToNode::ArgEscape, src_ptn, arg_ptn);
kvn@3651 919 }
kvn@3651 920 }
kvn@3651 921 }
kvn@3651 922 }
kvn@3651 923 break;
kvn@3651 924 }
kvn@3651 925 case Op_CallStaticJava: {
kvn@3651 926 // For a static call, we know exactly what method is being called.
kvn@3651 927 // Use bytecode estimator to record the call's escape affects
kvn@3651 928 #ifdef ASSERT
kvn@3651 929 const char* name = call->as_CallStaticJava()->_name;
kvn@3651 930 assert((name == NULL || strcmp(name, "uncommon_trap") != 0), "normal calls only");
kvn@3651 931 #endif
kvn@3651 932 ciMethod* meth = call->as_CallJava()->method();
kvn@3651 933 BCEscapeAnalyzer* call_analyzer = (meth !=NULL) ? meth->get_bcea() : NULL;
kvn@3651 934 // fall-through if not a Java method or no analyzer information
kvn@3651 935 if (call_analyzer != NULL) {
kvn@3651 936 PointsToNode* call_ptn = ptnode_adr(call->_idx);
kvn@3651 937 const TypeTuple* d = call->tf()->domain();
kvn@3651 938 for (uint i = TypeFunc::Parms; i < d->cnt(); i++) {
kvn@3651 939 const Type* at = d->field_at(i);
kvn@3651 940 int k = i - TypeFunc::Parms;
kvn@3651 941 Node* arg = call->in(i);
kvn@3651 942 PointsToNode* arg_ptn = ptnode_adr(arg->_idx);
kvn@3651 943 if (at->isa_ptr() != NULL &&
kvn@3651 944 call_analyzer->is_arg_returned(k)) {
kvn@3651 945 // The call returns arguments.
kvn@3651 946 if (call_ptn != NULL) { // Is call's result used?
kvn@3651 947 assert(call_ptn->is_LocalVar(), "node should be registered");
kvn@3651 948 assert(arg_ptn != NULL, "node should be registered");
kvn@3651 949 add_edge(call_ptn, arg_ptn);
kvn@3651 950 }
kvn@3651 951 }
kvn@3651 952 if (at->isa_oopptr() != NULL &&
kvn@3651 953 arg_ptn->escape_state() < PointsToNode::GlobalEscape) {
kvn@3651 954 if (!call_analyzer->is_arg_stack(k)) {
kvn@3651 955 // The argument global escapes
kvn@3651 956 set_escape_state(arg_ptn, PointsToNode::GlobalEscape);
kvn@3651 957 } else {
kvn@3651 958 set_escape_state(arg_ptn, PointsToNode::ArgEscape);
kvn@3651 959 if (!call_analyzer->is_arg_local(k)) {
kvn@3651 960 // The argument itself doesn't escape, but any fields might
kvn@3651 961 set_fields_escape_state(arg_ptn, PointsToNode::GlobalEscape);
kvn@3651 962 }
kvn@3651 963 }
kvn@3651 964 }
kvn@3651 965 }
kvn@3651 966 if (call_ptn != NULL && call_ptn->is_LocalVar()) {
kvn@3651 967 // The call returns arguments.
kvn@3651 968 assert(call_ptn->edge_count() > 0, "sanity");
kvn@3651 969 if (!call_analyzer->is_return_local()) {
kvn@3651 970 // Returns also unknown object.
kvn@3651 971 add_edge(call_ptn, phantom_obj);
kvn@3651 972 }
kvn@3651 973 }
kvn@3651 974 break;
kvn@3651 975 }
kvn@3651 976 }
kvn@3651 977 default: {
kvn@3651 978 // Fall-through here if not a Java method or no analyzer information
kvn@3651 979 // or some other type of call, assume the worst case: all arguments
kvn@3651 980 // globally escape.
kvn@3651 981 const TypeTuple* d = call->tf()->domain();
kvn@3651 982 for (uint i = TypeFunc::Parms; i < d->cnt(); i++) {
kvn@3651 983 const Type* at = d->field_at(i);
kvn@3651 984 if (at->isa_oopptr() != NULL) {
kvn@3651 985 Node* arg = call->in(i);
kvn@3651 986 if (arg->is_AddP()) {
kvn@3651 987 arg = get_addp_base(arg);
kvn@3651 988 }
kvn@3651 989 assert(ptnode_adr(arg->_idx) != NULL, "should be defined already");
kvn@3651 990 set_escape_state(ptnode_adr(arg->_idx), PointsToNode::GlobalEscape);
kvn@3651 991 }
kvn@3651 992 }
kvn@3651 993 }
kvn@3651 994 }
kvn@3651 995 }
kvn@3651 996
kvn@3651 997
kvn@3651 998 // Finish Graph construction.
kvn@3651 999 bool ConnectionGraph::complete_connection_graph(
kvn@3651 1000 GrowableArray<PointsToNode*>& ptnodes_worklist,
kvn@3651 1001 GrowableArray<JavaObjectNode*>& non_escaped_worklist,
kvn@3651 1002 GrowableArray<JavaObjectNode*>& java_objects_worklist,
kvn@3651 1003 GrowableArray<FieldNode*>& oop_fields_worklist) {
kvn@3651 1004 // Normally only 1-3 passes needed to build Connection Graph depending
kvn@3651 1005 // on graph complexity. Observed 8 passes in jvm2008 compiler.compiler.
kvn@3651 1006 // Set limit to 20 to catch situation when something did go wrong and
kvn@3651 1007 // bailout Escape Analysis.
kvn@3651 1008 // Also limit build time to 30 sec (60 in debug VM).
kvn@3651 1009 #define CG_BUILD_ITER_LIMIT 20
kvn@3651 1010 #ifdef ASSERT
kvn@3651 1011 #define CG_BUILD_TIME_LIMIT 60.0
kvn@3651 1012 #else
kvn@3651 1013 #define CG_BUILD_TIME_LIMIT 30.0
kvn@3651 1014 #endif
kvn@3651 1015
kvn@3651 1016 // Propagate GlobalEscape and ArgEscape escape states and check that
kvn@3651 1017 // we still have non-escaping objects. The method pushs on _worklist
kvn@3651 1018 // Field nodes which reference phantom_object.
kvn@3651 1019 if (!find_non_escaped_objects(ptnodes_worklist, non_escaped_worklist)) {
kvn@3651 1020 return false; // Nothing to do.
kvn@3651 1021 }
kvn@3651 1022 // Now propagate references to all JavaObject nodes.
kvn@3651 1023 int java_objects_length = java_objects_worklist.length();
kvn@3651 1024 elapsedTimer time;
kvn@3651 1025 int new_edges = 1;
kvn@3651 1026 int iterations = 0;
kvn@3651 1027 do {
kvn@3651 1028 while ((new_edges > 0) &&
kvn@3651 1029 (iterations++ < CG_BUILD_ITER_LIMIT) &&
kvn@3651 1030 (time.seconds() < CG_BUILD_TIME_LIMIT)) {
kvn@3651 1031 time.start();
kvn@3651 1032 new_edges = 0;
kvn@3651 1033 // Propagate references to phantom_object for nodes pushed on _worklist
kvn@3651 1034 // by find_non_escaped_objects() and find_field_value().
kvn@3651 1035 new_edges += add_java_object_edges(phantom_obj, false);
kvn@3651 1036 for (int next = 0; next < java_objects_length; ++next) {
kvn@3651 1037 JavaObjectNode* ptn = java_objects_worklist.at(next);
kvn@3651 1038 new_edges += add_java_object_edges(ptn, true);
kvn@3651 1039 }
kvn@3651 1040 if (new_edges > 0) {
kvn@3651 1041 // Update escape states on each iteration if graph was updated.
kvn@3651 1042 if (!find_non_escaped_objects(ptnodes_worklist, non_escaped_worklist)) {
kvn@3651 1043 return false; // Nothing to do.
kvn@3651 1044 }
kvn@3651 1045 }
kvn@3651 1046 time.stop();
kvn@3651 1047 }
kvn@3651 1048 if ((iterations < CG_BUILD_ITER_LIMIT) &&
kvn@3651 1049 (time.seconds() < CG_BUILD_TIME_LIMIT)) {
kvn@3651 1050 time.start();
kvn@3651 1051 // Find fields which have unknown value.
kvn@3651 1052 int fields_length = oop_fields_worklist.length();
kvn@3651 1053 for (int next = 0; next < fields_length; next++) {
kvn@3651 1054 FieldNode* field = oop_fields_worklist.at(next);
kvn@3651 1055 if (field->edge_count() == 0) {
kvn@3651 1056 new_edges += find_field_value(field);
kvn@3651 1057 // This code may added new edges to phantom_object.
kvn@3651 1058 // Need an other cycle to propagate references to phantom_object.
kvn@3651 1059 }
kvn@3651 1060 }
kvn@3651 1061 time.stop();
kvn@3651 1062 } else {
kvn@3651 1063 new_edges = 0; // Bailout
kvn@3651 1064 }
kvn@3651 1065 } while (new_edges > 0);
kvn@3651 1066
kvn@3651 1067 // Bailout if passed limits.
kvn@3651 1068 if ((iterations >= CG_BUILD_ITER_LIMIT) ||
kvn@3651 1069 (time.seconds() >= CG_BUILD_TIME_LIMIT)) {
kvn@3651 1070 Compile* C = _compile;
kvn@3651 1071 if (C->log() != NULL) {
kvn@3651 1072 C->log()->begin_elem("connectionGraph_bailout reason='reached ");
kvn@3651 1073 C->log()->text("%s", (iterations >= CG_BUILD_ITER_LIMIT) ? "iterations" : "time");
kvn@3651 1074 C->log()->end_elem(" limit'");
kvn@3651 1075 }
kvn@3971 1076 assert(false, err_msg_res("infinite EA connection graph build (%f sec, %d iterations) with %d nodes and worklist size %d",
kvn@3651 1077 time.seconds(), iterations, nodes_size(), ptnodes_worklist.length()));
kvn@3651 1078 // Possible infinite build_connection_graph loop,
kvn@3651 1079 // bailout (no changes to ideal graph were made).
kvn@3651 1080 return false;
kvn@3651 1081 }
kvn@3651 1082 #ifdef ASSERT
kvn@3651 1083 if (Verbose && PrintEscapeAnalysis) {
kvn@3651 1084 tty->print_cr("EA: %d iterations to build connection graph with %d nodes and worklist size %d",
kvn@3651 1085 iterations, nodes_size(), ptnodes_worklist.length());
kvn@3651 1086 }
kvn@3651 1087 #endif
kvn@3651 1088
kvn@3651 1089 #undef CG_BUILD_ITER_LIMIT
kvn@3651 1090 #undef CG_BUILD_TIME_LIMIT
kvn@3651 1091
kvn@3651 1092 // Find fields initialized by NULL for non-escaping Allocations.
kvn@3651 1093 int non_escaped_length = non_escaped_worklist.length();
kvn@3651 1094 for (int next = 0; next < non_escaped_length; next++) {
kvn@3651 1095 JavaObjectNode* ptn = non_escaped_worklist.at(next);
kvn@3651 1096 PointsToNode::EscapeState es = ptn->escape_state();
kvn@3651 1097 assert(es <= PointsToNode::ArgEscape, "sanity");
kvn@3651 1098 if (es == PointsToNode::NoEscape) {
kvn@3651 1099 if (find_init_values(ptn, null_obj, _igvn) > 0) {
kvn@3651 1100 // Adding references to NULL object does not change escape states
kvn@3651 1101 // since it does not escape. Also no fields are added to NULL object.
kvn@3651 1102 add_java_object_edges(null_obj, false);
kvn@3651 1103 }
kvn@3651 1104 }
kvn@3651 1105 Node* n = ptn->ideal_node();
kvn@3651 1106 if (n->is_Allocate()) {
kvn@3651 1107 // The object allocated by this Allocate node will never be
kvn@3651 1108 // seen by an other thread. Mark it so that when it is
kvn@3651 1109 // expanded no MemBarStoreStore is added.
kvn@3651 1110 InitializeNode* ini = n->as_Allocate()->initialization();
kvn@3651 1111 if (ini != NULL)
kvn@3651 1112 ini->set_does_not_escape();
kvn@3651 1113 }
kvn@3651 1114 }
kvn@3651 1115 return true; // Finished graph construction.
kvn@3651 1116 }
kvn@3651 1117
kvn@3651 1118 // Propagate GlobalEscape and ArgEscape escape states to all nodes
kvn@3651 1119 // and check that we still have non-escaping java objects.
kvn@3651 1120 bool ConnectionGraph::find_non_escaped_objects(GrowableArray<PointsToNode*>& ptnodes_worklist,
kvn@3651 1121 GrowableArray<JavaObjectNode*>& non_escaped_worklist) {
kvn@3651 1122 GrowableArray<PointsToNode*> escape_worklist;
kvn@3651 1123 // First, put all nodes with GlobalEscape and ArgEscape states on worklist.
kvn@3651 1124 int ptnodes_length = ptnodes_worklist.length();
kvn@3651 1125 for (int next = 0; next < ptnodes_length; ++next) {
kvn@3651 1126 PointsToNode* ptn = ptnodes_worklist.at(next);
kvn@3651 1127 if (ptn->escape_state() >= PointsToNode::ArgEscape ||
kvn@3651 1128 ptn->fields_escape_state() >= PointsToNode::ArgEscape) {
kvn@3651 1129 escape_worklist.push(ptn);
kvn@3651 1130 }
kvn@3651 1131 }
kvn@3651 1132 // Set escape states to referenced nodes (edges list).
kvn@3651 1133 while (escape_worklist.length() > 0) {
kvn@3651 1134 PointsToNode* ptn = escape_worklist.pop();
kvn@3651 1135 PointsToNode::EscapeState es = ptn->escape_state();
kvn@3651 1136 PointsToNode::EscapeState field_es = ptn->fields_escape_state();
kvn@3651 1137 if (ptn->is_Field() && ptn->as_Field()->is_oop() &&
kvn@3651 1138 es >= PointsToNode::ArgEscape) {
kvn@3651 1139 // GlobalEscape or ArgEscape state of field means it has unknown value.
kvn@3651 1140 if (add_edge(ptn, phantom_obj)) {
kvn@3651 1141 // New edge was added
kvn@3651 1142 add_field_uses_to_worklist(ptn->as_Field());
kvn@3651 1143 }
kvn@3651 1144 }
kvn@3651 1145 for (EdgeIterator i(ptn); i.has_next(); i.next()) {
kvn@3651 1146 PointsToNode* e = i.get();
kvn@3651 1147 if (e->is_Arraycopy()) {
kvn@3651 1148 assert(ptn->arraycopy_dst(), "sanity");
kvn@3651 1149 // Propagate only fields escape state through arraycopy edge.
kvn@3651 1150 if (e->fields_escape_state() < field_es) {
kvn@3651 1151 set_fields_escape_state(e, field_es);
kvn@3651 1152 escape_worklist.push(e);
kvn@3651 1153 }
kvn@3651 1154 } else if (es >= field_es) {
kvn@3651 1155 // fields_escape_state is also set to 'es' if it is less than 'es'.
kvn@3651 1156 if (e->escape_state() < es) {
kvn@3651 1157 set_escape_state(e, es);
kvn@3651 1158 escape_worklist.push(e);
kvn@3651 1159 }
kvn@3651 1160 } else {
kvn@3651 1161 // Propagate field escape state.
kvn@3651 1162 bool es_changed = false;
kvn@3651 1163 if (e->fields_escape_state() < field_es) {
kvn@3651 1164 set_fields_escape_state(e, field_es);
kvn@3651 1165 es_changed = true;
kvn@3651 1166 }
kvn@3651 1167 if ((e->escape_state() < field_es) &&
kvn@3651 1168 e->is_Field() && ptn->is_JavaObject() &&
kvn@3651 1169 e->as_Field()->is_oop()) {
kvn@3651 1170 // Change escape state of referenced fileds.
kvn@3651 1171 set_escape_state(e, field_es);
kvn@3651 1172 es_changed = true;;
kvn@3651 1173 } else if (e->escape_state() < es) {
kvn@3651 1174 set_escape_state(e, es);
kvn@3651 1175 es_changed = true;;
kvn@3651 1176 }
kvn@3651 1177 if (es_changed) {
kvn@3651 1178 escape_worklist.push(e);
kvn@3651 1179 }
kvn@3651 1180 }
kvn@3651 1181 }
kvn@3651 1182 }
kvn@3651 1183 // Remove escaped objects from non_escaped list.
kvn@3651 1184 for (int next = non_escaped_worklist.length()-1; next >= 0 ; --next) {
kvn@3651 1185 JavaObjectNode* ptn = non_escaped_worklist.at(next);
kvn@3651 1186 if (ptn->escape_state() >= PointsToNode::GlobalEscape) {
kvn@3651 1187 non_escaped_worklist.delete_at(next);
kvn@3651 1188 }
kvn@3651 1189 if (ptn->escape_state() == PointsToNode::NoEscape) {
kvn@3651 1190 // Find fields in non-escaped allocations which have unknown value.
kvn@3651 1191 find_init_values(ptn, phantom_obj, NULL);
kvn@3651 1192 }
kvn@3651 1193 }
kvn@3651 1194 return (non_escaped_worklist.length() > 0);
kvn@3651 1195 }
kvn@3651 1196
kvn@3651 1197 // Add all references to JavaObject node by walking over all uses.
kvn@3651 1198 int ConnectionGraph::add_java_object_edges(JavaObjectNode* jobj, bool populate_worklist) {
kvn@3651 1199 int new_edges = 0;
kvn@3651 1200 if (populate_worklist) {
kvn@3651 1201 // Populate _worklist by uses of jobj's uses.
kvn@3651 1202 for (UseIterator i(jobj); i.has_next(); i.next()) {
kvn@3651 1203 PointsToNode* use = i.get();
kvn@3651 1204 if (use->is_Arraycopy())
kvn@3651 1205 continue;
kvn@3651 1206 add_uses_to_worklist(use);
kvn@3651 1207 if (use->is_Field() && use->as_Field()->is_oop()) {
kvn@3651 1208 // Put on worklist all field's uses (loads) and
kvn@3651 1209 // related field nodes (same base and offset).
kvn@3651 1210 add_field_uses_to_worklist(use->as_Field());
kvn@3651 1211 }
kvn@3651 1212 }
kvn@3651 1213 }
kvn@3651 1214 while(_worklist.length() > 0) {
kvn@3651 1215 PointsToNode* use = _worklist.pop();
kvn@3651 1216 if (PointsToNode::is_base_use(use)) {
kvn@3651 1217 // Add reference from jobj to field and from field to jobj (field's base).
kvn@3651 1218 use = PointsToNode::get_use_node(use)->as_Field();
kvn@3651 1219 if (add_base(use->as_Field(), jobj)) {
kvn@3651 1220 new_edges++;
kvn@3651 1221 }
kvn@3651 1222 continue;
kvn@3651 1223 }
kvn@3651 1224 assert(!use->is_JavaObject(), "sanity");
kvn@3651 1225 if (use->is_Arraycopy()) {
kvn@3651 1226 if (jobj == null_obj) // NULL object does not have field edges
kvn@3651 1227 continue;
kvn@3651 1228 // Added edge from Arraycopy node to arraycopy's source java object
kvn@3651 1229 if (add_edge(use, jobj)) {
kvn@3651 1230 jobj->set_arraycopy_src();
kvn@3651 1231 new_edges++;
kvn@3651 1232 }
kvn@3651 1233 // and stop here.
kvn@3651 1234 continue;
kvn@3651 1235 }
kvn@3651 1236 if (!add_edge(use, jobj))
kvn@3651 1237 continue; // No new edge added, there was such edge already.
kvn@3651 1238 new_edges++;
kvn@3651 1239 if (use->is_LocalVar()) {
kvn@3651 1240 add_uses_to_worklist(use);
kvn@3651 1241 if (use->arraycopy_dst()) {
kvn@3651 1242 for (EdgeIterator i(use); i.has_next(); i.next()) {
kvn@3651 1243 PointsToNode* e = i.get();
kvn@3651 1244 if (e->is_Arraycopy()) {
kvn@3651 1245 if (jobj == null_obj) // NULL object does not have field edges
kvn@3651 1246 continue;
kvn@3651 1247 // Add edge from arraycopy's destination java object to Arraycopy node.
kvn@3651 1248 if (add_edge(jobj, e)) {
kvn@3651 1249 new_edges++;
kvn@3651 1250 jobj->set_arraycopy_dst();
kvn@3651 1251 }
kvn@3651 1252 }
kvn@3651 1253 }
kvn@3651 1254 }
kvn@3651 1255 } else {
kvn@3651 1256 // Added new edge to stored in field values.
kvn@3651 1257 // Put on worklist all field's uses (loads) and
kvn@3651 1258 // related field nodes (same base and offset).
kvn@3651 1259 add_field_uses_to_worklist(use->as_Field());
kvn@3651 1260 }
kvn@3651 1261 }
kvn@3651 1262 return new_edges;
kvn@3651 1263 }
kvn@3651 1264
kvn@3651 1265 // Put on worklist all related field nodes.
kvn@3651 1266 void ConnectionGraph::add_field_uses_to_worklist(FieldNode* field) {
kvn@3651 1267 assert(field->is_oop(), "sanity");
kvn@3651 1268 int offset = field->offset();
kvn@3651 1269 add_uses_to_worklist(field);
kvn@3651 1270 // Loop over all bases of this field and push on worklist Field nodes
kvn@3651 1271 // with the same offset and base (since they may reference the same field).
kvn@3651 1272 for (BaseIterator i(field); i.has_next(); i.next()) {
kvn@3651 1273 PointsToNode* base = i.get();
kvn@3651 1274 add_fields_to_worklist(field, base);
kvn@3651 1275 // Check if the base was source object of arraycopy and go over arraycopy's
kvn@3651 1276 // destination objects since values stored to a field of source object are
kvn@3651 1277 // accessable by uses (loads) of fields of destination objects.
kvn@3651 1278 if (base->arraycopy_src()) {
kvn@3651 1279 for (UseIterator j(base); j.has_next(); j.next()) {
kvn@3651 1280 PointsToNode* arycp = j.get();
kvn@3651 1281 if (arycp->is_Arraycopy()) {
kvn@3651 1282 for (UseIterator k(arycp); k.has_next(); k.next()) {
kvn@3651 1283 PointsToNode* abase = k.get();
kvn@3651 1284 if (abase->arraycopy_dst() && abase != base) {
kvn@3651 1285 // Look for the same arracopy reference.
kvn@3651 1286 add_fields_to_worklist(field, abase);
kvn@3651 1287 }
kvn@3651 1288 }
kvn@3651 1289 }
kvn@3651 1290 }
kvn@3651 1291 }
kvn@3651 1292 }
kvn@3651 1293 }
kvn@3651 1294
kvn@3651 1295 // Put on worklist all related field nodes.
kvn@3651 1296 void ConnectionGraph::add_fields_to_worklist(FieldNode* field, PointsToNode* base) {
kvn@3651 1297 int offset = field->offset();
kvn@3651 1298 if (base->is_LocalVar()) {
kvn@3651 1299 for (UseIterator j(base); j.has_next(); j.next()) {
kvn@3651 1300 PointsToNode* f = j.get();
kvn@3651 1301 if (PointsToNode::is_base_use(f)) { // Field
kvn@3651 1302 f = PointsToNode::get_use_node(f);
kvn@3651 1303 if (f == field || !f->as_Field()->is_oop())
kvn@3651 1304 continue;
kvn@3651 1305 int offs = f->as_Field()->offset();
kvn@3651 1306 if (offs == offset || offset == Type::OffsetBot || offs == Type::OffsetBot) {
kvn@3651 1307 add_to_worklist(f);
kvn@3651 1308 }
kvn@3651 1309 }
kvn@3651 1310 }
kvn@3651 1311 } else {
kvn@3651 1312 assert(base->is_JavaObject(), "sanity");
kvn@3651 1313 if (// Skip phantom_object since it is only used to indicate that
kvn@3651 1314 // this field's content globally escapes.
kvn@3651 1315 (base != phantom_obj) &&
kvn@3651 1316 // NULL object node does not have fields.
kvn@3651 1317 (base != null_obj)) {
kvn@3651 1318 for (EdgeIterator i(base); i.has_next(); i.next()) {
kvn@3651 1319 PointsToNode* f = i.get();
kvn@3651 1320 // Skip arraycopy edge since store to destination object field
kvn@3651 1321 // does not update value in source object field.
kvn@3651 1322 if (f->is_Arraycopy()) {
kvn@3651 1323 assert(base->arraycopy_dst(), "sanity");
kvn@3651 1324 continue;
kvn@3651 1325 }
kvn@3651 1326 if (f == field || !f->as_Field()->is_oop())
kvn@3651 1327 continue;
kvn@3651 1328 int offs = f->as_Field()->offset();
kvn@3651 1329 if (offs == offset || offset == Type::OffsetBot || offs == Type::OffsetBot) {
kvn@3651 1330 add_to_worklist(f);
kvn@3651 1331 }
kvn@3651 1332 }
kvn@3651 1333 }
kvn@3651 1334 }
kvn@3651 1335 }
kvn@3651 1336
kvn@3651 1337 // Find fields which have unknown value.
kvn@3651 1338 int ConnectionGraph::find_field_value(FieldNode* field) {
kvn@3651 1339 // Escaped fields should have init value already.
kvn@3651 1340 assert(field->escape_state() == PointsToNode::NoEscape, "sanity");
kvn@3651 1341 int new_edges = 0;
kvn@3651 1342 for (BaseIterator i(field); i.has_next(); i.next()) {
kvn@3651 1343 PointsToNode* base = i.get();
kvn@3651 1344 if (base->is_JavaObject()) {
kvn@3651 1345 // Skip Allocate's fields which will be processed later.
kvn@3651 1346 if (base->ideal_node()->is_Allocate())
kvn@3651 1347 return 0;
kvn@3651 1348 assert(base == null_obj, "only NULL ptr base expected here");
kvn@3651 1349 }
kvn@3651 1350 }
kvn@3651 1351 if (add_edge(field, phantom_obj)) {
kvn@3651 1352 // New edge was added
kvn@3651 1353 new_edges++;
kvn@3651 1354 add_field_uses_to_worklist(field);
kvn@3651 1355 }
kvn@3651 1356 return new_edges;
kvn@3651 1357 }
kvn@3651 1358
kvn@3651 1359 // Find fields initializing values for allocations.
kvn@3651 1360 int ConnectionGraph::find_init_values(JavaObjectNode* pta, PointsToNode* init_val, PhaseTransform* phase) {
kvn@3651 1361 assert(pta->escape_state() == PointsToNode::NoEscape, "Not escaped Allocate nodes only");
kvn@3651 1362 int new_edges = 0;
kvn@3651 1363 Node* alloc = pta->ideal_node();
kvn@3651 1364 if (init_val == phantom_obj) {
kvn@3651 1365 // Do nothing for Allocate nodes since its fields values are "known".
kvn@3651 1366 if (alloc->is_Allocate())
kvn@3651 1367 return 0;
kvn@3651 1368 assert(alloc->as_CallStaticJava(), "sanity");
kvn@3651 1369 #ifdef ASSERT
kvn@3651 1370 if (alloc->as_CallStaticJava()->method() == NULL) {
kvn@3651 1371 const char* name = alloc->as_CallStaticJava()->_name;
kvn@3651 1372 assert(strncmp(name, "_multianewarray", 15) == 0, "sanity");
kvn@3651 1373 }
kvn@3651 1374 #endif
kvn@3651 1375 // Non-escaped allocation returned from Java or runtime call have
kvn@3651 1376 // unknown values in fields.
kvn@3651 1377 for (EdgeIterator i(pta); i.has_next(); i.next()) {
kvn@3651 1378 PointsToNode* ptn = i.get();
kvn@3651 1379 if (ptn->is_Field() && ptn->as_Field()->is_oop()) {
kvn@3651 1380 if (add_edge(ptn, phantom_obj)) {
kvn@3651 1381 // New edge was added
kvn@3651 1382 new_edges++;
kvn@3651 1383 add_field_uses_to_worklist(ptn->as_Field());
kvn@3651 1384 }
kvn@3651 1385 }
kvn@3651 1386 }
kvn@3651 1387 return new_edges;
kvn@3651 1388 }
kvn@3651 1389 assert(init_val == null_obj, "sanity");
kvn@3651 1390 // Do nothing for Call nodes since its fields values are unknown.
kvn@3651 1391 if (!alloc->is_Allocate())
kvn@3651 1392 return 0;
kvn@3651 1393
kvn@3651 1394 InitializeNode* ini = alloc->as_Allocate()->initialization();
kvn@3651 1395 Compile* C = _compile;
kvn@3651 1396 bool visited_bottom_offset = false;
kvn@3651 1397 GrowableArray<int> offsets_worklist;
kvn@3651 1398
kvn@3651 1399 // Check if an oop field's initializing value is recorded and add
kvn@3651 1400 // a corresponding NULL if field's value if it is not recorded.
kvn@3651 1401 // Connection Graph does not record a default initialization by NULL
kvn@3651 1402 // captured by Initialize node.
kvn@3651 1403 //
kvn@3651 1404 for (EdgeIterator i(pta); i.has_next(); i.next()) {
kvn@3651 1405 PointsToNode* ptn = i.get(); // Field (AddP)
kvn@3651 1406 if (!ptn->is_Field() || !ptn->as_Field()->is_oop())
kvn@3651 1407 continue; // Not oop field
kvn@3651 1408 int offset = ptn->as_Field()->offset();
kvn@3651 1409 if (offset == Type::OffsetBot) {
kvn@3651 1410 if (!visited_bottom_offset) {
kvn@3651 1411 // OffsetBot is used to reference array's element,
kvn@3651 1412 // always add reference to NULL to all Field nodes since we don't
kvn@3651 1413 // known which element is referenced.
kvn@3651 1414 if (add_edge(ptn, null_obj)) {
kvn@3651 1415 // New edge was added
kvn@3651 1416 new_edges++;
kvn@3651 1417 add_field_uses_to_worklist(ptn->as_Field());
kvn@3651 1418 visited_bottom_offset = true;
kvn@3651 1419 }
kvn@3651 1420 }
kvn@3651 1421 } else {
kvn@3651 1422 // Check only oop fields.
kvn@3651 1423 const Type* adr_type = ptn->ideal_node()->as_AddP()->bottom_type();
kvn@3651 1424 if (adr_type->isa_rawptr()) {
kvn@3651 1425 #ifdef ASSERT
kvn@3651 1426 // Raw pointers are used for initializing stores so skip it
kvn@3651 1427 // since it should be recorded already
kvn@3651 1428 Node* base = get_addp_base(ptn->ideal_node());
kvn@3651 1429 assert(adr_type->isa_rawptr() && base->is_Proj() &&
kvn@3651 1430 (base->in(0) == alloc),"unexpected pointer type");
kvn@3651 1431 #endif
kvn@3651 1432 continue;
kvn@3651 1433 }
kvn@3651 1434 if (!offsets_worklist.contains(offset)) {
kvn@3651 1435 offsets_worklist.append(offset);
kvn@3651 1436 Node* value = NULL;
kvn@3651 1437 if (ini != NULL) {
kvn@3651 1438 BasicType ft = UseCompressedOops ? T_NARROWOOP : T_OBJECT;
kvn@3651 1439 Node* store = ini->find_captured_store(offset, type2aelembytes(ft), phase);
kvn@3651 1440 if (store != NULL && store->is_Store()) {
kvn@3651 1441 value = store->in(MemNode::ValueIn);
kvn@3651 1442 } else {
kvn@3651 1443 // There could be initializing stores which follow allocation.
kvn@3651 1444 // For example, a volatile field store is not collected
kvn@3651 1445 // by Initialize node.
kvn@3651 1446 //
kvn@3651 1447 // Need to check for dependent loads to separate such stores from
kvn@3651 1448 // stores which follow loads. For now, add initial value NULL so
kvn@3651 1449 // that compare pointers optimization works correctly.
kvn@3651 1450 }
kvn@3651 1451 }
kvn@3651 1452 if (value == NULL) {
kvn@3651 1453 // A field's initializing value was not recorded. Add NULL.
kvn@3651 1454 if (add_edge(ptn, null_obj)) {
kvn@3651 1455 // New edge was added
kvn@3651 1456 new_edges++;
kvn@3651 1457 add_field_uses_to_worklist(ptn->as_Field());
kvn@3651 1458 }
kvn@3651 1459 }
kvn@3651 1460 }
kvn@3651 1461 }
kvn@3651 1462 }
kvn@3651 1463 return new_edges;
kvn@3651 1464 }
kvn@3651 1465
kvn@3651 1466 // Adjust scalar_replaceable state after Connection Graph is built.
kvn@3651 1467 void ConnectionGraph::adjust_scalar_replaceable_state(JavaObjectNode* jobj) {
kvn@3651 1468 // Search for non-escaping objects which are not scalar replaceable
kvn@3651 1469 // and mark them to propagate the state to referenced objects.
kvn@3651 1470
kvn@3651 1471 // 1. An object is not scalar replaceable if the field into which it is
kvn@3651 1472 // stored has unknown offset (stored into unknown element of an array).
kvn@3651 1473 //
kvn@3651 1474 for (UseIterator i(jobj); i.has_next(); i.next()) {
kvn@3651 1475 PointsToNode* use = i.get();
kvn@3651 1476 assert(!use->is_Arraycopy(), "sanity");
kvn@3651 1477 if (use->is_Field()) {
kvn@3651 1478 FieldNode* field = use->as_Field();
kvn@3651 1479 assert(field->is_oop() && field->scalar_replaceable() &&
kvn@3651 1480 field->fields_escape_state() == PointsToNode::NoEscape, "sanity");
kvn@3651 1481 if (field->offset() == Type::OffsetBot) {
kvn@3651 1482 jobj->set_scalar_replaceable(false);
kvn@3651 1483 return;
kvn@3651 1484 }
kvn@3651 1485 }
kvn@3651 1486 assert(use->is_Field() || use->is_LocalVar(), "sanity");
kvn@3651 1487 // 2. An object is not scalar replaceable if it is merged with other objects.
kvn@3651 1488 for (EdgeIterator j(use); j.has_next(); j.next()) {
kvn@3651 1489 PointsToNode* ptn = j.get();
kvn@3651 1490 if (ptn->is_JavaObject() && ptn != jobj) {
kvn@3651 1491 // Mark all objects.
kvn@3651 1492 jobj->set_scalar_replaceable(false);
kvn@3651 1493 ptn->set_scalar_replaceable(false);
kvn@3651 1494 }
kvn@3651 1495 }
kvn@3651 1496 if (!jobj->scalar_replaceable()) {
kvn@3651 1497 return;
kvn@3651 1498 }
kvn@3651 1499 }
kvn@3651 1500
kvn@3651 1501 for (EdgeIterator j(jobj); j.has_next(); j.next()) {
kvn@3651 1502 // Non-escaping object node should point only to field nodes.
kvn@3651 1503 FieldNode* field = j.get()->as_Field();
kvn@3651 1504 int offset = field->as_Field()->offset();
kvn@3651 1505
kvn@3651 1506 // 3. An object is not scalar replaceable if it has a field with unknown
kvn@3651 1507 // offset (array's element is accessed in loop).
kvn@3651 1508 if (offset == Type::OffsetBot) {
kvn@3651 1509 jobj->set_scalar_replaceable(false);
kvn@3651 1510 return;
kvn@3651 1511 }
kvn@3651 1512 // 4. Currently an object is not scalar replaceable if a LoadStore node
kvn@3651 1513 // access its field since the field value is unknown after it.
kvn@3651 1514 //
kvn@3651 1515 Node* n = field->ideal_node();
kvn@3651 1516 for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
kvn@3651 1517 if (n->fast_out(i)->is_LoadStore()) {
kvn@3651 1518 jobj->set_scalar_replaceable(false);
kvn@3651 1519 return;
kvn@3651 1520 }
kvn@3651 1521 }
kvn@3651 1522
kvn@3651 1523 // 5. Or the address may point to more then one object. This may produce
kvn@3651 1524 // the false positive result (set not scalar replaceable)
kvn@3651 1525 // since the flow-insensitive escape analysis can't separate
kvn@3651 1526 // the case when stores overwrite the field's value from the case
kvn@3651 1527 // when stores happened on different control branches.
kvn@3651 1528 //
kvn@3651 1529 // Note: it will disable scalar replacement in some cases:
kvn@3651 1530 //
kvn@3651 1531 // Point p[] = new Point[1];
kvn@3651 1532 // p[0] = new Point(); // Will be not scalar replaced
kvn@3651 1533 //
kvn@3651 1534 // but it will save us from incorrect optimizations in next cases:
kvn@3651 1535 //
kvn@3651 1536 // Point p[] = new Point[1];
kvn@3651 1537 // if ( x ) p[0] = new Point(); // Will be not scalar replaced
kvn@3651 1538 //
kvn@3651 1539 if (field->base_count() > 1) {
kvn@3651 1540 for (BaseIterator i(field); i.has_next(); i.next()) {
kvn@3651 1541 PointsToNode* base = i.get();
kvn@3651 1542 // Don't take into account LocalVar nodes which
kvn@3651 1543 // may point to only one object which should be also
kvn@3651 1544 // this field's base by now.
kvn@3651 1545 if (base->is_JavaObject() && base != jobj) {
kvn@3651 1546 // Mark all bases.
kvn@3651 1547 jobj->set_scalar_replaceable(false);
kvn@3651 1548 base->set_scalar_replaceable(false);
kvn@3651 1549 }
kvn@3651 1550 }
kvn@3651 1551 }
kvn@3651 1552 }
kvn@3651 1553 }
kvn@3651 1554
kvn@3651 1555 #ifdef ASSERT
kvn@3651 1556 void ConnectionGraph::verify_connection_graph(
kvn@3651 1557 GrowableArray<PointsToNode*>& ptnodes_worklist,
kvn@3651 1558 GrowableArray<JavaObjectNode*>& non_escaped_worklist,
kvn@3651 1559 GrowableArray<JavaObjectNode*>& java_objects_worklist,
kvn@3651 1560 GrowableArray<Node*>& addp_worklist) {
kvn@3651 1561 // Verify that graph is complete - no new edges could be added.
kvn@3651 1562 int java_objects_length = java_objects_worklist.length();
kvn@3651 1563 int non_escaped_length = non_escaped_worklist.length();
kvn@3651 1564 int new_edges = 0;
kvn@3651 1565 for (int next = 0; next < java_objects_length; ++next) {
kvn@3651 1566 JavaObjectNode* ptn = java_objects_worklist.at(next);
kvn@3651 1567 new_edges += add_java_object_edges(ptn, true);
kvn@3651 1568 }
kvn@3651 1569 assert(new_edges == 0, "graph was not complete");
kvn@3651 1570 // Verify that escape state is final.
kvn@3651 1571 int length = non_escaped_worklist.length();
kvn@3651 1572 find_non_escaped_objects(ptnodes_worklist, non_escaped_worklist);
kvn@3651 1573 assert((non_escaped_length == non_escaped_worklist.length()) &&
kvn@3651 1574 (non_escaped_length == length) &&
kvn@3651 1575 (_worklist.length() == 0), "escape state was not final");
kvn@3651 1576
kvn@3651 1577 // Verify fields information.
kvn@3651 1578 int addp_length = addp_worklist.length();
kvn@3651 1579 for (int next = 0; next < addp_length; ++next ) {
kvn@3651 1580 Node* n = addp_worklist.at(next);
kvn@3651 1581 FieldNode* field = ptnode_adr(n->_idx)->as_Field();
kvn@3651 1582 if (field->is_oop()) {
kvn@3651 1583 // Verify that field has all bases
kvn@3651 1584 Node* base = get_addp_base(n);
kvn@3651 1585 PointsToNode* ptn = ptnode_adr(base->_idx);
kvn@3651 1586 if (ptn->is_JavaObject()) {
kvn@3651 1587 assert(field->has_base(ptn->as_JavaObject()), "sanity");
kvn@3651 1588 } else {
kvn@3651 1589 assert(ptn->is_LocalVar(), "sanity");
kvn@3651 1590 for (EdgeIterator i(ptn); i.has_next(); i.next()) {
kvn@3651 1591 PointsToNode* e = i.get();
kvn@3651 1592 if (e->is_JavaObject()) {
kvn@3651 1593 assert(field->has_base(e->as_JavaObject()), "sanity");
kvn@3651 1594 }
kvn@3651 1595 }
kvn@3651 1596 }
kvn@3651 1597 // Verify that all fields have initializing values.
kvn@3651 1598 if (field->edge_count() == 0) {
kvn@3651 1599 field->dump();
kvn@3651 1600 assert(field->edge_count() > 0, "sanity");
kvn@3651 1601 }
kvn@3651 1602 }
kvn@3651 1603 }
kvn@3651 1604 }
kvn@3651 1605 #endif
kvn@3651 1606
kvn@3651 1607 // Optimize ideal graph.
kvn@3651 1608 void ConnectionGraph::optimize_ideal_graph(GrowableArray<Node*>& ptr_cmp_worklist,
kvn@3651 1609 GrowableArray<Node*>& storestore_worklist) {
kvn@3651 1610 Compile* C = _compile;
kvn@3651 1611 PhaseIterGVN* igvn = _igvn;
kvn@3651 1612 if (EliminateLocks) {
kvn@3651 1613 // Mark locks before changing ideal graph.
kvn@3651 1614 int cnt = C->macro_count();
kvn@3651 1615 for( int i=0; i < cnt; i++ ) {
kvn@3651 1616 Node *n = C->macro_node(i);
kvn@3651 1617 if (n->is_AbstractLock()) { // Lock and Unlock nodes
kvn@3651 1618 AbstractLockNode* alock = n->as_AbstractLock();
kvn@3651 1619 if (!alock->is_non_esc_obj()) {
kvn@3651 1620 if (not_global_escape(alock->obj_node())) {
kvn@3651 1621 assert(!alock->is_eliminated() || alock->is_coarsened(), "sanity");
kvn@3651 1622 // The lock could be marked eliminated by lock coarsening
kvn@3651 1623 // code during first IGVN before EA. Replace coarsened flag
kvn@3651 1624 // to eliminate all associated locks/unlocks.
kvn@3651 1625 alock->set_non_esc_obj();
kvn@3651 1626 }
kvn@3651 1627 }
kvn@3651 1628 }
kvn@3651 1629 }
kvn@3651 1630 }
kvn@3651 1631
kvn@3651 1632 if (OptimizePtrCompare) {
kvn@3651 1633 // Add ConI(#CC_GT) and ConI(#CC_EQ).
kvn@3651 1634 _pcmp_neq = igvn->makecon(TypeInt::CC_GT);
kvn@3651 1635 _pcmp_eq = igvn->makecon(TypeInt::CC_EQ);
kvn@3651 1636 // Optimize objects compare.
kvn@3651 1637 while (ptr_cmp_worklist.length() != 0) {
kvn@3651 1638 Node *n = ptr_cmp_worklist.pop();
kvn@3651 1639 Node *res = optimize_ptr_compare(n);
kvn@3651 1640 if (res != NULL) {
kvn@3651 1641 #ifndef PRODUCT
kvn@3651 1642 if (PrintOptimizePtrCompare) {
kvn@3651 1643 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, (res == _pcmp_eq ? "EQ" : "NotEQ"));
kvn@3651 1644 if (Verbose) {
kvn@3651 1645 n->dump(1);
kvn@3651 1646 }
kvn@3651 1647 }
kvn@3651 1648 #endif
kvn@3651 1649 igvn->replace_node(n, res);
kvn@3651 1650 }
kvn@3651 1651 }
kvn@3651 1652 // cleanup
kvn@3651 1653 if (_pcmp_neq->outcnt() == 0)
kvn@3651 1654 igvn->hash_delete(_pcmp_neq);
kvn@3651 1655 if (_pcmp_eq->outcnt() == 0)
kvn@3651 1656 igvn->hash_delete(_pcmp_eq);
kvn@3651 1657 }
kvn@3651 1658
kvn@3651 1659 // For MemBarStoreStore nodes added in library_call.cpp, check
kvn@3651 1660 // escape status of associated AllocateNode and optimize out
kvn@3651 1661 // MemBarStoreStore node if the allocated object never escapes.
kvn@3651 1662 while (storestore_worklist.length() != 0) {
kvn@3651 1663 Node *n = storestore_worklist.pop();
kvn@3651 1664 MemBarStoreStoreNode *storestore = n ->as_MemBarStoreStore();
kvn@3651 1665 Node *alloc = storestore->in(MemBarNode::Precedent)->in(0);
kvn@3651 1666 assert (alloc->is_Allocate(), "storestore should point to AllocateNode");
kvn@3651 1667 if (not_global_escape(alloc)) {
kvn@3651 1668 MemBarNode* mb = MemBarNode::make(C, Op_MemBarCPUOrder, Compile::AliasIdxBot);
kvn@3651 1669 mb->init_req(TypeFunc::Memory, storestore->in(TypeFunc::Memory));
kvn@3651 1670 mb->init_req(TypeFunc::Control, storestore->in(TypeFunc::Control));
kvn@3651 1671 igvn->register_new_node_with_optimizer(mb);
kvn@3651 1672 igvn->replace_node(storestore, mb);
kvn@3651 1673 }
kvn@3651 1674 }
kvn@3651 1675 }
kvn@3651 1676
kvn@3651 1677 // Optimize objects compare.
kvn@3651 1678 Node* ConnectionGraph::optimize_ptr_compare(Node* n) {
kvn@3651 1679 assert(OptimizePtrCompare, "sanity");
kvn@3651 1680 PointsToNode* ptn1 = ptnode_adr(n->in(1)->_idx);
kvn@3651 1681 PointsToNode* ptn2 = ptnode_adr(n->in(2)->_idx);
kvn@3651 1682 JavaObjectNode* jobj1 = unique_java_object(n->in(1));
kvn@3651 1683 JavaObjectNode* jobj2 = unique_java_object(n->in(2));
kvn@3651 1684 assert(ptn1->is_JavaObject() || ptn1->is_LocalVar(), "sanity");
kvn@3651 1685 assert(ptn2->is_JavaObject() || ptn2->is_LocalVar(), "sanity");
kvn@3651 1686
kvn@3651 1687 // Check simple cases first.
kvn@3651 1688 if (jobj1 != NULL) {
kvn@3651 1689 if (jobj1->escape_state() == PointsToNode::NoEscape) {
kvn@3651 1690 if (jobj1 == jobj2) {
kvn@3651 1691 // Comparing the same not escaping object.
kvn@3651 1692 return _pcmp_eq;
kvn@3651 1693 }
kvn@3651 1694 Node* obj = jobj1->ideal_node();
kvn@3651 1695 // Comparing not escaping allocation.
kvn@3651 1696 if ((obj->is_Allocate() || obj->is_CallStaticJava()) &&
kvn@3651 1697 !ptn2->points_to(jobj1)) {
kvn@3651 1698 return _pcmp_neq; // This includes nullness check.
kvn@3651 1699 }
kvn@3651 1700 }
kvn@3651 1701 }
kvn@3651 1702 if (jobj2 != NULL) {
kvn@3651 1703 if (jobj2->escape_state() == PointsToNode::NoEscape) {
kvn@3651 1704 Node* obj = jobj2->ideal_node();
kvn@3651 1705 // Comparing not escaping allocation.
kvn@3651 1706 if ((obj->is_Allocate() || obj->is_CallStaticJava()) &&
kvn@3651 1707 !ptn1->points_to(jobj2)) {
kvn@3651 1708 return _pcmp_neq; // This includes nullness check.
kvn@3651 1709 }
kvn@3651 1710 }
kvn@3651 1711 }
kvn@3651 1712 if (jobj1 != NULL && jobj1 != phantom_obj &&
kvn@3651 1713 jobj2 != NULL && jobj2 != phantom_obj &&
kvn@3651 1714 jobj1->ideal_node()->is_Con() &&
kvn@3651 1715 jobj2->ideal_node()->is_Con()) {
kvn@3651 1716 // Klass or String constants compare. Need to be careful with
kvn@3651 1717 // compressed pointers - compare types of ConN and ConP instead of nodes.
kvn@3651 1718 const Type* t1 = jobj1->ideal_node()->bottom_type()->make_ptr();
kvn@3651 1719 const Type* t2 = jobj2->ideal_node()->bottom_type()->make_ptr();
kvn@3651 1720 assert(t1 != NULL && t2 != NULL, "sanity");
kvn@3651 1721 if (t1->make_ptr() == t2->make_ptr()) {
kvn@3651 1722 return _pcmp_eq;
kvn@3651 1723 } else {
kvn@3651 1724 return _pcmp_neq;
kvn@3651 1725 }
kvn@3651 1726 }
kvn@3651 1727 if (ptn1->meet(ptn2)) {
kvn@3651 1728 return NULL; // Sets are not disjoint
kvn@3651 1729 }
kvn@3651 1730
kvn@3651 1731 // Sets are disjoint.
kvn@3651 1732 bool set1_has_unknown_ptr = ptn1->points_to(phantom_obj);
kvn@3651 1733 bool set2_has_unknown_ptr = ptn2->points_to(phantom_obj);
kvn@3651 1734 bool set1_has_null_ptr = ptn1->points_to(null_obj);
kvn@3651 1735 bool set2_has_null_ptr = ptn2->points_to(null_obj);
kvn@3651 1736 if (set1_has_unknown_ptr && set2_has_null_ptr ||
kvn@3651 1737 set2_has_unknown_ptr && set1_has_null_ptr) {
kvn@3651 1738 // Check nullness of unknown object.
kvn@3651 1739 return NULL;
kvn@3651 1740 }
kvn@3651 1741
kvn@3651 1742 // Disjointness by itself is not sufficient since
kvn@3651 1743 // alias analysis is not complete for escaped objects.
kvn@3651 1744 // Disjoint sets are definitely unrelated only when
kvn@3651 1745 // at least one set has only not escaping allocations.
kvn@3651 1746 if (!set1_has_unknown_ptr && !set1_has_null_ptr) {
kvn@3651 1747 if (ptn1->non_escaping_allocation()) {
kvn@3651 1748 return _pcmp_neq;
kvn@3651 1749 }
kvn@3651 1750 }
kvn@3651 1751 if (!set2_has_unknown_ptr && !set2_has_null_ptr) {
kvn@3651 1752 if (ptn2->non_escaping_allocation()) {
kvn@3651 1753 return _pcmp_neq;
kvn@3651 1754 }
kvn@3651 1755 }
kvn@3651 1756 return NULL;
kvn@3651 1757 }
kvn@3651 1758
kvn@3651 1759 // Connection Graph constuction functions.
kvn@3651 1760
kvn@3651 1761 void ConnectionGraph::add_local_var(Node *n, PointsToNode::EscapeState es) {
kvn@3651 1762 PointsToNode* ptadr = _nodes.at(n->_idx);
kvn@3651 1763 if (ptadr != NULL) {
kvn@3651 1764 assert(ptadr->is_LocalVar() && ptadr->ideal_node() == n, "sanity");
kvn@3651 1765 return;
kvn@3651 1766 }
kvn@3651 1767 Compile* C = _compile;
kvn@3651 1768 ptadr = new (C->comp_arena()) LocalVarNode(C, n, es);
kvn@3651 1769 _nodes.at_put(n->_idx, ptadr);
kvn@3651 1770 }
kvn@3651 1771
kvn@3651 1772 void ConnectionGraph::add_java_object(Node *n, PointsToNode::EscapeState es) {
kvn@3651 1773 PointsToNode* ptadr = _nodes.at(n->_idx);
kvn@3651 1774 if (ptadr != NULL) {
kvn@3651 1775 assert(ptadr->is_JavaObject() && ptadr->ideal_node() == n, "sanity");
kvn@3651 1776 return;
kvn@3651 1777 }
kvn@3651 1778 Compile* C = _compile;
kvn@3651 1779 ptadr = new (C->comp_arena()) JavaObjectNode(C, n, es);
kvn@3651 1780 _nodes.at_put(n->_idx, ptadr);
kvn@3651 1781 }
kvn@3651 1782
kvn@3651 1783 void ConnectionGraph::add_field(Node *n, PointsToNode::EscapeState es, int offset) {
kvn@3651 1784 PointsToNode* ptadr = _nodes.at(n->_idx);
kvn@3651 1785 if (ptadr != NULL) {
kvn@3651 1786 assert(ptadr->is_Field() && ptadr->ideal_node() == n, "sanity");
kvn@3651 1787 return;
kvn@3651 1788 }
twisti@3969 1789 bool unsafe = false;
twisti@3969 1790 bool is_oop = is_oop_field(n, offset, &unsafe);
twisti@3969 1791 if (unsafe) {
twisti@3969 1792 es = PointsToNode::GlobalEscape;
twisti@3969 1793 }
kvn@3651 1794 Compile* C = _compile;
kvn@3651 1795 FieldNode* field = new (C->comp_arena()) FieldNode(C, n, es, offset, is_oop);
kvn@3651 1796 _nodes.at_put(n->_idx, field);
kvn@3651 1797 }
kvn@3651 1798
kvn@3651 1799 void ConnectionGraph::add_arraycopy(Node *n, PointsToNode::EscapeState es,
kvn@3651 1800 PointsToNode* src, PointsToNode* dst) {
kvn@3651 1801 assert(!src->is_Field() && !dst->is_Field(), "only for JavaObject and LocalVar");
kvn@3651 1802 assert((src != null_obj) && (dst != null_obj), "not for ConP NULL");
kvn@3651 1803 PointsToNode* ptadr = _nodes.at(n->_idx);
kvn@3651 1804 if (ptadr != NULL) {
kvn@3651 1805 assert(ptadr->is_Arraycopy() && ptadr->ideal_node() == n, "sanity");
kvn@3651 1806 return;
kvn@3651 1807 }
kvn@3651 1808 Compile* C = _compile;
kvn@3651 1809 ptadr = new (C->comp_arena()) ArraycopyNode(C, n, es);
kvn@3651 1810 _nodes.at_put(n->_idx, ptadr);
kvn@3651 1811 // Add edge from arraycopy node to source object.
kvn@3651 1812 (void)add_edge(ptadr, src);
kvn@3651 1813 src->set_arraycopy_src();
kvn@3651 1814 // Add edge from destination object to arraycopy node.
kvn@3651 1815 (void)add_edge(dst, ptadr);
kvn@3651 1816 dst->set_arraycopy_dst();
kvn@3651 1817 }
kvn@3651 1818
twisti@3969 1819 bool ConnectionGraph::is_oop_field(Node* n, int offset, bool* unsafe) {
kvn@3651 1820 const Type* adr_type = n->as_AddP()->bottom_type();
kvn@3651 1821 BasicType bt = T_INT;
kvn@3651 1822 if (offset == Type::OffsetBot) {
kvn@3651 1823 // Check only oop fields.
kvn@3651 1824 if (!adr_type->isa_aryptr() ||
kvn@3651 1825 (adr_type->isa_aryptr()->klass() == NULL) ||
kvn@3651 1826 adr_type->isa_aryptr()->klass()->is_obj_array_klass()) {
kvn@3651 1827 // OffsetBot is used to reference array's element. Ignore first AddP.
kvn@3651 1828 if (find_second_addp(n, n->in(AddPNode::Base)) == NULL) {
kvn@3651 1829 bt = T_OBJECT;
kvn@3651 1830 }
kvn@3651 1831 }
kvn@3651 1832 } else if (offset != oopDesc::klass_offset_in_bytes()) {
kvn@3651 1833 if (adr_type->isa_instptr()) {
kvn@3651 1834 ciField* field = _compile->alias_type(adr_type->isa_instptr())->field();
kvn@3651 1835 if (field != NULL) {
kvn@3651 1836 bt = field->layout_type();
kvn@3651 1837 } else {
twisti@3969 1838 // Check for unsafe oop field access
twisti@3969 1839 for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
twisti@3969 1840 int opcode = n->fast_out(i)->Opcode();
twisti@3969 1841 if (opcode == Op_StoreP || opcode == Op_LoadP ||
twisti@3969 1842 opcode == Op_StoreN || opcode == Op_LoadN) {
twisti@3969 1843 bt = T_OBJECT;
twisti@3969 1844 (*unsafe) = true;
twisti@3969 1845 break;
twisti@3969 1846 }
twisti@3969 1847 }
kvn@3651 1848 }
kvn@3651 1849 } else if (adr_type->isa_aryptr()) {
kvn@3651 1850 if (offset == arrayOopDesc::length_offset_in_bytes()) {
kvn@3651 1851 // Ignore array length load.
kvn@3651 1852 } else if (find_second_addp(n, n->in(AddPNode::Base)) != NULL) {
kvn@3651 1853 // Ignore first AddP.
kvn@3651 1854 } else {
kvn@3651 1855 const Type* elemtype = adr_type->isa_aryptr()->elem();
kvn@3651 1856 bt = elemtype->array_element_basic_type();
kvn@3651 1857 }
kvn@3651 1858 } else if (adr_type->isa_rawptr() || adr_type->isa_klassptr()) {
kvn@3651 1859 // Allocation initialization, ThreadLocal field access, unsafe access
kvn@3651 1860 for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
kvn@3651 1861 int opcode = n->fast_out(i)->Opcode();
kvn@3651 1862 if (opcode == Op_StoreP || opcode == Op_LoadP ||
kvn@3651 1863 opcode == Op_StoreN || opcode == Op_LoadN) {
kvn@3651 1864 bt = T_OBJECT;
twisti@3969 1865 break;
kvn@3651 1866 }
kvn@3651 1867 }
kvn@3651 1868 }
kvn@3651 1869 }
kvn@3651 1870 return (bt == T_OBJECT || bt == T_NARROWOOP || bt == T_ARRAY);
kvn@3651 1871 }
kvn@3651 1872
kvn@3651 1873 // Returns unique pointed java object or NULL.
kvn@3651 1874 JavaObjectNode* ConnectionGraph::unique_java_object(Node *n) {
kvn@3651 1875 assert(!_collecting, "should not call when contructed graph");
kvn@3651 1876 // If the node was created after the escape computation we can't answer.
kvn@3651 1877 uint idx = n->_idx;
kvn@3651 1878 if (idx >= nodes_size()) {
kvn@3651 1879 return NULL;
kvn@3651 1880 }
kvn@3651 1881 PointsToNode* ptn = ptnode_adr(idx);
kvn@3651 1882 if (ptn->is_JavaObject()) {
kvn@3651 1883 return ptn->as_JavaObject();
kvn@3651 1884 }
kvn@3651 1885 assert(ptn->is_LocalVar(), "sanity");
kvn@3651 1886 // Check all java objects it points to.
kvn@3651 1887 JavaObjectNode* jobj = NULL;
kvn@3651 1888 for (EdgeIterator i(ptn); i.has_next(); i.next()) {
kvn@3651 1889 PointsToNode* e = i.get();
kvn@3651 1890 if (e->is_JavaObject()) {
kvn@3651 1891 if (jobj == NULL) {
kvn@3651 1892 jobj = e->as_JavaObject();
kvn@3651 1893 } else if (jobj != e) {
kvn@3651 1894 return NULL;
kvn@3651 1895 }
kvn@3651 1896 }
kvn@3651 1897 }
kvn@3651 1898 return jobj;
kvn@3651 1899 }
kvn@3651 1900
kvn@3651 1901 // Return true if this node points only to non-escaping allocations.
kvn@3651 1902 bool PointsToNode::non_escaping_allocation() {
kvn@3651 1903 if (is_JavaObject()) {
kvn@3651 1904 Node* n = ideal_node();
kvn@3651 1905 if (n->is_Allocate() || n->is_CallStaticJava()) {
kvn@3651 1906 return (escape_state() == PointsToNode::NoEscape);
kvn@3651 1907 } else {
kvn@3651 1908 return false;
kvn@3651 1909 }
kvn@3651 1910 }
kvn@3651 1911 assert(is_LocalVar(), "sanity");
kvn@3651 1912 // Check all java objects it points to.
kvn@3651 1913 for (EdgeIterator i(this); i.has_next(); i.next()) {
kvn@3651 1914 PointsToNode* e = i.get();
kvn@3651 1915 if (e->is_JavaObject()) {
kvn@3651 1916 Node* n = e->ideal_node();
kvn@3651 1917 if ((e->escape_state() != PointsToNode::NoEscape) ||
kvn@3651 1918 !(n->is_Allocate() || n->is_CallStaticJava())) {
kvn@3651 1919 return false;
kvn@3651 1920 }
kvn@3651 1921 }
kvn@3651 1922 }
kvn@3651 1923 return true;
kvn@3651 1924 }
kvn@3651 1925
kvn@3651 1926 // Return true if we know the node does not escape globally.
kvn@3651 1927 bool ConnectionGraph::not_global_escape(Node *n) {
kvn@3651 1928 assert(!_collecting, "should not call during graph construction");
kvn@3651 1929 // If the node was created after the escape computation we can't answer.
kvn@3651 1930 uint idx = n->_idx;
kvn@3651 1931 if (idx >= nodes_size()) {
kvn@3651 1932 return false;
kvn@3651 1933 }
kvn@3651 1934 PointsToNode* ptn = ptnode_adr(idx);
kvn@3651 1935 PointsToNode::EscapeState es = ptn->escape_state();
kvn@3651 1936 // If we have already computed a value, return it.
kvn@3651 1937 if (es >= PointsToNode::GlobalEscape)
kvn@3651 1938 return false;
kvn@3651 1939 if (ptn->is_JavaObject()) {
kvn@3651 1940 return true; // (es < PointsToNode::GlobalEscape);
kvn@3651 1941 }
kvn@3651 1942 assert(ptn->is_LocalVar(), "sanity");
kvn@3651 1943 // Check all java objects it points to.
kvn@3651 1944 for (EdgeIterator i(ptn); i.has_next(); i.next()) {
kvn@3651 1945 if (i.get()->escape_state() >= PointsToNode::GlobalEscape)
kvn@3651 1946 return false;
kvn@3651 1947 }
kvn@3651 1948 return true;
kvn@3651 1949 }
kvn@3651 1950
kvn@3651 1951
kvn@3651 1952 // Helper functions
kvn@3651 1953
kvn@3651 1954 // Return true if this node points to specified node or nodes it points to.
kvn@3651 1955 bool PointsToNode::points_to(JavaObjectNode* ptn) const {
kvn@3651 1956 if (is_JavaObject()) {
kvn@3651 1957 return (this == ptn);
kvn@3651 1958 }
kvn@3651 1959 assert(is_LocalVar(), "sanity");
kvn@3651 1960 for (EdgeIterator i(this); i.has_next(); i.next()) {
kvn@3651 1961 if (i.get() == ptn)
kvn@3651 1962 return true;
kvn@3651 1963 }
kvn@3651 1964 return false;
kvn@3651 1965 }
kvn@3651 1966
kvn@3651 1967 // Return true if one node points to an other.
kvn@3651 1968 bool PointsToNode::meet(PointsToNode* ptn) {
kvn@3651 1969 if (this == ptn) {
kvn@3651 1970 return true;
kvn@3651 1971 } else if (ptn->is_JavaObject()) {
kvn@3651 1972 return this->points_to(ptn->as_JavaObject());
kvn@3651 1973 } else if (this->is_JavaObject()) {
kvn@3651 1974 return ptn->points_to(this->as_JavaObject());
kvn@3651 1975 }
kvn@3651 1976 assert(this->is_LocalVar() && ptn->is_LocalVar(), "sanity");
kvn@3651 1977 int ptn_count = ptn->edge_count();
kvn@3651 1978 for (EdgeIterator i(this); i.has_next(); i.next()) {
kvn@3651 1979 PointsToNode* this_e = i.get();
kvn@3651 1980 for (int j = 0; j < ptn_count; j++) {
kvn@3651 1981 if (this_e == ptn->edge(j))
kvn@3651 1982 return true;
kvn@3651 1983 }
kvn@3651 1984 }
kvn@3651 1985 return false;
kvn@3651 1986 }
kvn@3651 1987
kvn@3651 1988 #ifdef ASSERT
kvn@3651 1989 // Return true if bases point to this java object.
kvn@3651 1990 bool FieldNode::has_base(JavaObjectNode* jobj) const {
kvn@3651 1991 for (BaseIterator i(this); i.has_next(); i.next()) {
kvn@3651 1992 if (i.get() == jobj)
kvn@3651 1993 return true;
kvn@3651 1994 }
kvn@3651 1995 return false;
kvn@3651 1996 }
kvn@3651 1997 #endif
kvn@3651 1998
kvn@500 1999 int ConnectionGraph::address_offset(Node* adr, PhaseTransform *phase) {
kvn@500 2000 const Type *adr_type = phase->type(adr);
kvn@500 2001 if (adr->is_AddP() && adr_type->isa_oopptr() == NULL &&
kvn@500 2002 adr->in(AddPNode::Address)->is_Proj() &&
kvn@500 2003 adr->in(AddPNode::Address)->in(0)->is_Allocate()) {
kvn@500 2004 // We are computing a raw address for a store captured by an Initialize
kvn@500 2005 // compute an appropriate address type. AddP cases #3 and #5 (see below).
kvn@500 2006 int offs = (int)phase->find_intptr_t_con(adr->in(AddPNode::Offset), Type::OffsetBot);
kvn@500 2007 assert(offs != Type::OffsetBot ||
kvn@500 2008 adr->in(AddPNode::Address)->in(0)->is_AllocateArray(),
kvn@500 2009 "offset must be a constant or it is initialization of array");
kvn@500 2010 return offs;
kvn@500 2011 }
kvn@500 2012 const TypePtr *t_ptr = adr_type->isa_ptr();
duke@435 2013 assert(t_ptr != NULL, "must be a pointer type");
duke@435 2014 return t_ptr->offset();
duke@435 2015 }
duke@435 2016
kvn@3651 2017 Node* ConnectionGraph::get_addp_base(Node *addp) {
kvn@500 2018 assert(addp->is_AddP(), "must be AddP");
kvn@500 2019 //
kvn@500 2020 // AddP cases for Base and Address inputs:
kvn@500 2021 // case #1. Direct object's field reference:
kvn@500 2022 // Allocate
kvn@500 2023 // |
kvn@500 2024 // Proj #5 ( oop result )
kvn@500 2025 // |
kvn@500 2026 // CheckCastPP (cast to instance type)
kvn@500 2027 // | |
kvn@500 2028 // AddP ( base == address )
kvn@500 2029 //
kvn@500 2030 // case #2. Indirect object's field reference:
kvn@500 2031 // Phi
kvn@500 2032 // |
kvn@500 2033 // CastPP (cast to instance type)
kvn@500 2034 // | |
kvn@500 2035 // AddP ( base == address )
kvn@500 2036 //
kvn@500 2037 // case #3. Raw object's field reference for Initialize node:
kvn@500 2038 // Allocate
kvn@500 2039 // |
kvn@500 2040 // Proj #5 ( oop result )
kvn@500 2041 // top |
kvn@500 2042 // \ |
kvn@500 2043 // AddP ( base == top )
kvn@500 2044 //
kvn@500 2045 // case #4. Array's element reference:
kvn@500 2046 // {CheckCastPP | CastPP}
kvn@500 2047 // | | |
kvn@500 2048 // | AddP ( array's element offset )
kvn@500 2049 // | |
kvn@500 2050 // AddP ( array's offset )
kvn@500 2051 //
kvn@500 2052 // case #5. Raw object's field reference for arraycopy stub call:
kvn@500 2053 // The inline_native_clone() case when the arraycopy stub is called
kvn@500 2054 // after the allocation before Initialize and CheckCastPP nodes.
kvn@500 2055 // Allocate
kvn@500 2056 // |
kvn@500 2057 // Proj #5 ( oop result )
kvn@500 2058 // | |
kvn@500 2059 // AddP ( base == address )
kvn@500 2060 //
kvn@512 2061 // case #6. Constant Pool, ThreadLocal, CastX2P or
kvn@512 2062 // Raw object's field reference:
kvn@512 2063 // {ConP, ThreadLocal, CastX2P, raw Load}
kvn@500 2064 // top |
kvn@500 2065 // \ |
kvn@500 2066 // AddP ( base == top )
kvn@500 2067 //
kvn@512 2068 // case #7. Klass's field reference.
kvn@512 2069 // LoadKlass
kvn@512 2070 // | |
kvn@512 2071 // AddP ( base == address )
kvn@512 2072 //
kvn@599 2073 // case #8. narrow Klass's field reference.
kvn@599 2074 // LoadNKlass
kvn@599 2075 // |
kvn@599 2076 // DecodeN
kvn@599 2077 // | |
kvn@599 2078 // AddP ( base == address )
kvn@599 2079 //
kvn@3651 2080 Node *base = addp->in(AddPNode::Base);
kvn@3651 2081 if (base->uncast()->is_top()) { // The AddP case #3 and #6.
kvn@3651 2082 base = addp->in(AddPNode::Address);
kvn@1392 2083 while (base->is_AddP()) {
kvn@1392 2084 // Case #6 (unsafe access) may have several chained AddP nodes.
kvn@3651 2085 assert(base->in(AddPNode::Base)->uncast()->is_top(), "expected unsafe access address only");
kvn@3651 2086 base = base->in(AddPNode::Address);
kvn@1392 2087 }
kvn@3651 2088 Node* uncast_base = base->uncast();
kvn@3651 2089 int opcode = uncast_base->Opcode();
kvn@3651 2090 assert(opcode == Op_ConP || opcode == Op_ThreadLocal ||
kvn@3651 2091 opcode == Op_CastX2P || uncast_base->is_DecodeN() ||
kvn@3651 2092 (uncast_base->is_Mem() && uncast_base->bottom_type() == TypeRawPtr::NOTNULL) ||
kvn@3651 2093 (uncast_base->is_Proj() && uncast_base->in(0)->is_Allocate()), "sanity");
duke@435 2094 }
kvn@500 2095 return base;
kvn@500 2096 }
kvn@500 2097
kvn@3651 2098 Node* ConnectionGraph::find_second_addp(Node* addp, Node* n) {
kvn@500 2099 assert(addp->is_AddP() && addp->outcnt() > 0, "Don't process dead nodes");
kvn@500 2100 Node* addp2 = addp->raw_out(0);
kvn@500 2101 if (addp->outcnt() == 1 && addp2->is_AddP() &&
kvn@500 2102 addp2->in(AddPNode::Base) == n &&
kvn@500 2103 addp2->in(AddPNode::Address) == addp) {
kvn@500 2104 assert(addp->in(AddPNode::Base) == n, "expecting the same base");
kvn@500 2105 //
kvn@500 2106 // Find array's offset to push it on worklist first and
kvn@500 2107 // as result process an array's element offset first (pushed second)
kvn@500 2108 // to avoid CastPP for the array's offset.
kvn@500 2109 // Otherwise the inserted CastPP (LocalVar) will point to what
kvn@500 2110 // the AddP (Field) points to. Which would be wrong since
kvn@500 2111 // the algorithm expects the CastPP has the same point as
kvn@500 2112 // as AddP's base CheckCastPP (LocalVar).
kvn@500 2113 //
kvn@500 2114 // ArrayAllocation
kvn@500 2115 // |
kvn@500 2116 // CheckCastPP
kvn@500 2117 // |
kvn@500 2118 // memProj (from ArrayAllocation CheckCastPP)
kvn@500 2119 // | ||
kvn@500 2120 // | || Int (element index)
kvn@500 2121 // | || | ConI (log(element size))
kvn@500 2122 // | || | /
kvn@500 2123 // | || LShift
kvn@500 2124 // | || /
kvn@500 2125 // | AddP (array's element offset)
kvn@500 2126 // | |
kvn@500 2127 // | | ConI (array's offset: #12(32-bits) or #24(64-bits))
kvn@500 2128 // | / /
kvn@500 2129 // AddP (array's offset)
kvn@500 2130 // |
kvn@500 2131 // Load/Store (memory operation on array's element)
kvn@500 2132 //
kvn@500 2133 return addp2;
kvn@500 2134 }
kvn@500 2135 return NULL;
duke@435 2136 }
duke@435 2137
duke@435 2138 //
duke@435 2139 // Adjust the type and inputs of an AddP which computes the
duke@435 2140 // address of a field of an instance
duke@435 2141 //
kvn@3651 2142 bool ConnectionGraph::split_AddP(Node *addp, Node *base) {
kvn@3651 2143 PhaseGVN* igvn = _igvn;
kvn@500 2144 const TypeOopPtr *base_t = igvn->type(base)->isa_oopptr();
kvn@658 2145 assert(base_t != NULL && base_t->is_known_instance(), "expecting instance oopptr");
duke@435 2146 const TypeOopPtr *t = igvn->type(addp)->isa_oopptr();
kvn@500 2147 if (t == NULL) {
kvn@500 2148 // We are computing a raw address for a store captured by an Initialize
kvn@728 2149 // compute an appropriate address type (cases #3 and #5).
kvn@500 2150 assert(igvn->type(addp) == TypeRawPtr::NOTNULL, "must be raw pointer");
kvn@500 2151 assert(addp->in(AddPNode::Address)->is_Proj(), "base of raw address must be result projection from allocation");
kvn@741 2152 intptr_t offs = (int)igvn->find_intptr_t_con(addp->in(AddPNode::Offset), Type::OffsetBot);
kvn@500 2153 assert(offs != Type::OffsetBot, "offset must be a constant");
kvn@500 2154 t = base_t->add_offset(offs)->is_oopptr();
kvn@500 2155 }
kvn@658 2156 int inst_id = base_t->instance_id();
kvn@658 2157 assert(!t->is_known_instance() || t->instance_id() == inst_id,
duke@435 2158 "old type must be non-instance or match new type");
kvn@728 2159
kvn@728 2160 // The type 't' could be subclass of 'base_t'.
kvn@728 2161 // As result t->offset() could be large then base_t's size and it will
kvn@728 2162 // cause the failure in add_offset() with narrow oops since TypeOopPtr()
kvn@728 2163 // constructor verifies correctness of the offset.
kvn@728 2164 //
twisti@1040 2165 // It could happened on subclass's branch (from the type profiling
kvn@728 2166 // inlining) which was not eliminated during parsing since the exactness
kvn@728 2167 // of the allocation type was not propagated to the subclass type check.
kvn@728 2168 //
kvn@1423 2169 // Or the type 't' could be not related to 'base_t' at all.
kvn@1423 2170 // It could happened when CHA type is different from MDO type on a dead path
kvn@1423 2171 // (for example, from instanceof check) which is not collapsed during parsing.
kvn@1423 2172 //
kvn@728 2173 // Do nothing for such AddP node and don't process its users since
kvn@728 2174 // this code branch will go away.
kvn@728 2175 //
kvn@728 2176 if (!t->is_known_instance() &&
kvn@1423 2177 !base_t->klass()->is_subtype_of(t->klass())) {
kvn@728 2178 return false; // bail out
kvn@728 2179 }
duke@435 2180 const TypeOopPtr *tinst = base_t->add_offset(t->offset())->is_oopptr();
kvn@1497 2181 // Do NOT remove the next line: ensure a new alias index is allocated
kvn@1497 2182 // for the instance type. Note: C++ will not remove it since the call
kvn@1497 2183 // has side effect.
duke@435 2184 int alias_idx = _compile->get_alias_index(tinst);
duke@435 2185 igvn->set_type(addp, tinst);
duke@435 2186 // record the allocation in the node map
kvn@3651 2187 set_map(addp, get_map(base->_idx));
kvn@688 2188 // Set addp's Base and Address to 'base'.
kvn@688 2189 Node *abase = addp->in(AddPNode::Base);
kvn@688 2190 Node *adr = addp->in(AddPNode::Address);
kvn@688 2191 if (adr->is_Proj() && adr->in(0)->is_Allocate() &&
kvn@688 2192 adr->in(0)->_idx == (uint)inst_id) {
kvn@688 2193 // Skip AddP cases #3 and #5.
kvn@688 2194 } else {
kvn@688 2195 assert(!abase->is_top(), "sanity"); // AddP case #3
kvn@688 2196 if (abase != base) {
kvn@688 2197 igvn->hash_delete(addp);
kvn@688 2198 addp->set_req(AddPNode::Base, base);
kvn@688 2199 if (abase == adr) {
kvn@688 2200 addp->set_req(AddPNode::Address, base);
kvn@688 2201 } else {
kvn@688 2202 // AddP case #4 (adr is array's element offset AddP node)
kvn@688 2203 #ifdef ASSERT
kvn@688 2204 const TypeOopPtr *atype = igvn->type(adr)->isa_oopptr();
kvn@688 2205 assert(adr->is_AddP() && atype != NULL &&
kvn@688 2206 atype->instance_id() == inst_id, "array's element offset should be processed first");
kvn@688 2207 #endif
kvn@688 2208 }
kvn@688 2209 igvn->hash_insert(addp);
duke@435 2210 }
duke@435 2211 }
kvn@500 2212 // Put on IGVN worklist since at least addp's type was changed above.
kvn@500 2213 record_for_optimizer(addp);
kvn@728 2214 return true;
duke@435 2215 }
duke@435 2216
duke@435 2217 //
duke@435 2218 // Create a new version of orig_phi if necessary. Returns either the newly
kvn@2741 2219 // created phi or an existing phi. Sets create_new to indicate whether a new
duke@435 2220 // phi was created. Cache the last newly created phi in the node map.
duke@435 2221 //
kvn@3651 2222 PhiNode *ConnectionGraph::create_split_phi(PhiNode *orig_phi, int alias_idx, GrowableArray<PhiNode *> &orig_phi_worklist, bool &new_created) {
duke@435 2223 Compile *C = _compile;
kvn@3651 2224 PhaseGVN* igvn = _igvn;
duke@435 2225 new_created = false;
duke@435 2226 int phi_alias_idx = C->get_alias_index(orig_phi->adr_type());
duke@435 2227 // nothing to do if orig_phi is bottom memory or matches alias_idx
kvn@500 2228 if (phi_alias_idx == alias_idx) {
duke@435 2229 return orig_phi;
duke@435 2230 }
kvn@1286 2231 // Have we recently created a Phi for this alias index?
duke@435 2232 PhiNode *result = get_map_phi(orig_phi->_idx);
duke@435 2233 if (result != NULL && C->get_alias_index(result->adr_type()) == alias_idx) {
duke@435 2234 return result;
duke@435 2235 }
kvn@1286 2236 // Previous check may fail when the same wide memory Phi was split into Phis
kvn@1286 2237 // for different memory slices. Search all Phis for this region.
kvn@1286 2238 if (result != NULL) {
kvn@1286 2239 Node* region = orig_phi->in(0);
kvn@1286 2240 for (DUIterator_Fast imax, i = region->fast_outs(imax); i < imax; i++) {
kvn@1286 2241 Node* phi = region->fast_out(i);
kvn@1286 2242 if (phi->is_Phi() &&
kvn@1286 2243 C->get_alias_index(phi->as_Phi()->adr_type()) == alias_idx) {
kvn@1286 2244 assert(phi->_idx >= nodes_size(), "only new Phi per instance memory slice");
kvn@1286 2245 return phi->as_Phi();
kvn@1286 2246 }
kvn@1286 2247 }
kvn@1286 2248 }
kvn@473 2249 if ((int)C->unique() + 2*NodeLimitFudgeFactor > MaxNodeLimit) {
kvn@473 2250 if (C->do_escape_analysis() == true && !C->failing()) {
kvn@473 2251 // Retry compilation without escape analysis.
kvn@473 2252 // If this is the first failure, the sentinel string will "stick"
kvn@473 2253 // to the Compile object, and the C2Compiler will see it and retry.
kvn@473 2254 C->record_failure(C2Compiler::retry_no_escape_analysis());
kvn@473 2255 }
kvn@473 2256 return NULL;
kvn@473 2257 }
duke@435 2258 orig_phi_worklist.append_if_missing(orig_phi);
kvn@500 2259 const TypePtr *atype = C->get_adr_type(alias_idx);
duke@435 2260 result = PhiNode::make(orig_phi->in(0), NULL, Type::MEMORY, atype);
kvn@1286 2261 C->copy_node_notes_to(result, orig_phi);
duke@435 2262 igvn->set_type(result, result->bottom_type());
duke@435 2263 record_for_optimizer(result);
kvn@3651 2264 set_map(orig_phi, result);
duke@435 2265 new_created = true;
duke@435 2266 return result;
duke@435 2267 }
duke@435 2268
duke@435 2269 //
kvn@2741 2270 // Return a new version of Memory Phi "orig_phi" with the inputs having the
duke@435 2271 // specified alias index.
duke@435 2272 //
kvn@3651 2273 PhiNode *ConnectionGraph::split_memory_phi(PhiNode *orig_phi, int alias_idx, GrowableArray<PhiNode *> &orig_phi_worklist) {
duke@435 2274 assert(alias_idx != Compile::AliasIdxBot, "can't split out bottom memory");
duke@435 2275 Compile *C = _compile;
kvn@3651 2276 PhaseGVN* igvn = _igvn;
duke@435 2277 bool new_phi_created;
kvn@3651 2278 PhiNode *result = create_split_phi(orig_phi, alias_idx, orig_phi_worklist, new_phi_created);
duke@435 2279 if (!new_phi_created) {
duke@435 2280 return result;
duke@435 2281 }
duke@435 2282 GrowableArray<PhiNode *> phi_list;
duke@435 2283 GrowableArray<uint> cur_input;
duke@435 2284 PhiNode *phi = orig_phi;
duke@435 2285 uint idx = 1;
duke@435 2286 bool finished = false;
duke@435 2287 while(!finished) {
duke@435 2288 while (idx < phi->req()) {
kvn@3651 2289 Node *mem = find_inst_mem(phi->in(idx), alias_idx, orig_phi_worklist);
duke@435 2290 if (mem != NULL && mem->is_Phi()) {
kvn@3651 2291 PhiNode *newphi = create_split_phi(mem->as_Phi(), alias_idx, orig_phi_worklist, new_phi_created);
duke@435 2292 if (new_phi_created) {
duke@435 2293 // found an phi for which we created a new split, push current one on worklist and begin
duke@435 2294 // processing new one
duke@435 2295 phi_list.push(phi);
duke@435 2296 cur_input.push(idx);
duke@435 2297 phi = mem->as_Phi();
kvn@500 2298 result = newphi;
duke@435 2299 idx = 1;
duke@435 2300 continue;
duke@435 2301 } else {
kvn@500 2302 mem = newphi;
duke@435 2303 }
duke@435 2304 }
kvn@473 2305 if (C->failing()) {
kvn@473 2306 return NULL;
kvn@473 2307 }
duke@435 2308 result->set_req(idx++, mem);
duke@435 2309 }
duke@435 2310 #ifdef ASSERT
duke@435 2311 // verify that the new Phi has an input for each input of the original
duke@435 2312 assert( phi->req() == result->req(), "must have same number of inputs.");
duke@435 2313 assert( result->in(0) != NULL && result->in(0) == phi->in(0), "regions must match");
kvn@500 2314 #endif
kvn@500 2315 // Check if all new phi's inputs have specified alias index.
kvn@500 2316 // Otherwise use old phi.
duke@435 2317 for (uint i = 1; i < phi->req(); i++) {
kvn@500 2318 Node* in = result->in(i);
kvn@500 2319 assert((phi->in(i) == NULL) == (in == NULL), "inputs must correspond.");
duke@435 2320 }
duke@435 2321 // we have finished processing a Phi, see if there are any more to do
duke@435 2322 finished = (phi_list.length() == 0 );
duke@435 2323 if (!finished) {
duke@435 2324 phi = phi_list.pop();
duke@435 2325 idx = cur_input.pop();
kvn@500 2326 PhiNode *prev_result = get_map_phi(phi->_idx);
kvn@500 2327 prev_result->set_req(idx++, result);
kvn@500 2328 result = prev_result;
duke@435 2329 }
duke@435 2330 }
duke@435 2331 return result;
duke@435 2332 }
duke@435 2333
kvn@500 2334 //
kvn@500 2335 // The next methods are derived from methods in MemNode.
kvn@500 2336 //
kvn@3651 2337 Node* ConnectionGraph::step_through_mergemem(MergeMemNode *mmem, int alias_idx, const TypeOopPtr *toop) {
kvn@500 2338 Node *mem = mmem;
never@2170 2339 // TypeOopPtr::NOTNULL+any is an OOP with unknown offset - generally
kvn@500 2340 // means an array I have not precisely typed yet. Do not do any
kvn@500 2341 // alias stuff with it any time soon.
kvn@3651 2342 if (toop->base() != Type::AnyPtr &&
never@2170 2343 !(toop->klass() != NULL &&
never@2170 2344 toop->klass()->is_java_lang_Object() &&
kvn@3651 2345 toop->offset() == Type::OffsetBot)) {
kvn@500 2346 mem = mmem->memory_at(alias_idx);
kvn@500 2347 // Update input if it is progress over what we have now
kvn@500 2348 }
kvn@500 2349 return mem;
kvn@500 2350 }
kvn@500 2351
kvn@500 2352 //
kvn@1536 2353 // Move memory users to their memory slices.
kvn@1536 2354 //
kvn@3651 2355 void ConnectionGraph::move_inst_mem(Node* n, GrowableArray<PhiNode *> &orig_phis) {
kvn@1536 2356 Compile* C = _compile;
kvn@3651 2357 PhaseGVN* igvn = _igvn;
kvn@1536 2358 const TypePtr* tp = igvn->type(n->in(MemNode::Address))->isa_ptr();
kvn@1536 2359 assert(tp != NULL, "ptr type");
kvn@1536 2360 int alias_idx = C->get_alias_index(tp);
kvn@1536 2361 int general_idx = C->get_general_index(alias_idx);
kvn@1536 2362
kvn@1536 2363 // Move users first
kvn@1536 2364 for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
kvn@1536 2365 Node* use = n->fast_out(i);
kvn@1536 2366 if (use->is_MergeMem()) {
kvn@1536 2367 MergeMemNode* mmem = use->as_MergeMem();
kvn@1536 2368 assert(n == mmem->memory_at(alias_idx), "should be on instance memory slice");
kvn@1536 2369 if (n != mmem->memory_at(general_idx) || alias_idx == general_idx) {
kvn@1536 2370 continue; // Nothing to do
kvn@1536 2371 }
kvn@1536 2372 // Replace previous general reference to mem node.
kvn@1536 2373 uint orig_uniq = C->unique();
kvn@3651 2374 Node* m = find_inst_mem(n, general_idx, orig_phis);
kvn@1536 2375 assert(orig_uniq == C->unique(), "no new nodes");
kvn@1536 2376 mmem->set_memory_at(general_idx, m);
kvn@1536 2377 --imax;
kvn@1536 2378 --i;
kvn@1536 2379 } else if (use->is_MemBar()) {
kvn@1536 2380 assert(!use->is_Initialize(), "initializing stores should not be moved");
kvn@1536 2381 if (use->req() > MemBarNode::Precedent &&
kvn@1536 2382 use->in(MemBarNode::Precedent) == n) {
kvn@1536 2383 // Don't move related membars.
kvn@1536 2384 record_for_optimizer(use);
kvn@1536 2385 continue;
kvn@1536 2386 }
kvn@1536 2387 tp = use->as_MemBar()->adr_type()->isa_ptr();
kvn@1536 2388 if (tp != NULL && C->get_alias_index(tp) == alias_idx ||
kvn@1536 2389 alias_idx == general_idx) {
kvn@1536 2390 continue; // Nothing to do
kvn@1536 2391 }
kvn@1536 2392 // Move to general memory slice.
kvn@1536 2393 uint orig_uniq = C->unique();
kvn@3651 2394 Node* m = find_inst_mem(n, general_idx, orig_phis);
kvn@1536 2395 assert(orig_uniq == C->unique(), "no new nodes");
kvn@1536 2396 igvn->hash_delete(use);
kvn@1536 2397 imax -= use->replace_edge(n, m);
kvn@1536 2398 igvn->hash_insert(use);
kvn@1536 2399 record_for_optimizer(use);
kvn@1536 2400 --i;
kvn@1536 2401 #ifdef ASSERT
kvn@1536 2402 } else if (use->is_Mem()) {
kvn@1536 2403 if (use->Opcode() == Op_StoreCM && use->in(MemNode::OopStore) == n) {
kvn@1536 2404 // Don't move related cardmark.
kvn@1536 2405 continue;
kvn@1536 2406 }
kvn@1536 2407 // Memory nodes should have new memory input.
kvn@1536 2408 tp = igvn->type(use->in(MemNode::Address))->isa_ptr();
kvn@1536 2409 assert(tp != NULL, "ptr type");
kvn@1536 2410 int idx = C->get_alias_index(tp);
kvn@1536 2411 assert(get_map(use->_idx) != NULL || idx == alias_idx,
kvn@1536 2412 "Following memory nodes should have new memory input or be on the same memory slice");
kvn@1536 2413 } else if (use->is_Phi()) {
kvn@1536 2414 // Phi nodes should be split and moved already.
kvn@1536 2415 tp = use->as_Phi()->adr_type()->isa_ptr();
kvn@1536 2416 assert(tp != NULL, "ptr type");
kvn@1536 2417 int idx = C->get_alias_index(tp);
kvn@1536 2418 assert(idx == alias_idx, "Following Phi nodes should be on the same memory slice");
kvn@1536 2419 } else {
kvn@1536 2420 use->dump();
kvn@1536 2421 assert(false, "should not be here");
kvn@1536 2422 #endif
kvn@1536 2423 }
kvn@1536 2424 }
kvn@1536 2425 }
kvn@1536 2426
kvn@1536 2427 //
kvn@500 2428 // Search memory chain of "mem" to find a MemNode whose address
kvn@500 2429 // is the specified alias index.
kvn@500 2430 //
kvn@3651 2431 Node* ConnectionGraph::find_inst_mem(Node *orig_mem, int alias_idx, GrowableArray<PhiNode *> &orig_phis) {
kvn@500 2432 if (orig_mem == NULL)
kvn@500 2433 return orig_mem;
kvn@3651 2434 Compile* C = _compile;
kvn@3651 2435 PhaseGVN* igvn = _igvn;
never@2170 2436 const TypeOopPtr *toop = C->get_adr_type(alias_idx)->isa_oopptr();
never@2170 2437 bool is_instance = (toop != NULL) && toop->is_known_instance();
kvn@688 2438 Node *start_mem = C->start()->proj_out(TypeFunc::Memory);
kvn@500 2439 Node *prev = NULL;
kvn@500 2440 Node *result = orig_mem;
kvn@500 2441 while (prev != result) {
kvn@500 2442 prev = result;
kvn@688 2443 if (result == start_mem)
twisti@1040 2444 break; // hit one of our sentinels
kvn@500 2445 if (result->is_Mem()) {
kvn@3651 2446 const Type *at = igvn->type(result->in(MemNode::Address));
kvn@2741 2447 if (at == Type::TOP)
kvn@2741 2448 break; // Dead
kvn@2741 2449 assert (at->isa_ptr() != NULL, "pointer type required.");
kvn@2741 2450 int idx = C->get_alias_index(at->is_ptr());
kvn@2741 2451 if (idx == alias_idx)
kvn@2741 2452 break; // Found
kvn@2741 2453 if (!is_instance && (at->isa_oopptr() == NULL ||
kvn@2741 2454 !at->is_oopptr()->is_known_instance())) {
kvn@2741 2455 break; // Do not skip store to general memory slice.
kvn@500 2456 }
kvn@688 2457 result = result->in(MemNode::Memory);
kvn@500 2458 }
kvn@500 2459 if (!is_instance)
kvn@500 2460 continue; // don't search further for non-instance types
kvn@500 2461 // skip over a call which does not affect this memory slice
kvn@500 2462 if (result->is_Proj() && result->as_Proj()->_con == TypeFunc::Memory) {
kvn@500 2463 Node *proj_in = result->in(0);
never@2170 2464 if (proj_in->is_Allocate() && proj_in->_idx == (uint)toop->instance_id()) {
twisti@1040 2465 break; // hit one of our sentinels
kvn@688 2466 } else if (proj_in->is_Call()) {
kvn@500 2467 CallNode *call = proj_in->as_Call();
kvn@3651 2468 if (!call->may_modify(toop, igvn)) {
kvn@500 2469 result = call->in(TypeFunc::Memory);
kvn@500 2470 }
kvn@500 2471 } else if (proj_in->is_Initialize()) {
kvn@500 2472 AllocateNode* alloc = proj_in->as_Initialize()->allocation();
kvn@500 2473 // Stop if this is the initialization for the object instance which
kvn@500 2474 // which contains this memory slice, otherwise skip over it.
never@2170 2475 if (alloc == NULL || alloc->_idx != (uint)toop->instance_id()) {
kvn@500 2476 result = proj_in->in(TypeFunc::Memory);
kvn@500 2477 }
kvn@500 2478 } else if (proj_in->is_MemBar()) {
kvn@500 2479 result = proj_in->in(TypeFunc::Memory);
kvn@500 2480 }
kvn@500 2481 } else if (result->is_MergeMem()) {
kvn@500 2482 MergeMemNode *mmem = result->as_MergeMem();
never@2170 2483 result = step_through_mergemem(mmem, alias_idx, toop);
kvn@500 2484 if (result == mmem->base_memory()) {
kvn@500 2485 // Didn't find instance memory, search through general slice recursively.
kvn@500 2486 result = mmem->memory_at(C->get_general_index(alias_idx));
kvn@3651 2487 result = find_inst_mem(result, alias_idx, orig_phis);
kvn@500 2488 if (C->failing()) {
kvn@500 2489 return NULL;
kvn@500 2490 }
kvn@500 2491 mmem->set_memory_at(alias_idx, result);
kvn@500 2492 }
kvn@500 2493 } else if (result->is_Phi() &&
kvn@500 2494 C->get_alias_index(result->as_Phi()->adr_type()) != alias_idx) {
kvn@3651 2495 Node *un = result->as_Phi()->unique_input(igvn);
kvn@500 2496 if (un != NULL) {
kvn@1536 2497 orig_phis.append_if_missing(result->as_Phi());
kvn@500 2498 result = un;
kvn@500 2499 } else {
kvn@500 2500 break;
kvn@500 2501 }
kvn@1535 2502 } else if (result->is_ClearArray()) {
kvn@3651 2503 if (!ClearArrayNode::step_through(&result, (uint)toop->instance_id(), igvn)) {
kvn@1535 2504 // Can not bypass initialization of the instance
kvn@1535 2505 // we are looking for.
kvn@1535 2506 break;
kvn@1535 2507 }
kvn@1535 2508 // Otherwise skip it (the call updated 'result' value).
kvn@1019 2509 } else if (result->Opcode() == Op_SCMemProj) {
kvn@1019 2510 assert(result->in(0)->is_LoadStore(), "sanity");
kvn@3651 2511 const Type *at = igvn->type(result->in(0)->in(MemNode::Address));
kvn@1019 2512 if (at != Type::TOP) {
kvn@1019 2513 assert (at->isa_ptr() != NULL, "pointer type required.");
kvn@1019 2514 int idx = C->get_alias_index(at->is_ptr());
kvn@1019 2515 assert(idx != alias_idx, "Object is not scalar replaceable if a LoadStore node access its field");
kvn@1019 2516 break;
kvn@1019 2517 }
kvn@1019 2518 result = result->in(0)->in(MemNode::Memory);
kvn@500 2519 }
kvn@500 2520 }
kvn@682 2521 if (result->is_Phi()) {
kvn@500 2522 PhiNode *mphi = result->as_Phi();
kvn@500 2523 assert(mphi->bottom_type() == Type::MEMORY, "memory phi required");
kvn@500 2524 const TypePtr *t = mphi->adr_type();
kvn@2741 2525 if (!is_instance) {
kvn@682 2526 // Push all non-instance Phis on the orig_phis worklist to update inputs
kvn@682 2527 // during Phase 4 if needed.
kvn@682 2528 orig_phis.append_if_missing(mphi);
kvn@2741 2529 } else if (C->get_alias_index(t) != alias_idx) {
kvn@2741 2530 // Create a new Phi with the specified alias index type.
kvn@3651 2531 result = split_memory_phi(mphi, alias_idx, orig_phis);
kvn@500 2532 }
kvn@500 2533 }
kvn@500 2534 // the result is either MemNode, PhiNode, InitializeNode.
kvn@500 2535 return result;
kvn@500 2536 }
kvn@500 2537
duke@435 2538 //
duke@435 2539 // Convert the types of unescaped object to instance types where possible,
duke@435 2540 // propagate the new type information through the graph, and update memory
duke@435 2541 // edges and MergeMem inputs to reflect the new type.
duke@435 2542 //
duke@435 2543 // We start with allocations (and calls which may be allocations) on alloc_worklist.
duke@435 2544 // The processing is done in 4 phases:
duke@435 2545 //
duke@435 2546 // Phase 1: Process possible allocations from alloc_worklist. Create instance
duke@435 2547 // types for the CheckCastPP for allocations where possible.
duke@435 2548 // Propagate the the new types through users as follows:
duke@435 2549 // casts and Phi: push users on alloc_worklist
duke@435 2550 // AddP: cast Base and Address inputs to the instance type
duke@435 2551 // push any AddP users on alloc_worklist and push any memnode
duke@435 2552 // users onto memnode_worklist.
duke@435 2553 // Phase 2: Process MemNode's from memnode_worklist. compute new address type and
duke@435 2554 // search the Memory chain for a store with the appropriate type
duke@435 2555 // address type. If a Phi is found, create a new version with
twisti@1040 2556 // the appropriate memory slices from each of the Phi inputs.
duke@435 2557 // For stores, process the users as follows:
duke@435 2558 // MemNode: push on memnode_worklist
duke@435 2559 // MergeMem: push on mergemem_worklist
duke@435 2560 // Phase 3: Process MergeMem nodes from mergemem_worklist. Walk each memory slice
duke@435 2561 // moving the first node encountered of each instance type to the
duke@435 2562 // the input corresponding to its alias index.
duke@435 2563 // appropriate memory slice.
duke@435 2564 // Phase 4: Update the inputs of non-instance memory Phis and the Memory input of memnodes.
duke@435 2565 //
duke@435 2566 // In the following example, the CheckCastPP nodes are the cast of allocation
duke@435 2567 // results and the allocation of node 29 is unescaped and eligible to be an
duke@435 2568 // instance type.
duke@435 2569 //
duke@435 2570 // We start with:
duke@435 2571 //
duke@435 2572 // 7 Parm #memory
duke@435 2573 // 10 ConI "12"
duke@435 2574 // 19 CheckCastPP "Foo"
duke@435 2575 // 20 AddP _ 19 19 10 Foo+12 alias_index=4
duke@435 2576 // 29 CheckCastPP "Foo"
duke@435 2577 // 30 AddP _ 29 29 10 Foo+12 alias_index=4
duke@435 2578 //
duke@435 2579 // 40 StoreP 25 7 20 ... alias_index=4
duke@435 2580 // 50 StoreP 35 40 30 ... alias_index=4
duke@435 2581 // 60 StoreP 45 50 20 ... alias_index=4
duke@435 2582 // 70 LoadP _ 60 30 ... alias_index=4
duke@435 2583 // 80 Phi 75 50 60 Memory alias_index=4
duke@435 2584 // 90 LoadP _ 80 30 ... alias_index=4
duke@435 2585 // 100 LoadP _ 80 20 ... alias_index=4
duke@435 2586 //
duke@435 2587 //
duke@435 2588 // Phase 1 creates an instance type for node 29 assigning it an instance id of 24
duke@435 2589 // and creating a new alias index for node 30. This gives:
duke@435 2590 //
duke@435 2591 // 7 Parm #memory
duke@435 2592 // 10 ConI "12"
duke@435 2593 // 19 CheckCastPP "Foo"
duke@435 2594 // 20 AddP _ 19 19 10 Foo+12 alias_index=4
duke@435 2595 // 29 CheckCastPP "Foo" iid=24
duke@435 2596 // 30 AddP _ 29 29 10 Foo+12 alias_index=6 iid=24
duke@435 2597 //
duke@435 2598 // 40 StoreP 25 7 20 ... alias_index=4
duke@435 2599 // 50 StoreP 35 40 30 ... alias_index=6
duke@435 2600 // 60 StoreP 45 50 20 ... alias_index=4
duke@435 2601 // 70 LoadP _ 60 30 ... alias_index=6
duke@435 2602 // 80 Phi 75 50 60 Memory alias_index=4
duke@435 2603 // 90 LoadP _ 80 30 ... alias_index=6
duke@435 2604 // 100 LoadP _ 80 20 ... alias_index=4
duke@435 2605 //
duke@435 2606 // In phase 2, new memory inputs are computed for the loads and stores,
duke@435 2607 // And a new version of the phi is created. In phase 4, the inputs to
duke@435 2608 // node 80 are updated and then the memory nodes are updated with the
duke@435 2609 // values computed in phase 2. This results in:
duke@435 2610 //
duke@435 2611 // 7 Parm #memory
duke@435 2612 // 10 ConI "12"
duke@435 2613 // 19 CheckCastPP "Foo"
duke@435 2614 // 20 AddP _ 19 19 10 Foo+12 alias_index=4
duke@435 2615 // 29 CheckCastPP "Foo" iid=24
duke@435 2616 // 30 AddP _ 29 29 10 Foo+12 alias_index=6 iid=24
duke@435 2617 //
duke@435 2618 // 40 StoreP 25 7 20 ... alias_index=4
duke@435 2619 // 50 StoreP 35 7 30 ... alias_index=6
duke@435 2620 // 60 StoreP 45 40 20 ... alias_index=4
duke@435 2621 // 70 LoadP _ 50 30 ... alias_index=6
duke@435 2622 // 80 Phi 75 40 60 Memory alias_index=4
duke@435 2623 // 120 Phi 75 50 50 Memory alias_index=6
duke@435 2624 // 90 LoadP _ 120 30 ... alias_index=6
duke@435 2625 // 100 LoadP _ 80 20 ... alias_index=4
duke@435 2626 //
duke@435 2627 void ConnectionGraph::split_unique_types(GrowableArray<Node *> &alloc_worklist) {
duke@435 2628 GrowableArray<Node *> memnode_worklist;
duke@435 2629 GrowableArray<PhiNode *> orig_phis;
kvn@2276 2630 PhaseIterGVN *igvn = _igvn;
duke@435 2631 uint new_index_start = (uint) _compile->num_alias_types();
kvn@1536 2632 Arena* arena = Thread::current()->resource_area();
kvn@1536 2633 VectorSet visited(arena);
kvn@3651 2634 ideal_nodes.clear(); // Reset for use with set_map/get_map.
kvn@3651 2635 uint unique_old = _compile->unique();
kvn@500 2636
kvn@500 2637 // Phase 1: Process possible allocations from alloc_worklist.
kvn@500 2638 // Create instance types for the CheckCastPP for allocations where possible.
kvn@679 2639 //
kvn@679 2640 // (Note: don't forget to change the order of the second AddP node on
kvn@679 2641 // the alloc_worklist if the order of the worklist processing is changed,
kvn@679 2642 // see the comment in find_second_addp().)
kvn@679 2643 //
duke@435 2644 while (alloc_worklist.length() != 0) {
duke@435 2645 Node *n = alloc_worklist.pop();
duke@435 2646 uint ni = n->_idx;
duke@435 2647 if (n->is_Call()) {
duke@435 2648 CallNode *alloc = n->as_Call();
duke@435 2649 // copy escape information to call node
kvn@679 2650 PointsToNode* ptn = ptnode_adr(alloc->_idx);
kvn@3651 2651 PointsToNode::EscapeState es = ptn->escape_state();
kvn@500 2652 // We have an allocation or call which returns a Java object,
kvn@500 2653 // see if it is unescaped.
kvn@3254 2654 if (es != PointsToNode::NoEscape || !ptn->scalar_replaceable())
duke@435 2655 continue;
kvn@1219 2656 // Find CheckCastPP for the allocate or for the return value of a call
kvn@1219 2657 n = alloc->result_cast();
kvn@1219 2658 if (n == NULL) { // No uses except Initialize node
kvn@1219 2659 if (alloc->is_Allocate()) {
kvn@1219 2660 // Set the scalar_replaceable flag for allocation
kvn@1219 2661 // so it could be eliminated if it has no uses.
kvn@1219 2662 alloc->as_Allocate()->_is_scalar_replaceable = true;
kvn@1219 2663 }
kvn@1219 2664 continue;
kvn@474 2665 }
kvn@1219 2666 if (!n->is_CheckCastPP()) { // not unique CheckCastPP.
kvn@1219 2667 assert(!alloc->is_Allocate(), "allocation should have unique type");
kvn@500 2668 continue;
kvn@1219 2669 }
kvn@1219 2670
kvn@500 2671 // The inline code for Object.clone() casts the allocation result to
kvn@682 2672 // java.lang.Object and then to the actual type of the allocated
kvn@500 2673 // object. Detect this case and use the second cast.
kvn@682 2674 // Also detect j.l.reflect.Array.newInstance(jobject, jint) case when
kvn@682 2675 // the allocation result is cast to java.lang.Object and then
kvn@682 2676 // to the actual Array type.
kvn@500 2677 if (alloc->is_Allocate() && n->as_Type()->type() == TypeInstPtr::NOTNULL
kvn@682 2678 && (alloc->is_AllocateArray() ||
kvn@682 2679 igvn->type(alloc->in(AllocateNode::KlassNode)) != TypeKlassPtr::OBJECT)) {
kvn@500 2680 Node *cast2 = NULL;
kvn@500 2681 for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
kvn@500 2682 Node *use = n->fast_out(i);
kvn@500 2683 if (use->is_CheckCastPP()) {
kvn@500 2684 cast2 = use;
kvn@500 2685 break;
kvn@500 2686 }
kvn@500 2687 }
kvn@500 2688 if (cast2 != NULL) {
kvn@500 2689 n = cast2;
kvn@500 2690 } else {
kvn@1219 2691 // Non-scalar replaceable if the allocation type is unknown statically
kvn@1219 2692 // (reflection allocation), the object can't be restored during
kvn@1219 2693 // deoptimization without precise type.
kvn@500 2694 continue;
kvn@500 2695 }
kvn@500 2696 }
kvn@1219 2697 if (alloc->is_Allocate()) {
kvn@1219 2698 // Set the scalar_replaceable flag for allocation
kvn@1219 2699 // so it could be eliminated.
kvn@1219 2700 alloc->as_Allocate()->_is_scalar_replaceable = true;
kvn@1219 2701 }
kvn@3651 2702 set_escape_state(ptnode_adr(n->_idx), es); // CheckCastPP escape state
kvn@682 2703 // in order for an object to be scalar-replaceable, it must be:
kvn@500 2704 // - a direct allocation (not a call returning an object)
kvn@500 2705 // - non-escaping
kvn@500 2706 // - eligible to be a unique type
kvn@500 2707 // - not determined to be ineligible by escape analysis
kvn@3651 2708 set_map(alloc, n);
kvn@3651 2709 set_map(n, alloc);
kvn@500 2710 const TypeOopPtr *t = igvn->type(n)->isa_oopptr();
kvn@500 2711 if (t == NULL)
kvn@3254 2712 continue; // not a TypeOopPtr
kvn@3651 2713 const TypeOopPtr* tinst = t->cast_to_exactness(true)->is_oopptr()->cast_to_instance_id(ni);
duke@435 2714 igvn->hash_delete(n);
duke@435 2715 igvn->set_type(n, tinst);
duke@435 2716 n->raise_bottom_type(tinst);
duke@435 2717 igvn->hash_insert(n);
kvn@500 2718 record_for_optimizer(n);
kvn@3254 2719 if (alloc->is_Allocate() && (t->isa_instptr() || t->isa_aryptr())) {
kvn@598 2720
kvn@598 2721 // First, put on the worklist all Field edges from Connection Graph
kvn@598 2722 // which is more accurate then putting immediate users from Ideal Graph.
kvn@3651 2723 for (EdgeIterator e(ptn); e.has_next(); e.next()) {
kvn@3651 2724 PointsToNode* tgt = e.get();
kvn@3651 2725 Node* use = tgt->ideal_node();
kvn@3651 2726 assert(tgt->is_Field() && use->is_AddP(),
kvn@598 2727 "only AddP nodes are Field edges in CG");
kvn@598 2728 if (use->outcnt() > 0) { // Don't process dead nodes
kvn@598 2729 Node* addp2 = find_second_addp(use, use->in(AddPNode::Base));
kvn@598 2730 if (addp2 != NULL) {
kvn@598 2731 assert(alloc->is_AllocateArray(),"array allocation was expected");
kvn@598 2732 alloc_worklist.append_if_missing(addp2);
kvn@598 2733 }
kvn@598 2734 alloc_worklist.append_if_missing(use);
kvn@598 2735 }
kvn@598 2736 }
kvn@598 2737
kvn@500 2738 // An allocation may have an Initialize which has raw stores. Scan
kvn@500 2739 // the users of the raw allocation result and push AddP users
kvn@500 2740 // on alloc_worklist.
kvn@500 2741 Node *raw_result = alloc->proj_out(TypeFunc::Parms);
kvn@500 2742 assert (raw_result != NULL, "must have an allocation result");
kvn@500 2743 for (DUIterator_Fast imax, i = raw_result->fast_outs(imax); i < imax; i++) {
kvn@500 2744 Node *use = raw_result->fast_out(i);
kvn@500 2745 if (use->is_AddP() && use->outcnt() > 0) { // Don't process dead nodes
kvn@500 2746 Node* addp2 = find_second_addp(use, raw_result);
kvn@500 2747 if (addp2 != NULL) {
kvn@500 2748 assert(alloc->is_AllocateArray(),"array allocation was expected");
kvn@500 2749 alloc_worklist.append_if_missing(addp2);
kvn@500 2750 }
kvn@500 2751 alloc_worklist.append_if_missing(use);
kvn@1535 2752 } else if (use->is_MemBar()) {
kvn@500 2753 memnode_worklist.append_if_missing(use);
kvn@500 2754 }
kvn@500 2755 }
kvn@500 2756 }
duke@435 2757 } else if (n->is_AddP()) {
kvn@3651 2758 JavaObjectNode* jobj = unique_java_object(get_addp_base(n));
kvn@3651 2759 if (jobj == NULL || jobj == phantom_obj) {
kvn@3651 2760 #ifdef ASSERT
kvn@3651 2761 ptnode_adr(get_addp_base(n)->_idx)->dump();
kvn@3651 2762 ptnode_adr(n->_idx)->dump();
kvn@3651 2763 assert(jobj != NULL && jobj != phantom_obj, "escaped allocation");
kvn@3651 2764 #endif
kvn@3651 2765 _compile->record_failure(C2Compiler::retry_no_escape_analysis());
kvn@3651 2766 return;
kvn@1535 2767 }
kvn@3651 2768 Node *base = get_map(jobj->idx()); // CheckCastPP node
kvn@3651 2769 if (!split_AddP(n, base)) continue; // wrong type from dead path
kvn@500 2770 } else if (n->is_Phi() ||
kvn@500 2771 n->is_CheckCastPP() ||
kvn@603 2772 n->is_EncodeP() ||
kvn@603 2773 n->is_DecodeN() ||
kvn@500 2774 (n->is_ConstraintCast() && n->Opcode() == Op_CastPP)) {
duke@435 2775 if (visited.test_set(n->_idx)) {
duke@435 2776 assert(n->is_Phi(), "loops only through Phi's");
duke@435 2777 continue; // already processed
duke@435 2778 }
kvn@3651 2779 JavaObjectNode* jobj = unique_java_object(n);
kvn@3651 2780 if (jobj == NULL || jobj == phantom_obj) {
kvn@3651 2781 #ifdef ASSERT
kvn@3651 2782 ptnode_adr(n->_idx)->dump();
kvn@3651 2783 assert(jobj != NULL && jobj != phantom_obj, "escaped allocation");
kvn@3651 2784 #endif
kvn@3651 2785 _compile->record_failure(C2Compiler::retry_no_escape_analysis());
kvn@3651 2786 return;
kvn@3651 2787 } else {
kvn@3651 2788 Node *val = get_map(jobj->idx()); // CheckCastPP node
duke@435 2789 TypeNode *tn = n->as_Type();
kvn@3651 2790 const TypeOopPtr* tinst = igvn->type(val)->isa_oopptr();
kvn@658 2791 assert(tinst != NULL && tinst->is_known_instance() &&
kvn@3651 2792 tinst->instance_id() == jobj->idx() , "instance type expected.");
kvn@598 2793
kvn@598 2794 const Type *tn_type = igvn->type(tn);
kvn@658 2795 const TypeOopPtr *tn_t;
kvn@658 2796 if (tn_type->isa_narrowoop()) {
kvn@658 2797 tn_t = tn_type->make_ptr()->isa_oopptr();
kvn@658 2798 } else {
kvn@658 2799 tn_t = tn_type->isa_oopptr();
kvn@658 2800 }
kvn@1535 2801 if (tn_t != NULL && tinst->klass()->is_subtype_of(tn_t->klass())) {
kvn@598 2802 if (tn_type->isa_narrowoop()) {
kvn@598 2803 tn_type = tinst->make_narrowoop();
kvn@598 2804 } else {
kvn@598 2805 tn_type = tinst;
kvn@598 2806 }
duke@435 2807 igvn->hash_delete(tn);
kvn@598 2808 igvn->set_type(tn, tn_type);
kvn@598 2809 tn->set_type(tn_type);
duke@435 2810 igvn->hash_insert(tn);
kvn@500 2811 record_for_optimizer(n);
kvn@728 2812 } else {
kvn@1535 2813 assert(tn_type == TypePtr::NULL_PTR ||
kvn@1535 2814 tn_t != NULL && !tinst->klass()->is_subtype_of(tn_t->klass()),
kvn@1535 2815 "unexpected type");
kvn@1535 2816 continue; // Skip dead path with different type
duke@435 2817 }
duke@435 2818 }
duke@435 2819 } else {
kvn@1535 2820 debug_only(n->dump();)
kvn@1535 2821 assert(false, "EA: unexpected node");
duke@435 2822 continue;
duke@435 2823 }
kvn@1535 2824 // push allocation's users on appropriate worklist
duke@435 2825 for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
duke@435 2826 Node *use = n->fast_out(i);
duke@435 2827 if(use->is_Mem() && use->in(MemNode::Address) == n) {
kvn@1535 2828 // Load/store to instance's field
kvn@500 2829 memnode_worklist.append_if_missing(use);
kvn@1535 2830 } else if (use->is_MemBar()) {
kvn@500 2831 memnode_worklist.append_if_missing(use);
kvn@500 2832 } else if (use->is_AddP() && use->outcnt() > 0) { // No dead nodes
kvn@500 2833 Node* addp2 = find_second_addp(use, n);
kvn@500 2834 if (addp2 != NULL) {
kvn@500 2835 alloc_worklist.append_if_missing(addp2);
kvn@500 2836 }
kvn@500 2837 alloc_worklist.append_if_missing(use);
kvn@500 2838 } else if (use->is_Phi() ||
kvn@500 2839 use->is_CheckCastPP() ||
kvn@603 2840 use->is_EncodeP() ||
kvn@603 2841 use->is_DecodeN() ||
kvn@500 2842 (use->is_ConstraintCast() && use->Opcode() == Op_CastPP)) {
kvn@500 2843 alloc_worklist.append_if_missing(use);
kvn@1535 2844 #ifdef ASSERT
kvn@1535 2845 } else if (use->is_Mem()) {
kvn@1535 2846 assert(use->in(MemNode::Address) != n, "EA: missing allocation reference path");
kvn@1535 2847 } else if (use->is_MergeMem()) {
kvn@1535 2848 assert(_mergemem_worklist.contains(use->as_MergeMem()), "EA: missing MergeMem node in the worklist");
kvn@1535 2849 } else if (use->is_SafePoint()) {
kvn@1535 2850 // Look for MergeMem nodes for calls which reference unique allocation
kvn@1535 2851 // (through CheckCastPP nodes) even for debug info.
kvn@1535 2852 Node* m = use->in(TypeFunc::Memory);
kvn@1535 2853 if (m->is_MergeMem()) {
kvn@1535 2854 assert(_mergemem_worklist.contains(m->as_MergeMem()), "EA: missing MergeMem node in the worklist");
kvn@1535 2855 }
kvn@1535 2856 } else {
kvn@1535 2857 uint op = use->Opcode();
kvn@1535 2858 if (!(op == Op_CmpP || op == Op_Conv2B ||
kvn@1535 2859 op == Op_CastP2X || op == Op_StoreCM ||
kvn@1535 2860 op == Op_FastLock || op == Op_AryEq || op == Op_StrComp ||
kvn@1535 2861 op == Op_StrEquals || op == Op_StrIndexOf)) {
kvn@1535 2862 n->dump();
kvn@1535 2863 use->dump();
kvn@1535 2864 assert(false, "EA: missing allocation reference path");
kvn@1535 2865 }
kvn@1535 2866 #endif
duke@435 2867 }
duke@435 2868 }
duke@435 2869
duke@435 2870 }
kvn@500 2871 // New alias types were created in split_AddP().
duke@435 2872 uint new_index_end = (uint) _compile->num_alias_types();
kvn@3651 2873 assert(unique_old == _compile->unique(), "there should be no new ideal nodes after Phase 1");
duke@435 2874
duke@435 2875 // Phase 2: Process MemNode's from memnode_worklist. compute new address type and
duke@435 2876 // compute new values for Memory inputs (the Memory inputs are not
duke@435 2877 // actually updated until phase 4.)
duke@435 2878 if (memnode_worklist.length() == 0)
duke@435 2879 return; // nothing to do
duke@435 2880 while (memnode_worklist.length() != 0) {
duke@435 2881 Node *n = memnode_worklist.pop();
kvn@500 2882 if (visited.test_set(n->_idx))
kvn@500 2883 continue;
kvn@1535 2884 if (n->is_Phi() || n->is_ClearArray()) {
kvn@1535 2885 // we don't need to do anything, but the users must be pushed
kvn@1535 2886 } else if (n->is_MemBar()) { // Initialize, MemBar nodes
kvn@1535 2887 // we don't need to do anything, but the users must be pushed
kvn@1535 2888 n = n->as_MemBar()->proj_out(TypeFunc::Memory);
kvn@500 2889 if (n == NULL)
duke@435 2890 continue;
duke@435 2891 } else {
duke@435 2892 assert(n->is_Mem(), "memory node required.");
duke@435 2893 Node *addr = n->in(MemNode::Address);
duke@435 2894 const Type *addr_t = igvn->type(addr);
duke@435 2895 if (addr_t == Type::TOP)
duke@435 2896 continue;
duke@435 2897 assert (addr_t->isa_ptr() != NULL, "pointer type required.");
duke@435 2898 int alias_idx = _compile->get_alias_index(addr_t->is_ptr());
kvn@500 2899 assert ((uint)alias_idx < new_index_end, "wrong alias index");
kvn@3651 2900 Node *mem = find_inst_mem(n->in(MemNode::Memory), alias_idx, orig_phis);
kvn@473 2901 if (_compile->failing()) {
kvn@473 2902 return;
kvn@473 2903 }
kvn@500 2904 if (mem != n->in(MemNode::Memory)) {
kvn@1536 2905 // We delay the memory edge update since we need old one in
kvn@1536 2906 // MergeMem code below when instances memory slices are separated.
kvn@3651 2907 set_map(n, mem);
kvn@500 2908 }
duke@435 2909 if (n->is_Load()) {
duke@435 2910 continue; // don't push users
duke@435 2911 } else if (n->is_LoadStore()) {
duke@435 2912 // get the memory projection
duke@435 2913 for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
duke@435 2914 Node *use = n->fast_out(i);
duke@435 2915 if (use->Opcode() == Op_SCMemProj) {
duke@435 2916 n = use;
duke@435 2917 break;
duke@435 2918 }
duke@435 2919 }
duke@435 2920 assert(n->Opcode() == Op_SCMemProj, "memory projection required");
duke@435 2921 }
duke@435 2922 }
duke@435 2923 // push user on appropriate worklist
duke@435 2924 for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
duke@435 2925 Node *use = n->fast_out(i);
kvn@1535 2926 if (use->is_Phi() || use->is_ClearArray()) {
kvn@500 2927 memnode_worklist.append_if_missing(use);
duke@435 2928 } else if(use->is_Mem() && use->in(MemNode::Memory) == n) {
kvn@1535 2929 if (use->Opcode() == Op_StoreCM) // Ignore cardmark stores
kvn@1535 2930 continue;
kvn@500 2931 memnode_worklist.append_if_missing(use);
kvn@1535 2932 } else if (use->is_MemBar()) {
kvn@500 2933 memnode_worklist.append_if_missing(use);
kvn@1535 2934 #ifdef ASSERT
kvn@1535 2935 } else if(use->is_Mem()) {
kvn@1535 2936 assert(use->in(MemNode::Memory) != n, "EA: missing memory path");
duke@435 2937 } else if (use->is_MergeMem()) {
kvn@1535 2938 assert(_mergemem_worklist.contains(use->as_MergeMem()), "EA: missing MergeMem node in the worklist");
kvn@1535 2939 } else {
kvn@1535 2940 uint op = use->Opcode();
kvn@1535 2941 if (!(op == Op_StoreCM ||
kvn@1535 2942 (op == Op_CallLeaf && use->as_CallLeaf()->_name != NULL &&
kvn@1535 2943 strcmp(use->as_CallLeaf()->_name, "g1_wb_pre") == 0) ||
kvn@1535 2944 op == Op_AryEq || op == Op_StrComp ||
kvn@1535 2945 op == Op_StrEquals || op == Op_StrIndexOf)) {
kvn@1535 2946 n->dump();
kvn@1535 2947 use->dump();
kvn@1535 2948 assert(false, "EA: missing memory path");
kvn@1535 2949 }
kvn@1535 2950 #endif
duke@435 2951 }
duke@435 2952 }
duke@435 2953 }
duke@435 2954
kvn@500 2955 // Phase 3: Process MergeMem nodes from mergemem_worklist.
kvn@1535 2956 // Walk each memory slice moving the first node encountered of each
kvn@500 2957 // instance type to the the input corresponding to its alias index.
kvn@1535 2958 uint length = _mergemem_worklist.length();
kvn@1535 2959 for( uint next = 0; next < length; ++next ) {
kvn@1535 2960 MergeMemNode* nmm = _mergemem_worklist.at(next);
kvn@1535 2961 assert(!visited.test_set(nmm->_idx), "should not be visited before");
duke@435 2962 // Note: we don't want to use MergeMemStream here because we only want to
kvn@1535 2963 // scan inputs which exist at the start, not ones we add during processing.
kvn@1535 2964 // Note 2: MergeMem may already contains instance memory slices added
kvn@1535 2965 // during find_inst_mem() call when memory nodes were processed above.
kvn@1535 2966 igvn->hash_delete(nmm);
duke@435 2967 uint nslices = nmm->req();
duke@435 2968 for (uint i = Compile::AliasIdxRaw+1; i < nslices; i++) {
kvn@500 2969 Node* mem = nmm->in(i);
kvn@500 2970 Node* cur = NULL;
duke@435 2971 if (mem == NULL || mem->is_top())
duke@435 2972 continue;
kvn@1536 2973 // First, update mergemem by moving memory nodes to corresponding slices
kvn@1536 2974 // if their type became more precise since this mergemem was created.
duke@435 2975 while (mem->is_Mem()) {
duke@435 2976 const Type *at = igvn->type(mem->in(MemNode::Address));
duke@435 2977 if (at != Type::TOP) {
duke@435 2978 assert (at->isa_ptr() != NULL, "pointer type required.");
duke@435 2979 uint idx = (uint)_compile->get_alias_index(at->is_ptr());
duke@435 2980 if (idx == i) {
duke@435 2981 if (cur == NULL)
duke@435 2982 cur = mem;
duke@435 2983 } else {
duke@435 2984 if (idx >= nmm->req() || nmm->is_empty_memory(nmm->in(idx))) {
duke@435 2985 nmm->set_memory_at(idx, mem);
duke@435 2986 }
duke@435 2987 }
duke@435 2988 }
duke@435 2989 mem = mem->in(MemNode::Memory);
duke@435 2990 }
duke@435 2991 nmm->set_memory_at(i, (cur != NULL) ? cur : mem);
kvn@500 2992 // Find any instance of the current type if we haven't encountered
kvn@1536 2993 // already a memory slice of the instance along the memory chain.
kvn@500 2994 for (uint ni = new_index_start; ni < new_index_end; ni++) {
kvn@500 2995 if((uint)_compile->get_general_index(ni) == i) {
kvn@500 2996 Node *m = (ni >= nmm->req()) ? nmm->empty_memory() : nmm->in(ni);
kvn@500 2997 if (nmm->is_empty_memory(m)) {
kvn@3651 2998 Node* result = find_inst_mem(mem, ni, orig_phis);
kvn@500 2999 if (_compile->failing()) {
kvn@500 3000 return;
kvn@500 3001 }
kvn@500 3002 nmm->set_memory_at(ni, result);
kvn@500 3003 }
kvn@500 3004 }
kvn@500 3005 }
kvn@500 3006 }
kvn@500 3007 // Find the rest of instances values
kvn@500 3008 for (uint ni = new_index_start; ni < new_index_end; ni++) {
kvn@1536 3009 const TypeOopPtr *tinst = _compile->get_adr_type(ni)->isa_oopptr();
kvn@500 3010 Node* result = step_through_mergemem(nmm, ni, tinst);
kvn@500 3011 if (result == nmm->base_memory()) {
kvn@500 3012 // Didn't find instance memory, search through general slice recursively.
kvn@1536 3013 result = nmm->memory_at(_compile->get_general_index(ni));
kvn@3651 3014 result = find_inst_mem(result, ni, orig_phis);
kvn@500 3015 if (_compile->failing()) {
kvn@500 3016 return;
kvn@500 3017 }
kvn@500 3018 nmm->set_memory_at(ni, result);
kvn@500 3019 }
kvn@500 3020 }
kvn@500 3021 igvn->hash_insert(nmm);
kvn@500 3022 record_for_optimizer(nmm);
duke@435 3023 }
duke@435 3024
kvn@500 3025 // Phase 4: Update the inputs of non-instance memory Phis and
kvn@500 3026 // the Memory input of memnodes
duke@435 3027 // First update the inputs of any non-instance Phi's from
duke@435 3028 // which we split out an instance Phi. Note we don't have
duke@435 3029 // to recursively process Phi's encounted on the input memory
duke@435 3030 // chains as is done in split_memory_phi() since they will
duke@435 3031 // also be processed here.
kvn@682 3032 for (int j = 0; j < orig_phis.length(); j++) {
kvn@682 3033 PhiNode *phi = orig_phis.at(j);
duke@435 3034 int alias_idx = _compile->get_alias_index(phi->adr_type());
duke@435 3035 igvn->hash_delete(phi);
duke@435 3036 for (uint i = 1; i < phi->req(); i++) {
duke@435 3037 Node *mem = phi->in(i);
kvn@3651 3038 Node *new_mem = find_inst_mem(mem, alias_idx, orig_phis);
kvn@500 3039 if (_compile->failing()) {
kvn@500 3040 return;
kvn@500 3041 }
duke@435 3042 if (mem != new_mem) {
duke@435 3043 phi->set_req(i, new_mem);
duke@435 3044 }
duke@435 3045 }
duke@435 3046 igvn->hash_insert(phi);
duke@435 3047 record_for_optimizer(phi);
duke@435 3048 }
duke@435 3049
duke@435 3050 // Update the memory inputs of MemNodes with the value we computed
kvn@1536 3051 // in Phase 2 and move stores memory users to corresponding memory slices.
kvn@2810 3052 // Disable memory split verification code until the fix for 6984348.
kvn@2810 3053 // Currently it produces false negative results since it does not cover all cases.
kvn@2810 3054 #if 0 // ifdef ASSERT
kvn@2556 3055 visited.Reset();
kvn@1536 3056 Node_Stack old_mems(arena, _compile->unique() >> 2);
kvn@1536 3057 #endif
kvn@3651 3058 for (uint i = 0; i < ideal_nodes.size(); i++) {
kvn@3651 3059 Node* n = ideal_nodes.at(i);
kvn@3651 3060 Node* nmem = get_map(n->_idx);
kvn@3651 3061 assert(nmem != NULL, "sanity");
kvn@3651 3062 if (n->is_Mem()) {
kvn@2810 3063 #if 0 // ifdef ASSERT
kvn@3651 3064 Node* old_mem = n->in(MemNode::Memory);
kvn@3651 3065 if (!visited.test_set(old_mem->_idx)) {
kvn@3651 3066 old_mems.push(old_mem, old_mem->outcnt());
kvn@3651 3067 }
kvn@1536 3068 #endif
kvn@3651 3069 assert(n->in(MemNode::Memory) != nmem, "sanity");
kvn@3651 3070 if (!n->is_Load()) {
kvn@3651 3071 // Move memory users of a store first.
kvn@3651 3072 move_inst_mem(n, orig_phis);
duke@435 3073 }
kvn@3651 3074 // Now update memory input
kvn@3651 3075 igvn->hash_delete(n);
kvn@3651 3076 n->set_req(MemNode::Memory, nmem);
kvn@3651 3077 igvn->hash_insert(n);
kvn@3651 3078 record_for_optimizer(n);
kvn@3651 3079 } else {
kvn@3651 3080 assert(n->is_Allocate() || n->is_CheckCastPP() ||
kvn@3651 3081 n->is_AddP() || n->is_Phi(), "unknown node used for set_map()");
duke@435 3082 }
duke@435 3083 }
kvn@2810 3084 #if 0 // ifdef ASSERT
kvn@1536 3085 // Verify that memory was split correctly
kvn@1536 3086 while (old_mems.is_nonempty()) {
kvn@1536 3087 Node* old_mem = old_mems.node();
kvn@1536 3088 uint old_cnt = old_mems.index();
kvn@1536 3089 old_mems.pop();
kvn@2810 3090 assert(old_cnt == old_mem->outcnt(), "old mem could be lost");
kvn@1536 3091 }
kvn@1536 3092 #endif
duke@435 3093 }
duke@435 3094
kvn@3651 3095 #ifndef PRODUCT
kvn@3651 3096 static const char *node_type_names[] = {
kvn@3651 3097 "UnknownType",
kvn@3651 3098 "JavaObject",
kvn@3651 3099 "LocalVar",
kvn@3651 3100 "Field",
kvn@3651 3101 "Arraycopy"
kvn@3651 3102 };
kvn@3651 3103
kvn@3651 3104 static const char *esc_names[] = {
kvn@3651 3105 "UnknownEscape",
kvn@3651 3106 "NoEscape",
kvn@3651 3107 "ArgEscape",
kvn@3651 3108 "GlobalEscape"
kvn@3651 3109 };
kvn@3651 3110
kvn@3651 3111 void PointsToNode::dump(bool print_state) const {
kvn@3651 3112 NodeType nt = node_type();
kvn@3651 3113 tty->print("%s ", node_type_names[(int) nt]);
kvn@3651 3114 if (print_state) {
kvn@3651 3115 EscapeState es = escape_state();
kvn@3651 3116 EscapeState fields_es = fields_escape_state();
kvn@3651 3117 tty->print("%s(%s) ", esc_names[(int)es], esc_names[(int)fields_es]);
kvn@3651 3118 if (nt == PointsToNode::JavaObject && !this->scalar_replaceable())
kvn@3651 3119 tty->print("NSR");
kvn@3651 3120 }
kvn@3651 3121 if (is_Field()) {
kvn@3651 3122 FieldNode* f = (FieldNode*)this;
kvn@3651 3123 tty->print("(");
kvn@3651 3124 for (BaseIterator i(f); i.has_next(); i.next()) {
kvn@3651 3125 PointsToNode* b = i.get();
kvn@3651 3126 tty->print(" %d%s", b->idx(),(b->is_JavaObject() ? "P" : ""));
kvn@679 3127 }
kvn@3651 3128 tty->print(" )");
kvn@679 3129 }
kvn@3651 3130 tty->print("[");
kvn@3651 3131 for (EdgeIterator i(this); i.has_next(); i.next()) {
kvn@3651 3132 PointsToNode* e = i.get();
kvn@3651 3133 tty->print(" %d%s%s", e->idx(),(e->is_JavaObject() ? "P" : (e->is_Field() ? "F" : "")), e->is_Arraycopy() ? "cp" : "");
kvn@3651 3134 }
kvn@3651 3135 tty->print(" [");
kvn@3651 3136 for (UseIterator i(this); i.has_next(); i.next()) {
kvn@3651 3137 PointsToNode* u = i.get();
kvn@3651 3138 bool is_base = false;
kvn@3651 3139 if (PointsToNode::is_base_use(u)) {
kvn@3651 3140 is_base = true;
kvn@3651 3141 u = PointsToNode::get_use_node(u)->as_Field();
kvn@3651 3142 }
kvn@3651 3143 tty->print(" %d%s%s", u->idx(), is_base ? "b" : "", u->is_Arraycopy() ? "cp" : "");
kvn@3651 3144 }
kvn@3651 3145 tty->print(" ]] ");
kvn@3651 3146 if (_node == NULL)
kvn@3651 3147 tty->print_cr("<null>");
kvn@3651 3148 else
kvn@3651 3149 _node->dump();
kvn@679 3150 }
kvn@679 3151
kvn@3651 3152 void ConnectionGraph::dump(GrowableArray<PointsToNode*>& ptnodes_worklist) {
duke@435 3153 bool first = true;
kvn@3651 3154 int ptnodes_length = ptnodes_worklist.length();
kvn@3651 3155 for (int i = 0; i < ptnodes_length; i++) {
kvn@3651 3156 PointsToNode *ptn = ptnodes_worklist.at(i);
kvn@3651 3157 if (ptn == NULL || !ptn->is_JavaObject())
duke@435 3158 continue;
kvn@3651 3159 PointsToNode::EscapeState es = ptn->escape_state();
kvn@3651 3160 if (ptn->ideal_node()->is_Allocate() && (es == PointsToNode::NoEscape || Verbose)) {
kvn@500 3161 if (first) {
kvn@500 3162 tty->cr();
kvn@500 3163 tty->print("======== Connection graph for ");
kvn@679 3164 _compile->method()->print_short_name();
kvn@500 3165 tty->cr();
kvn@500 3166 first = false;
kvn@500 3167 }
kvn@500 3168 ptn->dump();
kvn@3651 3169 // Print all locals and fields which reference this allocation
kvn@3651 3170 for (UseIterator j(ptn); j.has_next(); j.next()) {
kvn@3651 3171 PointsToNode* use = j.get();
kvn@3651 3172 if (use->is_LocalVar()) {
kvn@3651 3173 use->dump(Verbose);
kvn@3651 3174 } else if (Verbose) {
kvn@3651 3175 use->dump();
kvn@500 3176 }
kvn@500 3177 }
kvn@500 3178 tty->cr();
duke@435 3179 }
duke@435 3180 }
duke@435 3181 }
duke@435 3182 #endif

mercurial