[llvm] r226868 - [PM] Actually add the new pass manager support for the assumption cache.

Chandler Carruth chandlerc at gmail.com
Thu Jan 22 13:53:09 PST 2015


Author: chandlerc
Date: Thu Jan 22 15:53:09 2015
New Revision: 226868

URL: http://llvm.org/viewvc/llvm-project?rev=226868&view=rev
Log:
[PM] Actually add the new pass manager support for the assumption cache.

I had already factored this analysis specifically to enable doing this,
but hadn't actually committed the necessary wiring to get at this from
the new pass manager. This also nicely shows how the separate cache
object can be directly managed by the new pass manager.

This analysis didn't have any direct tests and so I've added a printer
pass and a boring test case. I chose to print the i1 value which is
being assumed rather than the call to llvm.assume as that seems much
more useful for testing... but suggestions on an even better printing
strategy welcome. My main goal was to make sure things actually work. =]

Added:
    llvm/trunk/test/Analysis/AssumptionCache/
    llvm/trunk/test/Analysis/AssumptionCache/basic.ll
Modified:
    llvm/trunk/include/llvm/Analysis/AssumptionCache.h
    llvm/trunk/lib/Analysis/AssumptionCache.cpp
    llvm/trunk/tools/opt/PassRegistry.def
    llvm/trunk/tools/opt/Passes.cpp

Modified: llvm/trunk/include/llvm/Analysis/AssumptionCache.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Analysis/AssumptionCache.h?rev=226868&r1=226867&r2=226868&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Analysis/AssumptionCache.h (original)
+++ llvm/trunk/include/llvm/Analysis/AssumptionCache.h Thu Jan 22 15:53:09 2015
@@ -27,6 +27,11 @@
 
 namespace llvm {
 
+// FIXME: Replace this brittle forward declaration with the include of the new
+// PassManager.h when doing so doesn't break the PassManagerBuilder.
+template <typename IRUnitT> class AnalysisManager;
+class PreservedAnalyses;
+
 /// \brief A cache of @llvm.assume calls within a function.
 ///
 /// This cache provides fast lookup of assumptions within a function by caching
@@ -88,6 +93,42 @@ public:
   }
 };
 
+/// \brief A function analysis which provides an \c AssumptionCache.
+///
+/// This analysis is intended for use with the new pass manager and will vend
+/// assumption caches for a given function.
+class AssumptionAnalysis {
+  static char PassID;
+
+public:
+  typedef AssumptionCache Result;
+
+  /// \brief Opaque, unique identifier for this analysis pass.
+  static void *ID() { return (void *)&PassID; }
+
+  /// \brief Provide a name for the analysis for debugging and logging.
+  static StringRef name() { return "AssumptionAnalysis"; }
+
+  AssumptionAnalysis() {}
+  AssumptionAnalysis(const AssumptionAnalysis &Arg) {}
+  AssumptionAnalysis(AssumptionAnalysis &&Arg) {}
+  AssumptionAnalysis &operator=(const AssumptionAnalysis &RHS) { return *this; }
+  AssumptionAnalysis &operator=(AssumptionAnalysis &&RHS) { return *this; }
+
+  AssumptionCache run(Function &F) { return AssumptionCache(F); }
+};
+
+/// \brief Printer pass for the \c AssumptionAnalysis results.
+class AssumptionPrinterPass {
+  raw_ostream &OS;
+
+public:
+  explicit AssumptionPrinterPass(raw_ostream &OS) : OS(OS) {}
+  PreservedAnalyses run(Function &F, AnalysisManager<Function> *AM);
+
+  static StringRef name() { return "AssumptionPrinterPass"; }
+};
+
 /// \brief An immutable pass that tracks lazily created \c AssumptionCache
 /// objects.
 ///

Modified: llvm/trunk/lib/Analysis/AssumptionCache.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/AssumptionCache.cpp?rev=226868&r1=226867&r2=226868&view=diff
==============================================================================
--- llvm/trunk/lib/Analysis/AssumptionCache.cpp (original)
+++ llvm/trunk/lib/Analysis/AssumptionCache.cpp Thu Jan 22 15:53:09 2015
@@ -18,6 +18,7 @@
 #include "llvm/IR/Function.h"
 #include "llvm/IR/Instructions.h"
 #include "llvm/IR/IntrinsicInst.h"
+#include "llvm/IR/PassManager.h"
 #include "llvm/IR/PatternMatch.h"
 #include "llvm/Support/Debug.h"
 using namespace llvm;
@@ -73,6 +74,20 @@ void AssumptionCache::registerAssumption
 #endif
 }
 
+char AssumptionAnalysis::PassID;
+
+PreservedAnalyses AssumptionPrinterPass::run(Function &F,
+                                             AnalysisManager<Function> *AM) {
+  AssumptionCache &AC = AM->getResult<AssumptionAnalysis>(F);
+
+  OS << "Cached assumptions for function: " << F.getName() << "\n";
+  for (auto &VH : AC.assumptions())
+    if (VH)
+      OS << "  " << *cast<CallInst>(VH)->getArgOperand(0) << "\n";
+
+  return PreservedAnalyses::all();
+}
+
 void AssumptionCacheTracker::FunctionCallbackVH::deleted() {
   auto I = ACT->AssumptionCaches.find_as(cast<Function>(getValPtr()));
   if (I != ACT->AssumptionCaches.end())

Added: llvm/trunk/test/Analysis/AssumptionCache/basic.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Analysis/AssumptionCache/basic.ll?rev=226868&view=auto
==============================================================================
--- llvm/trunk/test/Analysis/AssumptionCache/basic.ll (added)
+++ llvm/trunk/test/Analysis/AssumptionCache/basic.ll Thu Jan 22 15:53:09 2015
@@ -0,0 +1,22 @@
+; RUN: opt < %s -disable-output -passes='print<assumptions>' 2>&1 | FileCheck %s
+
+target datalayout = "e-i64:64-f80:128-n8:16:32:64-S128"
+
+declare void @llvm.assume(i1)
+
+define void @test1(i32 %a) {
+; CHECK-LABEL: Cached assumptions for function: test1
+; CHECK-NEXT: icmp ne i32 %{{.*}}, 0
+; CHECK-NEXT: icmp slt i32 %{{.*}}, 0
+; CHECK-NEXT: icmp sgt i32 %{{.*}}, 0
+
+entry:
+  %cond1 = icmp ne i32 %a, 0
+  call void @llvm.assume(i1 %cond1)
+  %cond2 = icmp slt i32 %a, 0
+  call void @llvm.assume(i1 %cond2)
+  %cond3 = icmp sgt i32 %a, 0
+  call void @llvm.assume(i1 %cond3)
+
+  ret void
+}

Modified: llvm/trunk/tools/opt/PassRegistry.def
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/opt/PassRegistry.def?rev=226868&r1=226867&r2=226868&view=diff
==============================================================================
--- llvm/trunk/tools/opt/PassRegistry.def (original)
+++ llvm/trunk/tools/opt/PassRegistry.def Thu Jan 22 15:53:09 2015
@@ -50,6 +50,7 @@ CGSCC_PASS("no-op-cgscc", NoOpCGSCCPass(
 #ifndef FUNCTION_ANALYSIS
 #define FUNCTION_ANALYSIS(NAME, CREATE_PASS)
 #endif
+FUNCTION_ANALYSIS("assumptions", AssumptionAnalysis())
 FUNCTION_ANALYSIS("domtree", DominatorTreeAnalysis())
 FUNCTION_ANALYSIS("loops", LoopAnalysis())
 FUNCTION_ANALYSIS("no-op-function", NoOpFunctionAnalysis())
@@ -61,6 +62,7 @@ FUNCTION_ANALYSIS("no-op-function", NoOp
 FUNCTION_PASS("invalidate<all>", InvalidateAllAnalysesPass())
 FUNCTION_PASS("no-op-function", NoOpFunctionPass())
 FUNCTION_PASS("print", PrintFunctionPass(dbgs()))
+FUNCTION_PASS("print<assumptions>", AssumptionPrinterPass(dbgs()))
 FUNCTION_PASS("print<domtree>", DominatorTreePrinterPass(dbgs()))
 FUNCTION_PASS("print<loops>", LoopPrinterPass(dbgs()))
 FUNCTION_PASS("verify", VerifierPass())

Modified: llvm/trunk/tools/opt/Passes.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/opt/Passes.cpp?rev=226868&r1=226867&r2=226868&view=diff
==============================================================================
--- llvm/trunk/tools/opt/Passes.cpp (original)
+++ llvm/trunk/tools/opt/Passes.cpp Thu Jan 22 15:53:09 2015
@@ -15,6 +15,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "Passes.h"
+#include "llvm/Analysis/AssumptionCache.h"
 #include "llvm/Analysis/CGSCCPassManager.h"
 #include "llvm/Analysis/LazyCallGraph.h"
 #include "llvm/Analysis/LoopInfo.h"





More information about the llvm-commits mailing list