blob: 66b152cde007f360d0df1a755b6c328ddb529e0f [file] [log] [blame]
Michael Gottesman1649a872013-08-12 20:49:27 +00001//===-- BranchFolding.h - Fold machine code branch instructions -*- C++ -*-===//
Evan Cheng3d2fce02009-09-04 07:47:40 +00002//
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 Espindola3aeaf9e2011-06-14 15:31:54 +000013#include "llvm/ADT/SmallPtrSet.h"
Evan Cheng3d2fce02009-09-04 07:47:40 +000014#include "llvm/CodeGen/MachineBasicBlock.h"
Akira Hatanakabbd33f62014-08-07 19:30:13 +000015#include "llvm/Support/BlockFrequency.h"
Evan Cheng3d2fce02009-09-04 07:47:40 +000016#include <vector>
17
18namespace llvm {
Akira Hatanakabbd33f62014-08-07 19:30:13 +000019 class MachineBlockFrequencyInfo;
20 class MachineBranchProbabilityInfo;
Evan Cheng3d2fce02009-09-04 07:47:40 +000021 class MachineFunction;
22 class MachineModuleInfo;
23 class RegScavenger;
24 class TargetInstrInfo;
25 class TargetRegisterInfo;
26
Benjamin Kramer079b96e2013-09-11 18:05:11 +000027 class BranchFolder {
Evan Cheng3d2fce02009-09-04 07:47:40 +000028 public:
Akira Hatanakabbd33f62014-08-07 19:30:13 +000029 explicit BranchFolder(bool defaultEnableTailMerge, bool CommonHoist,
30 const MachineBlockFrequencyInfo &MBFI,
31 const MachineBranchProbabilityInfo &MBPI);
Evan Cheng3d2fce02009-09-04 07:47:40 +000032
33 bool OptimizeFunction(MachineFunction &MF,
34 const TargetInstrInfo *tii,
35 const TargetRegisterInfo *tri,
36 MachineModuleInfo *mmi);
37 private:
Dan Gohman02b15542009-11-11 21:57:02 +000038 class MergePotentialsElt {
39 unsigned Hash;
40 MachineBasicBlock *Block;
41 public:
42 MergePotentialsElt(unsigned h, MachineBasicBlock *b)
43 : Hash(h), Block(b) {}
44
45 unsigned getHash() const { return Hash; }
46 MachineBasicBlock *getBlock() const { return Block; }
47
48 void setBlock(MachineBasicBlock *MBB) {
49 Block = MBB;
50 }
51
52 bool operator<(const MergePotentialsElt &) const;
53 };
Evan Cheng3d2fce02009-09-04 07:47:40 +000054 typedef std::vector<MergePotentialsElt>::iterator MPIterator;
55 std::vector<MergePotentialsElt> MergePotentials;
Rafael Espindola3aeaf9e2011-06-14 15:31:54 +000056 SmallPtrSet<const MachineBasicBlock*, 2> TriedMerging;
Evan Cheng3d2fce02009-09-04 07:47:40 +000057
Dan Gohman02b15542009-11-11 21:57:02 +000058 class SameTailElt {
59 MPIterator MPIter;
60 MachineBasicBlock::iterator TailStartPos;
61 public:
62 SameTailElt(MPIterator mp, MachineBasicBlock::iterator tsp)
63 : MPIter(mp), TailStartPos(tsp) {}
64
65 MPIterator getMPIter() const {
66 return MPIter;
67 }
68 MergePotentialsElt &getMergePotentialsElt() const {
69 return *getMPIter();
70 }
71 MachineBasicBlock::iterator getTailStartPos() const {
72 return TailStartPos;
73 }
74 unsigned getHash() const {
75 return getMergePotentialsElt().getHash();
76 }
77 MachineBasicBlock *getBlock() const {
78 return getMergePotentialsElt().getBlock();
79 }
80 bool tailIsWholeBlock() const {
81 return TailStartPos == getBlock()->begin();
82 }
83
84 void setBlock(MachineBasicBlock *MBB) {
85 getMergePotentialsElt().setBlock(MBB);
86 }
87 void setTailStartPos(MachineBasicBlock::iterator Pos) {
88 TailStartPos = Pos;
89 }
90 };
Evan Cheng3d2fce02009-09-04 07:47:40 +000091 std::vector<SameTailElt> SameTails;
92
93 bool EnableTailMerge;
Evan Chengcfdf3392011-05-12 00:56:58 +000094 bool EnableHoistCommonCode;
Evan Cheng3d2fce02009-09-04 07:47:40 +000095 const TargetInstrInfo *TII;
96 const TargetRegisterInfo *TRI;
97 MachineModuleInfo *MMI;
98 RegScavenger *RS;
99
Akira Hatanakabbd33f62014-08-07 19:30:13 +0000100 /// \brief This class keeps track of branch frequencies of newly created
101 /// blocks and tail-merged blocks.
102 class MBFIWrapper {
103 public:
104 MBFIWrapper(const MachineBlockFrequencyInfo &I) : MBFI(I) {}
105 BlockFrequency getBlockFreq(const MachineBasicBlock *MBB) const;
106 void setBlockFreq(const MachineBasicBlock *MBB, BlockFrequency F);
107
108 private:
109 const MachineBlockFrequencyInfo &MBFI;
110 DenseMap<const MachineBasicBlock *, BlockFrequency> MergedBBFreq;
111 };
112
113 MBFIWrapper MBBFreqInfo;
114 const MachineBranchProbabilityInfo &MBPI;
115
Evan Cheng3d2fce02009-09-04 07:47:40 +0000116 bool TailMergeBlocks(MachineFunction &MF);
Dan Gohman34eeb4e2009-11-11 19:49:34 +0000117 bool TryTailMergeBlocks(MachineBasicBlock* SuccBB,
118 MachineBasicBlock* PredBB);
Akira Hatanakabbd33f62014-08-07 19:30:13 +0000119 void setCommonTailEdgeWeights(MachineBasicBlock &TailMBB);
Eli Friedmanbf007362011-07-06 23:41:48 +0000120 void MaintainLiveIns(MachineBasicBlock *CurMBB,
121 MachineBasicBlock *NewMBB);
Evan Cheng3d2fce02009-09-04 07:47:40 +0000122 void ReplaceTailWithBranchTo(MachineBasicBlock::iterator OldInst,
123 MachineBasicBlock *NewDest);
124 MachineBasicBlock *SplitMBBAt(MachineBasicBlock &CurMBB,
Andrew Trick97a1d7c2013-06-24 01:55:01 +0000125 MachineBasicBlock::iterator BBI1,
126 const BasicBlock *BB);
Dan Gohman34eeb4e2009-11-11 19:49:34 +0000127 unsigned ComputeSameTails(unsigned CurHash, unsigned minCommonTailLength,
128 MachineBasicBlock *SuccBB,
129 MachineBasicBlock *PredBB);
Evan Cheng3d2fce02009-09-04 07:47:40 +0000130 void RemoveBlocksWithHash(unsigned CurHash, MachineBasicBlock* SuccBB,
131 MachineBasicBlock* PredBB);
Evan Cheng37bb6172010-06-22 01:18:16 +0000132 bool CreateCommonTailOnlyBlock(MachineBasicBlock *&PredBB,
Andrew Trick97a1d7c2013-06-24 01:55:01 +0000133 MachineBasicBlock *SuccBB,
Evan Cheng37bb6172010-06-22 01:18:16 +0000134 unsigned maxCommonTailLength,
135 unsigned &commonTailIndex);
Evan Cheng3d2fce02009-09-04 07:47:40 +0000136
137 bool OptimizeBranches(MachineFunction &MF);
138 bool OptimizeBlock(MachineBasicBlock *MBB);
139 void RemoveDeadBlock(MachineBasicBlock *MBB);
140 bool OptimizeImpDefsBlock(MachineBasicBlock *MBB);
Evan Chengcfdf3392011-05-12 00:56:58 +0000141
142 bool HoistCommonCode(MachineFunction &MF);
143 bool HoistCommonCodeInSuccs(MachineBasicBlock *MBB);
Evan Cheng3d2fce02009-09-04 07:47:40 +0000144 };
Evan Cheng3d2fce02009-09-04 07:47:40 +0000145}
146
147#endif /* LLVM_CODEGEN_BRANCHFOLDING_HPP */