/
MatchStatement.java
157 lines (141 loc) 路 6.18 KB
/
MatchStatement.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
* Copyright (c) 2014, 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package org.graalvm.compiler.core.match;
import static org.graalvm.compiler.debug.DebugOptions.LogVerbose;
import org.graalvm.compiler.core.gen.NodeLIRBuilder;
import org.graalvm.compiler.core.match.MatchPattern.MatchResultCode;
import org.graalvm.compiler.core.match.MatchPattern.Result;
import org.graalvm.compiler.debug.CounterKey;
import org.graalvm.compiler.debug.DebugContext;
import org.graalvm.compiler.graph.GraalGraphError;
import org.graalvm.compiler.graph.Node;
import org.graalvm.compiler.nodeinfo.Verbosity;
import jdk.vm.ci.meta.Value;
import org.graalvm.compiler.nodes.StructuredGraph;
import org.graalvm.compiler.nodes.cfg.Block;
/**
* A named {@link MatchPattern} along with a {@link MatchGenerator} that can be evaluated to replace
* one or more {@link Node}s with a single {@link Value}.
*/
public class MatchStatement {
private static final CounterKey MatchStatementSuccess = DebugContext.counter("MatchStatementSuccess");
/**
* A printable name for this statement. Usually it's just the name of the method doing the
* emission.
*/
private final String name;
/**
* The actual match pattern.
*/
private final MatchPattern pattern;
/**
* The method in the {@link NodeLIRBuilder} subclass that will actually do the code emission.
*/
private MatchGenerator generatorMethod;
/**
* The name of arguments in the order they are expected to be passed to the generator method.
*/
private String[] arguments;
public MatchStatement(String name, MatchPattern pattern, MatchGenerator generator, String[] arguments) {
this.name = name;
this.pattern = pattern;
this.generatorMethod = generator;
this.arguments = arguments;
}
/**
* Attempt to match the current statement against a Node.
*
* @param builder the current builder instance.
* @param node the node to be matched
* @param block the current block
* @param schedule the schedule that's being used
* @return true if the statement matched something and set a {@link ComplexMatchResult} to be
* evaluated by the NodeLIRBuilder.
*/
public boolean generate(NodeLIRBuilder builder, int index, Node node, Block block, StructuredGraph.ScheduleResult schedule) {
DebugContext debug = node.getDebug();
assert index == schedule.getBlockToNodesMap().get(block).indexOf(node);
// Check that the basic shape matches
Result result = pattern.matchShape(node, this);
if (result != Result.OK) {
return false;
}
// Now ensure that the other safety constraints are matched.
MatchContext context = new MatchContext(builder, this, index, node, block, schedule);
result = pattern.matchUsage(node, context);
if (result == Result.OK) {
// Invoke the generator method and set the result if it's non null.
ComplexMatchResult value = generatorMethod.match(builder.getNodeMatchRules(), buildArgList(context));
if (value != null) {
context.setResult(value);
MatchStatementSuccess.increment(debug);
DebugContext.counter("MatchStatement[%s]", getName()).increment(debug);
return true;
}
// The pattern matched but some other code generation constraint disallowed code
// generation for the pattern.
if (LogVerbose.getValue(node.getOptions())) {
debug.log("while matching %s|%s %s %s returned null", context.getRoot().toString(Verbosity.Id), context.getRoot().getClass().getSimpleName(), getName(), generatorMethod.getName());
debug.log("with nodes %s", formatMatch(node));
}
} else {
if (LogVerbose.getValue(node.getOptions()) && result.code != MatchResultCode.WRONG_CLASS) {
debug.log("while matching %s|%s %s %s", context.getRoot().toString(Verbosity.Id), context.getRoot().getClass().getSimpleName(), getName(), result);
}
}
return false;
}
/**
* @param context
* @return the Nodes captured by the match rule in the order expected by the generatorMethod
*/
private Object[] buildArgList(MatchContext context) {
Object[] result = new Object[arguments.length];
for (int i = 0; i < arguments.length; i++) {
if ("root".equals(arguments[i])) {
result[i] = context.getRoot();
} else {
result[i] = context.namedNode(arguments[i]);
if (result[i] == null) {
throw new GraalGraphError("Can't find named node %s", arguments[i]);
}
}
}
return result;
}
public String formatMatch(Node root) {
return pattern.formatMatch(root);
}
public MatchPattern getPattern() {
return pattern;
}
public String getName() {
return name;
}
@Override
public String toString() {
return pattern.toString();
}
}