Skip to content

Commit

Permalink
Branch Folding: Accept explicit threshold for tail merge size.
Browse files Browse the repository at this point in the history
This is prep work for allowing the threshold to be different during layout,
and to enforce a single threshold between merging and duplicating during
layout. No observable change intended.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@279117 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
Kyle Butt committed Aug 18, 2016
1 parent a13b2e5 commit b1ee91e
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 22 deletions.
50 changes: 32 additions & 18 deletions lib/CodeGen/BranchFolding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,12 @@ bool BranchFolderPass::runOnMachineFunction(MachineFunction &MF) {

BranchFolder::BranchFolder(bool defaultEnableTailMerge, bool CommonHoist,
MBFIWrapper &FreqInfo,
const MachineBranchProbabilityInfo &ProbInfo)
: EnableHoistCommonCode(CommonHoist), MBBFreqInfo(FreqInfo),
MBPI(ProbInfo) {
const MachineBranchProbabilityInfo &ProbInfo,
unsigned MinTailLength)
: EnableHoistCommonCode(CommonHoist), MinCommonTailLength(MinTailLength),
MBBFreqInfo(FreqInfo), MBPI(ProbInfo) {
if (MinCommonTailLength == 0)
MinCommonTailLength = TailMergeSize;
switch (FlagEnableTailMerge) {
case cl::BOU_UNSET: EnableTailMerge = defaultEnableTailMerge; break;
case cl::BOU_TRUE: EnableTailMerge = true; break;
Expand Down Expand Up @@ -591,9 +594,21 @@ static unsigned CountTerminators(MachineBasicBlock *MBB,
/// and decide if it would be profitable to merge those tails. Return the
/// length of the common tail and iterators to the first common instruction
/// in each block.
/// MBB1, MBB2 The blocks to check
/// MinCommonTailLength Minimum size of tail block to be merged.
/// CommonTailLen Out parameter to record the size of the shared tail between
/// MBB1 and MBB2
/// I1, I2 Iterator references that will be changed to point to the first
/// instruction in the common tail shared by MBB1,MBB2
/// SuccBB A common successor of MBB1, MBB2 which are in a canonical form
/// relative to SuccBB
/// PredBB The layout predecessor of SuccBB, if any.
/// FuncletMembership map from block to funclet #.
/// AfterPlacement True if we are merging blocks after layout. Stricter
/// thresholds apply to prevent undoing tail-duplication.
static bool
ProfitableToMerge(MachineBasicBlock *MBB1, MachineBasicBlock *MBB2,
unsigned minCommonTailLength, unsigned &CommonTailLen,
unsigned MinCommonTailLength, unsigned &CommonTailLen,
MachineBasicBlock::iterator &I1,
MachineBasicBlock::iterator &I2, MachineBasicBlock *SuccBB,
MachineBasicBlock *PredBB,
Expand Down Expand Up @@ -651,7 +666,7 @@ ProfitableToMerge(MachineBasicBlock *MBB1, MachineBasicBlock *MBB2,
++EffectiveTailLen;

// Check if the common tail is long enough to be worthwhile.
if (EffectiveTailLen >= minCommonTailLength)
if (EffectiveTailLen >= MinCommonTailLength)
return true;

// If we are optimizing for code size, 2 instructions in common is enough if
Expand All @@ -674,7 +689,7 @@ ProfitableToMerge(MachineBasicBlock *MBB1, MachineBasicBlock *MBB2,
/// those blocks appear in MergePotentials (where they are not necessarily
/// consecutive).
unsigned BranchFolder::ComputeSameTails(unsigned CurHash,
unsigned minCommonTailLength,
unsigned MinCommonTailLength,
MachineBasicBlock *SuccBB,
MachineBasicBlock *PredBB) {
unsigned maxCommonTailLength = 0U;
Expand All @@ -687,7 +702,7 @@ unsigned BranchFolder::ComputeSameTails(unsigned CurHash,
for (MPIterator I = std::prev(CurMPIter); I->getHash() == CurHash; --I) {
unsigned CommonTailLen;
if (ProfitableToMerge(CurMPIter->getBlock(), I->getBlock(),
minCommonTailLength,
MinCommonTailLength,
CommonTailLen, TrialBBI1, TrialBBI2,
SuccBB, PredBB,
FuncletMembership,
Expand Down Expand Up @@ -832,14 +847,13 @@ mergeMMOsFromMemoryOperations(MachineBasicBlock::iterator MBBIStartPos,
// branch to Succ added (but the predecessor/successor lists need no
// adjustment). The lone predecessor of Succ that falls through into Succ,
// if any, is given in PredBB.
// MinCommonTailLength - Except for the special cases below, tail-merge if
// there are at least this many instructions in common.
bool BranchFolder::TryTailMergeBlocks(MachineBasicBlock *SuccBB,
MachineBasicBlock *PredBB) {
MachineBasicBlock *PredBB,
unsigned MinCommonTailLength) {
bool MadeChange = false;

// Except for the special cases below, tail-merge if there are at least
// this many instructions in common.
unsigned minCommonTailLength = TailMergeSize;

DEBUG(dbgs() << "\nTryTailMergeBlocks: ";
for (unsigned i = 0, e = MergePotentials.size(); i != e; ++i)
dbgs() << "BB#" << MergePotentials[i].getBlock()->getNumber()
Expand All @@ -852,8 +866,8 @@ bool BranchFolder::TryTailMergeBlocks(MachineBasicBlock *SuccBB,
<< PredBB->getNumber() << "\n";
}
dbgs() << "Looking for common tails of at least "
<< minCommonTailLength << " instruction"
<< (minCommonTailLength == 1 ? "" : "s") << '\n';
<< MinCommonTailLength << " instruction"
<< (MinCommonTailLength == 1 ? "" : "s") << '\n';
);

// Sort by hash value so that blocks with identical end sequences sort
Expand All @@ -867,10 +881,10 @@ bool BranchFolder::TryTailMergeBlocks(MachineBasicBlock *SuccBB,
// Build SameTails, identifying the set of blocks with this hash code
// and with the maximum number of instructions in common.
unsigned maxCommonTailLength = ComputeSameTails(CurHash,
minCommonTailLength,
MinCommonTailLength,
SuccBB, PredBB);

// If we didn't find any pair that has at least minCommonTailLength
// If we didn't find any pair that has at least MinCommonTailLength
// instructions in common, remove all blocks with this hash code and retry.
if (SameTails.empty()) {
RemoveBlocksWithHash(CurHash, SuccBB, PredBB);
Expand Down Expand Up @@ -976,7 +990,7 @@ bool BranchFolder::TailMergeBlocks(MachineFunction &MF) {

// See if we can do any tail merging on those.
if (MergePotentials.size() >= 2)
MadeChange |= TryTailMergeBlocks(nullptr, nullptr);
MadeChange |= TryTailMergeBlocks(nullptr, nullptr, MinCommonTailLength);
}

// Look at blocks (IBB) with multiple predecessors (PBB).
Expand Down Expand Up @@ -1110,7 +1124,7 @@ bool BranchFolder::TailMergeBlocks(MachineFunction &MF) {
TriedMerging.insert(MergePotentials[i].getBlock());

if (MergePotentials.size() >= 2)
MadeChange |= TryTailMergeBlocks(IBB, PredBB);
MadeChange |= TryTailMergeBlocks(IBB, PredBB, MinCommonTailLength);

// Reinsert an unconditional branch if needed. The 1 below can occur as a
// result of removing blocks in TryTailMergeBlocks.
Expand Down
12 changes: 9 additions & 3 deletions lib/CodeGen/BranchFolding.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,13 @@ namespace llvm {
public:
class MBFIWrapper;

explicit BranchFolder(bool defaultEnableTailMerge, bool CommonHoist,
explicit BranchFolder(bool defaultEnableTailMerge,
bool CommonHoist,
MBFIWrapper &MBFI,
const MachineBranchProbabilityInfo &MBPI);
const MachineBranchProbabilityInfo &MBPI,
// Min tail length to merge. Defaults to commandline
// flag. Ignored for optsize.
unsigned MinCommonTailLength = 0);

bool OptimizeFunction(MachineFunction &MF, const TargetInstrInfo *tii,
const TargetRegisterInfo *tri, MachineModuleInfo *mmi,
Expand Down Expand Up @@ -99,6 +103,7 @@ namespace llvm {
bool EnableTailMerge;
bool EnableHoistCommonCode;
bool UpdateLiveIns;
unsigned MinCommonTailLength;
const TargetInstrInfo *TII;
const TargetRegisterInfo *TRI;
MachineModuleInfo *MMI;
Expand Down Expand Up @@ -129,7 +134,8 @@ namespace llvm {

bool TailMergeBlocks(MachineFunction &MF);
bool TryTailMergeBlocks(MachineBasicBlock* SuccBB,
MachineBasicBlock* PredBB);
MachineBasicBlock* PredBB,
unsigned MinCommonTailLength);
void setCommonTailEdgeWeights(MachineBasicBlock &TailMBB);
void computeLiveIns(MachineBasicBlock &MBB);
void ReplaceTailWithBranchTo(MachineBasicBlock::iterator OldInst,
Expand Down
4 changes: 3 additions & 1 deletion lib/CodeGen/MachineBlockPlacement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1747,8 +1747,10 @@ bool MachineBlockPlacement::runOnMachineFunction(MachineFunction &MF) {
BranchFoldPlacement;
// No tail merging opportunities if the block number is less than four.
if (MF.size() > 3 && EnableTailMerge) {
// Default to the standard tail-merge-size option.
unsigned TailMergeSize = 0;
BranchFolder BF(/*EnableTailMerge=*/true, /*CommonHoist=*/false, *MBFI,
*MBPI);
*MBPI, TailMergeSize);

if (BF.OptimizeFunction(MF, TII, MF.getSubtarget().getRegisterInfo(),
getAnalysisIfAvailable<MachineModuleInfo>(), MLI,
Expand Down

0 comments on commit b1ee91e

Please sign in to comment.