BranchFolding.h 7.3 KB

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