LLVM API Documentation

VirtRegMap.h

Go to the documentation of this file.
00001 //===-- llvm/CodeGen/VirtRegMap.h - Virtual Register Map -*- C++ -*--------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file was developed by the LLVM research group and is distributed under
00006 // the University of Illinois Open Source License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file implements a virtual register map. This maps virtual registers to
00011 // physical registers and virtual registers to stack slots. It is created and
00012 // updated by a register allocator and then used by a machine code rewriter that
00013 // adds spill code and rewrites virtual into physical register references.
00014 //
00015 //===----------------------------------------------------------------------===//
00016 
00017 #ifndef LLVM_CODEGEN_VIRTREGMAP_H
00018 #define LLVM_CODEGEN_VIRTREGMAP_H
00019 
00020 #include "llvm/Target/MRegisterInfo.h"
00021 #include "llvm/ADT/DenseMap.h"
00022 #include <map>
00023 
00024 namespace llvm {
00025   class MachineInstr;
00026 
00027   class VirtRegMap {
00028   public:
00029     enum ModRef { isRef = 1, isMod = 2, isModRef = 3 };
00030     typedef std::multimap<MachineInstr*,
00031                           std::pair<unsigned, ModRef> > MI2VirtMapTy;
00032 
00033   private:
00034     MachineFunction &MF;
00035     /// Virt2PhysMap - This is a virtual to physical register
00036     /// mapping. Each virtual register is required to have an entry in
00037     /// it; even spilled virtual registers (the register mapped to a
00038     /// spilled register is the temporary used to load it from the
00039     /// stack).
00040     DenseMap<unsigned, VirtReg2IndexFunctor> Virt2PhysMap;
00041     /// Virt2StackSlotMap - This is virtual register to stack slot
00042     /// mapping. Each spilled virtual register has an entry in it
00043     /// which corresponds to the stack slot this register is spilled
00044     /// at.
00045     DenseMap<int, VirtReg2IndexFunctor> Virt2StackSlotMap;
00046     /// MI2VirtMap - This is MachineInstr to virtual register
00047     /// mapping. In the case of memory spill code being folded into
00048     /// instructions, we need to know which virtual register was
00049     /// read/written by this instruction.
00050     MI2VirtMapTy MI2VirtMap;
00051 
00052     VirtRegMap(const VirtRegMap&);     // DO NOT IMPLEMENT
00053     void operator=(const VirtRegMap&); // DO NOT IMPLEMENT
00054 
00055     enum {
00056       NO_PHYS_REG = 0,
00057       NO_STACK_SLOT = ~0 >> 1
00058     };
00059 
00060   public:
00061     VirtRegMap(MachineFunction &mf)
00062       : MF(mf), Virt2PhysMap(NO_PHYS_REG), Virt2StackSlotMap(NO_STACK_SLOT) {
00063       grow();
00064     }
00065 
00066     void grow();
00067 
00068     /// @brief returns true if the specified virtual register is
00069     /// mapped to a physical register
00070     bool hasPhys(unsigned virtReg) const {
00071       return getPhys(virtReg) != NO_PHYS_REG;
00072     }
00073 
00074     /// @brief returns the physical register mapped to the specified
00075     /// virtual register
00076     unsigned getPhys(unsigned virtReg) const {
00077       assert(MRegisterInfo::isVirtualRegister(virtReg));
00078       return Virt2PhysMap[virtReg];
00079     }
00080 
00081     /// @brief creates a mapping for the specified virtual register to
00082     /// the specified physical register
00083     void assignVirt2Phys(unsigned virtReg, unsigned physReg) {
00084       assert(MRegisterInfo::isVirtualRegister(virtReg) &&
00085              MRegisterInfo::isPhysicalRegister(physReg));
00086       assert(Virt2PhysMap[virtReg] == NO_PHYS_REG &&
00087              "attempt to assign physical register to already mapped "
00088              "virtual register");
00089       Virt2PhysMap[virtReg] = physReg;
00090     }
00091 
00092     /// @brief clears the specified virtual register's, physical
00093     /// register mapping
00094     void clearVirt(unsigned virtReg) {
00095       assert(MRegisterInfo::isVirtualRegister(virtReg));
00096       assert(Virt2PhysMap[virtReg] != NO_PHYS_REG &&
00097              "attempt to clear a not assigned virtual register");
00098       Virt2PhysMap[virtReg] = NO_PHYS_REG;
00099     }
00100 
00101     /// @brief clears all virtual to physical register mappings
00102     void clearAllVirt() {
00103       Virt2PhysMap.clear();
00104       grow();
00105     }
00106 
00107     /// @brief returns true is the specified virtual register is
00108     /// mapped to a stack slot
00109     bool hasStackSlot(unsigned virtReg) const {
00110       return getStackSlot(virtReg) != NO_STACK_SLOT;
00111     }
00112 
00113     /// @brief returns the stack slot mapped to the specified virtual
00114     /// register
00115     int getStackSlot(unsigned virtReg) const {
00116       assert(MRegisterInfo::isVirtualRegister(virtReg));
00117       return Virt2StackSlotMap[virtReg];
00118     }
00119 
00120     /// @brief create a mapping for the specifed virtual register to
00121     /// the next available stack slot
00122     int assignVirt2StackSlot(unsigned virtReg);
00123     /// @brief create a mapping for the specified virtual register to
00124     /// the specified stack slot
00125     void assignVirt2StackSlot(unsigned virtReg, int frameIndex);
00126 
00127     /// @brief Updates information about the specified virtual register's value
00128     /// folded into newMI machine instruction.  The OpNum argument indicates the
00129     /// operand number of OldMI that is folded.
00130     void virtFolded(unsigned VirtReg, MachineInstr *OldMI, unsigned OpNum,
00131                     MachineInstr *NewMI);
00132 
00133     /// @brief returns the virtual registers' values folded in memory
00134     /// operands of this instruction
00135     std::pair<MI2VirtMapTy::const_iterator, MI2VirtMapTy::const_iterator>
00136     getFoldedVirts(MachineInstr* MI) const {
00137       return MI2VirtMap.equal_range(MI);
00138     }
00139     
00140     /// RemoveFromFoldedVirtMap - If the specified machine instruction is in
00141     /// the folded instruction map, remove its entry from the map.
00142     void RemoveFromFoldedVirtMap(MachineInstr *MI) {
00143       MI2VirtMap.erase(MI);
00144     }
00145 
00146     void print(std::ostream &OS) const;
00147     void dump() const;
00148   };
00149 
00150   inline std::ostream &operator<<(std::ostream &OS, const VirtRegMap &VRM) {
00151     VRM.print(OS);
00152     return OS;
00153   }
00154 
00155   /// Spiller interface: Implementations of this interface assign spilled
00156   /// virtual registers to stack slots, rewriting the code.
00157   struct Spiller {
00158     virtual ~Spiller();
00159     virtual bool runOnMachineFunction(MachineFunction &MF,
00160                                       VirtRegMap &VRM) = 0;
00161   };
00162 
00163   /// createSpiller - Create an return a spiller object, as specified on the
00164   /// command line.
00165   Spiller* createSpiller();
00166 
00167 } // End llvm namespace
00168 
00169 #endif