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.compilers.opt.OptOptions;
016import org.jikesrvm.compilers.opt.driver.CompilerPhase;
017import org.jikesrvm.compilers.opt.driver.OptimizationPlanAtomicElement;
018import org.jikesrvm.compilers.opt.driver.OptimizationPlanCompositeElement;
019import org.jikesrvm.compilers.opt.driver.OptimizationPlanElement;
020import org.jikesrvm.compilers.opt.ir.IR;
021
022/**
023 * Driver routine for register allocation
024 */
025public final class RegisterAllocator extends OptimizationPlanCompositeElement {
026
027  public RegisterAllocator() {
028    super("Register Allocation", new OptimizationPlanElement[]{
029        // 1. Prepare for the allocation
030        new OptimizationPlanAtomicElement(new RegisterAllocPreparation()),
031        // 2. Perform the allocation, using the live information
032        new LinearScan()});
033  }
034
035  @Override
036  public boolean shouldPerform(OptOptions options) {
037    return true;
038  }
039
040  @Override
041  public String getName() {
042    return "RegAlloc";
043  }
044
045  @Override
046  public boolean printingEnabled(OptOptions options, boolean before) {
047    return options.PRINT_REGALLOC;
048  }
049
050  private static class RegisterAllocPreparation extends CompilerPhase {
051    @Override
052    public final boolean shouldPerform(OptOptions options) {
053      return true;
054    }
055
056    /**
057     * Return this instance of this phase. This phase contains no
058     * per-compilation instance fields.
059     * @param ir not used
060     * @return this
061     */
062    @Override
063    public CompilerPhase newExecution(IR ir) {
064      return this;
065    }
066
067    @Override
068    public final String getName() {
069      return "Register Allocation Preparation";
070    }
071
072    @Override
073    public final void perform(org.jikesrvm.compilers.opt.ir.IR ir) {
074      int registerCount = ir.regpool.getTotalNumberOfRegisters();
075      ir.MIRInfo.regAllocState = new RegisterAllocatorState(registerCount);
076      ir.stackManager.prepare(ir);
077    }
078  }
079}