SpillPlacement.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. };
  83. /// prepare - Reset state and prepare for a new spill placement computation.
  84. /// @param RegBundles Bit vector to receive the edge bundles where the
  85. /// variable should be kept in a register. Each bit
  86. /// corresponds to an edge bundle, a set bit means the
  87. /// variable should be kept in a register through the
  88. /// bundle. A clear bit means the variable should be
  89. /// spilled. This vector is retained.
  90. void prepare(BitVector &RegBundles);
  91. /// addConstraints - Add constraints and biases. This method may be called
  92. /// more than once to accumulate constraints.
  93. /// @param LiveBlocks Constraints for blocks that have the variable live in or
  94. /// live out.
  95. void addConstraints(ArrayRef<BlockConstraint> LiveBlocks);
  96. /// addPrefSpill - Add PrefSpill constraints to all blocks listed. This is
  97. /// equivalent to calling addConstraint with identical BlockConstraints with
  98. /// Entry = Exit = PrefSpill, and ChangesValue = false.
  99. ///
  100. /// @param Blocks Array of block numbers that prefer to spill in and out.
  101. /// @param Strong When true, double the negative bias for these blocks.
  102. void addPrefSpill(ArrayRef<unsigned> Blocks, bool Strong);
  103. /// addLinks - Add transparent blocks with the given numbers.
  104. void addLinks(ArrayRef<unsigned> Links);
  105. /// scanActiveBundles - Perform an initial scan of all bundles activated by
  106. /// addConstraints and addLinks, updating their state. Add all the bundles
  107. /// that now prefer a register to RecentPositive.
  108. /// Prepare internal data structures for iterate.
  109. /// Return true is there are any positive nodes.
  110. bool scanActiveBundles();
  111. /// iterate - Update the network iteratively until convergence, or new bundles
  112. /// are found.
  113. void iterate();
  114. /// getRecentPositive - Return an array of bundles that became positive during
  115. /// the previous call to scanActiveBundles or iterate.
  116. ArrayRef<unsigned> getRecentPositive() { return RecentPositive; }
  117. /// finish - Compute the optimal spill code placement given the
  118. /// constraints. No MustSpill constraints will be violated, and the smallest
  119. /// possible number of PrefX constraints will be violated, weighted by
  120. /// expected execution frequencies.
  121. /// The selected bundles are returned in the bitvector passed to prepare().
  122. /// @return True if a perfect solution was found, allowing the variable to be
  123. /// in a register through all relevant bundles.
  124. bool finish();
  125. /// getBlockFrequency - Return the estimated block execution frequency per
  126. /// function invocation.
  127. BlockFrequency getBlockFrequency(unsigned Number) const {
  128. return BlockFrequencies[Number];
  129. }
  130. private:
  131. bool runOnMachineFunction(MachineFunction &mf) override;
  132. void getAnalysisUsage(AnalysisUsage &AU) const override;
  133. void releaseMemory() override;
  134. void activate(unsigned n);
  135. void setThreshold(const BlockFrequency &Entry);
  136. bool update(unsigned n);
  137. };
  138. } // end namespace llvm
  139. #endif // LLVM_LIB_CODEGEN_SPILLPLACEMENT_H