[llvm] 9f8f6ce - [SROA] Avoid expensive isComplete() call (NFC)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Tue Jul 2 07:42:05 PDT 2024


Author: Nikita Popov
Date: 2024-07-02T16:41:56+02:00
New Revision: 9f8f6ce53cb7bc3f139db7b8f614a4f0e9a1b579

URL: https://github.com/llvm/llvm-project/commit/9f8f6ce53cb7bc3f139db7b8f614a4f0e9a1b579
DIFF: https://github.com/llvm/llvm-project/commit/9f8f6ce53cb7bc3f139db7b8f614a4f0e9a1b579.diff

LOG: [SROA] Avoid expensive isComplete() call (NFC)

https://github.com/llvm/llvm-project/pull/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.

Added: 
    

Modified: 
    llvm/lib/Transforms/Utils/PromoteMemoryToRegister.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/Utils/PromoteMemoryToRegister.cpp b/llvm/lib/Transforms/Utils/PromoteMemoryToRegister.cpp
index d2d50e5359878..6e021a5e2d05a 100644
--- a/llvm/lib/Transforms/Utils/PromoteMemoryToRegister.cpp
+++ b/llvm/lib/Transforms/Utils/PromoteMemoryToRegister.cpp
@@ -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)
@@ -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];
 
@@ -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.


        


More information about the llvm-commits mailing list