Skip to content

Commit 7f1e61b

Browse files
flohuemerrmosaner
authored andcommitted
Implemented reuse of arrays inside loop scopes when exploding loops during partial evaluation.
Added a cached value for track node insertion in graphs. Added a cache for the values of proxy placeholders to speed up hash computations and comparisons. Added a cache for the node orderIds of proxy placeholders to allow for easier proxy-placeholder creation during loop explosion.
1 parent 67ce159 commit 7f1e61b

File tree

7 files changed

+582
-109
lines changed

7 files changed

+582
-109
lines changed

compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/graph/Graph.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2011, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2011, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -84,6 +84,12 @@ private enum FreezeState {
8484
public final boolean verifyGraphs;
8585
public final boolean verifyGraphEdges;
8686

87+
/**
88+
* Cached actual value of {@link GraalOptions#TrackNodeInsertion} to avoid expensive map lookup
89+
* every time a node is registered.
90+
*/
91+
public final boolean trackNodeInsertion;
92+
8793
/**
8894
* The set of nodes in the graph, ordered by {@linkplain #register(Node) registration} time.
8995
*/
@@ -329,6 +335,8 @@ public Graph(String name, OptionValues options, DebugContext debug, boolean trac
329335

330336
verifyGraphs = Options.VerifyGraalGraphs.getValue(options);
331337
verifyGraphEdges = Options.VerifyGraalGraphEdges.getValue(options);
338+
339+
trackNodeInsertion = TrackNodeInsertion.getValue(options);
332340
}
333341

334342
int extractOriginalNodeId(Node node) {
@@ -1311,7 +1319,7 @@ void register(Node node) {
13111319
if (currentNodeSourcePosition != null && trackNodeSourcePosition()) {
13121320
node.setNodeSourcePosition(currentNodeSourcePosition);
13131321
}
1314-
if (TrackNodeInsertion.getValue(getOptions()) && node.getInsertionPosition() == null) {
1322+
if (trackNodeInsertion && node.getInsertionPosition() == null) {
13151323
node.setInsertionPosition(new NodeInsertionStackTrace());
13161324
}
13171325

compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/nodes/GraphDecoder.java

Lines changed: 447 additions & 92 deletions
Large diffs are not rendered by default.

compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/nodes/SimplifyingGraphDecoder.java

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2015, 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2015, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -38,6 +38,7 @@
3838
import jdk.graal.compiler.debug.TimerKey;
3939
import jdk.graal.compiler.graph.Edges;
4040
import jdk.graal.compiler.graph.Node;
41+
import jdk.graal.compiler.graph.NodeBitMap;
4142
import jdk.graal.compiler.graph.NodeClass;
4243
import jdk.graal.compiler.nodeinfo.InputType;
4344
import jdk.graal.compiler.nodeinfo.NodeInfo;
@@ -149,22 +150,30 @@ protected void cleanupGraph(MethodScope methodScope) {
149150
GraphUtil.normalizeLoops(graph);
150151
super.cleanupGraph(methodScope);
151152

153+
/*
154+
* To avoid calling tryKillUnused for each individual floating node we remove during
155+
* cleanup, we maintain unused nodes in a bitmap and kill them after we finish iterating the
156+
* new nodes in the graph.
157+
*/
158+
final NodeBitMap unusedNodes = new NodeBitMap(graph);
159+
152160
for (Node node : graph.getNewNodes(methodScope.methodStartMark)) {
153-
if (node instanceof MergeNode) {
154-
MergeNode mergeNode = (MergeNode) node;
161+
if (node instanceof MergeNode mergeNode) {
155162
if (mergeNode.forwardEndCount() == 1) {
156-
graph.reduceTrivialMerge(mergeNode);
163+
graph.reduceTrivialMerge(mergeNode, false, unusedNodes);
157164
}
158165
} else if (node instanceof BeginNode) {
159166
if (!(node.predecessor() instanceof ControlSplitNode) && node.hasNoUsages()) {
160167
GraphUtil.unlinkFixedNode((AbstractBeginNode) node);
161168
node.safeDelete();
162169
}
170+
} else if (GraphUtil.shouldKillUnused(node)) {
171+
unusedNodes.mark(node);
163172
}
164173
}
165174

166-
for (Node node : graph.getNewNodes(methodScope.methodStartMark)) {
167-
GraphUtil.tryKillUnused(node);
175+
if (unusedNodes.isNotEmpty()) {
176+
GraphUtil.killAllWithUnusedFloatingInputs(unusedNodes, false);
168177
}
169178
}
170179

compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/nodes/StructuredGraph.java

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,10 @@
5252
import jdk.graal.compiler.debug.TTY;
5353
import jdk.graal.compiler.graph.Graph;
5454
import jdk.graal.compiler.graph.Node;
55+
import jdk.graal.compiler.graph.NodeBitMap;
5556
import jdk.graal.compiler.graph.NodeMap;
5657
import jdk.graal.compiler.graph.NodeSourcePosition;
58+
import jdk.graal.compiler.graph.iterators.NodeIterable;
5759
import jdk.graal.compiler.nodes.GraphState.StageFlag;
5860
import jdk.graal.compiler.nodes.calc.FloatingNode;
5961
import jdk.graal.compiler.nodes.cfg.ControlFlowGraph;
@@ -1049,8 +1051,21 @@ public void reduceTrivialMerge(AbstractMergeNode merge) {
10491051
reduceTrivialMerge(merge, false);
10501052
}
10511053

1052-
@SuppressWarnings("static-method")
10531054
public void reduceTrivialMerge(AbstractMergeNode merge, boolean forKillCFG) {
1055+
reduceTrivialMerge(merge, forKillCFG, null);
1056+
}
1057+
1058+
/**
1059+
* Removes merges with phis that have a single input value.
1060+
*
1061+
* @param unusedNodes A set to mark unused nodes in the graph that should be killed later by the
1062+
* caller. The set should be used when calling this method for multiple merges. The
1063+
* resulting set must be killed with
1064+
* {@link GraphUtil#killAllWithUnusedFloatingInputs(NodeIterable, boolean)}. If this
1065+
* set is null, the nodes are killed immediately.
1066+
*/
1067+
@SuppressWarnings("static-method")
1068+
public void reduceTrivialMerge(AbstractMergeNode merge, boolean forKillCFG, NodeBitMap unusedNodes) {
10541069
assert merge.forwardEndCount() == 1 : Assertions.errorMessageContext("merge", merge);
10551070
assert !(merge instanceof LoopBeginNode) || ((LoopBeginNode) merge).loopEnds().isEmpty();
10561071
for (PhiNode phi : merge.phis().snapshot()) {
@@ -1061,7 +1076,11 @@ public void reduceTrivialMerge(AbstractMergeNode merge, boolean forKillCFG) {
10611076
} else {
10621077
phi.safeDelete();
10631078
if (singleValue != null) {
1064-
GraphUtil.tryKillUnused(singleValue);
1079+
if (unusedNodes == null) {
1080+
GraphUtil.tryKillUnused(singleValue);
1081+
} else if (GraphUtil.shouldKillUnused(singleValue)) {
1082+
unusedNodes.mark(singleValue);
1083+
}
10651084
}
10661085
}
10671086
}
@@ -1076,7 +1095,11 @@ public void reduceTrivialMerge(AbstractMergeNode merge, boolean forKillCFG) {
10761095
merge.prepareDelete((FixedNode) singleEnd.predecessor());
10771096
merge.safeDelete();
10781097
if (stateAfter != null) {
1079-
GraphUtil.tryKillUnused(stateAfter);
1098+
if (unusedNodes == null) {
1099+
GraphUtil.tryKillUnused(stateAfter);
1100+
} else if (GraphUtil.shouldKillUnused(stateAfter)) {
1101+
unusedNodes.mark(stateAfter);
1102+
}
10801103
}
10811104
if (sux == null) {
10821105
singleEnd.replaceAtPredecessor(null);

compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/nodes/util/GraphUtil.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2011, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2011, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -1075,13 +1075,17 @@ private static ValueNode originalValueForComplicatedPhi(ValueNode value, PhiNode
10751075
}
10761076

10771077
public static boolean tryKillUnused(Node node) {
1078-
if (node.isAlive() && isFloatingNode(node) && node.hasNoUsages() && !(node instanceof GuardNode)) {
1078+
if (shouldKillUnused(node)) {
10791079
killWithUnusedFloatingInputs(node);
10801080
return true;
10811081
}
10821082
return false;
10831083
}
10841084

1085+
public static boolean shouldKillUnused(Node node) {
1086+
return node.isAlive() && isFloatingNode(node) && node.hasNoUsages() && !(node instanceof GuardNode);
1087+
}
1088+
10851089
/**
10861090
* Returns an iterator that will return the given node followed by all its predecessors, up
10871091
* until the point where {@link Node#predecessor()} returns null.

compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/replacements/PEGraphDecoder.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2015, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2015, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -1279,9 +1279,7 @@ protected LoopScope doInline(PEMethodScope methodScope, LoopScope loopScope, Inv
12791279
* ParameterNodes.
12801280
*/
12811281
int firstArgumentNodeId = inlineScope.maxFixedNodeOrderId + 1;
1282-
for (int i = 0; i < arguments.length; i++) {
1283-
inlineLoopScope.createdNodes[firstArgumentNodeId + i] = arguments[i];
1284-
}
1282+
inlineLoopScope.setNodes(firstArgumentNodeId, arguments);
12851283

12861284
// Copy inlined methods from inlinee to caller
12871285
recordGraphElements(graphToInline);
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Copyright (c) 2025, 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. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
package jdk.graal.compiler.util;
26+
27+
import java.util.Arrays;
28+
29+
/**
30+
* Utility class for building dynamically sized int arrays.
31+
*/
32+
public class IntArrayBuilder {
33+
private static final int[] EMPTY_INT_ARRAY = {};
34+
35+
private int[] data;
36+
private int size;
37+
38+
public IntArrayBuilder() {
39+
this.data = new int[1];
40+
this.size = 0;
41+
}
42+
43+
private void ensureCapacity(int expectedSize) {
44+
if (expectedSize >= data.length) {
45+
final int nSize = Integer.highestOneBit(expectedSize) << 1;
46+
final int[] nData = new int[nSize];
47+
System.arraycopy(data, 0, nData, 0, size);
48+
data = nData;
49+
}
50+
}
51+
52+
public void set(int index, int value) {
53+
ensureCapacity(index);
54+
data[index] = value;
55+
size = Math.max(size, index + 1);
56+
}
57+
58+
public void add(int value) {
59+
ensureCapacity(size);
60+
data[size] = value;
61+
size++;
62+
}
63+
64+
/**
65+
* @return The int array generated by the builder. May return the underlying int array or a
66+
* correctly sized copy.
67+
*/
68+
public int[] toArray() {
69+
if (size == 0) {
70+
return EMPTY_INT_ARRAY;
71+
} else if (size == data.length) {
72+
return data;
73+
}
74+
return Arrays.copyOf(data, size);
75+
}
76+
}

0 commit comments

Comments
 (0)