Reorganization: Move the Spiller out of VirtRegMap.cpp into its own files. No (inten...
[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/Target/TargetRegisterInfo.h"
21 #include "llvm/ADT/BitVector.h"
22 #include "llvm/ADT/IndexedMap.h"
23 #include "llvm/ADT/SmallPtrSet.h"
24 #include "llvm/ADT/SmallVector.h"
25 #include "llvm/Support/Streams.h"
26 #include <map>
27
28 namespace llvm {
29   class MachineInstr;
30   class MachineFunction;
31   class TargetInstrInfo;
32
33   class VirtRegMap {
34   public:
35     enum {
36       NO_PHYS_REG = 0,
37       NO_STACK_SLOT = (1L << 30)-1,
38       MAX_STACK_SLOT = (1L << 18)-1
39     };
40
41     enum ModRef { isRef = 1, isMod = 2, isModRef = 3 };
42     typedef std::multimap<MachineInstr*,
43                           std::pair<unsigned, ModRef> > MI2VirtMapTy;
44
45   private:
46     const TargetInstrInfo &TII;
47
48     MachineFunction &MF;
49     /// Virt2PhysMap - This is a virtual to physical register
50     /// mapping. Each virtual register is required to have an entry in
51     /// it; even spilled virtual registers (the register mapped to a
52     /// spilled register is the temporary used to load it from the
53     /// stack).
54     IndexedMap<unsigned, VirtReg2IndexFunctor> Virt2PhysMap;
55
56     /// Virt2StackSlotMap - This is virtual register to stack slot
57     /// mapping. Each spilled virtual register has an entry in it
58     /// which corresponds to the stack slot this register is spilled
59     /// at.
60     IndexedMap<int, VirtReg2IndexFunctor> Virt2StackSlotMap;
61
62     /// Virt2ReMatIdMap - This is virtual register to rematerialization id
63     /// mapping. Each spilled virtual register that should be remat'd has an
64     /// entry in it which corresponds to the remat id.
65     IndexedMap<int, VirtReg2IndexFunctor> Virt2ReMatIdMap;
66
67     /// Virt2SplitMap - This is virtual register to splitted virtual register
68     /// mapping.
69     IndexedMap<unsigned, VirtReg2IndexFunctor> Virt2SplitMap;
70
71     /// Virt2SplitKillMap - This is splitted virtual register to its last use
72     /// (kill) index mapping.
73     IndexedMap<unsigned> Virt2SplitKillMap;
74
75     /// ReMatMap - This is virtual register to re-materialized instruction
76     /// mapping. Each virtual register whose definition is going to be
77     /// re-materialized has an entry in it.
78     IndexedMap<MachineInstr*, VirtReg2IndexFunctor> ReMatMap;
79
80     /// MI2VirtMap - This is MachineInstr to virtual register
81     /// mapping. In the case of memory spill code being folded into
82     /// instructions, we need to know which virtual register was
83     /// read/written by this instruction.
84     MI2VirtMapTy MI2VirtMap;
85
86     /// SpillPt2VirtMap - This records the virtual registers which should
87     /// be spilled right after the MachineInstr due to live interval
88     /// splitting.
89     std::map<MachineInstr*, std::vector<std::pair<unsigned,bool> > >
90     SpillPt2VirtMap;
91
92     /// RestorePt2VirtMap - This records the virtual registers which should
93     /// be restored right before the MachineInstr due to live interval
94     /// splitting.
95     std::map<MachineInstr*, std::vector<unsigned> > RestorePt2VirtMap;
96
97     /// EmergencySpillMap - This records the physical registers that should
98     /// be spilled / restored around the MachineInstr since the register
99     /// allocator has run out of registers.
100     std::map<MachineInstr*, std::vector<unsigned> > EmergencySpillMap;
101
102     /// EmergencySpillSlots - This records emergency spill slots used to
103     /// spill physical registers when the register allocator runs out of
104     /// registers. Ideally only one stack slot is used per function per
105     /// register class.
106     std::map<const TargetRegisterClass*, int> EmergencySpillSlots;
107
108     /// ReMatId - Instead of assigning a stack slot to a to be rematerialized
109     /// virtual register, an unique id is being assigned. This keeps track of
110     /// the highest id used so far. Note, this starts at (1<<18) to avoid
111     /// conflicts with stack slot numbers.
112     int ReMatId;
113
114     /// LowSpillSlot, HighSpillSlot - Lowest and highest spill slot indexes.
115     int LowSpillSlot, HighSpillSlot;
116
117     /// SpillSlotToUsesMap - Records uses for each register spill slot.
118     SmallVector<SmallPtrSet<MachineInstr*, 4>, 8> SpillSlotToUsesMap;
119
120     /// ImplicitDefed - One bit for each virtual register. If set it indicates
121     /// the register is implicitly defined.
122     BitVector ImplicitDefed;
123
124     VirtRegMap(const VirtRegMap&);     // DO NOT IMPLEMENT
125     void operator=(const VirtRegMap&); // DO NOT IMPLEMENT
126
127   public:
128     explicit VirtRegMap(MachineFunction &mf);
129
130     void grow();
131
132     /// @brief returns true if the specified virtual register is
133     /// mapped to a physical register
134     bool hasPhys(unsigned virtReg) const {
135       return getPhys(virtReg) != NO_PHYS_REG;
136     }
137
138     /// @brief returns the physical register mapped to the specified
139     /// virtual register
140     unsigned getPhys(unsigned virtReg) const {
141       assert(TargetRegisterInfo::isVirtualRegister(virtReg));
142       return Virt2PhysMap[virtReg];
143     }
144
145     /// @brief creates a mapping for the specified virtual register to
146     /// the specified physical register
147     void assignVirt2Phys(unsigned virtReg, unsigned physReg) {
148       assert(TargetRegisterInfo::isVirtualRegister(virtReg) &&
149              TargetRegisterInfo::isPhysicalRegister(physReg));
150       assert(Virt2PhysMap[virtReg] == NO_PHYS_REG &&
151              "attempt to assign physical register to already mapped "
152              "virtual register");
153       Virt2PhysMap[virtReg] = physReg;
154     }
155
156     /// @brief clears the specified virtual register's, physical
157     /// register mapping
158     void clearVirt(unsigned virtReg) {
159       assert(TargetRegisterInfo::isVirtualRegister(virtReg));
160       assert(Virt2PhysMap[virtReg] != NO_PHYS_REG &&
161              "attempt to clear a not assigned virtual register");
162       Virt2PhysMap[virtReg] = NO_PHYS_REG;
163     }
164
165     /// @brief clears all virtual to physical register mappings
166     void clearAllVirt() {
167       Virt2PhysMap.clear();
168       grow();
169     }
170
171     /// @brief records virtReg is a split live interval from SReg.
172     void setIsSplitFromReg(unsigned virtReg, unsigned SReg) {
173       Virt2SplitMap[virtReg] = SReg;
174     }
175
176     /// @brief returns the live interval virtReg is split from.
177     unsigned getPreSplitReg(unsigned virtReg) {
178       return Virt2SplitMap[virtReg];
179     }
180
181     /// @brief returns true if the specified virtual register is not
182     /// mapped to a stack slot or rematerialized.
183     bool isAssignedReg(unsigned virtReg) const {
184       if (getStackSlot(virtReg) == NO_STACK_SLOT &&
185           getReMatId(virtReg) == NO_STACK_SLOT)
186         return true;
187       // Split register can be assigned a physical register as well as a
188       // stack slot or remat id.
189       return (Virt2SplitMap[virtReg] && Virt2PhysMap[virtReg] != NO_PHYS_REG);
190     }
191
192     /// @brief returns the stack slot mapped to the specified virtual
193     /// register
194     int getStackSlot(unsigned virtReg) const {
195       assert(TargetRegisterInfo::isVirtualRegister(virtReg));
196       return Virt2StackSlotMap[virtReg];
197     }
198
199     /// @brief returns the rematerialization id mapped to the specified virtual
200     /// register
201     int getReMatId(unsigned virtReg) const {
202       assert(TargetRegisterInfo::isVirtualRegister(virtReg));
203       return Virt2ReMatIdMap[virtReg];
204     }
205
206     /// @brief create a mapping for the specifed virtual register to
207     /// the next available stack slot
208     int assignVirt2StackSlot(unsigned virtReg);
209     /// @brief create a mapping for the specified virtual register to
210     /// the specified stack slot
211     void assignVirt2StackSlot(unsigned virtReg, int frameIndex);
212
213     /// @brief assign an unique re-materialization id to the specified
214     /// virtual register.
215     int assignVirtReMatId(unsigned virtReg);
216     /// @brief assign an unique re-materialization id to the specified
217     /// virtual register.
218     void assignVirtReMatId(unsigned virtReg, int id);
219
220     /// @brief returns true if the specified virtual register is being
221     /// re-materialized.
222     bool isReMaterialized(unsigned virtReg) const {
223       return ReMatMap[virtReg] != NULL;
224     }
225
226     /// @brief returns the original machine instruction being re-issued
227     /// to re-materialize the specified virtual register.
228     MachineInstr *getReMaterializedMI(unsigned virtReg) const {
229       return ReMatMap[virtReg];
230     }
231
232     /// @brief records the specified virtual register will be
233     /// re-materialized and the original instruction which will be re-issed
234     /// for this purpose.  If parameter all is true, then all uses of the
235     /// registers are rematerialized and it's safe to delete the definition.
236     void setVirtIsReMaterialized(unsigned virtReg, MachineInstr *def) {
237       ReMatMap[virtReg] = def;
238     }
239
240     /// @brief record the last use (kill) of a split virtual register.
241     void addKillPoint(unsigned virtReg, unsigned index) {
242       Virt2SplitKillMap[virtReg] = index;
243     }
244
245     unsigned getKillPoint(unsigned virtReg) const {
246       return Virt2SplitKillMap[virtReg];
247     }
248
249     /// @brief remove the last use (kill) of a split virtual register.
250     void removeKillPoint(unsigned virtReg) {
251       Virt2SplitKillMap[virtReg] = 0;
252     }
253
254     /// @brief returns true if the specified MachineInstr is a spill point.
255     bool isSpillPt(MachineInstr *Pt) const {
256       return SpillPt2VirtMap.find(Pt) != SpillPt2VirtMap.end();
257     }
258
259     /// @brief returns the virtual registers that should be spilled due to
260     /// splitting right after the specified MachineInstr.
261     std::vector<std::pair<unsigned,bool> > &getSpillPtSpills(MachineInstr *Pt) {
262       return SpillPt2VirtMap[Pt];
263     }
264
265     /// @brief records the specified MachineInstr as a spill point for virtReg.
266     void addSpillPoint(unsigned virtReg, bool isKill, MachineInstr *Pt) {
267       if (SpillPt2VirtMap.find(Pt) != SpillPt2VirtMap.end())
268         SpillPt2VirtMap[Pt].push_back(std::make_pair(virtReg, isKill));
269       else {
270         std::vector<std::pair<unsigned,bool> > Virts;
271         Virts.push_back(std::make_pair(virtReg, isKill));
272         SpillPt2VirtMap.insert(std::make_pair(Pt, Virts));
273       }
274     }
275
276     /// @brief - transfer spill point information from one instruction to
277     /// another.
278     void transferSpillPts(MachineInstr *Old, MachineInstr *New) {
279       std::map<MachineInstr*,std::vector<std::pair<unsigned,bool> > >::iterator
280         I = SpillPt2VirtMap.find(Old);
281       if (I == SpillPt2VirtMap.end())
282         return;
283       while (!I->second.empty()) {
284         unsigned virtReg = I->second.back().first;
285         bool isKill = I->second.back().second;
286         I->second.pop_back();
287         addSpillPoint(virtReg, isKill, New);
288       }
289       SpillPt2VirtMap.erase(I);
290     }
291
292     /// @brief returns true if the specified MachineInstr is a restore point.
293     bool isRestorePt(MachineInstr *Pt) const {
294       return RestorePt2VirtMap.find(Pt) != RestorePt2VirtMap.end();
295     }
296
297     /// @brief returns the virtual registers that should be restoreed due to
298     /// splitting right after the specified MachineInstr.
299     std::vector<unsigned> &getRestorePtRestores(MachineInstr *Pt) {
300       return RestorePt2VirtMap[Pt];
301     }
302
303     /// @brief records the specified MachineInstr as a restore point for virtReg.
304     void addRestorePoint(unsigned virtReg, MachineInstr *Pt) {
305       if (RestorePt2VirtMap.find(Pt) != RestorePt2VirtMap.end())
306         RestorePt2VirtMap[Pt].push_back(virtReg);
307       else {
308         std::vector<unsigned> Virts;
309         Virts.push_back(virtReg);
310         RestorePt2VirtMap.insert(std::make_pair(Pt, Virts));
311       }
312     }
313
314     /// @brief - transfer restore point information from one instruction to
315     /// another.
316     void transferRestorePts(MachineInstr *Old, MachineInstr *New) {
317       std::map<MachineInstr*,std::vector<unsigned> >::iterator I =
318         RestorePt2VirtMap.find(Old);
319       if (I == RestorePt2VirtMap.end())
320         return;
321       while (!I->second.empty()) {
322         unsigned virtReg = I->second.back();
323         I->second.pop_back();
324         addRestorePoint(virtReg, New);
325       }
326       RestorePt2VirtMap.erase(I);
327     }
328
329     /// @brief records that the specified physical register must be spilled
330     /// around the specified machine instr.
331     void addEmergencySpill(unsigned PhysReg, MachineInstr *MI) {
332       if (EmergencySpillMap.find(MI) != EmergencySpillMap.end())
333         EmergencySpillMap[MI].push_back(PhysReg);
334       else {
335         std::vector<unsigned> PhysRegs;
336         PhysRegs.push_back(PhysReg);
337         EmergencySpillMap.insert(std::make_pair(MI, PhysRegs));
338       }
339     }
340
341     /// @brief returns true if one or more physical registers must be spilled
342     /// around the specified instruction.
343     bool hasEmergencySpills(MachineInstr *MI) const {
344       return EmergencySpillMap.find(MI) != EmergencySpillMap.end();
345     }
346
347     /// @brief returns the physical registers to be spilled and restored around
348     /// the instruction.
349     std::vector<unsigned> &getEmergencySpills(MachineInstr *MI) {
350       return EmergencySpillMap[MI];
351     }
352
353     /// @brief - transfer emergency spill information from one instruction to
354     /// another.
355     void transferEmergencySpills(MachineInstr *Old, MachineInstr *New) {
356       std::map<MachineInstr*,std::vector<unsigned> >::iterator I =
357         EmergencySpillMap.find(Old);
358       if (I == EmergencySpillMap.end())
359         return;
360       while (!I->second.empty()) {
361         unsigned virtReg = I->second.back();
362         I->second.pop_back();
363         addEmergencySpill(virtReg, New);
364       }
365       EmergencySpillMap.erase(I);
366     }
367
368     /// @brief return or get a emergency spill slot for the register class.
369     int getEmergencySpillSlot(const TargetRegisterClass *RC);
370
371     /// @brief Return lowest spill slot index.
372     int getLowSpillSlot() const {
373       return LowSpillSlot;
374     }
375
376     /// @brief Return highest spill slot index.
377     int getHighSpillSlot() const {
378       return HighSpillSlot;
379     }
380
381     /// @brief Records a spill slot use.
382     void addSpillSlotUse(int FrameIndex, MachineInstr *MI);
383
384     /// @brief Returns true if spill slot has been used.
385     bool isSpillSlotUsed(int FrameIndex) const {
386       assert(FrameIndex >= 0 && "Spill slot index should not be negative!");
387       return !SpillSlotToUsesMap[FrameIndex-LowSpillSlot].empty();
388     }
389
390     /// @brief Mark the specified register as being implicitly defined.
391     void setIsImplicitlyDefined(unsigned VirtReg) {
392       ImplicitDefed.set(VirtReg-TargetRegisterInfo::FirstVirtualRegister);
393     }
394
395     /// @brief Returns true if the virtual register is implicitly defined.
396     bool isImplicitlyDefined(unsigned VirtReg) const {
397       return ImplicitDefed[VirtReg-TargetRegisterInfo::FirstVirtualRegister];
398     }
399
400     /// @brief Updates information about the specified virtual register's value
401     /// folded into newMI machine instruction.
402     void virtFolded(unsigned VirtReg, MachineInstr *OldMI, MachineInstr *NewMI,
403                     ModRef MRInfo);
404
405     /// @brief Updates information about the specified virtual register's value
406     /// folded into the specified machine instruction.
407     void virtFolded(unsigned VirtReg, MachineInstr *MI, ModRef MRInfo);
408
409     /// @brief returns the virtual registers' values folded in memory
410     /// operands of this instruction
411     std::pair<MI2VirtMapTy::const_iterator, MI2VirtMapTy::const_iterator>
412     getFoldedVirts(MachineInstr* MI) const {
413       return MI2VirtMap.equal_range(MI);
414     }
415     
416     /// RemoveMachineInstrFromMaps - MI is being erased, remove it from the
417     /// the folded instruction map and spill point map.
418     void RemoveMachineInstrFromMaps(MachineInstr *MI);
419
420     void print(std::ostream &OS) const;
421     void print(std::ostream *OS) const { if (OS) print(*OS); }
422     void dump() const;
423   };
424
425   inline std::ostream *operator<<(std::ostream *OS, const VirtRegMap &VRM) {
426     VRM.print(OS);
427     return OS;
428   }
429   inline std::ostream &operator<<(std::ostream &OS, const VirtRegMap &VRM) {
430     VRM.print(OS);
431     return OS;
432   }
433 } // End llvm namespace
434
435 #endif