AggressiveAntiDepBreaker.h 6.7 KB

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