2013-12-22 00:04:03 +00:00
|
|
|
//===-- BranchFolding.h - Fold machine code branch instructions -*- C++ -*-===//
|
2009-10-14 17:57:32 +00:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_CODEGEN_BRANCHFOLDING_HPP
|
|
|
|
#define LLVM_CODEGEN_BRANCHFOLDING_HPP
|
|
|
|
|
2011-07-17 15:36:56 +00:00
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
2009-10-14 17:57:32 +00:00
|
|
|
#include "llvm/CodeGen/MachineBasicBlock.h"
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
class MachineFunction;
|
|
|
|
class MachineModuleInfo;
|
|
|
|
class RegScavenger;
|
|
|
|
class TargetInstrInfo;
|
|
|
|
class TargetRegisterInfo;
|
|
|
|
|
|
|
|
class BranchFolder {
|
|
|
|
public:
|
2011-06-12 15:42:51 +00:00
|
|
|
explicit BranchFolder(bool defaultEnableTailMerge, bool CommonHoist);
|
2009-10-14 17:57:32 +00:00
|
|
|
|
|
|
|
bool OptimizeFunction(MachineFunction &MF,
|
|
|
|
const TargetInstrInfo *tii,
|
|
|
|
const TargetRegisterInfo *tri,
|
|
|
|
MachineModuleInfo *mmi);
|
|
|
|
private:
|
2009-11-18 14:58:34 +00: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-10-14 17:57:32 +00:00
|
|
|
typedef std::vector<MergePotentialsElt>::iterator MPIterator;
|
|
|
|
std::vector<MergePotentialsElt> MergePotentials;
|
2011-07-17 15:36:56 +00:00
|
|
|
SmallPtrSet<const MachineBasicBlock*, 2> TriedMerging;
|
2009-10-14 17:57:32 +00:00
|
|
|
|
2009-11-18 14:58:34 +00: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-10-14 17:57:32 +00:00
|
|
|
std::vector<SameTailElt> SameTails;
|
|
|
|
|
|
|
|
bool EnableTailMerge;
|
2011-06-12 15:42:51 +00:00
|
|
|
bool EnableHoistCommonCode;
|
2009-10-14 17:57:32 +00:00
|
|
|
const TargetInstrInfo *TII;
|
|
|
|
const TargetRegisterInfo *TRI;
|
|
|
|
MachineModuleInfo *MMI;
|
|
|
|
RegScavenger *RS;
|
|
|
|
|
|
|
|
bool TailMergeBlocks(MachineFunction &MF);
|
2009-11-18 14:58:34 +00:00
|
|
|
bool TryTailMergeBlocks(MachineBasicBlock* SuccBB,
|
|
|
|
MachineBasicBlock* PredBB);
|
2011-07-17 15:36:56 +00:00
|
|
|
void MaintainLiveIns(MachineBasicBlock *CurMBB,
|
|
|
|
MachineBasicBlock *NewMBB);
|
2009-10-14 17:57:32 +00:00
|
|
|
void ReplaceTailWithBranchTo(MachineBasicBlock::iterator OldInst,
|
|
|
|
MachineBasicBlock *NewDest);
|
|
|
|
MachineBasicBlock *SplitMBBAt(MachineBasicBlock &CurMBB,
|
2013-12-22 00:04:03 +00:00
|
|
|
MachineBasicBlock::iterator BBI1,
|
|
|
|
const BasicBlock *BB);
|
2009-11-18 14:58:34 +00:00
|
|
|
unsigned ComputeSameTails(unsigned CurHash, unsigned minCommonTailLength,
|
|
|
|
MachineBasicBlock *SuccBB,
|
|
|
|
MachineBasicBlock *PredBB);
|
2009-10-14 17:57:32 +00:00
|
|
|
void RemoveBlocksWithHash(unsigned CurHash, MachineBasicBlock* SuccBB,
|
|
|
|
MachineBasicBlock* PredBB);
|
2010-07-13 17:19:57 +00:00
|
|
|
bool CreateCommonTailOnlyBlock(MachineBasicBlock *&PredBB,
|
2013-12-22 00:04:03 +00:00
|
|
|
MachineBasicBlock *SuccBB,
|
2010-07-13 17:19:57 +00:00
|
|
|
unsigned maxCommonTailLength,
|
|
|
|
unsigned &commonTailIndex);
|
2009-10-14 17:57:32 +00:00
|
|
|
|
|
|
|
bool OptimizeBranches(MachineFunction &MF);
|
|
|
|
bool OptimizeBlock(MachineBasicBlock *MBB);
|
|
|
|
void RemoveDeadBlock(MachineBasicBlock *MBB);
|
|
|
|
bool OptimizeImpDefsBlock(MachineBasicBlock *MBB);
|
2011-06-12 15:42:51 +00:00
|
|
|
|
|
|
|
bool HoistCommonCode(MachineFunction &MF);
|
|
|
|
bool HoistCommonCodeInSuccs(MachineBasicBlock *MBB);
|
2009-10-14 17:57:32 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* LLVM_CODEGEN_BRANCHFOLDING_HPP */
|