blob: 4852721eea10247f434c6d2aa6cfea826ed1b09e [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"
Matthias Braunaeab09f2016-07-12 18:44:33 +000014#include "llvm/CodeGen/LivePhysRegs.h"
Evan Cheng3d2fce02009-09-04 07:47:40 +000015#include "llvm/CodeGen/MachineBasicBlock.h"
Akira Hatanakabbd33f62014-08-07 19:30:13 +000016#include "llvm/Support/BlockFrequency.h"
Evan Cheng3d2fce02009-09-04 07:47:40 +000017#include <vector>
18
19namespace llvm {
Akira Hatanakabbd33f62014-08-07 19:30:13 +000020 class MachineBlockFrequencyInfo;
21 class MachineBranchProbabilityInfo;
Evan Cheng3d2fce02009-09-04 07:47:40 +000022 class MachineFunction;
23 class MachineModuleInfo;
Haicheng Wu5b458cc2016-06-09 15:24:29 +000024 class MachineLoopInfo;
Evan Cheng3d2fce02009-09-04 07:47:40 +000025 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
Kyle Butt64e42812016-08-18 18:57:29 +000032 explicit BranchFolder(bool defaultEnableTailMerge,
33 bool CommonHoist,
Haicheng Wu5b458cc2016-06-09 15:24:29 +000034 MBFIWrapper &MBFI,
Kyle Butt64e42812016-08-18 18:57:29 +000035 const MachineBranchProbabilityInfo &MBPI,
36 // Min tail length to merge. Defaults to commandline
37 // flag. Ignored for optsize.
38 unsigned MinCommonTailLength = 0);
Evan Cheng3d2fce02009-09-04 07:47:40 +000039
Taewook Oh1b192332017-03-15 06:29:23 +000040 /// Perhaps branch folding, tail merging and other CFG optimizations on the
41 /// given function. Block placement changes the layout and may create new
42 /// tail merging opportunities.
Haicheng Wu5b458cc2016-06-09 15:24:29 +000043 bool OptimizeFunction(MachineFunction &MF, const TargetInstrInfo *tii,
44 const TargetRegisterInfo *tri, MachineModuleInfo *mmi,
45 MachineLoopInfo *mli = nullptr,
46 bool AfterPlacement = false);
47
Evan Cheng3d2fce02009-09-04 07:47:40 +000048 private:
Dan Gohman02b15542009-11-11 21:57:02 +000049 class MergePotentialsElt {
50 unsigned Hash;
51 MachineBasicBlock *Block;
52 public:
53 MergePotentialsElt(unsigned h, MachineBasicBlock *b)
54 : Hash(h), Block(b) {}
55
56 unsigned getHash() const { return Hash; }
57 MachineBasicBlock *getBlock() const { return Block; }
58
59 void setBlock(MachineBasicBlock *MBB) {
60 Block = MBB;
61 }
62
63 bool operator<(const MergePotentialsElt &) const;
64 };
Evan Cheng3d2fce02009-09-04 07:47:40 +000065 typedef std::vector<MergePotentialsElt>::iterator MPIterator;
66 std::vector<MergePotentialsElt> MergePotentials;
Rafael Espindola3aeaf9e2011-06-14 15:31:54 +000067 SmallPtrSet<const MachineBasicBlock*, 2> TriedMerging;
David Majnemer16193552015-10-04 02:22:52 +000068 DenseMap<const MachineBasicBlock *, int> FuncletMembership;
Evan Cheng3d2fce02009-09-04 07:47:40 +000069
Dan Gohman02b15542009-11-11 21:57:02 +000070 class SameTailElt {
71 MPIterator MPIter;
72 MachineBasicBlock::iterator TailStartPos;
73 public:
74 SameTailElt(MPIterator mp, MachineBasicBlock::iterator tsp)
75 : MPIter(mp), TailStartPos(tsp) {}
76
77 MPIterator getMPIter() const {
78 return MPIter;
79 }
80 MergePotentialsElt &getMergePotentialsElt() const {
81 return *getMPIter();
82 }
83 MachineBasicBlock::iterator getTailStartPos() const {
84 return TailStartPos;
85 }
86 unsigned getHash() const {
87 return getMergePotentialsElt().getHash();
88 }
89 MachineBasicBlock *getBlock() const {
90 return getMergePotentialsElt().getBlock();
91 }
92 bool tailIsWholeBlock() const {
93 return TailStartPos == getBlock()->begin();
94 }
95
96 void setBlock(MachineBasicBlock *MBB) {
97 getMergePotentialsElt().setBlock(MBB);
98 }
99 void setTailStartPos(MachineBasicBlock::iterator Pos) {
100 TailStartPos = Pos;
101 }
102 };
Evan Cheng3d2fce02009-09-04 07:47:40 +0000103 std::vector<SameTailElt> SameTails;
104
Haicheng Wu5b458cc2016-06-09 15:24:29 +0000105 bool AfterBlockPlacement;
Evan Cheng3d2fce02009-09-04 07:47:40 +0000106 bool EnableTailMerge;
Evan Chengcfdf3392011-05-12 00:56:58 +0000107 bool EnableHoistCommonCode;
Matthias Braunaeab09f2016-07-12 18:44:33 +0000108 bool UpdateLiveIns;
Kyle Butt64e42812016-08-18 18:57:29 +0000109 unsigned MinCommonTailLength;
Evan Cheng3d2fce02009-09-04 07:47:40 +0000110 const TargetInstrInfo *TII;
111 const TargetRegisterInfo *TRI;
112 MachineModuleInfo *MMI;
Haicheng Wu5b458cc2016-06-09 15:24:29 +0000113 MachineLoopInfo *MLI;
Matthias Braunaeab09f2016-07-12 18:44:33 +0000114 LivePhysRegs LiveRegs;
Evan Cheng3d2fce02009-09-04 07:47:40 +0000115
Haicheng Wu5b458cc2016-06-09 15:24:29 +0000116 public:
Akira Hatanakabbd33f62014-08-07 19:30:13 +0000117 /// \brief This class keeps track of branch frequencies of newly created
118 /// blocks and tail-merged blocks.
119 class MBFIWrapper {
120 public:
121 MBFIWrapper(const MachineBlockFrequencyInfo &I) : MBFI(I) {}
122 BlockFrequency getBlockFreq(const MachineBasicBlock *MBB) const;
123 void setBlockFreq(const MachineBasicBlock *MBB, BlockFrequency F);
Haicheng Wu5b458cc2016-06-09 15:24:29 +0000124 raw_ostream &printBlockFreq(raw_ostream &OS,
125 const MachineBasicBlock *MBB) const;
126 raw_ostream &printBlockFreq(raw_ostream &OS,
127 const BlockFrequency Freq) const;
Xinliang David Li538d6662017-02-15 19:21:04 +0000128 void view(const Twine &Name, bool isSimple = true);
Kyle Buttb15c0662017-01-31 23:48:32 +0000129 uint64_t getEntryFreq() const;
Akira Hatanakabbd33f62014-08-07 19:30:13 +0000130
131 private:
132 const MachineBlockFrequencyInfo &MBFI;
133 DenseMap<const MachineBasicBlock *, BlockFrequency> MergedBBFreq;
134 };
135
Haicheng Wu5b458cc2016-06-09 15:24:29 +0000136 private:
137 MBFIWrapper &MBBFreqInfo;
Akira Hatanakabbd33f62014-08-07 19:30:13 +0000138 const MachineBranchProbabilityInfo &MBPI;
139
Evan Cheng3d2fce02009-09-04 07:47:40 +0000140 bool TailMergeBlocks(MachineFunction &MF);
Dan Gohman34eeb4e2009-11-11 19:49:34 +0000141 bool TryTailMergeBlocks(MachineBasicBlock* SuccBB,
Kyle Butt64e42812016-08-18 18:57:29 +0000142 MachineBasicBlock* PredBB,
143 unsigned MinCommonTailLength);
Akira Hatanakabbd33f62014-08-07 19:30:13 +0000144 void setCommonTailEdgeWeights(MachineBasicBlock &TailMBB);
Taewook Oh1b192332017-03-15 06:29:23 +0000145
146 /// Delete the instruction OldInst and everything after it, replacing it
147 /// with an unconditional branch to NewDest.
Evan Cheng3d2fce02009-09-04 07:47:40 +0000148 void ReplaceTailWithBranchTo(MachineBasicBlock::iterator OldInst,
149 MachineBasicBlock *NewDest);
Taewook Oh1b192332017-03-15 06:29:23 +0000150
151 /// Given a machine basic block and an iterator into it, split the MBB so
152 /// that the part before the iterator falls into the part starting at the
153 /// iterator. This returns the new MBB.
Evan Cheng3d2fce02009-09-04 07:47:40 +0000154 MachineBasicBlock *SplitMBBAt(MachineBasicBlock &CurMBB,
Andrew Trick97a1d7c2013-06-24 01:55:01 +0000155 MachineBasicBlock::iterator BBI1,
156 const BasicBlock *BB);
Taewook Oh1b192332017-03-15 06:29:23 +0000157
158 /// Look through all the blocks in MergePotentials that have hash CurHash
159 /// (guaranteed to match the last element). Build the vector SameTails of
160 /// all those that have the (same) largest number of instructions in common
161 /// of any pair of these blocks. SameTails entries contain an iterator into
162 /// MergePotentials (from which the MachineBasicBlock can be found) and a
163 /// MachineBasicBlock::iterator into that MBB indicating the instruction
164 /// where the matching code sequence begins. Order of elements in SameTails
165 /// is the reverse of the order in which those blocks appear in
166 /// MergePotentials (where they are not necessarily consecutive).
Dan Gohman34eeb4e2009-11-11 19:49:34 +0000167 unsigned ComputeSameTails(unsigned CurHash, unsigned minCommonTailLength,
168 MachineBasicBlock *SuccBB,
169 MachineBasicBlock *PredBB);
Taewook Oh1b192332017-03-15 06:29:23 +0000170
171 /// Remove all blocks with hash CurHash from MergePotentials, restoring
172 /// branches at ends of blocks as appropriate.
Evan Cheng3d2fce02009-09-04 07:47:40 +0000173 void RemoveBlocksWithHash(unsigned CurHash, MachineBasicBlock* SuccBB,
174 MachineBasicBlock* PredBB);
Taewook Oh1b192332017-03-15 06:29:23 +0000175
176 /// None of the blocks to be tail-merged consist only of the common tail.
177 /// Create a block that does by splitting one.
Evan Cheng37bb6172010-06-22 01:18:16 +0000178 bool CreateCommonTailOnlyBlock(MachineBasicBlock *&PredBB,
Andrew Trick97a1d7c2013-06-24 01:55:01 +0000179 MachineBasicBlock *SuccBB,
Evan Cheng37bb6172010-06-22 01:18:16 +0000180 unsigned maxCommonTailLength,
181 unsigned &commonTailIndex);
Taewook Oh1b192332017-03-15 06:29:23 +0000182
183 /// Create merged DebugLocs of identical instructions across SameTails and
184 /// assign it to the instruction in common tail.
Taewook Ohfb1833e2017-03-15 05:44:59 +0000185 void MergeCommonTailDebugLocs(unsigned commonTailIndex);
Evan Cheng3d2fce02009-09-04 07:47:40 +0000186
187 bool OptimizeBranches(MachineFunction &MF);
Taewook Oh1b192332017-03-15 06:29:23 +0000188
189 /// Analyze and optimize control flow related to the specified block. This
190 /// is never called on the entry block.
Evan Cheng3d2fce02009-09-04 07:47:40 +0000191 bool OptimizeBlock(MachineBasicBlock *MBB);
Taewook Oh1b192332017-03-15 06:29:23 +0000192
193 /// Remove the specified dead machine basic block from the function,
194 /// updating the CFG.
Evan Cheng3d2fce02009-09-04 07:47:40 +0000195 void RemoveDeadBlock(MachineBasicBlock *MBB);
Evan Chengcfdf3392011-05-12 00:56:58 +0000196
Taewook Oh1b192332017-03-15 06:29:23 +0000197 /// Hoist common instruction sequences at the start of basic blocks to their
198 /// common predecessor.
Evan Chengcfdf3392011-05-12 00:56:58 +0000199 bool HoistCommonCode(MachineFunction &MF);
Taewook Oh1b192332017-03-15 06:29:23 +0000200
201 /// If the successors of MBB has common instruction sequence at the start of
202 /// the function, move the instructions before MBB terminator if it's legal.
Evan Chengcfdf3392011-05-12 00:56:58 +0000203 bool HoistCommonCodeInSuccs(MachineBasicBlock *MBB);
Evan Cheng3d2fce02009-09-04 07:47:40 +0000204 };
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000205}
Evan Cheng3d2fce02009-09-04 07:47:40 +0000206
207#endif /* LLVM_CODEGEN_BRANCHFOLDING_HPP */