BranchFolding.h 7.3 KB

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