Refactor the code that checks that all operands of a node are UNDEFs.
[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/Target/TargetRegisterInfo.h"
18 #include "llvm/CodeGen/MachineInstrBundle.h"
19 #include "llvm/ADT/BitVector.h"
20 #include "llvm/ADT/IndexedMap.h"
21 #include <vector>
22
23 namespace llvm {
24
25 /// MachineRegisterInfo - Keep track of information for virtual and physical
26 /// registers, including vreg register classes, use/def chains for registers,
27 /// etc.
28 class MachineRegisterInfo {
29   const TargetRegisterInfo *const TRI;
30
31   /// IsSSA - True when the machine function is in SSA form and virtual
32   /// registers have a single def.
33   bool IsSSA;
34
35   /// TracksLiveness - True while register liveness is being tracked accurately.
36   /// Basic block live-in lists, kill flags, and implicit defs may not be
37   /// accurate when after this flag is cleared.
38   bool TracksLiveness;
39
40   /// VRegInfo - Information we keep for each virtual register.
41   ///
42   /// Each element in this list contains the register class of the vreg and the
43   /// start of the use/def list for the register.
44   IndexedMap<std::pair<const TargetRegisterClass*, MachineOperand*>,
45              VirtReg2IndexFunctor> VRegInfo;
46
47   /// RegAllocHints - This vector records register allocation hints for virtual
48   /// registers. For each virtual register, it keeps a register and hint type
49   /// pair making up the allocation hint. Hint type is target specific except
50   /// for the value 0 which means the second value of the pair is the preferred
51   /// register for allocation. For example, if the hint is <0, 1024>, it means
52   /// the allocator should prefer the physical register allocated to the virtual
53   /// register of the hint.
54   IndexedMap<std::pair<unsigned, unsigned>, VirtReg2IndexFunctor> RegAllocHints;
55
56   /// PhysRegUseDefLists - This is an array of the head of the use/def list for
57   /// physical registers.
58   MachineOperand **PhysRegUseDefLists;
59
60   /// UsedPhysRegs - This is a bit vector that is computed and set by the
61   /// register allocator, and must be kept up to date by passes that run after
62   /// register allocation (though most don't modify this).  This is used
63   /// so that the code generator knows which callee save registers to save and
64   /// for other target specific uses.
65   /// This vector only has bits set for registers explicitly used, not their
66   /// aliases.
67   BitVector UsedPhysRegs;
68
69   /// UsedPhysRegMask - Additional used physregs, but including aliases.
70   BitVector UsedPhysRegMask;
71
72   /// ReservedRegs - This is a bit vector of reserved registers.  The target
73   /// may change its mind about which registers should be reserved.  This
74   /// vector is the frozen set of reserved registers when register allocation
75   /// started.
76   BitVector ReservedRegs;
77
78   /// AllocatableRegs - From TRI->getAllocatableSet.
79   mutable BitVector AllocatableRegs;
80
81   /// LiveIns/LiveOuts - Keep track of the physical registers that are
82   /// livein/liveout of the function.  Live in values are typically arguments in
83   /// registers, live out values are typically return values in registers.
84   /// LiveIn values are allowed to have virtual registers associated with them,
85   /// stored in the second element.
86   std::vector<std::pair<unsigned, unsigned> > LiveIns;
87   std::vector<unsigned> LiveOuts;
88
89   MachineRegisterInfo(const MachineRegisterInfo&); // DO NOT IMPLEMENT
90   void operator=(const MachineRegisterInfo&);      // DO NOT IMPLEMENT
91 public:
92   explicit MachineRegisterInfo(const TargetRegisterInfo &TRI);
93   ~MachineRegisterInfo();
94
95   //===--------------------------------------------------------------------===//
96   // Function State
97   //===--------------------------------------------------------------------===//
98
99   // isSSA - Returns true when the machine function is in SSA form. Early
100   // passes require the machine function to be in SSA form where every virtual
101   // register has a single defining instruction.
102   //
103   // The TwoAddressInstructionPass and PHIElimination passes take the machine
104   // function out of SSA form when they introduce multiple defs per virtual
105   // register.
106   bool isSSA() const { return IsSSA; }
107
108   // leaveSSA - Indicates that the machine function is no longer in SSA form.
109   void leaveSSA() { IsSSA = false; }
110
111   /// tracksLiveness - Returns true when tracking register liveness accurately.
112   ///
113   /// While this flag is true, register liveness information in basic block
114   /// live-in lists and machine instruction operands is accurate. This means it
115   /// can be used to change the code in ways that affect the values in
116   /// registers, for example by the register scavenger.
117   ///
118   /// When this flag is false, liveness is no longer reliable.
119   bool tracksLiveness() const { return TracksLiveness; }
120
121   /// invalidateLiveness - Indicates that register liveness is no longer being
122   /// tracked accurately.
123   ///
124   /// This should be called by late passes that invalidate the liveness
125   /// information.
126   void invalidateLiveness() { TracksLiveness = false; }
127
128   //===--------------------------------------------------------------------===//
129   // Register Info
130   //===--------------------------------------------------------------------===//
131
132   /// reg_begin/reg_end - Provide iteration support to walk over all definitions
133   /// and uses of a register within the MachineFunction that corresponds to this
134   /// MachineRegisterInfo object.
135   template<bool Uses, bool Defs, bool SkipDebug>
136   class defusechain_iterator;
137
138   /// reg_iterator/reg_begin/reg_end - Walk all defs and uses of the specified
139   /// register.
140   typedef defusechain_iterator<true,true,false> reg_iterator;
141   reg_iterator reg_begin(unsigned RegNo) const {
142     return reg_iterator(getRegUseDefListHead(RegNo));
143   }
144   static reg_iterator reg_end() { return reg_iterator(0); }
145
146   /// reg_empty - Return true if there are no instructions using or defining the
147   /// specified register (it may be live-in).
148   bool reg_empty(unsigned RegNo) const { return reg_begin(RegNo) == reg_end(); }
149
150   /// reg_nodbg_iterator/reg_nodbg_begin/reg_nodbg_end - Walk all defs and uses
151   /// of the specified register, skipping those marked as Debug.
152   typedef defusechain_iterator<true,true,true> reg_nodbg_iterator;
153   reg_nodbg_iterator reg_nodbg_begin(unsigned RegNo) const {
154     return reg_nodbg_iterator(getRegUseDefListHead(RegNo));
155   }
156   static reg_nodbg_iterator reg_nodbg_end() { return reg_nodbg_iterator(0); }
157
158   /// reg_nodbg_empty - Return true if the only instructions using or defining
159   /// Reg are Debug instructions.
160   bool reg_nodbg_empty(unsigned RegNo) const {
161     return reg_nodbg_begin(RegNo) == reg_nodbg_end();
162   }
163
164   /// def_iterator/def_begin/def_end - Walk all defs of the specified register.
165   typedef defusechain_iterator<false,true,false> def_iterator;
166   def_iterator def_begin(unsigned RegNo) const {
167     return def_iterator(getRegUseDefListHead(RegNo));
168   }
169   static def_iterator def_end() { return def_iterator(0); }
170
171   /// def_empty - Return true if there are no instructions defining the
172   /// specified register (it may be live-in).
173   bool def_empty(unsigned RegNo) const { return def_begin(RegNo) == def_end(); }
174
175   /// use_iterator/use_begin/use_end - Walk all uses of the specified register.
176   typedef defusechain_iterator<true,false,false> use_iterator;
177   use_iterator use_begin(unsigned RegNo) const {
178     return use_iterator(getRegUseDefListHead(RegNo));
179   }
180   static use_iterator use_end() { return use_iterator(0); }
181
182   /// use_empty - Return true if there are no instructions using the specified
183   /// register.
184   bool use_empty(unsigned RegNo) const { return use_begin(RegNo) == use_end(); }
185
186   /// hasOneUse - Return true if there is exactly one instruction using the
187   /// specified register.
188   bool hasOneUse(unsigned RegNo) const;
189
190   /// use_nodbg_iterator/use_nodbg_begin/use_nodbg_end - Walk all uses of the
191   /// specified register, skipping those marked as Debug.
192   typedef defusechain_iterator<true,false,true> use_nodbg_iterator;
193   use_nodbg_iterator use_nodbg_begin(unsigned RegNo) const {
194     return use_nodbg_iterator(getRegUseDefListHead(RegNo));
195   }
196   static use_nodbg_iterator use_nodbg_end() { return use_nodbg_iterator(0); }
197
198   /// use_nodbg_empty - Return true if there are no non-Debug instructions
199   /// using the specified register.
200   bool use_nodbg_empty(unsigned RegNo) const {
201     return use_nodbg_begin(RegNo) == use_nodbg_end();
202   }
203
204   /// hasOneNonDBGUse - Return true if there is exactly one non-Debug
205   /// instruction using the specified register.
206   bool hasOneNonDBGUse(unsigned RegNo) const;
207
208   /// replaceRegWith - Replace all instances of FromReg with ToReg in the
209   /// machine function.  This is like llvm-level X->replaceAllUsesWith(Y),
210   /// except that it also changes any definitions of the register as well.
211   ///
212   /// Note that it is usually necessary to first constrain ToReg's register
213   /// class to match the FromReg constraints using:
214   ///
215   ///   constrainRegClass(ToReg, getRegClass(FromReg))
216   ///
217   /// That function will return NULL if the virtual registers have incompatible
218   /// constraints.
219   void replaceRegWith(unsigned FromReg, unsigned ToReg);
220
221   /// getRegUseDefListHead - Return the head pointer for the register use/def
222   /// list for the specified virtual or physical register.
223   MachineOperand *&getRegUseDefListHead(unsigned RegNo) {
224     if (TargetRegisterInfo::isVirtualRegister(RegNo))
225       return VRegInfo[RegNo].second;
226     return PhysRegUseDefLists[RegNo];
227   }
228
229   MachineOperand *getRegUseDefListHead(unsigned RegNo) const {
230     if (TargetRegisterInfo::isVirtualRegister(RegNo))
231       return VRegInfo[RegNo].second;
232     return PhysRegUseDefLists[RegNo];
233   }
234
235   /// getVRegDef - Return the machine instr that defines the specified virtual
236   /// register or null if none is found.  This assumes that the code is in SSA
237   /// form, so there should only be one definition.
238   MachineInstr *getVRegDef(unsigned Reg) const;
239
240   /// getUniqueVRegDef - Return the unique machine instr that defines the
241   /// specified virtual register or null if none is found.  If there are
242   /// multiple definitions or no definition, return null.
243   MachineInstr *getUniqueVRegDef(unsigned Reg) const;
244
245   /// clearKillFlags - Iterate over all the uses of the given register and
246   /// clear the kill flag from the MachineOperand. This function is used by
247   /// optimization passes which extend register lifetimes and need only
248   /// preserve conservative kill flag information.
249   void clearKillFlags(unsigned Reg) const;
250
251 #ifndef NDEBUG
252   void dumpUses(unsigned RegNo) const;
253 #endif
254
255   /// isConstantPhysReg - Returns true if PhysReg is unallocatable and constant
256   /// throughout the function.  It is safe to move instructions that read such
257   /// a physreg.
258   bool isConstantPhysReg(unsigned PhysReg, const MachineFunction &MF) const;
259
260   //===--------------------------------------------------------------------===//
261   // Virtual Register Info
262   //===--------------------------------------------------------------------===//
263
264   /// getRegClass - Return the register class of the specified virtual register.
265   ///
266   const TargetRegisterClass *getRegClass(unsigned Reg) const {
267     return VRegInfo[Reg].first;
268   }
269
270   /// setRegClass - Set the register class of the specified virtual register.
271   ///
272   void setRegClass(unsigned Reg, const TargetRegisterClass *RC);
273
274   /// constrainRegClass - Constrain the register class of the specified virtual
275   /// register to be a common subclass of RC and the current register class,
276   /// but only if the new class has at least MinNumRegs registers.  Return the
277   /// new register class, or NULL if no such class exists.
278   /// This should only be used when the constraint is known to be trivial, like
279   /// GR32 -> GR32_NOSP. Beware of increasing register pressure.
280   ///
281   const TargetRegisterClass *constrainRegClass(unsigned Reg,
282                                                const TargetRegisterClass *RC,
283                                                unsigned MinNumRegs = 0);
284
285   /// recomputeRegClass - Try to find a legal super-class of Reg's register
286   /// class that still satisfies the constraints from the instructions using
287   /// Reg.  Returns true if Reg was upgraded.
288   ///
289   /// This method can be used after constraints have been removed from a
290   /// virtual register, for example after removing instructions or splitting
291   /// the live range.
292   ///
293   bool recomputeRegClass(unsigned Reg, const TargetMachine&);
294
295   /// createVirtualRegister - Create and return a new virtual register in the
296   /// function with the specified register class.
297   ///
298   unsigned createVirtualRegister(const TargetRegisterClass *RegClass);
299
300   /// getNumVirtRegs - Return the number of virtual registers created.
301   ///
302   unsigned getNumVirtRegs() const { return VRegInfo.size(); }
303
304   /// clearVirtRegs - Remove all virtual registers (after physreg assignment).
305   void clearVirtRegs();
306
307   /// setRegAllocationHint - Specify a register allocation hint for the
308   /// specified virtual register.
309   void setRegAllocationHint(unsigned Reg, unsigned Type, unsigned PrefReg) {
310     RegAllocHints[Reg].first  = Type;
311     RegAllocHints[Reg].second = PrefReg;
312   }
313
314   /// getRegAllocationHint - Return the register allocation hint for the
315   /// specified virtual register.
316   std::pair<unsigned, unsigned>
317   getRegAllocationHint(unsigned Reg) const {
318     return RegAllocHints[Reg];
319   }
320
321   /// getSimpleHint - Return the preferred register allocation hint, or 0 if a
322   /// standard simple hint (Type == 0) is not set.
323   unsigned getSimpleHint(unsigned Reg) const {
324     std::pair<unsigned, unsigned> Hint = getRegAllocationHint(Reg);
325     return Hint.first ? 0 : Hint.second;
326   }
327
328
329   //===--------------------------------------------------------------------===//
330   // Physical Register Use Info
331   //===--------------------------------------------------------------------===//
332
333   /// isPhysRegUsed - Return true if the specified register is used in this
334   /// function.  This only works after register allocation.
335   bool isPhysRegUsed(unsigned Reg) const {
336     return UsedPhysRegs.test(Reg) || UsedPhysRegMask.test(Reg);
337   }
338
339   /// isPhysRegOrOverlapUsed - Return true if Reg or any overlapping register
340   /// is used in this function.
341   bool isPhysRegOrOverlapUsed(unsigned Reg) const {
342     if (UsedPhysRegMask.test(Reg))
343       return true;
344     for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI)
345       if (UsedPhysRegs.test(*AI))
346         return true;
347     return false;
348   }
349
350   /// setPhysRegUsed - Mark the specified register used in this function.
351   /// This should only be called during and after register allocation.
352   void setPhysRegUsed(unsigned Reg) { UsedPhysRegs.set(Reg); }
353
354   /// addPhysRegsUsed - Mark the specified registers used in this function.
355   /// This should only be called during and after register allocation.
356   void addPhysRegsUsed(const BitVector &Regs) { UsedPhysRegs |= Regs; }
357
358   /// addPhysRegsUsedFromRegMask - Mark any registers not in RegMask as used.
359   /// This corresponds to the bit mask attached to register mask operands.
360   void addPhysRegsUsedFromRegMask(const uint32_t *RegMask) {
361     UsedPhysRegMask.setBitsNotInMask(RegMask);
362   }
363
364   /// setPhysRegUnused - Mark the specified register unused in this function.
365   /// This should only be called during and after register allocation.
366   void setPhysRegUnused(unsigned Reg) {
367     UsedPhysRegs.reset(Reg);
368     UsedPhysRegMask.reset(Reg);
369   }
370
371
372   //===--------------------------------------------------------------------===//
373   // Reserved Register Info
374   //===--------------------------------------------------------------------===//
375   //
376   // The set of reserved registers must be invariant during register
377   // allocation.  For example, the target cannot suddenly decide it needs a
378   // frame pointer when the register allocator has already used the frame
379   // pointer register for something else.
380   //
381   // These methods can be used by target hooks like hasFP() to avoid changing
382   // the reserved register set during register allocation.
383
384   /// freezeReservedRegs - Called by the register allocator to freeze the set
385   /// of reserved registers before allocation begins.
386   void freezeReservedRegs(const MachineFunction&);
387
388   /// reservedRegsFrozen - Returns true after freezeReservedRegs() was called
389   /// to ensure the set of reserved registers stays constant.
390   bool reservedRegsFrozen() const {
391     return !ReservedRegs.empty();
392   }
393
394   /// canReserveReg - Returns true if PhysReg can be used as a reserved
395   /// register.  Any register can be reserved before freezeReservedRegs() is
396   /// called.
397   bool canReserveReg(unsigned PhysReg) const {
398     return !reservedRegsFrozen() || ReservedRegs.test(PhysReg);
399   }
400
401
402   //===--------------------------------------------------------------------===//
403   // LiveIn/LiveOut Management
404   //===--------------------------------------------------------------------===//
405
406   /// addLiveIn/Out - Add the specified register as a live in/out.  Note that it
407   /// is an error to add the same register to the same set more than once.
408   void addLiveIn(unsigned Reg, unsigned vreg = 0) {
409     LiveIns.push_back(std::make_pair(Reg, vreg));
410   }
411   void addLiveOut(unsigned Reg) { LiveOuts.push_back(Reg); }
412
413   // Iteration support for live in/out sets.  These sets are kept in sorted
414   // order by their register number.
415   typedef std::vector<std::pair<unsigned,unsigned> >::const_iterator
416   livein_iterator;
417   typedef std::vector<unsigned>::const_iterator liveout_iterator;
418   livein_iterator livein_begin() const { return LiveIns.begin(); }
419   livein_iterator livein_end()   const { return LiveIns.end(); }
420   bool            livein_empty() const { return LiveIns.empty(); }
421   liveout_iterator liveout_begin() const { return LiveOuts.begin(); }
422   liveout_iterator liveout_end()   const { return LiveOuts.end(); }
423   bool             liveout_empty() const { return LiveOuts.empty(); }
424
425   bool isLiveIn(unsigned Reg) const;
426   bool isLiveOut(unsigned Reg) const;
427
428   /// getLiveInPhysReg - If VReg is a live-in virtual register, return the
429   /// corresponding live-in physical register.
430   unsigned getLiveInPhysReg(unsigned VReg) const;
431
432   /// getLiveInVirtReg - If PReg is a live-in physical register, return the
433   /// corresponding live-in physical register.
434   unsigned getLiveInVirtReg(unsigned PReg) const;
435
436   /// EmitLiveInCopies - Emit copies to initialize livein virtual registers
437   /// into the given entry block.
438   void EmitLiveInCopies(MachineBasicBlock *EntryMBB,
439                         const TargetRegisterInfo &TRI,
440                         const TargetInstrInfo &TII);
441
442 private:
443   void HandleVRegListReallocation();
444
445 public:
446   /// defusechain_iterator - This class provides iterator support for machine
447   /// operands in the function that use or define a specific register.  If
448   /// ReturnUses is true it returns uses of registers, if ReturnDefs is true it
449   /// returns defs.  If neither are true then you are silly and it always
450   /// returns end().  If SkipDebug is true it skips uses marked Debug
451   /// when incrementing.
452   template<bool ReturnUses, bool ReturnDefs, bool SkipDebug>
453   class defusechain_iterator
454     : public std::iterator<std::forward_iterator_tag, MachineInstr, ptrdiff_t> {
455     MachineOperand *Op;
456     explicit defusechain_iterator(MachineOperand *op) : Op(op) {
457       // If the first node isn't one we're interested in, advance to one that
458       // we are interested in.
459       if (op) {
460         if ((!ReturnUses && op->isUse()) ||
461             (!ReturnDefs && op->isDef()) ||
462             (SkipDebug && op->isDebug()))
463           ++*this;
464       }
465     }
466     friend class MachineRegisterInfo;
467   public:
468     typedef std::iterator<std::forward_iterator_tag,
469                           MachineInstr, ptrdiff_t>::reference reference;
470     typedef std::iterator<std::forward_iterator_tag,
471                           MachineInstr, ptrdiff_t>::pointer pointer;
472
473     defusechain_iterator(const defusechain_iterator &I) : Op(I.Op) {}
474     defusechain_iterator() : Op(0) {}
475
476     bool operator==(const defusechain_iterator &x) const {
477       return Op == x.Op;
478     }
479     bool operator!=(const defusechain_iterator &x) const {
480       return !operator==(x);
481     }
482
483     /// atEnd - return true if this iterator is equal to reg_end() on the value.
484     bool atEnd() const { return Op == 0; }
485
486     // Iterator traversal: forward iteration only
487     defusechain_iterator &operator++() {          // Preincrement
488       assert(Op && "Cannot increment end iterator!");
489       Op = Op->getNextOperandForReg();
490
491       // If this is an operand we don't care about, skip it.
492       while (Op && ((!ReturnUses && Op->isUse()) ||
493                     (!ReturnDefs && Op->isDef()) ||
494                     (SkipDebug && Op->isDebug())))
495         Op = Op->getNextOperandForReg();
496
497       return *this;
498     }
499     defusechain_iterator operator++(int) {        // Postincrement
500       defusechain_iterator tmp = *this; ++*this; return tmp;
501     }
502
503     /// skipInstruction - move forward until reaching a different instruction.
504     /// Return the skipped instruction that is no longer pointed to, or NULL if
505     /// already pointing to end().
506     MachineInstr *skipInstruction() {
507       if (!Op) return 0;
508       MachineInstr *MI = Op->getParent();
509       do ++*this;
510       while (Op && Op->getParent() == MI);
511       return MI;
512     }
513
514     MachineInstr *skipBundle() {
515       if (!Op) return 0;
516       MachineInstr *MI = getBundleStart(Op->getParent());
517       do ++*this;
518       while (Op && getBundleStart(Op->getParent()) == MI);
519       return MI;
520     }
521
522     MachineOperand &getOperand() const {
523       assert(Op && "Cannot dereference end iterator!");
524       return *Op;
525     }
526
527     /// getOperandNo - Return the operand # of this MachineOperand in its
528     /// MachineInstr.
529     unsigned getOperandNo() const {
530       assert(Op && "Cannot dereference end iterator!");
531       return Op - &Op->getParent()->getOperand(0);
532     }
533
534     // Retrieve a reference to the current operand.
535     MachineInstr &operator*() const {
536       assert(Op && "Cannot dereference end iterator!");
537       return *Op->getParent();
538     }
539
540     MachineInstr *operator->() const {
541       assert(Op && "Cannot dereference end iterator!");
542       return Op->getParent();
543     }
544   };
545
546 };
547
548 } // End llvm namespace
549
550 #endif