1 //===- llvm/Analysis/AssumptionCache.h - Track @llvm.assume ---*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains a pass that keeps track of @llvm.assume intrinsics in
11 // the functions of a module (allowing assumptions within any function to be
12 // found cheaply by other parts of the optimizer).
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_ANALYSIS_ASSUMPTIONCACHE_H
17 #define LLVM_ANALYSIS_ASSUMPTIONCACHE_H
19 #include "llvm/ADT/ArrayRef.h"
20 #include "llvm/ADT/DenseMap.h"
21 #include "llvm/ADT/SmallSet.h"
22 #include "llvm/IR/Function.h"
23 #include "llvm/IR/Instructions.h"
24 #include "llvm/IR/ValueHandle.h"
25 #include "llvm/Pass.h"
30 /// \brief A cache of @llvm.assume calls within a function.
32 /// This cache provides fast lookup of assumptions within a function by caching
33 /// them and amortizing the cost of scanning for them across all queries. The
34 /// cache is also conservatively self-updating so that it will never return
35 /// incorrect results about a function even as the function is being mutated.
36 /// However, flushing the cache and rebuilding it (or explicitly updating it)
37 /// may allow it to discover new assumptions.
38 class AssumptionCache {
39 /// \brief The function for which this cache is handling assumptions.
41 /// We track this to lazily populate our assumptions.
44 /// \brief Vector of weak value handles to calls of the @llvm.assume
46 SmallVector<WeakVH, 4> AssumeHandles;
48 /// \brief Flag tracking whether we have scanned the function yet.
50 /// We want to be as lazy about this as possible, and so we scan the function
51 /// at the last moment.
54 /// \brief Scan the function for assumptions and add them to the cache.
58 /// \brief Construct an AssumptionCache from a function by scanning all of
60 AssumptionCache(Function &F) : F(F), Scanned(false) {}
62 /// \brief Add an @llvm.assume intrinsic to this function's cache.
64 /// The call passed in must be an instruction within this fuction and must
65 /// not already be in the cache.
66 void registerAssumption(CallInst *CI);
68 /// \brief Clear the cache of @llvm.assume intrinsics for a function.
70 /// It will be re-scanned the next time it is requested.
72 AssumeHandles.clear();
76 /// \brief Access the list of assumption handles currently tracked for this
79 /// Note that these produce weak handles that may be null. The caller must
81 /// FIXME: We should replace this with pointee_iterator<filter_iterator<...>>
82 /// when we can write that to filter out the null values. Then caller code
83 /// will become simpler.
84 MutableArrayRef<WeakVH> assumptions() {
91 /// \brief An immutable pass that tracks lazily created \c AssumptionCache
94 /// This is essentially a workaround for the legacy pass manager's weaknesses
95 /// which associates each assumption cache with Function and clears it if the
96 /// function is deleted. The nature of the AssumptionCache is that it is not
97 /// invalidated by any changes to the function body and so this is sufficient
98 /// to be conservatively correct.
99 class AssumptionCacheTracker : public ImmutablePass {
100 /// A callback value handle applied to function objects, which we use to
101 /// delete our cache of intrinsics for a function when it is deleted.
102 class FunctionCallbackVH : public CallbackVH {
103 AssumptionCacheTracker *ACT;
104 void deleted() override;
107 typedef DenseMapInfo<Value *> DMI;
109 FunctionCallbackVH(Value *V, AssumptionCacheTracker *ACT = nullptr)
110 : CallbackVH(V), ACT(ACT) {}
113 friend FunctionCallbackVH;
115 typedef DenseMap<FunctionCallbackVH, std::unique_ptr<AssumptionCache>,
116 FunctionCallbackVH::DMI> FunctionCallsMap;
117 FunctionCallsMap AssumptionCaches;
120 /// \brief Get the cached assumptions for a function.
122 /// If no assumptions are cached, this will scan the function. Otherwise, the
123 /// existing cache will be returned.
124 AssumptionCache &getAssumptionCache(Function &F);
126 AssumptionCacheTracker();
127 ~AssumptionCacheTracker();
129 void releaseMemory() override { AssumptionCaches.shrink_and_clear(); }
131 void verifyAnalysis() const override;
132 bool doFinalization(Module &) override {
137 static char ID; // Pass identification, replacement for typeid
140 } // end namespace llvm