blob: 4ed42c03464a8d8d0e252b52ecd9ceca3e0f95ec [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
Rafael Espindolaf924dea2011-06-14 15:31:54 +000013#include "llvm/ADT/SmallPtrSet.h"
Evan Cheng030a0a02009-09-04 07:47:40 +000014#include "llvm/CodeGen/MachineBasicBlock.h"
Evan Cheng030a0a02009-09-04 07:47:40 +000015#include <vector>
16
17namespace llvm {
18 class MachineFunction;
19 class MachineModuleInfo;
20 class RegScavenger;
21 class TargetInstrInfo;
22 class TargetRegisterInfo;
23
24 class BranchFolder {
25 public:
Evan Chengcbc988b2011-05-12 00:56:58 +000026 explicit BranchFolder(bool defaultEnableTailMerge, bool CommonHoist);
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;
Rafael Espindolaf924dea2011-06-14 15:31:54 +000051 SmallPtrSet<const MachineBasicBlock*, 2> TriedMerging;
Evan Cheng030a0a02009-09-04 07:47:40 +000052
Dan Gohmanffe644e2009-11-11 21:57:02 +000053 class SameTailElt {
54 MPIterator MPIter;
55 MachineBasicBlock::iterator TailStartPos;
56 public:
57 SameTailElt(MPIterator mp, MachineBasicBlock::iterator tsp)
58 : MPIter(mp), TailStartPos(tsp) {}
59
60 MPIterator getMPIter() const {
61 return MPIter;
62 }
63 MergePotentialsElt &getMergePotentialsElt() const {
64 return *getMPIter();
65 }
66 MachineBasicBlock::iterator getTailStartPos() const {
67 return TailStartPos;
68 }
69 unsigned getHash() const {
70 return getMergePotentialsElt().getHash();
71 }
72 MachineBasicBlock *getBlock() const {
73 return getMergePotentialsElt().getBlock();
74 }
75 bool tailIsWholeBlock() const {
76 return TailStartPos == getBlock()->begin();
77 }
78
79 void setBlock(MachineBasicBlock *MBB) {
80 getMergePotentialsElt().setBlock(MBB);
81 }
82 void setTailStartPos(MachineBasicBlock::iterator Pos) {
83 TailStartPos = Pos;
84 }
85 };
Evan Cheng030a0a02009-09-04 07:47:40 +000086 std::vector<SameTailElt> SameTails;
87
88 bool EnableTailMerge;
Evan Chengcbc988b2011-05-12 00:56:58 +000089 bool EnableHoistCommonCode;
Evan Cheng030a0a02009-09-04 07:47:40 +000090 const TargetInstrInfo *TII;
91 const TargetRegisterInfo *TRI;
92 MachineModuleInfo *MMI;
93 RegScavenger *RS;
94
95 bool TailMergeBlocks(MachineFunction &MF);
Dan Gohman412a3b92009-11-11 19:49:34 +000096 bool TryTailMergeBlocks(MachineBasicBlock* SuccBB,
97 MachineBasicBlock* PredBB);
Evan Cheng030a0a02009-09-04 07:47:40 +000098 void ReplaceTailWithBranchTo(MachineBasicBlock::iterator OldInst,
99 MachineBasicBlock *NewDest);
100 MachineBasicBlock *SplitMBBAt(MachineBasicBlock &CurMBB,
101 MachineBasicBlock::iterator BBI1);
Dan Gohman412a3b92009-11-11 19:49:34 +0000102 unsigned ComputeSameTails(unsigned CurHash, unsigned minCommonTailLength,
103 MachineBasicBlock *SuccBB,
104 MachineBasicBlock *PredBB);
Evan Cheng030a0a02009-09-04 07:47:40 +0000105 void RemoveBlocksWithHash(unsigned CurHash, MachineBasicBlock* SuccBB,
106 MachineBasicBlock* PredBB);
Evan Cheng4d54e5b2010-06-22 01:18:16 +0000107 bool CreateCommonTailOnlyBlock(MachineBasicBlock *&PredBB,
108 unsigned maxCommonTailLength,
109 unsigned &commonTailIndex);
Evan Cheng030a0a02009-09-04 07:47:40 +0000110
111 bool OptimizeBranches(MachineFunction &MF);
112 bool OptimizeBlock(MachineBasicBlock *MBB);
113 void RemoveDeadBlock(MachineBasicBlock *MBB);
114 bool OptimizeImpDefsBlock(MachineBasicBlock *MBB);
Evan Chengcbc988b2011-05-12 00:56:58 +0000115
116 bool HoistCommonCode(MachineFunction &MF);
117 bool HoistCommonCodeInSuccs(MachineBasicBlock *MBB);
Evan Cheng030a0a02009-09-04 07:47:40 +0000118 };
Evan Cheng030a0a02009-09-04 07:47:40 +0000119}
120
121#endif /* LLVM_CODEGEN_BRANCHFOLDING_HPP */