MachineOutliner.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. //===---- MachineOutliner.h - Outliner data structures ------*- 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. /// \file
  10. /// Contains all data structures shared between the outliner implemented in
  11. /// MachineOutliner.cpp and target implementations of the outliner.
  12. ///
  13. //===----------------------------------------------------------------------===//
  14. #ifndef LLVM_CODEGEN_MACHINEOUTLINER_H
  15. #define LLVM_CODEGEN_MACHINEOUTLINER_H
  16. #include "llvm/CodeGen/LivePhysRegs.h"
  17. #include "llvm/CodeGen/LiveRegUnits.h"
  18. #include "llvm/CodeGen/MachineFunction.h"
  19. #include "llvm/CodeGen/MachineRegisterInfo.h"
  20. #include "llvm/CodeGen/TargetRegisterInfo.h"
  21. namespace llvm {
  22. namespace outliner {
  23. /// Represents how an instruction should be mapped by the outliner.
  24. /// \p Legal instructions are those which are safe to outline.
  25. /// \p LegalTerminator instructions are safe to outline, but only as the
  26. /// last instruction in a sequence.
  27. /// \p Illegal instructions are those which cannot be outlined.
  28. /// \p Invisible instructions are instructions which can be outlined, but
  29. /// shouldn't actually impact the outlining result.
  30. enum InstrType { Legal, LegalTerminator, Illegal, Invisible };
  31. /// An individual sequence of instructions to be replaced with a call to
  32. /// an outlined function.
  33. struct Candidate {
  34. private:
  35. /// The start index of this \p Candidate in the instruction list.
  36. unsigned StartIdx = 0;
  37. /// The number of instructions in this \p Candidate.
  38. unsigned Len = 0;
  39. // The first instruction in this \p Candidate.
  40. MachineBasicBlock::iterator FirstInst;
  41. // The last instruction in this \p Candidate.
  42. MachineBasicBlock::iterator LastInst;
  43. // The basic block that contains this Candidate.
  44. MachineBasicBlock *MBB = nullptr;
  45. /// Cost of calling an outlined function from this point as defined by the
  46. /// target.
  47. unsigned CallOverhead = 0;
  48. public:
  49. /// The index of this \p Candidate's \p OutlinedFunction in the list of
  50. /// \p OutlinedFunctions.
  51. unsigned FunctionIdx = 0;
  52. /// Identifier denoting the instructions to emit to call an outlined function
  53. /// from this point. Defined by the target.
  54. unsigned CallConstructionID = 0;
  55. /// Contains physical register liveness information for the MBB containing
  56. /// this \p Candidate.
  57. ///
  58. /// This is optionally used by the target to calculate more fine-grained
  59. /// cost model information.
  60. LiveRegUnits LRU;
  61. /// Contains the accumulated register liveness information for the
  62. /// instructions in this \p Candidate.
  63. ///
  64. /// This is optionally used by the target to determine which registers have
  65. /// been used across the sequence.
  66. LiveRegUnits UsedInSequence;
  67. /// Target-specific flags for this Candidate's MBB.
  68. unsigned Flags = 0x0;
  69. /// True if initLRU has been called on this Candidate.
  70. bool LRUWasSet = false;
  71. /// Return the number of instructions in this Candidate.
  72. unsigned getLength() const { return Len; }
  73. /// Return the start index of this candidate.
  74. unsigned getStartIdx() const { return StartIdx; }
  75. /// Return the end index of this candidate.
  76. unsigned getEndIdx() const { return StartIdx + Len - 1; }
  77. /// Set the CallConstructionID and CallOverhead of this candidate to CID and
  78. /// CO respectively.
  79. void setCallInfo(unsigned CID, unsigned CO) {
  80. CallConstructionID = CID;
  81. CallOverhead = CO;
  82. }
  83. /// Returns the call overhead of this candidate if it is in the list.
  84. unsigned getCallOverhead() const { return CallOverhead; }
  85. MachineBasicBlock::iterator &front() { return FirstInst; }
  86. MachineBasicBlock::iterator &back() { return LastInst; }
  87. MachineFunction *getMF() const { return MBB->getParent(); }
  88. MachineBasicBlock *getMBB() const { return MBB; }
  89. /// The number of instructions that would be saved by outlining every
  90. /// candidate of this type.
  91. ///
  92. /// This is a fixed value which is not updated during the candidate pruning
  93. /// process. It is only used for deciding which candidate to keep if two
  94. /// candidates overlap. The true benefit is stored in the OutlinedFunction
  95. /// for some given candidate.
  96. unsigned Benefit = 0;
  97. Candidate(unsigned StartIdx, unsigned Len,
  98. MachineBasicBlock::iterator &FirstInst,
  99. MachineBasicBlock::iterator &LastInst, MachineBasicBlock *MBB,
  100. unsigned FunctionIdx, unsigned Flags)
  101. : StartIdx(StartIdx), Len(Len), FirstInst(FirstInst), LastInst(LastInst),
  102. MBB(MBB), FunctionIdx(FunctionIdx), Flags(Flags) {}
  103. Candidate() {}
  104. /// Used to ensure that \p Candidates are outlined in an order that
  105. /// preserves the start and end indices of other \p Candidates.
  106. bool operator<(const Candidate &RHS) const {
  107. return getStartIdx() > RHS.getStartIdx();
  108. }
  109. /// Compute the registers that are live across this Candidate.
  110. /// Used by targets that need this information for cost model calculation.
  111. /// If a target does not need this information, then this should not be
  112. /// called.
  113. void initLRU(const TargetRegisterInfo &TRI) {
  114. assert(MBB->getParent()->getRegInfo().tracksLiveness() &&
  115. "Candidate's Machine Function must track liveness");
  116. // Only initialize once.
  117. if (LRUWasSet)
  118. return;
  119. LRUWasSet = true;
  120. LRU.init(TRI);
  121. LRU.addLiveOuts(*MBB);
  122. // Compute liveness from the end of the block up to the beginning of the
  123. // outlining candidate.
  124. std::for_each(MBB->rbegin(), (MachineBasicBlock::reverse_iterator)front(),
  125. [this](MachineInstr &MI) { LRU.stepBackward(MI); });
  126. // Walk over the sequence itself and figure out which registers were used
  127. // in the sequence.
  128. UsedInSequence.init(TRI);
  129. std::for_each(front(), std::next(back()),
  130. [this](MachineInstr &MI) { UsedInSequence.accumulate(MI); });
  131. }
  132. };
  133. /// The information necessary to create an outlined function for some
  134. /// class of candidate.
  135. struct OutlinedFunction {
  136. public:
  137. std::vector<Candidate> Candidates;
  138. /// The actual outlined function created.
  139. /// This is initialized after we go through and create the actual function.
  140. MachineFunction *MF = nullptr;
  141. /// Represents the size of a sequence in bytes. (Some instructions vary
  142. /// widely in size, so just counting the instructions isn't very useful.)
  143. unsigned SequenceSize = 0;
  144. /// Target-defined overhead of constructing a frame for this function.
  145. unsigned FrameOverhead = 0;
  146. /// Target-defined identifier for constructing a frame for this function.
  147. unsigned FrameConstructionID = 0;
  148. /// Return the number of candidates for this \p OutlinedFunction.
  149. unsigned getOccurrenceCount() const { return Candidates.size(); }
  150. /// Return the number of bytes it would take to outline this
  151. /// function.
  152. unsigned getOutliningCost() const {
  153. unsigned CallOverhead = 0;
  154. for (const Candidate &C : Candidates)
  155. CallOverhead += C.getCallOverhead();
  156. return CallOverhead + SequenceSize + FrameOverhead;
  157. }
  158. /// Return the size in bytes of the unoutlined sequences.
  159. unsigned getNotOutlinedCost() const {
  160. return getOccurrenceCount() * SequenceSize;
  161. }
  162. /// Return the number of instructions that would be saved by outlining
  163. /// this function.
  164. unsigned getBenefit() const {
  165. unsigned NotOutlinedCost = getNotOutlinedCost();
  166. unsigned OutlinedCost = getOutliningCost();
  167. return (NotOutlinedCost < OutlinedCost) ? 0
  168. : NotOutlinedCost - OutlinedCost;
  169. }
  170. /// Return the number of instructions in this sequence.
  171. unsigned getNumInstrs() const { return Candidates[0].getLength(); }
  172. OutlinedFunction(std::vector<Candidate> &Candidates, unsigned SequenceSize,
  173. unsigned FrameOverhead, unsigned FrameConstructionID)
  174. : Candidates(Candidates), SequenceSize(SequenceSize),
  175. FrameOverhead(FrameOverhead), FrameConstructionID(FrameConstructionID) {
  176. const unsigned B = getBenefit();
  177. for (Candidate &C : Candidates)
  178. C.Benefit = B;
  179. }
  180. OutlinedFunction() {}
  181. };
  182. } // namespace outliner
  183. } // namespace llvm
  184. #endif