SpillPlacement.h 6.5 KB

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