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 
  24 
  25 package jdk.internal.vm.compiler.word;
  26 
  27 // JaCoCo Exclude
  28 
  29 /**
  30  * Marker interface for location identities. A different location identity of two memory accesses
  31  * guarantees that the two accesses do not interfere.
  32  * <p>
  33  * Clients of {@link LocationIdentity} must use {@link #equals(Object)}, not {@code ==}, when
  34  * comparing two {@link LocationIdentity} values for equality. Likewise, they must not use
  35  * {@link java.util.IdentityHashMap}s with {@link LocationIdentity} values as keys.
  36  *
  37  * @since 1.0
  38  */
  39 public abstract class LocationIdentity {
  40 
  41     private static final class AnyLocationIdentity extends LocationIdentity {
  42         @Override
  43         public boolean isImmutable() {
  44             return false;
  45         }
  46 
  47         @Override
  48         public String toString() {
  49             return "ANY_LOCATION";
  50         }
  51     }
  52 
  53     private static final class InitLocationIdentity extends LocationIdentity {
  54         @Override
  55         public boolean isImmutable() {
  56             return true;
  57         }
  58 
  59         @Override
  60         public String toString() {
  61             return "INIT_LOCATION";
  62         }
  63     }
  64 
  65     /**
  66      * Creates a new location identity. Subclasses are responsible to provide proper implementations
  67      * of {@link #equals} and {@link #hashCode}.
  68      *
  69      * @since 1.0
  70      */
  71     protected LocationIdentity() {
  72     }
  73 
  74     /**
  75      * Indicates that the given location is the union of all possible mutable locations. A write to
  76      * such a location kill all reads from mutable locations and a read from this location is killed
  77      * by any write (except for initialization writes).
  78      *
  79      * @since 1.0
  80      */
  81     public static final LocationIdentity ANY_LOCATION = new AnyLocationIdentity();
  82 
  83     /**
  84      * Location only allowed to be used for writes. Indicates that a completely new memory location
  85      * is written. Kills no read. The previous value at the given location must be either
  86      * uninitialized or null. Writes to this location do not need a GC pre-barrier.
  87      *
  88      * @since 1.0
  89      */
  90     public static final LocationIdentity INIT_LOCATION = new InitLocationIdentity();
  91 
  92     /**
  93      * Indicates that the given location is the union of all possible mutable locations. A write to
  94      * such a location kill all reads from mutable locations and a read from this location is killed
  95      * by any write (except for initialization writes).
  96      *
  97      * @since 1.0
  98      */
  99     public static LocationIdentity any() {
 100         return ANY_LOCATION;
 101     }
 102 
 103     /**
 104      * Location only allowed to be used for writes. Indicates that a completely new memory location
 105      * is written. Kills no read. The previous value at the given location must be either
 106      * uninitialized or null. Writes to this location do not need a GC pre-barrier.
 107      *
 108      * @since 1.0
 109      */
 110     public static LocationIdentity init() {
 111         return INIT_LOCATION;
 112     }
 113 
 114     /**
 115      * Denotes a location is unchanging in all cases. Not that this is different than the Java
 116      * notion of final which only requires definite assignment.
 117      *
 118      * @since 1.0
 119      */
 120     public abstract boolean isImmutable();
 121 
 122     /**
 123      * The inversion of {@link #isImmutable}.
 124      *
 125      * @since 1.0
 126      */
 127     public final boolean isMutable() {
 128         return !isImmutable();
 129     }
 130 
 131     /**
 132      * Returns true if this location identity is {@link #any}.
 133      *
 134      * @since 1.0
 135      */
 136     public final boolean isAny() {
 137         return this == ANY_LOCATION;
 138     }
 139 
 140     /**
 141      * Returns true if this location identity is {@link #init}.
 142      *
 143      * @since 1.0
 144      */
 145     public final boolean isInit() {
 146         return this == INIT_LOCATION;
 147     }
 148 
 149     /**
 150      * Returns true if this location identity is not {@link #any}.
 151      *
 152      * @since 1.0
 153      */
 154     public final boolean isSingle() {
 155         return this != ANY_LOCATION;
 156     }
 157 
 158     /**
 159      * Returns true if the memory slice denoted by this location identity may overlap with the
 160      * provided other location identity.
 161      *
 162      * @since 1.0
 163      */
 164     public final boolean overlaps(LocationIdentity other) {
 165         return isAny() || other.isAny() || this.equals(other);
 166     }
 167 }