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