Skip to content

Commit

Permalink
[SROA] Avoid expensive isComplete() call (NFC)
Browse files Browse the repository at this point in the history
llvm#83381 introduced a call
to PHINode::isComplete() in Mem2Reg, which is O(n^2) in the number
of predecessors, resulting in pathological compile-time regressions
for cases with many predecessors.

Remove the isComplete() check and instead cache the attribute
lookup, to only perform it once per function. Actually setting
the FMF flag is cheap.
  • Loading branch information
nikic committed Jul 2, 2024
1 parent a0ab0ca commit 9f8f6ce
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions llvm/lib/Transforms/Utils/PromoteMemoryToRegister.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,9 @@ struct PromoteMem2Reg {
/// Lazily compute the number of predecessors a block has.
DenseMap<const BasicBlock *, unsigned> BBNumPreds;

/// Whether the function has the no-signed-zeros-fp-math attribute set.
bool NoSignedZeros = false;

public:
PromoteMem2Reg(ArrayRef<AllocaInst *> Allocas, DominatorTree &DT,
AssumptionCache *AC)
Expand Down Expand Up @@ -740,6 +743,8 @@ void PromoteMem2Reg::run() {
LargeBlockInfo LBI;
ForwardIDFCalculator IDF(DT);

NoSignedZeros = F.getFnAttribute("no-signed-zeros-fp-math").getValueAsBool();

for (unsigned AllocaNum = 0; AllocaNum != Allocas.size(); ++AllocaNum) {
AllocaInst *AI = Allocas[AllocaNum];

Expand Down Expand Up @@ -1128,10 +1133,7 @@ void PromoteMem2Reg::RenamePass(BasicBlock *BB, BasicBlock *Pred,
// on the phi node generated at this stage, fabs folding does not
// happen. So, we try to infer nsz flag from the function attributes to
// enable this fabs folding.
if (APN->isComplete() && isa<FPMathOperator>(APN) &&
BB->getParent()
->getFnAttribute("no-signed-zeros-fp-math")
.getValueAsBool())
if (isa<FPMathOperator>(APN) && NoSignedZeros)
APN->setHasNoSignedZeros(true);

// The currently active variable for this block is now the PHI.
Expand Down

0 comments on commit 9f8f6ce

Please sign in to comment.