LiveRangeEdit.h 9.1 KB

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