Kill ObjectCodeEmitter and BinaryObject, they were unused and superseded by MC.
[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/ADT/ArrayRef.h"
22 #include "llvm/ADT/SmallPtrSet.h"
23 #include "llvm/CodeGen/LiveInterval.h"
24
25 namespace llvm {
26
27 class AliasAnalysis;
28 class LiveIntervals;
29 class MachineLoopInfo;
30 class MachineRegisterInfo;
31 class VirtRegMap;
32
33 class LiveRangeEdit {
34 public:
35   /// Callback methods for LiveRangeEdit owners.
36   class Delegate {
37     virtual void anchor();
38   public:
39     /// Called immediately before erasing a dead machine instruction.
40     virtual void LRE_WillEraseInstruction(MachineInstr *MI) {}
41
42     /// Called when a virtual register is no longer used. Return false to defer
43     /// its deletion from LiveIntervals.
44     virtual bool LRE_CanEraseVirtReg(unsigned) { return true; }
45
46     /// Called before shrinking the live range of a virtual register.
47     virtual void LRE_WillShrinkVirtReg(unsigned) {}
48
49     /// Called after cloning a virtual register.
50     /// This is used for new registers representing connected components of Old.
51     virtual void LRE_DidCloneVirtReg(unsigned New, unsigned Old) {}
52
53     virtual ~Delegate() {}
54   };
55
56 private:
57   LiveInterval &parent_;
58   SmallVectorImpl<LiveInterval*> &newRegs_;
59   Delegate *const delegate_;
60   const SmallVectorImpl<LiveInterval*> *uselessRegs_;
61
62   /// firstNew_ - Index of the first register added to newRegs_.
63   const unsigned firstNew_;
64
65   /// scannedRemattable_ - true when remattable values have been identified.
66   bool scannedRemattable_;
67
68   /// remattable_ - Values defined by remattable instructions as identified by
69   /// tii.isTriviallyReMaterializable().
70   SmallPtrSet<const VNInfo*,4> remattable_;
71
72   /// rematted_ - Values that were actually rematted, and so need to have their
73   /// live range trimmed or entirely removed.
74   SmallPtrSet<const VNInfo*,4> rematted_;
75
76   /// scanRemattable - Identify the parent_ values that may rematerialize.
77   void scanRemattable(LiveIntervals &lis,
78                       const TargetInstrInfo &tii,
79                       AliasAnalysis *aa);
80
81   /// allUsesAvailableAt - Return true if all registers used by OrigMI at
82   /// OrigIdx are also available with the same value at UseIdx.
83   bool allUsesAvailableAt(const MachineInstr *OrigMI, SlotIndex OrigIdx,
84                           SlotIndex UseIdx, LiveIntervals &lis);
85
86   /// foldAsLoad - If LI has a single use and a single def that can be folded as
87   /// a load, eliminate the register by folding the def into the use.
88   bool foldAsLoad(LiveInterval *LI, SmallVectorImpl<MachineInstr*> &Dead,
89                   MachineRegisterInfo&, LiveIntervals&, const TargetInstrInfo&);
90
91 public:
92   /// Create a LiveRangeEdit for breaking down parent into smaller pieces.
93   /// @param parent The register being spilled or split.
94   /// @param newRegs List to receive any new registers created. This needn't be
95   ///                empty initially, any existing registers are ignored.
96   /// @param uselessRegs List of registers that can't be used when
97   ///        rematerializing values because they are about to be removed.
98   LiveRangeEdit(LiveInterval &parent,
99                 SmallVectorImpl<LiveInterval*> &newRegs,
100                 Delegate *delegate = 0,
101                 const SmallVectorImpl<LiveInterval*> *uselessRegs = 0)
102     : parent_(parent), newRegs_(newRegs),
103       delegate_(delegate),
104       uselessRegs_(uselessRegs),
105       firstNew_(newRegs.size()),
106       scannedRemattable_(false) {}
107
108   LiveInterval &getParent() const { return parent_; }
109   unsigned getReg() const { return parent_.reg; }
110
111   /// Iterator for accessing the new registers added by this edit.
112   typedef SmallVectorImpl<LiveInterval*>::const_iterator iterator;
113   iterator begin() const { return newRegs_.begin()+firstNew_; }
114   iterator end() const { return newRegs_.end(); }
115   unsigned size() const { return newRegs_.size()-firstNew_; }
116   bool empty() const { return size() == 0; }
117   LiveInterval *get(unsigned idx) const { return newRegs_[idx+firstNew_]; }
118
119   ArrayRef<LiveInterval*> regs() const {
120     return makeArrayRef(newRegs_).slice(firstNew_);
121   }
122
123   /// FIXME: Temporary accessors until we can get rid of
124   /// LiveIntervals::AddIntervalsForSpills
125   SmallVectorImpl<LiveInterval*> *getNewVRegs() { return &newRegs_; }
126   const SmallVectorImpl<LiveInterval*> *getUselessVRegs() {
127     return uselessRegs_;
128   }
129
130   /// createFrom - Create a new virtual register based on OldReg.
131   LiveInterval &createFrom(unsigned OldReg, LiveIntervals&, VirtRegMap&);
132
133   /// create - Create a new register with the same class and original slot as
134   /// parent.
135   LiveInterval &create(LiveIntervals &LIS, VirtRegMap &VRM) {
136     return createFrom(getReg(), LIS, VRM);
137   }
138
139   /// anyRematerializable - Return true if any parent values may be
140   /// rematerializable.
141   /// This function must be called before any rematerialization is attempted.
142   bool anyRematerializable(LiveIntervals&, const TargetInstrInfo&,
143                            AliasAnalysis*);
144
145   /// checkRematerializable - Manually add VNI to the list of rematerializable
146   /// values if DefMI may be rematerializable.
147   bool checkRematerializable(VNInfo *VNI, const MachineInstr *DefMI,
148                              const TargetInstrInfo&, AliasAnalysis*);
149
150   /// Remat - Information needed to rematerialize at a specific location.
151   struct Remat {
152     VNInfo *ParentVNI;      // parent_'s value at the remat location.
153     MachineInstr *OrigMI;   // Instruction defining ParentVNI.
154     explicit Remat(VNInfo *ParentVNI) : ParentVNI(ParentVNI), OrigMI(0) {}
155   };
156
157   /// canRematerializeAt - Determine if ParentVNI can be rematerialized at
158   /// UseIdx. It is assumed that parent_.getVNINfoAt(UseIdx) == ParentVNI.
159   /// When cheapAsAMove is set, only cheap remats are allowed.
160   bool canRematerializeAt(Remat &RM,
161                           SlotIndex UseIdx,
162                           bool cheapAsAMove,
163                           LiveIntervals &lis);
164
165   /// rematerializeAt - Rematerialize RM.ParentVNI into DestReg by inserting an
166   /// instruction into MBB before MI. The new instruction is mapped, but
167   /// liveness is not updated.
168   /// Return the SlotIndex of the new instruction.
169   SlotIndex rematerializeAt(MachineBasicBlock &MBB,
170                             MachineBasicBlock::iterator MI,
171                             unsigned DestReg,
172                             const Remat &RM,
173                             LiveIntervals&,
174                             const TargetInstrInfo&,
175                             const TargetRegisterInfo&,
176                             bool Late = false);
177
178   /// markRematerialized - explicitly mark a value as rematerialized after doing
179   /// it manually.
180   void markRematerialized(const VNInfo *ParentVNI) {
181     rematted_.insert(ParentVNI);
182   }
183
184   /// didRematerialize - Return true if ParentVNI was rematerialized anywhere.
185   bool didRematerialize(const VNInfo *ParentVNI) const {
186     return rematted_.count(ParentVNI);
187   }
188
189   /// eraseVirtReg - Notify the delegate that Reg is no longer in use, and try
190   /// to erase it from LIS.
191   void eraseVirtReg(unsigned Reg, LiveIntervals &LIS);
192
193   /// eliminateDeadDefs - Try to delete machine instructions that are now dead
194   /// (allDefsAreDead returns true). This may cause live intervals to be trimmed
195   /// and further dead efs to be eliminated.
196   /// RegsBeingSpilled lists registers currently being spilled by the register
197   /// allocator.  These registers should not be split into new intervals
198   /// as currently those new intervals are not guaranteed to spill.
199   void eliminateDeadDefs(SmallVectorImpl<MachineInstr*> &Dead,
200                          LiveIntervals&, VirtRegMap&,
201                          const TargetInstrInfo&,
202                          ArrayRef<unsigned> RegsBeingSpilled 
203                           = ArrayRef<unsigned>());
204
205   /// calculateRegClassAndHint - Recompute register class and hint for each new
206   /// register.
207   void calculateRegClassAndHint(MachineFunction&, LiveIntervals&,
208                                 const MachineLoopInfo&);
209 };
210
211 }
212
213 #endif