Evan Cheng | 030a0a0 | 2009-09-04 07:47:40 +0000 | [diff] [blame^] | 1 | //===-- BranchFolding.h - Fold machine code branch instructions --*- C++ -*===// |
| 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 | |
| 10 | #ifndef LLVM_CODEGEN_BRANCHFOLDING_HPP |
| 11 | #define LLVM_CODEGEN_BRANCHFOLDING_HPP |
| 12 | |
| 13 | #include "llvm/CodeGen/MachineBasicBlock.h" |
| 14 | #include "llvm/CodeGen/MachineFunctionPass.h" |
| 15 | #include <vector> |
| 16 | |
| 17 | namespace llvm { |
| 18 | class MachineFunction; |
| 19 | class MachineModuleInfo; |
| 20 | class RegScavenger; |
| 21 | class TargetInstrInfo; |
| 22 | class TargetRegisterInfo; |
| 23 | |
| 24 | class BranchFolder { |
| 25 | public: |
| 26 | explicit BranchFolder(bool defaultEnableTailMerge); |
| 27 | |
| 28 | bool OptimizeFunction(MachineFunction &MF, |
| 29 | const TargetInstrInfo *tii, |
| 30 | const TargetRegisterInfo *tri, |
| 31 | MachineModuleInfo *mmi); |
| 32 | private: |
| 33 | typedef std::pair<unsigned,MachineBasicBlock*> MergePotentialsElt; |
| 34 | typedef std::vector<MergePotentialsElt>::iterator MPIterator; |
| 35 | std::vector<MergePotentialsElt> MergePotentials; |
| 36 | |
| 37 | typedef std::pair<MPIterator, MachineBasicBlock::iterator> SameTailElt; |
| 38 | std::vector<SameTailElt> SameTails; |
| 39 | |
| 40 | bool EnableTailMerge; |
| 41 | const TargetInstrInfo *TII; |
| 42 | const TargetRegisterInfo *TRI; |
| 43 | MachineModuleInfo *MMI; |
| 44 | RegScavenger *RS; |
| 45 | |
| 46 | bool TailMergeBlocks(MachineFunction &MF); |
| 47 | bool TryMergeBlocks(MachineBasicBlock* SuccBB, |
| 48 | MachineBasicBlock* PredBB); |
| 49 | void ReplaceTailWithBranchTo(MachineBasicBlock::iterator OldInst, |
| 50 | MachineBasicBlock *NewDest); |
| 51 | MachineBasicBlock *SplitMBBAt(MachineBasicBlock &CurMBB, |
| 52 | MachineBasicBlock::iterator BBI1); |
| 53 | unsigned ComputeSameTails(unsigned CurHash, unsigned minCommonTailLength); |
| 54 | void RemoveBlocksWithHash(unsigned CurHash, MachineBasicBlock* SuccBB, |
| 55 | MachineBasicBlock* PredBB); |
| 56 | unsigned CreateCommonTailOnlyBlock(MachineBasicBlock *&PredBB, |
| 57 | unsigned maxCommonTailLength); |
| 58 | |
| 59 | bool OptimizeBranches(MachineFunction &MF); |
| 60 | bool OptimizeBlock(MachineBasicBlock *MBB); |
| 61 | void RemoveDeadBlock(MachineBasicBlock *MBB); |
| 62 | bool OptimizeImpDefsBlock(MachineBasicBlock *MBB); |
| 63 | |
| 64 | bool CanFallThrough(MachineBasicBlock *CurBB); |
| 65 | bool CanFallThrough(MachineBasicBlock *CurBB, bool BranchUnAnalyzable, |
| 66 | MachineBasicBlock *TBB, MachineBasicBlock *FBB, |
| 67 | const SmallVectorImpl<MachineOperand> &Cond); |
| 68 | }; |
| 69 | |
| 70 | |
| 71 | /// BranchFolderPass - Wrap branch folder in a machine function pass. |
| 72 | class BranchFolderPass : public MachineFunctionPass, |
| 73 | public BranchFolder { |
| 74 | public: |
| 75 | static char ID; |
| 76 | explicit BranchFolderPass(bool defaultEnableTailMerge) |
| 77 | : MachineFunctionPass(&ID), BranchFolder(defaultEnableTailMerge) {} |
| 78 | |
| 79 | virtual bool runOnMachineFunction(MachineFunction &MF); |
| 80 | virtual const char *getPassName() const { return "Control Flow Optimizer"; } |
| 81 | }; |
| 82 | } |
| 83 | |
| 84 | #endif /* LLVM_CODEGEN_BRANCHFOLDING_HPP */ |