blob: f6efcb718c91992a263d5e631e7557a74f253eb9 [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;
Matthias Braune51c4352017-05-26 06:32:31 +0000111 const MachineRegisterInfo *MRI;
Evan Cheng3d2fce02009-09-04 07:47:40 +0000112 const TargetRegisterInfo *TRI;
113 MachineModuleInfo *MMI;
Haicheng Wu5b458cc2016-06-09 15:24:29 +0000114 MachineLoopInfo *MLI;
Matthias Braunaeab09f2016-07-12 18:44:33 +0000115 LivePhysRegs LiveRegs;
Evan Cheng3d2fce02009-09-04 07:47:40 +0000116
Haicheng Wu5b458cc2016-06-09 15:24:29 +0000117 public:
Akira Hatanakabbd33f62014-08-07 19:30:13 +0000118 /// \brief This class keeps track of branch frequencies of newly created
119 /// blocks and tail-merged blocks.
120 class MBFIWrapper {
121 public:
122 MBFIWrapper(const MachineBlockFrequencyInfo &I) : MBFI(I) {}
123 BlockFrequency getBlockFreq(const MachineBasicBlock *MBB) const;
124 void setBlockFreq(const MachineBasicBlock *MBB, BlockFrequency F);
Haicheng Wu5b458cc2016-06-09 15:24:29 +0000125 raw_ostream &printBlockFreq(raw_ostream &OS,
126 const MachineBasicBlock *MBB) const;
127 raw_ostream &printBlockFreq(raw_ostream &OS,
128 const BlockFrequency Freq) const;
Xinliang David Li538d6662017-02-15 19:21:04 +0000129 void view(const Twine &Name, bool isSimple = true);
Kyle Buttb15c0662017-01-31 23:48:32 +0000130 uint64_t getEntryFreq() const;
Akira Hatanakabbd33f62014-08-07 19:30:13 +0000131
132 private:
133 const MachineBlockFrequencyInfo &MBFI;
134 DenseMap<const MachineBasicBlock *, BlockFrequency> MergedBBFreq;
135 };
136
Haicheng Wu5b458cc2016-06-09 15:24:29 +0000137 private:
138 MBFIWrapper &MBBFreqInfo;
Akira Hatanakabbd33f62014-08-07 19:30:13 +0000139 const MachineBranchProbabilityInfo &MBPI;
140
Evan Cheng3d2fce02009-09-04 07:47:40 +0000141 bool TailMergeBlocks(MachineFunction &MF);
Dan Gohman34eeb4e2009-11-11 19:49:34 +0000142 bool TryTailMergeBlocks(MachineBasicBlock* SuccBB,
Kyle Butt64e42812016-08-18 18:57:29 +0000143 MachineBasicBlock* PredBB,
144 unsigned MinCommonTailLength);
Akira Hatanakabbd33f62014-08-07 19:30:13 +0000145 void setCommonTailEdgeWeights(MachineBasicBlock &TailMBB);
Taewook Oh1b192332017-03-15 06:29:23 +0000146
147 /// Delete the instruction OldInst and everything after it, replacing it
148 /// with an unconditional branch to NewDest.
Matthias Braunc9056b82017-09-06 20:45:24 +0000149 void replaceTailWithBranchTo(MachineBasicBlock::iterator OldInst,
150 MachineBasicBlock &NewDest);
Taewook Oh1b192332017-03-15 06:29:23 +0000151
152 /// Given a machine basic block and an iterator into it, split the MBB so
153 /// that the part before the iterator falls into the part starting at the
154 /// iterator. This returns the new MBB.
Evan Cheng3d2fce02009-09-04 07:47:40 +0000155 MachineBasicBlock *SplitMBBAt(MachineBasicBlock &CurMBB,
Andrew Trick97a1d7c2013-06-24 01:55:01 +0000156 MachineBasicBlock::iterator BBI1,
157 const BasicBlock *BB);
Taewook Oh1b192332017-03-15 06:29:23 +0000158
159 /// Look through all the blocks in MergePotentials that have hash CurHash
160 /// (guaranteed to match the last element). Build the vector SameTails of
161 /// all those that have the (same) largest number of instructions in common
162 /// of any pair of these blocks. SameTails entries contain an iterator into
163 /// MergePotentials (from which the MachineBasicBlock can be found) and a
164 /// MachineBasicBlock::iterator into that MBB indicating the instruction
165 /// where the matching code sequence begins. Order of elements in SameTails
166 /// is the reverse of the order in which those blocks appear in
167 /// MergePotentials (where they are not necessarily consecutive).
Dan Gohman34eeb4e2009-11-11 19:49:34 +0000168 unsigned ComputeSameTails(unsigned CurHash, unsigned minCommonTailLength,
169 MachineBasicBlock *SuccBB,
170 MachineBasicBlock *PredBB);
Taewook Oh1b192332017-03-15 06:29:23 +0000171
172 /// Remove all blocks with hash CurHash from MergePotentials, restoring
173 /// branches at ends of blocks as appropriate.
Evan Cheng3d2fce02009-09-04 07:47:40 +0000174 void RemoveBlocksWithHash(unsigned CurHash, MachineBasicBlock* SuccBB,
175 MachineBasicBlock* PredBB);
Taewook Oh1b192332017-03-15 06:29:23 +0000176
177 /// None of the blocks to be tail-merged consist only of the common tail.
178 /// Create a block that does by splitting one.
Evan Cheng37bb6172010-06-22 01:18:16 +0000179 bool CreateCommonTailOnlyBlock(MachineBasicBlock *&PredBB,
Andrew Trick97a1d7c2013-06-24 01:55:01 +0000180 MachineBasicBlock *SuccBB,
Evan Cheng37bb6172010-06-22 01:18:16 +0000181 unsigned maxCommonTailLength,
182 unsigned &commonTailIndex);
Taewook Oh1b192332017-03-15 06:29:23 +0000183
184 /// Create merged DebugLocs of identical instructions across SameTails and
Matthias Braunc9056b82017-09-06 20:45:24 +0000185 /// assign it to the instruction in common tail; merge MMOs and undef flags.
186 void mergeCommonTails(unsigned commonTailIndex);
Evan Cheng3d2fce02009-09-04 07:47:40 +0000187
188 bool OptimizeBranches(MachineFunction &MF);
Taewook Oh1b192332017-03-15 06:29:23 +0000189
190 /// Analyze and optimize control flow related to the specified block. This
191 /// is never called on the entry block.
Evan Cheng3d2fce02009-09-04 07:47:40 +0000192 bool OptimizeBlock(MachineBasicBlock *MBB);
Taewook Oh1b192332017-03-15 06:29:23 +0000193
194 /// Remove the specified dead machine basic block from the function,
195 /// updating the CFG.
Evan Cheng3d2fce02009-09-04 07:47:40 +0000196 void RemoveDeadBlock(MachineBasicBlock *MBB);
Evan Chengcfdf3392011-05-12 00:56:58 +0000197
Taewook Oh1b192332017-03-15 06:29:23 +0000198 /// Hoist common instruction sequences at the start of basic blocks to their
199 /// common predecessor.
Evan Chengcfdf3392011-05-12 00:56:58 +0000200 bool HoistCommonCode(MachineFunction &MF);
Taewook Oh1b192332017-03-15 06:29:23 +0000201
202 /// If the successors of MBB has common instruction sequence at the start of
203 /// the function, move the instructions before MBB terminator if it's legal.
Evan Chengcfdf3392011-05-12 00:56:58 +0000204 bool HoistCommonCodeInSuccs(MachineBasicBlock *MBB);
Evan Cheng3d2fce02009-09-04 07:47:40 +0000205 };
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000206}
Evan Cheng3d2fce02009-09-04 07:47:40 +0000207
208#endif /* LLVM_CODEGEN_BRANCHFOLDING_HPP */