1 //===- BranchFolding.h - Fold machine code branch instructions --*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_LIB_CODEGEN_BRANCHFOLDING_H
10 #define LLVM_LIB_CODEGEN_BRANCHFOLDING_H
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"
22 class MachineBranchProbabilityInfo
;
23 class MachineFunction
;
24 class MachineLoopInfo
;
25 class MachineRegisterInfo
;
27 class ProfileSummaryInfo
;
28 class TargetInstrInfo
;
29 class TargetRegisterInfo
;
31 class LLVM_LIBRARY_VISIBILITY BranchFolder
{
33 explicit BranchFolder(bool DefaultEnableTailMerge
, bool CommonHoist
,
34 MBFIWrapper
&FreqInfo
,
35 const MachineBranchProbabilityInfo
&ProbInfo
,
36 ProfileSummaryInfo
*PSI
,
37 // Min tail length to merge. Defaults to commandline
38 // flag. Ignored for optsize.
39 unsigned MinTailLength
= 0);
41 /// Perhaps branch folding, tail merging and other CFG optimizations on the
42 /// given function. Block placement changes the layout and may create new
43 /// tail merging opportunities.
44 bool OptimizeFunction(MachineFunction
&MF
, const TargetInstrInfo
*tii
,
45 const TargetRegisterInfo
*tri
,
46 MachineLoopInfo
*mli
= nullptr,
47 bool AfterPlacement
= false);
50 class MergePotentialsElt
{
52 MachineBasicBlock
*Block
;
53 DebugLoc BranchDebugLoc
;
56 MergePotentialsElt(unsigned h
, MachineBasicBlock
*b
, DebugLoc bdl
)
57 : Hash(h
), Block(b
), BranchDebugLoc(std::move(bdl
)) {}
59 unsigned getHash() const { return Hash
; }
60 MachineBasicBlock
*getBlock() const { return Block
; }
62 void setBlock(MachineBasicBlock
*MBB
) {
66 const DebugLoc
&getBranchDebugLoc() { return BranchDebugLoc
; }
68 bool operator<(const MergePotentialsElt
&) const;
71 using MPIterator
= std::vector
<MergePotentialsElt
>::iterator
;
73 std::vector
<MergePotentialsElt
> MergePotentials
;
74 SmallPtrSet
<const MachineBasicBlock
*, 2> TriedMerging
;
75 DenseMap
<const MachineBasicBlock
*, int> EHScopeMembership
;
79 MachineBasicBlock::iterator TailStartPos
;
82 SameTailElt(MPIterator mp
, MachineBasicBlock::iterator tsp
)
83 : MPIter(mp
), TailStartPos(tsp
) {}
85 MPIterator
getMPIter() const {
89 MergePotentialsElt
&getMergePotentialsElt() const {
93 MachineBasicBlock::iterator
getTailStartPos() const {
97 unsigned getHash() const {
98 return getMergePotentialsElt().getHash();
101 MachineBasicBlock
*getBlock() const {
102 return getMergePotentialsElt().getBlock();
105 bool tailIsWholeBlock() const {
106 return TailStartPos
== getBlock()->begin();
109 void setBlock(MachineBasicBlock
*MBB
) {
110 getMergePotentialsElt().setBlock(MBB
);
113 void setTailStartPos(MachineBasicBlock::iterator Pos
) {
117 std::vector
<SameTailElt
> SameTails
;
119 bool AfterBlockPlacement
= false;
120 bool EnableTailMerge
= false;
121 bool EnableHoistCommonCode
= false;
122 bool UpdateLiveIns
= false;
123 unsigned MinCommonTailLength
;
124 const TargetInstrInfo
*TII
= nullptr;
125 const MachineRegisterInfo
*MRI
= nullptr;
126 const TargetRegisterInfo
*TRI
= nullptr;
127 MachineLoopInfo
*MLI
= nullptr;
128 LivePhysRegs LiveRegs
;
131 MBFIWrapper
&MBBFreqInfo
;
132 const MachineBranchProbabilityInfo
&MBPI
;
133 ProfileSummaryInfo
*PSI
;
135 bool TailMergeBlocks(MachineFunction
&MF
);
136 bool TryTailMergeBlocks(MachineBasicBlock
* SuccBB
,
137 MachineBasicBlock
* PredBB
,
138 unsigned MinCommonTailLength
);
139 void setCommonTailEdgeWeights(MachineBasicBlock
&TailMBB
);
141 /// Delete the instruction OldInst and everything after it, replacing it
142 /// with an unconditional branch to NewDest.
143 void replaceTailWithBranchTo(MachineBasicBlock::iterator OldInst
,
144 MachineBasicBlock
&NewDest
);
146 /// Given a machine basic block and an iterator into it, split the MBB so
147 /// that the part before the iterator falls into the part starting at the
148 /// iterator. This returns the new MBB.
149 MachineBasicBlock
*SplitMBBAt(MachineBasicBlock
&CurMBB
,
150 MachineBasicBlock::iterator BBI1
,
151 const BasicBlock
*BB
);
153 /// Look through all the blocks in MergePotentials that have hash CurHash
154 /// (guaranteed to match the last element). Build the vector SameTails of
155 /// all those that have the (same) largest number of instructions in common
156 /// of any pair of these blocks. SameTails entries contain an iterator into
157 /// MergePotentials (from which the MachineBasicBlock can be found) and a
158 /// MachineBasicBlock::iterator into that MBB indicating the instruction
159 /// where the matching code sequence begins. Order of elements in SameTails
160 /// is the reverse of the order in which those blocks appear in
161 /// MergePotentials (where they are not necessarily consecutive).
162 unsigned ComputeSameTails(unsigned CurHash
, unsigned minCommonTailLength
,
163 MachineBasicBlock
*SuccBB
,
164 MachineBasicBlock
*PredBB
);
166 /// Remove all blocks with hash CurHash from MergePotentials, restoring
167 /// branches at ends of blocks as appropriate.
168 void RemoveBlocksWithHash(unsigned CurHash
, MachineBasicBlock
*SuccBB
,
169 MachineBasicBlock
*PredBB
,
170 const DebugLoc
&BranchDL
);
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
);
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
);
183 bool OptimizeBranches(MachineFunction
&MF
);
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
);
189 /// Remove the specified dead machine basic block from the function,
190 /// updating the CFG.
191 void RemoveDeadBlock(MachineBasicBlock
*MBB
);
193 /// Hoist common instruction sequences at the start of basic blocks to their
194 /// common predecessor.
195 bool HoistCommonCode(MachineFunction
&MF
);
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
);
202 } // end namespace llvm
204 #endif // LLVM_LIB_CODEGEN_BRANCHFOLDING_H