1 /*
   2  * Copyright (c) 2014, 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.core.match;
  26 
  27 import static org.graalvm.compiler.debug.DebugOptions.LogVerbose;
  28 
  29 import org.graalvm.compiler.core.gen.NodeLIRBuilder;
  30 import org.graalvm.compiler.core.match.MatchPattern.MatchResultCode;
  31 import org.graalvm.compiler.core.match.MatchPattern.Result;
  32 import org.graalvm.compiler.debug.CounterKey;
  33 import org.graalvm.compiler.debug.DebugContext;
  34 import org.graalvm.compiler.graph.GraalGraphError;
  35 import org.graalvm.compiler.graph.Node;
  36 import org.graalvm.compiler.nodeinfo.Verbosity;
  37 
  38 import jdk.vm.ci.meta.Value;
  39 import org.graalvm.compiler.nodes.StructuredGraph;
  40 import org.graalvm.compiler.nodes.cfg.Block;
  41 
  42 /**
  43  * A named {@link MatchPattern} along with a {@link MatchGenerator} that can be evaluated to replace
  44  * one or more {@link Node}s with a single {@link Value}.
  45  */
  46 
  47 public class MatchStatement {
  48     private static final CounterKey MatchStatementSuccess = DebugContext.counter("MatchStatementSuccess");
  49 
  50     /**
  51      * A printable name for this statement. Usually it's just the name of the method doing the
  52      * emission.
  53      */
  54     private final String name;
  55 
  56     /**
  57      * The actual match pattern.
  58      */
  59     private final MatchPattern pattern;
  60 
  61     /**
  62      * The method in the {@link NodeLIRBuilder} subclass that will actually do the code emission.
  63      */
  64     private MatchGenerator generatorMethod;
  65 
  66     /**
  67      * The name of arguments in the order they are expected to be passed to the generator method.
  68      */
  69     private String[] arguments;
  70 
  71     public MatchStatement(String name, MatchPattern pattern, MatchGenerator generator, String[] arguments) {
  72         this.name = name;
  73         this.pattern = pattern;
  74         this.generatorMethod = generator;
  75         this.arguments = arguments;
  76     }
  77 
  78     /**
  79      * Attempt to match the current statement against a Node.
  80      *
  81      * @param builder the current builder instance.
  82      * @param node the node to be matched
  83      * @param block the current block
  84      * @param schedule the schedule that's being used
  85      * @return true if the statement matched something and set a {@link ComplexMatchResult} to be
  86      *         evaluated by the NodeLIRBuilder.
  87      */
  88     public boolean generate(NodeLIRBuilder builder, int index, Node node, Block block, StructuredGraph.ScheduleResult schedule) {
  89         DebugContext debug = node.getDebug();
  90         assert index == schedule.getBlockToNodesMap().get(block).indexOf(node);
  91         // Check that the basic shape matches
  92         Result result = pattern.matchShape(node, this);
  93         if (result != Result.OK) {
  94             return false;
  95         }
  96         // Now ensure that the other safety constraints are matched.
  97         MatchContext context = new MatchContext(builder, this, index, node, block, schedule);
  98         result = pattern.matchUsage(node, context);
  99         if (result == Result.OK) {
 100             // Invoke the generator method and set the result if it's non null.
 101             ComplexMatchResult value = generatorMethod.match(builder.getNodeMatchRules(), buildArgList(context));
 102             if (value != null) {
 103                 context.setResult(value);
 104                 MatchStatementSuccess.increment(debug);
 105                 DebugContext.counter("MatchStatement[%s]", getName()).increment(debug);
 106                 return true;
 107             }
 108             // The pattern matched but some other code generation constraint disallowed code
 109             // generation for the pattern.
 110             if (LogVerbose.getValue(node.getOptions())) {
 111                 debug.log("while matching %s|%s %s %s returned null", context.getRoot().toString(Verbosity.Id), context.getRoot().getClass().getSimpleName(), getName(), generatorMethod.getName());
 112                 debug.log("with nodes %s", formatMatch(node));
 113             }
 114         } else {
 115             if (LogVerbose.getValue(node.getOptions()) && result.code != MatchResultCode.WRONG_CLASS) {
 116                 debug.log("while matching %s|%s %s %s", context.getRoot().toString(Verbosity.Id), context.getRoot().getClass().getSimpleName(), getName(), result);
 117             }
 118         }
 119         return false;
 120     }
 121 
 122     /**
 123      * @param context
 124      * @return the Nodes captured by the match rule in the order expected by the generatorMethod
 125      */
 126     private Object[] buildArgList(MatchContext context) {
 127         Object[] result = new Object[arguments.length];
 128         for (int i = 0; i < arguments.length; i++) {
 129             if ("root".equals(arguments[i])) {
 130                 result[i] = context.getRoot();
 131             } else {
 132                 result[i] = context.namedNode(arguments[i]);
 133                 if (result[i] == null) {
 134                     throw new GraalGraphError("Can't find named node %s", arguments[i]);
 135                 }
 136             }
 137         }
 138         return result;
 139     }
 140 
 141     public String formatMatch(Node root) {
 142         return pattern.formatMatch(root);
 143     }
 144 
 145     public MatchPattern getPattern() {
 146         return pattern;
 147     }
 148 
 149     public String getName() {
 150         return name;
 151     }
 152 
 153     @Override
 154     public String toString() {
 155         return pattern.toString();
 156     }
 157 }