Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

210 lines
8.0 KiB

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