1 /* 2 * Copyright (c) 2011, 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.nodes; 24 25 import java.util.EnumMap; 26 import java.util.HashSet; 27 28 import org.graalvm.compiler.core.common.LocationIdentity; 29 30 import jdk.vm.ci.meta.JavaKind; 31 import jdk.vm.ci.meta.JavaKind.FormatWithToString; 32 33 /** 34 * A {@link LocationIdentity} with a name. 35 */ 36 public class NamedLocationIdentity extends LocationIdentity implements FormatWithToString { 37 38 /** 39 * Map for asserting all {@link NamedLocationIdentity} instances have a unique name. 40 */ 41 static class DB { 42 private static final HashSet<String> map = new HashSet<>(); 43 44 static boolean checkUnique(String name) { 45 if (!map.add(name)) { 46 throw new AssertionError("identity " + name + " already exists"); 47 } 48 return true; 49 } 50 } 51 52 /** 53 * Denotes the location of a value that is guaranteed to be unchanging. 54 */ 55 public static final LocationIdentity FINAL_LOCATION = NamedLocationIdentity.immutable("FINAL_LOCATION"); 56 57 /** 58 * Denotes the location of the length field of a Java array. 59 */ 60 public static final LocationIdentity ARRAY_LENGTH_LOCATION = NamedLocationIdentity.immutable("[].length"); 61 62 public static LocationIdentity any() { 63 return ANY_LOCATION; 64 } 65 66 private final String name; 67 private final boolean immutable; 68 69 protected NamedLocationIdentity(String name, boolean immutable) { 70 this.name = name; 71 this.immutable = immutable; 72 assert DB.checkUnique(name); 73 } 74 75 /** 76 * Creates a named unique location identity for read and write operations against mutable 77 * memory. 78 * 79 * @param name the name of the new location identity 80 */ 81 public static NamedLocationIdentity mutable(String name) { 82 return create(name, false); 83 } 84 85 /** 86 * Creates a named unique location identity for read operations against immutable memory. 87 * Immutable memory will never have a visible write in the graph, which is more restictive than 88 * Java final. 89 * 90 * @param name the name of the new location identity 91 */ 92 public static NamedLocationIdentity immutable(String name) { 93 return create(name, true); 94 } 95 96 /** 97 * Creates a named unique location identity for read and write operations. 98 * 99 * @param name the name of the new location identity 100 * @param immutable true if the location is immutable 101 */ 102 private static NamedLocationIdentity create(String name, boolean immutable) { 103 return new NamedLocationIdentity(name, immutable); 104 } 105 106 @Override 107 public boolean isImmutable() { 108 return immutable; 109 } 110 111 @Override 112 public String toString() { 113 return name + (isImmutable() ? ":final" : ""); 114 } 115 116 /** 117 * Returns the named location identity for an array of the given element kind. Array accesses of 118 * the same kind must have the same location identity unless an alias analysis guarantees that 119 * two distinct arrays are accessed. 120 */ 121 public static LocationIdentity getArrayLocation(JavaKind elementKind) { 122 return ARRAY_LOCATIONS.get(elementKind); 123 } 124 125 private static final EnumMap<JavaKind, LocationIdentity> ARRAY_LOCATIONS = initArrayLocations(); 126 127 private static EnumMap<JavaKind, LocationIdentity> initArrayLocations() { 128 EnumMap<JavaKind, LocationIdentity> result = new EnumMap<>(JavaKind.class); 129 for (JavaKind kind : JavaKind.values()) { 130 result.put(kind, NamedLocationIdentity.mutable("Array: " + kind.getJavaName())); 131 } 132 return result; 133 } 134 }