1 /*
   2  * Copyright (c) 2011, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 package org.graalvm.compiler.virtual.phases.ea;
  24 
  25 import java.util.ArrayList;
  26 import java.util.BitSet;
  27 import java.util.Iterator;
  28 import java.util.List;
  29 import java.util.function.IntUnaryOperator;
  30 
  31 import org.graalvm.compiler.core.common.GraalOptions;
  32 import org.graalvm.compiler.core.common.cfg.Loop;
  33 import org.graalvm.compiler.core.common.spi.ConstantFieldProvider;
  34 import org.graalvm.compiler.core.common.type.Stamp;
  35 import org.graalvm.compiler.core.common.type.StampFactory;
  36 import org.graalvm.compiler.debug.CounterKey;
  37 import org.graalvm.compiler.debug.DebugContext;
  38 import org.graalvm.compiler.graph.Node;
  39 import org.graalvm.compiler.graph.NodeBitMap;
  40 import org.graalvm.compiler.graph.Position;
  41 import org.graalvm.compiler.graph.spi.Canonicalizable;
  42 import org.graalvm.compiler.nodes.AbstractEndNode;
  43 import org.graalvm.compiler.nodes.CallTargetNode;
  44 import org.graalvm.compiler.nodes.ConstantNode;
  45 import org.graalvm.compiler.nodes.ControlSinkNode;
  46 import org.graalvm.compiler.nodes.FixedNode;
  47 import org.graalvm.compiler.nodes.FixedWithNextNode;
  48 import org.graalvm.compiler.nodes.FrameState;
  49 import org.graalvm.compiler.nodes.Invoke;
  50 import org.graalvm.compiler.nodes.LoopBeginNode;
  51 import org.graalvm.compiler.nodes.LoopExitNode;
  52 import org.graalvm.compiler.nodes.PhiNode;
  53 import org.graalvm.compiler.nodes.ProxyNode;
  54 import org.graalvm.compiler.nodes.StructuredGraph;
  55 import org.graalvm.compiler.nodes.StructuredGraph.ScheduleResult;
  56 import org.graalvm.compiler.nodes.ValueNode;
  57 import org.graalvm.compiler.nodes.ValuePhiNode;
  58 import org.graalvm.compiler.nodes.ValueProxyNode;
  59 import org.graalvm.compiler.nodes.VirtualState;
  60 import org.graalvm.compiler.nodes.VirtualState.NodeClosure;
  61 import org.graalvm.compiler.nodes.cfg.Block;
  62 import org.graalvm.compiler.nodes.spi.LoweringProvider;
  63 import org.graalvm.compiler.nodes.spi.NodeWithState;
  64 import org.graalvm.compiler.nodes.spi.Virtualizable;
  65 import org.graalvm.compiler.nodes.spi.VirtualizableAllocation;
  66 import org.graalvm.compiler.nodes.spi.VirtualizerTool;
  67 import org.graalvm.compiler.nodes.virtual.AllocatedObjectNode;
  68 import org.graalvm.compiler.nodes.virtual.VirtualObjectNode;
  69 import org.graalvm.compiler.virtual.nodes.VirtualObjectState;
  70 import org.graalvm.util.EconomicMap;
  71 import org.graalvm.util.EconomicSet;
  72 import org.graalvm.util.Equivalence;
  73 
  74 import jdk.vm.ci.meta.ConstantReflectionProvider;
  75 import jdk.vm.ci.meta.JavaConstant;
  76 import jdk.vm.ci.meta.JavaKind;
  77 import jdk.vm.ci.meta.MetaAccessProvider;
  78 
  79 public abstract class PartialEscapeClosure<BlockT extends PartialEscapeBlockState<BlockT>> extends EffectsClosure<BlockT> {
  80 
  81     public static final CounterKey COUNTER_MATERIALIZATIONS = DebugContext.counter("Materializations");
  82     public static final CounterKey COUNTER_MATERIALIZATIONS_PHI = DebugContext.counter("MaterializationsPhi");
  83     public static final CounterKey COUNTER_MATERIALIZATIONS_MERGE = DebugContext.counter("MaterializationsMerge");
  84     public static final CounterKey COUNTER_MATERIALIZATIONS_UNHANDLED = DebugContext.counter("MaterializationsUnhandled");
  85     public static final CounterKey COUNTER_MATERIALIZATIONS_LOOP_REITERATION = DebugContext.counter("MaterializationsLoopReiteration");
  86     public static final CounterKey COUNTER_MATERIALIZATIONS_LOOP_END = DebugContext.counter("MaterializationsLoopEnd");
  87     public static final CounterKey COUNTER_ALLOCATION_REMOVED = DebugContext.counter("AllocationsRemoved");
  88     public static final CounterKey COUNTER_MEMORYCHECKPOINT = DebugContext.counter("MemoryCheckpoint");
  89 
  90     /**
  91      * Nodes with inputs that were modified during analysis are marked in this bitset - this way
  92      * nodes that are not influenced at all by analysis can be rejected quickly.
  93      */
  94     private final NodeBitMap hasVirtualInputs;
  95 
  96     /**
  97      * This is handed out to implementers of {@link Virtualizable}.
  98      */
  99     protected final VirtualizerToolImpl tool;
 100 
 101     /**
 102      * The indexes into this array correspond to {@link VirtualObjectNode#getObjectId()}.
 103      */
 104     public final ArrayList<VirtualObjectNode> virtualObjects = new ArrayList<>();
 105     public final DebugContext debug;
 106 
 107     @Override
 108     public boolean needsApplyEffects() {
 109         if (hasChanged()) {
 110             return true;
 111         }
 112         /*
 113          * If there is a mismatch between the number of materializations and the number of
 114          * virtualizations, we need to apply effects, even if there were no other significant
 115          * changes to the graph.
 116          */
 117         int delta = 0;
 118         for (Block block : cfg.getBlocks()) {
 119             GraphEffectList effects = blockEffects.get(block);
 120             if (effects != null) {
 121                 delta += effects.getVirtualizationDelta();
 122             }
 123         }
 124         for (Loop<Block> loop : cfg.getLoops()) {
 125             GraphEffectList effects = loopMergeEffects.get(loop);
 126             if (effects != null) {
 127                 delta += effects.getVirtualizationDelta();
 128             }
 129         }
 130         return delta != 0;
 131     }
 132 
 133     private final class CollectVirtualObjectsClosure extends NodeClosure<ValueNode> {
 134         private final EconomicSet<VirtualObjectNode> virtual;
 135         private final GraphEffectList effects;
 136         private final BlockT state;
 137 
 138         private CollectVirtualObjectsClosure(EconomicSet<VirtualObjectNode> virtual, GraphEffectList effects, BlockT state) {
 139             this.virtual = virtual;
 140             this.effects = effects;
 141             this.state = state;
 142         }
 143 
 144         @Override
 145         public void apply(Node usage, ValueNode value) {
 146             if (value instanceof VirtualObjectNode) {
 147                 VirtualObjectNode object = (VirtualObjectNode) value;
 148                 if (object.getObjectId() != -1 && state.getObjectStateOptional(object) != null) {
 149                     virtual.add(object);
 150                 }
 151             } else {
 152                 ValueNode alias = getAlias(value);
 153                 if (alias instanceof VirtualObjectNode) {
 154                     VirtualObjectNode object = (VirtualObjectNode) alias;
 155                     virtual.add(object);
 156                     effects.replaceFirstInput(usage, value, object);
 157                 }
 158             }
 159         }
 160     }
 161 
 162     /**
 163      * Final subclass of PartialEscapeClosure, for performance and to make everything behave nicely
 164      * with generics.
 165      */
 166     public static final class Final extends PartialEscapeClosure<PartialEscapeBlockState.Final> {
 167 
 168         public Final(ScheduleResult schedule, MetaAccessProvider metaAccess, ConstantReflectionProvider constantReflection, ConstantFieldProvider constantFieldProvider,
 169                         LoweringProvider loweringProvider) {
 170             super(schedule, metaAccess, constantReflection, constantFieldProvider, loweringProvider);
 171         }
 172 
 173         @Override
 174         protected PartialEscapeBlockState.Final getInitialState() {
 175             return new PartialEscapeBlockState.Final(tool.getOptions(), tool.getDebug());
 176         }
 177 
 178         @Override
 179         protected PartialEscapeBlockState.Final cloneState(PartialEscapeBlockState.Final oldState) {
 180             return new PartialEscapeBlockState.Final(oldState);
 181         }
 182     }
 183 
 184     public PartialEscapeClosure(ScheduleResult schedule, MetaAccessProvider metaAccess, ConstantReflectionProvider constantReflection, ConstantFieldProvider constantFieldProvider) {
 185         this(schedule, metaAccess, constantReflection, constantFieldProvider, null);
 186     }
 187 
 188     public PartialEscapeClosure(ScheduleResult schedule, MetaAccessProvider metaAccess, ConstantReflectionProvider constantReflection, ConstantFieldProvider constantFieldProvider,
 189                     LoweringProvider loweringProvider) {
 190         super(schedule, schedule.getCFG());
 191         StructuredGraph graph = schedule.getCFG().graph;
 192         this.hasVirtualInputs = graph.createNodeBitMap();
 193         this.debug = graph.getDebug();
 194         this.tool = new VirtualizerToolImpl(metaAccess, constantReflection, constantFieldProvider, this, graph.getAssumptions(), graph.getOptions(), debug, loweringProvider);
 195     }
 196 
 197     /**
 198      * @return true if the node was deleted, false otherwise
 199      */
 200     @Override
 201     protected boolean processNode(Node node, BlockT state, GraphEffectList effects, FixedWithNextNode lastFixedNode) {
 202         /*
 203          * These checks make up for the fact that an earliest schedule moves CallTargetNodes upwards
 204          * and thus materializes virtual objects needlessly. Also, FrameStates and ConstantNodes are
 205          * scheduled, but can safely be ignored.
 206          */
 207         if (node instanceof CallTargetNode || node instanceof FrameState || node instanceof ConstantNode) {
 208             return false;
 209         } else if (node instanceof Invoke) {
 210             processNodeInternal(((Invoke) node).callTarget(), state, effects, lastFixedNode);
 211         }
 212         return processNodeInternal(node, state, effects, lastFixedNode);
 213     }
 214 
 215     private boolean processNodeInternal(Node node, BlockT state, GraphEffectList effects, FixedWithNextNode lastFixedNode) {
 216         FixedNode nextFixedNode = lastFixedNode == null ? null : lastFixedNode.next();
 217         VirtualUtil.trace(node.getOptions(), debug, "%s", node);
 218 
 219         if (requiresProcessing(node)) {
 220             if (processVirtualizable((ValueNode) node, nextFixedNode, state, effects) == false) {
 221                 return false;
 222             }
 223             if (tool.isDeleted()) {
 224                 VirtualUtil.trace(node.getOptions(), debug, "deleted virtualizable allocation %s", node);
 225                 return true;
 226             }
 227         }
 228         if (hasVirtualInputs.isMarked(node) && node instanceof ValueNode) {
 229             if (node instanceof Virtualizable) {
 230                 if (processVirtualizable((ValueNode) node, nextFixedNode, state, effects) == false) {
 231                     return false;
 232                 }
 233                 if (tool.isDeleted()) {
 234                     VirtualUtil.trace(node.getOptions(), debug, "deleted virtualizable node %s", node);
 235                     return true;
 236                 }
 237             }
 238             processNodeInputs((ValueNode) node, nextFixedNode, state, effects);
 239         }
 240 
 241         if (hasScalarReplacedInputs(node) && node instanceof ValueNode) {
 242             if (processNodeWithScalarReplacedInputs((ValueNode) node, nextFixedNode, state, effects)) {
 243                 return true;
 244             }
 245         }
 246         return false;
 247     }
 248 
 249     protected boolean requiresProcessing(Node node) {
 250         return node instanceof VirtualizableAllocation;
 251     }
 252 
 253     private boolean processVirtualizable(ValueNode node, FixedNode insertBefore, BlockT state, GraphEffectList effects) {
 254         tool.reset(state, node, insertBefore, effects);
 255         return virtualize(node, tool);
 256     }
 257 
 258     protected boolean virtualize(ValueNode node, VirtualizerTool vt) {
 259         ((Virtualizable) node).virtualize(vt);
 260         return true; // request further processing
 261     }
 262 
 263     /**
 264      * This tries to canonicalize the node based on improved (replaced) inputs.
 265      */
 266     @SuppressWarnings("unchecked")
 267     private boolean processNodeWithScalarReplacedInputs(ValueNode node, FixedNode insertBefore, BlockT state, GraphEffectList effects) {
 268         ValueNode canonicalizedValue = node;
 269         if (node instanceof Canonicalizable.Unary<?>) {
 270             Canonicalizable.Unary<ValueNode> canonicalizable = (Canonicalizable.Unary<ValueNode>) node;
 271             ObjectState valueObj = getObjectState(state, canonicalizable.getValue());
 272             ValueNode valueAlias = valueObj != null ? valueObj.getMaterializedValue() : getScalarAlias(canonicalizable.getValue());
 273             if (valueAlias != canonicalizable.getValue()) {
 274                 canonicalizedValue = (ValueNode) canonicalizable.canonical(tool, valueAlias);
 275             }
 276         } else if (node instanceof Canonicalizable.Binary<?>) {
 277             Canonicalizable.Binary<ValueNode> canonicalizable = (Canonicalizable.Binary<ValueNode>) node;
 278             ObjectState xObj = getObjectState(state, canonicalizable.getX());
 279             ValueNode xAlias = xObj != null ? xObj.getMaterializedValue() : getScalarAlias(canonicalizable.getX());
 280             ObjectState yObj = getObjectState(state, canonicalizable.getY());
 281             ValueNode yAlias = yObj != null ? yObj.getMaterializedValue() : getScalarAlias(canonicalizable.getY());
 282             if (xAlias != canonicalizable.getX() || yAlias != canonicalizable.getY()) {
 283                 canonicalizedValue = (ValueNode) canonicalizable.canonical(tool, xAlias, yAlias);
 284             }
 285         } else {
 286             return false;
 287         }
 288         if (canonicalizedValue != node && canonicalizedValue != null) {
 289             if (canonicalizedValue.isAlive()) {
 290                 ValueNode alias = getAliasAndResolve(state, canonicalizedValue);
 291                 if (alias instanceof VirtualObjectNode) {
 292                     addVirtualAlias((VirtualObjectNode) alias, node);
 293                     effects.deleteNode(node);
 294                 } else {
 295                     effects.replaceAtUsages(node, alias, insertBefore);
 296                     addScalarAlias(node, alias);
 297                 }
 298             } else {
 299                 if (!prepareCanonicalNode(canonicalizedValue, state, effects)) {
 300                     VirtualUtil.trace(node.getOptions(), debug, "replacement via canonicalization too complex: %s -> %s", node, canonicalizedValue);
 301                     return false;
 302                 }
 303                 if (canonicalizedValue instanceof ControlSinkNode) {
 304                     effects.replaceWithSink((FixedWithNextNode) node, (ControlSinkNode) canonicalizedValue);
 305                     state.markAsDead();
 306                 } else {
 307                     effects.replaceAtUsages(node, canonicalizedValue, insertBefore);
 308                     addScalarAlias(node, canonicalizedValue);
 309                 }
 310             }
 311             VirtualUtil.trace(node.getOptions(), debug, "replaced via canonicalization: %s -> %s", node, canonicalizedValue);
 312             return true;
 313         }
 314         return false;
 315     }
 316 
 317     /**
 318      * Nodes created during canonicalizations need to be scanned for values that were replaced.
 319      */
 320     private boolean prepareCanonicalNode(ValueNode node, BlockT state, GraphEffectList effects) {
 321         assert !node.isAlive();
 322         for (Position pos : node.inputPositions()) {
 323             Node input = pos.get(node);
 324             if (input instanceof ValueNode) {
 325                 if (input.isAlive()) {
 326                     if (!(input instanceof VirtualObjectNode)) {
 327                         ObjectState obj = getObjectState(state, (ValueNode) input);
 328                         if (obj != null) {
 329                             if (obj.isVirtual()) {
 330                                 return false;
 331                             } else {
 332                                 pos.initialize(node, obj.getMaterializedValue());
 333                             }
 334                         } else {
 335                             pos.initialize(node, getScalarAlias((ValueNode) input));
 336                         }
 337                     }
 338                 } else {
 339                     if (!prepareCanonicalNode((ValueNode) input, state, effects)) {
 340                         return false;
 341                     }
 342                 }
 343             }
 344         }
 345         return true;
 346     }
 347 
 348     /**
 349      * This replaces all inputs that point to virtual or materialized values with the actual value,
 350      * materializing if necessary. Also takes care of frame states, adding the necessary
 351      * {@link VirtualObjectState}.
 352      */
 353     private void processNodeInputs(ValueNode node, FixedNode insertBefore, BlockT state, GraphEffectList effects) {
 354         VirtualUtil.trace(node.getOptions(), debug, "processing nodewithstate: %s", node);
 355         for (Node input : node.inputs()) {
 356             if (input instanceof ValueNode) {
 357                 ValueNode alias = getAlias((ValueNode) input);
 358                 if (alias instanceof VirtualObjectNode) {
 359                     int id = ((VirtualObjectNode) alias).getObjectId();
 360                     ensureMaterialized(state, id, insertBefore, effects, COUNTER_MATERIALIZATIONS_UNHANDLED);
 361                     effects.replaceFirstInput(node, input, state.getObjectState(id).getMaterializedValue());
 362                     VirtualUtil.trace(node.getOptions(), debug, "replacing input %s at %s", input, node);
 363                 }
 364             }
 365         }
 366         if (node instanceof NodeWithState) {
 367             processNodeWithState((NodeWithState) node, state, effects);
 368         }
 369     }
 370 
 371     private void processNodeWithState(NodeWithState nodeWithState, BlockT state, GraphEffectList effects) {
 372         for (FrameState fs : nodeWithState.states()) {
 373             FrameState frameState = getUniqueFramestate(nodeWithState, fs);
 374             EconomicSet<VirtualObjectNode> virtual = EconomicSet.create(Equivalence.IDENTITY_WITH_SYSTEM_HASHCODE);
 375             frameState.applyToNonVirtual(new CollectVirtualObjectsClosure(virtual, effects, state));
 376             collectLockedVirtualObjects(state, virtual);
 377             collectReferencedVirtualObjects(state, virtual);
 378             addVirtualMappings(frameState, virtual, state, effects);
 379         }
 380     }
 381 
 382     private static FrameState getUniqueFramestate(NodeWithState nodeWithState, FrameState frameState) {
 383         if (frameState.hasMoreThanOneUsage()) {
 384             // Can happen for example from inlined snippets with multiple state split nodes.
 385             FrameState copy = (FrameState) frameState.copyWithInputs();
 386             nodeWithState.asNode().replaceFirstInput(frameState, copy);
 387             return copy;
 388         }
 389         return frameState;
 390     }
 391 
 392     private void addVirtualMappings(FrameState frameState, EconomicSet<VirtualObjectNode> virtual, BlockT state, GraphEffectList effects) {
 393         for (VirtualObjectNode obj : virtual) {
 394             effects.addVirtualMapping(frameState, state.getObjectState(obj).createEscapeObjectState(debug, obj));
 395         }
 396     }
 397 
 398     private void collectReferencedVirtualObjects(BlockT state, EconomicSet<VirtualObjectNode> virtual) {
 399         Iterator<VirtualObjectNode> iterator = virtual.iterator();
 400         while (iterator.hasNext()) {
 401             VirtualObjectNode object = iterator.next();
 402             int id = object.getObjectId();
 403             if (id != -1) {
 404                 ObjectState objState = state.getObjectStateOptional(id);
 405                 if (objState != null && objState.isVirtual()) {
 406                     for (ValueNode entry : objState.getEntries()) {
 407                         if (entry instanceof VirtualObjectNode) {
 408                             VirtualObjectNode entryVirtual = (VirtualObjectNode) entry;
 409                             if (!virtual.contains(entryVirtual)) {
 410                                 virtual.add(entryVirtual);
 411                             }
 412                         }
 413                     }
 414                 }
 415             }
 416         }
 417     }
 418 
 419     private void collectLockedVirtualObjects(BlockT state, EconomicSet<VirtualObjectNode> virtual) {
 420         for (int i = 0; i < state.getStateCount(); i++) {
 421             ObjectState objState = state.getObjectStateOptional(i);
 422             if (objState != null && objState.isVirtual() && objState.hasLocks()) {
 423                 virtual.add(virtualObjects.get(i));
 424             }
 425         }
 426     }
 427 
 428     /**
 429      * @return true if materialization happened, false if not.
 430      */
 431     protected boolean ensureMaterialized(PartialEscapeBlockState<?> state, int object, FixedNode materializeBefore, GraphEffectList effects, CounterKey counter) {
 432         if (state.getObjectState(object).isVirtual()) {
 433             counter.increment(debug);
 434             VirtualObjectNode virtual = virtualObjects.get(object);
 435             state.materializeBefore(materializeBefore, virtual, effects);
 436             assert !updateStatesForMaterialized(state, virtual, state.getObjectState(object).getMaterializedValue()) : "method must already have been called before";
 437             return true;
 438         } else {
 439             return false;
 440         }
 441     }
 442 
 443     public static boolean updateStatesForMaterialized(PartialEscapeBlockState<?> state, VirtualObjectNode virtual, ValueNode materializedValue) {
 444         // update all existing states with the newly materialized object
 445         boolean change = false;
 446         for (int i = 0; i < state.getStateCount(); i++) {
 447             ObjectState objState = state.getObjectStateOptional(i);
 448             if (objState != null && objState.isVirtual()) {
 449                 ValueNode[] entries = objState.getEntries();
 450                 for (int i2 = 0; i2 < entries.length; i2++) {
 451                     if (entries[i2] == virtual) {
 452                         state.setEntry(i, i2, materializedValue);
 453                         change = true;
 454                     }
 455                 }
 456             }
 457         }
 458         return change;
 459     }
 460 
 461     @Override
 462     protected BlockT stripKilledLoopLocations(Loop<Block> loop, BlockT originalInitialState) {
 463         BlockT initialState = super.stripKilledLoopLocations(loop, originalInitialState);
 464         if (loop.getDepth() > GraalOptions.EscapeAnalysisLoopCutoff.getValue(cfg.graph.getOptions())) {
 465             /*
 466              * After we've reached the maximum loop nesting, we'll simply materialize everything we
 467              * can to make sure that the loops only need to be iterated one time. Care is taken here
 468              * to not materialize virtual objects that have the "ensureVirtualized" flag set.
 469              */
 470             LoopBeginNode loopBegin = (LoopBeginNode) loop.getHeader().getBeginNode();
 471             AbstractEndNode end = loopBegin.forwardEnd();
 472             Block loopPredecessor = loop.getHeader().getFirstPredecessor();
 473             assert loopPredecessor.getEndNode() == end;
 474             int length = initialState.getStateCount();
 475 
 476             boolean change;
 477             BitSet ensureVirtualized = new BitSet(length);
 478             for (int i = 0; i < length; i++) {
 479                 ObjectState state = initialState.getObjectStateOptional(i);
 480                 if (state != null && state.isVirtual() && state.getEnsureVirtualized()) {
 481                     ensureVirtualized.set(i);
 482                 }
 483             }
 484             do {
 485                 // propagate "ensureVirtualized" flag
 486                 change = false;
 487                 for (int i = 0; i < length; i++) {
 488                     if (!ensureVirtualized.get(i)) {
 489                         ObjectState state = initialState.getObjectStateOptional(i);
 490                         if (state != null && state.isVirtual()) {
 491                             for (ValueNode entry : state.getEntries()) {
 492                                 if (entry instanceof VirtualObjectNode) {
 493                                     if (ensureVirtualized.get(((VirtualObjectNode) entry).getObjectId())) {
 494                                         change = true;
 495                                         ensureVirtualized.set(i);
 496                                         break;
 497                                     }
 498                                 }
 499                             }
 500                         }
 501                     }
 502                 }
 503             } while (change);
 504 
 505             for (int i = 0; i < length; i++) {
 506                 ObjectState state = initialState.getObjectStateOptional(i);
 507                 if (state != null && state.isVirtual() && !ensureVirtualized.get(i)) {
 508                     initialState.materializeBefore(end, virtualObjects.get(i), blockEffects.get(loopPredecessor));
 509                 }
 510             }
 511         }
 512         return initialState;
 513     }
 514 
 515     @Override
 516     protected void processInitialLoopState(Loop<Block> loop, BlockT initialState) {
 517         for (PhiNode phi : ((LoopBeginNode) loop.getHeader().getBeginNode()).phis()) {
 518             if (phi.valueAt(0) != null) {
 519                 ValueNode alias = getAliasAndResolve(initialState, phi.valueAt(0));
 520                 if (alias instanceof VirtualObjectNode) {
 521                     VirtualObjectNode virtual = (VirtualObjectNode) alias;
 522                     addVirtualAlias(virtual, phi);
 523                 } else {
 524                     aliases.set(phi, null);
 525                 }
 526             }
 527         }
 528     }
 529 
 530     @Override
 531     protected void processLoopExit(LoopExitNode exitNode, BlockT initialState, BlockT exitState, GraphEffectList effects) {
 532         if (exitNode.graph().hasValueProxies()) {
 533             EconomicMap<Integer, ProxyNode> proxies = EconomicMap.create(Equivalence.DEFAULT);
 534             for (ProxyNode proxy : exitNode.proxies()) {
 535                 ValueNode alias = getAlias(proxy.value());
 536                 if (alias instanceof VirtualObjectNode) {
 537                     VirtualObjectNode virtual = (VirtualObjectNode) alias;
 538                     proxies.put(virtual.getObjectId(), proxy);
 539                 }
 540             }
 541             for (int i = 0; i < exitState.getStateCount(); i++) {
 542                 ObjectState exitObjState = exitState.getObjectStateOptional(i);
 543                 if (exitObjState != null) {
 544                     ObjectState initialObjState = initialState.getObjectStateOptional(i);
 545 
 546                     if (exitObjState.isVirtual()) {
 547                         processVirtualAtLoopExit(exitNode, effects, i, exitObjState, initialObjState, exitState);
 548                     } else {
 549                         processMaterializedAtLoopExit(exitNode, effects, proxies, i, exitObjState, initialObjState, exitState);
 550                     }
 551                 }
 552             }
 553         }
 554     }
 555 
 556     private static void processMaterializedAtLoopExit(LoopExitNode exitNode, GraphEffectList effects, EconomicMap<Integer, ProxyNode> proxies, int object, ObjectState exitObjState,
 557                     ObjectState initialObjState, PartialEscapeBlockState<?> exitState) {
 558         if (initialObjState == null || initialObjState.isVirtual()) {
 559             ProxyNode proxy = proxies.get(object);
 560             if (proxy == null) {
 561                 proxy = new ValueProxyNode(exitObjState.getMaterializedValue(), exitNode);
 562                 effects.addFloatingNode(proxy, "proxy");
 563             } else {
 564                 effects.replaceFirstInput(proxy, proxy.value(), exitObjState.getMaterializedValue());
 565                 // nothing to do - will be handled in processNode
 566             }
 567             exitState.updateMaterializedValue(object, proxy);
 568         } else {
 569             if (initialObjState.getMaterializedValue() != exitObjState.getMaterializedValue()) {
 570                 exitNode.getDebug().log("materialized value changes within loop: %s vs. %s at %s", initialObjState.getMaterializedValue(), exitObjState.getMaterializedValue(), exitNode);
 571             }
 572         }
 573     }
 574 
 575     private static void processVirtualAtLoopExit(LoopExitNode exitNode, GraphEffectList effects, int object, ObjectState exitObjState, ObjectState initialObjState,
 576                     PartialEscapeBlockState<?> exitState) {
 577         for (int i = 0; i < exitObjState.getEntries().length; i++) {
 578             ValueNode value = exitState.getObjectState(object).getEntry(i);
 579             if (!(value instanceof VirtualObjectNode || value.isConstant())) {
 580                 if (exitNode.loopBegin().isPhiAtMerge(value) || initialObjState == null || !initialObjState.isVirtual() || initialObjState.getEntry(i) != value) {
 581                     ProxyNode proxy = new ValueProxyNode(value, exitNode);
 582                     exitState.setEntry(object, i, proxy);
 583                     effects.addFloatingNode(proxy, "virtualProxy");
 584                 }
 585             }
 586         }
 587     }
 588 
 589     @Override
 590     protected MergeProcessor createMergeProcessor(Block merge) {
 591         return new MergeProcessor(merge);
 592     }
 593 
 594     protected class MergeProcessor extends EffectsClosure<BlockT>.MergeProcessor {
 595 
 596         private EconomicMap<Object, ValuePhiNode> materializedPhis;
 597         private EconomicMap<ValueNode, ValuePhiNode[]> valuePhis;
 598         private EconomicMap<ValuePhiNode, VirtualObjectNode> valueObjectVirtuals;
 599         private final boolean needsCaching;
 600 
 601         public MergeProcessor(Block mergeBlock) {
 602             super(mergeBlock);
 603             // merge will only be called multiple times for loop headers
 604             needsCaching = mergeBlock.isLoopHeader();
 605         }
 606 
 607         protected <T> PhiNode getPhi(T virtual, Stamp stamp) {
 608             if (needsCaching) {
 609                 return getPhiCached(virtual, stamp);
 610             } else {
 611                 return createValuePhi(stamp);
 612             }
 613         }
 614 
 615         private <T> PhiNode getPhiCached(T virtual, Stamp stamp) {
 616             if (materializedPhis == null) {
 617                 materializedPhis = EconomicMap.create(Equivalence.DEFAULT);
 618             }
 619             ValuePhiNode result = materializedPhis.get(virtual);
 620             if (result == null) {
 621                 result = createValuePhi(stamp);
 622                 materializedPhis.put(virtual, result);
 623             }
 624             return result;
 625         }
 626 
 627         private PhiNode[] getValuePhis(ValueNode key, int entryCount) {
 628             if (needsCaching) {
 629                 return getValuePhisCached(key, entryCount);
 630             } else {
 631                 return new ValuePhiNode[entryCount];
 632             }
 633         }
 634 
 635         private PhiNode[] getValuePhisCached(ValueNode key, int entryCount) {
 636             if (valuePhis == null) {
 637                 valuePhis = EconomicMap.create(Equivalence.IDENTITY_WITH_SYSTEM_HASHCODE);
 638             }
 639             ValuePhiNode[] result = valuePhis.get(key);
 640             if (result == null) {
 641                 result = new ValuePhiNode[entryCount];
 642                 valuePhis.put(key, result);
 643             }
 644             assert result.length == entryCount;
 645             return result;
 646         }
 647 
 648         private VirtualObjectNode getValueObjectVirtual(ValuePhiNode phi, VirtualObjectNode virtual) {
 649             if (needsCaching) {
 650                 return getValueObjectVirtualCached(phi, virtual);
 651             } else {
 652                 return virtual.duplicate();
 653             }
 654         }
 655 
 656         private VirtualObjectNode getValueObjectVirtualCached(ValuePhiNode phi, VirtualObjectNode virtual) {
 657             if (valueObjectVirtuals == null) {
 658                 valueObjectVirtuals = EconomicMap.create(Equivalence.IDENTITY);
 659             }
 660             VirtualObjectNode result = valueObjectVirtuals.get(phi);
 661             if (result == null) {
 662                 result = virtual.duplicate();
 663                 valueObjectVirtuals.put(phi, result);
 664             }
 665             return result;
 666         }
 667 
 668         /**
 669          * Merge all predecessor block states into one block state. This is an iterative process,
 670          * because merging states can lead to materializations which make previous parts of the
 671          * merging operation invalid. The merging process is executed until a stable state has been
 672          * reached. This method needs to be careful to place the effects of the merging operation
 673          * into the correct blocks.
 674          *
 675          * @param statesList the predecessor block states of the merge
 676          */
 677         @Override
 678         protected void merge(List<BlockT> statesList) {
 679 
 680             PartialEscapeBlockState<?>[] states = new PartialEscapeBlockState<?>[statesList.size()];
 681             for (int i = 0; i < statesList.size(); i++) {
 682                 states[i] = statesList.get(i);
 683             }
 684 
 685             // calculate the set of virtual objects that exist in all predecessors
 686             int[] virtualObjTemp = intersectVirtualObjects(states);
 687 
 688             boolean materialized;
 689             do {
 690                 materialized = false;
 691 
 692                 if (PartialEscapeBlockState.identicalObjectStates(states)) {
 693                     newState.adoptAddObjectStates(states[0]);
 694                 } else {
 695 
 696                     for (int object : virtualObjTemp) {
 697                         if (PartialEscapeBlockState.identicalObjectStates(states, object)) {
 698                             newState.addObject(object, states[0].getObjectState(object).share());
 699                             continue;
 700                         }
 701 
 702                         // determine if all inputs are virtual or the same materialized value
 703                         int virtualCount = 0;
 704                         ObjectState startObj = states[0].getObjectState(object);
 705                         boolean locksMatch = true;
 706                         boolean ensureVirtual = true;
 707                         ValueNode uniqueMaterializedValue = startObj.isVirtual() ? null : startObj.getMaterializedValue();
 708                         for (int i = 0; i < states.length; i++) {
 709                             ObjectState obj = states[i].getObjectState(object);
 710                             ensureVirtual &= obj.getEnsureVirtualized();
 711                             if (obj.isVirtual()) {
 712                                 virtualCount++;
 713                                 uniqueMaterializedValue = null;
 714                                 locksMatch &= obj.locksEqual(startObj);
 715                             } else if (obj.getMaterializedValue() != uniqueMaterializedValue) {
 716                                 uniqueMaterializedValue = null;
 717                             }
 718                         }
 719 
 720                         if (virtualCount == states.length && locksMatch) {
 721                             materialized |= mergeObjectStates(object, null, states);
 722                         } else {
 723                             if (uniqueMaterializedValue != null) {
 724                                 newState.addObject(object, new ObjectState(uniqueMaterializedValue, null, ensureVirtual));
 725                             } else {
 726                                 PhiNode materializedValuePhi = getPhi(object, StampFactory.forKind(JavaKind.Object));
 727                                 mergeEffects.addFloatingNode(materializedValuePhi, "materializedPhi");
 728                                 for (int i = 0; i < states.length; i++) {
 729                                     ObjectState obj = states[i].getObjectState(object);
 730                                     if (obj.isVirtual()) {
 731                                         Block predecessor = getPredecessor(i);
 732                                         if (!ensureVirtual && obj.isVirtual()) {
 733                                             // we can materialize if not all inputs are
 734                                             // "ensureVirtualized"
 735                                             obj.setEnsureVirtualized(false);
 736                                         }
 737                                         materialized |= ensureMaterialized(states[i], object, predecessor.getEndNode(), blockEffects.get(predecessor), COUNTER_MATERIALIZATIONS_MERGE);
 738                                         obj = states[i].getObjectState(object);
 739                                     }
 740                                     setPhiInput(materializedValuePhi, i, obj.getMaterializedValue());
 741                                 }
 742                                 newState.addObject(object, new ObjectState(materializedValuePhi, null, false));
 743                             }
 744                         }
 745                     }
 746                 }
 747 
 748                 for (PhiNode phi : getPhis()) {
 749                     aliases.set(phi, null);
 750                     if (hasVirtualInputs.isMarked(phi) && phi instanceof ValuePhiNode) {
 751                         materialized |= processPhi((ValuePhiNode) phi, states);
 752                     }
 753                 }
 754                 if (materialized) {
 755                     newState.resetObjectStates(virtualObjects.size());
 756                     mergeEffects.clear();
 757                     afterMergeEffects.clear();
 758                 }
 759             } while (materialized);
 760         }
 761 
 762         private int[] intersectVirtualObjects(PartialEscapeBlockState<?>[] states) {
 763             int length = states[0].getStateCount();
 764             for (int i = 1; i < states.length; i++) {
 765                 length = Math.min(length, states[i].getStateCount());
 766             }
 767 
 768             int count = 0;
 769             for (int objectIndex = 0; objectIndex < length; objectIndex++) {
 770                 if (intersectObjectState(states, objectIndex)) {
 771                     count++;
 772                 }
 773             }
 774 
 775             int index = 0;
 776             int[] resultInts = new int[count];
 777             for (int objectIndex = 0; objectIndex < length; objectIndex++) {
 778                 if (intersectObjectState(states, objectIndex)) {
 779                     resultInts[index++] = objectIndex;
 780                 }
 781             }
 782             assert index == count;
 783             return resultInts;
 784         }
 785 
 786         private boolean intersectObjectState(PartialEscapeBlockState<?>[] states, int objectIndex) {
 787             for (int i = 0; i < states.length; i++) {
 788                 PartialEscapeBlockState<?> state = states[i];
 789                 if (state.getObjectStateOptional(objectIndex) == null) {
 790                     return false;
 791                 }
 792             }
 793             return true;
 794         }
 795 
 796         /**
 797          * Try to merge multiple virtual object states into a single object state. If the incoming
 798          * object states are compatible, then this method will create PhiNodes for the object's
 799          * entries where needed. If they are incompatible, then all incoming virtual objects will be
 800          * materialized, and a PhiNode for the materialized values will be created. Object states
 801          * can be incompatible if they contain {@code long} or {@code double} values occupying two
 802          * {@code int} slots in such a way that that their values cannot be merged using PhiNodes.
 803          *
 804          * @param states the predecessor block states of the merge
 805          * @return true if materialization happened during the merge, false otherwise
 806          */
 807         private boolean mergeObjectStates(int resultObject, int[] sourceObjects, PartialEscapeBlockState<?>[] states) {
 808             boolean compatible = true;
 809             boolean ensureVirtual = true;
 810             IntUnaryOperator getObject = index -> sourceObjects == null ? resultObject : sourceObjects[index];
 811 
 812             VirtualObjectNode virtual = virtualObjects.get(resultObject);
 813             int entryCount = virtual.entryCount();
 814 
 815             // determine all entries that have a two-slot value
 816             JavaKind[] twoSlotKinds = null;
 817             outer: for (int i = 0; i < states.length; i++) {
 818                 ObjectState objectState = states[i].getObjectState(getObject.applyAsInt(i));
 819                 ValueNode[] entries = objectState.getEntries();
 820                 int valueIndex = 0;
 821                 ensureVirtual &= objectState.getEnsureVirtualized();
 822                 while (valueIndex < entryCount) {
 823                     JavaKind otherKind = entries[valueIndex].getStackKind();
 824                     JavaKind entryKind = virtual.entryKind(valueIndex);
 825                     if (entryKind == JavaKind.Int && otherKind.needsTwoSlots()) {
 826                         if (twoSlotKinds == null) {
 827                             twoSlotKinds = new JavaKind[entryCount];
 828                         }
 829                         if (twoSlotKinds[valueIndex] != null && twoSlotKinds[valueIndex] != otherKind) {
 830                             compatible = false;
 831                             break outer;
 832                         }
 833                         twoSlotKinds[valueIndex] = otherKind;
 834                         // skip the next entry
 835                         valueIndex++;
 836                     } else {
 837                         assert entryKind.getStackKind() == otherKind.getStackKind() || (entryKind == JavaKind.Int && otherKind == JavaKind.Illegal) ||
 838                                         entryKind.getBitCount() >= otherKind.getBitCount() : entryKind + " vs " + otherKind;
 839                     }
 840                     valueIndex++;
 841                 }
 842             }
 843             if (compatible && twoSlotKinds != null) {
 844                 // if there are two-slot values then make sure the incoming states can be merged
 845                 outer: for (int valueIndex = 0; valueIndex < entryCount; valueIndex++) {
 846                     if (twoSlotKinds[valueIndex] != null) {
 847                         assert valueIndex < virtual.entryCount() - 1 && virtual.entryKind(valueIndex) == JavaKind.Int && virtual.entryKind(valueIndex + 1) == JavaKind.Int;
 848                         for (int i = 0; i < states.length; i++) {
 849                             int object = getObject.applyAsInt(i);
 850                             ObjectState objectState = states[i].getObjectState(object);
 851                             ValueNode value = objectState.getEntry(valueIndex);
 852                             JavaKind valueKind = value.getStackKind();
 853                             if (valueKind != twoSlotKinds[valueIndex]) {
 854                                 ValueNode nextValue = objectState.getEntry(valueIndex + 1);
 855                                 if (value.isConstant() && value.asConstant().equals(JavaConstant.INT_0) && nextValue.isConstant() && nextValue.asConstant().equals(JavaConstant.INT_0)) {
 856                                     // rewrite to a zero constant of the larger kind
 857                                     debug.log("Rewriting entry %s to constant of larger size", valueIndex);
 858                                     states[i].setEntry(object, valueIndex, ConstantNode.defaultForKind(twoSlotKinds[valueIndex], graph()));
 859                                     states[i].setEntry(object, valueIndex + 1, ConstantNode.forConstant(JavaConstant.forIllegal(), tool.getMetaAccessProvider(), graph()));
 860                                 } else {
 861                                     compatible = false;
 862                                     break outer;
 863                                 }
 864                             }
 865                         }
 866                     }
 867                 }
 868             }
 869 
 870             if (compatible) {
 871                 // virtual objects are compatible: create phis for all entries that need them
 872                 ValueNode[] values = states[0].getObjectState(getObject.applyAsInt(0)).getEntries().clone();
 873                 PhiNode[] phis = getValuePhis(virtual, virtual.entryCount());
 874                 int valueIndex = 0;
 875                 while (valueIndex < values.length) {
 876                     for (int i = 1; i < states.length; i++) {
 877                         if (phis[valueIndex] == null) {
 878                             ValueNode field = states[i].getObjectState(getObject.applyAsInt(i)).getEntry(valueIndex);
 879                             if (values[valueIndex] != field) {
 880                                 phis[valueIndex] = createValuePhi(values[valueIndex].stamp().unrestricted());
 881                             }
 882                         }
 883                     }
 884                     if (phis[valueIndex] != null && !phis[valueIndex].stamp().isCompatible(values[valueIndex].stamp())) {
 885                         phis[valueIndex] = createValuePhi(values[valueIndex].stamp().unrestricted());
 886                     }
 887                     if (twoSlotKinds != null && twoSlotKinds[valueIndex] != null) {
 888                         // skip an entry after a long/double value that occupies two int slots
 889                         valueIndex++;
 890                         phis[valueIndex] = null;
 891                         values[valueIndex] = ConstantNode.forConstant(JavaConstant.forIllegal(), tool.getMetaAccessProvider(), graph());
 892                     }
 893                     valueIndex++;
 894                 }
 895 
 896                 boolean materialized = false;
 897                 for (int i = 0; i < values.length; i++) {
 898                     PhiNode phi = phis[i];
 899                     if (phi != null) {
 900                         mergeEffects.addFloatingNode(phi, "virtualMergePhi");
 901                         if (virtual.entryKind(i) == JavaKind.Object) {
 902                             materialized |= mergeObjectEntry(getObject, states, phi, i);
 903                         } else {
 904                             for (int i2 = 0; i2 < states.length; i2++) {
 905                                 ObjectState state = states[i2].getObjectState(getObject.applyAsInt(i2));
 906                                 if (!state.isVirtual()) {
 907                                     break;
 908                                 }
 909                                 setPhiInput(phi, i2, state.getEntry(i));
 910                             }
 911                         }
 912                         values[i] = phi;
 913                     }
 914                 }
 915                 newState.addObject(resultObject, new ObjectState(values, states[0].getObjectState(getObject.applyAsInt(0)).getLocks(), ensureVirtual));
 916                 return materialized;
 917             } else {
 918                 // not compatible: materialize in all predecessors
 919                 PhiNode materializedValuePhi = getPhi(resultObject, StampFactory.forKind(JavaKind.Object));
 920                 for (int i = 0; i < states.length; i++) {
 921                     Block predecessor = getPredecessor(i);
 922                     if (!ensureVirtual && states[i].getObjectState(getObject.applyAsInt(i)).isVirtual()) {
 923                         // we can materialize if not all inputs are "ensureVirtualized"
 924                         states[i].getObjectState(getObject.applyAsInt(i)).setEnsureVirtualized(false);
 925                     }
 926                     ensureMaterialized(states[i], getObject.applyAsInt(i), predecessor.getEndNode(), blockEffects.get(predecessor), COUNTER_MATERIALIZATIONS_MERGE);
 927                     setPhiInput(materializedValuePhi, i, states[i].getObjectState(getObject.applyAsInt(i)).getMaterializedValue());
 928                 }
 929                 newState.addObject(resultObject, new ObjectState(materializedValuePhi, null, ensureVirtual));
 930                 return true;
 931             }
 932         }
 933 
 934         /**
 935          * Fill the inputs of the PhiNode corresponding to one {@link JavaKind#Object} entry in the
 936          * virtual object.
 937          *
 938          * @return true if materialization happened during the merge, false otherwise
 939          */
 940         private boolean mergeObjectEntry(IntUnaryOperator objectIdFunc, PartialEscapeBlockState<?>[] states, PhiNode phi, int entryIndex) {
 941             boolean materialized = false;
 942             for (int i = 0; i < states.length; i++) {
 943                 int object = objectIdFunc.applyAsInt(i);
 944                 ObjectState objectState = states[i].getObjectState(object);
 945                 if (!objectState.isVirtual()) {
 946                     break;
 947                 }
 948                 ValueNode entry = objectState.getEntry(entryIndex);
 949                 if (entry instanceof VirtualObjectNode) {
 950                     VirtualObjectNode entryVirtual = (VirtualObjectNode) entry;
 951                     Block predecessor = getPredecessor(i);
 952                     materialized |= ensureMaterialized(states[i], entryVirtual.getObjectId(), predecessor.getEndNode(), blockEffects.get(predecessor), COUNTER_MATERIALIZATIONS_MERGE);
 953                     objectState = states[i].getObjectState(object);
 954                     if (objectState.isVirtual()) {
 955                         states[i].setEntry(object, entryIndex, entry = states[i].getObjectState(entryVirtual.getObjectId()).getMaterializedValue());
 956                     }
 957                 }
 958                 setPhiInput(phi, i, entry);
 959             }
 960             return materialized;
 961         }
 962 
 963         /**
 964          * Examine a PhiNode and try to replace it with merging of virtual objects if all its inputs
 965          * refer to virtual object states. In order for the merging to happen, all incoming object
 966          * states need to be compatible and without object identity (meaning that their object
 967          * identity if not used later on).
 968          *
 969          * @param phi the PhiNode that should be processed
 970          * @param states the predecessor block states of the merge
 971          * @return true if materialization happened during the merge, false otherwise
 972          */
 973         private boolean processPhi(ValuePhiNode phi, PartialEscapeBlockState<?>[] states) {
 974 
 975             // determine how many inputs are virtual and if they're all the same virtual object
 976             int virtualInputs = 0;
 977             boolean uniqueVirtualObject = true;
 978             boolean ensureVirtual = true;
 979             VirtualObjectNode[] virtualObjs = new VirtualObjectNode[states.length];
 980             for (int i = 0; i < states.length; i++) {
 981                 ValueNode alias = getAlias(getPhiValueAt(phi, i));
 982                 if (alias instanceof VirtualObjectNode) {
 983                     VirtualObjectNode virtual = (VirtualObjectNode) alias;
 984                     virtualObjs[i] = virtual;
 985                     ObjectState objectState = states[i].getObjectStateOptional(virtual);
 986                     if (objectState == null) {
 987                         assert getPhiValueAt(phi, i) instanceof PhiNode : "this should only happen for phi nodes";
 988                         return false;
 989                     }
 990                     if (objectState.isVirtual()) {
 991                         if (virtualObjs[0] != alias) {
 992                             uniqueVirtualObject = false;
 993                         }
 994                         ensureVirtual &= objectState.getEnsureVirtualized();
 995                         virtualInputs++;
 996                     }
 997                 }
 998             }
 999             if (virtualInputs == states.length) {
1000                 if (uniqueVirtualObject) {
1001                     // all inputs refer to the same object: just make the phi node an alias
1002                     addVirtualAlias(virtualObjs[0], phi);
1003                     mergeEffects.deleteNode(phi);
1004                     return false;
1005                 } else {
1006                     // all inputs are virtual: check if they're compatible and without identity
1007                     boolean compatible = true;
1008                     VirtualObjectNode firstVirtual = virtualObjs[0];
1009                     for (int i = 0; i < states.length; i++) {
1010                         VirtualObjectNode virtual = virtualObjs[i];
1011 
1012                         if (!firstVirtual.type().equals(virtual.type()) || firstVirtual.entryCount() != virtual.entryCount()) {
1013                             compatible = false;
1014                             break;
1015                         }
1016                         if (!states[0].getObjectState(firstVirtual).locksEqual(states[i].getObjectState(virtual))) {
1017                             compatible = false;
1018                             break;
1019                         }
1020                     }
1021                     if (compatible) {
1022                         for (int i = 0; i < states.length; i++) {
1023                             VirtualObjectNode virtual = virtualObjs[i];
1024                             /*
1025                              * check whether we trivially see that this is the only reference to
1026                              * this allocation
1027                              */
1028                             if (virtual.hasIdentity() && !isSingleUsageAllocation(getPhiValueAt(phi, i), virtualObjs, states[i])) {
1029                                 compatible = false;
1030                             }
1031                         }
1032                     }
1033                     if (compatible) {
1034                         VirtualObjectNode virtual = getValueObjectVirtual(phi, virtualObjs[0]);
1035                         mergeEffects.addFloatingNode(virtual, "valueObjectNode");
1036                         mergeEffects.deleteNode(phi);
1037                         if (virtual.getObjectId() == -1) {
1038                             int id = virtualObjects.size();
1039                             virtualObjects.add(virtual);
1040                             virtual.setObjectId(id);
1041                         }
1042 
1043                         int[] virtualObjectIds = new int[states.length];
1044                         for (int i = 0; i < states.length; i++) {
1045                             virtualObjectIds[i] = virtualObjs[i].getObjectId();
1046                         }
1047                         boolean materialized = mergeObjectStates(virtual.getObjectId(), virtualObjectIds, states);
1048                         addVirtualAlias(virtual, virtual);
1049                         addVirtualAlias(virtual, phi);
1050                         return materialized;
1051                     }
1052                 }
1053             }
1054 
1055             // otherwise: materialize all phi inputs
1056             boolean materialized = false;
1057             if (virtualInputs > 0) {
1058                 for (int i = 0; i < states.length; i++) {
1059                     VirtualObjectNode virtual = virtualObjs[i];
1060                     if (virtual != null) {
1061                         Block predecessor = getPredecessor(i);
1062                         if (!ensureVirtual && states[i].getObjectState(virtual).isVirtual()) {
1063                             // we can materialize if not all inputs are "ensureVirtualized"
1064                             states[i].getObjectState(virtual).setEnsureVirtualized(false);
1065                         }
1066                         materialized |= ensureMaterialized(states[i], virtual.getObjectId(), predecessor.getEndNode(), blockEffects.get(predecessor), COUNTER_MATERIALIZATIONS_PHI);
1067                     }
1068                 }
1069             }
1070             for (int i = 0; i < states.length; i++) {
1071                 VirtualObjectNode virtual = virtualObjs[i];
1072                 if (virtual != null) {
1073                     setPhiInput(phi, i, getAliasAndResolve(states[i], virtual));
1074                 }
1075             }
1076             return materialized;
1077         }
1078 
1079         private boolean isSingleUsageAllocation(ValueNode value, VirtualObjectNode[] virtualObjs, PartialEscapeBlockState<?> state) {
1080             /*
1081              * If the phi input is an allocation, we know that it is a "fresh" value, i.e., that
1082              * this is a value that will only appear through this source, and cannot appear anywhere
1083              * else. If the phi is also the only usage of this input, we know that no other place
1084              * can check object identity against it, so it is safe to lose the object identity here.
1085              */
1086             if (!(value instanceof AllocatedObjectNode && value.hasExactlyOneUsage())) {
1087                 return false;
1088             }
1089 
1090             /*
1091              * Check that the state only references the one virtual object from the Phi.
1092              */
1093             VirtualObjectNode singleVirtual = null;
1094             for (int v = 0; v < virtualObjs.length; v++) {
1095                 if (state.contains(virtualObjs[v])) {
1096                     if (singleVirtual == null) {
1097                         singleVirtual = virtualObjs[v];
1098                     } else if (singleVirtual != virtualObjs[v]) {
1099                         /*
1100                          * More than one virtual object is visible in the object state.
1101                          */
1102                         return false;
1103                     }
1104                 }
1105             }
1106             return true;
1107         }
1108     }
1109 
1110     public ObjectState getObjectState(PartialEscapeBlockState<?> state, ValueNode value) {
1111         if (value == null) {
1112             return null;
1113         }
1114         if (value.isAlive() && !aliases.isNew(value)) {
1115             ValueNode object = aliases.get(value);
1116             return object instanceof VirtualObjectNode ? state.getObjectStateOptional((VirtualObjectNode) object) : null;
1117         } else {
1118             if (value instanceof VirtualObjectNode) {
1119                 return state.getObjectStateOptional((VirtualObjectNode) value);
1120             }
1121             return null;
1122         }
1123     }
1124 
1125     public ValueNode getAlias(ValueNode value) {
1126         if (value != null && !(value instanceof VirtualObjectNode)) {
1127             if (value.isAlive() && !aliases.isNew(value)) {
1128                 ValueNode result = aliases.get(value);
1129                 if (result != null) {
1130                     return result;
1131                 }
1132             }
1133         }
1134         return value;
1135     }
1136 
1137     public ValueNode getAliasAndResolve(PartialEscapeBlockState<?> state, ValueNode value) {
1138         ValueNode result = getAlias(value);
1139         if (result instanceof VirtualObjectNode) {
1140             int id = ((VirtualObjectNode) result).getObjectId();
1141             if (id != -1 && !state.getObjectState(id).isVirtual()) {
1142                 result = state.getObjectState(id).getMaterializedValue();
1143             }
1144         }
1145         return result;
1146     }
1147 
1148     void addVirtualAlias(VirtualObjectNode virtual, ValueNode node) {
1149         if (node.isAlive()) {
1150             aliases.set(node, virtual);
1151             for (Node usage : node.usages()) {
1152                 markVirtualUsages(usage);
1153             }
1154         }
1155     }
1156 
1157     private void markVirtualUsages(Node node) {
1158         if (!hasVirtualInputs.isNew(node) && !hasVirtualInputs.isMarked(node)) {
1159             hasVirtualInputs.mark(node);
1160             if (node instanceof VirtualState) {
1161                 for (Node usage : node.usages()) {
1162                     markVirtualUsages(usage);
1163                 }
1164             }
1165         }
1166     }
1167 }