8212956: [JVCMI] SpeculationReason should maintain identity

Reviewed-by: kvn
This commit is contained in:
Tom Rodriguez 2018-10-25 19:00:46 -07:00
parent c24f6506e7
commit ff4cf4f40b
3 changed files with 82 additions and 5 deletions

View File

@ -51,7 +51,7 @@ public class HotSpotSpeculationLog implements SpeculationLog {
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 @@ public class HotSpotSpeculationLog implements SpeculationLog {
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 @@ public class HotSpotSpeculationLog implements SpeculationLog {
}
}
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 @@ public class HotSpotSpeculationLog implements SpeculationLog {
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 @@ public class HotSpotSpeculationLog implements SpeculationLog {
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);
}

View File

@ -58,6 +58,20 @@ public interface SpeculationLog {
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());

View File

@ -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));
}
}