Make 64bit args and float args work correct with calls. Thanks to Chris
[oota-llvm.git] / lib / CodeGen / VirtRegMap.h
1 //===-- llvm/CodeGen/VirtRegMap.h - Virtual Register Map -*- C++ -*--------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements a virtual register map. This maps virtual registers to
11 // physical registers and virtual registers to stack slots. It is created and
12 // updated by a register allocator and then used by a machine code rewriter that
13 // adds spill code and rewrites virtual into physical register references.
14 //
15 //===----------------------------------------------------------------------===//
16
17 #ifndef LLVM_CODEGEN_VIRTREGMAP_H
18 #define LLVM_CODEGEN_VIRTREGMAP_H
19
20 #include "llvm/Target/MRegisterInfo.h"
21 #include "llvm/ADT/DenseMap.h"
22 #include <map>
23
24 namespace llvm {
25   class MachineInstr;
26
27   class VirtRegMap {
28   public:
29     enum ModRef { isRef = 1, isMod = 2, isModRef = 3 };
30     typedef std::multimap<MachineInstr*,
31                           std::pair<unsigned, ModRef> > MI2VirtMapTy;
32
33   private:
34     MachineFunction &MF;
35     /// Virt2PhysMap - This is a virtual to physical register
36     /// mapping. Each virtual register is required to have an entry in
37     /// it; even spilled virtual registers (the register mapped to a
38     /// spilled register is the temporary used to load it from the
39     /// stack).
40     DenseMap<unsigned, VirtReg2IndexFunctor> Virt2PhysMap;
41     /// Virt2StackSlotMap - This is virtual register to stack slot
42     /// mapping. Each spilled virtual register has an entry in it
43     /// which corresponds to the stack slot this register is spilled
44     /// at.
45     DenseMap<int, VirtReg2IndexFunctor> Virt2StackSlotMap;
46     /// MI2VirtMap - This is MachineInstr to virtual register
47     /// mapping. In the case of memory spill code being folded into
48     /// instructions, we need to know which virtual register was
49     /// read/written by this instruction.
50     MI2VirtMapTy MI2VirtMap;
51     
52     VirtRegMap(const VirtRegMap&);     // DO NOT IMPLEMENT
53     void operator=(const VirtRegMap&); // DO NOT IMPLEMENT
54
55     enum {
56       NO_PHYS_REG = 0,
57       NO_STACK_SLOT = ~0 >> 1
58     };
59
60   public:
61     VirtRegMap(MachineFunction &mf)
62       : MF(mf), Virt2PhysMap(NO_PHYS_REG), Virt2StackSlotMap(NO_STACK_SLOT) {
63       grow();
64     }
65
66     void grow();
67
68     /// @brief returns true if the specified virtual register is
69     /// mapped to a physical register
70     bool hasPhys(unsigned virtReg) const {
71       return getPhys(virtReg) != NO_PHYS_REG;
72     }
73
74     /// @brief returns the physical register mapped to the specified
75     /// virtual register
76     unsigned getPhys(unsigned virtReg) const {
77       assert(MRegisterInfo::isVirtualRegister(virtReg));
78       return Virt2PhysMap[virtReg];
79     }
80
81     /// @brief creates a mapping for the specified virtual register to
82     /// the specified physical register
83     void assignVirt2Phys(unsigned virtReg, unsigned physReg) {
84       assert(MRegisterInfo::isVirtualRegister(virtReg) &&
85              MRegisterInfo::isPhysicalRegister(physReg));
86       assert(Virt2PhysMap[virtReg] == NO_PHYS_REG &&
87              "attempt to assign physical register to already mapped "
88              "virtual register");
89       Virt2PhysMap[virtReg] = physReg;
90     }
91
92     /// @brief clears the specified virtual register's, physical
93     /// register mapping
94     void clearVirt(unsigned virtReg) {
95       assert(MRegisterInfo::isVirtualRegister(virtReg));
96       assert(Virt2PhysMap[virtReg] != NO_PHYS_REG &&
97              "attempt to clear a not assigned virtual register");
98       Virt2PhysMap[virtReg] = NO_PHYS_REG;
99     }
100
101     /// @brief clears all virtual to physical register mappings
102     void clearAllVirt() {
103       Virt2PhysMap.clear();
104       grow();
105     }
106
107     /// @brief returns true is the specified virtual register is
108     /// mapped to a stack slot
109     bool hasStackSlot(unsigned virtReg) const {
110       return getStackSlot(virtReg) != NO_STACK_SLOT;
111     }
112
113     /// @brief returns the stack slot mapped to the specified virtual
114     /// register
115     int getStackSlot(unsigned virtReg) const {
116       assert(MRegisterInfo::isVirtualRegister(virtReg));
117       return Virt2StackSlotMap[virtReg];
118     }
119
120     /// @brief create a mapping for the specifed virtual register to
121     /// the next available stack slot
122     int assignVirt2StackSlot(unsigned virtReg);
123     /// @brief create a mapping for the specified virtual register to
124     /// the specified stack slot
125     void assignVirt2StackSlot(unsigned virtReg, int frameIndex);
126
127     /// @brief Updates information about the specified virtual register's value
128     /// folded into newMI machine instruction.  The OpNum argument indicates the
129     /// operand number of OldMI that is folded.
130     void virtFolded(unsigned VirtReg, MachineInstr *OldMI, unsigned OpNum,
131                     MachineInstr *NewMI);
132
133     /// @brief returns the virtual registers' values folded in memory
134     /// operands of this instruction
135     std::pair<MI2VirtMapTy::const_iterator, MI2VirtMapTy::const_iterator>
136     getFoldedVirts(MachineInstr* MI) const {
137       return MI2VirtMap.equal_range(MI);
138     }
139
140     void print(std::ostream &OS) const;
141     void dump() const;
142   };
143
144   inline std::ostream &operator<<(std::ostream &OS, const VirtRegMap &VRM) {
145     VRM.print(OS);
146     return OS;
147   }
148
149   /// Spiller interface: Implementations of this interface assign spilled
150   /// virtual registers to stack slots, rewriting the code.
151   struct Spiller {
152     virtual ~Spiller();
153     virtual bool runOnMachineFunction(MachineFunction &MF,
154                                       const VirtRegMap &VRM) = 0;
155   };
156
157   /// createSpiller - Create an return a spiller object, as specified on the
158   /// command line.
159   Spiller* createSpiller();
160
161 } // End llvm namespace
162
163 #endif