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.core.common.GraalOptions.ImmutableCode;
26 import static org.graalvm.compiler.replacements.SnippetTemplate.DEFAULT_REPLACER;
27
28 import java.util.EnumMap;
29
30 import org.graalvm.compiler.api.replacements.Snippet;
31 import org.graalvm.compiler.api.replacements.Snippet.ConstantParameter;
32 import org.graalvm.compiler.api.replacements.SnippetReflectionProvider;
33 import org.graalvm.compiler.debug.Debug;
34 import org.graalvm.compiler.nodes.ConstantNode;
35 import org.graalvm.compiler.nodes.PiNode;
36 import org.graalvm.compiler.nodes.ValueNode;
37 import org.graalvm.compiler.nodes.calc.FloatingNode;
38 import org.graalvm.compiler.nodes.extended.BoxNode;
39 import org.graalvm.compiler.nodes.extended.UnboxNode;
40 import org.graalvm.compiler.nodes.spi.LoweringTool;
41 import org.graalvm.compiler.options.OptionValues;
42 import org.graalvm.compiler.phases.util.Providers;
43 import org.graalvm.compiler.replacements.SnippetCounter.Group;
44 import org.graalvm.compiler.replacements.SnippetTemplate.AbstractTemplates;
45 import org.graalvm.compiler.replacements.SnippetTemplate.Arguments;
46 import org.graalvm.compiler.replacements.SnippetTemplate.SnippetInfo;
47
48 import jdk.vm.ci.code.TargetDescription;
49 import jdk.vm.ci.meta.ConstantReflectionProvider;
50 import jdk.vm.ci.meta.JavaConstant;
51 import jdk.vm.ci.meta.JavaKind;
52 import jdk.vm.ci.meta.MetaAccessProvider;
53
168 sourceConstant = JavaConstant.forShort((short) sourceConstant.asLong());
169 break;
170 }
171 }
172 JavaConstant boxedConstant = constantReflection.boxPrimitive(sourceConstant);
173 if (boxedConstant != null && sourceConstant.getJavaKind() == box.getBoxingKind()) {
174 return ConstantNode.forConstant(boxedConstant, metaAccess, box.graph());
175 }
176 }
177 return null;
178 }
179
180 public static class Templates extends AbstractTemplates {
181
182 private final EnumMap<JavaKind, SnippetInfo> boxSnippets = new EnumMap<>(JavaKind.class);
183 private final EnumMap<JavaKind, SnippetInfo> unboxSnippets = new EnumMap<>(JavaKind.class);
184
185 private final SnippetCounter valueOfCounter;
186 private final SnippetCounter valueCounter;
187
188 public Templates(OptionValues options, SnippetCounter.Group.Factory factory, Providers providers, SnippetReflectionProvider snippetReflection, TargetDescription target) {
189 super(options, providers, snippetReflection, target);
190 for (JavaKind kind : new JavaKind[]{JavaKind.Boolean, JavaKind.Byte, JavaKind.Char, JavaKind.Double, JavaKind.Float, JavaKind.Int, JavaKind.Long, JavaKind.Short}) {
191 boxSnippets.put(kind, snippet(BoxingSnippets.class, kind.getJavaName() + "ValueOf"));
192 unboxSnippets.put(kind, snippet(BoxingSnippets.class, kind.getJavaName() + "Value"));
193 }
194 Group group = factory.createSnippetCounterGroup("Boxing");
195 valueOfCounter = new SnippetCounter(group, "valueOf", "box intrinsification");
196 valueCounter = new SnippetCounter(group, "<kind>Value", "unbox intrinsification");
197 }
198
199 public void lower(BoxNode box, LoweringTool tool) {
200 FloatingNode canonical = canonicalizeBoxing(box, providers.getMetaAccess(), providers.getConstantReflection());
201 // if in AOT mode, we don't want to embed boxed constants.
202 if (canonical != null && !ImmutableCode.getValue(box.getOptions())) {
203 box.graph().replaceFixedWithFloating(box, canonical);
204 } else {
205 Arguments args = new Arguments(boxSnippets.get(box.getBoxingKind()), box.graph().getGuardsStage(), tool.getLoweringStage());
206 args.add("value", box.getValue());
207 args.addConst("valueOfCounter", valueOfCounter);
208
209 SnippetTemplate template = template(args);
210 Debug.log("Lowering integerValueOf in %s: node=%s, template=%s, arguments=%s", box.graph(), box, template, args);
211 template.instantiate(providers.getMetaAccess(), box, DEFAULT_REPLACER, args);
212 }
213 }
214
215 public void lower(UnboxNode unbox, LoweringTool tool) {
216 Arguments args = new Arguments(unboxSnippets.get(unbox.getBoxingKind()), unbox.graph().getGuardsStage(), tool.getLoweringStage());
217 args.add("value", unbox.getValue());
218 args.addConst("valueCounter", valueCounter);
219
220 SnippetTemplate template = template(args);
221 Debug.log("Lowering integerValueOf in %s: node=%s, template=%s, arguments=%s", unbox.graph(), unbox, template, args);
222 template.instantiate(providers.getMetaAccess(), unbox, DEFAULT_REPLACER, args);
223 }
224 }
225 }
|
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.core.common.GraalOptions.ImmutableCode;
26 import static org.graalvm.compiler.replacements.SnippetTemplate.DEFAULT_REPLACER;
27
28 import java.util.EnumMap;
29
30 import org.graalvm.compiler.api.replacements.Snippet;
31 import org.graalvm.compiler.api.replacements.Snippet.ConstantParameter;
32 import org.graalvm.compiler.debug.DebugHandlersFactory;
33 import org.graalvm.compiler.api.replacements.SnippetReflectionProvider;
34 import org.graalvm.compiler.nodes.ConstantNode;
35 import org.graalvm.compiler.nodes.PiNode;
36 import org.graalvm.compiler.nodes.ValueNode;
37 import org.graalvm.compiler.nodes.calc.FloatingNode;
38 import org.graalvm.compiler.nodes.extended.BoxNode;
39 import org.graalvm.compiler.nodes.extended.UnboxNode;
40 import org.graalvm.compiler.nodes.spi.LoweringTool;
41 import org.graalvm.compiler.options.OptionValues;
42 import org.graalvm.compiler.phases.util.Providers;
43 import org.graalvm.compiler.replacements.SnippetCounter.Group;
44 import org.graalvm.compiler.replacements.SnippetTemplate.AbstractTemplates;
45 import org.graalvm.compiler.replacements.SnippetTemplate.Arguments;
46 import org.graalvm.compiler.replacements.SnippetTemplate.SnippetInfo;
47
48 import jdk.vm.ci.code.TargetDescription;
49 import jdk.vm.ci.meta.ConstantReflectionProvider;
50 import jdk.vm.ci.meta.JavaConstant;
51 import jdk.vm.ci.meta.JavaKind;
52 import jdk.vm.ci.meta.MetaAccessProvider;
53
168 sourceConstant = JavaConstant.forShort((short) sourceConstant.asLong());
169 break;
170 }
171 }
172 JavaConstant boxedConstant = constantReflection.boxPrimitive(sourceConstant);
173 if (boxedConstant != null && sourceConstant.getJavaKind() == box.getBoxingKind()) {
174 return ConstantNode.forConstant(boxedConstant, metaAccess, box.graph());
175 }
176 }
177 return null;
178 }
179
180 public static class Templates extends AbstractTemplates {
181
182 private final EnumMap<JavaKind, SnippetInfo> boxSnippets = new EnumMap<>(JavaKind.class);
183 private final EnumMap<JavaKind, SnippetInfo> unboxSnippets = new EnumMap<>(JavaKind.class);
184
185 private final SnippetCounter valueOfCounter;
186 private final SnippetCounter valueCounter;
187
188 public Templates(OptionValues options, Iterable<DebugHandlersFactory> factories, SnippetCounter.Group.Factory factory, Providers providers, SnippetReflectionProvider snippetReflection,
189 TargetDescription target) {
190 super(options, factories, providers, snippetReflection, target);
191 for (JavaKind kind : new JavaKind[]{JavaKind.Boolean, JavaKind.Byte, JavaKind.Char, JavaKind.Double, JavaKind.Float, JavaKind.Int, JavaKind.Long, JavaKind.Short}) {
192 boxSnippets.put(kind, snippet(BoxingSnippets.class, kind.getJavaName() + "ValueOf"));
193 unboxSnippets.put(kind, snippet(BoxingSnippets.class, kind.getJavaName() + "Value"));
194 }
195 Group group = factory.createSnippetCounterGroup("Boxing");
196 valueOfCounter = new SnippetCounter(group, "valueOf", "box intrinsification");
197 valueCounter = new SnippetCounter(group, "<kind>Value", "unbox intrinsification");
198 }
199
200 public void lower(BoxNode box, LoweringTool tool) {
201 FloatingNode canonical = canonicalizeBoxing(box, providers.getMetaAccess(), providers.getConstantReflection());
202 // if in AOT mode, we don't want to embed boxed constants.
203 if (canonical != null && !ImmutableCode.getValue(box.getOptions())) {
204 box.graph().replaceFixedWithFloating(box, canonical);
205 } else {
206 Arguments args = new Arguments(boxSnippets.get(box.getBoxingKind()), box.graph().getGuardsStage(), tool.getLoweringStage());
207 args.add("value", box.getValue());
208 args.addConst("valueOfCounter", valueOfCounter);
209
210 SnippetTemplate template = template(box.getDebug(), args);
211 box.getDebug().log("Lowering integerValueOf in %s: node=%s, template=%s, arguments=%s", box.graph(), box, template, args);
212 template.instantiate(providers.getMetaAccess(), box, DEFAULT_REPLACER, args);
213 }
214 }
215
216 public void lower(UnboxNode unbox, LoweringTool tool) {
217 Arguments args = new Arguments(unboxSnippets.get(unbox.getBoxingKind()), unbox.graph().getGuardsStage(), tool.getLoweringStage());
218 args.add("value", unbox.getValue());
219 args.addConst("valueCounter", valueCounter);
220
221 SnippetTemplate template = template(unbox.getDebug(), args);
222 unbox.getDebug().log("Lowering integerValueOf in %s: node=%s, template=%s, arguments=%s", unbox.graph(), unbox, template, args);
223 template.instantiate(providers.getMetaAccess(), unbox, DEFAULT_REPLACER, args);
224 }
225 }
226 }
|