1 /*
   2  * Copyright (c) 2014, 2016, 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.replacements;
  24 
  25 import static org.graalvm.compiler.nodes.graphbuilderconf.IntrinsicContext.CompilationContext.INLINE_AFTER_PARSING;
  26 
  27 import java.lang.reflect.Method;
  28 import java.lang.reflect.Modifier;
  29 import java.util.ArrayList;
  30 import java.util.List;
  31 
  32 import org.graalvm.compiler.core.common.spi.ConstantFieldProvider;
  33 import org.graalvm.compiler.core.common.type.StampFactory;
  34 import org.graalvm.compiler.core.common.type.StampPair;
  35 import org.graalvm.compiler.debug.GraalError;
  36 import org.graalvm.compiler.graph.Graph;
  37 import org.graalvm.compiler.graph.Node.ValueNumberable;
  38 import org.graalvm.compiler.java.FrameStateBuilder;
  39 import org.graalvm.compiler.java.GraphBuilderPhase;
  40 import org.graalvm.compiler.nodes.AbstractBeginNode;
  41 import org.graalvm.compiler.nodes.AbstractMergeNode;
  42 import org.graalvm.compiler.nodes.BeginNode;
  43 import org.graalvm.compiler.nodes.CallTargetNode.InvokeKind;
  44 import org.graalvm.compiler.nodes.EndNode;
  45 import org.graalvm.compiler.nodes.FixedNode;
  46 import org.graalvm.compiler.nodes.FixedWithNextNode;
  47 import org.graalvm.compiler.nodes.IfNode;
  48 import org.graalvm.compiler.nodes.InvokeNode;
  49 import org.graalvm.compiler.nodes.InvokeWithExceptionNode;
  50 import org.graalvm.compiler.nodes.KillingBeginNode;
  51 import org.graalvm.compiler.nodes.LogicNode;
  52 import org.graalvm.compiler.nodes.MergeNode;
  53 import org.graalvm.compiler.nodes.StructuredGraph;
  54 import org.graalvm.compiler.nodes.ValueNode;
  55 import org.graalvm.compiler.nodes.calc.FloatingNode;
  56 import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderConfiguration;
  57 import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderConfiguration.Plugins;
  58 import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderTool;
  59 import org.graalvm.compiler.nodes.graphbuilderconf.IntrinsicContext;
  60 import org.graalvm.compiler.nodes.java.ExceptionObjectNode;
  61 import org.graalvm.compiler.nodes.java.MethodCallTargetNode;
  62 import org.graalvm.compiler.nodes.spi.StampProvider;
  63 import org.graalvm.compiler.nodes.type.StampTool;
  64 import org.graalvm.compiler.phases.OptimisticOptimizations;
  65 import org.graalvm.compiler.phases.common.DeadCodeEliminationPhase;
  66 import org.graalvm.compiler.phases.common.DeadCodeEliminationPhase.Optionality;
  67 import org.graalvm.compiler.phases.common.inlining.InliningUtil;
  68 import org.graalvm.compiler.phases.util.Providers;
  69 import org.graalvm.compiler.word.WordTypes;
  70 import org.graalvm.word.LocationIdentity;
  71 
  72 import jdk.vm.ci.code.BytecodeFrame;
  73 import jdk.vm.ci.meta.ConstantReflectionProvider;
  74 import jdk.vm.ci.meta.JavaKind;
  75 import jdk.vm.ci.meta.JavaType;
  76 import jdk.vm.ci.meta.MetaAccessProvider;
  77 import jdk.vm.ci.meta.ResolvedJavaMethod;
  78 import jdk.vm.ci.meta.Signature;
  79 
  80 /**
  81  * A utility for manually creating a graph. This will be expanded as necessary to support all
  82  * subsystems that employ manual graph creation (as opposed to {@linkplain GraphBuilderPhase
  83  * bytecode parsing} based graph creation).
  84  */
  85 public class GraphKit implements GraphBuilderTool {
  86 
  87     protected final Providers providers;
  88     protected final StructuredGraph graph;
  89     protected final WordTypes wordTypes;
  90     protected final GraphBuilderConfiguration.Plugins graphBuilderPlugins;
  91     protected FixedWithNextNode lastFixedNode;
  92 
  93     private final List<Structure> structures;
  94 
  95     protected abstract static class Structure {
  96     }
  97 
  98     public GraphKit(StructuredGraph graph, Providers providers, WordTypes wordTypes, GraphBuilderConfiguration.Plugins graphBuilderPlugins) {
  99         this.providers = providers;
 100         this.graph = graph;
 101         this.wordTypes = wordTypes;
 102         this.graphBuilderPlugins = graphBuilderPlugins;
 103         this.lastFixedNode = graph.start();
 104 
 105         structures = new ArrayList<>();
 106         /*
 107          * Add a dummy element, so that the access of the last element never leads to an exception.
 108          */
 109         structures.add(new Structure() {
 110         });
 111     }
 112 
 113     @Override
 114     public StructuredGraph getGraph() {
 115         return graph;
 116     }
 117 
 118     @Override
 119     public ConstantReflectionProvider getConstantReflection() {
 120         return providers.getConstantReflection();
 121     }
 122 
 123     @Override
 124     public ConstantFieldProvider getConstantFieldProvider() {
 125         return providers.getConstantFieldProvider();
 126     }
 127 
 128     @Override
 129     public MetaAccessProvider getMetaAccess() {
 130         return providers.getMetaAccess();
 131     }
 132 
 133     @Override
 134     public StampProvider getStampProvider() {
 135         return providers.getStampProvider();
 136     }
 137 
 138     @Override
 139     public boolean parsingIntrinsic() {
 140         return true;
 141     }
 142 
 143     /**
 144      * Ensures a floating node is added to or already present in the graph via {@link Graph#unique}.
 145      *
 146      * @return a node similar to {@code node} if one exists, otherwise {@code node}
 147      */
 148     public <T extends FloatingNode & ValueNumberable> T unique(T node) {
 149         return graph.unique(changeToWord(node));
 150     }
 151 
 152     public <T extends ValueNode> T add(T node) {
 153         return graph.add(changeToWord(node));
 154     }
 155 
 156     public <T extends ValueNode> T changeToWord(T node) {
 157         if (wordTypes != null && wordTypes.isWord(node)) {
 158             node.setStamp(wordTypes.getWordStamp(StampTool.typeOrNull(node)));
 159         }
 160         return node;
 161     }
 162 
 163     @Override
 164     public <T extends ValueNode> T append(T node) {
 165         T result = graph.addOrUniqueWithInputs(changeToWord(node));
 166         if (result instanceof FixedNode) {
 167             updateLastFixed((FixedNode) result);
 168         }
 169         return result;
 170     }
 171 
 172     private void updateLastFixed(FixedNode result) {
 173         assert lastFixedNode != null;
 174         assert result.predecessor() == null;
 175         graph.addAfterFixed(lastFixedNode, result);
 176         if (result instanceof FixedWithNextNode) {
 177             lastFixedNode = (FixedWithNextNode) result;
 178         } else {
 179             lastFixedNode = null;
 180         }
 181     }
 182 
 183     public InvokeNode createInvoke(Class<?> declaringClass, String name, ValueNode... args) {
 184         return createInvoke(declaringClass, name, InvokeKind.Static, null, BytecodeFrame.UNKNOWN_BCI, args);
 185     }
 186 
 187     /**
 188      * Creates and appends an {@link InvokeNode} for a call to a given method with a given set of
 189      * arguments. The method is looked up via reflection based on the declaring class and name.
 190      *
 191      * @param declaringClass the class declaring the invoked method
 192      * @param name the name of the invoked method
 193      * @param args the arguments to the invocation
 194      */
 195     public InvokeNode createInvoke(Class<?> declaringClass, String name, InvokeKind invokeKind, FrameStateBuilder frameStateBuilder, int bci, ValueNode... args) {
 196         boolean isStatic = invokeKind == InvokeKind.Static;
 197         ResolvedJavaMethod method = findMethod(declaringClass, name, isStatic);
 198         return createInvoke(method, invokeKind, frameStateBuilder, bci, args);
 199     }
 200 
 201     public ResolvedJavaMethod findMethod(Class<?> declaringClass, String name, boolean isStatic) {
 202         ResolvedJavaMethod method = null;
 203         for (Method m : declaringClass.getDeclaredMethods()) {
 204             if (Modifier.isStatic(m.getModifiers()) == isStatic && m.getName().equals(name)) {
 205                 assert method == null : "found more than one method in " + declaringClass + " named " + name;
 206                 method = providers.getMetaAccess().lookupJavaMethod(m);
 207             }
 208         }
 209         GraalError.guarantee(method != null, "Could not find %s.%s (%s)", declaringClass, name, isStatic ? "static" : "non-static");
 210         return method;
 211     }
 212 
 213     public ResolvedJavaMethod findMethod(Class<?> declaringClass, String name, Class<?>... parameterTypes) {
 214         try {
 215             Method m = declaringClass.getDeclaredMethod(name, parameterTypes);
 216             return providers.getMetaAccess().lookupJavaMethod(m);
 217         } catch (NoSuchMethodException | SecurityException e) {
 218             throw new AssertionError(e);
 219         }
 220     }
 221 
 222     /**
 223      * Creates and appends an {@link InvokeNode} for a call to a given method with a given set of
 224      * arguments.
 225      */
 226     public InvokeNode createInvoke(ResolvedJavaMethod method, InvokeKind invokeKind, FrameStateBuilder frameStateBuilder, int bci, ValueNode... args) {
 227         assert method.isStatic() == (invokeKind == InvokeKind.Static);
 228         Signature signature = method.getSignature();
 229         JavaType returnType = signature.getReturnType(null);
 230         assert checkArgs(method, args);
 231         StampPair returnStamp = graphBuilderPlugins.getOverridingStamp(this, returnType, false);
 232         if (returnStamp == null) {
 233             returnStamp = StampFactory.forDeclaredType(graph.getAssumptions(), returnType, false);
 234         }
 235         MethodCallTargetNode callTarget = graph.add(createMethodCallTarget(invokeKind, method, args, returnStamp, bci));
 236         InvokeNode invoke = append(new InvokeNode(callTarget, bci));
 237 
 238         if (frameStateBuilder != null) {
 239             if (invoke.getStackKind() != JavaKind.Void) {
 240                 frameStateBuilder.push(invoke.getStackKind(), invoke);
 241             }
 242             invoke.setStateAfter(frameStateBuilder.create(bci, invoke));
 243             if (invoke.getStackKind() != JavaKind.Void) {
 244                 frameStateBuilder.pop(invoke.getStackKind());
 245             }
 246         }
 247         return invoke;
 248     }
 249 
 250     protected MethodCallTargetNode createMethodCallTarget(InvokeKind invokeKind, ResolvedJavaMethod targetMethod, ValueNode[] args, StampPair returnStamp, @SuppressWarnings("unused") int bci) {
 251         return new MethodCallTargetNode(invokeKind, targetMethod, args, returnStamp, null);
 252     }
 253 
 254     protected final JavaKind asKind(JavaType type) {
 255         return wordTypes != null ? wordTypes.asKind(type) : type.getJavaKind();
 256     }
 257 
 258     /**
 259      * Determines if a given set of arguments is compatible with the signature of a given method.
 260      *
 261      * @return true if {@code args} are compatible with the signature if {@code method}
 262      * @throws AssertionError if {@code args} are not compatible with the signature if
 263      *             {@code method}
 264      */
 265     public boolean checkArgs(ResolvedJavaMethod method, ValueNode... args) {
 266         Signature signature = method.getSignature();
 267         boolean isStatic = method.isStatic();
 268         if (signature.getParameterCount(!isStatic) != args.length) {
 269             throw new AssertionError(graph + ": wrong number of arguments to " + method);
 270         }
 271         int argIndex = 0;
 272         if (!isStatic) {
 273             JavaKind expected = asKind(method.getDeclaringClass());
 274             JavaKind actual = args[argIndex++].stamp().getStackKind();
 275             assert expected == actual : graph + ": wrong kind of value for receiver argument of call to " + method + " [" + actual + " != " + expected + "]";
 276         }
 277         for (int i = 0; i != signature.getParameterCount(false); i++) {
 278             JavaKind expected = asKind(signature.getParameterType(i, method.getDeclaringClass())).getStackKind();
 279             JavaKind actual = args[argIndex++].stamp().getStackKind();
 280             if (expected != actual) {
 281                 throw new AssertionError(graph + ": wrong kind of value for argument " + i + " of call to " + method + " [" + actual + " != " + expected + "]");
 282             }
 283         }
 284         return true;
 285     }
 286 
 287     /**
 288      * Recursively {@linkplain #inline inlines} all invocations currently in the graph.
 289      */
 290     public void inlineInvokes() {
 291         while (!graph.getNodes().filter(InvokeNode.class).isEmpty()) {
 292             for (InvokeNode invoke : graph.getNodes().filter(InvokeNode.class).snapshot()) {
 293                 inline(invoke);
 294             }
 295         }
 296 
 297         // Clean up all code that is now dead after inlining.
 298         new DeadCodeEliminationPhase().apply(graph);
 299     }
 300 
 301     /**
 302      * Inlines a given invocation to a method. The graph of the inlined method is processed in the
 303      * same manner as for snippets and method substitutions.
 304      */
 305     public void inline(InvokeNode invoke) {
 306         ResolvedJavaMethod method = ((MethodCallTargetNode) invoke.callTarget()).targetMethod();
 307 
 308         MetaAccessProvider metaAccess = providers.getMetaAccess();
 309         Plugins plugins = new Plugins(graphBuilderPlugins);
 310         GraphBuilderConfiguration config = GraphBuilderConfiguration.getSnippetDefault(plugins);
 311 
 312         StructuredGraph calleeGraph = new StructuredGraph.Builder(invoke.getOptions(), invoke.getDebug()).method(method).build();
 313         IntrinsicContext initialReplacementContext = new IntrinsicContext(method, method, providers.getReplacements().getDefaultReplacementBytecodeProvider(), INLINE_AFTER_PARSING);
 314         GraphBuilderPhase.Instance instance = new GraphBuilderPhase.Instance(metaAccess, providers.getStampProvider(), providers.getConstantReflection(), providers.getConstantFieldProvider(), config,
 315                         OptimisticOptimizations.NONE,
 316                         initialReplacementContext);
 317         instance.apply(calleeGraph);
 318 
 319         // Remove all frame states from inlinee
 320         calleeGraph.clearAllStateAfter();
 321         new DeadCodeEliminationPhase(Optionality.Required).apply(calleeGraph);
 322 
 323         InliningUtil.inline(invoke, calleeGraph, false, method);
 324     }
 325 
 326     protected void pushStructure(Structure structure) {
 327         structures.add(structure);
 328     }
 329 
 330     protected <T extends Structure> T getTopStructure(Class<T> expectedClass) {
 331         return expectedClass.cast(structures.get(structures.size() - 1));
 332     }
 333 
 334     protected void popStructure() {
 335         structures.remove(structures.size() - 1);
 336     }
 337 
 338     protected enum IfState {
 339         CONDITION,
 340         THEN_PART,
 341         ELSE_PART,
 342         FINISHED
 343     }
 344 
 345     static class IfStructure extends Structure {
 346         protected IfState state;
 347         protected FixedNode thenPart;
 348         protected FixedNode elsePart;
 349     }
 350 
 351     /**
 352      * Starts an if-block. This call can be followed by a call to {@link #thenPart} to start
 353      * emitting the code executed when the condition hold; and a call to {@link #elsePart} to start
 354      * emititng the code when the condition does not hold. It must be followed by a call to
 355      * {@link #endIf} to close the if-block.
 356      *
 357      * @param condition The condition for the if-block
 358      * @param trueProbability The estimated probability the condition is true
 359      */
 360     public void startIf(LogicNode condition, double trueProbability) {
 361         AbstractBeginNode thenSuccessor = graph.add(new BeginNode());
 362         AbstractBeginNode elseSuccessor = graph.add(new BeginNode());
 363         append(new IfNode(condition, thenSuccessor, elseSuccessor, trueProbability));
 364         lastFixedNode = null;
 365 
 366         IfStructure s = new IfStructure();
 367         s.state = IfState.CONDITION;
 368         s.thenPart = thenSuccessor;
 369         s.elsePart = elseSuccessor;
 370         pushStructure(s);
 371     }
 372 
 373     private IfStructure saveLastIfNode() {
 374         IfStructure s = getTopStructure(IfStructure.class);
 375         switch (s.state) {
 376             case CONDITION:
 377                 assert lastFixedNode == null;
 378                 break;
 379             case THEN_PART:
 380                 s.thenPart = lastFixedNode;
 381                 break;
 382             case ELSE_PART:
 383                 s.elsePart = lastFixedNode;
 384                 break;
 385             case FINISHED:
 386                 assert false;
 387                 break;
 388         }
 389         lastFixedNode = null;
 390         return s;
 391     }
 392 
 393     public void thenPart() {
 394         IfStructure s = saveLastIfNode();
 395         lastFixedNode = (FixedWithNextNode) s.thenPart;
 396         s.state = IfState.THEN_PART;
 397     }
 398 
 399     public void elsePart() {
 400         IfStructure s = saveLastIfNode();
 401         lastFixedNode = (FixedWithNextNode) s.elsePart;
 402         s.state = IfState.ELSE_PART;
 403     }
 404 
 405     public void endIf() {
 406         IfStructure s = saveLastIfNode();
 407 
 408         FixedWithNextNode thenPart = s.thenPart instanceof FixedWithNextNode ? (FixedWithNextNode) s.thenPart : null;
 409         FixedWithNextNode elsePart = s.elsePart instanceof FixedWithNextNode ? (FixedWithNextNode) s.elsePart : null;
 410 
 411         if (thenPart != null && elsePart != null) {
 412             /* Both parts are alive, we need a real merge. */
 413             EndNode thenEnd = graph.add(new EndNode());
 414             graph.addAfterFixed(thenPart, thenEnd);
 415             EndNode elseEnd = graph.add(new EndNode());
 416             graph.addAfterFixed(elsePart, elseEnd);
 417 
 418             AbstractMergeNode merge = graph.add(new MergeNode());
 419             merge.addForwardEnd(thenEnd);
 420             merge.addForwardEnd(elseEnd);
 421 
 422             lastFixedNode = merge;
 423 
 424         } else if (thenPart != null) {
 425             /* elsePart ended with a control sink, so we can continue with thenPart. */
 426             lastFixedNode = thenPart;
 427 
 428         } else if (elsePart != null) {
 429             /* thenPart ended with a control sink, so we can continue with elsePart. */
 430             lastFixedNode = elsePart;
 431 
 432         } else {
 433             /* Both parts ended with a control sink, so no nodes can be added after the if. */
 434             assert lastFixedNode == null;
 435         }
 436         s.state = IfState.FINISHED;
 437         popStructure();
 438     }
 439 
 440     static class InvokeWithExceptionStructure extends Structure {
 441         protected enum State {
 442             INVOKE,
 443             NO_EXCEPTION_EDGE,
 444             EXCEPTION_EDGE,
 445             FINISHED
 446         }
 447 
 448         protected State state;
 449         protected ExceptionObjectNode exceptionObject;
 450         protected FixedNode noExceptionEdge;
 451         protected FixedNode exceptionEdge;
 452     }
 453 
 454     public InvokeWithExceptionNode startInvokeWithException(ResolvedJavaMethod method, InvokeKind invokeKind,
 455                     FrameStateBuilder frameStateBuilder, int invokeBci, int exceptionEdgeBci, ValueNode... args) {
 456 
 457         assert method.isStatic() == (invokeKind == InvokeKind.Static);
 458         Signature signature = method.getSignature();
 459         JavaType returnType = signature.getReturnType(null);
 460         assert checkArgs(method, args);
 461         StampPair returnStamp = graphBuilderPlugins.getOverridingStamp(this, returnType, false);
 462         if (returnStamp == null) {
 463             returnStamp = StampFactory.forDeclaredType(graph.getAssumptions(), returnType, false);
 464         }
 465         ExceptionObjectNode exceptionObject = add(new ExceptionObjectNode(getMetaAccess()));
 466         if (frameStateBuilder != null) {
 467             FrameStateBuilder exceptionState = frameStateBuilder.copy();
 468             exceptionState.clearStack();
 469             exceptionState.push(JavaKind.Object, exceptionObject);
 470             exceptionState.setRethrowException(false);
 471             exceptionObject.setStateAfter(exceptionState.create(exceptionEdgeBci, exceptionObject));
 472         }
 473         MethodCallTargetNode callTarget = graph.add(createMethodCallTarget(invokeKind, method, args, returnStamp, invokeBci));
 474         InvokeWithExceptionNode invoke = append(new InvokeWithExceptionNode(callTarget, exceptionObject, invokeBci));
 475         AbstractBeginNode noExceptionEdge = graph.add(KillingBeginNode.create(LocationIdentity.any()));
 476         invoke.setNext(noExceptionEdge);
 477         if (frameStateBuilder != null) {
 478             if (invoke.getStackKind() != JavaKind.Void) {
 479                 frameStateBuilder.push(invoke.getStackKind(), invoke);
 480             }
 481             invoke.setStateAfter(frameStateBuilder.create(invokeBci, invoke));
 482             if (invoke.getStackKind() != JavaKind.Void) {
 483                 frameStateBuilder.pop(invoke.getStackKind());
 484             }
 485         }
 486         lastFixedNode = null;
 487 
 488         InvokeWithExceptionStructure s = new InvokeWithExceptionStructure();
 489         s.state = InvokeWithExceptionStructure.State.INVOKE;
 490         s.noExceptionEdge = noExceptionEdge;
 491         s.exceptionEdge = exceptionObject;
 492         s.exceptionObject = exceptionObject;
 493         pushStructure(s);
 494 
 495         return invoke;
 496     }
 497 
 498     private InvokeWithExceptionStructure saveLastInvokeWithExceptionNode() {
 499         InvokeWithExceptionStructure s = getTopStructure(InvokeWithExceptionStructure.class);
 500         switch (s.state) {
 501             case INVOKE:
 502                 assert lastFixedNode == null;
 503                 break;
 504             case NO_EXCEPTION_EDGE:
 505                 s.noExceptionEdge = lastFixedNode;
 506                 break;
 507             case EXCEPTION_EDGE:
 508                 s.exceptionEdge = lastFixedNode;
 509                 break;
 510             case FINISHED:
 511                 assert false;
 512                 break;
 513         }
 514         lastFixedNode = null;
 515         return s;
 516     }
 517 
 518     public void noExceptionPart() {
 519         InvokeWithExceptionStructure s = saveLastInvokeWithExceptionNode();
 520         lastFixedNode = (FixedWithNextNode) s.noExceptionEdge;
 521         s.state = InvokeWithExceptionStructure.State.NO_EXCEPTION_EDGE;
 522     }
 523 
 524     public void exceptionPart() {
 525         InvokeWithExceptionStructure s = saveLastInvokeWithExceptionNode();
 526         lastFixedNode = (FixedWithNextNode) s.exceptionEdge;
 527         s.state = InvokeWithExceptionStructure.State.EXCEPTION_EDGE;
 528     }
 529 
 530     public ExceptionObjectNode exceptionObject() {
 531         InvokeWithExceptionStructure s = getTopStructure(InvokeWithExceptionStructure.class);
 532         return s.exceptionObject;
 533     }
 534 
 535     /**
 536      * Finishes a control flow started with {@link #startInvokeWithException}. If necessary, creates
 537      * a merge of the non-exception and exception edges. The merge node is returned and the
 538      * non-exception edge is the first forward end of the merge, the exception edge is the second
 539      * forward end (relevant for phi nodes).
 540      */
 541     public AbstractMergeNode endInvokeWithException() {
 542         InvokeWithExceptionStructure s = saveLastInvokeWithExceptionNode();
 543         FixedWithNextNode noExceptionEdge = s.noExceptionEdge instanceof FixedWithNextNode ? (FixedWithNextNode) s.noExceptionEdge : null;
 544         FixedWithNextNode exceptionEdge = s.exceptionEdge instanceof FixedWithNextNode ? (FixedWithNextNode) s.exceptionEdge : null;
 545         AbstractMergeNode merge = null;
 546         if (noExceptionEdge != null && exceptionEdge != null) {
 547             EndNode noExceptionEnd = graph.add(new EndNode());
 548             graph.addAfterFixed(noExceptionEdge, noExceptionEnd);
 549             EndNode exceptionEnd = graph.add(new EndNode());
 550             graph.addAfterFixed(exceptionEdge, exceptionEnd);
 551             merge = graph.add(new MergeNode());
 552             merge.addForwardEnd(noExceptionEnd);
 553             merge.addForwardEnd(exceptionEnd);
 554             lastFixedNode = merge;
 555         } else if (noExceptionEdge != null) {
 556             lastFixedNode = noExceptionEdge;
 557         } else if (exceptionEdge != null) {
 558             lastFixedNode = exceptionEdge;
 559         } else {
 560             assert lastFixedNode == null;
 561         }
 562         s.state = InvokeWithExceptionStructure.State.FINISHED;
 563         popStructure();
 564         return merge;
 565     }
 566 }