Eugene Zelenko | 887aef7 | 2017-10-10 22:33:29 +0000 | [diff] [blame] | 1 | //===- BranchFolding.h - Fold machine code branch instructions --*- C++ -*-===// |
Evan Cheng | 030a0a0 | 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 | 00e08fc | 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 | 030a0a0 | 2009-09-04 07:47:40 +0000 | [diff] [blame] | 12 | |
Eugene Zelenko | 887aef7 | 2017-10-10 22:33:29 +0000 | [diff] [blame] | 13 | #include "llvm/ADT/DenseMap.h" |
Rafael Espindola | f924dea | 2011-06-14 15:31:54 +0000 | [diff] [blame] | 14 | #include "llvm/ADT/SmallPtrSet.h" |
Matthias Braun | 1efbc7f | 2016-07-12 18:44:33 +0000 | [diff] [blame] | 15 | #include "llvm/CodeGen/LivePhysRegs.h" |
Evan Cheng | 030a0a0 | 2009-09-04 07:47:40 +0000 | [diff] [blame] | 16 | #include "llvm/CodeGen/MachineBasicBlock.h" |
Akira Hatanaka | 70b5605 | 2014-08-07 19:30:13 +0000 | [diff] [blame] | 17 | #include "llvm/Support/BlockFrequency.h" |
Eugene Zelenko | 887aef7 | 2017-10-10 22:33:29 +0000 | [diff] [blame] | 18 | #include "llvm/Support/Compiler.h" |
| 19 | #include <cstdint> |
Evan Cheng | 030a0a0 | 2009-09-04 07:47:40 +0000 | [diff] [blame] | 20 | #include <vector> |
| 21 | |
| 22 | namespace llvm { |
Eugene Zelenko | 887aef7 | 2017-10-10 22:33:29 +0000 | [diff] [blame] | 23 | |
| 24 | class BasicBlock; |
| 25 | class MachineBlockFrequencyInfo; |
| 26 | class MachineBranchProbabilityInfo; |
| 27 | class MachineFunction; |
| 28 | class MachineLoopInfo; |
| 29 | class MachineModuleInfo; |
| 30 | class MachineRegisterInfo; |
| 31 | class raw_ostream; |
| 32 | class TargetInstrInfo; |
| 33 | class TargetRegisterInfo; |
Evan Cheng | 030a0a0 | 2009-09-04 07:47:40 +0000 | [diff] [blame] | 34 | |
Benjamin Kramer | b453775 | 2015-07-01 14:47:39 +0000 | [diff] [blame] | 35 | class LLVM_LIBRARY_VISIBILITY BranchFolder { |
Evan Cheng | 030a0a0 | 2009-09-04 07:47:40 +0000 | [diff] [blame] | 36 | public: |
Haicheng Wu | c4f2258 | 2016-06-09 15:24:29 +0000 | [diff] [blame] | 37 | class MBFIWrapper; |
| 38 | |
Kyle Butt | b1ee91e | 2016-08-18 18:57:29 +0000 | [diff] [blame] | 39 | explicit BranchFolder(bool defaultEnableTailMerge, |
| 40 | bool CommonHoist, |
Fangrui Song | 7d88286 | 2018-07-16 18:51:40 +0000 | [diff] [blame] | 41 | MBFIWrapper &FreqInfo, |
| 42 | const MachineBranchProbabilityInfo &ProbInfo, |
Kyle Butt | b1ee91e | 2016-08-18 18:57:29 +0000 | [diff] [blame] | 43 | // Min tail length to merge. Defaults to commandline |
| 44 | // flag. Ignored for optsize. |
Fangrui Song | 7d88286 | 2018-07-16 18:51:40 +0000 | [diff] [blame] | 45 | unsigned MinTailLength = 0); |
Evan Cheng | 030a0a0 | 2009-09-04 07:47:40 +0000 | [diff] [blame] | 46 | |
Taewook Oh | 3998552 | 2017-03-15 06:29:23 +0000 | [diff] [blame] | 47 | /// 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 Wu | c4f2258 | 2016-06-09 15:24:29 +0000 | [diff] [blame] | 50 | bool OptimizeFunction(MachineFunction &MF, const TargetInstrInfo *tii, |
| 51 | const TargetRegisterInfo *tri, MachineModuleInfo *mmi, |
| 52 | MachineLoopInfo *mli = nullptr, |
| 53 | bool AfterPlacement = false); |
| 54 | |
Evan Cheng | 030a0a0 | 2009-09-04 07:47:40 +0000 | [diff] [blame] | 55 | private: |
Dan Gohman | ffe644e | 2009-11-11 21:57:02 +0000 | [diff] [blame] | 56 | class MergePotentialsElt { |
| 57 | unsigned Hash; |
| 58 | MachineBasicBlock *Block; |
Eugene Zelenko | 887aef7 | 2017-10-10 22:33:29 +0000 | [diff] [blame] | 59 | |
Dan Gohman | ffe644e | 2009-11-11 21:57:02 +0000 | [diff] [blame] | 60 | 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 Zelenko | 887aef7 | 2017-10-10 22:33:29 +0000 | [diff] [blame] | 73 | |
| 74 | using MPIterator = std::vector<MergePotentialsElt>::iterator; |
| 75 | |
Evan Cheng | 030a0a0 | 2009-09-04 07:47:40 +0000 | [diff] [blame] | 76 | std::vector<MergePotentialsElt> MergePotentials; |
Rafael Espindola | f924dea | 2011-06-14 15:31:54 +0000 | [diff] [blame] | 77 | SmallPtrSet<const MachineBasicBlock*, 2> TriedMerging; |
Heejin Ahn | 2522e34 | 2018-06-01 00:03:21 +0000 | [diff] [blame] | 78 | DenseMap<const MachineBasicBlock *, int> EHScopeMembership; |
Evan Cheng | 030a0a0 | 2009-09-04 07:47:40 +0000 | [diff] [blame] | 79 | |
Dan Gohman | ffe644e | 2009-11-11 21:57:02 +0000 | [diff] [blame] | 80 | class SameTailElt { |
| 81 | MPIterator MPIter; |
| 82 | MachineBasicBlock::iterator TailStartPos; |
Eugene Zelenko | 887aef7 | 2017-10-10 22:33:29 +0000 | [diff] [blame] | 83 | |
Dan Gohman | ffe644e | 2009-11-11 21:57:02 +0000 | [diff] [blame] | 84 | public: |
| 85 | SameTailElt(MPIterator mp, MachineBasicBlock::iterator tsp) |
| 86 | : MPIter(mp), TailStartPos(tsp) {} |
| 87 | |
| 88 | MPIterator getMPIter() const { |
| 89 | return MPIter; |
| 90 | } |
Eugene Zelenko | 887aef7 | 2017-10-10 22:33:29 +0000 | [diff] [blame] | 91 | |
Dan Gohman | ffe644e | 2009-11-11 21:57:02 +0000 | [diff] [blame] | 92 | MergePotentialsElt &getMergePotentialsElt() const { |
| 93 | return *getMPIter(); |
| 94 | } |
Eugene Zelenko | 887aef7 | 2017-10-10 22:33:29 +0000 | [diff] [blame] | 95 | |
Dan Gohman | ffe644e | 2009-11-11 21:57:02 +0000 | [diff] [blame] | 96 | MachineBasicBlock::iterator getTailStartPos() const { |
| 97 | return TailStartPos; |
| 98 | } |
Eugene Zelenko | 887aef7 | 2017-10-10 22:33:29 +0000 | [diff] [blame] | 99 | |
Dan Gohman | ffe644e | 2009-11-11 21:57:02 +0000 | [diff] [blame] | 100 | unsigned getHash() const { |
| 101 | return getMergePotentialsElt().getHash(); |
| 102 | } |
Eugene Zelenko | 887aef7 | 2017-10-10 22:33:29 +0000 | [diff] [blame] | 103 | |
Dan Gohman | ffe644e | 2009-11-11 21:57:02 +0000 | [diff] [blame] | 104 | MachineBasicBlock *getBlock() const { |
| 105 | return getMergePotentialsElt().getBlock(); |
| 106 | } |
Eugene Zelenko | 887aef7 | 2017-10-10 22:33:29 +0000 | [diff] [blame] | 107 | |
Dan Gohman | ffe644e | 2009-11-11 21:57:02 +0000 | [diff] [blame] | 108 | bool tailIsWholeBlock() const { |
| 109 | return TailStartPos == getBlock()->begin(); |
| 110 | } |
| 111 | |
| 112 | void setBlock(MachineBasicBlock *MBB) { |
| 113 | getMergePotentialsElt().setBlock(MBB); |
| 114 | } |
Eugene Zelenko | 887aef7 | 2017-10-10 22:33:29 +0000 | [diff] [blame] | 115 | |
Dan Gohman | ffe644e | 2009-11-11 21:57:02 +0000 | [diff] [blame] | 116 | void setTailStartPos(MachineBasicBlock::iterator Pos) { |
| 117 | TailStartPos = Pos; |
| 118 | } |
| 119 | }; |
Evan Cheng | 030a0a0 | 2009-09-04 07:47:40 +0000 | [diff] [blame] | 120 | std::vector<SameTailElt> SameTails; |
| 121 | |
Haicheng Wu | c4f2258 | 2016-06-09 15:24:29 +0000 | [diff] [blame] | 122 | bool AfterBlockPlacement; |
Evan Cheng | 030a0a0 | 2009-09-04 07:47:40 +0000 | [diff] [blame] | 123 | bool EnableTailMerge; |
Evan Cheng | cbc988b | 2011-05-12 00:56:58 +0000 | [diff] [blame] | 124 | bool EnableHoistCommonCode; |
Matthias Braun | 1efbc7f | 2016-07-12 18:44:33 +0000 | [diff] [blame] | 125 | bool UpdateLiveIns; |
Kyle Butt | b1ee91e | 2016-08-18 18:57:29 +0000 | [diff] [blame] | 126 | unsigned MinCommonTailLength; |
Evan Cheng | 030a0a0 | 2009-09-04 07:47:40 +0000 | [diff] [blame] | 127 | const TargetInstrInfo *TII; |
Matthias Braun | b0e29ac | 2017-05-26 06:32:31 +0000 | [diff] [blame] | 128 | const MachineRegisterInfo *MRI; |
Evan Cheng | 030a0a0 | 2009-09-04 07:47:40 +0000 | [diff] [blame] | 129 | const TargetRegisterInfo *TRI; |
| 130 | MachineModuleInfo *MMI; |
Haicheng Wu | c4f2258 | 2016-06-09 15:24:29 +0000 | [diff] [blame] | 131 | MachineLoopInfo *MLI; |
Matthias Braun | 1efbc7f | 2016-07-12 18:44:33 +0000 | [diff] [blame] | 132 | LivePhysRegs LiveRegs; |
Evan Cheng | 030a0a0 | 2009-09-04 07:47:40 +0000 | [diff] [blame] | 133 | |
Haicheng Wu | c4f2258 | 2016-06-09 15:24:29 +0000 | [diff] [blame] | 134 | public: |
Adrian Prantl | 26b584c | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 135 | /// This class keeps track of branch frequencies of newly created |
Akira Hatanaka | 70b5605 | 2014-08-07 19:30:13 +0000 | [diff] [blame] | 136 | /// blocks and tail-merged blocks. |
| 137 | class MBFIWrapper { |
| 138 | public: |
| 139 | MBFIWrapper(const MachineBlockFrequencyInfo &I) : MBFI(I) {} |
Eugene Zelenko | 887aef7 | 2017-10-10 22:33:29 +0000 | [diff] [blame] | 140 | |
Akira Hatanaka | 70b5605 | 2014-08-07 19:30:13 +0000 | [diff] [blame] | 141 | BlockFrequency getBlockFreq(const MachineBasicBlock *MBB) const; |
| 142 | void setBlockFreq(const MachineBasicBlock *MBB, BlockFrequency F); |
Haicheng Wu | c4f2258 | 2016-06-09 15:24:29 +0000 | [diff] [blame] | 143 | 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 Li | 210c690 | 2017-02-15 19:21:04 +0000 | [diff] [blame] | 147 | void view(const Twine &Name, bool isSimple = true); |
Kyle Butt | 5818a51 | 2017-01-31 23:48:32 +0000 | [diff] [blame] | 148 | uint64_t getEntryFreq() const; |
Akira Hatanaka | 70b5605 | 2014-08-07 19:30:13 +0000 | [diff] [blame] | 149 | |
| 150 | private: |
| 151 | const MachineBlockFrequencyInfo &MBFI; |
| 152 | DenseMap<const MachineBasicBlock *, BlockFrequency> MergedBBFreq; |
| 153 | }; |
| 154 | |
Haicheng Wu | c4f2258 | 2016-06-09 15:24:29 +0000 | [diff] [blame] | 155 | private: |
| 156 | MBFIWrapper &MBBFreqInfo; |
Akira Hatanaka | 70b5605 | 2014-08-07 19:30:13 +0000 | [diff] [blame] | 157 | const MachineBranchProbabilityInfo &MBPI; |
| 158 | |
Evan Cheng | 030a0a0 | 2009-09-04 07:47:40 +0000 | [diff] [blame] | 159 | bool TailMergeBlocks(MachineFunction &MF); |
Dan Gohman | 412a3b9 | 2009-11-11 19:49:34 +0000 | [diff] [blame] | 160 | bool TryTailMergeBlocks(MachineBasicBlock* SuccBB, |
Kyle Butt | b1ee91e | 2016-08-18 18:57:29 +0000 | [diff] [blame] | 161 | MachineBasicBlock* PredBB, |
| 162 | unsigned MinCommonTailLength); |
Akira Hatanaka | 70b5605 | 2014-08-07 19:30:13 +0000 | [diff] [blame] | 163 | void setCommonTailEdgeWeights(MachineBasicBlock &TailMBB); |
Taewook Oh | 3998552 | 2017-03-15 06:29:23 +0000 | [diff] [blame] | 164 | |
| 165 | /// Delete the instruction OldInst and everything after it, replacing it |
| 166 | /// with an unconditional branch to NewDest. |
Matthias Braun | 109c6e0 | 2017-09-06 20:45:24 +0000 | [diff] [blame] | 167 | void replaceTailWithBranchTo(MachineBasicBlock::iterator OldInst, |
| 168 | MachineBasicBlock &NewDest); |
Taewook Oh | 3998552 | 2017-03-15 06:29:23 +0000 | [diff] [blame] | 169 | |
| 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 Cheng | 030a0a0 | 2009-09-04 07:47:40 +0000 | [diff] [blame] | 173 | MachineBasicBlock *SplitMBBAt(MachineBasicBlock &CurMBB, |
Andrew Trick | f7b5e01 | 2013-06-24 01:55:01 +0000 | [diff] [blame] | 174 | MachineBasicBlock::iterator BBI1, |
| 175 | const BasicBlock *BB); |
Taewook Oh | 3998552 | 2017-03-15 06:29:23 +0000 | [diff] [blame] | 176 | |
| 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 Gohman | 412a3b9 | 2009-11-11 19:49:34 +0000 | [diff] [blame] | 186 | unsigned ComputeSameTails(unsigned CurHash, unsigned minCommonTailLength, |
| 187 | MachineBasicBlock *SuccBB, |
| 188 | MachineBasicBlock *PredBB); |
Taewook Oh | 3998552 | 2017-03-15 06:29:23 +0000 | [diff] [blame] | 189 | |
| 190 | /// Remove all blocks with hash CurHash from MergePotentials, restoring |
| 191 | /// branches at ends of blocks as appropriate. |
Evan Cheng | 030a0a0 | 2009-09-04 07:47:40 +0000 | [diff] [blame] | 192 | void RemoveBlocksWithHash(unsigned CurHash, MachineBasicBlock* SuccBB, |
| 193 | MachineBasicBlock* PredBB); |
Taewook Oh | 3998552 | 2017-03-15 06:29:23 +0000 | [diff] [blame] | 194 | |
| 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 Cheng | 4d54e5b | 2010-06-22 01:18:16 +0000 | [diff] [blame] | 197 | bool CreateCommonTailOnlyBlock(MachineBasicBlock *&PredBB, |
Andrew Trick | f7b5e01 | 2013-06-24 01:55:01 +0000 | [diff] [blame] | 198 | MachineBasicBlock *SuccBB, |
Evan Cheng | 4d54e5b | 2010-06-22 01:18:16 +0000 | [diff] [blame] | 199 | unsigned maxCommonTailLength, |
| 200 | unsigned &commonTailIndex); |
Taewook Oh | 3998552 | 2017-03-15 06:29:23 +0000 | [diff] [blame] | 201 | |
| 202 | /// Create merged DebugLocs of identical instructions across SameTails and |
Matthias Braun | 109c6e0 | 2017-09-06 20:45:24 +0000 | [diff] [blame] | 203 | /// assign it to the instruction in common tail; merge MMOs and undef flags. |
| 204 | void mergeCommonTails(unsigned commonTailIndex); |
Evan Cheng | 030a0a0 | 2009-09-04 07:47:40 +0000 | [diff] [blame] | 205 | |
| 206 | bool OptimizeBranches(MachineFunction &MF); |
Taewook Oh | 3998552 | 2017-03-15 06:29:23 +0000 | [diff] [blame] | 207 | |
| 208 | /// Analyze and optimize control flow related to the specified block. This |
| 209 | /// is never called on the entry block. |
Evan Cheng | 030a0a0 | 2009-09-04 07:47:40 +0000 | [diff] [blame] | 210 | bool OptimizeBlock(MachineBasicBlock *MBB); |
Taewook Oh | 3998552 | 2017-03-15 06:29:23 +0000 | [diff] [blame] | 211 | |
| 212 | /// Remove the specified dead machine basic block from the function, |
| 213 | /// updating the CFG. |
Evan Cheng | 030a0a0 | 2009-09-04 07:47:40 +0000 | [diff] [blame] | 214 | void RemoveDeadBlock(MachineBasicBlock *MBB); |
Evan Cheng | cbc988b | 2011-05-12 00:56:58 +0000 | [diff] [blame] | 215 | |
Taewook Oh | 3998552 | 2017-03-15 06:29:23 +0000 | [diff] [blame] | 216 | /// Hoist common instruction sequences at the start of basic blocks to their |
| 217 | /// common predecessor. |
Evan Cheng | cbc988b | 2011-05-12 00:56:58 +0000 | [diff] [blame] | 218 | bool HoistCommonCode(MachineFunction &MF); |
Taewook Oh | 3998552 | 2017-03-15 06:29:23 +0000 | [diff] [blame] | 219 | |
| 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 Cheng | cbc988b | 2011-05-12 00:56:58 +0000 | [diff] [blame] | 222 | bool HoistCommonCodeInSuccs(MachineBasicBlock *MBB); |
Evan Cheng | 030a0a0 | 2009-09-04 07:47:40 +0000 | [diff] [blame] | 223 | }; |
Evan Cheng | 030a0a0 | 2009-09-04 07:47:40 +0000 | [diff] [blame] | 224 | |
Eugene Zelenko | 887aef7 | 2017-10-10 22:33:29 +0000 | [diff] [blame] | 225 | } // end namespace llvm |
| 226 | |
| 227 | #endif // LLVM_LIB_CODEGEN_BRANCHFOLDING_H |