]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/include/llvm/CodeGen/TailDuplicator.h
Merge compiler-rt trunk r300890, and update build glue.
[FreeBSD/FreeBSD.git] / contrib / llvm / include / llvm / CodeGen / TailDuplicator.h
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/STLExtras.h"
19 #include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
20 #include "llvm/CodeGen/MachineModuleInfo.h"
21 #include "llvm/CodeGen/MachineRegisterInfo.h"
22 #include "llvm/CodeGen/MachineSSAUpdater.h"
23 #include "llvm/CodeGen/RegisterScavenging.h"
24 #include "llvm/Target/TargetInstrInfo.h"
25 #include "llvm/Target/TargetRegisterInfo.h"
26 #include "llvm/Target/TargetSubtargetInfo.h"
27
28 namespace llvm {
29
30 extern cl::opt<unsigned> TailDupIndirectBranchSize;
31
32 /// Utility class to perform tail duplication.
33 class TailDuplicator {
34   const TargetInstrInfo *TII;
35   const TargetRegisterInfo *TRI;
36   const MachineBranchProbabilityInfo *MBPI;
37   const MachineModuleInfo *MMI;
38   MachineRegisterInfo *MRI;
39   MachineFunction *MF;
40   bool PreRegAlloc;
41   bool LayoutMode;
42   unsigned TailDupSize;
43
44   // A list of virtual registers for which to update SSA form.
45   SmallVector<unsigned, 16> SSAUpdateVRs;
46
47   // For each virtual register in SSAUpdateVals keep a list of source virtual
48   // registers.
49   typedef std::vector<std::pair<MachineBasicBlock *, unsigned>> AvailableValsTy;
50
51   DenseMap<unsigned, AvailableValsTy> SSAUpdateVals;
52
53 public:
54   /// Prepare to run on a specific machine function.
55   /// @param MF - Function that will be processed
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,
63               const MachineBranchProbabilityInfo *MBPI,
64               bool LayoutMode, unsigned TailDupSize = 0);
65   bool tailDuplicateBlocks();
66   static bool isSimpleBB(MachineBasicBlock *TailBB);
67   bool shouldTailDuplicate(bool IsSimple, MachineBasicBlock &TailBB);
68   /// Returns true if TailBB can successfully be duplicated into PredBB
69   bool canTailDuplicate(MachineBasicBlock *TailBB, MachineBasicBlock *PredBB);
70   /// Tail duplicate a single basic block into its predecessors, and then clean
71   /// up.
72   /// If \p DuplicatePreds is not null, it will be updated to contain the list
73   /// of predecessors that received a copy of \p MBB.
74   /// If \p RemovalCallback is non-null. It will be called before MBB is
75   /// deleted.
76   bool tailDuplicateAndUpdate(
77       bool IsSimple, MachineBasicBlock *MBB,
78       MachineBasicBlock *ForcedLayoutPred,
79       SmallVectorImpl<MachineBasicBlock*> *DuplicatedPreds = nullptr,
80       llvm::function_ref<void(MachineBasicBlock *)> *RemovalCallback = nullptr);
81
82 private:
83   typedef TargetInstrInfo::RegSubRegPair RegSubRegPair;
84
85   void addSSAUpdateEntry(unsigned OrigReg, unsigned NewReg,
86                          MachineBasicBlock *BB);
87   void processPHI(MachineInstr *MI, MachineBasicBlock *TailBB,
88                   MachineBasicBlock *PredBB,
89                   DenseMap<unsigned, RegSubRegPair> &LocalVRMap,
90                   SmallVectorImpl<std::pair<unsigned, RegSubRegPair>> &Copies,
91                   const DenseSet<unsigned> &UsedByPhi, bool Remove);
92   void duplicateInstruction(MachineInstr *MI, MachineBasicBlock *TailBB,
93                             MachineBasicBlock *PredBB,
94                             DenseMap<unsigned, RegSubRegPair> &LocalVRMap,
95                             const DenseSet<unsigned> &UsedByPhi);
96   void updateSuccessorsPHIs(MachineBasicBlock *FromBB, bool isDead,
97                             SmallVectorImpl<MachineBasicBlock *> &TDBBs,
98                             SmallSetVector<MachineBasicBlock *, 8> &Succs);
99   bool canCompletelyDuplicateBB(MachineBasicBlock &BB);
100   bool duplicateSimpleBB(MachineBasicBlock *TailBB,
101                          SmallVectorImpl<MachineBasicBlock *> &TDBBs,
102                          const DenseSet<unsigned> &RegsUsedByPhi,
103                          SmallVectorImpl<MachineInstr *> &Copies);
104   bool tailDuplicate(bool IsSimple,
105                      MachineBasicBlock *TailBB,
106                      MachineBasicBlock *ForcedLayoutPred,
107                      SmallVectorImpl<MachineBasicBlock *> &TDBBs,
108                      SmallVectorImpl<MachineInstr *> &Copies);
109   void appendCopies(MachineBasicBlock *MBB,
110                  SmallVectorImpl<std::pair<unsigned,RegSubRegPair>> &CopyInfos,
111                  SmallVectorImpl<MachineInstr *> &Copies);
112
113   void removeDeadBlock(
114       MachineBasicBlock *MBB,
115       llvm::function_ref<void(MachineBasicBlock *)> *RemovalCallback = nullptr);
116 };
117
118 } // End llvm namespace
119
120 #endif