]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/lib/CodeGen/BranchFolding.h
Merge llvm, clang, lld, lldb, compiler-rt and libc++ r303197, and update
[FreeBSD/FreeBSD.git] / contrib / llvm / lib / CodeGen / BranchFolding.h
1 //===-- BranchFolding.h - Fold machine code branch instructions -*- 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 #ifndef LLVM_LIB_CODEGEN_BRANCHFOLDING_H
11 #define LLVM_LIB_CODEGEN_BRANCHFOLDING_H
12
13 #include "llvm/ADT/SmallPtrSet.h"
14 #include "llvm/CodeGen/LivePhysRegs.h"
15 #include "llvm/CodeGen/MachineBasicBlock.h"
16 #include "llvm/Support/BlockFrequency.h"
17 #include <vector>
18
19 namespace llvm {
20   class MachineBlockFrequencyInfo;
21   class MachineBranchProbabilityInfo;
22   class MachineFunction;
23   class MachineModuleInfo;
24   class MachineLoopInfo;
25   class TargetInstrInfo;
26   class TargetRegisterInfo;
27
28   class LLVM_LIBRARY_VISIBILITY BranchFolder {
29   public:
30     class MBFIWrapper;
31
32     explicit BranchFolder(bool defaultEnableTailMerge,
33                           bool CommonHoist,
34                           MBFIWrapper &MBFI,
35                           const MachineBranchProbabilityInfo &MBPI,
36                           // Min tail length to merge. Defaults to commandline
37                           // flag. Ignored for optsize.
38                           unsigned MinCommonTailLength = 0);
39
40     /// Perhaps branch folding, tail merging and other CFG optimizations on the
41     /// given function.  Block placement changes the layout and may create new
42     /// tail merging opportunities.
43     bool OptimizeFunction(MachineFunction &MF, const TargetInstrInfo *tii,
44                           const TargetRegisterInfo *tri, MachineModuleInfo *mmi,
45                           MachineLoopInfo *mli = nullptr,
46                           bool AfterPlacement = false);
47
48   private:
49     class MergePotentialsElt {
50       unsigned Hash;
51       MachineBasicBlock *Block;
52     public:
53       MergePotentialsElt(unsigned h, MachineBasicBlock *b)
54         : Hash(h), Block(b) {}
55
56       unsigned getHash() const { return Hash; }
57       MachineBasicBlock *getBlock() const { return Block; }
58
59       void setBlock(MachineBasicBlock *MBB) {
60         Block = MBB;
61       }
62
63       bool operator<(const MergePotentialsElt &) const;
64     };
65     typedef std::vector<MergePotentialsElt>::iterator MPIterator;
66     std::vector<MergePotentialsElt> MergePotentials;
67     SmallPtrSet<const MachineBasicBlock*, 2> TriedMerging;
68     DenseMap<const MachineBasicBlock *, int> FuncletMembership;
69
70     class SameTailElt {
71       MPIterator MPIter;
72       MachineBasicBlock::iterator TailStartPos;
73     public:
74       SameTailElt(MPIterator mp, MachineBasicBlock::iterator tsp)
75         : MPIter(mp), TailStartPos(tsp) {}
76
77       MPIterator getMPIter() const {
78         return MPIter;
79       }
80       MergePotentialsElt &getMergePotentialsElt() const {
81         return *getMPIter();
82       }
83       MachineBasicBlock::iterator getTailStartPos() const {
84         return TailStartPos;
85       }
86       unsigned getHash() const {
87         return getMergePotentialsElt().getHash();
88       }
89       MachineBasicBlock *getBlock() const {
90         return getMergePotentialsElt().getBlock();
91       }
92       bool tailIsWholeBlock() const {
93         return TailStartPos == getBlock()->begin();
94       }
95
96       void setBlock(MachineBasicBlock *MBB) {
97         getMergePotentialsElt().setBlock(MBB);
98       }
99       void setTailStartPos(MachineBasicBlock::iterator Pos) {
100         TailStartPos = Pos;
101       }
102     };
103     std::vector<SameTailElt> SameTails;
104
105     bool AfterBlockPlacement;
106     bool EnableTailMerge;
107     bool EnableHoistCommonCode;
108     bool UpdateLiveIns;
109     unsigned MinCommonTailLength;
110     const TargetInstrInfo *TII;
111     const TargetRegisterInfo *TRI;
112     MachineModuleInfo *MMI;
113     MachineLoopInfo *MLI;
114     LivePhysRegs LiveRegs;
115
116   public:
117     /// \brief This class keeps track of branch frequencies of newly created
118     /// blocks and tail-merged blocks.
119     class MBFIWrapper {
120     public:
121       MBFIWrapper(const MachineBlockFrequencyInfo &I) : MBFI(I) {}
122       BlockFrequency getBlockFreq(const MachineBasicBlock *MBB) const;
123       void setBlockFreq(const MachineBasicBlock *MBB, BlockFrequency F);
124       raw_ostream &printBlockFreq(raw_ostream &OS,
125                                   const MachineBasicBlock *MBB) const;
126       raw_ostream &printBlockFreq(raw_ostream &OS,
127                                   const BlockFrequency Freq) const;
128       void view(const Twine &Name, bool isSimple = true);
129       uint64_t getEntryFreq() const;
130
131     private:
132       const MachineBlockFrequencyInfo &MBFI;
133       DenseMap<const MachineBasicBlock *, BlockFrequency> MergedBBFreq;
134     };
135
136   private:
137     MBFIWrapper &MBBFreqInfo;
138     const MachineBranchProbabilityInfo &MBPI;
139
140     bool TailMergeBlocks(MachineFunction &MF);
141     bool TryTailMergeBlocks(MachineBasicBlock* SuccBB,
142                        MachineBasicBlock* PredBB,
143                        unsigned MinCommonTailLength);
144     void setCommonTailEdgeWeights(MachineBasicBlock &TailMBB);
145
146     /// Delete the instruction OldInst and everything after it, replacing it
147     /// with an unconditional branch to NewDest.
148     void ReplaceTailWithBranchTo(MachineBasicBlock::iterator OldInst,
149                                  MachineBasicBlock *NewDest);
150
151     /// Given a machine basic block and an iterator into it, split the MBB so
152     /// that the part before the iterator falls into the part starting at the
153     /// iterator.  This returns the new MBB.
154     MachineBasicBlock *SplitMBBAt(MachineBasicBlock &CurMBB,
155                                   MachineBasicBlock::iterator BBI1,
156                                   const BasicBlock *BB);
157
158     /// Look through all the blocks in MergePotentials that have hash CurHash
159     /// (guaranteed to match the last element).  Build the vector SameTails of
160     /// all those that have the (same) largest number of instructions in common
161     /// of any pair of these blocks.  SameTails entries contain an iterator into
162     /// MergePotentials (from which the MachineBasicBlock can be found) and a
163     /// MachineBasicBlock::iterator into that MBB indicating the instruction
164     /// where the matching code sequence begins.  Order of elements in SameTails
165     /// is the reverse of the order in which those blocks appear in
166     /// MergePotentials (where they are not necessarily consecutive).
167     unsigned ComputeSameTails(unsigned CurHash, unsigned minCommonTailLength,
168                               MachineBasicBlock *SuccBB,
169                               MachineBasicBlock *PredBB);
170
171     /// Remove all blocks with hash CurHash from MergePotentials, restoring
172     /// branches at ends of blocks as appropriate.
173     void RemoveBlocksWithHash(unsigned CurHash, MachineBasicBlock* SuccBB,
174                                                 MachineBasicBlock* PredBB);
175
176     /// None of the blocks to be tail-merged consist only of the common tail.
177     /// Create a block that does by splitting one.
178     bool CreateCommonTailOnlyBlock(MachineBasicBlock *&PredBB,
179                                    MachineBasicBlock *SuccBB,
180                                    unsigned maxCommonTailLength,
181                                    unsigned &commonTailIndex);
182
183     /// Create merged DebugLocs of identical instructions across SameTails and
184     /// assign it to the instruction in common tail.
185     void MergeCommonTailDebugLocs(unsigned commonTailIndex);
186
187     bool OptimizeBranches(MachineFunction &MF);
188
189     /// Analyze and optimize control flow related to the specified block. This
190     /// is never called on the entry block.
191     bool OptimizeBlock(MachineBasicBlock *MBB);
192
193     /// Remove the specified dead machine basic block from the function,
194     /// updating the CFG.
195     void RemoveDeadBlock(MachineBasicBlock *MBB);
196
197     /// Hoist common instruction sequences at the start of basic blocks to their
198     /// common predecessor.
199     bool HoistCommonCode(MachineFunction &MF);
200
201     /// If the successors of MBB has common instruction sequence at the start of
202     /// the function, move the instructions before MBB terminator if it's legal.
203     bool HoistCommonCodeInSuccs(MachineBasicBlock *MBB);
204   };
205 }
206
207 #endif /* LLVM_CODEGEN_BRANCHFOLDING_HPP */