[FuncSpec] Save compilation time by caching uses for propagation
authorBin Cheng <bin.cheng@linux.alibaba.com>
Tue, 15 Feb 2022 08:51:04 +0000 (16:51 +0800)
committerBin Cheng <bin.cheng@linux.alibaba.com>
Wed, 16 Feb 2022 02:46:26 +0000 (10:46 +0800)
We only need to do propagation on use instructions of the original
value, rather than the replacing const value which might have lots
of irrelavant uses.  This is done by caching uses before replacing.

Differential Revision: https://reviews.llvm.org/D119815

llvm/lib/Transforms/IPO/FunctionSpecialization.cpp

index 9b6246b..832fa46 100644 (file)
@@ -344,12 +344,17 @@ public:
     LLVM_DEBUG(dbgs() << "FnSpecialization: Replacing " << *V
                       << "\nFnSpecialization: with " << *Const << "\n");
 
-    V->replaceAllUsesWith(Const);
-
-    for (auto *U : Const->users())
+    // Record uses of V to avoid visiting irrelevant uses of const later.
+    SmallVector<Instruction *> UseInsts;
+    for (auto *U : V->users())
       if (auto *I = dyn_cast<Instruction>(U))
         if (Solver.isBlockExecutable(I->getParent()))
-          Solver.visit(I);
+          UseInsts.push_back(I);
+
+    V->replaceAllUsesWith(Const);
+
+    for (auto *I : UseInsts)
+      Solver.visit(I);
 
     // Remove the instruction from Block and Solver.
     if (auto *I = dyn_cast<Instruction>(V)) {