LLVM  14.0.0git
LiveRangeEdit.h
Go to the documentation of this file.
1 //===- LiveRangeEdit.h - Basic tools for split and spill --------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // The LiveRangeEdit class represents changes done to a virtual register when it
10 // is spilled or split.
11 //
12 // The parent register is never changed. Instead, a number of new virtual
13 // registers are created and added to the newRegs vector.
14 //
15 //===----------------------------------------------------------------------===//
16 
17 #ifndef LLVM_CODEGEN_LIVERANGEEDIT_H
18 #define LLVM_CODEGEN_LIVERANGEEDIT_H
19 
20 #include "llvm/ADT/ArrayRef.h"
21 #include "llvm/ADT/None.h"
22 #include "llvm/ADT/SetVector.h"
23 #include "llvm/ADT/SmallPtrSet.h"
24 #include "llvm/ADT/SmallVector.h"
31 #include <cassert>
32 
33 namespace llvm {
34 
35 class AAResults;
36 class LiveIntervals;
37 class MachineBlockFrequencyInfo;
38 class MachineInstr;
39 class MachineLoopInfo;
40 class MachineOperand;
41 class TargetInstrInfo;
42 class TargetRegisterInfo;
43 class VirtRegMap;
44 class VirtRegAuxInfo;
45 
47 public:
48  /// Callback methods for LiveRangeEdit owners.
49  class Delegate {
50  virtual void anchor();
51 
52  public:
53  virtual ~Delegate() = default;
54 
55  /// Called immediately before erasing a dead machine instruction.
57 
58  /// Called when a virtual register is no longer used. Return false to defer
59  /// its deletion from LiveIntervals.
60  virtual bool LRE_CanEraseVirtReg(Register) { return true; }
61 
62  /// Called before shrinking the live range of a virtual register.
64 
65  /// Called after cloning a virtual register.
66  /// This is used for new registers representing connected components of Old.
67  virtual void LRE_DidCloneVirtReg(Register New, Register Old) {}
68  };
69 
70 private:
71  LiveInterval *Parent;
74  LiveIntervals &LIS;
75  VirtRegMap *VRM;
76  const TargetInstrInfo &TII;
77  Delegate *const TheDelegate;
78 
79  /// FirstNew - Index of the first register added to NewRegs.
80  const unsigned FirstNew;
81 
82  /// ScannedRemattable - true when remattable values have been identified.
83  bool ScannedRemattable = false;
84 
85  /// DeadRemats - The saved instructions which have already been dead after
86  /// rematerialization but not deleted yet -- to be done in postOptimization.
88 
89  /// Remattable - Values defined by remattable instructions as identified by
90  /// tii.isTriviallyReMaterializable().
92 
93  /// Rematted - Values that were actually rematted, and so need to have their
94  /// live range trimmed or entirely removed.
96 
97  /// scanRemattable - Identify the Parent values that may rematerialize.
98  void scanRemattable(AAResults *aa);
99 
100  /// foldAsLoad - If LI has a single use and a single def that can be folded as
101  /// a load, eliminate the register by folding the def into the use.
102  bool foldAsLoad(LiveInterval *LI, SmallVectorImpl<MachineInstr *> &Dead);
103 
106 
107  /// Helper for eliminateDeadDefs.
108  void eliminateDeadDef(MachineInstr *MI, ToShrinkSet &ToShrink,
109  AAResults *AA);
110 
111  /// MachineRegisterInfo callback to notify when new virtual
112  /// registers are created.
113  void MRI_NoteNewVirtualRegister(Register VReg) override;
114 
115  /// Check if MachineOperand \p MO is a last use/kill either in the
116  /// main live range of \p LI or in one of the matching subregister ranges.
117  bool useIsKill(const LiveInterval &LI, const MachineOperand &MO) const;
118 
119  /// Create a new empty interval based on OldReg.
120  LiveInterval &createEmptyIntervalFrom(Register OldReg, bool createSubRanges);
121 
122 public:
123  /// Create a LiveRangeEdit for breaking down parent into smaller pieces.
124  /// @param parent The register being spilled or split.
125  /// @param newRegs List to receive any new registers created. This needn't be
126  /// empty initially, any existing registers are ignored.
127  /// @param MF The MachineFunction the live range edit is taking place in.
128  /// @param lis The collection of all live intervals in this function.
129  /// @param vrm Map of virtual registers to physical registers for this
130  /// function. If NULL, no virtual register map updates will
131  /// be done. This could be the case if called before Regalloc.
132  /// @param deadRemats The collection of all the instructions defining an
133  /// original reg and are dead after remat.
135  MachineFunction &MF, LiveIntervals &lis, VirtRegMap *vrm,
136  Delegate *delegate = nullptr,
137  SmallPtrSet<MachineInstr *, 32> *deadRemats = nullptr)
138  : Parent(parent), NewRegs(newRegs), MRI(MF.getRegInfo()), LIS(lis),
139  VRM(vrm), TII(*MF.getSubtarget().getInstrInfo()), TheDelegate(delegate),
140  FirstNew(newRegs.size()), DeadRemats(deadRemats) {
141  MRI.setDelegate(this);
142  }
143 
144  ~LiveRangeEdit() override { MRI.resetDelegate(this); }
145 
147  assert(Parent && "No parent LiveInterval");
148  return *Parent;
149  }
150 
151  Register getReg() const { return getParent().reg(); }
152 
153  /// Iterator for accessing the new registers added by this edit.
155  iterator begin() const { return NewRegs.begin() + FirstNew; }
156  iterator end() const { return NewRegs.end(); }
157  unsigned size() const { return NewRegs.size() - FirstNew; }
158  bool empty() const { return size() == 0; }
159  Register get(unsigned idx) const { return NewRegs[idx + FirstNew]; }
160 
161  /// pop_back - It allows LiveRangeEdit users to drop new registers.
162  /// The context is when an original def instruction of a register is
163  /// dead after rematerialization, we still want to keep it for following
164  /// rematerializations. We save the def instruction in DeadRemats,
165  /// and replace the original dst register with a new dummy register so
166  /// the live range of original dst register can be shrinked normally.
167  /// We don't want to allocate phys register for the dummy register, so
168  /// we want to drop it from the NewRegs set.
169  void pop_back() { NewRegs.pop_back(); }
170 
172  return makeArrayRef(NewRegs).slice(FirstNew);
173  }
174 
175  /// createFrom - Create a new virtual register based on OldReg.
176  Register createFrom(Register OldReg);
177 
178  /// create - Create a new register with the same class and original slot as
179  /// parent.
181  return createEmptyIntervalFrom(getReg(), true);
182  }
183 
184  Register create() { return createFrom(getReg()); }
185 
186  /// anyRematerializable - Return true if any parent values may be
187  /// rematerializable.
188  /// This function must be called before any rematerialization is attempted.
190 
191  /// checkRematerializable - Manually add VNI to the list of rematerializable
192  /// values if DefMI may be rematerializable.
194  AAResults *);
195 
196  /// Remat - Information needed to rematerialize at a specific location.
197  struct Remat {
198  VNInfo *ParentVNI; // parent_'s value at the remat location.
199  MachineInstr *OrigMI = nullptr; // Instruction defining OrigVNI. It contains
200  // the real expr for remat.
201 
203  };
204 
205  /// allUsesAvailableAt - Return true if all registers used by OrigMI at
206  /// OrigIdx are also available with the same value at UseIdx.
207  bool allUsesAvailableAt(const MachineInstr *OrigMI, SlotIndex OrigIdx,
208  SlotIndex UseIdx) const;
209 
210  /// canRematerializeAt - Determine if ParentVNI can be rematerialized at
211  /// UseIdx. It is assumed that parent_.getVNINfoAt(UseIdx) == ParentVNI.
212  /// When cheapAsAMove is set, only cheap remats are allowed.
213  bool canRematerializeAt(Remat &RM, VNInfo *OrigVNI, SlotIndex UseIdx,
214  bool cheapAsAMove);
215 
216  /// rematerializeAt - Rematerialize RM.ParentVNI into DestReg by inserting an
217  /// instruction into MBB before MI. The new instruction is mapped, but
218  /// liveness is not updated.
219  /// Return the SlotIndex of the new instruction.
221  MachineBasicBlock::iterator MI, unsigned DestReg,
222  const Remat &RM, const TargetRegisterInfo &,
223  bool Late = false);
224 
225  /// markRematerialized - explicitly mark a value as rematerialized after doing
226  /// it manually.
227  void markRematerialized(const VNInfo *ParentVNI) {
228  Rematted.insert(ParentVNI);
229  }
230 
231  /// didRematerialize - Return true if ParentVNI was rematerialized anywhere.
232  bool didRematerialize(const VNInfo *ParentVNI) const {
233  return Rematted.count(ParentVNI);
234  }
235 
236  /// eraseVirtReg - Notify the delegate that Reg is no longer in use, and try
237  /// to erase it from LIS.
238  void eraseVirtReg(Register Reg);
239 
240  /// eliminateDeadDefs - Try to delete machine instructions that are now dead
241  /// (allDefsAreDead returns true). This may cause live intervals to be trimmed
242  /// and further dead efs to be eliminated.
243  /// RegsBeingSpilled lists registers currently being spilled by the register
244  /// allocator. These registers should not be split into new intervals
245  /// as currently those new intervals are not guaranteed to spill.
247  ArrayRef<Register> RegsBeingSpilled = None,
248  AAResults *AA = nullptr);
249 
250  /// calculateRegClassAndHint - Recompute register class and hint for each new
251  /// register.
253 };
254 
255 } // end namespace llvm
256 
257 #endif // LLVM_CODEGEN_LIVERANGEEDIT_H
llvm::LiveRangeEdit::Remat::ParentVNI
VNInfo * ParentVNI
Definition: LiveRangeEdit.h:198
llvm::LiveRangeEdit::Remat
Remat - Information needed to rematerialize at a specific location.
Definition: LiveRangeEdit.h:197
llvm::LiveRangeEdit::checkRematerializable
bool checkRematerializable(VNInfo *VNI, const MachineInstr *DefMI, AAResults *)
checkRematerializable - Manually add VNI to the list of rematerializable values if DefMI may be remat...
Definition: LiveRangeEdit.cpp:70
llvm::LiveRangeEdit::iterator
SmallVectorImpl< Register >::const_iterator iterator
Iterator for accessing the new registers added by this edit.
Definition: LiveRangeEdit.h:154
MI
IRTranslator LLVM IR MI
Definition: IRTranslator.cpp:103
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
Reg
unsigned Reg
Definition: MachineSink.cpp:1566
llvm::LiveRangeEdit::calculateRegClassAndHint
void calculateRegClassAndHint(MachineFunction &, VirtRegAuxInfo &)
calculateRegClassAndHint - Recompute register class and hint for each new register.
Definition: LiveRangeEdit.cpp:469
llvm::MachineRegisterInfo
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
Definition: MachineRegisterInfo.h:52
aa
aa
Definition: AliasAnalysis.cpp:846
llvm::LiveRangeEdit::Delegate::LRE_CanEraseVirtReg
virtual bool LRE_CanEraseVirtReg(Register)
Called when a virtual register is no longer used.
Definition: LiveRangeEdit.h:60
llvm::LiveRangeEdit::createFrom
Register createFrom(Register OldReg)
createFrom - Create a new virtual register based on OldReg.
Definition: LiveRangeEdit.cpp:54
llvm::VirtRegAuxInfo
Calculate auxiliary information for a virtual register such as its spill weight and allocation hint.
Definition: CalcSpillWeights.h:46
llvm::VirtRegMap
Definition: VirtRegMap.h:33
MachineBasicBlock.h
llvm::TargetRegisterInfo
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
Definition: TargetRegisterInfo.h:233
llvm::LiveRangeEdit::eliminateDeadDefs
void eliminateDeadDefs(SmallVectorImpl< MachineInstr * > &Dead, ArrayRef< Register > RegsBeingSpilled=None, AAResults *AA=nullptr)
eliminateDeadDefs - Try to delete machine instructions that are now dead (allDefsAreDead returns true...
Definition: LiveRangeEdit.cpp:402
llvm::SmallPtrSet
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
Definition: SmallPtrSet.h:449
llvm::LiveRangeEdit::getReg
Register getReg() const
Definition: LiveRangeEdit.h:151
llvm::LiveRangeEdit::Delegate::LRE_WillEraseInstruction
virtual void LRE_WillEraseInstruction(MachineInstr *MI)
Called immediately before erasing a dead machine instruction.
Definition: LiveRangeEdit.h:56
llvm::LiveRangeEdit::didRematerialize
bool didRematerialize(const VNInfo *ParentVNI) const
didRematerialize - Return true if ParentVNI was rematerialized anywhere.
Definition: LiveRangeEdit.h:232
MachineRegisterInfo.h
llvm::LiveRangeEdit::regs
ArrayRef< Register > regs() const
Definition: LiveRangeEdit.h:171
llvm::TargetInstrInfo
TargetInstrInfo - Interface to description of machine instruction set.
Definition: TargetInstrInfo.h:97
llvm::LiveRangeEdit::get
Register get(unsigned idx) const
Definition: LiveRangeEdit.h:159
llvm::AAResults
Definition: AliasAnalysis.h:456
llvm::LiveRangeEdit::anyRematerializable
bool anyRematerializable(AAResults *)
anyRematerializable - Return true if any parent values may be rematerializable.
Definition: LiveRangeEdit.cpp:98
TII
const HexagonInstrInfo * TII
Definition: HexagonCopyToCombine.cpp:129
llvm::MachineOperand
MachineOperand class - Representation of each machine instruction operand.
Definition: MachineOperand.h:49
llvm::LiveRangeEdit
Definition: LiveRangeEdit.h:46
SmallPtrSet.h
llvm::LiveRangeEdit::eraseVirtReg
void eraseVirtReg(Register Reg)
eraseVirtReg - Notify the delegate that Reg is no longer in use, and try to erase it from LIS.
Definition: LiveRangeEdit.cpp:181
llvm::LiveRangeEdit::~LiveRangeEdit
~LiveRangeEdit() override
Definition: LiveRangeEdit.h:144
llvm::LiveInterval
LiveInterval - This class represents the liveness of a register, or stack slot.
Definition: LiveInterval.h:680
llvm::SlotIndex
SlotIndex - An opaque wrapper around machine indexes.
Definition: SlotIndexes.h:83
llvm::None
const NoneType None
Definition: None.h:23
llvm::LiveRangeEdit::Remat::Remat
Remat(VNInfo *ParentVNI)
Definition: LiveRangeEdit.h:202
llvm::MachineBasicBlock
Definition: MachineBasicBlock.h:95
llvm::LiveRangeEdit::getParent
LiveInterval & getParent() const
Definition: LiveRangeEdit.h:146
llvm::MachineInstr
Representation of each machine instruction.
Definition: MachineInstr.h:64
llvm::LiveRangeEdit::createEmptyInterval
LiveInterval & createEmptyInterval()
create - Create a new register with the same class and original slot as parent.
Definition: LiveRangeEdit.h:180
llvm::LiveRangeEdit::pop_back
void pop_back()
pop_back - It allows LiveRangeEdit users to drop new registers.
Definition: LiveRangeEdit.h:169
llvm::LiveRangeEdit::Delegate::~Delegate
virtual ~Delegate()=default
ArrayRef.h
llvm::SmallVectorImpl::const_iterator
typename SuperClass::const_iterator const_iterator
Definition: SmallVector.h:563
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::LiveRangeEdit::canRematerializeAt
bool canRematerializeAt(Remat &RM, VNInfo *OrigVNI, SlotIndex UseIdx, bool cheapAsAMove)
canRematerializeAt - Determine if ParentVNI can be rematerialized at UseIdx.
Definition: LiveRangeEdit.cpp:141
llvm::SmallPtrSetImpl::count
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
Definition: SmallPtrSet.h:382
llvm::LiveRangeEdit::markRematerialized
void markRematerialized(const VNInfo *ParentVNI)
markRematerialized - explicitly mark a value as rematerialized after doing it manually.
Definition: LiveRangeEdit.h:227
llvm::MachineFunction
Definition: MachineFunction.h:230
llvm::AArch64::RM
@ RM
Definition: AArch64ISelLowering.h:472
llvm::MachineRegisterInfo::Delegate
Definition: MachineRegisterInfo.h:54
llvm::ArrayRef
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: APInt.h:32
None.h
llvm::LiveRangeEdit::rematerializeAt
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...
Definition: LiveRangeEdit.cpp:165
llvm::LiveRangeEdit::create
Register create()
Definition: LiveRangeEdit.h:184
TargetSubtargetInfo.h
llvm::LiveRangeEdit::empty
bool empty() const
Definition: LiveRangeEdit.h:158
MRI
unsigned const MachineRegisterInfo * MRI
Definition: AArch64AdvSIMDScalarPass.cpp:105
llvm::Register
Wrapper class representing virtual and physical registers.
Definition: Register.h:19
llvm::LiveRangeEdit::begin
iterator begin() const
Definition: LiveRangeEdit.h:155
MBB
MachineBasicBlock & MBB
Definition: AArch64SLSHardening.cpp:74
llvm::LiveRangeEdit::allUsesAvailableAt
bool allUsesAvailableAt(const MachineInstr *OrigMI, SlotIndex OrigIdx, SlotIndex UseIdx) const
allUsesAvailableAt - Return true if all registers used by OrigMI at OrigIdx are also available with t...
Definition: LiveRangeEdit.cpp:106
llvm::LiveRangeEdit::Delegate::LRE_DidCloneVirtReg
virtual void LRE_DidCloneVirtReg(Register New, Register Old)
Called after cloning a virtual register.
Definition: LiveRangeEdit.h:67
llvm::LiveIntervals
Definition: LiveIntervals.h:54
llvm::VNInfo
VNInfo - Value Number Information.
Definition: LiveInterval.h:53
llvm::LiveRangeEdit::Remat::OrigMI
MachineInstr * OrigMI
Definition: LiveRangeEdit.h:199
llvm::makeArrayRef
ArrayRef< T > makeArrayRef(const T &OneElt)
Construct an ArrayRef from a single element.
Definition: ArrayRef.h:476
LiveInterval.h
llvm::RegState::Dead
@ Dead
Unused definition.
Definition: MachineInstrBuilder.h:50
SmallVector.h
llvm::LiveRangeEdit::size
unsigned size() const
Definition: LiveRangeEdit.h:157
DefMI
MachineInstrBuilder MachineInstrBuilder & DefMI
Definition: AArch64ExpandPseudoInsts.cpp:103
llvm::LiveInterval::reg
Register reg() const
Definition: LiveInterval.h:711
llvm::SmallVectorImpl
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: APFloat.h:43
llvm::LiveRangeEdit::Delegate
Callback methods for LiveRangeEdit owners.
Definition: LiveRangeEdit.h:49
llvm::LiveRangeEdit::end
iterator end() const
Definition: LiveRangeEdit.h:156
SlotIndexes.h
llvm::LiveRangeEdit::Delegate::LRE_WillShrinkVirtReg
virtual void LRE_WillShrinkVirtReg(Register)
Called before shrinking the live range of a virtual register.
Definition: LiveRangeEdit.h:63
llvm::SetVector
A vector that has set insertion semantics.
Definition: SetVector.h:40
MachineFunction.h
llvm::LiveRangeEdit::LiveRangeEdit
LiveRangeEdit(LiveInterval *parent, SmallVectorImpl< Register > &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.
Definition: LiveRangeEdit.h:134
llvm::MachineInstrBundleIterator< MachineInstr >
SetVector.h
llvm::SmallPtrSetImpl::insert
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
Definition: SmallPtrSet.h:364