Michael Gottesman | 1649a87 | 2013-08-12 20:49:27 +0000 | [diff] [blame] | 1 | //===-- BranchFolding.h - Fold machine code branch instructions -*- C++ -*-===// |
Evan Cheng | 3d2fce0 | 2009-09-04 07:47:40 +0000 | [diff] [blame] | 2 | // |
| 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 Kramer | a7c40ef | 2014-08-13 16:26:38 +0000 | [diff] [blame] | 10 | #ifndef LLVM_LIB_CODEGEN_BRANCHFOLDING_H |
| 11 | #define LLVM_LIB_CODEGEN_BRANCHFOLDING_H |
Evan Cheng | 3d2fce0 | 2009-09-04 07:47:40 +0000 | [diff] [blame] | 12 | |
Rafael Espindola | 3aeaf9e | 2011-06-14 15:31:54 +0000 | [diff] [blame] | 13 | #include "llvm/ADT/SmallPtrSet.h" |
Matthias Braun | aeab09f | 2016-07-12 18:44:33 +0000 | [diff] [blame] | 14 | #include "llvm/CodeGen/LivePhysRegs.h" |
Evan Cheng | 3d2fce0 | 2009-09-04 07:47:40 +0000 | [diff] [blame] | 15 | #include "llvm/CodeGen/MachineBasicBlock.h" |
Akira Hatanaka | bbd33f6 | 2014-08-07 19:30:13 +0000 | [diff] [blame] | 16 | #include "llvm/Support/BlockFrequency.h" |
Evan Cheng | 3d2fce0 | 2009-09-04 07:47:40 +0000 | [diff] [blame] | 17 | #include <vector> |
| 18 | |
| 19 | namespace llvm { |
Akira Hatanaka | bbd33f6 | 2014-08-07 19:30:13 +0000 | [diff] [blame] | 20 | class MachineBlockFrequencyInfo; |
| 21 | class MachineBranchProbabilityInfo; |
Evan Cheng | 3d2fce0 | 2009-09-04 07:47:40 +0000 | [diff] [blame] | 22 | class MachineFunction; |
| 23 | class MachineModuleInfo; |
Haicheng Wu | 5b458cc | 2016-06-09 15:24:29 +0000 | [diff] [blame] | 24 | class MachineLoopInfo; |
Evan Cheng | 3d2fce0 | 2009-09-04 07:47:40 +0000 | [diff] [blame] | 25 | class TargetInstrInfo; |
| 26 | class TargetRegisterInfo; |
| 27 | |
Benjamin Kramer | f4c2025 | 2015-07-01 14:47:39 +0000 | [diff] [blame] | 28 | class LLVM_LIBRARY_VISIBILITY BranchFolder { |
Evan Cheng | 3d2fce0 | 2009-09-04 07:47:40 +0000 | [diff] [blame] | 29 | public: |
Haicheng Wu | 5b458cc | 2016-06-09 15:24:29 +0000 | [diff] [blame] | 30 | class MBFIWrapper; |
| 31 | |
Kyle Butt | 64e4281 | 2016-08-18 18:57:29 +0000 | [diff] [blame] | 32 | explicit BranchFolder(bool defaultEnableTailMerge, |
| 33 | bool CommonHoist, |
Haicheng Wu | 5b458cc | 2016-06-09 15:24:29 +0000 | [diff] [blame] | 34 | MBFIWrapper &MBFI, |
Kyle Butt | 64e4281 | 2016-08-18 18:57:29 +0000 | [diff] [blame] | 35 | const MachineBranchProbabilityInfo &MBPI, |
| 36 | // Min tail length to merge. Defaults to commandline |
| 37 | // flag. Ignored for optsize. |
| 38 | unsigned MinCommonTailLength = 0); |
Evan Cheng | 3d2fce0 | 2009-09-04 07:47:40 +0000 | [diff] [blame] | 39 | |
Taewook Oh | 1b19233 | 2017-03-15 06:29:23 +0000 | [diff] [blame] | 40 | /// 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 Wu | 5b458cc | 2016-06-09 15:24:29 +0000 | [diff] [blame] | 43 | bool OptimizeFunction(MachineFunction &MF, const TargetInstrInfo *tii, |
| 44 | const TargetRegisterInfo *tri, MachineModuleInfo *mmi, |
| 45 | MachineLoopInfo *mli = nullptr, |
| 46 | bool AfterPlacement = false); |
| 47 | |
Evan Cheng | 3d2fce0 | 2009-09-04 07:47:40 +0000 | [diff] [blame] | 48 | private: |
Dan Gohman | 02b1554 | 2009-11-11 21:57:02 +0000 | [diff] [blame] | 49 | 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 Cheng | 3d2fce0 | 2009-09-04 07:47:40 +0000 | [diff] [blame] | 65 | typedef std::vector<MergePotentialsElt>::iterator MPIterator; |
| 66 | std::vector<MergePotentialsElt> MergePotentials; |
Rafael Espindola | 3aeaf9e | 2011-06-14 15:31:54 +0000 | [diff] [blame] | 67 | SmallPtrSet<const MachineBasicBlock*, 2> TriedMerging; |
David Majnemer | 1619355 | 2015-10-04 02:22:52 +0000 | [diff] [blame] | 68 | DenseMap<const MachineBasicBlock *, int> FuncletMembership; |
Evan Cheng | 3d2fce0 | 2009-09-04 07:47:40 +0000 | [diff] [blame] | 69 | |
Dan Gohman | 02b1554 | 2009-11-11 21:57:02 +0000 | [diff] [blame] | 70 | 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 Cheng | 3d2fce0 | 2009-09-04 07:47:40 +0000 | [diff] [blame] | 103 | std::vector<SameTailElt> SameTails; |
| 104 | |
Haicheng Wu | 5b458cc | 2016-06-09 15:24:29 +0000 | [diff] [blame] | 105 | bool AfterBlockPlacement; |
Evan Cheng | 3d2fce0 | 2009-09-04 07:47:40 +0000 | [diff] [blame] | 106 | bool EnableTailMerge; |
Evan Cheng | cfdf339 | 2011-05-12 00:56:58 +0000 | [diff] [blame] | 107 | bool EnableHoistCommonCode; |
Matthias Braun | aeab09f | 2016-07-12 18:44:33 +0000 | [diff] [blame] | 108 | bool UpdateLiveIns; |
Kyle Butt | 64e4281 | 2016-08-18 18:57:29 +0000 | [diff] [blame] | 109 | unsigned MinCommonTailLength; |
Evan Cheng | 3d2fce0 | 2009-09-04 07:47:40 +0000 | [diff] [blame] | 110 | const TargetInstrInfo *TII; |
Matthias Braun | e51c435 | 2017-05-26 06:32:31 +0000 | [diff] [blame] | 111 | const MachineRegisterInfo *MRI; |
Evan Cheng | 3d2fce0 | 2009-09-04 07:47:40 +0000 | [diff] [blame] | 112 | const TargetRegisterInfo *TRI; |
| 113 | MachineModuleInfo *MMI; |
Haicheng Wu | 5b458cc | 2016-06-09 15:24:29 +0000 | [diff] [blame] | 114 | MachineLoopInfo *MLI; |
Matthias Braun | aeab09f | 2016-07-12 18:44:33 +0000 | [diff] [blame] | 115 | LivePhysRegs LiveRegs; |
Evan Cheng | 3d2fce0 | 2009-09-04 07:47:40 +0000 | [diff] [blame] | 116 | |
Haicheng Wu | 5b458cc | 2016-06-09 15:24:29 +0000 | [diff] [blame] | 117 | public: |
Akira Hatanaka | bbd33f6 | 2014-08-07 19:30:13 +0000 | [diff] [blame] | 118 | /// \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 Wu | 5b458cc | 2016-06-09 15:24:29 +0000 | [diff] [blame] | 125 | 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 Li | 538d666 | 2017-02-15 19:21:04 +0000 | [diff] [blame] | 129 | void view(const Twine &Name, bool isSimple = true); |
Kyle Butt | b15c066 | 2017-01-31 23:48:32 +0000 | [diff] [blame] | 130 | uint64_t getEntryFreq() const; |
Akira Hatanaka | bbd33f6 | 2014-08-07 19:30:13 +0000 | [diff] [blame] | 131 | |
| 132 | private: |
| 133 | const MachineBlockFrequencyInfo &MBFI; |
| 134 | DenseMap<const MachineBasicBlock *, BlockFrequency> MergedBBFreq; |
| 135 | }; |
| 136 | |
Haicheng Wu | 5b458cc | 2016-06-09 15:24:29 +0000 | [diff] [blame] | 137 | private: |
| 138 | MBFIWrapper &MBBFreqInfo; |
Akira Hatanaka | bbd33f6 | 2014-08-07 19:30:13 +0000 | [diff] [blame] | 139 | const MachineBranchProbabilityInfo &MBPI; |
| 140 | |
Evan Cheng | 3d2fce0 | 2009-09-04 07:47:40 +0000 | [diff] [blame] | 141 | bool TailMergeBlocks(MachineFunction &MF); |
Dan Gohman | 34eeb4e | 2009-11-11 19:49:34 +0000 | [diff] [blame] | 142 | bool TryTailMergeBlocks(MachineBasicBlock* SuccBB, |
Kyle Butt | 64e4281 | 2016-08-18 18:57:29 +0000 | [diff] [blame] | 143 | MachineBasicBlock* PredBB, |
| 144 | unsigned MinCommonTailLength); |
Akira Hatanaka | bbd33f6 | 2014-08-07 19:30:13 +0000 | [diff] [blame] | 145 | void setCommonTailEdgeWeights(MachineBasicBlock &TailMBB); |
Taewook Oh | 1b19233 | 2017-03-15 06:29:23 +0000 | [diff] [blame] | 146 | |
| 147 | /// Delete the instruction OldInst and everything after it, replacing it |
| 148 | /// with an unconditional branch to NewDest. |
Matthias Braun | c9056b8 | 2017-09-06 20:45:24 +0000 | [diff] [blame^] | 149 | void replaceTailWithBranchTo(MachineBasicBlock::iterator OldInst, |
| 150 | MachineBasicBlock &NewDest); |
Taewook Oh | 1b19233 | 2017-03-15 06:29:23 +0000 | [diff] [blame] | 151 | |
| 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 Cheng | 3d2fce0 | 2009-09-04 07:47:40 +0000 | [diff] [blame] | 155 | MachineBasicBlock *SplitMBBAt(MachineBasicBlock &CurMBB, |
Andrew Trick | 97a1d7c | 2013-06-24 01:55:01 +0000 | [diff] [blame] | 156 | MachineBasicBlock::iterator BBI1, |
| 157 | const BasicBlock *BB); |
Taewook Oh | 1b19233 | 2017-03-15 06:29:23 +0000 | [diff] [blame] | 158 | |
| 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 Gohman | 34eeb4e | 2009-11-11 19:49:34 +0000 | [diff] [blame] | 168 | unsigned ComputeSameTails(unsigned CurHash, unsigned minCommonTailLength, |
| 169 | MachineBasicBlock *SuccBB, |
| 170 | MachineBasicBlock *PredBB); |
Taewook Oh | 1b19233 | 2017-03-15 06:29:23 +0000 | [diff] [blame] | 171 | |
| 172 | /// Remove all blocks with hash CurHash from MergePotentials, restoring |
| 173 | /// branches at ends of blocks as appropriate. |
Evan Cheng | 3d2fce0 | 2009-09-04 07:47:40 +0000 | [diff] [blame] | 174 | void RemoveBlocksWithHash(unsigned CurHash, MachineBasicBlock* SuccBB, |
| 175 | MachineBasicBlock* PredBB); |
Taewook Oh | 1b19233 | 2017-03-15 06:29:23 +0000 | [diff] [blame] | 176 | |
| 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 Cheng | 37bb617 | 2010-06-22 01:18:16 +0000 | [diff] [blame] | 179 | bool CreateCommonTailOnlyBlock(MachineBasicBlock *&PredBB, |
Andrew Trick | 97a1d7c | 2013-06-24 01:55:01 +0000 | [diff] [blame] | 180 | MachineBasicBlock *SuccBB, |
Evan Cheng | 37bb617 | 2010-06-22 01:18:16 +0000 | [diff] [blame] | 181 | unsigned maxCommonTailLength, |
| 182 | unsigned &commonTailIndex); |
Taewook Oh | 1b19233 | 2017-03-15 06:29:23 +0000 | [diff] [blame] | 183 | |
| 184 | /// Create merged DebugLocs of identical instructions across SameTails and |
Matthias Braun | c9056b8 | 2017-09-06 20:45:24 +0000 | [diff] [blame^] | 185 | /// assign it to the instruction in common tail; merge MMOs and undef flags. |
| 186 | void mergeCommonTails(unsigned commonTailIndex); |
Evan Cheng | 3d2fce0 | 2009-09-04 07:47:40 +0000 | [diff] [blame] | 187 | |
| 188 | bool OptimizeBranches(MachineFunction &MF); |
Taewook Oh | 1b19233 | 2017-03-15 06:29:23 +0000 | [diff] [blame] | 189 | |
| 190 | /// Analyze and optimize control flow related to the specified block. This |
| 191 | /// is never called on the entry block. |
Evan Cheng | 3d2fce0 | 2009-09-04 07:47:40 +0000 | [diff] [blame] | 192 | bool OptimizeBlock(MachineBasicBlock *MBB); |
Taewook Oh | 1b19233 | 2017-03-15 06:29:23 +0000 | [diff] [blame] | 193 | |
| 194 | /// Remove the specified dead machine basic block from the function, |
| 195 | /// updating the CFG. |
Evan Cheng | 3d2fce0 | 2009-09-04 07:47:40 +0000 | [diff] [blame] | 196 | void RemoveDeadBlock(MachineBasicBlock *MBB); |
Evan Cheng | cfdf339 | 2011-05-12 00:56:58 +0000 | [diff] [blame] | 197 | |
Taewook Oh | 1b19233 | 2017-03-15 06:29:23 +0000 | [diff] [blame] | 198 | /// Hoist common instruction sequences at the start of basic blocks to their |
| 199 | /// common predecessor. |
Evan Cheng | cfdf339 | 2011-05-12 00:56:58 +0000 | [diff] [blame] | 200 | bool HoistCommonCode(MachineFunction &MF); |
Taewook Oh | 1b19233 | 2017-03-15 06:29:23 +0000 | [diff] [blame] | 201 | |
| 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 Cheng | cfdf339 | 2011-05-12 00:56:58 +0000 | [diff] [blame] | 204 | bool HoistCommonCodeInSuccs(MachineBasicBlock *MBB); |
Evan Cheng | 3d2fce0 | 2009-09-04 07:47:40 +0000 | [diff] [blame] | 205 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 206 | } |
Evan Cheng | 3d2fce0 | 2009-09-04 07:47:40 +0000 | [diff] [blame] | 207 | |
| 208 | #endif /* LLVM_CODEGEN_BRANCHFOLDING_HPP */ |