LLVM  8.0.1
TailDuplicator.h
Go to the documentation of this file.
1 //===- llvm/CodeGen/TailDuplicator.h ----------------------------*- 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 //
10 // This file defines the TailDuplicator class. Used by the
11 // TailDuplication pass, and MachineBlockPlacement.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_CODEGEN_TAILDUPLICATOR_H
16 #define LLVM_CODEGEN_TAILDUPLICATOR_H
17 
18 #include "llvm/ADT/DenseMap.h"
19 #include "llvm/ADT/DenseSet.h"
20 #include "llvm/ADT/SetVector.h"
21 #include "llvm/ADT/SmallVector.h"
23 #include <utility>
24 #include <vector>
25 
26 namespace llvm {
27 
28 class MachineBasicBlock;
29 class MachineBranchProbabilityInfo;
30 class MachineFunction;
31 class MachineInstr;
32 class MachineModuleInfo;
33 class MachineRegisterInfo;
34 class TargetRegisterInfo;
35 
36 /// Utility class to perform tail duplication.
38  const TargetInstrInfo *TII;
39  const TargetRegisterInfo *TRI;
40  const MachineBranchProbabilityInfo *MBPI;
41  const MachineModuleInfo *MMI;
43  MachineFunction *MF;
44  bool PreRegAlloc;
45  bool LayoutMode;
46  unsigned TailDupSize;
47 
48  // A list of virtual registers for which to update SSA form.
49  SmallVector<unsigned, 16> SSAUpdateVRs;
50 
51  // For each virtual register in SSAUpdateVals keep a list of source virtual
52  // registers.
53  using AvailableValsTy = std::vector<std::pair<MachineBasicBlock *, unsigned>>;
54 
56 
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  bool LayoutMode, unsigned TailDupSize = 0);
70 
71  bool tailDuplicateBlocks();
72  static bool isSimpleBB(MachineBasicBlock *TailBB);
73  bool shouldTailDuplicate(bool IsSimple, MachineBasicBlock &TailBB);
74 
75  /// Returns true if TailBB can successfully be duplicated into PredBB
77 
78  /// Tail duplicate a single basic block into its predecessors, and then clean
79  /// up.
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
83  /// deleted.
85  bool IsSimple, MachineBasicBlock *MBB,
86  MachineBasicBlock *ForcedLayoutPred,
87  SmallVectorImpl<MachineBasicBlock*> *DuplicatedPreds = nullptr,
88  function_ref<void(MachineBasicBlock *)> *RemovalCallback = nullptr);
89 
90 private:
92 
93  void addSSAUpdateEntry(unsigned OrigReg, unsigned NewReg,
94  MachineBasicBlock *BB);
95  void processPHI(MachineInstr *MI, MachineBasicBlock *TailBB,
96  MachineBasicBlock *PredBB,
98  SmallVectorImpl<std::pair<unsigned, RegSubRegPair>> &Copies,
99  const DenseSet<unsigned> &UsedByPhi, bool Remove);
100  void duplicateInstruction(MachineInstr *MI, MachineBasicBlock *TailBB,
101  MachineBasicBlock *PredBB,
103  const DenseSet<unsigned> &UsedByPhi);
104  void updateSuccessorsPHIs(MachineBasicBlock *FromBB, bool isDead,
107  bool canCompletelyDuplicateBB(MachineBasicBlock &BB);
108  bool duplicateSimpleBB(MachineBasicBlock *TailBB,
110  const DenseSet<unsigned> &RegsUsedByPhi,
112  bool tailDuplicate(bool IsSimple,
113  MachineBasicBlock *TailBB,
114  MachineBasicBlock *ForcedLayoutPred,
117  void appendCopies(MachineBasicBlock *MBB,
118  SmallVectorImpl<std::pair<unsigned,RegSubRegPair>> &CopyInfos,
120 
121  void removeDeadBlock(
122  MachineBasicBlock *MBB,
123  function_ref<void(MachineBasicBlock *)> *RemovalCallback = nullptr);
124 };
125 
126 } // end namespace llvm
127 
128 #endif // LLVM_CODEGEN_TAILDUPLICATOR_H
bool shouldTailDuplicate(bool IsSimple, MachineBasicBlock &TailBB)
Determine if it is profitable to duplicate this block.
This class represents lattice values for constants.
Definition: AllocatorList.h:24
An efficient, type-erasing, non-owning reference to a callable.
Definition: STLExtras.h:117
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: APFloat.h:42
bool tailDuplicateBlocks()
Look for small blocks that are unconditionally branched to and do not fall through.
void initMF(MachineFunction &MF, bool PreRegAlloc, const MachineBranchProbabilityInfo *MBPI, bool LayoutMode, unsigned TailDupSize=0)
Prepare to run on a specific machine function.
TargetInstrInfo - Interface to description of machine instruction set.
static bool isSimpleBB(MachineBasicBlock *TailBB)
True if this BB has only one unconditional jump.
TargetInstrInfo::RegSubRegPair RegSubRegPair
bool tailDuplicateAndUpdate(bool IsSimple, MachineBasicBlock *MBB, MachineBasicBlock *ForcedLayoutPred, SmallVectorImpl< MachineBasicBlock *> *DuplicatedPreds=nullptr, function_ref< void(MachineBasicBlock *)> *RemovalCallback=nullptr)
Tail duplicate a single basic block into its predecessors, and then clean up.
bool canTailDuplicate(MachineBasicBlock *TailBB, MachineBasicBlock *PredBB)
Returns true if TailBB can successfully be duplicated into PredBB.
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
A SetVector that performs no allocations if smaller than a certain size.
Definition: SetVector.h:298
A pair composed of a register and a sub-register index.
SI Lower i1 Copies
MachineRegisterInfo - Keep track of information for virtual and physical registers, including vreg register classes, use/def chains for registers, etc.
Representation of each machine instruction.
Definition: MachineInstr.h:64
IRTranslator LLVM IR MI
Utility class to perform tail duplication.
This class contains meta information specific to a module.