Tell the register allocator about new unused virtual registers.
[oota-llvm.git] / lib / CodeGen / LiveRangeEdit.h
1 //===---- LiveRangeEdit.h - Basic tools for split and spill -----*- 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 // The LiveRangeEdit class represents changes done to a virtual register when it
11 // is spilled or split.
12 //
13 // The parent register is never changed. Instead, a number of new virtual
14 // registers are created and added to the newRegs vector.
15 //
16 //===----------------------------------------------------------------------===//
17
18 #ifndef LLVM_CODEGEN_LIVERANGEEDIT_H
19 #define LLVM_CODEGEN_LIVERANGEEDIT_H
20
21 #include "llvm/CodeGen/LiveInterval.h"
22 #include "llvm/ADT/SmallPtrSet.h"
23
24 namespace llvm {
25
26 class AliasAnalysis;
27 class LiveIntervals;
28 class MachineRegisterInfo;
29 class VirtRegMap;
30
31 class LiveRangeEdit {
32 public:
33   /// Callback methods for LiveRangeEdit owners.
34   struct Delegate {
35     /// Called immediately before erasing a dead machine instruction.
36     virtual void LRE_WillEraseInstruction(MachineInstr *MI) {}
37
38     /// Called when a virtual register is no longer used. Return false to defer
39     /// its deletion from LiveIntervals.
40     virtual bool LRE_CanEraseVirtReg(unsigned) { return true; }
41
42     virtual ~Delegate() {}
43   };
44
45 private:
46   LiveInterval &parent_;
47   SmallVectorImpl<LiveInterval*> &newRegs_;
48   Delegate *const delegate_;
49   const SmallVectorImpl<LiveInterval*> *uselessRegs_;
50
51   /// firstNew_ - Index of the first register added to newRegs_.
52   const unsigned firstNew_;
53
54   /// scannedRemattable_ - true when remattable values have been identified.
55   bool scannedRemattable_;
56
57   /// remattable_ - Values defined by remattable instructions as identified by
58   /// tii.isTriviallyReMaterializable().
59   SmallPtrSet<const VNInfo*,4> remattable_;
60
61   /// rematted_ - Values that were actually rematted, and so need to have their
62   /// live range trimmed or entirely removed.
63   SmallPtrSet<const VNInfo*,4> rematted_;
64
65   /// scanRemattable - Identify the parent_ values that may rematerialize.
66   void scanRemattable(LiveIntervals &lis,
67                       const TargetInstrInfo &tii,
68                       AliasAnalysis *aa);
69
70   /// allUsesAvailableAt - Return true if all registers used by OrigMI at
71   /// OrigIdx are also available with the same value at UseIdx.
72   bool allUsesAvailableAt(const MachineInstr *OrigMI, SlotIndex OrigIdx,
73                           SlotIndex UseIdx, LiveIntervals &lis);
74
75 public:
76   /// Create a LiveRangeEdit for breaking down parent into smaller pieces.
77   /// @param parent The register being spilled or split.
78   /// @param newRegs List to receive any new registers created. This needn't be
79   ///                empty initially, any existing registers are ignored.
80   /// @param uselessRegs List of registers that can't be used when
81   ///        rematerializing values because they are about to be removed.
82   LiveRangeEdit(LiveInterval &parent,
83                 SmallVectorImpl<LiveInterval*> &newRegs,
84                 Delegate *delegate = 0,
85                 const SmallVectorImpl<LiveInterval*> *uselessRegs = 0)
86     : parent_(parent), newRegs_(newRegs),
87       delegate_(delegate),
88       uselessRegs_(uselessRegs),
89       firstNew_(newRegs.size()),
90       scannedRemattable_(false) {}
91
92   LiveInterval &getParent() const { return parent_; }
93   unsigned getReg() const { return parent_.reg; }
94
95   /// Iterator for accessing the new registers added by this edit.
96   typedef SmallVectorImpl<LiveInterval*>::const_iterator iterator;
97   iterator begin() const { return newRegs_.begin()+firstNew_; }
98   iterator end() const { return newRegs_.end(); }
99   unsigned size() const { return newRegs_.size()-firstNew_; }
100   bool empty() const { return size() == 0; }
101   LiveInterval *get(unsigned idx) const { return newRegs_[idx+firstNew_]; }
102
103   /// FIXME: Temporary accessors until we can get rid of
104   /// LiveIntervals::AddIntervalsForSpills
105   SmallVectorImpl<LiveInterval*> *getNewVRegs() { return &newRegs_; }
106   const SmallVectorImpl<LiveInterval*> *getUselessVRegs() {
107     return uselessRegs_;
108   }
109
110   /// create - Create a new register with the same class and stack slot as
111   /// parent.
112   LiveInterval &create(MachineRegisterInfo&, LiveIntervals&, VirtRegMap&);
113
114   /// anyRematerializable - Return true if any parent values may be
115   /// rematerializable.
116   /// This function must be called before any rematerialization is attempted.
117   bool anyRematerializable(LiveIntervals&, const TargetInstrInfo&,
118                            AliasAnalysis*);
119
120   /// Remat - Information needed to rematerialize at a specific location.
121   struct Remat {
122     VNInfo *ParentVNI;      // parent_'s value at the remat location.
123     MachineInstr *OrigMI;   // Instruction defining ParentVNI.
124     explicit Remat(VNInfo *ParentVNI) : ParentVNI(ParentVNI), OrigMI(0) {}
125   };
126
127   /// canRematerializeAt - Determine if ParentVNI can be rematerialized at
128   /// UseIdx. It is assumed that parent_.getVNINfoAt(UseIdx) == ParentVNI.
129   /// When cheapAsAMove is set, only cheap remats are allowed.
130   bool canRematerializeAt(Remat &RM,
131                           SlotIndex UseIdx,
132                           bool cheapAsAMove,
133                           LiveIntervals &lis);
134
135   /// rematerializeAt - Rematerialize RM.ParentVNI into DestReg by inserting an
136   /// instruction into MBB before MI. The new instruction is mapped, but
137   /// liveness is not updated.
138   /// Return the SlotIndex of the new instruction.
139   SlotIndex rematerializeAt(MachineBasicBlock &MBB,
140                             MachineBasicBlock::iterator MI,
141                             unsigned DestReg,
142                             const Remat &RM,
143                             LiveIntervals&,
144                             const TargetInstrInfo&,
145                             const TargetRegisterInfo&);
146
147   /// markRematerialized - explicitly mark a value as rematerialized after doing
148   /// it manually.
149   void markRematerialized(const VNInfo *ParentVNI) {
150     rematted_.insert(ParentVNI);
151   }
152
153   /// didRematerialize - Return true if ParentVNI was rematerialized anywhere.
154   bool didRematerialize(const VNInfo *ParentVNI) const {
155     return rematted_.count(ParentVNI);
156   }
157
158   /// eraseVirtReg - Notify the delegate that Reg is no longer in use, and try
159   /// to erase it from LIS.
160   void eraseVirtReg(unsigned Reg, LiveIntervals &LIS);
161
162   /// eliminateDeadDefs - Try to delete machine instructions that are now dead
163   /// (allDefsAreDead returns true). This may cause live intervals to be trimmed
164   /// and further dead efs to be eliminated.
165   void eliminateDeadDefs(SmallVectorImpl<MachineInstr*> &Dead,
166                          LiveIntervals&,
167                          const TargetInstrInfo&);
168
169 };
170
171 }
172
173 #endif