LLVM API Documentation
00001 //===-- BranchFolding.h - Fold machine code branch instructions --*- C++ -*===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 00010 #ifndef LLVM_CODEGEN_BRANCHFOLDING_HPP 00011 #define LLVM_CODEGEN_BRANCHFOLDING_HPP 00012 00013 #include "llvm/ADT/SmallPtrSet.h" 00014 #include "llvm/CodeGen/MachineBasicBlock.h" 00015 #include <vector> 00016 00017 namespace llvm { 00018 class MachineFunction; 00019 class MachineModuleInfo; 00020 class RegScavenger; 00021 class TargetInstrInfo; 00022 class TargetRegisterInfo; 00023 00024 class BranchFolder { 00025 public: 00026 explicit BranchFolder(bool defaultEnableTailMerge, bool CommonHoist); 00027 00028 bool OptimizeFunction(MachineFunction &MF, 00029 const TargetInstrInfo *tii, 00030 const TargetRegisterInfo *tri, 00031 MachineModuleInfo *mmi); 00032 private: 00033 class MergePotentialsElt { 00034 unsigned Hash; 00035 MachineBasicBlock *Block; 00036 public: 00037 MergePotentialsElt(unsigned h, MachineBasicBlock *b) 00038 : Hash(h), Block(b) {} 00039 00040 unsigned getHash() const { return Hash; } 00041 MachineBasicBlock *getBlock() const { return Block; } 00042 00043 void setBlock(MachineBasicBlock *MBB) { 00044 Block = MBB; 00045 } 00046 00047 bool operator<(const MergePotentialsElt &) const; 00048 }; 00049 typedef std::vector<MergePotentialsElt>::iterator MPIterator; 00050 std::vector<MergePotentialsElt> MergePotentials; 00051 SmallPtrSet<const MachineBasicBlock*, 2> TriedMerging; 00052 00053 class SameTailElt { 00054 MPIterator MPIter; 00055 MachineBasicBlock::iterator TailStartPos; 00056 public: 00057 SameTailElt(MPIterator mp, MachineBasicBlock::iterator tsp) 00058 : MPIter(mp), TailStartPos(tsp) {} 00059 00060 MPIterator getMPIter() const { 00061 return MPIter; 00062 } 00063 MergePotentialsElt &getMergePotentialsElt() const { 00064 return *getMPIter(); 00065 } 00066 MachineBasicBlock::iterator getTailStartPos() const { 00067 return TailStartPos; 00068 } 00069 unsigned getHash() const { 00070 return getMergePotentialsElt().getHash(); 00071 } 00072 MachineBasicBlock *getBlock() const { 00073 return getMergePotentialsElt().getBlock(); 00074 } 00075 bool tailIsWholeBlock() const { 00076 return TailStartPos == getBlock()->begin(); 00077 } 00078 00079 void setBlock(MachineBasicBlock *MBB) { 00080 getMergePotentialsElt().setBlock(MBB); 00081 } 00082 void setTailStartPos(MachineBasicBlock::iterator Pos) { 00083 TailStartPos = Pos; 00084 } 00085 }; 00086 std::vector<SameTailElt> SameTails; 00087 00088 bool EnableTailMerge; 00089 bool EnableHoistCommonCode; 00090 const TargetInstrInfo *TII; 00091 const TargetRegisterInfo *TRI; 00092 MachineModuleInfo *MMI; 00093 RegScavenger *RS; 00094 00095 bool TailMergeBlocks(MachineFunction &MF); 00096 bool TryTailMergeBlocks(MachineBasicBlock* SuccBB, 00097 MachineBasicBlock* PredBB); 00098 void MaintainLiveIns(MachineBasicBlock *CurMBB, 00099 MachineBasicBlock *NewMBB); 00100 void ReplaceTailWithBranchTo(MachineBasicBlock::iterator OldInst, 00101 MachineBasicBlock *NewDest); 00102 MachineBasicBlock *SplitMBBAt(MachineBasicBlock &CurMBB, 00103 MachineBasicBlock::iterator BBI1); 00104 unsigned ComputeSameTails(unsigned CurHash, unsigned minCommonTailLength, 00105 MachineBasicBlock *SuccBB, 00106 MachineBasicBlock *PredBB); 00107 void RemoveBlocksWithHash(unsigned CurHash, MachineBasicBlock* SuccBB, 00108 MachineBasicBlock* PredBB); 00109 bool CreateCommonTailOnlyBlock(MachineBasicBlock *&PredBB, 00110 unsigned maxCommonTailLength, 00111 unsigned &commonTailIndex); 00112 00113 bool OptimizeBranches(MachineFunction &MF); 00114 bool OptimizeBlock(MachineBasicBlock *MBB); 00115 void RemoveDeadBlock(MachineBasicBlock *MBB); 00116 bool OptimizeImpDefsBlock(MachineBasicBlock *MBB); 00117 00118 bool HoistCommonCode(MachineFunction &MF); 00119 bool HoistCommonCodeInSuccs(MachineBasicBlock *MBB); 00120 }; 00121 } 00122 00123 #endif /* LLVM_CODEGEN_BRANCHFOLDING_HPP */