LLVM  5.0.0
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"
22 #include "llvm/ADT/STLExtras.h"
27 #include <utility>
28 #include <vector>
29 
30 namespace llvm {
31 
32 class MachineBasicBlock;
33 class MachineBranchProbabilityInfo;
34 class MachineFunction;
35 class MachineInstr;
36 class MachineModuleInfo;
37 class MachineRegisterInfo;
38 class TargetRegisterInfo;
39 
40 /// Utility class to perform tail duplication.
42  const TargetInstrInfo *TII;
43  const TargetRegisterInfo *TRI;
44  const MachineBranchProbabilityInfo *MBPI;
45  const MachineModuleInfo *MMI;
47  MachineFunction *MF;
48  bool PreRegAlloc;
49  bool LayoutMode;
50  unsigned TailDupSize;
51 
52  // A list of virtual registers for which to update SSA form.
53  SmallVector<unsigned, 16> SSAUpdateVRs;
54 
55  // For each virtual register in SSAUpdateVals keep a list of source virtual
56  // registers.
57  using AvailableValsTy = std::vector<std::pair<MachineBasicBlock *, unsigned>>;
58 
60 
61 public:
62  /// Prepare to run on a specific machine function.
63  /// @param MF - Function that will be processed
64  /// @param MBPI - Branch Probability Info. Used to propagate correct
65  /// probabilities when modifying the CFG.
66  /// @param LayoutMode - When true, don't use the existing layout to make
67  /// decisions.
68  /// @param TailDupSize - Maxmimum size of blocks to tail-duplicate. Zero
69  /// default implies using the command line value TailDupSize.
70  void initMF(MachineFunction &MF,
71  const MachineBranchProbabilityInfo *MBPI,
72  bool LayoutMode, unsigned TailDupSize = 0);
73 
74  bool tailDuplicateBlocks();
75  static bool isSimpleBB(MachineBasicBlock *TailBB);
76  bool shouldTailDuplicate(bool IsSimple, MachineBasicBlock &TailBB);
77 
78  /// Returns true if TailBB can successfully be duplicated into PredBB
80 
81  /// Tail duplicate a single basic block into its predecessors, and then clean
82  /// up.
83  /// If \p DuplicatePreds is not null, it will be updated to contain the list
84  /// of predecessors that received a copy of \p MBB.
85  /// If \p RemovalCallback is non-null. It will be called before MBB is
86  /// deleted.
88  bool IsSimple, MachineBasicBlock *MBB,
89  MachineBasicBlock *ForcedLayoutPred,
90  SmallVectorImpl<MachineBasicBlock*> *DuplicatedPreds = nullptr,
91  function_ref<void(MachineBasicBlock *)> *RemovalCallback = nullptr);
92 
93 private:
95 
96  void addSSAUpdateEntry(unsigned OrigReg, unsigned NewReg,
97  MachineBasicBlock *BB);
98  void processPHI(MachineInstr *MI, MachineBasicBlock *TailBB,
99  MachineBasicBlock *PredBB,
101  SmallVectorImpl<std::pair<unsigned, RegSubRegPair>> &Copies,
102  const DenseSet<unsigned> &UsedByPhi, bool Remove);
103  void duplicateInstruction(MachineInstr *MI, MachineBasicBlock *TailBB,
104  MachineBasicBlock *PredBB,
106  const DenseSet<unsigned> &UsedByPhi);
107  void updateSuccessorsPHIs(MachineBasicBlock *FromBB, bool isDead,
110  bool canCompletelyDuplicateBB(MachineBasicBlock &BB);
111  bool duplicateSimpleBB(MachineBasicBlock *TailBB,
113  const DenseSet<unsigned> &RegsUsedByPhi,
115  bool tailDuplicate(bool IsSimple,
116  MachineBasicBlock *TailBB,
117  MachineBasicBlock *ForcedLayoutPred,
120  void appendCopies(MachineBasicBlock *MBB,
121  SmallVectorImpl<std::pair<unsigned,RegSubRegPair>> &CopyInfos,
123 
124  void removeDeadBlock(
125  MachineBasicBlock *MBB,
126  function_ref<void(MachineBasicBlock *)> *RemovalCallback = nullptr);
127 };
128 
129 } // end namespace llvm
130 
131 #endif // LLVM_CODEGEN_TAILDUPLICATOR_H
bool shouldTailDuplicate(bool IsSimple, MachineBasicBlock &TailBB)
Determine if it is profitable to duplicate this block.
An efficient, type-erasing, non-owning reference to a callable.
Definition: STLExtras.h:89
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.
TargetInstrInfo - Interface to description of machine instruction set.
static bool isSimpleBB(MachineBasicBlock *TailBB)
True if this BB has only one unconditional jump.
This file declares the machine register scavenger class.
bool canTailDuplicate(MachineBasicBlock *TailBB, MachineBasicBlock *PredBB)
Returns true if TailBB can successfully be duplicated into PredBB.
void initMF(MachineFunction &MF, const MachineBranchProbabilityInfo *MBPI, bool LayoutMode, unsigned TailDupSize=0)
Prepare to run on a specific machine function.
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.
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:59
IRTranslator LLVM IR MI
Utility class to perform tail duplication.
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.
This class contains meta information specific to a module.