escape.cpp revision 221
844N/A * Copyright 2005-2006 Sun Microsystems, Inc. All Rights Reserved. 0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 0N/A * This code is free software; you can redistribute it and/or modify it 0N/A * under the terms of the GNU General Public License version 2 only, as 0N/A * published by the Free Software Foundation. 0N/A * This code is distributed in the hope that it will be useful, but WITHOUT 0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 0N/A * version 2 for more details (a copy is included in the LICENSE file that 0N/A * accompanied this code). 0N/A * You should have received a copy of the GNU General Public License version 0N/A * 2 along with this work; if not, write to the Free Software Foundation, 0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 0N/A * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 0N/A * CA 95054 USA or visit www.sun.com if you need additional information or 0N/A * have any questions. 0N/A#
include "incls/_precompiled.incl" 0N/A // don't add a self-referential edge, this can occur during removal of 0N/A // We are computing a raw address for a store captured by an Initialize 65N/A // compute an appropriate address type. AddP cases #3 and #5 (see below). 65N/A "offset must be a constant or it is initialization of array");
65N/A // inline set_escape_state(idx, es); 65N/A // If we are still collecting or there were no non-escaping allocations 65N/A // we don't know the answer yet 0N/A // if the node was created after the escape computation, return 0N/A // if we have already computed a value, return it 244N/A // compute max escape state of anything this node could point to 244N/A // cache the computed escape state 0N/A // If we have a JavaObject, return just that object 124N/A // ensure that all inputs of a Phi have been processed 244N/A assert(
false,
"neither PointsToEdge or DeferredEdge");
244N/A // no deferred or pointsto edges found. Assume the value was set 244N/A // outside this method. Add the phantom object to the pointsto set. 244N/A // This method is most expensive during ConnectionGraph construction. 244N/A // Reuse vectorSet and an additional growable array for deferred edges. 0N/A // Mark current edges as visited and move deferred edges to separate array. 101N/A // Special case - field set outside (globally escaping). 244N/A// Add an edge to node given by "to_i" from any field of adr_i whose offset 244N/A// matches "offset" A deferred edge is added if to_i is a LocalVar, and 244N/A// a pointsto edge is added if it is a JavaObject 244N/A// Add a deferred edge from node given by "from_i" to any field of adr_i 244N/A// whose offset matches "offset". 0N/A // we have not seen any stores to this field, assume it was set outside this method 0N/A // AddP cases for Base and Address inputs: 0N/A // case #1. Direct object's field reference: 0N/A // Proj #5 ( oop result ) 0N/A // CheckCastPP (cast to instance type) 65N/A // AddP ( base == address ) 65N/A // case #2. Indirect object's field reference: 65N/A // CastPP (cast to instance type) 65N/A // AddP ( base == address ) 65N/A // case #3. Raw object's field reference for Initialize node: 65N/A // Proj #5 ( oop result ) 65N/A // AddP ( base == top ) 65N/A // case #4. Array's element reference: 65N/A // {CheckCastPP | CastPP} 65N/A // | AddP ( array's element offset ) 65N/A // AddP ( array's offset ) 65N/A // case #5. Raw object's field reference for arraycopy stub call: 65N/A // The inline_native_clone() case when the arraycopy stub is called 65N/A // after the allocation before Initialize and CheckCastPP nodes. 65N/A // Proj #5 ( oop result ) 65N/A // AddP ( base == address ) 65N/A // case #6. Constant Pool, ThreadLocal, CastX2P or 65N/A // Raw object's field reference: 65N/A // {ConP, ThreadLocal, CastX2P, raw Load} 65N/A // AddP ( base == top ) 65N/A // case #7. Klass's field reference. 65N/A // AddP ( base == address ) 77N/A // case #8. narrow Klass's field reference. 65N/A // AddP ( base == address ) 168N/A // Find array's offset to push it on worklist first and 77N/A // as result process an array's element offset first (pushed second) 77N/A // to avoid CastPP for the array's offset. 0N/A // Otherwise the inserted CastPP (LocalVar) will point to what 65N/A // the AddP (Field) points to. Which would be wrong since 65N/A // the algorithm expects the CastPP has the same point as 65N/A // as AddP's base CheckCastPP (LocalVar). 65N/A // memProj (from ArrayAllocation CheckCastPP) 65N/A // | || Int (element index) 65N/A // | || | ConI (log(element size)) 65N/A // | AddP (array's element offset) 65N/A // | | ConI (array's offset: #12(32-bits) or #24(64-bits)) 65N/A // AddP (array's offset) 65N/A// Adjust the type and inputs of an AddP which computes the 65N/A// address of a field of an instance 65N/A // We are computing a raw address for a store captured by an Initialize 65N/A // compute an appropriate address type. 0N/A "old type must be non-instance or match new type");
293N/A // Do NOT remove the next call: ensure an new alias index is allocated 65N/A // for the instance type 65N/A // record the allocation in the node map 293N/A // if the Address input is not the appropriate instance type 65N/A // (due to intervening casts,) insert a cast 293N/A // Put on IGVN worklist since at least addp's type was changed above. 293N/A// Create a new version of orig_phi if necessary. Returns either the newly 293N/A// created phi or an existing phi. Sets create_new to indicate wheter a new 293N/A// phi was created. Cache the last newly created phi in the node map. 0N/A // nothing to do if orig_phi is bottom memory or matches alias_idx 253N/A // have we already created a Phi for this alias index? 253N/A // Retry compilation without escape analysis. 253N/A // If this is the first failure, the sentinel string will "stick" 253N/A // to the Compile object, and the C2Compiler will see it and retry. 65N/A// Return a new version of Memory Phi "orig_phi" with the inputs having the 65N/A// specified alias index. 851N/A // found an phi for which we created a new split, push current one on worklist and begin 38N/A // verify that the new Phi has an input for each input of the original 0N/A // Check if all new phi's inputs have specified alias index. 851N/A // Otherwise use old phi. 1101N/A // we have finished processing a Phi, see if there are any more to do 0N/A// The next methods are derived from methods in MemNode. 0N/A // TypeInstPtr::NOTNULL+any is an OOP with unknown offset - generally 0N/A // means an array I have not precisely typed yet. Do not do any 0N/A // alias stuff with it any time soon. 0N/A // Update input if it is progress over what we have now 65N/A// Search memory chain of "mem" to find a MemNode whose address 0N/A// is the specified alias index. 65N/A continue;
// don't search further for non-instance types 0N/A // skip over a call which does not affect this memory slice 65N/A // Stop if this is the initialization for the object instance which 65N/A // which contains this memory slice, otherwise skip over it. 65N/A // Didn't find instance memory, search through general slice recursively. 1101N/A // the result is either MemNode, PhiNode, InitializeNode. 1101N/A// Convert the types of unescaped object to instance types where possible, 1101N/A// propagate the new type information through the graph, and update memory 1101N/A// edges and MergeMem inputs to reflect the new type. 1101N/A// We start with allocations (and calls which may be allocations) on alloc_worklist. 1101N/A// The processing is done in 4 phases: 1101N/A// Phase 1: Process possible allocations from alloc_worklist. Create instance 1101N/A// types for the CheckCastPP for allocations where possible. 1101N/A// Propagate the the new types through users as follows: 1101N/A// casts and Phi: push users on alloc_worklist 1101N/A// AddP: cast Base and Address inputs to the instance type 1101N/A// push any AddP users on alloc_worklist and push any memnode 1101N/A// users onto memnode_worklist. 1101N/A// Phase 2: Process MemNode's from memnode_worklist. compute new address type and 1101N/A// search the Memory chain for a store with the appropriate type 1101N/A// address type. If a Phi is found, create a new version with 1101N/A// the approriate memory slices from each of the Phi inputs. 1101N/A// For stores, process the users as follows: 1101N/A// MemNode: push on memnode_worklist 1101N/A// MergeMem: push on mergemem_worklist 1101N/A// Phase 3: Process MergeMem nodes from mergemem_worklist. Walk each memory slice 1101N/A// moving the first node encountered of each instance type to the 1101N/A// the input corresponding to its alias index. 1101N/A// appropriate memory slice. 1101N/A// Phase 4: Update the inputs of non-instance memory Phis and the Memory input of memnodes. 1101N/A// In the following example, the CheckCastPP nodes are the cast of allocation 1101N/A// results and the allocation of node 29 is unescaped and eligible to be an 1101N/A// 20 AddP _ 19 19 10 Foo+12 alias_index=4 1101N/A// 30 AddP _ 29 29 10 Foo+12 alias_index=4 1101N/A// 40 StoreP 25 7 20 ... alias_index=4 1101N/A// 50 StoreP 35 40 30 ... alias_index=4 1101N/A// 60 StoreP 45 50 20 ... alias_index=4 1101N/A// 70 LoadP _ 60 30 ... alias_index=4 1101N/A// 80 Phi 75 50 60 Memory alias_index=4 1101N/A// 90 LoadP _ 80 30 ... alias_index=4 1101N/A// 100 LoadP _ 80 20 ... alias_index=4 1101N/A// Phase 1 creates an instance type for node 29 assigning it an instance id of 24 1101N/A// and creating a new alias index for node 30. This gives: 1101N/A// 20 AddP _ 19 19 10 Foo+12 alias_index=4 1101N/A// 29 CheckCastPP "Foo" iid=24 65N/A// 30 AddP _ 29 29 10 Foo+12 alias_index=6 iid=24 65N/A// 40 StoreP 25 7 20 ... alias_index=4 65N/A// 50 StoreP 35 40 30 ... alias_index=6 65N/A// 60 StoreP 45 50 20 ... alias_index=4 65N/A// 70 LoadP _ 60 30 ... alias_index=6 65N/A// 80 Phi 75 50 60 Memory alias_index=4 65N/A// 90 LoadP _ 80 30 ... alias_index=6 223N/A// 100 LoadP _ 80 20 ... alias_index=4 65N/A// In phase 2, new memory inputs are computed for the loads and stores, 65N/A// And a new version of the phi is created. In phase 4, the inputs to 65N/A// node 80 are updated and then the memory nodes are updated with the 65N/A// values computed in phase 2. This results in: 65N/A// 20 AddP _ 19 19 10 Foo+12 alias_index=4 65N/A// 29 CheckCastPP "Foo" iid=24 65N/A// 30 AddP _ 29 29 10 Foo+12 alias_index=6 iid=24 65N/A// 40 StoreP 25 7 20 ... alias_index=4 65N/A// 50 StoreP 35 7 30 ... alias_index=6 253N/A// 60 StoreP 45 40 20 ... alias_index=4 65N/A// 70 LoadP _ 50 30 ... alias_index=6 65N/A// 80 Phi 75 40 60 Memory alias_index=4 65N/A// 120 Phi 75 50 50 Memory alias_index=6 65N/A// 90 LoadP _ 120 30 ... alias_index=6 65N/A// 100 LoadP _ 80 20 ... alias_index=4 65N/A // Phase 1: Process possible allocations from alloc_worklist. 223N/A // Create instance types for the CheckCastPP for allocations where possible. 65N/A // copy escape information to call node 65N/A // We have an allocation or call which returns a Java object, 65N/A // see if it is unescaped. 65N/A // Set the scalar_replaceable flag before the next check. 65N/A // find CheckCastPP of call return value 65N/A if (n ==
NULL ||
// No uses accept Initialize or 65N/A // The inline code for Object.clone() casts the allocation result to 65N/A // java.lang.Object and then to the the actual type of the allocated 65N/A // object. Detect this case and use the second cast. 584N/A // in order for an object to be stackallocatable, it must be: 65N/A // - a direct allocation (not a call returning an object) 247N/A // - eligible to be a unique type 65N/A // - not determined to be ineligible by escape analysis 65N/A continue;
// not a TypeInstPtr 65N/A // First, put on the worklist all Field edges from Connection Graph 0N/A // which is more accurate then putting immediate users from Ideal Graph. 0N/A "only AddP nodes are Field edges in CG");
0N/A // An allocation may have an Initialize which has raw stores. Scan 0N/A // the users of the raw allocation result and push AddP users 0N/A // on alloc_worklist. 0N/A continue;
// Assume the value was set outside this method. 0N/A continue;
// already processed 0N/A continue;
// Assume the value was set outside this method. 0N/A // push users on appropriate worklist 1101N/A // Look for MergeMem nodes for calls which reference unique allocation 1101N/A // (through CheckCastPP nodes) even for debug info. 784N/A // New alias types were created in split_AddP(). 784N/A // Phase 2: Process MemNode's from memnode_worklist. compute new address type and 784N/A // compute new values for Memory inputs (the Memory inputs are not 784N/A // actually updated until phase 4.) 784N/A return;
// nothing to do 247N/A // we don't need to do anything, but the users must be pushed if we haven't processed 247N/A // we don't need to do anything, but the users of the memory projection must be pushed 784N/A continue;
// don't push users 784N/A // get the memory projection 0N/A // push user on appropriate worklist 163N/A // Phase 3: Process MergeMem nodes from mergemem_worklist. 244N/A // Walk each memory moving the first node encountered of each 163N/A // instance type to the the input corresponding to its alias index. 163N/A // Note: we don't want to use MergeMemStream here because we only want to 163N/A // scan inputs which exist at the start, not ones we add during processing. 65N/A // Find any instance of the current type if we haven't encountered 0N/A // a value of the instance along the chain. 0N/A // Find the rest of instances values 0N/A // Didn't find instance memory, search through general slice recursively. 163N/A // Propagate new memory slices to following MergeMem nodes. 1100N/A // Phase 4: Update the inputs of non-instance memory Phis and 65N/A // the Memory input of memnodes 1100N/A // First update the inputs of any non-instance Phi's from 1100N/A // which we split out an instance Phi. Note we don't have 65N/A // to recursively process Phi's encounted on the input memory 65N/A // chains as is done in split_memory_phi() since they will 65N/A // also be processed here. 1100N/A // Update the memory inputs of MemNodes with the value we computed 65N/A // 1. Populate Connection Graph (CG) with Ideal nodes. 0N/A // Initialize worklist 1100N/A // Push all useful nodes onto CG list and set their type. 0N/A // 2. First pass to create simple CG edges (doesn't require to walk CG). 0N/A // 3. Pass to create fields edges (Allocate -F-> AddP). 0N/A // 4. Build Connection Graph which need 0N/A // to walk the connection graph. 1100N/A // remove deferred edges from the graph and collect 1100N/A // information we will need for type splitting 1100N/A // If this AddP computes an address which may point to more that one 1100N/A // object or more then one field (array's element), nothing the address 0N/A // points to can be scalar replaceable. 1100N/A // Push call on alloc_worlist (alocations are calls) 1100N/A // for processing by split_unique_types(). 65N/A // push all GlobalEscape nodes on the worklist 0N/A // mark all node reachable from GlobalEscape nodes 0N/A // push all ArgEscape nodes on the worklist 65N/A // mark all node reachable from ArgEscape nodes 65N/A // push all NoEscape nodes on the worklist 65N/A // mark all node reachable from NoEscape nodes 0N/A return;
// Nothing to do. 0N/A // Now use the escape information to create unique types for 0N/A // unescaped objects 1101N/A // Clean up after split unique types. 0N/A // Stub calls, objects do not escape but they are not scale replaceable. 1101N/A // Adjust escape state for outgoing arguments. 244N/A // The inline_native_clone() case when the arraycopy stub is called 244N/A // after the allocation before Initialize and CheckCastPP nodes. 244N/A // Set AddP's base (Allocate) as not scalar replaceable since 244N/A // pointer to the base (with offset) is passed as argument. 65N/A // For a static call, we know exactly what method is being called. 244N/A // Use bytecode estimator to record the call's escape affects 244N/A // fall-through if not a Java method or no analyzer information 65N/A // The argument global escapes, mark everything it could point to 1100N/A // The argument itself doesn't escape, but any fields might 65N/A //The argument global escapes, mark everything it could point to 0N/A // The argument itself doesn't escape, but any fields might 244N/A // Fall-through here if not a Java method or no analyzer information 65N/A // or some other type of call, assume the worst case: all arguments 65N/A // adjust escape state for outgoing arguments 1100N/A // Also works for DecodeN(LoadNKlass). 0N/A // Not scalar replaceable if the length is not constant or too big. 0N/A // For a static call, we know exactly what method is being called. 244N/A // Use bytecode estimator to record whether the call's return value escapes 244N/A // Note: we use isa_ptr() instead of isa_oopptr() here because the 0N/A // _multianewarray functions return a TypeRawPtr. 65N/A break;
// doesn't return a pointer type 65N/A // not a Java method, assume global escape 244N/A // Returns a newly allocated unescaped object, simply 605N/A // update dependency information. 244N/A // Mark it as NoEscape so that objects referenced by 244N/A // it's fields will be marked as NoEscape at least. 65N/A // determine whether any arguments are returned 1100N/A // Some other type of call, assume the worst case that the 1100N/A // returned value, if any, globally escapes. 1100N/A // Note: we use isa_ptr() instead of isa_oopptr() here because the 1100N/A // _multianewarray functions return a TypeRawPtr. 1100N/A// Populate Connection Graph with Ideal nodes and create simple 1100N/A// connection graph edges (do not need to check the node_type of inputs 1100N/A// or to call PointsTo() to walk the connection graph). 1100N/A return;
// No need to redefine node's state. 1100N/A // Arguments to allocation and locking don't escape. 1100N/A // Put Lock and Unlock nodes on IGVN worklist to process them during 1100N/A // the first IGVN optimization when escape information is still available. 1100N/A // Have to process call's arguments first. 1100N/A // Check if a call returns an object. 1100N/A // Note: use isa_ptr() instead of isa_oopptr() here because 1100N/A // the _multianewarray functions return a TypeRawPtr. 1100N/A // Using isa_ptr() instead of isa_oopptr() for LoadP and Phi because 1100N/A // ThreadLocal has RawPrt type. 1100N/A {
// "Unsafe" memory access. 1100N/A // assume all pointer constants globally escape except for null 1100N/A // assume all narrow oop constants globally escape except for null 1100N/A // assume that all exception objects globally escape 0N/A // We have to assume all input parameters globally escape 0N/A // (Note: passing 'false' since _processed is already set). 0N/A // nothing to do if not an oop 65N/A continue;
// ignore NULL 65N/A continue;
// ignore top or inputs which go back this node 65N/A // we are only interested in the result projection from a call 65N/A // The call's result may need to be processed later if the call 65N/A // returns it's argument and the argument is not processed yet. 65N/A // Treat Return value as LocalVar with GlobalEscape escape state. 65N/A // We are computing a raw address for a store captured 65N/A // by an Initialize compute an appropriate address type. 0N/A // Don't set processed bit for AddP, LoadP, StoreP since 0N/A // they may need more then one pass to process. 0N/A return;
// No need to redefine node's state. 244N/A // Create a field edge to this node from everything base could point to. 0N/A // For everything "adr_base" could point to, create a deferred edge from 244N/A // this node to each field with the same offset. 65N/A continue;
// ignore NULL 65N/A continue;
// ignore top or inputs which go back this node 307N/A // we are only interested in the result projection from a call 0N/A // For everything "adr_base" could point to, create a deferred edge 65N/A // to "val" from each field with the same offset. 65N/A // Print all locals which reference this allocation 124N/A // Print all fields which reference this allocation