Skip to content

Commit

Permalink
Merge pull request #9878 from fhahn/taildup
Browse files Browse the repository at this point in the history
[TailDup] Allow large number of predecessors/successors without phis.…
  • Loading branch information
fhahn authored Jan 25, 2025
2 parents 2270d32 + c811c97 commit 578d2a2
Show file tree
Hide file tree
Showing 2 changed files with 516 additions and 8 deletions.
23 changes: 15 additions & 8 deletions llvm/lib/CodeGen/TailDuplicator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -574,14 +574,6 @@ bool TailDuplicator::shouldTailDuplicate(bool IsSimple,
if (TailBB.isSuccessor(&TailBB))
return false;

// Duplicating a BB which has both multiple predecessors and successors will
// result in a complex CFG and also may cause huge amount of PHI nodes. If we
// want to remove this limitation, we have to address
// https://github.com/llvm/llvm-project/issues/78578.
if (TailBB.pred_size() > TailDupPredSize &&
TailBB.succ_size() > TailDupSuccSize)
return false;

// Set the limit on the cost to duplicate. When optimizing for size,
// duplicate only one, because one branch instruction can be eliminated to
// compensate for the duplication.
Expand Down Expand Up @@ -621,6 +613,7 @@ bool TailDuplicator::shouldTailDuplicate(bool IsSimple,
// Check the instructions in the block to determine whether tail-duplication
// is invalid or unlikely to be profitable.
unsigned InstrCount = 0;
unsigned NumPhis = 0;
for (MachineInstr &MI : TailBB) {
// Non-duplicable things shouldn't be tail-duplicated.
// CFI instructions are marked as non-duplicable, because Darwin compact
Expand Down Expand Up @@ -664,6 +657,20 @@ bool TailDuplicator::shouldTailDuplicate(bool IsSimple,

if (InstrCount > MaxDuplicateCount)
return false;
NumPhis += MI.isPHI();
}

// Duplicating a BB which has both multiple predecessors and successors will
// may cause huge amount of PHI nodes. If we want to remove this limitation,
// we have to address https://github.com/llvm/llvm-project/issues/78578.
if (TailBB.pred_size() > TailDupPredSize &&
TailBB.succ_size() > TailDupSuccSize) {
// If TailBB or any of its successors contains a phi, we may have to add a
// large number of additional phis with additional incoming values.
if (NumPhis != 0 || any_of(TailBB.successors(), [](MachineBasicBlock *MBB) {
return any_of(*MBB, [](MachineInstr &MI) { return MI.isPHI(); });
}))
return false;
}

// Check if any of the successors of TailBB has a PHI node in which the
Expand Down
Loading

0 comments on commit 578d2a2

Please sign in to comment.