BranchFolding.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. //===- BranchFolding.h - Fold machine code branch instructions --*- 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. #ifndef LLVM_LIB_CODEGEN_BRANCHFOLDING_H
  9. #define LLVM_LIB_CODEGEN_BRANCHFOLDING_H
  10. #include "llvm/ADT/DenseMap.h"
  11. #include "llvm/ADT/SmallPtrSet.h"
  12. #include "llvm/CodeGen/LivePhysRegs.h"
  13. #include "llvm/CodeGen/MachineBasicBlock.h"
  14. #include "llvm/Support/BlockFrequency.h"
  15. #include "llvm/Support/Compiler.h"
  16. #include <cstdint>
  17. #include <vector>
  18. namespace llvm {
  19. class BasicBlock;
  20. class MachineBlockFrequencyInfo;
  21. class MachineBranchProbabilityInfo;
  22. class MachineFunction;
  23. class MachineLoopInfo;
  24. class MachineModuleInfo;
  25. class MachineRegisterInfo;
  26. class raw_ostream;
  27. class TargetInstrInfo;
  28. class TargetRegisterInfo;
  29. class LLVM_LIBRARY_VISIBILITY BranchFolder {
  30. public:
  31. class MBFIWrapper;
  32. explicit BranchFolder(bool defaultEnableTailMerge,
  33. bool CommonHoist,
  34. MBFIWrapper &FreqInfo,
  35. const MachineBranchProbabilityInfo &ProbInfo,
  36. // Min tail length to merge. Defaults to commandline
  37. // flag. Ignored for optsize.
  38. unsigned MinTailLength = 0);
  39. /// Perhaps branch folding, tail merging and other CFG optimizations on the
  40. /// given function. Block placement changes the layout and may create new
  41. /// tail merging opportunities.
  42. bool OptimizeFunction(MachineFunction &MF, const TargetInstrInfo *tii,
  43. const TargetRegisterInfo *tri, MachineModuleInfo *mmi,
  44. MachineLoopInfo *mli = nullptr,
  45. bool AfterPlacement = false);
  46. private:
  47. class MergePotentialsElt {
  48. unsigned Hash;
  49. MachineBasicBlock *Block;
  50. public:
  51. MergePotentialsElt(unsigned h, MachineBasicBlock *b)
  52. : Hash(h), Block(b) {}
  53. unsigned getHash() const { return Hash; }
  54. MachineBasicBlock *getBlock() const { return Block; }
  55. void setBlock(MachineBasicBlock *MBB) {
  56. Block = MBB;
  57. }
  58. bool operator<(const MergePotentialsElt &) const;
  59. };
  60. using MPIterator = std::vector<MergePotentialsElt>::iterator;
  61. std::vector<MergePotentialsElt> MergePotentials;
  62. SmallPtrSet<const MachineBasicBlock*, 2> TriedMerging;
  63. DenseMap<const MachineBasicBlock *, int> EHScopeMembership;
  64. class SameTailElt {
  65. MPIterator MPIter;
  66. MachineBasicBlock::iterator TailStartPos;
  67. public:
  68. SameTailElt(MPIterator mp, MachineBasicBlock::iterator tsp)
  69. : MPIter(mp), TailStartPos(tsp) {}
  70. MPIterator getMPIter() const {
  71. return MPIter;
  72. }
  73. MergePotentialsElt &getMergePotentialsElt() const {
  74. return *getMPIter();
  75. }
  76. MachineBasicBlock::iterator getTailStartPos() const {
  77. return TailStartPos;
  78. }
  79. unsigned getHash() const {
  80. return getMergePotentialsElt().getHash();
  81. }
  82. MachineBasicBlock *getBlock() const {
  83. return getMergePotentialsElt().getBlock();
  84. }
  85. bool tailIsWholeBlock() const {
  86. return TailStartPos == getBlock()->begin();
  87. }
  88. void setBlock(MachineBasicBlock *MBB) {
  89. getMergePotentialsElt().setBlock(MBB);
  90. }
  91. void setTailStartPos(MachineBasicBlock::iterator Pos) {
  92. TailStartPos = Pos;
  93. }
  94. };
  95. std::vector<SameTailElt> SameTails;
  96. bool AfterBlockPlacement;
  97. bool EnableTailMerge;
  98. bool EnableHoistCommonCode;
  99. bool UpdateLiveIns;
  100. unsigned MinCommonTailLength;
  101. const TargetInstrInfo *TII;
  102. const MachineRegisterInfo *MRI;
  103. const TargetRegisterInfo *TRI;
  104. MachineModuleInfo *MMI;
  105. MachineLoopInfo *MLI;
  106. LivePhysRegs LiveRegs;
  107. public:
  108. /// This class keeps track of branch frequencies of newly created
  109. /// blocks and tail-merged blocks.
  110. class MBFIWrapper {
  111. public:
  112. MBFIWrapper(const MachineBlockFrequencyInfo &I) : MBFI(I) {}
  113. BlockFrequency getBlockFreq(const MachineBasicBlock *MBB) const;
  114. void setBlockFreq(const MachineBasicBlock *MBB, BlockFrequency F);
  115. raw_ostream &printBlockFreq(raw_ostream &OS,
  116. const MachineBasicBlock *MBB) const;
  117. raw_ostream &printBlockFreq(raw_ostream &OS,
  118. const BlockFrequency Freq) const;
  119. void view(const Twine &Name, bool isSimple = true);
  120. uint64_t getEntryFreq() const;
  121. private:
  122. const MachineBlockFrequencyInfo &MBFI;
  123. DenseMap<const MachineBasicBlock *, BlockFrequency> MergedBBFreq;
  124. };
  125. private:
  126. MBFIWrapper &MBBFreqInfo;
  127. const MachineBranchProbabilityInfo &MBPI;
  128. bool TailMergeBlocks(MachineFunction &MF);
  129. bool TryTailMergeBlocks(MachineBasicBlock* SuccBB,
  130. MachineBasicBlock* PredBB,
  131. unsigned MinCommonTailLength);
  132. void setCommonTailEdgeWeights(MachineBasicBlock &TailMBB);
  133. /// Delete the instruction OldInst and everything after it, replacing it
  134. /// with an unconditional branch to NewDest.
  135. void replaceTailWithBranchTo(MachineBasicBlock::iterator OldInst,
  136. MachineBasicBlock &NewDest);
  137. /// Given a machine basic block and an iterator into it, split the MBB so
  138. /// that the part before the iterator falls into the part starting at the
  139. /// iterator. This returns the new MBB.
  140. MachineBasicBlock *SplitMBBAt(MachineBasicBlock &CurMBB,
  141. MachineBasicBlock::iterator BBI1,
  142. const BasicBlock *BB);
  143. /// Look through all the blocks in MergePotentials that have hash CurHash
  144. /// (guaranteed to match the last element). Build the vector SameTails of
  145. /// all those that have the (same) largest number of instructions in common
  146. /// of any pair of these blocks. SameTails entries contain an iterator into
  147. /// MergePotentials (from which the MachineBasicBlock can be found) and a
  148. /// MachineBasicBlock::iterator into that MBB indicating the instruction
  149. /// where the matching code sequence begins. Order of elements in SameTails
  150. /// is the reverse of the order in which those blocks appear in
  151. /// MergePotentials (where they are not necessarily consecutive).
  152. unsigned ComputeSameTails(unsigned CurHash, unsigned minCommonTailLength,
  153. MachineBasicBlock *SuccBB,
  154. MachineBasicBlock *PredBB);
  155. /// Remove all blocks with hash CurHash from MergePotentials, restoring
  156. /// branches at ends of blocks as appropriate.
  157. void RemoveBlocksWithHash(unsigned CurHash, MachineBasicBlock* SuccBB,
  158. MachineBasicBlock* PredBB);
  159. /// None of the blocks to be tail-merged consist only of the common tail.
  160. /// Create a block that does by splitting one.
  161. bool CreateCommonTailOnlyBlock(MachineBasicBlock *&PredBB,
  162. MachineBasicBlock *SuccBB,
  163. unsigned maxCommonTailLength,
  164. unsigned &commonTailIndex);
  165. /// Create merged DebugLocs of identical instructions across SameTails and
  166. /// assign it to the instruction in common tail; merge MMOs and undef flags.
  167. void mergeCommonTails(unsigned commonTailIndex);
  168. bool OptimizeBranches(MachineFunction &MF);
  169. /// Analyze and optimize control flow related to the specified block. This
  170. /// is never called on the entry block.
  171. bool OptimizeBlock(MachineBasicBlock *MBB);
  172. /// Remove the specified dead machine basic block from the function,
  173. /// updating the CFG.
  174. void RemoveDeadBlock(MachineBasicBlock *MBB);
  175. /// Hoist common instruction sequences at the start of basic blocks to their
  176. /// common predecessor.
  177. bool HoistCommonCode(MachineFunction &MF);
  178. /// If the successors of MBB has common instruction sequence at the start of
  179. /// the function, move the instructions before MBB terminator if it's legal.
  180. bool HoistCommonCodeInSuccs(MachineBasicBlock *MBB);
  181. };
  182. } // end namespace llvm
  183. #endif // LLVM_LIB_CODEGEN_BRANCHFOLDING_H