TailDuplicator.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- llvm/CodeGen/TailDuplicator.h ----------------------------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This file defines the TailDuplicator class. Used by the
  15. // TailDuplication pass, and MachineBlockPlacement.
  16. //
  17. //===----------------------------------------------------------------------===//
  18. #ifndef LLVM_CODEGEN_TAILDUPLICATOR_H
  19. #define LLVM_CODEGEN_TAILDUPLICATOR_H
  20. #include "llvm/ADT/DenseMap.h"
  21. #include "llvm/ADT/DenseSet.h"
  22. #include "llvm/ADT/SmallVector.h"
  23. #include "llvm/CodeGen/TargetInstrInfo.h"
  24. #include <utility>
  25. #include <vector>
  26. namespace llvm {
  27. template <typename T, unsigned int N> class SmallSetVector;
  28. template <typename Fn> class function_ref;
  29. class MBFIWrapper;
  30. class MachineBasicBlock;
  31. class MachineBranchProbabilityInfo;
  32. class MachineFunction;
  33. class MachineInstr;
  34. class MachineModuleInfo;
  35. class MachineRegisterInfo;
  36. class ProfileSummaryInfo;
  37. class TargetRegisterInfo;
  38. /// Utility class to perform tail duplication.
  39. class TailDuplicator {
  40. const TargetInstrInfo *TII;
  41. const TargetRegisterInfo *TRI;
  42. const MachineBranchProbabilityInfo *MBPI;
  43. const MachineModuleInfo *MMI;
  44. MachineRegisterInfo *MRI;
  45. MachineFunction *MF;
  46. MBFIWrapper *MBFI;
  47. ProfileSummaryInfo *PSI;
  48. bool PreRegAlloc;
  49. bool LayoutMode;
  50. unsigned TailDupSize;
  51. // A list of virtual registers for which to update SSA form.
  52. SmallVector<Register, 16> SSAUpdateVRs;
  53. // For each virtual register in SSAUpdateVals keep a list of source virtual
  54. // registers.
  55. using AvailableValsTy = std::vector<std::pair<MachineBasicBlock *, Register>>;
  56. DenseMap<Register, AvailableValsTy> SSAUpdateVals;
  57. public:
  58. /// Prepare to run on a specific machine function.
  59. /// @param MF - Function that will be processed
  60. /// @param PreRegAlloc - true if used before register allocation
  61. /// @param MBPI - Branch Probability Info. Used to propagate correct
  62. /// probabilities when modifying the CFG.
  63. /// @param LayoutMode - When true, don't use the existing layout to make
  64. /// decisions.
  65. /// @param TailDupSize - Maxmimum size of blocks to tail-duplicate. Zero
  66. /// default implies using the command line value TailDupSize.
  67. void initMF(MachineFunction &MF, bool PreRegAlloc,
  68. const MachineBranchProbabilityInfo *MBPI,
  69. MBFIWrapper *MBFI,
  70. ProfileSummaryInfo *PSI,
  71. bool LayoutMode, unsigned TailDupSize = 0);
  72. bool tailDuplicateBlocks();
  73. static bool isSimpleBB(MachineBasicBlock *TailBB);
  74. bool shouldTailDuplicate(bool IsSimple, MachineBasicBlock &TailBB);
  75. /// Returns true if TailBB can successfully be duplicated into PredBB
  76. bool canTailDuplicate(MachineBasicBlock *TailBB, MachineBasicBlock *PredBB);
  77. /// Tail duplicate a single basic block into its predecessors, and then clean
  78. /// up.
  79. /// If \p DuplicatePreds is not null, it will be updated to contain the list
  80. /// of predecessors that received a copy of \p MBB.
  81. /// If \p RemovalCallback is non-null. It will be called before MBB is
  82. /// deleted.
  83. /// If \p CandidatePtr is not null, duplicate into these blocks only.
  84. bool tailDuplicateAndUpdate(
  85. bool IsSimple, MachineBasicBlock *MBB,
  86. MachineBasicBlock *ForcedLayoutPred,
  87. SmallVectorImpl<MachineBasicBlock*> *DuplicatedPreds = nullptr,
  88. function_ref<void(MachineBasicBlock *)> *RemovalCallback = nullptr,
  89. SmallVectorImpl<MachineBasicBlock *> *CandidatePtr = nullptr);
  90. private:
  91. using RegSubRegPair = TargetInstrInfo::RegSubRegPair;
  92. void addSSAUpdateEntry(Register OrigReg, Register NewReg,
  93. MachineBasicBlock *BB);
  94. void processPHI(MachineInstr *MI, MachineBasicBlock *TailBB,
  95. MachineBasicBlock *PredBB,
  96. DenseMap<Register, RegSubRegPair> &LocalVRMap,
  97. SmallVectorImpl<std::pair<Register, RegSubRegPair>> &Copies,
  98. const DenseSet<Register> &UsedByPhi, bool Remove);
  99. void duplicateInstruction(MachineInstr *MI, MachineBasicBlock *TailBB,
  100. MachineBasicBlock *PredBB,
  101. DenseMap<Register, RegSubRegPair> &LocalVRMap,
  102. const DenseSet<Register> &UsedByPhi);
  103. void updateSuccessorsPHIs(MachineBasicBlock *FromBB, bool isDead,
  104. SmallVectorImpl<MachineBasicBlock *> &TDBBs,
  105. SmallSetVector<MachineBasicBlock *, 8> &Succs);
  106. bool canCompletelyDuplicateBB(MachineBasicBlock &BB);
  107. bool duplicateSimpleBB(MachineBasicBlock *TailBB,
  108. SmallVectorImpl<MachineBasicBlock *> &TDBBs,
  109. const DenseSet<Register> &RegsUsedByPhi);
  110. bool tailDuplicate(bool IsSimple,
  111. MachineBasicBlock *TailBB,
  112. MachineBasicBlock *ForcedLayoutPred,
  113. SmallVectorImpl<MachineBasicBlock *> &TDBBs,
  114. SmallVectorImpl<MachineInstr *> &Copies,
  115. SmallVectorImpl<MachineBasicBlock *> *CandidatePtr);
  116. void appendCopies(MachineBasicBlock *MBB,
  117. SmallVectorImpl<std::pair<Register, RegSubRegPair>> &CopyInfos,
  118. SmallVectorImpl<MachineInstr *> &Copies);
  119. void removeDeadBlock(
  120. MachineBasicBlock *MBB,
  121. function_ref<void(MachineBasicBlock *)> *RemovalCallback = nullptr);
  122. };
  123. } // end namespace llvm
  124. #endif // LLVM_CODEGEN_TAILDUPLICATOR_H
  125. #ifdef __GNUC__
  126. #pragma GCC diagnostic pop
  127. #endif