LiveRegMatrix.h 6.6 KB

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