blob: f7040990f1319c52acc5bacd534ba649b5e15f00 [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
Benjamin Kramera7c40ef2014-08-13 16:26:38 +000010#ifndef LLVM_LIB_CODEGEN_BRANCHFOLDING_H
11#define LLVM_LIB_CODEGEN_BRANCHFOLDING_H
Evan Cheng3d2fce02009-09-04 07:47:40 +000012
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;
Haicheng Wu5b458cc2016-06-09 15:24:29 +000023 class MachineLoopInfo;
Evan Cheng3d2fce02009-09-04 07:47:40 +000024 class RegScavenger;
25 class TargetInstrInfo;
26 class TargetRegisterInfo;
27
Benjamin Kramerf4c20252015-07-01 14:47:39 +000028 class LLVM_LIBRARY_VISIBILITY BranchFolder {
Evan Cheng3d2fce02009-09-04 07:47:40 +000029 public:
Haicheng Wu5b458cc2016-06-09 15:24:29 +000030 class MBFIWrapper;
31
Akira Hatanakabbd33f62014-08-07 19:30:13 +000032 explicit BranchFolder(bool defaultEnableTailMerge, bool CommonHoist,
Haicheng Wu5b458cc2016-06-09 15:24:29 +000033 MBFIWrapper &MBFI,
Akira Hatanakabbd33f62014-08-07 19:30:13 +000034 const MachineBranchProbabilityInfo &MBPI);
Evan Cheng3d2fce02009-09-04 07:47:40 +000035
Haicheng Wu5b458cc2016-06-09 15:24:29 +000036 bool OptimizeFunction(MachineFunction &MF, const TargetInstrInfo *tii,
37 const TargetRegisterInfo *tri, MachineModuleInfo *mmi,
38 MachineLoopInfo *mli = nullptr,
39 bool AfterPlacement = false);
40
Evan Cheng3d2fce02009-09-04 07:47:40 +000041 private:
Dan Gohman02b15542009-11-11 21:57:02 +000042 class MergePotentialsElt {
43 unsigned Hash;
44 MachineBasicBlock *Block;
45 public:
46 MergePotentialsElt(unsigned h, MachineBasicBlock *b)
47 : Hash(h), Block(b) {}
48
49 unsigned getHash() const { return Hash; }
50 MachineBasicBlock *getBlock() const { return Block; }
51
52 void setBlock(MachineBasicBlock *MBB) {
53 Block = MBB;
54 }
55
56 bool operator<(const MergePotentialsElt &) const;
57 };
Evan Cheng3d2fce02009-09-04 07:47:40 +000058 typedef std::vector<MergePotentialsElt>::iterator MPIterator;
59 std::vector<MergePotentialsElt> MergePotentials;
Rafael Espindola3aeaf9e2011-06-14 15:31:54 +000060 SmallPtrSet<const MachineBasicBlock*, 2> TriedMerging;
David Majnemer16193552015-10-04 02:22:52 +000061 DenseMap<const MachineBasicBlock *, int> FuncletMembership;
Evan Cheng3d2fce02009-09-04 07:47:40 +000062
Dan Gohman02b15542009-11-11 21:57:02 +000063 class SameTailElt {
64 MPIterator MPIter;
65 MachineBasicBlock::iterator TailStartPos;
66 public:
67 SameTailElt(MPIterator mp, MachineBasicBlock::iterator tsp)
68 : MPIter(mp), TailStartPos(tsp) {}
69
70 MPIterator getMPIter() const {
71 return MPIter;
72 }
73 MergePotentialsElt &getMergePotentialsElt() const {
74 return *getMPIter();
75 }
76 MachineBasicBlock::iterator getTailStartPos() const {
77 return TailStartPos;
78 }
79 unsigned getHash() const {
80 return getMergePotentialsElt().getHash();
81 }
82 MachineBasicBlock *getBlock() const {
83 return getMergePotentialsElt().getBlock();
84 }
85 bool tailIsWholeBlock() const {
86 return TailStartPos == getBlock()->begin();
87 }
88
89 void setBlock(MachineBasicBlock *MBB) {
90 getMergePotentialsElt().setBlock(MBB);
91 }
92 void setTailStartPos(MachineBasicBlock::iterator Pos) {
93 TailStartPos = Pos;
94 }
95 };
Evan Cheng3d2fce02009-09-04 07:47:40 +000096 std::vector<SameTailElt> SameTails;
97
Haicheng Wu5b458cc2016-06-09 15:24:29 +000098 bool AfterBlockPlacement;
Evan Cheng3d2fce02009-09-04 07:47:40 +000099 bool EnableTailMerge;
Evan Chengcfdf3392011-05-12 00:56:58 +0000100 bool EnableHoistCommonCode;
Evan Cheng3d2fce02009-09-04 07:47:40 +0000101 const TargetInstrInfo *TII;
102 const TargetRegisterInfo *TRI;
103 MachineModuleInfo *MMI;
Haicheng Wu5b458cc2016-06-09 15:24:29 +0000104 MachineLoopInfo *MLI;
Evan Cheng3d2fce02009-09-04 07:47:40 +0000105 RegScavenger *RS;
106
Haicheng Wu5b458cc2016-06-09 15:24:29 +0000107 public:
Akira Hatanakabbd33f62014-08-07 19:30:13 +0000108 /// \brief This class keeps track of branch frequencies of newly created
109 /// blocks and tail-merged blocks.
110 class MBFIWrapper {
111 public:
112 MBFIWrapper(const MachineBlockFrequencyInfo &I) : MBFI(I) {}
113 BlockFrequency getBlockFreq(const MachineBasicBlock *MBB) const;
114 void setBlockFreq(const MachineBasicBlock *MBB, BlockFrequency F);
Haicheng Wu5b458cc2016-06-09 15:24:29 +0000115 raw_ostream &printBlockFreq(raw_ostream &OS,
116 const MachineBasicBlock *MBB) const;
117 raw_ostream &printBlockFreq(raw_ostream &OS,
118 const BlockFrequency Freq) const;
Akira Hatanakabbd33f62014-08-07 19:30:13 +0000119
120 private:
121 const MachineBlockFrequencyInfo &MBFI;
122 DenseMap<const MachineBasicBlock *, BlockFrequency> MergedBBFreq;
123 };
124
Haicheng Wu5b458cc2016-06-09 15:24:29 +0000125 private:
126 MBFIWrapper &MBBFreqInfo;
Akira Hatanakabbd33f62014-08-07 19:30:13 +0000127 const MachineBranchProbabilityInfo &MBPI;
128
Evan Cheng3d2fce02009-09-04 07:47:40 +0000129 bool TailMergeBlocks(MachineFunction &MF);
Dan Gohman34eeb4e2009-11-11 19:49:34 +0000130 bool TryTailMergeBlocks(MachineBasicBlock* SuccBB,
131 MachineBasicBlock* PredBB);
Akira Hatanakabbd33f62014-08-07 19:30:13 +0000132 void setCommonTailEdgeWeights(MachineBasicBlock &TailMBB);
Eli Friedmanbf007362011-07-06 23:41:48 +0000133 void MaintainLiveIns(MachineBasicBlock *CurMBB,
134 MachineBasicBlock *NewMBB);
Evan Cheng3d2fce02009-09-04 07:47:40 +0000135 void ReplaceTailWithBranchTo(MachineBasicBlock::iterator OldInst,
136 MachineBasicBlock *NewDest);
137 MachineBasicBlock *SplitMBBAt(MachineBasicBlock &CurMBB,
Andrew Trick97a1d7c2013-06-24 01:55:01 +0000138 MachineBasicBlock::iterator BBI1,
139 const BasicBlock *BB);
Dan Gohman34eeb4e2009-11-11 19:49:34 +0000140 unsigned ComputeSameTails(unsigned CurHash, unsigned minCommonTailLength,
141 MachineBasicBlock *SuccBB,
142 MachineBasicBlock *PredBB);
Evan Cheng3d2fce02009-09-04 07:47:40 +0000143 void RemoveBlocksWithHash(unsigned CurHash, MachineBasicBlock* SuccBB,
144 MachineBasicBlock* PredBB);
Evan Cheng37bb6172010-06-22 01:18:16 +0000145 bool CreateCommonTailOnlyBlock(MachineBasicBlock *&PredBB,
Andrew Trick97a1d7c2013-06-24 01:55:01 +0000146 MachineBasicBlock *SuccBB,
Evan Cheng37bb6172010-06-22 01:18:16 +0000147 unsigned maxCommonTailLength,
148 unsigned &commonTailIndex);
Evan Cheng3d2fce02009-09-04 07:47:40 +0000149
150 bool OptimizeBranches(MachineFunction &MF);
151 bool OptimizeBlock(MachineBasicBlock *MBB);
152 void RemoveDeadBlock(MachineBasicBlock *MBB);
153 bool OptimizeImpDefsBlock(MachineBasicBlock *MBB);
Evan Chengcfdf3392011-05-12 00:56:58 +0000154
155 bool HoistCommonCode(MachineFunction &MF);
156 bool HoistCommonCodeInSuccs(MachineBasicBlock *MBB);
Evan Cheng3d2fce02009-09-04 07:47:40 +0000157 };
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000158}
Evan Cheng3d2fce02009-09-04 07:47:40 +0000159
160#endif /* LLVM_CODEGEN_BRANCHFOLDING_HPP */