blob: 761ff9c7d54e2e291033f48eee2571d81819a07c [file] [log] [blame]
Eugene Zelenko149178d2017-10-10 22:33:29 +00001//===- BranchFolding.h - Fold machine code branch instructions --*- C++ -*-===//
Evan Cheng3d2fce02009-09-04 07:47:40 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Evan Cheng3d2fce02009-09-04 07:47:40 +00006//
7//===----------------------------------------------------------------------===//
8
Benjamin Kramera7c40ef2014-08-13 16:26:38 +00009#ifndef LLVM_LIB_CODEGEN_BRANCHFOLDING_H
10#define LLVM_LIB_CODEGEN_BRANCHFOLDING_H
Evan Cheng3d2fce02009-09-04 07:47:40 +000011
Eugene Zelenko149178d2017-10-10 22:33:29 +000012#include "llvm/ADT/DenseMap.h"
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"
Eugene Zelenko149178d2017-10-10 22:33:29 +000017#include "llvm/Support/Compiler.h"
18#include <cstdint>
Evan Cheng3d2fce02009-09-04 07:47:40 +000019#include <vector>
20
21namespace llvm {
Eugene Zelenko149178d2017-10-10 22:33:29 +000022
23class BasicBlock;
24class MachineBlockFrequencyInfo;
25class MachineBranchProbabilityInfo;
26class MachineFunction;
27class MachineLoopInfo;
28class MachineModuleInfo;
29class MachineRegisterInfo;
30class raw_ostream;
31class TargetInstrInfo;
32class TargetRegisterInfo;
Evan Cheng3d2fce02009-09-04 07:47:40 +000033
Benjamin Kramerf4c20252015-07-01 14:47:39 +000034 class LLVM_LIBRARY_VISIBILITY BranchFolder {
Evan Cheng3d2fce02009-09-04 07:47:40 +000035 public:
Haicheng Wu5b458cc2016-06-09 15:24:29 +000036 class MBFIWrapper;
37
Kyle Butt64e42812016-08-18 18:57:29 +000038 explicit BranchFolder(bool defaultEnableTailMerge,
39 bool CommonHoist,
Fangrui Songcb0bab82018-07-16 18:51:40 +000040 MBFIWrapper &FreqInfo,
41 const MachineBranchProbabilityInfo &ProbInfo,
Kyle Butt64e42812016-08-18 18:57:29 +000042 // Min tail length to merge. Defaults to commandline
43 // flag. Ignored for optsize.
Fangrui Songcb0bab82018-07-16 18:51:40 +000044 unsigned MinTailLength = 0);
Evan Cheng3d2fce02009-09-04 07:47:40 +000045
Taewook Oh1b192332017-03-15 06:29:23 +000046 /// Perhaps branch folding, tail merging and other CFG optimizations on the
47 /// given function. Block placement changes the layout and may create new
48 /// tail merging opportunities.
Haicheng Wu5b458cc2016-06-09 15:24:29 +000049 bool OptimizeFunction(MachineFunction &MF, const TargetInstrInfo *tii,
50 const TargetRegisterInfo *tri, MachineModuleInfo *mmi,
51 MachineLoopInfo *mli = nullptr,
52 bool AfterPlacement = false);
53
Evan Cheng3d2fce02009-09-04 07:47:40 +000054 private:
Dan Gohman02b15542009-11-11 21:57:02 +000055 class MergePotentialsElt {
56 unsigned Hash;
57 MachineBasicBlock *Block;
Eugene Zelenko149178d2017-10-10 22:33:29 +000058
Dan Gohman02b15542009-11-11 21:57:02 +000059 public:
60 MergePotentialsElt(unsigned h, MachineBasicBlock *b)
61 : Hash(h), Block(b) {}
62
63 unsigned getHash() const { return Hash; }
64 MachineBasicBlock *getBlock() const { return Block; }
65
66 void setBlock(MachineBasicBlock *MBB) {
67 Block = MBB;
68 }
69
70 bool operator<(const MergePotentialsElt &) const;
71 };
Eugene Zelenko149178d2017-10-10 22:33:29 +000072
73 using MPIterator = std::vector<MergePotentialsElt>::iterator;
74
Evan Cheng3d2fce02009-09-04 07:47:40 +000075 std::vector<MergePotentialsElt> MergePotentials;
Rafael Espindola3aeaf9e2011-06-14 15:31:54 +000076 SmallPtrSet<const MachineBasicBlock*, 2> TriedMerging;
Heejin Ahnd69acf32018-06-01 00:03:21 +000077 DenseMap<const MachineBasicBlock *, int> EHScopeMembership;
Evan Cheng3d2fce02009-09-04 07:47:40 +000078
Dan Gohman02b15542009-11-11 21:57:02 +000079 class SameTailElt {
80 MPIterator MPIter;
81 MachineBasicBlock::iterator TailStartPos;
Eugene Zelenko149178d2017-10-10 22:33:29 +000082
Dan Gohman02b15542009-11-11 21:57:02 +000083 public:
84 SameTailElt(MPIterator mp, MachineBasicBlock::iterator tsp)
85 : MPIter(mp), TailStartPos(tsp) {}
86
87 MPIterator getMPIter() const {
88 return MPIter;
89 }
Eugene Zelenko149178d2017-10-10 22:33:29 +000090
Dan Gohman02b15542009-11-11 21:57:02 +000091 MergePotentialsElt &getMergePotentialsElt() const {
92 return *getMPIter();
93 }
Eugene Zelenko149178d2017-10-10 22:33:29 +000094
Dan Gohman02b15542009-11-11 21:57:02 +000095 MachineBasicBlock::iterator getTailStartPos() const {
96 return TailStartPos;
97 }
Eugene Zelenko149178d2017-10-10 22:33:29 +000098
Dan Gohman02b15542009-11-11 21:57:02 +000099 unsigned getHash() const {
100 return getMergePotentialsElt().getHash();
101 }
Eugene Zelenko149178d2017-10-10 22:33:29 +0000102
Dan Gohman02b15542009-11-11 21:57:02 +0000103 MachineBasicBlock *getBlock() const {
104 return getMergePotentialsElt().getBlock();
105 }
Eugene Zelenko149178d2017-10-10 22:33:29 +0000106
Dan Gohman02b15542009-11-11 21:57:02 +0000107 bool tailIsWholeBlock() const {
108 return TailStartPos == getBlock()->begin();
109 }
110
111 void setBlock(MachineBasicBlock *MBB) {
112 getMergePotentialsElt().setBlock(MBB);
113 }
Eugene Zelenko149178d2017-10-10 22:33:29 +0000114
Dan Gohman02b15542009-11-11 21:57:02 +0000115 void setTailStartPos(MachineBasicBlock::iterator Pos) {
116 TailStartPos = Pos;
117 }
118 };
Evan Cheng3d2fce02009-09-04 07:47:40 +0000119 std::vector<SameTailElt> SameTails;
120
Haicheng Wu5b458cc2016-06-09 15:24:29 +0000121 bool AfterBlockPlacement;
Evan Cheng3d2fce02009-09-04 07:47:40 +0000122 bool EnableTailMerge;
Evan Chengcfdf3392011-05-12 00:56:58 +0000123 bool EnableHoistCommonCode;
Matthias Braunaeab09f2016-07-12 18:44:33 +0000124 bool UpdateLiveIns;
Kyle Butt64e42812016-08-18 18:57:29 +0000125 unsigned MinCommonTailLength;
Evan Cheng3d2fce02009-09-04 07:47:40 +0000126 const TargetInstrInfo *TII;
Matthias Braune51c4352017-05-26 06:32:31 +0000127 const MachineRegisterInfo *MRI;
Evan Cheng3d2fce02009-09-04 07:47:40 +0000128 const TargetRegisterInfo *TRI;
129 MachineModuleInfo *MMI;
Haicheng Wu5b458cc2016-06-09 15:24:29 +0000130 MachineLoopInfo *MLI;
Matthias Braunaeab09f2016-07-12 18:44:33 +0000131 LivePhysRegs LiveRegs;
Evan Cheng3d2fce02009-09-04 07:47:40 +0000132
Haicheng Wu5b458cc2016-06-09 15:24:29 +0000133 public:
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000134 /// This class keeps track of branch frequencies of newly created
Akira Hatanakabbd33f62014-08-07 19:30:13 +0000135 /// blocks and tail-merged blocks.
136 class MBFIWrapper {
137 public:
138 MBFIWrapper(const MachineBlockFrequencyInfo &I) : MBFI(I) {}
Eugene Zelenko149178d2017-10-10 22:33:29 +0000139
Akira Hatanakabbd33f62014-08-07 19:30:13 +0000140 BlockFrequency getBlockFreq(const MachineBasicBlock *MBB) const;
141 void setBlockFreq(const MachineBasicBlock *MBB, BlockFrequency F);
Haicheng Wu5b458cc2016-06-09 15:24:29 +0000142 raw_ostream &printBlockFreq(raw_ostream &OS,
143 const MachineBasicBlock *MBB) const;
144 raw_ostream &printBlockFreq(raw_ostream &OS,
145 const BlockFrequency Freq) const;
Xinliang David Li538d6662017-02-15 19:21:04 +0000146 void view(const Twine &Name, bool isSimple = true);
Kyle Buttb15c0662017-01-31 23:48:32 +0000147 uint64_t getEntryFreq() const;
Akira Hatanakabbd33f62014-08-07 19:30:13 +0000148
149 private:
150 const MachineBlockFrequencyInfo &MBFI;
151 DenseMap<const MachineBasicBlock *, BlockFrequency> MergedBBFreq;
152 };
153
Haicheng Wu5b458cc2016-06-09 15:24:29 +0000154 private:
155 MBFIWrapper &MBBFreqInfo;
Akira Hatanakabbd33f62014-08-07 19:30:13 +0000156 const MachineBranchProbabilityInfo &MBPI;
157
Evan Cheng3d2fce02009-09-04 07:47:40 +0000158 bool TailMergeBlocks(MachineFunction &MF);
Dan Gohman34eeb4e2009-11-11 19:49:34 +0000159 bool TryTailMergeBlocks(MachineBasicBlock* SuccBB,
Kyle Butt64e42812016-08-18 18:57:29 +0000160 MachineBasicBlock* PredBB,
161 unsigned MinCommonTailLength);
Akira Hatanakabbd33f62014-08-07 19:30:13 +0000162 void setCommonTailEdgeWeights(MachineBasicBlock &TailMBB);
Taewook Oh1b192332017-03-15 06:29:23 +0000163
164 /// Delete the instruction OldInst and everything after it, replacing it
165 /// with an unconditional branch to NewDest.
Matthias Braunc9056b82017-09-06 20:45:24 +0000166 void replaceTailWithBranchTo(MachineBasicBlock::iterator OldInst,
167 MachineBasicBlock &NewDest);
Taewook Oh1b192332017-03-15 06:29:23 +0000168
169 /// Given a machine basic block and an iterator into it, split the MBB so
170 /// that the part before the iterator falls into the part starting at the
171 /// iterator. This returns the new MBB.
Evan Cheng3d2fce02009-09-04 07:47:40 +0000172 MachineBasicBlock *SplitMBBAt(MachineBasicBlock &CurMBB,
Andrew Trick97a1d7c2013-06-24 01:55:01 +0000173 MachineBasicBlock::iterator BBI1,
174 const BasicBlock *BB);
Taewook Oh1b192332017-03-15 06:29:23 +0000175
176 /// Look through all the blocks in MergePotentials that have hash CurHash
177 /// (guaranteed to match the last element). Build the vector SameTails of
178 /// all those that have the (same) largest number of instructions in common
179 /// of any pair of these blocks. SameTails entries contain an iterator into
180 /// MergePotentials (from which the MachineBasicBlock can be found) and a
181 /// MachineBasicBlock::iterator into that MBB indicating the instruction
182 /// where the matching code sequence begins. Order of elements in SameTails
183 /// is the reverse of the order in which those blocks appear in
184 /// MergePotentials (where they are not necessarily consecutive).
Dan Gohman34eeb4e2009-11-11 19:49:34 +0000185 unsigned ComputeSameTails(unsigned CurHash, unsigned minCommonTailLength,
186 MachineBasicBlock *SuccBB,
187 MachineBasicBlock *PredBB);
Taewook Oh1b192332017-03-15 06:29:23 +0000188
189 /// Remove all blocks with hash CurHash from MergePotentials, restoring
190 /// branches at ends of blocks as appropriate.
Evan Cheng3d2fce02009-09-04 07:47:40 +0000191 void RemoveBlocksWithHash(unsigned CurHash, MachineBasicBlock* SuccBB,
192 MachineBasicBlock* PredBB);
Taewook Oh1b192332017-03-15 06:29:23 +0000193
194 /// None of the blocks to be tail-merged consist only of the common tail.
195 /// Create a block that does by splitting one.
Evan Cheng37bb6172010-06-22 01:18:16 +0000196 bool CreateCommonTailOnlyBlock(MachineBasicBlock *&PredBB,
Andrew Trick97a1d7c2013-06-24 01:55:01 +0000197 MachineBasicBlock *SuccBB,
Evan Cheng37bb6172010-06-22 01:18:16 +0000198 unsigned maxCommonTailLength,
199 unsigned &commonTailIndex);
Taewook Oh1b192332017-03-15 06:29:23 +0000200
201 /// Create merged DebugLocs of identical instructions across SameTails and
Matthias Braunc9056b82017-09-06 20:45:24 +0000202 /// assign it to the instruction in common tail; merge MMOs and undef flags.
203 void mergeCommonTails(unsigned commonTailIndex);
Evan Cheng3d2fce02009-09-04 07:47:40 +0000204
205 bool OptimizeBranches(MachineFunction &MF);
Taewook Oh1b192332017-03-15 06:29:23 +0000206
207 /// Analyze and optimize control flow related to the specified block. This
208 /// is never called on the entry block.
Evan Cheng3d2fce02009-09-04 07:47:40 +0000209 bool OptimizeBlock(MachineBasicBlock *MBB);
Taewook Oh1b192332017-03-15 06:29:23 +0000210
211 /// Remove the specified dead machine basic block from the function,
212 /// updating the CFG.
Evan Cheng3d2fce02009-09-04 07:47:40 +0000213 void RemoveDeadBlock(MachineBasicBlock *MBB);
Evan Chengcfdf3392011-05-12 00:56:58 +0000214
Taewook Oh1b192332017-03-15 06:29:23 +0000215 /// Hoist common instruction sequences at the start of basic blocks to their
216 /// common predecessor.
Evan Chengcfdf3392011-05-12 00:56:58 +0000217 bool HoistCommonCode(MachineFunction &MF);
Taewook Oh1b192332017-03-15 06:29:23 +0000218
219 /// If the successors of MBB has common instruction sequence at the start of
220 /// the function, move the instructions before MBB terminator if it's legal.
Evan Chengcfdf3392011-05-12 00:56:58 +0000221 bool HoistCommonCodeInSuccs(MachineBasicBlock *MBB);
Evan Cheng3d2fce02009-09-04 07:47:40 +0000222 };
Evan Cheng3d2fce02009-09-04 07:47:40 +0000223
Eugene Zelenko149178d2017-10-10 22:33:29 +0000224} // end namespace llvm
225
226#endif // LLVM_LIB_CODEGEN_BRANCHFOLDING_H