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     */
013    package org.jikesrvm.compilers.opt.regalloc;
014    
015    import java.util.HashMap;
016    import org.jikesrvm.compilers.opt.ir.IR;
017    import 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     */
023    abstract class SpillCostEstimator {
024    
025      private final HashMap<Register, Double> map = new HashMap<Register, Double>();
026    
027      /**
028       * Return a number that represents an estimate of the relative cost of
029       * spilling register r.
030       */
031      double getCost(Register r) {
032        Double d = map.get(r);
033        if (d == null) {
034          return 0;
035        } else {
036          return d;
037        }
038      }
039    
040      /**
041       * Calculate the estimated cost for each register.
042       */
043      abstract void calculate(IR ir);
044    
045      /**
046       * Update the cost for a particular register.
047       */
048      protected void update(Register r, double delta) {
049        double c = getCost(r);
050        c += delta;
051        map.put(r, c);
052      }
053    }