AggressiveAntiDepBreaker.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. //==- llvm/CodeGen/AggressiveAntiDepBreaker.h - Anti-Dep Support -*- 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. // This file implements the AggressiveAntiDepBreaker class, which
  10. // implements register anti-dependence breaking during post-RA
  11. // scheduling. It attempts to break all anti-dependencies within a
  12. // block.
  13. //
  14. //===----------------------------------------------------------------------===//
  15. #ifndef LLVM_LIB_CODEGEN_AGGRESSIVEANTIDEPBREAKER_H
  16. #define LLVM_LIB_CODEGEN_AGGRESSIVEANTIDEPBREAKER_H
  17. #include "llvm/ADT/BitVector.h"
  18. #include "llvm/CodeGen/AntiDepBreaker.h"
  19. #include "llvm/CodeGen/TargetSubtargetInfo.h"
  20. #include "llvm/Support/Compiler.h"
  21. #include <map>
  22. #include <set>
  23. #include <vector>
  24. namespace llvm {
  25. class MachineBasicBlock;
  26. class MachineFunction;
  27. class MachineInstr;
  28. class MachineOperand;
  29. class MachineRegisterInfo;
  30. class RegisterClassInfo;
  31. class TargetInstrInfo;
  32. class TargetRegisterClass;
  33. class TargetRegisterInfo;
  34. /// Contains all the state necessary for anti-dep breaking.
  35. class LLVM_LIBRARY_VISIBILITY AggressiveAntiDepState {
  36. public:
  37. /// Information about a register reference within a liverange
  38. struct RegisterReference {
  39. /// The registers operand
  40. MachineOperand *Operand;
  41. /// The register class
  42. const TargetRegisterClass *RC;
  43. };
  44. private:
  45. /// Number of non-virtual target registers (i.e. TRI->getNumRegs()).
  46. const unsigned NumTargetRegs;
  47. /// Implements a disjoint-union data structure to
  48. /// form register groups. A node is represented by an index into
  49. /// the vector. A node can "point to" itself to indicate that it
  50. /// is the parent of a group, or point to another node to indicate
  51. /// that it is a member of the same group as that node.
  52. std::vector<unsigned> GroupNodes;
  53. /// For each register, the index of the GroupNode
  54. /// currently representing the group that the register belongs to.
  55. /// Register 0 is always represented by the 0 group, a group
  56. /// composed of registers that are not eligible for anti-aliasing.
  57. std::vector<unsigned> GroupNodeIndices;
  58. /// Map registers to all their references within a live range.
  59. std::multimap<unsigned, RegisterReference> RegRefs;
  60. /// The index of the most recent kill (proceeding bottom-up),
  61. /// or ~0u if the register is not live.
  62. std::vector<unsigned> KillIndices;
  63. /// The index of the most recent complete def (proceeding bottom
  64. /// up), or ~0u if the register is live.
  65. std::vector<unsigned> DefIndices;
  66. public:
  67. AggressiveAntiDepState(const unsigned TargetRegs, MachineBasicBlock *BB);
  68. /// Return the kill indices.
  69. std::vector<unsigned> &GetKillIndices() { return KillIndices; }
  70. /// Return the define indices.
  71. std::vector<unsigned> &GetDefIndices() { return DefIndices; }
  72. /// Return the RegRefs map.
  73. std::multimap<unsigned, RegisterReference>& GetRegRefs() { return RegRefs; }
  74. // Get the group for a register. The returned value is
  75. // the index of the GroupNode representing the group.
  76. unsigned GetGroup(unsigned Reg);
  77. // Return a vector of the registers belonging to a group.
  78. // If RegRefs is non-NULL then only included referenced registers.
  79. void GetGroupRegs(
  80. unsigned Group,
  81. std::vector<unsigned> &Regs,
  82. std::multimap<unsigned,
  83. AggressiveAntiDepState::RegisterReference> *RegRefs);
  84. // Union Reg1's and Reg2's groups to form a new group.
  85. // Return the index of the GroupNode representing the group.
  86. unsigned UnionGroups(unsigned Reg1, unsigned Reg2);
  87. // Remove a register from its current group and place
  88. // it alone in its own group. Return the index of the GroupNode
  89. // representing the registers new group.
  90. unsigned LeaveGroup(unsigned Reg);
  91. /// Return true if Reg is live.
  92. bool IsLive(unsigned Reg);
  93. };
  94. class LLVM_LIBRARY_VISIBILITY AggressiveAntiDepBreaker
  95. : public AntiDepBreaker {
  96. MachineFunction &MF;
  97. MachineRegisterInfo &MRI;
  98. const TargetInstrInfo *TII;
  99. const TargetRegisterInfo *TRI;
  100. const RegisterClassInfo &RegClassInfo;
  101. /// The set of registers that should only be
  102. /// renamed if they are on the critical path.
  103. BitVector CriticalPathSet;
  104. /// The state used to identify and rename anti-dependence registers.
  105. AggressiveAntiDepState *State = nullptr;
  106. public:
  107. AggressiveAntiDepBreaker(MachineFunction &MFi,
  108. const RegisterClassInfo &RCI,
  109. TargetSubtargetInfo::RegClassVector& CriticalPathRCs);
  110. ~AggressiveAntiDepBreaker() override;
  111. /// Initialize anti-dep breaking for a new basic block.
  112. void StartBlock(MachineBasicBlock *BB) override;
  113. /// Identifiy anti-dependencies along the critical path
  114. /// of the ScheduleDAG and break them by renaming registers.
  115. unsigned BreakAntiDependencies(const std::vector<SUnit> &SUnits,
  116. MachineBasicBlock::iterator Begin,
  117. MachineBasicBlock::iterator End,
  118. unsigned InsertPosIndex,
  119. DbgValueVector &DbgValues) override;
  120. /// Update liveness information to account for the current
  121. /// instruction, which will not be scheduled.
  122. void Observe(MachineInstr &MI, unsigned Count,
  123. unsigned InsertPosIndex) override;
  124. /// Finish anti-dep breaking for a basic block.
  125. void FinishBlock() override;
  126. private:
  127. /// Keep track of a position in the allocation order for each regclass.
  128. using RenameOrderType = std::map<const TargetRegisterClass *, unsigned>;
  129. /// Return true if MO represents a register
  130. /// that is both implicitly used and defined in MI
  131. bool IsImplicitDefUse(MachineInstr &MI, MachineOperand &MO);
  132. /// If MI implicitly def/uses a register, then
  133. /// return that register and all subregisters.
  134. void GetPassthruRegs(MachineInstr &MI, std::set<unsigned> &PassthruRegs);
  135. void HandleLastUse(unsigned Reg, unsigned KillIdx, const char *tag,
  136. const char *header = nullptr,
  137. const char *footer = nullptr);
  138. void PrescanInstruction(MachineInstr &MI, unsigned Count,
  139. std::set<unsigned> &PassthruRegs);
  140. void ScanInstruction(MachineInstr &MI, unsigned Count);
  141. BitVector GetRenameRegisters(unsigned Reg);
  142. bool FindSuitableFreeRegisters(unsigned AntiDepGroupIndex,
  143. RenameOrderType& RenameOrder,
  144. std::map<unsigned, unsigned> &RenameMap);
  145. };
  146. } // end namespace llvm
  147. #endif // LLVM_LIB_CODEGEN_AGGRESSIVEANTIDEPBREAKER_H