--- old/src/jdk.vm.ci/share/classes/jdk.vm.ci.hotspot/src/jdk/vm/ci/hotspot/HotSpotSpeculationLog.java 2015-11-02 17:06:16.000000000 -1000 +++ new/src/jdk.vm.ci/share/classes/jdk.vm.ci.hotspot/src/jdk/vm/ci/hotspot/HotSpotSpeculationLog.java 2015-11-02 17:06:16.000000000 -1000 @@ -22,13 +22,63 @@ */ package jdk.vm.ci.hotspot; -import jdk.vm.ci.meta.*; +import java.util.Collection; +import java.util.HashSet; +import java.util.Set; +import java.util.concurrent.ConcurrentLinkedQueue; -public class HotSpotSpeculationLog extends SpeculationLog { +import jdk.vm.ci.meta.JavaConstant; +import jdk.vm.ci.meta.SpeculationLog; + +public class HotSpotSpeculationLog implements SpeculationLog { + + /** Written by the C++ code that performs deoptimization. */ + private volatile Object lastFailed; + + /** All speculations that have been a deoptimization reason. */ + private Set failedSpeculations; + + /** Strong references to all reasons embededded in the current nmethod. */ + private volatile Collection speculations; + + @Override + public synchronized void collectFailedSpeculations() { + if (lastFailed != null) { + if (failedSpeculations == null) { + failedSpeculations = new HashSet<>(2); + } + failedSpeculations.add((SpeculationReason) lastFailed); + lastFailed = null; + speculations = null; + } + } + + @Override + public boolean maySpeculate(SpeculationReason reason) { + if (failedSpeculations != null && failedSpeculations.contains(reason)) { + return false; + } + return true; + } @Override - public JavaConstant speculate(Object reason) { - addSpeculation(reason); + public JavaConstant speculate(SpeculationReason reason) { + assert maySpeculate(reason); + + /* + * Objects referenced from nmethods are weak references. We need a strong reference to the + * reason objects that are embedded in nmethods, so we add them to the speculations + * collection. + */ + if (speculations == null) { + synchronized (this) { + if (speculations == null) { + speculations = new ConcurrentLinkedQueue<>(); + } + } + } + speculations.add(reason); + return HotSpotObjectConstantImpl.forObject(reason); } }