2013-08-13 04:49:27 +08:00
|
|
|
//===-- BranchFolding.h - Fold machine code branch instructions -*- C++ -*-===//
|
2009-09-04 15:47:40 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2014-08-14 00:26:38 +08:00
|
|
|
#ifndef LLVM_LIB_CODEGEN_BRANCHFOLDING_H
|
|
|
|
#define LLVM_LIB_CODEGEN_BRANCHFOLDING_H
|
2009-09-04 15:47:40 +08:00
|
|
|
|
2011-06-14 23:31:54 +08:00
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
2009-09-04 15:47:40 +08:00
|
|
|
#include "llvm/CodeGen/MachineBasicBlock.h"
|
2014-08-08 03:30:13 +08:00
|
|
|
#include "llvm/Support/BlockFrequency.h"
|
2009-09-04 15:47:40 +08:00
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
namespace llvm {
|
2014-08-08 03:30:13 +08:00
|
|
|
class MachineBlockFrequencyInfo;
|
|
|
|
class MachineBranchProbabilityInfo;
|
2009-09-04 15:47:40 +08:00
|
|
|
class MachineFunction;
|
|
|
|
class MachineModuleInfo;
|
|
|
|
class RegScavenger;
|
|
|
|
class TargetInstrInfo;
|
|
|
|
class TargetRegisterInfo;
|
|
|
|
|
2015-07-01 22:47:39 +08:00
|
|
|
class LLVM_LIBRARY_VISIBILITY BranchFolder {
|
2009-09-04 15:47:40 +08:00
|
|
|
public:
|
2014-08-08 03:30:13 +08:00
|
|
|
explicit BranchFolder(bool defaultEnableTailMerge, bool CommonHoist,
|
|
|
|
const MachineBlockFrequencyInfo &MBFI,
|
|
|
|
const MachineBranchProbabilityInfo &MBPI);
|
2009-09-04 15:47:40 +08:00
|
|
|
|
|
|
|
bool OptimizeFunction(MachineFunction &MF,
|
|
|
|
const TargetInstrInfo *tii,
|
|
|
|
const TargetRegisterInfo *tri,
|
|
|
|
MachineModuleInfo *mmi);
|
|
|
|
private:
|
2009-11-12 05:57:02 +08:00
|
|
|
class MergePotentialsElt {
|
|
|
|
unsigned Hash;
|
|
|
|
MachineBasicBlock *Block;
|
|
|
|
public:
|
|
|
|
MergePotentialsElt(unsigned h, MachineBasicBlock *b)
|
|
|
|
: Hash(h), Block(b) {}
|
|
|
|
|
|
|
|
unsigned getHash() const { return Hash; }
|
|
|
|
MachineBasicBlock *getBlock() const { return Block; }
|
|
|
|
|
|
|
|
void setBlock(MachineBasicBlock *MBB) {
|
|
|
|
Block = MBB;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator<(const MergePotentialsElt &) const;
|
|
|
|
};
|
2009-09-04 15:47:40 +08:00
|
|
|
typedef std::vector<MergePotentialsElt>::iterator MPIterator;
|
|
|
|
std::vector<MergePotentialsElt> MergePotentials;
|
2011-06-14 23:31:54 +08:00
|
|
|
SmallPtrSet<const MachineBasicBlock*, 2> TriedMerging;
|
2009-09-04 15:47:40 +08:00
|
|
|
|
2009-11-12 05:57:02 +08:00
|
|
|
class SameTailElt {
|
|
|
|
MPIterator MPIter;
|
|
|
|
MachineBasicBlock::iterator TailStartPos;
|
|
|
|
public:
|
|
|
|
SameTailElt(MPIterator mp, MachineBasicBlock::iterator tsp)
|
|
|
|
: MPIter(mp), TailStartPos(tsp) {}
|
|
|
|
|
|
|
|
MPIterator getMPIter() const {
|
|
|
|
return MPIter;
|
|
|
|
}
|
|
|
|
MergePotentialsElt &getMergePotentialsElt() const {
|
|
|
|
return *getMPIter();
|
|
|
|
}
|
|
|
|
MachineBasicBlock::iterator getTailStartPos() const {
|
|
|
|
return TailStartPos;
|
|
|
|
}
|
|
|
|
unsigned getHash() const {
|
|
|
|
return getMergePotentialsElt().getHash();
|
|
|
|
}
|
|
|
|
MachineBasicBlock *getBlock() const {
|
|
|
|
return getMergePotentialsElt().getBlock();
|
|
|
|
}
|
|
|
|
bool tailIsWholeBlock() const {
|
|
|
|
return TailStartPos == getBlock()->begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
void setBlock(MachineBasicBlock *MBB) {
|
|
|
|
getMergePotentialsElt().setBlock(MBB);
|
|
|
|
}
|
|
|
|
void setTailStartPos(MachineBasicBlock::iterator Pos) {
|
|
|
|
TailStartPos = Pos;
|
|
|
|
}
|
|
|
|
};
|
2009-09-04 15:47:40 +08:00
|
|
|
std::vector<SameTailElt> SameTails;
|
|
|
|
|
|
|
|
bool EnableTailMerge;
|
2011-05-12 08:56:58 +08:00
|
|
|
bool EnableHoistCommonCode;
|
2009-09-04 15:47:40 +08:00
|
|
|
const TargetInstrInfo *TII;
|
|
|
|
const TargetRegisterInfo *TRI;
|
|
|
|
MachineModuleInfo *MMI;
|
|
|
|
RegScavenger *RS;
|
|
|
|
|
2014-08-08 03:30:13 +08:00
|
|
|
/// \brief This class keeps track of branch frequencies of newly created
|
|
|
|
/// blocks and tail-merged blocks.
|
|
|
|
class MBFIWrapper {
|
|
|
|
public:
|
|
|
|
MBFIWrapper(const MachineBlockFrequencyInfo &I) : MBFI(I) {}
|
|
|
|
BlockFrequency getBlockFreq(const MachineBasicBlock *MBB) const;
|
|
|
|
void setBlockFreq(const MachineBasicBlock *MBB, BlockFrequency F);
|
|
|
|
|
|
|
|
private:
|
|
|
|
const MachineBlockFrequencyInfo &MBFI;
|
|
|
|
DenseMap<const MachineBasicBlock *, BlockFrequency> MergedBBFreq;
|
|
|
|
};
|
|
|
|
|
|
|
|
MBFIWrapper MBBFreqInfo;
|
|
|
|
const MachineBranchProbabilityInfo &MBPI;
|
|
|
|
|
2009-09-04 15:47:40 +08:00
|
|
|
bool TailMergeBlocks(MachineFunction &MF);
|
2009-11-12 03:49:34 +08:00
|
|
|
bool TryTailMergeBlocks(MachineBasicBlock* SuccBB,
|
|
|
|
MachineBasicBlock* PredBB);
|
2014-08-08 03:30:13 +08:00
|
|
|
void setCommonTailEdgeWeights(MachineBasicBlock &TailMBB);
|
2011-07-07 07:41:48 +08:00
|
|
|
void MaintainLiveIns(MachineBasicBlock *CurMBB,
|
|
|
|
MachineBasicBlock *NewMBB);
|
2009-09-04 15:47:40 +08:00
|
|
|
void ReplaceTailWithBranchTo(MachineBasicBlock::iterator OldInst,
|
|
|
|
MachineBasicBlock *NewDest);
|
|
|
|
MachineBasicBlock *SplitMBBAt(MachineBasicBlock &CurMBB,
|
2013-06-24 09:55:01 +08:00
|
|
|
MachineBasicBlock::iterator BBI1,
|
|
|
|
const BasicBlock *BB);
|
2009-11-12 03:49:34 +08:00
|
|
|
unsigned ComputeSameTails(unsigned CurHash, unsigned minCommonTailLength,
|
|
|
|
MachineBasicBlock *SuccBB,
|
|
|
|
MachineBasicBlock *PredBB);
|
2009-09-04 15:47:40 +08:00
|
|
|
void RemoveBlocksWithHash(unsigned CurHash, MachineBasicBlock* SuccBB,
|
|
|
|
MachineBasicBlock* PredBB);
|
2010-06-22 09:18:16 +08:00
|
|
|
bool CreateCommonTailOnlyBlock(MachineBasicBlock *&PredBB,
|
2013-06-24 09:55:01 +08:00
|
|
|
MachineBasicBlock *SuccBB,
|
2010-06-22 09:18:16 +08:00
|
|
|
unsigned maxCommonTailLength,
|
|
|
|
unsigned &commonTailIndex);
|
2009-09-04 15:47:40 +08:00
|
|
|
|
|
|
|
bool OptimizeBranches(MachineFunction &MF);
|
|
|
|
bool OptimizeBlock(MachineBasicBlock *MBB);
|
|
|
|
void RemoveDeadBlock(MachineBasicBlock *MBB);
|
|
|
|
bool OptimizeImpDefsBlock(MachineBasicBlock *MBB);
|
2011-05-12 08:56:58 +08:00
|
|
|
|
|
|
|
bool HoistCommonCode(MachineFunction &MF);
|
|
|
|
bool HoistCommonCodeInSuccs(MachineBasicBlock *MBB);
|
2009-09-04 15:47:40 +08:00
|
|
|
};
|
2015-06-23 17:49:53 +08:00
|
|
|
}
|
2009-09-04 15:47:40 +08:00
|
|
|
|
|
|
|
#endif /* LLVM_CODEGEN_BRANCHFOLDING_HPP */
|