SpillPlacement.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. //===- SpillPlacement.h - Optimal Spill Code Placement ---------*- 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 analysis computes the optimal spill code placement between basic blocks.
  10. //
  11. // The runOnMachineFunction() method only precomputes some profiling information
  12. // about the CFG. The real work is done by prepare(), addConstraints(), and
  13. // finish() which are called by the register allocator.
  14. //
  15. // Given a variable that is live across multiple basic blocks, and given
  16. // constraints on the basic blocks where the variable is live, determine which
  17. // edge bundles should have the variable in a register and which edge bundles
  18. // should have the variable in a stack slot.
  19. //
  20. // The returned bit vector can be used to place optimal spill code at basic
  21. // block entries and exits. Spill code placement inside a basic block is not
  22. // considered.
  23. //
  24. //===----------------------------------------------------------------------===//
  25. #ifndef LLVM_LIB_CODEGEN_SPILLPLACEMENT_H
  26. #define LLVM_LIB_CODEGEN_SPILLPLACEMENT_H
  27. #include "llvm/ADT/ArrayRef.h"
  28. #include "llvm/ADT/SmallVector.h"
  29. #include "llvm/ADT/SparseSet.h"
  30. #include "llvm/CodeGen/MachineFunctionPass.h"
  31. #include "llvm/Support/BlockFrequency.h"
  32. namespace llvm {
  33. class BitVector;
  34. class EdgeBundles;
  35. class MachineBlockFrequencyInfo;
  36. class MachineFunction;
  37. class MachineLoopInfo;
  38. class SpillPlacement : public MachineFunctionPass {
  39. struct Node;
  40. const MachineFunction *MF;
  41. const EdgeBundles *bundles;
  42. const MachineLoopInfo *loops;
  43. const MachineBlockFrequencyInfo *MBFI;
  44. Node *nodes = nullptr;
  45. // Nodes that are active in the current computation. Owned by the prepare()
  46. // caller.
  47. BitVector *ActiveNodes;
  48. // Nodes with active links. Populated by scanActiveBundles.
  49. SmallVector<unsigned, 8> Linked;
  50. // Nodes that went positive during the last call to scanActiveBundles or
  51. // iterate.
  52. SmallVector<unsigned, 8> RecentPositive;
  53. // Block frequencies are computed once. Indexed by block number.
  54. SmallVector<BlockFrequency, 8> BlockFrequencies;
  55. /// Decision threshold. A node gets the output value 0 if the weighted sum of
  56. /// its inputs falls in the open interval (-Threshold;Threshold).
  57. BlockFrequency Threshold;
  58. /// List of nodes that need to be updated in ::iterate.
  59. SparseSet<unsigned> TodoList;
  60. public:
  61. static char ID; // Pass identification, replacement for typeid.
  62. SpillPlacement() : MachineFunctionPass(ID) {}
  63. ~SpillPlacement() override { releaseMemory(); }
  64. /// BorderConstraint - A basic block has separate constraints for entry and
  65. /// exit.
  66. enum BorderConstraint {
  67. DontCare, ///< Block doesn't care / variable not live.
  68. PrefReg, ///< Block entry/exit prefers a register.
  69. PrefSpill, ///< Block entry/exit prefers a stack slot.
  70. PrefBoth, ///< Block entry prefers both register and stack.
  71. MustSpill ///< A register is impossible, variable must be spilled.
  72. };
  73. /// BlockConstraint - Entry and exit constraints for a basic block.
  74. struct BlockConstraint {
  75. unsigned Number; ///< Basic block number (from MBB::getNumber()).
  76. BorderConstraint Entry : 8; ///< Constraint on block entry.
  77. BorderConstraint Exit : 8; ///< Constraint on block exit.
  78. /// True when this block changes the value of the live range. This means
  79. /// the block has a non-PHI def. When this is false, a live-in value on
  80. /// the stack can be live-out on the stack without inserting a spill.
  81. bool ChangesValue;
  82. void print(raw_ostream &OS) const;
  83. void dump() const;
  84. };
  85. /// prepare - Reset state and prepare for a new spill placement computation.
  86. /// @param RegBundles Bit vector to receive the edge bundles where the
  87. /// variable should be kept in a register. Each bit
  88. /// corresponds to an edge bundle, a set bit means the
  89. /// variable should be kept in a register through the
  90. /// bundle. A clear bit means the variable should be
  91. /// spilled. This vector is retained.
  92. void prepare(BitVector &RegBundles);
  93. /// addConstraints - Add constraints and biases. This method may be called
  94. /// more than once to accumulate constraints.
  95. /// @param LiveBlocks Constraints for blocks that have the variable live in or
  96. /// live out.
  97. void addConstraints(ArrayRef<BlockConstraint> LiveBlocks);
  98. /// addPrefSpill - Add PrefSpill constraints to all blocks listed. This is
  99. /// equivalent to calling addConstraint with identical BlockConstraints with
  100. /// Entry = Exit = PrefSpill, and ChangesValue = false.
  101. ///
  102. /// @param Blocks Array of block numbers that prefer to spill in and out.
  103. /// @param Strong When true, double the negative bias for these blocks.
  104. void addPrefSpill(ArrayRef<unsigned> Blocks, bool Strong);
  105. /// addLinks - Add transparent blocks with the given numbers.
  106. void addLinks(ArrayRef<unsigned> Links);
  107. /// scanActiveBundles - Perform an initial scan of all bundles activated by
  108. /// addConstraints and addLinks, updating their state. Add all the bundles
  109. /// that now prefer a register to RecentPositive.
  110. /// Prepare internal data structures for iterate.
  111. /// Return true is there are any positive nodes.
  112. bool scanActiveBundles();
  113. /// iterate - Update the network iteratively until convergence, or new bundles
  114. /// are found.
  115. void iterate();
  116. /// getRecentPositive - Return an array of bundles that became positive during
  117. /// the previous call to scanActiveBundles or iterate.
  118. ArrayRef<unsigned> getRecentPositive() { return RecentPositive; }
  119. /// finish - Compute the optimal spill code placement given the
  120. /// constraints. No MustSpill constraints will be violated, and the smallest
  121. /// possible number of PrefX constraints will be violated, weighted by
  122. /// expected execution frequencies.
  123. /// The selected bundles are returned in the bitvector passed to prepare().
  124. /// @return True if a perfect solution was found, allowing the variable to be
  125. /// in a register through all relevant bundles.
  126. bool finish();
  127. /// getBlockFrequency - Return the estimated block execution frequency per
  128. /// function invocation.
  129. BlockFrequency getBlockFrequency(unsigned Number) const {
  130. return BlockFrequencies[Number];
  131. }
  132. private:
  133. bool runOnMachineFunction(MachineFunction &mf) override;
  134. void getAnalysisUsage(AnalysisUsage &AU) const override;
  135. void releaseMemory() override;
  136. void activate(unsigned n);
  137. void setThreshold(const BlockFrequency &Entry);
  138. bool update(unsigned n);
  139. };
  140. } // end namespace llvm
  141. #endif // LLVM_LIB_CODEGEN_SPILLPLACEMENT_H