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_CODEGEN_BRANCHFOLDING_HPP
11 #define LLVM_CODEGEN_BRANCHFOLDING_HPP
13 #include "llvm/CodeGen/MachineBasicBlock.h"
17 class MachineFunction
;
18 class MachineModuleInfo
;
20 class TargetInstrInfo
;
21 class TargetRegisterInfo
;
22 template<typename T
> class SmallVectorImpl
;
26 explicit BranchFolder(bool defaultEnableTailMerge
);
28 bool OptimizeFunction(MachineFunction
&MF
,
29 const TargetInstrInfo
*tii
,
30 const TargetRegisterInfo
*tri
,
31 MachineModuleInfo
*mmi
);
33 class MergePotentialsElt
{
35 MachineBasicBlock
*Block
;
37 MergePotentialsElt(unsigned h
, MachineBasicBlock
*b
)
38 : Hash(h
), Block(b
) {}
40 unsigned getHash() const { return Hash
; }
41 MachineBasicBlock
*getBlock() const { return Block
; }
43 void setBlock(MachineBasicBlock
*MBB
) {
47 bool operator<(const MergePotentialsElt
&) const;
49 typedef std::vector
<MergePotentialsElt
>::iterator MPIterator
;
50 std::vector
<MergePotentialsElt
> MergePotentials
;
54 MachineBasicBlock::iterator TailStartPos
;
56 SameTailElt(MPIterator mp
, MachineBasicBlock::iterator tsp
)
57 : MPIter(mp
), TailStartPos(tsp
) {}
59 MPIterator
getMPIter() const {
62 MergePotentialsElt
&getMergePotentialsElt() const {
65 MachineBasicBlock::iterator
getTailStartPos() const {
68 unsigned getHash() const {
69 return getMergePotentialsElt().getHash();
71 MachineBasicBlock
*getBlock() const {
72 return getMergePotentialsElt().getBlock();
74 bool tailIsWholeBlock() const {
75 return TailStartPos
== getBlock()->begin();
78 void setBlock(MachineBasicBlock
*MBB
) {
79 getMergePotentialsElt().setBlock(MBB
);
81 void setTailStartPos(MachineBasicBlock::iterator Pos
) {
85 std::vector
<SameTailElt
> SameTails
;
88 const TargetInstrInfo
*TII
;
89 const TargetRegisterInfo
*TRI
;
90 MachineModuleInfo
*MMI
;
93 bool TailMergeBlocks(MachineFunction
&MF
);
94 bool TryTailMergeBlocks(MachineBasicBlock
* SuccBB
,
95 MachineBasicBlock
* PredBB
);
96 void ReplaceTailWithBranchTo(MachineBasicBlock::iterator OldInst
,
97 MachineBasicBlock
*NewDest
);
98 MachineBasicBlock
*SplitMBBAt(MachineBasicBlock
&CurMBB
,
99 MachineBasicBlock::iterator BBI1
);
100 unsigned ComputeSameTails(unsigned CurHash
, unsigned minCommonTailLength
,
101 MachineBasicBlock
*SuccBB
,
102 MachineBasicBlock
*PredBB
);
103 void RemoveBlocksWithHash(unsigned CurHash
, MachineBasicBlock
* SuccBB
,
104 MachineBasicBlock
* PredBB
);
105 bool CreateCommonTailOnlyBlock(MachineBasicBlock
*&PredBB
,
106 unsigned maxCommonTailLength
,
107 unsigned &commonTailIndex
);
109 bool OptimizeBranches(MachineFunction
&MF
);
110 bool OptimizeBlock(MachineBasicBlock
*MBB
);
111 void RemoveDeadBlock(MachineBasicBlock
*MBB
);
112 bool OptimizeImpDefsBlock(MachineBasicBlock
*MBB
);
116 #endif /* LLVM_CODEGEN_BRANCHFOLDING_HPP */