changeset 52298:83039b8e6a42

8212956: [JVCMI] SpeculationReason should maintain identity Reviewed-by: kvn
author never
date Thu, 25 Oct 2018 19:00:46 -0700
parents 99962c340e73
children 17826b492ddd
files src/jdk.internal.vm.ci/share/classes/jdk.vm.ci.hotspot/src/jdk/vm/ci/hotspot/HotSpotSpeculationLog.java src/jdk.internal.vm.ci/share/classes/jdk.vm.ci.meta/src/jdk/vm/ci/meta/SpeculationLog.java test/hotspot/jtreg/compiler/jvmci/jdk.vm.ci.runtime.test/src/jdk/vm/ci/runtime/test/TestSpeculationLog.java
diffstat 3 files changed, 82 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/src/jdk.internal.vm.ci/share/classes/jdk.vm.ci.hotspot/src/jdk/vm/ci/hotspot/HotSpotSpeculationLog.java	Thu Oct 25 18:41:26 2018 -0700
+++ b/src/jdk.internal.vm.ci/share/classes/jdk.vm.ci.hotspot/src/jdk/vm/ci/hotspot/HotSpotSpeculationLog.java	Thu Oct 25 19:00:46 2018 -0700
@@ -51,7 +51,7 @@
     private Set<SpeculationReason> failedSpeculations;
 
     /** Strong references to all reasons embedded in the current nmethod. */
-    private Map<Long, SpeculationReason> speculations;
+    private HashMap<SpeculationReason, JavaConstant> speculations;
 
     private long currentSpeculationID;
 
@@ -62,7 +62,7 @@
                 failedSpeculations = new HashSet<>(2);
             }
             if (speculations != null) {
-                SpeculationReason lastFailedSpeculation = speculations.get(lastFailed);
+                SpeculationReason lastFailedSpeculation = lookupSpeculation(this.lastFailed);
                 if (lastFailedSpeculation != null) {
                     failedSpeculations.add(lastFailedSpeculation);
                 }
@@ -72,6 +72,15 @@
         }
     }
 
+    private SpeculationReason lookupSpeculation(long value) {
+        for (Map.Entry<SpeculationReason, JavaConstant> entry : speculations.entrySet()) {
+            if (value == entry.getValue().asLong()) {
+                return entry.getKey();
+            }
+        }
+        return null;
+    }
+
     @Override
     public synchronized boolean maySpeculate(SpeculationReason reason) {
         if (failedSpeculations != null && failedSpeculations.contains(reason)) {
@@ -85,8 +94,12 @@
         if (speculations == null) {
             speculations = new HashMap<>();
         }
-        speculations.put(++currentSpeculationID, reason);
-        return new HotSpotSpeculation(reason, JavaConstant.forLong(currentSpeculationID));
+        JavaConstant id = speculations.get(reason);
+        if (id == null) {
+            id = JavaConstant.forLong(++currentSpeculationID);
+            speculations.put(reason, id);
+        }
+        return new HotSpotSpeculation(reason, id);
     }
 
     @Override
@@ -99,7 +112,7 @@
         if (constant.isDefaultForKind()) {
             return NO_SPECULATION;
         }
-        SpeculationReason reason = speculations.get(constant.asLong());
+        SpeculationReason reason = lookupSpeculation(constant.asLong());
         assert reason != null : "Speculation should have been registered";
         return new HotSpotSpeculation(reason, constant);
     }
--- a/src/jdk.internal.vm.ci/share/classes/jdk.vm.ci.meta/src/jdk/vm/ci/meta/SpeculationLog.java	Thu Oct 25 18:41:26 2018 -0700
+++ b/src/jdk.internal.vm.ci/share/classes/jdk.vm.ci.meta/src/jdk/vm/ci/meta/SpeculationLog.java	Thu Oct 25 19:00:46 2018 -0700
@@ -58,6 +58,20 @@
         public String toString() {
             return reason.toString();
         }
+
+        @Override
+        public boolean equals(Object obj) {
+            if (obj instanceof Speculation) {
+                Speculation other = (Speculation) obj;
+                return reason.equals(other.reason);
+            }
+            return false;
+        }
+
+        @Override
+        public int hashCode() {
+            return getReason().hashCode();
+        }
     }
 
     Speculation NO_SPECULATION = new Speculation(new NoSpeculationReason());
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/hotspot/jtreg/compiler/jvmci/jdk.vm.ci.runtime.test/src/jdk/vm/ci/runtime/test/TestSpeculationLog.java	Thu Oct 25 19:00:46 2018 -0700
@@ -0,0 +1,50 @@
+/*
+ * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package jdk.vm.ci.runtime.test;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import jdk.vm.ci.meta.JavaConstant;
+import jdk.vm.ci.meta.SpeculationLog;
+
+public class TestSpeculationLog extends MethodUniverse {
+
+    static class Dummy implements SpeculationLog.SpeculationReason {
+
+    }
+
+    @Test
+    public void testSpeculationIdentity() {
+        Dummy spec = new Dummy();
+        SpeculationLog log = methods.entrySet().iterator().next().getValue().getSpeculationLog();
+        SpeculationLog.Speculation s1 = log.speculate(spec);
+        SpeculationLog.Speculation s2 = log.speculate(spec);
+        Assert.assertTrue("Speculation should maintain identity", s1.equals(s2));
+        JavaConstant e1 = metaAccess.encodeSpeculation(s1);
+        JavaConstant e2 = metaAccess.encodeSpeculation(s2);
+        Assert.assertTrue("speculation encoding should maintain identity", e1.equals(e2));
+    }
+}