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