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/SmallPtrSet.h"
14 #include "llvm/CodeGen/LivePhysRegs.h"
15 #include "llvm/CodeGen/MachineBasicBlock.h"
16 #include "llvm/Support/BlockFrequency.h"
20 class MachineBlockFrequencyInfo;
21 class MachineBranchProbabilityInfo;
22 class MachineFunction;
23 class MachineModuleInfo;
24 class MachineLoopInfo;
25 class TargetInstrInfo;
26 class TargetRegisterInfo;
28 class LLVM_LIBRARY_VISIBILITY BranchFolder {
32 explicit BranchFolder(bool defaultEnableTailMerge,
35 const MachineBranchProbabilityInfo &MBPI,
36 // Min tail length to merge. Defaults to commandline
37 // flag. Ignored for optsize.
38 unsigned MinCommonTailLength = 0);
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);
49 class MergePotentialsElt {
51 MachineBasicBlock *Block;
53 MergePotentialsElt(unsigned h, MachineBasicBlock *b)
54 : Hash(h), Block(b) {}
56 unsigned getHash() const { return Hash; }
57 MachineBasicBlock *getBlock() const { return Block; }
59 void setBlock(MachineBasicBlock *MBB) {
63 bool operator<(const MergePotentialsElt &) const;
65 typedef std::vector<MergePotentialsElt>::iterator MPIterator;
66 std::vector<MergePotentialsElt> MergePotentials;
67 SmallPtrSet<const MachineBasicBlock*, 2> TriedMerging;
68 DenseMap<const MachineBasicBlock *, int> FuncletMembership;
72 MachineBasicBlock::iterator TailStartPos;
74 SameTailElt(MPIterator mp, MachineBasicBlock::iterator tsp)
75 : MPIter(mp), TailStartPos(tsp) {}
77 MPIterator getMPIter() const {
80 MergePotentialsElt &getMergePotentialsElt() const {
83 MachineBasicBlock::iterator getTailStartPos() const {
86 unsigned getHash() const {
87 return getMergePotentialsElt().getHash();
89 MachineBasicBlock *getBlock() const {
90 return getMergePotentialsElt().getBlock();
92 bool tailIsWholeBlock() const {
93 return TailStartPos == getBlock()->begin();
96 void setBlock(MachineBasicBlock *MBB) {
97 getMergePotentialsElt().setBlock(MBB);
99 void setTailStartPos(MachineBasicBlock::iterator Pos) {
103 std::vector<SameTailElt> SameTails;
105 bool AfterBlockPlacement;
106 bool EnableTailMerge;
107 bool EnableHoistCommonCode;
109 unsigned MinCommonTailLength;
110 const TargetInstrInfo *TII;
111 const TargetRegisterInfo *TRI;
112 MachineModuleInfo *MMI;
113 MachineLoopInfo *MLI;
114 LivePhysRegs LiveRegs;
117 /// \brief This class keeps track of branch frequencies of newly created
118 /// blocks and tail-merged blocks.
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;
132 const MachineBlockFrequencyInfo &MBFI;
133 DenseMap<const MachineBasicBlock *, BlockFrequency> MergedBBFreq;
137 MBFIWrapper &MBBFreqInfo;
138 const MachineBranchProbabilityInfo &MBPI;
140 bool TailMergeBlocks(MachineFunction &MF);
141 bool TryTailMergeBlocks(MachineBasicBlock* SuccBB,
142 MachineBasicBlock* PredBB,
143 unsigned MinCommonTailLength);
144 void setCommonTailEdgeWeights(MachineBasicBlock &TailMBB);
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);
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);
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);
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);
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);
183 /// Create merged DebugLocs of identical instructions across SameTails and
184 /// assign it to the instruction in common tail.
185 void MergeCommonTailDebugLocs(unsigned commonTailIndex);
187 bool OptimizeBranches(MachineFunction &MF);
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);
193 /// Remove the specified dead machine basic block from the function,
194 /// updating the CFG.
195 void RemoveDeadBlock(MachineBasicBlock *MBB);
197 /// Hoist common instruction sequences at the start of basic blocks to their
198 /// common predecessor.
199 bool HoistCommonCode(MachineFunction &MF);
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);
207 #endif /* LLVM_CODEGEN_BRANCHFOLDING_HPP */