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