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