001/*
002 *  This file is part of the Jikes RVM project (http://jikesrvm.org).
003 *
004 *  This file is licensed to You under the Eclipse Public License (EPL);
005 *  You may not use this file except in compliance with the License. You
006 *  may obtain a copy of the License at
007 *
008 *      http://www.opensource.org/licenses/eclipse-1.0.php
009 *
010 *  See the COPYRIGHT.txt file distributed with this work for information
011 *  regarding copyright ownership.
012 */
013package org.jikesrvm.adaptive;
014
015import org.jikesrvm.compilers.common.CompiledMethod;
016import org.jikesrvm.compilers.common.CompiledMethods;
017import org.jikesrvm.runtime.Magic;
018import org.jikesrvm.scheduler.RVMThread;
019import org.vmmagic.pragma.Unpreemptible;
020import org.vmmagic.unboxed.Address;
021import org.vmmagic.unboxed.Offset;
022
023/**
024 * Code invoked from Thread.yieldpoint for the purposes of OSR.
025 */
026@Unpreemptible
027public class OSRListener {
028
029  public static boolean checkForOSRPromotion(int whereFrom, Address yieldpointServiceMethodFP) {
030    if (RVMThread.getCurrentThread().isSystemThread()) return false;
031
032    if (whereFrom != RVMThread.BACKEDGE) return false;
033
034    // See if we are at a loop backedge in an outdated baseline compiled method
035
036    Address fp = yieldpointServiceMethodFP;
037    fp = Magic.getCallerFramePointer(fp);
038    int ypTakenInCMID = Magic.getCompiledMethodID(fp);
039    CompiledMethod ypTakenInCM = CompiledMethods.getCompiledMethod(ypTakenInCMID);
040    if (ypTakenInCM.isOutdated() && ypTakenInCM.getCompilerType() == CompiledMethod.BASELINE) {
041      Address tsFromFP = yieldpointServiceMethodFP;
042      Address realFP = Magic.getCallerFramePointer(tsFromFP);
043
044      Address stackbeg = Magic.objectAsAddress(RVMThread.getCurrentThread().getStack());
045
046      Offset tsFromFPoff = tsFromFP.diff(stackbeg);
047      Offset realFPoff = realFP.diff(stackbeg);
048
049      OnStackReplacementTrigger.trigger(ypTakenInCMID, tsFromFPoff, realFPoff, whereFrom);
050      return true;
051    }
052    return false;
053  }
054
055  public static void handleOSRFromOpt(Address yieldpointServiceMethodFP) {
056    Address tsFromFP = yieldpointServiceMethodFP;
057    Address realFP = Magic.getCallerFramePointer(tsFromFP);
058    int ypTakenInCMID = Magic.getCompiledMethodID(realFP);
059    Address stackbeg = Magic.objectAsAddress(RVMThread.getCurrentThread().getStack());
060
061    Offset tsFromFPoff = tsFromFP.diff(stackbeg);
062    Offset realFPoff = realFP.diff(stackbeg);
063
064    OnStackReplacementTrigger.trigger(ypTakenInCMID, tsFromFPoff, realFPoff, RVMThread.OSROPT);
065  }
066}