LiveRangeEdit.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- LiveRangeEdit.h - Basic tools for split and spill --------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // The LiveRangeEdit class represents changes done to a virtual register when it
  15. // is spilled or split.
  16. //
  17. // The parent register is never changed. Instead, a number of new virtual
  18. // registers are created and added to the newRegs vector.
  19. //
  20. //===----------------------------------------------------------------------===//
  21. #ifndef LLVM_CODEGEN_LIVERANGEEDIT_H
  22. #define LLVM_CODEGEN_LIVERANGEEDIT_H
  23. #include "llvm/ADT/ArrayRef.h"
  24. #include "llvm/ADT/SetVector.h"
  25. #include "llvm/ADT/SmallPtrSet.h"
  26. #include "llvm/ADT/SmallVector.h"
  27. #include "llvm/CodeGen/LiveInterval.h"
  28. #include "llvm/CodeGen/MachineBasicBlock.h"
  29. #include "llvm/CodeGen/MachineFunction.h"
  30. #include "llvm/CodeGen/MachineRegisterInfo.h"
  31. #include "llvm/CodeGen/SlotIndexes.h"
  32. #include "llvm/CodeGen/TargetSubtargetInfo.h"
  33. #include <cassert>
  34. namespace llvm {
  35. class LiveIntervals;
  36. class MachineInstr;
  37. class MachineOperand;
  38. class TargetInstrInfo;
  39. class TargetRegisterInfo;
  40. class VirtRegMap;
  41. class VirtRegAuxInfo;
  42. class LiveRangeEdit : private MachineRegisterInfo::Delegate {
  43. public:
  44. /// Callback methods for LiveRangeEdit owners.
  45. class Delegate {
  46. virtual void anchor();
  47. public:
  48. virtual ~Delegate() = default;
  49. /// Called immediately before erasing a dead machine instruction.
  50. virtual void LRE_WillEraseInstruction(MachineInstr *MI) {}
  51. /// Called when a virtual register is no longer used. Return false to defer
  52. /// its deletion from LiveIntervals.
  53. virtual bool LRE_CanEraseVirtReg(Register) { return true; }
  54. /// Called before shrinking the live range of a virtual register.
  55. virtual void LRE_WillShrinkVirtReg(Register) {}
  56. /// Called after cloning a virtual register.
  57. /// This is used for new registers representing connected components of Old.
  58. virtual void LRE_DidCloneVirtReg(Register New, Register Old) {}
  59. };
  60. private:
  61. const LiveInterval *const Parent;
  62. SmallVectorImpl<Register> &NewRegs;
  63. MachineRegisterInfo &MRI;
  64. LiveIntervals &LIS;
  65. VirtRegMap *VRM;
  66. const TargetInstrInfo &TII;
  67. Delegate *const TheDelegate;
  68. /// FirstNew - Index of the first register added to NewRegs.
  69. const unsigned FirstNew;
  70. /// ScannedRemattable - true when remattable values have been identified.
  71. bool ScannedRemattable = false;
  72. /// DeadRemats - The saved instructions which have already been dead after
  73. /// rematerialization but not deleted yet -- to be done in postOptimization.
  74. SmallPtrSet<MachineInstr *, 32> *DeadRemats;
  75. /// Remattable - Values defined by remattable instructions as identified by
  76. /// tii.isTriviallyReMaterializable().
  77. SmallPtrSet<const VNInfo *, 4> Remattable;
  78. /// Rematted - Values that were actually rematted, and so need to have their
  79. /// live range trimmed or entirely removed.
  80. SmallPtrSet<const VNInfo *, 4> Rematted;
  81. /// scanRemattable - Identify the Parent values that may rematerialize.
  82. void scanRemattable();
  83. /// foldAsLoad - If LI has a single use and a single def that can be folded as
  84. /// a load, eliminate the register by folding the def into the use.
  85. bool foldAsLoad(LiveInterval *LI, SmallVectorImpl<MachineInstr *> &Dead);
  86. using ToShrinkSet = SetVector<LiveInterval *, SmallVector<LiveInterval *, 8>,
  87. SmallPtrSet<LiveInterval *, 8>>;
  88. /// Helper for eliminateDeadDefs.
  89. void eliminateDeadDef(MachineInstr *MI, ToShrinkSet &ToShrink);
  90. /// MachineRegisterInfo callback to notify when new virtual
  91. /// registers are created.
  92. void MRI_NoteNewVirtualRegister(Register VReg) override;
  93. /// Check if MachineOperand \p MO is a last use/kill either in the
  94. /// main live range of \p LI or in one of the matching subregister ranges.
  95. bool useIsKill(const LiveInterval &LI, const MachineOperand &MO) const;
  96. /// Create a new empty interval based on OldReg.
  97. LiveInterval &createEmptyIntervalFrom(Register OldReg, bool createSubRanges);
  98. public:
  99. /// Create a LiveRangeEdit for breaking down parent into smaller pieces.
  100. /// @param parent The register being spilled or split.
  101. /// @param newRegs List to receive any new registers created. This needn't be
  102. /// empty initially, any existing registers are ignored.
  103. /// @param MF The MachineFunction the live range edit is taking place in.
  104. /// @param lis The collection of all live intervals in this function.
  105. /// @param vrm Map of virtual registers to physical registers for this
  106. /// function. If NULL, no virtual register map updates will
  107. /// be done. This could be the case if called before Regalloc.
  108. /// @param deadRemats The collection of all the instructions defining an
  109. /// original reg and are dead after remat.
  110. LiveRangeEdit(const LiveInterval *parent, SmallVectorImpl<Register> &newRegs,
  111. MachineFunction &MF, LiveIntervals &lis, VirtRegMap *vrm,
  112. Delegate *delegate = nullptr,
  113. SmallPtrSet<MachineInstr *, 32> *deadRemats = nullptr)
  114. : Parent(parent), NewRegs(newRegs), MRI(MF.getRegInfo()), LIS(lis),
  115. VRM(vrm), TII(*MF.getSubtarget().getInstrInfo()), TheDelegate(delegate),
  116. FirstNew(newRegs.size()), DeadRemats(deadRemats) {
  117. MRI.addDelegate(this);
  118. }
  119. ~LiveRangeEdit() override { MRI.resetDelegate(this); }
  120. const LiveInterval &getParent() const {
  121. assert(Parent && "No parent LiveInterval");
  122. return *Parent;
  123. }
  124. Register getReg() const { return getParent().reg(); }
  125. /// Iterator for accessing the new registers added by this edit.
  126. using iterator = SmallVectorImpl<Register>::const_iterator;
  127. iterator begin() const { return NewRegs.begin() + FirstNew; }
  128. iterator end() const { return NewRegs.end(); }
  129. unsigned size() const { return NewRegs.size() - FirstNew; }
  130. bool empty() const { return size() == 0; }
  131. Register get(unsigned idx) const { return NewRegs[idx + FirstNew]; }
  132. /// pop_back - It allows LiveRangeEdit users to drop new registers.
  133. /// The context is when an original def instruction of a register is
  134. /// dead after rematerialization, we still want to keep it for following
  135. /// rematerializations. We save the def instruction in DeadRemats,
  136. /// and replace the original dst register with a new dummy register so
  137. /// the live range of original dst register can be shrinked normally.
  138. /// We don't want to allocate phys register for the dummy register, so
  139. /// we want to drop it from the NewRegs set.
  140. void pop_back() { NewRegs.pop_back(); }
  141. ArrayRef<Register> regs() const { return ArrayRef(NewRegs).slice(FirstNew); }
  142. /// createFrom - Create a new virtual register based on OldReg.
  143. Register createFrom(Register OldReg);
  144. /// create - Create a new register with the same class and original slot as
  145. /// parent.
  146. LiveInterval &createEmptyInterval() {
  147. return createEmptyIntervalFrom(getReg(), true);
  148. }
  149. Register create() { return createFrom(getReg()); }
  150. /// anyRematerializable - Return true if any parent values may be
  151. /// rematerializable.
  152. /// This function must be called before any rematerialization is attempted.
  153. bool anyRematerializable();
  154. /// checkRematerializable - Manually add VNI to the list of rematerializable
  155. /// values if DefMI may be rematerializable.
  156. bool checkRematerializable(VNInfo *VNI, const MachineInstr *DefMI);
  157. /// Remat - Information needed to rematerialize at a specific location.
  158. struct Remat {
  159. const VNInfo *const ParentVNI; // parent_'s value at the remat location.
  160. MachineInstr *OrigMI = nullptr; // Instruction defining OrigVNI. It contains
  161. // the real expr for remat.
  162. explicit Remat(const VNInfo *ParentVNI) : ParentVNI(ParentVNI) {}
  163. };
  164. /// allUsesAvailableAt - Return true if all registers used by OrigMI at
  165. /// OrigIdx are also available with the same value at UseIdx.
  166. bool allUsesAvailableAt(const MachineInstr *OrigMI, SlotIndex OrigIdx,
  167. SlotIndex UseIdx) const;
  168. /// canRematerializeAt - Determine if ParentVNI can be rematerialized at
  169. /// UseIdx. It is assumed that parent_.getVNINfoAt(UseIdx) == ParentVNI.
  170. /// When cheapAsAMove is set, only cheap remats are allowed.
  171. bool canRematerializeAt(Remat &RM, VNInfo *OrigVNI, SlotIndex UseIdx,
  172. bool cheapAsAMove);
  173. /// rematerializeAt - Rematerialize RM.ParentVNI into DestReg by inserting an
  174. /// instruction into MBB before MI. The new instruction is mapped, but
  175. /// liveness is not updated. If ReplaceIndexMI is not null it will be replaced
  176. /// by new MI in the index map.
  177. /// Return the SlotIndex of the new instruction.
  178. SlotIndex rematerializeAt(MachineBasicBlock &MBB,
  179. MachineBasicBlock::iterator MI, unsigned DestReg,
  180. const Remat &RM, const TargetRegisterInfo &,
  181. bool Late = false, unsigned SubIdx = 0,
  182. MachineInstr *ReplaceIndexMI = nullptr);
  183. /// markRematerialized - explicitly mark a value as rematerialized after doing
  184. /// it manually.
  185. void markRematerialized(const VNInfo *ParentVNI) {
  186. Rematted.insert(ParentVNI);
  187. }
  188. /// didRematerialize - Return true if ParentVNI was rematerialized anywhere.
  189. bool didRematerialize(const VNInfo *ParentVNI) const {
  190. return Rematted.count(ParentVNI);
  191. }
  192. /// eraseVirtReg - Notify the delegate that Reg is no longer in use, and try
  193. /// to erase it from LIS.
  194. void eraseVirtReg(Register Reg);
  195. /// eliminateDeadDefs - Try to delete machine instructions that are now dead
  196. /// (allDefsAreDead returns true). This may cause live intervals to be trimmed
  197. /// and further dead efs to be eliminated.
  198. /// RegsBeingSpilled lists registers currently being spilled by the register
  199. /// allocator. These registers should not be split into new intervals
  200. /// as currently those new intervals are not guaranteed to spill.
  201. void eliminateDeadDefs(SmallVectorImpl<MachineInstr *> &Dead,
  202. ArrayRef<Register> RegsBeingSpilled = std::nullopt);
  203. /// calculateRegClassAndHint - Recompute register class and hint for each new
  204. /// register.
  205. void calculateRegClassAndHint(MachineFunction &, VirtRegAuxInfo &);
  206. };
  207. } // end namespace llvm
  208. #endif // LLVM_CODEGEN_LIVERANGEEDIT_H
  209. #ifdef __GNUC__
  210. #pragma GCC diagnostic pop
  211. #endif