TailDuplicator.h 5.5 KB

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