1 //===- BranchFolding.h - Fold machine code branch instructions --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLVM_LIB_CODEGEN_BRANCHFOLDING_H
11 #define LLVM_LIB_CODEGEN_BRANCHFOLDING_H
13 #include "llvm/ADT/DenseMap.h"
14 #include "llvm/ADT/SmallPtrSet.h"
15 #include "llvm/CodeGen/LivePhysRegs.h"
16 #include "llvm/CodeGen/MachineBasicBlock.h"
17 #include "llvm/Support/BlockFrequency.h"
18 #include "llvm/Support/Compiler.h"
25 class MachineBlockFrequencyInfo
;
26 class MachineBranchProbabilityInfo
;
27 class MachineFunction
;
28 class MachineLoopInfo
;
29 class MachineModuleInfo
;
30 class MachineRegisterInfo
;
32 class TargetInstrInfo
;
33 class TargetRegisterInfo
;
35 class LLVM_LIBRARY_VISIBILITY BranchFolder
{
39 explicit BranchFolder(bool defaultEnableTailMerge
,
41 MBFIWrapper
&FreqInfo
,
42 const MachineBranchProbabilityInfo
&ProbInfo
,
43 // Min tail length to merge. Defaults to commandline
44 // flag. Ignored for optsize.
45 unsigned MinTailLength
= 0);
47 /// Perhaps branch folding, tail merging and other CFG optimizations on the
48 /// given function. Block placement changes the layout and may create new
49 /// tail merging opportunities.
50 bool OptimizeFunction(MachineFunction
&MF
, const TargetInstrInfo
*tii
,
51 const TargetRegisterInfo
*tri
, MachineModuleInfo
*mmi
,
52 MachineLoopInfo
*mli
= nullptr,
53 bool AfterPlacement
= false);
56 class MergePotentialsElt
{
58 MachineBasicBlock
*Block
;
61 MergePotentialsElt(unsigned h
, MachineBasicBlock
*b
)
62 : Hash(h
), Block(b
) {}
64 unsigned getHash() const { return Hash
; }
65 MachineBasicBlock
*getBlock() const { return Block
; }
67 void setBlock(MachineBasicBlock
*MBB
) {
71 bool operator<(const MergePotentialsElt
&) const;
74 using MPIterator
= std::vector
<MergePotentialsElt
>::iterator
;
76 std::vector
<MergePotentialsElt
> MergePotentials
;
77 SmallPtrSet
<const MachineBasicBlock
*, 2> TriedMerging
;
78 DenseMap
<const MachineBasicBlock
*, int> EHScopeMembership
;
82 MachineBasicBlock::iterator TailStartPos
;
85 SameTailElt(MPIterator mp
, MachineBasicBlock::iterator tsp
)
86 : MPIter(mp
), TailStartPos(tsp
) {}
88 MPIterator
getMPIter() const {
92 MergePotentialsElt
&getMergePotentialsElt() const {
96 MachineBasicBlock::iterator
getTailStartPos() const {
100 unsigned getHash() const {
101 return getMergePotentialsElt().getHash();
104 MachineBasicBlock
*getBlock() const {
105 return getMergePotentialsElt().getBlock();
108 bool tailIsWholeBlock() const {
109 return TailStartPos
== getBlock()->begin();
112 void setBlock(MachineBasicBlock
*MBB
) {
113 getMergePotentialsElt().setBlock(MBB
);
116 void setTailStartPos(MachineBasicBlock::iterator Pos
) {
120 std::vector
<SameTailElt
> SameTails
;
122 bool AfterBlockPlacement
;
123 bool EnableTailMerge
;
124 bool EnableHoistCommonCode
;
126 unsigned MinCommonTailLength
;
127 const TargetInstrInfo
*TII
;
128 const MachineRegisterInfo
*MRI
;
129 const TargetRegisterInfo
*TRI
;
130 MachineModuleInfo
*MMI
;
131 MachineLoopInfo
*MLI
;
132 LivePhysRegs LiveRegs
;
135 /// This class keeps track of branch frequencies of newly created
136 /// blocks and tail-merged blocks.
139 MBFIWrapper(const MachineBlockFrequencyInfo
&I
) : MBFI(I
) {}
141 BlockFrequency
getBlockFreq(const MachineBasicBlock
*MBB
) const;
142 void setBlockFreq(const MachineBasicBlock
*MBB
, BlockFrequency F
);
143 raw_ostream
&printBlockFreq(raw_ostream
&OS
,
144 const MachineBasicBlock
*MBB
) const;
145 raw_ostream
&printBlockFreq(raw_ostream
&OS
,
146 const BlockFrequency Freq
) const;
147 void view(const Twine
&Name
, bool isSimple
= true);
148 uint64_t getEntryFreq() const;
151 const MachineBlockFrequencyInfo
&MBFI
;
152 DenseMap
<const MachineBasicBlock
*, BlockFrequency
> MergedBBFreq
;
156 MBFIWrapper
&MBBFreqInfo
;
157 const MachineBranchProbabilityInfo
&MBPI
;
159 bool TailMergeBlocks(MachineFunction
&MF
);
160 bool TryTailMergeBlocks(MachineBasicBlock
* SuccBB
,
161 MachineBasicBlock
* PredBB
,
162 unsigned MinCommonTailLength
);
163 void setCommonTailEdgeWeights(MachineBasicBlock
&TailMBB
);
165 /// Delete the instruction OldInst and everything after it, replacing it
166 /// with an unconditional branch to NewDest.
167 void replaceTailWithBranchTo(MachineBasicBlock::iterator OldInst
,
168 MachineBasicBlock
&NewDest
);
170 /// Given a machine basic block and an iterator into it, split the MBB so
171 /// that the part before the iterator falls into the part starting at the
172 /// iterator. This returns the new MBB.
173 MachineBasicBlock
*SplitMBBAt(MachineBasicBlock
&CurMBB
,
174 MachineBasicBlock::iterator BBI1
,
175 const BasicBlock
*BB
);
177 /// Look through all the blocks in MergePotentials that have hash CurHash
178 /// (guaranteed to match the last element). Build the vector SameTails of
179 /// all those that have the (same) largest number of instructions in common
180 /// of any pair of these blocks. SameTails entries contain an iterator into
181 /// MergePotentials (from which the MachineBasicBlock can be found) and a
182 /// MachineBasicBlock::iterator into that MBB indicating the instruction
183 /// where the matching code sequence begins. Order of elements in SameTails
184 /// is the reverse of the order in which those blocks appear in
185 /// MergePotentials (where they are not necessarily consecutive).
186 unsigned ComputeSameTails(unsigned CurHash
, unsigned minCommonTailLength
,
187 MachineBasicBlock
*SuccBB
,
188 MachineBasicBlock
*PredBB
);
190 /// Remove all blocks with hash CurHash from MergePotentials, restoring
191 /// branches at ends of blocks as appropriate.
192 void RemoveBlocksWithHash(unsigned CurHash
, MachineBasicBlock
* SuccBB
,
193 MachineBasicBlock
* PredBB
);
195 /// None of the blocks to be tail-merged consist only of the common tail.
196 /// Create a block that does by splitting one.
197 bool CreateCommonTailOnlyBlock(MachineBasicBlock
*&PredBB
,
198 MachineBasicBlock
*SuccBB
,
199 unsigned maxCommonTailLength
,
200 unsigned &commonTailIndex
);
202 /// Create merged DebugLocs of identical instructions across SameTails and
203 /// assign it to the instruction in common tail; merge MMOs and undef flags.
204 void mergeCommonTails(unsigned commonTailIndex
);
206 bool OptimizeBranches(MachineFunction
&MF
);
208 /// Analyze and optimize control flow related to the specified block. This
209 /// is never called on the entry block.
210 bool OptimizeBlock(MachineBasicBlock
*MBB
);
212 /// Remove the specified dead machine basic block from the function,
213 /// updating the CFG.
214 void RemoveDeadBlock(MachineBasicBlock
*MBB
);
216 /// Hoist common instruction sequences at the start of basic blocks to their
217 /// common predecessor.
218 bool HoistCommonCode(MachineFunction
&MF
);
220 /// If the successors of MBB has common instruction sequence at the start of
221 /// the function, move the instructions before MBB terminator if it's legal.
222 bool HoistCommonCodeInSuccs(MachineBasicBlock
*MBB
);
225 } // end namespace llvm
227 #endif // LLVM_LIB_CODEGEN_BRANCHFOLDING_H