1 //===- llvm/CodeGen/TailDuplicator.h ----------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the TailDuplicator class. Used by the
11 // TailDuplication pass, and MachineBlockPlacement.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_CODEGEN_TAILDUPLICATOR_H
16 #define LLVM_CODEGEN_TAILDUPLICATOR_H
18 #include "llvm/ADT/DenseMap.h"
19 #include "llvm/ADT/DenseSet.h"
20 #include "llvm/ADT/SetVector.h"
21 #include "llvm/ADT/SmallVector.h"
22 #include "llvm/CodeGen/TargetInstrInfo.h"
28 class MachineBasicBlock;
29 class MachineBranchProbabilityInfo;
30 class MachineFunction;
32 class MachineModuleInfo;
33 class MachineRegisterInfo;
34 class TargetRegisterInfo;
36 /// Utility class to perform tail duplication.
37 class TailDuplicator {
38 const TargetInstrInfo *TII;
39 const TargetRegisterInfo *TRI;
40 const MachineBranchProbabilityInfo *MBPI;
41 const MachineModuleInfo *MMI;
42 MachineRegisterInfo *MRI;
48 // A list of virtual registers for which to update SSA form.
49 SmallVector<unsigned, 16> SSAUpdateVRs;
51 // For each virtual register in SSAUpdateVals keep a list of source virtual
53 using AvailableValsTy = std::vector<std::pair<MachineBasicBlock *, unsigned>>;
55 DenseMap<unsigned, AvailableValsTy> SSAUpdateVals;
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
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 bool LayoutMode, unsigned TailDupSize = 0);
71 bool tailDuplicateBlocks();
72 static bool isSimpleBB(MachineBasicBlock *TailBB);
73 bool shouldTailDuplicate(bool IsSimple, MachineBasicBlock &TailBB);
75 /// Returns true if TailBB can successfully be duplicated into PredBB
76 bool canTailDuplicate(MachineBasicBlock *TailBB, MachineBasicBlock *PredBB);
78 /// Tail duplicate a single basic block into its predecessors, and then clean
80 /// If \p DuplicatePreds is not null, it will be updated to contain the list
81 /// of predecessors that received a copy of \p MBB.
82 /// If \p RemovalCallback is non-null. It will be called before MBB is
84 bool tailDuplicateAndUpdate(
85 bool IsSimple, MachineBasicBlock *MBB,
86 MachineBasicBlock *ForcedLayoutPred,
87 SmallVectorImpl<MachineBasicBlock*> *DuplicatedPreds = nullptr,
88 function_ref<void(MachineBasicBlock *)> *RemovalCallback = nullptr);
91 using RegSubRegPair = TargetInstrInfo::RegSubRegPair;
93 void addSSAUpdateEntry(unsigned OrigReg, unsigned NewReg,
94 MachineBasicBlock *BB);
95 void processPHI(MachineInstr *MI, MachineBasicBlock *TailBB,
96 MachineBasicBlock *PredBB,
97 DenseMap<unsigned, RegSubRegPair> &LocalVRMap,
98 SmallVectorImpl<std::pair<unsigned, RegSubRegPair>> &Copies,
99 const DenseSet<unsigned> &UsedByPhi, bool Remove);
100 void duplicateInstruction(MachineInstr *MI, MachineBasicBlock *TailBB,
101 MachineBasicBlock *PredBB,
102 DenseMap<unsigned, RegSubRegPair> &LocalVRMap,
103 const DenseSet<unsigned> &UsedByPhi);
104 void updateSuccessorsPHIs(MachineBasicBlock *FromBB, bool isDead,
105 SmallVectorImpl<MachineBasicBlock *> &TDBBs,
106 SmallSetVector<MachineBasicBlock *, 8> &Succs);
107 bool canCompletelyDuplicateBB(MachineBasicBlock &BB);
108 bool duplicateSimpleBB(MachineBasicBlock *TailBB,
109 SmallVectorImpl<MachineBasicBlock *> &TDBBs,
110 const DenseSet<unsigned> &RegsUsedByPhi,
111 SmallVectorImpl<MachineInstr *> &Copies);
112 bool tailDuplicate(bool IsSimple,
113 MachineBasicBlock *TailBB,
114 MachineBasicBlock *ForcedLayoutPred,
115 SmallVectorImpl<MachineBasicBlock *> &TDBBs,
116 SmallVectorImpl<MachineInstr *> &Copies);
117 void appendCopies(MachineBasicBlock *MBB,
118 SmallVectorImpl<std::pair<unsigned,RegSubRegPair>> &CopyInfos,
119 SmallVectorImpl<MachineInstr *> &Copies);
121 void removeDeadBlock(
122 MachineBasicBlock *MBB,
123 function_ref<void(MachineBasicBlock *)> *RemovalCallback = nullptr);
126 } // end namespace llvm
128 #endif // LLVM_CODEGEN_TAILDUPLICATOR_H