[llvm] r249685 - Make demanded bits lazy

James Molloy via llvm-commits llvm-commits at lists.llvm.org
Thu Oct 8 05:39:50 PDT 2015


Author: jamesm
Date: Thu Oct  8 07:39:50 2015
New Revision: 249685

URL: http://llvm.org/viewvc/llvm-project?rev=249685&view=rev
Log:
Make demanded bits lazy

The algorithm itself is still eager, but it doesn't get run until a
query function is called. This greatly reduces the compile-time impact
of requiring DemandedBits when at runtime it is not often used.

NFCI.

Modified:
    llvm/trunk/include/llvm/Analysis/DemandedBits.h
    llvm/trunk/lib/Analysis/DemandedBits.cpp

Modified: llvm/trunk/include/llvm/Analysis/DemandedBits.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Analysis/DemandedBits.h?rev=249685&r1=249684&r2=249685&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Analysis/DemandedBits.h (original)
+++ llvm/trunk/include/llvm/Analysis/DemandedBits.h Thu Oct  8 07:39:50 2015
@@ -49,6 +49,7 @@ struct DemandedBits : public FunctionPas
   bool isInstructionDead(Instruction *I);
 
 private:
+  void performAnalysis();
   void determineLiveOperandBits(const Instruction *UserI,
                                 const Instruction *I, unsigned OperandNo,
                                 const APInt &AOut, APInt &AB,
@@ -57,6 +58,8 @@ private:
 
   AssumptionCache *AC;
   DominatorTree *DT;
+  Function *F;
+  bool Analyzed;
 
   // The set of visited instructions (non-integer-typed only).
   SmallPtrSet<Instruction*, 128> Visited;

Modified: llvm/trunk/lib/Analysis/DemandedBits.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/DemandedBits.cpp?rev=249685&r1=249684&r2=249685&view=diff
==============================================================================
--- llvm/trunk/lib/Analysis/DemandedBits.cpp (original)
+++ llvm/trunk/lib/Analysis/DemandedBits.cpp Thu Oct  8 07:39:50 2015
@@ -51,7 +51,7 @@ INITIALIZE_PASS_DEPENDENCY(DominatorTree
 INITIALIZE_PASS_END(DemandedBits, "demanded-bits", "Demanded bits analysis",
                     false, false)
 
-DemandedBits::DemandedBits() : FunctionPass(ID) {
+DemandedBits::DemandedBits() : FunctionPass(ID), F(nullptr), Analyzed(false) {
   initializeDemandedBitsPass(*PassRegistry::getPassRegistry());
 }
 
@@ -243,17 +243,27 @@ void DemandedBits::determineLiveOperandB
   }
 }
 
-bool DemandedBits::runOnFunction(Function& F) {
-  AC = &getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
-  DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
+bool DemandedBits::runOnFunction(Function& Fn) {
+  F = &Fn;
+  Analyzed = false;
+  return false;
+}
 
+void DemandedBits::performAnalysis() {
+  if (Analyzed)
+    // Analysis already completed for this function.
+    return;
+  Analyzed = true;
+  AC = &getAnalysis<AssumptionCacheTracker>().getAssumptionCache(*F);
+  DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
+  
   Visited.clear();
   AliveBits.clear();
 
   SmallVector<Instruction*, 128> Worklist;
 
   // Collect the set of "root" instructions that are known live.
-  for (Instruction &I : instructions(F)) {
+  for (Instruction &I : instructions(*F)) {
     if (!isAlwaysLive(&I))
       continue;
 
@@ -340,11 +350,11 @@ bool DemandedBits::runOnFunction(Functio
       }
     }
   }
-
-  return false;
 }
 
 APInt DemandedBits::getDemandedBits(Instruction *I) {
+  performAnalysis();
+  
   const DataLayout &DL = I->getParent()->getModule()->getDataLayout();
   if (AliveBits.count(I))
     return AliveBits[I];
@@ -352,6 +362,8 @@ APInt DemandedBits::getDemandedBits(Inst
 }
 
 bool DemandedBits::isInstructionDead(Instruction *I) {
+  performAnalysis();
+
   return !Visited.count(I) && AliveBits.find(I) == AliveBits.end() &&
     !isAlwaysLive(I);
 }




More information about the llvm-commits mailing list