LiveRangeEdit.h 10 KB

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