LiveRegMatrix.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. //===- LiveRegMatrix.h - Track register interference ----------*- 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 LiveRegMatrix analysis pass keeps track of virtual register interference
  10. // along two dimensions: Slot indexes and register units. The matrix is used by
  11. // register allocators to ensure that no interfering virtual registers get
  12. // assigned to overlapping physical registers.
  13. //
  14. // Register units are defined in MCRegisterInfo.h, they represent the smallest
  15. // unit of interference when dealing with overlapping physical registers. The
  16. // LiveRegMatrix is represented as a LiveIntervalUnion per register unit. When
  17. // a virtual register is assigned to a physical register, the live range for
  18. // the virtual register is inserted into the LiveIntervalUnion for each regunit
  19. // in the physreg.
  20. //
  21. //===----------------------------------------------------------------------===//
  22. #ifndef LLVM_CODEGEN_LIVEREGMATRIX_H
  23. #define LLVM_CODEGEN_LIVEREGMATRIX_H
  24. #include "llvm/ADT/BitVector.h"
  25. #include "llvm/CodeGen/LiveIntervalUnion.h"
  26. #include "llvm/CodeGen/MachineFunctionPass.h"
  27. #include <memory>
  28. namespace llvm {
  29. class AnalysisUsage;
  30. class LiveInterval;
  31. class LiveIntervals;
  32. class MachineFunction;
  33. class TargetRegisterInfo;
  34. class VirtRegMap;
  35. class LiveRegMatrix : public MachineFunctionPass {
  36. const TargetRegisterInfo *TRI;
  37. LiveIntervals *LIS;
  38. VirtRegMap *VRM;
  39. // UserTag changes whenever virtual registers have been modified.
  40. unsigned UserTag = 0;
  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 = 0;
  48. unsigned RegMaskVirtReg = 0;
  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, MCRegister PhysReg);
  89. /// Check for interference in the segment [Start, End) that may prevent
  90. /// assignment to PhysReg. If this function returns true, there is
  91. /// interference in the segment [Start, End) of some other interval already
  92. /// assigned to PhysReg. If this function returns false, PhysReg is free at
  93. /// the segment [Start, End).
  94. bool checkInterference(SlotIndex Start, SlotIndex End, MCRegister PhysReg);
  95. /// Assign VirtReg to PhysReg.
  96. /// This will mark VirtReg's live range as occupied in the LiveRegMatrix and
  97. /// update VirtRegMap. The live range is expected to be available in PhysReg.
  98. void assign(LiveInterval &VirtReg, MCRegister PhysReg);
  99. /// Unassign VirtReg from its PhysReg.
  100. /// Assuming that VirtReg was previously assigned to a PhysReg, this undoes
  101. /// the assignment and updates VirtRegMap accordingly.
  102. void unassign(LiveInterval &VirtReg);
  103. /// Returns true if the given \p PhysReg has any live intervals assigned.
  104. bool isPhysRegUsed(MCRegister PhysReg) const;
  105. //===--------------------------------------------------------------------===//
  106. // Low-level interface.
  107. //===--------------------------------------------------------------------===//
  108. //
  109. // Provide access to the underlying LiveIntervalUnions.
  110. //
  111. /// Check for regmask interference only.
  112. /// Return true if VirtReg crosses a regmask operand that clobbers PhysReg.
  113. /// If PhysReg is null, check if VirtReg crosses any regmask operands.
  114. bool checkRegMaskInterference(LiveInterval &VirtReg,
  115. MCRegister PhysReg = MCRegister::NoRegister);
  116. /// Check for regunit interference only.
  117. /// Return true if VirtReg overlaps a fixed assignment of one of PhysRegs's
  118. /// register units.
  119. bool checkRegUnitInterference(LiveInterval &VirtReg, MCRegister PhysReg);
  120. /// Query a line of the assigned virtual register matrix directly.
  121. /// Use MCRegUnitIterator to enumerate all regunits in the desired PhysReg.
  122. /// This returns a reference to an internal Query data structure that is only
  123. /// valid until the next query() call.
  124. LiveIntervalUnion::Query &query(const LiveRange &LR, MCRegister RegUnit);
  125. /// Directly access the live interval unions per regunit.
  126. /// This returns an array indexed by the regunit number.
  127. LiveIntervalUnion *getLiveUnions() { return &Matrix[0]; }
  128. Register getOneVReg(unsigned PhysReg) const;
  129. };
  130. } // end namespace llvm
  131. #endif // LLVM_CODEGEN_LIVEREGMATRIX_H