LLVM API Documentation

BranchFolding.h
Go to the documentation of this file.
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_LIB_CODEGEN_BRANCHFOLDING_H
00011 #define LLVM_LIB_CODEGEN_BRANCHFOLDING_H
00012 
00013 #include "llvm/ADT/SmallPtrSet.h"
00014 #include "llvm/CodeGen/MachineBasicBlock.h"
00015 #include "llvm/Support/BlockFrequency.h"
00016 #include <vector>
00017 
00018 namespace llvm {
00019   class MachineBlockFrequencyInfo;
00020   class MachineBranchProbabilityInfo;
00021   class MachineFunction;
00022   class MachineModuleInfo;
00023   class RegScavenger;
00024   class TargetInstrInfo;
00025   class TargetRegisterInfo;
00026 
00027   class BranchFolder {
00028   public:
00029     explicit BranchFolder(bool defaultEnableTailMerge, bool CommonHoist,
00030                           const MachineBlockFrequencyInfo &MBFI,
00031                           const MachineBranchProbabilityInfo &MBPI);
00032 
00033     bool OptimizeFunction(MachineFunction &MF,
00034                           const TargetInstrInfo *tii,
00035                           const TargetRegisterInfo *tri,
00036                           MachineModuleInfo *mmi);
00037   private:
00038     class MergePotentialsElt {
00039       unsigned Hash;
00040       MachineBasicBlock *Block;
00041     public:
00042       MergePotentialsElt(unsigned h, MachineBasicBlock *b)
00043         : Hash(h), Block(b) {}
00044 
00045       unsigned getHash() const { return Hash; }
00046       MachineBasicBlock *getBlock() const { return Block; }
00047 
00048       void setBlock(MachineBasicBlock *MBB) {
00049         Block = MBB;
00050       }
00051 
00052       bool operator<(const MergePotentialsElt &) const;
00053     };
00054     typedef std::vector<MergePotentialsElt>::iterator MPIterator;
00055     std::vector<MergePotentialsElt> MergePotentials;
00056     SmallPtrSet<const MachineBasicBlock*, 2> TriedMerging;
00057 
00058     class SameTailElt {
00059       MPIterator MPIter;
00060       MachineBasicBlock::iterator TailStartPos;
00061     public:
00062       SameTailElt(MPIterator mp, MachineBasicBlock::iterator tsp)
00063         : MPIter(mp), TailStartPos(tsp) {}
00064 
00065       MPIterator getMPIter() const {
00066         return MPIter;
00067       }
00068       MergePotentialsElt &getMergePotentialsElt() const {
00069         return *getMPIter();
00070       }
00071       MachineBasicBlock::iterator getTailStartPos() const {
00072         return TailStartPos;
00073       }
00074       unsigned getHash() const {
00075         return getMergePotentialsElt().getHash();
00076       }
00077       MachineBasicBlock *getBlock() const {
00078         return getMergePotentialsElt().getBlock();
00079       }
00080       bool tailIsWholeBlock() const {
00081         return TailStartPos == getBlock()->begin();
00082       }
00083 
00084       void setBlock(MachineBasicBlock *MBB) {
00085         getMergePotentialsElt().setBlock(MBB);
00086       }
00087       void setTailStartPos(MachineBasicBlock::iterator Pos) {
00088         TailStartPos = Pos;
00089       }
00090     };
00091     std::vector<SameTailElt> SameTails;
00092 
00093     bool EnableTailMerge;
00094     bool EnableHoistCommonCode;
00095     const TargetInstrInfo *TII;
00096     const TargetRegisterInfo *TRI;
00097     MachineModuleInfo *MMI;
00098     RegScavenger *RS;
00099 
00100     /// \brief This class keeps track of branch frequencies of newly created
00101     /// blocks and tail-merged blocks.
00102     class MBFIWrapper {
00103     public:
00104       MBFIWrapper(const MachineBlockFrequencyInfo &I) : MBFI(I) {}
00105       BlockFrequency getBlockFreq(const MachineBasicBlock *MBB) const;
00106       void setBlockFreq(const MachineBasicBlock *MBB, BlockFrequency F);
00107 
00108     private:
00109       const MachineBlockFrequencyInfo &MBFI;
00110       DenseMap<const MachineBasicBlock *, BlockFrequency> MergedBBFreq;
00111     };
00112 
00113     MBFIWrapper MBBFreqInfo;
00114     const MachineBranchProbabilityInfo &MBPI;
00115 
00116     bool TailMergeBlocks(MachineFunction &MF);
00117     bool TryTailMergeBlocks(MachineBasicBlock* SuccBB,
00118                        MachineBasicBlock* PredBB);
00119     void setCommonTailEdgeWeights(MachineBasicBlock &TailMBB);
00120     void MaintainLiveIns(MachineBasicBlock *CurMBB,
00121                          MachineBasicBlock *NewMBB);
00122     void ReplaceTailWithBranchTo(MachineBasicBlock::iterator OldInst,
00123                                  MachineBasicBlock *NewDest);
00124     MachineBasicBlock *SplitMBBAt(MachineBasicBlock &CurMBB,
00125                                   MachineBasicBlock::iterator BBI1,
00126                                   const BasicBlock *BB);
00127     unsigned ComputeSameTails(unsigned CurHash, unsigned minCommonTailLength,
00128                               MachineBasicBlock *SuccBB,
00129                               MachineBasicBlock *PredBB);
00130     void RemoveBlocksWithHash(unsigned CurHash, MachineBasicBlock* SuccBB,
00131                                                 MachineBasicBlock* PredBB);
00132     bool CreateCommonTailOnlyBlock(MachineBasicBlock *&PredBB,
00133                                    MachineBasicBlock *SuccBB,
00134                                    unsigned maxCommonTailLength,
00135                                    unsigned &commonTailIndex);
00136 
00137     bool OptimizeBranches(MachineFunction &MF);
00138     bool OptimizeBlock(MachineBasicBlock *MBB);
00139     void RemoveDeadBlock(MachineBasicBlock *MBB);
00140     bool OptimizeImpDefsBlock(MachineBasicBlock *MBB);
00141 
00142     bool HoistCommonCode(MachineFunction &MF);
00143     bool HoistCommonCodeInSuccs(MachineBasicBlock *MBB);
00144   };
00145 }
00146 
00147 #endif /* LLVM_CODEGEN_BRANCHFOLDING_HPP */