LLVM  4.0.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"
29 
30 namespace llvm {
31 
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  /// DeadRemats - The saved instructions which have already been dead after
76  /// rematerialization but not deleted yet -- to be done in postOptimization.
78 
79  /// Remattable - Values defined by remattable instructions as identified by
80  /// tii.isTriviallyReMaterializable().
82 
83  /// Rematted - Values that were actually rematted, and so need to have their
84  /// live range trimmed or entirely removed.
86 
87  /// scanRemattable - Identify the Parent values that may rematerialize.
88  void scanRemattable(AliasAnalysis *aa);
89 
90  /// allUsesAvailableAt - Return true if all registers used by OrigMI at
91  /// OrigIdx are also available with the same value at UseIdx.
92  bool allUsesAvailableAt(const MachineInstr *OrigMI, SlotIndex OrigIdx,
93  SlotIndex UseIdx) const;
94 
95  /// foldAsLoad - If LI has a single use and a single def that can be folded as
96  /// a load, eliminate the register by folding the def into the use.
97  bool foldAsLoad(LiveInterval *LI, SmallVectorImpl<MachineInstr*> &Dead);
98 
99  typedef SetVector<LiveInterval*,
101  SmallPtrSet<LiveInterval*, 8> > ToShrinkSet;
102  /// Helper for eliminateDeadDefs.
103  void eliminateDeadDef(MachineInstr *MI, ToShrinkSet &ToShrink,
104  AliasAnalysis *AA);
105 
106  /// MachineRegisterInfo callback to notify when new virtual
107  /// registers are created.
108  void MRI_NoteNewVirtualRegister(unsigned VReg) override;
109 
110  /// \brief Check if MachineOperand \p MO is a last use/kill either in the
111  /// main live range of \p LI or in one of the matching subregister ranges.
112  bool useIsKill(const LiveInterval &LI, const MachineOperand &MO) const;
113 
114 public:
115  /// Create a LiveRangeEdit for breaking down parent into smaller pieces.
116  /// @param parent The register being spilled or split.
117  /// @param newRegs List to receive any new registers created. This needn't be
118  /// empty initially, any existing registers are ignored.
119  /// @param MF The MachineFunction the live range edit is taking place in.
120  /// @param lis The collection of all live intervals in this function.
121  /// @param vrm Map of virtual registers to physical registers for this
122  /// function. If NULL, no virtual register map updates will
123  /// be done. This could be the case if called before Regalloc.
124  /// @param deadRemats The collection of all the instructions defining an
125  /// original reg and are dead after remat.
127  MachineFunction &MF, LiveIntervals &lis, VirtRegMap *vrm,
128  Delegate *delegate = nullptr,
129  SmallPtrSet<MachineInstr *, 32> *deadRemats = nullptr)
130  : Parent(parent), NewRegs(newRegs), MRI(MF.getRegInfo()), LIS(lis),
131  VRM(vrm), TII(*MF.getSubtarget().getInstrInfo()), TheDelegate(delegate),
132  FirstNew(newRegs.size()), ScannedRemattable(false),
133  DeadRemats(deadRemats) {
134  MRI.setDelegate(this);
135  }
136 
137  ~LiveRangeEdit() override { MRI.resetDelegate(this); }
138 
140  assert(Parent && "No parent LiveInterval");
141  return *Parent;
142  }
143  unsigned getReg() const { return getParent().reg; }
144 
145  /// Iterator for accessing the new registers added by this edit.
147  iterator begin() const { return NewRegs.begin()+FirstNew; }
148  iterator end() const { return NewRegs.end(); }
149  unsigned size() const { return NewRegs.size()-FirstNew; }
150  bool empty() const { return size() == 0; }
151  unsigned get(unsigned idx) const { return NewRegs[idx+FirstNew]; }
152 
153  /// pop_back - It allows LiveRangeEdit users to drop new registers.
154  /// The context is when an original def instruction of a register is
155  /// dead after rematerialization, we still want to keep it for following
156  /// rematerializations. We save the def instruction in DeadRemats,
157  /// and replace the original dst register with a new dummy register so
158  /// the live range of original dst register can be shrinked normally.
159  /// We don't want to allocate phys register for the dummy register, so
160  /// we want to drop it from the NewRegs set.
161  void pop_back() { NewRegs.pop_back(); }
162 
164  return makeArrayRef(NewRegs).slice(FirstNew);
165  }
166 
167  /// createEmptyIntervalFrom - Create a new empty interval based on OldReg.
168  LiveInterval &createEmptyIntervalFrom(unsigned OldReg);
169 
170  /// createFrom - Create a new virtual register based on OldReg.
171  unsigned createFrom(unsigned OldReg);
172 
173  /// create - Create a new register with the same class and original slot as
174  /// parent.
177  }
178 
179  unsigned create() {
180  return createFrom(getReg());
181  }
182 
183  /// anyRematerializable - Return true if any parent values may be
184  /// rematerializable.
185  /// This function must be called before any rematerialization is attempted.
187 
188  /// checkRematerializable - Manually add VNI to the list of rematerializable
189  /// values if DefMI may be rematerializable.
190  bool checkRematerializable(VNInfo *VNI, const MachineInstr *DefMI,
191  AliasAnalysis*);
192 
193  /// Remat - Information needed to rematerialize at a specific location.
194  struct Remat {
195  VNInfo *ParentVNI; // parent_'s value at the remat location.
196  MachineInstr *OrigMI; // Instruction defining OrigVNI. It contains the
197  // real expr for remat.
198  explicit Remat(VNInfo *ParentVNI) : ParentVNI(ParentVNI), OrigMI(nullptr) {}
199  };
200 
201  /// canRematerializeAt - Determine if ParentVNI can be rematerialized at
202  /// UseIdx. It is assumed that parent_.getVNINfoAt(UseIdx) == ParentVNI.
203  /// When cheapAsAMove is set, only cheap remats are allowed.
204  bool canRematerializeAt(Remat &RM, VNInfo *OrigVNI, SlotIndex UseIdx,
205  bool cheapAsAMove);
206 
207  /// rematerializeAt - Rematerialize RM.ParentVNI into DestReg by inserting an
208  /// instruction into MBB before MI. The new instruction is mapped, but
209  /// liveness is not updated.
210  /// Return the SlotIndex of the new instruction.
213  unsigned DestReg,
214  const Remat &RM,
215  const TargetRegisterInfo&,
216  bool Late = false);
217 
218  /// markRematerialized - explicitly mark a value as rematerialized after doing
219  /// it manually.
220  void markRematerialized(const VNInfo *ParentVNI) {
221  Rematted.insert(ParentVNI);
222  }
223 
224  /// didRematerialize - Return true if ParentVNI was rematerialized anywhere.
225  bool didRematerialize(const VNInfo *ParentVNI) const {
226  return Rematted.count(ParentVNI);
227  }
228 
230  // DeadRemats is an optional field.
231  if (DeadRemats)
232  DeadRemats->insert(inst);
233  }
234 
235  /// eraseVirtReg - Notify the delegate that Reg is no longer in use, and try
236  /// to erase it from LIS.
237  void eraseVirtReg(unsigned Reg);
238 
239  /// eliminateDeadDefs - Try to delete machine instructions that are now dead
240  /// (allDefsAreDead returns true). This may cause live intervals to be trimmed
241  /// and further dead efs to be eliminated.
242  /// RegsBeingSpilled lists registers currently being spilled by the register
243  /// allocator. These registers should not be split into new intervals
244  /// as currently those new intervals are not guaranteed to spill.
246  ArrayRef<unsigned> RegsBeingSpilled = None,
247  AliasAnalysis *AA = nullptr);
248 
249  /// calculateRegClassAndHint - Recompute register class and hint for each new
250  /// register.
252  const MachineLoopInfo&,
254 };
255 
256 }
257 
258 #endif
iterator begin() const
const unsigned reg
Definition: LiveInterval.h:656
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:625
MachineBlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate machine basic b...
VNInfo - Value Number Information.
Definition: LiveInterval.h:45
LiveRangeEdit(LiveInterval *parent, SmallVectorImpl< unsigned > &newRegs, MachineFunction &MF, LiveIntervals &lis, VirtRegMap *vrm, Delegate *delegate=nullptr, SmallPtrSet< MachineInstr *, 32 > *deadRemats=nullptr)
Create a LiveRangeEdit for breaking down parent into smaller pieces.
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:440
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.
void markDeadRemat(MachineInstr *inst)
bool canRematerializeAt(Remat &RM, VNInfo *OrigVNI, SlotIndex UseIdx, bool cheapAsAMove)
canRematerializeAt - Determine if ParentVNI can be rematerialized at UseIdx.
MachineBasicBlock * MBB
Function Alias Analysis false
void pop_back()
pop_back - It allows LiveRangeEdit users to drop new registers.
TargetInstrInfo - Interface to description of machine instruction set.
unsigned createFrom(unsigned OldReg)
createFrom - Create a new virtual register based on OldReg.
Remat(VNInfo *ParentVNI)
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator begin()
Definition: SmallVector.h:115
void eliminateDeadDefs(SmallVectorImpl< MachineInstr * > &Dead, ArrayRef< unsigned > RegsBeingSpilled=None, AliasAnalysis *AA=nullptr)
eliminateDeadDefs - Try to delete machine instructions that are now dead (allDefsAreDead returns true...
virtual bool LRE_CanEraseVirtReg(unsigned)
Called when a virtual register is no longer used.
Definition: LiveRangeEdit.h:48
bool empty() const
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:425
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:843
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:52
ArrayRef< unsigned > regs() const
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator end()
Definition: SmallVector.h:119
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...
LLVM_ATTRIBUTE_ALWAYS_INLINE size_type size() const
Definition: SmallVector.h:135
virtual void LRE_DidCloneVirtReg(unsigned New, unsigned Old)
Called after cloning a virtual register.
Definition: LiveRangeEdit.h:55
Remat - Information needed to rematerialize at a specific location.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LiveInterval & createEmptyIntervalFrom(unsigned OldReg)
createEmptyIntervalFrom - Create a new empty interval based on OldReg.
A vector that has set insertion semantics.
Definition: SetVector.h:41
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.
IRTranslator LLVM IR MI
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:76
unsigned getReg() const