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/ADT/SmallPtrSet.h"
14 #include "llvm/CodeGen/MachineBasicBlock.h"
18 class MachineFunction;
19 class MachineModuleInfo;
21 class TargetInstrInfo;
22 class TargetRegisterInfo;
26 explicit BranchFolder(bool defaultEnableTailMerge, bool CommonHoist);
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;
51 SmallPtrSet<const MachineBasicBlock*, 2> TriedMerging;
55 MachineBasicBlock::iterator TailStartPos;
57 SameTailElt(MPIterator mp, MachineBasicBlock::iterator tsp)
58 : MPIter(mp), TailStartPos(tsp) {}
60 MPIterator getMPIter() const {
63 MergePotentialsElt &getMergePotentialsElt() const {
66 MachineBasicBlock::iterator getTailStartPos() const {
69 unsigned getHash() const {
70 return getMergePotentialsElt().getHash();
72 MachineBasicBlock *getBlock() const {
73 return getMergePotentialsElt().getBlock();
75 bool tailIsWholeBlock() const {
76 return TailStartPos == getBlock()->begin();
79 void setBlock(MachineBasicBlock *MBB) {
80 getMergePotentialsElt().setBlock(MBB);
82 void setTailStartPos(MachineBasicBlock::iterator Pos) {
86 std::vector<SameTailElt> SameTails;
89 bool EnableHoistCommonCode;
90 const TargetInstrInfo *TII;
91 const TargetRegisterInfo *TRI;
92 MachineModuleInfo *MMI;
95 bool TailMergeBlocks(MachineFunction &MF);
96 bool TryTailMergeBlocks(MachineBasicBlock* SuccBB,
97 MachineBasicBlock* PredBB);
98 void MaintainLiveIns(MachineBasicBlock *CurMBB,
99 MachineBasicBlock *NewMBB);
100 void ReplaceTailWithBranchTo(MachineBasicBlock::iterator OldInst,
101 MachineBasicBlock *NewDest);
102 MachineBasicBlock *SplitMBBAt(MachineBasicBlock &CurMBB,
103 MachineBasicBlock::iterator BBI1,
104 const BasicBlock *BB);
105 unsigned ComputeSameTails(unsigned CurHash, unsigned minCommonTailLength,
106 MachineBasicBlock *SuccBB,
107 MachineBasicBlock *PredBB);
108 void RemoveBlocksWithHash(unsigned CurHash, MachineBasicBlock* SuccBB,
109 MachineBasicBlock* PredBB);
110 bool CreateCommonTailOnlyBlock(MachineBasicBlock *&PredBB,
111 MachineBasicBlock *SuccBB,
112 unsigned maxCommonTailLength,
113 unsigned &commonTailIndex);
115 bool OptimizeBranches(MachineFunction &MF);
116 bool OptimizeBlock(MachineBasicBlock *MBB);
117 void RemoveDeadBlock(MachineBasicBlock *MBB);
118 bool OptimizeImpDefsBlock(MachineBasicBlock *MBB);
120 bool HoistCommonCode(MachineFunction &MF);
121 bool HoistCommonCodeInSuccs(MachineBasicBlock *MBB);
125 #endif /* LLVM_CODEGEN_BRANCHFOLDING_HPP */