LiveRegMatrix.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. //===-- LiveRegMatrix.h - Track register interference ---------*- 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 LiveRegMatrix analysis pass keeps track of virtual register interference
  11. // along two dimensions: Slot indexes and register units. The matrix is used by
  12. // register allocators to ensure that no interfering virtual registers get
  13. // assigned to overlapping physical registers.
  14. //
  15. // Register units are defined in MCRegisterInfo.h, they represent the smallest
  16. // unit of interference when dealing with overlapping physical registers. The
  17. // LiveRegMatrix is represented as a LiveIntervalUnion per register unit. When
  18. // a virtual register is assigned to a physical register, the live range for
  19. // the virtual register is inserted into the LiveIntervalUnion for each regunit
  20. // in the physreg.
  21. //
  22. //===----------------------------------------------------------------------===//
  23. #ifndef LLVM_CODEGEN_LIVEREGMATRIX_H
  24. #define LLVM_CODEGEN_LIVEREGMATRIX_H
  25. #include "llvm/ADT/BitVector.h"
  26. #include "llvm/CodeGen/LiveIntervalUnion.h"
  27. #include "llvm/CodeGen/MachineFunctionPass.h"
  28. namespace llvm {
  29. class LiveInterval;
  30. class LiveIntervalAnalysis;
  31. class MachineRegisterInfo;
  32. class TargetRegisterInfo;
  33. class VirtRegMap;
  34. class LiveRegMatrix : public MachineFunctionPass {
  35. const TargetRegisterInfo *TRI;
  36. MachineRegisterInfo *MRI;
  37. LiveIntervals *LIS;
  38. VirtRegMap *VRM;
  39. // UserTag changes whenever virtual registers have been modified.
  40. unsigned UserTag;
  41. // The matrix is represented as a LiveIntervalUnion per register unit.
  42. LiveIntervalUnion::Allocator LIUAlloc;
  43. LiveIntervalUnion::Array Matrix;
  44. // Cached queries per register unit.
  45. std::unique_ptr<LiveIntervalUnion::Query[]> Queries;
  46. // Cached register mask interference info.
  47. unsigned RegMaskTag;
  48. unsigned RegMaskVirtReg;
  49. BitVector RegMaskUsable;
  50. // MachineFunctionPass boilerplate.
  51. void getAnalysisUsage(AnalysisUsage&) const override;
  52. bool runOnMachineFunction(MachineFunction&) override;
  53. void releaseMemory() override;
  54. public:
  55. static char ID;
  56. LiveRegMatrix();
  57. //===--------------------------------------------------------------------===//
  58. // High-level interface.
  59. //===--------------------------------------------------------------------===//
  60. //
  61. // Check for interference before assigning virtual registers to physical
  62. // registers.
  63. //
  64. /// Invalidate cached interference queries after modifying virtual register
  65. /// live ranges. Interference checks may return stale information unless
  66. /// caches are invalidated.
  67. void invalidateVirtRegs() { ++UserTag; }
  68. enum InterferenceKind {
  69. /// No interference, go ahead and assign.
  70. IK_Free = 0,
  71. /// Virtual register interference. There are interfering virtual registers
  72. /// assigned to PhysReg or its aliases. This interference could be resolved
  73. /// by unassigning those other virtual registers.
  74. IK_VirtReg,
  75. /// Register unit interference. A fixed live range is in the way, typically
  76. /// argument registers for a call. This can't be resolved by unassigning
  77. /// other virtual registers.
  78. IK_RegUnit,
  79. /// RegMask interference. The live range is crossing an instruction with a
  80. /// regmask operand that doesn't preserve PhysReg. This typically means
  81. /// VirtReg is live across a call, and PhysReg isn't call-preserved.
  82. IK_RegMask
  83. };
  84. /// Check for interference before assigning VirtReg to PhysReg.
  85. /// If this function returns IK_Free, it is legal to assign(VirtReg, PhysReg).
  86. /// When there is more than one kind of interference, the InterferenceKind
  87. /// with the highest enum value is returned.
  88. InterferenceKind checkInterference(LiveInterval &VirtReg, unsigned PhysReg);
  89. /// Assign VirtReg to PhysReg.
  90. /// This will mark VirtReg's live range as occupied in the LiveRegMatrix and
  91. /// update VirtRegMap. The live range is expected to be available in PhysReg.
  92. void assign(LiveInterval &VirtReg, unsigned PhysReg);
  93. /// Unassign VirtReg from its PhysReg.
  94. /// Assuming that VirtReg was previously assigned to a PhysReg, this undoes
  95. /// the assignment and updates VirtRegMap accordingly.
  96. void unassign(LiveInterval &VirtReg);
  97. /// Returns true if the given \p PhysReg has any live intervals assigned.
  98. bool isPhysRegUsed(unsigned PhysReg) const;
  99. //===--------------------------------------------------------------------===//
  100. // Low-level interface.
  101. //===--------------------------------------------------------------------===//
  102. //
  103. // Provide access to the underlying LiveIntervalUnions.
  104. //
  105. /// Check for regmask interference only.
  106. /// Return true if VirtReg crosses a regmask operand that clobbers PhysReg.
  107. /// If PhysReg is null, check if VirtReg crosses any regmask operands.
  108. bool checkRegMaskInterference(LiveInterval &VirtReg, unsigned PhysReg = 0);
  109. /// Check for regunit interference only.
  110. /// Return true if VirtReg overlaps a fixed assignment of one of PhysRegs's
  111. /// register units.
  112. bool checkRegUnitInterference(LiveInterval &VirtReg, unsigned PhysReg);
  113. /// Query a line of the assigned virtual register matrix directly.
  114. /// Use MCRegUnitIterator to enumerate all regunits in the desired PhysReg.
  115. /// This returns a reference to an internal Query data structure that is only
  116. /// valid until the next query() call.
  117. LiveIntervalUnion::Query &query(LiveInterval &VirtReg, unsigned RegUnit);
  118. /// Directly access the live interval unions per regunit.
  119. /// This returns an array indexed by the regunit number.
  120. LiveIntervalUnion *getLiveUnions() { return &Matrix[0]; }
  121. };
  122. } // end namespace llvm
  123. #endif // LLVM_CODEGEN_LIVEREGMATRIX_H