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 java.util.HashMap;
016import org.jikesrvm.compilers.opt.ir.IR;
017import org.jikesrvm.compilers.opt.ir.Register;
018
019/**
020 * An object that returns an estimate of the relative cost of spilling a
021 * symbolic register.
022 */
023abstract class SpillCostEstimator {
024
025  private final HashMap<Register, Double> map = new HashMap<Register, Double>();
026
027  /**
028   * Returns a number that represents an estimate of the relative cost of
029   * spilling register {@code r}.
030   *
031   * @param r the register to check
032   * @return a cost estimate for spilling; may be zero
033   */
034  double getCost(Register r) {
035    Double d = map.get(r);
036    if (d == null) {
037      return 0;
038    } else {
039      return d;
040    }
041  }
042
043  /**
044   * Calculates the estimated cost for each register.
045   *
046   * @param ir the IR object
047   */
048  abstract void calculate(IR ir);
049
050  /**
051   * Updates the cost for a particular register.
052   *
053   * @param r register whose cost is to be updated
054   * @param delta change in cost for the register
055   */
056  protected void update(Register r, double delta) {
057    double c = getCost(r);
058    c += delta;
059    map.put(r, c);
060  }
061}