1 /*
   2  * Copyright (c) 2013, 2019, 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 
  24 
  25 package org.graalvm.compiler.phases.common;
  26 
  27 import java.util.List;
  28 
  29 import jdk.internal.vm.compiler.collections.EconomicMap;
  30 import org.graalvm.compiler.debug.GraalError;
  31 import org.graalvm.compiler.graph.Node;
  32 import org.graalvm.compiler.nodes.AbstractBeginNode;
  33 import org.graalvm.compiler.nodes.AbstractMergeNode;
  34 import org.graalvm.compiler.nodes.DeoptimizingNode;
  35 import org.graalvm.compiler.nodes.FixedNode;
  36 import org.graalvm.compiler.nodes.FrameState;
  37 import org.graalvm.compiler.nodes.LoopBeginNode;
  38 import org.graalvm.compiler.nodes.LoopExitNode;
  39 import org.graalvm.compiler.nodes.StateSplit;
  40 import org.graalvm.compiler.nodes.StructuredGraph;
  41 import org.graalvm.compiler.nodes.StructuredGraph.GuardsStage;
  42 import org.graalvm.compiler.nodes.util.GraphUtil;
  43 import org.graalvm.compiler.phases.Phase;
  44 import org.graalvm.compiler.phases.graph.ReentrantNodeIterator;
  45 import org.graalvm.compiler.phases.graph.ReentrantNodeIterator.NodeIteratorClosure;
  46 
  47 import jdk.vm.ci.code.BytecodeFrame;
  48 
  49 /**
  50  * This phase transfers {@link FrameState} nodes from {@link StateSplit} nodes to
  51  * {@link DeoptimizingNode DeoptimizingNodes}.
  52  *
  53  * This allow to enter the {@link GuardsStage#AFTER_FSA AFTER_FSA} stage of the graph where no new
  54  * node that may cause deoptimization can be introduced anymore.
  55  * <p>
  56  * This Phase processes the graph in post order, assigning the {@link FrameState} from the last
  57  * {@link StateSplit} node to {@link DeoptimizingNode DeoptimizingNodes}.
  58  */
  59 public class FrameStateAssignmentPhase extends Phase {
  60 
  61     private static class FrameStateAssignmentClosure extends NodeIteratorClosure<FrameState> {
  62 
  63         @Override
  64         protected FrameState processNode(FixedNode node, FrameState previousState) {
  65             FrameState currentState = previousState;
  66             if (node instanceof DeoptimizingNode.DeoptBefore) {
  67                 DeoptimizingNode.DeoptBefore deopt = (DeoptimizingNode.DeoptBefore) node;
  68                 if (deopt.canDeoptimize() && deopt.stateBefore() == null) {
  69                     GraalError.guarantee(currentState != null, "no FrameState at DeoptimizingNode %s", deopt);
  70                     deopt.setStateBefore(currentState);
  71                 }
  72             }
  73 
  74             if (node instanceof StateSplit) {
  75                 StateSplit stateSplit = (StateSplit) node;
  76                 FrameState stateAfter = stateSplit.stateAfter();
  77                 if (stateAfter != null) {
  78                     if (stateAfter.bci == BytecodeFrame.INVALID_FRAMESTATE_BCI) {
  79                         currentState = null;
  80                     } else {
  81                         currentState = stateAfter;
  82                     }
  83                     stateSplit.setStateAfter(null);
  84                 }
  85             }
  86 
  87             if (node instanceof DeoptimizingNode.DeoptDuring) {
  88                 DeoptimizingNode.DeoptDuring deopt = (DeoptimizingNode.DeoptDuring) node;
  89                 if (deopt.canDeoptimize() && deopt.stateDuring() == null) {
  90                     GraalError.guarantee(currentState != null, "no FrameState at DeoptimizingNode %s", deopt);
  91                     deopt.computeStateDuring(currentState);
  92                 }
  93             }
  94 
  95             if (node instanceof DeoptimizingNode.DeoptAfter) {
  96                 DeoptimizingNode.DeoptAfter deopt = (DeoptimizingNode.DeoptAfter) node;
  97                 if (deopt.canDeoptimize() && deopt.stateAfter() == null) {
  98                     GraalError.guarantee(currentState != null, "no FrameState at DeoptimizingNode %s", deopt);
  99                     deopt.setStateAfter(currentState);
 100                 }
 101             }
 102 
 103             return currentState;
 104         }
 105 
 106         @Override
 107         protected FrameState merge(AbstractMergeNode merge, List<FrameState> states) {
 108             FrameState singleFrameState = singleFrameState(states);
 109             return singleFrameState == null ? merge.stateAfter() : singleFrameState;
 110         }
 111 
 112         @Override
 113         protected FrameState afterSplit(AbstractBeginNode node, FrameState oldState) {
 114             return oldState;
 115         }
 116 
 117         @Override
 118         protected EconomicMap<LoopExitNode, FrameState> processLoop(LoopBeginNode loop, FrameState initialState) {
 119             return ReentrantNodeIterator.processLoop(this, loop, initialState).exitStates;
 120         }
 121     }
 122 
 123     @Override
 124     protected void run(StructuredGraph graph) {
 125         assert !graph.getGuardsStage().allowsFloatingGuards() && !hasFloatingDeopts(graph);
 126         if (graph.getGuardsStage().areFrameStatesAtSideEffects()) {
 127             ReentrantNodeIterator.apply(new FrameStateAssignmentClosure(), graph.start(), null);
 128             graph.setGuardsStage(GuardsStage.AFTER_FSA);
 129             graph.getNodes(FrameState.TYPE).filter(state -> state.hasNoUsages()).forEach(GraphUtil::killWithUnusedFloatingInputs);
 130         }
 131     }
 132 
 133     private static boolean hasFloatingDeopts(StructuredGraph graph) {
 134         for (Node n : graph.getNodes()) {
 135             if (n instanceof DeoptimizingNode && GraphUtil.isFloatingNode(n)) {
 136                 DeoptimizingNode deoptimizingNode = (DeoptimizingNode) n;
 137                 if (deoptimizingNode.canDeoptimize()) {
 138                     return true;
 139                 }
 140             }
 141         }
 142         return false;
 143     }
 144 
 145     private static FrameState singleFrameState(List<FrameState> states) {
 146         FrameState singleState = states.get(0);
 147         for (int i = 1; i < states.size(); ++i) {
 148             if (states.get(i) != singleState) {
 149                 return null;
 150             }
 151         }
 152         if (singleState != null && singleState.bci != BytecodeFrame.INVALID_FRAMESTATE_BCI) {
 153             return singleState;
 154         }
 155         return null;
 156     }
 157 
 158     @Override
 159     public boolean checkContract() {
 160         // TODO GR-1409
 161         return false;
 162     }
 163 }