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 bool OptimizeFunction(MachineFunction &MF, const TargetInstrInfo *tii,
41 const TargetRegisterInfo *tri, MachineModuleInfo *mmi,
42 MachineLoopInfo *mli = nullptr,
43 bool AfterPlacement = false);
46 class MergePotentialsElt {
48 MachineBasicBlock *Block;
50 MergePotentialsElt(unsigned h, MachineBasicBlock *b)
51 : Hash(h), Block(b) {}
53 unsigned getHash() const { return Hash; }
54 MachineBasicBlock *getBlock() const { return Block; }
56 void setBlock(MachineBasicBlock *MBB) {
60 bool operator<(const MergePotentialsElt &) const;
62 typedef std::vector<MergePotentialsElt>::iterator MPIterator;
63 std::vector<MergePotentialsElt> MergePotentials;
64 SmallPtrSet<const MachineBasicBlock*, 2> TriedMerging;
65 DenseMap<const MachineBasicBlock *, int> FuncletMembership;
69 MachineBasicBlock::iterator TailStartPos;
71 SameTailElt(MPIterator mp, MachineBasicBlock::iterator tsp)
72 : MPIter(mp), TailStartPos(tsp) {}
74 MPIterator getMPIter() const {
77 MergePotentialsElt &getMergePotentialsElt() const {
80 MachineBasicBlock::iterator getTailStartPos() const {
83 unsigned getHash() const {
84 return getMergePotentialsElt().getHash();
86 MachineBasicBlock *getBlock() const {
87 return getMergePotentialsElt().getBlock();
89 bool tailIsWholeBlock() const {
90 return TailStartPos == getBlock()->begin();
93 void setBlock(MachineBasicBlock *MBB) {
94 getMergePotentialsElt().setBlock(MBB);
96 void setTailStartPos(MachineBasicBlock::iterator Pos) {
100 std::vector<SameTailElt> SameTails;
102 bool AfterBlockPlacement;
103 bool EnableTailMerge;
104 bool EnableHoistCommonCode;
106 unsigned MinCommonTailLength;
107 const TargetInstrInfo *TII;
108 const TargetRegisterInfo *TRI;
109 MachineModuleInfo *MMI;
110 MachineLoopInfo *MLI;
111 LivePhysRegs LiveRegs;
114 /// \brief This class keeps track of branch frequencies of newly created
115 /// blocks and tail-merged blocks.
118 MBFIWrapper(const MachineBlockFrequencyInfo &I) : MBFI(I) {}
119 BlockFrequency getBlockFreq(const MachineBasicBlock *MBB) const;
120 void setBlockFreq(const MachineBasicBlock *MBB, BlockFrequency F);
121 raw_ostream &printBlockFreq(raw_ostream &OS,
122 const MachineBasicBlock *MBB) const;
123 raw_ostream &printBlockFreq(raw_ostream &OS,
124 const BlockFrequency Freq) const;
127 const MachineBlockFrequencyInfo &MBFI;
128 DenseMap<const MachineBasicBlock *, BlockFrequency> MergedBBFreq;
132 MBFIWrapper &MBBFreqInfo;
133 const MachineBranchProbabilityInfo &MBPI;
135 bool TailMergeBlocks(MachineFunction &MF);
136 bool TryTailMergeBlocks(MachineBasicBlock* SuccBB,
137 MachineBasicBlock* PredBB,
138 unsigned MinCommonTailLength);
139 void setCommonTailEdgeWeights(MachineBasicBlock &TailMBB);
140 void ReplaceTailWithBranchTo(MachineBasicBlock::iterator OldInst,
141 MachineBasicBlock *NewDest);
142 MachineBasicBlock *SplitMBBAt(MachineBasicBlock &CurMBB,
143 MachineBasicBlock::iterator BBI1,
144 const BasicBlock *BB);
145 unsigned ComputeSameTails(unsigned CurHash, unsigned minCommonTailLength,
146 MachineBasicBlock *SuccBB,
147 MachineBasicBlock *PredBB);
148 void RemoveBlocksWithHash(unsigned CurHash, MachineBasicBlock* SuccBB,
149 MachineBasicBlock* PredBB);
150 bool CreateCommonTailOnlyBlock(MachineBasicBlock *&PredBB,
151 MachineBasicBlock *SuccBB,
152 unsigned maxCommonTailLength,
153 unsigned &commonTailIndex);
155 bool OptimizeBranches(MachineFunction &MF);
156 bool OptimizeBlock(MachineBasicBlock *MBB);
157 void RemoveDeadBlock(MachineBasicBlock *MBB);
159 bool HoistCommonCode(MachineFunction &MF);
160 bool HoistCommonCodeInSuccs(MachineBasicBlock *MBB);
164 #endif /* LLVM_CODEGEN_BRANCHFOLDING_HPP */