blob: accd0ab7317bb8adb5eadd420271e5527bea735d [file] [log] [blame]
Eugene Zelenko887aef72017-10-10 22:33:29 +00001//===- BranchFolding.h - Fold machine code branch instructions --*- C++ -*-===//
Evan Cheng030a0a02009-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 Kramer00e08fc2014-08-13 16:26:38 +000010#ifndef LLVM_LIB_CODEGEN_BRANCHFOLDING_H
11#define LLVM_LIB_CODEGEN_BRANCHFOLDING_H
Evan Cheng030a0a02009-09-04 07:47:40 +000012
Eugene Zelenko887aef72017-10-10 22:33:29 +000013#include "llvm/ADT/DenseMap.h"
Rafael Espindolaf924dea2011-06-14 15:31:54 +000014#include "llvm/ADT/SmallPtrSet.h"
Matthias Braun1efbc7f2016-07-12 18:44:33 +000015#include "llvm/CodeGen/LivePhysRegs.h"
Evan Cheng030a0a02009-09-04 07:47:40 +000016#include "llvm/CodeGen/MachineBasicBlock.h"
Akira Hatanaka70b56052014-08-07 19:30:13 +000017#include "llvm/Support/BlockFrequency.h"
Eugene Zelenko887aef72017-10-10 22:33:29 +000018#include "llvm/Support/Compiler.h"
19#include <cstdint>
Evan Cheng030a0a02009-09-04 07:47:40 +000020#include <vector>
21
22namespace llvm {
Eugene Zelenko887aef72017-10-10 22:33:29 +000023
24class BasicBlock;
25class MachineBlockFrequencyInfo;
26class MachineBranchProbabilityInfo;
27class MachineFunction;
28class MachineLoopInfo;
29class MachineModuleInfo;
30class MachineRegisterInfo;
31class raw_ostream;
32class TargetInstrInfo;
33class TargetRegisterInfo;
Evan Cheng030a0a02009-09-04 07:47:40 +000034
Benjamin Kramerb4537752015-07-01 14:47:39 +000035 class LLVM_LIBRARY_VISIBILITY BranchFolder {
Evan Cheng030a0a02009-09-04 07:47:40 +000036 public:
Haicheng Wuc4f22582016-06-09 15:24:29 +000037 class MBFIWrapper;
38
Kyle Buttb1ee91e2016-08-18 18:57:29 +000039 explicit BranchFolder(bool defaultEnableTailMerge,
40 bool CommonHoist,
Fangrui Song7d882862018-07-16 18:51:40 +000041 MBFIWrapper &FreqInfo,
42 const MachineBranchProbabilityInfo &ProbInfo,
Kyle Buttb1ee91e2016-08-18 18:57:29 +000043 // Min tail length to merge. Defaults to commandline
44 // flag. Ignored for optsize.
Fangrui Song7d882862018-07-16 18:51:40 +000045 unsigned MinTailLength = 0);
Evan Cheng030a0a02009-09-04 07:47:40 +000046
Taewook Oh39985522017-03-15 06:29:23 +000047 /// Perhaps branch folding, tail merging and other CFG optimizations on the
48 /// given function. Block placement changes the layout and may create new
49 /// tail merging opportunities.
Haicheng Wuc4f22582016-06-09 15:24:29 +000050 bool OptimizeFunction(MachineFunction &MF, const TargetInstrInfo *tii,
51 const TargetRegisterInfo *tri, MachineModuleInfo *mmi,
52 MachineLoopInfo *mli = nullptr,
53 bool AfterPlacement = false);
54
Evan Cheng030a0a02009-09-04 07:47:40 +000055 private:
Dan Gohmanffe644e2009-11-11 21:57:02 +000056 class MergePotentialsElt {
57 unsigned Hash;
58 MachineBasicBlock *Block;
Eugene Zelenko887aef72017-10-10 22:33:29 +000059
Dan Gohmanffe644e2009-11-11 21:57:02 +000060 public:
61 MergePotentialsElt(unsigned h, MachineBasicBlock *b)
62 : Hash(h), Block(b) {}
63
64 unsigned getHash() const { return Hash; }
65 MachineBasicBlock *getBlock() const { return Block; }
66
67 void setBlock(MachineBasicBlock *MBB) {
68 Block = MBB;
69 }
70
71 bool operator<(const MergePotentialsElt &) const;
72 };
Eugene Zelenko887aef72017-10-10 22:33:29 +000073
74 using MPIterator = std::vector<MergePotentialsElt>::iterator;
75
Evan Cheng030a0a02009-09-04 07:47:40 +000076 std::vector<MergePotentialsElt> MergePotentials;
Rafael Espindolaf924dea2011-06-14 15:31:54 +000077 SmallPtrSet<const MachineBasicBlock*, 2> TriedMerging;
Heejin Ahn2522e342018-06-01 00:03:21 +000078 DenseMap<const MachineBasicBlock *, int> EHScopeMembership;
Evan Cheng030a0a02009-09-04 07:47:40 +000079
Dan Gohmanffe644e2009-11-11 21:57:02 +000080 class SameTailElt {
81 MPIterator MPIter;
82 MachineBasicBlock::iterator TailStartPos;
Eugene Zelenko887aef72017-10-10 22:33:29 +000083
Dan Gohmanffe644e2009-11-11 21:57:02 +000084 public:
85 SameTailElt(MPIterator mp, MachineBasicBlock::iterator tsp)
86 : MPIter(mp), TailStartPos(tsp) {}
87
88 MPIterator getMPIter() const {
89 return MPIter;
90 }
Eugene Zelenko887aef72017-10-10 22:33:29 +000091
Dan Gohmanffe644e2009-11-11 21:57:02 +000092 MergePotentialsElt &getMergePotentialsElt() const {
93 return *getMPIter();
94 }
Eugene Zelenko887aef72017-10-10 22:33:29 +000095
Dan Gohmanffe644e2009-11-11 21:57:02 +000096 MachineBasicBlock::iterator getTailStartPos() const {
97 return TailStartPos;
98 }
Eugene Zelenko887aef72017-10-10 22:33:29 +000099
Dan Gohmanffe644e2009-11-11 21:57:02 +0000100 unsigned getHash() const {
101 return getMergePotentialsElt().getHash();
102 }
Eugene Zelenko887aef72017-10-10 22:33:29 +0000103
Dan Gohmanffe644e2009-11-11 21:57:02 +0000104 MachineBasicBlock *getBlock() const {
105 return getMergePotentialsElt().getBlock();
106 }
Eugene Zelenko887aef72017-10-10 22:33:29 +0000107
Dan Gohmanffe644e2009-11-11 21:57:02 +0000108 bool tailIsWholeBlock() const {
109 return TailStartPos == getBlock()->begin();
110 }
111
112 void setBlock(MachineBasicBlock *MBB) {
113 getMergePotentialsElt().setBlock(MBB);
114 }
Eugene Zelenko887aef72017-10-10 22:33:29 +0000115
Dan Gohmanffe644e2009-11-11 21:57:02 +0000116 void setTailStartPos(MachineBasicBlock::iterator Pos) {
117 TailStartPos = Pos;
118 }
119 };
Evan Cheng030a0a02009-09-04 07:47:40 +0000120 std::vector<SameTailElt> SameTails;
121
Haicheng Wuc4f22582016-06-09 15:24:29 +0000122 bool AfterBlockPlacement;
Evan Cheng030a0a02009-09-04 07:47:40 +0000123 bool EnableTailMerge;
Evan Chengcbc988b2011-05-12 00:56:58 +0000124 bool EnableHoistCommonCode;
Matthias Braun1efbc7f2016-07-12 18:44:33 +0000125 bool UpdateLiveIns;
Kyle Buttb1ee91e2016-08-18 18:57:29 +0000126 unsigned MinCommonTailLength;
Evan Cheng030a0a02009-09-04 07:47:40 +0000127 const TargetInstrInfo *TII;
Matthias Braunb0e29ac2017-05-26 06:32:31 +0000128 const MachineRegisterInfo *MRI;
Evan Cheng030a0a02009-09-04 07:47:40 +0000129 const TargetRegisterInfo *TRI;
130 MachineModuleInfo *MMI;
Haicheng Wuc4f22582016-06-09 15:24:29 +0000131 MachineLoopInfo *MLI;
Matthias Braun1efbc7f2016-07-12 18:44:33 +0000132 LivePhysRegs LiveRegs;
Evan Cheng030a0a02009-09-04 07:47:40 +0000133
Haicheng Wuc4f22582016-06-09 15:24:29 +0000134 public:
Adrian Prantl26b584c2018-05-01 15:54:18 +0000135 /// This class keeps track of branch frequencies of newly created
Akira Hatanaka70b56052014-08-07 19:30:13 +0000136 /// blocks and tail-merged blocks.
137 class MBFIWrapper {
138 public:
139 MBFIWrapper(const MachineBlockFrequencyInfo &I) : MBFI(I) {}
Eugene Zelenko887aef72017-10-10 22:33:29 +0000140
Akira Hatanaka70b56052014-08-07 19:30:13 +0000141 BlockFrequency getBlockFreq(const MachineBasicBlock *MBB) const;
142 void setBlockFreq(const MachineBasicBlock *MBB, BlockFrequency F);
Haicheng Wuc4f22582016-06-09 15:24:29 +0000143 raw_ostream &printBlockFreq(raw_ostream &OS,
144 const MachineBasicBlock *MBB) const;
145 raw_ostream &printBlockFreq(raw_ostream &OS,
146 const BlockFrequency Freq) const;
Xinliang David Li210c6902017-02-15 19:21:04 +0000147 void view(const Twine &Name, bool isSimple = true);
Kyle Butt5818a512017-01-31 23:48:32 +0000148 uint64_t getEntryFreq() const;
Akira Hatanaka70b56052014-08-07 19:30:13 +0000149
150 private:
151 const MachineBlockFrequencyInfo &MBFI;
152 DenseMap<const MachineBasicBlock *, BlockFrequency> MergedBBFreq;
153 };
154
Haicheng Wuc4f22582016-06-09 15:24:29 +0000155 private:
156 MBFIWrapper &MBBFreqInfo;
Akira Hatanaka70b56052014-08-07 19:30:13 +0000157 const MachineBranchProbabilityInfo &MBPI;
158
Evan Cheng030a0a02009-09-04 07:47:40 +0000159 bool TailMergeBlocks(MachineFunction &MF);
Dan Gohman412a3b92009-11-11 19:49:34 +0000160 bool TryTailMergeBlocks(MachineBasicBlock* SuccBB,
Kyle Buttb1ee91e2016-08-18 18:57:29 +0000161 MachineBasicBlock* PredBB,
162 unsigned MinCommonTailLength);
Akira Hatanaka70b56052014-08-07 19:30:13 +0000163 void setCommonTailEdgeWeights(MachineBasicBlock &TailMBB);
Taewook Oh39985522017-03-15 06:29:23 +0000164
165 /// Delete the instruction OldInst and everything after it, replacing it
166 /// with an unconditional branch to NewDest.
Matthias Braun109c6e02017-09-06 20:45:24 +0000167 void replaceTailWithBranchTo(MachineBasicBlock::iterator OldInst,
168 MachineBasicBlock &NewDest);
Taewook Oh39985522017-03-15 06:29:23 +0000169
170 /// Given a machine basic block and an iterator into it, split the MBB so
171 /// that the part before the iterator falls into the part starting at the
172 /// iterator. This returns the new MBB.
Evan Cheng030a0a02009-09-04 07:47:40 +0000173 MachineBasicBlock *SplitMBBAt(MachineBasicBlock &CurMBB,
Andrew Trickf7b5e012013-06-24 01:55:01 +0000174 MachineBasicBlock::iterator BBI1,
175 const BasicBlock *BB);
Taewook Oh39985522017-03-15 06:29:23 +0000176
177 /// Look through all the blocks in MergePotentials that have hash CurHash
178 /// (guaranteed to match the last element). Build the vector SameTails of
179 /// all those that have the (same) largest number of instructions in common
180 /// of any pair of these blocks. SameTails entries contain an iterator into
181 /// MergePotentials (from which the MachineBasicBlock can be found) and a
182 /// MachineBasicBlock::iterator into that MBB indicating the instruction
183 /// where the matching code sequence begins. Order of elements in SameTails
184 /// is the reverse of the order in which those blocks appear in
185 /// MergePotentials (where they are not necessarily consecutive).
Dan Gohman412a3b92009-11-11 19:49:34 +0000186 unsigned ComputeSameTails(unsigned CurHash, unsigned minCommonTailLength,
187 MachineBasicBlock *SuccBB,
188 MachineBasicBlock *PredBB);
Taewook Oh39985522017-03-15 06:29:23 +0000189
190 /// Remove all blocks with hash CurHash from MergePotentials, restoring
191 /// branches at ends of blocks as appropriate.
Evan Cheng030a0a02009-09-04 07:47:40 +0000192 void RemoveBlocksWithHash(unsigned CurHash, MachineBasicBlock* SuccBB,
193 MachineBasicBlock* PredBB);
Taewook Oh39985522017-03-15 06:29:23 +0000194
195 /// None of the blocks to be tail-merged consist only of the common tail.
196 /// Create a block that does by splitting one.
Evan Cheng4d54e5b2010-06-22 01:18:16 +0000197 bool CreateCommonTailOnlyBlock(MachineBasicBlock *&PredBB,
Andrew Trickf7b5e012013-06-24 01:55:01 +0000198 MachineBasicBlock *SuccBB,
Evan Cheng4d54e5b2010-06-22 01:18:16 +0000199 unsigned maxCommonTailLength,
200 unsigned &commonTailIndex);
Taewook Oh39985522017-03-15 06:29:23 +0000201
202 /// Create merged DebugLocs of identical instructions across SameTails and
Matthias Braun109c6e02017-09-06 20:45:24 +0000203 /// assign it to the instruction in common tail; merge MMOs and undef flags.
204 void mergeCommonTails(unsigned commonTailIndex);
Evan Cheng030a0a02009-09-04 07:47:40 +0000205
206 bool OptimizeBranches(MachineFunction &MF);
Taewook Oh39985522017-03-15 06:29:23 +0000207
208 /// Analyze and optimize control flow related to the specified block. This
209 /// is never called on the entry block.
Evan Cheng030a0a02009-09-04 07:47:40 +0000210 bool OptimizeBlock(MachineBasicBlock *MBB);
Taewook Oh39985522017-03-15 06:29:23 +0000211
212 /// Remove the specified dead machine basic block from the function,
213 /// updating the CFG.
Evan Cheng030a0a02009-09-04 07:47:40 +0000214 void RemoveDeadBlock(MachineBasicBlock *MBB);
Evan Chengcbc988b2011-05-12 00:56:58 +0000215
Taewook Oh39985522017-03-15 06:29:23 +0000216 /// Hoist common instruction sequences at the start of basic blocks to their
217 /// common predecessor.
Evan Chengcbc988b2011-05-12 00:56:58 +0000218 bool HoistCommonCode(MachineFunction &MF);
Taewook Oh39985522017-03-15 06:29:23 +0000219
220 /// If the successors of MBB has common instruction sequence at the start of
221 /// the function, move the instructions before MBB terminator if it's legal.
Evan Chengcbc988b2011-05-12 00:56:58 +0000222 bool HoistCommonCodeInSuccs(MachineBasicBlock *MBB);
Evan Cheng030a0a02009-09-04 07:47:40 +0000223 };
Evan Cheng030a0a02009-09-04 07:47:40 +0000224
Eugene Zelenko887aef72017-10-10 22:33:29 +0000225} // end namespace llvm
226
227#endif // LLVM_LIB_CODEGEN_BRANCHFOLDING_H