BranchFolding.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. //===-- BranchFolding.h - Fold machine code branch instructions -*- 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. #ifndef LLVM_LIB_CODEGEN_BRANCHFOLDING_H
  10. #define LLVM_LIB_CODEGEN_BRANCHFOLDING_H
  11. #include "llvm/ADT/SmallPtrSet.h"
  12. #include "llvm/CodeGen/MachineBasicBlock.h"
  13. #include "llvm/Support/BlockFrequency.h"
  14. #include <vector>
  15. namespace llvm {
  16. class MachineBlockFrequencyInfo;
  17. class MachineBranchProbabilityInfo;
  18. class MachineFunction;
  19. class MachineModuleInfo;
  20. class RegScavenger;
  21. class TargetInstrInfo;
  22. class TargetRegisterInfo;
  23. class LLVM_LIBRARY_VISIBILITY BranchFolder {
  24. public:
  25. explicit BranchFolder(bool defaultEnableTailMerge, bool CommonHoist,
  26. const MachineBlockFrequencyInfo &MBFI,
  27. const MachineBranchProbabilityInfo &MBPI);
  28. bool OptimizeFunction(MachineFunction &MF,
  29. const TargetInstrInfo *tii,
  30. const TargetRegisterInfo *tri,
  31. MachineModuleInfo *mmi);
  32. private:
  33. class MergePotentialsElt {
  34. unsigned Hash;
  35. MachineBasicBlock *Block;
  36. public:
  37. MergePotentialsElt(unsigned h, MachineBasicBlock *b)
  38. : Hash(h), Block(b) {}
  39. unsigned getHash() const { return Hash; }
  40. MachineBasicBlock *getBlock() const { return Block; }
  41. void setBlock(MachineBasicBlock *MBB) {
  42. Block = MBB;
  43. }
  44. bool operator<(const MergePotentialsElt &) const;
  45. };
  46. typedef std::vector<MergePotentialsElt>::iterator MPIterator;
  47. std::vector<MergePotentialsElt> MergePotentials;
  48. SmallPtrSet<const MachineBasicBlock*, 2> TriedMerging;
  49. class SameTailElt {
  50. MPIterator MPIter;
  51. MachineBasicBlock::iterator TailStartPos;
  52. public:
  53. SameTailElt(MPIterator mp, MachineBasicBlock::iterator tsp)
  54. : MPIter(mp), TailStartPos(tsp) {}
  55. MPIterator getMPIter() const {
  56. return MPIter;
  57. }
  58. MergePotentialsElt &getMergePotentialsElt() const {
  59. return *getMPIter();
  60. }
  61. MachineBasicBlock::iterator getTailStartPos() const {
  62. return TailStartPos;
  63. }
  64. unsigned getHash() const {
  65. return getMergePotentialsElt().getHash();
  66. }
  67. MachineBasicBlock *getBlock() const {
  68. return getMergePotentialsElt().getBlock();
  69. }
  70. bool tailIsWholeBlock() const {
  71. return TailStartPos == getBlock()->begin();
  72. }
  73. void setBlock(MachineBasicBlock *MBB) {
  74. getMergePotentialsElt().setBlock(MBB);
  75. }
  76. void setTailStartPos(MachineBasicBlock::iterator Pos) {
  77. TailStartPos = Pos;
  78. }
  79. };
  80. std::vector<SameTailElt> SameTails;
  81. bool EnableTailMerge;
  82. bool EnableHoistCommonCode;
  83. const TargetInstrInfo *TII;
  84. const TargetRegisterInfo *TRI;
  85. MachineModuleInfo *MMI;
  86. RegScavenger *RS;
  87. /// \brief This class keeps track of branch frequencies of newly created
  88. /// blocks and tail-merged blocks.
  89. class MBFIWrapper {
  90. public:
  91. MBFIWrapper(const MachineBlockFrequencyInfo &I) : MBFI(I) {}
  92. BlockFrequency getBlockFreq(const MachineBasicBlock *MBB) const;
  93. void setBlockFreq(const MachineBasicBlock *MBB, BlockFrequency F);
  94. private:
  95. const MachineBlockFrequencyInfo &MBFI;
  96. DenseMap<const MachineBasicBlock *, BlockFrequency> MergedBBFreq;
  97. };
  98. MBFIWrapper MBBFreqInfo;
  99. const MachineBranchProbabilityInfo &MBPI;
  100. bool TailMergeBlocks(MachineFunction &MF);
  101. bool TryTailMergeBlocks(MachineBasicBlock* SuccBB,
  102. MachineBasicBlock* PredBB);
  103. void setCommonTailEdgeWeights(MachineBasicBlock &TailMBB);
  104. void MaintainLiveIns(MachineBasicBlock *CurMBB,
  105. MachineBasicBlock *NewMBB);
  106. void ReplaceTailWithBranchTo(MachineBasicBlock::iterator OldInst,
  107. MachineBasicBlock *NewDest);
  108. MachineBasicBlock *SplitMBBAt(MachineBasicBlock &CurMBB,
  109. MachineBasicBlock::iterator BBI1,
  110. const BasicBlock *BB);
  111. unsigned ComputeSameTails(unsigned CurHash, unsigned minCommonTailLength,
  112. MachineBasicBlock *SuccBB,
  113. MachineBasicBlock *PredBB);
  114. void RemoveBlocksWithHash(unsigned CurHash, MachineBasicBlock* SuccBB,
  115. MachineBasicBlock* PredBB);
  116. bool CreateCommonTailOnlyBlock(MachineBasicBlock *&PredBB,
  117. MachineBasicBlock *SuccBB,
  118. unsigned maxCommonTailLength,
  119. unsigned &commonTailIndex);
  120. bool OptimizeBranches(MachineFunction &MF);
  121. bool OptimizeBlock(MachineBasicBlock *MBB);
  122. void RemoveDeadBlock(MachineBasicBlock *MBB);
  123. bool OptimizeImpDefsBlock(MachineBasicBlock *MBB);
  124. bool HoistCommonCode(MachineFunction &MF);
  125. bool HoistCommonCodeInSuccs(MachineBasicBlock *MBB);
  126. };
  127. }
  128. #endif /* LLVM_CODEGEN_BRANCHFOLDING_HPP */