68d817b45873533de235f9c07f7b3275f2dcb43a
[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 is distributed under the University of Illinois Open Source
6 // 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/CodeGen/MachineFunctionPass.h"
21 #include "llvm/CodeGen/LiveInterval.h"
22 #include "llvm/Target/TargetRegisterInfo.h"
23 #include "llvm/ADT/BitVector.h"
24 #include "llvm/ADT/DenseMap.h"
25 #include "llvm/ADT/IndexedMap.h"
26 #include "llvm/ADT/SmallPtrSet.h"
27 #include "llvm/ADT/SmallVector.h"
28 #include <map>
29
30 namespace llvm {
31   class LiveIntervals;
32   class MachineInstr;
33   class MachineFunction;
34   class MachineRegisterInfo;
35   class TargetInstrInfo;
36   class TargetRegisterInfo;
37   class raw_ostream;
38   class SlotIndexes;
39
40   class VirtRegMap : public MachineFunctionPass {
41   public:
42     enum {
43       NO_PHYS_REG = 0,
44       NO_STACK_SLOT = (1L << 30)-1,
45       MAX_STACK_SLOT = (1L << 18)-1
46     };
47
48     enum ModRef { isRef = 1, isMod = 2, isModRef = 3 };
49     typedef std::multimap<MachineInstr*,
50                           std::pair<unsigned, ModRef> > MI2VirtMapTy;
51
52   private:
53     MachineRegisterInfo *MRI;
54     const TargetInstrInfo *TII;
55     const TargetRegisterInfo *TRI;
56     MachineFunction *MF;
57
58     DenseMap<const TargetRegisterClass*, BitVector> allocatableRCRegs;
59
60     /// Virt2PhysMap - This is a virtual to physical register
61     /// mapping. Each virtual register is required to have an entry in
62     /// it; even spilled virtual registers (the register mapped to a
63     /// spilled register is the temporary used to load it from the
64     /// stack).
65     IndexedMap<unsigned, VirtReg2IndexFunctor> Virt2PhysMap;
66
67     /// Virt2StackSlotMap - This is virtual register to stack slot
68     /// mapping. Each spilled virtual register has an entry in it
69     /// which corresponds to the stack slot this register is spilled
70     /// at.
71     IndexedMap<int, VirtReg2IndexFunctor> Virt2StackSlotMap;
72
73     /// Virt2SplitMap - This is virtual register to splitted virtual register
74     /// mapping.
75     IndexedMap<unsigned, VirtReg2IndexFunctor> Virt2SplitMap;
76
77     /// createSpillSlot - Allocate a spill slot for RC from MFI.
78     unsigned createSpillSlot(const TargetRegisterClass *RC);
79
80     VirtRegMap(const VirtRegMap&);     // DO NOT IMPLEMENT
81     void operator=(const VirtRegMap&); // DO NOT IMPLEMENT
82
83   public:
84     static char ID;
85     VirtRegMap() : MachineFunctionPass(ID), Virt2PhysMap(NO_PHYS_REG),
86                    Virt2StackSlotMap(NO_STACK_SLOT), Virt2SplitMap(0) { }
87     virtual bool runOnMachineFunction(MachineFunction &MF);
88
89     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
90       AU.setPreservesAll();
91       MachineFunctionPass::getAnalysisUsage(AU);
92     }
93
94     MachineFunction &getMachineFunction() const {
95       assert(MF && "getMachineFunction called before runOnMachineFunction");
96       return *MF;
97     }
98
99     MachineRegisterInfo &getRegInfo() const { return *MRI; }
100     const TargetRegisterInfo &getTargetRegInfo() const { return *TRI; }
101
102     void grow();
103
104     /// @brief returns true if the specified virtual register is
105     /// mapped to a physical register
106     bool hasPhys(unsigned virtReg) const {
107       return getPhys(virtReg) != NO_PHYS_REG;
108     }
109
110     /// @brief returns the physical register mapped to the specified
111     /// virtual register
112     unsigned getPhys(unsigned virtReg) const {
113       assert(TargetRegisterInfo::isVirtualRegister(virtReg));
114       return Virt2PhysMap[virtReg];
115     }
116
117     /// @brief creates a mapping for the specified virtual register to
118     /// the specified physical register
119     void assignVirt2Phys(unsigned virtReg, unsigned physReg) {
120       assert(TargetRegisterInfo::isVirtualRegister(virtReg) &&
121              TargetRegisterInfo::isPhysicalRegister(physReg));
122       assert(Virt2PhysMap[virtReg] == NO_PHYS_REG &&
123              "attempt to assign physical register to already mapped "
124              "virtual register");
125       Virt2PhysMap[virtReg] = physReg;
126     }
127
128     /// @brief clears the specified virtual register's, physical
129     /// register mapping
130     void clearVirt(unsigned virtReg) {
131       assert(TargetRegisterInfo::isVirtualRegister(virtReg));
132       assert(Virt2PhysMap[virtReg] != NO_PHYS_REG &&
133              "attempt to clear a not assigned virtual register");
134       Virt2PhysMap[virtReg] = NO_PHYS_REG;
135     }
136
137     /// @brief clears all virtual to physical register mappings
138     void clearAllVirt() {
139       Virt2PhysMap.clear();
140       grow();
141     }
142
143     /// @brief returns the register allocation preference.
144     unsigned getRegAllocPref(unsigned virtReg);
145
146     /// @brief returns true if VirtReg is assigned to its preferred physreg.
147     bool hasPreferredPhys(unsigned VirtReg) {
148       return getPhys(VirtReg) == getRegAllocPref(VirtReg);
149     }
150
151     /// @brief records virtReg is a split live interval from SReg.
152     void setIsSplitFromReg(unsigned virtReg, unsigned SReg) {
153       Virt2SplitMap[virtReg] = SReg;
154     }
155
156     /// @brief returns the live interval virtReg is split from.
157     unsigned getPreSplitReg(unsigned virtReg) const {
158       return Virt2SplitMap[virtReg];
159     }
160
161     /// getOriginal - Return the original virtual register that VirtReg descends
162     /// from through splitting.
163     /// A register that was not created by splitting is its own original.
164     /// This operation is idempotent.
165     unsigned getOriginal(unsigned VirtReg) const {
166       unsigned Orig = getPreSplitReg(VirtReg);
167       return Orig ? Orig : VirtReg;
168     }
169
170     /// @brief returns true if the specified virtual register is not
171     /// mapped to a stack slot or rematerialized.
172     bool isAssignedReg(unsigned virtReg) const {
173       if (getStackSlot(virtReg) == NO_STACK_SLOT)
174         return true;
175       // Split register can be assigned a physical register as well as a
176       // stack slot or remat id.
177       return (Virt2SplitMap[virtReg] && Virt2PhysMap[virtReg] != NO_PHYS_REG);
178     }
179
180     /// @brief returns the stack slot mapped to the specified virtual
181     /// register
182     int getStackSlot(unsigned virtReg) const {
183       assert(TargetRegisterInfo::isVirtualRegister(virtReg));
184       return Virt2StackSlotMap[virtReg];
185     }
186
187     /// @brief create a mapping for the specifed virtual register to
188     /// the next available stack slot
189     int assignVirt2StackSlot(unsigned virtReg);
190     /// @brief create a mapping for the specified virtual register to
191     /// the specified stack slot
192     void assignVirt2StackSlot(unsigned virtReg, int frameIndex);
193
194     /// rewrite - Rewrite all instructions in MF to use only physical registers
195     /// by mapping all virtual register operands to their assigned physical
196     /// registers.
197     ///
198     /// @param Indexes Optionally remove deleted instructions from indexes.
199     void rewrite(SlotIndexes *Indexes);
200
201     void print(raw_ostream &OS, const Module* M = 0) const;
202     void dump() const;
203   };
204
205   inline raw_ostream &operator<<(raw_ostream &OS, const VirtRegMap &VRM) {
206     VRM.print(OS);
207     return OS;
208   }
209 } // End llvm namespace
210
211 #endif