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 | |
Haicheng Wu | 5b458cc | 2016-06-09 15:24:29 +0000 | [diff] [blame] | 40 | bool OptimizeFunction(MachineFunction &MF, const TargetInstrInfo *tii, |
| 41 | const TargetRegisterInfo *tri, MachineModuleInfo *mmi, |
| 42 | MachineLoopInfo *mli = nullptr, |
| 43 | bool AfterPlacement = false); |
| 44 | |
Evan Cheng | 3d2fce0 | 2009-09-04 07:47:40 +0000 | [diff] [blame] | 45 | private: |
Dan Gohman | 02b1554 | 2009-11-11 21:57:02 +0000 | [diff] [blame] | 46 | class MergePotentialsElt { |
| 47 | unsigned Hash; |
| 48 | MachineBasicBlock *Block; |
| 49 | public: |
| 50 | MergePotentialsElt(unsigned h, MachineBasicBlock *b) |
| 51 | : Hash(h), Block(b) {} |
| 52 | |
| 53 | unsigned getHash() const { return Hash; } |
| 54 | MachineBasicBlock *getBlock() const { return Block; } |
| 55 | |
| 56 | void setBlock(MachineBasicBlock *MBB) { |
| 57 | Block = MBB; |
| 58 | } |
| 59 | |
| 60 | bool operator<(const MergePotentialsElt &) const; |
| 61 | }; |
Evan Cheng | 3d2fce0 | 2009-09-04 07:47:40 +0000 | [diff] [blame] | 62 | typedef std::vector<MergePotentialsElt>::iterator MPIterator; |
| 63 | std::vector<MergePotentialsElt> MergePotentials; |
Rafael Espindola | 3aeaf9e | 2011-06-14 15:31:54 +0000 | [diff] [blame] | 64 | SmallPtrSet<const MachineBasicBlock*, 2> TriedMerging; |
David Majnemer | 1619355 | 2015-10-04 02:22:52 +0000 | [diff] [blame] | 65 | DenseMap<const MachineBasicBlock *, int> FuncletMembership; |
Evan Cheng | 3d2fce0 | 2009-09-04 07:47:40 +0000 | [diff] [blame] | 66 | |
Dan Gohman | 02b1554 | 2009-11-11 21:57:02 +0000 | [diff] [blame] | 67 | class SameTailElt { |
| 68 | MPIterator MPIter; |
| 69 | MachineBasicBlock::iterator TailStartPos; |
| 70 | public: |
| 71 | SameTailElt(MPIterator mp, MachineBasicBlock::iterator tsp) |
| 72 | : MPIter(mp), TailStartPos(tsp) {} |
| 73 | |
| 74 | MPIterator getMPIter() const { |
| 75 | return MPIter; |
| 76 | } |
| 77 | MergePotentialsElt &getMergePotentialsElt() const { |
| 78 | return *getMPIter(); |
| 79 | } |
| 80 | MachineBasicBlock::iterator getTailStartPos() const { |
| 81 | return TailStartPos; |
| 82 | } |
| 83 | unsigned getHash() const { |
| 84 | return getMergePotentialsElt().getHash(); |
| 85 | } |
| 86 | MachineBasicBlock *getBlock() const { |
| 87 | return getMergePotentialsElt().getBlock(); |
| 88 | } |
| 89 | bool tailIsWholeBlock() const { |
| 90 | return TailStartPos == getBlock()->begin(); |
| 91 | } |
| 92 | |
| 93 | void setBlock(MachineBasicBlock *MBB) { |
| 94 | getMergePotentialsElt().setBlock(MBB); |
| 95 | } |
| 96 | void setTailStartPos(MachineBasicBlock::iterator Pos) { |
| 97 | TailStartPos = Pos; |
| 98 | } |
| 99 | }; |
Evan Cheng | 3d2fce0 | 2009-09-04 07:47:40 +0000 | [diff] [blame] | 100 | std::vector<SameTailElt> SameTails; |
| 101 | |
Haicheng Wu | 5b458cc | 2016-06-09 15:24:29 +0000 | [diff] [blame] | 102 | bool AfterBlockPlacement; |
Evan Cheng | 3d2fce0 | 2009-09-04 07:47:40 +0000 | [diff] [blame] | 103 | bool EnableTailMerge; |
Evan Cheng | cfdf339 | 2011-05-12 00:56:58 +0000 | [diff] [blame] | 104 | bool EnableHoistCommonCode; |
Matthias Braun | aeab09f | 2016-07-12 18:44:33 +0000 | [diff] [blame] | 105 | bool UpdateLiveIns; |
Kyle Butt | 64e4281 | 2016-08-18 18:57:29 +0000 | [diff] [blame] | 106 | unsigned MinCommonTailLength; |
Evan Cheng | 3d2fce0 | 2009-09-04 07:47:40 +0000 | [diff] [blame] | 107 | const TargetInstrInfo *TII; |
| 108 | const TargetRegisterInfo *TRI; |
| 109 | MachineModuleInfo *MMI; |
Haicheng Wu | 5b458cc | 2016-06-09 15:24:29 +0000 | [diff] [blame] | 110 | MachineLoopInfo *MLI; |
Matthias Braun | aeab09f | 2016-07-12 18:44:33 +0000 | [diff] [blame] | 111 | LivePhysRegs LiveRegs; |
Evan Cheng | 3d2fce0 | 2009-09-04 07:47:40 +0000 | [diff] [blame] | 112 | |
Haicheng Wu | 5b458cc | 2016-06-09 15:24:29 +0000 | [diff] [blame] | 113 | public: |
Akira Hatanaka | bbd33f6 | 2014-08-07 19:30:13 +0000 | [diff] [blame] | 114 | /// \brief This class keeps track of branch frequencies of newly created |
| 115 | /// blocks and tail-merged blocks. |
| 116 | class MBFIWrapper { |
| 117 | public: |
| 118 | MBFIWrapper(const MachineBlockFrequencyInfo &I) : MBFI(I) {} |
| 119 | BlockFrequency getBlockFreq(const MachineBasicBlock *MBB) const; |
| 120 | void setBlockFreq(const MachineBasicBlock *MBB, BlockFrequency F); |
Haicheng Wu | 5b458cc | 2016-06-09 15:24:29 +0000 | [diff] [blame] | 121 | raw_ostream &printBlockFreq(raw_ostream &OS, |
| 122 | const MachineBasicBlock *MBB) const; |
| 123 | raw_ostream &printBlockFreq(raw_ostream &OS, |
| 124 | const BlockFrequency Freq) const; |
Akira Hatanaka | bbd33f6 | 2014-08-07 19:30:13 +0000 | [diff] [blame] | 125 | |
| 126 | private: |
| 127 | const MachineBlockFrequencyInfo &MBFI; |
| 128 | DenseMap<const MachineBasicBlock *, BlockFrequency> MergedBBFreq; |
| 129 | }; |
| 130 | |
Haicheng Wu | 5b458cc | 2016-06-09 15:24:29 +0000 | [diff] [blame] | 131 | private: |
| 132 | MBFIWrapper &MBBFreqInfo; |
Akira Hatanaka | bbd33f6 | 2014-08-07 19:30:13 +0000 | [diff] [blame] | 133 | const MachineBranchProbabilityInfo &MBPI; |
| 134 | |
Evan Cheng | 3d2fce0 | 2009-09-04 07:47:40 +0000 | [diff] [blame] | 135 | bool TailMergeBlocks(MachineFunction &MF); |
Dan Gohman | 34eeb4e | 2009-11-11 19:49:34 +0000 | [diff] [blame] | 136 | bool TryTailMergeBlocks(MachineBasicBlock* SuccBB, |
Kyle Butt | 64e4281 | 2016-08-18 18:57:29 +0000 | [diff] [blame] | 137 | MachineBasicBlock* PredBB, |
| 138 | unsigned MinCommonTailLength); |
Akira Hatanaka | bbd33f6 | 2014-08-07 19:30:13 +0000 | [diff] [blame] | 139 | void setCommonTailEdgeWeights(MachineBasicBlock &TailMBB); |
Matthias Braun | aeab09f | 2016-07-12 18:44:33 +0000 | [diff] [blame] | 140 | void computeLiveIns(MachineBasicBlock &MBB); |
Evan Cheng | 3d2fce0 | 2009-09-04 07:47:40 +0000 | [diff] [blame] | 141 | void ReplaceTailWithBranchTo(MachineBasicBlock::iterator OldInst, |
| 142 | MachineBasicBlock *NewDest); |
| 143 | MachineBasicBlock *SplitMBBAt(MachineBasicBlock &CurMBB, |
Andrew Trick | 97a1d7c | 2013-06-24 01:55:01 +0000 | [diff] [blame] | 144 | MachineBasicBlock::iterator BBI1, |
| 145 | const BasicBlock *BB); |
Dan Gohman | 34eeb4e | 2009-11-11 19:49:34 +0000 | [diff] [blame] | 146 | unsigned ComputeSameTails(unsigned CurHash, unsigned minCommonTailLength, |
| 147 | MachineBasicBlock *SuccBB, |
| 148 | MachineBasicBlock *PredBB); |
Evan Cheng | 3d2fce0 | 2009-09-04 07:47:40 +0000 | [diff] [blame] | 149 | void RemoveBlocksWithHash(unsigned CurHash, MachineBasicBlock* SuccBB, |
| 150 | MachineBasicBlock* PredBB); |
Evan Cheng | 37bb617 | 2010-06-22 01:18:16 +0000 | [diff] [blame] | 151 | bool CreateCommonTailOnlyBlock(MachineBasicBlock *&PredBB, |
Andrew Trick | 97a1d7c | 2013-06-24 01:55:01 +0000 | [diff] [blame] | 152 | MachineBasicBlock *SuccBB, |
Evan Cheng | 37bb617 | 2010-06-22 01:18:16 +0000 | [diff] [blame] | 153 | unsigned maxCommonTailLength, |
| 154 | unsigned &commonTailIndex); |
Evan Cheng | 3d2fce0 | 2009-09-04 07:47:40 +0000 | [diff] [blame] | 155 | |
| 156 | bool OptimizeBranches(MachineFunction &MF); |
| 157 | bool OptimizeBlock(MachineBasicBlock *MBB); |
| 158 | void RemoveDeadBlock(MachineBasicBlock *MBB); |
| 159 | bool OptimizeImpDefsBlock(MachineBasicBlock *MBB); |
Evan Cheng | cfdf339 | 2011-05-12 00:56:58 +0000 | [diff] [blame] | 160 | |
| 161 | bool HoistCommonCode(MachineFunction &MF); |
| 162 | bool HoistCommonCodeInSuccs(MachineBasicBlock *MBB); |
Evan Cheng | 3d2fce0 | 2009-09-04 07:47:40 +0000 | [diff] [blame] | 163 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 164 | } |
Evan Cheng | 3d2fce0 | 2009-09-04 07:47:40 +0000 | [diff] [blame] | 165 | |
| 166 | #endif /* LLVM_CODEGEN_BRANCHFOLDING_HPP */ |