1 /* 2 * Copyright (c) 2015, 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 */ 23 package org.graalvm.compiler.replacements; 24 25 import org.graalvm.compiler.api.replacements.SnippetReflectionProvider; 26 import org.graalvm.compiler.core.common.type.StampPair; 27 import org.graalvm.compiler.nodes.ConstantNode; 28 import org.graalvm.compiler.nodes.calc.FloatingNode; 29 import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderTool; 30 import org.graalvm.compiler.nodes.graphbuilderconf.ParameterPlugin; 31 32 import jdk.vm.ci.meta.Constant; 33 import jdk.vm.ci.meta.MetaAccessProvider; 34 35 /** 36 * A {@link ParameterPlugin} that binds constant values to some parameters. 37 */ 38 public class ConstantBindingParameterPlugin implements ParameterPlugin { 39 private final Object[] constantArgs; 40 private final MetaAccessProvider metaAccess; 41 private final SnippetReflectionProvider snippetReflection; 42 43 /** 44 * Creates a plugin that will create {@link ConstantNode}s for each parameter with an index 45 * equal to that of a non-null object in {@code constantArgs} (from which the 46 * {@link ConstantNode} is created if it isn't already a {@link ConstantNode}). 47 */ 48 public ConstantBindingParameterPlugin(Object[] constantArgs, MetaAccessProvider metaAccess, SnippetReflectionProvider snippetReflection) { 49 this.constantArgs = constantArgs; 50 this.metaAccess = metaAccess; 51 this.snippetReflection = snippetReflection; 52 } 53 54 @Override 55 public FloatingNode interceptParameter(GraphBuilderTool b, int index, StampPair stamp) { 56 Object arg = constantArgs[index]; 57 if (arg != null) { 58 ConstantNode constantNode; 59 if (arg instanceof ConstantNode) { 60 ConstantNode otherCon = (ConstantNode) arg; 61 if (otherCon.graph() != b.getGraph()) { 62 /* 63 * This is a node from another graph, so copy over extra state into a new 64 * ConstantNode. 65 */ 66 constantNode = ConstantNode.forConstant(stamp.getTrustedStamp(), otherCon.getValue(), otherCon.getStableDimension(), otherCon.isDefaultStable(), metaAccess); 67 } else { 68 constantNode = otherCon; 69 } 70 } else if (arg instanceof Constant) { 71 constantNode = ConstantNode.forConstant(stamp.getTrustedStamp(), (Constant) arg, metaAccess); 72 } else { 73 constantNode = ConstantNode.forConstant(snippetReflection.forBoxed(stamp.getTrustedStamp().getStackKind(), arg), metaAccess); 74 } 75 return constantNode; 76 } 77 return null; 78 } 79 }