001/* 002 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. 003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 004 * 005 * This code is free software; you can redistribute it and/or modify it 006 * under the terms of the GNU General Public License version 2 only, as 007 * published by the Free Software Foundation. 008 * 009 * This code is distributed in the hope that it will be useful, but WITHOUT 010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 011 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 012 * version 2 for more details (a copy is included in the LICENSE file that 013 * accompanied this code). 014 * 015 * You should have received a copy of the GNU General Public License version 016 * 2 along with this work; if not, write to the Free Software Foundation, 017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 018 * 019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 020 * or visit www.oracle.com if you need additional information or have any 021 * questions. 022 */ 023package com.oracle.graal.compiler.match; 024 025import static com.oracle.graal.debug.GraalDebugConfig.*; 026 027import java.util.*; 028import java.util.Map.Entry; 029 030import jdk.internal.jvmci.common.*; 031import com.oracle.graal.debug.*; 032import com.oracle.graal.debug.Debug.*; 033import jdk.internal.jvmci.service.*; 034 035import com.oracle.graal.compiler.gen.*; 036import com.oracle.graal.graph.*; 037 038public class MatchRuleRegistry { 039 040 /** 041 * Convert a list of field names into {@link com.oracle.graal.graph.Position} objects that can 042 * be used to read them during a match. The names should already have been confirmed to exist in 043 * the type. 044 * 045 * @param nodeClass 046 * @param names 047 * @return an array of Position objects corresponding to the named fields. 048 */ 049 public static Position[] findPositions(NodeClass<? extends Node> nodeClass, String[] names) { 050 Position[] result = new Position[names.length]; 051 for (int i = 0; i < names.length; i++) { 052 Edges edges = nodeClass.getInputEdges(); 053 for (int e = 0; e < edges.getDirectCount(); e++) { 054 if (names[i].equals(edges.getName(e))) { 055 result[i] = new Position(edges, e, Node.NOT_ITERABLE); 056 } 057 } 058 if (result[i] == null) { 059 throw new JVMCIError("unknown field \"%s\" in class %s", names[i], nodeClass); 060 } 061 } 062 return result; 063 } 064 065 private static final HashMap<Class<? extends NodeLIRBuilder>, Map<Class<? extends Node>, List<MatchStatement>>> registry = new HashMap<>(); 066 067 /** 068 * Collect all the {@link MatchStatement}s defined by the superclass chain of theClass. 069 * 070 * @param theClass 071 * @return the set of {@link MatchStatement}s applicable to theClass. 072 */ 073 public static synchronized Map<Class<? extends Node>, List<MatchStatement>> lookup(Class<? extends NodeLIRBuilder> theClass) { 074 Map<Class<? extends Node>, List<MatchStatement>> result = registry.get(theClass); 075 076 if (result == null) { 077 Map<Class<? extends Node>, List<MatchStatement>> rules = createRules(theClass); 078 registry.put(theClass, rules); 079 assert registry.get(theClass) == rules; 080 result = rules; 081 082 if (LogVerbose.getValue()) { 083 try (Scope s = Debug.scope("MatchComplexExpressions")) { 084 Debug.log("Match rules for %s", theClass.getSimpleName()); 085 for (Entry<Class<? extends Node>, List<MatchStatement>> entry : result.entrySet()) { 086 Debug.log(" For node class: %s", entry.getKey()); 087 for (MatchStatement statement : entry.getValue()) { 088 Debug.log(" %s", statement.getPattern()); 089 } 090 } 091 } 092 } 093 } 094 095 if (result.size() == 0) { 096 return null; 097 } 098 return result; 099 } 100 101 /* 102 * This is a separate, public method so that external clients can create rules with a custom 103 * lookup and without the default caching behavior. 104 */ 105 public static Map<Class<? extends Node>, List<MatchStatement>> createRules(Class<? extends NodeLIRBuilder> theClass) { 106 HashMap<Class<? extends NodeLIRBuilder>, MatchStatementSet> matchSets = new HashMap<>(); 107 Iterable<MatchStatementSet> sl = Services.load(MatchStatementSet.class); 108 for (MatchStatementSet rules : sl) { 109 matchSets.put(rules.forClass(), rules); 110 } 111 112 // Walk the class hierarchy collecting lists and merge them together. The subclass 113 // rules are first which gives them preference over earlier rules. 114 Map<Class<? extends Node>, List<MatchStatement>> rules = new HashMap<>(); 115 Class<?> currentClass = theClass; 116 do { 117 MatchStatementSet matchSet = matchSets.get(currentClass); 118 if (matchSet != null) { 119 List<MatchStatement> statements = matchSet.statements(); 120 for (MatchStatement statement : statements) { 121 Class<? extends Node> nodeClass = statement.getPattern().nodeClass(); 122 List<MatchStatement> current = rules.get(nodeClass); 123 if (current == null) { 124 current = new ArrayList<>(); 125 rules.put(nodeClass, current); 126 } 127 current.add(statement); 128 } 129 } 130 currentClass = currentClass.getSuperclass(); 131 } while (currentClass != NodeLIRBuilder.class); 132 return rules; 133 } 134}