LiveRangeEdit.h 11 KB

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