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"
14 #include "llvm/CodeGen/MachineFunctionPass.h"
18 class MachineFunction
;
19 class MachineModuleInfo
;
21 class TargetInstrInfo
;
22 class TargetRegisterInfo
;
26 explicit BranchFolder(bool defaultEnableTailMerge
);
28 bool OptimizeFunction(MachineFunction
&MF
,
29 const TargetInstrInfo
*tii
,
30 const TargetRegisterInfo
*tri
,
31 MachineModuleInfo
*mmi
);
33 typedef std::pair
<unsigned,MachineBasicBlock
*> MergePotentialsElt
;
34 typedef std::vector
<MergePotentialsElt
>::iterator MPIterator
;
35 std::vector
<MergePotentialsElt
> MergePotentials
;
37 typedef std::pair
<MPIterator
, MachineBasicBlock::iterator
> SameTailElt
;
38 std::vector
<SameTailElt
> SameTails
;
41 const TargetInstrInfo
*TII
;
42 const TargetRegisterInfo
*TRI
;
43 MachineModuleInfo
*MMI
;
46 bool TailMergeBlocks(MachineFunction
&MF
);
47 bool TryMergeBlocks(MachineBasicBlock
* SuccBB
,
48 MachineBasicBlock
* PredBB
);
49 void ReplaceTailWithBranchTo(MachineBasicBlock::iterator OldInst
,
50 MachineBasicBlock
*NewDest
);
51 MachineBasicBlock
*SplitMBBAt(MachineBasicBlock
&CurMBB
,
52 MachineBasicBlock::iterator BBI1
);
53 unsigned ComputeSameTails(unsigned CurHash
, unsigned minCommonTailLength
);
54 void RemoveBlocksWithHash(unsigned CurHash
, MachineBasicBlock
* SuccBB
,
55 MachineBasicBlock
* PredBB
);
56 unsigned CreateCommonTailOnlyBlock(MachineBasicBlock
*&PredBB
,
57 unsigned maxCommonTailLength
);
59 bool OptimizeBranches(MachineFunction
&MF
);
60 bool OptimizeBlock(MachineBasicBlock
*MBB
);
61 void RemoveDeadBlock(MachineBasicBlock
*MBB
);
62 bool OptimizeImpDefsBlock(MachineBasicBlock
*MBB
);
64 bool CanFallThrough(MachineBasicBlock
*CurBB
);
65 bool CanFallThrough(MachineBasicBlock
*CurBB
, bool BranchUnAnalyzable
,
66 MachineBasicBlock
*TBB
, MachineBasicBlock
*FBB
,
67 const SmallVectorImpl
<MachineOperand
> &Cond
);
71 /// BranchFolderPass - Wrap branch folder in a machine function pass.
72 class BranchFolderPass
: public MachineFunctionPass
,
76 explicit BranchFolderPass(bool defaultEnableTailMerge
)
77 : MachineFunctionPass(&ID
), BranchFolder(defaultEnableTailMerge
) {}
79 virtual bool runOnMachineFunction(MachineFunction
&MF
);
80 virtual const char *getPassName() const { return "Control Flow Optimizer"; }
84 #endif /* LLVM_CODEGEN_BRANCHFOLDING_HPP */