LLVM  3.7.0
LiveRangeEdit.h
Go to the documentation of this file.
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/SetVector.h"
23 #include "llvm/ADT/SmallPtrSet.h"
28 
29 namespace llvm {
30 
31 class AliasAnalysis;
32 class LiveIntervals;
33 class MachineBlockFrequencyInfo;
34 class MachineLoopInfo;
35 class VirtRegMap;
36 
38 public:
39  /// Callback methods for LiveRangeEdit owners.
40  class Delegate {
41  virtual void anchor();
42  public:
43  /// Called immediately before erasing a dead machine instruction.
45 
46  /// Called when a virtual register is no longer used. Return false to defer
47  /// its deletion from LiveIntervals.
48  virtual bool LRE_CanEraseVirtReg(unsigned) { return true; }
49 
50  /// Called before shrinking the live range of a virtual register.
51  virtual void LRE_WillShrinkVirtReg(unsigned) {}
52 
53  /// Called after cloning a virtual register.
54  /// This is used for new registers representing connected components of Old.
55  virtual void LRE_DidCloneVirtReg(unsigned New, unsigned Old) {}
56 
57  virtual ~Delegate() {}
58  };
59 
60 private:
61  LiveInterval *Parent;
64  LiveIntervals &LIS;
65  VirtRegMap *VRM;
66  const TargetInstrInfo &TII;
67  Delegate *const TheDelegate;
68 
69  /// FirstNew - Index of the first register added to NewRegs.
70  const unsigned FirstNew;
71 
72  /// ScannedRemattable - true when remattable values have been identified.
73  bool ScannedRemattable;
74 
75  /// Remattable - Values defined by remattable instructions as identified by
76  /// tii.isTriviallyReMaterializable().
78 
79  /// Rematted - Values that were actually rematted, and so need to have their
80  /// live range trimmed or entirely removed.
82 
83  /// scanRemattable - Identify the Parent values that may rematerialize.
84  void scanRemattable(AliasAnalysis *aa);
85 
86  /// allUsesAvailableAt - Return true if all registers used by OrigMI at
87  /// OrigIdx are also available with the same value at UseIdx.
88  bool allUsesAvailableAt(const MachineInstr *OrigMI, SlotIndex OrigIdx,
89  SlotIndex UseIdx) const;
90 
91  /// foldAsLoad - If LI has a single use and a single def that can be folded as
92  /// a load, eliminate the register by folding the def into the use.
93  bool foldAsLoad(LiveInterval *LI, SmallVectorImpl<MachineInstr*> &Dead);
94 
95  typedef SetVector<LiveInterval*,
97  SmallPtrSet<LiveInterval*, 8> > ToShrinkSet;
98  /// Helper for eliminateDeadDefs.
99  void eliminateDeadDef(MachineInstr *MI, ToShrinkSet &ToShrink);
100 
101  /// MachineRegisterInfo callback to notify when new virtual
102  /// registers are created.
103  void MRI_NoteNewVirtualRegister(unsigned VReg) override;
104 
105  /// \brief Check if MachineOperand \p MO is a last use/kill either in the
106  /// main live range of \p LI or in one of the matching subregister ranges.
107  bool useIsKill(const LiveInterval &LI, const MachineOperand &MO) const;
108 
109 public:
110  /// Create a LiveRangeEdit for breaking down parent into smaller pieces.
111  /// @param parent The register being spilled or split.
112  /// @param newRegs List to receive any new registers created. This needn't be
113  /// empty initially, any existing registers are ignored.
114  /// @param MF The MachineFunction the live range edit is taking place in.
115  /// @param lis The collection of all live intervals in this function.
116  /// @param vrm Map of virtual registers to physical registers for this
117  /// function. If NULL, no virtual register map updates will
118  /// be done. This could be the case if called before Regalloc.
120  MachineFunction &MF, LiveIntervals &lis, VirtRegMap *vrm,
121  Delegate *delegate = nullptr)
122  : Parent(parent), NewRegs(newRegs), MRI(MF.getRegInfo()), LIS(lis),
123  VRM(vrm), TII(*MF.getSubtarget().getInstrInfo()),
124  TheDelegate(delegate), FirstNew(newRegs.size()),
125  ScannedRemattable(false) {
126  MRI.setDelegate(this);
127  }
128 
129  ~LiveRangeEdit() override { MRI.resetDelegate(this); }
130 
132  assert(Parent && "No parent LiveInterval");
133  return *Parent;
134  }
135  unsigned getReg() const { return getParent().reg; }
136 
137  /// Iterator for accessing the new registers added by this edit.
139  iterator begin() const { return NewRegs.begin()+FirstNew; }
140  iterator end() const { return NewRegs.end(); }
141  unsigned size() const { return NewRegs.size()-FirstNew; }
142  bool empty() const { return size() == 0; }
143  unsigned get(unsigned idx) const { return NewRegs[idx+FirstNew]; }
144 
146  return makeArrayRef(NewRegs).slice(FirstNew);
147  }
148 
149  /// createEmptyIntervalFrom - Create a new empty interval based on OldReg.
150  LiveInterval &createEmptyIntervalFrom(unsigned OldReg);
151 
152  /// createFrom - Create a new virtual register based on OldReg.
153  unsigned createFrom(unsigned OldReg);
154 
155  /// create - Create a new register with the same class and original slot as
156  /// parent.
159  }
160 
161  unsigned create() {
162  return createFrom(getReg());
163  }
164 
165  /// anyRematerializable - Return true if any parent values may be
166  /// rematerializable.
167  /// This function must be called before any rematerialization is attempted.
169 
170  /// checkRematerializable - Manually add VNI to the list of rematerializable
171  /// values if DefMI may be rematerializable.
172  bool checkRematerializable(VNInfo *VNI, const MachineInstr *DefMI,
173  AliasAnalysis*);
174 
175  /// Remat - Information needed to rematerialize at a specific location.
176  struct Remat {
177  VNInfo *ParentVNI; // parent_'s value at the remat location.
178  MachineInstr *OrigMI; // Instruction defining ParentVNI.
179  explicit Remat(VNInfo *ParentVNI) : ParentVNI(ParentVNI), OrigMI(nullptr) {}
180  };
181 
182  /// canRematerializeAt - Determine if ParentVNI can be rematerialized at
183  /// UseIdx. It is assumed that parent_.getVNINfoAt(UseIdx) == ParentVNI.
184  /// When cheapAsAMove is set, only cheap remats are allowed.
185  bool canRematerializeAt(Remat &RM,
186  SlotIndex UseIdx,
187  bool cheapAsAMove);
188 
189  /// rematerializeAt - Rematerialize RM.ParentVNI into DestReg by inserting an
190  /// instruction into MBB before MI. The new instruction is mapped, but
191  /// liveness is not updated.
192  /// Return the SlotIndex of the new instruction.
195  unsigned DestReg,
196  const Remat &RM,
197  const TargetRegisterInfo&,
198  bool Late = false);
199 
200  /// markRematerialized - explicitly mark a value as rematerialized after doing
201  /// it manually.
202  void markRematerialized(const VNInfo *ParentVNI) {
203  Rematted.insert(ParentVNI);
204  }
205 
206  /// didRematerialize - Return true if ParentVNI was rematerialized anywhere.
207  bool didRematerialize(const VNInfo *ParentVNI) const {
208  return Rematted.count(ParentVNI);
209  }
210 
211  /// eraseVirtReg - Notify the delegate that Reg is no longer in use, and try
212  /// to erase it from LIS.
213  void eraseVirtReg(unsigned Reg);
214 
215  /// eliminateDeadDefs - Try to delete machine instructions that are now dead
216  /// (allDefsAreDead returns true). This may cause live intervals to be trimmed
217  /// and further dead efs to be eliminated.
218  /// RegsBeingSpilled lists registers currently being spilled by the register
219  /// allocator. These registers should not be split into new intervals
220  /// as currently those new intervals are not guaranteed to spill.
222  ArrayRef<unsigned> RegsBeingSpilled = None);
223 
224  /// calculateRegClassAndHint - Recompute register class and hint for each new
225  /// register.
227  const MachineLoopInfo&,
229 };
230 
231 }
232 
233 #endif
iterator begin() const
const unsigned reg
Definition: LiveInterval.h:616
bool anyRematerializable(AliasAnalysis *)
anyRematerializable - Return true if any parent values may be rematerializable.
LiveInterval - This class represents the liveness of a register, or stack slot.
Definition: LiveInterval.h:588
MachineBlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate machine basic b...
VNInfo - Value Number Information.
Definition: LiveInterval.h:45
bool didRematerialize(const VNInfo *ParentVNI) const
didRematerialize - Return true if ParentVNI was rematerialized anywhere.
iterator end() const
Callback methods for LiveRangeEdit owners.
Definition: LiveRangeEdit.h:40
bool checkRematerializable(VNInfo *VNI, const MachineInstr *DefMI, AliasAnalysis *)
checkRematerializable - Manually add VNI to the list of rematerializable values if DefMI may be remat...
ArrayRef< T > makeArrayRef(const T &OneElt)
Construct an ArrayRef from a single element.
Definition: ArrayRef.h:308
globalsmodref aa
SmallVectorImpl< unsigned >::const_iterator iterator
Iterator for accessing the new registers added by this edit.
Reg
All possible values of the reg field in the ModR/M byte.
#define false
Definition: ConvertUTF.c:65
TargetInstrInfo - Interface to description of machine instruction set.
unsigned createFrom(unsigned OldReg)
createFrom - Create a new virtual register based on OldReg.
Remat(VNInfo *ParentVNI)
bundle_iterator - MachineBasicBlock iterator that automatically skips over MIs that are inside bundle...
virtual bool LRE_CanEraseVirtReg(unsigned)
Called when a virtual register is no longer used.
Definition: LiveRangeEdit.h:48
bool empty() const
bool canRematerializeAt(Remat &RM, SlotIndex UseIdx, bool cheapAsAMove)
canRematerializeAt - Determine if ParentVNI can be rematerialized at UseIdx.
LiveInterval & getParent() const
~LiveRangeEdit() override
virtual void LRE_WillEraseInstruction(MachineInstr *MI)
Called immediately before erasing a dead machine instruction.
Definition: LiveRangeEdit.h:44
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
Definition: SmallPtrSet.h:299
MachineOperand class - Representation of each machine instruction operand.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:861
void resetDelegate(Delegate *delegate)
virtual void LRE_WillShrinkVirtReg(unsigned)
Called before shrinking the live range of a virtual register.
Definition: LiveRangeEdit.h:51
MachineRegisterInfo - Keep track of information for virtual and physical registers, including vreg register classes, use/def chains for registers, etc.
void markRematerialized(const VNInfo *ParentVNI)
markRematerialized - explicitly mark a value as rematerialized after doing it manually.
Representation of each machine instruction.
Definition: MachineInstr.h:51
ArrayRef< unsigned > regs() const
SlotIndex rematerializeAt(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI, unsigned DestReg, const Remat &RM, const TargetRegisterInfo &, bool Late=false)
rematerializeAt - Rematerialize RM.ParentVNI into DestReg by inserting an instruction into MBB before...
virtual void LRE_DidCloneVirtReg(unsigned New, unsigned Old)
Called after cloning a virtual register.
Definition: LiveRangeEdit.h:55
LiveRangeEdit(LiveInterval *parent, SmallVectorImpl< unsigned > &newRegs, MachineFunction &MF, LiveIntervals &lis, VirtRegMap *vrm, Delegate *delegate=nullptr)
Create a LiveRangeEdit for breaking down parent into smaller pieces.
Remat - Information needed to rematerialize at a specific location.
void eliminateDeadDefs(SmallVectorImpl< MachineInstr * > &Dead, ArrayRef< unsigned > RegsBeingSpilled=None)
eliminateDeadDefs - Try to delete machine instructions that are now dead (allDefsAreDead returns true...
LiveInterval & createEmptyIntervalFrom(unsigned OldReg)
createEmptyIntervalFrom - Create a new empty interval based on OldReg.
A vector that has set insertion semantics.
Definition: SetVector.h:37
LiveInterval & createEmptyInterval()
create - Create a new register with the same class and original slot as parent.
void calculateRegClassAndHint(MachineFunction &, const MachineLoopInfo &, const MachineBlockFrequencyInfo &)
calculateRegClassAndHint - Recompute register class and hint for each new register.
void setDelegate(Delegate *delegate)
void eraseVirtReg(unsigned Reg)
eraseVirtReg - Notify the delegate that Reg is no longer in use, and try to erase it from LIS...
unsigned size() const
SlotIndex - An opaque wrapper around machine indexes.
Definition: SlotIndexes.h:92
unsigned getReg() const