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