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