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.Enumeration;
016    import org.jikesrvm.ArchitectureSpecificOpt.PhysicalRegisterSet;
017    import org.jikesrvm.compilers.opt.ir.IR;
018    import org.jikesrvm.compilers.opt.ir.Register;
019    
020    /**
021     * The register allocator currently caches a bunch of state in the IR;
022     * This class provides accessors to this state.
023     * TODO: Consider caching the state in a lookaside structure.
024     * TODO: Currently, the physical registers are STATIC! fix this.
025     */
026    public class RegisterAllocatorState {
027    
028      /**
029       *  Resets the physical register info
030       */
031      static void resetPhysicalRegisters(IR ir) {
032        PhysicalRegisterSet phys = ir.regpool.getPhysicalRegisterSet();
033        for (Enumeration<Register> e = phys.enumerateAll(); e.hasMoreElements();) {
034          Register reg = e.nextElement();
035          reg.deallocateRegister();
036          reg.mapsToRegister = null;  // mapping from real to symbolic
037          //    putPhysicalRegResurrectList(reg, null);
038          reg.defList = null;
039          reg.useList = null;
040          setSpill(reg, 0);
041        }
042      }
043    
044      /**
045       * Special use of scratchObject field as "resurrect lists" for real registers
046       * TODO: use another field for safety; scratchObject is also used by
047       *  clan LinearScanLiveAnalysis
048       */
049      /*
050      static void putPhysicalRegResurrectList(Register r,
051                                              LinearScanLiveInterval li) {
052        if (VM.VerifyAssertions) VM._assert(r.isPhysical());
053        r.scratchObject = li;
054      }
055      */
056      /**
057       *
058       * Special use of scratchObject field as "resurrect lists" for real registers
059       * TODO: use another field for safety; scratchObject is also used by
060       *  clan LinearScanLiveAnalysis
061       */
062      /*
063      static LinearScanLiveInterval getPhysicalRegResurrectList(Register r) {
064        if (VM.VerifyAssertions) VM._assert(r.isPhysical());
065        return (LinearScanLiveInterval) r.scratchObject;
066      }
067      */
068      static void setSpill(Register reg, int spill) {
069        reg.spillRegister();
070        reg.scratch = spill;
071      }
072    
073      public static int getSpill(Register reg) {
074        return reg.scratch;
075      }
076    
077      /**
078       * Record that register A and register B are associated with each other
079       * in a bijection.
080       *
081       * The register allocator uses this state to indicate that a symbolic
082       * register is presently allocated to a physical register.
083       */
084      static void mapOneToOne(Register A, Register B) {
085        Register aFriend = getMapping(A);
086        Register bFriend = getMapping(B);
087        if (aFriend != null) {
088          aFriend.mapsToRegister = null;
089        }
090        if (bFriend != null) {
091          bFriend.mapsToRegister = null;
092        }
093        A.mapsToRegister = B;
094        B.mapsToRegister = A;
095      }
096    
097      /**
098       * @return the register currently mapped 1-to-1 to r
099       */
100      static Register getMapping(Register r) {
101        return r.mapsToRegister;
102      }
103    
104      /**
105       * Clear any 1-to-1 mapping for register R.
106       */
107      static void clearOneToOne(Register r) {
108        if (r != null) {
109          Register s = getMapping(r);
110          if (s != null) {
111            s.mapsToRegister = null;
112          }
113          r.mapsToRegister = null;
114        }
115      }
116    }