blob: b087395640608d0e39513e9caac2d151204f768e [file] [log] [blame]
Evan Cheng030a0a02009-09-04 07:47:40 +00001//===-- BranchFolding.h - Fold machine code branch instructions --*- C++ -*===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef LLVM_CODEGEN_BRANCHFOLDING_HPP
11#define LLVM_CODEGEN_BRANCHFOLDING_HPP
12
13#include "llvm/CodeGen/MachineBasicBlock.h"
Evan Cheng030a0a02009-09-04 07:47:40 +000014#include <vector>
15
16namespace llvm {
17 class MachineFunction;
18 class MachineModuleInfo;
19 class RegScavenger;
20 class TargetInstrInfo;
21 class TargetRegisterInfo;
Dan Gohman72b29902009-11-12 01:59:26 +000022 template<typename T> class SmallVectorImpl;
Evan Cheng030a0a02009-09-04 07:47:40 +000023
24 class BranchFolder {
25 public:
Bob Wilsona5971032009-10-28 20:46:46 +000026 explicit BranchFolder(bool defaultEnableTailMerge);
Evan Cheng030a0a02009-09-04 07:47:40 +000027
28 bool OptimizeFunction(MachineFunction &MF,
29 const TargetInstrInfo *tii,
30 const TargetRegisterInfo *tri,
31 MachineModuleInfo *mmi);
32 private:
Dan Gohmanffe644e2009-11-11 21:57:02 +000033 class MergePotentialsElt {
34 unsigned Hash;
35 MachineBasicBlock *Block;
36 public:
37 MergePotentialsElt(unsigned h, MachineBasicBlock *b)
38 : Hash(h), Block(b) {}
39
40 unsigned getHash() const { return Hash; }
41 MachineBasicBlock *getBlock() const { return Block; }
42
43 void setBlock(MachineBasicBlock *MBB) {
44 Block = MBB;
45 }
46
47 bool operator<(const MergePotentialsElt &) const;
48 };
Evan Cheng030a0a02009-09-04 07:47:40 +000049 typedef std::vector<MergePotentialsElt>::iterator MPIterator;
50 std::vector<MergePotentialsElt> MergePotentials;
51
Dan Gohmanffe644e2009-11-11 21:57:02 +000052 class SameTailElt {
53 MPIterator MPIter;
54 MachineBasicBlock::iterator TailStartPos;
55 public:
56 SameTailElt(MPIterator mp, MachineBasicBlock::iterator tsp)
57 : MPIter(mp), TailStartPos(tsp) {}
58
59 MPIterator getMPIter() const {
60 return MPIter;
61 }
62 MergePotentialsElt &getMergePotentialsElt() const {
63 return *getMPIter();
64 }
65 MachineBasicBlock::iterator getTailStartPos() const {
66 return TailStartPos;
67 }
68 unsigned getHash() const {
69 return getMergePotentialsElt().getHash();
70 }
71 MachineBasicBlock *getBlock() const {
72 return getMergePotentialsElt().getBlock();
73 }
74 bool tailIsWholeBlock() const {
75 return TailStartPos == getBlock()->begin();
76 }
77
78 void setBlock(MachineBasicBlock *MBB) {
79 getMergePotentialsElt().setBlock(MBB);
80 }
81 void setTailStartPos(MachineBasicBlock::iterator Pos) {
82 TailStartPos = Pos;
83 }
84 };
Evan Cheng030a0a02009-09-04 07:47:40 +000085 std::vector<SameTailElt> SameTails;
86
87 bool EnableTailMerge;
88 const TargetInstrInfo *TII;
89 const TargetRegisterInfo *TRI;
90 MachineModuleInfo *MMI;
91 RegScavenger *RS;
92
93 bool TailMergeBlocks(MachineFunction &MF);
Dan Gohman412a3b92009-11-11 19:49:34 +000094 bool TryTailMergeBlocks(MachineBasicBlock* SuccBB,
95 MachineBasicBlock* PredBB);
Evan Cheng030a0a02009-09-04 07:47:40 +000096 void ReplaceTailWithBranchTo(MachineBasicBlock::iterator OldInst,
97 MachineBasicBlock *NewDest);
98 MachineBasicBlock *SplitMBBAt(MachineBasicBlock &CurMBB,
99 MachineBasicBlock::iterator BBI1);
Dan Gohman412a3b92009-11-11 19:49:34 +0000100 unsigned ComputeSameTails(unsigned CurHash, unsigned minCommonTailLength,
101 MachineBasicBlock *SuccBB,
102 MachineBasicBlock *PredBB);
Evan Cheng030a0a02009-09-04 07:47:40 +0000103 void RemoveBlocksWithHash(unsigned CurHash, MachineBasicBlock* SuccBB,
104 MachineBasicBlock* PredBB);
105 unsigned CreateCommonTailOnlyBlock(MachineBasicBlock *&PredBB,
106 unsigned maxCommonTailLength);
107
108 bool OptimizeBranches(MachineFunction &MF);
109 bool OptimizeBlock(MachineBasicBlock *MBB);
110 void RemoveDeadBlock(MachineBasicBlock *MBB);
111 bool OptimizeImpDefsBlock(MachineBasicBlock *MBB);
Evan Cheng030a0a02009-09-04 07:47:40 +0000112 };
Evan Cheng030a0a02009-09-04 07:47:40 +0000113}
114
115#endif /* LLVM_CODEGEN_BRANCHFOLDING_HPP */