InvokeWithExceptionNode.java revision 13133:9ee4febb41aa
1/*
2 * Copyright (c) 2011, 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 */
23package org.graalvm.compiler.nodes;
24
25import jdk.vm.ci.meta.JavaKind;
26import org.graalvm.api.word.LocationIdentity;
27import org.graalvm.compiler.core.common.type.Stamp;
28import org.graalvm.compiler.graph.Node;
29import org.graalvm.compiler.graph.NodeClass;
30import org.graalvm.compiler.nodeinfo.NodeInfo;
31import org.graalvm.compiler.nodeinfo.Verbosity;
32import org.graalvm.compiler.nodes.extended.ForeignCallNode;
33import org.graalvm.compiler.nodes.java.MethodCallTargetNode;
34import org.graalvm.compiler.nodes.memory.MemoryCheckpoint;
35import org.graalvm.compiler.nodes.spi.LIRLowerable;
36import org.graalvm.compiler.nodes.spi.LoweringTool;
37import org.graalvm.compiler.nodes.spi.NodeLIRBuilderTool;
38import org.graalvm.compiler.nodes.spi.UncheckedInterfaceProvider;
39import org.graalvm.compiler.nodes.util.GraphUtil;
40
41import java.util.Map;
42
43import static org.graalvm.compiler.nodeinfo.InputType.Extension;
44import static org.graalvm.compiler.nodeinfo.InputType.Memory;
45import static org.graalvm.compiler.nodeinfo.InputType.State;
46import static org.graalvm.compiler.nodeinfo.NodeCycles.CYCLES_UNKNOWN;
47import static org.graalvm.compiler.nodeinfo.NodeSize.SIZE_UNKNOWN;
48
49@NodeInfo(nameTemplate = "Invoke!#{p#targetMethod/s}", allowedUsageTypes = {Memory}, cycles = CYCLES_UNKNOWN, size = SIZE_UNKNOWN)
50public final class InvokeWithExceptionNode extends ControlSplitNode implements Invoke, MemoryCheckpoint.Single, LIRLowerable, UncheckedInterfaceProvider {
51    public static final NodeClass<InvokeWithExceptionNode> TYPE = NodeClass.create(InvokeWithExceptionNode.class);
52
53    private static final double EXCEPTION_PROBA = 1e-5;
54
55    @Successor AbstractBeginNode next;
56    @Successor AbstractBeginNode exceptionEdge;
57    @OptionalInput ValueNode classInit;
58    @Input(Extension) CallTargetNode callTarget;
59    @OptionalInput(State) FrameState stateDuring;
60    @OptionalInput(State) FrameState stateAfter;
61    protected final int bci;
62    protected boolean polymorphic;
63    protected boolean useForInlining;
64    protected double exceptionProbability;
65
66    public InvokeWithExceptionNode(CallTargetNode callTarget, AbstractBeginNode exceptionEdge, int bci) {
67        super(TYPE, callTarget.returnStamp().getTrustedStamp());
68        this.exceptionEdge = exceptionEdge;
69        this.bci = bci;
70        this.callTarget = callTarget;
71        this.polymorphic = false;
72        this.useForInlining = true;
73        this.exceptionProbability = EXCEPTION_PROBA;
74    }
75
76    public AbstractBeginNode exceptionEdge() {
77        return exceptionEdge;
78    }
79
80    public void setExceptionEdge(AbstractBeginNode x) {
81        updatePredecessor(exceptionEdge, x);
82        exceptionEdge = x;
83    }
84
85    @Override
86    public AbstractBeginNode next() {
87        return next;
88    }
89
90    public void setNext(AbstractBeginNode x) {
91        updatePredecessor(next, x);
92        next = x;
93    }
94
95    @Override
96    public CallTargetNode callTarget() {
97        return callTarget;
98    }
99
100    void setCallTarget(CallTargetNode callTarget) {
101        updateUsages(this.callTarget, callTarget);
102        this.callTarget = callTarget;
103    }
104
105    public MethodCallTargetNode methodCallTarget() {
106        return (MethodCallTargetNode) callTarget;
107    }
108
109    @Override
110    public boolean isPolymorphic() {
111        return polymorphic;
112    }
113
114    @Override
115    public void setPolymorphic(boolean value) {
116        this.polymorphic = value;
117    }
118
119    @Override
120    public boolean useForInlining() {
121        return useForInlining;
122    }
123
124    @Override
125    public void setUseForInlining(boolean value) {
126        this.useForInlining = value;
127    }
128
129    @Override
130    public String toString(Verbosity verbosity) {
131        if (verbosity == Verbosity.Long) {
132            return super.toString(Verbosity.Short) + "(bci=" + bci() + ")";
133        } else if (verbosity == Verbosity.Name) {
134            return "Invoke#" + (callTarget == null ? "null" : callTarget().targetName());
135        } else {
136            return super.toString(verbosity);
137        }
138    }
139
140    @Override
141    public int bci() {
142        return bci;
143    }
144
145    @Override
146    public void setNext(FixedNode x) {
147        if (x != null) {
148            this.setNext(KillingBeginNode.begin(x, getLocationIdentity()));
149        } else {
150            this.setNext(null);
151        }
152    }
153
154    @Override
155    public void lower(LoweringTool tool) {
156        tool.getLowerer().lower(this, tool);
157    }
158
159    @Override
160    public void generate(NodeLIRBuilderTool gen) {
161        gen.emitInvoke(this);
162    }
163
164    @Override
165    public FrameState stateAfter() {
166        return stateAfter;
167    }
168
169    @Override
170    public void setStateAfter(FrameState stateAfter) {
171        updateUsages(this.stateAfter, stateAfter);
172        this.stateAfter = stateAfter;
173    }
174
175    @Override
176    public boolean hasSideEffect() {
177        return true;
178    }
179
180    @Override
181    public LocationIdentity getLocationIdentity() {
182        return LocationIdentity.any();
183    }
184
185    @Override
186    public Map<Object, Object> getDebugProperties(Map<Object, Object> map) {
187        Map<Object, Object> debugProperties = super.getDebugProperties(map);
188        if (callTarget != null) {
189            debugProperties.put("targetMethod", callTarget.targetName());
190        }
191        return debugProperties;
192    }
193
194    public void killExceptionEdge() {
195        AbstractBeginNode edge = exceptionEdge();
196        setExceptionEdge(null);
197        GraphUtil.killCFG(edge);
198    }
199
200    public void replaceWithNewBci(int newBci) {
201        AbstractBeginNode nextNode = next();
202        AbstractBeginNode exceptionObject = exceptionEdge;
203        setExceptionEdge(null);
204        setNext(null);
205        InvokeWithExceptionNode repl = graph().add(new InvokeWithExceptionNode(callTarget(), exceptionObject, newBci));
206        repl.setStateAfter(stateAfter);
207        this.setStateAfter(null);
208        this.replaceAtPredecessor(repl);
209        repl.setNext(nextNode);
210        boolean removed = this.callTarget().removeUsage(this);
211        assert removed;
212        this.replaceAtUsages(repl);
213        this.markDeleted();
214    }
215
216    @Override
217    public void intrinsify(Node node) {
218        assert !(node instanceof ValueNode) || (((ValueNode) node).getStackKind() == JavaKind.Void) == (getStackKind() == JavaKind.Void);
219        CallTargetNode call = callTarget;
220        FrameState state = stateAfter();
221        if (exceptionEdge != null) {
222            killExceptionEdge();
223        }
224        if (node instanceof StateSplit) {
225            StateSplit stateSplit = (StateSplit) node;
226            stateSplit.setStateAfter(state);
227        }
228        if (node instanceof ForeignCallNode) {
229            ForeignCallNode foreign = (ForeignCallNode) node;
230            foreign.setBci(bci());
231        }
232        if (node == null) {
233            assert getStackKind() == JavaKind.Void && hasNoUsages();
234            graph().removeSplit(this, next());
235        } else if (node instanceof ControlSinkNode) {
236            this.replaceAtPredecessor(node);
237            this.replaceAtUsages(null);
238            GraphUtil.killCFG(this);
239            return;
240        } else {
241            graph().replaceSplit(this, node, next());
242        }
243        GraphUtil.killWithUnusedFloatingInputs(call);
244        if (state.hasNoUsages()) {
245            GraphUtil.killWithUnusedFloatingInputs(state);
246        }
247    }
248
249    @Override
250    public double probability(AbstractBeginNode successor) {
251        return successor == next ? 1 - exceptionProbability : exceptionProbability;
252    }
253
254    @Override
255    public boolean canDeoptimize() {
256        return true;
257    }
258
259    @Override
260    public FrameState stateDuring() {
261        return stateDuring;
262    }
263
264    @Override
265    public void setStateDuring(FrameState stateDuring) {
266        updateUsages(this.stateDuring, stateDuring);
267        this.stateDuring = stateDuring;
268    }
269
270    @Override
271    public AbstractBeginNode getPrimarySuccessor() {
272        return this.next();
273    }
274
275    @Override
276    public Stamp uncheckedStamp() {
277        return this.callTarget.returnStamp().getUncheckedStamp();
278    }
279
280    @Override
281    public void setClassInit(ValueNode classInit) {
282        this.classInit = classInit;
283        updateUsages(null, classInit);
284    }
285
286    @Override
287    public ValueNode classInit() {
288        return classInit;
289    }
290
291    @Override
292    public boolean setProbability(AbstractBeginNode successor, double value) {
293        // Cannot set probability for exception invokes.
294        return false;
295    }
296
297    @Override
298    public int getSuccessorCount() {
299        return 2;
300    }
301
302    /**
303     * Replaces this InvokeWithExceptionNode with a normal InvokeNode. Kills the exception dispatch
304     * code.
305     */
306    public InvokeNode replaceWithInvoke() {
307        InvokeNode invokeNode = graph().add(new InvokeNode(callTarget, bci));
308        AbstractBeginNode oldException = this.exceptionEdge;
309        graph().replaceSplitWithFixed(this, invokeNode, this.next());
310        GraphUtil.killCFG(oldException);
311        return invokeNode;
312    }
313}
314