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