Phase 1 of refactoring the MachineRegisterInfo iterators to make them suitable
[oota-llvm.git] / include / llvm / CodeGen / MachineRegisterInfo.h
1 //===-- llvm/CodeGen/MachineRegisterInfo.h ----------------------*- 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 defines the MachineRegisterInfo class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_CODEGEN_MACHINEREGISTERINFO_H
15 #define LLVM_CODEGEN_MACHINEREGISTERINFO_H
16
17 #include "llvm/ADT/BitVector.h"
18 #include "llvm/ADT/IndexedMap.h"
19 #include "llvm/CodeGen/MachineInstrBundle.h"
20 #include "llvm/Target/TargetMachine.h"
21 #include "llvm/Target/TargetRegisterInfo.h"
22 #include <vector>
23
24 namespace llvm {
25 class PSetIterator;
26
27 /// MachineRegisterInfo - Keep track of information for virtual and physical
28 /// registers, including vreg register classes, use/def chains for registers,
29 /// etc.
30 class MachineRegisterInfo {
31 public:
32   class Delegate {
33     virtual void anchor();
34   public:
35     virtual void MRI_NoteNewVirtualRegister(unsigned Reg) = 0;
36
37     virtual ~Delegate() {}
38   };
39
40 private:
41   const TargetMachine &TM;
42   Delegate *TheDelegate;
43
44   /// IsSSA - True when the machine function is in SSA form and virtual
45   /// registers have a single def.
46   bool IsSSA;
47
48   /// TracksLiveness - True while register liveness is being tracked accurately.
49   /// Basic block live-in lists, kill flags, and implicit defs may not be
50   /// accurate when after this flag is cleared.
51   bool TracksLiveness;
52
53   /// VRegInfo - Information we keep for each virtual register.
54   ///
55   /// Each element in this list contains the register class of the vreg and the
56   /// start of the use/def list for the register.
57   IndexedMap<std::pair<const TargetRegisterClass*, MachineOperand*>,
58              VirtReg2IndexFunctor> VRegInfo;
59
60   /// RegAllocHints - This vector records register allocation hints for virtual
61   /// registers. For each virtual register, it keeps a register and hint type
62   /// pair making up the allocation hint. Hint type is target specific except
63   /// for the value 0 which means the second value of the pair is the preferred
64   /// register for allocation. For example, if the hint is <0, 1024>, it means
65   /// the allocator should prefer the physical register allocated to the virtual
66   /// register of the hint.
67   IndexedMap<std::pair<unsigned, unsigned>, VirtReg2IndexFunctor> RegAllocHints;
68
69   /// PhysRegUseDefLists - This is an array of the head of the use/def list for
70   /// physical registers.
71   MachineOperand **PhysRegUseDefLists;
72
73   /// getRegUseDefListHead - Return the head pointer for the register use/def
74   /// list for the specified virtual or physical register.
75   MachineOperand *&getRegUseDefListHead(unsigned RegNo) {
76     if (TargetRegisterInfo::isVirtualRegister(RegNo))
77       return VRegInfo[RegNo].second;
78     return PhysRegUseDefLists[RegNo];
79   }
80
81   MachineOperand *getRegUseDefListHead(unsigned RegNo) const {
82     if (TargetRegisterInfo::isVirtualRegister(RegNo))
83       return VRegInfo[RegNo].second;
84     return PhysRegUseDefLists[RegNo];
85   }
86
87   /// Get the next element in the use-def chain.
88   static MachineOperand *getNextOperandForReg(const MachineOperand *MO) {
89     assert(MO && MO->isReg() && "This is not a register operand!");
90     return MO->Contents.Reg.Next;
91   }
92
93   /// UsedRegUnits - This is a bit vector that is computed and set by the
94   /// register allocator, and must be kept up to date by passes that run after
95   /// register allocation (though most don't modify this).  This is used
96   /// so that the code generator knows which callee save registers to save and
97   /// for other target specific uses.
98   /// This vector has bits set for register units that are modified in the
99   /// current function. It doesn't include registers clobbered by function
100   /// calls with register mask operands.
101   BitVector UsedRegUnits;
102
103   /// UsedPhysRegMask - Additional used physregs including aliases.
104   /// This bit vector represents all the registers clobbered by function calls.
105   /// It can model things that UsedRegUnits can't, such as function calls that
106   /// clobber ymm7 but preserve the low half in xmm7.
107   BitVector UsedPhysRegMask;
108
109   /// ReservedRegs - This is a bit vector of reserved registers.  The target
110   /// may change its mind about which registers should be reserved.  This
111   /// vector is the frozen set of reserved registers when register allocation
112   /// started.
113   BitVector ReservedRegs;
114
115   /// Keep track of the physical registers that are live in to the function.
116   /// Live in values are typically arguments in registers.  LiveIn values are
117   /// allowed to have virtual registers associated with them, stored in the
118   /// second element.
119   std::vector<std::pair<unsigned, unsigned> > LiveIns;
120
121   MachineRegisterInfo(const MachineRegisterInfo&) LLVM_DELETED_FUNCTION;
122   void operator=(const MachineRegisterInfo&) LLVM_DELETED_FUNCTION;
123 public:
124   explicit MachineRegisterInfo(const TargetMachine &TM);
125   ~MachineRegisterInfo();
126
127   const TargetRegisterInfo *getTargetRegisterInfo() const {
128     return TM.getRegisterInfo();
129   }
130
131   void resetDelegate(Delegate *delegate) {
132     // Ensure another delegate does not take over unless the current
133     // delegate first unattaches itself. If we ever need to multicast
134     // notifications, we will need to change to using a list.
135     assert(TheDelegate == delegate &&
136            "Only the current delegate can perform reset!");
137     TheDelegate = 0;
138   }
139
140   void setDelegate(Delegate *delegate) {
141     assert(delegate && !TheDelegate &&
142            "Attempted to set delegate to null, or to change it without "
143            "first resetting it!");
144
145     TheDelegate = delegate;
146   }
147
148   //===--------------------------------------------------------------------===//
149   // Function State
150   //===--------------------------------------------------------------------===//
151
152   // isSSA - Returns true when the machine function is in SSA form. Early
153   // passes require the machine function to be in SSA form where every virtual
154   // register has a single defining instruction.
155   //
156   // The TwoAddressInstructionPass and PHIElimination passes take the machine
157   // function out of SSA form when they introduce multiple defs per virtual
158   // register.
159   bool isSSA() const { return IsSSA; }
160
161   // leaveSSA - Indicates that the machine function is no longer in SSA form.
162   void leaveSSA() { IsSSA = false; }
163
164   /// tracksLiveness - Returns true when tracking register liveness accurately.
165   ///
166   /// While this flag is true, register liveness information in basic block
167   /// live-in lists and machine instruction operands is accurate. This means it
168   /// can be used to change the code in ways that affect the values in
169   /// registers, for example by the register scavenger.
170   ///
171   /// When this flag is false, liveness is no longer reliable.
172   bool tracksLiveness() const { return TracksLiveness; }
173
174   /// invalidateLiveness - Indicates that register liveness is no longer being
175   /// tracked accurately.
176   ///
177   /// This should be called by late passes that invalidate the liveness
178   /// information.
179   void invalidateLiveness() { TracksLiveness = false; }
180
181   //===--------------------------------------------------------------------===//
182   // Register Info
183   //===--------------------------------------------------------------------===//
184
185   // Strictly for use by MachineInstr.cpp.
186   void addRegOperandToUseList(MachineOperand *MO);
187
188   // Strictly for use by MachineInstr.cpp.
189   void removeRegOperandFromUseList(MachineOperand *MO);
190
191   // Strictly for use by MachineInstr.cpp.
192   void moveOperands(MachineOperand *Dst, MachineOperand *Src, unsigned NumOps);
193
194   /// Verify the sanity of the use list for Reg.
195   void verifyUseList(unsigned Reg) const;
196
197   /// Verify the use list of all registers.
198   void verifyUseLists() const;
199
200   /// reg_begin/reg_end - Provide iteration support to walk over all definitions
201   /// and uses of a register within the MachineFunction that corresponds to this
202   /// MachineRegisterInfo object.
203   template<bool Uses, bool Defs, bool SkipDebug,
204            bool ByOperand, bool ByInstr, bool ByBundle>
205   class defusechain_iterator;
206
207   // Make it a friend so it can access getNextOperandForReg().
208   template<bool, bool, bool, bool, bool, bool>
209     friend class defusechain_iterator;
210
211   /// reg_iterator/reg_begin/reg_end - Walk all defs and uses of the specified
212   /// register.
213   typedef defusechain_iterator<true,true,false,true,false,false>
214           reg_iterator;
215   reg_iterator reg_begin(unsigned RegNo) const {
216     return reg_iterator(getRegUseDefListHead(RegNo));
217   }
218   static reg_iterator reg_end() { return reg_iterator(0); }
219
220   /// reg_instr_iterator/reg_instr_begin/reg_instr_end - Walk all defs and uses
221   /// of the specified register, stepping by MachineInstr.
222   typedef defusechain_iterator<true,true,false,false,true,false>
223           reg_instr_iterator;
224   reg_instr_iterator reg_instr_begin(unsigned RegNo) const {
225     return reg_instr_iterator(getRegUseDefListHead(RegNo));
226   }
227   static reg_instr_iterator reg_instr_end() { return reg_instr_iterator(0); }
228
229   /// reg_bundle_iterator/reg_bundle_begin/reg_bundle_end - Walk all defs and uses
230   /// of the specified register, stepping by bundle.
231   typedef defusechain_iterator<true,true,false,false,false,true>
232           reg_bundle_iterator;
233   reg_bundle_iterator reg_bundle_begin(unsigned RegNo) const {
234     return reg_bundle_iterator(getRegUseDefListHead(RegNo));
235   }
236   static reg_bundle_iterator reg_bundle_end() { return reg_bundle_iterator(0); }
237
238   /// reg_empty - Return true if there are no instructions using or defining the
239   /// specified register (it may be live-in).
240   bool reg_empty(unsigned RegNo) const { return reg_begin(RegNo) == reg_end(); }
241
242   /// reg_nodbg_iterator/reg_nodbg_begin/reg_nodbg_end - Walk all defs and uses
243   /// of the specified register, skipping those marked as Debug.
244   typedef defusechain_iterator<true,true,true,true,false,false>
245           reg_nodbg_iterator;
246   reg_nodbg_iterator reg_nodbg_begin(unsigned RegNo) const {
247     return reg_nodbg_iterator(getRegUseDefListHead(RegNo));
248   }
249   static reg_nodbg_iterator reg_nodbg_end() { return reg_nodbg_iterator(0); }
250
251   /// reg_instr_nodbg_iterator/reg_instr_nodbg_begin/reg_instr_nodbg_end - Walk
252   /// all defs and uses of the specified register, stepping by MachineInstr,
253   /// skipping those marked as Debug.
254   typedef defusechain_iterator<true,true,true,false,true,false>
255           reg_instr_nodbg_iterator;
256   reg_instr_nodbg_iterator reg_instr_nodbg_begin(unsigned RegNo) const {
257     return reg_instr_nodbg_iterator(getRegUseDefListHead(RegNo));
258   }
259   static reg_instr_nodbg_iterator reg_instr_nodbg_end() {
260     return reg_instr_nodbg_iterator(0);
261   }
262
263   /// reg_bundle_nodbg_iterator/reg_bundle_nodbg_begin/reg_bundle_nodbg_end - Walk
264   /// all defs and uses of the specified register, stepping by bundle,
265   /// skipping those marked as Debug.
266   typedef defusechain_iterator<true,true,true,false,false,true>
267           reg_bundle_nodbg_iterator;
268   reg_bundle_nodbg_iterator reg_bundle_nodbg_begin(unsigned RegNo) const {
269     return reg_bundle_nodbg_iterator(getRegUseDefListHead(RegNo));
270   }
271   static reg_bundle_nodbg_iterator reg_bundle_nodbg_end() {
272     return reg_bundle_nodbg_iterator(0);
273   }
274
275   /// reg_nodbg_empty - Return true if the only instructions using or defining
276   /// Reg are Debug instructions.
277   bool reg_nodbg_empty(unsigned RegNo) const {
278     return reg_nodbg_begin(RegNo) == reg_nodbg_end();
279   }
280
281   /// def_iterator/def_begin/def_end - Walk all defs of the specified register.
282   typedef defusechain_iterator<false,true,false,true,false,false>
283           def_iterator;
284   def_iterator def_begin(unsigned RegNo) const {
285     return def_iterator(getRegUseDefListHead(RegNo));
286   }
287   static def_iterator def_end() { return def_iterator(0); }
288
289   /// def_instr_iterator/def_instr_begin/def_instr_end - Walk all defs of the
290   /// specified register, stepping by MachineInst.
291   typedef defusechain_iterator<false,true,false,false,true,false>
292           def_instr_iterator;
293   def_instr_iterator def_instr_begin(unsigned RegNo) const {
294     return def_instr_iterator(getRegUseDefListHead(RegNo));
295   }
296   static def_instr_iterator def_instr_end() { return def_instr_iterator(0); }
297
298   /// def_bundle_iterator/def_bundle_begin/def_bundle_end - Walk all defs of the
299   /// specified register, stepping by bundle.
300   typedef defusechain_iterator<false,true,false,false,false,true>
301           def_bundle_iterator;
302   def_bundle_iterator def_bundle_begin(unsigned RegNo) const {
303     return def_bundle_iterator(getRegUseDefListHead(RegNo));
304   }
305   static def_bundle_iterator def_bundle_end() { return def_bundle_iterator(0); }
306
307   /// def_empty - Return true if there are no instructions defining the
308   /// specified register (it may be live-in).
309   bool def_empty(unsigned RegNo) const { return def_begin(RegNo) == def_end(); }
310
311   /// hasOneDef - Return true if there is exactly one instruction defining the
312   /// specified register.
313   bool hasOneDef(unsigned RegNo) const {
314     def_iterator DI = def_begin(RegNo);
315     if (DI == def_end())
316       return false;
317     return ++DI == def_end();
318   }
319
320   /// use_iterator/use_begin/use_end - Walk all uses of the specified register.
321   typedef defusechain_iterator<true,false,false,true,false,false>
322           use_iterator;
323   use_iterator use_begin(unsigned RegNo) const {
324     return use_iterator(getRegUseDefListHead(RegNo));
325   }
326   static use_iterator use_end() { return use_iterator(0); }
327
328   /// use_instr_iterator/use_instr_begin/use_instr_end - Walk all uses of the
329   /// specified register, stepping by MachineInstr.
330   typedef defusechain_iterator<true,false,false,false,true,false>
331           use_instr_iterator;
332   use_instr_iterator use_instr_begin(unsigned RegNo) const {
333     return use_instr_iterator(getRegUseDefListHead(RegNo));
334   }
335   static use_instr_iterator use_instr_end() { return use_instr_iterator(0); }
336
337   /// use_bundle_iterator/use_bundle_begin/use_bundle_end - Walk all uses of the
338   /// specified register, stepping by bundle.
339   typedef defusechain_iterator<true,false,false,false,false,true>
340           use_bundle_iterator;
341   use_bundle_iterator use_bundle_begin(unsigned RegNo) const {
342     return use_bundle_iterator(getRegUseDefListHead(RegNo));
343   }
344   static use_bundle_iterator use_bundle_end() { return use_bundle_iterator(0); }
345
346   /// use_empty - Return true if there are no instructions using the specified
347   /// register.
348   bool use_empty(unsigned RegNo) const { return use_begin(RegNo) == use_end(); }
349
350   /// hasOneUse - Return true if there is exactly one instruction using the
351   /// specified register.
352   bool hasOneUse(unsigned RegNo) const {
353     use_iterator UI = use_begin(RegNo);
354     if (UI == use_end())
355       return false;
356     return ++UI == use_end();
357   }
358
359   /// use_nodbg_iterator/use_nodbg_begin/use_nodbg_end - Walk all uses of the
360   /// specified register, skipping those marked as Debug.
361   typedef defusechain_iterator<true,false,true,true,false,false>
362           use_nodbg_iterator;
363   use_nodbg_iterator use_nodbg_begin(unsigned RegNo) const {
364     return use_nodbg_iterator(getRegUseDefListHead(RegNo));
365   }
366   static use_nodbg_iterator use_nodbg_end() { return use_nodbg_iterator(0); }
367
368   /// use_instr_nodbg_iterator/use_instr_nodbg_begin/use_instr_nodbg_end - Walk
369   /// all uses of the specified register, stepping by MachineInstr, skipping
370   /// those marked as Debug.
371   typedef defusechain_iterator<true,false,true,false,true,false>
372           use_instr_nodbg_iterator;
373   use_instr_nodbg_iterator use_instr_nodbg_begin(unsigned RegNo) const {
374     return use_instr_nodbg_iterator(getRegUseDefListHead(RegNo));
375   }
376   static use_instr_nodbg_iterator use_instr_nodbg_end() {
377     return use_instr_nodbg_iterator(0);
378   }
379
380   /// use_bundle_nodbg_iterator/use_bundle_nodbg_begin/use_bundle_nodbg_end - Walk
381   /// all uses of the specified register, stepping by bundle, skipping
382   /// those marked as Debug.
383   typedef defusechain_iterator<true,false,true,false,false,true>
384           use_bundle_nodbg_iterator;
385   use_bundle_nodbg_iterator use_bundle_nodbg_begin(unsigned RegNo) const {
386     return use_bundle_nodbg_iterator(getRegUseDefListHead(RegNo));
387   }
388   static use_bundle_nodbg_iterator use_bundle_nodbg_end() {
389     return use_bundle_nodbg_iterator(0);
390   }
391
392   /// use_nodbg_empty - Return true if there are no non-Debug instructions
393   /// using the specified register.
394   bool use_nodbg_empty(unsigned RegNo) const {
395     return use_nodbg_begin(RegNo) == use_nodbg_end();
396   }
397
398   /// hasOneNonDBGUse - Return true if there is exactly one non-Debug
399   /// instruction using the specified register.
400   bool hasOneNonDBGUse(unsigned RegNo) const;
401
402   /// replaceRegWith - Replace all instances of FromReg with ToReg in the
403   /// machine function.  This is like llvm-level X->replaceAllUsesWith(Y),
404   /// except that it also changes any definitions of the register as well.
405   ///
406   /// Note that it is usually necessary to first constrain ToReg's register
407   /// class to match the FromReg constraints using:
408   ///
409   ///   constrainRegClass(ToReg, getRegClass(FromReg))
410   ///
411   /// That function will return NULL if the virtual registers have incompatible
412   /// constraints.
413   void replaceRegWith(unsigned FromReg, unsigned ToReg);
414
415   /// getVRegDef - Return the machine instr that defines the specified virtual
416   /// register or null if none is found.  This assumes that the code is in SSA
417   /// form, so there should only be one definition.
418   MachineInstr *getVRegDef(unsigned Reg) const;
419
420   /// getUniqueVRegDef - Return the unique machine instr that defines the
421   /// specified virtual register or null if none is found.  If there are
422   /// multiple definitions or no definition, return null.
423   MachineInstr *getUniqueVRegDef(unsigned Reg) const;
424
425   /// clearKillFlags - Iterate over all the uses of the given register and
426   /// clear the kill flag from the MachineOperand. This function is used by
427   /// optimization passes which extend register lifetimes and need only
428   /// preserve conservative kill flag information.
429   void clearKillFlags(unsigned Reg) const;
430
431 #ifndef NDEBUG
432   void dumpUses(unsigned RegNo) const;
433 #endif
434
435   /// isConstantPhysReg - Returns true if PhysReg is unallocatable and constant
436   /// throughout the function.  It is safe to move instructions that read such
437   /// a physreg.
438   bool isConstantPhysReg(unsigned PhysReg, const MachineFunction &MF) const;
439
440   /// Get an iterator over the pressure sets affected by the given physical or
441   /// virtual register. If RegUnit is physical, it must be a register unit (from
442   /// MCRegUnitIterator).
443   PSetIterator getPressureSets(unsigned RegUnit) const;
444
445   //===--------------------------------------------------------------------===//
446   // Virtual Register Info
447   //===--------------------------------------------------------------------===//
448
449   /// getRegClass - Return the register class of the specified virtual register.
450   ///
451   const TargetRegisterClass *getRegClass(unsigned Reg) const {
452     return VRegInfo[Reg].first;
453   }
454
455   /// setRegClass - Set the register class of the specified virtual register.
456   ///
457   void setRegClass(unsigned Reg, const TargetRegisterClass *RC);
458
459   /// constrainRegClass - Constrain the register class of the specified virtual
460   /// register to be a common subclass of RC and the current register class,
461   /// but only if the new class has at least MinNumRegs registers.  Return the
462   /// new register class, or NULL if no such class exists.
463   /// This should only be used when the constraint is known to be trivial, like
464   /// GR32 -> GR32_NOSP. Beware of increasing register pressure.
465   ///
466   const TargetRegisterClass *constrainRegClass(unsigned Reg,
467                                                const TargetRegisterClass *RC,
468                                                unsigned MinNumRegs = 0);
469
470   /// recomputeRegClass - Try to find a legal super-class of Reg's register
471   /// class that still satisfies the constraints from the instructions using
472   /// Reg.  Returns true if Reg was upgraded.
473   ///
474   /// This method can be used after constraints have been removed from a
475   /// virtual register, for example after removing instructions or splitting
476   /// the live range.
477   ///
478   bool recomputeRegClass(unsigned Reg, const TargetMachine&);
479
480   /// createVirtualRegister - Create and return a new virtual register in the
481   /// function with the specified register class.
482   ///
483   unsigned createVirtualRegister(const TargetRegisterClass *RegClass);
484
485   /// getNumVirtRegs - Return the number of virtual registers created.
486   ///
487   unsigned getNumVirtRegs() const { return VRegInfo.size(); }
488
489   /// clearVirtRegs - Remove all virtual registers (after physreg assignment).
490   void clearVirtRegs();
491
492   /// setRegAllocationHint - Specify a register allocation hint for the
493   /// specified virtual register.
494   void setRegAllocationHint(unsigned Reg, unsigned Type, unsigned PrefReg) {
495     RegAllocHints[Reg].first  = Type;
496     RegAllocHints[Reg].second = PrefReg;
497   }
498
499   /// getRegAllocationHint - Return the register allocation hint for the
500   /// specified virtual register.
501   std::pair<unsigned, unsigned>
502   getRegAllocationHint(unsigned Reg) const {
503     return RegAllocHints[Reg];
504   }
505
506   /// getSimpleHint - Return the preferred register allocation hint, or 0 if a
507   /// standard simple hint (Type == 0) is not set.
508   unsigned getSimpleHint(unsigned Reg) const {
509     std::pair<unsigned, unsigned> Hint = getRegAllocationHint(Reg);
510     return Hint.first ? 0 : Hint.second;
511   }
512
513
514   //===--------------------------------------------------------------------===//
515   // Physical Register Use Info
516   //===--------------------------------------------------------------------===//
517
518   /// isPhysRegUsed - Return true if the specified register is used in this
519   /// function. Also check for clobbered aliases and registers clobbered by
520   /// function calls with register mask operands.
521   ///
522   /// This only works after register allocation. It is primarily used by
523   /// PrologEpilogInserter to determine which callee-saved registers need
524   /// spilling.
525   bool isPhysRegUsed(unsigned Reg) const {
526     if (UsedPhysRegMask.test(Reg))
527       return true;
528     for (MCRegUnitIterator Units(Reg, getTargetRegisterInfo());
529          Units.isValid(); ++Units)
530       if (UsedRegUnits.test(*Units))
531         return true;
532     return false;
533   }
534
535   /// Mark the specified register unit as used in this function.
536   /// This should only be called during and after register allocation.
537   void setRegUnitUsed(unsigned RegUnit) {
538     UsedRegUnits.set(RegUnit);
539   }
540
541   /// setPhysRegUsed - Mark the specified register used in this function.
542   /// This should only be called during and after register allocation.
543   void setPhysRegUsed(unsigned Reg) {
544     for (MCRegUnitIterator Units(Reg, getTargetRegisterInfo());
545          Units.isValid(); ++Units)
546       UsedRegUnits.set(*Units);
547   }
548
549   /// addPhysRegsUsedFromRegMask - Mark any registers not in RegMask as used.
550   /// This corresponds to the bit mask attached to register mask operands.
551   void addPhysRegsUsedFromRegMask(const uint32_t *RegMask) {
552     UsedPhysRegMask.setBitsNotInMask(RegMask);
553   }
554
555   /// setPhysRegUnused - Mark the specified register unused in this function.
556   /// This should only be called during and after register allocation.
557   void setPhysRegUnused(unsigned Reg) {
558     UsedPhysRegMask.reset(Reg);
559     for (MCRegUnitIterator Units(Reg, getTargetRegisterInfo());
560          Units.isValid(); ++Units)
561       UsedRegUnits.reset(*Units);
562   }
563
564
565   //===--------------------------------------------------------------------===//
566   // Reserved Register Info
567   //===--------------------------------------------------------------------===//
568   //
569   // The set of reserved registers must be invariant during register
570   // allocation.  For example, the target cannot suddenly decide it needs a
571   // frame pointer when the register allocator has already used the frame
572   // pointer register for something else.
573   //
574   // These methods can be used by target hooks like hasFP() to avoid changing
575   // the reserved register set during register allocation.
576
577   /// freezeReservedRegs - Called by the register allocator to freeze the set
578   /// of reserved registers before allocation begins.
579   void freezeReservedRegs(const MachineFunction&);
580
581   /// reservedRegsFrozen - Returns true after freezeReservedRegs() was called
582   /// to ensure the set of reserved registers stays constant.
583   bool reservedRegsFrozen() const {
584     return !ReservedRegs.empty();
585   }
586
587   /// canReserveReg - Returns true if PhysReg can be used as a reserved
588   /// register.  Any register can be reserved before freezeReservedRegs() is
589   /// called.
590   bool canReserveReg(unsigned PhysReg) const {
591     return !reservedRegsFrozen() || ReservedRegs.test(PhysReg);
592   }
593
594   /// getReservedRegs - Returns a reference to the frozen set of reserved
595   /// registers. This method should always be preferred to calling
596   /// TRI::getReservedRegs() when possible.
597   const BitVector &getReservedRegs() const {
598     assert(reservedRegsFrozen() &&
599            "Reserved registers haven't been frozen yet. "
600            "Use TRI::getReservedRegs().");
601     return ReservedRegs;
602   }
603
604   /// isReserved - Returns true when PhysReg is a reserved register.
605   ///
606   /// Reserved registers may belong to an allocatable register class, but the
607   /// target has explicitly requested that they are not used.
608   ///
609   bool isReserved(unsigned PhysReg) const {
610     return getReservedRegs().test(PhysReg);
611   }
612
613   /// isAllocatable - Returns true when PhysReg belongs to an allocatable
614   /// register class and it hasn't been reserved.
615   ///
616   /// Allocatable registers may show up in the allocation order of some virtual
617   /// register, so a register allocator needs to track its liveness and
618   /// availability.
619   bool isAllocatable(unsigned PhysReg) const {
620     return getTargetRegisterInfo()->isInAllocatableClass(PhysReg) &&
621       !isReserved(PhysReg);
622   }
623
624   //===--------------------------------------------------------------------===//
625   // LiveIn Management
626   //===--------------------------------------------------------------------===//
627
628   /// addLiveIn - Add the specified register as a live-in.  Note that it
629   /// is an error to add the same register to the same set more than once.
630   void addLiveIn(unsigned Reg, unsigned vreg = 0) {
631     LiveIns.push_back(std::make_pair(Reg, vreg));
632   }
633
634   // Iteration support for the live-ins set.  It's kept in sorted order
635   // by register number.
636   typedef std::vector<std::pair<unsigned,unsigned> >::const_iterator
637   livein_iterator;
638   livein_iterator livein_begin() const { return LiveIns.begin(); }
639   livein_iterator livein_end()   const { return LiveIns.end(); }
640   bool            livein_empty() const { return LiveIns.empty(); }
641
642   bool isLiveIn(unsigned Reg) const;
643
644   /// getLiveInPhysReg - If VReg is a live-in virtual register, return the
645   /// corresponding live-in physical register.
646   unsigned getLiveInPhysReg(unsigned VReg) const;
647
648   /// getLiveInVirtReg - If PReg is a live-in physical register, return the
649   /// corresponding live-in physical register.
650   unsigned getLiveInVirtReg(unsigned PReg) const;
651
652   /// EmitLiveInCopies - Emit copies to initialize livein virtual registers
653   /// into the given entry block.
654   void EmitLiveInCopies(MachineBasicBlock *EntryMBB,
655                         const TargetRegisterInfo &TRI,
656                         const TargetInstrInfo &TII);
657
658   /// defusechain_iterator - This class provides iterator support for machine
659   /// operands in the function that use or define a specific register.  If
660   /// ReturnUses is true it returns uses of registers, if ReturnDefs is true it
661   /// returns defs.  If neither are true then you are silly and it always
662   /// returns end().  If SkipDebug is true it skips uses marked Debug
663   /// when incrementing.
664   template<bool ReturnUses, bool ReturnDefs, bool SkipDebug,
665            bool ByOperand, bool ByInstr, bool ByBundle>
666   class defusechain_iterator
667     : public std::iterator<std::forward_iterator_tag, MachineInstr, ptrdiff_t> {
668     MachineOperand *Op;
669     explicit defusechain_iterator(MachineOperand *op) : Op(op) {
670       // If the first node isn't one we're interested in, advance to one that
671       // we are interested in.
672       if (op) {
673         if ((!ReturnUses && op->isUse()) ||
674             (!ReturnDefs && op->isDef()) ||
675             (SkipDebug && op->isDebug()))
676           ++*this;
677       }
678     }
679     friend class MachineRegisterInfo;
680
681     void advance() {
682       assert(Op && "Cannot increment end iterator!");
683       Op = getNextOperandForReg(Op);
684
685       // All defs come before the uses, so stop def_iterator early.
686       if (!ReturnUses) {
687         if (Op) {
688           if (Op->isUse())
689             Op = 0;
690           else
691             assert(!Op->isDebug() && "Can't have debug defs");
692         }
693       } else {
694         // If this is an operand we don't care about, skip it.
695         while (Op && ((!ReturnDefs && Op->isDef()) ||
696                       (SkipDebug && Op->isDebug())))
697           Op = getNextOperandForReg(Op);
698       }
699     }
700   public:
701     typedef std::iterator<std::forward_iterator_tag,
702                           MachineInstr, ptrdiff_t>::reference reference;
703     typedef std::iterator<std::forward_iterator_tag,
704                           MachineInstr, ptrdiff_t>::pointer pointer;
705
706     defusechain_iterator(const defusechain_iterator &I) : Op(I.Op) {}
707     defusechain_iterator() : Op(0) {}
708
709     bool operator==(const defusechain_iterator &x) const {
710       return Op == x.Op;
711     }
712     bool operator!=(const defusechain_iterator &x) const {
713       return !operator==(x);
714     }
715
716     /// atEnd - return true if this iterator is equal to reg_end() on the value.
717     bool atEnd() const { return Op == 0; }
718
719     // Iterator traversal: forward iteration only
720     defusechain_iterator &operator++() {          // Preincrement
721       assert(Op && "Cannot increment end iterator!");
722       if (ByOperand)
723         advance();
724       else if (ByInstr) {
725         MachineInstr *P = Op->getParent();
726         do {
727           advance();
728         } while (Op && Op->getParent() == P);
729       } else if (ByBundle) {
730         MachineInstr *P = getBundleStart(Op->getParent());
731         do {
732           advance();
733         } while (Op && getBundleStart(Op->getParent()) == P);
734       }
735
736       return *this;
737     }
738     defusechain_iterator operator++(int) {        // Postincrement
739       defusechain_iterator tmp = *this; ++*this; return tmp;
740     }
741
742     MachineOperand &getOperand() const {
743       assert(Op && "Cannot dereference end iterator!");
744       return *Op;
745     }
746
747     /// getOperandNo - Return the operand # of this MachineOperand in its
748     /// MachineInstr.
749     unsigned getOperandNo() const {
750       assert(Op && "Cannot dereference end iterator!");
751       return Op - &Op->getParent()->getOperand(0);
752     }
753
754     // Retrieve a reference to the current operand.
755     MachineInstr &operator*() const {
756       assert(Op && "Cannot dereference end iterator!");
757       return *Op->getParent();
758     }
759
760     MachineInstr *operator->() const {
761       assert(Op && "Cannot dereference end iterator!");
762       return Op->getParent();
763     }
764   };
765 };
766
767 /// Iterate over the pressure sets affected by the given physical or virtual
768 /// register. If Reg is physical, it must be a register unit (from
769 /// MCRegUnitIterator).
770 class PSetIterator {
771   const int *PSet;
772   unsigned Weight;
773 public:
774   PSetIterator(): PSet(0), Weight(0) {}
775   PSetIterator(unsigned RegUnit, const MachineRegisterInfo *MRI) {
776     const TargetRegisterInfo *TRI = MRI->getTargetRegisterInfo();
777     if (TargetRegisterInfo::isVirtualRegister(RegUnit)) {
778       const TargetRegisterClass *RC = MRI->getRegClass(RegUnit);
779       PSet = TRI->getRegClassPressureSets(RC);
780       Weight = TRI->getRegClassWeight(RC).RegWeight;
781     }
782     else {
783       PSet = TRI->getRegUnitPressureSets(RegUnit);
784       Weight = TRI->getRegUnitWeight(RegUnit);
785     }
786     if (*PSet == -1)
787       PSet = 0;
788   }
789   bool isValid() const { return PSet; }
790
791   unsigned getWeight() const { return Weight; }
792
793   unsigned operator*() const { return *PSet; }
794
795   void operator++() {
796     assert(isValid() && "Invalid PSetIterator.");
797     ++PSet;
798     if (*PSet == -1)
799       PSet = 0;
800   }
801 };
802
803 inline PSetIterator MachineRegisterInfo::
804 getPressureSets(unsigned RegUnit) const {
805   return PSetIterator(RegUnit, this);
806 }
807
808 } // End llvm namespace
809
810 #endif