BranchFolding.h 7.3 KB

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