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.compilers.opt.regalloc;
014
015import org.jikesrvm.VM;
016import org.jikesrvm.compilers.opt.OptOptions;
017import org.jikesrvm.compilers.opt.OptimizingCompilerException;
018import org.jikesrvm.compilers.opt.driver.CompilerPhase;
019import org.jikesrvm.compilers.opt.ir.GCIRMapElement;
020import org.jikesrvm.compilers.opt.ir.IR;
021import org.jikesrvm.compilers.opt.ir.RegSpillListElement;
022import org.jikesrvm.compilers.opt.ir.Register;
023
024/**
025 * Update GC maps after register allocation but before inserting spill
026 * code.
027 */
028final class UpdateGCMaps1 extends CompilerPhase {
029
030  @Override
031  public boolean shouldPerform(OptOptions options) {
032    return true;
033  }
034
035  /**
036   * Return this instance of this phase. This phase contains no
037   * per-compilation instance fields.
038   * @param ir not used
039   * @return this
040   */
041  @Override
042  public CompilerPhase newExecution(IR ir) {
043    return this;
044  }
045
046  @Override
047  public String getName() {
048    return "Update GCMaps 1";
049  }
050
051  @Override
052  public boolean printingEnabled(OptOptions options, boolean before) {
053    return false;
054  }
055
056  /**
057   *  Iterate over the IR-based GC map collection and for each entry
058   *  replace the symbolic reg with the real reg or spill it was allocated
059   *  @param ir the IR
060   */
061  @Override
062  public void perform(IR ir) {
063    RegisterAllocatorState regAllocState = ir.MIRInfo.regAllocState;
064
065    for (GCIRMapElement GCelement : ir.MIRInfo.gcIRMap) {
066      if (LinearScan.GC_DEBUG) {
067        VM.sysWrite("GCelement " + GCelement);
068      }
069
070      for (RegSpillListElement elem : GCelement.regSpillList()) {
071        Register symbolic = elem.getSymbolicReg();
072
073        if (LinearScan.GC_DEBUG) {
074          VM.sysWrite("get location for " + symbolic + '\n');
075        }
076
077        if (symbolic.isAllocated()) {
078          Register ra = regAllocState.getMapping(symbolic);
079          elem.setRealReg(ra);
080          if (LinearScan.GC_DEBUG) {
081            VM.sysWrite(ra + "\n");
082          }
083
084        } else if (symbolic.isSpilled()) {
085          int spill = ir.MIRInfo.regAllocState.getSpill(symbolic);
086          elem.setSpill(spill);
087          if (LinearScan.GC_DEBUG) {
088            VM.sysWrite(spill + "\n");
089          }
090        } else {
091          OptimizingCompilerException.UNREACHABLE("LinearScan", "register not alive:", symbolic.toString());
092        }
093      }
094    }
095  }
096}